[clang-tools-extra] r324328 - [clangd] Fix incorrect file path for symbols defined by the compile command-line option.

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 6 01:50:35 PST 2018


Author: hokein
Date: Tue Feb  6 01:50:35 2018
New Revision: 324328

URL: http://llvm.org/viewvc/llvm-project?rev=324328&view=rev
Log:
[clangd] Fix incorrect file path for symbols defined by the compile command-line option.

Summary:

Reviewers: ioeric

Reviewed By: ioeric

Subscribers: klimek, ilya-biryukov, jkorous-apple, cfe-commits

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

Modified:
    clang-tools-extra/trunk/clangd/index/SymbolCollector.cpp
    clang-tools-extra/trunk/unittests/clangd/SymbolCollectorTests.cpp

Modified: clang-tools-extra/trunk/clangd/index/SymbolCollector.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/index/SymbolCollector.cpp?rev=324328&r1=324327&r2=324328&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/index/SymbolCollector.cpp (original)
+++ clang-tools-extra/trunk/clangd/index/SymbolCollector.cpp Tue Feb  6 01:50:35 2018
@@ -124,10 +124,15 @@ SymbolLocation GetSymbolLocation(const N
 
   SourceLocation Loc = SM.getSpellingLoc(D->getLocation());
   if (D->getLocation().isMacroID()) {
-    // The symbol is formed via macro concatenation, the spelling location will
-    // be "<scratch space>", which is not interesting to us, use the expansion
-    // location instead.
-    if (llvm::StringRef(Loc.printToString(SM)).startswith("<scratch")) {
+    // We use the expansion location for the following symbols, as spelling
+    // locations of these symbols are not interesting to us:
+    //   * symbols formed via macro concatenation, the spelling location will
+    //     be "<scratch space>"
+    //   * symbols controlled and defined by a compile command-line option
+    //     `-DName=foo`, the spelling location will be "<command line>".
+    std::string PrintLoc = Loc.printToString(SM);
+    if (llvm::StringRef(PrintLoc).startswith("<scratch") ||
+        llvm::StringRef(PrintLoc).startswith("<command line>")) {
       FilePathStorage = makeAbsolutePath(
           SM, SM.getFilename(SM.getExpansionLoc(D->getLocation())),
           FallbackDir);

Modified: clang-tools-extra/trunk/unittests/clangd/SymbolCollectorTests.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/unittests/clangd/SymbolCollectorTests.cpp?rev=324328&r1=324327&r2=324328&view=diff
==============================================================================
--- clang-tools-extra/trunk/unittests/clangd/SymbolCollectorTests.cpp (original)
+++ clang-tools-extra/trunk/unittests/clangd/SymbolCollectorTests.cpp Tue Feb  6 01:50:35 2018
@@ -82,7 +82,8 @@ public:
 
 class SymbolCollectorTest : public ::testing::Test {
 public:
-  bool runSymbolCollector(StringRef HeaderCode, StringRef MainCode) {
+  bool runSymbolCollector(StringRef HeaderCode, StringRef MainCode,
+                          const std::vector<std::string> &ExtraArgs = {}) {
     llvm::IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem(
         new vfs::InMemoryFileSystem);
     llvm::IntrusiveRefCntPtr<FileManager> Files(
@@ -90,8 +91,11 @@ public:
 
     auto Factory = llvm::make_unique<SymbolIndexActionFactory>(CollectorOpts);
 
+    std::vector<std::string> Args = {"symbol_collector", "-fsyntax-only",
+                                     "-std=c++11", TestFileName};
+    Args.insert(Args.end(), ExtraArgs.begin(), ExtraArgs.end());
     tooling::ToolInvocation Invocation(
-        {"symbol_collector", "-fsyntax-only", "-std=c++11", TestFileName},
+        Args,
         Factory->create(), Files.get(),
         std::make_shared<PCHContainerOperations>());
 
@@ -264,6 +268,24 @@ TEST_F(SymbolCollectorTest, SymbolFormed
                         CPath(TestFileName))));
 }
 
+TEST_F(SymbolCollectorTest, SymbolFormedByCLI) {
+  CollectorOpts.IndexMainFiles = false;
+
+  Annotations Header(R"(
+    #ifdef NAME
+    $expansion[[class NAME {}]];
+    #endif
+  )");
+
+  runSymbolCollector(Header.code(), /*Main=*/"",
+                     /*ExtraArgs=*/{"-DNAME=name"});
+  EXPECT_THAT(Symbols,
+              UnorderedElementsAre(
+                  AllOf(QName("name"),
+                        LocationOffsets(Header.offsetRange("expansion")),
+                        CPath(TestHeaderName))));
+}
+
 TEST_F(SymbolCollectorTest, IgnoreSymbolsInMainFile) {
   CollectorOpts.IndexMainFiles = false;
   const std::string Header = R"(




More information about the cfe-commits mailing list