Debian Codegraph @

src/HierarchyUtils.h

raw
1 /*
2 SPDX-FileCopyrightText: 2015 Klarälvdalens Datakonsult AB a KDAB Group company [email protected]
3 SPDX-FileContributor: Sérgio Martins <[email protected]>
4
5 SPDX-FileCopyrightText: 2015 Sergio Martins <[email protected]>
6
7 SPDX-License-Identifier: LGPL-2.0-or-later
8 */
9
10 #ifndef CLAZY_HIERARCHY_UTILS_H
11 #define CLAZY_HIERARCHY_UTILS_H
12
13 // Contains utility classes to retrieve parents and childs from AST Nodes
14
15 #include "StringUtils.h"
16 #include "clazy_stl.h"
17
18 #include <clang/AST/ExprCXX.h>
19 #include <clang/AST/ParentMap.h>
20 #include <clang/AST/Stmt.h>
21 #include <clang/Frontend/CompilerInstance.h>
22
23 namespace clazy
24 {
25 enum IgnoreStmt { IgnoreNone = 0, IgnoreImplicitCasts = 1, IgnoreExprWithCleanups = 2 };
26
27 using IgnoreStmts = int;
28
29 /**
30 * Returns true if child is a child of parent.
31 */
32 inline bool isChildOf(clang::Stmt *child, clang::Stmt *parent)
33 {
34 if (!child || !parent) {
35 return false;
36 }
37
38 return clazy::any_of(parent->children(), [child](clang::Stmt *c) {
39 return c == child || isChildOf(child, c);
40 });
41 }
42
43 /**
44 * Returns true if stm is parent of a member function call named "name"
45 */
46
47 inline bool isParentOfMemberFunctionCall(clang::Stmt *stm, const std::string &name)
48 {
49 if (!stm) {
50 return false;
51 }
52
53 if (auto *expr = llvm::dyn_cast<clang::MemberExpr>(stm)) {
54 auto *namedDecl = llvm::dyn_cast<clang::NamedDecl>(expr->getMemberDecl());
55 if (namedDecl && clazy::name(namedDecl) == name) {
56 return true;
57 }
58 }
59
60 return clazy::any_of(stm->children(), [name](clang::Stmt *child) {
61 return isParentOfMemberFunctionCall(child, name);
62 });
63 }
64
65 /**
66 * Returns the first child of stm of type T.
67 * Does depth-first.
68 */
69 template<typename T>
70 T *getFirstChildOfType(clang::Stmt *stm)
71 {
72 if (!stm) {
73 return nullptr;
74 }
75
76 for (auto *child : stm->children()) {
77 if (!child) { // Can happen
78 continue;
79 }
80
81 if (auto s = clang::dyn_cast<T>(child)) {
82 return s;
83 }
84
85 if (auto s = getFirstChildOfType<T>(child)) {
86 return s;
87 }
88 }
89
90 return nullptr;
91 }
92
93 /**
94 * Returns the first child of stm of type T, but only looks at the first branch.
95 */
96 template<typename T>
97 T *getFirstChildOfType2(clang::Stmt *stm)
98 {
99 if (!stm) {
100 return nullptr;
101 }
102
103 if (clazy::hasChildren(stm)) {
104 auto *child = *(stm->child_begin());
105
106 if (!child) { // can happen
107 return nullptr;
108 }
109
110 if (auto s = clang::dyn_cast<T>(child)) {
111 return s;
112 }
113
114 if (auto s = getFirstChildOfType<T>(child)) {
115 return s;
116 }
117 }
118
119 return nullptr;
120 }
121
122 // If depth = 0, return s
123 // If depth = 1, returns parent of s
124 // etc.
125 inline clang::Stmt *parent(clang::ParentMap *map, clang::Stmt *s, unsigned int depth = 1)
126 {
127 if (!s) {
128 return nullptr;
129 }
130
131 return depth == 0 ? s : clazy::parent(map, map->getParent(s), depth - 1);
132 }
133
134 // Returns the first parent of type T, with max depth depth
135 template<typename T>
136 T *getFirstParentOfType(clang::ParentMap *pmap, clang::Stmt *s, unsigned int depth = -1)
137 {
138 if (!s) {
139 return nullptr;
140 }
141
142 if (auto t = clang::dyn_cast<T>(s)) {
143 return t;
144 }
145
146 if (depth == 0) {
147 return nullptr;
148 }
149
150 --depth;
151 return getFirstParentOfType<T>(pmap, parent(pmap, s), depth);
152 }
153
154 inline clang::Stmt *getFirstChild(clang::Stmt *parent)
155 {
156 if (!parent) {
157 return nullptr;
158 }
159
160 auto it = parent->child_begin();
161 return it == parent->child_end() ? nullptr : *it;
162 }
163
164 inline clang::Stmt *getFirstChildAtDepth(clang::Stmt *s, unsigned int depth)
165 {
166 if (depth == 0 || !s) {
167 return s;
168 }
169
170 return clazy::hasChildren(s) ? getFirstChildAtDepth(*s->child_begin(), --depth) : nullptr;
171 }
172
173 template<typename T>
174 void getChilds(clang::Stmt *stmt, std::vector<T *> &result_list, int depth = -1)
175 {
176 if (!stmt) {
177 return;
178 }
179
180 auto cexpr = llvm::dyn_cast<T>(stmt);
181 if (cexpr) {
182 result_list.push_back(cexpr);
183 }
184
185 if (depth > 0 || depth == -1) {
186 if (depth > 0) {
187 --depth;
188 }
189 for (auto *child : stmt->children()) {
190 getChilds(child, result_list, depth);
191 }
192 }
193 }
194
195 inline bool isIgnoredByOption(clang::Stmt *s, IgnoreStmts options)
196 {
197 return ((options & IgnoreImplicitCasts) && llvm::isa<clang::ImplicitCastExpr>(s))
198 || ((options & IgnoreExprWithCleanups) && llvm::isa<clang::ExprWithCleanups>(s));
199 }
200
201 /**
202 * Returns all statements of type T in body, starting from startLocation, or from body->getLocStart() if
203 * startLocation is null.
204 *
205 * Similar to getChilds(), but with startLocation support.
206 */
207 template<typename T>
208 std::vector<T *> getStatements(clang::Stmt *body,
209 const clang::SourceManager *sm = nullptr,
210 clang::SourceLocation startLocation = {},
211 int depth = -1,
212 bool includeParent = false,
213 IgnoreStmts ignoreOptions = IgnoreNone)
214 {
215 std::vector<T *> statements;
216 if (!body || depth == 0) {
217 return statements;
218 }
219
220 if (includeParent) {
221 if (T *t = clang::dyn_cast<T>(body)) {
222 statements.push_back(t);
223 }
224 }
225
226 for (auto *child : body->children()) {
227 if (!child) {
228 continue; // can happen
229 }
230 if (T *childT = clang::dyn_cast<T>(child)) {
231 if (!startLocation.isValid() || (sm && sm->isBeforeInSLocAddrSpace(sm->getSpellingLoc(startLocation), child->getBeginLoc()))) {
232 statements.push_back(childT);
233 }
234 }
235
236 if (!isIgnoredByOption(child, ignoreOptions)) {
237 --depth;
238 }
239
240 auto childStatements = getStatements<T>(child, sm, startLocation, depth, false, ignoreOptions);
241 statements.reserve(statements.size() + childStatements.size());
242 std::ranges::copy(childStatements, std::back_inserter(statements));
243 }
244
245 return statements;
246 }
247
248 /**
249 * If stmt is of type T, then stmt is returned.
250 * If stmt is of type IgnoreImplicitCast or IgnoreExprWithCleanups (depending on options) then stmt's
251 * first child is tested instead (recurses).
252 * Otherwise nullptr is returned.
253 *
254 * This is useful for example when the interesting statement is under an Implicit cast, so:
255 **/
256 template<typename T>
257 T *unpeal(clang::Stmt *stmt, IgnoreStmts options = IgnoreNone)
258 {
259 if (!stmt) {
260 return nullptr;
261 }
262
263 if (auto tt = llvm::dyn_cast<T>(stmt)) {
264 return tt;
265 }
266
267 if ((options & IgnoreImplicitCasts) && llvm::isa<clang::ImplicitCastExpr>(stmt)) {
268 return unpeal<T>(clazy::getFirstChild(stmt), options);
269 }
270
271 if ((options & IgnoreExprWithCleanups) && llvm::isa<clang::ExprWithCleanups>(stmt)) {
272 return unpeal<T>(clazy::getFirstChild(stmt), options);
273 }
274
275 return nullptr;
276 }
277
278 inline clang::SwitchStmt *getSwitchFromCase(clang::ParentMap *pmap, clang::CaseStmt *caseStm)
279 {
280 return getFirstParentOfType<clang::SwitchStmt>(pmap, caseStm);
281 }
282
283 }
284
285 #endif