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