Skip to content

Commit 1507898

Browse files
committed
[clang-format] Fix a bug in lexing C++ UDL ending in $
1 parent c99ffe5 commit 1507898

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

clang/lib/Format/FormatTokenLexer.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@ void FormatTokenLexer::tryMergePreviousTokens() {
128128
if (Style.isCpp() && tryTransformTryUsageForC())
129129
return;
130130

131+
if ((Style.Language == FormatStyle::LK_Cpp ||
132+
Style.Language == FormatStyle::LK_ObjC) &&
133+
tryMergeUserDefinedLiteral()) {
134+
return;
135+
}
136+
131137
if (Style.isJavaScript() || Style.isCSharp()) {
132138
static const tok::TokenKind NullishCoalescingOperator[] = {tok::question,
133139
tok::question};
@@ -559,6 +565,26 @@ bool FormatTokenLexer::tryMergeGreaterGreater() {
559565
return true;
560566
}
561567

568+
bool FormatTokenLexer::tryMergeUserDefinedLiteral() {
569+
if (Tokens.size() < 2)
570+
return false;
571+
572+
auto *First = Tokens.end() - 2;
573+
auto &Suffix = First[1];
574+
if (Suffix->hasWhitespaceBefore() || Suffix->TokenText != "$")
575+
return false;
576+
577+
auto &Literal = First[0];
578+
auto &Text = Literal->TokenText;
579+
if (!Literal->Tok.isLiteral() || !Text.ends_with("_"))
580+
return false;
581+
582+
Text = StringRef(Text.data(), Text.size() + 1);
583+
++Literal->ColumnWidth;
584+
Tokens.erase(&Suffix);
585+
return true;
586+
}
587+
562588
bool FormatTokenLexer::tryMergeTokens(ArrayRef<tok::TokenKind> Kinds,
563589
TokenType NewType) {
564590
if (Tokens.size() < Kinds.size())

clang/lib/Format/FormatTokenLexer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class FormatTokenLexer {
4848

4949
bool tryMergeLessLess();
5050
bool tryMergeGreaterGreater();
51+
bool tryMergeUserDefinedLiteral();
5152
bool tryMergeNSStringLiteral();
5253
bool tryMergeJSPrivateIdentifier();
5354
bool tryMergeCSharpStringLiteral();

clang/unittests/Format/TokenAnnotatorTest.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3969,6 +3969,12 @@ TEST_F(TokenAnnotatorTest, IdentifierPackage) {
39693969
EXPECT_FALSE(Tokens[0]->isObjCAccessSpecifier());
39703970
}
39713971

3972+
TEST_F(TokenAnnotatorTest, UserDefinedLiteral) {
3973+
auto Tokens = annotate("auto dollars = 2_$;");
3974+
ASSERT_EQ(Tokens.size(), 6u) << Tokens;
3975+
EXPECT_EQ(Tokens[3]->TokenText, "2_$");
3976+
}
3977+
39723978
} // namespace
39733979
} // namespace format
39743980
} // namespace clang

0 commit comments

Comments
 (0)