xref: /llvm-project/clang/lib/ASTMatchers/ASTMatchFinder.cpp (revision d89f9e963e4979466193dc6a15fe091bf7ca5c47)
1 //===--- ASTMatchFinder.cpp - Structural query framework ------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 //  Implements an algorithm to efficiently search for matches on AST nodes.
10 //  Uses memoization to support recursive matches like HasDescendant.
11 //
12 //  The general idea is to visit all AST nodes with a RecursiveASTVisitor,
13 //  calling the Matches(...) method of each matcher we are running on each
14 //  AST node. The matcher can recurse via the ASTMatchFinder interface.
15 //
16 //===----------------------------------------------------------------------===//
17 
18 #include "clang/ASTMatchers/ASTMatchFinder.h"
19 #include "clang/AST/ASTConsumer.h"
20 #include "clang/AST/ASTContext.h"
21 #include "clang/AST/RecursiveASTVisitor.h"
22 #include "llvm/ADT/DenseMap.h"
23 #include "llvm/ADT/StringMap.h"
24 #include "llvm/Support/PrettyStackTrace.h"
25 #include "llvm/Support/Timer.h"
26 #include <deque>
27 #include <memory>
28 #include <set>
29 
30 namespace clang {
31 namespace ast_matchers {
32 namespace internal {
33 namespace {
34 
35 typedef MatchFinder::MatchCallback MatchCallback;
36 
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.
41 //
42 // FIXME: Do some performance optimization in general and
43 // revisit this number; also, put up micro-benchmarks that we can
44 // optimize this on.
45 static const unsigned MaxMemoizationEntries = 10000;
46 
47 enum class MatchType {
48   Ancestors,
49 
50   Descendants,
51   Child,
52 };
53 
54 // We use memoization to avoid running the same matcher on the same
55 // AST node twice.  This struct is the key for looking up match
56 // result.  It consists of an ID of the MatcherInterface (for
57 // identifying the matcher), a pointer to the AST node and the
58 // bound nodes before the matcher was executed.
59 //
60 // We currently only memoize on nodes whose pointers identify the
61 // nodes (\c Stmt and \c Decl, but not \c QualType or \c TypeLoc).
62 // For \c QualType and \c TypeLoc it is possible to implement
63 // generation of keys for each type.
64 // FIXME: Benchmark whether memoization of non-pointer typed nodes
65 // provides enough benefit for the additional amount of code.
66 struct MatchKey {
67   DynTypedMatcher::MatcherIDType MatcherID;
68   DynTypedNode Node;
69   BoundNodesTreeBuilder BoundNodes;
70   TraversalKind Traversal = TK_AsIs;
71   MatchType Type;
72 
73   bool operator<(const MatchKey &Other) const {
74     return std::tie(Traversal, Type, MatcherID, Node, BoundNodes) <
75            std::tie(Other.Traversal, Other.Type, Other.MatcherID, Other.Node,
76                     Other.BoundNodes);
77   }
78 };
79 
80 // Used to store the result of a match and possibly bound nodes.
81 struct MemoizedMatchResult {
82   bool ResultOfMatch;
83   BoundNodesTreeBuilder Nodes;
84 };
85 
86 // A RecursiveASTVisitor that traverses all children or all descendants of
87 // a node.
88 class MatchChildASTVisitor
89     : public RecursiveASTVisitor<MatchChildASTVisitor> {
90 public:
91   typedef RecursiveASTVisitor<MatchChildASTVisitor> VisitorBase;
92 
93   // Creates an AST visitor that matches 'matcher' on all children or
94   // descendants of a traversed node. max_depth is the maximum depth
95   // to traverse: use 1 for matching the children and INT_MAX for
96   // matching the descendants.
97   MatchChildASTVisitor(const DynTypedMatcher *Matcher, ASTMatchFinder *Finder,
98                        BoundNodesTreeBuilder *Builder, int MaxDepth,
99                        bool IgnoreImplicitChildren,
100                        ASTMatchFinder::BindKind Bind)
101       : Matcher(Matcher), Finder(Finder), Builder(Builder), CurrentDepth(0),
102         MaxDepth(MaxDepth), IgnoreImplicitChildren(IgnoreImplicitChildren),
103         Bind(Bind), Matches(false) {}
104 
105   // Returns true if a match is found in the subtree rooted at the
106   // given AST node. This is done via a set of mutually recursive
107   // functions. Here's how the recursion is done (the  *wildcard can
108   // actually be Decl, Stmt, or Type):
109   //
110   //   - Traverse(node) calls BaseTraverse(node) when it needs
111   //     to visit the descendants of node.
112   //   - BaseTraverse(node) then calls (via VisitorBase::Traverse*(node))
113   //     Traverse*(c) for each child c of 'node'.
114   //   - Traverse*(c) in turn calls Traverse(c), completing the
115   //     recursion.
116   bool findMatch(const DynTypedNode &DynNode) {
117     reset();
118     if (const Decl *D = DynNode.get<Decl>())
119       traverse(*D);
120     else if (const Stmt *S = DynNode.get<Stmt>())
121       traverse(*S);
122     else if (const NestedNameSpecifier *NNS =
123              DynNode.get<NestedNameSpecifier>())
124       traverse(*NNS);
125     else if (const NestedNameSpecifierLoc *NNSLoc =
126              DynNode.get<NestedNameSpecifierLoc>())
127       traverse(*NNSLoc);
128     else if (const QualType *Q = DynNode.get<QualType>())
129       traverse(*Q);
130     else if (const TypeLoc *T = DynNode.get<TypeLoc>())
131       traverse(*T);
132     else if (const auto *C = DynNode.get<CXXCtorInitializer>())
133       traverse(*C);
134     else if (const TemplateArgumentLoc *TALoc =
135                  DynNode.get<TemplateArgumentLoc>())
136       traverse(*TALoc);
137     else if (const Attr *A = DynNode.get<Attr>())
138       traverse(*A);
139     // FIXME: Add other base types after adding tests.
140 
141     // It's OK to always overwrite the bound nodes, as if there was
142     // no match in this recursive branch, the result set is empty
143     // anyway.
144     *Builder = ResultBindings;
145 
146     return Matches;
147   }
148 
149   // The following are overriding methods from the base visitor class.
150   // They are public only to allow CRTP to work. They are *not *part
151   // of the public API of this class.
152   bool TraverseDecl(Decl *DeclNode) {
153 
154     if (DeclNode && DeclNode->isImplicit() &&
155         Finder->isTraversalIgnoringImplicitNodes())
156       return baseTraverse(*DeclNode);
157 
158     ScopedIncrement ScopedDepth(&CurrentDepth);
159     return (DeclNode == nullptr) || traverse(*DeclNode);
160   }
161 
162   Stmt *getStmtToTraverse(Stmt *StmtNode) {
163     Stmt *StmtToTraverse = StmtNode;
164     if (auto *ExprNode = dyn_cast_or_null<Expr>(StmtNode)) {
165       auto *LambdaNode = dyn_cast_or_null<LambdaExpr>(StmtNode);
166       if (LambdaNode && Finder->isTraversalIgnoringImplicitNodes())
167         StmtToTraverse = LambdaNode;
168       else
169         StmtToTraverse =
170             Finder->getASTContext().getParentMapContext().traverseIgnored(
171                 ExprNode);
172     }
173     return StmtToTraverse;
174   }
175 
176   bool TraverseStmt(Stmt *StmtNode, DataRecursionQueue *Queue = nullptr) {
177     // If we need to keep track of the depth, we can't perform data recursion.
178     if (CurrentDepth == 0 || (CurrentDepth <= MaxDepth && MaxDepth < INT_MAX))
179       Queue = nullptr;
180 
181     ScopedIncrement ScopedDepth(&CurrentDepth);
182     Stmt *StmtToTraverse = getStmtToTraverse(StmtNode);
183     if (!StmtToTraverse)
184       return true;
185 
186     if (IgnoreImplicitChildren && isa<CXXDefaultArgExpr>(StmtNode))
187       return true;
188 
189     if (!match(*StmtToTraverse))
190       return false;
191     return VisitorBase::TraverseStmt(StmtToTraverse, Queue);
192   }
193   // We assume that the QualType and the contained type are on the same
194   // hierarchy level. Thus, we try to match either of them.
195   bool TraverseType(QualType TypeNode) {
196     if (TypeNode.isNull())
197       return true;
198     ScopedIncrement ScopedDepth(&CurrentDepth);
199     // Match the Type.
200     if (!match(*TypeNode))
201       return false;
202     // The QualType is matched inside traverse.
203     return traverse(TypeNode);
204   }
205   // We assume that the TypeLoc, contained QualType and contained Type all are
206   // on the same hierarchy level. Thus, we try to match all of them.
207   bool TraverseTypeLoc(TypeLoc TypeLocNode) {
208     if (TypeLocNode.isNull())
209       return true;
210     ScopedIncrement ScopedDepth(&CurrentDepth);
211     // Match the Type.
212     if (!match(*TypeLocNode.getType()))
213       return false;
214     // Match the QualType.
215     if (!match(TypeLocNode.getType()))
216       return false;
217     // The TypeLoc is matched inside traverse.
218     return traverse(TypeLocNode);
219   }
220   bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS) {
221     ScopedIncrement ScopedDepth(&CurrentDepth);
222     return (NNS == nullptr) || traverse(*NNS);
223   }
224   bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
225     if (!NNS)
226       return true;
227     ScopedIncrement ScopedDepth(&CurrentDepth);
228     if (!match(*NNS.getNestedNameSpecifier()))
229       return false;
230     return traverse(NNS);
231   }
232   bool TraverseConstructorInitializer(CXXCtorInitializer *CtorInit) {
233     if (!CtorInit)
234       return true;
235     ScopedIncrement ScopedDepth(&CurrentDepth);
236     return traverse(*CtorInit);
237   }
238   bool TraverseTemplateArgumentLoc(TemplateArgumentLoc TAL) {
239     ScopedIncrement ScopedDepth(&CurrentDepth);
240     return traverse(TAL);
241   }
242   bool TraverseCXXForRangeStmt(CXXForRangeStmt *Node) {
243     if (!Finder->isTraversalIgnoringImplicitNodes())
244       return VisitorBase::TraverseCXXForRangeStmt(Node);
245     if (!Node)
246       return true;
247     ScopedIncrement ScopedDepth(&CurrentDepth);
248     if (auto *Init = Node->getInit())
249       if (!traverse(*Init))
250         return false;
251     if (!match(*Node->getLoopVariable()))
252       return false;
253     if (match(*Node->getRangeInit()))
254       if (!VisitorBase::TraverseStmt(Node->getRangeInit()))
255         return false;
256     if (!match(*Node->getBody()))
257       return false;
258     return VisitorBase::TraverseStmt(Node->getBody());
259   }
260   bool TraverseCXXRewrittenBinaryOperator(CXXRewrittenBinaryOperator *Node) {
261     if (!Finder->isTraversalIgnoringImplicitNodes())
262       return VisitorBase::TraverseCXXRewrittenBinaryOperator(Node);
263     if (!Node)
264       return true;
265     ScopedIncrement ScopedDepth(&CurrentDepth);
266 
267     return match(*Node->getLHS()) && match(*Node->getRHS());
268   }
269   bool TraverseAttr(Attr *A) {
270     if (A == nullptr ||
271         (A->isImplicit() &&
272          Finder->getASTContext().getParentMapContext().getTraversalKind() ==
273              TK_IgnoreUnlessSpelledInSource))
274       return true;
275     ScopedIncrement ScopedDepth(&CurrentDepth);
276     return traverse(*A);
277   }
278   bool TraverseLambdaExpr(LambdaExpr *Node) {
279     if (!Finder->isTraversalIgnoringImplicitNodes())
280       return VisitorBase::TraverseLambdaExpr(Node);
281     if (!Node)
282       return true;
283     ScopedIncrement ScopedDepth(&CurrentDepth);
284 
285     for (unsigned I = 0, N = Node->capture_size(); I != N; ++I) {
286       const auto *C = Node->capture_begin() + I;
287       if (!C->isExplicit())
288         continue;
289       if (Node->isInitCapture(C) && !match(*C->getCapturedVar()))
290         return false;
291       if (!match(*Node->capture_init_begin()[I]))
292         return false;
293     }
294 
295     if (const auto *TPL = Node->getTemplateParameterList()) {
296       for (const auto *TP : *TPL) {
297         if (!match(*TP))
298           return false;
299       }
300     }
301 
302     for (const auto *P : Node->getCallOperator()->parameters()) {
303       if (!match(*P))
304         return false;
305     }
306 
307     if (!match(*Node->getBody()))
308       return false;
309 
310     return VisitorBase::TraverseStmt(Node->getBody());
311   }
312 
313   bool shouldVisitTemplateInstantiations() const { return true; }
314   bool shouldVisitImplicitCode() const { return !IgnoreImplicitChildren; }
315 
316 private:
317   // Used for updating the depth during traversal.
318   struct ScopedIncrement {
319     explicit ScopedIncrement(int *Depth) : Depth(Depth) { ++(*Depth); }
320     ~ScopedIncrement() { --(*Depth); }
321 
322    private:
323     int *Depth;
324   };
325 
326   // Resets the state of this object.
327   void reset() {
328     Matches = false;
329     CurrentDepth = 0;
330   }
331 
332   // Forwards the call to the corresponding Traverse*() method in the
333   // base visitor class.
334   bool baseTraverse(const Decl &DeclNode) {
335     return VisitorBase::TraverseDecl(const_cast<Decl*>(&DeclNode));
336   }
337   bool baseTraverse(const Stmt &StmtNode) {
338     return VisitorBase::TraverseStmt(const_cast<Stmt*>(&StmtNode));
339   }
340   bool baseTraverse(QualType TypeNode) {
341     return VisitorBase::TraverseType(TypeNode);
342   }
343   bool baseTraverse(TypeLoc TypeLocNode) {
344     return VisitorBase::TraverseTypeLoc(TypeLocNode);
345   }
346   bool baseTraverse(const NestedNameSpecifier &NNS) {
347     return VisitorBase::TraverseNestedNameSpecifier(
348         const_cast<NestedNameSpecifier*>(&NNS));
349   }
350   bool baseTraverse(NestedNameSpecifierLoc NNS) {
351     return VisitorBase::TraverseNestedNameSpecifierLoc(NNS);
352   }
353   bool baseTraverse(const CXXCtorInitializer &CtorInit) {
354     return VisitorBase::TraverseConstructorInitializer(
355         const_cast<CXXCtorInitializer *>(&CtorInit));
356   }
357   bool baseTraverse(TemplateArgumentLoc TAL) {
358     return VisitorBase::TraverseTemplateArgumentLoc(TAL);
359   }
360   bool baseTraverse(const Attr &AttrNode) {
361     return VisitorBase::TraverseAttr(const_cast<Attr *>(&AttrNode));
362   }
363 
364   // Sets 'Matched' to true if 'Matcher' matches 'Node' and:
365   //   0 < CurrentDepth <= MaxDepth.
366   //
367   // Returns 'true' if traversal should continue after this function
368   // returns, i.e. if no match is found or 'Bind' is 'BK_All'.
369   template <typename T>
370   bool match(const T &Node) {
371     if (CurrentDepth == 0 || CurrentDepth > MaxDepth) {
372       return true;
373     }
374     if (Bind != ASTMatchFinder::BK_All) {
375       BoundNodesTreeBuilder RecursiveBuilder(*Builder);
376       if (Matcher->matches(DynTypedNode::create(Node), Finder,
377                            &RecursiveBuilder)) {
378         Matches = true;
379         ResultBindings.addMatch(RecursiveBuilder);
380         return false; // Abort as soon as a match is found.
381       }
382     } else {
383       BoundNodesTreeBuilder RecursiveBuilder(*Builder);
384       if (Matcher->matches(DynTypedNode::create(Node), Finder,
385                            &RecursiveBuilder)) {
386         // After the first match the matcher succeeds.
387         Matches = true;
388         ResultBindings.addMatch(RecursiveBuilder);
389       }
390     }
391     return true;
392   }
393 
394   // Traverses the subtree rooted at 'Node'; returns true if the
395   // traversal should continue after this function returns.
396   template <typename T>
397   bool traverse(const T &Node) {
398     static_assert(IsBaseType<T>::value,
399                   "traverse can only be instantiated with base type");
400     if (!match(Node))
401       return false;
402     return baseTraverse(Node);
403   }
404 
405   const DynTypedMatcher *const Matcher;
406   ASTMatchFinder *const Finder;
407   BoundNodesTreeBuilder *const Builder;
408   BoundNodesTreeBuilder ResultBindings;
409   int CurrentDepth;
410   const int MaxDepth;
411   const bool IgnoreImplicitChildren;
412   const ASTMatchFinder::BindKind Bind;
413   bool Matches;
414 };
415 
416 // Controls the outermost traversal of the AST and allows to match multiple
417 // matchers.
418 class MatchASTVisitor : public RecursiveASTVisitor<MatchASTVisitor>,
419                         public ASTMatchFinder {
420 public:
421   MatchASTVisitor(const MatchFinder::MatchersByType *Matchers,
422                   const MatchFinder::MatchFinderOptions &Options)
423       : Matchers(Matchers), Options(Options), ActiveASTContext(nullptr) {}
424 
425   ~MatchASTVisitor() override {
426     if (Options.CheckProfiling) {
427       Options.CheckProfiling->Records = std::move(TimeByBucket);
428     }
429   }
430 
431   void onStartOfTranslationUnit() {
432     const bool EnableCheckProfiling = Options.CheckProfiling.hasValue();
433     TimeBucketRegion Timer;
434     for (MatchCallback *MC : Matchers->AllCallbacks) {
435       if (EnableCheckProfiling)
436         Timer.setBucket(&TimeByBucket[MC->getID()]);
437       MC->onStartOfTranslationUnit();
438     }
439   }
440 
441   void onEndOfTranslationUnit() {
442     const bool EnableCheckProfiling = Options.CheckProfiling.hasValue();
443     TimeBucketRegion Timer;
444     for (MatchCallback *MC : Matchers->AllCallbacks) {
445       if (EnableCheckProfiling)
446         Timer.setBucket(&TimeByBucket[MC->getID()]);
447       MC->onEndOfTranslationUnit();
448     }
449   }
450 
451   void set_active_ast_context(ASTContext *NewActiveASTContext) {
452     ActiveASTContext = NewActiveASTContext;
453   }
454 
455   // The following Visit*() and Traverse*() functions "override"
456   // methods in RecursiveASTVisitor.
457 
458   bool VisitTypedefNameDecl(TypedefNameDecl *DeclNode) {
459     // When we see 'typedef A B', we add name 'B' to the set of names
460     // A's canonical type maps to.  This is necessary for implementing
461     // isDerivedFrom(x) properly, where x can be the name of the base
462     // class or any of its aliases.
463     //
464     // In general, the is-alias-of (as defined by typedefs) relation
465     // is tree-shaped, as you can typedef a type more than once.  For
466     // example,
467     //
468     //   typedef A B;
469     //   typedef A C;
470     //   typedef C D;
471     //   typedef C E;
472     //
473     // gives you
474     //
475     //   A
476     //   |- B
477     //   `- C
478     //      |- D
479     //      `- E
480     //
481     // It is wrong to assume that the relation is a chain.  A correct
482     // implementation of isDerivedFrom() needs to recognize that B and
483     // E are aliases, even though neither is a typedef of the other.
484     // Therefore, we cannot simply walk through one typedef chain to
485     // find out whether the type name matches.
486     const Type *TypeNode = DeclNode->getUnderlyingType().getTypePtr();
487     const Type *CanonicalType =  // root of the typedef tree
488         ActiveASTContext->getCanonicalType(TypeNode);
489     TypeAliases[CanonicalType].insert(DeclNode);
490     return true;
491   }
492 
493   bool VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *CAD) {
494     const ObjCInterfaceDecl *InterfaceDecl = CAD->getClassInterface();
495     CompatibleAliases[InterfaceDecl].insert(CAD);
496     return true;
497   }
498 
499   bool TraverseDecl(Decl *DeclNode);
500   bool TraverseStmt(Stmt *StmtNode, DataRecursionQueue *Queue = nullptr);
501   bool TraverseType(QualType TypeNode);
502   bool TraverseTypeLoc(TypeLoc TypeNode);
503   bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS);
504   bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS);
505   bool TraverseConstructorInitializer(CXXCtorInitializer *CtorInit);
506   bool TraverseTemplateArgumentLoc(TemplateArgumentLoc TAL);
507   bool TraverseAttr(Attr *AttrNode);
508 
509   bool dataTraverseNode(Stmt *S, DataRecursionQueue *Queue) {
510     if (auto *RF = dyn_cast<CXXForRangeStmt>(S)) {
511       {
512         ASTNodeNotAsIsSourceScope RAII(this, true);
513         TraverseStmt(RF->getInit());
514         // Don't traverse under the loop variable
515         match(*RF->getLoopVariable());
516         TraverseStmt(RF->getRangeInit());
517       }
518       {
519         ASTNodeNotSpelledInSourceScope RAII(this, true);
520         for (auto *SubStmt : RF->children()) {
521           if (SubStmt != RF->getBody())
522             TraverseStmt(SubStmt);
523         }
524       }
525       TraverseStmt(RF->getBody());
526       return true;
527     } else if (auto *RBO = dyn_cast<CXXRewrittenBinaryOperator>(S)) {
528       {
529         ASTNodeNotAsIsSourceScope RAII(this, true);
530         TraverseStmt(const_cast<Expr *>(RBO->getLHS()));
531         TraverseStmt(const_cast<Expr *>(RBO->getRHS()));
532       }
533       {
534         ASTNodeNotSpelledInSourceScope RAII(this, true);
535         for (auto *SubStmt : RBO->children()) {
536           TraverseStmt(SubStmt);
537         }
538       }
539       return true;
540     } else if (auto *LE = dyn_cast<LambdaExpr>(S)) {
541       for (auto I : llvm::zip(LE->captures(), LE->capture_inits())) {
542         auto C = std::get<0>(I);
543         ASTNodeNotSpelledInSourceScope RAII(
544             this, TraversingASTNodeNotSpelledInSource || !C.isExplicit());
545         TraverseLambdaCapture(LE, &C, std::get<1>(I));
546       }
547 
548       {
549         ASTNodeNotSpelledInSourceScope RAII(this, true);
550         TraverseDecl(LE->getLambdaClass());
551       }
552       {
553         ASTNodeNotAsIsSourceScope RAII(this, true);
554 
555         // We need to poke around to find the bits that might be explicitly
556         // written.
557         TypeLoc TL = LE->getCallOperator()->getTypeSourceInfo()->getTypeLoc();
558         FunctionProtoTypeLoc Proto = TL.getAsAdjusted<FunctionProtoTypeLoc>();
559 
560         if (auto *TPL = LE->getTemplateParameterList()) {
561           for (NamedDecl *D : *TPL) {
562             TraverseDecl(D);
563           }
564           if (Expr *RequiresClause = TPL->getRequiresClause()) {
565             TraverseStmt(RequiresClause);
566           }
567         }
568 
569         if (LE->hasExplicitParameters()) {
570           // Visit parameters.
571           for (ParmVarDecl *Param : Proto.getParams())
572             TraverseDecl(Param);
573         }
574 
575         const auto *T = Proto.getTypePtr();
576         for (const auto &E : T->exceptions())
577           TraverseType(E);
578 
579         if (Expr *NE = T->getNoexceptExpr())
580           TraverseStmt(NE, Queue);
581 
582         if (LE->hasExplicitResultType())
583           TraverseTypeLoc(Proto.getReturnLoc());
584         TraverseStmt(LE->getTrailingRequiresClause());
585       }
586 
587       TraverseStmt(LE->getBody());
588       return true;
589     }
590     return RecursiveASTVisitor<MatchASTVisitor>::dataTraverseNode(S, Queue);
591   }
592 
593   // Matches children or descendants of 'Node' with 'BaseMatcher'.
594   bool memoizedMatchesRecursively(const DynTypedNode &Node, ASTContext &Ctx,
595                                   const DynTypedMatcher &Matcher,
596                                   BoundNodesTreeBuilder *Builder, int MaxDepth,
597                                   BindKind Bind) {
598     // For AST-nodes that don't have an identity, we can't memoize.
599     if (!Node.getMemoizationData() || !Builder->isComparable())
600       return matchesRecursively(Node, Matcher, Builder, MaxDepth, Bind);
601 
602     MatchKey Key;
603     Key.MatcherID = Matcher.getID();
604     Key.Node = Node;
605     // Note that we key on the bindings *before* the match.
606     Key.BoundNodes = *Builder;
607     Key.Traversal = Ctx.getParentMapContext().getTraversalKind();
608     // Memoize result even doing a single-level match, it might be expensive.
609     Key.Type = MaxDepth == 1 ? MatchType::Child : MatchType::Descendants;
610     MemoizationMap::iterator I = ResultCache.find(Key);
611     if (I != ResultCache.end()) {
612       *Builder = I->second.Nodes;
613       return I->second.ResultOfMatch;
614     }
615 
616     MemoizedMatchResult Result;
617     Result.Nodes = *Builder;
618     Result.ResultOfMatch =
619         matchesRecursively(Node, Matcher, &Result.Nodes, MaxDepth, Bind);
620 
621     MemoizedMatchResult &CachedResult = ResultCache[Key];
622     CachedResult = std::move(Result);
623 
624     *Builder = CachedResult.Nodes;
625     return CachedResult.ResultOfMatch;
626   }
627 
628   // Matches children or descendants of 'Node' with 'BaseMatcher'.
629   bool matchesRecursively(const DynTypedNode &Node,
630                           const DynTypedMatcher &Matcher,
631                           BoundNodesTreeBuilder *Builder, int MaxDepth,
632                           BindKind Bind) {
633     bool ScopedTraversal = TraversingASTNodeNotSpelledInSource ||
634                            TraversingASTChildrenNotSpelledInSource;
635 
636     bool IgnoreImplicitChildren = false;
637 
638     if (isTraversalIgnoringImplicitNodes()) {
639       IgnoreImplicitChildren = true;
640     }
641 
642     ASTNodeNotSpelledInSourceScope RAII(this, ScopedTraversal);
643 
644     MatchChildASTVisitor Visitor(&Matcher, this, Builder, MaxDepth,
645                                  IgnoreImplicitChildren, Bind);
646     return Visitor.findMatch(Node);
647   }
648 
649   bool classIsDerivedFrom(const CXXRecordDecl *Declaration,
650                           const Matcher<NamedDecl> &Base,
651                           BoundNodesTreeBuilder *Builder,
652                           bool Directly) override;
653 
654   bool objcClassIsDerivedFrom(const ObjCInterfaceDecl *Declaration,
655                               const Matcher<NamedDecl> &Base,
656                               BoundNodesTreeBuilder *Builder,
657                               bool Directly) override;
658 
659   // Implements ASTMatchFinder::matchesChildOf.
660   bool matchesChildOf(const DynTypedNode &Node, ASTContext &Ctx,
661                       const DynTypedMatcher &Matcher,
662                       BoundNodesTreeBuilder *Builder, BindKind Bind) override {
663     if (ResultCache.size() > MaxMemoizationEntries)
664       ResultCache.clear();
665     return memoizedMatchesRecursively(Node, Ctx, Matcher, Builder, 1, Bind);
666   }
667   // Implements ASTMatchFinder::matchesDescendantOf.
668   bool matchesDescendantOf(const DynTypedNode &Node, ASTContext &Ctx,
669                            const DynTypedMatcher &Matcher,
670                            BoundNodesTreeBuilder *Builder,
671                            BindKind Bind) override {
672     if (ResultCache.size() > MaxMemoizationEntries)
673       ResultCache.clear();
674     return memoizedMatchesRecursively(Node, Ctx, Matcher, Builder, INT_MAX,
675                                       Bind);
676   }
677   // Implements ASTMatchFinder::matchesAncestorOf.
678   bool matchesAncestorOf(const DynTypedNode &Node, ASTContext &Ctx,
679                          const DynTypedMatcher &Matcher,
680                          BoundNodesTreeBuilder *Builder,
681                          AncestorMatchMode MatchMode) override {
682     // Reset the cache outside of the recursive call to make sure we
683     // don't invalidate any iterators.
684     if (ResultCache.size() > MaxMemoizationEntries)
685       ResultCache.clear();
686     if (MatchMode == AncestorMatchMode::AMM_ParentOnly)
687       return matchesParentOf(Node, Matcher, Builder);
688     return matchesAnyAncestorOf(Node, Ctx, Matcher, Builder);
689   }
690 
691   // Matches all registered matchers on the given node and calls the
692   // result callback for every node that matches.
693   void match(const DynTypedNode &Node) {
694     // FIXME: Improve this with a switch or a visitor pattern.
695     if (auto *N = Node.get<Decl>()) {
696       match(*N);
697     } else if (auto *N = Node.get<Stmt>()) {
698       match(*N);
699     } else if (auto *N = Node.get<Type>()) {
700       match(*N);
701     } else if (auto *N = Node.get<QualType>()) {
702       match(*N);
703     } else if (auto *N = Node.get<NestedNameSpecifier>()) {
704       match(*N);
705     } else if (auto *N = Node.get<NestedNameSpecifierLoc>()) {
706       match(*N);
707     } else if (auto *N = Node.get<TypeLoc>()) {
708       match(*N);
709     } else if (auto *N = Node.get<CXXCtorInitializer>()) {
710       match(*N);
711     } else if (auto *N = Node.get<TemplateArgumentLoc>()) {
712       match(*N);
713     } else if (auto *N = Node.get<Attr>()) {
714       match(*N);
715     }
716   }
717 
718   template <typename T> void match(const T &Node) {
719     matchDispatch(&Node);
720   }
721 
722   // Implements ASTMatchFinder::getASTContext.
723   ASTContext &getASTContext() const override { return *ActiveASTContext; }
724 
725   bool shouldVisitTemplateInstantiations() const { return true; }
726   bool shouldVisitImplicitCode() const { return true; }
727 
728   // We visit the lambda body explicitly, so instruct the RAV
729   // to not visit it on our behalf too.
730   bool shouldVisitLambdaBody() const { return false; }
731 
732   bool IsMatchingInASTNodeNotSpelledInSource() const override {
733     return TraversingASTNodeNotSpelledInSource;
734   }
735   bool isMatchingChildrenNotSpelledInSource() const override {
736     return TraversingASTChildrenNotSpelledInSource;
737   }
738   void setMatchingChildrenNotSpelledInSource(bool Set) override {
739     TraversingASTChildrenNotSpelledInSource = Set;
740   }
741 
742   bool IsMatchingInASTNodeNotAsIs() const override {
743     return TraversingASTNodeNotAsIs;
744   }
745 
746   bool TraverseTemplateInstantiations(ClassTemplateDecl *D) {
747     ASTNodeNotSpelledInSourceScope RAII(this, true);
748     return RecursiveASTVisitor<MatchASTVisitor>::TraverseTemplateInstantiations(
749         D);
750   }
751 
752   bool TraverseTemplateInstantiations(VarTemplateDecl *D) {
753     ASTNodeNotSpelledInSourceScope RAII(this, true);
754     return RecursiveASTVisitor<MatchASTVisitor>::TraverseTemplateInstantiations(
755         D);
756   }
757 
758   bool TraverseTemplateInstantiations(FunctionTemplateDecl *D) {
759     ASTNodeNotSpelledInSourceScope RAII(this, true);
760     return RecursiveASTVisitor<MatchASTVisitor>::TraverseTemplateInstantiations(
761         D);
762   }
763 
764   class TraceReporter : llvm::PrettyStackTraceEntry {
765   public:
766     TraceReporter(const MatchASTVisitor &MV) : MV(MV) {}
767     void print(raw_ostream &OS) const override {
768       if (!MV.CurMatched) {
769         OS << "ASTMatcher: Not currently matching\n";
770         return;
771       }
772       assert(MV.ActiveASTContext &&
773              "ActiveASTContext should be set if there is a matched callback");
774 
775       OS << "ASTMatcher: Processing '" << MV.CurMatched->getID() << "'\n";
776       const BoundNodes::IDToNodeMap &Map = MV.CurBoundNodes->getMap();
777       if (Map.empty()) {
778         OS << "No bound nodes\n";
779         return;
780       }
781       OS << "--- Bound Nodes Begin ---\n";
782       for (const auto &Item : Map) {
783         OS << "    " << Item.first << " - { ";
784         if (const auto *D = Item.second.get<Decl>()) {
785           OS << D->getDeclKindName() << "Decl ";
786           if (const auto *ND = dyn_cast<NamedDecl>(D)) {
787             ND->printQualifiedName(OS);
788             OS << " : ";
789           } else
790             OS << ": ";
791           D->getSourceRange().print(OS,
792                                     MV.ActiveASTContext->getSourceManager());
793         } else if (const auto *S = Item.second.get<Stmt>()) {
794           OS << S->getStmtClassName() << " : ";
795           S->getSourceRange().print(OS,
796                                     MV.ActiveASTContext->getSourceManager());
797         } else if (const auto *T = Item.second.get<Type>()) {
798           OS << T->getTypeClassName() << "Type : ";
799           QualType(T, 0).print(OS, MV.ActiveASTContext->getPrintingPolicy());
800         } else if (const auto *QT = Item.second.get<QualType>()) {
801           OS << "QualType : ";
802           QT->print(OS, MV.ActiveASTContext->getPrintingPolicy());
803         } else {
804           OS << Item.second.getNodeKind().asStringRef() << " : ";
805           Item.second.getSourceRange().print(
806               OS, MV.ActiveASTContext->getSourceManager());
807         }
808         OS << " }\n";
809       }
810       OS << "--- Bound Nodes End ---\n";
811     }
812 
813   private:
814     const MatchASTVisitor &MV;
815   };
816 
817 private:
818   bool TraversingASTNodeNotSpelledInSource = false;
819   bool TraversingASTNodeNotAsIs = false;
820   bool TraversingASTChildrenNotSpelledInSource = false;
821 
822   const MatchCallback *CurMatched = nullptr;
823   const BoundNodes *CurBoundNodes = nullptr;
824 
825   struct ASTNodeNotSpelledInSourceScope {
826     ASTNodeNotSpelledInSourceScope(MatchASTVisitor *V, bool B)
827         : MV(V), MB(V->TraversingASTNodeNotSpelledInSource) {
828       V->TraversingASTNodeNotSpelledInSource = B;
829     }
830     ~ASTNodeNotSpelledInSourceScope() {
831       MV->TraversingASTNodeNotSpelledInSource = MB;
832     }
833 
834   private:
835     MatchASTVisitor *MV;
836     bool MB;
837   };
838 
839   struct ASTNodeNotAsIsSourceScope {
840     ASTNodeNotAsIsSourceScope(MatchASTVisitor *V, bool B)
841         : MV(V), MB(V->TraversingASTNodeNotAsIs) {
842       V->TraversingASTNodeNotAsIs = B;
843     }
844     ~ASTNodeNotAsIsSourceScope() { MV->TraversingASTNodeNotAsIs = MB; }
845 
846   private:
847     MatchASTVisitor *MV;
848     bool MB;
849   };
850 
851   class TimeBucketRegion {
852   public:
853     TimeBucketRegion() : Bucket(nullptr) {}
854     ~TimeBucketRegion() { setBucket(nullptr); }
855 
856     /// Start timing for \p NewBucket.
857     ///
858     /// If there was a bucket already set, it will finish the timing for that
859     /// other bucket.
860     /// \p NewBucket will be timed until the next call to \c setBucket() or
861     /// until the \c TimeBucketRegion is destroyed.
862     /// If \p NewBucket is the same as the currently timed bucket, this call
863     /// does nothing.
864     void setBucket(llvm::TimeRecord *NewBucket) {
865       if (Bucket != NewBucket) {
866         auto Now = llvm::TimeRecord::getCurrentTime(true);
867         if (Bucket)
868           *Bucket += Now;
869         if (NewBucket)
870           *NewBucket -= Now;
871         Bucket = NewBucket;
872       }
873     }
874 
875   private:
876     llvm::TimeRecord *Bucket;
877   };
878 
879   /// Runs all the \p Matchers on \p Node.
880   ///
881   /// Used by \c matchDispatch() below.
882   template <typename T, typename MC>
883   void matchWithoutFilter(const T &Node, const MC &Matchers) {
884     const bool EnableCheckProfiling = Options.CheckProfiling.hasValue();
885     TimeBucketRegion Timer;
886     for (const auto &MP : Matchers) {
887       if (EnableCheckProfiling)
888         Timer.setBucket(&TimeByBucket[MP.second->getID()]);
889       BoundNodesTreeBuilder Builder;
890       if (MP.first.matches(Node, this, &Builder)) {
891         MatchVisitor Visitor(*this, ActiveASTContext, MP.second);
892         Builder.visitMatches(&Visitor);
893       }
894     }
895   }
896 
897   void matchWithFilter(const DynTypedNode &DynNode) {
898     auto Kind = DynNode.getNodeKind();
899     auto it = MatcherFiltersMap.find(Kind);
900     const auto &Filter =
901         it != MatcherFiltersMap.end() ? it->second : getFilterForKind(Kind);
902 
903     if (Filter.empty())
904       return;
905 
906     const bool EnableCheckProfiling = Options.CheckProfiling.hasValue();
907     TimeBucketRegion Timer;
908     auto &Matchers = this->Matchers->DeclOrStmt;
909     for (unsigned short I : Filter) {
910       auto &MP = Matchers[I];
911       if (EnableCheckProfiling)
912         Timer.setBucket(&TimeByBucket[MP.second->getID()]);
913       BoundNodesTreeBuilder Builder;
914 
915       {
916         TraversalKindScope RAII(getASTContext(), MP.first.getTraversalKind());
917         if (getASTContext().getParentMapContext().traverseIgnored(DynNode) !=
918             DynNode)
919           continue;
920       }
921 
922       if (MP.first.matches(DynNode, this, &Builder)) {
923         MatchVisitor Visitor(*this, ActiveASTContext, MP.second);
924         Builder.visitMatches(&Visitor);
925       }
926     }
927   }
928 
929   const std::vector<unsigned short> &getFilterForKind(ASTNodeKind Kind) {
930     auto &Filter = MatcherFiltersMap[Kind];
931     auto &Matchers = this->Matchers->DeclOrStmt;
932     assert((Matchers.size() < USHRT_MAX) && "Too many matchers.");
933     for (unsigned I = 0, E = Matchers.size(); I != E; ++I) {
934       if (Matchers[I].first.canMatchNodesOfKind(Kind)) {
935         Filter.push_back(I);
936       }
937     }
938     return Filter;
939   }
940 
941   /// @{
942   /// Overloads to pair the different node types to their matchers.
943   void matchDispatch(const Decl *Node) {
944     return matchWithFilter(DynTypedNode::create(*Node));
945   }
946   void matchDispatch(const Stmt *Node) {
947     return matchWithFilter(DynTypedNode::create(*Node));
948   }
949 
950   void matchDispatch(const Type *Node) {
951     matchWithoutFilter(QualType(Node, 0), Matchers->Type);
952   }
953   void matchDispatch(const TypeLoc *Node) {
954     matchWithoutFilter(*Node, Matchers->TypeLoc);
955   }
956   void matchDispatch(const QualType *Node) {
957     matchWithoutFilter(*Node, Matchers->Type);
958   }
959   void matchDispatch(const NestedNameSpecifier *Node) {
960     matchWithoutFilter(*Node, Matchers->NestedNameSpecifier);
961   }
962   void matchDispatch(const NestedNameSpecifierLoc *Node) {
963     matchWithoutFilter(*Node, Matchers->NestedNameSpecifierLoc);
964   }
965   void matchDispatch(const CXXCtorInitializer *Node) {
966     matchWithoutFilter(*Node, Matchers->CtorInit);
967   }
968   void matchDispatch(const TemplateArgumentLoc *Node) {
969     matchWithoutFilter(*Node, Matchers->TemplateArgumentLoc);
970   }
971   void matchDispatch(const Attr *Node) {
972     matchWithoutFilter(*Node, Matchers->Attr);
973   }
974   void matchDispatch(const void *) { /* Do nothing. */ }
975   /// @}
976 
977   // Returns whether a direct parent of \p Node matches \p Matcher.
978   // Unlike matchesAnyAncestorOf there's no memoization: it doesn't save much.
979   bool matchesParentOf(const DynTypedNode &Node, const DynTypedMatcher &Matcher,
980                        BoundNodesTreeBuilder *Builder) {
981     for (const auto &Parent : ActiveASTContext->getParents(Node)) {
982       BoundNodesTreeBuilder BuilderCopy = *Builder;
983       if (Matcher.matches(Parent, this, &BuilderCopy)) {
984         *Builder = std::move(BuilderCopy);
985         return true;
986       }
987     }
988     return false;
989   }
990 
991   // Returns whether an ancestor of \p Node matches \p Matcher.
992   //
993   // The order of matching (which can lead to different nodes being bound in
994   // case there are multiple matches) is breadth first search.
995   //
996   // To allow memoization in the very common case of having deeply nested
997   // expressions inside a template function, we first walk up the AST, memoizing
998   // the result of the match along the way, as long as there is only a single
999   // parent.
1000   //
1001   // Once there are multiple parents, the breadth first search order does not
1002   // allow simple memoization on the ancestors. Thus, we only memoize as long
1003   // as there is a single parent.
1004   //
1005   // We avoid a recursive implementation to prevent excessive stack use on
1006   // very deep ASTs (similarly to RecursiveASTVisitor's data recursion).
1007   bool matchesAnyAncestorOf(DynTypedNode Node, ASTContext &Ctx,
1008                             const DynTypedMatcher &Matcher,
1009                             BoundNodesTreeBuilder *Builder) {
1010 
1011     // Memoization keys that can be updated with the result.
1012     // These are the memoizable nodes in the chain of unique parents, which
1013     // terminates when a node has multiple parents, or matches, or is the root.
1014     std::vector<MatchKey> Keys;
1015     // When returning, update the memoization cache.
1016     auto Finish = [&](bool Matched) {
1017       for (const auto &Key : Keys) {
1018         MemoizedMatchResult &CachedResult = ResultCache[Key];
1019         CachedResult.ResultOfMatch = Matched;
1020         CachedResult.Nodes = *Builder;
1021       }
1022       return Matched;
1023     };
1024 
1025     // Loop while there's a single parent and we want to attempt memoization.
1026     DynTypedNodeList Parents{ArrayRef<DynTypedNode>()}; // after loop: size != 1
1027     for (;;) {
1028       // A cache key only makes sense if memoization is possible.
1029       if (Builder->isComparable()) {
1030         Keys.emplace_back();
1031         Keys.back().MatcherID = Matcher.getID();
1032         Keys.back().Node = Node;
1033         Keys.back().BoundNodes = *Builder;
1034         Keys.back().Traversal = Ctx.getParentMapContext().getTraversalKind();
1035         Keys.back().Type = MatchType::Ancestors;
1036 
1037         // Check the cache.
1038         MemoizationMap::iterator I = ResultCache.find(Keys.back());
1039         if (I != ResultCache.end()) {
1040           Keys.pop_back(); // Don't populate the cache for the matching node!
1041           *Builder = I->second.Nodes;
1042           return Finish(I->second.ResultOfMatch);
1043         }
1044       }
1045 
1046       Parents = ActiveASTContext->getParents(Node);
1047       // Either no parents or multiple parents: leave chain+memoize mode and
1048       // enter bfs+forgetful mode.
1049       if (Parents.size() != 1)
1050         break;
1051 
1052       // Check the next parent.
1053       Node = *Parents.begin();
1054       BoundNodesTreeBuilder BuilderCopy = *Builder;
1055       if (Matcher.matches(Node, this, &BuilderCopy)) {
1056         *Builder = std::move(BuilderCopy);
1057         return Finish(true);
1058       }
1059     }
1060     // We reached the end of the chain.
1061 
1062     if (Parents.empty()) {
1063       // Nodes may have no parents if:
1064       //  a) the node is the TranslationUnitDecl
1065       //  b) we have a limited traversal scope that excludes the parent edges
1066       //  c) there is a bug in the AST, and the node is not reachable
1067       // Usually the traversal scope is the whole AST, which precludes b.
1068       // Bugs are common enough that it's worthwhile asserting when we can.
1069 #ifndef NDEBUG
1070       if (!Node.get<TranslationUnitDecl>() &&
1071           /* Traversal scope is full AST if any of the bounds are the TU */
1072           llvm::any_of(ActiveASTContext->getTraversalScope(), [](Decl *D) {
1073             return D->getKind() == Decl::TranslationUnit;
1074           })) {
1075         llvm::errs() << "Tried to match orphan node:\n";
1076         Node.dump(llvm::errs(), *ActiveASTContext);
1077         llvm_unreachable("Parent map should be complete!");
1078       }
1079 #endif
1080     } else {
1081       assert(Parents.size() > 1);
1082       // BFS starting from the parents not yet considered.
1083       // Memoization of newly visited nodes is not possible (but we still update
1084       // results for the elements in the chain we found above).
1085       std::deque<DynTypedNode> Queue(Parents.begin(), Parents.end());
1086       llvm::DenseSet<const void *> Visited;
1087       while (!Queue.empty()) {
1088         BoundNodesTreeBuilder BuilderCopy = *Builder;
1089         if (Matcher.matches(Queue.front(), this, &BuilderCopy)) {
1090           *Builder = std::move(BuilderCopy);
1091           return Finish(true);
1092         }
1093         for (const auto &Parent : ActiveASTContext->getParents(Queue.front())) {
1094           // Make sure we do not visit the same node twice.
1095           // Otherwise, we'll visit the common ancestors as often as there
1096           // are splits on the way down.
1097           if (Visited.insert(Parent.getMemoizationData()).second)
1098             Queue.push_back(Parent);
1099         }
1100         Queue.pop_front();
1101       }
1102     }
1103     return Finish(false);
1104   }
1105 
1106   // Implements a BoundNodesTree::Visitor that calls a MatchCallback with
1107   // the aggregated bound nodes for each match.
1108   class MatchVisitor : public BoundNodesTreeBuilder::Visitor {
1109     struct CurBoundScope {
1110       CurBoundScope(MatchASTVisitor &MV, const BoundNodes &BN) : MV(MV) {
1111         assert(MV.CurMatched && !MV.CurBoundNodes);
1112         MV.CurBoundNodes = &BN;
1113       }
1114 
1115       ~CurBoundScope() { MV.CurBoundNodes = nullptr; }
1116 
1117     private:
1118       MatchASTVisitor &MV;
1119     };
1120 
1121   public:
1122     MatchVisitor(MatchASTVisitor &MV, ASTContext *Context,
1123                  MatchFinder::MatchCallback *Callback)
1124         : MV(MV), Context(Context), Callback(Callback) {
1125       assert(!MV.CurMatched && !MV.CurBoundNodes);
1126       MV.CurMatched = Callback;
1127     }
1128 
1129     ~MatchVisitor() { MV.CurMatched = nullptr; }
1130 
1131     void visitMatch(const BoundNodes& BoundNodesView) override {
1132       TraversalKindScope RAII(*Context, Callback->getCheckTraversalKind());
1133       CurBoundScope RAII2(MV, BoundNodesView);
1134       Callback->run(MatchFinder::MatchResult(BoundNodesView, Context));
1135     }
1136 
1137   private:
1138     MatchASTVisitor &MV;
1139     ASTContext* Context;
1140     MatchFinder::MatchCallback* Callback;
1141   };
1142 
1143   // Returns true if 'TypeNode' has an alias that matches the given matcher.
1144   bool typeHasMatchingAlias(const Type *TypeNode,
1145                             const Matcher<NamedDecl> &Matcher,
1146                             BoundNodesTreeBuilder *Builder) {
1147     const Type *const CanonicalType =
1148       ActiveASTContext->getCanonicalType(TypeNode);
1149     auto Aliases = TypeAliases.find(CanonicalType);
1150     if (Aliases == TypeAliases.end())
1151       return false;
1152     for (const TypedefNameDecl *Alias : Aliases->second) {
1153       BoundNodesTreeBuilder Result(*Builder);
1154       if (Matcher.matches(*Alias, this, &Result)) {
1155         *Builder = std::move(Result);
1156         return true;
1157       }
1158     }
1159     return false;
1160   }
1161 
1162   bool
1163   objcClassHasMatchingCompatibilityAlias(const ObjCInterfaceDecl *InterfaceDecl,
1164                                          const Matcher<NamedDecl> &Matcher,
1165                                          BoundNodesTreeBuilder *Builder) {
1166     auto Aliases = CompatibleAliases.find(InterfaceDecl);
1167     if (Aliases == CompatibleAliases.end())
1168       return false;
1169     for (const ObjCCompatibleAliasDecl *Alias : Aliases->second) {
1170       BoundNodesTreeBuilder Result(*Builder);
1171       if (Matcher.matches(*Alias, this, &Result)) {
1172         *Builder = std::move(Result);
1173         return true;
1174       }
1175     }
1176     return false;
1177   }
1178 
1179   /// Bucket to record map.
1180   ///
1181   /// Used to get the appropriate bucket for each matcher.
1182   llvm::StringMap<llvm::TimeRecord> TimeByBucket;
1183 
1184   const MatchFinder::MatchersByType *Matchers;
1185 
1186   /// Filtered list of matcher indices for each matcher kind.
1187   ///
1188   /// \c Decl and \c Stmt toplevel matchers usually apply to a specific node
1189   /// kind (and derived kinds) so it is a waste to try every matcher on every
1190   /// node.
1191   /// We precalculate a list of matchers that pass the toplevel restrict check.
1192   llvm::DenseMap<ASTNodeKind, std::vector<unsigned short>> MatcherFiltersMap;
1193 
1194   const MatchFinder::MatchFinderOptions &Options;
1195   ASTContext *ActiveASTContext;
1196 
1197   // Maps a canonical type to its TypedefDecls.
1198   llvm::DenseMap<const Type*, std::set<const TypedefNameDecl*> > TypeAliases;
1199 
1200   // Maps an Objective-C interface to its ObjCCompatibleAliasDecls.
1201   llvm::DenseMap<const ObjCInterfaceDecl *,
1202                  llvm::SmallPtrSet<const ObjCCompatibleAliasDecl *, 2>>
1203       CompatibleAliases;
1204 
1205   // Maps (matcher, node) -> the match result for memoization.
1206   typedef std::map<MatchKey, MemoizedMatchResult> MemoizationMap;
1207   MemoizationMap ResultCache;
1208 };
1209 
1210 static CXXRecordDecl *
1211 getAsCXXRecordDeclOrPrimaryTemplate(const Type *TypeNode) {
1212   if (auto *RD = TypeNode->getAsCXXRecordDecl())
1213     return RD;
1214 
1215   // Find the innermost TemplateSpecializationType that isn't an alias template.
1216   auto *TemplateType = TypeNode->getAs<TemplateSpecializationType>();
1217   while (TemplateType && TemplateType->isTypeAlias())
1218     TemplateType =
1219         TemplateType->getAliasedType()->getAs<TemplateSpecializationType>();
1220 
1221   // If this is the name of a (dependent) template specialization, use the
1222   // definition of the template, even though it might be specialized later.
1223   if (TemplateType)
1224     if (auto *ClassTemplate = dyn_cast_or_null<ClassTemplateDecl>(
1225           TemplateType->getTemplateName().getAsTemplateDecl()))
1226       return ClassTemplate->getTemplatedDecl();
1227 
1228   return nullptr;
1229 }
1230 
1231 // Returns true if the given C++ class is directly or indirectly derived
1232 // from a base type with the given name.  A class is not considered to be
1233 // derived from itself.
1234 bool MatchASTVisitor::classIsDerivedFrom(const CXXRecordDecl *Declaration,
1235                                          const Matcher<NamedDecl> &Base,
1236                                          BoundNodesTreeBuilder *Builder,
1237                                          bool Directly) {
1238   if (!Declaration->hasDefinition())
1239     return false;
1240   for (const auto &It : Declaration->bases()) {
1241     const Type *TypeNode = It.getType().getTypePtr();
1242 
1243     if (typeHasMatchingAlias(TypeNode, Base, Builder))
1244       return true;
1245 
1246     // FIXME: Going to the primary template here isn't really correct, but
1247     // unfortunately we accept a Decl matcher for the base class not a Type
1248     // matcher, so it's the best thing we can do with our current interface.
1249     CXXRecordDecl *ClassDecl = getAsCXXRecordDeclOrPrimaryTemplate(TypeNode);
1250     if (!ClassDecl)
1251       continue;
1252     if (ClassDecl == Declaration) {
1253       // This can happen for recursive template definitions.
1254       continue;
1255     }
1256     BoundNodesTreeBuilder Result(*Builder);
1257     if (Base.matches(*ClassDecl, this, &Result)) {
1258       *Builder = std::move(Result);
1259       return true;
1260     }
1261     if (!Directly && classIsDerivedFrom(ClassDecl, Base, Builder, Directly))
1262       return true;
1263   }
1264   return false;
1265 }
1266 
1267 // Returns true if the given Objective-C class is directly or indirectly
1268 // derived from a matching base class. A class is not considered to be derived
1269 // from itself.
1270 bool MatchASTVisitor::objcClassIsDerivedFrom(
1271     const ObjCInterfaceDecl *Declaration, const Matcher<NamedDecl> &Base,
1272     BoundNodesTreeBuilder *Builder, bool Directly) {
1273   // Check if any of the superclasses of the class match.
1274   for (const ObjCInterfaceDecl *ClassDecl = Declaration->getSuperClass();
1275        ClassDecl != nullptr; ClassDecl = ClassDecl->getSuperClass()) {
1276     // Check if there are any matching compatibility aliases.
1277     if (objcClassHasMatchingCompatibilityAlias(ClassDecl, Base, Builder))
1278       return true;
1279 
1280     // Check if there are any matching type aliases.
1281     const Type *TypeNode = ClassDecl->getTypeForDecl();
1282     if (typeHasMatchingAlias(TypeNode, Base, Builder))
1283       return true;
1284 
1285     if (Base.matches(*ClassDecl, this, Builder))
1286       return true;
1287 
1288     // Not `return false` as a temporary workaround for PR43879.
1289     if (Directly)
1290       break;
1291   }
1292 
1293   return false;
1294 }
1295 
1296 bool MatchASTVisitor::TraverseDecl(Decl *DeclNode) {
1297   if (!DeclNode) {
1298     return true;
1299   }
1300 
1301   bool ScopedTraversal =
1302       TraversingASTNodeNotSpelledInSource || DeclNode->isImplicit();
1303   bool ScopedChildren = TraversingASTChildrenNotSpelledInSource;
1304 
1305   if (const auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(DeclNode)) {
1306     auto SK = CTSD->getSpecializationKind();
1307     if (SK == TSK_ExplicitInstantiationDeclaration ||
1308         SK == TSK_ExplicitInstantiationDefinition)
1309       ScopedChildren = true;
1310   } else if (const auto *FD = dyn_cast<FunctionDecl>(DeclNode)) {
1311     if (FD->isDefaulted())
1312       ScopedChildren = true;
1313     if (FD->isTemplateInstantiation())
1314       ScopedTraversal = true;
1315   } else if (isa<BindingDecl>(DeclNode)) {
1316     ScopedChildren = true;
1317   }
1318 
1319   ASTNodeNotSpelledInSourceScope RAII1(this, ScopedTraversal);
1320   ASTChildrenNotSpelledInSourceScope RAII2(this, ScopedChildren);
1321 
1322   match(*DeclNode);
1323   return RecursiveASTVisitor<MatchASTVisitor>::TraverseDecl(DeclNode);
1324 }
1325 
1326 bool MatchASTVisitor::TraverseStmt(Stmt *StmtNode, DataRecursionQueue *Queue) {
1327   if (!StmtNode) {
1328     return true;
1329   }
1330   bool ScopedTraversal = TraversingASTNodeNotSpelledInSource ||
1331                          TraversingASTChildrenNotSpelledInSource;
1332 
1333   ASTNodeNotSpelledInSourceScope RAII(this, ScopedTraversal);
1334   match(*StmtNode);
1335   return RecursiveASTVisitor<MatchASTVisitor>::TraverseStmt(StmtNode, Queue);
1336 }
1337 
1338 bool MatchASTVisitor::TraverseType(QualType TypeNode) {
1339   match(TypeNode);
1340   return RecursiveASTVisitor<MatchASTVisitor>::TraverseType(TypeNode);
1341 }
1342 
1343 bool MatchASTVisitor::TraverseTypeLoc(TypeLoc TypeLocNode) {
1344   // The RecursiveASTVisitor only visits types if they're not within TypeLocs.
1345   // We still want to find those types via matchers, so we match them here. Note
1346   // that the TypeLocs are structurally a shadow-hierarchy to the expressed
1347   // type, so we visit all involved parts of a compound type when matching on
1348   // each TypeLoc.
1349   match(TypeLocNode);
1350   match(TypeLocNode.getType());
1351   return RecursiveASTVisitor<MatchASTVisitor>::TraverseTypeLoc(TypeLocNode);
1352 }
1353 
1354 bool MatchASTVisitor::TraverseNestedNameSpecifier(NestedNameSpecifier *NNS) {
1355   match(*NNS);
1356   return RecursiveASTVisitor<MatchASTVisitor>::TraverseNestedNameSpecifier(NNS);
1357 }
1358 
1359 bool MatchASTVisitor::TraverseNestedNameSpecifierLoc(
1360     NestedNameSpecifierLoc NNS) {
1361   if (!NNS)
1362     return true;
1363 
1364   match(NNS);
1365 
1366   // We only match the nested name specifier here (as opposed to traversing it)
1367   // because the traversal is already done in the parallel "Loc"-hierarchy.
1368   if (NNS.hasQualifier())
1369     match(*NNS.getNestedNameSpecifier());
1370   return
1371       RecursiveASTVisitor<MatchASTVisitor>::TraverseNestedNameSpecifierLoc(NNS);
1372 }
1373 
1374 bool MatchASTVisitor::TraverseConstructorInitializer(
1375     CXXCtorInitializer *CtorInit) {
1376   if (!CtorInit)
1377     return true;
1378 
1379   bool ScopedTraversal = TraversingASTNodeNotSpelledInSource ||
1380                          TraversingASTChildrenNotSpelledInSource;
1381 
1382   if (!CtorInit->isWritten())
1383     ScopedTraversal = true;
1384 
1385   ASTNodeNotSpelledInSourceScope RAII1(this, ScopedTraversal);
1386 
1387   match(*CtorInit);
1388 
1389   return RecursiveASTVisitor<MatchASTVisitor>::TraverseConstructorInitializer(
1390       CtorInit);
1391 }
1392 
1393 bool MatchASTVisitor::TraverseTemplateArgumentLoc(TemplateArgumentLoc Loc) {
1394   match(Loc);
1395   return RecursiveASTVisitor<MatchASTVisitor>::TraverseTemplateArgumentLoc(Loc);
1396 }
1397 
1398 bool MatchASTVisitor::TraverseAttr(Attr *AttrNode) {
1399   match(*AttrNode);
1400   return RecursiveASTVisitor<MatchASTVisitor>::TraverseAttr(AttrNode);
1401 }
1402 
1403 class MatchASTConsumer : public ASTConsumer {
1404 public:
1405   MatchASTConsumer(MatchFinder *Finder,
1406                    MatchFinder::ParsingDoneTestCallback *ParsingDone)
1407       : Finder(Finder), ParsingDone(ParsingDone) {}
1408 
1409 private:
1410   void HandleTranslationUnit(ASTContext &Context) override {
1411     if (ParsingDone != nullptr) {
1412       ParsingDone->run();
1413     }
1414     Finder->matchAST(Context);
1415   }
1416 
1417   MatchFinder *Finder;
1418   MatchFinder::ParsingDoneTestCallback *ParsingDone;
1419 };
1420 
1421 } // end namespace
1422 } // end namespace internal
1423 
1424 MatchFinder::MatchResult::MatchResult(const BoundNodes &Nodes,
1425                                       ASTContext *Context)
1426   : Nodes(Nodes), Context(Context),
1427     SourceManager(&Context->getSourceManager()) {}
1428 
1429 MatchFinder::MatchCallback::~MatchCallback() {}
1430 MatchFinder::ParsingDoneTestCallback::~ParsingDoneTestCallback() {}
1431 
1432 MatchFinder::MatchFinder(MatchFinderOptions Options)
1433     : Options(std::move(Options)), ParsingDone(nullptr) {}
1434 
1435 MatchFinder::~MatchFinder() {}
1436 
1437 void MatchFinder::addMatcher(const DeclarationMatcher &NodeMatch,
1438                              MatchCallback *Action) {
1439   llvm::Optional<TraversalKind> TK;
1440   if (Action)
1441     TK = Action->getCheckTraversalKind();
1442   if (TK)
1443     Matchers.DeclOrStmt.emplace_back(traverse(*TK, NodeMatch), Action);
1444   else
1445     Matchers.DeclOrStmt.emplace_back(NodeMatch, Action);
1446   Matchers.AllCallbacks.insert(Action);
1447 }
1448 
1449 void MatchFinder::addMatcher(const TypeMatcher &NodeMatch,
1450                              MatchCallback *Action) {
1451   Matchers.Type.emplace_back(NodeMatch, Action);
1452   Matchers.AllCallbacks.insert(Action);
1453 }
1454 
1455 void MatchFinder::addMatcher(const StatementMatcher &NodeMatch,
1456                              MatchCallback *Action) {
1457   llvm::Optional<TraversalKind> TK;
1458   if (Action)
1459     TK = Action->getCheckTraversalKind();
1460   if (TK)
1461     Matchers.DeclOrStmt.emplace_back(traverse(*TK, NodeMatch), Action);
1462   else
1463     Matchers.DeclOrStmt.emplace_back(NodeMatch, Action);
1464   Matchers.AllCallbacks.insert(Action);
1465 }
1466 
1467 void MatchFinder::addMatcher(const NestedNameSpecifierMatcher &NodeMatch,
1468                              MatchCallback *Action) {
1469   Matchers.NestedNameSpecifier.emplace_back(NodeMatch, Action);
1470   Matchers.AllCallbacks.insert(Action);
1471 }
1472 
1473 void MatchFinder::addMatcher(const NestedNameSpecifierLocMatcher &NodeMatch,
1474                              MatchCallback *Action) {
1475   Matchers.NestedNameSpecifierLoc.emplace_back(NodeMatch, Action);
1476   Matchers.AllCallbacks.insert(Action);
1477 }
1478 
1479 void MatchFinder::addMatcher(const TypeLocMatcher &NodeMatch,
1480                              MatchCallback *Action) {
1481   Matchers.TypeLoc.emplace_back(NodeMatch, Action);
1482   Matchers.AllCallbacks.insert(Action);
1483 }
1484 
1485 void MatchFinder::addMatcher(const CXXCtorInitializerMatcher &NodeMatch,
1486                              MatchCallback *Action) {
1487   Matchers.CtorInit.emplace_back(NodeMatch, Action);
1488   Matchers.AllCallbacks.insert(Action);
1489 }
1490 
1491 void MatchFinder::addMatcher(const TemplateArgumentLocMatcher &NodeMatch,
1492                              MatchCallback *Action) {
1493   Matchers.TemplateArgumentLoc.emplace_back(NodeMatch, Action);
1494   Matchers.AllCallbacks.insert(Action);
1495 }
1496 
1497 void MatchFinder::addMatcher(const AttrMatcher &AttrMatch,
1498                              MatchCallback *Action) {
1499   Matchers.Attr.emplace_back(AttrMatch, Action);
1500   Matchers.AllCallbacks.insert(Action);
1501 }
1502 
1503 bool MatchFinder::addDynamicMatcher(const internal::DynTypedMatcher &NodeMatch,
1504                                     MatchCallback *Action) {
1505   if (NodeMatch.canConvertTo<Decl>()) {
1506     addMatcher(NodeMatch.convertTo<Decl>(), Action);
1507     return true;
1508   } else if (NodeMatch.canConvertTo<QualType>()) {
1509     addMatcher(NodeMatch.convertTo<QualType>(), Action);
1510     return true;
1511   } else if (NodeMatch.canConvertTo<Stmt>()) {
1512     addMatcher(NodeMatch.convertTo<Stmt>(), Action);
1513     return true;
1514   } else if (NodeMatch.canConvertTo<NestedNameSpecifier>()) {
1515     addMatcher(NodeMatch.convertTo<NestedNameSpecifier>(), Action);
1516     return true;
1517   } else if (NodeMatch.canConvertTo<NestedNameSpecifierLoc>()) {
1518     addMatcher(NodeMatch.convertTo<NestedNameSpecifierLoc>(), Action);
1519     return true;
1520   } else if (NodeMatch.canConvertTo<TypeLoc>()) {
1521     addMatcher(NodeMatch.convertTo<TypeLoc>(), Action);
1522     return true;
1523   } else if (NodeMatch.canConvertTo<CXXCtorInitializer>()) {
1524     addMatcher(NodeMatch.convertTo<CXXCtorInitializer>(), Action);
1525     return true;
1526   } else if (NodeMatch.canConvertTo<TemplateArgumentLoc>()) {
1527     addMatcher(NodeMatch.convertTo<TemplateArgumentLoc>(), Action);
1528     return true;
1529   } else if (NodeMatch.canConvertTo<Attr>()) {
1530     addMatcher(NodeMatch.convertTo<Attr>(), Action);
1531     return true;
1532   }
1533   return false;
1534 }
1535 
1536 std::unique_ptr<ASTConsumer> MatchFinder::newASTConsumer() {
1537   return std::make_unique<internal::MatchASTConsumer>(this, ParsingDone);
1538 }
1539 
1540 void MatchFinder::match(const clang::DynTypedNode &Node, ASTContext &Context) {
1541   internal::MatchASTVisitor Visitor(&Matchers, Options);
1542   Visitor.set_active_ast_context(&Context);
1543   Visitor.match(Node);
1544 }
1545 
1546 void MatchFinder::matchAST(ASTContext &Context) {
1547   internal::MatchASTVisitor Visitor(&Matchers, Options);
1548   internal::MatchASTVisitor::TraceReporter StackTrace(Visitor);
1549   Visitor.set_active_ast_context(&Context);
1550   Visitor.onStartOfTranslationUnit();
1551   Visitor.TraverseAST(Context);
1552   Visitor.onEndOfTranslationUnit();
1553 }
1554 
1555 void MatchFinder::registerTestCallbackAfterParsing(
1556     MatchFinder::ParsingDoneTestCallback *NewParsingDone) {
1557   ParsingDone = NewParsingDone;
1558 }
1559 
1560 StringRef MatchFinder::MatchCallback::getID() const { return "<unknown>"; }
1561 
1562 llvm::Optional<TraversalKind>
1563 MatchFinder::MatchCallback::getCheckTraversalKind() const {
1564   return llvm::None;
1565 }
1566 
1567 } // end namespace ast_matchers
1568 } // end namespace clang
1569