From 7f7f8564808b51aa62744edf75c07c0df102056a Mon Sep 17 00:00:00 2001 From: Eduardo Caldas Date: Mon, 22 Jun 2020 16:04:54 +0000 Subject: [PATCH] Add `BoolLiteralExpression` to SyntaxTree Subscribers: cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D82310 --- clang/include/clang/Tooling/Syntax/Nodes.h | 11 ++++++ clang/lib/Tooling/Syntax/BuildTree.cpp | 7 ++++ clang/lib/Tooling/Syntax/Nodes.cpp | 7 ++++ clang/unittests/Tooling/Syntax/TreeTest.cpp | 52 +++++++++++++++++++++++++---- 4 files changed, 70 insertions(+), 7 deletions(-) diff --git a/clang/include/clang/Tooling/Syntax/Nodes.h b/clang/include/clang/Tooling/Syntax/Nodes.h index 139ac9a..255a108 100644 --- a/clang/include/clang/Tooling/Syntax/Nodes.h +++ b/clang/include/clang/Tooling/Syntax/Nodes.h @@ -45,6 +45,7 @@ enum class NodeKind : uint16_t { BinaryOperatorExpression, CxxNullPtrExpression, IntegerLiteralExpression, + BoolLiteralExpression, IdExpression, // Statements. @@ -264,6 +265,16 @@ public: syntax::Leaf *literalToken(); }; +/// Expression for boolean literals. C++ [lex.bool] +class BoolLiteralExpression final : public Expression { +public: + BoolLiteralExpression() : Expression(NodeKind::BoolLiteralExpression) {} + static bool classof(const Node *N) { + return N->kind() == NodeKind::BoolLiteralExpression; + } + syntax::Leaf *literalToken(); +}; + /// An abstract class for prefix and postfix unary operators. class UnaryOperatorExpression : public Expression { public: diff --git a/clang/lib/Tooling/Syntax/BuildTree.cpp b/clang/lib/Tooling/Syntax/BuildTree.cpp index 3ee66aa..7ff603f 100644 --- a/clang/lib/Tooling/Syntax/BuildTree.cpp +++ b/clang/lib/Tooling/Syntax/BuildTree.cpp @@ -654,6 +654,13 @@ public: return true; } + bool WalkUpFromCXXBoolLiteralExpr(CXXBoolLiteralExpr *S) { + Builder.markChildToken(S->getLocation(), syntax::NodeRole::LiteralToken); + Builder.foldNode(Builder.getExprRange(S), + new (allocator()) syntax::BoolLiteralExpression, S); + return true; + } + bool WalkUpFromCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *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 623391a..498721d 100644 --- a/clang/lib/Tooling/Syntax/Nodes.cpp +++ b/clang/lib/Tooling/Syntax/Nodes.cpp @@ -22,6 +22,8 @@ llvm::raw_ostream &syntax::operator<<(llvm::raw_ostream &OS, NodeKind K) { return OS << "CxxNullPtrExpression"; case NodeKind::IntegerLiteralExpression: return OS << "IntegerLiteralExpression"; + case NodeKind::BoolLiteralExpression: + return OS << "BoolLiteralExpression"; case NodeKind::PrefixUnaryOperatorExpression: return OS << "PrefixUnaryOperatorExpression"; case NodeKind::PostfixUnaryOperatorExpression: @@ -200,6 +202,11 @@ syntax::Leaf *syntax::IntegerLiteralExpression::literalToken() { findChild(syntax::NodeRole::LiteralToken)); } +syntax::Leaf *syntax::BoolLiteralExpression::literalToken() { + return llvm::cast_or_null( + findChild(syntax::NodeRole::LiteralToken)); +} + syntax::Leaf *syntax::CxxNullPtrExpression::nullPtrKeyword() { 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 5f48ef1..d32ce62 100644 --- a/clang/unittests/Tooling/Syntax/TreeTest.cpp +++ b/clang/unittests/Tooling/Syntax/TreeTest.cpp @@ -69,6 +69,10 @@ struct TestClangConfig { Language == Lang_CXX20; } + bool hasBoolType() const { + return Language == Lang_C89 || Language == Lang_C99; + } + bool supportsCXXDynamicExceptionSpecification() const { return Language == Lang_CXX03 || Language == Lang_CXX11 || Language == Lang_CXX14; @@ -1228,6 +1232,40 @@ void test() { )txt")); } +TEST_P(SyntaxTreeTest, BoolLiteral) { + if (GetParam().hasBoolType()) { + return; + } + EXPECT_TRUE(treeDumpEqual( + R"cpp( +void test() { + true; + false; +} +)cpp", + R"txt( +*: TranslationUnit +`-SimpleDeclaration + |-void + |-SimpleDeclarator + | |-test + | `-ParametersAndQualifiers + | |-( + | `-) + `-CompoundStatement + |-{ + |-ExpressionStatement + | |-BoolLiteralExpression + | | `-true + | `-; + |-ExpressionStatement + | |-BoolLiteralExpression + | | `-false + | `-; + `-} +)txt")); +} + TEST_P(SyntaxTreeTest, IntegerLiteral) { EXPECT_TRUE(treeDumpEqual( R"cpp( @@ -1691,18 +1729,18 @@ void test(int a) { |-{ |-ExpressionStatement | |-BinaryOperatorExpression - | | |-UnknownExpression + | | |-BoolLiteralExpression | | | `-true | | |-|| - | | `-UnknownExpression + | | `-BoolLiteralExpression | | `-false | `-; |-ExpressionStatement | |-BinaryOperatorExpression - | | |-UnknownExpression + | | |-BoolLiteralExpression | | | `-true | | |-or - | | `-UnknownExpression + | | `-BoolLiteralExpression | | `-false | `-; |-ExpressionStatement @@ -2556,7 +2594,7 @@ static_assert(true); |-StaticAssertDeclaration | |-static_assert | |-( -| |-UnknownExpression +| |-BoolLiteralExpression | | `-true | |-, | |-UnknownExpression @@ -2566,7 +2604,7 @@ static_assert(true); `-StaticAssertDeclaration |-static_assert |-( - |-UnknownExpression + |-BoolLiteralExpression | `-true |-) `-; @@ -3186,7 +3224,7 @@ int b() noexcept(true); | |-) | |-noexcept | |-( - | |-UnknownExpression + | |-BoolLiteralExpression | | `-true | `-) `-; -- 2.7.4