This revision was automatically updated to reflect the committed changes.
Closed by commit rG221d7bbe49cc: Add `CharLiteral` to SyntaxTree (authored by
eduucaldas).
Changed prior to commit:
https://reviews.llvm.org/D82312?vs=273343&id=273436#toc
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D82312/new/
https://reviews.llvm.org/D82312
Files:
clang/include/clang/Tooling/Syntax/Nodes.h
clang/lib/Tooling/Syntax/BuildTree.cpp
clang/lib/Tooling/Syntax/Nodes.cpp
clang/unittests/Tooling/Syntax/TreeTest.cpp
Index: clang/unittests/Tooling/Syntax/TreeTest.cpp
===================================================================
--- clang/unittests/Tooling/Syntax/TreeTest.cpp
+++ clang/unittests/Tooling/Syntax/TreeTest.cpp
@@ -73,6 +73,10 @@
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 @@
)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;
Index: clang/lib/Tooling/Syntax/Nodes.cpp
===================================================================
--- clang/lib/Tooling/Syntax/Nodes.cpp
+++ clang/lib/Tooling/Syntax/Nodes.cpp
@@ -24,6 +24,8 @@
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 @@
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));
Index: clang/lib/Tooling/Syntax/BuildTree.cpp
===================================================================
--- clang/lib/Tooling/Syntax/BuildTree.cpp
+++ clang/lib/Tooling/Syntax/BuildTree.cpp
@@ -661,6 +661,13 @@
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),
Index: clang/include/clang/Tooling/Syntax/Nodes.h
===================================================================
--- clang/include/clang/Tooling/Syntax/Nodes.h
+++ clang/include/clang/Tooling/Syntax/Nodes.h
@@ -46,6 +46,7 @@
CxxNullPtrExpression,
IntegerLiteralExpression,
BoolLiteralExpression,
+ CharacterLiteralExpression,
IdExpression,
// Statements.
@@ -255,6 +256,17 @@
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:
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits