Skip to content

Commit 875ad76

Browse files
Add a ClangdServer option to allow disabling outgoing calls
1 parent d4ea024 commit 875ad76

22 files changed

+130
-94
lines changed

clang-tools-extra/clangd/ClangdLSPServer.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1699,7 +1699,8 @@ void ClangdLSPServer::bindMethods(LSPBinder &Bind,
16991699
Bind.method("typeHierarchy/subtypes", this, &ClangdLSPServer::onSubTypes);
17001700
Bind.method("textDocument/prepareCallHierarchy", this, &ClangdLSPServer::onPrepareCallHierarchy);
17011701
Bind.method("callHierarchy/incomingCalls", this, &ClangdLSPServer::onCallHierarchyIncomingCalls);
1702-
Bind.method("callHierarchy/outgoingCalls", this, &ClangdLSPServer::onCallHierarchyOutgoingCalls);
1702+
if (Opts.EnableOutgoingCalls)
1703+
Bind.method("callHierarchy/outgoingCalls", this, &ClangdLSPServer::onCallHierarchyOutgoingCalls);
17031704
Bind.method("textDocument/selectionRange", this, &ClangdLSPServer::onSelectionRange);
17041705
Bind.method("textDocument/documentLink", this, &ClangdLSPServer::onDocumentLink);
17051706
Bind.method("textDocument/semanticTokens/full", this, &ClangdLSPServer::onSemanticTokens);

clang-tools-extra/clangd/ClangdServer.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,9 @@ ClangdServer::ClangdServer(const GlobalCompilationDatabase &CDB,
215215
const ThreadsafeFS &TFS, const Options &Opts,
216216
Callbacks *Callbacks)
217217
: FeatureModules(Opts.FeatureModules), CDB(CDB), TFS(TFS),
218-
DynamicIdx(Opts.BuildDynamicSymbolIndex ? new FileIndex() : nullptr),
218+
DynamicIdx(Opts.BuildDynamicSymbolIndex
219+
? new FileIndex(Opts.EnableOutgoingCalls)
220+
: nullptr),
219221
ModulesManager(Opts.ModulesManager),
220222
ClangTidyProvider(Opts.ClangTidyProvider),
221223
UseDirtyHeaders(Opts.UseDirtyHeaders),
@@ -256,6 +258,7 @@ ClangdServer::ClangdServer(const GlobalCompilationDatabase &CDB,
256258
Callbacks->onBackgroundIndexProgress(S);
257259
};
258260
BGOpts.ContextProvider = Opts.ContextProvider;
261+
BGOpts.SupportContainedRefs = Opts.EnableOutgoingCalls;
259262
BackgroundIdx = std::make_unique<BackgroundIndex>(
260263
TFS, CDB,
261264
BackgroundIndexStorage::createDiskBackedStorageFactory(

clang-tools-extra/clangd/ClangdServer.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ class ClangdServer {
110110
/// Cached preambles are potentially large. If false, store them on disk.
111111
bool StorePreamblesInMemory = true;
112112

113+
/// Call hierarchy's outgoing calls feature requires additional index
114+
/// serving structures which increase memory usage. If false, these are
115+
/// not created and the feature is not enabled.
116+
bool EnableOutgoingCalls = true;
117+
113118
/// This throttler controls which preambles may be built at a given time.
114119
clangd::PreambleThrottler *PreambleThrottler = nullptr;
115120

clang-tools-extra/clangd/index/Background.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ BackgroundIndex::BackgroundIndex(
9696
: SwapIndex(std::make_unique<MemIndex>()), TFS(TFS), CDB(CDB),
9797
IndexingPriority(Opts.IndexingPriority),
9898
ContextProvider(std::move(Opts.ContextProvider)),
99-
IndexedSymbols(IndexContents::All),
99+
IndexedSymbols(IndexContents::All, Opts.SupportContainedRefs),
100100
Rebuilder(this, &IndexedSymbols, Opts.ThreadPoolSize),
101101
IndexStorageFactory(std::move(IndexStorageFactory)),
102102
Queue(std::move(Opts.OnProgress)),

clang-tools-extra/clangd/index/Background.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ class BackgroundIndex : public SwapIndex {
145145
// file. Called with the empty string for other tasks.
146146
// (When called, the context from BackgroundIndex construction is active).
147147
std::function<Context(PathRef)> ContextProvider = nullptr;
148+
// Whether the index needs to support the containedRefs() operation.
149+
// May use extra memory.
150+
bool SupportContainedRefs = true;
148151
};
149152

150153
/// Creates a new background index and starts its threads.

clang-tools-extra/clangd/index/FileIndex.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,8 @@ SlabTuple indexHeaderSymbols(llvm::StringRef Version, ASTContext &AST,
239239
/*CollectMainFileRefs=*/false);
240240
}
241241

242-
FileSymbols::FileSymbols(IndexContents IdxContents)
243-
: IdxContents(IdxContents) {}
242+
FileSymbols::FileSymbols(IndexContents IdxContents, bool SupportContainedRefs)
243+
: IdxContents(IdxContents), SupportContainedRefs(SupportContainedRefs) {}
244244

245245
void FileSymbols::update(llvm::StringRef Key,
246246
std::unique_ptr<SymbolSlab> Symbols,
@@ -395,7 +395,7 @@ FileSymbols::buildIndex(IndexType Type, DuplicateHandling DuplicateHandle,
395395
std::move(AllRelations), std::move(Files), IdxContents,
396396
std::make_tuple(std::move(SymbolSlabs), std::move(RefSlabs),
397397
std::move(RefsStorage), std::move(SymsStorage)),
398-
StorageSize);
398+
StorageSize, SupportContainedRefs);
399399
}
400400
llvm_unreachable("Unknown clangd::IndexType");
401401
}
@@ -419,11 +419,12 @@ void FileSymbols::profile(MemoryTree &MT) const {
419419
}
420420
}
421421

422-
FileIndex::FileIndex()
422+
FileIndex::FileIndex(bool SupportContainedRefs)
423423
: MergedIndex(&MainFileIndex, &PreambleIndex),
424-
PreambleSymbols(IndexContents::Symbols | IndexContents::Relations),
424+
PreambleSymbols(IndexContents::Symbols | IndexContents::Relations,
425+
SupportContainedRefs),
425426
PreambleIndex(std::make_unique<MemIndex>()),
426-
MainFileSymbols(IndexContents::All),
427+
MainFileSymbols(IndexContents::All, SupportContainedRefs),
427428
MainFileIndex(std::make_unique<MemIndex>()) {}
428429

429430
void FileIndex::updatePreamble(IndexFileIn IF) {

clang-tools-extra/clangd/index/FileIndex.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ enum class DuplicateHandling {
6969
/// locking when we swap or obtain references to snapshots.
7070
class FileSymbols {
7171
public:
72-
FileSymbols(IndexContents IdxContents);
72+
FileSymbols(IndexContents IdxContents, bool SupportContainedRefs);
7373
/// Updates all slabs associated with the \p Key.
7474
/// If either is nullptr, corresponding data for \p Key will be removed.
7575
/// If CountReferences is true, \p Refs will be used for counting references
@@ -91,6 +91,7 @@ class FileSymbols {
9191

9292
private:
9393
IndexContents IdxContents;
94+
bool SupportContainedRefs;
9495

9596
struct RefSlabAndCountReferences {
9697
std::shared_ptr<RefSlab> Slab;
@@ -108,7 +109,7 @@ class FileSymbols {
108109
/// FIXME: Expose an interface to remove files that are closed.
109110
class FileIndex : public MergedIndex {
110111
public:
111-
FileIndex();
112+
FileIndex(bool SupportContainedRefs);
112113

113114
/// Update preamble symbols of file \p Path with all declarations in \p AST
114115
/// and macros in \p PP.

clang-tools-extra/clangd/index/Serialization.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,8 @@ llvm::Expected<IndexFileIn> readIndexFile(llvm::StringRef Data,
704704
}
705705

706706
std::unique_ptr<SymbolIndex> loadIndex(llvm::StringRef SymbolFilename,
707-
SymbolOrigin Origin, bool UseDex) {
707+
SymbolOrigin Origin, bool UseDex,
708+
bool SupportContainedRefs) {
708709
trace::Span OverallTracer("LoadIndex");
709710
auto Buffer = llvm::MemoryBuffer::getFile(SymbolFilename);
710711
if (!Buffer) {
@@ -735,10 +736,11 @@ std::unique_ptr<SymbolIndex> loadIndex(llvm::StringRef SymbolFilename,
735736
size_t NumRelations = Relations.size();
736737

737738
trace::Span Tracer("BuildIndex");
738-
auto Index = UseDex ? dex::Dex::build(std::move(Symbols), std::move(Refs),
739-
std::move(Relations))
740-
: MemIndex::build(std::move(Symbols), std::move(Refs),
741-
std::move(Relations));
739+
auto Index = UseDex
740+
? dex::Dex::build(std::move(Symbols), std::move(Refs),
741+
std::move(Relations), SupportContainedRefs)
742+
: MemIndex::build(std::move(Symbols), std::move(Refs),
743+
std::move(Relations));
742744
vlog("Loaded {0} from {1} with estimated memory usage {2} bytes\n"
743745
" - number of symbols: {3}\n"
744746
" - number of refs: {4}\n"

clang-tools-extra/clangd/index/Serialization.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ std::string toYAML(const Ref &);
8383
// Build an in-memory static index from an index file.
8484
// The size should be relatively small, so data can be managed in memory.
8585
std::unique_ptr<SymbolIndex> loadIndex(llvm::StringRef Filename,
86-
SymbolOrigin Origin, bool UseDex = true);
86+
SymbolOrigin Origin, bool UseDex,
87+
bool SupportContainedRefs);
8788

8889
} // namespace clangd
8990
} // namespace clang

clang-tools-extra/clangd/index/dex/Dex.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,14 @@ namespace clangd {
3333
namespace dex {
3434

3535
std::unique_ptr<SymbolIndex> Dex::build(SymbolSlab Symbols, RefSlab Refs,
36-
RelationSlab Rels) {
36+
RelationSlab Rels,
37+
bool SupportContainedRefs) {
3738
auto Size = Symbols.bytes() + Refs.bytes();
3839
// There is no need to include "Rels" in Data because the relations are self-
3940
// contained, without references into a backing store.
4041
auto Data = std::make_pair(std::move(Symbols), std::move(Refs));
4142
return std::make_unique<Dex>(Data.first, Data.second, Rels, std::move(Data),
42-
Size);
43+
Size, SupportContainedRefs);
4344
}
4445

4546
namespace {
@@ -120,7 +121,7 @@ class IndexBuilder {
120121

121122
} // namespace
122123

123-
void Dex::buildIndex() {
124+
void Dex::buildIndex(bool SupportContainedRefs) {
124125
this->Corpus = dex::Corpus(Symbols.size());
125126
std::vector<std::pair<float, const Symbol *>> ScoredSymbols(Symbols.size());
126127

@@ -148,7 +149,10 @@ void Dex::buildIndex() {
148149
Builder.add(*Symbols[SymbolRank], SymbolRank);
149150
InvertedIndex = std::move(Builder).build();
150151

151-
// Build RevRefs
152+
// If the containedRefs() operation is supported, build the RevRefs
153+
// data structure used to implement it.
154+
if (!SupportContainedRefs)
155+
return;
152156
for (const auto &[ID, RefList] : Refs)
153157
for (const auto &R : RefList)
154158
if ((R.Kind & ContainedRefsRequest::SupportedRefKinds) !=

0 commit comments

Comments
 (0)