1 //===- unittest/Format/FormatTestRawStrings.cpp - Formatting unit tests ---===// 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 #include "clang/Frontend/TextDiagnosticPrinter.h" 15 #include "llvm/Support/Debug.h" 16 #include "llvm/Support/MemoryBuffer.h" 17 #include "gtest/gtest.h" 18 19 #define DEBUG_TYPE "format-test" 20 21 using clang::tooling::ReplacementTest; 22 using clang::tooling::toReplacements; 23 24 namespace clang { 25 namespace format { 26 namespace { 27 28 class FormatTestRawStrings : public ::testing::Test { 29 protected: 30 enum StatusCheck { SC_ExpectComplete, SC_ExpectIncomplete, SC_DoNotCheck }; 31 32 std::string format(llvm::StringRef Code, 33 const FormatStyle &Style = getLLVMStyle(), 34 StatusCheck CheckComplete = SC_ExpectComplete) { 35 LLVM_DEBUG(llvm::errs() << "---\n"); 36 LLVM_DEBUG(llvm::errs() << Code << "\n\n"); 37 std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size())); 38 FormattingAttemptStatus Status; 39 tooling::Replacements Replaces = 40 reformat(Style, Code, Ranges, "<stdin>", &Status); 41 if (CheckComplete != SC_DoNotCheck) { 42 bool ExpectedCompleteFormat = CheckComplete == SC_ExpectComplete; 43 EXPECT_EQ(ExpectedCompleteFormat, Status.FormatComplete) 44 << Code << "\n\n"; 45 } 46 ReplacementCount = Replaces.size(); 47 auto Result = applyAllReplacements(Code, Replaces); 48 EXPECT_TRUE(static_cast<bool>(Result)); 49 LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n"); 50 return *Result; 51 } 52 53 FormatStyle getStyleWithColumns(FormatStyle Style, unsigned ColumnLimit) { 54 Style.ColumnLimit = ColumnLimit; 55 return Style; 56 } 57 58 FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) { 59 return getStyleWithColumns(getLLVMStyle(), ColumnLimit); 60 } 61 62 int ReplacementCount; 63 64 FormatStyle getRawStringPbStyleWithColumns(unsigned ColumnLimit) { 65 FormatStyle Style = getLLVMStyle(); 66 Style.ColumnLimit = ColumnLimit; 67 Style.RawStringFormats = { 68 { 69 /*Language=*/FormatStyle::LK_TextProto, 70 /*Delimiters=*/{"pb"}, 71 /*EnclosingFunctions=*/{}, 72 /*CanonicalDelimiter=*/"", 73 /*BasedOnStyle=*/"google", 74 }, 75 }; 76 return Style; 77 } 78 79 FormatStyle getRawStringLLVMCppStyleBasedOn(std::string BasedOnStyle) { 80 FormatStyle Style = getLLVMStyle(); 81 Style.RawStringFormats = { 82 { 83 /*Language=*/FormatStyle::LK_Cpp, 84 /*Delimiters=*/{"cpp"}, 85 /*EnclosingFunctions=*/{}, 86 /*CanonicalDelimiter=*/"", 87 BasedOnStyle, 88 }, 89 }; 90 return Style; 91 } 92 93 FormatStyle getRawStringGoogleCppStyleBasedOn(std::string BasedOnStyle) { 94 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); 95 Style.RawStringFormats = { 96 { 97 /*Language=*/FormatStyle::LK_Cpp, 98 /*Delimiters=*/{"cpp"}, 99 /*EnclosingFunctions=*/{}, 100 /*CanonicalDelimiter=*/"", 101 BasedOnStyle, 102 }, 103 }; 104 return Style; 105 } 106 107 // Gcc 4.8 doesn't support raw string literals in macros, which breaks some 108 // build bots. We use this function instead. 109 void expect_eq(const std::string Expected, const std::string Actual) { 110 EXPECT_EQ(Expected, Actual); 111 } 112 }; 113 114 TEST_F(FormatTestRawStrings, ReformatsAccordingToBaseStyle) { 115 // llvm style puts '*' on the right. 116 // google style puts '*' on the left. 117 118 // Use the llvm style if the raw string style has no BasedOnStyle. 119 expect_eq(R"test(int *i = R"cpp(int *p = nullptr;)cpp")test", 120 format(R"test(int * i = R"cpp(int * p = nullptr;)cpp")test", 121 getRawStringLLVMCppStyleBasedOn(""))); 122 123 // Use the google style if the raw string style has BasedOnStyle=google. 124 expect_eq(R"test(int *i = R"cpp(int* p = nullptr;)cpp")test", 125 format(R"test(int * i = R"cpp(int * p = nullptr;)cpp")test", 126 getRawStringLLVMCppStyleBasedOn("google"))); 127 128 // Use the llvm style if the raw string style has no BasedOnStyle=llvm. 129 expect_eq(R"test(int* i = R"cpp(int *p = nullptr;)cpp")test", 130 format(R"test(int * i = R"cpp(int * p = nullptr;)cpp")test", 131 getRawStringGoogleCppStyleBasedOn("llvm"))); 132 } 133 134 TEST_F(FormatTestRawStrings, UsesConfigurationOverBaseStyle) { 135 // llvm style puts '*' on the right. 136 // google style puts '*' on the left. 137 138 // Uses the configured google style inside raw strings even if BasedOnStyle in 139 // the raw string format is llvm. 140 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); 141 EXPECT_EQ(0, parseConfiguration("---\n" 142 "Language: Cpp\n" 143 "BasedOnStyle: Google", 144 &Style) 145 .value()); 146 Style.RawStringFormats = {{ 147 FormatStyle::LK_Cpp, 148 {"cpp"}, 149 {}, 150 /*CanonicalDelimiter=*/"", 151 /*BasedOnStyle=*/"llvm", 152 }}; 153 expect_eq(R"test(int* i = R"cpp(int* j = 0;)cpp";)test", 154 format(R"test(int * i = R"cpp(int * j = 0;)cpp";)test", Style)); 155 } 156 157 TEST_F(FormatTestRawStrings, MatchesDelimitersCaseSensitively) { 158 // Don't touch the 'PB' raw string, format the 'pb' raw string. 159 expect_eq(R"test( 160 s = R"PB(item:1)PB"; 161 t = R"pb(item: 1)pb";)test", 162 format(R"test( 163 s = R"PB(item:1)PB"; 164 t = R"pb(item:1)pb";)test", 165 getRawStringPbStyleWithColumns(40))); 166 } 167 168 TEST_F(FormatTestRawStrings, RespectsClangFormatOff) { 169 expect_eq(R"test( 170 // clang-format off 171 s = R"pb(item: 1)pb"; 172 // clang-format on 173 t = R"pb(item: 1)pb";)test", 174 format(R"test( 175 // clang-format off 176 s = R"pb(item: 1)pb"; 177 // clang-format on 178 t = R"pb(item: 1)pb";)test", 179 getRawStringPbStyleWithColumns(40))); 180 } 181 182 TEST_F(FormatTestRawStrings, ReformatsShortRawStringsOnSingleLine) { 183 expect_eq(R"test(P p = TP(R"pb()pb");)test", 184 format(R"test(P p = TP(R"pb( )pb");)test", 185 getRawStringPbStyleWithColumns(40))); 186 expect_eq(R"test(P p = TP(R"pb(item_1: 1)pb");)test", 187 format(R"test(P p = TP(R"pb(item_1:1)pb");)test", 188 getRawStringPbStyleWithColumns(40))); 189 expect_eq(R"test(P p = TP(R"pb(item_1: 1)pb");)test", 190 format(R"test(P p = TP(R"pb( item_1 : 1 )pb");)test", 191 getRawStringPbStyleWithColumns(40))); 192 expect_eq(R"test(P p = TP(R"pb(item_1: 1 item_2: 2)pb");)test", 193 format(R"test(P p = TP(R"pb(item_1:1 item_2:2)pb");)test", 194 getRawStringPbStyleWithColumns(40))); 195 // Merge two short lines into one. 196 expect_eq(R"test( 197 std::string s = R"pb( 198 item_1: 1 item_2: 2 199 )pb"; 200 )test", 201 format(R"test( 202 std::string s = R"pb( 203 item_1:1 204 item_2:2 205 )pb"; 206 )test", 207 getRawStringPbStyleWithColumns(40))); 208 } 209 210 TEST_F(FormatTestRawStrings, BreaksShortRawStringsWhenNeeded) { 211 // The raw string contains multiple submessage entries, so break for 212 // readability. 213 expect_eq(R"test( 214 P p = TP(R"pb(item_1 < 1 > 215 item_2: { 2 })pb");)test", 216 format( 217 R"test( 218 P p = TP(R"pb(item_1<1> item_2:{2})pb");)test", 219 getRawStringPbStyleWithColumns(40))); 220 } 221 222 TEST_F(FormatTestRawStrings, BreaksRawStringsExceedingColumnLimit) { 223 expect_eq(R"test( 224 P p = TPPPPPPPPPPPPPPP( 225 R"pb(item_1: 1, item_2: 2)pb");)test", 226 format(R"test( 227 P p = TPPPPPPPPPPPPPPP(R"pb(item_1: 1, item_2: 2)pb");)test", 228 getRawStringPbStyleWithColumns(40))); 229 230 expect_eq(R"test( 231 P p = 232 TPPPPPPPPPPPPPPP( 233 R"pb(item_1: 1, 234 item_2: 2, 235 item_3: 3)pb");)test", 236 format(R"test( 237 P p = TPPPPPPPPPPPPPPP(R"pb(item_1: 1, item_2: 2, item_3: 3)pb");)test", 238 getRawStringPbStyleWithColumns(40))); 239 240 expect_eq(R"test( 241 P p = TP(R"pb(item_1 < 1 > 242 item_2: < 2 > 243 item_3 {})pb");)test", 244 format(R"test( 245 P p = TP(R"pb(item_1<1> item_2:<2> item_3{ })pb");)test", 246 getRawStringPbStyleWithColumns(40))); 247 248 expect_eq( 249 R"test( 250 P p = TP(R"pb(item_1: 1, 251 item_2: 2, 252 item_3: 3, 253 item_4: 4)pb");)test", 254 format( 255 R"test( 256 P p = TP(R"pb(item_1: 1, item_2: 2, item_3: 3, item_4: 4)pb");)test", 257 getRawStringPbStyleWithColumns(40))); 258 259 expect_eq(R"test( 260 P p = TPPPPPPPPPPPPPPP( 261 R"pb(item_1 < 1 >, 262 item_2: { 2 }, 263 item_3: < 3 >, 264 item_4: { 4 })pb");)test", 265 format(R"test( 266 P p = TPPPPPPPPPPPPPPP(R"pb(item_1<1>, item_2: {2}, item_3: <3>, item_4:{4})pb");)test", 267 getRawStringPbStyleWithColumns(40))); 268 269 // Breaks before a short raw string exceeding the column limit. 270 expect_eq(R"test( 271 FFFFFFFFFFFFFFFFFFFFFFFFFFF( 272 R"pb(key: 1)pb"); 273 P p = TPPPPPPPPPPPPPPPPPPPP( 274 R"pb(key: 2)pb"); 275 auto TPPPPPPPPPPPPPPPPPPPP = 276 R"pb(key: 3)pb"; 277 P p = TPPPPPPPPPPPPPPPPPPPP( 278 R"pb(i: 1, j: 2)pb"); 279 280 int f(string s) { 281 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF( 282 R"pb(key: 1)pb"); 283 P p = TPPPPPPPPPPPPPPPPPPPP( 284 R"pb(key: 2)pb"); 285 auto TPPPPPPPPPPPPPPPPPPPP = 286 R"pb(key: 3)pb"; 287 if (s.empty()) 288 P p = TPPPPPPPPPPPPPPPPPPPP( 289 R"pb(i: 1, j: 2)pb"); 290 } 291 )test", 292 format(R"test( 293 FFFFFFFFFFFFFFFFFFFFFFFFFFF(R"pb(key:1)pb"); 294 P p = TPPPPPPPPPPPPPPPPPPPP(R"pb(key:2)pb"); 295 auto TPPPPPPPPPPPPPPPPPPPP = R"pb(key:3)pb"; 296 P p = TPPPPPPPPPPPPPPPPPPPP(R"pb(i: 1, j:2)pb"); 297 298 int f(string s) { 299 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF(R"pb(key:1)pb"); 300 P p = TPPPPPPPPPPPPPPPPPPPP(R"pb(key:2)pb"); 301 auto TPPPPPPPPPPPPPPPPPPPP = R"pb(key:3)pb"; 302 if (s.empty()) 303 P p = TPPPPPPPPPPPPPPPPPPPP(R"pb(i: 1, j:2)pb"); 304 } 305 )test", 306 getRawStringPbStyleWithColumns(40))); 307 } 308 309 TEST_F(FormatTestRawStrings, FormatsRawStringArguments) { 310 expect_eq(R"test( 311 P p = TP(R"pb(key { 1 })pb", param_2);)test", 312 format(R"test( 313 P p = TP(R"pb(key{1})pb",param_2);)test", 314 getRawStringPbStyleWithColumns(40))); 315 316 expect_eq(R"test( 317 PPPPPPPPPPPPP(R"pb(keykeyk)pb", 318 param_2);)test", 319 format(R"test( 320 PPPPPPPPPPPPP(R"pb(keykeyk)pb", param_2);)test", 321 getRawStringPbStyleWithColumns(40))); 322 323 expect_eq(R"test( 324 P p = TP( 325 R"pb(item: { i: 1, s: 's' } 326 item: { i: 2, s: 't' })pb");)test", 327 format(R"test( 328 P p = TP(R"pb(item: {i: 1, s: 's'} item: {i: 2, s: 't'})pb");)test", 329 getRawStringPbStyleWithColumns(40))); 330 expect_eq(R"test( 331 FFFFFFFFFFFFFFFFFFF( 332 R"pb(key: "value")pb", 333 R"pb(key2: "value")pb");)test", 334 format(R"test( 335 FFFFFFFFFFFFFFFFFFF(R"pb(key: "value")pb", R"pb(key2: "value")pb");)test", 336 getRawStringPbStyleWithColumns(40))); 337 338 // Formats the first out of two arguments. 339 expect_eq(R"test( 340 FFFFFFFF(R"pb(key: 1)pb", argument2); 341 struct S { 342 const s = 343 f(R"pb(key: 1)pb", argument2); 344 void f() { 345 if (gol) 346 return g(R"pb(key: 1)pb", 347 132789237); 348 return g(R"pb(key: 1)pb", "172893"); 349 } 350 };)test", 351 format(R"test( 352 FFFFFFFF(R"pb(key:1)pb", argument2); 353 struct S { 354 const s = f(R"pb(key:1)pb", argument2); 355 void f() { 356 if (gol) 357 return g(R"pb(key:1)pb", 132789237); 358 return g(R"pb(key:1)pb", "172893"); 359 } 360 };)test", 361 getRawStringPbStyleWithColumns(40))); 362 363 // Formats the second out of two arguments. 364 expect_eq(R"test( 365 FFFFFFFF(argument1, R"pb(key: 2)pb"); 366 struct S { 367 const s = 368 f(argument1, R"pb(key: 2)pb"); 369 void f() { 370 if (gol) 371 return g(12784137, 372 R"pb(key: 2)pb"); 373 return g(17283122, R"pb(key: 2)pb"); 374 } 375 };)test", 376 format(R"test( 377 FFFFFFFF(argument1, R"pb(key:2)pb"); 378 struct S { 379 const s = f(argument1, R"pb(key:2)pb"); 380 void f() { 381 if (gol) 382 return g(12784137, R"pb(key:2)pb"); 383 return g(17283122, R"pb(key:2)pb"); 384 } 385 };)test", 386 getRawStringPbStyleWithColumns(40))); 387 388 // Formats two short raw string arguments. 389 expect_eq(R"test( 390 FFFFF(R"pb(key: 1)pb", R"pb(key: 2)pb");)test", 391 format(R"test( 392 FFFFF(R"pb(key:1)pb", R"pb(key:2)pb");)test", 393 getRawStringPbStyleWithColumns(40))); 394 // TODO(krasimir): The original source code fits on one line, so the 395 // non-optimizing formatter is chosen. But after the formatting in protos is 396 // made, the code doesn't fit on one line anymore and further formatting 397 // splits it. 398 // 399 // Should we disable raw string formatting for the non-optimizing formatter? 400 expect_eq(R"test( 401 FFFFFFF(R"pb(key: 1)pb", R"pb(key: 2)pb");)test", 402 format(R"test( 403 FFFFFFF(R"pb(key:1)pb", R"pb(key:2)pb");)test", 404 getRawStringPbStyleWithColumns(40))); 405 406 // Formats two short raw string arguments, puts second on newline. 407 expect_eq(R"test( 408 FFFFFFFF(R"pb(key: 1)pb", 409 R"pb(key: 2)pb");)test", 410 format(R"test( 411 FFFFFFFF(R"pb(key:1)pb", R"pb(key:2)pb");)test", 412 getRawStringPbStyleWithColumns(40))); 413 414 // Formats both arguments. 415 expect_eq(R"test( 416 FFFFFFFF(R"pb(key: 1)pb", 417 R"pb(key: 2)pb"); 418 struct S { 419 const s = f(R"pb(key: 1)pb", 420 R"pb(key: 2)pb"); 421 void f() { 422 if (gol) 423 return g(R"pb(key: 1)pb", 424 R"pb(key: 2)pb"); 425 return g(R"pb(k1)pb", R"pb(k2)pb"); 426 } 427 };)test", 428 format(R"test( 429 FFFFFFFF(R"pb(key:1)pb", R"pb(key:2)pb"); 430 struct S { 431 const s = f(R"pb(key:1)pb", R"pb(key:2)pb"); 432 void f() { 433 if (gol) 434 return g(R"pb(key:1)pb", R"pb(key:2)pb"); 435 return g(R"pb( k1 )pb", R"pb( k2 )pb"); 436 } 437 };)test", 438 getRawStringPbStyleWithColumns(40))); 439 } 440 441 TEST_F(FormatTestRawStrings, RawStringStartingWithNewlines) { 442 expect_eq(R"test( 443 std::string s = R"pb( 444 item_1: 1 445 )pb"; 446 )test", 447 format(R"test( 448 std::string s = R"pb( 449 item_1:1 450 )pb"; 451 )test", 452 getRawStringPbStyleWithColumns(40))); 453 454 expect_eq(R"test( 455 std::string s = R"pb( 456 457 item_1: 1 458 )pb"; 459 )test", 460 format(R"test( 461 std::string s = R"pb( 462 463 item_1:1 464 )pb"; 465 )test", 466 getRawStringPbStyleWithColumns(40))); 467 468 expect_eq(R"test( 469 std::string s = R"pb( 470 item_1: 1 471 )pb"; 472 )test", 473 format(R"test( 474 std::string s = R"pb( 475 item_1:1 476 477 )pb"; 478 )test", 479 getRawStringPbStyleWithColumns(40))); 480 481 expect_eq(R"test( 482 std::string s = R"pb( 483 item_1: 1, 484 item_2: 2 485 )pb"; 486 )test", 487 format(R"test( 488 std::string s = R"pb( 489 item_1:1, item_2:2 490 )pb"; 491 )test", 492 getRawStringPbStyleWithColumns(40))); 493 494 expect_eq(R"test( 495 std::string s = R"pb( 496 book { 497 title: "Alice's Adventures" 498 author: "Lewis Caroll" 499 } 500 book { 501 title: "Peter Pan" 502 author: "J. M. Barrie" 503 } 504 )pb"; 505 )test", 506 format(R"test( 507 std::string s = R"pb( 508 book { title: "Alice's Adventures" author: "Lewis Caroll" } 509 book { title: "Peter Pan" author: "J. M. Barrie" } 510 )pb"; 511 )test", 512 getRawStringPbStyleWithColumns(40))); 513 } 514 515 TEST_F(FormatTestRawStrings, BreaksBeforeRawStrings) { 516 expect_eq(R"test( 517 ASSERT_TRUE( 518 ParseFromString(R"pb(item_1: 1)pb"), 519 ptr);)test", 520 format(R"test( 521 ASSERT_TRUE(ParseFromString(R"pb(item_1: 1)pb"), ptr);)test", 522 getRawStringPbStyleWithColumns(40))); 523 524 expect_eq(R"test( 525 ASSERT_TRUE(toolong::ParseFromString( 526 R"pb(item_1: 1)pb"), 527 ptr);)test", 528 format(R"test( 529 ASSERT_TRUE(toolong::ParseFromString(R"pb(item_1: 1)pb"), ptr);)test", 530 getRawStringPbStyleWithColumns(40))); 531 532 expect_eq(R"test( 533 ASSERT_TRUE(ParseFromString( 534 R"pb(item_1: 1, 535 item_2: 2)pb"), 536 ptr);)test", 537 format(R"test( 538 ASSERT_TRUE(ParseFromString(R"pb(item_1: 1, item_2: 2)pb"), ptr);)test", 539 getRawStringPbStyleWithColumns(40))); 540 541 expect_eq(R"test( 542 ASSERT_TRUE( 543 ParseFromString( 544 R"pb(item_1: 1 item_2: 2)pb"), 545 ptr);)test", 546 format(R"test( 547 ASSERT_TRUE(ParseFromString(R"pb(item_1: 1 item_2: 2)pb"), ptr);)test", 548 getRawStringPbStyleWithColumns(40))); 549 } 550 551 TEST_F(FormatTestRawStrings, RawStringsInOperands) { 552 // Formats the raw string first operand of a binary operator expression. 553 expect_eq(R"test(auto S = R"pb(item_1: 1)pb" + rest;)test", 554 format(R"test(auto S = R"pb(item_1:1)pb" + rest;)test", 555 getRawStringPbStyleWithColumns(40))); 556 557 expect_eq(R"test( 558 auto S = R"pb(item_1: 1, item_2: 2)pb" + 559 rest;)test", 560 format(R"test( 561 auto S = R"pb(item_1:1,item_2:2)pb"+rest;)test", 562 getRawStringPbStyleWithColumns(40))); 563 564 expect_eq(R"test( 565 auto S = 566 R"pb(item_1: 1 item_2: 2)pb" + rest;)test", 567 format(R"test( 568 auto S = R"pb(item_1:1 item_2:2)pb"+rest;)test", 569 getRawStringPbStyleWithColumns(40))); 570 571 // `rest` fits on the line after )pb", but forced on newline since the raw 572 // string literal is multiline. 573 expect_eq(R"test( 574 auto S = R"pb(item_1: 1, 575 item_2: 2, 576 item_3: 3)pb" + 577 rest;)test", 578 format(R"test( 579 auto S = R"pb(item_1:1,item_2:2,item_3:3)pb"+rest;)test", 580 getRawStringPbStyleWithColumns(40))); 581 582 expect_eq(R"test( 583 auto S = R"pb(item_1: 1, 584 item_2: 2, 585 item_3: 3)pb" + 586 longlongrest;)test", 587 format(R"test( 588 auto S = R"pb(item_1:1,item_2:2,item_3:3)pb"+longlongrest;)test", 589 getRawStringPbStyleWithColumns(40))); 590 591 // Formats the raw string second operand of a binary operator expression. 592 expect_eq(R"test(auto S = first + R"pb(item_1: 1)pb";)test", 593 format(R"test(auto S = first + R"pb(item_1:1)pb";)test", 594 getRawStringPbStyleWithColumns(40))); 595 596 expect_eq(R"test( 597 auto S = first + R"pb(item_1: 1, 598 item_2: 2)pb";)test", 599 format(R"test( 600 auto S = first+R"pb(item_1:1,item_2:2)pb";)test", 601 getRawStringPbStyleWithColumns(40))); 602 603 expect_eq(R"test( 604 auto S = first + R"pb(item_1: 1 605 item_2: 2)pb";)test", 606 format(R"test( 607 auto S = first+R"pb(item_1:1 item_2:2)pb";)test", 608 getRawStringPbStyleWithColumns(40))); 609 610 expect_eq(R"test( 611 auto S = R"pb(item_1: 1, 612 item_2: 2, 613 item_3: 3)pb" + 614 rest;)test", 615 format(R"test( 616 auto S = R"pb(item_1:1,item_2:2,item_3:3)pb"+rest;)test", 617 getRawStringPbStyleWithColumns(40))); 618 619 expect_eq(R"test( 620 auto S = R"pb(item_1: 1, 621 item_2: 2, 622 item_3: 3)pb" + 623 longlongrest;)test", 624 format(R"test( 625 auto S = R"pb(item_1:1,item_2:2,item_3:3)pb"+longlongrest;)test", 626 getRawStringPbStyleWithColumns(40))); 627 628 // Formats the raw string operands in expressions. 629 expect_eq(R"test( 630 auto S = R"pb(item_1: 1)pb" + 631 R"pb(item_2: 2)pb"; 632 )test", 633 format(R"test( 634 auto S=R"pb(item_1:1)pb"+R"pb(item_2:2)pb"; 635 )test", 636 getRawStringPbStyleWithColumns(40))); 637 638 expect_eq(R"test( 639 auto S = R"pb(item_1: 1)pb" + 640 R"pb(item_2: 2)pb" + 641 R"pb(item_3: 3)pb"; 642 )test", 643 format(R"test( 644 auto S=R"pb(item_1:1)pb"+R"pb(item_2:2)pb"+R"pb(item_3:3)pb"; 645 )test", 646 getRawStringPbStyleWithColumns(40))); 647 648 expect_eq(R"test( 649 auto S = (count < 3) 650 ? R"pb(item_1: 1)pb" 651 : R"pb(item_2: 2)pb"; 652 )test", 653 format(R"test( 654 auto S=(count<3)?R"pb(item_1:1)pb":R"pb(item_2:2)pb"; 655 )test", 656 getRawStringPbStyleWithColumns(40))); 657 658 expect_eq(R"test( 659 auto S = 660 (count < 3) 661 ? R"pb(item_1: 1, item_2: 2)pb" 662 : R"pb(item_3: 3)pb"; 663 )test", 664 format(R"test( 665 auto S=(count<3)?R"pb(item_1:1,item_2:2)pb":R"pb(item_3:3)pb"; 666 )test", 667 getRawStringPbStyleWithColumns(40))); 668 669 expect_eq(R"test( 670 auto S = 671 (count < 3) 672 ? R"pb(item_1: 1)pb" 673 : R"pb(item_2: 2, item_3: 3)pb"; 674 )test", 675 format(R"test( 676 auto S=(count<3)?R"pb(item_1:1)pb":R"pb(item_2:2,item_3:3)pb"; 677 )test", 678 getRawStringPbStyleWithColumns(40))); 679 } 680 681 TEST_F(FormatTestRawStrings, PrefixAndSuffixAlignment) { 682 // Keep the suffix at the end of line if not on newline. 683 expect_eq(R"test( 684 int s() { 685 auto S = PTP( 686 R"pb( 687 item_1: 1, 688 item_2: 2)pb"); 689 })test", 690 format(R"test( 691 int s() { 692 auto S = PTP( 693 R"pb( 694 item_1: 1, 695 item_2: 2)pb"); 696 })test", 697 getRawStringPbStyleWithColumns(20))); 698 699 // Align the suffix with the surrounding indent if the prefix is not on 700 // a line of its own. 701 expect_eq(R"test( 702 int s() { 703 auto S = PTP(R"pb( 704 item_1: 1, 705 item_2: 2 706 )pb"); 707 })test", 708 format(R"test( 709 int s() { 710 auto S = PTP(R"pb( 711 item_1: 1, 712 item_2: 2 713 )pb"); 714 })test", 715 getRawStringPbStyleWithColumns(20))); 716 717 // Align the prefix with the suffix if both the prefix and suffix are on a 718 // line of their own. 719 expect_eq(R"test( 720 int s() { 721 auto S = PTP( 722 R"pb( 723 item_1: 1, 724 item_2: 2, 725 )pb"); 726 })test", 727 format(R"test( 728 int s() { 729 auto S = PTP( 730 R"pb( 731 item_1: 1, 732 item_2: 2, 733 )pb"); 734 })test", 735 getRawStringPbStyleWithColumns(20))); 736 } 737 738 TEST_F(FormatTestRawStrings, EstimatesPenalty) { 739 // The penalty for characters exceeding the column limit in the raw string 740 // forces 'hh' to be put on a newline. 741 expect_eq(R"test( 742 ff(gggggg, 743 hh(R"pb(key { 744 i1: k1 745 i2: k2 746 })pb")); 747 )test", 748 format(R"test( 749 ff(gggggg, hh(R"pb(key { 750 i1: k1 751 i2: k2 752 })pb")); 753 )test", 754 getRawStringPbStyleWithColumns(20))); 755 } 756 757 TEST_F(FormatTestRawStrings, DontFormatNonRawStrings) { 758 expect_eq(R"test(a = R"pb(key:value)";)test", 759 format(R"test(a = R"pb(key:value)";)test", 760 getRawStringPbStyleWithColumns(20))); 761 } 762 763 TEST_F(FormatTestRawStrings, FormatsRawStringsWithEnclosingFunctionName) { 764 FormatStyle Style = getRawStringPbStyleWithColumns(40); 765 Style.RawStringFormats[0].EnclosingFunctions.push_back("PARSE_TEXT_PROTO"); 766 Style.RawStringFormats[0].EnclosingFunctions.push_back("ParseTextProto"); 767 expect_eq(R"test(a = PARSE_TEXT_PROTO(R"(key: value)");)test", 768 format(R"test(a = PARSE_TEXT_PROTO(R"(key:value)");)test", Style)); 769 770 expect_eq(R"test( 771 a = PARSE_TEXT_PROTO /**/ ( 772 /**/ R"(key: value)");)test", 773 format(R"test( 774 a = PARSE_TEXT_PROTO/**/(/**/R"(key:value)");)test", 775 Style)); 776 777 expect_eq(R"test( 778 a = ParseTextProto<ProtoType>( 779 R"(key: value)");)test", 780 format(R"test( 781 a = ParseTextProto<ProtoType>(R"(key:value)");)test", 782 Style)); 783 } 784 785 TEST_F(FormatTestRawStrings, UpdatesToCanonicalDelimiters) { 786 FormatStyle Style = getRawStringPbStyleWithColumns(25); 787 Style.RawStringFormats[0].CanonicalDelimiter = "proto"; 788 expect_eq(R"test(a = R"proto(key: value)proto";)test", 789 format(R"test(a = R"pb(key:value)pb";)test", Style)); 790 791 // Don't update to canonical delimiter if it occurs as a raw string suffix in 792 // the raw string content. 793 expect_eq(R"test(a = R"pb(key: ")proto")pb";)test", 794 format(R"test(a = R"pb(key:")proto")pb";)test", Style)); 795 } 796 797 TEST_F(FormatTestRawStrings, PenalizesPrefixExcessChars) { 798 FormatStyle Style = getRawStringPbStyleWithColumns(60); 799 800 // The '(' in R"pb is at column 60, no break. 801 expect_eq(R"test( 802 xxxxxxxaaaaax wwwwwww = _Verxrrrrrrrr(PARSE_TEXT_PROTO(R"pb( 803 Category: aaaaaaaaaaaaaaaaaaaaaaaaaa 804 )pb")); 805 )test", 806 format(R"test( 807 xxxxxxxaaaaax wwwwwww = _Verxrrrrrrrr(PARSE_TEXT_PROTO(R"pb( 808 Category: aaaaaaaaaaaaaaaaaaaaaaaaaa 809 )pb")); 810 )test", 811 Style)); 812 // The '(' in R"pb is at column 61, break. 813 expect_eq(R"test( 814 xxxxxxxaaaaax wwwwwww = 815 _Verxrrrrrrrrr(PARSE_TEXT_PROTO(R"pb( 816 Category: aaaaaaaaaaaaaaaaaaaaaaaaaa 817 )pb")); 818 )test", 819 format(R"test( 820 xxxxxxxaaaaax wwwwwww = _Verxrrrrrrrrr(PARSE_TEXT_PROTO(R"pb( 821 Category: aaaaaaaaaaaaaaaaaaaaaaaaaa 822 )pb")); 823 )test", 824 Style)); 825 } 826 827 TEST_F(FormatTestRawStrings, KeepsRBraceFolloedByMoreLBracesOnSameLine) { 828 FormatStyle Style = getRawStringPbStyleWithColumns(80); 829 830 expect_eq( 831 R"test( 832 int f() { 833 if (1) { 834 TTTTTTTTTTTTTTTTTTTTT s = PARSE_TEXT_PROTO(R"pb( 835 ttttttttt { 836 ppppppppppppp { 837 [cccccccccc.pppppppppppppp.TTTTTTTTTTTTTTTTTTTT] { field_1: "123_1" } 838 [cccccccccc.pppppppppppppp.TTTTTTTTTTTTTTTTTTTT] { field_2: "123_2" } 839 } 840 } 841 )pb"); 842 } 843 } 844 )test", 845 format( 846 R"test( 847 int f() { 848 if (1) { 849 TTTTTTTTTTTTTTTTTTTTT s = PARSE_TEXT_PROTO(R"pb( 850 ttttttttt { 851 ppppppppppppp { 852 [cccccccccc.pppppppppppppp.TTTTTTTTTTTTTTTTTTTT] { field_1: "123_1" } 853 [cccccccccc.pppppppppppppp.TTTTTTTTTTTTTTTTTTTT] { field_2: "123_2" }}} 854 )pb"); 855 } 856 } 857 )test", 858 Style)); 859 } 860 861 TEST_F(FormatTestRawStrings, 862 DoNotFormatUnrecognizedDelimitersInRecognizedFunctions) { 863 FormatStyle Style = getRawStringPbStyleWithColumns(60); 864 Style.RawStringFormats[0].EnclosingFunctions.push_back("EqualsProto"); 865 // EqualsProto is a recognized function, but the Raw delimiter is 866 // unrecognized. Do not touch the string in this case, since it might be 867 // special. 868 expect_eq(R"test( 869 void f() { 870 aaaaaaaaa(bbbbbbbbb, EqualsProto(R"Raw( 871 item { 872 key: value 873 } 874 )Raw")); 875 })test", 876 format(R"test( 877 void f() { 878 aaaaaaaaa(bbbbbbbbb, EqualsProto(R"Raw( 879 item { 880 key: value 881 } 882 )Raw")); 883 })test", 884 Style)); 885 } 886 887 TEST_F(FormatTestRawStrings, 888 BreaksBeforeNextParamAfterMultilineRawStringParam) { 889 FormatStyle Style = getRawStringPbStyleWithColumns(60); 890 expect_eq(R"test( 891 int f() { 892 int a = g(x, R"pb( 893 key: 1 # 894 key: 2 895 )pb", 896 3, 4); 897 } 898 )test", 899 format(R"test( 900 int f() { 901 int a = g(x, R"pb( 902 key: 1 # 903 key: 2 904 )pb", 3, 4); 905 } 906 )test", 907 Style)); 908 909 // Breaks after a parent of a multiline param. 910 expect_eq(R"test( 911 int f() { 912 int a = g(x, h(R"pb( 913 key: 1 # 914 key: 2 915 )pb"), 916 3, 4); 917 } 918 )test", 919 format(R"test( 920 int f() { 921 int a = g(x, h(R"pb( 922 key: 1 # 923 key: 2 924 )pb"), 3, 4); 925 } 926 )test", 927 Style)); 928 929 expect_eq(R"test( 930 int f() { 931 int a = g(x, 932 h(R"pb( 933 key: 1 # 934 key: 2 935 )pb", 936 2), 937 3, 4); 938 } 939 )test", 940 format(R"test( 941 int f() { 942 int a = g(x, h(R"pb( 943 key: 1 # 944 key: 2 945 )pb", 2), 3, 4); 946 } 947 )test", 948 Style)); 949 // Breaks if formatting introduces a multiline raw string. 950 expect_eq(R"test( 951 int f() { 952 int a = g(x, R"pb(key1: value111111111 953 key2: value2222222222)pb", 954 3, 4); 955 } 956 )test", 957 format(R"test( 958 int f() { 959 int a = g(x, R"pb(key1: value111111111 key2: value2222222222)pb", 3, 4); 960 } 961 )test", 962 Style)); 963 // Does not force a break after an original multiline param that is 964 // reformatterd as on single line. 965 expect_eq(R"test( 966 int f() { 967 int a = g(R"pb(key: 1)pb", 2); 968 })test", 969 format(R"test( 970 int f() { 971 int a = g(R"pb(key: 972 1)pb", 2); 973 })test", 974 Style)); 975 } 976 977 TEST_F(FormatTestRawStrings, IndentsLastParamAfterNewline) { 978 FormatStyle Style = getRawStringPbStyleWithColumns(60); 979 expect_eq(R"test( 980 fffffffffffffffffffff("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 981 R"pb( 982 b: c 983 )pb");)test", 984 format(R"test( 985 fffffffffffffffffffff("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 986 R"pb( 987 b: c 988 )pb");)test", 989 Style)); 990 } 991 } // end namespace 992 } // end namespace format 993 } // end namespace clang 994