Skip to content

Commit 04e21fb

Browse files
committed
Revert "[clangd] Adjust compile flags to contain only the requested file as input"
This reverts commit ba5dd94.
1 parent c7c70f2 commit 04e21fb

File tree

6 files changed

+25
-37
lines changed

6 files changed

+25
-37
lines changed

clang-tools-extra/clangd/CompileCommands.cpp

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include "clang/Driver/Options.h"
1414
#include "clang/Frontend/CompilerInvocation.h"
1515
#include "clang/Tooling/ArgumentsAdjusters.h"
16-
#include "llvm/ADT/ArrayRef.h"
1716
#include "llvm/ADT/STLExtras.h"
1817
#include "llvm/ADT/SmallVector.h"
1918
#include "llvm/ADT/StringRef.h"
@@ -194,8 +193,7 @@ CommandMangler CommandMangler::detect() {
194193

195194
CommandMangler CommandMangler::forTests() { return CommandMangler(); }
196195

197-
void CommandMangler::adjust(std::vector<std::string> &Cmd,
198-
llvm::StringRef File) const {
196+
void CommandMangler::adjust(std::vector<std::string> &Cmd) const {
199197
trace::Span S("AdjustCompileFlags");
200198
auto &OptTable = clang::driver::getDriverOptTable();
201199
// OriginalArgs needs to outlive ArgList.
@@ -206,27 +204,18 @@ void CommandMangler::adjust(std::vector<std::string> &Cmd,
206204
// ParseArgs propagates missig arg/opt counts on error, but preserves
207205
// everything it could parse in ArgList. So we just ignore those counts.
208206
unsigned IgnoredCount;
209-
// Drop the executable name, as ParseArgs doesn't expect it. This means
210-
// indices are actually of by one between ArgList and OriginalArgs.
211-
auto ArgList =
212-
OptTable.ParseArgs(llvm::makeArrayRef(OriginalArgs).drop_front(),
213-
IgnoredCount, IgnoredCount);
207+
auto ArgList = OptTable.ParseArgs(OriginalArgs, IgnoredCount, IgnoredCount);
214208

215209
// Move the inputs to the end, separated via `--` from flags. This ensures
216210
// modifications done in the following steps apply in more cases (like setting
217211
// -x, which only affects inputs that come after it).
218212
if (!ArgList.hasArgNoClaim(driver::options::OPT__DASH_DASH)) {
219-
// Drop all the inputs and only add one for the current file.
220-
llvm::SmallVector<unsigned, 1> IndicesToDrop;
221-
for (auto *Input : ArgList.filtered(driver::options::OPT_INPUT))
222-
IndicesToDrop.push_back(Input->getIndex());
223-
llvm::sort(IndicesToDrop);
224-
llvm::for_each(llvm::reverse(IndicesToDrop),
225-
// +1 to account for the executable name in Cmd[0] that
226-
// doesn't exist in ArgList.
227-
[&Cmd](unsigned Idx) { Cmd.erase(Cmd.begin() + Idx + 1); });
228-
Cmd.push_back("--");
229-
Cmd.push_back(File.str());
213+
// In theory there might be more than one input, but clangd can't deal with
214+
// them anyway.
215+
if (auto *Input = ArgList.getLastArg(driver::options::OPT_INPUT)) {
216+
Cmd.insert(Cmd.end(), {"--", Input->getAsString(ArgList)});
217+
Cmd.erase(Cmd.begin() + Input->getIndex());
218+
}
230219
}
231220

232221
for (auto &Edit : Config::current().CompileFlags.Edits)
@@ -276,7 +265,7 @@ CommandMangler::operator clang::tooling::ArgumentsAdjuster() && {
276265
return [Mangler = std::make_shared<CommandMangler>(std::move(*this))](
277266
const std::vector<std::string> &Args, llvm::StringRef File) {
278267
auto Result = Args;
279-
Mangler->adjust(Result, File);
268+
Mangler->adjust(Result);
280269
return Result;
281270
};
282271
}

clang-tools-extra/clangd/CompileCommands.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#include "clang/Tooling/ArgumentsAdjusters.h"
1313
#include "clang/Tooling/CompilationDatabase.h"
1414
#include "llvm/ADT/StringMap.h"
15-
#include "llvm/ADT/StringRef.h"
1615
#include <deque>
1716
#include <string>
1817
#include <vector>
@@ -47,7 +46,7 @@ struct CommandMangler {
4746
// - on mac, find clang and isysroot by querying the `xcrun` launcher
4847
static CommandMangler detect();
4948

50-
void adjust(std::vector<std::string> &Cmd, llvm::StringRef File) const;
49+
void adjust(std::vector<std::string> &Cmd) const;
5150
explicit operator clang::tooling::ArgumentsAdjuster() &&;
5251

5352
private:

clang-tools-extra/clangd/GlobalCompilationDatabase.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,7 @@ OverlayCDB::getCompileCommand(PathRef File) const {
766766
if (!Cmd)
767767
return llvm::None;
768768
if (ArgsAdjuster)
769-
Cmd->CommandLine = ArgsAdjuster(Cmd->CommandLine, File);
769+
Cmd->CommandLine = ArgsAdjuster(Cmd->CommandLine, Cmd->Filename);
770770
return Cmd;
771771
}
772772

@@ -776,7 +776,7 @@ tooling::CompileCommand OverlayCDB::getFallbackCommand(PathRef File) const {
776776
Cmd.CommandLine.insert(Cmd.CommandLine.end(), FallbackFlags.begin(),
777777
FallbackFlags.end());
778778
if (ArgsAdjuster)
779-
Cmd.CommandLine = ArgsAdjuster(Cmd.CommandLine, File);
779+
Cmd.CommandLine = ArgsAdjuster(Cmd.CommandLine, Cmd.Filename);
780780
return Cmd;
781781
}
782782

clang-tools-extra/clangd/test/did-change-configuration-params.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
#
4949
# ERR: ASTWorker building file {{.*}}foo.c version 0 with command
5050
# ERR: [{{.*}}clangd-test2]
51-
# ERR: clang -c -Wall -Werror {{.*}} -- {{.*}}foo.c
51+
# ERR: clang -c -Wall -Werror {{.*}} -- foo.c
5252
---
5353
{"jsonrpc":"2.0","id":5,"method":"shutdown"}
5454
---

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ TEST(CommandMangler, Everything) {
4343
Mangler.ResourceDir = testPath("fake/resources");
4444
Mangler.Sysroot = testPath("fake/sysroot");
4545
std::vector<std::string> Cmd = {"clang++", "--", "foo.cc"};
46-
Mangler.adjust(Cmd, "foo.cc");
46+
Mangler.adjust(Cmd);
4747
EXPECT_THAT(Cmd, ElementsAre(testPath("fake/clang++"),
4848
"-resource-dir=" + testPath("fake/resources"),
4949
"-isysroot", testPath("fake/sysroot"), "--",
@@ -54,7 +54,7 @@ TEST(CommandMangler, ResourceDir) {
5454
auto Mangler = CommandMangler::forTests();
5555
Mangler.ResourceDir = testPath("fake/resources");
5656
std::vector<std::string> Cmd = {"clang++", "foo.cc"};
57-
Mangler.adjust(Cmd, "foo.cc");
57+
Mangler.adjust(Cmd);
5858
EXPECT_THAT(Cmd, Contains("-resource-dir=" + testPath("fake/resources")));
5959
}
6060

@@ -63,7 +63,7 @@ TEST(CommandMangler, Sysroot) {
6363
Mangler.Sysroot = testPath("fake/sysroot");
6464

6565
std::vector<std::string> Cmd = {"clang++", "foo.cc"};
66-
Mangler.adjust(Cmd, "foo.cc");
66+
Mangler.adjust(Cmd);
6767
EXPECT_THAT(llvm::join(Cmd, " "),
6868
HasSubstr("-isysroot " + testPath("fake/sysroot")));
6969
}
@@ -73,19 +73,19 @@ TEST(CommandMangler, ClangPath) {
7373
Mangler.ClangPath = testPath("fake/clang");
7474

7575
std::vector<std::string> Cmd = {"clang++", "foo.cc"};
76-
Mangler.adjust(Cmd, "foo.cc");
76+
Mangler.adjust(Cmd);
7777
EXPECT_EQ(testPath("fake/clang++"), Cmd.front());
7878

7979
Cmd = {"unknown-binary", "foo.cc"};
80-
Mangler.adjust(Cmd, "foo.cc");
80+
Mangler.adjust(Cmd);
8181
EXPECT_EQ(testPath("fake/unknown-binary"), Cmd.front());
8282

8383
Cmd = {testPath("path/clang++"), "foo.cc"};
84-
Mangler.adjust(Cmd, "foo.cc");
84+
Mangler.adjust(Cmd);
8585
EXPECT_EQ(testPath("path/clang++"), Cmd.front());
8686

8787
Cmd = {"foo/unknown-binary", "foo.cc"};
88-
Mangler.adjust(Cmd, "foo.cc");
88+
Mangler.adjust(Cmd);
8989
EXPECT_EQ("foo/unknown-binary", Cmd.front());
9090
}
9191

@@ -128,7 +128,7 @@ TEST(CommandMangler, ClangPathResolve) {
128128
auto Mangler = CommandMangler::forTests();
129129
Mangler.ClangPath = testPath("fake/clang");
130130
std::vector<std::string> Cmd = {(TempDir + "/bin/foo").str(), "foo.cc"};
131-
Mangler.adjust(Cmd, "foo.cc");
131+
Mangler.adjust(Cmd);
132132
// Directory based on resolved symlink, basename preserved.
133133
EXPECT_EQ((TempDir + "/lib/foo").str(), Cmd.front());
134134

@@ -145,13 +145,13 @@ TEST(CommandMangler, ClangPathResolve) {
145145
Mangler.ClangPath = testPath("fake/clang");
146146
// Driver found on PATH.
147147
Cmd = {"foo", "foo.cc"};
148-
Mangler.adjust(Cmd, "foo.cc");
148+
Mangler.adjust(Cmd);
149149
// Found the symlink and resolved the path as above.
150150
EXPECT_EQ((TempDir + "/lib/foo").str(), Cmd.front());
151151

152152
// Symlink not resolved with -no-canonical-prefixes.
153153
Cmd = {"foo", "-no-canonical-prefixes", "foo.cc"};
154-
Mangler.adjust(Cmd, "foo.cc");
154+
Mangler.adjust(Cmd);
155155
EXPECT_EQ((TempDir + "/bin/foo").str(), Cmd.front());
156156
}
157157
#endif
@@ -170,7 +170,7 @@ TEST(CommandMangler, ConfigEdits) {
170170
Argv = tooling::getInsertArgumentAdjuster("--hello")(Argv, "");
171171
});
172172
WithContextValue WithConfig(Config::Key, std::move(Cfg));
173-
Mangler.adjust(Cmd, "foo.cc");
173+
Mangler.adjust(Cmd);
174174
}
175175
// Edits are applied in given order and before other mangling and they always
176176
// go before filename.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ ParseInputs TestTU::inputs(MockFS &FS) const {
5959
Argv.push_back(FullFilename);
6060

6161
auto Mangler = CommandMangler::forTests();
62-
Mangler.adjust(Inputs.CompileCommand.CommandLine, FullFilename);
62+
Mangler.adjust(Inputs.CompileCommand.CommandLine);
6363
Inputs.CompileCommand.Filename = FullFilename;
6464
Inputs.CompileCommand.Directory = testRoot();
6565
Inputs.Contents = Code;

0 commit comments

Comments
 (0)