From d1c9d1163b89fa5084204c155b3d7ad7ab258b26 Mon Sep 17 00:00:00 2001 From: Sam McCall Date: Tue, 23 Oct 2018 14:19:54 +0000 Subject: [PATCH] [clangd] Lazily create CDB, remove setCompileCommandsDir. Summary: The only way to actually set the directory is at initialize time, so now CDB is lazy we can pass it to the constructor. Reviewers: hokein Subscribers: ilya-biryukov, ioeric, MaskRay, jkorous, arphaman, kadircet, cfe-commits Differential Revision: https://reviews.llvm.org/D53572 llvm-svn: 345031 --- clang-tools-extra/clangd/ClangdLSPServer.cpp | 47 ++++++++++------------------ clang-tools-extra/clangd/ClangdLSPServer.h | 11 ++----- 2 files changed, 19 insertions(+), 39 deletions(-) diff --git a/clang-tools-extra/clangd/ClangdLSPServer.cpp b/clang-tools-extra/clangd/ClangdLSPServer.cpp index 91d3553..019b7eb 100644 --- a/clang-tools-extra/clangd/ClangdLSPServer.cpp +++ b/clang-tools-extra/clangd/ClangdLSPServer.cpp @@ -264,18 +264,16 @@ void ClangdLSPServer::onInitialize(const InitializeParams &Params, 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 @@ void ClangdLSPServer::onDocumentDidOpen( 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 @@ void ClangdLSPServer::onDocumentDidChange( // 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 @@ void ClangdLSPServer::onDocumentDidClose( 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 @@ void ClangdLSPServer::applyConfiguration( /// 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 @@ ClangdLSPServer::ClangdLSPServer(class Transport &Transp, 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 @@ void ClangdLSPServer::CompilationDB::setExtraFlagsForFile( ->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 diff --git a/clang-tools-extra/clangd/ClangdLSPServer.h b/clang-tools-extra/clangd/ClangdLSPServer.h index 19bdcb6..e13ab34 100644 --- a/clang-tools-extra/clangd/ClangdLSPServer.h +++ b/clang-tools-extra/clangd/ClangdLSPServer.h @@ -129,10 +129,6 @@ private: 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 @@ private: 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 @@ private: // 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; -- 2.7.4