[llvm-branch-commits] [cfe-branch] r156992 - in /cfe/branches/tooling/examples: CMakeLists.txt rename-method/ rename-method/CMakeLists.txt rename-method/Makefile rename-method/RenameMethod.cpp

Manuel Klimek klimek at google.com
Thu May 17 08:01:46 PDT 2012


Author: klimek
Date: Thu May 17 10:01:45 2012
New Revision: 156992

URL: http://llvm.org/viewvc/llvm-project?rev=156992&view=rev
Log: (empty)

Added:
    cfe/branches/tooling/examples/rename-method/
    cfe/branches/tooling/examples/rename-method/CMakeLists.txt
    cfe/branches/tooling/examples/rename-method/Makefile
    cfe/branches/tooling/examples/rename-method/RenameMethod.cpp
Modified:
    cfe/branches/tooling/examples/CMakeLists.txt

Modified: cfe/branches/tooling/examples/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/tooling/examples/CMakeLists.txt?rev=156992&r1=156991&r2=156992&view=diff
==============================================================================
--- cfe/branches/tooling/examples/CMakeLists.txt (original)
+++ cfe/branches/tooling/examples/CMakeLists.txt Thu May 17 10:01:45 2012
@@ -1,7 +1,8 @@
-if(NOT CLANG_BUILD_EXAMPLES)
-  set_property(DIRECTORY PROPERTY EXCLUDE_FROM_ALL ON)
-endif()
-
-add_subdirectory(analyzer-plugin)
-add_subdirectory(clang-interpreter)
-add_subdirectory(PrintFunctionNames)
+if(NOT CLANG_BUILD_EXAMPLES)
+  set_property(DIRECTORY PROPERTY EXCLUDE_FROM_ALL ON)
+endif()
+
+add_subdirectory(analyzer-plugin)
+add_subdirectory(clang-interpreter)
+add_subdirectory(PrintFunctionNames)
+add_subdirectory(rename-method)
\ No newline at end of file

Added: cfe/branches/tooling/examples/rename-method/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/tooling/examples/rename-method/CMakeLists.txt?rev=156992&view=auto
==============================================================================
--- cfe/branches/tooling/examples/rename-method/CMakeLists.txt (added)
+++ cfe/branches/tooling/examples/rename-method/CMakeLists.txt Thu May 17 10:01:45 2012
@@ -0,0 +1,5 @@
+set(LLVM_USED_LIBS clangTooling clangBasic clangAST)
+
+add_clang_executable(rename-method
+  RenameMethod.cpp
+  )

Added: cfe/branches/tooling/examples/rename-method/Makefile
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/tooling/examples/rename-method/Makefile?rev=156992&view=auto
==============================================================================
--- cfe/branches/tooling/examples/rename-method/Makefile (added)
+++ cfe/branches/tooling/examples/rename-method/Makefile Thu May 17 10:01:45 2012
@@ -0,0 +1,23 @@
+##===- examples/rename-method/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 = rename-method
+NO_INSTALL = 1
+
+# No plugins, optimize startup time.
+TOOL_NO_EXPORTS = 1
+
+LINK_COMPONENTS := support mc
+USEDLIBS = clangTooling.a clangFrontend.a clangSerialization.a clangDriver.a \
+           clangRewrite.a clangParse.a clangSema.a clangAnalysis.a \
+           clangAST.a clangASTMatchers.a clangLex.a clangBasic.a
+
+include $(CLANG_LEVEL)/Makefile

Added: cfe/branches/tooling/examples/rename-method/RenameMethod.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/tooling/examples/rename-method/RenameMethod.cpp?rev=156992&view=auto
==============================================================================
--- cfe/branches/tooling/examples/rename-method/RenameMethod.cpp (added)
+++ cfe/branches/tooling/examples/rename-method/RenameMethod.cpp Thu May 17 10:01:45 2012
@@ -0,0 +1,105 @@
+//=- examples/rename-method/RenameMethod.cpp ------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// A small example tool that uses AST matchers to find calls to the Get() method
+// in subclasses of ElementsBase and replaces them with calls to Front().
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/ASTMatchers/ASTMatchers.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/Tooling/CompilationDatabase.h"
+#include "clang/Tooling/Refactoring.h"
+#include "llvm/Support/CommandLine.h"
+
+using namespace clang;
+using namespace clang::ast_matchers;
+using namespace llvm;
+using namespace clang::tooling;
+
+cl::opt<std::string> BuildPath(
+  cl::Positional,
+  cl::desc("<build-path>"));
+
+cl::list<std::string> SourcePaths(
+  cl::Positional,
+  cl::desc("<source0> [... <sourceN>]"),
+  cl::OneOrMore);
+
+// Implements a callback that replaces the calls for the AST
+// nodes we matched.
+class Renamer : public MatchFinder::MatchCallback {
+public:
+  Renamer(Replacements *Replace) : Replace(Replace) {}
+
+  // This method is called every time the registered matcher matches
+  // on the AST.
+	virtual void run(const MatchFinder::MatchResult &Result) {
+    const MemberExpr *M = Result.Nodes.getStmtAs<MemberExpr>("member");
+    // We can assume M is non-null, because the ast matchers guarantee
+    // that a node with this type was bound, as the matcher would otherwise
+    // not match.
+
+    Replace->insert(
+      // Replacements are a source manager independent way to express
+      // transformation on the source.
+      Replacement(*Result.SourceManager,
+                  // Replace the range of the member name...
+                  CharSourceRange::getTokenRange(
+                    SourceRange(M->getMemberLoc())),
+                  // ... with "Front".
+                  "Front"));
+  }
+
+private:
+  // Replacements are the RefactoringTool's way to keep track of code
+  // transformations, deduplicate them and apply them to the code when
+  // the tool has finished with all translation units.
+  Replacements *Replace;
+};
+
+int main(int argc, const char **argv) {
+  // First see if we can create the compile command line from the
+  // positional parameters after "--".
+  OwningPtr<CompilationDatabase> Compilations(
+    FixedCompilationDatabase::loadFromCommandLine(argc, argv));
+
+  // Do normal command line handling from the rest of the arguments.
+  cl::ParseCommandLineOptions(argc, argv);
+
+  if (!Compilations) {
+    // If the caller didn't specify a compile command line to use, try to
+    // load it from a build directory. For example when running cmake, use
+    // CMAKE_EXPORT_COMPILE_COMMANDS=ON to prepare your build directory to
+    // be useable with clang tools.
+    std::string ErrorMessage;
+    Compilations.reset(CompilationDatabase::loadFromDirectory(BuildPath,
+                                                              ErrorMessage));
+    if (!Compilations)
+      llvm::report_fatal_error(ErrorMessage);
+  }
+  RefactoringTool Tool(*Compilations, SourcePaths);
+  ast_matchers::MatchFinder Finder;
+  Renamer Callback(&Tool.getReplacements());
+  Finder.addMatcher(
+    // Match calls...
+    Call(
+      // Where the callee is a method called "Get"...
+      Callee(Method(HasName("Get"))),
+      // ... and the class on which the method is called is derived
+      // from ElementsBase ...
+      ThisPointerType(Class(
+        IsDerivedFrom("ElementsBase"))),
+      // ... and bind the member expression to the ID "member", under which
+      // it can later be found in the callback.
+      Callee(Id("member", MemberExpression()))),
+    &Callback);
+
+  return Tool.run(newFrontendActionFactory(&Finder));
+}





More information about the llvm-branch-commits mailing list