1 //===- unittest/Format/FormatTest.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 "FormatTestBase.h" 10 11 #define DEBUG_TYPE "format-test" 12 13 namespace clang { 14 namespace format { 15 namespace test { 16 namespace { 17 18 class FormatTest : public test::FormatTestBase {}; 19 20 TEST_F(FormatTest, MessUp) { 21 EXPECT_EQ("1 2 3", messUp("1 2 3")); 22 EXPECT_EQ("1 2 3", messUp("1\n2\n3")); 23 EXPECT_EQ("a\n//b\nc", messUp("a\n//b\nc")); 24 EXPECT_EQ("a\n#b\nc", messUp("a\n#b\nc")); 25 EXPECT_EQ("a\n#b c d\ne", messUp("a\n#b\\\nc\\\nd\ne")); 26 } 27 28 TEST_F(FormatTest, DefaultLLVMStyleIsCpp) { 29 EXPECT_EQ(FormatStyle::LK_Cpp, getLLVMStyle().Language); 30 } 31 32 TEST_F(FormatTest, LLVMStyleOverride) { 33 EXPECT_EQ(FormatStyle::LK_Proto, 34 getLLVMStyle(FormatStyle::LK_Proto).Language); 35 } 36 37 //===----------------------------------------------------------------------===// 38 // Basic function tests. 39 //===----------------------------------------------------------------------===// 40 41 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) { verifyFormat(";"); } 42 43 TEST_F(FormatTest, FormatsGlobalStatementsAt0) { 44 verifyFormat("int i;", " int i;"); 45 verifyFormat("\nint i;", " \n\t \v \f int i;"); 46 verifyFormat("int i;\nint j;", " int i; int j;"); 47 verifyFormat("int i;\nint j;", " int i;\n int j;"); 48 49 auto Style = getLLVMStyle(); 50 Style.KeepEmptyLines.AtStartOfFile = false; 51 verifyFormat("int i;", " \n\t \v \f int i;", Style); 52 } 53 54 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) { 55 verifyFormat("int i;", "int\ni;"); 56 } 57 58 TEST_F(FormatTest, FormatsNestedBlockStatements) { 59 verifyFormat("{\n" 60 " {\n" 61 " {\n" 62 " }\n" 63 " }\n" 64 "}", 65 "{{{}}}"); 66 } 67 68 TEST_F(FormatTest, FormatsNestedCall) { 69 verifyFormat("Method(f1, f2(f3));"); 70 verifyFormat("Method(f1(f2, f3()));"); 71 verifyFormat("Method(f1(f2, (f3())));"); 72 } 73 74 TEST_F(FormatTest, NestedNameSpecifiers) { 75 verifyFormat("vector<::Type> v;"); 76 verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())"); 77 verifyFormat("static constexpr bool Bar = decltype(bar())::value;"); 78 verifyFormat("static constexpr bool Bar = typeof(bar())::value;"); 79 verifyFormat("static constexpr bool Bar = __underlying_type(bar())::value;"); 80 verifyFormat("static constexpr bool Bar = _Atomic(bar())::value;"); 81 verifyFormat("bool a = 2 < ::SomeFunction();"); 82 verifyFormat("ALWAYS_INLINE ::std::string getName();"); 83 verifyFormat("some::string getName();"); 84 } 85 86 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) { 87 verifyFormat("if (a) {\n" 88 " f();\n" 89 "}", 90 "if(a){f();}"); 91 EXPECT_EQ(4, ReplacementCount); 92 verifyNoChange("if (a) {\n" 93 " f();\n" 94 "}"); 95 EXPECT_EQ(0, ReplacementCount); 96 verifyNoChange("/*\r\n" 97 "\r\n" 98 "*/"); 99 EXPECT_EQ(0, ReplacementCount); 100 } 101 102 TEST_F(FormatTest, RemovesEmptyLines) { 103 verifyFormat("class C {\n" 104 " int i;\n" 105 "};", 106 "class C {\n" 107 " int i;\n" 108 "\n" 109 "};"); 110 111 // Don't remove empty lines at the start of namespaces or extern "C" blocks. 112 verifyFormat("namespace N {\n" 113 "\n" 114 "int i;\n" 115 "}", 116 "namespace N {\n" 117 "\n" 118 "int i;\n" 119 "}", 120 getGoogleStyle()); 121 verifyFormat("/* something */ namespace N {\n" 122 "\n" 123 "int i;\n" 124 "}", 125 "/* something */ namespace N {\n" 126 "\n" 127 "int i;\n" 128 "}", 129 getGoogleStyle()); 130 verifyFormat("inline namespace N {\n" 131 "\n" 132 "int i;\n" 133 "}", 134 "inline namespace N {\n" 135 "\n" 136 "int i;\n" 137 "}", 138 getGoogleStyle()); 139 verifyFormat("/* something */ inline namespace N {\n" 140 "\n" 141 "int i;\n" 142 "}", 143 "/* something */ inline namespace N {\n" 144 "\n" 145 "int i;\n" 146 "}", 147 getGoogleStyle()); 148 verifyFormat("export namespace N {\n" 149 "\n" 150 "int i;\n" 151 "}", 152 "export namespace N {\n" 153 "\n" 154 "int i;\n" 155 "}", 156 getGoogleStyle()); 157 verifyFormat("extern /**/ \"C\" /**/ {\n" 158 "\n" 159 "int i;\n" 160 "}", 161 "extern /**/ \"C\" /**/ {\n" 162 "\n" 163 "int i;\n" 164 "}", 165 getGoogleStyle()); 166 167 auto CustomStyle = getLLVMStyle(); 168 CustomStyle.BreakBeforeBraces = FormatStyle::BS_Custom; 169 CustomStyle.BraceWrapping.AfterNamespace = true; 170 CustomStyle.KeepEmptyLines.AtStartOfBlock = false; 171 verifyFormat("namespace N\n" 172 "{\n" 173 "\n" 174 "int i;\n" 175 "}", 176 "namespace N\n" 177 "{\n" 178 "\n" 179 "\n" 180 "int i;\n" 181 "}", 182 CustomStyle); 183 verifyFormat("/* something */ namespace N\n" 184 "{\n" 185 "\n" 186 "int i;\n" 187 "}", 188 "/* something */ namespace N {\n" 189 "\n" 190 "\n" 191 "int i;\n" 192 "}", 193 CustomStyle); 194 verifyFormat("inline namespace N\n" 195 "{\n" 196 "\n" 197 "int i;\n" 198 "}", 199 "inline namespace N\n" 200 "{\n" 201 "\n" 202 "\n" 203 "int i;\n" 204 "}", 205 CustomStyle); 206 verifyFormat("/* something */ inline namespace N\n" 207 "{\n" 208 "\n" 209 "int i;\n" 210 "}", 211 "/* something */ inline namespace N\n" 212 "{\n" 213 "\n" 214 "int i;\n" 215 "}", 216 CustomStyle); 217 verifyFormat("export namespace N\n" 218 "{\n" 219 "\n" 220 "int i;\n" 221 "}", 222 "export namespace N\n" 223 "{\n" 224 "\n" 225 "int i;\n" 226 "}", 227 CustomStyle); 228 verifyFormat("namespace a\n" 229 "{\n" 230 "namespace b\n" 231 "{\n" 232 "\n" 233 "class AA {};\n" 234 "\n" 235 "} // namespace b\n" 236 "} // namespace a", 237 "namespace a\n" 238 "{\n" 239 "namespace b\n" 240 "{\n" 241 "\n" 242 "\n" 243 "class AA {};\n" 244 "\n" 245 "\n" 246 "}\n" 247 "}", 248 CustomStyle); 249 verifyFormat("namespace A /* comment */\n" 250 "{\n" 251 "class B {}\n" 252 "} // namespace A", 253 "namespace A /* comment */ { class B {} }", CustomStyle); 254 verifyFormat("namespace A\n" 255 "{ /* comment */\n" 256 "class B {}\n" 257 "} // namespace A", 258 "namespace A {/* comment */ class B {} }", CustomStyle); 259 verifyFormat("namespace A\n" 260 "{ /* comment */\n" 261 "\n" 262 "class B {}\n" 263 "\n" 264 "" 265 "} // namespace A", 266 "namespace A { /* comment */\n" 267 "\n" 268 "\n" 269 "class B {}\n" 270 "\n" 271 "\n" 272 "}", 273 CustomStyle); 274 verifyFormat("namespace A /* comment */\n" 275 "{\n" 276 "\n" 277 "class B {}\n" 278 "\n" 279 "} // namespace A", 280 "namespace A/* comment */ {\n" 281 "\n" 282 "\n" 283 "class B {}\n" 284 "\n" 285 "\n" 286 "}", 287 CustomStyle); 288 289 // ...but do keep inlining and removing empty lines for non-block extern "C" 290 // functions. 291 verifyGoogleFormat("extern \"C\" int f() { return 42; }"); 292 verifyFormat("extern \"C\" int f() {\n" 293 " int i = 42;\n" 294 " return i;\n" 295 "}", 296 "extern \"C\" int f() {\n" 297 "\n" 298 " int i = 42;\n" 299 " return i;\n" 300 "}", 301 getGoogleStyle()); 302 303 // Remove empty lines at the beginning and end of blocks. 304 verifyFormat("void f() {\n" 305 "\n" 306 " if (a) {\n" 307 "\n" 308 " f();\n" 309 " }\n" 310 "}", 311 "void f() {\n" 312 "\n" 313 " if (a) {\n" 314 "\n" 315 " f();\n" 316 "\n" 317 " }\n" 318 "\n" 319 "}"); 320 verifyFormat("void f() {\n" 321 " if (a) {\n" 322 " f();\n" 323 " }\n" 324 "}", 325 "void f() {\n" 326 "\n" 327 " if (a) {\n" 328 "\n" 329 " f();\n" 330 "\n" 331 " }\n" 332 "\n" 333 "}", 334 getGoogleStyle()); 335 336 // Don't remove empty lines in more complex control statements. 337 verifyFormat("void f() {\n" 338 " if (a) {\n" 339 " f();\n" 340 "\n" 341 " } else if (b) {\n" 342 " f();\n" 343 " }\n" 344 "}", 345 "void f() {\n" 346 " if (a) {\n" 347 " f();\n" 348 "\n" 349 " } else if (b) {\n" 350 " f();\n" 351 "\n" 352 " }\n" 353 "\n" 354 "}"); 355 356 // Don't remove empty lines before namespace endings. 357 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 358 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 359 verifyNoChange("namespace {\n" 360 "int i;\n" 361 "\n" 362 "}", 363 LLVMWithNoNamespaceFix); 364 verifyFormat("namespace {\n" 365 "int i;\n" 366 "}", 367 LLVMWithNoNamespaceFix); 368 verifyNoChange("namespace {\n" 369 "int i;\n" 370 "\n" 371 "};", 372 LLVMWithNoNamespaceFix); 373 verifyFormat("namespace {\n" 374 "int i;\n" 375 "};", 376 LLVMWithNoNamespaceFix); 377 verifyNoChange("namespace {\n" 378 "int i;\n" 379 "\n" 380 "}"); 381 verifyFormat("namespace {\n" 382 "int i;\n" 383 "\n" 384 "} // namespace", 385 "namespace {\n" 386 "int i;\n" 387 "\n" 388 "} // namespace"); 389 390 FormatStyle Style = getLLVMStyle(); 391 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 392 Style.MaxEmptyLinesToKeep = 2; 393 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 394 Style.BraceWrapping.AfterClass = true; 395 Style.BraceWrapping.AfterFunction = true; 396 Style.KeepEmptyLines.AtStartOfBlock = false; 397 398 verifyFormat("class Foo\n" 399 "{\n" 400 " Foo() {}\n" 401 "\n" 402 " void funk() {}\n" 403 "};", 404 "class Foo\n" 405 "{\n" 406 " Foo()\n" 407 " {\n" 408 " }\n" 409 "\n" 410 " void funk() {}\n" 411 "};", 412 Style); 413 } 414 415 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) { 416 verifyFormat("x = (a) and (b);"); 417 verifyFormat("x = (a) or (b);"); 418 verifyFormat("x = (a) bitand (b);"); 419 verifyFormat("x = (a) bitor (b);"); 420 verifyFormat("x = (a) not_eq (b);"); 421 verifyFormat("x = (a) and_eq (b);"); 422 verifyFormat("x = (a) or_eq (b);"); 423 verifyFormat("x = (a) xor (b);"); 424 } 425 426 TEST_F(FormatTest, RecognizesUnaryOperatorKeywords) { 427 verifyFormat("x = compl(a);"); 428 verifyFormat("x = not(a);"); 429 verifyFormat("x = bitand(a);"); 430 // Unary operator must not be merged with the next identifier 431 verifyFormat("x = compl a;"); 432 verifyFormat("x = not a;"); 433 verifyFormat("x = bitand a;"); 434 } 435 436 //===----------------------------------------------------------------------===// 437 // Tests for control statements. 438 //===----------------------------------------------------------------------===// 439 440 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) { 441 verifyFormat("if (true)\n f();\ng();"); 442 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); 443 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); 444 verifyFormat("if constexpr (true)\n" 445 " f();\ng();"); 446 verifyFormat("if CONSTEXPR (true)\n" 447 " f();\ng();"); 448 verifyFormat("if constexpr (a)\n" 449 " if constexpr (b)\n" 450 " if constexpr (c)\n" 451 " g();\n" 452 "h();"); 453 verifyFormat("if CONSTEXPR (a)\n" 454 " if CONSTEXPR (b)\n" 455 " if CONSTEXPR (c)\n" 456 " g();\n" 457 "h();"); 458 verifyFormat("if constexpr (a)\n" 459 " if constexpr (b) {\n" 460 " f();\n" 461 " }\n" 462 "g();"); 463 verifyFormat("if CONSTEXPR (a)\n" 464 " if CONSTEXPR (b) {\n" 465 " f();\n" 466 " }\n" 467 "g();"); 468 469 verifyFormat("if consteval {\n}"); 470 verifyFormat("if !consteval {\n}"); 471 verifyFormat("if not consteval {\n}"); 472 verifyFormat("if consteval {\n} else {\n}"); 473 verifyFormat("if !consteval {\n} else {\n}"); 474 verifyFormat("if consteval {\n" 475 " f();\n" 476 "}"); 477 verifyFormat("if !consteval {\n" 478 " f();\n" 479 "}"); 480 verifyFormat("if consteval {\n" 481 " f();\n" 482 "} else {\n" 483 " g();\n" 484 "}"); 485 verifyFormat("if CONSTEVAL {\n" 486 " f();\n" 487 "}"); 488 verifyFormat("if !CONSTEVAL {\n" 489 " f();\n" 490 "}"); 491 492 verifyFormat("if (a)\n" 493 " g();"); 494 verifyFormat("if (a) {\n" 495 " g()\n" 496 "};"); 497 verifyFormat("if (a)\n" 498 " g();\n" 499 "else\n" 500 " g();"); 501 verifyFormat("if (a) {\n" 502 " g();\n" 503 "} else\n" 504 " g();"); 505 verifyFormat("if (a)\n" 506 " g();\n" 507 "else {\n" 508 " g();\n" 509 "}"); 510 verifyFormat("if (a) {\n" 511 " g();\n" 512 "} else {\n" 513 " g();\n" 514 "}"); 515 verifyFormat("if (a)\n" 516 " g();\n" 517 "else if (b)\n" 518 " g();\n" 519 "else\n" 520 " g();"); 521 verifyFormat("if (a) {\n" 522 " g();\n" 523 "} else if (b)\n" 524 " g();\n" 525 "else\n" 526 " g();"); 527 verifyFormat("if (a)\n" 528 " g();\n" 529 "else if (b) {\n" 530 " g();\n" 531 "} else\n" 532 " g();"); 533 verifyFormat("if (a)\n" 534 " g();\n" 535 "else if (b)\n" 536 " g();\n" 537 "else {\n" 538 " g();\n" 539 "}"); 540 verifyFormat("if (a)\n" 541 " g();\n" 542 "else if (b) {\n" 543 " g();\n" 544 "} else {\n" 545 " g();\n" 546 "}"); 547 verifyFormat("if (a) {\n" 548 " g();\n" 549 "} else if (b) {\n" 550 " g();\n" 551 "} else {\n" 552 " g();\n" 553 "}"); 554 555 FormatStyle AllowsMergedIf = getLLVMStyle(); 556 AllowsMergedIf.IfMacros.push_back("MYIF"); 557 AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left; 558 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 559 FormatStyle::SIS_WithoutElse; 560 verifyFormat("if (a)\n" 561 " // comment\n" 562 " f();", 563 AllowsMergedIf); 564 verifyFormat("{\n" 565 " if (a)\n" 566 " label:\n" 567 " f();\n" 568 "}", 569 AllowsMergedIf); 570 verifyFormat("#define A \\\n" 571 " if (a) \\\n" 572 " label: \\\n" 573 " f()", 574 AllowsMergedIf); 575 verifyFormat("if (a)\n" 576 " ;", 577 AllowsMergedIf); 578 verifyFormat("if (a)\n" 579 " if (b) return;", 580 AllowsMergedIf); 581 582 verifyFormat("if (a) // Can't merge this\n" 583 " f();", 584 AllowsMergedIf); 585 verifyFormat("if (a) /* still don't merge */\n" 586 " f();", 587 AllowsMergedIf); 588 verifyFormat("if (a) { // Never merge this\n" 589 " f();\n" 590 "}", 591 AllowsMergedIf); 592 verifyFormat("if (a) { /* Never merge this */\n" 593 " f();\n" 594 "}", 595 AllowsMergedIf); 596 verifyFormat("MYIF (a)\n" 597 " // comment\n" 598 " f();", 599 AllowsMergedIf); 600 verifyFormat("{\n" 601 " MYIF (a)\n" 602 " label:\n" 603 " f();\n" 604 "}", 605 AllowsMergedIf); 606 verifyFormat("#define A \\\n" 607 " MYIF (a) \\\n" 608 " label: \\\n" 609 " f()", 610 AllowsMergedIf); 611 verifyFormat("MYIF (a)\n" 612 " ;", 613 AllowsMergedIf); 614 verifyFormat("MYIF (a)\n" 615 " MYIF (b) return;", 616 AllowsMergedIf); 617 618 verifyFormat("MYIF (a) // Can't merge this\n" 619 " f();", 620 AllowsMergedIf); 621 verifyFormat("MYIF (a) /* still don't merge */\n" 622 " f();", 623 AllowsMergedIf); 624 verifyFormat("MYIF (a) { // Never merge this\n" 625 " f();\n" 626 "}", 627 AllowsMergedIf); 628 verifyFormat("MYIF (a) { /* Never merge this */\n" 629 " f();\n" 630 "}", 631 AllowsMergedIf); 632 633 AllowsMergedIf.ColumnLimit = 14; 634 // Where line-lengths matter, a 2-letter synonym that maintains line length. 635 // Not IF to avoid any confusion that IF is somehow special. 636 AllowsMergedIf.IfMacros.push_back("FI"); 637 verifyFormat("if (a) return;", AllowsMergedIf); 638 verifyFormat("if (aaaaaaaaa)\n" 639 " return;", 640 AllowsMergedIf); 641 verifyFormat("FI (a) return;", AllowsMergedIf); 642 verifyFormat("FI (aaaaaaaaa)\n" 643 " return;", 644 AllowsMergedIf); 645 646 AllowsMergedIf.ColumnLimit = 13; 647 verifyFormat("if (a)\n return;", AllowsMergedIf); 648 verifyFormat("FI (a)\n return;", AllowsMergedIf); 649 650 FormatStyle AllowsMergedIfElse = getLLVMStyle(); 651 AllowsMergedIfElse.IfMacros.push_back("MYIF"); 652 AllowsMergedIfElse.AllowShortIfStatementsOnASingleLine = 653 FormatStyle::SIS_AllIfsAndElse; 654 verifyFormat("if (a)\n" 655 " // comment\n" 656 " f();\n" 657 "else\n" 658 " // comment\n" 659 " f();", 660 AllowsMergedIfElse); 661 verifyFormat("{\n" 662 " if (a)\n" 663 " label:\n" 664 " f();\n" 665 " else\n" 666 " label:\n" 667 " f();\n" 668 "}", 669 AllowsMergedIfElse); 670 verifyFormat("if (a)\n" 671 " ;\n" 672 "else\n" 673 " ;", 674 AllowsMergedIfElse); 675 verifyFormat("if (a) {\n" 676 "} else {\n" 677 "}", 678 AllowsMergedIfElse); 679 verifyFormat("if (a) return;\n" 680 "else if (b) return;\n" 681 "else return;", 682 AllowsMergedIfElse); 683 verifyFormat("if (a) {\n" 684 "} else return;", 685 AllowsMergedIfElse); 686 verifyFormat("if (a) {\n" 687 "} else if (b) return;\n" 688 "else return;", 689 AllowsMergedIfElse); 690 verifyFormat("if (a) return;\n" 691 "else if (b) {\n" 692 "} else return;", 693 AllowsMergedIfElse); 694 verifyFormat("if (a)\n" 695 " if (b) return;\n" 696 " else return;", 697 AllowsMergedIfElse); 698 verifyFormat("if constexpr (a)\n" 699 " if constexpr (b) return;\n" 700 " else if constexpr (c) return;\n" 701 " else return;", 702 AllowsMergedIfElse); 703 verifyFormat("MYIF (a)\n" 704 " // comment\n" 705 " f();\n" 706 "else\n" 707 " // comment\n" 708 " f();", 709 AllowsMergedIfElse); 710 verifyFormat("{\n" 711 " MYIF (a)\n" 712 " label:\n" 713 " f();\n" 714 " else\n" 715 " label:\n" 716 " f();\n" 717 "}", 718 AllowsMergedIfElse); 719 verifyFormat("MYIF (a)\n" 720 " ;\n" 721 "else\n" 722 " ;", 723 AllowsMergedIfElse); 724 verifyFormat("MYIF (a) {\n" 725 "} else {\n" 726 "}", 727 AllowsMergedIfElse); 728 verifyFormat("MYIF (a) return;\n" 729 "else MYIF (b) return;\n" 730 "else return;", 731 AllowsMergedIfElse); 732 verifyFormat("MYIF (a) {\n" 733 "} else return;", 734 AllowsMergedIfElse); 735 verifyFormat("MYIF (a) {\n" 736 "} else MYIF (b) return;\n" 737 "else return;", 738 AllowsMergedIfElse); 739 verifyFormat("MYIF (a) return;\n" 740 "else MYIF (b) {\n" 741 "} else return;", 742 AllowsMergedIfElse); 743 verifyFormat("MYIF (a)\n" 744 " MYIF (b) return;\n" 745 " else return;", 746 AllowsMergedIfElse); 747 verifyFormat("MYIF constexpr (a)\n" 748 " MYIF constexpr (b) return;\n" 749 " else MYIF constexpr (c) return;\n" 750 " else return;", 751 AllowsMergedIfElse); 752 } 753 754 TEST_F(FormatTest, FormatIfWithoutCompoundStatementButElseWith) { 755 FormatStyle AllowsMergedIf = getLLVMStyle(); 756 AllowsMergedIf.IfMacros.push_back("MYIF"); 757 AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left; 758 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 759 FormatStyle::SIS_WithoutElse; 760 verifyFormat("if (a)\n" 761 " f();\n" 762 "else {\n" 763 " g();\n" 764 "}", 765 AllowsMergedIf); 766 verifyFormat("if (a)\n" 767 " f();\n" 768 "else\n" 769 " g();", 770 AllowsMergedIf); 771 772 verifyFormat("if (a) g();", AllowsMergedIf); 773 verifyFormat("if (a) {\n" 774 " g()\n" 775 "};", 776 AllowsMergedIf); 777 verifyFormat("if (a)\n" 778 " g();\n" 779 "else\n" 780 " g();", 781 AllowsMergedIf); 782 verifyFormat("if (a) {\n" 783 " g();\n" 784 "} else\n" 785 " g();", 786 AllowsMergedIf); 787 verifyFormat("if (a)\n" 788 " g();\n" 789 "else {\n" 790 " g();\n" 791 "}", 792 AllowsMergedIf); 793 verifyFormat("if (a) {\n" 794 " g();\n" 795 "} else {\n" 796 " g();\n" 797 "}", 798 AllowsMergedIf); 799 verifyFormat("if (a)\n" 800 " g();\n" 801 "else if (b)\n" 802 " g();\n" 803 "else\n" 804 " g();", 805 AllowsMergedIf); 806 verifyFormat("if (a) {\n" 807 " g();\n" 808 "} else if (b)\n" 809 " g();\n" 810 "else\n" 811 " g();", 812 AllowsMergedIf); 813 verifyFormat("if (a)\n" 814 " g();\n" 815 "else if (b) {\n" 816 " g();\n" 817 "} else\n" 818 " g();", 819 AllowsMergedIf); 820 verifyFormat("if (a)\n" 821 " g();\n" 822 "else if (b)\n" 823 " g();\n" 824 "else {\n" 825 " g();\n" 826 "}", 827 AllowsMergedIf); 828 verifyFormat("if (a)\n" 829 " g();\n" 830 "else if (b) {\n" 831 " g();\n" 832 "} else {\n" 833 " g();\n" 834 "}", 835 AllowsMergedIf); 836 verifyFormat("if (a) {\n" 837 " g();\n" 838 "} else if (b) {\n" 839 " g();\n" 840 "} else {\n" 841 " g();\n" 842 "}", 843 AllowsMergedIf); 844 verifyFormat("MYIF (a)\n" 845 " f();\n" 846 "else {\n" 847 " g();\n" 848 "}", 849 AllowsMergedIf); 850 verifyFormat("MYIF (a)\n" 851 " f();\n" 852 "else\n" 853 " g();", 854 AllowsMergedIf); 855 856 verifyFormat("MYIF (a) g();", AllowsMergedIf); 857 verifyFormat("MYIF (a) {\n" 858 " g()\n" 859 "};", 860 AllowsMergedIf); 861 verifyFormat("MYIF (a)\n" 862 " g();\n" 863 "else\n" 864 " g();", 865 AllowsMergedIf); 866 verifyFormat("MYIF (a) {\n" 867 " g();\n" 868 "} else\n" 869 " g();", 870 AllowsMergedIf); 871 verifyFormat("MYIF (a)\n" 872 " g();\n" 873 "else {\n" 874 " g();\n" 875 "}", 876 AllowsMergedIf); 877 verifyFormat("MYIF (a) {\n" 878 " g();\n" 879 "} else {\n" 880 " g();\n" 881 "}", 882 AllowsMergedIf); 883 verifyFormat("MYIF (a)\n" 884 " g();\n" 885 "else MYIF (b)\n" 886 " g();\n" 887 "else\n" 888 " g();", 889 AllowsMergedIf); 890 verifyFormat("MYIF (a)\n" 891 " g();\n" 892 "else if (b)\n" 893 " g();\n" 894 "else\n" 895 " g();", 896 AllowsMergedIf); 897 verifyFormat("MYIF (a) {\n" 898 " g();\n" 899 "} else MYIF (b)\n" 900 " g();\n" 901 "else\n" 902 " g();", 903 AllowsMergedIf); 904 verifyFormat("MYIF (a) {\n" 905 " g();\n" 906 "} else if (b)\n" 907 " g();\n" 908 "else\n" 909 " g();", 910 AllowsMergedIf); 911 verifyFormat("MYIF (a)\n" 912 " g();\n" 913 "else MYIF (b) {\n" 914 " g();\n" 915 "} else\n" 916 " g();", 917 AllowsMergedIf); 918 verifyFormat("MYIF (a)\n" 919 " g();\n" 920 "else if (b) {\n" 921 " g();\n" 922 "} else\n" 923 " g();", 924 AllowsMergedIf); 925 verifyFormat("MYIF (a)\n" 926 " g();\n" 927 "else MYIF (b)\n" 928 " g();\n" 929 "else {\n" 930 " g();\n" 931 "}", 932 AllowsMergedIf); 933 verifyFormat("MYIF (a)\n" 934 " g();\n" 935 "else if (b)\n" 936 " g();\n" 937 "else {\n" 938 " g();\n" 939 "}", 940 AllowsMergedIf); 941 verifyFormat("MYIF (a)\n" 942 " g();\n" 943 "else MYIF (b) {\n" 944 " g();\n" 945 "} else {\n" 946 " g();\n" 947 "}", 948 AllowsMergedIf); 949 verifyFormat("MYIF (a)\n" 950 " g();\n" 951 "else if (b) {\n" 952 " g();\n" 953 "} else {\n" 954 " g();\n" 955 "}", 956 AllowsMergedIf); 957 verifyFormat("MYIF (a) {\n" 958 " g();\n" 959 "} else MYIF (b) {\n" 960 " g();\n" 961 "} else {\n" 962 " g();\n" 963 "}", 964 AllowsMergedIf); 965 verifyFormat("MYIF (a) {\n" 966 " g();\n" 967 "} else if (b) {\n" 968 " g();\n" 969 "} else {\n" 970 " g();\n" 971 "}", 972 AllowsMergedIf); 973 974 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 975 FormatStyle::SIS_OnlyFirstIf; 976 977 verifyFormat("if (a) f();\n" 978 "else {\n" 979 " g();\n" 980 "}", 981 AllowsMergedIf); 982 verifyFormat("if (a) f();\n" 983 "else {\n" 984 " if (a) f();\n" 985 " else {\n" 986 " g();\n" 987 " }\n" 988 " g();\n" 989 "}", 990 AllowsMergedIf); 991 992 verifyFormat("if (a) g();", AllowsMergedIf); 993 verifyFormat("if (a) {\n" 994 " g()\n" 995 "};", 996 AllowsMergedIf); 997 verifyFormat("if (a) g();\n" 998 "else\n" 999 " g();", 1000 AllowsMergedIf); 1001 verifyFormat("if (a) {\n" 1002 " g();\n" 1003 "} else\n" 1004 " g();", 1005 AllowsMergedIf); 1006 verifyFormat("if (a) g();\n" 1007 "else {\n" 1008 " g();\n" 1009 "}", 1010 AllowsMergedIf); 1011 verifyFormat("if (a) {\n" 1012 " g();\n" 1013 "} else {\n" 1014 " g();\n" 1015 "}", 1016 AllowsMergedIf); 1017 verifyFormat("if (a) g();\n" 1018 "else if (b)\n" 1019 " g();\n" 1020 "else\n" 1021 " g();", 1022 AllowsMergedIf); 1023 verifyFormat("if (a) {\n" 1024 " g();\n" 1025 "} else if (b)\n" 1026 " g();\n" 1027 "else\n" 1028 " g();", 1029 AllowsMergedIf); 1030 verifyFormat("if (a) g();\n" 1031 "else if (b) {\n" 1032 " g();\n" 1033 "} else\n" 1034 " g();", 1035 AllowsMergedIf); 1036 verifyFormat("if (a) g();\n" 1037 "else if (b)\n" 1038 " g();\n" 1039 "else {\n" 1040 " g();\n" 1041 "}", 1042 AllowsMergedIf); 1043 verifyFormat("if (a) g();\n" 1044 "else if (b) {\n" 1045 " g();\n" 1046 "} else {\n" 1047 " g();\n" 1048 "}", 1049 AllowsMergedIf); 1050 verifyFormat("if (a) {\n" 1051 " g();\n" 1052 "} else if (b) {\n" 1053 " g();\n" 1054 "} else {\n" 1055 " g();\n" 1056 "}", 1057 AllowsMergedIf); 1058 verifyFormat("MYIF (a) f();\n" 1059 "else {\n" 1060 " g();\n" 1061 "}", 1062 AllowsMergedIf); 1063 verifyFormat("MYIF (a) f();\n" 1064 "else {\n" 1065 " if (a) f();\n" 1066 " else {\n" 1067 " g();\n" 1068 " }\n" 1069 " g();\n" 1070 "}", 1071 AllowsMergedIf); 1072 1073 verifyFormat("MYIF (a) g();", AllowsMergedIf); 1074 verifyFormat("MYIF (a) {\n" 1075 " g()\n" 1076 "};", 1077 AllowsMergedIf); 1078 verifyFormat("MYIF (a) g();\n" 1079 "else\n" 1080 " g();", 1081 AllowsMergedIf); 1082 verifyFormat("MYIF (a) {\n" 1083 " g();\n" 1084 "} else\n" 1085 " g();", 1086 AllowsMergedIf); 1087 verifyFormat("MYIF (a) g();\n" 1088 "else {\n" 1089 " g();\n" 1090 "}", 1091 AllowsMergedIf); 1092 verifyFormat("MYIF (a) {\n" 1093 " g();\n" 1094 "} else {\n" 1095 " g();\n" 1096 "}", 1097 AllowsMergedIf); 1098 verifyFormat("MYIF (a) g();\n" 1099 "else MYIF (b)\n" 1100 " g();\n" 1101 "else\n" 1102 " g();", 1103 AllowsMergedIf); 1104 verifyFormat("MYIF (a) g();\n" 1105 "else if (b)\n" 1106 " g();\n" 1107 "else\n" 1108 " g();", 1109 AllowsMergedIf); 1110 verifyFormat("MYIF (a) {\n" 1111 " g();\n" 1112 "} else MYIF (b)\n" 1113 " g();\n" 1114 "else\n" 1115 " g();", 1116 AllowsMergedIf); 1117 verifyFormat("MYIF (a) {\n" 1118 " g();\n" 1119 "} else if (b)\n" 1120 " g();\n" 1121 "else\n" 1122 " g();", 1123 AllowsMergedIf); 1124 verifyFormat("MYIF (a) g();\n" 1125 "else MYIF (b) {\n" 1126 " g();\n" 1127 "} else\n" 1128 " g();", 1129 AllowsMergedIf); 1130 verifyFormat("MYIF (a) g();\n" 1131 "else if (b) {\n" 1132 " g();\n" 1133 "} else\n" 1134 " g();", 1135 AllowsMergedIf); 1136 verifyFormat("MYIF (a) g();\n" 1137 "else MYIF (b)\n" 1138 " g();\n" 1139 "else {\n" 1140 " g();\n" 1141 "}", 1142 AllowsMergedIf); 1143 verifyFormat("MYIF (a) g();\n" 1144 "else if (b)\n" 1145 " g();\n" 1146 "else {\n" 1147 " g();\n" 1148 "}", 1149 AllowsMergedIf); 1150 verifyFormat("MYIF (a) g();\n" 1151 "else MYIF (b) {\n" 1152 " g();\n" 1153 "} else {\n" 1154 " g();\n" 1155 "}", 1156 AllowsMergedIf); 1157 verifyFormat("MYIF (a) g();\n" 1158 "else if (b) {\n" 1159 " g();\n" 1160 "} else {\n" 1161 " g();\n" 1162 "}", 1163 AllowsMergedIf); 1164 verifyFormat("MYIF (a) {\n" 1165 " g();\n" 1166 "} else MYIF (b) {\n" 1167 " g();\n" 1168 "} else {\n" 1169 " g();\n" 1170 "}", 1171 AllowsMergedIf); 1172 verifyFormat("MYIF (a) {\n" 1173 " g();\n" 1174 "} else if (b) {\n" 1175 " g();\n" 1176 "} else {\n" 1177 " g();\n" 1178 "}", 1179 AllowsMergedIf); 1180 1181 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 1182 FormatStyle::SIS_AllIfsAndElse; 1183 1184 verifyFormat("if (a) f();\n" 1185 "else {\n" 1186 " g();\n" 1187 "}", 1188 AllowsMergedIf); 1189 verifyFormat("if (a) f();\n" 1190 "else {\n" 1191 " if (a) f();\n" 1192 " else {\n" 1193 " g();\n" 1194 " }\n" 1195 " g();\n" 1196 "}", 1197 AllowsMergedIf); 1198 1199 verifyFormat("if (a) g();", AllowsMergedIf); 1200 verifyFormat("if (a) {\n" 1201 " g()\n" 1202 "};", 1203 AllowsMergedIf); 1204 verifyFormat("if (a) g();\n" 1205 "else g();", 1206 AllowsMergedIf); 1207 verifyFormat("if (a) {\n" 1208 " g();\n" 1209 "} else g();", 1210 AllowsMergedIf); 1211 verifyFormat("if (a) g();\n" 1212 "else {\n" 1213 " g();\n" 1214 "}", 1215 AllowsMergedIf); 1216 verifyFormat("if (a) {\n" 1217 " g();\n" 1218 "} else {\n" 1219 " g();\n" 1220 "}", 1221 AllowsMergedIf); 1222 verifyFormat("if (a) g();\n" 1223 "else if (b) g();\n" 1224 "else g();", 1225 AllowsMergedIf); 1226 verifyFormat("if (a) {\n" 1227 " g();\n" 1228 "} else if (b) g();\n" 1229 "else g();", 1230 AllowsMergedIf); 1231 verifyFormat("if (a) g();\n" 1232 "else if (b) {\n" 1233 " g();\n" 1234 "} else g();", 1235 AllowsMergedIf); 1236 verifyFormat("if (a) g();\n" 1237 "else if (b) g();\n" 1238 "else {\n" 1239 " g();\n" 1240 "}", 1241 AllowsMergedIf); 1242 verifyFormat("if (a) g();\n" 1243 "else if (b) {\n" 1244 " g();\n" 1245 "} else {\n" 1246 " g();\n" 1247 "}", 1248 AllowsMergedIf); 1249 verifyFormat("if (a) {\n" 1250 " g();\n" 1251 "} else if (b) {\n" 1252 " g();\n" 1253 "} else {\n" 1254 " g();\n" 1255 "}", 1256 AllowsMergedIf); 1257 verifyFormat("MYIF (a) f();\n" 1258 "else {\n" 1259 " g();\n" 1260 "}", 1261 AllowsMergedIf); 1262 verifyFormat("MYIF (a) f();\n" 1263 "else {\n" 1264 " if (a) f();\n" 1265 " else {\n" 1266 " g();\n" 1267 " }\n" 1268 " g();\n" 1269 "}", 1270 AllowsMergedIf); 1271 1272 verifyFormat("MYIF (a) g();", AllowsMergedIf); 1273 verifyFormat("MYIF (a) {\n" 1274 " g()\n" 1275 "};", 1276 AllowsMergedIf); 1277 verifyFormat("MYIF (a) g();\n" 1278 "else g();", 1279 AllowsMergedIf); 1280 verifyFormat("MYIF (a) {\n" 1281 " g();\n" 1282 "} else g();", 1283 AllowsMergedIf); 1284 verifyFormat("MYIF (a) g();\n" 1285 "else {\n" 1286 " g();\n" 1287 "}", 1288 AllowsMergedIf); 1289 verifyFormat("MYIF (a) {\n" 1290 " g();\n" 1291 "} else {\n" 1292 " g();\n" 1293 "}", 1294 AllowsMergedIf); 1295 verifyFormat("MYIF (a) g();\n" 1296 "else MYIF (b) g();\n" 1297 "else g();", 1298 AllowsMergedIf); 1299 verifyFormat("MYIF (a) g();\n" 1300 "else if (b) g();\n" 1301 "else g();", 1302 AllowsMergedIf); 1303 verifyFormat("MYIF (a) {\n" 1304 " g();\n" 1305 "} else MYIF (b) g();\n" 1306 "else g();", 1307 AllowsMergedIf); 1308 verifyFormat("MYIF (a) {\n" 1309 " g();\n" 1310 "} else if (b) g();\n" 1311 "else g();", 1312 AllowsMergedIf); 1313 verifyFormat("MYIF (a) g();\n" 1314 "else MYIF (b) {\n" 1315 " g();\n" 1316 "} else g();", 1317 AllowsMergedIf); 1318 verifyFormat("MYIF (a) g();\n" 1319 "else if (b) {\n" 1320 " g();\n" 1321 "} else g();", 1322 AllowsMergedIf); 1323 verifyFormat("MYIF (a) g();\n" 1324 "else MYIF (b) g();\n" 1325 "else {\n" 1326 " g();\n" 1327 "}", 1328 AllowsMergedIf); 1329 verifyFormat("MYIF (a) g();\n" 1330 "else if (b) g();\n" 1331 "else {\n" 1332 " g();\n" 1333 "}", 1334 AllowsMergedIf); 1335 verifyFormat("MYIF (a) g();\n" 1336 "else MYIF (b) {\n" 1337 " g();\n" 1338 "} else {\n" 1339 " g();\n" 1340 "}", 1341 AllowsMergedIf); 1342 verifyFormat("MYIF (a) g();\n" 1343 "else if (b) {\n" 1344 " g();\n" 1345 "} else {\n" 1346 " g();\n" 1347 "}", 1348 AllowsMergedIf); 1349 verifyFormat("MYIF (a) {\n" 1350 " g();\n" 1351 "} else MYIF (b) {\n" 1352 " g();\n" 1353 "} else {\n" 1354 " g();\n" 1355 "}", 1356 AllowsMergedIf); 1357 verifyFormat("MYIF (a) {\n" 1358 " g();\n" 1359 "} else if (b) {\n" 1360 " g();\n" 1361 "} else {\n" 1362 " g();\n" 1363 "}", 1364 AllowsMergedIf); 1365 } 1366 1367 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) { 1368 verifyFormat("while (true)\n" 1369 " ;"); 1370 verifyFormat("for (;;)\n" 1371 " ;"); 1372 1373 FormatStyle AllowsMergedLoops = getLLVMStyle(); 1374 AllowsMergedLoops.AllowShortLoopsOnASingleLine = true; 1375 1376 verifyFormat("while (true) continue;", AllowsMergedLoops); 1377 verifyFormat("for (;;) continue;", AllowsMergedLoops); 1378 verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops); 1379 verifyFormat("BOOST_FOREACH (int &v, vec) v *= 2;", AllowsMergedLoops); 1380 verifyFormat("while (true);", AllowsMergedLoops); 1381 verifyFormat("for (;;);", AllowsMergedLoops); 1382 verifyFormat("for (;;)\n" 1383 " for (;;) continue;", 1384 AllowsMergedLoops); 1385 verifyFormat("for (;;)\n" 1386 " while (true) continue;", 1387 AllowsMergedLoops); 1388 verifyFormat("while (true)\n" 1389 " for (;;) continue;", 1390 AllowsMergedLoops); 1391 verifyFormat("BOOST_FOREACH (int &v, vec)\n" 1392 " for (;;) continue;", 1393 AllowsMergedLoops); 1394 verifyFormat("for (;;)\n" 1395 " BOOST_FOREACH (int &v, vec) continue;", 1396 AllowsMergedLoops); 1397 verifyFormat("for (;;) // Can't merge this\n" 1398 " continue;", 1399 AllowsMergedLoops); 1400 verifyFormat("for (;;) /* still don't merge */\n" 1401 " continue;", 1402 AllowsMergedLoops); 1403 verifyFormat("do a++;\n" 1404 "while (true);", 1405 AllowsMergedLoops); 1406 verifyFormat("do /* Don't merge */\n" 1407 " a++;\n" 1408 "while (true);", 1409 AllowsMergedLoops); 1410 verifyFormat("do // Don't merge\n" 1411 " a++;\n" 1412 "while (true);", 1413 AllowsMergedLoops); 1414 verifyFormat("do\n" 1415 " // Don't merge\n" 1416 " a++;\n" 1417 "while (true);", 1418 AllowsMergedLoops); 1419 1420 // Without braces labels are interpreted differently. 1421 verifyFormat("{\n" 1422 " do\n" 1423 " label:\n" 1424 " a++;\n" 1425 " while (true);\n" 1426 "}", 1427 AllowsMergedLoops); 1428 1429 // Don't merge if there are comments before the null statement. 1430 verifyFormat("while (1) //\n" 1431 " ;", 1432 AllowsMergedLoops); 1433 verifyFormat("for (;;) /**/\n" 1434 " ;", 1435 AllowsMergedLoops); 1436 verifyFormat("while (true) /**/\n" 1437 " ;", 1438 "while (true) /**/;", AllowsMergedLoops); 1439 } 1440 1441 TEST_F(FormatTest, FormatShortBracedStatements) { 1442 FormatStyle AllowSimpleBracedStatements = getLLVMStyle(); 1443 EXPECT_EQ(AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine, false); 1444 EXPECT_EQ(AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine, 1445 FormatStyle::SIS_Never); 1446 EXPECT_EQ(AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine, false); 1447 EXPECT_EQ(AllowSimpleBracedStatements.BraceWrapping.AfterFunction, false); 1448 verifyFormat("for (;;) {\n" 1449 " f();\n" 1450 "}"); 1451 verifyFormat("/*comment*/ for (;;) {\n" 1452 " f();\n" 1453 "}"); 1454 verifyFormat("BOOST_FOREACH (int v, vec) {\n" 1455 " f();\n" 1456 "}"); 1457 verifyFormat("/*comment*/ BOOST_FOREACH (int v, vec) {\n" 1458 " f();\n" 1459 "}"); 1460 verifyFormat("while (true) {\n" 1461 " f();\n" 1462 "}"); 1463 verifyFormat("/*comment*/ while (true) {\n" 1464 " f();\n" 1465 "}"); 1466 verifyFormat("if (true) {\n" 1467 " f();\n" 1468 "}"); 1469 verifyFormat("/*comment*/ if (true) {\n" 1470 " f();\n" 1471 "}"); 1472 1473 AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = 1474 FormatStyle::SBS_Empty; 1475 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 1476 FormatStyle::SIS_WithoutElse; 1477 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 1478 verifyFormat("if (i) break;", AllowSimpleBracedStatements); 1479 verifyFormat("if (i > 0) {\n" 1480 " return i;\n" 1481 "}", 1482 AllowSimpleBracedStatements); 1483 1484 AllowSimpleBracedStatements.IfMacros.push_back("MYIF"); 1485 // Where line-lengths matter, a 2-letter synonym that maintains line length. 1486 // Not IF to avoid any confusion that IF is somehow special. 1487 AllowSimpleBracedStatements.IfMacros.push_back("FI"); 1488 AllowSimpleBracedStatements.ColumnLimit = 40; 1489 AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = 1490 FormatStyle::SBS_Always; 1491 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 1492 AllowSimpleBracedStatements.BreakBeforeBraces = FormatStyle::BS_Custom; 1493 AllowSimpleBracedStatements.BraceWrapping.AfterFunction = true; 1494 AllowSimpleBracedStatements.BraceWrapping.SplitEmptyRecord = false; 1495 1496 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 1497 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements); 1498 verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements); 1499 verifyFormat("if consteval {}", AllowSimpleBracedStatements); 1500 verifyFormat("if !consteval {}", AllowSimpleBracedStatements); 1501 verifyFormat("if CONSTEVAL {}", AllowSimpleBracedStatements); 1502 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements); 1503 verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements); 1504 verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements); 1505 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 1506 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 1507 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 1508 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements); 1509 verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); 1510 verifyFormat("if consteval { f(); }", AllowSimpleBracedStatements); 1511 verifyFormat("if CONSTEVAL { f(); }", AllowSimpleBracedStatements); 1512 verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements); 1513 verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements); 1514 verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); 1515 verifyFormat("MYIF consteval { f(); }", AllowSimpleBracedStatements); 1516 verifyFormat("MYIF CONSTEVAL { f(); }", AllowSimpleBracedStatements); 1517 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 1518 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 1519 verifyFormat("if (true) { fffffffffffffffffffffff(); }", 1520 AllowSimpleBracedStatements); 1521 verifyFormat("if (true) {\n" 1522 " ffffffffffffffffffffffff();\n" 1523 "}", 1524 AllowSimpleBracedStatements); 1525 verifyFormat("if (true) {\n" 1526 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 1527 "}", 1528 AllowSimpleBracedStatements); 1529 verifyFormat("if (true) { //\n" 1530 " f();\n" 1531 "}", 1532 AllowSimpleBracedStatements); 1533 verifyFormat("if (true) {\n" 1534 " f();\n" 1535 " f();\n" 1536 "}", 1537 AllowSimpleBracedStatements); 1538 verifyFormat("if (true) {\n" 1539 " f();\n" 1540 "} else {\n" 1541 " f();\n" 1542 "}", 1543 AllowSimpleBracedStatements); 1544 verifyFormat("FI (true) { fffffffffffffffffffffff(); }", 1545 AllowSimpleBracedStatements); 1546 verifyFormat("MYIF (true) {\n" 1547 " ffffffffffffffffffffffff();\n" 1548 "}", 1549 AllowSimpleBracedStatements); 1550 verifyFormat("MYIF (true) {\n" 1551 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 1552 "}", 1553 AllowSimpleBracedStatements); 1554 verifyFormat("MYIF (true) { //\n" 1555 " f();\n" 1556 "}", 1557 AllowSimpleBracedStatements); 1558 verifyFormat("MYIF (true) {\n" 1559 " f();\n" 1560 " f();\n" 1561 "}", 1562 AllowSimpleBracedStatements); 1563 verifyFormat("MYIF (true) {\n" 1564 " f();\n" 1565 "} else {\n" 1566 " f();\n" 1567 "}", 1568 AllowSimpleBracedStatements); 1569 1570 verifyFormat("struct A2 {\n" 1571 " int X;\n" 1572 "};", 1573 AllowSimpleBracedStatements); 1574 verifyFormat("typedef struct A2 {\n" 1575 " int X;\n" 1576 "} A2_t;", 1577 AllowSimpleBracedStatements); 1578 verifyFormat("template <int> struct A2 {\n" 1579 " struct B {};\n" 1580 "};", 1581 AllowSimpleBracedStatements); 1582 1583 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 1584 FormatStyle::SIS_Never; 1585 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 1586 verifyFormat("if (true) {\n" 1587 " f();\n" 1588 "}", 1589 AllowSimpleBracedStatements); 1590 verifyFormat("if (true) {\n" 1591 " f();\n" 1592 "} else {\n" 1593 " f();\n" 1594 "}", 1595 AllowSimpleBracedStatements); 1596 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements); 1597 verifyFormat("MYIF (true) {\n" 1598 " f();\n" 1599 "}", 1600 AllowSimpleBracedStatements); 1601 verifyFormat("MYIF (true) {\n" 1602 " f();\n" 1603 "} else {\n" 1604 " f();\n" 1605 "}", 1606 AllowSimpleBracedStatements); 1607 1608 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 1609 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 1610 verifyFormat("while (true) {\n" 1611 " f();\n" 1612 "}", 1613 AllowSimpleBracedStatements); 1614 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 1615 verifyFormat("for (;;) {\n" 1616 " f();\n" 1617 "}", 1618 AllowSimpleBracedStatements); 1619 verifyFormat("BOOST_FOREACH (int v, vec) {}", AllowSimpleBracedStatements); 1620 verifyFormat("BOOST_FOREACH (int v, vec) {\n" 1621 " f();\n" 1622 "}", 1623 AllowSimpleBracedStatements); 1624 1625 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 1626 FormatStyle::SIS_WithoutElse; 1627 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 1628 AllowSimpleBracedStatements.BraceWrapping.AfterControlStatement = 1629 FormatStyle::BWACS_Always; 1630 1631 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 1632 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements); 1633 verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements); 1634 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements); 1635 verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements); 1636 verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements); 1637 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 1638 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 1639 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 1640 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements); 1641 verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); 1642 verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements); 1643 verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements); 1644 verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); 1645 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 1646 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 1647 verifyFormat("if (true) { fffffffffffffffffffffff(); }", 1648 AllowSimpleBracedStatements); 1649 verifyFormat("if (true)\n" 1650 "{\n" 1651 " ffffffffffffffffffffffff();\n" 1652 "}", 1653 AllowSimpleBracedStatements); 1654 verifyFormat("if (true)\n" 1655 "{\n" 1656 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 1657 "}", 1658 AllowSimpleBracedStatements); 1659 verifyFormat("if (true)\n" 1660 "{ //\n" 1661 " f();\n" 1662 "}", 1663 AllowSimpleBracedStatements); 1664 verifyFormat("if (true)\n" 1665 "{\n" 1666 " f();\n" 1667 " f();\n" 1668 "}", 1669 AllowSimpleBracedStatements); 1670 verifyFormat("if (true)\n" 1671 "{\n" 1672 " f();\n" 1673 "} else\n" 1674 "{\n" 1675 " f();\n" 1676 "}", 1677 AllowSimpleBracedStatements); 1678 verifyFormat("FI (true) { fffffffffffffffffffffff(); }", 1679 AllowSimpleBracedStatements); 1680 verifyFormat("MYIF (true)\n" 1681 "{\n" 1682 " ffffffffffffffffffffffff();\n" 1683 "}", 1684 AllowSimpleBracedStatements); 1685 verifyFormat("MYIF (true)\n" 1686 "{\n" 1687 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 1688 "}", 1689 AllowSimpleBracedStatements); 1690 verifyFormat("MYIF (true)\n" 1691 "{ //\n" 1692 " f();\n" 1693 "}", 1694 AllowSimpleBracedStatements); 1695 verifyFormat("MYIF (true)\n" 1696 "{\n" 1697 " f();\n" 1698 " f();\n" 1699 "}", 1700 AllowSimpleBracedStatements); 1701 verifyFormat("MYIF (true)\n" 1702 "{\n" 1703 " f();\n" 1704 "} else\n" 1705 "{\n" 1706 " f();\n" 1707 "}", 1708 AllowSimpleBracedStatements); 1709 1710 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 1711 FormatStyle::SIS_Never; 1712 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 1713 verifyFormat("if (true)\n" 1714 "{\n" 1715 " f();\n" 1716 "}", 1717 AllowSimpleBracedStatements); 1718 verifyFormat("if (true)\n" 1719 "{\n" 1720 " f();\n" 1721 "} else\n" 1722 "{\n" 1723 " f();\n" 1724 "}", 1725 AllowSimpleBracedStatements); 1726 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements); 1727 verifyFormat("MYIF (true)\n" 1728 "{\n" 1729 " f();\n" 1730 "}", 1731 AllowSimpleBracedStatements); 1732 verifyFormat("MYIF (true)\n" 1733 "{\n" 1734 " f();\n" 1735 "} else\n" 1736 "{\n" 1737 " f();\n" 1738 "}", 1739 AllowSimpleBracedStatements); 1740 1741 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 1742 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 1743 verifyFormat("while (true)\n" 1744 "{\n" 1745 " f();\n" 1746 "}", 1747 AllowSimpleBracedStatements); 1748 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 1749 verifyFormat("for (;;)\n" 1750 "{\n" 1751 " f();\n" 1752 "}", 1753 AllowSimpleBracedStatements); 1754 verifyFormat("BOOST_FOREACH (int v, vec) {}", AllowSimpleBracedStatements); 1755 verifyFormat("BOOST_FOREACH (int v, vec)\n" 1756 "{\n" 1757 " f();\n" 1758 "}", 1759 AllowSimpleBracedStatements); 1760 1761 FormatStyle Style = getLLVMStyle(); 1762 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 1763 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 1764 1765 verifyFormat("while (i > 0)\n" 1766 "{\n" 1767 " --i;\n" 1768 "}", 1769 Style); 1770 1771 verifyFormat("if (a)\n" 1772 "{\n" 1773 " ++b;\n" 1774 "}", 1775 Style); 1776 1777 verifyFormat("if (a)\n" 1778 "{\n" 1779 " b = 1;\n" 1780 "} else\n" 1781 "{\n" 1782 " b = 0;\n" 1783 "}", 1784 Style); 1785 1786 verifyFormat("if (a)\n" 1787 "{\n" 1788 " b = 1;\n" 1789 "} else if (c)\n" 1790 "{\n" 1791 " b = 2;\n" 1792 "} else\n" 1793 "{\n" 1794 " b = 0;\n" 1795 "}", 1796 Style); 1797 1798 Style.BraceWrapping.BeforeElse = true; 1799 1800 verifyFormat("if (a)\n" 1801 "{\n" 1802 " b = 1;\n" 1803 "}\n" 1804 "else\n" 1805 "{\n" 1806 " b = 0;\n" 1807 "}", 1808 Style); 1809 1810 verifyFormat("if (a)\n" 1811 "{\n" 1812 " b = 1;\n" 1813 "}\n" 1814 "else if (c)\n" 1815 "{\n" 1816 " b = 2;\n" 1817 "}\n" 1818 "else\n" 1819 "{\n" 1820 " b = 0;\n" 1821 "}", 1822 Style); 1823 } 1824 1825 TEST_F(FormatTest, UnderstandsMacros) { 1826 verifyFormat("#define A (parentheses)"); 1827 verifyFormat("/* comment */ #define A (parentheses)"); 1828 verifyFormat("/* comment */ /* another comment */ #define A (parentheses)"); 1829 // Even the partial code should never be merged. 1830 verifyNoChange("/* comment */ #define A (parentheses)\n" 1831 "#"); 1832 verifyFormat("/* comment */ #define A (parentheses)\n" 1833 "#\n"); 1834 verifyFormat("/* comment */ #define A (parentheses)\n" 1835 "#define B (parentheses)"); 1836 verifyFormat("#define true ((int)1)"); 1837 verifyFormat("#define and(x)"); 1838 verifyFormat("#define if(x) x"); 1839 verifyFormat("#define return(x) (x)"); 1840 verifyFormat("#define while(x) for (; x;)"); 1841 verifyFormat("#define xor(x) (^(x))"); 1842 verifyFormat("#define __except(x)"); 1843 verifyFormat("#define __try(x)"); 1844 1845 // https://llvm.org/PR54348. 1846 verifyFormat( 1847 "#define A" 1848 " " 1849 "\\\n" 1850 " class & {}"); 1851 1852 FormatStyle Style = getLLVMStyle(); 1853 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 1854 Style.BraceWrapping.AfterFunction = true; 1855 // Test that a macro definition never gets merged with the following 1856 // definition. 1857 // FIXME: The AAA macro definition probably should not be split into 3 lines. 1858 verifyFormat("#define AAA " 1859 " \\\n" 1860 " N " 1861 " \\\n" 1862 " {\n" 1863 "#define BBB }", 1864 Style); 1865 // verifyFormat("#define AAA N { //", Style); 1866 1867 verifyFormat("MACRO(return)"); 1868 verifyFormat("MACRO(co_await)"); 1869 verifyFormat("MACRO(co_return)"); 1870 verifyFormat("MACRO(co_yield)"); 1871 verifyFormat("MACRO(return, something)"); 1872 verifyFormat("MACRO(co_return, something)"); 1873 verifyFormat("MACRO(something##something)"); 1874 verifyFormat("MACRO(return##something)"); 1875 verifyFormat("MACRO(co_return##something)"); 1876 1877 verifyFormat("#define A x:"); 1878 1879 verifyFormat("#define Foo(Bar) {#Bar}", "#define Foo(Bar) \\\n" 1880 " { \\\n" 1881 " #Bar \\\n" 1882 " }"); 1883 verifyFormat("#define Foo(Bar) {#Bar}", "#define Foo(Bar) \\\n" 1884 " { #Bar }"); 1885 } 1886 1887 TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) { 1888 FormatStyle Style = getLLVMStyleWithColumns(60); 1889 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 1890 Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse; 1891 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 1892 verifyFormat("#define A \\\n" 1893 " if (HANDLEwernufrnuLwrmviferuvnierv) \\\n" 1894 " { \\\n" 1895 " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n" 1896 " }\n" 1897 "X;", 1898 "#define A \\\n" 1899 " if (HANDLEwernufrnuLwrmviferuvnierv) { \\\n" 1900 " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n" 1901 " }\n" 1902 "X;", 1903 Style); 1904 } 1905 1906 TEST_F(FormatTest, ParseIfElse) { 1907 verifyFormat("if (true)\n" 1908 " if (true)\n" 1909 " if (true)\n" 1910 " f();\n" 1911 " else\n" 1912 " g();\n" 1913 " else\n" 1914 " h();\n" 1915 "else\n" 1916 " i();"); 1917 verifyFormat("if (true)\n" 1918 " if (true)\n" 1919 " if (true) {\n" 1920 " if (true)\n" 1921 " f();\n" 1922 " } else {\n" 1923 " g();\n" 1924 " }\n" 1925 " else\n" 1926 " h();\n" 1927 "else {\n" 1928 " i();\n" 1929 "}"); 1930 verifyFormat("if (true)\n" 1931 " if constexpr (true)\n" 1932 " if (true) {\n" 1933 " if constexpr (true)\n" 1934 " f();\n" 1935 " } else {\n" 1936 " g();\n" 1937 " }\n" 1938 " else\n" 1939 " h();\n" 1940 "else {\n" 1941 " i();\n" 1942 "}"); 1943 verifyFormat("if (true)\n" 1944 " if CONSTEXPR (true)\n" 1945 " if (true) {\n" 1946 " if CONSTEXPR (true)\n" 1947 " f();\n" 1948 " } else {\n" 1949 " g();\n" 1950 " }\n" 1951 " else\n" 1952 " h();\n" 1953 "else {\n" 1954 " i();\n" 1955 "}"); 1956 verifyFormat("void f() {\n" 1957 " if (a) {\n" 1958 " } else {\n" 1959 " }\n" 1960 "}"); 1961 } 1962 1963 TEST_F(FormatTest, ElseIf) { 1964 verifyFormat("if (a) {\n} else if (b) {\n}"); 1965 verifyFormat("if (a)\n" 1966 " f();\n" 1967 "else if (b)\n" 1968 " g();\n" 1969 "else\n" 1970 " h();"); 1971 verifyFormat("if (a)\n" 1972 " f();\n" 1973 "else // comment\n" 1974 " if (b) {\n" 1975 " g();\n" 1976 " h();\n" 1977 " }"); 1978 verifyFormat("if constexpr (a)\n" 1979 " f();\n" 1980 "else if constexpr (b)\n" 1981 " g();\n" 1982 "else\n" 1983 " h();"); 1984 verifyFormat("if CONSTEXPR (a)\n" 1985 " f();\n" 1986 "else if CONSTEXPR (b)\n" 1987 " g();\n" 1988 "else\n" 1989 " h();"); 1990 verifyFormat("if (a) {\n" 1991 " f();\n" 1992 "}\n" 1993 "// or else ..\n" 1994 "else {\n" 1995 " g()\n" 1996 "}"); 1997 1998 verifyFormat("if (a) {\n" 1999 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2000 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 2001 "}"); 2002 verifyFormat("if (a) {\n" 2003 "} else if constexpr (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2004 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 2005 "}"); 2006 verifyFormat("if (a) {\n" 2007 "} else if CONSTEXPR (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2008 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 2009 "}"); 2010 verifyFormat("if (a) {\n" 2011 "} else if (\n" 2012 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 2013 "}", 2014 getLLVMStyleWithColumns(62)); 2015 verifyFormat("if (a) {\n" 2016 "} else if constexpr (\n" 2017 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 2018 "}", 2019 getLLVMStyleWithColumns(62)); 2020 verifyFormat("if (a) {\n" 2021 "} else if CONSTEXPR (\n" 2022 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 2023 "}", 2024 getLLVMStyleWithColumns(62)); 2025 } 2026 2027 TEST_F(FormatTest, SeparatePointerReferenceAlignment) { 2028 FormatStyle Style = getLLVMStyle(); 2029 EXPECT_EQ(Style.PointerAlignment, FormatStyle::PAS_Right); 2030 EXPECT_EQ(Style.ReferenceAlignment, FormatStyle::RAS_Pointer); 2031 verifyFormat("int *f1(int *a, int &b, int &&c);", Style); 2032 verifyFormat("int &f2(int &&c, int *a, int &b);", Style); 2033 verifyFormat("int &&f3(int &b, int &&c, int *a);", Style); 2034 verifyFormat("int *f1(int &a) const &;", Style); 2035 verifyFormat("int *f1(int &a) const & = 0;", Style); 2036 verifyFormat("int *a = f1();", Style); 2037 verifyFormat("int &b = f2();", Style); 2038 verifyFormat("int &&c = f3();", Style); 2039 verifyFormat("int f3() { return sizeof(Foo &); }", Style); 2040 verifyFormat("int f4() { return sizeof(Foo &&); }", Style); 2041 verifyFormat("void f5() { int f6(Foo &, Bar &); }", Style); 2042 verifyFormat("void f5() { int f6(Foo &&, Bar &&); }", Style); 2043 verifyFormat("for (auto a = 0, b = 0; const auto &c : {1, 2, 3})", Style); 2044 verifyFormat("for (auto a = 0, b = 0; const int &c : {1, 2, 3})", Style); 2045 verifyFormat("for (auto a = 0, b = 0; const Foo &c : {1, 2, 3})", Style); 2046 verifyFormat("for (auto a = 0, b = 0; const Foo *c : {1, 2, 3})", Style); 2047 verifyFormat("for (int a = 0, b = 0; const auto &c : {1, 2, 3})", Style); 2048 verifyFormat("for (int a = 0, b = 0; const int &c : {1, 2, 3})", Style); 2049 verifyFormat("for (int a = 0, b = 0; const Foo &c : {1, 2, 3})", Style); 2050 verifyFormat("for (int a = 0, b++; const auto &c : {1, 2, 3})", Style); 2051 verifyFormat("for (int a = 0, b++; const int &c : {1, 2, 3})", Style); 2052 verifyFormat("for (int a = 0, b++; const Foo &c : {1, 2, 3})", Style); 2053 verifyFormat("for (auto x = 0; auto &c : {1, 2, 3})", Style); 2054 verifyFormat("for (auto x = 0; int &c : {1, 2, 3})", Style); 2055 verifyFormat("for (int x = 0; auto &c : {1, 2, 3})", Style); 2056 verifyFormat("for (int x = 0; int &c : {1, 2, 3})", Style); 2057 verifyFormat("for (f(); auto &c : {1, 2, 3})", Style); 2058 verifyFormat("for (f(); int &c : {1, 2, 3})", Style); 2059 verifyFormat( 2060 "function<int(int &)> res1 = [](int &a) { return 0000000000000; },\n" 2061 " res2 = [](int &a) { return 0000000000000; };", 2062 Style); 2063 2064 Style.AlignConsecutiveDeclarations.Enabled = true; 2065 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = true; 2066 verifyFormat("Const unsigned int *c;\n" 2067 "const unsigned int *d;\n" 2068 "Const unsigned int &e;\n" 2069 "const unsigned int &f;\n" 2070 "int *f1(int *a, int &b, int &&c);\n" 2071 "double *(*f2)(int *a, double &&b);\n" 2072 "const unsigned &&g;\n" 2073 "Const unsigned h;", 2074 Style); 2075 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = false; 2076 verifyFormat("Const unsigned int *c;\n" 2077 "const unsigned int *d;\n" 2078 "Const unsigned int &e;\n" 2079 "const unsigned int &f;\n" 2080 "int *f1(int *a, int &b, int &&c);\n" 2081 "double *(*f2)(int *a, double &&b);\n" 2082 "const unsigned &&g;\n" 2083 "Const unsigned h;", 2084 Style); 2085 2086 Style.PointerAlignment = FormatStyle::PAS_Left; 2087 Style.ReferenceAlignment = FormatStyle::RAS_Pointer; 2088 verifyFormat("int* f1(int* a, int& b, int&& c);", Style); 2089 verifyFormat("int& f2(int&& c, int* a, int& b);", Style); 2090 verifyFormat("int&& f3(int& b, int&& c, int* a);", Style); 2091 verifyFormat("int* f1(int& a) const& = 0;", Style); 2092 verifyFormat("int* a = f1();", Style); 2093 verifyFormat("int& b = f2();", Style); 2094 verifyFormat("int&& c = f3();", Style); 2095 verifyFormat("int f3() { return sizeof(Foo&); }", Style); 2096 verifyFormat("int f4() { return sizeof(Foo&&); }", Style); 2097 verifyFormat("void f5() { int f6(Foo&, Bar&); }", Style); 2098 verifyFormat("void f5() { int f6(Foo&&, Bar&&); }", Style); 2099 verifyFormat("for (auto a = 0, b = 0; const auto& c : {1, 2, 3})", Style); 2100 verifyFormat("for (auto a = 0, b = 0; const int& c : {1, 2, 3})", Style); 2101 verifyFormat("for (auto a = 0, b = 0; const Foo& c : {1, 2, 3})", Style); 2102 verifyFormat("for (auto a = 0, b = 0; const Foo* c : {1, 2, 3})", Style); 2103 verifyFormat("for (int a = 0, b = 0; const auto& c : {1, 2, 3})", Style); 2104 verifyFormat("for (int a = 0, b = 0; const int& c : {1, 2, 3})", Style); 2105 verifyFormat("for (int a = 0, b = 0; const Foo& c : {1, 2, 3})", Style); 2106 verifyFormat("for (int a = 0, b = 0; const Foo* c : {1, 2, 3})", Style); 2107 verifyFormat("for (int a = 0, b++; const auto& c : {1, 2, 3})", Style); 2108 verifyFormat("for (int a = 0, b++; const int& c : {1, 2, 3})", Style); 2109 verifyFormat("for (int a = 0, b++; const Foo& c : {1, 2, 3})", Style); 2110 verifyFormat("for (int a = 0, b++; const Foo* c : {1, 2, 3})", Style); 2111 verifyFormat("for (auto x = 0; auto& c : {1, 2, 3})", Style); 2112 verifyFormat("for (auto x = 0; int& c : {1, 2, 3})", Style); 2113 verifyFormat("for (int x = 0; auto& c : {1, 2, 3})", Style); 2114 verifyFormat("for (int x = 0; int& c : {1, 2, 3})", Style); 2115 verifyFormat("for (f(); auto& c : {1, 2, 3})", Style); 2116 verifyFormat("for (f(); int& c : {1, 2, 3})", Style); 2117 verifyFormat( 2118 "function<int(int&)> res1 = [](int& a) { return 0000000000000; },\n" 2119 " res2 = [](int& a) { return 0000000000000; };", 2120 Style); 2121 2122 Style.AlignConsecutiveDeclarations.Enabled = true; 2123 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = true; 2124 verifyFormat("Const unsigned int* c;\n" 2125 "const unsigned int* d;\n" 2126 "Const unsigned int& e;\n" 2127 "const unsigned int& f;\n" 2128 "int* f1(int* a, int& b, int&& c);\n" 2129 "double* (*f2)(int* a, double&& b);\n" 2130 "const unsigned&& g;\n" 2131 "Const unsigned h;", 2132 Style); 2133 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = false; 2134 verifyFormat("Const unsigned int* c;\n" 2135 "const unsigned int* d;\n" 2136 "Const unsigned int& e;\n" 2137 "const unsigned int& f;\n" 2138 "int* f1(int* a, int& b, int&& c);\n" 2139 "double* (*f2)(int* a, double&& b);\n" 2140 "const unsigned&& g;\n" 2141 "Const unsigned h;", 2142 Style); 2143 2144 Style.PointerAlignment = FormatStyle::PAS_Right; 2145 Style.ReferenceAlignment = FormatStyle::RAS_Left; 2146 verifyFormat("int *f1(int *a, int& b, int&& c);", Style); 2147 verifyFormat("int& f2(int&& c, int *a, int& b);", Style); 2148 verifyFormat("int&& f3(int& b, int&& c, int *a);", Style); 2149 verifyFormat("int *a = f1();", Style); 2150 verifyFormat("int& b = f2();", Style); 2151 verifyFormat("int&& c = f3();", Style); 2152 verifyFormat("int f3() { return sizeof(Foo&); }", Style); 2153 verifyFormat("int f4() { return sizeof(Foo&&); }", Style); 2154 verifyFormat("void f5() { int f6(Foo&, Bar&); }", Style); 2155 verifyFormat("void f5() { int f6(Foo&&, Bar&&); }", Style); 2156 verifyFormat("for (auto a = 0, b = 0; const Foo *c : {1, 2, 3})", Style); 2157 verifyFormat("for (int a = 0, b = 0; const Foo *c : {1, 2, 3})", Style); 2158 verifyFormat("for (int a = 0, b++; const Foo *c : {1, 2, 3})", Style); 2159 2160 Style.AlignConsecutiveDeclarations.Enabled = true; 2161 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = true; 2162 verifyFormat("Const unsigned int *c;\n" 2163 "const unsigned int *d;\n" 2164 "Const unsigned int& e;\n" 2165 "const unsigned int& f;\n" 2166 "int *f1(int *a, int& b, int&& c);\n" 2167 "double *(*f2)(int *a, double&& b);\n" 2168 "const unsigned&& g;\n" 2169 "Const unsigned h;", 2170 Style); 2171 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = false; 2172 verifyFormat("Const unsigned int *c;\n" 2173 "const unsigned int *d;\n" 2174 "Const unsigned int& e;\n" 2175 "const unsigned int& f;\n" 2176 "int *f1(int *a, int& b, int&& c);\n" 2177 "double *(*f2)(int *a, double&& b);\n" 2178 "const unsigned&& g;\n" 2179 "Const unsigned h;", 2180 Style); 2181 2182 Style.PointerAlignment = FormatStyle::PAS_Left; 2183 Style.ReferenceAlignment = FormatStyle::RAS_Middle; 2184 verifyFormat("int* f1(int* a, int & b, int && c);", Style); 2185 verifyFormat("int & f2(int && c, int* a, int & b);", Style); 2186 verifyFormat("int && f3(int & b, int && c, int* a);", Style); 2187 verifyFormat("int* a = f1();", Style); 2188 verifyFormat("int & b = f2();", Style); 2189 verifyFormat("int && c = f3();", Style); 2190 verifyFormat("int f3() { return sizeof(Foo &); }", Style); 2191 verifyFormat("int f4() { return sizeof(Foo &&); }", Style); 2192 verifyFormat("void f5() { int f6(Foo &, Bar &); }", Style); 2193 verifyFormat("void f5() { int f6(Foo &&, Bar &&); }", Style); 2194 verifyFormat("for (auto a = 0, b = 0; const auto & c : {1, 2, 3})", Style); 2195 verifyFormat("for (auto a = 0, b = 0; const int & c : {1, 2, 3})", Style); 2196 verifyFormat("for (auto a = 0, b = 0; const Foo & c : {1, 2, 3})", Style); 2197 verifyFormat("for (auto a = 0, b = 0; const Foo* c : {1, 2, 3})", Style); 2198 verifyFormat("for (int a = 0, b++; const auto & c : {1, 2, 3})", Style); 2199 verifyFormat("for (int a = 0, b++; const int & c : {1, 2, 3})", Style); 2200 verifyFormat("for (int a = 0, b++; const Foo & c : {1, 2, 3})", Style); 2201 verifyFormat("for (int a = 0, b++; const Foo* c : {1, 2, 3})", Style); 2202 verifyFormat("for (auto x = 0; auto & c : {1, 2, 3})", Style); 2203 verifyFormat("for (auto x = 0; int & c : {1, 2, 3})", Style); 2204 verifyFormat("for (int x = 0; auto & c : {1, 2, 3})", Style); 2205 verifyFormat("for (int x = 0; int & c : {1, 2, 3})", Style); 2206 verifyFormat("for (f(); auto & c : {1, 2, 3})", Style); 2207 verifyFormat("for (f(); int & c : {1, 2, 3})", Style); 2208 verifyFormat( 2209 "function<int(int &)> res1 = [](int & a) { return 0000000000000; },\n" 2210 " res2 = [](int & a) { return 0000000000000; };", 2211 Style); 2212 2213 Style.AlignConsecutiveDeclarations.Enabled = true; 2214 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = true; 2215 verifyFormat("Const unsigned int* c;\n" 2216 "const unsigned int* d;\n" 2217 "Const unsigned int & e;\n" 2218 "const unsigned int & f;\n" 2219 "int* f1(int* a, int & b, int && c);\n" 2220 "double* (*f2)(int* a, double && b);\n" 2221 "const unsigned && g;\n" 2222 "Const unsigned h;", 2223 Style); 2224 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = false; 2225 verifyFormat("Const unsigned int* c;\n" 2226 "const unsigned int* d;\n" 2227 "Const unsigned int & e;\n" 2228 "const unsigned int & f;\n" 2229 "int* f1(int* a, int & b, int && c);\n" 2230 "double* (*f2)(int* a, double && b);\n" 2231 "const unsigned && g;\n" 2232 "Const unsigned h;", 2233 Style); 2234 2235 Style.PointerAlignment = FormatStyle::PAS_Middle; 2236 Style.ReferenceAlignment = FormatStyle::RAS_Right; 2237 verifyFormat("int * f1(int * a, int &b, int &&c);", Style); 2238 verifyFormat("int &f2(int &&c, int * a, int &b);", Style); 2239 verifyFormat("int &&f3(int &b, int &&c, int * a);", Style); 2240 verifyFormat("int * a = f1();", Style); 2241 verifyFormat("int &b = f2();", Style); 2242 verifyFormat("int &&c = f3();", Style); 2243 verifyFormat("int f3() { return sizeof(Foo &); }", Style); 2244 verifyFormat("int f4() { return sizeof(Foo &&); }", Style); 2245 verifyFormat("void f5() { int f6(Foo &, Bar &); }", Style); 2246 verifyFormat("void f5() { int f6(Foo &&, Bar &&); }", Style); 2247 verifyFormat("for (auto a = 0, b = 0; const Foo * c : {1, 2, 3})", Style); 2248 verifyFormat("for (int a = 0, b = 0; const Foo * c : {1, 2, 3})", Style); 2249 verifyFormat("for (int a = 0, b++; const Foo * c : {1, 2, 3})", Style); 2250 2251 Style.AlignConsecutiveDeclarations.Enabled = true; 2252 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = true; 2253 verifyFormat("Const unsigned int * c;\n" 2254 "const unsigned int * d;\n" 2255 "Const unsigned int &e;\n" 2256 "const unsigned int &f;\n" 2257 "int * f1(int * a, int &b, int &&c);\n" 2258 "double * (*f2)(int * a, double &&b);\n" 2259 "const unsigned &&g;\n" 2260 "Const unsigned h;", 2261 Style); 2262 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = false; 2263 verifyFormat("Const unsigned int * c;\n" 2264 "const unsigned int * d;\n" 2265 "Const unsigned int &e;\n" 2266 "const unsigned int &f;\n" 2267 "int * f1(int * a, int &b, int &&c);\n" 2268 "double * (*f2)(int * a, double &&b);\n" 2269 "const unsigned &&g;\n" 2270 "Const unsigned h;", 2271 Style); 2272 2273 // FIXME: we don't handle this yet, so output may be arbitrary until it's 2274 // specifically handled 2275 // verifyFormat("int Add2(BTree * &Root, char * szToAdd)", Style); 2276 } 2277 2278 TEST_F(FormatTest, FormatsForLoop) { 2279 verifyFormat( 2280 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" 2281 " ++VeryVeryLongLoopVariable)\n" 2282 " ;"); 2283 verifyFormat("for (;;)\n" 2284 " f();"); 2285 verifyFormat("for (;;) {\n}"); 2286 verifyFormat("for (;;) {\n" 2287 " f();\n" 2288 "}"); 2289 verifyFormat("for (int i = 0; (i < 10); ++i) {\n}"); 2290 2291 verifyFormat( 2292 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 2293 " E = UnwrappedLines.end();\n" 2294 " I != E; ++I) {\n}"); 2295 2296 verifyFormat( 2297 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" 2298 " ++IIIII) {\n}"); 2299 verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n" 2300 " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n" 2301 " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}"); 2302 verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n" 2303 " I = FD->getDeclsInPrototypeScope().begin(),\n" 2304 " E = FD->getDeclsInPrototypeScope().end();\n" 2305 " I != E; ++I) {\n}"); 2306 verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n" 2307 " I = Container.begin(),\n" 2308 " E = Container.end();\n" 2309 " I != E; ++I) {\n}", 2310 getLLVMStyleWithColumns(76)); 2311 2312 verifyFormat( 2313 "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 2314 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n" 2315 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2316 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 2317 " ++aaaaaaaaaaa) {\n}"); 2318 verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2319 " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n" 2320 " ++i) {\n}"); 2321 verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n" 2322 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 2323 "}"); 2324 verifyFormat("for (some_namespace::SomeIterator iter( // force break\n" 2325 " aaaaaaaaaa);\n" 2326 " iter; ++iter) {\n" 2327 "}"); 2328 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2329 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 2330 " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n" 2331 " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {"); 2332 2333 // These should not be formatted as Objective-C for-in loops. 2334 verifyFormat("for (Foo *x = 0; x != in; x++) {\n}"); 2335 verifyFormat("Foo *x;\nfor (x = 0; x != in; x++) {\n}"); 2336 verifyFormat("Foo *x;\nfor (x in y) {\n}"); 2337 verifyFormat( 2338 "for (const Foo<Bar> &baz = in.value(); !baz.at_end(); ++baz) {\n}"); 2339 2340 FormatStyle NoBinPacking = getLLVMStyle(); 2341 NoBinPacking.BinPackParameters = FormatStyle::BPPS_OnePerLine; 2342 verifyFormat("for (int aaaaaaaaaaa = 1;\n" 2343 " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n" 2344 " aaaaaaaaaaaaaaaa,\n" 2345 " aaaaaaaaaaaaaaaa,\n" 2346 " aaaaaaaaaaaaaaaa);\n" 2347 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 2348 "}", 2349 NoBinPacking); 2350 verifyFormat( 2351 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 2352 " E = UnwrappedLines.end();\n" 2353 " I != E;\n" 2354 " ++I) {\n}", 2355 NoBinPacking); 2356 2357 FormatStyle AlignLeft = getLLVMStyle(); 2358 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 2359 verifyFormat("for (A* a = start; a < end; ++a, ++value) {\n}", AlignLeft); 2360 } 2361 2362 TEST_F(FormatTest, RangeBasedForLoops) { 2363 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 2364 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 2365 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n" 2366 " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}"); 2367 verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n" 2368 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 2369 verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n" 2370 " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}"); 2371 } 2372 2373 TEST_F(FormatTest, ForEachLoops) { 2374 FormatStyle Style = getLLVMStyle(); 2375 EXPECT_EQ(Style.AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); 2376 EXPECT_EQ(Style.AllowShortLoopsOnASingleLine, false); 2377 verifyFormat("void f() {\n" 2378 " for (;;) {\n" 2379 " }\n" 2380 " foreach (Item *item, itemlist) {\n" 2381 " }\n" 2382 " Q_FOREACH (Item *item, itemlist) {\n" 2383 " }\n" 2384 " BOOST_FOREACH (Item *item, itemlist) {\n" 2385 " }\n" 2386 " UNKNOWN_FOREACH(Item * item, itemlist) {}\n" 2387 "}", 2388 Style); 2389 verifyFormat("void f() {\n" 2390 " for (;;)\n" 2391 " int j = 1;\n" 2392 " Q_FOREACH (int v, vec)\n" 2393 " v *= 2;\n" 2394 " for (;;) {\n" 2395 " int j = 1;\n" 2396 " }\n" 2397 " Q_FOREACH (int v, vec) {\n" 2398 " v *= 2;\n" 2399 " }\n" 2400 "}", 2401 Style); 2402 2403 FormatStyle ShortBlocks = getLLVMStyle(); 2404 ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 2405 EXPECT_EQ(ShortBlocks.AllowShortLoopsOnASingleLine, false); 2406 verifyFormat("void f() {\n" 2407 " for (;;)\n" 2408 " int j = 1;\n" 2409 " Q_FOREACH (int &v, vec)\n" 2410 " v *= 2;\n" 2411 " for (;;) {\n" 2412 " int j = 1;\n" 2413 " }\n" 2414 " Q_FOREACH (int &v, vec) {\n" 2415 " int j = 1;\n" 2416 " }\n" 2417 "}", 2418 ShortBlocks); 2419 2420 FormatStyle ShortLoops = getLLVMStyle(); 2421 ShortLoops.AllowShortLoopsOnASingleLine = true; 2422 EXPECT_EQ(ShortLoops.AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); 2423 verifyFormat("void f() {\n" 2424 " for (;;) int j = 1;\n" 2425 " Q_FOREACH (int &v, vec) int j = 1;\n" 2426 " for (;;) {\n" 2427 " int j = 1;\n" 2428 " }\n" 2429 " Q_FOREACH (int &v, vec) {\n" 2430 " int j = 1;\n" 2431 " }\n" 2432 "}", 2433 ShortLoops); 2434 2435 FormatStyle ShortBlocksAndLoops = getLLVMStyle(); 2436 ShortBlocksAndLoops.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 2437 ShortBlocksAndLoops.AllowShortLoopsOnASingleLine = true; 2438 verifyFormat("void f() {\n" 2439 " for (;;) int j = 1;\n" 2440 " Q_FOREACH (int &v, vec) int j = 1;\n" 2441 " for (;;) { int j = 1; }\n" 2442 " Q_FOREACH (int &v, vec) { int j = 1; }\n" 2443 "}", 2444 ShortBlocksAndLoops); 2445 2446 Style.SpaceBeforeParens = 2447 FormatStyle::SBPO_ControlStatementsExceptControlMacros; 2448 verifyFormat("void f() {\n" 2449 " for (;;) {\n" 2450 " }\n" 2451 " foreach(Item *item, itemlist) {\n" 2452 " }\n" 2453 " Q_FOREACH(Item *item, itemlist) {\n" 2454 " }\n" 2455 " BOOST_FOREACH(Item *item, itemlist) {\n" 2456 " }\n" 2457 " UNKNOWN_FOREACH(Item * item, itemlist) {}\n" 2458 "}", 2459 Style); 2460 2461 // As function-like macros. 2462 verifyFormat("#define foreach(x, y)\n" 2463 "#define Q_FOREACH(x, y)\n" 2464 "#define BOOST_FOREACH(x, y)\n" 2465 "#define UNKNOWN_FOREACH(x, y)"); 2466 2467 // Not as function-like macros. 2468 verifyFormat("#define foreach (x, y)\n" 2469 "#define Q_FOREACH (x, y)\n" 2470 "#define BOOST_FOREACH (x, y)\n" 2471 "#define UNKNOWN_FOREACH (x, y)"); 2472 2473 // handle microsoft non standard extension 2474 verifyFormat("for each (char c in x->MyStringProperty)"); 2475 } 2476 2477 TEST_F(FormatTest, FormatsWhileLoop) { 2478 verifyFormat("while (true) {\n}"); 2479 verifyFormat("while (true)\n" 2480 " f();"); 2481 verifyFormat("while () {\n}"); 2482 verifyFormat("while () {\n" 2483 " f();\n" 2484 "}"); 2485 } 2486 2487 TEST_F(FormatTest, FormatsDoWhile) { 2488 verifyFormat("do {\n" 2489 " do_something();\n" 2490 "} while (something());"); 2491 verifyFormat("do\n" 2492 " do_something();\n" 2493 "while (something());"); 2494 } 2495 2496 TEST_F(FormatTest, FormatsSwitchStatement) { 2497 verifyFormat("switch (x) {\n" 2498 "case 1:\n" 2499 " f();\n" 2500 " break;\n" 2501 "case kFoo:\n" 2502 "case ns::kBar:\n" 2503 "case kBaz:\n" 2504 " break;\n" 2505 "default:\n" 2506 " g();\n" 2507 " break;\n" 2508 "}"); 2509 verifyFormat("switch (x) {\n" 2510 "case 1: {\n" 2511 " f();\n" 2512 " break;\n" 2513 "}\n" 2514 "case 2: {\n" 2515 " break;\n" 2516 "}\n" 2517 "}"); 2518 verifyFormat("switch (x) {\n" 2519 "case 1: {\n" 2520 " f();\n" 2521 " {\n" 2522 " g();\n" 2523 " h();\n" 2524 " }\n" 2525 " break;\n" 2526 "}\n" 2527 "}"); 2528 verifyFormat("switch (x) {\n" 2529 "case 1: {\n" 2530 " f();\n" 2531 " if (foo) {\n" 2532 " g();\n" 2533 " h();\n" 2534 " }\n" 2535 " break;\n" 2536 "}\n" 2537 "}"); 2538 verifyFormat("switch (x) {\n" 2539 "case 1: {\n" 2540 " f();\n" 2541 " g();\n" 2542 "} break;\n" 2543 "}"); 2544 verifyFormat("switch (test)\n" 2545 " ;"); 2546 verifyFormat("switch (x) {\n" 2547 "default: {\n" 2548 " // Do nothing.\n" 2549 "}\n" 2550 "}"); 2551 verifyFormat("switch (x) {\n" 2552 "// comment\n" 2553 "// if 1, do f()\n" 2554 "case 1:\n" 2555 " f();\n" 2556 "}"); 2557 verifyFormat("switch (x) {\n" 2558 "case 1:\n" 2559 " // Do amazing stuff\n" 2560 " {\n" 2561 " f();\n" 2562 " g();\n" 2563 " }\n" 2564 " break;\n" 2565 "}"); 2566 verifyFormat("#define A \\\n" 2567 " switch (x) { \\\n" 2568 " case a: \\\n" 2569 " foo = b; \\\n" 2570 " }", 2571 getLLVMStyleWithColumns(20)); 2572 verifyFormat("#define OPERATION_CASE(name) \\\n" 2573 " case OP_name: \\\n" 2574 " return operations::Operation##name", 2575 getLLVMStyleWithColumns(40)); 2576 verifyFormat("switch (x) {\n" 2577 "case 1:;\n" 2578 "default:;\n" 2579 " int i;\n" 2580 "}"); 2581 2582 verifyGoogleFormat("switch (x) {\n" 2583 " case 1:\n" 2584 " f();\n" 2585 " break;\n" 2586 " case kFoo:\n" 2587 " case ns::kBar:\n" 2588 " case kBaz:\n" 2589 " break;\n" 2590 " default:\n" 2591 " g();\n" 2592 " break;\n" 2593 "}"); 2594 verifyGoogleFormat("switch (x) {\n" 2595 " case 1: {\n" 2596 " f();\n" 2597 " break;\n" 2598 " }\n" 2599 "}"); 2600 verifyGoogleFormat("switch (test)\n" 2601 " ;"); 2602 2603 verifyGoogleFormat("#define OPERATION_CASE(name) \\\n" 2604 " case OP_name: \\\n" 2605 " return operations::Operation##name"); 2606 verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n" 2607 " // Get the correction operation class.\n" 2608 " switch (OpCode) {\n" 2609 " CASE(Add);\n" 2610 " CASE(Subtract);\n" 2611 " default:\n" 2612 " return operations::Unknown;\n" 2613 " }\n" 2614 "#undef OPERATION_CASE\n" 2615 "}"); 2616 verifyFormat("DEBUG({\n" 2617 " switch (x) {\n" 2618 " case A:\n" 2619 " f();\n" 2620 " break;\n" 2621 " // fallthrough\n" 2622 " case B:\n" 2623 " g();\n" 2624 " break;\n" 2625 " }\n" 2626 "});"); 2627 verifyNoChange("DEBUG({\n" 2628 " switch (x) {\n" 2629 " case A:\n" 2630 " f();\n" 2631 " break;\n" 2632 " // On B:\n" 2633 " case B:\n" 2634 " g();\n" 2635 " break;\n" 2636 " }\n" 2637 "});"); 2638 verifyFormat("switch (n) {\n" 2639 "case 0: {\n" 2640 " return false;\n" 2641 "}\n" 2642 "default: {\n" 2643 " return true;\n" 2644 "}\n" 2645 "}", 2646 "switch (n)\n" 2647 "{\n" 2648 "case 0: {\n" 2649 " return false;\n" 2650 "}\n" 2651 "default: {\n" 2652 " return true;\n" 2653 "}\n" 2654 "}"); 2655 verifyFormat("switch (a) {\n" 2656 "case (b):\n" 2657 " return;\n" 2658 "}"); 2659 2660 verifyFormat("switch (a) {\n" 2661 "case some_namespace::\n" 2662 " some_constant:\n" 2663 " return;\n" 2664 "}", 2665 getLLVMStyleWithColumns(34)); 2666 2667 verifyFormat("switch (a) {\n" 2668 "[[likely]] case 1:\n" 2669 " return;\n" 2670 "}"); 2671 verifyFormat("switch (a) {\n" 2672 "[[likely]] [[other::likely]] case 1:\n" 2673 " return;\n" 2674 "}"); 2675 verifyFormat("switch (x) {\n" 2676 "case 1:\n" 2677 " return;\n" 2678 "[[likely]] case 2:\n" 2679 " return;\n" 2680 "}"); 2681 verifyFormat("switch (a) {\n" 2682 "case 1:\n" 2683 "[[likely]] case 2:\n" 2684 " return;\n" 2685 "}"); 2686 FormatStyle Attributes = getLLVMStyle(); 2687 Attributes.AttributeMacros.push_back("LIKELY"); 2688 Attributes.AttributeMacros.push_back("OTHER_LIKELY"); 2689 verifyFormat("switch (a) {\n" 2690 "LIKELY case b:\n" 2691 " return;\n" 2692 "}", 2693 Attributes); 2694 verifyFormat("switch (a) {\n" 2695 "LIKELY OTHER_LIKELY() case b:\n" 2696 " return;\n" 2697 "}", 2698 Attributes); 2699 verifyFormat("switch (a) {\n" 2700 "case 1:\n" 2701 " return;\n" 2702 "LIKELY case 2:\n" 2703 " return;\n" 2704 "}", 2705 Attributes); 2706 verifyFormat("switch (a) {\n" 2707 "case 1:\n" 2708 "LIKELY case 2:\n" 2709 " return;\n" 2710 "}", 2711 Attributes); 2712 2713 FormatStyle Style = getLLVMStyle(); 2714 Style.IndentCaseLabels = true; 2715 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never; 2716 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2717 Style.BraceWrapping.AfterCaseLabel = true; 2718 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 2719 verifyFormat("switch (n)\n" 2720 "{\n" 2721 " case 0:\n" 2722 " {\n" 2723 " return false;\n" 2724 " }\n" 2725 " default:\n" 2726 " {\n" 2727 " return true;\n" 2728 " }\n" 2729 "}", 2730 "switch (n) {\n" 2731 " case 0: {\n" 2732 " return false;\n" 2733 " }\n" 2734 " default: {\n" 2735 " return true;\n" 2736 " }\n" 2737 "}", 2738 Style); 2739 Style.BraceWrapping.AfterCaseLabel = false; 2740 verifyFormat("switch (n)\n" 2741 "{\n" 2742 " case 0: {\n" 2743 " return false;\n" 2744 " }\n" 2745 " default: {\n" 2746 " return true;\n" 2747 " }\n" 2748 "}", 2749 "switch (n) {\n" 2750 " case 0:\n" 2751 " {\n" 2752 " return false;\n" 2753 " }\n" 2754 " default:\n" 2755 " {\n" 2756 " return true;\n" 2757 " }\n" 2758 "}", 2759 Style); 2760 Style.IndentCaseLabels = false; 2761 Style.IndentCaseBlocks = true; 2762 verifyFormat("switch (n)\n" 2763 "{\n" 2764 "case 0:\n" 2765 " {\n" 2766 " return false;\n" 2767 " }\n" 2768 "case 1:\n" 2769 " break;\n" 2770 "default:\n" 2771 " {\n" 2772 " return true;\n" 2773 " }\n" 2774 "}", 2775 "switch (n) {\n" 2776 "case 0: {\n" 2777 " return false;\n" 2778 "}\n" 2779 "case 1:\n" 2780 " break;\n" 2781 "default: {\n" 2782 " return true;\n" 2783 "}\n" 2784 "}", 2785 Style); 2786 Style.IndentCaseLabels = true; 2787 Style.IndentCaseBlocks = true; 2788 verifyFormat("switch (n)\n" 2789 "{\n" 2790 " case 0:\n" 2791 " {\n" 2792 " return false;\n" 2793 " }\n" 2794 " case 1:\n" 2795 " break;\n" 2796 " default:\n" 2797 " {\n" 2798 " return true;\n" 2799 " }\n" 2800 "}", 2801 "switch (n) {\n" 2802 "case 0: {\n" 2803 " return false;\n" 2804 "}\n" 2805 "case 1:\n" 2806 " break;\n" 2807 "default: {\n" 2808 " return true;\n" 2809 "}\n" 2810 "}", 2811 Style); 2812 } 2813 2814 TEST_F(FormatTest, CaseRanges) { 2815 verifyFormat("switch (x) {\n" 2816 "case 'A' ... 'Z':\n" 2817 "case 1 ... 5:\n" 2818 "case a ... b:\n" 2819 " break;\n" 2820 "}"); 2821 } 2822 2823 TEST_F(FormatTest, ShortEnums) { 2824 FormatStyle Style = getLLVMStyle(); 2825 EXPECT_TRUE(Style.AllowShortEnumsOnASingleLine); 2826 EXPECT_FALSE(Style.BraceWrapping.AfterEnum); 2827 verifyFormat("enum { A, B, C } ShortEnum1, ShortEnum2;", Style); 2828 verifyFormat("typedef enum { A, B, C } ShortEnum1, ShortEnum2;", Style); 2829 Style.AllowShortEnumsOnASingleLine = false; 2830 verifyFormat("enum {\n" 2831 " A,\n" 2832 " B,\n" 2833 " C\n" 2834 "} ShortEnum1, ShortEnum2;", 2835 Style); 2836 verifyFormat("typedef enum {\n" 2837 " A,\n" 2838 " B,\n" 2839 " C\n" 2840 "} ShortEnum1, ShortEnum2;", 2841 Style); 2842 verifyFormat("enum {\n" 2843 " A,\n" 2844 "} ShortEnum1, ShortEnum2;", 2845 Style); 2846 verifyFormat("typedef enum {\n" 2847 " A,\n" 2848 "} ShortEnum1, ShortEnum2;", 2849 Style); 2850 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2851 Style.BraceWrapping.AfterEnum = true; 2852 verifyFormat("enum\n" 2853 "{\n" 2854 " A,\n" 2855 " B,\n" 2856 " C\n" 2857 "} ShortEnum1, ShortEnum2;", 2858 Style); 2859 verifyFormat("typedef enum\n" 2860 "{\n" 2861 " A,\n" 2862 " B,\n" 2863 " C\n" 2864 "} ShortEnum1, ShortEnum2;", 2865 Style); 2866 } 2867 2868 TEST_F(FormatTest, ShortCompoundRequirement) { 2869 FormatStyle Style = getLLVMStyle(); 2870 EXPECT_TRUE(Style.AllowShortCompoundRequirementOnASingleLine); 2871 verifyFormat("template <typename T>\n" 2872 "concept c = requires(T x) {\n" 2873 " { x + 1 } -> std::same_as<int>;\n" 2874 "};", 2875 Style); 2876 verifyFormat("template <typename T>\n" 2877 "concept c = requires(T x) {\n" 2878 " { x + 1 } -> std::same_as<int>;\n" 2879 " { x + 2 } -> std::same_as<int>;\n" 2880 "};", 2881 Style); 2882 Style.AllowShortCompoundRequirementOnASingleLine = false; 2883 verifyFormat("template <typename T>\n" 2884 "concept c = requires(T x) {\n" 2885 " {\n" 2886 " x + 1\n" 2887 " } -> std::same_as<int>;\n" 2888 "};", 2889 Style); 2890 verifyFormat("template <typename T>\n" 2891 "concept c = requires(T x) {\n" 2892 " {\n" 2893 " x + 1\n" 2894 " } -> std::same_as<int>;\n" 2895 " {\n" 2896 " x + 2\n" 2897 " } -> std::same_as<int>;\n" 2898 "};", 2899 Style); 2900 } 2901 2902 TEST_F(FormatTest, ShortCaseLabels) { 2903 FormatStyle Style = getLLVMStyle(); 2904 Style.AllowShortCaseLabelsOnASingleLine = true; 2905 verifyFormat("switch (a) {\n" 2906 "case 1: x = 1; break;\n" 2907 "case 2: return;\n" 2908 "case 3:\n" 2909 "case 4:\n" 2910 "case 5: return;\n" 2911 "case 6: // comment\n" 2912 " return;\n" 2913 "case 7:\n" 2914 " // comment\n" 2915 " return;\n" 2916 "case 8:\n" 2917 " x = 8; // comment\n" 2918 " break;\n" 2919 "default: y = 1; break;\n" 2920 "}", 2921 Style); 2922 verifyFormat("switch (a) {\n" 2923 "case 0: return; // comment\n" 2924 "case 1: break; // comment\n" 2925 "case 2: return;\n" 2926 "// comment\n" 2927 "case 3: return;\n" 2928 "// comment 1\n" 2929 "// comment 2\n" 2930 "// comment 3\n" 2931 "case 4: break; /* comment */\n" 2932 "case 5:\n" 2933 " // comment\n" 2934 " break;\n" 2935 "case 6: /* comment */ x = 1; break;\n" 2936 "case 7: x = /* comment */ 1; break;\n" 2937 "case 8:\n" 2938 " x = 1; /* comment */\n" 2939 " break;\n" 2940 "case 9:\n" 2941 " break; // comment line 1\n" 2942 " // comment line 2\n" 2943 "}", 2944 Style); 2945 verifyFormat("switch (a) {\n" 2946 "case 1:\n" 2947 " x = 8;\n" 2948 " // fall through\n" 2949 "case 2: x = 8;\n" 2950 "// comment\n" 2951 "case 3:\n" 2952 " return; /* comment line 1\n" 2953 " * comment line 2 */\n" 2954 "case 4: i = 8;\n" 2955 "// something else\n" 2956 "#if FOO\n" 2957 "case 5: break;\n" 2958 "#endif\n" 2959 "}", 2960 "switch (a) {\n" 2961 "case 1: x = 8;\n" 2962 " // fall through\n" 2963 "case 2:\n" 2964 " x = 8;\n" 2965 "// comment\n" 2966 "case 3:\n" 2967 " return; /* comment line 1\n" 2968 " * comment line 2 */\n" 2969 "case 4:\n" 2970 " i = 8;\n" 2971 "// something else\n" 2972 "#if FOO\n" 2973 "case 5: break;\n" 2974 "#endif\n" 2975 "}", 2976 Style); 2977 verifyFormat("switch (a) {\n" 2978 "case 0:\n" 2979 " return; // long long long long long long long long long long " 2980 "long long comment\n" 2981 " // line\n" 2982 "}", 2983 "switch (a) {\n" 2984 "case 0: return; // long long long long long long long long " 2985 "long long long long comment line\n" 2986 "}", 2987 Style); 2988 verifyFormat("switch (a) {\n" 2989 "case 0:\n" 2990 " return; /* long long long long long long long long long long " 2991 "long long comment\n" 2992 " line */\n" 2993 "}", 2994 "switch (a) {\n" 2995 "case 0: return; /* long long long long long long long long " 2996 "long long long long comment line */\n" 2997 "}", 2998 Style); 2999 verifyFormat("switch (a) {\n" 3000 "#if FOO\n" 3001 "case 0: return 0;\n" 3002 "#endif\n" 3003 "}", 3004 Style); 3005 verifyFormat("switch (a) {\n" 3006 "case 1: {\n" 3007 "}\n" 3008 "case 2: {\n" 3009 " return;\n" 3010 "}\n" 3011 "case 3: {\n" 3012 " x = 1;\n" 3013 " return;\n" 3014 "}\n" 3015 "case 4:\n" 3016 " if (x)\n" 3017 " return;\n" 3018 "}", 3019 Style); 3020 Style.ColumnLimit = 21; 3021 verifyFormat("#define X \\\n" 3022 " case 0: break;\n" 3023 "#include \"f\"", 3024 Style); 3025 verifyFormat("switch (a) {\n" 3026 "case 1: x = 1; break;\n" 3027 "case 2: return;\n" 3028 "case 3:\n" 3029 "case 4:\n" 3030 "case 5: return;\n" 3031 "default:\n" 3032 " y = 1;\n" 3033 " break;\n" 3034 "}", 3035 Style); 3036 Style.ColumnLimit = 80; 3037 Style.AllowShortCaseLabelsOnASingleLine = false; 3038 Style.IndentCaseLabels = true; 3039 verifyFormat("switch (n) {\n" 3040 " default /*comments*/:\n" 3041 " return true;\n" 3042 " case 0:\n" 3043 " return false;\n" 3044 "}", 3045 "switch (n) {\n" 3046 "default/*comments*/:\n" 3047 " return true;\n" 3048 "case 0:\n" 3049 " return false;\n" 3050 "}", 3051 Style); 3052 Style.AllowShortCaseLabelsOnASingleLine = true; 3053 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 3054 Style.BraceWrapping.AfterCaseLabel = true; 3055 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 3056 verifyFormat("switch (n)\n" 3057 "{\n" 3058 " case 0:\n" 3059 " {\n" 3060 " return false;\n" 3061 " }\n" 3062 " default:\n" 3063 " {\n" 3064 " return true;\n" 3065 " }\n" 3066 "}", 3067 "switch (n) {\n" 3068 " case 0: {\n" 3069 " return false;\n" 3070 " }\n" 3071 " default:\n" 3072 " {\n" 3073 " return true;\n" 3074 " }\n" 3075 "}", 3076 Style); 3077 } 3078 3079 TEST_F(FormatTest, FormatsLabels) { 3080 verifyFormat("void f() {\n" 3081 " some_code();\n" 3082 "test_label:\n" 3083 " some_other_code();\n" 3084 " {\n" 3085 " some_more_code();\n" 3086 " another_label:\n" 3087 " some_more_code();\n" 3088 " }\n" 3089 "}"); 3090 verifyFormat("{\n" 3091 " some_code();\n" 3092 "test_label:\n" 3093 " some_other_code();\n" 3094 "}"); 3095 verifyFormat("{\n" 3096 " some_code();\n" 3097 "test_label:;\n" 3098 " int i = 0;\n" 3099 "}"); 3100 verifyFormat("{\n" 3101 " some_code();\n" 3102 "test_label: { some_other_code(); }\n" 3103 "}"); 3104 verifyFormat("{\n" 3105 " some_code();\n" 3106 "test_label: {\n" 3107 " some_other_code();\n" 3108 " some_other_code();\n" 3109 "}\n" 3110 "}"); 3111 verifyFormat("{\n" 3112 "L0:\n" 3113 "[[foo]] L1:\n" 3114 "[[bar]] [[baz]] L2:\n" 3115 " g();\n" 3116 "}"); 3117 verifyFormat("{\n" 3118 "[[foo]] L1: {\n" 3119 "[[bar]] [[baz]] L2:\n" 3120 " g();\n" 3121 "}\n" 3122 "}"); 3123 verifyFormat("{\n" 3124 "[[foo]] L1:\n" 3125 " f();\n" 3126 " {\n" 3127 " [[bar]] [[baz]] L2:\n" 3128 " g();\n" 3129 " }\n" 3130 "}"); 3131 3132 FormatStyle Style = getLLVMStyle(); 3133 Style.IndentGotoLabels = false; 3134 verifyFormat("void f() {\n" 3135 " some_code();\n" 3136 "test_label:\n" 3137 " some_other_code();\n" 3138 " {\n" 3139 " some_more_code();\n" 3140 "another_label:\n" 3141 " some_more_code();\n" 3142 " }\n" 3143 "}", 3144 Style); 3145 verifyFormat("{\n" 3146 " some_code();\n" 3147 "test_label:\n" 3148 " some_other_code();\n" 3149 "}", 3150 Style); 3151 verifyFormat("{\n" 3152 " some_code();\n" 3153 "test_label:;\n" 3154 " int i = 0;\n" 3155 "}", 3156 Style); 3157 verifyFormat("{\n" 3158 " some_code();\n" 3159 "test_label: { some_other_code(); }\n" 3160 "}", 3161 Style); 3162 verifyFormat("{\n" 3163 "[[foo]] L1:\n" 3164 " f();\n" 3165 " {\n" 3166 "[[bar]] [[baz]] L2:\n" 3167 " g();\n" 3168 " }\n" 3169 "}", 3170 Style); 3171 3172 Style.ColumnLimit = 15; 3173 verifyFormat("#define FOO \\\n" 3174 "label: \\\n" 3175 " break;", 3176 Style); 3177 3178 // The opening brace may either be on the same unwrapped line as the colon or 3179 // on a separate one. The formatter should recognize both. 3180 Style = getLLVMStyle(); 3181 Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Allman; 3182 verifyFormat("{\n" 3183 " some_code();\n" 3184 "test_label:\n" 3185 "{\n" 3186 " some_other_code();\n" 3187 "}\n" 3188 "}", 3189 Style); 3190 verifyFormat("{\n" 3191 "[[foo]] L1:\n" 3192 "{\n" 3193 "[[bar]] [[baz]] L2:\n" 3194 " g();\n" 3195 "}\n" 3196 "}", 3197 Style); 3198 } 3199 3200 TEST_F(FormatTest, MultiLineControlStatements) { 3201 FormatStyle Style = getLLVMStyleWithColumns(20); 3202 Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom; 3203 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine; 3204 // Short lines should keep opening brace on same line. 3205 verifyFormat("if (foo) {\n" 3206 " bar();\n" 3207 "}", 3208 "if(foo){bar();}", Style); 3209 verifyFormat("if (foo) {\n" 3210 " bar();\n" 3211 "} else {\n" 3212 " baz();\n" 3213 "}", 3214 "if(foo){bar();}else{baz();}", Style); 3215 verifyFormat("if (foo && bar) {\n" 3216 " baz();\n" 3217 "}", 3218 "if(foo&&bar){baz();}", Style); 3219 verifyFormat("if (foo) {\n" 3220 " bar();\n" 3221 "} else if (baz) {\n" 3222 " quux();\n" 3223 "}", 3224 "if(foo){bar();}else if(baz){quux();}", Style); 3225 verifyFormat("if (foo) {\n" 3226 " bar();\n" 3227 "} else if (baz) {\n" 3228 " quux();\n" 3229 "} else {\n" 3230 " foobar();\n" 3231 "}", 3232 "if(foo){bar();}else if(baz){quux();}else{foobar();}", Style); 3233 verifyFormat("for (;;) {\n" 3234 " foo();\n" 3235 "}", 3236 "for(;;){foo();}"); 3237 verifyFormat("while (1) {\n" 3238 " foo();\n" 3239 "}", 3240 "while(1){foo();}", Style); 3241 verifyFormat("switch (foo) {\n" 3242 "case bar:\n" 3243 " return;\n" 3244 "}", 3245 "switch(foo){case bar:return;}", Style); 3246 verifyFormat("try {\n" 3247 " foo();\n" 3248 "} catch (...) {\n" 3249 " bar();\n" 3250 "}", 3251 "try{foo();}catch(...){bar();}", Style); 3252 verifyFormat("do {\n" 3253 " foo();\n" 3254 "} while (bar &&\n" 3255 " baz);", 3256 "do{foo();}while(bar&&baz);", Style); 3257 // Long lines should put opening brace on new line. 3258 verifyFormat("void f() {\n" 3259 " if (a1 && a2 &&\n" 3260 " a3)\n" 3261 " {\n" 3262 " quux();\n" 3263 " }\n" 3264 "}", 3265 "void f(){if(a1&&a2&&a3){quux();}}", Style); 3266 verifyFormat("if (foo && bar &&\n" 3267 " baz)\n" 3268 "{\n" 3269 " quux();\n" 3270 "}", 3271 "if(foo&&bar&&baz){quux();}", Style); 3272 verifyFormat("if (foo && bar &&\n" 3273 " baz)\n" 3274 "{\n" 3275 " quux();\n" 3276 "}", 3277 "if (foo && bar &&\n" 3278 " baz) {\n" 3279 " quux();\n" 3280 "}", 3281 Style); 3282 verifyFormat("if (foo) {\n" 3283 " bar();\n" 3284 "} else if (baz ||\n" 3285 " quux)\n" 3286 "{\n" 3287 " foobar();\n" 3288 "}", 3289 "if(foo){bar();}else if(baz||quux){foobar();}", Style); 3290 verifyFormat("if (foo) {\n" 3291 " bar();\n" 3292 "} else if (baz ||\n" 3293 " quux)\n" 3294 "{\n" 3295 " foobar();\n" 3296 "} else {\n" 3297 " barbaz();\n" 3298 "}", 3299 "if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}", 3300 Style); 3301 verifyFormat("for (int i = 0;\n" 3302 " i < 10; ++i)\n" 3303 "{\n" 3304 " foo();\n" 3305 "}", 3306 "for(int i=0;i<10;++i){foo();}", Style); 3307 verifyFormat("foreach (int i,\n" 3308 " list)\n" 3309 "{\n" 3310 " foo();\n" 3311 "}", 3312 "foreach(int i, list){foo();}", Style); 3313 Style.ColumnLimit = 3314 40; // to concentrate at brace wrapping, not line wrap due to column limit 3315 verifyFormat("foreach (int i, list) {\n" 3316 " foo();\n" 3317 "}", 3318 "foreach(int i, list){foo();}", Style); 3319 Style.ColumnLimit = 3320 20; // to concentrate at brace wrapping, not line wrap due to column limit 3321 verifyFormat("while (foo || bar ||\n" 3322 " baz)\n" 3323 "{\n" 3324 " quux();\n" 3325 "}", 3326 "while(foo||bar||baz){quux();}", Style); 3327 verifyFormat("switch (\n" 3328 " foo = barbaz)\n" 3329 "{\n" 3330 "case quux:\n" 3331 " return;\n" 3332 "}", 3333 "switch(foo=barbaz){case quux:return;}", Style); 3334 verifyFormat("try {\n" 3335 " foo();\n" 3336 "} catch (\n" 3337 " Exception &bar)\n" 3338 "{\n" 3339 " baz();\n" 3340 "}", 3341 "try{foo();}catch(Exception&bar){baz();}", Style); 3342 Style.ColumnLimit = 3343 40; // to concentrate at brace wrapping, not line wrap due to column limit 3344 verifyFormat("try {\n" 3345 " foo();\n" 3346 "} catch (Exception &bar) {\n" 3347 " baz();\n" 3348 "}", 3349 "try{foo();}catch(Exception&bar){baz();}", Style); 3350 Style.ColumnLimit = 3351 20; // to concentrate at brace wrapping, not line wrap due to column limit 3352 3353 Style.BraceWrapping.BeforeElse = true; 3354 verifyFormat("if (foo) {\n" 3355 " bar();\n" 3356 "}\n" 3357 "else if (baz ||\n" 3358 " quux)\n" 3359 "{\n" 3360 " foobar();\n" 3361 "}\n" 3362 "else {\n" 3363 " barbaz();\n" 3364 "}", 3365 "if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}", 3366 Style); 3367 3368 Style.BraceWrapping.BeforeCatch = true; 3369 verifyFormat("try {\n" 3370 " foo();\n" 3371 "}\n" 3372 "catch (...) {\n" 3373 " baz();\n" 3374 "}", 3375 "try{foo();}catch(...){baz();}", Style); 3376 3377 Style.BraceWrapping.AfterFunction = true; 3378 Style.BraceWrapping.AfterStruct = false; 3379 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine; 3380 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 3381 Style.ColumnLimit = 80; 3382 verifyFormat("void shortfunction() { bar(); }", Style); 3383 verifyFormat("struct T shortfunction() { return bar(); }", Style); 3384 verifyFormat("struct T {};", Style); 3385 3386 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 3387 verifyFormat("void shortfunction()\n" 3388 "{\n" 3389 " bar();\n" 3390 "}", 3391 Style); 3392 verifyFormat("struct T shortfunction()\n" 3393 "{\n" 3394 " return bar();\n" 3395 "}", 3396 Style); 3397 verifyFormat("struct T {};", Style); 3398 3399 Style.BraceWrapping.AfterFunction = false; 3400 Style.BraceWrapping.AfterStruct = true; 3401 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 3402 verifyFormat("void shortfunction() { bar(); }", Style); 3403 verifyFormat("struct T shortfunction() { return bar(); }", Style); 3404 verifyFormat("struct T\n" 3405 "{\n" 3406 "};", 3407 Style); 3408 3409 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 3410 verifyFormat("void shortfunction() {\n" 3411 " bar();\n" 3412 "}", 3413 Style); 3414 verifyFormat("struct T shortfunction() {\n" 3415 " return bar();\n" 3416 "}", 3417 Style); 3418 verifyFormat("struct T\n" 3419 "{\n" 3420 "};", 3421 Style); 3422 } 3423 3424 TEST_F(FormatTest, BeforeWhile) { 3425 FormatStyle Style = getLLVMStyle(); 3426 Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom; 3427 3428 verifyFormat("do {\n" 3429 " foo();\n" 3430 "} while (1);", 3431 Style); 3432 Style.BraceWrapping.BeforeWhile = true; 3433 verifyFormat("do {\n" 3434 " foo();\n" 3435 "}\n" 3436 "while (1);", 3437 Style); 3438 } 3439 3440 //===----------------------------------------------------------------------===// 3441 // Tests for classes, namespaces, etc. 3442 //===----------------------------------------------------------------------===// 3443 3444 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { 3445 verifyFormat("class A {};"); 3446 } 3447 3448 TEST_F(FormatTest, UnderstandsAccessSpecifiers) { 3449 verifyFormat("class A {\n" 3450 "public:\n" 3451 "public: // comment\n" 3452 "protected:\n" 3453 "private:\n" 3454 " void f() {}\n" 3455 "};"); 3456 verifyFormat("export class A {\n" 3457 "public:\n" 3458 "public: // comment\n" 3459 "protected:\n" 3460 "private:\n" 3461 " void f() {}\n" 3462 "};"); 3463 verifyGoogleFormat("class A {\n" 3464 " public:\n" 3465 " protected:\n" 3466 " private:\n" 3467 " void f() {}\n" 3468 "};"); 3469 verifyGoogleFormat("export class A {\n" 3470 " public:\n" 3471 " protected:\n" 3472 " private:\n" 3473 " void f() {}\n" 3474 "};"); 3475 verifyFormat("class A {\n" 3476 "public slots:\n" 3477 " void f1() {}\n" 3478 "public Q_SLOTS:\n" 3479 " void f2() {}\n" 3480 "protected slots:\n" 3481 " void f3() {}\n" 3482 "protected Q_SLOTS:\n" 3483 " void f4() {}\n" 3484 "private slots:\n" 3485 " void f5() {}\n" 3486 "private Q_SLOTS:\n" 3487 " void f6() {}\n" 3488 "signals:\n" 3489 " void g1();\n" 3490 "Q_SIGNALS:\n" 3491 " void g2();\n" 3492 "};"); 3493 3494 // Don't interpret 'signals' the wrong way. 3495 verifyFormat("signals.set();"); 3496 verifyFormat("for (Signals signals : f()) {\n}"); 3497 verifyFormat("{\n" 3498 " signals.set(); // This needs indentation.\n" 3499 "}"); 3500 verifyFormat("void f() {\n" 3501 "label:\n" 3502 " signals.baz();\n" 3503 "}"); 3504 verifyFormat("private[1];"); 3505 verifyFormat("testArray[public] = 1;"); 3506 verifyFormat("public();"); 3507 verifyFormat("myFunc(public);"); 3508 verifyFormat("std::vector<int> testVec = {private};"); 3509 verifyFormat("private.p = 1;"); 3510 verifyFormat("void function(private...) {};"); 3511 verifyFormat("if (private && public)"); 3512 verifyFormat("private &= true;"); 3513 verifyFormat("int x = private * public;"); 3514 verifyFormat("public *= private;"); 3515 verifyFormat("int x = public + private;"); 3516 verifyFormat("private++;"); 3517 verifyFormat("++private;"); 3518 verifyFormat("public += private;"); 3519 verifyFormat("public = public - private;"); 3520 verifyFormat("public->foo();"); 3521 verifyFormat("private--;"); 3522 verifyFormat("--private;"); 3523 verifyFormat("public -= 1;"); 3524 verifyFormat("if (!private && !public)"); 3525 verifyFormat("public != private;"); 3526 verifyFormat("int x = public / private;"); 3527 verifyFormat("public /= 2;"); 3528 verifyFormat("public = public % 2;"); 3529 verifyFormat("public %= 2;"); 3530 verifyFormat("if (public < private)"); 3531 verifyFormat("public << private;"); 3532 verifyFormat("public <<= private;"); 3533 verifyFormat("if (public > private)"); 3534 verifyFormat("public >> private;"); 3535 verifyFormat("public >>= private;"); 3536 verifyFormat("public ^ private;"); 3537 verifyFormat("public ^= private;"); 3538 verifyFormat("public | private;"); 3539 verifyFormat("public |= private;"); 3540 verifyFormat("auto x = private ? 1 : 2;"); 3541 verifyFormat("if (public == private)"); 3542 verifyFormat("void foo(public, private)"); 3543 verifyFormat("public::foo();"); 3544 3545 verifyFormat("class A {\n" 3546 "public:\n" 3547 " std::unique_ptr<int *[]> b() { return nullptr; }\n" 3548 "\n" 3549 "private:\n" 3550 " int c;\n" 3551 "};\n" 3552 "class B {\n" 3553 "public:\n" 3554 " std::unique_ptr<int *[] /* okay */> b() { return nullptr; }\n" 3555 "\n" 3556 "private:\n" 3557 " int c;\n" 3558 "};"); 3559 } 3560 3561 TEST_F(FormatTest, SeparatesLogicalBlocks) { 3562 verifyFormat("class A {\n" 3563 "public:\n" 3564 " void f();\n" 3565 "\n" 3566 "private:\n" 3567 " void g() {}\n" 3568 " // test\n" 3569 "protected:\n" 3570 " int h;\n" 3571 "};", 3572 "class A {\n" 3573 "public:\n" 3574 "void f();\n" 3575 "private:\n" 3576 "void g() {}\n" 3577 "// test\n" 3578 "protected:\n" 3579 "int h;\n" 3580 "};"); 3581 verifyFormat("class A {\n" 3582 "protected:\n" 3583 "public:\n" 3584 " void f();\n" 3585 "};", 3586 "class A {\n" 3587 "protected:\n" 3588 "\n" 3589 "public:\n" 3590 "\n" 3591 " void f();\n" 3592 "};"); 3593 3594 // Even ensure proper spacing inside macros. 3595 verifyFormat("#define B \\\n" 3596 " class A { \\\n" 3597 " protected: \\\n" 3598 " public: \\\n" 3599 " void f(); \\\n" 3600 " };", 3601 "#define B \\\n" 3602 " class A { \\\n" 3603 " protected: \\\n" 3604 " \\\n" 3605 " public: \\\n" 3606 " \\\n" 3607 " void f(); \\\n" 3608 " };", 3609 getGoogleStyle()); 3610 // But don't remove empty lines after macros ending in access specifiers. 3611 verifyFormat("#define A private:\n" 3612 "\n" 3613 "int i;", 3614 "#define A private:\n" 3615 "\n" 3616 "int i;"); 3617 } 3618 3619 TEST_F(FormatTest, FormatsClasses) { 3620 verifyFormat("class A : public B {};"); 3621 verifyFormat("class A : public ::B {};"); 3622 3623 verifyFormat( 3624 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 3625 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 3626 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 3627 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 3628 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 3629 verifyFormat( 3630 "class A : public B, public C, public D, public E, public F {};"); 3631 verifyFormat("class AAAAAAAAAAAA : public B,\n" 3632 " public C,\n" 3633 " public D,\n" 3634 " public E,\n" 3635 " public F,\n" 3636 " public G {};"); 3637 3638 verifyFormat("class\n" 3639 " ReallyReallyLongClassName {\n" 3640 " int i;\n" 3641 "};", 3642 getLLVMStyleWithColumns(32)); 3643 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 3644 " aaaaaaaaaaaaaaaa> {};"); 3645 verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n" 3646 " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n" 3647 " aaaaaaaaaaaaaaaaaaaaaa> {};"); 3648 verifyFormat("template <class R, class C>\n" 3649 "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n" 3650 " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};"); 3651 verifyFormat("class ::A::B {};"); 3652 } 3653 3654 TEST_F(FormatTest, BreakInheritanceStyle) { 3655 FormatStyle StyleWithInheritanceBreakBeforeComma = getLLVMStyle(); 3656 StyleWithInheritanceBreakBeforeComma.BreakInheritanceList = 3657 FormatStyle::BILS_BeforeComma; 3658 verifyFormat("class MyClass : public X {};", 3659 StyleWithInheritanceBreakBeforeComma); 3660 verifyFormat("class MyClass\n" 3661 " : public X\n" 3662 " , public Y {};", 3663 StyleWithInheritanceBreakBeforeComma); 3664 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA\n" 3665 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n" 3666 " , public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};", 3667 StyleWithInheritanceBreakBeforeComma); 3668 verifyFormat("struct aaaaaaaaaaaaa\n" 3669 " : public aaaaaaaaaaaaaaaaaaa< // break\n" 3670 " aaaaaaaaaaaaaaaa> {};", 3671 StyleWithInheritanceBreakBeforeComma); 3672 3673 FormatStyle StyleWithInheritanceBreakAfterColon = getLLVMStyle(); 3674 StyleWithInheritanceBreakAfterColon.BreakInheritanceList = 3675 FormatStyle::BILS_AfterColon; 3676 verifyFormat("class MyClass : public X {};", 3677 StyleWithInheritanceBreakAfterColon); 3678 verifyFormat("class MyClass : public X, public Y {};", 3679 StyleWithInheritanceBreakAfterColon); 3680 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA :\n" 3681 " public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 3682 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};", 3683 StyleWithInheritanceBreakAfterColon); 3684 verifyFormat("struct aaaaaaaaaaaaa :\n" 3685 " public aaaaaaaaaaaaaaaaaaa< // break\n" 3686 " aaaaaaaaaaaaaaaa> {};", 3687 StyleWithInheritanceBreakAfterColon); 3688 3689 FormatStyle StyleWithInheritanceBreakAfterComma = getLLVMStyle(); 3690 StyleWithInheritanceBreakAfterComma.BreakInheritanceList = 3691 FormatStyle::BILS_AfterComma; 3692 verifyFormat("class MyClass : public X {};", 3693 StyleWithInheritanceBreakAfterComma); 3694 verifyFormat("class MyClass : public X,\n" 3695 " public Y {};", 3696 StyleWithInheritanceBreakAfterComma); 3697 verifyFormat( 3698 "class AAAAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 3699 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC " 3700 "{};", 3701 StyleWithInheritanceBreakAfterComma); 3702 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 3703 " aaaaaaaaaaaaaaaa> {};", 3704 StyleWithInheritanceBreakAfterComma); 3705 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 3706 " : public OnceBreak,\n" 3707 " public AlwaysBreak,\n" 3708 " EvenBasesFitInOneLine {};", 3709 StyleWithInheritanceBreakAfterComma); 3710 } 3711 3712 TEST_F(FormatTest, FormatsVariableDeclarationsAfterRecord) { 3713 verifyFormat("class A {\n} a, b;"); 3714 verifyFormat("struct A {\n} a, b;"); 3715 verifyFormat("union A {\n} a, b;"); 3716 3717 verifyFormat("constexpr class A {\n} a, b;"); 3718 verifyFormat("constexpr struct A {\n} a, b;"); 3719 verifyFormat("constexpr union A {\n} a, b;"); 3720 3721 verifyFormat("namespace {\nclass A {\n} a, b;\n} // namespace"); 3722 verifyFormat("namespace {\nstruct A {\n} a, b;\n} // namespace"); 3723 verifyFormat("namespace {\nunion A {\n} a, b;\n} // namespace"); 3724 3725 verifyFormat("namespace {\nconstexpr class A {\n} a, b;\n} // namespace"); 3726 verifyFormat("namespace {\nconstexpr struct A {\n} a, b;\n} // namespace"); 3727 verifyFormat("namespace {\nconstexpr union A {\n} a, b;\n} // namespace"); 3728 3729 verifyFormat("namespace ns {\n" 3730 "class {\n" 3731 "} a, b;\n" 3732 "} // namespace ns"); 3733 verifyFormat("namespace ns {\n" 3734 "const class {\n" 3735 "} a, b;\n" 3736 "} // namespace ns"); 3737 verifyFormat("namespace ns {\n" 3738 "constexpr class C {\n" 3739 "} a, b;\n" 3740 "} // namespace ns"); 3741 verifyFormat("namespace ns {\n" 3742 "class { /* comment */\n" 3743 "} a, b;\n" 3744 "} // namespace ns"); 3745 verifyFormat("namespace ns {\n" 3746 "const class { /* comment */\n" 3747 "} a, b;\n" 3748 "} // namespace ns"); 3749 } 3750 3751 TEST_F(FormatTest, FormatsEnum) { 3752 verifyFormat("enum {\n" 3753 " Zero,\n" 3754 " One = 1,\n" 3755 " Two = One + 1,\n" 3756 " Three = (One + Two),\n" 3757 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 3758 " Five = (One, Two, Three, Four, 5)\n" 3759 "};"); 3760 verifyGoogleFormat("enum {\n" 3761 " Zero,\n" 3762 " One = 1,\n" 3763 " Two = One + 1,\n" 3764 " Three = (One + Two),\n" 3765 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 3766 " Five = (One, Two, Three, Four, 5)\n" 3767 "};"); 3768 verifyFormat("enum Enum {};"); 3769 verifyFormat("enum {};"); 3770 verifyFormat("enum X E {} d;"); 3771 verifyFormat("enum __attribute__((...)) E {} d;"); 3772 verifyFormat("enum __declspec__((...)) E {} d;"); 3773 verifyFormat("enum [[nodiscard]] E {} d;"); 3774 verifyFormat("enum {\n" 3775 " Bar = Foo<int, int>::value\n" 3776 "};", 3777 getLLVMStyleWithColumns(30)); 3778 3779 verifyFormat("enum ShortEnum { A, B, C };"); 3780 verifyGoogleFormat("enum ShortEnum { A, B, C };"); 3781 3782 verifyFormat("enum KeepEmptyLines {\n" 3783 " ONE,\n" 3784 "\n" 3785 " TWO,\n" 3786 "\n" 3787 " THREE\n" 3788 "}", 3789 "enum KeepEmptyLines {\n" 3790 " ONE,\n" 3791 "\n" 3792 " TWO,\n" 3793 "\n" 3794 "\n" 3795 " THREE\n" 3796 "}"); 3797 verifyFormat("enum E { // comment\n" 3798 " ONE,\n" 3799 " TWO\n" 3800 "};\n" 3801 "int i;"); 3802 3803 FormatStyle EightIndent = getLLVMStyle(); 3804 EightIndent.IndentWidth = 8; 3805 verifyFormat("enum {\n" 3806 " VOID,\n" 3807 " CHAR,\n" 3808 " SHORT,\n" 3809 " INT,\n" 3810 " LONG,\n" 3811 " SIGNED,\n" 3812 " UNSIGNED,\n" 3813 " BOOL,\n" 3814 " FLOAT,\n" 3815 " DOUBLE,\n" 3816 " COMPLEX\n" 3817 "};", 3818 EightIndent); 3819 3820 verifyFormat("enum [[nodiscard]] E {\n" 3821 " ONE,\n" 3822 " TWO,\n" 3823 "};"); 3824 verifyFormat("enum [[nodiscard]] E {\n" 3825 " // Comment 1\n" 3826 " ONE,\n" 3827 " // Comment 2\n" 3828 " TWO,\n" 3829 "};"); 3830 verifyFormat("enum [[clang::enum_extensibility(open)]] E {\n" 3831 " // Comment 1\n" 3832 " ONE,\n" 3833 " // Comment 2\n" 3834 " TWO\n" 3835 "};"); 3836 verifyFormat("enum [[nodiscard]] [[clang::enum_extensibility(open)]] E {\n" 3837 " // Comment 1\n" 3838 " ONE,\n" 3839 " // Comment 2\n" 3840 " TWO\n" 3841 "};"); 3842 verifyFormat("enum [[clang::enum_extensibility(open)]] E { // foo\n" 3843 " A,\n" 3844 " // bar\n" 3845 " B\n" 3846 "};", 3847 "enum [[clang::enum_extensibility(open)]] E{// foo\n" 3848 " A,\n" 3849 " // bar\n" 3850 " B};"); 3851 3852 // Not enums. 3853 verifyFormat("enum X f() {\n" 3854 " a();\n" 3855 " return 42;\n" 3856 "}"); 3857 verifyFormat("enum X Type::f() {\n" 3858 " a();\n" 3859 " return 42;\n" 3860 "}"); 3861 verifyFormat("enum ::X f() {\n" 3862 " a();\n" 3863 " return 42;\n" 3864 "}"); 3865 verifyFormat("enum ns::X f() {\n" 3866 " a();\n" 3867 " return 42;\n" 3868 "}"); 3869 } 3870 3871 TEST_F(FormatTest, FormatsEnumsWithErrors) { 3872 verifyFormat("enum Type {\n" 3873 " One = 0; // These semicolons should be commas.\n" 3874 " Two = 1;\n" 3875 "};"); 3876 verifyFormat("namespace n {\n" 3877 "enum Type {\n" 3878 " One,\n" 3879 " Two, // missing };\n" 3880 " int i;\n" 3881 "}\n" 3882 "void g() {}"); 3883 } 3884 3885 TEST_F(FormatTest, FormatsEnumStruct) { 3886 verifyFormat("enum struct {\n" 3887 " Zero,\n" 3888 " One = 1,\n" 3889 " Two = One + 1,\n" 3890 " Three = (One + Two),\n" 3891 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 3892 " Five = (One, Two, Three, Four, 5)\n" 3893 "};"); 3894 verifyFormat("enum struct Enum {};"); 3895 verifyFormat("enum struct {};"); 3896 verifyFormat("enum struct X E {} d;"); 3897 verifyFormat("enum struct __attribute__((...)) E {} d;"); 3898 verifyFormat("enum struct __declspec__((...)) E {} d;"); 3899 verifyFormat("enum struct [[nodiscard]] E {} d;"); 3900 verifyFormat("enum struct X f() {\n a();\n return 42;\n}"); 3901 3902 verifyFormat("enum struct [[nodiscard]] E {\n" 3903 " ONE,\n" 3904 " TWO,\n" 3905 "};"); 3906 verifyFormat("enum struct [[nodiscard]] E {\n" 3907 " // Comment 1\n" 3908 " ONE,\n" 3909 " // Comment 2\n" 3910 " TWO,\n" 3911 "};"); 3912 } 3913 3914 TEST_F(FormatTest, FormatsEnumClass) { 3915 verifyFormat("enum class {\n" 3916 " Zero,\n" 3917 " One = 1,\n" 3918 " Two = One + 1,\n" 3919 " Three = (One + Two),\n" 3920 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 3921 " Five = (One, Two, Three, Four, 5)\n" 3922 "};"); 3923 verifyFormat("enum class Enum {};"); 3924 verifyFormat("enum class {};"); 3925 verifyFormat("enum class X E {} d;"); 3926 verifyFormat("enum class __attribute__((...)) E {} d;"); 3927 verifyFormat("enum class __declspec__((...)) E {} d;"); 3928 verifyFormat("enum class [[nodiscard]] E {} d;"); 3929 verifyFormat("enum class X f() {\n a();\n return 42;\n}"); 3930 3931 verifyFormat("enum class [[nodiscard]] E {\n" 3932 " ONE,\n" 3933 " TWO,\n" 3934 "};"); 3935 verifyFormat("enum class [[nodiscard]] E {\n" 3936 " // Comment 1\n" 3937 " ONE,\n" 3938 " // Comment 2\n" 3939 " TWO,\n" 3940 "};"); 3941 } 3942 3943 TEST_F(FormatTest, FormatsEnumTypes) { 3944 verifyFormat("enum X : int {\n" 3945 " A, // Force multiple lines.\n" 3946 " B\n" 3947 "};"); 3948 verifyFormat("enum X : int { A, B };"); 3949 verifyFormat("enum X : std::uint32_t { A, B };"); 3950 } 3951 3952 TEST_F(FormatTest, FormatsTypedefEnum) { 3953 FormatStyle Style = getLLVMStyleWithColumns(40); 3954 verifyFormat("typedef enum {} EmptyEnum;"); 3955 verifyFormat("typedef enum { A, B, C } ShortEnum;"); 3956 verifyFormat("typedef enum {\n" 3957 " ZERO = 0,\n" 3958 " ONE = 1,\n" 3959 " TWO = 2,\n" 3960 " THREE = 3\n" 3961 "} LongEnum;", 3962 Style); 3963 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 3964 Style.BraceWrapping.AfterEnum = true; 3965 verifyFormat("typedef enum {} EmptyEnum;"); 3966 verifyFormat("typedef enum { A, B, C } ShortEnum;"); 3967 verifyFormat("typedef enum\n" 3968 "{\n" 3969 " ZERO = 0,\n" 3970 " ONE = 1,\n" 3971 " TWO = 2,\n" 3972 " THREE = 3\n" 3973 "} LongEnum;", 3974 Style); 3975 } 3976 3977 TEST_F(FormatTest, FormatsNSEnums) { 3978 verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }"); 3979 verifyGoogleFormat( 3980 "typedef NS_CLOSED_ENUM(NSInteger, SomeName) { AAA, BBB }"); 3981 verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n" 3982 " // Information about someDecentlyLongValue.\n" 3983 " someDecentlyLongValue,\n" 3984 " // Information about anotherDecentlyLongValue.\n" 3985 " anotherDecentlyLongValue,\n" 3986 " // Information about aThirdDecentlyLongValue.\n" 3987 " aThirdDecentlyLongValue\n" 3988 "};"); 3989 verifyGoogleFormat("typedef NS_CLOSED_ENUM(NSInteger, MyType) {\n" 3990 " // Information about someDecentlyLongValue.\n" 3991 " someDecentlyLongValue,\n" 3992 " // Information about anotherDecentlyLongValue.\n" 3993 " anotherDecentlyLongValue,\n" 3994 " // Information about aThirdDecentlyLongValue.\n" 3995 " aThirdDecentlyLongValue\n" 3996 "};"); 3997 verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n" 3998 " a = 1,\n" 3999 " b = 2,\n" 4000 " c = 3,\n" 4001 "};"); 4002 verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n" 4003 " a = 1,\n" 4004 " b = 2,\n" 4005 " c = 3,\n" 4006 "};"); 4007 verifyGoogleFormat("typedef CF_CLOSED_ENUM(NSInteger, MyType) {\n" 4008 " a = 1,\n" 4009 " b = 2,\n" 4010 " c = 3,\n" 4011 "};"); 4012 verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n" 4013 " a = 1,\n" 4014 " b = 2,\n" 4015 " c = 3,\n" 4016 "};"); 4017 } 4018 4019 TEST_F(FormatTest, FormatsBitfields) { 4020 verifyFormat("struct Bitfields {\n" 4021 " unsigned sClass : 8;\n" 4022 " unsigned ValueKind : 2;\n" 4023 "};"); 4024 verifyFormat("struct A {\n" 4025 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n" 4026 " bbbbbbbbbbbbbbbbbbbbbbbbb;\n" 4027 "};"); 4028 verifyFormat("struct MyStruct {\n" 4029 " uchar data;\n" 4030 " uchar : 8;\n" 4031 " uchar : 8;\n" 4032 " uchar other;\n" 4033 "};"); 4034 FormatStyle Style = getLLVMStyle(); 4035 Style.BitFieldColonSpacing = FormatStyle::BFCS_None; 4036 verifyFormat("struct Bitfields {\n" 4037 " unsigned sClass:8;\n" 4038 " unsigned ValueKind:2;\n" 4039 " uchar other;\n" 4040 "};", 4041 Style); 4042 verifyFormat("struct A {\n" 4043 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:1,\n" 4044 " bbbbbbbbbbbbbbbbbbbbbbbbb:2;\n" 4045 "};", 4046 Style); 4047 Style.BitFieldColonSpacing = FormatStyle::BFCS_Before; 4048 verifyFormat("struct Bitfields {\n" 4049 " unsigned sClass :8;\n" 4050 " unsigned ValueKind :2;\n" 4051 " uchar other;\n" 4052 "};", 4053 Style); 4054 Style.BitFieldColonSpacing = FormatStyle::BFCS_After; 4055 verifyFormat("struct Bitfields {\n" 4056 " unsigned sClass: 8;\n" 4057 " unsigned ValueKind: 2;\n" 4058 " uchar other;\n" 4059 "};", 4060 Style); 4061 } 4062 4063 TEST_F(FormatTest, FormatsNamespaces) { 4064 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 4065 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 4066 4067 verifyFormat("namespace some_namespace {\n" 4068 "class A {};\n" 4069 "void f() { f(); }\n" 4070 "}", 4071 LLVMWithNoNamespaceFix); 4072 verifyFormat("#define M(x) x##x\n" 4073 "namespace M(x) {\n" 4074 "class A {};\n" 4075 "void f() { f(); }\n" 4076 "}", 4077 LLVMWithNoNamespaceFix); 4078 verifyFormat("#define M(x) x##x\n" 4079 "namespace N::inline M(x) {\n" 4080 "class A {};\n" 4081 "void f() { f(); }\n" 4082 "}", 4083 LLVMWithNoNamespaceFix); 4084 verifyFormat("#define M(x) x##x\n" 4085 "namespace M(x)::inline N {\n" 4086 "class A {};\n" 4087 "void f() { f(); }\n" 4088 "}", 4089 LLVMWithNoNamespaceFix); 4090 verifyFormat("#define M(x) x##x\n" 4091 "namespace N::M(x) {\n" 4092 "class A {};\n" 4093 "void f() { f(); }\n" 4094 "}", 4095 LLVMWithNoNamespaceFix); 4096 verifyFormat("#define M(x) x##x\n" 4097 "namespace M::N(x) {\n" 4098 "class A {};\n" 4099 "void f() { f(); }\n" 4100 "}", 4101 LLVMWithNoNamespaceFix); 4102 verifyFormat("namespace N::inline D {\n" 4103 "class A {};\n" 4104 "void f() { f(); }\n" 4105 "}", 4106 LLVMWithNoNamespaceFix); 4107 verifyFormat("namespace N::inline D::E {\n" 4108 "class A {};\n" 4109 "void f() { f(); }\n" 4110 "}", 4111 LLVMWithNoNamespaceFix); 4112 verifyFormat("namespace [[deprecated(\"foo[bar\")]] some_namespace {\n" 4113 "class A {};\n" 4114 "void f() { f(); }\n" 4115 "}", 4116 LLVMWithNoNamespaceFix); 4117 verifyFormat("/* something */ namespace some_namespace {\n" 4118 "class A {};\n" 4119 "void f() { f(); }\n" 4120 "}", 4121 LLVMWithNoNamespaceFix); 4122 verifyFormat("namespace {\n" 4123 "class A {};\n" 4124 "void f() { f(); }\n" 4125 "}", 4126 LLVMWithNoNamespaceFix); 4127 verifyFormat("/* something */ namespace {\n" 4128 "class A {};\n" 4129 "void f() { f(); }\n" 4130 "}", 4131 LLVMWithNoNamespaceFix); 4132 verifyFormat("inline namespace X {\n" 4133 "class A {};\n" 4134 "void f() { f(); }\n" 4135 "}", 4136 LLVMWithNoNamespaceFix); 4137 verifyFormat("/* something */ inline namespace X {\n" 4138 "class A {};\n" 4139 "void f() { f(); }\n" 4140 "}", 4141 LLVMWithNoNamespaceFix); 4142 verifyFormat("export namespace X {\n" 4143 "class A {};\n" 4144 "void f() { f(); }\n" 4145 "}", 4146 LLVMWithNoNamespaceFix); 4147 verifyFormat("using namespace some_namespace;\n" 4148 "class A {};\n" 4149 "void f() { f(); }", 4150 LLVMWithNoNamespaceFix); 4151 4152 // This code is more common than we thought; if we 4153 // layout this correctly the semicolon will go into 4154 // its own line, which is undesirable. 4155 verifyFormat("namespace {};", LLVMWithNoNamespaceFix); 4156 verifyFormat("namespace {\n" 4157 "class A {};\n" 4158 "};", 4159 LLVMWithNoNamespaceFix); 4160 4161 verifyFormat("namespace {\n" 4162 "int SomeVariable = 0; // comment\n" 4163 "} // namespace", 4164 LLVMWithNoNamespaceFix); 4165 verifyFormat("#ifndef HEADER_GUARD\n" 4166 "#define HEADER_GUARD\n" 4167 "namespace my_namespace {\n" 4168 "int i;\n" 4169 "} // my_namespace\n" 4170 "#endif // HEADER_GUARD", 4171 "#ifndef HEADER_GUARD\n" 4172 " #define HEADER_GUARD\n" 4173 " namespace my_namespace {\n" 4174 "int i;\n" 4175 "} // my_namespace\n" 4176 "#endif // HEADER_GUARD", 4177 LLVMWithNoNamespaceFix); 4178 4179 verifyFormat("namespace A::B {\n" 4180 "class C {};\n" 4181 "}", 4182 LLVMWithNoNamespaceFix); 4183 4184 FormatStyle Style = getLLVMStyle(); 4185 Style.NamespaceIndentation = FormatStyle::NI_All; 4186 verifyFormat("namespace out {\n" 4187 " int i;\n" 4188 " namespace in {\n" 4189 " int i;\n" 4190 " } // namespace in\n" 4191 "} // namespace out", 4192 "namespace out {\n" 4193 "int i;\n" 4194 "namespace in {\n" 4195 "int i;\n" 4196 "} // namespace in\n" 4197 "} // namespace out", 4198 Style); 4199 4200 FormatStyle ShortInlineFunctions = getLLVMStyle(); 4201 ShortInlineFunctions.NamespaceIndentation = FormatStyle::NI_All; 4202 ShortInlineFunctions.AllowShortFunctionsOnASingleLine = 4203 FormatStyle::SFS_Inline; 4204 verifyFormat("namespace {\n" 4205 " void f() {\n" 4206 " return;\n" 4207 " }\n" 4208 "} // namespace", 4209 ShortInlineFunctions); 4210 verifyFormat("namespace { /* comment */\n" 4211 " void f() {\n" 4212 " return;\n" 4213 " }\n" 4214 "} // namespace", 4215 ShortInlineFunctions); 4216 verifyFormat("namespace { // comment\n" 4217 " void f() {\n" 4218 " return;\n" 4219 " }\n" 4220 "} // namespace", 4221 ShortInlineFunctions); 4222 verifyFormat("namespace {\n" 4223 " int some_int;\n" 4224 " void f() {\n" 4225 " return;\n" 4226 " }\n" 4227 "} // namespace", 4228 ShortInlineFunctions); 4229 verifyFormat("namespace interface {\n" 4230 " void f() {\n" 4231 " return;\n" 4232 " }\n" 4233 "} // namespace interface", 4234 ShortInlineFunctions); 4235 verifyFormat("namespace {\n" 4236 " class X {\n" 4237 " void f() { return; }\n" 4238 " };\n" 4239 "} // namespace", 4240 ShortInlineFunctions); 4241 verifyFormat("namespace {\n" 4242 " class X { /* comment */\n" 4243 " void f() { return; }\n" 4244 " };\n" 4245 "} // namespace", 4246 ShortInlineFunctions); 4247 verifyFormat("namespace {\n" 4248 " class X { // comment\n" 4249 " void f() { return; }\n" 4250 " };\n" 4251 "} // namespace", 4252 ShortInlineFunctions); 4253 verifyFormat("namespace {\n" 4254 " struct X {\n" 4255 " void f() { return; }\n" 4256 " };\n" 4257 "} // namespace", 4258 ShortInlineFunctions); 4259 verifyFormat("namespace {\n" 4260 " union X {\n" 4261 " void f() { return; }\n" 4262 " };\n" 4263 "} // namespace", 4264 ShortInlineFunctions); 4265 verifyFormat("extern \"C\" {\n" 4266 "void f() {\n" 4267 " return;\n" 4268 "}\n" 4269 "} // namespace", 4270 ShortInlineFunctions); 4271 verifyFormat("namespace {\n" 4272 " class X {\n" 4273 " void f() { return; }\n" 4274 " } x;\n" 4275 "} // namespace", 4276 ShortInlineFunctions); 4277 verifyFormat("namespace {\n" 4278 " [[nodiscard]] class X {\n" 4279 " void f() { return; }\n" 4280 " };\n" 4281 "} // namespace", 4282 ShortInlineFunctions); 4283 verifyFormat("namespace {\n" 4284 " static class X {\n" 4285 " void f() { return; }\n" 4286 " } x;\n" 4287 "} // namespace", 4288 ShortInlineFunctions); 4289 verifyFormat("namespace {\n" 4290 " constexpr class X {\n" 4291 " void f() { return; }\n" 4292 " } x;\n" 4293 "} // namespace", 4294 ShortInlineFunctions); 4295 4296 ShortInlineFunctions.IndentExternBlock = FormatStyle::IEBS_Indent; 4297 verifyFormat("extern \"C\" {\n" 4298 " void f() {\n" 4299 " return;\n" 4300 " }\n" 4301 "} // namespace", 4302 ShortInlineFunctions); 4303 4304 Style.NamespaceIndentation = FormatStyle::NI_Inner; 4305 verifyFormat("namespace out {\n" 4306 "int i;\n" 4307 "namespace in {\n" 4308 " int i;\n" 4309 "} // namespace in\n" 4310 "} // namespace out", 4311 "namespace out {\n" 4312 "int i;\n" 4313 "namespace in {\n" 4314 "int i;\n" 4315 "} // namespace in\n" 4316 "} // namespace out", 4317 Style); 4318 4319 Style.NamespaceIndentation = FormatStyle::NI_None; 4320 verifyFormat("template <class T>\n" 4321 "concept a_concept = X<>;\n" 4322 "namespace B {\n" 4323 "struct b_struct {};\n" 4324 "} // namespace B", 4325 Style); 4326 verifyFormat("template <int I>\n" 4327 "constexpr void foo()\n" 4328 " requires(I == 42)\n" 4329 "{}\n" 4330 "namespace ns {\n" 4331 "void foo() {}\n" 4332 "} // namespace ns", 4333 Style); 4334 4335 FormatStyle LLVMWithCompactInnerNamespace = getLLVMStyle(); 4336 LLVMWithCompactInnerNamespace.CompactNamespaces = true; 4337 LLVMWithCompactInnerNamespace.NamespaceIndentation = FormatStyle::NI_Inner; 4338 verifyFormat("namespace ns1 { namespace ns2 { namespace ns3 {\n" 4339 "// block for debug mode\n" 4340 "#ifndef NDEBUG\n" 4341 "#endif\n" 4342 "}}} // namespace ns1::ns2::ns3", 4343 LLVMWithCompactInnerNamespace); 4344 } 4345 4346 TEST_F(FormatTest, NamespaceMacros) { 4347 FormatStyle Style = getLLVMStyle(); 4348 Style.NamespaceMacros.push_back("TESTSUITE"); 4349 4350 verifyFormat("TESTSUITE(A) {\n" 4351 "int foo();\n" 4352 "} // TESTSUITE(A)", 4353 Style); 4354 4355 verifyFormat("TESTSUITE(A, B) {\n" 4356 "int foo();\n" 4357 "} // TESTSUITE(A)", 4358 Style); 4359 4360 // Properly indent according to NamespaceIndentation style 4361 Style.NamespaceIndentation = FormatStyle::NI_All; 4362 verifyFormat("TESTSUITE(A) {\n" 4363 " int foo();\n" 4364 "} // TESTSUITE(A)", 4365 Style); 4366 verifyFormat("TESTSUITE(A) {\n" 4367 " namespace B {\n" 4368 " int foo();\n" 4369 " } // namespace B\n" 4370 "} // TESTSUITE(A)", 4371 Style); 4372 verifyFormat("namespace A {\n" 4373 " TESTSUITE(B) {\n" 4374 " int foo();\n" 4375 " } // TESTSUITE(B)\n" 4376 "} // namespace A", 4377 Style); 4378 4379 Style.NamespaceIndentation = FormatStyle::NI_Inner; 4380 verifyFormat("TESTSUITE(A) {\n" 4381 "TESTSUITE(B) {\n" 4382 " int foo();\n" 4383 "} // TESTSUITE(B)\n" 4384 "} // TESTSUITE(A)", 4385 Style); 4386 verifyFormat("TESTSUITE(A) {\n" 4387 "namespace B {\n" 4388 " int foo();\n" 4389 "} // namespace B\n" 4390 "} // TESTSUITE(A)", 4391 Style); 4392 verifyFormat("namespace A {\n" 4393 "TESTSUITE(B) {\n" 4394 " int foo();\n" 4395 "} // TESTSUITE(B)\n" 4396 "} // namespace A", 4397 Style); 4398 4399 // Properly merge namespace-macros blocks in CompactNamespaces mode 4400 Style.NamespaceIndentation = FormatStyle::NI_None; 4401 Style.CompactNamespaces = true; 4402 verifyFormat("TESTSUITE(A) { TESTSUITE(B) {\n" 4403 "}} // TESTSUITE(A::B)", 4404 Style); 4405 4406 verifyFormat("TESTSUITE(out) { TESTSUITE(in) {\n" 4407 "}} // TESTSUITE(out::in)", 4408 "TESTSUITE(out) {\n" 4409 "TESTSUITE(in) {\n" 4410 "} // TESTSUITE(in)\n" 4411 "} // TESTSUITE(out)", 4412 Style); 4413 4414 verifyFormat("TESTSUITE(out) { TESTSUITE(in) {\n" 4415 "}} // TESTSUITE(out::in)", 4416 "TESTSUITE(out) {\n" 4417 "TESTSUITE(in) {\n" 4418 "} // TESTSUITE(in)\n" 4419 "} // TESTSUITE(out)", 4420 Style); 4421 4422 // Do not merge different namespaces/macros 4423 verifyFormat("namespace out {\n" 4424 "TESTSUITE(in) {\n" 4425 "} // TESTSUITE(in)\n" 4426 "} // namespace out", 4427 Style); 4428 verifyFormat("TESTSUITE(out) {\n" 4429 "namespace in {\n" 4430 "} // namespace in\n" 4431 "} // TESTSUITE(out)", 4432 Style); 4433 Style.NamespaceMacros.push_back("FOOBAR"); 4434 verifyFormat("TESTSUITE(out) {\n" 4435 "FOOBAR(in) {\n" 4436 "} // FOOBAR(in)\n" 4437 "} // TESTSUITE(out)", 4438 Style); 4439 } 4440 4441 TEST_F(FormatTest, FormatsCompactNamespaces) { 4442 FormatStyle Style = getLLVMStyle(); 4443 Style.CompactNamespaces = true; 4444 Style.NamespaceMacros.push_back("TESTSUITE"); 4445 4446 verifyFormat("namespace A { namespace B {\n" 4447 "}} // namespace A::B", 4448 Style); 4449 4450 verifyFormat("namespace out { namespace in {\n" 4451 "}} // namespace out::in", 4452 "namespace out {\n" 4453 "namespace in {\n" 4454 "} // namespace in\n" 4455 "} // namespace out", 4456 Style); 4457 4458 // Only namespaces which have both consecutive opening and end get compacted 4459 verifyFormat("namespace out {\n" 4460 "namespace in1 {\n" 4461 "} // namespace in1\n" 4462 "namespace in2 {\n" 4463 "} // namespace in2\n" 4464 "} // namespace out", 4465 Style); 4466 4467 verifyFormat("namespace out {\n" 4468 "int i;\n" 4469 "namespace in {\n" 4470 "int j;\n" 4471 "} // namespace in\n" 4472 "int k;\n" 4473 "} // namespace out", 4474 "namespace out { int i;\n" 4475 "namespace in { int j; } // namespace in\n" 4476 "int k; } // namespace out", 4477 Style); 4478 4479 verifyFormat("namespace A { namespace B { namespace C {\n" 4480 "}}} // namespace A::B::C", 4481 "namespace A { namespace B {\n" 4482 "namespace C {\n" 4483 "}} // namespace B::C\n" 4484 "} // namespace A", 4485 Style); 4486 4487 Style.ColumnLimit = 40; 4488 verifyFormat("namespace aaaaaaaaaa {\n" 4489 "namespace bbbbbbbbbb {\n" 4490 "}} // namespace aaaaaaaaaa::bbbbbbbbbb", 4491 "namespace aaaaaaaaaa {\n" 4492 "namespace bbbbbbbbbb {\n" 4493 "} // namespace bbbbbbbbbb\n" 4494 "} // namespace aaaaaaaaaa", 4495 Style); 4496 4497 verifyFormat("namespace aaaaaa { namespace bbbbbb {\n" 4498 "namespace cccccc {\n" 4499 "}}} // namespace aaaaaa::bbbbbb::cccccc", 4500 "namespace aaaaaa {\n" 4501 "namespace bbbbbb {\n" 4502 "namespace cccccc {\n" 4503 "} // namespace cccccc\n" 4504 "} // namespace bbbbbb\n" 4505 "} // namespace aaaaaa", 4506 Style); 4507 Style.ColumnLimit = 80; 4508 4509 // Extra semicolon after 'inner' closing brace prevents merging 4510 verifyFormat("namespace out { namespace in {\n" 4511 "}; } // namespace out::in", 4512 "namespace out {\n" 4513 "namespace in {\n" 4514 "}; // namespace in\n" 4515 "} // namespace out", 4516 Style); 4517 4518 // Extra semicolon after 'outer' closing brace is conserved 4519 verifyFormat("namespace out { namespace in {\n" 4520 "}}; // namespace out::in", 4521 "namespace out {\n" 4522 "namespace in {\n" 4523 "} // namespace in\n" 4524 "}; // namespace out", 4525 Style); 4526 4527 Style.NamespaceIndentation = FormatStyle::NI_All; 4528 verifyFormat("namespace out { namespace in {\n" 4529 " int i;\n" 4530 "}} // namespace out::in", 4531 "namespace out {\n" 4532 "namespace in {\n" 4533 "int i;\n" 4534 "} // namespace in\n" 4535 "} // namespace out", 4536 Style); 4537 verifyFormat("namespace out { namespace mid {\n" 4538 " namespace in {\n" 4539 " int j;\n" 4540 " } // namespace in\n" 4541 " int k;\n" 4542 "}} // namespace out::mid", 4543 "namespace out { namespace mid {\n" 4544 "namespace in { int j; } // namespace in\n" 4545 "int k; }} // namespace out::mid", 4546 Style); 4547 4548 verifyFormat("namespace A { namespace B { namespace C {\n" 4549 " int i;\n" 4550 "}}} // namespace A::B::C\n" 4551 "int main() {\n" 4552 " if (true)\n" 4553 " return 0;\n" 4554 "}", 4555 "namespace A { namespace B {\n" 4556 "namespace C {\n" 4557 " int i;\n" 4558 "}} // namespace B::C\n" 4559 "} // namespace A\n" 4560 "int main() {\n" 4561 " if (true)\n" 4562 " return 0;\n" 4563 "}", 4564 Style); 4565 4566 verifyFormat("namespace A { namespace B { namespace C {\n" 4567 "#ifdef FOO\n" 4568 " int i;\n" 4569 "#endif\n" 4570 "}}} // namespace A::B::C\n" 4571 "int main() {\n" 4572 " if (true)\n" 4573 " return 0;\n" 4574 "}", 4575 "namespace A { namespace B {\n" 4576 "namespace C {\n" 4577 "#ifdef FOO\n" 4578 " int i;\n" 4579 "#endif\n" 4580 "}} // namespace B::C\n" 4581 "} // namespace A\n" 4582 "int main() {\n" 4583 " if (true)\n" 4584 " return 0;\n" 4585 "}", 4586 Style); 4587 4588 Style.NamespaceIndentation = FormatStyle::NI_Inner; 4589 verifyFormat("namespace out { namespace in {\n" 4590 " int i;\n" 4591 "}} // namespace out::in", 4592 "namespace out {\n" 4593 "namespace in {\n" 4594 "int i;\n" 4595 "} // namespace in\n" 4596 "} // namespace out", 4597 Style); 4598 verifyFormat("namespace out { namespace mid { namespace in {\n" 4599 " int i;\n" 4600 "}}} // namespace out::mid::in", 4601 "namespace out {\n" 4602 "namespace mid {\n" 4603 "namespace in {\n" 4604 "int i;\n" 4605 "} // namespace in\n" 4606 "} // namespace mid\n" 4607 "} // namespace out", 4608 Style); 4609 4610 Style.CompactNamespaces = true; 4611 Style.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 4612 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 4613 Style.BraceWrapping.BeforeLambdaBody = true; 4614 verifyFormat("namespace out { namespace in {\n" 4615 "}} // namespace out::in", 4616 Style); 4617 verifyFormat("namespace out { namespace in {\n" 4618 "}} // namespace out::in", 4619 "namespace out {\n" 4620 "namespace in {\n" 4621 "} // namespace in\n" 4622 "} // namespace out", 4623 Style); 4624 } 4625 4626 TEST_F(FormatTest, FormatsExternC) { 4627 verifyFormat("extern \"C\" {\nint a;"); 4628 verifyFormat("extern \"C\" {}"); 4629 verifyFormat("extern \"C\" {\n" 4630 "int foo();\n" 4631 "}"); 4632 verifyFormat("extern \"C\" int foo() {}"); 4633 verifyFormat("extern \"C\" int foo();"); 4634 verifyFormat("extern \"C\" int foo() {\n" 4635 " int i = 42;\n" 4636 " return i;\n" 4637 "}"); 4638 4639 FormatStyle Style = getLLVMStyle(); 4640 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 4641 Style.BraceWrapping.AfterFunction = true; 4642 verifyFormat("extern \"C\" int foo() {}", Style); 4643 verifyFormat("extern \"C\" int foo();", Style); 4644 verifyFormat("extern \"C\" int foo()\n" 4645 "{\n" 4646 " int i = 42;\n" 4647 " return i;\n" 4648 "}", 4649 Style); 4650 4651 Style.BraceWrapping.AfterExternBlock = true; 4652 Style.BraceWrapping.SplitEmptyRecord = false; 4653 verifyFormat("extern \"C\"\n" 4654 "{}", 4655 Style); 4656 verifyFormat("extern \"C\"\n" 4657 "{\n" 4658 " int foo();\n" 4659 "}", 4660 Style); 4661 } 4662 4663 TEST_F(FormatTest, IndentExternBlockStyle) { 4664 FormatStyle Style = getLLVMStyle(); 4665 Style.IndentWidth = 2; 4666 4667 Style.IndentExternBlock = FormatStyle::IEBS_Indent; 4668 verifyFormat("extern \"C\" { /*9*/\n" 4669 "}", 4670 Style); 4671 verifyFormat("extern \"C\" {\n" 4672 " int foo10();\n" 4673 "}", 4674 Style); 4675 4676 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent; 4677 verifyFormat("extern \"C\" { /*11*/\n" 4678 "}", 4679 Style); 4680 verifyFormat("extern \"C\" {\n" 4681 "int foo12();\n" 4682 "}", 4683 Style); 4684 4685 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 4686 verifyFormat("extern \"C\"\n" 4687 "{\n" 4688 "int i;\n" 4689 "}", 4690 Style); 4691 4692 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 4693 Style.BraceWrapping.AfterExternBlock = true; 4694 Style.IndentExternBlock = FormatStyle::IEBS_Indent; 4695 verifyFormat("extern \"C\"\n" 4696 "{ /*13*/\n" 4697 "}", 4698 Style); 4699 verifyFormat("extern \"C\"\n{\n" 4700 " int foo14();\n" 4701 "}", 4702 Style); 4703 4704 Style.BraceWrapping.AfterExternBlock = false; 4705 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent; 4706 verifyFormat("extern \"C\" { /*15*/\n" 4707 "}", 4708 Style); 4709 verifyFormat("extern \"C\" {\n" 4710 "int foo16();\n" 4711 "}", 4712 Style); 4713 4714 Style.BraceWrapping.AfterExternBlock = true; 4715 verifyFormat("extern \"C\"\n" 4716 "{ /*13*/\n" 4717 "}", 4718 Style); 4719 verifyFormat("extern \"C\"\n" 4720 "{\n" 4721 "int foo14();\n" 4722 "}", 4723 Style); 4724 4725 Style.IndentExternBlock = FormatStyle::IEBS_Indent; 4726 verifyFormat("extern \"C\"\n" 4727 "{ /*13*/\n" 4728 "}", 4729 Style); 4730 verifyFormat("extern \"C\"\n" 4731 "{\n" 4732 " int foo14();\n" 4733 "}", 4734 Style); 4735 } 4736 4737 TEST_F(FormatTest, FormatsInlineASM) { 4738 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"); 4739 verifyFormat("asm(\"nop\" ::: \"memory\");"); 4740 verifyFormat( 4741 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" 4742 " \"cpuid\\n\\t\"\n" 4743 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n" 4744 " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n" 4745 " : \"a\"(value));"); 4746 verifyFormat( 4747 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 4748 " __asm {\n" 4749 " mov edx,[that] // vtable in edx\n" 4750 " mov eax,methodIndex\n" 4751 " call [edx][eax*4] // stdcall\n" 4752 " }\n" 4753 "}", 4754 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 4755 " __asm {\n" 4756 " mov edx,[that] // vtable in edx\n" 4757 " mov eax,methodIndex\n" 4758 " call [edx][eax*4] // stdcall\n" 4759 " }\n" 4760 "}"); 4761 verifyNoChange("_asm {\n" 4762 " xor eax, eax;\n" 4763 " cpuid;\n" 4764 "}"); 4765 verifyFormat("void function() {\n" 4766 " // comment\n" 4767 " asm(\"\");\n" 4768 "}"); 4769 verifyFormat("__asm {\n" 4770 "}\n" 4771 "int i;", 4772 "__asm {\n" 4773 "}\n" 4774 "int i;"); 4775 4776 auto Style = getLLVMStyleWithColumns(0); 4777 const StringRef Code1{"asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"}; 4778 const StringRef Code2{"asm(\"xyz\"\n" 4779 " : \"=a\"(a), \"=d\"(b)\n" 4780 " : \"a\"(data));"}; 4781 const StringRef Code3{"asm(\"xyz\" : \"=a\"(a), \"=d\"(b)\n" 4782 " : \"a\"(data));"}; 4783 4784 Style.BreakBeforeInlineASMColon = FormatStyle::BBIAS_OnlyMultiline; 4785 verifyFormat(Code1, Style); 4786 verifyNoChange(Code2, Style); 4787 verifyNoChange(Code3, Style); 4788 4789 Style.BreakBeforeInlineASMColon = FormatStyle::BBIAS_Always; 4790 verifyFormat(Code2, Code1, Style); 4791 verifyNoChange(Code2, Style); 4792 verifyFormat(Code2, Code3, Style); 4793 } 4794 4795 TEST_F(FormatTest, FormatTryCatch) { 4796 verifyFormat("try {\n" 4797 " throw a * b;\n" 4798 "} catch (int a) {\n" 4799 " // Do nothing.\n" 4800 "} catch (...) {\n" 4801 " exit(42);\n" 4802 "}"); 4803 4804 // Function-level try statements. 4805 verifyFormat("int f() try { return 4; } catch (...) {\n" 4806 " return 5;\n" 4807 "}"); 4808 verifyFormat("class A {\n" 4809 " int a;\n" 4810 " A() try : a(0) {\n" 4811 " } catch (...) {\n" 4812 " throw;\n" 4813 " }\n" 4814 "};"); 4815 verifyFormat("class A {\n" 4816 " int a;\n" 4817 " A() try : a(0), b{1} {\n" 4818 " } catch (...) {\n" 4819 " throw;\n" 4820 " }\n" 4821 "};"); 4822 verifyFormat("class A {\n" 4823 " int a;\n" 4824 " A() try : a(0), b{1}, c{2} {\n" 4825 " } catch (...) {\n" 4826 " throw;\n" 4827 " }\n" 4828 "};"); 4829 verifyFormat("class A {\n" 4830 " int a;\n" 4831 " A() try : a(0), b{1}, c{2} {\n" 4832 " { // New scope.\n" 4833 " }\n" 4834 " } catch (...) {\n" 4835 " throw;\n" 4836 " }\n" 4837 "};"); 4838 4839 // Incomplete try-catch blocks. 4840 verifyIncompleteFormat("try {} catch ("); 4841 } 4842 4843 TEST_F(FormatTest, FormatTryAsAVariable) { 4844 verifyFormat("int try;"); 4845 verifyFormat("int try, size;"); 4846 verifyFormat("try = foo();"); 4847 verifyFormat("if (try < size) {\n return true;\n}"); 4848 4849 verifyFormat("int catch;"); 4850 verifyFormat("int catch, size;"); 4851 verifyFormat("catch = foo();"); 4852 verifyFormat("if (catch < size) {\n return true;\n}"); 4853 4854 FormatStyle Style = getLLVMStyle(); 4855 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 4856 Style.BraceWrapping.AfterFunction = true; 4857 Style.BraceWrapping.BeforeCatch = true; 4858 verifyFormat("try {\n" 4859 " int bar = 1;\n" 4860 "}\n" 4861 "catch (...) {\n" 4862 " int bar = 1;\n" 4863 "}", 4864 Style); 4865 verifyFormat("#if NO_EX\n" 4866 "try\n" 4867 "#endif\n" 4868 "{\n" 4869 "}\n" 4870 "#if NO_EX\n" 4871 "catch (...) {\n" 4872 "}", 4873 Style); 4874 verifyFormat("try /* abc */ {\n" 4875 " int bar = 1;\n" 4876 "}\n" 4877 "catch (...) {\n" 4878 " int bar = 1;\n" 4879 "}", 4880 Style); 4881 verifyFormat("try\n" 4882 "// abc\n" 4883 "{\n" 4884 " int bar = 1;\n" 4885 "}\n" 4886 "catch (...) {\n" 4887 " int bar = 1;\n" 4888 "}", 4889 Style); 4890 } 4891 4892 TEST_F(FormatTest, FormatSEHTryCatch) { 4893 verifyFormat("__try {\n" 4894 " int a = b * c;\n" 4895 "} __except (EXCEPTION_EXECUTE_HANDLER) {\n" 4896 " // Do nothing.\n" 4897 "}"); 4898 4899 verifyFormat("__try {\n" 4900 " int a = b * c;\n" 4901 "} __finally {\n" 4902 " // Do nothing.\n" 4903 "}"); 4904 4905 verifyFormat("DEBUG({\n" 4906 " __try {\n" 4907 " } __finally {\n" 4908 " }\n" 4909 "});"); 4910 } 4911 4912 TEST_F(FormatTest, IncompleteTryCatchBlocks) { 4913 verifyFormat("try {\n" 4914 " f();\n" 4915 "} catch {\n" 4916 " g();\n" 4917 "}"); 4918 verifyFormat("try {\n" 4919 " f();\n" 4920 "} catch (A a) MACRO(x) {\n" 4921 " g();\n" 4922 "} catch (B b) MACRO(x) {\n" 4923 " g();\n" 4924 "}"); 4925 } 4926 4927 TEST_F(FormatTest, FormatTryCatchBraceStyles) { 4928 FormatStyle Style = getLLVMStyle(); 4929 for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla, 4930 FormatStyle::BS_WebKit}) { 4931 Style.BreakBeforeBraces = BraceStyle; 4932 verifyFormat("try {\n" 4933 " // something\n" 4934 "} catch (...) {\n" 4935 " // something\n" 4936 "}", 4937 Style); 4938 } 4939 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 4940 verifyFormat("try {\n" 4941 " // something\n" 4942 "}\n" 4943 "catch (...) {\n" 4944 " // something\n" 4945 "}", 4946 Style); 4947 verifyFormat("__try {\n" 4948 " // something\n" 4949 "}\n" 4950 "__finally {\n" 4951 " // something\n" 4952 "}", 4953 Style); 4954 verifyFormat("@try {\n" 4955 " // something\n" 4956 "}\n" 4957 "@finally {\n" 4958 " // something\n" 4959 "}", 4960 Style); 4961 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 4962 verifyFormat("try\n" 4963 "{\n" 4964 " // something\n" 4965 "}\n" 4966 "catch (...)\n" 4967 "{\n" 4968 " // something\n" 4969 "}", 4970 Style); 4971 Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; 4972 verifyFormat("try\n" 4973 " {\n" 4974 " // something white\n" 4975 " }\n" 4976 "catch (...)\n" 4977 " {\n" 4978 " // something white\n" 4979 " }", 4980 Style); 4981 Style.BreakBeforeBraces = FormatStyle::BS_GNU; 4982 verifyFormat("try\n" 4983 " {\n" 4984 " // something\n" 4985 " }\n" 4986 "catch (...)\n" 4987 " {\n" 4988 " // something\n" 4989 " }", 4990 Style); 4991 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 4992 Style.BraceWrapping.BeforeCatch = true; 4993 verifyFormat("try {\n" 4994 " // something\n" 4995 "}\n" 4996 "catch (...) {\n" 4997 " // something\n" 4998 "}", 4999 Style); 5000 } 5001 5002 TEST_F(FormatTest, StaticInitializers) { 5003 verifyFormat("static SomeClass SC = {1, 'a'};"); 5004 5005 verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n" 5006 " 100000000, " 5007 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};"); 5008 5009 // Here, everything other than the "}" would fit on a line. 5010 verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n" 5011 " 10000000000000000000000000};"); 5012 verifyFormat("S s = {a,\n" 5013 "\n" 5014 " b};", 5015 "S s = {\n" 5016 " a,\n" 5017 "\n" 5018 " b\n" 5019 "};"); 5020 5021 // FIXME: This would fit into the column limit if we'd fit "{ {" on the first 5022 // line. However, the formatting looks a bit off and this probably doesn't 5023 // happen often in practice. 5024 verifyFormat("static int Variable[1] = {\n" 5025 " {1000000000000000000000000000000000000}};", 5026 getLLVMStyleWithColumns(40)); 5027 } 5028 5029 TEST_F(FormatTest, DesignatedInitializers) { 5030 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 5031 verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n" 5032 " .bbbbbbbbbb = 2,\n" 5033 " .cccccccccc = 3,\n" 5034 " .dddddddddd = 4,\n" 5035 " .eeeeeeeeee = 5};"); 5036 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 5037 " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n" 5038 " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n" 5039 " .ccccccccccccccccccccccccccc = 3,\n" 5040 " .ddddddddddddddddddddddddddd = 4,\n" 5041 " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};"); 5042 5043 verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};"); 5044 5045 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 5046 verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n" 5047 " [2] = bbbbbbbbbb,\n" 5048 " [3] = cccccccccc,\n" 5049 " [4] = dddddddddd,\n" 5050 " [5] = eeeeeeeeee};"); 5051 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 5052 " [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5053 " [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 5054 " [3] = cccccccccccccccccccccccccccccccccccccc,\n" 5055 " [4] = dddddddddddddddddddddddddddddddddddddd,\n" 5056 " [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};"); 5057 5058 verifyFormat("for (const TestCase &test_case : {\n" 5059 " TestCase{\n" 5060 " .a = 1,\n" 5061 " .b = 1,\n" 5062 " },\n" 5063 " TestCase{\n" 5064 " .a = 2,\n" 5065 " .b = 2,\n" 5066 " },\n" 5067 " }) {\n" 5068 "}"); 5069 } 5070 5071 TEST_F(FormatTest, BracedInitializerIndentWidth) { 5072 auto Style = getLLVMStyleWithColumns(60); 5073 Style.BinPackArguments = true; 5074 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 5075 Style.BracedInitializerIndentWidth = 6; 5076 5077 // Non-initializing braces are unaffected by BracedInitializerIndentWidth. 5078 verifyFormat("enum class {\n" 5079 " One,\n" 5080 " Two,\n" 5081 "};", 5082 Style); 5083 verifyFormat("class Foo {\n" 5084 " Foo() {}\n" 5085 " void bar();\n" 5086 "};", 5087 Style); 5088 verifyFormat("void foo() {\n" 5089 " auto bar = baz;\n" 5090 " return baz;\n" 5091 "};", 5092 Style); 5093 verifyFormat("auto foo = [&] {\n" 5094 " auto bar = baz;\n" 5095 " return baz;\n" 5096 "};", 5097 Style); 5098 verifyFormat("{\n" 5099 " auto bar = baz;\n" 5100 " return baz;\n" 5101 "};", 5102 Style); 5103 // Non-brace initialization is unaffected by BracedInitializerIndentWidth. 5104 verifyFormat("SomeClass clazz(\n" 5105 " \"xxxxxxxxxxxxxxxxxx\", \"yyyyyyyyyyyyyyyyyy\",\n" 5106 " \"zzzzzzzzzzzzzzzzzz\");", 5107 Style); 5108 5109 // The following types of initialization are all affected by 5110 // BracedInitializerIndentWidth. Aggregate initialization. 5111 verifyFormat("int LooooooooooooooooooooooooongVariable[2] = {\n" 5112 " 10000000, 20000000};", 5113 Style); 5114 verifyFormat("SomeStruct s{\n" 5115 " \"xxxxxxxxxxxxxxxx\", \"yyyyyyyyyyyyyyyy\",\n" 5116 " \"zzzzzzzzzzzzzzzz\"};", 5117 Style); 5118 // Designated initializers. 5119 verifyFormat("int LooooooooooooooooooooooooongVariable[2] = {\n" 5120 " [0] = 10000000, [1] = 20000000};", 5121 Style); 5122 verifyFormat("SomeStruct s{\n" 5123 " .foo = \"xxxxxxxxxxxxx\",\n" 5124 " .bar = \"yyyyyyyyyyyyy\",\n" 5125 " .baz = \"zzzzzzzzzzzzz\"};", 5126 Style); 5127 // List initialization. 5128 verifyFormat("SomeStruct s{\n" 5129 " \"xxxxxxxxxxxxx\",\n" 5130 " \"yyyyyyyyyyyyy\",\n" 5131 " \"zzzzzzzzzzzzz\",\n" 5132 "};", 5133 Style); 5134 verifyFormat("SomeStruct{\n" 5135 " \"xxxxxxxxxxxxx\",\n" 5136 " \"yyyyyyyyyyyyy\",\n" 5137 " \"zzzzzzzzzzzzz\",\n" 5138 "};", 5139 Style); 5140 verifyFormat("new SomeStruct{\n" 5141 " \"xxxxxxxxxxxxx\",\n" 5142 " \"yyyyyyyyyyyyy\",\n" 5143 " \"zzzzzzzzzzzzz\",\n" 5144 "};", 5145 Style); 5146 // Member initializer. 5147 verifyFormat("class SomeClass {\n" 5148 " SomeStruct s{\n" 5149 " \"xxxxxxxxxxxxx\",\n" 5150 " \"yyyyyyyyyyyyy\",\n" 5151 " \"zzzzzzzzzzzzz\",\n" 5152 " };\n" 5153 "};", 5154 Style); 5155 // Constructor member initializer. 5156 verifyFormat("SomeClass::SomeClass : strct{\n" 5157 " \"xxxxxxxxxxxxx\",\n" 5158 " \"yyyyyyyyyyyyy\",\n" 5159 " \"zzzzzzzzzzzzz\",\n" 5160 " } {}", 5161 Style); 5162 // Copy initialization. 5163 verifyFormat("SomeStruct s = SomeStruct{\n" 5164 " \"xxxxxxxxxxxxx\",\n" 5165 " \"yyyyyyyyyyyyy\",\n" 5166 " \"zzzzzzzzzzzzz\",\n" 5167 "};", 5168 Style); 5169 // Copy list initialization. 5170 verifyFormat("SomeStruct s = {\n" 5171 " \"xxxxxxxxxxxxx\",\n" 5172 " \"yyyyyyyyyyyyy\",\n" 5173 " \"zzzzzzzzzzzzz\",\n" 5174 "};", 5175 Style); 5176 // Assignment operand initialization. 5177 verifyFormat("s = {\n" 5178 " \"xxxxxxxxxxxxx\",\n" 5179 " \"yyyyyyyyyyyyy\",\n" 5180 " \"zzzzzzzzzzzzz\",\n" 5181 "};", 5182 Style); 5183 // Returned object initialization. 5184 verifyFormat("return {\n" 5185 " \"xxxxxxxxxxxxx\",\n" 5186 " \"yyyyyyyyyyyyy\",\n" 5187 " \"zzzzzzzzzzzzz\",\n" 5188 "};", 5189 Style); 5190 // Initializer list. 5191 verifyFormat("auto initializerList = {\n" 5192 " \"xxxxxxxxxxxxx\",\n" 5193 " \"yyyyyyyyyyyyy\",\n" 5194 " \"zzzzzzzzzzzzz\",\n" 5195 "};", 5196 Style); 5197 // Function parameter initialization. 5198 verifyFormat("func({\n" 5199 " \"xxxxxxxxxxxxx\",\n" 5200 " \"yyyyyyyyyyyyy\",\n" 5201 " \"zzzzzzzzzzzzz\",\n" 5202 "});", 5203 Style); 5204 // Nested init lists. 5205 verifyFormat("SomeStruct s = {\n" 5206 " {{init1, init2, init3, init4, init5},\n" 5207 " {init1, init2, init3, init4, init5}}};", 5208 Style); 5209 verifyFormat("SomeStruct s = {\n" 5210 " {{\n" 5211 " .init1 = 1,\n" 5212 " .init2 = 2,\n" 5213 " .init3 = 3,\n" 5214 " .init4 = 4,\n" 5215 " .init5 = 5,\n" 5216 " },\n" 5217 " {init1, init2, init3, init4, init5}}};", 5218 Style); 5219 verifyFormat("SomeArrayT a[3] = {\n" 5220 " {\n" 5221 " foo,\n" 5222 " bar,\n" 5223 " },\n" 5224 " {\n" 5225 " foo,\n" 5226 " bar,\n" 5227 " },\n" 5228 " SomeArrayT{},\n" 5229 "};", 5230 Style); 5231 verifyFormat("SomeArrayT a[3] = {\n" 5232 " {foo},\n" 5233 " {\n" 5234 " {\n" 5235 " init1,\n" 5236 " init2,\n" 5237 " init3,\n" 5238 " },\n" 5239 " {\n" 5240 " init1,\n" 5241 " init2,\n" 5242 " init3,\n" 5243 " },\n" 5244 " },\n" 5245 " {baz},\n" 5246 "};", 5247 Style); 5248 5249 // Aligning after open braces unaffected by BracedInitializerIndentWidth. 5250 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 5251 verifyFormat("SomeStruct s{\"xxxxxxxxxxxxx\", \"yyyyyyyyyyyyy\",\n" 5252 " \"zzzzzzzzzzzzz\"};", 5253 Style); 5254 } 5255 5256 TEST_F(FormatTest, NestedStaticInitializers) { 5257 verifyFormat("static A x = {{{}}};"); 5258 verifyFormat("static A x = {{{init1, init2, init3, init4},\n" 5259 " {init1, init2, init3, init4}}};", 5260 getLLVMStyleWithColumns(50)); 5261 5262 verifyFormat("somes Status::global_reps[3] = {\n" 5263 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 5264 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 5265 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};", 5266 getLLVMStyleWithColumns(60)); 5267 verifyGoogleFormat("SomeType Status::global_reps[3] = {\n" 5268 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 5269 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 5270 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};"); 5271 verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n" 5272 " {rect.fRight - rect.fLeft, rect.fBottom - " 5273 "rect.fTop}};"); 5274 5275 verifyFormat( 5276 "SomeArrayOfSomeType a = {\n" 5277 " {{1, 2, 3},\n" 5278 " {1, 2, 3},\n" 5279 " {111111111111111111111111111111, 222222222222222222222222222222,\n" 5280 " 333333333333333333333333333333},\n" 5281 " {1, 2, 3},\n" 5282 " {1, 2, 3}}};"); 5283 verifyFormat( 5284 "SomeArrayOfSomeType a = {\n" 5285 " {{1, 2, 3}},\n" 5286 " {{1, 2, 3}},\n" 5287 " {{111111111111111111111111111111, 222222222222222222222222222222,\n" 5288 " 333333333333333333333333333333}},\n" 5289 " {{1, 2, 3}},\n" 5290 " {{1, 2, 3}}};"); 5291 5292 verifyFormat("struct {\n" 5293 " unsigned bit;\n" 5294 " const char *const name;\n" 5295 "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n" 5296 " {kOsWin, \"Windows\"},\n" 5297 " {kOsLinux, \"Linux\"},\n" 5298 " {kOsCrOS, \"Chrome OS\"}};"); 5299 verifyFormat("struct {\n" 5300 " unsigned bit;\n" 5301 " const char *const name;\n" 5302 "} kBitsToOs[] = {\n" 5303 " {kOsMac, \"Mac\"},\n" 5304 " {kOsWin, \"Windows\"},\n" 5305 " {kOsLinux, \"Linux\"},\n" 5306 " {kOsCrOS, \"Chrome OS\"},\n" 5307 "};"); 5308 } 5309 5310 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { 5311 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 5312 " \\\n" 5313 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); 5314 } 5315 5316 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { 5317 verifyFormat("virtual void write(ELFWriter *writerrr,\n" 5318 " OwningPtr<FileOutputBuffer> &buffer) = 0;"); 5319 5320 // Do break defaulted and deleted functions. 5321 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 5322 " default;", 5323 getLLVMStyleWithColumns(40)); 5324 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 5325 " delete;", 5326 getLLVMStyleWithColumns(40)); 5327 } 5328 5329 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) { 5330 verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3", 5331 getLLVMStyleWithColumns(40)); 5332 verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 5333 getLLVMStyleWithColumns(40)); 5334 verifyFormat("#define Q \\\n" 5335 " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n" 5336 " \"aaaaaaaa.cpp\"", 5337 "#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 5338 getLLVMStyleWithColumns(40)); 5339 } 5340 5341 TEST_F(FormatTest, UnderstandsLinePPDirective) { 5342 verifyFormat("# 123 \"A string literal\"", 5343 " # 123 \"A string literal\""); 5344 } 5345 5346 TEST_F(FormatTest, LayoutUnknownPPDirective) { 5347 verifyFormat("#;"); 5348 verifyFormat("#\n;\n;\n;"); 5349 } 5350 5351 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { 5352 verifyFormat("#line 42 \"test\"", "# \\\n line \\\n 42 \\\n \"test\""); 5353 verifyFormat("#define A B", "# \\\n define \\\n A \\\n B", 5354 getLLVMStyleWithColumns(12)); 5355 } 5356 5357 TEST_F(FormatTest, EndOfFileEndsPPDirective) { 5358 verifyFormat("#line 42 \"test\"", "# \\\n line \\\n 42 \\\n \"test\""); 5359 verifyFormat("#define A B", "# \\\n define \\\n A \\\n B"); 5360 } 5361 5362 TEST_F(FormatTest, DoesntRemoveUnknownTokens) { 5363 verifyFormat("#define A \\x20"); 5364 verifyFormat("#define A \\ x20"); 5365 verifyFormat("#define A \\ x20", "#define A \\ x20"); 5366 verifyFormat("#define A ''"); 5367 verifyFormat("#define A ''qqq"); 5368 verifyFormat("#define A `qqq"); 5369 verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");"); 5370 verifyFormat("const char *c = STRINGIFY(\n" 5371 "\\na : b);", 5372 "const char * c = STRINGIFY(\n" 5373 "\\na : b);"); 5374 5375 verifyFormat("a\r\\"); 5376 verifyFormat("a\v\\"); 5377 verifyFormat("a\f\\"); 5378 } 5379 5380 TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) { 5381 FormatStyle style = getChromiumStyle(FormatStyle::LK_Cpp); 5382 style.IndentWidth = 4; 5383 style.PPIndentWidth = 1; 5384 5385 style.IndentPPDirectives = FormatStyle::PPDIS_None; 5386 verifyFormat("#ifdef __linux__\n" 5387 "void foo() {\n" 5388 " int x = 0;\n" 5389 "}\n" 5390 "#define FOO\n" 5391 "#endif\n" 5392 "void bar() {\n" 5393 " int y = 0;\n" 5394 "}", 5395 style); 5396 5397 style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash; 5398 verifyFormat("#ifdef __linux__\n" 5399 "void foo() {\n" 5400 " int x = 0;\n" 5401 "}\n" 5402 "# define FOO foo\n" 5403 "#endif\n" 5404 "void bar() {\n" 5405 " int y = 0;\n" 5406 "}", 5407 style); 5408 5409 style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash; 5410 verifyFormat("#ifdef __linux__\n" 5411 "void foo() {\n" 5412 " int x = 0;\n" 5413 "}\n" 5414 " #define FOO foo\n" 5415 "#endif\n" 5416 "void bar() {\n" 5417 " int y = 0;\n" 5418 "}", 5419 style); 5420 verifyFormat("#if 1\n" 5421 " // some comments\n" 5422 " // another\n" 5423 " #define foo 1\n" 5424 "// not a define comment\n" 5425 "void bar() {\n" 5426 " // comment\n" 5427 " int y = 0;\n" 5428 "}", 5429 "#if 1\n" 5430 "// some comments\n" 5431 "// another\n" 5432 "#define foo 1\n" 5433 "// not a define comment\n" 5434 "void bar() {\n" 5435 " // comment\n" 5436 " int y = 0;\n" 5437 "}", 5438 style); 5439 5440 style.IndentPPDirectives = FormatStyle::PPDIS_None; 5441 verifyFormat("#ifdef foo\n" 5442 "#define bar() \\\n" 5443 " if (A) { \\\n" 5444 " B(); \\\n" 5445 " } \\\n" 5446 " C();\n" 5447 "#endif", 5448 style); 5449 verifyFormat("if (emacs) {\n" 5450 "#ifdef is\n" 5451 "#define lit \\\n" 5452 " if (af) { \\\n" 5453 " return duh(); \\\n" 5454 " }\n" 5455 "#endif\n" 5456 "}", 5457 style); 5458 verifyFormat("#if abc\n" 5459 "#ifdef foo\n" 5460 "#define bar() \\\n" 5461 " if (A) { \\\n" 5462 " if (B) { \\\n" 5463 " C(); \\\n" 5464 " } \\\n" 5465 " } \\\n" 5466 " D();\n" 5467 "#endif\n" 5468 "#endif", 5469 style); 5470 verifyFormat("#ifndef foo\n" 5471 "#define foo\n" 5472 "if (emacs) {\n" 5473 "#ifdef is\n" 5474 "#define lit \\\n" 5475 " if (af) { \\\n" 5476 " return duh(); \\\n" 5477 " }\n" 5478 "#endif\n" 5479 "}\n" 5480 "#endif", 5481 style); 5482 verifyFormat("#if 1\n" 5483 "#define X \\\n" 5484 " { \\\n" 5485 " x; \\\n" 5486 " x; \\\n" 5487 " }\n" 5488 "#endif", 5489 style); 5490 verifyFormat("#define X \\\n" 5491 " { \\\n" 5492 " x; \\\n" 5493 " x; \\\n" 5494 " }", 5495 style); 5496 5497 style.PPIndentWidth = 2; 5498 verifyFormat("#ifdef foo\n" 5499 "#define bar() \\\n" 5500 " if (A) { \\\n" 5501 " B(); \\\n" 5502 " } \\\n" 5503 " C();\n" 5504 "#endif", 5505 style); 5506 style.IndentWidth = 8; 5507 verifyFormat("#ifdef foo\n" 5508 "#define bar() \\\n" 5509 " if (A) { \\\n" 5510 " B(); \\\n" 5511 " } \\\n" 5512 " C();\n" 5513 "#endif", 5514 style); 5515 5516 style.IndentWidth = 1; 5517 style.PPIndentWidth = 4; 5518 verifyFormat("#if 1\n" 5519 "#define X \\\n" 5520 " { \\\n" 5521 " x; \\\n" 5522 " x; \\\n" 5523 " }\n" 5524 "#endif", 5525 style); 5526 verifyFormat("#define X \\\n" 5527 " { \\\n" 5528 " x; \\\n" 5529 " x; \\\n" 5530 " }", 5531 style); 5532 5533 style.IndentWidth = 4; 5534 style.PPIndentWidth = 1; 5535 style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash; 5536 verifyFormat("#ifdef foo\n" 5537 "# define bar() \\\n" 5538 " if (A) { \\\n" 5539 " B(); \\\n" 5540 " } \\\n" 5541 " C();\n" 5542 "#endif", 5543 style); 5544 verifyFormat("#if abc\n" 5545 "# ifdef foo\n" 5546 "# define bar() \\\n" 5547 " if (A) { \\\n" 5548 " if (B) { \\\n" 5549 " C(); \\\n" 5550 " } \\\n" 5551 " } \\\n" 5552 " D();\n" 5553 "# endif\n" 5554 "#endif", 5555 style); 5556 verifyFormat("#ifndef foo\n" 5557 "#define foo\n" 5558 "if (emacs) {\n" 5559 "#ifdef is\n" 5560 "# define lit \\\n" 5561 " if (af) { \\\n" 5562 " return duh(); \\\n" 5563 " }\n" 5564 "#endif\n" 5565 "}\n" 5566 "#endif", 5567 style); 5568 verifyFormat("#define X \\\n" 5569 " { \\\n" 5570 " x; \\\n" 5571 " x; \\\n" 5572 " }", 5573 style); 5574 5575 style.PPIndentWidth = 2; 5576 style.IndentWidth = 8; 5577 verifyFormat("#ifdef foo\n" 5578 "# define bar() \\\n" 5579 " if (A) { \\\n" 5580 " B(); \\\n" 5581 " } \\\n" 5582 " C();\n" 5583 "#endif", 5584 style); 5585 5586 style.PPIndentWidth = 4; 5587 style.IndentWidth = 1; 5588 verifyFormat("#define X \\\n" 5589 " { \\\n" 5590 " x; \\\n" 5591 " x; \\\n" 5592 " }", 5593 style); 5594 5595 style.IndentWidth = 4; 5596 style.PPIndentWidth = 1; 5597 style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash; 5598 verifyFormat("if (emacs) {\n" 5599 "#ifdef is\n" 5600 " #define lit \\\n" 5601 " if (af) { \\\n" 5602 " return duh(); \\\n" 5603 " }\n" 5604 "#endif\n" 5605 "}", 5606 style); 5607 verifyFormat("#if abc\n" 5608 " #ifdef foo\n" 5609 " #define bar() \\\n" 5610 " if (A) { \\\n" 5611 " B(); \\\n" 5612 " } \\\n" 5613 " C();\n" 5614 " #endif\n" 5615 "#endif", 5616 style); 5617 verifyFormat("#if 1\n" 5618 " #define X \\\n" 5619 " { \\\n" 5620 " x; \\\n" 5621 " x; \\\n" 5622 " }\n" 5623 "#endif", 5624 style); 5625 5626 style.PPIndentWidth = 2; 5627 verifyFormat("#ifdef foo\n" 5628 " #define bar() \\\n" 5629 " if (A) { \\\n" 5630 " B(); \\\n" 5631 " } \\\n" 5632 " C();\n" 5633 "#endif", 5634 style); 5635 5636 style.PPIndentWidth = 4; 5637 style.IndentWidth = 1; 5638 verifyFormat("#if 1\n" 5639 " #define X \\\n" 5640 " { \\\n" 5641 " x; \\\n" 5642 " x; \\\n" 5643 " }\n" 5644 "#endif", 5645 style); 5646 } 5647 5648 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { 5649 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13)); 5650 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12)); 5651 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); 5652 // FIXME: We never break before the macro name. 5653 verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12)); 5654 5655 verifyFormat("#define A A\n#define A A"); 5656 verifyFormat("#define A(X) A\n#define A A"); 5657 5658 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23)); 5659 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22)); 5660 } 5661 5662 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { 5663 verifyFormat("// somecomment\n" 5664 "#include \"a.h\"\n" 5665 "#define A( \\\n" 5666 " A, B)\n" 5667 "#include \"b.h\"\n" 5668 "// somecomment", 5669 " // somecomment\n" 5670 " #include \"a.h\"\n" 5671 "#define A(A,\\\n" 5672 " B)\n" 5673 " #include \"b.h\"\n" 5674 " // somecomment", 5675 getLLVMStyleWithColumns(13)); 5676 } 5677 5678 TEST_F(FormatTest, LayoutSingleHash) { verifyFormat("#\na;"); } 5679 5680 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { 5681 verifyFormat("#define A \\\n" 5682 " c; \\\n" 5683 " e;\n" 5684 "f;", 5685 "#define A c; e;\n" 5686 "f;", 5687 getLLVMStyleWithColumns(14)); 5688 } 5689 5690 TEST_F(FormatTest, LayoutRemainingTokens) { 5691 verifyFormat("{\n" 5692 "}"); 5693 } 5694 5695 TEST_F(FormatTest, MacroDefinitionInsideStatement) { 5696 verifyFormat("int x,\n" 5697 "#define A\n" 5698 " y;", 5699 "int x,\n#define A\ny;"); 5700 } 5701 5702 TEST_F(FormatTest, HashInMacroDefinition) { 5703 verifyFormat("#define A(c) L#c"); 5704 verifyFormat("#define A(c) u#c"); 5705 verifyFormat("#define A(c) U#c"); 5706 verifyFormat("#define A(c) u8#c"); 5707 verifyFormat("#define A(c) LR#c"); 5708 verifyFormat("#define A(c) uR#c"); 5709 verifyFormat("#define A(c) UR#c"); 5710 verifyFormat("#define A(c) u8R#c"); 5711 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); 5712 verifyFormat("#define A \\\n" 5713 " { \\\n" 5714 " f(#c); \\\n" 5715 " }", 5716 getLLVMStyleWithColumns(11)); 5717 5718 verifyFormat("#define A(X) \\\n" 5719 " void function##X()", 5720 getLLVMStyleWithColumns(22)); 5721 5722 verifyFormat("#define A(a, b, c) \\\n" 5723 " void a##b##c()", 5724 getLLVMStyleWithColumns(22)); 5725 5726 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22)); 5727 5728 #if 0 5729 // FIXME: The correct format is: 5730 verifyFormat("{\n" 5731 " {\n" 5732 "#define GEN_ID(_x) char *_x{#_x}\n" 5733 " GEN_ID(one);\n" 5734 " }\n" 5735 "}"); 5736 #endif 5737 verifyFormat("{\n" 5738 " {\n" 5739 "#define GEN_ID(_x) \\\n" 5740 " char *_x { #_x }\n" 5741 " GEN_ID(one);\n" 5742 " }\n" 5743 "}", 5744 getGoogleStyle()); 5745 } 5746 5747 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { 5748 verifyFormat("#define A (x)"); 5749 verifyFormat("#define A(x)"); 5750 5751 FormatStyle Style = getLLVMStyle(); 5752 Style.SpaceBeforeParens = FormatStyle::SBPO_Never; 5753 verifyFormat("#define true ((foo)1)", Style); 5754 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 5755 verifyFormat("#define false((foo)0)", Style); 5756 } 5757 5758 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) { 5759 verifyFormat("#define A b;", 5760 "#define A \\\n" 5761 " \\\n" 5762 " b;", 5763 getLLVMStyleWithColumns(25)); 5764 verifyNoChange("#define A \\\n" 5765 " \\\n" 5766 " a; \\\n" 5767 " b;", 5768 getLLVMStyleWithColumns(11)); 5769 verifyNoChange("#define A \\\n" 5770 " a; \\\n" 5771 " \\\n" 5772 " b;", 5773 getLLVMStyleWithColumns(11)); 5774 } 5775 5776 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { 5777 verifyIncompleteFormat("#define A :"); 5778 verifyFormat("#define SOMECASES \\\n" 5779 " case 1: \\\n" 5780 " case 2", 5781 getLLVMStyleWithColumns(20)); 5782 verifyFormat("#define MACRO(a) \\\n" 5783 " if (a) \\\n" 5784 " f(); \\\n" 5785 " else \\\n" 5786 " g()", 5787 getLLVMStyleWithColumns(18)); 5788 verifyFormat("#define A template <typename T>"); 5789 verifyIncompleteFormat("#define STR(x) #x\n" 5790 "f(STR(this_is_a_string_literal{));"); 5791 verifyFormat("#pragma omp threadprivate( \\\n" 5792 " y)), // expected-warning", 5793 getLLVMStyleWithColumns(28)); 5794 verifyFormat("#d, = };"); 5795 verifyFormat("#if \"a"); 5796 verifyIncompleteFormat("({\n" 5797 "#define b \\\n" 5798 " } \\\n" 5799 " a\n" 5800 "a", 5801 getLLVMStyleWithColumns(15)); 5802 verifyFormat("#define A \\\n" 5803 " { \\\n" 5804 " {\n" 5805 "#define B \\\n" 5806 " } \\\n" 5807 " }", 5808 getLLVMStyleWithColumns(15)); 5809 verifyNoCrash("#if a\na(\n#else\n#endif\n{a"); 5810 verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}"); 5811 verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};"); 5812 verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}"); 5813 verifyNoCrash("#else\n" 5814 "#else\n" 5815 "#endif\n" 5816 "#endif"); 5817 verifyNoCrash("#else\n" 5818 "#if X\n" 5819 "#endif\n" 5820 "#endif"); 5821 verifyNoCrash("#else\n" 5822 "#endif\n" 5823 "#if X\n" 5824 "#endif"); 5825 verifyNoCrash("#if X\n" 5826 "#else\n" 5827 "#else\n" 5828 "#endif\n" 5829 "#endif"); 5830 verifyNoCrash("#if X\n" 5831 "#elif Y\n" 5832 "#elif Y\n" 5833 "#endif\n" 5834 "#endif"); 5835 verifyNoCrash("#endif\n" 5836 "#endif"); 5837 verifyNoCrash("#endif\n" 5838 "#else"); 5839 verifyNoCrash("#endif\n" 5840 "#elif Y"); 5841 } 5842 5843 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) { 5844 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline. 5845 verifyFormat("class A : public QObject {\n" 5846 " Q_OBJECT\n" 5847 "\n" 5848 " A() {}\n" 5849 "};", 5850 "class A : public QObject {\n" 5851 " Q_OBJECT\n" 5852 "\n" 5853 " A() {\n}\n" 5854 "} ;"); 5855 verifyFormat("MACRO\n" 5856 "/*static*/ int i;", 5857 "MACRO\n" 5858 " /*static*/ int i;"); 5859 verifyFormat("SOME_MACRO\n" 5860 "namespace {\n" 5861 "void f();\n" 5862 "} // namespace", 5863 "SOME_MACRO\n" 5864 " namespace {\n" 5865 "void f( );\n" 5866 "} // namespace"); 5867 // Only if the identifier contains at least 5 characters. 5868 verifyFormat("HTTP f();", "HTTP\nf();"); 5869 verifyNoChange("MACRO\nf();"); 5870 // Only if everything is upper case. 5871 verifyFormat("class A : public QObject {\n" 5872 " Q_Object A() {}\n" 5873 "};", 5874 "class A : public QObject {\n" 5875 " Q_Object\n" 5876 " A() {\n}\n" 5877 "} ;"); 5878 5879 // Only if the next line can actually start an unwrapped line. 5880 verifyFormat("SOME_WEIRD_LOG_MACRO << SomeThing;", "SOME_WEIRD_LOG_MACRO\n" 5881 "<< SomeThing;"); 5882 5883 verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), " 5884 "(n, buffers))", 5885 getChromiumStyle(FormatStyle::LK_Cpp)); 5886 5887 // See PR41483 5888 verifyNoChange("/**/ FOO(a)\n" 5889 "FOO(b)"); 5890 } 5891 5892 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) { 5893 verifyFormat("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 5894 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 5895 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 5896 "class X {};\n" 5897 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 5898 "int *createScopDetectionPass() { return 0; }", 5899 " INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 5900 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 5901 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 5902 " class X {};\n" 5903 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 5904 " int *createScopDetectionPass() { return 0; }"); 5905 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as 5906 // braces, so that inner block is indented one level more. 5907 verifyFormat("int q() {\n" 5908 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 5909 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 5910 " IPC_END_MESSAGE_MAP()\n" 5911 "}", 5912 "int q() {\n" 5913 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 5914 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 5915 " IPC_END_MESSAGE_MAP()\n" 5916 "}"); 5917 5918 // Same inside macros. 5919 verifyFormat("#define LIST(L) \\\n" 5920 " L(A) \\\n" 5921 " L(B) \\\n" 5922 " L(C)", 5923 "#define LIST(L) \\\n" 5924 " L(A) \\\n" 5925 " L(B) \\\n" 5926 " L(C)", 5927 getGoogleStyle()); 5928 5929 // These must not be recognized as macros. 5930 verifyFormat("int q() {\n" 5931 " f(x);\n" 5932 " f(x) {}\n" 5933 " f(x)->g();\n" 5934 " f(x)->*g();\n" 5935 " f(x).g();\n" 5936 " f(x) = x;\n" 5937 " f(x) += x;\n" 5938 " f(x) -= x;\n" 5939 " f(x) *= x;\n" 5940 " f(x) /= x;\n" 5941 " f(x) %= x;\n" 5942 " f(x) &= x;\n" 5943 " f(x) |= x;\n" 5944 " f(x) ^= x;\n" 5945 " f(x) >>= x;\n" 5946 " f(x) <<= x;\n" 5947 " f(x)[y].z();\n" 5948 " LOG(INFO) << x;\n" 5949 " ifstream(x) >> x;\n" 5950 "}", 5951 "int q() {\n" 5952 " f(x)\n;\n" 5953 " f(x)\n {}\n" 5954 " f(x)\n->g();\n" 5955 " f(x)\n->*g();\n" 5956 " f(x)\n.g();\n" 5957 " f(x)\n = x;\n" 5958 " f(x)\n += x;\n" 5959 " f(x)\n -= x;\n" 5960 " f(x)\n *= x;\n" 5961 " f(x)\n /= x;\n" 5962 " f(x)\n %= x;\n" 5963 " f(x)\n &= x;\n" 5964 " f(x)\n |= x;\n" 5965 " f(x)\n ^= x;\n" 5966 " f(x)\n >>= x;\n" 5967 " f(x)\n <<= x;\n" 5968 " f(x)\n[y].z();\n" 5969 " LOG(INFO)\n << x;\n" 5970 " ifstream(x)\n >> x;\n" 5971 "}"); 5972 verifyFormat("int q() {\n" 5973 " F(x)\n" 5974 " if (1) {\n" 5975 " }\n" 5976 " F(x)\n" 5977 " while (1) {\n" 5978 " }\n" 5979 " F(x)\n" 5980 " G(x);\n" 5981 " F(x)\n" 5982 " try {\n" 5983 " Q();\n" 5984 " } catch (...) {\n" 5985 " }\n" 5986 "}", 5987 "int q() {\n" 5988 "F(x)\n" 5989 "if (1) {}\n" 5990 "F(x)\n" 5991 "while (1) {}\n" 5992 "F(x)\n" 5993 "G(x);\n" 5994 "F(x)\n" 5995 "try { Q(); } catch (...) {}\n" 5996 "}"); 5997 verifyFormat("class A {\n" 5998 " A() : t(0) {}\n" 5999 " A(int i) noexcept() : {}\n" 6000 " A(X x)\n" // FIXME: function-level try blocks are broken. 6001 " try : t(0) {\n" 6002 " } catch (...) {\n" 6003 " }\n" 6004 "};", 6005 "class A {\n" 6006 " A()\n : t(0) {}\n" 6007 " A(int i)\n noexcept() : {}\n" 6008 " A(X x)\n" 6009 " try : t(0) {} catch (...) {}\n" 6010 "};"); 6011 FormatStyle Style = getLLVMStyle(); 6012 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 6013 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 6014 Style.BraceWrapping.AfterFunction = true; 6015 verifyFormat("void f()\n" 6016 "try\n" 6017 "{\n" 6018 "}", 6019 "void f() try {\n" 6020 "}", 6021 Style); 6022 verifyFormat("class SomeClass {\n" 6023 "public:\n" 6024 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 6025 "};", 6026 "class SomeClass {\n" 6027 "public:\n" 6028 " SomeClass()\n" 6029 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 6030 "};"); 6031 verifyFormat("class SomeClass {\n" 6032 "public:\n" 6033 " SomeClass()\n" 6034 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 6035 "};", 6036 "class SomeClass {\n" 6037 "public:\n" 6038 " SomeClass()\n" 6039 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 6040 "};", 6041 getLLVMStyleWithColumns(40)); 6042 6043 verifyFormat("MACRO(>)"); 6044 6045 // Some macros contain an implicit semicolon. 6046 Style = getLLVMStyle(); 6047 Style.StatementMacros.push_back("FOO"); 6048 verifyFormat("FOO(a) int b = 0;"); 6049 verifyFormat("FOO(a)\n" 6050 "int b = 0;", 6051 Style); 6052 verifyFormat("FOO(a);\n" 6053 "int b = 0;", 6054 Style); 6055 verifyFormat("FOO(argc, argv, \"4.0.2\")\n" 6056 "int b = 0;", 6057 Style); 6058 verifyFormat("FOO()\n" 6059 "int b = 0;", 6060 Style); 6061 verifyFormat("FOO\n" 6062 "int b = 0;", 6063 Style); 6064 verifyFormat("void f() {\n" 6065 " FOO(a)\n" 6066 " return a;\n" 6067 "}", 6068 Style); 6069 verifyFormat("FOO(a)\n" 6070 "FOO(b)", 6071 Style); 6072 verifyFormat("int a = 0;\n" 6073 "FOO(b)\n" 6074 "int c = 0;", 6075 Style); 6076 verifyFormat("int a = 0;\n" 6077 "int x = FOO(a)\n" 6078 "int b = 0;", 6079 Style); 6080 verifyFormat("void foo(int a) { FOO(a) }\n" 6081 "uint32_t bar() {}", 6082 Style); 6083 } 6084 6085 TEST_F(FormatTest, FormatsMacrosWithZeroColumnWidth) { 6086 FormatStyle ZeroColumn = getLLVMStyleWithColumns(0); 6087 6088 verifyFormat("#define A LOOOOOOOOOOOOOOOOOOONG() LOOOOOOOOOOOOOOOOOOONG()", 6089 ZeroColumn); 6090 } 6091 6092 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) { 6093 verifyFormat("#define A \\\n" 6094 " f({ \\\n" 6095 " g(); \\\n" 6096 " });", 6097 getLLVMStyleWithColumns(11)); 6098 } 6099 6100 TEST_F(FormatTest, IndentPreprocessorDirectives) { 6101 FormatStyle Style = getLLVMStyleWithColumns(40); 6102 Style.IndentPPDirectives = FormatStyle::PPDIS_None; 6103 verifyFormat("#ifdef _WIN32\n" 6104 "#define A 0\n" 6105 "#ifdef VAR2\n" 6106 "#define B 1\n" 6107 "#include <someheader.h>\n" 6108 "#define MACRO \\\n" 6109 " some_very_long_func_aaaaaaaaaa();\n" 6110 "#endif\n" 6111 "#else\n" 6112 "#define A 1\n" 6113 "#endif", 6114 Style); 6115 Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash; 6116 verifyFormat("#if 1\n" 6117 "# define __STR(x) #x\n" 6118 "#endif", 6119 Style); 6120 verifyFormat("#ifdef _WIN32\n" 6121 "# define A 0\n" 6122 "# ifdef VAR2\n" 6123 "# define B 1\n" 6124 "# include <someheader.h>\n" 6125 "# define MACRO \\\n" 6126 " some_very_long_func_aaaaaaaaaa();\n" 6127 "# endif\n" 6128 "#else\n" 6129 "# define A 1\n" 6130 "#endif", 6131 Style); 6132 verifyFormat("#if A\n" 6133 "# define MACRO \\\n" 6134 " void a(int x) { \\\n" 6135 " b(); \\\n" 6136 " c(); \\\n" 6137 " d(); \\\n" 6138 " e(); \\\n" 6139 " f(); \\\n" 6140 " }\n" 6141 "#endif", 6142 Style); 6143 // Comments before include guard. 6144 verifyFormat("// file comment\n" 6145 "// file comment\n" 6146 "#ifndef HEADER_H\n" 6147 "#define HEADER_H\n" 6148 "code();\n" 6149 "#endif", 6150 Style); 6151 // Test with include guards. 6152 verifyFormat("#ifndef HEADER_H\n" 6153 "#define HEADER_H\n" 6154 "code();\n" 6155 "#endif", 6156 Style); 6157 // Include guards must have a #define with the same variable immediately 6158 // after #ifndef. 6159 verifyFormat("#ifndef NOT_GUARD\n" 6160 "# define FOO\n" 6161 "code();\n" 6162 "#endif", 6163 Style); 6164 6165 // Include guards must cover the entire file. 6166 verifyFormat("code();\n" 6167 "code();\n" 6168 "#ifndef NOT_GUARD\n" 6169 "# define NOT_GUARD\n" 6170 "code();\n" 6171 "#endif", 6172 Style); 6173 verifyFormat("#ifndef NOT_GUARD\n" 6174 "# define NOT_GUARD\n" 6175 "code();\n" 6176 "#endif\n" 6177 "code();", 6178 Style); 6179 // Test with trailing blank lines. 6180 verifyFormat("#ifndef HEADER_H\n" 6181 "#define HEADER_H\n" 6182 "code();\n" 6183 "#endif", 6184 Style); 6185 // Include guards don't have #else. 6186 verifyFormat("#ifndef NOT_GUARD\n" 6187 "# define NOT_GUARD\n" 6188 "code();\n" 6189 "#else\n" 6190 "#endif", 6191 Style); 6192 verifyFormat("#ifndef NOT_GUARD\n" 6193 "# define NOT_GUARD\n" 6194 "code();\n" 6195 "#elif FOO\n" 6196 "#endif", 6197 Style); 6198 // Non-identifier #define after potential include guard. 6199 verifyFormat("#ifndef FOO\n" 6200 "# define 1\n" 6201 "#endif", 6202 Style); 6203 // #if closes past last non-preprocessor line. 6204 verifyFormat("#ifndef FOO\n" 6205 "#define FOO\n" 6206 "#if 1\n" 6207 "int i;\n" 6208 "# define A 0\n" 6209 "#endif\n" 6210 "#endif", 6211 Style); 6212 // Don't crash if there is an #elif directive without a condition. 6213 verifyFormat("#if 1\n" 6214 "int x;\n" 6215 "#elif\n" 6216 "int y;\n" 6217 "#else\n" 6218 "int z;\n" 6219 "#endif", 6220 Style); 6221 // FIXME: This doesn't handle the case where there's code between the 6222 // #ifndef and #define but all other conditions hold. This is because when 6223 // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the 6224 // previous code line yet, so we can't detect it. 6225 verifyFormat("#ifndef NOT_GUARD\n" 6226 "code();\n" 6227 "#define NOT_GUARD\n" 6228 "code();\n" 6229 "#endif", 6230 "#ifndef NOT_GUARD\n" 6231 "code();\n" 6232 "# define NOT_GUARD\n" 6233 "code();\n" 6234 "#endif", 6235 Style); 6236 // FIXME: This doesn't handle cases where legitimate preprocessor lines may 6237 // be outside an include guard. Examples are #pragma once and 6238 // #pragma GCC diagnostic, or anything else that does not change the meaning 6239 // of the file if it's included multiple times. 6240 verifyFormat("#ifdef WIN32\n" 6241 "# pragma once\n" 6242 "#endif\n" 6243 "#ifndef HEADER_H\n" 6244 "# define HEADER_H\n" 6245 "code();\n" 6246 "#endif", 6247 "#ifdef WIN32\n" 6248 "# pragma once\n" 6249 "#endif\n" 6250 "#ifndef HEADER_H\n" 6251 "#define HEADER_H\n" 6252 "code();\n" 6253 "#endif", 6254 Style); 6255 // FIXME: This does not detect when there is a single non-preprocessor line 6256 // in front of an include-guard-like structure where other conditions hold 6257 // because ScopedLineState hides the line. 6258 verifyFormat("code();\n" 6259 "#ifndef HEADER_H\n" 6260 "#define HEADER_H\n" 6261 "code();\n" 6262 "#endif", 6263 "code();\n" 6264 "#ifndef HEADER_H\n" 6265 "# define HEADER_H\n" 6266 "code();\n" 6267 "#endif", 6268 Style); 6269 // Keep comments aligned with #, otherwise indent comments normally. These 6270 // tests cannot use verifyFormat because messUp manipulates leading 6271 // whitespace. 6272 { 6273 const char *Expected = "" 6274 "void f() {\n" 6275 "#if 1\n" 6276 "// Preprocessor aligned.\n" 6277 "# define A 0\n" 6278 " // Code. Separated by blank line.\n" 6279 "\n" 6280 "# define B 0\n" 6281 " // Code. Not aligned with #\n" 6282 "# define C 0\n" 6283 "#endif"; 6284 const char *ToFormat = "" 6285 "void f() {\n" 6286 "#if 1\n" 6287 "// Preprocessor aligned.\n" 6288 "# define A 0\n" 6289 "// Code. Separated by blank line.\n" 6290 "\n" 6291 "# define B 0\n" 6292 " // Code. Not aligned with #\n" 6293 "# define C 0\n" 6294 "#endif"; 6295 verifyFormat(Expected, ToFormat, Style); 6296 verifyNoChange(Expected, Style); 6297 } 6298 // Keep block quotes aligned. 6299 { 6300 const char *Expected = "" 6301 "void f() {\n" 6302 "#if 1\n" 6303 "/* Preprocessor aligned. */\n" 6304 "# define A 0\n" 6305 " /* Code. Separated by blank line. */\n" 6306 "\n" 6307 "# define B 0\n" 6308 " /* Code. Not aligned with # */\n" 6309 "# define C 0\n" 6310 "#endif"; 6311 const char *ToFormat = "" 6312 "void f() {\n" 6313 "#if 1\n" 6314 "/* Preprocessor aligned. */\n" 6315 "# define A 0\n" 6316 "/* Code. Separated by blank line. */\n" 6317 "\n" 6318 "# define B 0\n" 6319 " /* Code. Not aligned with # */\n" 6320 "# define C 0\n" 6321 "#endif"; 6322 verifyFormat(Expected, ToFormat, Style); 6323 verifyNoChange(Expected, Style); 6324 } 6325 // Keep comments aligned with un-indented directives. 6326 { 6327 const char *Expected = "" 6328 "void f() {\n" 6329 "// Preprocessor aligned.\n" 6330 "#define A 0\n" 6331 " // Code. Separated by blank line.\n" 6332 "\n" 6333 "#define B 0\n" 6334 " // Code. Not aligned with #\n" 6335 "#define C 0\n"; 6336 const char *ToFormat = "" 6337 "void f() {\n" 6338 "// Preprocessor aligned.\n" 6339 "#define A 0\n" 6340 "// Code. Separated by blank line.\n" 6341 "\n" 6342 "#define B 0\n" 6343 " // Code. Not aligned with #\n" 6344 "#define C 0\n"; 6345 verifyFormat(Expected, ToFormat, Style); 6346 verifyNoChange(Expected, Style); 6347 } 6348 // Test AfterHash with tabs. 6349 { 6350 FormatStyle Tabbed = Style; 6351 Tabbed.UseTab = FormatStyle::UT_Always; 6352 Tabbed.IndentWidth = 8; 6353 Tabbed.TabWidth = 8; 6354 verifyFormat("#ifdef _WIN32\n" 6355 "#\tdefine A 0\n" 6356 "#\tifdef VAR2\n" 6357 "#\t\tdefine B 1\n" 6358 "#\t\tinclude <someheader.h>\n" 6359 "#\t\tdefine MACRO \\\n" 6360 "\t\t\tsome_very_long_func_aaaaaaaaaa();\n" 6361 "#\tendif\n" 6362 "#else\n" 6363 "#\tdefine A 1\n" 6364 "#endif", 6365 Tabbed); 6366 } 6367 6368 // Regression test: Multiline-macro inside include guards. 6369 verifyFormat("#ifndef HEADER_H\n" 6370 "#define HEADER_H\n" 6371 "#define A() \\\n" 6372 " int i; \\\n" 6373 " int j;\n" 6374 "#endif // HEADER_H", 6375 getLLVMStyleWithColumns(20)); 6376 6377 Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash; 6378 // Basic before hash indent tests 6379 verifyFormat("#ifdef _WIN32\n" 6380 " #define A 0\n" 6381 " #ifdef VAR2\n" 6382 " #define B 1\n" 6383 " #include <someheader.h>\n" 6384 " #define MACRO \\\n" 6385 " some_very_long_func_aaaaaaaaaa();\n" 6386 " #endif\n" 6387 "#else\n" 6388 " #define A 1\n" 6389 "#endif", 6390 Style); 6391 verifyFormat("#if A\n" 6392 " #define MACRO \\\n" 6393 " void a(int x) { \\\n" 6394 " b(); \\\n" 6395 " c(); \\\n" 6396 " d(); \\\n" 6397 " e(); \\\n" 6398 " f(); \\\n" 6399 " }\n" 6400 "#endif", 6401 Style); 6402 // Keep comments aligned with indented directives. These 6403 // tests cannot use verifyFormat because messUp manipulates leading 6404 // whitespace. 6405 { 6406 const char *Expected = "void f() {\n" 6407 "// Aligned to preprocessor.\n" 6408 "#if 1\n" 6409 " // Aligned to code.\n" 6410 " int a;\n" 6411 " #if 1\n" 6412 " // Aligned to preprocessor.\n" 6413 " #define A 0\n" 6414 " // Aligned to code.\n" 6415 " int b;\n" 6416 " #endif\n" 6417 "#endif\n" 6418 "}"; 6419 const char *ToFormat = "void f() {\n" 6420 "// Aligned to preprocessor.\n" 6421 "#if 1\n" 6422 "// Aligned to code.\n" 6423 "int a;\n" 6424 "#if 1\n" 6425 "// Aligned to preprocessor.\n" 6426 "#define A 0\n" 6427 "// Aligned to code.\n" 6428 "int b;\n" 6429 "#endif\n" 6430 "#endif\n" 6431 "}"; 6432 verifyFormat(Expected, ToFormat, Style); 6433 verifyNoChange(Expected, Style); 6434 } 6435 { 6436 const char *Expected = "void f() {\n" 6437 "/* Aligned to preprocessor. */\n" 6438 "#if 1\n" 6439 " /* Aligned to code. */\n" 6440 " int a;\n" 6441 " #if 1\n" 6442 " /* Aligned to preprocessor. */\n" 6443 " #define A 0\n" 6444 " /* Aligned to code. */\n" 6445 " int b;\n" 6446 " #endif\n" 6447 "#endif\n" 6448 "}"; 6449 const char *ToFormat = "void f() {\n" 6450 "/* Aligned to preprocessor. */\n" 6451 "#if 1\n" 6452 "/* Aligned to code. */\n" 6453 "int a;\n" 6454 "#if 1\n" 6455 "/* Aligned to preprocessor. */\n" 6456 "#define A 0\n" 6457 "/* Aligned to code. */\n" 6458 "int b;\n" 6459 "#endif\n" 6460 "#endif\n" 6461 "}"; 6462 verifyFormat(Expected, ToFormat, Style); 6463 verifyNoChange(Expected, Style); 6464 } 6465 6466 // Test single comment before preprocessor 6467 verifyFormat("// Comment\n" 6468 "\n" 6469 "#if 1\n" 6470 "#endif", 6471 Style); 6472 } 6473 6474 TEST_F(FormatTest, FormatAlignInsidePreprocessorElseBlock) { 6475 FormatStyle Style = getLLVMStyle(); 6476 Style.AlignConsecutiveAssignments.Enabled = true; 6477 Style.AlignConsecutiveDeclarations.Enabled = true; 6478 6479 // Test with just #if blocks. 6480 verifyFormat("void f1() {\n" 6481 "#if 1\n" 6482 " int foo = 1;\n" 6483 " int foobar = 2;\n" 6484 "#endif\n" 6485 "}\n" 6486 "#if 1\n" 6487 "int baz = 3;\n" 6488 "#endif\n" 6489 "void f2() {\n" 6490 "#if 1\n" 6491 " char *foobarbaz = \"foobarbaz\";\n" 6492 " int quux = 4;\n" 6493 "}", 6494 Style); 6495 6496 // Test with just #else blocks. 6497 verifyFormat("void f1() {\n" 6498 "#if 1\n" 6499 "#else\n" 6500 " int foo = 1;\n" 6501 " int foobar = 2;\n" 6502 "#endif\n" 6503 "}\n" 6504 "#if 1\n" 6505 "#else\n" 6506 "int baz = 3;\n" 6507 "#endif\n" 6508 "void f2() {\n" 6509 "#if 1\n" 6510 "#else\n" 6511 " char *foobarbaz = \"foobarbaz\";\n" 6512 " int quux = 4;\n" 6513 "}", 6514 Style); 6515 verifyFormat("auto foo = [] { return; };\n" 6516 "#if FOO\n" 6517 "#else\n" 6518 "count = bar;\n" 6519 "mbid = bid;\n" 6520 "#endif", 6521 Style); 6522 6523 // Test with a mix of #if and #else blocks. 6524 verifyFormat("void f1() {\n" 6525 "#if 1\n" 6526 "#else\n" 6527 " int foo = 1;\n" 6528 " int foobar = 2;\n" 6529 "#endif\n" 6530 "}\n" 6531 "#if 1\n" 6532 "int baz = 3;\n" 6533 "#endif\n" 6534 "void f2() {\n" 6535 "#if 1\n" 6536 "#else\n" 6537 " // prevent alignment with #else in f1\n" 6538 " char *foobarbaz = \"foobarbaz\";\n" 6539 " int quux = 4;\n" 6540 "}", 6541 Style); 6542 6543 // Test with nested #if and #else blocks. 6544 verifyFormat("void f1() {\n" 6545 "#if 1\n" 6546 "#else\n" 6547 "#if 2\n" 6548 "#else\n" 6549 " int foo = 1;\n" 6550 " int foobar = 2;\n" 6551 "#endif\n" 6552 "#endif\n" 6553 "}\n" 6554 "#if 1\n" 6555 "#else\n" 6556 "#if 2\n" 6557 "int baz = 3;\n" 6558 "#endif\n" 6559 "#endif\n" 6560 "void f2() {\n" 6561 "#if 1\n" 6562 "#if 2\n" 6563 "#else\n" 6564 " // prevent alignment with #else in f1\n" 6565 " char *foobarbaz = \"foobarbaz\";\n" 6566 " int quux = 4;\n" 6567 "#endif\n" 6568 "#endif\n" 6569 "}", 6570 Style); 6571 6572 verifyFormat("#if FOO\n" 6573 "int a = 1;\n" 6574 "#else\n" 6575 "int ab = 2;\n" 6576 "#endif\n" 6577 "#ifdef BAR\n" 6578 "int abc = 3;\n" 6579 "#elifdef BAZ\n" 6580 "int abcd = 4;\n" 6581 "#endif", 6582 Style); 6583 6584 verifyFormat("void f() {\n" 6585 " if (foo) {\n" 6586 "#if FOO\n" 6587 " int a = 1;\n" 6588 "#else\n" 6589 " bool a = true;\n" 6590 "#endif\n" 6591 " int abc = 3;\n" 6592 "#ifndef BAR\n" 6593 " int abcd = 4;\n" 6594 "#elif BAZ\n" 6595 " bool abcd = true;\n" 6596 "#endif\n" 6597 " }\n" 6598 "}", 6599 Style); 6600 6601 verifyFormat("void f() {\n" 6602 "#if FOO\n" 6603 " a = 1;\n" 6604 "#else\n" 6605 " ab = 2;\n" 6606 "#endif\n" 6607 "}\n" 6608 "void g() {\n" 6609 "#if BAR\n" 6610 " abc = 3;\n" 6611 "#elifndef BAZ\n" 6612 " abcd = 4;\n" 6613 "#endif\n" 6614 "}", 6615 Style); 6616 } 6617 6618 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { 6619 verifyFormat("{\n" 6620 " {\n" 6621 " a #c;\n" 6622 " }\n" 6623 "}"); 6624 } 6625 6626 TEST_F(FormatTest, FormatUnbalancedStructuralElements) { 6627 verifyFormat("#define A \\\n { \\\n {\nint i;", 6628 "#define A { {\nint i;", getLLVMStyleWithColumns(11)); 6629 verifyFormat("#define A \\\n } \\\n }\nint i;", 6630 "#define A } }\nint i;", getLLVMStyleWithColumns(11)); 6631 } 6632 6633 TEST_F(FormatTest, EscapedNewlines) { 6634 FormatStyle Narrow = getLLVMStyleWithColumns(11); 6635 verifyFormat("#define A \\\n int i; \\\n int j;", 6636 "#define A \\\nint i;\\\n int j;", Narrow); 6637 verifyFormat("#define A\n\nint i;", "#define A \\\n\n int i;"); 6638 verifyFormat("template <class T> f();", "\\\ntemplate <class T> f();"); 6639 verifyFormat("/* \\ \\ \\\n */", "\\\n/* \\ \\ \\\n */"); 6640 verifyNoChange("<a\n\\\\\n>"); 6641 6642 FormatStyle AlignLeft = getLLVMStyle(); 6643 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 6644 verifyFormat("#define MACRO(x) \\\n" 6645 "private: \\\n" 6646 " int x(int a);", 6647 AlignLeft); 6648 6649 // CRLF line endings 6650 verifyFormat("#define A \\\r\n int i; \\\r\n int j;", 6651 "#define A \\\r\nint i;\\\r\n int j;", Narrow); 6652 verifyFormat("#define A\r\n\r\nint i;", "#define A \\\r\n\r\n int i;"); 6653 verifyFormat("template <class T> f();", "\\\ntemplate <class T> f();"); 6654 verifyFormat("/* \\ \\ \\\r\n */", "\\\r\n/* \\ \\ \\\r\n */"); 6655 verifyNoChange("<a\r\n\\\\\r\n>"); 6656 verifyFormat("#define MACRO(x) \\\r\n" 6657 "private: \\\r\n" 6658 " int x(int a);", 6659 AlignLeft); 6660 6661 constexpr StringRef Code{"#define A \\\n" 6662 " int a123; \\\n" 6663 " int a; \\\n" 6664 " int a1234;"}; 6665 verifyFormat(Code, AlignLeft); 6666 6667 constexpr StringRef Code2{"#define A \\\n" 6668 " int a123; \\\n" 6669 " int a; \\\n" 6670 " int a1234;"}; 6671 auto LastLine = getLLVMStyle(); 6672 LastLine.AlignEscapedNewlines = FormatStyle::ENAS_LeftWithLastLine; 6673 verifyFormat(Code2, LastLine); 6674 6675 LastLine.ColumnLimit = 13; 6676 verifyFormat(Code, LastLine); 6677 6678 LastLine.ColumnLimit = 0; 6679 verifyFormat(Code2, LastLine); 6680 6681 FormatStyle DontAlign = getLLVMStyle(); 6682 DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 6683 DontAlign.MaxEmptyLinesToKeep = 3; 6684 // FIXME: can't use verifyFormat here because the newline before 6685 // "public:" is not inserted the first time it's reformatted 6686 verifyNoChange("#define A \\\n" 6687 " class Foo { \\\n" 6688 " void bar(); \\\n" 6689 "\\\n" 6690 "\\\n" 6691 "\\\n" 6692 " public: \\\n" 6693 " void baz(); \\\n" 6694 " };", 6695 DontAlign); 6696 } 6697 6698 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { 6699 verifyFormat("#define A \\\n" 6700 " int v( \\\n" 6701 " a); \\\n" 6702 " int i;", 6703 getLLVMStyleWithColumns(11)); 6704 } 6705 6706 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { 6707 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 6708 " \\\n" 6709 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 6710 "\n" 6711 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 6712 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);", 6713 " #define ALooooooooooooooooooooooooooooooooooooooongMacro(" 6714 "\\\n" 6715 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 6716 " \n" 6717 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 6718 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);"); 6719 } 6720 6721 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { 6722 verifyFormat("int\n" 6723 "#define A\n" 6724 " a;", 6725 "int\n#define A\na;"); 6726 verifyFormat("functionCallTo(\n" 6727 " someOtherFunction(\n" 6728 " withSomeParameters, whichInSequence,\n" 6729 " areLongerThanALine(andAnotherCall,\n" 6730 "#define A B\n" 6731 " withMoreParamters,\n" 6732 " whichStronglyInfluenceTheLayout),\n" 6733 " andMoreParameters),\n" 6734 " trailing);", 6735 getLLVMStyleWithColumns(69)); 6736 verifyFormat("Foo::Foo()\n" 6737 "#ifdef BAR\n" 6738 " : baz(0)\n" 6739 "#endif\n" 6740 "{\n" 6741 "}"); 6742 verifyFormat("void f() {\n" 6743 " if (true)\n" 6744 "#ifdef A\n" 6745 " f(42);\n" 6746 " x();\n" 6747 "#else\n" 6748 " g();\n" 6749 " x();\n" 6750 "#endif\n" 6751 "}"); 6752 verifyFormat("void f(param1, param2,\n" 6753 " param3,\n" 6754 "#ifdef A\n" 6755 " param4(param5,\n" 6756 "#ifdef A1\n" 6757 " param6,\n" 6758 "#ifdef A2\n" 6759 " param7),\n" 6760 "#else\n" 6761 " param8),\n" 6762 " param9,\n" 6763 "#endif\n" 6764 " param10,\n" 6765 "#endif\n" 6766 " param11)\n" 6767 "#else\n" 6768 " param12)\n" 6769 "#endif\n" 6770 "{\n" 6771 " x();\n" 6772 "}", 6773 getLLVMStyleWithColumns(28)); 6774 verifyFormat("#if 1\n" 6775 "int i;"); 6776 verifyFormat("#if 1\n" 6777 "#endif\n" 6778 "#if 1\n" 6779 "#else\n" 6780 "#endif"); 6781 verifyFormat("DEBUG({\n" 6782 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6783 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 6784 "});\n" 6785 "#if a\n" 6786 "#else\n" 6787 "#endif"); 6788 6789 verifyIncompleteFormat("void f(\n" 6790 "#if A\n" 6791 ");\n" 6792 "#else\n" 6793 "#endif"); 6794 6795 // Verify that indentation is correct when there is an `#if 0` with an 6796 // `#else`. 6797 verifyFormat("#if 0\n" 6798 "{\n" 6799 "#else\n" 6800 "{\n" 6801 "#endif\n" 6802 " x;\n" 6803 "}"); 6804 6805 verifyFormat("#if 0\n" 6806 "#endif\n" 6807 "#if X\n" 6808 "int something_fairly_long; // Align here please\n" 6809 "#endif // Should be aligned"); 6810 6811 verifyFormat("#if 0\n" 6812 "#endif\n" 6813 "#if X\n" 6814 "#else // Align\n" 6815 ";\n" 6816 "#endif // Align"); 6817 6818 verifyFormat("void SomeFunction(int param1,\n" 6819 " template <\n" 6820 "#ifdef A\n" 6821 "#if 0\n" 6822 "#endif\n" 6823 " MyType<Some>>\n" 6824 "#else\n" 6825 " Type1, Type2>\n" 6826 "#endif\n" 6827 " param2,\n" 6828 " param3) {\n" 6829 " f();\n" 6830 "}"); 6831 } 6832 6833 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) { 6834 verifyFormat("#endif\n" 6835 "#if B"); 6836 } 6837 6838 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) { 6839 FormatStyle SingleLine = getLLVMStyle(); 6840 SingleLine.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse; 6841 verifyFormat("#if 0\n" 6842 "#elif 1\n" 6843 "#endif\n" 6844 "void foo() {\n" 6845 " if (test) foo2();\n" 6846 "}", 6847 SingleLine); 6848 } 6849 6850 TEST_F(FormatTest, LayoutBlockInsideParens) { 6851 verifyFormat("functionCall({ int i; });"); 6852 verifyFormat("functionCall({\n" 6853 " int i;\n" 6854 " int j;\n" 6855 "});"); 6856 verifyFormat("functionCall(\n" 6857 " {\n" 6858 " int i;\n" 6859 " int j;\n" 6860 " },\n" 6861 " aaaa, bbbb, cccc);"); 6862 verifyFormat("functionA(functionB({\n" 6863 " int i;\n" 6864 " int j;\n" 6865 " }),\n" 6866 " aaaa, bbbb, cccc);"); 6867 verifyFormat("functionCall(\n" 6868 " {\n" 6869 " int i;\n" 6870 " int j;\n" 6871 " },\n" 6872 " aaaa, bbbb, // comment\n" 6873 " cccc);"); 6874 verifyFormat("functionA(functionB({\n" 6875 " int i;\n" 6876 " int j;\n" 6877 " }),\n" 6878 " aaaa, bbbb, // comment\n" 6879 " cccc);"); 6880 verifyFormat("functionCall(aaaa, bbbb, { int i; });"); 6881 verifyFormat("functionCall(aaaa, bbbb, {\n" 6882 " int i;\n" 6883 " int j;\n" 6884 "});"); 6885 verifyFormat( 6886 "Aaa(\n" // FIXME: There shouldn't be a linebreak here. 6887 " {\n" 6888 " int i; // break\n" 6889 " },\n" 6890 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 6891 " ccccccccccccccccc));"); 6892 verifyFormat("DEBUG({\n" 6893 " if (a)\n" 6894 " f();\n" 6895 "});"); 6896 } 6897 6898 TEST_F(FormatTest, LayoutBlockInsideStatement) { 6899 verifyFormat("SOME_MACRO { int i; }\n" 6900 "int i;", 6901 " SOME_MACRO {int i;} int i;"); 6902 } 6903 6904 TEST_F(FormatTest, LayoutNestedBlocks) { 6905 verifyFormat("void AddOsStrings(unsigned bitmask) {\n" 6906 " struct s {\n" 6907 " int i;\n" 6908 " };\n" 6909 " s kBitsToOs[] = {{10}};\n" 6910 " for (int i = 0; i < 10; ++i)\n" 6911 " return;\n" 6912 "}"); 6913 verifyFormat("call(parameter, {\n" 6914 " something();\n" 6915 " // Comment using all columns.\n" 6916 " somethingelse();\n" 6917 "});", 6918 getLLVMStyleWithColumns(40)); 6919 verifyFormat("DEBUG( //\n" 6920 " { f(); }, a);"); 6921 verifyFormat("DEBUG( //\n" 6922 " {\n" 6923 " f(); //\n" 6924 " },\n" 6925 " a);"); 6926 6927 verifyFormat("call(parameter, {\n" 6928 " something();\n" 6929 " // Comment too\n" 6930 " // looooooooooong.\n" 6931 " somethingElse();\n" 6932 "});", 6933 "call(parameter, {\n" 6934 " something();\n" 6935 " // Comment too looooooooooong.\n" 6936 " somethingElse();\n" 6937 "});", 6938 getLLVMStyleWithColumns(29)); 6939 verifyFormat("DEBUG({ int i; });", "DEBUG({ int i; });"); 6940 verifyFormat("DEBUG({ // comment\n" 6941 " int i;\n" 6942 "});", 6943 "DEBUG({ // comment\n" 6944 "int i;\n" 6945 "});"); 6946 verifyFormat("DEBUG({\n" 6947 " int i;\n" 6948 "\n" 6949 " // comment\n" 6950 " int j;\n" 6951 "});", 6952 "DEBUG({\n" 6953 " int i;\n" 6954 "\n" 6955 " // comment\n" 6956 " int j;\n" 6957 "});"); 6958 6959 verifyFormat("DEBUG({\n" 6960 " if (a)\n" 6961 " return;\n" 6962 "});"); 6963 verifyGoogleFormat("DEBUG({\n" 6964 " if (a) return;\n" 6965 "});"); 6966 FormatStyle Style = getGoogleStyle(); 6967 Style.ColumnLimit = 45; 6968 verifyFormat("Debug(\n" 6969 " aaaaa,\n" 6970 " {\n" 6971 " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n" 6972 " },\n" 6973 " a);", 6974 Style); 6975 6976 verifyFormat("SomeFunction({MACRO({ return output; }), b});"); 6977 6978 verifyNoCrash("^{v^{a}}"); 6979 } 6980 6981 TEST_F(FormatTest, FormatNestedBlocksInMacros) { 6982 verifyFormat("#define MACRO() \\\n" 6983 " Debug(aaa, /* force line break */ \\\n" 6984 " { \\\n" 6985 " int i; \\\n" 6986 " int j; \\\n" 6987 " })", 6988 "#define MACRO() Debug(aaa, /* force line break */ \\\n" 6989 " { int i; int j; })", 6990 getGoogleStyle()); 6991 6992 verifyFormat("#define A \\\n" 6993 " [] { \\\n" 6994 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 6995 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 6996 " }", 6997 "#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 6998 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 6999 getGoogleStyle()); 7000 } 7001 7002 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { 7003 verifyFormat("enum E {};"); 7004 verifyFormat("enum E {}"); 7005 FormatStyle Style = getLLVMStyle(); 7006 Style.SpaceInEmptyBlock = true; 7007 verifyFormat("void f() { }", "void f() {}", Style); 7008 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty; 7009 verifyFormat("{ }", Style); 7010 verifyFormat("while (true) { }", "while (true) {}", Style); 7011 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7012 Style.BraceWrapping.BeforeElse = false; 7013 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 7014 verifyFormat("if (a)\n" 7015 "{\n" 7016 "} else if (b)\n" 7017 "{\n" 7018 "} else\n" 7019 "{ }", 7020 Style); 7021 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never; 7022 verifyFormat("if (a) {\n" 7023 "} else if (b) {\n" 7024 "} else {\n" 7025 "}", 7026 Style); 7027 Style.BraceWrapping.BeforeElse = true; 7028 verifyFormat("if (a) { }\n" 7029 "else if (b) { }\n" 7030 "else { }", 7031 Style); 7032 7033 Style = getLLVMStyle(FormatStyle::LK_CSharp); 7034 Style.SpaceInEmptyBlock = true; 7035 verifyFormat("Event += () => { };", Style); 7036 } 7037 7038 TEST_F(FormatTest, FormatBeginBlockEndMacros) { 7039 FormatStyle Style = getLLVMStyle(); 7040 Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$"; 7041 Style.MacroBlockEnd = "^[A-Z_]+_END$"; 7042 verifyFormat("FOO_BEGIN\n" 7043 " FOO_ENTRY\n" 7044 "FOO_END", 7045 Style); 7046 verifyFormat("FOO_BEGIN\n" 7047 " NESTED_FOO_BEGIN\n" 7048 " NESTED_FOO_ENTRY\n" 7049 " NESTED_FOO_END\n" 7050 "FOO_END", 7051 Style); 7052 verifyFormat("FOO_BEGIN(Foo, Bar)\n" 7053 " int x;\n" 7054 " x = 1;\n" 7055 "FOO_END(Baz)", 7056 Style); 7057 7058 Style.RemoveBracesLLVM = true; 7059 verifyNoCrash("for (;;)\n" 7060 " FOO_BEGIN\n" 7061 " foo();\n" 7062 " FOO_END", 7063 Style); 7064 } 7065 7066 //===----------------------------------------------------------------------===// 7067 // Line break tests. 7068 //===----------------------------------------------------------------------===// 7069 7070 TEST_F(FormatTest, PreventConfusingIndents) { 7071 verifyFormat( 7072 "void f() {\n" 7073 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" 7074 " parameter, parameter, parameter)),\n" 7075 " SecondLongCall(parameter));\n" 7076 "}"); 7077 verifyFormat( 7078 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7079 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 7080 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 7081 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 7082 verifyFormat( 7083 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7084 " [aaaaaaaaaaaaaaaaaaaaaaaa\n" 7085 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 7086 " [aaaaaaaaaaaaaaaaaaaaaaaa]];"); 7087 verifyFormat( 7088 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 7089 " aaaaaaaaaaaaaaaaaaaaaaaa<\n" 7090 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n" 7091 " aaaaaaaaaaaaaaaaaaaaaaaa>;"); 7092 verifyFormat("int a = bbbb && ccc &&\n" 7093 " fffff(\n" 7094 "#define A Just forcing a new line\n" 7095 " ddd);"); 7096 } 7097 7098 TEST_F(FormatTest, LineBreakingInBinaryExpressions) { 7099 verifyFormat( 7100 "bool aaaaaaa =\n" 7101 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n" 7102 " bbbbbbbb();"); 7103 verifyFormat( 7104 "bool aaaaaaa =\n" 7105 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n" 7106 " bbbbbbbb();"); 7107 7108 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 7109 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n" 7110 " ccccccccc == ddddddddddd;"); 7111 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 7112 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n" 7113 " ccccccccc == ddddddddddd;"); 7114 verifyFormat( 7115 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 7116 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n" 7117 " ccccccccc == ddddddddddd;"); 7118 7119 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 7120 " aaaaaa) &&\n" 7121 " bbbbbb && cccccc;"); 7122 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 7123 " aaaaaa) >>\n" 7124 " bbbbbb;"); 7125 verifyFormat("aa = Whitespaces.addUntouchableComment(\n" 7126 " SourceMgr.getSpellingColumnNumber(\n" 7127 " TheLine.Last->FormatTok.Tok.getLocation()) -\n" 7128 " 1);"); 7129 7130 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 7131 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n" 7132 " cccccc) {\n}"); 7133 verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 7134 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n" 7135 " cccccc) {\n}"); 7136 verifyFormat("if CONSTEXPR ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 7137 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n" 7138 " cccccc) {\n}"); 7139 verifyFormat("b = a &&\n" 7140 " // Comment\n" 7141 " b.c && d;"); 7142 7143 // If the LHS of a comparison is not a binary expression itself, the 7144 // additional linebreak confuses many people. 7145 verifyFormat( 7146 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7147 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n" 7148 "}"); 7149 verifyFormat( 7150 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7151 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 7152 "}"); 7153 verifyFormat( 7154 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n" 7155 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 7156 "}"); 7157 verifyFormat( 7158 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7159 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n" 7160 "}"); 7161 // Even explicit parentheses stress the precedence enough to make the 7162 // additional break unnecessary. 7163 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7164 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 7165 "}"); 7166 // This cases is borderline, but with the indentation it is still readable. 7167 verifyFormat( 7168 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7169 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7170 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 7171 "}", 7172 getLLVMStyleWithColumns(75)); 7173 7174 // If the LHS is a binary expression, we should still use the additional break 7175 // as otherwise the formatting hides the operator precedence. 7176 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7177 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 7178 " 5) {\n" 7179 "}"); 7180 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7181 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n" 7182 " 5) {\n" 7183 "}"); 7184 7185 FormatStyle OnePerLine = getLLVMStyle(); 7186 OnePerLine.BinPackParameters = FormatStyle::BPPS_OnePerLine; 7187 verifyFormat( 7188 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 7189 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 7190 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}", 7191 OnePerLine); 7192 7193 verifyFormat("int i = someFunction(aaaaaaa, 0)\n" 7194 " .aaa(aaaaaaaaaaaaa) *\n" 7195 " aaaaaaa +\n" 7196 " aaaaaaa;", 7197 getLLVMStyleWithColumns(40)); 7198 } 7199 7200 TEST_F(FormatTest, ExpressionIndentation) { 7201 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7202 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7203 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 7204 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 7205 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 7206 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n" 7207 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 7208 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n" 7209 " ccccccccccccccccccccccccccccccccccccccccc;"); 7210 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 7211 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7212 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 7213 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 7214 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7215 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 7216 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 7217 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 7218 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 7219 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 7220 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7221 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 7222 verifyFormat("if () {\n" 7223 "} else if (aaaaa && bbbbb > // break\n" 7224 " ccccc) {\n" 7225 "}"); 7226 verifyFormat("if () {\n" 7227 "} else if constexpr (aaaaa && bbbbb > // break\n" 7228 " ccccc) {\n" 7229 "}"); 7230 verifyFormat("if () {\n" 7231 "} else if CONSTEXPR (aaaaa && bbbbb > // break\n" 7232 " ccccc) {\n" 7233 "}"); 7234 verifyFormat("if () {\n" 7235 "} else if (aaaaa &&\n" 7236 " bbbbb > // break\n" 7237 " ccccc &&\n" 7238 " ddddd) {\n" 7239 "}"); 7240 7241 // Presence of a trailing comment used to change indentation of b. 7242 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n" 7243 " b;\n" 7244 "return aaaaaaaaaaaaaaaaaaa +\n" 7245 " b; //", 7246 getLLVMStyleWithColumns(30)); 7247 } 7248 7249 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) { 7250 // Not sure what the best system is here. Like this, the LHS can be found 7251 // immediately above an operator (everything with the same or a higher 7252 // indent). The RHS is aligned right of the operator and so compasses 7253 // everything until something with the same indent as the operator is found. 7254 // FIXME: Is this a good system? 7255 FormatStyle Style = getLLVMStyle(); 7256 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 7257 verifyFormat( 7258 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7259 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7260 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7261 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7262 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 7263 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 7264 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7265 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7266 " > ccccccccccccccccccccccccccccccccccccccccc;", 7267 Style); 7268 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7269 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7270 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7271 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 7272 Style); 7273 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7274 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7275 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7276 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 7277 Style); 7278 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7279 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7280 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7281 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 7282 Style); 7283 verifyFormat("if () {\n" 7284 "} else if (aaaaa\n" 7285 " && bbbbb // break\n" 7286 " > ccccc) {\n" 7287 "}", 7288 Style); 7289 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7290 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 7291 Style); 7292 verifyFormat("return (a)\n" 7293 " // comment\n" 7294 " + b;", 7295 Style); 7296 verifyFormat( 7297 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7298 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 7299 " + cc;", 7300 Style); 7301 7302 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7303 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 7304 Style); 7305 7306 // Forced by comments. 7307 verifyFormat( 7308 "unsigned ContentSize =\n" 7309 " sizeof(int16_t) // DWARF ARange version number\n" 7310 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 7311 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 7312 " + sizeof(int8_t); // Segment Size (in bytes)"); 7313 7314 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 7315 " == boost::fusion::at_c<1>(iiii).second;", 7316 Style); 7317 7318 Style.ColumnLimit = 60; 7319 verifyFormat("zzzzzzzzzz\n" 7320 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 7321 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 7322 Style); 7323 7324 Style.ColumnLimit = 80; 7325 Style.IndentWidth = 4; 7326 Style.TabWidth = 4; 7327 Style.UseTab = FormatStyle::UT_Always; 7328 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 7329 Style.AlignOperands = FormatStyle::OAS_DontAlign; 7330 verifyFormat("return someVeryVeryLongConditionThatBarelyFitsOnALine\n" 7331 "\t&& (someOtherLongishConditionPart1\n" 7332 "\t\t|| someOtherEvenLongerNestedConditionPart2);", 7333 "return someVeryVeryLongConditionThatBarelyFitsOnALine && " 7334 "(someOtherLongishConditionPart1 || " 7335 "someOtherEvenLongerNestedConditionPart2);", 7336 Style); 7337 7338 Style = getLLVMStyleWithColumns(20); 7339 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 7340 Style.BinPackParameters = FormatStyle::BPPS_OnePerLine; 7341 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 7342 Style.ContinuationIndentWidth = 2; 7343 verifyFormat("struct Foo {\n" 7344 " Foo(\n" 7345 " int arg1,\n" 7346 " int arg2)\n" 7347 " : Base(\n" 7348 " arg1,\n" 7349 " arg2) {}\n" 7350 "};", 7351 Style); 7352 verifyFormat("return abc\n" 7353 " ? foo(\n" 7354 " a,\n" 7355 " b,\n" 7356 " bar(\n" 7357 " abc))\n" 7358 " : g(abc);", 7359 Style); 7360 } 7361 7362 TEST_F(FormatTest, ExpressionIndentationStrictAlign) { 7363 FormatStyle Style = getLLVMStyle(); 7364 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 7365 Style.AlignOperands = FormatStyle::OAS_AlignAfterOperator; 7366 7367 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7368 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7369 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7370 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7371 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 7372 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 7373 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7374 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7375 " > ccccccccccccccccccccccccccccccccccccccccc;", 7376 Style); 7377 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7378 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7379 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7380 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 7381 Style); 7382 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7383 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7384 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7385 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 7386 Style); 7387 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7388 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7389 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7390 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 7391 Style); 7392 verifyFormat("if () {\n" 7393 "} else if (aaaaa\n" 7394 " && bbbbb // break\n" 7395 " > ccccc) {\n" 7396 "}", 7397 Style); 7398 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7399 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 7400 Style); 7401 verifyFormat("return (a)\n" 7402 " // comment\n" 7403 " + b;", 7404 Style); 7405 verifyFormat( 7406 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7407 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 7408 " + cc;", 7409 Style); 7410 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7411 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n" 7412 " : 3333333333333333;", 7413 Style); 7414 verifyFormat( 7415 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7416 " : ccccccccccccccc ? dddddddddddddddddd\n" 7417 " : eeeeeeeeeeeeeeeeee)\n" 7418 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n" 7419 " : 3333333333333333;", 7420 Style); 7421 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7422 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 7423 Style); 7424 7425 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 7426 " == boost::fusion::at_c<1>(iiii).second;", 7427 Style); 7428 7429 Style.ColumnLimit = 60; 7430 verifyFormat("zzzzzzzzzzzzz\n" 7431 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 7432 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 7433 Style); 7434 7435 // Forced by comments. 7436 Style.ColumnLimit = 80; 7437 verifyFormat( 7438 "unsigned ContentSize\n" 7439 " = sizeof(int16_t) // DWARF ARange version number\n" 7440 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 7441 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 7442 " + sizeof(int8_t); // Segment Size (in bytes)", 7443 Style); 7444 7445 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 7446 verifyFormat( 7447 "unsigned ContentSize =\n" 7448 " sizeof(int16_t) // DWARF ARange version number\n" 7449 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 7450 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 7451 " + sizeof(int8_t); // Segment Size (in bytes)", 7452 Style); 7453 7454 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 7455 verifyFormat( 7456 "unsigned ContentSize =\n" 7457 " sizeof(int16_t) // DWARF ARange version number\n" 7458 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 7459 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 7460 " + sizeof(int8_t); // Segment Size (in bytes)", 7461 Style); 7462 } 7463 7464 TEST_F(FormatTest, EnforcedOperatorWraps) { 7465 // Here we'd like to wrap after the || operators, but a comment is forcing an 7466 // earlier wrap. 7467 verifyFormat("bool x = aaaaa //\n" 7468 " || bbbbb\n" 7469 " //\n" 7470 " || cccc;"); 7471 } 7472 7473 TEST_F(FormatTest, NoOperandAlignment) { 7474 FormatStyle Style = getLLVMStyle(); 7475 Style.AlignOperands = FormatStyle::OAS_DontAlign; 7476 verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n" 7477 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7478 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7479 Style); 7480 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 7481 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7482 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7483 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7484 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7485 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 7486 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 7487 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7488 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7489 " > ccccccccccccccccccccccccccccccccccccccccc;", 7490 Style); 7491 7492 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7493 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 7494 " + cc;", 7495 Style); 7496 verifyFormat("int a = aa\n" 7497 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 7498 " * cccccccccccccccccccccccccccccccccccc;", 7499 Style); 7500 7501 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 7502 verifyFormat("return (a > b\n" 7503 " // comment1\n" 7504 " // comment2\n" 7505 " || c);", 7506 Style); 7507 } 7508 7509 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) { 7510 FormatStyle Style = getLLVMStyle(); 7511 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 7512 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 7513 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7514 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 7515 Style); 7516 } 7517 7518 TEST_F(FormatTest, AllowBinPackingInsideArguments) { 7519 FormatStyle Style = getLLVMStyleWithColumns(40); 7520 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 7521 Style.BinPackArguments = false; 7522 verifyFormat("void test() {\n" 7523 " someFunction(\n" 7524 " this + argument + is + quite\n" 7525 " + long + so + it + gets + wrapped\n" 7526 " + but + remains + bin - packed);\n" 7527 "}", 7528 Style); 7529 verifyFormat("void test() {\n" 7530 " someFunction(arg1,\n" 7531 " this + argument + is\n" 7532 " + quite + long + so\n" 7533 " + it + gets + wrapped\n" 7534 " + but + remains + bin\n" 7535 " - packed,\n" 7536 " arg3);\n" 7537 "}", 7538 Style); 7539 verifyFormat("void test() {\n" 7540 " someFunction(\n" 7541 " arg1,\n" 7542 " this + argument + has\n" 7543 " + anotherFunc(nested,\n" 7544 " calls + whose\n" 7545 " + arguments\n" 7546 " + are + also\n" 7547 " + wrapped,\n" 7548 " in + addition)\n" 7549 " + to + being + bin - packed,\n" 7550 " arg3);\n" 7551 "}", 7552 Style); 7553 7554 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 7555 verifyFormat("void test() {\n" 7556 " someFunction(\n" 7557 " arg1,\n" 7558 " this + argument + has +\n" 7559 " anotherFunc(nested,\n" 7560 " calls + whose +\n" 7561 " arguments +\n" 7562 " are + also +\n" 7563 " wrapped,\n" 7564 " in + addition) +\n" 7565 " to + being + bin - packed,\n" 7566 " arg3);\n" 7567 "}", 7568 Style); 7569 } 7570 7571 TEST_F(FormatTest, BreakBinaryOperatorsInPresenceOfTemplates) { 7572 auto Style = getLLVMStyleWithColumns(45); 7573 EXPECT_EQ(Style.BreakBeforeBinaryOperators, FormatStyle::BOS_None); 7574 verifyFormat("bool b =\n" 7575 " is_default_constructible_v<hash<T>> and\n" 7576 " is_copy_constructible_v<hash<T>> and\n" 7577 " is_move_constructible_v<hash<T>> and\n" 7578 " is_copy_assignable_v<hash<T>> and\n" 7579 " is_move_assignable_v<hash<T>> and\n" 7580 " is_destructible_v<hash<T>> and\n" 7581 " is_swappable_v<hash<T>> and\n" 7582 " is_callable_v<hash<T>(T)>;", 7583 Style); 7584 7585 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 7586 verifyFormat("bool b = is_default_constructible_v<hash<T>>\n" 7587 " and is_copy_constructible_v<hash<T>>\n" 7588 " and is_move_constructible_v<hash<T>>\n" 7589 " and is_copy_assignable_v<hash<T>>\n" 7590 " and is_move_assignable_v<hash<T>>\n" 7591 " and is_destructible_v<hash<T>>\n" 7592 " and is_swappable_v<hash<T>>\n" 7593 " and is_callable_v<hash<T>(T)>;", 7594 Style); 7595 7596 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 7597 verifyFormat("bool b = is_default_constructible_v<hash<T>>\n" 7598 " and is_copy_constructible_v<hash<T>>\n" 7599 " and is_move_constructible_v<hash<T>>\n" 7600 " and is_copy_assignable_v<hash<T>>\n" 7601 " and is_move_assignable_v<hash<T>>\n" 7602 " and is_destructible_v<hash<T>>\n" 7603 " and is_swappable_v<hash<T>>\n" 7604 " and is_callable_v<hash<T>(T)>;", 7605 Style); 7606 } 7607 7608 TEST_F(FormatTest, ConstructorInitializers) { 7609 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 7610 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", 7611 getLLVMStyleWithColumns(45)); 7612 verifyFormat("Constructor()\n" 7613 " : Inttializer(FitsOnTheLine) {}", 7614 getLLVMStyleWithColumns(44)); 7615 verifyFormat("Constructor()\n" 7616 " : Inttializer(FitsOnTheLine) {}", 7617 getLLVMStyleWithColumns(43)); 7618 7619 verifyFormat("template <typename T>\n" 7620 "Constructor() : Initializer(FitsOnTheLine) {}", 7621 getLLVMStyleWithColumns(45)); 7622 7623 verifyFormat( 7624 "SomeClass::Constructor()\n" 7625 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 7626 7627 verifyFormat( 7628 "SomeClass::Constructor()\n" 7629 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 7630 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}"); 7631 verifyFormat( 7632 "SomeClass::Constructor()\n" 7633 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 7634 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 7635 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7636 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7637 " : aaaaaaaaaa(aaaaaa) {}"); 7638 7639 verifyFormat("Constructor()\n" 7640 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 7641 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7642 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 7643 " aaaaaaaaaaaaaaaaaaaaaaa() {}"); 7644 7645 verifyFormat("Constructor()\n" 7646 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7647 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 7648 7649 verifyFormat("Constructor(int Parameter = 0)\n" 7650 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 7651 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}"); 7652 verifyFormat("Constructor()\n" 7653 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 7654 "}", 7655 getLLVMStyleWithColumns(60)); 7656 verifyFormat("Constructor()\n" 7657 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7658 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}"); 7659 7660 // Here a line could be saved by splitting the second initializer onto two 7661 // lines, but that is not desirable. 7662 verifyFormat("Constructor()\n" 7663 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 7664 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 7665 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 7666 7667 FormatStyle OnePerLine = getLLVMStyle(); 7668 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_Never; 7669 verifyFormat("MyClass::MyClass()\n" 7670 " : a(a),\n" 7671 " b(b),\n" 7672 " c(c) {}", 7673 OnePerLine); 7674 verifyFormat("MyClass::MyClass()\n" 7675 " : a(a), // comment\n" 7676 " b(b),\n" 7677 " c(c) {}", 7678 OnePerLine); 7679 verifyFormat("MyClass::MyClass(int a)\n" 7680 " : b(a), // comment\n" 7681 " c(a + 1) { // lined up\n" 7682 "}", 7683 OnePerLine); 7684 verifyFormat("Constructor()\n" 7685 " : a(b, b, b) {}", 7686 OnePerLine); 7687 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 7688 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false; 7689 verifyFormat("SomeClass::Constructor()\n" 7690 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 7691 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 7692 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 7693 OnePerLine); 7694 verifyFormat("SomeClass::Constructor()\n" 7695 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 7696 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 7697 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 7698 OnePerLine); 7699 verifyFormat("MyClass::MyClass(int var)\n" 7700 " : some_var_(var), // 4 space indent\n" 7701 " some_other_var_(var + 1) { // lined up\n" 7702 "}", 7703 OnePerLine); 7704 verifyFormat("Constructor()\n" 7705 " : aaaaa(aaaaaa),\n" 7706 " aaaaa(aaaaaa),\n" 7707 " aaaaa(aaaaaa),\n" 7708 " aaaaa(aaaaaa),\n" 7709 " aaaaa(aaaaaa) {}", 7710 OnePerLine); 7711 verifyFormat("Constructor()\n" 7712 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 7713 " aaaaaaaaaaaaaaaaaaaaaa) {}", 7714 OnePerLine); 7715 OnePerLine.BinPackParameters = FormatStyle::BPPS_OnePerLine; 7716 verifyFormat( 7717 "Constructor()\n" 7718 " : aaaaaaaaaaaaaaaaaaaaaaaa(\n" 7719 " aaaaaaaaaaa().aaa(),\n" 7720 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 7721 OnePerLine); 7722 OnePerLine.ColumnLimit = 60; 7723 verifyFormat("Constructor()\n" 7724 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 7725 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 7726 OnePerLine); 7727 7728 verifyFormat("Constructor()\n" 7729 " : // Comment forcing unwanted break.\n" 7730 " aaaa(aaaa) {}", 7731 "Constructor() :\n" 7732 " // Comment forcing unwanted break.\n" 7733 " aaaa(aaaa) {}"); 7734 } 7735 7736 TEST_F(FormatTest, AllowAllConstructorInitializersOnNextLine) { 7737 FormatStyle Style = getLLVMStyleWithColumns(60); 7738 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 7739 Style.BinPackParameters = FormatStyle::BPPS_OnePerLine; 7740 7741 for (int i = 0; i < 4; ++i) { 7742 // Test all combinations of parameters that should not have an effect. 7743 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1; 7744 Style.AllowAllArgumentsOnNextLine = i & 2; 7745 7746 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 7747 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 7748 verifyFormat("Constructor()\n" 7749 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 7750 Style); 7751 verifyFormat("Constructor() : a(a), b(b) {}", Style); 7752 7753 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 7754 verifyFormat("Constructor()\n" 7755 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 7756 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 7757 Style); 7758 verifyFormat("Constructor() : a(a), b(b) {}", Style); 7759 7760 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly; 7761 verifyFormat("Constructor()\n" 7762 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 7763 Style); 7764 verifyFormat("Constructor()\n" 7765 " : a(a), b(b) {}", 7766 Style); 7767 verifyFormat("Constructor()\n" 7768 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 7769 " , bbbbbbbbbbbbbbbbbbbbb(b)\n" 7770 " , cccccccccccccccccccccc(c) {}", 7771 Style); 7772 7773 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 7774 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 7775 verifyFormat("Constructor()\n" 7776 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 7777 Style); 7778 7779 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 7780 verifyFormat("Constructor()\n" 7781 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 7782 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 7783 Style); 7784 7785 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly; 7786 verifyFormat("Constructor()\n" 7787 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 7788 Style); 7789 verifyFormat("Constructor()\n" 7790 " : a(a), b(b) {}", 7791 Style); 7792 verifyFormat("Constructor()\n" 7793 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 7794 " bbbbbbbbbbbbbbbbbbbbb(b),\n" 7795 " cccccccccccccccccccccc(c) {}", 7796 Style); 7797 7798 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 7799 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 7800 verifyFormat("Constructor() :\n" 7801 " aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 7802 Style); 7803 7804 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 7805 verifyFormat("Constructor() :\n" 7806 " aaaaaaaaaaaaaaaaaa(a),\n" 7807 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 7808 Style); 7809 7810 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly; 7811 verifyFormat("Constructor() :\n" 7812 " aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 7813 Style); 7814 verifyFormat("Constructor() :\n" 7815 " a(a), b(b) {}", 7816 Style); 7817 verifyFormat("Constructor() :\n" 7818 " aaaaaaaaaaaaaaaaaaaa(a),\n" 7819 " bbbbbbbbbbbbbbbbbbbbb(b),\n" 7820 " cccccccccccccccccccccc(c) {}", 7821 Style); 7822 } 7823 7824 // Test interactions between AllowAllParametersOfDeclarationOnNextLine and 7825 // AllowAllConstructorInitializersOnNextLine in all 7826 // BreakConstructorInitializers modes 7827 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 7828 Style.AllowAllParametersOfDeclarationOnNextLine = true; 7829 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 7830 verifyFormat("SomeClassWithALongName::Constructor(\n" 7831 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n" 7832 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 7833 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 7834 Style); 7835 7836 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 7837 verifyFormat("SomeClassWithALongName::Constructor(\n" 7838 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 7839 " int bbbbbbbbbbbbb,\n" 7840 " int cccccccccccccccc)\n" 7841 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 7842 Style); 7843 7844 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly; 7845 verifyFormat("SomeClassWithALongName::Constructor(\n" 7846 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 7847 " int bbbbbbbbbbbbb,\n" 7848 " int cccccccccccccccc)\n" 7849 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 7850 Style); 7851 7852 Style.AllowAllParametersOfDeclarationOnNextLine = false; 7853 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 7854 verifyFormat("SomeClassWithALongName::Constructor(\n" 7855 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 7856 " int bbbbbbbbbbbbb)\n" 7857 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 7858 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 7859 Style); 7860 7861 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 7862 7863 Style.AllowAllParametersOfDeclarationOnNextLine = true; 7864 verifyFormat("SomeClassWithALongName::Constructor(\n" 7865 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n" 7866 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 7867 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 7868 Style); 7869 7870 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 7871 verifyFormat("SomeClassWithALongName::Constructor(\n" 7872 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 7873 " int bbbbbbbbbbbbb,\n" 7874 " int cccccccccccccccc)\n" 7875 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 7876 Style); 7877 7878 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly; 7879 verifyFormat("SomeClassWithALongName::Constructor(\n" 7880 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 7881 " int bbbbbbbbbbbbb,\n" 7882 " int cccccccccccccccc)\n" 7883 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 7884 Style); 7885 7886 Style.AllowAllParametersOfDeclarationOnNextLine = false; 7887 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 7888 verifyFormat("SomeClassWithALongName::Constructor(\n" 7889 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 7890 " int bbbbbbbbbbbbb)\n" 7891 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 7892 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 7893 Style); 7894 7895 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 7896 Style.AllowAllParametersOfDeclarationOnNextLine = true; 7897 verifyFormat("SomeClassWithALongName::Constructor(\n" 7898 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb) :\n" 7899 " aaaaaaaaaaaaaaaaaaaa(a),\n" 7900 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 7901 Style); 7902 7903 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 7904 verifyFormat("SomeClassWithALongName::Constructor(\n" 7905 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 7906 " int bbbbbbbbbbbbb,\n" 7907 " int cccccccccccccccc) :\n" 7908 " aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 7909 Style); 7910 7911 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly; 7912 verifyFormat("SomeClassWithALongName::Constructor(\n" 7913 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 7914 " int bbbbbbbbbbbbb,\n" 7915 " int cccccccccccccccc) :\n" 7916 " aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 7917 Style); 7918 7919 Style.AllowAllParametersOfDeclarationOnNextLine = false; 7920 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 7921 verifyFormat("SomeClassWithALongName::Constructor(\n" 7922 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 7923 " int bbbbbbbbbbbbb) :\n" 7924 " aaaaaaaaaaaaaaaaaaaa(a),\n" 7925 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 7926 Style); 7927 7928 Style = getLLVMStyleWithColumns(0); 7929 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 7930 verifyFormat("Foo(Bar bar, Baz baz) : bar(bar), baz(baz) {}", Style); 7931 verifyNoChange("Foo(Bar bar, Baz baz)\n" 7932 " : bar(bar), baz(baz) {}", 7933 Style); 7934 } 7935 7936 TEST_F(FormatTest, AllowAllArgumentsOnNextLine) { 7937 FormatStyle Style = getLLVMStyleWithColumns(60); 7938 Style.BinPackArguments = false; 7939 for (int i = 0; i < 4; ++i) { 7940 // Test all combinations of parameters that should not have an effect. 7941 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1; 7942 Style.PackConstructorInitializers = 7943 i & 2 ? FormatStyle::PCIS_BinPack : FormatStyle::PCIS_Never; 7944 7945 Style.AllowAllArgumentsOnNextLine = true; 7946 verifyFormat("void foo() {\n" 7947 " FunctionCallWithReallyLongName(\n" 7948 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb);\n" 7949 "}", 7950 Style); 7951 Style.AllowAllArgumentsOnNextLine = false; 7952 verifyFormat("void foo() {\n" 7953 " FunctionCallWithReallyLongName(\n" 7954 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7955 " bbbbbbbbbbbb);\n" 7956 "}", 7957 Style); 7958 7959 Style.AllowAllArgumentsOnNextLine = true; 7960 verifyFormat("void foo() {\n" 7961 " auto VariableWithReallyLongName = {\n" 7962 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb};\n" 7963 "}", 7964 Style); 7965 Style.AllowAllArgumentsOnNextLine = false; 7966 verifyFormat("void foo() {\n" 7967 " auto VariableWithReallyLongName = {\n" 7968 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7969 " bbbbbbbbbbbb};\n" 7970 "}", 7971 Style); 7972 } 7973 7974 // This parameter should not affect declarations. 7975 Style.BinPackParameters = FormatStyle::BPPS_OnePerLine; 7976 Style.AllowAllArgumentsOnNextLine = false; 7977 Style.AllowAllParametersOfDeclarationOnNextLine = true; 7978 verifyFormat("void FunctionCallWithReallyLongName(\n" 7979 " int aaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbb);", 7980 Style); 7981 Style.AllowAllParametersOfDeclarationOnNextLine = false; 7982 verifyFormat("void FunctionCallWithReallyLongName(\n" 7983 " int aaaaaaaaaaaaaaaaaaaaaaa,\n" 7984 " int bbbbbbbbbbbb);", 7985 Style); 7986 } 7987 7988 TEST_F(FormatTest, AllowAllArgumentsOnNextLineDontAlign) { 7989 // Check that AllowAllArgumentsOnNextLine is respected for both BAS_DontAlign 7990 // and BAS_Align. 7991 FormatStyle Style = getLLVMStyleWithColumns(35); 7992 StringRef Input = "functionCall(paramA, paramB, paramC);\n" 7993 "void functionDecl(int A, int B, int C);"; 7994 Style.AllowAllArgumentsOnNextLine = false; 7995 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 7996 verifyFormat(StringRef("functionCall(paramA, paramB,\n" 7997 " paramC);\n" 7998 "void functionDecl(int A, int B,\n" 7999 " int C);"), 8000 Input, Style); 8001 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 8002 verifyFormat(StringRef("functionCall(paramA, paramB,\n" 8003 " paramC);\n" 8004 "void functionDecl(int A, int B,\n" 8005 " int C);"), 8006 Input, Style); 8007 // However, BAS_AlwaysBreak and BAS_BlockIndent should take precedence over 8008 // AllowAllArgumentsOnNextLine. 8009 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 8010 verifyFormat(StringRef("functionCall(\n" 8011 " paramA, paramB, paramC);\n" 8012 "void functionDecl(\n" 8013 " int A, int B, int C);"), 8014 Input, Style); 8015 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; 8016 verifyFormat("functionCall(\n" 8017 " paramA, paramB, paramC\n" 8018 ");\n" 8019 "void functionDecl(\n" 8020 " int A, int B, int C\n" 8021 ");", 8022 Input, Style); 8023 8024 // When AllowAllArgumentsOnNextLine is set, we prefer breaking before the 8025 // first argument. 8026 Style.AllowAllArgumentsOnNextLine = true; 8027 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 8028 verifyFormat(StringRef("functionCall(\n" 8029 " paramA, paramB, paramC);\n" 8030 "void functionDecl(\n" 8031 " int A, int B, int C);"), 8032 Input, Style); 8033 // It wouldn't fit on one line with aligned parameters so this setting 8034 // doesn't change anything for BAS_Align. 8035 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 8036 verifyFormat(StringRef("functionCall(paramA, paramB,\n" 8037 " paramC);\n" 8038 "void functionDecl(int A, int B,\n" 8039 " int C);"), 8040 Input, Style); 8041 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 8042 verifyFormat(StringRef("functionCall(\n" 8043 " paramA, paramB, paramC);\n" 8044 "void functionDecl(\n" 8045 " int A, int B, int C);"), 8046 Input, Style); 8047 } 8048 8049 TEST_F(FormatTest, BreakFunctionDefinitionParameters) { 8050 StringRef Input = "void functionDecl(paramA, paramB, paramC);\n" 8051 "void emptyFunctionDefinition() {}\n" 8052 "void functionDefinition(int A, int B, int C) {}\n" 8053 "Class::Class(int A, int B) : m_A(A), m_B(B) {}"; 8054 verifyFormat(Input); 8055 8056 FormatStyle Style = getLLVMStyle(); 8057 EXPECT_FALSE(Style.BreakFunctionDefinitionParameters); 8058 Style.BreakFunctionDefinitionParameters = true; 8059 verifyFormat("void functionDecl(paramA, paramB, paramC);\n" 8060 "void emptyFunctionDefinition() {}\n" 8061 "void functionDefinition(\n" 8062 " int A, int B, int C) {}\n" 8063 "Class::Class(\n" 8064 " int A, int B)\n" 8065 " : m_A(A), m_B(B) {}", 8066 Input, Style); 8067 8068 // Test the style where all parameters are on their own lines. 8069 Style.AllowAllParametersOfDeclarationOnNextLine = false; 8070 Style.BinPackParameters = FormatStyle::BPPS_OnePerLine; 8071 verifyFormat("void functionDecl(paramA, paramB, paramC);\n" 8072 "void emptyFunctionDefinition() {}\n" 8073 "void functionDefinition(\n" 8074 " int A,\n" 8075 " int B,\n" 8076 " int C) {}\n" 8077 "Class::Class(\n" 8078 " int A,\n" 8079 " int B)\n" 8080 " : m_A(A), m_B(B) {}", 8081 Input, Style); 8082 } 8083 8084 TEST_F(FormatTest, BreakBeforeInlineASMColon) { 8085 FormatStyle Style = getLLVMStyle(); 8086 Style.BreakBeforeInlineASMColon = FormatStyle::BBIAS_Never; 8087 /* Test the behaviour with long lines */ 8088 Style.ColumnLimit = 40; 8089 verifyFormat("asm volatile(\"loooooooooooooooooooong\",\n" 8090 " : : val);", 8091 Style); 8092 verifyFormat("asm volatile(\"loooooooooooooooooooong\",\n" 8093 " : val1 : val2);", 8094 Style); 8095 verifyFormat("asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" 8096 " \"cpuid\\n\\t\"\n" 8097 " \"xchgq\\t%%rbx %%rsi\\n\\t\",\n" 8098 " : \"=a\" : \"a\");", 8099 Style); 8100 Style.ColumnLimit = 80; 8101 verifyFormat("asm volatile(\"string\", : : val);", Style); 8102 verifyFormat("asm volatile(\"string\", : val1 : val2);", Style); 8103 8104 Style.BreakBeforeInlineASMColon = FormatStyle::BBIAS_Always; 8105 verifyFormat("asm volatile(\"string\",\n" 8106 " :\n" 8107 " : val);", 8108 Style); 8109 verifyFormat("asm volatile(\"string\",\n" 8110 " : val1\n" 8111 " : val2);", 8112 Style); 8113 /* Test the behaviour with long lines */ 8114 Style.ColumnLimit = 40; 8115 verifyFormat("asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" 8116 " \"cpuid\\n\\t\"\n" 8117 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n" 8118 " : \"=a\"(*rEAX)\n" 8119 " : \"a\"(value));", 8120 Style); 8121 verifyFormat("asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" 8122 " \"cpuid\\n\\t\"\n" 8123 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n" 8124 " :\n" 8125 " : \"a\"(value));", 8126 Style); 8127 verifyFormat("asm volatile(\"loooooooooooooooooooong\",\n" 8128 " :\n" 8129 " : val);", 8130 Style); 8131 verifyFormat("asm volatile(\"loooooooooooooooooooong\",\n" 8132 " : val1\n" 8133 " : val2);", 8134 Style); 8135 } 8136 8137 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) { 8138 FormatStyle Style = getLLVMStyle(); 8139 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 8140 8141 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 8142 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}", 8143 getStyleWithColumns(Style, 45)); 8144 verifyFormat("Constructor() :\n" 8145 " Initializer(FitsOnTheLine) {}", 8146 getStyleWithColumns(Style, 44)); 8147 verifyFormat("Constructor() :\n" 8148 " Initializer(FitsOnTheLine) {}", 8149 getStyleWithColumns(Style, 43)); 8150 8151 verifyFormat("template <typename T>\n" 8152 "Constructor() : Initializer(FitsOnTheLine) {}", 8153 getStyleWithColumns(Style, 50)); 8154 verifyFormat( 8155 "Class::Class(int some, int arguments, int loooooooooooooooooooong,\n" 8156 " int mooooooooooooore) noexcept :\n" 8157 " Super{some, arguments}, Member{5}, Member2{2} {}", 8158 Style); 8159 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 8160 verifyFormat( 8161 "SomeClass::Constructor() :\n" 8162 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 8163 Style); 8164 verifyFormat( 8165 "SomeClass::Constructor() : // NOLINT\n" 8166 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 8167 Style); 8168 8169 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly; 8170 verifyFormat( 8171 "SomeClass::Constructor() :\n" 8172 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 8173 Style); 8174 verifyFormat( 8175 "SomeClass::Constructor() : // NOLINT\n" 8176 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 8177 Style); 8178 8179 Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack; 8180 verifyFormat( 8181 "SomeClass::Constructor() :\n" 8182 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 8183 Style); 8184 8185 verifyFormat( 8186 "SomeClass::Constructor() :\n" 8187 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 8188 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 8189 Style); 8190 verifyFormat( 8191 "SomeClass::Constructor() :\n" 8192 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 8193 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 8194 Style); 8195 verifyFormat( 8196 "Ctor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8197 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) : aaaaaaaaaa(aaaaaa) {}", 8198 Style); 8199 8200 verifyFormat("Constructor() :\n" 8201 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 8202 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8203 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 8204 " aaaaaaaaaaaaaaaaaaaaaaa() {}", 8205 Style); 8206 8207 verifyFormat("Constructor() :\n" 8208 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8209 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 8210 Style); 8211 8212 verifyFormat("Constructor(int Parameter = 0) :\n" 8213 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 8214 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}", 8215 Style); 8216 verifyFormat("Constructor() :\n" 8217 " aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 8218 "}", 8219 getStyleWithColumns(Style, 60)); 8220 verifyFormat("Constructor() :\n" 8221 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8222 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}", 8223 Style); 8224 8225 // Here a line could be saved by splitting the second initializer onto two 8226 // lines, but that is not desirable. 8227 verifyFormat("Constructor() :\n" 8228 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 8229 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 8230 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 8231 Style); 8232 8233 FormatStyle OnePerLine = Style; 8234 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 8235 verifyFormat("SomeClass::Constructor() :\n" 8236 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 8237 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 8238 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 8239 OnePerLine); 8240 verifyFormat("SomeClass::Constructor() :\n" 8241 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 8242 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 8243 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 8244 OnePerLine); 8245 verifyFormat("Foo::Foo(int i, int j) : // NOLINT\n" 8246 " i(i), // comment\n" 8247 " j(j) {}", 8248 OnePerLine); 8249 verifyFormat("MyClass::MyClass(int var) :\n" 8250 " some_var_(var), // 4 space indent\n" 8251 " some_other_var_(var + 1) { // lined up\n" 8252 "}", 8253 OnePerLine); 8254 verifyFormat("Constructor() :\n" 8255 " aaaaa(aaaaaa),\n" 8256 " aaaaa(aaaaaa),\n" 8257 " aaaaa(aaaaaa),\n" 8258 " aaaaa(aaaaaa),\n" 8259 " aaaaa(aaaaaa) {}", 8260 OnePerLine); 8261 verifyFormat("Constructor() :\n" 8262 " aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 8263 " aaaaaaaaaaaaaaaaaaaaaa) {}", 8264 OnePerLine); 8265 OnePerLine.BinPackParameters = FormatStyle::BPPS_OnePerLine; 8266 verifyFormat("Constructor() :\n" 8267 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 8268 " aaaaaaaaaaa().aaa(),\n" 8269 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 8270 OnePerLine); 8271 OnePerLine.ColumnLimit = 60; 8272 verifyFormat("Constructor() :\n" 8273 " aaaaaaaaaaaaaaaaaaaa(a),\n" 8274 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 8275 OnePerLine); 8276 8277 verifyFormat("Constructor() :\n" 8278 " // Comment forcing unwanted break.\n" 8279 " aaaa(aaaa) {}", 8280 Style); 8281 verifyFormat("Constructor() : // NOLINT\n" 8282 " aaaa(aaaa) {}", 8283 Style); 8284 verifyFormat("Constructor() : // A very long trailing comment that cannot fit" 8285 " on a single\n" 8286 " // line.\n" 8287 " aaaa(aaaa) {}", 8288 "Constructor() : // A very long trailing comment that cannot fit" 8289 " on a single line.\n" 8290 " aaaa(aaaa) {}", 8291 Style); 8292 8293 Style.ColumnLimit = 0; 8294 verifyFormat("SomeClass::Constructor() :\n" 8295 " a(a) {}", 8296 Style); 8297 verifyFormat("SomeClass::Constructor() noexcept :\n" 8298 " a(a) {}", 8299 Style); 8300 verifyFormat("SomeClass::Constructor() :\n" 8301 " a(a), b(b), c(c) {}", 8302 Style); 8303 verifyFormat("SomeClass::Constructor() :\n" 8304 " a(a) {\n" 8305 " foo();\n" 8306 " bar();\n" 8307 "}", 8308 Style); 8309 8310 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 8311 verifyFormat("SomeClass::Constructor() :\n" 8312 " a(a), b(b), c(c) {\n" 8313 "}", 8314 Style); 8315 verifyFormat("SomeClass::Constructor() :\n" 8316 " a(a) {\n" 8317 "}", 8318 Style); 8319 8320 Style.ColumnLimit = 80; 8321 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 8322 Style.ConstructorInitializerIndentWidth = 2; 8323 verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", Style); 8324 verifyFormat("SomeClass::Constructor() :\n" 8325 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8326 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}", 8327 Style); 8328 8329 // `ConstructorInitializerIndentWidth` actually applies to InheritanceList as 8330 // well 8331 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 8332 verifyFormat( 8333 "class SomeClass\n" 8334 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8335 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 8336 Style); 8337 Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma; 8338 verifyFormat( 8339 "class SomeClass\n" 8340 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8341 " , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 8342 Style); 8343 Style.BreakInheritanceList = FormatStyle::BILS_AfterColon; 8344 verifyFormat( 8345 "class SomeClass :\n" 8346 " public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8347 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 8348 Style); 8349 Style.BreakInheritanceList = FormatStyle::BILS_AfterComma; 8350 verifyFormat( 8351 "class SomeClass\n" 8352 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8353 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 8354 Style); 8355 } 8356 8357 #ifndef EXPENSIVE_CHECKS 8358 // Expensive checks enables libstdc++ checking which includes validating the 8359 // state of ranges used in std::priority_queue - this blows out the 8360 // runtime/scalability of the function and makes this test unacceptably slow. 8361 TEST_F(FormatTest, MemoizationTests) { 8362 // This breaks if the memoization lookup does not take \c Indent and 8363 // \c LastSpace into account. 8364 verifyFormat( 8365 "extern CFRunLoopTimerRef\n" 8366 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n" 8367 " CFTimeInterval interval, CFOptionFlags flags,\n" 8368 " CFIndex order, CFRunLoopTimerCallBack callout,\n" 8369 " CFRunLoopTimerContext *context) {}"); 8370 8371 // Deep nesting somewhat works around our memoization. 8372 verifyFormat( 8373 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 8374 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 8375 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 8376 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 8377 " aaaaa())))))))))))))))))))))))))))))))))))))));", 8378 getLLVMStyleWithColumns(65)); 8379 verifyFormat( 8380 "aaaaa(\n" 8381 " aaaaa,\n" 8382 " aaaaa(\n" 8383 " aaaaa,\n" 8384 " aaaaa(\n" 8385 " aaaaa,\n" 8386 " aaaaa(\n" 8387 " aaaaa,\n" 8388 " aaaaa(\n" 8389 " aaaaa,\n" 8390 " aaaaa(\n" 8391 " aaaaa,\n" 8392 " aaaaa(\n" 8393 " aaaaa,\n" 8394 " aaaaa(\n" 8395 " aaaaa,\n" 8396 " aaaaa(\n" 8397 " aaaaa,\n" 8398 " aaaaa(\n" 8399 " aaaaa,\n" 8400 " aaaaa(\n" 8401 " aaaaa,\n" 8402 " aaaaa(\n" 8403 " aaaaa,\n" 8404 " aaaaa))))))))))));", 8405 getLLVMStyleWithColumns(65)); 8406 verifyFormat( 8407 "a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(), a), a), a), a),\n" 8408 " a),\n" 8409 " a),\n" 8410 " a),\n" 8411 " a),\n" 8412 " a),\n" 8413 " a),\n" 8414 " a),\n" 8415 " a),\n" 8416 " a),\n" 8417 " a),\n" 8418 " a),\n" 8419 " a),\n" 8420 " a),\n" 8421 " a),\n" 8422 " a),\n" 8423 " a),\n" 8424 " a)", 8425 getLLVMStyleWithColumns(65)); 8426 8427 // This test takes VERY long when memoization is broken. 8428 FormatStyle OnePerLine = getLLVMStyle(); 8429 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 8430 OnePerLine.BinPackParameters = FormatStyle::BPPS_OnePerLine; 8431 std::string input = "Constructor()\n" 8432 " : aaaa(a,\n"; 8433 for (unsigned i = 0, e = 80; i != e; ++i) 8434 input += " a,\n"; 8435 input += " a) {}"; 8436 verifyFormat(input, OnePerLine); 8437 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly; 8438 verifyFormat(input, OnePerLine); 8439 } 8440 #endif 8441 8442 TEST_F(FormatTest, BreaksAsHighAsPossible) { 8443 verifyFormat( 8444 "void f() {\n" 8445 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" 8446 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" 8447 " f();\n" 8448 "}"); 8449 verifyFormat("if (Intervals[i].getRange().getFirst() <\n" 8450 " Intervals[i - 1].getRange().getLast()) {\n}"); 8451 } 8452 8453 TEST_F(FormatTest, BreaksFunctionDeclarations) { 8454 // Principially, we break function declarations in a certain order: 8455 // 1) break amongst arguments. 8456 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n" 8457 " Cccccccccccccc cccccccccccccc);"); 8458 verifyFormat("template <class TemplateIt>\n" 8459 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n" 8460 " TemplateIt *stop) {}"); 8461 8462 // 2) break after return type. 8463 verifyGoogleFormat( 8464 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8465 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);"); 8466 8467 // 3) break after (. 8468 verifyGoogleFormat( 8469 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n" 8470 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);"); 8471 8472 // 4) break before after nested name specifiers. 8473 verifyGoogleFormat( 8474 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8475 "SomeClasssssssssssssssssssssssssssssssssssssss::\n" 8476 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);"); 8477 8478 // However, there are exceptions, if a sufficient amount of lines can be 8479 // saved. 8480 // FIXME: The precise cut-offs wrt. the number of saved lines might need some 8481 // more adjusting. 8482 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 8483 " Cccccccccccccc cccccccccc,\n" 8484 " Cccccccccccccc cccccccccc,\n" 8485 " Cccccccccccccc cccccccccc,\n" 8486 " Cccccccccccccc cccccccccc);"); 8487 verifyGoogleFormat( 8488 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8489 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 8490 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 8491 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); 8492 verifyFormat( 8493 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 8494 " Cccccccccccccc cccccccccc,\n" 8495 " Cccccccccccccc cccccccccc,\n" 8496 " Cccccccccccccc cccccccccc,\n" 8497 " Cccccccccccccc cccccccccc,\n" 8498 " Cccccccccccccc cccccccccc,\n" 8499 " Cccccccccccccc cccccccccc);"); 8500 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 8501 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 8502 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 8503 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 8504 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); 8505 8506 // Break after multi-line parameters. 8507 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8508 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8509 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8510 " bbbb bbbb);"); 8511 verifyFormat("void SomeLoooooooooooongFunction(\n" 8512 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 8513 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8514 " int bbbbbbbbbbbbb);"); 8515 8516 // Treat overloaded operators like other functions. 8517 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 8518 "operator>(const SomeLoooooooooooooooooooooooooogType &other);"); 8519 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 8520 "operator>>(const SomeLooooooooooooooooooooooooogType &other);"); 8521 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 8522 "operator<<(const SomeLooooooooooooooooooooooooogType &other);"); 8523 verifyGoogleFormat( 8524 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n" 8525 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 8526 verifyGoogleFormat( 8527 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n" 8528 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 8529 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8530 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 8531 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n" 8532 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 8533 verifyGoogleFormat( 8534 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n" 8535 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8536 " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}"); 8537 verifyGoogleFormat("template <typename T>\n" 8538 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8539 "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n" 8540 " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);"); 8541 8542 FormatStyle Style = getLLVMStyle(); 8543 Style.PointerAlignment = FormatStyle::PAS_Left; 8544 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8545 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}", 8546 Style); 8547 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n" 8548 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 8549 Style); 8550 } 8551 8552 TEST_F(FormatTest, DontBreakBeforeQualifiedOperator) { 8553 // Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516: 8554 // Prefer keeping `::` followed by `operator` together. 8555 verifyFormat("const aaaa::bbbbbbb &\n" 8556 "ccccccccc::operator++() {\n" 8557 " stuff();\n" 8558 "}", 8559 "const aaaa::bbbbbbb\n" 8560 "&ccccccccc::operator++() { stuff(); }", 8561 getLLVMStyleWithColumns(40)); 8562 } 8563 8564 TEST_F(FormatTest, TrailingReturnType) { 8565 verifyFormat("auto foo() -> int;"); 8566 // correct trailing return type spacing 8567 verifyFormat("auto operator->() -> int;"); 8568 verifyFormat("auto operator++(int) -> int;"); 8569 8570 verifyFormat("struct S {\n" 8571 " auto bar() const -> int;\n" 8572 "};"); 8573 verifyFormat("template <size_t Order, typename T>\n" 8574 "auto load_img(const std::string &filename)\n" 8575 " -> alias::tensor<Order, T, mem::tag::cpu> {}"); 8576 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n" 8577 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}"); 8578 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}"); 8579 verifyFormat("template <typename T>\n" 8580 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n" 8581 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());"); 8582 8583 FormatStyle Style = getLLVMStyleWithColumns(60); 8584 verifyFormat("#define MAKE_DEF(NAME) \\\n" 8585 " auto NAME() -> int { return 42; }", 8586 Style); 8587 8588 // Not trailing return types. 8589 verifyFormat("void f() { auto a = b->c(); }"); 8590 verifyFormat("auto a = p->foo();"); 8591 verifyFormat("int a = p->foo();"); 8592 verifyFormat("auto lmbd = [] NOEXCEPT -> int { return 0; };"); 8593 } 8594 8595 TEST_F(FormatTest, DeductionGuides) { 8596 verifyFormat("template <class T> A(const T &, const T &) -> A<T &>;"); 8597 verifyFormat("template <class T> explicit A(T &, T &&) -> A<T>;"); 8598 verifyFormat("template <class... Ts> S(Ts...) -> S<Ts...>;"); 8599 verifyFormat( 8600 "template <class... T>\n" 8601 "array(T &&...t) -> array<std::common_type_t<T...>, sizeof...(T)>;"); 8602 verifyFormat("template <class T> A() -> A<decltype(p->foo<3>())>;"); 8603 verifyFormat("template <class T> A() -> A<decltype(foo<traits<1>>)>;"); 8604 verifyFormat("template <class T> A() -> A<sizeof(p->foo<1>)>;"); 8605 verifyFormat("template <class T> A() -> A<(3 < 2)>;"); 8606 verifyFormat("template <class T> A() -> A<((3) < (2))>;"); 8607 verifyFormat("template <class T> x() -> x<1>;"); 8608 verifyFormat("template <class T> explicit x(T &) -> x<1>;"); 8609 8610 verifyFormat("A(const char *) -> A<string &>;"); 8611 verifyFormat("A() -> A<int>;"); 8612 8613 // Ensure not deduction guides. 8614 verifyFormat("c()->f<int>();"); 8615 verifyFormat("x()->foo<1>;"); 8616 verifyFormat("x = p->foo<3>();"); 8617 verifyFormat("x()->x<1>();"); 8618 } 8619 8620 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { 8621 // Avoid breaking before trailing 'const' or other trailing annotations, if 8622 // they are not function-like. 8623 FormatStyle Style = getGoogleStyleWithColumns(47); 8624 verifyFormat("void someLongFunction(\n" 8625 " int someLoooooooooooooongParameter) const {\n}", 8626 getLLVMStyleWithColumns(47)); 8627 verifyFormat("LoooooongReturnType\n" 8628 "someLoooooooongFunction() const {}", 8629 getLLVMStyleWithColumns(47)); 8630 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n" 8631 " const {}", 8632 Style); 8633 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 8634 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;"); 8635 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 8636 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;"); 8637 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 8638 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;"); 8639 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n" 8640 " aaaaaaaaaaa aaaaa) const override;"); 8641 verifyGoogleFormat( 8642 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 8643 " const override;"); 8644 8645 // Even if the first parameter has to be wrapped. 8646 verifyFormat("void someLongFunction(\n" 8647 " int someLongParameter) const {}", 8648 getLLVMStyleWithColumns(46)); 8649 verifyFormat("void someLongFunction(\n" 8650 " int someLongParameter) const {}", 8651 Style); 8652 verifyFormat("void someLongFunction(\n" 8653 " int someLongParameter) override {}", 8654 Style); 8655 verifyFormat("void someLongFunction(\n" 8656 " int someLongParameter) OVERRIDE {}", 8657 Style); 8658 verifyFormat("void someLongFunction(\n" 8659 " int someLongParameter) final {}", 8660 Style); 8661 verifyFormat("void someLongFunction(\n" 8662 " int someLongParameter) FINAL {}", 8663 Style); 8664 verifyFormat("void someLongFunction(\n" 8665 " int parameter) const override {}", 8666 Style); 8667 8668 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 8669 verifyFormat("void someLongFunction(\n" 8670 " int someLongParameter) const\n" 8671 "{\n" 8672 "}", 8673 Style); 8674 8675 Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; 8676 verifyFormat("void someLongFunction(\n" 8677 " int someLongParameter) const\n" 8678 " {\n" 8679 " }", 8680 Style); 8681 8682 // Unless these are unknown annotations. 8683 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n" 8684 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8685 " LONG_AND_UGLY_ANNOTATION;"); 8686 8687 // Breaking before function-like trailing annotations is fine to keep them 8688 // close to their arguments. 8689 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8690 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 8691 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 8692 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 8693 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 8694 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}"); 8695 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n" 8696 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);"); 8697 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});"); 8698 8699 verifyFormat( 8700 "void aaaaaaaaaaaaaaaaaa()\n" 8701 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n" 8702 " aaaaaaaaaaaaaaaaaaaaaaaaa));"); 8703 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8704 " __attribute__((unused));"); 8705 8706 Style = getGoogleStyle(); 8707 8708 verifyFormat( 8709 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8710 " GUARDED_BY(aaaaaaaaaaaa);", 8711 Style); 8712 verifyFormat( 8713 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8714 " GUARDED_BY(aaaaaaaaaaaa);", 8715 Style); 8716 verifyFormat( 8717 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 8718 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 8719 Style); 8720 verifyFormat( 8721 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 8722 " aaaaaaaaaaaaaaaaaaaaaaaaa;", 8723 Style); 8724 8725 verifyFormat( 8726 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8727 " ABSL_GUARDED_BY(aaaaaaaaaaaa);", 8728 Style); 8729 verifyFormat( 8730 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8731 " ABSL_GUARDED_BY(aaaaaaaaaaaa);", 8732 Style); 8733 verifyFormat( 8734 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ABSL_GUARDED_BY(aaaaaaaaaaaa) =\n" 8735 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 8736 Style); 8737 verifyFormat( 8738 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ABSL_GUARDED_BY(aaaaaaaaaaaa) =\n" 8739 " aaaaaaaaaaaaaaaaaaaaaaaaa;", 8740 Style); 8741 } 8742 8743 TEST_F(FormatTest, FunctionAnnotations) { 8744 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 8745 "int OldFunction(const string ¶meter) {}"); 8746 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 8747 "string OldFunction(const string ¶meter) {}"); 8748 verifyFormat("template <typename T>\n" 8749 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 8750 "string OldFunction(const string ¶meter) {}"); 8751 8752 // Not function annotations. 8753 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8754 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); 8755 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n" 8756 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}"); 8757 verifyFormat("MACRO(abc).function() // wrap\n" 8758 " << abc;"); 8759 verifyFormat("MACRO(abc)->function() // wrap\n" 8760 " << abc;"); 8761 verifyFormat("MACRO(abc)::function() // wrap\n" 8762 " << abc;"); 8763 verifyFormat("FOO(bar)();", getLLVMStyleWithColumns(0)); 8764 } 8765 8766 TEST_F(FormatTest, BreaksDesireably) { 8767 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 8768 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 8769 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); 8770 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8771 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 8772 "}"); 8773 8774 verifyFormat( 8775 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8776 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 8777 8778 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8779 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8780 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 8781 8782 verifyFormat( 8783 "aaaaaaaa(aaaaaaaaaaaaa,\n" 8784 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8785 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 8786 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8787 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 8788 8789 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 8790 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8791 8792 verifyFormat( 8793 "void f() {\n" 8794 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 8795 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 8796 "}"); 8797 verifyFormat( 8798 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8799 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 8800 verifyFormat( 8801 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8802 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 8803 verifyFormat( 8804 "aaaaaa(aaa,\n" 8805 " new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8806 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 8807 " aaaa);"); 8808 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 8809 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8810 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8811 8812 // Indent consistently independent of call expression and unary operator. 8813 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 8814 " dddddddddddddddddddddddddddddd));"); 8815 verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 8816 " dddddddddddddddddddddddddddddd));"); 8817 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n" 8818 " dddddddddddddddddddddddddddddd));"); 8819 8820 // This test case breaks on an incorrect memoization, i.e. an optimization not 8821 // taking into account the StopAt value. 8822 verifyFormat( 8823 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 8824 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 8825 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 8826 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8827 8828 verifyFormat("{\n {\n {\n" 8829 " Annotation.SpaceRequiredBefore =\n" 8830 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" 8831 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" 8832 " }\n }\n}"); 8833 8834 // Break on an outer level if there was a break on an inner level. 8835 verifyFormat("f(g(h(a, // comment\n" 8836 " b, c),\n" 8837 " d, e),\n" 8838 " x, y);", 8839 "f(g(h(a, // comment\n" 8840 " b, c), d, e), x, y);"); 8841 8842 // Prefer breaking similar line breaks. 8843 verifyFormat( 8844 "const int kTrackingOptions = NSTrackingMouseMoved |\n" 8845 " NSTrackingMouseEnteredAndExited |\n" 8846 " NSTrackingActiveAlways;"); 8847 } 8848 8849 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) { 8850 FormatStyle NoBinPacking = getGoogleStyle(); 8851 NoBinPacking.BinPackParameters = FormatStyle::BPPS_OnePerLine; 8852 NoBinPacking.BinPackArguments = true; 8853 verifyFormat("void f() {\n" 8854 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n" 8855 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 8856 "}", 8857 NoBinPacking); 8858 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n" 8859 " int aaaaaaaaaaaaaaaaaaaa,\n" 8860 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 8861 NoBinPacking); 8862 8863 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 8864 verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8865 " vector<int> bbbbbbbbbbbbbbb);", 8866 NoBinPacking); 8867 // FIXME: This behavior difference is probably not wanted. However, currently 8868 // we cannot distinguish BreakBeforeParameter being set because of the wrapped 8869 // template arguments from BreakBeforeParameter being set because of the 8870 // one-per-line formatting. 8871 verifyFormat( 8872 "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n" 8873 " aaaaaaaaaa> aaaaaaaaaa);", 8874 NoBinPacking); 8875 verifyFormat( 8876 "void fffffffffff(\n" 8877 " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n" 8878 " aaaaaaaaaa);"); 8879 } 8880 8881 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { 8882 FormatStyle NoBinPacking = getGoogleStyle(); 8883 NoBinPacking.BinPackParameters = FormatStyle::BPPS_OnePerLine; 8884 NoBinPacking.BinPackArguments = false; 8885 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" 8886 " aaaaaaaaaaaaaaaaaaaa,\n" 8887 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);", 8888 NoBinPacking); 8889 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n" 8890 " aaaaaaaaaaaaa,\n" 8891 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));", 8892 NoBinPacking); 8893 verifyFormat( 8894 "aaaaaaaa(aaaaaaaaaaaaa,\n" 8895 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8896 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 8897 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8898 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));", 8899 NoBinPacking); 8900 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 8901 " .aaaaaaaaaaaaaaaaaa();", 8902 NoBinPacking); 8903 verifyFormat("void f() {\n" 8904 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8905 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n" 8906 "}", 8907 NoBinPacking); 8908 8909 verifyFormat( 8910 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8911 " aaaaaaaaaaaa,\n" 8912 " aaaaaaaaaaaa);", 8913 NoBinPacking); 8914 verifyFormat( 8915 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" 8916 " ddddddddddddddddddddddddddddd),\n" 8917 " test);", 8918 NoBinPacking); 8919 8920 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" 8921 " aaaaaaaaaaaaaaaaaaaaaaa,\n" 8922 " aaaaaaaaaaaaaaaaaaaaaaa>\n" 8923 " aaaaaaaaaaaaaaaaaa;", 8924 NoBinPacking); 8925 verifyFormat("a(\"a\"\n" 8926 " \"a\",\n" 8927 " a);"); 8928 8929 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 8930 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" 8931 " aaaaaaaaa,\n" 8932 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 8933 NoBinPacking); 8934 verifyFormat( 8935 "void f() {\n" 8936 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 8937 " .aaaaaaa();\n" 8938 "}", 8939 NoBinPacking); 8940 verifyFormat( 8941 "template <class SomeType, class SomeOtherType>\n" 8942 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}", 8943 NoBinPacking); 8944 } 8945 8946 TEST_F(FormatTest, FormatsDeclarationBreakAlways) { 8947 FormatStyle BreakAlways = getGoogleStyle(); 8948 BreakAlways.BinPackParameters = FormatStyle::BPPS_AlwaysOnePerLine; 8949 verifyFormat("void f(int a,\n" 8950 " int b);", 8951 BreakAlways); 8952 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8953 " int bbbbbbbbbbbbbbbbbbbbbbbbb,\n" 8954 " int cccccccccccccccccccccccc);", 8955 BreakAlways); 8956 8957 // Ensure AlignAfterOpenBracket interacts correctly with BinPackParameters set 8958 // to BPPS_AlwaysOnePerLine. 8959 BreakAlways.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 8960 verifyFormat( 8961 "void someLongFunctionName(\n" 8962 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8963 " int b);", 8964 BreakAlways); 8965 BreakAlways.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; 8966 verifyFormat( 8967 "void someLongFunctionName(\n" 8968 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8969 " int b\n" 8970 ");", 8971 BreakAlways); 8972 } 8973 8974 TEST_F(FormatTest, FormatsDefinitionBreakAlways) { 8975 FormatStyle BreakAlways = getGoogleStyle(); 8976 BreakAlways.BinPackParameters = FormatStyle::BPPS_AlwaysOnePerLine; 8977 verifyFormat("void f(int a,\n" 8978 " int b) {\n" 8979 " f(a, b);\n" 8980 "}", 8981 BreakAlways); 8982 8983 // Ensure BinPackArguments interact correctly when BinPackParameters is set to 8984 // BPPS_AlwaysOnePerLine. 8985 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8986 " int bbbbbbbbbbbbbbbbbbbbbbbbb,\n" 8987 " int cccccccccccccccccccccccc) {\n" 8988 " f(aaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbb,\n" 8989 " cccccccccccccccccccccccc);\n" 8990 "}", 8991 BreakAlways); 8992 BreakAlways.BinPackArguments = false; 8993 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8994 " int bbbbbbbbbbbbbbbbbbbbbbbbb,\n" 8995 " int cccccccccccccccccccccccc) {\n" 8996 " f(aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8997 " bbbbbbbbbbbbbbbbbbbbbbbbb,\n" 8998 " cccccccccccccccccccccccc);\n" 8999 "}", 9000 BreakAlways); 9001 9002 // Ensure BreakFunctionDefinitionParameters interacts correctly when 9003 // BinPackParameters is set to BPPS_AlwaysOnePerLine. 9004 BreakAlways.BreakFunctionDefinitionParameters = true; 9005 verifyFormat("void f(\n" 9006 " int a,\n" 9007 " int b) {\n" 9008 " f(a, b);\n" 9009 "}", 9010 BreakAlways); 9011 BreakAlways.BreakFunctionDefinitionParameters = false; 9012 9013 // Ensure AlignAfterOpenBracket interacts correctly with BinPackParameters set 9014 // to BPPS_AlwaysOnePerLine. 9015 BreakAlways.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 9016 verifyFormat( 9017 "void someLongFunctionName(\n" 9018 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9019 " int b) {\n" 9020 " someLongFunctionName(\n" 9021 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, b);\n" 9022 "}", 9023 BreakAlways); 9024 BreakAlways.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; 9025 verifyFormat( 9026 "void someLongFunctionName(\n" 9027 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9028 " int b\n" 9029 ") {\n" 9030 " someLongFunctionName(\n" 9031 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, b\n" 9032 " );\n" 9033 "}", 9034 BreakAlways); 9035 } 9036 9037 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) { 9038 FormatStyle Style = getLLVMStyleWithColumns(15); 9039 Style.ExperimentalAutoDetectBinPacking = true; 9040 verifyFormat("aaa(aaaa,\n" 9041 " aaaa,\n" 9042 " aaaa);\n" 9043 "aaa(aaaa,\n" 9044 " aaaa,\n" 9045 " aaaa);", 9046 "aaa(aaaa,\n" // one-per-line 9047 " aaaa,\n" 9048 " aaaa );\n" 9049 "aaa(aaaa, aaaa, aaaa);", // inconclusive 9050 Style); 9051 verifyFormat("aaa(aaaa, aaaa,\n" 9052 " aaaa);\n" 9053 "aaa(aaaa, aaaa,\n" 9054 " aaaa);", 9055 "aaa(aaaa, aaaa,\n" // bin-packed 9056 " aaaa );\n" 9057 "aaa(aaaa, aaaa, aaaa);", // inconclusive 9058 Style); 9059 } 9060 9061 TEST_F(FormatTest, FormatsBuilderPattern) { 9062 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n" 9063 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" 9064 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n" 9065 " .StartsWith(\".init\", ORDER_INIT)\n" 9066 " .StartsWith(\".fini\", ORDER_FINI)\n" 9067 " .StartsWith(\".hash\", ORDER_HASH)\n" 9068 " .Default(ORDER_TEXT);"); 9069 9070 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" 9071 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); 9072 verifyFormat("aaaaaaa->aaaaaaa\n" 9073 " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9074 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 9075 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 9076 verifyFormat( 9077 "aaaaaaa->aaaaaaa\n" 9078 " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 9079 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 9080 verifyFormat( 9081 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" 9082 " aaaaaaaaaaaaaa);"); 9083 verifyFormat( 9084 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n" 9085 " aaaaaa->aaaaaaaaaaaa()\n" 9086 " ->aaaaaaaaaaaaaaaa(\n" 9087 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 9088 " ->aaaaaaaaaaaaaaaaa();"); 9089 verifyGoogleFormat( 9090 "void f() {\n" 9091 " someo->Add((new util::filetools::Handler(dir))\n" 9092 " ->OnEvent1(NewPermanentCallback(\n" 9093 " this, &HandlerHolderClass::EventHandlerCBA))\n" 9094 " ->OnEvent2(NewPermanentCallback(\n" 9095 " this, &HandlerHolderClass::EventHandlerCBB))\n" 9096 " ->OnEvent3(NewPermanentCallback(\n" 9097 " this, &HandlerHolderClass::EventHandlerCBC))\n" 9098 " ->OnEvent5(NewPermanentCallback(\n" 9099 " this, &HandlerHolderClass::EventHandlerCBD))\n" 9100 " ->OnEvent6(NewPermanentCallback(\n" 9101 " this, &HandlerHolderClass::EventHandlerCBE)));\n" 9102 "}"); 9103 9104 verifyFormat( 9105 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();"); 9106 verifyFormat("aaaaaaaaaaaaaaa()\n" 9107 " .aaaaaaaaaaaaaaa()\n" 9108 " .aaaaaaaaaaaaaaa()\n" 9109 " .aaaaaaaaaaaaaaa()\n" 9110 " .aaaaaaaaaaaaaaa();"); 9111 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 9112 " .aaaaaaaaaaaaaaa()\n" 9113 " .aaaaaaaaaaaaaaa()\n" 9114 " .aaaaaaaaaaaaaaa();"); 9115 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 9116 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 9117 " .aaaaaaaaaaaaaaa();"); 9118 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n" 9119 " ->aaaaaaaaaaaaaae(0)\n" 9120 " ->aaaaaaaaaaaaaaa();"); 9121 9122 // Don't linewrap after very short segments. 9123 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 9124 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 9125 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 9126 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 9127 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 9128 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 9129 verifyFormat("aaa()\n" 9130 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 9131 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 9132 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 9133 9134 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 9135 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 9136 " .has<bbbbbbbbbbbbbbbbbbbbb>();"); 9137 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 9138 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 9139 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();"); 9140 9141 // Prefer not to break after empty parentheses. 9142 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n" 9143 " First->LastNewlineOffset);"); 9144 9145 // Prefer not to create "hanging" indents. 9146 verifyFormat( 9147 "return !soooooooooooooome_map\n" 9148 " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 9149 " .second;"); 9150 verifyFormat( 9151 "return aaaaaaaaaaaaaaaa\n" 9152 " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n" 9153 " .aaaa(aaaaaaaaaaaaaa);"); 9154 // No hanging indent here. 9155 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n" 9156 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9157 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n" 9158 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9159 verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 9160 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 9161 getLLVMStyleWithColumns(60)); 9162 verifyFormat("aaaaaaaaaaaaaaaaaa\n" 9163 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 9164 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 9165 getLLVMStyleWithColumns(59)); 9166 verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9167 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 9168 " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9169 9170 // Dont break if only closing statements before member call 9171 verifyFormat("test() {\n" 9172 " ([]() -> {\n" 9173 " int b = 32;\n" 9174 " return 3;\n" 9175 " }).foo();\n" 9176 "}"); 9177 verifyFormat("test() {\n" 9178 " (\n" 9179 " []() -> {\n" 9180 " int b = 32;\n" 9181 " return 3;\n" 9182 " },\n" 9183 " foo, bar)\n" 9184 " .foo();\n" 9185 "}"); 9186 verifyFormat("test() {\n" 9187 " ([]() -> {\n" 9188 " int b = 32;\n" 9189 " return 3;\n" 9190 " })\n" 9191 " .foo()\n" 9192 " .bar();\n" 9193 "}"); 9194 verifyFormat("test() {\n" 9195 " ([]() -> {\n" 9196 " int b = 32;\n" 9197 " return 3;\n" 9198 " })\n" 9199 " .foo(\"aaaaaaaaaaaaaaaaa\"\n" 9200 " \"bbbb\");\n" 9201 "}", 9202 getLLVMStyleWithColumns(30)); 9203 } 9204 9205 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { 9206 verifyFormat( 9207 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 9208 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); 9209 verifyFormat( 9210 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n" 9211 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}"); 9212 9213 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 9214 " ccccccccccccccccccccccccc) {\n}"); 9215 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n" 9216 " ccccccccccccccccccccccccc) {\n}"); 9217 9218 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 9219 " ccccccccccccccccccccccccc) {\n}"); 9220 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n" 9221 " ccccccccccccccccccccccccc) {\n}"); 9222 9223 verifyFormat( 9224 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" 9225 " ccccccccccccccccccccccccc) {\n}"); 9226 verifyFormat( 9227 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n" 9228 " ccccccccccccccccccccccccc) {\n}"); 9229 9230 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" 9231 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" 9232 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" 9233 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 9234 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n" 9235 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n" 9236 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n" 9237 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 9238 9239 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" 9240 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" 9241 " aaaaaaaaaaaaaaa != aa) {\n}"); 9242 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n" 9243 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n" 9244 " aaaaaaaaaaaaaaa != aa) {\n}"); 9245 } 9246 9247 TEST_F(FormatTest, BreaksAfterAssignments) { 9248 verifyFormat( 9249 "unsigned Cost =\n" 9250 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" 9251 " SI->getPointerAddressSpaceee());"); 9252 verifyFormat( 9253 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" 9254 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); 9255 9256 verifyFormat( 9257 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n" 9258 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); 9259 verifyFormat("unsigned OriginalStartColumn =\n" 9260 " SourceMgr.getSpellingColumnNumber(\n" 9261 " Current.FormatTok.getStartOfNonWhitespace()) -\n" 9262 " 1;"); 9263 } 9264 9265 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) { 9266 FormatStyle Style = getLLVMStyle(); 9267 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 9268 " bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;", 9269 Style); 9270 9271 Style.PenaltyBreakAssignment = 20; 9272 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 9273 " cccccccccccccccccccccccccc;", 9274 Style); 9275 } 9276 9277 TEST_F(FormatTest, AlignsAfterAssignments) { 9278 verifyFormat( 9279 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 9280 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 9281 verifyFormat( 9282 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 9283 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 9284 verifyFormat( 9285 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 9286 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 9287 verifyFormat( 9288 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 9289 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 9290 verifyFormat( 9291 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" 9292 " aaaaaaaaaaaaaaaaaaaaaaaa +\n" 9293 " aaaaaaaaaaaaaaaaaaaaaaaa;"); 9294 } 9295 9296 TEST_F(FormatTest, AlignsAfterReturn) { 9297 verifyFormat( 9298 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 9299 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 9300 verifyFormat( 9301 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 9302 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 9303 verifyFormat( 9304 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 9305 " aaaaaaaaaaaaaaaaaaaaaa();"); 9306 verifyFormat( 9307 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 9308 " aaaaaaaaaaaaaaaaaaaaaa());"); 9309 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9310 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9311 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9312 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n" 9313 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 9314 verifyFormat("return\n" 9315 " // true if code is one of a or b.\n" 9316 " code == a || code == b;"); 9317 } 9318 9319 TEST_F(FormatTest, AlignsAfterOpenBracket) { 9320 verifyFormat( 9321 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 9322 " aaaaaaaaa aaaaaaa) {}"); 9323 verifyFormat( 9324 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 9325 " aaaaaaaaaaa aaaaaaaaa);"); 9326 verifyFormat( 9327 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 9328 " aaaaaaaaaaaaaaaaaaaaa));"); 9329 FormatStyle Style = getLLVMStyle(); 9330 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 9331 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9332 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}", 9333 Style); 9334 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 9335 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);", 9336 Style); 9337 verifyFormat("SomeLongVariableName->someFunction(\n" 9338 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));", 9339 Style); 9340 verifyFormat( 9341 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 9342 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 9343 Style); 9344 verifyFormat( 9345 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 9346 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 9347 Style); 9348 verifyFormat( 9349 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 9350 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 9351 Style); 9352 9353 verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n" 9354 " ccccccc(aaaaaaaaaaaaaaaaa, //\n" 9355 " b));", 9356 Style); 9357 9358 Style.ColumnLimit = 30; 9359 verifyFormat("for (int foo = 0; foo < FOO;\n" 9360 " ++foo) {\n" 9361 " bar(foo);\n" 9362 "}", 9363 Style); 9364 Style.ColumnLimit = 80; 9365 9366 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 9367 Style.BinPackArguments = false; 9368 Style.BinPackParameters = FormatStyle::BPPS_OnePerLine; 9369 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9370 " aaaaaaaaaaa aaaaaaaa,\n" 9371 " aaaaaaaaa aaaaaaa,\n" 9372 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 9373 Style); 9374 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 9375 " aaaaaaaaaaa aaaaaaaaa,\n" 9376 " aaaaaaaaaaa aaaaaaaaa,\n" 9377 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 9378 Style); 9379 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n" 9380 " aaaaaaaaaaaaaaa,\n" 9381 " aaaaaaaaaaaaaaaaaaaaa,\n" 9382 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 9383 Style); 9384 verifyFormat( 9385 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n" 9386 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 9387 Style); 9388 verifyFormat( 9389 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n" 9390 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 9391 Style); 9392 verifyFormat( 9393 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 9394 " aaaaaaaaaaaaaaaaaaaaa(\n" 9395 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n" 9396 " aaaaaaaaaaaaaaaa);", 9397 Style); 9398 verifyFormat( 9399 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 9400 " aaaaaaaaaaaaaaaaaaaaa(\n" 9401 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n" 9402 " aaaaaaaaaaaaaaaa);", 9403 Style); 9404 verifyFormat( 9405 "fooooooooooo(new BARRRRRRRRR(\n" 9406 " XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXZZZZZZZZZZZZZZZZZZZZZZZZZ()));", 9407 Style); 9408 verifyFormat( 9409 "fooooooooooo(::new BARRRRRRRRR(\n" 9410 " XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXZZZZZZZZZZZZZZZZZZZZZZZZZ()));", 9411 Style); 9412 verifyFormat( 9413 "fooooooooooo(new FOO::BARRRR(\n" 9414 " XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXZZZZZZZZZZZZZZZZZZZZZZZZZ()));", 9415 Style); 9416 9417 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; 9418 Style.BinPackArguments = false; 9419 Style.BinPackParameters = FormatStyle::BPPS_OnePerLine; 9420 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9421 " aaaaaaaaaaa aaaaaaaa,\n" 9422 " aaaaaaaaa aaaaaaa,\n" 9423 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9424 ") {}", 9425 Style); 9426 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 9427 " aaaaaaaaaaa aaaaaaaaa,\n" 9428 " aaaaaaaaaaa aaaaaaaaa,\n" 9429 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9430 ");", 9431 Style); 9432 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n" 9433 " aaaaaaaaaaaaaaa,\n" 9434 " aaaaaaaaaaaaaaaaaaaaa,\n" 9435 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9436 "));", 9437 Style); 9438 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n" 9439 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n" 9440 "));", 9441 Style); 9442 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n" 9443 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n" 9444 "));", 9445 Style); 9446 verifyFormat( 9447 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 9448 " aaaaaaaaaaaaaaaaaaaaa(\n" 9449 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n" 9450 " ),\n" 9451 " aaaaaaaaaaaaaaaa\n" 9452 ");", 9453 Style); 9454 verifyFormat( 9455 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 9456 " aaaaaaaaaaaaaaaaaaaaa(\n" 9457 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n" 9458 " ) &&\n" 9459 " aaaaaaaaaaaaaaaa\n" 9460 ");", 9461 Style); 9462 verifyFormat("void foo(\n" 9463 " void (*foobarpntr)(\n" 9464 " aaaaaaaaaaaaaaaaaa *,\n" 9465 " bbbbbbbbbbbbbb *,\n" 9466 " cccccccccccccccccccc *,\n" 9467 " dddddddddddddddddd *\n" 9468 " )\n" 9469 ");", 9470 Style); 9471 verifyFormat("aaaaaaa<bbbbbbbb> const aaaaaaaaaa{\n" 9472 " aaaaaaaaaaaaa(aaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n" 9473 "};", 9474 Style); 9475 9476 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9477 " const bool &aaaaaaaaa, const void *aaaaaaaaaa\n" 9478 ") const {\n" 9479 " return true;\n" 9480 "}", 9481 Style); 9482 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaa(\n" 9483 " const bool &aaaaaaaaaa, const void *aaaaaaaaaa\n" 9484 ") const;", 9485 Style); 9486 verifyFormat("void aaaaaaaaa(\n" 9487 " int aaaaaa, int bbbbbb, int cccccc, int dddddddddd\n" 9488 ") const noexcept -> std::vector<of_very_long_type>;", 9489 Style); 9490 verifyFormat( 9491 "x = aaaaaaaaaaaaaaa(\n" 9492 " \"a aaaaaaa aaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaa\"\n" 9493 ");", 9494 Style); 9495 Style.ColumnLimit = 60; 9496 verifyFormat("auto lambda =\n" 9497 " [&b](\n" 9498 " auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9499 " ) {};", 9500 Style); 9501 } 9502 9503 TEST_F(FormatTest, ParenthesesAndOperandAlignment) { 9504 FormatStyle Style = getLLVMStyleWithColumns(40); 9505 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 9506 " bbbbbbbbbbbbbbbbbbbbbb);", 9507 Style); 9508 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 9509 Style.AlignOperands = FormatStyle::OAS_DontAlign; 9510 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 9511 " bbbbbbbbbbbbbbbbbbbbbb);", 9512 Style); 9513 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 9514 Style.AlignOperands = FormatStyle::OAS_Align; 9515 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 9516 " bbbbbbbbbbbbbbbbbbbbbb);", 9517 Style); 9518 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 9519 Style.AlignOperands = FormatStyle::OAS_DontAlign; 9520 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 9521 " bbbbbbbbbbbbbbbbbbbbbb);", 9522 Style); 9523 } 9524 9525 TEST_F(FormatTest, BreaksConditionalExpressions) { 9526 verifyFormat( 9527 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9528 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9529 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9530 verifyFormat( 9531 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 9532 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9533 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9534 verifyFormat( 9535 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9536 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9537 verifyFormat("aaaa(aaaaaaaaa, aaaaaaaaa,\n" 9538 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9539 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9540 verifyFormat( 9541 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" 9542 " : aaaaaaaaaaaaa);"); 9543 verifyFormat( 9544 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9545 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9546 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9547 " aaaaaaaaaaaaa);"); 9548 verifyFormat( 9549 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9550 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9551 " aaaaaaaaaaaaa);"); 9552 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9553 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9554 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 9555 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9556 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9557 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9558 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9559 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9560 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 9561 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9562 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 9563 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9564 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9565 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9566 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9567 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 9568 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9569 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9570 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9571 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 9572 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 9573 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9574 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9575 " : aaaaaaaaaaaaaaaa;"); 9576 verifyFormat( 9577 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9578 " ? aaaaaaaaaaaaaaa\n" 9579 " : aaaaaaaaaaaaaaa;"); 9580 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 9581 " aaaaaaaaa\n" 9582 " ? b\n" 9583 " : c);"); 9584 verifyFormat("return aaaa == bbbb\n" 9585 " // comment\n" 9586 " ? aaaa\n" 9587 " : bbbb;"); 9588 verifyFormat("unsigned Indent =\n" 9589 " format(TheLine.First,\n" 9590 " IndentForLevel[TheLine.Level] >= 0\n" 9591 " ? IndentForLevel[TheLine.Level]\n" 9592 " : TheLine * 2,\n" 9593 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 9594 getLLVMStyleWithColumns(60)); 9595 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 9596 " ? aaaaaaaaaaaaaaa\n" 9597 " : bbbbbbbbbbbbbbb //\n" 9598 " ? ccccccccccccccc\n" 9599 " : ddddddddddddddd;"); 9600 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 9601 " ? aaaaaaaaaaaaaaa\n" 9602 " : (bbbbbbbbbbbbbbb //\n" 9603 " ? ccccccccccccccc\n" 9604 " : ddddddddddddddd);"); 9605 verifyFormat( 9606 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9607 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 9608 " aaaaaaaaaaaaaaaaaaaaa +\n" 9609 " aaaaaaaaaaaaaaaaaaaaa\n" 9610 " : aaaaaaaaaa;"); 9611 verifyFormat( 9612 "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9613 " : aaaaaaaaaaaaaaaaaaaaaa\n" 9614 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 9615 9616 FormatStyle NoBinPacking = getLLVMStyle(); 9617 NoBinPacking.BinPackArguments = false; 9618 verifyFormat( 9619 "void f() {\n" 9620 " g(aaa,\n" 9621 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 9622 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9623 " ? aaaaaaaaaaaaaaa\n" 9624 " : aaaaaaaaaaaaaaa);\n" 9625 "}", 9626 NoBinPacking); 9627 verifyFormat( 9628 "void f() {\n" 9629 " g(aaa,\n" 9630 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 9631 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9632 " ?: aaaaaaaaaaaaaaa);\n" 9633 "}", 9634 NoBinPacking); 9635 9636 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n" 9637 " // comment.\n" 9638 " ccccccccccccccccccccccccccccccccccccccc\n" 9639 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9640 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);"); 9641 9642 // Assignments in conditional expressions. Apparently not uncommon :-(. 9643 verifyFormat("return a != b\n" 9644 " // comment\n" 9645 " ? a = b\n" 9646 " : a = b;"); 9647 verifyFormat("return a != b\n" 9648 " // comment\n" 9649 " ? a = a != b\n" 9650 " // comment\n" 9651 " ? a = b\n" 9652 " : a\n" 9653 " : a;"); 9654 verifyFormat("return a != b\n" 9655 " // comment\n" 9656 " ? a\n" 9657 " : a = a != b\n" 9658 " // comment\n" 9659 " ? a = b\n" 9660 " : a;"); 9661 9662 // Chained conditionals 9663 FormatStyle Style = getLLVMStyleWithColumns(70); 9664 Style.AlignOperands = FormatStyle::OAS_Align; 9665 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 9666 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 9667 " : 3333333333333333;", 9668 Style); 9669 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 9670 " : bbbbbbbbbb ? 2222222222222222\n" 9671 " : 3333333333333333;", 9672 Style); 9673 verifyFormat("return aaaaaaaaaa ? 1111111111111111\n" 9674 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n" 9675 " : 3333333333333333;", 9676 Style); 9677 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 9678 " : bbbbbbbbbbbbbb ? 222222\n" 9679 " : 333333;", 9680 Style); 9681 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 9682 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 9683 " : cccccccccccccc ? 3333333333333333\n" 9684 " : 4444444444444444;", 9685 Style); 9686 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc)\n" 9687 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 9688 " : 3333333333333333;", 9689 Style); 9690 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 9691 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 9692 " : (aaa ? bbb : ccc);", 9693 Style); 9694 verifyFormat( 9695 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 9696 " : cccccccccccccccccc)\n" 9697 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 9698 " : 3333333333333333;", 9699 Style); 9700 verifyFormat( 9701 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 9702 " : cccccccccccccccccc)\n" 9703 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 9704 " : 3333333333333333;", 9705 Style); 9706 verifyFormat( 9707 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 9708 " : dddddddddddddddddd)\n" 9709 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 9710 " : 3333333333333333;", 9711 Style); 9712 verifyFormat( 9713 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 9714 " : dddddddddddddddddd)\n" 9715 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 9716 " : 3333333333333333;", 9717 Style); 9718 verifyFormat( 9719 "return aaaaaaaaa ? 1111111111111111\n" 9720 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 9721 " : a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 9722 " : dddddddddddddddddd)", 9723 Style); 9724 verifyFormat( 9725 "return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 9726 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 9727 " : (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 9728 " : cccccccccccccccccc);", 9729 Style); 9730 verifyFormat( 9731 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 9732 " : ccccccccccccccc ? dddddddddddddddddd\n" 9733 " : eeeeeeeeeeeeeeeeee)\n" 9734 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 9735 " : 3333333333333333;", 9736 Style); 9737 verifyFormat( 9738 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 9739 " : ccccccccccccccc ? dddddddddddddddddd\n" 9740 " : eeeeeeeeeeeeeeeeee)\n" 9741 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 9742 " : 3333333333333333;", 9743 Style); 9744 verifyFormat( 9745 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 9746 " : cccccccccccc ? dddddddddddddddddd\n" 9747 " : eeeeeeeeeeeeeeeeee)\n" 9748 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 9749 " : 3333333333333333;", 9750 Style); 9751 verifyFormat( 9752 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 9753 " : cccccccccccccccccc\n" 9754 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 9755 " : 3333333333333333;", 9756 Style); 9757 verifyFormat( 9758 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 9759 " : cccccccccccccccc ? dddddddddddddddddd\n" 9760 " : eeeeeeeeeeeeeeeeee\n" 9761 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 9762 " : 3333333333333333;", 9763 Style); 9764 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa\n" 9765 " ? (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 9766 " : cccccccccccccccccc ? dddddddddddddddddd\n" 9767 " : eeeeeeeeeeeeeeeeee)\n" 9768 " : bbbbbbbbbbbbbbbbbbb ? 2222222222222222\n" 9769 " : 3333333333333333;", 9770 Style); 9771 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaa\n" 9772 " ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 9773 " : cccccccccccccccc ? dddddddddddddddddd\n" 9774 " : eeeeeeeeeeeeeeeeee\n" 9775 " : bbbbbbbbbbbbbbbbbbbbbbb ? 2222222222222222\n" 9776 " : 3333333333333333;", 9777 Style); 9778 9779 Style.AlignOperands = FormatStyle::OAS_DontAlign; 9780 Style.BreakBeforeTernaryOperators = false; 9781 // FIXME: Aligning the question marks is weird given DontAlign. 9782 // Consider disabling this alignment in this case. Also check whether this 9783 // will render the adjustment from https://reviews.llvm.org/D82199 9784 // unnecessary. 9785 verifyFormat("int x = aaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa :\n" 9786 " bbbb ? cccccccccccccccccc :\n" 9787 " ddddd;", 9788 Style); 9789 9790 verifyFormat( 9791 "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n" 9792 " /*\n" 9793 " */\n" 9794 " function() {\n" 9795 " try {\n" 9796 " return JJJJJJJJJJJJJJ(\n" 9797 " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n" 9798 " }\n" 9799 " } :\n" 9800 " function() {};", 9801 "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n" 9802 " /*\n" 9803 " */\n" 9804 " function() {\n" 9805 " try {\n" 9806 " return JJJJJJJJJJJJJJ(\n" 9807 " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n" 9808 " }\n" 9809 " } :\n" 9810 " function() {};", 9811 getGoogleStyle(FormatStyle::LK_JavaScript)); 9812 } 9813 9814 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) { 9815 FormatStyle Style = getLLVMStyleWithColumns(70); 9816 Style.BreakBeforeTernaryOperators = false; 9817 verifyFormat( 9818 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 9819 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 9820 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 9821 Style); 9822 verifyFormat( 9823 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 9824 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 9825 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 9826 Style); 9827 verifyFormat( 9828 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 9829 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 9830 Style); 9831 verifyFormat("aaaa(aaaaaaaa, aaaaaaaaaa,\n" 9832 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 9833 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 9834 Style); 9835 verifyFormat( 9836 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n" 9837 " aaaaaaaaaaaaa);", 9838 Style); 9839 verifyFormat( 9840 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9841 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 9842 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9843 " aaaaaaaaaaaaa);", 9844 Style); 9845 verifyFormat( 9846 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9847 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9848 " aaaaaaaaaaaaa);", 9849 Style); 9850 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 9851 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9852 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 9853 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9854 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 9855 Style); 9856 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9857 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 9858 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9859 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 9860 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9861 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 9862 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 9863 Style); 9864 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9865 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n" 9866 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9867 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 9868 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 9869 Style); 9870 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 9871 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 9872 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;", 9873 Style); 9874 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 9875 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 9876 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 9877 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 9878 Style); 9879 verifyFormat( 9880 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 9881 " aaaaaaaaaaaaaaa :\n" 9882 " aaaaaaaaaaaaaaa;", 9883 Style); 9884 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 9885 " aaaaaaaaa ?\n" 9886 " b :\n" 9887 " c);", 9888 Style); 9889 verifyFormat("unsigned Indent =\n" 9890 " format(TheLine.First,\n" 9891 " IndentForLevel[TheLine.Level] >= 0 ?\n" 9892 " IndentForLevel[TheLine.Level] :\n" 9893 " TheLine * 2,\n" 9894 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 9895 Style); 9896 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 9897 " aaaaaaaaaaaaaaa :\n" 9898 " bbbbbbbbbbbbbbb ? //\n" 9899 " ccccccccccccccc :\n" 9900 " ddddddddddddddd;", 9901 Style); 9902 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 9903 " aaaaaaaaaaaaaaa :\n" 9904 " (bbbbbbbbbbbbbbb ? //\n" 9905 " ccccccccccccccc :\n" 9906 " ddddddddddddddd);", 9907 Style); 9908 verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 9909 " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n" 9910 " ccccccccccccccccccccccccccc;", 9911 Style); 9912 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 9913 " aaaaa :\n" 9914 " bbbbbbbbbbbbbbb + cccccccccccccccc;", 9915 Style); 9916 9917 // Chained conditionals 9918 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 9919 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 9920 " 3333333333333333;", 9921 Style); 9922 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 9923 " bbbbbbbbbb ? 2222222222222222 :\n" 9924 " 3333333333333333;", 9925 Style); 9926 verifyFormat("return aaaaaaaaaa ? 1111111111111111 :\n" 9927 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 9928 " 3333333333333333;", 9929 Style); 9930 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 9931 " bbbbbbbbbbbbbbbb ? 222222 :\n" 9932 " 333333;", 9933 Style); 9934 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 9935 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 9936 " cccccccccccccccc ? 3333333333333333 :\n" 9937 " 4444444444444444;", 9938 Style); 9939 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc) :\n" 9940 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 9941 " 3333333333333333;", 9942 Style); 9943 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 9944 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 9945 " (aaa ? bbb : ccc);", 9946 Style); 9947 verifyFormat( 9948 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 9949 " cccccccccccccccccc) :\n" 9950 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 9951 " 3333333333333333;", 9952 Style); 9953 verifyFormat( 9954 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 9955 " cccccccccccccccccc) :\n" 9956 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 9957 " 3333333333333333;", 9958 Style); 9959 verifyFormat( 9960 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 9961 " dddddddddddddddddd) :\n" 9962 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 9963 " 3333333333333333;", 9964 Style); 9965 verifyFormat( 9966 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 9967 " dddddddddddddddddd) :\n" 9968 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 9969 " 3333333333333333;", 9970 Style); 9971 verifyFormat( 9972 "return aaaaaaaaa ? 1111111111111111 :\n" 9973 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 9974 " a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 9975 " dddddddddddddddddd)", 9976 Style); 9977 verifyFormat( 9978 "return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 9979 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 9980 " (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 9981 " cccccccccccccccccc);", 9982 Style); 9983 verifyFormat( 9984 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 9985 " ccccccccccccccccc ? dddddddddddddddddd :\n" 9986 " eeeeeeeeeeeeeeeeee) :\n" 9987 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 9988 " 3333333333333333;", 9989 Style); 9990 verifyFormat( 9991 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 9992 " ccccccccccccc ? dddddddddddddddddd :\n" 9993 " eeeeeeeeeeeeeeeeee) :\n" 9994 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 9995 " 3333333333333333;", 9996 Style); 9997 verifyFormat( 9998 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 9999 " ccccccccccccccccc ? dddddddddddddddddd :\n" 10000 " eeeeeeeeeeeeeeeeee) :\n" 10001 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 10002 " 3333333333333333;", 10003 Style); 10004 verifyFormat( 10005 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 10006 " cccccccccccccccccc :\n" 10007 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 10008 " 3333333333333333;", 10009 Style); 10010 verifyFormat( 10011 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 10012 " cccccccccccccccccc ? dddddddddddddddddd :\n" 10013 " eeeeeeeeeeeeeeeeee :\n" 10014 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 10015 " 3333333333333333;", 10016 Style); 10017 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n" 10018 " (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 10019 " cccccccccccccccccc ? dddddddddddddddddd :\n" 10020 " eeeeeeeeeeeeeeeeee) :\n" 10021 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 10022 " 3333333333333333;", 10023 Style); 10024 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n" 10025 " aaaaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 10026 " cccccccccccccccccccc ? dddddddddddddddddd :\n" 10027 " eeeeeeeeeeeeeeeeee :\n" 10028 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 10029 " 3333333333333333;", 10030 Style); 10031 } 10032 10033 TEST_F(FormatTest, DeclarationsOfMultipleVariables) { 10034 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" 10035 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); 10036 verifyFormat("bool a = true, b = false;"); 10037 10038 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" 10039 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" 10040 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" 10041 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); 10042 verifyFormat( 10043 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 10044 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n" 10045 " d = e && f;"); 10046 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n" 10047 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;"); 10048 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 10049 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;"); 10050 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n" 10051 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;"); 10052 10053 FormatStyle Style = getGoogleStyle(); 10054 Style.PointerAlignment = FormatStyle::PAS_Left; 10055 Style.DerivePointerAlignment = false; 10056 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10057 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" 10058 " *b = bbbbbbbbbbbbbbbbbbb;", 10059 Style); 10060 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 10061 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;", 10062 Style); 10063 verifyFormat("vector<int*> a, b;", Style); 10064 verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style); 10065 verifyFormat("/*comment*/ for (int *p, *q; p != q; p = p->next) {\n}", Style); 10066 verifyFormat("if (int *p, *q; p != q) {\n p = p->next;\n}", Style); 10067 verifyFormat("/*comment*/ if (int *p, *q; p != q) {\n p = p->next;\n}", 10068 Style); 10069 verifyFormat("switch (int *p, *q; p != q) {\n default:\n break;\n}", 10070 Style); 10071 verifyFormat( 10072 "/*comment*/ switch (int *p, *q; p != q) {\n default:\n break;\n}", 10073 Style); 10074 10075 verifyFormat("if ([](int* p, int* q) {}()) {\n}", Style); 10076 verifyFormat("for ([](int* p, int* q) {}();;) {\n}", Style); 10077 verifyFormat("for (; [](int* p, int* q) {}();) {\n}", Style); 10078 verifyFormat("for (;; [](int* p, int* q) {}()) {\n}", Style); 10079 verifyFormat("switch ([](int* p, int* q) {}()) {\n default:\n break;\n}", 10080 Style); 10081 } 10082 10083 TEST_F(FormatTest, ConditionalExpressionsInBrackets) { 10084 verifyFormat("arr[foo ? bar : baz];"); 10085 verifyFormat("f()[foo ? bar : baz];"); 10086 verifyFormat("(a + b)[foo ? bar : baz];"); 10087 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); 10088 } 10089 10090 TEST_F(FormatTest, AlignsStringLiterals) { 10091 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" 10092 " \"short literal\");"); 10093 verifyFormat( 10094 "looooooooooooooooooooooooongFunction(\n" 10095 " \"short literal\"\n" 10096 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); 10097 verifyFormat("someFunction(\"Always break between multi-line\"\n" 10098 " \" string literals\",\n" 10099 " also, other, parameters);"); 10100 verifyFormat("fun + \"1243\" /* comment */\n" 10101 " \"5678\";", 10102 "fun + \"1243\" /* comment */\n" 10103 " \"5678\";", 10104 getLLVMStyleWithColumns(28)); 10105 verifyFormat( 10106 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 10107 " \"aaaaaaaaaaaaaaaaaaaaa\"\n" 10108 " \"aaaaaaaaaaaaaaaa\";", 10109 "aaaaaa =" 10110 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " 10111 "aaaaaaaaaaaaaaaaaaaaa\" " 10112 "\"aaaaaaaaaaaaaaaa\";"); 10113 verifyFormat("a = a + \"a\"\n" 10114 " \"a\"\n" 10115 " \"a\";"); 10116 verifyFormat("f(\"a\", \"b\"\n" 10117 " \"c\");"); 10118 10119 verifyFormat( 10120 "#define LL_FORMAT \"ll\"\n" 10121 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n" 10122 " \"d, ddddddddd: %\" LL_FORMAT \"d\");"); 10123 10124 verifyFormat("#define A(X) \\\n" 10125 " \"aaaaa\" #X \"bbbbbb\" \\\n" 10126 " \"ccccc\"", 10127 getLLVMStyleWithColumns(23)); 10128 verifyFormat("#define A \"def\"\n" 10129 "f(\"abc\" A \"ghi\"\n" 10130 " \"jkl\");"); 10131 10132 verifyFormat("f(L\"a\"\n" 10133 " L\"b\");"); 10134 verifyFormat("#define A(X) \\\n" 10135 " L\"aaaaa\" #X L\"bbbbbb\" \\\n" 10136 " L\"ccccc\"", 10137 getLLVMStyleWithColumns(25)); 10138 10139 verifyFormat("f(@\"a\"\n" 10140 " @\"b\");"); 10141 verifyFormat("NSString s = @\"a\"\n" 10142 " @\"b\"\n" 10143 " @\"c\";"); 10144 verifyFormat("NSString s = @\"a\"\n" 10145 " \"b\"\n" 10146 " \"c\";"); 10147 } 10148 10149 TEST_F(FormatTest, ReturnTypeBreakingStyle) { 10150 FormatStyle Style = getLLVMStyle(); 10151 Style.ColumnLimit = 60; 10152 10153 // No declarations or definitions should be moved to own line. 10154 Style.BreakAfterReturnType = FormatStyle::RTBS_None; 10155 verifyFormat("class A {\n" 10156 " int f() { return 1; }\n" 10157 " int g();\n" 10158 " long\n" 10159 " foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaar();\n" 10160 "};\n" 10161 "int f() { return 1; }\n" 10162 "int g();\n" 10163 "int foooooooooooooooooooooooooooo::\n" 10164 " baaaaaaaaaaaaaaaaaaaaar();", 10165 Style); 10166 10167 // It is now allowed to break after a short return type if necessary. 10168 Style.BreakAfterReturnType = FormatStyle::RTBS_Automatic; 10169 verifyFormat("class A {\n" 10170 " int f() { return 1; }\n" 10171 " int g();\n" 10172 " long\n" 10173 " foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaar();\n" 10174 "};\n" 10175 "int f() { return 1; }\n" 10176 "int g();\n" 10177 "int\n" 10178 "foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaaar();", 10179 Style); 10180 10181 // It now must never break after a short return type. 10182 Style.BreakAfterReturnType = FormatStyle::RTBS_ExceptShortType; 10183 verifyFormat("class A {\n" 10184 " int f() { return 1; }\n" 10185 " int g();\n" 10186 " long foooooooooooooooooooooooooooo::\n" 10187 " baaaaaaaaaaaaaaaaaaaar();\n" 10188 "};\n" 10189 "int f() { return 1; }\n" 10190 "int g();\n" 10191 "int foooooooooooooooooooooooooooo::\n" 10192 " baaaaaaaaaaaaaaaaaaaaar();", 10193 Style); 10194 10195 // All declarations and definitions should have the return type moved to its 10196 // own line. 10197 Style.BreakAfterReturnType = FormatStyle::RTBS_All; 10198 Style.TypenameMacros = {"LIST"}; 10199 verifyFormat("SomeType\n" 10200 "funcdecl(LIST(uint64_t));", 10201 Style); 10202 verifyFormat("class E {\n" 10203 " int\n" 10204 " f() {\n" 10205 " return 1;\n" 10206 " }\n" 10207 " int\n" 10208 " g();\n" 10209 " long\n" 10210 " foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaar();\n" 10211 "};\n" 10212 "int\n" 10213 "f() {\n" 10214 " return 1;\n" 10215 "}\n" 10216 "int\n" 10217 "g();\n" 10218 "int\n" 10219 "foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaaar();", 10220 Style); 10221 10222 // Top-level definitions, and no kinds of declarations should have the 10223 // return type moved to its own line. 10224 Style.BreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions; 10225 verifyFormat("class B {\n" 10226 " int f() { return 1; }\n" 10227 " int g();\n" 10228 "};\n" 10229 "int\n" 10230 "f() {\n" 10231 " return 1;\n" 10232 "}\n" 10233 "int g();", 10234 Style); 10235 10236 // Top-level definitions and declarations should have the return type moved 10237 // to its own line. 10238 Style.BreakAfterReturnType = FormatStyle::RTBS_TopLevel; 10239 verifyFormat("class C {\n" 10240 " int f() { return 1; }\n" 10241 " int g();\n" 10242 "};\n" 10243 "int\n" 10244 "f() {\n" 10245 " return 1;\n" 10246 "}\n" 10247 "int\n" 10248 "g();\n" 10249 "int\n" 10250 "foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaaar();", 10251 Style); 10252 10253 // All definitions should have the return type moved to its own line, but no 10254 // kinds of declarations. 10255 Style.BreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 10256 verifyFormat("class D {\n" 10257 " int\n" 10258 " f() {\n" 10259 " return 1;\n" 10260 " }\n" 10261 " int g();\n" 10262 "};\n" 10263 "int\n" 10264 "f() {\n" 10265 " return 1;\n" 10266 "}\n" 10267 "int g();", 10268 Style); 10269 verifyFormat("const char *\n" 10270 "f(void) {\n" // Break here. 10271 " return \"\";\n" 10272 "}\n" 10273 "const char *bar(void);", // No break here. 10274 Style); 10275 verifyFormat("template <class T>\n" 10276 "T *\n" 10277 "f(T &c) {\n" // Break here. 10278 " return NULL;\n" 10279 "}\n" 10280 "template <class T> T *f(T &c);", // No break here. 10281 Style); 10282 verifyFormat("class C {\n" 10283 " int\n" 10284 " operator+() {\n" 10285 " return 1;\n" 10286 " }\n" 10287 " int\n" 10288 " operator()() {\n" 10289 " return 1;\n" 10290 " }\n" 10291 "};", 10292 Style); 10293 verifyFormat("void\n" 10294 "A::operator()() {}\n" 10295 "void\n" 10296 "A::operator>>() {}\n" 10297 "void\n" 10298 "A::operator+() {}\n" 10299 "void\n" 10300 "A::operator*() {}\n" 10301 "void\n" 10302 "A::operator->() {}\n" 10303 "void\n" 10304 "A::operator void *() {}\n" 10305 "void\n" 10306 "A::operator void &() {}\n" 10307 "void\n" 10308 "A::operator void &&() {}\n" 10309 "void\n" 10310 "A::operator char *() {}\n" 10311 "void\n" 10312 "A::operator[]() {}\n" 10313 "void\n" 10314 "A::operator!() {}\n" 10315 "void\n" 10316 "A::operator**() {}\n" 10317 "void\n" 10318 "A::operator<Foo> *() {}\n" 10319 "void\n" 10320 "A::operator<Foo> **() {}\n" 10321 "void\n" 10322 "A::operator<Foo> &() {}\n" 10323 "void\n" 10324 "A::operator void **() {}", 10325 Style); 10326 verifyFormat("constexpr auto\n" 10327 "operator()() const -> reference {}\n" 10328 "constexpr auto\n" 10329 "operator>>() const -> reference {}\n" 10330 "constexpr auto\n" 10331 "operator+() const -> reference {}\n" 10332 "constexpr auto\n" 10333 "operator*() const -> reference {}\n" 10334 "constexpr auto\n" 10335 "operator->() const -> reference {}\n" 10336 "constexpr auto\n" 10337 "operator++() const -> reference {}\n" 10338 "constexpr auto\n" 10339 "operator void *() const -> reference {}\n" 10340 "constexpr auto\n" 10341 "operator void **() const -> reference {}\n" 10342 "constexpr auto\n" 10343 "operator void *() const -> reference {}\n" 10344 "constexpr auto\n" 10345 "operator void &() const -> reference {}\n" 10346 "constexpr auto\n" 10347 "operator void &&() const -> reference {}\n" 10348 "constexpr auto\n" 10349 "operator char *() const -> reference {}\n" 10350 "constexpr auto\n" 10351 "operator!() const -> reference {}\n" 10352 "constexpr auto\n" 10353 "operator[]() const -> reference {}", 10354 Style); 10355 verifyFormat("void *operator new(std::size_t s);", // No break here. 10356 Style); 10357 verifyFormat("void *\n" 10358 "operator new(std::size_t s) {}", 10359 Style); 10360 verifyFormat("void *\n" 10361 "operator delete[](void *ptr) {}", 10362 Style); 10363 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 10364 verifyFormat("const char *\n" 10365 "f(void)\n" // Break here. 10366 "{\n" 10367 " return \"\";\n" 10368 "}\n" 10369 "const char *bar(void);", // No break here. 10370 Style); 10371 verifyFormat("template <class T>\n" 10372 "T *\n" // Problem here: no line break 10373 "f(T &c)\n" // Break here. 10374 "{\n" 10375 " return NULL;\n" 10376 "}\n" 10377 "template <class T> T *f(T &c);", // No break here. 10378 Style); 10379 verifyFormat("int\n" 10380 "foo(A<bool> a)\n" 10381 "{\n" 10382 " return a;\n" 10383 "}", 10384 Style); 10385 verifyFormat("int\n" 10386 "foo(A<8> a)\n" 10387 "{\n" 10388 " return a;\n" 10389 "}", 10390 Style); 10391 verifyFormat("int\n" 10392 "foo(A<B<bool>, 8> a)\n" 10393 "{\n" 10394 " return a;\n" 10395 "}", 10396 Style); 10397 verifyFormat("int\n" 10398 "foo(A<B<8>, bool> a)\n" 10399 "{\n" 10400 " return a;\n" 10401 "}", 10402 Style); 10403 verifyFormat("int\n" 10404 "foo(A<B<bool>, bool> a)\n" 10405 "{\n" 10406 " return a;\n" 10407 "}", 10408 Style); 10409 verifyFormat("int\n" 10410 "foo(A<B<8>, 8> a)\n" 10411 "{\n" 10412 " return a;\n" 10413 "}", 10414 Style); 10415 10416 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 10417 Style.BraceWrapping.AfterFunction = true; 10418 verifyFormat("int f(i);\n" // No break here. 10419 "int\n" // Break here. 10420 "f(i)\n" 10421 "{\n" 10422 " return i + 1;\n" 10423 "}\n" 10424 "int\n" // Break here. 10425 "f(i)\n" 10426 "{\n" 10427 " return i + 1;\n" 10428 "};", 10429 Style); 10430 verifyFormat("int f(a, b, c);\n" // No break here. 10431 "int\n" // Break here. 10432 "f(a, b, c)\n" // Break here. 10433 "short a, b;\n" 10434 "float c;\n" 10435 "{\n" 10436 " return a + b < c;\n" 10437 "}\n" 10438 "int\n" // Break here. 10439 "f(a, b, c)\n" // Break here. 10440 "short a, b;\n" 10441 "float c;\n" 10442 "{\n" 10443 " return a + b < c;\n" 10444 "};", 10445 Style); 10446 verifyFormat("byte *\n" // Break here. 10447 "f(a)\n" // Break here. 10448 "byte a[];\n" 10449 "{\n" 10450 " return a;\n" 10451 "}", 10452 Style); 10453 verifyFormat("byte *\n" 10454 "f(a)\n" 10455 "byte /* K&R C */ a[];\n" 10456 "{\n" 10457 " return a;\n" 10458 "}\n" 10459 "byte *\n" 10460 "g(p)\n" 10461 "byte /* K&R C */ *p;\n" 10462 "{\n" 10463 " return p;\n" 10464 "}", 10465 Style); 10466 verifyFormat("bool f(int a, int) override;\n" 10467 "Bar g(int a, Bar) final;\n" 10468 "Bar h(a, Bar) final;", 10469 Style); 10470 verifyFormat("int\n" 10471 "f(a)", 10472 Style); 10473 verifyFormat("bool\n" 10474 "f(size_t = 0, bool b = false)\n" 10475 "{\n" 10476 " return !b;\n" 10477 "}", 10478 Style); 10479 10480 // The return breaking style doesn't affect: 10481 // * function and object definitions with attribute-like macros 10482 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n" 10483 " ABSL_GUARDED_BY(mutex) = {};", 10484 getGoogleStyleWithColumns(40)); 10485 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n" 10486 " ABSL_GUARDED_BY(mutex); // comment", 10487 getGoogleStyleWithColumns(40)); 10488 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n" 10489 " ABSL_GUARDED_BY(mutex1)\n" 10490 " ABSL_GUARDED_BY(mutex2);", 10491 getGoogleStyleWithColumns(40)); 10492 verifyFormat("Tttttt f(int a, int b)\n" 10493 " ABSL_GUARDED_BY(mutex1)\n" 10494 " ABSL_GUARDED_BY(mutex2);", 10495 getGoogleStyleWithColumns(40)); 10496 // * typedefs 10497 verifyGoogleFormat("typedef ATTR(X) char x;"); 10498 10499 Style = getGNUStyle(); 10500 10501 // Test for comments at the end of function declarations. 10502 verifyFormat("void\n" 10503 "foo (int a, /*abc*/ int b) // def\n" 10504 "{\n" 10505 "}", 10506 Style); 10507 10508 verifyFormat("void\n" 10509 "foo (int a, /* abc */ int b) /* def */\n" 10510 "{\n" 10511 "}", 10512 Style); 10513 10514 // Definitions that should not break after return type 10515 verifyFormat("void foo (int a, int b); // def", Style); 10516 verifyFormat("void foo (int a, int b); /* def */", Style); 10517 verifyFormat("void foo (int a, int b);", Style); 10518 } 10519 10520 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) { 10521 FormatStyle NoBreak = getLLVMStyle(); 10522 NoBreak.AlwaysBreakBeforeMultilineStrings = false; 10523 FormatStyle Break = getLLVMStyle(); 10524 Break.AlwaysBreakBeforeMultilineStrings = true; 10525 verifyFormat("aaaa = \"bbbb\"\n" 10526 " \"cccc\";", 10527 NoBreak); 10528 verifyFormat("aaaa =\n" 10529 " \"bbbb\"\n" 10530 " \"cccc\";", 10531 Break); 10532 verifyFormat("aaaa(\"bbbb\"\n" 10533 " \"cccc\");", 10534 NoBreak); 10535 verifyFormat("aaaa(\n" 10536 " \"bbbb\"\n" 10537 " \"cccc\");", 10538 Break); 10539 verifyFormat("aaaa(qqq, \"bbbb\"\n" 10540 " \"cccc\");", 10541 NoBreak); 10542 verifyFormat("aaaa(qqq,\n" 10543 " \"bbbb\"\n" 10544 " \"cccc\");", 10545 Break); 10546 verifyFormat("aaaa(qqq,\n" 10547 " L\"bbbb\"\n" 10548 " L\"cccc\");", 10549 Break); 10550 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n" 10551 " \"bbbb\"));", 10552 Break); 10553 verifyFormat("string s = someFunction(\n" 10554 " \"abc\"\n" 10555 " \"abc\");", 10556 Break); 10557 10558 // As we break before unary operators, breaking right after them is bad. 10559 verifyFormat("string foo = abc ? \"x\"\n" 10560 " \"blah blah blah blah blah blah\"\n" 10561 " : \"y\";", 10562 Break); 10563 10564 // Don't break if there is no column gain. 10565 verifyFormat("f(\"aaaa\"\n" 10566 " \"bbbb\");", 10567 Break); 10568 10569 // Treat literals with escaped newlines like multi-line string literals. 10570 verifyNoChange("x = \"a\\\n" 10571 "b\\\n" 10572 "c\";", 10573 NoBreak); 10574 verifyFormat("xxxx =\n" 10575 " \"a\\\n" 10576 "b\\\n" 10577 "c\";", 10578 "xxxx = \"a\\\n" 10579 "b\\\n" 10580 "c\";", 10581 Break); 10582 10583 verifyFormat("NSString *const kString =\n" 10584 " @\"aaaa\"\n" 10585 " @\"bbbb\";", 10586 "NSString *const kString = @\"aaaa\"\n" 10587 "@\"bbbb\";", 10588 Break); 10589 10590 Break.ColumnLimit = 0; 10591 verifyFormat("const char *hello = \"hello llvm\";", Break); 10592 } 10593 10594 TEST_F(FormatTest, AlignsPipes) { 10595 verifyFormat( 10596 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10597 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10598 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 10599 verifyFormat( 10600 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 10601 " << aaaaaaaaaaaaaaaaaaaa;"); 10602 verifyFormat( 10603 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10604 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 10605 verifyFormat( 10606 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 10607 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 10608 verifyFormat( 10609 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 10610 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 10611 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 10612 verifyFormat( 10613 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10614 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 10615 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 10616 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10617 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 10618 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 10619 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 10620 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n" 10621 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);"); 10622 verifyFormat( 10623 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10624 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 10625 verifyFormat( 10626 "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n" 10627 " aaaaaaaaaaaaaaaaaaaaaaaaaa);"); 10628 10629 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n" 10630 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();"); 10631 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10632 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 10633 " aaaaaaaaaaaaaaaaaaaaa)\n" 10634 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;"); 10635 verifyFormat("LOG_IF(aaa == //\n" 10636 " bbb)\n" 10637 " << a << b;"); 10638 10639 // But sometimes, breaking before the first "<<" is desirable. 10640 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 10641 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);"); 10642 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n" 10643 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10644 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 10645 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n" 10646 " << BEF << IsTemplate << Description << E->getType();"); 10647 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 10648 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10649 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 10650 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 10651 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10652 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 10653 " << aaa;"); 10654 10655 verifyFormat( 10656 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10657 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 10658 10659 // Incomplete string literal. 10660 verifyFormat("llvm::errs() << \"\n" 10661 " << a;", 10662 "llvm::errs() << \"\n<<a;"); 10663 10664 verifyFormat("void f() {\n" 10665 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n" 10666 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n" 10667 "}"); 10668 10669 // Handle 'endl'. 10670 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n" 10671 " << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 10672 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 10673 10674 // Handle '\n'. 10675 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n" 10676 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 10677 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n" 10678 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';"); 10679 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n" 10680 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";"); 10681 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 10682 } 10683 10684 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) { 10685 verifyFormat("return out << \"somepacket = {\\n\"\n" 10686 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" 10687 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" 10688 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" 10689 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" 10690 " << \"}\";"); 10691 10692 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 10693 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 10694 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;"); 10695 verifyFormat( 10696 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" 10697 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" 10698 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" 10699 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" 10700 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); 10701 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" 10702 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 10703 verifyFormat( 10704 "void f() {\n" 10705 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n" 10706 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 10707 "}"); 10708 10709 // Breaking before the first "<<" is generally not desirable. 10710 verifyFormat( 10711 "llvm::errs()\n" 10712 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10713 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10714 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10715 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 10716 getLLVMStyleWithColumns(70)); 10717 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n" 10718 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10719 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 10720 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10721 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 10722 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 10723 getLLVMStyleWithColumns(70)); 10724 10725 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 10726 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 10727 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;"); 10728 verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 10729 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 10730 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);"); 10731 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n" 10732 " (aaaa + aaaa);", 10733 getLLVMStyleWithColumns(40)); 10734 verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n" 10735 " (aaaaaaa + aaaaa));", 10736 getLLVMStyleWithColumns(40)); 10737 verifyFormat( 10738 "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n" 10739 " SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n" 10740 " bbbbbbbbbbbbbbbbbbbbbbb);"); 10741 } 10742 10743 TEST_F(FormatTest, WrapBeforeInsertionOperatorbetweenStringLiterals) { 10744 verifyFormat("QStringList() << \"foo\" << \"bar\";"); 10745 10746 verifyNoChange("QStringList() << \"foo\"\n" 10747 " << \"bar\";"); 10748 10749 verifyFormat("log_error(log, \"foo\" << \"bar\");", 10750 "log_error(log, \"foo\"\n" 10751 " << \"bar\");"); 10752 } 10753 10754 TEST_F(FormatTest, UnderstandsEquals) { 10755 verifyFormat( 10756 "aaaaaaaaaaaaaaaaa =\n" 10757 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 10758 verifyFormat( 10759 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 10760 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 10761 verifyFormat( 10762 "if (a) {\n" 10763 " f();\n" 10764 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 10765 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 10766 "}"); 10767 10768 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 10769 " 100000000 + 10000000) {\n}"); 10770 } 10771 10772 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 10773 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 10774 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 10775 10776 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 10777 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 10778 10779 verifyFormat( 10780 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 10781 " Parameter2);"); 10782 10783 verifyFormat( 10784 "ShortObject->shortFunction(\n" 10785 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 10786 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 10787 10788 verifyFormat("loooooooooooooongFunction(\n" 10789 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 10790 10791 verifyFormat( 10792 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 10793 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 10794 10795 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 10796 " .WillRepeatedly(Return(SomeValue));"); 10797 verifyFormat("void f() {\n" 10798 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 10799 " .Times(2)\n" 10800 " .WillRepeatedly(Return(SomeValue));\n" 10801 "}"); 10802 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n" 10803 " ccccccccccccccccccccccc);"); 10804 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 10805 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 10806 " .aaaaa(aaaaa),\n" 10807 " aaaaaaaaaaaaaaaaaaaaa);"); 10808 verifyFormat("void f() {\n" 10809 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10810 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n" 10811 "}"); 10812 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 10813 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 10814 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 10815 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 10816 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 10817 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10818 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10819 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10820 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n" 10821 "}"); 10822 10823 // Here, it is not necessary to wrap at "." or "->". 10824 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 10825 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 10826 verifyFormat( 10827 "aaaaaaaaaaa->aaaaaaaaa(\n" 10828 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 10829 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));"); 10830 10831 verifyFormat( 10832 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10833 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); 10834 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n" 10835 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 10836 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" 10837 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 10838 10839 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 10840 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 10841 " .a();"); 10842 10843 FormatStyle NoBinPacking = getLLVMStyle(); 10844 NoBinPacking.BinPackParameters = FormatStyle::BPPS_OnePerLine; 10845 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 10846 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 10847 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" 10848 " aaaaaaaaaaaaaaaaaaa,\n" 10849 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 10850 NoBinPacking); 10851 10852 // If there is a subsequent call, change to hanging indentation. 10853 verifyFormat( 10854 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10855 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n" 10856 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 10857 verifyFormat( 10858 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10859 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));"); 10860 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10861 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 10862 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 10863 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10864 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 10865 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 10866 } 10867 10868 TEST_F(FormatTest, WrapsTemplateDeclarations) { 10869 verifyFormat("template <typename T>\n" 10870 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 10871 verifyFormat("template <typename T>\n" 10872 "// T should be one of {A, B}.\n" 10873 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 10874 verifyFormat( 10875 "template <typename T>\n" 10876 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); 10877 verifyFormat("template <typename T>\n" 10878 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 10879 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 10880 verifyFormat( 10881 "template <typename T>\n" 10882 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 10883 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 10884 verifyFormat( 10885 "template <typename T>\n" 10886 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 10887 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 10888 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 10889 verifyFormat("template <typename T>\n" 10890 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10891 " int aaaaaaaaaaaaaaaaaaaaaa);"); 10892 verifyFormat( 10893 "template <typename T1, typename T2 = char, typename T3 = char,\n" 10894 " typename T4 = char>\n" 10895 "void f();"); 10896 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n" 10897 " template <typename> class cccccccccccccccccccccc,\n" 10898 " typename ddddddddddddd>\n" 10899 "class C {};"); 10900 verifyFormat( 10901 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 10902 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 10903 10904 verifyFormat("void f() {\n" 10905 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 10906 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n" 10907 "}"); 10908 10909 verifyFormat("template <typename T> class C {};"); 10910 verifyFormat("template <typename T> void f();"); 10911 verifyFormat("template <typename T> void f() {}"); 10912 verifyFormat( 10913 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 10914 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 10915 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n" 10916 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 10917 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 10918 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n" 10919 " bbbbbbbbbbbbbbbbbbbbbbbb);", 10920 getLLVMStyleWithColumns(72)); 10921 verifyFormat("static_cast<A< //\n" 10922 " B> *>(\n" 10923 "\n" 10924 ");", 10925 "static_cast<A<//\n" 10926 " B>*>(\n" 10927 "\n" 10928 " );"); 10929 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10930 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);"); 10931 10932 FormatStyle AlwaysBreak = getLLVMStyle(); 10933 AlwaysBreak.BreakTemplateDeclarations = FormatStyle::BTDS_Yes; 10934 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak); 10935 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak); 10936 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak); 10937 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 10938 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 10939 " ccccccccccccccccccccccccccccccccccccccccccccccc);"); 10940 verifyFormat("template <template <typename> class Fooooooo,\n" 10941 " template <typename> class Baaaaaaar>\n" 10942 "struct C {};", 10943 AlwaysBreak); 10944 verifyFormat("template <typename T> // T can be A, B or C.\n" 10945 "struct C {};", 10946 AlwaysBreak); 10947 verifyFormat("template <typename T>\n" 10948 "C(T) noexcept;", 10949 AlwaysBreak); 10950 verifyFormat("template <typename T>\n" 10951 "ClassName(T) noexcept;", 10952 AlwaysBreak); 10953 verifyFormat("template <typename T>\n" 10954 "POOR_NAME(T) noexcept;", 10955 AlwaysBreak); 10956 verifyFormat("template <enum E> class A {\n" 10957 "public:\n" 10958 " E *f();\n" 10959 "};"); 10960 10961 FormatStyle NeverBreak = getLLVMStyle(); 10962 NeverBreak.BreakTemplateDeclarations = FormatStyle::BTDS_No; 10963 verifyFormat("template <typename T> class C {};", NeverBreak); 10964 verifyFormat("template <typename T> void f();", NeverBreak); 10965 verifyFormat("template <typename T> void f() {}", NeverBreak); 10966 verifyFormat("template <typename T> C(T) noexcept;", NeverBreak); 10967 verifyFormat("template <typename T> ClassName(T) noexcept;", NeverBreak); 10968 verifyFormat("template <typename T> POOR_NAME(T) noexcept;", NeverBreak); 10969 verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa " 10970 "bbbbbbbbbbbbbbbbbbbb) {}", 10971 NeverBreak); 10972 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 10973 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 10974 " ccccccccccccccccccccccccccccccccccccccccccccccc);", 10975 NeverBreak); 10976 verifyFormat("template <template <typename> class Fooooooo,\n" 10977 " template <typename> class Baaaaaaar>\n" 10978 "struct C {};", 10979 NeverBreak); 10980 verifyFormat("template <typename T> // T can be A, B or C.\n" 10981 "struct C {};", 10982 NeverBreak); 10983 verifyFormat("template <enum E> class A {\n" 10984 "public:\n" 10985 " E *f();\n" 10986 "};", 10987 NeverBreak); 10988 NeverBreak.PenaltyBreakTemplateDeclaration = 100; 10989 verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa " 10990 "bbbbbbbbbbbbbbbbbbbb) {}", 10991 NeverBreak); 10992 10993 auto Style = getLLVMStyle(); 10994 Style.BreakTemplateDeclarations = FormatStyle::BTDS_Leave; 10995 10996 verifyNoChange("template <typename T>\n" 10997 "class C {};", 10998 Style); 10999 verifyFormat("template <typename T> class C {};", Style); 11000 11001 verifyNoChange("template <typename T>\n" 11002 "void f();", 11003 Style); 11004 verifyFormat("template <typename T> void f();", Style); 11005 11006 verifyNoChange("template <typename T>\n" 11007 "void f() {}", 11008 Style); 11009 verifyFormat("template <typename T> void f() {}", Style); 11010 11011 verifyNoChange("template <typename T>\n" 11012 "// T can be A, B or C.\n" 11013 "struct C {};", 11014 Style); 11015 verifyFormat("template <typename T> // T can be A, B or C.\n" 11016 "struct C {};", 11017 Style); 11018 11019 verifyNoChange("template <typename T>\n" 11020 "C(T) noexcept;", 11021 Style); 11022 verifyFormat("template <typename T> C(T) noexcept;", Style); 11023 11024 verifyNoChange("template <enum E>\n" 11025 "class A {\n" 11026 "public:\n" 11027 " E *f();\n" 11028 "};", 11029 Style); 11030 verifyFormat("template <enum E> class A {\n" 11031 "public:\n" 11032 " E *f();\n" 11033 "};", 11034 Style); 11035 11036 verifyNoChange("template <auto x>\n" 11037 "constexpr int simple(int) {\n" 11038 " char c;\n" 11039 " return 1;\n" 11040 "}", 11041 Style); 11042 verifyFormat("template <auto x> constexpr int simple(int) {\n" 11043 " char c;\n" 11044 " return 1;\n" 11045 "}", 11046 Style); 11047 11048 Style.RequiresClausePosition = FormatStyle::RCPS_WithPreceding; 11049 verifyNoChange("template <auto x>\n" 11050 "requires(x > 1)\n" 11051 "constexpr int with_req(int) {\n" 11052 " return 1;\n" 11053 "}", 11054 Style); 11055 verifyFormat("template <auto x> requires(x > 1)\n" 11056 "constexpr int with_req(int) {\n" 11057 " return 1;\n" 11058 "}", 11059 Style); 11060 } 11061 11062 TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) { 11063 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); 11064 Style.ColumnLimit = 60; 11065 verifyFormat("// Baseline - no comments.\n" 11066 "template <\n" 11067 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n" 11068 "void f() {}", 11069 Style); 11070 11071 verifyFormat("template <\n" 11072 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 11073 "void f() {}", 11074 "template <\n" 11075 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 11076 "void f() {}", 11077 Style); 11078 11079 verifyFormat( 11080 "template <\n" 11081 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n" 11082 "void f() {}", 11083 "template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n" 11084 "void f() {}", 11085 Style); 11086 11087 verifyFormat("template <\n" 11088 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 11089 " // multiline\n" 11090 "void f() {}", 11091 "template <\n" 11092 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 11093 " // multiline\n" 11094 "void f() {}", 11095 Style); 11096 11097 verifyFormat( 11098 "template <typename aaaaaaaaaa<\n" 11099 " bbbbbbbbbbbb>::value> // trailing loooong\n" 11100 "void f() {}", 11101 "template <\n" 11102 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n" 11103 "void f() {}", 11104 Style); 11105 } 11106 11107 TEST_F(FormatTest, WrapsTemplateParameters) { 11108 FormatStyle Style = getLLVMStyle(); 11109 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 11110 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 11111 verifyFormat( 11112 "template <typename... a> struct q {};\n" 11113 "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 11114 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 11115 " y;", 11116 Style); 11117 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 11118 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 11119 verifyFormat( 11120 "template <typename... a> struct r {};\n" 11121 "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 11122 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 11123 " y;", 11124 Style); 11125 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 11126 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 11127 verifyFormat("template <typename... a> struct s {};\n" 11128 "extern s<\n" 11129 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 11130 "aaaaaaaaaaaaaaaaaaaaaa,\n" 11131 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 11132 "aaaaaaaaaaaaaaaaaaaaaa>\n" 11133 " y;", 11134 Style); 11135 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 11136 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 11137 verifyFormat("template <typename... a> struct t {};\n" 11138 "extern t<\n" 11139 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 11140 "aaaaaaaaaaaaaaaaaaaaaa,\n" 11141 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 11142 "aaaaaaaaaaaaaaaaaaaaaa>\n" 11143 " y;", 11144 Style); 11145 } 11146 11147 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 11148 verifyFormat( 11149 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 11150 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 11151 verifyFormat( 11152 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 11153 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 11154 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 11155 11156 // FIXME: Should we have the extra indent after the second break? 11157 verifyFormat( 11158 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 11159 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 11160 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 11161 11162 verifyFormat( 11163 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 11164 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 11165 11166 // Breaking at nested name specifiers is generally not desirable. 11167 verifyFormat( 11168 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 11169 " aaaaaaaaaaaaaaaaaaaaaaa);"); 11170 11171 verifyFormat("aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n" 11172 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 11173 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 11174 " aaaaaaaaaaaaaaaaaaaaa);", 11175 getLLVMStyleWithColumns(74)); 11176 11177 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 11178 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 11179 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 11180 11181 verifyFormat( 11182 "LongClassNameToShowTheIssue::AndAnotherLongClassNameToShowTheIssue::\n" 11183 " AndAnotherLongClassNameToShowTheIssue() {}\n" 11184 "LongClassNameToShowTheIssue::AndAnotherLongClassNameToShowTheIssue::\n" 11185 " ~AndAnotherLongClassNameToShowTheIssue() {}"); 11186 } 11187 11188 TEST_F(FormatTest, UnderstandsTemplateParameters) { 11189 verifyFormat("A<int> a;"); 11190 verifyFormat("A<A<A<int>>> a;"); 11191 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 11192 verifyFormat("bool x = a < 1 || 2 > a;"); 11193 verifyFormat("bool x = 5 < f<int>();"); 11194 verifyFormat("bool x = f<int>() > 5;"); 11195 verifyFormat("bool x = 5 < a<int>::x;"); 11196 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 11197 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 11198 11199 verifyGoogleFormat("A<A<int>> a;"); 11200 verifyGoogleFormat("A<A<A<int>>> a;"); 11201 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 11202 verifyGoogleFormat("A<A<int> > a;"); 11203 verifyGoogleFormat("A<A<A<int> > > a;"); 11204 verifyGoogleFormat("A<A<A<A<int> > > > a;"); 11205 verifyGoogleFormat("A<::A<int>> a;"); 11206 verifyGoogleFormat("A<::A> a;"); 11207 verifyGoogleFormat("A< ::A> a;"); 11208 verifyGoogleFormat("A< ::A<int> > a;"); 11209 verifyFormat("A<A<A<A>>> a;", "A<A<A<A> >> a;", getGoogleStyle()); 11210 verifyFormat("A<A<A<A>>> a;", "A<A<A<A>> > a;", getGoogleStyle()); 11211 verifyFormat("A<::A<int>> a;", "A< ::A<int>> a;", getGoogleStyle()); 11212 verifyFormat("A<::A<int>> a;", "A<::A<int> > a;", getGoogleStyle()); 11213 verifyFormat("auto x = [] { A<A<A<A>>> a; };", "auto x=[]{A<A<A<A> >> a;};", 11214 getGoogleStyle()); 11215 11216 verifyFormat("A<A<int>> a;", getChromiumStyle(FormatStyle::LK_Cpp)); 11217 11218 // template closer followed by a token that starts with > or = 11219 verifyFormat("bool b = a<1> > 1;"); 11220 verifyFormat("bool b = a<1> >= 1;"); 11221 verifyFormat("int i = a<1> >> 1;"); 11222 FormatStyle Style = getLLVMStyle(); 11223 Style.SpaceBeforeAssignmentOperators = false; 11224 verifyFormat("bool b= a<1> == 1;", Style); 11225 verifyFormat("a<int> = 1;", Style); 11226 verifyFormat("a<int> >>= 1;", Style); 11227 11228 verifyFormat("test < a | b >> c;"); 11229 verifyFormat("test<test<a | b>> c;"); 11230 verifyFormat("test >> a >> b;"); 11231 verifyFormat("test << a >> b;"); 11232 11233 verifyFormat("f<int>();"); 11234 verifyFormat("template <typename T> void f() {}"); 11235 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;"); 11236 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : " 11237 "sizeof(char)>::type>;"); 11238 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};"); 11239 verifyFormat("f(a.operator()<A>());"); 11240 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 11241 " .template operator()<A>());", 11242 getLLVMStyleWithColumns(35)); 11243 verifyFormat("bool_constant<a && noexcept(f())>;"); 11244 verifyFormat("bool_constant<a || noexcept(f())>;"); 11245 11246 verifyFormat("if (std::tuple_size_v<T> > 0)"); 11247 11248 // Not template parameters. 11249 verifyFormat("return a < b && c > d;"); 11250 verifyFormat("a < 0 ? b : a > 0 ? c : d;"); 11251 verifyFormat("ratio{-1, 2} < ratio{-1, 3} == -1 / 3 > -1 / 2;"); 11252 verifyFormat("void f() {\n" 11253 " while (a < b && c > d) {\n" 11254 " }\n" 11255 "}"); 11256 verifyFormat("template <typename... Types>\n" 11257 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}"); 11258 11259 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 11260 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);", 11261 getLLVMStyleWithColumns(60)); 11262 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");"); 11263 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}"); 11264 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <"); 11265 verifyFormat("some_templated_type<decltype([](int i) { return i; })>"); 11266 11267 verifyFormat("#define FOO(typeName, realClass) \\\n" 11268 " {#typeName, foo<FooType>(new foo<realClass>(#typeName))}", 11269 getLLVMStyleWithColumns(60)); 11270 } 11271 11272 TEST_F(FormatTest, UnderstandsShiftOperators) { 11273 verifyFormat("if (i < x >> 1)"); 11274 verifyFormat("while (i < x >> 1)"); 11275 verifyFormat("for (unsigned i = 0; i < i; ++i, v = v >> 1)"); 11276 verifyFormat("for (unsigned i = 0; i < x >> 1; ++i, v = v >> 1)"); 11277 verifyFormat( 11278 "for (std::vector<int>::iterator i = 0; i < x >> 1; ++i, v = v >> 1)"); 11279 verifyFormat("Foo.call<Bar<Function>>()"); 11280 verifyFormat("if (Foo.call<Bar<Function>>() == 0)"); 11281 verifyFormat("for (std::vector<std::pair<int>>::iterator i = 0; i < x >> 1; " 11282 "++i, v = v >> 1)"); 11283 verifyFormat("if (w<u<v<x>>, 1>::t)"); 11284 } 11285 11286 TEST_F(FormatTest, BitshiftOperatorWidth) { 11287 verifyFormat("int a = 1 << 2; /* foo\n" 11288 " bar */", 11289 "int a=1<<2; /* foo\n" 11290 " bar */"); 11291 11292 verifyFormat("int b = 256 >> 1; /* foo\n" 11293 " bar */", 11294 "int b =256>>1 ; /* foo\n" 11295 " bar */"); 11296 } 11297 11298 TEST_F(FormatTest, UnderstandsBinaryOperators) { 11299 verifyFormat("COMPARE(a, ==, b);"); 11300 verifyFormat("auto s = sizeof...(Ts) - 1;"); 11301 } 11302 11303 TEST_F(FormatTest, UnderstandsPointersToMembers) { 11304 verifyFormat("int A::*x;"); 11305 verifyFormat("int (S::*func)(void *);"); 11306 verifyFormat("void f() { int (S::*func)(void *); }"); 11307 verifyFormat("typedef bool *(Class::*Member)() const;"); 11308 verifyFormat("void f() {\n" 11309 " (a->*f)();\n" 11310 " a->*x;\n" 11311 " (a.*f)();\n" 11312 " ((*a).*f)();\n" 11313 " a.*x;\n" 11314 "}"); 11315 verifyFormat("void f() {\n" 11316 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 11317 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n" 11318 "}"); 11319 verifyFormat( 11320 "(aaaaaaaaaa->*bbbbbbb)(\n" 11321 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 11322 11323 FormatStyle Style = getLLVMStyle(); 11324 EXPECT_EQ(Style.PointerAlignment, FormatStyle::PAS_Right); 11325 verifyFormat("typedef bool *(Class::*Member)() const;", Style); 11326 verifyFormat("void f(int A::*p) { int A::*v = &A::B; }", Style); 11327 11328 Style.PointerAlignment = FormatStyle::PAS_Left; 11329 verifyFormat("typedef bool* (Class::*Member)() const;", Style); 11330 verifyFormat("void f(int A::* p) { int A::* v = &A::B; }", Style); 11331 11332 Style.PointerAlignment = FormatStyle::PAS_Middle; 11333 verifyFormat("typedef bool * (Class::*Member)() const;", Style); 11334 verifyFormat("void f(int A::* p) { int A::* v = &A::B; }", Style); 11335 } 11336 11337 TEST_F(FormatTest, UnderstandsUnaryOperators) { 11338 verifyFormat("int a = -2;"); 11339 verifyFormat("f(-1, -2, -3);"); 11340 verifyFormat("a[-1] = 5;"); 11341 verifyFormat("int a = 5 + -2;"); 11342 verifyFormat("if (i == -1) {\n}"); 11343 verifyFormat("if (i != -1) {\n}"); 11344 verifyFormat("if (i > -1) {\n}"); 11345 verifyFormat("if (i < -1) {\n}"); 11346 verifyFormat("++(a->f());"); 11347 verifyFormat("--(a->f());"); 11348 verifyFormat("(a->f())++;"); 11349 verifyFormat("a[42]++;"); 11350 verifyFormat("if (!(a->f())) {\n}"); 11351 verifyFormat("if (!+i) {\n}"); 11352 verifyFormat("~&a;"); 11353 verifyFormat("for (x = 0; -10 < x; --x) {\n}"); 11354 verifyFormat("sizeof -x"); 11355 verifyFormat("sizeof +x"); 11356 verifyFormat("sizeof *x"); 11357 verifyFormat("sizeof &x"); 11358 verifyFormat("delete +x;"); 11359 verifyFormat("co_await +x;"); 11360 verifyFormat("case *x:"); 11361 verifyFormat("case &x:"); 11362 11363 verifyFormat("a-- > b;"); 11364 verifyFormat("b ? -a : c;"); 11365 verifyFormat("n * sizeof char16;"); 11366 verifyGoogleFormat("n * alignof char16;"); 11367 verifyFormat("sizeof(char);"); 11368 verifyGoogleFormat("alignof(char);"); 11369 11370 verifyFormat("return -1;"); 11371 verifyFormat("throw -1;"); 11372 verifyFormat("switch (a) {\n" 11373 "case -1:\n" 11374 " break;\n" 11375 "}"); 11376 verifyFormat("#define X -1"); 11377 verifyFormat("#define X -kConstant"); 11378 11379 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};"); 11380 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};"); 11381 11382 verifyFormat("int a = /* confusing comment */ -1;"); 11383 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 11384 verifyFormat("int a = i /* confusing comment */++;"); 11385 11386 verifyFormat("co_yield -1;"); 11387 verifyFormat("co_return -1;"); 11388 11389 // Check that * is not treated as a binary operator when we set 11390 // PointerAlignment as PAS_Left after a keyword and not a declaration. 11391 FormatStyle PASLeftStyle = getLLVMStyle(); 11392 PASLeftStyle.PointerAlignment = FormatStyle::PAS_Left; 11393 verifyFormat("co_return *a;", PASLeftStyle); 11394 verifyFormat("co_await *a;", PASLeftStyle); 11395 verifyFormat("co_yield *a", PASLeftStyle); 11396 verifyFormat("return *a;", PASLeftStyle); 11397 } 11398 11399 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) { 11400 verifyFormat("if (!aaaaaaaaaa( // break\n" 11401 " aaaaa)) {\n" 11402 "}"); 11403 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n" 11404 " aaaaa));"); 11405 verifyFormat("*aaa = aaaaaaa( // break\n" 11406 " bbbbbb);"); 11407 } 11408 11409 TEST_F(FormatTest, UnderstandsOverloadedOperators) { 11410 verifyFormat("bool operator<();"); 11411 verifyFormat("bool operator>();"); 11412 verifyFormat("bool operator=();"); 11413 verifyFormat("bool operator==();"); 11414 verifyFormat("bool operator!=();"); 11415 verifyFormat("int operator+();"); 11416 verifyFormat("int operator++();"); 11417 verifyFormat("int operator++(int) volatile noexcept;"); 11418 verifyFormat("bool operator,();"); 11419 verifyFormat("bool operator();"); 11420 verifyFormat("bool operator()();"); 11421 verifyFormat("bool operator[]();"); 11422 verifyFormat("operator bool();"); 11423 verifyFormat("operator int();"); 11424 verifyFormat("operator void *();"); 11425 verifyFormat("operator SomeType<int>();"); 11426 verifyFormat("operator SomeType<int, int>();"); 11427 verifyFormat("operator SomeType<SomeType<int>>();"); 11428 verifyFormat("operator< <>();"); 11429 verifyFormat("operator<< <>();"); 11430 verifyFormat("< <>"); 11431 11432 verifyFormat("void *operator new(std::size_t size);"); 11433 verifyFormat("void *operator new[](std::size_t size);"); 11434 verifyFormat("void operator delete(void *ptr);"); 11435 verifyFormat("void operator delete[](void *ptr);"); 11436 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n" 11437 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);"); 11438 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n" 11439 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;"); 11440 11441 verifyFormat( 11442 "ostream &operator<<(ostream &OutputStream,\n" 11443 " SomeReallyLongType WithSomeReallyLongValue);"); 11444 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n" 11445 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n" 11446 " return left.group < right.group;\n" 11447 "}"); 11448 verifyFormat("SomeType &operator=(const SomeType &S);"); 11449 verifyFormat("f.template operator()<int>();"); 11450 11451 verifyGoogleFormat("operator void*();"); 11452 verifyGoogleFormat("operator SomeType<SomeType<int>>();"); 11453 verifyGoogleFormat("operator ::A();"); 11454 11455 verifyFormat("using A::operator+;"); 11456 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n" 11457 "int i;"); 11458 11459 // Calling an operator as a member function. 11460 verifyFormat("void f() { a.operator*(); }"); 11461 verifyFormat("void f() { a.operator*(b & b); }"); 11462 verifyFormat("void f() { a->operator&(a * b); }"); 11463 verifyFormat("void f() { NS::a.operator+(*b * *b); }"); 11464 verifyFormat("void f() { operator*(a & a); }"); 11465 verifyFormat("void f() { operator&(a, b * b); }"); 11466 11467 verifyFormat("void f() { return operator()(x) * b; }"); 11468 verifyFormat("void f() { return operator[](x) * b; }"); 11469 verifyFormat("void f() { return operator\"\"_a(x) * b; }"); 11470 verifyFormat("void f() { return operator\"\" _a(x) * b; }"); 11471 verifyFormat("void f() { return operator\"\"s(x) * b; }"); 11472 verifyFormat("void f() { return operator\"\" s(x) * b; }"); 11473 verifyFormat("void f() { return operator\"\"if(x) * b; }"); 11474 11475 verifyFormat("::operator delete(foo);"); 11476 verifyFormat("::operator new(n * sizeof(foo));"); 11477 verifyFormat("foo() { ::operator delete(foo); }"); 11478 verifyFormat("foo() { ::operator new(n * sizeof(foo)); }"); 11479 } 11480 11481 TEST_F(FormatTest, SpaceBeforeTemplateCloser) { 11482 verifyFormat("C<&operator- > minus;"); 11483 verifyFormat("C<&operator> > gt;"); 11484 verifyFormat("C<&operator>= > ge;"); 11485 verifyFormat("C<&operator<= > le;"); 11486 verifyFormat("C<&operator< <X>> lt;"); 11487 } 11488 11489 TEST_F(FormatTest, UnderstandsFunctionRefQualification) { 11490 verifyFormat("void A::b() && {}"); 11491 verifyFormat("void A::b() && noexcept {}"); 11492 verifyFormat("Deleted &operator=(const Deleted &) & = default;"); 11493 verifyFormat("Deleted &operator=(const Deleted &) && = delete;"); 11494 verifyFormat("Deleted &operator=(const Deleted &) & noexcept = default;"); 11495 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;"); 11496 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;"); 11497 verifyFormat("Deleted &operator=(const Deleted &) &;"); 11498 verifyFormat("Deleted &operator=(const Deleted &) &&;"); 11499 verifyFormat("SomeType MemberFunction(const Deleted &) &;"); 11500 verifyFormat("SomeType MemberFunction(const Deleted &) &&;"); 11501 verifyFormat("SomeType MemberFunction(const Deleted &) && {}"); 11502 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}"); 11503 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}"); 11504 verifyFormat("SomeType MemberFunction(const Deleted &) && noexcept {}"); 11505 verifyFormat("void Fn(T const &) const &;"); 11506 verifyFormat("void Fn(T const volatile &&) const volatile &&;"); 11507 verifyFormat("void Fn(T const volatile &&) const volatile && noexcept;"); 11508 verifyGoogleFormat("template <typename T>\n" 11509 "void F(T) && = delete;"); 11510 verifyFormat("template <typename T> void operator=(T) &;"); 11511 verifyFormat("template <typename T> void operator=(T) const &;"); 11512 verifyFormat("template <typename T> void operator=(T) & noexcept;"); 11513 verifyFormat("template <typename T> void operator=(T) & = default;"); 11514 verifyFormat("template <typename T> void operator=(T) &&;"); 11515 verifyFormat("template <typename T> void operator=(T) && = delete;"); 11516 verifyFormat("template <typename T> void operator=(T) & {}"); 11517 verifyFormat("template <typename T> void operator=(T) && {}"); 11518 11519 FormatStyle AlignLeft = getLLVMStyle(); 11520 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 11521 verifyFormat("void A::b() && {}", AlignLeft); 11522 verifyFormat("void A::b() && noexcept {}", AlignLeft); 11523 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft); 11524 verifyFormat("Deleted& operator=(const Deleted&) & noexcept = default;", 11525 AlignLeft); 11526 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;", 11527 AlignLeft); 11528 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft); 11529 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft); 11530 verifyFormat("auto Function(T t) & -> void {}", AlignLeft); 11531 verifyFormat("auto Function(T... t) & -> void {}", AlignLeft); 11532 verifyFormat("auto Function(T) & -> void {}", AlignLeft); 11533 verifyFormat("auto Function(T) & -> void;", AlignLeft); 11534 verifyFormat("void Fn(T const&) const&;", AlignLeft); 11535 verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft); 11536 verifyFormat("void Fn(T const volatile&&) const volatile&& noexcept;", 11537 AlignLeft); 11538 verifyFormat("template <typename T> void operator=(T) &;", AlignLeft); 11539 verifyFormat("template <typename T> void operator=(T) const&;", AlignLeft); 11540 verifyFormat("template <typename T> void operator=(T) & noexcept;", 11541 AlignLeft); 11542 verifyFormat("template <typename T> void operator=(T) & = default;", 11543 AlignLeft); 11544 verifyFormat("template <typename T> void operator=(T) &&;", AlignLeft); 11545 verifyFormat("template <typename T> void operator=(T) && = delete;", 11546 AlignLeft); 11547 verifyFormat("template <typename T> void operator=(T) & {}", AlignLeft); 11548 verifyFormat("template <typename T> void operator=(T) && {}", AlignLeft); 11549 verifyFormat("for (foo<void() &&>& cb : X)", AlignLeft); 11550 11551 FormatStyle AlignMiddle = getLLVMStyle(); 11552 AlignMiddle.PointerAlignment = FormatStyle::PAS_Middle; 11553 verifyFormat("void A::b() && {}", AlignMiddle); 11554 verifyFormat("void A::b() && noexcept {}", AlignMiddle); 11555 verifyFormat("Deleted & operator=(const Deleted &) & = default;", 11556 AlignMiddle); 11557 verifyFormat("Deleted & operator=(const Deleted &) & noexcept = default;", 11558 AlignMiddle); 11559 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", 11560 AlignMiddle); 11561 verifyFormat("Deleted & operator=(const Deleted &) &;", AlignMiddle); 11562 verifyFormat("SomeType MemberFunction(const Deleted &) &;", AlignMiddle); 11563 verifyFormat("auto Function(T t) & -> void {}", AlignMiddle); 11564 verifyFormat("auto Function(T... t) & -> void {}", AlignMiddle); 11565 verifyFormat("auto Function(T) & -> void {}", AlignMiddle); 11566 verifyFormat("auto Function(T) & -> void;", AlignMiddle); 11567 verifyFormat("void Fn(T const &) const &;", AlignMiddle); 11568 verifyFormat("void Fn(T const volatile &&) const volatile &&;", AlignMiddle); 11569 verifyFormat("void Fn(T const volatile &&) const volatile && noexcept;", 11570 AlignMiddle); 11571 verifyFormat("template <typename T> void operator=(T) &;", AlignMiddle); 11572 verifyFormat("template <typename T> void operator=(T) const &;", AlignMiddle); 11573 verifyFormat("template <typename T> void operator=(T) & noexcept;", 11574 AlignMiddle); 11575 verifyFormat("template <typename T> void operator=(T) & = default;", 11576 AlignMiddle); 11577 verifyFormat("template <typename T> void operator=(T) &&;", AlignMiddle); 11578 verifyFormat("template <typename T> void operator=(T) && = delete;", 11579 AlignMiddle); 11580 verifyFormat("template <typename T> void operator=(T) & {}", AlignMiddle); 11581 verifyFormat("template <typename T> void operator=(T) && {}", AlignMiddle); 11582 11583 FormatStyle Spaces = getLLVMStyle(); 11584 Spaces.SpacesInParens = FormatStyle::SIPO_Custom; 11585 Spaces.SpacesInParensOptions = {}; 11586 Spaces.SpacesInParensOptions.InCStyleCasts = true; 11587 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces); 11588 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces); 11589 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces); 11590 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces); 11591 11592 Spaces.SpacesInParensOptions.InCStyleCasts = false; 11593 Spaces.SpacesInParensOptions.Other = true; 11594 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces); 11595 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", 11596 Spaces); 11597 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces); 11598 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces); 11599 11600 FormatStyle BreakTemplate = getLLVMStyle(); 11601 BreakTemplate.BreakTemplateDeclarations = FormatStyle::BTDS_Yes; 11602 11603 verifyFormat("struct f {\n" 11604 " template <class T>\n" 11605 " int &foo(const std::string &str) & noexcept {}\n" 11606 "};", 11607 BreakTemplate); 11608 11609 verifyFormat("struct f {\n" 11610 " template <class T>\n" 11611 " int &foo(const std::string &str) && noexcept {}\n" 11612 "};", 11613 BreakTemplate); 11614 11615 verifyFormat("struct f {\n" 11616 " template <class T>\n" 11617 " int &foo(const std::string &str) const & noexcept {}\n" 11618 "};", 11619 BreakTemplate); 11620 11621 verifyFormat("struct f {\n" 11622 " template <class T>\n" 11623 " int &foo(const std::string &str) const & noexcept {}\n" 11624 "};", 11625 BreakTemplate); 11626 11627 verifyFormat("struct f {\n" 11628 " template <class T>\n" 11629 " auto foo(const std::string &str) && noexcept -> int & {}\n" 11630 "};", 11631 BreakTemplate); 11632 11633 FormatStyle AlignLeftBreakTemplate = getLLVMStyle(); 11634 AlignLeftBreakTemplate.BreakTemplateDeclarations = FormatStyle::BTDS_Yes; 11635 AlignLeftBreakTemplate.PointerAlignment = FormatStyle::PAS_Left; 11636 11637 verifyFormat("struct f {\n" 11638 " template <class T>\n" 11639 " int& foo(const std::string& str) & noexcept {}\n" 11640 "};", 11641 AlignLeftBreakTemplate); 11642 11643 verifyFormat("struct f {\n" 11644 " template <class T>\n" 11645 " int& foo(const std::string& str) && noexcept {}\n" 11646 "};", 11647 AlignLeftBreakTemplate); 11648 11649 verifyFormat("struct f {\n" 11650 " template <class T>\n" 11651 " int& foo(const std::string& str) const& noexcept {}\n" 11652 "};", 11653 AlignLeftBreakTemplate); 11654 11655 verifyFormat("struct f {\n" 11656 " template <class T>\n" 11657 " int& foo(const std::string& str) const&& noexcept {}\n" 11658 "};", 11659 AlignLeftBreakTemplate); 11660 11661 verifyFormat("struct f {\n" 11662 " template <class T>\n" 11663 " auto foo(const std::string& str) && noexcept -> int& {}\n" 11664 "};", 11665 AlignLeftBreakTemplate); 11666 11667 // The `&` in `Type&` should not be confused with a trailing `&` of 11668 // DEPRECATED(reason) member function. 11669 verifyFormat("struct f {\n" 11670 " template <class T>\n" 11671 " DEPRECATED(reason)\n" 11672 " Type &foo(arguments) {}\n" 11673 "};", 11674 BreakTemplate); 11675 11676 verifyFormat("struct f {\n" 11677 " template <class T>\n" 11678 " DEPRECATED(reason)\n" 11679 " Type& foo(arguments) {}\n" 11680 "};", 11681 AlignLeftBreakTemplate); 11682 11683 verifyFormat("void (*foopt)(int) = &func;"); 11684 11685 FormatStyle DerivePointerAlignment = getLLVMStyle(); 11686 DerivePointerAlignment.DerivePointerAlignment = true; 11687 // There's always a space between the function and its trailing qualifiers. 11688 // This isn't evidence for PAS_Right (or for PAS_Left). 11689 std::string Prefix = "void a() &;\n" 11690 "void b() &;\n"; 11691 verifyFormat(Prefix + "int* x;", DerivePointerAlignment); 11692 verifyFormat(Prefix + "int *x;", DerivePointerAlignment); 11693 // Same if the function is an overloaded operator, and with &&. 11694 Prefix = "void operator()() &&;\n" 11695 "void operator()() &&;\n"; 11696 verifyFormat(Prefix + "int* x;", DerivePointerAlignment); 11697 verifyFormat(Prefix + "int *x;", DerivePointerAlignment); 11698 // However a space between cv-qualifiers and ref-qualifiers *is* evidence. 11699 Prefix = "void a() const &;\n" 11700 "void b() const &;\n"; 11701 verifyFormat(Prefix + "int *x;", Prefix + "int* x;", DerivePointerAlignment); 11702 } 11703 11704 TEST_F(FormatTest, PointerAlignmentFallback) { 11705 FormatStyle Style = getLLVMStyle(); 11706 Style.DerivePointerAlignment = true; 11707 11708 const StringRef Code("int* p;\n" 11709 "int *q;\n" 11710 "int * r;"); 11711 11712 EXPECT_EQ(Style.PointerAlignment, FormatStyle::PAS_Right); 11713 verifyFormat("int *p;\n" 11714 "int *q;\n" 11715 "int *r;", 11716 Code, Style); 11717 11718 Style.PointerAlignment = FormatStyle::PAS_Left; 11719 verifyFormat("int* p;\n" 11720 "int* q;\n" 11721 "int* r;", 11722 Code, Style); 11723 11724 Style.PointerAlignment = FormatStyle::PAS_Middle; 11725 verifyFormat("int * p;\n" 11726 "int * q;\n" 11727 "int * r;", 11728 Code, Style); 11729 } 11730 11731 TEST_F(FormatTest, UnderstandsNewAndDelete) { 11732 verifyFormat("void f() {\n" 11733 " A *a = new A;\n" 11734 " A *a = new (placement) A;\n" 11735 " delete a;\n" 11736 " delete (A *)a;\n" 11737 "}"); 11738 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 11739 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 11740 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 11741 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 11742 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 11743 verifyFormat("delete[] h->p;"); 11744 verifyFormat("delete[] (void *)p;"); 11745 11746 verifyFormat("void operator delete(void *foo) ATTRIB;"); 11747 verifyFormat("void operator new(void *foo) ATTRIB;"); 11748 verifyFormat("void operator delete[](void *foo) ATTRIB;"); 11749 verifyFormat("void operator delete(void *ptr) noexcept;"); 11750 11751 verifyFormat("void new(link p);\n" 11752 "void delete(link p);", 11753 "void new (link p);\n" 11754 "void delete (link p);"); 11755 11756 verifyFormat("{\n" 11757 " p->new();\n" 11758 "}\n" 11759 "{\n" 11760 " p->delete();\n" 11761 "}", 11762 "{\n" 11763 " p->new ();\n" 11764 "}\n" 11765 "{\n" 11766 " p->delete ();\n" 11767 "}"); 11768 11769 FormatStyle AfterPlacementOperator = getLLVMStyle(); 11770 AfterPlacementOperator.SpaceBeforeParens = FormatStyle::SBPO_Custom; 11771 EXPECT_TRUE( 11772 AfterPlacementOperator.SpaceBeforeParensOptions.AfterPlacementOperator); 11773 verifyFormat("new (buf) int;", AfterPlacementOperator); 11774 verifyFormat("struct A {\n" 11775 " int *a;\n" 11776 " A(int *p) : a(new (p) int) {\n" 11777 " new (p) int;\n" 11778 " int *b = new (p) int;\n" 11779 " int *c = new (p) int(3);\n" 11780 " delete (b);\n" 11781 " }\n" 11782 "};", 11783 AfterPlacementOperator); 11784 verifyFormat("void operator new(void *foo) ATTRIB;", AfterPlacementOperator); 11785 verifyFormat("delete (int *)p;", AfterPlacementOperator); 11786 11787 AfterPlacementOperator.SpaceBeforeParensOptions.AfterPlacementOperator = 11788 false; 11789 verifyFormat("new(buf) int;", AfterPlacementOperator); 11790 verifyFormat("struct A {\n" 11791 " int *a;\n" 11792 " A(int *p) : a(new(p) int) {\n" 11793 " new(p) int;\n" 11794 " int *b = new(p) int;\n" 11795 " int *c = new(p) int(3);\n" 11796 " delete(b);\n" 11797 " }\n" 11798 "};", 11799 AfterPlacementOperator); 11800 verifyFormat("void operator new(void *foo) ATTRIB;", AfterPlacementOperator); 11801 verifyFormat("delete (int *)p;", AfterPlacementOperator); 11802 } 11803 11804 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 11805 verifyFormat("int *f(int *a) {}"); 11806 verifyFormat("int main(int argc, char **argv) {}"); 11807 verifyFormat("Test::Test(int b) : a(b * b) {}"); 11808 verifyIndependentOfContext("f(a, *a);"); 11809 verifyFormat("void g() { f(*a); }"); 11810 verifyIndependentOfContext("int a = b * 10;"); 11811 verifyIndependentOfContext("int a = 10 * b;"); 11812 verifyIndependentOfContext("int a = b * c;"); 11813 verifyIndependentOfContext("int a += b * c;"); 11814 verifyIndependentOfContext("int a -= b * c;"); 11815 verifyIndependentOfContext("int a *= b * c;"); 11816 verifyIndependentOfContext("int a /= b * c;"); 11817 verifyIndependentOfContext("int a = *b;"); 11818 verifyIndependentOfContext("int a = *b * c;"); 11819 verifyIndependentOfContext("int a = b * *c;"); 11820 verifyIndependentOfContext("int a = b * (10);"); 11821 verifyIndependentOfContext("S << b * (10);"); 11822 verifyIndependentOfContext("return 10 * b;"); 11823 verifyIndependentOfContext("return *b * *c;"); 11824 verifyIndependentOfContext("return a & ~b;"); 11825 verifyIndependentOfContext("f(b ? *c : *d);"); 11826 verifyIndependentOfContext("int a = b ? *c : *d;"); 11827 verifyIndependentOfContext("*b = a;"); 11828 verifyIndependentOfContext("a * ~b;"); 11829 verifyIndependentOfContext("a * !b;"); 11830 verifyIndependentOfContext("a * +b;"); 11831 verifyIndependentOfContext("a * -b;"); 11832 verifyIndependentOfContext("a * ++b;"); 11833 verifyIndependentOfContext("a * --b;"); 11834 verifyIndependentOfContext("a[4] * b;"); 11835 verifyIndependentOfContext("a[a * a] = 1;"); 11836 verifyIndependentOfContext("f() * b;"); 11837 verifyIndependentOfContext("a * [self dostuff];"); 11838 verifyIndependentOfContext("int x = a * (a + b);"); 11839 verifyIndependentOfContext("(a *)(a + b);"); 11840 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;"); 11841 verifyIndependentOfContext("int *pa = (int *)&a;"); 11842 verifyIndependentOfContext("return sizeof(int **);"); 11843 verifyIndependentOfContext("return sizeof(int ******);"); 11844 verifyIndependentOfContext("return (int **&)a;"); 11845 verifyIndependentOfContext("f((*PointerToArray)[10]);"); 11846 verifyFormat("void f(Type (*parameter)[10]) {}"); 11847 verifyFormat("void f(Type (¶meter)[10]) {}"); 11848 verifyGoogleFormat("return sizeof(int**);"); 11849 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); 11850 verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); 11851 verifyFormat("auto a = [](int **&, int ***) {};"); 11852 verifyFormat("auto PointerBinding = [](const char *S) {};"); 11853 verifyFormat("typedef typeof(int(int, int)) *MyFunc;"); 11854 verifyFormat("[](const decltype(*a) &value) {}"); 11855 verifyFormat("[](const typeof(*a) &value) {}"); 11856 verifyFormat("[](const _Atomic(a *) &value) {}"); 11857 verifyFormat("[](const __underlying_type(a) &value) {}"); 11858 verifyFormat("decltype(a * b) F();"); 11859 verifyFormat("typeof(a * b) F();"); 11860 verifyFormat("#define MACRO() [](A *a) { return 1; }"); 11861 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}"); 11862 verifyIndependentOfContext("typedef void (*f)(int *a);"); 11863 verifyIndependentOfContext("typedef void (*f)(Type *a);"); 11864 verifyIndependentOfContext("int i{a * b};"); 11865 verifyIndependentOfContext("aaa && aaa->f();"); 11866 verifyIndependentOfContext("int x = ~*p;"); 11867 verifyFormat("Constructor() : a(a), area(width * height) {}"); 11868 verifyFormat("Constructor() : a(a), area(a, width * height) {}"); 11869 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}"); 11870 verifyFormat("void f() { f(a, c * d); }"); 11871 verifyFormat("void f() { f(new a(), c * d); }"); 11872 verifyFormat("void f(const MyOverride &override);"); 11873 verifyFormat("void f(const MyFinal &final);"); 11874 verifyIndependentOfContext("bool a = f() && override.f();"); 11875 verifyIndependentOfContext("bool a = f() && final.f();"); 11876 11877 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 11878 11879 verifyIndependentOfContext("A<int *> a;"); 11880 verifyIndependentOfContext("A<int **> a;"); 11881 verifyIndependentOfContext("A<int *, int *> a;"); 11882 verifyIndependentOfContext("A<int *[]> a;"); 11883 verifyIndependentOfContext( 11884 "const char *const p = reinterpret_cast<const char *const>(q);"); 11885 verifyIndependentOfContext("A<int **, int **> a;"); 11886 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); 11887 verifyFormat("for (char **a = b; *a; ++a) {\n}"); 11888 verifyFormat("for (; a && b;) {\n}"); 11889 verifyFormat("bool foo = true && [] { return false; }();"); 11890 11891 verifyFormat( 11892 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 11893 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 11894 11895 verifyGoogleFormat("int const* a = &b;"); 11896 verifyGoogleFormat("**outparam = 1;"); 11897 verifyGoogleFormat("*outparam = a * b;"); 11898 verifyGoogleFormat("int main(int argc, char** argv) {}"); 11899 verifyGoogleFormat("A<int*> a;"); 11900 verifyGoogleFormat("A<int**> a;"); 11901 verifyGoogleFormat("A<int*, int*> a;"); 11902 verifyGoogleFormat("A<int**, int**> a;"); 11903 verifyGoogleFormat("f(b ? *c : *d);"); 11904 verifyGoogleFormat("int a = b ? *c : *d;"); 11905 verifyGoogleFormat("Type* t = **x;"); 11906 verifyGoogleFormat("Type* t = *++*x;"); 11907 verifyGoogleFormat("*++*x;"); 11908 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 11909 verifyGoogleFormat("Type* t = x++ * y;"); 11910 verifyGoogleFormat( 11911 "const char* const p = reinterpret_cast<const char* const>(q);"); 11912 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);"); 11913 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);"); 11914 verifyGoogleFormat("template <typename T>\n" 11915 "void f(int i = 0, SomeType** temps = NULL);"); 11916 11917 FormatStyle Left = getLLVMStyle(); 11918 Left.PointerAlignment = FormatStyle::PAS_Left; 11919 verifyFormat("x = *a(x) = *a(y);", Left); 11920 verifyFormat("for (;; *a = b) {\n}", Left); 11921 verifyFormat("return *this += 1;", Left); 11922 verifyFormat("throw *x;", Left); 11923 verifyFormat("delete *x;", Left); 11924 verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left); 11925 verifyFormat("[](const decltype(*a)* ptr) {}", Left); 11926 verifyFormat("[](const typeof(*a)* ptr) {}", Left); 11927 verifyFormat("[](const _Atomic(a*)* ptr) {}", Left); 11928 verifyFormat("[](const __underlying_type(a)* ptr) {}", Left); 11929 verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left); 11930 verifyFormat("auto x(A&&, B&&, C&&) -> D;", Left); 11931 verifyFormat("auto x = [](A&&, B&&, C&&) -> D {};", Left); 11932 verifyFormat("template <class T> X(T&&, T&&, T&&) -> X<T>;", Left); 11933 11934 verifyIndependentOfContext("a = *(x + y);"); 11935 verifyIndependentOfContext("a = &(x + y);"); 11936 verifyIndependentOfContext("*(x + y).call();"); 11937 verifyIndependentOfContext("&(x + y)->call();"); 11938 verifyFormat("void f() { &(*I).first; }"); 11939 11940 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 11941 verifyFormat("f(* /* confusing comment */ foo);"); 11942 verifyFormat("void (* /*deleter*/)(const Slice &key, void *value)"); 11943 verifyFormat("void foo(int * // this is the first paramters\n" 11944 " ,\n" 11945 " int second);"); 11946 verifyFormat("double term = a * // first\n" 11947 " b;"); 11948 verifyFormat( 11949 "int *MyValues = {\n" 11950 " *A, // Operator detection might be confused by the '{'\n" 11951 " *BB // Operator detection might be confused by previous comment\n" 11952 "};"); 11953 11954 verifyIndependentOfContext("if (int *a = &b)"); 11955 verifyIndependentOfContext("if (int &a = *b)"); 11956 verifyIndependentOfContext("if (a & b[i])"); 11957 verifyIndependentOfContext("if constexpr (a & b[i])"); 11958 verifyIndependentOfContext("if CONSTEXPR (a & b[i])"); 11959 verifyIndependentOfContext("if (a * (b * c))"); 11960 verifyIndependentOfContext("if constexpr (a * (b * c))"); 11961 verifyIndependentOfContext("if CONSTEXPR (a * (b * c))"); 11962 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 11963 verifyIndependentOfContext("if (*b[i])"); 11964 verifyIndependentOfContext("if (int *a = (&b))"); 11965 verifyIndependentOfContext("while (int *a = &b)"); 11966 verifyIndependentOfContext("while (a * (b * c))"); 11967 verifyIndependentOfContext("size = sizeof *a;"); 11968 verifyIndependentOfContext("if (a && (b = c))"); 11969 verifyFormat("void f() {\n" 11970 " for (const int &v : Values) {\n" 11971 " }\n" 11972 "}"); 11973 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); 11974 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); 11975 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}"); 11976 11977 verifyFormat("#define A (!a * b)"); 11978 verifyFormat("#define MACRO \\\n" 11979 " int *i = a * b; \\\n" 11980 " void f(a *b);", 11981 getLLVMStyleWithColumns(19)); 11982 11983 verifyIndependentOfContext("A = new SomeType *[Length];"); 11984 verifyIndependentOfContext("A = new SomeType *[Length]();"); 11985 verifyIndependentOfContext("T **t = new T *;"); 11986 verifyIndependentOfContext("T **t = new T *();"); 11987 verifyGoogleFormat("A = new SomeType*[Length]();"); 11988 verifyGoogleFormat("A = new SomeType*[Length];"); 11989 verifyGoogleFormat("T** t = new T*;"); 11990 verifyGoogleFormat("T** t = new T*();"); 11991 11992 verifyFormat("STATIC_ASSERT((a & b) == 0);"); 11993 verifyFormat("STATIC_ASSERT(0 == (a & b));"); 11994 verifyFormat("template <bool a, bool b> " 11995 "typename t::if<x && y>::type f() {}"); 11996 verifyFormat("template <int *y> f() {}"); 11997 verifyFormat("vector<int *> v;"); 11998 verifyFormat("vector<int *const> v;"); 11999 verifyFormat("vector<int *const **const *> v;"); 12000 verifyFormat("vector<int *volatile> v;"); 12001 verifyFormat("vector<a *_Nonnull> v;"); 12002 verifyFormat("vector<a *_Nullable> v;"); 12003 verifyFormat("vector<a *_Null_unspecified> v;"); 12004 verifyFormat("vector<a *__ptr32> v;"); 12005 verifyFormat("vector<a *__ptr64> v;"); 12006 verifyFormat("vector<a *__capability> v;"); 12007 FormatStyle TypeMacros = getLLVMStyle(); 12008 TypeMacros.TypenameMacros = {"LIST"}; 12009 verifyFormat("vector<LIST(uint64_t)> v;", TypeMacros); 12010 verifyFormat("vector<LIST(uint64_t) *> v;", TypeMacros); 12011 verifyFormat("vector<LIST(uint64_t) **> v;", TypeMacros); 12012 verifyFormat("vector<LIST(uint64_t) *attr> v;", TypeMacros); 12013 verifyFormat("vector<A(uint64_t) * attr> v;", TypeMacros); // multiplication 12014 12015 FormatStyle CustomQualifier = getLLVMStyle(); 12016 // Add identifiers that should not be parsed as a qualifier by default. 12017 CustomQualifier.AttributeMacros.push_back("__my_qualifier"); 12018 CustomQualifier.AttributeMacros.push_back("_My_qualifier"); 12019 CustomQualifier.AttributeMacros.push_back("my_other_qualifier"); 12020 verifyFormat("vector<a * __my_qualifier> parse_as_multiply;"); 12021 verifyFormat("vector<a *__my_qualifier> v;", CustomQualifier); 12022 verifyFormat("vector<a * _My_qualifier> parse_as_multiply;"); 12023 verifyFormat("vector<a *_My_qualifier> v;", CustomQualifier); 12024 verifyFormat("vector<a * my_other_qualifier> parse_as_multiply;"); 12025 verifyFormat("vector<a *my_other_qualifier> v;", CustomQualifier); 12026 verifyFormat("vector<a * _NotAQualifier> v;"); 12027 verifyFormat("vector<a * __not_a_qualifier> v;"); 12028 verifyFormat("vector<a * b> v;"); 12029 verifyFormat("foo<b && false>();"); 12030 verifyFormat("foo<b & 1>();"); 12031 verifyFormat("foo<b & (1)>();"); 12032 verifyFormat("foo<b & (~0)>();"); 12033 verifyFormat("foo<b & (true)>();"); 12034 verifyFormat("foo<b & ((1))>();"); 12035 verifyFormat("foo<b & (/*comment*/ 1)>();"); 12036 verifyFormat("decltype(*::std::declval<const T &>()) void F();"); 12037 verifyFormat("typeof(*::std::declval<const T &>()) void F();"); 12038 verifyFormat("_Atomic(*::std::declval<const T &>()) void F();"); 12039 verifyFormat("__underlying_type(*::std::declval<const T &>()) void F();"); 12040 verifyFormat( 12041 "template <class T, class = typename std::enable_if<\n" 12042 " std::is_integral<T>::value &&\n" 12043 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n" 12044 "void F();", 12045 getLLVMStyleWithColumns(70)); 12046 verifyFormat("template <class T,\n" 12047 " class = typename std::enable_if<\n" 12048 " std::is_integral<T>::value &&\n" 12049 " (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n" 12050 " class U>\n" 12051 "void F();", 12052 getLLVMStyleWithColumns(70)); 12053 verifyFormat( 12054 "template <class T,\n" 12055 " class = typename ::std::enable_if<\n" 12056 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n" 12057 "void F();", 12058 getGoogleStyleWithColumns(68)); 12059 12060 FormatStyle Style = getLLVMStyle(); 12061 Style.PointerAlignment = FormatStyle::PAS_Left; 12062 verifyFormat("struct {\n" 12063 "}* ptr;", 12064 Style); 12065 verifyFormat("union {\n" 12066 "}* ptr;", 12067 Style); 12068 verifyFormat("class {\n" 12069 "}* ptr;", 12070 Style); 12071 // Don't confuse a multiplication after a brace-initialized expression with 12072 // a class pointer. 12073 verifyFormat("int i = int{42} * 34;", Style); 12074 verifyFormat("struct {\n" 12075 "}&& ptr = {};", 12076 Style); 12077 verifyFormat("union {\n" 12078 "}&& ptr = {};", 12079 Style); 12080 verifyFormat("class {\n" 12081 "}&& ptr = {};", 12082 Style); 12083 verifyFormat("bool b = 3 == int{3} && true;"); 12084 12085 Style.PointerAlignment = FormatStyle::PAS_Middle; 12086 verifyFormat("struct {\n" 12087 "} * ptr;", 12088 Style); 12089 verifyFormat("union {\n" 12090 "} * ptr;", 12091 Style); 12092 verifyFormat("class {\n" 12093 "} * ptr;", 12094 Style); 12095 verifyFormat("struct {\n" 12096 "} && ptr = {};", 12097 Style); 12098 verifyFormat("union {\n" 12099 "} && ptr = {};", 12100 Style); 12101 verifyFormat("class {\n" 12102 "} && ptr = {};", 12103 Style); 12104 12105 Style.PointerAlignment = FormatStyle::PAS_Right; 12106 verifyFormat("struct {\n" 12107 "} *ptr;", 12108 Style); 12109 verifyFormat("union {\n" 12110 "} *ptr;", 12111 Style); 12112 verifyFormat("class {\n" 12113 "} *ptr;", 12114 Style); 12115 verifyFormat("struct {\n" 12116 "} &&ptr = {};", 12117 Style); 12118 verifyFormat("union {\n" 12119 "} &&ptr = {};", 12120 Style); 12121 verifyFormat("class {\n" 12122 "} &&ptr = {};", 12123 Style); 12124 12125 Style.PointerAlignment = FormatStyle::PAS_Left; 12126 verifyFormat("delete[] *ptr;", Style); 12127 verifyFormat("delete[] **ptr;", Style); 12128 verifyFormat("delete[] *(ptr);", Style); 12129 12130 verifyIndependentOfContext("MACRO(int *i);"); 12131 verifyIndependentOfContext("MACRO(auto *a);"); 12132 verifyIndependentOfContext("MACRO(const A *a);"); 12133 verifyIndependentOfContext("MACRO(_Atomic(A) *a);"); 12134 verifyIndependentOfContext("MACRO(decltype(A) *a);"); 12135 verifyIndependentOfContext("MACRO(typeof(A) *a);"); 12136 verifyIndependentOfContext("MACRO(__underlying_type(A) *a);"); 12137 verifyIndependentOfContext("MACRO(A *const a);"); 12138 verifyIndependentOfContext("MACRO(A *restrict a);"); 12139 verifyIndependentOfContext("MACRO(A *__restrict__ a);"); 12140 verifyIndependentOfContext("MACRO(A *__restrict a);"); 12141 verifyIndependentOfContext("MACRO(A *volatile a);"); 12142 verifyIndependentOfContext("MACRO(A *__volatile a);"); 12143 verifyIndependentOfContext("MACRO(A *__volatile__ a);"); 12144 verifyIndependentOfContext("MACRO(A *_Nonnull a);"); 12145 verifyIndependentOfContext("MACRO(A *_Nullable a);"); 12146 verifyIndependentOfContext("MACRO(A *_Null_unspecified a);"); 12147 verifyIndependentOfContext("MACRO(A *__attribute__((foo)) a);"); 12148 verifyIndependentOfContext("MACRO(A *__attribute((foo)) a);"); 12149 verifyIndependentOfContext("MACRO(A *[[clang::attr]] a);"); 12150 verifyIndependentOfContext("MACRO(A *[[clang::attr(\"foo\")]] a);"); 12151 verifyIndependentOfContext("MACRO(A *__ptr32 a);"); 12152 verifyIndependentOfContext("MACRO(A *__ptr64 a);"); 12153 verifyIndependentOfContext("MACRO(A *__capability);"); 12154 verifyIndependentOfContext("MACRO(A &__capability);"); 12155 verifyFormat("MACRO(A *__my_qualifier);"); // type declaration 12156 verifyFormat("void f() { MACRO(A * __my_qualifier); }"); // multiplication 12157 // If we add __my_qualifier to AttributeMacros it should always be parsed as 12158 // a type declaration: 12159 verifyFormat("MACRO(A *__my_qualifier);", CustomQualifier); 12160 verifyFormat("void f() { MACRO(A *__my_qualifier); }", CustomQualifier); 12161 // Also check that TypenameMacros prevents parsing it as multiplication: 12162 verifyIndependentOfContext("MACRO(LIST(uint64_t) * a);"); // multiplication 12163 verifyIndependentOfContext("MACRO(LIST(uint64_t) *a);", TypeMacros); // type 12164 12165 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');"); 12166 verifyFormat("void f() { f(float{1}, a * a); }"); 12167 verifyFormat("void f() { f(float(1), a * a); }"); 12168 12169 verifyFormat("f((void (*)(int))g);"); 12170 verifyFormat("f((void (&)(int))g);"); 12171 verifyFormat("f((void (^)(int))g);"); 12172 12173 // FIXME: Is there a way to make this work? 12174 // verifyIndependentOfContext("MACRO(A *a);"); 12175 verifyFormat("MACRO(A &B);"); 12176 verifyFormat("MACRO(A *B);"); 12177 verifyFormat("void f() { MACRO(A * B); }"); 12178 verifyFormat("void f() { MACRO(A & B); }"); 12179 12180 // This lambda was mis-formatted after D88956 (treating it as a binop): 12181 verifyFormat("auto x = [](const decltype(x) &ptr) {};"); 12182 verifyFormat("auto x = [](const decltype(x) *ptr) {};"); 12183 verifyFormat("#define lambda [](const decltype(x) &ptr) {}"); 12184 verifyFormat("#define lambda [](const decltype(x) *ptr) {}"); 12185 12186 verifyFormat("DatumHandle const *operator->() const { return input_; }"); 12187 verifyFormat("return options != nullptr && operator==(*options);"); 12188 12189 verifyFormat("#define OP(x) \\\n" 12190 " ostream &operator<<(ostream &s, const A &a) { \\\n" 12191 " return s << a.DebugString(); \\\n" 12192 " }", 12193 "#define OP(x) \\\n" 12194 " ostream &operator<<(ostream &s, const A &a) { \\\n" 12195 " return s << a.DebugString(); \\\n" 12196 " }", 12197 getLLVMStyleWithColumns(50)); 12198 12199 verifyFormat("#define FOO \\\n" 12200 " void foo() { \\\n" 12201 " operator+(a * b); \\\n" 12202 " }", 12203 getLLVMStyleWithColumns(25)); 12204 12205 // FIXME: We cannot handle this case yet; we might be able to figure out that 12206 // foo<x> d > v; doesn't make sense. 12207 verifyFormat("foo<a<b && c> d> v;"); 12208 12209 FormatStyle PointerMiddle = getLLVMStyle(); 12210 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 12211 verifyFormat("delete *x;", PointerMiddle); 12212 verifyFormat("int * x;", PointerMiddle); 12213 verifyFormat("int *[] x;", PointerMiddle); 12214 verifyFormat("template <int * y> f() {}", PointerMiddle); 12215 verifyFormat("int * f(int * a) {}", PointerMiddle); 12216 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle); 12217 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle); 12218 verifyFormat("A<int *> a;", PointerMiddle); 12219 verifyFormat("A<int **> a;", PointerMiddle); 12220 verifyFormat("A<int *, int *> a;", PointerMiddle); 12221 verifyFormat("A<int *[]> a;", PointerMiddle); 12222 verifyFormat("A = new SomeType *[Length]();", PointerMiddle); 12223 verifyFormat("A = new SomeType *[Length];", PointerMiddle); 12224 verifyFormat("T ** t = new T *;", PointerMiddle); 12225 12226 // Member function reference qualifiers aren't binary operators. 12227 verifyFormat("string // break\n" 12228 "operator()() & {}"); 12229 verifyFormat("string // break\n" 12230 "operator()() && {}"); 12231 verifyGoogleFormat("template <typename T>\n" 12232 "auto x() & -> int {}"); 12233 12234 // Should be binary operators when used as an argument expression (overloaded 12235 // operator invoked as a member function). 12236 verifyFormat("void f() { a.operator()(a * a); }"); 12237 verifyFormat("void f() { a->operator()(a & a); }"); 12238 verifyFormat("void f() { a.operator()(*a & *a); }"); 12239 verifyFormat("void f() { a->operator()(*a * *a); }"); 12240 12241 verifyFormat("int operator()(T (&&)[N]) { return 1; }"); 12242 verifyFormat("int operator()(T (&)[N]) { return 0; }"); 12243 12244 verifyFormat("val1 & val2;"); 12245 verifyFormat("val1 & val2 & val3;"); 12246 verifyFormat("class c {\n" 12247 " void func(type &a) { a & member; }\n" 12248 " anotherType &member;\n" 12249 "}"); 12250 } 12251 12252 TEST_F(FormatTest, UnderstandsAttributes) { 12253 verifyFormat("SomeType s __attribute__((unused)) (InitValue);"); 12254 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n" 12255 "aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 12256 verifyFormat("__attribute__((nodebug)) ::qualified_type f();"); 12257 FormatStyle AfterType = getLLVMStyle(); 12258 AfterType.BreakAfterReturnType = FormatStyle::RTBS_All; 12259 verifyFormat("__attribute__((nodebug)) void\n" 12260 "foo() {}", 12261 AfterType); 12262 verifyFormat("__unused void\n" 12263 "foo() {}", 12264 AfterType); 12265 12266 FormatStyle CustomAttrs = getLLVMStyle(); 12267 CustomAttrs.AttributeMacros.push_back("__unused"); 12268 CustomAttrs.AttributeMacros.push_back("__attr1"); 12269 CustomAttrs.AttributeMacros.push_back("__attr2"); 12270 CustomAttrs.AttributeMacros.push_back("no_underscore_attr"); 12271 verifyFormat("vector<SomeType *__attribute((foo))> v;"); 12272 verifyFormat("vector<SomeType *__attribute__((foo))> v;"); 12273 verifyFormat("vector<SomeType * __not_attribute__((foo))> v;"); 12274 // Check that it is parsed as a multiplication without AttributeMacros and 12275 // as a pointer qualifier when we add __attr1/__attr2 to AttributeMacros. 12276 verifyFormat("vector<SomeType * __attr1> v;"); 12277 verifyFormat("vector<SomeType __attr1 *> v;"); 12278 verifyFormat("vector<SomeType __attr1 *const> v;"); 12279 verifyFormat("vector<SomeType __attr1 * __attr2> v;"); 12280 verifyFormat("vector<SomeType *__attr1> v;", CustomAttrs); 12281 verifyFormat("vector<SomeType *__attr2> v;", CustomAttrs); 12282 verifyFormat("vector<SomeType *no_underscore_attr> v;", CustomAttrs); 12283 verifyFormat("vector<SomeType __attr1 *> v;", CustomAttrs); 12284 verifyFormat("vector<SomeType __attr1 *const> v;", CustomAttrs); 12285 verifyFormat("vector<SomeType __attr1 *__attr2> v;", CustomAttrs); 12286 verifyFormat("vector<SomeType __attr1 *no_underscore_attr> v;", CustomAttrs); 12287 verifyFormat("__attr1 ::qualified_type f();", CustomAttrs); 12288 verifyFormat("__attr1() ::qualified_type f();", CustomAttrs); 12289 verifyFormat("__attr1(nodebug) ::qualified_type f();", CustomAttrs); 12290 12291 // Check that these are not parsed as function declarations: 12292 CustomAttrs.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 12293 CustomAttrs.BreakBeforeBraces = FormatStyle::BS_Allman; 12294 verifyFormat("SomeType s(InitValue);", CustomAttrs); 12295 verifyFormat("SomeType s{InitValue};", CustomAttrs); 12296 verifyFormat("SomeType *__unused s(InitValue);", CustomAttrs); 12297 verifyFormat("SomeType *__unused s{InitValue};", CustomAttrs); 12298 verifyFormat("SomeType s __unused(InitValue);", CustomAttrs); 12299 verifyFormat("SomeType s __unused{InitValue};", CustomAttrs); 12300 verifyFormat("SomeType *__capability s(InitValue);", CustomAttrs); 12301 verifyFormat("SomeType *__capability s{InitValue};", CustomAttrs); 12302 } 12303 12304 TEST_F(FormatTest, UnderstandsPointerQualifiersInCast) { 12305 // Check that qualifiers on pointers don't break parsing of casts. 12306 verifyFormat("x = (foo *const)*v;"); 12307 verifyFormat("x = (foo *volatile)*v;"); 12308 verifyFormat("x = (foo *restrict)*v;"); 12309 verifyFormat("x = (foo *__attribute__((foo)))*v;"); 12310 verifyFormat("x = (foo *_Nonnull)*v;"); 12311 verifyFormat("x = (foo *_Nullable)*v;"); 12312 verifyFormat("x = (foo *_Null_unspecified)*v;"); 12313 verifyFormat("x = (foo *_Nonnull)*v;"); 12314 verifyFormat("x = (foo *[[clang::attr]])*v;"); 12315 verifyFormat("x = (foo *[[clang::attr(\"foo\")]])*v;"); 12316 verifyFormat("x = (foo *__ptr32)*v;"); 12317 verifyFormat("x = (foo *__ptr64)*v;"); 12318 verifyFormat("x = (foo *__capability)*v;"); 12319 12320 // Check that we handle multiple trailing qualifiers and skip them all to 12321 // determine that the expression is a cast to a pointer type. 12322 FormatStyle LongPointerRight = getLLVMStyleWithColumns(999); 12323 FormatStyle LongPointerLeft = getLLVMStyleWithColumns(999); 12324 LongPointerLeft.PointerAlignment = FormatStyle::PAS_Left; 12325 StringRef AllQualifiers = 12326 "const volatile restrict __attribute__((foo)) _Nonnull _Null_unspecified " 12327 "_Nonnull [[clang::attr]] __ptr32 __ptr64 __capability"; 12328 verifyFormat(("x = (foo *" + AllQualifiers + ")*v;").str(), LongPointerRight); 12329 verifyFormat(("x = (foo* " + AllQualifiers + ")*v;").str(), LongPointerLeft); 12330 12331 // Also check that address-of is not parsed as a binary bitwise-and: 12332 verifyFormat("x = (foo *const)&v;"); 12333 verifyFormat(("x = (foo *" + AllQualifiers + ")&v;").str(), LongPointerRight); 12334 verifyFormat(("x = (foo* " + AllQualifiers + ")&v;").str(), LongPointerLeft); 12335 12336 // Check custom qualifiers: 12337 FormatStyle CustomQualifier = getLLVMStyleWithColumns(999); 12338 CustomQualifier.AttributeMacros.push_back("__my_qualifier"); 12339 verifyFormat("x = (foo * __my_qualifier) * v;"); // not parsed as qualifier. 12340 verifyFormat("x = (foo *__my_qualifier)*v;", CustomQualifier); 12341 verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)*v;").str(), 12342 CustomQualifier); 12343 verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)&v;").str(), 12344 CustomQualifier); 12345 12346 // Check that unknown identifiers result in binary operator parsing: 12347 verifyFormat("x = (foo * __unknown_qualifier) * v;"); 12348 verifyFormat("x = (foo * __unknown_qualifier) & v;"); 12349 } 12350 12351 TEST_F(FormatTest, UnderstandsSquareAttributes) { 12352 verifyFormat("SomeType s [[unused]] (InitValue);"); 12353 verifyFormat("SomeType s [[gnu::unused]] (InitValue);"); 12354 verifyFormat("SomeType s [[using gnu: unused]] (InitValue);"); 12355 verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}"); 12356 verifyFormat("[[suppress(type.5)]] int uninitialized_on_purpose;"); 12357 verifyFormat("void f() [[deprecated(\"so sorry\")]];"); 12358 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 12359 " [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 12360 verifyFormat("[[nodiscard]] bool f() { return false; }"); 12361 verifyFormat("class [[nodiscard]] f {\npublic:\n f() {}\n}"); 12362 verifyFormat("class [[deprecated(\"so sorry\")]] f {\npublic:\n f() {}\n}"); 12363 verifyFormat("class [[gnu::unused]] f {\npublic:\n f() {}\n}"); 12364 verifyFormat("[[nodiscard]] ::qualified_type f();"); 12365 12366 // Make sure we do not mistake attributes for array subscripts. 12367 verifyFormat("int a() {}\n" 12368 "[[unused]] int b() {}"); 12369 verifyFormat("NSArray *arr;\n" 12370 "arr[[Foo() bar]];"); 12371 12372 // On the other hand, we still need to correctly find array subscripts. 12373 verifyFormat("int a = std::vector<int>{1, 2, 3}[0];"); 12374 12375 // Make sure that we do not mistake Objective-C method inside array literals 12376 // as attributes, even if those method names are also keywords. 12377 verifyFormat("@[ [foo bar] ];"); 12378 verifyFormat("@[ [NSArray class] ];"); 12379 verifyFormat("@[ [foo enum] ];"); 12380 12381 verifyFormat("template <typename T> [[nodiscard]] int a() { return 1; }"); 12382 12383 // Make sure we do not parse attributes as lambda introducers. 12384 FormatStyle MultiLineFunctions = getLLVMStyle(); 12385 MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 12386 verifyFormat("[[unused]] int b() {\n" 12387 " return 42;\n" 12388 "}", 12389 MultiLineFunctions); 12390 } 12391 12392 TEST_F(FormatTest, AttributeClass) { 12393 FormatStyle Style = getChromiumStyle(FormatStyle::LK_Cpp); 12394 verifyFormat("class S {\n" 12395 " S(S&&) = default;\n" 12396 "};", 12397 Style); 12398 verifyFormat("class [[nodiscard]] S {\n" 12399 " S(S&&) = default;\n" 12400 "};", 12401 Style); 12402 verifyFormat("class __attribute((maybeunused)) S {\n" 12403 " S(S&&) = default;\n" 12404 "};", 12405 Style); 12406 verifyFormat("struct S {\n" 12407 " S(S&&) = default;\n" 12408 "};", 12409 Style); 12410 verifyFormat("struct [[nodiscard]] S {\n" 12411 " S(S&&) = default;\n" 12412 "};", 12413 Style); 12414 } 12415 12416 TEST_F(FormatTest, AttributesAfterMacro) { 12417 FormatStyle Style = getLLVMStyle(); 12418 verifyFormat("MACRO;\n" 12419 "__attribute__((maybe_unused)) int foo() {\n" 12420 " //...\n" 12421 "}"); 12422 12423 verifyFormat("MACRO;\n" 12424 "[[nodiscard]] int foo() {\n" 12425 " //...\n" 12426 "}"); 12427 12428 verifyNoChange("MACRO\n\n" 12429 "__attribute__((maybe_unused)) int foo() {\n" 12430 " //...\n" 12431 "}"); 12432 12433 verifyNoChange("MACRO\n\n" 12434 "[[nodiscard]] int foo() {\n" 12435 " //...\n" 12436 "}"); 12437 } 12438 12439 TEST_F(FormatTest, AttributePenaltyBreaking) { 12440 FormatStyle Style = getLLVMStyle(); 12441 verifyFormat("void ABCDEFGH::ABCDEFGHIJKLMN(\n" 12442 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}", 12443 Style); 12444 verifyFormat("void ABCDEFGH::ABCDEFGHIJK(\n" 12445 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}", 12446 Style); 12447 verifyFormat("void ABCDEFGH::ABCDEFGH([[maybe_unused]] const " 12448 "shared_ptr<ALongTypeName> &C d) {\n}", 12449 Style); 12450 } 12451 12452 TEST_F(FormatTest, UnderstandsEllipsis) { 12453 FormatStyle Style = getLLVMStyle(); 12454 verifyFormat("int printf(const char *fmt, ...);"); 12455 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); 12456 verifyFormat("template <class... Ts> void Foo(Ts *...ts) {}"); 12457 12458 verifyFormat("template <int *...PP> a;", Style); 12459 12460 Style.PointerAlignment = FormatStyle::PAS_Left; 12461 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", Style); 12462 12463 verifyFormat("template <int*... PP> a;", Style); 12464 12465 Style.PointerAlignment = FormatStyle::PAS_Middle; 12466 verifyFormat("template <int *... PP> a;", Style); 12467 } 12468 12469 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { 12470 verifyFormat("int *a;\n" 12471 "int *a;\n" 12472 "int *a;", 12473 "int *a;\n" 12474 "int* a;\n" 12475 "int *a;", 12476 getGoogleStyle()); 12477 verifyFormat("int* a;\n" 12478 "int* a;\n" 12479 "int* a;", 12480 "int* a;\n" 12481 "int* a;\n" 12482 "int *a;", 12483 getGoogleStyle()); 12484 verifyFormat("int *a;\n" 12485 "int *a;\n" 12486 "int *a;", 12487 "int *a;\n" 12488 "int * a;\n" 12489 "int * a;", 12490 getGoogleStyle()); 12491 verifyFormat("auto x = [] {\n" 12492 " int *a;\n" 12493 " int *a;\n" 12494 " int *a;\n" 12495 "};", 12496 "auto x=[]{int *a;\n" 12497 "int * a;\n" 12498 "int * a;};", 12499 getGoogleStyle()); 12500 } 12501 12502 TEST_F(FormatTest, UnderstandsRvalueReferences) { 12503 verifyFormat("int f(int &&a) {}"); 12504 verifyFormat("int f(int a, char &&b) {}"); 12505 verifyFormat("void f() { int &&a = b; }"); 12506 verifyGoogleFormat("int f(int a, char&& b) {}"); 12507 verifyGoogleFormat("void f() { int&& a = b; }"); 12508 12509 verifyIndependentOfContext("A<int &&> a;"); 12510 verifyIndependentOfContext("A<int &&, int &&> a;"); 12511 verifyGoogleFormat("A<int&&> a;"); 12512 verifyGoogleFormat("A<int&&, int&&> a;"); 12513 12514 // Not rvalue references: 12515 verifyFormat("template <bool B, bool C> class A {\n" 12516 " static_assert(B && C, \"Something is wrong\");\n" 12517 "};"); 12518 verifyFormat("template <typename T> void swap() noexcept(Bar<T> && Foo<T>);"); 12519 verifyFormat("template <typename T> struct S {\n" 12520 " explicit(Bar<T> && Foo<T>) S(const S &);\n" 12521 "};"); 12522 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))"); 12523 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))"); 12524 verifyFormat("#define A(a, b) (a && b)"); 12525 } 12526 12527 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 12528 verifyFormat("void f() {\n" 12529 " x[aaaaaaaaa -\n" 12530 " b] = 23;\n" 12531 "}", 12532 getLLVMStyleWithColumns(15)); 12533 } 12534 12535 TEST_F(FormatTest, FormatsCasts) { 12536 verifyFormat("Type *A = static_cast<Type *>(P);"); 12537 verifyFormat("static_cast<Type *>(P);"); 12538 verifyFormat("static_cast<Type &>(Fun)(Args);"); 12539 verifyFormat("static_cast<Type &>(*Fun)(Args);"); 12540 verifyFormat("if (static_cast<int>(A) + B >= 0)\n ;"); 12541 // Check that static_cast<...>(...) does not require the next token to be on 12542 // the same line. 12543 verifyFormat("some_loooong_output << something_something__ << " 12544 "static_cast<const void *>(R)\n" 12545 " << something;"); 12546 verifyFormat("a = static_cast<Type &>(*Fun)(Args);"); 12547 verifyFormat("const_cast<Type &>(*Fun)(Args);"); 12548 verifyFormat("dynamic_cast<Type &>(*Fun)(Args);"); 12549 verifyFormat("reinterpret_cast<Type &>(*Fun)(Args);"); 12550 verifyFormat("Type *A = (Type *)P;"); 12551 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 12552 verifyFormat("int a = (int)(2.0f);"); 12553 verifyFormat("int a = (int)2.0f;"); 12554 verifyFormat("x[(int32)y];"); 12555 verifyFormat("x = (int32)y;"); 12556 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)"); 12557 verifyFormat("int a = (int)*b;"); 12558 verifyFormat("int a = (int)2.0f;"); 12559 verifyFormat("int a = (int)~0;"); 12560 verifyFormat("int a = (int)++a;"); 12561 verifyFormat("int a = (int)sizeof(int);"); 12562 verifyFormat("int a = (int)+2;"); 12563 verifyFormat("my_int a = (my_int)2.0f;"); 12564 verifyFormat("my_int a = (my_int)sizeof(int);"); 12565 verifyFormat("return (my_int)aaa;"); 12566 verifyFormat("throw (my_int)aaa;"); 12567 verifyFormat("#define x ((int)-1)"); 12568 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1"); 12569 verifyFormat("#define p(q) ((int *)&q)"); 12570 verifyFormat("fn(a)(b) + 1;"); 12571 12572 verifyFormat("void f() { my_int a = (my_int)*b; }"); 12573 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }"); 12574 verifyFormat("my_int a = (my_int)~0;"); 12575 verifyFormat("my_int a = (my_int)++a;"); 12576 verifyFormat("my_int a = (my_int)-2;"); 12577 verifyFormat("my_int a = (my_int)1;"); 12578 verifyFormat("my_int a = (my_int *)1;"); 12579 verifyFormat("my_int a = (const my_int)-1;"); 12580 verifyFormat("my_int a = (const my_int *)-1;"); 12581 verifyFormat("my_int a = (my_int)(my_int)-1;"); 12582 verifyFormat("my_int a = (ns::my_int)-2;"); 12583 verifyFormat("case (my_int)ONE:"); 12584 verifyFormat("auto x = (X)this;"); 12585 // Casts in Obj-C style calls used to not be recognized as such. 12586 verifyGoogleFormat("int a = [(type*)[((type*)val) arg] arg];"); 12587 12588 // FIXME: single value wrapped with paren will be treated as cast. 12589 verifyFormat("void f(int i = (kValue)*kMask) {}"); 12590 12591 verifyFormat("{\n" 12592 " (void)F;\n" 12593 "}"); 12594 12595 // Don't break after a cast's 12596 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 12597 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n" 12598 " bbbbbbbbbbbbbbbbbbbbbb);"); 12599 12600 verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(x)"); 12601 verifyFormat("#define CONF_BOOL(x) (bool *)(x)"); 12602 verifyFormat("#define CONF_BOOL(x) (bool)(x)"); 12603 verifyFormat("bool *y = (bool *)(void *)(x);"); 12604 verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(int)(x)"); 12605 verifyFormat("bool *y = (bool *)(void *)(int)(x);"); 12606 verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(int)foo(x)"); 12607 verifyFormat("bool *y = (bool *)(void *)(int)foo(x);"); 12608 12609 // These are not casts. 12610 verifyFormat("void f(int *) {}"); 12611 verifyFormat("f(foo)->b;"); 12612 verifyFormat("f(foo).b;"); 12613 verifyFormat("f(foo)(b);"); 12614 verifyFormat("f(foo)[b];"); 12615 verifyFormat("[](foo) { return 4; }(bar);"); 12616 verifyFormat("(*funptr)(foo)[4];"); 12617 verifyFormat("funptrs[4](foo)[4];"); 12618 verifyFormat("void f(int *);"); 12619 verifyFormat("void f(int *) = 0;"); 12620 verifyFormat("void f(SmallVector<int>) {}"); 12621 verifyFormat("void f(SmallVector<int>);"); 12622 verifyFormat("void f(SmallVector<int>) = 0;"); 12623 verifyFormat("void f(int i = (kA * kB) & kMask) {}"); 12624 verifyFormat("int a = sizeof(int) * b;"); 12625 verifyGoogleFormat("int a = alignof(int) * b;"); 12626 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;"); 12627 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");"); 12628 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;"); 12629 12630 // These are not casts, but at some point were confused with casts. 12631 verifyFormat("virtual void foo(int *) override;"); 12632 verifyFormat("virtual void foo(char &) const;"); 12633 verifyFormat("virtual void foo(int *a, char *) const;"); 12634 verifyFormat("int a = sizeof(int *) + b;"); 12635 verifyGoogleFormat("int a = alignof(int *) + b;"); 12636 verifyFormat("bool b = f(g<int>) && c;"); 12637 verifyFormat("typedef void (*f)(int i) func;"); 12638 verifyFormat("void operator++(int) noexcept;"); 12639 verifyFormat("void operator++(int &) noexcept;"); 12640 verifyFormat("void operator delete(void *, std::size_t, const std::nothrow_t " 12641 "&) noexcept;"); 12642 verifyFormat( 12643 "void operator delete(std::size_t, const std::nothrow_t &) noexcept;"); 12644 verifyFormat("void operator delete(const std::nothrow_t &) noexcept;"); 12645 verifyFormat("void operator delete(std::nothrow_t &) noexcept;"); 12646 verifyFormat("void operator delete(nothrow_t &) noexcept;"); 12647 verifyFormat("void operator delete(foo &) noexcept;"); 12648 verifyFormat("void operator delete(foo) noexcept;"); 12649 verifyFormat("void operator delete(int) noexcept;"); 12650 verifyFormat("void operator delete(int &) noexcept;"); 12651 verifyFormat("void operator delete(int &) volatile noexcept;"); 12652 verifyFormat("void operator delete(int &) const"); 12653 verifyFormat("void operator delete(int &) = default"); 12654 verifyFormat("void operator delete(int &) = delete"); 12655 verifyFormat("void operator delete(int &) [[noreturn]]"); 12656 verifyFormat("void operator delete(int &) throw();"); 12657 verifyFormat("void operator delete(int &) throw(int);"); 12658 verifyFormat("auto operator delete(int &) -> int;"); 12659 verifyFormat("auto operator delete(int &) override"); 12660 verifyFormat("auto operator delete(int &) final"); 12661 12662 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n" 12663 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 12664 // FIXME: The indentation here is not ideal. 12665 verifyFormat( 12666 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 12667 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n" 12668 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];"); 12669 } 12670 12671 TEST_F(FormatTest, FormatsFunctionTypes) { 12672 verifyFormat("A<bool()> a;"); 12673 verifyFormat("A<SomeType()> a;"); 12674 verifyFormat("A<void (*)(int, std::string)> a;"); 12675 verifyFormat("A<void *(int)>;"); 12676 verifyFormat("void *(*a)(int *, SomeType *);"); 12677 verifyFormat("int (*func)(void *);"); 12678 verifyFormat("void f() { int (*func)(void *); }"); 12679 verifyFormat("template <class CallbackClass>\n" 12680 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);"); 12681 12682 verifyGoogleFormat("A<void*(int*, SomeType*)>;"); 12683 verifyGoogleFormat("void* (*a)(int);"); 12684 verifyGoogleFormat( 12685 "template <class CallbackClass>\n" 12686 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);"); 12687 12688 // Other constructs can look somewhat like function types: 12689 verifyFormat("A<sizeof(*x)> a;"); 12690 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)"); 12691 verifyFormat("some_var = function(*some_pointer_var)[0];"); 12692 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }"); 12693 verifyFormat("int x = f(&h)();"); 12694 verifyFormat("returnsFunction(¶m1, ¶m2)(param);"); 12695 verifyFormat("std::function<\n" 12696 " LooooooooooongTemplatedType<\n" 12697 " SomeType>*(\n" 12698 " LooooooooooooooooongType type)>\n" 12699 " function;", 12700 getGoogleStyleWithColumns(40)); 12701 } 12702 12703 TEST_F(FormatTest, FormatsPointersToArrayTypes) { 12704 verifyFormat("A (*foo_)[6];"); 12705 verifyFormat("vector<int> (*foo_)[6];"); 12706 } 12707 12708 TEST_F(FormatTest, BreaksLongVariableDeclarations) { 12709 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 12710 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 12711 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n" 12712 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 12713 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 12714 " *LoooooooooooooooooooooooooooooooooooooooongVariable;"); 12715 12716 // Different ways of ()-initializiation. 12717 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 12718 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);"); 12719 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 12720 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);"); 12721 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 12722 " LoooooooooooooooooooooooooooooooooooooooongVariable({});"); 12723 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 12724 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);"); 12725 12726 // Lambdas should not confuse the variable declaration heuristic. 12727 verifyFormat("LooooooooooooooooongType\n" 12728 " variable(nullptr, [](A *a) {});", 12729 getLLVMStyleWithColumns(40)); 12730 } 12731 12732 TEST_F(FormatTest, BreaksLongDeclarations) { 12733 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n" 12734 " AnotherNameForTheLongType;"); 12735 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n" 12736 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 12737 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 12738 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 12739 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n" 12740 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 12741 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 12742 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 12743 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n" 12744 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 12745 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 12746 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 12747 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 12748 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 12749 verifyFormat("typeof(LoooooooooooooooooooooooooooooooooooooooooongName)\n" 12750 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 12751 verifyFormat("_Atomic(LooooooooooooooooooooooooooooooooooooooooongName)\n" 12752 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 12753 verifyFormat("__underlying_type(LooooooooooooooooooooooooooooooongName)\n" 12754 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 12755 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 12756 "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);"); 12757 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 12758 "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}"); 12759 FormatStyle Indented = getLLVMStyle(); 12760 Indented.IndentWrappedFunctionNames = true; 12761 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 12762 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();", 12763 Indented); 12764 verifyFormat( 12765 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 12766 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 12767 Indented); 12768 verifyFormat( 12769 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 12770 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 12771 Indented); 12772 verifyFormat( 12773 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 12774 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 12775 Indented); 12776 12777 // FIXME: Without the comment, this breaks after "(". 12778 verifyGoogleFormat( 12779 "LoooooooooooooooooooooooooooooooooooooooongType // break\n" 12780 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();"); 12781 12782 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 12783 " int LoooooooooooooooooooongParam2) {}"); 12784 verifyFormat( 12785 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" 12786 " SourceLocation L, IdentifierIn *II,\n" 12787 " Type *T) {}"); 12788 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" 12789 "ReallyReaaallyLongFunctionName(\n" 12790 " const std::string &SomeParameter,\n" 12791 " const SomeType<string, SomeOtherTemplateParameter>\n" 12792 " &ReallyReallyLongParameterName,\n" 12793 " const SomeType<string, SomeOtherTemplateParameter>\n" 12794 " &AnotherLongParameterName) {}"); 12795 verifyFormat("template <typename A>\n" 12796 "SomeLoooooooooooooooooooooongType<\n" 12797 " typename some_namespace::SomeOtherType<A>::Type>\n" 12798 "Function() {}"); 12799 12800 verifyGoogleFormat( 12801 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" 12802 " aaaaaaaaaaaaaaaaaaaaaaa;"); 12803 verifyGoogleFormat( 12804 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" 12805 " SourceLocation L) {}"); 12806 verifyGoogleFormat( 12807 "some_namespace::LongReturnType\n" 12808 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" 12809 " int first_long_parameter, int second_parameter) {}"); 12810 12811 verifyGoogleFormat("template <typename T>\n" 12812 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" 12813 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); 12814 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 12815 " int aaaaaaaaaaaaaaaaaaaaaaa);"); 12816 12817 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 12818 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 12819 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 12820 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 12821 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 12822 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 12823 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 12824 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 12825 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n" 12826 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 12827 12828 verifyFormat("template <typename T> // Templates on own line.\n" 12829 "static int // Some comment.\n" 12830 "MyFunction(int a);"); 12831 } 12832 12833 TEST_F(FormatTest, FormatsAccessModifiers) { 12834 FormatStyle Style = getLLVMStyle(); 12835 EXPECT_EQ(Style.EmptyLineBeforeAccessModifier, 12836 FormatStyle::ELBAMS_LogicalBlock); 12837 verifyFormat("struct foo {\n" 12838 "private:\n" 12839 " void f() {}\n" 12840 "\n" 12841 "private:\n" 12842 " int i;\n" 12843 "\n" 12844 "protected:\n" 12845 " int j;\n" 12846 "};", 12847 Style); 12848 verifyFormat("struct foo {\n" 12849 "private:\n" 12850 " void f() {}\n" 12851 "\n" 12852 "private:\n" 12853 " int i;\n" 12854 "\n" 12855 "protected:\n" 12856 " int j;\n" 12857 "};", 12858 "struct foo {\n" 12859 "private:\n" 12860 " void f() {}\n" 12861 "private:\n" 12862 " int i;\n" 12863 "protected:\n" 12864 " int j;\n" 12865 "};", 12866 Style); 12867 verifyFormat("struct foo { /* comment */\n" 12868 "private:\n" 12869 " int i;\n" 12870 " // comment\n" 12871 "private:\n" 12872 " int j;\n" 12873 "};", 12874 Style); 12875 verifyFormat("struct foo {\n" 12876 "#ifdef FOO\n" 12877 "#endif\n" 12878 "private:\n" 12879 " int i;\n" 12880 "#ifdef FOO\n" 12881 "private:\n" 12882 "#endif\n" 12883 " int j;\n" 12884 "};", 12885 Style); 12886 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 12887 verifyFormat("struct foo {\n" 12888 "private:\n" 12889 " void f() {}\n" 12890 "private:\n" 12891 " int i;\n" 12892 "protected:\n" 12893 " int j;\n" 12894 "};", 12895 Style); 12896 verifyFormat("struct foo {\n" 12897 "private:\n" 12898 " void f() {}\n" 12899 "private:\n" 12900 " int i;\n" 12901 "protected:\n" 12902 " int j;\n" 12903 "};", 12904 "struct foo {\n" 12905 "\n" 12906 "private:\n" 12907 " void f() {}\n" 12908 "\n" 12909 "private:\n" 12910 " int i;\n" 12911 "\n" 12912 "protected:\n" 12913 " int j;\n" 12914 "};", 12915 Style); 12916 verifyFormat("struct foo { /* comment */\n" 12917 "private:\n" 12918 " int i;\n" 12919 " // comment\n" 12920 "private:\n" 12921 " int j;\n" 12922 "};", 12923 "struct foo { /* comment */\n" 12924 "\n" 12925 "private:\n" 12926 " int i;\n" 12927 " // comment\n" 12928 "\n" 12929 "private:\n" 12930 " int j;\n" 12931 "};", 12932 Style); 12933 verifyFormat("struct foo {\n" 12934 "#ifdef FOO\n" 12935 "#endif\n" 12936 "private:\n" 12937 " int i;\n" 12938 "#ifdef FOO\n" 12939 "private:\n" 12940 "#endif\n" 12941 " int j;\n" 12942 "};", 12943 "struct foo {\n" 12944 "#ifdef FOO\n" 12945 "#endif\n" 12946 "\n" 12947 "private:\n" 12948 " int i;\n" 12949 "#ifdef FOO\n" 12950 "\n" 12951 "private:\n" 12952 "#endif\n" 12953 " int j;\n" 12954 "};", 12955 Style); 12956 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 12957 verifyFormat("struct foo {\n" 12958 "private:\n" 12959 " void f() {}\n" 12960 "\n" 12961 "private:\n" 12962 " int i;\n" 12963 "\n" 12964 "protected:\n" 12965 " int j;\n" 12966 "};", 12967 Style); 12968 verifyFormat("struct foo {\n" 12969 "private:\n" 12970 " void f() {}\n" 12971 "\n" 12972 "private:\n" 12973 " int i;\n" 12974 "\n" 12975 "protected:\n" 12976 " int j;\n" 12977 "};", 12978 "struct foo {\n" 12979 "private:\n" 12980 " void f() {}\n" 12981 "private:\n" 12982 " int i;\n" 12983 "protected:\n" 12984 " int j;\n" 12985 "};", 12986 Style); 12987 verifyFormat("struct foo { /* comment */\n" 12988 "private:\n" 12989 " int i;\n" 12990 " // comment\n" 12991 "\n" 12992 "private:\n" 12993 " int j;\n" 12994 "};", 12995 Style); 12996 verifyFormat("struct foo {\n" 12997 "#ifdef FOO\n" 12998 "#endif\n" 12999 "\n" 13000 "private:\n" 13001 " int i;\n" 13002 "#ifdef FOO\n" 13003 "\n" 13004 "private:\n" 13005 "#endif\n" 13006 " int j;\n" 13007 "};", 13008 "struct foo {\n" 13009 "#ifdef FOO\n" 13010 "#endif\n" 13011 "private:\n" 13012 " int i;\n" 13013 "#ifdef FOO\n" 13014 "private:\n" 13015 "#endif\n" 13016 " int j;\n" 13017 "};", 13018 Style); 13019 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 13020 verifyNoChange("struct foo {\n" 13021 "\n" 13022 "private:\n" 13023 " void f() {}\n" 13024 "\n" 13025 "private:\n" 13026 " int i;\n" 13027 "\n" 13028 "protected:\n" 13029 " int j;\n" 13030 "};", 13031 Style); 13032 verifyFormat("struct foo {\n" 13033 "private:\n" 13034 " void f() {}\n" 13035 "private:\n" 13036 " int i;\n" 13037 "protected:\n" 13038 " int j;\n" 13039 "};", 13040 Style); 13041 verifyNoChange("struct foo { /* comment */\n" 13042 "\n" 13043 "private:\n" 13044 " int i;\n" 13045 " // comment\n" 13046 "\n" 13047 "private:\n" 13048 " int j;\n" 13049 "};", 13050 Style); 13051 verifyFormat("struct foo { /* comment */\n" 13052 "private:\n" 13053 " int i;\n" 13054 " // comment\n" 13055 "private:\n" 13056 " int j;\n" 13057 "};", 13058 Style); 13059 verifyNoChange("struct foo {\n" 13060 "#ifdef FOO\n" 13061 "#endif\n" 13062 "\n" 13063 "private:\n" 13064 " int i;\n" 13065 "#ifdef FOO\n" 13066 "\n" 13067 "private:\n" 13068 "#endif\n" 13069 " int j;\n" 13070 "};", 13071 Style); 13072 verifyFormat("struct foo {\n" 13073 "#ifdef FOO\n" 13074 "#endif\n" 13075 "private:\n" 13076 " int i;\n" 13077 "#ifdef FOO\n" 13078 "private:\n" 13079 "#endif\n" 13080 " int j;\n" 13081 "};", 13082 Style); 13083 Style.AttributeMacros.push_back("FOO"); 13084 Style.AttributeMacros.push_back("BAR"); 13085 verifyFormat("struct foo {\n" 13086 "FOO private:\n" 13087 " int i;\n" 13088 "BAR(x) protected:\n" 13089 " int j;\n" 13090 "};", 13091 Style); 13092 13093 FormatStyle NoEmptyLines = getLLVMStyle(); 13094 NoEmptyLines.MaxEmptyLinesToKeep = 0; 13095 verifyFormat("struct foo {\n" 13096 "private:\n" 13097 " void f() {}\n" 13098 "\n" 13099 "private:\n" 13100 " int i;\n" 13101 "\n" 13102 "public:\n" 13103 "protected:\n" 13104 " int j;\n" 13105 "};", 13106 NoEmptyLines); 13107 13108 NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 13109 verifyFormat("struct foo {\n" 13110 "private:\n" 13111 " void f() {}\n" 13112 "private:\n" 13113 " int i;\n" 13114 "public:\n" 13115 "protected:\n" 13116 " int j;\n" 13117 "};", 13118 NoEmptyLines); 13119 13120 NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 13121 verifyFormat("struct foo {\n" 13122 "private:\n" 13123 " void f() {}\n" 13124 "\n" 13125 "private:\n" 13126 " int i;\n" 13127 "\n" 13128 "public:\n" 13129 "\n" 13130 "protected:\n" 13131 " int j;\n" 13132 "};", 13133 NoEmptyLines); 13134 } 13135 13136 TEST_F(FormatTest, FormatsAfterAccessModifiers) { 13137 13138 FormatStyle Style = getLLVMStyle(); 13139 EXPECT_EQ(Style.EmptyLineAfterAccessModifier, FormatStyle::ELAAMS_Never); 13140 verifyFormat("struct foo {\n" 13141 "private:\n" 13142 " void f() {}\n" 13143 "\n" 13144 "private:\n" 13145 " int i;\n" 13146 "\n" 13147 "protected:\n" 13148 " int j;\n" 13149 "};", 13150 Style); 13151 13152 // Check if lines are removed. 13153 verifyFormat("struct foo {\n" 13154 "private:\n" 13155 " void f() {}\n" 13156 "\n" 13157 "private:\n" 13158 " int i;\n" 13159 "\n" 13160 "protected:\n" 13161 " int j;\n" 13162 "};", 13163 "struct foo {\n" 13164 "private:\n" 13165 "\n" 13166 " void f() {}\n" 13167 "\n" 13168 "private:\n" 13169 "\n" 13170 " int i;\n" 13171 "\n" 13172 "protected:\n" 13173 "\n" 13174 " int j;\n" 13175 "};", 13176 Style); 13177 13178 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 13179 verifyFormat("struct foo {\n" 13180 "private:\n" 13181 "\n" 13182 " void f() {}\n" 13183 "\n" 13184 "private:\n" 13185 "\n" 13186 " int i;\n" 13187 "\n" 13188 "protected:\n" 13189 "\n" 13190 " int j;\n" 13191 "};", 13192 Style); 13193 13194 // Check if lines are added. 13195 verifyFormat("struct foo {\n" 13196 "private:\n" 13197 "\n" 13198 " void f() {}\n" 13199 "\n" 13200 "private:\n" 13201 "\n" 13202 " int i;\n" 13203 "\n" 13204 "protected:\n" 13205 "\n" 13206 " int j;\n" 13207 "};", 13208 "struct foo {\n" 13209 "private:\n" 13210 " void f() {}\n" 13211 "\n" 13212 "private:\n" 13213 " int i;\n" 13214 "\n" 13215 "protected:\n" 13216 " int j;\n" 13217 "};", 13218 Style); 13219 13220 // Leave tests rely on the code layout, test::messUp can not be used. 13221 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 13222 Style.MaxEmptyLinesToKeep = 0u; 13223 verifyFormat("struct foo {\n" 13224 "private:\n" 13225 " void f() {}\n" 13226 "\n" 13227 "private:\n" 13228 " int i;\n" 13229 "\n" 13230 "protected:\n" 13231 " int j;\n" 13232 "};", 13233 Style); 13234 13235 // Check if MaxEmptyLinesToKeep is respected. 13236 verifyFormat("struct foo {\n" 13237 "private:\n" 13238 " void f() {}\n" 13239 "\n" 13240 "private:\n" 13241 " int i;\n" 13242 "\n" 13243 "protected:\n" 13244 " int j;\n" 13245 "};", 13246 "struct foo {\n" 13247 "private:\n" 13248 "\n\n\n" 13249 " void f() {}\n" 13250 "\n" 13251 "private:\n" 13252 "\n\n\n" 13253 " int i;\n" 13254 "\n" 13255 "protected:\n" 13256 "\n\n\n" 13257 " int j;\n" 13258 "};", 13259 Style); 13260 13261 Style.MaxEmptyLinesToKeep = 1u; 13262 verifyNoChange("struct foo {\n" 13263 "private:\n" 13264 "\n" 13265 " void f() {}\n" 13266 "\n" 13267 "private:\n" 13268 "\n" 13269 " int i;\n" 13270 "\n" 13271 "protected:\n" 13272 "\n" 13273 " int j;\n" 13274 "};", 13275 Style); 13276 // Check if no lines are kept. 13277 verifyFormat("struct foo {\n" 13278 "private:\n" 13279 " void f() {}\n" 13280 "\n" 13281 "private:\n" 13282 " int i;\n" 13283 "\n" 13284 "protected:\n" 13285 " int j;\n" 13286 "};", 13287 Style); 13288 // Check if MaxEmptyLinesToKeep is respected. 13289 verifyFormat("struct foo {\n" 13290 "private:\n" 13291 "\n" 13292 " void f() {}\n" 13293 "\n" 13294 "private:\n" 13295 "\n" 13296 " int i;\n" 13297 "\n" 13298 "protected:\n" 13299 "\n" 13300 " int j;\n" 13301 "};", 13302 "struct foo {\n" 13303 "private:\n" 13304 "\n\n\n" 13305 " void f() {}\n" 13306 "\n" 13307 "private:\n" 13308 "\n\n\n" 13309 " int i;\n" 13310 "\n" 13311 "protected:\n" 13312 "\n\n\n" 13313 " int j;\n" 13314 "};", 13315 Style); 13316 13317 Style.MaxEmptyLinesToKeep = 10u; 13318 verifyNoChange("struct foo {\n" 13319 "private:\n" 13320 "\n\n\n" 13321 " void f() {}\n" 13322 "\n" 13323 "private:\n" 13324 "\n\n\n" 13325 " int i;\n" 13326 "\n" 13327 "protected:\n" 13328 "\n\n\n" 13329 " int j;\n" 13330 "};", 13331 Style); 13332 13333 // Test with comments. 13334 Style = getLLVMStyle(); 13335 verifyFormat("struct foo {\n" 13336 "private:\n" 13337 " // comment\n" 13338 " void f() {}\n" 13339 "\n" 13340 "private: /* comment */\n" 13341 " int i;\n" 13342 "};", 13343 Style); 13344 verifyFormat("struct foo {\n" 13345 "private:\n" 13346 " // comment\n" 13347 " void f() {}\n" 13348 "\n" 13349 "private: /* comment */\n" 13350 " int i;\n" 13351 "};", 13352 "struct foo {\n" 13353 "private:\n" 13354 "\n" 13355 " // comment\n" 13356 " void f() {}\n" 13357 "\n" 13358 "private: /* comment */\n" 13359 "\n" 13360 " int i;\n" 13361 "};", 13362 Style); 13363 13364 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 13365 verifyFormat("struct foo {\n" 13366 "private:\n" 13367 "\n" 13368 " // comment\n" 13369 " void f() {}\n" 13370 "\n" 13371 "private: /* comment */\n" 13372 "\n" 13373 " int i;\n" 13374 "};", 13375 "struct foo {\n" 13376 "private:\n" 13377 " // comment\n" 13378 " void f() {}\n" 13379 "\n" 13380 "private: /* comment */\n" 13381 " int i;\n" 13382 "};", 13383 Style); 13384 verifyFormat("struct foo {\n" 13385 "private:\n" 13386 "\n" 13387 " // comment\n" 13388 " void f() {}\n" 13389 "\n" 13390 "private: /* comment */\n" 13391 "\n" 13392 " int i;\n" 13393 "};", 13394 Style); 13395 13396 // Test with preprocessor defines. 13397 Style = getLLVMStyle(); 13398 verifyFormat("struct foo {\n" 13399 "private:\n" 13400 "#ifdef FOO\n" 13401 "#endif\n" 13402 " void f() {}\n" 13403 "};", 13404 Style); 13405 verifyFormat("struct foo {\n" 13406 "private:\n" 13407 "#ifdef FOO\n" 13408 "#endif\n" 13409 " void f() {}\n" 13410 "};", 13411 "struct foo {\n" 13412 "private:\n" 13413 "\n" 13414 "#ifdef FOO\n" 13415 "#endif\n" 13416 " void f() {}\n" 13417 "};", 13418 Style); 13419 verifyNoChange("struct foo {\n" 13420 "#ifdef FOO\n" 13421 "#else\n" 13422 "private:\n" 13423 "\n" 13424 "#endif\n" 13425 "};", 13426 Style); 13427 verifyFormat("struct foo {\n" 13428 "#ifdef FOO\n" 13429 "#else\n" 13430 "private:\n" 13431 "\n" 13432 "#endif\n" 13433 "};", 13434 "struct foo {\n" 13435 "#ifdef FOO\n" 13436 "#else\n" 13437 "private:\n" 13438 "\n" 13439 "\n" 13440 "#endif\n" 13441 "};", 13442 Style); 13443 verifyFormat("struct foo {\n" 13444 "#ifdef FOO\n" 13445 "private:\n" 13446 "#else\n" 13447 "#endif\n" 13448 "};", 13449 "struct foo {\n" 13450 "#ifdef FOO\n" 13451 "private:\n" 13452 "\n" 13453 "\n" 13454 "#else\n" 13455 "#endif\n" 13456 "};", 13457 Style); 13458 verifyFormat("struct foo {\n" 13459 "#if 0\n" 13460 "#else\n" 13461 "#endif\n" 13462 "#ifdef FOO\n" 13463 "private:\n" 13464 "#endif\n" 13465 "};", 13466 "struct foo {\n" 13467 "#if 0\n" 13468 "#else\n" 13469 "#endif\n" 13470 "#ifdef FOO\n" 13471 "private:\n" 13472 "\n" 13473 "\n" 13474 "#endif\n" 13475 "};", 13476 Style); 13477 13478 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 13479 verifyFormat("struct foo {\n" 13480 "private:\n" 13481 "\n" 13482 "#ifdef FOO\n" 13483 "#endif\n" 13484 " void f() {}\n" 13485 "};", 13486 "struct foo {\n" 13487 "private:\n" 13488 "#ifdef FOO\n" 13489 "#endif\n" 13490 " void f() {}\n" 13491 "};", 13492 Style); 13493 verifyFormat("struct foo {\n" 13494 "private:\n" 13495 "\n" 13496 "#ifdef FOO\n" 13497 "#endif\n" 13498 " void f() {}\n" 13499 "};", 13500 Style); 13501 } 13502 13503 TEST_F(FormatTest, FormatsAfterAndBeforeAccessModifiersInteraction) { 13504 // Combined tests of EmptyLineAfterAccessModifier and 13505 // EmptyLineBeforeAccessModifier. 13506 FormatStyle Style = getLLVMStyle(); 13507 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 13508 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 13509 verifyFormat("struct foo {\n" 13510 "private:\n" 13511 "\n" 13512 "protected:\n" 13513 "};", 13514 Style); 13515 13516 Style.MaxEmptyLinesToKeep = 10u; 13517 // Both remove all new lines. 13518 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 13519 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 13520 verifyFormat("struct foo {\n" 13521 "private:\n" 13522 "protected:\n" 13523 "};", 13524 "struct foo {\n" 13525 "private:\n" 13526 "\n\n\n" 13527 "protected:\n" 13528 "};", 13529 Style); 13530 13531 // Leave tests rely on the code layout, test::messUp can not be used. 13532 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 13533 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 13534 Style.MaxEmptyLinesToKeep = 10u; 13535 verifyNoChange("struct foo {\n" 13536 "private:\n" 13537 "\n\n\n" 13538 "protected:\n" 13539 "};", 13540 Style); 13541 Style.MaxEmptyLinesToKeep = 3u; 13542 verifyNoChange("struct foo {\n" 13543 "private:\n" 13544 "\n\n\n" 13545 "protected:\n" 13546 "};", 13547 Style); 13548 Style.MaxEmptyLinesToKeep = 1u; 13549 verifyNoChange("struct foo {\n" 13550 "private:\n" 13551 "\n\n\n" 13552 "protected:\n" 13553 "};", 13554 Style); // Based on new lines in original document and not 13555 // on the setting. 13556 13557 Style.MaxEmptyLinesToKeep = 10u; 13558 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 13559 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 13560 // Newlines are kept if they are greater than zero, 13561 // test::messUp removes all new lines which changes the logic 13562 verifyNoChange("struct foo {\n" 13563 "private:\n" 13564 "\n\n\n" 13565 "protected:\n" 13566 "};", 13567 Style); 13568 13569 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 13570 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 13571 // test::messUp removes all new lines which changes the logic 13572 verifyNoChange("struct foo {\n" 13573 "private:\n" 13574 "\n\n\n" 13575 "protected:\n" 13576 "};", 13577 Style); 13578 13579 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 13580 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 13581 verifyNoChange("struct foo {\n" 13582 "private:\n" 13583 "\n\n\n" 13584 "protected:\n" 13585 "};", 13586 Style); // test::messUp removes all new lines which changes 13587 // the logic. 13588 13589 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 13590 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 13591 verifyFormat("struct foo {\n" 13592 "private:\n" 13593 "protected:\n" 13594 "};", 13595 "struct foo {\n" 13596 "private:\n" 13597 "\n\n\n" 13598 "protected:\n" 13599 "};", 13600 Style); 13601 13602 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 13603 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 13604 verifyNoChange("struct foo {\n" 13605 "private:\n" 13606 "\n\n\n" 13607 "protected:\n" 13608 "};", 13609 Style); // test::messUp removes all new lines which changes 13610 // the logic. 13611 13612 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 13613 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 13614 verifyFormat("struct foo {\n" 13615 "private:\n" 13616 "protected:\n" 13617 "};", 13618 "struct foo {\n" 13619 "private:\n" 13620 "\n\n\n" 13621 "protected:\n" 13622 "};", 13623 Style); 13624 13625 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 13626 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 13627 verifyFormat("struct foo {\n" 13628 "private:\n" 13629 "protected:\n" 13630 "};", 13631 "struct foo {\n" 13632 "private:\n" 13633 "\n\n\n" 13634 "protected:\n" 13635 "};", 13636 Style); 13637 13638 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 13639 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 13640 verifyFormat("struct foo {\n" 13641 "private:\n" 13642 "protected:\n" 13643 "};", 13644 "struct foo {\n" 13645 "private:\n" 13646 "\n\n\n" 13647 "protected:\n" 13648 "};", 13649 Style); 13650 13651 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 13652 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 13653 verifyFormat("struct foo {\n" 13654 "private:\n" 13655 "protected:\n" 13656 "};", 13657 "struct foo {\n" 13658 "private:\n" 13659 "\n\n\n" 13660 "protected:\n" 13661 "};", 13662 Style); 13663 } 13664 13665 TEST_F(FormatTest, FormatsArrays) { 13666 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 13667 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;"); 13668 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n" 13669 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;"); 13670 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n" 13671 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}"); 13672 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13673 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 13674 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13675 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;"); 13676 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13677 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 13678 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 13679 verifyFormat( 13680 "llvm::outs() << \"aaaaaaaaaaaa: \"\n" 13681 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 13682 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 13683 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n" 13684 " .aaaaaaaaaaaaaaaaaaaaaa();"); 13685 13686 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n" 13687 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];"); 13688 verifyFormat( 13689 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n" 13690 " .aaaaaaa[0]\n" 13691 " .aaaaaaaaaaaaaaaaaaaaaa();"); 13692 verifyFormat("a[::b::c];"); 13693 13694 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10)); 13695 13696 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0); 13697 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit); 13698 } 13699 13700 TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 13701 verifyFormat("(a)->b();"); 13702 verifyFormat("--a;"); 13703 } 13704 13705 TEST_F(FormatTest, HandlesIncludeDirectives) { 13706 verifyFormat("#include <string>\n" 13707 "#include <a/b/c.h>\n" 13708 "#include \"a/b/string\"\n" 13709 "#include \"string.h\"\n" 13710 "#include \"string.h\"\n" 13711 "#include <a-a>\n" 13712 "#include < path with space >\n" 13713 "#include_next <test.h>" 13714 "#include \"abc.h\" // this is included for ABC\n" 13715 "#include \"some long include\" // with a comment\n" 13716 "#include \"some very long include path\"\n" 13717 "#include <some/very/long/include/path>", 13718 getLLVMStyleWithColumns(35)); 13719 verifyFormat("#include \"a.h\"", "#include \"a.h\""); 13720 verifyFormat("#include <a>", "#include<a>"); 13721 13722 verifyFormat("#import <string>"); 13723 verifyFormat("#import <a/b/c.h>"); 13724 verifyFormat("#import \"a/b/string\""); 13725 verifyFormat("#import \"string.h\""); 13726 verifyFormat("#import \"string.h\""); 13727 verifyFormat("#if __has_include(<strstream>)\n" 13728 "#include <strstream>\n" 13729 "#endif"); 13730 13731 verifyFormat("#define MY_IMPORT <a/b>"); 13732 13733 verifyFormat("#if __has_include(<a/b>)"); 13734 verifyFormat("#if __has_include_next(<a/b>)"); 13735 verifyFormat("#define F __has_include(<a/b>)"); 13736 verifyFormat("#define F __has_include_next(<a/b>)"); 13737 13738 // Protocol buffer definition or missing "#". 13739 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";", 13740 getLLVMStyleWithColumns(30)); 13741 13742 FormatStyle Style = getLLVMStyle(); 13743 Style.AlwaysBreakBeforeMultilineStrings = true; 13744 Style.ColumnLimit = 0; 13745 verifyFormat("#import \"abc.h\"", Style); 13746 13747 // But 'import' might also be a regular C++ namespace. 13748 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 13749 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 13750 verifyFormat("import::Bar foo(val ? 2 : 1);"); 13751 } 13752 13753 //===----------------------------------------------------------------------===// 13754 // Error recovery tests. 13755 //===----------------------------------------------------------------------===// 13756 13757 TEST_F(FormatTest, IncompleteParameterLists) { 13758 FormatStyle NoBinPacking = getLLVMStyle(); 13759 NoBinPacking.BinPackParameters = FormatStyle::BPPS_OnePerLine; 13760 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" 13761 " double *min_x,\n" 13762 " double *max_x,\n" 13763 " double *min_y,\n" 13764 " double *max_y,\n" 13765 " double *min_z,\n" 13766 " double *max_z, ) {}", 13767 NoBinPacking); 13768 } 13769 13770 TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 13771 verifyFormat("void f() { return; }\n42"); 13772 verifyFormat("void f() {\n" 13773 " if (0)\n" 13774 " return;\n" 13775 "}\n" 13776 "42"); 13777 verifyFormat("void f() { return }\n42"); 13778 verifyFormat("void f() {\n" 13779 " if (0)\n" 13780 " return\n" 13781 "}\n" 13782 "42"); 13783 } 13784 13785 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 13786 verifyFormat("void f() { return }", "void f ( ) { return }"); 13787 verifyFormat("void f() {\n" 13788 " if (a)\n" 13789 " return\n" 13790 "}", 13791 "void f ( ) { if ( a ) return }"); 13792 verifyFormat("namespace N {\n" 13793 "void f()\n" 13794 "}", 13795 "namespace N { void f() }"); 13796 verifyFormat("namespace N {\n" 13797 "void f() {}\n" 13798 "void g()\n" 13799 "} // namespace N", 13800 "namespace N { void f( ) { } void g( ) }"); 13801 } 13802 13803 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 13804 verifyFormat("int aaaaaaaa =\n" 13805 " // Overlylongcomment\n" 13806 " b;", 13807 getLLVMStyleWithColumns(20)); 13808 verifyFormat("function(\n" 13809 " ShortArgument,\n" 13810 " LoooooooooooongArgument);", 13811 getLLVMStyleWithColumns(20)); 13812 } 13813 13814 TEST_F(FormatTest, IncorrectAccessSpecifier) { 13815 verifyFormat("public:"); 13816 verifyFormat("class A {\n" 13817 "public\n" 13818 " void f() {}\n" 13819 "};"); 13820 verifyFormat("public\n" 13821 "int qwerty;"); 13822 verifyFormat("public\n" 13823 "B {}"); 13824 verifyFormat("public\n" 13825 "{\n" 13826 "}"); 13827 verifyFormat("public\n" 13828 "B { int x; }"); 13829 } 13830 13831 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 13832 verifyFormat("{"); 13833 verifyFormat("#})"); 13834 verifyNoCrash("(/**/[:!] ?[)."); 13835 verifyNoCrash("struct X {\n" 13836 " operator iunt(\n" 13837 "};"); 13838 verifyNoCrash("struct Foo {\n" 13839 " operator foo(bar\n" 13840 "};"); 13841 } 13842 13843 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) { 13844 // Found by oss-fuzz: 13845 // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212 13846 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); 13847 Style.ColumnLimit = 60; 13848 verifyNoCrash( 13849 "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20" 13850 "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20" 13851 "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a", 13852 Style); 13853 } 13854 13855 TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 13856 verifyFormat("do {\n}"); 13857 verifyFormat("do {\n}\n" 13858 "f();"); 13859 verifyFormat("do {\n}\n" 13860 "wheeee(fun);"); 13861 verifyFormat("do {\n" 13862 " f();\n" 13863 "}"); 13864 } 13865 13866 TEST_F(FormatTest, IncorrectCodeMissingParens) { 13867 verifyFormat("if {\n foo;\n foo();\n}"); 13868 verifyFormat("switch {\n foo;\n foo();\n}"); 13869 verifyIncompleteFormat("for {\n foo;\n foo();\n}"); 13870 verifyIncompleteFormat("ERROR: for target;"); 13871 verifyFormat("while {\n foo;\n foo();\n}"); 13872 verifyFormat("do {\n foo;\n foo();\n} while;"); 13873 } 13874 13875 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 13876 verifyIncompleteFormat("namespace {\n" 13877 "class Foo { Foo (\n" 13878 "};\n" 13879 "} // namespace"); 13880 } 13881 13882 TEST_F(FormatTest, IncorrectCodeErrorDetection) { 13883 verifyFormat("{\n" 13884 " {\n" 13885 " }", 13886 "{\n" 13887 "{\n" 13888 "}"); 13889 verifyFormat("{\n" 13890 " {\n" 13891 " }", 13892 "{\n" 13893 " {\n" 13894 "}"); 13895 verifyFormat("{\n" 13896 " {\n" 13897 " }"); 13898 verifyFormat("{\n" 13899 " {\n" 13900 " }\n" 13901 "}\n" 13902 "}", 13903 "{\n" 13904 " {\n" 13905 " }\n" 13906 " }\n" 13907 "}"); 13908 13909 verifyFormat("{\n" 13910 " {\n" 13911 " breakme(\n" 13912 " qwe);\n" 13913 " }", 13914 "{\n" 13915 " {\n" 13916 " breakme(qwe);\n" 13917 "}", 13918 getLLVMStyleWithColumns(10)); 13919 } 13920 13921 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 13922 verifyFormat("int x = {\n" 13923 " avariable,\n" 13924 " b(alongervariable)};", 13925 getLLVMStyleWithColumns(25)); 13926 } 13927 13928 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 13929 verifyFormat("return (a)(b){1, 2, 3};"); 13930 } 13931 13932 TEST_F(FormatTest, LayoutCxx11BraceInitializers) { 13933 verifyFormat("vector<int> x{1, 2, 3, 4};"); 13934 verifyFormat("vector<int> x{\n" 13935 " 1,\n" 13936 " 2,\n" 13937 " 3,\n" 13938 " 4,\n" 13939 "};"); 13940 verifyFormat("vector<T> x{{}, {}, {}, {}};"); 13941 verifyFormat("f({1, 2});"); 13942 verifyFormat("auto v = Foo{-1};"); 13943 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});"); 13944 verifyFormat("Class::Class : member{1, 2, 3} {}"); 13945 verifyFormat("new vector<int>{1, 2, 3};"); 13946 verifyFormat("new int[3]{1, 2, 3};"); 13947 verifyFormat("new int{1};"); 13948 verifyFormat("return {arg1, arg2};"); 13949 verifyFormat("return {arg1, SomeType{parameter}};"); 13950 verifyFormat("int count = set<int>{f(), g(), h()}.size();"); 13951 verifyFormat("new T{arg1, arg2};"); 13952 verifyFormat("f(MyMap[{composite, key}]);"); 13953 verifyFormat("class Class {\n" 13954 " T member = {arg1, arg2};\n" 13955 "};"); 13956 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};"); 13957 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 13958 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 13959 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");"); 13960 verifyFormat("int a = std::is_integral<int>{} + 0;"); 13961 13962 verifyFormat("int foo(int i) { return fo1{}(i); }"); 13963 verifyFormat("int foo(int i) { return fo1{}(i); }"); 13964 verifyFormat("auto i = decltype(x){};"); 13965 verifyFormat("auto i = typeof(x){};"); 13966 verifyFormat("auto i = _Atomic(x){};"); 13967 verifyFormat("std::vector<int> v = {1, 0 /* comment */};"); 13968 verifyFormat("Node n{1, Node{1000}, //\n" 13969 " 2};"); 13970 verifyFormat("Aaaa aaaaaaa{\n" 13971 " {\n" 13972 " aaaa,\n" 13973 " },\n" 13974 "};"); 13975 verifyFormat("class C : public D {\n" 13976 " SomeClass SC{2};\n" 13977 "};"); 13978 verifyFormat("class C : public A {\n" 13979 " class D : public B {\n" 13980 " void f() { int i{2}; }\n" 13981 " };\n" 13982 "};"); 13983 verifyFormat("#define A {a, a},"); 13984 // Don't confuse braced list initializers with compound statements. 13985 verifyFormat( 13986 "class A {\n" 13987 " A() : a{} {}\n" 13988 " A() : Base<int>{} {}\n" 13989 " A() : Base<Foo<int>>{} {}\n" 13990 " A(int b) : b(b) {}\n" 13991 " A(int a, int b) : a(a), bs{{bs...}} { f(); }\n" 13992 " int a, b;\n" 13993 " explicit Expr(const Scalar<Result> &x) : u{Constant<Result>{x}} {}\n" 13994 " explicit Expr(Scalar<Result> &&x) : u{Constant<Result>{std::move(x)}} " 13995 "{}\n" 13996 "};"); 13997 13998 // Avoid breaking between equal sign and opening brace 13999 FormatStyle AvoidBreakingFirstArgument = getLLVMStyle(); 14000 AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200; 14001 verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n" 14002 " {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n" 14003 " {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n" 14004 " {\"ccccccccccccccccccccc\", 2}};", 14005 AvoidBreakingFirstArgument); 14006 14007 // Binpacking only if there is no trailing comma 14008 verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n" 14009 " cccccccccc, dddddddddd};", 14010 getLLVMStyleWithColumns(50)); 14011 verifyFormat("const Aaaaaa aaaaa = {\n" 14012 " aaaaaaaaaaa,\n" 14013 " bbbbbbbbbbb,\n" 14014 " ccccccccccc,\n" 14015 " ddddddddddd,\n" 14016 "};", 14017 getLLVMStyleWithColumns(50)); 14018 14019 // Cases where distinguising braced lists and blocks is hard. 14020 verifyFormat("vector<int> v{12} GUARDED_BY(mutex);"); 14021 verifyFormat("void f() {\n" 14022 " return; // comment\n" 14023 "}\n" 14024 "SomeType t;"); 14025 verifyFormat("void f() {\n" 14026 " if (a) {\n" 14027 " f();\n" 14028 " }\n" 14029 "}\n" 14030 "SomeType t;"); 14031 14032 // In combination with BinPackArguments = false. 14033 FormatStyle NoBinPacking = getLLVMStyle(); 14034 NoBinPacking.BinPackArguments = false; 14035 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n" 14036 " bbbbb,\n" 14037 " ccccc,\n" 14038 " ddddd,\n" 14039 " eeeee,\n" 14040 " ffffff,\n" 14041 " ggggg,\n" 14042 " hhhhhh,\n" 14043 " iiiiii,\n" 14044 " jjjjjj,\n" 14045 " kkkkkk};", 14046 NoBinPacking); 14047 verifyFormat("const Aaaaaa aaaaa = {\n" 14048 " aaaaa,\n" 14049 " bbbbb,\n" 14050 " ccccc,\n" 14051 " ddddd,\n" 14052 " eeeee,\n" 14053 " ffffff,\n" 14054 " ggggg,\n" 14055 " hhhhhh,\n" 14056 " iiiiii,\n" 14057 " jjjjjj,\n" 14058 " kkkkkk,\n" 14059 "};", 14060 NoBinPacking); 14061 verifyFormat( 14062 "const Aaaaaa aaaaa = {\n" 14063 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n" 14064 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n" 14065 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n" 14066 "};", 14067 NoBinPacking); 14068 14069 NoBinPacking.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 14070 verifyFormat("static uint8 CddDp83848Reg[] = {\n" 14071 " CDDDP83848_BMCR_REGISTER,\n" 14072 " CDDDP83848_BMSR_REGISTER,\n" 14073 " CDDDP83848_RBR_REGISTER};", 14074 "static uint8 CddDp83848Reg[] = {CDDDP83848_BMCR_REGISTER,\n" 14075 " CDDDP83848_BMSR_REGISTER,\n" 14076 " CDDDP83848_RBR_REGISTER};", 14077 NoBinPacking); 14078 14079 // FIXME: The alignment of these trailing comments might be bad. Then again, 14080 // this might be utterly useless in real code. 14081 verifyFormat("Constructor::Constructor()\n" 14082 " : some_value{ //\n" 14083 " aaaaaaa, //\n" 14084 " bbbbbbb} {}"); 14085 14086 // In braced lists, the first comment is always assumed to belong to the 14087 // first element. Thus, it can be moved to the next or previous line as 14088 // appropriate. 14089 verifyFormat("function({// First element:\n" 14090 " 1,\n" 14091 " // Second element:\n" 14092 " 2});", 14093 "function({\n" 14094 " // First element:\n" 14095 " 1,\n" 14096 " // Second element:\n" 14097 " 2});"); 14098 verifyFormat("std::vector<int> MyNumbers{\n" 14099 " // First element:\n" 14100 " 1,\n" 14101 " // Second element:\n" 14102 " 2};", 14103 "std::vector<int> MyNumbers{// First element:\n" 14104 " 1,\n" 14105 " // Second element:\n" 14106 " 2};", 14107 getLLVMStyleWithColumns(30)); 14108 // A trailing comma should still lead to an enforced line break and no 14109 // binpacking. 14110 verifyFormat("vector<int> SomeVector = {\n" 14111 " // aaa\n" 14112 " 1,\n" 14113 " 2,\n" 14114 "};", 14115 "vector<int> SomeVector = { // aaa\n" 14116 " 1, 2, };"); 14117 14118 // C++11 brace initializer list l-braces should not be treated any differently 14119 // when breaking before lambda bodies is enabled 14120 FormatStyle BreakBeforeLambdaBody = getLLVMStyle(); 14121 BreakBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 14122 BreakBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 14123 BreakBeforeLambdaBody.AlwaysBreakBeforeMultilineStrings = true; 14124 verifyFormat( 14125 "std::runtime_error{\n" 14126 " \"Long string which will force a break onto the next line...\"};", 14127 BreakBeforeLambdaBody); 14128 14129 FormatStyle ExtraSpaces = getLLVMStyle(); 14130 ExtraSpaces.Cpp11BracedListStyle = false; 14131 ExtraSpaces.ColumnLimit = 75; 14132 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces); 14133 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces); 14134 verifyFormat("f({ 1, 2 });", ExtraSpaces); 14135 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces); 14136 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces); 14137 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces); 14138 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces); 14139 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces); 14140 verifyFormat("return { arg1, arg2 };", ExtraSpaces); 14141 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces); 14142 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces); 14143 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces); 14144 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces); 14145 verifyFormat("class Class {\n" 14146 " T member = { arg1, arg2 };\n" 14147 "};", 14148 ExtraSpaces); 14149 verifyFormat( 14150 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 14151 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n" 14152 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 14153 " bbbbbbbbbbbbbbbbbbbb, bbbbb };", 14154 ExtraSpaces); 14155 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces); 14156 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });", 14157 ExtraSpaces); 14158 verifyFormat( 14159 "someFunction(OtherParam,\n" 14160 " BracedList{ // comment 1 (Forcing interesting break)\n" 14161 " param1, param2,\n" 14162 " // comment 2\n" 14163 " param3, param4 });", 14164 ExtraSpaces); 14165 verifyFormat( 14166 "std::this_thread::sleep_for(\n" 14167 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);", 14168 ExtraSpaces); 14169 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n" 14170 " aaaaaaa,\n" 14171 " aaaaaaaaaa,\n" 14172 " aaaaa,\n" 14173 " aaaaaaaaaaaaaaa,\n" 14174 " aaa,\n" 14175 " aaaaaaaaaa,\n" 14176 " a,\n" 14177 " aaaaaaaaaaaaaaaaaaaaa,\n" 14178 " aaaaaaaaaaaa,\n" 14179 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n" 14180 " aaaaaaa,\n" 14181 " a};"); 14182 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces); 14183 verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces); 14184 verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces); 14185 14186 // Avoid breaking between initializer/equal sign and opening brace 14187 ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200; 14188 verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n" 14189 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n" 14190 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n" 14191 " { \"ccccccccccccccccccccc\", 2 }\n" 14192 "};", 14193 ExtraSpaces); 14194 verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n" 14195 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n" 14196 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n" 14197 " { \"ccccccccccccccccccccc\", 2 }\n" 14198 "};", 14199 ExtraSpaces); 14200 14201 FormatStyle SpaceBeforeBrace = getLLVMStyle(); 14202 SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true; 14203 verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace); 14204 verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace); 14205 14206 FormatStyle SpaceBetweenBraces = getLLVMStyle(); 14207 SpaceBetweenBraces.SpacesInAngles = FormatStyle::SIAS_Always; 14208 SpaceBetweenBraces.SpacesInParens = FormatStyle::SIPO_Custom; 14209 SpaceBetweenBraces.SpacesInParensOptions.Other = true; 14210 SpaceBetweenBraces.SpacesInSquareBrackets = true; 14211 verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces); 14212 verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces); 14213 verifyFormat("vector< int > x{ // comment 1\n" 14214 " 1, 2, 3, 4 };", 14215 SpaceBetweenBraces); 14216 SpaceBetweenBraces.ColumnLimit = 20; 14217 verifyFormat("vector< int > x{\n" 14218 " 1, 2, 3, 4 };", 14219 "vector<int>x{1,2,3,4};", SpaceBetweenBraces); 14220 SpaceBetweenBraces.ColumnLimit = 24; 14221 verifyFormat("vector< int > x{ 1, 2,\n" 14222 " 3, 4 };", 14223 "vector<int>x{1,2,3,4};", SpaceBetweenBraces); 14224 verifyFormat("vector< int > x{\n" 14225 " 1,\n" 14226 " 2,\n" 14227 " 3,\n" 14228 " 4,\n" 14229 "};", 14230 "vector<int>x{1,2,3,4,};", SpaceBetweenBraces); 14231 verifyFormat("vector< int > x{};", SpaceBetweenBraces); 14232 SpaceBetweenBraces.SpacesInParens = FormatStyle::SIPO_Custom; 14233 SpaceBetweenBraces.SpacesInParensOptions.InEmptyParentheses = true; 14234 verifyFormat("vector< int > x{ };", SpaceBetweenBraces); 14235 } 14236 14237 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { 14238 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 14239 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 14240 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 14241 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 14242 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 14243 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 14244 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n" 14245 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 14246 " 1, 22, 333, 4444, 55555, //\n" 14247 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 14248 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 14249 verifyFormat( 14250 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 14251 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 14252 " 1, 22, 333, 4444, 55555, 666666, // comment\n" 14253 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 14254 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 14255 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 14256 " 7777777};"); 14257 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 14258 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 14259 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 14260 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 14261 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 14262 " // Separating comment.\n" 14263 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 14264 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 14265 " // Leading comment\n" 14266 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 14267 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 14268 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 14269 " 1, 1, 1, 1};", 14270 getLLVMStyleWithColumns(39)); 14271 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 14272 " 1, 1, 1, 1};", 14273 getLLVMStyleWithColumns(38)); 14274 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n" 14275 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};", 14276 getLLVMStyleWithColumns(43)); 14277 verifyFormat( 14278 "static unsigned SomeValues[10][3] = {\n" 14279 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n" 14280 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};"); 14281 verifyFormat("static auto fields = new vector<string>{\n" 14282 " \"aaaaaaaaaaaaa\",\n" 14283 " \"aaaaaaaaaaaaa\",\n" 14284 " \"aaaaaaaaaaaa\",\n" 14285 " \"aaaaaaaaaaaaaa\",\n" 14286 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 14287 " \"aaaaaaaaaaaa\",\n" 14288 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 14289 "};"); 14290 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};"); 14291 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n" 14292 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n" 14293 " 3, cccccccccccccccccccccc};", 14294 getLLVMStyleWithColumns(60)); 14295 14296 // Trailing commas. 14297 verifyFormat("vector<int> x = {\n" 14298 " 1, 1, 1, 1, 1, 1, 1, 1,\n" 14299 "};", 14300 getLLVMStyleWithColumns(39)); 14301 verifyFormat("vector<int> x = {\n" 14302 " 1, 1, 1, 1, 1, 1, 1, 1, //\n" 14303 "};", 14304 getLLVMStyleWithColumns(39)); 14305 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 14306 " 1, 1, 1, 1,\n" 14307 " /**/ /**/};", 14308 getLLVMStyleWithColumns(39)); 14309 14310 // Trailing comment in the first line. 14311 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n" 14312 " 1111111111, 2222222222, 33333333333, 4444444444, //\n" 14313 " 111111111, 222222222, 3333333333, 444444444, //\n" 14314 " 11111111, 22222222, 333333333, 44444444};"); 14315 // Trailing comment in the last line. 14316 verifyFormat("int aaaaa[] = {\n" 14317 " 1, 2, 3, // comment\n" 14318 " 4, 5, 6 // comment\n" 14319 "};"); 14320 14321 // With nested lists, we should either format one item per line or all nested 14322 // lists one on line. 14323 // FIXME: For some nested lists, we can do better. 14324 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n" 14325 " {aaaaaaaaaaaaaaaaaaa},\n" 14326 " {aaaaaaaaaaaaaaaaaaaaa},\n" 14327 " {aaaaaaaaaaaaaaaaa}};", 14328 getLLVMStyleWithColumns(60)); 14329 verifyFormat( 14330 "SomeStruct my_struct_array = {\n" 14331 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n" 14332 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n" 14333 " {aaa, aaa},\n" 14334 " {aaa, aaa},\n" 14335 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n" 14336 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n" 14337 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};"); 14338 14339 // No column layout should be used here. 14340 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n" 14341 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};"); 14342 14343 verifyNoCrash("a<,"); 14344 14345 // No braced initializer here. 14346 verifyFormat("void f() {\n" 14347 " struct Dummy {};\n" 14348 " f(v);\n" 14349 "}"); 14350 verifyFormat("void foo() {\n" 14351 " { // asdf\n" 14352 " {\n" 14353 " int a;\n" 14354 " }\n" 14355 " }\n" 14356 " {\n" 14357 " {\n" 14358 " int b;\n" 14359 " }\n" 14360 " }\n" 14361 "}"); 14362 verifyFormat("namespace n {\n" 14363 "void foo() {\n" 14364 " {\n" 14365 " {\n" 14366 " statement();\n" 14367 " if (false) {\n" 14368 " }\n" 14369 " }\n" 14370 " }\n" 14371 " {\n" 14372 " }\n" 14373 "}\n" 14374 "} // namespace n"); 14375 14376 // Long lists should be formatted in columns even if they are nested. 14377 verifyFormat( 14378 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n" 14379 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 14380 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 14381 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 14382 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 14383 " 1, 22, 333, 4444, 55555, 666666, 7777777});"); 14384 14385 // Allow "single-column" layout even if that violates the column limit. There 14386 // isn't going to be a better way. 14387 verifyFormat("std::vector<int> a = {\n" 14388 " aaaaaaaa,\n" 14389 " aaaaaaaa,\n" 14390 " aaaaaaaa,\n" 14391 " aaaaaaaa,\n" 14392 " aaaaaaaaaa,\n" 14393 " aaaaaaaa,\n" 14394 " aaaaaaaaaaaaaaaaaaaaaaaaaaa};", 14395 getLLVMStyleWithColumns(30)); 14396 verifyFormat("vector<int> aaaa = {\n" 14397 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 14398 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 14399 " aaaaaa.aaaaaaa,\n" 14400 " aaaaaa.aaaaaaa,\n" 14401 " aaaaaa.aaaaaaa,\n" 14402 " aaaaaa.aaaaaaa,\n" 14403 "};"); 14404 14405 // Don't create hanging lists. 14406 verifyFormat("someFunction(Param, {List1, List2,\n" 14407 " List3});", 14408 getLLVMStyleWithColumns(35)); 14409 verifyFormat("someFunction(Param, Param,\n" 14410 " {List1, List2,\n" 14411 " List3});", 14412 getLLVMStyleWithColumns(35)); 14413 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n" 14414 " aaaaaaaaaaaaaaaaaaaaaaa);"); 14415 14416 // No possible column formats, don't want the optimal paths penalized. 14417 verifyFormat( 14418 "waarudo::unit desk = {\n" 14419 " .s = \"desk\", .p = p, .b = [] { return w::r{3, 10} * w::m; }};"); 14420 verifyFormat("SomeType something1([](const Input &i) -> Output { return " 14421 "Output{1, 2}; },\n" 14422 " [](const Input &i) -> Output { return " 14423 "Output{1, 2}; });"); 14424 FormatStyle NoBinPacking = getLLVMStyle(); 14425 NoBinPacking.BinPackParameters = FormatStyle::BPPS_OnePerLine; 14426 verifyFormat("waarudo::unit desk = {\n" 14427 " .s = \"desk\", .p = p, .b = [] { return w::r{3, 10, 1, 1, " 14428 "1, 1} * w::m; }};", 14429 NoBinPacking); 14430 } 14431 14432 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 14433 FormatStyle DoNotMerge = getLLVMStyle(); 14434 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 14435 14436 verifyFormat("void f() { return 42; }"); 14437 verifyFormat("void f() {\n" 14438 " return 42;\n" 14439 "}", 14440 DoNotMerge); 14441 verifyFormat("void f() {\n" 14442 " // Comment\n" 14443 "}"); 14444 verifyFormat("{\n" 14445 "#error {\n" 14446 " int a;\n" 14447 "}"); 14448 verifyFormat("{\n" 14449 " int a;\n" 14450 "#error {\n" 14451 "}"); 14452 verifyFormat("void f() {} // comment"); 14453 verifyFormat("void f() { int a; } // comment"); 14454 verifyFormat("void f() {\n" 14455 "} // comment", 14456 DoNotMerge); 14457 verifyFormat("void f() {\n" 14458 " int a;\n" 14459 "} // comment", 14460 DoNotMerge); 14461 verifyFormat("void f() {\n" 14462 "} // comment", 14463 getLLVMStyleWithColumns(15)); 14464 14465 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 14466 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 14467 14468 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 14469 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 14470 verifyGoogleFormat("class C {\n" 14471 " C()\n" 14472 " : iiiiiiii(nullptr),\n" 14473 " kkkkkkk(nullptr),\n" 14474 " mmmmmmm(nullptr),\n" 14475 " nnnnnnn(nullptr) {}\n" 14476 "};"); 14477 14478 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0); 14479 verifyFormat("A() : b(0) {}", "A():b(0){}", NoColumnLimit); 14480 verifyFormat("class C {\n" 14481 " A() : b(0) {}\n" 14482 "};", 14483 "class C{A():b(0){}};", NoColumnLimit); 14484 verifyFormat("A()\n" 14485 " : b(0) {\n" 14486 "}", 14487 "A()\n:b(0)\n{\n}", NoColumnLimit); 14488 14489 FormatStyle NoColumnLimitWrapAfterFunction = NoColumnLimit; 14490 NoColumnLimitWrapAfterFunction.BreakBeforeBraces = FormatStyle::BS_Custom; 14491 NoColumnLimitWrapAfterFunction.BraceWrapping.AfterFunction = true; 14492 verifyFormat("class C {\n" 14493 "#pragma foo\n" 14494 " int foo { return 0; }\n" 14495 "};", 14496 NoColumnLimitWrapAfterFunction); 14497 verifyFormat("class C {\n" 14498 "#pragma foo\n" 14499 " void foo {}\n" 14500 "};", 14501 NoColumnLimitWrapAfterFunction); 14502 14503 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit; 14504 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine = 14505 FormatStyle::SFS_None; 14506 verifyFormat("A() : b(0) {\n" 14507 "}", 14508 DoNotMergeNoColumnLimit); 14509 verifyNoChange("A()\n" 14510 " : b(0) {\n" 14511 "}", 14512 DoNotMergeNoColumnLimit); 14513 verifyFormat("A()\n" 14514 " : b(0) {\n" 14515 "}", 14516 "A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit); 14517 14518 verifyFormat("#define A \\\n" 14519 " void f() { \\\n" 14520 " int i; \\\n" 14521 " }", 14522 getLLVMStyleWithColumns(20)); 14523 verifyFormat("#define A \\\n" 14524 " void f() { int i; }", 14525 getLLVMStyleWithColumns(21)); 14526 verifyFormat("#define A \\\n" 14527 " void f() { \\\n" 14528 " int i; \\\n" 14529 " } \\\n" 14530 " int j;", 14531 getLLVMStyleWithColumns(22)); 14532 verifyFormat("#define A \\\n" 14533 " void f() { int i; } \\\n" 14534 " int j;", 14535 getLLVMStyleWithColumns(23)); 14536 14537 verifyFormat( 14538 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 14539 " aaaaaaaaaaaaaaaaaa,\n" 14540 " aaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {}"); 14541 14542 constexpr StringRef Code{"void foo() { /* Empty */ }"}; 14543 verifyFormat(Code); 14544 verifyFormat(Code, "void foo() { /* Empty */\n" 14545 "}"); 14546 verifyFormat(Code, "void foo() {\n" 14547 "/* Empty */\n" 14548 "}"); 14549 } 14550 14551 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) { 14552 FormatStyle MergeEmptyOnly = getLLVMStyle(); 14553 MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 14554 verifyFormat("class C {\n" 14555 " int f() {}\n" 14556 "};", 14557 MergeEmptyOnly); 14558 verifyFormat("class C {\n" 14559 " int f() {\n" 14560 " return 42;\n" 14561 " }\n" 14562 "};", 14563 MergeEmptyOnly); 14564 verifyFormat("int f() {}", MergeEmptyOnly); 14565 verifyFormat("int f() {\n" 14566 " return 42;\n" 14567 "}", 14568 MergeEmptyOnly); 14569 14570 // Also verify behavior when BraceWrapping.AfterFunction = true 14571 MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 14572 MergeEmptyOnly.BraceWrapping.AfterFunction = true; 14573 verifyFormat("int f() {}", MergeEmptyOnly); 14574 verifyFormat("class C {\n" 14575 " int f() {}\n" 14576 "};", 14577 MergeEmptyOnly); 14578 } 14579 14580 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) { 14581 FormatStyle MergeInlineOnly = getLLVMStyle(); 14582 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 14583 verifyFormat("class C {\n" 14584 " int f() { return 42; }\n" 14585 "};", 14586 MergeInlineOnly); 14587 verifyFormat("int f() {\n" 14588 " return 42;\n" 14589 "}", 14590 MergeInlineOnly); 14591 14592 // SFS_Inline implies SFS_Empty 14593 verifyFormat("class C {\n" 14594 " int f() {}\n" 14595 "};", 14596 MergeInlineOnly); 14597 verifyFormat("int f() {}", MergeInlineOnly); 14598 // https://llvm.org/PR54147 14599 verifyFormat("auto lambda = []() {\n" 14600 " // comment\n" 14601 " f();\n" 14602 " g();\n" 14603 "};", 14604 MergeInlineOnly); 14605 14606 verifyFormat("class C {\n" 14607 "#ifdef A\n" 14608 " int f() { return 42; }\n" 14609 "#endif\n" 14610 "};", 14611 MergeInlineOnly); 14612 14613 verifyFormat("struct S {\n" 14614 "// comment\n" 14615 "#ifdef FOO\n" 14616 " int foo() { bar(); }\n" 14617 "#endif\n" 14618 "};", 14619 MergeInlineOnly); 14620 14621 // Also verify behavior when BraceWrapping.AfterFunction = true 14622 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 14623 MergeInlineOnly.BraceWrapping.AfterFunction = true; 14624 verifyFormat("class C {\n" 14625 " int f() { return 42; }\n" 14626 "};", 14627 MergeInlineOnly); 14628 verifyFormat("int f()\n" 14629 "{\n" 14630 " return 42;\n" 14631 "}", 14632 MergeInlineOnly); 14633 14634 // SFS_Inline implies SFS_Empty 14635 verifyFormat("int f() {}", MergeInlineOnly); 14636 verifyFormat("class C {\n" 14637 " int f() {}\n" 14638 "};", 14639 MergeInlineOnly); 14640 14641 MergeInlineOnly.BraceWrapping.AfterClass = true; 14642 MergeInlineOnly.BraceWrapping.AfterStruct = true; 14643 verifyFormat("class C\n" 14644 "{\n" 14645 " int f() { return 42; }\n" 14646 "};", 14647 MergeInlineOnly); 14648 verifyFormat("struct C\n" 14649 "{\n" 14650 " int f() { return 42; }\n" 14651 "};", 14652 MergeInlineOnly); 14653 verifyFormat("int f()\n" 14654 "{\n" 14655 " return 42;\n" 14656 "}", 14657 MergeInlineOnly); 14658 verifyFormat("int f() {}", MergeInlineOnly); 14659 verifyFormat("class C\n" 14660 "{\n" 14661 " int f() { return 42; }\n" 14662 "};", 14663 MergeInlineOnly); 14664 verifyFormat("struct C\n" 14665 "{\n" 14666 " int f() { return 42; }\n" 14667 "};", 14668 MergeInlineOnly); 14669 verifyFormat("struct C\n" 14670 "// comment\n" 14671 "/* comment */\n" 14672 "// comment\n" 14673 "{\n" 14674 " int f() { return 42; }\n" 14675 "};", 14676 MergeInlineOnly); 14677 verifyFormat("/* comment */ struct C\n" 14678 "{\n" 14679 " int f() { return 42; }\n" 14680 "};", 14681 MergeInlineOnly); 14682 } 14683 14684 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) { 14685 FormatStyle MergeInlineOnly = getLLVMStyle(); 14686 MergeInlineOnly.AllowShortFunctionsOnASingleLine = 14687 FormatStyle::SFS_InlineOnly; 14688 verifyFormat("class C {\n" 14689 " int f() { return 42; }\n" 14690 "};", 14691 MergeInlineOnly); 14692 verifyFormat("int f() {\n" 14693 " return 42;\n" 14694 "}", 14695 MergeInlineOnly); 14696 14697 // SFS_InlineOnly does not imply SFS_Empty 14698 verifyFormat("class C {\n" 14699 " int f() {}\n" 14700 "};", 14701 MergeInlineOnly); 14702 verifyFormat("int f() {\n" 14703 "}", 14704 MergeInlineOnly); 14705 14706 // Also verify behavior when BraceWrapping.AfterFunction = true 14707 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 14708 MergeInlineOnly.BraceWrapping.AfterFunction = true; 14709 verifyFormat("class C {\n" 14710 " int f() { return 42; }\n" 14711 "};", 14712 MergeInlineOnly); 14713 verifyFormat("int f()\n" 14714 "{\n" 14715 " return 42;\n" 14716 "}", 14717 MergeInlineOnly); 14718 14719 // SFS_InlineOnly does not imply SFS_Empty 14720 verifyFormat("int f()\n" 14721 "{\n" 14722 "}", 14723 MergeInlineOnly); 14724 verifyFormat("class C {\n" 14725 " int f() {}\n" 14726 "};", 14727 MergeInlineOnly); 14728 } 14729 14730 TEST_F(FormatTest, SplitEmptyFunction) { 14731 FormatStyle Style = getLLVMStyleWithColumns(40); 14732 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 14733 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 14734 Style.BraceWrapping.AfterFunction = true; 14735 Style.BraceWrapping.SplitEmptyFunction = false; 14736 14737 verifyFormat("int f()\n" 14738 "{}", 14739 Style); 14740 verifyFormat("int f()\n" 14741 "{\n" 14742 " return 42;\n" 14743 "}", 14744 Style); 14745 verifyFormat("int f()\n" 14746 "{\n" 14747 " // some comment\n" 14748 "}", 14749 Style); 14750 14751 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 14752 verifyFormat("int f() {}", Style); 14753 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 14754 "{}", 14755 Style); 14756 verifyFormat("int f()\n" 14757 "{\n" 14758 " return 0;\n" 14759 "}", 14760 Style); 14761 14762 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 14763 verifyFormat("class Foo {\n" 14764 " int f() {}\n" 14765 "};", 14766 Style); 14767 verifyFormat("class Foo {\n" 14768 " int f() { return 0; }\n" 14769 "};", 14770 Style); 14771 verifyFormat("class Foo {\n" 14772 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 14773 " {}\n" 14774 "};", 14775 Style); 14776 verifyFormat("class Foo {\n" 14777 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 14778 " {\n" 14779 " return 0;\n" 14780 " }\n" 14781 "};", 14782 Style); 14783 14784 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 14785 verifyFormat("int f() {}", Style); 14786 verifyFormat("int f() { return 0; }", Style); 14787 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 14788 "{}", 14789 Style); 14790 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 14791 "{\n" 14792 " return 0;\n" 14793 "}", 14794 Style); 14795 } 14796 14797 TEST_F(FormatTest, SplitEmptyFunctionButNotRecord) { 14798 FormatStyle Style = getLLVMStyleWithColumns(40); 14799 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 14800 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 14801 Style.BraceWrapping.AfterFunction = true; 14802 Style.BraceWrapping.SplitEmptyFunction = true; 14803 Style.BraceWrapping.SplitEmptyRecord = false; 14804 14805 verifyFormat("class C {};", Style); 14806 verifyFormat("struct C {};", Style); 14807 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 14808 " int bbbbbbbbbbbbbbbbbbbbbbbb)\n" 14809 "{\n" 14810 "}", 14811 Style); 14812 verifyFormat("class C {\n" 14813 " C()\n" 14814 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa(),\n" 14815 " bbbbbbbbbbbbbbbbbbb()\n" 14816 " {\n" 14817 " }\n" 14818 " void\n" 14819 " m(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 14820 " int bbbbbbbbbbbbbbbbbbbbbbbb)\n" 14821 " {\n" 14822 " }\n" 14823 "};", 14824 Style); 14825 } 14826 14827 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) { 14828 FormatStyle Style = getLLVMStyle(); 14829 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 14830 verifyFormat("#ifdef A\n" 14831 "int f() {}\n" 14832 "#else\n" 14833 "int g() {}\n" 14834 "#endif", 14835 Style); 14836 } 14837 14838 TEST_F(FormatTest, SplitEmptyClass) { 14839 FormatStyle Style = getLLVMStyle(); 14840 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 14841 Style.BraceWrapping.AfterClass = true; 14842 Style.BraceWrapping.SplitEmptyRecord = false; 14843 14844 verifyFormat("class Foo\n" 14845 "{};", 14846 Style); 14847 verifyFormat("/* something */ class Foo\n" 14848 "{};", 14849 Style); 14850 verifyFormat("template <typename X> class Foo\n" 14851 "{};", 14852 Style); 14853 verifyFormat("class Foo\n" 14854 "{\n" 14855 " Foo();\n" 14856 "};", 14857 Style); 14858 verifyFormat("typedef class Foo\n" 14859 "{\n" 14860 "} Foo_t;", 14861 Style); 14862 14863 Style.BraceWrapping.SplitEmptyRecord = true; 14864 Style.BraceWrapping.AfterStruct = true; 14865 verifyFormat("class rep\n" 14866 "{\n" 14867 "};", 14868 Style); 14869 verifyFormat("struct rep\n" 14870 "{\n" 14871 "};", 14872 Style); 14873 verifyFormat("template <typename T> class rep\n" 14874 "{\n" 14875 "};", 14876 Style); 14877 verifyFormat("template <typename T> struct rep\n" 14878 "{\n" 14879 "};", 14880 Style); 14881 verifyFormat("class rep\n" 14882 "{\n" 14883 " int x;\n" 14884 "};", 14885 Style); 14886 verifyFormat("struct rep\n" 14887 "{\n" 14888 " int x;\n" 14889 "};", 14890 Style); 14891 verifyFormat("template <typename T> class rep\n" 14892 "{\n" 14893 " int x;\n" 14894 "};", 14895 Style); 14896 verifyFormat("template <typename T> struct rep\n" 14897 "{\n" 14898 " int x;\n" 14899 "};", 14900 Style); 14901 verifyFormat("template <typename T> class rep // Foo\n" 14902 "{\n" 14903 " int x;\n" 14904 "};", 14905 Style); 14906 verifyFormat("template <typename T> struct rep // Bar\n" 14907 "{\n" 14908 " int x;\n" 14909 "};", 14910 Style); 14911 14912 verifyFormat("template <typename T> class rep<T>\n" 14913 "{\n" 14914 " int x;\n" 14915 "};", 14916 Style); 14917 14918 verifyFormat("template <typename T> class rep<std::complex<T>>\n" 14919 "{\n" 14920 " int x;\n" 14921 "};", 14922 Style); 14923 verifyFormat("template <typename T> class rep<std::complex<T>>\n" 14924 "{\n" 14925 "};", 14926 Style); 14927 14928 verifyFormat("#include \"stdint.h\"\n" 14929 "namespace rep {}", 14930 Style); 14931 verifyFormat("#include <stdint.h>\n" 14932 "namespace rep {}", 14933 Style); 14934 verifyFormat("#include <stdint.h>\n" 14935 "namespace rep {}", 14936 "#include <stdint.h>\n" 14937 "namespace rep {\n" 14938 "\n" 14939 "\n" 14940 "}", 14941 Style); 14942 } 14943 14944 TEST_F(FormatTest, SplitEmptyStruct) { 14945 FormatStyle Style = getLLVMStyle(); 14946 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 14947 Style.BraceWrapping.AfterStruct = true; 14948 Style.BraceWrapping.SplitEmptyRecord = false; 14949 14950 verifyFormat("struct Foo\n" 14951 "{};", 14952 Style); 14953 verifyFormat("/* something */ struct Foo\n" 14954 "{};", 14955 Style); 14956 verifyFormat("template <typename X> struct Foo\n" 14957 "{};", 14958 Style); 14959 verifyFormat("struct Foo\n" 14960 "{\n" 14961 " Foo();\n" 14962 "};", 14963 Style); 14964 verifyFormat("typedef struct Foo\n" 14965 "{\n" 14966 "} Foo_t;", 14967 Style); 14968 // typedef struct Bar {} Bar_t; 14969 } 14970 14971 TEST_F(FormatTest, SplitEmptyUnion) { 14972 FormatStyle Style = getLLVMStyle(); 14973 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 14974 Style.BraceWrapping.AfterUnion = true; 14975 Style.BraceWrapping.SplitEmptyRecord = false; 14976 14977 verifyFormat("union Foo\n" 14978 "{};", 14979 Style); 14980 verifyFormat("/* something */ union Foo\n" 14981 "{};", 14982 Style); 14983 verifyFormat("union Foo\n" 14984 "{\n" 14985 " A,\n" 14986 "};", 14987 Style); 14988 verifyFormat("typedef union Foo\n" 14989 "{\n" 14990 "} Foo_t;", 14991 Style); 14992 } 14993 14994 TEST_F(FormatTest, SplitEmptyNamespace) { 14995 FormatStyle Style = getLLVMStyle(); 14996 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 14997 Style.BraceWrapping.AfterNamespace = true; 14998 Style.BraceWrapping.SplitEmptyNamespace = false; 14999 15000 verifyFormat("namespace Foo\n" 15001 "{};", 15002 Style); 15003 verifyFormat("/* something */ namespace Foo\n" 15004 "{};", 15005 Style); 15006 verifyFormat("inline namespace Foo\n" 15007 "{};", 15008 Style); 15009 verifyFormat("/* something */ inline namespace Foo\n" 15010 "{};", 15011 Style); 15012 verifyFormat("export namespace Foo\n" 15013 "{};", 15014 Style); 15015 verifyFormat("namespace Foo\n" 15016 "{\n" 15017 "void Bar();\n" 15018 "};", 15019 Style); 15020 } 15021 15022 TEST_F(FormatTest, NeverMergeShortRecords) { 15023 FormatStyle Style = getLLVMStyle(); 15024 15025 verifyFormat("class Foo {\n" 15026 " Foo();\n" 15027 "};", 15028 Style); 15029 verifyFormat("typedef class Foo {\n" 15030 " Foo();\n" 15031 "} Foo_t;", 15032 Style); 15033 verifyFormat("struct Foo {\n" 15034 " Foo();\n" 15035 "};", 15036 Style); 15037 verifyFormat("typedef struct Foo {\n" 15038 " Foo();\n" 15039 "} Foo_t;", 15040 Style); 15041 verifyFormat("union Foo {\n" 15042 " A,\n" 15043 "};", 15044 Style); 15045 verifyFormat("typedef union Foo {\n" 15046 " A,\n" 15047 "} Foo_t;", 15048 Style); 15049 verifyFormat("namespace Foo {\n" 15050 "void Bar();\n" 15051 "};", 15052 Style); 15053 15054 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 15055 Style.BraceWrapping.AfterClass = true; 15056 Style.BraceWrapping.AfterStruct = true; 15057 Style.BraceWrapping.AfterUnion = true; 15058 Style.BraceWrapping.AfterNamespace = true; 15059 verifyFormat("class Foo\n" 15060 "{\n" 15061 " Foo();\n" 15062 "};", 15063 Style); 15064 verifyFormat("typedef class Foo\n" 15065 "{\n" 15066 " Foo();\n" 15067 "} Foo_t;", 15068 Style); 15069 verifyFormat("struct Foo\n" 15070 "{\n" 15071 " Foo();\n" 15072 "};", 15073 Style); 15074 verifyFormat("typedef struct Foo\n" 15075 "{\n" 15076 " Foo();\n" 15077 "} Foo_t;", 15078 Style); 15079 verifyFormat("union Foo\n" 15080 "{\n" 15081 " A,\n" 15082 "};", 15083 Style); 15084 verifyFormat("typedef union Foo\n" 15085 "{\n" 15086 " A,\n" 15087 "} Foo_t;", 15088 Style); 15089 verifyFormat("namespace Foo\n" 15090 "{\n" 15091 "void Bar();\n" 15092 "};", 15093 Style); 15094 } 15095 15096 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 15097 // Elaborate type variable declarations. 15098 verifyFormat("struct foo a = {bar};\nint n;"); 15099 verifyFormat("class foo a = {bar};\nint n;"); 15100 verifyFormat("union foo a = {bar};\nint n;"); 15101 15102 // Elaborate types inside function definitions. 15103 verifyFormat("struct foo f() {}\nint n;"); 15104 verifyFormat("class foo f() {}\nint n;"); 15105 verifyFormat("union foo f() {}\nint n;"); 15106 15107 // Templates. 15108 verifyFormat("template <class X> void f() {}\nint n;"); 15109 verifyFormat("template <struct X> void f() {}\nint n;"); 15110 verifyFormat("template <union X> void f() {}\nint n;"); 15111 15112 // Actual definitions... 15113 verifyFormat("struct {\n} n;"); 15114 verifyFormat( 15115 "template <template <class T, class Y>, class Z> class X {\n} n;"); 15116 verifyFormat("union Z {\n int n;\n} x;"); 15117 verifyFormat("class MACRO Z {\n} n;"); 15118 verifyFormat("class MACRO(X) Z {\n} n;"); 15119 verifyFormat("class __attribute__((X)) Z {\n} n;"); 15120 verifyFormat("class __declspec(X) Z {\n} n;"); 15121 verifyFormat("class A##B##C {\n} n;"); 15122 verifyFormat("class alignas(16) Z {\n} n;"); 15123 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;"); 15124 verifyFormat("class MACROA MACRO(X) Z {\n} n;"); 15125 15126 // Redefinition from nested context: 15127 verifyFormat("class A::B::C {\n} n;"); 15128 15129 // Template definitions. 15130 verifyFormat( 15131 "template <typename F>\n" 15132 "Matcher(const Matcher<F> &Other,\n" 15133 " typename enable_if_c<is_base_of<F, T>::value &&\n" 15134 " !is_same<F, T>::value>::type * = 0)\n" 15135 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}"); 15136 15137 // FIXME: This is still incorrectly handled at the formatter side. 15138 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};"); 15139 verifyFormat("int i = SomeFunction(a<b, a> b);"); 15140 15141 verifyFormat("class A<int> f() {}\n" 15142 "int n;"); 15143 verifyFormat("template <typename T> class A<T> f() {}\n" 15144 "int n;"); 15145 15146 verifyFormat("template <> class Foo<int> F() {\n" 15147 "} n;"); 15148 15149 // Elaborate types where incorrectly parsing the structural element would 15150 // break the indent. 15151 verifyFormat("if (true)\n" 15152 " class X x;\n" 15153 "else\n" 15154 " f();"); 15155 15156 // This is simply incomplete. Formatting is not important, but must not crash. 15157 verifyFormat("class A:"); 15158 } 15159 15160 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 15161 verifyNoChange("#error Leave all white!!!!! space* alone!"); 15162 verifyNoChange("#warning Leave all white!!!!! space* alone!"); 15163 verifyFormat("#error 1", " # error 1"); 15164 verifyFormat("#warning 1", " # warning 1"); 15165 } 15166 15167 TEST_F(FormatTest, FormatHashIfExpressions) { 15168 verifyFormat("#if AAAA && BBBB"); 15169 verifyFormat("#if (AAAA && BBBB)"); 15170 verifyFormat("#elif (AAAA && BBBB)"); 15171 // FIXME: Come up with a better indentation for #elif. 15172 verifyFormat( 15173 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" 15174 " defined(BBBBBBBB)\n" 15175 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" 15176 " defined(BBBBBBBB)\n" 15177 "#endif", 15178 getLLVMStyleWithColumns(65)); 15179 } 15180 15181 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 15182 FormatStyle AllowsMergedIf = getGoogleStyle(); 15183 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 15184 FormatStyle::SIS_WithoutElse; 15185 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 15186 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 15187 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 15188 verifyFormat("if (true) return 42;", "if (true)\nreturn 42;", AllowsMergedIf); 15189 FormatStyle ShortMergedIf = AllowsMergedIf; 15190 ShortMergedIf.ColumnLimit = 25; 15191 verifyFormat("#define A \\\n" 15192 " if (true) return 42;", 15193 ShortMergedIf); 15194 verifyFormat("#define A \\\n" 15195 " f(); \\\n" 15196 " if (true)\n" 15197 "#define B", 15198 ShortMergedIf); 15199 verifyFormat("#define A \\\n" 15200 " f(); \\\n" 15201 " if (true)\n" 15202 "g();", 15203 ShortMergedIf); 15204 verifyFormat("{\n" 15205 "#ifdef A\n" 15206 " // Comment\n" 15207 " if (true) continue;\n" 15208 "#endif\n" 15209 " // Comment\n" 15210 " if (true) continue;\n" 15211 "}", 15212 ShortMergedIf); 15213 ShortMergedIf.ColumnLimit = 33; 15214 verifyFormat("#define A \\\n" 15215 " if constexpr (true) return 42;", 15216 ShortMergedIf); 15217 verifyFormat("#define A \\\n" 15218 " if CONSTEXPR (true) return 42;", 15219 ShortMergedIf); 15220 ShortMergedIf.ColumnLimit = 29; 15221 verifyFormat("#define A \\\n" 15222 " if (aaaaaaaaaa) return 1; \\\n" 15223 " return 2;", 15224 ShortMergedIf); 15225 ShortMergedIf.ColumnLimit = 28; 15226 verifyFormat("#define A \\\n" 15227 " if (aaaaaaaaaa) \\\n" 15228 " return 1; \\\n" 15229 " return 2;", 15230 ShortMergedIf); 15231 verifyFormat("#define A \\\n" 15232 " if constexpr (aaaaaaa) \\\n" 15233 " return 1; \\\n" 15234 " return 2;", 15235 ShortMergedIf); 15236 verifyFormat("#define A \\\n" 15237 " if CONSTEXPR (aaaaaaa) \\\n" 15238 " return 1; \\\n" 15239 " return 2;", 15240 ShortMergedIf); 15241 15242 verifyFormat("//\n" 15243 "#define a \\\n" 15244 " if \\\n" 15245 " 0", 15246 getChromiumStyle(FormatStyle::LK_Cpp)); 15247 } 15248 15249 TEST_F(FormatTest, FormatStarDependingOnContext) { 15250 verifyFormat("void f(int *a);"); 15251 verifyFormat("void f() { f(fint * b); }"); 15252 verifyFormat("class A {\n void f(int *a);\n};"); 15253 verifyFormat("class A {\n int *a;\n};"); 15254 verifyFormat("namespace a {\n" 15255 "namespace b {\n" 15256 "class A {\n" 15257 " void f() {}\n" 15258 " int *a;\n" 15259 "};\n" 15260 "} // namespace b\n" 15261 "} // namespace a"); 15262 } 15263 15264 TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 15265 verifyFormat("while"); 15266 verifyFormat("operator"); 15267 } 15268 15269 TEST_F(FormatTest, SkipsDeeplyNestedLines) { 15270 // This code would be painfully slow to format if we didn't skip it. 15271 std::string Code("A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" // 20x 15272 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 15273 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 15274 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 15275 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 15276 "A(1, 1)\n" 15277 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x 15278 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 15279 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 15280 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 15281 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 15282 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 15283 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 15284 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 15285 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 15286 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n"); 15287 // Deeply nested part is untouched, rest is formatted. 15288 EXPECT_EQ(std::string("int i;") + Code + "int j;", 15289 format(std::string("int i;") + Code + "int j;", 15290 getLLVMStyle(), SC_ExpectIncomplete)); 15291 } 15292 15293 //===----------------------------------------------------------------------===// 15294 // Objective-C tests. 15295 //===----------------------------------------------------------------------===// 15296 15297 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 15298 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 15299 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject;", 15300 "-(NSUInteger)indexOfObject:(id)anObject;"); 15301 verifyFormat("- (NSInteger)Mthod1;", "-(NSInteger)Mthod1;"); 15302 verifyFormat("+ (id)Mthod2;", "+(id)Mthod2;"); 15303 verifyFormat("- (NSInteger)Method3:(id)anObject;", 15304 "-(NSInteger)Method3:(id)anObject;"); 15305 verifyFormat("- (NSInteger)Method4:(id)anObject;", 15306 "-(NSInteger)Method4:(id)anObject;"); 15307 verifyFormat("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 15308 "-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"); 15309 verifyFormat("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"); 15310 verifyFormat("- (void)sendAction:(SEL)aSelector to:(id)anObject " 15311 "forAllCells:(BOOL)flag;"); 15312 15313 // Very long objectiveC method declaration. 15314 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 15315 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;"); 15316 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 15317 " inRange:(NSRange)range\n" 15318 " outRange:(NSRange)out_range\n" 15319 " outRange1:(NSRange)out_range1\n" 15320 " outRange2:(NSRange)out_range2\n" 15321 " outRange3:(NSRange)out_range3\n" 15322 " outRange4:(NSRange)out_range4\n" 15323 " outRange5:(NSRange)out_range5\n" 15324 " outRange6:(NSRange)out_range6\n" 15325 " outRange7:(NSRange)out_range7\n" 15326 " outRange8:(NSRange)out_range8\n" 15327 " outRange9:(NSRange)out_range9;"); 15328 15329 // When the function name has to be wrapped. 15330 FormatStyle Style = getLLVMStyle(); 15331 // ObjC ignores IndentWrappedFunctionNames when wrapping methods 15332 // and always indents instead. 15333 Style.IndentWrappedFunctionNames = false; 15334 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 15335 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 15336 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 15337 "}", 15338 Style); 15339 Style.IndentWrappedFunctionNames = true; 15340 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 15341 " veryLooooooooooongName:(NSString)cccccccccccccc\n" 15342 " anotherName:(NSString)dddddddddddddd {\n" 15343 "}", 15344 Style); 15345 15346 verifyFormat("- (int)sum:(vector<int>)numbers;"); 15347 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 15348 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 15349 // protocol lists (but not for template classes): 15350 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 15351 15352 verifyFormat("- (int (*)())foo:(int (*)())f;"); 15353 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); 15354 15355 // If there's no return type (very rare in practice!), LLVM and Google style 15356 // agree. 15357 verifyFormat("- foo;"); 15358 verifyFormat("- foo:(int)f;"); 15359 verifyGoogleFormat("- foo:(int)foo;"); 15360 } 15361 15362 TEST_F(FormatTest, BreaksStringLiterals) { 15363 // FIXME: unstable test case 15364 EXPECT_EQ("\"some text \"\n" 15365 "\"other\";", 15366 format("\"some text other\";", getLLVMStyleWithColumns(12))); 15367 // FIXME: unstable test case 15368 EXPECT_EQ("\"some text \"\n" 15369 "\"other\";", 15370 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); 15371 verifyFormat("#define A \\\n" 15372 " \"some \" \\\n" 15373 " \"text \" \\\n" 15374 " \"other\";", 15375 "#define A \"some text other\";", getLLVMStyleWithColumns(12)); 15376 verifyFormat("#define A \\\n" 15377 " \"so \" \\\n" 15378 " \"text \" \\\n" 15379 " \"other\";", 15380 "#define A \"so text other\";", getLLVMStyleWithColumns(12)); 15381 15382 verifyFormat("\"some text\"", getLLVMStyleWithColumns(1)); 15383 verifyFormat("\"some text\"", getLLVMStyleWithColumns(11)); 15384 // FIXME: unstable test case 15385 EXPECT_EQ("\"some \"\n" 15386 "\"text\"", 15387 format("\"some text\"", getLLVMStyleWithColumns(10))); 15388 // FIXME: unstable test case 15389 EXPECT_EQ("\"some \"\n" 15390 "\"text\"", 15391 format("\"some text\"", getLLVMStyleWithColumns(7))); 15392 // FIXME: unstable test case 15393 EXPECT_EQ("\"some\"\n" 15394 "\" tex\"\n" 15395 "\"t\"", 15396 format("\"some text\"", getLLVMStyleWithColumns(6))); 15397 // FIXME: unstable test case 15398 EXPECT_EQ("\"some\"\n" 15399 "\" tex\"\n" 15400 "\" and\"", 15401 format("\"some tex and\"", getLLVMStyleWithColumns(6))); 15402 // FIXME: unstable test case 15403 EXPECT_EQ("\"some\"\n" 15404 "\"/tex\"\n" 15405 "\"/and\"", 15406 format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); 15407 15408 verifyFormat("variable =\n" 15409 " \"long string \"\n" 15410 " \"literal\";", 15411 "variable = \"long string literal\";", 15412 getLLVMStyleWithColumns(20)); 15413 15414 verifyFormat("variable = f(\n" 15415 " \"long string \"\n" 15416 " \"literal\",\n" 15417 " short,\n" 15418 " loooooooooooooooooooong);", 15419 "variable = f(\"long string literal\", short, " 15420 "loooooooooooooooooooong);", 15421 getLLVMStyleWithColumns(20)); 15422 15423 verifyFormat("f(g(\"long string \"\n" 15424 " \"literal\"),\n" 15425 " b);", 15426 "f(g(\"long string literal\"), b);", 15427 getLLVMStyleWithColumns(20)); 15428 verifyFormat("f(g(\"long string \"\n" 15429 " \"literal\",\n" 15430 " a),\n" 15431 " b);", 15432 "f(g(\"long string literal\", a), b);", 15433 getLLVMStyleWithColumns(20)); 15434 verifyFormat("f(\"one two\".split(\n" 15435 " variable));", 15436 "f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)); 15437 verifyFormat("f(\"one two three four five six \"\n" 15438 " \"seven\".split(\n" 15439 " really_looooong_variable));", 15440 "f(\"one two three four five six seven\"." 15441 "split(really_looooong_variable));", 15442 getLLVMStyleWithColumns(33)); 15443 15444 verifyFormat("f(\"some \"\n" 15445 " \"text\",\n" 15446 " other);", 15447 "f(\"some text\", other);", getLLVMStyleWithColumns(10)); 15448 15449 // Only break as a last resort. 15450 verifyFormat( 15451 "aaaaaaaaaaaaaaaaaaaa(\n" 15452 " aaaaaaaaaaaaaaaaaaaa,\n" 15453 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); 15454 15455 // FIXME: unstable test case 15456 EXPECT_EQ("\"splitmea\"\n" 15457 "\"trandomp\"\n" 15458 "\"oint\"", 15459 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); 15460 15461 // FIXME: unstable test case 15462 EXPECT_EQ("\"split/\"\n" 15463 "\"pathat/\"\n" 15464 "\"slashes\"", 15465 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 15466 15467 // FIXME: unstable test case 15468 EXPECT_EQ("\"split/\"\n" 15469 "\"pathat/\"\n" 15470 "\"slashes\"", 15471 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 15472 // FIXME: unstable test case 15473 EXPECT_EQ("\"split at \"\n" 15474 "\"spaces/at/\"\n" 15475 "\"slashes.at.any$\"\n" 15476 "\"non-alphanumeric%\"\n" 15477 "\"1111111111characte\"\n" 15478 "\"rs\"", 15479 format("\"split at " 15480 "spaces/at/" 15481 "slashes.at." 15482 "any$non-" 15483 "alphanumeric%" 15484 "1111111111characte" 15485 "rs\"", 15486 getLLVMStyleWithColumns(20))); 15487 15488 // Verify that splitting the strings understands 15489 // Style::AlwaysBreakBeforeMultilineStrings. 15490 verifyFormat("aaaaaaaaaaaa(\n" 15491 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" 15492 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", 15493 "aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa " 15494 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 15495 "aaaaaaaaaaaaaaaaaaaaaa\");", 15496 getGoogleStyle()); 15497 verifyFormat("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 15498 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", 15499 "return \"aaaaaaaaaaaaaaaaaaaaaa " 15500 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 15501 "aaaaaaaaaaaaaaaaaaaaaa\";", 15502 getGoogleStyle()); 15503 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 15504 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 15505 "llvm::outs() << " 15506 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa" 15507 "aaaaaaaaaaaaaaaaaaa\";"); 15508 verifyFormat("ffff(\n" 15509 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 15510 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 15511 "ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " 15512 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 15513 getGoogleStyle()); 15514 15515 FormatStyle Style = getLLVMStyleWithColumns(12); 15516 Style.BreakStringLiterals = false; 15517 verifyFormat("\"some text other\";", Style); 15518 15519 FormatStyle AlignLeft = getLLVMStyleWithColumns(12); 15520 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 15521 verifyFormat("#define A \\\n" 15522 " \"some \" \\\n" 15523 " \"text \" \\\n" 15524 " \"other\";", 15525 "#define A \"some text other\";", AlignLeft); 15526 } 15527 15528 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) { 15529 verifyFormat("C a = \"some more \"\n" 15530 " \"text\";", 15531 "C a = \"some more text\";", getLLVMStyleWithColumns(18)); 15532 } 15533 15534 TEST_F(FormatTest, FullyRemoveEmptyLines) { 15535 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80); 15536 NoEmptyLines.MaxEmptyLinesToKeep = 0; 15537 verifyFormat("int i = a(b());", "int i=a(\n\n b(\n\n\n )\n\n);", 15538 NoEmptyLines); 15539 } 15540 15541 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) { 15542 // FIXME: unstable test case 15543 EXPECT_EQ( 15544 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 15545 "(\n" 15546 " \"x\t\");", 15547 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 15548 "aaaaaaa(" 15549 "\"x\t\");")); 15550 } 15551 15552 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) { 15553 // FIXME: unstable test case 15554 EXPECT_EQ( 15555 "u8\"utf8 string \"\n" 15556 "u8\"literal\";", 15557 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16))); 15558 // FIXME: unstable test case 15559 EXPECT_EQ( 15560 "u\"utf16 string \"\n" 15561 "u\"literal\";", 15562 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16))); 15563 // FIXME: unstable test case 15564 EXPECT_EQ( 15565 "U\"utf32 string \"\n" 15566 "U\"literal\";", 15567 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16))); 15568 // FIXME: unstable test case 15569 EXPECT_EQ("L\"wide string \"\n" 15570 "L\"literal\";", 15571 format("L\"wide string literal\";", getGoogleStyleWithColumns(16))); 15572 verifyFormat("@\"NSString \"\n" 15573 "@\"literal\";", 15574 "@\"NSString literal\";", getGoogleStyleWithColumns(19)); 15575 verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26)); 15576 15577 // This input makes clang-format try to split the incomplete unicode escape 15578 // sequence, which used to lead to a crasher. 15579 verifyNoCrash( 15580 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 15581 getLLVMStyleWithColumns(60)); 15582 } 15583 15584 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) { 15585 FormatStyle Style = getGoogleStyleWithColumns(15); 15586 verifyFormat("R\"x(raw literal)x\";", Style); 15587 verifyFormat("uR\"x(raw literal)x\";", Style); 15588 verifyFormat("LR\"x(raw literal)x\";", Style); 15589 verifyFormat("UR\"x(raw literal)x\";", Style); 15590 verifyFormat("u8R\"x(raw literal)x\";", Style); 15591 } 15592 15593 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { 15594 FormatStyle Style = getLLVMStyleWithColumns(20); 15595 // FIXME: unstable test case 15596 EXPECT_EQ( 15597 "_T(\"aaaaaaaaaaaaaa\")\n" 15598 "_T(\"aaaaaaaaaaaaaa\")\n" 15599 "_T(\"aaaaaaaaaaaa\")", 15600 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style)); 15601 verifyFormat("f(x,\n" 15602 " _T(\"aaaaaaaaaaaa\")\n" 15603 " _T(\"aaa\"),\n" 15604 " z);", 15605 "f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style); 15606 15607 // FIXME: Handle embedded spaces in one iteration. 15608 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n" 15609 // "_T(\"aaaaaaaaaaaaa\")\n" 15610 // "_T(\"aaaaaaaaaaaaa\")\n" 15611 // "_T(\"a\")", 15612 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 15613 // getLLVMStyleWithColumns(20))); 15614 verifyFormat("_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 15615 " _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style); 15616 verifyFormat("f(\n" 15617 "#if !TEST\n" 15618 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 15619 "#endif\n" 15620 ");", 15621 "f(\n" 15622 "#if !TEST\n" 15623 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 15624 "#endif\n" 15625 ");"); 15626 verifyFormat("f(\n" 15627 "\n" 15628 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));", 15629 "f(\n" 15630 "\n" 15631 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"); 15632 // Regression test for accessing tokens past the end of a vector in the 15633 // TokenLexer. 15634 verifyNoCrash(R"(_T( 15635 " 15636 ) 15637 )"); 15638 } 15639 15640 TEST_F(FormatTest, BreaksStringLiteralOperands) { 15641 // In a function call with two operands, the second can be broken with no line 15642 // break before it. 15643 verifyFormat("func(a, \"long long \"\n" 15644 " \"long long\");", 15645 "func(a, \"long long long long\");", 15646 getLLVMStyleWithColumns(24)); 15647 // In a function call with three operands, the second must be broken with a 15648 // line break before it. 15649 verifyFormat("func(a,\n" 15650 " \"long long long \"\n" 15651 " \"long\",\n" 15652 " c);", 15653 "func(a, \"long long long long\", c);", 15654 getLLVMStyleWithColumns(24)); 15655 // In a function call with three operands, the third must be broken with a 15656 // line break before it. 15657 verifyFormat("func(a, b,\n" 15658 " \"long long long \"\n" 15659 " \"long\");", 15660 "func(a, b, \"long long long long\");", 15661 getLLVMStyleWithColumns(24)); 15662 // In a function call with three operands, both the second and the third must 15663 // be broken with a line break before them. 15664 verifyFormat("func(a,\n" 15665 " \"long long long \"\n" 15666 " \"long\",\n" 15667 " \"long long long \"\n" 15668 " \"long\");", 15669 "func(a, \"long long long long\", \"long long long long\");", 15670 getLLVMStyleWithColumns(24)); 15671 // In a chain of << with two operands, the second can be broken with no line 15672 // break before it. 15673 verifyFormat("a << \"line line \"\n" 15674 " \"line\";", 15675 "a << \"line line line\";", getLLVMStyleWithColumns(20)); 15676 // In a chain of << with three operands, the second can be broken with no line 15677 // break before it. 15678 verifyFormat("abcde << \"line \"\n" 15679 " \"line line\"\n" 15680 " << c;", 15681 "abcde << \"line line line\" << c;", 15682 getLLVMStyleWithColumns(20)); 15683 // In a chain of << with three operands, the third must be broken with a line 15684 // break before it. 15685 verifyFormat("a << b\n" 15686 " << \"line line \"\n" 15687 " \"line\";", 15688 "a << b << \"line line line\";", getLLVMStyleWithColumns(20)); 15689 // In a chain of << with three operands, the second can be broken with no line 15690 // break before it and the third must be broken with a line break before it. 15691 verifyFormat("abcd << \"line line \"\n" 15692 " \"line\"\n" 15693 " << \"line line \"\n" 15694 " \"line\";", 15695 "abcd << \"line line line\" << \"line line line\";", 15696 getLLVMStyleWithColumns(20)); 15697 // In a chain of binary operators with two operands, the second can be broken 15698 // with no line break before it. 15699 verifyFormat("abcd + \"line line \"\n" 15700 " \"line line\";", 15701 "abcd + \"line line line line\";", getLLVMStyleWithColumns(20)); 15702 // In a chain of binary operators with three operands, the second must be 15703 // broken with a line break before it. 15704 verifyFormat("abcd +\n" 15705 " \"line line \"\n" 15706 " \"line line\" +\n" 15707 " e;", 15708 "abcd + \"line line line line\" + e;", 15709 getLLVMStyleWithColumns(20)); 15710 // In a function call with two operands, with AlignAfterOpenBracket enabled, 15711 // the first must be broken with a line break before it. 15712 FormatStyle Style = getLLVMStyleWithColumns(25); 15713 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 15714 verifyFormat("someFunction(\n" 15715 " \"long long long \"\n" 15716 " \"long\",\n" 15717 " a);", 15718 "someFunction(\"long long long long\", a);", Style); 15719 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; 15720 verifyFormat("someFunction(\n" 15721 " \"long long long \"\n" 15722 " \"long\",\n" 15723 " a\n" 15724 ");", 15725 Style); 15726 } 15727 15728 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { 15729 verifyFormat("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 15730 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 15731 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 15732 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 15733 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 15734 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"); 15735 } 15736 15737 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { 15738 verifyFormat("f(g(R\"x(raw literal)x\", a), b);", 15739 "f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle()); 15740 verifyFormat("fffffffffff(g(R\"x(\n" 15741 "multiline raw string literal xxxxxxxxxxxxxx\n" 15742 ")x\",\n" 15743 " a),\n" 15744 " b);", 15745 "fffffffffff(g(R\"x(\n" 15746 "multiline raw string literal xxxxxxxxxxxxxx\n" 15747 ")x\", a), b);", 15748 getGoogleStyleWithColumns(20)); 15749 verifyFormat("fffffffffff(\n" 15750 " g(R\"x(qqq\n" 15751 "multiline raw string literal xxxxxxxxxxxxxx\n" 15752 ")x\",\n" 15753 " a),\n" 15754 " b);", 15755 "fffffffffff(g(R\"x(qqq\n" 15756 "multiline raw string literal xxxxxxxxxxxxxx\n" 15757 ")x\", a), b);", 15758 getGoogleStyleWithColumns(20)); 15759 15760 verifyNoChange("fffffffffff(R\"x(\n" 15761 "multiline raw string literal xxxxxxxxxxxxxx\n" 15762 ")x\");", 15763 getGoogleStyleWithColumns(20)); 15764 verifyFormat("fffffffffff(R\"x(\n" 15765 "multiline raw string literal xxxxxxxxxxxxxx\n" 15766 ")x\" + bbbbbb);", 15767 "fffffffffff(R\"x(\n" 15768 "multiline raw string literal xxxxxxxxxxxxxx\n" 15769 ")x\" + bbbbbb);", 15770 getGoogleStyleWithColumns(20)); 15771 verifyFormat("fffffffffff(\n" 15772 " R\"x(\n" 15773 "multiline raw string literal xxxxxxxxxxxxxx\n" 15774 ")x\" +\n" 15775 " bbbbbb);", 15776 "fffffffffff(\n" 15777 " R\"x(\n" 15778 "multiline raw string literal xxxxxxxxxxxxxx\n" 15779 ")x\" + bbbbbb);", 15780 getGoogleStyleWithColumns(20)); 15781 verifyFormat("fffffffffff(R\"(single line raw string)\" + bbbbbb);", 15782 "fffffffffff(\n" 15783 " R\"(single line raw string)\" + bbbbbb);"); 15784 } 15785 15786 TEST_F(FormatTest, SkipsUnknownStringLiterals) { 15787 verifyFormat("string a = \"unterminated;"); 15788 verifyFormat("function(\"unterminated,\n" 15789 " OtherParameter);", 15790 "function( \"unterminated,\n" 15791 " OtherParameter);"); 15792 } 15793 15794 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { 15795 FormatStyle Style = getLLVMStyle(); 15796 Style.Standard = FormatStyle::LS_Cpp03; 15797 verifyFormat("#define x(_a) printf(\"foo\" _a);", 15798 "#define x(_a) printf(\"foo\"_a);", Style); 15799 } 15800 15801 TEST_F(FormatTest, CppLexVersion) { 15802 FormatStyle Style = getLLVMStyle(); 15803 // Formatting of x * y differs if x is a type. 15804 verifyFormat("void foo() { MACRO(a * b); }", Style); 15805 verifyFormat("void foo() { MACRO(int *b); }", Style); 15806 15807 // LLVM style uses latest lexer. 15808 verifyFormat("void foo() { MACRO(char8_t *b); }", Style); 15809 Style.Standard = FormatStyle::LS_Cpp17; 15810 // But in c++17, char8_t isn't a keyword. 15811 verifyFormat("void foo() { MACRO(char8_t * b); }", Style); 15812 } 15813 15814 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); } 15815 15816 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { 15817 verifyFormat("someFunction(\"aaabbbcccd\"\n" 15818 " \"ddeeefff\");", 15819 "someFunction(\"aaabbbcccdddeeefff\");", 15820 getLLVMStyleWithColumns(25)); 15821 verifyFormat("someFunction1234567890(\n" 15822 " \"aaabbbcccdddeeefff\");", 15823 "someFunction1234567890(\"aaabbbcccdddeeefff\");", 15824 getLLVMStyleWithColumns(26)); 15825 verifyFormat("someFunction1234567890(\n" 15826 " \"aaabbbcccdddeeeff\"\n" 15827 " \"f\");", 15828 "someFunction1234567890(\"aaabbbcccdddeeefff\");", 15829 getLLVMStyleWithColumns(25)); 15830 verifyFormat("someFunction1234567890(\n" 15831 " \"aaabbbcccdddeeeff\"\n" 15832 " \"f\");", 15833 "someFunction1234567890(\"aaabbbcccdddeeefff\");", 15834 getLLVMStyleWithColumns(24)); 15835 verifyFormat("someFunction(\n" 15836 " \"aaabbbcc ddde \"\n" 15837 " \"efff\");", 15838 "someFunction(\"aaabbbcc ddde efff\");", 15839 getLLVMStyleWithColumns(25)); 15840 verifyFormat("someFunction(\"aaabbbccc \"\n" 15841 " \"ddeeefff\");", 15842 "someFunction(\"aaabbbccc ddeeefff\");", 15843 getLLVMStyleWithColumns(25)); 15844 verifyFormat("someFunction1234567890(\n" 15845 " \"aaabb \"\n" 15846 " \"cccdddeeefff\");", 15847 "someFunction1234567890(\"aaabb cccdddeeefff\");", 15848 getLLVMStyleWithColumns(25)); 15849 verifyFormat("#define A \\\n" 15850 " string s = \\\n" 15851 " \"123456789\" \\\n" 15852 " \"0\"; \\\n" 15853 " int i;", 15854 "#define A string s = \"1234567890\"; int i;", 15855 getLLVMStyleWithColumns(20)); 15856 verifyFormat("someFunction(\n" 15857 " \"aaabbbcc \"\n" 15858 " \"dddeeefff\");", 15859 "someFunction(\"aaabbbcc dddeeefff\");", 15860 getLLVMStyleWithColumns(25)); 15861 } 15862 15863 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { 15864 verifyFormat("\"\\a\"", getLLVMStyleWithColumns(3)); 15865 verifyFormat("\"\\\"", getLLVMStyleWithColumns(2)); 15866 // FIXME: unstable test case 15867 EXPECT_EQ("\"test\"\n" 15868 "\"\\n\"", 15869 format("\"test\\n\"", getLLVMStyleWithColumns(7))); 15870 // FIXME: unstable test case 15871 EXPECT_EQ("\"tes\\\\\"\n" 15872 "\"n\"", 15873 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); 15874 // FIXME: unstable test case 15875 EXPECT_EQ("\"\\\\\\\\\"\n" 15876 "\"\\n\"", 15877 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); 15878 verifyFormat("\"\\uff01\"", getLLVMStyleWithColumns(7)); 15879 // FIXME: unstable test case 15880 EXPECT_EQ("\"\\uff01\"\n" 15881 "\"test\"", 15882 format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); 15883 verifyFormat("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)); 15884 // FIXME: unstable test case 15885 EXPECT_EQ("\"\\x000000000001\"\n" 15886 "\"next\"", 15887 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); 15888 verifyFormat("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)); 15889 verifyFormat("\"\\x000000000001\"", getLLVMStyleWithColumns(7)); 15890 // FIXME: unstable test case 15891 EXPECT_EQ("\"test\"\n" 15892 "\"\\000000\"\n" 15893 "\"000001\"", 15894 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); 15895 // FIXME: unstable test case 15896 EXPECT_EQ("\"test\\000\"\n" 15897 "\"00000000\"\n" 15898 "\"1\"", 15899 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); 15900 } 15901 15902 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { 15903 verifyFormat("void f() {\n" 15904 " return g() {}\n" 15905 " void h() {}"); 15906 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n" 15907 "g();\n" 15908 "}"); 15909 } 15910 15911 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) { 15912 verifyFormat( 15913 "void f() { return C{param1, param2}.SomeCall(param1, param2); }"); 15914 } 15915 15916 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { 15917 verifyFormat("class X {\n" 15918 " void f() {\n" 15919 " }\n" 15920 "};", 15921 getLLVMStyleWithColumns(12)); 15922 } 15923 15924 TEST_F(FormatTest, ConfigurableIndentWidth) { 15925 FormatStyle EightIndent = getLLVMStyleWithColumns(18); 15926 EightIndent.IndentWidth = 8; 15927 EightIndent.ContinuationIndentWidth = 8; 15928 verifyFormat("void f() {\n" 15929 " someFunction();\n" 15930 " if (true) {\n" 15931 " f();\n" 15932 " }\n" 15933 "}", 15934 EightIndent); 15935 verifyFormat("class X {\n" 15936 " void f() {\n" 15937 " }\n" 15938 "};", 15939 EightIndent); 15940 verifyFormat("int x[] = {\n" 15941 " call(),\n" 15942 " call()};", 15943 EightIndent); 15944 } 15945 15946 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) { 15947 verifyFormat("double\n" 15948 "f();", 15949 getLLVMStyleWithColumns(8)); 15950 } 15951 15952 TEST_F(FormatTest, ConfigurableUseOfTab) { 15953 FormatStyle Tab = getLLVMStyleWithColumns(42); 15954 Tab.IndentWidth = 8; 15955 Tab.UseTab = FormatStyle::UT_Always; 15956 Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left; 15957 15958 verifyFormat("if (aaaaaaaa && // q\n" 15959 " bb)\t\t// w\n" 15960 "\t;", 15961 "if (aaaaaaaa &&// q\n" 15962 "bb)// w\n" 15963 ";", 15964 Tab); 15965 verifyFormat("if (aaa && bbb) // w\n" 15966 "\t;", 15967 "if(aaa&&bbb)// w\n" 15968 ";", 15969 Tab); 15970 15971 verifyFormat("class X {\n" 15972 "\tvoid f() {\n" 15973 "\t\tsomeFunction(parameter1,\n" 15974 "\t\t\t parameter2);\n" 15975 "\t}\n" 15976 "};", 15977 Tab); 15978 verifyFormat("#define A \\\n" 15979 "\tvoid f() { \\\n" 15980 "\t\tsomeFunction( \\\n" 15981 "\t\t parameter1, \\\n" 15982 "\t\t parameter2); \\\n" 15983 "\t}", 15984 Tab); 15985 verifyFormat("int a;\t // x\n" 15986 "int bbbbbbbb; // x", 15987 Tab); 15988 15989 FormatStyle TabAlignment = Tab; 15990 TabAlignment.AlignConsecutiveDeclarations.Enabled = true; 15991 TabAlignment.PointerAlignment = FormatStyle::PAS_Left; 15992 verifyFormat("unsigned long long big;\n" 15993 "char*\t\t ptr;", 15994 TabAlignment); 15995 TabAlignment.PointerAlignment = FormatStyle::PAS_Middle; 15996 verifyFormat("unsigned long long big;\n" 15997 "char *\t\t ptr;", 15998 TabAlignment); 15999 TabAlignment.PointerAlignment = FormatStyle::PAS_Right; 16000 verifyFormat("unsigned long long big;\n" 16001 "char\t\t *ptr;", 16002 TabAlignment); 16003 16004 Tab.TabWidth = 4; 16005 Tab.IndentWidth = 8; 16006 verifyFormat("class TabWidth4Indent8 {\n" 16007 "\t\tvoid f() {\n" 16008 "\t\t\t\tsomeFunction(parameter1,\n" 16009 "\t\t\t\t\t\t\t parameter2);\n" 16010 "\t\t}\n" 16011 "};", 16012 Tab); 16013 16014 Tab.TabWidth = 4; 16015 Tab.IndentWidth = 4; 16016 verifyFormat("class TabWidth4Indent4 {\n" 16017 "\tvoid f() {\n" 16018 "\t\tsomeFunction(parameter1,\n" 16019 "\t\t\t\t\t parameter2);\n" 16020 "\t}\n" 16021 "};", 16022 Tab); 16023 16024 Tab.TabWidth = 8; 16025 Tab.IndentWidth = 4; 16026 verifyFormat("class TabWidth8Indent4 {\n" 16027 " void f() {\n" 16028 "\tsomeFunction(parameter1,\n" 16029 "\t\t parameter2);\n" 16030 " }\n" 16031 "};", 16032 Tab); 16033 16034 Tab.TabWidth = 8; 16035 Tab.IndentWidth = 8; 16036 verifyFormat("/*\n" 16037 "\t a\t\tcomment\n" 16038 "\t in multiple lines\n" 16039 " */", 16040 " /*\t \t \n" 16041 " \t \t a\t\tcomment\t \t\n" 16042 " \t \t in multiple lines\t\n" 16043 " \t */", 16044 Tab); 16045 16046 TabAlignment.UseTab = FormatStyle::UT_ForIndentation; 16047 TabAlignment.PointerAlignment = FormatStyle::PAS_Left; 16048 verifyFormat("void f() {\n" 16049 "\tunsigned long long big;\n" 16050 "\tchar* ptr;\n" 16051 "}", 16052 TabAlignment); 16053 TabAlignment.PointerAlignment = FormatStyle::PAS_Middle; 16054 verifyFormat("void f() {\n" 16055 "\tunsigned long long big;\n" 16056 "\tchar * ptr;\n" 16057 "}", 16058 TabAlignment); 16059 TabAlignment.PointerAlignment = FormatStyle::PAS_Right; 16060 verifyFormat("void f() {\n" 16061 "\tunsigned long long big;\n" 16062 "\tchar *ptr;\n" 16063 "}", 16064 TabAlignment); 16065 16066 Tab.UseTab = FormatStyle::UT_ForIndentation; 16067 verifyFormat("{\n" 16068 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 16069 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 16070 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 16071 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 16072 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 16073 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 16074 "};", 16075 Tab); 16076 verifyFormat("enum AA {\n" 16077 "\ta1, // Force multiple lines\n" 16078 "\ta2,\n" 16079 "\ta3\n" 16080 "};", 16081 Tab); 16082 verifyFormat("if (aaaaaaaa && // q\n" 16083 " bb) // w\n" 16084 "\t;", 16085 "if (aaaaaaaa &&// q\n" 16086 "bb)// w\n" 16087 ";", 16088 Tab); 16089 verifyFormat("class X {\n" 16090 "\tvoid f() {\n" 16091 "\t\tsomeFunction(parameter1,\n" 16092 "\t\t parameter2);\n" 16093 "\t}\n" 16094 "};", 16095 Tab); 16096 verifyFormat("{\n" 16097 "\tQ(\n" 16098 "\t {\n" 16099 "\t\t int a;\n" 16100 "\t\t someFunction(aaaaaaaa,\n" 16101 "\t\t bbbbbbb);\n" 16102 "\t },\n" 16103 "\t p);\n" 16104 "}", 16105 Tab); 16106 verifyFormat("{\n" 16107 "\t/* aaaa\n" 16108 "\t bbbb */\n" 16109 "}", 16110 "{\n" 16111 "/* aaaa\n" 16112 " bbbb */\n" 16113 "}", 16114 Tab); 16115 verifyFormat("{\n" 16116 "\t/*\n" 16117 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 16118 "\t bbbbbbbbbbbbb\n" 16119 "\t*/\n" 16120 "}", 16121 "{\n" 16122 "/*\n" 16123 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 16124 "*/\n" 16125 "}", 16126 Tab); 16127 verifyFormat("{\n" 16128 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 16129 "\t// bbbbbbbbbbbbb\n" 16130 "}", 16131 "{\n" 16132 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 16133 "}", 16134 Tab); 16135 verifyFormat("{\n" 16136 "\t/*\n" 16137 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 16138 "\t bbbbbbbbbbbbb\n" 16139 "\t*/\n" 16140 "}", 16141 "{\n" 16142 "\t/*\n" 16143 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 16144 "\t*/\n" 16145 "}", 16146 Tab); 16147 verifyNoChange("{\n" 16148 "\t/*\n" 16149 "\n" 16150 "\t*/\n" 16151 "}", 16152 Tab); 16153 verifyNoChange("{\n" 16154 "\t/*\n" 16155 " asdf\n" 16156 "\t*/\n" 16157 "}", 16158 Tab); 16159 16160 verifyFormat("void f() {\n" 16161 "\treturn true ? aaaaaaaaaaaaaaaaaa\n" 16162 "\t : bbbbbbbbbbbbbbbbbb\n" 16163 "}", 16164 Tab); 16165 FormatStyle TabNoBreak = Tab; 16166 TabNoBreak.BreakBeforeTernaryOperators = false; 16167 verifyFormat("void f() {\n" 16168 "\treturn true ? aaaaaaaaaaaaaaaaaa :\n" 16169 "\t bbbbbbbbbbbbbbbbbb\n" 16170 "}", 16171 TabNoBreak); 16172 verifyFormat("void f() {\n" 16173 "\treturn true ?\n" 16174 "\t aaaaaaaaaaaaaaaaaaaa :\n" 16175 "\t bbbbbbbbbbbbbbbbbbbb\n" 16176 "}", 16177 TabNoBreak); 16178 16179 Tab.UseTab = FormatStyle::UT_Never; 16180 verifyFormat("/*\n" 16181 " a\t\tcomment\n" 16182 " in multiple lines\n" 16183 " */", 16184 " /*\t \t \n" 16185 " \t \t a\t\tcomment\t \t\n" 16186 " \t \t in multiple lines\t\n" 16187 " \t */", 16188 Tab); 16189 verifyFormat("/* some\n" 16190 " comment */", 16191 " \t \t /* some\n" 16192 " \t \t comment */", 16193 Tab); 16194 verifyFormat("int a; /* some\n" 16195 " comment */", 16196 " \t \t int a; /* some\n" 16197 " \t \t comment */", 16198 Tab); 16199 16200 verifyFormat("int a; /* some\n" 16201 "comment */", 16202 " \t \t int\ta; /* some\n" 16203 " \t \t comment */", 16204 Tab); 16205 verifyFormat("f(\"\t\t\"); /* some\n" 16206 " comment */", 16207 " \t \t f(\"\t\t\"); /* some\n" 16208 " \t \t comment */", 16209 Tab); 16210 verifyFormat("{\n" 16211 " /*\n" 16212 " * Comment\n" 16213 " */\n" 16214 " int i;\n" 16215 "}", 16216 "{\n" 16217 "\t/*\n" 16218 "\t * Comment\n" 16219 "\t */\n" 16220 "\t int i;\n" 16221 "}", 16222 Tab); 16223 16224 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 16225 Tab.TabWidth = 8; 16226 Tab.IndentWidth = 8; 16227 verifyFormat("if (aaaaaaaa && // q\n" 16228 " bb) // w\n" 16229 "\t;", 16230 "if (aaaaaaaa &&// q\n" 16231 "bb)// w\n" 16232 ";", 16233 Tab); 16234 verifyFormat("if (aaa && bbb) // w\n" 16235 "\t;", 16236 "if(aaa&&bbb)// w\n" 16237 ";", 16238 Tab); 16239 verifyFormat("class X {\n" 16240 "\tvoid f() {\n" 16241 "\t\tsomeFunction(parameter1,\n" 16242 "\t\t\t parameter2);\n" 16243 "\t}\n" 16244 "};", 16245 Tab); 16246 verifyFormat("#define A \\\n" 16247 "\tvoid f() { \\\n" 16248 "\t\tsomeFunction( \\\n" 16249 "\t\t parameter1, \\\n" 16250 "\t\t parameter2); \\\n" 16251 "\t}", 16252 Tab); 16253 Tab.TabWidth = 4; 16254 Tab.IndentWidth = 8; 16255 verifyFormat("class TabWidth4Indent8 {\n" 16256 "\t\tvoid f() {\n" 16257 "\t\t\t\tsomeFunction(parameter1,\n" 16258 "\t\t\t\t\t\t\t parameter2);\n" 16259 "\t\t}\n" 16260 "};", 16261 Tab); 16262 Tab.TabWidth = 4; 16263 Tab.IndentWidth = 4; 16264 verifyFormat("class TabWidth4Indent4 {\n" 16265 "\tvoid f() {\n" 16266 "\t\tsomeFunction(parameter1,\n" 16267 "\t\t\t\t\t parameter2);\n" 16268 "\t}\n" 16269 "};", 16270 Tab); 16271 Tab.TabWidth = 8; 16272 Tab.IndentWidth = 4; 16273 verifyFormat("class TabWidth8Indent4 {\n" 16274 " void f() {\n" 16275 "\tsomeFunction(parameter1,\n" 16276 "\t\t parameter2);\n" 16277 " }\n" 16278 "};", 16279 Tab); 16280 Tab.TabWidth = 8; 16281 Tab.IndentWidth = 8; 16282 verifyFormat("/*\n" 16283 "\t a\t\tcomment\n" 16284 "\t in multiple lines\n" 16285 " */", 16286 " /*\t \t \n" 16287 " \t \t a\t\tcomment\t \t\n" 16288 " \t \t in multiple lines\t\n" 16289 " \t */", 16290 Tab); 16291 verifyFormat("{\n" 16292 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 16293 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 16294 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 16295 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 16296 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 16297 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 16298 "};", 16299 Tab); 16300 verifyFormat("enum AA {\n" 16301 "\ta1, // Force multiple lines\n" 16302 "\ta2,\n" 16303 "\ta3\n" 16304 "};", 16305 Tab); 16306 verifyFormat("if (aaaaaaaa && // q\n" 16307 " bb) // w\n" 16308 "\t;", 16309 "if (aaaaaaaa &&// q\n" 16310 "bb)// w\n" 16311 ";", 16312 Tab); 16313 verifyFormat("class X {\n" 16314 "\tvoid f() {\n" 16315 "\t\tsomeFunction(parameter1,\n" 16316 "\t\t\t parameter2);\n" 16317 "\t}\n" 16318 "};", 16319 Tab); 16320 verifyFormat("{\n" 16321 "\tQ(\n" 16322 "\t {\n" 16323 "\t\t int a;\n" 16324 "\t\t someFunction(aaaaaaaa,\n" 16325 "\t\t\t\t bbbbbbb);\n" 16326 "\t },\n" 16327 "\t p);\n" 16328 "}", 16329 Tab); 16330 verifyFormat("{\n" 16331 "\t/* aaaa\n" 16332 "\t bbbb */\n" 16333 "}", 16334 "{\n" 16335 "/* aaaa\n" 16336 " bbbb */\n" 16337 "}", 16338 Tab); 16339 verifyFormat("{\n" 16340 "\t/*\n" 16341 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 16342 "\t bbbbbbbbbbbbb\n" 16343 "\t*/\n" 16344 "}", 16345 "{\n" 16346 "/*\n" 16347 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 16348 "*/\n" 16349 "}", 16350 Tab); 16351 verifyFormat("{\n" 16352 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 16353 "\t// bbbbbbbbbbbbb\n" 16354 "}", 16355 "{\n" 16356 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 16357 "}", 16358 Tab); 16359 verifyFormat("{\n" 16360 "\t/*\n" 16361 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 16362 "\t bbbbbbbbbbbbb\n" 16363 "\t*/\n" 16364 "}", 16365 "{\n" 16366 "\t/*\n" 16367 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 16368 "\t*/\n" 16369 "}", 16370 Tab); 16371 verifyNoChange("{\n" 16372 "\t/*\n" 16373 "\n" 16374 "\t*/\n" 16375 "}", 16376 Tab); 16377 verifyNoChange("{\n" 16378 "\t/*\n" 16379 " asdf\n" 16380 "\t*/\n" 16381 "}", 16382 Tab); 16383 verifyFormat("/* some\n" 16384 " comment */", 16385 " \t \t /* some\n" 16386 " \t \t comment */", 16387 Tab); 16388 verifyFormat("int a; /* some\n" 16389 " comment */", 16390 " \t \t int a; /* some\n" 16391 " \t \t comment */", 16392 Tab); 16393 verifyFormat("int a; /* some\n" 16394 "comment */", 16395 " \t \t int\ta; /* some\n" 16396 " \t \t comment */", 16397 Tab); 16398 verifyFormat("f(\"\t\t\"); /* some\n" 16399 " comment */", 16400 " \t \t f(\"\t\t\"); /* some\n" 16401 " \t \t comment */", 16402 Tab); 16403 verifyFormat("{\n" 16404 "\t/*\n" 16405 "\t * Comment\n" 16406 "\t */\n" 16407 "\tint i;\n" 16408 "}", 16409 "{\n" 16410 "\t/*\n" 16411 "\t * Comment\n" 16412 "\t */\n" 16413 "\t int i;\n" 16414 "}", 16415 Tab); 16416 Tab.TabWidth = 2; 16417 Tab.IndentWidth = 2; 16418 verifyFormat("{\n" 16419 "\t/* aaaa\n" 16420 "\t\t bbbb */\n" 16421 "}", 16422 "{\n" 16423 "/* aaaa\n" 16424 "\t bbbb */\n" 16425 "}", 16426 Tab); 16427 verifyFormat("{\n" 16428 "\t/*\n" 16429 "\t\taaaaaaaaaaaaaaaaaaaaaaaaaa\n" 16430 "\t\tbbbbbbbbbbbbb\n" 16431 "\t*/\n" 16432 "}", 16433 "{\n" 16434 "/*\n" 16435 "\taaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 16436 "*/\n" 16437 "}", 16438 Tab); 16439 Tab.AlignConsecutiveAssignments.Enabled = true; 16440 Tab.AlignConsecutiveDeclarations.Enabled = true; 16441 Tab.TabWidth = 4; 16442 Tab.IndentWidth = 4; 16443 verifyFormat("class Assign {\n" 16444 "\tvoid f() {\n" 16445 "\t\tint x = 123;\n" 16446 "\t\tint random = 4;\n" 16447 "\t\tstd::string alphabet =\n" 16448 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 16449 "\t}\n" 16450 "};", 16451 Tab); 16452 16453 Tab.UseTab = FormatStyle::UT_AlignWithSpaces; 16454 Tab.TabWidth = 8; 16455 Tab.IndentWidth = 8; 16456 verifyFormat("if (aaaaaaaa && // q\n" 16457 " bb) // w\n" 16458 "\t;", 16459 "if (aaaaaaaa &&// q\n" 16460 "bb)// w\n" 16461 ";", 16462 Tab); 16463 verifyFormat("if (aaa && bbb) // w\n" 16464 "\t;", 16465 "if(aaa&&bbb)// w\n" 16466 ";", 16467 Tab); 16468 verifyFormat("class X {\n" 16469 "\tvoid f() {\n" 16470 "\t\tsomeFunction(parameter1,\n" 16471 "\t\t parameter2);\n" 16472 "\t}\n" 16473 "};", 16474 Tab); 16475 verifyFormat("#define A \\\n" 16476 "\tvoid f() { \\\n" 16477 "\t\tsomeFunction( \\\n" 16478 "\t\t parameter1, \\\n" 16479 "\t\t parameter2); \\\n" 16480 "\t}", 16481 Tab); 16482 Tab.TabWidth = 4; 16483 Tab.IndentWidth = 8; 16484 verifyFormat("class TabWidth4Indent8 {\n" 16485 "\t\tvoid f() {\n" 16486 "\t\t\t\tsomeFunction(parameter1,\n" 16487 "\t\t\t\t parameter2);\n" 16488 "\t\t}\n" 16489 "};", 16490 Tab); 16491 Tab.TabWidth = 4; 16492 Tab.IndentWidth = 4; 16493 verifyFormat("class TabWidth4Indent4 {\n" 16494 "\tvoid f() {\n" 16495 "\t\tsomeFunction(parameter1,\n" 16496 "\t\t parameter2);\n" 16497 "\t}\n" 16498 "};", 16499 Tab); 16500 Tab.TabWidth = 8; 16501 Tab.IndentWidth = 4; 16502 verifyFormat("class TabWidth8Indent4 {\n" 16503 " void f() {\n" 16504 "\tsomeFunction(parameter1,\n" 16505 "\t parameter2);\n" 16506 " }\n" 16507 "};", 16508 Tab); 16509 Tab.TabWidth = 8; 16510 Tab.IndentWidth = 8; 16511 verifyFormat("/*\n" 16512 " a\t\tcomment\n" 16513 " in multiple lines\n" 16514 " */", 16515 " /*\t \t \n" 16516 " \t \t a\t\tcomment\t \t\n" 16517 " \t \t in multiple lines\t\n" 16518 " \t */", 16519 Tab); 16520 verifyFormat("{\n" 16521 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 16522 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 16523 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 16524 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 16525 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 16526 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 16527 "};", 16528 Tab); 16529 verifyFormat("enum AA {\n" 16530 "\ta1, // Force multiple lines\n" 16531 "\ta2,\n" 16532 "\ta3\n" 16533 "};", 16534 Tab); 16535 verifyFormat("if (aaaaaaaa && // q\n" 16536 " bb) // w\n" 16537 "\t;", 16538 "if (aaaaaaaa &&// q\n" 16539 "bb)// w\n" 16540 ";", 16541 Tab); 16542 verifyFormat("class X {\n" 16543 "\tvoid f() {\n" 16544 "\t\tsomeFunction(parameter1,\n" 16545 "\t\t parameter2);\n" 16546 "\t}\n" 16547 "};", 16548 Tab); 16549 verifyFormat("{\n" 16550 "\tQ(\n" 16551 "\t {\n" 16552 "\t\t int a;\n" 16553 "\t\t someFunction(aaaaaaaa,\n" 16554 "\t\t bbbbbbb);\n" 16555 "\t },\n" 16556 "\t p);\n" 16557 "}", 16558 Tab); 16559 verifyFormat("{\n" 16560 "\t/* aaaa\n" 16561 "\t bbbb */\n" 16562 "}", 16563 "{\n" 16564 "/* aaaa\n" 16565 " bbbb */\n" 16566 "}", 16567 Tab); 16568 verifyFormat("{\n" 16569 "\t/*\n" 16570 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 16571 "\t bbbbbbbbbbbbb\n" 16572 "\t*/\n" 16573 "}", 16574 "{\n" 16575 "/*\n" 16576 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 16577 "*/\n" 16578 "}", 16579 Tab); 16580 verifyFormat("{\n" 16581 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 16582 "\t// bbbbbbbbbbbbb\n" 16583 "}", 16584 "{\n" 16585 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 16586 "}", 16587 Tab); 16588 verifyFormat("{\n" 16589 "\t/*\n" 16590 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 16591 "\t bbbbbbbbbbbbb\n" 16592 "\t*/\n" 16593 "}", 16594 "{\n" 16595 "\t/*\n" 16596 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 16597 "\t*/\n" 16598 "}", 16599 Tab); 16600 verifyNoChange("{\n" 16601 "\t/*\n" 16602 "\n" 16603 "\t*/\n" 16604 "}", 16605 Tab); 16606 verifyNoChange("{\n" 16607 "\t/*\n" 16608 " asdf\n" 16609 "\t*/\n" 16610 "}", 16611 Tab); 16612 verifyFormat("/* some\n" 16613 " comment */", 16614 " \t \t /* some\n" 16615 " \t \t comment */", 16616 Tab); 16617 verifyFormat("int a; /* some\n" 16618 " comment */", 16619 " \t \t int a; /* some\n" 16620 " \t \t comment */", 16621 Tab); 16622 verifyFormat("int a; /* some\n" 16623 "comment */", 16624 " \t \t int\ta; /* some\n" 16625 " \t \t comment */", 16626 Tab); 16627 verifyFormat("f(\"\t\t\"); /* some\n" 16628 " comment */", 16629 " \t \t f(\"\t\t\"); /* some\n" 16630 " \t \t comment */", 16631 Tab); 16632 verifyFormat("{\n" 16633 "\t/*\n" 16634 "\t * Comment\n" 16635 "\t */\n" 16636 "\tint i;\n" 16637 "}", 16638 "{\n" 16639 "\t/*\n" 16640 "\t * Comment\n" 16641 "\t */\n" 16642 "\t int i;\n" 16643 "}", 16644 Tab); 16645 Tab.TabWidth = 2; 16646 Tab.IndentWidth = 2; 16647 verifyFormat("{\n" 16648 "\t/* aaaa\n" 16649 "\t bbbb */\n" 16650 "}", 16651 "{\n" 16652 "/* aaaa\n" 16653 " bbbb */\n" 16654 "}", 16655 Tab); 16656 verifyFormat("{\n" 16657 "\t/*\n" 16658 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 16659 "\t bbbbbbbbbbbbb\n" 16660 "\t*/\n" 16661 "}", 16662 "{\n" 16663 "/*\n" 16664 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 16665 "*/\n" 16666 "}", 16667 Tab); 16668 Tab.AlignConsecutiveAssignments.Enabled = true; 16669 Tab.AlignConsecutiveDeclarations.Enabled = true; 16670 Tab.TabWidth = 4; 16671 Tab.IndentWidth = 4; 16672 verifyFormat("class Assign {\n" 16673 "\tvoid f() {\n" 16674 "\t\tint x = 123;\n" 16675 "\t\tint random = 4;\n" 16676 "\t\tstd::string alphabet =\n" 16677 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 16678 "\t}\n" 16679 "};", 16680 Tab); 16681 Tab.AlignOperands = FormatStyle::OAS_Align; 16682 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb +\n" 16683 " cccccccccccccccccccc;", 16684 Tab); 16685 // no alignment 16686 verifyFormat("int aaaaaaaaaa =\n" 16687 "\tbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 16688 Tab); 16689 verifyFormat("return aaaaaaaaaaaaaaaa ? 111111111111111\n" 16690 " : bbbbbbbbbbbbbb ? 222222222222222\n" 16691 " : 333333333333333;", 16692 Tab); 16693 Tab.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 16694 Tab.AlignOperands = FormatStyle::OAS_AlignAfterOperator; 16695 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb\n" 16696 " + cccccccccccccccccccc;", 16697 Tab); 16698 } 16699 16700 TEST_F(FormatTest, ZeroTabWidth) { 16701 FormatStyle Tab = getLLVMStyleWithColumns(42); 16702 Tab.IndentWidth = 8; 16703 Tab.UseTab = FormatStyle::UT_Never; 16704 Tab.TabWidth = 0; 16705 verifyFormat("void a() {\n" 16706 " // line starts with '\t'\n" 16707 "};", 16708 "void a(){\n" 16709 "\t// line starts with '\t'\n" 16710 "};", 16711 Tab); 16712 16713 verifyFormat("void a() {\n" 16714 " // line starts with '\t'\n" 16715 "};", 16716 "void a(){\n" 16717 "\t\t// line starts with '\t'\n" 16718 "};", 16719 Tab); 16720 16721 Tab.UseTab = FormatStyle::UT_ForIndentation; 16722 verifyFormat("void a() {\n" 16723 " // line starts with '\t'\n" 16724 "};", 16725 "void a(){\n" 16726 "\t// line starts with '\t'\n" 16727 "};", 16728 Tab); 16729 16730 verifyFormat("void a() {\n" 16731 " // line starts with '\t'\n" 16732 "};", 16733 "void a(){\n" 16734 "\t\t// line starts with '\t'\n" 16735 "};", 16736 Tab); 16737 16738 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 16739 verifyFormat("void a() {\n" 16740 " // line starts with '\t'\n" 16741 "};", 16742 "void a(){\n" 16743 "\t// line starts with '\t'\n" 16744 "};", 16745 Tab); 16746 16747 verifyFormat("void a() {\n" 16748 " // line starts with '\t'\n" 16749 "};", 16750 "void a(){\n" 16751 "\t\t// line starts with '\t'\n" 16752 "};", 16753 Tab); 16754 16755 Tab.UseTab = FormatStyle::UT_AlignWithSpaces; 16756 verifyFormat("void a() {\n" 16757 " // line starts with '\t'\n" 16758 "};", 16759 "void a(){\n" 16760 "\t// line starts with '\t'\n" 16761 "};", 16762 Tab); 16763 16764 verifyFormat("void a() {\n" 16765 " // line starts with '\t'\n" 16766 "};", 16767 "void a(){\n" 16768 "\t\t// line starts with '\t'\n" 16769 "};", 16770 Tab); 16771 16772 Tab.UseTab = FormatStyle::UT_Always; 16773 verifyFormat("void a() {\n" 16774 "// line starts with '\t'\n" 16775 "};", 16776 "void a(){\n" 16777 "\t// line starts with '\t'\n" 16778 "};", 16779 Tab); 16780 16781 verifyFormat("void a() {\n" 16782 "// line starts with '\t'\n" 16783 "};", 16784 "void a(){\n" 16785 "\t\t// line starts with '\t'\n" 16786 "};", 16787 Tab); 16788 } 16789 16790 TEST_F(FormatTest, CalculatesOriginalColumn) { 16791 verifyFormat("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 16792 "q\"; /* some\n" 16793 " comment */", 16794 " \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 16795 "q\"; /* some\n" 16796 " comment */"); 16797 verifyFormat("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 16798 "/* some\n" 16799 " comment */", 16800 "// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 16801 " /* some\n" 16802 " comment */"); 16803 verifyFormat("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 16804 "qqq\n" 16805 "/* some\n" 16806 " comment */", 16807 "// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 16808 "qqq\n" 16809 " /* some\n" 16810 " comment */"); 16811 verifyFormat("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 16812 "wwww; /* some\n" 16813 " comment */", 16814 " inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 16815 "wwww; /* some\n" 16816 " comment */"); 16817 } 16818 16819 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { 16820 FormatStyle NoSpace = getLLVMStyle(); 16821 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never; 16822 16823 verifyFormat("while(true)\n" 16824 " continue;", 16825 NoSpace); 16826 verifyFormat("for(;;)\n" 16827 " continue;", 16828 NoSpace); 16829 verifyFormat("if(true)\n" 16830 " f();\n" 16831 "else if(true)\n" 16832 " f();", 16833 NoSpace); 16834 verifyFormat("do {\n" 16835 " do_something();\n" 16836 "} while(something());", 16837 NoSpace); 16838 verifyFormat("switch(x) {\n" 16839 "default:\n" 16840 " break;\n" 16841 "}", 16842 NoSpace); 16843 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace); 16844 verifyFormat("size_t x = sizeof(x);", NoSpace); 16845 verifyFormat("auto f(int x) -> decltype(x);", NoSpace); 16846 verifyFormat("auto f(int x) -> typeof(x);", NoSpace); 16847 verifyFormat("auto f(int x) -> _Atomic(x);", NoSpace); 16848 verifyFormat("auto f(int x) -> __underlying_type(x);", NoSpace); 16849 verifyFormat("int f(T x) noexcept(x.create());", NoSpace); 16850 verifyFormat("alignas(128) char a[128];", NoSpace); 16851 verifyFormat("size_t x = alignof(MyType);", NoSpace); 16852 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace); 16853 verifyFormat("int f() throw(Deprecated);", NoSpace); 16854 verifyFormat("typedef void (*cb)(int);", NoSpace); 16855 verifyFormat("T A::operator()();", NoSpace); 16856 verifyFormat("X A::operator++(T);", NoSpace); 16857 verifyFormat("auto lambda = []() { return 0; };", NoSpace); 16858 verifyFormat("#if (foo || bar) && baz\n" 16859 "#elif ((a || b) && c) || d\n" 16860 "#endif", 16861 NoSpace); 16862 16863 FormatStyle Space = getLLVMStyle(); 16864 Space.SpaceBeforeParens = FormatStyle::SBPO_Always; 16865 16866 verifyFormat("int f ();", Space); 16867 verifyFormat("bool operator< ();", Space); 16868 verifyFormat("bool operator> ();", Space); 16869 verifyFormat("void f (int a, T b) {\n" 16870 " while (true)\n" 16871 " continue;\n" 16872 "}", 16873 Space); 16874 verifyFormat("if (true)\n" 16875 " f ();\n" 16876 "else if (true)\n" 16877 " f ();", 16878 Space); 16879 verifyFormat("do {\n" 16880 " do_something ();\n" 16881 "} while (something ());", 16882 Space); 16883 verifyFormat("switch (x) {\n" 16884 "default:\n" 16885 " break;\n" 16886 "}", 16887 Space); 16888 verifyFormat("A::A () : a (1) {}", Space); 16889 verifyFormat("void f () __attribute__ ((asdf));", Space); 16890 verifyFormat("*(&a + 1);\n" 16891 "&((&a)[1]);\n" 16892 "a[(b + c) * d];\n" 16893 "(((a + 1) * 2) + 3) * 4;", 16894 Space); 16895 verifyFormat("#define A(x) x", Space); 16896 verifyFormat("#define A (x) x", Space); 16897 verifyFormat("#if defined(x)\n" 16898 "#endif", 16899 Space); 16900 verifyFormat("auto i = std::make_unique<int> (5);", Space); 16901 verifyFormat("size_t x = sizeof (x);", Space); 16902 verifyFormat("auto f (int x) -> decltype (x);", Space); 16903 verifyFormat("auto f (int x) -> typeof (x);", Space); 16904 verifyFormat("auto f (int x) -> _Atomic (x);", Space); 16905 verifyFormat("auto f (int x) -> __underlying_type (x);", Space); 16906 verifyFormat("int f (T x) noexcept (x.create ());", Space); 16907 verifyFormat("alignas (128) char a[128];", Space); 16908 verifyFormat("size_t x = alignof (MyType);", Space); 16909 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space); 16910 verifyFormat("int f () throw (Deprecated);", Space); 16911 verifyFormat("typedef void (*cb) (int);", Space); 16912 verifyFormat("T A::operator() ();", Space); 16913 verifyFormat("X A::operator++ (T);", Space); 16914 verifyFormat("auto lambda = [] () { return 0; };", Space); 16915 verifyFormat("int x = int (y);", Space); 16916 verifyFormat("#define F(...) __VA_OPT__ (__VA_ARGS__)", Space); 16917 verifyFormat("__builtin_LINE ()", Space); 16918 verifyFormat("__builtin_UNKNOWN ()", Space); 16919 16920 FormatStyle SomeSpace = getLLVMStyle(); 16921 SomeSpace.SpaceBeforeParens = FormatStyle::SBPO_NonEmptyParentheses; 16922 16923 verifyFormat("[]() -> float {}", SomeSpace); 16924 verifyFormat("[] (auto foo) {}", SomeSpace); 16925 verifyFormat("[foo]() -> int {}", SomeSpace); 16926 verifyFormat("int f();", SomeSpace); 16927 verifyFormat("void f (int a, T b) {\n" 16928 " while (true)\n" 16929 " continue;\n" 16930 "}", 16931 SomeSpace); 16932 verifyFormat("if (true)\n" 16933 " f();\n" 16934 "else if (true)\n" 16935 " f();", 16936 SomeSpace); 16937 verifyFormat("do {\n" 16938 " do_something();\n" 16939 "} while (something());", 16940 SomeSpace); 16941 verifyFormat("switch (x) {\n" 16942 "default:\n" 16943 " break;\n" 16944 "}", 16945 SomeSpace); 16946 verifyFormat("A::A() : a (1) {}", SomeSpace); 16947 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace); 16948 verifyFormat("*(&a + 1);\n" 16949 "&((&a)[1]);\n" 16950 "a[(b + c) * d];\n" 16951 "(((a + 1) * 2) + 3) * 4;", 16952 SomeSpace); 16953 verifyFormat("#define A(x) x", SomeSpace); 16954 verifyFormat("#define A (x) x", SomeSpace); 16955 verifyFormat("#if defined(x)\n" 16956 "#endif", 16957 SomeSpace); 16958 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace); 16959 verifyFormat("size_t x = sizeof (x);", SomeSpace); 16960 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace); 16961 verifyFormat("auto f (int x) -> typeof (x);", SomeSpace); 16962 verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace); 16963 verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace); 16964 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace); 16965 verifyFormat("alignas (128) char a[128];", SomeSpace); 16966 verifyFormat("size_t x = alignof (MyType);", SomeSpace); 16967 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", 16968 SomeSpace); 16969 verifyFormat("int f() throw (Deprecated);", SomeSpace); 16970 verifyFormat("typedef void (*cb) (int);", SomeSpace); 16971 verifyFormat("T A::operator()();", SomeSpace); 16972 verifyFormat("X A::operator++ (T);", SomeSpace); 16973 verifyFormat("int x = int (y);", SomeSpace); 16974 verifyFormat("auto lambda = []() { return 0; };", SomeSpace); 16975 16976 FormatStyle SpaceControlStatements = getLLVMStyle(); 16977 SpaceControlStatements.SpaceBeforeParens = FormatStyle::SBPO_Custom; 16978 SpaceControlStatements.SpaceBeforeParensOptions.AfterControlStatements = true; 16979 16980 verifyFormat("while (true)\n" 16981 " continue;", 16982 SpaceControlStatements); 16983 verifyFormat("if (true)\n" 16984 " f();\n" 16985 "else if (true)\n" 16986 " f();", 16987 SpaceControlStatements); 16988 verifyFormat("for (;;) {\n" 16989 " do_something();\n" 16990 "}", 16991 SpaceControlStatements); 16992 verifyFormat("do {\n" 16993 " do_something();\n" 16994 "} while (something());", 16995 SpaceControlStatements); 16996 verifyFormat("switch (x) {\n" 16997 "default:\n" 16998 " break;\n" 16999 "}", 17000 SpaceControlStatements); 17001 17002 FormatStyle SpaceFuncDecl = getLLVMStyle(); 17003 SpaceFuncDecl.SpaceBeforeParens = FormatStyle::SBPO_Custom; 17004 SpaceFuncDecl.SpaceBeforeParensOptions.AfterFunctionDeclarationName = true; 17005 17006 verifyFormat("int f ();", SpaceFuncDecl); 17007 verifyFormat("void f(int a, T b) {}", SpaceFuncDecl); 17008 verifyFormat("void __attribute__((asdf)) f(int a, T b) {}", SpaceFuncDecl); 17009 verifyFormat("A::A() : a(1) {}", SpaceFuncDecl); 17010 verifyFormat("void f () __attribute__((asdf));", SpaceFuncDecl); 17011 verifyFormat("void __attribute__((asdf)) f ();", SpaceFuncDecl); 17012 verifyFormat("#define A(x) x", SpaceFuncDecl); 17013 verifyFormat("#define A (x) x", SpaceFuncDecl); 17014 verifyFormat("#if defined(x)\n" 17015 "#endif", 17016 SpaceFuncDecl); 17017 verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDecl); 17018 verifyFormat("size_t x = sizeof(x);", SpaceFuncDecl); 17019 verifyFormat("auto f (int x) -> decltype(x);", SpaceFuncDecl); 17020 verifyFormat("auto f (int x) -> typeof(x);", SpaceFuncDecl); 17021 verifyFormat("auto f (int x) -> _Atomic(x);", SpaceFuncDecl); 17022 verifyFormat("auto f (int x) -> __underlying_type(x);", SpaceFuncDecl); 17023 verifyFormat("int f (T x) noexcept(x.create());", SpaceFuncDecl); 17024 verifyFormat("alignas(128) char a[128];", SpaceFuncDecl); 17025 verifyFormat("size_t x = alignof(MyType);", SpaceFuncDecl); 17026 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", 17027 SpaceFuncDecl); 17028 verifyFormat("int f () throw(Deprecated);", SpaceFuncDecl); 17029 verifyFormat("typedef void (*cb)(int);", SpaceFuncDecl); 17030 verifyFormat("T A::operator()();", SpaceFuncDecl); 17031 verifyFormat("X A::operator++(T);", SpaceFuncDecl); 17032 verifyFormat("T A::operator()() {}", SpaceFuncDecl); 17033 verifyFormat("auto lambda = []() { return 0; };", SpaceFuncDecl); 17034 verifyFormat("int x = int(y);", SpaceFuncDecl); 17035 verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}", 17036 SpaceFuncDecl); 17037 17038 FormatStyle SpaceFuncDef = getLLVMStyle(); 17039 SpaceFuncDef.SpaceBeforeParens = FormatStyle::SBPO_Custom; 17040 SpaceFuncDef.SpaceBeforeParensOptions.AfterFunctionDefinitionName = true; 17041 17042 verifyFormat("int f();", SpaceFuncDef); 17043 verifyFormat("void f (int a, T b) {}", SpaceFuncDef); 17044 verifyFormat("void __attribute__((asdf)) f (int a, T b) {}", SpaceFuncDef); 17045 verifyFormat("A::A () : a(1) {}", SpaceFuncDef); 17046 verifyFormat("void f() __attribute__((asdf));", SpaceFuncDef); 17047 verifyFormat("void __attribute__((asdf)) f();", SpaceFuncDef); 17048 verifyFormat("#define A(x) x", SpaceFuncDef); 17049 verifyFormat("#define A (x) x", SpaceFuncDef); 17050 verifyFormat("#if defined(x)\n" 17051 "#endif", 17052 SpaceFuncDef); 17053 verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDef); 17054 verifyFormat("size_t x = sizeof(x);", SpaceFuncDef); 17055 verifyFormat("auto f(int x) -> decltype(x);", SpaceFuncDef); 17056 verifyFormat("auto f(int x) -> typeof(x);", SpaceFuncDef); 17057 verifyFormat("auto f(int x) -> _Atomic(x);", SpaceFuncDef); 17058 verifyFormat("auto f(int x) -> __underlying_type(x);", SpaceFuncDef); 17059 verifyFormat("int f(T x) noexcept(x.create());", SpaceFuncDef); 17060 verifyFormat("alignas(128) char a[128];", SpaceFuncDef); 17061 verifyFormat("size_t x = alignof(MyType);", SpaceFuncDef); 17062 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", 17063 SpaceFuncDef); 17064 verifyFormat("int f() throw(Deprecated);", SpaceFuncDef); 17065 verifyFormat("typedef void (*cb)(int);", SpaceFuncDef); 17066 verifyFormat("T A::operator()();", SpaceFuncDef); 17067 verifyFormat("X A::operator++(T);", SpaceFuncDef); 17068 verifyFormat("T A::operator()() {}", SpaceFuncDef); 17069 verifyFormat("auto lambda = [] () { return 0; };", SpaceFuncDef); 17070 verifyFormat("int x = int(y);", SpaceFuncDef); 17071 verifyFormat("void foo::bar () {}", SpaceFuncDef); 17072 verifyFormat("M (std::size_t R, std::size_t C) : C(C), data(R) {}", 17073 SpaceFuncDef); 17074 17075 FormatStyle SpaceIfMacros = getLLVMStyle(); 17076 SpaceIfMacros.IfMacros.clear(); 17077 SpaceIfMacros.IfMacros.push_back("MYIF"); 17078 SpaceIfMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom; 17079 SpaceIfMacros.SpaceBeforeParensOptions.AfterIfMacros = true; 17080 verifyFormat("MYIF (a)\n return;", SpaceIfMacros); 17081 verifyFormat("MYIF (a)\n return;\nelse MYIF (b)\n return;", SpaceIfMacros); 17082 verifyFormat("MYIF (a)\n return;\nelse\n return;", SpaceIfMacros); 17083 17084 FormatStyle SpaceForeachMacros = getLLVMStyle(); 17085 EXPECT_EQ(SpaceForeachMacros.AllowShortBlocksOnASingleLine, 17086 FormatStyle::SBS_Never); 17087 EXPECT_EQ(SpaceForeachMacros.AllowShortLoopsOnASingleLine, false); 17088 SpaceForeachMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom; 17089 SpaceForeachMacros.SpaceBeforeParensOptions.AfterForeachMacros = true; 17090 verifyFormat("for (;;) {\n" 17091 "}", 17092 SpaceForeachMacros); 17093 verifyFormat("foreach (Item *item, itemlist) {\n" 17094 "}", 17095 SpaceForeachMacros); 17096 verifyFormat("Q_FOREACH (Item *item, itemlist) {\n" 17097 "}", 17098 SpaceForeachMacros); 17099 verifyFormat("BOOST_FOREACH (Item *item, itemlist) {\n" 17100 "}", 17101 SpaceForeachMacros); 17102 verifyFormat("UNKNOWN_FOREACH(Item *item, itemlist) {}", SpaceForeachMacros); 17103 17104 FormatStyle SomeSpace2 = getLLVMStyle(); 17105 SomeSpace2.SpaceBeforeParens = FormatStyle::SBPO_Custom; 17106 SomeSpace2.SpaceBeforeParensOptions.BeforeNonEmptyParentheses = true; 17107 verifyFormat("[]() -> float {}", SomeSpace2); 17108 verifyFormat("[] (auto foo) {}", SomeSpace2); 17109 verifyFormat("[foo]() -> int {}", SomeSpace2); 17110 verifyFormat("int f();", SomeSpace2); 17111 verifyFormat("void f (int a, T b) {\n" 17112 " while (true)\n" 17113 " continue;\n" 17114 "}", 17115 SomeSpace2); 17116 verifyFormat("if (true)\n" 17117 " f();\n" 17118 "else if (true)\n" 17119 " f();", 17120 SomeSpace2); 17121 verifyFormat("do {\n" 17122 " do_something();\n" 17123 "} while (something());", 17124 SomeSpace2); 17125 verifyFormat("switch (x) {\n" 17126 "default:\n" 17127 " break;\n" 17128 "}", 17129 SomeSpace2); 17130 verifyFormat("A::A() : a (1) {}", SomeSpace2); 17131 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace2); 17132 verifyFormat("*(&a + 1);\n" 17133 "&((&a)[1]);\n" 17134 "a[(b + c) * d];\n" 17135 "(((a + 1) * 2) + 3) * 4;", 17136 SomeSpace2); 17137 verifyFormat("#define A(x) x", SomeSpace2); 17138 verifyFormat("#define A (x) x", SomeSpace2); 17139 verifyFormat("#if defined(x)\n" 17140 "#endif", 17141 SomeSpace2); 17142 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace2); 17143 verifyFormat("size_t x = sizeof (x);", SomeSpace2); 17144 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace2); 17145 verifyFormat("auto f (int x) -> typeof (x);", SomeSpace2); 17146 verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace2); 17147 verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace2); 17148 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace2); 17149 verifyFormat("alignas (128) char a[128];", SomeSpace2); 17150 verifyFormat("size_t x = alignof (MyType);", SomeSpace2); 17151 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", 17152 SomeSpace2); 17153 verifyFormat("int f() throw (Deprecated);", SomeSpace2); 17154 verifyFormat("typedef void (*cb) (int);", SomeSpace2); 17155 verifyFormat("T A::operator()();", SomeSpace2); 17156 verifyFormat("X A::operator++ (T);", SomeSpace2); 17157 verifyFormat("int x = int (y);", SomeSpace2); 17158 verifyFormat("auto lambda = []() { return 0; };", SomeSpace2); 17159 17160 FormatStyle SpaceAfterOverloadedOperator = getLLVMStyle(); 17161 SpaceAfterOverloadedOperator.SpaceBeforeParens = FormatStyle::SBPO_Custom; 17162 SpaceAfterOverloadedOperator.SpaceBeforeParensOptions 17163 .AfterOverloadedOperator = true; 17164 17165 verifyFormat("auto operator++ () -> int;", SpaceAfterOverloadedOperator); 17166 verifyFormat("X A::operator++ ();", SpaceAfterOverloadedOperator); 17167 verifyFormat("some_object.operator++ ();", SpaceAfterOverloadedOperator); 17168 verifyFormat("auto func() -> int;", SpaceAfterOverloadedOperator); 17169 17170 SpaceAfterOverloadedOperator.SpaceBeforeParensOptions 17171 .AfterOverloadedOperator = false; 17172 17173 verifyFormat("auto operator++() -> int;", SpaceAfterOverloadedOperator); 17174 verifyFormat("X A::operator++();", SpaceAfterOverloadedOperator); 17175 verifyFormat("some_object.operator++();", SpaceAfterOverloadedOperator); 17176 verifyFormat("auto func() -> int;", SpaceAfterOverloadedOperator); 17177 17178 auto SpaceAfterRequires = getLLVMStyle(); 17179 SpaceAfterRequires.SpaceBeforeParens = FormatStyle::SBPO_Custom; 17180 EXPECT_FALSE( 17181 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInClause); 17182 EXPECT_FALSE( 17183 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInExpression); 17184 verifyFormat("void f(auto x)\n" 17185 " requires requires(int i) { x + i; }\n" 17186 "{}", 17187 SpaceAfterRequires); 17188 verifyFormat("void f(auto x)\n" 17189 " requires(requires(int i) { x + i; })\n" 17190 "{}", 17191 SpaceAfterRequires); 17192 verifyFormat("if (requires(int i) { x + i; })\n" 17193 " return;", 17194 SpaceAfterRequires); 17195 verifyFormat("bool b = requires(int i) { x + i; };", SpaceAfterRequires); 17196 verifyFormat("template <typename T>\n" 17197 " requires(Foo<T>)\n" 17198 "class Bar;", 17199 SpaceAfterRequires); 17200 17201 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInClause = true; 17202 verifyFormat("void f(auto x)\n" 17203 " requires requires(int i) { x + i; }\n" 17204 "{}", 17205 SpaceAfterRequires); 17206 verifyFormat("void f(auto x)\n" 17207 " requires (requires(int i) { x + i; })\n" 17208 "{}", 17209 SpaceAfterRequires); 17210 verifyFormat("if (requires(int i) { x + i; })\n" 17211 " return;", 17212 SpaceAfterRequires); 17213 verifyFormat("bool b = requires(int i) { x + i; };", SpaceAfterRequires); 17214 verifyFormat("template <typename T>\n" 17215 " requires (Foo<T>)\n" 17216 "class Bar;", 17217 SpaceAfterRequires); 17218 17219 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInClause = false; 17220 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInExpression = true; 17221 verifyFormat("void f(auto x)\n" 17222 " requires requires (int i) { x + i; }\n" 17223 "{}", 17224 SpaceAfterRequires); 17225 verifyFormat("void f(auto x)\n" 17226 " requires(requires (int i) { x + i; })\n" 17227 "{}", 17228 SpaceAfterRequires); 17229 verifyFormat("if (requires (int i) { x + i; })\n" 17230 " return;", 17231 SpaceAfterRequires); 17232 verifyFormat("bool b = requires (int i) { x + i; };", SpaceAfterRequires); 17233 verifyFormat("template <typename T>\n" 17234 " requires(Foo<T>)\n" 17235 "class Bar;", 17236 SpaceAfterRequires); 17237 17238 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInClause = true; 17239 verifyFormat("void f(auto x)\n" 17240 " requires requires (int i) { x + i; }\n" 17241 "{}", 17242 SpaceAfterRequires); 17243 verifyFormat("void f(auto x)\n" 17244 " requires (requires (int i) { x + i; })\n" 17245 "{}", 17246 SpaceAfterRequires); 17247 verifyFormat("if (requires (int i) { x + i; })\n" 17248 " return;", 17249 SpaceAfterRequires); 17250 verifyFormat("bool b = requires (int i) { x + i; };", SpaceAfterRequires); 17251 verifyFormat("template <typename T>\n" 17252 " requires (Foo<T>)\n" 17253 "class Bar;", 17254 SpaceAfterRequires); 17255 } 17256 17257 TEST_F(FormatTest, SpaceAfterLogicalNot) { 17258 FormatStyle Spaces = getLLVMStyle(); 17259 Spaces.SpaceAfterLogicalNot = true; 17260 17261 verifyFormat("bool x = ! y", Spaces); 17262 verifyFormat("if (! isFailure())", Spaces); 17263 verifyFormat("if (! (a && b))", Spaces); 17264 verifyFormat("\"Error!\"", Spaces); 17265 verifyFormat("! ! x", Spaces); 17266 } 17267 17268 TEST_F(FormatTest, ConfigurableSpacesInParens) { 17269 FormatStyle Spaces = getLLVMStyle(); 17270 17271 verifyFormat("do_something(::globalVar);", Spaces); 17272 verifyFormat("call(x, y, z);", Spaces); 17273 verifyFormat("call();", Spaces); 17274 verifyFormat("std::function<void(int, int)> callback;", Spaces); 17275 verifyFormat("void inFunction() { std::function<void(int, int)> fct; }", 17276 Spaces); 17277 verifyFormat("while ((bool)1)\n" 17278 " continue;", 17279 Spaces); 17280 verifyFormat("for (;;)\n" 17281 " continue;", 17282 Spaces); 17283 verifyFormat("if (true)\n" 17284 " f();\n" 17285 "else if (true)\n" 17286 " f();", 17287 Spaces); 17288 verifyFormat("do {\n" 17289 " do_something((int)i);\n" 17290 "} while (something());", 17291 Spaces); 17292 verifyFormat("switch (x) {\n" 17293 "default:\n" 17294 " break;\n" 17295 "}", 17296 Spaces); 17297 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces); 17298 verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces); 17299 verifyFormat("void f() __attribute__((asdf));", Spaces); 17300 verifyFormat("x = (int32)y;", Spaces); 17301 verifyFormat("y = ((int (*)(int))foo)(x);", Spaces); 17302 verifyFormat("decltype(x) y = 42;", Spaces); 17303 verifyFormat("decltype((x)) y = z;", Spaces); 17304 verifyFormat("decltype((foo())) a = foo();", Spaces); 17305 verifyFormat("decltype((bar(10))) a = bar(11);", Spaces); 17306 verifyFormat("if ((x - y) && (a ^ b))\n" 17307 " f();", 17308 Spaces); 17309 verifyFormat("for (int i = 0; i < 10; i = (i + 1))\n" 17310 " foo(i);", 17311 Spaces); 17312 verifyFormat("switch (x / (y + z)) {\n" 17313 "default:\n" 17314 " break;\n" 17315 "}", 17316 Spaces); 17317 17318 Spaces.SpacesInParens = FormatStyle::SIPO_Custom; 17319 Spaces.SpacesInParensOptions = {}; 17320 Spaces.SpacesInParensOptions.Other = true; 17321 17322 EXPECT_FALSE(Spaces.SpacesInParensOptions.InConditionalStatements); 17323 verifyFormat("if (a)\n" 17324 " return;", 17325 Spaces); 17326 17327 Spaces.SpacesInParensOptions.InConditionalStatements = true; 17328 verifyFormat("do_something( ::globalVar );", Spaces); 17329 verifyFormat("call( x, y, z );", Spaces); 17330 verifyFormat("call();", Spaces); 17331 verifyFormat("std::function<void( int, int )> callback;", Spaces); 17332 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }", 17333 Spaces); 17334 verifyFormat("while ( (bool)1 )\n" 17335 " continue;", 17336 Spaces); 17337 verifyFormat("for ( ;; )\n" 17338 " continue;", 17339 Spaces); 17340 verifyFormat("if ( true )\n" 17341 " f();\n" 17342 "else if ( true )\n" 17343 " f();", 17344 Spaces); 17345 verifyFormat("do {\n" 17346 " do_something( (int)i );\n" 17347 "} while ( something() );", 17348 Spaces); 17349 verifyFormat("switch ( x ) {\n" 17350 "default:\n" 17351 " break;\n" 17352 "}", 17353 Spaces); 17354 verifyFormat("SomeType *__attribute__( ( attr ) ) *a = NULL;", Spaces); 17355 verifyFormat("void __attribute__( ( naked ) ) foo( int bar )", Spaces); 17356 verifyFormat("void f() __attribute__( ( asdf ) );", Spaces); 17357 verifyFormat("x = (int32)y;", Spaces); 17358 verifyFormat("y = ( (int ( * )( int ))foo )( x );", Spaces); 17359 verifyFormat("decltype( x ) y = 42;", Spaces); 17360 verifyFormat("decltype( ( x ) ) y = z;", Spaces); 17361 verifyFormat("decltype( ( foo() ) ) a = foo();", Spaces); 17362 verifyFormat("decltype( ( bar( 10 ) ) ) a = bar( 11 );", Spaces); 17363 verifyFormat("if ( ( x - y ) && ( a ^ b ) )\n" 17364 " f();", 17365 Spaces); 17366 verifyFormat("for ( int i = 0; i < 10; i = ( i + 1 ) )\n" 17367 " foo( i );", 17368 Spaces); 17369 verifyFormat("switch ( x / ( y + z ) ) {\n" 17370 "default:\n" 17371 " break;\n" 17372 "}", 17373 Spaces); 17374 17375 Spaces.SpacesInParens = FormatStyle::SIPO_Custom; 17376 Spaces.SpacesInParensOptions = {}; 17377 Spaces.SpacesInParensOptions.InCStyleCasts = true; 17378 verifyFormat("Type *A = ( Type * )P;", Spaces); 17379 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces); 17380 verifyFormat("x = ( int32 )y;", Spaces); 17381 verifyFormat("throw ( int32 )x;", Spaces); 17382 verifyFormat("int a = ( int )(2.0f);", Spaces); 17383 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces); 17384 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces); 17385 verifyFormat("#define x (( int )-1)", Spaces); 17386 verifyFormat("y = (( int (*)(int) )foo)(x);", Spaces); 17387 17388 // Run the first set of tests again with: 17389 Spaces.SpacesInParens = FormatStyle::SIPO_Custom; 17390 Spaces.SpacesInParensOptions = {}; 17391 Spaces.SpacesInParensOptions.InEmptyParentheses = true; 17392 Spaces.SpacesInParensOptions.InCStyleCasts = true; 17393 verifyFormat("call(x, y, z);", Spaces); 17394 verifyFormat("call( );", Spaces); 17395 verifyFormat("std::function<void(int, int)> callback;", Spaces); 17396 verifyFormat("while (( bool )1)\n" 17397 " continue;", 17398 Spaces); 17399 verifyFormat("for (;;)\n" 17400 " continue;", 17401 Spaces); 17402 verifyFormat("if (true)\n" 17403 " f( );\n" 17404 "else if (true)\n" 17405 " f( );", 17406 Spaces); 17407 verifyFormat("do {\n" 17408 " do_something(( int )i);\n" 17409 "} while (something( ));", 17410 Spaces); 17411 verifyFormat("switch (x) {\n" 17412 "default:\n" 17413 " break;\n" 17414 "}", 17415 Spaces); 17416 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces); 17417 verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces); 17418 verifyFormat("void f( ) __attribute__((asdf));", Spaces); 17419 verifyFormat("x = ( int32 )y;", Spaces); 17420 verifyFormat("y = (( int (*)(int) )foo)(x);", Spaces); 17421 verifyFormat("decltype(x) y = 42;", Spaces); 17422 verifyFormat("decltype((x)) y = z;", Spaces); 17423 verifyFormat("decltype((foo( ))) a = foo( );", Spaces); 17424 verifyFormat("decltype((bar(10))) a = bar(11);", Spaces); 17425 verifyFormat("if ((x - y) && (a ^ b))\n" 17426 " f( );", 17427 Spaces); 17428 verifyFormat("for (int i = 0; i < 10; i = (i + 1))\n" 17429 " foo(i);", 17430 Spaces); 17431 verifyFormat("switch (x / (y + z)) {\n" 17432 "default:\n" 17433 " break;\n" 17434 "}", 17435 Spaces); 17436 17437 // Run the first set of tests again with: 17438 Spaces.SpaceAfterCStyleCast = true; 17439 verifyFormat("call(x, y, z);", Spaces); 17440 verifyFormat("call( );", Spaces); 17441 verifyFormat("std::function<void(int, int)> callback;", Spaces); 17442 verifyFormat("while (( bool ) 1)\n" 17443 " continue;", 17444 Spaces); 17445 verifyFormat("for (;;)\n" 17446 " continue;", 17447 Spaces); 17448 verifyFormat("if (true)\n" 17449 " f( );\n" 17450 "else if (true)\n" 17451 " f( );", 17452 Spaces); 17453 verifyFormat("do {\n" 17454 " do_something(( int ) i);\n" 17455 "} while (something( ));", 17456 Spaces); 17457 verifyFormat("switch (x) {\n" 17458 "default:\n" 17459 " break;\n" 17460 "}", 17461 Spaces); 17462 verifyFormat("#define CONF_BOOL(x) ( bool * ) ( void * ) (x)", Spaces); 17463 verifyFormat("#define CONF_BOOL(x) ( bool * ) (x)", Spaces); 17464 verifyFormat("#define CONF_BOOL(x) ( bool ) (x)", Spaces); 17465 verifyFormat("bool *y = ( bool * ) ( void * ) (x);", Spaces); 17466 verifyFormat("bool *y = ( bool * ) (x);", Spaces); 17467 verifyFormat("throw ( int32 ) x;", Spaces); 17468 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces); 17469 verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces); 17470 verifyFormat("void f( ) __attribute__((asdf));", Spaces); 17471 17472 // Run subset of tests again with: 17473 Spaces.SpacesInParensOptions.InCStyleCasts = false; 17474 Spaces.SpaceAfterCStyleCast = true; 17475 verifyFormat("while ((bool) 1)\n" 17476 " continue;", 17477 Spaces); 17478 verifyFormat("do {\n" 17479 " do_something((int) i);\n" 17480 "} while (something( ));", 17481 Spaces); 17482 17483 verifyFormat("size_t idx = (size_t) (ptr - ((char *) file));", Spaces); 17484 verifyFormat("size_t idx = (size_t) a;", Spaces); 17485 verifyFormat("size_t idx = (size_t) (a - 1);", Spaces); 17486 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces); 17487 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces); 17488 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces); 17489 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces); 17490 verifyFormat("#define CONF_BOOL(x) (bool *) (void *) (x)", Spaces); 17491 verifyFormat("#define CONF_BOOL(x) (bool *) (void *) (int) (x)", Spaces); 17492 verifyFormat("bool *y = (bool *) (void *) (x);", Spaces); 17493 verifyFormat("bool *y = (bool *) (void *) (int) (x);", Spaces); 17494 verifyFormat("bool *y = (bool *) (void *) (int) foo(x);", Spaces); 17495 verifyFormat("throw (int32) x;", Spaces); 17496 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces); 17497 verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces); 17498 verifyFormat("void f( ) __attribute__((asdf));", Spaces); 17499 17500 Spaces.ColumnLimit = 80; 17501 Spaces.IndentWidth = 4; 17502 Spaces.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 17503 verifyFormat("void foo( ) {\n" 17504 " size_t foo = (*(function))(\n" 17505 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, " 17506 "BarrrrrrrrrrrrLong,\n" 17507 " FoooooooooLooooong);\n" 17508 "}", 17509 Spaces); 17510 Spaces.SpaceAfterCStyleCast = false; 17511 verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces); 17512 verifyFormat("size_t idx = (size_t)a;", Spaces); 17513 verifyFormat("size_t idx = (size_t)(a - 1);", Spaces); 17514 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces); 17515 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces); 17516 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces); 17517 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces); 17518 17519 verifyFormat("void foo( ) {\n" 17520 " size_t foo = (*(function))(\n" 17521 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, " 17522 "BarrrrrrrrrrrrLong,\n" 17523 " FoooooooooLooooong);\n" 17524 "}", 17525 Spaces); 17526 17527 Spaces.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; 17528 verifyFormat("void foo( ) {\n" 17529 " size_t foo = (*(function))(\n" 17530 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, " 17531 "BarrrrrrrrrrrrLong,\n" 17532 " FoooooooooLooooong\n" 17533 " );\n" 17534 "}", 17535 Spaces); 17536 verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces); 17537 verifyFormat("size_t idx = (size_t)a;", Spaces); 17538 verifyFormat("size_t idx = (size_t)(a - 1);", Spaces); 17539 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces); 17540 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces); 17541 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces); 17542 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces); 17543 17544 // Check ExceptDoubleParentheses spaces 17545 Spaces.IndentWidth = 2; 17546 Spaces.SpacesInParens = FormatStyle::SIPO_Custom; 17547 Spaces.SpacesInParensOptions = {}; 17548 Spaces.SpacesInParensOptions.Other = true; 17549 Spaces.SpacesInParensOptions.ExceptDoubleParentheses = true; 17550 verifyFormat("SomeType *__attribute__(( attr )) *a = NULL;", Spaces); 17551 verifyFormat("void __attribute__(( naked )) foo( int bar )", Spaces); 17552 verifyFormat("void f() __attribute__(( asdf ));", Spaces); 17553 verifyFormat("__attribute__(( __aligned__( x ) )) z;", Spaces); 17554 verifyFormat("int x __attribute__(( aligned( 16 ) )) = 0;", Spaces); 17555 verifyFormat("class __declspec( dllimport ) X {};", Spaces); 17556 verifyFormat("class __declspec(( dllimport )) X {};", Spaces); 17557 verifyFormat("int x = ( ( a - 1 ) * 3 );", Spaces); 17558 verifyFormat("int x = ( 3 * ( a - 1 ) );", Spaces); 17559 verifyFormat("decltype( x ) y = 42;", Spaces); 17560 verifyFormat("decltype(( bar( 10 ) )) a = bar( 11 );", Spaces); 17561 verifyFormat("if (( i = j ))\n" 17562 " do_something( i );", 17563 Spaces); 17564 17565 Spaces.SpacesInParens = FormatStyle::SIPO_Custom; 17566 Spaces.SpacesInParensOptions = {}; 17567 Spaces.SpacesInParensOptions.InConditionalStatements = true; 17568 Spaces.SpacesInParensOptions.ExceptDoubleParentheses = true; 17569 verifyFormat("while ( (bool)1 )\n" 17570 " continue;", 17571 Spaces); 17572 verifyFormat("while ((i = j))\n" 17573 " continue;", 17574 Spaces); 17575 verifyFormat("do {\n" 17576 " do_something((int)i);\n" 17577 "} while ( something() );", 17578 Spaces); 17579 verifyFormat("do {\n" 17580 " do_something((int)i);\n" 17581 "} while ((i = i + 1));", 17582 Spaces); 17583 verifyFormat("if ( (x - y) && (a ^ b) )\n" 17584 " f();", 17585 Spaces); 17586 verifyFormat("if ((i = j))\n" 17587 " do_something(i);", 17588 Spaces); 17589 verifyFormat("for ( int i = 0; i < 10; i = (i + 1) )\n" 17590 " foo(i);", 17591 Spaces); 17592 verifyFormat("switch ( x / (y + z) ) {\n" 17593 "default:\n" 17594 " break;\n" 17595 "}", 17596 Spaces); 17597 verifyFormat("if constexpr ((a = b))\n" 17598 " c;", 17599 Spaces); 17600 } 17601 17602 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) { 17603 verifyFormat("int a[5];"); 17604 verifyFormat("a[3] += 42;"); 17605 17606 FormatStyle Spaces = getLLVMStyle(); 17607 Spaces.SpacesInSquareBrackets = true; 17608 // Not lambdas. 17609 verifyFormat("int a[ 5 ];", Spaces); 17610 verifyFormat("a[ 3 ] += 42;", Spaces); 17611 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces); 17612 verifyFormat("double &operator[](int i) { return 0; }\n" 17613 "int i;", 17614 Spaces); 17615 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces); 17616 verifyFormat("int i = a[ a ][ a ]->f();", Spaces); 17617 verifyFormat("int i = (*b)[ a ]->f();", Spaces); 17618 // Lambdas. 17619 verifyFormat("int c = []() -> int { return 2; }();", Spaces); 17620 verifyFormat("return [ i, args... ] {};", Spaces); 17621 verifyFormat("int foo = [ &bar ]() {};", Spaces); 17622 verifyFormat("int foo = [ = ]() {};", Spaces); 17623 verifyFormat("int foo = [ & ]() {};", Spaces); 17624 verifyFormat("int foo = [ =, &bar ]() {};", Spaces); 17625 verifyFormat("int foo = [ &bar, = ]() {};", Spaces); 17626 } 17627 17628 TEST_F(FormatTest, ConfigurableSpaceBeforeBrackets) { 17629 FormatStyle NoSpaceStyle = getLLVMStyle(); 17630 verifyFormat("int a[5];", NoSpaceStyle); 17631 verifyFormat("a[3] += 42;", NoSpaceStyle); 17632 17633 verifyFormat("int a[1];", NoSpaceStyle); 17634 verifyFormat("int 1 [a];", NoSpaceStyle); 17635 verifyFormat("int a[1][2];", NoSpaceStyle); 17636 verifyFormat("a[7] = 5;", NoSpaceStyle); 17637 verifyFormat("int a = (f())[23];", NoSpaceStyle); 17638 verifyFormat("f([] {})", NoSpaceStyle); 17639 17640 FormatStyle Space = getLLVMStyle(); 17641 Space.SpaceBeforeSquareBrackets = true; 17642 verifyFormat("int c = []() -> int { return 2; }();", Space); 17643 verifyFormat("return [i, args...] {};", Space); 17644 17645 verifyFormat("int a [5];", Space); 17646 verifyFormat("a [3] += 42;", Space); 17647 verifyFormat("constexpr char hello []{\"hello\"};", Space); 17648 verifyFormat("double &operator[](int i) { return 0; }\n" 17649 "int i;", 17650 Space); 17651 verifyFormat("std::unique_ptr<int []> foo() {}", Space); 17652 verifyFormat("int i = a [a][a]->f();", Space); 17653 verifyFormat("int i = (*b) [a]->f();", Space); 17654 17655 verifyFormat("int a [1];", Space); 17656 verifyFormat("int 1 [a];", Space); 17657 verifyFormat("int a [1][2];", Space); 17658 verifyFormat("a [7] = 5;", Space); 17659 verifyFormat("int a = (f()) [23];", Space); 17660 verifyFormat("f([] {})", Space); 17661 } 17662 17663 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) { 17664 verifyFormat("int a = 5;"); 17665 verifyFormat("a += 42;"); 17666 verifyFormat("a or_eq 8;"); 17667 verifyFormat("xor = foo;"); 17668 17669 FormatStyle Spaces = getLLVMStyle(); 17670 Spaces.SpaceBeforeAssignmentOperators = false; 17671 verifyFormat("int a= 5;", Spaces); 17672 verifyFormat("a+= 42;", Spaces); 17673 verifyFormat("a or_eq 8;", Spaces); 17674 verifyFormat("xor= foo;", Spaces); 17675 } 17676 17677 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) { 17678 verifyFormat("class Foo : public Bar {};"); 17679 verifyFormat("Foo::Foo() : foo(1) {}"); 17680 verifyFormat("for (auto a : b) {\n}"); 17681 verifyFormat("int x = a ? b : c;"); 17682 verifyFormat("{\n" 17683 "label0:\n" 17684 " int x = 0;\n" 17685 "}"); 17686 verifyFormat("switch (x) {\n" 17687 "case 1:\n" 17688 "default:\n" 17689 "}"); 17690 verifyFormat("switch (allBraces) {\n" 17691 "case 1: {\n" 17692 " break;\n" 17693 "}\n" 17694 "case 2: {\n" 17695 " [[fallthrough]];\n" 17696 "}\n" 17697 "default: {\n" 17698 " break;\n" 17699 "}\n" 17700 "}"); 17701 17702 FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30); 17703 CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false; 17704 verifyFormat("class Foo : public Bar {};", CtorInitializerStyle); 17705 verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle); 17706 verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle); 17707 verifyFormat("int x = a ? b : c;", CtorInitializerStyle); 17708 verifyFormat("{\n" 17709 "label1:\n" 17710 " int x = 0;\n" 17711 "}", 17712 CtorInitializerStyle); 17713 verifyFormat("switch (x) {\n" 17714 "case 1:\n" 17715 "default:\n" 17716 "}", 17717 CtorInitializerStyle); 17718 verifyFormat("switch (allBraces) {\n" 17719 "case 1: {\n" 17720 " break;\n" 17721 "}\n" 17722 "case 2: {\n" 17723 " [[fallthrough]];\n" 17724 "}\n" 17725 "default: {\n" 17726 " break;\n" 17727 "}\n" 17728 "}", 17729 CtorInitializerStyle); 17730 CtorInitializerStyle.BreakConstructorInitializers = 17731 FormatStyle::BCIS_AfterColon; 17732 verifyFormat("Fooooooooooo::Fooooooooooo():\n" 17733 " aaaaaaaaaaaaaaaa(1),\n" 17734 " bbbbbbbbbbbbbbbb(2) {}", 17735 CtorInitializerStyle); 17736 CtorInitializerStyle.BreakConstructorInitializers = 17737 FormatStyle::BCIS_BeforeComma; 17738 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 17739 " : aaaaaaaaaaaaaaaa(1)\n" 17740 " , bbbbbbbbbbbbbbbb(2) {}", 17741 CtorInitializerStyle); 17742 CtorInitializerStyle.BreakConstructorInitializers = 17743 FormatStyle::BCIS_BeforeColon; 17744 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 17745 " : aaaaaaaaaaaaaaaa(1),\n" 17746 " bbbbbbbbbbbbbbbb(2) {}", 17747 CtorInitializerStyle); 17748 CtorInitializerStyle.ConstructorInitializerIndentWidth = 0; 17749 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 17750 ": aaaaaaaaaaaaaaaa(1),\n" 17751 " bbbbbbbbbbbbbbbb(2) {}", 17752 CtorInitializerStyle); 17753 17754 FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30); 17755 InheritanceStyle.SpaceBeforeInheritanceColon = false; 17756 verifyFormat("class Foo: public Bar {};", InheritanceStyle); 17757 verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle); 17758 verifyFormat("for (auto a : b) {\n}", InheritanceStyle); 17759 verifyFormat("int x = a ? b : c;", InheritanceStyle); 17760 verifyFormat("{\n" 17761 "label2:\n" 17762 " int x = 0;\n" 17763 "}", 17764 InheritanceStyle); 17765 verifyFormat("switch (x) {\n" 17766 "case 1:\n" 17767 "default:\n" 17768 "}", 17769 InheritanceStyle); 17770 verifyFormat("switch (allBraces) {\n" 17771 "case 1: {\n" 17772 " break;\n" 17773 "}\n" 17774 "case 2: {\n" 17775 " [[fallthrough]];\n" 17776 "}\n" 17777 "default: {\n" 17778 " break;\n" 17779 "}\n" 17780 "}", 17781 InheritanceStyle); 17782 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterComma; 17783 verifyFormat("class Foooooooooooooooooooooo\n" 17784 " : public aaaaaaaaaaaaaaaaaa,\n" 17785 " public bbbbbbbbbbbbbbbbbb {\n" 17786 "}", 17787 InheritanceStyle); 17788 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon; 17789 verifyFormat("class Foooooooooooooooooooooo:\n" 17790 " public aaaaaaaaaaaaaaaaaa,\n" 17791 " public bbbbbbbbbbbbbbbbbb {\n" 17792 "}", 17793 InheritanceStyle); 17794 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma; 17795 verifyFormat("class Foooooooooooooooooooooo\n" 17796 " : public aaaaaaaaaaaaaaaaaa\n" 17797 " , public bbbbbbbbbbbbbbbbbb {\n" 17798 "}", 17799 InheritanceStyle); 17800 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 17801 verifyFormat("class Foooooooooooooooooooooo\n" 17802 " : public aaaaaaaaaaaaaaaaaa,\n" 17803 " public bbbbbbbbbbbbbbbbbb {\n" 17804 "}", 17805 InheritanceStyle); 17806 InheritanceStyle.ConstructorInitializerIndentWidth = 0; 17807 verifyFormat("class Foooooooooooooooooooooo\n" 17808 ": public aaaaaaaaaaaaaaaaaa,\n" 17809 " public bbbbbbbbbbbbbbbbbb {}", 17810 InheritanceStyle); 17811 17812 FormatStyle ForLoopStyle = getLLVMStyle(); 17813 ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false; 17814 verifyFormat("class Foo : public Bar {};", ForLoopStyle); 17815 verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle); 17816 verifyFormat("for (auto a: b) {\n}", ForLoopStyle); 17817 verifyFormat("int x = a ? b : c;", ForLoopStyle); 17818 verifyFormat("{\n" 17819 "label2:\n" 17820 " int x = 0;\n" 17821 "}", 17822 ForLoopStyle); 17823 verifyFormat("switch (x) {\n" 17824 "case 1:\n" 17825 "default:\n" 17826 "}", 17827 ForLoopStyle); 17828 verifyFormat("switch (allBraces) {\n" 17829 "case 1: {\n" 17830 " break;\n" 17831 "}\n" 17832 "case 2: {\n" 17833 " [[fallthrough]];\n" 17834 "}\n" 17835 "default: {\n" 17836 " break;\n" 17837 "}\n" 17838 "}", 17839 ForLoopStyle); 17840 17841 FormatStyle CaseStyle = getLLVMStyle(); 17842 CaseStyle.SpaceBeforeCaseColon = true; 17843 verifyFormat("class Foo : public Bar {};", CaseStyle); 17844 verifyFormat("Foo::Foo() : foo(1) {}", CaseStyle); 17845 verifyFormat("for (auto a : b) {\n}", CaseStyle); 17846 verifyFormat("int x = a ? b : c;", CaseStyle); 17847 verifyFormat("switch (x) {\n" 17848 "case 1 :\n" 17849 "default :\n" 17850 "}", 17851 CaseStyle); 17852 verifyFormat("switch (allBraces) {\n" 17853 "case 1 : {\n" 17854 " break;\n" 17855 "}\n" 17856 "case 2 : {\n" 17857 " [[fallthrough]];\n" 17858 "}\n" 17859 "default : {\n" 17860 " break;\n" 17861 "}\n" 17862 "}", 17863 CaseStyle); 17864 // Goto labels should not be affected. 17865 verifyFormat("switch (x) {\n" 17866 "goto_label:\n" 17867 "default :\n" 17868 "}", 17869 CaseStyle); 17870 verifyFormat("switch (x) {\n" 17871 "goto_label: { break; }\n" 17872 "default : {\n" 17873 " break;\n" 17874 "}\n" 17875 "}", 17876 CaseStyle); 17877 17878 FormatStyle NoSpaceStyle = getLLVMStyle(); 17879 EXPECT_EQ(NoSpaceStyle.SpaceBeforeCaseColon, false); 17880 NoSpaceStyle.SpaceBeforeCtorInitializerColon = false; 17881 NoSpaceStyle.SpaceBeforeInheritanceColon = false; 17882 NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false; 17883 verifyFormat("class Foo: public Bar {};", NoSpaceStyle); 17884 verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle); 17885 verifyFormat("for (auto a: b) {\n}", NoSpaceStyle); 17886 verifyFormat("int x = a ? b : c;", NoSpaceStyle); 17887 verifyFormat("{\n" 17888 "label3:\n" 17889 " int x = 0;\n" 17890 "}", 17891 NoSpaceStyle); 17892 verifyFormat("switch (x) {\n" 17893 "case 1:\n" 17894 "default:\n" 17895 "}", 17896 NoSpaceStyle); 17897 verifyFormat("switch (allBraces) {\n" 17898 "case 1: {\n" 17899 " break;\n" 17900 "}\n" 17901 "case 2: {\n" 17902 " [[fallthrough]];\n" 17903 "}\n" 17904 "default: {\n" 17905 " break;\n" 17906 "}\n" 17907 "}", 17908 NoSpaceStyle); 17909 17910 FormatStyle InvertedSpaceStyle = getLLVMStyle(); 17911 InvertedSpaceStyle.SpaceBeforeCaseColon = true; 17912 InvertedSpaceStyle.SpaceBeforeCtorInitializerColon = false; 17913 InvertedSpaceStyle.SpaceBeforeInheritanceColon = false; 17914 InvertedSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false; 17915 verifyFormat("class Foo: public Bar {};", InvertedSpaceStyle); 17916 verifyFormat("Foo::Foo(): foo(1) {}", InvertedSpaceStyle); 17917 verifyFormat("for (auto a: b) {\n}", InvertedSpaceStyle); 17918 verifyFormat("int x = a ? b : c;", InvertedSpaceStyle); 17919 verifyFormat("{\n" 17920 "label3:\n" 17921 " int x = 0;\n" 17922 "}", 17923 InvertedSpaceStyle); 17924 verifyFormat("switch (x) {\n" 17925 "case 1 :\n" 17926 "case 2 : {\n" 17927 " break;\n" 17928 "}\n" 17929 "default :\n" 17930 " break;\n" 17931 "}", 17932 InvertedSpaceStyle); 17933 verifyFormat("switch (allBraces) {\n" 17934 "case 1 : {\n" 17935 " break;\n" 17936 "}\n" 17937 "case 2 : {\n" 17938 " [[fallthrough]];\n" 17939 "}\n" 17940 "default : {\n" 17941 " break;\n" 17942 "}\n" 17943 "}", 17944 InvertedSpaceStyle); 17945 } 17946 17947 TEST_F(FormatTest, ConfigurableSpaceAroundPointerQualifiers) { 17948 FormatStyle Style = getLLVMStyle(); 17949 17950 Style.PointerAlignment = FormatStyle::PAS_Left; 17951 Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default; 17952 verifyFormat("void* const* x = NULL;", Style); 17953 17954 #define verifyQualifierSpaces(Code, Pointers, Qualifiers) \ 17955 do { \ 17956 Style.PointerAlignment = FormatStyle::Pointers; \ 17957 Style.SpaceAroundPointerQualifiers = FormatStyle::Qualifiers; \ 17958 verifyFormat(Code, Style); \ 17959 } while (false) 17960 17961 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Default); 17962 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_Default); 17963 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Default); 17964 17965 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Before); 17966 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Before); 17967 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Before); 17968 17969 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_After); 17970 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_After); 17971 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_After); 17972 17973 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_Both); 17974 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Both); 17975 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Both); 17976 17977 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Default); 17978 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, 17979 SAPQ_Default); 17980 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 17981 SAPQ_Default); 17982 17983 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Before); 17984 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, 17985 SAPQ_Before); 17986 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 17987 SAPQ_Before); 17988 17989 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_After); 17990 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_After); 17991 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 17992 SAPQ_After); 17993 17994 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_Both); 17995 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_Both); 17996 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, SAPQ_Both); 17997 17998 #undef verifyQualifierSpaces 17999 18000 FormatStyle Spaces = getLLVMStyle(); 18001 Spaces.AttributeMacros.push_back("qualified"); 18002 Spaces.PointerAlignment = FormatStyle::PAS_Right; 18003 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default; 18004 verifyFormat("SomeType *volatile *a = NULL;", Spaces); 18005 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces); 18006 verifyFormat("std::vector<SomeType *const *> x;", Spaces); 18007 verifyFormat("std::vector<SomeType *qualified *> x;", Spaces); 18008 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 18009 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before; 18010 verifyFormat("SomeType * volatile *a = NULL;", Spaces); 18011 verifyFormat("SomeType * __attribute__((attr)) *a = NULL;", Spaces); 18012 verifyFormat("std::vector<SomeType * const *> x;", Spaces); 18013 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces); 18014 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 18015 18016 // Check that SAPQ_Before doesn't result in extra spaces for PAS_Left. 18017 Spaces.PointerAlignment = FormatStyle::PAS_Left; 18018 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before; 18019 verifyFormat("SomeType* volatile* a = NULL;", Spaces); 18020 verifyFormat("SomeType* __attribute__((attr))* a = NULL;", Spaces); 18021 verifyFormat("std::vector<SomeType* const*> x;", Spaces); 18022 verifyFormat("std::vector<SomeType* qualified*> x;", Spaces); 18023 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 18024 // However, setting it to SAPQ_After should add spaces after __attribute, etc. 18025 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After; 18026 verifyFormat("SomeType* volatile * a = NULL;", Spaces); 18027 verifyFormat("SomeType* __attribute__((attr)) * a = NULL;", Spaces); 18028 verifyFormat("std::vector<SomeType* const *> x;", Spaces); 18029 verifyFormat("std::vector<SomeType* qualified *> x;", Spaces); 18030 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 18031 18032 // PAS_Middle should not have any noticeable changes even for SAPQ_Both 18033 Spaces.PointerAlignment = FormatStyle::PAS_Middle; 18034 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After; 18035 verifyFormat("SomeType * volatile * a = NULL;", Spaces); 18036 verifyFormat("SomeType * __attribute__((attr)) * a = NULL;", Spaces); 18037 verifyFormat("std::vector<SomeType * const *> x;", Spaces); 18038 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces); 18039 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 18040 } 18041 18042 TEST_F(FormatTest, AlignConsecutiveMacros) { 18043 FormatStyle Style = getLLVMStyle(); 18044 Style.AlignConsecutiveAssignments.Enabled = true; 18045 Style.AlignConsecutiveDeclarations.Enabled = true; 18046 18047 verifyFormat("#define a 3\n" 18048 "#define bbbb 4\n" 18049 "#define ccc (5)", 18050 Style); 18051 18052 verifyFormat("#define f(x) (x * x)\n" 18053 "#define fff(x, y, z) (x * y + z)\n" 18054 "#define ffff(x, y) (x - y)", 18055 Style); 18056 18057 verifyFormat("#define foo(x, y) (x + y)\n" 18058 "#define bar (5, 6)(2 + 2)", 18059 Style); 18060 18061 verifyFormat("#define a 3\n" 18062 "#define bbbb 4\n" 18063 "#define ccc (5)\n" 18064 "#define f(x) (x * x)\n" 18065 "#define fff(x, y, z) (x * y + z)\n" 18066 "#define ffff(x, y) (x - y)", 18067 Style); 18068 18069 Style.AlignConsecutiveMacros.Enabled = true; 18070 verifyFormat("#define a 3\n" 18071 "#define bbbb 4\n" 18072 "#define ccc (5)", 18073 Style); 18074 18075 verifyFormat("#define true 1\n" 18076 "#define false 0", 18077 Style); 18078 18079 verifyFormat("#define f(x) (x * x)\n" 18080 "#define fff(x, y, z) (x * y + z)\n" 18081 "#define ffff(x, y) (x - y)", 18082 Style); 18083 18084 verifyFormat("#define foo(x, y) (x + y)\n" 18085 "#define bar (5, 6)(2 + 2)", 18086 Style); 18087 18088 verifyFormat("#define a 3\n" 18089 "#define bbbb 4\n" 18090 "#define ccc (5)\n" 18091 "#define f(x) (x * x)\n" 18092 "#define fff(x, y, z) (x * y + z)\n" 18093 "#define ffff(x, y) (x - y)", 18094 Style); 18095 18096 verifyFormat("#define a 5\n" 18097 "#define foo(x, y) (x + y)\n" 18098 "#define CCC (6)\n" 18099 "auto lambda = []() {\n" 18100 " auto ii = 0;\n" 18101 " float j = 0;\n" 18102 " return 0;\n" 18103 "};\n" 18104 "int i = 0;\n" 18105 "float i2 = 0;\n" 18106 "auto v = type{\n" 18107 " i = 1, //\n" 18108 " (i = 2), //\n" 18109 " i = 3 //\n" 18110 "};", 18111 Style); 18112 18113 Style.AlignConsecutiveMacros.Enabled = false; 18114 Style.ColumnLimit = 20; 18115 18116 verifyFormat("#define a \\\n" 18117 " \"aabbbbbbbbbbbb\"\n" 18118 "#define D \\\n" 18119 " \"aabbbbbbbbbbbb\" \\\n" 18120 " \"ccddeeeeeeeee\"\n" 18121 "#define B \\\n" 18122 " \"QQQQQQQQQQQQQ\" \\\n" 18123 " \"FFFFFFFFFFFFF\" \\\n" 18124 " \"LLLLLLLL\"", 18125 Style); 18126 18127 Style.AlignConsecutiveMacros.Enabled = true; 18128 verifyFormat("#define a \\\n" 18129 " \"aabbbbbbbbbbbb\"\n" 18130 "#define D \\\n" 18131 " \"aabbbbbbbbbbbb\" \\\n" 18132 " \"ccddeeeeeeeee\"\n" 18133 "#define B \\\n" 18134 " \"QQQQQQQQQQQQQ\" \\\n" 18135 " \"FFFFFFFFFFFFF\" \\\n" 18136 " \"LLLLLLLL\"", 18137 Style); 18138 18139 // Test across comments 18140 Style.MaxEmptyLinesToKeep = 10; 18141 Style.ReflowComments = FormatStyle::RCS_Never; 18142 Style.AlignConsecutiveMacros.AcrossComments = true; 18143 verifyFormat("#define a 3\n" 18144 "// line comment\n" 18145 "#define bbbb 4\n" 18146 "#define ccc (5)", 18147 "#define a 3\n" 18148 "// line comment\n" 18149 "#define bbbb 4\n" 18150 "#define ccc (5)", 18151 Style); 18152 18153 verifyFormat("#define a 3\n" 18154 "/* block comment */\n" 18155 "#define bbbb 4\n" 18156 "#define ccc (5)", 18157 "#define a 3\n" 18158 "/* block comment */\n" 18159 "#define bbbb 4\n" 18160 "#define ccc (5)", 18161 Style); 18162 18163 verifyFormat("#define a 3\n" 18164 "/* multi-line *\n" 18165 " * block comment */\n" 18166 "#define bbbb 4\n" 18167 "#define ccc (5)", 18168 "#define a 3\n" 18169 "/* multi-line *\n" 18170 " * block comment */\n" 18171 "#define bbbb 4\n" 18172 "#define ccc (5)", 18173 Style); 18174 18175 verifyFormat("#define a 3\n" 18176 "// multi-line line comment\n" 18177 "//\n" 18178 "#define bbbb 4\n" 18179 "#define ccc (5)", 18180 "#define a 3\n" 18181 "// multi-line line comment\n" 18182 "//\n" 18183 "#define bbbb 4\n" 18184 "#define ccc (5)", 18185 Style); 18186 18187 verifyFormat("#define a 3\n" 18188 "// empty lines still break.\n" 18189 "\n" 18190 "#define bbbb 4\n" 18191 "#define ccc (5)", 18192 "#define a 3\n" 18193 "// empty lines still break.\n" 18194 "\n" 18195 "#define bbbb 4\n" 18196 "#define ccc (5)", 18197 Style); 18198 18199 // Test across empty lines 18200 Style.AlignConsecutiveMacros.AcrossComments = false; 18201 Style.AlignConsecutiveMacros.AcrossEmptyLines = true; 18202 verifyFormat("#define a 3\n" 18203 "\n" 18204 "#define bbbb 4\n" 18205 "#define ccc (5)", 18206 "#define a 3\n" 18207 "\n" 18208 "#define bbbb 4\n" 18209 "#define ccc (5)", 18210 Style); 18211 18212 verifyFormat("#define a 3\n" 18213 "\n" 18214 "\n" 18215 "\n" 18216 "#define bbbb 4\n" 18217 "#define ccc (5)", 18218 "#define a 3\n" 18219 "\n" 18220 "\n" 18221 "\n" 18222 "#define bbbb 4\n" 18223 "#define ccc (5)", 18224 Style); 18225 18226 verifyFormat("#define a 3\n" 18227 "// comments should break alignment\n" 18228 "//\n" 18229 "#define bbbb 4\n" 18230 "#define ccc (5)", 18231 "#define a 3\n" 18232 "// comments should break alignment\n" 18233 "//\n" 18234 "#define bbbb 4\n" 18235 "#define ccc (5)", 18236 Style); 18237 18238 // Test across empty lines and comments 18239 Style.AlignConsecutiveMacros.AcrossComments = true; 18240 verifyFormat("#define a 3\n" 18241 "\n" 18242 "// line comment\n" 18243 "#define bbbb 4\n" 18244 "#define ccc (5)", 18245 Style); 18246 18247 verifyFormat("#define a 3\n" 18248 "\n" 18249 "\n" 18250 "/* multi-line *\n" 18251 " * block comment */\n" 18252 "\n" 18253 "\n" 18254 "#define bbbb 4\n" 18255 "#define ccc (5)", 18256 "#define a 3\n" 18257 "\n" 18258 "\n" 18259 "/* multi-line *\n" 18260 " * block comment */\n" 18261 "\n" 18262 "\n" 18263 "#define bbbb 4\n" 18264 "#define ccc (5)", 18265 Style); 18266 18267 verifyFormat("#define a 3\n" 18268 "\n" 18269 "\n" 18270 "/* multi-line *\n" 18271 " * block comment */\n" 18272 "\n" 18273 "\n" 18274 "#define bbbb 4\n" 18275 "#define ccc (5)", 18276 "#define a 3\n" 18277 "\n" 18278 "\n" 18279 "/* multi-line *\n" 18280 " * block comment */\n" 18281 "\n" 18282 "\n" 18283 "#define bbbb 4\n" 18284 "#define ccc (5)", 18285 Style); 18286 } 18287 18288 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLines) { 18289 FormatStyle Alignment = getLLVMStyle(); 18290 Alignment.AlignConsecutiveMacros.Enabled = true; 18291 Alignment.AlignConsecutiveAssignments.Enabled = true; 18292 Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = true; 18293 18294 Alignment.MaxEmptyLinesToKeep = 10; 18295 /* Test alignment across empty lines */ 18296 verifyFormat("int a = 5;\n" 18297 "\n" 18298 "int oneTwoThree = 123;", 18299 "int a = 5;\n" 18300 "\n" 18301 "int oneTwoThree= 123;", 18302 Alignment); 18303 verifyFormat("int a = 5;\n" 18304 "int one = 1;\n" 18305 "\n" 18306 "int oneTwoThree = 123;", 18307 "int a = 5;\n" 18308 "int one = 1;\n" 18309 "\n" 18310 "int oneTwoThree = 123;", 18311 Alignment); 18312 verifyFormat("int a = 5;\n" 18313 "int one = 1;\n" 18314 "\n" 18315 "int oneTwoThree = 123;\n" 18316 "int oneTwo = 12;", 18317 "int a = 5;\n" 18318 "int one = 1;\n" 18319 "\n" 18320 "int oneTwoThree = 123;\n" 18321 "int oneTwo = 12;", 18322 Alignment); 18323 18324 /* Test across comments */ 18325 verifyFormat("int a = 5;\n" 18326 "/* block comment */\n" 18327 "int oneTwoThree = 123;", 18328 "int a = 5;\n" 18329 "/* block comment */\n" 18330 "int oneTwoThree=123;", 18331 Alignment); 18332 18333 verifyFormat("int a = 5;\n" 18334 "// line comment\n" 18335 "int oneTwoThree = 123;", 18336 "int a = 5;\n" 18337 "// line comment\n" 18338 "int oneTwoThree=123;", 18339 Alignment); 18340 18341 /* Test across comments and newlines */ 18342 verifyFormat("int a = 5;\n" 18343 "\n" 18344 "/* block comment */\n" 18345 "int oneTwoThree = 123;", 18346 "int a = 5;\n" 18347 "\n" 18348 "/* block comment */\n" 18349 "int oneTwoThree=123;", 18350 Alignment); 18351 18352 verifyFormat("int a = 5;\n" 18353 "\n" 18354 "// line comment\n" 18355 "int oneTwoThree = 123;", 18356 "int a = 5;\n" 18357 "\n" 18358 "// line comment\n" 18359 "int oneTwoThree=123;", 18360 Alignment); 18361 } 18362 18363 TEST_F(FormatTest, AlignConsecutiveDeclarationsAcrossEmptyLinesAndComments) { 18364 FormatStyle Alignment = getLLVMStyle(); 18365 Alignment.AlignConsecutiveDeclarations.Enabled = true; 18366 Alignment.AlignConsecutiveDeclarations.AcrossEmptyLines = true; 18367 Alignment.AlignConsecutiveDeclarations.AcrossComments = true; 18368 18369 Alignment.MaxEmptyLinesToKeep = 10; 18370 /* Test alignment across empty lines */ 18371 verifyFormat("int a = 5;\n" 18372 "\n" 18373 "float const oneTwoThree = 123;", 18374 "int a = 5;\n" 18375 "\n" 18376 "float const oneTwoThree = 123;", 18377 Alignment); 18378 verifyFormat("int a = 5;\n" 18379 "float const one = 1;\n" 18380 "\n" 18381 "int oneTwoThree = 123;", 18382 "int a = 5;\n" 18383 "float const one = 1;\n" 18384 "\n" 18385 "int oneTwoThree = 123;", 18386 Alignment); 18387 18388 /* Test across comments */ 18389 verifyFormat("float const a = 5;\n" 18390 "/* block comment */\n" 18391 "int oneTwoThree = 123;", 18392 "float const a = 5;\n" 18393 "/* block comment */\n" 18394 "int oneTwoThree=123;", 18395 Alignment); 18396 18397 verifyFormat("float const a = 5;\n" 18398 "// line comment\n" 18399 "int oneTwoThree = 123;", 18400 "float const a = 5;\n" 18401 "// line comment\n" 18402 "int oneTwoThree=123;", 18403 Alignment); 18404 18405 /* Test across comments and newlines */ 18406 verifyFormat("float const a = 5;\n" 18407 "\n" 18408 "/* block comment */\n" 18409 "int oneTwoThree = 123;", 18410 "float const a = 5;\n" 18411 "\n" 18412 "/* block comment */\n" 18413 "int oneTwoThree=123;", 18414 Alignment); 18415 18416 verifyFormat("float const a = 5;\n" 18417 "\n" 18418 "// line comment\n" 18419 "int oneTwoThree = 123;", 18420 "float const a = 5;\n" 18421 "\n" 18422 "// line comment\n" 18423 "int oneTwoThree=123;", 18424 Alignment); 18425 } 18426 18427 TEST_F(FormatTest, AlignConsecutiveBitFieldsAcrossEmptyLinesAndComments) { 18428 FormatStyle Alignment = getLLVMStyle(); 18429 Alignment.AlignConsecutiveBitFields.Enabled = true; 18430 Alignment.AlignConsecutiveBitFields.AcrossEmptyLines = true; 18431 Alignment.AlignConsecutiveBitFields.AcrossComments = true; 18432 18433 Alignment.MaxEmptyLinesToKeep = 10; 18434 /* Test alignment across empty lines */ 18435 verifyFormat("int a : 5;\n" 18436 "\n" 18437 "int longbitfield : 6;", 18438 "int a : 5;\n" 18439 "\n" 18440 "int longbitfield : 6;", 18441 Alignment); 18442 verifyFormat("int a : 5;\n" 18443 "int one : 1;\n" 18444 "\n" 18445 "int longbitfield : 6;", 18446 "int a : 5;\n" 18447 "int one : 1;\n" 18448 "\n" 18449 "int longbitfield : 6;", 18450 Alignment); 18451 18452 /* Test across comments */ 18453 verifyFormat("int a : 5;\n" 18454 "/* block comment */\n" 18455 "int longbitfield : 6;", 18456 "int a : 5;\n" 18457 "/* block comment */\n" 18458 "int longbitfield : 6;", 18459 Alignment); 18460 verifyFormat("int a : 5;\n" 18461 "int one : 1;\n" 18462 "// line comment\n" 18463 "int longbitfield : 6;", 18464 "int a : 5;\n" 18465 "int one : 1;\n" 18466 "// line comment\n" 18467 "int longbitfield : 6;", 18468 Alignment); 18469 18470 /* Test across comments and newlines */ 18471 verifyFormat("int a : 5;\n" 18472 "/* block comment */\n" 18473 "\n" 18474 "int longbitfield : 6;", 18475 "int a : 5;\n" 18476 "/* block comment */\n" 18477 "\n" 18478 "int longbitfield : 6;", 18479 Alignment); 18480 verifyFormat("int a : 5;\n" 18481 "int one : 1;\n" 18482 "\n" 18483 "// line comment\n" 18484 "\n" 18485 "int longbitfield : 6;", 18486 "int a : 5;\n" 18487 "int one : 1;\n" 18488 "\n" 18489 "// line comment \n" 18490 "\n" 18491 "int longbitfield : 6;", 18492 Alignment); 18493 } 18494 18495 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossComments) { 18496 FormatStyle Alignment = getLLVMStyle(); 18497 Alignment.AlignConsecutiveMacros.Enabled = true; 18498 Alignment.AlignConsecutiveAssignments.Enabled = true; 18499 Alignment.AlignConsecutiveAssignments.AcrossComments = true; 18500 18501 Alignment.MaxEmptyLinesToKeep = 10; 18502 /* Test alignment across empty lines */ 18503 verifyFormat("int a = 5;\n" 18504 "\n" 18505 "int oneTwoThree = 123;", 18506 "int a = 5;\n" 18507 "\n" 18508 "int oneTwoThree= 123;", 18509 Alignment); 18510 verifyFormat("int a = 5;\n" 18511 "int one = 1;\n" 18512 "\n" 18513 "int oneTwoThree = 123;", 18514 "int a = 5;\n" 18515 "int one = 1;\n" 18516 "\n" 18517 "int oneTwoThree = 123;", 18518 Alignment); 18519 18520 /* Test across comments */ 18521 verifyFormat("int a = 5;\n" 18522 "/* block comment */\n" 18523 "int oneTwoThree = 123;", 18524 "int a = 5;\n" 18525 "/* block comment */\n" 18526 "int oneTwoThree=123;", 18527 Alignment); 18528 18529 verifyFormat("int a = 5;\n" 18530 "// line comment\n" 18531 "int oneTwoThree = 123;", 18532 "int a = 5;\n" 18533 "// line comment\n" 18534 "int oneTwoThree=123;", 18535 Alignment); 18536 18537 verifyFormat("int a = 5;\n" 18538 "/*\n" 18539 " * multi-line block comment\n" 18540 " */\n" 18541 "int oneTwoThree = 123;", 18542 "int a = 5;\n" 18543 "/*\n" 18544 " * multi-line block comment\n" 18545 " */\n" 18546 "int oneTwoThree=123;", 18547 Alignment); 18548 18549 verifyFormat("int a = 5;\n" 18550 "//\n" 18551 "// multi-line line comment\n" 18552 "//\n" 18553 "int oneTwoThree = 123;", 18554 "int a = 5;\n" 18555 "//\n" 18556 "// multi-line line comment\n" 18557 "//\n" 18558 "int oneTwoThree=123;", 18559 Alignment); 18560 18561 /* Test across comments and newlines */ 18562 verifyFormat("int a = 5;\n" 18563 "\n" 18564 "/* block comment */\n" 18565 "int oneTwoThree = 123;", 18566 "int a = 5;\n" 18567 "\n" 18568 "/* block comment */\n" 18569 "int oneTwoThree=123;", 18570 Alignment); 18571 18572 verifyFormat("int a = 5;\n" 18573 "\n" 18574 "// line comment\n" 18575 "int oneTwoThree = 123;", 18576 "int a = 5;\n" 18577 "\n" 18578 "// line comment\n" 18579 "int oneTwoThree=123;", 18580 Alignment); 18581 } 18582 18583 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLinesAndComments) { 18584 FormatStyle Alignment = getLLVMStyle(); 18585 Alignment.AlignConsecutiveMacros.Enabled = true; 18586 Alignment.AlignConsecutiveAssignments.Enabled = true; 18587 Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = true; 18588 Alignment.AlignConsecutiveAssignments.AcrossComments = true; 18589 verifyFormat("int a = 5;\n" 18590 "int oneTwoThree = 123;", 18591 Alignment); 18592 verifyFormat("int a = method();\n" 18593 "int oneTwoThree = 133;", 18594 Alignment); 18595 verifyFormat("a &= 5;\n" 18596 "bcd *= 5;\n" 18597 "ghtyf += 5;\n" 18598 "dvfvdb -= 5;\n" 18599 "a /= 5;\n" 18600 "vdsvsv %= 5;\n" 18601 "sfdbddfbdfbb ^= 5;\n" 18602 "dvsdsv |= 5;\n" 18603 "int dsvvdvsdvvv = 123;", 18604 Alignment); 18605 verifyFormat("int i = 1, j = 10;\n" 18606 "something = 2000;", 18607 Alignment); 18608 verifyFormat("something = 2000;\n" 18609 "int i = 1, j = 10;", 18610 Alignment); 18611 verifyFormat("something = 2000;\n" 18612 "another = 911;\n" 18613 "int i = 1, j = 10;\n" 18614 "oneMore = 1;\n" 18615 "i = 2;", 18616 Alignment); 18617 verifyFormat("int a = 5;\n" 18618 "int one = 1;\n" 18619 "method();\n" 18620 "int oneTwoThree = 123;\n" 18621 "int oneTwo = 12;", 18622 Alignment); 18623 verifyFormat("int oneTwoThree = 123;\n" 18624 "int oneTwo = 12;\n" 18625 "method();", 18626 Alignment); 18627 verifyFormat("int oneTwoThree = 123; // comment\n" 18628 "int oneTwo = 12; // comment", 18629 Alignment); 18630 18631 // Bug 25167 18632 /* Uncomment when fixed 18633 verifyFormat("#if A\n" 18634 "#else\n" 18635 "int aaaaaaaa = 12;\n" 18636 "#endif\n" 18637 "#if B\n" 18638 "#else\n" 18639 "int a = 12;\n" 18640 "#endif", 18641 Alignment); 18642 verifyFormat("enum foo {\n" 18643 "#if A\n" 18644 "#else\n" 18645 " aaaaaaaa = 12;\n" 18646 "#endif\n" 18647 "#if B\n" 18648 "#else\n" 18649 " a = 12;\n" 18650 "#endif\n" 18651 "};", 18652 Alignment); 18653 */ 18654 18655 Alignment.MaxEmptyLinesToKeep = 10; 18656 /* Test alignment across empty lines */ 18657 verifyFormat("int a = 5;\n" 18658 "\n" 18659 "int oneTwoThree = 123;", 18660 "int a = 5;\n" 18661 "\n" 18662 "int oneTwoThree= 123;", 18663 Alignment); 18664 verifyFormat("int a = 5;\n" 18665 "int one = 1;\n" 18666 "\n" 18667 "int oneTwoThree = 123;", 18668 "int a = 5;\n" 18669 "int one = 1;\n" 18670 "\n" 18671 "int oneTwoThree = 123;", 18672 Alignment); 18673 verifyFormat("int a = 5;\n" 18674 "int one = 1;\n" 18675 "\n" 18676 "int oneTwoThree = 123;\n" 18677 "int oneTwo = 12;", 18678 "int a = 5;\n" 18679 "int one = 1;\n" 18680 "\n" 18681 "int oneTwoThree = 123;\n" 18682 "int oneTwo = 12;", 18683 Alignment); 18684 18685 /* Test across comments */ 18686 verifyFormat("int a = 5;\n" 18687 "/* block comment */\n" 18688 "int oneTwoThree = 123;", 18689 "int a = 5;\n" 18690 "/* block comment */\n" 18691 "int oneTwoThree=123;", 18692 Alignment); 18693 18694 verifyFormat("int a = 5;\n" 18695 "// line comment\n" 18696 "int oneTwoThree = 123;", 18697 "int a = 5;\n" 18698 "// line comment\n" 18699 "int oneTwoThree=123;", 18700 Alignment); 18701 18702 /* Test across comments and newlines */ 18703 verifyFormat("int a = 5;\n" 18704 "\n" 18705 "/* block comment */\n" 18706 "int oneTwoThree = 123;", 18707 "int a = 5;\n" 18708 "\n" 18709 "/* block comment */\n" 18710 "int oneTwoThree=123;", 18711 Alignment); 18712 18713 verifyFormat("int a = 5;\n" 18714 "\n" 18715 "// line comment\n" 18716 "int oneTwoThree = 123;", 18717 "int a = 5;\n" 18718 "\n" 18719 "// line comment\n" 18720 "int oneTwoThree=123;", 18721 Alignment); 18722 18723 verifyFormat("int a = 5;\n" 18724 "//\n" 18725 "// multi-line line comment\n" 18726 "//\n" 18727 "int oneTwoThree = 123;", 18728 "int a = 5;\n" 18729 "//\n" 18730 "// multi-line line comment\n" 18731 "//\n" 18732 "int oneTwoThree=123;", 18733 Alignment); 18734 18735 verifyFormat("int a = 5;\n" 18736 "/*\n" 18737 " * multi-line block comment\n" 18738 " */\n" 18739 "int oneTwoThree = 123;", 18740 "int a = 5;\n" 18741 "/*\n" 18742 " * multi-line block comment\n" 18743 " */\n" 18744 "int oneTwoThree=123;", 18745 Alignment); 18746 18747 verifyFormat("int a = 5;\n" 18748 "\n" 18749 "/* block comment */\n" 18750 "\n" 18751 "\n" 18752 "\n" 18753 "int oneTwoThree = 123;", 18754 "int a = 5;\n" 18755 "\n" 18756 "/* block comment */\n" 18757 "\n" 18758 "\n" 18759 "\n" 18760 "int oneTwoThree=123;", 18761 Alignment); 18762 18763 verifyFormat("int a = 5;\n" 18764 "\n" 18765 "// line comment\n" 18766 "\n" 18767 "\n" 18768 "\n" 18769 "int oneTwoThree = 123;", 18770 "int a = 5;\n" 18771 "\n" 18772 "// line comment\n" 18773 "\n" 18774 "\n" 18775 "\n" 18776 "int oneTwoThree=123;", 18777 Alignment); 18778 18779 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 18780 verifyFormat("#define A \\\n" 18781 " int aaaa = 12; \\\n" 18782 " int b = 23; \\\n" 18783 " int ccc = 234; \\\n" 18784 " int dddddddddd = 2345;", 18785 Alignment); 18786 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 18787 verifyFormat("#define A \\\n" 18788 " int aaaa = 12; \\\n" 18789 " int b = 23; \\\n" 18790 " int ccc = 234; \\\n" 18791 " int dddddddddd = 2345;", 18792 Alignment); 18793 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 18794 verifyFormat("#define A " 18795 " \\\n" 18796 " int aaaa = 12; " 18797 " \\\n" 18798 " int b = 23; " 18799 " \\\n" 18800 " int ccc = 234; " 18801 " \\\n" 18802 " int dddddddddd = 2345;", 18803 Alignment); 18804 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 18805 "k = 4, int l = 5,\n" 18806 " int m = 6) {\n" 18807 " int j = 10;\n" 18808 " otherThing = 1;\n" 18809 "}", 18810 Alignment); 18811 verifyFormat("void SomeFunction(int parameter = 0) {\n" 18812 " int i = 1;\n" 18813 " int j = 2;\n" 18814 " int big = 10000;\n" 18815 "}", 18816 Alignment); 18817 verifyFormat("class C {\n" 18818 "public:\n" 18819 " int i = 1;\n" 18820 " virtual void f() = 0;\n" 18821 "};", 18822 Alignment); 18823 verifyFormat("int i = 1;\n" 18824 "if (SomeType t = getSomething()) {\n" 18825 "}\n" 18826 "int j = 2;\n" 18827 "int big = 10000;", 18828 Alignment); 18829 verifyFormat("int j = 7;\n" 18830 "for (int k = 0; k < N; ++k) {\n" 18831 "}\n" 18832 "int j = 2;\n" 18833 "int big = 10000;\n" 18834 "}", 18835 Alignment); 18836 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 18837 verifyFormat("int i = 1;\n" 18838 "LooooooooooongType loooooooooooooooooooooongVariable\n" 18839 " = someLooooooooooooooooongFunction();\n" 18840 "int j = 2;", 18841 Alignment); 18842 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 18843 verifyFormat("int i = 1;\n" 18844 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 18845 " someLooooooooooooooooongFunction();\n" 18846 "int j = 2;", 18847 Alignment); 18848 18849 verifyFormat("auto lambda = []() {\n" 18850 " auto i = 0;\n" 18851 " return 0;\n" 18852 "};\n" 18853 "int i = 0;\n" 18854 "auto v = type{\n" 18855 " i = 1, //\n" 18856 " (i = 2), //\n" 18857 " i = 3 //\n" 18858 "};", 18859 Alignment); 18860 18861 verifyFormat( 18862 "int i = 1;\n" 18863 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 18864 " loooooooooooooooooooooongParameterB);\n" 18865 "int j = 2;", 18866 Alignment); 18867 18868 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 18869 " typename B = very_long_type_name_1,\n" 18870 " typename T_2 = very_long_type_name_2>\n" 18871 "auto foo() {}", 18872 Alignment); 18873 verifyFormat("int a, b = 1;\n" 18874 "int c = 2;\n" 18875 "int dd = 3;", 18876 Alignment); 18877 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 18878 "float b[1][] = {{3.f}};", 18879 Alignment); 18880 verifyFormat("for (int i = 0; i < 1; i++)\n" 18881 " int x = 1;", 18882 Alignment); 18883 verifyFormat("for (i = 0; i < 1; i++)\n" 18884 " x = 1;\n" 18885 "y = 1;", 18886 Alignment); 18887 18888 Alignment.ReflowComments = FormatStyle::RCS_Always; 18889 Alignment.ColumnLimit = 50; 18890 verifyFormat("int x = 0;\n" 18891 "int yy = 1; /// specificlennospace\n" 18892 "int zzz = 2;", 18893 "int x = 0;\n" 18894 "int yy = 1; ///specificlennospace\n" 18895 "int zzz = 2;", 18896 Alignment); 18897 } 18898 18899 TEST_F(FormatTest, AlignCompoundAssignments) { 18900 FormatStyle Alignment = getLLVMStyle(); 18901 Alignment.AlignConsecutiveAssignments.Enabled = true; 18902 Alignment.AlignConsecutiveAssignments.AlignCompound = true; 18903 Alignment.AlignConsecutiveAssignments.PadOperators = false; 18904 verifyFormat("sfdbddfbdfbb = 5;\n" 18905 "dvsdsv = 5;\n" 18906 "int dsvvdvsdvvv = 123;", 18907 Alignment); 18908 verifyFormat("sfdbddfbdfbb ^= 5;\n" 18909 "dvsdsv |= 5;\n" 18910 "int dsvvdvsdvvv = 123;", 18911 Alignment); 18912 verifyFormat("sfdbddfbdfbb ^= 5;\n" 18913 "dvsdsv <<= 5;\n" 18914 "int dsvvdvsdvvv = 123;", 18915 Alignment); 18916 verifyFormat("int xxx = 5;\n" 18917 "xxx = 5;\n" 18918 "{\n" 18919 " int yyy = 6;\n" 18920 " yyy = 6;\n" 18921 "}", 18922 Alignment); 18923 verifyFormat("int xxx = 5;\n" 18924 "xxx += 5;\n" 18925 "{\n" 18926 " int yyy = 6;\n" 18927 " yyy += 6;\n" 18928 "}", 18929 Alignment); 18930 // Test that `<=` is not treated as a compound assignment. 18931 verifyFormat("aa &= 5;\n" 18932 "b <= 10;\n" 18933 "c = 15;", 18934 Alignment); 18935 Alignment.AlignConsecutiveAssignments.PadOperators = true; 18936 verifyFormat("sfdbddfbdfbb = 5;\n" 18937 "dvsdsv = 5;\n" 18938 "int dsvvdvsdvvv = 123;", 18939 Alignment); 18940 verifyFormat("sfdbddfbdfbb ^= 5;\n" 18941 "dvsdsv |= 5;\n" 18942 "int dsvvdvsdvvv = 123;", 18943 Alignment); 18944 verifyFormat("sfdbddfbdfbb ^= 5;\n" 18945 "dvsdsv <<= 5;\n" 18946 "int dsvvdvsdvvv = 123;", 18947 Alignment); 18948 verifyFormat("a += 5;\n" 18949 "one = 1;\n" 18950 "\n" 18951 "oneTwoThree = 123;", 18952 "a += 5;\n" 18953 "one = 1;\n" 18954 "\n" 18955 "oneTwoThree = 123;", 18956 Alignment); 18957 verifyFormat("a += 5;\n" 18958 "one = 1;\n" 18959 "//\n" 18960 "oneTwoThree = 123;", 18961 "a += 5;\n" 18962 "one = 1;\n" 18963 "//\n" 18964 "oneTwoThree = 123;", 18965 Alignment); 18966 Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = true; 18967 verifyFormat("a += 5;\n" 18968 "one = 1;\n" 18969 "\n" 18970 "oneTwoThree = 123;", 18971 "a += 5;\n" 18972 "one = 1;\n" 18973 "\n" 18974 "oneTwoThree = 123;", 18975 Alignment); 18976 verifyFormat("a += 5;\n" 18977 "one = 1;\n" 18978 "//\n" 18979 "oneTwoThree = 123;", 18980 "a += 5;\n" 18981 "one = 1;\n" 18982 "//\n" 18983 "oneTwoThree = 123;", 18984 Alignment); 18985 Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = false; 18986 Alignment.AlignConsecutiveAssignments.AcrossComments = true; 18987 verifyFormat("a += 5;\n" 18988 "one = 1;\n" 18989 "\n" 18990 "oneTwoThree = 123;", 18991 "a += 5;\n" 18992 "one = 1;\n" 18993 "\n" 18994 "oneTwoThree = 123;", 18995 Alignment); 18996 verifyFormat("a += 5;\n" 18997 "one = 1;\n" 18998 "//\n" 18999 "oneTwoThree = 123;", 19000 "a += 5;\n" 19001 "one = 1;\n" 19002 "//\n" 19003 "oneTwoThree = 123;", 19004 Alignment); 19005 Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = true; 19006 verifyFormat("a += 5;\n" 19007 "one >>= 1;\n" 19008 "\n" 19009 "oneTwoThree = 123;", 19010 "a += 5;\n" 19011 "one >>= 1;\n" 19012 "\n" 19013 "oneTwoThree = 123;", 19014 Alignment); 19015 verifyFormat("a += 5;\n" 19016 "one = 1;\n" 19017 "//\n" 19018 "oneTwoThree <<= 123;", 19019 "a += 5;\n" 19020 "one = 1;\n" 19021 "//\n" 19022 "oneTwoThree <<= 123;", 19023 Alignment); 19024 } 19025 19026 TEST_F(FormatTest, AlignConsecutiveAssignments) { 19027 FormatStyle Alignment = getLLVMStyle(); 19028 Alignment.AlignConsecutiveMacros.Enabled = true; 19029 verifyFormat("int a = 5;\n" 19030 "int oneTwoThree = 123;", 19031 Alignment); 19032 verifyFormat("int a = 5;\n" 19033 "int oneTwoThree = 123;", 19034 Alignment); 19035 19036 Alignment.AlignConsecutiveAssignments.Enabled = true; 19037 verifyFormat("int a = 5;\n" 19038 "int oneTwoThree = 123;", 19039 Alignment); 19040 verifyFormat("int a = method();\n" 19041 "int oneTwoThree = 133;", 19042 Alignment); 19043 verifyFormat("aa <= 5;\n" 19044 "a &= 5;\n" 19045 "bcd *= 5;\n" 19046 "ghtyf += 5;\n" 19047 "dvfvdb -= 5;\n" 19048 "a /= 5;\n" 19049 "vdsvsv %= 5;\n" 19050 "sfdbddfbdfbb ^= 5;\n" 19051 "dvsdsv |= 5;\n" 19052 "int dsvvdvsdvvv = 123;", 19053 Alignment); 19054 verifyFormat("int i = 1, j = 10;\n" 19055 "something = 2000;", 19056 Alignment); 19057 verifyFormat("something = 2000;\n" 19058 "int i = 1, j = 10;", 19059 Alignment); 19060 verifyFormat("something = 2000;\n" 19061 "another = 911;\n" 19062 "int i = 1, j = 10;\n" 19063 "oneMore = 1;\n" 19064 "i = 2;", 19065 Alignment); 19066 verifyFormat("int a = 5;\n" 19067 "int one = 1;\n" 19068 "method();\n" 19069 "int oneTwoThree = 123;\n" 19070 "int oneTwo = 12;", 19071 Alignment); 19072 verifyFormat("int oneTwoThree = 123;\n" 19073 "int oneTwo = 12;\n" 19074 "method();", 19075 Alignment); 19076 verifyFormat("int oneTwoThree = 123; // comment\n" 19077 "int oneTwo = 12; // comment", 19078 Alignment); 19079 verifyFormat("int f() = default;\n" 19080 "int &operator() = default;\n" 19081 "int &operator=() {", 19082 Alignment); 19083 verifyFormat("int f() = delete;\n" 19084 "int &operator() = delete;\n" 19085 "int &operator=() {", 19086 Alignment); 19087 verifyFormat("int f() = default; // comment\n" 19088 "int &operator() = default; // comment\n" 19089 "int &operator=() {", 19090 Alignment); 19091 verifyFormat("int f() = default;\n" 19092 "int &operator() = default;\n" 19093 "int &operator==() {", 19094 Alignment); 19095 verifyFormat("int f() = default;\n" 19096 "int &operator() = default;\n" 19097 "int &operator<=() {", 19098 Alignment); 19099 verifyFormat("int f() = default;\n" 19100 "int &operator() = default;\n" 19101 "int &operator!=() {", 19102 Alignment); 19103 verifyFormat("int f() = default;\n" 19104 "int &operator() = default;\n" 19105 "int &operator=();", 19106 Alignment); 19107 verifyFormat("int f() = delete;\n" 19108 "int &operator() = delete;\n" 19109 "int &operator=();", 19110 Alignment); 19111 verifyFormat("/* long long padding */ int f() = default;\n" 19112 "int &operator() = default;\n" 19113 "int &operator/**/ =();", 19114 Alignment); 19115 // https://llvm.org/PR33697 19116 FormatStyle AlignmentWithPenalty = getLLVMStyle(); 19117 AlignmentWithPenalty.AlignConsecutiveAssignments.Enabled = true; 19118 AlignmentWithPenalty.PenaltyReturnTypeOnItsOwnLine = 5000; 19119 verifyFormat("class SSSSSSSSSSSSSSSSSSSSSSSSSSSS {\n" 19120 " void f() = delete;\n" 19121 " SSSSSSSSSSSSSSSSSSSSSSSSSSSS &operator=(\n" 19122 " const SSSSSSSSSSSSSSSSSSSSSSSSSSSS &other) = delete;\n" 19123 "};", 19124 AlignmentWithPenalty); 19125 19126 // Bug 25167 19127 /* Uncomment when fixed 19128 verifyFormat("#if A\n" 19129 "#else\n" 19130 "int aaaaaaaa = 12;\n" 19131 "#endif\n" 19132 "#if B\n" 19133 "#else\n" 19134 "int a = 12;\n" 19135 "#endif", 19136 Alignment); 19137 verifyFormat("enum foo {\n" 19138 "#if A\n" 19139 "#else\n" 19140 " aaaaaaaa = 12;\n" 19141 "#endif\n" 19142 "#if B\n" 19143 "#else\n" 19144 " a = 12;\n" 19145 "#endif\n" 19146 "};", 19147 Alignment); 19148 */ 19149 19150 verifyFormat("int a = 5;\n" 19151 "\n" 19152 "int oneTwoThree = 123;", 19153 "int a = 5;\n" 19154 "\n" 19155 "int oneTwoThree= 123;", 19156 Alignment); 19157 verifyFormat("int a = 5;\n" 19158 "int one = 1;\n" 19159 "\n" 19160 "int oneTwoThree = 123;", 19161 "int a = 5;\n" 19162 "int one = 1;\n" 19163 "\n" 19164 "int oneTwoThree = 123;", 19165 Alignment); 19166 verifyFormat("int a = 5;\n" 19167 "int one = 1;\n" 19168 "\n" 19169 "int oneTwoThree = 123;\n" 19170 "int oneTwo = 12;", 19171 "int a = 5;\n" 19172 "int one = 1;\n" 19173 "\n" 19174 "int oneTwoThree = 123;\n" 19175 "int oneTwo = 12;", 19176 Alignment); 19177 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 19178 verifyFormat("#define A \\\n" 19179 " int aaaa = 12; \\\n" 19180 " int b = 23; \\\n" 19181 " int ccc = 234; \\\n" 19182 " int dddddddddd = 2345;", 19183 Alignment); 19184 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 19185 verifyFormat("#define A \\\n" 19186 " int aaaa = 12; \\\n" 19187 " int b = 23; \\\n" 19188 " int ccc = 234; \\\n" 19189 " int dddddddddd = 2345;", 19190 Alignment); 19191 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 19192 verifyFormat("#define A " 19193 " \\\n" 19194 " int aaaa = 12; " 19195 " \\\n" 19196 " int b = 23; " 19197 " \\\n" 19198 " int ccc = 234; " 19199 " \\\n" 19200 " int dddddddddd = 2345;", 19201 Alignment); 19202 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 19203 "k = 4, int l = 5,\n" 19204 " int m = 6) {\n" 19205 " int j = 10;\n" 19206 " otherThing = 1;\n" 19207 "}", 19208 Alignment); 19209 verifyFormat("void SomeFunction(int parameter = 0) {\n" 19210 " int i = 1;\n" 19211 " int j = 2;\n" 19212 " int big = 10000;\n" 19213 "}", 19214 Alignment); 19215 verifyFormat("class C {\n" 19216 "public:\n" 19217 " int i = 1;\n" 19218 " virtual void f() = 0;\n" 19219 "};", 19220 Alignment); 19221 verifyFormat("int i = 1;\n" 19222 "if (SomeType t = getSomething()) {\n" 19223 "}\n" 19224 "int j = 2;\n" 19225 "int big = 10000;", 19226 Alignment); 19227 verifyFormat("int j = 7;\n" 19228 "for (int k = 0; k < N; ++k) {\n" 19229 "}\n" 19230 "int j = 2;\n" 19231 "int big = 10000;\n" 19232 "}", 19233 Alignment); 19234 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 19235 verifyFormat("int i = 1;\n" 19236 "LooooooooooongType loooooooooooooooooooooongVariable\n" 19237 " = someLooooooooooooooooongFunction();\n" 19238 "int j = 2;", 19239 Alignment); 19240 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 19241 verifyFormat("int i = 1;\n" 19242 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 19243 " someLooooooooooooooooongFunction();\n" 19244 "int j = 2;", 19245 Alignment); 19246 19247 verifyFormat("auto lambda = []() {\n" 19248 " auto i = 0;\n" 19249 " return 0;\n" 19250 "};\n" 19251 "int i = 0;\n" 19252 "auto v = type{\n" 19253 " i = 1, //\n" 19254 " (i = 2), //\n" 19255 " i = 3 //\n" 19256 "};", 19257 Alignment); 19258 19259 verifyFormat( 19260 "int i = 1;\n" 19261 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 19262 " loooooooooooooooooooooongParameterB);\n" 19263 "int j = 2;", 19264 Alignment); 19265 19266 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 19267 " typename B = very_long_type_name_1,\n" 19268 " typename T_2 = very_long_type_name_2>\n" 19269 "auto foo() {}", 19270 Alignment); 19271 verifyFormat("int a, b = 1;\n" 19272 "int c = 2;\n" 19273 "int dd = 3;", 19274 Alignment); 19275 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 19276 "float b[1][] = {{3.f}};", 19277 Alignment); 19278 verifyFormat("for (int i = 0; i < 1; i++)\n" 19279 " int x = 1;", 19280 Alignment); 19281 verifyFormat("for (i = 0; i < 1; i++)\n" 19282 " x = 1;\n" 19283 "y = 1;", 19284 Alignment); 19285 19286 EXPECT_EQ(Alignment.ReflowComments, FormatStyle::RCS_Always); 19287 Alignment.ColumnLimit = 50; 19288 verifyFormat("int x = 0;\n" 19289 "int yy = 1; /// specificlennospace\n" 19290 "int zzz = 2;", 19291 "int x = 0;\n" 19292 "int yy = 1; ///specificlennospace\n" 19293 "int zzz = 2;", 19294 Alignment); 19295 19296 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n" 19297 "auto b = [] {\n" 19298 " f();\n" 19299 " return;\n" 19300 "};", 19301 Alignment); 19302 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n" 19303 "auto b = g([] {\n" 19304 " f();\n" 19305 " return;\n" 19306 "});", 19307 Alignment); 19308 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n" 19309 "auto b = g(param, [] {\n" 19310 " f();\n" 19311 " return;\n" 19312 "});", 19313 Alignment); 19314 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n" 19315 "auto b = [] {\n" 19316 " if (condition) {\n" 19317 " return;\n" 19318 " }\n" 19319 "};", 19320 Alignment); 19321 19322 verifyFormat("auto b = f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 19323 " ccc ? aaaaa : bbbbb,\n" 19324 " dddddddddddddddddddddddddd);", 19325 Alignment); 19326 // FIXME: https://llvm.org/PR53497 19327 // verifyFormat("auto aaaaaaaaaaaa = f();\n" 19328 // "auto b = f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 19329 // " ccc ? aaaaa : bbbbb,\n" 19330 // " dddddddddddddddddddddddddd);", 19331 // Alignment); 19332 19333 // Confirm proper handling of AlignConsecutiveAssignments with 19334 // BinPackArguments. 19335 // See https://llvm.org/PR55360 19336 Alignment = getLLVMStyleWithColumns(50); 19337 Alignment.AlignConsecutiveAssignments.Enabled = true; 19338 Alignment.BinPackArguments = false; 19339 verifyFormat("int a_long_name = 1;\n" 19340 "auto b = B({a_long_name, a_long_name},\n" 19341 " {a_longer_name_for_wrap,\n" 19342 " a_longer_name_for_wrap});", 19343 Alignment); 19344 verifyFormat("int a_long_name = 1;\n" 19345 "auto b = B{{a_long_name, a_long_name},\n" 19346 " {a_longer_name_for_wrap,\n" 19347 " a_longer_name_for_wrap}};", 19348 Alignment); 19349 19350 Alignment = getLLVMStyleWithColumns(60); 19351 Alignment.AlignConsecutiveAssignments.Enabled = true; 19352 verifyFormat("using II = typename TI<T, std::tuple<Types...>>::I;\n" 19353 "using I = std::conditional_t<II::value >= 0,\n" 19354 " std::ic<int, II::value + 1>,\n" 19355 " std::ic<int, -1>>;", 19356 Alignment); 19357 verifyFormat("SomeName = Foo;\n" 19358 "X = func<Type, Type>(looooooooooooooooooooooooong,\n" 19359 " arrrrrrrrrrg);", 19360 Alignment); 19361 19362 Alignment.ColumnLimit = 80; 19363 Alignment.SpacesInAngles = FormatStyle::SIAS_Always; 19364 verifyFormat("void **ptr = reinterpret_cast< void ** >(unkn);\n" 19365 "ptr = reinterpret_cast< void ** >(ptr[0]);", 19366 Alignment); 19367 verifyFormat("quint32 *dstimg = reinterpret_cast< quint32 * >(out(i));\n" 19368 "quint32 *dstmask = reinterpret_cast< quint32 * >(outmask(i));", 19369 Alignment); 19370 19371 Alignment.SpacesInParens = FormatStyle::SIPO_Custom; 19372 Alignment.SpacesInParensOptions.InCStyleCasts = true; 19373 verifyFormat("void **ptr = ( void ** )unkn;\n" 19374 "ptr = ( void ** )ptr[0];", 19375 Alignment); 19376 verifyFormat("quint32 *dstimg = ( quint32 * )out.scanLine(i);\n" 19377 "quint32 *dstmask = ( quint32 * )outmask.scanLine(i);", 19378 Alignment); 19379 } 19380 19381 TEST_F(FormatTest, AlignConsecutiveBitFields) { 19382 FormatStyle Alignment = getLLVMStyle(); 19383 Alignment.AlignConsecutiveBitFields.Enabled = true; 19384 verifyFormat("int const a : 5;\n" 19385 "int oneTwoThree : 23;", 19386 Alignment); 19387 19388 // Initializers are allowed starting with c++2a 19389 verifyFormat("int const a : 5 = 1;\n" 19390 "int oneTwoThree : 23 = 0;", 19391 Alignment); 19392 19393 Alignment.AlignConsecutiveDeclarations.Enabled = true; 19394 verifyFormat("int const a : 5;\n" 19395 "int oneTwoThree : 23;", 19396 Alignment); 19397 19398 verifyFormat("int const a : 5; // comment\n" 19399 "int oneTwoThree : 23; // comment", 19400 Alignment); 19401 19402 verifyFormat("int const a : 5 = 1;\n" 19403 "int oneTwoThree : 23 = 0;", 19404 Alignment); 19405 19406 Alignment.AlignConsecutiveAssignments.Enabled = true; 19407 verifyFormat("int const a : 5 = 1;\n" 19408 "int oneTwoThree : 23 = 0;", 19409 Alignment); 19410 verifyFormat("int const a : 5 = {1};\n" 19411 "int oneTwoThree : 23 = 0;", 19412 Alignment); 19413 19414 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_None; 19415 verifyFormat("int const a :5;\n" 19416 "int oneTwoThree:23;", 19417 Alignment); 19418 19419 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_Before; 19420 verifyFormat("int const a :5;\n" 19421 "int oneTwoThree :23;", 19422 Alignment); 19423 19424 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_After; 19425 verifyFormat("int const a : 5;\n" 19426 "int oneTwoThree: 23;", 19427 Alignment); 19428 19429 // Known limitations: ':' is only recognized as a bitfield colon when 19430 // followed by a number. 19431 /* 19432 verifyFormat("int oneTwoThree : SOME_CONSTANT;\n" 19433 "int a : 5;", 19434 Alignment); 19435 */ 19436 } 19437 19438 TEST_F(FormatTest, AlignConsecutiveDeclarations) { 19439 FormatStyle Alignment = getLLVMStyle(); 19440 Alignment.AlignConsecutiveMacros.Enabled = true; 19441 Alignment.PointerAlignment = FormatStyle::PAS_Right; 19442 verifyFormat("float const a = 5;\n" 19443 "int oneTwoThree = 123;", 19444 Alignment); 19445 verifyFormat("int a = 5;\n" 19446 "float const oneTwoThree = 123;", 19447 Alignment); 19448 19449 Alignment.AlignConsecutiveDeclarations.Enabled = true; 19450 verifyFormat("float const a = 5;\n" 19451 "int oneTwoThree = 123;", 19452 Alignment); 19453 verifyFormat("int a = method();\n" 19454 "float const oneTwoThree = 133;", 19455 Alignment); 19456 verifyFormat("int i = 1, j = 10;\n" 19457 "something = 2000;", 19458 Alignment); 19459 verifyFormat("something = 2000;\n" 19460 "int i = 1, j = 10;", 19461 Alignment); 19462 verifyFormat("float something = 2000;\n" 19463 "double another = 911;\n" 19464 "int i = 1, j = 10;\n" 19465 "const int *oneMore = 1;\n" 19466 "unsigned i = 2;", 19467 Alignment); 19468 verifyFormat("float a = 5;\n" 19469 "int one = 1;\n" 19470 "method();\n" 19471 "const double oneTwoThree = 123;\n" 19472 "const unsigned int oneTwo = 12;", 19473 Alignment); 19474 verifyFormat("int oneTwoThree{0}; // comment\n" 19475 "unsigned oneTwo; // comment", 19476 Alignment); 19477 verifyFormat("unsigned int *a;\n" 19478 "int *b;\n" 19479 "unsigned int Const *c;\n" 19480 "unsigned int const *d;\n" 19481 "unsigned int Const &e;\n" 19482 "unsigned int const &f;", 19483 Alignment); 19484 verifyFormat("Const unsigned int *c;\n" 19485 "const unsigned int *d;\n" 19486 "Const unsigned int &e;\n" 19487 "const unsigned int &f;\n" 19488 "const unsigned g;\n" 19489 "Const unsigned h;", 19490 Alignment); 19491 verifyFormat("float const a = 5;\n" 19492 "\n" 19493 "int oneTwoThree = 123;", 19494 "float const a = 5;\n" 19495 "\n" 19496 "int oneTwoThree= 123;", 19497 Alignment); 19498 verifyFormat("float a = 5;\n" 19499 "int one = 1;\n" 19500 "\n" 19501 "unsigned oneTwoThree = 123;", 19502 "float a = 5;\n" 19503 "int one = 1;\n" 19504 "\n" 19505 "unsigned oneTwoThree = 123;", 19506 Alignment); 19507 verifyFormat("float a = 5;\n" 19508 "int one = 1;\n" 19509 "\n" 19510 "unsigned oneTwoThree = 123;\n" 19511 "int oneTwo = 12;", 19512 "float a = 5;\n" 19513 "int one = 1;\n" 19514 "\n" 19515 "unsigned oneTwoThree = 123;\n" 19516 "int oneTwo = 12;", 19517 Alignment); 19518 // Function prototype alignment 19519 verifyFormat("int a();\n" 19520 "double b();", 19521 Alignment); 19522 verifyFormat("int a(int x);\n" 19523 "double b();", 19524 Alignment); 19525 verifyFormat("int a(const Test & = Test());\n" 19526 "int a1(int &foo, const Test & = Test());\n" 19527 "int a2(int &foo, const Test &name = Test());\n" 19528 "double b();", 19529 Alignment); 19530 verifyFormat("struct Test {\n" 19531 " Test(const Test &) = default;\n" 19532 " ~Test() = default;\n" 19533 " Test &operator=(const Test &) = default;\n" 19534 "};", 19535 Alignment); 19536 unsigned OldColumnLimit = Alignment.ColumnLimit; 19537 // We need to set ColumnLimit to zero, in order to stress nested alignments, 19538 // otherwise the function parameters will be re-flowed onto a single line. 19539 Alignment.ColumnLimit = 0; 19540 verifyFormat("int a(int x,\n" 19541 " float y);\n" 19542 "double b(int x,\n" 19543 " double y);", 19544 "int a(int x,\n" 19545 " float y);\n" 19546 "double b(int x,\n" 19547 " double y);", 19548 Alignment); 19549 // This ensures that function parameters of function declarations are 19550 // correctly indented when their owning functions are indented. 19551 // The failure case here is for 'double y' to not be indented enough. 19552 verifyFormat("double a(int x);\n" 19553 "int b(int y,\n" 19554 " double z);", 19555 "double a(int x);\n" 19556 "int b(int y,\n" 19557 " double z);", 19558 Alignment); 19559 // Set ColumnLimit low so that we induce wrapping immediately after 19560 // the function name and opening paren. 19561 Alignment.ColumnLimit = 13; 19562 verifyFormat("int function(\n" 19563 " int x,\n" 19564 " bool y);", 19565 Alignment); 19566 // Set ColumnLimit low so that we break the argument list in multiple lines. 19567 Alignment.ColumnLimit = 35; 19568 verifyFormat("int a3(SomeTypeName1 &x,\n" 19569 " SomeTypeName2 &y,\n" 19570 " const Test & = Test());\n" 19571 "double b();", 19572 Alignment); 19573 Alignment.ColumnLimit = OldColumnLimit; 19574 // Ensure function pointers don't screw up recursive alignment 19575 verifyFormat("int a(int x, void (*fp)(int y));\n" 19576 "double b();", 19577 Alignment); 19578 Alignment.AlignConsecutiveAssignments.Enabled = true; 19579 verifyFormat("struct Test {\n" 19580 " Test(const Test &) = default;\n" 19581 " ~Test() = default;\n" 19582 " Test &operator=(const Test &) = default;\n" 19583 "};", 19584 Alignment); 19585 // Ensure recursive alignment is broken by function braces, so that the 19586 // "a = 1" does not align with subsequent assignments inside the function 19587 // body. 19588 verifyFormat("int func(int a = 1) {\n" 19589 " int b = 2;\n" 19590 " int cc = 3;\n" 19591 "}", 19592 Alignment); 19593 verifyFormat("float something = 2000;\n" 19594 "double another = 911;\n" 19595 "int i = 1, j = 10;\n" 19596 "const int *oneMore = 1;\n" 19597 "unsigned i = 2;", 19598 Alignment); 19599 verifyFormat("int oneTwoThree = {0}; // comment\n" 19600 "unsigned oneTwo = 0; // comment", 19601 Alignment); 19602 // Make sure that scope is correctly tracked, in the absence of braces 19603 verifyFormat("for (int i = 0; i < n; i++)\n" 19604 " j = i;\n" 19605 "double x = 1;", 19606 Alignment); 19607 verifyFormat("if (int i = 0)\n" 19608 " j = i;\n" 19609 "double x = 1;", 19610 Alignment); 19611 // Ensure operator[] and operator() are comprehended 19612 verifyFormat("struct test {\n" 19613 " long long int foo();\n" 19614 " int operator[](int a);\n" 19615 " double bar();\n" 19616 "};", 19617 Alignment); 19618 verifyFormat("struct test {\n" 19619 " long long int foo();\n" 19620 " int operator()(int a);\n" 19621 " double bar();\n" 19622 "};", 19623 Alignment); 19624 // http://llvm.org/PR52914 19625 verifyFormat("char *a[] = {\"a\", // comment\n" 19626 " \"bb\"};\n" 19627 "int bbbbbbb = 0;", 19628 Alignment); 19629 // http://llvm.org/PR68079 19630 verifyFormat("using Fn = int (A::*)();\n" 19631 "using RFn = int (A::*)() &;\n" 19632 "using RRFn = int (A::*)() &&;", 19633 Alignment); 19634 verifyFormat("using Fn = int (A::*)();\n" 19635 "using RFn = int *(A::*)() &;\n" 19636 "using RRFn = double (A::*)() &&;", 19637 Alignment); 19638 19639 // PAS_Right 19640 verifyFormat("void SomeFunction(int parameter = 0) {\n" 19641 " int const i = 1;\n" 19642 " int *j = 2;\n" 19643 " int big = 10000;\n" 19644 "\n" 19645 " unsigned oneTwoThree = 123;\n" 19646 " int oneTwo = 12;\n" 19647 " method();\n" 19648 " float k = 2;\n" 19649 " int ll = 10000;\n" 19650 "}", 19651 "void SomeFunction(int parameter= 0) {\n" 19652 " int const i= 1;\n" 19653 " int *j=2;\n" 19654 " int big = 10000;\n" 19655 "\n" 19656 "unsigned oneTwoThree =123;\n" 19657 "int oneTwo = 12;\n" 19658 " method();\n" 19659 "float k= 2;\n" 19660 "int ll=10000;\n" 19661 "}", 19662 Alignment); 19663 verifyFormat("void SomeFunction(int parameter = 0) {\n" 19664 " int const i = 1;\n" 19665 " int **j = 2, ***k;\n" 19666 " int &k = i;\n" 19667 " int &&l = i + j;\n" 19668 " int big = 10000;\n" 19669 "\n" 19670 " unsigned oneTwoThree = 123;\n" 19671 " int oneTwo = 12;\n" 19672 " method();\n" 19673 " float k = 2;\n" 19674 " int ll = 10000;\n" 19675 "}", 19676 "void SomeFunction(int parameter= 0) {\n" 19677 " int const i= 1;\n" 19678 " int **j=2,***k;\n" 19679 "int &k=i;\n" 19680 "int &&l=i+j;\n" 19681 " int big = 10000;\n" 19682 "\n" 19683 "unsigned oneTwoThree =123;\n" 19684 "int oneTwo = 12;\n" 19685 " method();\n" 19686 "float k= 2;\n" 19687 "int ll=10000;\n" 19688 "}", 19689 Alignment); 19690 // variables are aligned at their name, pointers are at the right most 19691 // position 19692 verifyFormat("int *a;\n" 19693 "int **b;\n" 19694 "int ***c;\n" 19695 "int foobar;", 19696 Alignment); 19697 19698 // PAS_Left 19699 FormatStyle AlignmentLeft = Alignment; 19700 AlignmentLeft.PointerAlignment = FormatStyle::PAS_Left; 19701 verifyFormat("void SomeFunction(int parameter = 0) {\n" 19702 " int const i = 1;\n" 19703 " int* j = 2;\n" 19704 " int big = 10000;\n" 19705 "\n" 19706 " unsigned oneTwoThree = 123;\n" 19707 " int oneTwo = 12;\n" 19708 " method();\n" 19709 " float k = 2;\n" 19710 " int ll = 10000;\n" 19711 "}", 19712 "void SomeFunction(int parameter= 0) {\n" 19713 " int const i= 1;\n" 19714 " int *j=2;\n" 19715 " int big = 10000;\n" 19716 "\n" 19717 "unsigned oneTwoThree =123;\n" 19718 "int oneTwo = 12;\n" 19719 " method();\n" 19720 "float k= 2;\n" 19721 "int ll=10000;\n" 19722 "}", 19723 AlignmentLeft); 19724 verifyFormat("void SomeFunction(int parameter = 0) {\n" 19725 " int const i = 1;\n" 19726 " int** j = 2;\n" 19727 " int& k = i;\n" 19728 " int&& l = i + j;\n" 19729 " int big = 10000;\n" 19730 "\n" 19731 " unsigned oneTwoThree = 123;\n" 19732 " int oneTwo = 12;\n" 19733 " method();\n" 19734 " float k = 2;\n" 19735 " int ll = 10000;\n" 19736 "}", 19737 "void SomeFunction(int parameter= 0) {\n" 19738 " int const i= 1;\n" 19739 " int **j=2;\n" 19740 "int &k=i;\n" 19741 "int &&l=i+j;\n" 19742 " int big = 10000;\n" 19743 "\n" 19744 "unsigned oneTwoThree =123;\n" 19745 "int oneTwo = 12;\n" 19746 " method();\n" 19747 "float k= 2;\n" 19748 "int ll=10000;\n" 19749 "}", 19750 AlignmentLeft); 19751 // variables are aligned at their name, pointers are at the left most position 19752 verifyFormat("int* a;\n" 19753 "int** b;\n" 19754 "int*** c;\n" 19755 "int foobar;", 19756 AlignmentLeft); 19757 19758 verifyFormat("int a(SomeType& foo, const Test& = Test());\n" 19759 "double b();", 19760 AlignmentLeft); 19761 19762 // PAS_Middle 19763 FormatStyle AlignmentMiddle = Alignment; 19764 AlignmentMiddle.PointerAlignment = FormatStyle::PAS_Middle; 19765 verifyFormat("void SomeFunction(int parameter = 0) {\n" 19766 " int const i = 1;\n" 19767 " int * j = 2;\n" 19768 " int big = 10000;\n" 19769 "\n" 19770 " unsigned oneTwoThree = 123;\n" 19771 " int oneTwo = 12;\n" 19772 " method();\n" 19773 " float k = 2;\n" 19774 " int ll = 10000;\n" 19775 "}", 19776 "void SomeFunction(int parameter= 0) {\n" 19777 " int const i= 1;\n" 19778 " int *j=2;\n" 19779 " int big = 10000;\n" 19780 "\n" 19781 "unsigned oneTwoThree =123;\n" 19782 "int oneTwo = 12;\n" 19783 " method();\n" 19784 "float k= 2;\n" 19785 "int ll=10000;\n" 19786 "}", 19787 AlignmentMiddle); 19788 verifyFormat("void SomeFunction(int parameter = 0) {\n" 19789 " int const i = 1;\n" 19790 " int ** j = 2, ***k;\n" 19791 " int & k = i;\n" 19792 " int && l = i + j;\n" 19793 " int big = 10000;\n" 19794 "\n" 19795 " unsigned oneTwoThree = 123;\n" 19796 " int oneTwo = 12;\n" 19797 " method();\n" 19798 " float k = 2;\n" 19799 " int ll = 10000;\n" 19800 "}", 19801 "void SomeFunction(int parameter= 0) {\n" 19802 " int const i= 1;\n" 19803 " int **j=2,***k;\n" 19804 "int &k=i;\n" 19805 "int &&l=i+j;\n" 19806 " int big = 10000;\n" 19807 "\n" 19808 "unsigned oneTwoThree =123;\n" 19809 "int oneTwo = 12;\n" 19810 " method();\n" 19811 "float k= 2;\n" 19812 "int ll=10000;\n" 19813 "}", 19814 AlignmentMiddle); 19815 // variables are aligned at their name, pointers are in the middle 19816 verifyFormat("int * a;\n" 19817 "int * b;\n" 19818 "int *** c;\n" 19819 "int foobar;", 19820 AlignmentMiddle); 19821 19822 verifyFormat("int a(SomeType & foo, const Test & = Test());\n" 19823 "double b();", 19824 AlignmentMiddle); 19825 19826 Alignment.AlignConsecutiveAssignments.Enabled = false; 19827 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 19828 verifyFormat("#define A \\\n" 19829 " int aaaa = 12; \\\n" 19830 " float b = 23; \\\n" 19831 " const int ccc = 234; \\\n" 19832 " unsigned dddddddddd = 2345;", 19833 Alignment); 19834 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 19835 verifyFormat("#define A \\\n" 19836 " int aaaa = 12; \\\n" 19837 " float b = 23; \\\n" 19838 " const int ccc = 234; \\\n" 19839 " unsigned dddddddddd = 2345;", 19840 Alignment); 19841 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 19842 Alignment.ColumnLimit = 30; 19843 verifyFormat("#define A \\\n" 19844 " int aaaa = 12; \\\n" 19845 " float b = 23; \\\n" 19846 " const int ccc = 234; \\\n" 19847 " int dddddddddd = 2345;", 19848 Alignment); 19849 Alignment.ColumnLimit = 80; 19850 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 19851 "k = 4, int l = 5,\n" 19852 " int m = 6) {\n" 19853 " const int j = 10;\n" 19854 " otherThing = 1;\n" 19855 "}", 19856 Alignment); 19857 verifyFormat("void SomeFunction(int parameter = 0) {\n" 19858 " int const i = 1;\n" 19859 " int *j = 2;\n" 19860 " int big = 10000;\n" 19861 "}", 19862 Alignment); 19863 verifyFormat("class C {\n" 19864 "public:\n" 19865 " int i = 1;\n" 19866 " virtual void f() = 0;\n" 19867 "};", 19868 Alignment); 19869 verifyFormat("float i = 1;\n" 19870 "if (SomeType t = getSomething()) {\n" 19871 "}\n" 19872 "const unsigned j = 2;\n" 19873 "int big = 10000;", 19874 Alignment); 19875 verifyFormat("float j = 7;\n" 19876 "for (int k = 0; k < N; ++k) {\n" 19877 "}\n" 19878 "unsigned j = 2;\n" 19879 "int big = 10000;\n" 19880 "}", 19881 Alignment); 19882 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 19883 verifyFormat("float i = 1;\n" 19884 "LooooooooooongType loooooooooooooooooooooongVariable\n" 19885 " = someLooooooooooooooooongFunction();\n" 19886 "int j = 2;", 19887 Alignment); 19888 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 19889 verifyFormat("int i = 1;\n" 19890 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 19891 " someLooooooooooooooooongFunction();\n" 19892 "int j = 2;", 19893 Alignment); 19894 19895 Alignment.AlignConsecutiveAssignments.Enabled = true; 19896 verifyFormat("auto lambda = []() {\n" 19897 " auto ii = 0;\n" 19898 " float j = 0;\n" 19899 " return 0;\n" 19900 "};\n" 19901 "int i = 0;\n" 19902 "float i2 = 0;\n" 19903 "auto v = type{\n" 19904 " i = 1, //\n" 19905 " (i = 2), //\n" 19906 " i = 3 //\n" 19907 "};", 19908 Alignment); 19909 Alignment.AlignConsecutiveAssignments.Enabled = false; 19910 19911 verifyFormat( 19912 "int i = 1;\n" 19913 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 19914 " loooooooooooooooooooooongParameterB);\n" 19915 "int j = 2;", 19916 Alignment); 19917 19918 // Test interactions with ColumnLimit and AlignConsecutiveAssignments: 19919 // We expect declarations and assignments to align, as long as it doesn't 19920 // exceed the column limit, starting a new alignment sequence whenever it 19921 // happens. 19922 Alignment.AlignConsecutiveAssignments.Enabled = true; 19923 Alignment.ColumnLimit = 30; 19924 verifyFormat("float ii = 1;\n" 19925 "unsigned j = 2;\n" 19926 "int someVerylongVariable = 1;\n" 19927 "AnotherLongType ll = 123456;\n" 19928 "VeryVeryLongType k = 2;\n" 19929 "int myvar = 1;", 19930 Alignment); 19931 Alignment.ColumnLimit = 80; 19932 Alignment.AlignConsecutiveAssignments.Enabled = false; 19933 19934 verifyFormat( 19935 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n" 19936 " typename LongType, typename B>\n" 19937 "auto foo() {}", 19938 Alignment); 19939 verifyFormat("float a, b = 1;\n" 19940 "int c = 2;\n" 19941 "int dd = 3;", 19942 Alignment); 19943 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 19944 "float b[1][] = {{3.f}};", 19945 Alignment); 19946 Alignment.AlignConsecutiveAssignments.Enabled = true; 19947 verifyFormat("float a, b = 1;\n" 19948 "int c = 2;\n" 19949 "int dd = 3;", 19950 Alignment); 19951 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 19952 "float b[1][] = {{3.f}};", 19953 Alignment); 19954 Alignment.AlignConsecutiveAssignments.Enabled = false; 19955 19956 Alignment.ColumnLimit = 30; 19957 Alignment.BinPackParameters = FormatStyle::BPPS_OnePerLine; 19958 verifyFormat("void foo(float a,\n" 19959 " float b,\n" 19960 " int c,\n" 19961 " uint32_t *d) {\n" 19962 " int *e = 0;\n" 19963 " float f = 0;\n" 19964 " double g = 0;\n" 19965 "}\n" 19966 "void bar(ino_t a,\n" 19967 " int b,\n" 19968 " uint32_t *c,\n" 19969 " bool d) {}", 19970 Alignment); 19971 Alignment.BinPackParameters = FormatStyle::BPPS_BinPack; 19972 Alignment.ColumnLimit = 80; 19973 19974 // Bug 33507 19975 Alignment.PointerAlignment = FormatStyle::PAS_Middle; 19976 verifyFormat( 19977 "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n" 19978 " static const Version verVs2017;\n" 19979 " return true;\n" 19980 "});", 19981 Alignment); 19982 Alignment.PointerAlignment = FormatStyle::PAS_Right; 19983 19984 // See llvm.org/PR35641 19985 Alignment.AlignConsecutiveDeclarations.Enabled = true; 19986 verifyFormat("int func() { //\n" 19987 " int b;\n" 19988 " unsigned c;\n" 19989 "}", 19990 Alignment); 19991 19992 // See PR37175 19993 FormatStyle Style = getMozillaStyle(); 19994 Style.AlignConsecutiveDeclarations.Enabled = true; 19995 verifyFormat("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n" 19996 "foo(int a);", 19997 "DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style); 19998 19999 Alignment.PointerAlignment = FormatStyle::PAS_Left; 20000 verifyFormat("unsigned int* a;\n" 20001 "int* b;\n" 20002 "unsigned int Const* c;\n" 20003 "unsigned int const* d;\n" 20004 "unsigned int Const& e;\n" 20005 "unsigned int const& f;", 20006 Alignment); 20007 verifyFormat("Const unsigned int* c;\n" 20008 "const unsigned int* d;\n" 20009 "Const unsigned int& e;\n" 20010 "const unsigned int& f;\n" 20011 "const unsigned g;\n" 20012 "Const unsigned h;", 20013 Alignment); 20014 20015 Alignment.PointerAlignment = FormatStyle::PAS_Middle; 20016 verifyFormat("unsigned int * a;\n" 20017 "int * b;\n" 20018 "unsigned int Const * c;\n" 20019 "unsigned int const * d;\n" 20020 "unsigned int Const & e;\n" 20021 "unsigned int const & f;", 20022 Alignment); 20023 verifyFormat("Const unsigned int * c;\n" 20024 "const unsigned int * d;\n" 20025 "Const unsigned int & e;\n" 20026 "const unsigned int & f;\n" 20027 "const unsigned g;\n" 20028 "Const unsigned h;", 20029 Alignment); 20030 20031 // See PR46529 20032 FormatStyle BracedAlign = getLLVMStyle(); 20033 BracedAlign.AlignConsecutiveDeclarations.Enabled = true; 20034 verifyFormat("const auto result{[]() {\n" 20035 " const auto something = 1;\n" 20036 " return 2;\n" 20037 "}};", 20038 BracedAlign); 20039 verifyFormat("int foo{[]() {\n" 20040 " int bar{0};\n" 20041 " return 0;\n" 20042 "}()};", 20043 BracedAlign); 20044 BracedAlign.Cpp11BracedListStyle = false; 20045 verifyFormat("const auto result{ []() {\n" 20046 " const auto something = 1;\n" 20047 " return 2;\n" 20048 "} };", 20049 BracedAlign); 20050 verifyFormat("int foo{ []() {\n" 20051 " int bar{ 0 };\n" 20052 " return 0;\n" 20053 "}() };", 20054 BracedAlign); 20055 20056 Alignment.AlignConsecutiveDeclarations.AlignFunctionDeclarations = false; 20057 verifyFormat("unsigned int f1(void);\n" 20058 "void f2(void);\n" 20059 "size_t f3(void);", 20060 Alignment); 20061 } 20062 20063 TEST_F(FormatTest, AlignConsecutiveShortCaseStatements) { 20064 FormatStyle Alignment = getLLVMStyle(); 20065 Alignment.AllowShortCaseLabelsOnASingleLine = true; 20066 Alignment.AlignConsecutiveShortCaseStatements.Enabled = true; 20067 20068 verifyFormat("switch (level) {\n" 20069 "case log::info: return \"info\";\n" 20070 "case log::warning: return \"warning\";\n" 20071 "default: return \"default\";\n" 20072 "}", 20073 Alignment); 20074 20075 verifyFormat("switch (level) {\n" 20076 "case log::info: return \"info\";\n" 20077 "case log::warning: return \"warning\";\n" 20078 "}", 20079 "switch (level) {\n" 20080 "case log::info: return \"info\";\n" 20081 "case log::warning:\n" 20082 " return \"warning\";\n" 20083 "}", 20084 Alignment); 20085 20086 // Empty case statements push out the alignment, but non-short case labels 20087 // don't. 20088 verifyFormat("switch (level) {\n" 20089 "case log::info: return \"info\";\n" 20090 "case log::critical:\n" 20091 "case log::warning:\n" 20092 "case log::severe: return \"severe\";\n" 20093 "case log::extra_severe:\n" 20094 " // comment\n" 20095 " return \"extra_severe\";\n" 20096 "}", 20097 Alignment); 20098 20099 // Verify comments and empty lines break the alignment. 20100 verifyNoChange("switch (level) {\n" 20101 "case log::info: return \"info\";\n" 20102 "case log::warning: return \"warning\";\n" 20103 "// comment\n" 20104 "case log::critical: return \"critical\";\n" 20105 "default: return \"default\";\n" 20106 "\n" 20107 "case log::severe: return \"severe\";\n" 20108 "}", 20109 Alignment); 20110 20111 // Empty case statements don't break the alignment, and potentially push it 20112 // out. 20113 verifyFormat("switch (level) {\n" 20114 "case log::info: return \"info\";\n" 20115 "case log::warning:\n" 20116 "case log::critical:\n" 20117 "default: return \"default\";\n" 20118 "}", 20119 Alignment); 20120 20121 // Implicit fallthrough cases can be aligned with either a comment or 20122 // [[fallthrough]] 20123 verifyFormat("switch (level) {\n" 20124 "case log::info: return \"info\";\n" 20125 "case log::warning: // fallthrough\n" 20126 "case log::error: return \"error\";\n" 20127 "case log::critical: /*fallthrough*/\n" 20128 "case log::severe: return \"severe\";\n" 20129 "case log::diag: [[fallthrough]];\n" 20130 "default: return \"default\";\n" 20131 "}", 20132 Alignment); 20133 20134 // Verify trailing comment that needs a reflow also gets aligned properly. 20135 verifyFormat("switch (level) {\n" 20136 "case log::info: return \"info\";\n" 20137 "case log::warning: // fallthrough\n" 20138 "case log::error: return \"error\";\n" 20139 "}", 20140 "switch (level) {\n" 20141 "case log::info: return \"info\";\n" 20142 "case log::warning: //fallthrough\n" 20143 "case log::error: return \"error\";\n" 20144 "}", 20145 Alignment); 20146 20147 // Verify adjacent non-short case statements don't change the alignment, and 20148 // properly break the set of consecutive statements. 20149 verifyFormat("switch (level) {\n" 20150 "case log::critical:\n" 20151 " // comment\n" 20152 " return \"critical\";\n" 20153 "case log::info: return \"info\";\n" 20154 "case log::warning: return \"warning\";\n" 20155 "default:\n" 20156 " // comment\n" 20157 " return \"\";\n" 20158 "case log::error: return \"error\";\n" 20159 "case log::severe: return \"severe\";\n" 20160 "case log::extra_critical:\n" 20161 " // comment\n" 20162 " return \"extra critical\";\n" 20163 "}", 20164 Alignment); 20165 20166 Alignment.SpaceBeforeCaseColon = true; 20167 verifyFormat("switch (level) {\n" 20168 "case log::info : return \"info\";\n" 20169 "case log::warning : return \"warning\";\n" 20170 "default : return \"default\";\n" 20171 "}", 20172 Alignment); 20173 Alignment.SpaceBeforeCaseColon = false; 20174 20175 // Make sure we don't incorrectly align correctly across nested switch cases. 20176 verifyFormat("switch (level) {\n" 20177 "case log::info: return \"info\";\n" 20178 "case log::warning: return \"warning\";\n" 20179 "case log::other:\n" 20180 " switch (sublevel) {\n" 20181 " case log::info: return \"info\";\n" 20182 " case log::warning: return \"warning\";\n" 20183 " }\n" 20184 " break;\n" 20185 "case log::error: return \"error\";\n" 20186 "default: return \"default\";\n" 20187 "}", 20188 "switch (level) {\n" 20189 "case log::info: return \"info\";\n" 20190 "case log::warning: return \"warning\";\n" 20191 "case log::other: switch (sublevel) {\n" 20192 " case log::info: return \"info\";\n" 20193 " case log::warning: return \"warning\";\n" 20194 "}\n" 20195 "break;\n" 20196 "case log::error: return \"error\";\n" 20197 "default: return \"default\";\n" 20198 "}", 20199 Alignment); 20200 20201 Alignment.AlignConsecutiveShortCaseStatements.AcrossEmptyLines = true; 20202 20203 verifyFormat("switch (level) {\n" 20204 "case log::info: return \"info\";\n" 20205 "\n" 20206 "case log::warning: return \"warning\";\n" 20207 "}", 20208 "switch (level) {\n" 20209 "case log::info: return \"info\";\n" 20210 "\n" 20211 "case log::warning: return \"warning\";\n" 20212 "}", 20213 Alignment); 20214 20215 Alignment.AlignConsecutiveShortCaseStatements.AcrossComments = true; 20216 20217 verifyNoChange("switch (level) {\n" 20218 "case log::info: return \"info\";\n" 20219 "\n" 20220 "/* block comment */\n" 20221 "\n" 20222 "// line comment\n" 20223 "case log::warning: return \"warning\";\n" 20224 "}", 20225 Alignment); 20226 20227 Alignment.AlignConsecutiveShortCaseStatements.AcrossEmptyLines = false; 20228 20229 verifyFormat("switch (level) {\n" 20230 "case log::info: return \"info\";\n" 20231 "//\n" 20232 "case log::warning: return \"warning\";\n" 20233 "}", 20234 Alignment); 20235 20236 Alignment.AlignConsecutiveShortCaseStatements.AlignCaseColons = true; 20237 20238 verifyFormat("switch (level) {\n" 20239 "case log::info : return \"info\";\n" 20240 "case log::warning: return \"warning\";\n" 20241 "default : return \"default\";\n" 20242 "}", 20243 Alignment); 20244 20245 // With AlignCaseColons, empty case statements don't break alignment of 20246 // consecutive case statements (and are aligned). 20247 verifyFormat("switch (level) {\n" 20248 "case log::info : return \"info\";\n" 20249 "case log::warning :\n" 20250 "case log::critical:\n" 20251 "default : return \"default\";\n" 20252 "}", 20253 Alignment); 20254 20255 // Final non-short case labels shouldn't have their colon aligned 20256 verifyFormat("switch (level) {\n" 20257 "case log::info : return \"info\";\n" 20258 "case log::warning :\n" 20259 "case log::critical:\n" 20260 "case log::severe : return \"severe\";\n" 20261 "default:\n" 20262 " // comment\n" 20263 " return \"default\";\n" 20264 "}", 20265 Alignment); 20266 20267 // Verify adjacent non-short case statements break the set of consecutive 20268 // alignments and aren't aligned with adjacent non-short case statements if 20269 // AlignCaseColons is set. 20270 verifyFormat("switch (level) {\n" 20271 "case log::critical:\n" 20272 " // comment\n" 20273 " return \"critical\";\n" 20274 "case log::info : return \"info\";\n" 20275 "case log::warning: return \"warning\";\n" 20276 "default:\n" 20277 " // comment\n" 20278 " return \"\";\n" 20279 "case log::error : return \"error\";\n" 20280 "case log::severe: return \"severe\";\n" 20281 "case log::extra_critical:\n" 20282 " // comment\n" 20283 " return \"extra critical\";\n" 20284 "}", 20285 Alignment); 20286 20287 Alignment.SpaceBeforeCaseColon = true; 20288 verifyFormat("switch (level) {\n" 20289 "case log::info : return \"info\";\n" 20290 "case log::warning : return \"warning\";\n" 20291 "case log::error :\n" 20292 "default : return \"default\";\n" 20293 "}", 20294 Alignment); 20295 } 20296 20297 TEST_F(FormatTest, AlignWithLineBreaks) { 20298 auto Style = getLLVMStyleWithColumns(120); 20299 20300 EXPECT_EQ(Style.AlignConsecutiveAssignments, 20301 FormatStyle::AlignConsecutiveStyle( 20302 {/*Enabled=*/false, /*AcrossEmptyLines=*/false, 20303 /*AcrossComments=*/false, /*AlignCompound=*/false, 20304 /*AlignFunctionDeclarations=*/false, 20305 /*AlignFunctionPointers=*/false, 20306 /*PadOperators=*/true})); 20307 EXPECT_EQ(Style.AlignConsecutiveDeclarations, 20308 FormatStyle::AlignConsecutiveStyle( 20309 {/*Enabled=*/false, /*AcrossEmptyLines=*/false, 20310 /*AcrossComments=*/false, /*AlignCompound=*/false, 20311 /*AlignFunctionDeclarations=*/true, 20312 /*AlignFunctionPointers=*/false, 20313 /*PadOperators=*/false})); 20314 verifyFormat("void foo() {\n" 20315 " int myVar = 5;\n" 20316 " double x = 3.14;\n" 20317 " auto str = \"Hello \"\n" 20318 " \"World\";\n" 20319 " auto s = \"Hello \"\n" 20320 " \"Again\";\n" 20321 "}", 20322 Style); 20323 20324 // clang-format off 20325 verifyFormat("void foo() {\n" 20326 " const int capacityBefore = Entries.capacity();\n" 20327 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 20328 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 20329 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 20330 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 20331 "}", 20332 Style); 20333 // clang-format on 20334 20335 Style.AlignConsecutiveAssignments.Enabled = true; 20336 verifyFormat("void foo() {\n" 20337 " int myVar = 5;\n" 20338 " double x = 3.14;\n" 20339 " auto str = \"Hello \"\n" 20340 " \"World\";\n" 20341 " auto s = \"Hello \"\n" 20342 " \"Again\";\n" 20343 "}", 20344 Style); 20345 20346 // clang-format off 20347 verifyFormat("void foo() {\n" 20348 " const int capacityBefore = Entries.capacity();\n" 20349 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 20350 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 20351 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 20352 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 20353 "}", 20354 Style); 20355 // clang-format on 20356 20357 Style.AlignConsecutiveAssignments.Enabled = false; 20358 Style.AlignConsecutiveDeclarations.Enabled = true; 20359 verifyFormat("void foo() {\n" 20360 " int myVar = 5;\n" 20361 " double x = 3.14;\n" 20362 " auto str = \"Hello \"\n" 20363 " \"World\";\n" 20364 " auto s = \"Hello \"\n" 20365 " \"Again\";\n" 20366 "}", 20367 Style); 20368 20369 // clang-format off 20370 verifyFormat("void foo() {\n" 20371 " const int capacityBefore = Entries.capacity();\n" 20372 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 20373 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 20374 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 20375 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 20376 "}", 20377 Style); 20378 // clang-format on 20379 20380 Style.AlignConsecutiveAssignments.Enabled = true; 20381 Style.AlignConsecutiveDeclarations.Enabled = true; 20382 20383 verifyFormat("void foo() {\n" 20384 " int myVar = 5;\n" 20385 " double x = 3.14;\n" 20386 " auto str = \"Hello \"\n" 20387 " \"World\";\n" 20388 " auto s = \"Hello \"\n" 20389 " \"Again\";\n" 20390 "}", 20391 Style); 20392 20393 // clang-format off 20394 verifyFormat("void foo() {\n" 20395 " const int capacityBefore = Entries.capacity();\n" 20396 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 20397 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 20398 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 20399 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 20400 "}", 20401 Style); 20402 // clang-format on 20403 20404 Style = getLLVMStyleWithColumns(20); 20405 Style.AlignConsecutiveAssignments.Enabled = true; 20406 Style.IndentWidth = 4; 20407 20408 verifyFormat("void foo() {\n" 20409 " int i1 = 1;\n" 20410 " int j = 0;\n" 20411 " int k = bar(\n" 20412 " argument1,\n" 20413 " argument2);\n" 20414 "}", 20415 Style); 20416 20417 verifyFormat("unsigned i = 0;\n" 20418 "int a[] = {\n" 20419 " 1234567890,\n" 20420 " -1234567890};", 20421 Style); 20422 20423 Style.ColumnLimit = 120; 20424 20425 // clang-format off 20426 verifyFormat("void SomeFunc() {\n" 20427 " newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n" 20428 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 20429 " newWatcher.maxAge = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n" 20430 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 20431 " newWatcher.max = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n" 20432 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 20433 "}", 20434 Style); 20435 // clang-format on 20436 20437 Style.BinPackArguments = false; 20438 20439 // clang-format off 20440 verifyFormat("void SomeFunc() {\n" 20441 " newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(\n" 20442 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 20443 " newWatcher.maxAge = ToLegacyTimestamp(GetMaxAge(\n" 20444 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 20445 " newWatcher.max = ToLegacyTimestamp(GetMaxAge(\n" 20446 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 20447 "}", 20448 Style); 20449 // clang-format on 20450 } 20451 20452 TEST_F(FormatTest, AlignWithInitializerPeriods) { 20453 auto Style = getLLVMStyleWithColumns(60); 20454 20455 verifyFormat("void foo1(void) {\n" 20456 " BYTE p[1] = 1;\n" 20457 " A B = {.one_foooooooooooooooo = 2,\n" 20458 " .two_fooooooooooooo = 3,\n" 20459 " .three_fooooooooooooo = 4};\n" 20460 " BYTE payload = 2;\n" 20461 "}", 20462 Style); 20463 20464 Style.AlignConsecutiveAssignments.Enabled = true; 20465 Style.AlignConsecutiveDeclarations.Enabled = false; 20466 verifyFormat("void foo2(void) {\n" 20467 " BYTE p[1] = 1;\n" 20468 " A B = {.one_foooooooooooooooo = 2,\n" 20469 " .two_fooooooooooooo = 3,\n" 20470 " .three_fooooooooooooo = 4};\n" 20471 " BYTE payload = 2;\n" 20472 "}", 20473 Style); 20474 20475 Style.AlignConsecutiveAssignments.Enabled = false; 20476 Style.AlignConsecutiveDeclarations.Enabled = true; 20477 verifyFormat("void foo3(void) {\n" 20478 " BYTE p[1] = 1;\n" 20479 " A B = {.one_foooooooooooooooo = 2,\n" 20480 " .two_fooooooooooooo = 3,\n" 20481 " .three_fooooooooooooo = 4};\n" 20482 " BYTE payload = 2;\n" 20483 "}", 20484 Style); 20485 20486 Style.AlignConsecutiveAssignments.Enabled = true; 20487 Style.AlignConsecutiveDeclarations.Enabled = true; 20488 verifyFormat("void foo4(void) {\n" 20489 " BYTE p[1] = 1;\n" 20490 " A B = {.one_foooooooooooooooo = 2,\n" 20491 " .two_fooooooooooooo = 3,\n" 20492 " .three_fooooooooooooo = 4};\n" 20493 " BYTE payload = 2;\n" 20494 "}", 20495 Style); 20496 } 20497 20498 TEST_F(FormatTest, LinuxBraceBreaking) { 20499 FormatStyle LinuxBraceStyle = getLLVMStyle(); 20500 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux; 20501 verifyFormat("namespace a\n" 20502 "{\n" 20503 "class A\n" 20504 "{\n" 20505 " void f()\n" 20506 " {\n" 20507 " if (true) {\n" 20508 " a();\n" 20509 " b();\n" 20510 " } else {\n" 20511 " a();\n" 20512 " }\n" 20513 " }\n" 20514 " void g() { return; }\n" 20515 "};\n" 20516 "struct B {\n" 20517 " int x;\n" 20518 "};\n" 20519 "} // namespace a", 20520 LinuxBraceStyle); 20521 verifyFormat("enum X {\n" 20522 " Y = 0,\n" 20523 "}", 20524 LinuxBraceStyle); 20525 verifyFormat("struct S {\n" 20526 " int Type;\n" 20527 " union {\n" 20528 " int x;\n" 20529 " double y;\n" 20530 " } Value;\n" 20531 " class C\n" 20532 " {\n" 20533 " MyFavoriteType Value;\n" 20534 " } Class;\n" 20535 "}", 20536 LinuxBraceStyle); 20537 } 20538 20539 TEST_F(FormatTest, MozillaBraceBreaking) { 20540 FormatStyle MozillaBraceStyle = getLLVMStyle(); 20541 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; 20542 MozillaBraceStyle.FixNamespaceComments = false; 20543 verifyFormat("namespace a {\n" 20544 "class A\n" 20545 "{\n" 20546 " void f()\n" 20547 " {\n" 20548 " if (true) {\n" 20549 " a();\n" 20550 " b();\n" 20551 " }\n" 20552 " }\n" 20553 " void g() { return; }\n" 20554 "};\n" 20555 "enum E\n" 20556 "{\n" 20557 " A,\n" 20558 " // foo\n" 20559 " B,\n" 20560 " C\n" 20561 "};\n" 20562 "struct B\n" 20563 "{\n" 20564 " int x;\n" 20565 "};\n" 20566 "}", 20567 MozillaBraceStyle); 20568 verifyFormat("struct S\n" 20569 "{\n" 20570 " int Type;\n" 20571 " union\n" 20572 " {\n" 20573 " int x;\n" 20574 " double y;\n" 20575 " } Value;\n" 20576 " class C\n" 20577 " {\n" 20578 " MyFavoriteType Value;\n" 20579 " } Class;\n" 20580 "}", 20581 MozillaBraceStyle); 20582 } 20583 20584 TEST_F(FormatTest, StroustrupBraceBreaking) { 20585 FormatStyle StroustrupBraceStyle = getLLVMStyle(); 20586 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 20587 verifyFormat("namespace a {\n" 20588 "class A {\n" 20589 " void f()\n" 20590 " {\n" 20591 " if (true) {\n" 20592 " a();\n" 20593 " b();\n" 20594 " }\n" 20595 " }\n" 20596 " void g() { return; }\n" 20597 "};\n" 20598 "struct B {\n" 20599 " int x;\n" 20600 "};\n" 20601 "} // namespace a", 20602 StroustrupBraceStyle); 20603 20604 verifyFormat("void foo()\n" 20605 "{\n" 20606 " if (a) {\n" 20607 " a();\n" 20608 " }\n" 20609 " else {\n" 20610 " b();\n" 20611 " }\n" 20612 "}", 20613 StroustrupBraceStyle); 20614 20615 verifyFormat("#ifdef _DEBUG\n" 20616 "int foo(int i = 0)\n" 20617 "#else\n" 20618 "int foo(int i = 5)\n" 20619 "#endif\n" 20620 "{\n" 20621 " return i;\n" 20622 "}", 20623 StroustrupBraceStyle); 20624 20625 verifyFormat("void foo() {}\n" 20626 "void bar()\n" 20627 "#ifdef _DEBUG\n" 20628 "{\n" 20629 " foo();\n" 20630 "}\n" 20631 "#else\n" 20632 "{\n" 20633 "}\n" 20634 "#endif", 20635 StroustrupBraceStyle); 20636 20637 verifyFormat("void foobar() { int i = 5; }\n" 20638 "#ifdef _DEBUG\n" 20639 "void bar() {}\n" 20640 "#else\n" 20641 "void bar() { foobar(); }\n" 20642 "#endif", 20643 StroustrupBraceStyle); 20644 } 20645 20646 TEST_F(FormatTest, AllmanBraceBreaking) { 20647 FormatStyle AllmanBraceStyle = getLLVMStyle(); 20648 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman; 20649 20650 verifyFormat("namespace a\n" 20651 "{\n" 20652 "void f();\n" 20653 "void g();\n" 20654 "} // namespace a", 20655 "namespace a\n" 20656 "{\n" 20657 "void f();\n" 20658 "void g();\n" 20659 "}", 20660 AllmanBraceStyle); 20661 20662 verifyFormat("namespace a\n" 20663 "{\n" 20664 "class A\n" 20665 "{\n" 20666 " void f()\n" 20667 " {\n" 20668 " if (true)\n" 20669 " {\n" 20670 " a();\n" 20671 " b();\n" 20672 " }\n" 20673 " }\n" 20674 " void g() { return; }\n" 20675 "};\n" 20676 "struct B\n" 20677 "{\n" 20678 " int x;\n" 20679 "};\n" 20680 "union C\n" 20681 "{\n" 20682 "};\n" 20683 "} // namespace a", 20684 AllmanBraceStyle); 20685 20686 verifyFormat("void f()\n" 20687 "{\n" 20688 " if (true)\n" 20689 " {\n" 20690 " a();\n" 20691 " }\n" 20692 " else if (false)\n" 20693 " {\n" 20694 " b();\n" 20695 " }\n" 20696 " else\n" 20697 " {\n" 20698 " c();\n" 20699 " }\n" 20700 "}", 20701 AllmanBraceStyle); 20702 20703 verifyFormat("void f()\n" 20704 "{\n" 20705 " for (int i = 0; i < 10; ++i)\n" 20706 " {\n" 20707 " a();\n" 20708 " }\n" 20709 " while (false)\n" 20710 " {\n" 20711 " b();\n" 20712 " }\n" 20713 " do\n" 20714 " {\n" 20715 " c();\n" 20716 " } while (false)\n" 20717 "}", 20718 AllmanBraceStyle); 20719 20720 verifyFormat("void f(int a)\n" 20721 "{\n" 20722 " switch (a)\n" 20723 " {\n" 20724 " case 0:\n" 20725 " break;\n" 20726 " case 1:\n" 20727 " {\n" 20728 " break;\n" 20729 " }\n" 20730 " case 2:\n" 20731 " {\n" 20732 " }\n" 20733 " break;\n" 20734 " default:\n" 20735 " break;\n" 20736 " }\n" 20737 "}", 20738 AllmanBraceStyle); 20739 20740 verifyFormat("enum X\n" 20741 "{\n" 20742 " Y = 0,\n" 20743 "}", 20744 AllmanBraceStyle); 20745 verifyFormat("enum X\n" 20746 "{\n" 20747 " Y = 0\n" 20748 "}", 20749 AllmanBraceStyle); 20750 20751 verifyFormat("@interface BSApplicationController ()\n" 20752 "{\n" 20753 "@private\n" 20754 " id _extraIvar;\n" 20755 "}\n" 20756 "@end", 20757 AllmanBraceStyle); 20758 20759 verifyFormat("#ifdef _DEBUG\n" 20760 "int foo(int i = 0)\n" 20761 "#else\n" 20762 "int foo(int i = 5)\n" 20763 "#endif\n" 20764 "{\n" 20765 " return i;\n" 20766 "}", 20767 AllmanBraceStyle); 20768 20769 verifyFormat("void foo() {}\n" 20770 "void bar()\n" 20771 "#ifdef _DEBUG\n" 20772 "{\n" 20773 " foo();\n" 20774 "}\n" 20775 "#else\n" 20776 "{\n" 20777 "}\n" 20778 "#endif", 20779 AllmanBraceStyle); 20780 20781 verifyFormat("void foobar() { int i = 5; }\n" 20782 "#ifdef _DEBUG\n" 20783 "void bar() {}\n" 20784 "#else\n" 20785 "void bar() { foobar(); }\n" 20786 "#endif", 20787 AllmanBraceStyle); 20788 20789 EXPECT_EQ(AllmanBraceStyle.AllowShortLambdasOnASingleLine, 20790 FormatStyle::SLS_All); 20791 20792 verifyFormat("[](int i) { return i + 2; };\n" 20793 "[](int i, int j)\n" 20794 "{\n" 20795 " auto x = i + j;\n" 20796 " auto y = i * j;\n" 20797 " return x ^ y;\n" 20798 "};\n" 20799 "void foo()\n" 20800 "{\n" 20801 " auto shortLambda = [](int i) { return i + 2; };\n" 20802 " auto longLambda = [](int i, int j)\n" 20803 " {\n" 20804 " auto x = i + j;\n" 20805 " auto y = i * j;\n" 20806 " return x ^ y;\n" 20807 " };\n" 20808 "}", 20809 AllmanBraceStyle); 20810 20811 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 20812 20813 verifyFormat("[](int i)\n" 20814 "{\n" 20815 " return i + 2;\n" 20816 "};\n" 20817 "[](int i, int j)\n" 20818 "{\n" 20819 " auto x = i + j;\n" 20820 " auto y = i * j;\n" 20821 " return x ^ y;\n" 20822 "};\n" 20823 "void foo()\n" 20824 "{\n" 20825 " auto shortLambda = [](int i)\n" 20826 " {\n" 20827 " return i + 2;\n" 20828 " };\n" 20829 " auto longLambda = [](int i, int j)\n" 20830 " {\n" 20831 " auto x = i + j;\n" 20832 " auto y = i * j;\n" 20833 " return x ^ y;\n" 20834 " };\n" 20835 "}", 20836 AllmanBraceStyle); 20837 20838 // Reset 20839 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_All; 20840 20841 // This shouldn't affect ObjC blocks.. 20842 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 20843 " // ...\n" 20844 " int i;\n" 20845 "}];", 20846 AllmanBraceStyle); 20847 verifyFormat("void (^block)(void) = ^{\n" 20848 " // ...\n" 20849 " int i;\n" 20850 "};", 20851 AllmanBraceStyle); 20852 // .. or dict literals. 20853 verifyFormat("void f()\n" 20854 "{\n" 20855 " // ...\n" 20856 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 20857 "}", 20858 AllmanBraceStyle); 20859 verifyFormat("void f()\n" 20860 "{\n" 20861 " // ...\n" 20862 " [object someMethod:@{a : @\"b\"}];\n" 20863 "}", 20864 AllmanBraceStyle); 20865 verifyFormat("int f()\n" 20866 "{ // comment\n" 20867 " return 42;\n" 20868 "}", 20869 AllmanBraceStyle); 20870 20871 AllmanBraceStyle.ColumnLimit = 19; 20872 verifyFormat("void f() { int i; }", AllmanBraceStyle); 20873 AllmanBraceStyle.ColumnLimit = 18; 20874 verifyFormat("void f()\n" 20875 "{\n" 20876 " int i;\n" 20877 "}", 20878 AllmanBraceStyle); 20879 AllmanBraceStyle.ColumnLimit = 80; 20880 20881 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle; 20882 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = 20883 FormatStyle::SIS_WithoutElse; 20884 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 20885 verifyFormat("void f(bool b)\n" 20886 "{\n" 20887 " if (b)\n" 20888 " {\n" 20889 " return;\n" 20890 " }\n" 20891 "}", 20892 BreakBeforeBraceShortIfs); 20893 verifyFormat("void f(bool b)\n" 20894 "{\n" 20895 " if constexpr (b)\n" 20896 " {\n" 20897 " return;\n" 20898 " }\n" 20899 "}", 20900 BreakBeforeBraceShortIfs); 20901 verifyFormat("void f(bool b)\n" 20902 "{\n" 20903 " if CONSTEXPR (b)\n" 20904 " {\n" 20905 " return;\n" 20906 " }\n" 20907 "}", 20908 BreakBeforeBraceShortIfs); 20909 verifyFormat("void f(bool b)\n" 20910 "{\n" 20911 " if (b) return;\n" 20912 "}", 20913 BreakBeforeBraceShortIfs); 20914 verifyFormat("void f(bool b)\n" 20915 "{\n" 20916 " if constexpr (b) return;\n" 20917 "}", 20918 BreakBeforeBraceShortIfs); 20919 verifyFormat("void f(bool b)\n" 20920 "{\n" 20921 " if CONSTEXPR (b) return;\n" 20922 "}", 20923 BreakBeforeBraceShortIfs); 20924 verifyFormat("void f(bool b)\n" 20925 "{\n" 20926 " while (b)\n" 20927 " {\n" 20928 " return;\n" 20929 " }\n" 20930 "}", 20931 BreakBeforeBraceShortIfs); 20932 } 20933 20934 TEST_F(FormatTest, WhitesmithsBraceBreaking) { 20935 FormatStyle WhitesmithsBraceStyle = getLLVMStyleWithColumns(0); 20936 WhitesmithsBraceStyle.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; 20937 20938 // Make a few changes to the style for testing purposes 20939 WhitesmithsBraceStyle.AllowShortFunctionsOnASingleLine = 20940 FormatStyle::SFS_Empty; 20941 WhitesmithsBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 20942 20943 // FIXME: this test case can't decide whether there should be a blank line 20944 // after the ~D() line or not. It adds one if one doesn't exist in the test 20945 // and it removes the line if one exists. 20946 /* 20947 verifyFormat("class A;\n" 20948 "namespace B\n" 20949 " {\n" 20950 "class C;\n" 20951 "// Comment\n" 20952 "class D\n" 20953 " {\n" 20954 "public:\n" 20955 " D();\n" 20956 " ~D() {}\n" 20957 "private:\n" 20958 " enum E\n" 20959 " {\n" 20960 " F\n" 20961 " }\n" 20962 " };\n" 20963 " } // namespace B", 20964 WhitesmithsBraceStyle); 20965 */ 20966 20967 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_None; 20968 verifyFormat("namespace a\n" 20969 " {\n" 20970 "class A\n" 20971 " {\n" 20972 " void f()\n" 20973 " {\n" 20974 " if (true)\n" 20975 " {\n" 20976 " a();\n" 20977 " b();\n" 20978 " }\n" 20979 " }\n" 20980 " void g()\n" 20981 " {\n" 20982 " return;\n" 20983 " }\n" 20984 " };\n" 20985 "struct B\n" 20986 " {\n" 20987 " int x;\n" 20988 " };\n" 20989 " } // namespace a", 20990 WhitesmithsBraceStyle); 20991 20992 verifyFormat("namespace a\n" 20993 " {\n" 20994 "namespace b\n" 20995 " {\n" 20996 "class A\n" 20997 " {\n" 20998 " void f()\n" 20999 " {\n" 21000 " if (true)\n" 21001 " {\n" 21002 " a();\n" 21003 " b();\n" 21004 " }\n" 21005 " }\n" 21006 " void g()\n" 21007 " {\n" 21008 " return;\n" 21009 " }\n" 21010 " };\n" 21011 "struct B\n" 21012 " {\n" 21013 " int x;\n" 21014 " };\n" 21015 " } // namespace b\n" 21016 " } // namespace a", 21017 WhitesmithsBraceStyle); 21018 21019 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_Inner; 21020 verifyFormat("namespace a\n" 21021 " {\n" 21022 "namespace b\n" 21023 " {\n" 21024 " class A\n" 21025 " {\n" 21026 " void f()\n" 21027 " {\n" 21028 " if (true)\n" 21029 " {\n" 21030 " a();\n" 21031 " b();\n" 21032 " }\n" 21033 " }\n" 21034 " void g()\n" 21035 " {\n" 21036 " return;\n" 21037 " }\n" 21038 " };\n" 21039 " struct B\n" 21040 " {\n" 21041 " int x;\n" 21042 " };\n" 21043 " } // namespace b\n" 21044 " } // namespace a", 21045 WhitesmithsBraceStyle); 21046 21047 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_All; 21048 verifyFormat("namespace a\n" 21049 " {\n" 21050 " namespace b\n" 21051 " {\n" 21052 " class A\n" 21053 " {\n" 21054 " void f()\n" 21055 " {\n" 21056 " if (true)\n" 21057 " {\n" 21058 " a();\n" 21059 " b();\n" 21060 " }\n" 21061 " }\n" 21062 " void g()\n" 21063 " {\n" 21064 " return;\n" 21065 " }\n" 21066 " };\n" 21067 " struct B\n" 21068 " {\n" 21069 " int x;\n" 21070 " };\n" 21071 " } // namespace b\n" 21072 " } // namespace a", 21073 WhitesmithsBraceStyle); 21074 21075 verifyFormat("void f()\n" 21076 " {\n" 21077 " if (true)\n" 21078 " {\n" 21079 " a();\n" 21080 " }\n" 21081 " else if (false)\n" 21082 " {\n" 21083 " b();\n" 21084 " }\n" 21085 " else\n" 21086 " {\n" 21087 " c();\n" 21088 " }\n" 21089 " }", 21090 WhitesmithsBraceStyle); 21091 21092 verifyFormat("void f()\n" 21093 " {\n" 21094 " for (int i = 0; i < 10; ++i)\n" 21095 " {\n" 21096 " a();\n" 21097 " }\n" 21098 " while (false)\n" 21099 " {\n" 21100 " b();\n" 21101 " }\n" 21102 " do\n" 21103 " {\n" 21104 " c();\n" 21105 " } while (false)\n" 21106 " }", 21107 WhitesmithsBraceStyle); 21108 21109 WhitesmithsBraceStyle.IndentCaseLabels = true; 21110 verifyFormat("void switchTest1(int a)\n" 21111 " {\n" 21112 " switch (a)\n" 21113 " {\n" 21114 " case 2:\n" 21115 " {\n" 21116 " }\n" 21117 " break;\n" 21118 " }\n" 21119 " }", 21120 WhitesmithsBraceStyle); 21121 21122 verifyFormat("void switchTest2(int a)\n" 21123 " {\n" 21124 " switch (a)\n" 21125 " {\n" 21126 " case 0:\n" 21127 " break;\n" 21128 " case 1:\n" 21129 " {\n" 21130 " break;\n" 21131 " }\n" 21132 " case 2:\n" 21133 " {\n" 21134 " }\n" 21135 " break;\n" 21136 " default:\n" 21137 " break;\n" 21138 " }\n" 21139 " }", 21140 WhitesmithsBraceStyle); 21141 21142 verifyFormat("void switchTest3(int a)\n" 21143 " {\n" 21144 " switch (a)\n" 21145 " {\n" 21146 " case 0:\n" 21147 " {\n" 21148 " foo(x);\n" 21149 " }\n" 21150 " break;\n" 21151 " default:\n" 21152 " {\n" 21153 " foo(1);\n" 21154 " }\n" 21155 " break;\n" 21156 " }\n" 21157 " }", 21158 WhitesmithsBraceStyle); 21159 21160 WhitesmithsBraceStyle.IndentCaseLabels = false; 21161 21162 verifyFormat("void switchTest4(int a)\n" 21163 " {\n" 21164 " switch (a)\n" 21165 " {\n" 21166 " case 2:\n" 21167 " {\n" 21168 " }\n" 21169 " break;\n" 21170 " }\n" 21171 " }", 21172 WhitesmithsBraceStyle); 21173 21174 verifyFormat("void switchTest5(int a)\n" 21175 " {\n" 21176 " switch (a)\n" 21177 " {\n" 21178 " case 0:\n" 21179 " break;\n" 21180 " case 1:\n" 21181 " {\n" 21182 " foo();\n" 21183 " break;\n" 21184 " }\n" 21185 " case 2:\n" 21186 " {\n" 21187 " }\n" 21188 " break;\n" 21189 " default:\n" 21190 " break;\n" 21191 " }\n" 21192 " }", 21193 WhitesmithsBraceStyle); 21194 21195 verifyFormat("void switchTest6(int a)\n" 21196 " {\n" 21197 " switch (a)\n" 21198 " {\n" 21199 " case 0:\n" 21200 " {\n" 21201 " foo(x);\n" 21202 " }\n" 21203 " break;\n" 21204 " default:\n" 21205 " {\n" 21206 " foo(1);\n" 21207 " }\n" 21208 " break;\n" 21209 " }\n" 21210 " }", 21211 WhitesmithsBraceStyle); 21212 21213 verifyFormat("enum X\n" 21214 " {\n" 21215 " Y = 0, // testing\n" 21216 " }", 21217 WhitesmithsBraceStyle); 21218 21219 verifyFormat("enum X\n" 21220 " {\n" 21221 " Y = 0\n" 21222 " }", 21223 WhitesmithsBraceStyle); 21224 verifyFormat("enum X\n" 21225 " {\n" 21226 " Y = 0,\n" 21227 " Z = 1\n" 21228 " };", 21229 WhitesmithsBraceStyle); 21230 21231 verifyFormat("@interface BSApplicationController ()\n" 21232 " {\n" 21233 "@private\n" 21234 " id _extraIvar;\n" 21235 " }\n" 21236 "@end", 21237 WhitesmithsBraceStyle); 21238 21239 verifyFormat("#ifdef _DEBUG\n" 21240 "int foo(int i = 0)\n" 21241 "#else\n" 21242 "int foo(int i = 5)\n" 21243 "#endif\n" 21244 " {\n" 21245 " return i;\n" 21246 " }", 21247 WhitesmithsBraceStyle); 21248 21249 verifyFormat("void foo() {}\n" 21250 "void bar()\n" 21251 "#ifdef _DEBUG\n" 21252 " {\n" 21253 " foo();\n" 21254 " }\n" 21255 "#else\n" 21256 " {\n" 21257 " }\n" 21258 "#endif", 21259 WhitesmithsBraceStyle); 21260 21261 verifyFormat("void foobar()\n" 21262 " {\n" 21263 " int i = 5;\n" 21264 " }\n" 21265 "#ifdef _DEBUG\n" 21266 "void bar() {}\n" 21267 "#else\n" 21268 "void bar()\n" 21269 " {\n" 21270 " foobar();\n" 21271 " }\n" 21272 "#endif", 21273 WhitesmithsBraceStyle); 21274 21275 // This shouldn't affect ObjC blocks.. 21276 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 21277 " // ...\n" 21278 " int i;\n" 21279 "}];", 21280 WhitesmithsBraceStyle); 21281 verifyFormat("void (^block)(void) = ^{\n" 21282 " // ...\n" 21283 " int i;\n" 21284 "};", 21285 WhitesmithsBraceStyle); 21286 // .. or dict literals. 21287 verifyFormat("void f()\n" 21288 " {\n" 21289 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 21290 " }", 21291 WhitesmithsBraceStyle); 21292 21293 verifyFormat("int f()\n" 21294 " { // comment\n" 21295 " return 42;\n" 21296 " }", 21297 WhitesmithsBraceStyle); 21298 21299 FormatStyle BreakBeforeBraceShortIfs = WhitesmithsBraceStyle; 21300 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = 21301 FormatStyle::SIS_OnlyFirstIf; 21302 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 21303 verifyFormat("void f(bool b)\n" 21304 " {\n" 21305 " if (b)\n" 21306 " {\n" 21307 " return;\n" 21308 " }\n" 21309 " }", 21310 BreakBeforeBraceShortIfs); 21311 verifyFormat("void f(bool b)\n" 21312 " {\n" 21313 " if (b) return;\n" 21314 " }", 21315 BreakBeforeBraceShortIfs); 21316 verifyFormat("void f(bool b)\n" 21317 " {\n" 21318 " while (b)\n" 21319 " {\n" 21320 " return;\n" 21321 " }\n" 21322 " }", 21323 BreakBeforeBraceShortIfs); 21324 } 21325 21326 TEST_F(FormatTest, GNUBraceBreaking) { 21327 FormatStyle GNUBraceStyle = getLLVMStyle(); 21328 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU; 21329 verifyFormat("namespace a\n" 21330 "{\n" 21331 "class A\n" 21332 "{\n" 21333 " void f()\n" 21334 " {\n" 21335 " int a;\n" 21336 " {\n" 21337 " int b;\n" 21338 " }\n" 21339 " if (true)\n" 21340 " {\n" 21341 " a();\n" 21342 " b();\n" 21343 " }\n" 21344 " }\n" 21345 " void g() { return; }\n" 21346 "}\n" 21347 "} // namespace a", 21348 GNUBraceStyle); 21349 21350 verifyFormat("void f()\n" 21351 "{\n" 21352 " if (true)\n" 21353 " {\n" 21354 " a();\n" 21355 " }\n" 21356 " else if (false)\n" 21357 " {\n" 21358 " b();\n" 21359 " }\n" 21360 " else\n" 21361 " {\n" 21362 " c();\n" 21363 " }\n" 21364 "}", 21365 GNUBraceStyle); 21366 21367 verifyFormat("void f()\n" 21368 "{\n" 21369 " for (int i = 0; i < 10; ++i)\n" 21370 " {\n" 21371 " a();\n" 21372 " }\n" 21373 " while (false)\n" 21374 " {\n" 21375 " b();\n" 21376 " }\n" 21377 " do\n" 21378 " {\n" 21379 " c();\n" 21380 " }\n" 21381 " while (false);\n" 21382 "}", 21383 GNUBraceStyle); 21384 21385 verifyFormat("void f(int a)\n" 21386 "{\n" 21387 " switch (a)\n" 21388 " {\n" 21389 " case 0:\n" 21390 " break;\n" 21391 " case 1:\n" 21392 " {\n" 21393 " break;\n" 21394 " }\n" 21395 " case 2:\n" 21396 " {\n" 21397 " }\n" 21398 " break;\n" 21399 " default:\n" 21400 " break;\n" 21401 " }\n" 21402 "}", 21403 GNUBraceStyle); 21404 21405 verifyFormat("enum X\n" 21406 "{\n" 21407 " Y = 0,\n" 21408 "}", 21409 GNUBraceStyle); 21410 21411 verifyFormat("@interface BSApplicationController ()\n" 21412 "{\n" 21413 "@private\n" 21414 " id _extraIvar;\n" 21415 "}\n" 21416 "@end", 21417 GNUBraceStyle); 21418 21419 verifyFormat("#ifdef _DEBUG\n" 21420 "int foo(int i = 0)\n" 21421 "#else\n" 21422 "int foo(int i = 5)\n" 21423 "#endif\n" 21424 "{\n" 21425 " return i;\n" 21426 "}", 21427 GNUBraceStyle); 21428 21429 verifyFormat("void foo() {}\n" 21430 "void bar()\n" 21431 "#ifdef _DEBUG\n" 21432 "{\n" 21433 " foo();\n" 21434 "}\n" 21435 "#else\n" 21436 "{\n" 21437 "}\n" 21438 "#endif", 21439 GNUBraceStyle); 21440 21441 verifyFormat("void foobar() { int i = 5; }\n" 21442 "#ifdef _DEBUG\n" 21443 "void bar() {}\n" 21444 "#else\n" 21445 "void bar() { foobar(); }\n" 21446 "#endif", 21447 GNUBraceStyle); 21448 } 21449 21450 TEST_F(FormatTest, WebKitBraceBreaking) { 21451 FormatStyle WebKitBraceStyle = getLLVMStyle(); 21452 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit; 21453 WebKitBraceStyle.FixNamespaceComments = false; 21454 verifyFormat("namespace a {\n" 21455 "class A {\n" 21456 " void f()\n" 21457 " {\n" 21458 " if (true) {\n" 21459 " a();\n" 21460 " b();\n" 21461 " }\n" 21462 " }\n" 21463 " void g() { return; }\n" 21464 "};\n" 21465 "enum E {\n" 21466 " A,\n" 21467 " // foo\n" 21468 " B,\n" 21469 " C\n" 21470 "};\n" 21471 "struct B {\n" 21472 " int x;\n" 21473 "};\n" 21474 "}", 21475 WebKitBraceStyle); 21476 verifyFormat("struct S {\n" 21477 " int Type;\n" 21478 " union {\n" 21479 " int x;\n" 21480 " double y;\n" 21481 " } Value;\n" 21482 " class C {\n" 21483 " MyFavoriteType Value;\n" 21484 " } Class;\n" 21485 "};", 21486 WebKitBraceStyle); 21487 } 21488 21489 TEST_F(FormatTest, CatchExceptionReferenceBinding) { 21490 verifyFormat("void f() {\n" 21491 " try {\n" 21492 " } catch (const Exception &e) {\n" 21493 " }\n" 21494 "}"); 21495 } 21496 21497 TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) { 21498 auto Style = getLLVMStyle(); 21499 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 21500 verifyNoCrash("f({\n" 21501 "table({}, table({{\"\", false}}, {}))\n" 21502 "});", 21503 Style); 21504 21505 Style.AlignConsecutiveAssignments.Enabled = true; 21506 Style.AlignConsecutiveDeclarations.Enabled = true; 21507 verifyFormat("struct test demo[] = {\n" 21508 " {56, 23, \"hello\"},\n" 21509 " {-1, 93463, \"world\"},\n" 21510 " { 7, 5, \"!!\"}\n" 21511 "};", 21512 Style); 21513 21514 verifyFormat("struct test demo[] = {\n" 21515 " {56, 23, \"hello\"}, // first line\n" 21516 " {-1, 93463, \"world\"}, // second line\n" 21517 " { 7, 5, \"!!\"} // third line\n" 21518 "};", 21519 Style); 21520 21521 verifyFormat("struct test demo[4] = {\n" 21522 " { 56, 23, 21, \"oh\"}, // first line\n" 21523 " { -1, 93463, 22, \"my\"}, // second line\n" 21524 " { 7, 5, 1, \"goodness\"} // third line\n" 21525 " {234, 5, 1, \"gracious\"} // fourth line\n" 21526 "};", 21527 Style); 21528 21529 verifyFormat("struct test demo[3] = {\n" 21530 " {56, 23, \"hello\"},\n" 21531 " {-1, 93463, \"world\"},\n" 21532 " { 7, 5, \"!!\"}\n" 21533 "};", 21534 Style); 21535 21536 verifyFormat("struct test demo[3] = {\n" 21537 " {int{56}, 23, \"hello\"},\n" 21538 " {int{-1}, 93463, \"world\"},\n" 21539 " { int{7}, 5, \"!!\"}\n" 21540 "};", 21541 Style); 21542 21543 verifyFormat("struct test demo[] = {\n" 21544 " {56, 23, \"hello\"},\n" 21545 " {-1, 93463, \"world\"},\n" 21546 " { 7, 5, \"!!\"},\n" 21547 "};", 21548 Style); 21549 21550 verifyFormat("test demo[] = {\n" 21551 " {56, 23, \"hello\"},\n" 21552 " {-1, 93463, \"world\"},\n" 21553 " { 7, 5, \"!!\"},\n" 21554 "};", 21555 Style); 21556 21557 verifyFormat("demo = std::array<struct test, 3>{\n" 21558 " test{56, 23, \"hello\"},\n" 21559 " test{-1, 93463, \"world\"},\n" 21560 " test{ 7, 5, \"!!\"},\n" 21561 "};", 21562 Style); 21563 21564 verifyFormat("test demo[] = {\n" 21565 " {56, 23, \"hello\"},\n" 21566 "#if X\n" 21567 " {-1, 93463, \"world\"},\n" 21568 "#endif\n" 21569 " { 7, 5, \"!!\"}\n" 21570 "};", 21571 Style); 21572 21573 verifyFormat( 21574 "test demo[] = {\n" 21575 " { 7, 23,\n" 21576 " \"hello world i am a very long line that really, in any\"\n" 21577 " \"just world, ought to be split over multiple lines\"},\n" 21578 " {-1, 93463, \"world\"},\n" 21579 " {56, 5, \"!!\"}\n" 21580 "};", 21581 Style); 21582 21583 verifyNoCrash("Foo f[] = {\n" 21584 " [0] = { 1, },\n" 21585 " [i] { 1, },\n" 21586 "};", 21587 Style); 21588 verifyNoCrash("Foo foo[] = {\n" 21589 " [0] = {1, 1},\n" 21590 " [1] { 1, 1, },\n" 21591 " [2] { 1, 1, },\n" 21592 "};", 21593 Style); 21594 verifyNoCrash("test arr[] = {\n" 21595 "#define FOO(i) {i, i},\n" 21596 "SOME_GENERATOR(FOO)\n" 21597 "{2, 2}\n" 21598 "};", 21599 Style); 21600 21601 verifyFormat("return GradForUnaryCwise(g, {\n" 21602 " {{\"sign\"}, \"Sign\", " 21603 " {\"x\", \"dy\"}},\n" 21604 " { {\"dx\"}, \"Mul\", {\"dy\"" 21605 ", \"sign\"}},\n" 21606 "});", 21607 Style); 21608 21609 Style.Cpp11BracedListStyle = false; 21610 verifyFormat("struct test demo[] = {\n" 21611 " { 56, 23, \"hello\" },\n" 21612 " { -1, 93463, \"world\" },\n" 21613 " { 7, 5, \"!!\" }\n" 21614 "};", 21615 Style); 21616 Style.Cpp11BracedListStyle = true; 21617 21618 Style.ColumnLimit = 0; 21619 verifyFormat( 21620 "test demo[] = {\n" 21621 " {56, 23, \"hello world i am a very long line that really, " 21622 "in any just world, ought to be split over multiple lines\"},\n" 21623 " {-1, 93463, " 21624 " \"world\"},\n" 21625 " { 7, 5, " 21626 " \"!!\"},\n" 21627 "};", 21628 "test demo[] = {{56, 23, \"hello world i am a very long line " 21629 "that really, in any just world, ought to be split over multiple " 21630 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 21631 Style); 21632 21633 Style.ColumnLimit = 80; 21634 verifyFormat("test demo[] = {\n" 21635 " {56, 23, /* a comment */ \"hello\"},\n" 21636 " {-1, 93463, \"world\"},\n" 21637 " { 7, 5, \"!!\"}\n" 21638 "};", 21639 Style); 21640 21641 verifyFormat("test demo[] = {\n" 21642 " {56, 23, \"hello\"},\n" 21643 " {-1, 93463, \"world\" /* comment here */},\n" 21644 " { 7, 5, \"!!\"}\n" 21645 "};", 21646 Style); 21647 21648 verifyFormat("test demo[] = {\n" 21649 " {56, /* a comment */ 23, \"hello\"},\n" 21650 " {-1, 93463, \"world\"},\n" 21651 " { 7, 5, \"!!\"}\n" 21652 "};", 21653 Style); 21654 21655 Style.ColumnLimit = 20; 21656 verifyFormat("demo = std::array<\n" 21657 " struct test, 3>{\n" 21658 " test{\n" 21659 " 56, 23,\n" 21660 " \"hello \"\n" 21661 " \"world i \"\n" 21662 " \"am a very \"\n" 21663 " \"long line \"\n" 21664 " \"that \"\n" 21665 " \"really, \"\n" 21666 " \"in any \"\n" 21667 " \"just \"\n" 21668 " \"world, \"\n" 21669 " \"ought to \"\n" 21670 " \"be split \"\n" 21671 " \"over \"\n" 21672 " \"multiple \"\n" 21673 " \"lines\"},\n" 21674 " test{-1, 93463,\n" 21675 " \"world\"},\n" 21676 " test{ 7, 5,\n" 21677 " \"!!\" },\n" 21678 "};", 21679 "demo = std::array<struct test, 3>{test{56, 23, \"hello world " 21680 "i am a very long line that really, in any just world, ought " 21681 "to be split over multiple lines\"},test{-1, 93463, \"world\"}," 21682 "test{7, 5, \"!!\"},};", 21683 Style); 21684 // This caused a core dump by enabling Alignment in the LLVMStyle globally 21685 Style = getLLVMStyleWithColumns(50); 21686 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 21687 verifyFormat("static A x = {\n" 21688 " {{init1, init2, init3, init4},\n" 21689 " {init1, init2, init3, init4}}\n" 21690 "};", 21691 Style); 21692 // TODO: Fix the indentations below when this option is fully functional. 21693 #if 0 21694 verifyFormat("int a[][] = {\n" 21695 " {\n" 21696 " {0, 2}, //\n" 21697 " {1, 2} //\n" 21698 " }\n" 21699 "};", 21700 Style); 21701 #endif 21702 Style.ColumnLimit = 100; 21703 verifyFormat( 21704 "test demo[] = {\n" 21705 " {56, 23,\n" 21706 " \"hello world i am a very long line that really, in any just world" 21707 ", ought to be split over \"\n" 21708 " \"multiple lines\" },\n" 21709 " {-1, 93463, \"world\"},\n" 21710 " { 7, 5, \"!!\"},\n" 21711 "};", 21712 "test demo[] = {{56, 23, \"hello world i am a very long line " 21713 "that really, in any just world, ought to be split over multiple " 21714 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 21715 Style); 21716 21717 Style = getLLVMStyleWithColumns(50); 21718 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 21719 verifyFormat("struct test demo[] = {\n" 21720 " {56, 23, \"hello\"},\n" 21721 " {-1, 93463, \"world\"},\n" 21722 " { 7, 5, \"!!\"}\n" 21723 "};\n" 21724 "static A x = {\n" 21725 " {{init1, init2, init3, init4},\n" 21726 " {init1, init2, init3, init4}}\n" 21727 "};", 21728 Style); 21729 Style.ColumnLimit = 100; 21730 Style.AlignConsecutiveAssignments.AcrossComments = true; 21731 Style.AlignConsecutiveDeclarations.AcrossComments = true; 21732 verifyFormat("struct test demo[] = {\n" 21733 " {56, 23, \"hello\"},\n" 21734 " {-1, 93463, \"world\"},\n" 21735 " { 7, 5, \"!!\"}\n" 21736 "};\n" 21737 "struct test demo[4] = {\n" 21738 " { 56, 23, 21, \"oh\"}, // first line\n" 21739 " { -1, 93463, 22, \"my\"}, // second line\n" 21740 " { 7, 5, 1, \"goodness\"} // third line\n" 21741 " {234, 5, 1, \"gracious\"} // fourth line\n" 21742 "};", 21743 Style); 21744 verifyFormat( 21745 "test demo[] = {\n" 21746 " {56,\n" 21747 " \"hello world i am a very long line that really, in any just world" 21748 ", ought to be split over \"\n" 21749 " \"multiple lines\", 23},\n" 21750 " {-1, \"world\", 93463},\n" 21751 " { 7, \"!!\", 5},\n" 21752 "};", 21753 "test demo[] = {{56, \"hello world i am a very long line " 21754 "that really, in any just world, ought to be split over multiple " 21755 "lines\", 23},{-1, \"world\", 93463},{7, \"!!\", 5},};", 21756 Style); 21757 } 21758 21759 TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) { 21760 auto Style = getLLVMStyle(); 21761 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left; 21762 /* FIXME: This case gets misformatted. 21763 verifyFormat("auto foo = Items{\n" 21764 " Section{0, bar(), },\n" 21765 " Section{1, boo() }\n" 21766 "};", 21767 Style); 21768 */ 21769 verifyFormat("auto foo = Items{\n" 21770 " Section{\n" 21771 " 0, bar(),\n" 21772 " }\n" 21773 "};", 21774 Style); 21775 verifyFormat("struct test demo[] = {\n" 21776 " {56, 23, \"hello\"},\n" 21777 " {-1, 93463, \"world\"},\n" 21778 " {7, 5, \"!!\" }\n" 21779 "};", 21780 Style); 21781 verifyFormat("struct test demo[] = {\n" 21782 " {56, 23, \"hello\"}, // first line\n" 21783 " {-1, 93463, \"world\"}, // second line\n" 21784 " {7, 5, \"!!\" } // third line\n" 21785 "};", 21786 Style); 21787 verifyFormat("struct test demo[4] = {\n" 21788 " {56, 23, 21, \"oh\" }, // first line\n" 21789 " {-1, 93463, 22, \"my\" }, // second line\n" 21790 " {7, 5, 1, \"goodness\"} // third line\n" 21791 " {234, 5, 1, \"gracious\"} // fourth line\n" 21792 "};", 21793 Style); 21794 verifyFormat("struct test demo[3] = {\n" 21795 " {56, 23, \"hello\"},\n" 21796 " {-1, 93463, \"world\"},\n" 21797 " {7, 5, \"!!\" }\n" 21798 "};", 21799 Style); 21800 21801 verifyFormat("struct test demo[3] = {\n" 21802 " {int{56}, 23, \"hello\"},\n" 21803 " {int{-1}, 93463, \"world\"},\n" 21804 " {int{7}, 5, \"!!\" }\n" 21805 "};", 21806 Style); 21807 verifyFormat("struct test demo[] = {\n" 21808 " {56, 23, \"hello\"},\n" 21809 " {-1, 93463, \"world\"},\n" 21810 " {7, 5, \"!!\" },\n" 21811 "};", 21812 Style); 21813 verifyFormat("test demo[] = {\n" 21814 " {56, 23, \"hello\"},\n" 21815 " {-1, 93463, \"world\"},\n" 21816 " {7, 5, \"!!\" },\n" 21817 "};", 21818 Style); 21819 verifyFormat("demo = std::array<struct test, 3>{\n" 21820 " test{56, 23, \"hello\"},\n" 21821 " test{-1, 93463, \"world\"},\n" 21822 " test{7, 5, \"!!\" },\n" 21823 "};", 21824 Style); 21825 verifyFormat("test demo[] = {\n" 21826 " {56, 23, \"hello\"},\n" 21827 "#if X\n" 21828 " {-1, 93463, \"world\"},\n" 21829 "#endif\n" 21830 " {7, 5, \"!!\" }\n" 21831 "};", 21832 Style); 21833 verifyFormat( 21834 "test demo[] = {\n" 21835 " {7, 23,\n" 21836 " \"hello world i am a very long line that really, in any\"\n" 21837 " \"just world, ought to be split over multiple lines\"},\n" 21838 " {-1, 93463, \"world\" },\n" 21839 " {56, 5, \"!!\" }\n" 21840 "};", 21841 Style); 21842 21843 verifyNoCrash("Foo f[] = {\n" 21844 " [0] = { 1, },\n" 21845 " [i] { 1, },\n" 21846 "};", 21847 Style); 21848 verifyNoCrash("Foo foo[] = {\n" 21849 " [0] = {1, 1},\n" 21850 " [1] { 1, 1, },\n" 21851 " [2] { 1, 1, },\n" 21852 "};", 21853 Style); 21854 verifyNoCrash("test arr[] = {\n" 21855 "#define FOO(i) {i, i},\n" 21856 "SOME_GENERATOR(FOO)\n" 21857 "{2, 2}\n" 21858 "};", 21859 Style); 21860 21861 verifyFormat("return GradForUnaryCwise(g, {\n" 21862 " {{\"sign\"}, \"Sign\", {\"x\", " 21863 "\"dy\"} },\n" 21864 " {{\"dx\"}, \"Mul\", " 21865 "{\"dy\", \"sign\"}},\n" 21866 "});", 21867 Style); 21868 21869 Style.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 21870 verifyFormat("#define FOO \\\n" 21871 " int foo[][2] = { \\\n" 21872 " {0, 1} \\\n" 21873 " };", 21874 Style); 21875 21876 Style.Cpp11BracedListStyle = false; 21877 verifyFormat("struct test demo[] = {\n" 21878 " { 56, 23, \"hello\" },\n" 21879 " { -1, 93463, \"world\" },\n" 21880 " { 7, 5, \"!!\" }\n" 21881 "};", 21882 Style); 21883 Style.Cpp11BracedListStyle = true; 21884 21885 Style.ColumnLimit = 0; 21886 verifyFormat( 21887 "test demo[] = {\n" 21888 " {56, 23, \"hello world i am a very long line that really, in any " 21889 "just world, ought to be split over multiple lines\"},\n" 21890 " {-1, 93463, \"world\" " 21891 " },\n" 21892 " {7, 5, \"!!\" " 21893 " },\n" 21894 "};", 21895 "test demo[] = {{56, 23, \"hello world i am a very long line " 21896 "that really, in any just world, ought to be split over multiple " 21897 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 21898 Style); 21899 21900 Style.ColumnLimit = 80; 21901 verifyFormat("test demo[] = {\n" 21902 " {56, 23, /* a comment */ \"hello\"},\n" 21903 " {-1, 93463, \"world\" },\n" 21904 " {7, 5, \"!!\" }\n" 21905 "};", 21906 Style); 21907 21908 verifyFormat("test demo[] = {\n" 21909 " {56, 23, \"hello\" },\n" 21910 " {-1, 93463, \"world\" /* comment here */},\n" 21911 " {7, 5, \"!!\" }\n" 21912 "};", 21913 Style); 21914 21915 verifyFormat("test demo[] = {\n" 21916 " {56, /* a comment */ 23, \"hello\"},\n" 21917 " {-1, 93463, \"world\"},\n" 21918 " {7, 5, \"!!\" }\n" 21919 "};", 21920 Style); 21921 verifyFormat("Foo foo = {\n" 21922 " // comment\n" 21923 " {1, 2}\n" 21924 "};", 21925 Style); 21926 21927 Style.ColumnLimit = 20; 21928 // FIXME: unstable test case 21929 EXPECT_EQ( 21930 "demo = std::array<\n" 21931 " struct test, 3>{\n" 21932 " test{\n" 21933 " 56, 23,\n" 21934 " \"hello \"\n" 21935 " \"world i \"\n" 21936 " \"am a very \"\n" 21937 " \"long line \"\n" 21938 " \"that \"\n" 21939 " \"really, \"\n" 21940 " \"in any \"\n" 21941 " \"just \"\n" 21942 " \"world, \"\n" 21943 " \"ought to \"\n" 21944 " \"be split \"\n" 21945 " \"over \"\n" 21946 " \"multiple \"\n" 21947 " \"lines\"},\n" 21948 " test{-1, 93463,\n" 21949 " \"world\"},\n" 21950 " test{7, 5,\n" 21951 " \"!!\" },\n" 21952 "};", 21953 format("demo = std::array<struct test, 3>{test{56, 23, \"hello world " 21954 "i am a very long line that really, in any just world, ought " 21955 "to be split over multiple lines\"},test{-1, 93463, \"world\"}," 21956 "test{7, 5, \"!!\"},};", 21957 Style)); 21958 21959 // This caused a core dump by enabling Alignment in the LLVMStyle globally 21960 Style = getLLVMStyleWithColumns(50); 21961 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left; 21962 verifyFormat("static A x = {\n" 21963 " {{init1, init2, init3, init4},\n" 21964 " {init1, init2, init3, init4}}\n" 21965 "};", 21966 Style); 21967 Style.ColumnLimit = 100; 21968 verifyFormat( 21969 "test demo[] = {\n" 21970 " {56, 23,\n" 21971 " \"hello world i am a very long line that really, in any just world" 21972 ", ought to be split over \"\n" 21973 " \"multiple lines\" },\n" 21974 " {-1, 93463, \"world\"},\n" 21975 " {7, 5, \"!!\" },\n" 21976 "};", 21977 "test demo[] = {{56, 23, \"hello world i am a very long line " 21978 "that really, in any just world, ought to be split over multiple " 21979 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 21980 Style); 21981 21982 Style.ColumnLimit = 25; 21983 verifyNoCrash("Type foo{\n" 21984 " {\n" 21985 " 1, // A\n" 21986 " 2, // B\n" 21987 " 3, // C\n" 21988 " },\n" 21989 " \"hello\",\n" 21990 "};", 21991 Style); 21992 verifyNoCrash("Type object[X][Y] = {\n" 21993 " {{val}, {val}, {val}},\n" 21994 " {{val}, {val}, // some comment\n" 21995 " {val}}\n" 21996 "};", 21997 Style); 21998 21999 Style.ColumnLimit = 120; 22000 verifyNoCrash( 22001 "T v[] {\n" 22002 " { AAAAAAAAAAAAAAAAAAAAAAAAA::aaaaaaaaaaaaaaaaaaa, " 22003 "AAAAAAAAAAAAAAAAAAAAAAAAA::aaaaaaaaaaaaaaaaaaaaaaaa, 1, 0.000000000f, " 22004 "\"00000000000000000000000000000000000000000000000000000000" 22005 "00000000000000000000000000000000000000000000000000000000\" },\n" 22006 "};", 22007 Style); 22008 22009 Style.SpacesInParens = FormatStyle::SIPO_Custom; 22010 Style.SpacesInParensOptions.Other = true; 22011 verifyFormat("Foo foo[] = {\n" 22012 " {1, 1},\n" 22013 " {1, 1},\n" 22014 "};", 22015 Style); 22016 } 22017 22018 TEST_F(FormatTest, UnderstandsPragmas) { 22019 verifyFormat("#pragma omp reduction(| : var)"); 22020 verifyFormat("#pragma omp reduction(+ : var)"); 22021 22022 verifyFormat("#pragma mark Any non-hyphenated or hyphenated string " 22023 "(including parentheses).", 22024 "#pragma mark Any non-hyphenated or hyphenated string " 22025 "(including parentheses)."); 22026 22027 verifyFormat("#pragma mark Any non-hyphenated or hyphenated string " 22028 "(including parentheses).", 22029 "#pragma mark Any non-hyphenated or hyphenated string " 22030 "(including parentheses)."); 22031 22032 verifyFormat("#pragma comment(linker, \\\n" 22033 " \"argument\" \\\n" 22034 " \"argument\"", 22035 "#pragma comment(linker, \\\n" 22036 " \"argument\" \\\n" 22037 " \"argument\"", 22038 getStyleWithColumns( 22039 getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp), 32)); 22040 } 22041 22042 TEST_F(FormatTest, UnderstandsPragmaOmpTarget) { 22043 verifyFormat("#pragma omp target map(to : var)"); 22044 verifyFormat("#pragma omp target map(to : var[ : N])"); 22045 verifyFormat("#pragma omp target map(to : var[0 : N])"); 22046 verifyFormat("#pragma omp target map(always, to : var[0 : N])"); 22047 22048 verifyFormat( 22049 "#pragma omp target \\\n" 22050 " reduction(+ : var) \\\n" 22051 " map(to : A[0 : N]) \\\n" 22052 " map(to : B[0 : N]) \\\n" 22053 " map(from : C[0 : N]) \\\n" 22054 " firstprivate(i) \\\n" 22055 " firstprivate(j) \\\n" 22056 " firstprivate(k)", 22057 "#pragma omp target reduction(+:var) map(to:A[0:N]) map(to:B[0:N]) " 22058 "map(from:C[0:N]) firstprivate(i) firstprivate(j) firstprivate(k)", 22059 getLLVMStyleWithColumns(26)); 22060 } 22061 22062 TEST_F(FormatTest, UnderstandPragmaOption) { 22063 verifyFormat("#pragma option -C -A"); 22064 22065 verifyFormat("#pragma option -C -A", "#pragma option -C -A"); 22066 } 22067 22068 TEST_F(FormatTest, UnderstandPragmaRegion) { 22069 auto Style = getLLVMStyleWithColumns(0); 22070 verifyFormat("#pragma region TEST(FOO : BAR)", Style); 22071 verifyFormat("#pragma region TEST(FOO: NOSPACE)", Style); 22072 } 22073 22074 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) { 22075 FormatStyle Style = getLLVMStyleWithColumns(20); 22076 22077 // See PR41213 22078 verifyFormat("/*\n" 22079 " *\t9012345\n" 22080 " * /8901\n" 22081 " */", 22082 "/*\n" 22083 " *\t9012345 /8901\n" 22084 " */", 22085 Style); 22086 verifyFormat("/*\n" 22087 " *345678\n" 22088 " *\t/8901\n" 22089 " */", 22090 "/*\n" 22091 " *345678\t/8901\n" 22092 " */", 22093 Style); 22094 22095 verifyFormat("int a; // the\n" 22096 " // comment", 22097 Style); 22098 verifyNoChange("int a; /* first line\n" 22099 " * second\n" 22100 " * line third\n" 22101 " * line\n" 22102 " */", 22103 Style); 22104 verifyFormat("int a; // first line\n" 22105 " // second\n" 22106 " // line third\n" 22107 " // line", 22108 "int a; // first line\n" 22109 " // second line\n" 22110 " // third line", 22111 Style); 22112 22113 Style.PenaltyExcessCharacter = 90; 22114 verifyFormat("int a; // the comment", Style); 22115 verifyFormat("int a; // the comment\n" 22116 " // aaa", 22117 "int a; // the comment aaa", Style); 22118 verifyNoChange("int a; /* first line\n" 22119 " * second line\n" 22120 " * third line\n" 22121 " */", 22122 Style); 22123 verifyFormat("int a; // first line\n" 22124 " // second line\n" 22125 " // third line", 22126 Style); 22127 // FIXME: Investigate why this is not getting the same layout as the test 22128 // above. 22129 verifyFormat("int a; /* first line\n" 22130 " * second line\n" 22131 " * third line\n" 22132 " */", 22133 "int a; /* first line second line third line" 22134 "\n*/", 22135 Style); 22136 22137 verifyFormat("// foo bar baz bazfoo\n" 22138 "// foo bar foo bar", 22139 "// foo bar baz bazfoo\n" 22140 "// foo bar foo bar", 22141 Style); 22142 verifyFormat("// foo bar baz bazfoo\n" 22143 "// foo bar foo bar", 22144 "// foo bar baz bazfoo\n" 22145 "// foo bar foo bar", 22146 Style); 22147 22148 // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the 22149 // next one. 22150 verifyFormat("// foo bar baz bazfoo\n" 22151 "// bar foo bar", 22152 "// foo bar baz bazfoo bar\n" 22153 "// foo bar", 22154 Style); 22155 22156 // FIXME: unstable test case 22157 EXPECT_EQ("// foo bar baz bazfoo\n" 22158 "// foo bar baz bazfoo\n" 22159 "// bar foo bar", 22160 format("// foo bar baz bazfoo\n" 22161 "// foo bar baz bazfoo bar\n" 22162 "// foo bar", 22163 Style)); 22164 22165 // FIXME: unstable test case 22166 EXPECT_EQ("// foo bar baz bazfoo\n" 22167 "// foo bar baz bazfoo\n" 22168 "// bar foo bar", 22169 format("// foo bar baz bazfoo\n" 22170 "// foo bar baz bazfoo bar\n" 22171 "// foo bar", 22172 Style)); 22173 22174 // Make sure we do not keep protruding characters if strict mode reflow is 22175 // cheaper than keeping protruding characters. 22176 Style.ColumnLimit = 21; 22177 verifyFormat("// foo foo foo foo\n" 22178 "// foo foo foo foo\n" 22179 "// foo foo foo foo", 22180 "// foo foo foo foo foo foo foo foo foo foo foo foo", Style); 22181 22182 verifyFormat("int a = /* long block\n" 22183 " comment */\n" 22184 " 42;", 22185 "int a = /* long block comment */ 42;", Style); 22186 } 22187 22188 TEST_F(FormatTest, BreakPenaltyAfterLParen) { 22189 FormatStyle Style = getLLVMStyle(); 22190 Style.ColumnLimit = 8; 22191 Style.PenaltyExcessCharacter = 15; 22192 verifyFormat("int foo(\n" 22193 " int aaaaaaaaaaaaaaaaaaaaaaaa);", 22194 Style); 22195 Style.PenaltyBreakOpenParenthesis = 200; 22196 verifyFormat("int foo(int aaaaaaaaaaaaaaaaaaaaaaaa);", 22197 "int foo(\n" 22198 " int aaaaaaaaaaaaaaaaaaaaaaaa);", 22199 Style); 22200 } 22201 22202 TEST_F(FormatTest, BreakPenaltyAfterCastLParen) { 22203 FormatStyle Style = getLLVMStyle(); 22204 Style.ColumnLimit = 5; 22205 Style.PenaltyExcessCharacter = 150; 22206 verifyFormat("foo((\n" 22207 " int)aaaaaaaaaaaaaaaaaaaaaaaa);", 22208 22209 Style); 22210 Style.PenaltyBreakOpenParenthesis = 100'000; 22211 verifyFormat("foo((int)\n" 22212 " aaaaaaaaaaaaaaaaaaaaaaaa);", 22213 "foo((\n" 22214 "int)aaaaaaaaaaaaaaaaaaaaaaaa);", 22215 Style); 22216 } 22217 22218 TEST_F(FormatTest, BreakPenaltyAfterForLoopLParen) { 22219 FormatStyle Style = getLLVMStyle(); 22220 Style.ColumnLimit = 4; 22221 Style.PenaltyExcessCharacter = 100; 22222 verifyFormat("for (\n" 22223 " int iiiiiiiiiiiiiiiii =\n" 22224 " 0;\n" 22225 " iiiiiiiiiiiiiiiii <\n" 22226 " 2;\n" 22227 " iiiiiiiiiiiiiiiii++) {\n" 22228 "}", 22229 22230 Style); 22231 Style.PenaltyBreakOpenParenthesis = 1250; 22232 verifyFormat("for (int iiiiiiiiiiiiiiiii =\n" 22233 " 0;\n" 22234 " iiiiiiiiiiiiiiiii <\n" 22235 " 2;\n" 22236 " iiiiiiiiiiiiiiiii++) {\n" 22237 "}", 22238 "for (\n" 22239 " int iiiiiiiiiiiiiiiii =\n" 22240 " 0;\n" 22241 " iiiiiiiiiiiiiiiii <\n" 22242 " 2;\n" 22243 " iiiiiiiiiiiiiiiii++) {\n" 22244 "}", 22245 Style); 22246 } 22247 22248 TEST_F(FormatTest, BreakPenaltyScopeResolution) { 22249 FormatStyle Style = getLLVMStyle(); 22250 Style.ColumnLimit = 20; 22251 Style.PenaltyExcessCharacter = 100; 22252 verifyFormat("unsigned long\n" 22253 "foo::bar();", 22254 Style); 22255 Style.PenaltyBreakScopeResolution = 10; 22256 verifyFormat("unsigned long foo::\n" 22257 " bar();", 22258 Style); 22259 } 22260 22261 TEST_F(FormatTest, WorksFor8bitEncodings) { 22262 // FIXME: unstable test case 22263 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" 22264 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" 22265 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" 22266 "\"\xef\xee\xf0\xf3...\"", 22267 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " 22268 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " 22269 "\xef\xee\xf0\xf3...\"", 22270 getLLVMStyleWithColumns(12))); 22271 } 22272 22273 TEST_F(FormatTest, HandlesUTF8BOM) { 22274 verifyFormat("\xef\xbb\xbf"); 22275 verifyFormat("\xef\xbb\xbf#include <iostream>"); 22276 verifyFormat("\xef\xbb\xbf\n#include <iostream>"); 22277 22278 auto Style = getLLVMStyle(); 22279 Style.KeepEmptyLines.AtStartOfFile = false; 22280 verifyFormat("\xef\xbb\xbf#include <iostream>", 22281 "\xef\xbb\xbf\n#include <iostream>", Style); 22282 } 22283 22284 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. 22285 #if !defined(_MSC_VER) 22286 22287 TEST_F(FormatTest, CountsUTF8CharactersProperly) { 22288 verifyFormat("\"Однажды в студёную зимнюю пору...\"", 22289 getLLVMStyleWithColumns(35)); 22290 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", 22291 getLLVMStyleWithColumns(31)); 22292 verifyFormat("// Однажды в студёную зимнюю пору...", 22293 getLLVMStyleWithColumns(36)); 22294 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32)); 22295 verifyFormat("/* Однажды в студёную зимнюю пору... */", 22296 getLLVMStyleWithColumns(39)); 22297 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", 22298 getLLVMStyleWithColumns(35)); 22299 } 22300 22301 TEST_F(FormatTest, SplitsUTF8Strings) { 22302 // Non-printable characters' width is currently considered to be the length in 22303 // bytes in UTF8. The characters can be displayed in very different manner 22304 // (zero-width, single width with a substitution glyph, expanded to their code 22305 // (e.g. "<8d>"), so there's no single correct way to handle them. 22306 // FIXME: unstable test case 22307 EXPECT_EQ("\"aaaaÄ\"\n" 22308 "\"\xc2\x8d\";", 22309 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 22310 // FIXME: unstable test case 22311 EXPECT_EQ("\"aaaaaaaÄ\"\n" 22312 "\"\xc2\x8d\";", 22313 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 22314 // FIXME: unstable test case 22315 EXPECT_EQ("\"Однажды, в \"\n" 22316 "\"студёную \"\n" 22317 "\"зимнюю \"\n" 22318 "\"пору,\"", 22319 format("\"Однажды, в студёную зимнюю пору,\"", 22320 getLLVMStyleWithColumns(13))); 22321 // FIXME: unstable test case 22322 EXPECT_EQ( 22323 "\"一 二 三 \"\n" 22324 "\"四 五六 \"\n" 22325 "\"七 八 九 \"\n" 22326 "\"十\"", 22327 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); 22328 // FIXME: unstable test case 22329 EXPECT_EQ("\"一\t\"\n" 22330 "\"二 \t\"\n" 22331 "\"三 四 \"\n" 22332 "\"五\t\"\n" 22333 "\"六 \t\"\n" 22334 "\"七 \"\n" 22335 "\"八九十\tqq\"", 22336 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", 22337 getLLVMStyleWithColumns(11))); 22338 22339 // UTF8 character in an escape sequence. 22340 // FIXME: unstable test case 22341 EXPECT_EQ("\"aaaaaa\"\n" 22342 "\"\\\xC2\x8D\"", 22343 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10))); 22344 } 22345 22346 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { 22347 verifyFormat("const char *sssss =\n" 22348 " \"一二三四五六七八\\\n" 22349 " 九 十\";", 22350 "const char *sssss = \"一二三四五六七八\\\n" 22351 " 九 十\";", 22352 getLLVMStyleWithColumns(30)); 22353 } 22354 22355 TEST_F(FormatTest, SplitsUTF8LineComments) { 22356 verifyFormat("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)); 22357 verifyFormat("// Я из лесу\n" 22358 "// вышел; был\n" 22359 "// сильный\n" 22360 "// мороз.", 22361 "// Я из лесу вышел; был сильный мороз.", 22362 getLLVMStyleWithColumns(13)); 22363 verifyFormat("// 一二三\n" 22364 "// 四五六七\n" 22365 "// 八 九\n" 22366 "// 十", 22367 "// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9)); 22368 } 22369 22370 TEST_F(FormatTest, SplitsUTF8BlockComments) { 22371 verifyFormat("/* Гляжу,\n" 22372 " * поднимается\n" 22373 " * медленно в\n" 22374 " * гору\n" 22375 " * Лошадка,\n" 22376 " * везущая\n" 22377 " * хворосту\n" 22378 " * воз. */", 22379 "/* Гляжу, поднимается медленно в гору\n" 22380 " * Лошадка, везущая хворосту воз. */", 22381 getLLVMStyleWithColumns(13)); 22382 verifyFormat("/* 一二三\n" 22383 " * 四五六七\n" 22384 " * 八 九\n" 22385 " * 十 */", 22386 "/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9)); 22387 verifyFormat("/* \n" 22388 " * \n" 22389 " * - */", 22390 "/* - */", getLLVMStyleWithColumns(12)); 22391 } 22392 22393 #endif // _MSC_VER 22394 22395 TEST_F(FormatTest, ConstructorInitializerIndentWidth) { 22396 FormatStyle Style = getLLVMStyle(); 22397 22398 Style.ConstructorInitializerIndentWidth = 4; 22399 verifyFormat( 22400 "SomeClass::Constructor()\n" 22401 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 22402 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 22403 Style); 22404 22405 Style.ConstructorInitializerIndentWidth = 2; 22406 verifyFormat( 22407 "SomeClass::Constructor()\n" 22408 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 22409 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 22410 Style); 22411 22412 Style.ConstructorInitializerIndentWidth = 0; 22413 verifyFormat( 22414 "SomeClass::Constructor()\n" 22415 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 22416 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 22417 Style); 22418 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 22419 verifyFormat( 22420 "SomeLongTemplateVariableName<\n" 22421 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>", 22422 Style); 22423 verifyFormat("bool smaller = 1 < " 22424 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 22425 " " 22426 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 22427 Style); 22428 22429 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 22430 verifyFormat("SomeClass::Constructor() :\n" 22431 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa),\n" 22432 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa) {}", 22433 Style); 22434 } 22435 22436 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) { 22437 FormatStyle Style = getLLVMStyle(); 22438 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 22439 Style.ConstructorInitializerIndentWidth = 4; 22440 verifyFormat("SomeClass::Constructor()\n" 22441 " : a(a)\n" 22442 " , b(b)\n" 22443 " , c(c) {}", 22444 Style); 22445 verifyFormat("SomeClass::Constructor()\n" 22446 " : a(a) {}", 22447 Style); 22448 22449 Style.ColumnLimit = 0; 22450 verifyFormat("SomeClass::Constructor()\n" 22451 " : a(a) {}", 22452 Style); 22453 verifyFormat("SomeClass::Constructor() noexcept\n" 22454 " : a(a) {}", 22455 Style); 22456 verifyFormat("SomeClass::Constructor()\n" 22457 " : a(a)\n" 22458 " , b(b)\n" 22459 " , c(c) {}", 22460 Style); 22461 verifyFormat("SomeClass::Constructor()\n" 22462 " : a(a) {\n" 22463 " foo();\n" 22464 " bar();\n" 22465 "}", 22466 Style); 22467 22468 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 22469 verifyFormat("SomeClass::Constructor()\n" 22470 " : a(a)\n" 22471 " , b(b)\n" 22472 " , c(c) {\n}", 22473 Style); 22474 verifyFormat("SomeClass::Constructor()\n" 22475 " : a(a) {\n}", 22476 Style); 22477 22478 Style.ColumnLimit = 80; 22479 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 22480 Style.ConstructorInitializerIndentWidth = 2; 22481 verifyFormat("SomeClass::Constructor()\n" 22482 " : a(a)\n" 22483 " , b(b)\n" 22484 " , c(c) {}", 22485 Style); 22486 22487 Style.ConstructorInitializerIndentWidth = 0; 22488 verifyFormat("SomeClass::Constructor()\n" 22489 ": a(a)\n" 22490 ", b(b)\n" 22491 ", c(c) {}", 22492 Style); 22493 22494 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 22495 Style.ConstructorInitializerIndentWidth = 4; 22496 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style); 22497 verifyFormat( 22498 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)", 22499 Style); 22500 verifyFormat( 22501 "SomeClass::Constructor()\n" 22502 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", 22503 Style); 22504 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly; 22505 verifyFormat("SomeClass::Constructor()\n" 22506 " : aaaaaaaa(aaaaaaaa) {}", 22507 Style); 22508 verifyFormat("SomeClass::Constructor()\n" 22509 " : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)", 22510 Style); 22511 verifyFormat( 22512 "SomeClass::Constructor()\n" 22513 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", 22514 Style); 22515 22516 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 22517 Style.ConstructorInitializerIndentWidth = 4; 22518 Style.ColumnLimit = 60; 22519 verifyFormat("SomeClass::Constructor()\n" 22520 " : aaaaaaaa(aaaaaaaa)\n" 22521 " , aaaaaaaa(aaaaaaaa)\n" 22522 " , aaaaaaaa(aaaaaaaa) {}", 22523 Style); 22524 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly; 22525 verifyFormat("SomeClass::Constructor()\n" 22526 " : aaaaaaaa(aaaaaaaa)\n" 22527 " , aaaaaaaa(aaaaaaaa)\n" 22528 " , aaaaaaaa(aaaaaaaa) {}", 22529 Style); 22530 } 22531 22532 TEST_F(FormatTest, ConstructorInitializersWithPreprocessorDirective) { 22533 FormatStyle Style = getLLVMStyle(); 22534 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 22535 Style.ConstructorInitializerIndentWidth = 4; 22536 verifyFormat("SomeClass::Constructor()\n" 22537 " : a{a}\n" 22538 " , b{b} {}", 22539 Style); 22540 verifyFormat("SomeClass::Constructor()\n" 22541 " : a{a}\n" 22542 "#if CONDITION\n" 22543 " , b{b}\n" 22544 "#endif\n" 22545 "{\n}", 22546 Style); 22547 Style.ConstructorInitializerIndentWidth = 2; 22548 verifyFormat("SomeClass::Constructor()\n" 22549 "#if CONDITION\n" 22550 " : a{a}\n" 22551 "#endif\n" 22552 " , b{b}\n" 22553 " , c{c} {\n}", 22554 Style); 22555 Style.ConstructorInitializerIndentWidth = 0; 22556 verifyFormat("SomeClass::Constructor()\n" 22557 ": a{a}\n" 22558 "#ifdef CONDITION\n" 22559 ", b{b}\n" 22560 "#else\n" 22561 ", c{c}\n" 22562 "#endif\n" 22563 ", d{d} {\n}", 22564 Style); 22565 Style.ConstructorInitializerIndentWidth = 4; 22566 verifyFormat("SomeClass::Constructor()\n" 22567 " : a{a}\n" 22568 "#if WINDOWS\n" 22569 "#if DEBUG\n" 22570 " , b{0}\n" 22571 "#else\n" 22572 " , b{1}\n" 22573 "#endif\n" 22574 "#else\n" 22575 "#if DEBUG\n" 22576 " , b{2}\n" 22577 "#else\n" 22578 " , b{3}\n" 22579 "#endif\n" 22580 "#endif\n" 22581 "{\n}", 22582 Style); 22583 verifyFormat("SomeClass::Constructor()\n" 22584 " : a{a}\n" 22585 "#if WINDOWS\n" 22586 " , b{0}\n" 22587 "#if DEBUG\n" 22588 " , c{0}\n" 22589 "#else\n" 22590 " , c{1}\n" 22591 "#endif\n" 22592 "#else\n" 22593 "#if DEBUG\n" 22594 " , c{2}\n" 22595 "#else\n" 22596 " , c{3}\n" 22597 "#endif\n" 22598 " , b{1}\n" 22599 "#endif\n" 22600 "{\n}", 22601 Style); 22602 } 22603 22604 TEST_F(FormatTest, Destructors) { 22605 verifyFormat("void F(int &i) { i.~int(); }"); 22606 verifyFormat("void F(int &i) { i->~int(); }"); 22607 } 22608 22609 TEST_F(FormatTest, FormatsWithWebKitStyle) { 22610 FormatStyle Style = getWebKitStyle(); 22611 22612 // Don't indent in outer namespaces. 22613 verifyFormat("namespace outer {\n" 22614 "int i;\n" 22615 "namespace inner {\n" 22616 " int i;\n" 22617 "} // namespace inner\n" 22618 "} // namespace outer\n" 22619 "namespace other_outer {\n" 22620 "int i;\n" 22621 "}", 22622 Style); 22623 22624 // Don't indent case labels. 22625 verifyFormat("switch (variable) {\n" 22626 "case 1:\n" 22627 "case 2:\n" 22628 " doSomething();\n" 22629 " break;\n" 22630 "default:\n" 22631 " ++variable;\n" 22632 "}", 22633 Style); 22634 22635 // Wrap before binary operators. 22636 verifyFormat( 22637 "void f()\n" 22638 "{\n" 22639 " if (aaaaaaaaaaaaaaaa\n" 22640 " && bbbbbbbbbbbbbbbbbbbbbbbb\n" 22641 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 22642 " return;\n" 22643 "}", 22644 "void f() {\n" 22645 "if (aaaaaaaaaaaaaaaa\n" 22646 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" 22647 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 22648 "return;\n" 22649 "}", 22650 Style); 22651 22652 // Allow functions on a single line. 22653 verifyFormat("void f() { return; }", Style); 22654 22655 // Allow empty blocks on a single line and insert a space in empty blocks. 22656 verifyFormat("void f() { }", "void f() {}", Style); 22657 verifyFormat("while (true) { }", "while (true) {}", Style); 22658 // However, don't merge non-empty short loops. 22659 verifyFormat("while (true) {\n" 22660 " continue;\n" 22661 "}", 22662 "while (true) { continue; }", Style); 22663 22664 // Constructor initializers are formatted one per line with the "," on the 22665 // new line. 22666 verifyFormat("Constructor()\n" 22667 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 22668 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" 22669 " aaaaaaaaaaaaaa)\n" 22670 " , aaaaaaaaaaaaaaaaaaaaaaa()\n" 22671 "{\n" 22672 "}", 22673 Style); 22674 verifyFormat("SomeClass::Constructor()\n" 22675 " : a(a)\n" 22676 "{\n" 22677 "}", 22678 Style); 22679 verifyFormat("SomeClass::Constructor()\n" 22680 " : a(a)\n" 22681 "{\n" 22682 "}", 22683 "SomeClass::Constructor():a(a){}", Style); 22684 verifyFormat("SomeClass::Constructor()\n" 22685 " : a(a)\n" 22686 " , b(b)\n" 22687 " , c(c)\n" 22688 "{\n" 22689 "}", 22690 Style); 22691 verifyFormat("SomeClass::Constructor()\n" 22692 " : a(a)\n" 22693 "{\n" 22694 " foo();\n" 22695 " bar();\n" 22696 "}", 22697 Style); 22698 22699 // Access specifiers should be aligned left. 22700 verifyFormat("class C {\n" 22701 "public:\n" 22702 " int i;\n" 22703 "};", 22704 Style); 22705 22706 // Do not align comments. 22707 verifyFormat("int a; // Do not\n" 22708 "double b; // align comments.", 22709 Style); 22710 22711 // Do not align operands. 22712 verifyFormat("ASSERT(aaaa\n" 22713 " || bbbb);", 22714 "ASSERT ( aaaa\n||bbbb);", Style); 22715 22716 // Accept input's line breaks. 22717 verifyFormat("if (aaaaaaaaaaaaaaa\n" 22718 " || bbbbbbbbbbbbbbb) {\n" 22719 " i++;\n" 22720 "}", 22721 "if (aaaaaaaaaaaaaaa\n" 22722 "|| bbbbbbbbbbbbbbb) { i++; }", 22723 Style); 22724 verifyFormat("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n" 22725 " i++;\n" 22726 "}", 22727 "if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style); 22728 22729 // Don't automatically break all macro definitions (llvm.org/PR17842). 22730 verifyFormat("#define aNumber 10", Style); 22731 // However, generally keep the line breaks that the user authored. 22732 verifyFormat("#define aNumber \\\n" 22733 " 10", 22734 "#define aNumber \\\n" 22735 " 10", 22736 Style); 22737 22738 // Keep empty and one-element array literals on a single line. 22739 verifyFormat("NSArray* a = [[NSArray alloc] initWithArray:@[]\n" 22740 " copyItems:YES];", 22741 "NSArray*a=[[NSArray alloc] initWithArray:@[]\n" 22742 "copyItems:YES];", 22743 Style); 22744 verifyFormat("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n" 22745 " copyItems:YES];", 22746 "NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n" 22747 " copyItems:YES];", 22748 Style); 22749 // FIXME: This does not seem right, there should be more indentation before 22750 // the array literal's entries. Nested blocks have the same problem. 22751 verifyFormat("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 22752 " @\"a\",\n" 22753 " @\"a\"\n" 22754 "]\n" 22755 " copyItems:YES];", 22756 "NSArray* a = [[NSArray alloc] initWithArray:@[\n" 22757 " @\"a\",\n" 22758 " @\"a\"\n" 22759 " ]\n" 22760 " copyItems:YES];", 22761 Style); 22762 verifyFormat( 22763 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 22764 " copyItems:YES];", 22765 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 22766 " copyItems:YES];", 22767 Style); 22768 22769 verifyFormat("[self.a b:c c:d];", Style); 22770 verifyFormat("[self.a b:c\n" 22771 " c:d];", 22772 "[self.a b:c\n" 22773 "c:d];", 22774 Style); 22775 } 22776 22777 TEST_F(FormatTest, FormatsLambdas) { 22778 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();"); 22779 verifyFormat( 22780 "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();"); 22781 verifyFormat("int c = [&] { [=] { return b++; }(); }();"); 22782 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();"); 22783 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();"); 22784 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}"); 22785 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}"); 22786 verifyFormat("auto c = [a = [b = 42] {}] {};"); 22787 verifyFormat("auto c = [a = &i + 10, b = [] {}] {};"); 22788 verifyFormat("int x = f(*+[] {});"); 22789 verifyFormat("void f() {\n" 22790 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n" 22791 "}"); 22792 verifyFormat("void f() {\n" 22793 " other(x.begin(), //\n" 22794 " x.end(), //\n" 22795 " [&](int, int) { return 1; });\n" 22796 "}"); 22797 verifyFormat("void f() {\n" 22798 " other.other.other.other.other(\n" 22799 " x.begin(), x.end(),\n" 22800 " [something, rather](int, int, int, int, int, int, int) { " 22801 "return 1; });\n" 22802 "}"); 22803 verifyFormat( 22804 "void f() {\n" 22805 " other.other.other.other.other(\n" 22806 " x.begin(), x.end(),\n" 22807 " [something, rather](int, int, int, int, int, int, int) {\n" 22808 " //\n" 22809 " });\n" 22810 "}"); 22811 verifyFormat("SomeFunction([]() { // A cool function...\n" 22812 " return 43;\n" 22813 "});"); 22814 verifyFormat("SomeFunction([]() {\n" 22815 "#define A a\n" 22816 " return 43;\n" 22817 "});", 22818 "SomeFunction([](){\n" 22819 "#define A a\n" 22820 "return 43;\n" 22821 "});"); 22822 verifyFormat("void f() {\n" 22823 " SomeFunction([](decltype(x), A *a) {});\n" 22824 " SomeFunction([](typeof(x), A *a) {});\n" 22825 " SomeFunction([](_Atomic(x), A *a) {});\n" 22826 " SomeFunction([](__underlying_type(x), A *a) {});\n" 22827 "}"); 22828 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 22829 " [](const aaaaaaaaaa &a) { return a; });"); 22830 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n" 22831 " SomeOtherFunctioooooooooooooooooooooooooon();\n" 22832 "});"); 22833 verifyFormat("Constructor()\n" 22834 " : Field([] { // comment\n" 22835 " int i;\n" 22836 " }) {}"); 22837 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n" 22838 " return some_parameter.size();\n" 22839 "};"); 22840 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n" 22841 " [](const string &s) { return s; };"); 22842 verifyFormat("int i = aaaaaa ? 1 //\n" 22843 " : [] {\n" 22844 " return 2; //\n" 22845 " }();"); 22846 verifyFormat("llvm::errs() << \"number of twos is \"\n" 22847 " << std::count_if(v.begin(), v.end(), [](int x) {\n" 22848 " return x == 2; // force break\n" 22849 " });"); 22850 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 22851 " [=](int iiiiiiiiiiii) {\n" 22852 " return aaaaaaaaaaaaaaaaaaaaaaa !=\n" 22853 " aaaaaaaaaaaaaaaaaaaaaaa;\n" 22854 " });", 22855 getLLVMStyleWithColumns(60)); 22856 22857 verifyFormat("SomeFunction({[&] {\n" 22858 " // comment\n" 22859 " },\n" 22860 " [&] {\n" 22861 " // comment\n" 22862 " }});"); 22863 verifyFormat("SomeFunction({[&] {\n" 22864 " // comment\n" 22865 "}});"); 22866 verifyFormat( 22867 "virtual aaaaaaaaaaaaaaaa(\n" 22868 " std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n" 22869 " aaaaa aaaaaaaaa);"); 22870 22871 // Lambdas with return types. 22872 verifyFormat("int c = []() -> int { return 2; }();"); 22873 verifyFormat("int c = []() -> int * { return 2; }();"); 22874 verifyFormat("int c = []() -> vector<int> { return {2}; }();"); 22875 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());"); 22876 verifyFormat("foo([]() noexcept -> int {});"); 22877 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};"); 22878 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};"); 22879 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};"); 22880 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};"); 22881 verifyFormat("[a, a]() -> a<1> {};"); 22882 verifyFormat("[]() -> foo<5 + 2> { return {}; };"); 22883 verifyFormat("[]() -> foo<5 - 2> { return {}; };"); 22884 verifyFormat("[]() -> foo<5 / 2> { return {}; };"); 22885 verifyFormat("[]() -> foo<5 * 2> { return {}; };"); 22886 verifyFormat("[]() -> foo<5 % 2> { return {}; };"); 22887 verifyFormat("[]() -> foo<5 << 2> { return {}; };"); 22888 verifyFormat("[]() -> foo<!5> { return {}; };"); 22889 verifyFormat("[]() -> foo<~5> { return {}; };"); 22890 verifyFormat("[]() -> foo<5 | 2> { return {}; };"); 22891 verifyFormat("[]() -> foo<5 || 2> { return {}; };"); 22892 verifyFormat("[]() -> foo<5 & 2> { return {}; };"); 22893 verifyFormat("[]() -> foo<5 && 2> { return {}; };"); 22894 verifyFormat("[]() -> foo<5 == 2> { return {}; };"); 22895 verifyFormat("[]() -> foo<5 != 2> { return {}; };"); 22896 verifyFormat("[]() -> foo<5 >= 2> { return {}; };"); 22897 verifyFormat("[]() -> foo<5 <= 2> { return {}; };"); 22898 verifyFormat("[]() -> foo<5 < 2> { return {}; };"); 22899 verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };"); 22900 verifyFormat("namespace bar {\n" 22901 "// broken:\n" 22902 "auto foo{[]() -> foo<5 + 2> { return {}; }};\n" 22903 "} // namespace bar"); 22904 verifyFormat("namespace bar {\n" 22905 "// broken:\n" 22906 "auto foo{[]() -> foo<5 - 2> { return {}; }};\n" 22907 "} // namespace bar"); 22908 verifyFormat("namespace bar {\n" 22909 "// broken:\n" 22910 "auto foo{[]() -> foo<5 / 2> { return {}; }};\n" 22911 "} // namespace bar"); 22912 verifyFormat("namespace bar {\n" 22913 "// broken:\n" 22914 "auto foo{[]() -> foo<5 * 2> { return {}; }};\n" 22915 "} // namespace bar"); 22916 verifyFormat("namespace bar {\n" 22917 "// broken:\n" 22918 "auto foo{[]() -> foo<5 % 2> { return {}; }};\n" 22919 "} // namespace bar"); 22920 verifyFormat("namespace bar {\n" 22921 "// broken:\n" 22922 "auto foo{[]() -> foo<5 << 2> { return {}; }};\n" 22923 "} // namespace bar"); 22924 verifyFormat("namespace bar {\n" 22925 "// broken:\n" 22926 "auto foo{[]() -> foo<!5> { return {}; }};\n" 22927 "} // namespace bar"); 22928 verifyFormat("namespace bar {\n" 22929 "// broken:\n" 22930 "auto foo{[]() -> foo<~5> { return {}; }};\n" 22931 "} // namespace bar"); 22932 verifyFormat("namespace bar {\n" 22933 "// broken:\n" 22934 "auto foo{[]() -> foo<5 | 2> { return {}; }};\n" 22935 "} // namespace bar"); 22936 verifyFormat("namespace bar {\n" 22937 "// broken:\n" 22938 "auto foo{[]() -> foo<5 || 2> { return {}; }};\n" 22939 "} // namespace bar"); 22940 verifyFormat("namespace bar {\n" 22941 "// broken:\n" 22942 "auto foo{[]() -> foo<5 & 2> { return {}; }};\n" 22943 "} // namespace bar"); 22944 verifyFormat("namespace bar {\n" 22945 "// broken:\n" 22946 "auto foo{[]() -> foo<5 && 2> { return {}; }};\n" 22947 "} // namespace bar"); 22948 verifyFormat("namespace bar {\n" 22949 "// broken:\n" 22950 "auto foo{[]() -> foo<5 == 2> { return {}; }};\n" 22951 "} // namespace bar"); 22952 verifyFormat("namespace bar {\n" 22953 "// broken:\n" 22954 "auto foo{[]() -> foo<5 != 2> { return {}; }};\n" 22955 "} // namespace bar"); 22956 verifyFormat("namespace bar {\n" 22957 "// broken:\n" 22958 "auto foo{[]() -> foo<5 >= 2> { return {}; }};\n" 22959 "} // namespace bar"); 22960 verifyFormat("namespace bar {\n" 22961 "// broken:\n" 22962 "auto foo{[]() -> foo<5 <= 2> { return {}; }};\n" 22963 "} // namespace bar"); 22964 verifyFormat("namespace bar {\n" 22965 "// broken:\n" 22966 "auto foo{[]() -> foo<5 < 2> { return {}; }};\n" 22967 "} // namespace bar"); 22968 verifyFormat("namespace bar {\n" 22969 "// broken:\n" 22970 "auto foo{[]() -> foo<2 ? 1 : 0> { return {}; }};\n" 22971 "} // namespace bar"); 22972 verifyFormat("[]() -> a<1> {};"); 22973 verifyFormat("[]() -> a<1> { ; };"); 22974 verifyFormat("[]() -> a<1> { ; }();"); 22975 verifyFormat("[a, a]() -> a<true> {};"); 22976 verifyFormat("[]() -> a<true> {};"); 22977 verifyFormat("[]() -> a<true> { ; };"); 22978 verifyFormat("[]() -> a<true> { ; }();"); 22979 verifyFormat("[a, a]() -> a<false> {};"); 22980 verifyFormat("[]() -> a<false> {};"); 22981 verifyFormat("[]() -> a<false> { ; };"); 22982 verifyFormat("[]() -> a<false> { ; }();"); 22983 verifyFormat("auto foo{[]() -> foo<false> { ; }};"); 22984 verifyFormat("namespace bar {\n" 22985 "auto foo{[]() -> foo<false> { ; }};\n" 22986 "} // namespace bar"); 22987 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n" 22988 " int j) -> int {\n" 22989 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n" 22990 "};"); 22991 verifyFormat( 22992 "aaaaaaaaaaaaaaaaaaaaaa(\n" 22993 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n" 22994 " return aaaaaaaaaaaaaaaaa;\n" 22995 " });", 22996 getLLVMStyleWithColumns(70)); 22997 verifyFormat("[]() //\n" 22998 " -> int {\n" 22999 " return 1; //\n" 23000 "};"); 23001 verifyFormat("[]() -> Void<T...> {};"); 23002 verifyFormat("[a, b]() -> Tuple<T...> { return {}; };"); 23003 verifyFormat("SomeFunction({[]() -> int[] { return {}; }});"); 23004 verifyFormat("SomeFunction({[]() -> int *[] { return {}; }});"); 23005 verifyFormat("SomeFunction({[]() -> int (*)[] { return {}; }});"); 23006 verifyFormat("SomeFunction({[]() -> ns::type<int (*)[]> { return {}; }});"); 23007 verifyFormat("foo([&](u32 bar) __attribute__((always_inline)) -> void {});"); 23008 verifyFormat("return int{[x = x]() { return x; }()};"); 23009 23010 // Lambdas with explicit template argument lists. 23011 verifyFormat( 23012 "auto L = []<template <typename> class T, class U>(T<U> &&a) {};"); 23013 verifyFormat("auto L = []<class T>(T) {\n" 23014 " {\n" 23015 " f();\n" 23016 " g();\n" 23017 " }\n" 23018 "};"); 23019 verifyFormat("auto L = []<class... T>(T...) {\n" 23020 " {\n" 23021 " f();\n" 23022 " g();\n" 23023 " }\n" 23024 "};"); 23025 verifyFormat("auto L = []<typename... T>(T...) {\n" 23026 " {\n" 23027 " f();\n" 23028 " g();\n" 23029 " }\n" 23030 "};"); 23031 verifyFormat("auto L = []<template <typename...> class T>(T...) {\n" 23032 " {\n" 23033 " f();\n" 23034 " g();\n" 23035 " }\n" 23036 "};"); 23037 verifyFormat("auto L = []</*comment*/ class... T>(T...) {\n" 23038 " {\n" 23039 " f();\n" 23040 " g();\n" 23041 " }\n" 23042 "};"); 23043 verifyFormat("auto L = []<int... T>(T...) {\n" 23044 " {\n" 23045 " f();\n" 23046 " g();\n" 23047 " }\n" 23048 "};"); 23049 verifyFormat("auto L = []<Foo... T>(T...) {\n" 23050 " {\n" 23051 " f();\n" 23052 " g();\n" 23053 " }\n" 23054 "};"); 23055 23056 // Lambdas that fit on a single line within an argument list are not forced 23057 // onto new lines. 23058 verifyFormat("SomeFunction([] {});"); 23059 verifyFormat("SomeFunction(0, [] {});"); 23060 verifyFormat("SomeFunction([] {}, 0);"); 23061 verifyFormat("SomeFunction(0, [] {}, 0);"); 23062 verifyFormat("SomeFunction([] { return 0; }, 0);"); 23063 verifyFormat("SomeFunction(a, [] { return 0; }, b);"); 23064 verifyFormat("SomeFunction([] { return 0; }, [] { return 0; });"); 23065 verifyFormat("SomeFunction([] { return 0; }, [] { return 0; }, b);"); 23066 verifyFormat("auto loooooooooooooooooooooooooooong =\n" 23067 " SomeFunction([] { return 0; }, [] { return 0; }, b);"); 23068 // Exceeded column limit. We need to break. 23069 verifyFormat("auto loooooooooooooooooooooooooooongName = SomeFunction(\n" 23070 " [] { return anotherLooooooooooonoooooooongName; }, [] { " 23071 "return 0; }, b);"); 23072 23073 // Multiple multi-line lambdas in the same parentheses change indentation 23074 // rules. These lambdas are always forced to start on new lines. 23075 verifyFormat("SomeFunction(\n" 23076 " []() {\n" 23077 " //\n" 23078 " },\n" 23079 " []() {\n" 23080 " //\n" 23081 " });"); 23082 23083 // A multi-line lambda passed as arg0 is always pushed to the next line. 23084 verifyFormat("SomeFunction(\n" 23085 " [this] {\n" 23086 " //\n" 23087 " },\n" 23088 " 1);"); 23089 23090 // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like 23091 // the arg0 case above. 23092 auto Style = getGoogleStyle(); 23093 Style.BinPackArguments = false; 23094 verifyFormat("SomeFunction(\n" 23095 " a,\n" 23096 " [this] {\n" 23097 " //\n" 23098 " },\n" 23099 " b);", 23100 Style); 23101 verifyFormat("SomeFunction(\n" 23102 " a,\n" 23103 " [this] {\n" 23104 " //\n" 23105 " },\n" 23106 " b);"); 23107 23108 // A lambda with a very long line forces arg0 to be pushed out irrespective of 23109 // the BinPackArguments value (as long as the code is wide enough). 23110 verifyFormat( 23111 "something->SomeFunction(\n" 23112 " a,\n" 23113 " [this] {\n" 23114 " " 23115 "D0000000000000000000000000000000000000000000000000000000000001();\n" 23116 " },\n" 23117 " b);"); 23118 23119 // A multi-line lambda is pulled up as long as the introducer fits on the 23120 // previous line and there are no further args. 23121 verifyFormat("function(1, [this, that] {\n" 23122 " //\n" 23123 "});"); 23124 verifyFormat("function([this, that] {\n" 23125 " //\n" 23126 "});"); 23127 // FIXME: this format is not ideal and we should consider forcing the first 23128 // arg onto its own line. 23129 verifyFormat("function(a, b, c, //\n" 23130 " d, [this, that] {\n" 23131 " //\n" 23132 " });"); 23133 23134 // Multiple lambdas are treated correctly even when there is a short arg0. 23135 verifyFormat("SomeFunction(\n" 23136 " 1,\n" 23137 " [this] {\n" 23138 " //\n" 23139 " },\n" 23140 " [this] {\n" 23141 " //\n" 23142 " },\n" 23143 " 1);"); 23144 23145 // More complex introducers. 23146 verifyFormat("return [i, args...] {};"); 23147 23148 // Not lambdas. 23149 verifyFormat("constexpr char hello[]{\"hello\"};"); 23150 verifyFormat("double &operator[](int i) { return 0; }\n" 23151 "int i;"); 23152 verifyFormat("std::unique_ptr<int[]> foo() {}"); 23153 verifyFormat("int i = a[a][a]->f();"); 23154 verifyFormat("int i = (*b)[a]->f();"); 23155 23156 // Other corner cases. 23157 verifyFormat("void f() {\n" 23158 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n" 23159 " );\n" 23160 "}"); 23161 verifyFormat("auto k = *[](int *j) { return j; }(&i);"); 23162 23163 // Lambdas created through weird macros. 23164 verifyFormat("void f() {\n" 23165 " MACRO((const AA &a) { return 1; });\n" 23166 " MACRO((AA &a) { return 1; });\n" 23167 "}"); 23168 23169 verifyFormat("if (blah_blah(whatever, whatever, [] {\n" 23170 " doo_dah();\n" 23171 " doo_dah();\n" 23172 " })) {\n" 23173 "}"); 23174 verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n" 23175 " doo_dah();\n" 23176 " doo_dah();\n" 23177 " })) {\n" 23178 "}"); 23179 verifyFormat("if CONSTEXPR (blah_blah(whatever, whatever, [] {\n" 23180 " doo_dah();\n" 23181 " doo_dah();\n" 23182 " })) {\n" 23183 "}"); 23184 verifyFormat("auto lambda = []() {\n" 23185 " int a = 2\n" 23186 "#if A\n" 23187 " + 2\n" 23188 "#endif\n" 23189 " ;\n" 23190 "};"); 23191 23192 // Lambdas with complex multiline introducers. 23193 verifyFormat( 23194 "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 23195 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n" 23196 " -> ::std::unordered_set<\n" 23197 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n" 23198 " //\n" 23199 " });"); 23200 23201 FormatStyle LLVMStyle = getLLVMStyleWithColumns(60); 23202 verifyFormat("very_long_function_name_yes_it_is_really_long(\n" 23203 " [](auto n) noexcept [[back_attr]]\n" 23204 " -> std::unordered_map<very_long_type_name_A,\n" 23205 " very_long_type_name_B> {\n" 23206 " really_do_something();\n" 23207 " });", 23208 LLVMStyle); 23209 verifyFormat("very_long_function_name_yes_it_is_really_long(\n" 23210 " [](auto n) constexpr\n" 23211 " -> std::unordered_map<very_long_type_name_A,\n" 23212 " very_long_type_name_B> {\n" 23213 " really_do_something();\n" 23214 " });", 23215 LLVMStyle); 23216 23217 FormatStyle DoNotMerge = getLLVMStyle(); 23218 DoNotMerge.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 23219 verifyFormat("auto c = []() {\n" 23220 " return b;\n" 23221 "};", 23222 "auto c = []() { return b; };", DoNotMerge); 23223 verifyFormat("auto c = []() {\n" 23224 "};", 23225 " auto c = []() {};", DoNotMerge); 23226 23227 FormatStyle MergeEmptyOnly = getLLVMStyle(); 23228 MergeEmptyOnly.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Empty; 23229 verifyFormat("auto c = []() {\n" 23230 " return b;\n" 23231 "};", 23232 "auto c = []() {\n" 23233 " return b;\n" 23234 " };", 23235 MergeEmptyOnly); 23236 verifyFormat("auto c = []() {};", 23237 "auto c = []() {\n" 23238 "};", 23239 MergeEmptyOnly); 23240 23241 FormatStyle MergeInline = getLLVMStyle(); 23242 MergeInline.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Inline; 23243 verifyFormat("auto c = []() {\n" 23244 " return b;\n" 23245 "};", 23246 "auto c = []() { return b; };", MergeInline); 23247 verifyFormat("function([]() { return b; })", MergeInline); 23248 verifyFormat("function([]() { return b; }, a)", MergeInline); 23249 verifyFormat("function(a, []() { return b; })", MergeInline); 23250 23251 // Check option "BraceWrapping.BeforeLambdaBody" and different state of 23252 // AllowShortLambdasOnASingleLine 23253 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle(); 23254 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 23255 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 23256 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 23257 FormatStyle::ShortLambdaStyle::SLS_None; 23258 verifyFormat("FctWithOneNestedLambdaInline_SLS_None(\n" 23259 " []()\n" 23260 " {\n" 23261 " return 17;\n" 23262 " });", 23263 LLVMWithBeforeLambdaBody); 23264 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_None(\n" 23265 " []()\n" 23266 " {\n" 23267 " });", 23268 LLVMWithBeforeLambdaBody); 23269 verifyFormat("auto fct_SLS_None = []()\n" 23270 "{\n" 23271 " return 17;\n" 23272 "};", 23273 LLVMWithBeforeLambdaBody); 23274 verifyFormat("TwoNestedLambdas_SLS_None(\n" 23275 " []()\n" 23276 " {\n" 23277 " return Call(\n" 23278 " []()\n" 23279 " {\n" 23280 " return 17;\n" 23281 " });\n" 23282 " });", 23283 LLVMWithBeforeLambdaBody); 23284 verifyFormat("void Fct() {\n" 23285 " return {[]()\n" 23286 " {\n" 23287 " return 17;\n" 23288 " }};\n" 23289 "}", 23290 LLVMWithBeforeLambdaBody); 23291 23292 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 23293 FormatStyle::ShortLambdaStyle::SLS_Empty; 23294 verifyFormat("FctWithOneNestedLambdaInline_SLS_Empty(\n" 23295 " []()\n" 23296 " {\n" 23297 " return 17;\n" 23298 " });", 23299 LLVMWithBeforeLambdaBody); 23300 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Empty([]() {});", 23301 LLVMWithBeforeLambdaBody); 23302 verifyFormat("FctWithOneNestedLambdaEmptyInsideAVeryVeryVeryVeryVeryVeryVeryL" 23303 "ongFunctionName_SLS_Empty(\n" 23304 " []() {});", 23305 LLVMWithBeforeLambdaBody); 23306 verifyFormat("FctWithMultipleParams_SLS_Empty(A, B,\n" 23307 " []()\n" 23308 " {\n" 23309 " return 17;\n" 23310 " });", 23311 LLVMWithBeforeLambdaBody); 23312 verifyFormat("auto fct_SLS_Empty = []()\n" 23313 "{\n" 23314 " return 17;\n" 23315 "};", 23316 LLVMWithBeforeLambdaBody); 23317 verifyFormat("TwoNestedLambdas_SLS_Empty(\n" 23318 " []()\n" 23319 " {\n" 23320 " return Call([]() {});\n" 23321 " });", 23322 LLVMWithBeforeLambdaBody); 23323 verifyFormat("TwoNestedLambdas_SLS_Empty(A,\n" 23324 " []()\n" 23325 " {\n" 23326 " return Call([]() {});\n" 23327 " });", 23328 LLVMWithBeforeLambdaBody); 23329 verifyFormat( 23330 "FctWithLongLineInLambda_SLS_Empty(\n" 23331 " []()\n" 23332 " {\n" 23333 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 23334 " AndShouldNotBeConsiderAsInline,\n" 23335 " LambdaBodyMustBeBreak);\n" 23336 " });", 23337 LLVMWithBeforeLambdaBody); 23338 23339 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 23340 FormatStyle::ShortLambdaStyle::SLS_Inline; 23341 verifyFormat("FctWithOneNestedLambdaInline_SLS_Inline([]() { return 17; });", 23342 LLVMWithBeforeLambdaBody); 23343 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Inline([]() {});", 23344 LLVMWithBeforeLambdaBody); 23345 verifyFormat("auto fct_SLS_Inline = []()\n" 23346 "{\n" 23347 " return 17;\n" 23348 "};", 23349 LLVMWithBeforeLambdaBody); 23350 verifyFormat("TwoNestedLambdas_SLS_Inline([]() { return Call([]() { return " 23351 "17; }); });", 23352 LLVMWithBeforeLambdaBody); 23353 verifyFormat( 23354 "FctWithLongLineInLambda_SLS_Inline(\n" 23355 " []()\n" 23356 " {\n" 23357 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 23358 " AndShouldNotBeConsiderAsInline,\n" 23359 " LambdaBodyMustBeBreak);\n" 23360 " });", 23361 LLVMWithBeforeLambdaBody); 23362 verifyFormat("FctWithMultipleParams_SLS_Inline(" 23363 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n" 23364 " []() { return 17; });", 23365 LLVMWithBeforeLambdaBody); 23366 verifyFormat( 23367 "FctWithMultipleParams_SLS_Inline(FirstParam, []() { return 17; });", 23368 LLVMWithBeforeLambdaBody); 23369 23370 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 23371 FormatStyle::ShortLambdaStyle::SLS_All; 23372 verifyFormat("FctWithOneNestedLambdaInline_SLS_All([]() { return 17; });", 23373 LLVMWithBeforeLambdaBody); 23374 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_All([]() {});", 23375 LLVMWithBeforeLambdaBody); 23376 verifyFormat("auto fct_SLS_All = []() { return 17; };", 23377 LLVMWithBeforeLambdaBody); 23378 verifyFormat("FctWithOneParam_SLS_All(\n" 23379 " []()\n" 23380 " {\n" 23381 " // A cool function...\n" 23382 " return 43;\n" 23383 " });", 23384 LLVMWithBeforeLambdaBody); 23385 verifyFormat("FctWithMultipleParams_SLS_All(" 23386 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n" 23387 " []() { return 17; });", 23388 LLVMWithBeforeLambdaBody); 23389 verifyFormat("FctWithMultipleParams_SLS_All(A, []() { return 17; });", 23390 LLVMWithBeforeLambdaBody); 23391 verifyFormat("FctWithMultipleParams_SLS_All(A, B, []() { return 17; });", 23392 LLVMWithBeforeLambdaBody); 23393 verifyFormat( 23394 "FctWithLongLineInLambda_SLS_All(\n" 23395 " []()\n" 23396 " {\n" 23397 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 23398 " AndShouldNotBeConsiderAsInline,\n" 23399 " LambdaBodyMustBeBreak);\n" 23400 " });", 23401 LLVMWithBeforeLambdaBody); 23402 verifyFormat( 23403 "auto fct_SLS_All = []()\n" 23404 "{\n" 23405 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 23406 " AndShouldNotBeConsiderAsInline,\n" 23407 " LambdaBodyMustBeBreak);\n" 23408 "};", 23409 LLVMWithBeforeLambdaBody); 23410 LLVMWithBeforeLambdaBody.BinPackParameters = FormatStyle::BPPS_OnePerLine; 23411 verifyFormat("FctAllOnSameLine_SLS_All([]() { return S; }, Fst, Second);", 23412 LLVMWithBeforeLambdaBody); 23413 verifyFormat( 23414 "FctWithLongLineInLambda_SLS_All([]() { return SomeValueNotSoLong; },\n" 23415 " FirstParam,\n" 23416 " SecondParam,\n" 23417 " ThirdParam,\n" 23418 " FourthParam);", 23419 LLVMWithBeforeLambdaBody); 23420 verifyFormat("FctWithLongLineInLambda_SLS_All(\n" 23421 " []() { return " 23422 "SomeValueVeryVeryVeryVeryVeryVeryVeryVeryVeryLong; },\n" 23423 " FirstParam,\n" 23424 " SecondParam,\n" 23425 " ThirdParam,\n" 23426 " FourthParam);", 23427 LLVMWithBeforeLambdaBody); 23428 verifyFormat( 23429 "FctWithLongLineInLambda_SLS_All(FirstParam,\n" 23430 " SecondParam,\n" 23431 " ThirdParam,\n" 23432 " FourthParam,\n" 23433 " []() { return SomeValueNotSoLong; });", 23434 LLVMWithBeforeLambdaBody); 23435 verifyFormat("FctWithLongLineInLambda_SLS_All(\n" 23436 " []()\n" 23437 " {\n" 23438 " return " 23439 "HereAVeryLongLineThatWillBeFormattedOnMultipleLineAndShouldNotB" 23440 "eConsiderAsInline;\n" 23441 " });", 23442 LLVMWithBeforeLambdaBody); 23443 verifyFormat( 23444 "FctWithLongLineInLambda_SLS_All(\n" 23445 " []()\n" 23446 " {\n" 23447 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 23448 " AndShouldNotBeConsiderAsInline,\n" 23449 " LambdaBodyMustBeBreak);\n" 23450 " });", 23451 LLVMWithBeforeLambdaBody); 23452 verifyFormat("FctWithTwoParams_SLS_All(\n" 23453 " []()\n" 23454 " {\n" 23455 " // A cool function...\n" 23456 " return 43;\n" 23457 " },\n" 23458 " 87);", 23459 LLVMWithBeforeLambdaBody); 23460 verifyFormat("FctWithTwoParams_SLS_All([]() { return 43; }, 87);", 23461 LLVMWithBeforeLambdaBody); 23462 verifyFormat( 23463 "FctWithTwoParams_SLS_All(\n" 23464 " 87, []() { return LongLineThatWillForceBothParamsToNewLine(); });", 23465 LLVMWithBeforeLambdaBody); 23466 verifyFormat( 23467 "FctWithTwoParams_SLS_All(\n" 23468 " 87,\n" 23469 " []()\n" 23470 " {\n" 23471 " return " 23472 "LongLineThatWillForceTheLambdaBodyToBeBrokenIntoMultipleLines();\n" 23473 " });", 23474 LLVMWithBeforeLambdaBody); 23475 verifyFormat("FctWithOneNestedLambdas_SLS_All([]() { return 17; });", 23476 LLVMWithBeforeLambdaBody); 23477 verifyFormat( 23478 "TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; }); });", 23479 LLVMWithBeforeLambdaBody); 23480 verifyFormat("TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; " 23481 "}); }, x);", 23482 LLVMWithBeforeLambdaBody); 23483 verifyFormat("TwoNestedLambdas_SLS_All(\n" 23484 " []()\n" 23485 " {\n" 23486 " // A cool function...\n" 23487 " return Call([]() { return 17; });\n" 23488 " });", 23489 LLVMWithBeforeLambdaBody); 23490 verifyFormat("TwoNestedLambdas_SLS_All(\n" 23491 " []()\n" 23492 " {\n" 23493 " return Call(\n" 23494 " []()\n" 23495 " {\n" 23496 " // A cool function...\n" 23497 " return 17;\n" 23498 " });\n" 23499 " });", 23500 LLVMWithBeforeLambdaBody); 23501 23502 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 23503 FormatStyle::ShortLambdaStyle::SLS_None; 23504 23505 verifyFormat("auto select = [this]() -> const Library::Object *\n" 23506 "{\n" 23507 " return MyAssignment::SelectFromList(this);\n" 23508 "};", 23509 LLVMWithBeforeLambdaBody); 23510 23511 verifyFormat("auto select = [this]() -> const Library::Object &\n" 23512 "{\n" 23513 " return MyAssignment::SelectFromList(this);\n" 23514 "};", 23515 LLVMWithBeforeLambdaBody); 23516 23517 verifyFormat("auto select = [this]() -> std::unique_ptr<Object>\n" 23518 "{\n" 23519 " return MyAssignment::SelectFromList(this);\n" 23520 "};", 23521 LLVMWithBeforeLambdaBody); 23522 23523 verifyFormat("namespace test {\n" 23524 "class Test {\n" 23525 "public:\n" 23526 " Test() = default;\n" 23527 "};\n" 23528 "} // namespace test", 23529 LLVMWithBeforeLambdaBody); 23530 23531 // Lambdas with different indentation styles. 23532 Style = getLLVMStyleWithColumns(60); 23533 verifyFormat("Result doSomething(Promise promise) {\n" 23534 " return promise.then(\n" 23535 " [this, obj = std::move(s)](int bar) mutable {\n" 23536 " return someObject.startAsyncAction().then(\n" 23537 " [this, &obj](Result result) mutable {\n" 23538 " result.processMore();\n" 23539 " });\n" 23540 " });\n" 23541 "}", 23542 Style); 23543 Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope; 23544 verifyFormat("Result doSomething(Promise promise) {\n" 23545 " return promise.then(\n" 23546 " [this, obj = std::move(s)](int bar) mutable {\n" 23547 " return obj.startAsyncAction().then(\n" 23548 " [this, &obj](Result result) mutable {\n" 23549 " result.processMore();\n" 23550 " });\n" 23551 " });\n" 23552 "}", 23553 Style); 23554 verifyFormat("Result doSomething(Promise promise) {\n" 23555 " return promise.then([this, obj = std::move(s)] {\n" 23556 " return obj.startAsyncAction().then(\n" 23557 " [this, &obj](Result result) mutable {\n" 23558 " result.processMore();\n" 23559 " });\n" 23560 " });\n" 23561 "}", 23562 Style); 23563 verifyFormat("void test() {\n" 23564 " ([]() -> auto {\n" 23565 " int b = 32;\n" 23566 " return 3;\n" 23567 " }).foo();\n" 23568 "}", 23569 Style); 23570 verifyFormat("void test() {\n" 23571 " []() -> auto {\n" 23572 " int b = 32;\n" 23573 " return 3;\n" 23574 " }\n" 23575 "}", 23576 Style); 23577 verifyFormat("void test() {\n" 23578 " std::sort(v.begin(), v.end(),\n" 23579 " [](const auto &foo, const auto &bar) {\n" 23580 " return foo.baz < bar.baz;\n" 23581 " });\n" 23582 "};", 23583 Style); 23584 verifyFormat("void test() {\n" 23585 " (\n" 23586 " []() -> auto {\n" 23587 " int b = 32;\n" 23588 " return 3;\n" 23589 " }, foo, bar)\n" 23590 " .foo();\n" 23591 "}", 23592 Style); 23593 verifyFormat("void test() {\n" 23594 " ([]() -> auto {\n" 23595 " int b = 32;\n" 23596 " return 3;\n" 23597 " })\n" 23598 " .foo()\n" 23599 " .bar();\n" 23600 "}", 23601 Style); 23602 verifyFormat("#define A \\\n" 23603 " [] { \\\n" 23604 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 23605 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 23606 " }", 23607 Style); 23608 verifyFormat("#define SORT(v) \\\n" 23609 " std::sort(v.begin(), v.end(), \\\n" 23610 " [](const auto &foo, const auto &bar) { \\\n" 23611 " return foo.baz < bar.baz; \\\n" 23612 " });", 23613 Style); 23614 verifyFormat("void foo() {\n" 23615 " aFunction(1, b(c(foo, bar, baz, [](d) {\n" 23616 " auto f = e(d);\n" 23617 " return f;\n" 23618 " })));\n" 23619 "}", 23620 Style); 23621 verifyFormat("void foo() {\n" 23622 " aFunction(1, b(c(foo, Bar{}, baz, [](d) -> Foo {\n" 23623 " auto f = e(foo, [&] {\n" 23624 " auto g = h();\n" 23625 " return g;\n" 23626 " }, qux, [&] -> Bar {\n" 23627 " auto i = j();\n" 23628 " return i;\n" 23629 " });\n" 23630 " return f;\n" 23631 " })));\n" 23632 "}", 23633 Style); 23634 verifyFormat("Namespace::Foo::Foo(LongClassName bar,\n" 23635 " AnotherLongClassName baz)\n" 23636 " : baz{baz}, func{[&] {\n" 23637 " auto qux = bar;\n" 23638 " return aFunkyFunctionCall(qux);\n" 23639 " }} {}", 23640 Style); 23641 verifyFormat("void foo() {\n" 23642 " class Foo {\n" 23643 " public:\n" 23644 " Foo()\n" 23645 " : qux{[](int quux) {\n" 23646 " auto tmp = quux;\n" 23647 " return tmp;\n" 23648 " }} {}\n" 23649 "\n" 23650 " private:\n" 23651 " std::function<void(int quux)> qux;\n" 23652 " };\n" 23653 "}", 23654 Style); 23655 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 23656 verifyFormat("Namespace::Foo::Foo(LongClassName bar,\n" 23657 " AnotherLongClassName baz) :\n" 23658 " baz{baz}, func{[&] {\n" 23659 " auto qux = bar;\n" 23660 " return aFunkyFunctionCall(qux);\n" 23661 " }} {}", 23662 Style); 23663 Style.PackConstructorInitializers = FormatStyle::PCIS_Never; 23664 verifyFormat("Namespace::Foo::Foo(LongClassName bar,\n" 23665 " AnotherLongClassName baz) :\n" 23666 " baz{baz},\n" 23667 " func{[&] {\n" 23668 " auto qux = bar;\n" 23669 " return aFunkyFunctionCall(qux);\n" 23670 " }} {}", 23671 Style); 23672 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 23673 // FIXME: The following test should pass, but fails at the time of writing. 23674 #if 0 23675 // As long as all the non-lambda arguments fit on a single line, AlwaysBreak 23676 // doesn't force an initial line break, even if lambdas span multiple lines. 23677 verifyFormat("void foo() {\n" 23678 " aFunction(\n" 23679 " [](d) -> Foo {\n" 23680 " auto f = e(d);\n" 23681 " return f;\n" 23682 " }, foo, Bar{}, [] {\n" 23683 " auto g = h();\n" 23684 " return g;\n" 23685 " }, baz);\n" 23686 "}", 23687 Style); 23688 #endif 23689 // A long non-lambda argument forces arguments to span multiple lines and thus 23690 // forces an initial line break when using AlwaysBreak. 23691 verifyFormat("void foo() {\n" 23692 " aFunction(\n" 23693 " 1,\n" 23694 " [](d) -> Foo {\n" 23695 " auto f = e(d);\n" 23696 " return f;\n" 23697 " }, foo, Bar{},\n" 23698 " [] {\n" 23699 " auto g = h();\n" 23700 " return g;\n" 23701 " }, bazzzzz,\n" 23702 " quuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuux);\n" 23703 "}", 23704 Style); 23705 Style.BinPackArguments = false; 23706 verifyFormat("void foo() {\n" 23707 " aFunction(\n" 23708 " 1,\n" 23709 " [](d) -> Foo {\n" 23710 " auto f = e(d);\n" 23711 " return f;\n" 23712 " },\n" 23713 " foo,\n" 23714 " Bar{},\n" 23715 " [] {\n" 23716 " auto g = h();\n" 23717 " return g;\n" 23718 " },\n" 23719 " bazzzzz,\n" 23720 " quuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuux);\n" 23721 "}", 23722 Style); 23723 Style.BinPackArguments = true; 23724 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 23725 Style.BraceWrapping.BeforeLambdaBody = true; 23726 verifyFormat("void foo() {\n" 23727 " aFunction(\n" 23728 " 1, b(c(foo, Bar{}, baz, [](d) -> Foo\n" 23729 " {\n" 23730 " auto f = e(\n" 23731 " [&]\n" 23732 " {\n" 23733 " auto g = h();\n" 23734 " return g;\n" 23735 " }, qux, [&] -> Bar\n" 23736 " {\n" 23737 " auto i = j();\n" 23738 " return i;\n" 23739 " });\n" 23740 " return f;\n" 23741 " })));\n" 23742 "}", 23743 Style); 23744 } 23745 23746 TEST_F(FormatTest, LambdaWithLineComments) { 23747 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle(); 23748 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 23749 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 23750 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 23751 FormatStyle::ShortLambdaStyle::SLS_All; 23752 23753 verifyFormat("auto k = []() { return; }", LLVMWithBeforeLambdaBody); 23754 verifyFormat("auto k = []() // comment\n" 23755 "{ return; }", 23756 LLVMWithBeforeLambdaBody); 23757 verifyFormat("auto k = []() /* comment */ { return; }", 23758 LLVMWithBeforeLambdaBody); 23759 verifyFormat("auto k = []() /* comment */ /* comment */ { return; }", 23760 LLVMWithBeforeLambdaBody); 23761 verifyFormat("auto k = []() // X\n" 23762 "{ return; }", 23763 LLVMWithBeforeLambdaBody); 23764 verifyFormat( 23765 "auto k = []() // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n" 23766 "{ return; }", 23767 LLVMWithBeforeLambdaBody); 23768 23769 LLVMWithBeforeLambdaBody.ColumnLimit = 0; 23770 23771 verifyFormat("foo([]()\n" 23772 " {\n" 23773 " bar(); //\n" 23774 " return 1; // comment\n" 23775 " }());", 23776 "foo([]() {\n" 23777 " bar(); //\n" 23778 " return 1; // comment\n" 23779 "}());", 23780 LLVMWithBeforeLambdaBody); 23781 verifyFormat("foo(\n" 23782 " 1, MACRO {\n" 23783 " baz();\n" 23784 " bar(); // comment\n" 23785 " },\n" 23786 " []() {});", 23787 "foo(\n" 23788 " 1, MACRO { baz(); bar(); // comment\n" 23789 " }, []() {}\n" 23790 ");", 23791 LLVMWithBeforeLambdaBody); 23792 } 23793 23794 TEST_F(FormatTest, EmptyLinesInLambdas) { 23795 verifyFormat("auto lambda = []() {\n" 23796 " x(); //\n" 23797 "};", 23798 "auto lambda = []() {\n" 23799 "\n" 23800 " x(); //\n" 23801 "\n" 23802 "};"); 23803 } 23804 23805 TEST_F(FormatTest, FormatsBlocks) { 23806 FormatStyle ShortBlocks = getLLVMStyle(); 23807 ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 23808 verifyFormat("int (^Block)(int, int);", ShortBlocks); 23809 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks); 23810 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks); 23811 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks); 23812 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks); 23813 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks); 23814 23815 verifyFormat("foo(^{ bar(); });", ShortBlocks); 23816 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks); 23817 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks); 23818 23819 verifyFormat("[operation setCompletionBlock:^{\n" 23820 " [self onOperationDone];\n" 23821 "}];"); 23822 verifyFormat("int i = {[operation setCompletionBlock:^{\n" 23823 " [self onOperationDone];\n" 23824 "}]};"); 23825 verifyFormat("[operation setCompletionBlock:^(int *i) {\n" 23826 " f();\n" 23827 "}];"); 23828 verifyFormat("int a = [operation block:^int(int *i) {\n" 23829 " return 1;\n" 23830 "}];"); 23831 verifyFormat("[myObject doSomethingWith:arg1\n" 23832 " aaa:^int(int *a) {\n" 23833 " return 1;\n" 23834 " }\n" 23835 " bbb:f(a * bbbbbbbb)];"); 23836 23837 verifyFormat("[operation setCompletionBlock:^{\n" 23838 " [self.delegate newDataAvailable];\n" 23839 "}];", 23840 getLLVMStyleWithColumns(60)); 23841 verifyFormat("dispatch_async(_fileIOQueue, ^{\n" 23842 " NSString *path = [self sessionFilePath];\n" 23843 " if (path) {\n" 23844 " // ...\n" 23845 " }\n" 23846 "});"); 23847 verifyFormat("[[SessionService sharedService]\n" 23848 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 23849 " if (window) {\n" 23850 " [self windowDidLoad:window];\n" 23851 " } else {\n" 23852 " [self errorLoadingWindow];\n" 23853 " }\n" 23854 " }];"); 23855 verifyFormat("void (^largeBlock)(void) = ^{\n" 23856 " // ...\n" 23857 "};", 23858 getLLVMStyleWithColumns(40)); 23859 verifyFormat("[[SessionService sharedService]\n" 23860 " loadWindowWithCompletionBlock: //\n" 23861 " ^(SessionWindow *window) {\n" 23862 " if (window) {\n" 23863 " [self windowDidLoad:window];\n" 23864 " } else {\n" 23865 " [self errorLoadingWindow];\n" 23866 " }\n" 23867 " }];", 23868 getLLVMStyleWithColumns(60)); 23869 verifyFormat("[myObject doSomethingWith:arg1\n" 23870 " firstBlock:^(Foo *a) {\n" 23871 " // ...\n" 23872 " int i;\n" 23873 " }\n" 23874 " secondBlock:^(Bar *b) {\n" 23875 " // ...\n" 23876 " int i;\n" 23877 " }\n" 23878 " thirdBlock:^Foo(Bar *b) {\n" 23879 " // ...\n" 23880 " int i;\n" 23881 " }];"); 23882 verifyFormat("[myObject doSomethingWith:arg1\n" 23883 " firstBlock:-1\n" 23884 " secondBlock:^(Bar *b) {\n" 23885 " // ...\n" 23886 " int i;\n" 23887 " }];"); 23888 23889 verifyFormat("f(^{\n" 23890 " @autoreleasepool {\n" 23891 " if (a) {\n" 23892 " g();\n" 23893 " }\n" 23894 " }\n" 23895 "});"); 23896 verifyFormat("Block b = ^int *(A *a, B *b) {\n" 23897 "};"); 23898 verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n" 23899 "};"); 23900 23901 FormatStyle FourIndent = getLLVMStyle(); 23902 FourIndent.ObjCBlockIndentWidth = 4; 23903 verifyFormat("[operation setCompletionBlock:^{\n" 23904 " [self onOperationDone];\n" 23905 "}];", 23906 FourIndent); 23907 } 23908 23909 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) { 23910 FormatStyle ZeroColumn = getLLVMStyleWithColumns(0); 23911 23912 verifyFormat("[[SessionService sharedService] " 23913 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 23914 " if (window) {\n" 23915 " [self windowDidLoad:window];\n" 23916 " } else {\n" 23917 " [self errorLoadingWindow];\n" 23918 " }\n" 23919 "}];", 23920 ZeroColumn); 23921 verifyFormat("[[SessionService sharedService]\n" 23922 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 23923 " if (window) {\n" 23924 " [self windowDidLoad:window];\n" 23925 " } else {\n" 23926 " [self errorLoadingWindow];\n" 23927 " }\n" 23928 " }];", 23929 "[[SessionService sharedService]\n" 23930 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 23931 " if (window) {\n" 23932 " [self windowDidLoad:window];\n" 23933 " } else {\n" 23934 " [self errorLoadingWindow];\n" 23935 " }\n" 23936 "}];", 23937 ZeroColumn); 23938 verifyFormat("[myObject doSomethingWith:arg1\n" 23939 " firstBlock:^(Foo *a) {\n" 23940 " // ...\n" 23941 " int i;\n" 23942 " }\n" 23943 " secondBlock:^(Bar *b) {\n" 23944 " // ...\n" 23945 " int i;\n" 23946 " }\n" 23947 " thirdBlock:^Foo(Bar *b) {\n" 23948 " // ...\n" 23949 " int i;\n" 23950 " }];", 23951 ZeroColumn); 23952 verifyFormat("f(^{\n" 23953 " @autoreleasepool {\n" 23954 " if (a) {\n" 23955 " g();\n" 23956 " }\n" 23957 " }\n" 23958 "});", 23959 ZeroColumn); 23960 verifyFormat("void (^largeBlock)(void) = ^{\n" 23961 " // ...\n" 23962 "};", 23963 ZeroColumn); 23964 23965 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 23966 verifyFormat("void (^largeBlock)(void) = ^{ int i; };", 23967 "void (^largeBlock)(void) = ^{ int i; };", ZeroColumn); 23968 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never; 23969 verifyFormat("void (^largeBlock)(void) = ^{\n" 23970 " int i;\n" 23971 "};", 23972 "void (^largeBlock)(void) = ^{ int i; };", ZeroColumn); 23973 } 23974 23975 TEST_F(FormatTest, SupportsCRLF) { 23976 verifyFormat("int a;\r\n" 23977 "int b;\r\n" 23978 "int c;", 23979 "int a;\r\n" 23980 " int b;\r\n" 23981 " int c;"); 23982 verifyFormat("int a;\r\n" 23983 "int b;\r\n" 23984 "int c;\r\n", 23985 "int a;\r\n" 23986 " int b;\n" 23987 " int c;\r\n"); 23988 verifyFormat("int a;\n" 23989 "int b;\n" 23990 "int c;", 23991 "int a;\r\n" 23992 " int b;\n" 23993 " int c;"); 23994 // FIXME: unstable test case 23995 EXPECT_EQ("\"aaaaaaa \"\r\n" 23996 "\"bbbbbbb\";\r\n", 23997 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10))); 23998 verifyFormat("#define A \\\r\n" 23999 " b; \\\r\n" 24000 " c; \\\r\n" 24001 " d;", 24002 "#define A \\\r\n" 24003 " b; \\\r\n" 24004 " c; d; ", 24005 getGoogleStyle()); 24006 24007 verifyNoChange("/*\r\n" 24008 "multi line block comments\r\n" 24009 "should not introduce\r\n" 24010 "an extra carriage return\r\n" 24011 "*/"); 24012 verifyFormat("/*\r\n" 24013 "\r\n" 24014 "*/", 24015 "/*\r\n" 24016 " \r\r\r\n" 24017 "*/"); 24018 24019 FormatStyle style = getLLVMStyle(); 24020 24021 EXPECT_EQ(style.LineEnding, FormatStyle::LE_DeriveLF); 24022 verifyFormat("union FooBarBazQux {\n" 24023 " int foo;\n" 24024 " int bar;\n" 24025 " int baz;\n" 24026 "};", 24027 "union FooBarBazQux {\r\n" 24028 " int foo;\n" 24029 " int bar;\r\n" 24030 " int baz;\n" 24031 "};", 24032 style); 24033 style.LineEnding = FormatStyle::LE_DeriveCRLF; 24034 verifyFormat("union FooBarBazQux {\r\n" 24035 " int foo;\r\n" 24036 " int bar;\r\n" 24037 " int baz;\r\n" 24038 "};", 24039 "union FooBarBazQux {\r\n" 24040 " int foo;\n" 24041 " int bar;\r\n" 24042 " int baz;\n" 24043 "};", 24044 style); 24045 24046 style.LineEnding = FormatStyle::LE_LF; 24047 verifyFormat("union FooBarBazQux {\n" 24048 " int foo;\n" 24049 " int bar;\n" 24050 " int baz;\n" 24051 " int qux;\n" 24052 "};", 24053 "union FooBarBazQux {\r\n" 24054 " int foo;\n" 24055 " int bar;\r\n" 24056 " int baz;\n" 24057 " int qux;\r\n" 24058 "};", 24059 style); 24060 style.LineEnding = FormatStyle::LE_CRLF; 24061 verifyFormat("union FooBarBazQux {\r\n" 24062 " int foo;\r\n" 24063 " int bar;\r\n" 24064 " int baz;\r\n" 24065 " int qux;\r\n" 24066 "};", 24067 "union FooBarBazQux {\r\n" 24068 " int foo;\n" 24069 " int bar;\r\n" 24070 " int baz;\n" 24071 " int qux;\n" 24072 "};", 24073 style); 24074 24075 style.LineEnding = FormatStyle::LE_DeriveLF; 24076 verifyFormat("union FooBarBazQux {\r\n" 24077 " int foo;\r\n" 24078 " int bar;\r\n" 24079 " int baz;\r\n" 24080 " int qux;\r\n" 24081 "};", 24082 "union FooBarBazQux {\r\n" 24083 " int foo;\n" 24084 " int bar;\r\n" 24085 " int baz;\n" 24086 " int qux;\r\n" 24087 "};", 24088 style); 24089 style.LineEnding = FormatStyle::LE_DeriveCRLF; 24090 verifyFormat("union FooBarBazQux {\n" 24091 " int foo;\n" 24092 " int bar;\n" 24093 " int baz;\n" 24094 " int qux;\n" 24095 "};", 24096 "union FooBarBazQux {\r\n" 24097 " int foo;\n" 24098 " int bar;\r\n" 24099 " int baz;\n" 24100 " int qux;\n" 24101 "};", 24102 style); 24103 } 24104 24105 TEST_F(FormatTest, MunchSemicolonAfterBlocks) { 24106 verifyFormat("MY_CLASS(C) {\n" 24107 " int i;\n" 24108 " int j;\n" 24109 "};"); 24110 } 24111 24112 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) { 24113 FormatStyle TwoIndent = getLLVMStyleWithColumns(15); 24114 TwoIndent.ContinuationIndentWidth = 2; 24115 24116 verifyFormat("int i =\n" 24117 " longFunction(\n" 24118 " arg);", 24119 "int i = longFunction(arg);", TwoIndent); 24120 24121 FormatStyle SixIndent = getLLVMStyleWithColumns(20); 24122 SixIndent.ContinuationIndentWidth = 6; 24123 24124 verifyFormat("int i =\n" 24125 " longFunction(\n" 24126 " arg);", 24127 "int i = longFunction(arg);", SixIndent); 24128 } 24129 24130 TEST_F(FormatTest, WrappedClosingParenthesisIndent) { 24131 FormatStyle Style = getLLVMStyle(); 24132 verifyFormat("int Foo::getter(\n" 24133 " //\n" 24134 ") const {\n" 24135 " return foo;\n" 24136 "}", 24137 Style); 24138 verifyFormat("void Foo::setter(\n" 24139 " //\n" 24140 ") {\n" 24141 " foo = 1;\n" 24142 "}", 24143 Style); 24144 } 24145 24146 TEST_F(FormatTest, SpacesInAngles) { 24147 FormatStyle Spaces = getLLVMStyle(); 24148 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 24149 24150 verifyFormat("vector< ::std::string > x1;", Spaces); 24151 verifyFormat("Foo< int, Bar > x2;", Spaces); 24152 verifyFormat("Foo< ::int, ::Bar > x3;", Spaces); 24153 24154 verifyFormat("static_cast< int >(arg);", Spaces); 24155 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces); 24156 verifyFormat("f< int, float >();", Spaces); 24157 verifyFormat("template <> g() {}", Spaces); 24158 verifyFormat("template < std::vector< int > > f() {}", Spaces); 24159 verifyFormat("std::function< void(int, int) > fct;", Spaces); 24160 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }", 24161 Spaces); 24162 24163 Spaces.Standard = FormatStyle::LS_Cpp03; 24164 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 24165 verifyFormat("A< A< int > >();", Spaces); 24166 24167 Spaces.SpacesInAngles = FormatStyle::SIAS_Never; 24168 verifyFormat("A<A<int> >();", Spaces); 24169 24170 Spaces.SpacesInAngles = FormatStyle::SIAS_Leave; 24171 verifyFormat("vector< ::std::string> x4;", "vector<::std::string> x4;", 24172 Spaces); 24173 verifyFormat("vector< ::std::string > x4;", "vector<::std::string > x4;", 24174 Spaces); 24175 24176 verifyFormat("A<A<int> >();", Spaces); 24177 verifyFormat("A<A<int> >();", "A<A<int>>();", Spaces); 24178 verifyFormat("A< A< int > >();", Spaces); 24179 24180 Spaces.Standard = FormatStyle::LS_Cpp11; 24181 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 24182 verifyFormat("A< A< int > >();", Spaces); 24183 24184 Spaces.SpacesInAngles = FormatStyle::SIAS_Never; 24185 verifyFormat("vector<::std::string> x4;", Spaces); 24186 verifyFormat("vector<int> x5;", Spaces); 24187 verifyFormat("Foo<int, Bar> x6;", Spaces); 24188 verifyFormat("Foo<::int, ::Bar> x7;", Spaces); 24189 24190 verifyFormat("A<A<int>>();", Spaces); 24191 24192 Spaces.SpacesInAngles = FormatStyle::SIAS_Leave; 24193 verifyFormat("vector<::std::string> x4;", Spaces); 24194 verifyFormat("vector< ::std::string > x4;", Spaces); 24195 verifyFormat("vector<int> x5;", Spaces); 24196 verifyFormat("vector< int > x5;", Spaces); 24197 verifyFormat("Foo<int, Bar> x6;", Spaces); 24198 verifyFormat("Foo< int, Bar > x6;", Spaces); 24199 verifyFormat("Foo<::int, ::Bar> x7;", Spaces); 24200 verifyFormat("Foo< ::int, ::Bar > x7;", Spaces); 24201 24202 verifyFormat("A<A<int>>();", Spaces); 24203 verifyFormat("A< A< int > >();", Spaces); 24204 verifyFormat("A<A<int > >();", Spaces); 24205 verifyFormat("A< A< int>>();", Spaces); 24206 24207 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 24208 verifyFormat("// clang-format off\n" 24209 "foo<<<1, 1>>>();\n" 24210 "// clang-format on", 24211 Spaces); 24212 verifyFormat("// clang-format off\n" 24213 "foo< < <1, 1> > >();\n" 24214 "// clang-format on", 24215 Spaces); 24216 } 24217 24218 TEST_F(FormatTest, SpaceAfterTemplateKeyword) { 24219 FormatStyle Style = getLLVMStyle(); 24220 Style.SpaceAfterTemplateKeyword = false; 24221 verifyFormat("template<int> void foo();", Style); 24222 } 24223 24224 TEST_F(FormatTest, TripleAngleBrackets) { 24225 verifyFormat("f<<<1, 1>>>();"); 24226 verifyFormat("f<<<1, 1, 1, s>>>();"); 24227 verifyFormat("f<<<a, b, c, d>>>();"); 24228 verifyFormat("f<<<1, 1>>>();", "f <<< 1, 1 >>> ();"); 24229 verifyFormat("f<param><<<1, 1>>>();"); 24230 verifyFormat("f<1><<<1, 1>>>();"); 24231 verifyFormat("f<param><<<1, 1>>>();", "f< param > <<< 1, 1 >>> ();"); 24232 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 24233 "aaaaaaaaaaa<<<\n 1, 1>>>();"); 24234 verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n" 24235 " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();"); 24236 } 24237 24238 TEST_F(FormatTest, MergeLessLessAtEnd) { 24239 verifyFormat("<<"); 24240 verifyFormat("< < <", "\\\n<<<"); 24241 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 24242 "aaallvm::outs() <<"); 24243 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 24244 "aaaallvm::outs()\n <<"); 24245 } 24246 24247 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) { 24248 std::string code = "#if A\n" 24249 "#if B\n" 24250 "a.\n" 24251 "#endif\n" 24252 " a = 1;\n" 24253 "#else\n" 24254 "#endif\n" 24255 "#if C\n" 24256 "#else\n" 24257 "#endif\n"; 24258 verifyFormat(code); 24259 } 24260 24261 TEST_F(FormatTest, HandleConflictMarkers) { 24262 // Git/SVN conflict markers. 24263 verifyFormat("int a;\n" 24264 "void f() {\n" 24265 " callme(some(parameter1,\n" 24266 "<<<<<<< text by the vcs\n" 24267 " parameter2),\n" 24268 "||||||| text by the vcs\n" 24269 " parameter2),\n" 24270 " parameter3,\n" 24271 "======= text by the vcs\n" 24272 " parameter2, parameter3),\n" 24273 ">>>>>>> text by the vcs\n" 24274 " otherparameter);", 24275 "int a;\n" 24276 "void f() {\n" 24277 " callme(some(parameter1,\n" 24278 "<<<<<<< text by the vcs\n" 24279 " parameter2),\n" 24280 "||||||| text by the vcs\n" 24281 " parameter2),\n" 24282 " parameter3,\n" 24283 "======= text by the vcs\n" 24284 " parameter2,\n" 24285 " parameter3),\n" 24286 ">>>>>>> text by the vcs\n" 24287 " otherparameter);"); 24288 24289 // Perforce markers. 24290 verifyFormat("void f() {\n" 24291 " function(\n" 24292 ">>>> text by the vcs\n" 24293 " parameter,\n" 24294 "==== text by the vcs\n" 24295 " parameter,\n" 24296 "==== text by the vcs\n" 24297 " parameter,\n" 24298 "<<<< text by the vcs\n" 24299 " parameter);", 24300 "void f() {\n" 24301 " function(\n" 24302 ">>>> text by the vcs\n" 24303 " parameter,\n" 24304 "==== text by the vcs\n" 24305 " parameter,\n" 24306 "==== text by the vcs\n" 24307 " parameter,\n" 24308 "<<<< text by the vcs\n" 24309 " parameter);"); 24310 24311 verifyNoChange("<<<<<<<\n" 24312 "|||||||\n" 24313 "=======\n" 24314 ">>>>>>>"); 24315 24316 verifyNoChange("<<<<<<<\n" 24317 "|||||||\n" 24318 "int i;\n" 24319 "=======\n" 24320 ">>>>>>>"); 24321 24322 // FIXME: Handle parsing of macros around conflict markers correctly: 24323 verifyFormat("#define Macro \\\n" 24324 "<<<<<<<\n" 24325 "Something \\\n" 24326 "|||||||\n" 24327 "Else \\\n" 24328 "=======\n" 24329 "Other \\\n" 24330 ">>>>>>>\n" 24331 " End int i;", 24332 "#define Macro \\\n" 24333 "<<<<<<<\n" 24334 " Something \\\n" 24335 "|||||||\n" 24336 " Else \\\n" 24337 "=======\n" 24338 " Other \\\n" 24339 ">>>>>>>\n" 24340 " End\n" 24341 "int i;"); 24342 24343 verifyFormat(R"(==== 24344 #ifdef A 24345 a 24346 #else 24347 b 24348 #endif 24349 )"); 24350 } 24351 24352 TEST_F(FormatTest, DisableRegions) { 24353 verifyFormat("int i;\n" 24354 "// clang-format off\n" 24355 " int j;\n" 24356 "// clang-format on\n" 24357 "int k;", 24358 " int i;\n" 24359 " // clang-format off\n" 24360 " int j;\n" 24361 " // clang-format on\n" 24362 " int k;"); 24363 verifyFormat("int i;\n" 24364 "/* clang-format off */\n" 24365 " int j;\n" 24366 "/* clang-format on */\n" 24367 "int k;", 24368 " int i;\n" 24369 " /* clang-format off */\n" 24370 " int j;\n" 24371 " /* clang-format on */\n" 24372 " int k;"); 24373 24374 // Don't reflow comments within disabled regions. 24375 verifyFormat("// clang-format off\n" 24376 "// long long long long long long line\n" 24377 "/* clang-format on */\n" 24378 "/* long long long\n" 24379 " * long long long\n" 24380 " * line */\n" 24381 "int i;\n" 24382 "/* clang-format off */\n" 24383 "/* long long long long long long line */", 24384 "// clang-format off\n" 24385 "// long long long long long long line\n" 24386 "/* clang-format on */\n" 24387 "/* long long long long long long line */\n" 24388 "int i;\n" 24389 "/* clang-format off */\n" 24390 "/* long long long long long long line */", 24391 getLLVMStyleWithColumns(20)); 24392 24393 verifyFormat("int *i;\n" 24394 "// clang-format off:\n" 24395 "int* j;\n" 24396 "// clang-format on: 1\n" 24397 "int *k;", 24398 "int* i;\n" 24399 "// clang-format off:\n" 24400 "int* j;\n" 24401 "// clang-format on: 1\n" 24402 "int* k;"); 24403 24404 verifyFormat("int *i;\n" 24405 "// clang-format off:0\n" 24406 "int* j;\n" 24407 "// clang-format only\n" 24408 "int* k;", 24409 "int* i;\n" 24410 "// clang-format off:0\n" 24411 "int* j;\n" 24412 "// clang-format only\n" 24413 "int* k;"); 24414 24415 verifyNoChange("// clang-format off\n" 24416 "#if 0\n" 24417 " #if SHOULD_STAY_INDENTED\n" 24418 " #endif\n" 24419 "#endif\n" 24420 "// clang-format on"); 24421 } 24422 24423 TEST_F(FormatTest, DoNotCrashOnInvalidInput) { 24424 format("? ) ="); 24425 verifyNoCrash("#define a\\\n /**/}"); 24426 verifyNoCrash(" tst %o5 ! are we doing the gray case?\n" 24427 "LY52: ! [internal]"); 24428 } 24429 24430 TEST_F(FormatTest, FormatsTableGenCode) { 24431 FormatStyle Style = getLLVMStyle(); 24432 Style.Language = FormatStyle::LK_TableGen; 24433 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style); 24434 } 24435 24436 TEST_F(FormatTest, ArrayOfTemplates) { 24437 verifyFormat("auto a = new unique_ptr<int>[10];", 24438 "auto a = new unique_ptr<int > [ 10];"); 24439 24440 FormatStyle Spaces = getLLVMStyle(); 24441 Spaces.SpacesInSquareBrackets = true; 24442 verifyFormat("auto a = new unique_ptr<int>[ 10 ];", 24443 "auto a = new unique_ptr<int > [10];", Spaces); 24444 } 24445 24446 TEST_F(FormatTest, ArrayAsTemplateType) { 24447 verifyFormat("auto a = unique_ptr<Foo<Bar>[10]>;", 24448 "auto a = unique_ptr < Foo < Bar>[ 10]> ;"); 24449 24450 FormatStyle Spaces = getLLVMStyle(); 24451 Spaces.SpacesInSquareBrackets = true; 24452 verifyFormat("auto a = unique_ptr<Foo<Bar>[ 10 ]>;", 24453 "auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces); 24454 } 24455 24456 TEST_F(FormatTest, NoSpaceAfterSuper) { verifyFormat("__super::FooBar();"); } 24457 24458 TEST_F(FormatTest, FormatSortsUsingDeclarations) { 24459 verifyFormat("using std::cin;\n" 24460 "using std::cout;", 24461 "using std::cout;\n" 24462 "using std::cin;", 24463 getGoogleStyle()); 24464 } 24465 24466 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) { 24467 FormatStyle Style = getLLVMStyle(); 24468 Style.Standard = FormatStyle::LS_Cpp03; 24469 // cpp03 recognize this string as identifier u8 and literal character 'a' 24470 verifyFormat("auto c = u8 'a';", "auto c = u8'a';", Style); 24471 } 24472 24473 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) { 24474 // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers 24475 // all modes, including C++11, C++14 and C++17 24476 verifyFormat("auto c = u8'a';"); 24477 } 24478 24479 TEST_F(FormatTest, DoNotFormatLikelyXml) { 24480 verifyGoogleFormat("<!-- ;> -->"); 24481 verifyNoChange(" <!-- >; -->", getGoogleStyle()); 24482 } 24483 24484 TEST_F(FormatTest, StructuredBindings) { 24485 // Structured bindings is a C++17 feature. 24486 // all modes, including C++11, C++14 and C++17 24487 verifyFormat("auto [a, b] = f();"); 24488 verifyFormat("auto [a, b] = f();", "auto[a, b] = f();"); 24489 verifyFormat("const auto [a, b] = f();", "const auto[a, b] = f();"); 24490 verifyFormat("auto const [a, b] = f();", "auto const[a, b] = f();"); 24491 verifyFormat("auto const volatile [a, b] = f();", 24492 "auto const volatile[a, b] = f();"); 24493 verifyFormat("auto [a, b, c] = f();", "auto [ a , b,c ] = f();"); 24494 verifyFormat("auto &[a, b, c] = f();", "auto &[ a , b,c ] = f();"); 24495 verifyFormat("auto &&[a, b, c] = f();", "auto &&[ a , b,c ] = f();"); 24496 verifyFormat("auto const &[a, b] = f();", "auto const&[a, b] = f();"); 24497 verifyFormat("auto const volatile &&[a, b] = f();", 24498 "auto const volatile &&[a, b] = f();"); 24499 verifyFormat("auto const &&[a, b] = f();", "auto const && [a, b] = f();"); 24500 verifyFormat("const auto &[a, b] = f();", "const auto & [a, b] = f();"); 24501 verifyFormat("const auto volatile &&[a, b] = f();", 24502 "const auto volatile &&[a, b] = f();"); 24503 verifyFormat("volatile const auto &&[a, b] = f();", 24504 "volatile const auto &&[a, b] = f();"); 24505 verifyFormat("const auto &&[a, b] = f();", "const auto && [a, b] = f();"); 24506 24507 // Make sure we don't mistake structured bindings for lambdas. 24508 FormatStyle PointerMiddle = getLLVMStyle(); 24509 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 24510 verifyGoogleFormat("auto [a1, b]{A * i};"); 24511 verifyFormat("auto [a2, b]{A * i};"); 24512 verifyFormat("auto [a3, b]{A * i};", PointerMiddle); 24513 verifyGoogleFormat("auto const [a1, b]{A * i};"); 24514 verifyFormat("auto const [a2, b]{A * i};"); 24515 verifyFormat("auto const [a3, b]{A * i};", PointerMiddle); 24516 verifyGoogleFormat("auto const& [a1, b]{A * i};"); 24517 verifyFormat("auto const &[a2, b]{A * i};"); 24518 verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle); 24519 verifyGoogleFormat("auto const&& [a1, b]{A * i};"); 24520 verifyFormat("auto const &&[a2, b]{A * i};"); 24521 verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle); 24522 24523 verifyFormat("for (const auto &&[a, b] : some_range) {\n}", 24524 "for (const auto && [a, b] : some_range) {\n}"); 24525 verifyFormat("for (const auto &[a, b] : some_range) {\n}", 24526 "for (const auto & [a, b] : some_range) {\n}"); 24527 verifyFormat("for (const auto [a, b] : some_range) {\n}", 24528 "for (const auto[a, b] : some_range) {\n}"); 24529 verifyFormat("auto [x, y](expr);", "auto[x,y] (expr);"); 24530 verifyFormat("auto &[x, y](expr);", "auto & [x,y] (expr);"); 24531 verifyFormat("auto &&[x, y](expr);", "auto && [x,y] (expr);"); 24532 verifyFormat("auto const &[x, y](expr);", "auto const & [x,y] (expr);"); 24533 verifyFormat("auto const &&[x, y](expr);", "auto const && [x,y] (expr);"); 24534 verifyFormat("auto [x, y]{expr};", "auto[x,y] {expr};"); 24535 verifyFormat("auto const &[x, y]{expr};", "auto const & [x,y] {expr};"); 24536 verifyFormat("auto const &&[x, y]{expr};", "auto const && [x,y] {expr};"); 24537 24538 FormatStyle Spaces = getLLVMStyle(); 24539 Spaces.SpacesInSquareBrackets = true; 24540 verifyFormat("auto [ a, b ] = f();", Spaces); 24541 verifyFormat("auto &&[ a, b ] = f();", Spaces); 24542 verifyFormat("auto &[ a, b ] = f();", Spaces); 24543 verifyFormat("auto const &&[ a, b ] = f();", Spaces); 24544 verifyFormat("auto const &[ a, b ] = f();", Spaces); 24545 } 24546 24547 TEST_F(FormatTest, FileAndCode) { 24548 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", "")); 24549 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", "")); 24550 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", "")); 24551 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "")); 24552 EXPECT_EQ(FormatStyle::LK_ObjC, 24553 guessLanguage("foo.h", "@interface Foo\n@end")); 24554 EXPECT_EQ( 24555 FormatStyle::LK_ObjC, 24556 guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }")); 24557 EXPECT_EQ(FormatStyle::LK_ObjC, 24558 guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))")); 24559 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;")); 24560 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", "")); 24561 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo", "@interface Foo\n@end")); 24562 EXPECT_EQ(FormatStyle::LK_ObjC, 24563 guessLanguage("foo.h", "int DoStuff(CGRect rect);")); 24564 EXPECT_EQ(FormatStyle::LK_ObjC, 24565 guessLanguage( 24566 "foo.h", "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));")); 24567 EXPECT_EQ( 24568 FormatStyle::LK_Cpp, 24569 guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;")); 24570 // Only one of the two preprocessor regions has ObjC-like code. 24571 EXPECT_EQ(FormatStyle::LK_ObjC, 24572 guessLanguage("foo.h", "#if A\n" 24573 "#define B() C\n" 24574 "#else\n" 24575 "#define B() [NSString a:@\"\"]\n" 24576 "#endif")); 24577 } 24578 24579 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) { 24580 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];")); 24581 EXPECT_EQ(FormatStyle::LK_ObjC, 24582 guessLanguage("foo.h", "array[[calculator getIndex]];")); 24583 EXPECT_EQ(FormatStyle::LK_Cpp, 24584 guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];")); 24585 EXPECT_EQ( 24586 FormatStyle::LK_Cpp, 24587 guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];")); 24588 EXPECT_EQ(FormatStyle::LK_ObjC, 24589 guessLanguage("foo.h", "[[noreturn foo] bar];")); 24590 EXPECT_EQ(FormatStyle::LK_Cpp, 24591 guessLanguage("foo.h", "[[clang::fallthrough]];")); 24592 EXPECT_EQ(FormatStyle::LK_ObjC, 24593 guessLanguage("foo.h", "[[clang:fallthrough] foo];")); 24594 EXPECT_EQ(FormatStyle::LK_Cpp, 24595 guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];")); 24596 EXPECT_EQ(FormatStyle::LK_Cpp, 24597 guessLanguage("foo.h", "[[using clang: fallthrough]];")); 24598 EXPECT_EQ(FormatStyle::LK_ObjC, 24599 guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];")); 24600 EXPECT_EQ(FormatStyle::LK_Cpp, 24601 guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];")); 24602 EXPECT_EQ( 24603 FormatStyle::LK_Cpp, 24604 guessLanguage("foo.h", "for (auto &&[endpoint, stream] : streams_)")); 24605 EXPECT_EQ( 24606 FormatStyle::LK_Cpp, 24607 guessLanguage("foo.h", 24608 "[[clang::callable_when(\"unconsumed\", \"unknown\")]]")); 24609 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]")); 24610 } 24611 24612 TEST_F(FormatTest, GuessLanguageWithCaret) { 24613 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);")); 24614 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);")); 24615 EXPECT_EQ(FormatStyle::LK_ObjC, 24616 guessLanguage("foo.h", "int(^)(char, float);")); 24617 EXPECT_EQ(FormatStyle::LK_ObjC, 24618 guessLanguage("foo.h", "int(^foo)(char, float);")); 24619 EXPECT_EQ(FormatStyle::LK_ObjC, 24620 guessLanguage("foo.h", "int(^foo[10])(char, float);")); 24621 EXPECT_EQ(FormatStyle::LK_ObjC, 24622 guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);")); 24623 EXPECT_EQ( 24624 FormatStyle::LK_ObjC, 24625 guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);")); 24626 } 24627 24628 TEST_F(FormatTest, GuessLanguageWithPragmas) { 24629 EXPECT_EQ(FormatStyle::LK_Cpp, 24630 guessLanguage("foo.h", "__pragma(warning(disable:))")); 24631 EXPECT_EQ(FormatStyle::LK_Cpp, 24632 guessLanguage("foo.h", "#pragma(warning(disable:))")); 24633 EXPECT_EQ(FormatStyle::LK_Cpp, 24634 guessLanguage("foo.h", "_Pragma(warning(disable:))")); 24635 } 24636 24637 TEST_F(FormatTest, FormatsInlineAsmSymbolicNames) { 24638 // ASM symbolic names are identifiers that must be surrounded by [] without 24639 // space in between: 24640 // https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#InputOperands 24641 24642 // Example from https://bugs.llvm.org/show_bug.cgi?id=45108. 24643 verifyFormat(R"(// 24644 asm volatile("mrs %x[result], FPCR" : [result] "=r"(result)); 24645 )"); 24646 24647 // A list of several ASM symbolic names. 24648 verifyFormat(R"(asm("mov %[e], %[d]" : [d] "=rm"(d), [e] "rm"(*e));)"); 24649 24650 // ASM symbolic names in inline ASM with inputs and outputs. 24651 verifyFormat(R"(// 24652 asm("cmoveq %1, %2, %[result]" 24653 : [result] "=r"(result) 24654 : "r"(test), "r"(new), "[result]"(old)); 24655 )"); 24656 24657 // ASM symbolic names in inline ASM with no outputs. 24658 verifyFormat(R"(asm("mov %[e], %[d]" : : [d] "=rm"(d), [e] "rm"(*e));)"); 24659 } 24660 24661 TEST_F(FormatTest, GuessedLanguageWithInlineAsmClobbers) { 24662 EXPECT_EQ(FormatStyle::LK_Cpp, 24663 guessLanguage("foo.h", "void f() {\n" 24664 " asm (\"mov %[e], %[d]\"\n" 24665 " : [d] \"=rm\" (d)\n" 24666 " [e] \"rm\" (*e));\n" 24667 "}")); 24668 EXPECT_EQ(FormatStyle::LK_Cpp, 24669 guessLanguage("foo.h", "void f() {\n" 24670 " _asm (\"mov %[e], %[d]\"\n" 24671 " : [d] \"=rm\" (d)\n" 24672 " [e] \"rm\" (*e));\n" 24673 "}")); 24674 EXPECT_EQ(FormatStyle::LK_Cpp, 24675 guessLanguage("foo.h", "void f() {\n" 24676 " __asm (\"mov %[e], %[d]\"\n" 24677 " : [d] \"=rm\" (d)\n" 24678 " [e] \"rm\" (*e));\n" 24679 "}")); 24680 EXPECT_EQ(FormatStyle::LK_Cpp, 24681 guessLanguage("foo.h", "void f() {\n" 24682 " __asm__ (\"mov %[e], %[d]\"\n" 24683 " : [d] \"=rm\" (d)\n" 24684 " [e] \"rm\" (*e));\n" 24685 "}")); 24686 EXPECT_EQ(FormatStyle::LK_Cpp, 24687 guessLanguage("foo.h", "void f() {\n" 24688 " asm (\"mov %[e], %[d]\"\n" 24689 " : [d] \"=rm\" (d),\n" 24690 " [e] \"rm\" (*e));\n" 24691 "}")); 24692 EXPECT_EQ(FormatStyle::LK_Cpp, 24693 guessLanguage("foo.h", "void f() {\n" 24694 " asm volatile (\"mov %[e], %[d]\"\n" 24695 " : [d] \"=rm\" (d)\n" 24696 " [e] \"rm\" (*e));\n" 24697 "}")); 24698 } 24699 24700 TEST_F(FormatTest, GuessLanguageWithChildLines) { 24701 EXPECT_EQ(FormatStyle::LK_Cpp, 24702 guessLanguage("foo.h", "#define FOO ({ std::string s; })")); 24703 EXPECT_EQ(FormatStyle::LK_ObjC, 24704 guessLanguage("foo.h", "#define FOO ({ NSString *s; })")); 24705 EXPECT_EQ( 24706 FormatStyle::LK_Cpp, 24707 guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })")); 24708 EXPECT_EQ( 24709 FormatStyle::LK_ObjC, 24710 guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })")); 24711 } 24712 24713 TEST_F(FormatTest, TypenameMacros) { 24714 std::vector<std::string> TypenameMacros = {"STACK_OF", "LIST", "TAILQ_ENTRY"}; 24715 24716 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=30353 24717 FormatStyle Google = getGoogleStyleWithColumns(0); 24718 Google.TypenameMacros = TypenameMacros; 24719 verifyFormat("struct foo {\n" 24720 " int bar;\n" 24721 " TAILQ_ENTRY(a) bleh;\n" 24722 "};", 24723 Google); 24724 24725 FormatStyle Macros = getLLVMStyle(); 24726 Macros.TypenameMacros = TypenameMacros; 24727 24728 verifyFormat("STACK_OF(int) a;", Macros); 24729 verifyFormat("STACK_OF(int) *a;", Macros); 24730 verifyFormat("STACK_OF(int const *) *a;", Macros); 24731 verifyFormat("STACK_OF(int *const) *a;", Macros); 24732 verifyFormat("STACK_OF(int, string) a;", Macros); 24733 verifyFormat("STACK_OF(LIST(int)) a;", Macros); 24734 verifyFormat("STACK_OF(LIST(int)) a, b;", Macros); 24735 verifyFormat("for (LIST(int) *a = NULL; a;) {\n}", Macros); 24736 verifyFormat("STACK_OF(int) f(LIST(int) *arg);", Macros); 24737 verifyFormat("vector<LIST(uint64_t) *attr> x;", Macros); 24738 verifyFormat("vector<LIST(uint64_t) *const> f(LIST(uint64_t) *arg);", Macros); 24739 24740 Macros.PointerAlignment = FormatStyle::PAS_Left; 24741 verifyFormat("STACK_OF(int)* a;", Macros); 24742 verifyFormat("STACK_OF(int*)* a;", Macros); 24743 verifyFormat("x = (STACK_OF(uint64_t))*a;", Macros); 24744 verifyFormat("x = (STACK_OF(uint64_t))&a;", Macros); 24745 verifyFormat("vector<STACK_OF(uint64_t)* attr> x;", Macros); 24746 } 24747 24748 TEST_F(FormatTest, AtomicQualifier) { 24749 // Check that we treate _Atomic as a type and not a function call 24750 FormatStyle Google = getGoogleStyleWithColumns(0); 24751 verifyFormat("struct foo {\n" 24752 " int a1;\n" 24753 " _Atomic(a) a2;\n" 24754 " _Atomic(_Atomic(int) *const) a3;\n" 24755 "};", 24756 Google); 24757 verifyFormat("_Atomic(uint64_t) a;"); 24758 verifyFormat("_Atomic(uint64_t) *a;"); 24759 verifyFormat("_Atomic(uint64_t const *) *a;"); 24760 verifyFormat("_Atomic(uint64_t *const) *a;"); 24761 verifyFormat("_Atomic(const uint64_t *) *a;"); 24762 verifyFormat("_Atomic(uint64_t) a;"); 24763 verifyFormat("_Atomic(_Atomic(uint64_t)) a;"); 24764 verifyFormat("_Atomic(_Atomic(uint64_t)) a, b;"); 24765 verifyFormat("for (_Atomic(uint64_t) *a = NULL; a;) {\n}"); 24766 verifyFormat("_Atomic(uint64_t) f(_Atomic(uint64_t) *arg);"); 24767 24768 verifyFormat("_Atomic(uint64_t) *s(InitValue);"); 24769 verifyFormat("_Atomic(uint64_t) *s{InitValue};"); 24770 FormatStyle Style = getLLVMStyle(); 24771 Style.PointerAlignment = FormatStyle::PAS_Left; 24772 verifyFormat("_Atomic(uint64_t)* s(InitValue);", Style); 24773 verifyFormat("_Atomic(uint64_t)* s{InitValue};", Style); 24774 verifyFormat("_Atomic(int)* a;", Style); 24775 verifyFormat("_Atomic(int*)* a;", Style); 24776 verifyFormat("vector<_Atomic(uint64_t)* attr> x;", Style); 24777 24778 Style.SpacesInParens = FormatStyle::SIPO_Custom; 24779 Style.SpacesInParensOptions.InCStyleCasts = true; 24780 verifyFormat("x = ( _Atomic(uint64_t) )*a;", Style); 24781 Style.SpacesInParensOptions.InCStyleCasts = false; 24782 Style.SpacesInParensOptions.Other = true; 24783 verifyFormat("x = (_Atomic( uint64_t ))*a;", Style); 24784 verifyFormat("x = (_Atomic( uint64_t ))&a;", Style); 24785 } 24786 24787 TEST_F(FormatTest, C11Generic) { 24788 verifyFormat("_Generic(x, int: 1, default: 0)"); 24789 verifyFormat("#define cbrt(X) _Generic((X), float: cbrtf, default: cbrt)(X)"); 24790 verifyFormat("_Generic(x, const char *: 1, char *const: 16, int: 8);"); 24791 verifyFormat("_Generic(x, int: f1, const int: f2)();"); 24792 verifyFormat("_Generic(x, struct A: 1, void (*)(void): 2);"); 24793 24794 verifyFormat("_Generic(x,\n" 24795 " float: f,\n" 24796 " default: d,\n" 24797 " long double: ld,\n" 24798 " float _Complex: fc,\n" 24799 " double _Complex: dc,\n" 24800 " long double _Complex: ldc)"); 24801 24802 verifyFormat("while (_Generic(x, //\n" 24803 " long: x)(x) > x) {\n" 24804 "}"); 24805 verifyFormat("while (_Generic(x, //\n" 24806 " long: x)(x)) {\n" 24807 "}"); 24808 verifyFormat("x(_Generic(x, //\n" 24809 " long: x)(x));"); 24810 24811 FormatStyle Style = getLLVMStyle(); 24812 Style.ColumnLimit = 40; 24813 verifyFormat("#define LIMIT_MAX(T) \\\n" 24814 " _Generic(((T)0), \\\n" 24815 " unsigned int: UINT_MAX, \\\n" 24816 " unsigned long: ULONG_MAX, \\\n" 24817 " unsigned long long: ULLONG_MAX)", 24818 Style); 24819 verifyFormat("_Generic(x,\n" 24820 " struct A: 1,\n" 24821 " void (*)(void): 2);", 24822 Style); 24823 24824 Style.ContinuationIndentWidth = 2; 24825 verifyFormat("_Generic(x,\n" 24826 " struct A: 1,\n" 24827 " void (*)(void): 2);", 24828 Style); 24829 } 24830 24831 TEST_F(FormatTest, AmbersandInLamda) { 24832 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=41899 24833 FormatStyle AlignStyle = getLLVMStyle(); 24834 AlignStyle.PointerAlignment = FormatStyle::PAS_Left; 24835 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle); 24836 AlignStyle.PointerAlignment = FormatStyle::PAS_Right; 24837 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle); 24838 } 24839 24840 TEST_F(FormatTest, TrailingReturnTypeAuto) { 24841 FormatStyle Style = getLLVMStyle(); 24842 verifyFormat("[]() -> auto { return Val; }", Style); 24843 verifyFormat("[]() -> auto * { return Val; }", Style); 24844 verifyFormat("[]() -> auto & { return Val; }", Style); 24845 verifyFormat("auto foo() -> auto { return Val; }", Style); 24846 verifyFormat("auto foo() -> auto * { return Val; }", Style); 24847 verifyFormat("auto foo() -> auto & { return Val; }", Style); 24848 } 24849 24850 TEST_F(FormatTest, SpacesInConditionalStatement) { 24851 FormatStyle Spaces = getLLVMStyle(); 24852 Spaces.IfMacros.clear(); 24853 Spaces.IfMacros.push_back("MYIF"); 24854 Spaces.SpacesInParens = FormatStyle::SIPO_Custom; 24855 Spaces.SpacesInParensOptions.InConditionalStatements = true; 24856 verifyFormat("for ( int i = 0; i; i++ )\n continue;", Spaces); 24857 verifyFormat("if ( !a )\n return;", Spaces); 24858 verifyFormat("if ( a )\n return;", Spaces); 24859 verifyFormat("if constexpr ( a )\n return;", Spaces); 24860 verifyFormat("MYIF ( a )\n return;", Spaces); 24861 verifyFormat("MYIF ( a )\n return;\nelse MYIF ( b )\n return;", Spaces); 24862 verifyFormat("MYIF ( a )\n return;\nelse\n return;", Spaces); 24863 verifyFormat("switch ( a )\ncase 1:\n return;", Spaces); 24864 verifyFormat("while ( a )\n return;", Spaces); 24865 verifyFormat("while ( (a && b) )\n return;", Spaces); 24866 verifyFormat("do {\n} while ( 1 != 0 );", Spaces); 24867 verifyFormat("try {\n} catch ( const std::exception & ) {\n}", Spaces); 24868 // Check that space on the left of "::" is inserted as expected at beginning 24869 // of condition. 24870 verifyFormat("while ( ::func() )\n return;", Spaces); 24871 24872 // Check impact of ControlStatementsExceptControlMacros is honored. 24873 Spaces.SpaceBeforeParens = 24874 FormatStyle::SBPO_ControlStatementsExceptControlMacros; 24875 verifyFormat("MYIF( a )\n return;", Spaces); 24876 verifyFormat("MYIF( a )\n return;\nelse MYIF( b )\n return;", Spaces); 24877 verifyFormat("MYIF( a )\n return;\nelse\n return;", Spaces); 24878 } 24879 24880 TEST_F(FormatTest, AlternativeOperators) { 24881 // Test case for ensuring alternate operators are not 24882 // combined with their right most neighbour. 24883 verifyFormat("int a and b;"); 24884 verifyFormat("int a and_eq b;"); 24885 verifyFormat("int a bitand b;"); 24886 verifyFormat("int a bitor b;"); 24887 verifyFormat("int a compl b;"); 24888 verifyFormat("int a not b;"); 24889 verifyFormat("int a not_eq b;"); 24890 verifyFormat("int a or b;"); 24891 verifyFormat("int a xor b;"); 24892 verifyFormat("int a xor_eq b;"); 24893 verifyFormat("return this not_eq bitand other;"); 24894 verifyFormat("bool operator not_eq(const X bitand other)"); 24895 24896 verifyFormat("int a and 5;"); 24897 verifyFormat("int a and_eq 5;"); 24898 verifyFormat("int a bitand 5;"); 24899 verifyFormat("int a bitor 5;"); 24900 verifyFormat("int a compl 5;"); 24901 verifyFormat("int a not 5;"); 24902 verifyFormat("int a not_eq 5;"); 24903 verifyFormat("int a or 5;"); 24904 verifyFormat("int a xor 5;"); 24905 verifyFormat("int a xor_eq 5;"); 24906 24907 verifyFormat("int a compl(5);"); 24908 verifyFormat("int a not(5);"); 24909 24910 verifyFormat("compl foo();"); // ~foo(); 24911 verifyFormat("foo() <%%>"); // foo() {} 24912 verifyFormat("void foo() <%%>"); // void foo() {} 24913 verifyFormat("int a<:1:>;"); // int a[1]; 24914 verifyFormat("%:define ABC abc"); // #define ABC abc 24915 verifyFormat("%:%:"); // ## 24916 24917 verifyFormat("a = v(not;);\n" 24918 "b = v(not+);\n" 24919 "c = v(not x);\n" 24920 "d = v(not 1);\n" 24921 "e = v(not 123.f);"); 24922 24923 verifyNoChange("#define ASSEMBLER_INSTRUCTION_LIST(V) \\\n" 24924 " V(and) \\\n" 24925 " V(not) \\\n" 24926 " V(not!) \\\n" 24927 " V(other)", 24928 getLLVMStyleWithColumns(40)); 24929 } 24930 24931 TEST_F(FormatTest, STLWhileNotDefineChed) { 24932 verifyFormat("#if defined(while)\n" 24933 "#define while EMIT WARNING C4005\n" 24934 "#endif // while"); 24935 } 24936 24937 TEST_F(FormatTest, OperatorSpacing) { 24938 FormatStyle Style = getLLVMStyle(); 24939 Style.PointerAlignment = FormatStyle::PAS_Right; 24940 verifyFormat("Foo::operator*();", Style); 24941 verifyFormat("Foo::operator void *();", Style); 24942 verifyFormat("Foo::operator void **();", Style); 24943 verifyFormat("Foo::operator void *&();", Style); 24944 verifyFormat("Foo::operator void *&&();", Style); 24945 verifyFormat("Foo::operator void const *();", Style); 24946 verifyFormat("Foo::operator void const **();", Style); 24947 verifyFormat("Foo::operator void const *&();", Style); 24948 verifyFormat("Foo::operator void const *&&();", Style); 24949 verifyFormat("Foo::operator()(void *);", Style); 24950 verifyFormat("Foo::operator*(void *);", Style); 24951 verifyFormat("Foo::operator*();", Style); 24952 verifyFormat("Foo::operator**();", Style); 24953 verifyFormat("Foo::operator&();", Style); 24954 verifyFormat("Foo::operator<int> *();", Style); 24955 verifyFormat("Foo::operator<Foo> *();", Style); 24956 verifyFormat("Foo::operator<int> **();", Style); 24957 verifyFormat("Foo::operator<Foo> **();", Style); 24958 verifyFormat("Foo::operator<int> &();", Style); 24959 verifyFormat("Foo::operator<Foo> &();", Style); 24960 verifyFormat("Foo::operator<int> &&();", Style); 24961 verifyFormat("Foo::operator<Foo> &&();", Style); 24962 verifyFormat("Foo::operator<int> *&();", Style); 24963 verifyFormat("Foo::operator<Foo> *&();", Style); 24964 verifyFormat("Foo::operator<int> *&&();", Style); 24965 verifyFormat("Foo::operator<Foo> *&&();", Style); 24966 verifyFormat("operator*(int (*)(), class Foo);", Style); 24967 24968 verifyFormat("Foo::operator&();", Style); 24969 verifyFormat("Foo::operator void &();", Style); 24970 verifyFormat("Foo::operator void const &();", Style); 24971 verifyFormat("Foo::operator()(void &);", Style); 24972 verifyFormat("Foo::operator&(void &);", Style); 24973 verifyFormat("Foo::operator&();", Style); 24974 verifyFormat("operator&(int (&)(), class Foo);", Style); 24975 verifyFormat("operator&&(int (&)(), class Foo);", Style); 24976 24977 verifyFormat("Foo::operator&&();", Style); 24978 verifyFormat("Foo::operator**();", Style); 24979 verifyFormat("Foo::operator void &&();", Style); 24980 verifyFormat("Foo::operator void const &&();", Style); 24981 verifyFormat("Foo::operator()(void &&);", Style); 24982 verifyFormat("Foo::operator&&(void &&);", Style); 24983 verifyFormat("Foo::operator&&();", Style); 24984 verifyFormat("operator&&(int (&&)(), class Foo);", Style); 24985 verifyFormat("operator const nsTArrayRight<E> &()", Style); 24986 verifyFormat("[[nodiscard]] operator const nsTArrayRight<E, Allocator> &()", 24987 Style); 24988 verifyFormat("operator void **()", Style); 24989 verifyFormat("operator const FooRight<Object> &()", Style); 24990 verifyFormat("operator const FooRight<Object> *()", Style); 24991 verifyFormat("operator const FooRight<Object> **()", Style); 24992 verifyFormat("operator const FooRight<Object> *&()", Style); 24993 verifyFormat("operator const FooRight<Object> *&&()", Style); 24994 24995 Style.PointerAlignment = FormatStyle::PAS_Left; 24996 verifyFormat("Foo::operator*();", Style); 24997 verifyFormat("Foo::operator**();", Style); 24998 verifyFormat("Foo::operator void*();", Style); 24999 verifyFormat("Foo::operator void**();", Style); 25000 verifyFormat("Foo::operator void*&();", Style); 25001 verifyFormat("Foo::operator void*&&();", Style); 25002 verifyFormat("Foo::operator void const*();", Style); 25003 verifyFormat("Foo::operator void const**();", Style); 25004 verifyFormat("Foo::operator void const*&();", Style); 25005 verifyFormat("Foo::operator void const*&&();", Style); 25006 verifyFormat("Foo::operator/*comment*/ void*();", Style); 25007 verifyFormat("Foo::operator/*a*/ const /*b*/ void*();", Style); 25008 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void*();", Style); 25009 verifyFormat("Foo::operator()(void*);", Style); 25010 verifyFormat("Foo::operator*(void*);", Style); 25011 verifyFormat("Foo::operator*();", Style); 25012 verifyFormat("Foo::operator<int>*();", Style); 25013 verifyFormat("Foo::operator<Foo>*();", Style); 25014 verifyFormat("Foo::operator<int>**();", Style); 25015 verifyFormat("Foo::operator<Foo>**();", Style); 25016 verifyFormat("Foo::operator<Foo>*&();", Style); 25017 verifyFormat("Foo::operator<int>&();", Style); 25018 verifyFormat("Foo::operator<Foo>&();", Style); 25019 verifyFormat("Foo::operator<int>&&();", Style); 25020 verifyFormat("Foo::operator<Foo>&&();", Style); 25021 verifyFormat("Foo::operator<int>*&();", Style); 25022 verifyFormat("Foo::operator<Foo>*&();", Style); 25023 verifyFormat("operator*(int (*)(), class Foo);", Style); 25024 25025 verifyFormat("Foo::operator&();", Style); 25026 verifyFormat("Foo::operator void&();", Style); 25027 verifyFormat("Foo::operator void const&();", Style); 25028 verifyFormat("Foo::operator/*comment*/ void&();", Style); 25029 verifyFormat("Foo::operator/*a*/ const /*b*/ void&();", Style); 25030 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&();", Style); 25031 verifyFormat("Foo::operator()(void&);", Style); 25032 verifyFormat("Foo::operator&(void&);", Style); 25033 verifyFormat("Foo::operator&();", Style); 25034 verifyFormat("operator&(int (&)(), class Foo);", Style); 25035 verifyFormat("operator&(int (&&)(), class Foo);", Style); 25036 verifyFormat("operator&&(int (&&)(), class Foo);", Style); 25037 25038 verifyFormat("Foo::operator&&();", Style); 25039 verifyFormat("Foo::operator void&&();", Style); 25040 verifyFormat("Foo::operator void const&&();", Style); 25041 verifyFormat("Foo::operator/*comment*/ void&&();", Style); 25042 verifyFormat("Foo::operator/*a*/ const /*b*/ void&&();", Style); 25043 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&&();", Style); 25044 verifyFormat("Foo::operator()(void&&);", Style); 25045 verifyFormat("Foo::operator&&(void&&);", Style); 25046 verifyFormat("Foo::operator&&();", Style); 25047 verifyFormat("operator&&(int (&&)(), class Foo);", Style); 25048 verifyFormat("operator const nsTArrayLeft<E>&()", Style); 25049 verifyFormat("[[nodiscard]] operator const nsTArrayLeft<E, Allocator>&()", 25050 Style); 25051 verifyFormat("operator void**()", Style); 25052 verifyFormat("operator const FooLeft<Object>&()", Style); 25053 verifyFormat("operator const FooLeft<Object>*()", Style); 25054 verifyFormat("operator const FooLeft<Object>**()", Style); 25055 verifyFormat("operator const FooLeft<Object>*&()", Style); 25056 verifyFormat("operator const FooLeft<Object>*&&()", Style); 25057 25058 // PR45107 25059 verifyFormat("operator Vector<String>&();", Style); 25060 verifyFormat("operator const Vector<String>&();", Style); 25061 verifyFormat("operator foo::Bar*();", Style); 25062 verifyFormat("operator const Foo<X>::Bar<Y>*();", Style); 25063 verifyFormat("operator/*a*/ const /*b*/ Foo /*c*/<X> /*d*/ ::Bar<Y>*();", 25064 Style); 25065 25066 Style.PointerAlignment = FormatStyle::PAS_Middle; 25067 verifyFormat("Foo::operator*();", Style); 25068 verifyFormat("Foo::operator void *();", Style); 25069 verifyFormat("Foo::operator()(void *);", Style); 25070 verifyFormat("Foo::operator*(void *);", Style); 25071 verifyFormat("Foo::operator*();", Style); 25072 verifyFormat("operator*(int (*)(), class Foo);", Style); 25073 25074 verifyFormat("Foo::operator&();", Style); 25075 verifyFormat("Foo::operator void &();", Style); 25076 verifyFormat("Foo::operator void const &();", Style); 25077 verifyFormat("Foo::operator()(void &);", Style); 25078 verifyFormat("Foo::operator&(void &);", Style); 25079 verifyFormat("Foo::operator&();", Style); 25080 verifyFormat("operator&(int (&)(), class Foo);", Style); 25081 25082 verifyFormat("Foo::operator&&();", Style); 25083 verifyFormat("Foo::operator void &&();", Style); 25084 verifyFormat("Foo::operator void const &&();", Style); 25085 verifyFormat("Foo::operator()(void &&);", Style); 25086 verifyFormat("Foo::operator&&(void &&);", Style); 25087 verifyFormat("Foo::operator&&();", Style); 25088 verifyFormat("operator&&(int (&&)(), class Foo);", Style); 25089 } 25090 25091 TEST_F(FormatTest, OperatorPassedAsAFunctionPtr) { 25092 FormatStyle Style = getLLVMStyle(); 25093 // PR46157 25094 verifyFormat("foo(operator+, -42);", Style); 25095 verifyFormat("foo(operator++, -42);", Style); 25096 verifyFormat("foo(operator--, -42);", Style); 25097 verifyFormat("foo(-42, operator--);", Style); 25098 verifyFormat("foo(-42, operator, );", Style); 25099 verifyFormat("foo(operator, , -42);", Style); 25100 } 25101 25102 TEST_F(FormatTest, WhitespaceSensitiveMacros) { 25103 FormatStyle Style = getLLVMStyle(); 25104 Style.WhitespaceSensitiveMacros.push_back("FOO"); 25105 25106 // Newlines are important here. 25107 verifyNoChange("FOO(1+2 )\n", Style); 25108 verifyNoChange("FOO(a:b:c)\n", Style); 25109 25110 // Don't use the helpers here, since 'mess up' will change the whitespace 25111 // and these are all whitespace sensitive by definition 25112 verifyNoChange("FOO(String-ized&Messy+But(: :Still)=Intentional);", Style); 25113 verifyNoChange("FOO(String-ized&Messy+But\\(: :Still)=Intentional);", Style); 25114 verifyNoChange("FOO(String-ized&Messy+But,: :Still=Intentional);", Style); 25115 verifyNoChange("FOO(String-ized&Messy+But,: :\n" 25116 " Still=Intentional);", 25117 Style); 25118 Style.AlignConsecutiveAssignments.Enabled = true; 25119 verifyNoChange("FOO(String-ized=&Messy+But,: :\n" 25120 " Still=Intentional);", 25121 Style); 25122 25123 Style.ColumnLimit = 21; 25124 verifyNoChange("FOO(String-ized&Messy+But: :Still=Intentional);", Style); 25125 } 25126 25127 TEST_F(FormatTest, SkipMacroDefinitionBody) { 25128 auto Style = getLLVMStyle(); 25129 Style.SkipMacroDefinitionBody = true; 25130 25131 verifyFormat("#define A", "#define A", Style); 25132 verifyFormat("#define A a aa", "#define A a aa", Style); 25133 verifyNoChange("#define A b", Style); 25134 verifyNoChange("#define A ( args )", Style); 25135 verifyNoChange("#define A ( args ) = func ( args )", Style); 25136 verifyNoChange("#define A ( args ) { int a = 1 ; }", Style); 25137 verifyNoChange("#define A ( args ) \\\n" 25138 " {\\\n" 25139 " int a = 1 ;\\\n" 25140 "}", 25141 Style); 25142 25143 verifyNoChange("#define A x:", Style); 25144 verifyNoChange("#define A a. b", Style); 25145 25146 // Surrounded with formatted code. 25147 verifyFormat("int a;\n" 25148 "#define A a\n" 25149 "int a;", 25150 "int a ;\n" 25151 "#define A a\n" 25152 "int a ;", 25153 Style); 25154 25155 // Columns are not broken when a limit is set. 25156 Style.ColumnLimit = 10; 25157 verifyFormat("#define A a a a a", " # define A a a a a ", Style); 25158 verifyNoChange("#define A a a a a", Style); 25159 25160 Style.ColumnLimit = 15; 25161 verifyFormat("#define A // a\n" 25162 " // very\n" 25163 " // long\n" 25164 " // comment", 25165 "#define A //a very long comment", Style); 25166 Style.ColumnLimit = 0; 25167 25168 // Multiline definition. 25169 verifyNoChange("#define A \\\n" 25170 "Line one with spaces . \\\n" 25171 " Line two.", 25172 Style); 25173 verifyNoChange("#define A \\\n" 25174 "a a \\\n" 25175 "a \\\n" 25176 "a", 25177 Style); 25178 Style.AlignEscapedNewlines = FormatStyle::ENAS_Left; 25179 verifyNoChange("#define A \\\n" 25180 "a a \\\n" 25181 "a \\\n" 25182 "a", 25183 Style); 25184 Style.AlignEscapedNewlines = FormatStyle::ENAS_Right; 25185 verifyNoChange("#define A \\\n" 25186 "a a \\\n" 25187 "a \\\n" 25188 "a", 25189 Style); 25190 25191 // Adjust indendations but don't change the definition. 25192 Style.IndentPPDirectives = FormatStyle::PPDIS_None; 25193 verifyNoChange("#if A\n" 25194 "#define A a\n" 25195 "#endif", 25196 Style); 25197 verifyFormat("#if A\n" 25198 "#define A a\n" 25199 "#endif", 25200 "#if A\n" 25201 " #define A a\n" 25202 "#endif", 25203 Style); 25204 Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash; 25205 verifyNoChange("#if A\n" 25206 "# define A a\n" 25207 "#endif", 25208 Style); 25209 verifyFormat("#if A\n" 25210 "# define A a\n" 25211 "#endif", 25212 "#if A\n" 25213 " #define A a\n" 25214 "#endif", 25215 Style); 25216 Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash; 25217 verifyNoChange("#if A\n" 25218 " #define A a\n" 25219 "#endif", 25220 Style); 25221 verifyFormat("#if A\n" 25222 " #define A a\n" 25223 "#endif", 25224 "#if A\n" 25225 " # define A a\n" 25226 "#endif", 25227 Style); 25228 25229 Style.IndentPPDirectives = FormatStyle::PPDIS_None; 25230 // SkipMacroDefinitionBody should not affect other PP directives 25231 verifyFormat("#if !defined(A)\n" 25232 "#define A a\n" 25233 "#endif", 25234 "#if ! defined ( A )\n" 25235 " #define A a\n" 25236 "#endif", 25237 Style); 25238 25239 // With comments. 25240 verifyFormat("/* */ #define A a // a a", "/* */ # define A a // a a", 25241 Style); 25242 verifyNoChange("/* */ #define A a // a a", Style); 25243 25244 verifyFormat("int a; // a\n" 25245 "#define A // a\n" 25246 "int aaa; // a", 25247 "int a; // a\n" 25248 "#define A // a\n" 25249 "int aaa; // a", 25250 Style); 25251 25252 verifyNoChange( 25253 "#define MACRO_WITH_COMMENTS() \\\n" 25254 " public: \\\n" 25255 " /* Documentation parsed by Doxygen for the following method. */ \\\n" 25256 " static MyType getClassTypeId(); \\\n" 25257 " /** Normal comment for the following method. */ \\\n" 25258 " virtual MyType getTypeId() const;", 25259 Style); 25260 25261 // multiline macro definitions 25262 verifyNoChange("#define A a\\\n" 25263 " A a \\\n " 25264 " A a", 25265 Style); 25266 } 25267 25268 TEST_F(FormatTest, VeryLongNamespaceCommentSplit) { 25269 // These tests are not in NamespaceEndCommentsFixerTest because that doesn't 25270 // test its interaction with line wrapping 25271 FormatStyle Style = getLLVMStyleWithColumns(80); 25272 verifyFormat("namespace {\n" 25273 "int i;\n" 25274 "int j;\n" 25275 "} // namespace", 25276 Style); 25277 25278 verifyFormat("namespace AAA {\n" 25279 "int i;\n" 25280 "int j;\n" 25281 "} // namespace AAA", 25282 Style); 25283 25284 verifyFormat("namespace Averyveryveryverylongnamespace {\n" 25285 "int i;\n" 25286 "int j;\n" 25287 "} // namespace Averyveryveryverylongnamespace", 25288 "namespace Averyveryveryverylongnamespace {\n" 25289 "int i;\n" 25290 "int j;\n" 25291 "}", 25292 Style); 25293 25294 verifyFormat( 25295 "namespace " 25296 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n" 25297 " went::mad::now {\n" 25298 "int i;\n" 25299 "int j;\n" 25300 "} // namespace\n" 25301 " // " 25302 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::" 25303 "went::mad::now", 25304 "namespace " 25305 "would::it::save::you::a::lot::of::time::if_::i::" 25306 "just::gave::up::and_::went::mad::now {\n" 25307 "int i;\n" 25308 "int j;\n" 25309 "}", 25310 Style); 25311 25312 // This used to duplicate the comment again and again on subsequent runs 25313 verifyFormat( 25314 "namespace " 25315 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n" 25316 " went::mad::now {\n" 25317 "int i;\n" 25318 "int j;\n" 25319 "} // namespace\n" 25320 " // " 25321 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::" 25322 "went::mad::now", 25323 "namespace " 25324 "would::it::save::you::a::lot::of::time::if_::i::" 25325 "just::gave::up::and_::went::mad::now {\n" 25326 "int i;\n" 25327 "int j;\n" 25328 "} // namespace\n" 25329 " // " 25330 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::" 25331 "and_::went::mad::now", 25332 Style); 25333 } 25334 25335 TEST_F(FormatTest, LikelyUnlikely) { 25336 FormatStyle Style = getLLVMStyle(); 25337 25338 verifyFormat("if (argc > 5) [[unlikely]] {\n" 25339 " return 29;\n" 25340 "}", 25341 Style); 25342 25343 verifyFormat("if (argc > 5) [[likely]] {\n" 25344 " return 29;\n" 25345 "}", 25346 Style); 25347 25348 verifyFormat("if (argc > 5) [[unlikely]] {\n" 25349 " return 29;\n" 25350 "} else [[likely]] {\n" 25351 " return 42;\n" 25352 "}", 25353 Style); 25354 25355 verifyFormat("if (argc > 5) [[unlikely]] {\n" 25356 " return 29;\n" 25357 "} else if (argc > 10) [[likely]] {\n" 25358 " return 99;\n" 25359 "} else {\n" 25360 " return 42;\n" 25361 "}", 25362 Style); 25363 25364 verifyFormat("if (argc > 5) [[gnu::unused]] {\n" 25365 " return 29;\n" 25366 "}", 25367 Style); 25368 25369 verifyFormat("if (argc > 5) [[unlikely]]\n" 25370 " return 29;", 25371 Style); 25372 verifyFormat("if (argc > 5) [[likely]]\n" 25373 " return 29;", 25374 Style); 25375 25376 verifyFormat("while (limit > 0) [[unlikely]] {\n" 25377 " --limit;\n" 25378 "}", 25379 Style); 25380 verifyFormat("for (auto &limit : limits) [[likely]] {\n" 25381 " --limit;\n" 25382 "}", 25383 Style); 25384 25385 verifyFormat("for (auto &limit : limits) [[unlikely]]\n" 25386 " --limit;", 25387 Style); 25388 verifyFormat("while (limit > 0) [[likely]]\n" 25389 " --limit;", 25390 Style); 25391 25392 Style.AttributeMacros.push_back("UNLIKELY"); 25393 Style.AttributeMacros.push_back("LIKELY"); 25394 verifyFormat("if (argc > 5) UNLIKELY\n" 25395 " return 29;", 25396 Style); 25397 25398 verifyFormat("if (argc > 5) UNLIKELY {\n" 25399 " return 29;\n" 25400 "}", 25401 Style); 25402 verifyFormat("if (argc > 5) UNLIKELY {\n" 25403 " return 29;\n" 25404 "} else [[likely]] {\n" 25405 " return 42;\n" 25406 "}", 25407 Style); 25408 verifyFormat("if (argc > 5) UNLIKELY {\n" 25409 " return 29;\n" 25410 "} else LIKELY {\n" 25411 " return 42;\n" 25412 "}", 25413 Style); 25414 verifyFormat("if (argc > 5) [[unlikely]] {\n" 25415 " return 29;\n" 25416 "} else LIKELY {\n" 25417 " return 42;\n" 25418 "}", 25419 Style); 25420 25421 verifyFormat("for (auto &limit : limits) UNLIKELY {\n" 25422 " --limit;\n" 25423 "}", 25424 Style); 25425 verifyFormat("while (limit > 0) LIKELY {\n" 25426 " --limit;\n" 25427 "}", 25428 Style); 25429 25430 verifyFormat("while (limit > 0) UNLIKELY\n" 25431 " --limit;", 25432 Style); 25433 verifyFormat("for (auto &limit : limits) LIKELY\n" 25434 " --limit;", 25435 Style); 25436 } 25437 25438 TEST_F(FormatTest, PenaltyIndentedWhitespace) { 25439 verifyFormat("Constructor()\n" 25440 " : aaaaaa(aaaaaa), aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 25441 " aaaa(aaaaaaaaaaaaaaaaaa, " 25442 "aaaaaaaaaaaaaaaaaat))"); 25443 verifyFormat("Constructor()\n" 25444 " : aaaaaaaaaaaaa(aaaaaa), " 25445 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)"); 25446 25447 FormatStyle StyleWithWhitespacePenalty = getLLVMStyle(); 25448 StyleWithWhitespacePenalty.PenaltyIndentedWhitespace = 5; 25449 verifyFormat("Constructor()\n" 25450 " : aaaaaa(aaaaaa),\n" 25451 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 25452 " aaaa(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaat))", 25453 StyleWithWhitespacePenalty); 25454 verifyFormat("Constructor()\n" 25455 " : aaaaaaaaaaaaa(aaaaaa), " 25456 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)", 25457 StyleWithWhitespacePenalty); 25458 } 25459 25460 TEST_F(FormatTest, LLVMDefaultStyle) { 25461 FormatStyle Style = getLLVMStyle(); 25462 verifyFormat("extern \"C\" {\n" 25463 "int foo();\n" 25464 "}", 25465 Style); 25466 } 25467 TEST_F(FormatTest, GNUDefaultStyle) { 25468 FormatStyle Style = getGNUStyle(); 25469 verifyFormat("extern \"C\"\n" 25470 "{\n" 25471 " int foo ();\n" 25472 "}", 25473 Style); 25474 } 25475 TEST_F(FormatTest, MozillaDefaultStyle) { 25476 FormatStyle Style = getMozillaStyle(); 25477 verifyFormat("extern \"C\"\n" 25478 "{\n" 25479 " int foo();\n" 25480 "}", 25481 Style); 25482 } 25483 TEST_F(FormatTest, GoogleDefaultStyle) { 25484 FormatStyle Style = getGoogleStyle(); 25485 verifyFormat("extern \"C\" {\n" 25486 "int foo();\n" 25487 "}", 25488 Style); 25489 } 25490 TEST_F(FormatTest, ChromiumDefaultStyle) { 25491 FormatStyle Style = getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp); 25492 verifyFormat("extern \"C\" {\n" 25493 "int foo();\n" 25494 "}", 25495 Style); 25496 } 25497 TEST_F(FormatTest, MicrosoftDefaultStyle) { 25498 FormatStyle Style = getMicrosoftStyle(FormatStyle::LanguageKind::LK_Cpp); 25499 verifyFormat("extern \"C\"\n" 25500 "{\n" 25501 " int foo();\n" 25502 "}", 25503 Style); 25504 } 25505 TEST_F(FormatTest, WebKitDefaultStyle) { 25506 FormatStyle Style = getWebKitStyle(); 25507 verifyFormat("extern \"C\" {\n" 25508 "int foo();\n" 25509 "}", 25510 Style); 25511 } 25512 25513 TEST_F(FormatTest, Concepts) { 25514 EXPECT_EQ(getLLVMStyle().BreakBeforeConceptDeclarations, 25515 FormatStyle::BBCDS_Always); 25516 25517 // The default in LLVM style is REI_OuterScope, but these tests were written 25518 // when the default was REI_Keyword. 25519 FormatStyle Style = getLLVMStyle(); 25520 Style.RequiresExpressionIndentation = FormatStyle::REI_Keyword; 25521 25522 verifyFormat("template <typename T>\n" 25523 "concept True = true;"); 25524 25525 verifyFormat("template <typename T>\n" 25526 "concept C = ((false || foo()) && C2<T>) ||\n" 25527 " (std::trait<T>::value && Baz) || sizeof(T) >= 6;", 25528 getLLVMStyleWithColumns(60)); 25529 25530 verifyFormat("template <typename T>\n" 25531 "concept DelayedCheck = true && requires(T t) { t.bar(); } && " 25532 "sizeof(T) <= 8;"); 25533 25534 verifyFormat("template <typename T>\n" 25535 "concept DelayedCheck = true && requires(T t) {\n" 25536 " t.bar();\n" 25537 " t.baz();\n" 25538 " } && sizeof(T) <= 8;", 25539 Style); 25540 25541 verifyFormat("template <typename T>\n" 25542 "concept DelayedCheck = true && requires(T t) { // Comment\n" 25543 " t.bar();\n" 25544 " t.baz();\n" 25545 " } && sizeof(T) <= 8;", 25546 Style); 25547 25548 verifyFormat("template <typename T>\n" 25549 "concept DelayedCheck = false || requires(T t) { t.bar(); } && " 25550 "sizeof(T) <= 8;"); 25551 25552 verifyFormat("template <typename T>\n" 25553 "concept DelayedCheck = Unit<T> && !DerivedUnit<T>;"); 25554 25555 verifyFormat("template <typename T>\n" 25556 "concept DelayedCheck = Unit<T> && !(DerivedUnit<T>);"); 25557 25558 verifyFormat("template <typename T>\n" 25559 "concept DelayedCheck = Unit<T> && !!DerivedUnit<T>;"); 25560 25561 verifyFormat("template <typename T>\n" 25562 "concept DelayedCheck = !!false || requires(T t) { t.bar(); } " 25563 "&& sizeof(T) <= 8;"); 25564 25565 verifyFormat("template <typename T>\n" 25566 "concept DelayedCheck =\n" 25567 " static_cast<bool>(0) || requires(T t) { t.bar(); } && " 25568 "sizeof(T) <= 8;"); 25569 25570 verifyFormat("template <typename T>\n" 25571 "concept DelayedCheck = bool(0) || requires(T t) { t.bar(); } " 25572 "&& sizeof(T) <= 8;"); 25573 25574 verifyFormat( 25575 "template <typename T>\n" 25576 "concept DelayedCheck =\n" 25577 " (bool)(0) || requires(T t) { t.bar(); } && sizeof(T) <= 8;"); 25578 25579 verifyFormat("template <typename T>\n" 25580 "concept DelayedCheck = (bool)0 || requires(T t) { t.bar(); } " 25581 "&& sizeof(T) <= 8;"); 25582 25583 verifyFormat("template <typename T>\n" 25584 "concept Size = sizeof(T) >= 5 && requires(T t) { t.bar(); } && " 25585 "sizeof(T) <= 8;"); 25586 25587 verifyFormat("template <typename T>\n" 25588 "concept Size = 2 < 5 && 2 <= 5 && 8 >= 5 && 8 > 5 &&\n" 25589 " requires(T t) {\n" 25590 " t.bar();\n" 25591 " t.baz();\n" 25592 " } && sizeof(T) <= 8 && !(4 < 3);", 25593 getLLVMStyleWithColumns(60)); 25594 25595 verifyFormat("template <typename T>\n" 25596 "concept TrueOrNot = IsAlwaysTrue || IsNeverTrue;"); 25597 25598 verifyFormat("template <typename T>\n" 25599 "concept C = foo();"); 25600 25601 verifyFormat("template <typename T>\n" 25602 "concept C = foo(T());"); 25603 25604 verifyFormat("template <typename T>\n" 25605 "concept C = foo(T{});"); 25606 25607 verifyFormat("template <typename T>\n" 25608 "concept Size = V<sizeof(T)>::Value > 5;"); 25609 25610 verifyFormat("template <typename T>\n" 25611 "concept True = S<T>::Value;"); 25612 25613 verifyFormat("template <S T>\n" 25614 "concept True = T.field;"); 25615 25616 verifyFormat( 25617 "template <typename T>\n" 25618 "concept C = []() { return true; }() && requires(T t) { t.bar(); } &&\n" 25619 " sizeof(T) <= 8;"); 25620 25621 // FIXME: This is misformatted because the fake l paren starts at bool, not at 25622 // the lambda l square. 25623 verifyFormat("template <typename T>\n" 25624 "concept C = [] -> bool { return true; }() && requires(T t) { " 25625 "t.bar(); } &&\n" 25626 " sizeof(T) <= 8;"); 25627 25628 verifyFormat( 25629 "template <typename T>\n" 25630 "concept C = decltype([]() { return std::true_type{}; }())::value &&\n" 25631 " requires(T t) { t.bar(); } && sizeof(T) <= 8;"); 25632 25633 verifyFormat("template <typename T>\n" 25634 "concept C = decltype([]() { return std::true_type{}; " 25635 "}())::value && requires(T t) { t.bar(); } && sizeof(T) <= 8;", 25636 getLLVMStyleWithColumns(120)); 25637 25638 verifyFormat("template <typename T>\n" 25639 "concept C = decltype([]() -> std::true_type { return {}; " 25640 "}())::value &&\n" 25641 " requires(T t) { t.bar(); } && sizeof(T) <= 8;"); 25642 25643 verifyFormat("template <typename T>\n" 25644 "concept C = true;\n" 25645 "Foo Bar;"); 25646 25647 verifyFormat("template <typename T>\n" 25648 "concept Hashable = requires(T a) {\n" 25649 " { std::hash<T>{}(a) } -> " 25650 "std::convertible_to<std::size_t>;\n" 25651 " };", 25652 Style); 25653 25654 verifyFormat( 25655 "template <typename T>\n" 25656 "concept EqualityComparable = requires(T a, T b) {\n" 25657 " { a == b } -> std::same_as<bool>;\n" 25658 " };", 25659 Style); 25660 25661 verifyFormat( 25662 "template <typename T>\n" 25663 "concept EqualityComparable = requires(T a, T b) {\n" 25664 " { a == b } -> std::same_as<bool>;\n" 25665 " { a != b } -> std::same_as<bool>;\n" 25666 " };", 25667 Style); 25668 25669 verifyFormat("template <typename T>\n" 25670 "concept WeakEqualityComparable = requires(T a, T b) {\n" 25671 " { a == b };\n" 25672 " { a != b };\n" 25673 " };", 25674 Style); 25675 25676 verifyFormat("template <typename T>\n" 25677 "concept HasSizeT = requires { typename T::size_t; };"); 25678 25679 verifyFormat("template <typename T>\n" 25680 "concept Semiregular =\n" 25681 " DefaultConstructible<T> && CopyConstructible<T> && " 25682 "CopyAssignable<T> &&\n" 25683 " requires(T a, std::size_t n) {\n" 25684 " requires Same<T *, decltype(&a)>;\n" 25685 " { a.~T() } noexcept;\n" 25686 " requires Same<T *, decltype(new T)>;\n" 25687 " requires Same<T *, decltype(new T[n])>;\n" 25688 " { delete new T; };\n" 25689 " { delete new T[n]; };\n" 25690 " };", 25691 Style); 25692 25693 verifyFormat("template <typename T>\n" 25694 "concept Semiregular =\n" 25695 " requires(T a, std::size_t n) {\n" 25696 " requires Same<T *, decltype(&a)>;\n" 25697 " { a.~T() } noexcept;\n" 25698 " requires Same<T *, decltype(new T)>;\n" 25699 " requires Same<T *, decltype(new T[n])>;\n" 25700 " { delete new T; };\n" 25701 " { delete new T[n]; };\n" 25702 " { new T } -> std::same_as<T *>;\n" 25703 " } && DefaultConstructible<T> && CopyConstructible<T> && " 25704 "CopyAssignable<T>;", 25705 Style); 25706 25707 verifyFormat( 25708 "template <typename T>\n" 25709 "concept Semiregular =\n" 25710 " DefaultConstructible<T> && requires(T a, std::size_t n) {\n" 25711 " requires Same<T *, decltype(&a)>;\n" 25712 " { a.~T() } noexcept;\n" 25713 " requires Same<T *, decltype(new T)>;\n" 25714 " requires Same<T *, decltype(new " 25715 "T[n])>;\n" 25716 " { delete new T; };\n" 25717 " { delete new T[n]; };\n" 25718 " } && CopyConstructible<T> && " 25719 "CopyAssignable<T>;", 25720 Style); 25721 25722 verifyFormat("template <typename T>\n" 25723 "concept Two = requires(T t) {\n" 25724 " { t.foo() } -> std::same_as<Bar>;\n" 25725 " } && requires(T &&t) {\n" 25726 " { t.foo() } -> std::same_as<Bar &&>;\n" 25727 " };", 25728 Style); 25729 25730 verifyFormat( 25731 "template <typename T>\n" 25732 "concept C = requires(T x) {\n" 25733 " { *x } -> std::convertible_to<typename T::inner>;\n" 25734 " { x + 1 } noexcept -> std::same_as<int>;\n" 25735 " { x * 1 } -> std::convertible_to<T>;\n" 25736 " };", 25737 Style); 25738 25739 verifyFormat("template <typename T>\n" 25740 "concept C = requires(T x) {\n" 25741 " {\n" 25742 " long_long_long_function_call(1, 2, 3, 4, 5)\n" 25743 " } -> long_long_concept_name<T>;\n" 25744 " {\n" 25745 " long_long_long_function_call(1, 2, 3, 4, 5)\n" 25746 " } noexcept -> long_long_concept_name<T>;\n" 25747 " };", 25748 Style); 25749 25750 verifyFormat( 25751 "template <typename T, typename U = T>\n" 25752 "concept Swappable = requires(T &&t, U &&u) {\n" 25753 " swap(std::forward<T>(t), std::forward<U>(u));\n" 25754 " swap(std::forward<U>(u), std::forward<T>(t));\n" 25755 " };", 25756 Style); 25757 25758 verifyFormat("template <typename T, typename U>\n" 25759 "concept Common = requires(T &&t, U &&u) {\n" 25760 " typename CommonType<T, U>;\n" 25761 " { CommonType<T, U>(std::forward<T>(t)) };\n" 25762 " };", 25763 Style); 25764 25765 verifyFormat("template <typename T, typename U>\n" 25766 "concept Common = requires(T &&t, U &&u) {\n" 25767 " typename CommonType<T, U>;\n" 25768 " { CommonType<T, U>{std::forward<T>(t)} };\n" 25769 " };", 25770 Style); 25771 25772 verifyFormat( 25773 "template <typename T>\n" 25774 "concept C = requires(T t) {\n" 25775 " requires Bar<T> && Foo<T>;\n" 25776 " requires((trait<T> && Baz) || (T2<T> && Foo<T>));\n" 25777 " };", 25778 Style); 25779 25780 verifyFormat("template <typename T>\n" 25781 "concept HasFoo = requires(T t) {\n" 25782 " { t.foo() };\n" 25783 " t.foo();\n" 25784 " };\n" 25785 "template <typename T>\n" 25786 "concept HasBar = requires(T t) {\n" 25787 " { t.bar() };\n" 25788 " t.bar();\n" 25789 " };", 25790 Style); 25791 25792 verifyFormat("template <typename T>\n" 25793 "concept Large = sizeof(T) > 10;"); 25794 25795 verifyFormat("template <typename T, typename U>\n" 25796 "concept FooableWith = requires(T t, U u) {\n" 25797 " typename T::foo_type;\n" 25798 " { t.foo(u) } -> typename T::foo_type;\n" 25799 " t++;\n" 25800 " };\n" 25801 "void doFoo(FooableWith<int> auto t) { t.foo(3); }", 25802 Style); 25803 25804 verifyFormat("template <typename T>\n" 25805 "concept Context = is_specialization_of_v<context, T>;"); 25806 25807 verifyFormat("template <typename T>\n" 25808 "concept Node = std::is_object_v<T>;"); 25809 25810 verifyFormat("template <class T>\n" 25811 "concept integral = __is_integral(T);"); 25812 25813 verifyFormat("template <class T>\n" 25814 "concept is2D = __array_extent(T, 1) == 2;"); 25815 25816 verifyFormat("template <class T>\n" 25817 "concept isRhs = __is_rvalue_expr(std::declval<T>() + 2)"); 25818 25819 verifyFormat("template <class T, class T2>\n" 25820 "concept Same = __is_same_as<T, T2>;"); 25821 25822 verifyFormat( 25823 "template <class _InIt, class _OutIt>\n" 25824 "concept _Can_reread_dest =\n" 25825 " std::forward_iterator<_OutIt> &&\n" 25826 " std::same_as<std::iter_value_t<_InIt>, std::iter_value_t<_OutIt>>;"); 25827 25828 Style.BreakBeforeConceptDeclarations = FormatStyle::BBCDS_Allowed; 25829 25830 verifyFormat( 25831 "template <typename T>\n" 25832 "concept C = requires(T t) {\n" 25833 " requires Bar<T> && Foo<T>;\n" 25834 " requires((trait<T> && Baz) || (T2<T> && Foo<T>));\n" 25835 " };", 25836 Style); 25837 25838 verifyFormat("template <typename T>\n" 25839 "concept HasFoo = requires(T t) {\n" 25840 " { t.foo() };\n" 25841 " t.foo();\n" 25842 " };\n" 25843 "template <typename T>\n" 25844 "concept HasBar = requires(T t) {\n" 25845 " { t.bar() };\n" 25846 " t.bar();\n" 25847 " };", 25848 Style); 25849 25850 verifyFormat("template <typename T> concept True = true;", Style); 25851 25852 verifyFormat("template <typename T>\n" 25853 "concept C = decltype([]() -> std::true_type { return {}; " 25854 "}())::value &&\n" 25855 " requires(T t) { t.bar(); } && sizeof(T) <= 8;", 25856 Style); 25857 25858 verifyFormat("template <typename T>\n" 25859 "concept Semiregular =\n" 25860 " DefaultConstructible<T> && CopyConstructible<T> && " 25861 "CopyAssignable<T> &&\n" 25862 " requires(T a, std::size_t n) {\n" 25863 " requires Same<T *, decltype(&a)>;\n" 25864 " { a.~T() } noexcept;\n" 25865 " requires Same<T *, decltype(new T)>;\n" 25866 " requires Same<T *, decltype(new T[n])>;\n" 25867 " { delete new T; };\n" 25868 " { delete new T[n]; };\n" 25869 " };", 25870 Style); 25871 25872 Style.BreakBeforeConceptDeclarations = FormatStyle::BBCDS_Never; 25873 25874 verifyFormat("template <typename T> concept C =\n" 25875 " requires(T t) {\n" 25876 " requires Bar<T> && Foo<T>;\n" 25877 " requires((trait<T> && Baz) || (T2<T> && Foo<T>));\n" 25878 " };", 25879 Style); 25880 25881 verifyFormat("template <typename T> concept HasFoo = requires(T t) {\n" 25882 " { t.foo() };\n" 25883 " t.foo();\n" 25884 " };\n" 25885 "template <typename T> concept HasBar = requires(T t) {\n" 25886 " { t.bar() };\n" 25887 " t.bar();\n" 25888 " };", 25889 Style); 25890 25891 verifyFormat("template <typename T> concept True = true;", Style); 25892 25893 verifyFormat( 25894 "template <typename T> concept C =\n" 25895 " decltype([]() -> std::true_type { return {}; }())::value &&\n" 25896 " requires(T t) { t.bar(); } && sizeof(T) <= 8;", 25897 Style); 25898 25899 verifyFormat("template <typename T> concept Semiregular =\n" 25900 " DefaultConstructible<T> && CopyConstructible<T> && " 25901 "CopyAssignable<T> &&\n" 25902 " requires(T a, std::size_t n) {\n" 25903 " requires Same<T *, decltype(&a)>;\n" 25904 " { a.~T() } noexcept;\n" 25905 " requires Same<T *, decltype(new T)>;\n" 25906 " requires Same<T *, decltype(new T[n])>;\n" 25907 " { delete new T; };\n" 25908 " { delete new T[n]; };\n" 25909 " };", 25910 Style); 25911 25912 // The following tests are invalid C++, we just want to make sure we don't 25913 // assert. 25914 verifyNoCrash("template <typename T>\n" 25915 "concept C = requires C2<T>;"); 25916 25917 verifyNoCrash("template <typename T>\n" 25918 "concept C = 5 + 4;"); 25919 25920 verifyNoCrash("template <typename T>\n" 25921 "concept C = class X;"); 25922 25923 verifyNoCrash("template <typename T>\n" 25924 "concept C = [] && true;"); 25925 25926 verifyNoCrash("template <typename T>\n" 25927 "concept C = [] && requires(T t) { typename T::size_type; };"); 25928 } 25929 25930 TEST_F(FormatTest, RequiresClausesPositions) { 25931 auto Style = getLLVMStyle(); 25932 EXPECT_EQ(Style.RequiresClausePosition, FormatStyle::RCPS_OwnLine); 25933 EXPECT_EQ(Style.IndentRequiresClause, true); 25934 25935 // The default in LLVM style is REI_OuterScope, but these tests were written 25936 // when the default was REI_Keyword. 25937 Style.RequiresExpressionIndentation = FormatStyle::REI_Keyword; 25938 25939 verifyFormat("template <typename T>\n" 25940 " requires(Foo<T> && std::trait<T>)\n" 25941 "struct Bar;", 25942 Style); 25943 25944 verifyFormat("template <typename T>\n" 25945 " requires(Foo<T> && std::trait<T>)\n" 25946 "class Bar {\n" 25947 "public:\n" 25948 " Bar(T t);\n" 25949 " bool baz();\n" 25950 "};", 25951 Style); 25952 25953 verifyFormat( 25954 "template <typename T>\n" 25955 " requires requires(T &&t) {\n" 25956 " typename T::I;\n" 25957 " requires(F<typename T::I> && std::trait<typename T::I>);\n" 25958 " }\n" 25959 "Bar(T) -> Bar<typename T::I>;", 25960 Style); 25961 25962 verifyFormat("template <typename T>\n" 25963 " requires(Foo<T> && std::trait<T>)\n" 25964 "constexpr T MyGlobal;", 25965 Style); 25966 25967 verifyFormat("template <typename T>\n" 25968 " requires Foo<T> && requires(T t) {\n" 25969 " { t.baz() } -> std::same_as<bool>;\n" 25970 " requires std::same_as<T::Factor, int>;\n" 25971 " }\n" 25972 "inline int bar(T t) {\n" 25973 " return t.baz() ? T::Factor : 5;\n" 25974 "}", 25975 Style); 25976 25977 verifyFormat("template <typename T>\n" 25978 "inline int bar(T t)\n" 25979 " requires Foo<T> && requires(T t) {\n" 25980 " { t.baz() } -> std::same_as<bool>;\n" 25981 " requires std::same_as<T::Factor, int>;\n" 25982 " }\n" 25983 "{\n" 25984 " return t.baz() ? T::Factor : 5;\n" 25985 "}", 25986 Style); 25987 25988 verifyFormat("template <typename T>\n" 25989 " requires F<T>\n" 25990 "int bar(T t) {\n" 25991 " return 5;\n" 25992 "}", 25993 Style); 25994 25995 verifyFormat("template <typename T>\n" 25996 "int bar(T t)\n" 25997 " requires F<T>\n" 25998 "{\n" 25999 " return 5;\n" 26000 "}", 26001 Style); 26002 26003 verifyFormat("template <typename T>\n" 26004 "int S::bar(T t) &&\n" 26005 " requires F<T>\n" 26006 "{\n" 26007 " return 5;\n" 26008 "}", 26009 Style); 26010 26011 verifyFormat("template <typename T>\n" 26012 "int bar(T t)\n" 26013 " requires F<T>;", 26014 Style); 26015 26016 Style.IndentRequiresClause = false; 26017 verifyFormat("template <typename T>\n" 26018 "requires F<T>\n" 26019 "int bar(T t) {\n" 26020 " return 5;\n" 26021 "}", 26022 Style); 26023 26024 verifyFormat("template <typename T>\n" 26025 "int S::bar(T t) &&\n" 26026 "requires F<T>\n" 26027 "{\n" 26028 " return 5;\n" 26029 "}", 26030 Style); 26031 26032 verifyFormat("template <typename T>\n" 26033 "int bar(T t)\n" 26034 "requires F<T>\n" 26035 "{\n" 26036 " return 5;\n" 26037 "}", 26038 Style); 26039 26040 Style.RequiresClausePosition = FormatStyle::RCPS_OwnLineWithBrace; 26041 Style.IndentRequiresClause = true; 26042 26043 verifyFormat("template <typename T>\n" 26044 " requires(Foo<T> && std::trait<T>)\n" 26045 "struct Bar;", 26046 Style); 26047 26048 verifyFormat("template <typename T>\n" 26049 " requires(Foo<T> && std::trait<T>)\n" 26050 "class Bar {\n" 26051 "public:\n" 26052 " Bar(T t);\n" 26053 " bool baz();\n" 26054 "};", 26055 Style); 26056 26057 verifyFormat( 26058 "template <typename T>\n" 26059 " requires requires(T &&t) {\n" 26060 " typename T::I;\n" 26061 " requires(F<typename T::I> && std::trait<typename T::I>);\n" 26062 " }\n" 26063 "Bar(T) -> Bar<typename T::I>;", 26064 Style); 26065 26066 verifyFormat("template <typename T>\n" 26067 " requires(Foo<T> && std::trait<T>)\n" 26068 "constexpr T MyGlobal;", 26069 Style); 26070 26071 verifyFormat("template <typename T>\n" 26072 " requires Foo<T> && requires(T t) {\n" 26073 " { t.baz() } -> std::same_as<bool>;\n" 26074 " requires std::same_as<T::Factor, int>;\n" 26075 " }\n" 26076 "inline int bar(T t) {\n" 26077 " return t.baz() ? T::Factor : 5;\n" 26078 "}", 26079 Style); 26080 26081 verifyFormat("template <typename T>\n" 26082 "inline int bar(T t)\n" 26083 " requires Foo<T> && requires(T t) {\n" 26084 " { t.baz() } -> std::same_as<bool>;\n" 26085 " requires std::same_as<T::Factor, int>;\n" 26086 " } {\n" 26087 " return t.baz() ? T::Factor : 5;\n" 26088 "}", 26089 Style); 26090 26091 verifyFormat("template <typename T>\n" 26092 " requires F<T>\n" 26093 "int bar(T t) {\n" 26094 " return 5;\n" 26095 "}", 26096 Style); 26097 26098 verifyFormat("template <typename T>\n" 26099 "int bar(T t)\n" 26100 " requires F<T> {\n" 26101 " return 5;\n" 26102 "}", 26103 Style); 26104 26105 verifyFormat("template <typename T>\n" 26106 "int S::bar(T t) &&\n" 26107 " requires F<T> {\n" 26108 " return 5;\n" 26109 "}", 26110 Style); 26111 26112 verifyFormat("template <typename T>\n" 26113 "int bar(T t)\n" 26114 " requires F<T>;", 26115 Style); 26116 26117 verifyFormat("template <typename T>\n" 26118 "int bar(T t)\n" 26119 " requires F<T> {}", 26120 Style); 26121 26122 Style.RequiresClausePosition = FormatStyle::RCPS_SingleLine; 26123 Style.IndentRequiresClause = false; 26124 verifyFormat("template <typename T> requires Foo<T> struct Bar {};\n" 26125 "template <typename T> requires Foo<T> void bar() {}\n" 26126 "template <typename T> void bar() requires Foo<T> {}\n" 26127 "template <typename T> void bar() requires Foo<T>;\n" 26128 "template <typename T> void S::bar() && requires Foo<T> {}\n" 26129 "template <typename T> requires Foo<T> Bar(T) -> Bar<T>;", 26130 Style); 26131 26132 auto ColumnStyle = Style; 26133 ColumnStyle.ColumnLimit = 40; 26134 verifyFormat("template <typename AAAAAAA>\n" 26135 "requires Foo<T> struct Bar {};\n" 26136 "template <typename AAAAAAA>\n" 26137 "requires Foo<T> void bar() {}\n" 26138 "template <typename AAAAAAA>\n" 26139 "void bar() requires Foo<T> {}\n" 26140 "template <typename T>\n" 26141 "void S::bar() && requires Foo<T> {}\n" 26142 "template <typename AAAAAAA>\n" 26143 "requires Foo<T> Baz(T) -> Baz<T>;", 26144 ColumnStyle); 26145 26146 verifyFormat("template <typename T>\n" 26147 "requires Foo<AAAAAAA> struct Bar {};\n" 26148 "template <typename T>\n" 26149 "requires Foo<AAAAAAA> void bar() {}\n" 26150 "template <typename T>\n" 26151 "void bar() requires Foo<AAAAAAA> {}\n" 26152 "template <typename T>\n" 26153 "requires Foo<AAAAAAA> Bar(T) -> Bar<T>;", 26154 ColumnStyle); 26155 26156 verifyFormat("template <typename AAAAAAA>\n" 26157 "requires Foo<AAAAAAAAAAAAAAAA>\n" 26158 "struct Bar {};\n" 26159 "template <typename AAAAAAA>\n" 26160 "requires Foo<AAAAAAAAAAAAAAAA>\n" 26161 "void bar() {}\n" 26162 "template <typename AAAAAAA>\n" 26163 "void bar()\n" 26164 " requires Foo<AAAAAAAAAAAAAAAA> {}\n" 26165 "template <typename AAAAAAA>\n" 26166 "requires Foo<AAAAAAAA> Bar(T) -> Bar<T>;\n" 26167 "template <typename AAAAAAA>\n" 26168 "requires Foo<AAAAAAAAAAAAAAAA>\n" 26169 "Bar(T) -> Bar<T>;", 26170 ColumnStyle); 26171 26172 Style.RequiresClausePosition = FormatStyle::RCPS_WithFollowing; 26173 ColumnStyle.RequiresClausePosition = FormatStyle::RCPS_WithFollowing; 26174 26175 verifyFormat("template <typename T>\n" 26176 "requires Foo<T> struct Bar {};\n" 26177 "template <typename T>\n" 26178 "requires Foo<T> void bar() {}\n" 26179 "template <typename T>\n" 26180 "void bar()\n" 26181 "requires Foo<T> {}\n" 26182 "template <typename T>\n" 26183 "void bar()\n" 26184 "requires Foo<T>;\n" 26185 "template <typename T>\n" 26186 "void S::bar() &&\n" 26187 "requires Foo<T> {}\n" 26188 "template <typename T>\n" 26189 "requires Foo<T> Bar(T) -> Bar<T>;", 26190 Style); 26191 26192 verifyFormat("template <typename AAAAAAA>\n" 26193 "requires Foo<AAAAAAAAAAAAAAAA>\n" 26194 "struct Bar {};\n" 26195 "template <typename AAAAAAA>\n" 26196 "requires Foo<AAAAAAAAAAAAAAAA>\n" 26197 "void bar() {}\n" 26198 "template <typename AAAAAAA>\n" 26199 "void bar()\n" 26200 "requires Foo<AAAAAAAAAAAAAAAA> {}\n" 26201 "template <typename AAAAAAA>\n" 26202 "requires Foo<AAAAAAAA> Bar(T) -> Bar<T>;\n" 26203 "template <typename AAAAAAA>\n" 26204 "requires Foo<AAAAAAAAAAAAAAAA>\n" 26205 "Bar(T) -> Bar<T>;", 26206 ColumnStyle); 26207 26208 Style.IndentRequiresClause = true; 26209 ColumnStyle.IndentRequiresClause = true; 26210 26211 verifyFormat("template <typename T>\n" 26212 " requires Foo<T> struct Bar {};\n" 26213 "template <typename T>\n" 26214 " requires Foo<T> void bar() {}\n" 26215 "template <typename T>\n" 26216 "void bar()\n" 26217 " requires Foo<T> {}\n" 26218 "template <typename T>\n" 26219 "void S::bar() &&\n" 26220 " requires Foo<T> {}\n" 26221 "template <typename T>\n" 26222 " requires Foo<T> Bar(T) -> Bar<T>;", 26223 Style); 26224 26225 verifyFormat("template <typename AAAAAAA>\n" 26226 " requires Foo<AAAAAAAAAAAAAAAA>\n" 26227 "struct Bar {};\n" 26228 "template <typename AAAAAAA>\n" 26229 " requires Foo<AAAAAAAAAAAAAAAA>\n" 26230 "void bar() {}\n" 26231 "template <typename AAAAAAA>\n" 26232 "void bar()\n" 26233 " requires Foo<AAAAAAAAAAAAAAAA> {}\n" 26234 "template <typename AAAAAAA>\n" 26235 " requires Foo<AAAAAA> Bar(T) -> Bar<T>;\n" 26236 "template <typename AAAAAAA>\n" 26237 " requires Foo<AAAAAAAAAAAAAAAA>\n" 26238 "Bar(T) -> Bar<T>;", 26239 ColumnStyle); 26240 26241 Style.RequiresClausePosition = FormatStyle::RCPS_WithPreceding; 26242 ColumnStyle.RequiresClausePosition = FormatStyle::RCPS_WithPreceding; 26243 26244 verifyFormat("template <typename T> requires Foo<T>\n" 26245 "struct Bar {};\n" 26246 "template <typename T> requires Foo<T>\n" 26247 "void bar() {}\n" 26248 "template <typename T>\n" 26249 "void bar() requires Foo<T>\n" 26250 "{}\n" 26251 "template <typename T> void bar() requires Foo<T>;\n" 26252 "template <typename T>\n" 26253 "void S::bar() && requires Foo<T>\n" 26254 "{}\n" 26255 "template <typename T> requires Foo<T>\n" 26256 "Bar(T) -> Bar<T>;", 26257 Style); 26258 26259 verifyFormat("template <typename AAAAAAA>\n" 26260 "requires Foo<AAAAAAAAAAAAAAAA>\n" 26261 "struct Bar {};\n" 26262 "template <typename AAAAAAA>\n" 26263 "requires Foo<AAAAAAAAAAAAAAAA>\n" 26264 "void bar() {}\n" 26265 "template <typename AAAAAAA>\n" 26266 "void bar()\n" 26267 " requires Foo<AAAAAAAAAAAAAAAA>\n" 26268 "{}\n" 26269 "template <typename AAAAAAA>\n" 26270 "requires Foo<AAAAAAAA>\n" 26271 "Bar(T) -> Bar<T>;\n" 26272 "template <typename AAAAAAA>\n" 26273 "requires Foo<AAAAAAAAAAAAAAAA>\n" 26274 "Bar(T) -> Bar<T>;", 26275 ColumnStyle); 26276 } 26277 26278 TEST_F(FormatTest, RequiresClauses) { 26279 verifyFormat("struct [[nodiscard]] zero_t {\n" 26280 " template <class T>\n" 26281 " requires requires { number_zero_v<T>; }\n" 26282 " [[nodiscard]] constexpr operator T() const {\n" 26283 " return number_zero_v<T>;\n" 26284 " }\n" 26285 "};"); 26286 26287 verifyFormat("template <class T>\n" 26288 " requires(std::same_as<int, T>)\n" 26289 "decltype(auto) fun() {}"); 26290 26291 auto Style = getLLVMStyle(); 26292 26293 verifyFormat( 26294 "template <typename T>\n" 26295 " requires is_default_constructible_v<hash<T>> and\n" 26296 " is_copy_constructible_v<hash<T>> and\n" 26297 " is_move_constructible_v<hash<T>> and\n" 26298 " is_copy_assignable_v<hash<T>> and " 26299 "is_move_assignable_v<hash<T>> and\n" 26300 " is_destructible_v<hash<T>> and is_swappable_v<hash<T>> and\n" 26301 " is_callable_v<hash<T>(T)> and\n" 26302 " is_same_v<size_t, decltype(hash<T>(declval<T>()))> and\n" 26303 " is_same_v<size_t, decltype(hash<T>(declval<T &>()))> and\n" 26304 " is_same_v<size_t, decltype(hash<T>(declval<const T &>()))>\n" 26305 "struct S {};", 26306 Style); 26307 26308 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 26309 verifyFormat( 26310 "template <typename T>\n" 26311 " requires is_default_constructible_v<hash<T>>\n" 26312 " and is_copy_constructible_v<hash<T>>\n" 26313 " and is_move_constructible_v<hash<T>>\n" 26314 " and is_copy_assignable_v<hash<T>> and " 26315 "is_move_assignable_v<hash<T>>\n" 26316 " and is_destructible_v<hash<T>> and is_swappable_v<hash<T>>\n" 26317 " and is_callable_v<hash<T>(T)>\n" 26318 " and is_same_v<size_t, decltype(hash<T>(declval<T>()))>\n" 26319 " and is_same_v<size_t, decltype(hash<T>(declval<T &>()))>\n" 26320 " and is_same_v<size_t, decltype(hash<T>(declval<const T " 26321 "&>()))>\n" 26322 "struct S {};", 26323 Style); 26324 26325 Style = getLLVMStyle(); 26326 Style.ConstructorInitializerIndentWidth = 4; 26327 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 26328 Style.PackConstructorInitializers = FormatStyle::PCIS_Never; 26329 verifyFormat("constexpr Foo(Foo const &other)\n" 26330 " requires std::is_copy_constructible<T>\n" 26331 " : value{other.value} {\n" 26332 " do_magic();\n" 26333 " do_more_magic();\n" 26334 "}", 26335 Style); 26336 26337 // Not a clause, but we once hit an assert. 26338 verifyFormat("#if 0\n" 26339 "#else\n" 26340 "foo();\n" 26341 "#endif\n" 26342 "bar(requires);"); 26343 } 26344 26345 TEST_F(FormatTest, RequiresExpressionIndentation) { 26346 auto Style = getLLVMStyle(); 26347 EXPECT_EQ(Style.RequiresExpressionIndentation, FormatStyle::REI_OuterScope); 26348 26349 verifyFormat("template <typename T>\n" 26350 "concept C = requires(T t) {\n" 26351 " typename T::value;\n" 26352 " requires requires(typename T::value v) {\n" 26353 " { t == v } -> std::same_as<bool>;\n" 26354 " };\n" 26355 "};", 26356 Style); 26357 26358 verifyFormat("template <typename T>\n" 26359 "void bar(T)\n" 26360 " requires Foo<T> && requires(T t) {\n" 26361 " { t.foo() } -> std::same_as<int>;\n" 26362 " } && requires(T t) {\n" 26363 " { t.bar() } -> std::same_as<bool>;\n" 26364 " --t;\n" 26365 " };", 26366 Style); 26367 26368 verifyFormat("template <typename T>\n" 26369 " requires Foo<T> &&\n" 26370 " requires(T t) {\n" 26371 " { t.foo() } -> std::same_as<int>;\n" 26372 " } && requires(T t) {\n" 26373 " { t.bar() } -> std::same_as<bool>;\n" 26374 " --t;\n" 26375 " }\n" 26376 "void bar(T);", 26377 Style); 26378 26379 verifyFormat("template <typename T> void f() {\n" 26380 " if constexpr (requires(T t) {\n" 26381 " { t.bar() } -> std::same_as<bool>;\n" 26382 " }) {\n" 26383 " }\n" 26384 "}", 26385 Style); 26386 26387 verifyFormat("template <typename T> void f() {\n" 26388 " if constexpr (condition && requires(T t) {\n" 26389 " { t.bar() } -> std::same_as<bool>;\n" 26390 " }) {\n" 26391 " }\n" 26392 "}", 26393 Style); 26394 26395 verifyFormat("template <typename T> struct C {\n" 26396 " void f()\n" 26397 " requires requires(T t) {\n" 26398 " { t.bar() } -> std::same_as<bool>;\n" 26399 " };\n" 26400 "};", 26401 Style); 26402 26403 Style.RequiresExpressionIndentation = FormatStyle::REI_Keyword; 26404 26405 verifyFormat("template <typename T>\n" 26406 "concept C = requires(T t) {\n" 26407 " typename T::value;\n" 26408 " requires requires(typename T::value v) {\n" 26409 " { t == v } -> std::same_as<bool>;\n" 26410 " };\n" 26411 " };", 26412 Style); 26413 26414 verifyFormat( 26415 "template <typename T>\n" 26416 "void bar(T)\n" 26417 " requires Foo<T> && requires(T t) {\n" 26418 " { t.foo() } -> std::same_as<int>;\n" 26419 " } && requires(T t) {\n" 26420 " { t.bar() } -> std::same_as<bool>;\n" 26421 " --t;\n" 26422 " };", 26423 Style); 26424 26425 verifyFormat("template <typename T>\n" 26426 " requires Foo<T> &&\n" 26427 " requires(T t) {\n" 26428 " { t.foo() } -> std::same_as<int>;\n" 26429 " } && requires(T t) {\n" 26430 " { t.bar() } -> std::same_as<bool>;\n" 26431 " --t;\n" 26432 " }\n" 26433 "void bar(T);", 26434 Style); 26435 26436 verifyFormat("template <typename T> void f() {\n" 26437 " if constexpr (requires(T t) {\n" 26438 " { t.bar() } -> std::same_as<bool>;\n" 26439 " }) {\n" 26440 " }\n" 26441 "}", 26442 Style); 26443 26444 verifyFormat( 26445 "template <typename T> void f() {\n" 26446 " if constexpr (condition && requires(T t) {\n" 26447 " { t.bar() } -> std::same_as<bool>;\n" 26448 " }) {\n" 26449 " }\n" 26450 "}", 26451 Style); 26452 26453 verifyFormat("template <typename T> struct C {\n" 26454 " void f()\n" 26455 " requires requires(T t) {\n" 26456 " { t.bar() } -> std::same_as<bool>;\n" 26457 " };\n" 26458 "};", 26459 Style); 26460 } 26461 26462 TEST_F(FormatTest, StatementAttributeLikeMacros) { 26463 FormatStyle Style = getLLVMStyle(); 26464 StringRef Source = "void Foo::slot() {\n" 26465 " unsigned char MyChar = 'x';\n" 26466 " emit signal(MyChar);\n" 26467 " Q_EMIT signal(MyChar);\n" 26468 "}"; 26469 26470 verifyFormat(Source, Style); 26471 26472 Style.AlignConsecutiveDeclarations.Enabled = true; 26473 verifyFormat("void Foo::slot() {\n" 26474 " unsigned char MyChar = 'x';\n" 26475 " emit signal(MyChar);\n" 26476 " Q_EMIT signal(MyChar);\n" 26477 "}", 26478 Source, Style); 26479 26480 Style.StatementAttributeLikeMacros.push_back("emit"); 26481 verifyFormat(Source, Style); 26482 26483 Style.StatementAttributeLikeMacros = {}; 26484 verifyFormat("void Foo::slot() {\n" 26485 " unsigned char MyChar = 'x';\n" 26486 " emit signal(MyChar);\n" 26487 " Q_EMIT signal(MyChar);\n" 26488 "}", 26489 Source, Style); 26490 } 26491 26492 TEST_F(FormatTest, IndentAccessModifiers) { 26493 FormatStyle Style = getLLVMStyle(); 26494 Style.IndentAccessModifiers = true; 26495 // Members are *two* levels below the record; 26496 // Style.IndentWidth == 2, thus yielding a 4 spaces wide indentation. 26497 verifyFormat("class C {\n" 26498 " int i;\n" 26499 "};", 26500 Style); 26501 verifyFormat("union C {\n" 26502 " int i;\n" 26503 " unsigned u;\n" 26504 "};", 26505 Style); 26506 // Access modifiers should be indented one level below the record. 26507 verifyFormat("class C {\n" 26508 " public:\n" 26509 " int i;\n" 26510 "};", 26511 Style); 26512 verifyFormat("class C {\n" 26513 " public /* comment */:\n" 26514 " int i;\n" 26515 "};", 26516 Style); 26517 verifyFormat("struct S {\n" 26518 " private:\n" 26519 " class C {\n" 26520 " int j;\n" 26521 "\n" 26522 " public:\n" 26523 " C();\n" 26524 " };\n" 26525 "\n" 26526 " public:\n" 26527 " int i;\n" 26528 "};", 26529 Style); 26530 // Enumerations are not records and should be unaffected. 26531 Style.AllowShortEnumsOnASingleLine = false; 26532 verifyFormat("enum class E {\n" 26533 " A,\n" 26534 " B\n" 26535 "};", 26536 Style); 26537 // Test with a different indentation width; 26538 // also proves that the result is Style.AccessModifierOffset agnostic. 26539 Style.IndentWidth = 3; 26540 verifyFormat("class C {\n" 26541 " public:\n" 26542 " int i;\n" 26543 "};", 26544 Style); 26545 verifyFormat("class C {\n" 26546 " public /**/:\n" 26547 " int i;\n" 26548 "};", 26549 Style); 26550 Style.AttributeMacros.push_back("FOO"); 26551 verifyFormat("class C {\n" 26552 " FOO public:\n" 26553 " int i;\n" 26554 "};", 26555 Style); 26556 } 26557 26558 TEST_F(FormatTest, LimitlessStringsAndComments) { 26559 auto Style = getLLVMStyleWithColumns(0); 26560 constexpr StringRef Code = 26561 "/**\n" 26562 " * This is a multiline comment with quite some long lines, at least for " 26563 "the LLVM Style.\n" 26564 " * We will redo this with strings and line comments. Just to check if " 26565 "everything is working.\n" 26566 " */\n" 26567 "bool foo() {\n" 26568 " /* Single line multi line comment. */\n" 26569 " const std::string String = \"This is a multiline string with quite " 26570 "some long lines, at least for the LLVM Style.\"\n" 26571 " \"We already did it with multi line " 26572 "comments, and we will do it with line comments. Just to check if " 26573 "everything is working.\";\n" 26574 " // This is a line comment (block) with quite some long lines, at " 26575 "least for the LLVM Style.\n" 26576 " // We already did this with multi line comments and strings. Just to " 26577 "check if everything is working.\n" 26578 " const std::string SmallString = \"Hello World\";\n" 26579 " // Small line comment\n" 26580 " return String.size() > SmallString.size();\n" 26581 "}"; 26582 verifyNoChange(Code, Style); 26583 } 26584 26585 TEST_F(FormatTest, FormatDecayCopy) { 26586 // error cases from unit tests 26587 verifyFormat("foo(auto())"); 26588 verifyFormat("foo(auto{})"); 26589 verifyFormat("foo(auto({}))"); 26590 verifyFormat("foo(auto{{}})"); 26591 26592 verifyFormat("foo(auto(1))"); 26593 verifyFormat("foo(auto{1})"); 26594 verifyFormat("foo(new auto(1))"); 26595 verifyFormat("foo(new auto{1})"); 26596 verifyFormat("decltype(auto(1)) x;"); 26597 verifyFormat("decltype(auto{1}) x;"); 26598 verifyFormat("auto(x);"); 26599 verifyFormat("auto{x};"); 26600 verifyFormat("new auto{x};"); 26601 verifyFormat("auto{x} = y;"); 26602 verifyFormat("auto(x) = y;"); // actually a declaration, but this is clearly 26603 // the user's own fault 26604 verifyFormat("integral auto(x) = y;"); // actually a declaration, but this is 26605 // clearly the user's own fault 26606 verifyFormat("auto (*p)() = f;"); 26607 } 26608 26609 TEST_F(FormatTest, Cpp20ModulesSupport) { 26610 FormatStyle Style = getLLVMStyle(); 26611 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never; 26612 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 26613 26614 verifyFormat("export import foo;", Style); 26615 verifyFormat("export import foo:bar;", Style); 26616 verifyFormat("export import foo.bar;", Style); 26617 verifyFormat("export import foo.bar:baz;", Style); 26618 verifyFormat("export import :bar;", Style); 26619 verifyFormat("export module foo:bar;", Style); 26620 verifyFormat("export module foo;", Style); 26621 verifyFormat("export module foo.bar;", Style); 26622 verifyFormat("export module foo.bar:baz;", Style); 26623 verifyFormat("export import <string_view>;", Style); 26624 verifyFormat("export import <Foo/Bar>;", Style); 26625 26626 verifyFormat("export type_name var;", Style); 26627 verifyFormat("template <class T> export using A = B<T>;", Style); 26628 verifyFormat("export using A = B;", Style); 26629 verifyFormat("export int func() {\n" 26630 " foo();\n" 26631 "}", 26632 Style); 26633 verifyFormat("export struct {\n" 26634 " int foo;\n" 26635 "};", 26636 Style); 26637 verifyFormat("export {\n" 26638 " int foo;\n" 26639 "};", 26640 Style); 26641 verifyFormat("export export char const *hello() { return \"hello\"; }"); 26642 26643 verifyFormat("import bar;", Style); 26644 verifyFormat("import foo.bar;", Style); 26645 verifyFormat("import foo:bar;", Style); 26646 verifyFormat("import :bar;", Style); 26647 verifyFormat("import /* module partition */ :bar;", Style); 26648 verifyFormat("import <ctime>;", Style); 26649 verifyFormat("import \"header\";", Style); 26650 26651 verifyFormat("module foo;", Style); 26652 verifyFormat("module foo:bar;", Style); 26653 verifyFormat("module foo.bar;", Style); 26654 verifyFormat("module;", Style); 26655 26656 verifyFormat("export namespace hi {\n" 26657 "const char *sayhi();\n" 26658 "}", 26659 Style); 26660 26661 verifyFormat("module :private;", Style); 26662 verifyFormat("import <foo/bar.h>;", Style); 26663 verifyFormat("import foo...bar;", Style); 26664 verifyFormat("import ..........;", Style); 26665 verifyFormat("module foo:private;", Style); 26666 verifyFormat("import a", Style); 26667 verifyFormat("module a", Style); 26668 verifyFormat("export import a", Style); 26669 verifyFormat("export module a", Style); 26670 26671 verifyFormat("import", Style); 26672 verifyFormat("module", Style); 26673 verifyFormat("export", Style); 26674 26675 verifyFormat("import /* not keyword */ = val ? 2 : 1;"); 26676 } 26677 26678 TEST_F(FormatTest, CoroutineForCoawait) { 26679 FormatStyle Style = getLLVMStyle(); 26680 verifyFormat("for co_await (auto x : range())\n ;"); 26681 verifyFormat("for (auto i : arr) {\n" 26682 "}", 26683 Style); 26684 verifyFormat("for co_await (auto i : arr) {\n" 26685 "}", 26686 Style); 26687 verifyFormat("for co_await (auto i : foo(T{})) {\n" 26688 "}", 26689 Style); 26690 } 26691 26692 TEST_F(FormatTest, CoroutineCoAwait) { 26693 verifyFormat("int x = co_await foo();"); 26694 verifyFormat("int x = (co_await foo());"); 26695 verifyFormat("co_await (42);"); 26696 verifyFormat("void operator co_await(int);"); 26697 verifyFormat("void operator co_await(a);"); 26698 verifyFormat("co_await a;"); 26699 verifyFormat("co_await missing_await_resume{};"); 26700 verifyFormat("co_await a; // comment"); 26701 verifyFormat("void test0() { co_await a; }"); 26702 verifyFormat("co_await co_await co_await foo();"); 26703 verifyFormat("co_await foo().bar();"); 26704 verifyFormat("co_await [this]() -> Task { co_return x; }"); 26705 verifyFormat("co_await [this](int a, int b) -> Task { co_return co_await " 26706 "foo(); }(x, y);"); 26707 26708 FormatStyle Style = getLLVMStyleWithColumns(40); 26709 verifyFormat("co_await [this](int a, int b) -> Task {\n" 26710 " co_return co_await foo();\n" 26711 "}(x, y);", 26712 Style); 26713 verifyFormat("co_await;"); 26714 } 26715 26716 TEST_F(FormatTest, CoroutineCoYield) { 26717 verifyFormat("int x = co_yield foo();"); 26718 verifyFormat("int x = (co_yield foo());"); 26719 verifyFormat("co_yield (42);"); 26720 verifyFormat("co_yield {42};"); 26721 verifyFormat("co_yield 42;"); 26722 verifyFormat("co_yield n++;"); 26723 verifyFormat("co_yield ++n;"); 26724 verifyFormat("co_yield;"); 26725 } 26726 26727 TEST_F(FormatTest, CoroutineCoReturn) { 26728 verifyFormat("co_return (42);"); 26729 verifyFormat("co_return;"); 26730 verifyFormat("co_return {};"); 26731 verifyFormat("co_return x;"); 26732 verifyFormat("co_return co_await foo();"); 26733 verifyFormat("co_return co_yield foo();"); 26734 } 26735 26736 TEST_F(FormatTest, EmptyShortBlock) { 26737 auto Style = getLLVMStyle(); 26738 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty; 26739 26740 verifyFormat("try {\n" 26741 " doA();\n" 26742 "} catch (Exception &e) {\n" 26743 " e.printStackTrace();\n" 26744 "}", 26745 Style); 26746 26747 verifyFormat("try {\n" 26748 " doA();\n" 26749 "} catch (Exception &e) {}", 26750 Style); 26751 } 26752 26753 TEST_F(FormatTest, ShortTemplatedArgumentLists) { 26754 auto Style = getLLVMStyle(); 26755 26756 verifyFormat("template <> struct S : Template<int (*)[]> {};", Style); 26757 verifyFormat("template <> struct S : Template<int (*)[10]> {};", Style); 26758 verifyFormat("struct Y : X<[] { return 0; }> {};", Style); 26759 verifyFormat("struct Y<[] { return 0; }> {};", Style); 26760 26761 verifyFormat("struct Z : X<decltype([] { return 0; }){}> {};", Style); 26762 verifyFormat("template <int N> struct Foo<char[N]> {};", Style); 26763 } 26764 26765 TEST_F(FormatTest, MultilineLambdaInConditional) { 26766 auto Style = getLLVMStyleWithColumns(70); 26767 verifyFormat("auto aLengthyIdentifier = oneExpressionSoThatWeBreak ? []() {\n" 26768 " ;\n" 26769 " return 5;\n" 26770 "}()\n" 26771 " : 2;", 26772 Style); 26773 verifyFormat( 26774 "auto aLengthyIdentifier = oneExpressionSoThatWeBreak ? 2 : []() {\n" 26775 " ;\n" 26776 " return 5;\n" 26777 "}();", 26778 Style); 26779 26780 Style = getLLVMStyleWithColumns(60); 26781 verifyFormat("auto aLengthyIdentifier = oneExpressionSoThatWeBreak\n" 26782 " ? []() {\n" 26783 " ;\n" 26784 " return 5;\n" 26785 " }()\n" 26786 " : 2;", 26787 Style); 26788 verifyFormat("auto aLengthyIdentifier =\n" 26789 " oneExpressionSoThatWeBreak ? 2 : []() {\n" 26790 " ;\n" 26791 " return 5;\n" 26792 " }();", 26793 Style); 26794 26795 Style = getLLVMStyleWithColumns(40); 26796 verifyFormat("auto aLengthyIdentifier =\n" 26797 " oneExpressionSoThatWeBreak ? []() {\n" 26798 " ;\n" 26799 " return 5;\n" 26800 " }()\n" 26801 " : 2;", 26802 Style); 26803 verifyFormat("auto aLengthyIdentifier =\n" 26804 " oneExpressionSoThatWeBreak\n" 26805 " ? 2\n" 26806 " : []() {\n" 26807 " ;\n" 26808 " return 5;\n" 26809 " };", 26810 Style); 26811 } 26812 26813 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndent) { 26814 auto Style = getLLVMStyle(); 26815 26816 StringRef Short = "functionCall(paramA, paramB, paramC);\n" 26817 "void functionDecl(int a, int b, int c);"; 26818 26819 StringRef Medium = "functionCall(paramA, paramB, paramC, paramD, paramE, " 26820 "paramF, paramG, paramH, paramI);\n" 26821 "void functionDecl(int argumentA, int argumentB, int " 26822 "argumentC, int argumentD, int argumentE);"; 26823 26824 verifyFormat(Short, Style); 26825 26826 StringRef NoBreak = "functionCall(paramA, paramB, paramC, paramD, paramE, " 26827 "paramF, paramG, paramH,\n" 26828 " paramI);\n" 26829 "void functionDecl(int argumentA, int argumentB, int " 26830 "argumentC, int argumentD,\n" 26831 " int argumentE);"; 26832 26833 verifyFormat(NoBreak, Medium, Style); 26834 verifyFormat(NoBreak, 26835 "functionCall(\n" 26836 " paramA,\n" 26837 " paramB,\n" 26838 " paramC,\n" 26839 " paramD,\n" 26840 " paramE,\n" 26841 " paramF,\n" 26842 " paramG,\n" 26843 " paramH,\n" 26844 " paramI\n" 26845 ");\n" 26846 "void functionDecl(\n" 26847 " int argumentA,\n" 26848 " int argumentB,\n" 26849 " int argumentC,\n" 26850 " int argumentD,\n" 26851 " int argumentE\n" 26852 ");", 26853 Style); 26854 26855 verifyFormat("outerFunctionCall(nestedFunctionCall(argument1),\n" 26856 " nestedLongFunctionCall(argument1, " 26857 "argument2, argument3,\n" 26858 " argument4, " 26859 "argument5));", 26860 Style); 26861 26862 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; 26863 26864 verifyFormat(Short, Style); 26865 verifyFormat( 26866 "functionCall(\n" 26867 " paramA, paramB, paramC, paramD, paramE, paramF, paramG, paramH, " 26868 "paramI\n" 26869 ");\n" 26870 "void functionDecl(\n" 26871 " int argumentA, int argumentB, int argumentC, int argumentD, int " 26872 "argumentE\n" 26873 ");", 26874 Medium, Style); 26875 26876 Style.AllowAllArgumentsOnNextLine = false; 26877 Style.AllowAllParametersOfDeclarationOnNextLine = false; 26878 26879 verifyFormat(Short, Style); 26880 verifyFormat( 26881 "functionCall(\n" 26882 " paramA, paramB, paramC, paramD, paramE, paramF, paramG, paramH, " 26883 "paramI\n" 26884 ");\n" 26885 "void functionDecl(\n" 26886 " int argumentA, int argumentB, int argumentC, int argumentD, int " 26887 "argumentE\n" 26888 ");", 26889 Medium, Style); 26890 26891 Style.BinPackArguments = false; 26892 Style.BinPackParameters = FormatStyle::BPPS_OnePerLine; 26893 26894 verifyFormat(Short, Style); 26895 26896 verifyFormat("functionCall(\n" 26897 " paramA,\n" 26898 " paramB,\n" 26899 " paramC,\n" 26900 " paramD,\n" 26901 " paramE,\n" 26902 " paramF,\n" 26903 " paramG,\n" 26904 " paramH,\n" 26905 " paramI\n" 26906 ");\n" 26907 "void functionDecl(\n" 26908 " int argumentA,\n" 26909 " int argumentB,\n" 26910 " int argumentC,\n" 26911 " int argumentD,\n" 26912 " int argumentE\n" 26913 ");", 26914 Medium, Style); 26915 26916 verifyFormat("outerFunctionCall(\n" 26917 " nestedFunctionCall(argument1),\n" 26918 " nestedLongFunctionCall(\n" 26919 " argument1,\n" 26920 " argument2,\n" 26921 " argument3,\n" 26922 " argument4,\n" 26923 " argument5\n" 26924 " )\n" 26925 ");", 26926 Style); 26927 26928 verifyFormat("int a = (int)b;", Style); 26929 verifyFormat("int a = (int)b;", 26930 "int a = (\n" 26931 " int\n" 26932 ") b;", 26933 Style); 26934 26935 verifyFormat("return (true);", Style); 26936 verifyFormat("return (true);", 26937 "return (\n" 26938 " true\n" 26939 ");", 26940 Style); 26941 26942 verifyFormat("void foo();", Style); 26943 verifyFormat("void foo();", 26944 "void foo(\n" 26945 ");", 26946 Style); 26947 26948 verifyFormat("void foo() {}", Style); 26949 verifyFormat("void foo() {}", 26950 "void foo(\n" 26951 ") {\n" 26952 "}", 26953 Style); 26954 26955 verifyFormat("auto string = std::string();", Style); 26956 verifyFormat("auto string = std::string();", 26957 "auto string = std::string(\n" 26958 ");", 26959 Style); 26960 26961 verifyFormat("void (*functionPointer)() = nullptr;", Style); 26962 verifyFormat("void (*functionPointer)() = nullptr;", 26963 "void (\n" 26964 " *functionPointer\n" 26965 ")\n" 26966 "(\n" 26967 ") = nullptr;", 26968 Style); 26969 } 26970 26971 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentIfStatement) { 26972 auto Style = getLLVMStyle(); 26973 26974 verifyFormat("if (foo()) {\n" 26975 " return;\n" 26976 "}", 26977 Style); 26978 26979 verifyFormat("if (quiteLongArg !=\n" 26980 " (alsoLongArg - 1)) { // ABC is a very longgggggggggggg " 26981 "comment\n" 26982 " return;\n" 26983 "}", 26984 Style); 26985 26986 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; 26987 26988 verifyFormat("if (foo()) {\n" 26989 " return;\n" 26990 "}", 26991 Style); 26992 26993 verifyFormat("if (quiteLongArg !=\n" 26994 " (alsoLongArg - 1)) { // ABC is a very longgggggggggggg " 26995 "comment\n" 26996 " return;\n" 26997 "}", 26998 Style); 26999 27000 verifyFormat("void foo() {\n" 27001 " if (camelCaseName < alsoLongName ||\n" 27002 " anotherEvenLongerName <=\n" 27003 " thisReallyReallyReallyReallyReallyReallyLongerName ||" 27004 "\n" 27005 " otherName < thisLastName) {\n" 27006 " return;\n" 27007 " } else if (quiteLongName < alsoLongName ||\n" 27008 " anotherEvenLongerName <=\n" 27009 " thisReallyReallyReallyReallyReallyReallyLonger" 27010 "Name ||\n" 27011 " otherName < thisLastName) {\n" 27012 " return;\n" 27013 " }\n" 27014 "}", 27015 Style); 27016 27017 Style.ContinuationIndentWidth = 2; 27018 verifyFormat("void foo() {\n" 27019 " if (ThisIsRatherALongIfClause && thatIExpectToBeBroken ||\n" 27020 " ontoMultipleLines && whenFormattedCorrectly) {\n" 27021 " if (false) {\n" 27022 " return;\n" 27023 " } else if (thisIsRatherALongIfClause && " 27024 "thatIExpectToBeBroken ||\n" 27025 " ontoMultipleLines && whenFormattedCorrectly) {\n" 27026 " return;\n" 27027 " }\n" 27028 " }\n" 27029 "}", 27030 Style); 27031 } 27032 27033 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentForStatement) { 27034 auto Style = getLLVMStyle(); 27035 27036 verifyFormat("for (int i = 0; i < 5; ++i) {\n" 27037 " doSomething();\n" 27038 "}", 27039 Style); 27040 27041 verifyFormat("for (int myReallyLongCountVariable = 0; " 27042 "myReallyLongCountVariable < count;\n" 27043 " myReallyLongCountVariable++) {\n" 27044 " doSomething();\n" 27045 "}", 27046 Style); 27047 27048 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; 27049 27050 verifyFormat("for (int i = 0; i < 5; ++i) {\n" 27051 " doSomething();\n" 27052 "}", 27053 Style); 27054 27055 verifyFormat("for (int myReallyLongCountVariable = 0; " 27056 "myReallyLongCountVariable < count;\n" 27057 " myReallyLongCountVariable++) {\n" 27058 " doSomething();\n" 27059 "}", 27060 Style); 27061 } 27062 27063 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentInitializers) { 27064 auto Style = getLLVMStyleWithColumns(60); 27065 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; 27066 // Aggregate initialization. 27067 verifyFormat("int LooooooooooooooooooooooooongVariable[2] = {\n" 27068 " 10000000, 20000000\n" 27069 "};", 27070 Style); 27071 verifyFormat("SomeStruct s{\n" 27072 " \"xxxxxxxxxxxxxxxx\", \"yyyyyyyyyyyyyyyy\",\n" 27073 " \"zzzzzzzzzzzzzzzz\"\n" 27074 "};", 27075 Style); 27076 // Designated initializers. 27077 verifyFormat("int LooooooooooooooooooooooooongVariable[2] = {\n" 27078 " [0] = 10000000, [1] = 20000000\n" 27079 "};", 27080 Style); 27081 verifyFormat("SomeStruct s{\n" 27082 " .foo = \"xxxxxxxxxxxxx\",\n" 27083 " .bar = \"yyyyyyyyyyyyy\",\n" 27084 " .baz = \"zzzzzzzzzzzzz\"\n" 27085 "};", 27086 Style); 27087 // List initialization. 27088 verifyFormat("SomeStruct s{\n" 27089 " \"xxxxxxxxxxxxx\",\n" 27090 " \"yyyyyyyyyyyyy\",\n" 27091 " \"zzzzzzzzzzzzz\",\n" 27092 "};", 27093 Style); 27094 verifyFormat("SomeStruct{\n" 27095 " \"xxxxxxxxxxxxx\",\n" 27096 " \"yyyyyyyyyyyyy\",\n" 27097 " \"zzzzzzzzzzzzz\",\n" 27098 "};", 27099 Style); 27100 verifyFormat("new SomeStruct{\n" 27101 " \"xxxxxxxxxxxxx\",\n" 27102 " \"yyyyyyyyyyyyy\",\n" 27103 " \"zzzzzzzzzzzzz\",\n" 27104 "};", 27105 Style); 27106 // Member initializer. 27107 verifyFormat("class SomeClass {\n" 27108 " SomeStruct s{\n" 27109 " \"xxxxxxxxxxxxx\",\n" 27110 " \"yyyyyyyyyyyyy\",\n" 27111 " \"zzzzzzzzzzzzz\",\n" 27112 " };\n" 27113 "};", 27114 Style); 27115 // Constructor member initializer. 27116 verifyFormat("SomeClass::SomeClass : strct{\n" 27117 " \"xxxxxxxxxxxxx\",\n" 27118 " \"yyyyyyyyyyyyy\",\n" 27119 " \"zzzzzzzzzzzzz\",\n" 27120 " } {}", 27121 Style); 27122 // Copy initialization. 27123 verifyFormat("SomeStruct s = SomeStruct{\n" 27124 " \"xxxxxxxxxxxxx\",\n" 27125 " \"yyyyyyyyyyyyy\",\n" 27126 " \"zzzzzzzzzzzzz\",\n" 27127 "};", 27128 Style); 27129 // Copy list initialization. 27130 verifyFormat("SomeStruct s = {\n" 27131 " \"xxxxxxxxxxxxx\",\n" 27132 " \"yyyyyyyyyyyyy\",\n" 27133 " \"zzzzzzzzzzzzz\",\n" 27134 "};", 27135 Style); 27136 // Assignment operand initialization. 27137 verifyFormat("s = {\n" 27138 " \"xxxxxxxxxxxxx\",\n" 27139 " \"yyyyyyyyyyyyy\",\n" 27140 " \"zzzzzzzzzzzzz\",\n" 27141 "};", 27142 Style); 27143 // Returned object initialization. 27144 verifyFormat("return {\n" 27145 " \"xxxxxxxxxxxxx\",\n" 27146 " \"yyyyyyyyyyyyy\",\n" 27147 " \"zzzzzzzzzzzzz\",\n" 27148 "};", 27149 Style); 27150 // Initializer list. 27151 verifyFormat("auto initializerList = {\n" 27152 " \"xxxxxxxxxxxxx\",\n" 27153 " \"yyyyyyyyyyyyy\",\n" 27154 " \"zzzzzzzzzzzzz\",\n" 27155 "};", 27156 Style); 27157 // Function parameter initialization. 27158 verifyFormat("func({\n" 27159 " \"xxxxxxxxxxxxx\",\n" 27160 " \"yyyyyyyyyyyyy\",\n" 27161 " \"zzzzzzzzzzzzz\",\n" 27162 "});", 27163 Style); 27164 // Nested init lists. 27165 verifyFormat("SomeStruct s = {\n" 27166 " {{init1, init2, init3, init4, init5},\n" 27167 " {init1, init2, init3, init4, init5}}\n" 27168 "};", 27169 Style); 27170 verifyFormat("SomeStruct s = {\n" 27171 " {{\n" 27172 " .init1 = 1,\n" 27173 " .init2 = 2,\n" 27174 " .init3 = 3,\n" 27175 " .init4 = 4,\n" 27176 " .init5 = 5,\n" 27177 " },\n" 27178 " {init1, init2, init3, init4, init5}}\n" 27179 "};", 27180 Style); 27181 verifyFormat("SomeArrayT a[3] = {\n" 27182 " {\n" 27183 " foo,\n" 27184 " bar,\n" 27185 " },\n" 27186 " {\n" 27187 " foo,\n" 27188 " bar,\n" 27189 " },\n" 27190 " SomeArrayT{},\n" 27191 "};", 27192 Style); 27193 verifyFormat("SomeArrayT a[3] = {\n" 27194 " {foo},\n" 27195 " {\n" 27196 " {\n" 27197 " init1,\n" 27198 " init2,\n" 27199 " init3,\n" 27200 " },\n" 27201 " {\n" 27202 " init1,\n" 27203 " init2,\n" 27204 " init3,\n" 27205 " },\n" 27206 " },\n" 27207 " {baz},\n" 27208 "};", 27209 Style); 27210 } 27211 27212 TEST_F(FormatTest, UnderstandsDigraphs) { 27213 verifyFormat("int arr<:5:> = {};"); 27214 verifyFormat("int arr[5] = <%%>;"); 27215 verifyFormat("int arr<:::qualified_variable:> = {};"); 27216 verifyFormat("int arr[::qualified_variable] = <%%>;"); 27217 verifyFormat("%:include <header>"); 27218 verifyFormat("%:define A x##y"); 27219 verifyFormat("#define A x%:%:y"); 27220 } 27221 27222 TEST_F(FormatTest, AlignArrayOfStructuresLeftAlignmentNonSquare) { 27223 auto Style = getLLVMStyle(); 27224 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left; 27225 Style.AlignConsecutiveAssignments.Enabled = true; 27226 Style.AlignConsecutiveDeclarations.Enabled = true; 27227 27228 // The AlignArray code is incorrect for non square Arrays and can cause 27229 // crashes, these tests assert that the array is not changed but will 27230 // also act as regression tests for when it is properly fixed 27231 verifyFormat("struct test demo[] = {\n" 27232 " {1, 2},\n" 27233 " {3, 4, 5},\n" 27234 " {6, 7, 8}\n" 27235 "};", 27236 Style); 27237 verifyFormat("struct test demo[] = {\n" 27238 " {1, 2, 3, 4, 5},\n" 27239 " {3, 4, 5},\n" 27240 " {6, 7, 8}\n" 27241 "};", 27242 Style); 27243 verifyFormat("struct test demo[] = {\n" 27244 " {1, 2, 3, 4, 5},\n" 27245 " {3, 4, 5},\n" 27246 " {6, 7, 8, 9, 10, 11, 12}\n" 27247 "};", 27248 Style); 27249 verifyFormat("struct test demo[] = {\n" 27250 " {1, 2, 3},\n" 27251 " {3, 4, 5},\n" 27252 " {6, 7, 8, 9, 10, 11, 12}\n" 27253 "};", 27254 Style); 27255 27256 verifyFormat("S{\n" 27257 " {},\n" 27258 " {},\n" 27259 " {a, b}\n" 27260 "};", 27261 Style); 27262 verifyFormat("S{\n" 27263 " {},\n" 27264 " {},\n" 27265 " {a, b},\n" 27266 "};", 27267 Style); 27268 verifyFormat("void foo() {\n" 27269 " auto thing = test{\n" 27270 " {\n" 27271 " {13}, {something}, // A\n" 27272 " }\n" 27273 " };\n" 27274 "}", 27275 "void foo() {\n" 27276 " auto thing = test{\n" 27277 " {\n" 27278 " {13},\n" 27279 " {something}, // A\n" 27280 " }\n" 27281 " };\n" 27282 "}", 27283 Style); 27284 } 27285 27286 TEST_F(FormatTest, AlignArrayOfStructuresRightAlignmentNonSquare) { 27287 auto Style = getLLVMStyle(); 27288 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 27289 Style.AlignConsecutiveAssignments.Enabled = true; 27290 Style.AlignConsecutiveDeclarations.Enabled = true; 27291 27292 // The AlignArray code is incorrect for non square Arrays and can cause 27293 // crashes, these tests assert that the array is not changed but will 27294 // also act as regression tests for when it is properly fixed 27295 verifyFormat("struct test demo[] = {\n" 27296 " {1, 2},\n" 27297 " {3, 4, 5},\n" 27298 " {6, 7, 8}\n" 27299 "};", 27300 Style); 27301 verifyFormat("struct test demo[] = {\n" 27302 " {1, 2, 3, 4, 5},\n" 27303 " {3, 4, 5},\n" 27304 " {6, 7, 8}\n" 27305 "};", 27306 Style); 27307 verifyFormat("struct test demo[] = {\n" 27308 " {1, 2, 3, 4, 5},\n" 27309 " {3, 4, 5},\n" 27310 " {6, 7, 8, 9, 10, 11, 12}\n" 27311 "};", 27312 Style); 27313 verifyFormat("struct test demo[] = {\n" 27314 " {1, 2, 3},\n" 27315 " {3, 4, 5},\n" 27316 " {6, 7, 8, 9, 10, 11, 12}\n" 27317 "};", 27318 Style); 27319 27320 verifyFormat("S{\n" 27321 " {},\n" 27322 " {},\n" 27323 " {a, b}\n" 27324 "};", 27325 Style); 27326 verifyFormat("S{\n" 27327 " {},\n" 27328 " {},\n" 27329 " {a, b},\n" 27330 "};", 27331 Style); 27332 verifyFormat("void foo() {\n" 27333 " auto thing = test{\n" 27334 " {\n" 27335 " {13}, {something}, // A\n" 27336 " }\n" 27337 " };\n" 27338 "}", 27339 "void foo() {\n" 27340 " auto thing = test{\n" 27341 " {\n" 27342 " {13},\n" 27343 " {something}, // A\n" 27344 " }\n" 27345 " };\n" 27346 "}", 27347 Style); 27348 } 27349 27350 TEST_F(FormatTest, FormatsVariableTemplates) { 27351 verifyFormat("inline bool var = is_integral_v<int> && is_signed_v<int>;"); 27352 verifyFormat("template <typename T> " 27353 "inline bool var = is_integral_v<T> && is_signed_v<T>;"); 27354 } 27355 27356 TEST_F(FormatTest, RemoveSemicolon) { 27357 FormatStyle Style = getLLVMStyle(); 27358 Style.RemoveSemicolon = true; 27359 27360 verifyFormat("int max(int a, int b) { return a > b ? a : b; }", 27361 "int max(int a, int b) { return a > b ? a : b; };", Style); 27362 27363 verifyFormat("int max(int a, int b) { return a > b ? a : b; }", 27364 "int max(int a, int b) { return a > b ? a : b; };;", Style); 27365 27366 verifyFormat("class Foo {\n" 27367 " int getSomething() const { return something; }\n" 27368 "};", 27369 "class Foo {\n" 27370 " int getSomething() const { return something; };\n" 27371 "};", 27372 Style); 27373 27374 verifyFormat("class Foo {\n" 27375 " int getSomething() const { return something; }\n" 27376 "};", 27377 "class Foo {\n" 27378 " int getSomething() const { return something; };;\n" 27379 "};", 27380 Style); 27381 27382 verifyFormat("for (;;) {\n" 27383 "}", 27384 Style); 27385 27386 verifyFormat("class [[deprecated(\"\")]] C {\n" 27387 " int i;\n" 27388 "};", 27389 Style); 27390 27391 verifyFormat("struct EXPORT_MACRO [[nodiscard]] C {\n" 27392 " int i;\n" 27393 "};", 27394 Style); 27395 27396 verifyIncompleteFormat("class C final [[deprecated(l]] {});", Style); 27397 27398 verifyFormat("void main() {}", "void main() {};", Style); 27399 27400 verifyFormat("struct Foo {\n" 27401 " Foo() {}\n" 27402 " ~Foo() {}\n" 27403 "};", 27404 "struct Foo {\n" 27405 " Foo() {};\n" 27406 " ~Foo() {};\n" 27407 "};", 27408 Style); 27409 27410 // We can't (and probably shouldn't) support the following. 27411 #if 0 27412 verifyFormat("void foo() {} //\n" 27413 "int bar;", 27414 "void foo() {}; //\n" 27415 "; int bar;", 27416 Style); 27417 #endif 27418 27419 verifyFormat("auto sgf = [] {\n" 27420 " ogl = {\n" 27421 " a, b, c, d, e,\n" 27422 " };\n" 27423 "};", 27424 Style); 27425 27426 Style.TypenameMacros.push_back("STRUCT"); 27427 verifyFormat("STRUCT(T, B) { int i; };", Style); 27428 } 27429 27430 TEST_F(FormatTest, BreakAfterAttributes) { 27431 constexpr StringRef Code("[[maybe_unused]] const int i;\n" 27432 "[[foo([[]])]] [[maybe_unused]]\n" 27433 "int j;\n" 27434 "[[maybe_unused]]\n" 27435 "foo<int> k;\n" 27436 "[[nodiscard]] inline int f(int &i);\n" 27437 "[[foo([[]])]] [[nodiscard]]\n" 27438 "int g(int &i);\n" 27439 "[[nodiscard]]\n" 27440 "inline int f(int &i) {\n" 27441 " i = 1;\n" 27442 " return 0;\n" 27443 "}\n" 27444 "[[foo([[]])]] [[nodiscard]] int g(int &i) {\n" 27445 " i = 0;\n" 27446 " return 1;\n" 27447 "}"); 27448 27449 FormatStyle Style = getLLVMStyle(); 27450 EXPECT_EQ(Style.BreakAfterAttributes, FormatStyle::ABS_Leave); 27451 verifyNoChange(Code, Style); 27452 27453 Style.BreakAfterAttributes = FormatStyle::ABS_Never; 27454 verifyFormat("[[maybe_unused]] const int i;\n" 27455 "[[foo([[]])]] [[maybe_unused]] int j;\n" 27456 "[[maybe_unused]] foo<int> k;\n" 27457 "[[nodiscard]] inline int f(int &i);\n" 27458 "[[foo([[]])]] [[nodiscard]] int g(int &i);\n" 27459 "[[nodiscard]] inline int f(int &i) {\n" 27460 " i = 1;\n" 27461 " return 0;\n" 27462 "}\n" 27463 "[[foo([[]])]] [[nodiscard]] int g(int &i) {\n" 27464 " i = 0;\n" 27465 " return 1;\n" 27466 "}", 27467 Code, Style); 27468 27469 Style.BreakAfterAttributes = FormatStyle::ABS_Always; 27470 verifyFormat("[[maybe_unused]]\n" 27471 "const int i;\n" 27472 "[[foo([[]])]] [[maybe_unused]]\n" 27473 "int j;\n" 27474 "[[maybe_unused]]\n" 27475 "foo<int> k;\n" 27476 "[[nodiscard]]\n" 27477 "inline int f(int &i);\n" 27478 "[[foo([[]])]] [[nodiscard]]\n" 27479 "int g(int &i);\n" 27480 "[[nodiscard]]\n" 27481 "inline int f(int &i) {\n" 27482 " i = 1;\n" 27483 " return 0;\n" 27484 "}\n" 27485 "[[foo([[]])]] [[nodiscard]]\n" 27486 "int g(int &i) {\n" 27487 " i = 0;\n" 27488 " return 1;\n" 27489 "}", 27490 Code, Style); 27491 27492 constexpr StringRef CtrlStmtCode("[[likely]] if (a)\n" 27493 " f();\n" 27494 "else\n" 27495 " g();\n" 27496 "[[foo([[]])]]\n" 27497 "switch (b) {\n" 27498 "[[unlikely]] case 1:\n" 27499 " ++b;\n" 27500 " break;\n" 27501 "[[likely]]\n" 27502 "default:\n" 27503 " return;\n" 27504 "}\n" 27505 "[[unlikely]] for (; c > 0; --c)\n" 27506 " h();\n" 27507 "[[likely]]\n" 27508 "while (d > 0)\n" 27509 " --d;"); 27510 27511 Style.BreakAfterAttributes = FormatStyle::ABS_Leave; 27512 verifyNoChange(CtrlStmtCode, Style); 27513 27514 Style.BreakAfterAttributes = FormatStyle::ABS_Never; 27515 verifyFormat("[[likely]] if (a)\n" 27516 " f();\n" 27517 "else\n" 27518 " g();\n" 27519 "[[foo([[]])]] switch (b) {\n" 27520 "[[unlikely]] case 1:\n" 27521 " ++b;\n" 27522 " break;\n" 27523 "[[likely]] default:\n" 27524 " return;\n" 27525 "}\n" 27526 "[[unlikely]] for (; c > 0; --c)\n" 27527 " h();\n" 27528 "[[likely]] while (d > 0)\n" 27529 " --d;", 27530 CtrlStmtCode, Style); 27531 27532 Style.BreakAfterAttributes = FormatStyle::ABS_Always; 27533 verifyFormat("[[likely]]\n" 27534 "if (a)\n" 27535 " f();\n" 27536 "else\n" 27537 " g();\n" 27538 "[[foo([[]])]]\n" 27539 "switch (b) {\n" 27540 "[[unlikely]]\n" 27541 "case 1:\n" 27542 " ++b;\n" 27543 " break;\n" 27544 "[[likely]]\n" 27545 "default:\n" 27546 " return;\n" 27547 "}\n" 27548 "[[unlikely]]\n" 27549 "for (; c > 0; --c)\n" 27550 " h();\n" 27551 "[[likely]]\n" 27552 "while (d > 0)\n" 27553 " --d;", 27554 CtrlStmtCode, Style); 27555 27556 constexpr StringRef CtorDtorCode("struct Foo {\n" 27557 " [[deprecated]] Foo();\n" 27558 " [[deprecated]] Foo() {}\n" 27559 " [[deprecated]] ~Foo();\n" 27560 " [[deprecated]] ~Foo() {}\n" 27561 " [[deprecated]] void f();\n" 27562 " [[deprecated]] void f() {}\n" 27563 "};\n" 27564 "[[deprecated]] Bar::Bar() {}\n" 27565 "[[deprecated]] Bar::~Bar() {}\n" 27566 "[[deprecated]] void g() {}"); 27567 verifyFormat("struct Foo {\n" 27568 " [[deprecated]]\n" 27569 " Foo();\n" 27570 " [[deprecated]]\n" 27571 " Foo() {}\n" 27572 " [[deprecated]]\n" 27573 " ~Foo();\n" 27574 " [[deprecated]]\n" 27575 " ~Foo() {}\n" 27576 " [[deprecated]]\n" 27577 " void f();\n" 27578 " [[deprecated]]\n" 27579 " void f() {}\n" 27580 "};\n" 27581 "[[deprecated]]\n" 27582 "Bar::Bar() {}\n" 27583 "[[deprecated]]\n" 27584 "Bar::~Bar() {}\n" 27585 "[[deprecated]]\n" 27586 "void g() {}", 27587 CtorDtorCode, Style); 27588 27589 Style.BreakBeforeBraces = FormatStyle::BS_Linux; 27590 verifyFormat("struct Foo {\n" 27591 " [[deprecated]]\n" 27592 " Foo();\n" 27593 " [[deprecated]]\n" 27594 " Foo()\n" 27595 " {\n" 27596 " }\n" 27597 " [[deprecated]]\n" 27598 " ~Foo();\n" 27599 " [[deprecated]]\n" 27600 " ~Foo()\n" 27601 " {\n" 27602 " }\n" 27603 " [[deprecated]]\n" 27604 " void f();\n" 27605 " [[deprecated]]\n" 27606 " void f()\n" 27607 " {\n" 27608 " }\n" 27609 "};\n" 27610 "[[deprecated]]\n" 27611 "Bar::Bar()\n" 27612 "{\n" 27613 "}\n" 27614 "[[deprecated]]\n" 27615 "Bar::~Bar()\n" 27616 "{\n" 27617 "}\n" 27618 "[[deprecated]]\n" 27619 "void g()\n" 27620 "{\n" 27621 "}", 27622 CtorDtorCode, Style); 27623 27624 verifyFormat("struct Foo {\n" 27625 " [[maybe_unused]]\n" 27626 " void operator+();\n" 27627 "};\n" 27628 "[[nodiscard]]\n" 27629 "Foo &operator-(Foo &);", 27630 Style); 27631 27632 Style.ReferenceAlignment = FormatStyle::ReferenceAlignmentStyle::RAS_Left; 27633 verifyFormat("[[nodiscard]]\n" 27634 "Foo& operator-(Foo&);", 27635 Style); 27636 } 27637 27638 TEST_F(FormatTest, InsertNewlineAtEOF) { 27639 FormatStyle Style = getLLVMStyle(); 27640 Style.InsertNewlineAtEOF = true; 27641 27642 verifyNoChange("int i;\n", Style); 27643 verifyFormat("int i;\n", "int i;", Style); 27644 27645 constexpr StringRef Code{"namespace {\n" 27646 "int i;\n" 27647 "} // namespace"}; 27648 verifyFormat(Code.str() + '\n', Code, Style, 27649 {tooling::Range(19, 13)}); // line 3 27650 } 27651 27652 TEST_F(FormatTest, KeepEmptyLinesAtEOF) { 27653 FormatStyle Style = getLLVMStyle(); 27654 Style.KeepEmptyLines.AtEndOfFile = true; 27655 27656 const StringRef Code{"int i;\n\n"}; 27657 verifyNoChange(Code, Style); 27658 verifyFormat(Code, "int i;\n\n\n", Style); 27659 } 27660 27661 TEST_F(FormatTest, SpaceAfterUDL) { 27662 verifyFormat("auto c = (4s).count();"); 27663 verifyFormat("auto x = 5s .count() == 5;"); 27664 } 27665 27666 TEST_F(FormatTest, InterfaceAsClassMemberName) { 27667 verifyFormat("class Foo {\n" 27668 " int interface;\n" 27669 " Foo::Foo(int iface) : interface{iface} {}\n" 27670 "}"); 27671 } 27672 27673 TEST_F(FormatTest, PreprocessorOverlappingRegions) { 27674 verifyFormat("#ifdef\n\n" 27675 "#else\n" 27676 "#endif", 27677 "#ifdef \n" 27678 " \n" 27679 "\n" 27680 "#else \n" 27681 "#endif ", 27682 getGoogleStyle()); 27683 } 27684 27685 TEST_F(FormatTest, RemoveParentheses) { 27686 FormatStyle Style = getLLVMStyle(); 27687 EXPECT_EQ(Style.RemoveParentheses, FormatStyle::RPS_Leave); 27688 27689 Style.RemoveParentheses = FormatStyle::RPS_MultipleParentheses; 27690 verifyFormat("#define Foo(...) foo((__VA_ARGS__))", Style); 27691 verifyFormat("int x __attribute__((aligned(16))) = 0;", Style); 27692 verifyFormat("decltype((foo->bar)) baz;", Style); 27693 verifyFormat("class __declspec(dllimport) X {};", 27694 "class __declspec((dllimport)) X {};", Style); 27695 verifyFormat("int x = (({ 0; }));", "int x = ((({ 0; })));", Style); 27696 verifyFormat("while (a)\n" 27697 " b;", 27698 "while (((a)))\n" 27699 " b;", 27700 Style); 27701 verifyFormat("while ((a = b))\n" 27702 " c;", 27703 "while (((a = b)))\n" 27704 " c;", 27705 Style); 27706 verifyFormat("if (a)\n" 27707 " b;", 27708 "if (((a)))\n" 27709 " b;", 27710 Style); 27711 verifyFormat("if constexpr ((a = b))\n" 27712 " c;", 27713 "if constexpr (((a = b)))\n" 27714 " c;", 27715 Style); 27716 verifyFormat("if (({ a; }))\n" 27717 " b;", 27718 "if ((({ a; })))\n" 27719 " b;", 27720 Style); 27721 verifyFormat("static_assert((std::is_constructible_v<T, Args &&> && ...));", 27722 "static_assert(((std::is_constructible_v<T, Args &&> && ...)));", 27723 Style); 27724 verifyFormat("foo((a, b));", "foo(((a, b)));", Style); 27725 verifyFormat("foo((a, b));", "foo(((a), b));", Style); 27726 verifyFormat("foo((a, b));", "foo((a, (b)));", Style); 27727 verifyFormat("foo((a, b, c));", "foo((a, ((b)), c));", Style); 27728 verifyFormat("return (0);", "return (((0)));", Style); 27729 verifyFormat("return (({ 0; }));", "return ((({ 0; })));", Style); 27730 verifyFormat("return ((... && std::is_convertible_v<TArgsLocal, TArgs>));", 27731 "return (((... && std::is_convertible_v<TArgsLocal, TArgs>)));", 27732 Style); 27733 27734 Style.RemoveParentheses = FormatStyle::RPS_ReturnStatement; 27735 verifyFormat("#define Return0 return (0);", Style); 27736 verifyFormat("return 0;", "return (0);", Style); 27737 verifyFormat("co_return 0;", "co_return ((0));", Style); 27738 verifyFormat("return 0;", "return (((0)));", Style); 27739 verifyFormat("return ({ 0; });", "return ((({ 0; })));", Style); 27740 verifyFormat("return (... && std::is_convertible_v<TArgsLocal, TArgs>);", 27741 "return (((... && std::is_convertible_v<TArgsLocal, TArgs>)));", 27742 Style); 27743 verifyFormat("inline decltype(auto) f() {\n" 27744 " if (a) {\n" 27745 " return (a);\n" 27746 " }\n" 27747 " return (b);\n" 27748 "}", 27749 "inline decltype(auto) f() {\n" 27750 " if (a) {\n" 27751 " return ((a));\n" 27752 " }\n" 27753 " return ((b));\n" 27754 "}", 27755 Style); 27756 verifyFormat("auto g() {\n" 27757 " decltype(auto) x = [] {\n" 27758 " auto y = [] {\n" 27759 " if (a) {\n" 27760 " return a;\n" 27761 " }\n" 27762 " return b;\n" 27763 " };\n" 27764 " if (c) {\n" 27765 " return (c);\n" 27766 " }\n" 27767 " return (d);\n" 27768 " };\n" 27769 " if (e) {\n" 27770 " return e;\n" 27771 " }\n" 27772 " return f;\n" 27773 "}", 27774 "auto g() {\n" 27775 " decltype(auto) x = [] {\n" 27776 " auto y = [] {\n" 27777 " if (a) {\n" 27778 " return ((a));\n" 27779 " }\n" 27780 " return ((b));\n" 27781 " };\n" 27782 " if (c) {\n" 27783 " return ((c));\n" 27784 " }\n" 27785 " return ((d));\n" 27786 " };\n" 27787 " if (e) {\n" 27788 " return ((e));\n" 27789 " }\n" 27790 " return ((f));\n" 27791 "}", 27792 Style); 27793 27794 Style.ColumnLimit = 25; 27795 verifyFormat("return (a + b) - (c + d);", 27796 "return (((a + b)) -\n" 27797 " ((c + d)));", 27798 Style); 27799 } 27800 27801 TEST_F(FormatTest, AllowBreakBeforeNoexceptSpecifier) { 27802 auto Style = getLLVMStyleWithColumns(35); 27803 27804 EXPECT_EQ(Style.AllowBreakBeforeNoexceptSpecifier, FormatStyle::BBNSS_Never); 27805 verifyFormat("void foo(int arg1,\n" 27806 " double arg2) noexcept;", 27807 Style); 27808 27809 // The following line does not fit within the 35 column limit, but that's what 27810 // happens with no break allowed. 27811 verifyFormat("void bar(int arg1, double arg2) noexcept(\n" 27812 " noexcept(baz(arg1)) &&\n" 27813 " noexcept(baz(arg2)));", 27814 Style); 27815 27816 verifyFormat("void aVeryLongFunctionNameWithoutAnyArguments() noexcept;", 27817 Style); 27818 27819 Style.AllowBreakBeforeNoexceptSpecifier = FormatStyle::BBNSS_Always; 27820 verifyFormat("void foo(int arg1,\n" 27821 " double arg2) noexcept;", 27822 Style); 27823 27824 verifyFormat("void bar(int arg1, double arg2)\n" 27825 " noexcept(noexcept(baz(arg1)) &&\n" 27826 " noexcept(baz(arg2)));", 27827 Style); 27828 27829 verifyFormat("void aVeryLongFunctionNameWithoutAnyArguments()\n" 27830 " noexcept;", 27831 Style); 27832 27833 Style.AllowBreakBeforeNoexceptSpecifier = FormatStyle::BBNSS_OnlyWithParen; 27834 verifyFormat("void foo(int arg1,\n" 27835 " double arg2) noexcept;", 27836 Style); 27837 27838 verifyFormat("void bar(int arg1, double arg2)\n" 27839 " noexcept(noexcept(baz(arg1)) &&\n" 27840 " noexcept(baz(arg2)));", 27841 Style); 27842 27843 verifyFormat("void aVeryLongFunctionNameWithoutAnyArguments() noexcept;", 27844 Style); 27845 } 27846 27847 TEST_F(FormatTest, PPBranchesInBracedInit) { 27848 verifyFormat("A a_{kFlag1,\n" 27849 "#if BUILD_FLAG\n" 27850 " kFlag2,\n" 27851 "#else\n" 27852 " kFlag3,\n" 27853 "#endif\n" 27854 " kFlag4};", 27855 "A a_{\n" 27856 " kFlag1,\n" 27857 "#if BUILD_FLAG\n" 27858 " kFlag2,\n" 27859 "#else\n" 27860 " kFlag3,\n" 27861 "#endif\n" 27862 " kFlag4\n" 27863 "};"); 27864 } 27865 27866 TEST_F(FormatTest, PPDirectivesAndCommentsInBracedInit) { 27867 verifyFormat("{\n" 27868 " char *a[] = {\n" 27869 " /* abc */ \"abc\",\n" 27870 "#if FOO\n" 27871 " /* xyz */ \"xyz\",\n" 27872 "#endif\n" 27873 " /* last */ \"last\"};\n" 27874 "}", 27875 getLLVMStyleWithColumns(30)); 27876 } 27877 27878 TEST_F(FormatTest, BreakAdjacentStringLiterals) { 27879 constexpr StringRef Code{ 27880 "return \"Code\" \"\\0\\52\\26\\55\\55\\0\" \"x013\" \"\\02\\xBA\";"}; 27881 27882 verifyFormat("return \"Code\"\n" 27883 " \"\\0\\52\\26\\55\\55\\0\"\n" 27884 " \"x013\"\n" 27885 " \"\\02\\xBA\";", 27886 Code); 27887 27888 auto Style = getLLVMStyle(); 27889 Style.BreakAdjacentStringLiterals = false; 27890 verifyFormat(Code, Style); 27891 } 27892 27893 TEST_F(FormatTest, AlignUTFCommentsAndStringLiterals) { 27894 verifyFormat( 27895 "int rus; // А теперь комментарии, например, на русском, 2-байта\n" 27896 "int long_rus; // Верхний коммент еще не превысил границу в 80, однако\n" 27897 " // уже отодвинут. Перенос, при этом, отрабатывает верно"); 27898 27899 auto Style = getLLVMStyle(); 27900 Style.ColumnLimit = 15; 27901 verifyNoChange("#define test \\\n" 27902 " /* 测试 */ \\\n" 27903 " \"aa\" \\\n" 27904 " \"bb\"", 27905 Style); 27906 27907 Style.ColumnLimit = 25; 27908 verifyFormat("struct foo {\n" 27909 " int iiiiii; ///< iiiiii\n" 27910 " int b; ///< ыыы\n" 27911 " int c; ///< ыыыы\n" 27912 "};", 27913 Style); 27914 27915 Style.ColumnLimit = 35; 27916 verifyFormat("#define SENSOR_DESC_1 \\\n" 27917 " \"{\" \\\n" 27918 " \"unit_of_measurement: \\\"°C\\\",\" \\\n" 27919 " \"}\"", 27920 Style); 27921 27922 Style.ColumnLimit = 80; 27923 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left; 27924 verifyFormat("Languages languages = {\n" 27925 " Language{{'e', 'n'}, U\"Test English\" },\n" 27926 " Language{{'l', 'v'}, U\"Test Latviešu\"},\n" 27927 " Language{{'r', 'u'}, U\"Test Русский\" },\n" 27928 "};", 27929 Style); 27930 } 27931 27932 TEST_F(FormatTest, SpaceBetweenKeywordAndLiteral) { 27933 verifyFormat("return .5;"); 27934 verifyFormat("return not '5';"); 27935 verifyFormat("return sizeof \"5\";"); 27936 } 27937 27938 TEST_F(FormatTest, BreakBinaryOperations) { 27939 auto Style = getLLVMStyleWithColumns(60); 27940 EXPECT_EQ(Style.BreakBinaryOperations, FormatStyle::BBO_Never); 27941 27942 // Logical operations 27943 verifyFormat("if (condition1 && condition2) {\n" 27944 "}", 27945 Style); 27946 27947 verifyFormat("if (condition1 && condition2 &&\n" 27948 " (condition3 || condition4) && condition5 &&\n" 27949 " condition6) {\n" 27950 "}", 27951 Style); 27952 27953 verifyFormat("if (loooooooooooooooooooooongcondition1 &&\n" 27954 " loooooooooooooooooooooongcondition2) {\n" 27955 "}", 27956 Style); 27957 27958 // Arithmetic 27959 verifyFormat("const int result = lhs + rhs;", Style); 27960 27961 verifyFormat("const int result = loooooooongop1 + looooooooongop2 +\n" 27962 " loooooooooooooooooooooongop3;", 27963 Style); 27964 27965 verifyFormat("result = longOperand1 + longOperand2 -\n" 27966 " (longOperand3 + longOperand4) -\n" 27967 " longOperand5 * longOperand6;", 27968 Style); 27969 27970 verifyFormat("const int result =\n" 27971 " operand1 + operand2 - (operand3 + operand4);", 27972 Style); 27973 27974 Style.BreakBinaryOperations = FormatStyle::BBO_OnePerLine; 27975 27976 // Logical operations 27977 verifyFormat("if (condition1 && condition2) {\n" 27978 "}", 27979 Style); 27980 27981 verifyFormat("if (condition1 && // comment\n" 27982 " condition2 &&\n" 27983 " (condition3 || condition4) && // comment\n" 27984 " condition5 &&\n" 27985 " condition6) {\n" 27986 "}", 27987 Style); 27988 27989 verifyFormat("if (loooooooooooooooooooooongcondition1 &&\n" 27990 " loooooooooooooooooooooongcondition2) {\n" 27991 "}", 27992 Style); 27993 27994 // Arithmetic 27995 verifyFormat("const int result = lhs + rhs;", Style); 27996 27997 verifyFormat("result = loooooooooooooooooooooongop1 +\n" 27998 " loooooooooooooooooooooongop2 +\n" 27999 " loooooooooooooooooooooongop3;", 28000 Style); 28001 28002 verifyFormat("const int result =\n" 28003 " operand1 + operand2 - (operand3 + operand4);", 28004 Style); 28005 28006 verifyFormat("result = longOperand1 +\n" 28007 " longOperand2 -\n" 28008 " (longOperand3 + longOperand4) -\n" 28009 " longOperand5 +\n" 28010 " longOperand6;", 28011 Style); 28012 28013 verifyFormat("result = operand1 +\n" 28014 " operand2 -\n" 28015 " operand3 +\n" 28016 " operand4 -\n" 28017 " operand5 +\n" 28018 " operand6;", 28019 Style); 28020 28021 // Ensure mixed precedence operations are handled properly 28022 verifyFormat("result = op1 + op2 * op3 - op4;", Style); 28023 28024 verifyFormat("result = operand1 +\n" 28025 " operand2 /\n" 28026 " operand3 +\n" 28027 " operand4 /\n" 28028 " operand5 *\n" 28029 " operand6;", 28030 Style); 28031 28032 verifyFormat("result = operand1 *\n" 28033 " operand2 -\n" 28034 " operand3 *\n" 28035 " operand4 -\n" 28036 " operand5 +\n" 28037 " operand6;", 28038 Style); 28039 28040 verifyFormat("result = operand1 *\n" 28041 " (operand2 - operand3 * operand4) -\n" 28042 " operand5 +\n" 28043 " operand6;", 28044 Style); 28045 28046 verifyFormat("result = operand1.member *\n" 28047 " (operand2.member() - operand3->mem * operand4) -\n" 28048 " operand5.member() +\n" 28049 " operand6->member;", 28050 Style); 28051 28052 Style.BreakBinaryOperations = FormatStyle::BBO_RespectPrecedence; 28053 verifyFormat("result = op1 + op2 * op3 - op4;", Style); 28054 28055 verifyFormat("result = operand1 +\n" 28056 " operand2 / operand3 +\n" 28057 " operand4 / operand5 * operand6;", 28058 Style); 28059 28060 verifyFormat("result = operand1 * operand2 -\n" 28061 " operand3 * operand4 -\n" 28062 " operand5 +\n" 28063 " operand6;", 28064 Style); 28065 28066 verifyFormat("result = operand1 * (operand2 - operand3 * operand4) -\n" 28067 " operand5 +\n" 28068 " operand6;", 28069 Style); 28070 28071 verifyFormat("std::uint32_t a = byte_buffer[0] |\n" 28072 " byte_buffer[1] << 8 |\n" 28073 " byte_buffer[2] << 16 |\n" 28074 " byte_buffer[3] << 24;", 28075 Style); 28076 28077 Style.BreakBinaryOperations = FormatStyle::BBO_OnePerLine; 28078 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 28079 28080 // Logical operations 28081 verifyFormat("if (condition1 && condition2) {\n" 28082 "}", 28083 Style); 28084 28085 verifyFormat("if (loooooooooooooooooooooongcondition1\n" 28086 " && loooooooooooooooooooooongcondition2) {\n" 28087 "}", 28088 Style); 28089 28090 // Arithmetic 28091 verifyFormat("const int result = lhs + rhs;", Style); 28092 28093 verifyFormat("result = loooooooooooooooooooooongop1\n" 28094 " + loooooooooooooooooooooongop2\n" 28095 " + loooooooooooooooooooooongop3;", 28096 Style); 28097 28098 verifyFormat("const int result =\n" 28099 " operand1 + operand2 - (operand3 + operand4);", 28100 Style); 28101 28102 verifyFormat("result = longOperand1\n" 28103 " + longOperand2\n" 28104 " - (longOperand3 + longOperand4)\n" 28105 " - longOperand5\n" 28106 " + longOperand6;", 28107 Style); 28108 28109 verifyFormat("result = operand1\n" 28110 " + operand2\n" 28111 " - operand3\n" 28112 " + operand4\n" 28113 " - operand5\n" 28114 " + operand6;", 28115 Style); 28116 28117 // Ensure mixed precedence operations are handled properly 28118 verifyFormat("result = op1 + op2 * op3 - op4;", Style); 28119 28120 verifyFormat("result = operand1\n" 28121 " + operand2\n" 28122 " / operand3\n" 28123 " + operand4\n" 28124 " / operand5\n" 28125 " * operand6;", 28126 Style); 28127 28128 verifyFormat("result = operand1\n" 28129 " * operand2\n" 28130 " - operand3\n" 28131 " * operand4\n" 28132 " - operand5\n" 28133 " + operand6;", 28134 Style); 28135 28136 verifyFormat("result = operand1\n" 28137 " * (operand2 - operand3 * operand4)\n" 28138 " - operand5\n" 28139 " + operand6;", 28140 Style); 28141 28142 verifyFormat("std::uint32_t a = byte_buffer[0]\n" 28143 " | byte_buffer[1]\n" 28144 " << 8\n" 28145 " | byte_buffer[2]\n" 28146 " << 16\n" 28147 " | byte_buffer[3]\n" 28148 " << 24;", 28149 Style); 28150 28151 Style.BreakBinaryOperations = FormatStyle::BBO_RespectPrecedence; 28152 verifyFormat("result = op1 + op2 * op3 - op4;", Style); 28153 28154 verifyFormat("result = operand1\n" 28155 " + operand2 / operand3\n" 28156 " + operand4 / operand5 * operand6;", 28157 Style); 28158 28159 verifyFormat("result = operand1 * operand2\n" 28160 " - operand3 * operand4\n" 28161 " - operand5\n" 28162 " + operand6;", 28163 Style); 28164 28165 verifyFormat("result = operand1 * (operand2 - operand3 * operand4)\n" 28166 " - operand5\n" 28167 " + operand6;", 28168 Style); 28169 28170 verifyFormat("std::uint32_t a = byte_buffer[0]\n" 28171 " | byte_buffer[1] << 8\n" 28172 " | byte_buffer[2] << 16\n" 28173 " | byte_buffer[3] << 24;", 28174 Style); 28175 } 28176 28177 TEST_F(FormatTest, RemoveEmptyLinesInUnwrappedLines) { 28178 auto Style = getLLVMStyle(); 28179 Style.RemoveEmptyLinesInUnwrappedLines = true; 28180 28181 verifyFormat("int c = a + b;", 28182 "int c\n" 28183 "\n" 28184 " = a + b;", 28185 Style); 28186 28187 verifyFormat("enum : unsigned { AA = 0, BB } myEnum;", 28188 "enum : unsigned\n" 28189 "\n" 28190 "{\n" 28191 " AA = 0,\n" 28192 " BB\n" 28193 "} myEnum;", 28194 Style); 28195 28196 verifyFormat("class B : public E {\n" 28197 "private:\n" 28198 "};", 28199 "class B : public E\n" 28200 "\n" 28201 "{\n" 28202 "private:\n" 28203 "};", 28204 Style); 28205 28206 verifyFormat( 28207 "struct AAAAAAAAAAAAAAA test[3] = {{56, 23, \"hello\"}, {7, 5, \"!!\"}};", 28208 "struct AAAAAAAAAAAAAAA test[3] = {{56,\n" 28209 "\n" 28210 " 23, \"hello\"},\n" 28211 " {7, 5, \"!!\"}};", 28212 Style); 28213 28214 verifyFormat("int myFunction(int aaaaaaaaaaaaa, int ccccccccccccc, int d);", 28215 "int myFunction(\n" 28216 "\n" 28217 " int aaaaaaaaaaaaa,\n" 28218 "\n" 28219 " int ccccccccccccc, int d);", 28220 Style); 28221 28222 verifyFormat("switch (e) {\n" 28223 "case 1:\n" 28224 " return e;\n" 28225 "case 2:\n" 28226 " return 2;\n" 28227 "}", 28228 "switch (\n" 28229 "\n" 28230 " e) {\n" 28231 "case 1:\n" 28232 " return e;\n" 28233 "case 2:\n" 28234 " return 2;\n" 28235 "}", 28236 Style); 28237 28238 verifyFormat("while (true) {\n" 28239 "}", 28240 "while (\n" 28241 "\n" 28242 " true) {\n" 28243 "}", 28244 Style); 28245 28246 verifyFormat("void loooonFunctionIsVeryLongButNotAsLongAsJavaTypeNames(\n" 28247 " std::map<int, std::string> *outputMap);", 28248 "void loooonFunctionIsVeryLongButNotAsLongAsJavaTypeNames\n" 28249 "\n" 28250 " (std::map<int, std::string> *outputMap);", 28251 Style); 28252 } 28253 28254 TEST_F(FormatTest, KeepFormFeed) { 28255 auto Style = getLLVMStyle(); 28256 Style.KeepFormFeed = true; 28257 28258 constexpr StringRef NoFormFeed{"int i;\n" 28259 "\n" 28260 "void f();"}; 28261 verifyFormat(NoFormFeed, 28262 "int i;\n" 28263 " \f\n" 28264 "void f();", 28265 Style); 28266 verifyFormat(NoFormFeed, 28267 "int i;\n" 28268 "\n" 28269 "\fvoid f();", 28270 Style); 28271 verifyFormat(NoFormFeed, 28272 "\fint i;\n" 28273 "\n" 28274 "void f();", 28275 Style); 28276 verifyFormat(NoFormFeed, 28277 "int i;\n" 28278 "\n" 28279 "void f();\f", 28280 Style); 28281 28282 constexpr StringRef FormFeed{"int i;\n" 28283 "\f\n" 28284 "void f();"}; 28285 verifyNoChange(FormFeed, Style); 28286 28287 Style.LineEnding = FormatStyle::LE_LF; 28288 verifyFormat(FormFeed, 28289 "int i;\r\n" 28290 "\f\r\n" 28291 "void f();", 28292 Style); 28293 28294 constexpr StringRef FormFeedBeforeEmptyLine{"int i;\n" 28295 "\f\n" 28296 "\n" 28297 "void f();"}; 28298 Style.MaxEmptyLinesToKeep = 2; 28299 verifyFormat(FormFeedBeforeEmptyLine, 28300 "int i;\n" 28301 "\n" 28302 "\f\n" 28303 "void f();", 28304 Style); 28305 verifyFormat(FormFeedBeforeEmptyLine, 28306 "int i;\n" 28307 "\f\n" 28308 "\f\n" 28309 "void f();", 28310 Style); 28311 } 28312 28313 } // namespace 28314 } // namespace test 28315 } // namespace format 28316 } // namespace clang 28317