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 @@ -623,6 +623,19 @@ return NNS; } + bool TraverseUserDefinedLiteral(UserDefinedLiteral *S) { return true; } + + bool WalkUpFromUserDefinedLiteral(UserDefinedLiteral *S) { + // The user-defined-literal `1.2_w` is not split in the tokens `1.2` and + // `_w` as we might imagine, its tokens are `1.2_w` only. + // As a node is built by folding tokens we cannot generate one + // for `_w` + Builder.markChildToken(S->getBeginLoc(), syntax::NodeRole::LiteralToken); + Builder.foldNode(Builder.getExprRange(S), + new (allocator()) syntax::IntegerLiteralExpression, S); + return true; + } + bool WalkUpFromDeclRefExpr(DeclRefExpr *S) { if (auto *NNS = BuildNestedNameSpecifier(S->getQualifierLoc())) Builder.markChild(NNS, syntax::NodeRole::IdExpression_qualifier); 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 @@ -1228,6 +1228,93 @@ )txt")); } +TEST_P(SyntaxTreeTest, UserDefinedLiteral) { + if (!GetParam().isCXX11OrLater()) { + return; + } + EXPECT_TRUE(treeDumpEqual( + R"cpp( +long double operator "" _w(long double); +unsigned operator "" _w(const char*); +template unsigned operator "" _x(); +int main() { + 1.2_w; // calls operator "" _w(1.2L) + 12_w; // calls operator "" _w("12") + 12_x; // calls operator<'1', '2'> "" _x() +} + )cpp", + R"txt( +*: TranslationUnit +|-SimpleDeclaration +| |-long +| |-double +| |-SimpleDeclarator +| | |-operator +| | |-"" +| | |-_w +| | `-ParametersAndQualifiers +| | |-( +| | |-SimpleDeclaration +| | | |-long +| | | `-double +| | `-) +| `-; +|-SimpleDeclaration +| |-unsigned +| |-SimpleDeclarator +| | |-operator +| | |-"" +| | |-_w +| | `-ParametersAndQualifiers +| | |-( +| | |-SimpleDeclaration +| | | |-const +| | | |-char +| | | `-SimpleDeclarator +| | | `-* +| | `-) +| `-; +|-TemplateDeclaration +| |-template +| |-< +| |-SimpleDeclaration +| | `-char +| |-... +| |-> +| `-SimpleDeclaration +| |-unsigned +| |-SimpleDeclarator +| | |-operator +| | |-"" +| | |-_x +| | `-ParametersAndQualifiers +| | |-( +| | `-) +| `-; +`-SimpleDeclaration + |-int + |-SimpleDeclarator + | |-main + | `-ParametersAndQualifiers + | |-( + | `-) + `-CompoundStatement + |-{ + |-ExpressionStatement + | |-UserDefinedLiteralExpression + | | `-1.2_w + | `-; + |-ExpressionStatement + | |-UserDefinedLiteralExpression + | | `-12_w + | `-; + |-ExpressionStatement + | |-UserDefinedLiteralExpression + | | `-12_x + | `-; + `-} +)txt")); +} TEST_P(SyntaxTreeTest, IntegerLiteral) { EXPECT_TRUE(treeDumpEqual( R"cpp(