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