[PATCH] D89148: [SyntaxTree] Artificial use of the Mutations API.
Eduardo Caldas via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Sat Oct 10 01:29:57 PDT 2020
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 &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"
@@ -96,3 +97,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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D89148.297398.patch
Type: text/x-patch
Size: 3170 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20201010/1183acb7/attachment-0001.bin>
More information about the cfe-commits
mailing list