blob: 8f28c385b928f0ebe9b17c2652528296d9a33c3c [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 Klimeka78d0d62012-09-05 12:12:07 +000030typedef MatchFinder::MatchCallback MatchCallback;
31
Manuel Klimek4da21662012-07-06 05:48:52 +000032// We use memoization to avoid running the same matcher on the same
33// AST node twice. This pair is the key for looking up match
34// result. It consists of an ID of the MatcherInterface (for
35// identifying the matcher) and a pointer to the AST node.
Manuel Klimeka78d0d62012-09-05 12:12:07 +000036//
37// We currently only memoize on nodes whose pointers identify the
38// nodes (\c Stmt and \c Decl, but not \c QualType or \c TypeLoc).
39// For \c QualType and \c TypeLoc it is possible to implement
40// generation of keys for each type.
41// FIXME: Benchmark whether memoization of non-pointer typed nodes
42// provides enough benefit for the additional amount of code.
Manuel Klimek4da21662012-07-06 05:48:52 +000043typedef std::pair<uint64_t, const void*> UntypedMatchInput;
44
45// Used to store the result of a match and possibly bound nodes.
46struct MemoizedMatchResult {
47 bool ResultOfMatch;
48 BoundNodesTree Nodes;
49};
50
51// A RecursiveASTVisitor that traverses all children or all descendants of
52// a node.
53class MatchChildASTVisitor
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000054 : public RecursiveASTVisitor<MatchChildASTVisitor> {
Manuel Klimek4da21662012-07-06 05:48:52 +000055public:
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000056 typedef RecursiveASTVisitor<MatchChildASTVisitor> VisitorBase;
Manuel Klimek4da21662012-07-06 05:48:52 +000057
58 // Creates an AST visitor that matches 'matcher' on all children or
59 // descendants of a traversed node. max_depth is the maximum depth
60 // to traverse: use 1 for matching the children and INT_MAX for
61 // matching the descendants.
Manuel Klimeka78d0d62012-09-05 12:12:07 +000062 MatchChildASTVisitor(const DynTypedMatcher *Matcher,
Manuel Klimek4da21662012-07-06 05:48:52 +000063 ASTMatchFinder *Finder,
64 BoundNodesTreeBuilder *Builder,
65 int MaxDepth,
66 ASTMatchFinder::TraversalKind Traversal,
67 ASTMatchFinder::BindKind Bind)
Manuel Klimeka78d0d62012-09-05 12:12:07 +000068 : Matcher(Matcher),
Manuel Klimek4da21662012-07-06 05:48:52 +000069 Finder(Finder),
70 Builder(Builder),
71 CurrentDepth(-1),
72 MaxDepth(MaxDepth),
73 Traversal(Traversal),
74 Bind(Bind),
75 Matches(false) {}
76
77 // Returns true if a match is found in the subtree rooted at the
78 // given AST node. This is done via a set of mutually recursive
79 // functions. Here's how the recursion is done (the *wildcard can
80 // actually be Decl, Stmt, or Type):
81 //
82 // - Traverse(node) calls BaseTraverse(node) when it needs
83 // to visit the descendants of node.
84 // - BaseTraverse(node) then calls (via VisitorBase::Traverse*(node))
85 // Traverse*(c) for each child c of 'node'.
86 // - Traverse*(c) in turn calls Traverse(c), completing the
87 // recursion.
Manuel Klimeka78d0d62012-09-05 12:12:07 +000088 bool findMatch(const ast_type_traits::DynTypedNode &DynNode) {
Manuel Klimek4da21662012-07-06 05:48:52 +000089 reset();
Manuel Klimeka78d0d62012-09-05 12:12:07 +000090 if (const Decl *D = DynNode.get<Decl>())
91 traverse(*D);
92 else if (const Stmt *S = DynNode.get<Stmt>())
93 traverse(*S);
94 // FIXME: Add other base types after adding tests.
Manuel Klimek4da21662012-07-06 05:48:52 +000095 return Matches;
96 }
97
98 // The following are overriding methods from the base visitor class.
99 // They are public only to allow CRTP to work. They are *not *part
100 // of the public API of this class.
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000101 bool TraverseDecl(Decl *DeclNode) {
Manuel Klimek4da21662012-07-06 05:48:52 +0000102 return (DeclNode == NULL) || traverse(*DeclNode);
103 }
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000104 bool TraverseStmt(Stmt *StmtNode) {
105 const Stmt *StmtToTraverse = StmtNode;
Manuel Klimek4da21662012-07-06 05:48:52 +0000106 if (Traversal ==
107 ASTMatchFinder::TK_IgnoreImplicitCastsAndParentheses) {
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000108 const Expr *ExprNode = dyn_cast_or_null<Expr>(StmtNode);
Manuel Klimek4da21662012-07-06 05:48:52 +0000109 if (ExprNode != NULL) {
110 StmtToTraverse = ExprNode->IgnoreParenImpCasts();
111 }
112 }
113 return (StmtToTraverse == NULL) || traverse(*StmtToTraverse);
114 }
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000115 bool TraverseType(QualType TypeNode) {
Manuel Klimek4da21662012-07-06 05:48:52 +0000116 return traverse(TypeNode);
117 }
118
119 bool shouldVisitTemplateInstantiations() const { return true; }
120 bool shouldVisitImplicitCode() const { return true; }
121
122private:
123 // Used for updating the depth during traversal.
124 struct ScopedIncrement {
125 explicit ScopedIncrement(int *Depth) : Depth(Depth) { ++(*Depth); }
126 ~ScopedIncrement() { --(*Depth); }
127
128 private:
129 int *Depth;
130 };
131
132 // Resets the state of this object.
133 void reset() {
134 Matches = false;
135 CurrentDepth = -1;
136 }
137
138 // Forwards the call to the corresponding Traverse*() method in the
139 // base visitor class.
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000140 bool baseTraverse(const Decl &DeclNode) {
141 return VisitorBase::TraverseDecl(const_cast<Decl*>(&DeclNode));
Manuel Klimek4da21662012-07-06 05:48:52 +0000142 }
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000143 bool baseTraverse(const Stmt &StmtNode) {
144 return VisitorBase::TraverseStmt(const_cast<Stmt*>(&StmtNode));
Manuel Klimek4da21662012-07-06 05:48:52 +0000145 }
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000146 bool baseTraverse(QualType TypeNode) {
Manuel Klimek4da21662012-07-06 05:48:52 +0000147 return VisitorBase::TraverseType(TypeNode);
148 }
149
150 // Traverses the subtree rooted at 'node'; returns true if the
151 // traversal should continue after this function returns; also sets
152 // matched_ to true if a match is found during the traversal.
153 template <typename T>
154 bool traverse(const T &Node) {
155 TOOLING_COMPILE_ASSERT(IsBaseType<T>::value,
156 traverse_can_only_be_instantiated_with_base_type);
157 ScopedIncrement ScopedDepth(&CurrentDepth);
158 if (CurrentDepth == 0) {
159 // We don't want to match the root node, so just recurse.
160 return baseTraverse(Node);
161 }
162 if (Bind != ASTMatchFinder::BK_All) {
Manuel Klimeka78d0d62012-09-05 12:12:07 +0000163 if (Matcher->matches(ast_type_traits::DynTypedNode::create(Node),
164 Finder, Builder)) {
Manuel Klimek4da21662012-07-06 05:48:52 +0000165 Matches = true;
166 return false; // Abort as soon as a match is found.
167 }
168 if (CurrentDepth < MaxDepth) {
169 // The current node doesn't match, and we haven't reached the
170 // maximum depth yet, so recurse.
171 return baseTraverse(Node);
172 }
173 // The current node doesn't match, and we have reached the
174 // maximum depth, so don't recurse (but continue the traversal
175 // such that other nodes at the current level can be visited).
176 return true;
177 } else {
178 BoundNodesTreeBuilder RecursiveBuilder;
Manuel Klimeka78d0d62012-09-05 12:12:07 +0000179 if (Matcher->matches(ast_type_traits::DynTypedNode::create(Node),
180 Finder, &RecursiveBuilder)) {
Manuel Klimek4da21662012-07-06 05:48:52 +0000181 // After the first match the matcher succeeds.
182 Matches = true;
183 Builder->addMatch(RecursiveBuilder.build());
184 }
185 if (CurrentDepth < MaxDepth) {
186 baseTraverse(Node);
187 }
188 // In kBindAll mode we always search for more matches.
189 return true;
190 }
191 }
192
Manuel Klimeka78d0d62012-09-05 12:12:07 +0000193 const DynTypedMatcher *const Matcher;
Manuel Klimek4da21662012-07-06 05:48:52 +0000194 ASTMatchFinder *const Finder;
195 BoundNodesTreeBuilder *const Builder;
196 int CurrentDepth;
197 const int MaxDepth;
198 const ASTMatchFinder::TraversalKind Traversal;
199 const ASTMatchFinder::BindKind Bind;
200 bool Matches;
201};
202
203// Controls the outermost traversal of the AST and allows to match multiple
204// matchers.
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000205class MatchASTVisitor : public RecursiveASTVisitor<MatchASTVisitor>,
Manuel Klimek4da21662012-07-06 05:48:52 +0000206 public ASTMatchFinder {
207public:
Manuel Klimeka78d0d62012-09-05 12:12:07 +0000208 MatchASTVisitor(std::vector<std::pair<const internal::DynTypedMatcher*,
209 MatchCallback*> > *MatcherCallbackPairs)
210 : MatcherCallbackPairs(MatcherCallbackPairs),
Manuel Klimek4da21662012-07-06 05:48:52 +0000211 ActiveASTContext(NULL) {
212 }
213
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000214 void set_active_ast_context(ASTContext *NewActiveASTContext) {
Manuel Klimek4da21662012-07-06 05:48:52 +0000215 ActiveASTContext = NewActiveASTContext;
216 }
217
218 // The following Visit*() and Traverse*() functions "override"
219 // methods in RecursiveASTVisitor.
220
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000221 bool VisitTypedefDecl(TypedefDecl *DeclNode) {
Manuel Klimek4da21662012-07-06 05:48:52 +0000222 // When we see 'typedef A B', we add name 'B' to the set of names
223 // A's canonical type maps to. This is necessary for implementing
224 // IsDerivedFrom(x) properly, where x can be the name of the base
225 // class or any of its aliases.
226 //
227 // In general, the is-alias-of (as defined by typedefs) relation
228 // is tree-shaped, as you can typedef a type more than once. For
229 // example,
230 //
231 // typedef A B;
232 // typedef A C;
233 // typedef C D;
234 // typedef C E;
235 //
236 // gives you
237 //
238 // A
239 // |- B
240 // `- C
241 // |- D
242 // `- E
243 //
244 // It is wrong to assume that the relation is a chain. A correct
245 // implementation of IsDerivedFrom() needs to recognize that B and
246 // E are aliases, even though neither is a typedef of the other.
247 // Therefore, we cannot simply walk through one typedef chain to
248 // find out whether the type name matches.
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000249 const Type *TypeNode = DeclNode->getUnderlyingType().getTypePtr();
250 const Type *CanonicalType = // root of the typedef tree
Manuel Klimek4da21662012-07-06 05:48:52 +0000251 ActiveASTContext->getCanonicalType(TypeNode);
Daniel Jasper20b802d2012-07-17 07:39:27 +0000252 TypeAliases[CanonicalType].insert(DeclNode);
Manuel Klimek4da21662012-07-06 05:48:52 +0000253 return true;
254 }
255
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000256 bool TraverseDecl(Decl *DeclNode);
257 bool TraverseStmt(Stmt *StmtNode);
258 bool TraverseType(QualType TypeNode);
259 bool TraverseTypeLoc(TypeLoc TypeNode);
Manuel Klimek4da21662012-07-06 05:48:52 +0000260
261 // Matches children or descendants of 'Node' with 'BaseMatcher'.
Manuel Klimeka78d0d62012-09-05 12:12:07 +0000262 bool memoizedMatchesRecursively(const ast_type_traits::DynTypedNode &Node,
263 const DynTypedMatcher &Matcher,
Manuel Klimek4da21662012-07-06 05:48:52 +0000264 BoundNodesTreeBuilder *Builder, int MaxDepth,
265 TraversalKind Traversal, BindKind Bind) {
Manuel Klimeka78d0d62012-09-05 12:12:07 +0000266 const UntypedMatchInput input(Matcher.getID(), Node.getMemoizationData());
267 assert(input.second &&
268 "Fix getMemoizationData once more types allow recursive matching.");
Manuel Klimek4da21662012-07-06 05:48:52 +0000269 std::pair<MemoizationMap::iterator, bool> InsertResult
270 = ResultCache.insert(std::make_pair(input, MemoizedMatchResult()));
271 if (InsertResult.second) {
272 BoundNodesTreeBuilder DescendantBoundNodesBuilder;
273 InsertResult.first->second.ResultOfMatch =
Manuel Klimeka78d0d62012-09-05 12:12:07 +0000274 matchesRecursively(Node, Matcher, &DescendantBoundNodesBuilder,
Manuel Klimek4da21662012-07-06 05:48:52 +0000275 MaxDepth, Traversal, Bind);
276 InsertResult.first->second.Nodes =
277 DescendantBoundNodesBuilder.build();
278 }
279 InsertResult.first->second.Nodes.copyTo(Builder);
280 return InsertResult.first->second.ResultOfMatch;
281 }
282
283 // Matches children or descendants of 'Node' with 'BaseMatcher'.
Manuel Klimeka78d0d62012-09-05 12:12:07 +0000284 bool matchesRecursively(const ast_type_traits::DynTypedNode &Node,
285 const DynTypedMatcher &Matcher,
Manuel Klimek4da21662012-07-06 05:48:52 +0000286 BoundNodesTreeBuilder *Builder, int MaxDepth,
287 TraversalKind Traversal, BindKind Bind) {
288 MatchChildASTVisitor Visitor(
Manuel Klimeka78d0d62012-09-05 12:12:07 +0000289 &Matcher, this, Builder, MaxDepth, Traversal, Bind);
Manuel Klimek4da21662012-07-06 05:48:52 +0000290 return Visitor.findMatch(Node);
291 }
292
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000293 virtual bool classIsDerivedFrom(const CXXRecordDecl *Declaration,
Daniel Jasper20b802d2012-07-17 07:39:27 +0000294 const Matcher<NamedDecl> &Base,
295 BoundNodesTreeBuilder *Builder);
Manuel Klimek4da21662012-07-06 05:48:52 +0000296
297 // Implements ASTMatchFinder::MatchesChildOf.
Manuel Klimeka78d0d62012-09-05 12:12:07 +0000298 virtual bool matchesChildOf(const ast_type_traits::DynTypedNode &Node,
299 const DynTypedMatcher &Matcher,
Manuel Klimek4da21662012-07-06 05:48:52 +0000300 BoundNodesTreeBuilder *Builder,
301 TraversalKind Traversal,
302 BindKind Bind) {
Manuel Klimeka78d0d62012-09-05 12:12:07 +0000303 return matchesRecursively(Node, Matcher, Builder, 1, Traversal,
Manuel Klimek4da21662012-07-06 05:48:52 +0000304 Bind);
305 }
Manuel Klimek4da21662012-07-06 05:48:52 +0000306 // Implements ASTMatchFinder::MatchesDescendantOf.
Manuel Klimeka78d0d62012-09-05 12:12:07 +0000307 virtual bool matchesDescendantOf(const ast_type_traits::DynTypedNode &Node,
308 const DynTypedMatcher &Matcher,
Manuel Klimek4da21662012-07-06 05:48:52 +0000309 BoundNodesTreeBuilder *Builder,
310 BindKind Bind) {
Manuel Klimeka78d0d62012-09-05 12:12:07 +0000311 return memoizedMatchesRecursively(Node, Matcher, Builder, INT_MAX,
Manuel Klimek4da21662012-07-06 05:48:52 +0000312 TK_AsIs, Bind);
313 }
314
315 bool shouldVisitTemplateInstantiations() const { return true; }
316 bool shouldVisitImplicitCode() const { return true; }
317
318private:
319 // Implements a BoundNodesTree::Visitor that calls a MatchCallback with
320 // the aggregated bound nodes for each match.
321 class MatchVisitor : public BoundNodesTree::Visitor {
322 public:
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000323 MatchVisitor(ASTContext* Context,
Manuel Klimek4da21662012-07-06 05:48:52 +0000324 MatchFinder::MatchCallback* Callback)
325 : Context(Context),
326 Callback(Callback) {}
327
328 virtual void visitMatch(const BoundNodes& BoundNodesView) {
329 Callback->run(MatchFinder::MatchResult(BoundNodesView, Context));
330 }
331
332 private:
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000333 ASTContext* Context;
Manuel Klimek4da21662012-07-06 05:48:52 +0000334 MatchFinder::MatchCallback* Callback;
335 };
336
Daniel Jasper20b802d2012-07-17 07:39:27 +0000337 // Returns true if 'TypeNode' has an alias that matches the given matcher.
338 bool typeHasMatchingAlias(const Type *TypeNode,
339 const Matcher<NamedDecl> Matcher,
340 BoundNodesTreeBuilder *Builder) {
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000341 const Type *const CanonicalType =
Manuel Klimek4da21662012-07-06 05:48:52 +0000342 ActiveASTContext->getCanonicalType(TypeNode);
Daniel Jasper20b802d2012-07-17 07:39:27 +0000343 const std::set<const TypedefDecl*> &Aliases = TypeAliases[CanonicalType];
344 for (std::set<const TypedefDecl*>::const_iterator
345 It = Aliases.begin(), End = Aliases.end();
346 It != End; ++It) {
347 if (Matcher.matches(**It, this, Builder))
348 return true;
349 }
350 return false;
Manuel Klimek4da21662012-07-06 05:48:52 +0000351 }
352
353 // Matches all registered matchers on the given node and calls the
354 // result callback for every node that matches.
355 template <typename T>
356 void match(const T &node) {
Manuel Klimeka78d0d62012-09-05 12:12:07 +0000357 for (std::vector<std::pair<const internal::DynTypedMatcher*,
358 MatchCallback*> >::const_iterator
359 I = MatcherCallbackPairs->begin(), E = MatcherCallbackPairs->end();
360 I != E; ++I) {
Manuel Klimek4da21662012-07-06 05:48:52 +0000361 BoundNodesTreeBuilder Builder;
Manuel Klimeka78d0d62012-09-05 12:12:07 +0000362 if (I->first->matches(ast_type_traits::DynTypedNode::create(node),
363 this, &Builder)) {
Manuel Klimek4da21662012-07-06 05:48:52 +0000364 BoundNodesTree BoundNodes = Builder.build();
Manuel Klimeka78d0d62012-09-05 12:12:07 +0000365 MatchVisitor Visitor(ActiveASTContext, I->second);
Manuel Klimek4da21662012-07-06 05:48:52 +0000366 BoundNodes.visitMatches(&Visitor);
367 }
368 }
369 }
370
Manuel Klimeka78d0d62012-09-05 12:12:07 +0000371 std::vector<std::pair<const internal::DynTypedMatcher*,
372 MatchCallback*> > *const MatcherCallbackPairs;
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000373 ASTContext *ActiveASTContext;
Manuel Klimek4da21662012-07-06 05:48:52 +0000374
Daniel Jasper20b802d2012-07-17 07:39:27 +0000375 // Maps a canonical type to its TypedefDecls.
376 llvm::DenseMap<const Type*, std::set<const TypedefDecl*> > TypeAliases;
Manuel Klimek4da21662012-07-06 05:48:52 +0000377
378 // Maps (matcher, node) -> the match result for memoization.
379 typedef llvm::DenseMap<UntypedMatchInput, MemoizedMatchResult> MemoizationMap;
380 MemoizationMap ResultCache;
381};
382
383// Returns true if the given class is directly or indirectly derived
384// from a base type with the given name. A class is considered to be
385// also derived from itself.
Daniel Jasper20b802d2012-07-17 07:39:27 +0000386bool MatchASTVisitor::classIsDerivedFrom(const CXXRecordDecl *Declaration,
387 const Matcher<NamedDecl> &Base,
388 BoundNodesTreeBuilder *Builder) {
389 if (Base.matches(*Declaration, this, Builder))
Manuel Klimek4da21662012-07-06 05:48:52 +0000390 return true;
Daniel Jasper20b802d2012-07-17 07:39:27 +0000391 if (!Declaration->hasDefinition())
Manuel Klimek4da21662012-07-06 05:48:52 +0000392 return false;
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000393 typedef CXXRecordDecl::base_class_const_iterator BaseIterator;
Manuel Klimek4da21662012-07-06 05:48:52 +0000394 for (BaseIterator It = Declaration->bases_begin(),
395 End = Declaration->bases_end(); It != End; ++It) {
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000396 const Type *TypeNode = It->getType().getTypePtr();
Manuel Klimek4da21662012-07-06 05:48:52 +0000397
Daniel Jasper20b802d2012-07-17 07:39:27 +0000398 if (typeHasMatchingAlias(TypeNode, Base, Builder))
Manuel Klimek4da21662012-07-06 05:48:52 +0000399 return true;
400
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000401 // Type::getAs<...>() drills through typedefs.
402 if (TypeNode->getAs<DependentNameType>() != NULL ||
Daniel Jasper20b802d2012-07-17 07:39:27 +0000403 TypeNode->getAs<TemplateTypeParmType>() != NULL)
Manuel Klimek4da21662012-07-06 05:48:52 +0000404 // Dependent names and template TypeNode parameters will be matched when
405 // the template is instantiated.
406 continue;
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000407 CXXRecordDecl *ClassDecl = NULL;
408 TemplateSpecializationType const *TemplateType =
409 TypeNode->getAs<TemplateSpecializationType>();
Manuel Klimek4da21662012-07-06 05:48:52 +0000410 if (TemplateType != NULL) {
Daniel Jasper20b802d2012-07-17 07:39:27 +0000411 if (TemplateType->getTemplateName().isDependent())
Manuel Klimek4da21662012-07-06 05:48:52 +0000412 // Dependent template specializations will be matched when the
413 // template is instantiated.
414 continue;
Daniel Jasper20b802d2012-07-17 07:39:27 +0000415
Manuel Klimek4da21662012-07-06 05:48:52 +0000416 // For template specialization types which are specializing a template
417 // declaration which is an explicit or partial specialization of another
418 // template declaration, getAsCXXRecordDecl() returns the corresponding
419 // ClassTemplateSpecializationDecl.
420 //
421 // For template specialization types which are specializing a template
422 // declaration which is neither an explicit nor partial specialization of
423 // another template declaration, getAsCXXRecordDecl() returns NULL and
424 // we get the CXXRecordDecl of the templated declaration.
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000425 CXXRecordDecl *SpecializationDecl =
Manuel Klimek4da21662012-07-06 05:48:52 +0000426 TemplateType->getAsCXXRecordDecl();
427 if (SpecializationDecl != NULL) {
428 ClassDecl = SpecializationDecl;
429 } else {
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000430 ClassDecl = llvm::dyn_cast<CXXRecordDecl>(
Manuel Klimek4da21662012-07-06 05:48:52 +0000431 TemplateType->getTemplateName()
432 .getAsTemplateDecl()->getTemplatedDecl());
433 }
434 } else {
435 ClassDecl = TypeNode->getAsCXXRecordDecl();
436 }
437 assert(ClassDecl != NULL);
438 assert(ClassDecl != Declaration);
Daniel Jasper20b802d2012-07-17 07:39:27 +0000439 if (classIsDerivedFrom(ClassDecl, Base, Builder))
Manuel Klimek4da21662012-07-06 05:48:52 +0000440 return true;
Manuel Klimek4da21662012-07-06 05:48:52 +0000441 }
442 return false;
443}
444
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000445bool MatchASTVisitor::TraverseDecl(Decl *DeclNode) {
Manuel Klimek4da21662012-07-06 05:48:52 +0000446 if (DeclNode == NULL) {
447 return true;
448 }
449 match(*DeclNode);
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000450 return RecursiveASTVisitor<MatchASTVisitor>::TraverseDecl(DeclNode);
Manuel Klimek4da21662012-07-06 05:48:52 +0000451}
452
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000453bool MatchASTVisitor::TraverseStmt(Stmt *StmtNode) {
Manuel Klimek4da21662012-07-06 05:48:52 +0000454 if (StmtNode == NULL) {
455 return true;
456 }
457 match(*StmtNode);
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000458 return RecursiveASTVisitor<MatchASTVisitor>::TraverseStmt(StmtNode);
Manuel Klimek4da21662012-07-06 05:48:52 +0000459}
460
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000461bool MatchASTVisitor::TraverseType(QualType TypeNode) {
Manuel Klimek4da21662012-07-06 05:48:52 +0000462 match(TypeNode);
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000463 return RecursiveASTVisitor<MatchASTVisitor>::TraverseType(TypeNode);
Manuel Klimek4da21662012-07-06 05:48:52 +0000464}
465
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000466bool MatchASTVisitor::TraverseTypeLoc(TypeLoc TypeLoc) {
Daniel Jasper9bd28092012-07-30 05:03:25 +0000467 match(TypeLoc.getType());
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000468 return RecursiveASTVisitor<MatchASTVisitor>::
Daniel Jasper9bd28092012-07-30 05:03:25 +0000469 TraverseTypeLoc(TypeLoc);
Manuel Klimek4da21662012-07-06 05:48:52 +0000470}
471
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000472class MatchASTConsumer : public ASTConsumer {
Manuel Klimek4da21662012-07-06 05:48:52 +0000473public:
Manuel Klimeka78d0d62012-09-05 12:12:07 +0000474 MatchASTConsumer(
475 std::vector<std::pair<const internal::DynTypedMatcher*,
476 MatchCallback*> > *MatcherCallbackPairs,
477 MatchFinder::ParsingDoneTestCallback *ParsingDone)
478 : Visitor(MatcherCallbackPairs),
479 ParsingDone(ParsingDone) {}
Manuel Klimek4da21662012-07-06 05:48:52 +0000480
481private:
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000482 virtual void HandleTranslationUnit(ASTContext &Context) {
Manuel Klimek4da21662012-07-06 05:48:52 +0000483 if (ParsingDone != NULL) {
484 ParsingDone->run();
485 }
486 Visitor.set_active_ast_context(&Context);
487 Visitor.TraverseDecl(Context.getTranslationUnitDecl());
488 Visitor.set_active_ast_context(NULL);
489 }
490
491 MatchASTVisitor Visitor;
492 MatchFinder::ParsingDoneTestCallback *ParsingDone;
493};
494
495} // end namespace
496} // end namespace internal
497
498MatchFinder::MatchResult::MatchResult(const BoundNodes &Nodes,
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000499 ASTContext *Context)
Manuel Klimek4da21662012-07-06 05:48:52 +0000500 : Nodes(Nodes), Context(Context),
501 SourceManager(&Context->getSourceManager()) {}
502
503MatchFinder::MatchCallback::~MatchCallback() {}
504MatchFinder::ParsingDoneTestCallback::~ParsingDoneTestCallback() {}
505
506MatchFinder::MatchFinder() : ParsingDone(NULL) {}
507
508MatchFinder::~MatchFinder() {
Manuel Klimeka78d0d62012-09-05 12:12:07 +0000509 for (std::vector<std::pair<const internal::DynTypedMatcher*,
510 MatchCallback*> >::const_iterator
511 It = MatcherCallbackPairs.begin(), End = MatcherCallbackPairs.end();
Manuel Klimek4da21662012-07-06 05:48:52 +0000512 It != End; ++It) {
513 delete It->first;
514 }
515}
516
517void MatchFinder::addMatcher(const DeclarationMatcher &NodeMatch,
518 MatchCallback *Action) {
Manuel Klimeka78d0d62012-09-05 12:12:07 +0000519 MatcherCallbackPairs.push_back(std::make_pair(
520 new internal::Matcher<Decl>(NodeMatch), Action));
Manuel Klimek4da21662012-07-06 05:48:52 +0000521}
522
523void MatchFinder::addMatcher(const TypeMatcher &NodeMatch,
524 MatchCallback *Action) {
Manuel Klimeka78d0d62012-09-05 12:12:07 +0000525 MatcherCallbackPairs.push_back(std::make_pair(
526 new internal::Matcher<QualType>(NodeMatch), Action));
Manuel Klimek4da21662012-07-06 05:48:52 +0000527}
528
529void MatchFinder::addMatcher(const StatementMatcher &NodeMatch,
530 MatchCallback *Action) {
Manuel Klimeka78d0d62012-09-05 12:12:07 +0000531 MatcherCallbackPairs.push_back(std::make_pair(
532 new internal::Matcher<Stmt>(NodeMatch), Action));
Manuel Klimek4da21662012-07-06 05:48:52 +0000533}
534
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000535ASTConsumer *MatchFinder::newASTConsumer() {
Manuel Klimeka78d0d62012-09-05 12:12:07 +0000536 return new internal::MatchASTConsumer(&MatcherCallbackPairs, ParsingDone);
Manuel Klimek4da21662012-07-06 05:48:52 +0000537}
538
539void MatchFinder::registerTestCallbackAfterParsing(
540 MatchFinder::ParsingDoneTestCallback *NewParsingDone) {
541 ParsingDone = NewParsingDone;
542}
543
544} // end namespace ast_matchers
545} // end namespace clang