1 //===--- ASTMatchFinder.cpp - Structural query framework ------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Implements an algorithm to efficiently search for matches on AST nodes.
11 // Uses memoization to support recursive matches like HasDescendant.
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.
17 //===----------------------------------------------------------------------===//
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 "llvm/ADT/DenseMap.h"
24 #include "llvm/ADT/StringMap.h"
25 #include "llvm/Support/Timer.h"
31 namespace ast_matchers {
35 typedef MatchFinder::MatchCallback MatchCallback;
37 // The maximum number of memoization entries to store.
38 // 10k has been experimentally found to give a good trade-off
39 // of performance vs. memory consumption by running matcher
40 // that match on every statement over a very large codebase.
42 // FIXME: Do some performance optimization in general and
43 // revisit this number; also, put up micro-benchmarks that we can
45 static const unsigned MaxMemoizationEntries = 10000;
47 // We use memoization to avoid running the same matcher on the same
48 // AST node twice. This struct is the key for looking up match
49 // result. It consists of an ID of the MatcherInterface (for
50 // identifying the matcher), a pointer to the AST node and the
51 // bound nodes before the matcher was executed.
53 // We currently only memoize on nodes whose pointers identify the
54 // nodes (\c Stmt and \c Decl, but not \c QualType or \c TypeLoc).
55 // For \c QualType and \c TypeLoc it is possible to implement
56 // generation of keys for each type.
57 // FIXME: Benchmark whether memoization of non-pointer typed nodes
58 // provides enough benefit for the additional amount of code.
60 DynTypedMatcher::MatcherIDType MatcherID;
61 ast_type_traits::DynTypedNode Node;
62 BoundNodesTreeBuilder BoundNodes;
64 bool operator<(const MatchKey &Other) const {
65 return std::tie(MatcherID, Node, BoundNodes) <
66 std::tie(Other.MatcherID, Other.Node, Other.BoundNodes);
70 // Used to store the result of a match and possibly bound nodes.
71 struct MemoizedMatchResult {
73 BoundNodesTreeBuilder Nodes;
76 // A RecursiveASTVisitor that traverses all children or all descendants of
78 class MatchChildASTVisitor
79 : public RecursiveASTVisitor<MatchChildASTVisitor> {
81 typedef RecursiveASTVisitor<MatchChildASTVisitor> VisitorBase;
83 // Creates an AST visitor that matches 'matcher' on all children or
84 // descendants of a traversed node. max_depth is the maximum depth
85 // to traverse: use 1 for matching the children and INT_MAX for
86 // matching the descendants.
87 MatchChildASTVisitor(const DynTypedMatcher *Matcher,
88 ASTMatchFinder *Finder,
89 BoundNodesTreeBuilder *Builder,
91 ASTMatchFinder::TraversalKind Traversal,
92 ASTMatchFinder::BindKind Bind)
102 // Returns true if a match is found in the subtree rooted at the
103 // given AST node. This is done via a set of mutually recursive
104 // functions. Here's how the recursion is done (the *wildcard can
105 // actually be Decl, Stmt, or Type):
107 // - Traverse(node) calls BaseTraverse(node) when it needs
108 // to visit the descendants of node.
109 // - BaseTraverse(node) then calls (via VisitorBase::Traverse*(node))
110 // Traverse*(c) for each child c of 'node'.
111 // - Traverse*(c) in turn calls Traverse(c), completing the
113 bool findMatch(const ast_type_traits::DynTypedNode &DynNode) {
115 if (const Decl *D = DynNode.get<Decl>())
117 else if (const Stmt *S = DynNode.get<Stmt>())
119 else if (const NestedNameSpecifier *NNS =
120 DynNode.get<NestedNameSpecifier>())
122 else if (const NestedNameSpecifierLoc *NNSLoc =
123 DynNode.get<NestedNameSpecifierLoc>())
125 else if (const QualType *Q = DynNode.get<QualType>())
127 else if (const TypeLoc *T = DynNode.get<TypeLoc>())
129 else if (const auto *C = DynNode.get<CXXCtorInitializer>())
131 // FIXME: Add other base types after adding tests.
133 // It's OK to always overwrite the bound nodes, as if there was
134 // no match in this recursive branch, the result set is empty
136 *Builder = ResultBindings;
141 // The following are overriding methods from the base visitor class.
142 // They are public only to allow CRTP to work. They are *not *part
143 // of the public API of this class.
144 bool TraverseDecl(Decl *DeclNode) {
145 ScopedIncrement ScopedDepth(&CurrentDepth);
146 return (DeclNode == nullptr) || traverse(*DeclNode);
148 bool TraverseStmt(Stmt *StmtNode, DataRecursionQueue *Queue = nullptr) {
149 // If we need to keep track of the depth, we can't perform data recursion.
150 if (CurrentDepth == 0 || (CurrentDepth <= MaxDepth && MaxDepth < INT_MAX))
153 ScopedIncrement ScopedDepth(&CurrentDepth);
154 Stmt *StmtToTraverse = StmtNode;
155 if (Traversal == ASTMatchFinder::TK_IgnoreImplicitCastsAndParentheses) {
156 if (Expr *ExprNode = dyn_cast_or_null<Expr>(StmtNode))
157 StmtToTraverse = ExprNode->IgnoreParenImpCasts();
161 if (!match(*StmtToTraverse))
163 return VisitorBase::TraverseStmt(StmtToTraverse, Queue);
165 // We assume that the QualType and the contained type are on the same
166 // hierarchy level. Thus, we try to match either of them.
167 bool TraverseType(QualType TypeNode) {
168 if (TypeNode.isNull())
170 ScopedIncrement ScopedDepth(&CurrentDepth);
172 if (!match(*TypeNode))
174 // The QualType is matched inside traverse.
175 return traverse(TypeNode);
177 // We assume that the TypeLoc, contained QualType and contained Type all are
178 // on the same hierarchy level. Thus, we try to match all of them.
179 bool TraverseTypeLoc(TypeLoc TypeLocNode) {
180 if (TypeLocNode.isNull())
182 ScopedIncrement ScopedDepth(&CurrentDepth);
184 if (!match(*TypeLocNode.getType()))
186 // Match the QualType.
187 if (!match(TypeLocNode.getType()))
189 // The TypeLoc is matched inside traverse.
190 return traverse(TypeLocNode);
192 bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS) {
193 ScopedIncrement ScopedDepth(&CurrentDepth);
194 return (NNS == nullptr) || traverse(*NNS);
196 bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
199 ScopedIncrement ScopedDepth(&CurrentDepth);
200 if (!match(*NNS.getNestedNameSpecifier()))
202 return traverse(NNS);
204 bool TraverseConstructorInitializer(CXXCtorInitializer *CtorInit) {
207 ScopedIncrement ScopedDepth(&CurrentDepth);
208 return traverse(*CtorInit);
211 bool shouldVisitTemplateInstantiations() const { return true; }
212 bool shouldVisitImplicitCode() const { return true; }
215 // Used for updating the depth during traversal.
216 struct ScopedIncrement {
217 explicit ScopedIncrement(int *Depth) : Depth(Depth) { ++(*Depth); }
218 ~ScopedIncrement() { --(*Depth); }
224 // Resets the state of this object.
230 // Forwards the call to the corresponding Traverse*() method in the
231 // base visitor class.
232 bool baseTraverse(const Decl &DeclNode) {
233 return VisitorBase::TraverseDecl(const_cast<Decl*>(&DeclNode));
235 bool baseTraverse(const Stmt &StmtNode) {
236 return VisitorBase::TraverseStmt(const_cast<Stmt*>(&StmtNode));
238 bool baseTraverse(QualType TypeNode) {
239 return VisitorBase::TraverseType(TypeNode);
241 bool baseTraverse(TypeLoc TypeLocNode) {
242 return VisitorBase::TraverseTypeLoc(TypeLocNode);
244 bool baseTraverse(const NestedNameSpecifier &NNS) {
245 return VisitorBase::TraverseNestedNameSpecifier(
246 const_cast<NestedNameSpecifier*>(&NNS));
248 bool baseTraverse(NestedNameSpecifierLoc NNS) {
249 return VisitorBase::TraverseNestedNameSpecifierLoc(NNS);
251 bool baseTraverse(const CXXCtorInitializer &CtorInit) {
252 return VisitorBase::TraverseConstructorInitializer(
253 const_cast<CXXCtorInitializer *>(&CtorInit));
256 // Sets 'Matched' to true if 'Matcher' matches 'Node' and:
257 // 0 < CurrentDepth <= MaxDepth.
259 // Returns 'true' if traversal should continue after this function
260 // returns, i.e. if no match is found or 'Bind' is 'BK_All'.
261 template <typename T>
262 bool match(const T &Node) {
263 if (CurrentDepth == 0 || CurrentDepth > MaxDepth) {
266 if (Bind != ASTMatchFinder::BK_All) {
267 BoundNodesTreeBuilder RecursiveBuilder(*Builder);
268 if (Matcher->matches(ast_type_traits::DynTypedNode::create(Node), Finder,
269 &RecursiveBuilder)) {
271 ResultBindings.addMatch(RecursiveBuilder);
272 return false; // Abort as soon as a match is found.
275 BoundNodesTreeBuilder RecursiveBuilder(*Builder);
276 if (Matcher->matches(ast_type_traits::DynTypedNode::create(Node), Finder,
277 &RecursiveBuilder)) {
278 // After the first match the matcher succeeds.
280 ResultBindings.addMatch(RecursiveBuilder);
286 // Traverses the subtree rooted at 'Node'; returns true if the
287 // traversal should continue after this function returns.
288 template <typename T>
289 bool traverse(const T &Node) {
290 static_assert(IsBaseType<T>::value,
291 "traverse can only be instantiated with base type");
294 return baseTraverse(Node);
297 const DynTypedMatcher *const Matcher;
298 ASTMatchFinder *const Finder;
299 BoundNodesTreeBuilder *const Builder;
300 BoundNodesTreeBuilder ResultBindings;
303 const ASTMatchFinder::TraversalKind Traversal;
304 const ASTMatchFinder::BindKind Bind;
308 // Controls the outermost traversal of the AST and allows to match multiple
310 class MatchASTVisitor : public RecursiveASTVisitor<MatchASTVisitor>,
311 public ASTMatchFinder {
313 MatchASTVisitor(const MatchFinder::MatchersByType *Matchers,
314 const MatchFinder::MatchFinderOptions &Options)
315 : Matchers(Matchers), Options(Options), ActiveASTContext(nullptr) {}
317 ~MatchASTVisitor() override {
318 if (Options.CheckProfiling) {
319 Options.CheckProfiling->Records = std::move(TimeByBucket);
323 void onStartOfTranslationUnit() {
324 const bool EnableCheckProfiling = Options.CheckProfiling.hasValue();
325 TimeBucketRegion Timer;
326 for (MatchCallback *MC : Matchers->AllCallbacks) {
327 if (EnableCheckProfiling)
328 Timer.setBucket(&TimeByBucket[MC->getID()]);
329 MC->onStartOfTranslationUnit();
333 void onEndOfTranslationUnit() {
334 const bool EnableCheckProfiling = Options.CheckProfiling.hasValue();
335 TimeBucketRegion Timer;
336 for (MatchCallback *MC : Matchers->AllCallbacks) {
337 if (EnableCheckProfiling)
338 Timer.setBucket(&TimeByBucket[MC->getID()]);
339 MC->onEndOfTranslationUnit();
343 void set_active_ast_context(ASTContext *NewActiveASTContext) {
344 ActiveASTContext = NewActiveASTContext;
347 // The following Visit*() and Traverse*() functions "override"
348 // methods in RecursiveASTVisitor.
350 bool VisitTypedefNameDecl(TypedefNameDecl *DeclNode) {
351 // When we see 'typedef A B', we add name 'B' to the set of names
352 // A's canonical type maps to. This is necessary for implementing
353 // isDerivedFrom(x) properly, where x can be the name of the base
354 // class or any of its aliases.
356 // In general, the is-alias-of (as defined by typedefs) relation
357 // is tree-shaped, as you can typedef a type more than once. For
373 // It is wrong to assume that the relation is a chain. A correct
374 // implementation of isDerivedFrom() needs to recognize that B and
375 // E are aliases, even though neither is a typedef of the other.
376 // Therefore, we cannot simply walk through one typedef chain to
377 // find out whether the type name matches.
378 const Type *TypeNode = DeclNode->getUnderlyingType().getTypePtr();
379 const Type *CanonicalType = // root of the typedef tree
380 ActiveASTContext->getCanonicalType(TypeNode);
381 TypeAliases[CanonicalType].insert(DeclNode);
385 bool TraverseDecl(Decl *DeclNode);
386 bool TraverseStmt(Stmt *StmtNode, DataRecursionQueue *Queue = nullptr);
387 bool TraverseType(QualType TypeNode);
388 bool TraverseTypeLoc(TypeLoc TypeNode);
389 bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS);
390 bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS);
391 bool TraverseConstructorInitializer(CXXCtorInitializer *CtorInit);
393 // Matches children or descendants of 'Node' with 'BaseMatcher'.
394 bool memoizedMatchesRecursively(const ast_type_traits::DynTypedNode &Node,
395 const DynTypedMatcher &Matcher,
396 BoundNodesTreeBuilder *Builder, int MaxDepth,
397 TraversalKind Traversal, BindKind Bind) {
398 // For AST-nodes that don't have an identity, we can't memoize.
399 if (!Node.getMemoizationData() || !Builder->isComparable())
400 return matchesRecursively(Node, Matcher, Builder, MaxDepth, Traversal,
404 Key.MatcherID = Matcher.getID();
406 // Note that we key on the bindings *before* the match.
407 Key.BoundNodes = *Builder;
409 MemoizationMap::iterator I = ResultCache.find(Key);
410 if (I != ResultCache.end()) {
411 *Builder = I->second.Nodes;
412 return I->second.ResultOfMatch;
415 MemoizedMatchResult Result;
416 Result.Nodes = *Builder;
417 Result.ResultOfMatch = matchesRecursively(Node, Matcher, &Result.Nodes,
418 MaxDepth, Traversal, Bind);
420 MemoizedMatchResult &CachedResult = ResultCache[Key];
421 CachedResult = std::move(Result);
423 *Builder = CachedResult.Nodes;
424 return CachedResult.ResultOfMatch;
427 // Matches children or descendants of 'Node' with 'BaseMatcher'.
428 bool matchesRecursively(const ast_type_traits::DynTypedNode &Node,
429 const DynTypedMatcher &Matcher,
430 BoundNodesTreeBuilder *Builder, int MaxDepth,
431 TraversalKind Traversal, BindKind Bind) {
432 MatchChildASTVisitor Visitor(
433 &Matcher, this, Builder, MaxDepth, Traversal, Bind);
434 return Visitor.findMatch(Node);
437 bool classIsDerivedFrom(const CXXRecordDecl *Declaration,
438 const Matcher<NamedDecl> &Base,
439 BoundNodesTreeBuilder *Builder) override;
441 // Implements ASTMatchFinder::matchesChildOf.
442 bool matchesChildOf(const ast_type_traits::DynTypedNode &Node,
443 const DynTypedMatcher &Matcher,
444 BoundNodesTreeBuilder *Builder,
445 TraversalKind Traversal,
446 BindKind Bind) override {
447 if (ResultCache.size() > MaxMemoizationEntries)
449 return memoizedMatchesRecursively(Node, Matcher, Builder, 1, Traversal,
452 // Implements ASTMatchFinder::matchesDescendantOf.
453 bool matchesDescendantOf(const ast_type_traits::DynTypedNode &Node,
454 const DynTypedMatcher &Matcher,
455 BoundNodesTreeBuilder *Builder,
456 BindKind Bind) override {
457 if (ResultCache.size() > MaxMemoizationEntries)
459 return memoizedMatchesRecursively(Node, Matcher, Builder, INT_MAX,
462 // Implements ASTMatchFinder::matchesAncestorOf.
463 bool matchesAncestorOf(const ast_type_traits::DynTypedNode &Node,
464 const DynTypedMatcher &Matcher,
465 BoundNodesTreeBuilder *Builder,
466 AncestorMatchMode MatchMode) override {
467 // Reset the cache outside of the recursive call to make sure we
468 // don't invalidate any iterators.
469 if (ResultCache.size() > MaxMemoizationEntries)
471 return memoizedMatchesAncestorOfRecursively(Node, Matcher, Builder,
475 // Matches all registered matchers on the given node and calls the
476 // result callback for every node that matches.
477 void match(const ast_type_traits::DynTypedNode &Node) {
478 // FIXME: Improve this with a switch or a visitor pattern.
479 if (auto *N = Node.get<Decl>()) {
481 } else if (auto *N = Node.get<Stmt>()) {
483 } else if (auto *N = Node.get<Type>()) {
485 } else if (auto *N = Node.get<QualType>()) {
487 } else if (auto *N = Node.get<NestedNameSpecifier>()) {
489 } else if (auto *N = Node.get<NestedNameSpecifierLoc>()) {
491 } else if (auto *N = Node.get<TypeLoc>()) {
493 } else if (auto *N = Node.get<CXXCtorInitializer>()) {
498 template <typename T> void match(const T &Node) {
499 matchDispatch(&Node);
502 // Implements ASTMatchFinder::getASTContext.
503 ASTContext &getASTContext() const override { return *ActiveASTContext; }
505 bool shouldVisitTemplateInstantiations() const { return true; }
506 bool shouldVisitImplicitCode() const { return true; }
509 class TimeBucketRegion {
511 TimeBucketRegion() : Bucket(nullptr) {}
512 ~TimeBucketRegion() { setBucket(nullptr); }
514 /// Start timing for \p NewBucket.
516 /// If there was a bucket already set, it will finish the timing for that
518 /// \p NewBucket will be timed until the next call to \c setBucket() or
519 /// until the \c TimeBucketRegion is destroyed.
520 /// If \p NewBucket is the same as the currently timed bucket, this call
522 void setBucket(llvm::TimeRecord *NewBucket) {
523 if (Bucket != NewBucket) {
524 auto Now = llvm::TimeRecord::getCurrentTime(true);
534 llvm::TimeRecord *Bucket;
537 /// Runs all the \p Matchers on \p Node.
539 /// Used by \c matchDispatch() below.
540 template <typename T, typename MC>
541 void matchWithoutFilter(const T &Node, const MC &Matchers) {
542 const bool EnableCheckProfiling = Options.CheckProfiling.hasValue();
543 TimeBucketRegion Timer;
544 for (const auto &MP : Matchers) {
545 if (EnableCheckProfiling)
546 Timer.setBucket(&TimeByBucket[MP.second->getID()]);
547 BoundNodesTreeBuilder Builder;
548 if (MP.first.matches(Node, this, &Builder)) {
549 MatchVisitor Visitor(ActiveASTContext, MP.second);
550 Builder.visitMatches(&Visitor);
555 void matchWithFilter(const ast_type_traits::DynTypedNode &DynNode) {
556 auto Kind = DynNode.getNodeKind();
557 auto it = MatcherFiltersMap.find(Kind);
559 it != MatcherFiltersMap.end() ? it->second : getFilterForKind(Kind);
564 const bool EnableCheckProfiling = Options.CheckProfiling.hasValue();
565 TimeBucketRegion Timer;
566 auto &Matchers = this->Matchers->DeclOrStmt;
567 for (unsigned short I : Filter) {
568 auto &MP = Matchers[I];
569 if (EnableCheckProfiling)
570 Timer.setBucket(&TimeByBucket[MP.second->getID()]);
571 BoundNodesTreeBuilder Builder;
572 if (MP.first.matchesNoKindCheck(DynNode, this, &Builder)) {
573 MatchVisitor Visitor(ActiveASTContext, MP.second);
574 Builder.visitMatches(&Visitor);
579 const std::vector<unsigned short> &
580 getFilterForKind(ast_type_traits::ASTNodeKind Kind) {
581 auto &Filter = MatcherFiltersMap[Kind];
582 auto &Matchers = this->Matchers->DeclOrStmt;
583 assert((Matchers.size() < USHRT_MAX) && "Too many matchers.");
584 for (unsigned I = 0, E = Matchers.size(); I != E; ++I) {
585 if (Matchers[I].first.canMatchNodesOfKind(Kind)) {
593 /// Overloads to pair the different node types to their matchers.
594 void matchDispatch(const Decl *Node) {
595 return matchWithFilter(ast_type_traits::DynTypedNode::create(*Node));
597 void matchDispatch(const Stmt *Node) {
598 return matchWithFilter(ast_type_traits::DynTypedNode::create(*Node));
601 void matchDispatch(const Type *Node) {
602 matchWithoutFilter(QualType(Node, 0), Matchers->Type);
604 void matchDispatch(const TypeLoc *Node) {
605 matchWithoutFilter(*Node, Matchers->TypeLoc);
607 void matchDispatch(const QualType *Node) {
608 matchWithoutFilter(*Node, Matchers->Type);
610 void matchDispatch(const NestedNameSpecifier *Node) {
611 matchWithoutFilter(*Node, Matchers->NestedNameSpecifier);
613 void matchDispatch(const NestedNameSpecifierLoc *Node) {
614 matchWithoutFilter(*Node, Matchers->NestedNameSpecifierLoc);
616 void matchDispatch(const CXXCtorInitializer *Node) {
617 matchWithoutFilter(*Node, Matchers->CtorInit);
619 void matchDispatch(const void *) { /* Do nothing. */ }
622 // Returns whether an ancestor of \p Node matches \p Matcher.
624 // The order of matching ((which can lead to different nodes being bound in
625 // case there are multiple matches) is breadth first search.
627 // To allow memoization in the very common case of having deeply nested
628 // expressions inside a template function, we first walk up the AST, memoizing
629 // the result of the match along the way, as long as there is only a single
632 // Once there are multiple parents, the breadth first search order does not
633 // allow simple memoization on the ancestors. Thus, we only memoize as long
634 // as there is a single parent.
635 bool memoizedMatchesAncestorOfRecursively(
636 const ast_type_traits::DynTypedNode &Node, const DynTypedMatcher &Matcher,
637 BoundNodesTreeBuilder *Builder, AncestorMatchMode MatchMode) {
638 // For AST-nodes that don't have an identity, we can't memoize.
639 if (!Builder->isComparable())
640 return matchesAncestorOfRecursively(Node, Matcher, Builder, MatchMode);
643 Key.MatcherID = Matcher.getID();
645 Key.BoundNodes = *Builder;
647 // Note that we cannot use insert and reuse the iterator, as recursive
648 // calls to match might invalidate the result cache iterators.
649 MemoizationMap::iterator I = ResultCache.find(Key);
650 if (I != ResultCache.end()) {
651 *Builder = I->second.Nodes;
652 return I->second.ResultOfMatch;
655 MemoizedMatchResult Result;
656 Result.Nodes = *Builder;
657 Result.ResultOfMatch =
658 matchesAncestorOfRecursively(Node, Matcher, &Result.Nodes, MatchMode);
660 MemoizedMatchResult &CachedResult = ResultCache[Key];
661 CachedResult = std::move(Result);
663 *Builder = CachedResult.Nodes;
664 return CachedResult.ResultOfMatch;
667 bool matchesAncestorOfRecursively(const ast_type_traits::DynTypedNode &Node,
668 const DynTypedMatcher &Matcher,
669 BoundNodesTreeBuilder *Builder,
670 AncestorMatchMode MatchMode) {
671 const auto &Parents = ActiveASTContext->getParents(Node);
672 if (Parents.empty()) {
673 // Nodes may have no parents if:
674 // a) the node is the TranslationUnitDecl
675 // b) we have a limited traversal scope that excludes the parent edges
676 // c) there is a bug in the AST, and the node is not reachable
677 // Usually the traversal scope is the whole AST, which precludes b.
678 // Bugs are common enough that it's worthwhile asserting when we can.
680 if (!Node.get<TranslationUnitDecl>() &&
681 /* Traversal scope is full AST if any of the bounds are the TU */
682 llvm::any_of(ActiveASTContext->getTraversalScope(), [](Decl *D) {
683 return D->getKind() == Decl::TranslationUnit;
685 llvm::errs() << "Tried to match orphan node:\n";
686 Node.dump(llvm::errs(), ActiveASTContext->getSourceManager());
687 llvm_unreachable("Parent map should be complete!");
692 if (Parents.size() == 1) {
693 // Only one parent - do recursive memoization.
694 const ast_type_traits::DynTypedNode Parent = Parents[0];
695 BoundNodesTreeBuilder BuilderCopy = *Builder;
696 if (Matcher.matches(Parent, this, &BuilderCopy)) {
697 *Builder = std::move(BuilderCopy);
700 if (MatchMode != ASTMatchFinder::AMM_ParentOnly) {
701 return memoizedMatchesAncestorOfRecursively(Parent, Matcher, Builder,
703 // Once we get back from the recursive call, the result will be the
704 // same as the parent's result.
707 // Multiple parents - BFS over the rest of the nodes.
708 llvm::DenseSet<const void *> Visited;
709 std::deque<ast_type_traits::DynTypedNode> Queue(Parents.begin(),
711 while (!Queue.empty()) {
712 BoundNodesTreeBuilder BuilderCopy = *Builder;
713 if (Matcher.matches(Queue.front(), this, &BuilderCopy)) {
714 *Builder = std::move(BuilderCopy);
717 if (MatchMode != ASTMatchFinder::AMM_ParentOnly) {
718 for (const auto &Parent :
719 ActiveASTContext->getParents(Queue.front())) {
720 // Make sure we do not visit the same node twice.
721 // Otherwise, we'll visit the common ancestors as often as there
722 // are splits on the way down.
723 if (Visited.insert(Parent.getMemoizationData()).second)
724 Queue.push_back(Parent);
733 // Implements a BoundNodesTree::Visitor that calls a MatchCallback with
734 // the aggregated bound nodes for each match.
735 class MatchVisitor : public BoundNodesTreeBuilder::Visitor {
737 MatchVisitor(ASTContext* Context,
738 MatchFinder::MatchCallback* Callback)
740 Callback(Callback) {}
742 void visitMatch(const BoundNodes& BoundNodesView) override {
743 Callback->run(MatchFinder::MatchResult(BoundNodesView, Context));
748 MatchFinder::MatchCallback* Callback;
751 // Returns true if 'TypeNode' has an alias that matches the given matcher.
752 bool typeHasMatchingAlias(const Type *TypeNode,
753 const Matcher<NamedDecl> &Matcher,
754 BoundNodesTreeBuilder *Builder) {
755 const Type *const CanonicalType =
756 ActiveASTContext->getCanonicalType(TypeNode);
757 auto Aliases = TypeAliases.find(CanonicalType);
758 if (Aliases == TypeAliases.end())
760 for (const TypedefNameDecl *Alias : Aliases->second) {
761 BoundNodesTreeBuilder Result(*Builder);
762 if (Matcher.matches(*Alias, this, &Result)) {
763 *Builder = std::move(Result);
770 /// Bucket to record map.
772 /// Used to get the appropriate bucket for each matcher.
773 llvm::StringMap<llvm::TimeRecord> TimeByBucket;
775 const MatchFinder::MatchersByType *Matchers;
777 /// Filtered list of matcher indices for each matcher kind.
779 /// \c Decl and \c Stmt toplevel matchers usually apply to a specific node
780 /// kind (and derived kinds) so it is a waste to try every matcher on every
782 /// We precalculate a list of matchers that pass the toplevel restrict check.
783 /// This also allows us to skip the restrict check at matching time. See
784 /// use \c matchesNoKindCheck() above.
785 llvm::DenseMap<ast_type_traits::ASTNodeKind, std::vector<unsigned short>>
788 const MatchFinder::MatchFinderOptions &Options;
789 ASTContext *ActiveASTContext;
791 // Maps a canonical type to its TypedefDecls.
792 llvm::DenseMap<const Type*, std::set<const TypedefNameDecl*> > TypeAliases;
794 // Maps (matcher, node) -> the match result for memoization.
795 typedef std::map<MatchKey, MemoizedMatchResult> MemoizationMap;
796 MemoizationMap ResultCache;
799 static CXXRecordDecl *
800 getAsCXXRecordDeclOrPrimaryTemplate(const Type *TypeNode) {
801 if (auto *RD = TypeNode->getAsCXXRecordDecl())
804 // Find the innermost TemplateSpecializationType that isn't an alias template.
805 auto *TemplateType = TypeNode->getAs<TemplateSpecializationType>();
806 while (TemplateType && TemplateType->isTypeAlias())
808 TemplateType->getAliasedType()->getAs<TemplateSpecializationType>();
810 // If this is the name of a (dependent) template specialization, use the
811 // definition of the template, even though it might be specialized later.
813 if (auto *ClassTemplate = dyn_cast_or_null<ClassTemplateDecl>(
814 TemplateType->getTemplateName().getAsTemplateDecl()))
815 return ClassTemplate->getTemplatedDecl();
820 // Returns true if the given class is directly or indirectly derived
821 // from a base type with the given name. A class is not considered to be
822 // derived from itself.
823 bool MatchASTVisitor::classIsDerivedFrom(const CXXRecordDecl *Declaration,
824 const Matcher<NamedDecl> &Base,
825 BoundNodesTreeBuilder *Builder) {
826 if (!Declaration->hasDefinition())
828 for (const auto &It : Declaration->bases()) {
829 const Type *TypeNode = It.getType().getTypePtr();
831 if (typeHasMatchingAlias(TypeNode, Base, Builder))
834 // FIXME: Going to the primary template here isn't really correct, but
835 // unfortunately we accept a Decl matcher for the base class not a Type
836 // matcher, so it's the best thing we can do with our current interface.
837 CXXRecordDecl *ClassDecl = getAsCXXRecordDeclOrPrimaryTemplate(TypeNode);
840 if (ClassDecl == Declaration) {
841 // This can happen for recursive template definitions; if the
842 // current declaration did not match, we can safely return false.
845 BoundNodesTreeBuilder Result(*Builder);
846 if (Base.matches(*ClassDecl, this, &Result)) {
847 *Builder = std::move(Result);
850 if (classIsDerivedFrom(ClassDecl, Base, Builder))
856 bool MatchASTVisitor::TraverseDecl(Decl *DeclNode) {
861 return RecursiveASTVisitor<MatchASTVisitor>::TraverseDecl(DeclNode);
864 bool MatchASTVisitor::TraverseStmt(Stmt *StmtNode, DataRecursionQueue *Queue) {
869 return RecursiveASTVisitor<MatchASTVisitor>::TraverseStmt(StmtNode, Queue);
872 bool MatchASTVisitor::TraverseType(QualType TypeNode) {
874 return RecursiveASTVisitor<MatchASTVisitor>::TraverseType(TypeNode);
877 bool MatchASTVisitor::TraverseTypeLoc(TypeLoc TypeLocNode) {
878 // The RecursiveASTVisitor only visits types if they're not within TypeLocs.
879 // We still want to find those types via matchers, so we match them here. Note
880 // that the TypeLocs are structurally a shadow-hierarchy to the expressed
881 // type, so we visit all involved parts of a compound type when matching on
884 match(TypeLocNode.getType());
885 return RecursiveASTVisitor<MatchASTVisitor>::TraverseTypeLoc(TypeLocNode);
888 bool MatchASTVisitor::TraverseNestedNameSpecifier(NestedNameSpecifier *NNS) {
890 return RecursiveASTVisitor<MatchASTVisitor>::TraverseNestedNameSpecifier(NNS);
893 bool MatchASTVisitor::TraverseNestedNameSpecifierLoc(
894 NestedNameSpecifierLoc NNS) {
900 // We only match the nested name specifier here (as opposed to traversing it)
901 // because the traversal is already done in the parallel "Loc"-hierarchy.
902 if (NNS.hasQualifier())
903 match(*NNS.getNestedNameSpecifier());
905 RecursiveASTVisitor<MatchASTVisitor>::TraverseNestedNameSpecifierLoc(NNS);
908 bool MatchASTVisitor::TraverseConstructorInitializer(
909 CXXCtorInitializer *CtorInit) {
915 return RecursiveASTVisitor<MatchASTVisitor>::TraverseConstructorInitializer(
919 class MatchASTConsumer : public ASTConsumer {
921 MatchASTConsumer(MatchFinder *Finder,
922 MatchFinder::ParsingDoneTestCallback *ParsingDone)
923 : Finder(Finder), ParsingDone(ParsingDone) {}
926 void HandleTranslationUnit(ASTContext &Context) override {
927 if (ParsingDone != nullptr) {
930 Finder->matchAST(Context);
934 MatchFinder::ParsingDoneTestCallback *ParsingDone;
938 } // end namespace internal
940 MatchFinder::MatchResult::MatchResult(const BoundNodes &Nodes,
942 : Nodes(Nodes), Context(Context),
943 SourceManager(&Context->getSourceManager()) {}
945 MatchFinder::MatchCallback::~MatchCallback() {}
946 MatchFinder::ParsingDoneTestCallback::~ParsingDoneTestCallback() {}
948 MatchFinder::MatchFinder(MatchFinderOptions Options)
949 : Options(std::move(Options)), ParsingDone(nullptr) {}
951 MatchFinder::~MatchFinder() {}
953 void MatchFinder::addMatcher(const DeclarationMatcher &NodeMatch,
954 MatchCallback *Action) {
955 Matchers.DeclOrStmt.emplace_back(NodeMatch, Action);
956 Matchers.AllCallbacks.insert(Action);
959 void MatchFinder::addMatcher(const TypeMatcher &NodeMatch,
960 MatchCallback *Action) {
961 Matchers.Type.emplace_back(NodeMatch, Action);
962 Matchers.AllCallbacks.insert(Action);
965 void MatchFinder::addMatcher(const StatementMatcher &NodeMatch,
966 MatchCallback *Action) {
967 Matchers.DeclOrStmt.emplace_back(NodeMatch, Action);
968 Matchers.AllCallbacks.insert(Action);
971 void MatchFinder::addMatcher(const NestedNameSpecifierMatcher &NodeMatch,
972 MatchCallback *Action) {
973 Matchers.NestedNameSpecifier.emplace_back(NodeMatch, Action);
974 Matchers.AllCallbacks.insert(Action);
977 void MatchFinder::addMatcher(const NestedNameSpecifierLocMatcher &NodeMatch,
978 MatchCallback *Action) {
979 Matchers.NestedNameSpecifierLoc.emplace_back(NodeMatch, Action);
980 Matchers.AllCallbacks.insert(Action);
983 void MatchFinder::addMatcher(const TypeLocMatcher &NodeMatch,
984 MatchCallback *Action) {
985 Matchers.TypeLoc.emplace_back(NodeMatch, Action);
986 Matchers.AllCallbacks.insert(Action);
989 void MatchFinder::addMatcher(const CXXCtorInitializerMatcher &NodeMatch,
990 MatchCallback *Action) {
991 Matchers.CtorInit.emplace_back(NodeMatch, Action);
992 Matchers.AllCallbacks.insert(Action);
995 bool MatchFinder::addDynamicMatcher(const internal::DynTypedMatcher &NodeMatch,
996 MatchCallback *Action) {
997 if (NodeMatch.canConvertTo<Decl>()) {
998 addMatcher(NodeMatch.convertTo<Decl>(), Action);
1000 } else if (NodeMatch.canConvertTo<QualType>()) {
1001 addMatcher(NodeMatch.convertTo<QualType>(), Action);
1003 } else if (NodeMatch.canConvertTo<Stmt>()) {
1004 addMatcher(NodeMatch.convertTo<Stmt>(), Action);
1006 } else if (NodeMatch.canConvertTo<NestedNameSpecifier>()) {
1007 addMatcher(NodeMatch.convertTo<NestedNameSpecifier>(), Action);
1009 } else if (NodeMatch.canConvertTo<NestedNameSpecifierLoc>()) {
1010 addMatcher(NodeMatch.convertTo<NestedNameSpecifierLoc>(), Action);
1012 } else if (NodeMatch.canConvertTo<TypeLoc>()) {
1013 addMatcher(NodeMatch.convertTo<TypeLoc>(), Action);
1015 } else if (NodeMatch.canConvertTo<CXXCtorInitializer>()) {
1016 addMatcher(NodeMatch.convertTo<CXXCtorInitializer>(), Action);
1022 std::unique_ptr<ASTConsumer> MatchFinder::newASTConsumer() {
1023 return llvm::make_unique<internal::MatchASTConsumer>(this, ParsingDone);
1026 void MatchFinder::match(const clang::ast_type_traits::DynTypedNode &Node,
1027 ASTContext &Context) {
1028 internal::MatchASTVisitor Visitor(&Matchers, Options);
1029 Visitor.set_active_ast_context(&Context);
1030 Visitor.match(Node);
1033 void MatchFinder::matchAST(ASTContext &Context) {
1034 internal::MatchASTVisitor Visitor(&Matchers, Options);
1035 Visitor.set_active_ast_context(&Context);
1036 Visitor.onStartOfTranslationUnit();
1037 Visitor.TraverseAST(Context);
1038 Visitor.onEndOfTranslationUnit();
1041 void MatchFinder::registerTestCallbackAfterParsing(
1042 MatchFinder::ParsingDoneTestCallback *NewParsingDone) {
1043 ParsingDone = NewParsingDone;
1046 StringRef MatchFinder::MatchCallback::getID() const { return "<unknown>"; }
1048 } // end namespace ast_matchers
1049 } // end namespace clang