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) !=

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ class Dex : public SymbolIndex {
3636
public:
3737
// All data must outlive this index.
3838
template <typename SymbolRange, typename RefsRange, typename RelationsRange>
39-
Dex(SymbolRange &&Symbols, RefsRange &&Refs, RelationsRange &&Relations)
39+
Dex(SymbolRange &&Symbols, RefsRange &&Refs, RelationsRange &&Relations,
40+
bool SupportContainedRefs)
4041
: Corpus(0) {
4142
for (auto &&Sym : Symbols)
4243
this->Symbols.push_back(&Sym);
@@ -46,15 +47,15 @@ class Dex : public SymbolIndex {
4647
this->Relations[std::make_pair(Rel.Subject,
4748
static_cast<uint8_t>(Rel.Predicate))]
4849
.push_back(Rel.Object);
49-
buildIndex();
50+
buildIndex(SupportContainedRefs);
5051
}
5152
// Symbols and Refs are owned by BackingData, Index takes ownership.
5253
template <typename SymbolRange, typename RefsRange, typename RelationsRange,
5354
typename Payload>
5455
Dex(SymbolRange &&Symbols, RefsRange &&Refs, RelationsRange &&Relations,
55-
Payload &&BackingData, size_t BackingDataSize)
56+
Payload &&BackingData, size_t BackingDataSize, bool SupportContainedRefs)
5657
: Dex(std::forward<SymbolRange>(Symbols), std::forward<RefsRange>(Refs),
57-
std::forward<RelationsRange>(Relations)) {
58+
std::forward<RelationsRange>(Relations), SupportContainedRefs) {
5859
KeepAlive = std::shared_ptr<void>(
5960
std::make_shared<Payload>(std::move(BackingData)), nullptr);
6061
this->BackingDataSize = BackingDataSize;
@@ -64,16 +65,18 @@ class Dex : public SymbolIndex {
6465
typename FileRange, typename Payload>
6566
Dex(SymbolRange &&Symbols, RefsRange &&Refs, RelationsRange &&Relations,
6667
FileRange &&Files, IndexContents IdxContents, Payload &&BackingData,
67-
size_t BackingDataSize)
68+
size_t BackingDataSize, bool SupportContainedRefs)
6869
: Dex(std::forward<SymbolRange>(Symbols), std::forward<RefsRange>(Refs),
6970
std::forward<RelationsRange>(Relations),
70-
std::forward<Payload>(BackingData), BackingDataSize) {
71+
std::forward<Payload>(BackingData), BackingDataSize,
72+
SupportContainedRefs) {
7173
this->Files = std::forward<FileRange>(Files);
7274
this->IdxContents = IdxContents;
7375
}
7476

7577
/// Builds an index from slabs. The index takes ownership of the slab.
76-
static std::unique_ptr<SymbolIndex> build(SymbolSlab, RefSlab, RelationSlab);
78+
static std::unique_ptr<SymbolIndex> build(SymbolSlab, RefSlab, RelationSlab,
79+
bool SupportContainedRefs);
7780

7881
bool
7982
fuzzyFind(const FuzzyFindRequest &Req,
@@ -112,7 +115,7 @@ class Dex : public SymbolIndex {
112115
}
113116
};
114117

115-
void buildIndex();
118+
void buildIndex(bool EnableOutgoingCalls);
116119
llvm::iterator_range<std::vector<RevRef>::const_iterator>
117120
lookupRevRefs(const SymbolID &Container) const;
118121
std::unique_ptr<Iterator> iterator(const Token &Tok) const;

clang-tools-extra/clangd/index/dex/dexp/Dexp.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,8 @@ std::unique_ptr<SymbolIndex> openIndex(llvm::StringRef Index) {
375375
return Index.starts_with("remote:")
376376
? remote::getClient(Index.drop_front(strlen("remote:")),
377377
ProjectRoot)
378-
: loadIndex(Index, SymbolOrigin::Static, /*UseDex=*/true);
378+
: loadIndex(Index, SymbolOrigin::Static, /*UseDex=*/true,
379+
/*SupportContainedRefs=*/true);
379380
}
380381

381382
bool runCommand(std::string Request, const SymbolIndex &Index) {

clang-tools-extra/clangd/index/remote/server/Server.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,8 @@ void hotReload(clangd::SwapIndex &Index, llvm::StringRef IndexPath,
443443
LastStatus.getLastModificationTime(), Status->getLastModificationTime());
444444
LastStatus = *Status;
445445
std::unique_ptr<clang::clangd::SymbolIndex> NewIndex =
446-
loadIndex(IndexPath, SymbolOrigin::Static);
446+
loadIndex(IndexPath, SymbolOrigin::Static, /*UseDex=*/true,
447+
/*SupportContainedRefs=*/true);
447448
if (!NewIndex) {
448449
elog("Failed to load new index. Old index will be served.");
449450
return;
@@ -579,8 +580,9 @@ int main(int argc, char *argv[]) {
579580
return Status.getError().value();
580581
}
581582

582-
auto SymIndex =
583-
clang::clangd::loadIndex(IndexPath, clang::clangd::SymbolOrigin::Static);
583+
auto SymIndex = clang::clangd::loadIndex(
584+
IndexPath, clang::clangd::SymbolOrigin::Static, /*UseDex=*/true,
585+
/*SupportContainedRefs=*/true);
584586
if (!SymIndex) {
585587
llvm::errs() << "Failed to open the index.\n";
586588
return -1;

clang-tools-extra/clangd/tool/Check.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ class Checker {
163163
unsigned ErrCount = 0;
164164

165165
Checker(llvm::StringRef File, const ClangdLSPServer::Options &Opts)
166-
: File(File), Opts(Opts) {}
166+
: File(File), Opts(Opts), Index(/*SupportContainedRefs=*/true) {}
167167

168168
// Read compilation database and choose a compile command for the file.
169169
bool buildCommand(const ThreadsafeFS &TFS) {

clang-tools-extra/clangd/tool/ClangdMain.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ const char TestScheme::TestDir[] = "/clangd-test";
604604

605605
std::unique_ptr<SymbolIndex>
606606
loadExternalIndex(const Config::ExternalIndexSpec &External,
607-
AsyncTaskRunner *Tasks) {
607+
AsyncTaskRunner *Tasks, bool SupportContainedRefs) {
608608
static const trace::Metric RemoteIndexUsed("used_remote_index",
609609
trace::Metric::Value, "address");
610610
switch (External.Kind) {
@@ -620,8 +620,9 @@ loadExternalIndex(const Config::ExternalIndexSpec &External,
620620
External.Location);
621621
auto NewIndex = std::make_unique<SwapIndex>(std::make_unique<MemIndex>());
622622
auto IndexLoadTask = [File = External.Location,
623-
PlaceHolder = NewIndex.get()] {
624-
if (auto Idx = loadIndex(File, SymbolOrigin::Static, /*UseDex=*/true))
623+
PlaceHolder = NewIndex.get(), SupportContainedRefs] {
624+
if (auto Idx = loadIndex(File, SymbolOrigin::Static, /*UseDex=*/true,
625+
SupportContainedRefs))
625626
PlaceHolder->reset(std::move(Idx));
626627
};
627628
if (Tasks) {
@@ -909,7 +910,12 @@ clangd accepts flags on the commandline, and in the CLANGD_FLAGS environment var
909910
Opts.BackgroundIndexPriority = BackgroundIndexPriority;
910911
Opts.ReferencesLimit = ReferencesLimit;
911912
Opts.Rename.LimitFiles = RenameFileLimit;
912-
auto PAI = createProjectAwareIndex(loadExternalIndex, Sync);
913+
auto PAI = createProjectAwareIndex(
914+
[SupportContainedRefs = Opts.EnableOutgoingCalls](
915+
const Config::ExternalIndexSpec &External, AsyncTaskRunner *Tasks) {
916+
return loadExternalIndex(External, Tasks, SupportContainedRefs);
917+
},
918+
Sync);
913919
Opts.StaticIndex = PAI.get();
914920
Opts.AsyncThreadsCount = WorkerThreadsCount;
915921
Opts.MemoryCleanup = getMemoryCleanupFunction();

clang-tools-extra/clangd/unittests/BackgroundIndexTests.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,8 @@ TEST_F(BackgroundIndexTest, Reindex) {
685685
class BackgroundIndexRebuilderTest : public testing::Test {
686686
protected:
687687
BackgroundIndexRebuilderTest()
688-
: Source(IndexContents::All), Target(std::make_unique<MemIndex>()),
688+
: Source(IndexContents::All, /*SupportContainedRefs=*/true),
689+
Target(std::make_unique<MemIndex>()),
689690
Rebuilder(&Target, &Source, /*Threads=*/10) {
690691
// Prepare FileSymbols with TestSymbol in it, for checkRebuild.
691692
TestSymbol.ID = SymbolID("foo");

0 commit comments

Comments
 (0)