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

Kadir Cetinkaya via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 1 02:58:25 PST 2022


kadircet created this revision.
kadircet added a reviewer: sammccall.
Herald added a project: All.
kadircet requested review of this revision.
Herald added a reviewer: jdoerfert.
Herald added subscribers: cfe-commits, sstefan1.
Herald added a project: clang-tools-extra.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D139093

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


Index: clang-tools-extra/include-cleaner/unittests/LocateSymbolTest.cpp
===================================================================
--- clang-tools-extra/include-cleaner/unittests/LocateSymbolTest.cpp
+++ 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,23 @@
         }()) {}
 
   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)
+          return true;
+        Out = ND;
+        return false;
       }
-      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) {
Index: clang-tools-extra/include-cleaner/unittests/CMakeLists.txt
===================================================================
--- clang-tools-extra/include-cleaner/unittests/CMakeLists.txt
+++ clang-tools-extra/include-cleaner/unittests/CMakeLists.txt
@@ -1,5 +1,4 @@
 set(LLVM_LINK_COMPONENTS
-  FrontendOpenMP
   Support
   TestingSupport
   )
@@ -21,7 +20,6 @@
 clang_target_link_libraries(ClangIncludeCleanerTests
   PRIVATE
   clangAST
-  clangASTMatchers
   clangBasic
   clangFrontend
   clangLex


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D139093.479224.patch
Type: text/x-patch
Size: 2918 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221201/3938c1b4/attachment-0001.bin>


More information about the cfe-commits mailing list