clang  14.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 =
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\": ";
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\": ";
201  printSourceLocationAsJson(Out, S->getBeginLoc(), SM);
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 }
clang::ProgramPoint::PostLValueKind
@ PostLValueKind
Definition: ProgramPoint.h:73
clang::SimpleProgramPointTag::getTagDescription
StringRef getTagDescription() const override
Definition: ProgramPoint.cpp:241
clang::AnalysisDeclContext::getASTContext
ASTContext & getASTContext() const
Definition: AnalysisDeclContext.h:104
clang::ProgramPoint::BlockExitKind
@ BlockExitKind
Definition: ProgramPoint.h:63
clang::LocationContext
It wraps the AnalysisDeclContext to represent both the call stack with the help of StackFrameContext ...
Definition: AnalysisDeclContext.h:215
clang::Decl::getAsFunction
FunctionDecl * getAsFunction() LLVM_READONLY
Returns the function itself, or the templated function if this is a function template.
Definition: DeclBase.cpp:218
clang::ProgramPointTag
ProgramPoints can be "tagged" as representing points specific to a given analysis entity.
Definition: ProgramPoint.h:39
clang::CFGBlock::succ_begin
succ_iterator succ_begin()
Definition: CFG.h:955
clang::CFGBlock::getBlockID
unsigned getBlockID() const
Definition: CFG.h:1074
clang::QualType
A (possibly-)qualified type.
Definition: Type.h:673
clang::FieldDecl
Represents a member of a struct/union/class.
Definition: Decl.h:2835
clang::ProgramPoint::PostInitializerKind
@ PostInitializerKind
Definition: ProgramPoint.h:77
clang::ProgramPoint::CallEnterKind
@ CallEnterKind
Definition: ProgramPoint.h:78
clang::PrintingPolicy
Describes how types, statements, expressions, and declarations should be printed.
Definition: PrettyPrinter.h:59
clang::CFGBlock::printTerminatorJson
void printTerminatorJson(raw_ostream &Out, const LangOptions &LO, bool AddQuotes) const
printTerminatorJson - Pretty-prints the terminator in JSON format.
Definition: CFG.cpp:5889
clang::ASTContext::getSourceManager
SourceManager & getSourceManager()
Definition: ASTContext.h:695
clang::ProgramPoint::getLocationContext
const LocationContext * getLocationContext() const
Definition: ProgramPoint.h:179
clang::SourceManager
This class handles loading and caching of source files into memory.
Definition: SourceManager.h:626
clang::BlockEdge::getSrc
const CFGBlock * getSrc() const
Definition: ProgramPoint.h:511
JsonSupport.h
ProgramPoint.h
clang::ImplicitCallPoint::getLocation
SourceLocation getLocation() const
Definition: ProgramPoint.h:566
clang::ProgramPoint::PostLoadKind
@ PostLoadKind
Definition: ProgramPoint.h:69
clang::ProgramPoint::BlockEntranceKind
@ BlockEntranceKind
Definition: ProgramPoint.h:62
clang::ProgramPoint::LoopExitKind
@ LoopExitKind
Definition: ProgramPoint.h:86
clang::LocationContext::getAnalysisDeclContext
AnalysisDeclContext * getAnalysisDeclContext() const
Definition: AnalysisDeclContext.h:241
clang::ProgramPoint::printJson
void printJson(llvm::raw_ostream &Out, const char *NL="\n") const
Definition: ProgramPoint.cpp:51
clang::ASTContext
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:212
clang::PostStmtPurgeDeadSymbols
Represents a point after we ran remove dead bindings AFTER processing the given statement.
Definition: ProgramPoint.h:489
ASTContext.h
clang::BlockEdge::getDst
const CFGBlock * getDst() const
Definition: ProgramPoint.h:515
clang::ProgramPoint::PreStmtPurgeDeadSymbolsKind
@ PreStmtPurgeDeadSymbolsKind
Definition: ProgramPoint.h:65
clang::ProgramPointTag::~ProgramPointTag
virtual ~ProgramPointTag()
Definition: ProgramPoint.cpp:20
clang::SimpleProgramPointTag::SimpleProgramPointTag
SimpleProgramPointTag(StringRef MsgProvider, StringRef Msg)
Definition: ProgramPoint.cpp:237
Label
std::string Label
Definition: UsingDeclarationsSorter.cpp:69
clang::PostLValue
Definition: ProgramPoint.h:457
clang::ProgramPoint::PreStoreKind
@ PreStoreKind
Definition: ProgramPoint.h:70
clang::printSourceLocationAsJson
void printSourceLocationAsJson(raw_ostream &Out, SourceLocation Loc, const SourceManager &SM, bool AddBraces=true)
Definition: JsonSupport.h:81
clang::PostStmt
Definition: ProgramPoint.h:311
clang::PreStmt
Definition: ProgramPoint.h:295
clang::ProgramPoint::getKind
Kind getKind() const
Definition: ProgramPoint.h:160
clang::ImplicitCallPoint
Represents an implicit call event.
Definition: ProgramPoint.h:559
clang::ProgramPoint::CallExitEndKind
@ CallExitEndKind
Definition: ProgramPoint.h:80
clang::ProgramPoint::BlockEdgeKind
@ BlockEdgeKind
Definition: ProgramPoint.h:61
clang::ProgramPoint::CallExitBeginKind
@ CallExitBeginKind
Definition: ProgramPoint.h:79
clang::QualType::getLocalUnqualifiedType
QualType getLocalUnqualifiedType() const
Return this type with all of the instance-specific qualifiers removed, but without removing any quali...
Definition: Type.h:912
clang::ProgramPoint::PreImplicitCallKind
@ PreImplicitCallKind
Definition: ProgramPoint.h:82
clang::ProgramPoint::PostImplicitCallKind
@ PostImplicitCallKind
Definition: ProgramPoint.h:83
clang::ProgramPoint::Kind
Kind
Definition: ProgramPoint.h:61
clang::ASTContext::getPrintingPolicy
const clang::PrintingPolicy & getPrintingPolicy() const
Definition: ASTContext.h:687
clang::CFGBlock::getLabel
Stmt * getLabel()
Definition: CFG.h:1069
clang::ProgramPoint::PreStmtKind
@ PreStmtKind
Definition: ProgramPoint.h:64
clang::NamedDecl::getQualifiedNameAsString
std::string getQualifiedNameAsString() const
Definition: Decl.cpp:1582
clang::ProgramPoint::PostStmtKind
@ PostStmtKind
Definition: ProgramPoint.h:67
clang::PostLoad
Definition: ProgramPoint.h:416
clang::ProgramPoint::PreLoadKind
@ PreLoadKind
Definition: ProgramPoint.h:68
clang
Definition: CalledOnceCheck.h:17
clang::ProgramPoint::EpsilonKind
@ EpsilonKind
Definition: ProgramPoint.h:87
clang::Stmt
Stmt - This represents one statement.
Definition: Stmt.h:69
clang::ProgramPoint::getProgramPoint
static ProgramPoint getProgramPoint(const Stmt *S, ProgramPoint::Kind K, const LocationContext *LC, const ProgramPointTag *tag)
Definition: ProgramPoint.cpp:22
clang::ImplicitCallPoint::getDecl
const Decl * getDecl() const
Definition: ProgramPoint.h:565
clang::CFGBlock::getTerminatorStmt
Stmt * getTerminatorStmt()
Definition: CFG.h:1050
clang::Stmt::getBeginLoc
SourceLocation getBeginLoc() const LLVM_READONLY
Definition: Stmt.cpp:336
clang::ProgramPoint::dump
LLVM_DUMP_METHOD void dump() const
Definition: ProgramPoint.cpp:47
clang::BlockEdge
Definition: ProgramPoint.h:503
clang::QualType::print
void print(raw_ostream &OS, const PrintingPolicy &Policy, const Twine &PlaceHolder=Twine(), unsigned Indentation=0) const
Definition: TypePrinter.cpp:2252
clang::PreLoad
Definition: ProgramPoint.h:388
clang::PreStore
Definition: ProgramPoint.h:402
clang::ProgramPoint::FunctionExitKind
@ FunctionExitKind
Definition: ProgramPoint.h:81
SM
#define SM(sm)
Definition: Cuda.cpp:78
clang::CXXCtorInitializer
Represents a C++ base or member initializer.
Definition: DeclCXX.h:2172
clang::ProgramPoint
Definition: ProgramPoint.h:59
clang::ProgramPoint::PostStmtPurgeDeadSymbolsKind
@ PostStmtPurgeDeadSymbolsKind
Definition: ProgramPoint.h:66
clang::ASTContext::getLangOpts
const LangOptions & getLangOpts() const
Definition: ASTContext.h:765
clang::ReturnStmt
ReturnStmt - This represents a return, optionally of an expression: return; return 4;.
Definition: Stmt.h:2760
clang::PreStmtPurgeDeadSymbols
Represents a point after we ran remove dead bindings BEFORE processing the given statement.
Definition: ProgramPoint.h:473