[llvm-branch-commits] [cfe-branch] r157016 - /cfe/branches/tooling/examples/rename-method/RenameMethod.cpp

Chandler Carruth chandlerc at gmail.com
Thu May 17 14:14:15 PDT 2012


Author: chandlerc
Date: Thu May 17 16:14:15 2012
New Revision: 157016

URL: http://llvm.org/viewvc/llvm-project?rev=157016&view=rev
Log:
Cleanup some whitespace, remove all the windows line endings (I'm looking at
you Manuel), and implement declaration renaming as well as call renaming.

Modified:
    cfe/branches/tooling/examples/rename-method/RenameMethod.cpp

Modified: cfe/branches/tooling/examples/rename-method/RenameMethod.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/tooling/examples/rename-method/RenameMethod.cpp?rev=157016&r1=157015&r2=157016&view=diff
==============================================================================
--- cfe/branches/tooling/examples/rename-method/RenameMethod.cpp (original)
+++ cfe/branches/tooling/examples/rename-method/RenameMethod.cpp Thu May 17 16:14:15 2012
@@ -20,8 +20,8 @@
 
 using namespace clang;
 using namespace clang::ast_matchers;
-using namespace llvm;
 using namespace clang::tooling;
+using namespace llvm;
 
 cl::opt<std::string> BuildPath(
   cl::Positional,
@@ -34,34 +34,66 @@
 
 // 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;
+class CallRenamer : public MatchFinder::MatchCallback {
+public:
+  CallRenamer(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;
+};
+
+// Implements a callback that replaces the decls for the AST
+// nodes we matched.
+class DeclRenamer : public MatchFinder::MatchCallback {
+public:
+  DeclRenamer(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 CXXMethodDecl *D = Result.Nodes.getDeclAs<CXXMethodDecl>("method");
+    // We can assume D 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 declarator identifier...
+                  CharSourceRange::getTokenRange(
+                    SourceRange(D->getLocation())),
+                  // ... 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) {
@@ -84,22 +116,30 @@
     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);
-
+  ast_matchers::MatchFinder Finder;
+  CallRenamer CallCallback(&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()))),
+    &CallCallback);
+
+  DeclRenamer DeclCallback(&Tool.getReplacements());
+  Finder.addMatcher(
+    // Match declarations...
+    Id("method", Method(HasName("Get"),
+                        OfClass(IsDerivedFrom("ElementsBase")))),
+    &DeclCallback);
+
   return Tool.run(newFrontendActionFactory(&Finder));
 }





More information about the llvm-branch-commits mailing list