[clang] 538677a - Add an API to simplify setting TraversalKind in clang-tidy matchers

Stephen Kelly via cfe-commits cfe-commits at lists.llvm.org
Fri Feb 5 06:04:05 PST 2021


Author: Stephen Kelly
Date: 2021-02-05T14:03:40Z
New Revision: 538677abbde4e74795d0bc21a77a3d263893c37d

URL: https://github.com/llvm/llvm-project/commit/538677abbde4e74795d0bc21a77a3d263893c37d
DIFF: https://github.com/llvm/llvm-project/commit/538677abbde4e74795d0bc21a77a3d263893c37d.diff

LOG: Add an API to simplify setting TraversalKind in clang-tidy matchers

Reviewers: sammccall

Subscribers: cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D80623

Added: 
    

Modified: 
    clang/include/clang/ASTMatchers/ASTMatchFinder.h
    clang/lib/ASTMatchers/ASTMatchFinder.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/ASTMatchers/ASTMatchFinder.h b/clang/include/clang/ASTMatchers/ASTMatchFinder.h
index 81125ad8d96d..91024f9425e0 100644
--- a/clang/include/clang/ASTMatchers/ASTMatchFinder.h
+++ b/clang/include/clang/ASTMatchers/ASTMatchFinder.h
@@ -110,6 +110,12 @@ class MatchFinder {
     /// This id is used, for example, for the profiling output.
     /// It defaults to "<unknown>".
     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<TraversalKind> getCheckTraversalKind() const;
   };
 
   /// Called when parsing is finished. Intended for testing only.
@@ -280,6 +286,11 @@ class CollectMatchesCallback : public MatchFinder::MatchCallback {
   void run(const MatchFinder::MatchResult &Result) override {
     Nodes.push_back(Result.Nodes);
   }
+
+  llvm::Optional<TraversalKind> getCheckTraversalKind() const override {
+    return llvm::None;
+  }
+
   SmallVector<BoundNodes, 1> Nodes;
 };
 }

diff  --git a/clang/lib/ASTMatchers/ASTMatchFinder.cpp b/clang/lib/ASTMatchers/ASTMatchFinder.cpp
index 58e69bb29df6..9be275a528eb 100644
--- a/clang/lib/ASTMatchers/ASTMatchFinder.cpp
+++ b/clang/lib/ASTMatchers/ASTMatchFinder.cpp
@@ -1036,6 +1036,7 @@ class MatchASTVisitor : public RecursiveASTVisitor<MatchASTVisitor>,
         Callback(Callback) {}
 
     void visitMatch(const BoundNodes& BoundNodesView) override {
+      TraversalKindScope RAII(*Context, Callback->getCheckTraversalKind());
       Callback->run(MatchFinder::MatchResult(BoundNodesView, Context));
     }
 
@@ -1335,7 +1336,10 @@ MatchFinder::~MatchFinder() {}
 
 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);
 }
 
@@ -1347,7 +1351,10 @@ void MatchFinder::addMatcher(const TypeMatcher &NodeMatch,
 
 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);
 }
 
@@ -1436,5 +1443,10 @@ void MatchFinder::registerTestCallbackAfterParsing(
 
 StringRef MatchFinder::MatchCallback::getID() const { return "<unknown>"; }
 
+llvm::Optional<TraversalKind>
+MatchFinder::MatchCallback::getCheckTraversalKind() const {
+  return llvm::None;
+}
+
 } // end namespace ast_matchers
 } // end namespace clang


        


More information about the cfe-commits mailing list