Skip to content

Commit f239d90

Browse files
committed
liveness-based-lifetime-policy
1 parent e2d5efd commit f239d90

File tree

6 files changed

+991
-489
lines changed

6 files changed

+991
-489
lines changed

clang/include/clang/Analysis/Analyses/LifetimeSafety.h

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,18 @@
2929
namespace clang::lifetimes {
3030

3131
/// Enum to track the confidence level of a potential error.
32-
enum class Confidence {
32+
enum class Confidence : uint8_t {
3333
None,
3434
Maybe, // Reported as a potential error (-Wlifetime-safety-strict)
3535
Definite // Reported as a definite error (-Wlifetime-safety-permissive)
3636
};
3737

38+
enum class LivenessKind : uint8_t {
39+
Dead, // Not alive
40+
Maybe, // Live on some path but not all paths (may-be-live)
41+
Must // Live on all paths (must-be-live)
42+
};
43+
3844
class LifetimeSafetyReporter {
3945
public:
4046
LifetimeSafetyReporter() = default;
@@ -55,6 +61,7 @@ class Fact;
5561
class FactManager;
5662
class LoanPropagationAnalysis;
5763
class ExpiredLoansAnalysis;
64+
class LiveOriginAnalysis;
5865
struct LifetimeFactory;
5966

6067
/// A generic, type-safe wrapper for an ID, distinguished by its `Tag` type.
@@ -89,6 +96,7 @@ inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, OriginID ID) {
8996
// TODO(opt): Consider using a bitset to represent the set of loans.
9097
using LoanSet = llvm::ImmutableSet<LoanID>;
9198
using OriginSet = llvm::ImmutableSet<OriginID>;
99+
using OriginLoanMap = llvm::ImmutableMap<OriginID, LoanSet>;
92100

93101
/// A `ProgramPoint` identifies a location in the CFG by pointing to a specific
94102
/// `Fact`. identified by a lifetime-related event (`Fact`).
@@ -110,8 +118,16 @@ class LifetimeSafetyAnalysis {
110118
/// Returns the set of loans an origin holds at a specific program point.
111119
LoanSet getLoansAtPoint(OriginID OID, ProgramPoint PP) const;
112120

113-
/// Returns the set of loans that have expired at a specific program point.
114-
std::vector<LoanID> getExpiredLoansAtPoint(ProgramPoint PP) const;
121+
/// Returns the set of origins that are live at a specific program point,
122+
/// along with the confidence level of their liveness.
123+
///
124+
/// An origin is considered live if there are potential future uses of that
125+
/// origin after the given program point. The confidence level indicates
126+
/// whether the origin is definitely live (Definite) due to being domintated
127+
/// by a set of uses or only possibly live (Maybe) only on some but not all
128+
/// control flow paths.
129+
std::vector<std::pair<OriginID, LivenessKind>>
130+
getLiveOriginsAtPoint(ProgramPoint PP) const;
115131

116132
/// Finds the OriginID for a given declaration.
117133
/// Returns a null optional if not found.
@@ -138,7 +154,7 @@ class LifetimeSafetyAnalysis {
138154
std::unique_ptr<LifetimeFactory> Factory;
139155
std::unique_ptr<FactManager> FactMgr;
140156
std::unique_ptr<LoanPropagationAnalysis> LoanPropagation;
141-
std::unique_ptr<ExpiredLoansAnalysis> ExpiredLoans;
157+
std::unique_ptr<LiveOriginAnalysis> LiveOrigins;
142158
};
143159
} // namespace internal
144160
} // namespace clang::lifetimes

0 commit comments

Comments
 (0)