23 #include "llvm/ADT/DenseMap.h"
24 #include "llvm/ADT/StringMap.h"
25 #include "llvm/Support/Timer.h"
31 namespace ast_matchers {
35 typedef MatchFinder::MatchCallback MatchCallback;
45 static const unsigned MaxMemoizationEntries = 10000;
61 ast_type_traits::DynTypedNode
Node;
64 bool operator<(
const MatchKey &Other)
const {
66 std::tie(Other.MatcherID, Other.Node, Other.BoundNodes);
71 struct MemoizedMatchResult {
78 class MatchChildASTVisitor
79 :
public RecursiveASTVisitor<MatchChildASTVisitor> {
81 typedef RecursiveASTVisitor<MatchChildASTVisitor> VisitorBase;
87 MatchChildASTVisitor(
const DynTypedMatcher *
Matcher,
92 ASTMatchFinder::BindKind
Bind)
113 bool findMatch(
const ast_type_traits::DynTypedNode &DynNode) {
115 if (
const Decl *D = DynNode.get<
Decl>())
117 else if (
const Stmt *
S = DynNode.get<
Stmt>())
119 else if (
const NestedNameSpecifier *NNS =
120 DynNode.get<NestedNameSpecifier>())
122 else if (
const NestedNameSpecifierLoc *NNSLoc =
123 DynNode.get<NestedNameSpecifierLoc>())
125 else if (
const QualType *Q = DynNode.get<QualType>())
127 else if (
const TypeLoc *T = DynNode.get<TypeLoc>())
142 bool TraverseDecl(
Decl *DeclNode) {
144 return (DeclNode ==
nullptr) || traverse(*DeclNode);
146 bool TraverseStmt(
Stmt *StmtNode) {
148 const Stmt *StmtToTraverse = StmtNode;
150 ASTMatchFinder::TK_IgnoreImplicitCastsAndParentheses) {
151 const Expr *ExprNode = dyn_cast_or_null<Expr>(StmtNode);
153 StmtToTraverse = ExprNode->IgnoreParenImpCasts();
156 return (StmtToTraverse ==
nullptr) || traverse(*StmtToTraverse);
160 bool TraverseType(QualType TypeNode) {
161 if (TypeNode.isNull())
165 if (!
match(*TypeNode))
168 return traverse(TypeNode);
172 bool TraverseTypeLoc(TypeLoc TypeLocNode) {
173 if (TypeLocNode.isNull())
177 if (!
match(*TypeLocNode.getType()))
180 if (!
match(TypeLocNode.getType()))
183 return traverse(TypeLocNode);
185 bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS) {
187 return (NNS ==
nullptr) || traverse(*NNS);
189 bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
193 if (!
match(*NNS.getNestedNameSpecifier()))
195 return traverse(NNS);
198 bool shouldVisitTemplateInstantiations()
const {
return true; }
199 bool shouldVisitImplicitCode()
const {
return true; }
203 struct ScopedIncrement {
204 explicit ScopedIncrement(
int *
Depth) : Depth(Depth) { ++(*Depth); }
205 ~ScopedIncrement() { --(*Depth); }
219 bool baseTraverse(
const Decl &DeclNode) {
222 bool baseTraverse(
const Stmt &StmtNode) {
225 bool baseTraverse(QualType TypeNode) {
228 bool baseTraverse(TypeLoc TypeLocNode) {
231 bool baseTraverse(
const NestedNameSpecifier &NNS) {
233 const_cast<NestedNameSpecifier*>(&NNS));
235 bool baseTraverse(NestedNameSpecifierLoc NNS) {
244 template <
typename T>
249 if (Bind != ASTMatchFinder::BK_All) {
250 BoundNodesTreeBuilder RecursiveBuilder(*Builder);
252 &RecursiveBuilder)) {
258 BoundNodesTreeBuilder RecursiveBuilder(*Builder);
260 &RecursiveBuilder)) {
271 template <
typename T>
272 bool traverse(
const T &Node) {
273 static_assert(IsBaseType<T>::value,
274 "traverse can only be instantiated with base type");
277 return baseTraverse(Node);
287 const ASTMatchFinder::BindKind
Bind;
293 class MatchASTVisitor :
public RecursiveASTVisitor<MatchASTVisitor>,
294 public ASTMatchFinder {
296 MatchASTVisitor(
const MatchFinder::MatchersByType *
Matchers,
297 const MatchFinder::MatchFinderOptions &
Options)
300 ~MatchASTVisitor()
override {
301 if (Options.CheckProfiling) {
302 Options.CheckProfiling->Records = std::move(
TimeByBucket);
306 void onStartOfTranslationUnit() {
307 const bool EnableCheckProfiling = Options.CheckProfiling.hasValue();
308 TimeBucketRegion Timer;
309 for (MatchCallback *MC : Matchers->AllCallbacks) {
310 if (EnableCheckProfiling)
312 MC->onStartOfTranslationUnit();
316 void onEndOfTranslationUnit() {
317 const bool EnableCheckProfiling = Options.CheckProfiling.hasValue();
318 TimeBucketRegion Timer;
319 for (MatchCallback *MC : Matchers->AllCallbacks) {
320 if (EnableCheckProfiling)
322 MC->onEndOfTranslationUnit();
326 void set_active_ast_context(
ASTContext *NewActiveASTContext) {
333 bool VisitTypedefNameDecl(TypedefNameDecl *DeclNode) {
361 const Type *TypeNode = DeclNode->getUnderlyingType().getTypePtr();
362 const Type *CanonicalType =
368 bool TraverseDecl(
Decl *DeclNode);
369 bool TraverseStmt(
Stmt *StmtNode);
370 bool TraverseType(QualType TypeNode);
371 bool TraverseTypeLoc(TypeLoc TypeNode);
372 bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS);
373 bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS);
376 bool memoizedMatchesRecursively(
const ast_type_traits::DynTypedNode &Node,
377 const DynTypedMatcher &Matcher,
378 BoundNodesTreeBuilder *Builder,
int MaxDepth,
379 TraversalKind Traversal, BindKind Bind) {
381 if (!Node.getMemoizationData() || !Builder->isComparable())
382 return matchesRecursively(Node, Matcher, Builder, MaxDepth, Traversal,
386 Key.MatcherID = Matcher.getID();
393 *Builder = I->second.Nodes;
394 return I->second.ResultOfMatch;
397 MemoizedMatchResult
Result;
399 Result.ResultOfMatch = matchesRecursively(Node, Matcher, &Result.Nodes,
400 MaxDepth, Traversal, Bind);
402 MemoizedMatchResult &CachedResult =
ResultCache[Key];
403 CachedResult = std::move(Result);
405 *Builder = CachedResult.Nodes;
406 return CachedResult.ResultOfMatch;
410 bool matchesRecursively(
const ast_type_traits::DynTypedNode &Node,
411 const DynTypedMatcher &Matcher,
412 BoundNodesTreeBuilder *Builder,
int MaxDepth,
413 TraversalKind Traversal, BindKind Bind) {
414 MatchChildASTVisitor Visitor(
415 &Matcher,
this, Builder, MaxDepth, Traversal, Bind);
416 return Visitor.findMatch(Node);
419 bool classIsDerivedFrom(
const CXXRecordDecl *Declaration,
420 const Matcher<NamedDecl> &
Base,
421 BoundNodesTreeBuilder *Builder)
override;
424 bool matchesChildOf(
const ast_type_traits::DynTypedNode &Node,
425 const DynTypedMatcher &Matcher,
426 BoundNodesTreeBuilder *Builder,
427 TraversalKind Traversal,
428 BindKind Bind)
override {
431 return memoizedMatchesRecursively(Node, Matcher, Builder, 1, Traversal,
435 bool matchesDescendantOf(
const ast_type_traits::DynTypedNode &Node,
436 const DynTypedMatcher &Matcher,
437 BoundNodesTreeBuilder *Builder,
438 BindKind Bind)
override {
441 return memoizedMatchesRecursively(Node, Matcher, Builder,
INT_MAX,
445 bool matchesAncestorOf(
const ast_type_traits::DynTypedNode &Node,
446 const DynTypedMatcher &Matcher,
447 BoundNodesTreeBuilder *Builder,
448 AncestorMatchMode MatchMode)
override {
453 return memoizedMatchesAncestorOfRecursively(Node, Matcher, Builder,
459 void match(
const ast_type_traits::DynTypedNode &Node) {
461 if (
auto *N = Node.get<
Decl>()) {
463 }
else if (
auto *N = Node.get<
Stmt>()) {
465 }
else if (
auto *N = Node.get<Type>()) {
467 }
else if (
auto *N = Node.get<QualType>()) {
469 }
else if (
auto *N = Node.get<NestedNameSpecifier>()) {
471 }
else if (
auto *N = Node.get<NestedNameSpecifierLoc>()) {
473 }
else if (
auto *N = Node.get<TypeLoc>()) {
478 template <
typename T>
void match(
const T &Node) {
479 matchDispatch(&Node);
485 bool shouldVisitTemplateInstantiations()
const {
return true; }
486 bool shouldVisitImplicitCode()
const {
return true; }
489 class TimeBucketRegion {
491 TimeBucketRegion() :
Bucket(nullptr) {}
492 ~TimeBucketRegion() { setBucket(
nullptr); }
502 void setBucket(llvm::TimeRecord *NewBucket) {
503 if (
Bucket != NewBucket) {
504 auto Now = llvm::TimeRecord::getCurrentTime(
true);
520 template <
typename T,
typename MC>
521 void matchWithoutFilter(
const T &Node,
const MC &Matchers) {
522 const bool EnableCheckProfiling = Options.CheckProfiling.hasValue();
523 TimeBucketRegion Timer;
524 for (
const auto &MP : Matchers) {
525 if (EnableCheckProfiling)
528 if (MP.first.matches(Node,
this, &Builder)) {
530 Builder.visitMatches(&Visitor);
535 void matchWithFilter(
const ast_type_traits::DynTypedNode &DynNode) {
536 auto Kind = DynNode.getNodeKind();
544 const bool EnableCheckProfiling = Options.CheckProfiling.hasValue();
545 TimeBucketRegion Timer;
546 auto &Matchers = this->Matchers->DeclOrStmt;
547 for (
unsigned short I : Filter) {
548 auto &MP = Matchers[
I];
549 if (EnableCheckProfiling)
552 if (MP.first.matchesNoKindCheck(DynNode,
this, &Builder)) {
554 Builder.visitMatches(&Visitor);
559 const std::vector<unsigned short> &
560 getFilterForKind(ast_type_traits::ASTNodeKind
Kind) {
562 auto &Matchers = this->Matchers->DeclOrStmt;
563 assert((Matchers.size() <
USHRT_MAX) &&
"Too many matchers.");
564 for (
unsigned I = 0,
E = Matchers.size(); I !=
E; ++
I) {
565 if (Matchers[I].first.canMatchNodesOfKind(Kind)) {
574 void matchDispatch(
const Decl *Node) {
577 void matchDispatch(
const Stmt *Node) {
581 void matchDispatch(
const Type *Node) {
582 matchWithoutFilter(QualType(Node, 0), Matchers->Type);
584 void matchDispatch(
const TypeLoc *Node) {
585 matchWithoutFilter(*Node, Matchers->TypeLoc);
587 void matchDispatch(
const QualType *Node) {
588 matchWithoutFilter(*Node, Matchers->Type);
590 void matchDispatch(
const NestedNameSpecifier *Node) {
591 matchWithoutFilter(*Node, Matchers->NestedNameSpecifier);
593 void matchDispatch(
const NestedNameSpecifierLoc *Node) {
594 matchWithoutFilter(*Node, Matchers->NestedNameSpecifierLoc);
596 void matchDispatch(
const void *) { }
612 bool memoizedMatchesAncestorOfRecursively(
613 const ast_type_traits::DynTypedNode &Node,
const DynTypedMatcher &Matcher,
614 BoundNodesTreeBuilder *Builder, AncestorMatchMode MatchMode) {
615 if (Node.get<TranslationUnitDecl>() ==
620 if (!Builder->isComparable())
621 return matchesAncestorOfRecursively(Node, Matcher, Builder, MatchMode);
624 Key.MatcherID = Matcher.getID();
632 *Builder = I->second.Nodes;
633 return I->second.ResultOfMatch;
636 MemoizedMatchResult
Result;
638 Result.ResultOfMatch =
639 matchesAncestorOfRecursively(Node, Matcher, &Result.Nodes, MatchMode);
641 MemoizedMatchResult &CachedResult =
ResultCache[Key];
642 CachedResult = std::move(Result);
644 *Builder = CachedResult.Nodes;
645 return CachedResult.ResultOfMatch;
648 bool matchesAncestorOfRecursively(
const ast_type_traits::DynTypedNode &Node,
649 const DynTypedMatcher &Matcher,
650 BoundNodesTreeBuilder *Builder,
651 AncestorMatchMode MatchMode) {
653 assert(!Parents.empty() &&
"Found node that is not in the parent map.");
654 if (Parents.size() == 1) {
656 const ast_type_traits::DynTypedNode Parent = Parents[0];
657 BoundNodesTreeBuilder BuilderCopy = *
Builder;
658 if (Matcher.matches(Parent,
this, &BuilderCopy)) {
659 *Builder = std::move(BuilderCopy);
662 if (MatchMode != ASTMatchFinder::AMM_ParentOnly) {
663 return memoizedMatchesAncestorOfRecursively(Parent, Matcher, Builder,
671 std::deque<ast_type_traits::DynTypedNode> Queue(Parents.begin(),
673 while (!Queue.empty()) {
674 BoundNodesTreeBuilder BuilderCopy = *
Builder;
675 if (Matcher.matches(Queue.front(),
this, &BuilderCopy)) {
676 *Builder = std::move(BuilderCopy);
679 if (MatchMode != ASTMatchFinder::AMM_ParentOnly) {
680 for (
const auto &Parent :
685 if (Visited.insert(Parent.getMemoizationData()).second)
686 Queue.push_back(Parent);
697 class MatchVisitor :
public BoundNodesTreeBuilder::Visitor {
700 MatchFinder::MatchCallback*
Callback)
702 Callback(Callback) {}
704 void visitMatch(
const BoundNodes& BoundNodesView)
override {
714 bool typeHasMatchingAlias(
const Type *TypeNode,
715 const Matcher<NamedDecl> &Matcher,
716 BoundNodesTreeBuilder *Builder) {
717 const Type *
const CanonicalType =
719 for (
const TypedefNameDecl *Alias :
TypeAliases.lookup(CanonicalType)) {
720 BoundNodesTreeBuilder
Result(*Builder);
721 if (Matcher.matches(*Alias,
this, &Result)) {
722 *Builder = std::move(Result);
744 llvm::DenseMap<ast_type_traits::ASTNodeKind, std::vector<unsigned short>>
747 const MatchFinder::MatchFinderOptions &
Options;
751 llvm::DenseMap<const Type*, std::set<const TypedefNameDecl*> >
TypeAliases;
754 typedef std::map<MatchKey, MemoizedMatchResult> MemoizationMap;
758 static CXXRecordDecl *
759 getAsCXXRecordDeclOrPrimaryTemplate(
const Type *TypeNode) {
760 if (
auto *RD = TypeNode->getAsCXXRecordDecl())
765 while (TemplateType && TemplateType->isTypeAlias())
772 if (
auto *ClassTemplate = dyn_cast_or_null<ClassTemplateDecl>(
773 TemplateType->getTemplateName().getAsTemplateDecl()))
774 return ClassTemplate->getTemplatedDecl();
782 bool MatchASTVisitor::classIsDerivedFrom(
const CXXRecordDecl *Declaration,
783 const Matcher<NamedDecl> &
Base,
784 BoundNodesTreeBuilder *Builder) {
785 if (!Declaration->hasDefinition())
787 for (
const auto &It : Declaration->bases()) {
788 const Type *TypeNode = It.getType().getTypePtr();
790 if (typeHasMatchingAlias(TypeNode, Base, Builder))
796 CXXRecordDecl *ClassDecl = getAsCXXRecordDeclOrPrimaryTemplate(TypeNode);
799 if (ClassDecl == Declaration) {
804 BoundNodesTreeBuilder
Result(*Builder);
805 if (Base.matches(*ClassDecl,
this, &Result)) {
806 *Builder = std::move(Result);
809 if (classIsDerivedFrom(ClassDecl, Base, Builder))
815 bool MatchASTVisitor::TraverseDecl(
Decl *DeclNode) {
823 bool MatchASTVisitor::TraverseStmt(
Stmt *StmtNode) {
831 bool MatchASTVisitor::TraverseType(QualType TypeNode) {
836 bool MatchASTVisitor::TraverseTypeLoc(TypeLoc TypeLocNode) {
843 match(TypeLocNode.getType());
847 bool MatchASTVisitor::TraverseNestedNameSpecifier(NestedNameSpecifier *NNS) {
852 bool MatchASTVisitor::TraverseNestedNameSpecifierLoc(
853 NestedNameSpecifierLoc NNS) {
861 if (NNS.hasQualifier())
862 match(*NNS.getNestedNameSpecifier());
867 class MatchASTConsumer :
public ASTConsumer {
869 MatchASTConsumer(MatchFinder *Finder,
871 : Finder(Finder), ParsingDone(ParsingDone) {}
878 Finder->matchAST(Context);
890 : Nodes(Nodes), Context(Context),
897 : Options(std::move(Options)), ParsingDone(nullptr) {}
903 Matchers.DeclOrStmt.emplace_back(NodeMatch, Action);
904 Matchers.AllCallbacks.insert(Action);
909 Matchers.Type.emplace_back(NodeMatch, Action);
910 Matchers.AllCallbacks.insert(Action);
915 Matchers.DeclOrStmt.emplace_back(NodeMatch, Action);
916 Matchers.AllCallbacks.insert(Action);
921 Matchers.NestedNameSpecifier.emplace_back(NodeMatch, Action);
922 Matchers.AllCallbacks.insert(Action);
927 Matchers.NestedNameSpecifierLoc.emplace_back(NodeMatch, Action);
928 Matchers.AllCallbacks.insert(Action);
933 Matchers.TypeLoc.emplace_back(NodeMatch, Action);
934 Matchers.AllCallbacks.insert(Action);
939 if (NodeMatch.canConvertTo<
Decl>()) {
942 }
else if (NodeMatch.canConvertTo<
QualType>()) {
945 }
else if (NodeMatch.canConvertTo<
Stmt>()) {
954 }
else if (NodeMatch.canConvertTo<
TypeLoc>()) {
962 return llvm::make_unique<internal::MatchASTConsumer>(
this, ParsingDone);
967 internal::MatchASTVisitor Visitor(&Matchers, Options);
968 Visitor.set_active_ast_context(&Context);
973 internal::MatchASTVisitor Visitor(&Matchers, Options);
974 Visitor.set_active_ast_context(&Context);
975 Visitor.onStartOfTranslationUnit();
977 Visitor.onEndOfTranslationUnit();
982 ParsingDone = NewParsingDone;
Defines the clang::ASTContext interface.
const MatchFinder::MatchersByType * Matchers
A (possibly-)qualified type.
MatchFinder::ParsingDoneTestCallback * ParsingDone
internal::Matcher< Decl > DeclarationMatcher
Types of matchers for the top-level classes in the AST class hierarchy.
const DynTypedMatcher *const Matcher
Called when parsing is finished. Intended for testing only.
MatchFinder(MatchFinderOptions Options=MatchFinderOptions())
BoundNodesTreeBuilder Nodes
bool TraverseDecl(Decl *D)
Recursively visit a declaration, by dispatching to Traverse*Decl() based on the argument's dynamic ty...
Base wrapper for a particular "section" of type source info.
virtual ~ParsingDoneTestCallback()
void match(const T &Node, ASTContext &Context)
Calls the registered callbacks on all matches on the given Node.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
A C++ nested-name-specifier augmented with source location information.
const ASTMatchFinder::BindKind Bind
ASTContext * ActiveASTContext
void addMatcher(const DeclarationMatcher &NodeMatch, MatchCallback *Action)
Adds a matcher to execute when running over the AST.
SmallVector< BoundNodes, 1 > match(MatcherT Matcher, const NodeT &Node, ASTContext &Context)
Returns the results of matching Matcher on Node.
internal::Matcher< Stmt > StatementMatcher
detail::InMemoryDirectory::const_iterator I
BoundNodesTreeBuilder ResultBindings
DynTypedMatcher::MatcherIDType MatcherID
internal::Matcher< NestedNameSpecifierLoc > NestedNameSpecifierLocMatcher
virtual StringRef getID() const
An id used to group the matchers.
MatchFinder::MatchCallback * Callback
TranslationUnitDecl * getTranslationUnitDecl() const
llvm::DenseMap< ast_type_traits::ASTNodeKind, std::vector< unsigned short > > MatcherFiltersMap
Filtered list of matcher indices for each matcher kind.
void registerTestCallbackAfterParsing(ParsingDoneTestCallback *ParsingDone)
Registers a callback to notify the end of parsing.
MatchResult(const BoundNodes &Nodes, clang::ASTContext *Context)
The result type of a method or function.
std::unique_ptr< clang::ASTConsumer > newASTConsumer()
Creates a clang ASTConsumer that finds all matches.
The l-value was considered opaque, so the alignment was determined from a type.
Maps string IDs to AST nodes matched by parts of a matcher.
const MatchFinder::MatchFinderOptions & Options
ASTMatchFinder *const Finder
const TemplateArgument * iterator
bool TraverseTypeLoc(TypeLoc TL)
Recursively visit a type with location, by dispatching to Traverse*TypeLoc() based on the argument ty...
bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS)
Recursively visit a C++ nested-name-specifier with location information.
const ASTMatchFinder::TraversalKind Traversal
Represents a C++ nested name specifier, such as "\::std::vector<int>::".
internal::Matcher< TypeLoc > TypeLocMatcher
void matchAST(ASTContext &Context)
Finds all matches in the given AST.
bool TraverseStmt(Stmt *S, DataRecursionQueue *Queue=nullptr)
Recursively visit a statement or expression, by dispatching to Traverse*() based on the argument's dy...
bool operator<(DeclarationName LHS, DeclarationName RHS)
Ordering on two declaration names.
bool TraverseType(QualType T)
Recursively visit a type, by dispatching to Traverse*Type() based on the argument's getTypeClass() pr...
static DynTypedNode create(const T &Node)
Creates a DynTypedNode from Node.
BoundNodesTreeBuilder BoundNodes
ast_type_traits::DynTypedNode Node
internal::Matcher< NestedNameSpecifier > NestedNameSpecifierMatcher
detail::InMemoryDirectory::const_iterator E
A dynamically typed AST node container.
bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS)
Recursively visit a C++ nested-name-specifier.
MemoizationMap ResultCache
internal::Matcher< QualType > TypeMatcher
BoundNodesTreeBuilder *const Builder
llvm::DenseMap< const Type *, std::set< const TypedefNameDecl * > > TypeAliases
TemplateSpecializationType(TemplateName T, ArrayRef< TemplateArgument > Args, QualType Canon, QualType Aliased)
This class handles loading and caching of source files into memory.
bool addDynamicMatcher(const internal::DynTypedMatcher &NodeMatch, MatchCallback *Action)
Adds a matcher to execute when running over the AST.
llvm::StringMap< llvm::TimeRecord > TimeByBucket
Bucket to record map.
llvm::TimeRecord * Bucket