Add `CharLiteral` to SyntaxTree

Subscribers: cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D82312
This commit is contained in:
Eduardo Caldas 2020-06-25 16:10:19 +00:00
parent c4b1daed1d
commit 221d7bbe49
4 changed files with 159 additions and 0 deletions

View File

@ -46,6 +46,7 @@ enum class NodeKind : uint16_t {
CxxNullPtrExpression,
IntegerLiteralExpression,
BoolLiteralExpression,
CharacterLiteralExpression,
IdExpression,
// Statements.
@ -255,6 +256,17 @@ public:
syntax::Leaf *nullPtrKeyword();
};
/// Expression for character literals. C++ [lex.ccon]
class CharacterLiteralExpression final : public Expression {
public:
CharacterLiteralExpression()
: Expression(NodeKind::CharacterLiteralExpression) {}
static bool classof(const Node *N) {
return N->kind() == NodeKind::CharacterLiteralExpression;
}
syntax::Leaf *literalToken();
};
/// Expression for integer literals.
class IntegerLiteralExpression final : public Expression {
public:

View File

@ -661,6 +661,13 @@ public:
return true;
}
bool WalkUpFromCharacterLiteral(CharacterLiteral *S) {
Builder.markChildToken(S->getLocation(), syntax::NodeRole::LiteralToken);
Builder.foldNode(Builder.getExprRange(S),
new (allocator()) syntax::CharacterLiteralExpression, S);
return true;
}
bool WalkUpFromCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *S) {
Builder.markChildToken(S->getLocation(), syntax::NodeRole::LiteralToken);
Builder.foldNode(Builder.getExprRange(S),

View File

@ -24,6 +24,8 @@ llvm::raw_ostream &syntax::operator<<(llvm::raw_ostream &OS, NodeKind K) {
return OS << "IntegerLiteralExpression";
case NodeKind::BoolLiteralExpression:
return OS << "BoolLiteralExpression";
case NodeKind::CharacterLiteralExpression:
return OS << "CharacterLiteralExpression";
case NodeKind::PrefixUnaryOperatorExpression:
return OS << "PrefixUnaryOperatorExpression";
case NodeKind::PostfixUnaryOperatorExpression:
@ -207,6 +209,11 @@ syntax::Leaf *syntax::BoolLiteralExpression::literalToken() {
findChild(syntax::NodeRole::LiteralToken));
}
syntax::Leaf *syntax::CharacterLiteralExpression::literalToken() {
return llvm::cast_or_null<syntax::Leaf>(
findChild(syntax::NodeRole::LiteralToken));
}
syntax::Leaf *syntax::CxxNullPtrExpression::nullPtrKeyword() {
return llvm::cast_or_null<syntax::Leaf>(
findChild(syntax::NodeRole::LiteralToken));

View File

@ -73,6 +73,10 @@ struct TestClangConfig {
return Language == Lang_C89 || Language == Lang_C99;
}
bool isCXX17OrLater() const {
return Language == Lang_CXX17 || Language == Lang_CXX20;
}
bool supportsCXXDynamicExceptionSpecification() const {
return Language == Lang_CXX03 || Language == Lang_CXX11 ||
Language == Lang_CXX14;
@ -1232,6 +1236,135 @@ void test() {
)txt"));
}
TEST_P(SyntaxTreeTest, CharacterLiteral) {
EXPECT_TRUE(treeDumpEqual(
R"cpp(
void test() {
'a';
'\n';
'\x20';
'\0';
L'a';
L'α';
}
)cpp",
R"txt(
*: TranslationUnit
`-SimpleDeclaration
|-void
|-SimpleDeclarator
| |-test
| `-ParametersAndQualifiers
| |-(
| `-)
`-CompoundStatement
|-{
|-ExpressionStatement
| |-CharacterLiteralExpression
| | `-'a'
| `-;
|-ExpressionStatement
| |-CharacterLiteralExpression
| | `-'\n'
| `-;
|-ExpressionStatement
| |-CharacterLiteralExpression
| | `-'\x20'
| `-;
|-ExpressionStatement
| |-CharacterLiteralExpression
| | `-'\0'
| `-;
|-ExpressionStatement
| |-CharacterLiteralExpression
| | `-L'a'
| `-;
|-ExpressionStatement
| |-CharacterLiteralExpression
| | `-L'α'
| `-;
`-}
)txt"));
}
TEST_P(SyntaxTreeTest, CharacterLiteralUtf) {
if (!GetParam().isCXX11OrLater()) {
return;
}
EXPECT_TRUE(treeDumpEqual(
R"cpp(
void test() {
u'a';
u'';
U'a';
U'🌲';
}
)cpp",
R"txt(
*: TranslationUnit
`-SimpleDeclaration
|-void
|-SimpleDeclarator
| |-test
| `-ParametersAndQualifiers
| |-(
| `-)
`-CompoundStatement
|-{
|-ExpressionStatement
| |-CharacterLiteralExpression
| | `-u'a'
| `-;
|-ExpressionStatement
| |-CharacterLiteralExpression
| | `-u''
| `-;
|-ExpressionStatement
| |-CharacterLiteralExpression
| | `-U'a'
| `-;
|-ExpressionStatement
| |-CharacterLiteralExpression
| | `-U'🌲'
| `-;
`-}
)txt"));
}
TEST_P(SyntaxTreeTest, CharacterLiteralUtf8) {
if (!GetParam().isCXX17OrLater()) {
return;
}
EXPECT_TRUE(treeDumpEqual(
R"cpp(
void test() {
u8'a';
u8'\x7f';
}
)cpp",
R"txt(
*: TranslationUnit
`-SimpleDeclaration
|-void
|-SimpleDeclarator
| |-test
| `-ParametersAndQualifiers
| |-(
| `-)
`-CompoundStatement
|-{
|-ExpressionStatement
| |-CharacterLiteralExpression
| | `-u8'a'
| `-;
|-ExpressionStatement
| |-CharacterLiteralExpression
| | `-u8'\x7f'
| `-;
`-}
)txt"));
}
TEST_P(SyntaxTreeTest, BoolLiteral) {
if (GetParam().hasBoolType()) {
return;