diff --git a/clang/include/clang/ASTMatchers/ASTMatchFinder.h b/clang/include/clang/ASTMatchers/ASTMatchFinder.h --- a/clang/include/clang/ASTMatchers/ASTMatchFinder.h +++ b/clang/include/clang/ASTMatchers/ASTMatchFinder.h @@ -110,6 +110,13 @@ /// This id is used, for example, for the profiling output. /// It defaults to "". virtual StringRef getID() const; + + /// TraversalKind to use while matching and processing + /// the result nodes. This API is temporary to facilitate + /// third parties porting existing code to the default + /// behavior of clang-tidy. + virtual llvm::Optional + getCheckTraversalKind() const; }; /// Called when parsing is finished. Intended for testing only. diff --git a/clang/lib/ASTMatchers/ASTMatchFinder.cpp b/clang/lib/ASTMatchers/ASTMatchFinder.cpp --- a/clang/lib/ASTMatchers/ASTMatchFinder.cpp +++ b/clang/lib/ASTMatchers/ASTMatchFinder.cpp @@ -877,6 +877,7 @@ Callback(Callback) {} void visitMatch(const BoundNodes& BoundNodesView) override { + TraversalKindScope RAII(*Context, Callback->getCheckTraversalKind()); Callback->run(MatchFinder::MatchResult(BoundNodesView, Context)); } @@ -1142,7 +1143,11 @@ void MatchFinder::addMatcher(const DeclarationMatcher &NodeMatch, MatchCallback *Action) { - Matchers.DeclOrStmt.emplace_back(NodeMatch, Action); + if (auto TK = Action->getCheckTraversalKind()) { + Matchers.DeclOrStmt.emplace_back(traverse(*TK, NodeMatch), Action); + } else { + Matchers.DeclOrStmt.emplace_back(NodeMatch, Action); + } Matchers.AllCallbacks.insert(Action); } @@ -1154,7 +1159,11 @@ void MatchFinder::addMatcher(const StatementMatcher &NodeMatch, MatchCallback *Action) { - Matchers.DeclOrStmt.emplace_back(NodeMatch, Action); + if (auto TK = Action->getCheckTraversalKind()) { + Matchers.DeclOrStmt.emplace_back(traverse(*TK, NodeMatch), Action); + } else { + Matchers.DeclOrStmt.emplace_back(NodeMatch, Action); + } Matchers.AllCallbacks.insert(Action); } @@ -1243,5 +1252,10 @@ StringRef MatchFinder::MatchCallback::getID() const { return ""; } +llvm::Optional +MatchFinder::MatchCallback::getCheckTraversalKind() const { + return llvm::None; +} + } // end namespace ast_matchers } // end namespace clang