eduucaldas updated this revision to Diff 298092.
eduucaldas added a comment.

Rebase


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D89148/new/

https://reviews.llvm.org/D89148

Files:
  clang/include/clang/Tooling/Syntax/Mutations.h
  clang/lib/Tooling/Syntax/Mutations.cpp
  clang/unittests/Tooling/Syntax/MutationsTest.cpp


Index: clang/unittests/Tooling/Syntax/MutationsTest.cpp
===================================================================
--- clang/unittests/Tooling/Syntax/MutationsTest.cpp
+++ clang/unittests/Tooling/Syntax/MutationsTest.cpp
@@ -52,6 +52,16 @@
     EXPECT_FALSE(S->isOriginal())
         << "node removed from tree cannot be marked as original";
   };
+
+  Transformation ParenthesizeRHS = [this](const llvm::Annotations &Input,
+                                          TranslationUnit *Root) {
+    auto *BOE = cast<syntax::BinaryOperatorExpression>(
+        nodeByRange(Input.range(), Root));
+    ASSERT_TRUE(BOE->canModify()) << "cannot remove a statement";
+    syntax::parenthesizeRHS(*Arena, BOE);
+    EXPECT_FALSE(BOE->isOriginal())
+        << "modified node cannot be marked as original";
+  };
 };
 
 INSTANTIATE_TEST_CASE_P(SyntaxTreeTests, MutationTest,
@@ -71,4 +81,22 @@
   CheckTransformation(RemoveStatement, "void test() { if (1) [[{}]] else {} }",
                       "void test() { if (1) ; else {} }");
 }
+
+TEST_P(MutationTest, Parenthesize_RHS) {
+  CheckTransformation(ParenthesizeRHS, R"cpp(void test() { [[1 + 2]]; })cpp",
+                      "void test() { 1 + (2); }");
+}
+
+TEST_P(MutationTest, Parenthesize_RHS_MACRO) {
+  CheckTransformation(ParenthesizeRHS, R"cpp(
+#define THENUMBER 42
+void test() {
+  [[1 + (THENUMBER)]];
+})cpp",
+                      R"cpp(
+#define THENUMBER 42
+void test() {
+  1 + ((THENUMBER));
+})cpp");
+}
 } // namespace
Index: clang/lib/Tooling/Syntax/Mutations.cpp
===================================================================
--- clang/lib/Tooling/Syntax/Mutations.cpp
+++ clang/lib/Tooling/Syntax/Mutations.cpp
@@ -8,6 +8,7 @@
 #include "clang/Tooling/Syntax/Mutations.h"
 #include "clang/Basic/LLVM.h"
 #include "clang/Basic/SourceLocation.h"
+#include "clang/Basic/TokenKinds.h"
 #include "clang/Lex/Token.h"
 #include "clang/Tooling/Core/Replacement.h"
 #include "clang/Tooling/Syntax/BuildTree.h"
@@ -102,3 +103,22 @@
 
   MutationsImpl::replace(S, createEmptyStatement(A));
 }
+
+void syntax::parenthesizeRHS(syntax::Arena &A,
+                             syntax::BinaryOperatorExpression *BOE) {
+  assert(BOE);
+  assert(BOE->canModify());
+
+  auto *RHS = BOE->getRhs();
+  auto *BeforeRHS = findPrevious(RHS);
+  MutationsImpl::remove(RHS);
+
+  auto *NewRHS =
+      createTree(A,
+                 {{createLeaf(A, tok::l_paren), NodeRole::OpenParen},
+                  {RHS, NodeRole::SubExpression},
+                  {createLeaf(A, tok::r_paren), NodeRole::CloseParen}},
+                 NodeKind::ParenExpression);
+
+  MutationsImpl::addAfter(BeforeRHS, NewRHS, NodeRole::RightHandSide);
+}
Index: clang/include/clang/Tooling/Syntax/Mutations.h
===================================================================
--- clang/include/clang/Tooling/Syntax/Mutations.h
+++ clang/include/clang/Tooling/Syntax/Mutations.h
@@ -31,6 +31,7 @@
 /// EXPECTS: S->canModify() == true
 void removeStatement(syntax::Arena &A, syntax::Statement *S);
 
