Skip to content

Commit 118c33e

Browse files
committed
[clangd] Allow configuration database to be specified in config.
This allows for more flexibility than -compile-commands-dir or ancestor discovery. See clangd/clangd#116 Differential Revision: https://reviews.llvm.org/D95057
1 parent 835104a commit 118c33e

10 files changed

+418
-90
lines changed

clang-tools-extra/clangd/ClangdLSPServer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,7 @@ void ClangdLSPServer::onInitialize(const InitializeParams &Params,
523523
if (Opts.UseDirBasedCDB) {
524524
DirectoryBasedGlobalCompilationDatabase::Options CDBOpts(TFS);
525525
CDBOpts.CompileCommandsDir = Opts.CompileCommandsDir;
526+
CDBOpts.ContextProvider = Opts.ContextProvider;
526527
BaseCDB =
527528
std::make_unique<DirectoryBasedGlobalCompilationDatabase>(CDBOpts);
528529
BaseCDB = getQueryDriverDatabase(llvm::makeArrayRef(Opts.QueryDriverGlobs),

clang-tools-extra/clangd/Config.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,19 @@ struct Config {
5252
Config(Config &&) = default;
5353
Config &operator=(Config &&) = default;
5454

55+
struct CDBSearchSpec {
56+
enum { Ancestors, FixedDir, NoCDBSearch } Policy = Ancestors;
57+
// Absolute, native slashes, no trailing slash.
58+
llvm::Optional<std::string> FixedCDBPath;
59+
};
60+
5561
/// Controls how the compile command for the current file is determined.
5662
struct {
57-
// Edits to apply to the compile command, in sequence.
63+
/// Edits to apply to the compile command, in sequence.
5864
std::vector<llvm::unique_function<void(std::vector<std::string> &) const>>
5965
Edits;
66+
/// Where to search for compilation databases for this file's flags.
67+
CDBSearchSpec CDBSearch = {CDBSearchSpec::Ancestors, llvm::None};
6068
} CompileFlags;
6169

6270
enum class BackgroundPolicy { Build, Skip };

clang-tools-extra/clangd/ConfigCompile.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,36 @@ struct FragmentCompiler {
263263
});
264264
});
265265
}
266+
267+
if (F.CompilationDatabase) {
268+
llvm::Optional<Config::CDBSearchSpec> Spec;
269+
if (**F.CompilationDatabase == "Ancestors") {
270+
Spec.emplace();
271+
Spec->Policy = Config::CDBSearchSpec::Ancestors;
272+
} else if (**F.CompilationDatabase == "None") {
273+
Spec.emplace();
274+
Spec->Policy = Config::CDBSearchSpec::NoCDBSearch;
275+
} else {
276+
if (auto Path =
277+
makeAbsolute(*F.CompilationDatabase, "CompilationDatabase",
278+
llvm::sys::path::Style::native)) {
279+
// Drop trailing slash to put the path in canonical form.
280+
// Should makeAbsolute do this?
281+
llvm::StringRef Rel = llvm::sys::path::relative_path(*Path);
282+
if (!Rel.empty() && llvm::sys::path::is_separator(Rel.back()))
283+
Path->pop_back();
284+
285+
Spec.emplace();
286+
Spec->Policy = Config::CDBSearchSpec::FixedDir;
287+
Spec->FixedCDBPath = std::move(Path);
288+
}
289+
}
290+
if (Spec)
291+
Out.Apply.push_back(
292+
[Spec(std::move(*Spec))](const Params &, Config &C) {
293+
C.CompileFlags.CDBSearch = Spec;
294+
});
295+
}
266296
}
267297

268298
void compile(Fragment::IndexBlock &&F) {

clang-tools-extra/clangd/ConfigFragment.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,13 @@ struct Fragment {
151151
///
152152
/// Flags added by the same CompileFlags entry will not be removed.
153153
std::vector<Located<std::string>> Remove;
154+
155+
/// Directory to search for compilation database (compile_comands.json etc).
156+
/// Valid values are:
157+
/// - A single path to a directory (absolute, or relative to the fragment)
158+
/// - Ancestors: search all parent directories (the default)
159+
/// - None: do not use a compilation database, just default flags.
160+
llvm::Optional<Located<std::string>> CompilationDatabase;
154161
};
155162
CompileFlagsBlock CompileFlags;
156163

clang-tools-extra/clangd/ConfigYAML.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ class Parser {
9595
if (auto Values = scalarValues(N))
9696
F.Remove = std::move(*Values);
9797
});
98+
Dict.handle("CompilationDatabase", [&](Node &N) {
99+
F.CompilationDatabase = scalarValue(N, "CompilationDatabase");
100+
});
98101
Dict.parse(N);
99102
}
100103

0 commit comments

Comments
 (0)