[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 03:36:50 PST 2022


This revision was automatically updated to reflect the committed changes.
kadircet marked an inline comment as done.
Closed by commit rG4764ee6055c7: [include-cleaner] Use RAV instead of ASTMatchers in LocateSymbolTest (authored by kadircet).

Changed prior to commit:
  https://reviews.llvm.org/D139093?vs=479224&id=479229#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D139093/new/

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,25 @@
         }()) {}
 
   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) {
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.479229.patch
Type: text/x-patch
Size: 3080 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221201/0f258a66/attachment-0001.bin>


More information about the cfe-commits mailing list