kadircet created this revision. kadircet added a reviewer: sammccall. Herald added subscribers: cfe-commits, usaxena95, arphaman, jkorous, MaskRay, ilya-biryukov. Herald added a project: clang.
Currently clangd initializes the ClangdServer lazily during onInitialize request, and it results in propagation of caller's context rather than the main context created ClangdLSPServer. This patch changes the logic to store main context that created ClangdLSPServer and pass it onto to ClangdServer and other objects like CDBs. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D68978 Files: clang-tools-extra/clangd/ClangdLSPServer.cpp clang-tools-extra/clangd/ClangdLSPServer.h Index: clang-tools-extra/clangd/ClangdLSPServer.h =================================================================== --- clang-tools-extra/clangd/ClangdLSPServer.h +++ clang-tools-extra/clangd/ClangdLSPServer.h @@ -10,6 +10,7 @@ #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_CLANGDLSPSERVER_H #include "ClangdServer.h" +#include "Context.h" #include "DraftStore.h" #include "Features.inc" #include "FindSymbols.h" @@ -131,6 +132,11 @@ void publishDiagnostics(const URIForFile &File, std::vector<clangd::Diagnostic> Diagnostics); + // Since initialization of CDBs and ClangdServer is done lazily, the following + // context captures the one used while creating ClangdLSPServer and passes it + // to above mentioned object instances to make sure they share the same state. + Context BackgroundContext; + /// Used to indicate that the 'shutdown' request was received from the /// Language Server client. bool ShutdownRequestReceived = false; Index: clang-tools-extra/clangd/ClangdLSPServer.cpp =================================================================== --- clang-tools-extra/clangd/ClangdLSPServer.cpp +++ clang-tools-extra/clangd/ClangdLSPServer.cpp @@ -7,6 +7,7 @@ //===----------------------------------------------------------------------===// #include "ClangdLSPServer.h" +#include "Context.h" #include "Diagnostics.h" #include "DraftStore.h" #include "FormattedString.h" @@ -456,6 +457,11 @@ void ClangdLSPServer::onInitialize(const InitializeParams &Params, Callback<llvm::json::Value> Reply) { + // Switch caller's context with LSPServer's background context. Since we + // rather want to propagate information from LSPServer's context into the + // Server, CDB, etc. + llvm::Optional<WithContext> MainContext; + MainContext.emplace(BackgroundContext.clone()); // Determine character encoding first as it affects constructed ClangdServer. if (Params.capabilities.offsetEncoding && !NegotiatedOffsetEncoding) { NegotiatedOffsetEncoding = OffsetEncoding::UTF16; // fallback @@ -577,6 +583,8 @@ ->insert( {"semanticHighlighting", llvm::json::Object{{"scopes", buildHighlightScopeLookupTable()}}}); + // Restore caller's context. + MainContext.reset(); Reply(std::move(Result)); } @@ -1184,9 +1192,9 @@ llvm::Optional<Path> CompileCommandsDir, bool UseDirBasedCDB, llvm::Optional<OffsetEncoding> ForcedOffsetEncoding, const ClangdServer::Options &Opts) - : Transp(Transp), MsgHandler(new MessageHandler(*this)), - FSProvider(FSProvider), CCOpts(CCOpts), - SupportedSymbolKinds(defaultSymbolKinds()), + : BackgroundContext(Context::current().clone()), Transp(Transp), + MsgHandler(new MessageHandler(*this)), FSProvider(FSProvider), + CCOpts(CCOpts), SupportedSymbolKinds(defaultSymbolKinds()), SupportedCompletionItemKinds(defaultCompletionItemKinds()), UseDirBasedCDB(UseDirBasedCDB), CompileCommandsDir(std::move(CompileCommandsDir)), ClangdServerOpts(Opts),
Index: clang-tools-extra/clangd/ClangdLSPServer.h =================================================================== --- clang-tools-extra/clangd/ClangdLSPServer.h +++ clang-tools-extra/clangd/ClangdLSPServer.h @@ -10,6 +10,7 @@ #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_CLANGDLSPSERVER_H #include "ClangdServer.h" +#include "Context.h" #include "DraftStore.h" #include "Features.inc" #include "FindSymbols.h" @@ -131,6 +132,11 @@ void publishDiagnostics(const URIForFile &File, std::vector<clangd::Diagnostic> Diagnostics); + // Since initialization of CDBs and ClangdServer is done lazily, the following + // context captures the one used while creating ClangdLSPServer and passes it + // to above mentioned object instances to make sure they share the same state. + Context BackgroundContext; + /// Used to indicate that the 'shutdown' request was received from the /// Language Server client. bool ShutdownRequestReceived = false; Index: clang-tools-extra/clangd/ClangdLSPServer.cpp =================================================================== --- clang-tools-extra/clangd/ClangdLSPServer.cpp +++ clang-tools-extra/clangd/ClangdLSPServer.cpp @@ -7,6 +7,7 @@ //===----------------------------------------------------------------------===// #include "ClangdLSPServer.h" +#include "Context.h" #include "Diagnostics.h" #include "DraftStore.h" #include "FormattedString.h" @@ -456,6 +457,11 @@ void ClangdLSPServer::onInitialize(const InitializeParams &Params, Callback<llvm::json::Value> Reply) { + // Switch caller's context with LSPServer's background context. Since we + // rather want to propagate information from LSPServer's context into the + // Server, CDB, etc. + llvm::Optional<WithContext> MainContext; + MainContext.emplace(BackgroundContext.clone()); // Determine character encoding first as it affects constructed ClangdServer. if (Params.capabilities.offsetEncoding && !NegotiatedOffsetEncoding) { NegotiatedOffsetEncoding = OffsetEncoding::UTF16; // fallback @@ -577,6 +583,8 @@ ->insert( {"semanticHighlighting", llvm::json::Object{{"scopes", buildHighlightScopeLookupTable()}}}); + // Restore caller's context. + MainContext.reset(); Reply(std::move(Result)); } @@ -1184,9 +1192,9 @@ llvm::Optional<Path> CompileCommandsDir, bool UseDirBasedCDB, llvm::Optional<OffsetEncoding> ForcedOffsetEncoding, const ClangdServer::Options &Opts) - : Transp(Transp), MsgHandler(new MessageHandler(*this)), - FSProvider(FSProvider), CCOpts(CCOpts), - SupportedSymbolKinds(defaultSymbolKinds()), + : BackgroundContext(Context::current().clone()), Transp(Transp), + MsgHandler(new MessageHandler(*this)), FSProvider(FSProvider), + CCOpts(CCOpts), SupportedSymbolKinds(defaultSymbolKinds()), SupportedCompletionItemKinds(defaultCompletionItemKinds()), UseDirBasedCDB(UseDirBasedCDB), CompileCommandsDir(std::move(CompileCommandsDir)), ClangdServerOpts(Opts),
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits