Skip to content

Commit

Permalink
Remove possibility to change compile database path at runtime
Browse files Browse the repository at this point in the history
Summary:
This patch removes the possibility to change the compilation database
path at runtime using the didChangeConfiguration request.  Instead, it
is suggested to use the setting on the initialize request, and clangd
whenever the user wants to use a different build configuration.

Subscribers: ilya-biryukov, ioeric, jkorous, arphaman, kadircet, cfe-commits

Differential Revision: https://reviews.llvm.org/D53220

llvm-svn: 344614
  • Loading branch information
Simon Marchi committed Oct 16, 2018
1 parent 6f732bf commit abeed66
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 69 deletions.
27 changes: 14 additions & 13 deletions clang-tools-extra/clangd/ClangdLSPServer.cpp
Expand Up @@ -81,8 +81,16 @@ CompletionItemKindBitset defaultCompletionItemKinds() {
} // namespace

void ClangdLSPServer::onInitialize(InitializeParams &Params) {
if (Params.initializationOptions)
applyConfiguration(*Params.initializationOptions);
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.rootUri && *Params.rootUri)
Server->setRootPath(Params.rootUri->file());
Expand Down Expand Up @@ -425,17 +433,10 @@ void ClangdLSPServer::onHover(TextDocumentPositionParams &Params) {
}

void ClangdLSPServer::applyConfiguration(
const ClangdConfigurationParamsChange &Settings) {
// Compilation database change.
if (Settings.compilationDatabasePath.hasValue()) {
CDB.setCompileCommandsDir(Settings.compilationDatabasePath.getValue());

reparseOpenedFiles();
}

// Update to the compilation database.
if (Settings.compilationDatabaseChanges) {
const auto &CompileCommandUpdates = *Settings.compilationDatabaseChanges;
const ClangdConfigurationParamsChange &Params) {
// Per-file update to the compilation database.
if (Params.compilationDatabaseChanges) {
const auto &CompileCommandUpdates = *Params.compilationDatabaseChanges;
bool ShouldReparseOpenFiles = false;
for (auto &Entry : CompileCommandUpdates) {
/// The opened files need to be reparsed only when some existing
Expand Down
11 changes: 10 additions & 1 deletion clang-tools-extra/clangd/Protocol.cpp
Expand Up @@ -665,10 +665,19 @@ bool fromJSON(const llvm::json::Value &Params,
bool fromJSON(const json::Value &Params,
ClangdConfigurationParamsChange &CCPC) {
json::ObjectMapper O(Params);
return O && O.map("compilationDatabasePath", CCPC.compilationDatabasePath) &&
return O &&
O.map("compilationDatabaseChanges", CCPC.compilationDatabaseChanges);
}

bool fromJSON(const json::Value &Params, ClangdInitializationOptions &Opts) {
if (!fromJSON(Params, Opts.ParamsChange)) {
return false;
}

json::ObjectMapper O(Params);
return O && O.map("compilationDatabasePath", Opts.compilationDatabasePath);
}

bool fromJSON(const json::Value &Params, ReferenceParams &R) {
TextDocumentPositionParams &Base = R;
return fromJSON(Params, Base);
Expand Down
11 changes: 8 additions & 3 deletions clang-tools-extra/clangd/Protocol.h
Expand Up @@ -411,16 +411,21 @@ bool fromJSON(const llvm::json::Value &, ClangdCompileCommand &);
/// "initialize" request and for the "workspace/didChangeConfiguration"
/// notification since the data received is described as 'any' type in LSP.
struct ClangdConfigurationParamsChange {
llvm::Optional<std::string> compilationDatabasePath;

// The changes that happened to the compilation database.
// The key of the map is a file name.
llvm::Optional<std::map<std::string, ClangdCompileCommand>>
compilationDatabaseChanges;
};
bool fromJSON(const llvm::json::Value &, ClangdConfigurationParamsChange &);

struct ClangdInitializationOptions : public ClangdConfigurationParamsChange {};
struct ClangdInitializationOptions {
// What we can change throught the didChangeConfiguration request, we can
// also set through the initialize request (initializationOptions field).
ClangdConfigurationParamsChange ParamsChange;

llvm::Optional<std::string> compilationDatabasePath;
};
bool fromJSON(const llvm::json::Value &, ClangdInitializationOptions &);

struct InitializeParams {
/// The process Id of the parent process that started
Expand Down
Expand Up @@ -3,10 +3,8 @@

# RUN: rm -rf %t.dir/* && mkdir -p %t.dir
# RUN: mkdir %t.dir/build-1
# RUN: mkdir %t.dir/build-2
# RUN: echo '[{"directory": "%/t.dir", "command": "c++ the-file.cpp", "file": "the-file.cpp"}]' > %t.dir/compile_commands.json
# RUN: echo '[{"directory": "%/t.dir/build-1", "command": "c++ -DMACRO=1 the-file.cpp", "file": "../the-file.cpp"}]' > %t.dir/build-1/compile_commands.json
# RUN: echo '[{"directory": "%/t.dir/build-2", "command": "c++ -DMACRO=2 the-file.cpp", "file": "../the-file.cpp"}]' > %t.dir/build-2/compile_commands.json

# RUN: sed -e "s|INPUT_DIR|%/t.dir|g" %s > %t.test.1

Expand All @@ -18,18 +16,11 @@

{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"initializationOptions":{"compilationDatabasePath":"INPUT_DIR/build-1"}}}
---
{"jsonrpc":"2.0","method":"textDocument/didOpen","params":{"textDocument":{"uri":"file://INPUT_DIR/the-file.cpp","languageId":"cpp","version":1,"text":"#if !defined(MACRO)\n#pragma message (\"MACRO is not defined\")\n#elif MACRO == 1\n#pragma message (\"MACRO is one\")\n#elif MACRO == 2\n#pragma message (\"MACRO is two\")\n#else\n#pragma message (\"woops\")\n#endif\nint main() {}\n"}}}
{"jsonrpc":"2.0","method":"textDocument/didOpen","params":{"textDocument":{"uri":"file://INPUT_DIR/the-file.cpp","languageId":"cpp","version":1,"text":"#if !defined(MACRO)\n#pragma message (\"MACRO is not defined\")\n#elif MACRO == 1\n#pragma message (\"MACRO is one\")\n#else\n#pragma message (\"woops\")\n#endif\nint main() {}\n"}}}
# CHECK: "method": "textDocument/publishDiagnostics",
# CHECK-NEXT: "params": {
# CHECK-NEXT: "diagnostics": [
# CHECK-NEXT: {
# CHECK-NEXT: "message": "MACRO is one",
---
{"jsonrpc":"2.0","id":0,"method":"workspace/didChangeConfiguration","params":{"settings":{"compilationDatabasePath":"INPUT_DIR/build-2"}}}
# CHECK: "method": "textDocument/publishDiagnostics",
# CHECK-NEXT: "params": {
# CHECK-NEXT: "diagnostics": [
# CHECK-NEXT: {
# CHECK-NEXT: "message": "MACRO is two",
---
{"jsonrpc":"2.0","id":10000,"method":"shutdown"}
42 changes: 0 additions & 42 deletions clang-tools-extra/test/clangd/compile-commands-path.test

This file was deleted.

0 comments on commit abeed66

Please sign in to comment.