+void parenthesizeRHS(syntax::Arena &A, syntax::BinaryOperatorExpression *BOE);
 } // namespace syntax
 } // namespace clang
 


Index: clang/unittests/Tooling/Syntax/MutationsTest.cpp
===================================================================
--- clang/unittests/Tooling/Syntax/MutationsTest.cpp
+++ clang/unittests/Tooling/Syntax/MutationsTest.cpp
@@ -52,6 +52,16 @@
     EXPECT_FALSE(S->isOriginal())
         << "node removed from tree cannot be marked as original";
   };
+
+  Transformation ParenthesizeRHS = [this](const llvm::Annotations &Input,
+                                          TranslationUnit *Root) {
+    auto *BOE = cast<syntax::BinaryOperatorExpression>(
+        nodeByRange(Input.range(), Root));
+    ASSERT_TRUE(BOE->canModify()) << "cannot remove a statement";
+    syntax::parenthesizeRHS(*Arena, BOE);
+    EXPECT_FALSE(BOE->isOriginal())
+        << "modified node cannot be marked as original";
+  };
 };
 
 INSTANTIATE_TEST_CASE_P(SyntaxTreeTests, MutationTest,
@@ -71,4 +81,22 @@
   CheckTransformation(RemoveStatement, "void test() { if (1) [[{}]] else {} }",
                       "void test() { if (1) ; else {} }");
 }
+
+TEST_P(MutationTest, Parenthesize_RHS) {
+  CheckTransformation(ParenthesizeRHS, R"cpp(void test() { [[1 + 2]]; })cpp",
+                      "void test() { 1 + (2); }");
+}
+
+TEST_P(MutationTest, Parenthesize_RHS_MACRO) {
+  CheckTransformation(ParenthesizeRHS, R"cpp(
+#define THENUMBER 42
+void test() {
+  [[1 + (THENUMBER)]];
+})cpp",
+                      R"cpp(
+#define THENUMBER 42
+void test() {
+  1 + ((THENUMBER));
+})cpp");
+}
 } // namespace
Index: clang/lib/Tooling/Syntax/Mutations.cpp
===================================================================
--- clang/lib/Tooling/Syntax/Mutations.cpp
+++ clang/lib/Tooling/Syntax/Mutations.cpp
@@ -8,6 +8,7 @@
 #include "clang/Tooling/Syntax/Mutations.h"
 #include "clang/Basic/LLVM.h"
 #include "clang/Basic/SourceLocation.h"
+#include "clang/Basic/TokenKinds.h"
 #include "clang/Lex/Token.h"
 #include "clang/Tooling/Core/Replacement.h"
 #include "clang/Tooling/Syntax/BuildTree.h"
@@ -102,3 +103,22 @@
 
   MutationsImpl::replace(S, createEmptyStatement(A));
 }
+
+void syntax::parenthesizeRHS(syntax::Arena &A,
+                             syntax::BinaryOperatorExpression *BOE) {
+  assert(BOE);
+  assert(BOE->canModify());
+
+  auto *RHS = BOE->getRhs();
+  auto *BeforeRHS = findPrevious(RHS);
+  MutationsImpl::remove(RHS);
+
+  auto *NewRHS =
+      createTree(A,
+                 {{createLeaf(A, tok::l_paren), NodeRole::OpenParen},
+                  {RHS, NodeRole::SubExpression},
+                  {createLeaf(A, tok::r_paren), NodeRole::CloseParen}},
+                 NodeKind::ParenExpression);
+
+  MutationsImpl::addAfter(BeforeRHS, NewRHS, NodeRole::RightHandSide);
+}
Index: clang/include/clang/Tooling/Syntax/Mutations.h
===================================================================
--- clang/include/clang/Tooling/Syntax/Mutations.h
+++ clang/include/clang/Tooling/Syntax/Mutations.h
@@ -31,6 +31,7 @@
 /// EXPECTS: S->canModify() == true
 void removeStatement(syntax::Arena &A, syntax::Statement *S);
 
+void parenthesizeRHS(syntax::Arena &A, syntax::BinaryOperatorExpression *BOE);
 } // namespace syntax
 } // namespace clang
 
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to