blob: 0398251b5eab64067d95f164f0bd58b8fd56222a [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"
15
16using namespace clang;
17
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +000018ProgramPointTag::~ProgramPointTag() {}
Ted Kremeneke8f73162011-08-12 23:04:46 +000019
Anna Zaks8de8cfd2011-10-07 21:01:38 +000020ProgramPoint 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);
Anna Zaks8de8cfd2011-10-07 21:01:38 +000036 case ProgramPoint::PostLValueKind:
37 return PostLValue(S, LC, tag);
Anna Zaks7e53bd62012-04-20 21:59:08 +000038 case ProgramPoint::PostStmtPurgeDeadSymbolsKind:
39 return PostStmtPurgeDeadSymbols(S, LC, tag);
40 case ProgramPoint::PreStmtPurgeDeadSymbolsKind:
41 return PreStmtPurgeDeadSymbols(S, LC, tag);
Anna Zaks8de8cfd2011-10-07 21:01:38 +000042 }
43}
44
Eric Fiselier407584c2018-09-30 18:05:39 +000045LLVM_DUMP_METHOD void ProgramPoint::dump() const {
Csaba Dabis13e491c2019-05-29 18:05:53 +000046 return printJson(llvm::errs());
Eric Fiselier407584c2018-09-30 18:05:39 +000047}
48
Csaba Dabis9ee26c82019-05-29 18:17:18 +000049static void printLocJson(raw_ostream &Out, SourceLocation Loc,
50 const SourceManager &SM) {
Csaba Dabis13e491c2019-05-29 18:05:53 +000051 Out << "\"location\": ";
52 if (!Loc.isFileID()) {
53 Out << "null";
54 return;
George Karpenkov27ec2102018-09-27 01:46:18 +000055 }
Csaba Dabis13e491c2019-05-29 18:05:53 +000056
57 Out << "{ \"line\": " << SM.getExpansionLineNumber(Loc)
Csaba Dabis3a4a60e2019-06-24 16:19:39 +000058 << ", \"column\": " << SM.getExpansionColumnNumber(Loc)
59 << ", \"file\": \"" << SM.getFilename(Loc) << "\" }";
George Karpenkov27ec2102018-09-27 01:46:18 +000060}
61
Csaba Dabis13e491c2019-05-29 18:05:53 +000062void ProgramPoint::printJson(llvm::raw_ostream &Out, const char *NL) const {
George Karpenkov27ec2102018-09-27 01:46:18 +000063 const ASTContext &Context =
64 getLocationContext()->getAnalysisDeclContext()->getASTContext();
65 const SourceManager &SM = Context.getSourceManager();
Csaba Dabis9ee26c82019-05-29 18:17:18 +000066 const PrintingPolicy &PP = Context.getPrintingPolicy();
67 const bool AddQuotes = true;
Csaba Dabis13e491c2019-05-29 18:05:53 +000068
69 Out << "\"kind\": \"";
George Karpenkov27ec2102018-09-27 01:46:18 +000070 switch (getKind()) {
71 case ProgramPoint::BlockEntranceKind:
Csaba Dabis13e491c2019-05-29 18:05:53 +000072 Out << "BlockEntrance\""
73 << ", \"block_id\": "
George Karpenkov27ec2102018-09-27 01:46:18 +000074 << castAs<BlockEntrance>().getBlock()->getBlockID();
75 break;
76
77 case ProgramPoint::FunctionExitKind: {
78 auto FEP = getAs<FunctionExitPoint>();
Csaba Dabis13e491c2019-05-29 18:05:53 +000079 Out << "FunctionExit\""
80 << ", \"block_id\": " << FEP->getBlock()->getBlockID()
81 << ", \"stmt_id\": ";
82
George Karpenkov27ec2102018-09-27 01:46:18 +000083 if (const ReturnStmt *RS = FEP->getStmt()) {
Csaba Dabis9ee26c82019-05-29 18:17:18 +000084 Out << RS->getID(Context) << ", \"stmt\": ";
85 RS->printJson(Out, nullptr, PP, AddQuotes);
Csaba Dabis13e491c2019-05-29 18:05:53 +000086 } else {
87 Out << "null, \"stmt\": null";
George Karpenkov27ec2102018-09-27 01:46:18 +000088 }
89 break;
90 }
91 case ProgramPoint::BlockExitKind:
Csaba Dabis13e491c2019-05-29 18:05:53 +000092 llvm_unreachable("BlockExitKind");
George Karpenkov27ec2102018-09-27 01:46:18 +000093 break;
George Karpenkov27ec2102018-09-27 01:46:18 +000094 case ProgramPoint::CallEnterKind:
Csaba Dabis13e491c2019-05-29 18:05:53 +000095 Out << "CallEnter\"";
George Karpenkov27ec2102018-09-27 01:46:18 +000096 break;
George Karpenkov27ec2102018-09-27 01:46:18 +000097 case ProgramPoint::CallExitBeginKind:
Csaba Dabis13e491c2019-05-29 18:05:53 +000098 Out << "CallExitBegin\"";
George Karpenkov27ec2102018-09-27 01:46:18 +000099 break;
George Karpenkov27ec2102018-09-27 01:46:18 +0000100 case ProgramPoint::CallExitEndKind:
Csaba Dabis13e491c2019-05-29 18:05:53 +0000101 Out << "CallExitEnd\"";
George Karpenkov27ec2102018-09-27 01:46:18 +0000102 break;
George Karpenkov27ec2102018-09-27 01:46:18 +0000103 case ProgramPoint::EpsilonKind:
Csaba Dabis13e491c2019-05-29 18:05:53 +0000104 Out << "EpsilonPoint\"";
George Karpenkov27ec2102018-09-27 01:46:18 +0000105 break;
106
Csaba Dabis13e491c2019-05-29 18:05:53 +0000107 case ProgramPoint::LoopExitKind:
108 Out << "LoopExit\", \"stmt\": \""
109 << castAs<LoopExit>().getLoopStmt()->getStmtClassName() << '\"';
George Karpenkov27ec2102018-09-27 01:46:18 +0000110 break;
George Karpenkov27ec2102018-09-27 01:46:18 +0000111
112 case ProgramPoint::PreImplicitCallKind: {
113 ImplicitCallPoint PC = castAs<ImplicitCallPoint>();
Csaba Dabis02be6502019-05-29 18:21:14 +0000114 Out << "PreCall\", \"decl\": \""
115 << PC.getDecl()->getAsFunction()->getQualifiedNameAsString() << "\", ";
Csaba Dabis9ee26c82019-05-29 18:17:18 +0000116 printLocJson(Out, PC.getLocation(), SM);
George Karpenkov27ec2102018-09-27 01:46:18 +0000117 break;
118 }
119
120 case ProgramPoint::PostImplicitCallKind: {
121 ImplicitCallPoint PC = castAs<ImplicitCallPoint>();
Csaba Dabis02be6502019-05-29 18:21:14 +0000122 Out << "PostCall\", \"decl\": \""
123 << PC.getDecl()->getAsFunction()->getQualifiedNameAsString() << "\", ";
Csaba Dabis9ee26c82019-05-29 18:17:18 +0000124 printLocJson(Out, PC.getLocation(), SM);
George Karpenkov27ec2102018-09-27 01:46:18 +0000125 break;
126 }
127
128 case ProgramPoint::PostInitializerKind: {
Csaba Dabis13e491c2019-05-29 18:05:53 +0000129 Out << "PostInitializer\", ";
George Karpenkov27ec2102018-09-27 01:46:18 +0000130 const CXXCtorInitializer *Init = castAs<PostInitializer>().getInitializer();
Csaba Dabis13e491c2019-05-29 18:05:53 +0000131 if (const FieldDecl *FD = Init->getAnyMember()) {
132 Out << "\"field_decl\": \"" << *FD << '\"';
133 } else {
134 Out << "\"type\": \"";
George Karpenkov27ec2102018-09-27 01:46:18 +0000135 QualType Ty = Init->getTypeSourceInfo()->getType();
136 Ty = Ty.getLocalUnqualifiedType();
137 Ty.print(Out, Context.getLangOpts());
Csaba Dabis13e491c2019-05-29 18:05:53 +0000138 Out << '\"';
George Karpenkov27ec2102018-09-27 01:46:18 +0000139 }
140 break;
141 }
142
143 case ProgramPoint::BlockEdgeKind: {
144 const BlockEdge &E = castAs<BlockEdge>();
Csaba Dabis13e491c2019-05-29 18:05:53 +0000145 const Stmt *T = E.getSrc()->getTerminatorStmt();
146 Out << "Edge\", \"src_id\": " << E.getSrc()->getBlockID()
Csaba Dabisdea605e2019-05-29 18:29:31 +0000147 << ", \"dst_id\": " << E.getDst()->getBlockID() << ", \"terminator\": ";
George Karpenkov27ec2102018-09-27 01:46:18 +0000148
Csaba Dabisdea605e2019-05-29 18:29:31 +0000149 if (!T) {
150 Out << "null, \"term_kind\": null";
151 break;
152 }
153
154 E.getSrc()->printTerminatorJson(Out, Context.getLangOpts(),
155 /*AddQuotes=*/true);
156 Out << ", ";
Csaba Dabis9ee26c82019-05-29 18:17:18 +0000157 printLocJson(Out, T->getBeginLoc(), SM);
George Karpenkov27ec2102018-09-27 01:46:18 +0000158
Csaba Dabis9ee26c82019-05-29 18:17:18 +0000159 Out << ", \"term_kind\": \"";
Csaba Dabis13e491c2019-05-29 18:05:53 +0000160 if (isa<SwitchStmt>(T)) {
161 Out << "SwitchStmt\", \"case\": ";
162 if (const Stmt *Label = E.getDst()->getLabel()) {
163 if (const auto *C = dyn_cast<CaseStmt>(Label)) {
164 Out << "{ \"lhs\": ";
Csaba Dabis9ee26c82019-05-29 18:17:18 +0000165 if (const Stmt *LHS = C->getLHS()) {
166 LHS->printJson(Out, nullptr, PP, AddQuotes);
167 } else {
Csaba Dabis13e491c2019-05-29 18:05:53 +0000168 Out << "null";
Csaba Dabis9ee26c82019-05-29 18:17:18 +0000169 }
170
Csaba Dabis13e491c2019-05-29 18:05:53 +0000171 Out << ", \"rhs\": ";
Csaba Dabis9ee26c82019-05-29 18:17:18 +0000172 if (const Stmt *RHS = C->getRHS()) {
173 RHS->printJson(Out, nullptr, PP, AddQuotes);
174 } else {
Csaba Dabis13e491c2019-05-29 18:05:53 +0000175 Out << "null";
Csaba Dabis9ee26c82019-05-29 18:17:18 +0000176 }
Csaba Dabis13e491c2019-05-29 18:05:53 +0000177 Out << " }";
178 } else {
179 assert(isa<DefaultStmt>(Label));
180 Out << "\"default\"";
181 }
George Karpenkov27ec2102018-09-27 01:46:18 +0000182 } else {
Csaba Dabis13e491c2019-05-29 18:05:53 +0000183 Out << "\"implicit default\"";
George Karpenkov27ec2102018-09-27 01:46:18 +0000184 }
Csaba Dabis13e491c2019-05-29 18:05:53 +0000185 } else if (isa<IndirectGotoStmt>(T)) {
186 // FIXME: More info.
187 Out << "IndirectGotoStmt\"";
188 } else {
189 Out << "Condition\", \"value\": "
190 << (*E.getSrc()->succ_begin() == E.getDst() ? "true" : "false");
George Karpenkov27ec2102018-09-27 01:46:18 +0000191 }
George Karpenkov27ec2102018-09-27 01:46:18 +0000192 break;
193 }
194
195 default: {
196 const Stmt *S = castAs<StmtPoint>().getStmt();
197 assert(S != nullptr && "Expecting non-null Stmt");
198
Csaba Dabis13e491c2019-05-29 18:05:53 +0000199 Out << "Statement\", \"stmt_kind\": \"" << S->getStmtClassName()
200 << "\", \"stmt_id\": " << S->getID(Context)
201 << ", \"pointer\": \"" << (const void *)S << "\", \"pretty\": ";
202
Csaba Dabis9ee26c82019-05-29 18:17:18 +0000203 S->printJson(Out, nullptr, PP, AddQuotes);
Csaba Dabis13e491c2019-05-29 18:05:53 +0000204
Csaba Dabis9ee26c82019-05-29 18:17:18 +0000205 Out << ", ";
206 printLocJson(Out, S->getBeginLoc(), SM);
Csaba Dabis13e491c2019-05-29 18:05:53 +0000207
Csaba Dabisfa880e62019-06-12 18:24:02 +0000208 Out << ", \"stmt_point_kind\": \"";
209 if (getAs<PreLoad>())
210 Out << "PreLoad";
211 else if (getAs<PreStore>())
212 Out << "PreStore";
George Karpenkov27ec2102018-09-27 01:46:18 +0000213 else if (getAs<PostAllocatorCall>())
Csaba Dabisfa880e62019-06-12 18:24:02 +0000214 Out << "PostAllocatorCall";
215 else if (getAs<PostCondition>())
216 Out << "PostCondition";
217 else if (getAs<PostLoad>())
218 Out << "PostLoad";
219 else if (getAs<PostLValue>())
220 Out << "PostLValue";
221 else if (getAs<PostStore>())
222 Out << "PostStore";
223 else if (getAs<PostStmt>())
224 Out << "PostStmt";
225 else if (getAs<PostStmtPurgeDeadSymbols>())
226 Out << "PostStmtPurgeDeadSymbols";
227 else if (getAs<PreStmtPurgeDeadSymbols>())
228 Out << "PreStmtPurgeDeadSymbols";
229 else if (getAs<PreStmt>())
230 Out << "PreStmt";
231 else {
232 Out << "\nKind: '" << getKind();
233 llvm_unreachable("' is unhandled StmtPoint kind!");
234 }
George Karpenkov27ec2102018-09-27 01:46:18 +0000235
Csaba Dabisfa880e62019-06-12 18:24:02 +0000236 Out << '\"';
George Karpenkov27ec2102018-09-27 01:46:18 +0000237 break;
238 }
239 }
240}
241
Fangrui Song6907ce22018-07-30 19:24:48 +0000242SimpleProgramPointTag::SimpleProgramPointTag(StringRef MsgProvider,
Anton Yartsev6a619222014-02-17 18:25:34 +0000243 StringRef Msg)
244 : Desc((MsgProvider + " : " + Msg).str()) {}
Ted Kremeneke8f73162011-08-12 23:04:46 +0000245
246StringRef SimpleProgramPointTag::getTagDescription() const {
Anton Yartsev6a619222014-02-17 18:25:34 +0000247 return Desc;
Ted Kremeneke8f73162011-08-12 23:04:46 +0000248}