summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam McCall <sam.mccall@gmail.com>2021-12-23 01:26:49 +0100
committerSam McCall <sam.mccall@gmail.com>2022-01-04 11:38:27 +0100
commitf2b3e25f860ed029ddb9d96744a8158ba2a95b23 (patch)
treeae559fc4d14bcb9096f12fec571a821646259c38
parentf4ef79306cee2b5866aff681174f16b816810c4a (diff)
downloadllvm-f2b3e25f860ed029ddb9d96744a8158ba2a95b23.tar.gz
[clangd] Add CompileFlags.Compiler option to override argv0
This is separate from --query-driver but can combine with it. Fixes https://github.com/clangd/clangd/issues/642 Differential Revision: https://reviews.llvm.org/D116196
-rw-r--r--clang-tools-extra/clangd/ConfigCompile.cpp10
-rw-r--r--clang-tools-extra/clangd/ConfigFragment.h10
-rw-r--r--clang-tools-extra/clangd/ConfigYAML.cpp4
-rw-r--r--clang-tools-extra/clangd/unittests/ConfigCompileTests.cpp5
4 files changed, 27 insertions, 2 deletions
diff --git a/clang-tools-extra/clangd/ConfigCompile.cpp b/clang-tools-extra/clangd/ConfigCompile.cpp
index 4f7f90847433..18afdeb3cb5c 100644
--- a/clang-tools-extra/clangd/ConfigCompile.cpp
+++ b/clang-tools-extra/clangd/ConfigCompile.cpp
@@ -253,6 +253,16 @@ struct FragmentCompiler {
}
void compile(Fragment::CompileFlagsBlock &&F) {
+ if (F.Compiler)
+ Out.Apply.push_back(
+ [Compiler(std::move(**F.Compiler))](const Params &, Config &C) {
+ C.CompileFlags.Edits.push_back(
+ [Compiler](std::vector<std::string> &Args) {
+ if (!Args.empty())
+ Args.front() = Compiler;
+ });
+ });
+
if (!F.Remove.empty()) {
auto Remove = std::make_shared<ArgStripper>();
for (auto &A : F.Remove)
diff --git a/clang-tools-extra/clangd/ConfigFragment.h b/clang-tools-extra/clangd/ConfigFragment.h
index 63d2d75a2262..31c4636efa0b 100644
--- a/clang-tools-extra/clangd/ConfigFragment.h
+++ b/clang-tools-extra/clangd/ConfigFragment.h
@@ -134,6 +134,16 @@ struct Fragment {
///
/// This section modifies how the compile command is constructed.
struct CompileFlagsBlock {
+ /// Override the compiler executable name to simulate.
+ ///
+ /// The name can affect how flags are parsed (clang++ vs clang).
+ /// If the executable name is in the --query-driver allowlist, then it will
+ /// be invoked to extract include paths.
+ ///
+ /// (That this simply replaces argv[0], and may mangle commands that use
+ /// more complicated drivers like ccache).
+ llvm::Optional<Located<std::string>> Compiler;
+
/// List of flags to append to the compile command.
std::vector<Located<std::string>> Add;
/// List of flags to remove from the compile command.
diff --git a/clang-tools-extra/clangd/ConfigYAML.cpp b/clang-tools-extra/clangd/ConfigYAML.cpp
index 6be11f199b48..0487c3281576 100644
--- a/clang-tools-extra/clangd/ConfigYAML.cpp
+++ b/clang-tools-extra/clangd/ConfigYAML.cpp
@@ -90,6 +90,10 @@ private:
void parse(Fragment::CompileFlagsBlock &F, Node &N) {
DictParser Dict("CompileFlags", this);
+ Dict.handle("Compiler", [&](Node &N) {
+ if (auto Value = scalarValue(N, "Compiler"))
+ F.Compiler = std::move(*Value);
+ });
Dict.handle("Add", [&](Node &N) {
if (auto Values = scalarValues(N))
F.Add = std::move(*Values);
diff --git a/clang-tools-extra/clangd/unittests/ConfigCompileTests.cpp b/clang-tools-extra/clangd/unittests/ConfigCompileTests.cpp
index 87d8b9d976f0..661784256af8 100644
--- a/clang-tools-extra/clangd/unittests/ConfigCompileTests.cpp
+++ b/clang-tools-extra/clangd/unittests/ConfigCompileTests.cpp
@@ -121,14 +121,15 @@ TEST_F(ConfigCompileTests, Condition) {
}
TEST_F(ConfigCompileTests, CompileCommands) {
+ Frag.CompileFlags.Compiler.emplace("tpc.exe");
Frag.CompileFlags.Add.emplace_back("-foo");
Frag.CompileFlags.Remove.emplace_back("--include-directory=");
std::vector<std::string> Argv = {"clang", "-I", "bar/", "--", "a.cc"};
EXPECT_TRUE(compileAndApply());
- EXPECT_THAT(Conf.CompileFlags.Edits, SizeIs(2));
+ EXPECT_THAT(Conf.CompileFlags.Edits, SizeIs(3));
for (auto &Edit : Conf.CompileFlags.Edits)
Edit(Argv);
- EXPECT_THAT(Argv, ElementsAre("clang", "-foo", "--", "a.cc"));
+ EXPECT_THAT(Argv, ElementsAre("tpc.exe", "-foo", "--", "a.cc"));
}
TEST_F(ConfigCompileTests, CompilationDatabase) {