diff --git a/clang/include/clang/Tooling/Syntax/Nodes.h b/clang/include/clang/Tooling/Syntax/Nodes.h --- a/clang/include/clang/Tooling/Syntax/Nodes.h +++ b/clang/include/clang/Tooling/Syntax/Nodes.h @@ -45,6 +45,7 @@ BinaryOperatorExpression, CxxNullPtrExpression, IntegerLiteralExpression, + BoolLiteralExpression, IdExpression, // Statements. @@ -264,6 +265,16 @@ 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 --- a/clang/lib/Tooling/Syntax/BuildTree.cpp +++ b/clang/lib/Tooling/Syntax/BuildTree.cpp @@ -654,6 +654,13 @@ 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 --- a/clang/lib/Tooling/Syntax/Nodes.cpp +++ b/clang/lib/Tooling/Syntax/Nodes.cpp @@ -22,6 +22,8 @@ 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 @@ 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 --- a/clang/unittests/Tooling/Syntax/TreeTest.cpp +++ b/clang/unittests/Tooling/Syntax/TreeTest.cpp @@ -69,6 +69,10 @@ 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 @@ )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 @@ |-{ |-ExpressionStatement | |-BinaryOperatorExpression - | | |-UnknownExpression + | | |-BoolLiteralExpression | | | `-true | | |-|| - | | `-UnknownExpression + | | `-BoolLiteralExpression | | `-false | `-; |-ExpressionStatement | |-BinaryOperatorExpression - | | |-UnknownExpression + | | |-BoolLiteralExpression | | | `-true | | |-or - | | `-UnknownExpression + | | `-BoolLiteralExpression | | `-false | `-; |-ExpressionStatement @@ -2556,7 +2594,7 @@ |-StaticAssertDeclaration | |-static_assert | |-( -| |-UnknownExpression +| |-BoolLiteralExpression | | `-true | |-, | |-UnknownExpression @@ -2566,7 +2604,7 @@ `-StaticAssertDeclaration |-static_assert |-( - |-UnknownExpression + |-BoolLiteralExpression | `-true |-) `-; @@ -3186,7 +3224,7 @@ | |-) | |-noexcept | |-( - | |-UnknownExpression + | |-BoolLiteralExpression | | `-true | `-) `-;