[cfe-commits] r160351 - in /cfe/trunk: include/clang/ASTMatchers/RefactoringCallbacks.h include/clang/Tooling/RefactoringCallbacks.h lib/ASTMatchers/CMakeLists.txt lib/ASTMatchers/RefactoringCallbacks.cpp lib/Tooling/CMakeLists.txt lib/Tooling/RefactoringCallbacks.cpp unittests/ASTMatchers/CMakeLists.txt unittests/ASTMatchers/RefactoringCallbacksTest.cpp unittests/Tooling/CMakeLists.txt unittests/Tooling/RefactoringCallbacksTest.cpp

Daniel Jasper djasper at google.com
Tue Jul 17 01:03:01 PDT 2012


Author: djasper
Date: Tue Jul 17 03:03:01 2012
New Revision: 160351

URL: http://llvm.org/viewvc/llvm-project?rev=160351&view=rev
Log:
Move RefactoringCallbacks to Tooling to avoid dependency from
ASTMatchers (lower level abstraction) to Tooling (higher level
abstraction).

Added:
    cfe/trunk/include/clang/Tooling/RefactoringCallbacks.h
      - copied, changed from r160348, cfe/trunk/include/clang/ASTMatchers/RefactoringCallbacks.h
    cfe/trunk/lib/Tooling/RefactoringCallbacks.cpp
      - copied, changed from r160348, cfe/trunk/lib/ASTMatchers/RefactoringCallbacks.cpp
    cfe/trunk/unittests/Tooling/RefactoringCallbacksTest.cpp
      - copied, changed from r160348, cfe/trunk/unittests/ASTMatchers/RefactoringCallbacksTest.cpp
Removed:
    cfe/trunk/include/clang/ASTMatchers/RefactoringCallbacks.h
    cfe/trunk/lib/ASTMatchers/RefactoringCallbacks.cpp
    cfe/trunk/unittests/ASTMatchers/RefactoringCallbacksTest.cpp
Modified:
    cfe/trunk/lib/ASTMatchers/CMakeLists.txt
    cfe/trunk/lib/Tooling/CMakeLists.txt
    cfe/trunk/unittests/ASTMatchers/CMakeLists.txt
    cfe/trunk/unittests/Tooling/CMakeLists.txt

Removed: cfe/trunk/include/clang/ASTMatchers/RefactoringCallbacks.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/ASTMatchers/RefactoringCallbacks.h?rev=160350&view=auto
==============================================================================
--- cfe/trunk/include/clang/ASTMatchers/RefactoringCallbacks.h (original)
+++ cfe/trunk/include/clang/ASTMatchers/RefactoringCallbacks.h (removed)
@@ -1,90 +0,0 @@
-//===--- RefactoringCallbacks.h - Structural query framework ----*- C++ -*-===//
-//
-//                     The LLVM Compiler Infrastructure
-//
-// This file is distributed under the University of Illinois Open Source
-// License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-//
-//  Provides callbacks to make common kinds of refactorings easy.
-//
-//  The general idea is to construct a matcher expression that describes a
-//  subtree match on the AST and then replace the corresponding source code
-//  either by some specific text or some other AST node.
-//
-//  Example:
-//  int main(int argc, char **argv) {
-//    ClangTool Tool(argc, argv);
-//    MatchFinder Finder;
-//    ReplaceStmtWithText Callback("integer", "42");
-//    Finder.AddMatcher(id("integer", expression(integerLiteral())), Callback);
-//    return Tool.run(newFrontendActionFactory(&Finder));
-//  }
-//
-//  This will replace all integer literals with "42".
-//
-//===----------------------------------------------------------------------===//
-
-#ifndef LLVM_CLANG_AST_MATCHERS_REFACTORING_CALLBACKS_H
-#define LLVM_CLANG_AST_MATCHERS_REFACTORING_CALLBACKS_H
-
-#include "clang/ASTMatchers/ASTMatchFinder.h"
-#include "clang/Tooling/Refactoring.h"
-
-namespace clang {
-namespace ast_matchers {
-
-/// \brief Base class for RefactoringCallbacks.
-///
-/// Collects \c tooling::Replacements while running.
-class RefactoringCallback : public MatchFinder::MatchCallback {
-public:
-  RefactoringCallback();
-  tooling::Replacements &getReplacements();
-
-protected:
-  tooling::Replacements Replace;
-};
-
-/// \brief Replace the text of the statement bound to \c FromId with the text in
-/// \c ToText.
-class ReplaceStmtWithText : public RefactoringCallback {
-public:
-  ReplaceStmtWithText(StringRef FromId, StringRef ToText);
-  virtual void run(const MatchFinder::MatchResult &Result);
-
-private:
-  std::string FromId;
-  std::string ToText;
-};
-
-/// \brief Replace the text of the statement bound to \c FromId with the text of
-/// the statement bound to \c ToId.
-class ReplaceStmtWithStmt : public RefactoringCallback {
-public:
-  ReplaceStmtWithStmt(StringRef FromId, StringRef ToId);
-  virtual void run(const MatchFinder::MatchResult &Result);
-
-private:
-  std::string FromId;
-  std::string ToId;
-};
-
-/// \brief Replace an if-statement bound to \c Id with the outdented text of its
-/// body, choosing the consequent or the alternative based on whether
-/// \c PickTrueBranch is true.
-class ReplaceIfStmtWithItsBody : public RefactoringCallback {
-public:
-  ReplaceIfStmtWithItsBody(StringRef Id, bool PickTrueBranch);
-  virtual void run(const MatchFinder::MatchResult &Result);
-
-private:
-  std::string Id;
-  const bool PickTrueBranch;
-};
-
-} // end namespace ast_matchers
-} // end namespace clang
-
-#endif // LLVM_CLANG_AST_MATCHERS_REFACTORING_CALLBACKS_H

