[cfe-commits] [PATCH] Add -fixit option to clang-check
Daniel Jasper
reviews at llvm-reviews.chandlerc.com
Mon Oct 1 05:57:10 PDT 2012
Hi klimek, doug.gregor, chandlerc,
http://llvm-reviews.chandlerc.com/D51
CHANGE SINCE LAST DIFF
http://llvm-reviews.chandlerc.com/D51?vs=140&id=143#toc
Files:
tools/clang-check/CMakeLists.txt
tools/clang-check/ClangCheck.cpp
tools/clang-check/Makefile
Index: tools/clang-check/CMakeLists.txt
===================================================================
--- tools/clang-check/CMakeLists.txt
+++ tools/clang-check/CMakeLists.txt
@@ -12,6 +12,7 @@
target_link_libraries(clang-check
clangTooling
clangBasic
+ clangRewriteFrontend
)
install(TARGETS clang-check
Index: tools/clang-check/ClangCheck.cpp
===================================================================
--- tools/clang-check/ClangCheck.cpp
+++ tools/clang-check/ClangCheck.cpp
@@ -7,8 +7,8 @@
//
//===----------------------------------------------------------------------===//
//
-// This file implements a clang-check tool that runs the
-// clang::SyntaxOnlyAction over a number of translation units.
+// This file implements a clang-check tool that runs clang based on the info
+// stored in a compilation database.
//
// This tool uses the Clang Tooling infrastructure, see
// http://clang.llvm.org/docs/HowToSetupToolingForLLVM.html
@@ -20,9 +20,13 @@
#include "clang/Driver/OptTable.h"
#include "clang/Driver/Options.h"
#include "clang/Frontend/ASTConsumers.h"
+#include "clang/Frontend/CompilerInstance.h"
+#include "clang/Rewrite/Frontend/FixItRewriter.h"
+#include "clang/Rewrite/Frontend/FrontendActions.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Tooling.h"
#include "llvm/Support/CommandLine.h"
+#include "llvm/Support/Path.h"
using namespace clang::driver;
using namespace clang::tooling;
@@ -58,6 +62,64 @@
"ast-dump-filter",
cl::desc(Options->getOptionHelpText(options::OPT_ast_dump_filter)));
+static cl::opt<bool> Fixit(
+ "fixit",
+ cl::desc(Options->getOptionHelpText(options::OPT_fixit)));
+static cl::opt<bool> FixWhatYouCan(
+ "fix-what-you-can",
+ cl::desc(Options->getOptionHelpText(options::OPT_fix_what_you_can)));
+
+namespace {
+
+class FixItOptions : public clang::FixItOptions {
+public:
+ FixItOptions() {
+ FixWhatYouCan = ::FixWhatYouCan;
+ }
+
+ std::string RewriteFilename(const std::string& filename, int &fd) {
+ assert(llvm::sys::path::is_absolute(filename) &&
+ "clang-fixit expects absolute paths only.");
+
+ // We don't need to do permission checking here since clang will diagnose
+ // any I/O errors itself.
+
+ fd = -1; // No file descriptor for file.
+
+ return filename;
+ }
+};
+
+/// \brief Subclasses \c clang::FixItRewriter to not count fixed errors/warnings
+/// in the final error counts.
+///
+/// This has the side-effect that clang-check -fixit exits with code 0 on
+/// successfully fixing all errors.
+class FixItRewriter : public clang::FixItRewriter {
+public:
+ FixItRewriter(clang::DiagnosticsEngine& Diags,
+ clang::SourceManager& SourceMgr,
+ const clang::LangOptions& LangOpts,
+ clang::FixItOptions* FixItOpts)
+ : clang::FixItRewriter(Diags, SourceMgr, LangOpts, FixItOpts) {
+ }
+
+ virtual bool IncludeInDiagnosticCounts() const { return false; }
+};
+
+class FixItAction : public clang::FixItAction {
+public:
+ virtual bool BeginSourceFileAction(clang::CompilerInstance& CI,
+ StringRef Filename) {
+ FixItOpts.reset(new FixItOptions);
+ Rewriter.reset(new FixItRewriter(CI.getDiagnostics(), CI.getSourceManager(),
+ CI.getLangOpts(), FixItOpts.get()));
+ return true;
+ }
+};
+
+} // namespace
+
// Anonymous namespace here causes problems with gcc <= 4.4 on MacOS 10.6.
// "Non-global symbol: ... can't be a weak_definition"
namespace clang_check {
@@ -76,9 +138,11 @@
}
int main(int argc, const char **argv) {
- clang_check::ClangCheckActionFactory Factory;
CommonOptionsParser OptionsParser(argc, argv);
ClangTool Tool(OptionsParser.GetCompilations(),
OptionsParser.GetSourcePathList());
+ if (Fixit)
+ return Tool.run(newFrontendActionFactory<FixItAction>());
+ clang_check::ClangCheckActionFactory Factory;
return Tool.run(newFrontendActionFactory(&Factory));
}
Index: tools/clang-check/Makefile
===================================================================
--- tools/clang-check/Makefile
+++ tools/clang-check/Makefile
@@ -18,7 +18,7 @@
LINK_COMPONENTS := $(TARGETS_TO_BUILD) asmparser support mc
USEDLIBS = clangFrontend.a clangSerialization.a clangDriver.a \
clangTooling.a clangParse.a clangSema.a clangAnalysis.a \
- clangEdit.a clangAST.a clangLex.a clangBasic.a
+ clangEdit.a clangAST.a clangLex.a clangBasic.a \
+ clangRewriteFrontend.a
include $(CLANG_LEVEL)/Makefile
-
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D51.3.patch
Type: text/x-patch
Size: 4626 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20121001/bbb6d446/attachment.bin>
More information about the cfe-commits
mailing list