[PATCH] D42913: [clangd] Fix incorrect file path for symbols defined by the compile command-line option.

Haojian Wu via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 6 01:52:49 PST 2018


This revision was automatically updated to reflect the committed changes.
Closed by commit rL324328: [clangd] Fix incorrect file path for symbols defined by the compile command… (authored by hokein, committed by ).
Herald added a subscriber: llvm-commits.

Repository:
  rL LLVM

https://reviews.llvm.org/D42913

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


Index: clang-tools-extra/trunk/unittests/clangd/SymbolCollectorTests.cpp
===================================================================
--- clang-tools-extra/trunk/unittests/clangd/SymbolCollectorTests.cpp
+++ clang-tools-extra/trunk/unittests/clangd/SymbolCollectorTests.cpp
@@ -82,16 +82,20 @@
 
 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(
         new FileManager(FileSystemOptions(), InMemoryFileSystem));
 
     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 @@
                         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"(
Index: clang-tools-extra/trunk/clangd/index/SymbolCollector.cpp
===================================================================
--- clang-tools-extra/trunk/clangd/index/SymbolCollector.cpp
+++ clang-tools-extra/trunk/clangd/index/SymbolCollector.cpp
@@ -124,10 +124,15 @@
 
   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);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D42913.132957.patch
Type: text/x-patch
Size: 3426 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180206/cd50c28c/attachment.bin>


More information about the llvm-commits mailing list