Copied: cfe/trunk/include/clang/Tooling/RefactoringCallbacks.h (from r160348, cfe/trunk/include/clang/ASTMatchers/RefactoringCallbacks.h)
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Tooling/RefactoringCallbacks.h?p2=cfe/trunk/include/clang/Tooling/RefactoringCallbacks.h&p1=cfe/trunk/include/clang/ASTMatchers/RefactoringCallbacks.h&r1=160348&r2=160351&rev=160351&view=diff
==============================================================================
--- cfe/trunk/include/clang/ASTMatchers/RefactoringCallbacks.h (original)
+++ cfe/trunk/include/clang/Tooling/RefactoringCallbacks.h Tue Jul 17 03:03:01 2012
@@ -26,8 +26,8 @@
 //
 //===----------------------------------------------------------------------===//
 
-#ifndef LLVM_CLANG_AST_MATCHERS_REFACTORING_CALLBACKS_H
-#define LLVM_CLANG_AST_MATCHERS_REFACTORING_CALLBACKS_H
+#ifndef LLVM_CLANG_TOOLING_REFACTORING_CALLBACKS_H
+#define LLVM_CLANG_TOOLING_REFACTORING_CALLBACKS_H
 
 #include "clang/ASTMatchers/ASTMatchFinder.h"
 #include "clang/Tooling/Refactoring.h"
@@ -87,4 +87,4 @@
 } // end namespace ast_matchers
 } // end namespace clang
 
-#endif // LLVM_CLANG_AST_MATCHERS_REFACTORING_CALLBACKS_H
+#endif // LLVM_CLANG_TOOLING_REFACTORING_CALLBACKS_H

