2012-07-06 13:48:52 +08:00
|
|
|
//===--- ASTMatchFinder.cpp - Structural query framework ------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2012-07-06 13:48:52 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Implements an algorithm to efficiently search for matches on AST nodes.
|
|
|
|
// Uses memoization to support recursive matches like HasDescendant.
|
|
|
|
//
|
|
|
|
// The general idea is to visit all AST nodes with a RecursiveASTVisitor,
|
|
|
|
// calling the Matches(...) method of each matcher we are running on each
|
|
|
|
// AST node. The matcher can recurse via the ASTMatchFinder interface.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
|
|
|
#include "clang/AST/ASTConsumer.h"
|
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/AST/RecursiveASTVisitor.h"
|
2014-11-25 05:21:09 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2014-10-23 04:31:05 +08:00
|
|
|
#include "llvm/ADT/StringMap.h"
|
|
|
|
#include "llvm/Support/Timer.h"
|
2013-03-15 00:33:21 +08:00
|
|
|
#include <deque>
|
2014-10-23 04:31:05 +08:00
|
|
|
#include <memory>
|
2012-07-06 13:48:52 +08:00
|
|
|
#include <set>
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace ast_matchers {
|
|
|
|
namespace internal {
|
|
|
|
namespace {
|
|
|
|
|
2012-09-05 20:12:07 +08:00
|
|
|
typedef MatchFinder::MatchCallback MatchCallback;
|
|
|
|
|
Completely revamp node binding for AST matchers.
This is in preparation for the backwards references to bound
nodes, which will expose a lot more about how matches occur. Main
changes:
- instead of building the tree of bound nodes, we build a "set" of bound
nodes and explode all possible match combinations while running
through the matchers; this will allow us to also implement matchers
that filter down the current set of matches, like "equalsBoundNode"
- take the set of bound nodes at the start of the match into
consideration when doing memoization; as part of that, reevaluated
that memoization gives us benefits that are large enough (it still
does - the effect on common match patterns is up to an order of
magnitude)
- reset the bound nodes when a node does not match, thus never leaking
information from partial sub-matcher matches for failing matchers
Effects:
- we can now correctly "explode" combinatorial matches, for example:
allOf(forEachDescendant(...bind("a")),
forEachDescendant(...bind("b"))) will now trigger matches for all
combinations of matching "a" and "b"s.
- we now never expose bound nodes from partial matches in matchers that
did not match in the end - this fixes a long-standing issue
FIXMEs:
- rename BoundNodesTreeBuilder to BoundNodesBuilder or
BoundNodesSetBuilder, as we don't build a tree any more; this is out
of scope for this change, though
- we're seeing some performance regressions (around 10%), but I expect
some performance tuning will get that back, and it's easily worth
the increase in expressiveness for now
llvm-svn: 184313
2013-06-19 23:42:45 +08:00
|
|
|
// The maximum number of memoization entries to store.
|
|
|
|
// 10k has been experimentally found to give a good trade-off
|
|
|
|
// of performance vs. memory consumption by running matcher
|
|
|
|
// that match on every statement over a very large codebase.
|
|
|
|
//
|
|
|
|
// FIXME: Do some performance optimization in general and
|
|
|
|
// revisit this number; also, put up micro-benchmarks that we can
|
|
|
|
// optimize this on.
|
|
|
|
static const unsigned MaxMemoizationEntries = 10000;
|
|
|
|
|
2012-07-06 13:48:52 +08:00
|
|
|
// We use memoization to avoid running the same matcher on the same
|
Completely revamp node binding for AST matchers.
This is in preparation for the backwards references to bound
nodes, which will expose a lot more about how matches occur. Main
changes:
- instead of building the tree of bound nodes, we build a "set" of bound
nodes and explode all possible match combinations while running
through the matchers; this will allow us to also implement matchers
that filter down the current set of matches, like "equalsBoundNode"
- take the set of bound nodes at the start of the match into
consideration when doing memoization; as part of that, reevaluated
that memoization gives us benefits that are large enough (it still
does - the effect on common match patterns is up to an order of
magnitude)
- reset the bound nodes when a node does not match, thus never leaking
information from partial sub-matcher matches for failing matchers
Effects:
- we can now correctly "explode" combinatorial matches, for example:
allOf(forEachDescendant(...bind("a")),
forEachDescendant(...bind("b"))) will now trigger matches for all
combinations of matching "a" and "b"s.
- we now never expose bound nodes from partial matches in matchers that
did not match in the end - this fixes a long-standing issue
FIXMEs:
- rename BoundNodesTreeBuilder to BoundNodesBuilder or
BoundNodesSetBuilder, as we don't build a tree any more; this is out
of scope for this change, though
- we're seeing some performance regressions (around 10%), but I expect
some performance tuning will get that back, and it's easily worth
the increase in expressiveness for now
llvm-svn: 184313
2013-06-19 23:42:45 +08:00
|
|
|
// AST node twice. This struct is the key for looking up match
|
2012-07-06 13:48:52 +08:00
|
|
|
// result. It consists of an ID of the MatcherInterface (for
|
Completely revamp node binding for AST matchers.
This is in preparation for the backwards references to bound
nodes, which will expose a lot more about how matches occur. Main
changes:
- instead of building the tree of bound nodes, we build a "set" of bound
nodes and explode all possible match combinations while running
through the matchers; this will allow us to also implement matchers
that filter down the current set of matches, like "equalsBoundNode"
- take the set of bound nodes at the start of the match into
consideration when doing memoization; as part of that, reevaluated
that memoization gives us benefits that are large enough (it still
does - the effect on common match patterns is up to an order of
magnitude)
- reset the bound nodes when a node does not match, thus never leaking
information from partial sub-matcher matches for failing matchers
Effects:
- we can now correctly "explode" combinatorial matches, for example:
allOf(forEachDescendant(...bind("a")),
forEachDescendant(...bind("b"))) will now trigger matches for all
combinations of matching "a" and "b"s.
- we now never expose bound nodes from partial matches in matchers that
did not match in the end - this fixes a long-standing issue
FIXMEs:
- rename BoundNodesTreeBuilder to BoundNodesBuilder or
BoundNodesSetBuilder, as we don't build a tree any more; this is out
of scope for this change, though
- we're seeing some performance regressions (around 10%), but I expect
some performance tuning will get that back, and it's easily worth
the increase in expressiveness for now
llvm-svn: 184313
2013-06-19 23:42:45 +08:00
|
|
|
// identifying the matcher), a pointer to the AST node and the
|
|
|
|
// bound nodes before the matcher was executed.
|
2012-09-05 20:12:07 +08:00
|
|
|
//
|
|
|
|
// We currently only memoize on nodes whose pointers identify the
|
|
|
|
// nodes (\c Stmt and \c Decl, but not \c QualType or \c TypeLoc).
|
|
|
|
// For \c QualType and \c TypeLoc it is possible to implement
|
|
|
|
// generation of keys for each type.
|
|
|
|
// FIXME: Benchmark whether memoization of non-pointer typed nodes
|
|
|
|
// provides enough benefit for the additional amount of code.
|
Completely revamp node binding for AST matchers.
This is in preparation for the backwards references to bound
nodes, which will expose a lot more about how matches occur. Main
changes:
- instead of building the tree of bound nodes, we build a "set" of bound
nodes and explode all possible match combinations while running
through the matchers; this will allow us to also implement matchers
that filter down the current set of matches, like "equalsBoundNode"
- take the set of bound nodes at the start of the match into
consideration when doing memoization; as part of that, reevaluated
that memoization gives us benefits that are large enough (it still
does - the effect on common match patterns is up to an order of
magnitude)
- reset the bound nodes when a node does not match, thus never leaking
information from partial sub-matcher matches for failing matchers
Effects:
- we can now correctly "explode" combinatorial matches, for example:
allOf(forEachDescendant(...bind("a")),
forEachDescendant(...bind("b"))) will now trigger matches for all
combinations of matching "a" and "b"s.
- we now never expose bound nodes from partial matches in matchers that
did not match in the end - this fixes a long-standing issue
FIXMEs:
- rename BoundNodesTreeBuilder to BoundNodesBuilder or
BoundNodesSetBuilder, as we don't build a tree any more; this is out
of scope for this change, though
- we're seeing some performance regressions (around 10%), but I expect
some performance tuning will get that back, and it's easily worth
the increase in expressiveness for now
llvm-svn: 184313
2013-06-19 23:42:45 +08:00
|
|
|
struct MatchKey {
|
2014-10-01 23:08:07 +08:00
|
|
|
DynTypedMatcher::MatcherIDType MatcherID;
|
Completely revamp node binding for AST matchers.
This is in preparation for the backwards references to bound
nodes, which will expose a lot more about how matches occur. Main
changes:
- instead of building the tree of bound nodes, we build a "set" of bound
nodes and explode all possible match combinations while running
through the matchers; this will allow us to also implement matchers
that filter down the current set of matches, like "equalsBoundNode"
- take the set of bound nodes at the start of the match into
consideration when doing memoization; as part of that, reevaluated
that memoization gives us benefits that are large enough (it still
does - the effect on common match patterns is up to an order of
magnitude)
- reset the bound nodes when a node does not match, thus never leaking
information from partial sub-matcher matches for failing matchers
Effects:
- we can now correctly "explode" combinatorial matches, for example:
allOf(forEachDescendant(...bind("a")),
forEachDescendant(...bind("b"))) will now trigger matches for all
combinations of matching "a" and "b"s.
- we now never expose bound nodes from partial matches in matchers that
did not match in the end - this fixes a long-standing issue
FIXMEs:
- rename BoundNodesTreeBuilder to BoundNodesBuilder or
BoundNodesSetBuilder, as we don't build a tree any more; this is out
of scope for this change, though
- we're seeing some performance regressions (around 10%), but I expect
some performance tuning will get that back, and it's easily worth
the increase in expressiveness for now
llvm-svn: 184313
2013-06-19 23:42:45 +08:00
|
|
|
ast_type_traits::DynTypedNode Node;
|
|
|
|
BoundNodesTreeBuilder BoundNodes;
|
2019-05-13 04:09:32 +08:00
|
|
|
ast_type_traits::TraversalKind Traversal = ast_type_traits::TK_AsIs;
|
Completely revamp node binding for AST matchers.
This is in preparation for the backwards references to bound
nodes, which will expose a lot more about how matches occur. Main
changes:
- instead of building the tree of bound nodes, we build a "set" of bound
nodes and explode all possible match combinations while running
through the matchers; this will allow us to also implement matchers
that filter down the current set of matches, like "equalsBoundNode"
- take the set of bound nodes at the start of the match into
consideration when doing memoization; as part of that, reevaluated
that memoization gives us benefits that are large enough (it still
does - the effect on common match patterns is up to an order of
magnitude)
- reset the bound nodes when a node does not match, thus never leaking
information from partial sub-matcher matches for failing matchers
Effects:
- we can now correctly "explode" combinatorial matches, for example:
allOf(forEachDescendant(...bind("a")),
forEachDescendant(...bind("b"))) will now trigger matches for all
combinations of matching "a" and "b"s.
- we now never expose bound nodes from partial matches in matchers that
did not match in the end - this fixes a long-standing issue
FIXMEs:
- rename BoundNodesTreeBuilder to BoundNodesBuilder or
BoundNodesSetBuilder, as we don't build a tree any more; this is out
of scope for this change, though
- we're seeing some performance regressions (around 10%), but I expect
some performance tuning will get that back, and it's easily worth
the increase in expressiveness for now
llvm-svn: 184313
2013-06-19 23:42:45 +08:00
|
|
|
|
|
|
|
bool operator<(const MatchKey &Other) const {
|
2019-05-13 04:09:32 +08:00
|
|
|
return std::tie(MatcherID, Node, BoundNodes, Traversal) <
|
|
|
|
std::tie(Other.MatcherID, Other.Node, Other.BoundNodes,
|
|
|
|
Other.Traversal);
|
Completely revamp node binding for AST matchers.
This is in preparation for the backwards references to bound
nodes, which will expose a lot more about how matches occur. Main
changes:
- instead of building the tree of bound nodes, we build a "set" of bound
nodes and explode all possible match combinations while running
through the matchers; this will allow us to also implement matchers
that filter down the current set of matches, like "equalsBoundNode"
- take the set of bound nodes at the start of the match into
consideration when doing memoization; as part of that, reevaluated
that memoization gives us benefits that are large enough (it still
does - the effect on common match patterns is up to an order of
magnitude)
- reset the bound nodes when a node does not match, thus never leaking
information from partial sub-matcher matches for failing matchers
Effects:
- we can now correctly "explode" combinatorial matches, for example:
allOf(forEachDescendant(...bind("a")),
forEachDescendant(...bind("b"))) will now trigger matches for all
combinations of matching "a" and "b"s.
- we now never expose bound nodes from partial matches in matchers that
did not match in the end - this fixes a long-standing issue
FIXMEs:
- rename BoundNodesTreeBuilder to BoundNodesBuilder or
BoundNodesSetBuilder, as we don't build a tree any more; this is out
of scope for this change, though
- we're seeing some performance regressions (around 10%), but I expect
some performance tuning will get that back, and it's easily worth
the increase in expressiveness for now
llvm-svn: 184313
2013-06-19 23:42:45 +08:00
|
|
|
}
|
|
|
|
};
|
2012-07-06 13:48:52 +08:00
|
|
|
|
|
|
|
// Used to store the result of a match and possibly bound nodes.
|
|
|
|
struct MemoizedMatchResult {
|
|
|
|
bool ResultOfMatch;
|
Completely revamp node binding for AST matchers.
This is in preparation for the backwards references to bound
nodes, which will expose a lot more about how matches occur. Main
changes:
- instead of building the tree of bound nodes, we build a "set" of bound
nodes and explode all possible match combinations while running
through the matchers; this will allow us to also implement matchers
that filter down the current set of matches, like "equalsBoundNode"
- take the set of bound nodes at the start of the match into
consideration when doing memoization; as part of that, reevaluated
that memoization gives us benefits that are large enough (it still
does - the effect on common match patterns is up to an order of
magnitude)
- reset the bound nodes when a node does not match, thus never leaking
information from partial sub-matcher matches for failing matchers
Effects:
- we can now correctly "explode" combinatorial matches, for example:
allOf(forEachDescendant(...bind("a")),
forEachDescendant(...bind("b"))) will now trigger matches for all
combinations of matching "a" and "b"s.
- we now never expose bound nodes from partial matches in matchers that
did not match in the end - this fixes a long-standing issue
FIXMEs:
- rename BoundNodesTreeBuilder to BoundNodesBuilder or
BoundNodesSetBuilder, as we don't build a tree any more; this is out
of scope for this change, though
- we're seeing some performance regressions (around 10%), but I expect
some performance tuning will get that back, and it's easily worth
the increase in expressiveness for now
llvm-svn: 184313
2013-06-19 23:42:45 +08:00
|
|
|
BoundNodesTreeBuilder Nodes;
|
2012-07-06 13:48:52 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// A RecursiveASTVisitor that traverses all children or all descendants of
|
|
|
|
// a node.
|
|
|
|
class MatchChildASTVisitor
|
2012-07-11 04:20:19 +08:00
|
|
|
: public RecursiveASTVisitor<MatchChildASTVisitor> {
|
2012-07-06 13:48:52 +08:00
|
|
|
public:
|
2012-07-11 04:20:19 +08:00
|
|
|
typedef RecursiveASTVisitor<MatchChildASTVisitor> VisitorBase;
|
2012-07-06 13:48:52 +08:00
|
|
|
|
|
|
|
// Creates an AST visitor that matches 'matcher' on all children or
|
|
|
|
// descendants of a traversed node. max_depth is the maximum depth
|
|
|
|
// to traverse: use 1 for matching the children and INT_MAX for
|
|
|
|
// matching the descendants.
|
2019-05-17 01:57:38 +08:00
|
|
|
MatchChildASTVisitor(const DynTypedMatcher *Matcher, ASTMatchFinder *Finder,
|
|
|
|
BoundNodesTreeBuilder *Builder, int MaxDepth,
|
|
|
|
ast_type_traits::TraversalKind Traversal,
|
2012-07-06 13:48:52 +08:00
|
|
|
ASTMatchFinder::BindKind Bind)
|
2019-05-17 01:57:38 +08:00
|
|
|
: Matcher(Matcher), Finder(Finder), Builder(Builder), CurrentDepth(0),
|
|
|
|
MaxDepth(MaxDepth), Traversal(Traversal), Bind(Bind), Matches(false) {}
|
2012-07-06 13:48:52 +08:00
|
|
|
|
|
|
|
// Returns true if a match is found in the subtree rooted at the
|
|
|
|
// given AST node. This is done via a set of mutually recursive
|
|
|
|
// functions. Here's how the recursion is done (the *wildcard can
|
|
|
|
// actually be Decl, Stmt, or Type):
|
|
|
|
//
|
|
|
|
// - Traverse(node) calls BaseTraverse(node) when it needs
|
|
|
|
// to visit the descendants of node.
|
|
|
|
// - BaseTraverse(node) then calls (via VisitorBase::Traverse*(node))
|
|
|
|
// Traverse*(c) for each child c of 'node'.
|
|
|
|
// - Traverse*(c) in turn calls Traverse(c), completing the
|
|
|
|
// recursion.
|
2012-09-05 20:12:07 +08:00
|
|
|
bool findMatch(const ast_type_traits::DynTypedNode &DynNode) {
|
2012-07-06 13:48:52 +08:00
|
|
|
reset();
|
2012-09-05 20:12:07 +08:00
|
|
|
if (const Decl *D = DynNode.get<Decl>())
|
|
|
|
traverse(*D);
|
|
|
|
else if (const Stmt *S = DynNode.get<Stmt>())
|
|
|
|
traverse(*S);
|
2012-10-30 23:42:00 +08:00
|
|
|
else if (const NestedNameSpecifier *NNS =
|
|
|
|
DynNode.get<NestedNameSpecifier>())
|
|
|
|
traverse(*NNS);
|
|
|
|
else if (const NestedNameSpecifierLoc *NNSLoc =
|
|
|
|
DynNode.get<NestedNameSpecifierLoc>())
|
|
|
|
traverse(*NNSLoc);
|
2012-10-29 18:14:44 +08:00
|
|
|
else if (const QualType *Q = DynNode.get<QualType>())
|
|
|
|
traverse(*Q);
|
|
|
|
else if (const TypeLoc *T = DynNode.get<TypeLoc>())
|
|
|
|
traverse(*T);
|
2016-09-27 01:04:27 +08:00
|
|
|
else if (const auto *C = DynNode.get<CXXCtorInitializer>())
|
|
|
|
traverse(*C);
|
2012-09-05 20:12:07 +08:00
|
|
|
// FIXME: Add other base types after adding tests.
|
Completely revamp node binding for AST matchers.
This is in preparation for the backwards references to bound
nodes, which will expose a lot more about how matches occur. Main
changes:
- instead of building the tree of bound nodes, we build a "set" of bound
nodes and explode all possible match combinations while running
through the matchers; this will allow us to also implement matchers
that filter down the current set of matches, like "equalsBoundNode"
- take the set of bound nodes at the start of the match into
consideration when doing memoization; as part of that, reevaluated
that memoization gives us benefits that are large enough (it still
does - the effect on common match patterns is up to an order of
magnitude)
- reset the bound nodes when a node does not match, thus never leaking
information from partial sub-matcher matches for failing matchers
Effects:
- we can now correctly "explode" combinatorial matches, for example:
allOf(forEachDescendant(...bind("a")),
forEachDescendant(...bind("b"))) will now trigger matches for all
combinations of matching "a" and "b"s.
- we now never expose bound nodes from partial matches in matchers that
did not match in the end - this fixes a long-standing issue
FIXMEs:
- rename BoundNodesTreeBuilder to BoundNodesBuilder or
BoundNodesSetBuilder, as we don't build a tree any more; this is out
of scope for this change, though
- we're seeing some performance regressions (around 10%), but I expect
some performance tuning will get that back, and it's easily worth
the increase in expressiveness for now
llvm-svn: 184313
2013-06-19 23:42:45 +08:00
|
|
|
|
|
|
|
// It's OK to always overwrite the bound nodes, as if there was
|
|
|
|
// no match in this recursive branch, the result set is empty
|
|
|
|
// anyway.
|
|
|
|
*Builder = ResultBindings;
|
|
|
|
|
2012-07-06 13:48:52 +08:00
|
|
|
return Matches;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The following are overriding methods from the base visitor class.
|
|
|
|
// They are public only to allow CRTP to work. They are *not *part
|
|
|
|
// of the public API of this class.
|
2012-07-11 04:20:19 +08:00
|
|
|
bool TraverseDecl(Decl *DeclNode) {
|
2012-10-29 18:14:44 +08:00
|
|
|
ScopedIncrement ScopedDepth(&CurrentDepth);
|
2014-05-18 02:49:24 +08:00
|
|
|
return (DeclNode == nullptr) || traverse(*DeclNode);
|
2012-07-06 13:48:52 +08:00
|
|
|
}
|
2018-03-03 05:55:03 +08:00
|
|
|
|
2019-12-19 06:35:46 +08:00
|
|
|
Stmt *getStmtToTraverse(Stmt *StmtNode) {
|
2018-03-03 05:55:03 +08:00
|
|
|
Stmt *StmtToTraverse = StmtNode;
|
2019-12-19 06:35:46 +08:00
|
|
|
if (auto *ExprNode = dyn_cast_or_null<Expr>(StmtNode)) {
|
|
|
|
auto *LambdaNode = dyn_cast_or_null<LambdaExpr>(StmtNode);
|
|
|
|
if (LambdaNode && Finder->getASTContext().getTraversalKind() ==
|
|
|
|
ast_type_traits::TK_IgnoreUnlessSpelledInSource)
|
|
|
|
StmtToTraverse = LambdaNode;
|
|
|
|
else
|
|
|
|
StmtToTraverse = Finder->getASTContext().traverseIgnored(ExprNode);
|
|
|
|
}
|
2019-05-17 01:57:38 +08:00
|
|
|
if (Traversal ==
|
|
|
|
ast_type_traits::TraversalKind::TK_IgnoreImplicitCastsAndParentheses) {
|
2018-03-03 05:55:03 +08:00
|
|
|
if (Expr *ExprNode = dyn_cast_or_null<Expr>(StmtNode))
|
2012-07-06 13:48:52 +08:00
|
|
|
StmtToTraverse = ExprNode->IgnoreParenImpCasts();
|
|
|
|
}
|
2019-12-19 06:35:46 +08:00
|
|
|
return StmtToTraverse;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TraverseStmt(Stmt *StmtNode, DataRecursionQueue *Queue = nullptr) {
|
|
|
|
// If we need to keep track of the depth, we can't perform data recursion.
|
|
|
|
if (CurrentDepth == 0 || (CurrentDepth <= MaxDepth && MaxDepth < INT_MAX))
|
|
|
|
Queue = nullptr;
|
|
|
|
|
|
|
|
ScopedIncrement ScopedDepth(&CurrentDepth);
|
|
|
|
Stmt *StmtToTraverse = getStmtToTraverse(StmtNode);
|
2018-03-03 05:55:03 +08:00
|
|
|
if (!StmtToTraverse)
|
|
|
|
return true;
|
|
|
|
if (!match(*StmtToTraverse))
|
|
|
|
return false;
|
|
|
|
return VisitorBase::TraverseStmt(StmtToTraverse, Queue);
|
2012-07-06 13:48:52 +08:00
|
|
|
}
|
2012-10-29 18:14:44 +08:00
|
|
|
// We assume that the QualType and the contained type are on the same
|
|
|
|
// hierarchy level. Thus, we try to match either of them.
|
2012-07-11 04:20:19 +08:00
|
|
|
bool TraverseType(QualType TypeNode) {
|
2012-11-14 01:14:11 +08:00
|
|
|
if (TypeNode.isNull())
|
|
|
|
return true;
|
2012-10-29 18:14:44 +08:00
|
|
|
ScopedIncrement ScopedDepth(&CurrentDepth);
|
|
|
|
// Match the Type.
|
|
|
|
if (!match(*TypeNode))
|
|
|
|
return false;
|
|
|
|
// The QualType is matched inside traverse.
|
2012-07-06 13:48:52 +08:00
|
|
|
return traverse(TypeNode);
|
|
|
|
}
|
2012-10-29 18:14:44 +08:00
|
|
|
// We assume that the TypeLoc, contained QualType and contained Type all are
|
|
|
|
// on the same hierarchy level. Thus, we try to match all of them.
|
|
|
|
bool TraverseTypeLoc(TypeLoc TypeLocNode) {
|
2012-11-14 01:14:11 +08:00
|
|
|
if (TypeLocNode.isNull())
|
|
|
|
return true;
|
2012-10-29 18:14:44 +08:00
|
|
|
ScopedIncrement ScopedDepth(&CurrentDepth);
|
|
|
|
// Match the Type.
|
|
|
|
if (!match(*TypeLocNode.getType()))
|
|
|
|
return false;
|
|
|
|
// Match the QualType.
|
|
|
|
if (!match(TypeLocNode.getType()))
|
|
|
|
return false;
|
|
|
|
// The TypeLoc is matched inside traverse.
|
|
|
|
return traverse(TypeLocNode);
|
|
|
|
}
|
2012-10-30 23:42:00 +08:00
|
|
|
bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS) {
|
|
|
|
ScopedIncrement ScopedDepth(&CurrentDepth);
|
2014-05-18 02:49:24 +08:00
|
|
|
return (NNS == nullptr) || traverse(*NNS);
|
2012-10-30 23:42:00 +08:00
|
|
|
}
|
|
|
|
bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
|
2012-11-14 01:14:11 +08:00
|
|
|
if (!NNS)
|
|
|
|
return true;
|
2012-10-30 23:42:00 +08:00
|
|
|
ScopedIncrement ScopedDepth(&CurrentDepth);
|
|
|
|
if (!match(*NNS.getNestedNameSpecifier()))
|
|
|
|
return false;
|
2012-11-14 01:14:11 +08:00
|
|
|
return traverse(NNS);
|
2012-10-30 23:42:00 +08:00
|
|
|
}
|
2016-09-27 01:04:27 +08:00
|
|
|
bool TraverseConstructorInitializer(CXXCtorInitializer *CtorInit) {
|
|
|
|
if (!CtorInit)
|
|
|
|
return true;
|
|
|
|
ScopedIncrement ScopedDepth(&CurrentDepth);
|
|
|
|
return traverse(*CtorInit);
|
|
|
|
}
|
2019-12-19 06:35:46 +08:00
|
|
|
bool TraverseLambdaExpr(LambdaExpr *Node) {
|
|
|
|
if (Finder->getASTContext().getTraversalKind() !=
|
|
|
|
ast_type_traits::TK_IgnoreUnlessSpelledInSource)
|
|
|
|
return VisitorBase::TraverseLambdaExpr(Node);
|
|
|
|
if (!Node)
|
|
|
|
return true;
|
|
|
|
ScopedIncrement ScopedDepth(&CurrentDepth);
|
|
|
|
|
|
|
|
for (unsigned I = 0, N = Node->capture_size(); I != N; ++I) {
|
|
|
|
const auto *C = Node->capture_begin() + I;
|
|
|
|
if (!C->isExplicit())
|
|
|
|
continue;
|
|
|
|
if (Node->isInitCapture(C) && !match(*C->getCapturedVar()))
|
|
|
|
return false;
|
|
|
|
if (!match(*Node->capture_init_begin()[I]))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (const auto *TPL = Node->getTemplateParameterList()) {
|
|
|
|
for (const auto *TP : *TPL) {
|
|
|
|
if (!match(*TP))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto *P : Node->getCallOperator()->parameters()) {
|
|
|
|
if (!match(*P))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!match(*Node->getBody()))
|
|
|
|
return false;
|
|
|
|
|
2019-12-30 03:19:35 +08:00
|
|
|
return true;
|
2019-12-19 06:35:46 +08:00
|
|
|
}
|
2012-07-06 13:48:52 +08:00
|
|
|
|
|
|
|
bool shouldVisitTemplateInstantiations() const { return true; }
|
|
|
|
bool shouldVisitImplicitCode() const { return true; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Used for updating the depth during traversal.
|
|
|
|
struct ScopedIncrement {
|
|
|
|
explicit ScopedIncrement(int *Depth) : Depth(Depth) { ++(*Depth); }
|
|
|
|
~ScopedIncrement() { --(*Depth); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
int *Depth;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Resets the state of this object.
|
|
|
|
void reset() {
|
|
|
|
Matches = false;
|
2012-10-29 18:14:44 +08:00
|
|
|
CurrentDepth = 0;
|
2012-07-06 13:48:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Forwards the call to the corresponding Traverse*() method in the
|
|
|
|
// base visitor class.
|
2012-07-11 04:20:19 +08:00
|
|
|
bool baseTraverse(const Decl &DeclNode) {
|
|
|
|
return VisitorBase::TraverseDecl(const_cast<Decl*>(&DeclNode));
|
2012-07-06 13:48:52 +08:00
|
|
|
}
|
2012-07-11 04:20:19 +08:00
|
|
|
bool baseTraverse(const Stmt &StmtNode) {
|
|
|
|
return VisitorBase::TraverseStmt(const_cast<Stmt*>(&StmtNode));
|
2012-07-06 13:48:52 +08:00
|
|
|
}
|
2012-07-11 04:20:19 +08:00
|
|
|
bool baseTraverse(QualType TypeNode) {
|
2012-07-06 13:48:52 +08:00
|
|
|
return VisitorBase::TraverseType(TypeNode);
|
|
|
|
}
|
2012-10-29 18:14:44 +08:00
|
|
|
bool baseTraverse(TypeLoc TypeLocNode) {
|
|
|
|
return VisitorBase::TraverseTypeLoc(TypeLocNode);
|
|
|
|
}
|
2012-10-30 23:42:00 +08:00
|
|
|
bool baseTraverse(const NestedNameSpecifier &NNS) {
|
|
|
|
return VisitorBase::TraverseNestedNameSpecifier(
|
|
|
|
const_cast<NestedNameSpecifier*>(&NNS));
|
|
|
|
}
|
|
|
|
bool baseTraverse(NestedNameSpecifierLoc NNS) {
|
|
|
|
return VisitorBase::TraverseNestedNameSpecifierLoc(NNS);
|
|
|
|
}
|
2016-09-27 01:04:27 +08:00
|
|
|
bool baseTraverse(const CXXCtorInitializer &CtorInit) {
|
|
|
|
return VisitorBase::TraverseConstructorInitializer(
|
|
|
|
const_cast<CXXCtorInitializer *>(&CtorInit));
|
|
|
|
}
|
2012-07-06 13:48:52 +08:00
|
|
|
|
2012-10-29 18:14:44 +08:00
|
|
|
// Sets 'Matched' to true if 'Matcher' matches 'Node' and:
|
|
|
|
// 0 < CurrentDepth <= MaxDepth.
|
|
|
|
//
|
|
|
|
// Returns 'true' if traversal should continue after this function
|
|
|
|
// returns, i.e. if no match is found or 'Bind' is 'BK_All'.
|
2012-07-06 13:48:52 +08:00
|
|
|
template <typename T>
|
2012-10-29 18:14:44 +08:00
|
|
|
bool match(const T &Node) {
|
|
|
|
if (CurrentDepth == 0 || CurrentDepth > MaxDepth) {
|
|
|
|
return true;
|
2012-07-06 13:48:52 +08:00
|
|
|
}
|
|
|
|
if (Bind != ASTMatchFinder::BK_All) {
|
Completely revamp node binding for AST matchers.
This is in preparation for the backwards references to bound
nodes, which will expose a lot more about how matches occur. Main
changes:
- instead of building the tree of bound nodes, we build a "set" of bound
nodes and explode all possible match combinations while running
through the matchers; this will allow us to also implement matchers
that filter down the current set of matches, like "equalsBoundNode"
- take the set of bound nodes at the start of the match into
consideration when doing memoization; as part of that, reevaluated
that memoization gives us benefits that are large enough (it still
does - the effect on common match patterns is up to an order of
magnitude)
- reset the bound nodes when a node does not match, thus never leaking
information from partial sub-matcher matches for failing matchers
Effects:
- we can now correctly "explode" combinatorial matches, for example:
allOf(forEachDescendant(...bind("a")),
forEachDescendant(...bind("b"))) will now trigger matches for all
combinations of matching "a" and "b"s.
- we now never expose bound nodes from partial matches in matchers that
did not match in the end - this fixes a long-standing issue
FIXMEs:
- rename BoundNodesTreeBuilder to BoundNodesBuilder or
BoundNodesSetBuilder, as we don't build a tree any more; this is out
of scope for this change, though
- we're seeing some performance regressions (around 10%), but I expect
some performance tuning will get that back, and it's easily worth
the increase in expressiveness for now
llvm-svn: 184313
2013-06-19 23:42:45 +08:00
|
|
|
BoundNodesTreeBuilder RecursiveBuilder(*Builder);
|
|
|
|
if (Matcher->matches(ast_type_traits::DynTypedNode::create(Node), Finder,
|
|
|
|
&RecursiveBuilder)) {
|
2012-07-06 13:48:52 +08:00
|
|
|
Matches = true;
|
Completely revamp node binding for AST matchers.
This is in preparation for the backwards references to bound
nodes, which will expose a lot more about how matches occur. Main
changes:
- instead of building the tree of bound nodes, we build a "set" of bound
nodes and explode all possible match combinations while running
through the matchers; this will allow us to also implement matchers
that filter down the current set of matches, like "equalsBoundNode"
- take the set of bound nodes at the start of the match into
consideration when doing memoization; as part of that, reevaluated
that memoization gives us benefits that are large enough (it still
does - the effect on common match patterns is up to an order of
magnitude)
- reset the bound nodes when a node does not match, thus never leaking
information from partial sub-matcher matches for failing matchers
Effects:
- we can now correctly "explode" combinatorial matches, for example:
allOf(forEachDescendant(...bind("a")),
forEachDescendant(...bind("b"))) will now trigger matches for all
combinations of matching "a" and "b"s.
- we now never expose bound nodes from partial matches in matchers that
did not match in the end - this fixes a long-standing issue
FIXMEs:
- rename BoundNodesTreeBuilder to BoundNodesBuilder or
BoundNodesSetBuilder, as we don't build a tree any more; this is out
of scope for this change, though
- we're seeing some performance regressions (around 10%), but I expect
some performance tuning will get that back, and it's easily worth
the increase in expressiveness for now
llvm-svn: 184313
2013-06-19 23:42:45 +08:00
|
|
|
ResultBindings.addMatch(RecursiveBuilder);
|
|
|
|
return false; // Abort as soon as a match is found.
|
2012-07-06 13:48:52 +08:00
|
|
|
}
|
|
|
|
} else {
|
Completely revamp node binding for AST matchers.
This is in preparation for the backwards references to bound
nodes, which will expose a lot more about how matches occur. Main
changes:
- instead of building the tree of bound nodes, we build a "set" of bound
nodes and explode all possible match combinations while running
through the matchers; this will allow us to also implement matchers
that filter down the current set of matches, like "equalsBoundNode"
- take the set of bound nodes at the start of the match into
consideration when doing memoization; as part of that, reevaluated
that memoization gives us benefits that are large enough (it still
does - the effect on common match patterns is up to an order of
magnitude)
- reset the bound nodes when a node does not match, thus never leaking
information from partial sub-matcher matches for failing matchers
Effects:
- we can now correctly "explode" combinatorial matches, for example:
allOf(forEachDescendant(...bind("a")),
forEachDescendant(...bind("b"))) will now trigger matches for all
combinations of matching "a" and "b"s.
- we now never expose bound nodes from partial matches in matchers that
did not match in the end - this fixes a long-standing issue
FIXMEs:
- rename BoundNodesTreeBuilder to BoundNodesBuilder or
BoundNodesSetBuilder, as we don't build a tree any more; this is out
of scope for this change, though
- we're seeing some performance regressions (around 10%), but I expect
some performance tuning will get that back, and it's easily worth
the increase in expressiveness for now
llvm-svn: 184313
2013-06-19 23:42:45 +08:00
|
|
|
BoundNodesTreeBuilder RecursiveBuilder(*Builder);
|
|
|
|
if (Matcher->matches(ast_type_traits::DynTypedNode::create(Node), Finder,
|
|
|
|
&RecursiveBuilder)) {
|
2012-07-06 13:48:52 +08:00
|
|
|
// After the first match the matcher succeeds.
|
|
|
|
Matches = true;
|
Completely revamp node binding for AST matchers.
This is in preparation for the backwards references to bound
nodes, which will expose a lot more about how matches occur. Main
changes:
- instead of building the tree of bound nodes, we build a "set" of bound
nodes and explode all possible match combinations while running
through the matchers; this will allow us to also implement matchers
that filter down the current set of matches, like "equalsBoundNode"
- take the set of bound nodes at the start of the match into
consideration when doing memoization; as part of that, reevaluated
that memoization gives us benefits that are large enough (it still
does - the effect on common match patterns is up to an order of
magnitude)
- reset the bound nodes when a node does not match, thus never leaking
information from partial sub-matcher matches for failing matchers
Effects:
- we can now correctly "explode" combinatorial matches, for example:
allOf(forEachDescendant(...bind("a")),
forEachDescendant(...bind("b"))) will now trigger matches for all
combinations of matching "a" and "b"s.
- we now never expose bound nodes from partial matches in matchers that
did not match in the end - this fixes a long-standing issue
FIXMEs:
- rename BoundNodesTreeBuilder to BoundNodesBuilder or
BoundNodesSetBuilder, as we don't build a tree any more; this is out
of scope for this change, though
- we're seeing some performance regressions (around 10%), but I expect
some performance tuning will get that back, and it's easily worth
the increase in expressiveness for now
llvm-svn: 184313
2013-06-19 23:42:45 +08:00
|
|
|
ResultBindings.addMatch(RecursiveBuilder);
|
2012-07-06 13:48:52 +08:00
|
|
|
}
|
|
|
|
}
|
2012-10-29 18:14:44 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Traverses the subtree rooted at 'Node'; returns true if the
|
|
|
|
// traversal should continue after this function returns.
|
|
|
|
template <typename T>
|
|
|
|
bool traverse(const T &Node) {
|
2014-03-03 01:08:43 +08:00
|
|
|
static_assert(IsBaseType<T>::value,
|
|
|
|
"traverse can only be instantiated with base type");
|
2012-10-29 18:14:44 +08:00
|
|
|
if (!match(Node))
|
|
|
|
return false;
|
|
|
|
return baseTraverse(Node);
|
2012-07-06 13:48:52 +08:00
|
|
|
}
|
|
|
|
|
2012-09-05 20:12:07 +08:00
|
|
|
const DynTypedMatcher *const Matcher;
|
2012-07-06 13:48:52 +08:00
|
|
|
ASTMatchFinder *const Finder;
|
|
|
|
BoundNodesTreeBuilder *const Builder;
|
Completely revamp node binding for AST matchers.
This is in preparation for the backwards references to bound
nodes, which will expose a lot more about how matches occur. Main
changes:
- instead of building the tree of bound nodes, we build a "set" of bound
nodes and explode all possible match combinations while running
through the matchers; this will allow us to also implement matchers
that filter down the current set of matches, like "equalsBoundNode"
- take the set of bound nodes at the start of the match into
consideration when doing memoization; as part of that, reevaluated
that memoization gives us benefits that are large enough (it still
does - the effect on common match patterns is up to an order of
magnitude)
- reset the bound nodes when a node does not match, thus never leaking
information from partial sub-matcher matches for failing matchers
Effects:
- we can now correctly "explode" combinatorial matches, for example:
allOf(forEachDescendant(...bind("a")),
forEachDescendant(...bind("b"))) will now trigger matches for all
combinations of matching "a" and "b"s.
- we now never expose bound nodes from partial matches in matchers that
did not match in the end - this fixes a long-standing issue
FIXMEs:
- rename BoundNodesTreeBuilder to BoundNodesBuilder or
BoundNodesSetBuilder, as we don't build a tree any more; this is out
of scope for this change, though
- we're seeing some performance regressions (around 10%), but I expect
some performance tuning will get that back, and it's easily worth
the increase in expressiveness for now
llvm-svn: 184313
2013-06-19 23:42:45 +08:00
|
|
|
BoundNodesTreeBuilder ResultBindings;
|
2012-07-06 13:48:52 +08:00
|
|
|
int CurrentDepth;
|
|
|
|
const int MaxDepth;
|
2019-05-17 01:57:38 +08:00
|
|
|
const ast_type_traits::TraversalKind Traversal;
|
2012-07-06 13:48:52 +08:00
|
|
|
const ASTMatchFinder::BindKind Bind;
|
|
|
|
bool Matches;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Controls the outermost traversal of the AST and allows to match multiple
|
|
|
|
// matchers.
|
2012-07-11 04:20:19 +08:00
|
|
|
class MatchASTVisitor : public RecursiveASTVisitor<MatchASTVisitor>,
|
2012-07-06 13:48:52 +08:00
|
|
|
public ASTMatchFinder {
|
|
|
|
public:
|
2014-10-23 04:31:05 +08:00
|
|
|
MatchASTVisitor(const MatchFinder::MatchersByType *Matchers,
|
|
|
|
const MatchFinder::MatchFinderOptions &Options)
|
|
|
|
: Matchers(Matchers), Options(Options), ActiveASTContext(nullptr) {}
|
|
|
|
|
2015-04-11 10:00:23 +08:00
|
|
|
~MatchASTVisitor() override {
|
2014-10-23 04:31:05 +08:00
|
|
|
if (Options.CheckProfiling) {
|
|
|
|
Options.CheckProfiling->Records = std::move(TimeByBucket);
|
|
|
|
}
|
|
|
|
}
|
2012-07-06 13:48:52 +08:00
|
|
|
|
2012-11-02 09:31:03 +08:00
|
|
|
void onStartOfTranslationUnit() {
|
2014-10-23 04:31:05 +08:00
|
|
|
const bool EnableCheckProfiling = Options.CheckProfiling.hasValue();
|
2014-10-27 23:22:59 +08:00
|
|
|
TimeBucketRegion Timer;
|
2014-10-23 04:31:05 +08:00
|
|
|
for (MatchCallback *MC : Matchers->AllCallbacks) {
|
2014-10-27 23:22:59 +08:00
|
|
|
if (EnableCheckProfiling)
|
|
|
|
Timer.setBucket(&TimeByBucket[MC->getID()]);
|
2014-09-06 04:15:31 +08:00
|
|
|
MC->onStartOfTranslationUnit();
|
2014-10-23 04:31:05 +08:00
|
|
|
}
|
2012-11-02 09:31:03 +08:00
|
|
|
}
|
|
|
|
|
2013-05-29 03:21:51 +08:00
|
|
|
void onEndOfTranslationUnit() {
|
2014-10-23 04:31:05 +08:00
|
|
|
const bool EnableCheckProfiling = Options.CheckProfiling.hasValue();
|
2014-10-27 23:22:59 +08:00
|
|
|
TimeBucketRegion Timer;
|
2014-10-23 04:31:05 +08:00
|
|
|
for (MatchCallback *MC : Matchers->AllCallbacks) {
|
2014-10-27 23:22:59 +08:00
|
|
|
if (EnableCheckProfiling)
|
|
|
|
Timer.setBucket(&TimeByBucket[MC->getID()]);
|
2014-09-06 04:15:31 +08:00
|
|
|
MC->onEndOfTranslationUnit();
|
2014-10-23 04:31:05 +08:00
|
|
|
}
|
2013-05-29 03:21:51 +08:00
|
|
|
}
|
|
|
|
|
2012-07-11 04:20:19 +08:00
|
|
|
void set_active_ast_context(ASTContext *NewActiveASTContext) {
|
2012-07-06 13:48:52 +08:00
|
|
|
ActiveASTContext = NewActiveASTContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The following Visit*() and Traverse*() functions "override"
|
|
|
|
// methods in RecursiveASTVisitor.
|
|
|
|
|
2013-08-03 05:24:09 +08:00
|
|
|
bool VisitTypedefNameDecl(TypedefNameDecl *DeclNode) {
|
2012-07-06 13:48:52 +08:00
|
|
|
// When we see 'typedef A B', we add name 'B' to the set of names
|
|
|
|
// A's canonical type maps to. This is necessary for implementing
|
2012-09-07 20:48:17 +08:00
|
|
|
// isDerivedFrom(x) properly, where x can be the name of the base
|
2012-07-06 13:48:52 +08:00
|
|
|
// class or any of its aliases.
|
|
|
|
//
|
|
|
|
// In general, the is-alias-of (as defined by typedefs) relation
|
|
|
|
// is tree-shaped, as you can typedef a type more than once. For
|
|
|
|
// example,
|
|
|
|
//
|
|
|
|
// typedef A B;
|
|
|
|
// typedef A C;
|
|
|
|
// typedef C D;
|
|
|
|
// typedef C E;
|
|
|
|
//
|
|
|
|
// gives you
|
|
|
|
//
|
|
|
|
// A
|
|
|
|
// |- B
|
|
|
|
// `- C
|
|
|
|
// |- D
|
|
|
|
// `- E
|
|
|
|
//
|
|
|
|
// It is wrong to assume that the relation is a chain. A correct
|
2012-09-07 20:48:17 +08:00
|
|
|
// implementation of isDerivedFrom() needs to recognize that B and
|
2012-07-06 13:48:52 +08:00
|
|
|
// E are aliases, even though neither is a typedef of the other.
|
|
|
|
// Therefore, we cannot simply walk through one typedef chain to
|
|
|
|
// find out whether the type name matches.
|
2012-07-11 04:20:19 +08:00
|
|
|
const Type *TypeNode = DeclNode->getUnderlyingType().getTypePtr();
|
|
|
|
const Type *CanonicalType = // root of the typedef tree
|
2012-07-06 13:48:52 +08:00
|
|
|
ActiveASTContext->getCanonicalType(TypeNode);
|
2012-07-17 15:39:27 +08:00
|
|
|
TypeAliases[CanonicalType].insert(DeclNode);
|
2012-07-06 13:48:52 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-08-13 07:23:35 +08:00
|
|
|
bool VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *CAD) {
|
|
|
|
const ObjCInterfaceDecl *InterfaceDecl = CAD->getClassInterface();
|
|
|
|
CompatibleAliases[InterfaceDecl].insert(CAD);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-07-11 04:20:19 +08:00
|
|
|
bool TraverseDecl(Decl *DeclNode);
|
2018-03-03 05:55:03 +08:00
|
|
|
bool TraverseStmt(Stmt *StmtNode, DataRecursionQueue *Queue = nullptr);
|
2012-07-11 04:20:19 +08:00
|
|
|
bool TraverseType(QualType TypeNode);
|
|
|
|
bool TraverseTypeLoc(TypeLoc TypeNode);
|
2012-09-13 21:11:25 +08:00
|
|
|
bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS);
|
|
|
|
bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS);
|
2016-09-27 01:04:27 +08:00
|
|
|
bool TraverseConstructorInitializer(CXXCtorInitializer *CtorInit);
|
2012-07-06 13:48:52 +08:00
|
|
|
|
|
|
|
// Matches children or descendants of 'Node' with 'BaseMatcher'.
|
2012-09-05 20:12:07 +08:00
|
|
|
bool memoizedMatchesRecursively(const ast_type_traits::DynTypedNode &Node,
|
2019-05-13 04:09:32 +08:00
|
|
|
ASTContext &Ctx,
|
2012-09-05 20:12:07 +08:00
|
|
|
const DynTypedMatcher &Matcher,
|
2012-07-06 13:48:52 +08:00
|
|
|
BoundNodesTreeBuilder *Builder, int MaxDepth,
|
2019-05-17 01:57:38 +08:00
|
|
|
ast_type_traits::TraversalKind Traversal,
|
|
|
|
BindKind Bind) {
|
2013-07-16 21:20:30 +08:00
|
|
|
// For AST-nodes that don't have an identity, we can't memoize.
|
2014-07-23 21:17:47 +08:00
|
|
|
if (!Node.getMemoizationData() || !Builder->isComparable())
|
2013-07-16 21:20:30 +08:00
|
|
|
return matchesRecursively(Node, Matcher, Builder, MaxDepth, Traversal,
|
|
|
|
Bind);
|
|
|
|
|
Completely revamp node binding for AST matchers.
This is in preparation for the backwards references to bound
nodes, which will expose a lot more about how matches occur. Main
changes:
- instead of building the tree of bound nodes, we build a "set" of bound
nodes and explode all possible match combinations while running
through the matchers; this will allow us to also implement matchers
that filter down the current set of matches, like "equalsBoundNode"
- take the set of bound nodes at the start of the match into
consideration when doing memoization; as part of that, reevaluated
that memoization gives us benefits that are large enough (it still
does - the effect on common match patterns is up to an order of
magnitude)
- reset the bound nodes when a node does not match, thus never leaking
information from partial sub-matcher matches for failing matchers
Effects:
- we can now correctly "explode" combinatorial matches, for example:
allOf(forEachDescendant(...bind("a")),
forEachDescendant(...bind("b"))) will now trigger matches for all
combinations of matching "a" and "b"s.
- we now never expose bound nodes from partial matches in matchers that
did not match in the end - this fixes a long-standing issue
FIXMEs:
- rename BoundNodesTreeBuilder to BoundNodesBuilder or
BoundNodesSetBuilder, as we don't build a tree any more; this is out
of scope for this change, though
- we're seeing some performance regressions (around 10%), but I expect
some performance tuning will get that back, and it's easily worth
the increase in expressiveness for now
llvm-svn: 184313
2013-06-19 23:42:45 +08:00
|
|
|
MatchKey Key;
|
|
|
|
Key.MatcherID = Matcher.getID();
|
|
|
|
Key.Node = Node;
|
|
|
|
// Note that we key on the bindings *before* the match.
|
|
|
|
Key.BoundNodes = *Builder;
|
2019-05-13 04:09:32 +08:00
|
|
|
Key.Traversal = Ctx.getTraversalKind();
|
2012-10-29 18:14:44 +08:00
|
|
|
|
2013-07-16 21:20:30 +08:00
|
|
|
MemoizationMap::iterator I = ResultCache.find(Key);
|
|
|
|
if (I != ResultCache.end()) {
|
|
|
|
*Builder = I->second.Nodes;
|
|
|
|
return I->second.ResultOfMatch;
|
2012-07-06 13:48:52 +08:00
|
|
|
}
|
2013-07-16 21:20:30 +08:00
|
|
|
|
|
|
|
MemoizedMatchResult Result;
|
|
|
|
Result.Nodes = *Builder;
|
|
|
|
Result.ResultOfMatch = matchesRecursively(Node, Matcher, &Result.Nodes,
|
|
|
|
MaxDepth, Traversal, Bind);
|
2014-10-05 01:01:26 +08:00
|
|
|
|
|
|
|
MemoizedMatchResult &CachedResult = ResultCache[Key];
|
|
|
|
CachedResult = std::move(Result);
|
|
|
|
|
|
|
|
*Builder = CachedResult.Nodes;
|
|
|
|
return CachedResult.ResultOfMatch;
|
2012-07-06 13:48:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Matches children or descendants of 'Node' with 'BaseMatcher'.
|
2012-09-05 20:12:07 +08:00
|
|
|
bool matchesRecursively(const ast_type_traits::DynTypedNode &Node,
|
|
|
|
const DynTypedMatcher &Matcher,
|
2012-07-06 13:48:52 +08:00
|
|
|
BoundNodesTreeBuilder *Builder, int MaxDepth,
|
2019-05-17 01:57:38 +08:00
|
|
|
ast_type_traits::TraversalKind Traversal,
|
|
|
|
BindKind Bind) {
|
2012-07-06 13:48:52 +08:00
|
|
|
MatchChildASTVisitor Visitor(
|
2012-09-05 20:12:07 +08:00
|
|
|
&Matcher, this, Builder, MaxDepth, Traversal, Bind);
|
2012-07-06 13:48:52 +08:00
|
|
|
return Visitor.findMatch(Node);
|
|
|
|
}
|
|
|
|
|
2014-03-13 16:12:15 +08:00
|
|
|
bool classIsDerivedFrom(const CXXRecordDecl *Declaration,
|
|
|
|
const Matcher<NamedDecl> &Base,
|
2019-07-25 19:54:13 +08:00
|
|
|
BoundNodesTreeBuilder *Builder,
|
|
|
|
bool Directly) override;
|
2012-07-06 13:48:52 +08:00
|
|
|
|
2019-08-13 07:23:35 +08:00
|
|
|
bool objcClassIsDerivedFrom(const ObjCInterfaceDecl *Declaration,
|
|
|
|
const Matcher<NamedDecl> &Base,
|
|
|
|
BoundNodesTreeBuilder *Builder,
|
|
|
|
bool Directly) override;
|
|
|
|
|
2012-10-23 00:26:51 +08:00
|
|
|
// Implements ASTMatchFinder::matchesChildOf.
|
2014-03-13 16:12:15 +08:00
|
|
|
bool matchesChildOf(const ast_type_traits::DynTypedNode &Node,
|
2019-05-13 04:09:32 +08:00
|
|
|
ASTContext &Ctx, const DynTypedMatcher &Matcher,
|
2014-03-13 16:12:15 +08:00
|
|
|
BoundNodesTreeBuilder *Builder,
|
2019-05-17 01:57:38 +08:00
|
|
|
ast_type_traits::TraversalKind Traversal,
|
2014-03-13 16:12:15 +08:00
|
|
|
BindKind Bind) override {
|
2013-07-25 17:32:14 +08:00
|
|
|
if (ResultCache.size() > MaxMemoizationEntries)
|
|
|
|
ResultCache.clear();
|
2019-05-13 04:09:32 +08:00
|
|
|
return memoizedMatchesRecursively(Node, Ctx, Matcher, Builder, 1, Traversal,
|
2013-07-25 17:32:14 +08:00
|
|
|
Bind);
|
2012-07-06 13:48:52 +08:00
|
|
|
}
|
2012-10-23 00:26:51 +08:00
|
|
|
// Implements ASTMatchFinder::matchesDescendantOf.
|
2014-03-13 16:12:15 +08:00
|
|
|
bool matchesDescendantOf(const ast_type_traits::DynTypedNode &Node,
|
2019-05-13 04:09:32 +08:00
|
|
|
ASTContext &Ctx, const DynTypedMatcher &Matcher,
|
2014-03-13 16:12:15 +08:00
|
|
|
BoundNodesTreeBuilder *Builder,
|
|
|
|
BindKind Bind) override {
|
2013-07-08 22:16:30 +08:00
|
|
|
if (ResultCache.size() > MaxMemoizationEntries)
|
|
|
|
ResultCache.clear();
|
2019-05-13 04:09:32 +08:00
|
|
|
return memoizedMatchesRecursively(Node, Ctx, Matcher, Builder, INT_MAX,
|
2019-05-17 01:57:38 +08:00
|
|
|
ast_type_traits::TraversalKind::TK_AsIs,
|
|
|
|
Bind);
|
2012-07-06 13:48:52 +08:00
|
|
|
}
|
2012-09-07 17:26:10 +08:00
|
|
|
// Implements ASTMatchFinder::matchesAncestorOf.
|
2014-03-13 16:12:15 +08:00
|
|
|
bool matchesAncestorOf(const ast_type_traits::DynTypedNode &Node,
|
2019-05-13 04:09:32 +08:00
|
|
|
ASTContext &Ctx, const DynTypedMatcher &Matcher,
|
2014-03-13 16:12:15 +08:00
|
|
|
BoundNodesTreeBuilder *Builder,
|
|
|
|
AncestorMatchMode MatchMode) override {
|
2013-07-08 22:16:30 +08:00
|
|
|
// Reset the cache outside of the recursive call to make sure we
|
|
|
|
// don't invalidate any iterators.
|
|
|
|
if (ResultCache.size() > MaxMemoizationEntries)
|
|
|
|
ResultCache.clear();
|
2019-05-13 04:09:32 +08:00
|
|
|
return memoizedMatchesAncestorOfRecursively(Node, Ctx, Matcher, Builder,
|
2013-03-15 00:33:21 +08:00
|
|
|
MatchMode);
|
2012-09-07 17:26:10 +08:00
|
|
|
}
|
2012-07-06 13:48:52 +08:00
|
|
|
|
2013-02-01 21:41:35 +08:00
|
|
|
// Matches all registered matchers on the given node and calls the
|
|
|
|
// result callback for every node that matches.
|
2014-09-06 04:15:31 +08:00
|
|
|
void match(const ast_type_traits::DynTypedNode &Node) {
|
|
|
|
// FIXME: Improve this with a switch or a visitor pattern.
|
|
|
|
if (auto *N = Node.get<Decl>()) {
|
|
|
|
match(*N);
|
|
|
|
} else if (auto *N = Node.get<Stmt>()) {
|
|
|
|
match(*N);
|
|
|
|
} else if (auto *N = Node.get<Type>()) {
|
|
|
|
match(*N);
|
|
|
|
} else if (auto *N = Node.get<QualType>()) {
|
|
|
|
match(*N);
|
|
|
|
} else if (auto *N = Node.get<NestedNameSpecifier>()) {
|
|
|
|
match(*N);
|
|
|
|
} else if (auto *N = Node.get<NestedNameSpecifierLoc>()) {
|
|
|
|
match(*N);
|
|
|
|
} else if (auto *N = Node.get<TypeLoc>()) {
|
|
|
|
match(*N);
|
2016-09-27 01:04:27 +08:00
|
|
|
} else if (auto *N = Node.get<CXXCtorInitializer>()) {
|
|
|
|
match(*N);
|
2013-02-01 21:41:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T> void match(const T &Node) {
|
2014-09-06 04:15:31 +08:00
|
|
|
matchDispatch(&Node);
|
2013-02-01 21:41:35 +08:00
|
|
|
}
|
|
|
|
|
2012-11-30 21:45:19 +08:00
|
|
|
// Implements ASTMatchFinder::getASTContext.
|
2014-03-13 16:12:15 +08:00
|
|
|
ASTContext &getASTContext() const override { return *ActiveASTContext; }
|
2012-11-30 21:45:19 +08:00
|
|
|
|
2012-07-06 13:48:52 +08:00
|
|
|
bool shouldVisitTemplateInstantiations() const { return true; }
|
|
|
|
bool shouldVisitImplicitCode() const { return true; }
|
|
|
|
|
|
|
|
private:
|
2014-10-27 23:22:59 +08:00
|
|
|
class TimeBucketRegion {
|
2014-10-23 04:31:05 +08:00
|
|
|
public:
|
2014-10-27 23:22:59 +08:00
|
|
|
TimeBucketRegion() : Bucket(nullptr) {}
|
|
|
|
~TimeBucketRegion() { setBucket(nullptr); }
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Start timing for \p NewBucket.
|
2014-10-27 23:22:59 +08:00
|
|
|
///
|
|
|
|
/// If there was a bucket already set, it will finish the timing for that
|
|
|
|
/// other bucket.
|
|
|
|
/// \p NewBucket will be timed until the next call to \c setBucket() or
|
|
|
|
/// until the \c TimeBucketRegion is destroyed.
|
|
|
|
/// If \p NewBucket is the same as the currently timed bucket, this call
|
|
|
|
/// does nothing.
|
|
|
|
void setBucket(llvm::TimeRecord *NewBucket) {
|
|
|
|
if (Bucket != NewBucket) {
|
|
|
|
auto Now = llvm::TimeRecord::getCurrentTime(true);
|
|
|
|
if (Bucket)
|
|
|
|
*Bucket += Now;
|
|
|
|
if (NewBucket)
|
|
|
|
*NewBucket -= Now;
|
|
|
|
Bucket = NewBucket;
|
|
|
|
}
|
2014-10-23 04:31:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-10-27 23:22:59 +08:00
|
|
|
llvm::TimeRecord *Bucket;
|
2014-10-23 04:31:05 +08:00
|
|
|
};
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Runs all the \p Matchers on \p Node.
|
2014-09-06 04:15:31 +08:00
|
|
|
///
|
|
|
|
/// Used by \c matchDispatch() below.
|
|
|
|
template <typename T, typename MC>
|
2014-11-25 05:21:09 +08:00
|
|
|
void matchWithoutFilter(const T &Node, const MC &Matchers) {
|
2014-10-23 04:31:05 +08:00
|
|
|
const bool EnableCheckProfiling = Options.CheckProfiling.hasValue();
|
2014-10-27 23:22:59 +08:00
|
|
|
TimeBucketRegion Timer;
|
2014-09-06 04:15:31 +08:00
|
|
|
for (const auto &MP : Matchers) {
|
2014-10-27 23:22:59 +08:00
|
|
|
if (EnableCheckProfiling)
|
|
|
|
Timer.setBucket(&TimeByBucket[MP.second->getID()]);
|
2014-09-06 04:15:31 +08:00
|
|
|
BoundNodesTreeBuilder Builder;
|
|
|
|
if (MP.first.matches(Node, this, &Builder)) {
|
|
|
|
MatchVisitor Visitor(ActiveASTContext, MP.second);
|
|
|
|
Builder.visitMatches(&Visitor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-25 05:21:09 +08:00
|
|
|
void matchWithFilter(const ast_type_traits::DynTypedNode &DynNode) {
|
|
|
|
auto Kind = DynNode.getNodeKind();
|
|
|
|
auto it = MatcherFiltersMap.find(Kind);
|
|
|
|
const auto &Filter =
|
|
|
|
it != MatcherFiltersMap.end() ? it->second : getFilterForKind(Kind);
|
|
|
|
|
|
|
|
if (Filter.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
const bool EnableCheckProfiling = Options.CheckProfiling.hasValue();
|
|
|
|
TimeBucketRegion Timer;
|
|
|
|
auto &Matchers = this->Matchers->DeclOrStmt;
|
|
|
|
for (unsigned short I : Filter) {
|
|
|
|
auto &MP = Matchers[I];
|
|
|
|
if (EnableCheckProfiling)
|
|
|
|
Timer.setBucket(&TimeByBucket[MP.second->getID()]);
|
|
|
|
BoundNodesTreeBuilder Builder;
|
2019-05-13 04:09:32 +08:00
|
|
|
if (MP.first.matches(DynNode, this, &Builder)) {
|
2014-11-25 05:21:09 +08:00
|
|
|
MatchVisitor Visitor(ActiveASTContext, MP.second);
|
|
|
|
Builder.visitMatches(&Visitor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::vector<unsigned short> &
|
|
|
|
getFilterForKind(ast_type_traits::ASTNodeKind Kind) {
|
|
|
|
auto &Filter = MatcherFiltersMap[Kind];
|
|
|
|
auto &Matchers = this->Matchers->DeclOrStmt;
|
|
|
|
assert((Matchers.size() < USHRT_MAX) && "Too many matchers.");
|
|
|
|
for (unsigned I = 0, E = Matchers.size(); I != E; ++I) {
|
|
|
|
if (Matchers[I].first.canMatchNodesOfKind(Kind)) {
|
|
|
|
Filter.push_back(I);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Filter;
|
|
|
|
}
|
|
|
|
|
2014-09-06 04:15:31 +08:00
|
|
|
/// @{
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Overloads to pair the different node types to their matchers.
|
2014-11-25 05:21:09 +08:00
|
|
|
void matchDispatch(const Decl *Node) {
|
|
|
|
return matchWithFilter(ast_type_traits::DynTypedNode::create(*Node));
|
|
|
|
}
|
|
|
|
void matchDispatch(const Stmt *Node) {
|
|
|
|
return matchWithFilter(ast_type_traits::DynTypedNode::create(*Node));
|
|
|
|
}
|
|
|
|
|
2014-09-06 04:15:31 +08:00
|
|
|
void matchDispatch(const Type *Node) {
|
2014-11-25 05:21:09 +08:00
|
|
|
matchWithoutFilter(QualType(Node, 0), Matchers->Type);
|
2014-09-06 04:15:31 +08:00
|
|
|
}
|
|
|
|
void matchDispatch(const TypeLoc *Node) {
|
2014-11-25 05:21:09 +08:00
|
|
|
matchWithoutFilter(*Node, Matchers->TypeLoc);
|
|
|
|
}
|
|
|
|
void matchDispatch(const QualType *Node) {
|
|
|
|
matchWithoutFilter(*Node, Matchers->Type);
|
2014-09-06 04:15:31 +08:00
|
|
|
}
|
|
|
|
void matchDispatch(const NestedNameSpecifier *Node) {
|
2014-11-25 05:21:09 +08:00
|
|
|
matchWithoutFilter(*Node, Matchers->NestedNameSpecifier);
|
2014-09-06 04:15:31 +08:00
|
|
|
}
|
|
|
|
void matchDispatch(const NestedNameSpecifierLoc *Node) {
|
2014-11-25 05:21:09 +08:00
|
|
|
matchWithoutFilter(*Node, Matchers->NestedNameSpecifierLoc);
|
2014-09-06 04:15:31 +08:00
|
|
|
}
|
2016-09-27 01:04:27 +08:00
|
|
|
void matchDispatch(const CXXCtorInitializer *Node) {
|
|
|
|
matchWithoutFilter(*Node, Matchers->CtorInit);
|
|
|
|
}
|
2014-09-06 04:15:31 +08:00
|
|
|
void matchDispatch(const void *) { /* Do nothing. */ }
|
|
|
|
/// @}
|
|
|
|
|
2013-03-15 00:33:21 +08:00
|
|
|
// Returns whether an ancestor of \p Node matches \p Matcher.
|
|
|
|
//
|
|
|
|
// The order of matching ((which can lead to different nodes being bound in
|
|
|
|
// case there are multiple matches) is breadth first search.
|
|
|
|
//
|
|
|
|
// To allow memoization in the very common case of having deeply nested
|
|
|
|
// expressions inside a template function, we first walk up the AST, memoizing
|
|
|
|
// the result of the match along the way, as long as there is only a single
|
|
|
|
// parent.
|
|
|
|
//
|
|
|
|
// Once there are multiple parents, the breadth first search order does not
|
|
|
|
// allow simple memoization on the ancestors. Thus, we only memoize as long
|
|
|
|
// as there is a single parent.
|
|
|
|
bool memoizedMatchesAncestorOfRecursively(
|
2019-05-13 04:09:32 +08:00
|
|
|
const ast_type_traits::DynTypedNode &Node, ASTContext &Ctx,
|
|
|
|
const DynTypedMatcher &Matcher, BoundNodesTreeBuilder *Builder,
|
|
|
|
AncestorMatchMode MatchMode) {
|
2016-04-19 23:52:56 +08:00
|
|
|
// For AST-nodes that don't have an identity, we can't memoize.
|
|
|
|
if (!Builder->isComparable())
|
2019-05-13 04:09:32 +08:00
|
|
|
return matchesAncestorOfRecursively(Node, Ctx, Matcher, Builder,
|
|
|
|
MatchMode);
|
2016-04-19 23:52:56 +08:00
|
|
|
|
Completely revamp node binding for AST matchers.
This is in preparation for the backwards references to bound
nodes, which will expose a lot more about how matches occur. Main
changes:
- instead of building the tree of bound nodes, we build a "set" of bound
nodes and explode all possible match combinations while running
through the matchers; this will allow us to also implement matchers
that filter down the current set of matches, like "equalsBoundNode"
- take the set of bound nodes at the start of the match into
consideration when doing memoization; as part of that, reevaluated
that memoization gives us benefits that are large enough (it still
does - the effect on common match patterns is up to an order of
magnitude)
- reset the bound nodes when a node does not match, thus never leaking
information from partial sub-matcher matches for failing matchers
Effects:
- we can now correctly "explode" combinatorial matches, for example:
allOf(forEachDescendant(...bind("a")),
forEachDescendant(...bind("b"))) will now trigger matches for all
combinations of matching "a" and "b"s.
- we now never expose bound nodes from partial matches in matchers that
did not match in the end - this fixes a long-standing issue
FIXMEs:
- rename BoundNodesTreeBuilder to BoundNodesBuilder or
BoundNodesSetBuilder, as we don't build a tree any more; this is out
of scope for this change, though
- we're seeing some performance regressions (around 10%), but I expect
some performance tuning will get that back, and it's easily worth
the increase in expressiveness for now
llvm-svn: 184313
2013-06-19 23:42:45 +08:00
|
|
|
MatchKey Key;
|
|
|
|
Key.MatcherID = Matcher.getID();
|
|
|
|
Key.Node = Node;
|
|
|
|
Key.BoundNodes = *Builder;
|
2019-05-13 04:09:32 +08:00
|
|
|
Key.Traversal = Ctx.getTraversalKind();
|
2013-07-16 21:20:30 +08:00
|
|
|
|
|
|
|
// Note that we cannot use insert and reuse the iterator, as recursive
|
|
|
|
// calls to match might invalidate the result cache iterators.
|
|
|
|
MemoizationMap::iterator I = ResultCache.find(Key);
|
|
|
|
if (I != ResultCache.end()) {
|
|
|
|
*Builder = I->second.Nodes;
|
|
|
|
return I->second.ResultOfMatch;
|
|
|
|
}
|
2014-10-05 01:01:26 +08:00
|
|
|
|
2013-07-16 21:20:30 +08:00
|
|
|
MemoizedMatchResult Result;
|
|
|
|
Result.Nodes = *Builder;
|
2019-05-13 04:09:32 +08:00
|
|
|
Result.ResultOfMatch = matchesAncestorOfRecursively(
|
|
|
|
Node, Ctx, Matcher, &Result.Nodes, MatchMode);
|
2016-04-19 23:52:56 +08:00
|
|
|
|
|
|
|
MemoizedMatchResult &CachedResult = ResultCache[Key];
|
|
|
|
CachedResult = std::move(Result);
|
|
|
|
|
|
|
|
*Builder = CachedResult.Nodes;
|
|
|
|
return CachedResult.ResultOfMatch;
|
|
|
|
}
|
2014-10-05 01:01:26 +08:00
|
|
|
|
2016-04-19 23:52:56 +08:00
|
|
|
bool matchesAncestorOfRecursively(const ast_type_traits::DynTypedNode &Node,
|
2019-05-13 04:09:32 +08:00
|
|
|
ASTContext &Ctx,
|
2016-04-19 23:52:56 +08:00
|
|
|
const DynTypedMatcher &Matcher,
|
|
|
|
BoundNodesTreeBuilder *Builder,
|
|
|
|
AncestorMatchMode MatchMode) {
|
2014-10-05 01:01:26 +08:00
|
|
|
const auto &Parents = ActiveASTContext->getParents(Node);
|
[AST] Allow limiting the scope of common AST traversals (getParents, RAV).
Summary:
The goal is to allow analyses such as clang-tidy checks to run on a
subset of the AST, e.g. "only on main-file decls" for interactive tools.
Today, these become "problematically global" by running RecursiveASTVisitors
rooted at the TUDecl, or by navigating up via ASTContext::getParent().
The scope is restricted using a set of top-level-decls that RecursiveASTVisitors
should be rooted at. This also applies to the visitor that populates the
parent map, and so the top-level-decls are considered to have no parents.
This patch makes the traversal scope a mutable property of ASTContext.
The more obvious way to do this is to pass the top-level decls to
relevant functions directly, but this has some problems:
- it's error-prone: accidentally mixing restricted and unrestricted
scopes is a performance trap. Interleaving multiple analyses is
common (many clang-tidy checks run matchers or RAVs from matcher callbacks)
- it doesn't map well to the actual use cases, where we really do want
*all* traversals to be restricted.
- it involves a lot of plumbing in parts of the code that don't care
about traversals.
This approach was tried out in D54259 and D54261, I wanted to like it
but it feels pretty awful in practice.
Caveats: to get scope-limiting behavior of RecursiveASTVisitors, callers
have to call the new TraverseAST(Ctx) function instead of TraverseDecl(TU).
I think this is an improvement to the API regardless.
Reviewers: klimek, ioeric
Subscribers: mgorny, cfe-commits
Differential Revision: https://reviews.llvm.org/D54309
llvm-svn: 346847
2018-11-14 18:33:30 +08:00
|
|
|
if (Parents.empty()) {
|
|
|
|
// Nodes may have no parents if:
|
|
|
|
// a) the node is the TranslationUnitDecl
|
|
|
|
// b) we have a limited traversal scope that excludes the parent edges
|
|
|
|
// c) there is a bug in the AST, and the node is not reachable
|
|
|
|
// Usually the traversal scope is the whole AST, which precludes b.
|
|
|
|
// Bugs are common enough that it's worthwhile asserting when we can.
|
2019-01-08 15:29:46 +08:00
|
|
|
#ifndef NDEBUG
|
|
|
|
if (!Node.get<TranslationUnitDecl>() &&
|
|
|
|
/* Traversal scope is full AST if any of the bounds are the TU */
|
|
|
|
llvm::any_of(ActiveASTContext->getTraversalScope(), [](Decl *D) {
|
|
|
|
return D->getKind() == Decl::TranslationUnit;
|
|
|
|
})) {
|
|
|
|
llvm::errs() << "Tried to match orphan node:\n";
|
|
|
|
Node.dump(llvm::errs(), ActiveASTContext->getSourceManager());
|
|
|
|
llvm_unreachable("Parent map should be complete!");
|
|
|
|
}
|
|
|
|
#endif
|
[AST] Allow limiting the scope of common AST traversals (getParents, RAV).
Summary:
The goal is to allow analyses such as clang-tidy checks to run on a
subset of the AST, e.g. "only on main-file decls" for interactive tools.
Today, these become "problematically global" by running RecursiveASTVisitors
rooted at the TUDecl, or by navigating up via ASTContext::getParent().
The scope is restricted using a set of top-level-decls that RecursiveASTVisitors
should be rooted at. This also applies to the visitor that populates the
parent map, and so the top-level-decls are considered to have no parents.
This patch makes the traversal scope a mutable property of ASTContext.
The more obvious way to do this is to pass the top-level decls to
relevant functions directly, but this has some problems:
- it's error-prone: accidentally mixing restricted and unrestricted
scopes is a performance trap. Interleaving multiple analyses is
common (many clang-tidy checks run matchers or RAVs from matcher callbacks)
- it doesn't map well to the actual use cases, where we really do want
*all* traversals to be restricted.
- it involves a lot of plumbing in parts of the code that don't care
about traversals.
This approach was tried out in D54259 and D54261, I wanted to like it
but it feels pretty awful in practice.
Caveats: to get scope-limiting behavior of RecursiveASTVisitors, callers
have to call the new TraverseAST(Ctx) function instead of TraverseDecl(TU).
I think this is an improvement to the API regardless.
Reviewers: klimek, ioeric
Subscribers: mgorny, cfe-commits
Differential Revision: https://reviews.llvm.org/D54309
llvm-svn: 346847
2018-11-14 18:33:30 +08:00
|
|
|
return false;
|
|
|
|
}
|
2013-07-16 21:20:30 +08:00
|
|
|
if (Parents.size() == 1) {
|
|
|
|
// Only one parent - do recursive memoization.
|
|
|
|
const ast_type_traits::DynTypedNode Parent = Parents[0];
|
2016-04-19 23:52:56 +08:00
|
|
|
BoundNodesTreeBuilder BuilderCopy = *Builder;
|
|
|
|
if (Matcher.matches(Parent, this, &BuilderCopy)) {
|
|
|
|
*Builder = std::move(BuilderCopy);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (MatchMode != ASTMatchFinder::AMM_ParentOnly) {
|
2019-05-13 04:09:32 +08:00
|
|
|
return memoizedMatchesAncestorOfRecursively(Parent, Ctx, Matcher,
|
|
|
|
Builder, MatchMode);
|
2013-07-16 21:20:30 +08:00
|
|
|
// Once we get back from the recursive call, the result will be the
|
|
|
|
// same as the parent's result.
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Multiple parents - BFS over the rest of the nodes.
|
|
|
|
llvm::DenseSet<const void *> Visited;
|
|
|
|
std::deque<ast_type_traits::DynTypedNode> Queue(Parents.begin(),
|
|
|
|
Parents.end());
|
|
|
|
while (!Queue.empty()) {
|
2016-04-19 23:52:56 +08:00
|
|
|
BoundNodesTreeBuilder BuilderCopy = *Builder;
|
|
|
|
if (Matcher.matches(Queue.front(), this, &BuilderCopy)) {
|
|
|
|
*Builder = std::move(BuilderCopy);
|
|
|
|
return true;
|
2013-03-15 00:33:21 +08:00
|
|
|
}
|
2013-07-16 21:20:30 +08:00
|
|
|
if (MatchMode != ASTMatchFinder::AMM_ParentOnly) {
|
2014-10-05 01:01:26 +08:00
|
|
|
for (const auto &Parent :
|
|
|
|
ActiveASTContext->getParents(Queue.front())) {
|
2013-07-16 21:20:30 +08:00
|
|
|
// Make sure we do not visit the same node twice.
|
|
|
|
// Otherwise, we'll visit the common ancestors as often as there
|
|
|
|
// are splits on the way down.
|
2014-10-05 01:01:26 +08:00
|
|
|
if (Visited.insert(Parent.getMemoizationData()).second)
|
|
|
|
Queue.push_back(Parent);
|
2013-03-15 00:33:21 +08:00
|
|
|
}
|
|
|
|
}
|
2013-07-16 21:20:30 +08:00
|
|
|
Queue.pop_front();
|
2013-03-15 00:33:21 +08:00
|
|
|
}
|
2012-12-06 22:42:48 +08:00
|
|
|
}
|
2016-04-19 23:52:56 +08:00
|
|
|
return false;
|
2012-12-06 22:42:48 +08:00
|
|
|
}
|
|
|
|
|
2012-07-06 13:48:52 +08:00
|
|
|
// Implements a BoundNodesTree::Visitor that calls a MatchCallback with
|
|
|
|
// the aggregated bound nodes for each match.
|
Completely revamp node binding for AST matchers.
This is in preparation for the backwards references to bound
nodes, which will expose a lot more about how matches occur. Main
changes:
- instead of building the tree of bound nodes, we build a "set" of bound
nodes and explode all possible match combinations while running
through the matchers; this will allow us to also implement matchers
that filter down the current set of matches, like "equalsBoundNode"
- take the set of bound nodes at the start of the match into
consideration when doing memoization; as part of that, reevaluated
that memoization gives us benefits that are large enough (it still
does - the effect on common match patterns is up to an order of
magnitude)
- reset the bound nodes when a node does not match, thus never leaking
information from partial sub-matcher matches for failing matchers
Effects:
- we can now correctly "explode" combinatorial matches, for example:
allOf(forEachDescendant(...bind("a")),
forEachDescendant(...bind("b"))) will now trigger matches for all
combinations of matching "a" and "b"s.
- we now never expose bound nodes from partial matches in matchers that
did not match in the end - this fixes a long-standing issue
FIXMEs:
- rename BoundNodesTreeBuilder to BoundNodesBuilder or
BoundNodesSetBuilder, as we don't build a tree any more; this is out
of scope for this change, though
- we're seeing some performance regressions (around 10%), but I expect
some performance tuning will get that back, and it's easily worth
the increase in expressiveness for now
llvm-svn: 184313
2013-06-19 23:42:45 +08:00
|
|
|
class MatchVisitor : public BoundNodesTreeBuilder::Visitor {
|
2012-07-06 13:48:52 +08:00
|
|
|
public:
|
2012-07-11 04:20:19 +08:00
|
|
|
MatchVisitor(ASTContext* Context,
|
2012-07-06 13:48:52 +08:00
|
|
|
MatchFinder::MatchCallback* Callback)
|
|
|
|
: Context(Context),
|
|
|
|
Callback(Callback) {}
|
|
|
|
|
2014-03-13 16:12:15 +08:00
|
|
|
void visitMatch(const BoundNodes& BoundNodesView) override {
|
2012-07-06 13:48:52 +08:00
|
|
|
Callback->run(MatchFinder::MatchResult(BoundNodesView, Context));
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2012-07-11 04:20:19 +08:00
|
|
|
ASTContext* Context;
|
2012-07-06 13:48:52 +08:00
|
|
|
MatchFinder::MatchCallback* Callback;
|
|
|
|
};
|
|
|
|
|
2012-07-17 15:39:27 +08:00
|
|
|
// Returns true if 'TypeNode' has an alias that matches the given matcher.
|
|
|
|
bool typeHasMatchingAlias(const Type *TypeNode,
|
2016-06-15 22:20:56 +08:00
|
|
|
const Matcher<NamedDecl> &Matcher,
|
2012-07-17 15:39:27 +08:00
|
|
|
BoundNodesTreeBuilder *Builder) {
|
2012-07-11 04:20:19 +08:00
|
|
|
const Type *const CanonicalType =
|
2012-07-06 13:48:52 +08:00
|
|
|
ActiveASTContext->getCanonicalType(TypeNode);
|
2017-10-10 15:21:34 +08:00
|
|
|
auto Aliases = TypeAliases.find(CanonicalType);
|
|
|
|
if (Aliases == TypeAliases.end())
|
|
|
|
return false;
|
|
|
|
for (const TypedefNameDecl *Alias : Aliases->second) {
|
Completely revamp node binding for AST matchers.
This is in preparation for the backwards references to bound
nodes, which will expose a lot more about how matches occur. Main
changes:
- instead of building the tree of bound nodes, we build a "set" of bound
nodes and explode all possible match combinations while running
through the matchers; this will allow us to also implement matchers
that filter down the current set of matches, like "equalsBoundNode"
- take the set of bound nodes at the start of the match into
consideration when doing memoization; as part of that, reevaluated
that memoization gives us benefits that are large enough (it still
does - the effect on common match patterns is up to an order of
magnitude)
- reset the bound nodes when a node does not match, thus never leaking
information from partial sub-matcher matches for failing matchers
Effects:
- we can now correctly "explode" combinatorial matches, for example:
allOf(forEachDescendant(...bind("a")),
forEachDescendant(...bind("b"))) will now trigger matches for all
combinations of matching "a" and "b"s.
- we now never expose bound nodes from partial matches in matchers that
did not match in the end - this fixes a long-standing issue
FIXMEs:
- rename BoundNodesTreeBuilder to BoundNodesBuilder or
BoundNodesSetBuilder, as we don't build a tree any more; this is out
of scope for this change, though
- we're seeing some performance regressions (around 10%), but I expect
some performance tuning will get that back, and it's easily worth
the increase in expressiveness for now
llvm-svn: 184313
2013-06-19 23:42:45 +08:00
|
|
|
BoundNodesTreeBuilder Result(*Builder);
|
2014-10-05 01:01:26 +08:00
|
|
|
if (Matcher.matches(*Alias, this, &Result)) {
|
|
|
|
*Builder = std::move(Result);
|
2012-07-17 15:39:27 +08:00
|
|
|
return true;
|
Completely revamp node binding for AST matchers.
This is in preparation for the backwards references to bound
nodes, which will expose a lot more about how matches occur. Main
changes:
- instead of building the tree of bound nodes, we build a "set" of bound
nodes and explode all possible match combinations while running
through the matchers; this will allow us to also implement matchers
that filter down the current set of matches, like "equalsBoundNode"
- take the set of bound nodes at the start of the match into
consideration when doing memoization; as part of that, reevaluated
that memoization gives us benefits that are large enough (it still
does - the effect on common match patterns is up to an order of
magnitude)
- reset the bound nodes when a node does not match, thus never leaking
information from partial sub-matcher matches for failing matchers
Effects:
- we can now correctly "explode" combinatorial matches, for example:
allOf(forEachDescendant(...bind("a")),
forEachDescendant(...bind("b"))) will now trigger matches for all
combinations of matching "a" and "b"s.
- we now never expose bound nodes from partial matches in matchers that
did not match in the end - this fixes a long-standing issue
FIXMEs:
- rename BoundNodesTreeBuilder to BoundNodesBuilder or
BoundNodesSetBuilder, as we don't build a tree any more; this is out
of scope for this change, though
- we're seeing some performance regressions (around 10%), but I expect
some performance tuning will get that back, and it's easily worth
the increase in expressiveness for now
llvm-svn: 184313
2013-06-19 23:42:45 +08:00
|
|
|
}
|
2012-07-17 15:39:27 +08:00
|
|
|
}
|
|
|
|
return false;
|
2012-07-06 13:48:52 +08:00
|
|
|
}
|
|
|
|
|
2019-08-13 07:23:35 +08:00
|
|
|
bool
|
|
|
|
objcClassHasMatchingCompatibilityAlias(const ObjCInterfaceDecl *InterfaceDecl,
|
|
|
|
const Matcher<NamedDecl> &Matcher,
|
|
|
|
BoundNodesTreeBuilder *Builder) {
|
|
|
|
auto Aliases = CompatibleAliases.find(InterfaceDecl);
|
|
|
|
if (Aliases == CompatibleAliases.end())
|
|
|
|
return false;
|
|
|
|
for (const ObjCCompatibleAliasDecl *Alias : Aliases->second) {
|
|
|
|
BoundNodesTreeBuilder Result(*Builder);
|
|
|
|
if (Matcher.matches(*Alias, this, &Result)) {
|
|
|
|
*Builder = std::move(Result);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Bucket to record map.
|
2014-10-23 04:31:05 +08:00
|
|
|
///
|
|
|
|
/// Used to get the appropriate bucket for each matcher.
|
|
|
|
llvm::StringMap<llvm::TimeRecord> TimeByBucket;
|
|
|
|
|
2014-09-06 04:15:31 +08:00
|
|
|
const MatchFinder::MatchersByType *Matchers;
|
2014-11-25 05:21:09 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Filtered list of matcher indices for each matcher kind.
|
2014-11-25 05:21:09 +08:00
|
|
|
///
|
|
|
|
/// \c Decl and \c Stmt toplevel matchers usually apply to a specific node
|
|
|
|
/// kind (and derived kinds) so it is a waste to try every matcher on every
|
|
|
|
/// node.
|
|
|
|
/// We precalculate a list of matchers that pass the toplevel restrict check.
|
|
|
|
llvm::DenseMap<ast_type_traits::ASTNodeKind, std::vector<unsigned short>>
|
|
|
|
MatcherFiltersMap;
|
|
|
|
|
2014-10-23 04:31:05 +08:00
|
|
|
const MatchFinder::MatchFinderOptions &Options;
|
2012-07-11 04:20:19 +08:00
|
|
|
ASTContext *ActiveASTContext;
|
2012-07-06 13:48:52 +08:00
|
|
|
|
2012-07-17 15:39:27 +08:00
|
|
|
// Maps a canonical type to its TypedefDecls.
|
2013-08-03 05:24:09 +08:00
|
|
|
llvm::DenseMap<const Type*, std::set<const TypedefNameDecl*> > TypeAliases;
|
2012-07-06 13:48:52 +08:00
|
|
|
|
2019-08-13 07:23:35 +08:00
|
|
|
// Maps an Objective-C interface to its ObjCCompatibleAliasDecls.
|
|
|
|
llvm::DenseMap<const ObjCInterfaceDecl *,
|
|
|
|
llvm::SmallPtrSet<const ObjCCompatibleAliasDecl *, 2>>
|
|
|
|
CompatibleAliases;
|
|
|
|
|
2012-07-06 13:48:52 +08:00
|
|
|
// Maps (matcher, node) -> the match result for memoization.
|
Completely revamp node binding for AST matchers.
This is in preparation for the backwards references to bound
nodes, which will expose a lot more about how matches occur. Main
changes:
- instead of building the tree of bound nodes, we build a "set" of bound
nodes and explode all possible match combinations while running
through the matchers; this will allow us to also implement matchers
that filter down the current set of matches, like "equalsBoundNode"
- take the set of bound nodes at the start of the match into
consideration when doing memoization; as part of that, reevaluated
that memoization gives us benefits that are large enough (it still
does - the effect on common match patterns is up to an order of
magnitude)
- reset the bound nodes when a node does not match, thus never leaking
information from partial sub-matcher matches for failing matchers
Effects:
- we can now correctly "explode" combinatorial matches, for example:
allOf(forEachDescendant(...bind("a")),
forEachDescendant(...bind("b"))) will now trigger matches for all
combinations of matching "a" and "b"s.
- we now never expose bound nodes from partial matches in matchers that
did not match in the end - this fixes a long-standing issue
FIXMEs:
- rename BoundNodesTreeBuilder to BoundNodesBuilder or
BoundNodesSetBuilder, as we don't build a tree any more; this is out
of scope for this change, though
- we're seeing some performance regressions (around 10%), but I expect
some performance tuning will get that back, and it's easily worth
the increase in expressiveness for now
llvm-svn: 184313
2013-06-19 23:42:45 +08:00
|
|
|
typedef std::map<MatchKey, MemoizedMatchResult> MemoizationMap;
|
2012-07-06 13:48:52 +08:00
|
|
|
MemoizationMap ResultCache;
|
|
|
|
};
|
|
|
|
|
2016-02-10 04:59:05 +08:00
|
|
|
static CXXRecordDecl *
|
|
|
|
getAsCXXRecordDeclOrPrimaryTemplate(const Type *TypeNode) {
|
|
|
|
if (auto *RD = TypeNode->getAsCXXRecordDecl())
|
|
|
|
return RD;
|
|
|
|
|
|
|
|
// Find the innermost TemplateSpecializationType that isn't an alias template.
|
|
|
|
auto *TemplateType = TypeNode->getAs<TemplateSpecializationType>();
|
|
|
|
while (TemplateType && TemplateType->isTypeAlias())
|
|
|
|
TemplateType =
|
|
|
|
TemplateType->getAliasedType()->getAs<TemplateSpecializationType>();
|
|
|
|
|
|
|
|
// If this is the name of a (dependent) template specialization, use the
|
|
|
|
// definition of the template, even though it might be specialized later.
|
|
|
|
if (TemplateType)
|
|
|
|
if (auto *ClassTemplate = dyn_cast_or_null<ClassTemplateDecl>(
|
|
|
|
TemplateType->getTemplateName().getAsTemplateDecl()))
|
|
|
|
return ClassTemplate->getTemplatedDecl();
|
|
|
|
|
|
|
|
return nullptr;
|
2013-08-03 05:24:09 +08:00
|
|
|
}
|
|
|
|
|
2019-08-13 07:23:35 +08:00
|
|
|
// Returns true if the given C++ class is directly or indirectly derived
|
2012-09-07 20:48:17 +08:00
|
|
|
// from a base type with the given name. A class is not considered to be
|
|
|
|
// derived from itself.
|
2012-07-17 15:39:27 +08:00
|
|
|
bool MatchASTVisitor::classIsDerivedFrom(const CXXRecordDecl *Declaration,
|
|
|
|
const Matcher<NamedDecl> &Base,
|
2019-07-25 19:54:13 +08:00
|
|
|
BoundNodesTreeBuilder *Builder,
|
|
|
|
bool Directly) {
|
2012-07-17 15:39:27 +08:00
|
|
|
if (!Declaration->hasDefinition())
|
2012-07-06 13:48:52 +08:00
|
|
|
return false;
|
2014-03-13 23:41:46 +08:00
|
|
|
for (const auto &It : Declaration->bases()) {
|
|
|
|
const Type *TypeNode = It.getType().getTypePtr();
|
2012-07-06 13:48:52 +08:00
|
|
|
|
2012-07-17 15:39:27 +08:00
|
|
|
if (typeHasMatchingAlias(TypeNode, Base, Builder))
|
2012-07-06 13:48:52 +08:00
|
|
|
return true;
|
|
|
|
|
2016-02-10 04:59:05 +08:00
|
|
|
// FIXME: Going to the primary template here isn't really correct, but
|
|
|
|
// unfortunately we accept a Decl matcher for the base class not a Type
|
|
|
|
// matcher, so it's the best thing we can do with our current interface.
|
|
|
|
CXXRecordDecl *ClassDecl = getAsCXXRecordDeclOrPrimaryTemplate(TypeNode);
|
2014-05-18 02:49:24 +08:00
|
|
|
if (!ClassDecl)
|
2012-07-06 13:48:52 +08:00
|
|
|
continue;
|
2012-12-04 21:40:29 +08:00
|
|
|
if (ClassDecl == Declaration) {
|
|
|
|
// This can happen for recursive template definitions; if the
|
|
|
|
// current declaration did not match, we can safely return false.
|
|
|
|
return false;
|
|
|
|
}
|
Completely revamp node binding for AST matchers.
This is in preparation for the backwards references to bound
nodes, which will expose a lot more about how matches occur. Main
changes:
- instead of building the tree of bound nodes, we build a "set" of bound
nodes and explode all possible match combinations while running
through the matchers; this will allow us to also implement matchers
that filter down the current set of matches, like "equalsBoundNode"
- take the set of bound nodes at the start of the match into
consideration when doing memoization; as part of that, reevaluated
that memoization gives us benefits that are large enough (it still
does - the effect on common match patterns is up to an order of
magnitude)
- reset the bound nodes when a node does not match, thus never leaking
information from partial sub-matcher matches for failing matchers
Effects:
- we can now correctly "explode" combinatorial matches, for example:
allOf(forEachDescendant(...bind("a")),
forEachDescendant(...bind("b"))) will now trigger matches for all
combinations of matching "a" and "b"s.
- we now never expose bound nodes from partial matches in matchers that
did not match in the end - this fixes a long-standing issue
FIXMEs:
- rename BoundNodesTreeBuilder to BoundNodesBuilder or
BoundNodesSetBuilder, as we don't build a tree any more; this is out
of scope for this change, though
- we're seeing some performance regressions (around 10%), but I expect
some performance tuning will get that back, and it's easily worth
the increase in expressiveness for now
llvm-svn: 184313
2013-06-19 23:42:45 +08:00
|
|
|
BoundNodesTreeBuilder Result(*Builder);
|
|
|
|
if (Base.matches(*ClassDecl, this, &Result)) {
|
2014-10-05 01:01:26 +08:00
|
|
|
*Builder = std::move(Result);
|
2012-09-07 20:48:17 +08:00
|
|
|
return true;
|
Completely revamp node binding for AST matchers.
This is in preparation for the backwards references to bound
nodes, which will expose a lot more about how matches occur. Main
changes:
- instead of building the tree of bound nodes, we build a "set" of bound
nodes and explode all possible match combinations while running
through the matchers; this will allow us to also implement matchers
that filter down the current set of matches, like "equalsBoundNode"
- take the set of bound nodes at the start of the match into
consideration when doing memoization; as part of that, reevaluated
that memoization gives us benefits that are large enough (it still
does - the effect on common match patterns is up to an order of
magnitude)
- reset the bound nodes when a node does not match, thus never leaking
information from partial sub-matcher matches for failing matchers
Effects:
- we can now correctly "explode" combinatorial matches, for example:
allOf(forEachDescendant(...bind("a")),
forEachDescendant(...bind("b"))) will now trigger matches for all
combinations of matching "a" and "b"s.
- we now never expose bound nodes from partial matches in matchers that
did not match in the end - this fixes a long-standing issue
FIXMEs:
- rename BoundNodesTreeBuilder to BoundNodesBuilder or
BoundNodesSetBuilder, as we don't build a tree any more; this is out
of scope for this change, though
- we're seeing some performance regressions (around 10%), but I expect
some performance tuning will get that back, and it's easily worth
the increase in expressiveness for now
llvm-svn: 184313
2013-06-19 23:42:45 +08:00
|
|
|
}
|
2019-07-25 19:54:13 +08:00
|
|
|
if (!Directly && classIsDerivedFrom(ClassDecl, Base, Builder, Directly))
|
2012-07-06 13:48:52 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-08-13 07:23:35 +08:00
|
|
|
// Returns true if the given Objective-C class is directly or indirectly
|
|
|
|
// derived from a matching base class. A class is not considered to be derived
|
|
|
|
// from itself.
|
|
|
|
bool MatchASTVisitor::objcClassIsDerivedFrom(
|
|
|
|
const ObjCInterfaceDecl *Declaration, const Matcher<NamedDecl> &Base,
|
|
|
|
BoundNodesTreeBuilder *Builder, bool Directly) {
|
|
|
|
// Check if any of the superclasses of the class match.
|
|
|
|
for (const ObjCInterfaceDecl *ClassDecl = Declaration->getSuperClass();
|
|
|
|
ClassDecl != nullptr; ClassDecl = ClassDecl->getSuperClass()) {
|
|
|
|
// Check if there are any matching compatibility aliases.
|
|
|
|
if (objcClassHasMatchingCompatibilityAlias(ClassDecl, Base, Builder))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Check if there are any matching type aliases.
|
|
|
|
const Type *TypeNode = ClassDecl->getTypeForDecl();
|
|
|
|
if (typeHasMatchingAlias(TypeNode, Base, Builder))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (Base.matches(*ClassDecl, this, Builder))
|
|
|
|
return true;
|
|
|
|
|
2019-11-23 12:03:06 +08:00
|
|
|
// Not `return false` as a temporary workaround for PR43879.
|
2019-08-13 07:23:35 +08:00
|
|
|
if (Directly)
|
2019-11-23 12:03:06 +08:00
|
|
|
break;
|
2019-08-13 07:23:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-07-11 04:20:19 +08:00
|
|
|
bool MatchASTVisitor::TraverseDecl(Decl *DeclNode) {
|
2014-05-18 02:49:24 +08:00
|
|
|
if (!DeclNode) {
|
2012-07-06 13:48:52 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
match(*DeclNode);
|
2012-07-11 04:20:19 +08:00
|
|
|
return RecursiveASTVisitor<MatchASTVisitor>::TraverseDecl(DeclNode);
|
2012-07-06 13:48:52 +08:00
|
|
|
}
|
|
|
|
|
2018-03-03 05:55:03 +08:00
|
|
|
bool MatchASTVisitor::TraverseStmt(Stmt *StmtNode, DataRecursionQueue *Queue) {
|
2014-05-18 02:49:24 +08:00
|
|
|
if (!StmtNode) {
|
2012-07-06 13:48:52 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
match(*StmtNode);
|
2018-03-03 05:55:03 +08:00
|
|
|
return RecursiveASTVisitor<MatchASTVisitor>::TraverseStmt(StmtNode, Queue);
|
2012-07-06 13:48:52 +08:00
|
|
|
}
|
|
|
|
|
2012-07-11 04:20:19 +08:00
|
|
|
bool MatchASTVisitor::TraverseType(QualType TypeNode) {
|
2012-07-06 13:48:52 +08:00
|
|
|
match(TypeNode);
|
2012-07-11 04:20:19 +08:00
|
|
|
return RecursiveASTVisitor<MatchASTVisitor>::TraverseType(TypeNode);
|
2012-07-06 13:48:52 +08:00
|
|
|
}
|
|
|
|
|
2012-10-17 16:52:59 +08:00
|
|
|
bool MatchASTVisitor::TraverseTypeLoc(TypeLoc TypeLocNode) {
|
|
|
|
// The RecursiveASTVisitor only visits types if they're not within TypeLocs.
|
|
|
|
// We still want to find those types via matchers, so we match them here. Note
|
|
|
|
// that the TypeLocs are structurally a shadow-hierarchy to the expressed
|
|
|
|
// type, so we visit all involved parts of a compound type when matching on
|
|
|
|
// each TypeLoc.
|
|
|
|
match(TypeLocNode);
|
|
|
|
match(TypeLocNode.getType());
|
|
|
|
return RecursiveASTVisitor<MatchASTVisitor>::TraverseTypeLoc(TypeLocNode);
|
2012-07-06 13:48:52 +08:00
|
|
|
}
|
|
|
|
|
2012-09-13 21:11:25 +08:00
|
|
|
bool MatchASTVisitor::TraverseNestedNameSpecifier(NestedNameSpecifier *NNS) {
|
|
|
|
match(*NNS);
|
|
|
|
return RecursiveASTVisitor<MatchASTVisitor>::TraverseNestedNameSpecifier(NNS);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MatchASTVisitor::TraverseNestedNameSpecifierLoc(
|
|
|
|
NestedNameSpecifierLoc NNS) {
|
2015-10-23 17:04:55 +08:00
|
|
|
if (!NNS)
|
|
|
|
return true;
|
|
|
|
|
2012-09-13 21:11:25 +08:00
|
|
|
match(NNS);
|
2015-10-23 17:04:55 +08:00
|
|
|
|
2012-09-13 21:11:25 +08:00
|
|
|
// We only match the nested name specifier here (as opposed to traversing it)
|
|
|
|
// because the traversal is already done in the parallel "Loc"-hierarchy.
|
2014-08-29 06:18:42 +08:00
|
|
|
if (NNS.hasQualifier())
|
|
|
|
match(*NNS.getNestedNameSpecifier());
|
2012-09-13 21:11:25 +08:00
|
|
|
return
|
|
|
|
RecursiveASTVisitor<MatchASTVisitor>::TraverseNestedNameSpecifierLoc(NNS);
|
|
|
|
}
|
|
|
|
|
2016-09-27 01:04:27 +08:00
|
|
|
bool MatchASTVisitor::TraverseConstructorInitializer(
|
|
|
|
CXXCtorInitializer *CtorInit) {
|
|
|
|
if (!CtorInit)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
match(*CtorInit);
|
|
|
|
|
|
|
|
return RecursiveASTVisitor<MatchASTVisitor>::TraverseConstructorInitializer(
|
|
|
|
CtorInit);
|
|
|
|
}
|
|
|
|
|
2012-07-11 04:20:19 +08:00
|
|
|
class MatchASTConsumer : public ASTConsumer {
|
2012-07-06 13:48:52 +08:00
|
|
|
public:
|
2013-11-08 06:30:36 +08:00
|
|
|
MatchASTConsumer(MatchFinder *Finder,
|
|
|
|
MatchFinder::ParsingDoneTestCallback *ParsingDone)
|
|
|
|
: Finder(Finder), ParsingDone(ParsingDone) {}
|
2012-07-06 13:48:52 +08:00
|
|
|
|
|
|
|
private:
|
2014-03-13 16:12:15 +08:00
|
|
|
void HandleTranslationUnit(ASTContext &Context) override {
|
2014-05-18 02:49:24 +08:00
|
|
|
if (ParsingDone != nullptr) {
|
2012-07-06 13:48:52 +08:00
|
|
|
ParsingDone->run();
|
|
|
|
}
|
2013-11-08 06:30:36 +08:00
|
|
|
Finder->matchAST(Context);
|
2012-07-06 13:48:52 +08:00
|
|
|
}
|
|
|
|
|
2013-11-08 06:30:36 +08:00
|
|
|
MatchFinder *Finder;
|
2012-07-06 13:48:52 +08:00
|
|
|
MatchFinder::ParsingDoneTestCallback *ParsingDone;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace
|
|
|
|
} // end namespace internal
|
|
|
|
|
|
|
|
MatchFinder::MatchResult::MatchResult(const BoundNodes &Nodes,
|
2012-07-11 04:20:19 +08:00
|
|
|
ASTContext *Context)
|
2012-07-06 13:48:52 +08:00
|
|
|
: Nodes(Nodes), Context(Context),
|
|
|
|
SourceManager(&Context->getSourceManager()) {}
|
|
|
|
|
2015-10-20 21:23:58 +08:00
|
|
|
MatchFinder::MatchCallback::~MatchCallback() {}
|
|
|
|
MatchFinder::ParsingDoneTestCallback::~ParsingDoneTestCallback() {}
|
2012-07-06 13:48:52 +08:00
|
|
|
|
2014-10-23 04:31:05 +08:00
|
|
|
MatchFinder::MatchFinder(MatchFinderOptions Options)
|
|
|
|
: Options(std::move(Options)), ParsingDone(nullptr) {}
|
2012-07-06 13:48:52 +08:00
|
|
|
|
2015-10-20 21:23:58 +08:00
|
|
|
MatchFinder::~MatchFinder() {}
|
2012-07-06 13:48:52 +08:00
|
|
|
|
|
|
|
void MatchFinder::addMatcher(const DeclarationMatcher &NodeMatch,
|
|
|
|
MatchCallback *Action) {
|
2015-05-30 03:42:19 +08:00
|
|
|
Matchers.DeclOrStmt.emplace_back(NodeMatch, Action);
|
2015-10-08 03:56:12 +08:00
|
|
|
Matchers.AllCallbacks.insert(Action);
|
2012-07-06 13:48:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MatchFinder::addMatcher(const TypeMatcher &NodeMatch,
|
|
|
|
MatchCallback *Action) {
|
2015-05-30 03:42:19 +08:00
|
|
|
Matchers.Type.emplace_back(NodeMatch, Action);
|
2015-10-08 03:56:12 +08:00
|
|
|
Matchers.AllCallbacks.insert(Action);
|
2012-07-06 13:48:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MatchFinder::addMatcher(const StatementMatcher &NodeMatch,
|
|
|
|
MatchCallback *Action) {
|
2015-05-30 03:42:19 +08:00
|
|
|
Matchers.DeclOrStmt.emplace_back(NodeMatch, Action);
|
2015-10-08 03:56:12 +08:00
|
|
|
Matchers.AllCallbacks.insert(Action);
|
2012-07-06 13:48:52 +08:00
|
|
|
}
|
|
|
|
|
2012-09-13 21:11:25 +08:00
|
|
|
void MatchFinder::addMatcher(const NestedNameSpecifierMatcher &NodeMatch,
|
|
|
|
MatchCallback *Action) {
|
2015-05-30 03:42:19 +08:00
|
|
|
Matchers.NestedNameSpecifier.emplace_back(NodeMatch, Action);
|
2015-10-08 03:56:12 +08:00
|
|
|
Matchers.AllCallbacks.insert(Action);
|
2012-09-13 21:11:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MatchFinder::addMatcher(const NestedNameSpecifierLocMatcher &NodeMatch,
|
|
|
|
MatchCallback *Action) {
|
2015-05-30 03:42:19 +08:00
|
|
|
Matchers.NestedNameSpecifierLoc.emplace_back(NodeMatch, Action);
|
2015-10-08 03:56:12 +08:00
|
|
|
Matchers.AllCallbacks.insert(Action);
|
2012-09-13 21:11:25 +08:00
|
|
|
}
|
|
|
|
|
2012-10-17 16:52:59 +08:00
|
|
|
void MatchFinder::addMatcher(const TypeLocMatcher &NodeMatch,
|
|
|
|
MatchCallback *Action) {
|
2015-05-30 03:42:19 +08:00
|
|
|
Matchers.TypeLoc.emplace_back(NodeMatch, Action);
|
2015-10-08 03:56:12 +08:00
|
|
|
Matchers.AllCallbacks.insert(Action);
|
2012-10-17 16:52:59 +08:00
|
|
|
}
|
|
|
|
|
2016-09-27 01:04:27 +08:00
|
|
|
void MatchFinder::addMatcher(const CXXCtorInitializerMatcher &NodeMatch,
|
|
|
|
MatchCallback *Action) {
|
|
|
|
Matchers.CtorInit.emplace_back(NodeMatch, Action);
|
|
|
|
Matchers.AllCallbacks.insert(Action);
|
|
|
|
}
|
|
|
|
|
2013-11-08 06:30:32 +08:00
|
|
|
bool MatchFinder::addDynamicMatcher(const internal::DynTypedMatcher &NodeMatch,
|
|
|
|
MatchCallback *Action) {
|
|
|
|
if (NodeMatch.canConvertTo<Decl>()) {
|
|
|
|
addMatcher(NodeMatch.convertTo<Decl>(), Action);
|
|
|
|
return true;
|
|
|
|
} else if (NodeMatch.canConvertTo<QualType>()) {
|
|
|
|
addMatcher(NodeMatch.convertTo<QualType>(), Action);
|
|
|
|
return true;
|
|
|
|
} else if (NodeMatch.canConvertTo<Stmt>()) {
|
|
|
|
addMatcher(NodeMatch.convertTo<Stmt>(), Action);
|
|
|
|
return true;
|
|
|
|
} else if (NodeMatch.canConvertTo<NestedNameSpecifier>()) {
|
|
|
|
addMatcher(NodeMatch.convertTo<NestedNameSpecifier>(), Action);
|
|
|
|
return true;
|
|
|
|
} else if (NodeMatch.canConvertTo<NestedNameSpecifierLoc>()) {
|
|
|
|
addMatcher(NodeMatch.convertTo<NestedNameSpecifierLoc>(), Action);
|
|
|
|
return true;
|
|
|
|
} else if (NodeMatch.canConvertTo<TypeLoc>()) {
|
|
|
|
addMatcher(NodeMatch.convertTo<TypeLoc>(), Action);
|
|
|
|
return true;
|
2016-09-27 01:04:27 +08:00
|
|
|
} else if (NodeMatch.canConvertTo<CXXCtorInitializer>()) {
|
|
|
|
addMatcher(NodeMatch.convertTo<CXXCtorInitializer>(), Action);
|
|
|
|
return true;
|
2013-11-08 06:30:32 +08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-08-11 03:56:51 +08:00
|
|
|
std::unique_ptr<ASTConsumer> MatchFinder::newASTConsumer() {
|
2019-08-15 07:04:18 +08:00
|
|
|
return std::make_unique<internal::MatchASTConsumer>(this, ParsingDone);
|
2012-07-06 13:48:52 +08:00
|
|
|
}
|
|
|
|
|
2013-02-01 21:41:35 +08:00
|
|
|
void MatchFinder::match(const clang::ast_type_traits::DynTypedNode &Node,
|
|
|
|
ASTContext &Context) {
|
2014-10-23 04:31:05 +08:00
|
|
|
internal::MatchASTVisitor Visitor(&Matchers, Options);
|
2012-10-24 22:47:44 +08:00
|
|
|
Visitor.set_active_ast_context(&Context);
|
2013-02-01 21:41:35 +08:00
|
|
|
Visitor.match(Node);
|
2012-10-24 22:47:44 +08:00
|
|
|
}
|
|
|
|
|
2013-11-08 06:30:36 +08:00
|
|
|
void MatchFinder::matchAST(ASTContext &Context) {
|
2014-10-23 04:31:05 +08:00
|
|
|
internal::MatchASTVisitor Visitor(&Matchers, Options);
|
2013-11-08 06:30:36 +08:00
|
|
|
Visitor.set_active_ast_context(&Context);
|
|
|
|
Visitor.onStartOfTranslationUnit();
|
[AST] Allow limiting the scope of common AST traversals (getParents, RAV).
Summary:
The goal is to allow analyses such as clang-tidy checks to run on a
subset of the AST, e.g. "only on main-file decls" for interactive tools.
Today, these become "problematically global" by running RecursiveASTVisitors
rooted at the TUDecl, or by navigating up via ASTContext::getParent().
The scope is restricted using a set of top-level-decls that RecursiveASTVisitors
should be rooted at. This also applies to the visitor that populates the
parent map, and so the top-level-decls are considered to have no parents.
This patch makes the traversal scope a mutable property of ASTContext.
The more obvious way to do this is to pass the top-level decls to
relevant functions directly, but this has some problems:
- it's error-prone: accidentally mixing restricted and unrestricted
scopes is a performance trap. Interleaving multiple analyses is
common (many clang-tidy checks run matchers or RAVs from matcher callbacks)
- it doesn't map well to the actual use cases, where we really do want
*all* traversals to be restricted.
- it involves a lot of plumbing in parts of the code that don't care
about traversals.
This approach was tried out in D54259 and D54261, I wanted to like it
but it feels pretty awful in practice.
Caveats: to get scope-limiting behavior of RecursiveASTVisitors, callers
have to call the new TraverseAST(Ctx) function instead of TraverseDecl(TU).
I think this is an improvement to the API regardless.
Reviewers: klimek, ioeric
Subscribers: mgorny, cfe-commits
Differential Revision: https://reviews.llvm.org/D54309
llvm-svn: 346847
2018-11-14 18:33:30 +08:00
|
|
|
Visitor.TraverseAST(Context);
|
2013-11-08 06:30:36 +08:00
|
|
|
Visitor.onEndOfTranslationUnit();
|
|
|
|
}
|
|
|
|
|
2012-07-06 13:48:52 +08:00
|
|
|
void MatchFinder::registerTestCallbackAfterParsing(
|
|
|
|
MatchFinder::ParsingDoneTestCallback *NewParsingDone) {
|
|
|
|
ParsingDone = NewParsingDone;
|
|
|
|
}
|
|
|
|
|
2014-10-23 04:31:05 +08:00
|
|
|
StringRef MatchFinder::MatchCallback::getID() const { return "<unknown>"; }
|
|
|
|
|
2012-07-06 13:48:52 +08:00
|
|
|
} // end namespace ast_matchers
|
|
|
|
} // end namespace clang
|