[PATCH] D89148: [SyntaxTree] Artificial use of the Mutations API.

2020-10-14 Thread Eduardo Caldas via Phabricator via cfe-commits
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 ,
+  TranslationUnit *Root) {
+auto *BOE = cast(
+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 ,
+ 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 , syntax::Statement *S);
 
+void parenthesizeRHS(syntax::Arena , 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 ,
+  TranslationUnit *Root) {
+auto *BOE = cast(
+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: 

[PATCH] D89148: [SyntaxTree] Artificial use of the Mutations API.

2020-10-12 Thread Eduardo Caldas via Phabricator via cfe-commits
eduucaldas added a subscriber: gribozavr2.
eduucaldas added a comment.

This patch implements the use case we discussed. It is merely for illustration.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D89148

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D89148: [SyntaxTree] Artificial use of the Mutations API.

2020-10-10 Thread Eduardo Caldas via Phabricator via cfe-commits
eduucaldas updated this revision to Diff 297398.
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 ,
+  TranslationUnit *Root) {
+auto *BOE = cast(
+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"
@@ -96,3 +97,22 @@
 
   MutationsImpl::replace(S, createEmptyStatement(A));
 }
+
+void syntax::parenthesizeRHS(syntax::Arena ,
+ 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 , syntax::Statement *S);
 
+void parenthesizeRHS(syntax::Arena , 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 ,
+  TranslationUnit *Root) {
+auto *BOE = cast(
+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: 

[PATCH] D89148: [SyntaxTree] Artificial use of the Mutations API.

2020-10-09 Thread Eduardo Caldas via Phabricator via cfe-commits
eduucaldas created this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
eduucaldas requested review of this revision.

Not intended for submission.


Repository:
  rG LLVM Github Monorepo

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 ,
+  TranslationUnit *Root) {
+auto *BOE = cast(
+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"
@@ -96,3 +97,22 @@
 
   MutationsImpl::replace(S, createEmptyStatement(A));
 }
+
+void syntax::parenthesizeRHS(syntax::Arena ,
+ 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 , syntax::Statement *S);
 
+void parenthesizeRHS(syntax::Arena , 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 ,
+  TranslationUnit *Root) {
+auto *BOE = cast(
+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