clang 8.0.0

ParentMap.cpp
Go to the documentation of this file.
1 //===--- ParentMap.cpp - Mappings from Stmts to their Parents ---*- 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 ParentMap class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "clang/AST/ParentMap.h"
15 #include "clang/AST/Decl.h"
16 #include "clang/AST/Expr.h"
17 #include "clang/AST/ExprCXX.h"
18 #include "clang/AST/StmtObjC.h"
19 #include "llvm/ADT/DenseMap.h"
20
21 using namespace clang;
22
23 typedef llvm::DenseMap<Stmt*, Stmt*> MapTy;
24
25 enum OpaqueValueMode {
26 OV_Transparent,
27 OV_Opaque
28 };
29
30 static void BuildParentMap(MapTy& M, Stmt* S,
31 OpaqueValueMode OVMode = OV_Transparent) {
32 if (!S)
33 return;
34
35 switch (S->getStmtClass()) {
36 case Stmt::PseudoObjectExprClass: {
37 assert(OVMode == OV_Transparent && "Should not appear alongside OVEs");
38 PseudoObjectExpr *POE = cast<PseudoObjectExpr>(S);
39
40 // If we are rebuilding the map, clear out any existing state.
41 if (M[POE->getSyntacticForm()])
42 for (Stmt *SubStmt : S->children())
43 M[SubStmt] = nullptr;
44
45 M[POE->getSyntacticForm()] = S;
46 BuildParentMap(M, POE->getSyntacticForm(), OV_Transparent);
47
48 for (PseudoObjectExpr::semantics_iterator I = POE->semantics_begin(),
49 E = POE->semantics_end();
50 I != E; ++I) {
51 M[*I] = S;
52 BuildParentMap(M, *I, OV_Opaque);
53 }
54 break;
55 }
56 case Stmt::BinaryConditionalOperatorClass: {
57 assert(OVMode == OV_Transparent && "Should not appear alongside OVEs");
58 BinaryConditionalOperator *BCO = cast<BinaryConditionalOperator>(S);
59
60 M[BCO->getCommon()] = S;
61 BuildParentMap(M, BCO->getCommon(), OV_Transparent);
62
63 M[BCO->getCond()] = S;
64 BuildParentMap(M, BCO->getCond(), OV_Opaque);
65
66 M[BCO->getTrueExpr()] = S;
67 BuildParentMap(M, BCO->getTrueExpr(), OV_Opaque);
68
69 M[BCO->getFalseExpr()] = S;
70 BuildParentMap(M, BCO->getFalseExpr(), OV_Transparent);
71
72 break;
73 }
74 case Stmt::OpaqueValueExprClass: {
75 // FIXME: This isn't correct; it assumes that multiple OpaqueValueExprs
76 // share a single source expression, but in the AST a single
77 // OpaqueValueExpr is shared among multiple parent expressions.
78 // The right thing to do is to give the OpaqueValueExpr its syntactic
79 // parent, then not reassign that when traversing the semantic expressions.
80 OpaqueValueExpr *OVE = cast<OpaqueValueExpr>(S);
81 if (OVMode == OV_Transparent || !M[OVE->getSourceExpr()]) {
82 M[OVE->getSourceExpr()] = S;
83 BuildParentMap(M, OVE->getSourceExpr(), OV_Transparent);
84 }
85 break;
86 }
87 default:
88 for (Stmt *SubStmt : S->children()) {
89 if (SubStmt) {
90 M[SubStmt] = S;
91 BuildParentMap(M, SubStmt, OVMode);
92 }
93 }
94 break;
95 }
96 }
97
98 ParentMap::ParentMap(Stmt *S) : Impl(nullptr) {
99 if (S) {
100 MapTy *M = new MapTy();
101 BuildParentMap(*M, S);
102 Impl = M;
103 }
104 }
105
106 ParentMap::~ParentMap() {
107 delete (MapTy*) Impl;
108 }
109
110 void ParentMap::addStmt(Stmt* S) {
111 if (S) {
112 BuildParentMap(*(MapTy*) Impl, S);
113 }
114 }
115
116 void ParentMap::setParent(const Stmt *S, const Stmt *Parent) {
117 assert(S);
118 assert(Parent);
119 MapTy *M = reinterpret_cast<MapTy *>(Impl);
120 M->insert(std::make_pair(const_cast<Stmt *>(S), const_cast<Stmt *>(Parent)));
121 }
122
123 Stmt* ParentMap::getParent(Stmt* S) const {
124 MapTy* M = (MapTy*) Impl;
125 MapTy::iterator I = M->find(S);
126 return I == M->end() ? nullptr : I->second;
127 }
128
129 Stmt *ParentMap::getParentIgnoreParens(Stmt *S) const {
130 do { S = getParent(S); } while (S && isa<ParenExpr>(S));
131 return S;
132 }
133
134 Stmt *ParentMap::getParentIgnoreParenCasts(Stmt *S) const {
135 do {
136 S = getParent(S);
137 }
138 while (S && (isa<ParenExpr>(S) || isa<CastExpr>(S)));
139
140 return S;
141 }
142
143 Stmt *ParentMap::getParentIgnoreParenImpCasts(Stmt *S) const {
144 do {
145 S = getParent(S);
146 } while (S && isa<Expr>(S) && cast<Expr>(S)->IgnoreParenImpCasts() != S);
147
148 return S;
149 }
150
151 Stmt *ParentMap::getOuterParenParent(Stmt *S) const {
152 Stmt *Paren = nullptr;
153 while (isa<ParenExpr>(S)) {
154 Paren = S;
155 S = getParent(S);
156 };
157 return Paren;
158 }
159
160 bool ParentMap::isConsumedExpr(Expr* E) const {
161 Stmt *P = getParent(E);
162 Stmt *DirectChild = E;
163
164 // Ignore parents that don't guarantee consumption.
165 while (P && (isa<ParenExpr>(P) || isa<CastExpr>(P) ||
166 isa<FullExpr>(P))) {
167 DirectChild = P;
168 P = getParent(P);
169 }
170
171 if (!P)
172 return false;
173
174 switch (P->getStmtClass()) {
175 default:
176 return isa<Expr>(P);
177 case Stmt::DeclStmtClass:
178 return true;
179 case Stmt::BinaryOperatorClass: {
180 BinaryOperator *BE = cast<BinaryOperator>(P);
181 // If it is a comma, only the right side is consumed.
182 // If it isn't a comma, both sides are consumed.
183 return BE->getOpcode()!=BO_Comma ||DirectChild==BE->getRHS();
184 }
185 case Stmt::ForStmtClass:
186 return DirectChild == cast<ForStmt>(P)->getCond();
187 case Stmt::WhileStmtClass:
188 return DirectChild == cast<WhileStmt>(P)->getCond();
189 case Stmt::DoStmtClass:
190 return DirectChild == cast<DoStmt>(P)->getCond();
191 case Stmt::IfStmtClass:
192 return DirectChild == cast<IfStmt>(P)->getCond();
193 case Stmt::IndirectGotoStmtClass:
194 return DirectChild == cast<IndirectGotoStmt>(P)->getTarget();
195 case Stmt::SwitchStmtClass:
196 return DirectChild == cast<SwitchStmt>(P)->getCond();
197 case Stmt::ObjCForCollectionStmtClass:
198 return DirectChild == cast<ObjCForCollectionStmt>(P)->getCollection();
199 case Stmt::ReturnStmtClass:
200 return true;
201 }
202 }
203
clang::PseudoObjectExpr::getSyntacticForm
Expr * getSyntacticForm()
Return the syntactic form of this expression, i.e.
Definition: Expr.h:5343
clang::PseudoObjectExpr::semantics_iterator
Expr *const * semantics_iterator
Definition: Expr.h:5365
clang::Stmt
Stmt - This represents one statement.
Definition: Stmt.h:66
clang::BinaryOperator::getOpcode
Opcode getOpcode() const
Definition: Expr.h:3322
P
StringRef P
Definition: ASTMatchersInternal.cpp:439
clang::ParentMap::getParent
Stmt * getParent(Stmt *) const
Definition: ParentMap.cpp:123
MapTy
llvm::DenseMap< Stmt *, Stmt * > MapTy
Definition: ParentMap.cpp:23
clang::ParentMap::isConsumedExpr
bool isConsumedExpr(Expr *E) const
Definition: ParentMap.cpp:160
StmtObjC.h
Defines the Objective-C statement AST node classes.
ExprCXX.h
Defines the clang::Expr interface and subclasses for C++ expressions.
clang::ParentMap::addStmt
void addStmt(Stmt *S)
Adds and/or updates the parent/child-relations of the complete stmt tree of S.
Definition: ParentMap.cpp:110
clang::OpaqueValueExpr::getSourceExpr
Expr * getSourceExpr() const
The source expression of an opaque value expression is the expression which originally generated the ...
Definition: Expr.h:1003
clang::ParentMap::~ParentMap
~ParentMap()
Definition: ParentMap.cpp:106
clang::ParentMap::setParent
void setParent(const Stmt *S, const Stmt *Parent)
Manually sets the parent of S to Parent.
Definition: ParentMap.cpp:116
OV_Opaque
Definition: ParentMap.cpp:27
clang::Stmt::children
child_range children()
Definition: Stmt.cpp:237
clang::PseudoObjectExpr::semantics_end
semantics_iterator semantics_end()
Definition: Expr.h:5373
clang::BinaryOperator
A builtin binary operation expression such as "x + y" or "x <= y".
Definition: Expr.h:3287
Parent
NodeId Parent
Definition: ASTDiff.cpp:192
OV_Transparent
Definition: ParentMap.cpp:26
clang::Expr
This represents one expression.
Definition: Expr.h:106
ParentMap.h
BuildParentMap
static void BuildParentMap(MapTy &M, Stmt *S, OpaqueValueMode OVMode=OV_Transparent)
Definition: ParentMap.cpp:30
clang::BinaryConditionalOperator::getTrueExpr
Expr * getTrueExpr() const
getTrueExpr - Return the subexpression which will be evaluated if the condition evaluates to true; th...
Definition: Expr.h:3707
clang::OpaqueValueExpr
OpaqueValueExpr - An expression referring to an opaque object of a fixed type and value class...
Definition: Expr.h:945
clang::PseudoObjectExpr
PseudoObjectExpr - An expression which accesses a pseudo-object l-value.
Definition: Expr.h:5299
clang::ParentMap::getParentIgnoreParens
Stmt * getParentIgnoreParens(Stmt *) const
Definition: ParentMap.cpp:129
OpaqueValueMode
OpaqueValueMode
Definition: ParentMap.cpp:25
Paren
Definition: PPMacroExpansion.cpp:619
clang::BinaryConditionalOperator::getFalseExpr
Expr * getFalseExpr() const
getFalseExpr - Return the subexpression which will be evaluated if the condnition evaluates to false;...
Definition: Expr.h:3714
Decl.h
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:22
clang::ParentMap::ParentMap
ParentMap(Stmt *ASTRoot)
Definition: ParentMap.cpp:98
clang::Stmt::getStmtClass
StmtClass getStmtClass() const
Definition: Stmt.h:1029
clang::PseudoObjectExpr::semantics_begin
semantics_iterator semantics_begin()
Definition: Expr.h:5367
clang::ParentMap::getParentIgnoreParenImpCasts
Stmt * getParentIgnoreParenImpCasts(Stmt *) const
Definition: ParentMap.cpp:143
clang::BinaryConditionalOperator::getCond
Expr * getCond() const
getCond - Return the condition expression; this is defined in terms of the opaque value...
Definition: Expr.h:3702
clang::BinaryConditionalOperator
BinaryConditionalOperator - The GNU extension to the conditional operator which allows the middle ope...
Definition: Expr.h:3655
Expr.h
clang::ParentMap::getParentIgnoreParenCasts
Stmt * getParentIgnoreParenCasts(Stmt *) const
Definition: ParentMap.cpp:134
clang::BinaryOperator::getRHS
Expr * getRHS() const
Definition: Expr.h:3329
clang::BinaryConditionalOperator::getCommon
Expr * getCommon() const
getCommon - Return the common expression, written to the left of the condition.
Definition: Expr.h:3695
clang::ParentMap::getOuterParenParent
Stmt * getOuterParenParent(Stmt *) const
Definition: ParentMap.cpp:151