diff --git a/clang-tools-extra/clangd/index/SymbolCollector.cpp b/clang-tools-extra/clangd/index/SymbolCollector.cpp --- a/clang-tools-extra/clangd/index/SymbolCollector.cpp +++ b/clang-tools-extra/clangd/index/SymbolCollector.cpp @@ -302,7 +302,7 @@ if (D->getFriendObjectKind() != Decl::FriendObjectKind::FOK_None) D = CanonicalDecls.try_emplace(D, ASTNode.OrigD).first->second; const NamedDecl *ND = dyn_cast(D); - if (!ND) + if (!ND || ND->getLocation().isInvalid()) return true; // Mark D as referenced if this is a reference coming from the main file. @@ -540,6 +540,7 @@ S.SymInfo = index::getSymbolInfo(&ND); std::string FileURI; auto Loc = findNameLoc(&ND); + assert(Loc.isValid() && "Invalid source location for NamedDecl"); // FIXME: use the result to filter out symbols. shouldIndexFile(SM, SM.getFileID(Loc), Opts, &FilesToIndexCache); if (auto DeclLoc = diff --git a/clang-tools-extra/unittests/clangd/SymbolCollectorTests.cpp b/clang-tools-extra/unittests/clangd/SymbolCollectorTests.cpp --- a/clang-tools-extra/unittests/clangd/SymbolCollectorTests.cpp +++ b/clang-tools-extra/unittests/clangd/SymbolCollectorTests.cpp @@ -1241,6 +1241,14 @@ EXPECT_THAT(Symbols, Contains(QName("printf"))); } +TEST_F(SymbolCollectorTest, InvalidSourceLoc) { + const char *Header = R"( + void operator delete(void*) + __attribute__((__externally_visible__));)"; + runSymbolCollector(Header, /**/ ""); + EXPECT_THAT(Symbols, IsEmpty()); +} + } // namespace } // namespace clangd } // namespace clang