Modified: cfe/trunk/lib/ASTMatchers/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/ASTMatchers/CMakeLists.txt?rev=160351&r1=160350&r2=160351&view=diff
==============================================================================
--- cfe/trunk/lib/ASTMatchers/CMakeLists.txt (original)
+++ cfe/trunk/lib/ASTMatchers/CMakeLists.txt Tue Jul 17 03:03:01 2012
@@ -4,7 +4,6 @@
 add_clang_library(clangASTMatchers
   ASTMatchFinder.cpp
   ASTMatchersInternal.cpp
-  RefactoringCallbacks.cpp
   )
 
 add_dependencies(clangASTMatchers

Removed: cfe/trunk/lib/ASTMatchers/RefactoringCallbacks.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/ASTMatchers/RefactoringCallbacks.cpp?rev=160350&view=auto
==============================================================================
--- cfe/trunk/lib/ASTMatchers/RefactoringCallbacks.cpp (original)
+++ cfe/trunk/lib/ASTMatchers/RefactoringCallbacks.cpp (removed)
@@ -1,78 +0,0 @@
-//===--- RefactoringCallbacks.cpp - Structural query framework ------------===//
-//
-//                     The LLVM Compiler Infrastructure
-//
-// This file is distributed under the University of Illinois Open Source
-// License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-//
-//
-//===----------------------------------------------------------------------===//
-#include "clang/Lex/Lexer.h"
-#include "clang/ASTMatchers/RefactoringCallbacks.h"
-
-namespace clang {
-namespace ast_matchers {
-
-RefactoringCallback::RefactoringCallback() {}
-tooling::Replacements &RefactoringCallback::getReplacements() {
-  return Replace;
-}
-
-static tooling::Replacement replaceStmtWithText(SourceManager &Sources,
-                                                const Stmt &From,
-                                                StringRef Text) {
-  return tooling::Replacement(Sources, CharSourceRange::getTokenRange(
-      From.getSourceRange()), Text);
-}
-static tooling::Replacement replaceStmtWithStmt(SourceManager &Sources,
-                                                const Stmt &From,
-                                                const Stmt &To) {
-  return replaceStmtWithText(Sources, From, Lexer::getSourceText(
-      CharSourceRange::getTokenRange(To.getSourceRange()),
-      Sources, LangOptions()));
-}
-
-ReplaceStmtWithText::ReplaceStmtWithText(StringRef FromId, StringRef ToText)
-    : FromId(FromId), ToText(ToText) {}
-
-void ReplaceStmtWithText::run(const MatchFinder::MatchResult &Result) {
-  if (const Stmt *FromMatch = Result.Nodes.getStmtAs<Stmt>(FromId)) {
-    Replace.insert(tooling::Replacement(
-        *Result.SourceManager,
-        CharSourceRange::getTokenRange(FromMatch->getSourceRange()),
-        ToText));
-  }
-}
-
-ReplaceStmtWithStmt::ReplaceStmtWithStmt(StringRef FromId, StringRef ToId)
-    : FromId(FromId), ToId(ToId) {}
-
-void ReplaceStmtWithStmt::run(const MatchFinder::MatchResult &Result) {
-  const Stmt *FromMatch = Result.Nodes.getStmtAs<Stmt>(FromId);
-  const Stmt *ToMatch = Result.Nodes.getStmtAs<Stmt>(ToId);
-  if (FromMatch && ToMatch)
-    Replace.insert(replaceStmtWithStmt(
-        *Result.SourceManager, *FromMatch, *ToMatch));
-}
-
-ReplaceIfStmtWithItsBody::ReplaceIfStmtWithItsBody(StringRef Id,
-                                                   bool PickTrueBranch)
-    : Id(Id), PickTrueBranch(PickTrueBranch) {}
-
-void ReplaceIfStmtWithItsBody::run(const MatchFinder::MatchResult &Result) {
-  if (const IfStmt *Node = Result.Nodes.getStmtAs<IfStmt>(Id)) {
-    const Stmt *Body = PickTrueBranch ? Node->getThen() : Node->getElse();
-    if (Body) {
-      Replace.insert(replaceStmtWithStmt(*Result.SourceManager, *Node, *Body));
-    } else if (!PickTrueBranch) {
-      // If we want to use the 'else'-branch, but it doesn't exist, delete
-      // the whole 'if'.
-      Replace.insert(replaceStmtWithText(*Result.SourceManager, *Node, ""));
-    }
-  }
-}
-
-} // end namespace ast_matchers
-} // end namespace clang

Modified: cfe/trunk/lib/Tooling/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Tooling/CMakeLists.txt?rev=160351&r1=160350&r2=160351&view=diff
==============================================================================
--- cfe/trunk/lib/Tooling/CMakeLists.txt (original)
+++ cfe/trunk/lib/Tooling/CMakeLists.txt Tue Jul 17 03:03:01 2012
@@ -6,6 +6,7 @@
   Refactoring.cpp
   Tooling.cpp
   ArgumentsAdjusters.cpp
+  RefactoringCallbacks.cpp
   )
 
 add_dependencies(clangTooling
@@ -16,5 +17,6 @@
   clangBasic
   clangFrontend
   clangAST
+  clangASTMatchers
   clangRewrite
   )

