blob: e1e5f44c07b1754bc2fd097bc7b47eb7981427b6 [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"
Manuel Klimek374516c2013-03-14 16:33:21 +000023#include <deque>
Manuel Klimek4da21662012-07-06 05:48:52 +000024#include <set>
25
26namespace clang {
27namespace ast_matchers {
28namespace internal {
29namespace {
30
Manuel Klimeka78d0d62012-09-05 12:12:07 +000031typedef MatchFinder::MatchCallback MatchCallback;
32
Manuel Klimek054d0492013-06-19 15:42:45 +000033// The maximum number of memoization entries to store.
34// 10k has been experimentally found to give a good trade-off
35// of performance vs. memory consumption by running matcher
36// that match on every statement over a very large codebase.
37//
38// FIXME: Do some performance optimization in general and
39// revisit this number; also, put up micro-benchmarks that we can
40// optimize this on.
41static const unsigned MaxMemoizationEntries = 10000;
42
Manuel Klimek4da21662012-07-06 05:48:52 +000043// We use memoization to avoid running the same matcher on the same
Manuel Klimek054d0492013-06-19 15:42:45 +000044// AST node twice. This struct is the key for looking up match
Manuel Klimek4da21662012-07-06 05:48:52 +000045// result. It consists of an ID of the MatcherInterface (for
Manuel Klimek054d0492013-06-19 15:42:45 +000046// identifying the matcher), a pointer to the AST node and the
47// bound nodes before the matcher was executed.
Manuel Klimeka78d0d62012-09-05 12:12:07 +000048//
49// We currently only memoize on nodes whose pointers identify the
50// nodes (\c Stmt and \c Decl, but not \c QualType or \c TypeLoc).
51// For \c QualType and \c TypeLoc it is possible to implement
52// generation of keys for each type.
53// FIXME: Benchmark whether memoization of non-pointer typed nodes
54// provides enough benefit for the additional amount of code.
Manuel Klimek054d0492013-06-19 15:42:45 +000055struct MatchKey {
56 uint64_t MatcherID;
57 ast_type_traits::DynTypedNode Node;
58 BoundNodesTreeBuilder BoundNodes;
59
60 bool operator<(const MatchKey &Other) const {
61 if (MatcherID != Other.MatcherID)
62 return MatcherID < Other.MatcherID;
63 if (Node != Other.Node)
64 return Node < Other.Node;
65 return BoundNodes < Other.BoundNodes;
66 }
67};
Manuel Klimek4da21662012-07-06 05:48:52 +000068
69// Used to store the result of a match and possibly bound nodes.
70struct MemoizedMatchResult {
71 bool ResultOfMatch;
Manuel Klimek054d0492013-06-19 15:42:45 +000072 BoundNodesTreeBuilder Nodes;
Manuel Klimek4da21662012-07-06 05:48:52 +000073};
74
75// A RecursiveASTVisitor that traverses all children or all descendants of
76// a node.
77class MatchChildASTVisitor
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000078 : public RecursiveASTVisitor<MatchChildASTVisitor> {
Manuel Klimek4da21662012-07-06 05:48:52 +000079public:
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000080 typedef RecursiveASTVisitor<MatchChildASTVisitor> VisitorBase;
Manuel Klimek4da21662012-07-06 05:48:52 +000081
82 // Creates an AST visitor that matches 'matcher' on all children or
83 // descendants of a traversed node. max_depth is the maximum depth
84 // to traverse: use 1 for matching the children and INT_MAX for
85 // matching the descendants.
Manuel Klimeka78d0d62012-09-05 12:12:07 +000086 MatchChildASTVisitor(const DynTypedMatcher *Matcher,
Manuel Klimek4da21662012-07-06 05:48:52 +000087 ASTMatchFinder *Finder,
88 BoundNodesTreeBuilder *Builder,
89 int MaxDepth,
90 ASTMatchFinder::TraversalKind Traversal,
91 ASTMatchFinder::BindKind Bind)
Manuel Klimeka78d0d62012-09-05 12:12:07 +000092 : Matcher(Matcher),
Manuel Klimek4da21662012-07-06 05:48:52 +000093 Finder(Finder),
94 Builder(Builder),
Daniel Jaspera267cf62012-10-29 10:14:44 +000095 CurrentDepth(0),
Manuel Klimek4da21662012-07-06 05:48:52 +000096 MaxDepth(MaxDepth),
97 Traversal(Traversal),
98 Bind(Bind),
99 Matches(false) {}
100
101 // Returns true if a match is found in the subtree rooted at the
102 // given AST node. This is done via a set of mutually recursive
103 // functions. Here's how the recursion is done (the *wildcard can
104 // actually be Decl, Stmt, or Type):
105 //
106 // - Traverse(node) calls BaseTraverse(node) when it needs
107 // to visit the descendants of node.
108 // - BaseTraverse(node) then calls (via VisitorBase::Traverse*(node))
109 // Traverse*(c) for each child c of 'node'.
110 // - Traverse*(c) in turn calls Traverse(c), completing the
111 // recursion.
Manuel Klimeka78d0d62012-09-05 12:12:07 +0000112 bool findMatch(const ast_type_traits::DynTypedNode &DynNode) {
Manuel Klimek4da21662012-07-06 05:48:52 +0000113 reset();
Manuel Klimeka78d0d62012-09-05 12:12:07 +0000114 if (const Decl *D = DynNode.get<Decl>())
115 traverse(*D);
116 else if (const Stmt *S = DynNode.get<Stmt>())
117 traverse(*S);
Daniel Jasperd1ce3c12012-10-30 15:42:00 +0000118 else if (const NestedNameSpecifier *NNS =
119 DynNode.get<NestedNameSpecifier>())
120 traverse(*NNS);
121 else if (const NestedNameSpecifierLoc *NNSLoc =
122 DynNode.get<NestedNameSpecifierLoc>())
123 traverse(*NNSLoc);
Daniel Jaspera267cf62012-10-29 10:14:44 +0000124 else if (const QualType *Q = DynNode.get<QualType>())
125 traverse(*Q);
126 else if (const TypeLoc *T = DynNode.get<TypeLoc>())
127 traverse(*T);
Manuel Klimeka78d0d62012-09-05 12:12:07 +0000128 // FIXME: Add other base types after adding tests.
Manuel Klimek054d0492013-06-19 15:42:45 +0000129
130 // It's OK to always overwrite the bound nodes, as if there was
131 // no match in this recursive branch, the result set is empty
132 // anyway.
133 *Builder = ResultBindings;
134
Manuel Klimek4da21662012-07-06 05:48:52 +0000135 return Matches;
136 }
137
138 // The following are overriding methods from the base visitor class.
139 // They are public only to allow CRTP to work. They are *not *part
140 // of the public API of this class.
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000141 bool TraverseDecl(Decl *DeclNode) {
Daniel Jaspera267cf62012-10-29 10:14:44 +0000142 ScopedIncrement ScopedDepth(&CurrentDepth);
Manuel Klimek4da21662012-07-06 05:48:52 +0000143 return (DeclNode == NULL) || traverse(*DeclNode);
144 }
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000145 bool TraverseStmt(Stmt *StmtNode) {
Daniel Jaspera267cf62012-10-29 10:14:44 +0000146 ScopedIncrement ScopedDepth(&CurrentDepth);
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000147 const Stmt *StmtToTraverse = StmtNode;
Manuel Klimek4da21662012-07-06 05:48:52 +0000148 if (Traversal ==
149 ASTMatchFinder::TK_IgnoreImplicitCastsAndParentheses) {
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000150 const Expr *ExprNode = dyn_cast_or_null<Expr>(StmtNode);
Manuel Klimek4da21662012-07-06 05:48:52 +0000151 if (ExprNode != NULL) {
152 StmtToTraverse = ExprNode->IgnoreParenImpCasts();
153 }
154 }
155 return (StmtToTraverse == NULL) || traverse(*StmtToTraverse);
156 }
Daniel Jaspera267cf62012-10-29 10:14:44 +0000157 // We assume that the QualType and the contained type are on the same
158 // hierarchy level. Thus, we try to match either of them.
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000159 bool TraverseType(QualType TypeNode) {
Daniel Jasperb55c67d2012-11-13 17:14:11 +0000160 if (TypeNode.isNull())
161 return true;
Daniel Jaspera267cf62012-10-29 10:14:44 +0000162 ScopedIncrement ScopedDepth(&CurrentDepth);
163 // Match the Type.
164 if (!match(*TypeNode))
165 return false;
166 // The QualType is matched inside traverse.
Manuel Klimek4da21662012-07-06 05:48:52 +0000167 return traverse(TypeNode);
168 }
Daniel Jaspera267cf62012-10-29 10:14:44 +0000169 // We assume that the TypeLoc, contained QualType and contained Type all are
170 // on the same hierarchy level. Thus, we try to match all of them.
171 bool TraverseTypeLoc(TypeLoc TypeLocNode) {
Daniel Jasperb55c67d2012-11-13 17:14:11 +0000172 if (TypeLocNode.isNull())
173 return true;
Daniel Jaspera267cf62012-10-29 10:14:44 +0000174 ScopedIncrement ScopedDepth(&CurrentDepth);
175 // Match the Type.
176 if (!match(*TypeLocNode.getType()))
177 return false;
178 // Match the QualType.
179 if (!match(TypeLocNode.getType()))
180 return false;
181 // The TypeLoc is matched inside traverse.
182 return traverse(TypeLocNode);
183 }
Daniel Jasperd1ce3c12012-10-30 15:42:00 +0000184 bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS) {
185 ScopedIncrement ScopedDepth(&CurrentDepth);
186 return (NNS == NULL) || traverse(*NNS);
187 }
188 bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
Daniel Jasperb55c67d2012-11-13 17:14:11 +0000189 if (!NNS)
190 return true;
Daniel Jasperd1ce3c12012-10-30 15:42:00 +0000191 ScopedIncrement ScopedDepth(&CurrentDepth);
192 if (!match(*NNS.getNestedNameSpecifier()))
193 return false;
Daniel Jasperb55c67d2012-11-13 17:14:11 +0000194 return traverse(NNS);
Daniel Jasperd1ce3c12012-10-30 15:42:00 +0000195 }
Manuel Klimek4da21662012-07-06 05:48:52 +0000196
197 bool shouldVisitTemplateInstantiations() const { return true; }
198 bool shouldVisitImplicitCode() const { return true; }
Daniel Jasper278057f2012-11-15 03:29:05 +0000199 // Disables data recursion. We intercept Traverse* methods in the RAV, which
200 // are not triggered during data recursion.
201 bool shouldUseDataRecursionFor(clang::Stmt *S) const { return false; }
Manuel Klimek4da21662012-07-06 05:48:52 +0000202
203private:
204 // Used for updating the depth during traversal.
205 struct ScopedIncrement {
206 explicit ScopedIncrement(int *Depth) : Depth(Depth) { ++(*Depth); }
207 ~ScopedIncrement() { --(*Depth); }
208
209 private:
210 int *Depth;
211 };
212
213 // Resets the state of this object.
214 void reset() {
215 Matches = false;
Daniel Jaspera267cf62012-10-29 10:14:44 +0000216 CurrentDepth = 0;
Manuel Klimek4da21662012-07-06 05:48:52 +0000217 }
218
219 // Forwards the call to the corresponding Traverse*() method in the
220 // base visitor class.
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000221 bool baseTraverse(const Decl &DeclNode) {
222 return VisitorBase::TraverseDecl(const_cast<Decl*>(&DeclNode));
Manuel Klimek4da21662012-07-06 05:48:52 +0000223 }
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000224 bool baseTraverse(const Stmt &StmtNode) {
225 return VisitorBase::TraverseStmt(const_cast<Stmt*>(&StmtNode));
Manuel Klimek4da21662012-07-06 05:48:52 +0000226 }
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000227 bool baseTraverse(QualType TypeNode) {
Manuel Klimek4da21662012-07-06 05:48:52 +0000228 return VisitorBase::TraverseType(TypeNode);
229 }
Daniel Jaspera267cf62012-10-29 10:14:44 +0000230 bool baseTraverse(TypeLoc TypeLocNode) {
231 return VisitorBase::TraverseTypeLoc(TypeLocNode);
232 }
Daniel Jasperd1ce3c12012-10-30 15:42:00 +0000233 bool baseTraverse(const NestedNameSpecifier &NNS) {
234 return VisitorBase::TraverseNestedNameSpecifier(
235 const_cast<NestedNameSpecifier*>(&NNS));
236 }
237 bool baseTraverse(NestedNameSpecifierLoc NNS) {
238 return VisitorBase::TraverseNestedNameSpecifierLoc(NNS);
239 }
Manuel Klimek4da21662012-07-06 05:48:52 +0000240
Daniel Jaspera267cf62012-10-29 10:14:44 +0000241 // Sets 'Matched' to true if 'Matcher' matches 'Node' and:
242 // 0 < CurrentDepth <= MaxDepth.
243 //
244 // Returns 'true' if traversal should continue after this function
245 // returns, i.e. if no match is found or 'Bind' is 'BK_All'.
Manuel Klimek4da21662012-07-06 05:48:52 +0000246 template <typename T>
Daniel Jaspera267cf62012-10-29 10:14:44 +0000247 bool match(const T &Node) {
248 if (CurrentDepth == 0 || CurrentDepth > MaxDepth) {
249 return true;
Manuel Klimek4da21662012-07-06 05:48:52 +0000250 }
251 if (Bind != ASTMatchFinder::BK_All) {
Manuel Klimek054d0492013-06-19 15:42:45 +0000252 BoundNodesTreeBuilder RecursiveBuilder(*Builder);
253 if (Matcher->matches(ast_type_traits::DynTypedNode::create(Node), Finder,
254 &RecursiveBuilder)) {
Manuel Klimek4da21662012-07-06 05:48:52 +0000255 Matches = true;
Manuel Klimek054d0492013-06-19 15:42:45 +0000256 ResultBindings.addMatch(RecursiveBuilder);
257 return false; // Abort as soon as a match is found.
Manuel Klimek4da21662012-07-06 05:48:52 +0000258 }
Manuel Klimek4da21662012-07-06 05:48:52 +0000259 } else {
Manuel Klimek054d0492013-06-19 15:42:45 +0000260 BoundNodesTreeBuilder RecursiveBuilder(*Builder);
261 if (Matcher->matches(ast_type_traits::DynTypedNode::create(Node), Finder,
262 &RecursiveBuilder)) {
Manuel Klimek4da21662012-07-06 05:48:52 +0000263 // After the first match the matcher succeeds.
264 Matches = true;
Manuel Klimek054d0492013-06-19 15:42:45 +0000265 ResultBindings.addMatch(RecursiveBuilder);
Manuel Klimek4da21662012-07-06 05:48:52 +0000266 }
Manuel Klimek4da21662012-07-06 05:48:52 +0000267 }
Daniel Jaspera267cf62012-10-29 10:14:44 +0000268 return true;
269 }
270
271 // Traverses the subtree rooted at 'Node'; returns true if the
272 // traversal should continue after this function returns.
273 template <typename T>
274 bool traverse(const T &Node) {
275 TOOLING_COMPILE_ASSERT(IsBaseType<T>::value,
276 traverse_can_only_be_instantiated_with_base_type);
277 if (!match(Node))
278 return false;
279 return baseTraverse(Node);
Manuel Klimek4da21662012-07-06 05:48:52 +0000280 }
281
Manuel Klimeka78d0d62012-09-05 12:12:07 +0000282 const DynTypedMatcher *const Matcher;
Manuel Klimek4da21662012-07-06 05:48:52 +0000283 ASTMatchFinder *const Finder;
284 BoundNodesTreeBuilder *const Builder;
Manuel Klimek054d0492013-06-19 15:42:45 +0000285 BoundNodesTreeBuilder ResultBindings;
Manuel Klimek4da21662012-07-06 05:48:52 +0000286 int CurrentDepth;
287 const int MaxDepth;
288 const ASTMatchFinder::TraversalKind Traversal;
289 const ASTMatchFinder::BindKind Bind;
290 bool Matches;
291};
292
293// Controls the outermost traversal of the AST and allows to match multiple
294// matchers.
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000295class MatchASTVisitor : public RecursiveASTVisitor<MatchASTVisitor>,
Manuel Klimek4da21662012-07-06 05:48:52 +0000296 public ASTMatchFinder {
297public:
Manuel Klimeka78d0d62012-09-05 12:12:07 +0000298 MatchASTVisitor(std::vector<std::pair<const internal::DynTypedMatcher*,
299 MatchCallback*> > *MatcherCallbackPairs)
300 : MatcherCallbackPairs(MatcherCallbackPairs),
Manuel Klimek4da21662012-07-06 05:48:52 +0000301 ActiveASTContext(NULL) {
302 }
303
Manuel Klimeke5793282012-11-02 01:31:03 +0000304 void onStartOfTranslationUnit() {
305 for (std::vector<std::pair<const internal::DynTypedMatcher*,
306 MatchCallback*> >::const_iterator
307 I = MatcherCallbackPairs->begin(), E = MatcherCallbackPairs->end();
308 I != E; ++I) {
309 I->second->onStartOfTranslationUnit();
310 }
311 }
312
Peter Collingbourne8f9e5902013-05-28 19:21:51 +0000313 void onEndOfTranslationUnit() {
314 for (std::vector<std::pair<const internal::DynTypedMatcher*,
315 MatchCallback*> >::const_iterator
316 I = MatcherCallbackPairs->begin(), E = MatcherCallbackPairs->end();
317 I != E; ++I) {
318 I->second->onEndOfTranslationUnit();
319 }
320 }
321
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000322 void set_active_ast_context(ASTContext *NewActiveASTContext) {
Manuel Klimek4da21662012-07-06 05:48:52 +0000323 ActiveASTContext = NewActiveASTContext;
324 }
325
326 // The following Visit*() and Traverse*() functions "override"
327 // methods in RecursiveASTVisitor.
328
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000329 bool VisitTypedefDecl(TypedefDecl *DeclNode) {
Manuel Klimek4da21662012-07-06 05:48:52 +0000330 // When we see 'typedef A B', we add name 'B' to the set of names
331 // A's canonical type maps to. This is necessary for implementing
Daniel Jasper76dafa72012-09-07 12:48:17 +0000332 // isDerivedFrom(x) properly, where x can be the name of the base
Manuel Klimek4da21662012-07-06 05:48:52 +0000333 // class or any of its aliases.
334 //
335 // In general, the is-alias-of (as defined by typedefs) relation
336 // is tree-shaped, as you can typedef a type more than once. For
337 // example,
338 //
339 // typedef A B;
340 // typedef A C;
341 // typedef C D;
342 // typedef C E;
343 //
344 // gives you
345 //
346 // A
347 // |- B
348 // `- C
349 // |- D
350 // `- E
351 //
352 // It is wrong to assume that the relation is a chain. A correct
Daniel Jasper76dafa72012-09-07 12:48:17 +0000353 // implementation of isDerivedFrom() needs to recognize that B and
Manuel Klimek4da21662012-07-06 05:48:52 +0000354 // E are aliases, even though neither is a typedef of the other.
355 // Therefore, we cannot simply walk through one typedef chain to
356 // find out whether the type name matches.
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000357 const Type *TypeNode = DeclNode->getUnderlyingType().getTypePtr();
358 const Type *CanonicalType = // root of the typedef tree
Manuel Klimek4da21662012-07-06 05:48:52 +0000359 ActiveASTContext->getCanonicalType(TypeNode);
Daniel Jasper20b802d2012-07-17 07:39:27 +0000360 TypeAliases[CanonicalType].insert(DeclNode);
Manuel Klimek4da21662012-07-06 05:48:52 +0000361 return true;
362 }
363
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000364 bool TraverseDecl(Decl *DeclNode);
365 bool TraverseStmt(Stmt *StmtNode);
366 bool TraverseType(QualType TypeNode);
367 bool TraverseTypeLoc(TypeLoc TypeNode);
Daniel Jaspera7564432012-09-13 13:11:25 +0000368 bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS);
369 bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS);
Manuel Klimek4da21662012-07-06 05:48:52 +0000370
371 // Matches children or descendants of 'Node' with 'BaseMatcher'.
Manuel Klimeka78d0d62012-09-05 12:12:07 +0000372 bool memoizedMatchesRecursively(const ast_type_traits::DynTypedNode &Node,
373 const DynTypedMatcher &Matcher,
Manuel Klimek4da21662012-07-06 05:48:52 +0000374 BoundNodesTreeBuilder *Builder, int MaxDepth,
375 TraversalKind Traversal, BindKind Bind) {
Manuel Klimek054d0492013-06-19 15:42:45 +0000376 MatchKey Key;
377 Key.MatcherID = Matcher.getID();
378 Key.Node = Node;
379 // Note that we key on the bindings *before* the match.
380 Key.BoundNodes = *Builder;
Daniel Jaspera267cf62012-10-29 10:14:44 +0000381
382 // For AST-nodes that don't have an identity, we can't memoize.
Manuel Klimek054d0492013-06-19 15:42:45 +0000383 if (!Node.getMemoizationData())
Daniel Jaspera267cf62012-10-29 10:14:44 +0000384 return matchesRecursively(Node, Matcher, Builder, MaxDepth, Traversal,
385 Bind);
386
Manuel Klimek054d0492013-06-19 15:42:45 +0000387 std::pair<MemoizationMap::iterator, bool> InsertResult =
388 ResultCache.insert(std::make_pair(Key, MemoizedMatchResult()));
Manuel Klimek4da21662012-07-06 05:48:52 +0000389 if (InsertResult.second) {
Manuel Klimek054d0492013-06-19 15:42:45 +0000390 InsertResult.first->second.Nodes = *Builder;
Manuel Klimek4da21662012-07-06 05:48:52 +0000391 InsertResult.first->second.ResultOfMatch =
Manuel Klimek054d0492013-06-19 15:42:45 +0000392 matchesRecursively(Node, Matcher, &InsertResult.first->second.Nodes,
393 MaxDepth, Traversal, Bind);
Manuel Klimek4da21662012-07-06 05:48:52 +0000394 }
Manuel Klimek054d0492013-06-19 15:42:45 +0000395 *Builder = InsertResult.first->second.Nodes;
Manuel Klimek4da21662012-07-06 05:48:52 +0000396 return InsertResult.first->second.ResultOfMatch;
397 }
398
399 // Matches children or descendants of 'Node' with 'BaseMatcher'.
Manuel Klimeka78d0d62012-09-05 12:12:07 +0000400 bool matchesRecursively(const ast_type_traits::DynTypedNode &Node,
401 const DynTypedMatcher &Matcher,
Manuel Klimek4da21662012-07-06 05:48:52 +0000402 BoundNodesTreeBuilder *Builder, int MaxDepth,
403 TraversalKind Traversal, BindKind Bind) {
404 MatchChildASTVisitor Visitor(
Manuel Klimeka78d0d62012-09-05 12:12:07 +0000405 &Matcher, this, Builder, MaxDepth, Traversal, Bind);
Manuel Klimek4da21662012-07-06 05:48:52 +0000406 return Visitor.findMatch(Node);
407 }
408
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000409 virtual bool classIsDerivedFrom(const CXXRecordDecl *Declaration,
Daniel Jasper20b802d2012-07-17 07:39:27 +0000410 const Matcher<NamedDecl> &Base,
411 BoundNodesTreeBuilder *Builder);
Manuel Klimek4da21662012-07-06 05:48:52 +0000412
Daniel Jasperc99a3ad2012-10-22 16:26:51 +0000413 // Implements ASTMatchFinder::matchesChildOf.
Manuel Klimeka78d0d62012-09-05 12:12:07 +0000414 virtual bool matchesChildOf(const ast_type_traits::DynTypedNode &Node,
415 const DynTypedMatcher &Matcher,
Manuel Klimek4da21662012-07-06 05:48:52 +0000416 BoundNodesTreeBuilder *Builder,
417 TraversalKind Traversal,
418 BindKind Bind) {
Manuel Klimeka78d0d62012-09-05 12:12:07 +0000419 return matchesRecursively(Node, Matcher, Builder, 1, Traversal,
Manuel Klimek4da21662012-07-06 05:48:52 +0000420 Bind);
421 }
Daniel Jasperc99a3ad2012-10-22 16:26:51 +0000422 // Implements ASTMatchFinder::matchesDescendantOf.
Manuel Klimeka78d0d62012-09-05 12:12:07 +0000423 virtual bool matchesDescendantOf(const ast_type_traits::DynTypedNode &Node,
424 const DynTypedMatcher &Matcher,
Manuel Klimek4da21662012-07-06 05:48:52 +0000425 BoundNodesTreeBuilder *Builder,
426 BindKind Bind) {
Manuel Klimek4d50d252013-07-08 14:16:30 +0000427 if (ResultCache.size() > MaxMemoizationEntries)
428 ResultCache.clear();
Manuel Klimeka78d0d62012-09-05 12:12:07 +0000429 return memoizedMatchesRecursively(Node, Matcher, Builder, INT_MAX,
Manuel Klimek4da21662012-07-06 05:48:52 +0000430 TK_AsIs, Bind);
431 }
Manuel Klimek579b1202012-09-07 09:26:10 +0000432 // Implements ASTMatchFinder::matchesAncestorOf.
433 virtual bool matchesAncestorOf(const ast_type_traits::DynTypedNode &Node,
434 const DynTypedMatcher &Matcher,
Daniel Jasperc99a3ad2012-10-22 16:26:51 +0000435 BoundNodesTreeBuilder *Builder,
436 AncestorMatchMode MatchMode) {
Manuel Klimek4d50d252013-07-08 14:16:30 +0000437 // Reset the cache outside of the recursive call to make sure we
438 // don't invalidate any iterators.
439 if (ResultCache.size() > MaxMemoizationEntries)
440 ResultCache.clear();
Manuel Klimek374516c2013-03-14 16:33:21 +0000441 return memoizedMatchesAncestorOfRecursively(Node, Matcher, Builder,
442 MatchMode);
Manuel Klimek579b1202012-09-07 09:26:10 +0000443 }
Manuel Klimek4da21662012-07-06 05:48:52 +0000444
Manuel Klimek60969f52013-02-01 13:41:35 +0000445 // Matches all registered matchers on the given node and calls the
446 // result callback for every node that matches.
447 void match(const ast_type_traits::DynTypedNode& Node) {
448 for (std::vector<std::pair<const internal::DynTypedMatcher*,
449 MatchCallback*> >::const_iterator
450 I = MatcherCallbackPairs->begin(), E = MatcherCallbackPairs->end();
451 I != E; ++I) {
452 BoundNodesTreeBuilder Builder;
453 if (I->first->matches(Node, this, &Builder)) {
Manuel Klimek60969f52013-02-01 13:41:35 +0000454 MatchVisitor Visitor(ActiveASTContext, I->second);
Manuel Klimek054d0492013-06-19 15:42:45 +0000455 Builder.visitMatches(&Visitor);
Manuel Klimek60969f52013-02-01 13:41:35 +0000456 }
457 }
458 }
459
460 template <typename T> void match(const T &Node) {
461 match(ast_type_traits::DynTypedNode::create(Node));
462 }
463
Manuel Klimek7f2d4802012-11-30 13:45:19 +0000464 // Implements ASTMatchFinder::getASTContext.
465 virtual ASTContext &getASTContext() const { return *ActiveASTContext; }
466
Manuel Klimek4da21662012-07-06 05:48:52 +0000467 bool shouldVisitTemplateInstantiations() const { return true; }
468 bool shouldVisitImplicitCode() const { return true; }
Daniel Jasper278057f2012-11-15 03:29:05 +0000469 // Disables data recursion. We intercept Traverse* methods in the RAV, which
470 // are not triggered during data recursion.
471 bool shouldUseDataRecursionFor(clang::Stmt *S) const { return false; }
Manuel Klimek4da21662012-07-06 05:48:52 +0000472
473private:
Manuel Klimek374516c2013-03-14 16:33:21 +0000474 // Returns whether an ancestor of \p Node matches \p Matcher.
475 //
476 // The order of matching ((which can lead to different nodes being bound in
477 // case there are multiple matches) is breadth first search.
478 //
479 // To allow memoization in the very common case of having deeply nested
480 // expressions inside a template function, we first walk up the AST, memoizing
481 // the result of the match along the way, as long as there is only a single
482 // parent.
483 //
484 // Once there are multiple parents, the breadth first search order does not
485 // allow simple memoization on the ancestors. Thus, we only memoize as long
486 // as there is a single parent.
487 bool memoizedMatchesAncestorOfRecursively(
Manuel Klimek30ace372012-12-06 14:42:48 +0000488 const ast_type_traits::DynTypedNode &Node, const DynTypedMatcher &Matcher,
489 BoundNodesTreeBuilder *Builder, AncestorMatchMode MatchMode) {
490 if (Node.get<TranslationUnitDecl>() ==
491 ActiveASTContext->getTranslationUnitDecl())
492 return false;
493 assert(Node.getMemoizationData() &&
494 "Invariant broken: only nodes that support memoization may be "
495 "used in the parent map.");
Manuel Klimekff9a0102013-02-28 13:21:39 +0000496 ASTContext::ParentVector Parents = ActiveASTContext->getParents(Node);
497 if (Parents.empty()) {
Manuel Klimek30ace372012-12-06 14:42:48 +0000498 assert(false && "Found node that is not in the parent map.");
499 return false;
500 }
Manuel Klimek054d0492013-06-19 15:42:45 +0000501 MatchKey Key;
502 Key.MatcherID = Matcher.getID();
503 Key.Node = Node;
504 Key.BoundNodes = *Builder;
Manuel Klimek054d0492013-06-19 15:42:45 +0000505 std::pair<MemoizationMap::iterator, bool> InsertResult =
506 ResultCache.insert(std::make_pair(Key, MemoizedMatchResult()));
507 if (InsertResult.second) {
Manuel Klimek374516c2013-03-14 16:33:21 +0000508 bool Matches = false;
509 if (Parents.size() == 1) {
510 // Only one parent - do recursive memoization.
511 const ast_type_traits::DynTypedNode Parent = Parents[0];
Manuel Klimek054d0492013-06-19 15:42:45 +0000512 BoundNodesTreeBuilder Result(*Builder);
513 if (Matcher.matches(Parent, this, &Result)) {
514 InsertResult.first->second.Nodes = Result;
Manuel Klimek374516c2013-03-14 16:33:21 +0000515 Matches = true;
516 } else if (MatchMode != ASTMatchFinder::AMM_ParentOnly) {
Manuel Klimek054d0492013-06-19 15:42:45 +0000517 Matches = memoizedMatchesAncestorOfRecursively(Parent, Matcher,
518 Builder, MatchMode);
519 // Once we get back from the recursive call, the result will be the
520 // same as the parent's result.
521 InsertResult.first->second.Nodes = *Builder;
Manuel Klimek374516c2013-03-14 16:33:21 +0000522 }
523 } else {
524 // Multiple parents - BFS over the rest of the nodes.
525 llvm::DenseSet<const void *> Visited;
526 std::deque<ast_type_traits::DynTypedNode> Queue(Parents.begin(),
527 Parents.end());
528 while (!Queue.empty()) {
Manuel Klimek054d0492013-06-19 15:42:45 +0000529 BoundNodesTreeBuilder Result(*Builder);
530 if (Matcher.matches(Queue.front(), this, &Result)) {
531 InsertResult.first->second.Nodes = Result;
Manuel Klimek374516c2013-03-14 16:33:21 +0000532 Matches = true;
533 break;
534 }
535 if (MatchMode != ASTMatchFinder::AMM_ParentOnly) {
536 ASTContext::ParentVector Ancestors =
537 ActiveASTContext->getParents(Queue.front());
538 for (ASTContext::ParentVector::const_iterator I = Ancestors.begin(),
539 E = Ancestors.end();
540 I != E; ++I) {
541 // Make sure we do not visit the same node twice.
542 // Otherwise, we'll visit the common ancestors as often as there
543 // are splits on the way down.
544 if (Visited.insert(I->getMemoizationData()).second)
545 Queue.push_back(*I);
546 }
547 }
548 Queue.pop_front();
549 }
550 }
Manuel Klimek30ace372012-12-06 14:42:48 +0000551
Manuel Klimek054d0492013-06-19 15:42:45 +0000552 InsertResult.first->second.ResultOfMatch = Matches;
Manuel Klimek374516c2013-03-14 16:33:21 +0000553 }
Manuel Klimek054d0492013-06-19 15:42:45 +0000554 *Builder = InsertResult.first->second.Nodes;
555 return InsertResult.first->second.ResultOfMatch;
Manuel Klimek374516c2013-03-14 16:33:21 +0000556 }
Manuel Klimek30ace372012-12-06 14:42:48 +0000557
Manuel Klimek4da21662012-07-06 05:48:52 +0000558 // Implements a BoundNodesTree::Visitor that calls a MatchCallback with
559 // the aggregated bound nodes for each match.
Manuel Klimek054d0492013-06-19 15:42:45 +0000560 class MatchVisitor : public BoundNodesTreeBuilder::Visitor {
Manuel Klimek4da21662012-07-06 05:48:52 +0000561 public:
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000562 MatchVisitor(ASTContext* Context,
Manuel Klimek4da21662012-07-06 05:48:52 +0000563 MatchFinder::MatchCallback* Callback)
564 : Context(Context),
565 Callback(Callback) {}
566
567 virtual void visitMatch(const BoundNodes& BoundNodesView) {
568 Callback->run(MatchFinder::MatchResult(BoundNodesView, Context));
569 }
570
571 private:
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000572 ASTContext* Context;
Manuel Klimek4da21662012-07-06 05:48:52 +0000573 MatchFinder::MatchCallback* Callback;
574 };
575
Daniel Jasper20b802d2012-07-17 07:39:27 +0000576 // Returns true if 'TypeNode' has an alias that matches the given matcher.
577 bool typeHasMatchingAlias(const Type *TypeNode,
578 const Matcher<NamedDecl> Matcher,
579 BoundNodesTreeBuilder *Builder) {
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000580 const Type *const CanonicalType =
Manuel Klimek4da21662012-07-06 05:48:52 +0000581 ActiveASTContext->getCanonicalType(TypeNode);
Daniel Jasper20b802d2012-07-17 07:39:27 +0000582 const std::set<const TypedefDecl*> &Aliases = TypeAliases[CanonicalType];
583 for (std::set<const TypedefDecl*>::const_iterator
584 It = Aliases.begin(), End = Aliases.end();
585 It != End; ++It) {
Manuel Klimek054d0492013-06-19 15:42:45 +0000586 BoundNodesTreeBuilder Result(*Builder);
587 if (Matcher.matches(**It, this, &Result)) {
588 *Builder = Result;
Daniel Jasper20b802d2012-07-17 07:39:27 +0000589 return true;
Manuel Klimek054d0492013-06-19 15:42:45 +0000590 }
Daniel Jasper20b802d2012-07-17 07:39:27 +0000591 }
592 return false;
Manuel Klimek4da21662012-07-06 05:48:52 +0000593 }
594
Manuel Klimeka78d0d62012-09-05 12:12:07 +0000595 std::vector<std::pair<const internal::DynTypedMatcher*,
596 MatchCallback*> > *const MatcherCallbackPairs;
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000597 ASTContext *ActiveASTContext;
Manuel Klimek4da21662012-07-06 05:48:52 +0000598
Daniel Jasper20b802d2012-07-17 07:39:27 +0000599 // Maps a canonical type to its TypedefDecls.
600 llvm::DenseMap<const Type*, std::set<const TypedefDecl*> > TypeAliases;
Manuel Klimek4da21662012-07-06 05:48:52 +0000601
602 // Maps (matcher, node) -> the match result for memoization.
Manuel Klimek054d0492013-06-19 15:42:45 +0000603 typedef std::map<MatchKey, MemoizedMatchResult> MemoizationMap;
Manuel Klimek4da21662012-07-06 05:48:52 +0000604 MemoizationMap ResultCache;
605};
606
607// Returns true if the given class is directly or indirectly derived
Daniel Jasper76dafa72012-09-07 12:48:17 +0000608// from a base type with the given name. A class is not considered to be
609// derived from itself.
Daniel Jasper20b802d2012-07-17 07:39:27 +0000610bool MatchASTVisitor::classIsDerivedFrom(const CXXRecordDecl *Declaration,
611 const Matcher<NamedDecl> &Base,
612 BoundNodesTreeBuilder *Builder) {
Daniel Jasper20b802d2012-07-17 07:39:27 +0000613 if (!Declaration->hasDefinition())
Manuel Klimek4da21662012-07-06 05:48:52 +0000614 return false;
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000615 typedef CXXRecordDecl::base_class_const_iterator BaseIterator;
Manuel Klimek4da21662012-07-06 05:48:52 +0000616 for (BaseIterator It = Declaration->bases_begin(),
617 End = Declaration->bases_end(); It != End; ++It) {
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000618 const Type *TypeNode = It->getType().getTypePtr();
Manuel Klimek4da21662012-07-06 05:48:52 +0000619
Daniel Jasper20b802d2012-07-17 07:39:27 +0000620 if (typeHasMatchingAlias(TypeNode, Base, Builder))
Manuel Klimek4da21662012-07-06 05:48:52 +0000621 return true;
622
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000623 // Type::getAs<...>() drills through typedefs.
624 if (TypeNode->getAs<DependentNameType>() != NULL ||
Daniel Jasper08f0c532012-09-18 14:17:42 +0000625 TypeNode->getAs<DependentTemplateSpecializationType>() != NULL ||
Daniel Jasper20b802d2012-07-17 07:39:27 +0000626 TypeNode->getAs<TemplateTypeParmType>() != NULL)
Manuel Klimek4da21662012-07-06 05:48:52 +0000627 // Dependent names and template TypeNode parameters will be matched when
628 // the template is instantiated.
629 continue;
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000630 CXXRecordDecl *ClassDecl = NULL;
631 TemplateSpecializationType const *TemplateType =
632 TypeNode->getAs<TemplateSpecializationType>();
Manuel Klimek4da21662012-07-06 05:48:52 +0000633 if (TemplateType != NULL) {
Daniel Jasper20b802d2012-07-17 07:39:27 +0000634 if (TemplateType->getTemplateName().isDependent())
Manuel Klimek4da21662012-07-06 05:48:52 +0000635 // Dependent template specializations will be matched when the
636 // template is instantiated.
637 continue;
Daniel Jasper20b802d2012-07-17 07:39:27 +0000638
Manuel Klimek4da21662012-07-06 05:48:52 +0000639 // For template specialization types which are specializing a template
640 // declaration which is an explicit or partial specialization of another
641 // template declaration, getAsCXXRecordDecl() returns the corresponding
642 // ClassTemplateSpecializationDecl.
643 //
644 // For template specialization types which are specializing a template
645 // declaration which is neither an explicit nor partial specialization of
646 // another template declaration, getAsCXXRecordDecl() returns NULL and
647 // we get the CXXRecordDecl of the templated declaration.
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000648 CXXRecordDecl *SpecializationDecl =
Manuel Klimek4da21662012-07-06 05:48:52 +0000649 TemplateType->getAsCXXRecordDecl();
650 if (SpecializationDecl != NULL) {
651 ClassDecl = SpecializationDecl;
652 } else {
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000653 ClassDecl = dyn_cast<CXXRecordDecl>(
Manuel Klimek4da21662012-07-06 05:48:52 +0000654 TemplateType->getTemplateName()
655 .getAsTemplateDecl()->getTemplatedDecl());
656 }
657 } else {
658 ClassDecl = TypeNode->getAsCXXRecordDecl();
659 }
660 assert(ClassDecl != NULL);
Manuel Klimek987c2f52012-12-04 13:40:29 +0000661 if (ClassDecl == Declaration) {
662 // This can happen for recursive template definitions; if the
663 // current declaration did not match, we can safely return false.
664 assert(TemplateType);
665 return false;
666 }
Manuel Klimek054d0492013-06-19 15:42:45 +0000667 BoundNodesTreeBuilder Result(*Builder);
668 if (Base.matches(*ClassDecl, this, &Result)) {
669 *Builder = Result;
Daniel Jasper76dafa72012-09-07 12:48:17 +0000670 return true;
Manuel Klimek054d0492013-06-19 15:42:45 +0000671 }
Daniel Jasper20b802d2012-07-17 07:39:27 +0000672 if (classIsDerivedFrom(ClassDecl, Base, Builder))
Manuel Klimek4da21662012-07-06 05:48:52 +0000673 return true;
Manuel Klimek4da21662012-07-06 05:48:52 +0000674 }
675 return false;
676}
677
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000678bool MatchASTVisitor::TraverseDecl(Decl *DeclNode) {
Manuel Klimek4da21662012-07-06 05:48:52 +0000679 if (DeclNode == NULL) {
680 return true;
681 }
682 match(*DeclNode);
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000683 return RecursiveASTVisitor<MatchASTVisitor>::TraverseDecl(DeclNode);
Manuel Klimek4da21662012-07-06 05:48:52 +0000684}
685
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000686bool MatchASTVisitor::TraverseStmt(Stmt *StmtNode) {
Manuel Klimek4da21662012-07-06 05:48:52 +0000687 if (StmtNode == NULL) {
688 return true;
689 }
690 match(*StmtNode);
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000691 return RecursiveASTVisitor<MatchASTVisitor>::TraverseStmt(StmtNode);
Manuel Klimek4da21662012-07-06 05:48:52 +0000692}
693
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000694bool MatchASTVisitor::TraverseType(QualType TypeNode) {
Manuel Klimek4da21662012-07-06 05:48:52 +0000695 match(TypeNode);
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000696 return RecursiveASTVisitor<MatchASTVisitor>::TraverseType(TypeNode);
Manuel Klimek4da21662012-07-06 05:48:52 +0000697}
698
Daniel Jasperce620072012-10-17 08:52:59 +0000699bool MatchASTVisitor::TraverseTypeLoc(TypeLoc TypeLocNode) {
700 // The RecursiveASTVisitor only visits types if they're not within TypeLocs.
701 // We still want to find those types via matchers, so we match them here. Note
702 // that the TypeLocs are structurally a shadow-hierarchy to the expressed
703 // type, so we visit all involved parts of a compound type when matching on
704 // each TypeLoc.
705 match(TypeLocNode);
706 match(TypeLocNode.getType());
707 return RecursiveASTVisitor<MatchASTVisitor>::TraverseTypeLoc(TypeLocNode);
Manuel Klimek4da21662012-07-06 05:48:52 +0000708}
709
Daniel Jaspera7564432012-09-13 13:11:25 +0000710bool MatchASTVisitor::TraverseNestedNameSpecifier(NestedNameSpecifier *NNS) {
711 match(*NNS);
712 return RecursiveASTVisitor<MatchASTVisitor>::TraverseNestedNameSpecifier(NNS);
713}
714
715bool MatchASTVisitor::TraverseNestedNameSpecifierLoc(
716 NestedNameSpecifierLoc NNS) {
717 match(NNS);
718 // We only match the nested name specifier here (as opposed to traversing it)
719 // because the traversal is already done in the parallel "Loc"-hierarchy.
720 match(*NNS.getNestedNameSpecifier());
721 return
722 RecursiveASTVisitor<MatchASTVisitor>::TraverseNestedNameSpecifierLoc(NNS);
723}
724
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000725class MatchASTConsumer : public ASTConsumer {
Manuel Klimek4da21662012-07-06 05:48:52 +0000726public:
Manuel Klimeka78d0d62012-09-05 12:12:07 +0000727 MatchASTConsumer(
728 std::vector<std::pair<const internal::DynTypedMatcher*,
729 MatchCallback*> > *MatcherCallbackPairs,
730 MatchFinder::ParsingDoneTestCallback *ParsingDone)
731 : Visitor(MatcherCallbackPairs),
732 ParsingDone(ParsingDone) {}
Manuel Klimek4da21662012-07-06 05:48:52 +0000733
734private:
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000735 virtual void HandleTranslationUnit(ASTContext &Context) {
Manuel Klimek4da21662012-07-06 05:48:52 +0000736 if (ParsingDone != NULL) {
737 ParsingDone->run();
738 }
739 Visitor.set_active_ast_context(&Context);
Manuel Klimeke5793282012-11-02 01:31:03 +0000740 Visitor.onStartOfTranslationUnit();
Manuel Klimek4da21662012-07-06 05:48:52 +0000741 Visitor.TraverseDecl(Context.getTranslationUnitDecl());
Peter Collingbourne8f9e5902013-05-28 19:21:51 +0000742 Visitor.onEndOfTranslationUnit();
Manuel Klimek4da21662012-07-06 05:48:52 +0000743 Visitor.set_active_ast_context(NULL);
744 }
745
746 MatchASTVisitor Visitor;
747 MatchFinder::ParsingDoneTestCallback *ParsingDone;
748};
749
750} // end namespace
751} // end namespace internal
752
753MatchFinder::MatchResult::MatchResult(const BoundNodes &Nodes,
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000754 ASTContext *Context)
Manuel Klimek4da21662012-07-06 05:48:52 +0000755 : Nodes(Nodes), Context(Context),
756 SourceManager(&Context->getSourceManager()) {}
757
758MatchFinder::MatchCallback::~MatchCallback() {}
759MatchFinder::ParsingDoneTestCallback::~ParsingDoneTestCallback() {}
760
761MatchFinder::MatchFinder() : ParsingDone(NULL) {}
762
763MatchFinder::~MatchFinder() {
Manuel Klimeka78d0d62012-09-05 12:12:07 +0000764 for (std::vector<std::pair<const internal::DynTypedMatcher*,
765 MatchCallback*> >::const_iterator
766 It = MatcherCallbackPairs.begin(), End = MatcherCallbackPairs.end();
Manuel Klimek4da21662012-07-06 05:48:52 +0000767 It != End; ++It) {
768 delete It->first;
769 }
770}
771
772void MatchFinder::addMatcher(const DeclarationMatcher &NodeMatch,
773 MatchCallback *Action) {
Manuel Klimeka78d0d62012-09-05 12:12:07 +0000774 MatcherCallbackPairs.push_back(std::make_pair(
775 new internal::Matcher<Decl>(NodeMatch), Action));
Manuel Klimek4da21662012-07-06 05:48:52 +0000776}
777
778void MatchFinder::addMatcher(const TypeMatcher &NodeMatch,
779 MatchCallback *Action) {
Manuel Klimeka78d0d62012-09-05 12:12:07 +0000780 MatcherCallbackPairs.push_back(std::make_pair(
781 new internal::Matcher<QualType>(NodeMatch), Action));
Manuel Klimek4da21662012-07-06 05:48:52 +0000782}
783
784void MatchFinder::addMatcher(const StatementMatcher &NodeMatch,
785 MatchCallback *Action) {
Manuel Klimeka78d0d62012-09-05 12:12:07 +0000786 MatcherCallbackPairs.push_back(std::make_pair(
787 new internal::Matcher<Stmt>(NodeMatch), Action));
Manuel Klimek4da21662012-07-06 05:48:52 +0000788}
789
Daniel Jaspera7564432012-09-13 13:11:25 +0000790void MatchFinder::addMatcher(const NestedNameSpecifierMatcher &NodeMatch,
791 MatchCallback *Action) {
792 MatcherCallbackPairs.push_back(std::make_pair(
793 new NestedNameSpecifierMatcher(NodeMatch), Action));
794}
795
796void MatchFinder::addMatcher(const NestedNameSpecifierLocMatcher &NodeMatch,
797 MatchCallback *Action) {
798 MatcherCallbackPairs.push_back(std::make_pair(
799 new NestedNameSpecifierLocMatcher(NodeMatch), Action));
800}
801
Daniel Jasperce620072012-10-17 08:52:59 +0000802void MatchFinder::addMatcher(const TypeLocMatcher &NodeMatch,
803 MatchCallback *Action) {
804 MatcherCallbackPairs.push_back(std::make_pair(
805 new TypeLocMatcher(NodeMatch), Action));
806}
807
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000808ASTConsumer *MatchFinder::newASTConsumer() {
Manuel Klimeka78d0d62012-09-05 12:12:07 +0000809 return new internal::MatchASTConsumer(&MatcherCallbackPairs, ParsingDone);
Manuel Klimek4da21662012-07-06 05:48:52 +0000810}
811
Manuel Klimek60969f52013-02-01 13:41:35 +0000812void MatchFinder::match(const clang::ast_type_traits::DynTypedNode &Node,
813 ASTContext &Context) {
Manuel Klimek3e2aa992012-10-24 14:47:44 +0000814 internal::MatchASTVisitor Visitor(&MatcherCallbackPairs);
815 Visitor.set_active_ast_context(&Context);
Manuel Klimek60969f52013-02-01 13:41:35 +0000816 Visitor.match(Node);
Manuel Klimek3e2aa992012-10-24 14:47:44 +0000817}
818
Manuel Klimek4da21662012-07-06 05:48:52 +0000819void MatchFinder::registerTestCallbackAfterParsing(
820 MatchFinder::ParsingDoneTestCallback *NewParsingDone) {
821 ParsingDone = NewParsingDone;
822}
823
824} // end namespace ast_matchers
825} // end namespace clang