[PATCH] D60685: [clangd] Dont index Symbols with invalid source location

Kadir Cetinkaya via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Apr 15 04:22:40 PDT 2019


kadircet created this revision.
Herald added subscribers: cfe-commits, arphaman, jkorous, MaskRay, ioeric, ilya-biryukov.
Herald added a project: clang.

After rL358098 <https://reviews.llvm.org/rL358098> clangd started to index redecls of implicit symbols, but
sometimes these decls lack the source location information which results in
undefined behaviors in clangd.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D60685

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


Index: clang-tools-extra/unittests/clangd/SymbolCollectorTests.cpp
===================================================================
--- clang-tools-extra/unittests/clangd/SymbolCollectorTests.cpp
+++ 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
Index: clang-tools-extra/clangd/index/SymbolCollector.cpp
===================================================================
--- clang-tools-extra/clangd/index/SymbolCollector.cpp
+++ 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<NamedDecl>(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 =


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D60685.195129.patch
Type: text/x-patch
Size: 1576 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190415/0722367e/attachment.bin>


More information about the cfe-commits mailing list