[clang-tools-extra] r221152 - [clang-tidy] Added -fix-errors option

Alexander Kornienko alexfh at google.com
Mon Nov 3 06:06:31 PST 2014


Author: alexfh
Date: Mon Nov  3 08:06:31 2014
New Revision: 221152

URL: http://llvm.org/viewvc/llvm-project?rev=221152&view=rev
Log:
[clang-tidy] Added -fix-errors option

Summary:
Added -fix-errors option to allow applying fixes when compiler errors
are present. Without this flag -fix would bail out if there are compiler errors.
This is needed to avoid applying wrong fixes if Clang fails to recover from
compilation errors correctly.

Reviewers: djasper, klimek

Reviewed By: klimek

Subscribers: curdeius, cfe-commits

Differential Revision: http://reviews.llvm.org/D6059

Added:
    clang-tools-extra/trunk/test/clang-tidy/fix-errors.cpp
Modified:
    clang-tools-extra/trunk/clang-tidy/tool/ClangTidyMain.cpp

Modified: clang-tools-extra/trunk/clang-tidy/tool/ClangTidyMain.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/tool/ClangTidyMain.cpp?rev=221152&r1=221151&r2=221152&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/tool/ClangTidyMain.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/tool/ClangTidyMain.cpp Mon Nov  3 08:06:31 2014
@@ -64,7 +64,7 @@ HeaderFilter("header-filter",
 
 static cl::opt<bool>
     SystemHeaders("system-headers",
-                  cl::desc("Display the errors from system headers"),
+                  cl::desc("Display the errors from system headers."),
                   cl::init(false), cl::cat(ClangTidyCategory));
 static cl::opt<std::string>
 LineFilter("line-filter",
@@ -78,8 +78,18 @@ LineFilter("line-filter",
                     "  ]"),
            cl::init(""), cl::cat(ClangTidyCategory));
 
-static cl::opt<bool> Fix("fix", cl::desc("Fix detected errors if possible."),
-                         cl::init(false), cl::cat(ClangTidyCategory));
+static cl::opt<bool>
+    Fix("fix", cl::desc("Apply suggested fixes. Without -fix-errors\n"
+                        "clang-tidy will bail out if any compilation\n"
+                        "errors were found."),
+        cl::init(false), cl::cat(ClangTidyCategory));
+
+static cl::opt<bool>
+    FixErrors("fix-errors",
+              cl::desc("Apply suggested fixes even if compilation errors\n"
+                       "were found. If compiler errors have attached\n"
+                       "fix-its, clang-tidy will apply them as well."),
+              cl::init(false), cl::cat(ClangTidyCategory));
 
 static cl::opt<bool>
 ListChecks("list-checks",
@@ -277,7 +287,15 @@ int clangTidyMain(int argc, const char *
       runClangTidy(std::move(OptionsProvider), OptionsParser.getCompilations(),
                    OptionsParser.getSourcePathList(), &Errors,
                    EnableCheckProfile ? &Profile : nullptr);
-  handleErrors(Errors, Fix);
+  bool FoundErrors =
+      std::find_if(Errors.begin(), Errors.end(), [](const ClangTidyError &E) {
+        return E.DiagLevel == ClangTidyError::Error;
+      }) != Errors.end();
+
+  const bool DisableFixes = Fix && FoundErrors && !FixErrors;
+
+  // -fix-errors implies -fix.
+  handleErrors(Errors, (FixErrors || Fix) && !DisableFixes);
 
   if (!ExportFixes.empty() && !Errors.empty()) {
     std::error_code EC;
@@ -290,6 +308,10 @@ int clangTidyMain(int argc, const char *
   }
 
   printStats(Stats);
+  if (DisableFixes)
+    llvm::errs() << "Found compiler errors, but -fix-error was not specified.\n"
+                    "Fixes have NOT been applied.\n\n";
+
   if (EnableCheckProfile)
     printProfileData(Profile, llvm::errs());
 

Added: clang-tools-extra/trunk/test/clang-tidy/fix-errors.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/fix-errors.cpp?rev=221152&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/fix-errors.cpp (added)
+++ clang-tools-extra/trunk/test/clang-tidy/fix-errors.cpp Mon Nov  3 08:06:31 2014
@@ -0,0 +1,15 @@
+// RUN: grep -Ev "// *[A-Z-]+:" %s > %t.cpp
+// RUN: clang-tidy %t.cpp -checks='-*,google-explicit-constructor' -fix -- > %t.msg 2>&1
+// RUN: FileCheck -input-file=%t.cpp -check-prefix=CHECK-FIX %s
+// RUN: FileCheck -input-file=%t.msg -check-prefix=CHECK-MESSAGES %s
+// RUN: grep -Ev "// *[A-Z-]+:" %s > %t.cpp
+// RUN: clang-tidy %t.cpp -checks='-*,google-explicit-constructor' -fix-errors -- > %t.msg 2>&1
+// RUN: FileCheck -input-file=%t.cpp -check-prefix=CHECK-FIX2 %s
+// RUN: FileCheck -input-file=%t.msg -check-prefix=CHECK-MESSAGES2 %s
+
+class A { A(int i); }
+// CHECK-FIX: class A { A(int i); }{{$}}
+// CHECK-MESSAGES: Fixes have NOT been applied.
+// CHECK-FIX2: class A { explicit A(int i); };
+// CHECK-MESSAGES2: note: FIX-IT applied suggested code changes
+// CHECK-MESSAGES2: clang-tidy applied 2 of 2 suggested fixes.





More information about the cfe-commits mailing list