[clang] 2168dbf - getClangStripDependencyFileAdjuster(): Do not remove -M args when using MSVC cl driver
Hans Wennborg via cfe-commits
cfe-commits at lists.llvm.org
Tue Sep 8 01:21:33 PDT 2020
Author: Shivanshu Goyal
Date: 2020-09-08T10:21:18+02:00
New Revision: 2168dbf4cc766dfb552076d9b1e84b00122b7993
URL: https://github.com/llvm/llvm-project/commit/2168dbf4cc766dfb552076d9b1e84b00122b7993
DIFF: https://github.com/llvm/llvm-project/commit/2168dbf4cc766dfb552076d9b1e84b00122b7993.diff
LOG: getClangStripDependencyFileAdjuster(): Do not remove -M args when using MSVC cl driver
MSVC's cl.exe has a few command line arguments which start with -M such
as "-MD", "-MDd", "-MT", "-MTd", "-MP".
These arguments are not dependency file generation related, and these
arguments were being removed by getClangStripDependencyFileAdjuster()
which was wrong.
Differential revision: https://reviews.llvm.org/D86999
Added:
Modified:
clang/lib/Tooling/ArgumentsAdjusters.cpp
clang/unittests/Tooling/ToolingTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/Tooling/ArgumentsAdjusters.cpp b/clang/lib/Tooling/ArgumentsAdjusters.cpp
index a857b57fbf7b..bcfb5b39a077 100644
--- a/clang/lib/Tooling/ArgumentsAdjusters.cpp
+++ b/clang/lib/Tooling/ArgumentsAdjusters.cpp
@@ -21,6 +21,16 @@
namespace clang {
namespace tooling {
+static StringRef getDriverMode(const CommandLineArguments &Args) {
+ for (const auto &Arg : Args) {
+ StringRef ArgRef = Arg;
+ if (ArgRef.consume_front("--driver-mode=")) {
+ return ArgRef;
+ }
+ }
+ return StringRef();
+}
+
/// Add -fsyntax-only option and drop options that triggers output generation.
ArgumentsAdjuster getClangSyntaxOnlyAdjuster() {
return [](const CommandLineArguments &Args, StringRef /*unused*/) {
@@ -93,20 +103,28 @@ ArgumentsAdjuster getClangStripSerializeDiagnosticAdjuster() {
ArgumentsAdjuster getClangStripDependencyFileAdjuster() {
return [](const CommandLineArguments &Args, StringRef /*unused*/) {
+ auto UsingClDriver = (getDriverMode(Args) == "cl");
+
CommandLineArguments AdjustedArgs;
for (size_t i = 0, e = Args.size(); i < e; ++i) {
StringRef Arg = Args[i];
- // All dependency-file options begin with -M. These include -MM,
- // -MF, -MG, -MP, -MT, -MQ, -MD, and -MMD.
- if (!Arg.startswith("-M") && !Arg.startswith("/showIncludes") &&
- !Arg.startswith("-showIncludes")) {
- AdjustedArgs.push_back(Args[i]);
+
+ // These flags take an argument: -MX foo. Skip the next argument also.
+ if (!UsingClDriver && (Arg == "-MF" || Arg == "-MT" || Arg == "-MQ")) {
+ ++i;
continue;
}
+ // When not using the cl driver mode, dependency file generation options
+ // begin with -M. These include -MM, -MF, -MG, -MP, -MT, -MQ, -MD, and
+ // -MMD.
+ if (!UsingClDriver && Arg.startswith("-M"))
+ continue;
+ // Under MSVC's cl driver mode, dependency file generation is controlled
+ // using /showIncludes
+ if (Arg.startswith("/showIncludes") || Arg.startswith("-showIncludes"))
+ continue;
- if (Arg == "-MF" || Arg == "-MT" || Arg == "-MQ")
- // These flags take an argument: -MX foo. Skip the next argument also.
- ++i;
+ AdjustedArgs.push_back(Args[i]);
}
return AdjustedArgs;
};
diff --git a/clang/unittests/Tooling/ToolingTest.cpp b/clang/unittests/Tooling/ToolingTest.cpp
index cc6f453284d7..691a847d5a71 100644
--- a/clang/unittests/Tooling/ToolingTest.cpp
+++ b/clang/unittests/Tooling/ToolingTest.cpp
@@ -563,6 +563,40 @@ TEST(ClangToolTest, StripDependencyFileAdjusterShowIncludes) {
EXPECT_TRUE(HasFlag("-c"));
}
+// Check getClangStripDependencyFileAdjuster doesn't strip args when using the
+// MSVC cl.exe driver
+TEST(ClangToolTest, StripDependencyFileAdjusterMsvc) {
+ FixedCompilationDatabase Compilations(
+ "/", {"--driver-mode=cl", "-MD", "-MDd", "-MT", "-O1", "-MTd", "-MP"});
+
+ ClangTool Tool(Compilations, std::vector<std::string>(1, "/a.cc"));
+ Tool.mapVirtualFile("/a.cc", "void a() {}");
+
+ std::unique_ptr<FrontendActionFactory> Action(
+ newFrontendActionFactory<SyntaxOnlyAction>());
+
+ CommandLineArguments FinalArgs;
+ ArgumentsAdjuster CheckFlagsAdjuster =
+ [&FinalArgs](const CommandLineArguments &Args, StringRef /*unused*/) {
+ FinalArgs = Args;
+ return Args;
+ };
+ Tool.clearArgumentsAdjusters();
+ Tool.appendArgumentsAdjuster(getClangStripDependencyFileAdjuster());
+ Tool.appendArgumentsAdjuster(CheckFlagsAdjuster);
+ Tool.run(Action.get());
+
+ auto HasFlag = [&FinalArgs](const std::string &Flag) {
+ return llvm::find(FinalArgs, Flag) != FinalArgs.end();
+ };
+ EXPECT_TRUE(HasFlag("-MD"));
+ EXPECT_TRUE(HasFlag("-MDd"));
+ EXPECT_TRUE(HasFlag("-MT"));
+ EXPECT_TRUE(HasFlag("-O1"));
+ EXPECT_TRUE(HasFlag("-MTd"));
+ EXPECT_TRUE(HasFlag("-MP"));
+}
+
// Check getClangStripPluginsAdjuster strips plugin related args.
TEST(ClangToolTest, StripPluginsAdjuster) {
FixedCompilationDatabase Compilations(
More information about the cfe-commits
mailing list