Skip to content

Commit 8b88ff0

Browse files
committed
[clangd] Add option to use dirty file contents when building preambles.
Adds a option `use-dirty-preambles` to enable using unsaved in editor contents when building pre-ambles. This enables a more seamless user experience when switching between header and implementation files and forgetting to save inbetween. It's also in line with the LSP spec that states open files in the editor should be used instead of on the contents on disk - https://microsoft.github.io/language-server-protocol/overviews/lsp/overview/ For now the option is defaulted to off and hidden, Though I have a feeling it should be moved into the `.clangd` config and possibly defaulted to true. Addresses clangd/clangd#488 Reviewed By: sammccall Differential Revision: https://reviews.llvm.org/D95046
1 parent ca2ac2b commit 8b88ff0

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

clang-tools-extra/clangd/ClangdServer.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ ClangdServer::ClangdServer(const GlobalCompilationDatabase &CDB,
156156
: FeatureModules(Opts.FeatureModules), CDB(CDB), TFS(TFS),
157157
DynamicIdx(Opts.BuildDynamicSymbolIndex ? new FileIndex() : nullptr),
158158
ClangTidyProvider(Opts.ClangTidyProvider),
159-
WorkspaceRoot(Opts.WorkspaceRoot),
159+
UseDirtyHeaders(Opts.UseDirtyHeaders), WorkspaceRoot(Opts.WorkspaceRoot),
160160
Transient(Opts.ImplicitCancellation ? TUScheduler::InvalidateOnUpdate
161161
: TUScheduler::NoInvalidation),
162162
DirtyFS(std::make_unique<DraftStoreFS>(TFS, DraftMgr)) {
@@ -228,7 +228,7 @@ void ClangdServer::addDocument(PathRef File, llvm::StringRef Contents,
228228

229229
// Compile command is set asynchronously during update, as it can be slow.
230230
ParseInputs Inputs;
231-
Inputs.TFS = &TFS;
231+
Inputs.TFS = &getHeaderFS();
232232
Inputs.Contents = std::string(Contents);
233233
Inputs.Version = std::move(ActualVersion);
234234
Inputs.ForceRebuild = ForceRebuild;
@@ -368,7 +368,7 @@ void ClangdServer::codeComplete(PathRef File, Position Pos,
368368
SpecFuzzyFind->CachedReq = CachedCompletionFuzzyFindRequestByFile[File];
369369
}
370370
}
371-
ParseInputs ParseInput{IP->Command, &TFS, IP->Contents.str()};
371+
ParseInputs ParseInput{IP->Command, &getHeaderFS(), IP->Contents.str()};
372372
// FIXME: Add traling new line if there is none at eof, workaround a crash,
373373
// see https://github.com/clangd/clangd/issues/332
374374
if (!IP->Contents.endswith("\n"))
@@ -420,7 +420,7 @@ void ClangdServer::signatureHelp(PathRef File, Position Pos,
420420
if (!PreambleData)
421421
return CB(error("Failed to parse includes"));
422422

423-
ParseInputs ParseInput{IP->Command, &TFS, IP->Contents.str()};
423+
ParseInputs ParseInput{IP->Command, &getHeaderFS(), IP->Contents.str()};
424424
// FIXME: Add traling new line if there is none at eof, workaround a crash,
425425
// see https://github.com/clangd/clangd/issues/332
426426
if (!IP->Contents.endswith("\n"))

clang-tools-extra/clangd/ClangdServer.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ class ClangdServer {
165165
bool FoldingRanges = false;
166166

167167
FeatureModuleSet *FeatureModules = nullptr;
168+
/// If true, use the dirty buffer contents when building Preambles.
169+
bool UseDirtyHeaders = false;
168170

169171
explicit operator TUScheduler::Options() const;
170172
};
@@ -394,6 +396,9 @@ class ClangdServer {
394396
private:
395397
FeatureModuleSet *FeatureModules;
396398
const GlobalCompilationDatabase &CDB;
399+
const ThreadsafeFS &getHeaderFS() const {
400+
return UseDirtyHeaders ? *DirtyFS : TFS;
401+
}
397402
const ThreadsafeFS &TFS;
398403

399404
Path ResourceDir;
@@ -413,6 +418,8 @@ class ClangdServer {
413418
// When set, provides clang-tidy options for a specific file.
414419
TidyProviderRef ClangTidyProvider;
415420

421+
bool UseDirtyHeaders = false;
422+
416423
// GUARDED_BY(CachedCompletionFuzzyFindRequestMutex)
417424
llvm::StringMap<llvm::Optional<FuzzyFindRequest>>
418425
CachedCompletionFuzzyFindRequestByFile;

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,12 @@ opt<bool> EnableConfig{
492492
init(true),
493493
};
494494

495+
opt<bool> UseDirtyHeaders{"use-dirty-headers", cat(Misc),
496+
desc("Use files open in the editor when parsing "
497+
"headers instead of reading from the disk"),
498+
Hidden,
499+
init(ClangdServer::Options().UseDirtyHeaders)};
500+
495501
#if defined(__GLIBC__) && CLANGD_MALLOC_TRIM
496502
opt<bool> EnableMallocTrim{
497503
"malloc-trim",
@@ -928,6 +934,7 @@ clangd accepts flags on the commandline, and in the CLANGD_FLAGS environment var
928934
ClangTidyOptProvider = combine(std::move(Providers));
929935
Opts.ClangTidyProvider = ClangTidyOptProvider;
930936
}
937+
Opts.UseDirtyHeaders = UseDirtyHeaders;
931938
Opts.QueryDriverGlobs = std::move(QueryDriverGlobs);
932939
Opts.TweakFilter = [&](const Tweak &T) {
933940
if (T.hidden() && !HiddenFeatures)

0 commit comments

Comments
 (0)