[cfe-commits] [clang-tools-extra] r161886 - in /clang-tools-extra/trunk: CMakeLists.txt Makefile toolTemplate/ toolTemplate/CMakeLists.txt toolTemplate/Makefile toolTemplate/ToolTemplate.cpp

Marshall Clow mclow at qualcomm.com
Tue Aug 14 11:53:39 PDT 2012


Author: marshall
Date: Tue Aug 14 13:53:39 2012
New Revision: 161886

URL: http://llvm.org/viewvc/llvm-project?rev=161886&view=rev
Log:
Added a tool template to make creating new tools simpler

Added:
    clang-tools-extra/trunk/toolTemplate/
    clang-tools-extra/trunk/toolTemplate/CMakeLists.txt
    clang-tools-extra/trunk/toolTemplate/Makefile
    clang-tools-extra/trunk/toolTemplate/ToolTemplate.cpp
Modified:
    clang-tools-extra/trunk/CMakeLists.txt
    clang-tools-extra/trunk/Makefile

Modified: clang-tools-extra/trunk/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/CMakeLists.txt?rev=161886&r1=161885&r2=161886&view=diff
==============================================================================
--- clang-tools-extra/trunk/CMakeLists.txt (original)
+++ clang-tools-extra/trunk/CMakeLists.txt Tue Aug 14 13:53:39 2012
@@ -1,4 +1,5 @@
 add_subdirectory(remove-cstr-calls)
+add_subdirectory(toolTemplate)
 
 # Add the common testsuite after all the tools.
 add_subdirectory(test)

Modified: clang-tools-extra/trunk/Makefile
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/Makefile?rev=161886&r1=161885&r2=161886&view=diff
==============================================================================
--- clang-tools-extra/trunk/Makefile (original)
+++ clang-tools-extra/trunk/Makefile Tue Aug 14 13:53:39 2012
@@ -11,7 +11,7 @@
 
 include $(CLANG_LEVEL)/../../Makefile.config
 
-PARALLEL_DIRS := remove-cstr-calls
+PARALLEL_DIRS := remove-cstr-calls toolTemplate
 
 include $(CLANG_LEVEL)/Makefile
 

Added: clang-tools-extra/trunk/toolTemplate/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/toolTemplate/CMakeLists.txt?rev=161886&view=auto
==============================================================================
--- clang-tools-extra/trunk/toolTemplate/CMakeLists.txt (added)
+++ clang-tools-extra/trunk/toolTemplate/CMakeLists.txt Tue Aug 14 13:53:39 2012
@@ -0,0 +1,6 @@
+add_clang_executable(tool-template
+  ToolTemplate.cpp
+  )
+
+target_link_libraries(tool-template
+  clangEdit clangTooling clangBasic clangAST clangASTMatchers)

Added: clang-tools-extra/trunk/toolTemplate/Makefile
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/toolTemplate/Makefile?rev=161886&view=auto
==============================================================================
--- clang-tools-extra/trunk/toolTemplate/Makefile (added)
+++ clang-tools-extra/trunk/toolTemplate/Makefile Tue Aug 14 13:53:39 2012
@@ -0,0 +1,24 @@
+##===-------- tools/toolTemplate/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 = tool-template
+NO_INSTALL = 1
+
+# No plugins, optimize startup time.
+TOOL_NO_EXPORTS = 1
+
+include $(CLANG_LEVEL)/../../Makefile.config
+LINK_COMPONENTS := $(TARGETS_TO_BUILD) asmparser support mc
+USEDLIBS = clangTooling.a clangFrontend.a clangSerialization.a clangDriver.a \
+					 clangRewrite.a clangParse.a clangSema.a clangAnalysis.a \
+					 clangAST.a clangASTMatchers.a clangEdit.a clangLex.a clangBasic.a
+
+include $(CLANG_LEVEL)/Makefile

