Skip to content

Commit b662651

Browse files
committed
[clangd] Use command line adjusters for inserting compile flags
This fixes issues with `--` in the compile flags. Fixes clangd/clangd#632. Differential Revision: https://reviews.llvm.org/D99523
1 parent 6f0e74c commit b662651

File tree

4 files changed

+23
-13
lines changed

4 files changed

+23
-13
lines changed

clang-tools-extra/clangd/CompileCommands.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include "llvm/Support/MemoryBuffer.h"
2121
#include "llvm/Support/Path.h"
2222
#include "llvm/Support/Program.h"
23+
#include <string>
24+
#include <vector>
2325

2426
namespace clang {
2527
namespace clangd {
@@ -209,14 +211,20 @@ void CommandMangler::adjust(std::vector<std::string> &Cmd) const {
209211
Cmd = tooling::getStripPluginsAdjuster()(Cmd, "");
210212
Cmd = tooling::getClangSyntaxOnlyAdjuster()(Cmd, "");
211213

214+
std::vector<std::string> ToAppend;
212215
if (ResourceDir && !Has("-resource-dir"))
213-
Cmd.push_back(("-resource-dir=" + *ResourceDir));
216+
ToAppend.push_back(("-resource-dir=" + *ResourceDir));
214217

215218
// Don't set `-isysroot` if it is already set or if `--sysroot` is set.
216219
// `--sysroot` is a superset of the `-isysroot` argument.
217220
if (Sysroot && !Has("-isysroot") && !Has("--sysroot")) {
218-
Cmd.push_back("-isysroot");
219-
Cmd.push_back(*Sysroot);
221+
ToAppend.push_back("-isysroot");
222+
ToAppend.push_back(*Sysroot);
223+
}
224+
225+
if (!ToAppend.empty()) {
226+
Cmd = tooling::getInsertArgumentAdjuster(
227+
std::move(ToAppend), tooling::ArgumentInsertPosition::END)(Cmd, "");
220228
}
221229

222230
if (!Cmd.empty()) {
@@ -504,7 +512,6 @@ void ArgStripper::process(std::vector<std::string> &Args) const {
504512
Args.resize(Write);
505513
}
506514

507-
508515
std::string printArgv(llvm::ArrayRef<llvm::StringRef> Args) {
509516
std::string Buf;
510517
llvm::raw_string_ostream OS(Buf);

clang-tools-extra/clangd/ConfigCompile.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#include "llvm/Support/Regex.h"
4848
#include "llvm/Support/SMLoc.h"
4949
#include "llvm/Support/SourceMgr.h"
50+
#include <algorithm>
5051
#include <string>
5152

5253
namespace clang {
@@ -270,7 +271,9 @@ struct FragmentCompiler {
270271
Add.push_back(std::move(*A));
271272
Out.Apply.push_back([Add(std::move(Add))](const Params &, Config &C) {
272273
C.CompileFlags.Edits.push_back([Add](std::vector<std::string> &Args) {
273-
Args.insert(Args.end(), Add.begin(), Add.end());
274+
// The point to insert at. Just append when `--` isn't present.
275+
auto It = llvm::find(Args, "--");
276+
Args.insert(It, Add.begin(), Add.end());
274277
});
275278
});
276279
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,14 @@ TEST(CommandMangler, Everything) {
4141
Mangler.ClangPath = testPath("fake/clang");
4242
Mangler.ResourceDir = testPath("fake/resources");
4343
Mangler.Sysroot = testPath("fake/sysroot");
44-
std::vector<std::string> Cmd = {"clang++", "-Xclang", "-load", "-Xclang",
45-
"plugin", "-MF", "dep", "foo.cc"};
44+
std::vector<std::string> Cmd = {"clang++", "-Xclang", "-load",
45+
"-Xclang", "plugin", "-MF",
46+
"dep", "--", "foo.cc"};
4647
Mangler.adjust(Cmd);
47-
EXPECT_THAT(Cmd, ElementsAre(testPath("fake/clang++"), "foo.cc",
48-
"-fsyntax-only",
48+
EXPECT_THAT(Cmd, ElementsAre(testPath("fake/clang++"), "-fsyntax-only",
4949
"-resource-dir=" + testPath("fake/resources"),
50-
"-isysroot", testPath("fake/sysroot")));
50+
"-isysroot", testPath("fake/sysroot"), "--",
51+
"foo.cc"));
5152
}
5253

5354
TEST(CommandMangler, ResourceDir) {
@@ -378,4 +379,3 @@ TEST(PrintArgvTest, All) {
378379
} // namespace
379380
} // namespace clangd
380381
} // namespace clang
381-

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,12 @@ TEST_F(ConfigCompileTests, Condition) {
123123
TEST_F(ConfigCompileTests, CompileCommands) {
124124
Frag.CompileFlags.Add.emplace_back("-foo");
125125
Frag.CompileFlags.Remove.emplace_back("--include-directory=");
126-
std::vector<std::string> Argv = {"clang", "-I", "bar/", "a.cc"};
126+
std::vector<std::string> Argv = {"clang", "-I", "bar/", "--", "a.cc"};
127127
EXPECT_TRUE(compileAndApply());
128128
EXPECT_THAT(Conf.CompileFlags.Edits, SizeIs(2));
129129
for (auto &Edit : Conf.CompileFlags.Edits)
130130
Edit(Argv);
131-
EXPECT_THAT(Argv, ElementsAre("clang", "a.cc", "-foo"));
131+
EXPECT_THAT(Argv, ElementsAre("clang", "-foo", "--", "a.cc"));
132132
}
133133

134134
TEST_F(ConfigCompileTests, CompilationDatabase) {

0 commit comments

Comments
 (0)