clang 8.0.0

CFGStmtMap.cpp
Go to the documentation of this file.
1 //===--- CFGStmtMap.h - Map from Stmt* to CFGBlock* -----------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the CFGStmtMap class, which defines a mapping from
11 // Stmt* to CFGBlock*
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/ADT/DenseMap.h"
16 #include "clang/AST/ParentMap.h"
17 #include "clang/Analysis/CFG.h"
18 #include "clang/Analysis/CFGStmtMap.h"
19
20 using namespace clang;
21
22 typedef llvm::DenseMap<const Stmt*, CFGBlock*> SMap;
23 static SMap *AsMap(void *m) { return (SMap*) m; }
24
25 CFGStmtMap::~CFGStmtMap() { delete AsMap(M); }
26
27 CFGBlock *CFGStmtMap::getBlock(Stmt *S) {
28 SMap *SM = AsMap(M);
29 Stmt *X = S;
30
31 // If 'S' isn't in the map, walk the ParentMap to see if one of its ancestors
32 // is in the map.
33 while (X) {
34 SMap::iterator I = SM->find(X);
35 if (I != SM->end()) {
36 CFGBlock *B = I->second;
37 // Memoize this lookup.
38 if (X != S)
39 (*SM)[X] = B;
40 return B;
41 }
42
43 X = PM->getParentIgnoreParens(X);
44 }
45
46 return nullptr;
47 }
48
49 static void Accumulate(SMap &SM, CFGBlock *B) {
50 // First walk the block-level expressions.
51 for (CFGBlock::iterator I = B->begin(), E = B->end(); I != E; ++I) {
52 const CFGElement &CE = *I;
53 Optional<CFGStmt> CS = CE.getAs<CFGStmt>();
54 if (!CS)
55 continue;
56
57 CFGBlock *&Entry = SM[CS->getStmt()];
58 // If 'Entry' is already initialized (e.g., a terminator was already),
59 // skip.
60 if (Entry)
61 continue;
62
63 Entry = B;
64
65 }
66
67 // Look at the label of the block.
68 if (Stmt *Label = B->getLabel())
69 SM[Label] = B;
70
71 // Finally, look at the terminator. If the terminator was already added
72 // because it is a block-level expression in another block, overwrite
73 // that mapping.
74 if (Stmt *Term = B->getTerminator())
75 SM[Term] = B;
76 }
77
78 CFGStmtMap *CFGStmtMap::Build(CFG *C, ParentMap *PM) {
79 if (!C || !PM)
80 return nullptr;
81
82 SMap *SM = new SMap();
83
84 // Walk all blocks, accumulating the block-level expressions, labels,
85 // and terminators.
86 for (CFG::iterator I = C->begin(), E = C->end(); I != E; ++I)
87 Accumulate(*SM, *I);
88
89 return new CFGStmtMap(PM, SM);
90 }
91
clang::ParentMap
Definition: ParentMap.h:21
clang::CFGBlock::iterator
ElementList::iterator iterator
Definition: CFG.h:695
clang::Stmt
Stmt - This represents one statement.
Definition: Stmt.h:66
Accumulate
static void Accumulate(SMap &SM, CFGBlock *B)
Definition: CFGStmtMap.cpp:49
clang::CFGBlock::begin
iterator begin()
Definition: CFG.h:703
CFGStmtMap.h
llvm::Optional
Definition: LLVM.h:38
clang::CFG::end
iterator end()
Definition: CFG.h:1079
clang::CFGStmt
Definition: CFG.h:127
clang::CFG::iterator
CFGBlockListTy::iterator iterator
Definition: CFG.h:1070
clang::CFGBlock
Represents a single basic block in a source-level CFG.
Definition: CFG.h:552
ParentMap.h
Label
std::string Label
Definition: UsingDeclarationsSorter.cpp:70
clang::CFG
Represents a source-level, intra-procedural CFG that represents the control-flow of a Stmt...
Definition: CFG.h:1003
clang::CFGStmtMap::getBlock
CFGBlock * getBlock(Stmt *S)
Returns the CFGBlock the specified Stmt* appears in.
Definition: CFGStmtMap.cpp:27
clang::CFGStmtMap::~CFGStmtMap
~CFGStmtMap()
Definition: CFGStmtMap.cpp:25
clang::CFGStmtMap
Definition: CFGStmtMap.h:25
SM
const SourceManager & SM
Definition: Format.cpp:1490
clang::CFGBlock::getTerminator
CFGTerminator getTerminator()
Definition: CFG.h:840
clang::CFGBlock::getLabel
Stmt * getLabel()
Definition: CFG.h:851
clang::ParentMap::getParentIgnoreParens
Stmt * getParentIgnoreParens(Stmt *) const
Definition: ParentMap.cpp:129
clang::CFG::begin
iterator begin()
Definition: CFG.h:1078
clang::CFGElement::getAs
Optional< T > getAs() const
Convert to the specified CFGElement type, returning None if this CFGElement is not of the desired typ...
Definition: CFG.h:110
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:22
clang::CFGStmtMap::Build
static CFGStmtMap * Build(CFG *C, ParentMap *PM)
Returns a new CFGMap for the given CFG.
Definition: CFGStmtMap.cpp:78
AsMap
static SMap * AsMap(void *m)
Definition: CFGStmtMap.cpp:23
X
X
Add a minimal nested name specifier fixit hint to allow lookup of a tag name from an outer enclosing ...
Definition: SemaDecl.cpp:13954
CFG.h
clang::AttrSyntax::C
clang::CFGElement
Represents a top-level expression in a basic block.
Definition: CFG.h:56
clang::CFGBlock::end
iterator end()
Definition: CFG.h:704
SMap
llvm::DenseMap< const Stmt *, CFGBlock * > SMap
Definition: CFGStmtMap.cpp:22