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