Added: clang-tools-extra/trunk/toolTemplate/ToolTemplate.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/toolTemplate/ToolTemplate.cpp?rev=161886&view=auto
==============================================================================
--- clang-tools-extra/trunk/toolTemplate/ToolTemplate.cpp (added)
+++ clang-tools-extra/trunk/toolTemplate/ToolTemplate.cpp Tue Aug 14 13:53:39 2012
@@ -0,0 +1,105 @@
+//===- tools/extra/RefactoringTemplate.cpp - Template for refactoring 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 an empty refactoring tool using the clang tooling.
+//  The goal is to lower the "barrier to entry" for writing refactoring tools.
+//
+//  Usage:
+//  refactoringTemplate <cmake-output-dir> <file1> <file2> ...
+//
+//  Where <cmake-output-dir> is a CMake build directory in which a file named
+//  compile_commands.json exists (enable -DCMAKE_EXPORT_COMPILE_COMMANDS in
+//  CMake to get this output).
+//
+//  <file1> ... specify the paths of files in the CMake source tree. This path
+//  is looked up in the compile command database. If the path of a file is
+//  absolute, it needs to point into CMake's source tree. If the path is
+//  relative, the current working directory needs to be in the CMake source
+//  tree and the file must be in a subdirectory of the current working
+//  directory. "./" prefixes in the relative files will be automatically
+//  removed, but the rest of a relative path must be a suffix of a path in
+//  the compile command line database.
+//
+//  For example, to use refactoringTemplate on all files in a subtree of the
+//  source tree, use:
+//
+//    /path/in/subtree $ find . -name '*.cpp'|
+//        xargs refactoringTemplate /path/to/build
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/ASTMatchers/ASTMatchers.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/Basic/SourceManager.h"
+#include "clang/Frontend/FrontendActions.h"
+#include "clang/Lex/Lexer.h"
+#include "clang/Tooling/CompilationDatabase.h"
+#include "clang/Tooling/Refactoring.h"
+#include "clang/Tooling/Tooling.h"
+#include "llvm/ADT/OwningPtr.h"
+#include "llvm/Support/CommandLine.h"
+#include "llvm/Support/MemoryBuffer.h"
+
+using namespace clang;
+using namespace clang::ast_matchers;
+using namespace clang::tooling;
+using namespace llvm;
+
+namespace {
+class TemplateCallback : public MatchFinder::MatchCallback {
+ public:
+  TemplateCallback(Replacements *Replace) : Replace(Replace) {}
+
+  virtual void run(const MatchFinder::MatchResult &Result) {
+//  TODO: This routine will get called for each thing that the matchers find.
+//  At this point, you can examine the match, and do whatever you want,
+//  including replacing the matched text with other text
+  }
+
+ private:
+  Replacements *Replace;
+};
+} // end anonymous namespace
+
+// Set up the command line options
+cl::opt<std::string> BuildPath(
+  cl::Positional,
+  cl::desc("<build-path>"));
+
+cl::list<std::string> SourcePaths(
+  cl::Positional,
+  cl::desc("<source0> [... <sourceN>]"),
+  cl::OneOrMore);
+
+int main(int argc, const char **argv) {
+  llvm::OwningPtr<CompilationDatabase> Compilations(
+        FixedCompilationDatabase::loadFromCommandLine(argc, argv));
+  cl::ParseCommandLineOptions(argc, argv);
+  if (!Compilations) {  // Couldn't find a compilation DB from the command line
+    std::string ErrorMessage;
+    Compilations.reset(
+      !BuildPath.empty() ?
+        CompilationDatabase::autoDetectFromDirectory(BuildPath, ErrorMessage) :
+        CompilationDatabase::autoDetectFromSource(SourcePaths[0], ErrorMessage)
+      );
+
+//  Still no compilation DB? - bail.
+    if (!Compilations)
+      llvm::report_fatal_error(ErrorMessage);
+    }
+  RefactoringTool Tool(*Compilations, SourcePaths);
+  ast_matchers::MatchFinder Finder;
+  TemplateCallback Callback(&Tool.getReplacements());
+
+// TODO: Put your matchers here.
+// Use Finder.addMatcher(...) to define the patterns in the AST that you
+// want to match against. You are not limited to just one matcher!
+
+  return Tool.run(newFrontendActionFactory(&Finder));
+}





More information about the cfe-commits mailing list