diff --git a/clang-tools-extra/clangd/ClangdServer.h b/clang-tools-extra/clangd/ClangdServer.h --- a/clang-tools-extra/clangd/ClangdServer.h +++ b/clang-tools-extra/clangd/ClangdServer.h @@ -156,6 +156,9 @@ /// Enable preview of FoldingRanges feature. bool FoldingRanges = false; + /// If true, use the dirty buffer contents when building Preambles. + bool UseDirtyPreambles = false; + explicit operator TUScheduler::Options() const; }; // Sensible default options for use in tests. @@ -358,6 +361,10 @@ ArrayRef Ranges, Callback CB); + const ThreadsafeFS &getPreambleFS() const { + return UseDirtyPreambles ? DirtyFS : TFS; + } + /// Derives a context for a task processing the specified source file. /// This includes the current configuration (see Options::ConfigProvider). /// The empty string means no particular file is the target. @@ -399,6 +406,8 @@ // If true, preserve the type for recovery AST. bool PreserveRecoveryASTType = false; + bool UseDirtyPreambles = false; + // GUARDED_BY(CachedCompletionFuzzyFindRequestMutex) llvm::StringMap> CachedCompletionFuzzyFindRequestByFile; diff --git a/clang-tools-extra/clangd/ClangdServer.cpp b/clang-tools-extra/clangd/ClangdServer.cpp --- a/clang-tools-extra/clangd/ClangdServer.cpp +++ b/clang-tools-extra/clangd/ClangdServer.cpp @@ -149,6 +149,7 @@ SuggestMissingIncludes(Opts.SuggestMissingIncludes), BuildRecoveryAST(Opts.BuildRecoveryAST), PreserveRecoveryASTType(Opts.PreserveRecoveryASTType), + UseDirtyPreambles(Opts.UseDirtyPreambles), WorkspaceRoot(Opts.WorkspaceRoot), // Pass a callback into `WorkScheduler` to extract symbols from a newly // parsed file and rebuild the file index synchronously each time an AST @@ -209,7 +210,7 @@ // Compile command is set asynchronously during update, as it can be slow. ParseInputs Inputs; - Inputs.TFS = &TFS; + Inputs.TFS = &getPreambleFS(); Inputs.Contents = std::string(Contents); Inputs.Version = Version.str(); Inputs.ForceRebuild = ForceRebuild; @@ -253,7 +254,7 @@ SpecFuzzyFind->CachedReq = CachedCompletionFuzzyFindRequestByFile[File]; } } - ParseInputs ParseInput{IP->Command, &TFS, IP->Contents.str()}; + ParseInputs ParseInput{IP->Command, &getPreambleFS(), IP->Contents.str()}; ParseInput.Index = Index; ParseInput.Opts.BuildRecoveryAST = BuildRecoveryAST; ParseInput.Opts.PreserveRecoveryASTType = PreserveRecoveryASTType; @@ -300,7 +301,7 @@ if (!PreambleData) return CB(error("Failed to parse includes")); - ParseInputs ParseInput{IP->Command, &TFS, IP->Contents.str()}; + ParseInputs ParseInput{IP->Command, &getPreambleFS(), IP->Contents.str()}; ParseInput.Index = Index; ParseInput.Opts.BuildRecoveryAST = BuildRecoveryAST; ParseInput.Opts.PreserveRecoveryASTType = PreserveRecoveryASTType; diff --git a/clang-tools-extra/clangd/tool/ClangdMain.cpp b/clang-tools-extra/clangd/tool/ClangdMain.cpp --- a/clang-tools-extra/clangd/tool/ClangdMain.cpp +++ b/clang-tools-extra/clangd/tool/ClangdMain.cpp @@ -493,6 +493,12 @@ init(ClangdServer::Options().CollectMainFileRefs), }; +opt UseDirtyPreambles{"use-dirty-preambles", cat(Misc), + desc("Use files open in the editor when building " + "pre-ambles instead of reading from the disk"), + Hidden, + init(ClangdServer::Options().UseDirtyPreambles)}; + #if defined(__GLIBC__) && CLANGD_MALLOC_TRIM opt EnableMallocTrim{ "malloc-trim", @@ -873,6 +879,7 @@ Opts.ClangTidyProvider = ClangTidyOptProvider; } Opts.AsyncPreambleBuilds = AsyncPreamble; + Opts.UseDirtyPreambles = UseDirtyPreambles; Opts.SuggestMissingIncludes = SuggestMissingIncludes; Opts.QueryDriverGlobs = std::move(QueryDriverGlobs); Opts.TweakFilter = [&](const Tweak &T) {