/src/llvm-project/clang/lib/AST/ParentMap.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===--- ParentMap.cpp - Mappings from Stmts to their Parents ---*- 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 ParentMap class. |
10 | | // |
11 | | //===----------------------------------------------------------------------===// |
12 | | |
13 | | #include "clang/AST/ParentMap.h" |
14 | | #include "clang/AST/Decl.h" |
15 | | #include "clang/AST/Expr.h" |
16 | | #include "clang/AST/ExprCXX.h" |
17 | | #include "clang/AST/StmtObjC.h" |
18 | | #include "llvm/ADT/DenseMap.h" |
19 | | |
20 | | using namespace clang; |
21 | | |
22 | | typedef llvm::DenseMap<Stmt*, Stmt*> MapTy; |
23 | | |
24 | | enum OpaqueValueMode { |
25 | | OV_Transparent, |
26 | | OV_Opaque |
27 | | }; |
28 | | |
29 | | static void BuildParentMap(MapTy& M, Stmt* S, |
30 | 0 | OpaqueValueMode OVMode = OV_Transparent) { |
31 | 0 | if (!S) |
32 | 0 | return; |
33 | | |
34 | 0 | switch (S->getStmtClass()) { |
35 | 0 | case Stmt::PseudoObjectExprClass: { |
36 | 0 | PseudoObjectExpr *POE = cast<PseudoObjectExpr>(S); |
37 | |
|
38 | 0 | if (OVMode == OV_Opaque && M[POE->getSyntacticForm()]) |
39 | 0 | break; |
40 | | |
41 | | // If we are rebuilding the map, clear out any existing state. |
42 | 0 | if (M[POE->getSyntacticForm()]) |
43 | 0 | for (Stmt *SubStmt : S->children()) |
44 | 0 | M[SubStmt] = nullptr; |
45 | |
|
46 | 0 | M[POE->getSyntacticForm()] = S; |
47 | 0 | BuildParentMap(M, POE->getSyntacticForm(), OV_Transparent); |
48 | |
|
49 | 0 | for (PseudoObjectExpr::semantics_iterator I = POE->semantics_begin(), |
50 | 0 | E = POE->semantics_end(); |
51 | 0 | I != E; ++I) { |
52 | 0 | M[*I] = S; |
53 | 0 | BuildParentMap(M, *I, OV_Opaque); |
54 | 0 | } |
55 | 0 | break; |
56 | 0 | } |
57 | 0 | case Stmt::BinaryConditionalOperatorClass: { |
58 | 0 | assert(OVMode == OV_Transparent && "Should not appear alongside OVEs"); |
59 | 0 | BinaryConditionalOperator *BCO = cast<BinaryConditionalOperator>(S); |
60 | |
|
61 | 0 | M[BCO->getCommon()] = S; |
62 | 0 | BuildParentMap(M, BCO->getCommon(), OV_Transparent); |
63 | |
|
64 | 0 | M[BCO->getCond()] = S; |
65 | 0 | BuildParentMap(M, BCO->getCond(), OV_Opaque); |
66 | |
|
67 | 0 | M[BCO->getTrueExpr()] = S; |
68 | 0 | BuildParentMap(M, BCO->getTrueExpr(), OV_Opaque); |
69 | |
|
70 | 0 | M[BCO->getFalseExpr()] = S; |
71 | 0 | BuildParentMap(M, BCO->getFalseExpr(), OV_Transparent); |
72 | |
|
73 | 0 | break; |
74 | 0 | } |
75 | 0 | case Stmt::OpaqueValueExprClass: { |
76 | | // FIXME: This isn't correct; it assumes that multiple OpaqueValueExprs |
77 | | // share a single source expression, but in the AST a single |
78 | | // OpaqueValueExpr is shared among multiple parent expressions. |
79 | | // The right thing to do is to give the OpaqueValueExpr its syntactic |
80 | | // parent, then not reassign that when traversing the semantic expressions. |
81 | 0 | OpaqueValueExpr *OVE = cast<OpaqueValueExpr>(S); |
82 | 0 | if (OVMode == OV_Transparent || !M[OVE->getSourceExpr()]) { |
83 | 0 | M[OVE->getSourceExpr()] = S; |
84 | 0 | BuildParentMap(M, OVE->getSourceExpr(), OV_Transparent); |
85 | 0 | } |
86 | 0 | break; |
87 | 0 | } |
88 | 0 | case Stmt::CapturedStmtClass: |
89 | 0 | for (Stmt *SubStmt : S->children()) { |
90 | 0 | if (SubStmt) { |
91 | 0 | M[SubStmt] = S; |
92 | 0 | BuildParentMap(M, SubStmt, OVMode); |
93 | 0 | } |
94 | 0 | } |
95 | 0 | if (Stmt *SubStmt = cast<CapturedStmt>(S)->getCapturedStmt()) { |
96 | 0 | M[SubStmt] = S; |
97 | 0 | BuildParentMap(M, SubStmt, OVMode); |
98 | 0 | } |
99 | 0 | break; |
100 | 0 | default: |
101 | 0 | for (Stmt *SubStmt : S->children()) { |
102 | 0 | if (SubStmt) { |
103 | 0 | M[SubStmt] = S; |
104 | 0 | BuildParentMap(M, SubStmt, OVMode); |
105 | 0 | } |
106 | 0 | } |
107 | 0 | break; |
108 | 0 | } |
109 | 0 | } |
110 | | |
111 | 0 | ParentMap::ParentMap(Stmt *S) : Impl(nullptr) { |
112 | 0 | if (S) { |
113 | 0 | MapTy *M = new MapTy(); |
114 | 0 | BuildParentMap(*M, S); |
115 | 0 | Impl = M; |
116 | 0 | } |
117 | 0 | } |
118 | | |
119 | 0 | ParentMap::~ParentMap() { |
120 | 0 | delete (MapTy*) Impl; |
121 | 0 | } |
122 | | |
123 | 0 | void ParentMap::addStmt(Stmt* S) { |
124 | 0 | if (S) { |
125 | 0 | BuildParentMap(*(MapTy*) Impl, S); |
126 | 0 | } |
127 | 0 | } |
128 | | |
129 | 0 | void ParentMap::setParent(const Stmt *S, const Stmt *Parent) { |
130 | 0 | assert(S); |
131 | 0 | assert(Parent); |
132 | 0 | MapTy *M = reinterpret_cast<MapTy *>(Impl); |
133 | 0 | M->insert(std::make_pair(const_cast<Stmt *>(S), const_cast<Stmt *>(Parent))); |
134 | 0 | } |
135 | | |
136 | 0 | Stmt* ParentMap::getParent(Stmt* S) const { |
137 | 0 | MapTy* M = (MapTy*) Impl; |
138 | 0 | return M->lookup(S); |
139 | 0 | } |
140 | | |
141 | 0 | Stmt *ParentMap::getParentIgnoreParens(Stmt *S) const { |
142 | 0 | do { S = getParent(S); } while (S && isa<ParenExpr>(S)); |
143 | 0 | return S; |
144 | 0 | } |
145 | | |
146 | 0 | Stmt *ParentMap::getParentIgnoreParenCasts(Stmt *S) const { |
147 | 0 | do { |
148 | 0 | S = getParent(S); |
149 | 0 | } |
150 | 0 | while (S && (isa<ParenExpr>(S) || isa<CastExpr>(S))); |
151 | |
|
152 | 0 | return S; |
153 | 0 | } |
154 | | |
155 | 0 | Stmt *ParentMap::getParentIgnoreParenImpCasts(Stmt *S) const { |
156 | 0 | do { |
157 | 0 | S = getParent(S); |
158 | 0 | } while (S && isa<Expr>(S) && cast<Expr>(S)->IgnoreParenImpCasts() != S); |
159 | |
|
160 | 0 | return S; |
161 | 0 | } |
162 | | |
163 | 0 | Stmt *ParentMap::getOuterParenParent(Stmt *S) const { |
164 | 0 | Stmt *Paren = nullptr; |
165 | 0 | while (isa<ParenExpr>(S)) { |
166 | 0 | Paren = S; |
167 | 0 | S = getParent(S); |
168 | 0 | }; |
169 | 0 | return Paren; |
170 | 0 | } |
171 | | |
172 | 0 | bool ParentMap::isConsumedExpr(Expr* E) const { |
173 | 0 | Stmt *P = getParent(E); |
174 | 0 | Stmt *DirectChild = E; |
175 | | |
176 | | // Ignore parents that don't guarantee consumption. |
177 | 0 | while (P && (isa<ParenExpr>(P) || isa<CastExpr>(P) || |
178 | 0 | isa<FullExpr>(P))) { |
179 | 0 | DirectChild = P; |
180 | 0 | P = getParent(P); |
181 | 0 | } |
182 | |
|
183 | 0 | if (!P) |
184 | 0 | return false; |
185 | | |
186 | 0 | switch (P->getStmtClass()) { |
187 | 0 | default: |
188 | 0 | return isa<Expr>(P); |
189 | 0 | case Stmt::DeclStmtClass: |
190 | 0 | return true; |
191 | 0 | case Stmt::BinaryOperatorClass: { |
192 | 0 | BinaryOperator *BE = cast<BinaryOperator>(P); |
193 | | // If it is a comma, only the right side is consumed. |
194 | | // If it isn't a comma, both sides are consumed. |
195 | 0 | return BE->getOpcode()!=BO_Comma ||DirectChild==BE->getRHS(); |
196 | 0 | } |
197 | 0 | case Stmt::ForStmtClass: |
198 | 0 | return DirectChild == cast<ForStmt>(P)->getCond(); |
199 | 0 | case Stmt::WhileStmtClass: |
200 | 0 | return DirectChild == cast<WhileStmt>(P)->getCond(); |
201 | 0 | case Stmt::DoStmtClass: |
202 | 0 | return DirectChild == cast<DoStmt>(P)->getCond(); |
203 | 0 | case Stmt::IfStmtClass: |
204 | 0 | return DirectChild == cast<IfStmt>(P)->getCond(); |
205 | 0 | case Stmt::IndirectGotoStmtClass: |
206 | 0 | return DirectChild == cast<IndirectGotoStmt>(P)->getTarget(); |
207 | 0 | case Stmt::SwitchStmtClass: |
208 | 0 | return DirectChild == cast<SwitchStmt>(P)->getCond(); |
209 | 0 | case Stmt::ObjCForCollectionStmtClass: |
210 | 0 | return DirectChild == cast<ObjCForCollectionStmt>(P)->getCollection(); |
211 | 0 | case Stmt::ReturnStmtClass: |
212 | 0 | return true; |
213 | 0 | } |
214 | 0 | } |
215 | | |