| 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 | |
| 9 | #include "clang/Analysis/Analyses/LifetimeSafety/Facts.h" |
| 10 | #include "clang/AST/Decl.h" |
| 11 | #include "clang/Analysis/Analyses/LifetimeSafety/LoanPropagation.h" |
| 12 | #include "clang/Analysis/Analyses/PostOrderCFGView.h" |
| 13 | |
| 14 | namespace clang::lifetimes::internal { |
| 15 | |
| 16 | void Fact::dump(llvm::raw_ostream &OS, const LoanManager &, |
| 17 | const OriginManager &, const LoanPropagationAnalysis *) const { |
| 18 | OS << "Fact (Kind: " << static_cast<int>(K) << ")\n" ; |
| 19 | } |
| 20 | |
| 21 | void IssueFact::dump(llvm::raw_ostream &OS, const LoanManager &LM, |
| 22 | const OriginManager &OM, |
| 23 | const LoanPropagationAnalysis *) const { |
| 24 | OS << "Issue (" ; |
| 25 | LM.getLoan(ID: getLoanID())->dump(OS); |
| 26 | OS << ", ToOrigin: " ; |
| 27 | OM.dump(OID: getOriginID(), OS); |
| 28 | OS << ")\n" ; |
| 29 | } |
| 30 | |
| 31 | void ExpireFact::dump(llvm::raw_ostream &OS, const LoanManager &LM, |
| 32 | const OriginManager &OM, |
| 33 | const LoanPropagationAnalysis *LPA) const { |
| 34 | OS << "Expire (" ; |
| 35 | getAccessPath().dump(OS); |
| 36 | if (auto OID = getOriginID()) { |
| 37 | OS << ", Origin: " ; |
| 38 | OM.dump(OID: *OID, OS); |
| 39 | } |
| 40 | OS << ")\n" ; |
| 41 | } |
| 42 | |
| 43 | void OriginFlowFact::dump(llvm::raw_ostream &OS, const LoanManager &LM, |
| 44 | const OriginManager &OM, |
| 45 | const LoanPropagationAnalysis *LPA) const { |
| 46 | OS << "OriginFlow: \n" ; |
| 47 | OS << "\tDest: " ; |
| 48 | OM.dump(OID: getDestOriginID(), OS); |
| 49 | if (LPA) { |
| 50 | LoanSet DestinationLoans = LPA->getLoans(OID: getDestOriginID(), P: this); |
| 51 | if (DestinationLoans.isEmpty()) |
| 52 | OS << " has no loans" ; |
| 53 | else { |
| 54 | OS << " has loans to { " ; |
| 55 | for (LoanID LID : DestinationLoans) { |
| 56 | LM.getLoan(ID: LID)->getAccessPath().dump(OS); |
| 57 | OS << " " ; |
| 58 | } |
| 59 | OS << "}" ; |
| 60 | } |
| 61 | } |
| 62 | OS << "\n" ; |
| 63 | OS << "\tSrc: " ; |
| 64 | OM.dump(OID: getSrcOriginID(), OS); |
| 65 | OS << (getKillDest() ? "" : ", Merge" ); |
| 66 | OS << "\n" ; |
| 67 | } |
| 68 | |
| 69 | void MovedOriginFact::dump(llvm::raw_ostream &OS, const LoanManager &, |
| 70 | const OriginManager &OM, |
| 71 | const LoanPropagationAnalysis *) const { |
| 72 | OS << "MovedOrigins (" ; |
| 73 | OM.dump(OID: getMovedOrigin(), OS); |
| 74 | OS << ")\n" ; |
| 75 | } |
| 76 | |
| 77 | void ReturnEscapeFact::dump(llvm::raw_ostream &OS, const LoanManager &, |
| 78 | const OriginManager &OM, |
| 79 | const LoanPropagationAnalysis *) const { |
| 80 | OS << "OriginEscapes (" ; |
| 81 | OM.dump(OID: getEscapedOriginID(), OS); |
| 82 | OS << ", via Return)\n" ; |
| 83 | } |
| 84 | |
| 85 | void FieldEscapeFact::dump(llvm::raw_ostream &OS, const LoanManager &, |
| 86 | const OriginManager &OM, |
| 87 | const LoanPropagationAnalysis *) const { |
| 88 | OS << "OriginEscapes (" ; |
| 89 | OM.dump(OID: getEscapedOriginID(), OS); |
| 90 | OS << ", via Field)\n" ; |
| 91 | } |
| 92 | |
| 93 | void GlobalEscapeFact::dump(llvm::raw_ostream &OS, const LoanManager &, |
| 94 | const OriginManager &OM, |
| 95 | const LoanPropagationAnalysis *) const { |
| 96 | OS << "OriginEscapes (" ; |
| 97 | OM.dump(OID: getEscapedOriginID(), OS); |
| 98 | OS << ", via Global)\n" ; |
| 99 | } |
| 100 | |
| 101 | void UseFact::dump(llvm::raw_ostream &OS, const LoanManager &, |
| 102 | const OriginManager &OM, |
| 103 | const LoanPropagationAnalysis *) const { |
| 104 | OS << "Use (" ; |
| 105 | size_t NumUsedOrigins = getUsedOrigins()->getLength(); |
| 106 | size_t I = 0; |
| 107 | for (const OriginList *Cur = getUsedOrigins(); Cur; |
| 108 | Cur = Cur->peelOuterOrigin(), ++I) { |
| 109 | OM.dump(OID: Cur->getOuterOriginID(), OS); |
| 110 | if (I < NumUsedOrigins - 1) |
| 111 | OS << ", " ; |
| 112 | } |
| 113 | OS << ", " << (isWritten() ? "Write" : "Read" ) << ")\n" ; |
| 114 | } |
| 115 | |
| 116 | void InvalidateOriginFact::dump(llvm::raw_ostream &OS, const LoanManager &, |
| 117 | const OriginManager &OM, |
| 118 | const LoanPropagationAnalysis *) const { |
| 119 | OS << "InvalidateOrigin (" ; |
| 120 | OM.dump(OID: getInvalidatedOrigin(), OS); |
| 121 | OS << ")\n" ; |
| 122 | } |
| 123 | |
| 124 | void TestPointFact::dump(llvm::raw_ostream &OS, const LoanManager &, |
| 125 | const OriginManager &, |
| 126 | const LoanPropagationAnalysis *) const { |
| 127 | OS << "TestPoint (Annotation: \"" << getAnnotation() << "\")\n" ; |
| 128 | } |
| 129 | |
| 130 | void KillOriginFact::dump(llvm::raw_ostream &OS, const LoanManager &, |
| 131 | const OriginManager &OM, |
| 132 | const LoanPropagationAnalysis *) const { |
| 133 | OS << "KillOrigin (" ; |
| 134 | OM.dump(OID: getKilledOrigin(), OS); |
| 135 | OS << ")\n" ; |
| 136 | } |
| 137 | |
| 138 | llvm::StringMap<ProgramPoint> FactManager::getTestPoints() const { |
| 139 | llvm::StringMap<ProgramPoint> AnnotationToPointMap; |
| 140 | for (const auto &BlockFacts : BlockToFacts) { |
| 141 | for (const Fact *F : BlockFacts) { |
| 142 | if (const auto *TPF = F->getAs<TestPointFact>()) { |
| 143 | StringRef PointName = TPF->getAnnotation(); |
| 144 | assert(!AnnotationToPointMap.contains(PointName) && |
| 145 | "more than one test points with the same name" ); |
| 146 | AnnotationToPointMap[PointName] = F; |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | return AnnotationToPointMap; |
| 151 | } |
| 152 | |
| 153 | void FactManager::dump(const CFG &Cfg, AnalysisDeclContext &AC, |
| 154 | const LoanPropagationAnalysis *LPA) const { |
| 155 | llvm::dbgs() << "==========================================\n" ; |
| 156 | llvm::dbgs() << " Lifetime Analysis Facts:\n" ; |
| 157 | llvm::dbgs() << "==========================================\n" ; |
| 158 | if (const Decl *D = AC.getDecl()) |
| 159 | if (const auto *ND = dyn_cast<NamedDecl>(Val: D)) |
| 160 | llvm::dbgs() << "Function: " << ND->getQualifiedNameAsString() << "\n" ; |
| 161 | // Print blocks in the order as they appear in code for a stable ordering. |
| 162 | for (const CFGBlock *B : *AC.getAnalysis<PostOrderCFGView>()) { |
| 163 | llvm::dbgs() << " Block B" << B->getBlockID() << ":\n" ; |
| 164 | for (const Fact *F : getFacts(B)) { |
| 165 | llvm::dbgs() << " " ; |
| 166 | F->dump(OS&: llvm::dbgs(), LoanMgr, OriginMgr, LPA); |
| 167 | } |
| 168 | llvm::dbgs() << " End of Block\n" ; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | llvm::ArrayRef<const Fact *> |
| 173 | FactManager::getBlockContaining(ProgramPoint P) const { |
| 174 | return BlockToFacts[getBlockID(P)]; |
| 175 | } |
| 176 | |
| 177 | size_t FactManager::getBlockID(ProgramPoint P) const { |
| 178 | for (size_t i = 0; i < BlockToFacts.size(); ++i) |
| 179 | for (const Fact *F : BlockToFacts[i]) |
| 180 | if (F == P) |
| 181 | return i; |
| 182 | llvm_unreachable("Failed to find BlockID for given ProgramPoint" ); |
| 183 | } |
| 184 | } // namespace clang::lifetimes::internal |
| 185 | |