VitaNuo updated this revision to Diff 483910.
VitaNuo added a comment.

Address review comments.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D139716

Files:
  clang-tools-extra/include-cleaner/lib/FindHeaders.cpp
  clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp

Index: clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp
===================================================================
--- clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp
+++ clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp
@@ -7,11 +7,17 @@
 //===----------------------------------------------------------------------===//
 
 #include "AnalysisInternal.h"
+#include "clang-include-cleaner/Analysis.h"
 #include "clang-include-cleaner/Record.h"
+#include "clang-include-cleaner/Types.h"
+#include "clang/AST/RecursiveASTVisitor.h"
 #include "clang/Basic/FileEntry.h"
 #include "clang/Basic/FileManager.h"
 #include "clang/Frontend/FrontendActions.h"
 #include "clang/Testing/TestAST.h"
+#include "llvm/ADT/ArrayRef.h"
+#include "llvm/Support/raw_ostream.h"
+#include "llvm/Testing/Support/Annotations.h"
 #include "gmock/gmock.h"
 #include "gtest/gtest.h"
 #include <memory>
@@ -31,7 +37,7 @@
   std::unique_ptr<TestAST> AST;
   FindHeadersTest() {
     Inputs.MakeAction = [this] {
-      struct Hook : public PreprocessOnlyAction {
+      struct Hook : public SyntaxOnlyAction {
       public:
         Hook(PragmaIncludes *Out) : Out(Out) {}
         bool BeginSourceFileAction(clang::CompilerInstance &CI) override {
@@ -153,5 +159,89 @@
                                    physicalHeader("exporter.h")));
 }
 
+struct CustomVisitor : RecursiveASTVisitor<CustomVisitor> {
+  const Decl *Out = nullptr;
+
+  bool VisitCXXRecordDecl(const CXXRecordDecl *ND) {
+    if (ND->getName() == "Foo") {
+      EXPECT_TRUE(Out == nullptr);
+      Out = ND;
+    }
+    return true;
+  }
+
+  bool VisitNamedDecl(const NamedDecl *ND) {
+    if (ND->getName() == "FLAG_foo") {
+      EXPECT_TRUE(Out == nullptr);
+      Out = ND;
+    }
+    return true;
+  }
+};
+
+TEST_F(FindHeadersTest, TargetIsExpandedFromMacroInHeader) {
+  struct {
+    llvm::StringRef Main;
+    llvm::StringRef DeclareHeader;
+    llvm::StringRef MacroHeader;
+  } TestCases[] = {{
+                       R"cpp(
+    #include "declare.h"
+    Foo foo;
+  )cpp",
+                       R"cpp(
+    #include "macro.h"
+    DEFINE_CLASS(Foo)
+  )cpp",
+                       R"cpp(
+    #define DEFINE_CLASS(name) class name {};
+  )cpp"},
+                   {
+                       R"cpp(
+    #include "declare.h"
+    void test(Foo foo);
+  )cpp",
+                       R"cpp(
+    #include "macro.h"
+    DEFINE_Foo
+  )cpp",
+                       R"cpp(
+    #define DEFINE_Foo class Foo {};
+  )cpp"},
+                   {
+                       R"cpp(
+    #include "declare.h"
+    void foo() {
+      FLAG_foo;
+    }
+  )cpp",
+                       R"cpp(
+    #include "macro.h"
+    DECLARE_FLAGS(foo);
+  )cpp",
+                       R"cpp(
+    #define DECLARE_FLAGS(name) extern int FLAG_##name
+  )cpp"}};
+
+  for (const auto &T : TestCases) {
+    llvm::Annotations MainFile(T.Main);
+    Inputs.Code = MainFile.code();
+    Inputs.ExtraFiles["declare.h"] = T.DeclareHeader;
+    Inputs.ExtraFiles["macro.h"] = T.MacroHeader;
+    buildAST();
+
+    CustomVisitor Visitor;
+    for (Decl *D : AST->context().getTranslationUnitDecl()->decls()) {
+      Visitor.TraverseDecl(D);
+    }
+
+    const Decl *Foo = Visitor.Out;
+    llvm::SmallVector<Header> Headers = clang::include_cleaner::findHeaders(
+        Foo->getLocation(), AST->sourceManager(), nullptr);
+    EXPECT_EQ(Headers.size(), 1u);
+    EXPECT_THAT(Headers, UnorderedElementsAre(physicalHeader("declare.h")));
+  }
+}
+
 } // namespace
 } // namespace clang::include_cleaner
Index: clang-tools-extra/include-cleaner/lib/FindHeaders.cpp
===================================================================
--- clang-tools-extra/include-cleaner/lib/FindHeaders.cpp
+++ clang-tools-extra/include-cleaner/lib/FindHeaders.cpp
@@ -18,8 +18,7 @@
   llvm::SmallVector<Header> Results;
   switch (Loc.kind()) {
   case SymbolLocation::Physical: {
-    // FIXME: Handle macro locations.
-    FileID FID = SM.getFileID(Loc.physical());
+    FileID FID = SM.getFileID(SM.getExpansionLoc(Loc.physical()));
     const FileEntry *FE = SM.getFileEntryForID(FID);
     if (!PI) {
       return FE ? llvm::SmallVector<Header>{Header(FE)}
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to