[LibTooling] Add insert/remove convenience functions for creating `ASTEdit`s.
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
llvm-svn: 362707
diff --git a/clang/unittests/Tooling/TransformerTest.cpp b/clang/unittests/Tooling/TransformerTest.cpp
index 41c7e7a..e9de00d 100644
--- a/clang/unittests/Tooling/TransformerTest.cpp
+++ b/clang/unittests/Tooling/TransformerTest.cpp
@@ -349,6 +349,64 @@
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() {