347 lines · cpp
1//===- unittest/Format/NumericLiteralCaseTest.cpp -------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "FormatTestBase.h"10 11#define DEBUG_TYPE "numeric-literal-case-test"12 13namespace clang {14namespace format {15namespace test {16namespace {17 18class NumericLiteralCaseTest : public FormatTestBase {};19 20TEST_F(NumericLiteralCaseTest, Prefix) {21 constexpr StringRef Bin0("b = 0b0'10'010uL;");22 constexpr StringRef Bin1("b = 0B010'010Ul;");23 constexpr StringRef Hex0("b = 0xdead'BEEFuL;");24 constexpr StringRef Hex1("b = 0Xdead'BEEFUl;");25 verifyFormat(Bin0);26 verifyFormat(Bin1);27 verifyFormat(Hex0);28 verifyFormat(Hex1);29 30 auto Style = getLLVMStyle();31 EXPECT_EQ(Style.NumericLiteralCase.Prefix, FormatStyle::NLCS_Leave);32 EXPECT_EQ(Style.NumericLiteralCase.HexDigit, FormatStyle::NLCS_Leave);33 EXPECT_EQ(Style.NumericLiteralCase.ExponentLetter, FormatStyle::NLCS_Leave);34 EXPECT_EQ(Style.NumericLiteralCase.Suffix, FormatStyle::NLCS_Leave);35 36 Style.NumericLiteralCase.Prefix = FormatStyle::NLCS_Upper;37 verifyFormat("b = 0B0'10'010uL;", Bin0, Style);38 verifyFormat(Bin1, Style);39 verifyFormat("b = 0Xdead'BEEFuL;", Hex0, Style);40 verifyFormat(Hex1, Style);41 verifyFormat("i = 0XaBcD.a0Ebp123F;", Style);42 verifyFormat("j = 0XaBcD.a0EbP123f;", Style);43 44 Style.NumericLiteralCase.Prefix = FormatStyle::NLCS_Lower;45 verifyFormat(Bin0, Style);46 verifyFormat("b = 0b010'010Ul;", Bin1, Style);47 verifyFormat(Hex0, Style);48 verifyFormat("b = 0xdead'BEEFUl;", Hex1, Style);49}50 51TEST_F(NumericLiteralCaseTest, HexDigit) {52 constexpr StringRef A("a = 0xaBc0'123fuL;");53 constexpr StringRef B("b = 0XaBc0'123FUl;");54 constexpr StringRef C("c = 0xa'Bc.0p12'3f32;");55 constexpr StringRef D("d = 0xa'Bc.0P12'3F128;");56 constexpr StringRef E("e = 0b0011'00Ull;");57 constexpr StringRef F("f = 0B0100'000zu;");58 constexpr StringRef G("g = 0.123e-19f;");59 constexpr StringRef H("h = 0.12'3E-19F16;");60 constexpr StringRef I("i = 0x.0000aBcp12'3F128;");61 constexpr StringRef J("j = 0xaa1'fP12'3F128;");62 constexpr StringRef K("k = 0x0;");63 constexpr StringRef L("l = 0xA;");64 verifyFormat(A);65 verifyFormat(B);66 verifyFormat(C);67 verifyFormat(D);68 verifyFormat(E);69 verifyFormat(F);70 verifyFormat(G);71 verifyFormat(H);72 verifyFormat(I);73 verifyFormat(J);74 verifyFormat(K);75 verifyFormat(L);76 77 auto Style = getLLVMStyle();78 Style.NumericLiteralCase.HexDigit = FormatStyle::NLCS_Upper;79 verifyFormat("a = 0xABC0'123FuL;", A, Style);80 verifyFormat("b = 0XABC0'123FUl;", B, Style);81 verifyFormat("c = 0xA'BC.0p12'3f32;", C, Style);82 verifyFormat("d = 0xA'BC.0P12'3F128;", D, Style);83 verifyFormat(E, Style);84 verifyFormat(F, Style);85 verifyFormat(G, Style);86 verifyFormat(H, Style);87 verifyFormat("i = 0x.0000ABCp12'3F128;", I, Style);88 verifyFormat("j = 0xAA1'FP12'3F128;", J, Style);89 verifyFormat(K, Style);90 verifyFormat(L, Style);91 92 Style.NumericLiteralCase.HexDigit = FormatStyle::NLCS_Lower;93 verifyFormat("a = 0xabc0'123fuL;", A, Style);94 verifyFormat("b = 0Xabc0'123fUl;", B, Style);95 verifyFormat("c = 0xa'bc.0p12'3f32;", C, Style);96 verifyFormat("d = 0xa'bc.0P12'3F128;", D, Style);97 verifyFormat(E, Style);98 verifyFormat(F, Style);99 verifyFormat(G, Style);100 verifyFormat(H, Style);101 verifyFormat("i = 0x.0000abcp12'3F128;", I, Style);102 verifyFormat("j = 0xaa1'fP12'3F128;", J, Style);103 verifyFormat(K, Style);104 verifyFormat("l = 0xa;", Style);105}106 107TEST_F(NumericLiteralCaseTest, ExponentLetter) {108 constexpr StringRef A("a = .0'01e-19f;");109 constexpr StringRef B("b = .00'1E2F;");110 constexpr StringRef C("c = 10'2.e99;");111 constexpr StringRef D("d = 123.456E-1;");112 constexpr StringRef E("e = 0x12abEe3.456p-10'0;");113 constexpr StringRef F("f = 0x.deEfP23;");114 constexpr StringRef G("g = 0xe0E1.p-1;");115 verifyFormat(A);116 verifyFormat(B);117 verifyFormat(C);118 verifyFormat(D);119 verifyFormat(E);120 verifyFormat(F);121 verifyFormat(G);122 123 auto Style = getLLVMStyle();124 Style.NumericLiteralCase.ExponentLetter = FormatStyle::NLCS_Lower;125 verifyFormat(A, Style);126 verifyFormat("b = .00'1e2F;", B, Style);127 verifyFormat(C, Style);128 verifyFormat("d = 123.456e-1;", D, Style);129 verifyFormat(E, Style);130 verifyFormat("f = 0x.deEfp23;", F, Style);131 verifyFormat(G, Style);132 133 Style.NumericLiteralCase.ExponentLetter = FormatStyle::NLCS_Upper;134 verifyFormat("a = .0'01E-19f;", A, Style);135 verifyFormat(B, Style);136 verifyFormat("c = 10'2.E99;", C, Style);137 verifyFormat(D, Style);138 verifyFormat("e = 0x12abEe3.456P-10'0;", E, Style);139 verifyFormat(F, Style);140 verifyFormat("g = 0xe0E1.P-1;", G, Style);141}142 143TEST_F(NumericLiteralCaseTest, IntegerSuffix) {144 constexpr StringRef A("a = 102u;");145 constexpr StringRef B("b = 0177U;");146 constexpr StringRef C("c = 0b101'111llU;");147 constexpr StringRef D("d = 0xdead'BeefuZ;");148 constexpr StringRef E("e = 3lU;");149 constexpr StringRef F("f = 1zu;");150 constexpr StringRef G("g = 0uLL;");151 constexpr StringRef H("h = 10'233'213'0101uLL;");152 verifyFormat(A);153 verifyFormat(B);154 verifyFormat(C);155 verifyFormat(D);156 verifyFormat(E);157 verifyFormat(F);158 verifyFormat(G);159 verifyFormat(H);160 161 auto Style = getLLVMStyle();162 Style.NumericLiteralCase.Suffix = FormatStyle::NLCS_Lower;163 verifyFormat(A, Style);164 verifyFormat("b = 0177u;", B, Style);165 verifyFormat("c = 0b101'111llu;", C, Style);166 verifyFormat("d = 0xdead'Beefuz;", D, Style);167 verifyFormat("e = 3lu;", E, Style);168 verifyFormat(F, Style);169 verifyFormat("g = 0ull;", G, Style);170 verifyFormat("h = 10'233'213'0101ull;", H, Style);171 172 Style.NumericLiteralCase.Suffix = FormatStyle::NLCS_Upper;173 verifyFormat("a = 102U;", A, Style);174 verifyFormat(B, Style);175 verifyFormat("c = 0b101'111LLU;", C, Style);176 verifyFormat("d = 0xdead'BeefUZ;", D, Style);177 verifyFormat("e = 3LU;", E, Style);178 verifyFormat("f = 1ZU;", F, Style);179 verifyFormat("g = 0ULL;", G, Style);180 verifyFormat("h = 10'233'213'0101ULL;", H, Style);181}182 183TEST_F(NumericLiteralCaseTest, FloatingPointSuffix) {184 auto Style = getLLVMStyle();185 // Floating point literals without suffixes.186 constexpr std::array<StringRef, 6> FloatingPointStatements = {187 "a = 0.", "b = 1.0", "c = .123'45E-10",188 "d = 12'3.0e1", "e = 0Xa0eE.P10", "f = 0xeE01.aFf3p6",189 };190 191 // All legal floating-point literal suffixes defined in the C++23 standard in192 // lowercase.193 constexpr std::array<StringRef, 7> FloatingPointSuffixes = {194 "f", "l", "f16", "f32", "f64", "f128", "bf16",195 };196 197 // Test all combinations of literals with suffixes.198 for (const auto &Statement : FloatingPointStatements) {199 for (const auto &Suffix : FloatingPointSuffixes) {200 const auto LowerLine = Statement.str() + Suffix.str() + ";";201 const auto UpperLine = Statement.str() + Suffix.upper() + ";";202 203 Style.NumericLiteralCase.Suffix = FormatStyle::NLCS_Leave;204 verifyFormat(LowerLine, Style);205 verifyFormat(UpperLine, Style);206 207 Style.NumericLiteralCase.Suffix = FormatStyle::NLCS_Lower;208 verifyFormat(LowerLine, Style);209 verifyFormat(LowerLine, UpperLine, Style);210 211 Style.NumericLiteralCase.Suffix = FormatStyle::NLCS_Upper;212 verifyFormat(UpperLine, LowerLine, Style);213 verifyFormat(UpperLine, Style);214 }215 }216}217 218TEST_F(NumericLiteralCaseTest, CppStandardAndUserDefinedLiteralsAreUntouched) {219 auto Style = getLLVMStyle();220 Style.NumericLiteralCase.Prefix = FormatStyle::NLCS_Upper;221 Style.NumericLiteralCase.HexDigit = FormatStyle::NLCS_Upper;222 Style.NumericLiteralCase.ExponentLetter = FormatStyle::NLCS_Upper;223 Style.NumericLiteralCase.Suffix = FormatStyle::NLCS_Upper;224 225 // C++ user-defined suffixes begin with '_' or are reserved for the standard226 // library.227 constexpr StringRef UDLiterals("a = 12.if;\n"228 "b = -3i;\n"229 "c = 100'01il;\n"230 "d = 100'0.12il;\n"231 "e = 12h;\n"232 "f = 0XABE12h;\n"233 "g = 0XFA03min;\n"234 "h = 0X12B4Ds;\n"235 "i = 20.13E-1ms;\n"236 "j = 20.13E-1us;\n"237 "k = 20.13E-1ns;\n"238 "l = 20.13E-1y;\n"239 "m = 20.13E-1d;\n"240 "n = 20.13E-1d;\n"241 "o = 1d;\n"242 "p = 102_ffl_lzlz;\n"243 "q = 10.2_l;\n"244 "r = 0XABDE.0'1P-23_f;\n"245 "s = 102_foo_bar;\n"246 "t = 123.456_felfz_ballpen;\n"247 "u = 0XBEAD1_spacebar;");248 249 verifyFormat(UDLiterals, Style);250 Style.NumericLiteralCase.Suffix = FormatStyle::NLCS_Lower;251 verifyFormat(UDLiterals, Style);252}253 254TEST_F(NumericLiteralCaseTest, FixRanges) {255 auto Style = getLLVMStyle();256 Style.NumericLiteralCase.Prefix = FormatStyle::NLCS_Lower;257 Style.NumericLiteralCase.HexDigit = FormatStyle::NLCS_Lower;258 Style.NumericLiteralCase.ExponentLetter = FormatStyle::NLCS_Lower;259 Style.NumericLiteralCase.Suffix = FormatStyle::NLCS_Lower;260 261 constexpr StringRef CodeBlock("a = 0xFea3duLL;\n"262 "b = 0X.aEbp-12f;\n"263 "c = 0uLL;\n"264 "// clang-format off\n"265 "e = 0xBeAdu;\n"266 "// clang-format on\n"267 "g = 0xabCDu;\n"268 "h = 0b010uL;\n"269 "// clang-format off\n"270 "i = 0B1010'000Zu;\n"271 "// clang-format on\n"272 "k = 0XaBuL;");273 274 verifyFormat("a = 0xfea3dull;\n"275 "b = 0x.aebp-12f;\n"276 "c = 0ull;\n"277 "// clang-format off\n"278 "e = 0xBeAdu;\n"279 "// clang-format on\n"280 "g = 0xabcdu;\n"281 "h = 0b010ul;\n"282 "// clang-format off\n"283 "i = 0B1010'000Zu;\n"284 "// clang-format on\n"285 "k = 0xabul;",286 CodeBlock, Style);287}288 289TEST_F(NumericLiteralCaseTest, UnderScoreSeparatorLanguages) {290 auto Style = getLLVMStyle();291 292 constexpr StringRef CodeBlock("a = 0xFea_3dl;\n"293 "b = 0123_345;\n"294 "c = 0b11____00lU;\n"295 "d = 0XB_e_A_du;\n"296 "e = 123_456.333__456e-10f;\n"297 "f = .1_0E-10D;\n"298 "g = 1_0.F;\n"299 "h = 0B1_0;");300 auto TestUnderscore = [&](auto Language) {301 Style.Language = Language;302 Style.NumericLiteralCase.Prefix = FormatStyle::NLCS_Lower;303 Style.NumericLiteralCase.HexDigit = FormatStyle::NLCS_Upper;304 Style.NumericLiteralCase.ExponentLetter = FormatStyle::NLCS_Lower;305 Style.NumericLiteralCase.Suffix = FormatStyle::NLCS_Upper;306 verifyFormat("a = 0xFEA_3DL;\n"307 "b = 0123_345;\n"308 "c = 0b11____00LU;\n"309 "d = 0xB_E_A_DU;\n"310 "e = 123_456.333__456e-10F;\n"311 "f = .1_0e-10D;\n"312 "g = 1_0.F;\n"313 "h = 0b1_0;",314 CodeBlock, Style);315 316 Style.NumericLiteralCase.Prefix = FormatStyle::NLCS_Upper;317 Style.NumericLiteralCase.HexDigit = FormatStyle::NLCS_Lower;318 Style.NumericLiteralCase.ExponentLetter = FormatStyle::NLCS_Upper;319 Style.NumericLiteralCase.Suffix = FormatStyle::NLCS_Lower;320 321 verifyFormat("a = 0Xfea_3dl;\n"322 "b = 0123_345;\n"323 "c = 0B11____00lu;\n"324 "d = 0Xb_e_a_du;\n"325 "e = 123_456.333__456E-10f;\n"326 "f = .1_0E-10d;\n"327 "g = 1_0.f;\n"328 "h = 0B1_0;",329 CodeBlock, Style);330 };331 332 TestUnderscore(FormatStyle::LK_CSharp);333 TestUnderscore(FormatStyle::LK_Java);334 TestUnderscore(FormatStyle::LK_JavaScript);335 336 Style.Language = FormatStyle::LK_JavaScript;337 Style.NumericLiteralCase.Prefix = FormatStyle::NLCS_Upper;338 verifyFormat("o = 0O0_10_010;", "o = 0o0_10_010;", Style);339 Style.NumericLiteralCase.Prefix = FormatStyle::NLCS_Lower;340 verifyFormat("o = 0o0_10_010;", "o = 0O0_10_010;", Style);341}342 343} // namespace344} // namespace test345} // namespace format346} // namespace clang347