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