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::EpsilonKind: 103 Out << "EpsilonPoint\""; 104 break; 105 106 case ProgramPoint::LoopExitKind: 107 Out << "LoopExit\", \"stmt\": \"" 108 << castAs<LoopExit>().getLoopStmt()->getStmtClassName() << '\"'; 109 break; 110 111 case ProgramPoint::PreImplicitCallKind: { 112 ImplicitCallPoint PC = castAs<ImplicitCallPoint>(); 113 Out << "PreCall\", \"decl\": \"" 114 << PC.getDecl()->getAsFunction()->getQualifiedNameAsString() << "\", "; 115 printLocJson(Out, PC.getLocation(), SM); 116 break; 117 } 118 119 case ProgramPoint::PostImplicitCallKind: { 120 ImplicitCallPoint PC = castAs<ImplicitCallPoint>(); 121 Out << "PostCall\", \"decl\": \"" 122 << PC.getDecl()->getAsFunction()->getQualifiedNameAsString() << "\", "; 123 printLocJson(Out, PC.getLocation(), SM); 124 break; 125 } 126 127 case ProgramPoint::PostInitializerKind: { 128 Out << "PostInitializer\", "; 129 const CXXCtorInitializer *Init = castAs<PostInitializer>().getInitializer(); 130 if (const FieldDecl *FD = Init->getAnyMember()) { 131 Out << "\"field_decl\": \"" << *FD << '\"'; 132 } else { 133 Out << "\"type\": \""; 134 QualType Ty = Init->getTypeSourceInfo()->getType(); 135 Ty = Ty.getLocalUnqualifiedType(); 136 Ty.print(Out, Context.getLangOpts()); 137 Out << '\"'; 138 } 139 break; 140 } 141 142 case ProgramPoint::BlockEdgeKind: { 143 const BlockEdge &E = castAs<BlockEdge>(); 144 const Stmt *T = E.getSrc()->getTerminatorStmt(); 145 Out << "Edge\", \"src_id\": " << E.getSrc()->getBlockID() 146 << ", \"dst_id\": " << E.getDst()->getBlockID() << ", \"terminator\": "; 147 148 if (!T) { 149 Out << "null, \"term_kind\": null"; 150 break; 151 } 152 153 E.getSrc()->printTerminatorJson(Out, Context.getLangOpts(), 154 /*AddQuotes=*/true); 155 Out << ", "; 156 printLocJson(Out, T->getBeginLoc(), SM); 157 158 Out << ", \"term_kind\": \""; 159 if (isa<SwitchStmt>(T)) { 160 Out << "SwitchStmt\", \"case\": "; 161 if (const Stmt *Label = E.getDst()->getLabel()) { 162 if (const auto *C = dyn_cast<CaseStmt>(Label)) { 163 Out << "{ \"lhs\": "; 164 if (const Stmt *LHS = C->getLHS()) { 165 LHS->printJson(Out, nullptr, PP, AddQuotes); 166 } else { 167 Out << "null"; 168 } 169 170 Out << ", \"rhs\": "; 171 if (const Stmt *RHS = C->getRHS()) { 172 RHS->printJson(Out, nullptr, PP, AddQuotes); 173 } else { 174 Out << "null"; 175 } 176 Out << " }"; 177 } else { 178 assert(isa<DefaultStmt>(Label)); 179 Out << "\"default\""; 180 } 181 } else { 182 Out << "\"implicit default\""; 183 } 184 } else if (isa<IndirectGotoStmt>(T)) { 185 // FIXME: More info. 186 Out << "IndirectGotoStmt\""; 187 } else { 188 Out << "Condition\", \"value\": " 189 << (*E.getSrc()->succ_begin() == E.getDst() ? "true" : "false"); 190 } 191 break; 192 } 193 194 default: { 195 const Stmt *S = castAs<StmtPoint>().getStmt(); 196 assert(S != nullptr && "Expecting non-null Stmt"); 197 198 Out << "Statement\", \"stmt_kind\": \"" << S->getStmtClassName() 199 << "\", \"stmt_id\": " << S->getID(Context) 200 << ", \"pointer\": \"" << (const void *)S << "\", \"pretty\": "; 201 202 S->printJson(Out, nullptr, PP, AddQuotes); 203 204 Out << ", "; 205 printLocJson(Out, S->getBeginLoc(), SM); 206 207 Out << ", \"stmt_point_kind\": \""; 208 if (getAs<PreLoad>()) 209 Out << "PreLoad"; 210 else if (getAs<PreStore>()) 211 Out << "PreStore"; 212 else if (getAs<PostAllocatorCall>()) 213 Out << "PostAllocatorCall"; 214 else if (getAs<PostCondition>()) 215 Out << "PostCondition"; 216 else if (getAs<PostLoad>()) 217 Out << "PostLoad"; 218 else if (getAs<PostLValue>()) 219 Out << "PostLValue"; 220 else if (getAs<PostStore>()) 221 Out << "PostStore"; 222 else if (getAs<PostStmt>()) 223 Out << "PostStmt"; 224 else if (getAs<PostStmtPurgeDeadSymbols>()) 225 Out << "PostStmtPurgeDeadSymbols"; 226 else if (getAs<PreStmtPurgeDeadSymbols>()) 227 Out << "PreStmtPurgeDeadSymbols"; 228 else if (getAs<PreStmt>()) 229 Out << "PreStmt"; 230 else { 231 Out << "\nKind: '" << getKind(); 232 llvm_unreachable("' is unhandled StmtPoint kind!"); 233 } 234 235 Out << '\"'; 236 break; 237 } 238 } 239 } 240 241 SimpleProgramPointTag::SimpleProgramPointTag(StringRef MsgProvider, 242 StringRef Msg) 243 : Desc((MsgProvider + " : " + Msg).str()) {} 244 245 StringRef SimpleProgramPointTag::getTagDescription() const { 246 return Desc; 247 } 248