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