[clang-tools-extra] r345031 - [clangd] Lazily create CDB, remove setCompileCommandsDir.

Sam McCall via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 23 07:19:54 PDT 2018


Author: sammccall
Date: Tue Oct 23 07:19:54 2018
New Revision: 345031

URL: http://llvm.org/viewvc/llvm-project?rev=345031&view=rev
Log:
[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

Modified:
    clang-tools-extra/trunk/clangd/ClangdLSPServer.cpp
    clang-tools-extra/trunk/clangd/ClangdLSPServer.h

Modified: clang-tools-extra/trunk/clangd/ClangdLSPServer.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/ClangdLSPServer.cpp?rev=345031&r1=345030&r2=345031&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/ClangdLSPServer.cpp (original)
+++ clang-tools-extra/trunk/clangd/ClangdLSPServer.cpp Tue Oct 23 07:19:54 2018
@@ -264,18 +264,16 @@ void ClangdLSPServer::onInitialize(const
   if (Server)
     return Reply(make_error<LSPError>("server already initialized",
                                       ErrorCode::InvalidRequest));
-  Server.emplace(CDB.getCDB(), FSProvider,
+  Optional<Path> CompileCommandsDir;
+  if (Params.initializationOptions)
+    CompileCommandsDir = Params.initializationOptions->compilationDatabasePath;
+  CDB.emplace(UseInMemoryCDB
+                  ? CompilationDB::makeInMemory()
+                  : CompilationDB::makeDirectoryBased(CompileCommandsDir));
+  Server.emplace(CDB->getCDB(), FSProvider,
                  static_cast<DiagnosticsConsumer &>(*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::onDocumentDidChang
     // 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 T
                                  Optional<Path> 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::set
       ->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<DirectoryBasedGlobalCompilationDatabase *>(CDB.get())
-      ->setCompileCommandsDir(P);
-}
-
 } // namespace clangd
 } // namespace clang

Modified: clang-tools-extra/trunk/clangd/ClangdLSPServer.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/ClangdLSPServer.h?rev=345031&r1=345030&r2=345031&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/ClangdLSPServer.h (original)
+++ clang-tools-extra/trunk/clangd/ClangdLSPServer.h Tue Oct 23 07:19:54 2018
@@ -129,10 +129,6 @@ private:
     void setExtraFlagsForFile(PathRef File,
                               std::vector<std::string> 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<llvm::json::Value> 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<CompilationDB> CDB;
   // The ClangdServer is created by the "initialize" LSP method.
   // It is destroyed before run() returns, to ensure worker threads exit.
   ClangdServer::Options ClangdServerOpts;




More information about the cfe-commits mailing list