[PATCH] D98329: [clangd] Add cache for FID to Header mappings

Kadir Cetinkaya via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Mar 10 03:10:49 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.

Depends on D98242 <https://reviews.llvm.org/D98242>


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D98329

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


Index: clang-tools-extra/clangd/index/SymbolCollector.h
===================================================================
--- clang-tools-extra/clangd/index/SymbolCollector.h
+++ clang-tools-extra/clangd/index/SymbolCollector.h
@@ -22,6 +22,7 @@
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/Support/Regex.h"
 #include <functional>
+#include <string>
 
 namespace clang {
 namespace clangd {
@@ -142,6 +143,9 @@
   // File IDs for Symbol.IncludeHeaders.
   // The final spelling is calculated in finish().
   llvm::DenseMap<SymbolID, FileID> IncludeFiles;
+  // Caches FileID to header mappings, values are either verbatim headers or
+  // URIs. None in case of a bad header, e.g. not include-guarded.
+  llvm::DenseMap<FileID, llvm::Optional<std::string>> HeaderToInclude;
   void setIncludeLocation(const Symbol &S, SourceLocation);
   // Indexed macros, to be erased if they turned out to be include guards.
   llvm::DenseSet<const IdentifierInfo *> IndexedMacros;
Index: clang-tools-extra/clangd/index/SymbolCollector.cpp
===================================================================
--- clang-tools-extra/clangd/index/SymbolCollector.cpp
+++ clang-tools-extra/clangd/index/SymbolCollector.cpp
@@ -781,13 +781,17 @@
     if (Canonical != Filename)
       return toURI(SM, Canonical, Opts);
   }
+  auto FIDMapping = HeaderToInclude.find(FID);
+  if (FIDMapping != HeaderToInclude.end())
+    return FIDMapping->second;
   if (!isSelfContainedHeader(FID)) {
     // A .inc or .def file is often included into a real header to define
     // symbols (e.g. LLVM tablegen files).
     if (Filename.endswith(".inc") || Filename.endswith(".def"))
-      return getIncludeHeader(S, SM.getFileID(SM.getIncludeLoc(FID)));
+      return HeaderToInclude[FID] =
+                 getIncludeHeader(S, SM.getFileID(SM.getIncludeLoc(FID)));
     // Conservatively refuse to insert #includes to files without guards.
-    return llvm::None;
+    return HeaderToInclude[FID] = 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>
@@ -798,9 +802,9 @@
       PP->getHeaderSearchInfo().suggestPathToFileForDiagnostics(FE, "",
                                                                 &IsSystem);
   if (IsSystem)
-    return "<" + ShorterInclude + ">";
+    return HeaderToInclude[FID] = "<" + ShorterInclude + ">";
   // Standard case: just insert the file itself.
-  return toURI(SM, Filename, Opts);
+  return HeaderToInclude[FID] = toURI(SM, Filename, Opts);
 }
 
 bool SymbolCollector::isSelfContainedHeader(FileID FID) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D98329.329587.patch
Type: text/x-patch
Size: 2648 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210310/f298c113/attachment-0001.bin>


More information about the cfe-commits mailing list