Index: clangd/ClangdLSPServer.h =================================================================== --- clangd/ClangdLSPServer.h +++ clangd/ClangdLSPServer.h @@ -35,7 +35,7 @@ /// for compile_commands.json in all parent directories of each file. ClangdLSPServer(JSONOutput &Out, const clangd::CodeCompleteOptions &CCOpts, llvm::Optional CompileCommandsDir, - const ClangdServer::Options &Opts); + const ClangdServer::Options &Opts, bool CacheCompileCommands); /// Run LSP server loop, receiving input for it from \p In. \p In must be /// opened in binary mode. Output will be written using Out variable passed to @@ -101,7 +101,8 @@ // Various ClangdServer parameters go here. It's important they're created // before ClangdServer. DirectoryBasedGlobalCompilationDatabase NonCachedCDB; - CachingCompilationDb CDB; + // Can be null if no caching was requested. + std::unique_ptr CachedCDB; RealFileSystemProvider FSProvider; /// Options used for code completion Index: clangd/ClangdLSPServer.cpp =================================================================== --- clangd/ClangdLSPServer.cpp +++ clangd/ClangdLSPServer.cpp @@ -12,6 +12,7 @@ #include "JSONRPCDispatcher.h" #include "SourceCode.h" #include "URI.h" +#include "llvm/ADT/STLExtras.h" #include "llvm/Support/Errc.h" #include "llvm/Support/FormatVariadic.h" #include "llvm/Support/Path.h" @@ -133,7 +134,8 @@ if (Params.metadata && !Params.metadata->extraFlags.empty()) { NonCachedCDB.setExtraFlagsForFile(File, std::move(Params.metadata->extraFlags)); - CDB.invalidate(File); + if (CachedCDB) + CachedCDB->invalidate(File); } std::string &Contents = Params.textDocument.text; @@ -157,7 +159,8 @@ // fail rather than giving wrong results. DraftMgr.removeDraft(File); Server.removeDocument(File); - CDB.invalidate(File); + if (CachedCDB) + CachedCDB->invalidate(File); log(llvm::toString(Contents.takeError())); return; } @@ -388,7 +391,8 @@ if (Settings.compilationDatabasePath.hasValue()) { NonCachedCDB.setCompileCommandsDir( Settings.compilationDatabasePath.getValue()); - CDB.clear(); + if (CachedCDB) + CachedCDB->clear(); reparseOpenedFiles(); } @@ -397,10 +401,15 @@ ClangdLSPServer::ClangdLSPServer(JSONOutput &Out, const clangd::CodeCompleteOptions &CCOpts, llvm::Optional CompileCommandsDir, - const ClangdServer::Options &Opts) - : Out(Out), NonCachedCDB(std::move(CompileCommandsDir)), CDB(NonCachedCDB), + const ClangdServer::Options &Opts, + bool CacheCompileCommands) + : Out(Out), NonCachedCDB(std::move(CompileCommandsDir)), + CachedCDB(CacheCompileCommands + ? llvm::make_unique(NonCachedCDB) + : nullptr), CCOpts(CCOpts), SupportedSymbolKinds(defaultSymbolKinds()), - Server(CDB, FSProvider, /*DiagConsumer=*/*this, Opts) {} + Server(CachedCDB ? (GlobalCompilationDatabase&) *CachedCDB : NonCachedCDB, FSProvider, + /*DiagConsumer=*/*this, Opts) {} bool ClangdLSPServer::run(std::FILE *In, JSONStreamStyle InputStyle) { assert(!IsDone && "Run was called before"); Index: clangd/tool/ClangdMain.cpp =================================================================== --- clangd/tool/ClangdMain.cpp +++ clangd/tool/ClangdMain.cpp @@ -134,6 +134,12 @@ "eventually. Don't rely on it."), llvm::cl::init(""), llvm::cl::Hidden); +static llvm::cl::opt CacheCompilationArgs( + "cache-compilation-args", + llvm::cl::desc("When true, clangd will cache compilation arguments that " + "come from the compilation databases."), + llvm::cl::init(false), llvm::cl::Hidden); + int main(int argc, char *argv[]) { llvm::sys::PrintStackTraceOnErrorSignal(argv[0]); llvm::cl::ParseCommandLineOptions(argc, argv, "clangd"); @@ -235,7 +241,8 @@ CCOpts.Limit = LimitResults; // Initialize and run ClangdLSPServer. - ClangdLSPServer LSPServer(Out, CCOpts, CompileCommandsDirPath, Opts); + ClangdLSPServer LSPServer(Out, CCOpts, CompileCommandsDirPath, Opts, + CacheCompilationArgs); constexpr int NoShutdownRequestErrorCode = 1; llvm::set_thread_name("clangd.main"); // Change stdin to binary to not lose \r\n on windows.