[PATCH] D55275: [clangd] Dont provide locations for non-existent files.

Kadir Cetinkaya via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 5 04:00:22 PST 2018


This revision was automatically updated to reflect the committed changes.
Closed by commit rL348359: [clangd] Dont provide locations for non-existent files. (authored by kadircet, committed by ).
Herald added a subscriber: llvm-commits.

Repository:
  rL LLVM

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

https://reviews.llvm.org/D55275

Files:
  clang-tools-extra/trunk/clangd/AST.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
@@ -614,6 +614,18 @@
                 DeclURI(TestHeaderURI))));
 }
 
+TEST_F(SymbolCollectorTest, SymbolFormedByCLI) {
+  Annotations Header(R"(
+    #ifdef NAME
+    class $expansion[[NAME]] {};
+    #endif
+  )");
+  runSymbolCollector(Header.code(), /*Main=*/"", /*ExtraArgs=*/{"-DNAME=name"});
+  EXPECT_THAT(Symbols, UnorderedElementsAre(AllOf(
+                           QName("name"), DeclRange(Header.range("expansion")),
+                           DeclURI(TestHeaderURI))));
+}
+
 TEST_F(SymbolCollectorTest, IgnoreSymbolsInMainFile) {
   const std::string Header = R"(
     class Foo {};
Index: clang-tools-extra/trunk/clangd/AST.cpp
===================================================================
--- clang-tools-extra/trunk/clangd/AST.cpp
+++ clang-tools-extra/trunk/clangd/AST.cpp
@@ -23,9 +23,11 @@
 namespace clangd {
 
 // Returns true if the complete name of decl \p D is spelled in the source code.
-// This is not the case for symbols formed via macro concatenation.
-// (We used to attempt to treat names spelled on the command-line this way too,
-// but the preamble doesn't preserve the required information).
+// This is not the case for:
+//   * 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>".
 bool isSpelledInSourceCode(const Decl *D) {
   const auto &SM = D->getASTContext().getSourceManager();
   auto Loc = D->getLocation();
@@ -33,7 +35,8 @@
   // macros, we should use the location where the whole definition occurs.
   if (Loc.isMacroID()) {
     std::string PrintLoc = SM.getSpellingLoc(Loc).printToString(SM);
-    if (StringRef(PrintLoc).startswith("<scratch"))
+    if (StringRef(PrintLoc).startswith("<scratch") ||
+        StringRef(PrintLoc).startswith("<command line>"))
       return false;
   }
   return true;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D55275.176789.patch
Type: text/x-patch
Size: 2231 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20181205/bd1b53ab/attachment.bin>


More information about the llvm-commits mailing list