clang 10.0.0git

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