-
Notifications
You must be signed in to change notification settings - Fork 15k
[clangd] Fix off-by-one error in CommandMangler #160029
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[clangd] Fix off-by-one error in CommandMangler #160029
Conversation
SawInput() is intended to be called for every argument after a `--`, but it was mistakenly being called for the `--` itself. Partially fixes clangd/clangd#1850
|
@llvm/pr-subscribers-clangd @llvm/pr-subscribers-clang-tools-extra Author: Nathan Ridge (HighCommander4) ChangesSawInput() is intended to be called for every argument after a Partially fixes clangd/clangd#1850 Full diff: https://github.com/llvm/llvm-project/pull/160029.diff 2 Files Affected:
diff --git a/clang-tools-extra/clangd/CompileCommands.cpp b/clang-tools-extra/clangd/CompileCommands.cpp
index 80391fe8cce25..c9da98e96ccfb 100644
--- a/clang-tools-extra/clangd/CompileCommands.cpp
+++ b/clang-tools-extra/clangd/CompileCommands.cpp
@@ -270,7 +270,8 @@ void CommandMangler::operator()(tooling::CompileCommand &Command,
if (auto *DashDash =
ArgList.getLastArgNoClaim(driver::options::OPT__DASH_DASH)) {
auto DashDashIndex = DashDash->getIndex() + 1; // +1 accounts for Cmd[0]
- for (unsigned I = DashDashIndex; I < Cmd.size(); ++I)
+ // Another +1 so we don't treat the `--` itself as an input.
+ for (unsigned I = DashDashIndex + 1; I < Cmd.size(); ++I)
SawInput(Cmd[I]);
Cmd.resize(DashDashIndex);
}
diff --git a/clang-tools-extra/clangd/unittests/CompileCommandsTests.cpp b/clang-tools-extra/clangd/unittests/CompileCommandsTests.cpp
index 2ce2975bd962b..e324404e627c2 100644
--- a/clang-tools-extra/clangd/unittests/CompileCommandsTests.cpp
+++ b/clang-tools-extra/clangd/unittests/CompileCommandsTests.cpp
@@ -526,6 +526,16 @@ TEST(CommandMangler, RespectsOriginalSysroot) {
Not(HasSubstr(testPath("fake/sysroot"))));
}
}
+
+TEST(CommandMangler, StdLatestFlag) {
+ const auto Mangler = CommandMangler::forTests();
+ tooling::CompileCommand Cmd;
+ Cmd.CommandLine = {"clang-cl", "/std:c++latest", "--", "/Users/foo.cc"};
+ Mangler(Cmd, "/Users/foo.cc");
+ // Check that the /std:c++latest flag is not dropped
+ EXPECT_THAT(llvm::join(Cmd.CommandLine, " "), HasSubstr("/std:c++latest"));
+}
+
} // namespace
} // namespace clangd
} // namespace clang
|
|
(Originally submitted as #156593. I'm resubmitting it now on a user branch so I can stack the other part of the fix for clangd/clangd#1850 on top of it.) |
| } | ||
| } | ||
|
|
||
| TEST(CommandMangler, StdLatestFlag) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As mentioned in the original PR, I'm not sure if this is the best test case for this change, because the fact that transferCompileCommand() drops /std:c++latest is itself a bug which will be fixed in #160030.
But I'm not sure how to write a more direct test for "transferCompileCommand() was not called", and the scenario being tested here is a valid one from a user expectation point of view.
|
Review ping |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good catch, thank you for the fix.
SawInput() is intended to be called for every argument after a `--`, but it was mistakenly being called for the `--` itself. Partially fixes clangd/clangd#1850
SawInput() is intended to be called for every argument after a
--, but it was mistakenly being called for the--itself.Partially fixes clangd/clangd#1850