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 const 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 const 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 const 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 56 TEST_F(IntegerLiteralSeparatorTest, UnderscoreAsSeparator) { 57 FormatStyle Style = getLLVMStyle(); 58 const StringRef Binary("B = 0B10011_11_0110_1;"); 59 const StringRef Decimal("d = 184467_440737_0_95505_92;"); 60 const StringRef Hex("H = 0XDEAD_BEEF_DE_AD_BEE_F;"); 61 62 auto TestUnderscore = [&](auto Language) { 63 Style.Language = Language; 64 65 Style.IntegerLiteralSeparator.Binary = 0; 66 verifyFormat(Binary, Style); 67 Style.IntegerLiteralSeparator.Binary = -1; 68 verifyFormat("B = 0B100111101101;", Binary, Style); 69 Style.IntegerLiteralSeparator.Binary = 4; 70 verifyFormat("B = 0B1001_1110_1101;", Binary, Style); 71 72 Style.IntegerLiteralSeparator.Decimal = 0; 73 verifyFormat(Decimal, Style); 74 Style.IntegerLiteralSeparator.Decimal = -1; 75 verifyFormat("d = 18446744073709550592;", Decimal, Style); 76 Style.IntegerLiteralSeparator.Decimal = 3; 77 verifyFormat("d = 18_446_744_073_709_550_592;", Decimal, Style); 78 79 Style.IntegerLiteralSeparator.Hex = 0; 80 verifyFormat(Hex, Style); 81 Style.IntegerLiteralSeparator.Hex = -1; 82 verifyFormat("H = 0XDEADBEEFDEADBEEF;", Hex, Style); 83 Style.IntegerLiteralSeparator.Hex = 2; 84 verifyFormat("H = 0XDE_AD_BE_EF_DE_AD_BE_EF;", Hex, Style); 85 }; 86 87 TestUnderscore(FormatStyle::LK_CSharp); 88 TestUnderscore(FormatStyle::LK_Java); 89 TestUnderscore(FormatStyle::LK_JavaScript); 90 91 verifyFormat("d = 9_007_199_254_740_995n;", Style); 92 verifyFormat("d = 9_007_199_254_740_995n;", "d = 9007199254740995n;", Style); 93 94 Style.IntegerLiteralSeparator.Binary = 8; 95 verifyFormat( 96 "b = 0b100000_00000000_00000000_00000000_00000000_00000000_00000011n;", 97 "b = 0b100000000000000000000000000000000000000000000000000011n;", Style); 98 99 verifyFormat("h = 0x20_00_00_00_00_00_03n;", Style); 100 verifyFormat("h = 0x20_00_00_00_00_00_03n;", "h = 0x20000000000003n;", Style); 101 102 verifyFormat("o = 0o400000000000000003n;", Style); 103 } 104 105 TEST_F(IntegerLiteralSeparatorTest, FixRanges) { 106 FormatStyle Style = getLLVMStyle(); 107 Style.IntegerLiteralSeparator.Decimal = 3; 108 109 const StringRef Code("i = -12'34;\n" 110 "// clang-format off\n" 111 "j = 123'4;\n" 112 "// clang-format on\n" 113 "k = +1'23'4;"); 114 const StringRef Expected("i = -1'234;\n" 115 "// clang-format off\n" 116 "j = 123'4;\n" 117 "// clang-format on\n" 118 "k = +1'234;"); 119 120 verifyFormat(Expected, Code, Style); 121 122 verifyFormat("i = -1'234;\n" 123 "// clang-format off\n" 124 "j = 123'4;\n" 125 "// clang-format on\n" 126 "k = +1'23'4;", 127 Code, Style, {tooling::Range(0, 11)}); // line 1 128 129 verifyFormat(Code, Code, Style, {tooling::Range(32, 10)}); // line 3 130 131 verifyFormat("i = -12'34;\n" 132 "// clang-format off\n" 133 "j = 123'4;\n" 134 "// clang-format on\n" 135 "k = +1'234;", 136 Code, Style, {tooling::Range(61, 12)}); // line 5 137 138 verifyFormat(Expected, Code, Style, 139 {tooling::Range(0, 11), tooling::Range(61, 12)}); // lines 1, 5 140 } 141 142 TEST_F(IntegerLiteralSeparatorTest, FloatingPoint) { 143 FormatStyle Style = getLLVMStyle(); 144 Style.IntegerLiteralSeparator.Decimal = 3; 145 Style.IntegerLiteralSeparator.Hex = 2; 146 147 verifyFormat("d0 = .0;\n" 148 "d1 = 0.;\n" 149 "y = 7890.;\n" 150 "E = 3456E2;\n" 151 "p = 0xABCp2;", 152 Style); 153 154 Style.Language = FormatStyle::LK_JavaScript; 155 verifyFormat("y = 7890.;\n" 156 "e = 3456e2;", 157 Style); 158 159 Style.Language = FormatStyle::LK_Java; 160 verifyFormat("y = 7890.;\n" 161 "E = 3456E2;\n" 162 "P = 0xABCP2;\n" 163 "f = 1234f;\n" 164 "D = 5678D;", 165 Style); 166 167 Style.Language = FormatStyle::LK_CSharp; 168 verifyFormat("y = 7890.;\n" 169 "e = 3456e2;\n" 170 "F = 1234F;\n" 171 "d = 5678d;\n" 172 "M = 9012M", 173 Style); 174 } 175 176 } // namespace 177 } // namespace test 178 } // namespace format 179 } // namespace clang 180