Manuel Klimek | 4da2166 | 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" |
| 23 | #include <set> |
| 24 | |
| 25 | namespace clang { |
| 26 | namespace ast_matchers { |
| 27 | namespace internal { |
| 28 | namespace { |
| 29 | |
Manuel Klimek | a78d0d6 | 2012-09-05 12:12:07 +0000 | [diff] [blame] | 30 | typedef MatchFinder::MatchCallback MatchCallback; |
| 31 | |
Manuel Klimek | 579b120 | 2012-09-07 09:26:10 +0000 | [diff] [blame] | 32 | /// \brief A \c RecursiveASTVisitor that builds a map from nodes to their |
| 33 | /// parents as defined by the \c RecursiveASTVisitor. |
| 34 | /// |
| 35 | /// Note that the relationship described here is purely in terms of AST |
| 36 | /// traversal - there are other relationships (for example declaration context) |
| 37 | /// in the AST that are better modeled by special matchers. |
| 38 | /// |
| 39 | /// FIXME: Currently only builds up the map using \c Stmt and \c Decl nodes. |
| 40 | class ParentMapASTVisitor : public RecursiveASTVisitor<ParentMapASTVisitor> { |
| 41 | public: |
| 42 | /// \brief Maps from a node to its parent. |
| 43 | typedef llvm::DenseMap<const void*, ast_type_traits::DynTypedNode> ParentMap; |
| 44 | |
| 45 | /// \brief Builds and returns the translation unit's parent map. |
| 46 | /// |
| 47 | /// The caller takes ownership of the returned \c ParentMap. |
| 48 | static ParentMap *buildMap(TranslationUnitDecl &TU) { |
| 49 | ParentMapASTVisitor Visitor(new ParentMap); |
| 50 | Visitor.TraverseDecl(&TU); |
| 51 | return Visitor.Parents; |
| 52 | } |
| 53 | |
| 54 | private: |
| 55 | typedef RecursiveASTVisitor<ParentMapASTVisitor> VisitorBase; |
| 56 | |
| 57 | ParentMapASTVisitor(ParentMap *Parents) : Parents(Parents) {} |
| 58 | |
| 59 | bool shouldVisitTemplateInstantiations() const { return true; } |
| 60 | bool shouldVisitImplicitCode() const { return true; } |
| 61 | |
| 62 | template <typename T> |
| 63 | bool TraverseNode(T *Node, bool (VisitorBase::*traverse)(T*)) { |
| 64 | if (Node == NULL) |
| 65 | return true; |
| 66 | if (ParentStack.size() > 0) |
| 67 | (*Parents)[Node] = ParentStack.back(); |
| 68 | ParentStack.push_back(ast_type_traits::DynTypedNode::create(*Node)); |
| 69 | bool Result = (this->*traverse)(Node); |
| 70 | ParentStack.pop_back(); |
| 71 | return Result; |
| 72 | } |
| 73 | |
| 74 | bool TraverseDecl(Decl *DeclNode) { |
| 75 | return TraverseNode(DeclNode, &VisitorBase::TraverseDecl); |
| 76 | } |
| 77 | |
| 78 | bool TraverseStmt(Stmt *StmtNode) { |
| 79 | return TraverseNode(StmtNode, &VisitorBase::TraverseStmt); |
| 80 | } |
| 81 | |
| 82 | ParentMap *Parents; |
| 83 | llvm::SmallVector<ast_type_traits::DynTypedNode, 16> ParentStack; |
| 84 | |
| 85 | friend class RecursiveASTVisitor<ParentMapASTVisitor>; |
| 86 | }; |
| 87 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 88 | // We use memoization to avoid running the same matcher on the same |
| 89 | // AST node twice. This pair is the key for looking up match |
| 90 | // result. It consists of an ID of the MatcherInterface (for |
| 91 | // identifying the matcher) and a pointer to the AST node. |
Manuel Klimek | a78d0d6 | 2012-09-05 12:12:07 +0000 | [diff] [blame] | 92 | // |
| 93 | // We currently only memoize on nodes whose pointers identify the |
| 94 | // nodes (\c Stmt and \c Decl, but not \c QualType or \c TypeLoc). |
| 95 | // For \c QualType and \c TypeLoc it is possible to implement |
| 96 | // generation of keys for each type. |
| 97 | // FIXME: Benchmark whether memoization of non-pointer typed nodes |
| 98 | // provides enough benefit for the additional amount of code. |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 99 | typedef std::pair<uint64_t, const void*> UntypedMatchInput; |
| 100 | |
| 101 | // Used to store the result of a match and possibly bound nodes. |
| 102 | struct MemoizedMatchResult { |
| 103 | bool ResultOfMatch; |
| 104 | BoundNodesTree Nodes; |
| 105 | }; |
| 106 | |
| 107 | // A RecursiveASTVisitor that traverses all children or all descendants of |
| 108 | // a node. |
| 109 | class MatchChildASTVisitor |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 110 | : public RecursiveASTVisitor<MatchChildASTVisitor> { |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 111 | public: |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 112 | typedef RecursiveASTVisitor<MatchChildASTVisitor> VisitorBase; |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 113 | |
| 114 | // Creates an AST visitor that matches 'matcher' on all children or |
| 115 | // descendants of a traversed node. max_depth is the maximum depth |
| 116 | // to traverse: use 1 for matching the children and INT_MAX for |
| 117 | // matching the descendants. |
Manuel Klimek | a78d0d6 | 2012-09-05 12:12:07 +0000 | [diff] [blame] | 118 | MatchChildASTVisitor(const DynTypedMatcher *Matcher, |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 119 | ASTMatchFinder *Finder, |
| 120 | BoundNodesTreeBuilder *Builder, |
| 121 | int MaxDepth, |
| 122 | ASTMatchFinder::TraversalKind Traversal, |
| 123 | ASTMatchFinder::BindKind Bind) |
Manuel Klimek | a78d0d6 | 2012-09-05 12:12:07 +0000 | [diff] [blame] | 124 | : Matcher(Matcher), |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 125 | Finder(Finder), |
| 126 | Builder(Builder), |
| 127 | CurrentDepth(-1), |
| 128 | MaxDepth(MaxDepth), |
| 129 | Traversal(Traversal), |
| 130 | Bind(Bind), |
| 131 | Matches(false) {} |
| 132 | |
| 133 | // Returns true if a match is found in the subtree rooted at the |
| 134 | // given AST node. This is done via a set of mutually recursive |
| 135 | // functions. Here's how the recursion is done (the *wildcard can |
| 136 | // actually be Decl, Stmt, or Type): |
| 137 | // |
| 138 | // - Traverse(node) calls BaseTraverse(node) when it needs |
| 139 | // to visit the descendants of node. |
| 140 | // - BaseTraverse(node) then calls (via VisitorBase::Traverse*(node)) |
| 141 | // Traverse*(c) for each child c of 'node'. |
| 142 | // - Traverse*(c) in turn calls Traverse(c), completing the |
| 143 | // recursion. |
Manuel Klimek | a78d0d6 | 2012-09-05 12:12:07 +0000 | [diff] [blame] | 144 | bool findMatch(const ast_type_traits::DynTypedNode &DynNode) { |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 145 | reset(); |
Manuel Klimek | a78d0d6 | 2012-09-05 12:12:07 +0000 | [diff] [blame] | 146 | if (const Decl *D = DynNode.get<Decl>()) |
| 147 | traverse(*D); |
| 148 | else if (const Stmt *S = DynNode.get<Stmt>()) |
| 149 | traverse(*S); |
| 150 | // FIXME: Add other base types after adding tests. |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 151 | return Matches; |
| 152 | } |
| 153 | |
| 154 | // The following are overriding methods from the base visitor class. |
| 155 | // They are public only to allow CRTP to work. They are *not *part |
| 156 | // of the public API of this class. |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 157 | bool TraverseDecl(Decl *DeclNode) { |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 158 | return (DeclNode == NULL) || traverse(*DeclNode); |
| 159 | } |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 160 | bool TraverseStmt(Stmt *StmtNode) { |
| 161 | const Stmt *StmtToTraverse = StmtNode; |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 162 | if (Traversal == |
| 163 | ASTMatchFinder::TK_IgnoreImplicitCastsAndParentheses) { |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 164 | const Expr *ExprNode = dyn_cast_or_null<Expr>(StmtNode); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 165 | if (ExprNode != NULL) { |
| 166 | StmtToTraverse = ExprNode->IgnoreParenImpCasts(); |
| 167 | } |
| 168 | } |
| 169 | return (StmtToTraverse == NULL) || traverse(*StmtToTraverse); |
| 170 | } |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 171 | bool TraverseType(QualType TypeNode) { |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 172 | return traverse(TypeNode); |
| 173 | } |
| 174 | |
| 175 | bool shouldVisitTemplateInstantiations() const { return true; } |
| 176 | bool shouldVisitImplicitCode() const { return true; } |
| 177 | |
| 178 | private: |
| 179 | // Used for updating the depth during traversal. |
| 180 | struct ScopedIncrement { |
| 181 | explicit ScopedIncrement(int *Depth) : Depth(Depth) { ++(*Depth); } |
| 182 | ~ScopedIncrement() { --(*Depth); } |
| 183 | |
| 184 | private: |
| 185 | int *Depth; |
| 186 | }; |
| 187 | |
| 188 | // Resets the state of this object. |
| 189 | void reset() { |
| 190 | Matches = false; |
| 191 | CurrentDepth = -1; |
| 192 | } |
| 193 | |
| 194 | // Forwards the call to the corresponding Traverse*() method in the |
| 195 | // base visitor class. |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 196 | bool baseTraverse(const Decl &DeclNode) { |
| 197 | return VisitorBase::TraverseDecl(const_cast<Decl*>(&DeclNode)); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 198 | } |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 199 | bool baseTraverse(const Stmt &StmtNode) { |
| 200 | return VisitorBase::TraverseStmt(const_cast<Stmt*>(&StmtNode)); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 201 | } |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 202 | bool baseTraverse(QualType TypeNode) { |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 203 | return VisitorBase::TraverseType(TypeNode); |
| 204 | } |
| 205 | |
| 206 | // Traverses the subtree rooted at 'node'; returns true if the |
| 207 | // traversal should continue after this function returns; also sets |
| 208 | // matched_ to true if a match is found during the traversal. |
| 209 | template <typename T> |
| 210 | bool traverse(const T &Node) { |
| 211 | TOOLING_COMPILE_ASSERT(IsBaseType<T>::value, |
| 212 | traverse_can_only_be_instantiated_with_base_type); |
| 213 | ScopedIncrement ScopedDepth(&CurrentDepth); |
| 214 | if (CurrentDepth == 0) { |
| 215 | // We don't want to match the root node, so just recurse. |
| 216 | return baseTraverse(Node); |
| 217 | } |
| 218 | if (Bind != ASTMatchFinder::BK_All) { |
Manuel Klimek | a78d0d6 | 2012-09-05 12:12:07 +0000 | [diff] [blame] | 219 | if (Matcher->matches(ast_type_traits::DynTypedNode::create(Node), |
| 220 | Finder, Builder)) { |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 221 | Matches = true; |
| 222 | return false; // Abort as soon as a match is found. |
| 223 | } |
| 224 | if (CurrentDepth < MaxDepth) { |
| 225 | // The current node doesn't match, and we haven't reached the |
| 226 | // maximum depth yet, so recurse. |
| 227 | return baseTraverse(Node); |
| 228 | } |
| 229 | // The current node doesn't match, and we have reached the |
| 230 | // maximum depth, so don't recurse (but continue the traversal |
| 231 | // such that other nodes at the current level can be visited). |
| 232 | return true; |
| 233 | } else { |
| 234 | BoundNodesTreeBuilder RecursiveBuilder; |
Manuel Klimek | a78d0d6 | 2012-09-05 12:12:07 +0000 | [diff] [blame] | 235 | if (Matcher->matches(ast_type_traits::DynTypedNode::create(Node), |
| 236 | Finder, &RecursiveBuilder)) { |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 237 | // After the first match the matcher succeeds. |
| 238 | Matches = true; |
| 239 | Builder->addMatch(RecursiveBuilder.build()); |
| 240 | } |
| 241 | if (CurrentDepth < MaxDepth) { |
| 242 | baseTraverse(Node); |
| 243 | } |
| 244 | // In kBindAll mode we always search for more matches. |
| 245 | return true; |
| 246 | } |
| 247 | } |
| 248 | |
Manuel Klimek | a78d0d6 | 2012-09-05 12:12:07 +0000 | [diff] [blame] | 249 | const DynTypedMatcher *const Matcher; |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 250 | ASTMatchFinder *const Finder; |
| 251 | BoundNodesTreeBuilder *const Builder; |
| 252 | int CurrentDepth; |
| 253 | const int MaxDepth; |
| 254 | const ASTMatchFinder::TraversalKind Traversal; |
| 255 | const ASTMatchFinder::BindKind Bind; |
| 256 | bool Matches; |
| 257 | }; |
| 258 | |
| 259 | // Controls the outermost traversal of the AST and allows to match multiple |
| 260 | // matchers. |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 261 | class MatchASTVisitor : public RecursiveASTVisitor<MatchASTVisitor>, |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 262 | public ASTMatchFinder { |
| 263 | public: |
Manuel Klimek | a78d0d6 | 2012-09-05 12:12:07 +0000 | [diff] [blame] | 264 | MatchASTVisitor(std::vector<std::pair<const internal::DynTypedMatcher*, |
| 265 | MatchCallback*> > *MatcherCallbackPairs) |
| 266 | : MatcherCallbackPairs(MatcherCallbackPairs), |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 267 | ActiveASTContext(NULL) { |
| 268 | } |
| 269 | |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 270 | void set_active_ast_context(ASTContext *NewActiveASTContext) { |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 271 | ActiveASTContext = NewActiveASTContext; |
| 272 | } |
| 273 | |
| 274 | // The following Visit*() and Traverse*() functions "override" |
| 275 | // methods in RecursiveASTVisitor. |
| 276 | |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 277 | bool VisitTypedefDecl(TypedefDecl *DeclNode) { |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 278 | // When we see 'typedef A B', we add name 'B' to the set of names |
| 279 | // A's canonical type maps to. This is necessary for implementing |
Daniel Jasper | 76dafa7 | 2012-09-07 12:48:17 +0000 | [diff] [blame] | 280 | // isDerivedFrom(x) properly, where x can be the name of the base |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 281 | // class or any of its aliases. |
| 282 | // |
| 283 | // In general, the is-alias-of (as defined by typedefs) relation |
| 284 | // is tree-shaped, as you can typedef a type more than once. For |
| 285 | // example, |
| 286 | // |
| 287 | // typedef A B; |
| 288 | // typedef A C; |
| 289 | // typedef C D; |
| 290 | // typedef C E; |
| 291 | // |
| 292 | // gives you |
| 293 | // |
| 294 | // A |
| 295 | // |- B |
| 296 | // `- C |
| 297 | // |- D |
| 298 | // `- E |
| 299 | // |
| 300 | // It is wrong to assume that the relation is a chain. A correct |
Daniel Jasper | 76dafa7 | 2012-09-07 12:48:17 +0000 | [diff] [blame] | 301 | // implementation of isDerivedFrom() needs to recognize that B and |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 302 | // E are aliases, even though neither is a typedef of the other. |
| 303 | // Therefore, we cannot simply walk through one typedef chain to |
| 304 | // find out whether the type name matches. |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 305 | const Type *TypeNode = DeclNode->getUnderlyingType().getTypePtr(); |
| 306 | const Type *CanonicalType = // root of the typedef tree |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 307 | ActiveASTContext->getCanonicalType(TypeNode); |
Daniel Jasper | 20b802d | 2012-07-17 07:39:27 +0000 | [diff] [blame] | 308 | TypeAliases[CanonicalType].insert(DeclNode); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 309 | return true; |
| 310 | } |
| 311 | |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 312 | bool TraverseDecl(Decl *DeclNode); |
| 313 | bool TraverseStmt(Stmt *StmtNode); |
| 314 | bool TraverseType(QualType TypeNode); |
| 315 | bool TraverseTypeLoc(TypeLoc TypeNode); |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 316 | bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS); |
| 317 | bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 318 | |
| 319 | // Matches children or descendants of 'Node' with 'BaseMatcher'. |
Manuel Klimek | a78d0d6 | 2012-09-05 12:12:07 +0000 | [diff] [blame] | 320 | bool memoizedMatchesRecursively(const ast_type_traits::DynTypedNode &Node, |
| 321 | const DynTypedMatcher &Matcher, |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 322 | BoundNodesTreeBuilder *Builder, int MaxDepth, |
| 323 | TraversalKind Traversal, BindKind Bind) { |
Manuel Klimek | a78d0d6 | 2012-09-05 12:12:07 +0000 | [diff] [blame] | 324 | const UntypedMatchInput input(Matcher.getID(), Node.getMemoizationData()); |
| 325 | assert(input.second && |
| 326 | "Fix getMemoizationData once more types allow recursive matching."); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 327 | std::pair<MemoizationMap::iterator, bool> InsertResult |
| 328 | = ResultCache.insert(std::make_pair(input, MemoizedMatchResult())); |
| 329 | if (InsertResult.second) { |
| 330 | BoundNodesTreeBuilder DescendantBoundNodesBuilder; |
| 331 | InsertResult.first->second.ResultOfMatch = |
Manuel Klimek | a78d0d6 | 2012-09-05 12:12:07 +0000 | [diff] [blame] | 332 | matchesRecursively(Node, Matcher, &DescendantBoundNodesBuilder, |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 333 | MaxDepth, Traversal, Bind); |
| 334 | InsertResult.first->second.Nodes = |
| 335 | DescendantBoundNodesBuilder.build(); |
| 336 | } |
| 337 | InsertResult.first->second.Nodes.copyTo(Builder); |
| 338 | return InsertResult.first->second.ResultOfMatch; |
| 339 | } |
| 340 | |
| 341 | // Matches children or descendants of 'Node' with 'BaseMatcher'. |
Manuel Klimek | a78d0d6 | 2012-09-05 12:12:07 +0000 | [diff] [blame] | 342 | bool matchesRecursively(const ast_type_traits::DynTypedNode &Node, |
| 343 | const DynTypedMatcher &Matcher, |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 344 | BoundNodesTreeBuilder *Builder, int MaxDepth, |
| 345 | TraversalKind Traversal, BindKind Bind) { |
| 346 | MatchChildASTVisitor Visitor( |
Manuel Klimek | a78d0d6 | 2012-09-05 12:12:07 +0000 | [diff] [blame] | 347 | &Matcher, this, Builder, MaxDepth, Traversal, Bind); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 348 | return Visitor.findMatch(Node); |
| 349 | } |
| 350 | |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 351 | virtual bool classIsDerivedFrom(const CXXRecordDecl *Declaration, |
Daniel Jasper | 20b802d | 2012-07-17 07:39:27 +0000 | [diff] [blame] | 352 | const Matcher<NamedDecl> &Base, |
| 353 | BoundNodesTreeBuilder *Builder); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 354 | |
| 355 | // Implements ASTMatchFinder::MatchesChildOf. |
Manuel Klimek | a78d0d6 | 2012-09-05 12:12:07 +0000 | [diff] [blame] | 356 | virtual bool matchesChildOf(const ast_type_traits::DynTypedNode &Node, |
| 357 | const DynTypedMatcher &Matcher, |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 358 | BoundNodesTreeBuilder *Builder, |
| 359 | TraversalKind Traversal, |
| 360 | BindKind Bind) { |
Manuel Klimek | a78d0d6 | 2012-09-05 12:12:07 +0000 | [diff] [blame] | 361 | return matchesRecursively(Node, Matcher, Builder, 1, Traversal, |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 362 | Bind); |
| 363 | } |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 364 | // Implements ASTMatchFinder::MatchesDescendantOf. |
Manuel Klimek | a78d0d6 | 2012-09-05 12:12:07 +0000 | [diff] [blame] | 365 | virtual bool matchesDescendantOf(const ast_type_traits::DynTypedNode &Node, |
| 366 | const DynTypedMatcher &Matcher, |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 367 | BoundNodesTreeBuilder *Builder, |
| 368 | BindKind Bind) { |
Manuel Klimek | a78d0d6 | 2012-09-05 12:12:07 +0000 | [diff] [blame] | 369 | return memoizedMatchesRecursively(Node, Matcher, Builder, INT_MAX, |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 370 | TK_AsIs, Bind); |
| 371 | } |
Manuel Klimek | 579b120 | 2012-09-07 09:26:10 +0000 | [diff] [blame] | 372 | // Implements ASTMatchFinder::matchesAncestorOf. |
| 373 | virtual bool matchesAncestorOf(const ast_type_traits::DynTypedNode &Node, |
| 374 | const DynTypedMatcher &Matcher, |
| 375 | BoundNodesTreeBuilder *Builder) { |
| 376 | if (!Parents) { |
| 377 | // We always need to run over the whole translation unit, as |
| 378 | // \c hasAncestor can escape any subtree. |
| 379 | Parents.reset(ParentMapASTVisitor::buildMap( |
| 380 | *ActiveASTContext->getTranslationUnitDecl())); |
| 381 | } |
| 382 | ast_type_traits::DynTypedNode Ancestor = Node; |
| 383 | while (Ancestor.get<TranslationUnitDecl>() != |
| 384 | ActiveASTContext->getTranslationUnitDecl()) { |
| 385 | assert(Ancestor.getMemoizationData() && |
| 386 | "Invariant broken: only nodes that support memoization may be " |
| 387 | "used in the parent map."); |
| 388 | ParentMapASTVisitor::ParentMap::const_iterator I = |
| 389 | Parents->find(Ancestor.getMemoizationData()); |
| 390 | if (I == Parents->end()) { |
| 391 | assert(false && |
| 392 | "Found node that is not in the parent map."); |
| 393 | return false; |
| 394 | } |
| 395 | Ancestor = I->second; |
| 396 | if (Matcher.matches(Ancestor, this, Builder)) |
| 397 | return true; |
| 398 | } |
| 399 | return false; |
| 400 | } |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 401 | |
| 402 | bool shouldVisitTemplateInstantiations() const { return true; } |
| 403 | bool shouldVisitImplicitCode() const { return true; } |
| 404 | |
| 405 | private: |
| 406 | // Implements a BoundNodesTree::Visitor that calls a MatchCallback with |
| 407 | // the aggregated bound nodes for each match. |
| 408 | class MatchVisitor : public BoundNodesTree::Visitor { |
| 409 | public: |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 410 | MatchVisitor(ASTContext* Context, |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 411 | MatchFinder::MatchCallback* Callback) |
| 412 | : Context(Context), |
| 413 | Callback(Callback) {} |
| 414 | |
| 415 | virtual void visitMatch(const BoundNodes& BoundNodesView) { |
| 416 | Callback->run(MatchFinder::MatchResult(BoundNodesView, Context)); |
| 417 | } |
| 418 | |
| 419 | private: |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 420 | ASTContext* Context; |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 421 | MatchFinder::MatchCallback* Callback; |
| 422 | }; |
| 423 | |
Daniel Jasper | 20b802d | 2012-07-17 07:39:27 +0000 | [diff] [blame] | 424 | // Returns true if 'TypeNode' has an alias that matches the given matcher. |
| 425 | bool typeHasMatchingAlias(const Type *TypeNode, |
| 426 | const Matcher<NamedDecl> Matcher, |
| 427 | BoundNodesTreeBuilder *Builder) { |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 428 | const Type *const CanonicalType = |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 429 | ActiveASTContext->getCanonicalType(TypeNode); |
Daniel Jasper | 20b802d | 2012-07-17 07:39:27 +0000 | [diff] [blame] | 430 | const std::set<const TypedefDecl*> &Aliases = TypeAliases[CanonicalType]; |
| 431 | for (std::set<const TypedefDecl*>::const_iterator |
| 432 | It = Aliases.begin(), End = Aliases.end(); |
| 433 | It != End; ++It) { |
| 434 | if (Matcher.matches(**It, this, Builder)) |
| 435 | return true; |
| 436 | } |
| 437 | return false; |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | // Matches all registered matchers on the given node and calls the |
| 441 | // result callback for every node that matches. |
| 442 | template <typename T> |
| 443 | void match(const T &node) { |
Manuel Klimek | a78d0d6 | 2012-09-05 12:12:07 +0000 | [diff] [blame] | 444 | for (std::vector<std::pair<const internal::DynTypedMatcher*, |
| 445 | MatchCallback*> >::const_iterator |
| 446 | I = MatcherCallbackPairs->begin(), E = MatcherCallbackPairs->end(); |
| 447 | I != E; ++I) { |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 448 | BoundNodesTreeBuilder Builder; |
Manuel Klimek | a78d0d6 | 2012-09-05 12:12:07 +0000 | [diff] [blame] | 449 | if (I->first->matches(ast_type_traits::DynTypedNode::create(node), |
| 450 | this, &Builder)) { |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 451 | BoundNodesTree BoundNodes = Builder.build(); |
Manuel Klimek | a78d0d6 | 2012-09-05 12:12:07 +0000 | [diff] [blame] | 452 | MatchVisitor Visitor(ActiveASTContext, I->second); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 453 | BoundNodes.visitMatches(&Visitor); |
| 454 | } |
| 455 | } |
| 456 | } |
| 457 | |
Manuel Klimek | a78d0d6 | 2012-09-05 12:12:07 +0000 | [diff] [blame] | 458 | std::vector<std::pair<const internal::DynTypedMatcher*, |
| 459 | MatchCallback*> > *const MatcherCallbackPairs; |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 460 | ASTContext *ActiveASTContext; |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 461 | |
Daniel Jasper | 20b802d | 2012-07-17 07:39:27 +0000 | [diff] [blame] | 462 | // Maps a canonical type to its TypedefDecls. |
| 463 | llvm::DenseMap<const Type*, std::set<const TypedefDecl*> > TypeAliases; |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 464 | |
| 465 | // Maps (matcher, node) -> the match result for memoization. |
| 466 | typedef llvm::DenseMap<UntypedMatchInput, MemoizedMatchResult> MemoizationMap; |
| 467 | MemoizationMap ResultCache; |
Manuel Klimek | 579b120 | 2012-09-07 09:26:10 +0000 | [diff] [blame] | 468 | |
| 469 | llvm::OwningPtr<ParentMapASTVisitor::ParentMap> Parents; |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 470 | }; |
| 471 | |
| 472 | // Returns true if the given class is directly or indirectly derived |
Daniel Jasper | 76dafa7 | 2012-09-07 12:48:17 +0000 | [diff] [blame] | 473 | // from a base type with the given name. A class is not considered to be |
| 474 | // derived from itself. |
Daniel Jasper | 20b802d | 2012-07-17 07:39:27 +0000 | [diff] [blame] | 475 | bool MatchASTVisitor::classIsDerivedFrom(const CXXRecordDecl *Declaration, |
| 476 | const Matcher<NamedDecl> &Base, |
| 477 | BoundNodesTreeBuilder *Builder) { |
Daniel Jasper | 20b802d | 2012-07-17 07:39:27 +0000 | [diff] [blame] | 478 | if (!Declaration->hasDefinition()) |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 479 | return false; |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 480 | typedef CXXRecordDecl::base_class_const_iterator BaseIterator; |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 481 | for (BaseIterator It = Declaration->bases_begin(), |
| 482 | End = Declaration->bases_end(); It != End; ++It) { |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 483 | const Type *TypeNode = It->getType().getTypePtr(); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 484 | |
Daniel Jasper | 20b802d | 2012-07-17 07:39:27 +0000 | [diff] [blame] | 485 | if (typeHasMatchingAlias(TypeNode, Base, Builder)) |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 486 | return true; |
| 487 | |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 488 | // Type::getAs<...>() drills through typedefs. |
| 489 | if (TypeNode->getAs<DependentNameType>() != NULL || |
Daniel Jasper | 08f0c53 | 2012-09-18 14:17:42 +0000 | [diff] [blame] | 490 | TypeNode->getAs<DependentTemplateSpecializationType>() != NULL || |
Daniel Jasper | 20b802d | 2012-07-17 07:39:27 +0000 | [diff] [blame] | 491 | TypeNode->getAs<TemplateTypeParmType>() != NULL) |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 492 | // Dependent names and template TypeNode parameters will be matched when |
| 493 | // the template is instantiated. |
| 494 | continue; |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 495 | CXXRecordDecl *ClassDecl = NULL; |
| 496 | TemplateSpecializationType const *TemplateType = |
| 497 | TypeNode->getAs<TemplateSpecializationType>(); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 498 | if (TemplateType != NULL) { |
Daniel Jasper | 20b802d | 2012-07-17 07:39:27 +0000 | [diff] [blame] | 499 | if (TemplateType->getTemplateName().isDependent()) |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 500 | // Dependent template specializations will be matched when the |
| 501 | // template is instantiated. |
| 502 | continue; |
Daniel Jasper | 20b802d | 2012-07-17 07:39:27 +0000 | [diff] [blame] | 503 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 504 | // For template specialization types which are specializing a template |
| 505 | // declaration which is an explicit or partial specialization of another |
| 506 | // template declaration, getAsCXXRecordDecl() returns the corresponding |
| 507 | // ClassTemplateSpecializationDecl. |
| 508 | // |
| 509 | // For template specialization types which are specializing a template |
| 510 | // declaration which is neither an explicit nor partial specialization of |
| 511 | // another template declaration, getAsCXXRecordDecl() returns NULL and |
| 512 | // we get the CXXRecordDecl of the templated declaration. |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 513 | CXXRecordDecl *SpecializationDecl = |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 514 | TemplateType->getAsCXXRecordDecl(); |
| 515 | if (SpecializationDecl != NULL) { |
| 516 | ClassDecl = SpecializationDecl; |
| 517 | } else { |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 518 | ClassDecl = llvm::dyn_cast<CXXRecordDecl>( |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 519 | TemplateType->getTemplateName() |
| 520 | .getAsTemplateDecl()->getTemplatedDecl()); |
| 521 | } |
| 522 | } else { |
| 523 | ClassDecl = TypeNode->getAsCXXRecordDecl(); |
| 524 | } |
| 525 | assert(ClassDecl != NULL); |
| 526 | assert(ClassDecl != Declaration); |
Daniel Jasper | 76dafa7 | 2012-09-07 12:48:17 +0000 | [diff] [blame] | 527 | if (Base.matches(*ClassDecl, this, Builder)) |
| 528 | return true; |
Daniel Jasper | 20b802d | 2012-07-17 07:39:27 +0000 | [diff] [blame] | 529 | if (classIsDerivedFrom(ClassDecl, Base, Builder)) |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 530 | return true; |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 531 | } |
| 532 | return false; |
| 533 | } |
| 534 | |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 535 | bool MatchASTVisitor::TraverseDecl(Decl *DeclNode) { |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 536 | if (DeclNode == NULL) { |
| 537 | return true; |
| 538 | } |
| 539 | match(*DeclNode); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 540 | return RecursiveASTVisitor<MatchASTVisitor>::TraverseDecl(DeclNode); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 541 | } |
| 542 | |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 543 | bool MatchASTVisitor::TraverseStmt(Stmt *StmtNode) { |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 544 | if (StmtNode == NULL) { |
| 545 | return true; |
| 546 | } |
| 547 | match(*StmtNode); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 548 | return RecursiveASTVisitor<MatchASTVisitor>::TraverseStmt(StmtNode); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 549 | } |
| 550 | |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 551 | bool MatchASTVisitor::TraverseType(QualType TypeNode) { |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 552 | match(TypeNode); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 553 | return RecursiveASTVisitor<MatchASTVisitor>::TraverseType(TypeNode); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 554 | } |
| 555 | |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 556 | bool MatchASTVisitor::TraverseTypeLoc(TypeLoc TypeLoc) { |
Daniel Jasper | 9bd2809 | 2012-07-30 05:03:25 +0000 | [diff] [blame] | 557 | match(TypeLoc.getType()); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 558 | return RecursiveASTVisitor<MatchASTVisitor>:: |
Daniel Jasper | 9bd2809 | 2012-07-30 05:03:25 +0000 | [diff] [blame] | 559 | TraverseTypeLoc(TypeLoc); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 560 | } |
| 561 | |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 562 | bool MatchASTVisitor::TraverseNestedNameSpecifier(NestedNameSpecifier *NNS) { |
| 563 | match(*NNS); |
| 564 | return RecursiveASTVisitor<MatchASTVisitor>::TraverseNestedNameSpecifier(NNS); |
| 565 | } |
| 566 | |
| 567 | bool MatchASTVisitor::TraverseNestedNameSpecifierLoc( |
| 568 | NestedNameSpecifierLoc NNS) { |
| 569 | match(NNS); |
| 570 | // We only match the nested name specifier here (as opposed to traversing it) |
| 571 | // because the traversal is already done in the parallel "Loc"-hierarchy. |
| 572 | match(*NNS.getNestedNameSpecifier()); |
| 573 | return |
| 574 | RecursiveASTVisitor<MatchASTVisitor>::TraverseNestedNameSpecifierLoc(NNS); |
| 575 | } |
| 576 | |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 577 | class MatchASTConsumer : public ASTConsumer { |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 578 | public: |
Manuel Klimek | a78d0d6 | 2012-09-05 12:12:07 +0000 | [diff] [blame] | 579 | MatchASTConsumer( |
| 580 | std::vector<std::pair<const internal::DynTypedMatcher*, |
| 581 | MatchCallback*> > *MatcherCallbackPairs, |
| 582 | MatchFinder::ParsingDoneTestCallback *ParsingDone) |
| 583 | : Visitor(MatcherCallbackPairs), |
| 584 | ParsingDone(ParsingDone) {} |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 585 | |
| 586 | private: |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 587 | virtual void HandleTranslationUnit(ASTContext &Context) { |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 588 | if (ParsingDone != NULL) { |
| 589 | ParsingDone->run(); |
| 590 | } |
| 591 | Visitor.set_active_ast_context(&Context); |
| 592 | Visitor.TraverseDecl(Context.getTranslationUnitDecl()); |
| 593 | Visitor.set_active_ast_context(NULL); |
| 594 | } |
| 595 | |
| 596 | MatchASTVisitor Visitor; |
| 597 | MatchFinder::ParsingDoneTestCallback *ParsingDone; |
| 598 | }; |
| 599 | |
| 600 | } // end namespace |
| 601 | } // end namespace internal |
| 602 | |
| 603 | MatchFinder::MatchResult::MatchResult(const BoundNodes &Nodes, |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 604 | ASTContext *Context) |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 605 | : Nodes(Nodes), Context(Context), |
| 606 | SourceManager(&Context->getSourceManager()) {} |
| 607 | |
| 608 | MatchFinder::MatchCallback::~MatchCallback() {} |
| 609 | MatchFinder::ParsingDoneTestCallback::~ParsingDoneTestCallback() {} |
| 610 | |
| 611 | MatchFinder::MatchFinder() : ParsingDone(NULL) {} |
| 612 | |
| 613 | MatchFinder::~MatchFinder() { |
Manuel Klimek | a78d0d6 | 2012-09-05 12:12:07 +0000 | [diff] [blame] | 614 | for (std::vector<std::pair<const internal::DynTypedMatcher*, |
| 615 | MatchCallback*> >::const_iterator |
| 616 | It = MatcherCallbackPairs.begin(), End = MatcherCallbackPairs.end(); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 617 | It != End; ++It) { |
| 618 | delete It->first; |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | void MatchFinder::addMatcher(const DeclarationMatcher &NodeMatch, |
| 623 | MatchCallback *Action) { |
Manuel Klimek | a78d0d6 | 2012-09-05 12:12:07 +0000 | [diff] [blame] | 624 | MatcherCallbackPairs.push_back(std::make_pair( |
| 625 | new internal::Matcher<Decl>(NodeMatch), Action)); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | void MatchFinder::addMatcher(const TypeMatcher &NodeMatch, |
| 629 | MatchCallback *Action) { |
Manuel Klimek | a78d0d6 | 2012-09-05 12:12:07 +0000 | [diff] [blame] | 630 | MatcherCallbackPairs.push_back(std::make_pair( |
| 631 | new internal::Matcher<QualType>(NodeMatch), Action)); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | void MatchFinder::addMatcher(const StatementMatcher &NodeMatch, |
| 635 | MatchCallback *Action) { |
Manuel Klimek | a78d0d6 | 2012-09-05 12:12:07 +0000 | [diff] [blame] | 636 | MatcherCallbackPairs.push_back(std::make_pair( |
| 637 | new internal::Matcher<Stmt>(NodeMatch), Action)); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 638 | } |
| 639 | |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 640 | void MatchFinder::addMatcher(const NestedNameSpecifierMatcher &NodeMatch, |
| 641 | MatchCallback *Action) { |
| 642 | MatcherCallbackPairs.push_back(std::make_pair( |
| 643 | new NestedNameSpecifierMatcher(NodeMatch), Action)); |
| 644 | } |
| 645 | |
| 646 | void MatchFinder::addMatcher(const NestedNameSpecifierLocMatcher &NodeMatch, |
| 647 | MatchCallback *Action) { |
| 648 | MatcherCallbackPairs.push_back(std::make_pair( |
| 649 | new NestedNameSpecifierLocMatcher(NodeMatch), Action)); |
| 650 | } |
| 651 | |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 652 | ASTConsumer *MatchFinder::newASTConsumer() { |
Manuel Klimek | a78d0d6 | 2012-09-05 12:12:07 +0000 | [diff] [blame] | 653 | return new internal::MatchASTConsumer(&MatcherCallbackPairs, ParsingDone); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | void MatchFinder::registerTestCallbackAfterParsing( |
| 657 | MatchFinder::ParsingDoneTestCallback *NewParsingDone) { |
| 658 | ParsingDone = NewParsingDone; |
| 659 | } |
| 660 | |
| 661 | } // end namespace ast_matchers |
| 662 | } // end namespace clang |