From: Eduardo Caldas Date: Wed, 1 Jul 2020 13:58:18 +0000 (+0000) Subject: Add parenthesized expression to SyntaxTree X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=fdbd78333fc6f1deb3037d0961130f05dce059e7;p=platform%2Fupstream%2Fllvm.git Add parenthesized expression to SyntaxTree Subscribers: cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D82960 --- diff --git a/clang/include/clang/Tooling/Syntax/Nodes.h b/clang/include/clang/Tooling/Syntax/Nodes.h index 9446d91..97605ce 100644 --- a/clang/include/clang/Tooling/Syntax/Nodes.h +++ b/clang/include/clang/Tooling/Syntax/Nodes.h @@ -43,6 +43,7 @@ enum class NodeKind : uint16_t { PrefixUnaryOperatorExpression, PostfixUnaryOperatorExpression, BinaryOperatorExpression, + ParenExpression, IntegerLiteralExpression, CharacterLiteralExpression, FloatingLiteralExpression, @@ -161,7 +162,8 @@ enum class NodeRole : uint8_t { ParametersAndQualifiers_trailingReturn, IdExpression_id, IdExpression_qualifier, - NestedNameSpecifier_specifier + NestedNameSpecifier_specifier, + ParenExpression_subExpression }; /// For debugging purposes. llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, NodeRole R); @@ -248,6 +250,19 @@ public: } }; +/// Models a parenthesized expression `(E)`. C++ [expr.prim.paren] +/// e.g. `(3 + 2)` in `a = 1 + (3 + 2);` +class ParenExpression final : public Expression { +public: + ParenExpression() : Expression(NodeKind::ParenExpression) {} + static bool classof(const Node *N) { + return N->kind() == NodeKind::ParenExpression; + } + syntax::Leaf *openParen(); + syntax::Expression *subExpression(); + syntax::Leaf *closeParen(); +}; + /// Expression for integer literals. C++ [lex.icon] class IntegerLiteralExpression final : public Expression { public: diff --git a/clang/lib/Tooling/Syntax/BuildTree.cpp b/clang/lib/Tooling/Syntax/BuildTree.cpp index 6f740b3..8185af2 100644 --- a/clang/lib/Tooling/Syntax/BuildTree.cpp +++ b/clang/lib/Tooling/Syntax/BuildTree.cpp @@ -647,6 +647,16 @@ public: return true; } + bool WalkUpFromParenExpr(ParenExpr *S) { + Builder.markChildToken(S->getLParen(), syntax::NodeRole::OpenParen); + Builder.markExprChild(S->getSubExpr(), + syntax::NodeRole::ParenExpression_subExpression); + Builder.markChildToken(S->getRParen(), syntax::NodeRole::CloseParen); + Builder.foldNode(Builder.getExprRange(S), + new (allocator()) syntax::ParenExpression, S); + return true; + } + bool WalkUpFromIntegerLiteral(IntegerLiteral *S) { Builder.markChildToken(S->getLocation(), syntax::NodeRole::LiteralToken); Builder.foldNode(Builder.getExprRange(S), diff --git a/clang/lib/Tooling/Syntax/Nodes.cpp b/clang/lib/Tooling/Syntax/Nodes.cpp index 1f601fd..3d9b943 100644 --- a/clang/lib/Tooling/Syntax/Nodes.cpp +++ b/clang/lib/Tooling/Syntax/Nodes.cpp @@ -18,6 +18,8 @@ llvm::raw_ostream &syntax::operator<<(llvm::raw_ostream &OS, NodeKind K) { return OS << "TranslationUnit"; case NodeKind::UnknownExpression: return OS << "UnknownExpression"; + case NodeKind::ParenExpression: + return OS << "ParenExpression"; case NodeKind::IntegerLiteralExpression: return OS << "IntegerLiteralExpression"; case NodeKind::CharacterLiteralExpression: @@ -180,6 +182,8 @@ llvm::raw_ostream &syntax::operator<<(llvm::raw_ostream &OS, NodeRole R) { return OS << "IdExpression_qualifier"; case syntax::NodeRole::NestedNameSpecifier_specifier: return OS << "NestedNameSpecifier_specifier"; + case syntax::NodeRole::ParenExpression_subExpression: + return OS << "ParenExpression_subExpression"; } llvm_unreachable("invalid role"); } @@ -203,6 +207,21 @@ syntax::UnqualifiedId *syntax::IdExpression::unqualifiedId() { findChild(syntax::NodeRole::IdExpression_id)); } +syntax::Leaf *syntax::ParenExpression::openParen() { + return llvm::cast_or_null( + findChild(syntax::NodeRole::OpenParen)); +} + +syntax::Expression *syntax::ParenExpression::subExpression() { + return llvm::cast_or_null( + findChild(syntax::NodeRole::ParenExpression_subExpression)); +} + +syntax::Leaf *syntax::ParenExpression::closeParen() { + return llvm::cast_or_null( + findChild(syntax::NodeRole::CloseParen)); +} + syntax::Leaf *syntax::IntegerLiteralExpression::literalToken() { return llvm::cast_or_null( findChild(syntax::NodeRole::LiteralToken)); diff --git a/clang/unittests/Tooling/Syntax/TreeTest.cpp b/clang/unittests/Tooling/Syntax/TreeTest.cpp index 7a438f1..094a495 100644 --- a/clang/unittests/Tooling/Syntax/TreeTest.cpp +++ b/clang/unittests/Tooling/Syntax/TreeTest.cpp @@ -1129,6 +1129,61 @@ void test(S s) { )txt")); } +TEST_P(SyntaxTreeTest, ParenExpr) { + EXPECT_TRUE(treeDumpEqual( + R"cpp( +void test() { + (1); + ((1)); + (1 + (2)); +} +)cpp", + R"txt( +*: TranslationUnit +`-SimpleDeclaration + |-void + |-SimpleDeclarator + | |-test + | `-ParametersAndQualifiers + | |-( + | `-) + `-CompoundStatement + |-{ + |-ExpressionStatement + | |-ParenExpression + | | |-( + | | |-IntegerLiteralExpression + | | | `-1 + | | `-) + | `-; + |-ExpressionStatement + | |-ParenExpression + | | |-( + | | |-ParenExpression + | | | |-( + | | | |-IntegerLiteralExpression + | | | | `-1 + | | | `-) + | | `-) + | `-; + |-ExpressionStatement + | |-ParenExpression + | | |-( + | | |-BinaryOperatorExpression + | | | |-IntegerLiteralExpression + | | | | `-1 + | | | |-+ + | | | `-ParenExpression + | | | |-( + | | | |-IntegerLiteralExpression + | | | | `-2 + | | | `-) + | | `-) + | `-; + `-} +)txt")); +} + TEST_P(SyntaxTreeTest, IntegerLiteral) { EXPECT_TRUE(treeDumpEqual( R"cpp( @@ -2040,7 +2095,7 @@ void test(int a, int b) { |-{ |-ExpressionStatement | |-BinaryOperatorExpression - | | |-UnknownExpression + | | |-ParenExpression | | | |-( | | | |-BinaryOperatorExpression | | | | |-IntegerLiteralExpression @@ -2050,7 +2105,7 @@ void test(int a, int b) { | | | | `-2 | | | `-) | | |-* - | | `-UnknownExpression + | | `-ParenExpression | | |-( | | |-BinaryOperatorExpression | | | |-IntegerLiteralExpression