Index: clangd/ClangdLSPServer.h =================================================================== --- clangd/ClangdLSPServer.h +++ clangd/ClangdLSPServer.h @@ -129,10 +129,6 @@ void setExtraFlagsForFile(PathRef File, std::vector ExtraFlags); - /// Set the compile commands directory to \p P. - /// Only valid for directory-based CDB, no-op and error log on InMemoryCDB; - void setCompileCommandsDir(Path P); - /// Returns a CDB that should be used to get compile commands for the /// current instance of ClangdLSPServer. GlobalCompilationDatabase &getCDB() { return *CDB; } @@ -160,10 +156,6 @@ void notify(StringRef Method, llvm::json::Value Params); void reply(llvm::json::Value ID, llvm::Expected Result); - // Various ClangdServer parameters go here. It's important they're created - // before ClangdServer. - CompilationDB CDB; - RealFileSystemProvider FSProvider; /// Options used for code completion clangd::CodeCompleteOptions CCOpts; @@ -179,6 +171,9 @@ // Store of the current versions of the open documents. DraftStore DraftMgr; + // The CDB is created by the "initialize" LSP method. + bool UseInMemoryCDB; // FIXME: make this a capability. + llvm::Optional CDB; // The ClangdServer is created by the "initialize" LSP method. // It is destroyed before run() returns, to ensure worker threads exit. ClangdServer::Options ClangdServerOpts; Index: clangd/ClangdLSPServer.cpp =================================================================== --- clangd/ClangdLSPServer.cpp +++ clangd/ClangdLSPServer.cpp @@ -264,18 +264,16 @@ if (Server) return Reply(make_error("server already initialized", ErrorCode::InvalidRequest)); - Server.emplace(CDB.getCDB(), FSProvider, + Optional CompileCommandsDir; + if (Params.initializationOptions) + CompileCommandsDir = Params.initializationOptions->compilationDatabasePath; + CDB.emplace(UseInMemoryCDB + ? CompilationDB::makeInMemory() + : CompilationDB::makeDirectoryBased(CompileCommandsDir)); + Server.emplace(CDB->getCDB(), FSProvider, static_cast(*this), ClangdServerOpts); - if (Params.initializationOptions) { - const ClangdInitializationOptions &Opts = *Params.initializationOptions; - - // Explicit compilation database path. - if (Opts.compilationDatabasePath.hasValue()) { - CDB.setCompileCommandsDir(Opts.compilationDatabasePath.getValue()); - } - - applyConfiguration(Opts.ParamsChange); - } + if (Params.initializationOptions) + applyConfiguration(Params.initializationOptions->ParamsChange); CCOpts.EnableSnippets = Params.capabilities.CompletionSnippets; DiagOpts.EmbedFixesInDiagnostics = Params.capabilities.DiagnosticFixes; @@ -332,7 +330,7 @@ const DidOpenTextDocumentParams &Params) { PathRef File = Params.textDocument.uri.file(); if (Params.metadata && !Params.metadata->extraFlags.empty()) - CDB.setExtraFlagsForFile(File, std::move(Params.metadata->extraFlags)); + CDB->setExtraFlagsForFile(File, std::move(Params.metadata->extraFlags)); const std::string &Contents = Params.textDocument.text; @@ -356,7 +354,7 @@ // fail rather than giving wrong results. DraftMgr.removeDraft(File); Server->removeDocument(File); - CDB.invalidate(File); + CDB->invalidate(File); elog("Failed to update {0}: {1}", File, Contents.takeError()); return; } @@ -452,7 +450,7 @@ PathRef File = Params.textDocument.uri.file(); DraftMgr.removeDraft(File); Server->removeDocument(File); - CDB.invalidate(File); + CDB->invalidate(File); } void ClangdLSPServer::onDocumentOnTypeFormatting( @@ -635,7 +633,7 @@ /// The opened files need to be reparsed only when some existing /// entries are changed. PathRef File = Entry.first; - if (!CDB.setCompilationCommandForFile( + if (!CDB->setCompilationCommandForFile( File, tooling::CompileCommand( std::move(Entry.second.workingDirectory), File, std::move(Entry.second.compilationCommand), @@ -664,13 +662,10 @@ Optional CompileCommandsDir, bool ShouldUseInMemoryCDB, const ClangdServer::Options &Opts) - : Transp(Transp), MsgHandler(new MessageHandler(*this)), - CDB(ShouldUseInMemoryCDB ? CompilationDB::makeInMemory() - : CompilationDB::makeDirectoryBased( - std::move(CompileCommandsDir))), - CCOpts(CCOpts), SupportedSymbolKinds(defaultSymbolKinds()), + : Transp(Transp), MsgHandler(new MessageHandler(*this)), CCOpts(CCOpts), + SupportedSymbolKinds(defaultSymbolKinds()), SupportedCompletionItemKinds(defaultCompletionItemKinds()), - ClangdServerOpts(Opts) { + UseInMemoryCDB(ShouldUseInMemoryCDB), ClangdServerOpts(Opts) { // clang-format off MsgHandler->bind("initialize", &ClangdLSPServer::onInitialize); MsgHandler->bind("shutdown", &ClangdLSPServer::onShutdown); @@ -825,15 +820,5 @@ ->setExtraFlagsForFile(File, std::move(ExtraFlags)); } -void ClangdLSPServer::CompilationDB::setCompileCommandsDir(Path P) { - if (!IsDirectoryBased) { - elog("Trying to set compile commands dir while using in-memory compilation " - "database"); - return; - } - static_cast(CDB.get()) - ->setCompileCommandsDir(P); -} - } // namespace clangd } // namespace clang