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 @@ -99,7 +99,9 @@ /// Clangd supports only a small subset of ClangTidyOptions, these options /// (Checks, CheckOptions) are about which clang-tidy checks will be /// enabled. - tidy::ClangTidyOptionsProvider *ClangTidyOptProvider = nullptr; + std::function + GetClangTidyOptions; /// Clangd's workspace root. Relevant for "workspace" operations not bound /// to a particular file. @@ -278,8 +280,10 @@ // Storage for merged views of the various indexes. std::vector> MergedIdx; - // The provider used to provide a clang-tidy option for a specific file. - tidy::ClangTidyOptionsProvider *ClangTidyOptProvider = nullptr; + // When set, provides clang-tidy options for a specific file. + std::function + GetClangTidyOptions; // If this is true, suggest include insertion fixes for diagnostic errors that // can be caused by missing includes (e.g. member access in incomplete type). 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 @@ -90,7 +90,7 @@ DynamicIdx(Opts.BuildDynamicSymbolIndex ? new FileIndex(Opts.HeavyweightDynamicSymbolIndex) : nullptr), - ClangTidyOptProvider(Opts.ClangTidyOptProvider), + GetClangTidyOptions(Opts.GetClangTidyOptions), SuggestMissingIncludes(Opts.SuggestMissingIncludes), WorkspaceRoot(Opts.WorkspaceRoot), // Pass a callback into `WorkScheduler` to extract symbols from a newly @@ -126,15 +126,18 @@ void ClangdServer::addDocument(PathRef File, llvm::StringRef Contents, WantDiagnostics WantDiags) { + auto FS = FSProvider.getFileSystem(); + ParseOptions Opts; Opts.ClangTidyOpts = tidy::ClangTidyOptions::getDefaults(); - if (ClangTidyOptProvider) - Opts.ClangTidyOpts = ClangTidyOptProvider->getOptions(File); + // FIXME: call on the worker thread when GetClangTidyOptions is thread-safe. + if (GetClangTidyOptions) + Opts.ClangTidyOpts = GetClangTidyOptions(*FS, File); Opts.SuggestMissingIncludes = SuggestMissingIncludes; // Compile command is set asynchronously during update, as it can be slow. ParseInputs Inputs; - Inputs.FS = FSProvider.getFileSystem(); + Inputs.FS = FS; Inputs.Contents = Contents; Inputs.Opts = std::move(Opts); Inputs.Index = Index; 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 @@ -18,6 +18,7 @@ #include "clang/Basic/Version.h" #include "clang/Format/Format.h" #include "llvm/ADT/Optional.h" +#include "llvm/ADT/StringRef.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/Path.h" @@ -505,7 +506,11 @@ /* Default */ tidy::ClangTidyOptions::getDefaults(), /* Override */ OverrideClangTidyOptions, FSProvider.getFileSystem()); } - Opts.ClangTidyOptProvider = ClangTidyOptProvider.get(); + Opts.GetClangTidyOptions = [&](llvm::vfs::FileSystem &, + llvm::StringRef File) { + // FIXME: use the FS provided to the function. + return ClangTidyOptProvider->getOptions(File); + }; Opts.SuggestMissingIncludes = SuggestMissingIncludes; llvm::Optional OffsetEncodingFromFlag; if (ForceOffsetEncoding != OffsetEncoding::UnsupportedEncoding)