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() << ", \"terminator\": "; 153 154 if (!T) { 155 Out << "null, \"term_kind\": null"; 156 break; 157 } 158 159 E.getSrc()->printTerminatorJson(Out, Context.getLangOpts(), 160 /*AddQuotes=*/true); 161 Out << ", "; 162 printLocJson(Out, T->getBeginLoc(), SM); 163 164 Out << ", \"term_kind\": \""; 165 if (isa<SwitchStmt>(T)) { 166 Out << "SwitchStmt\", \"case\": "; 167 if (const Stmt *Label = E.getDst()->getLabel()) { 168 if (const auto *C = dyn_cast<CaseStmt>(Label)) { 169 Out << "{ \"lhs\": "; 170 if (const Stmt *LHS = C->getLHS()) { 171 LHS->printJson(Out, nullptr, PP, AddQuotes); 172 } else { 173 Out << "null"; 174 } 175 176 Out << ", \"rhs\": "; 177 if (const Stmt *RHS = C->getRHS()) { 178 RHS->printJson(Out, nullptr, PP, AddQuotes); 179 } else { 180 Out << "null"; 181 } 182 Out << " }"; 183 } else { 184 assert(isa<DefaultStmt>(Label)); 185 Out << "\"default\""; 186 } 187 } else { 188 Out << "\"implicit default\""; 189 } 190 } else if (isa<IndirectGotoStmt>(T)) { 191 // FIXME: More info. 192 Out << "IndirectGotoStmt\""; 193 } else { 194 Out << "Condition\", \"value\": " 195 << (*E.getSrc()->succ_begin() == E.getDst() ? "true" : "false"); 196 } 197 break; 198 } 199 200 default: { 201 const Stmt *S = castAs<StmtPoint>().getStmt(); 202 assert(S != nullptr && "Expecting non-null Stmt"); 203 204 Out << "Statement\", \"stmt_kind\": \"" << S->getStmtClassName() 205 << "\", \"stmt_id\": " << S->getID(Context) 206 << ", \"pointer\": \"" << (const void *)S << "\", \"pretty\": "; 207 208 S->printJson(Out, nullptr, PP, AddQuotes); 209 210 Out << ", "; 211 printLocJson(Out, S->getBeginLoc(), SM); 212 213 Out << ", \"stmt_point_kind\": "; 214 if (getAs<PreStmt>()) 215 Out << "\"PreStmt\""; 216 else if (getAs<PostLoad>()) 217 Out << "\"PostLoad\""; 218 else if (getAs<PostStore>()) 219 Out << "\"PostStore\""; 220 else if (getAs<PostLValue>()) 221 Out << "\"PostLValue\""; 222 else if (getAs<PostAllocatorCall>()) 223 Out << "\"PostAllocatorCall\""; 224 else 225 Out << "null"; 226 227 break; 228 } 229 } 230 } 231 232 SimpleProgramPointTag::SimpleProgramPointTag(StringRef MsgProvider, 233 StringRef Msg) 234 : Desc((MsgProvider + " : " + Msg).str()) {} 235 236 StringRef SimpleProgramPointTag::getTagDescription() const { 237 return Desc; 238 } 239