12#include "clang-include-cleaner/Record.h"
27#include "clang/AST/ASTContext.h"
28#include "clang/Index/IndexingAction.h"
29#include "clang/Index/IndexingOptions.h"
30#include "clang/Lex/Preprocessor.h"
31#include "llvm/ADT/DenseMap.h"
32#include "llvm/ADT/STLExtras.h"
33#include "llvm/ADT/StringMap.h"
34#include "llvm/ADT/StringRef.h"
47 llvm::ArrayRef<Decl *> DeclsToIndex,
49 const include_cleaner::PragmaIncludes &PI,
50 bool IsIndexMainAST, llvm::StringRef Version,
54 CollectorOpts.PragmaIncludes = &PI;
55 CollectorOpts.CountReferences =
false;
56 CollectorOpts.Origin = Origin;
57 CollectorOpts.CollectMainFileRefs = CollectMainFileRefs;
60 CollectorOpts.CollectReserved = IsIndexMainAST;
62 index::IndexingOptions IndexOpts;
64 IndexOpts.SystemSymbolFilter =
65 index::IndexingOptions::SystemSymbolFilterKind::DeclarationsOnly;
67 IndexOpts.IndexFunctionLocals =
true;
73 CollectorOpts.StoreAllDocumentation =
false;
75 IndexOpts.IndexMacrosInPreprocessor =
true;
76 CollectorOpts.CollectMacro =
true;
77 CollectorOpts.StoreAllDocumentation =
true;
81 Collector.setPreprocessor(PP);
82 index::indexTopLevelDecls(
AST, PP, DeclsToIndex, Collector,
83 std::move(IndexOpts));
85 Collector.handleMacros(*MacroRefsToIndex);
87 const auto &SM =
AST.getSourceManager();
88 const auto MainFileEntry = SM.getFileEntryRefForID(SM.getMainFileID());
89 std::string FileName =
90 std::string(MainFileEntry ? MainFileEntry->getName() :
"");
92 auto Syms = Collector.takeSymbols();
93 auto Refs = Collector.takeRefs();
94 auto Relations = Collector.takeRelations();
96 vlog(
"indexed {0} AST for {1} version {2}:\n"
97 " symbol slab: {3} symbols, {4} bytes\n"
98 " ref slab: {5} symbols, {6} refs, {7} bytes\n"
99 " relations slab: {8} relations, {9} bytes",
100 IsIndexMainAST ?
"file" :
"preamble", FileName, Version, Syms.size(),
101 Syms.bytes(), Refs.size(), Refs.numRefs(), Refs.bytes(),
102 Relations.size(), Relations.bytes());
103 return std::make_tuple(std::move(Syms), std::move(Refs),
104 std::move(Relations));
112 auto Entry = IG.try_emplace(
URI).first;
113 auto &Node = Entry->getValue();
114 Node = FullGraph.lookup(Entry->getKey());
115 Node.URI = Entry->getKey();
118 for (
auto &Include : Node.DirectIncludes) {
119 auto I = IG.try_emplace(Include).first;
120 I->getValue().URI = I->getKey();
121 Include = I->getKey();
128 : Index(std::
move(Input)) {
130 llvm::DenseMap<SymbolID, FileShard *> SymbolIDToFile;
134 for (const auto &S : *Index.Symbols) {
135 auto It = Shards.try_emplace(S.CanonicalDeclaration.FileURI);
136 It.first->getValue().Symbols.insert(&S);
137 SymbolIDToFile[S.ID] = &It.first->getValue();
140 S.Definition.FileURI != S.CanonicalDeclaration.FileURI) {
141 auto It = Shards.try_emplace(S.Definition.FileURI);
142 It.first->getValue().Symbols.insert(&S);
148 for (const auto &SymRefs : *Index.Refs) {
149 for (const auto &R : SymRefs.second) {
150 const auto It = Shards.try_emplace(R.Location.FileURI);
151 It.first->getValue().Refs.insert(&R);
152 RefToSymID[&R] = SymRefs.first;
162 if (Index.Relations) {
163 for (const auto &R : *Index.Relations) {
165 FileShard *SubjectFile = SymbolIDToFile.lookup(R.Subject);
166 FileShard *ObjectFile = SymbolIDToFile.lookup(R.Object);
168 SubjectFile->Relations.insert(&R);
169 if (ObjectFile && ObjectFile != SubjectFile)
170 ObjectFile->Relations.insert(&R);
175 const auto &FullGraph = *Index.Sources;
176 for (const auto &It : FullGraph) {
177 auto ShardIt = Shards.try_emplace(It.first());
178 ShardIt.first->getValue().IG = getSubGraph(It.first(), FullGraph);
185 std::vector<PathRef> Result;
186 Result.reserve(Shards.size());
187 for (
auto Key : Shards.keys())
188 Result.push_back(
Key);
192std::optional<IndexFileIn>
194 auto It = Shards.find(Uri);
195 if (It == Shards.end())
199 IF.
Sources = It->getValue().IG;
203 for (
const auto *S : It->getValue().Symbols)
205 IF.
Symbols = std::move(SymB).build();
208 for (
const auto *
Ref : It->getValue().Refs) {
209 auto SID = RefToSymID.lookup(
Ref);
212 IF.
Refs = std::move(RefB).build();
215 for (
const auto *Rel : It->getValue().Relations) {
220 return std::move(IF);
224 return indexSymbols(
AST.getASTContext(),
AST.getPreprocessor(),
225 AST.getLocalTopLevelDecls(), &
AST.getMacros(),
226 AST.getPragmaIncludes(),
233 const include_cleaner::PragmaIncludes &PI,
235 std::vector<Decl *> DeclsToIndex(
236 AST.getTranslationUnitDecl()->decls().begin(),
237 AST.getTranslationUnitDecl()->decls().end());
238 return indexSymbols(
AST, PP, DeclsToIndex,
245 : IdxContents(IdxContents), SupportContainedRefs(SupportContainedRefs) {}
248 std::unique_ptr<SymbolSlab>
Symbols,
249 std::unique_ptr<RefSlab> Refs,
250 std::unique_ptr<RelationSlab> Relations,
251 bool CountReferences) {
252 std::lock_guard<std::mutex> Lock(Mutex);
255 SymbolsSnapshot.erase(
Key);
259 RefsSnapshot.erase(
Key);
261 RefSlabAndCountReferences Item;
262 Item.CountReferences = CountReferences;
263 Item.Slab = std::move(Refs);
264 RefsSnapshot[
Key] = std::move(Item);
267 RelationsSnapshot.erase(
Key);
269 RelationsSnapshot[
Key] = std::move(Relations);
272std::unique_ptr<SymbolIndex>
275 std::vector<std::shared_ptr<SymbolSlab>> SymbolSlabs;
276 std::vector<std::shared_ptr<RefSlab>> RefSlabs;
277 std::vector<std::shared_ptr<RelationSlab>> RelationSlabs;
278 llvm::StringSet<> Files;
279 std::vector<RefSlab *> MainFileRefs;
281 std::lock_guard<std::mutex> Lock(Mutex);
282 for (
const auto &FileAndSymbols : SymbolsSnapshot) {
283 SymbolSlabs.push_back(FileAndSymbols.second);
284 Files.insert(FileAndSymbols.first());
286 for (
const auto &FileAndRefs : RefsSnapshot) {
287 RefSlabs.push_back(FileAndRefs.second.Slab);
288 Files.insert(FileAndRefs.first());
289 if (FileAndRefs.second.CountReferences)
290 MainFileRefs.push_back(RefSlabs.back().get());
292 for (
const auto &FileAndRelations : RelationsSnapshot) {
293 Files.insert(FileAndRelations.first());
294 RelationSlabs.push_back(FileAndRelations.second);
298 *Version = this->Version;
300 std::vector<const Symbol *> AllSymbols;
301 std::vector<Symbol> SymsStorage;
302 switch (DuplicateHandle) {
304 llvm::DenseMap<SymbolID, Symbol> Merged;
305 for (
const auto &Slab : SymbolSlabs) {
306 for (
const auto &Sym : *Slab) {
307 assert(Sym.References == 0 &&
308 "Symbol with non-zero references sent to FileSymbols");
309 auto I = Merged.try_emplace(Sym.ID, Sym);
311 I.first->second =
mergeSymbol(I.first->second, Sym);
314 for (
const RefSlab *Refs : MainFileRefs)
315 for (
const auto &Sym : *Refs) {
316 auto It = Merged.find(Sym.first);
318 if (It == Merged.end())
320 It->getSecond().References += Sym.second.size();
322 SymsStorage.reserve(Merged.size());
323 for (
auto &Sym : Merged) {
324 SymsStorage.push_back(std::move(Sym.second));
325 AllSymbols.push_back(&SymsStorage.back());
330 llvm::DenseSet<SymbolID> AddedSymbols;
331 for (
const auto &Slab : SymbolSlabs)
332 for (
const auto &Sym : *Slab) {
333 assert(Sym.References == 0 &&
334 "Symbol with non-zero references sent to FileSymbols");
335 if (AddedSymbols.insert(Sym.ID).second)
336 AllSymbols.push_back(&Sym);
342 std::vector<Ref> RefsStorage;
343 llvm::DenseMap<SymbolID, llvm::ArrayRef<Ref>> AllRefs;
345 llvm::DenseMap<SymbolID, llvm::SmallVector<Ref, 4>> MergedRefs;
347 for (
const auto &
RefSlab : RefSlabs)
348 for (
const auto &Sym : *
RefSlab) {
349 MergedRefs[Sym.first].append(Sym.second.begin(), Sym.second.end());
350 Count += Sym.second.size();
352 RefsStorage.reserve(Count);
353 AllRefs.reserve(MergedRefs.size());
354 for (
auto &Sym : MergedRefs) {
355 auto &SymRefs = Sym.second;
358 llvm::copy(SymRefs, back_inserter(RefsStorage));
361 llvm::ArrayRef<Ref>(&RefsStorage[RefsStorage.size() - SymRefs.size()],
366 std::vector<Relation> AllRelations;
369 AllRelations.push_back(R);
374 llvm::sort(AllRelations);
375 AllRelations.erase(llvm::unique(AllRelations), AllRelations.end());
378 RefsStorage.size() *
sizeof(
Ref) + SymsStorage.size() *
sizeof(
Symbol);
379 for (
const auto &Slab : SymbolSlabs)
380 StorageSize += Slab->bytes();
381 for (
const auto &
RefSlab : RefSlabs)
387 return std::make_unique<MemIndex>(
388 llvm::make_pointee_range(AllSymbols), std::move(AllRefs),
389 std::move(AllRelations), std::move(Files), IdxContents,
390 std::make_tuple(std::move(SymbolSlabs), std::move(RefSlabs),
391 std::move(RefsStorage), std::move(SymsStorage)),
394 return std::make_unique<dex::Dex>(
395 llvm::make_pointee_range(AllSymbols), std::move(AllRefs),
396 std::move(AllRelations), std::move(Files), IdxContents,
397 std::make_tuple(std::move(SymbolSlabs), std::move(RefSlabs),
398 std::move(RefsStorage), std::move(SymsStorage)),
399 StorageSize, SupportContainedRefs);
401 llvm_unreachable(
"Unknown clangd::IndexType");
405 std::lock_guard<std::mutex> Lock(Mutex);
406 for (
const auto &SymSlab : SymbolsSnapshot) {
407 MT.
detail(SymSlab.first())
411 for (
const auto &
RefSlab : RefsSnapshot) {
416 for (
const auto &RelSlab : RelationsSnapshot) {
417 MT.
detail(RelSlab.first())
426 SupportContainedRefs),
427 PreambleIndex(std::make_unique<
MemIndex>()),
429 MainFileIndex(std::make_unique<
MemIndex>()) {}
434 auto IF = ShardedIndex.
getShard(Uri);
438 PreambleSymbols.update(
439 Uri, std::make_unique<SymbolSlab>(std::move(*IF->
Symbols)),
440 std::make_unique<RefSlab>(),
441 std::make_unique<RelationSlab>(std::move(*IF->
Relations)),
444 size_t IndexVersion = 0;
445 auto NewIndex = PreambleSymbols.buildIndex(
448 std::lock_guard<std::mutex> Lock(UpdateIndexMu);
449 if (IndexVersion <= PreambleIndexVersion) {
453 PreambleIndexVersion = IndexVersion;
454 PreambleIndex.reset(std::move(NewIndex));
456 "Build dynamic index for header symbols with estimated memory usage of "
458 PreambleIndex.estimateMemoryUsage());
463 ASTContext &
AST, Preprocessor &PP,
464 const include_cleaner::PragmaIncludes &PI) {
473 MainFileSymbols.update(
475 std::make_unique<SymbolSlab>(std::move(std::get<0>(Contents))),
476 std::make_unique<RefSlab>(std::move(std::get<1>(Contents))),
477 std::make_unique<RelationSlab>(std::move(std::get<2>(Contents))),
479 size_t IndexVersion = 0;
480 auto NewIndex = MainFileSymbols.buildIndex(
483 std::lock_guard<std::mutex> Lock(UpdateIndexMu);
484 if (IndexVersion <= MainIndexVersion) {
488 MainIndexVersion = IndexVersion;
489 MainFileIndex.reset(std::move(NewIndex));
491 "Build dynamic index for main-file symbols with estimated memory usage "
493 MainFileIndex.estimateMemoryUsage());
498 PreambleSymbols.profile(MT.
child(
"preamble").
child(
"slabs"));
501 .
addUsage(PreambleIndex.estimateMemoryUsage());
502 MainFileSymbols.profile(MT.
child(
"main_file").
child(
"slabs"));
503 MT.
child(
"main_file")
505 .
addUsage(MainFileIndex.estimateMemoryUsage());
This defines Dex - a symbol index implementation based on query iterators over symbol tokens,...
void updatePreamble(PathRef Path, llvm::StringRef Version, ASTContext &AST, Preprocessor &PP, const include_cleaner::PragmaIncludes &PI)
Update preamble symbols of file Path with all declarations in AST and macros in PP.
FileIndex(bool SupportContainedRefs)
void profile(MemoryTree &MT) const
void updateMain(PathRef Path, ParsedAST &AST)
Update symbols and references from main file Path with indexMainDecls.
FileSymbols(IndexContents IdxContents, bool SupportContainedRefs)
void update(llvm::StringRef Key, std::unique_ptr< SymbolSlab > Symbols, std::unique_ptr< RefSlab > Refs, std::unique_ptr< RelationSlab > Relations, bool CountReferences)
Updates all slabs associated with the Key.
std::unique_ptr< SymbolIndex > buildIndex(IndexType, DuplicateHandling DuplicateHandle=DuplicateHandling::PickOne, size_t *Version=nullptr)
The index keeps the slabs alive.
void profile(MemoryTree &MT) const
Values in a Context are indexed by typed keys.
MemIndex is a naive in-memory index suitable for a small set of symbols.
MergedIndex(const SymbolIndex *Dynamic, const SymbolIndex *Static)
Stores and provides access to parsed AST.
RefSlab::Builder is a mutable container that can 'freeze' to RefSlab.
void insert(const SymbolID &ID, const Ref &S)
Adds a ref to the slab. Deep copy: Strings will be owned by the slab.
An efficient structure of storing large set of symbol references in memory.
RelationSlab::Builder is a mutable container that can 'freeze' to RelationSlab.
void insert(const Relation &R)
Adds a relation to the slab.
Collect declarations (symbols) from an AST.
SymbolSlab::Builder is a mutable container that can 'freeze' to SymbolSlab.
void insert(const Symbol &S)
Adds a symbol, overwriting any existing one with the same ID.
A URI describes the location of a source file.
static llvm::Expected< URI > create(llvm::StringRef AbsolutePath, llvm::StringRef Scheme)
Creates a URI for a file in the given scheme.
FIXME: Skip testing on windows temporarily due to the different escaping code mode.
IndexType
Select between in-memory index implementations, which have tradeoffs.
IndexContents
Describes what data is covered by an index.
Symbol mergeSymbol(const Symbol &L, const Symbol &R)
void vlog(const char *Fmt, Ts &&... Vals)
static const char * toString(OffsetEncoding OE)
SlabTuple indexMainDecls(ParsedAST &AST)
Retrieves symbols and refs of local top level decls in AST (i.e.
DuplicateHandling
How to handle duplicated symbols across multiple files.
SlabTuple indexHeaderSymbols(llvm::StringRef Version, ASTContext &AST, Preprocessor &PP, const include_cleaner::PragmaIncludes &PI, SymbolOrigin Origin)
Index declarations from AST and macros from PP that are declared in included headers.
llvm::StringMap< IncludeGraphNode > IncludeGraph
llvm::StringRef PathRef
A typedef to represent a ref to file path.
@ Type
An inlay hint that for a type annotation.
std::string Path
A typedef to represent a file path.
std::tuple< SymbolSlab, RefSlab, RelationSlab > SlabTuple
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Takes slabs coming from a TU (multiple files) and shards them per declaration location.
std::vector< llvm::StringRef > getAllSources() const
Returns uris for all files that has a shard.
std::optional< IndexFileIn > getShard(llvm::StringRef Uri) const
Generates index shard for the Uri.
FileShardedIndex(IndexFileIn Input)
HintPath is used to convert file URIs stored in symbols into absolute paths.
std::optional< RelationSlab > Relations
std::optional< SymbolSlab > Symbols
std::optional< RefSlab > Refs
std::optional< tooling::CompileCommand > Cmd
std::optional< IncludeGraph > Sources
A tree that can be used to represent memory usage of nested components while preserving the hierarchy...
void addUsage(size_t Increment)
Increases size of current node by Increment.
MemoryTree & child(llvm::StringLiteral Name)
No copy of the Name.
MemoryTree & detail(llvm::StringRef Name)
Makes a copy of the Name in detailed mode, returns current node otherwise.
Represents a symbol occurrence in the source file.
The class presents a C++ symbol, e.g.