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 &) const {
31 OS << "Expire (";
32 LM.getLoan(getLoanID())->dump(OS);
33 OS << ")\n";
34}
35
36void OriginFlowFact::dump(llvm::raw_ostream &OS, const LoanManager &,
37 const OriginManager &OM) const {
38 OS << "OriginFlow: \n";
39 OS << "\tDest: ";
40 OM.dump(getDestOriginID(), OS);
41 OS << "\n";
42 OS << "\tSrc: ";
43 OM.dump(getSrcOriginID(), OS);
44 OS << (getKillDest() ? "" : ", Merge");
45 OS << "\n";
46}
47
48void ReturnEscapeFact::dump(llvm::raw_ostream &OS, const LoanManager &,
49 const OriginManager &OM) const {
50 OS << "OriginEscapes (";
51 OM.dump(getEscapedOriginID(), OS);
52 OS << ", via Return)\n";
53}
54
55void FieldEscapeFact::dump(llvm::raw_ostream &OS, const LoanManager &,
56 const OriginManager &OM) const {
57 OS << "OriginEscapes (";
58 OM.dump(getEscapedOriginID(), OS);
59 OS << ", via Field)\n";
60}
61
62void UseFact::dump(llvm::raw_ostream &OS, const LoanManager &,
63 const OriginManager &OM) const {
64 OS << "Use (";
65 size_t NumUsedOrigins = getUsedOrigins()->getLength();
66 size_t I = 0;
67 for (const OriginList *Cur = getUsedOrigins(); Cur;
68 Cur = Cur->peelOuterOrigin(), ++I) {
69 OM.dump(Cur->getOuterOriginID(), OS);
70 if (I < NumUsedOrigins - 1)
71 OS << ", ";
72 }
73 OS << ", " << (isWritten() ? "Write" : "Read") << ")\n";
74}
75
76void TestPointFact::dump(llvm::raw_ostream &OS, const LoanManager &,
77 const OriginManager &) const {
78 OS << "TestPoint (Annotation: \"" << getAnnotation() << "\")\n";
79}
80
81llvm::StringMap<ProgramPoint> FactManager::getTestPoints() const {
82 llvm::StringMap<ProgramPoint> AnnotationToPointMap;
83 for (const auto &BlockFacts : BlockToFacts) {
84 for (const Fact *F : BlockFacts) {
85 if (const auto *TPF = F->getAs<TestPointFact>()) {
86 StringRef PointName = TPF->getAnnotation();
87 assert(!AnnotationToPointMap.contains(PointName) &&
88 "more than one test points with the same name");
89 AnnotationToPointMap[PointName] = F;
90 }
91 }
92 }
93 return AnnotationToPointMap;
94}
95
96void FactManager::dump(const CFG &Cfg, AnalysisDeclContext &AC) const {
97 llvm::dbgs() << "==========================================\n";
98 llvm::dbgs() << " Lifetime Analysis Facts:\n";
99 llvm::dbgs() << "==========================================\n";
100 if (const Decl *D = AC.getDecl())
101 if (const auto *ND = dyn_cast<NamedDecl>(D))
102 llvm::dbgs() << "Function: " << ND->getQualifiedNameAsString() << "\n";
103 // Print blocks in the order as they appear in code for a stable ordering.
104 for (const CFGBlock *B : *AC.getAnalysis<PostOrderCFGView>()) {
105 llvm::dbgs() << " Block B" << B->getBlockID() << ":\n";
106 for (const Fact *F : getFacts(B)) {
107 llvm::dbgs() << " ";
108 F->dump(llvm::dbgs(), LoanMgr, OriginMgr);
109 }
110 llvm::dbgs() << " End of Block\n";
111 }
112}
113
116 for (const auto &BlockToFactsVec : BlockToFacts) {
117 for (const Fact *F : BlockToFactsVec)
118 if (F == P)
119 return BlockToFactsVec;
120 }
121 return {};
122}
123
124} // 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:605
Represents a source-level, intra-procedural CFG that represents the control-flow of a Stmt.
Definition CFG.h:1218
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
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:246
llvm::StringMap< ProgramPoint > getTestPoints() const
Retrieves program points that were specially marked in the source code for testing.
Definition Facts.cpp:81
void dump(const CFG &Cfg, AnalysisDeclContext &AC) const
Definition Facts.cpp:96
llvm::ArrayRef< const Fact * > getBlockContaining(ProgramPoint P) const
Retrieves all the facts in the block containing Program Point P.
Definition Facts.cpp:115
An abstract base class for a single, atomic lifetime-relevant event.
Definition Facts.h:31
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:55
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:146
const Loan * getLoan(LoanID ID) const
Definition Loans.h:163
virtual void dump(llvm::raw_ostream &OS) const =0
void dump(llvm::raw_ostream &OS, const LoanManager &, const OriginManager &OM) const override
Definition Facts.cpp:36
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:193
void dump(llvm::raw_ostream &OS, const LoanManager &, const OriginManager &OM) const override
Definition Facts.cpp:48
A dummy-fact used to mark a specific point in the code for testing.
Definition Facts.h:224
void dump(llvm::raw_ostream &OS, const LoanManager &, const OriginManager &) const override
Definition Facts.cpp:76
void dump(llvm::raw_ostream &OS, const LoanManager &, const OriginManager &OM) const override
Definition Facts.cpp:62
const OriginList * getUsedOrigins() const
Definition Facts.h:213
const Fact * ProgramPoint
A ProgramPoint identifies a location in the CFG by pointing to a specific Fact.
Definition Facts.h:82