<div class="gmail_extra"><div class="gmail_quote">On Tue, Aug 14, 2012 at 11:53 AM, Marshall Clow <span dir="ltr"><<a href="mailto:mclow@qualcomm.com" target="_blank" class="cremed">mclow@qualcomm.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: marshall<br>
Date: Tue Aug 14 13:53:39 2012<br>
New Revision: 161886<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=161886&view=rev" target="_blank" class="cremed">http://llvm.org/viewvc/llvm-project?rev=161886&view=rev</a><br>
Log:<br>
Added a tool template to make creating new tools simpler<br>
<br>
Added:<br>
clang-tools-extra/trunk/toolTemplate/<br></blockquote><div><br></div><div>The directory name should be consistent with the binary name, and with the tool already in the tree....</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
clang-tools-extra/trunk/toolTemplate/CMakeLists.txt<br>
clang-tools-extra/trunk/toolTemplate/Makefile<br>
clang-tools-extra/trunk/toolTemplate/ToolTemplate.cpp<br>
Modified:<br>
clang-tools-extra/trunk/CMakeLists.txt<br>
clang-tools-extra/trunk/Makefile<br>
<br>
Modified: clang-tools-extra/trunk/CMakeLists.txt<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/CMakeLists.txt?rev=161886&r1=161885&r2=161886&view=diff" target="_blank" class="cremed">http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/CMakeLists.txt?rev=161886&r1=161885&r2=161886&view=diff</a><br>
==============================================================================<br>
--- clang-tools-extra/trunk/CMakeLists.txt (original)<br>
+++ clang-tools-extra/trunk/CMakeLists.txt Tue Aug 14 13:53:39 2012<br>
@@ -1,4 +1,5 @@<br>
add_subdirectory(remove-cstr-calls)<br>
+add_subdirectory(toolTemplate)<br>
<br>
# Add the common testsuite after all the tools.<br>
add_subdirectory(test)<br>
<br>
Modified: clang-tools-extra/trunk/Makefile<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/Makefile?rev=161886&r1=161885&r2=161886&view=diff" target="_blank" class="cremed">http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/Makefile?rev=161886&r1=161885&r2=161886&view=diff</a><br>
==============================================================================<br>
--- clang-tools-extra/trunk/Makefile (original)<br>
+++ clang-tools-extra/trunk/Makefile Tue Aug 14 13:53:39 2012<br>
@@ -11,7 +11,7 @@<br>
<br>
include $(CLANG_LEVEL)/../../Makefile.config<br>
<br>
-PARALLEL_DIRS := remove-cstr-calls<br>
+PARALLEL_DIRS := remove-cstr-calls toolTemplate<br>
<br>
include $(CLANG_LEVEL)/Makefile<br>
<br>
<br>
Added: clang-tools-extra/trunk/toolTemplate/CMakeLists.txt<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/toolTemplate/CMakeLists.txt?rev=161886&view=auto" target="_blank" class="cremed">http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/toolTemplate/CMakeLists.txt?rev=161886&view=auto</a><br>
==============================================================================<br>
--- clang-tools-extra/trunk/toolTemplate/CMakeLists.txt (added)<br>
+++ clang-tools-extra/trunk/toolTemplate/CMakeLists.txt Tue Aug 14 13:53:39 2012<br>
@@ -0,0 +1,6 @@<br>
+add_clang_executable(tool-template<br>
+ ToolTemplate.cpp<br>
+ )<br>
+<br>
+target_link_libraries(tool-template<br>
+ clangEdit clangTooling clangBasic clangAST clangASTMatchers)<br>
<br>
Added: clang-tools-extra/trunk/toolTemplate/Makefile<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/toolTemplate/Makefile?rev=161886&view=auto" target="_blank" class="cremed">http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/toolTemplate/Makefile?rev=161886&view=auto</a><br>
==============================================================================<br>
--- clang-tools-extra/trunk/toolTemplate/Makefile (added)<br>
+++ clang-tools-extra/trunk/toolTemplate/Makefile Tue Aug 14 13:53:39 2012<br>
@@ -0,0 +1,24 @@<br>
+##===-------- tools/toolTemplate/Makefile ------------------*- Makefile -*-===##<br>
+#<br>
+# The LLVM Compiler Infrastructure<br>
+#<br>
+# This file is distributed under the University of Illinois Open Source<br>
+# License. See LICENSE.TXT for details.<br>
+#<br>
+##===----------------------------------------------------------------------===##<br>
+<br>
+CLANG_LEVEL := ../../..<br>
+<br>
+TOOLNAME = tool-template<br>
+NO_INSTALL = 1<br>
+<br>
+# No plugins, optimize startup time.<br>
+TOOL_NO_EXPORTS = 1<br>
+<br>
+include $(CLANG_LEVEL)/../../Makefile.config<br>
+LINK_COMPONENTS := $(TARGETS_TO_BUILD) asmparser support mc<br>
+USEDLIBS = clangTooling.a clangFrontend.a clangSerialization.a clangDriver.a \<br>
+ clangRewrite.a clangParse.a clangSema.a clangAnalysis.a \<br>
+ clangAST.a clangASTMatchers.a clangEdit.a clangLex.a clangBasic.a<br>
+<br>
+include $(CLANG_LEVEL)/Makefile<br>
<br>
Added: clang-tools-extra/trunk/toolTemplate/ToolTemplate.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/toolTemplate/ToolTemplate.cpp?rev=161886&view=auto" target="_blank" class="cremed">http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/toolTemplate/ToolTemplate.cpp?rev=161886&view=auto</a><br>
==============================================================================<br>
--- clang-tools-extra/trunk/toolTemplate/ToolTemplate.cpp (added)<br>
+++ clang-tools-extra/trunk/toolTemplate/ToolTemplate.cpp Tue Aug 14 13:53:39 2012<br>
@@ -0,0 +1,105 @@<br>
+//===- tools/extra/RefactoringTemplate.cpp - Template for refactoring tool ===//<br>
+//<br>
+// The LLVM Compiler Infrastructure<br>
+//<br>
+// This file is distributed under the University of Illinois Open Source<br>
+// License. See LICENSE.TXT for details.<br>
+//<br>
+//===----------------------------------------------------------------------===//<br>
+//<br>
+// This file implements an empty refactoring tool using the clang tooling.<br>
+// The goal is to lower the "barrier to entry" for writing refactoring tools.<br>
+//<br>
+// Usage:<br>
+// refactoringTemplate <cmake-output-dir> <file1> <file2> ...<br>
+//<br>
+// Where <cmake-output-dir> is a CMake build directory in which a file named<br>
+// compile_commands.json exists (enable -DCMAKE_EXPORT_COMPILE_COMMANDS in<br>
+// CMake to get this output).<br>
+//<br>
+// <file1> ... specify the paths of files in the CMake source tree. This path<br>
+// is looked up in the compile command database. If the path of a file is<br>
+// absolute, it needs to point into CMake's source tree. If the path is<br>
+// relative, the current working directory needs to be in the CMake source<br>
+// tree and the file must be in a subdirectory of the current working<br>
+// directory. "./" prefixes in the relative files will be automatically<br>
+// removed, but the rest of a relative path must be a suffix of a path in<br>
+// the compile command line database.<br>
+//<br>
+// For example, to use refactoringTemplate on all files in a subtree of the<br>
+// source tree, use:<br>
+//<br>
+// /path/in/subtree $ find . -name '*.cpp'|<br>
+// xargs refactoringTemplate /path/to/build<br>
+//<br>
+//===----------------------------------------------------------------------===//<br>
+<br>
+#include "clang/ASTMatchers/ASTMatchers.h"<br>
+#include "clang/ASTMatchers/ASTMatchFinder.h"<br>
+#include "clang/Basic/SourceManager.h"<br>
+#include "clang/Frontend/FrontendActions.h"<br>
+#include "clang/Lex/Lexer.h"<br>
+#include "clang/Tooling/CompilationDatabase.h"<br>
+#include "clang/Tooling/Refactoring.h"<br>
+#include "clang/Tooling/Tooling.h"<br>
+#include "llvm/ADT/OwningPtr.h"<br>
+#include "llvm/Support/CommandLine.h"<br>
+#include "llvm/Support/MemoryBuffer.h"<br>
+<br>
+using namespace clang;<br>
+using namespace clang::ast_matchers;<br>
+using namespace clang::tooling;<br>
+using namespace llvm;<br>
+<br>
+namespace {<br>
+class TemplateCallback : public MatchFinder::MatchCallback {<br>
+ public:<br>
+ TemplateCallback(Replacements *Replace) : Replace(Replace) {}<br>
+<br>
+ virtual void run(const MatchFinder::MatchResult &Result) {<br>
+// TODO: This routine will get called for each thing that the matchers find.<br>
+// At this point, you can examine the match, and do whatever you want,<br>
+// including replacing the matched text with other text<br>
+ }<br>
+<br>
+ private:<br>
+ Replacements *Replace;<br>
+};<br>
+} // end anonymous namespace<br>
+<br>
+// Set up the command line options<br>
+cl::opt<std::string> BuildPath(<br>
+ cl::Positional,<br>
+ cl::desc("<build-path>"));<br>
+<br>
+cl::list<std::string> SourcePaths(<br>
+ cl::Positional,<br>
+ cl::desc("<source0> [... <sourceN>]"),<br>
+ cl::OneOrMore);<br>
+<br>
+int main(int argc, const char **argv) {<br>
+ llvm::OwningPtr<CompilationDatabase> Compilations(<br>
+ FixedCompilationDatabase::loadFromCommandLine(argc, argv));<br>
+ cl::ParseCommandLineOptions(argc, argv);<br>
+ if (!Compilations) { // Couldn't find a compilation DB from the command line<br>
+ std::string ErrorMessage;<br>
+ Compilations.reset(<br>
+ !BuildPath.empty() ?<br>
+ CompilationDatabase::autoDetectFromDirectory(BuildPath, ErrorMessage) :<br>
+ CompilationDatabase::autoDetectFromSource(SourcePaths[0], ErrorMessage)<br>
+ );<br>
+<br>
+// Still no compilation DB? - bail.<br>
+ if (!Compilations)<br>
+ llvm::report_fatal_error(ErrorMessage);<br>
+ }<br>
+ RefactoringTool Tool(*Compilations, SourcePaths);<br>
+ ast_matchers::MatchFinder Finder;<br>
+ TemplateCallback Callback(&Tool.getReplacements());<br>
+<br>
+// TODO: Put your matchers here.<br>
+// Use Finder.addMatcher(...) to define the patterns in the AST that you<br>
+// want to match against. You are not limited to just one matcher!<br>
+<br>
+ return Tool.run(newFrontendActionFactory(&Finder));<br>
+}<br>
<br>
<br>
_______________________________________________<br>
cfe-commits mailing list<br>
<a href="mailto:cfe-commits@cs.uiuc.edu" class="cremed">cfe-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits" target="_blank" class="cremed">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits</a><br>
</blockquote></div><br></div>