Skip to content

Commit f2b3e25

Browse files
committed
[clangd] Add CompileFlags.Compiler option to override argv0
This is separate from --query-driver but can combine with it. Fixes clangd/clangd#642 Differential Revision: https://reviews.llvm.org/D116196
1 parent f4ef793 commit f2b3e25

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

clang-tools-extra/clangd/ConfigCompile.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,16 @@ struct FragmentCompiler {
253253
}
254254

255255
void compile(Fragment::CompileFlagsBlock &&F) {
256+
if (F.Compiler)
257+
Out.Apply.push_back(
258+
[Compiler(std::move(**F.Compiler))](const Params &, Config &C) {
259+
C.CompileFlags.Edits.push_back(
260+
[Compiler](std::vector<std::string> &Args) {
261+
if (!Args.empty())
262+
Args.front() = Compiler;
263+
});
264+
});
265+
256266
if (!F.Remove.empty()) {
257267
auto Remove = std::make_shared<ArgStripper>();
258268
for (auto &A : F.Remove)

clang-tools-extra/clangd/ConfigFragment.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,16 @@ struct Fragment {
134134
///
135135
/// This section modifies how the compile command is constructed.
136136
struct CompileFlagsBlock {
137+
/// Override the compiler executable name to simulate.
138+
///
139+
/// The name can affect how flags are parsed (clang++ vs clang).
140+
/// If the executable name is in the --query-driver allowlist, then it will
141+
/// be invoked to extract include paths.
142+
///
143+
/// (That this simply replaces argv[0], and may mangle commands that use
144+
/// more complicated drivers like ccache).
145+
llvm::Optional<Located<std::string>> Compiler;
146+
137147
/// List of flags to append to the compile command.
138148
std::vector<Located<std::string>> Add;
139149
/// List of flags to remove from the compile command.

clang-tools-extra/clangd/ConfigYAML.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ class Parser {
9090

9191
void parse(Fragment::CompileFlagsBlock &F, Node &N) {
9292
DictParser Dict("CompileFlags", this);
93+
Dict.handle("Compiler", [&](Node &N) {
94+
if (auto Value = scalarValue(N, "Compiler"))
95+
F.Compiler = std::move(*Value);
96+
});
9397
Dict.handle("Add", [&](Node &N) {
9498
if (auto Values = scalarValues(N))
9599
F.Add = std::move(*Values);

clang-tools-extra/clangd/unittests/ConfigCompileTests.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,15 @@ TEST_F(ConfigCompileTests, Condition) {
121121
}
122122

123123
TEST_F(ConfigCompileTests, CompileCommands) {
124+
Frag.CompileFlags.Compiler.emplace("tpc.exe");
124125
Frag.CompileFlags.Add.emplace_back("-foo");
125126
Frag.CompileFlags.Remove.emplace_back("--include-directory=");
126127
std::vector<std::string> Argv = {"clang", "-I", "bar/", "--", "a.cc"};
127128
EXPECT_TRUE(compileAndApply());
128-
EXPECT_THAT(Conf.CompileFlags.Edits, SizeIs(2));
129+
EXPECT_THAT(Conf.CompileFlags.Edits, SizeIs(3));
129130
for (auto &Edit : Conf.CompileFlags.Edits)
130131
Edit(Argv);
131-
EXPECT_THAT(Argv, ElementsAre("clang", "-foo", "--", "a.cc"));
132+
EXPECT_THAT(Argv, ElementsAre("tpc.exe", "-foo", "--", "a.cc"));
132133
}
133134

134135
TEST_F(ConfigCompileTests, CompilationDatabase) {

0 commit comments

Comments
 (0)