clang 23.0.0git
Facts.cpp
Go to the documentation of this file.
1//===- Facts.cpp - Lifetime Analysis Facts Implementation -------*- 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
10#include "clang/AST/Decl.h"
12
14
15void Fact::dump(llvm::raw_ostream &OS, const LoanManager &,
16 const OriginManager &) const {
17 OS << "Fact (Kind: " << static_cast<int>(K) << ")\n";
18}
19
20void IssueFact::dump(llvm::raw_ostream &OS, const LoanManager &LM,
21 const OriginManager &OM) const {
22 OS << "Issue (";
23 LM.getLoan(getLoanID())->dump(OS);
24 OS << ", ToOrigin: ";
25 OM.dump(getOriginID(), OS);
26 OS << ")\n";
27}
28
29void ExpireFact::dump(llvm::raw_ostream &OS, const LoanManager &LM,
30 const OriginManager &OM) const {
31 OS << "Expire (";
32 getAccessPath().dump(OS);
33 if (auto OID = getOriginID()) {
34 OS << ", Origin: ";
35 OM.dump(*OID, OS);
36 }
37 OS << ")\n";
38}
39
40void OriginFlowFact::dump(llvm::raw_ostream &OS, const LoanManager &,
41 const OriginManager &OM) const {
42 OS << "OriginFlow: \n";
43 OS << "\tDest: ";
44 OM.dump(getDestOriginID(), OS);
45 OS << "\n";
46 OS << "\tSrc: ";
47 OM.dump(getSrcOriginID(), OS);
48 OS << (getKillDest() ? "" : ", Merge");
49 OS << "\n";
50}
51
52void MovedOriginFact::dump(llvm::raw_ostream &OS, const LoanManager &,
53 const OriginManager &OM) const {
54 OS << "MovedOrigins (";
55 OM.dump(getMovedOrigin(), OS);
56 OS << ")\n";
57}
58
59void ReturnEscapeFact::dump(llvm::raw_ostream &OS, const LoanManager &,
60 const OriginManager &OM) const {
61 OS << "OriginEscapes (";
62 OM.dump(getEscapedOriginID(), OS);
63 OS << ", via Return)\n";
64}
65
66void FieldEscapeFact::dump(llvm::raw_ostream &OS, const LoanManager &,
67 const OriginManager &OM) const {
68 OS << "OriginEscapes (";
69 OM.dump(getEscapedOriginID(), OS);
70 OS << ", via Field)\n";
71}
72
73void GlobalEscapeFact::dump(llvm::raw_ostream &OS, const LoanManager &,
74 const OriginManager &OM) const {
75 OS << "OriginEscapes (";
76 OM.dump(getEscapedOriginID(), OS);
77 OS << ", via Global)\n";
78}
79
80void UseFact::dump(llvm::raw_ostream &OS, const LoanManager &,
81 const OriginManager &OM) const {
82 OS << "Use (";
83 size_t NumUsedOrigins = getUsedOrigins()->getLength();
84 size_t I = 0;
85 for (const OriginList *Cur = getUsedOrigins(); Cur;
86 Cur = Cur->peelOuterOrigin(), ++I) {
87 OM.dump(Cur->getOuterOriginID(), OS);
88 if (I < NumUsedOrigins - 1)
89 OS << ", ";
90 }
91 OS << ", " << (isWritten() ? "Write" : "Read") << ")\n";
92}
93
94void InvalidateOriginFact::dump(llvm::raw_ostream &OS, const LoanManager &,
95 const OriginManager &OM) const {
96 OS << "InvalidateOrigin (";
97 OM.dump(getInvalidatedOrigin(), OS);
98 OS << ")\n";
99}
100
101void TestPointFact::dump(llvm::raw_ostream &OS, const LoanManager &,
102 const OriginManager &) const {
103 OS << "TestPoint (Annotation: \"" << getAnnotation() << "\")\n";
104}
105
106llvm::StringMap<ProgramPoint> FactManager::getTestPoints() const {
107 llvm::StringMap<ProgramPoint> AnnotationToPointMap;
108 for (const auto &BlockFacts : BlockToFacts) {
109 for (const Fact *F : BlockFacts) {
110 if (const auto *TPF = F->getAs<TestPointFact>()) {
111 StringRef PointName = TPF->getAnnotation();
112 assert(!AnnotationToPointMap.contains(PointName) &&
113 "more than one test points with the same name");
114 AnnotationToPointMap[PointName] = F;
115 }
116 }
117 }
118 return AnnotationToPointMap;
119}
120
121void FactManager::dump(const CFG &Cfg, AnalysisDeclContext &AC) const {
122 llvm::dbgs() << "==========================================\n";
123 llvm::dbgs() << " Lifetime Analysis Facts:\n";
124 llvm::dbgs() << "==========================================\n";
125 if (const Decl *D = AC.getDecl())
126 if (const auto *ND = dyn_cast<NamedDecl>(D))
127 llvm::dbgs() << "Function: " << ND->getQualifiedNameAsString() << "\n";
128 // Print blocks in the order as they appear in code for a stable ordering.
129 for (const CFGBlock *B : *AC.getAnalysis<PostOrderCFGView>()) {
130 llvm::dbgs() << " Block B" << B->getBlockID() << ":\n";
131 for (const Fact *F : getFacts(B)) {
132 llvm::dbgs() << " ";
133 F->dump(llvm::dbgs(), LoanMgr, OriginMgr);
134 }
135 llvm::dbgs() << " End of Block\n";
136 }
137}
138
141 for (const auto &BlockToFactsVec : BlockToFacts) {
142 for (const Fact *F : BlockToFactsVec)
143 if (F == P)
144 return BlockToFactsVec;
145 }
146 return {};
147}
148
149} // namespace clang::lifetimes::internal
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
Represents a source-level, intra-procedural CFG that represents the control-flow of a Stmt.
Definition CFG.h:1250
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
void dump(llvm::raw_ostream &OS) const
Definition Loans.cpp:13
const AccessPath & getAccessPath() const
Definition Facts.h:123
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
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
llvm::ArrayRef< const Fact * > getBlockContaining(ProgramPoint P) const
Retrieves all the facts in the block containing Program Point P.
Definition Facts.cpp:140
An abstract base class for a single, atomic lifetime-relevant event.
Definition Facts.h:34
virtual void dump(llvm::raw_ostream &OS, const LoanManager &, const OriginManager &) const
Definition Facts.cpp:15
void dump(llvm::raw_ostream &OS, const LoanManager &, const OriginManager &OM) const override
Definition Facts.cpp:66
void dump(llvm::raw_ostream &OS, const LoanManager &, const OriginManager &OM) const override
Definition Facts.cpp:73
void dump(llvm::raw_ostream &OS, const LoanManager &, const OriginManager &OM) const override
Definition Facts.cpp:94
void dump(llvm::raw_ostream &OS, const LoanManager &LM, const OriginManager &OM) const override
Definition Facts.cpp:20
Manages the creation, storage and retrieval of loans.
Definition Loans.h:129
const Loan * getLoan(LoanID ID) const
Definition Loans.h:140
void dump(llvm::raw_ostream &OS) const
Definition Loans.cpp:34
void dump(llvm::raw_ostream &OS, const LoanManager &, const OriginManager &OM) const override
Definition Facts.cpp:52
void dump(llvm::raw_ostream &OS, const LoanManager &, const OriginManager &OM) const override
Definition Facts.cpp:40
A list of origins representing levels of indirection for pointer-like types.
Definition Origins.h:94
OriginList * peelOuterOrigin() const
Definition Origins.h:98
Manages the creation, storage, and retrieval of origins for pointer-like variables and expressions.
Definition Origins.h:126
void dump(OriginID OID, llvm::raw_ostream &OS) const
Definition Origins.cpp:209
void dump(llvm::raw_ostream &OS, const LoanManager &, const OriginManager &OM) const override
Definition Facts.cpp:59
A dummy-fact used to mark a specific point in the code for testing.
Definition Facts.h:303
void dump(llvm::raw_ostream &OS, const LoanManager &, const OriginManager &) const override
Definition Facts.cpp:101
void dump(llvm::raw_ostream &OS, const LoanManager &, const OriginManager &OM) const override
Definition Facts.cpp:80
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