Author: Haojian Wu
Date: 2019-11-15T16:28:10+01:00
New Revision: b221c9d09dd12bde75f00f3541c8f344925d4d59

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

LOG: [clangd] Replace getLangOpts().isHeaderFile usage with isHeaderFile helper.

Summary:
The helper is more correct to detect header file, this would fix our
issues caused by false positive before.

Reviewers: sammccall

Reviewed By: sammccall

Subscribers: merge_guards_bot, ilya-biryukov, MaskRay, jkorous, arphaman, 
kadircet, usaxena95, cfe-commits

Tags: #clang

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

Added: 
    

Modified: 
    clang-tools-extra/clangd/HeaderSourceSwitch.cpp
    clang-tools-extra/clangd/index/SymbolCollector.cpp
    clang-tools-extra/clangd/refactor/Rename.cpp
    clang-tools-extra/clangd/unittests/FindSymbolsTests.cpp
    clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clangd/HeaderSourceSwitch.cpp 
b/clang-tools-extra/clangd/HeaderSourceSwitch.cpp
index 06ad71d29b27..698f2460fea5 100644
--- a/clang-tools-extra/clangd/HeaderSourceSwitch.cpp
+++ b/clang-tools-extra/clangd/HeaderSourceSwitch.cpp
@@ -9,6 +9,7 @@
 #include "HeaderSourceSwitch.h"
 #include "AST.h"
 #include "Logger.h"
+#include "SourceCode.h"
 #include "index/SymbolCollector.h"
 #include "clang/AST/Decl.h"
 
@@ -96,7 +97,7 @@ llvm::Optional<Path> getCorrespondingHeaderOrSource(const 
Path &OriginalFile,
   //
   // For each symbol in the original file, we get its target location (decl or
   // def) from the index, then award that target file.
-  bool IsHeader = AST.getASTContext().getLangOpts().IsHeaderFile;
+  bool IsHeader = isHeaderFile(OriginalFile, 
AST.getASTContext().getLangOpts());
   Index->lookup(Request, [&](const Symbol &Sym) {
     if (IsHeader)
       AwardTarget(Sym.Definition.FileURI);

diff  --git a/clang-tools-extra/clangd/index/SymbolCollector.cpp 
b/clang-tools-extra/clangd/index/SymbolCollector.cpp
index 06fe854b2e0b..00adbd84fd62 100644
--- a/clang-tools-extra/clangd/index/SymbolCollector.cpp
+++ b/clang-tools-extra/clangd/index/SymbolCollector.cpp
@@ -304,7 +304,8 @@ bool SymbolCollector::handleDeclOccurence(
   // it's main-file only.
   bool IsMainFileOnly =
       SM.isWrittenInMainFile(SM.getExpansionLoc(ND->getBeginLoc())) &&
-      !ASTCtx->getLangOpts().IsHeaderFile;
+      !isHeaderFile(SM.getFileEntryForID(SM.getMainFileID())->getName(),
+                    ASTCtx->getLangOpts());
   // In C, printf is a redecl of an implicit builtin! So check OrigD instead.
   if (ASTNode.OrigD->isImplicit() ||
       !shouldCollectSymbol(*ND, *ASTCtx, Opts, IsMainFileOnly))

diff  --git a/clang-tools-extra/clangd/refactor/Rename.cpp 
b/clang-tools-extra/clangd/refactor/Rename.cpp
index a594f7a78102..3969f3e2e4e2 100644
--- a/clang-tools-extra/clangd/refactor/Rename.cpp
+++ b/clang-tools-extra/clangd/refactor/Rename.cpp
@@ -76,7 +76,7 @@ llvm::Optional<ReasonToReject> renamableWithinFile(const Decl 
&RenameDecl,
   }
   auto &ASTCtx = RenameDecl.getASTContext();
   const auto &SM = ASTCtx.getSourceManager();
-  bool MainFileIsHeader = ASTCtx.getLangOpts().IsHeaderFile;
+  bool MainFileIsHeader = isHeaderFile(MainFile, ASTCtx.getLangOpts());
   bool DeclaredInMainFile = isInsideMainFile(RenameDecl.getBeginLoc(), SM);
 
   if (!DeclaredInMainFile)

diff  --git a/clang-tools-extra/clangd/unittests/FindSymbolsTests.cpp 
b/clang-tools-extra/clangd/unittests/FindSymbolsTests.cpp
index beaf8df6a75b..8d4633626340 100644
--- a/clang-tools-extra/clangd/unittests/FindSymbolsTests.cpp
+++ b/clang-tools-extra/clangd/unittests/FindSymbolsTests.cpp
@@ -178,14 +178,11 @@ TEST_F(WorkspaceSymbolsTest, Namespaces) {
 }
 
 TEST_F(WorkspaceSymbolsTest, AnonymousNamespace) {
-  addFile("foo.h", R"cpp(
+  addFile("foo.cpp", R"cpp(
       namespace {
       void test() {}
       }
       )cpp");
-  addFile("foo.cpp", R"cpp(
-      #include "foo.h"
-      )cpp");
   EXPECT_THAT(getSymbols("test"), ElementsAre(QName("test")));
 }
 

diff  --git a/clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp 
b/clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
index 798c5bbcd4e6..d737862fa046 100644
--- a/clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
+++ b/clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
@@ -664,6 +664,17 @@ TEST_F(SymbolCollectorTest, HeaderAsMainFile) {
   runSymbolCollector("", Header.code(),
                      /*ExtraArgs=*/{"-xobjective-c++-header"});
   EXPECT_THAT(Symbols, UnorderedElementsAre(QName("Foo"), QName("Func")));
+  EXPECT_THAT(Refs,
+              UnorderedElementsAre(Pair(findSymbol(Symbols, "Foo").ID,
+                                        HaveRanges(Header.ranges("Foo"))),
+                                   Pair(findSymbol(Symbols, "Func").ID,
+                                        HaveRanges(Header.ranges("Func")))));
+
+  // Run the .hh file as main file (without "-x c++-header"), we should collect
+  // the refs as well.
+  TestFileName = testPath("foo.hh");
+  runSymbolCollector("", Header.code());
+  EXPECT_THAT(Symbols, UnorderedElementsAre(QName("Foo"), QName("Func")));
   EXPECT_THAT(Refs, UnorderedElementsAre(Pair(findSymbol(Symbols, "Foo").ID,
                                   HaveRanges(Header.ranges("Foo"))),
                              Pair(findSymbol(Symbols, "Func").ID,


        
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to