| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1 | //===--- ASTMatchFinder.cpp - Structural query framework ------------------===// | 
|  | 2 | // | 
|  | 3 | //                     The LLVM Compiler Infrastructure | 
|  | 4 | // | 
|  | 5 | // This file is distributed under the University of Illinois Open Source | 
|  | 6 | // License. See LICENSE.TXT for details. | 
|  | 7 | // | 
|  | 8 | //===----------------------------------------------------------------------===// | 
|  | 9 | // | 
|  | 10 | //  Implements an algorithm to efficiently search for matches on AST nodes. | 
|  | 11 | //  Uses memoization to support recursive matches like HasDescendant. | 
|  | 12 | // | 
|  | 13 | //  The general idea is to visit all AST nodes with a RecursiveASTVisitor, | 
|  | 14 | //  calling the Matches(...) method of each matcher we are running on each | 
|  | 15 | //  AST node. The matcher can recurse via the ASTMatchFinder interface. | 
|  | 16 | // | 
|  | 17 | //===----------------------------------------------------------------------===// | 
|  | 18 |  | 
|  | 19 | #include "clang/ASTMatchers/ASTMatchFinder.h" | 
|  | 20 | #include "clang/AST/ASTConsumer.h" | 
|  | 21 | #include "clang/AST/ASTContext.h" | 
|  | 22 | #include "clang/AST/RecursiveASTVisitor.h" | 
| Manuel Klimek | b64d6b7 | 2013-03-14 16:33:21 +0000 | [diff] [blame] | 23 | #include <deque> | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 24 | #include <set> | 
|  | 25 |  | 
|  | 26 | namespace clang { | 
|  | 27 | namespace ast_matchers { | 
|  | 28 | namespace internal { | 
|  | 29 | namespace { | 
|  | 30 |  | 
| Manuel Klimek | eb958de | 2012-09-05 12:12:07 +0000 | [diff] [blame] | 31 | typedef MatchFinder::MatchCallback MatchCallback; | 
|  | 32 |  | 
| Manuel Klimek | a0c025f | 2013-06-19 15:42:45 +0000 | [diff] [blame] | 33 | // The maximum number of memoization entries to store. | 
|  | 34 | // 10k has been experimentally found to give a good trade-off | 
|  | 35 | // of performance vs. memory consumption by running matcher | 
|  | 36 | // that match on every statement over a very large codebase. | 
|  | 37 | // | 
|  | 38 | // FIXME: Do some performance optimization in general and | 
|  | 39 | // revisit this number; also, put up micro-benchmarks that we can | 
|  | 40 | // optimize this on. | 
|  | 41 | static const unsigned MaxMemoizationEntries = 10000; | 
|  | 42 |  | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 43 | // We use memoization to avoid running the same matcher on the same | 
| Manuel Klimek | a0c025f | 2013-06-19 15:42:45 +0000 | [diff] [blame] | 44 | // AST node twice.  This struct is the key for looking up match | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 45 | // result.  It consists of an ID of the MatcherInterface (for | 
| Manuel Klimek | a0c025f | 2013-06-19 15:42:45 +0000 | [diff] [blame] | 46 | // identifying the matcher), a pointer to the AST node and the | 
|  | 47 | // bound nodes before the matcher was executed. | 
| Manuel Klimek | eb958de | 2012-09-05 12:12:07 +0000 | [diff] [blame] | 48 | // | 
|  | 49 | // We currently only memoize on nodes whose pointers identify the | 
|  | 50 | // nodes (\c Stmt and \c Decl, but not \c QualType or \c TypeLoc). | 
|  | 51 | // For \c QualType and \c TypeLoc it is possible to implement | 
|  | 52 | // generation of keys for each type. | 
|  | 53 | // FIXME: Benchmark whether memoization of non-pointer typed nodes | 
|  | 54 | // provides enough benefit for the additional amount of code. | 
| Manuel Klimek | a0c025f | 2013-06-19 15:42:45 +0000 | [diff] [blame] | 55 | struct MatchKey { | 
|  | 56 | uint64_t MatcherID; | 
|  | 57 | ast_type_traits::DynTypedNode Node; | 
|  | 58 | BoundNodesTreeBuilder BoundNodes; | 
|  | 59 |  | 
|  | 60 | bool operator<(const MatchKey &Other) const { | 
|  | 61 | if (MatcherID != Other.MatcherID) | 
|  | 62 | return MatcherID < Other.MatcherID; | 
|  | 63 | if (Node != Other.Node) | 
|  | 64 | return Node < Other.Node; | 
|  | 65 | return BoundNodes < Other.BoundNodes; | 
|  | 66 | } | 
|  | 67 | }; | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 68 |  | 
|  | 69 | // Used to store the result of a match and possibly bound nodes. | 
|  | 70 | struct MemoizedMatchResult { | 
|  | 71 | bool ResultOfMatch; | 
| Manuel Klimek | a0c025f | 2013-06-19 15:42:45 +0000 | [diff] [blame] | 72 | BoundNodesTreeBuilder Nodes; | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 73 | }; | 
|  | 74 |  | 
|  | 75 | // A RecursiveASTVisitor that traverses all children or all descendants of | 
|  | 76 | // a node. | 
|  | 77 | class MatchChildASTVisitor | 
| Daniel Jasper | 1dad183 | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 78 | : public RecursiveASTVisitor<MatchChildASTVisitor> { | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 79 | public: | 
| Daniel Jasper | 1dad183 | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 80 | typedef RecursiveASTVisitor<MatchChildASTVisitor> VisitorBase; | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 81 |  | 
|  | 82 | // Creates an AST visitor that matches 'matcher' on all children or | 
|  | 83 | // descendants of a traversed node. max_depth is the maximum depth | 
|  | 84 | // to traverse: use 1 for matching the children and INT_MAX for | 
|  | 85 | // matching the descendants. | 
| Manuel Klimek | eb958de | 2012-09-05 12:12:07 +0000 | [diff] [blame] | 86 | MatchChildASTVisitor(const DynTypedMatcher *Matcher, | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 87 | ASTMatchFinder *Finder, | 
|  | 88 | BoundNodesTreeBuilder *Builder, | 
|  | 89 | int MaxDepth, | 
|  | 90 | ASTMatchFinder::TraversalKind Traversal, | 
|  | 91 | ASTMatchFinder::BindKind Bind) | 
| Manuel Klimek | eb958de | 2012-09-05 12:12:07 +0000 | [diff] [blame] | 92 | : Matcher(Matcher), | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 93 | Finder(Finder), | 
|  | 94 | Builder(Builder), | 
| Daniel Jasper | d29d5fa | 2012-10-29 10:14:44 +0000 | [diff] [blame] | 95 | CurrentDepth(0), | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 96 | MaxDepth(MaxDepth), | 
|  | 97 | Traversal(Traversal), | 
|  | 98 | Bind(Bind), | 
|  | 99 | Matches(false) {} | 
|  | 100 |  | 
|  | 101 | // Returns true if a match is found in the subtree rooted at the | 
|  | 102 | // given AST node. This is done via a set of mutually recursive | 
|  | 103 | // functions. Here's how the recursion is done (the  *wildcard can | 
|  | 104 | // actually be Decl, Stmt, or Type): | 
|  | 105 | // | 
|  | 106 | //   - Traverse(node) calls BaseTraverse(node) when it needs | 
|  | 107 | //     to visit the descendants of node. | 
|  | 108 | //   - BaseTraverse(node) then calls (via VisitorBase::Traverse*(node)) | 
|  | 109 | //     Traverse*(c) for each child c of 'node'. | 
|  | 110 | //   - Traverse*(c) in turn calls Traverse(c), completing the | 
|  | 111 | //     recursion. | 
| Manuel Klimek | eb958de | 2012-09-05 12:12:07 +0000 | [diff] [blame] | 112 | bool findMatch(const ast_type_traits::DynTypedNode &DynNode) { | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 113 | reset(); | 
| Manuel Klimek | eb958de | 2012-09-05 12:12:07 +0000 | [diff] [blame] | 114 | if (const Decl *D = DynNode.get<Decl>()) | 
|  | 115 | traverse(*D); | 
|  | 116 | else if (const Stmt *S = DynNode.get<Stmt>()) | 
|  | 117 | traverse(*S); | 
| Daniel Jasper | 6fc3433 | 2012-10-30 15:42:00 +0000 | [diff] [blame] | 118 | else if (const NestedNameSpecifier *NNS = | 
|  | 119 | DynNode.get<NestedNameSpecifier>()) | 
|  | 120 | traverse(*NNS); | 
|  | 121 | else if (const NestedNameSpecifierLoc *NNSLoc = | 
|  | 122 | DynNode.get<NestedNameSpecifierLoc>()) | 
|  | 123 | traverse(*NNSLoc); | 
| Daniel Jasper | d29d5fa | 2012-10-29 10:14:44 +0000 | [diff] [blame] | 124 | else if (const QualType *Q = DynNode.get<QualType>()) | 
|  | 125 | traverse(*Q); | 
|  | 126 | else if (const TypeLoc *T = DynNode.get<TypeLoc>()) | 
|  | 127 | traverse(*T); | 
| Manuel Klimek | eb958de | 2012-09-05 12:12:07 +0000 | [diff] [blame] | 128 | // FIXME: Add other base types after adding tests. | 
| Manuel Klimek | a0c025f | 2013-06-19 15:42:45 +0000 | [diff] [blame] | 129 |  | 
|  | 130 | // It's OK to always overwrite the bound nodes, as if there was | 
|  | 131 | // no match in this recursive branch, the result set is empty | 
|  | 132 | // anyway. | 
|  | 133 | *Builder = ResultBindings; | 
|  | 134 |  | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 135 | return Matches; | 
|  | 136 | } | 
|  | 137 |  | 
|  | 138 | // The following are overriding methods from the base visitor class. | 
|  | 139 | // They are public only to allow CRTP to work. They are *not *part | 
|  | 140 | // of the public API of this class. | 
| Daniel Jasper | 1dad183 | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 141 | bool TraverseDecl(Decl *DeclNode) { | 
| Daniel Jasper | d29d5fa | 2012-10-29 10:14:44 +0000 | [diff] [blame] | 142 | ScopedIncrement ScopedDepth(&CurrentDepth); | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 143 | return (DeclNode == NULL) || traverse(*DeclNode); | 
|  | 144 | } | 
| Daniel Jasper | 1dad183 | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 145 | bool TraverseStmt(Stmt *StmtNode) { | 
| Daniel Jasper | d29d5fa | 2012-10-29 10:14:44 +0000 | [diff] [blame] | 146 | ScopedIncrement ScopedDepth(&CurrentDepth); | 
| Daniel Jasper | 1dad183 | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 147 | const Stmt *StmtToTraverse = StmtNode; | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 148 | if (Traversal == | 
|  | 149 | ASTMatchFinder::TK_IgnoreImplicitCastsAndParentheses) { | 
| Daniel Jasper | 1dad183 | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 150 | const Expr *ExprNode = dyn_cast_or_null<Expr>(StmtNode); | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 151 | if (ExprNode != NULL) { | 
|  | 152 | StmtToTraverse = ExprNode->IgnoreParenImpCasts(); | 
|  | 153 | } | 
|  | 154 | } | 
|  | 155 | return (StmtToTraverse == NULL) || traverse(*StmtToTraverse); | 
|  | 156 | } | 
| Daniel Jasper | d29d5fa | 2012-10-29 10:14:44 +0000 | [diff] [blame] | 157 | // We assume that the QualType and the contained type are on the same | 
|  | 158 | // hierarchy level. Thus, we try to match either of them. | 
| Daniel Jasper | 1dad183 | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 159 | bool TraverseType(QualType TypeNode) { | 
| Daniel Jasper | a05bb6b | 2012-11-13 17:14:11 +0000 | [diff] [blame] | 160 | if (TypeNode.isNull()) | 
|  | 161 | return true; | 
| Daniel Jasper | d29d5fa | 2012-10-29 10:14:44 +0000 | [diff] [blame] | 162 | ScopedIncrement ScopedDepth(&CurrentDepth); | 
|  | 163 | // Match the Type. | 
|  | 164 | if (!match(*TypeNode)) | 
|  | 165 | return false; | 
|  | 166 | // The QualType is matched inside traverse. | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 167 | return traverse(TypeNode); | 
|  | 168 | } | 
| Daniel Jasper | d29d5fa | 2012-10-29 10:14:44 +0000 | [diff] [blame] | 169 | // We assume that the TypeLoc, contained QualType and contained Type all are | 
|  | 170 | // on the same hierarchy level. Thus, we try to match all of them. | 
|  | 171 | bool TraverseTypeLoc(TypeLoc TypeLocNode) { | 
| Daniel Jasper | a05bb6b | 2012-11-13 17:14:11 +0000 | [diff] [blame] | 172 | if (TypeLocNode.isNull()) | 
|  | 173 | return true; | 
| Daniel Jasper | d29d5fa | 2012-10-29 10:14:44 +0000 | [diff] [blame] | 174 | ScopedIncrement ScopedDepth(&CurrentDepth); | 
|  | 175 | // Match the Type. | 
|  | 176 | if (!match(*TypeLocNode.getType())) | 
|  | 177 | return false; | 
|  | 178 | // Match the QualType. | 
|  | 179 | if (!match(TypeLocNode.getType())) | 
|  | 180 | return false; | 
|  | 181 | // The TypeLoc is matched inside traverse. | 
|  | 182 | return traverse(TypeLocNode); | 
|  | 183 | } | 
| Daniel Jasper | 6fc3433 | 2012-10-30 15:42:00 +0000 | [diff] [blame] | 184 | bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS) { | 
|  | 185 | ScopedIncrement ScopedDepth(&CurrentDepth); | 
|  | 186 | return (NNS == NULL) || traverse(*NNS); | 
|  | 187 | } | 
|  | 188 | bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) { | 
| Daniel Jasper | a05bb6b | 2012-11-13 17:14:11 +0000 | [diff] [blame] | 189 | if (!NNS) | 
|  | 190 | return true; | 
| Daniel Jasper | 6fc3433 | 2012-10-30 15:42:00 +0000 | [diff] [blame] | 191 | ScopedIncrement ScopedDepth(&CurrentDepth); | 
|  | 192 | if (!match(*NNS.getNestedNameSpecifier())) | 
|  | 193 | return false; | 
| Daniel Jasper | a05bb6b | 2012-11-13 17:14:11 +0000 | [diff] [blame] | 194 | return traverse(NNS); | 
| Daniel Jasper | 6fc3433 | 2012-10-30 15:42:00 +0000 | [diff] [blame] | 195 | } | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 196 |  | 
|  | 197 | bool shouldVisitTemplateInstantiations() const { return true; } | 
|  | 198 | bool shouldVisitImplicitCode() const { return true; } | 
| Daniel Jasper | 0f9f019 | 2012-11-15 03:29:05 +0000 | [diff] [blame] | 199 | // Disables data recursion. We intercept Traverse* methods in the RAV, which | 
|  | 200 | // are not triggered during data recursion. | 
|  | 201 | bool shouldUseDataRecursionFor(clang::Stmt *S) const { return false; } | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 202 |  | 
|  | 203 | private: | 
|  | 204 | // Used for updating the depth during traversal. | 
|  | 205 | struct ScopedIncrement { | 
|  | 206 | explicit ScopedIncrement(int *Depth) : Depth(Depth) { ++(*Depth); } | 
|  | 207 | ~ScopedIncrement() { --(*Depth); } | 
|  | 208 |  | 
|  | 209 | private: | 
|  | 210 | int *Depth; | 
|  | 211 | }; | 
|  | 212 |  | 
|  | 213 | // Resets the state of this object. | 
|  | 214 | void reset() { | 
|  | 215 | Matches = false; | 
| Daniel Jasper | d29d5fa | 2012-10-29 10:14:44 +0000 | [diff] [blame] | 216 | CurrentDepth = 0; | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 217 | } | 
|  | 218 |  | 
|  | 219 | // Forwards the call to the corresponding Traverse*() method in the | 
|  | 220 | // base visitor class. | 
| Daniel Jasper | 1dad183 | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 221 | bool baseTraverse(const Decl &DeclNode) { | 
|  | 222 | return VisitorBase::TraverseDecl(const_cast<Decl*>(&DeclNode)); | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 223 | } | 
| Daniel Jasper | 1dad183 | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 224 | bool baseTraverse(const Stmt &StmtNode) { | 
|  | 225 | return VisitorBase::TraverseStmt(const_cast<Stmt*>(&StmtNode)); | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 226 | } | 
| Daniel Jasper | 1dad183 | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 227 | bool baseTraverse(QualType TypeNode) { | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 228 | return VisitorBase::TraverseType(TypeNode); | 
|  | 229 | } | 
| Daniel Jasper | d29d5fa | 2012-10-29 10:14:44 +0000 | [diff] [blame] | 230 | bool baseTraverse(TypeLoc TypeLocNode) { | 
|  | 231 | return VisitorBase::TraverseTypeLoc(TypeLocNode); | 
|  | 232 | } | 
| Daniel Jasper | 6fc3433 | 2012-10-30 15:42:00 +0000 | [diff] [blame] | 233 | bool baseTraverse(const NestedNameSpecifier &NNS) { | 
|  | 234 | return VisitorBase::TraverseNestedNameSpecifier( | 
|  | 235 | const_cast<NestedNameSpecifier*>(&NNS)); | 
|  | 236 | } | 
|  | 237 | bool baseTraverse(NestedNameSpecifierLoc NNS) { | 
|  | 238 | return VisitorBase::TraverseNestedNameSpecifierLoc(NNS); | 
|  | 239 | } | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 240 |  | 
| Daniel Jasper | d29d5fa | 2012-10-29 10:14:44 +0000 | [diff] [blame] | 241 | // Sets 'Matched' to true if 'Matcher' matches 'Node' and: | 
|  | 242 | //   0 < CurrentDepth <= MaxDepth. | 
|  | 243 | // | 
|  | 244 | // Returns 'true' if traversal should continue after this function | 
|  | 245 | // returns, i.e. if no match is found or 'Bind' is 'BK_All'. | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 246 | template <typename T> | 
| Daniel Jasper | d29d5fa | 2012-10-29 10:14:44 +0000 | [diff] [blame] | 247 | bool match(const T &Node) { | 
|  | 248 | if (CurrentDepth == 0 || CurrentDepth > MaxDepth) { | 
|  | 249 | return true; | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 250 | } | 
|  | 251 | if (Bind != ASTMatchFinder::BK_All) { | 
| Manuel Klimek | a0c025f | 2013-06-19 15:42:45 +0000 | [diff] [blame] | 252 | BoundNodesTreeBuilder RecursiveBuilder(*Builder); | 
|  | 253 | if (Matcher->matches(ast_type_traits::DynTypedNode::create(Node), Finder, | 
|  | 254 | &RecursiveBuilder)) { | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 255 | Matches = true; | 
| Manuel Klimek | a0c025f | 2013-06-19 15:42:45 +0000 | [diff] [blame] | 256 | ResultBindings.addMatch(RecursiveBuilder); | 
|  | 257 | return false; // Abort as soon as a match is found. | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 258 | } | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 259 | } else { | 
| Manuel Klimek | a0c025f | 2013-06-19 15:42:45 +0000 | [diff] [blame] | 260 | BoundNodesTreeBuilder RecursiveBuilder(*Builder); | 
|  | 261 | if (Matcher->matches(ast_type_traits::DynTypedNode::create(Node), Finder, | 
|  | 262 | &RecursiveBuilder)) { | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 263 | // After the first match the matcher succeeds. | 
|  | 264 | Matches = true; | 
| Manuel Klimek | a0c025f | 2013-06-19 15:42:45 +0000 | [diff] [blame] | 265 | ResultBindings.addMatch(RecursiveBuilder); | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 266 | } | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 267 | } | 
| Daniel Jasper | d29d5fa | 2012-10-29 10:14:44 +0000 | [diff] [blame] | 268 | return true; | 
|  | 269 | } | 
|  | 270 |  | 
|  | 271 | // Traverses the subtree rooted at 'Node'; returns true if the | 
|  | 272 | // traversal should continue after this function returns. | 
|  | 273 | template <typename T> | 
|  | 274 | bool traverse(const T &Node) { | 
|  | 275 | TOOLING_COMPILE_ASSERT(IsBaseType<T>::value, | 
|  | 276 | traverse_can_only_be_instantiated_with_base_type); | 
|  | 277 | if (!match(Node)) | 
|  | 278 | return false; | 
|  | 279 | return baseTraverse(Node); | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 280 | } | 
|  | 281 |  | 
| Manuel Klimek | eb958de | 2012-09-05 12:12:07 +0000 | [diff] [blame] | 282 | const DynTypedMatcher *const Matcher; | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 283 | ASTMatchFinder *const Finder; | 
|  | 284 | BoundNodesTreeBuilder *const Builder; | 
| Manuel Klimek | a0c025f | 2013-06-19 15:42:45 +0000 | [diff] [blame] | 285 | BoundNodesTreeBuilder ResultBindings; | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 286 | int CurrentDepth; | 
|  | 287 | const int MaxDepth; | 
|  | 288 | const ASTMatchFinder::TraversalKind Traversal; | 
|  | 289 | const ASTMatchFinder::BindKind Bind; | 
|  | 290 | bool Matches; | 
|  | 291 | }; | 
|  | 292 |  | 
|  | 293 | // Controls the outermost traversal of the AST and allows to match multiple | 
|  | 294 | // matchers. | 
| Daniel Jasper | 1dad183 | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 295 | class MatchASTVisitor : public RecursiveASTVisitor<MatchASTVisitor>, | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 296 | public ASTMatchFinder { | 
|  | 297 | public: | 
| Reid Kleckner | 9171f02 | 2013-10-21 22:26:36 +0000 | [diff] [blame] | 298 | MatchASTVisitor(std::vector<std::pair<const internal::DynTypedMatcher*, | 
|  | 299 | MatchCallback*> > *MatcherCallbackPairs) | 
|  | 300 | : MatcherCallbackPairs(MatcherCallbackPairs), | 
|  | 301 | ActiveASTContext(NULL) { | 
|  | 302 | } | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 303 |  | 
| Manuel Klimek | bd0e2b7 | 2012-11-02 01:31:03 +0000 | [diff] [blame] | 304 | void onStartOfTranslationUnit() { | 
| Reid Kleckner | 9171f02 | 2013-10-21 22:26:36 +0000 | [diff] [blame] | 305 | for (std::vector<std::pair<const internal::DynTypedMatcher*, | 
|  | 306 | MatchCallback*> >::const_iterator | 
|  | 307 | I = MatcherCallbackPairs->begin(), E = MatcherCallbackPairs->end(); | 
| Manuel Klimek | bd0e2b7 | 2012-11-02 01:31:03 +0000 | [diff] [blame] | 308 | I != E; ++I) { | 
|  | 309 | I->second->onStartOfTranslationUnit(); | 
|  | 310 | } | 
|  | 311 | } | 
|  | 312 |  | 
| Peter Collingbourne | 6a55bb2 | 2013-05-28 19:21:51 +0000 | [diff] [blame] | 313 | void onEndOfTranslationUnit() { | 
| Reid Kleckner | 9171f02 | 2013-10-21 22:26:36 +0000 | [diff] [blame] | 314 | for (std::vector<std::pair<const internal::DynTypedMatcher*, | 
|  | 315 | MatchCallback*> >::const_iterator | 
|  | 316 | I = MatcherCallbackPairs->begin(), E = MatcherCallbackPairs->end(); | 
| Peter Collingbourne | 6a55bb2 | 2013-05-28 19:21:51 +0000 | [diff] [blame] | 317 | I != E; ++I) { | 
|  | 318 | I->second->onEndOfTranslationUnit(); | 
|  | 319 | } | 
|  | 320 | } | 
|  | 321 |  | 
| Daniel Jasper | 1dad183 | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 322 | void set_active_ast_context(ASTContext *NewActiveASTContext) { | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 323 | ActiveASTContext = NewActiveASTContext; | 
|  | 324 | } | 
|  | 325 |  | 
|  | 326 | // The following Visit*() and Traverse*() functions "override" | 
|  | 327 | // methods in RecursiveASTVisitor. | 
|  | 328 |  | 
| Manuel Klimek | 1863e50 | 2013-08-02 21:24:09 +0000 | [diff] [blame] | 329 | bool VisitTypedefNameDecl(TypedefNameDecl *DeclNode) { | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 330 | // When we see 'typedef A B', we add name 'B' to the set of names | 
|  | 331 | // A's canonical type maps to.  This is necessary for implementing | 
| Daniel Jasper | f49d1e0 | 2012-09-07 12:48:17 +0000 | [diff] [blame] | 332 | // isDerivedFrom(x) properly, where x can be the name of the base | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 333 | // class or any of its aliases. | 
|  | 334 | // | 
|  | 335 | // In general, the is-alias-of (as defined by typedefs) relation | 
|  | 336 | // is tree-shaped, as you can typedef a type more than once.  For | 
|  | 337 | // example, | 
|  | 338 | // | 
|  | 339 | //   typedef A B; | 
|  | 340 | //   typedef A C; | 
|  | 341 | //   typedef C D; | 
|  | 342 | //   typedef C E; | 
|  | 343 | // | 
|  | 344 | // gives you | 
|  | 345 | // | 
|  | 346 | //   A | 
|  | 347 | //   |- B | 
|  | 348 | //   `- C | 
|  | 349 | //      |- D | 
|  | 350 | //      `- E | 
|  | 351 | // | 
|  | 352 | // It is wrong to assume that the relation is a chain.  A correct | 
| Daniel Jasper | f49d1e0 | 2012-09-07 12:48:17 +0000 | [diff] [blame] | 353 | // implementation of isDerivedFrom() needs to recognize that B and | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 354 | // E are aliases, even though neither is a typedef of the other. | 
|  | 355 | // Therefore, we cannot simply walk through one typedef chain to | 
|  | 356 | // find out whether the type name matches. | 
| Daniel Jasper | 1dad183 | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 357 | const Type *TypeNode = DeclNode->getUnderlyingType().getTypePtr(); | 
|  | 358 | const Type *CanonicalType =  // root of the typedef tree | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 359 | ActiveASTContext->getCanonicalType(TypeNode); | 
| Daniel Jasper | 2b3c7d4 | 2012-07-17 07:39:27 +0000 | [diff] [blame] | 360 | TypeAliases[CanonicalType].insert(DeclNode); | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 361 | return true; | 
|  | 362 | } | 
|  | 363 |  | 
| Daniel Jasper | 1dad183 | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 364 | bool TraverseDecl(Decl *DeclNode); | 
|  | 365 | bool TraverseStmt(Stmt *StmtNode); | 
|  | 366 | bool TraverseType(QualType TypeNode); | 
|  | 367 | bool TraverseTypeLoc(TypeLoc TypeNode); | 
| Daniel Jasper | a6bc1f6 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 368 | bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS); | 
|  | 369 | bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS); | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 370 |  | 
|  | 371 | // Matches children or descendants of 'Node' with 'BaseMatcher'. | 
| Manuel Klimek | eb958de | 2012-09-05 12:12:07 +0000 | [diff] [blame] | 372 | bool memoizedMatchesRecursively(const ast_type_traits::DynTypedNode &Node, | 
|  | 373 | const DynTypedMatcher &Matcher, | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 374 | BoundNodesTreeBuilder *Builder, int MaxDepth, | 
|  | 375 | TraversalKind Traversal, BindKind Bind) { | 
| Manuel Klimek | 55d8fb5 | 2013-07-16 13:20:30 +0000 | [diff] [blame] | 376 | // For AST-nodes that don't have an identity, we can't memoize. | 
|  | 377 | if (!Node.getMemoizationData()) | 
|  | 378 | return matchesRecursively(Node, Matcher, Builder, MaxDepth, Traversal, | 
|  | 379 | Bind); | 
|  | 380 |  | 
| Manuel Klimek | a0c025f | 2013-06-19 15:42:45 +0000 | [diff] [blame] | 381 | MatchKey Key; | 
|  | 382 | Key.MatcherID = Matcher.getID(); | 
|  | 383 | Key.Node = Node; | 
|  | 384 | // Note that we key on the bindings *before* the match. | 
|  | 385 | Key.BoundNodes = *Builder; | 
| Daniel Jasper | d29d5fa | 2012-10-29 10:14:44 +0000 | [diff] [blame] | 386 |  | 
| Manuel Klimek | 55d8fb5 | 2013-07-16 13:20:30 +0000 | [diff] [blame] | 387 | MemoizationMap::iterator I = ResultCache.find(Key); | 
|  | 388 | if (I != ResultCache.end()) { | 
|  | 389 | *Builder = I->second.Nodes; | 
|  | 390 | return I->second.ResultOfMatch; | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 391 | } | 
| Manuel Klimek | 55d8fb5 | 2013-07-16 13:20:30 +0000 | [diff] [blame] | 392 |  | 
|  | 393 | MemoizedMatchResult Result; | 
| Manuel Klimek | 55d8fb5 | 2013-07-16 13:20:30 +0000 | [diff] [blame] | 394 | Result.Nodes = *Builder; | 
|  | 395 | Result.ResultOfMatch = matchesRecursively(Node, Matcher, &Result.Nodes, | 
|  | 396 | MaxDepth, Traversal, Bind); | 
|  | 397 | ResultCache[Key] = Result; | 
|  | 398 | *Builder = Result.Nodes; | 
|  | 399 | return Result.ResultOfMatch; | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 400 | } | 
|  | 401 |  | 
|  | 402 | // Matches children or descendants of 'Node' with 'BaseMatcher'. | 
| Manuel Klimek | eb958de | 2012-09-05 12:12:07 +0000 | [diff] [blame] | 403 | bool matchesRecursively(const ast_type_traits::DynTypedNode &Node, | 
|  | 404 | const DynTypedMatcher &Matcher, | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 405 | BoundNodesTreeBuilder *Builder, int MaxDepth, | 
|  | 406 | TraversalKind Traversal, BindKind Bind) { | 
|  | 407 | MatchChildASTVisitor Visitor( | 
| Manuel Klimek | eb958de | 2012-09-05 12:12:07 +0000 | [diff] [blame] | 408 | &Matcher, this, Builder, MaxDepth, Traversal, Bind); | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 409 | return Visitor.findMatch(Node); | 
|  | 410 | } | 
|  | 411 |  | 
| Daniel Jasper | 1dad183 | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 412 | virtual bool classIsDerivedFrom(const CXXRecordDecl *Declaration, | 
| Daniel Jasper | 2b3c7d4 | 2012-07-17 07:39:27 +0000 | [diff] [blame] | 413 | const Matcher<NamedDecl> &Base, | 
|  | 414 | BoundNodesTreeBuilder *Builder); | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 415 |  | 
| Daniel Jasper | 632aea9 | 2012-10-22 16:26:51 +0000 | [diff] [blame] | 416 | // Implements ASTMatchFinder::matchesChildOf. | 
| Manuel Klimek | eb958de | 2012-09-05 12:12:07 +0000 | [diff] [blame] | 417 | virtual bool matchesChildOf(const ast_type_traits::DynTypedNode &Node, | 
|  | 418 | const DynTypedMatcher &Matcher, | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 419 | BoundNodesTreeBuilder *Builder, | 
|  | 420 | TraversalKind Traversal, | 
|  | 421 | BindKind Bind) { | 
| Daniel Jasper | abe2a36 | 2013-07-25 09:32:14 +0000 | [diff] [blame] | 422 | if (ResultCache.size() > MaxMemoizationEntries) | 
|  | 423 | ResultCache.clear(); | 
|  | 424 | return memoizedMatchesRecursively(Node, Matcher, Builder, 1, Traversal, | 
|  | 425 | Bind); | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 426 | } | 
| Daniel Jasper | 632aea9 | 2012-10-22 16:26:51 +0000 | [diff] [blame] | 427 | // Implements ASTMatchFinder::matchesDescendantOf. | 
| Manuel Klimek | eb958de | 2012-09-05 12:12:07 +0000 | [diff] [blame] | 428 | virtual bool matchesDescendantOf(const ast_type_traits::DynTypedNode &Node, | 
|  | 429 | const DynTypedMatcher &Matcher, | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 430 | BoundNodesTreeBuilder *Builder, | 
|  | 431 | BindKind Bind) { | 
| Manuel Klimek | 7e3d969 | 2013-07-08 14:16:30 +0000 | [diff] [blame] | 432 | if (ResultCache.size() > MaxMemoizationEntries) | 
|  | 433 | ResultCache.clear(); | 
| Manuel Klimek | eb958de | 2012-09-05 12:12:07 +0000 | [diff] [blame] | 434 | return memoizedMatchesRecursively(Node, Matcher, Builder, INT_MAX, | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 435 | TK_AsIs, Bind); | 
|  | 436 | } | 
| Manuel Klimek | 3ca12c5 | 2012-09-07 09:26:10 +0000 | [diff] [blame] | 437 | // Implements ASTMatchFinder::matchesAncestorOf. | 
|  | 438 | virtual bool matchesAncestorOf(const ast_type_traits::DynTypedNode &Node, | 
|  | 439 | const DynTypedMatcher &Matcher, | 
| Daniel Jasper | 632aea9 | 2012-10-22 16:26:51 +0000 | [diff] [blame] | 440 | BoundNodesTreeBuilder *Builder, | 
|  | 441 | AncestorMatchMode MatchMode) { | 
| Manuel Klimek | 7e3d969 | 2013-07-08 14:16:30 +0000 | [diff] [blame] | 442 | // Reset the cache outside of the recursive call to make sure we | 
|  | 443 | // don't invalidate any iterators. | 
|  | 444 | if (ResultCache.size() > MaxMemoizationEntries) | 
|  | 445 | ResultCache.clear(); | 
| Manuel Klimek | b64d6b7 | 2013-03-14 16:33:21 +0000 | [diff] [blame] | 446 | return memoizedMatchesAncestorOfRecursively(Node, Matcher, Builder, | 
|  | 447 | MatchMode); | 
| Manuel Klimek | 3ca12c5 | 2012-09-07 09:26:10 +0000 | [diff] [blame] | 448 | } | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 449 |  | 
| Manuel Klimek | 191c093 | 2013-02-01 13:41:35 +0000 | [diff] [blame] | 450 | // Matches all registered matchers on the given node and calls the | 
|  | 451 | // result callback for every node that matches. | 
|  | 452 | void match(const ast_type_traits::DynTypedNode& Node) { | 
| Reid Kleckner | 9171f02 | 2013-10-21 22:26:36 +0000 | [diff] [blame] | 453 | for (std::vector<std::pair<const internal::DynTypedMatcher*, | 
|  | 454 | MatchCallback*> >::const_iterator | 
|  | 455 | I = MatcherCallbackPairs->begin(), E = MatcherCallbackPairs->end(); | 
| Manuel Klimek | 191c093 | 2013-02-01 13:41:35 +0000 | [diff] [blame] | 456 | I != E; ++I) { | 
|  | 457 | BoundNodesTreeBuilder Builder; | 
| Reid Kleckner | 9171f02 | 2013-10-21 22:26:36 +0000 | [diff] [blame] | 458 | if (I->first->matches(Node, this, &Builder)) { | 
| Manuel Klimek | 191c093 | 2013-02-01 13:41:35 +0000 | [diff] [blame] | 459 | MatchVisitor Visitor(ActiveASTContext, I->second); | 
| Manuel Klimek | a0c025f | 2013-06-19 15:42:45 +0000 | [diff] [blame] | 460 | Builder.visitMatches(&Visitor); | 
| Manuel Klimek | 191c093 | 2013-02-01 13:41:35 +0000 | [diff] [blame] | 461 | } | 
|  | 462 | } | 
|  | 463 | } | 
|  | 464 |  | 
|  | 465 | template <typename T> void match(const T &Node) { | 
|  | 466 | match(ast_type_traits::DynTypedNode::create(Node)); | 
|  | 467 | } | 
|  | 468 |  | 
| Manuel Klimek | 9d04123 | 2012-11-30 13:45:19 +0000 | [diff] [blame] | 469 | // Implements ASTMatchFinder::getASTContext. | 
|  | 470 | virtual ASTContext &getASTContext() const { return *ActiveASTContext; } | 
|  | 471 |  | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 472 | bool shouldVisitTemplateInstantiations() const { return true; } | 
|  | 473 | bool shouldVisitImplicitCode() const { return true; } | 
| Daniel Jasper | 0f9f019 | 2012-11-15 03:29:05 +0000 | [diff] [blame] | 474 | // Disables data recursion. We intercept Traverse* methods in the RAV, which | 
|  | 475 | // are not triggered during data recursion. | 
|  | 476 | bool shouldUseDataRecursionFor(clang::Stmt *S) const { return false; } | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 477 |  | 
|  | 478 | private: | 
| Manuel Klimek | b64d6b7 | 2013-03-14 16:33:21 +0000 | [diff] [blame] | 479 | // Returns whether an ancestor of \p Node matches \p Matcher. | 
|  | 480 | // | 
|  | 481 | // The order of matching ((which can lead to different nodes being bound in | 
|  | 482 | // case there are multiple matches) is breadth first search. | 
|  | 483 | // | 
|  | 484 | // To allow memoization in the very common case of having deeply nested | 
|  | 485 | // expressions inside a template function, we first walk up the AST, memoizing | 
|  | 486 | // the result of the match along the way, as long as there is only a single | 
|  | 487 | // parent. | 
|  | 488 | // | 
|  | 489 | // Once there are multiple parents, the breadth first search order does not | 
|  | 490 | // allow simple memoization on the ancestors. Thus, we only memoize as long | 
|  | 491 | // as there is a single parent. | 
|  | 492 | bool memoizedMatchesAncestorOfRecursively( | 
| Manuel Klimek | c844a46 | 2012-12-06 14:42:48 +0000 | [diff] [blame] | 493 | const ast_type_traits::DynTypedNode &Node, const DynTypedMatcher &Matcher, | 
|  | 494 | BoundNodesTreeBuilder *Builder, AncestorMatchMode MatchMode) { | 
|  | 495 | if (Node.get<TranslationUnitDecl>() == | 
|  | 496 | ActiveASTContext->getTranslationUnitDecl()) | 
|  | 497 | return false; | 
|  | 498 | assert(Node.getMemoizationData() && | 
|  | 499 | "Invariant broken: only nodes that support memoization may be " | 
|  | 500 | "used in the parent map."); | 
| Manuel Klimek | d4be408 | 2013-02-28 13:21:39 +0000 | [diff] [blame] | 501 | ASTContext::ParentVector Parents = ActiveASTContext->getParents(Node); | 
|  | 502 | if (Parents.empty()) { | 
| Manuel Klimek | c844a46 | 2012-12-06 14:42:48 +0000 | [diff] [blame] | 503 | assert(false && "Found node that is not in the parent map."); | 
|  | 504 | return false; | 
|  | 505 | } | 
| Manuel Klimek | a0c025f | 2013-06-19 15:42:45 +0000 | [diff] [blame] | 506 | MatchKey Key; | 
|  | 507 | Key.MatcherID = Matcher.getID(); | 
|  | 508 | Key.Node = Node; | 
|  | 509 | Key.BoundNodes = *Builder; | 
| Manuel Klimek | c844a46 | 2012-12-06 14:42:48 +0000 | [diff] [blame] | 510 |  | 
| Manuel Klimek | 55d8fb5 | 2013-07-16 13:20:30 +0000 | [diff] [blame] | 511 | // Note that we cannot use insert and reuse the iterator, as recursive | 
|  | 512 | // calls to match might invalidate the result cache iterators. | 
|  | 513 | MemoizationMap::iterator I = ResultCache.find(Key); | 
|  | 514 | if (I != ResultCache.end()) { | 
|  | 515 | *Builder = I->second.Nodes; | 
|  | 516 | return I->second.ResultOfMatch; | 
| Manuel Klimek | b64d6b7 | 2013-03-14 16:33:21 +0000 | [diff] [blame] | 517 | } | 
| Manuel Klimek | 55d8fb5 | 2013-07-16 13:20:30 +0000 | [diff] [blame] | 518 | MemoizedMatchResult Result; | 
|  | 519 | Result.ResultOfMatch = false; | 
|  | 520 | Result.Nodes = *Builder; | 
|  | 521 | if (Parents.size() == 1) { | 
|  | 522 | // Only one parent - do recursive memoization. | 
|  | 523 | const ast_type_traits::DynTypedNode Parent = Parents[0]; | 
|  | 524 | if (Matcher.matches(Parent, this, &Result.Nodes)) { | 
|  | 525 | Result.ResultOfMatch = true; | 
|  | 526 | } else if (MatchMode != ASTMatchFinder::AMM_ParentOnly) { | 
|  | 527 | // Reset the results to not include the bound nodes from the failed | 
|  | 528 | // match above. | 
|  | 529 | Result.Nodes = *Builder; | 
|  | 530 | Result.ResultOfMatch = memoizedMatchesAncestorOfRecursively( | 
|  | 531 | Parent, Matcher, &Result.Nodes, MatchMode); | 
|  | 532 | // Once we get back from the recursive call, the result will be the | 
|  | 533 | // same as the parent's result. | 
|  | 534 | } | 
|  | 535 | } else { | 
|  | 536 | // Multiple parents - BFS over the rest of the nodes. | 
|  | 537 | llvm::DenseSet<const void *> Visited; | 
|  | 538 | std::deque<ast_type_traits::DynTypedNode> Queue(Parents.begin(), | 
|  | 539 | Parents.end()); | 
|  | 540 | while (!Queue.empty()) { | 
|  | 541 | Result.Nodes = *Builder; | 
|  | 542 | if (Matcher.matches(Queue.front(), this, &Result.Nodes)) { | 
|  | 543 | Result.ResultOfMatch = true; | 
|  | 544 | break; | 
|  | 545 | } | 
|  | 546 | if (MatchMode != ASTMatchFinder::AMM_ParentOnly) { | 
|  | 547 | ASTContext::ParentVector Ancestors = | 
|  | 548 | ActiveASTContext->getParents(Queue.front()); | 
|  | 549 | for (ASTContext::ParentVector::const_iterator I = Ancestors.begin(), | 
|  | 550 | E = Ancestors.end(); | 
|  | 551 | I != E; ++I) { | 
|  | 552 | // Make sure we do not visit the same node twice. | 
|  | 553 | // Otherwise, we'll visit the common ancestors as often as there | 
|  | 554 | // are splits on the way down. | 
|  | 555 | if (Visited.insert(I->getMemoizationData()).second) | 
|  | 556 | Queue.push_back(*I); | 
|  | 557 | } | 
|  | 558 | } | 
|  | 559 | Queue.pop_front(); | 
|  | 560 | } | 
|  | 561 | } | 
|  | 562 | ResultCache[Key] = Result; | 
|  | 563 |  | 
|  | 564 | *Builder = Result.Nodes; | 
|  | 565 | return Result.ResultOfMatch; | 
| Manuel Klimek | b64d6b7 | 2013-03-14 16:33:21 +0000 | [diff] [blame] | 566 | } | 
| Manuel Klimek | c844a46 | 2012-12-06 14:42:48 +0000 | [diff] [blame] | 567 |  | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 568 | // Implements a BoundNodesTree::Visitor that calls a MatchCallback with | 
|  | 569 | // the aggregated bound nodes for each match. | 
| Manuel Klimek | a0c025f | 2013-06-19 15:42:45 +0000 | [diff] [blame] | 570 | class MatchVisitor : public BoundNodesTreeBuilder::Visitor { | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 571 | public: | 
| Daniel Jasper | 1dad183 | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 572 | MatchVisitor(ASTContext* Context, | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 573 | MatchFinder::MatchCallback* Callback) | 
|  | 574 | : Context(Context), | 
|  | 575 | Callback(Callback) {} | 
|  | 576 |  | 
|  | 577 | virtual void visitMatch(const BoundNodes& BoundNodesView) { | 
|  | 578 | Callback->run(MatchFinder::MatchResult(BoundNodesView, Context)); | 
|  | 579 | } | 
|  | 580 |  | 
|  | 581 | private: | 
| Daniel Jasper | 1dad183 | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 582 | ASTContext* Context; | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 583 | MatchFinder::MatchCallback* Callback; | 
|  | 584 | }; | 
|  | 585 |  | 
| Daniel Jasper | 2b3c7d4 | 2012-07-17 07:39:27 +0000 | [diff] [blame] | 586 | // Returns true if 'TypeNode' has an alias that matches the given matcher. | 
|  | 587 | bool typeHasMatchingAlias(const Type *TypeNode, | 
|  | 588 | const Matcher<NamedDecl> Matcher, | 
|  | 589 | BoundNodesTreeBuilder *Builder) { | 
| Daniel Jasper | 1dad183 | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 590 | const Type *const CanonicalType = | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 591 | ActiveASTContext->getCanonicalType(TypeNode); | 
| Manuel Klimek | 1863e50 | 2013-08-02 21:24:09 +0000 | [diff] [blame] | 592 | const std::set<const TypedefNameDecl *> &Aliases = | 
|  | 593 | TypeAliases[CanonicalType]; | 
|  | 594 | for (std::set<const TypedefNameDecl*>::const_iterator | 
| Daniel Jasper | 2b3c7d4 | 2012-07-17 07:39:27 +0000 | [diff] [blame] | 595 | It = Aliases.begin(), End = Aliases.end(); | 
|  | 596 | It != End; ++It) { | 
| Manuel Klimek | a0c025f | 2013-06-19 15:42:45 +0000 | [diff] [blame] | 597 | BoundNodesTreeBuilder Result(*Builder); | 
|  | 598 | if (Matcher.matches(**It, this, &Result)) { | 
|  | 599 | *Builder = Result; | 
| Daniel Jasper | 2b3c7d4 | 2012-07-17 07:39:27 +0000 | [diff] [blame] | 600 | return true; | 
| Manuel Klimek | a0c025f | 2013-06-19 15:42:45 +0000 | [diff] [blame] | 601 | } | 
| Daniel Jasper | 2b3c7d4 | 2012-07-17 07:39:27 +0000 | [diff] [blame] | 602 | } | 
|  | 603 | return false; | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 604 | } | 
|  | 605 |  | 
| Reid Kleckner | 9171f02 | 2013-10-21 22:26:36 +0000 | [diff] [blame] | 606 | std::vector<std::pair<const internal::DynTypedMatcher*, | 
|  | 607 | MatchCallback*> > *const MatcherCallbackPairs; | 
| Daniel Jasper | 1dad183 | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 608 | ASTContext *ActiveASTContext; | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 609 |  | 
| Daniel Jasper | 2b3c7d4 | 2012-07-17 07:39:27 +0000 | [diff] [blame] | 610 | // Maps a canonical type to its TypedefDecls. | 
| Manuel Klimek | 1863e50 | 2013-08-02 21:24:09 +0000 | [diff] [blame] | 611 | llvm::DenseMap<const Type*, std::set<const TypedefNameDecl*> > TypeAliases; | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 612 |  | 
|  | 613 | // Maps (matcher, node) -> the match result for memoization. | 
| Manuel Klimek | a0c025f | 2013-06-19 15:42:45 +0000 | [diff] [blame] | 614 | typedef std::map<MatchKey, MemoizedMatchResult> MemoizationMap; | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 615 | MemoizationMap ResultCache; | 
|  | 616 | }; | 
|  | 617 |  | 
| Manuel Klimek | 1863e50 | 2013-08-02 21:24:09 +0000 | [diff] [blame] | 618 | static CXXRecordDecl *getAsCXXRecordDecl(const Type *TypeNode) { | 
|  | 619 | // Type::getAs<...>() drills through typedefs. | 
|  | 620 | if (TypeNode->getAs<DependentNameType>() != NULL || | 
|  | 621 | TypeNode->getAs<DependentTemplateSpecializationType>() != NULL || | 
|  | 622 | TypeNode->getAs<TemplateTypeParmType>() != NULL) | 
|  | 623 | // Dependent names and template TypeNode parameters will be matched when | 
|  | 624 | // the template is instantiated. | 
|  | 625 | return NULL; | 
|  | 626 | TemplateSpecializationType const *TemplateType = | 
|  | 627 | TypeNode->getAs<TemplateSpecializationType>(); | 
|  | 628 | if (TemplateType == NULL) { | 
|  | 629 | return TypeNode->getAsCXXRecordDecl(); | 
|  | 630 | } | 
|  | 631 | if (TemplateType->getTemplateName().isDependent()) | 
|  | 632 | // Dependent template specializations will be matched when the | 
|  | 633 | // template is instantiated. | 
|  | 634 | return NULL; | 
|  | 635 |  | 
|  | 636 | // For template specialization types which are specializing a template | 
|  | 637 | // declaration which is an explicit or partial specialization of another | 
|  | 638 | // template declaration, getAsCXXRecordDecl() returns the corresponding | 
|  | 639 | // ClassTemplateSpecializationDecl. | 
|  | 640 | // | 
|  | 641 | // For template specialization types which are specializing a template | 
|  | 642 | // declaration which is neither an explicit nor partial specialization of | 
|  | 643 | // another template declaration, getAsCXXRecordDecl() returns NULL and | 
|  | 644 | // we get the CXXRecordDecl of the templated declaration. | 
|  | 645 | CXXRecordDecl *SpecializationDecl = TemplateType->getAsCXXRecordDecl(); | 
|  | 646 | if (SpecializationDecl != NULL) { | 
|  | 647 | return SpecializationDecl; | 
|  | 648 | } | 
|  | 649 | NamedDecl *Templated = | 
|  | 650 | TemplateType->getTemplateName().getAsTemplateDecl()->getTemplatedDecl(); | 
|  | 651 | if (CXXRecordDecl *TemplatedRecord = dyn_cast<CXXRecordDecl>(Templated)) { | 
|  | 652 | return TemplatedRecord; | 
|  | 653 | } | 
|  | 654 | // Now it can still be that we have an alias template. | 
|  | 655 | TypeAliasDecl *AliasDecl = dyn_cast<TypeAliasDecl>(Templated); | 
|  | 656 | assert(AliasDecl); | 
|  | 657 | return getAsCXXRecordDecl(AliasDecl->getUnderlyingType().getTypePtr()); | 
|  | 658 | } | 
|  | 659 |  | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 660 | // Returns true if the given class is directly or indirectly derived | 
| Daniel Jasper | f49d1e0 | 2012-09-07 12:48:17 +0000 | [diff] [blame] | 661 | // from a base type with the given name.  A class is not considered to be | 
|  | 662 | // derived from itself. | 
| Daniel Jasper | 2b3c7d4 | 2012-07-17 07:39:27 +0000 | [diff] [blame] | 663 | bool MatchASTVisitor::classIsDerivedFrom(const CXXRecordDecl *Declaration, | 
|  | 664 | const Matcher<NamedDecl> &Base, | 
|  | 665 | BoundNodesTreeBuilder *Builder) { | 
| Daniel Jasper | 2b3c7d4 | 2012-07-17 07:39:27 +0000 | [diff] [blame] | 666 | if (!Declaration->hasDefinition()) | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 667 | return false; | 
| Daniel Jasper | 1dad183 | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 668 | typedef CXXRecordDecl::base_class_const_iterator BaseIterator; | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 669 | for (BaseIterator It = Declaration->bases_begin(), | 
| Manuel Klimek | 1863e50 | 2013-08-02 21:24:09 +0000 | [diff] [blame] | 670 | End = Declaration->bases_end(); | 
|  | 671 | It != End; ++It) { | 
| Daniel Jasper | 1dad183 | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 672 | const Type *TypeNode = It->getType().getTypePtr(); | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 673 |  | 
| Daniel Jasper | 2b3c7d4 | 2012-07-17 07:39:27 +0000 | [diff] [blame] | 674 | if (typeHasMatchingAlias(TypeNode, Base, Builder)) | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 675 | return true; | 
|  | 676 |  | 
| Manuel Klimek | 1863e50 | 2013-08-02 21:24:09 +0000 | [diff] [blame] | 677 | CXXRecordDecl *ClassDecl = getAsCXXRecordDecl(TypeNode); | 
|  | 678 | if (ClassDecl == NULL) | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 679 | continue; | 
| Manuel Klimek | 5472a52 | 2012-12-04 13:40:29 +0000 | [diff] [blame] | 680 | if (ClassDecl == Declaration) { | 
|  | 681 | // This can happen for recursive template definitions; if the | 
|  | 682 | // current declaration did not match, we can safely return false. | 
| Manuel Klimek | 5472a52 | 2012-12-04 13:40:29 +0000 | [diff] [blame] | 683 | return false; | 
|  | 684 | } | 
| Manuel Klimek | a0c025f | 2013-06-19 15:42:45 +0000 | [diff] [blame] | 685 | BoundNodesTreeBuilder Result(*Builder); | 
|  | 686 | if (Base.matches(*ClassDecl, this, &Result)) { | 
|  | 687 | *Builder = Result; | 
| Daniel Jasper | f49d1e0 | 2012-09-07 12:48:17 +0000 | [diff] [blame] | 688 | return true; | 
| Manuel Klimek | a0c025f | 2013-06-19 15:42:45 +0000 | [diff] [blame] | 689 | } | 
| Daniel Jasper | 2b3c7d4 | 2012-07-17 07:39:27 +0000 | [diff] [blame] | 690 | if (classIsDerivedFrom(ClassDecl, Base, Builder)) | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 691 | return true; | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 692 | } | 
|  | 693 | return false; | 
|  | 694 | } | 
|  | 695 |  | 
| Daniel Jasper | 1dad183 | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 696 | bool MatchASTVisitor::TraverseDecl(Decl *DeclNode) { | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 697 | if (DeclNode == NULL) { | 
|  | 698 | return true; | 
|  | 699 | } | 
|  | 700 | match(*DeclNode); | 
| Daniel Jasper | 1dad183 | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 701 | return RecursiveASTVisitor<MatchASTVisitor>::TraverseDecl(DeclNode); | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 702 | } | 
|  | 703 |  | 
| Daniel Jasper | 1dad183 | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 704 | bool MatchASTVisitor::TraverseStmt(Stmt *StmtNode) { | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 705 | if (StmtNode == NULL) { | 
|  | 706 | return true; | 
|  | 707 | } | 
|  | 708 | match(*StmtNode); | 
| Daniel Jasper | 1dad183 | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 709 | return RecursiveASTVisitor<MatchASTVisitor>::TraverseStmt(StmtNode); | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 710 | } | 
|  | 711 |  | 
| Daniel Jasper | 1dad183 | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 712 | bool MatchASTVisitor::TraverseType(QualType TypeNode) { | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 713 | match(TypeNode); | 
| Daniel Jasper | 1dad183 | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 714 | return RecursiveASTVisitor<MatchASTVisitor>::TraverseType(TypeNode); | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 715 | } | 
|  | 716 |  | 
| Daniel Jasper | 516b02e | 2012-10-17 08:52:59 +0000 | [diff] [blame] | 717 | bool MatchASTVisitor::TraverseTypeLoc(TypeLoc TypeLocNode) { | 
|  | 718 | // The RecursiveASTVisitor only visits types if they're not within TypeLocs. | 
|  | 719 | // We still want to find those types via matchers, so we match them here. Note | 
|  | 720 | // that the TypeLocs are structurally a shadow-hierarchy to the expressed | 
|  | 721 | // type, so we visit all involved parts of a compound type when matching on | 
|  | 722 | // each TypeLoc. | 
|  | 723 | match(TypeLocNode); | 
|  | 724 | match(TypeLocNode.getType()); | 
|  | 725 | return RecursiveASTVisitor<MatchASTVisitor>::TraverseTypeLoc(TypeLocNode); | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 726 | } | 
|  | 727 |  | 
| Daniel Jasper | a6bc1f6 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 728 | bool MatchASTVisitor::TraverseNestedNameSpecifier(NestedNameSpecifier *NNS) { | 
|  | 729 | match(*NNS); | 
|  | 730 | return RecursiveASTVisitor<MatchASTVisitor>::TraverseNestedNameSpecifier(NNS); | 
|  | 731 | } | 
|  | 732 |  | 
|  | 733 | bool MatchASTVisitor::TraverseNestedNameSpecifierLoc( | 
|  | 734 | NestedNameSpecifierLoc NNS) { | 
|  | 735 | match(NNS); | 
|  | 736 | // We only match the nested name specifier here (as opposed to traversing it) | 
|  | 737 | // because the traversal is already done in the parallel "Loc"-hierarchy. | 
|  | 738 | match(*NNS.getNestedNameSpecifier()); | 
|  | 739 | return | 
|  | 740 | RecursiveASTVisitor<MatchASTVisitor>::TraverseNestedNameSpecifierLoc(NNS); | 
|  | 741 | } | 
|  | 742 |  | 
| Daniel Jasper | 1dad183 | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 743 | class MatchASTConsumer : public ASTConsumer { | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 744 | public: | 
| Manuel Klimek | eb958de | 2012-09-05 12:12:07 +0000 | [diff] [blame] | 745 | MatchASTConsumer( | 
| Reid Kleckner | 9171f02 | 2013-10-21 22:26:36 +0000 | [diff] [blame] | 746 | std::vector<std::pair<const internal::DynTypedMatcher*, | 
|  | 747 | MatchCallback*> > *MatcherCallbackPairs, | 
|  | 748 | MatchFinder::ParsingDoneTestCallback *ParsingDone) | 
|  | 749 | : Visitor(MatcherCallbackPairs), | 
|  | 750 | ParsingDone(ParsingDone) {} | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 751 |  | 
|  | 752 | private: | 
| Daniel Jasper | 1dad183 | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 753 | virtual void HandleTranslationUnit(ASTContext &Context) { | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 754 | if (ParsingDone != NULL) { | 
|  | 755 | ParsingDone->run(); | 
|  | 756 | } | 
|  | 757 | Visitor.set_active_ast_context(&Context); | 
| Manuel Klimek | bd0e2b7 | 2012-11-02 01:31:03 +0000 | [diff] [blame] | 758 | Visitor.onStartOfTranslationUnit(); | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 759 | Visitor.TraverseDecl(Context.getTranslationUnitDecl()); | 
| Peter Collingbourne | 6a55bb2 | 2013-05-28 19:21:51 +0000 | [diff] [blame] | 760 | Visitor.onEndOfTranslationUnit(); | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 761 | Visitor.set_active_ast_context(NULL); | 
|  | 762 | } | 
|  | 763 |  | 
|  | 764 | MatchASTVisitor Visitor; | 
|  | 765 | MatchFinder::ParsingDoneTestCallback *ParsingDone; | 
|  | 766 | }; | 
|  | 767 |  | 
|  | 768 | } // end namespace | 
|  | 769 | } // end namespace internal | 
|  | 770 |  | 
|  | 771 | MatchFinder::MatchResult::MatchResult(const BoundNodes &Nodes, | 
| Daniel Jasper | 1dad183 | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 772 | ASTContext *Context) | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 773 | : Nodes(Nodes), Context(Context), | 
|  | 774 | SourceManager(&Context->getSourceManager()) {} | 
|  | 775 |  | 
|  | 776 | MatchFinder::MatchCallback::~MatchCallback() {} | 
|  | 777 | MatchFinder::ParsingDoneTestCallback::~ParsingDoneTestCallback() {} | 
|  | 778 |  | 
|  | 779 | MatchFinder::MatchFinder() : ParsingDone(NULL) {} | 
|  | 780 |  | 
| Reid Kleckner | 9171f02 | 2013-10-21 22:26:36 +0000 | [diff] [blame] | 781 | MatchFinder::~MatchFinder() { | 
|  | 782 | for (std::vector<std::pair<const internal::DynTypedMatcher*, | 
|  | 783 | MatchCallback*> >::const_iterator | 
|  | 784 | It = MatcherCallbackPairs.begin(), End = MatcherCallbackPairs.end(); | 
|  | 785 | It != End; ++It) { | 
|  | 786 | delete It->first; | 
|  | 787 | } | 
|  | 788 | } | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 789 |  | 
|  | 790 | void MatchFinder::addMatcher(const DeclarationMatcher &NodeMatch, | 
|  | 791 | MatchCallback *Action) { | 
| Reid Kleckner | 9171f02 | 2013-10-21 22:26:36 +0000 | [diff] [blame] | 792 | MatcherCallbackPairs.push_back(std::make_pair( | 
|  | 793 | new internal::Matcher<Decl>(NodeMatch), Action)); | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 794 | } | 
|  | 795 |  | 
|  | 796 | void MatchFinder::addMatcher(const TypeMatcher &NodeMatch, | 
|  | 797 | MatchCallback *Action) { | 
| Reid Kleckner | 9171f02 | 2013-10-21 22:26:36 +0000 | [diff] [blame] | 798 | MatcherCallbackPairs.push_back(std::make_pair( | 
|  | 799 | new internal::Matcher<QualType>(NodeMatch), Action)); | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 800 | } | 
|  | 801 |  | 
|  | 802 | void MatchFinder::addMatcher(const StatementMatcher &NodeMatch, | 
|  | 803 | MatchCallback *Action) { | 
| Reid Kleckner | 9171f02 | 2013-10-21 22:26:36 +0000 | [diff] [blame] | 804 | MatcherCallbackPairs.push_back(std::make_pair( | 
|  | 805 | new internal::Matcher<Stmt>(NodeMatch), Action)); | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 806 | } | 
|  | 807 |  | 
| Daniel Jasper | a6bc1f6 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 808 | void MatchFinder::addMatcher(const NestedNameSpecifierMatcher &NodeMatch, | 
|  | 809 | MatchCallback *Action) { | 
| Reid Kleckner | 9171f02 | 2013-10-21 22:26:36 +0000 | [diff] [blame] | 810 | MatcherCallbackPairs.push_back(std::make_pair( | 
|  | 811 | new NestedNameSpecifierMatcher(NodeMatch), Action)); | 
| Daniel Jasper | a6bc1f6 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 812 | } | 
|  | 813 |  | 
|  | 814 | void MatchFinder::addMatcher(const NestedNameSpecifierLocMatcher &NodeMatch, | 
|  | 815 | MatchCallback *Action) { | 
| Reid Kleckner | 9171f02 | 2013-10-21 22:26:36 +0000 | [diff] [blame] | 816 | MatcherCallbackPairs.push_back(std::make_pair( | 
|  | 817 | new NestedNameSpecifierLocMatcher(NodeMatch), Action)); | 
| Daniel Jasper | a6bc1f6 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 818 | } | 
|  | 819 |  | 
| Daniel Jasper | 516b02e | 2012-10-17 08:52:59 +0000 | [diff] [blame] | 820 | void MatchFinder::addMatcher(const TypeLocMatcher &NodeMatch, | 
|  | 821 | MatchCallback *Action) { | 
| Reid Kleckner | 9171f02 | 2013-10-21 22:26:36 +0000 | [diff] [blame] | 822 | MatcherCallbackPairs.push_back(std::make_pair( | 
|  | 823 | new TypeLocMatcher(NodeMatch), Action)); | 
| Daniel Jasper | 516b02e | 2012-10-17 08:52:59 +0000 | [diff] [blame] | 824 | } | 
|  | 825 |  | 
| Daniel Jasper | 1dad183 | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 826 | ASTConsumer *MatchFinder::newASTConsumer() { | 
| Manuel Klimek | eb958de | 2012-09-05 12:12:07 +0000 | [diff] [blame] | 827 | return new internal::MatchASTConsumer(&MatcherCallbackPairs, ParsingDone); | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 828 | } | 
|  | 829 |  | 
| Manuel Klimek | 191c093 | 2013-02-01 13:41:35 +0000 | [diff] [blame] | 830 | void MatchFinder::match(const clang::ast_type_traits::DynTypedNode &Node, | 
|  | 831 | ASTContext &Context) { | 
| Manuel Klimek | c268745 | 2012-10-24 14:47:44 +0000 | [diff] [blame] | 832 | internal::MatchASTVisitor Visitor(&MatcherCallbackPairs); | 
|  | 833 | Visitor.set_active_ast_context(&Context); | 
| Manuel Klimek | 191c093 | 2013-02-01 13:41:35 +0000 | [diff] [blame] | 834 | Visitor.match(Node); | 
| Manuel Klimek | c268745 | 2012-10-24 14:47:44 +0000 | [diff] [blame] | 835 | } | 
|  | 836 |  | 
| Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 837 | void MatchFinder::registerTestCallbackAfterParsing( | 
|  | 838 | MatchFinder::ParsingDoneTestCallback *NewParsingDone) { | 
|  | 839 | ParsingDone = NewParsingDone; | 
|  | 840 | } | 
|  | 841 |  | 
|  | 842 | } // end namespace ast_matchers | 
|  | 843 | } // end namespace clang |