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