[clang-tidy] Add an option to export suggested fixes into a file.

Allows gathering fixes and applying them with clang-apply-fixes.

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

llvm-svn: 217139
diff --git a/clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp b/clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp
index 030e4b1..d49994d 100644
--- a/clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp
+++ b/clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp
@@ -78,6 +78,13 @@
                                "clang-analyzer- checks."),
                       cl::init(false), cl::cat(ClangTidyCategory));
 
+static cl::opt<std::string> ExportFixes(
+    "export-fixes",
+    cl::desc("YAML file to store suggested fixes in. The\n"
+             "stored fixes can be applied to the input source\n"
+             "code with clang-apply-replacements."),
+    cl::value_desc("filename"), cl::cat(ClangTidyCategory));
+
 static void printStats(const clang::tidy::ClangTidyStats &Stats) {
   if (Stats.errorsIgnored()) {
     llvm::errs() << "Suppressed " << Stats.errorsIgnored() << " warnings (";
@@ -147,6 +154,16 @@
       OptionsParser.getSourcePathList(), &Errors);
   clang::tidy::handleErrors(Errors, Fix);
 
+  if (!ExportFixes.empty()) {
+    std::error_code EC;
+    llvm::raw_fd_ostream OS(ExportFixes, EC, llvm::sys::fs::F_None);
+    if (EC) {
+      llvm::errs() << "Error opening output file: " << EC.message() << '\n';
+      return 1;
+    }
+    clang::tidy::exportReplacements(Errors, OS);
+  }
+
   printStats(Stats);
   return 0;
 }