[clang-tools-extra] r348359 - [clangd] Dont provide locations for non-existent files.

Kadir Cetinkaya via cfe-commits cfe-commits at lists.llvm.org
Wed Dec 5 03:57:15 PST 2018


Author: kadircet
Date: Wed Dec  5 03:57:15 2018
New Revision: 348359

URL: http://llvm.org/viewvc/llvm-project?rev=348359&view=rev
Log:
[clangd] Dont provide locations for non-existent files.

Summary:
We were getting assertion errors when we had bad file names, instead we
should skip those.

Reviewers: hokein

Subscribers: ilya-biryukov, ioeric, MaskRay, jkorous, arphaman, cfe-commits

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

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

Modified: clang-tools-extra/trunk/clangd/AST.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/AST.cpp?rev=348359&r1=348358&r2=348359&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/AST.cpp (original)
+++ clang-tools-extra/trunk/clangd/AST.cpp Wed Dec  5 03:57:15 2018
@@ -23,9 +23,11 @@ namespace clang {
 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 @@ bool isSpelledInSourceCode(const Decl *D
   // 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;

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=348359&r1=348358&r2=348359&view=diff
==============================================================================
--- clang-tools-extra/trunk/unittests/clangd/SymbolCollectorTests.cpp (original)
+++ clang-tools-extra/trunk/unittests/clangd/SymbolCollectorTests.cpp Wed Dec  5 03:57:15 2018
@@ -614,6 +614,18 @@ TEST_F(SymbolCollectorTest, SymbolFormed
                 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 {};




More information about the cfe-commits mailing list