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 | |
| 13 | #include "clang/Analysis/FlowSensitive/DataflowLattice.h" |
| 14 | #include "llvm/Support/ErrorHandling.h" |
| 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 | 8122285 | 2022-08-05 06:36:10 -0700 | [diff] [blame] | 28 | return std::get<0>(var_).first; |
Luca Versari | 99fddff | 2022-05-25 10:22:32 -0700 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | const PointsToMap& LifetimeLattice::PointsTo() const { |
Luca Versari | 8122285 | 2022-08-05 06:36:10 -0700 | [diff] [blame] | 32 | return std::get<0>(var_).first; |
| 33 | } |
| 34 | |
| 35 | LifetimeConstraints& LifetimeLattice::Constraints() { |
Luca Versari | 8122285 | 2022-08-05 06:36:10 -0700 | [diff] [blame] | 36 | return std::get<0>(var_).second; |
| 37 | } |
| 38 | |
| 39 | const LifetimeConstraints& LifetimeLattice::Constraints() const { |
Luca Versari | 8122285 | 2022-08-05 06:36:10 -0700 | [diff] [blame] | 40 | return std::get<0>(var_).second; |
Luca Versari | 99fddff | 2022-05-25 10:22:32 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | llvm::StringRef LifetimeLattice::Error() const { |
Luca Versari | 99fddff | 2022-05-25 10:22:32 -0700 | [diff] [blame] | 44 | return std::get<std::string>(var_); |
| 45 | } |
| 46 | |
| 47 | clang::dataflow::LatticeJoinEffect LifetimeLattice::join( |
| 48 | const LifetimeLattice& other) { |
| 49 | if (IsError()) { |
| 50 | return clang::dataflow::LatticeJoinEffect::Unchanged; |
| 51 | } |
| 52 | if (other.IsError()) { |
| 53 | *this = other; |
| 54 | return clang::dataflow::LatticeJoinEffect::Changed; |
| 55 | } |
| 56 | |
Luca Versari | 8122285 | 2022-08-05 06:36:10 -0700 | [diff] [blame] | 57 | auto constraints_effect = Constraints().join(other.Constraints()); |
| 58 | |
Luca Versari | 99fddff | 2022-05-25 10:22:32 -0700 | [diff] [blame] | 59 | PointsToMap joined_points_to_map = PointsTo().Union(other.PointsTo()); |
Luca Versari | 8122285 | 2022-08-05 06:36:10 -0700 | [diff] [blame] | 60 | if (PointsTo() == joined_points_to_map && |
| 61 | constraints_effect == clang::dataflow::LatticeJoinEffect::Unchanged) { |
Luca Versari | 99fddff | 2022-05-25 10:22:32 -0700 | [diff] [blame] | 62 | return clang::dataflow::LatticeJoinEffect::Unchanged; |
| 63 | } |
| 64 | |
Luca Versari | 8122285 | 2022-08-05 06:36:10 -0700 | [diff] [blame] | 65 | PointsTo() = std::move(joined_points_to_map); |
Luca Versari | 99fddff | 2022-05-25 10:22:32 -0700 | [diff] [blame] | 66 | return clang::dataflow::LatticeJoinEffect::Changed; |
| 67 | } |
| 68 | |
| 69 | bool LifetimeLattice::operator==(const LifetimeLattice& other) const { |
| 70 | if (IsError() || other.IsError()) { |
| 71 | // Any error compares equal to any other error. |
| 72 | return IsError() && other.IsError(); |
| 73 | } |
Luca Versari | 91a56ff | 2022-08-22 01:58:33 -0700 | [diff] [blame] | 74 | return PointsTo() == other.PointsTo() && Constraints() == other.Constraints(); |
Luca Versari | 99fddff | 2022-05-25 10:22:32 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | } // namespace lifetimes |
| 78 | } // namespace tidy |
| 79 | } // namespace clang |