Copied: cfe/trunk/lib/Tooling/RefactoringCallbacks.cpp (from r160348, cfe/trunk/lib/ASTMatchers/RefactoringCallbacks.cpp)
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Tooling/RefactoringCallbacks.cpp?p2=cfe/trunk/lib/Tooling/RefactoringCallbacks.cpp&p1=cfe/trunk/lib/ASTMatchers/RefactoringCallbacks.cpp&r1=160348&r2=160351&rev=160351&view=diff
==============================================================================
--- cfe/trunk/lib/ASTMatchers/RefactoringCallbacks.cpp (original)
+++ cfe/trunk/lib/Tooling/RefactoringCallbacks.cpp Tue Jul 17 03:03:01 2012
@@ -10,7 +10,7 @@
 //
 //===----------------------------------------------------------------------===//
 #include "clang/Lex/Lexer.h"
-#include "clang/ASTMatchers/RefactoringCallbacks.h"
+#include "clang/Tooling/RefactoringCallbacks.h"
 
 namespace clang {
 namespace ast_matchers {

Modified: cfe/trunk/unittests/ASTMatchers/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/ASTMatchers/CMakeLists.txt?rev=160351&r1=160350&r2=160351&view=diff
==============================================================================
--- cfe/trunk/unittests/ASTMatchers/CMakeLists.txt (original)
+++ cfe/trunk/unittests/ASTMatchers/CMakeLists.txt Tue Jul 17 03:03:01 2012
@@ -1,6 +1,5 @@
 add_clang_unittest(ASTMatchersTests
-  ASTMatchersTest.cpp
-  RefactoringCallbacksTest.cpp)
+  ASTMatchersTest.cpp)
 
 target_link_libraries(ASTMatchersTests
   gtest gtest_main clangASTMatchers clangTooling)

Removed: cfe/trunk/unittests/ASTMatchers/RefactoringCallbacksTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/ASTMatchers/RefactoringCallbacksTest.cpp?rev=160350&view=auto
==============================================================================
--- cfe/trunk/unittests/ASTMatchers/RefactoringCallbacksTest.cpp (original)
+++ cfe/trunk/unittests/ASTMatchers/RefactoringCallbacksTest.cpp (removed)
@@ -1,98 +0,0 @@
-//===- unittest/ASTMatchers/RefactoringCallbacksTest.cpp ------------------===//
-//
-//                     The LLVM Compiler Infrastructure
-//
-// This file is distributed under the University of Illinois Open Source
-// License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-
-#include "clang/ASTMatchers/ASTMatchers.h"
-#include "clang/ASTMatchers/ASTMatchFinder.h"
-#include "clang/ASTMatchers/RefactoringCallbacks.h"
-#include "../Tooling/RewriterTestContext.h"
-#include "gtest/gtest.h"
-
-namespace clang {
-namespace ast_matchers {
-
-template <typename T>
-void expectRewritten(const std::string &Code,
-                     const std::string &Expected,
-                     const T &AMatcher,
-                     RefactoringCallback &Callback) {
-  MatchFinder Finder;
-  Finder.addMatcher(AMatcher, &Callback);
-  OwningPtr<tooling::FrontendActionFactory> Factory(
-      tooling::newFrontendActionFactory(&Finder));
-  ASSERT_TRUE(tooling::runToolOnCode(Factory->create(), Code))
-      << "Parsing error in \"" << Code << "\"";
-  RewriterTestContext Context;
-  FileID ID = Context.createInMemoryFile("input.cc", Code);
-  EXPECT_TRUE(tooling::applyAllReplacements(Callback.getReplacements(),
-                                            Context.Rewrite));
-  EXPECT_EQ(Expected, Context.getRewrittenText(ID));
-}
-
-TEST(RefactoringCallbacksTest, ReplacesStmtsWithString) {
-  std::string Code = "void f() { int i = 1; }";
-  std::string Expected = "void f() { ; }";
-  ReplaceStmtWithText Callback("id", ";");
-  expectRewritten(Code, Expected, id("id", declarationStatement()), Callback);
-}
-
-TEST(RefactoringCallbacksTest, ReplacesStmtsInCalledMacros) {
-  std::string Code = "#define A void f() { int i = 1; }\nA";
-  std::string Expected = "#define A void f() { ; }\nA";
-  ReplaceStmtWithText Callback("id", ";");
-  expectRewritten(Code, Expected, id("id", declarationStatement()), Callback);
-}
-
-TEST(RefactoringCallbacksTest, IgnoresStmtsInUncalledMacros) {
-  std::string Code = "#define A void f() { int i = 1; }";
-  std::string Expected = "#define A void f() { int i = 1; }";
-  ReplaceStmtWithText Callback("id", ";");
-  expectRewritten(Code, Expected, id("id", declarationStatement()), Callback);
-}
-
-TEST(RefactoringCallbacksTest, ReplacesInteger) {
-  std::string Code = "void f() { int i = 1; }";
-  std::string Expected = "void f() { int i = 2; }";
-  ReplaceStmtWithText Callback("id", "2");
-  expectRewritten(Code, Expected, id("id", expression(integerLiteral())),
-                  Callback);
-}
-
-TEST(RefactoringCallbacksTest, ReplacesStmtWithStmt) {
-  std::string Code = "void f() { int i = false ? 1 : i * 2; }";
-  std::string Expected = "void f() { int i = i * 2; }";
-  ReplaceStmtWithStmt Callback("always-false", "should-be");
-  expectRewritten(Code, Expected,
-      id("always-false", conditionalOperator(
-          hasCondition(boolLiteral(equals(false))),
-          hasFalseExpression(id("should-be", expression())))),
-      Callback);
-}
-
-TEST(RefactoringCallbacksTest, ReplacesIfStmt) {
-  std::string Code = "bool a; void f() { if (a) f(); else a = true; }";
-  std::string Expected = "bool a; void f() { f(); }";
-  ReplaceIfStmtWithItsBody Callback("id", true);
-  expectRewritten(Code, Expected,
-      id("id", ifStmt(
-          hasCondition(implicitCast(hasSourceExpression(
-              declarationReference(to(variable(hasName("a"))))))))),
-      Callback);
-}
-
-TEST(RefactoringCallbacksTest, RemovesEntireIfOnEmptyElse) {
-  std::string Code = "void f() { if (false) int i = 0; }";
-  std::string Expected = "void f() {  }";
-  ReplaceIfStmtWithItsBody Callback("id", false);
-  expectRewritten(Code, Expected,
-      id("id", ifStmt(hasCondition(boolLiteral(equals(false))))),
-      Callback);
-}
-
-} // end namespace ast_matchers
-} // end namespace clang

Modified: cfe/trunk/unittests/Tooling/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Tooling/CMakeLists.txt?rev=160351&r1=160350&r2=160351&view=diff
==============================================================================
--- cfe/trunk/unittests/Tooling/CMakeLists.txt (original)
+++ cfe/trunk/unittests/Tooling/CMakeLists.txt Tue Jul 17 03:03:01 2012
@@ -5,6 +5,7 @@
   RecursiveASTVisitorTest.cpp
   RefactoringTest.cpp
   RewriterTest.cpp
+  RefactoringCallbacksTest.cpp
   )
 
 target_link_libraries(ToolingTests

Copied: cfe/trunk/unittests/Tooling/RefactoringCallbacksTest.cpp (from r160348, cfe/trunk/unittests/ASTMatchers/RefactoringCallbacksTest.cpp)
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Tooling/RefactoringCallbacksTest.cpp?p2=cfe/trunk/unittests/Tooling/RefactoringCallbacksTest.cpp&p1=cfe/trunk/unittests/ASTMatchers/RefactoringCallbacksTest.cpp&r1=160348&r2=160351&rev=160351&view=diff
==============================================================================
--- cfe/trunk/unittests/ASTMatchers/RefactoringCallbacksTest.cpp (original)
+++ cfe/trunk/unittests/Tooling/RefactoringCallbacksTest.cpp Tue Jul 17 03:03:01 2012
@@ -9,9 +9,9 @@
 
 #include "clang/ASTMatchers/ASTMatchers.h"
 #include "clang/ASTMatchers/ASTMatchFinder.h"
-#include "clang/ASTMatchers/RefactoringCallbacks.h"
-#include "../Tooling/RewriterTestContext.h"
+#include "clang/Tooling/RefactoringCallbacks.h"
 #include "gtest/gtest.h"
+#include "RewriterTestContext.h"
 
 namespace clang {
 namespace ast_matchers {





More information about the cfe-commits mailing list