]> granicus.if.org Git - clang/commitdiff
Fix skipping of flags in getClangStripDependencyFileAdjuster
authorDave Lee <davelee.com@gmail.com>
Fri, 17 Nov 2017 16:27:21 +0000 (16:27 +0000)
committerDave Lee <davelee.com@gmail.com>
Fri, 17 Nov 2017 16:27:21 +0000 (16:27 +0000)
Summary:
The ArgumentsAdjuster returned from `getClangStripDependencyFileAdjuster` will
skip dependency flags, and also their associated values for those flags that
take an argument. This change corrects the handling of the `-MD` and `-MMD`
flags, which do not take an argument.

Reviewers: saugustine, klimek, alexshap

Reviewed By: alexshap

Subscribers: cfe-commits

Differential Revision: https://reviews.llvm.org/D40024

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@318529 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Tooling/ArgumentsAdjusters.cpp
unittests/Tooling/ToolingTest.cpp

index 962ea45258552aa5b7102c296e97e30b9b0b584c..7068ec2c4010e7cc474c356ab419234b68531a29 100644 (file)
@@ -58,14 +58,14 @@ ArgumentsAdjuster getClangStripDependencyFileAdjuster() {
       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"))
+      if (!Arg.startswith("-M")) {
         AdjustedArgs.push_back(Args[i]);
+        continue;
+      }
 
-      if ((Arg == "-MF") || (Arg == "-MT") || (Arg == "-MQ") ||
-          (Arg == "-MD") || (Arg == "-MMD")) {
-        // Output is specified as -MX foo. Skip the next argument also.
+      if (Arg == "-MF" || Arg == "-MT" || Arg == "-MQ")
+        // These flags take an argument: -MX foo. Skip the next argument also.
         ++i;
-      }
     }
     return AdjustedArgs;
   };
index b179f033d3579478950368869d0b00bd7cb760ec..430dcaa831f663131b6fe85065289dd890fd3fac 100644 (file)
@@ -402,6 +402,37 @@ TEST(ClangToolTest, ArgumentAdjusters) {
   EXPECT_FALSE(Found);
 }
 
+// Check getClangStripDependencyFileAdjuster doesn't strip args after -MD/-MMD.
+TEST(ClangToolTest, StripDependencyFileAdjuster) {
+  FixedCompilationDatabase Compilations("/", {"-MD", "-c", "-MMD", "-w"});
+
+  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 std::find(FinalArgs.begin(), FinalArgs.end(), Flag) !=
+           FinalArgs.end();
+  };
+  EXPECT_FALSE(HasFlag("-MD"));
+  EXPECT_FALSE(HasFlag("-MMD"));
+  EXPECT_TRUE(HasFlag("-c"));
+  EXPECT_TRUE(HasFlag("-w"));
+}
+
 namespace {
 /// Find a target name such that looking for it in TargetRegistry by that name
 /// returns the same target. We expect that there is at least one target