Luca Versari | 99fddff | 2022-05-25 10:22:32 -0700 | [diff] [blame] | 1 | // Part of the Crubit project, under the Apache License v2.0 with LLVM |
| 2 | // Exceptions. See /LICENSE for license information. |
| 3 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 4 | |
| 5 | #include "lifetime_analysis/lifetime_lattice.h" |
| 6 | |
| 7 | #include <assert.h> |
| 8 | |
| 9 | #include <string> |
| 10 | #include <tuple> |
| 11 | #include <utility> |
| 12 | |
Luca Versari | 53a2f58 | 2023-01-16 10:09:29 -0800 | [diff] [blame] | 13 | #include "lifetime_analysis/lifetime_constraints.h" |
| 14 | #include "lifetime_analysis/object_set.h" |
| 15 | #include "lifetime_analysis/points_to_map.h" |
Luca Versari | 99fddff | 2022-05-25 10:22:32 -0700 | [diff] [blame] | 16 | #include "clang/Analysis/FlowSensitive/DataflowLattice.h" |
Luca Versari | 99fddff | 2022-05-25 10:22:32 -0700 | [diff] [blame] | 17 | |
| 18 | namespace clang { |
| 19 | namespace tidy { |
| 20 | namespace lifetimes { |
| 21 | |
| 22 | std::string LifetimeLattice::ToString() const { |
| 23 | if (IsError()) { |
| 24 | return Error().str(); |
| 25 | } |
| 26 | return PointsTo().DebugString(); |
| 27 | } |
| 28 | |
| 29 | PointsToMap& LifetimeLattice::PointsTo() { |
Luca Versari | 53a2f58 | 2023-01-16 10:09:29 -0800 | [diff] [blame] | 30 | return std::get<PointsToMap>(std::get<0>(var_)); |
Luca Versari | 99fddff | 2022-05-25 10:22:32 -0700 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | const PointsToMap& LifetimeLattice::PointsTo() const { |
Luca Versari | 53a2f58 | 2023-01-16 10:09:29 -0800 | [diff] [blame] | 34 | return std::get<PointsToMap>(std::get<0>(var_)); |
Luca Versari | 8122285 | 2022-08-05 06:36:10 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | LifetimeConstraints& LifetimeLattice::Constraints() { |
Luca Versari | 53a2f58 | 2023-01-16 10:09:29 -0800 | [diff] [blame] | 38 | return std::get<LifetimeConstraints>(std::get<0>(var_)); |
Luca Versari | 8122285 | 2022-08-05 06:36:10 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | const LifetimeConstraints& LifetimeLattice::Constraints() const { |
Luca Versari | 53a2f58 | 2023-01-16 10:09:29 -0800 | [diff] [blame] | 42 | return std::get<LifetimeConstraints>(std::get<0>(var_)); |
| 43 | } |
| 44 | |
| 45 | ObjectSet& LifetimeLattice::SingleValuedObjects() { |
| 46 | return std::get<ObjectSet>(std::get<0>(var_)); |
| 47 | } |
| 48 | |
| 49 | const ObjectSet& LifetimeLattice::SingleValuedObjects() const { |
| 50 | return std::get<ObjectSet>(std::get<0>(var_)); |
Luca Versari | 99fddff | 2022-05-25 10:22:32 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | llvm::StringRef LifetimeLattice::Error() const { |
Luca Versari | 99fddff | 2022-05-25 10:22:32 -0700 | [diff] [blame] | 54 | return std::get<std::string>(var_); |
| 55 | } |
| 56 | |
| 57 | clang::dataflow::LatticeJoinEffect LifetimeLattice::join( |
| 58 | const LifetimeLattice& other) { |
| 59 | if (IsError()) { |
| 60 | return clang::dataflow::LatticeJoinEffect::Unchanged; |
| 61 | } |
| 62 | if (other.IsError()) { |
| 63 | *this = other; |
| 64 | return clang::dataflow::LatticeJoinEffect::Changed; |
| 65 | } |
| 66 | |
Luca Versari | 53a2f58 | 2023-01-16 10:09:29 -0800 | [diff] [blame] | 67 | auto effect = Constraints().join(other.Constraints()); |
Luca Versari | 8122285 | 2022-08-05 06:36:10 -0700 | [diff] [blame] | 68 | |
Luca Versari | 99fddff | 2022-05-25 10:22:32 -0700 | [diff] [blame] | 69 | PointsToMap joined_points_to_map = PointsTo().Union(other.PointsTo()); |
Luca Versari | 53a2f58 | 2023-01-16 10:09:29 -0800 | [diff] [blame] | 70 | if (PointsTo() != joined_points_to_map) { |
| 71 | PointsTo() = std::move(joined_points_to_map); |
| 72 | effect = clang::dataflow::LatticeJoinEffect::Changed; |
Luca Versari | 99fddff | 2022-05-25 10:22:32 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Luca Versari | 53a2f58 | 2023-01-16 10:09:29 -0800 | [diff] [blame] | 75 | ObjectSet joined_single_valued_objects = |
| 76 | SingleValuedObjects().Intersection(other.SingleValuedObjects()); |
| 77 | if (SingleValuedObjects() != joined_single_valued_objects) { |
| 78 | SingleValuedObjects() = std::move(joined_single_valued_objects); |
| 79 | effect = clang::dataflow::LatticeJoinEffect::Changed; |
| 80 | } |
| 81 | |
| 82 | return effect; |
Luca Versari | 99fddff | 2022-05-25 10:22:32 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | bool LifetimeLattice::operator==(const LifetimeLattice& other) const { |
| 86 | if (IsError() || other.IsError()) { |
| 87 | // Any error compares equal to any other error. |
| 88 | return IsError() && other.IsError(); |
| 89 | } |
Luca Versari | 91a56ff | 2022-08-22 01:58:33 -0700 | [diff] [blame] | 90 | return PointsTo() == other.PointsTo() && Constraints() == other.Constraints(); |
Luca Versari | 99fddff | 2022-05-25 10:22:32 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | } // namespace lifetimes |
| 94 | } // namespace tidy |
| 95 | } // namespace clang |