1 //==- ProgramPoint.cpp - Program Points for Path-Sensitive Analysis -*- C++ -*-/
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
7 //===----------------------------------------------------------------------===//
9 // This file defines the interface ProgramPoint, which identifies a
10 // distinct location in a function.
12 //===----------------------------------------------------------------------===//
14 #include "clang/Analysis/ProgramPoint.h"
15 #include "clang/Basic/JsonSupport.h"
17 using namespace clang;
19 ProgramPointTag::~ProgramPointTag() {}
21 ProgramPoint ProgramPoint::getProgramPoint(const Stmt *S, ProgramPoint::Kind K,
22 const LocationContext *LC,
23 const ProgramPointTag *tag){
26 llvm_unreachable("Unhandled ProgramPoint kind");
27 case ProgramPoint::PreStmtKind:
28 return PreStmt(S, LC, tag);
29 case ProgramPoint::PostStmtKind:
30 return PostStmt(S, LC, tag);
31 case ProgramPoint::PreLoadKind:
32 return PreLoad(S, LC, tag);
33 case ProgramPoint::PostLoadKind:
34 return PostLoad(S, LC, tag);
35 case ProgramPoint::PreStoreKind:
36 return PreStore(S, LC, tag);
37 case ProgramPoint::PostLValueKind:
38 return PostLValue(S, LC, tag);
39 case ProgramPoint::PostStmtPurgeDeadSymbolsKind:
40 return PostStmtPurgeDeadSymbols(S, LC, tag);
41 case ProgramPoint::PreStmtPurgeDeadSymbolsKind:
42 return PreStmtPurgeDeadSymbols(S, LC, tag);
46 LLVM_DUMP_METHOD void ProgramPoint::dump() const {
47 return printJson(llvm::errs());
50 void ProgramPoint::printJson(llvm::raw_ostream &Out, const char *NL) const {
51 const ASTContext &Context =
52 getLocationContext()->getAnalysisDeclContext()->getASTContext();
53 const SourceManager &SM = Context.getSourceManager();
54 const PrintingPolicy &PP = Context.getPrintingPolicy();
55 const bool AddQuotes = true;
57 Out << "\"kind\": \"";
59 case ProgramPoint::BlockEntranceKind:
60 Out << "BlockEntrance\""
62 << castAs<BlockEntrance>().getBlock()->getBlockID();
65 case ProgramPoint::FunctionExitKind: {
66 auto FEP = getAs<FunctionExitPoint>();
67 Out << "FunctionExit\""
68 << ", \"block_id\": " << FEP->getBlock()->getBlockID()
71 if (const ReturnStmt *RS = FEP->getStmt()) {
72 Out << RS->getID(Context) << ", \"stmt\": ";
73 RS->printJson(Out, nullptr, PP, AddQuotes);
75 Out << "null, \"stmt\": null";
79 case ProgramPoint::BlockExitKind:
80 llvm_unreachable("BlockExitKind");
82 case ProgramPoint::CallEnterKind:
85 case ProgramPoint::CallExitBeginKind:
86 Out << "CallExitBegin\"";
88 case ProgramPoint::CallExitEndKind:
89 Out << "CallExitEnd\"";
91 case ProgramPoint::EpsilonKind:
92 Out << "EpsilonPoint\"";
95 case ProgramPoint::LoopExitKind:
96 Out << "LoopExit\", \"stmt\": \""
97 << castAs<LoopExit>().getLoopStmt()->getStmtClassName() << '\"';
100 case ProgramPoint::PreImplicitCallKind: {
101 ImplicitCallPoint PC = castAs<ImplicitCallPoint>();
102 Out << "PreCall\", \"decl\": \""
103 << PC.getDecl()->getAsFunction()->getQualifiedNameAsString()
104 << "\", \"location\": ";
105 printSourceLocationAsJson(Out, PC.getLocation(), SM);
109 case ProgramPoint::PostImplicitCallKind: {
110 ImplicitCallPoint PC = castAs<ImplicitCallPoint>();
111 Out << "PostCall\", \"decl\": \""
112 << PC.getDecl()->getAsFunction()->getQualifiedNameAsString()
113 << "\", \"location\": ";
114 printSourceLocationAsJson(Out, PC.getLocation(), SM);
118 case ProgramPoint::PostInitializerKind: {
119 Out << "PostInitializer\", ";
120 const CXXCtorInitializer *Init = castAs<PostInitializer>().getInitializer();
121 if (const FieldDecl *FD = Init->getAnyMember()) {
122 Out << "\"field_decl\": \"" << *FD << '\"';
124 Out << "\"type\": \"";
125 QualType Ty = Init->getTypeSourceInfo()->getType();
126 Ty = Ty.getLocalUnqualifiedType();
127 Ty.print(Out, Context.getLangOpts());
133 case ProgramPoint::BlockEdgeKind: {
134 const BlockEdge &E = castAs<BlockEdge>();
135 const Stmt *T = E.getSrc()->getTerminatorStmt();
136 Out << "Edge\", \"src_id\": " << E.getSrc()->getBlockID()
137 << ", \"dst_id\": " << E.getDst()->getBlockID() << ", \"terminator\": ";
140 Out << "null, \"term_kind\": null";
144 E.getSrc()->printTerminatorJson(Out, Context.getLangOpts(),
146 Out << ", \"location\": ";
147 printSourceLocationAsJson(Out, T->getBeginLoc(), SM);
149 Out << ", \"term_kind\": \"";
150 if (isa<SwitchStmt>(T)) {
151 Out << "SwitchStmt\", \"case\": ";
152 if (const Stmt *Label = E.getDst()->getLabel()) {
153 if (const auto *C = dyn_cast<CaseStmt>(Label)) {
154 Out << "{ \"lhs\": ";
155 if (const Stmt *LHS = C->getLHS()) {
156 LHS->printJson(Out, nullptr, PP, AddQuotes);
161 Out << ", \"rhs\": ";
162 if (const Stmt *RHS = C->getRHS()) {
163 RHS->printJson(Out, nullptr, PP, AddQuotes);
169 assert(isa<DefaultStmt>(Label));
170 Out << "\"default\"";
173 Out << "\"implicit default\"";
175 } else if (isa<IndirectGotoStmt>(T)) {
177 Out << "IndirectGotoStmt\"";
179 Out << "Condition\", \"value\": "
180 << (*E.getSrc()->succ_begin() == E.getDst() ? "true" : "false");
186 const Stmt *S = castAs<StmtPoint>().getStmt();
187 assert(S != nullptr && "Expecting non-null Stmt");
189 Out << "Statement\", \"stmt_kind\": \"" << S->getStmtClassName()
190 << "\", \"stmt_id\": " << S->getID(Context)
191 << ", \"pointer\": \"" << (const void *)S << "\", \"pretty\": ";
193 S->printJson(Out, nullptr, PP, AddQuotes);
195 Out << ", \"location\": ";
196 printSourceLocationAsJson(Out, S->getBeginLoc(), SM);
198 Out << ", \"stmt_point_kind\": \"";
199 if (getAs<PreLoad>())
201 else if (getAs<PreStore>())
203 else if (getAs<PostAllocatorCall>())
204 Out << "PostAllocatorCall";
205 else if (getAs<PostCondition>())
206 Out << "PostCondition";
207 else if (getAs<PostLoad>())
209 else if (getAs<PostLValue>())
211 else if (getAs<PostStore>())
213 else if (getAs<PostStmt>())
215 else if (getAs<PostStmtPurgeDeadSymbols>())
216 Out << "PostStmtPurgeDeadSymbols";
217 else if (getAs<PreStmtPurgeDeadSymbols>())
218 Out << "PreStmtPurgeDeadSymbols";
219 else if (getAs<PreStmt>())
222 Out << "\nKind: '" << getKind();
223 llvm_unreachable("' is unhandled StmtPoint kind!");
232 SimpleProgramPointTag::SimpleProgramPointTag(StringRef MsgProvider,
234 : Desc((MsgProvider + " : " + Msg).str()) {}
236 StringRef SimpleProgramPointTag::getTagDescription() const {