[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
llvm-svn: 348359
diff --git a/clang-tools-extra/clangd/AST.cpp b/clang-tools-extra/clangd/AST.cpp
index c5e34ef..dea4567 100644
--- a/clang-tools-extra/clangd/AST.cpp
+++ b/clang-tools-extra/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;