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 | #ifndef DEVTOOLS_RUST_CC_INTEROP_LIFETIME_ANALYSIS_LIFETIME_LATTICE_H_ |
| 6 | #define DEVTOOLS_RUST_CC_INTEROP_LIFETIME_ANALYSIS_LIFETIME_LATTICE_H_ |
| 7 | |
| 8 | #include <string> |
| 9 | #include <utility> |
| 10 | #include <variant> |
| 11 | |
Luca Versari | 8122285 | 2022-08-05 06:36:10 -0700 | [diff] [blame] | 12 | #include "lifetime_analysis/lifetime_constraints.h" |
Luca Versari | 53a2f58 | 2023-01-16 10:09:29 -0800 | [diff] [blame] | 13 | #include "lifetime_analysis/object_set.h" |
Luca Versari | 99fddff | 2022-05-25 10:22:32 -0700 | [diff] [blame] | 14 | #include "lifetime_analysis/points_to_map.h" |
| 15 | #include "clang/Analysis/FlowSensitive/DataflowAnalysis.h" |
| 16 | #include "clang/Analysis/FlowSensitive/DataflowLattice.h" |
| 17 | #include "llvm/ADT/StringRef.h" |
| 18 | |
| 19 | namespace clang { |
| 20 | namespace tidy { |
| 21 | namespace lifetimes { |
| 22 | |
| 23 | class LifetimeLattice { |
| 24 | public: |
Luca Versari | 8122285 | 2022-08-05 06:36:10 -0700 | [diff] [blame] | 25 | // Creates a lattice holding an empty points-to map and empty constraints. |
Luca Versari | 99fddff | 2022-05-25 10:22:32 -0700 | [diff] [blame] | 26 | LifetimeLattice() = default; |
| 27 | |
| 28 | LifetimeLattice(const LifetimeLattice&) = default; |
| 29 | LifetimeLattice(LifetimeLattice&&) = default; |
| 30 | LifetimeLattice& operator=(const LifetimeLattice&) = default; |
| 31 | LifetimeLattice& operator=(LifetimeLattice&&) = default; |
| 32 | |
Luca Versari | 53a2f58 | 2023-01-16 10:09:29 -0800 | [diff] [blame] | 33 | // Creates a lattice containing the given points-to map, single-valued object |
| 34 | // set, and empty constraints. |
| 35 | explicit LifetimeLattice(PointsToMap points_to_map, |
| 36 | ObjectSet single_valued_objects) |
| 37 | : var_(std::make_tuple(std::move(points_to_map), LifetimeConstraints(), |
| 38 | std::move(single_valued_objects))) {} |
Luca Versari | 99fddff | 2022-05-25 10:22:32 -0700 | [diff] [blame] | 39 | |
| 40 | // Creates an error state containing the error message `err`. |
| 41 | explicit LifetimeLattice(std::string err) : var_(err) {} |
| 42 | |
| 43 | // Returns the points-to map. |
| 44 | // Precondition: !IsError(). |
| 45 | PointsToMap& PointsTo(); |
| 46 | const PointsToMap& PointsTo() const; |
| 47 | |
Luca Versari | 8122285 | 2022-08-05 06:36:10 -0700 | [diff] [blame] | 48 | // Returns the lifetime constraints. |
| 49 | // Precondition: !IsError(). |
| 50 | LifetimeConstraints& Constraints(); |
| 51 | const LifetimeConstraints& Constraints() const; |
| 52 | |
Luca Versari | 53a2f58 | 2023-01-16 10:09:29 -0800 | [diff] [blame] | 53 | // Returns the set of single-valued objects, i.e. objects that will be |
| 54 | // guaranteed to be overwritten completely by a write operation. |
| 55 | // For example, all local variables are single-valued unless they are |
| 56 | // conditionally overwritten. Values that represent pointees of pointers are |
| 57 | // not (as they could be arrays), but values that represent pointees of |
| 58 | // references can be. |
| 59 | // Precondition: !IsError(). |
| 60 | ObjectSet& SingleValuedObjects(); |
| 61 | const ObjectSet& SingleValuedObjects() const; |
| 62 | |
Luca Versari | 99fddff | 2022-05-25 10:22:32 -0700 | [diff] [blame] | 63 | // Returns whether the lattice is in the error state. |
| 64 | bool IsError() const { return std::holds_alternative<std::string>(var_); } |
| 65 | |
| 66 | // Returns the error string. |
| 67 | // Precondition: IsError(). |
| 68 | llvm::StringRef Error() const; |
| 69 | |
| 70 | // Returns a human-readable representation of the lattice. |
| 71 | std::string ToString() const; |
| 72 | |
| 73 | // Sets the lattice to the result of the "join" operation with `other` and |
| 74 | // returns the effect of the operation. |
| 75 | // If either of the lattices contains an error, sets this lattice to the |
| 76 | // first error encountered. |
| 77 | clang::dataflow::LatticeJoinEffect join(const LifetimeLattice& other); |
| 78 | |
| 79 | // Compares for (in-)equality. |
| 80 | // All error states are considered to be equal. |
| 81 | bool operator==(const LifetimeLattice& other) const; |
| 82 | bool operator!=(const LifetimeLattice& other) const { |
| 83 | return !(*this == other); |
| 84 | } |
| 85 | |
| 86 | private: |
Luca Versari | 53a2f58 | 2023-01-16 10:09:29 -0800 | [diff] [blame] | 87 | std::variant<std::tuple<PointsToMap, LifetimeConstraints, ObjectSet>, |
| 88 | std::string> |
| 89 | var_; |
Luca Versari | 99fddff | 2022-05-25 10:22:32 -0700 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | } // namespace lifetimes |
| 93 | } // namespace tidy |
| 94 | } // namespace clang |
| 95 | |
| 96 | #endif // DEVTOOLS_RUST_CC_INTEROP_LIFETIME_ANALYSIS_LIFETIME_LATTICE_H_ |