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::literal() { + 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)); 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 @@ -314,7 +314,7 @@ |-SimpleDeclarator | |-b | |-= - | `-UnknownExpression + | `-IntegerLiteralExpression | `-42 `-; )txt"); @@ -371,7 +371,7 @@ |-IfStatement | |-if | |-( - | |-UnknownExpression + | |-IntegerLiteralExpression | | `-1 | |-) | `-CompoundStatement @@ -380,7 +380,7 @@ |-IfStatement | |-if | |-( - | |-UnknownExpression + | |-IntegerLiteralExpression | | `-1 | |-) | |-CompoundStatement @@ -390,7 +390,7 @@ | `-IfStatement | |-if | |-( - | |-UnknownExpression + | |-IntegerLiteralExpression | | `-0 | |-) | `-CompoundStatement @@ -461,7 +461,7 @@ | | |-a | | `-ArraySubscript | | |-[ - | | |-UnknownExpression + | | |-IntegerLiteralExpression | | | `-3 | | `-] | `-; @@ -501,7 +501,7 @@ | | `-SimpleDeclarator | | |-a | | |-= - | | `-UnknownExpression + | | `-IntegerLiteralExpression | | `-10 | `-; `-} @@ -532,14 +532,14 @@ |-SwitchStatement | |-switch | |-( - | |-UnknownExpression + | |-IntegerLiteralExpression | | `-1 | |-) | `-CompoundStatement | |-{ | |-CaseStatement | | |-case - | | |-UnknownExpression + | | |-IntegerLiteralExpression | | | `-0 | | |-: | | `-DefaultStatement @@ -573,7 +573,7 @@ |-WhileStatement | |-while | |-( - | |-UnknownExpression + | |-IntegerLiteralExpression | | `-1 | |-) | `-CompoundStatement @@ -610,7 +610,7 @@ | |-: | `-ReturnStatement | |-return - | |-UnknownExpression + | |-IntegerLiteralExpression | | `-100 | `-; `-} @@ -648,7 +648,7 @@ |-IfStatement | |-if | |-( - | |-UnknownExpression + | |-IntegerLiteralExpression | | `-1 | |-) | |-ExpressionStatement @@ -699,6 +699,32 @@ )txt"); } +TEST_P(SyntaxTreeTest, IntegerLiteralExpression) { + expectTreeDumpEqual( + R"cpp( +void test() { + 42; +} + )cpp", + R"txt( +*: TranslationUnit +`-SimpleDeclaration + |-void + |-SimpleDeclarator + | |-test + | `-ParametersAndQualifiers + | |-( + | `-) + `-CompoundStatement + |-{ + |-ExpressionStatement + | |-IntegerLiteralExpression + | | `-42 + | `-; + `-} +)txt"); +} + TEST_P(SyntaxTreeTest, PostfixUnaryOperator) { expectTreeDumpEqual( R"cpp( @@ -914,18 +940,18 @@ |-{ |-ExpressionStatement | |-BinaryOperatorExpression - | | |-UnknownExpression + | | |-IntegerLiteralExpression | | | `-1 | | |-- - | | `-UnknownExpression + | | `-IntegerLiteralExpression | | `-2 | `-; |-ExpressionStatement | |-BinaryOperatorExpression - | | |-UnknownExpression + | | |-IntegerLiteralExpression | | | `-1 | | |-== - | | `-UnknownExpression + | | `-IntegerLiteralExpression | | `-2 | `-; |-ExpressionStatement @@ -933,7 +959,7 @@ | | |-UnknownExpression | | | `-a | | |-= - | | `-UnknownExpression + | | `-IntegerLiteralExpression | | `-1 | `-; |-ExpressionStatement @@ -941,23 +967,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 +991,7 @@ | | |-UnknownExpression | | | `-a | | |-^= - | | `-UnknownExpression + | | `-IntegerLiteralExpression | | `-3 | `-; `-} @@ -1018,10 +1044,10 @@ | `-; |-ExpressionStatement | |-BinaryOperatorExpression - | | |-UnknownExpression + | | |-IntegerLiteralExpression | | | `-1 | | |-bitand - | | `-UnknownExpression + | | `-IntegerLiteralExpression | | `-2 | `-; |-ExpressionStatement @@ -1029,7 +1055,7 @@ | | |-UnknownExpression | | | `-a | | |-xor_eq - | | `-UnknownExpression + | | `-IntegerLiteralExpression | | `-3 | `-; `-} @@ -1072,20 +1098,20 @@ | | |-UnknownExpression | | | |-( | | | |-BinaryOperatorExpression - | | | | |-UnknownExpression + | | | | |-IntegerLiteralExpression | | | | | `-1 | | | | |-+ - | | | | `-UnknownExpression + | | | | `-IntegerLiteralExpression | | | | `-2 | | | `-) | | |-* | | `-UnknownExpression | | |-( | | |-BinaryOperatorExpression - | | | |-UnknownExpression + | | | |-IntegerLiteralExpression | | | | `-4 | | | |-/ - | | | `-UnknownExpression + | | | `-IntegerLiteralExpression | | | `-2 | | `-) | `-; @@ -1098,7 +1124,7 @@ | | | `-UnknownExpression | | | `-b | | |-+ - | | `-UnknownExpression + | | `-IntegerLiteralExpression | | `-42 | `-; |-ExpressionStatement @@ -1110,7 +1136,7 @@ | | |-UnknownExpression | | | `-b | | |-= - | | `-UnknownExpression + | | `-IntegerLiteralExpression | | `-42 | `-; |-ExpressionStatement @@ -1123,10 +1149,10 @@ | | | |-UnknownExpression | | | | `-b | | | |-* - | | | `-UnknownExpression + | | | `-IntegerLiteralExpression | | | `-4 | | |-+ - | | `-UnknownExpression + | | `-IntegerLiteralExpression | | `-2 | `-; |-ExpressionStatement @@ -1135,14 +1161,14 @@ | | | |-UnknownExpression | | | | `-a | | | |-% - | | | `-UnknownExpression + | | | `-IntegerLiteralExpression | | | `-2 | | |-+ | | `-BinaryOperatorExpression | | |-UnknownExpression | | | `-b | | |-* - | | `-UnknownExpression + | | `-IntegerLiteralExpression | | `-42 | `-; `-} @@ -1564,7 +1590,7 @@ | |-SimpleDeclarator | | |-var | | |-= -| | `-UnknownExpression +| | `-IntegerLiteralExpression | | `-10 | `-; `-TemplateDeclaration @@ -1926,10 +1952,10 @@ | |-I: if | |-I: ( | |-I: BinaryOperatorExpression - | | |-I: UnknownExpression + | | |-I: IntegerLiteralExpression | | | `-I: 1 | | |-I: + - | | `-I: UnknownExpression + | | `-I: IntegerLiteralExpression | | `-I: 1 | |-I: ) | |-I: CompoundStatement @@ -1974,14 +2000,14 @@ |-CompoundStatement | |-{ | |-ExpressionStatement - | | |-UnknownExpression + | | |-IntegerLiteralExpression | | | `-1 | | `-; | `-} |-CompoundStatement | |-{ | |-ExpressionStatement - | | |-UnknownExpression + | | |-IntegerLiteralExpression | | | `-2 | | `-; | `-} @@ -2004,7 +2030,7 @@ | | |-a | | `-ArraySubscript | | |-[ -| | |-UnknownExpression +| | |-IntegerLiteralExpression | | | `-10 | | `-] | `-; @@ -2014,17 +2040,17 @@ | | |-b | | |-ArraySubscript | | | |-[ -| | | |-UnknownExpression +| | | |-IntegerLiteralExpression | | | | `-1 | | | `-] | | |-ArraySubscript | | | |-[ -| | | |-UnknownExpression +| | | |-IntegerLiteralExpression | | | | `-2 | | | `-] | | `-ArraySubscript | | |-[ -| | |-UnknownExpression +| | |-IntegerLiteralExpression | | | `-3 | | `-] | `-; @@ -2039,13 +2065,13 @@ | `-UnknownExpression | `-UnknownExpression | |-{ - | |-UnknownExpression + | |-IntegerLiteralExpression | | `-1 | |-, - | |-UnknownExpression + | |-IntegerLiteralExpression | | `-2 | |-, - | |-UnknownExpression + | |-IntegerLiteralExpression | | `-3 | `-} `-; )txt"); @@ -2074,7 +2100,7 @@ | | `-ArraySubscript | | |-[ | | |-static - | | |-UnknownExpression + | | |-IntegerLiteralExpression | | | `-10 | | `-] | `-) @@ -2559,7 +2585,7 @@ | | |-= | | `-PrefixUnaryOperatorExpression | | |-- -| | `-UnknownExpression +| | `-IntegerLiteralExpression | | `-1 | `-; |-SimpleDeclaration @@ -2568,7 +2594,7 @@ | |-SimpleDeclarator | | |-east | | |-= -| | `-UnknownExpression +| | `-IntegerLiteralExpression | | `-1 | `-; |-SimpleDeclaration @@ -2578,7 +2604,7 @@ | |-SimpleDeclarator | | |-universal | | |-= -| | `-UnknownExpression +| | `-IntegerLiteralExpression | | `-0 | `-; `-SimpleDeclaration