| 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 "clang/Format/Format.h" |
| 10 |
|
| 11 |
#include "../Tooling/ReplacementTest.h" |
| 12 |
#include "FormatTestUtils.h" |
| 13 |
|
| 14 |
#define DEBUG_TYPE "integer-literal-separator-test" |
| 15 |
|
| 16 |
namespace clang { |
| 17 |
namespace format { |
| 18 |
namespace { |
| 19 |
|
| 20 |
// TODO: |
| 21 |
// Refactor the class declaration, which is copied from BracesInserterTest.cpp. |
| 22 |
class IntegerLiteralSeparatorTest : public ::testing::Test { |
| 23 |
protected: |
| 24 |
std::string format(llvm::StringRef Code, const FormatStyle &Style, |
| 25 |
const std::vector<tooling::Range> &Ranges) { |
| 26 |
LLVM_DEBUG(llvm::errs() << "---\n"); |
| 27 |
LLVM_DEBUG(llvm::errs() << Code << "\n\n"); |
| 28 |
auto NonEmptyRanges = Ranges; |
| 29 |
if (Ranges.empty()) |
| 30 |
NonEmptyRanges = {1, tooling::Range(0, Code.size())}; |
| 31 |
FormattingAttemptStatus Status; |
| 32 |
tooling::Replacements Replaces = |
| 33 |
reformat(Style, Code, NonEmptyRanges, "<stdin>", &Status); |
| 34 |
EXPECT_EQ(true, Status.FormatComplete) << Code << "\n\n"; |
| 35 |
ReplacementCount = Replaces.size(); |
| 36 |
auto Result = applyAllReplacements(Code, Replaces); |
| 37 |
EXPECT_TRUE(static_cast<bool>(Result)); |
| 38 |
LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n"); |
| 39 |
return *Result; |
| 40 |
} |
| 41 |
|
| 42 |
void _verifyFormat(const char *File, int Line, llvm::StringRef Expected, |
| 43 |
llvm::StringRef Code, |
| 44 |
const FormatStyle &Style = getLLVMStyle(), |
| 45 |
const std::vector<tooling::Range> &Ranges = {}) { |
| 46 |
testing::ScopedTrace t(File, Line, ::testing::Message() << Code.str()); |
| 47 |
EXPECT_EQ(Expected.str(), format(Expected, Style, Ranges)) |
| 48 |
<< "Expected code is not stable"; |
| 49 |
EXPECT_EQ(Expected.str(), format(Code, Style, Ranges)); |
| 50 |
if (Style.Language == FormatStyle::LK_Cpp && Ranges.empty()) { |
| 51 |
// Objective-C++ is a superset of C++, so everything checked for C++ |
| 52 |
// needs to be checked for Objective-C++ as well. |
| 53 |
FormatStyle ObjCStyle = Style; |
| 54 |
ObjCStyle.Language = FormatStyle::LK_ObjC; |
| 55 |
EXPECT_EQ(Expected.str(), format(test::messUp(Code), ObjCStyle, Ranges)); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
void _verifyFormat(const char *File, int Line, llvm::StringRef Code, |
| 60 |
const FormatStyle &Style = getLLVMStyle(), |
| 61 |
const std::vector<tooling::Range> &Ranges = {}) { |
| 62 |
_verifyFormat(File, Line, Code, Code, Style, Ranges); |
| 63 |
} |
| 64 |
|
| 65 |
int ReplacementCount; |
| 66 |
}; |
| 67 |
|
| 68 |
#define verifyFormat(...) _verifyFormat(__FILE__, __LINE__, __VA_ARGS__) |
| 69 |
|
| 70 |
TEST_F(IntegerLiteralSeparatorTest, SingleQuoteAsSeparator) { |
| 71 |
FormatStyle Style = getLLVMStyle(); |
| 72 |
EXPECT_EQ(Style.Language, FormatStyle::LK_Cpp); |
| 73 |
EXPECT_EQ(Style.IntegerLiteralSeparator.Binary, 0); |
| 74 |
EXPECT_EQ(Style.IntegerLiteralSeparator.Decimal, 0); |
| 75 |
EXPECT_EQ(Style.IntegerLiteralSeparator.Hex, 0); |
| 76 |
|
| 77 |
const StringRef Binary("b = 0b10011'11'0110'1u;"); |
| 78 |
verifyFormat(Binary, Style); |
| 79 |
Style.IntegerLiteralSeparator.Binary = -1; |
| 80 |
verifyFormat("b = 0b100111101101u;", Binary, Style); |
| 81 |
Style.IntegerLiteralSeparator.Binary = 1; |
| 82 |
verifyFormat("b = 0b1'0'0'1'1'1'1'0'1'1'0'1u;", Binary, Style); |
| 83 |
Style.IntegerLiteralSeparator.Binary = 4; |
| 84 |
verifyFormat("b = 0b1001'1110'1101u;", Binary, Style); |
| 85 |
|
| 86 |
const StringRef Decimal("d = 184467'440737'0'95505'92Ull;"); |
| 87 |
verifyFormat(Decimal, Style); |
| 88 |
Style.IntegerLiteralSeparator.Decimal = -1; |
| 89 |
verifyFormat("d = 18446744073709550592Ull;", Decimal, Style); |
| 90 |
Style.IntegerLiteralSeparator.Decimal = 3; |
| 91 |
verifyFormat("d = 18'446'744'073'709'550'592Ull;", Decimal, Style); |
| 92 |
|
| 93 |
const StringRef Hex("h = 0xDEAD'BEEF'DE'AD'BEE'Fuz;"); |
| 94 |
verifyFormat(Hex, Style); |
| 95 |
Style.IntegerLiteralSeparator.Hex = -1; |
| 96 |
verifyFormat("h = 0xDEADBEEFDEADBEEFuz;", Hex, Style); |
| 97 |
Style.IntegerLiteralSeparator.Hex = 2; |
| 98 |
verifyFormat("h = 0xDE'AD'BE'EF'DE'AD'BE'EFuz;", Hex, Style); |
| 99 |
|
| 100 |
verifyFormat("o0 = 0;\n" |
| 101 |
"o1 = 07;\n" |
| 102 |
"o5 = 012345;", |
| 103 |
Style); |
| 104 |
|
| 105 |
verifyFormat("bi = 0b1'0000i;\n" |
| 106 |
"dif = 1'234if;\n" |
| 107 |
"hil = 0xA'BCil;", |
| 108 |
"bi = 0b10000i;\n" |
| 109 |
"dif = 1234if;\n" |
| 110 |
"hil = 0xABCil;", |
| 111 |
Style); |
| 112 |
|
| 113 |
verifyFormat("bd = 0b1'0000d;\n" |
| 114 |
"dh = 1'234h;\n" |
| 115 |
"dmin = 1'234min;\n" |
| 116 |
"dns = 1'234ns;\n" |
| 117 |
"ds = 1'234s;\n" |
| 118 |
"dus = 1'234us;\n" |
| 119 |
"hy = 0xA'BCy;", |
| 120 |
"bd = 0b10000d;\n" |
| 121 |
"dh = 1234h;\n" |
| 122 |
"dmin = 1234min;\n" |
| 123 |
"dns = 1234ns;\n" |
| 124 |
"ds = 1234s;\n" |
| 125 |
"dus = 1234us;\n" |
| 126 |
"hy = 0xABCy;", |
| 127 |
Style); |
| 128 |
|
| 129 |
verifyFormat("hd = 0xAB'Cd;", "hd = 0xABCd;", Style); |
| 130 |
|
| 131 |
verifyFormat("d = 5'678_km;\n" |
| 132 |
"h = 0xD'EF_u16;", |
| 133 |
"d = 5678_km;\n" |
| 134 |
"h = 0xDEF_u16;", |
| 135 |
Style); |
| 136 |
} |
| 137 |
|
| 138 |
TEST_F(IntegerLiteralSeparatorTest, UnderscoreAsSeparator) { |
| 139 |
FormatStyle Style = getLLVMStyle(); |
| 140 |
const StringRef Binary("B = 0B10011_11_0110_1;"); |
| 141 |
const StringRef Decimal("d = 184467_440737_0_95505_92;"); |
| 142 |
const StringRef Hex("H = 0XDEAD_BEEF_DE_AD_BEE_F;"); |
| 143 |
|
| 144 |
auto TestUnderscore = [&](auto Language) { |
| 145 |
Style.Language = Language; |
| 146 |
|
| 147 |
Style.IntegerLiteralSeparator.Binary = 0; |
| 148 |
verifyFormat(Binary, Style); |
| 149 |
Style.IntegerLiteralSeparator.Binary = -1; |
| 150 |
verifyFormat("B = 0B100111101101;", Binary, Style); |
| 151 |
Style.IntegerLiteralSeparator.Binary = 4; |
| 152 |
verifyFormat("B = 0B1001_1110_1101;", Binary, Style); |
| 153 |
|
| 154 |
Style.IntegerLiteralSeparator.Decimal = 0; |
| 155 |
verifyFormat(Decimal, Style); |
| 156 |
Style.IntegerLiteralSeparator.Decimal = -1; |
| 157 |
verifyFormat("d = 18446744073709550592;", Decimal, Style); |
| 158 |
Style.IntegerLiteralSeparator.Decimal = 3; |
| 159 |
verifyFormat("d = 18_446_744_073_709_550_592;", Decimal, Style); |
| 160 |
|
| 161 |
Style.IntegerLiteralSeparator.Hex = 0; |
| 162 |
verifyFormat(Hex, Style); |
| 163 |
Style.IntegerLiteralSeparator.Hex = -1; |
| 164 |
verifyFormat("H = 0XDEADBEEFDEADBEEF;", Hex, Style); |
| 165 |
Style.IntegerLiteralSeparator.Hex = 2; |
| 166 |
verifyFormat("H = 0XDE_AD_BE_EF_DE_AD_BE_EF;", Hex, Style); |
| 167 |
}; |
| 168 |
|
| 169 |
TestUnderscore(FormatStyle::LK_CSharp); |
| 170 |
TestUnderscore(FormatStyle::LK_Java); |
| 171 |
TestUnderscore(FormatStyle::LK_JavaScript); |
| 172 |
|
| 173 |
verifyFormat("d = 9_007_199_254_740_995n;", Style); |
| 174 |
verifyFormat("d = 9_007_199_254_740_995n;", "d = 9007199254740995n;", Style); |
| 175 |
|
| 176 |
Style.IntegerLiteralSeparator.Binary = 8; |
| 177 |
verifyFormat( |
| 178 |
"b = 0b100000_00000000_00000000_00000000_00000000_00000000_00000011n;", |
| 179 |
"b = 0b100000000000000000000000000000000000000000000000000011n;", Style); |
| 180 |
|
| 181 |
verifyFormat("h = 0x20_00_00_00_00_00_03n;", Style); |
| 182 |
verifyFormat("h = 0x20_00_00_00_00_00_03n;", "h = 0x20000000000003n;", Style); |
| 183 |
|
| 184 |
verifyFormat("o = 0o400000000000000003n;", Style); |
| 185 |
} |
| 186 |
|
| 187 |
TEST_F(IntegerLiteralSeparatorTest, MinDigits) { |
| 188 |
FormatStyle Style = getLLVMStyle(); |
| 189 |
Style.IntegerLiteralSeparator.Binary = 3; |
| 190 |
Style.IntegerLiteralSeparator.Decimal = 3; |
| 191 |
Style.IntegerLiteralSeparator.Hex = 2; |
| 192 |
|
| 193 |
Style.IntegerLiteralSeparator.BinaryMinDigits = 7; |
| 194 |
verifyFormat("b1 = 0b101101;\n" |
| 195 |
"b2 = 0b1'101'101;", |
| 196 |
"b1 = 0b101'101;\n" |
| 197 |
"b2 = 0b1101101;", |
| 198 |
Style); |
| 199 |
|
| 200 |
Style.IntegerLiteralSeparator.DecimalMinDigits = 5; |
| 201 |
verifyFormat("d1 = 2023;\n" |
| 202 |
"d2 = 10'000;", |
| 203 |
"d1 = 2'023;\n" |
| 204 |
"d2 = 100'00;", |
| 205 |
Style); |
| 206 |
|
| 207 |
Style.IntegerLiteralSeparator.DecimalMinDigits = 3; |
| 208 |
verifyFormat("d1 = 123;\n" |
| 209 |
"d2 = 1'234;", |
| 210 |
"d1 = 12'3;\n" |
| 211 |
"d2 = 12'34;", |
| 212 |
Style); |
| 213 |
|
| 214 |
Style.IntegerLiteralSeparator.HexMinDigits = 6; |
| 215 |
verifyFormat("h1 = 0xABCDE;\n" |
| 216 |
"h2 = 0xAB'CD'EF;", |
| 217 |
"h1 = 0xA'BC'DE;\n" |
| 218 |
"h2 = 0xABC'DEF;", |
| 219 |
Style); |
| 220 |
} |
| 221 |
|
| 222 |
TEST_F(IntegerLiteralSeparatorTest, FixRanges) { |
| 223 |
FormatStyle Style = getLLVMStyle(); |
| 224 |
Style.IntegerLiteralSeparator.Decimal = 3; |
| 225 |
|
| 226 |
const StringRef Code("i = -12'34;\n" |
| 227 |
"// clang-format off\n" |
| 228 |
"j = 123'4;\n" |
| 229 |
"// clang-format on\n" |
| 230 |
"k = +1'23'4;"); |
| 231 |
const StringRef Expected("i = -1'234;\n" |
| 232 |
"// clang-format off\n" |
| 233 |
"j = 123'4;\n" |
| 234 |
"// clang-format on\n" |
| 235 |
"k = +1'234;"); |
| 236 |
|
| 237 |
verifyFormat(Expected, Code, Style); |
| 238 |
|
| 239 |
verifyFormat("i = -1'234;\n" |
| 240 |
"// clang-format off\n" |
| 241 |
"j = 123'4;\n" |
| 242 |
"// clang-format on\n" |
| 243 |
"k = +1'23'4;", |
| 244 |
Code, Style, {tooling::Range(0, 11)}); // line 1 |
| 245 |
|
| 246 |
verifyFormat(Code, Style, {tooling::Range(32, 10)}); // line 3 |
| 247 |
|
| 248 |
verifyFormat("i = -12'34;\n" |
| 249 |
"// clang-format off\n" |
| 250 |
"j = 123'4;\n" |
| 251 |
"// clang-format on\n" |
| 252 |
"k = +1'234;", |
| 253 |
Code, Style, {tooling::Range(61, 12)}); // line 5 |
| 254 |
|
| 255 |
verifyFormat(Expected, Code, Style, |
| 256 |
{tooling::Range(0, 11), tooling::Range(61, 12)}); // lines 1, 5 |
| 257 |
} |
| 258 |
|
| 259 |
TEST_F(IntegerLiteralSeparatorTest, FloatingPoint) { |
| 260 |
FormatStyle Style = getLLVMStyle(); |
| 261 |
Style.IntegerLiteralSeparator.Decimal = 3; |
| 262 |
Style.IntegerLiteralSeparator.Hex = 2; |
| 263 |
|
| 264 |
verifyFormat("d0 = .0;\n" |
| 265 |
"d1 = 0.;\n" |
| 266 |
"y = 7890.;\n" |
| 267 |
"E = 3456E2;\n" |
| 268 |
"p = 0xABCp2;", |
| 269 |
Style); |
| 270 |
|
| 271 |
Style.Language = FormatStyle::LK_JavaScript; |
| 272 |
verifyFormat("y = 7890.;\n" |
| 273 |
"e = 3456e2;", |
| 274 |
Style); |
| 275 |
|
| 276 |
Style.Language = FormatStyle::LK_Java; |
| 277 |
verifyFormat("y = 7890.;\n" |
| 278 |
"E = 3456E2;\n" |
| 279 |
"P = 0xABCP2;\n" |
| 280 |
"f = 1234f;\n" |
| 281 |
"D = 5678D;", |
| 282 |
Style); |
| 283 |
|
| 284 |
Style.Language = FormatStyle::LK_CSharp; |
| 285 |
verifyFormat("y = 7890.;\n" |
| 286 |
"e = 3456e2;\n" |
| 287 |
"F = 1234F;\n" |
| 288 |
"d = 5678d;\n" |
| 289 |
"M = 9012M", |
| 290 |
Style); |
| 291 |
} |
| 292 |
|
| 293 |
} // namespace |
| 294 |
} // namespace format |
| 295 |
} // namespace clang |