Author: Eduardo Caldas Date: 2020-09-11T20:37:23Z New Revision: 7c37b82f5ba5883b331608b0077c0b30bf301874
URL: https://github.com/llvm/llvm-project/commit/7c37b82f5ba5883b331608b0077c0b30bf301874 DIFF: https://github.com/llvm/llvm-project/commit/7c37b82f5ba5883b331608b0077c0b30bf301874.diff LOG: [SyntaxTree][Synthesis] Add support for Tree. In a future patch * Implement helper function to generate Trees for tests * and test Tree methods, namely `findFirstLeaf` and `findLastLeaf` Differential Revision: https://reviews.llvm.org/D87533 Added: Modified: clang/include/clang/Tooling/Syntax/BuildTree.h clang/lib/Tooling/Syntax/Synthesis.cpp clang/unittests/Tooling/Syntax/SynthesisTest.cpp Removed: ################################################################################ diff --git a/clang/include/clang/Tooling/Syntax/BuildTree.h b/clang/include/clang/Tooling/Syntax/BuildTree.h index c2ae4348bc16..b9405167bf99 100644 --- a/clang/include/clang/Tooling/Syntax/BuildTree.h +++ b/clang/include/clang/Tooling/Syntax/BuildTree.h @@ -34,6 +34,12 @@ syntax::Leaf *createLeaf(syntax::Arena &A, tok::TokenKind K, /// this token syntax::Leaf *createLeaf(syntax::Arena &A, tok::TokenKind K); +// Synthesis of Trees +syntax::Tree * +createTree(Arena &A, + std::vector<std::pair<syntax::Node *, syntax::NodeRole>> Children, + syntax::NodeKind K); + // Synthesis of Syntax Nodes clang::syntax::EmptyStatement *createEmptyStatement(clang::syntax::Arena &A); diff --git a/clang/lib/Tooling/Syntax/Synthesis.cpp b/clang/lib/Tooling/Syntax/Synthesis.cpp index 772429ff4c46..6de3d5b5752d 100644 --- a/clang/lib/Tooling/Syntax/Synthesis.cpp +++ b/clang/lib/Tooling/Syntax/Synthesis.cpp @@ -52,6 +52,20 @@ syntax::Leaf *clang::syntax::createLeaf(syntax::Arena &A, tok::TokenKind K) { return createLeaf(A, K, Spelling); } +syntax::Tree *clang::syntax::createTree( + syntax::Arena &A, + std::vector<std::pair<syntax::Node *, syntax::NodeRole>> Children, + syntax::NodeKind K) { + auto *T = new (A.getAllocator()) syntax::Tree(K); + FactoryImpl::setCanModify(T); + for (auto ChildIt = Children.rbegin(); ChildIt != Children.rend(); + std::advance(ChildIt, 1)) + FactoryImpl::prependChildLowLevel(T, ChildIt->first, ChildIt->second); + + T->assertInvariants(); + return T; +} + syntax::EmptyStatement *clang::syntax::createEmptyStatement(syntax::Arena &A) { auto *S = new (A.getAllocator()) syntax::EmptyStatement; FactoryImpl::setCanModify(S); diff --git a/clang/unittests/Tooling/Syntax/SynthesisTest.cpp b/clang/unittests/Tooling/Syntax/SynthesisTest.cpp index 1c1aef8bd8c8..a882714ccf33 100644 --- a/clang/unittests/Tooling/Syntax/SynthesisTest.cpp +++ b/clang/unittests/Tooling/Syntax/SynthesisTest.cpp @@ -12,6 +12,7 @@ #include "TreeTestBase.h" #include "clang/Tooling/Syntax/BuildTree.h" +#include "clang/Tooling/Syntax/Nodes.h" #include "gtest/gtest.h" using namespace clang; @@ -80,6 +81,62 @@ TEST_P(SynthesisTest, Leaf_Number) { )txt")); } +TEST_P(SynthesisTest, Tree_Empty) { + buildTree("", GetParam()); + + auto *Tree = createTree(*Arena, {}, NodeKind::UnknownExpression); + + EXPECT_TRUE(treeDumpEqual(Tree, R"txt( +UnknownExpression Detached synthesized + )txt")); +} + +TEST_P(SynthesisTest, Tree_Flat) { + buildTree("", GetParam()); + + auto *LeafLParen = createLeaf(*Arena, tok::l_paren); + auto *LeafRParen = createLeaf(*Arena, tok::r_paren); + auto *TreeParen = createTree(*Arena, + {{LeafLParen, NodeRole::LeftHandSide}, + {LeafRParen, NodeRole::RightHandSide}}, + NodeKind::ParenExpression); + + EXPECT_TRUE(treeDumpEqual(TreeParen, R"txt( +ParenExpression Detached synthesized +|-'(' LeftHandSide synthesized +`-')' RightHandSide synthesized + )txt")); +} + +TEST_P(SynthesisTest, Tree_OfTree) { + buildTree("", GetParam()); + + auto *Leaf1 = createLeaf(*Arena, tok::numeric_constant, "1"); + auto *Int1 = createTree(*Arena, {{Leaf1, NodeRole::LiteralToken}}, + NodeKind::IntegerLiteralExpression); + + auto *LeafPlus = createLeaf(*Arena, tok::plus); + + auto *Leaf2 = createLeaf(*Arena, tok::numeric_constant, "2"); + auto *Int2 = createTree(*Arena, {{Leaf2, NodeRole::LiteralToken}}, + NodeKind::IntegerLiteralExpression); + + auto *TreeBinaryOperator = createTree(*Arena, + {{Int1, NodeRole::LeftHandSide}, + {LeafPlus, NodeRole::OperatorToken}, + {Int2, NodeRole::RightHandSide}}, + NodeKind::BinaryOperatorExpression); + + EXPECT_TRUE(treeDumpEqual(TreeBinaryOperator, R"txt( +BinaryOperatorExpression Detached synthesized +|-IntegerLiteralExpression LeftHandSide synthesized +| `-'1' LiteralToken synthesized +|-'+' OperatorToken synthesized +`-IntegerLiteralExpression RightHandSide synthesized + `-'2' LiteralToken synthesized + )txt")); +} + TEST_P(SynthesisTest, Statement_EmptyStatement) { buildTree("", GetParam()); _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits