[cfe-commits] [PATCH] Standalone clang-fixit tool

Daniel Jasper reviews at llvm-reviews.chandlerc.com
Thu Sep 27 07:37:54 PDT 2012


Hi klimek, doug.gregor,

This is a first version of a standalone clang-fixit tool analog to clang-check (as already described on http://clang.llvm.org/docs/ClangTools.html).

http://llvm-reviews.chandlerc.com/D51

Files:
  tools/CMakeLists.txt
  tools/Makefile
  tools/clang-fixit/CMakeLists.txt
  tools/clang-fixit/ClangFixit.cpp
  tools/clang-fixit/Makefile

Index: tools/CMakeLists.txt
===================================================================
--- tools/CMakeLists.txt
+++ tools/CMakeLists.txt
@@ -5,6 +5,7 @@
 add_subdirectory(diagtool)
 add_subdirectory(driver)
 add_subdirectory(clang-check)
+add_subdirectory(clang-fixit)
 
 # We support checking out the clang-tools-extra repository into the 'extra'
 # subdirectory. It contains tools developed as part of the Clang/LLVM project
Index: tools/Makefile
===================================================================
--- tools/Makefile
+++ tools/Makefile
@@ -12,7 +12,7 @@
 include $(CLANG_LEVEL)/../../Makefile.config
 
 DIRS := driver libclang c-index-test arcmt-test c-arcmt-test diagtool \
-        clang-check
+        clang-check clang-fixit
 
 ifeq ($(CLANG_NO_TEST_TOOLS),YES)
 DIRS := driver libclang
Index: tools/clang-fixit/CMakeLists.txt
===================================================================
--- /dev/null
+++ tools/clang-fixit/CMakeLists.txt
@@ -0,0 +1,19 @@
+set(LLVM_LINK_COMPONENTS
+  ${LLVM_TARGETS_TO_BUILD}
+  asmparser
+  support
+  mc
+  )
+
+add_clang_executable(clang-fixit
+	ClangFixit.cpp
+  )
+
+target_link_libraries(clang-fixit
+  clangTooling
+  clangBasic
+  clangRewriteFrontend
+  )
+
+install(TARGETS clang-fixit
+  RUNTIME DESTINATION bin)
Index: tools/clang-fixit/ClangFixit.cpp
===================================================================
--- /dev/null
+++ tools/clang-fixit/ClangFixit.cpp
@@ -0,0 +1,90 @@
+//===--- tools/clang-fixit/ClangFixit.cpp - Clang fixit tool --------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+//  This file implements a clang-fixit tool that runs the
+//  clang::FixItAction over a number of translation units.
+//
+//  This tool uses the Clang Tooling infrastructure, see
+//    http://clang.llvm.org/docs/HowToSetupToolingForLLVM.html
+//  for details on setting it up with LLVM source tree.
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/Driver/OptTable.h"
+#include "clang/Driver/Options.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;
+using namespace llvm;
+
+static OwningPtr<OptTable> Options(createDriverOptTable());
+static cl::opt<bool> FixWhatYouCan(
+    "fix-what-you-can",
+    cl::desc(Options->getOptionHelpText(options::OPT_fix_what_you_can)));
+
+static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);
+
+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;
+  }
+};
+
+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
+
+int main(int argc, const char **argv) {
+  CommonOptionsParser OptionsParser(argc, argv);
+  ClangTool Tool(OptionsParser.GetCompilations(),
+                 OptionsParser.GetSourcePathList());
+  return Tool.run(newFrontendActionFactory<FixItAction>());
+}
Index: tools/clang-fixit/Makefile
===================================================================
--- /dev/null
+++ tools/clang-fixit/Makefile
@@ -0,0 +1,24 @@
+##===- tools/clang-fixit/Makefile --------------------------*- Makefile -*-===##
+#
+#                     The LLVM Compiler Infrastructure
+#
+# This file is distributed under the University of Illinois Open Source
+# License. See LICENSE.TXT for details.
+#
+##===----------------------------------------------------------------------===##
+
+CLANG_LEVEL := ../..
+
+TOOLNAME = clang-fixit
+
+# No plugins, optimize startup time.
+TOOL_NO_EXPORTS = 1
+
+include $(CLANG_LEVEL)/../../Makefile.config
+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 \
+           clangRewriteFrontend.a
+
+include $(CLANG_LEVEL)/Makefile
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D51.1.patch
Type: text/x-patch
Size: 5532 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20120927/720820c7/attachment.bin>


More information about the cfe-commits mailing list