https://github.com/owenca updated https://github.com/llvm/llvm-project/pull/136476
>From e6374fe18fa7b4147d7e7dafe668728034fea946 Mon Sep 17 00:00:00 2001 From: Owen Pan <owenpi...@gmail.com> Date: Sun, 20 Apr 2025 00:47:22 -0700 Subject: [PATCH] [clang-format] Fix a bug in lexing C++ UDL ending in $ --- clang/lib/Format/FormatTokenLexer.cpp | 29 +++++++++++++++++++ clang/lib/Format/FormatTokenLexer.h | 1 + clang/unittests/Format/TokenAnnotatorTest.cpp | 6 ++++ 3 files changed, 36 insertions(+) diff --git a/clang/lib/Format/FormatTokenLexer.cpp b/clang/lib/Format/FormatTokenLexer.cpp index 5c4e1f814d9b7..a4c94ac411fe0 100644 --- a/clang/lib/Format/FormatTokenLexer.cpp +++ b/clang/lib/Format/FormatTokenLexer.cpp @@ -128,6 +128,12 @@ void FormatTokenLexer::tryMergePreviousTokens() { if (Style.isCpp() && tryTransformTryUsageForC()) return; + if ((Style.Language == FormatStyle::LK_Cpp || + Style.Language == FormatStyle::LK_ObjC) && + tryMergeUserDefinedLiteral()) { + return; + } + if (Style.isJavaScript() || Style.isCSharp()) { static const tok::TokenKind NullishCoalescingOperator[] = {tok::question, tok::question}; @@ -559,6 +565,29 @@ bool FormatTokenLexer::tryMergeGreaterGreater() { return true; } +bool FormatTokenLexer::tryMergeUserDefinedLiteral() { + if (Tokens.size() < 2) + return false; + + auto *First = Tokens.end() - 2; + auto &Suffix = First[1]; + if (Suffix->hasWhitespaceBefore() || Suffix->TokenText != "$") + return false; + + auto &Literal = First[0]; + if (!Literal->Tok.isLiteral()) + return false; + + auto &Text = Literal->TokenText; + if (!Text.ends_with("_")) + return false; + + Text = StringRef(Text.data(), Text.size() + 1); + ++Literal->ColumnWidth; + Tokens.erase(&Suffix); + return true; +} + bool FormatTokenLexer::tryMergeTokens(ArrayRef<tok::TokenKind> Kinds, TokenType NewType) { if (Tokens.size() < Kinds.size()) diff --git a/clang/lib/Format/FormatTokenLexer.h b/clang/lib/Format/FormatTokenLexer.h index 61474a3f9ada8..3f001bc69415d 100644 --- a/clang/lib/Format/FormatTokenLexer.h +++ b/clang/lib/Format/FormatTokenLexer.h @@ -48,6 +48,7 @@ class FormatTokenLexer { bool tryMergeLessLess(); bool tryMergeGreaterGreater(); + bool tryMergeUserDefinedLiteral(); bool tryMergeNSStringLiteral(); bool tryMergeJSPrivateIdentifier(); bool tryMergeCSharpStringLiteral(); diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp index 043ee2e088ddb..c0bdbb6bad09f 100644 --- a/clang/unittests/Format/TokenAnnotatorTest.cpp +++ b/clang/unittests/Format/TokenAnnotatorTest.cpp @@ -3969,6 +3969,12 @@ TEST_F(TokenAnnotatorTest, IdentifierPackage) { EXPECT_FALSE(Tokens[0]->isObjCAccessSpecifier()); } +TEST_F(TokenAnnotatorTest, UserDefinedLiteral) { + auto Tokens = annotate("auto dollars = 2_$;"); + ASSERT_EQ(Tokens.size(), 6u) << Tokens; + EXPECT_EQ(Tokens[3]->TokenText, "2_$"); +} + } // namespace } // namespace format } // namespace clang _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits