]> granicus.if.org Git - clang/commitdiff
[LibTooling] Add insert/remove convenience functions for creating `ASTEdit`s.
authorYitzhak Mandelbaum <yitzhakm@google.com>
Thu, 6 Jun 2019 14:20:29 +0000 (14:20 +0000)
committerYitzhak Mandelbaum <yitzhakm@google.com>
Thu, 6 Jun 2019 14:20:29 +0000 (14:20 +0000)
Summary: `change()` is an all purpose function; the revision adds simple shortcuts for the specific operations of inserting (before/after) or removing source.

Reviewers: ilya-biryukov

Subscribers: cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D62621

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@362707 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Tooling/Refactoring/Transformer.h
unittests/Tooling/TransformerTest.cpp

index e66dd0550247e7f67082dc4d93ff80416577ad8b..1b71d1541d0bc391c6869d8c9820e32adb776078 100644 (file)
@@ -193,6 +193,23 @@ inline ASTEdit change(TextGenerator Replacement) {
   return change(node(RewriteRule::RootID), std::move(Replacement));
 }
 
+/// Inserts \p Replacement before \p S, leaving the source selected by \S
+/// unchanged.
+inline ASTEdit insertBefore(RangeSelector S, TextGenerator Replacement) {
+  return change(before(std::move(S)), std::move(Replacement));
+}
+
+/// Inserts \p Replacement after \p S, leaving the source selected by \S
+/// unchanged.
+inline ASTEdit insertAfter(RangeSelector S, TextGenerator Replacement) {
+  return change(after(std::move(S)), std::move(Replacement));
+}
+
+/// Removes the source selected by \p S.
+inline ASTEdit remove(RangeSelector S) {
+  return change(std::move(S), text(""));
+}
+
 /// The following three functions are a low-level part of the RewriteRule
 /// API. We expose them for use in implementing the fixtures that interpret
 /// RewriteRule, like Transformer and TransfomerTidy, or for more advanced
index 41c7e7a28bd5a1cd3b67bda57c52e30b5abff873..e9de00d22a55a18783d3165577117642e42dd170 100644 (file)
@@ -349,6 +349,64 @@ TEST_F(TransformerTest, NodePartMemberMultiToken) {
            Input, Expected);
 }
 
+TEST_F(TransformerTest, InsertBeforeEdit) {
+  std::string Input = R"cc(
+    int f() {
+      return 7;
+    }
+  )cc";
+  std::string Expected = R"cc(
+    int f() {
+      int y = 3;
+      return 7;
+    }
+  )cc";
+
+  StringRef Ret = "return";
+  testRule(makeRule(returnStmt().bind(Ret),
+                    insertBefore(statement(Ret), text("int y = 3;"))),
+           Input, Expected);
+}
+
+TEST_F(TransformerTest, InsertAfterEdit) {
+  std::string Input = R"cc(
+    int f() {
+      int x = 5;
+      return 7;
+    }
+  )cc";
+  std::string Expected = R"cc(
+    int f() {
+      int x = 5;
+      int y = 3;
+      return 7;
+    }
+  )cc";
+
+  StringRef Decl = "decl";
+  testRule(makeRule(declStmt().bind(Decl),
+                    insertAfter(statement(Decl), text("int y = 3;"))),
+           Input, Expected);
+}
+
+TEST_F(TransformerTest, RemoveEdit) {
+  std::string Input = R"cc(
+    int f() {
+      int x = 5;
+      return 7;
+    }
+  )cc";
+  std::string Expected = R"cc(
+    int f() {
+      return 7;
+    }
+  )cc";
+
+  StringRef Decl = "decl";
+  testRule(makeRule(declStmt().bind(Decl), remove(statement(Decl))), Input,
+           Expected);
+}
+
 TEST_F(TransformerTest, MultiChange) {
   std::string Input = R"cc(
     void foo() {