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

Kadir Cetinkaya via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 11 05:14:35 PST 2021


kadircet updated this revision to Diff 329930.
kadircet added a comment.

- Rebase
- Add a storage for returned verbatim header spellings, as getHeaderIncludeUncached can only return a ref.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D98242/new/

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
@@ -35,6 +35,7 @@
 #include "llvm/Support/FileSystem.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/Path.h"
+#include <list>
 
 namespace clang {
 namespace clangd {
@@ -167,6 +168,7 @@
   llvm::StringRef FallbackDir;
   llvm::DenseMap<const FileEntry *, const std::string *> CacheFEToURI;
   llvm::StringMap<std::string> CachePathToURI;
+  std::list<std::string> StorageForSystemHeaders;
   llvm::DenseMap<FileID, llvm::StringRef> CacheFIDToInclude;
 
 public:
@@ -250,6 +252,18 @@
       // Conservatively refuse to insert #includes to files without guards.
       return "";
     }
+    // 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) {
+      StorageForSystemHeaders.push_back("<" + ShorterInclude + ">");
+      return StorageForSystemHeaders.back();
+    }
     // Standard case: just insert the file itself.
     return toURI(FE);
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D98242.329930.patch
Type: text/x-patch
Size: 2698 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210311/00788f5d/attachment.bin>


More information about the cfe-commits mailing list