[clang-tools-extra] 4764ee6 - [include-cleaner] Use RAV instead of ASTMatchers in LocateSymbolTest

Kadir Cetinkaya via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 1 03:36:43 PST 2022


Author: Kadir Cetinkaya
Date: 2022-12-01T12:35:34+01:00
New Revision: 4764ee6055c7ea1cd4818a2a5c496861c371660a

URL: https://github.com/llvm/llvm-project/commit/4764ee6055c7ea1cd4818a2a5c496861c371660a
DIFF: https://github.com/llvm/llvm-project/commit/4764ee6055c7ea1cd4818a2a5c496861c371660a.diff

LOG: [include-cleaner] Use RAV instead of ASTMatchers in LocateSymbolTest

ASTMatchers are pulling in lots of dependencies that we don't really
need for just finding a decl based on name. So use a simple RAV instead.

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

Added: 
    

Modified: 
    clang-tools-extra/include-cleaner/unittests/CMakeLists.txt
    clang-tools-extra/include-cleaner/unittests/LocateSymbolTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/include-cleaner/unittests/CMakeLists.txt b/clang-tools-extra/include-cleaner/unittests/CMakeLists.txt
index 11d93f526f3c..d911b5df70c5 100644
--- a/clang-tools-extra/include-cleaner/unittests/CMakeLists.txt
+++ b/clang-tools-extra/include-cleaner/unittests/CMakeLists.txt
@@ -1,5 +1,4 @@
 set(LLVM_LINK_COMPONENTS
-  FrontendOpenMP
   Support
   TestingSupport
   )
@@ -21,7 +20,6 @@ target_include_directories(ClangIncludeCleanerTests
 clang_target_link_libraries(ClangIncludeCleanerTests
   PRIVATE
   clangAST
-  clangASTMatchers
   clangBasic
   clangFrontend
   clangLex

diff  --git a/clang-tools-extra/include-cleaner/unittests/LocateSymbolTest.cpp b/clang-tools-extra/include-cleaner/unittests/LocateSymbolTest.cpp
index f347db927e27..7d0fd1b56e09 100644
--- a/clang-tools-extra/include-cleaner/unittests/LocateSymbolTest.cpp
+++ b/clang-tools-extra/include-cleaner/unittests/LocateSymbolTest.cpp
@@ -9,14 +9,12 @@
 #include "clang-include-cleaner/Types.h"
 #include "clang/AST/Decl.h"
 #include "clang/AST/DeclBase.h"
-#include "clang/ASTMatchers/ASTMatchFinder.h"
-#include "clang/ASTMatchers/ASTMatchers.h"
+#include "clang/AST/RecursiveASTVisitor.h"
 #include "clang/Basic/SourceLocation.h"
 #include "clang/Lex/Preprocessor.h"
 #include "clang/Testing/TestAST.h"
 #include "clang/Tooling/Inclusions/StandardLibrary.h"
 #include "llvm/ADT/StringRef.h"
-#include "llvm/Support/Casting.h"
 #include "llvm/Testing/Support/Annotations.h"
 #include "gmock/gmock.h"
 #include "gtest/gtest.h"
@@ -53,27 +51,25 @@ struct LocateExample {
         }()) {}
 
   const Decl &findDecl(llvm::StringRef SymbolName) {
-    const NamedDecl *DeclToLocate;
-    struct MatchCB : public ast_matchers::MatchFinder::MatchCallback {
-      MatchCB(const NamedDecl *&Out) : Out(Out) {}
-      void run(const ast_matchers::MatchFinder::MatchResult &Result) override {
-        Out = Result.Nodes.getNodeAs<NamedDecl>("id");
-        assert(Out);
-        Out = llvm::cast<NamedDecl>(Out->getCanonicalDecl());
+    struct Visitor : RecursiveASTVisitor<Visitor> {
+      llvm::StringRef NameToFind;
+      const NamedDecl *Out = nullptr;
+      bool VisitNamedDecl(const NamedDecl *ND) {
+        if (ND->getName() == NameToFind) {
+          EXPECT_TRUE(Out == nullptr || Out == ND->getCanonicalDecl())
+              << "Found multiple matches for " << NameToFind;
+          Out = cast<NamedDecl>(ND->getCanonicalDecl());
+        }
+        return true;
       }
-      const NamedDecl *&Out;
-    } CB(DeclToLocate);
-    ast_matchers::MatchFinder Finder;
-    Finder.addMatcher(ast_matchers::namedDecl(
-                          ast_matchers::unless(ast_matchers::isImplicit()),
-                          ast_matchers::hasName(SymbolName))
-                          .bind("id"),
-                      &CB);
-    Finder.matchAST(AST.context());
-    if (!DeclToLocate)
+    };
+    Visitor V;
+    V.NameToFind = SymbolName;
+    V.TraverseDecl(AST.context().getTranslationUnitDecl());
+    if (!V.Out)
       ADD_FAILURE() << "Couldn't find any decls with name: " << SymbolName;
-    assert(DeclToLocate);
-    return *DeclToLocate;
+    assert(V.Out);
+    return *V.Out;
   }
 
   Macro findMacro(llvm::StringRef Name) {


        


More information about the cfe-commits mailing list