clang 22.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
21#include "clang/Analysis/CFG.h"
22#include "llvm/ADT/SmallVector.h"
23#include "llvm/Support/Debug.h"
24#include <cstdint>
25
27
29
30/// An abstract base class for a single, atomic lifetime-relevant event.
31class Fact {
32
33public:
34 enum class Kind : uint8_t {
35 /// A new loan is issued from a borrow expression (e.g., &x).
37 /// A loan expires as its underlying storage is freed (e.g., variable goes
38 /// out of scope).
40 /// An origin is propagated from a source to a destination (e.g., p = q).
41 /// This can also optionally kill the destination origin before flowing into
42 /// it. Otherwise, the source's loan set is merged into the destination's
43 /// loan set.
45 /// An origin is used (eg. appears as l-value expression like DeclRefExpr).
47 /// A marker for a specific point in the code, for testing.
49 /// An origin that escapes the function scope (e.g., via return).
51 };
52
53private:
54 Kind K;
55 FactID ID;
56
57protected:
58 Fact(Kind K) : K(K) {}
59
60public:
61 virtual ~Fact() = default;
62 Kind getKind() const { return K; }
63
64 void setID(FactID ID) { this->ID = ID; }
65 FactID getID() const { return ID; }
66
67 template <typename T> const T *getAs() const {
68 if (T::classof(this))
69 return static_cast<const T *>(this);
70 return nullptr;
71 }
72
73 virtual void dump(llvm::raw_ostream &OS, const LoanManager &,
74 const OriginManager &) const;
75};
76
77/// A `ProgramPoint` identifies a location in the CFG by pointing to a specific
78/// `Fact`. identified by a lifetime-related event (`Fact`).
79///
80/// A `ProgramPoint` has "after" semantics: it represents the location
81/// immediately after its corresponding `Fact`.
82using ProgramPoint = const Fact *;
83
84class IssueFact : public Fact {
85 LoanID LID;
86 OriginID OID;
87
88public:
89 static bool classof(const Fact *F) { return F->getKind() == Kind::Issue; }
90
91 IssueFact(LoanID LID, OriginID OID) : Fact(Kind::Issue), LID(LID), OID(OID) {}
92 LoanID getLoanID() const { return LID; }
93 OriginID getOriginID() const { return OID; }
94 void dump(llvm::raw_ostream &OS, const LoanManager &LM,
95 const OriginManager &OM) const override;
96};
97
98class ExpireFact : public Fact {
99 LoanID LID;
100 SourceLocation ExpiryLoc;
101
102public:
103 static bool classof(const Fact *F) { return F->getKind() == Kind::Expire; }
104
106 : Fact(Kind::Expire), LID(LID), ExpiryLoc(ExpiryLoc) {}
107
108 LoanID getLoanID() const { return LID; }
109 SourceLocation getExpiryLoc() const { return ExpiryLoc; }
110
111 void dump(llvm::raw_ostream &OS, const LoanManager &LM,
112 const OriginManager &) const override;
113};
114
115class OriginFlowFact : public Fact {
116 OriginID OIDDest;
117 OriginID OIDSrc;
118 // True if the destination origin should be killed (i.e., its current loans
119 // cleared) before the source origin's loans are flowed into it.
120 bool KillDest;
121
122public:
123 static bool classof(const Fact *F) {
124 return F->getKind() == Kind::OriginFlow;
125 }
126
127 OriginFlowFact(OriginID OIDDest, OriginID OIDSrc, bool KillDest)
128 : Fact(Kind::OriginFlow), OIDDest(OIDDest), OIDSrc(OIDSrc),
129 KillDest(KillDest) {}
130
131 OriginID getDestOriginID() const { return OIDDest; }
132 OriginID getSrcOriginID() const { return OIDSrc; }
133 bool getKillDest() const { return KillDest; }
134
135 void dump(llvm::raw_ostream &OS, const LoanManager &,
136 const OriginManager &OM) const override;
137};
138
139class OriginEscapesFact : public Fact {
140 OriginID OID;
141 const Expr *EscapeExpr;
142
143public:
144 static bool classof(const Fact *F) {
145 return F->getKind() == Kind::OriginEscapes;
146 }
147
148 OriginEscapesFact(OriginID OID, const Expr *EscapeExpr)
149 : Fact(Kind::OriginEscapes), OID(OID), EscapeExpr(EscapeExpr) {}
150 OriginID getEscapedOriginID() const { return OID; }
151 const Expr *getEscapeExpr() const { return EscapeExpr; };
152 void dump(llvm::raw_ostream &OS, const LoanManager &,
153 const OriginManager &OM) const override;
154};
155
156class UseFact : public Fact {
157 const Expr *UseExpr;
158 OriginID OID;
159 // True if this use is a write operation (e.g., left-hand side of assignment).
160 // Write operations are exempted from use-after-free checks.
161 bool IsWritten = false;
162
163public:
164 static bool classof(const Fact *F) { return F->getKind() == Kind::Use; }
165
166 UseFact(const Expr *UseExpr, OriginManager &OM)
167 : Fact(Kind::Use), UseExpr(UseExpr), OID(OM.get(*UseExpr)) {}
168
169 OriginID getUsedOrigin() const { return OID; }
170 const Expr *getUseExpr() const { return UseExpr; }
171 void markAsWritten() { IsWritten = true; }
172 bool isWritten() const { return IsWritten; }
173
174 void dump(llvm::raw_ostream &OS, const LoanManager &,
175 const OriginManager &OM) const override;
176};
177
178/// A dummy-fact used to mark a specific point in the code for testing.
179/// It is generated by recognizing a `void("__lifetime_test_point_...")` cast.
180class TestPointFact : public Fact {
181 StringRef Annotation;
182
183public:
184 static bool classof(const Fact *F) { return F->getKind() == Kind::TestPoint; }
185
186 explicit TestPointFact(StringRef Annotation)
187 : Fact(Kind::TestPoint), Annotation(Annotation) {}
188
189 StringRef getAnnotation() const { return Annotation; }
190
191 void dump(llvm::raw_ostream &OS, const LoanManager &,
192 const OriginManager &) const override;
193};
194
196public:
197 void init(const CFG &Cfg) {
198 assert(BlockToFacts.empty() && "FactManager already initialized");
199 BlockToFacts.resize(Cfg.getNumBlockIDs());
200 }
201
203 return BlockToFacts[B->getBlockID()];
204 }
205
207 if (!NewFacts.empty())
208 BlockToFacts[B->getBlockID()].assign(NewFacts.begin(), NewFacts.end());
209 }
210
211 template <typename FactType, typename... Args>
212 FactType *createFact(Args &&...args) {
213 void *Mem = FactAllocator.Allocate<FactType>();
214 FactType *Res = new (Mem) FactType(std::forward<Args>(args)...);
215 Res->setID(NextFactID++);
216 return Res;
217 }
218
219 void dump(const CFG &Cfg, AnalysisDeclContext &AC) const;
220
221 /// Retrieves program points that were specially marked in the source code
222 /// for testing.
223 ///
224 /// The analysis recognizes special function calls of the form
225 /// `void("__lifetime_test_point_<name>")` as test points. This method returns
226 /// a map from the annotation string (<name>) to the corresponding
227 /// `ProgramPoint`. This allows test harnesses to query the analysis state at
228 /// user-defined locations in the code.
229 /// \note This is intended for testing only.
230 llvm::StringMap<ProgramPoint> getTestPoints() const;
231 /// Retrieves all the facts in the block containing Program Point P.
232 /// \note This is intended for testing only.
234
235 unsigned getNumFacts() const { return NextFactID.Value; }
236
237 LoanManager &getLoanMgr() { return LoanMgr; }
238 const LoanManager &getLoanMgr() const { return LoanMgr; }
239 OriginManager &getOriginMgr() { return OriginMgr; }
240 const OriginManager &getOriginMgr() const { return OriginMgr; }
241
242private:
243 FactID NextFactID{0};
244 LoanManager LoanMgr;
245 OriginManager OriginMgr;
246 /// Facts for each CFG block, indexed by block ID.
248 llvm::BumpPtrAllocator FactAllocator;
249};
250} // namespace clang::lifetimes::internal
251
252#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:605
unsigned getBlockID() const
Definition CFG.h:1111
Represents a source-level, intra-procedural CFG that represents the control-flow of a Stmt.
Definition CFG.h:1222
unsigned getNumBlockIDs() const
Returns the total number of BlockIDs allocated (which start at 0).
Definition CFG.h:1410
This represents one expression.
Definition Expr.h:112
Encodes a location in the source.
SourceLocation getExpiryLoc() const
Definition Facts.h:109
static bool classof(const Fact *F)
Definition Facts.h:103
ExpireFact(LoanID LID, SourceLocation ExpiryLoc)
Definition Facts.h:105
void dump(llvm::raw_ostream &OS, const LoanManager &LM, const OriginManager &) const override
Definition Facts.cpp:29
llvm::ArrayRef< const Fact * > getFacts(const CFGBlock *B) const
Definition Facts.h:202
FactType * createFact(Args &&...args)
Definition Facts.h:212
llvm::StringMap< ProgramPoint > getTestPoints() const
Retrieves program points that were specially marked in the source code for testing.
Definition Facts.cpp:65
void dump(const CFG &Cfg, AnalysisDeclContext &AC) const
Definition Facts.cpp:80
void addBlockFacts(const CFGBlock *B, llvm::ArrayRef< Fact * > NewFacts)
Definition Facts.h:206
const OriginManager & getOriginMgr() const
Definition Facts.h:240
const LoanManager & getLoanMgr() const
Definition Facts.h:238
llvm::ArrayRef< const Fact * > getBlockContaining(ProgramPoint P) const
Retrieves all the facts in the block containing Program Point P.
Definition Facts.cpp:99
An abstract base class for a single, atomic lifetime-relevant event.
Definition Facts.h:31
@ TestPoint
A marker for a specific point in the code, for testing.
Definition Facts.h:48
@ Expire
A loan expires as its underlying storage is freed (e.g., variable goes out of scope).
Definition Facts.h:39
@ Issue
A new loan is issued from a borrow expression (e.g., &x).
Definition Facts.h:36
@ OriginFlow
An origin is propagated from a source to a destination (e.g., p = q).
Definition Facts.h:44
@ Use
An origin is used (eg. appears as l-value expression like DeclRefExpr).
Definition Facts.h:46
@ OriginEscapes
An origin that escapes the function scope (e.g., via return).
Definition Facts.h:50
const T * getAs() const
Definition Facts.h:67
virtual void dump(llvm::raw_ostream &OS, const LoanManager &, const OriginManager &) const
Definition Facts.cpp:15
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:91
static bool classof(const Fact *F)
Definition Facts.h:89
Manages the creation, storage and retrieval of loans.
Definition Loans.h:55
static bool classof(const Fact *F)
Definition Facts.h:144
void dump(llvm::raw_ostream &OS, const LoanManager &, const OriginManager &OM) const override
Definition Facts.cpp:46
OriginEscapesFact(OriginID OID, const Expr *EscapeExpr)
Definition Facts.h:148
static bool classof(const Fact *F)
Definition Facts.h:123
void dump(llvm::raw_ostream &OS, const LoanManager &, const OriginManager &OM) const override
Definition Facts.cpp:36
OriginFlowFact(OriginID OIDDest, OriginID OIDSrc, bool KillDest)
Definition Facts.h:127
Manages the creation, storage, and retrieval of origins for pointer-like variables and expressions.
Definition Origins.h:57
void dump(llvm::raw_ostream &OS, const LoanManager &, const OriginManager &) const override
Definition Facts.cpp:60
static bool classof(const Fact *F)
Definition Facts.h:184
TestPointFact(StringRef Annotation)
Definition Facts.h:186
UseFact(const Expr *UseExpr, OriginManager &OM)
Definition Facts.h:166
const Expr * getUseExpr() const
Definition Facts.h:170
void dump(llvm::raw_ostream &OS, const LoanManager &, const OriginManager &OM) const override
Definition Facts.cpp:53
static bool classof(const Fact *F)
Definition Facts.h:164
OriginID getUsedOrigin() const
Definition Facts.h:169
const Fact * ProgramPoint
A ProgramPoint identifies a location in the CFG by pointing to a specific Fact.
Definition Facts.h:82
utils::ID< struct LoanTag > LoanID
Definition Loans.h:23
utils::ID< struct OriginTag > OriginID
Definition Origins.h:23
utils::ID< struct FactTag > FactID
Definition Facts.h:28
const FunctionProtoType * T
A generic, type-safe wrapper for an ID, distinguished by its Tag type.
Definition Utils.h:21