blob: 97e90965d007a45ac93c428c655b8e6c880c5191 [file] [log] [blame]
Ted Kremeneke8f73162011-08-12 23:04:46 +00001//==- ProgramPoint.cpp - Program Points for Path-Sensitive Analysis -*- C++ -*-/
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Ted Kremeneke8f73162011-08-12 23:04:46 +00006//
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"
Artem Dergacheved035ff2019-07-12 02:10:33 +000015#include "clang/Basic/JsonSupport.h"
Ted Kremeneke8f73162011-08-12 23:04:46 +000016
17using namespace clang;
18
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +000019ProgramPointTag::~ProgramPointTag() {}
Ted Kremeneke8f73162011-08-12 23:04:46 +000020
Anna Zaks8de8cfd2011-10-07 21:01:38 +000021ProgramPoint ProgramPoint::getProgramPoint(const Stmt *S, ProgramPoint::Kind K,
22 const LocationContext *LC,
23 const ProgramPointTag *tag){
24 switch (K) {
25 default:
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);
Anna Zaks8de8cfd2011-10-07 21:01:38 +000037 case ProgramPoint::PostLValueKind:
38 return PostLValue(S, LC, tag);
Anna Zaks7e53bd62012-04-20 21:59:08 +000039 case ProgramPoint::PostStmtPurgeDeadSymbolsKind:
40 return PostStmtPurgeDeadSymbols(S, LC, tag);
41 case ProgramPoint::PreStmtPurgeDeadSymbolsKind:
42 return PreStmtPurgeDeadSymbols(S, LC, tag);
Anna Zaks8de8cfd2011-10-07 21:01:38 +000043 }
44}
45
Eric Fiselier407584c2018-09-30 18:05:39 +000046LLVM_DUMP_METHOD void ProgramPoint::dump() const {
Csaba Dabis13e491c2019-05-29 18:05:53 +000047 return printJson(llvm::errs());
Eric Fiselier407584c2018-09-30 18:05:39 +000048}
49
Csaba Dabis13e491c2019-05-29 18:05:53 +000050void ProgramPoint::printJson(llvm::raw_ostream &Out, const char *NL) const {
George Karpenkov27ec2102018-09-27 01:46:18 +000051 const ASTContext &Context =
52 getLocationContext()->getAnalysisDeclContext()->getASTContext();
53 const SourceManager &SM = Context.getSourceManager();
Csaba Dabis9ee26c82019-05-29 18:17:18 +000054 const PrintingPolicy &PP = Context.getPrintingPolicy();
55 const bool AddQuotes = true;
Csaba Dabis13e491c2019-05-29 18:05:53 +000056
57 Out << "\"kind\": \"";
George Karpenkov27ec2102018-09-27 01:46:18 +000058 switch (getKind()) {
59 case ProgramPoint::BlockEntranceKind:
Csaba Dabis13e491c2019-05-29 18:05:53 +000060 Out << "BlockEntrance\""
61 << ", \"block_id\": "
George Karpenkov27ec2102018-09-27 01:46:18 +000062 << castAs<BlockEntrance>().getBlock()->getBlockID();
63 break;
64
65 case ProgramPoint::FunctionExitKind: {
66 auto FEP = getAs<FunctionExitPoint>();
Csaba Dabis13e491c2019-05-29 18:05:53 +000067 Out << "FunctionExit\""
68 << ", \"block_id\": " << FEP->getBlock()->getBlockID()
69 << ", \"stmt_id\": ";
70
George Karpenkov27ec2102018-09-27 01:46:18 +000071 if (const ReturnStmt *RS = FEP->getStmt()) {
Csaba Dabis9ee26c82019-05-29 18:17:18 +000072 Out << RS->getID(Context) << ", \"stmt\": ";
73 RS->printJson(Out, nullptr, PP, AddQuotes);
Csaba Dabis13e491c2019-05-29 18:05:53 +000074 } else {
75 Out << "null, \"stmt\": null";
George Karpenkov27ec2102018-09-27 01:46:18 +000076 }
77 break;
78 }
79 case ProgramPoint::BlockExitKind:
Csaba Dabis13e491c2019-05-29 18:05:53 +000080 llvm_unreachable("BlockExitKind");
George Karpenkov27ec2102018-09-27 01:46:18 +000081 break;
George Karpenkov27ec2102018-09-27 01:46:18 +000082 case ProgramPoint::CallEnterKind:
Csaba Dabis13e491c2019-05-29 18:05:53 +000083 Out << "CallEnter\"";
George Karpenkov27ec2102018-09-27 01:46:18 +000084 break;
George Karpenkov27ec2102018-09-27 01:46:18 +000085 case ProgramPoint::CallExitBeginKind:
Csaba Dabis13e491c2019-05-29 18:05:53 +000086 Out << "CallExitBegin\"";
George Karpenkov27ec2102018-09-27 01:46:18 +000087 break;
George Karpenkov27ec2102018-09-27 01:46:18 +000088 case ProgramPoint::CallExitEndKind:
Csaba Dabis13e491c2019-05-29 18:05:53 +000089 Out << "CallExitEnd\"";
George Karpenkov27ec2102018-09-27 01:46:18 +000090 break;
George Karpenkov27ec2102018-09-27 01:46:18 +000091 case ProgramPoint::EpsilonKind:
Csaba Dabis13e491c2019-05-29 18:05:53 +000092 Out << "EpsilonPoint\"";
George Karpenkov27ec2102018-09-27 01:46:18 +000093 break;
94
Csaba Dabis13e491c2019-05-29 18:05:53 +000095 case ProgramPoint::LoopExitKind:
96 Out << "LoopExit\", \"stmt\": \""
97 << castAs<LoopExit>().getLoopStmt()->getStmtClassName() << '\"';
George Karpenkov27ec2102018-09-27 01:46:18 +000098 break;
George Karpenkov27ec2102018-09-27 01:46:18 +000099
100 case ProgramPoint::PreImplicitCallKind: {
101 ImplicitCallPoint PC = castAs<ImplicitCallPoint>();
Csaba Dabis02be6502019-05-29 18:21:14 +0000102 Out << "PreCall\", \"decl\": \""
Artem Dergacheved035ff2019-07-12 02:10:33 +0000103 << PC.getDecl()->getAsFunction()->getQualifiedNameAsString()
104 << "\", \"location\": ";
105 printSourceLocationAsJson(Out, PC.getLocation(), SM);
George Karpenkov27ec2102018-09-27 01:46:18 +0000106 break;
107 }
108
109 case ProgramPoint::PostImplicitCallKind: {
110 ImplicitCallPoint PC = castAs<ImplicitCallPoint>();
Csaba Dabis02be6502019-05-29 18:21:14 +0000111 Out << "PostCall\", \"decl\": \""
Artem Dergacheved035ff2019-07-12 02:10:33 +0000112 << PC.getDecl()->getAsFunction()->getQualifiedNameAsString()
113 << "\", \"location\": ";
114 printSourceLocationAsJson(Out, PC.getLocation(), SM);
George Karpenkov27ec2102018-09-27 01:46:18 +0000115 break;
116 }
117
118 case ProgramPoint::PostInitializerKind: {
Csaba Dabis13e491c2019-05-29 18:05:53 +0000119 Out << "PostInitializer\", ";
George Karpenkov27ec2102018-09-27 01:46:18 +0000120 const CXXCtorInitializer *Init = castAs<PostInitializer>().getInitializer();
Csaba Dabis13e491c2019-05-29 18:05:53 +0000121 if (const FieldDecl *FD = Init->getAnyMember()) {
122 Out << "\"field_decl\": \"" << *FD << '\"';
123 } else {
124 Out << "\"type\": \"";
George Karpenkov27ec2102018-09-27 01:46:18 +0000125 QualType Ty = Init->getTypeSourceInfo()->getType();
126 Ty = Ty.getLocalUnqualifiedType();
127 Ty.print(Out, Context.getLangOpts());
Csaba Dabis13e491c2019-05-29 18:05:53 +0000128 Out << '\"';
George Karpenkov27ec2102018-09-27 01:46:18 +0000129 }
130 break;
131 }
132
133 case ProgramPoint::BlockEdgeKind: {
134 const BlockEdge &E = castAs<BlockEdge>();
Csaba Dabis13e491c2019-05-29 18:05:53 +0000135 const Stmt *T = E.getSrc()->getTerminatorStmt();
136 Out << "Edge\", \"src_id\": " << E.getSrc()->getBlockID()
Csaba Dabisdea605e2019-05-29 18:29:31 +0000137 << ", \"dst_id\": " << E.getDst()->getBlockID() << ", \"terminator\": ";
George Karpenkov27ec2102018-09-27 01:46:18 +0000138
Csaba Dabisdea605e2019-05-29 18:29:31 +0000139 if (!T) {
140 Out << "null, \"term_kind\": null";
141 break;
142 }
143
144 E.getSrc()->printTerminatorJson(Out, Context.getLangOpts(),
145 /*AddQuotes=*/true);
Artem Dergacheved035ff2019-07-12 02:10:33 +0000146 Out << ", \"location\": ";
147 printSourceLocationAsJson(Out, T->getBeginLoc(), SM);
George Karpenkov27ec2102018-09-27 01:46:18 +0000148
Csaba Dabis9ee26c82019-05-29 18:17:18 +0000149 Out << ", \"term_kind\": \"";
Csaba Dabis13e491c2019-05-29 18:05:53 +0000150 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\": ";
Csaba Dabis9ee26c82019-05-29 18:17:18 +0000155 if (const Stmt *LHS = C->getLHS()) {
156 LHS->printJson(Out, nullptr, PP, AddQuotes);
157 } else {
Csaba Dabis13e491c2019-05-29 18:05:53 +0000158 Out << "null";
Csaba Dabis9ee26c82019-05-29 18:17:18 +0000159 }
160
Csaba Dabis13e491c2019-05-29 18:05:53 +0000161 Out << ", \"rhs\": ";
Csaba Dabis9ee26c82019-05-29 18:17:18 +0000162 if (const Stmt *RHS = C->getRHS()) {
163 RHS->printJson(Out, nullptr, PP, AddQuotes);
164 } else {
Csaba Dabis13e491c2019-05-29 18:05:53 +0000165 Out << "null";
Csaba Dabis9ee26c82019-05-29 18:17:18 +0000166 }
Csaba Dabis13e491c2019-05-29 18:05:53 +0000167 Out << " }";
168 } else {
169 assert(isa<DefaultStmt>(Label));
170 Out << "\"default\"";
171 }
George Karpenkov27ec2102018-09-27 01:46:18 +0000172 } else {
Csaba Dabis13e491c2019-05-29 18:05:53 +0000173 Out << "\"implicit default\"";
George Karpenkov27ec2102018-09-27 01:46:18 +0000174 }
Csaba Dabis13e491c2019-05-29 18:05:53 +0000175 } else if (isa<IndirectGotoStmt>(T)) {
176 // FIXME: More info.
177 Out << "IndirectGotoStmt\"";
178 } else {
179 Out << "Condition\", \"value\": "
180 << (*E.getSrc()->succ_begin() == E.getDst() ? "true" : "false");
George Karpenkov27ec2102018-09-27 01:46:18 +0000181 }
George Karpenkov27ec2102018-09-27 01:46:18 +0000182 break;
183 }
184
185 default: {
186 const Stmt *S = castAs<StmtPoint>().getStmt();
187 assert(S != nullptr && "Expecting non-null Stmt");
188
Csaba Dabis13e491c2019-05-29 18:05:53 +0000189 Out << "Statement\", \"stmt_kind\": \"" << S->getStmtClassName()
190 << "\", \"stmt_id\": " << S->getID(Context)
191 << ", \"pointer\": \"" << (const void *)S << "\", \"pretty\": ";
192
Csaba Dabis9ee26c82019-05-29 18:17:18 +0000193 S->printJson(Out, nullptr, PP, AddQuotes);
Csaba Dabis13e491c2019-05-29 18:05:53 +0000194
Artem Dergacheved035ff2019-07-12 02:10:33 +0000195 Out << ", \"location\": ";
196 printSourceLocationAsJson(Out, S->getBeginLoc(), SM);
Csaba Dabis13e491c2019-05-29 18:05:53 +0000197
Csaba Dabisfa880e62019-06-12 18:24:02 +0000198 Out << ", \"stmt_point_kind\": \"";
199 if (getAs<PreLoad>())
200 Out << "PreLoad";
201 else if (getAs<PreStore>())
202 Out << "PreStore";
George Karpenkov27ec2102018-09-27 01:46:18 +0000203 else if (getAs<PostAllocatorCall>())
Csaba Dabisfa880e62019-06-12 18:24:02 +0000204 Out << "PostAllocatorCall";
205 else if (getAs<PostCondition>())
206 Out << "PostCondition";
207 else if (getAs<PostLoad>())
208 Out << "PostLoad";
209 else if (getAs<PostLValue>())
210 Out << "PostLValue";
211 else if (getAs<PostStore>())
212 Out << "PostStore";
213 else if (getAs<PostStmt>())
214 Out << "PostStmt";
215 else if (getAs<PostStmtPurgeDeadSymbols>())
216 Out << "PostStmtPurgeDeadSymbols";
217 else if (getAs<PreStmtPurgeDeadSymbols>())
218 Out << "PreStmtPurgeDeadSymbols";
219 else if (getAs<PreStmt>())
220 Out << "PreStmt";
221 else {
222 Out << "\nKind: '" << getKind();
223 llvm_unreachable("' is unhandled StmtPoint kind!");
224 }
George Karpenkov27ec2102018-09-27 01:46:18 +0000225
Csaba Dabisfa880e62019-06-12 18:24:02 +0000226 Out << '\"';
George Karpenkov27ec2102018-09-27 01:46:18 +0000227 break;
228 }
229 }
230}
231
Fangrui Song6907ce22018-07-30 19:24:48 +0000232SimpleProgramPointTag::SimpleProgramPointTag(StringRef MsgProvider,
Anton Yartsev6a619222014-02-17 18:25:34 +0000233 StringRef Msg)
234 : Desc((MsgProvider + " : " + Msg).str()) {}
Ted Kremeneke8f73162011-08-12 23:04:46 +0000235
236StringRef SimpleProgramPointTag::getTagDescription() const {
Anton Yartsev6a619222014-02-17 18:25:34 +0000237 return Desc;
Ted Kremeneke8f73162011-08-12 23:04:46 +0000238}