blob: a58d4f0fb4775ac3bfd99bb3987a03f167ae028c [file] [log] [blame]
Manuel Klimek4da21662012-07-06 05:48:52 +00001//===--- ASTMatchFinder.cpp - Structural query framework ------------------===//
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// Implements an algorithm to efficiently search for matches on AST nodes.
11// Uses memoization to support recursive matches like HasDescendant.
12//
13// The general idea is to visit all AST nodes with a RecursiveASTVisitor,
14// calling the Matches(...) method of each matcher we are running on each
15// AST node. The matcher can recurse via the ASTMatchFinder interface.
16//
17//===----------------------------------------------------------------------===//
18
19#include "clang/ASTMatchers/ASTMatchFinder.h"
20#include "clang/AST/ASTConsumer.h"
21#include "clang/AST/ASTContext.h"
22#include "clang/AST/RecursiveASTVisitor.h"
23#include <set>
24
25namespace clang {
26namespace ast_matchers {
27namespace internal {
28namespace {
29
Manuel Klimek4da21662012-07-06 05:48:52 +000030// We use memoization to avoid running the same matcher on the same
31// AST node twice. This pair is the key for looking up match
32// result. It consists of an ID of the MatcherInterface (for
33// identifying the matcher) and a pointer to the AST node.
34typedef std::pair<uint64_t, const void*> UntypedMatchInput;
35
36// Used to store the result of a match and possibly bound nodes.
37struct MemoizedMatchResult {
38 bool ResultOfMatch;
39 BoundNodesTree Nodes;
40};
41
42// A RecursiveASTVisitor that traverses all children or all descendants of
43// a node.
44class MatchChildASTVisitor
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000045 : public RecursiveASTVisitor<MatchChildASTVisitor> {
Manuel Klimek4da21662012-07-06 05:48:52 +000046public:
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000047 typedef RecursiveASTVisitor<MatchChildASTVisitor> VisitorBase;
Manuel Klimek4da21662012-07-06 05:48:52 +000048
49 // Creates an AST visitor that matches 'matcher' on all children or
50 // descendants of a traversed node. max_depth is the maximum depth
51 // to traverse: use 1 for matching the children and INT_MAX for
52 // matching the descendants.
53 MatchChildASTVisitor(const UntypedBaseMatcher *BaseMatcher,
54 ASTMatchFinder *Finder,
55 BoundNodesTreeBuilder *Builder,
56 int MaxDepth,
57 ASTMatchFinder::TraversalKind Traversal,
58 ASTMatchFinder::BindKind Bind)
59 : BaseMatcher(BaseMatcher),
60 Finder(Finder),
61 Builder(Builder),
62 CurrentDepth(-1),
63 MaxDepth(MaxDepth),
64 Traversal(Traversal),
65 Bind(Bind),
66 Matches(false) {}
67
68 // Returns true if a match is found in the subtree rooted at the
69 // given AST node. This is done via a set of mutually recursive
70 // functions. Here's how the recursion is done (the *wildcard can
71 // actually be Decl, Stmt, or Type):
72 //
73 // - Traverse(node) calls BaseTraverse(node) when it needs
74 // to visit the descendants of node.
75 // - BaseTraverse(node) then calls (via VisitorBase::Traverse*(node))
76 // Traverse*(c) for each child c of 'node'.
77 // - Traverse*(c) in turn calls Traverse(c), completing the
78 // recursion.
79 template <typename T>
80 bool findMatch(const T &Node) {
81 reset();
82 traverse(Node);
83 return Matches;
84 }
85
86 // The following are overriding methods from the base visitor class.
87 // They are public only to allow CRTP to work. They are *not *part
88 // of the public API of this class.
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000089 bool TraverseDecl(Decl *DeclNode) {
Manuel Klimek4da21662012-07-06 05:48:52 +000090 return (DeclNode == NULL) || traverse(*DeclNode);
91 }
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000092 bool TraverseStmt(Stmt *StmtNode) {
93 const Stmt *StmtToTraverse = StmtNode;
Manuel Klimek4da21662012-07-06 05:48:52 +000094 if (Traversal ==
95 ASTMatchFinder::TK_IgnoreImplicitCastsAndParentheses) {
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000096 const Expr *ExprNode = dyn_cast_or_null<Expr>(StmtNode);
Manuel Klimek4da21662012-07-06 05:48:52 +000097 if (ExprNode != NULL) {
98 StmtToTraverse = ExprNode->IgnoreParenImpCasts();
99 }
100 }
101 return (StmtToTraverse == NULL) || traverse(*StmtToTraverse);
102 }
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000103 bool TraverseType(QualType TypeNode) {
Manuel Klimek4da21662012-07-06 05:48:52 +0000104 return traverse(TypeNode);
105 }
106
107 bool shouldVisitTemplateInstantiations() const { return true; }
108 bool shouldVisitImplicitCode() const { return true; }
109
110private:
111 // Used for updating the depth during traversal.
112 struct ScopedIncrement {
113 explicit ScopedIncrement(int *Depth) : Depth(Depth) { ++(*Depth); }
114 ~ScopedIncrement() { --(*Depth); }
115
116 private:
117 int *Depth;
118 };
119
120 // Resets the state of this object.
121 void reset() {
122 Matches = false;
123 CurrentDepth = -1;
124 }
125
126 // Forwards the call to the corresponding Traverse*() method in the
127 // base visitor class.
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000128 bool baseTraverse(const Decl &DeclNode) {
129 return VisitorBase::TraverseDecl(const_cast<Decl*>(&DeclNode));
Manuel Klimek4da21662012-07-06 05:48:52 +0000130 }
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000131 bool baseTraverse(const Stmt &StmtNode) {
132 return VisitorBase::TraverseStmt(const_cast<Stmt*>(&StmtNode));
Manuel Klimek4da21662012-07-06 05:48:52 +0000133 }
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000134 bool baseTraverse(QualType TypeNode) {
Manuel Klimek4da21662012-07-06 05:48:52 +0000135 return VisitorBase::TraverseType(TypeNode);
136 }
137
138 // Traverses the subtree rooted at 'node'; returns true if the
139 // traversal should continue after this function returns; also sets
140 // matched_ to true if a match is found during the traversal.
141 template <typename T>
142 bool traverse(const T &Node) {
143 TOOLING_COMPILE_ASSERT(IsBaseType<T>::value,
144 traverse_can_only_be_instantiated_with_base_type);
145 ScopedIncrement ScopedDepth(&CurrentDepth);
146 if (CurrentDepth == 0) {
147 // We don't want to match the root node, so just recurse.
148 return baseTraverse(Node);
149 }
150 if (Bind != ASTMatchFinder::BK_All) {
151 if (BaseMatcher->matches(Node, Finder, Builder)) {
152 Matches = true;
153 return false; // Abort as soon as a match is found.
154 }
155 if (CurrentDepth < MaxDepth) {
156 // The current node doesn't match, and we haven't reached the
157 // maximum depth yet, so recurse.
158 return baseTraverse(Node);
159 }
160 // The current node doesn't match, and we have reached the
161 // maximum depth, so don't recurse (but continue the traversal
162 // such that other nodes at the current level can be visited).
163 return true;
164 } else {
165 BoundNodesTreeBuilder RecursiveBuilder;
166 if (BaseMatcher->matches(Node, Finder, &RecursiveBuilder)) {
167 // After the first match the matcher succeeds.
168 Matches = true;
169 Builder->addMatch(RecursiveBuilder.build());
170 }
171 if (CurrentDepth < MaxDepth) {
172 baseTraverse(Node);
173 }
174 // In kBindAll mode we always search for more matches.
175 return true;
176 }
177 }
178
179 const UntypedBaseMatcher *const BaseMatcher;
180 ASTMatchFinder *const Finder;
181 BoundNodesTreeBuilder *const Builder;
182 int CurrentDepth;
183 const int MaxDepth;
184 const ASTMatchFinder::TraversalKind Traversal;
185 const ASTMatchFinder::BindKind Bind;
186 bool Matches;
187};
188
189// Controls the outermost traversal of the AST and allows to match multiple
190// matchers.
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000191class MatchASTVisitor : public RecursiveASTVisitor<MatchASTVisitor>,
Manuel Klimek4da21662012-07-06 05:48:52 +0000192 public ASTMatchFinder {
193public:
194 MatchASTVisitor(std::vector< std::pair<const UntypedBaseMatcher*,
195 MatchFinder::MatchCallback*> > *Triggers)
196 : Triggers(Triggers),
197 ActiveASTContext(NULL) {
198 }
199
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000200 void set_active_ast_context(ASTContext *NewActiveASTContext) {
Manuel Klimek4da21662012-07-06 05:48:52 +0000201 ActiveASTContext = NewActiveASTContext;
202 }
203
204 // The following Visit*() and Traverse*() functions "override"
205 // methods in RecursiveASTVisitor.
206
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000207 bool VisitTypedefDecl(TypedefDecl *DeclNode) {
Manuel Klimek4da21662012-07-06 05:48:52 +0000208 // When we see 'typedef A B', we add name 'B' to the set of names
209 // A's canonical type maps to. This is necessary for implementing
210 // IsDerivedFrom(x) properly, where x can be the name of the base
211 // class or any of its aliases.
212 //
213 // In general, the is-alias-of (as defined by typedefs) relation
214 // is tree-shaped, as you can typedef a type more than once. For
215 // example,
216 //
217 // typedef A B;
218 // typedef A C;
219 // typedef C D;
220 // typedef C E;
221 //
222 // gives you
223 //
224 // A
225 // |- B
226 // `- C
227 // |- D
228 // `- E
229 //
230 // It is wrong to assume that the relation is a chain. A correct
231 // implementation of IsDerivedFrom() needs to recognize that B and
232 // E are aliases, even though neither is a typedef of the other.
233 // Therefore, we cannot simply walk through one typedef chain to
234 // find out whether the type name matches.
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000235 const Type *TypeNode = DeclNode->getUnderlyingType().getTypePtr();
236 const Type *CanonicalType = // root of the typedef tree
Manuel Klimek4da21662012-07-06 05:48:52 +0000237 ActiveASTContext->getCanonicalType(TypeNode);
Daniel Jasper20b802d2012-07-17 07:39:27 +0000238 TypeAliases[CanonicalType].insert(DeclNode);
Manuel Klimek4da21662012-07-06 05:48:52 +0000239 return true;
240 }
241
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000242 bool TraverseDecl(Decl *DeclNode);
243 bool TraverseStmt(Stmt *StmtNode);
244 bool TraverseType(QualType TypeNode);
245 bool TraverseTypeLoc(TypeLoc TypeNode);
Manuel Klimek4da21662012-07-06 05:48:52 +0000246
247 // Matches children or descendants of 'Node' with 'BaseMatcher'.
248 template <typename T>
249 bool memoizedMatchesRecursively(const T &Node,
250 const UntypedBaseMatcher &BaseMatcher,
251 BoundNodesTreeBuilder *Builder, int MaxDepth,
252 TraversalKind Traversal, BindKind Bind) {
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000253 TOOLING_COMPILE_ASSERT((llvm::is_same<T, Decl>::value) ||
254 (llvm::is_same<T, Stmt>::value),
Manuel Klimek4da21662012-07-06 05:48:52 +0000255 type_does_not_support_memoization);
256 const UntypedMatchInput input(BaseMatcher.getID(), &Node);
257 std::pair<MemoizationMap::iterator, bool> InsertResult
258 = ResultCache.insert(std::make_pair(input, MemoizedMatchResult()));
259 if (InsertResult.second) {
260 BoundNodesTreeBuilder DescendantBoundNodesBuilder;
261 InsertResult.first->second.ResultOfMatch =
262 matchesRecursively(Node, BaseMatcher, &DescendantBoundNodesBuilder,
263 MaxDepth, Traversal, Bind);
264 InsertResult.first->second.Nodes =
265 DescendantBoundNodesBuilder.build();
266 }
267 InsertResult.first->second.Nodes.copyTo(Builder);
268 return InsertResult.first->second.ResultOfMatch;
269 }
270
271 // Matches children or descendants of 'Node' with 'BaseMatcher'.
272 template <typename T>
273 bool matchesRecursively(const T &Node, const UntypedBaseMatcher &BaseMatcher,
274 BoundNodesTreeBuilder *Builder, int MaxDepth,
275 TraversalKind Traversal, BindKind Bind) {
276 MatchChildASTVisitor Visitor(
277 &BaseMatcher, this, Builder, MaxDepth, Traversal, Bind);
278 return Visitor.findMatch(Node);
279 }
280
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000281 virtual bool classIsDerivedFrom(const CXXRecordDecl *Declaration,
Daniel Jasper20b802d2012-07-17 07:39:27 +0000282 const Matcher<NamedDecl> &Base,
283 BoundNodesTreeBuilder *Builder);
Manuel Klimek4da21662012-07-06 05:48:52 +0000284
285 // Implements ASTMatchFinder::MatchesChildOf.
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000286 virtual bool matchesChildOf(const Decl &DeclNode,
Manuel Klimek4da21662012-07-06 05:48:52 +0000287 const UntypedBaseMatcher &BaseMatcher,
288 BoundNodesTreeBuilder *Builder,
289 TraversalKind Traversal,
290 BindKind Bind) {
291 return matchesRecursively(DeclNode, BaseMatcher, Builder, 1, Traversal,
292 Bind);
293 }
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000294 virtual bool matchesChildOf(const Stmt &StmtNode,
Manuel Klimek4da21662012-07-06 05:48:52 +0000295 const UntypedBaseMatcher &BaseMatcher,
296 BoundNodesTreeBuilder *Builder,
297 TraversalKind Traversal,
298 BindKind Bind) {
299 return matchesRecursively(StmtNode, BaseMatcher, Builder, 1, Traversal,
300 Bind);
301 }
302
303 // Implements ASTMatchFinder::MatchesDescendantOf.
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000304 virtual bool matchesDescendantOf(const Decl &DeclNode,
Manuel Klimek4da21662012-07-06 05:48:52 +0000305 const UntypedBaseMatcher &BaseMatcher,
306 BoundNodesTreeBuilder *Builder,
307 BindKind Bind) {
308 return memoizedMatchesRecursively(DeclNode, BaseMatcher, Builder, INT_MAX,
309 TK_AsIs, Bind);
310 }
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000311 virtual bool matchesDescendantOf(const Stmt &StmtNode,
Manuel Klimek4da21662012-07-06 05:48:52 +0000312 const UntypedBaseMatcher &BaseMatcher,
313 BoundNodesTreeBuilder *Builder,
314 BindKind Bind) {
315 return memoizedMatchesRecursively(StmtNode, BaseMatcher, Builder, INT_MAX,
316 TK_AsIs, Bind);
317 }
318
319 bool shouldVisitTemplateInstantiations() const { return true; }
320 bool shouldVisitImplicitCode() const { return true; }
321
322private:
323 // Implements a BoundNodesTree::Visitor that calls a MatchCallback with
324 // the aggregated bound nodes for each match.
325 class MatchVisitor : public BoundNodesTree::Visitor {
326 public:
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000327 MatchVisitor(ASTContext* Context,
Manuel Klimek4da21662012-07-06 05:48:52 +0000328 MatchFinder::MatchCallback* Callback)
329 : Context(Context),
330 Callback(Callback) {}
331
332 virtual void visitMatch(const BoundNodes& BoundNodesView) {
333 Callback->run(MatchFinder::MatchResult(BoundNodesView, Context));
334 }
335
336 private:
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000337 ASTContext* Context;
Manuel Klimek4da21662012-07-06 05:48:52 +0000338 MatchFinder::MatchCallback* Callback;
339 };
340
Daniel Jasper20b802d2012-07-17 07:39:27 +0000341 // Returns true if 'TypeNode' has an alias that matches the given matcher.
342 bool typeHasMatchingAlias(const Type *TypeNode,
343 const Matcher<NamedDecl> Matcher,
344 BoundNodesTreeBuilder *Builder) {
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000345 const Type *const CanonicalType =
Manuel Klimek4da21662012-07-06 05:48:52 +0000346 ActiveASTContext->getCanonicalType(TypeNode);
Daniel Jasper20b802d2012-07-17 07:39:27 +0000347 const std::set<const TypedefDecl*> &Aliases = TypeAliases[CanonicalType];
348 for (std::set<const TypedefDecl*>::const_iterator
349 It = Aliases.begin(), End = Aliases.end();
350 It != End; ++It) {
351 if (Matcher.matches(**It, this, Builder))
352 return true;
353 }
354 return false;
Manuel Klimek4da21662012-07-06 05:48:52 +0000355 }
356
357 // Matches all registered matchers on the given node and calls the
358 // result callback for every node that matches.
359 template <typename T>
360 void match(const T &node) {
361 for (std::vector< std::pair<const UntypedBaseMatcher*,
362 MatchFinder::MatchCallback*> >::const_iterator
363 It = Triggers->begin(), End = Triggers->end();
364 It != End; ++It) {
365 BoundNodesTreeBuilder Builder;
366 if (It->first->matches(node, this, &Builder)) {
367 BoundNodesTree BoundNodes = Builder.build();
368 MatchVisitor Visitor(ActiveASTContext, It->second);
369 BoundNodes.visitMatches(&Visitor);
370 }
371 }
372 }
373
374 std::vector< std::pair<const UntypedBaseMatcher*,
375 MatchFinder::MatchCallback*> > *const Triggers;
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000376 ASTContext *ActiveASTContext;
Manuel Klimek4da21662012-07-06 05:48:52 +0000377
Daniel Jasper20b802d2012-07-17 07:39:27 +0000378 // Maps a canonical type to its TypedefDecls.
379 llvm::DenseMap<const Type*, std::set<const TypedefDecl*> > TypeAliases;
Manuel Klimek4da21662012-07-06 05:48:52 +0000380
381 // Maps (matcher, node) -> the match result for memoization.
382 typedef llvm::DenseMap<UntypedMatchInput, MemoizedMatchResult> MemoizationMap;
383 MemoizationMap ResultCache;
384};
385
386// Returns true if the given class is directly or indirectly derived
387// from a base type with the given name. A class is considered to be
388// also derived from itself.
Daniel Jasper20b802d2012-07-17 07:39:27 +0000389bool MatchASTVisitor::classIsDerivedFrom(const CXXRecordDecl *Declaration,
390 const Matcher<NamedDecl> &Base,
391 BoundNodesTreeBuilder *Builder) {
392 if (Base.matches(*Declaration, this, Builder))
Manuel Klimek4da21662012-07-06 05:48:52 +0000393 return true;
Daniel Jasper20b802d2012-07-17 07:39:27 +0000394 if (!Declaration->hasDefinition())
Manuel Klimek4da21662012-07-06 05:48:52 +0000395 return false;
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000396 typedef CXXRecordDecl::base_class_const_iterator BaseIterator;
Manuel Klimek4da21662012-07-06 05:48:52 +0000397 for (BaseIterator It = Declaration->bases_begin(),
398 End = Declaration->bases_end(); It != End; ++It) {
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000399 const Type *TypeNode = It->getType().getTypePtr();
Manuel Klimek4da21662012-07-06 05:48:52 +0000400
Daniel Jasper20b802d2012-07-17 07:39:27 +0000401 if (typeHasMatchingAlias(TypeNode, Base, Builder))
Manuel Klimek4da21662012-07-06 05:48:52 +0000402 return true;
403
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000404 // Type::getAs<...>() drills through typedefs.
405 if (TypeNode->getAs<DependentNameType>() != NULL ||
Daniel Jasper20b802d2012-07-17 07:39:27 +0000406 TypeNode->getAs<TemplateTypeParmType>() != NULL)
Manuel Klimek4da21662012-07-06 05:48:52 +0000407 // Dependent names and template TypeNode parameters will be matched when
408 // the template is instantiated.
409 continue;
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000410 CXXRecordDecl *ClassDecl = NULL;
411 TemplateSpecializationType const *TemplateType =
412 TypeNode->getAs<TemplateSpecializationType>();
Manuel Klimek4da21662012-07-06 05:48:52 +0000413 if (TemplateType != NULL) {
Daniel Jasper20b802d2012-07-17 07:39:27 +0000414 if (TemplateType->getTemplateName().isDependent())
Manuel Klimek4da21662012-07-06 05:48:52 +0000415 // Dependent template specializations will be matched when the
416 // template is instantiated.
417 continue;
Daniel Jasper20b802d2012-07-17 07:39:27 +0000418
Manuel Klimek4da21662012-07-06 05:48:52 +0000419 // For template specialization types which are specializing a template
420 // declaration which is an explicit or partial specialization of another
421 // template declaration, getAsCXXRecordDecl() returns the corresponding
422 // ClassTemplateSpecializationDecl.
423 //
424 // For template specialization types which are specializing a template
425 // declaration which is neither an explicit nor partial specialization of
426 // another template declaration, getAsCXXRecordDecl() returns NULL and
427 // we get the CXXRecordDecl of the templated declaration.
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000428 CXXRecordDecl *SpecializationDecl =
Manuel Klimek4da21662012-07-06 05:48:52 +0000429 TemplateType->getAsCXXRecordDecl();
430 if (SpecializationDecl != NULL) {
431 ClassDecl = SpecializationDecl;
432 } else {
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000433 ClassDecl = llvm::dyn_cast<CXXRecordDecl>(
Manuel Klimek4da21662012-07-06 05:48:52 +0000434 TemplateType->getTemplateName()
435 .getAsTemplateDecl()->getTemplatedDecl());
436 }
437 } else {
438 ClassDecl = TypeNode->getAsCXXRecordDecl();
439 }
440 assert(ClassDecl != NULL);
441 assert(ClassDecl != Declaration);
Daniel Jasper20b802d2012-07-17 07:39:27 +0000442 if (classIsDerivedFrom(ClassDecl, Base, Builder))
Manuel Klimek4da21662012-07-06 05:48:52 +0000443 return true;
Manuel Klimek4da21662012-07-06 05:48:52 +0000444 }
445 return false;
446}
447
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000448bool MatchASTVisitor::TraverseDecl(Decl *DeclNode) {
Manuel Klimek4da21662012-07-06 05:48:52 +0000449 if (DeclNode == NULL) {
450 return true;
451 }
452 match(*DeclNode);
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000453 return RecursiveASTVisitor<MatchASTVisitor>::TraverseDecl(DeclNode);
Manuel Klimek4da21662012-07-06 05:48:52 +0000454}
455
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000456bool MatchASTVisitor::TraverseStmt(Stmt *StmtNode) {
Manuel Klimek4da21662012-07-06 05:48:52 +0000457 if (StmtNode == NULL) {
458 return true;
459 }
460 match(*StmtNode);
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000461 return RecursiveASTVisitor<MatchASTVisitor>::TraverseStmt(StmtNode);
Manuel Klimek4da21662012-07-06 05:48:52 +0000462}
463
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000464bool MatchASTVisitor::TraverseType(QualType TypeNode) {
Manuel Klimek4da21662012-07-06 05:48:52 +0000465 match(TypeNode);
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000466 return RecursiveASTVisitor<MatchASTVisitor>::TraverseType(TypeNode);
Manuel Klimek4da21662012-07-06 05:48:52 +0000467}
468
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000469bool MatchASTVisitor::TraverseTypeLoc(TypeLoc TypeLoc) {
470 return RecursiveASTVisitor<MatchASTVisitor>::
Manuel Klimek4da21662012-07-06 05:48:52 +0000471 TraverseType(TypeLoc.getType());
472}
473
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000474class MatchASTConsumer : public ASTConsumer {
Manuel Klimek4da21662012-07-06 05:48:52 +0000475public:
476 MatchASTConsumer(std::vector< std::pair<const UntypedBaseMatcher*,
477 MatchFinder::MatchCallback*> > *Triggers,
478 MatchFinder::ParsingDoneTestCallback *ParsingDone)
479 : Visitor(Triggers),
480 ParsingDone(ParsingDone) {}
481
482private:
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000483 virtual void HandleTranslationUnit(ASTContext &Context) {
Manuel Klimek4da21662012-07-06 05:48:52 +0000484 if (ParsingDone != NULL) {
485 ParsingDone->run();
486 }
487 Visitor.set_active_ast_context(&Context);
488 Visitor.TraverseDecl(Context.getTranslationUnitDecl());
489 Visitor.set_active_ast_context(NULL);
490 }
491
492 MatchASTVisitor Visitor;
493 MatchFinder::ParsingDoneTestCallback *ParsingDone;
494};
495
496} // end namespace
497} // end namespace internal
498
499MatchFinder::MatchResult::MatchResult(const BoundNodes &Nodes,
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000500 ASTContext *Context)
Manuel Klimek4da21662012-07-06 05:48:52 +0000501 : Nodes(Nodes), Context(Context),
502 SourceManager(&Context->getSourceManager()) {}
503
504MatchFinder::MatchCallback::~MatchCallback() {}
505MatchFinder::ParsingDoneTestCallback::~ParsingDoneTestCallback() {}
506
507MatchFinder::MatchFinder() : ParsingDone(NULL) {}
508
509MatchFinder::~MatchFinder() {
510 for (std::vector< std::pair<const internal::UntypedBaseMatcher*,
511 MatchFinder::MatchCallback*> >::const_iterator
512 It = Triggers.begin(), End = Triggers.end();
513 It != End; ++It) {
514 delete It->first;
515 }
516}
517
518void MatchFinder::addMatcher(const DeclarationMatcher &NodeMatch,
519 MatchCallback *Action) {
520 Triggers.push_back(std::make_pair(
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000521 new internal::TypedBaseMatcher<Decl>(NodeMatch), Action));
Manuel Klimek4da21662012-07-06 05:48:52 +0000522}
523
524void MatchFinder::addMatcher(const TypeMatcher &NodeMatch,
525 MatchCallback *Action) {
526 Triggers.push_back(std::make_pair(
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000527 new internal::TypedBaseMatcher<QualType>(NodeMatch), Action));
Manuel Klimek4da21662012-07-06 05:48:52 +0000528}
529
530void MatchFinder::addMatcher(const StatementMatcher &NodeMatch,
531 MatchCallback *Action) {
532 Triggers.push_back(std::make_pair(
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000533 new internal::TypedBaseMatcher<Stmt>(NodeMatch), Action));
Manuel Klimek4da21662012-07-06 05:48:52 +0000534}
535
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000536ASTConsumer *MatchFinder::newASTConsumer() {
Manuel Klimek4da21662012-07-06 05:48:52 +0000537 return new internal::MatchASTConsumer(&Triggers, ParsingDone);
538}
539
540void MatchFinder::registerTestCallbackAfterParsing(
541 MatchFinder::ParsingDoneTestCallback *NewParsingDone) {
542 ParsingDone = NewParsingDone;
543}
544
545} // end namespace ast_matchers
546} // end namespace clang