| 1 |
//===- unittest/Format/IntegerLiteralSeparatorTest.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-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
|
| 9 |
#include "FormatTestBase.h" |
| 10 |
|
| 11 |
#define DEBUG_TYPE "integer-literal-separator-test" |
| 12 |
|
| 13 |
namespace clang { |
| 14 |
namespace format { |
| 15 |
namespace test { |
| 16 |
namespace { |
| 17 |
|
| 18 |
class IntegerLiteralSeparatorTest : public FormatTestBase {}; |
| 19 |
|
| 20 |
TEST_F(IntegerLiteralSeparatorTest, SingleQuoteAsSeparator) { |
| 21 |
FormatStyle Style = getLLVMStyle(); |
| 22 |
EXPECT_EQ(Style.Language, FormatStyle::LK_Cpp); |
| 23 |
EXPECT_EQ(Style.IntegerLiteralSeparator.Binary, 0); |
| 24 |
EXPECT_EQ(Style.IntegerLiteralSeparator.Decimal, 0); |
| 25 |
EXPECT_EQ(Style.IntegerLiteralSeparator.Hex, 0); |
| 26 |
|
| 27 |
constexpr StringRef Binary("b = 0b10011'11'0110'1u;"); |
| 28 |
verifyFormat(Binary, Style); |
| 29 |
Style.IntegerLiteralSeparator.Binary = -1; |
| 30 |
verifyFormat("b = 0b100111101101u;", Binary, Style); |
| 31 |
Style.IntegerLiteralSeparator.Binary = 1; |
| 32 |
verifyFormat("b = 0b1'0'0'1'1'1'1'0'1'1'0'1u;", Binary, Style); |
| 33 |
Style.IntegerLiteralSeparator.Binary = 4; |
| 34 |
verifyFormat("b = 0b1001'1110'1101u;", Binary, Style); |
| 35 |
|
| 36 |
constexpr StringRef Decimal("d = 184467'440737'0'95505'92Ull;"); |
| 37 |
verifyFormat(Decimal, Style); |
| 38 |
Style.IntegerLiteralSeparator.Decimal = -1; |
| 39 |
verifyFormat("d = 18446744073709550592Ull;", Decimal, Style); |
| 40 |
Style.IntegerLiteralSeparator.Decimal = 3; |
| 41 |
verifyFormat("d = 18'446'744'073'709'550'592Ull;", Decimal, Style); |
| 42 |
|
| 43 |
constexpr StringRef Hex("h = 0xDEAD'BEEF'DE'AD'BEE'Fuz;"); |
| 44 |
verifyFormat(Hex, Style); |
| 45 |
Style.IntegerLiteralSeparator.Hex = -1; |
| 46 |
verifyFormat("h = 0xDEADBEEFDEADBEEFuz;", Hex, Style); |
| 47 |
Style.IntegerLiteralSeparator.Hex = 2; |
| 48 |
verifyFormat("h = 0xDE'AD'BE'EF'DE'AD'BE'EFuz;", Hex, Style); |
| 49 |
|
| 50 |
verifyFormat("o0 = 0;\n" |
| 51 |
"o1 = 07;\n" |
| 52 |
"o5 = 012345;", |
| 53 |
Style); |
| 54 |
|
| 55 |
verifyFormat("bi = 0b1'0000i;\n" |
| 56 |
"dif = 1'234if;\n" |
| 57 |
"hil = 0xA'BCil;", |
| 58 |
"bi = 0b10000i;\n" |
| 59 |
"dif = 1234if;\n" |
| 60 |
"hil = 0xABCil;", |
| 61 |
Style); |
| 62 |
|
| 63 |
verifyFormat("bd = 0b1'0000d;\n" |
| 64 |
"dh = 1'234h;\n" |
| 65 |
"dmin = 1'234min;\n" |
| 66 |
"dns = 1'234ns;\n" |
| 67 |
"ds = 1'234s;\n" |
| 68 |
"dus = 1'234us;\n" |
| 69 |
"hy = 0xA'BCy;", |
| 70 |
"bd = 0b10000d;\n" |
| 71 |
"dh = 1234h;\n" |
| 72 |
"dmin = 1234min;\n" |
| 73 |
"dns = 1234ns;\n" |
| 74 |
"ds = 1234s;\n" |
| 75 |
"dus = 1234us;\n" |
| 76 |
"hy = 0xABCy;", |
| 77 |
Style); |
| 78 |
|
| 79 |
verifyFormat("hd = 0xAB'Cd;", "hd = 0xABCd;", Style); |
| 80 |
|
| 81 |
verifyFormat("d = 5'678_km;\n" |
| 82 |
"h = 0xD'EF_u16;", |
| 83 |
"d = 5678_km;\n" |
| 84 |
"h = 0xDEF_u16;", |
| 85 |
Style); |
| 86 |
|
| 87 |
Style.Standard = FormatStyle::LS_Cpp11; |
| 88 |
verifyFormat("ld = 1234L;", Style); |
| 89 |
} |
| 90 |
|
| 91 |
TEST_F(IntegerLiteralSeparatorTest, UnderscoreAsSeparator) { |
| 92 |
FormatStyle Style = getLLVMStyle(); |
| 93 |
constexpr StringRef Binary("B = 0B10011_11_0110_1;"); |
| 94 |
constexpr StringRef Decimal("d = 184467_440737_0_95505_92;"); |
| 95 |
constexpr StringRef Hex("H = 0XDEAD_BEEF_DE_AD_BEE_F;"); |
| 96 |
|
| 97 |
auto TestUnderscore = [&](auto Language) { |
| 98 |
Style.Language = Language; |
| 99 |
|
| 100 |
Style.IntegerLiteralSeparator.Binary = 0; |
| 101 |
verifyFormat(Binary, Style); |
| 102 |
Style.IntegerLiteralSeparator.Binary = -1; |
| 103 |
verifyFormat("B = 0B100111101101;", Binary, Style); |
| 104 |
Style.IntegerLiteralSeparator.Binary = 4; |
| 105 |
verifyFormat("B = 0B1001_1110_1101;", Binary, Style); |
| 106 |
|
| 107 |
Style.IntegerLiteralSeparator.Decimal = 0; |
| 108 |
verifyFormat(Decimal, Style); |
| 109 |
Style.IntegerLiteralSeparator.Decimal = -1; |
| 110 |
verifyFormat("d = 18446744073709550592;", Decimal, Style); |
| 111 |
Style.IntegerLiteralSeparator.Decimal = 3; |
| 112 |
verifyFormat("d = 18_446_744_073_709_550_592;", Decimal, Style); |
| 113 |
|
| 114 |
Style.IntegerLiteralSeparator.Hex = 0; |
| 115 |
verifyFormat(Hex, Style); |
| 116 |
Style.IntegerLiteralSeparator.Hex = -1; |
| 117 |
verifyFormat("H = 0XDEADBEEFDEADBEEF;", Hex, Style); |
| 118 |
Style.IntegerLiteralSeparator.Hex = 2; |
| 119 |
verifyFormat("H = 0XDE_AD_BE_EF_DE_AD_BE_EF;", Hex, Style); |
| 120 |
}; |
| 121 |
|
| 122 |
TestUnderscore(FormatStyle::LK_CSharp); |
| 123 |
TestUnderscore(FormatStyle::LK_Java); |
| 124 |
TestUnderscore(FormatStyle::LK_JavaScript); |
| 125 |
|
| 126 |
verifyFormat("d = 9_007_199_254_740_995n;", Style); |
| 127 |
verifyFormat("d = 9_007_199_254_740_995n;", "d = 9007199254740995n;", Style); |
| 128 |
|
| 129 |
Style.IntegerLiteralSeparator.Binary = 8; |
| 130 |
verifyFormat( |
| 131 |
"b = 0b100000_00000000_00000000_00000000_00000000_00000000_00000011n;", |
| 132 |
"b = 0b100000000000000000000000000000000000000000000000000011n;", Style); |
| 133 |
|
| 134 |
verifyFormat("h = 0x20_00_00_00_00_00_03n;", Style); |
| 135 |
verifyFormat("h = 0x20_00_00_00_00_00_03n;", "h = 0x20000000000003n;", Style); |
| 136 |
|
| 137 |
verifyFormat("o = 0o400000000000000003n;", Style); |
| 138 |
} |
| 139 |
|
| 140 |
TEST_F(IntegerLiteralSeparatorTest, MinDigitsInsert) { |
| 141 |
FormatStyle Style = getLLVMStyle(); |
| 142 |
Style.IntegerLiteralSeparator.Binary = 3; |
| 143 |
Style.IntegerLiteralSeparator.Decimal = 3; |
| 144 |
Style.IntegerLiteralSeparator.Hex = 2; |
| 145 |
|
| 146 |
Style.IntegerLiteralSeparator.BinaryMinDigitsInsert = 7; |
| 147 |
verifyFormat("b1 = 0b101101;\n" |
| 148 |
"b2 = 0b1'101'101;", |
| 149 |
"b1 = 0b101'101;\n" |
| 150 |
"b2 = 0b1101101;", |
| 151 |
Style); |
| 152 |
|
| 153 |
Style.IntegerLiteralSeparator.DecimalMinDigitsInsert = 5; |
| 154 |
verifyFormat("d1 = 2023;\n" |
| 155 |
"d2 = 10'000;", |
| 156 |
"d1 = 2'023;\n" |
| 157 |
"d2 = 100'00;", |
| 158 |
Style); |
| 159 |
|
| 160 |
Style.IntegerLiteralSeparator.DecimalMinDigitsInsert = 3; |
| 161 |
verifyFormat("d1 = 123;\n" |
| 162 |
"d2 = 1'234;", |
| 163 |
"d1 = 12'3;\n" |
| 164 |
"d2 = 12'34;", |
| 165 |
Style); |
| 166 |
|
| 167 |
Style.IntegerLiteralSeparator.HexMinDigitsInsert = 6; |
| 168 |
verifyFormat("h1 = 0xABCDE;\n" |
| 169 |
"h2 = 0xAB'CD'EF;", |
| 170 |
"h1 = 0xA'BC'DE;\n" |
| 171 |
"h2 = 0xABC'DEF;", |
| 172 |
Style); |
| 173 |
} |
| 174 |
|
| 175 |
TEST_F(IntegerLiteralSeparatorTest, FixRanges) { |
| 176 |
FormatStyle Style = getLLVMStyle(); |
| 177 |
Style.IntegerLiteralSeparator.Decimal = 3; |
| 178 |
|
| 179 |
constexpr StringRef Code("i = -12'34;\n" |
| 180 |
"// clang-format off\n" |
| 181 |
"j = 123'4;\n" |
| 182 |
"// clang-format on\n" |
| 183 |
"k = +1'23'4;"); |
| 184 |
constexpr StringRef Expected("i = -1'234;\n" |
| 185 |
"// clang-format off\n" |
| 186 |
"j = 123'4;\n" |
| 187 |
"// clang-format on\n" |
| 188 |
"k = +1'234;"); |
| 189 |
|
| 190 |
verifyFormat(Expected, Code, Style); |
| 191 |
|
| 192 |
verifyFormat("i = -1'234;\n" |
| 193 |
"// clang-format off\n" |
| 194 |
"j = 123'4;\n" |
| 195 |
"// clang-format on\n" |
| 196 |
"k = +1'23'4;", |
| 197 |
Code, Style, {tooling::Range(0, 11)}); // line 1 |
| 198 |
|
| 199 |
verifyFormat(Code, Code, Style, {tooling::Range(32, 10)}); // line 3 |
| 200 |
|
| 201 |
verifyFormat("i = -12'34;\n" |
| 202 |
"// clang-format off\n" |
| 203 |
"j = 123'4;\n" |
| 204 |
"// clang-format on\n" |
| 205 |
"k = +1'234;", |
| 206 |
Code, Style, {tooling::Range(61, 12)}); // line 5 |
| 207 |
|
| 208 |
verifyFormat(Expected, Code, Style, |
| 209 |
{tooling::Range(0, 11), tooling::Range(61, 12)}); // lines 1, 5 |
| 210 |
} |
| 211 |
|
| 212 |
TEST_F(IntegerLiteralSeparatorTest, FloatingPoint) { |
| 213 |
FormatStyle Style = getLLVMStyle(); |
| 214 |
Style.IntegerLiteralSeparator.Decimal = 3; |
| 215 |
Style.IntegerLiteralSeparator.Hex = 2; |
| 216 |
|
| 217 |
verifyFormat("d0 = .0;\n" |
| 218 |
"d1 = 0.;\n" |
| 219 |
"y = 7890.;\n" |
| 220 |
"E = 3456E2;\n" |
| 221 |
"p = 0xABCp2;", |
| 222 |
Style); |
| 223 |
|
| 224 |
Style.Language = FormatStyle::LK_JavaScript; |
| 225 |
verifyFormat("y = 7890.;\n" |
| 226 |
"e = 3456e2;", |
| 227 |
Style); |
| 228 |
|
| 229 |
Style.Language = FormatStyle::LK_Java; |
| 230 |
verifyFormat("y = 7890.;\n" |
| 231 |
"E = 3456E2;\n" |
| 232 |
"P = 0xABCP2;\n" |
| 233 |
"f = 1234f;\n" |
| 234 |
"D = 5678D;", |
| 235 |
Style); |
| 236 |
|
| 237 |
Style.Language = FormatStyle::LK_CSharp; |
| 238 |
verifyFormat("y = 7890.;\n" |
| 239 |
"e = 3456e2;\n" |
| 240 |
"F = 1234F;\n" |
| 241 |
"d = 5678d;\n" |
| 242 |
"M = 9012M", |
| 243 |
Style); |
| 244 |
} |
| 245 |
|
| 246 |
TEST_F(IntegerLiteralSeparatorTest, MaxDigitsRemove) { |
| 247 |
auto Style = getLLVMStyle(); |
| 248 |
Style.IntegerLiteralSeparator.Decimal = 3; |
| 249 |
Style.IntegerLiteralSeparator.DecimalMaxDigitsRemove = 4; |
| 250 |
Style.IntegerLiteralSeparator.DecimalMinDigitsInsert = 7; |
| 251 |
|
| 252 |
verifyFormat("d1 = 123456;\n" |
| 253 |
"d2 = 1234'56;", |
| 254 |
Style); |
| 255 |
|
| 256 |
verifyFormat("d0 = 2023;\n" |
| 257 |
"d3 = 5'000'000;", |
| 258 |
"d0 = 20'2'3;\n" |
| 259 |
"d3 = 5000000;", |
| 260 |
Style); |
| 261 |
} |
| 262 |
|
| 263 |
} // namespace |
| 264 |
} // namespace test |
| 265 |
} // namespace format |
| 266 |
} // namespace clang |