clang  11.0.0git
ProgramPoint.cpp
Go to the documentation of this file.
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 
15 #include "clang/AST/ASTContext.h"
17 
18 using namespace clang;
19 
21 
23  const LocationContext *LC,
24  const ProgramPointTag *tag){
25  switch (K) {
26  default:
27  llvm_unreachable("Unhandled ProgramPoint kind");
29  return PreStmt(S, LC, tag);
31  return PostStmt(S, LC, tag);
33  return PreLoad(S, LC, tag);
35  return PostLoad(S, LC, tag);
37  return PreStore(S, LC, tag);
39  return PostLValue(S, LC, tag);
41  return PostStmtPurgeDeadSymbols(S, LC, tag);
43  return PreStmtPurgeDeadSymbols(S, LC, tag);
44  }
45 }
46 
47 LLVM_DUMP_METHOD void ProgramPoint::dump() const {
48  return printJson(llvm::errs());
49 }
50 
51 void ProgramPoint::printJson(llvm::raw_ostream &Out, const char *NL) const {
52  const ASTContext &Context =
53  getLocationContext()->getAnalysisDeclContext()->getASTContext();
54  const SourceManager &SM = Context.getSourceManager();
55  const PrintingPolicy &PP = Context.getPrintingPolicy();
56  const bool AddQuotes = true;
57 
58  Out << "\"kind\": \"";
59  switch (getKind()) {
61  Out << "BlockEntrance\""
62  << ", \"block_id\": "
63  << castAs<BlockEntrance>().getBlock()->getBlockID();
64  break;
65 
67  auto FEP = getAs<FunctionExitPoint>();
68  Out << "FunctionExit\""
69  << ", \"block_id\": " << FEP->getBlock()->getBlockID()
70  << ", \"stmt_id\": ";
71 
72  if (const ReturnStmt *RS = FEP->getStmt()) {
73  Out << RS->getID(Context) << ", \"stmt\": ";
74  RS->printJson(Out, nullptr, PP, AddQuotes);
75  } else {
76  Out << "null, \"stmt\": null";
77  }
78  break;
79  }
81  llvm_unreachable("BlockExitKind");
82  break;
84  Out << "CallEnter\"";
85  break;
87  Out << "CallExitBegin\"";
88  break;
90  Out << "CallExitEnd\"";
91  break;
93  Out << "EpsilonPoint\"";
94  break;
95 
97  Out << "LoopExit\", \"stmt\": \""
98  << castAs<LoopExit>().getLoopStmt()->getStmtClassName() << '\"';
99  break;
100 
102  ImplicitCallPoint PC = castAs<ImplicitCallPoint>();
103  Out << "PreCall\", \"decl\": \""
105  << "\", \"location\": ";
107  break;
108  }
109 
111  ImplicitCallPoint PC = castAs<ImplicitCallPoint>();
112  Out << "PostCall\", \"decl\": \""
114  << "\", \"location\": ";
116  break;
117  }
118 
120  Out << "PostInitializer\", ";
121  const CXXCtorInitializer *Init = castAs<PostInitializer>().getInitializer();
122  if (const FieldDecl *FD = Init->getAnyMember()) {
123  Out << "\"field_decl\": \"" << *FD << '\"';
124  } else {
125  Out << "\"type\": \"";
126  QualType Ty = Init->getTypeSourceInfo()->getType();
127  Ty = Ty.getLocalUnqualifiedType();
128  Ty.print(Out, Context.getLangOpts());
129  Out << '\"';
130  }
131  break;
132  }
133 
135  const BlockEdge &E = castAs<BlockEdge>();
136  const Stmt *T = E.getSrc()->getTerminatorStmt();
137  Out << "Edge\", \"src_id\": " << E.getSrc()->getBlockID()
138  << ", \"dst_id\": " << E.getDst()->getBlockID() << ", \"terminator\": ";
139 
140  if (!T) {
141  Out << "null, \"term_kind\": null";
142  break;
143  }
144 
145  E.getSrc()->printTerminatorJson(Out, Context.getLangOpts(),
146  /*AddQuotes=*/true);
147  Out << ", \"location\": ";
148  printSourceLocationAsJson(Out, T->getBeginLoc(), SM);
149 
150  Out << ", \"term_kind\": \"";
151  if (isa<SwitchStmt>(T)) {
152  Out << "SwitchStmt\", \"case\": ";
153  if (const Stmt *Label = E.getDst()->getLabel()) {
154  if (const auto *C = dyn_cast<CaseStmt>(Label)) {
155  Out << "{ \"lhs\": ";
156  if (const Stmt *LHS = C->getLHS()) {
157  LHS->printJson(Out, nullptr, PP, AddQuotes);
158  } else {
159  Out << "null";
160  }
161 
162  Out << ", \"rhs\": ";
163  if (const Stmt *RHS = C->getRHS()) {
164  RHS->printJson(Out, nullptr, PP, AddQuotes);
165  } else {
166  Out << "null";
167  }
168  Out << " }";
169  } else {
170  assert(isa<DefaultStmt>(Label));
171  Out << "\"default\"";
172  }
173  } else {
174  Out << "\"implicit default\"";
175  }
176  } else if (isa<IndirectGotoStmt>(T)) {
177  // FIXME: More info.
178  Out << "IndirectGotoStmt\"";
179  } else {
180  Out << "Condition\", \"value\": "
181  << (*E.getSrc()->succ_begin() == E.getDst() ? "true" : "false");
182  }
183  break;
184  }
185 
186  default: {
187  const Stmt *S = castAs<StmtPoint>().getStmt();
188  assert(S != nullptr && "Expecting non-null Stmt");
189 
190  Out << "Statement\", \"stmt_kind\": \"" << S->getStmtClassName()
191  << "\", \"stmt_id\": " << S->getID(Context)
192  << ", \"pointer\": \"" << (const void *)S << "\", ";
193  if (const auto *CS = dyn_cast<CastExpr>(S))
194  Out << "\"cast_kind\": \"" << CS->getCastKindName() << "\", ";
195 
196  Out << "\"pretty\": ";
197 
198  S->printJson(Out, nullptr, PP, AddQuotes);
199 
200  Out << ", \"location\": ";
202 
203  Out << ", \"stmt_point_kind\": \"";
204  if (getAs<PreLoad>())
205  Out << "PreLoad";
206  else if (getAs<PreStore>())
207  Out << "PreStore";
208  else if (getAs<PostAllocatorCall>())
209  Out << "PostAllocatorCall";
210  else if (getAs<PostCondition>())
211  Out << "PostCondition";
212  else if (getAs<PostLoad>())
213  Out << "PostLoad";
214  else if (getAs<PostLValue>())
215  Out << "PostLValue";
216  else if (getAs<PostStore>())
217  Out << "PostStore";
218  else if (getAs<PostStmt>())
219  Out << "PostStmt";
220  else if (getAs<PostStmtPurgeDeadSymbols>())
221  Out << "PostStmtPurgeDeadSymbols";
222  else if (getAs<PreStmtPurgeDeadSymbols>())
223  Out << "PreStmtPurgeDeadSymbols";
224  else if (getAs<PreStmt>())
225  Out << "PreStmt";
226  else {
227  Out << "\nKind: '" << getKind();
228  llvm_unreachable("' is unhandled StmtPoint kind!");
229  }
230 
231  Out << '\"';
232  break;
233  }
234  }
235 }
236 
238  StringRef Msg)
239  : Desc((MsgProvider + " : " + Msg).str()) {}
240 
242  return Desc;
243 }
Defines the clang::ASTContext interface.
A (possibly-)qualified type.
Definition: Type.h:655
succ_iterator succ_begin()
Definition: CFG.h:955
Stmt - This represents one statement.
Definition: Stmt.h:68
unsigned getBlockID() const
Definition: CFG.h:1074
const CFGBlock * getSrc() const
Definition: ProgramPoint.h:511
Represents a point after we ran remove dead bindings BEFORE processing the given statement.
Definition: ProgramPoint.h:473
It wraps the AnalysisDeclContext to represent both the call stack with the help of StackFrameContext ...
Represents an implicit call event.
Definition: ProgramPoint.h:559
Describes how types, statements, expressions, and declarations should be printed. ...
Definition: PrettyPrinter.h:49
QualType getLocalUnqualifiedType() const
Return this type with all of the instance-specific qualifiers removed, but without removing any quali...
Definition: Type.h:894
void printTerminatorJson(raw_ostream &Out, const LangOptions &LO, bool AddQuotes) const
printTerminatorJson - Pretty-prints the terminator in JSON format.
Definition: CFG.cpp:5851
const char * getStmtClassName() const
Definition: Stmt.cpp:76
static ProgramPoint getProgramPoint(const Stmt *S, ProgramPoint::Kind K, const LocationContext *LC, const ProgramPointTag *tag)
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:174
void printJson(raw_ostream &Out, PrinterHelper *Helper, const PrintingPolicy &Policy, bool AddQuotes) const
Pretty-prints in JSON format.
SourceLocation getBeginLoc() const LLVM_READONLY
Definition: Stmt.cpp:275
Represents a member of a struct/union/class.
Definition: Decl.h:2740
TypeSourceInfo * getTypeSourceInfo() const
Returns the declarator information for a base class or delegating initializer.
Definition: DeclCXX.h:2315
LLVM_DUMP_METHOD void dump() const
SimpleProgramPointTag(StringRef MsgProvider, StringRef Msg)
SourceLocation getLocation() const
Definition: ProgramPoint.h:566
void print(raw_ostream &OS, const PrintingPolicy &Policy, const Twine &PlaceHolder=Twine(), unsigned Indentation=0) const
StringRef getTagDescription() const override
const clang::PrintingPolicy & getPrintingPolicy() const
Definition: ASTContext.h:611
void printJson(llvm::raw_ostream &Out, const char *NL="\) const
Represents a point after we ran remove dead bindings AFTER processing the given statement.
Definition: ProgramPoint.h:489
FieldDecl * getAnyMember() const
Definition: DeclCXX.h:2327
std::string Label
const CFGBlock * getDst() const
Definition: ProgramPoint.h:515
ReturnStmt - This represents a return, optionally of an expression: return; return 4;...
Definition: Stmt.h:2668
#define SM(sm)
Definition: Cuda.cpp:62
FunctionDecl * getAsFunction() LLVM_READONLY
Returns the function itself, or the templated function if this is a function template.
Definition: DeclBase.cpp:218
Stmt * getLabel()
Definition: CFG.h:1069
ProgramPoints can be "tagged" as representing points specific to a given analysis entity...
Definition: ProgramPoint.h:39
int64_t getID(const ASTContext &Context) const
Definition: Stmt.cpp:299
void printSourceLocationAsJson(raw_ostream &Out, SourceLocation Loc, const SourceManager &SM, bool AddBraces=true)
Definition: JsonSupport.h:81
const Decl * getDecl() const
Definition: ProgramPoint.h:565
Dataflow Directional Tag Classes.
Stmt * getTerminatorStmt()
Definition: CFG.h:1050
Represents a C++ base or member initializer.
Definition: DeclCXX.h:2185
SourceManager & getSourceManager()
Definition: ASTContext.h:619
std::string getQualifiedNameAsString() const
Definition: Decl.cpp:1530
static Decl::Kind getKind(const Decl *D)
Definition: DeclBase.cpp:994
const LangOptions & getLangOpts() const
Definition: ASTContext.h:664
This class handles loading and caching of source files into memory.
QualType getType() const
Return the type wrapped by this type source info.
Definition: Type.h:6384