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 @@ -44,6 +44,7 @@ PostfixUnaryOperatorExpression, BinaryOperatorExpression, CxxNullPtrExpression, + IntegerLiteralExpression, // Statements. UnknownStatement, @@ -114,6 +115,7 @@ BinaryOperatorExpression_operatorToken, BinaryOperatorExpression_rightHandSide, CxxNullPtrExpression_keyword, + IntegerLiteralExpression_literalToken, CaseStatement_value, IfStatement_thenStatement, IfStatement_elseKeyword, @@ -178,6 +180,16 @@ syntax::Leaf *nullPtrKeyword(); }; +/// Expression for integer literals. +class IntegerLiteralExpression final : public Expression { +public: + IntegerLiteralExpression() : Expression(NodeKind::IntegerLiteralExpression) {} + static bool classof(const Node *N) { + return N->kind() == NodeKind::IntegerLiteralExpression; + } + syntax::Leaf *literalToken(); +}; + /// An abstract class for prefix and postfix unary operators. class UnaryOperatorExpression : public Expression { public: @@ -627,7 +639,7 @@ return N->kind() == NodeKind::TrailingReturnType; } // TODO: add accessors for specifiers. - syntax::Leaf *arrow(); + syntax::Leaf *arrowToken(); syntax::SimpleDeclarator *declarator(); }; 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 @@ -608,6 +608,15 @@ return true; } + bool WalkUpFromIntegerLiteral(IntegerLiteral *S) { + Builder.markChildToken( + S->getLocation(), + syntax::NodeRole::IntegerLiteralExpression_literalToken); + Builder.foldNode(Builder.getExprRange(S), + new (allocator()) syntax::IntegerLiteralExpression, S); + return true; + } + bool WalkUpFromCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *S) { Builder.markChildToken(S->getLocation(), syntax::NodeRole::CxxNullPtrExpression_keyword); 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 @@ -20,6 +20,8 @@ return OS << "UnknownExpression"; case NodeKind::CxxNullPtrExpression: return OS << "CxxNullPtrExpression"; + case NodeKind::IntegerLiteralExpression: + return OS << "IntegerLiteralExpression"; case NodeKind::PrefixUnaryOperatorExpression: return OS << "PrefixUnaryOperatorExpression"; case NodeKind::PostfixUnaryOperatorExpression: @@ -118,6 +120,8 @@ return OS << "IfStatement_elseKeyword"; case syntax::NodeRole::IfStatement_elseStatement: return OS << "IfStatement_elseStatement"; + case syntax::NodeRole::IntegerLiteralExpression_literalToken: + return OS << "IntegerLiteralExpression_literalToken"; case syntax::NodeRole::CxxNullPtrExpression_keyword: return OS << "CxxNullPtrExpression_keyword"; case syntax::NodeRole::UnaryOperatorExpression_operatorToken: @@ -162,6 +166,11 @@ llvm_unreachable("invalid role"); } +syntax::Leaf *syntax::IntegerLiteralExpression::literalToken() { + return llvm::cast_or_null( + findChild(syntax::NodeRole::IntegerLiteralExpression_literalToken)); +} + syntax::Leaf *syntax::CxxNullPtrExpression::nullPtrKeyword() { return llvm::cast_or_null( findChild(syntax::NodeRole::CxxNullPtrExpression_keyword)); @@ -391,7 +400,7 @@ findChild(syntax::NodeRole::CloseParen)); } -syntax::Leaf *syntax::TrailingReturnType::arrow() { +syntax::Leaf *syntax::TrailingReturnType::arrowToken() { return llvm::cast_or_null( findChild(syntax::NodeRole::TrailingReturnType_arrow)); } 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 @@ -64,6 +64,11 @@ Language == Lang_CXX17 || Language == Lang_CXX20; } + bool isCXX14OrLater() const { + return Language == Lang_CXX14 || Language == Lang_CXX17 || + Language == Lang_CXX20; + } + bool supportsCXXDynamicExceptionSpecification() const { return Language == Lang_CXX03 || Language == Lang_CXX11 || Language == Lang_CXX14; @@ -314,7 +319,7 @@ |-SimpleDeclarator | |-b | |-= - | `-UnknownExpression + | `-IntegerLiteralExpression | `-42 `-; )txt"); @@ -371,7 +376,7 @@ |-IfStatement | |-if | |-( - | |-UnknownExpression + | |-IntegerLiteralExpression | | `-1 | |-) | `-CompoundStatement @@ -380,7 +385,7 @@ |-IfStatement | |-if | |-( - | |-UnknownExpression + | |-IntegerLiteralExpression | | `-1 | |-) | |-CompoundStatement @@ -390,7 +395,7 @@ | `-IfStatement | |-if | |-( - | |-UnknownExpression + | |-IntegerLiteralExpression | | `-0 | |-) | `-CompoundStatement @@ -461,7 +466,7 @@ | | |-a | | `-ArraySubscript | | |-[ - | | |-UnknownExpression + | | |-IntegerLiteralExpression | | | `-3 | | `-] | `-; @@ -501,7 +506,7 @@ | | `-SimpleDeclarator | | |-a | | |-= - | | `-UnknownExpression + | | `-IntegerLiteralExpression | | `-10 | `-; `-} @@ -532,14 +537,14 @@ |-SwitchStatement | |-switch | |-( - | |-UnknownExpression + | |-IntegerLiteralExpression | | `-1 | |-) | `-CompoundStatement | |-{ | |-CaseStatement | | |-case - | | |-UnknownExpression + | | |-IntegerLiteralExpression | | | `-0 | | |-: | | `-DefaultStatement @@ -573,7 +578,7 @@ |-WhileStatement | |-while | |-( - | |-UnknownExpression + | |-IntegerLiteralExpression | | `-1 | |-) | `-CompoundStatement @@ -610,7 +615,7 @@ | |-: | `-ReturnStatement | |-return - | |-UnknownExpression + | |-IntegerLiteralExpression | | `-100 | `-; `-} @@ -648,7 +653,7 @@ |-IfStatement | |-if | |-( - | |-UnknownExpression + | |-IntegerLiteralExpression | | `-1 | |-) | |-ExpressionStatement @@ -699,6 +704,149 @@ )txt"); } +TEST_P(SyntaxTreeTest, IntegerLiteral) { + expectTreeDumpEqual( + R"cpp( +void test() { + 12; + 12u; + 12l; + 12ul; + 014; + 0XC; +} + )cpp", + R"txt( +*: TranslationUnit +`-SimpleDeclaration + |-void + |-SimpleDeclarator + | |-test + | `-ParametersAndQualifiers + | |-( + | `-) + `-CompoundStatement + |-{ + |-ExpressionStatement + | |-IntegerLiteralExpression + | | `-12 + | `-; + |-ExpressionStatement + | |-IntegerLiteralExpression + | | `-12u + | `-; + |-ExpressionStatement + | |-IntegerLiteralExpression + | | `-12l + | `-; + |-ExpressionStatement + | |-IntegerLiteralExpression + | | `-12ul + | `-; + |-ExpressionStatement + | |-IntegerLiteralExpression + | | `-014 + | `-; + |-ExpressionStatement + | |-IntegerLiteralExpression + | | `-0XC + | `-; + `-} +)txt"); +} + +TEST_P(SyntaxTreeTest, LongLongLiteral) { + if (!GetParam().isCXX11OrLater()) { + return; + } + expectTreeDumpEqual( + R"cpp( +void test() { + 12ll; + 12ull; +} + )cpp", + R"txt( +*: TranslationUnit +`-SimpleDeclaration + |-void + |-SimpleDeclarator + | |-test + | `-ParametersAndQualifiers + | |-( + | `-) + `-CompoundStatement + |-{ + |-ExpressionStatement + | |-IntegerLiteralExpression + | | `-12ll + | `-; + |-ExpressionStatement + | |-IntegerLiteralExpression + | | `-12ull + | `-; + `-} +)txt"); +} + +TEST_P(SyntaxTreeTest, BinaryLiteral) { + if (!GetParam().isCXX14OrLater()) { + return; + } + expectTreeDumpEqual( + R"cpp( +void test() { + 0b1100; +} + )cpp", + R"txt( +*: TranslationUnit +`-SimpleDeclaration + |-void + |-SimpleDeclarator + | |-test + | `-ParametersAndQualifiers + | |-( + | `-) + `-CompoundStatement + |-{ + |-ExpressionStatement + | |-IntegerLiteralExpression + | | `-0b1100 + | `-; + `-} +)txt"); +} + +TEST_P(SyntaxTreeTest, QuotedIntegerLiteral) { + if (!GetParam().isCXX14OrLater()) { + return; + } + expectTreeDumpEqual( + R"cpp( +void test() { + 1'2'0ull; +} + )cpp", + R"txt( +*: TranslationUnit +`-SimpleDeclaration + |-void + |-SimpleDeclarator + | |-test + | `-ParametersAndQualifiers + | |-( + | `-) + `-CompoundStatement + |-{ + |-ExpressionStatement + | |-IntegerLiteralExpression + | | `-1'2'0ull + | `-; + `-} +)txt"); +} + TEST_P(SyntaxTreeTest, PostfixUnaryOperator) { expectTreeDumpEqual( R"cpp( @@ -914,18 +1062,18 @@ |-{ |-ExpressionStatement | |-BinaryOperatorExpression - | | |-UnknownExpression + | | |-IntegerLiteralExpression | | | `-1 | | |-- - | | `-UnknownExpression + | | `-IntegerLiteralExpression | | `-2 | `-; |-ExpressionStatement | |-BinaryOperatorExpression - | | |-UnknownExpression + | | |-IntegerLiteralExpression | | | `-1 | | |-== - | | `-UnknownExpression + | | `-IntegerLiteralExpression | | `-2 | `-; |-ExpressionStatement @@ -933,7 +1081,7 @@ | | |-UnknownExpression | | | `-a | | |-= - | | `-UnknownExpression + | | `-IntegerLiteralExpression | | `-1 | `-; |-ExpressionStatement @@ -941,23 +1089,23 @@ | | |-UnknownExpression | | | `-a | | |-<<= - | | `-UnknownExpression + | | `-IntegerLiteralExpression | | `-1 | `-; |-ExpressionStatement | |-BinaryOperatorExpression - | | |-UnknownExpression + | | |-IntegerLiteralExpression | | | `-1 | | |-|| - | | `-UnknownExpression + | | `-IntegerLiteralExpression | | `-0 | `-; |-ExpressionStatement | |-BinaryOperatorExpression - | | |-UnknownExpression + | | |-IntegerLiteralExpression | | | `-1 | | |-& - | | `-UnknownExpression + | | `-IntegerLiteralExpression | | `-2 | `-; |-ExpressionStatement @@ -965,7 +1113,7 @@ | | |-UnknownExpression | | | `-a | | |-^= - | | `-UnknownExpression + | | `-IntegerLiteralExpression | | `-3 | `-; `-} @@ -1018,10 +1166,10 @@ | `-; |-ExpressionStatement | |-BinaryOperatorExpression - | | |-UnknownExpression + | | |-IntegerLiteralExpression | | | `-1 | | |-bitand - | | `-UnknownExpression + | | `-IntegerLiteralExpression | | `-2 | `-; |-ExpressionStatement @@ -1029,7 +1177,7 @@ | | |-UnknownExpression | | | `-a | | |-xor_eq - | | `-UnknownExpression + | | `-IntegerLiteralExpression | | `-3 | `-; `-} @@ -1072,20 +1220,20 @@ | | |-UnknownExpression | | | |-( | | | |-BinaryOperatorExpression - | | | | |-UnknownExpression + | | | | |-IntegerLiteralExpression | | | | | `-1 | | | | |-+ - | | | | `-UnknownExpression + | | | | `-IntegerLiteralExpression | | | | `-2 | | | `-) | | |-* | | `-UnknownExpression | | |-( | | |-BinaryOperatorExpression - | | | |-UnknownExpression + | | | |-IntegerLiteralExpression | | | | `-4 | | | |-/ - | | | `-UnknownExpression + | | | `-IntegerLiteralExpression | | | `-2 | | `-) | `-; @@ -1098,7 +1246,7 @@ | | | `-UnknownExpression | | | `-b | | |-+ - | | `-UnknownExpression + | | `-IntegerLiteralExpression | | `-42 | `-; |-ExpressionStatement @@ -1110,7 +1258,7 @@ | | |-UnknownExpression | | | `-b | | |-= - | | `-UnknownExpression + | | `-IntegerLiteralExpression | | `-42 | `-; |-ExpressionStatement @@ -1123,10 +1271,10 @@ | | | |-UnknownExpression | | | | `-b | | | |-* - | | | `-UnknownExpression + | | | `-IntegerLiteralExpression | | | `-4 | | |-+ - | | `-UnknownExpression + | | `-IntegerLiteralExpression | | `-2 | `-; |-ExpressionStatement @@ -1135,14 +1283,14 @@ | | | |-UnknownExpression | | | | `-a | | | |-% - | | | `-UnknownExpression + | | | `-IntegerLiteralExpression | | | `-2 | | |-+ | | `-BinaryOperatorExpression | | |-UnknownExpression | | | `-b | | |-* - | | `-UnknownExpression + | | `-IntegerLiteralExpression | | `-42 | `-; `-} @@ -1564,7 +1712,7 @@ | |-SimpleDeclarator | | |-var | | |-= -| | `-UnknownExpression +| | `-IntegerLiteralExpression | | `-10 | `-; `-TemplateDeclaration @@ -1926,10 +2074,10 @@ | |-I: if | |-I: ( | |-I: BinaryOperatorExpression - | | |-I: UnknownExpression + | | |-I: IntegerLiteralExpression | | | `-I: 1 | | |-I: + - | | `-I: UnknownExpression + | | `-I: IntegerLiteralExpression | | `-I: 1 | |-I: ) | |-I: CompoundStatement @@ -1974,14 +2122,14 @@ |-CompoundStatement | |-{ | |-ExpressionStatement - | | |-UnknownExpression + | | |-IntegerLiteralExpression | | | `-1 | | `-; | `-} |-CompoundStatement | |-{ | |-ExpressionStatement - | | |-UnknownExpression + | | |-IntegerLiteralExpression | | | `-2 | | `-; | `-} @@ -2004,7 +2152,7 @@ | | |-a | | `-ArraySubscript | | |-[ -| | |-UnknownExpression +| | |-IntegerLiteralExpression | | | `-10 | | `-] | `-; @@ -2014,17 +2162,17 @@ | | |-b | | |-ArraySubscript | | | |-[ -| | | |-UnknownExpression +| | | |-IntegerLiteralExpression | | | | `-1 | | | `-] | | |-ArraySubscript | | | |-[ -| | | |-UnknownExpression +| | | |-IntegerLiteralExpression | | | | `-2 | | | `-] | | `-ArraySubscript | | |-[ -| | |-UnknownExpression +| | |-IntegerLiteralExpression | | | `-3 | | `-] | `-; @@ -2039,13 +2187,13 @@ | `-UnknownExpression | `-UnknownExpression | |-{ - | |-UnknownExpression + | |-IntegerLiteralExpression | | `-1 | |-, - | |-UnknownExpression + | |-IntegerLiteralExpression | | `-2 | |-, - | |-UnknownExpression + | |-IntegerLiteralExpression | | `-3 | `-} `-; )txt"); @@ -2074,7 +2222,7 @@ | | `-ArraySubscript | | |-[ | | |-static - | | |-UnknownExpression + | | |-IntegerLiteralExpression | | | `-10 | | `-] | `-) @@ -2559,7 +2707,7 @@ | | |-= | | `-PrefixUnaryOperatorExpression | | |-- -| | `-UnknownExpression +| | `-IntegerLiteralExpression | | `-1 | `-; |-SimpleDeclaration @@ -2568,7 +2716,7 @@ | |-SimpleDeclarator | | |-east | | |-= -| | `-UnknownExpression +| | `-IntegerLiteralExpression | | `-1 | `-; |-SimpleDeclaration @@ -2578,7 +2726,7 @@ | |-SimpleDeclarator | | |-universal | | |-= -| | `-UnknownExpression +| | `-IntegerLiteralExpression | | `-0 | `-; `-SimpleDeclaration