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 106 TEST_F(IntegerLiteralSeparatorTest, UnderscoreAsSeparator) { 107 FormatStyle Style = getLLVMStyle(); 108 const StringRef Binary("B = 0B10011_11_0110_1;"); 109 const StringRef Decimal("d = 184467_440737_0_95505_92;"); 110 const StringRef Hex("H = 0XDEAD_BEEF_DE_AD_BEE_F;"); 111 112 auto TestUnderscore = [&](auto Language) { 113 Style.Language = Language; 114 115 Style.IntegerLiteralSeparator.Binary = 0; 116 verifyFormat(Binary, Style); 117 Style.IntegerLiteralSeparator.Binary = -1; 118 verifyFormat("B = 0B100111101101;", Binary, Style); 119 Style.IntegerLiteralSeparator.Binary = 4; 120 verifyFormat("B = 0B1001_1110_1101;", Binary, Style); 121 122 Style.IntegerLiteralSeparator.Decimal = 0; 123 verifyFormat(Decimal, Style); 124 Style.IntegerLiteralSeparator.Decimal = -1; 125 verifyFormat("d = 18446744073709550592;", Decimal, Style); 126 Style.IntegerLiteralSeparator.Decimal = 3; 127 verifyFormat("d = 18_446_744_073_709_550_592;", Decimal, Style); 128 129 Style.IntegerLiteralSeparator.Hex = 0; 130 verifyFormat(Hex, Style); 131 Style.IntegerLiteralSeparator.Hex = -1; 132 verifyFormat("H = 0XDEADBEEFDEADBEEF;", Hex, Style); 133 Style.IntegerLiteralSeparator.Hex = 2; 134 verifyFormat("H = 0XDE_AD_BE_EF_DE_AD_BE_EF;", Hex, Style); 135 }; 136 137 TestUnderscore(FormatStyle::LK_CSharp); 138 TestUnderscore(FormatStyle::LK_Java); 139 TestUnderscore(FormatStyle::LK_JavaScript); 140 141 verifyFormat("d = 9_007_199_254_740_995n;", Style); 142 verifyFormat("d = 9_007_199_254_740_995n;", "d = 9007199254740995n;", Style); 143 144 Style.IntegerLiteralSeparator.Binary = 8; 145 verifyFormat( 146 "b = 0b100000_00000000_00000000_00000000_00000000_00000000_00000011n;", 147 "b = 0b100000000000000000000000000000000000000000000000000011n;", Style); 148 149 verifyFormat("h = 0x20_00_00_00_00_00_03n;", Style); 150 verifyFormat("h = 0x20_00_00_00_00_00_03n;", "h = 0x20000000000003n;", Style); 151 152 verifyFormat("o = 0o400000000000000003n;", Style); 153 } 154 155 TEST_F(IntegerLiteralSeparatorTest, FixRanges) { 156 FormatStyle Style = getLLVMStyle(); 157 Style.IntegerLiteralSeparator.Decimal = 3; 158 159 const StringRef Code("i = -12'34;\n" 160 "// clang-format off\n" 161 "j = 123'4;\n" 162 "// clang-format on\n" 163 "k = +1'23'4;"); 164 const StringRef Expected("i = -1'234;\n" 165 "// clang-format off\n" 166 "j = 123'4;\n" 167 "// clang-format on\n" 168 "k = +1'234;"); 169 170 verifyFormat(Expected, Code, Style); 171 172 verifyFormat("i = -1'234;\n" 173 "// clang-format off\n" 174 "j = 123'4;\n" 175 "// clang-format on\n" 176 "k = +1'23'4;", 177 Code, Style, {tooling::Range(0, 11)}); // line 1 178 179 verifyFormat(Code, Style, {tooling::Range(32, 10)}); // line 3 180 181 verifyFormat("i = -12'34;\n" 182 "// clang-format off\n" 183 "j = 123'4;\n" 184 "// clang-format on\n" 185 "k = +1'234;", 186 Code, Style, {tooling::Range(61, 12)}); // line 5 187 188 verifyFormat(Expected, Code, Style, 189 {tooling::Range(0, 11), tooling::Range(61, 12)}); // lines 1, 5 190 } 191 192 TEST_F(IntegerLiteralSeparatorTest, FloatingPoint) { 193 FormatStyle Style = getLLVMStyle(); 194 Style.IntegerLiteralSeparator.Decimal = 3; 195 Style.IntegerLiteralSeparator.Hex = 2; 196 197 verifyFormat("d0 = .0;\n" 198 "d1 = 0.;\n" 199 "y = 7890.;\n" 200 "E = 3456E2;\n" 201 "p = 0xABCp2;", 202 Style); 203 204 Style.Language = FormatStyle::LK_JavaScript; 205 verifyFormat("y = 7890.;\n" 206 "e = 3456e2;", 207 Style); 208 209 Style.Language = FormatStyle::LK_Java; 210 verifyFormat("y = 7890.;\n" 211 "E = 3456E2;\n" 212 "P = 0xABCP2;\n" 213 "f = 1234f;\n" 214 "D = 5678D;", 215 Style); 216 217 Style.Language = FormatStyle::LK_CSharp; 218 verifyFormat("y = 7890.;\n" 219 "e = 3456e2;\n" 220 "F = 1234F;\n" 221 "d = 5678d;\n" 222 "M = 9012M", 223 Style); 224 } 225 226 } // namespace 227 } // namespace format 228 } // namespace clang 229