[clang] [clang-format] Add an option to format integer and float literal case (PR #151590)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Aug 13 23:24:47 PDT 2025
================
@@ -0,0 +1,354 @@
+//===- unittest/Format/NumericLiteralCaseTest.cpp --------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "FormatTestBase.h"
+
+#define DEBUG_TYPE "numeric-literal-case-test"
+
+namespace clang {
+namespace format {
+namespace test {
+namespace {
+
+class NumericLiteralCaseTest : public FormatTestBase {};
+
+TEST_F(NumericLiteralCaseTest, Prefix) {
+ FormatStyle Style = getLLVMStyle();
+ EXPECT_EQ(Style.Language, FormatStyle::LK_Cpp);
+ EXPECT_EQ(Style.NumericLiteralCase.Prefix, FormatStyle::NLCS_Leave);
+ EXPECT_EQ(Style.NumericLiteralCase.HexDigit, FormatStyle::NLCS_Leave);
+ EXPECT_EQ(Style.NumericLiteralCase.ExponentLetter, FormatStyle::NLCS_Leave);
+ EXPECT_EQ(Style.NumericLiteralCase.Suffix, FormatStyle::NLCS_Leave);
+
+ const StringRef Bin0{"b = 0b0'10'010uL;"};
+ const StringRef Bin1{"b = 0B010'010Ul;"};
+ const StringRef Hex0{"b = 0xdead'BEEFuL;"};
+ const StringRef Hex1{"b = 0Xdead'BEEFUl;"};
+ verifyFormat(Bin0, Style);
+ verifyFormat(Bin1, Style);
+ verifyFormat(Hex0, Style);
+ verifyFormat(Hex1, Style);
+
+ Style.NumericLiteralCase.Prefix = FormatStyle::NLCS_Upper;
+ verifyFormat("b = 0B0'10'010uL;", Bin0, Style);
+ verifyFormat(Bin1, Style);
+ verifyFormat("b = 0Xdead'BEEFuL;", Hex0, Style);
+ verifyFormat(Hex1, Style);
+ verifyFormat("i = 0XaBcD.a0Ebp123F;", Style);
+ verifyFormat("j = 0XaBcD.a0EbP123f;", Style);
+
+ Style.NumericLiteralCase.Prefix = FormatStyle::NLCS_Lower;
+ verifyFormat(Bin0, Style);
+ verifyFormat("b = 0b010'010Ul;", Bin1, Style);
+ verifyFormat(Hex0, Style);
+ verifyFormat("b = 0xdead'BEEFUl;", Hex1, Style);
+}
+
+TEST_F(NumericLiteralCaseTest, HexDigit) {
+ FormatStyle Style = getLLVMStyle();
+
+ const StringRef A{"a = 0xaBc0'123fuL;"};
+ const StringRef B{"b = 0XaBc0'123FUl;"};
+ const StringRef C{"c = 0xa'Bc.0p12'3f32;"};
+ const StringRef D{"d = 0xa'Bc.0P12'3F128;"};
+ const StringRef E{"e = 0b0011'00Ull;"};
+ const StringRef F{"f = 0B0100'000zu;"};
+ const StringRef G{"g = 0.123e-19f;"};
+ const StringRef H{"h = 0.12'3E-19F16;"};
+ const StringRef I{"i = 0x.0000aBcp12'3F128;"};
+ const StringRef J{"j = 0xaa1'fP12'3F128;"};
+ const StringRef K{"k = 0x0;"};
+ const StringRef L{"l = 0xA;"};
+
+ verifyFormat(A, Style);
+ verifyFormat(B, Style);
+ verifyFormat(C, Style);
+ verifyFormat(D, Style);
+ verifyFormat(E, Style);
+ verifyFormat(F, Style);
+ verifyFormat(G, Style);
+ verifyFormat(H, Style);
+ verifyFormat(I, Style);
+ verifyFormat(J, Style);
+
+ Style.NumericLiteralCase.HexDigit = FormatStyle::NLCS_Upper;
+ verifyFormat("a = 0xABC0'123FuL;", A, Style);
+ verifyFormat("b = 0XABC0'123FUl;", B, Style);
+ verifyFormat("c = 0xA'BC.0p12'3f32;", C, Style);
+ verifyFormat("d = 0xA'BC.0P12'3F128;", D, Style);
+ verifyFormat("e = 0b0011'00Ull;", E, Style);
+ verifyFormat("f = 0B0100'000zu;", F, Style);
+ verifyFormat("g = 0.123e-19f;", G, Style);
+ verifyFormat("h = 0.12'3E-19F16;", H, Style);
+ verifyFormat("i = 0x.0000ABCp12'3F128;", I, Style);
+ verifyFormat("j = 0xAA1'FP12'3F128;", J, Style);
+
+ Style.NumericLiteralCase.HexDigit = FormatStyle::NLCS_Lower;
+ verifyFormat("a = 0xabc0'123fuL;", A, Style);
+ verifyFormat("b = 0Xabc0'123fUl;", B, Style);
+ verifyFormat("c = 0xa'bc.0p12'3f32;", C, Style);
+ verifyFormat("d = 0xa'bc.0P12'3F128;", D, Style);
+ verifyFormat("e = 0b0011'00Ull;", E, Style);
+ verifyFormat("f = 0B0100'000zu;", F, Style);
+ verifyFormat("g = 0.123e-19f;", G, Style);
+ verifyFormat("h = 0.12'3E-19F16;", H, Style);
+ verifyFormat("i = 0x.0000abcp12'3F128;", I, Style);
+ verifyFormat("j = 0xaa1'fP12'3F128;", J, Style);
+}
+
+TEST_F(NumericLiteralCaseTest, ExponentLetter) {
+ FormatStyle Style = getLLVMStyle();
+
+ const StringRef A{"a = .0'01e-19f;"};
+ const StringRef B{"b = .00'1E2F;"};
+ const StringRef C{"c = 10'2.e99;"};
+ const StringRef D{"d = 123.456E-1;"};
+ const StringRef E{"e = 0x12abEe3.456p-10'0;"};
+ const StringRef F{"f = 0x.deEfP23;"};
+ const StringRef G{"g = 0xe0E1.p-1;"};
+
+ verifyFormat(A, Style);
+ verifyFormat(B, Style);
+ verifyFormat(C, Style);
+ verifyFormat(D, Style);
+ verifyFormat(E, Style);
+ verifyFormat(F, Style);
+ verifyFormat(G, Style);
+
+ Style.NumericLiteralCase.ExponentLetter = FormatStyle::NLCS_Lower;
+ verifyFormat(A, Style);
+ verifyFormat("b = .00'1e2F;", B, Style);
+ verifyFormat(C, Style);
+ verifyFormat("d = 123.456e-1;", D, Style);
+ verifyFormat(E, Style);
+ verifyFormat("f = 0x.deEfp23;", F, Style);
+ verifyFormat(G, Style);
+
+ Style.NumericLiteralCase.ExponentLetter = FormatStyle::NLCS_Upper;
+ verifyFormat("a = .0'01E-19f;", A, Style);
+ verifyFormat(B, Style);
+ verifyFormat("c = 10'2.E99;", C, Style);
+ verifyFormat(D, Style);
+ verifyFormat("e = 0x12abEe3.456P-10'0;", E, Style);
+ verifyFormat(F, Style);
+ verifyFormat("g = 0xe0E1.P-1;", G, Style);
+}
+
+TEST_F(NumericLiteralCaseTest, IntegerSuffix) {
+ FormatStyle Style = getLLVMStyle();
+
+ const StringRef A{"a = 102u;"};
+ const StringRef B{"b = 0177U;"};
+ const StringRef C{"c = 0b101'111llU;"};
+ const StringRef D{"d = 0xdead'BeefuZ;"};
+ const StringRef E{"e = 3lU;"};
+ const StringRef F{"f = 1zu;"};
+ const StringRef G{"g = 0uLL;"};
+ const StringRef H{"h = 10'233'213'0101uLL;"};
+
+ verifyFormat(A, Style);
+ verifyFormat(B, Style);
+ verifyFormat(C, Style);
+ verifyFormat(D, Style);
+ verifyFormat(E, Style);
+ verifyFormat(F, Style);
+ verifyFormat(G, Style);
+ verifyFormat(H, Style);
+
+ Style.NumericLiteralCase.Suffix = FormatStyle::NLCS_Lower;
+ verifyFormat(A, Style);
+ verifyFormat("b = 0177u;", B, Style);
+ verifyFormat("c = 0b101'111llu;", C, Style);
+ verifyFormat("d = 0xdead'Beefuz;", D, Style);
+ verifyFormat("e = 3lu;", E, Style);
+ verifyFormat(F, Style);
+ verifyFormat("g = 0ull;", G, Style);
+ verifyFormat("h = 10'233'213'0101ull;", H, Style);
+
+ Style.NumericLiteralCase.Suffix = FormatStyle::NLCS_Upper;
+ verifyFormat("a = 102U;", A, Style);
+ verifyFormat(B, Style);
+ verifyFormat("c = 0b101'111LLU;", C, Style);
+ verifyFormat("d = 0xdead'BeefUZ;", D, Style);
+ verifyFormat("e = 3LU;", E, Style);
+ verifyFormat("f = 1ZU;", F, Style);
+ verifyFormat("g = 0ULL;", G, Style);
+ verifyFormat("h = 10'233'213'0101ULL;", H, Style);
+}
+
+TEST_F(NumericLiteralCaseTest, FloatingPointSuffix) {
+ FormatStyle Style = getLLVMStyle();
+ // Floating point literals without suffixes.
+ std::vector<StringRef> FloatingPointStatements = {
+ StringRef("a = 0."), StringRef("b = 1.0"),
+ StringRef("c = .123'45E-10"), StringRef("d = 12'3.0e1"),
+ StringRef("e = 0Xa0eE.P10"), StringRef("f = 0xeE01.aFf3p6"),
+ };
+
+ struct FloatSuffix {
+ StringRef Lower;
+ StringRef Upper;
+ };
+ // All legal floating point literals defined in the C++23 standard.
+ std::vector<FloatSuffix> FloatingPointSuffixes = {
+ {"f", "F"}, {"l", "L"}, {"f16", "F16"}, {"f32", "F32"},
+ {"f64", "F64"}, {"f128", "F128"}, {"bf16", "BF16"},
+ };
+
+ // Test all combinations of literals with suffixes.
+ for (auto &Statement : FloatingPointStatements) {
+ for (auto &Suffix : FloatingPointSuffixes) {
+ std::string LowerLine =
+ std::string{Statement} + std::string{Suffix.Lower} + ";";
+ std::string UpperLine =
+ std::string{Statement} + std::string{Suffix.Upper} + ";";
+
+ Style.NumericLiteralCase.Suffix = FormatStyle::NLCS_Leave;
+ verifyFormat(LowerLine, Style);
+ verifyFormat(UpperLine, Style);
+
+ Style.NumericLiteralCase.Suffix = FormatStyle::NLCS_Lower;
+ verifyFormat(LowerLine, Style);
+ verifyFormat(LowerLine, UpperLine, Style);
+
+ Style.NumericLiteralCase.Suffix = FormatStyle::NLCS_Upper;
+ verifyFormat(UpperLine, LowerLine, Style);
+ verifyFormat(UpperLine, Style);
+ }
+ }
+}
+
+TEST_F(NumericLiteralCaseTest, CppStandardAndUserDefinedLiteralsAreUntouched) {
+ FormatStyle Style = getLLVMStyle();
+ Style.NumericLiteralCase.Prefix = FormatStyle::NLCS_Upper;
+ Style.NumericLiteralCase.HexDigit = FormatStyle::NLCS_Upper;
+ Style.NumericLiteralCase.ExponentLetter = FormatStyle::NLCS_Upper;
+ Style.NumericLiteralCase.Suffix = FormatStyle::NLCS_Upper;
+
+ // C++ user-defined suffixes begin with '_' or are reserved for the standard
+ // library.
+ const StringRef UDLiterals{"a = 12.if;\n"
+ "b = -3i;\n"
+ "c = 100'01il;\n"
+ "d = 100'0.12il;\n"
+ "e = 12h;\n"
+ "f = 0XABE12h;\n"
+ "g = 0XFA03min;\n"
+ "h = 0X12B4Ds;\n"
+ "i = 20.13E-1ms;\n"
+ "j = 20.13E-1us;\n"
+ "k = 20.13E-1ns;\n"
+ "l = 20.13E-1y;\n"
+ "m = 20.13E-1d;\n"
+ "n = 20.13E-1d;\n"
+ "o = 1d;\n"
+ "p = 102_ffl_lzlz;\n"
+ "q = 10.2_l;\n"
+ "r = 0XABDE.0'1P-23_f;\n"
+ "s = 102_foo_bar;\n"
+ "t = 123.456_felfz_ballpen;\n"
+ "u = 0XBEAD1_spacebar;\n"};
----------------
owenca wrote:
```suggestion
"u = 0XBEAD1_spacebar;"};
```
and elsewhere. Except in rare cases, we don't end test cases with a newline.
https://github.com/llvm/llvm-project/pull/151590
More information about the cfe-commits
mailing list