[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
This commit is contained in:
Benjamin Kramer 2014-09-04 10:31:23 +00:00
parent bb72e6c804
commit fb98b74f8e
4 changed files with 38 additions and 1 deletions

View File

@ -34,6 +34,7 @@
#include "clang/Rewrite/Frontend/FrontendActions.h"
#include "clang/StaticAnalyzer/Frontend/AnalysisConsumer.h"
#include "clang/Tooling/Refactoring.h"
#include "clang/Tooling/ReplacementsYaml.h"
#include "clang/Tooling/Tooling.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/Process.h"
@ -362,5 +363,16 @@ void handleErrors(const std::vector<ClangTidyError> &Errors, bool Fix) {
Reporter.Finish();
}
void exportReplacements(const std::vector<ClangTidyError> &Errors,
raw_ostream &OS) {
tooling::TranslationUnitReplacements TUR;
for (const ClangTidyError &Error : Errors)
TUR.Replacements.insert(TUR.Replacements.end(), Error.Fix.begin(),
Error.Fix.end());
yaml::Output YAML(OS);
YAML << TUR;
}
} // namespace tidy
} // namespace clang

View File

@ -133,6 +133,11 @@ runClangTidy(ClangTidyOptionsProvider *OptionsProvider,
/// Errors containing fixes are automatically applied.
void handleErrors(const std::vector<ClangTidyError> &Errors, bool Fix);
/// \brief Serializes replacements into YAML and writes them to the specified
/// output stream.
void exportReplacements(const std::vector<ClangTidyError> &Errors,
raw_ostream &OS);
} // end namespace tidy
} // end namespace clang

View File

@ -78,6 +78,13 @@ AnalyzeTemporaryDtors("analyze-temporary-dtors",
"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 @@ int main(int argc, const char **argv) {
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;
}

View File

@ -1,14 +1,17 @@
// RUN: grep -Ev "// *[A-Z-]+:" %s > %t.cpp
// RUN: clang-tidy %t.cpp -checks='-*,google-explicit-constructor,llvm-namespace-comment' -fix -- > %t.msg 2>&1
// RUN: clang-tidy %t.cpp -checks='-*,google-explicit-constructor,llvm-namespace-comment' -fix -export-fixes=%t.yaml -- > %t.msg 2>&1
// RUN: FileCheck -input-file=%t.cpp %s
// RUN: FileCheck -input-file=%t.msg -check-prefix=CHECK-MESSAGES %s
// RUN: FileCheck -input-file=%t.yaml -check-prefix=CHECK-YAML %s
namespace i {
}
// CHECK: } // namespace i
// CHECK-MESSAGES: note: FIX-IT applied suggested code changes
// CHECK-YAML: ReplacementText: ' // namespace i'
class A { A(int i); };
// CHECK: class A { explicit A(int i); };
// CHECK-MESSAGES: note: FIX-IT applied suggested code changes
// CHECK-MESSAGES: clang-tidy applied 2 of 2 suggested fixes.
// CHECK-YAML: ReplacementText: 'explicit '