1 //==- ProgramPoint.cpp - Program Points for Path-Sensitive Analysis -*- 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 the interface ProgramPoint, which identifies a 10 // distinct location in a function. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "clang/Analysis/ProgramPoint.h" 15 16 using namespace clang; 17 18 ProgramPointTag::~ProgramPointTag() {} 19 20 ProgramPoint ProgramPoint::getProgramPoint(const Stmt *S, ProgramPoint::Kind K, 21 const LocationContext *LC, 22 const ProgramPointTag *tag){ 23 switch (K) { 24 default: 25 llvm_unreachable("Unhandled ProgramPoint kind"); 26 case ProgramPoint::PreStmtKind: 27 return PreStmt(S, LC, tag); 28 case ProgramPoint::PostStmtKind: 29 return PostStmt(S, LC, tag); 30 case ProgramPoint::PreLoadKind: 31 return PreLoad(S, LC, tag); 32 case ProgramPoint::PostLoadKind: 33 return PostLoad(S, LC, tag); 34 case ProgramPoint::PreStoreKind: 35 return PreStore(S, LC, tag); 36 case ProgramPoint::PostLValueKind: 37 return PostLValue(S, LC, tag); 38 case ProgramPoint::PostStmtPurgeDeadSymbolsKind: 39 return PostStmtPurgeDeadSymbols(S, LC, tag); 40 case ProgramPoint::PreStmtPurgeDeadSymbolsKind: 41 return PreStmtPurgeDeadSymbols(S, LC, tag); 42 } 43 } 44 45 LLVM_DUMP_METHOD void ProgramPoint::dump() const { 46 return printJson(llvm::errs()); 47 } 48 49 static void printLocJson(raw_ostream &Out, SourceLocation Loc, 50 const SourceManager &SM) { 51 Out << "\"location\": "; 52 if (!Loc.isFileID()) { 53 Out << "null"; 54 return; 55 } 56 57 Out << "{ \"line\": " << SM.getExpansionLineNumber(Loc) 58 << ", \"column\": " << SM.getExpansionColumnNumber(Loc) << " }"; 59 } 60 61 void ProgramPoint::printJson(llvm::raw_ostream &Out, const char *NL) const { 62 const ASTContext &Context = 63 getLocationContext()->getAnalysisDeclContext()->getASTContext(); 64 const SourceManager &SM = Context.getSourceManager(); 65 const PrintingPolicy &PP = Context.getPrintingPolicy(); 66 const bool AddQuotes = true; 67 68 Out << "\"kind\": \""; 69 switch (getKind()) { 70 case ProgramPoint::BlockEntranceKind: 71 Out << "BlockEntrance\"" 72 << ", \"block_id\": " 73 << castAs<BlockEntrance>().getBlock()->getBlockID(); 74 break; 75 76 case ProgramPoint::FunctionExitKind: { 77 auto FEP = getAs<FunctionExitPoint>(); 78 Out << "FunctionExit\"" 79 << ", \"block_id\": " << FEP->getBlock()->getBlockID() 80 << ", \"stmt_id\": "; 81 82 if (const ReturnStmt *RS = FEP->getStmt()) { 83 Out << RS->getID(Context) << ", \"stmt\": "; 84 RS->printJson(Out, nullptr, PP, AddQuotes); 85 } else { 86 Out << "null, \"stmt\": null"; 87 } 88 break; 89 } 90 case ProgramPoint::BlockExitKind: 91 llvm_unreachable("BlockExitKind"); 92 break; 93 case ProgramPoint::CallEnterKind: 94 Out << "CallEnter\""; 95 break; 96 case ProgramPoint::CallExitBeginKind: 97 Out << "CallExitBegin\""; 98 break; 99 case ProgramPoint::CallExitEndKind: 100 Out << "CallExitEnd\""; 101 break; 102 case ProgramPoint::PostStmtPurgeDeadSymbolsKind: 103 Out << "PostStmtPurgeDeadSymbols\""; 104 break; 105 case ProgramPoint::PreStmtPurgeDeadSymbolsKind: 106 Out << "PreStmtPurgeDeadSymbols\""; 107 break; 108 case ProgramPoint::EpsilonKind: 109 Out << "EpsilonPoint\""; 110 break; 111 112 case ProgramPoint::LoopExitKind: 113 Out << "LoopExit\", \"stmt\": \"" 114 << castAs<LoopExit>().getLoopStmt()->getStmtClassName() << '\"'; 115 break; 116 117 case ProgramPoint::PreImplicitCallKind: { 118 ImplicitCallPoint PC = castAs<ImplicitCallPoint>(); 119 Out << "PreCall\", \"decl\": \"" 120 << PC.getDecl()->getAsFunction()->getQualifiedNameAsString() << "\", "; 121 printLocJson(Out, PC.getLocation(), SM); 122 break; 123 } 124 125 case ProgramPoint::PostImplicitCallKind: { 126 ImplicitCallPoint PC = castAs<ImplicitCallPoint>(); 127 Out << "PostCall\", \"decl\": \"" 128 << PC.getDecl()->getAsFunction()->getQualifiedNameAsString() << "\", "; 129 printLocJson(Out, PC.getLocation(), SM); 130 break; 131 } 132 133 case ProgramPoint::PostInitializerKind: { 134 Out << "PostInitializer\", "; 135 const CXXCtorInitializer *Init = castAs<PostInitializer>().getInitializer(); 136 if (const FieldDecl *FD = Init->getAnyMember()) { 137 Out << "\"field_decl\": \"" << *FD << '\"'; 138 } else { 139 Out << "\"type\": \""; 140 QualType Ty = Init->getTypeSourceInfo()->getType(); 141 Ty = Ty.getLocalUnqualifiedType(); 142 Ty.print(Out, Context.getLangOpts()); 143 Out << '\"'; 144 } 145 break; 146 } 147 148 case ProgramPoint::BlockEdgeKind: { 149 const BlockEdge &E = castAs<BlockEdge>(); 150 const Stmt *T = E.getSrc()->getTerminatorStmt(); 151 Out << "Edge\", \"src_id\": " << E.getSrc()->getBlockID() 152 << ", \"dst_id\": " << E.getDst()->getBlockID() 153 << ", \"terminator\": " << (!T ? "null, \"term_kind\": null" : "\""); 154 if (!T) 155 break; 156 157 E.getSrc()->printTerminator(Out, Context.getLangOpts()); 158 Out << "\", "; 159 printLocJson(Out, T->getBeginLoc(), SM); 160 161 Out << ", \"term_kind\": \""; 162 if (isa<SwitchStmt>(T)) { 163 Out << "SwitchStmt\", \"case\": "; 164 if (const Stmt *Label = E.getDst()->getLabel()) { 165 if (const auto *C = dyn_cast<CaseStmt>(Label)) { 166 Out << "{ \"lhs\": "; 167 if (const Stmt *LHS = C->getLHS()) { 168 LHS->printJson(Out, nullptr, PP, AddQuotes); 169 } else { 170 Out << "null"; 171 } 172 173 Out << ", \"rhs\": "; 174 if (const Stmt *RHS = C->getRHS()) { 175 RHS->printJson(Out, nullptr, PP, AddQuotes); 176 } else { 177 Out << "null"; 178 } 179 Out << " }"; 180 } else { 181 assert(isa<DefaultStmt>(Label)); 182 Out << "\"default\""; 183 } 184 } else { 185 Out << "\"implicit default\""; 186 } 187 } else if (isa<IndirectGotoStmt>(T)) { 188 // FIXME: More info. 189 Out << "IndirectGotoStmt\""; 190 } else { 191 Out << "Condition\", \"value\": " 192 << (*E.getSrc()->succ_begin() == E.getDst() ? "true" : "false"); 193 } 194 break; 195 } 196 197 default: { 198 const Stmt *S = castAs<StmtPoint>().getStmt(); 199 assert(S != nullptr && "Expecting non-null Stmt"); 200 201 Out << "Statement\", \"stmt_kind\": \"" << S->getStmtClassName() 202 << "\", \"stmt_id\": " << S->getID(Context) 203 << ", \"pointer\": \"" << (const void *)S << "\", \"pretty\": "; 204 205 S->printJson(Out, nullptr, PP, AddQuotes); 206 207 Out << ", "; 208 printLocJson(Out, S->getBeginLoc(), SM); 209 210 Out << ", \"stmt_point_kind\": "; 211 if (getAs<PreStmt>()) 212 Out << "\"PreStmt\""; 213 else if (getAs<PostLoad>()) 214 Out << "\"PostLoad\""; 215 else if (getAs<PostStore>()) 216 Out << "\"PostStore\""; 217 else if (getAs<PostLValue>()) 218 Out << "\"PostLValue\""; 219 else if (getAs<PostAllocatorCall>()) 220 Out << "\"PostAllocatorCall\""; 221 else 222 Out << "null"; 223 224 break; 225 } 226 } 227 } 228 229 SimpleProgramPointTag::SimpleProgramPointTag(StringRef MsgProvider, 230 StringRef Msg) 231 : Desc((MsgProvider + " : " + Msg).str()) {} 232 233 StringRef SimpleProgramPointTag::getTagDescription() const { 234 return Desc; 235 } 236