https://github.com/fauxprogrammer updated https://github.com/llvm/llvm-project/pull/182296
>From 2549cdd1153f0336d9f9e5a2e8b479ad08a34c6e Mon Sep 17 00:00:00 2001 From: John Mitchell <[email protected]> Date: Thu, 19 Feb 2026 08:54:09 -0600 Subject: [PATCH 1/3] Add C language support for clang-format IntegerLiteralSeparator --- clang/docs/ClangFormatStyleOptions.rst | 4 +- .../Format/IntegerLiteralSeparatorFixer.cpp | 3 + .../Format/IntegerLiteralSeparatorTest.cpp | 119 +++++++++--------- 3 files changed, 68 insertions(+), 58 deletions(-) diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst index 437f559fc0395..e3005759deeda 100644 --- a/clang/docs/ClangFormatStyleOptions.rst +++ b/clang/docs/ClangFormatStyleOptions.rst @@ -4751,8 +4751,8 @@ the configuration (without a prefix: ``Auto``). .. _IntegerLiteralSeparator: **IntegerLiteralSeparator** (``IntegerLiteralSeparatorStyle``) :versionbadge:`clang-format 16` :ref:`¶ <IntegerLiteralSeparator>` - Format integer literal separators (``'`` for C++ and ``_`` for C#, Java, - and JavaScript). + Format integer literal separators (``'`` for C, C++ and ``_`` for C#, Java, + and JavaScript). Note that using the separator with C requires a C23+ compiler. Nested configuration flags: diff --git a/clang/lib/Format/IntegerLiteralSeparatorFixer.cpp b/clang/lib/Format/IntegerLiteralSeparatorFixer.cpp index a283884b6c341..1256925bf6d7d 100644 --- a/clang/lib/Format/IntegerLiteralSeparatorFixer.cpp +++ b/clang/lib/Format/IntegerLiteralSeparatorFixer.cpp @@ -50,6 +50,9 @@ IntegerLiteralSeparatorFixer::process(const Environment &Env, case FormatStyle::LK_JavaScript: Separator = '_'; break; + case FormatStyle::LK_C: + Separator = '\''; + break; case FormatStyle::LK_Cpp: case FormatStyle::LK_ObjC: if (Style.Standard >= FormatStyle::LS_Cpp14) { diff --git a/clang/unittests/Format/IntegerLiteralSeparatorTest.cpp b/clang/unittests/Format/IntegerLiteralSeparatorTest.cpp index 21cdab2187d90..8401c1994a1e3 100644 --- a/clang/unittests/Format/IntegerLiteralSeparatorTest.cpp +++ b/clang/unittests/Format/IntegerLiteralSeparatorTest.cpp @@ -24,65 +24,72 @@ TEST_F(IntegerLiteralSeparatorTest, SingleQuoteAsSeparator) { EXPECT_EQ(Style.IntegerLiteralSeparator.Decimal, 0); EXPECT_EQ(Style.IntegerLiteralSeparator.Hex, 0); - constexpr StringRef Binary("b = 0b10011'11'0110'1u;"); - verifyFormat(Binary, Style); - Style.IntegerLiteralSeparator.Binary = -1; - verifyFormat("b = 0b100111101101u;", Binary, Style); - Style.IntegerLiteralSeparator.Binary = 1; - verifyFormat("b = 0b1'0'0'1'1'1'1'0'1'1'0'1u;", Binary, Style); - Style.IntegerLiteralSeparator.Binary = 4; - verifyFormat("b = 0b1001'1110'1101u;", Binary, Style); - - constexpr StringRef Decimal("d = 184467'440737'0'95505'92Ull;"); - verifyFormat(Decimal, Style); - Style.IntegerLiteralSeparator.Decimal = -1; - verifyFormat("d = 18446744073709550592Ull;", Decimal, Style); - Style.IntegerLiteralSeparator.Decimal = 3; - verifyFormat("d = 18'446'744'073'709'550'592Ull;", Decimal, Style); - - constexpr StringRef Hex("h = 0xDEAD'BEEF'DE'AD'BEE'Fuz;"); - verifyFormat(Hex, Style); - Style.IntegerLiteralSeparator.Hex = -1; - verifyFormat("h = 0xDEADBEEFDEADBEEFuz;", Hex, Style); - Style.IntegerLiteralSeparator.Hex = 2; - verifyFormat("h = 0xDE'AD'BE'EF'DE'AD'BE'EFuz;", Hex, Style); - - verifyFormat("o0 = 0;\n" - "o1 = 07;\n" - "o5 = 012345;", - Style); - - verifyFormat("bi = 0b1'0000i;\n" - "dif = 1'234if;\n" - "hil = 0xA'BCil;", - "bi = 0b10000i;\n" - "dif = 1234if;\n" - "hil = 0xABCil;", - Style); + auto TestSingleQuote = [&](auto Language) { + Style.Language = Language; + + constexpr StringRef Binary("b = 0b10011'11'0110'1u;"); + verifyFormat(Binary, Style); + Style.IntegerLiteralSeparator.Binary = -1; + verifyFormat("b = 0b100111101101u;", Binary, Style); + Style.IntegerLiteralSeparator.Binary = 1; + verifyFormat("b = 0b1'0'0'1'1'1'1'0'1'1'0'1u;", Binary, Style); + Style.IntegerLiteralSeparator.Binary = 4; + verifyFormat("b = 0b1001'1110'1101u;", Binary, Style); - verifyFormat("bd = 0b1'0000d;\n" - "dh = 1'234h;\n" - "dmin = 1'234min;\n" - "dns = 1'234ns;\n" - "ds = 1'234s;\n" - "dus = 1'234us;\n" - "hy = 0xA'BCy;", - "bd = 0b10000d;\n" - "dh = 1234h;\n" - "dmin = 1234min;\n" - "dns = 1234ns;\n" - "ds = 1234s;\n" - "dus = 1234us;\n" - "hy = 0xABCy;", - Style); + constexpr StringRef Decimal("d = 184467'440737'0'95505'92Ull;"); + verifyFormat(Decimal, Style); + Style.IntegerLiteralSeparator.Decimal = -1; + verifyFormat("d = 18446744073709550592Ull;", Decimal, Style); + Style.IntegerLiteralSeparator.Decimal = 3; + verifyFormat("d = 18'446'744'073'709'550'592Ull;", Decimal, Style); - verifyFormat("hd = 0xAB'Cd;", "hd = 0xABCd;", Style); + constexpr StringRef Hex("h = 0xDEAD'BEEF'DE'AD'BEE'Fuz;"); + verifyFormat(Hex, Style); + Style.IntegerLiteralSeparator.Hex = -1; + verifyFormat("h = 0xDEADBEEFDEADBEEFuz;", Hex, Style); + Style.IntegerLiteralSeparator.Hex = 2; + verifyFormat("h = 0xDE'AD'BE'EF'DE'AD'BE'EFuz;", Hex, Style); + + verifyFormat("o0 = 0;\n" + "o1 = 07;\n" + "o5 = 012345;", + Style); + + verifyFormat("bi = 0b1'0000i;\n" + "dif = 1'234if;\n" + "hil = 0xA'BCil;", + "bi = 0b10000i;\n" + "dif = 1234if;\n" + "hil = 0xABCil;", + Style); + + verifyFormat("bd = 0b1'0000d;\n" + "dh = 1'234h;\n" + "dmin = 1'234min;\n" + "dns = 1'234ns;\n" + "ds = 1'234s;\n" + "dus = 1'234us;\n" + "hy = 0xA'BCy;", + "bd = 0b10000d;\n" + "dh = 1234h;\n" + "dmin = 1234min;\n" + "dns = 1234ns;\n" + "ds = 1234s;\n" + "dus = 1234us;\n" + "hy = 0xABCy;", + Style); + + verifyFormat("hd = 0xAB'Cd;", "hd = 0xABCd;", Style); + + verifyFormat("d = 5'678_km;\n" + "h = 0xD'EF_u16;", + "d = 5678_km;\n" + "h = 0xDEF_u16;", + Style); + }; - verifyFormat("d = 5'678_km;\n" - "h = 0xD'EF_u16;", - "d = 5678_km;\n" - "h = 0xDEF_u16;", - Style); + TestSingleQuote(FormatStyle::LK_C); + TestSingleQuote(FormatStyle::LK_Cpp); Style.Standard = FormatStyle::LS_Cpp11; verifyFormat("ld = 1234L;", Style); >From 024441dfc8aa4bab32d5b50de9b3a687420edf6d Mon Sep 17 00:00:00 2001 From: John Mitchell <[email protected]> Date: Sun, 22 Feb 2026 12:16:57 -0600 Subject: [PATCH 2/3] Incorporate changes per reviewer comments --- clang/docs/ClangFormatStyleOptions.rst | 4 +- clang/include/clang/Format/Format.h | 2 +- .../Format/IntegerLiteralSeparatorFixer.cpp | 2 - .../Format/IntegerLiteralSeparatorTest.cpp | 132 +++++++++--------- 4 files changed, 69 insertions(+), 71 deletions(-) diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst index e3005759deeda..4a0cfc2545352 100644 --- a/clang/docs/ClangFormatStyleOptions.rst +++ b/clang/docs/ClangFormatStyleOptions.rst @@ -4751,8 +4751,8 @@ the configuration (without a prefix: ``Auto``). .. _IntegerLiteralSeparator: **IntegerLiteralSeparator** (``IntegerLiteralSeparatorStyle``) :versionbadge:`clang-format 16` :ref:`¶ <IntegerLiteralSeparator>` - Format integer literal separators (``'`` for C, C++ and ``_`` for C#, Java, - and JavaScript). Note that using the separator with C requires a C23+ compiler. + Format integer literal separators (``'`` for C/C++ and ``_`` for C#, Java, + and JavaScript). Nested configuration flags: diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index ca0bd47d10613..8d5cbea4494c7 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -3395,7 +3395,7 @@ struct FormatStyle { } }; - /// Format integer literal separators (``'`` for C++ and ``_`` for C#, Java, + /// Format integer literal separators (``'`` for C/C++ and ``_`` for C#, Java, /// and JavaScript). /// \version 16 IntegerLiteralSeparatorStyle IntegerLiteralSeparator; diff --git a/clang/lib/Format/IntegerLiteralSeparatorFixer.cpp b/clang/lib/Format/IntegerLiteralSeparatorFixer.cpp index 1256925bf6d7d..5f40100c2b968 100644 --- a/clang/lib/Format/IntegerLiteralSeparatorFixer.cpp +++ b/clang/lib/Format/IntegerLiteralSeparatorFixer.cpp @@ -51,8 +51,6 @@ IntegerLiteralSeparatorFixer::process(const Environment &Env, Separator = '_'; break; case FormatStyle::LK_C: - Separator = '\''; - break; case FormatStyle::LK_Cpp: case FormatStyle::LK_ObjC: if (Style.Standard >= FormatStyle::LS_Cpp14) { diff --git a/clang/unittests/Format/IntegerLiteralSeparatorTest.cpp b/clang/unittests/Format/IntegerLiteralSeparatorTest.cpp index 8401c1994a1e3..511af1ba6ce2b 100644 --- a/clang/unittests/Format/IntegerLiteralSeparatorTest.cpp +++ b/clang/unittests/Format/IntegerLiteralSeparatorTest.cpp @@ -23,76 +23,76 @@ TEST_F(IntegerLiteralSeparatorTest, SingleQuoteAsSeparator) { EXPECT_EQ(Style.IntegerLiteralSeparator.Binary, 0); EXPECT_EQ(Style.IntegerLiteralSeparator.Decimal, 0); EXPECT_EQ(Style.IntegerLiteralSeparator.Hex, 0); + + constexpr StringRef Binary("b = 0b10011'11'0110'1u;"); + verifyFormat(Binary, Style); + Style.IntegerLiteralSeparator.Binary = -1; + verifyFormat("b = 0b100111101101u;", Binary, Style); + Style.IntegerLiteralSeparator.Binary = 1; + verifyFormat("b = 0b1'0'0'1'1'1'1'0'1'1'0'1u;", Binary, Style); + Style.IntegerLiteralSeparator.Binary = 4; + verifyFormat("b = 0b1001'1110'1101u;", Binary, Style); + + constexpr StringRef Decimal("d = 184467'440737'0'95505'92Ull;"); + verifyFormat(Decimal, Style); + Style.IntegerLiteralSeparator.Decimal = -1; + verifyFormat("d = 18446744073709550592Ull;", Decimal, Style); + Style.IntegerLiteralSeparator.Decimal = 3; + verifyFormat("d = 18'446'744'073'709'550'592Ull;", Decimal, Style); - auto TestSingleQuote = [&](auto Language) { - Style.Language = Language; - - constexpr StringRef Binary("b = 0b10011'11'0110'1u;"); - verifyFormat(Binary, Style); - Style.IntegerLiteralSeparator.Binary = -1; - verifyFormat("b = 0b100111101101u;", Binary, Style); - Style.IntegerLiteralSeparator.Binary = 1; - verifyFormat("b = 0b1'0'0'1'1'1'1'0'1'1'0'1u;", Binary, Style); - Style.IntegerLiteralSeparator.Binary = 4; - verifyFormat("b = 0b1001'1110'1101u;", Binary, Style); - - constexpr StringRef Decimal("d = 184467'440737'0'95505'92Ull;"); - verifyFormat(Decimal, Style); - Style.IntegerLiteralSeparator.Decimal = -1; - verifyFormat("d = 18446744073709550592Ull;", Decimal, Style); - Style.IntegerLiteralSeparator.Decimal = 3; - verifyFormat("d = 18'446'744'073'709'550'592Ull;", Decimal, Style); - - constexpr StringRef Hex("h = 0xDEAD'BEEF'DE'AD'BEE'Fuz;"); - verifyFormat(Hex, Style); - Style.IntegerLiteralSeparator.Hex = -1; - verifyFormat("h = 0xDEADBEEFDEADBEEFuz;", Hex, Style); - Style.IntegerLiteralSeparator.Hex = 2; - verifyFormat("h = 0xDE'AD'BE'EF'DE'AD'BE'EFuz;", Hex, Style); - - verifyFormat("o0 = 0;\n" - "o1 = 07;\n" - "o5 = 012345;", - Style); - - verifyFormat("bi = 0b1'0000i;\n" - "dif = 1'234if;\n" - "hil = 0xA'BCil;", - "bi = 0b10000i;\n" - "dif = 1234if;\n" - "hil = 0xABCil;", - Style); - - verifyFormat("bd = 0b1'0000d;\n" - "dh = 1'234h;\n" - "dmin = 1'234min;\n" - "dns = 1'234ns;\n" - "ds = 1'234s;\n" - "dus = 1'234us;\n" - "hy = 0xA'BCy;", - "bd = 0b10000d;\n" - "dh = 1234h;\n" - "dmin = 1234min;\n" - "dns = 1234ns;\n" - "ds = 1234s;\n" - "dus = 1234us;\n" - "hy = 0xABCy;", - Style); - - verifyFormat("hd = 0xAB'Cd;", "hd = 0xABCd;", Style); - - verifyFormat("d = 5'678_km;\n" - "h = 0xD'EF_u16;", - "d = 5678_km;\n" - "h = 0xDEF_u16;", - Style); - }; - - TestSingleQuote(FormatStyle::LK_C); - TestSingleQuote(FormatStyle::LK_Cpp); + constexpr StringRef Hex("h = 0xDEAD'BEEF'DE'AD'BEE'Fuz;"); + verifyFormat(Hex, Style); + Style.IntegerLiteralSeparator.Hex = -1; + verifyFormat("h = 0xDEADBEEFDEADBEEFuz;", Hex, Style); + Style.IntegerLiteralSeparator.Hex = 2; + verifyFormat("h = 0xDE'AD'BE'EF'DE'AD'BE'EFuz;", Hex, Style); + + verifyFormat("o0 = 0;\n" + "o1 = 07;\n" + "o5 = 012345;", + Style); + + verifyFormat("bi = 0b1'0000i;\n" + "dif = 1'234if;\n" + "hil = 0xA'BCil;", + "bi = 0b10000i;\n" + "dif = 1234if;\n" + "hil = 0xABCil;", + Style); + + verifyFormat("bd = 0b1'0000d;\n" + "dh = 1'234h;\n" + "dmin = 1'234min;\n" + "dns = 1'234ns;\n" + "ds = 1'234s;\n" + "dus = 1'234us;\n" + "hy = 0xA'BCy;", + "bd = 0b10000d;\n" + "dh = 1234h;\n" + "dmin = 1234min;\n" + "dns = 1234ns;\n" + "ds = 1234s;\n" + "dus = 1234us;\n" + "hy = 0xABCy;", + Style); + + verifyFormat("hd = 0xAB'Cd;", "hd = 0xABCd;", Style); + + verifyFormat("d = 5'678_km;\n" + "h = 0xD'EF_u16;", + "d = 5678_km;\n" + "h = 0xDEF_u16;", + Style); Style.Standard = FormatStyle::LS_Cpp11; verifyFormat("ld = 1234L;", Style); + + Style.Language = FormatStyle::LK_C; + Style.Standard = FormatStyle::LS_Cpp14; + Style.IntegerLiteralSeparator.Binary = -1; + verifyFormat("b = 0b100111101101u;", Binary, Style); + Style.IntegerLiteralSeparator.Binary = 4; + verifyFormat("b = 0b1001'1110'1101u;", Binary, Style); } TEST_F(IntegerLiteralSeparatorTest, UnderscoreAsSeparator) { >From e2deffe95a66167121fbbccc16b79963570676b4 Mon Sep 17 00:00:00 2001 From: John Mitchell <[email protected]> Date: Sun, 22 Feb 2026 12:55:03 -0600 Subject: [PATCH 3/3] Update IntegerLiteralSeparatorTest.cpp Based on reviewer comments reverting to original file without a test for C. --- .../Format/IntegerLiteralSeparatorTest.cpp | 63 +++++++++---------- 1 file changed, 28 insertions(+), 35 deletions(-) diff --git a/clang/unittests/Format/IntegerLiteralSeparatorTest.cpp b/clang/unittests/Format/IntegerLiteralSeparatorTest.cpp index 511af1ba6ce2b..21cdab2187d90 100644 --- a/clang/unittests/Format/IntegerLiteralSeparatorTest.cpp +++ b/clang/unittests/Format/IntegerLiteralSeparatorTest.cpp @@ -23,7 +23,7 @@ TEST_F(IntegerLiteralSeparatorTest, SingleQuoteAsSeparator) { EXPECT_EQ(Style.IntegerLiteralSeparator.Binary, 0); EXPECT_EQ(Style.IntegerLiteralSeparator.Decimal, 0); EXPECT_EQ(Style.IntegerLiteralSeparator.Hex, 0); - + constexpr StringRef Binary("b = 0b10011'11'0110'1u;"); verifyFormat(Binary, Style); Style.IntegerLiteralSeparator.Binary = -1; @@ -48,51 +48,44 @@ TEST_F(IntegerLiteralSeparatorTest, SingleQuoteAsSeparator) { verifyFormat("h = 0xDE'AD'BE'EF'DE'AD'BE'EFuz;", Hex, Style); verifyFormat("o0 = 0;\n" - "o1 = 07;\n" - "o5 = 012345;", - Style); + "o1 = 07;\n" + "o5 = 012345;", + Style); verifyFormat("bi = 0b1'0000i;\n" - "dif = 1'234if;\n" - "hil = 0xA'BCil;", - "bi = 0b10000i;\n" - "dif = 1234if;\n" - "hil = 0xABCil;", - Style); + "dif = 1'234if;\n" + "hil = 0xA'BCil;", + "bi = 0b10000i;\n" + "dif = 1234if;\n" + "hil = 0xABCil;", + Style); verifyFormat("bd = 0b1'0000d;\n" - "dh = 1'234h;\n" - "dmin = 1'234min;\n" - "dns = 1'234ns;\n" - "ds = 1'234s;\n" - "dus = 1'234us;\n" - "hy = 0xA'BCy;", - "bd = 0b10000d;\n" - "dh = 1234h;\n" - "dmin = 1234min;\n" - "dns = 1234ns;\n" - "ds = 1234s;\n" - "dus = 1234us;\n" - "hy = 0xABCy;", - Style); + "dh = 1'234h;\n" + "dmin = 1'234min;\n" + "dns = 1'234ns;\n" + "ds = 1'234s;\n" + "dus = 1'234us;\n" + "hy = 0xA'BCy;", + "bd = 0b10000d;\n" + "dh = 1234h;\n" + "dmin = 1234min;\n" + "dns = 1234ns;\n" + "ds = 1234s;\n" + "dus = 1234us;\n" + "hy = 0xABCy;", + Style); verifyFormat("hd = 0xAB'Cd;", "hd = 0xABCd;", Style); verifyFormat("d = 5'678_km;\n" - "h = 0xD'EF_u16;", - "d = 5678_km;\n" - "h = 0xDEF_u16;", - Style); + "h = 0xD'EF_u16;", + "d = 5678_km;\n" + "h = 0xDEF_u16;", + Style); Style.Standard = FormatStyle::LS_Cpp11; verifyFormat("ld = 1234L;", Style); - - Style.Language = FormatStyle::LK_C; - Style.Standard = FormatStyle::LS_Cpp14; - Style.IntegerLiteralSeparator.Binary = -1; - verifyFormat("b = 0b100111101101u;", Binary, Style); - Style.IntegerLiteralSeparator.Binary = 4; - verifyFormat("b = 0b1001'1110'1101u;", Binary, Style); } TEST_F(IntegerLiteralSeparatorTest, UnderscoreAsSeparator) { _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
