[PATCH] D98242: [clangd] Store system relative includes as verbatim

Kadir Cetinkaya via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 9 02:36:18 PST 2021


kadircet created this revision.
kadircet added a reviewer: sammccall.
Herald added subscribers: usaxena95, arphaman.
kadircet requested review of this revision.
Herald added subscribers: cfe-commits, MaskRay, ilya-biryukov.
Herald added a project: clang.

This makes our index more portable between different systems, e.g. we
can index a codebase with a --sysroot and make use of it on a system
that has a different abs path for those includes.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D98242

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


Index: clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
+++ clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
@@ -1446,6 +1446,20 @@
               UnorderedElementsAre(IncludeHeaderWithRef(TestHeaderURI, 1u)));
 }
 
+TEST_F(SymbolCollectorTest, IncludeHeaderFromSystemIsVerbatim) {
+  llvm::StringRef HeaderName = "header.h";
+  TestHeaderName = testPath(HeaderName);
+  TestHeaderURI = URI::create(TestHeaderName).toString();
+  CollectorOpts.CollectIncludePath = true;
+  runSymbolCollector("#pragma once\nclass Foo {};", /*Main=*/"",
+                     {"-isystem", testRoot()});
+  EXPECT_THAT(Symbols, UnorderedElementsAre(
+                           AllOf(QName("Foo"), DeclURI(TestHeaderURI))));
+  EXPECT_THAT(Symbols.begin()->IncludeHeaders,
+              UnorderedElementsAre(
+                  IncludeHeaderWithRef("<" + HeaderName.str() + ">", 1u)));
+}
+
 TEST_F(SymbolCollectorTest, CanonicalSTLHeader) {
   CollectorOpts.CollectIncludePath = true;
   CanonicalIncludes Includes;
Index: clang-tools-extra/clangd/index/SymbolCollector.cpp
===================================================================
--- clang-tools-extra/clangd/index/SymbolCollector.cpp
+++ clang-tools-extra/clangd/index/SymbolCollector.cpp
@@ -789,6 +789,16 @@
     // Conservatively refuse to insert #includes to files without guards.
     return llvm::None;
   }
+  // Store system includes as verbatim. This enables making use of the same
+  // index in different environments, e.g. a system header like <linux/dlm.h>
+  // might resolve to different absolute paths, but the path relative to sysroot
+  // will be the same.
+  bool IsSystem = false;
+  auto ShorterInclude =
+      PP->getHeaderSearchInfo().suggestPathToFileForDiagnostics(FE, "",
+                                                                &IsSystem);
+  if (IsSystem)
+    return "<" + ShorterInclude + ">";
   // Standard case: just insert the file itself.
   return toURI(SM, Filename, Opts);
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D98242.329262.patch
Type: text/x-patch
Size: 2125 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210309/0c5c7f6e/attachment.bin>


More information about the cfe-commits mailing list