blob: bdfa7bfef7f06188520143826b06fad8ef8882cb [file] [log] [blame]
Luca Versari99fddff2022-05-25 10:22:32 -07001// 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 Versari81222852022-08-05 06:36:10 -070012#include "lifetime_analysis/lifetime_constraints.h"
Luca Versari53a2f582023-01-16 10:09:29 -080013#include "lifetime_analysis/object_set.h"
Luca Versari99fddff2022-05-25 10:22:32 -070014#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
19namespace clang {
20namespace tidy {
21namespace lifetimes {
22
23class LifetimeLattice {
24 public:
Luca Versari81222852022-08-05 06:36:10 -070025 // Creates a lattice holding an empty points-to map and empty constraints.
Luca Versari99fddff2022-05-25 10:22:32 -070026 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 Versari53a2f582023-01-16 10:09:29 -080033 // 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 Versari99fddff2022-05-25 10:22:32 -070039
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 Versari81222852022-08-05 06:36:10 -070048 // Returns the lifetime constraints.
49 // Precondition: !IsError().
50 LifetimeConstraints& Constraints();
51 const LifetimeConstraints& Constraints() const;
52
Luca Versari53a2f582023-01-16 10:09:29 -080053 // 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 Versari99fddff2022-05-25 10:22:32 -070063 // 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 Versari53a2f582023-01-16 10:09:29 -080087 std::variant<std::tuple<PointsToMap, LifetimeConstraints, ObjectSet>,
88 std::string>
89 var_;
Luca Versari99fddff2022-05-25 10:22:32 -070090};
91
92} // namespace lifetimes
93} // namespace tidy
94} // namespace clang
95
96#endif // DEVTOOLS_RUST_CC_INTEROP_LIFETIME_ANALYSIS_LIFETIME_LATTICE_H_