clang 23.0.0git
Facts.h
Go to the documentation of this file.
1//===- Facts.h - Lifetime Analysis Facts and Fact Manager ------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines Facts, which are atomic lifetime-relevant events (such as
10// loan issuance, loan expiration, origin flow, and use), and the FactManager,
11// which manages the storage and retrieval of facts for each CFG block.
12//
13//===----------------------------------------------------------------------===//
14#ifndef LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_FACTS_H
15#define LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_FACTS_H
16
17#include "clang/AST/Decl.h"
22#include "clang/Analysis/CFG.h"
23#include "llvm/ADT/STLFunctionalExtras.h"
24#include "llvm/ADT/SmallVector.h"
25#include "llvm/Support/Debug.h"
26#include <cstdint>
27#include <optional>
28
30
32
33/// An abstract base class for a single, atomic lifetime-relevant event.
34class Fact {
35
36public:
37 enum class Kind : uint8_t {
38 /// A new loan is issued from a borrow expression (e.g., &x).
40 /// A loan expires as its underlying storage is freed (e.g., variable goes
41 /// out of scope).
43 /// An origin is propagated from a source to a destination (e.g., p = q).
44 /// This can also optionally kill the destination origin before flowing into
45 /// it. Otherwise, the source's loan set is merged into the destination's
46 /// loan set.
48 /// An origin is used (eg. appears as l-value expression like DeclRefExpr).
50 /// An origin that is moved (e.g., passed to an rvalue reference parameter).
52 /// A marker for a specific point in the code, for testing.
54 /// An origin that escapes the function scope (e.g., via return).
56 /// An origin is invalidated (e.g. vector resized).
58 };
59
60private:
61 Kind K;
62 FactID ID;
63
64protected:
65 Fact(Kind K) : K(K) {}
66
67public:
68 virtual ~Fact() = default;
69 Kind getKind() const { return K; }
70
71 void setID(FactID ID) { this->ID = ID; }
72 FactID getID() const { return ID; }
73
74 template <typename T> const T *getAs() const {
75 if (T::classof(this))
76 return static_cast<const T *>(this);
77 return nullptr;
78 }
79
80 virtual void dump(llvm::raw_ostream &OS, const LoanManager &,
81 const OriginManager &) const;
82};
83
84/// A `ProgramPoint` identifies a location in the CFG by pointing to a specific
85/// `Fact`. identified by a lifetime-related event (`Fact`).
86///
87/// A `ProgramPoint` has "after" semantics: it represents the location
88/// immediately after its corresponding `Fact`.
89using ProgramPoint = const Fact *;
90
91class IssueFact : public Fact {
92 LoanID LID;
93 OriginID OID;
94
95public:
96 static bool classof(const Fact *F) { return F->getKind() == Kind::Issue; }
97
98 IssueFact(LoanID LID, OriginID OID) : Fact(Kind::Issue), LID(LID), OID(OID) {}
99 LoanID getLoanID() const { return LID; }
100 OriginID getOriginID() const { return OID; }
101 void dump(llvm::raw_ostream &OS, const LoanManager &LM,
102 const OriginManager &OM) const override;
103};
104
105/// When an AccessPath expires (e.g., a variable goes out of scope), all loans
106/// that are associated with this path expire. For example, if `x` expires, then
107/// the loan to `x` expires.
108class ExpireFact : public Fact {
109 // The access path that expires.
110 AccessPath AP;
111
112 // Expired origin (e.g., its variable goes out of scope).
113 std::optional<OriginID> OID;
114 SourceLocation ExpiryLoc;
115
116public:
117 static bool classof(const Fact *F) { return F->getKind() == Kind::Expire; }
118
120 std::optional<OriginID> OID = std::nullopt)
121 : Fact(Kind::Expire), AP(AP), OID(OID), ExpiryLoc(ExpiryLoc) {}
122
123 const AccessPath &getAccessPath() const { return AP; }
124 std::optional<OriginID> getOriginID() const { return OID; }
125 SourceLocation getExpiryLoc() const { return ExpiryLoc; }
126
127 void dump(llvm::raw_ostream &OS, const LoanManager &LM,
128 const OriginManager &OM) const override;
129};
130
131class OriginFlowFact : public Fact {
132 OriginID OIDDest;
133 OriginID OIDSrc;
134 // True if the destination origin should be killed (i.e., its current loans
135 // cleared) before the source origin's loans are flowed into it.
136 bool KillDest;
137
138public:
139 static bool classof(const Fact *F) {
140 return F->getKind() == Kind::OriginFlow;
141 }
142
143 OriginFlowFact(OriginID OIDDest, OriginID OIDSrc, bool KillDest)
144 : Fact(Kind::OriginFlow), OIDDest(OIDDest), OIDSrc(OIDSrc),
145 KillDest(KillDest) {}
146
147 OriginID getDestOriginID() const { return OIDDest; }
148 OriginID getSrcOriginID() const { return OIDSrc; }
149 bool getKillDest() const { return KillDest; }
150
151 void dump(llvm::raw_ostream &OS, const LoanManager &,
152 const OriginManager &OM) const override;
153};
154
155/// Represents that an origin escapes the current scope through various means.
156/// This is the base class for different escape scenarios.
157class OriginEscapesFact : public Fact {
158 OriginID OID;
159
160public:
161 /// The way an origin can escape the current scope.
162 enum class EscapeKind : uint8_t {
163 Return, /// Escapes via return statement.
164 Field, /// Escapes via assignment to a field.
165 Global, /// Escapes via assignment to global storage.
167
168 static bool classof(const Fact *F) {
169 return F->getKind() == Kind::OriginEscapes;
170 }
171
174 OriginID getEscapedOriginID() const { return OID; }
175 EscapeKind getEscapeKind() const { return EscKind; }
176};
177
178/// Represents that an origin escapes via a return statement.
180 const Expr *ReturnExpr;
181
182public:
183 ReturnEscapeFact(OriginID OID, const Expr *ReturnExpr)
184 : OriginEscapesFact(OID, EscapeKind::Return), ReturnExpr(ReturnExpr) {}
185
186 static bool classof(const Fact *F) {
187 return F->getKind() == Kind::OriginEscapes &&
188 static_cast<const OriginEscapesFact *>(F)->getEscapeKind() ==
190 }
191 const Expr *getReturnExpr() const { return ReturnExpr; };
192 void dump(llvm::raw_ostream &OS, const LoanManager &,
193 const OriginManager &OM) const override;
194};
195
196/// Represents that an origin escapes via assignment to a field.
197/// Example: `this->view = local_var;` where local_var outlives the assignment
198/// but not the object containing the field.
200 const FieldDecl *FDecl;
201
202public:
204 : OriginEscapesFact(OID, EscapeKind::Field), FDecl(FDecl) {}
205
206 static bool classof(const Fact *F) {
207 return F->getKind() == Kind::OriginEscapes &&
208 static_cast<const OriginEscapesFact *>(F)->getEscapeKind() ==
210 }
211 const FieldDecl *getFieldDecl() const { return FDecl; };
212 void dump(llvm::raw_ostream &OS, const LoanManager &,
213 const OriginManager &OM) const override;
214};
215
216/// Represents that an origin escapes via assignment to global or static
217/// storage. Example: `global_storage = local_var;`
219 const VarDecl *Global;
220
221public:
223 : OriginEscapesFact(OID, EscapeKind::Global), Global(VDecl) {}
224
225 static bool classof(const Fact *F) {
226 return F->getKind() == Kind::OriginEscapes &&
227 static_cast<const OriginEscapesFact *>(F)->getEscapeKind() ==
229 }
230 const VarDecl *getGlobal() const { return Global; };
231 void dump(llvm::raw_ostream &OS, const LoanManager &,
232 const OriginManager &OM) const override;
233};
234
235class UseFact : public Fact {
236 const Expr *UseExpr;
237 const OriginList *OList;
238 // True if this use is a write operation (e.g., left-hand side of assignment).
239 // Write operations are exempted from use-after-free checks.
240 bool IsWritten = false;
241
242public:
243 static bool classof(const Fact *F) { return F->getKind() == Kind::Use; }
244
245 UseFact(const Expr *UseExpr, const OriginList *OList)
246 : Fact(Kind::Use), UseExpr(UseExpr), OList(OList) {}
247
248 const OriginList *getUsedOrigins() const { return OList; }
249 const Expr *getUseExpr() const { return UseExpr; }
250 void markAsWritten() { IsWritten = true; }
251 bool isWritten() const { return IsWritten; }
252
253 void dump(llvm::raw_ostream &OS, const LoanManager &,
254 const OriginManager &OM) const override;
255};
256
257/// Represents that an origin's storage has been invalidated by a container
258/// operation (e.g., vector::push_back may reallocate, invalidating iterators).
259/// Created when a container method that may invalidate references/iterators
260/// is called on the container.
262 OriginID OID;
263 const Expr *InvalidationExpr;
264
265public:
266 static bool classof(const Fact *F) {
267 return F->getKind() == Kind::InvalidateOrigin;
268 }
269
270 InvalidateOriginFact(OriginID OID, const Expr *InvalidationExpr)
271 : Fact(Kind::InvalidateOrigin), OID(OID),
272 InvalidationExpr(InvalidationExpr) {}
273
274 OriginID getInvalidatedOrigin() const { return OID; }
275 const Expr *getInvalidationExpr() const { return InvalidationExpr; }
276 void dump(llvm::raw_ostream &OS, const LoanManager &,
277 const OriginManager &OM) const override;
278};
279
280/// Top-level origin of the expression which was found to be moved, e.g, when
281/// being used as an argument to an r-value reference parameter.
282class MovedOriginFact : public Fact {
283 const OriginID MovedOrigin;
284 const Expr *MoveExpr;
285
286public:
287 static bool classof(const Fact *F) {
288 return F->getKind() == Kind::MovedOrigin;
289 }
290
291 MovedOriginFact(const Expr *MoveExpr, OriginID MovedOrigin)
292 : Fact(Kind::MovedOrigin), MovedOrigin(MovedOrigin), MoveExpr(MoveExpr) {}
293
294 OriginID getMovedOrigin() const { return MovedOrigin; }
295 const Expr *getMoveExpr() const { return MoveExpr; }
296
297 void dump(llvm::raw_ostream &OS, const LoanManager &,
298 const OriginManager &OM) const override;
299};
300
301/// A dummy-fact used to mark a specific point in the code for testing.
302/// It is generated by recognizing a `void("__lifetime_test_point_...")` cast.
303class TestPointFact : public Fact {
304 StringRef Annotation;
305
306public:
307 static bool classof(const Fact *F) { return F->getKind() == Kind::TestPoint; }
308
309 explicit TestPointFact(StringRef Annotation)
310 : Fact(Kind::TestPoint), Annotation(Annotation) {}
311
312 StringRef getAnnotation() const { return Annotation; }
313
314 void dump(llvm::raw_ostream &OS, const LoanManager &,
315 const OriginManager &) const override;
316};
317
319public:
320 FactManager(const AnalysisDeclContext &AC, const CFG &Cfg)
321 : OriginMgr(AC.getASTContext(), AC.getDecl()) {
322 BlockToFacts.resize(Cfg.getNumBlockIDs());
323 }
324
326 return BlockToFacts[B->getBlockID()];
327 }
328
330 if (!NewFacts.empty())
331 BlockToFacts[B->getBlockID()].assign(NewFacts.begin(), NewFacts.end());
332 }
333
334 template <typename FactType, typename... Args>
335 FactType *createFact(Args &&...args) {
336 void *Mem = FactAllocator.Allocate<FactType>();
337 FactType *Res = new (Mem) FactType(std::forward<Args>(args)...);
338 Res->setID(NextFactID++);
339 return Res;
340 }
341
342 void dump(const CFG &Cfg, AnalysisDeclContext &AC) const;
343
344 /// Retrieves program points that were specially marked in the source code
345 /// for testing.
346 ///
347 /// The analysis recognizes special function calls of the form
348 /// `void("__lifetime_test_point_<name>")` as test points. This method returns
349 /// a map from the annotation string (<name>) to the corresponding
350 /// `ProgramPoint`. This allows test harnesses to query the analysis state at
351 /// user-defined locations in the code.
352 /// \note This is intended for testing only.
353 llvm::StringMap<ProgramPoint> getTestPoints() const;
354 /// Retrieves all the facts in the block containing Program Point P.
355 /// \note This is intended for testing only.
357
358 unsigned getNumFacts() const { return NextFactID.Value; }
359
360 LoanManager &getLoanMgr() { return LoanMgr; }
361 const LoanManager &getLoanMgr() const { return LoanMgr; }
362 OriginManager &getOriginMgr() { return OriginMgr; }
363 const OriginManager &getOriginMgr() const { return OriginMgr; }
364
365private:
366 FactID NextFactID{0};
367 LoanManager LoanMgr;
368 OriginManager OriginMgr;
369 /// Facts for each CFG block, indexed by block ID.
371 llvm::BumpPtrAllocator FactAllocator;
372};
373} // namespace clang::lifetimes::internal
374
375#endif // LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_FACTS_H
This file defines AnalysisDeclContext, a class that manages the analysis context data for context sen...
AnalysisDeclContext contains the context data for the function, method or block under analysis.
Represents a single basic block in a source-level CFG.
Definition CFG.h:632
unsigned getBlockID() const
Definition CFG.h:1134
Represents a source-level, intra-procedural CFG that represents the control-flow of a Stmt.
Definition CFG.h:1250
unsigned getNumBlockIDs() const
Returns the total number of BlockIDs allocated (which start at 0).
Definition CFG.h:1443
This represents one expression.
Definition Expr.h:112
Represents a member of a struct/union/class.
Definition Decl.h:3175
Encodes a location in the source.
Represents a variable declaration or definition.
Definition Decl.h:926
Represents the storage location being borrowed, e.g., a specific stack variable or a field within it:...
Definition Loans.h:44
const AccessPath & getAccessPath() const
Definition Facts.h:123
SourceLocation getExpiryLoc() const
Definition Facts.h:125
static bool classof(const Fact *F)
Definition Facts.h:117
ExpireFact(AccessPath AP, SourceLocation ExpiryLoc, std::optional< OriginID > OID=std::nullopt)
Definition Facts.h:119
std::optional< OriginID > getOriginID() const
Definition Facts.h:124
void dump(llvm::raw_ostream &OS, const LoanManager &LM, const OriginManager &OM) const override
Definition Facts.cpp:29
llvm::ArrayRef< const Fact * > getFacts(const CFGBlock *B) const
Definition Facts.h:325
FactType * createFact(Args &&...args)
Definition Facts.h:335
llvm::StringMap< ProgramPoint > getTestPoints() const
Retrieves program points that were specially marked in the source code for testing.
Definition Facts.cpp:106
void dump(const CFG &Cfg, AnalysisDeclContext &AC) const
Definition Facts.cpp:121
void addBlockFacts(const CFGBlock *B, llvm::ArrayRef< Fact * > NewFacts)
Definition Facts.h:329
const OriginManager & getOriginMgr() const
Definition Facts.h:363
const LoanManager & getLoanMgr() const
Definition Facts.h:361
llvm::ArrayRef< const Fact * > getBlockContaining(ProgramPoint P) const
Retrieves all the facts in the block containing Program Point P.
Definition Facts.cpp:140
FactManager(const AnalysisDeclContext &AC, const CFG &Cfg)
Definition Facts.h:320
An abstract base class for a single, atomic lifetime-relevant event.
Definition Facts.h:34
@ InvalidateOrigin
An origin is invalidated (e.g. vector resized).
Definition Facts.h:57
@ TestPoint
A marker for a specific point in the code, for testing.
Definition Facts.h:53
@ Expire
A loan expires as its underlying storage is freed (e.g., variable goes out of scope).
Definition Facts.h:42
@ Issue
A new loan is issued from a borrow expression (e.g., &x).
Definition Facts.h:39
@ OriginFlow
An origin is propagated from a source to a destination (e.g., p = q).
Definition Facts.h:47
@ MovedOrigin
An origin that is moved (e.g., passed to an rvalue reference parameter).
Definition Facts.h:51
@ Use
An origin is used (eg. appears as l-value expression like DeclRefExpr).
Definition Facts.h:49
@ OriginEscapes
An origin that escapes the function scope (e.g., via return).
Definition Facts.h:55
const T * getAs() const
Definition Facts.h:74
virtual void dump(llvm::raw_ostream &OS, const LoanManager &, const OriginManager &) const
Definition Facts.cpp:15
FieldEscapeFact(OriginID OID, const FieldDecl *FDecl)
Definition Facts.h:203
void dump(llvm::raw_ostream &OS, const LoanManager &, const OriginManager &OM) const override
Definition Facts.cpp:66
const FieldDecl * getFieldDecl() const
Definition Facts.h:211
static bool classof(const Fact *F)
Definition Facts.h:206
void dump(llvm::raw_ostream &OS, const LoanManager &, const OriginManager &OM) const override
Definition Facts.cpp:73
GlobalEscapeFact(OriginID OID, const VarDecl *VDecl)
Definition Facts.h:222
static bool classof(const Fact *F)
Definition Facts.h:225
const VarDecl * getGlobal() const
Definition Facts.h:230
void dump(llvm::raw_ostream &OS, const LoanManager &, const OriginManager &OM) const override
Definition Facts.cpp:94
InvalidateOriginFact(OriginID OID, const Expr *InvalidationExpr)
Definition Facts.h:270
void dump(llvm::raw_ostream &OS, const LoanManager &LM, const OriginManager &OM) const override
Definition Facts.cpp:20
IssueFact(LoanID LID, OriginID OID)
Definition Facts.h:98
static bool classof(const Fact *F)
Definition Facts.h:96
Manages the creation, storage and retrieval of loans.
Definition Loans.h:129
void dump(llvm::raw_ostream &OS, const LoanManager &, const OriginManager &OM) const override
Definition Facts.cpp:52
static bool classof(const Fact *F)
Definition Facts.h:287
MovedOriginFact(const Expr *MoveExpr, OriginID MovedOrigin)
Definition Facts.h:291
enum clang::lifetimes::internal::OriginEscapesFact::EscapeKind EscKind
EscapeKind
The way an origin can escape the current scope.
Definition Facts.h:162
@ Global
Escapes via assignment to a field.
Definition Facts.h:165
static bool classof(const Fact *F)
Definition Facts.h:168
OriginEscapesFact(OriginID OID, EscapeKind EscKind)
Definition Facts.h:172
static bool classof(const Fact *F)
Definition Facts.h:139
void dump(llvm::raw_ostream &OS, const LoanManager &, const OriginManager &OM) const override
Definition Facts.cpp:40
OriginFlowFact(OriginID OIDDest, OriginID OIDSrc, bool KillDest)
Definition Facts.h:143
A list of origins representing levels of indirection for pointer-like types.
Definition Origins.h:94
Manages the creation, storage, and retrieval of origins for pointer-like variables and expressions.
Definition Origins.h:126
static bool classof(const Fact *F)
Definition Facts.h:186
ReturnEscapeFact(OriginID OID, const Expr *ReturnExpr)
Definition Facts.h:183
void dump(llvm::raw_ostream &OS, const LoanManager &, const OriginManager &OM) const override
Definition Facts.cpp:59
void dump(llvm::raw_ostream &OS, const LoanManager &, const OriginManager &) const override
Definition Facts.cpp:101
static bool classof(const Fact *F)
Definition Facts.h:307
TestPointFact(StringRef Annotation)
Definition Facts.h:309
const Expr * getUseExpr() const
Definition Facts.h:249
void dump(llvm::raw_ostream &OS, const LoanManager &, const OriginManager &OM) const override
Definition Facts.cpp:80
static bool classof(const Fact *F)
Definition Facts.h:243
UseFact(const Expr *UseExpr, const OriginList *OList)
Definition Facts.h:245
const OriginList * getUsedOrigins() const
Definition Facts.h:248
const Fact * ProgramPoint
A ProgramPoint identifies a location in the CFG by pointing to a specific Fact.
Definition Facts.h:89
utils::ID< struct LoanTag > LoanID
Definition Loans.h:25
utils::ID< struct OriginTag > OriginID
Definition Origins.h:27
utils::ID< struct FactTag > FactID
Definition Facts.h:31
A generic, type-safe wrapper for an ID, distinguished by its Tag type.
Definition Utils.h:21