1 //===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "clang/Format/Format.h" 11 12 #include "../Tooling/ReplacementTest.h" 13 #include "FormatTestUtils.h" 14 15 #include "clang/Frontend/TextDiagnosticPrinter.h" 16 #include "llvm/Support/Debug.h" 17 #include "llvm/Support/MemoryBuffer.h" 18 #include "gtest/gtest.h" 19 20 #define DEBUG_TYPE "format-test" 21 22 using clang::tooling::ReplacementTest; 23 using clang::tooling::toReplacements; 24 25 namespace clang { 26 namespace format { 27 namespace { 28 29 FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); } 30 31 class FormatTest : public ::testing::Test { 32 protected: 33 enum StatusCheck { 34 SC_ExpectComplete, 35 SC_ExpectIncomplete, 36 SC_DoNotCheck 37 }; 38 39 std::string format(llvm::StringRef Code, 40 const FormatStyle &Style = getLLVMStyle(), 41 StatusCheck CheckComplete = SC_ExpectComplete) { 42 DEBUG(llvm::errs() << "---\n"); 43 DEBUG(llvm::errs() << Code << "\n\n"); 44 std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size())); 45 FormattingAttemptStatus Status; 46 tooling::Replacements Replaces = 47 reformat(Style, Code, Ranges, "<stdin>", &Status); 48 if (CheckComplete != SC_DoNotCheck) { 49 bool ExpectedCompleteFormat = CheckComplete == SC_ExpectComplete; 50 EXPECT_EQ(ExpectedCompleteFormat, Status.FormatComplete) 51 << Code << "\n\n"; 52 } 53 ReplacementCount = Replaces.size(); 54 auto Result = applyAllReplacements(Code, Replaces); 55 EXPECT_TRUE(static_cast<bool>(Result)); 56 DEBUG(llvm::errs() << "\n" << *Result << "\n\n"); 57 return *Result; 58 } 59 60 FormatStyle getStyleWithColumns(FormatStyle Style, unsigned ColumnLimit) { 61 Style.ColumnLimit = ColumnLimit; 62 return Style; 63 } 64 65 FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) { 66 return getStyleWithColumns(getLLVMStyle(), ColumnLimit); 67 } 68 69 FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) { 70 return getStyleWithColumns(getGoogleStyle(), ColumnLimit); 71 } 72 73 void verifyFormat(llvm::StringRef Expected, llvm::StringRef Code, 74 const FormatStyle &Style = getLLVMStyle()) { 75 EXPECT_EQ(Expected.str(), format(Code, Style)); 76 if (Style.Language == FormatStyle::LK_Cpp) { 77 // Objective-C++ is a superset of C++, so everything checked for C++ 78 // needs to be checked for Objective-C++ as well. 79 FormatStyle ObjCStyle = Style; 80 ObjCStyle.Language = FormatStyle::LK_ObjC; 81 EXPECT_EQ(Expected.str(), format(test::messUp(Code), ObjCStyle)); 82 } 83 } 84 85 void verifyFormat(llvm::StringRef Code, 86 const FormatStyle &Style = getLLVMStyle()) { 87 verifyFormat(Code, test::messUp(Code), Style); 88 } 89 90 void verifyIncompleteFormat(llvm::StringRef Code, 91 const FormatStyle &Style = getLLVMStyle()) { 92 EXPECT_EQ(Code.str(), 93 format(test::messUp(Code), Style, SC_ExpectIncomplete)); 94 } 95 96 void verifyGoogleFormat(llvm::StringRef Code) { 97 verifyFormat(Code, getGoogleStyle()); 98 } 99 100 void verifyIndependentOfContext(llvm::StringRef text) { 101 verifyFormat(text); 102 verifyFormat(llvm::Twine("void f() { " + text + " }").str()); 103 } 104 105 /// \brief Verify that clang-format does not crash on the given input. 106 void verifyNoCrash(llvm::StringRef Code, 107 const FormatStyle &Style = getLLVMStyle()) { 108 format(Code, Style, SC_DoNotCheck); 109 } 110 111 int ReplacementCount; 112 }; 113 114 TEST_F(FormatTest, MessUp) { 115 EXPECT_EQ("1 2 3", test::messUp("1 2 3")); 116 EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n")); 117 EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc")); 118 EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc")); 119 EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne")); 120 } 121 122 //===----------------------------------------------------------------------===// 123 // Basic function tests. 124 //===----------------------------------------------------------------------===// 125 126 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) { 127 EXPECT_EQ(";", format(";")); 128 } 129 130 TEST_F(FormatTest, FormatsGlobalStatementsAt0) { 131 EXPECT_EQ("int i;", format(" int i;")); 132 EXPECT_EQ("\nint i;", format(" \n\t \v \f int i;")); 133 EXPECT_EQ("int i;\nint j;", format(" int i; int j;")); 134 EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;")); 135 } 136 137 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) { 138 EXPECT_EQ("int i;", format("int\ni;")); 139 } 140 141 TEST_F(FormatTest, FormatsNestedBlockStatements) { 142 EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}")); 143 } 144 145 TEST_F(FormatTest, FormatsNestedCall) { 146 verifyFormat("Method(f1, f2(f3));"); 147 verifyFormat("Method(f1(f2, f3()));"); 148 verifyFormat("Method(f1(f2, (f3())));"); 149 } 150 151 TEST_F(FormatTest, NestedNameSpecifiers) { 152 verifyFormat("vector<::Type> v;"); 153 verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())"); 154 verifyFormat("static constexpr bool Bar = decltype(bar())::value;"); 155 verifyFormat("bool a = 2 < ::SomeFunction();"); 156 verifyFormat("ALWAYS_INLINE ::std::string getName();"); 157 verifyFormat("some::string getName();"); 158 } 159 160 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) { 161 EXPECT_EQ("if (a) {\n" 162 " f();\n" 163 "}", 164 format("if(a){f();}")); 165 EXPECT_EQ(4, ReplacementCount); 166 EXPECT_EQ("if (a) {\n" 167 " f();\n" 168 "}", 169 format("if (a) {\n" 170 " f();\n" 171 "}")); 172 EXPECT_EQ(0, ReplacementCount); 173 EXPECT_EQ("/*\r\n" 174 "\r\n" 175 "*/\r\n", 176 format("/*\r\n" 177 "\r\n" 178 "*/\r\n")); 179 EXPECT_EQ(0, ReplacementCount); 180 } 181 182 TEST_F(FormatTest, RemovesEmptyLines) { 183 EXPECT_EQ("class C {\n" 184 " int i;\n" 185 "};", 186 format("class C {\n" 187 " int i;\n" 188 "\n" 189 "};")); 190 191 // Don't remove empty lines at the start of namespaces or extern "C" blocks. 192 EXPECT_EQ("namespace N {\n" 193 "\n" 194 "int i;\n" 195 "}", 196 format("namespace N {\n" 197 "\n" 198 "int i;\n" 199 "}", 200 getGoogleStyle())); 201 EXPECT_EQ("extern /**/ \"C\" /**/ {\n" 202 "\n" 203 "int i;\n" 204 "}", 205 format("extern /**/ \"C\" /**/ {\n" 206 "\n" 207 "int i;\n" 208 "}", 209 getGoogleStyle())); 210 211 // ...but do keep inlining and removing empty lines for non-block extern "C" 212 // functions. 213 verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle()); 214 EXPECT_EQ("extern \"C\" int f() {\n" 215 " int i = 42;\n" 216 " return i;\n" 217 "}", 218 format("extern \"C\" int f() {\n" 219 "\n" 220 " int i = 42;\n" 221 " return i;\n" 222 "}", 223 getGoogleStyle())); 224 225 // Remove empty lines at the beginning and end of blocks. 226 EXPECT_EQ("void f() {\n" 227 "\n" 228 " if (a) {\n" 229 "\n" 230 " f();\n" 231 " }\n" 232 "}", 233 format("void f() {\n" 234 "\n" 235 " if (a) {\n" 236 "\n" 237 " f();\n" 238 "\n" 239 " }\n" 240 "\n" 241 "}", 242 getLLVMStyle())); 243 EXPECT_EQ("void f() {\n" 244 " if (a) {\n" 245 " f();\n" 246 " }\n" 247 "}", 248 format("void f() {\n" 249 "\n" 250 " if (a) {\n" 251 "\n" 252 " f();\n" 253 "\n" 254 " }\n" 255 "\n" 256 "}", 257 getGoogleStyle())); 258 259 // Don't remove empty lines in more complex control statements. 260 EXPECT_EQ("void f() {\n" 261 " if (a) {\n" 262 " f();\n" 263 "\n" 264 " } else if (b) {\n" 265 " f();\n" 266 " }\n" 267 "}", 268 format("void f() {\n" 269 " if (a) {\n" 270 " f();\n" 271 "\n" 272 " } else if (b) {\n" 273 " f();\n" 274 "\n" 275 " }\n" 276 "\n" 277 "}")); 278 279 // FIXME: This is slightly inconsistent. 280 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 281 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 282 EXPECT_EQ("namespace {\n" 283 "int i;\n" 284 "}", 285 format("namespace {\n" 286 "int i;\n" 287 "\n" 288 "}", LLVMWithNoNamespaceFix)); 289 EXPECT_EQ("namespace {\n" 290 "int i;\n" 291 "}", 292 format("namespace {\n" 293 "int i;\n" 294 "\n" 295 "}")); 296 EXPECT_EQ("namespace {\n" 297 "int i;\n" 298 "\n" 299 "} // namespace", 300 format("namespace {\n" 301 "int i;\n" 302 "\n" 303 "} // namespace")); 304 305 FormatStyle Style = getLLVMStyle(); 306 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 307 Style.MaxEmptyLinesToKeep = 2; 308 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 309 Style.BraceWrapping.AfterClass = true; 310 Style.BraceWrapping.AfterFunction = true; 311 Style.KeepEmptyLinesAtTheStartOfBlocks = false; 312 313 EXPECT_EQ("class Foo\n" 314 "{\n" 315 " Foo() {}\n" 316 "\n" 317 " void funk() {}\n" 318 "};", 319 format("class Foo\n" 320 "{\n" 321 " Foo()\n" 322 " {\n" 323 " }\n" 324 "\n" 325 " void funk() {}\n" 326 "};", 327 Style)); 328 } 329 330 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) { 331 verifyFormat("x = (a) and (b);"); 332 verifyFormat("x = (a) or (b);"); 333 verifyFormat("x = (a) bitand (b);"); 334 verifyFormat("x = (a) bitor (b);"); 335 verifyFormat("x = (a) not_eq (b);"); 336 verifyFormat("x = (a) and_eq (b);"); 337 verifyFormat("x = (a) or_eq (b);"); 338 verifyFormat("x = (a) xor (b);"); 339 } 340 341 TEST_F(FormatTest, RecognizesUnaryOperatorKeywords) { 342 verifyFormat("x = compl(a);"); 343 verifyFormat("x = not(a);"); 344 verifyFormat("x = bitand(a);"); 345 // Unary operator must not be merged with the next identifier 346 verifyFormat("x = compl a;"); 347 verifyFormat("x = not a;"); 348 verifyFormat("x = bitand a;"); 349 } 350 351 //===----------------------------------------------------------------------===// 352 // Tests for control statements. 353 //===----------------------------------------------------------------------===// 354 355 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) { 356 verifyFormat("if (true)\n f();\ng();"); 357 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); 358 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); 359 verifyFormat("if constexpr (true)\n" 360 " f();\ng();"); 361 verifyFormat("if constexpr (a)\n" 362 " if constexpr (b)\n" 363 " if constexpr (c)\n" 364 " g();\n" 365 "h();"); 366 verifyFormat("if constexpr (a)\n" 367 " if constexpr (b) {\n" 368 " f();\n" 369 " }\n" 370 "g();"); 371 372 FormatStyle AllowsMergedIf = getLLVMStyle(); 373 AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left; 374 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 375 verifyFormat("if (a)\n" 376 " // comment\n" 377 " f();", 378 AllowsMergedIf); 379 verifyFormat("{\n" 380 " if (a)\n" 381 " label:\n" 382 " f();\n" 383 "}", 384 AllowsMergedIf); 385 verifyFormat("#define A \\\n" 386 " if (a) \\\n" 387 " label: \\\n" 388 " f()", 389 AllowsMergedIf); 390 verifyFormat("if (a)\n" 391 " ;", 392 AllowsMergedIf); 393 verifyFormat("if (a)\n" 394 " if (b) return;", 395 AllowsMergedIf); 396 397 verifyFormat("if (a) // Can't merge this\n" 398 " f();\n", 399 AllowsMergedIf); 400 verifyFormat("if (a) /* still don't merge */\n" 401 " f();", 402 AllowsMergedIf); 403 verifyFormat("if (a) { // Never merge this\n" 404 " f();\n" 405 "}", 406 AllowsMergedIf); 407 verifyFormat("if (a) { /* Never merge this */\n" 408 " f();\n" 409 "}", 410 AllowsMergedIf); 411 412 AllowsMergedIf.ColumnLimit = 14; 413 verifyFormat("if (a) return;", AllowsMergedIf); 414 verifyFormat("if (aaaaaaaaa)\n" 415 " return;", 416 AllowsMergedIf); 417 418 AllowsMergedIf.ColumnLimit = 13; 419 verifyFormat("if (a)\n return;", AllowsMergedIf); 420 } 421 422 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) { 423 FormatStyle AllowsMergedLoops = getLLVMStyle(); 424 AllowsMergedLoops.AllowShortLoopsOnASingleLine = true; 425 verifyFormat("while (true) continue;", AllowsMergedLoops); 426 verifyFormat("for (;;) continue;", AllowsMergedLoops); 427 verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops); 428 verifyFormat("while (true)\n" 429 " ;", 430 AllowsMergedLoops); 431 verifyFormat("for (;;)\n" 432 " ;", 433 AllowsMergedLoops); 434 verifyFormat("for (;;)\n" 435 " for (;;) continue;", 436 AllowsMergedLoops); 437 verifyFormat("for (;;) // Can't merge this\n" 438 " continue;", 439 AllowsMergedLoops); 440 verifyFormat("for (;;) /* still don't merge */\n" 441 " continue;", 442 AllowsMergedLoops); 443 } 444 445 TEST_F(FormatTest, FormatShortBracedStatements) { 446 FormatStyle AllowSimpleBracedStatements = getLLVMStyle(); 447 AllowSimpleBracedStatements.ColumnLimit = 40; 448 AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true; 449 450 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true; 451 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 452 453 AllowSimpleBracedStatements.BreakBeforeBraces = FormatStyle::BS_Custom; 454 AllowSimpleBracedStatements.BraceWrapping.AfterFunction = true; 455 AllowSimpleBracedStatements.BraceWrapping.SplitEmptyRecord = false; 456 457 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 458 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements); 459 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 460 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 461 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 462 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements); 463 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 464 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 465 verifyFormat("if (true) {\n" 466 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 467 "}", 468 AllowSimpleBracedStatements); 469 verifyFormat("if (true) { //\n" 470 " f();\n" 471 "}", 472 AllowSimpleBracedStatements); 473 verifyFormat("if (true) {\n" 474 " f();\n" 475 " f();\n" 476 "}", 477 AllowSimpleBracedStatements); 478 verifyFormat("if (true) {\n" 479 " f();\n" 480 "} else {\n" 481 " f();\n" 482 "}", 483 AllowSimpleBracedStatements); 484 485 verifyFormat("struct A2 {\n" 486 " int X;\n" 487 "};", 488 AllowSimpleBracedStatements); 489 verifyFormat("typedef struct A2 {\n" 490 " int X;\n" 491 "} A2_t;", 492 AllowSimpleBracedStatements); 493 verifyFormat("template <int> struct A2 {\n" 494 " struct B {};\n" 495 "};", 496 AllowSimpleBracedStatements); 497 498 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false; 499 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 500 verifyFormat("if (true) {\n" 501 " f();\n" 502 "}", 503 AllowSimpleBracedStatements); 504 verifyFormat("if (true) {\n" 505 " f();\n" 506 "} else {\n" 507 " f();\n" 508 "}", 509 AllowSimpleBracedStatements); 510 511 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 512 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 513 verifyFormat("while (true) {\n" 514 " f();\n" 515 "}", 516 AllowSimpleBracedStatements); 517 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 518 verifyFormat("for (;;) {\n" 519 " f();\n" 520 "}", 521 AllowSimpleBracedStatements); 522 523 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true; 524 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 525 AllowSimpleBracedStatements.BraceWrapping.AfterControlStatement = true; 526 527 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 528 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements); 529 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 530 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 531 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 532 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements); 533 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 534 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 535 verifyFormat("if (true)\n" 536 "{\n" 537 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 538 "}", 539 AllowSimpleBracedStatements); 540 verifyFormat("if (true)\n" 541 "{ //\n" 542 " f();\n" 543 "}", 544 AllowSimpleBracedStatements); 545 verifyFormat("if (true)\n" 546 "{\n" 547 " f();\n" 548 " f();\n" 549 "}", 550 AllowSimpleBracedStatements); 551 verifyFormat("if (true)\n" 552 "{\n" 553 " f();\n" 554 "} else\n" 555 "{\n" 556 " f();\n" 557 "}", 558 AllowSimpleBracedStatements); 559 560 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false; 561 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 562 verifyFormat("if (true)\n" 563 "{\n" 564 " f();\n" 565 "}", 566 AllowSimpleBracedStatements); 567 verifyFormat("if (true)\n" 568 "{\n" 569 " f();\n" 570 "} else\n" 571 "{\n" 572 " f();\n" 573 "}", 574 AllowSimpleBracedStatements); 575 576 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 577 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 578 verifyFormat("while (true)\n" 579 "{\n" 580 " f();\n" 581 "}", 582 AllowSimpleBracedStatements); 583 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 584 verifyFormat("for (;;)\n" 585 "{\n" 586 " f();\n" 587 "}", 588 AllowSimpleBracedStatements); 589 } 590 591 TEST_F(FormatTest, ParseIfElse) { 592 verifyFormat("if (true)\n" 593 " if (true)\n" 594 " if (true)\n" 595 " f();\n" 596 " else\n" 597 " g();\n" 598 " else\n" 599 " h();\n" 600 "else\n" 601 " i();"); 602 verifyFormat("if (true)\n" 603 " if (true)\n" 604 " if (true) {\n" 605 " if (true)\n" 606 " f();\n" 607 " } else {\n" 608 " g();\n" 609 " }\n" 610 " else\n" 611 " h();\n" 612 "else {\n" 613 " i();\n" 614 "}"); 615 verifyFormat("if (true)\n" 616 " if constexpr (true)\n" 617 " if (true) {\n" 618 " if constexpr (true)\n" 619 " f();\n" 620 " } else {\n" 621 " g();\n" 622 " }\n" 623 " else\n" 624 " h();\n" 625 "else {\n" 626 " i();\n" 627 "}"); 628 verifyFormat("void f() {\n" 629 " if (a) {\n" 630 " } else {\n" 631 " }\n" 632 "}"); 633 } 634 635 TEST_F(FormatTest, ElseIf) { 636 verifyFormat("if (a) {\n} else if (b) {\n}"); 637 verifyFormat("if (a)\n" 638 " f();\n" 639 "else if (b)\n" 640 " g();\n" 641 "else\n" 642 " h();"); 643 verifyFormat("if constexpr (a)\n" 644 " f();\n" 645 "else if constexpr (b)\n" 646 " g();\n" 647 "else\n" 648 " h();"); 649 verifyFormat("if (a) {\n" 650 " f();\n" 651 "}\n" 652 "// or else ..\n" 653 "else {\n" 654 " g()\n" 655 "}"); 656 657 verifyFormat("if (a) {\n" 658 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 659 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 660 "}"); 661 verifyFormat("if (a) {\n" 662 "} else if (\n" 663 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 664 "}", 665 getLLVMStyleWithColumns(62)); 666 verifyFormat("if (a) {\n" 667 "} else if constexpr (\n" 668 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 669 "}", 670 getLLVMStyleWithColumns(62)); 671 } 672 673 TEST_F(FormatTest, FormatsForLoop) { 674 verifyFormat( 675 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" 676 " ++VeryVeryLongLoopVariable)\n" 677 " ;"); 678 verifyFormat("for (;;)\n" 679 " f();"); 680 verifyFormat("for (;;) {\n}"); 681 verifyFormat("for (;;) {\n" 682 " f();\n" 683 "}"); 684 verifyFormat("for (int i = 0; (i < 10); ++i) {\n}"); 685 686 verifyFormat( 687 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 688 " E = UnwrappedLines.end();\n" 689 " I != E; ++I) {\n}"); 690 691 verifyFormat( 692 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" 693 " ++IIIII) {\n}"); 694 verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n" 695 " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n" 696 " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}"); 697 verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n" 698 " I = FD->getDeclsInPrototypeScope().begin(),\n" 699 " E = FD->getDeclsInPrototypeScope().end();\n" 700 " I != E; ++I) {\n}"); 701 verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n" 702 " I = Container.begin(),\n" 703 " E = Container.end();\n" 704 " I != E; ++I) {\n}", 705 getLLVMStyleWithColumns(76)); 706 707 verifyFormat( 708 "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 709 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n" 710 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 711 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 712 " ++aaaaaaaaaaa) {\n}"); 713 verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 714 " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n" 715 " ++i) {\n}"); 716 verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n" 717 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 718 "}"); 719 verifyFormat("for (some_namespace::SomeIterator iter( // force break\n" 720 " aaaaaaaaaa);\n" 721 " iter; ++iter) {\n" 722 "}"); 723 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 724 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 725 " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n" 726 " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {"); 727 728 FormatStyle NoBinPacking = getLLVMStyle(); 729 NoBinPacking.BinPackParameters = false; 730 verifyFormat("for (int aaaaaaaaaaa = 1;\n" 731 " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n" 732 " aaaaaaaaaaaaaaaa,\n" 733 " aaaaaaaaaaaaaaaa,\n" 734 " aaaaaaaaaaaaaaaa);\n" 735 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 736 "}", 737 NoBinPacking); 738 verifyFormat( 739 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 740 " E = UnwrappedLines.end();\n" 741 " I != E;\n" 742 " ++I) {\n}", 743 NoBinPacking); 744 745 FormatStyle AlignLeft = getLLVMStyle(); 746 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 747 verifyFormat("for (A* a = start; a < end; ++a, ++value) {\n}", AlignLeft); 748 } 749 750 TEST_F(FormatTest, RangeBasedForLoops) { 751 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 752 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 753 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n" 754 " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}"); 755 verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n" 756 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 757 verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n" 758 " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}"); 759 } 760 761 TEST_F(FormatTest, ForEachLoops) { 762 verifyFormat("void f() {\n" 763 " foreach (Item *item, itemlist) {}\n" 764 " Q_FOREACH (Item *item, itemlist) {}\n" 765 " BOOST_FOREACH (Item *item, itemlist) {}\n" 766 " UNKNOWN_FORACH(Item * item, itemlist) {}\n" 767 "}"); 768 769 // As function-like macros. 770 verifyFormat("#define foreach(x, y)\n" 771 "#define Q_FOREACH(x, y)\n" 772 "#define BOOST_FOREACH(x, y)\n" 773 "#define UNKNOWN_FOREACH(x, y)\n"); 774 775 // Not as function-like macros. 776 verifyFormat("#define foreach (x, y)\n" 777 "#define Q_FOREACH (x, y)\n" 778 "#define BOOST_FOREACH (x, y)\n" 779 "#define UNKNOWN_FOREACH (x, y)\n"); 780 } 781 782 TEST_F(FormatTest, FormatsWhileLoop) { 783 verifyFormat("while (true) {\n}"); 784 verifyFormat("while (true)\n" 785 " f();"); 786 verifyFormat("while () {\n}"); 787 verifyFormat("while () {\n" 788 " f();\n" 789 "}"); 790 } 791 792 TEST_F(FormatTest, FormatsDoWhile) { 793 verifyFormat("do {\n" 794 " do_something();\n" 795 "} while (something());"); 796 verifyFormat("do\n" 797 " do_something();\n" 798 "while (something());"); 799 } 800 801 TEST_F(FormatTest, FormatsSwitchStatement) { 802 verifyFormat("switch (x) {\n" 803 "case 1:\n" 804 " f();\n" 805 " break;\n" 806 "case kFoo:\n" 807 "case ns::kBar:\n" 808 "case kBaz:\n" 809 " break;\n" 810 "default:\n" 811 " g();\n" 812 " break;\n" 813 "}"); 814 verifyFormat("switch (x) {\n" 815 "case 1: {\n" 816 " f();\n" 817 " break;\n" 818 "}\n" 819 "case 2: {\n" 820 " break;\n" 821 "}\n" 822 "}"); 823 verifyFormat("switch (x) {\n" 824 "case 1: {\n" 825 " f();\n" 826 " {\n" 827 " g();\n" 828 " h();\n" 829 " }\n" 830 " break;\n" 831 "}\n" 832 "}"); 833 verifyFormat("switch (x) {\n" 834 "case 1: {\n" 835 " f();\n" 836 " if (foo) {\n" 837 " g();\n" 838 " h();\n" 839 " }\n" 840 " break;\n" 841 "}\n" 842 "}"); 843 verifyFormat("switch (x) {\n" 844 "case 1: {\n" 845 " f();\n" 846 " g();\n" 847 "} break;\n" 848 "}"); 849 verifyFormat("switch (test)\n" 850 " ;"); 851 verifyFormat("switch (x) {\n" 852 "default: {\n" 853 " // Do nothing.\n" 854 "}\n" 855 "}"); 856 verifyFormat("switch (x) {\n" 857 "// comment\n" 858 "// if 1, do f()\n" 859 "case 1:\n" 860 " f();\n" 861 "}"); 862 verifyFormat("switch (x) {\n" 863 "case 1:\n" 864 " // Do amazing stuff\n" 865 " {\n" 866 " f();\n" 867 " g();\n" 868 " }\n" 869 " break;\n" 870 "}"); 871 verifyFormat("#define A \\\n" 872 " switch (x) { \\\n" 873 " case a: \\\n" 874 " foo = b; \\\n" 875 " }", 876 getLLVMStyleWithColumns(20)); 877 verifyFormat("#define OPERATION_CASE(name) \\\n" 878 " case OP_name: \\\n" 879 " return operations::Operation##name\n", 880 getLLVMStyleWithColumns(40)); 881 verifyFormat("switch (x) {\n" 882 "case 1:;\n" 883 "default:;\n" 884 " int i;\n" 885 "}"); 886 887 verifyGoogleFormat("switch (x) {\n" 888 " case 1:\n" 889 " f();\n" 890 " break;\n" 891 " case kFoo:\n" 892 " case ns::kBar:\n" 893 " case kBaz:\n" 894 " break;\n" 895 " default:\n" 896 " g();\n" 897 " break;\n" 898 "}"); 899 verifyGoogleFormat("switch (x) {\n" 900 " case 1: {\n" 901 " f();\n" 902 " break;\n" 903 " }\n" 904 "}"); 905 verifyGoogleFormat("switch (test)\n" 906 " ;"); 907 908 verifyGoogleFormat("#define OPERATION_CASE(name) \\\n" 909 " case OP_name: \\\n" 910 " return operations::Operation##name\n"); 911 verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n" 912 " // Get the correction operation class.\n" 913 " switch (OpCode) {\n" 914 " CASE(Add);\n" 915 " CASE(Subtract);\n" 916 " default:\n" 917 " return operations::Unknown;\n" 918 " }\n" 919 "#undef OPERATION_CASE\n" 920 "}"); 921 verifyFormat("DEBUG({\n" 922 " switch (x) {\n" 923 " case A:\n" 924 " f();\n" 925 " break;\n" 926 " // fallthrough\n" 927 " case B:\n" 928 " g();\n" 929 " break;\n" 930 " }\n" 931 "});"); 932 EXPECT_EQ("DEBUG({\n" 933 " switch (x) {\n" 934 " case A:\n" 935 " f();\n" 936 " break;\n" 937 " // On B:\n" 938 " case B:\n" 939 " g();\n" 940 " break;\n" 941 " }\n" 942 "});", 943 format("DEBUG({\n" 944 " switch (x) {\n" 945 " case A:\n" 946 " f();\n" 947 " break;\n" 948 " // On B:\n" 949 " case B:\n" 950 " g();\n" 951 " break;\n" 952 " }\n" 953 "});", 954 getLLVMStyle())); 955 verifyFormat("switch (a) {\n" 956 "case (b):\n" 957 " return;\n" 958 "}"); 959 960 verifyFormat("switch (a) {\n" 961 "case some_namespace::\n" 962 " some_constant:\n" 963 " return;\n" 964 "}", 965 getLLVMStyleWithColumns(34)); 966 } 967 968 TEST_F(FormatTest, CaseRanges) { 969 verifyFormat("switch (x) {\n" 970 "case 'A' ... 'Z':\n" 971 "case 1 ... 5:\n" 972 "case a ... b:\n" 973 " break;\n" 974 "}"); 975 } 976 977 TEST_F(FormatTest, ShortCaseLabels) { 978 FormatStyle Style = getLLVMStyle(); 979 Style.AllowShortCaseLabelsOnASingleLine = true; 980 verifyFormat("switch (a) {\n" 981 "case 1: x = 1; break;\n" 982 "case 2: return;\n" 983 "case 3:\n" 984 "case 4:\n" 985 "case 5: return;\n" 986 "case 6: // comment\n" 987 " return;\n" 988 "case 7:\n" 989 " // comment\n" 990 " return;\n" 991 "case 8:\n" 992 " x = 8; // comment\n" 993 " break;\n" 994 "default: y = 1; break;\n" 995 "}", 996 Style); 997 verifyFormat("switch (a) {\n" 998 "case 0: return; // comment\n" 999 "case 1: break; // comment\n" 1000 "case 2: return;\n" 1001 "// comment\n" 1002 "case 3: return;\n" 1003 "// comment 1\n" 1004 "// comment 2\n" 1005 "// comment 3\n" 1006 "case 4: break; /* comment */\n" 1007 "case 5:\n" 1008 " // comment\n" 1009 " break;\n" 1010 "case 6: /* comment */ x = 1; break;\n" 1011 "case 7: x = /* comment */ 1; break;\n" 1012 "case 8:\n" 1013 " x = 1; /* comment */\n" 1014 " break;\n" 1015 "case 9:\n" 1016 " break; // comment line 1\n" 1017 " // comment line 2\n" 1018 "}", 1019 Style); 1020 EXPECT_EQ("switch (a) {\n" 1021 "case 1:\n" 1022 " x = 8;\n" 1023 " // fall through\n" 1024 "case 2: x = 8;\n" 1025 "// comment\n" 1026 "case 3:\n" 1027 " return; /* comment line 1\n" 1028 " * comment line 2 */\n" 1029 "case 4: i = 8;\n" 1030 "// something else\n" 1031 "#if FOO\n" 1032 "case 5: break;\n" 1033 "#endif\n" 1034 "}", 1035 format("switch (a) {\n" 1036 "case 1: x = 8;\n" 1037 " // fall through\n" 1038 "case 2:\n" 1039 " x = 8;\n" 1040 "// comment\n" 1041 "case 3:\n" 1042 " return; /* comment line 1\n" 1043 " * comment line 2 */\n" 1044 "case 4:\n" 1045 " i = 8;\n" 1046 "// something else\n" 1047 "#if FOO\n" 1048 "case 5: break;\n" 1049 "#endif\n" 1050 "}", 1051 Style)); 1052 EXPECT_EQ("switch (a) {\n" "case 0:\n" 1053 " return; // long long long long long long long long long long long long comment\n" 1054 " // line\n" "}", 1055 format("switch (a) {\n" 1056 "case 0: return; // long long long long long long long long long long long long comment line\n" 1057 "}", 1058 Style)); 1059 EXPECT_EQ("switch (a) {\n" 1060 "case 0:\n" 1061 " return; /* long long long long long long long long long long long long comment\n" 1062 " line */\n" 1063 "}", 1064 format("switch (a) {\n" 1065 "case 0: return; /* long long long long long long long long long long long long comment line */\n" 1066 "}", 1067 Style)); 1068 verifyFormat("switch (a) {\n" 1069 "#if FOO\n" 1070 "case 0: return 0;\n" 1071 "#endif\n" 1072 "}", 1073 Style); 1074 verifyFormat("switch (a) {\n" 1075 "case 1: {\n" 1076 "}\n" 1077 "case 2: {\n" 1078 " return;\n" 1079 "}\n" 1080 "case 3: {\n" 1081 " x = 1;\n" 1082 " return;\n" 1083 "}\n" 1084 "case 4:\n" 1085 " if (x)\n" 1086 " return;\n" 1087 "}", 1088 Style); 1089 Style.ColumnLimit = 21; 1090 verifyFormat("switch (a) {\n" 1091 "case 1: x = 1; break;\n" 1092 "case 2: return;\n" 1093 "case 3:\n" 1094 "case 4:\n" 1095 "case 5: return;\n" 1096 "default:\n" 1097 " y = 1;\n" 1098 " break;\n" 1099 "}", 1100 Style); 1101 } 1102 1103 TEST_F(FormatTest, FormatsLabels) { 1104 verifyFormat("void f() {\n" 1105 " some_code();\n" 1106 "test_label:\n" 1107 " some_other_code();\n" 1108 " {\n" 1109 " some_more_code();\n" 1110 " another_label:\n" 1111 " some_more_code();\n" 1112 " }\n" 1113 "}"); 1114 verifyFormat("{\n" 1115 " some_code();\n" 1116 "test_label:\n" 1117 " some_other_code();\n" 1118 "}"); 1119 verifyFormat("{\n" 1120 " some_code();\n" 1121 "test_label:;\n" 1122 " int i = 0;\n" 1123 "}"); 1124 } 1125 1126 //===----------------------------------------------------------------------===// 1127 // Tests for classes, namespaces, etc. 1128 //===----------------------------------------------------------------------===// 1129 1130 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { 1131 verifyFormat("class A {};"); 1132 } 1133 1134 TEST_F(FormatTest, UnderstandsAccessSpecifiers) { 1135 verifyFormat("class A {\n" 1136 "public:\n" 1137 "public: // comment\n" 1138 "protected:\n" 1139 "private:\n" 1140 " void f() {}\n" 1141 "};"); 1142 verifyGoogleFormat("class A {\n" 1143 " public:\n" 1144 " protected:\n" 1145 " private:\n" 1146 " void f() {}\n" 1147 "};"); 1148 verifyFormat("class A {\n" 1149 "public slots:\n" 1150 " void f1() {}\n" 1151 "public Q_SLOTS:\n" 1152 " void f2() {}\n" 1153 "protected slots:\n" 1154 " void f3() {}\n" 1155 "protected Q_SLOTS:\n" 1156 " void f4() {}\n" 1157 "private slots:\n" 1158 " void f5() {}\n" 1159 "private Q_SLOTS:\n" 1160 " void f6() {}\n" 1161 "signals:\n" 1162 " void g1();\n" 1163 "Q_SIGNALS:\n" 1164 " void g2();\n" 1165 "};"); 1166 1167 // Don't interpret 'signals' the wrong way. 1168 verifyFormat("signals.set();"); 1169 verifyFormat("for (Signals signals : f()) {\n}"); 1170 verifyFormat("{\n" 1171 " signals.set(); // This needs indentation.\n" 1172 "}"); 1173 verifyFormat("void f() {\n" 1174 "label:\n" 1175 " signals.baz();\n" 1176 "}"); 1177 } 1178 1179 TEST_F(FormatTest, SeparatesLogicalBlocks) { 1180 EXPECT_EQ("class A {\n" 1181 "public:\n" 1182 " void f();\n" 1183 "\n" 1184 "private:\n" 1185 " void g() {}\n" 1186 " // test\n" 1187 "protected:\n" 1188 " int h;\n" 1189 "};", 1190 format("class A {\n" 1191 "public:\n" 1192 "void f();\n" 1193 "private:\n" 1194 "void g() {}\n" 1195 "// test\n" 1196 "protected:\n" 1197 "int h;\n" 1198 "};")); 1199 EXPECT_EQ("class A {\n" 1200 "protected:\n" 1201 "public:\n" 1202 " void f();\n" 1203 "};", 1204 format("class A {\n" 1205 "protected:\n" 1206 "\n" 1207 "public:\n" 1208 "\n" 1209 " void f();\n" 1210 "};")); 1211 1212 // Even ensure proper spacing inside macros. 1213 EXPECT_EQ("#define B \\\n" 1214 " class A { \\\n" 1215 " protected: \\\n" 1216 " public: \\\n" 1217 " void f(); \\\n" 1218 " };", 1219 format("#define B \\\n" 1220 " class A { \\\n" 1221 " protected: \\\n" 1222 " \\\n" 1223 " public: \\\n" 1224 " \\\n" 1225 " void f(); \\\n" 1226 " };", 1227 getGoogleStyle())); 1228 // But don't remove empty lines after macros ending in access specifiers. 1229 EXPECT_EQ("#define A private:\n" 1230 "\n" 1231 "int i;", 1232 format("#define A private:\n" 1233 "\n" 1234 "int i;")); 1235 } 1236 1237 TEST_F(FormatTest, FormatsClasses) { 1238 verifyFormat("class A : public B {};"); 1239 verifyFormat("class A : public ::B {};"); 1240 1241 verifyFormat( 1242 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 1243 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 1244 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 1245 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 1246 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 1247 verifyFormat( 1248 "class A : public B, public C, public D, public E, public F {};"); 1249 verifyFormat("class AAAAAAAAAAAA : public B,\n" 1250 " public C,\n" 1251 " public D,\n" 1252 " public E,\n" 1253 " public F,\n" 1254 " public G {};"); 1255 1256 verifyFormat("class\n" 1257 " ReallyReallyLongClassName {\n" 1258 " int i;\n" 1259 "};", 1260 getLLVMStyleWithColumns(32)); 1261 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 1262 " aaaaaaaaaaaaaaaa> {};"); 1263 verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n" 1264 " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n" 1265 " aaaaaaaaaaaaaaaaaaaaaa> {};"); 1266 verifyFormat("template <class R, class C>\n" 1267 "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n" 1268 " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};"); 1269 verifyFormat("class ::A::B {};"); 1270 } 1271 1272 TEST_F(FormatTest, BreakBeforeInheritanceComma) { 1273 FormatStyle StyleWithInheritanceBreak = getLLVMStyle(); 1274 StyleWithInheritanceBreak.BreakBeforeInheritanceComma = true; 1275 1276 verifyFormat("class MyClass : public X {};", StyleWithInheritanceBreak); 1277 verifyFormat("class MyClass\n" 1278 " : public X\n" 1279 " , public Y {};", 1280 StyleWithInheritanceBreak); 1281 } 1282 1283 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) { 1284 verifyFormat("class A {\n} a, b;"); 1285 verifyFormat("struct A {\n} a, b;"); 1286 verifyFormat("union A {\n} a;"); 1287 } 1288 1289 TEST_F(FormatTest, FormatsEnum) { 1290 verifyFormat("enum {\n" 1291 " Zero,\n" 1292 " One = 1,\n" 1293 " Two = One + 1,\n" 1294 " Three = (One + Two),\n" 1295 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1296 " Five = (One, Two, Three, Four, 5)\n" 1297 "};"); 1298 verifyGoogleFormat("enum {\n" 1299 " Zero,\n" 1300 " One = 1,\n" 1301 " Two = One + 1,\n" 1302 " Three = (One + Two),\n" 1303 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1304 " Five = (One, Two, Three, Four, 5)\n" 1305 "};"); 1306 verifyFormat("enum Enum {};"); 1307 verifyFormat("enum {};"); 1308 verifyFormat("enum X E {} d;"); 1309 verifyFormat("enum __attribute__((...)) E {} d;"); 1310 verifyFormat("enum __declspec__((...)) E {} d;"); 1311 verifyFormat("enum {\n" 1312 " Bar = Foo<int, int>::value\n" 1313 "};", 1314 getLLVMStyleWithColumns(30)); 1315 1316 verifyFormat("enum ShortEnum { A, B, C };"); 1317 verifyGoogleFormat("enum ShortEnum { A, B, C };"); 1318 1319 EXPECT_EQ("enum KeepEmptyLines {\n" 1320 " ONE,\n" 1321 "\n" 1322 " TWO,\n" 1323 "\n" 1324 " THREE\n" 1325 "}", 1326 format("enum KeepEmptyLines {\n" 1327 " ONE,\n" 1328 "\n" 1329 " TWO,\n" 1330 "\n" 1331 "\n" 1332 " THREE\n" 1333 "}")); 1334 verifyFormat("enum E { // comment\n" 1335 " ONE,\n" 1336 " TWO\n" 1337 "};\n" 1338 "int i;"); 1339 // Not enums. 1340 verifyFormat("enum X f() {\n" 1341 " a();\n" 1342 " return 42;\n" 1343 "}"); 1344 verifyFormat("enum X Type::f() {\n" 1345 " a();\n" 1346 " return 42;\n" 1347 "}"); 1348 verifyFormat("enum ::X f() {\n" 1349 " a();\n" 1350 " return 42;\n" 1351 "}"); 1352 verifyFormat("enum ns::X f() {\n" 1353 " a();\n" 1354 " return 42;\n" 1355 "}"); 1356 } 1357 1358 TEST_F(FormatTest, FormatsEnumsWithErrors) { 1359 verifyFormat("enum Type {\n" 1360 " One = 0; // These semicolons should be commas.\n" 1361 " Two = 1;\n" 1362 "};"); 1363 verifyFormat("namespace n {\n" 1364 "enum Type {\n" 1365 " One,\n" 1366 " Two, // missing };\n" 1367 " int i;\n" 1368 "}\n" 1369 "void g() {}"); 1370 } 1371 1372 TEST_F(FormatTest, FormatsEnumStruct) { 1373 verifyFormat("enum struct {\n" 1374 " Zero,\n" 1375 " One = 1,\n" 1376 " Two = One + 1,\n" 1377 " Three = (One + Two),\n" 1378 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1379 " Five = (One, Two, Three, Four, 5)\n" 1380 "};"); 1381 verifyFormat("enum struct Enum {};"); 1382 verifyFormat("enum struct {};"); 1383 verifyFormat("enum struct X E {} d;"); 1384 verifyFormat("enum struct __attribute__((...)) E {} d;"); 1385 verifyFormat("enum struct __declspec__((...)) E {} d;"); 1386 verifyFormat("enum struct X f() {\n a();\n return 42;\n}"); 1387 } 1388 1389 TEST_F(FormatTest, FormatsEnumClass) { 1390 verifyFormat("enum class {\n" 1391 " Zero,\n" 1392 " One = 1,\n" 1393 " Two = One + 1,\n" 1394 " Three = (One + Two),\n" 1395 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1396 " Five = (One, Two, Three, Four, 5)\n" 1397 "};"); 1398 verifyFormat("enum class Enum {};"); 1399 verifyFormat("enum class {};"); 1400 verifyFormat("enum class X E {} d;"); 1401 verifyFormat("enum class __attribute__((...)) E {} d;"); 1402 verifyFormat("enum class __declspec__((...)) E {} d;"); 1403 verifyFormat("enum class X f() {\n a();\n return 42;\n}"); 1404 } 1405 1406 TEST_F(FormatTest, FormatsEnumTypes) { 1407 verifyFormat("enum X : int {\n" 1408 " A, // Force multiple lines.\n" 1409 " B\n" 1410 "};"); 1411 verifyFormat("enum X : int { A, B };"); 1412 verifyFormat("enum X : std::uint32_t { A, B };"); 1413 } 1414 1415 TEST_F(FormatTest, FormatsTypedefEnum) { 1416 FormatStyle Style = getLLVMStyle(); 1417 Style.ColumnLimit = 40; 1418 verifyFormat("typedef enum {} EmptyEnum;"); 1419 verifyFormat("typedef enum { A, B, C } ShortEnum;"); 1420 verifyFormat("typedef enum {\n" 1421 " ZERO = 0,\n" 1422 " ONE = 1,\n" 1423 " TWO = 2,\n" 1424 " THREE = 3\n" 1425 "} LongEnum;", 1426 Style); 1427 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 1428 Style.BraceWrapping.AfterEnum = true; 1429 verifyFormat("typedef enum {} EmptyEnum;"); 1430 verifyFormat("typedef enum { A, B, C } ShortEnum;"); 1431 verifyFormat("typedef enum\n" 1432 "{\n" 1433 " ZERO = 0,\n" 1434 " ONE = 1,\n" 1435 " TWO = 2,\n" 1436 " THREE = 3\n" 1437 "} LongEnum;", 1438 Style); 1439 } 1440 1441 TEST_F(FormatTest, FormatsNSEnums) { 1442 verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }"); 1443 verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n" 1444 " // Information about someDecentlyLongValue.\n" 1445 " someDecentlyLongValue,\n" 1446 " // Information about anotherDecentlyLongValue.\n" 1447 " anotherDecentlyLongValue,\n" 1448 " // Information about aThirdDecentlyLongValue.\n" 1449 " aThirdDecentlyLongValue\n" 1450 "};"); 1451 verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n" 1452 " a = 1,\n" 1453 " b = 2,\n" 1454 " c = 3,\n" 1455 "};"); 1456 verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n" 1457 " a = 1,\n" 1458 " b = 2,\n" 1459 " c = 3,\n" 1460 "};"); 1461 verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n" 1462 " a = 1,\n" 1463 " b = 2,\n" 1464 " c = 3,\n" 1465 "};"); 1466 } 1467 1468 TEST_F(FormatTest, FormatsBitfields) { 1469 verifyFormat("struct Bitfields {\n" 1470 " unsigned sClass : 8;\n" 1471 " unsigned ValueKind : 2;\n" 1472 "};"); 1473 verifyFormat("struct A {\n" 1474 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n" 1475 " bbbbbbbbbbbbbbbbbbbbbbbbb;\n" 1476 "};"); 1477 verifyFormat("struct MyStruct {\n" 1478 " uchar data;\n" 1479 " uchar : 8;\n" 1480 " uchar : 8;\n" 1481 " uchar other;\n" 1482 "};"); 1483 } 1484 1485 TEST_F(FormatTest, FormatsNamespaces) { 1486 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 1487 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 1488 1489 verifyFormat("namespace some_namespace {\n" 1490 "class A {};\n" 1491 "void f() { f(); }\n" 1492 "}", 1493 LLVMWithNoNamespaceFix); 1494 verifyFormat("namespace {\n" 1495 "class A {};\n" 1496 "void f() { f(); }\n" 1497 "}", 1498 LLVMWithNoNamespaceFix); 1499 verifyFormat("inline namespace X {\n" 1500 "class A {};\n" 1501 "void f() { f(); }\n" 1502 "}", 1503 LLVMWithNoNamespaceFix); 1504 verifyFormat("using namespace some_namespace;\n" 1505 "class A {};\n" 1506 "void f() { f(); }", 1507 LLVMWithNoNamespaceFix); 1508 1509 // This code is more common than we thought; if we 1510 // layout this correctly the semicolon will go into 1511 // its own line, which is undesirable. 1512 verifyFormat("namespace {};", 1513 LLVMWithNoNamespaceFix); 1514 verifyFormat("namespace {\n" 1515 "class A {};\n" 1516 "};", 1517 LLVMWithNoNamespaceFix); 1518 1519 verifyFormat("namespace {\n" 1520 "int SomeVariable = 0; // comment\n" 1521 "} // namespace", 1522 LLVMWithNoNamespaceFix); 1523 EXPECT_EQ("#ifndef HEADER_GUARD\n" 1524 "#define HEADER_GUARD\n" 1525 "namespace my_namespace {\n" 1526 "int i;\n" 1527 "} // my_namespace\n" 1528 "#endif // HEADER_GUARD", 1529 format("#ifndef HEADER_GUARD\n" 1530 " #define HEADER_GUARD\n" 1531 " namespace my_namespace {\n" 1532 "int i;\n" 1533 "} // my_namespace\n" 1534 "#endif // HEADER_GUARD", 1535 LLVMWithNoNamespaceFix)); 1536 1537 EXPECT_EQ("namespace A::B {\n" 1538 "class C {};\n" 1539 "}", 1540 format("namespace A::B {\n" 1541 "class C {};\n" 1542 "}", 1543 LLVMWithNoNamespaceFix)); 1544 1545 FormatStyle Style = getLLVMStyle(); 1546 Style.NamespaceIndentation = FormatStyle::NI_All; 1547 EXPECT_EQ("namespace out {\n" 1548 " int i;\n" 1549 " namespace in {\n" 1550 " int i;\n" 1551 " } // namespace in\n" 1552 "} // namespace out", 1553 format("namespace out {\n" 1554 "int i;\n" 1555 "namespace in {\n" 1556 "int i;\n" 1557 "} // namespace in\n" 1558 "} // namespace out", 1559 Style)); 1560 1561 Style.NamespaceIndentation = FormatStyle::NI_Inner; 1562 EXPECT_EQ("namespace out {\n" 1563 "int i;\n" 1564 "namespace in {\n" 1565 " int i;\n" 1566 "} // namespace in\n" 1567 "} // namespace out", 1568 format("namespace out {\n" 1569 "int i;\n" 1570 "namespace in {\n" 1571 "int i;\n" 1572 "} // namespace in\n" 1573 "} // namespace out", 1574 Style)); 1575 } 1576 1577 TEST_F(FormatTest, FormatsCompactNamespaces) { 1578 FormatStyle Style = getLLVMStyle(); 1579 Style.CompactNamespaces = true; 1580 1581 verifyFormat("namespace A { namespace B {\n" 1582 "}} // namespace A::B", 1583 Style); 1584 1585 EXPECT_EQ("namespace out { namespace in {\n" 1586 "}} // namespace out::in", 1587 format("namespace out {\n" 1588 "namespace in {\n" 1589 "} // namespace in\n" 1590 "} // namespace out", 1591 Style)); 1592 1593 // Only namespaces which have both consecutive opening and end get compacted 1594 EXPECT_EQ("namespace out {\n" 1595 "namespace in1 {\n" 1596 "} // namespace in1\n" 1597 "namespace in2 {\n" 1598 "} // namespace in2\n" 1599 "} // namespace out", 1600 format("namespace out {\n" 1601 "namespace in1 {\n" 1602 "} // namespace in1\n" 1603 "namespace in2 {\n" 1604 "} // namespace in2\n" 1605 "} // namespace out", 1606 Style)); 1607 1608 EXPECT_EQ("namespace out {\n" 1609 "int i;\n" 1610 "namespace in {\n" 1611 "int j;\n" 1612 "} // namespace in\n" 1613 "int k;\n" 1614 "} // namespace out", 1615 format("namespace out { int i;\n" 1616 "namespace in { int j; } // namespace in\n" 1617 "int k; } // namespace out", 1618 Style)); 1619 1620 EXPECT_EQ("namespace A { namespace B { namespace C {\n" 1621 "}}} // namespace A::B::C\n", 1622 format("namespace A { namespace B {\n" 1623 "namespace C {\n" 1624 "}} // namespace B::C\n" 1625 "} // namespace A\n", 1626 Style)); 1627 1628 Style.ColumnLimit = 40; 1629 EXPECT_EQ("namespace aaaaaaaaaa {\n" 1630 "namespace bbbbbbbbbb {\n" 1631 "}} // namespace aaaaaaaaaa::bbbbbbbbbb", 1632 format("namespace aaaaaaaaaa {\n" 1633 "namespace bbbbbbbbbb {\n" 1634 "} // namespace bbbbbbbbbb\n" 1635 "} // namespace aaaaaaaaaa", 1636 Style)); 1637 1638 EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n" 1639 "namespace cccccc {\n" 1640 "}}} // namespace aaaaaa::bbbbbb::cccccc", 1641 format("namespace aaaaaa {\n" 1642 "namespace bbbbbb {\n" 1643 "namespace cccccc {\n" 1644 "} // namespace cccccc\n" 1645 "} // namespace bbbbbb\n" 1646 "} // namespace aaaaaa", 1647 Style)); 1648 Style.ColumnLimit = 80; 1649 1650 // Extra semicolon after 'inner' closing brace prevents merging 1651 EXPECT_EQ("namespace out { namespace in {\n" 1652 "}; } // namespace out::in", 1653 format("namespace out {\n" 1654 "namespace in {\n" 1655 "}; // namespace in\n" 1656 "} // namespace out", 1657 Style)); 1658 1659 // Extra semicolon after 'outer' closing brace is conserved 1660 EXPECT_EQ("namespace out { namespace in {\n" 1661 "}}; // namespace out::in", 1662 format("namespace out {\n" 1663 "namespace in {\n" 1664 "} // namespace in\n" 1665 "}; // namespace out", 1666 Style)); 1667 1668 Style.NamespaceIndentation = FormatStyle::NI_All; 1669 EXPECT_EQ("namespace out { namespace in {\n" 1670 " int i;\n" 1671 "}} // namespace out::in", 1672 format("namespace out {\n" 1673 "namespace in {\n" 1674 "int i;\n" 1675 "} // namespace in\n" 1676 "} // namespace out", 1677 Style)); 1678 EXPECT_EQ("namespace out { namespace mid {\n" 1679 " namespace in {\n" 1680 " int j;\n" 1681 " } // namespace in\n" 1682 " int k;\n" 1683 "}} // namespace out::mid", 1684 format("namespace out { namespace mid {\n" 1685 "namespace in { int j; } // namespace in\n" 1686 "int k; }} // namespace out::mid", 1687 Style)); 1688 1689 Style.NamespaceIndentation = FormatStyle::NI_Inner; 1690 EXPECT_EQ("namespace out { namespace in {\n" 1691 " int i;\n" 1692 "}} // namespace out::in", 1693 format("namespace out {\n" 1694 "namespace in {\n" 1695 "int i;\n" 1696 "} // namespace in\n" 1697 "} // namespace out", 1698 Style)); 1699 EXPECT_EQ("namespace out { namespace mid { namespace in {\n" 1700 " int i;\n" 1701 "}}} // namespace out::mid::in", 1702 format("namespace out {\n" 1703 "namespace mid {\n" 1704 "namespace in {\n" 1705 "int i;\n" 1706 "} // namespace in\n" 1707 "} // namespace mid\n" 1708 "} // namespace out", 1709 Style)); 1710 } 1711 1712 TEST_F(FormatTest, FormatsExternC) { 1713 verifyFormat("extern \"C\" {\nint a;"); 1714 verifyFormat("extern \"C\" {}"); 1715 verifyFormat("extern \"C\" {\n" 1716 "int foo();\n" 1717 "}"); 1718 verifyFormat("extern \"C\" int foo() {}"); 1719 verifyFormat("extern \"C\" int foo();"); 1720 verifyFormat("extern \"C\" int foo() {\n" 1721 " int i = 42;\n" 1722 " return i;\n" 1723 "}"); 1724 1725 FormatStyle Style = getLLVMStyle(); 1726 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 1727 Style.BraceWrapping.AfterFunction = true; 1728 verifyFormat("extern \"C\" int foo() {}", Style); 1729 verifyFormat("extern \"C\" int foo();", Style); 1730 verifyFormat("extern \"C\" int foo()\n" 1731 "{\n" 1732 " int i = 42;\n" 1733 " return i;\n" 1734 "}", 1735 Style); 1736 1737 Style.BraceWrapping.AfterExternBlock = true; 1738 Style.BraceWrapping.SplitEmptyRecord = false; 1739 verifyFormat("extern \"C\"\n" 1740 "{}", 1741 Style); 1742 verifyFormat("extern \"C\"\n" 1743 "{\n" 1744 " int foo();\n" 1745 "}", 1746 Style); 1747 } 1748 1749 TEST_F(FormatTest, FormatsInlineASM) { 1750 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"); 1751 verifyFormat("asm(\"nop\" ::: \"memory\");"); 1752 verifyFormat( 1753 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" 1754 " \"cpuid\\n\\t\"\n" 1755 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n" 1756 " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n" 1757 " : \"a\"(value));"); 1758 EXPECT_EQ( 1759 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 1760 " __asm {\n" 1761 " mov edx,[that] // vtable in edx\n" 1762 " mov eax,methodIndex\n" 1763 " call [edx][eax*4] // stdcall\n" 1764 " }\n" 1765 "}", 1766 format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 1767 " __asm {\n" 1768 " mov edx,[that] // vtable in edx\n" 1769 " mov eax,methodIndex\n" 1770 " call [edx][eax*4] // stdcall\n" 1771 " }\n" 1772 "}")); 1773 EXPECT_EQ("_asm {\n" 1774 " xor eax, eax;\n" 1775 " cpuid;\n" 1776 "}", 1777 format("_asm {\n" 1778 " xor eax, eax;\n" 1779 " cpuid;\n" 1780 "}")); 1781 verifyFormat("void function() {\n" 1782 " // comment\n" 1783 " asm(\"\");\n" 1784 "}"); 1785 EXPECT_EQ("__asm {\n" 1786 "}\n" 1787 "int i;", 1788 format("__asm {\n" 1789 "}\n" 1790 "int i;")); 1791 } 1792 1793 TEST_F(FormatTest, FormatTryCatch) { 1794 verifyFormat("try {\n" 1795 " throw a * b;\n" 1796 "} catch (int a) {\n" 1797 " // Do nothing.\n" 1798 "} catch (...) {\n" 1799 " exit(42);\n" 1800 "}"); 1801 1802 // Function-level try statements. 1803 verifyFormat("int f() try { return 4; } catch (...) {\n" 1804 " return 5;\n" 1805 "}"); 1806 verifyFormat("class A {\n" 1807 " int a;\n" 1808 " A() try : a(0) {\n" 1809 " } catch (...) {\n" 1810 " throw;\n" 1811 " }\n" 1812 "};\n"); 1813 1814 // Incomplete try-catch blocks. 1815 verifyIncompleteFormat("try {} catch ("); 1816 } 1817 1818 TEST_F(FormatTest, FormatSEHTryCatch) { 1819 verifyFormat("__try {\n" 1820 " int a = b * c;\n" 1821 "} __except (EXCEPTION_EXECUTE_HANDLER) {\n" 1822 " // Do nothing.\n" 1823 "}"); 1824 1825 verifyFormat("__try {\n" 1826 " int a = b * c;\n" 1827 "} __finally {\n" 1828 " // Do nothing.\n" 1829 "}"); 1830 1831 verifyFormat("DEBUG({\n" 1832 " __try {\n" 1833 " } __finally {\n" 1834 " }\n" 1835 "});\n"); 1836 } 1837 1838 TEST_F(FormatTest, IncompleteTryCatchBlocks) { 1839 verifyFormat("try {\n" 1840 " f();\n" 1841 "} catch {\n" 1842 " g();\n" 1843 "}"); 1844 verifyFormat("try {\n" 1845 " f();\n" 1846 "} catch (A a) MACRO(x) {\n" 1847 " g();\n" 1848 "} catch (B b) MACRO(x) {\n" 1849 " g();\n" 1850 "}"); 1851 } 1852 1853 TEST_F(FormatTest, FormatTryCatchBraceStyles) { 1854 FormatStyle Style = getLLVMStyle(); 1855 for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla, 1856 FormatStyle::BS_WebKit}) { 1857 Style.BreakBeforeBraces = BraceStyle; 1858 verifyFormat("try {\n" 1859 " // something\n" 1860 "} catch (...) {\n" 1861 " // something\n" 1862 "}", 1863 Style); 1864 } 1865 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 1866 verifyFormat("try {\n" 1867 " // something\n" 1868 "}\n" 1869 "catch (...) {\n" 1870 " // something\n" 1871 "}", 1872 Style); 1873 verifyFormat("__try {\n" 1874 " // something\n" 1875 "}\n" 1876 "__finally {\n" 1877 " // something\n" 1878 "}", 1879 Style); 1880 verifyFormat("@try {\n" 1881 " // something\n" 1882 "}\n" 1883 "@finally {\n" 1884 " // something\n" 1885 "}", 1886 Style); 1887 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 1888 verifyFormat("try\n" 1889 "{\n" 1890 " // something\n" 1891 "}\n" 1892 "catch (...)\n" 1893 "{\n" 1894 " // something\n" 1895 "}", 1896 Style); 1897 Style.BreakBeforeBraces = FormatStyle::BS_GNU; 1898 verifyFormat("try\n" 1899 " {\n" 1900 " // something\n" 1901 " }\n" 1902 "catch (...)\n" 1903 " {\n" 1904 " // something\n" 1905 " }", 1906 Style); 1907 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 1908 Style.BraceWrapping.BeforeCatch = true; 1909 verifyFormat("try {\n" 1910 " // something\n" 1911 "}\n" 1912 "catch (...) {\n" 1913 " // something\n" 1914 "}", 1915 Style); 1916 } 1917 1918 TEST_F(FormatTest, StaticInitializers) { 1919 verifyFormat("static SomeClass SC = {1, 'a'};"); 1920 1921 verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n" 1922 " 100000000, " 1923 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};"); 1924 1925 // Here, everything other than the "}" would fit on a line. 1926 verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n" 1927 " 10000000000000000000000000};"); 1928 EXPECT_EQ("S s = {a,\n" 1929 "\n" 1930 " b};", 1931 format("S s = {\n" 1932 " a,\n" 1933 "\n" 1934 " b\n" 1935 "};")); 1936 1937 // FIXME: This would fit into the column limit if we'd fit "{ {" on the first 1938 // line. However, the formatting looks a bit off and this probably doesn't 1939 // happen often in practice. 1940 verifyFormat("static int Variable[1] = {\n" 1941 " {1000000000000000000000000000000000000}};", 1942 getLLVMStyleWithColumns(40)); 1943 } 1944 1945 TEST_F(FormatTest, DesignatedInitializers) { 1946 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 1947 verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n" 1948 " .bbbbbbbbbb = 2,\n" 1949 " .cccccccccc = 3,\n" 1950 " .dddddddddd = 4,\n" 1951 " .eeeeeeeeee = 5};"); 1952 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 1953 " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n" 1954 " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n" 1955 " .ccccccccccccccccccccccccccc = 3,\n" 1956 " .ddddddddddddddddddddddddddd = 4,\n" 1957 " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};"); 1958 1959 verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};"); 1960 1961 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 1962 verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n" 1963 " [2] = bbbbbbbbbb,\n" 1964 " [3] = cccccccccc,\n" 1965 " [4] = dddddddddd,\n" 1966 " [5] = eeeeeeeeee};"); 1967 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 1968 " [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1969 " [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 1970 " [3] = cccccccccccccccccccccccccccccccccccccc,\n" 1971 " [4] = dddddddddddddddddddddddddddddddddddddd,\n" 1972 " [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};"); 1973 } 1974 1975 TEST_F(FormatTest, NestedStaticInitializers) { 1976 verifyFormat("static A x = {{{}}};\n"); 1977 verifyFormat("static A x = {{{init1, init2, init3, init4},\n" 1978 " {init1, init2, init3, init4}}};", 1979 getLLVMStyleWithColumns(50)); 1980 1981 verifyFormat("somes Status::global_reps[3] = {\n" 1982 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 1983 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 1984 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};", 1985 getLLVMStyleWithColumns(60)); 1986 verifyGoogleFormat("SomeType Status::global_reps[3] = {\n" 1987 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 1988 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 1989 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};"); 1990 verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n" 1991 " {rect.fRight - rect.fLeft, rect.fBottom - " 1992 "rect.fTop}};"); 1993 1994 verifyFormat( 1995 "SomeArrayOfSomeType a = {\n" 1996 " {{1, 2, 3},\n" 1997 " {1, 2, 3},\n" 1998 " {111111111111111111111111111111, 222222222222222222222222222222,\n" 1999 " 333333333333333333333333333333},\n" 2000 " {1, 2, 3},\n" 2001 " {1, 2, 3}}};"); 2002 verifyFormat( 2003 "SomeArrayOfSomeType a = {\n" 2004 " {{1, 2, 3}},\n" 2005 " {{1, 2, 3}},\n" 2006 " {{111111111111111111111111111111, 222222222222222222222222222222,\n" 2007 " 333333333333333333333333333333}},\n" 2008 " {{1, 2, 3}},\n" 2009 " {{1, 2, 3}}};"); 2010 2011 verifyFormat("struct {\n" 2012 " unsigned bit;\n" 2013 " const char *const name;\n" 2014 "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n" 2015 " {kOsWin, \"Windows\"},\n" 2016 " {kOsLinux, \"Linux\"},\n" 2017 " {kOsCrOS, \"Chrome OS\"}};"); 2018 verifyFormat("struct {\n" 2019 " unsigned bit;\n" 2020 " const char *const name;\n" 2021 "} kBitsToOs[] = {\n" 2022 " {kOsMac, \"Mac\"},\n" 2023 " {kOsWin, \"Windows\"},\n" 2024 " {kOsLinux, \"Linux\"},\n" 2025 " {kOsCrOS, \"Chrome OS\"},\n" 2026 "};"); 2027 } 2028 2029 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { 2030 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 2031 " \\\n" 2032 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); 2033 } 2034 2035 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { 2036 verifyFormat("virtual void write(ELFWriter *writerrr,\n" 2037 " OwningPtr<FileOutputBuffer> &buffer) = 0;"); 2038 2039 // Do break defaulted and deleted functions. 2040 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 2041 " default;", 2042 getLLVMStyleWithColumns(40)); 2043 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 2044 " delete;", 2045 getLLVMStyleWithColumns(40)); 2046 } 2047 2048 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) { 2049 verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3", 2050 getLLVMStyleWithColumns(40)); 2051 verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 2052 getLLVMStyleWithColumns(40)); 2053 EXPECT_EQ("#define Q \\\n" 2054 " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n" 2055 " \"aaaaaaaa.cpp\"", 2056 format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 2057 getLLVMStyleWithColumns(40))); 2058 } 2059 2060 TEST_F(FormatTest, UnderstandsLinePPDirective) { 2061 EXPECT_EQ("# 123 \"A string literal\"", 2062 format(" # 123 \"A string literal\"")); 2063 } 2064 2065 TEST_F(FormatTest, LayoutUnknownPPDirective) { 2066 EXPECT_EQ("#;", format("#;")); 2067 verifyFormat("#\n;\n;\n;"); 2068 } 2069 2070 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { 2071 EXPECT_EQ("#line 42 \"test\"\n", 2072 format("# \\\n line \\\n 42 \\\n \"test\"\n")); 2073 EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n", 2074 getLLVMStyleWithColumns(12))); 2075 } 2076 2077 TEST_F(FormatTest, EndOfFileEndsPPDirective) { 2078 EXPECT_EQ("#line 42 \"test\"", 2079 format("# \\\n line \\\n 42 \\\n \"test\"")); 2080 EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B")); 2081 } 2082 2083 TEST_F(FormatTest, DoesntRemoveUnknownTokens) { 2084 verifyFormat("#define A \\x20"); 2085 verifyFormat("#define A \\ x20"); 2086 EXPECT_EQ("#define A \\ x20", format("#define A \\ x20")); 2087 verifyFormat("#define A ''"); 2088 verifyFormat("#define A ''qqq"); 2089 verifyFormat("#define A `qqq"); 2090 verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");"); 2091 EXPECT_EQ("const char *c = STRINGIFY(\n" 2092 "\\na : b);", 2093 format("const char * c = STRINGIFY(\n" 2094 "\\na : b);")); 2095 2096 verifyFormat("a\r\\"); 2097 verifyFormat("a\v\\"); 2098 verifyFormat("a\f\\"); 2099 } 2100 2101 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { 2102 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13)); 2103 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12)); 2104 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); 2105 // FIXME: We never break before the macro name. 2106 verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12)); 2107 2108 verifyFormat("#define A A\n#define A A"); 2109 verifyFormat("#define A(X) A\n#define A A"); 2110 2111 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23)); 2112 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22)); 2113 } 2114 2115 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { 2116 EXPECT_EQ("// somecomment\n" 2117 "#include \"a.h\"\n" 2118 "#define A( \\\n" 2119 " A, B)\n" 2120 "#include \"b.h\"\n" 2121 "// somecomment\n", 2122 format(" // somecomment\n" 2123 " #include \"a.h\"\n" 2124 "#define A(A,\\\n" 2125 " B)\n" 2126 " #include \"b.h\"\n" 2127 " // somecomment\n", 2128 getLLVMStyleWithColumns(13))); 2129 } 2130 2131 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); } 2132 2133 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { 2134 EXPECT_EQ("#define A \\\n" 2135 " c; \\\n" 2136 " e;\n" 2137 "f;", 2138 format("#define A c; e;\n" 2139 "f;", 2140 getLLVMStyleWithColumns(14))); 2141 } 2142 2143 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); } 2144 2145 TEST_F(FormatTest, MacroDefinitionInsideStatement) { 2146 EXPECT_EQ("int x,\n" 2147 "#define A\n" 2148 " y;", 2149 format("int x,\n#define A\ny;")); 2150 } 2151 2152 TEST_F(FormatTest, HashInMacroDefinition) { 2153 EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle())); 2154 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); 2155 verifyFormat("#define A \\\n" 2156 " { \\\n" 2157 " f(#c); \\\n" 2158 " }", 2159 getLLVMStyleWithColumns(11)); 2160 2161 verifyFormat("#define A(X) \\\n" 2162 " void function##X()", 2163 getLLVMStyleWithColumns(22)); 2164 2165 verifyFormat("#define A(a, b, c) \\\n" 2166 " void a##b##c()", 2167 getLLVMStyleWithColumns(22)); 2168 2169 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22)); 2170 } 2171 2172 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { 2173 EXPECT_EQ("#define A (x)", format("#define A (x)")); 2174 EXPECT_EQ("#define A(x)", format("#define A(x)")); 2175 } 2176 2177 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) { 2178 EXPECT_EQ("#define A b;", format("#define A \\\n" 2179 " \\\n" 2180 " b;", 2181 getLLVMStyleWithColumns(25))); 2182 EXPECT_EQ("#define A \\\n" 2183 " \\\n" 2184 " a; \\\n" 2185 " b;", 2186 format("#define A \\\n" 2187 " \\\n" 2188 " a; \\\n" 2189 " b;", 2190 getLLVMStyleWithColumns(11))); 2191 EXPECT_EQ("#define A \\\n" 2192 " a; \\\n" 2193 " \\\n" 2194 " b;", 2195 format("#define A \\\n" 2196 " a; \\\n" 2197 " \\\n" 2198 " b;", 2199 getLLVMStyleWithColumns(11))); 2200 } 2201 2202 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { 2203 verifyIncompleteFormat("#define A :"); 2204 verifyFormat("#define SOMECASES \\\n" 2205 " case 1: \\\n" 2206 " case 2\n", 2207 getLLVMStyleWithColumns(20)); 2208 verifyFormat("#define MACRO(a) \\\n" 2209 " if (a) \\\n" 2210 " f(); \\\n" 2211 " else \\\n" 2212 " g()", 2213 getLLVMStyleWithColumns(18)); 2214 verifyFormat("#define A template <typename T>"); 2215 verifyIncompleteFormat("#define STR(x) #x\n" 2216 "f(STR(this_is_a_string_literal{));"); 2217 verifyFormat("#pragma omp threadprivate( \\\n" 2218 " y)), // expected-warning", 2219 getLLVMStyleWithColumns(28)); 2220 verifyFormat("#d, = };"); 2221 verifyFormat("#if \"a"); 2222 verifyIncompleteFormat("({\n" 2223 "#define b \\\n" 2224 " } \\\n" 2225 " a\n" 2226 "a", 2227 getLLVMStyleWithColumns(15)); 2228 verifyFormat("#define A \\\n" 2229 " { \\\n" 2230 " {\n" 2231 "#define B \\\n" 2232 " } \\\n" 2233 " }", 2234 getLLVMStyleWithColumns(15)); 2235 verifyNoCrash("#if a\na(\n#else\n#endif\n{a"); 2236 verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}"); 2237 verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};"); 2238 verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}"); 2239 } 2240 2241 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) { 2242 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline. 2243 EXPECT_EQ("class A : public QObject {\n" 2244 " Q_OBJECT\n" 2245 "\n" 2246 " A() {}\n" 2247 "};", 2248 format("class A : public QObject {\n" 2249 " Q_OBJECT\n" 2250 "\n" 2251 " A() {\n}\n" 2252 "} ;")); 2253 EXPECT_EQ("MACRO\n" 2254 "/*static*/ int i;", 2255 format("MACRO\n" 2256 " /*static*/ int i;")); 2257 EXPECT_EQ("SOME_MACRO\n" 2258 "namespace {\n" 2259 "void f();\n" 2260 "} // namespace", 2261 format("SOME_MACRO\n" 2262 " namespace {\n" 2263 "void f( );\n" 2264 "} // namespace")); 2265 // Only if the identifier contains at least 5 characters. 2266 EXPECT_EQ("HTTP f();", format("HTTP\nf();")); 2267 EXPECT_EQ("MACRO\nf();", format("MACRO\nf();")); 2268 // Only if everything is upper case. 2269 EXPECT_EQ("class A : public QObject {\n" 2270 " Q_Object A() {}\n" 2271 "};", 2272 format("class A : public QObject {\n" 2273 " Q_Object\n" 2274 " A() {\n}\n" 2275 "} ;")); 2276 2277 // Only if the next line can actually start an unwrapped line. 2278 EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;", 2279 format("SOME_WEIRD_LOG_MACRO\n" 2280 "<< SomeThing;")); 2281 2282 verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), " 2283 "(n, buffers))\n", 2284 getChromiumStyle(FormatStyle::LK_Cpp)); 2285 } 2286 2287 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) { 2288 EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 2289 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 2290 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 2291 "class X {};\n" 2292 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 2293 "int *createScopDetectionPass() { return 0; }", 2294 format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 2295 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 2296 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 2297 " class X {};\n" 2298 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 2299 " int *createScopDetectionPass() { return 0; }")); 2300 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as 2301 // braces, so that inner block is indented one level more. 2302 EXPECT_EQ("int q() {\n" 2303 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 2304 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 2305 " IPC_END_MESSAGE_MAP()\n" 2306 "}", 2307 format("int q() {\n" 2308 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 2309 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 2310 " IPC_END_MESSAGE_MAP()\n" 2311 "}")); 2312 2313 // Same inside macros. 2314 EXPECT_EQ("#define LIST(L) \\\n" 2315 " L(A) \\\n" 2316 " L(B) \\\n" 2317 " L(C)", 2318 format("#define LIST(L) \\\n" 2319 " L(A) \\\n" 2320 " L(B) \\\n" 2321 " L(C)", 2322 getGoogleStyle())); 2323 2324 // These must not be recognized as macros. 2325 EXPECT_EQ("int q() {\n" 2326 " f(x);\n" 2327 " f(x) {}\n" 2328 " f(x)->g();\n" 2329 " f(x)->*g();\n" 2330 " f(x).g();\n" 2331 " f(x) = x;\n" 2332 " f(x) += x;\n" 2333 " f(x) -= x;\n" 2334 " f(x) *= x;\n" 2335 " f(x) /= x;\n" 2336 " f(x) %= x;\n" 2337 " f(x) &= x;\n" 2338 " f(x) |= x;\n" 2339 " f(x) ^= x;\n" 2340 " f(x) >>= x;\n" 2341 " f(x) <<= x;\n" 2342 " f(x)[y].z();\n" 2343 " LOG(INFO) << x;\n" 2344 " ifstream(x) >> x;\n" 2345 "}\n", 2346 format("int q() {\n" 2347 " f(x)\n;\n" 2348 " f(x)\n {}\n" 2349 " f(x)\n->g();\n" 2350 " f(x)\n->*g();\n" 2351 " f(x)\n.g();\n" 2352 " f(x)\n = x;\n" 2353 " f(x)\n += x;\n" 2354 " f(x)\n -= x;\n" 2355 " f(x)\n *= x;\n" 2356 " f(x)\n /= x;\n" 2357 " f(x)\n %= x;\n" 2358 " f(x)\n &= x;\n" 2359 " f(x)\n |= x;\n" 2360 " f(x)\n ^= x;\n" 2361 " f(x)\n >>= x;\n" 2362 " f(x)\n <<= x;\n" 2363 " f(x)\n[y].z();\n" 2364 " LOG(INFO)\n << x;\n" 2365 " ifstream(x)\n >> x;\n" 2366 "}\n")); 2367 EXPECT_EQ("int q() {\n" 2368 " F(x)\n" 2369 " if (1) {\n" 2370 " }\n" 2371 " F(x)\n" 2372 " while (1) {\n" 2373 " }\n" 2374 " F(x)\n" 2375 " G(x);\n" 2376 " F(x)\n" 2377 " try {\n" 2378 " Q();\n" 2379 " } catch (...) {\n" 2380 " }\n" 2381 "}\n", 2382 format("int q() {\n" 2383 "F(x)\n" 2384 "if (1) {}\n" 2385 "F(x)\n" 2386 "while (1) {}\n" 2387 "F(x)\n" 2388 "G(x);\n" 2389 "F(x)\n" 2390 "try { Q(); } catch (...) {}\n" 2391 "}\n")); 2392 EXPECT_EQ("class A {\n" 2393 " A() : t(0) {}\n" 2394 " A(int i) noexcept() : {}\n" 2395 " A(X x)\n" // FIXME: function-level try blocks are broken. 2396 " try : t(0) {\n" 2397 " } catch (...) {\n" 2398 " }\n" 2399 "};", 2400 format("class A {\n" 2401 " A()\n : t(0) {}\n" 2402 " A(int i)\n noexcept() : {}\n" 2403 " A(X x)\n" 2404 " try : t(0) {} catch (...) {}\n" 2405 "};")); 2406 EXPECT_EQ("class SomeClass {\n" 2407 "public:\n" 2408 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2409 "};", 2410 format("class SomeClass {\n" 2411 "public:\n" 2412 " SomeClass()\n" 2413 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2414 "};")); 2415 EXPECT_EQ("class SomeClass {\n" 2416 "public:\n" 2417 " SomeClass()\n" 2418 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2419 "};", 2420 format("class SomeClass {\n" 2421 "public:\n" 2422 " SomeClass()\n" 2423 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2424 "};", 2425 getLLVMStyleWithColumns(40))); 2426 2427 verifyFormat("MACRO(>)"); 2428 } 2429 2430 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) { 2431 verifyFormat("#define A \\\n" 2432 " f({ \\\n" 2433 " g(); \\\n" 2434 " });", 2435 getLLVMStyleWithColumns(11)); 2436 } 2437 2438 TEST_F(FormatTest, IndentPreprocessorDirectives) { 2439 FormatStyle Style = getLLVMStyle(); 2440 Style.IndentPPDirectives = FormatStyle::PPDIS_None; 2441 Style.ColumnLimit = 40; 2442 verifyFormat("#ifdef _WIN32\n" 2443 "#define A 0\n" 2444 "#ifdef VAR2\n" 2445 "#define B 1\n" 2446 "#include <someheader.h>\n" 2447 "#define MACRO \\\n" 2448 " some_very_long_func_aaaaaaaaaa();\n" 2449 "#endif\n" 2450 "#else\n" 2451 "#define A 1\n" 2452 "#endif", 2453 Style); 2454 Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash; 2455 verifyFormat("#ifdef _WIN32\n" 2456 "# define A 0\n" 2457 "# ifdef VAR2\n" 2458 "# define B 1\n" 2459 "# include <someheader.h>\n" 2460 "# define MACRO \\\n" 2461 " some_very_long_func_aaaaaaaaaa();\n" 2462 "# endif\n" 2463 "#else\n" 2464 "# define A 1\n" 2465 "#endif", 2466 Style); 2467 verifyFormat("#if A\n" 2468 "# define MACRO \\\n" 2469 " void a(int x) { \\\n" 2470 " b(); \\\n" 2471 " c(); \\\n" 2472 " d(); \\\n" 2473 " e(); \\\n" 2474 " f(); \\\n" 2475 " }\n" 2476 "#endif", 2477 Style); 2478 // Comments before include guard. 2479 verifyFormat("// file comment\n" 2480 "// file comment\n" 2481 "#ifndef HEADER_H\n" 2482 "#define HEADER_H\n" 2483 "code();\n" 2484 "#endif", 2485 Style); 2486 // Test with include guards. 2487 // EXPECT_EQ is used because verifyFormat() calls messUp() which incorrectly 2488 // merges lines. 2489 verifyFormat("#ifndef HEADER_H\n" 2490 "#define HEADER_H\n" 2491 "code();\n" 2492 "#endif", 2493 Style); 2494 // Include guards must have a #define with the same variable immediately 2495 // after #ifndef. 2496 verifyFormat("#ifndef NOT_GUARD\n" 2497 "# define FOO\n" 2498 "code();\n" 2499 "#endif", 2500 Style); 2501 2502 // Include guards must cover the entire file. 2503 verifyFormat("code();\n" 2504 "code();\n" 2505 "#ifndef NOT_GUARD\n" 2506 "# define NOT_GUARD\n" 2507 "code();\n" 2508 "#endif", 2509 Style); 2510 verifyFormat("#ifndef NOT_GUARD\n" 2511 "# define NOT_GUARD\n" 2512 "code();\n" 2513 "#endif\n" 2514 "code();", 2515 Style); 2516 // Test with trailing blank lines. 2517 verifyFormat("#ifndef HEADER_H\n" 2518 "#define HEADER_H\n" 2519 "code();\n" 2520 "#endif\n", 2521 Style); 2522 // Include guards don't have #else. 2523 verifyFormat("#ifndef NOT_GUARD\n" 2524 "# define NOT_GUARD\n" 2525 "code();\n" 2526 "#else\n" 2527 "#endif", 2528 Style); 2529 verifyFormat("#ifndef NOT_GUARD\n" 2530 "# define NOT_GUARD\n" 2531 "code();\n" 2532 "#elif FOO\n" 2533 "#endif", 2534 Style); 2535 // FIXME: This doesn't handle the case where there's code between the 2536 // #ifndef and #define but all other conditions hold. This is because when 2537 // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the 2538 // previous code line yet, so we can't detect it. 2539 EXPECT_EQ("#ifndef NOT_GUARD\n" 2540 "code();\n" 2541 "#define NOT_GUARD\n" 2542 "code();\n" 2543 "#endif", 2544 format("#ifndef NOT_GUARD\n" 2545 "code();\n" 2546 "# define NOT_GUARD\n" 2547 "code();\n" 2548 "#endif", 2549 Style)); 2550 // FIXME: This doesn't handle cases where legitimate preprocessor lines may 2551 // be outside an include guard. Examples are #pragma once and 2552 // #pragma GCC diagnostic, or anything else that does not change the meaning 2553 // of the file if it's included multiple times. 2554 EXPECT_EQ("#ifdef WIN32\n" 2555 "# pragma once\n" 2556 "#endif\n" 2557 "#ifndef HEADER_H\n" 2558 "# define HEADER_H\n" 2559 "code();\n" 2560 "#endif", 2561 format("#ifdef WIN32\n" 2562 "# pragma once\n" 2563 "#endif\n" 2564 "#ifndef HEADER_H\n" 2565 "#define HEADER_H\n" 2566 "code();\n" 2567 "#endif", 2568 Style)); 2569 // FIXME: This does not detect when there is a single non-preprocessor line 2570 // in front of an include-guard-like structure where other conditions hold 2571 // because ScopedLineState hides the line. 2572 EXPECT_EQ("code();\n" 2573 "#ifndef HEADER_H\n" 2574 "#define HEADER_H\n" 2575 "code();\n" 2576 "#endif", 2577 format("code();\n" 2578 "#ifndef HEADER_H\n" 2579 "# define HEADER_H\n" 2580 "code();\n" 2581 "#endif", 2582 Style)); 2583 // FIXME: The comment indent corrector in TokenAnnotator gets thrown off by 2584 // preprocessor indentation. 2585 EXPECT_EQ("#if 1\n" 2586 " // comment\n" 2587 "# define A 0\n" 2588 "// comment\n" 2589 "# define B 0\n" 2590 "#endif", 2591 format("#if 1\n" 2592 "// comment\n" 2593 "# define A 0\n" 2594 " // comment\n" 2595 "# define B 0\n" 2596 "#endif", 2597 Style)); 2598 // Test with tabs. 2599 Style.UseTab = FormatStyle::UT_Always; 2600 Style.IndentWidth = 8; 2601 Style.TabWidth = 8; 2602 verifyFormat("#ifdef _WIN32\n" 2603 "#\tdefine A 0\n" 2604 "#\tifdef VAR2\n" 2605 "#\t\tdefine B 1\n" 2606 "#\t\tinclude <someheader.h>\n" 2607 "#\t\tdefine MACRO \\\n" 2608 "\t\t\tsome_very_long_func_aaaaaaaaaa();\n" 2609 "#\tendif\n" 2610 "#else\n" 2611 "#\tdefine A 1\n" 2612 "#endif", 2613 Style); 2614 2615 // Regression test: Multiline-macro inside include guards. 2616 verifyFormat("#ifndef HEADER_H\n" 2617 "#define HEADER_H\n" 2618 "#define A() \\\n" 2619 " int i; \\\n" 2620 " int j;\n" 2621 "#endif // HEADER_H", 2622 getLLVMStyleWithColumns(20)); 2623 } 2624 2625 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { 2626 verifyFormat("{\n { a #c; }\n}"); 2627 } 2628 2629 TEST_F(FormatTest, FormatUnbalancedStructuralElements) { 2630 EXPECT_EQ("#define A \\\n { \\\n {\nint i;", 2631 format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); 2632 EXPECT_EQ("#define A \\\n } \\\n }\nint i;", 2633 format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); 2634 } 2635 2636 TEST_F(FormatTest, EscapedNewlines) { 2637 FormatStyle Narrow = getLLVMStyleWithColumns(11); 2638 EXPECT_EQ("#define A \\\n int i; \\\n int j;", 2639 format("#define A \\\nint i;\\\n int j;", Narrow)); 2640 EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;")); 2641 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 2642 EXPECT_EQ("/* \\ \\ \\\n */", format("\\\n/* \\ \\ \\\n */")); 2643 EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>")); 2644 2645 FormatStyle AlignLeft = getLLVMStyle(); 2646 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 2647 EXPECT_EQ("#define MACRO(x) \\\n" 2648 "private: \\\n" 2649 " int x(int a);\n", 2650 format("#define MACRO(x) \\\n" 2651 "private: \\\n" 2652 " int x(int a);\n", 2653 AlignLeft)); 2654 2655 // CRLF line endings 2656 EXPECT_EQ("#define A \\\r\n int i; \\\r\n int j;", 2657 format("#define A \\\r\nint i;\\\r\n int j;", Narrow)); 2658 EXPECT_EQ("#define A\r\n\r\nint i;", format("#define A \\\r\n\r\n int i;")); 2659 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 2660 EXPECT_EQ("/* \\ \\ \\\r\n */", format("\\\r\n/* \\ \\ \\\r\n */")); 2661 EXPECT_EQ("<a\r\n\\\\\r\n>", format("<a\r\n\\\\\r\n>")); 2662 EXPECT_EQ("#define MACRO(x) \\\r\n" 2663 "private: \\\r\n" 2664 " int x(int a);\r\n", 2665 format("#define MACRO(x) \\\r\n" 2666 "private: \\\r\n" 2667 " int x(int a);\r\n", 2668 AlignLeft)); 2669 2670 FormatStyle DontAlign = getLLVMStyle(); 2671 DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 2672 DontAlign.MaxEmptyLinesToKeep = 3; 2673 // FIXME: can't use verifyFormat here because the newline before 2674 // "public:" is not inserted the first time it's reformatted 2675 EXPECT_EQ("#define A \\\n" 2676 " class Foo { \\\n" 2677 " void bar(); \\\n" 2678 "\\\n" 2679 "\\\n" 2680 "\\\n" 2681 " public: \\\n" 2682 " void baz(); \\\n" 2683 " };", 2684 format("#define A \\\n" 2685 " class Foo { \\\n" 2686 " void bar(); \\\n" 2687 "\\\n" 2688 "\\\n" 2689 "\\\n" 2690 " public: \\\n" 2691 " void baz(); \\\n" 2692 " };", 2693 DontAlign)); 2694 } 2695 2696 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { 2697 verifyFormat("#define A \\\n" 2698 " int v( \\\n" 2699 " a); \\\n" 2700 " int i;", 2701 getLLVMStyleWithColumns(11)); 2702 } 2703 2704 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { 2705 EXPECT_EQ( 2706 "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 2707 " \\\n" 2708 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 2709 "\n" 2710 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 2711 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", 2712 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" 2713 "\\\n" 2714 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 2715 " \n" 2716 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 2717 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); 2718 } 2719 2720 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { 2721 EXPECT_EQ("int\n" 2722 "#define A\n" 2723 " a;", 2724 format("int\n#define A\na;")); 2725 verifyFormat("functionCallTo(\n" 2726 " someOtherFunction(\n" 2727 " withSomeParameters, whichInSequence,\n" 2728 " areLongerThanALine(andAnotherCall,\n" 2729 "#define A B\n" 2730 " withMoreParamters,\n" 2731 " whichStronglyInfluenceTheLayout),\n" 2732 " andMoreParameters),\n" 2733 " trailing);", 2734 getLLVMStyleWithColumns(69)); 2735 verifyFormat("Foo::Foo()\n" 2736 "#ifdef BAR\n" 2737 " : baz(0)\n" 2738 "#endif\n" 2739 "{\n" 2740 "}"); 2741 verifyFormat("void f() {\n" 2742 " if (true)\n" 2743 "#ifdef A\n" 2744 " f(42);\n" 2745 " x();\n" 2746 "#else\n" 2747 " g();\n" 2748 " x();\n" 2749 "#endif\n" 2750 "}"); 2751 verifyFormat("void f(param1, param2,\n" 2752 " param3,\n" 2753 "#ifdef A\n" 2754 " param4(param5,\n" 2755 "#ifdef A1\n" 2756 " param6,\n" 2757 "#ifdef A2\n" 2758 " param7),\n" 2759 "#else\n" 2760 " param8),\n" 2761 " param9,\n" 2762 "#endif\n" 2763 " param10,\n" 2764 "#endif\n" 2765 " param11)\n" 2766 "#else\n" 2767 " param12)\n" 2768 "#endif\n" 2769 "{\n" 2770 " x();\n" 2771 "}", 2772 getLLVMStyleWithColumns(28)); 2773 verifyFormat("#if 1\n" 2774 "int i;"); 2775 verifyFormat("#if 1\n" 2776 "#endif\n" 2777 "#if 1\n" 2778 "#else\n" 2779 "#endif\n"); 2780 verifyFormat("DEBUG({\n" 2781 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2782 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 2783 "});\n" 2784 "#if a\n" 2785 "#else\n" 2786 "#endif"); 2787 2788 verifyIncompleteFormat("void f(\n" 2789 "#if A\n" 2790 ");\n" 2791 "#else\n" 2792 "#endif"); 2793 } 2794 2795 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) { 2796 verifyFormat("#endif\n" 2797 "#if B"); 2798 } 2799 2800 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) { 2801 FormatStyle SingleLine = getLLVMStyle(); 2802 SingleLine.AllowShortIfStatementsOnASingleLine = true; 2803 verifyFormat("#if 0\n" 2804 "#elif 1\n" 2805 "#endif\n" 2806 "void foo() {\n" 2807 " if (test) foo2();\n" 2808 "}", 2809 SingleLine); 2810 } 2811 2812 TEST_F(FormatTest, LayoutBlockInsideParens) { 2813 verifyFormat("functionCall({ int i; });"); 2814 verifyFormat("functionCall({\n" 2815 " int i;\n" 2816 " int j;\n" 2817 "});"); 2818 verifyFormat("functionCall(\n" 2819 " {\n" 2820 " int i;\n" 2821 " int j;\n" 2822 " },\n" 2823 " aaaa, bbbb, cccc);"); 2824 verifyFormat("functionA(functionB({\n" 2825 " int i;\n" 2826 " int j;\n" 2827 " }),\n" 2828 " aaaa, bbbb, cccc);"); 2829 verifyFormat("functionCall(\n" 2830 " {\n" 2831 " int i;\n" 2832 " int j;\n" 2833 " },\n" 2834 " aaaa, bbbb, // comment\n" 2835 " cccc);"); 2836 verifyFormat("functionA(functionB({\n" 2837 " int i;\n" 2838 " int j;\n" 2839 " }),\n" 2840 " aaaa, bbbb, // comment\n" 2841 " cccc);"); 2842 verifyFormat("functionCall(aaaa, bbbb, { int i; });"); 2843 verifyFormat("functionCall(aaaa, bbbb, {\n" 2844 " int i;\n" 2845 " int j;\n" 2846 "});"); 2847 verifyFormat( 2848 "Aaa(\n" // FIXME: There shouldn't be a linebreak here. 2849 " {\n" 2850 " int i; // break\n" 2851 " },\n" 2852 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 2853 " ccccccccccccccccc));"); 2854 verifyFormat("DEBUG({\n" 2855 " if (a)\n" 2856 " f();\n" 2857 "});"); 2858 } 2859 2860 TEST_F(FormatTest, LayoutBlockInsideStatement) { 2861 EXPECT_EQ("SOME_MACRO { int i; }\n" 2862 "int i;", 2863 format(" SOME_MACRO {int i;} int i;")); 2864 } 2865 2866 TEST_F(FormatTest, LayoutNestedBlocks) { 2867 verifyFormat("void AddOsStrings(unsigned bitmask) {\n" 2868 " struct s {\n" 2869 " int i;\n" 2870 " };\n" 2871 " s kBitsToOs[] = {{10}};\n" 2872 " for (int i = 0; i < 10; ++i)\n" 2873 " return;\n" 2874 "}"); 2875 verifyFormat("call(parameter, {\n" 2876 " something();\n" 2877 " // Comment using all columns.\n" 2878 " somethingelse();\n" 2879 "});", 2880 getLLVMStyleWithColumns(40)); 2881 verifyFormat("DEBUG( //\n" 2882 " { f(); }, a);"); 2883 verifyFormat("DEBUG( //\n" 2884 " {\n" 2885 " f(); //\n" 2886 " },\n" 2887 " a);"); 2888 2889 EXPECT_EQ("call(parameter, {\n" 2890 " something();\n" 2891 " // Comment too\n" 2892 " // looooooooooong.\n" 2893 " somethingElse();\n" 2894 "});", 2895 format("call(parameter, {\n" 2896 " something();\n" 2897 " // Comment too looooooooooong.\n" 2898 " somethingElse();\n" 2899 "});", 2900 getLLVMStyleWithColumns(29))); 2901 EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });")); 2902 EXPECT_EQ("DEBUG({ // comment\n" 2903 " int i;\n" 2904 "});", 2905 format("DEBUG({ // comment\n" 2906 "int i;\n" 2907 "});")); 2908 EXPECT_EQ("DEBUG({\n" 2909 " int i;\n" 2910 "\n" 2911 " // comment\n" 2912 " int j;\n" 2913 "});", 2914 format("DEBUG({\n" 2915 " int i;\n" 2916 "\n" 2917 " // comment\n" 2918 " int j;\n" 2919 "});")); 2920 2921 verifyFormat("DEBUG({\n" 2922 " if (a)\n" 2923 " return;\n" 2924 "});"); 2925 verifyGoogleFormat("DEBUG({\n" 2926 " if (a) return;\n" 2927 "});"); 2928 FormatStyle Style = getGoogleStyle(); 2929 Style.ColumnLimit = 45; 2930 verifyFormat("Debug(aaaaa,\n" 2931 " {\n" 2932 " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n" 2933 " },\n" 2934 " a);", 2935 Style); 2936 2937 verifyFormat("SomeFunction({MACRO({ return output; }), b});"); 2938 2939 verifyNoCrash("^{v^{a}}"); 2940 } 2941 2942 TEST_F(FormatTest, FormatNestedBlocksInMacros) { 2943 EXPECT_EQ("#define MACRO() \\\n" 2944 " Debug(aaa, /* force line break */ \\\n" 2945 " { \\\n" 2946 " int i; \\\n" 2947 " int j; \\\n" 2948 " })", 2949 format("#define MACRO() Debug(aaa, /* force line break */ \\\n" 2950 " { int i; int j; })", 2951 getGoogleStyle())); 2952 2953 EXPECT_EQ("#define A \\\n" 2954 " [] { \\\n" 2955 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 2956 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 2957 " }", 2958 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 2959 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 2960 getGoogleStyle())); 2961 } 2962 2963 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { 2964 EXPECT_EQ("{}", format("{}")); 2965 verifyFormat("enum E {};"); 2966 verifyFormat("enum E {}"); 2967 } 2968 2969 TEST_F(FormatTest, FormatBeginBlockEndMacros) { 2970 FormatStyle Style = getLLVMStyle(); 2971 Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$"; 2972 Style.MacroBlockEnd = "^[A-Z_]+_END$"; 2973 verifyFormat("FOO_BEGIN\n" 2974 " FOO_ENTRY\n" 2975 "FOO_END", Style); 2976 verifyFormat("FOO_BEGIN\n" 2977 " NESTED_FOO_BEGIN\n" 2978 " NESTED_FOO_ENTRY\n" 2979 " NESTED_FOO_END\n" 2980 "FOO_END", Style); 2981 verifyFormat("FOO_BEGIN(Foo, Bar)\n" 2982 " int x;\n" 2983 " x = 1;\n" 2984 "FOO_END(Baz)", Style); 2985 } 2986 2987 //===----------------------------------------------------------------------===// 2988 // Line break tests. 2989 //===----------------------------------------------------------------------===// 2990 2991 TEST_F(FormatTest, PreventConfusingIndents) { 2992 verifyFormat( 2993 "void f() {\n" 2994 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" 2995 " parameter, parameter, parameter)),\n" 2996 " SecondLongCall(parameter));\n" 2997 "}"); 2998 verifyFormat( 2999 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3000 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 3001 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3002 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 3003 verifyFormat( 3004 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3005 " [aaaaaaaaaaaaaaaaaaaaaaaa\n" 3006 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 3007 " [aaaaaaaaaaaaaaaaaaaaaaaa]];"); 3008 verifyFormat( 3009 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 3010 " aaaaaaaaaaaaaaaaaaaaaaaa<\n" 3011 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n" 3012 " aaaaaaaaaaaaaaaaaaaaaaaa>;"); 3013 verifyFormat("int a = bbbb && ccc &&\n" 3014 " fffff(\n" 3015 "#define A Just forcing a new line\n" 3016 " ddd);"); 3017 } 3018 3019 TEST_F(FormatTest, LineBreakingInBinaryExpressions) { 3020 verifyFormat( 3021 "bool aaaaaaa =\n" 3022 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n" 3023 " bbbbbbbb();"); 3024 verifyFormat( 3025 "bool aaaaaaa =\n" 3026 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n" 3027 " bbbbbbbb();"); 3028 3029 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 3030 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n" 3031 " ccccccccc == ddddddddddd;"); 3032 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 3033 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n" 3034 " ccccccccc == ddddddddddd;"); 3035 verifyFormat( 3036 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 3037 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n" 3038 " ccccccccc == ddddddddddd;"); 3039 3040 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 3041 " aaaaaa) &&\n" 3042 " bbbbbb && cccccc;"); 3043 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 3044 " aaaaaa) >>\n" 3045 " bbbbbb;"); 3046 verifyFormat("aa = Whitespaces.addUntouchableComment(\n" 3047 " SourceMgr.getSpellingColumnNumber(\n" 3048 " TheLine.Last->FormatTok.Tok.getLocation()) -\n" 3049 " 1);"); 3050 3051 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3052 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n" 3053 " cccccc) {\n}"); 3054 verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3055 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n" 3056 " cccccc) {\n}"); 3057 verifyFormat("b = a &&\n" 3058 " // Comment\n" 3059 " b.c && d;"); 3060 3061 // If the LHS of a comparison is not a binary expression itself, the 3062 // additional linebreak confuses many people. 3063 verifyFormat( 3064 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3065 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n" 3066 "}"); 3067 verifyFormat( 3068 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3069 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3070 "}"); 3071 verifyFormat( 3072 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n" 3073 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3074 "}"); 3075 verifyFormat( 3076 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3077 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n" 3078 "}"); 3079 // Even explicit parentheses stress the precedence enough to make the 3080 // additional break unnecessary. 3081 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3082 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3083 "}"); 3084 // This cases is borderline, but with the indentation it is still readable. 3085 verifyFormat( 3086 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3087 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3088 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 3089 "}", 3090 getLLVMStyleWithColumns(75)); 3091 3092 // If the LHS is a binary expression, we should still use the additional break 3093 // as otherwise the formatting hides the operator precedence. 3094 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3095 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3096 " 5) {\n" 3097 "}"); 3098 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3099 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n" 3100 " 5) {\n" 3101 "}"); 3102 3103 FormatStyle OnePerLine = getLLVMStyle(); 3104 OnePerLine.BinPackParameters = false; 3105 verifyFormat( 3106 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3107 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3108 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}", 3109 OnePerLine); 3110 3111 verifyFormat("int i = someFunction(aaaaaaa, 0)\n" 3112 " .aaa(aaaaaaaaaaaaa) *\n" 3113 " aaaaaaa +\n" 3114 " aaaaaaa;", 3115 getLLVMStyleWithColumns(40)); 3116 } 3117 3118 TEST_F(FormatTest, ExpressionIndentation) { 3119 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3120 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3121 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3122 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3123 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 3124 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n" 3125 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3126 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n" 3127 " ccccccccccccccccccccccccccccccccccccccccc;"); 3128 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3129 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3130 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3131 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3132 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3133 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3134 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3135 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3136 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3137 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3138 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3139 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3140 verifyFormat("if () {\n" 3141 "} else if (aaaaa && bbbbb > // break\n" 3142 " ccccc) {\n" 3143 "}"); 3144 verifyFormat("if () {\n" 3145 "} else if (aaaaa &&\n" 3146 " bbbbb > // break\n" 3147 " ccccc &&\n" 3148 " ddddd) {\n" 3149 "}"); 3150 3151 // Presence of a trailing comment used to change indentation of b. 3152 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n" 3153 " b;\n" 3154 "return aaaaaaaaaaaaaaaaaaa +\n" 3155 " b; //", 3156 getLLVMStyleWithColumns(30)); 3157 } 3158 3159 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) { 3160 // Not sure what the best system is here. Like this, the LHS can be found 3161 // immediately above an operator (everything with the same or a higher 3162 // indent). The RHS is aligned right of the operator and so compasses 3163 // everything until something with the same indent as the operator is found. 3164 // FIXME: Is this a good system? 3165 FormatStyle Style = getLLVMStyle(); 3166 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 3167 verifyFormat( 3168 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3169 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3170 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3171 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3172 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3173 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3174 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3175 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3176 " > ccccccccccccccccccccccccccccccccccccccccc;", 3177 Style); 3178 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3179 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3180 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3181 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3182 Style); 3183 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3184 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3185 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3186 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3187 Style); 3188 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3189 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3190 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3191 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3192 Style); 3193 verifyFormat("if () {\n" 3194 "} else if (aaaaa\n" 3195 " && bbbbb // break\n" 3196 " > ccccc) {\n" 3197 "}", 3198 Style); 3199 verifyFormat("return (a)\n" 3200 " // comment\n" 3201 " + b;", 3202 Style); 3203 verifyFormat( 3204 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3205 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3206 " + cc;", 3207 Style); 3208 3209 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3210 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 3211 Style); 3212 3213 // Forced by comments. 3214 verifyFormat( 3215 "unsigned ContentSize =\n" 3216 " sizeof(int16_t) // DWARF ARange version number\n" 3217 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 3218 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 3219 " + sizeof(int8_t); // Segment Size (in bytes)"); 3220 3221 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 3222 " == boost::fusion::at_c<1>(iiii).second;", 3223 Style); 3224 3225 Style.ColumnLimit = 60; 3226 verifyFormat("zzzzzzzzzz\n" 3227 " = bbbbbbbbbbbbbbbbb\n" 3228 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 3229 Style); 3230 } 3231 3232 TEST_F(FormatTest, EnforcedOperatorWraps) { 3233 // Here we'd like to wrap after the || operators, but a comment is forcing an 3234 // earlier wrap. 3235 verifyFormat("bool x = aaaaa //\n" 3236 " || bbbbb\n" 3237 " //\n" 3238 " || cccc;"); 3239 } 3240 3241 TEST_F(FormatTest, NoOperandAlignment) { 3242 FormatStyle Style = getLLVMStyle(); 3243 Style.AlignOperands = false; 3244 verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n" 3245 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3246 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 3247 Style); 3248 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 3249 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3250 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3251 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3252 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3253 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3254 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3255 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3256 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3257 " > ccccccccccccccccccccccccccccccccccccccccc;", 3258 Style); 3259 3260 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3261 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3262 " + cc;", 3263 Style); 3264 verifyFormat("int a = aa\n" 3265 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3266 " * cccccccccccccccccccccccccccccccccccc;\n", 3267 Style); 3268 3269 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 3270 verifyFormat("return (a > b\n" 3271 " // comment1\n" 3272 " // comment2\n" 3273 " || c);", 3274 Style); 3275 } 3276 3277 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) { 3278 FormatStyle Style = getLLVMStyle(); 3279 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 3280 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 3281 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3282 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 3283 Style); 3284 } 3285 3286 TEST_F(FormatTest, AllowBinPackingInsideArguments) { 3287 FormatStyle Style = getLLVMStyle(); 3288 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 3289 Style.BinPackArguments = false; 3290 Style.ColumnLimit = 40; 3291 verifyFormat("void test() {\n" 3292 " someFunction(\n" 3293 " this + argument + is + quite\n" 3294 " + long + so + it + gets + wrapped\n" 3295 " + but + remains + bin - packed);\n" 3296 "}", 3297 Style); 3298 verifyFormat("void test() {\n" 3299 " someFunction(arg1,\n" 3300 " this + argument + is\n" 3301 " + quite + long + so\n" 3302 " + it + gets + wrapped\n" 3303 " + but + remains + bin\n" 3304 " - packed,\n" 3305 " arg3);\n" 3306 "}", 3307 Style); 3308 verifyFormat("void test() {\n" 3309 " someFunction(\n" 3310 " arg1,\n" 3311 " this + argument + has\n" 3312 " + anotherFunc(nested,\n" 3313 " calls + whose\n" 3314 " + arguments\n" 3315 " + are + also\n" 3316 " + wrapped,\n" 3317 " in + addition)\n" 3318 " + to + being + bin - packed,\n" 3319 " arg3);\n" 3320 "}", 3321 Style); 3322 3323 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 3324 verifyFormat("void test() {\n" 3325 " someFunction(\n" 3326 " arg1,\n" 3327 " this + argument + has +\n" 3328 " anotherFunc(nested,\n" 3329 " calls + whose +\n" 3330 " arguments +\n" 3331 " are + also +\n" 3332 " wrapped,\n" 3333 " in + addition) +\n" 3334 " to + being + bin - packed,\n" 3335 " arg3);\n" 3336 "}", 3337 Style); 3338 } 3339 3340 TEST_F(FormatTest, ConstructorInitializers) { 3341 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 3342 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", 3343 getLLVMStyleWithColumns(45)); 3344 verifyFormat("Constructor()\n" 3345 " : Inttializer(FitsOnTheLine) {}", 3346 getLLVMStyleWithColumns(44)); 3347 verifyFormat("Constructor()\n" 3348 " : Inttializer(FitsOnTheLine) {}", 3349 getLLVMStyleWithColumns(43)); 3350 3351 verifyFormat("template <typename T>\n" 3352 "Constructor() : Initializer(FitsOnTheLine) {}", 3353 getLLVMStyleWithColumns(45)); 3354 3355 verifyFormat( 3356 "SomeClass::Constructor()\n" 3357 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 3358 3359 verifyFormat( 3360 "SomeClass::Constructor()\n" 3361 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3362 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}"); 3363 verifyFormat( 3364 "SomeClass::Constructor()\n" 3365 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3366 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 3367 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3368 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3369 " : aaaaaaaaaa(aaaaaa) {}"); 3370 3371 verifyFormat("Constructor()\n" 3372 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3373 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3374 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3375 " aaaaaaaaaaaaaaaaaaaaaaa() {}"); 3376 3377 verifyFormat("Constructor()\n" 3378 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3379 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 3380 3381 verifyFormat("Constructor(int Parameter = 0)\n" 3382 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 3383 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}"); 3384 verifyFormat("Constructor()\n" 3385 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 3386 "}", 3387 getLLVMStyleWithColumns(60)); 3388 verifyFormat("Constructor()\n" 3389 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3390 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}"); 3391 3392 // Here a line could be saved by splitting the second initializer onto two 3393 // lines, but that is not desirable. 3394 verifyFormat("Constructor()\n" 3395 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 3396 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 3397 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 3398 3399 FormatStyle OnePerLine = getLLVMStyle(); 3400 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 3401 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false; 3402 verifyFormat("SomeClass::Constructor()\n" 3403 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3404 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3405 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3406 OnePerLine); 3407 verifyFormat("SomeClass::Constructor()\n" 3408 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 3409 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3410 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3411 OnePerLine); 3412 verifyFormat("MyClass::MyClass(int var)\n" 3413 " : some_var_(var), // 4 space indent\n" 3414 " some_other_var_(var + 1) { // lined up\n" 3415 "}", 3416 OnePerLine); 3417 verifyFormat("Constructor()\n" 3418 " : aaaaa(aaaaaa),\n" 3419 " aaaaa(aaaaaa),\n" 3420 " aaaaa(aaaaaa),\n" 3421 " aaaaa(aaaaaa),\n" 3422 " aaaaa(aaaaaa) {}", 3423 OnePerLine); 3424 verifyFormat("Constructor()\n" 3425 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 3426 " aaaaaaaaaaaaaaaaaaaaaa) {}", 3427 OnePerLine); 3428 OnePerLine.BinPackParameters = false; 3429 verifyFormat( 3430 "Constructor()\n" 3431 " : aaaaaaaaaaaaaaaaaaaaaaaa(\n" 3432 " aaaaaaaaaaa().aaa(),\n" 3433 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3434 OnePerLine); 3435 OnePerLine.ColumnLimit = 60; 3436 verifyFormat("Constructor()\n" 3437 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 3438 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 3439 OnePerLine); 3440 3441 EXPECT_EQ("Constructor()\n" 3442 " : // Comment forcing unwanted break.\n" 3443 " aaaa(aaaa) {}", 3444 format("Constructor() :\n" 3445 " // Comment forcing unwanted break.\n" 3446 " aaaa(aaaa) {}")); 3447 } 3448 3449 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) { 3450 FormatStyle Style = getLLVMStyle(); 3451 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 3452 3453 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 3454 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}", 3455 getStyleWithColumns(Style, 45)); 3456 verifyFormat("Constructor() :\n" 3457 " Initializer(FitsOnTheLine) {}", 3458 getStyleWithColumns(Style, 44)); 3459 verifyFormat("Constructor() :\n" 3460 " Initializer(FitsOnTheLine) {}", 3461 getStyleWithColumns(Style, 43)); 3462 3463 verifyFormat("template <typename T>\n" 3464 "Constructor() : Initializer(FitsOnTheLine) {}", 3465 getStyleWithColumns(Style, 50)); 3466 3467 verifyFormat( 3468 "SomeClass::Constructor() :\n" 3469 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 3470 Style); 3471 3472 verifyFormat( 3473 "SomeClass::Constructor() :\n" 3474 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3475 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3476 Style); 3477 verifyFormat( 3478 "SomeClass::Constructor() :\n" 3479 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3480 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 3481 Style); 3482 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3483 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 3484 " aaaaaaaaaa(aaaaaa) {}", 3485 Style); 3486 3487 verifyFormat("Constructor() :\n" 3488 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3489 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3490 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3491 " aaaaaaaaaaaaaaaaaaaaaaa() {}", 3492 Style); 3493 3494 verifyFormat("Constructor() :\n" 3495 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3496 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3497 Style); 3498 3499 verifyFormat("Constructor(int Parameter = 0) :\n" 3500 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 3501 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}", 3502 Style); 3503 verifyFormat("Constructor() :\n" 3504 " aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 3505 "}", 3506 getStyleWithColumns(Style, 60)); 3507 verifyFormat("Constructor() :\n" 3508 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3509 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}", 3510 Style); 3511 3512 // Here a line could be saved by splitting the second initializer onto two 3513 // lines, but that is not desirable. 3514 verifyFormat("Constructor() :\n" 3515 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 3516 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 3517 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3518 Style); 3519 3520 FormatStyle OnePerLine = Style; 3521 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 3522 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false; 3523 verifyFormat("SomeClass::Constructor() :\n" 3524 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3525 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3526 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3527 OnePerLine); 3528 verifyFormat("SomeClass::Constructor() :\n" 3529 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 3530 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3531 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3532 OnePerLine); 3533 verifyFormat("MyClass::MyClass(int var) :\n" 3534 " some_var_(var), // 4 space indent\n" 3535 " some_other_var_(var + 1) { // lined up\n" 3536 "}", 3537 OnePerLine); 3538 verifyFormat("Constructor() :\n" 3539 " aaaaa(aaaaaa),\n" 3540 " aaaaa(aaaaaa),\n" 3541 " aaaaa(aaaaaa),\n" 3542 " aaaaa(aaaaaa),\n" 3543 " aaaaa(aaaaaa) {}", 3544 OnePerLine); 3545 verifyFormat("Constructor() :\n" 3546 " aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 3547 " aaaaaaaaaaaaaaaaaaaaaa) {}", 3548 OnePerLine); 3549 OnePerLine.BinPackParameters = false; 3550 verifyFormat( 3551 "Constructor() :\n" 3552 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 3553 " aaaaaaaaaaa().aaa(),\n" 3554 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3555 OnePerLine); 3556 OnePerLine.ColumnLimit = 60; 3557 verifyFormat("Constructor() :\n" 3558 " aaaaaaaaaaaaaaaaaaaa(a),\n" 3559 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 3560 OnePerLine); 3561 3562 EXPECT_EQ("Constructor() :\n" 3563 " // Comment forcing unwanted break.\n" 3564 " aaaa(aaaa) {}", 3565 format("Constructor() :\n" 3566 " // Comment forcing unwanted break.\n" 3567 " aaaa(aaaa) {}", 3568 Style)); 3569 3570 Style.ColumnLimit = 0; 3571 verifyFormat("SomeClass::Constructor() :\n" 3572 " a(a) {}", 3573 Style); 3574 verifyFormat("SomeClass::Constructor() noexcept :\n" 3575 " a(a) {}", 3576 Style); 3577 verifyFormat("SomeClass::Constructor() :\n" 3578 " a(a), b(b), c(c) {}", 3579 Style); 3580 verifyFormat("SomeClass::Constructor() :\n" 3581 " a(a) {\n" 3582 " foo();\n" 3583 " bar();\n" 3584 "}", 3585 Style); 3586 3587 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 3588 verifyFormat("SomeClass::Constructor() :\n" 3589 " a(a), b(b), c(c) {\n" 3590 "}", 3591 Style); 3592 verifyFormat("SomeClass::Constructor() :\n" 3593 " a(a) {\n" 3594 "}", 3595 Style); 3596 3597 Style.ColumnLimit = 80; 3598 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 3599 Style.ConstructorInitializerIndentWidth = 2; 3600 verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", 3601 Style); 3602 verifyFormat("SomeClass::Constructor() :\n" 3603 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3604 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}", 3605 Style); 3606 } 3607 3608 #ifndef EXPENSIVE_CHECKS 3609 // Expensive checks enables libstdc++ checking which includes validating the 3610 // state of ranges used in std::priority_queue - this blows out the 3611 // runtime/scalability of the function and makes this test unacceptably slow. 3612 TEST_F(FormatTest, MemoizationTests) { 3613 // This breaks if the memoization lookup does not take \c Indent and 3614 // \c LastSpace into account. 3615 verifyFormat( 3616 "extern CFRunLoopTimerRef\n" 3617 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n" 3618 " CFTimeInterval interval, CFOptionFlags flags,\n" 3619 " CFIndex order, CFRunLoopTimerCallBack callout,\n" 3620 " CFRunLoopTimerContext *context) {}"); 3621 3622 // Deep nesting somewhat works around our memoization. 3623 verifyFormat( 3624 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3625 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3626 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3627 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3628 " aaaaa())))))))))))))))))))))))))))))))))))))));", 3629 getLLVMStyleWithColumns(65)); 3630 verifyFormat( 3631 "aaaaa(\n" 3632 " aaaaa,\n" 3633 " aaaaa(\n" 3634 " aaaaa,\n" 3635 " aaaaa(\n" 3636 " aaaaa,\n" 3637 " aaaaa(\n" 3638 " aaaaa,\n" 3639 " aaaaa(\n" 3640 " aaaaa,\n" 3641 " aaaaa(\n" 3642 " aaaaa,\n" 3643 " aaaaa(\n" 3644 " aaaaa,\n" 3645 " aaaaa(\n" 3646 " aaaaa,\n" 3647 " aaaaa(\n" 3648 " aaaaa,\n" 3649 " aaaaa(\n" 3650 " aaaaa,\n" 3651 " aaaaa(\n" 3652 " aaaaa,\n" 3653 " aaaaa(\n" 3654 " aaaaa,\n" 3655 " aaaaa))))))))))));", 3656 getLLVMStyleWithColumns(65)); 3657 verifyFormat( 3658 "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" 3659 " a),\n" 3660 " a),\n" 3661 " a),\n" 3662 " a),\n" 3663 " a),\n" 3664 " a),\n" 3665 " a),\n" 3666 " a),\n" 3667 " a),\n" 3668 " a),\n" 3669 " a),\n" 3670 " a),\n" 3671 " a),\n" 3672 " a),\n" 3673 " a),\n" 3674 " a),\n" 3675 " a)", 3676 getLLVMStyleWithColumns(65)); 3677 3678 // This test takes VERY long when memoization is broken. 3679 FormatStyle OnePerLine = getLLVMStyle(); 3680 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 3681 OnePerLine.BinPackParameters = false; 3682 std::string input = "Constructor()\n" 3683 " : aaaa(a,\n"; 3684 for (unsigned i = 0, e = 80; i != e; ++i) { 3685 input += " a,\n"; 3686 } 3687 input += " a) {}"; 3688 verifyFormat(input, OnePerLine); 3689 } 3690 #endif 3691 3692 TEST_F(FormatTest, BreaksAsHighAsPossible) { 3693 verifyFormat( 3694 "void f() {\n" 3695 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" 3696 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" 3697 " f();\n" 3698 "}"); 3699 verifyFormat("if (Intervals[i].getRange().getFirst() <\n" 3700 " Intervals[i - 1].getRange().getLast()) {\n}"); 3701 } 3702 3703 TEST_F(FormatTest, BreaksFunctionDeclarations) { 3704 // Principially, we break function declarations in a certain order: 3705 // 1) break amongst arguments. 3706 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n" 3707 " Cccccccccccccc cccccccccccccc);"); 3708 verifyFormat("template <class TemplateIt>\n" 3709 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n" 3710 " TemplateIt *stop) {}"); 3711 3712 // 2) break after return type. 3713 verifyFormat( 3714 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3715 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);", 3716 getGoogleStyle()); 3717 3718 // 3) break after (. 3719 verifyFormat( 3720 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n" 3721 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);", 3722 getGoogleStyle()); 3723 3724 // 4) break before after nested name specifiers. 3725 verifyFormat( 3726 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3727 "SomeClasssssssssssssssssssssssssssssssssssssss::\n" 3728 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);", 3729 getGoogleStyle()); 3730 3731 // However, there are exceptions, if a sufficient amount of lines can be 3732 // saved. 3733 // FIXME: The precise cut-offs wrt. the number of saved lines might need some 3734 // more adjusting. 3735 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 3736 " Cccccccccccccc cccccccccc,\n" 3737 " Cccccccccccccc cccccccccc,\n" 3738 " Cccccccccccccc cccccccccc,\n" 3739 " Cccccccccccccc cccccccccc);"); 3740 verifyFormat( 3741 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3742 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3743 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3744 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);", 3745 getGoogleStyle()); 3746 verifyFormat( 3747 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 3748 " Cccccccccccccc cccccccccc,\n" 3749 " Cccccccccccccc cccccccccc,\n" 3750 " Cccccccccccccc cccccccccc,\n" 3751 " Cccccccccccccc cccccccccc,\n" 3752 " Cccccccccccccc cccccccccc,\n" 3753 " Cccccccccccccc cccccccccc);"); 3754 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 3755 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3756 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3757 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3758 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); 3759 3760 // Break after multi-line parameters. 3761 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3762 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3763 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3764 " bbbb bbbb);"); 3765 verifyFormat("void SomeLoooooooooooongFunction(\n" 3766 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 3767 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3768 " int bbbbbbbbbbbbb);"); 3769 3770 // Treat overloaded operators like other functions. 3771 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3772 "operator>(const SomeLoooooooooooooooooooooooooogType &other);"); 3773 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3774 "operator>>(const SomeLooooooooooooooooooooooooogType &other);"); 3775 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3776 "operator<<(const SomeLooooooooooooooooooooooooogType &other);"); 3777 verifyGoogleFormat( 3778 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n" 3779 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 3780 verifyGoogleFormat( 3781 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n" 3782 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 3783 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3784 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 3785 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n" 3786 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 3787 verifyGoogleFormat( 3788 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n" 3789 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3790 " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}"); 3791 verifyGoogleFormat( 3792 "template <typename T>\n" 3793 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3794 "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n" 3795 " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);"); 3796 3797 FormatStyle Style = getLLVMStyle(); 3798 Style.PointerAlignment = FormatStyle::PAS_Left; 3799 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3800 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}", 3801 Style); 3802 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n" 3803 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3804 Style); 3805 } 3806 3807 TEST_F(FormatTest, TrailingReturnType) { 3808 verifyFormat("auto foo() -> int;\n"); 3809 verifyFormat("struct S {\n" 3810 " auto bar() const -> int;\n" 3811 "};"); 3812 verifyFormat("template <size_t Order, typename T>\n" 3813 "auto load_img(const std::string &filename)\n" 3814 " -> alias::tensor<Order, T, mem::tag::cpu> {}"); 3815 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n" 3816 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}"); 3817 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}"); 3818 verifyFormat("template <typename T>\n" 3819 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n" 3820 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());"); 3821 3822 // Not trailing return types. 3823 verifyFormat("void f() { auto a = b->c(); }"); 3824 } 3825 3826 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { 3827 // Avoid breaking before trailing 'const' or other trailing annotations, if 3828 // they are not function-like. 3829 FormatStyle Style = getGoogleStyle(); 3830 Style.ColumnLimit = 47; 3831 verifyFormat("void someLongFunction(\n" 3832 " int someLoooooooooooooongParameter) const {\n}", 3833 getLLVMStyleWithColumns(47)); 3834 verifyFormat("LoooooongReturnType\n" 3835 "someLoooooooongFunction() const {}", 3836 getLLVMStyleWithColumns(47)); 3837 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n" 3838 " const {}", 3839 Style); 3840 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3841 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;"); 3842 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3843 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;"); 3844 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3845 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;"); 3846 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n" 3847 " aaaaaaaaaaa aaaaa) const override;"); 3848 verifyGoogleFormat( 3849 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 3850 " const override;"); 3851 3852 // Even if the first parameter has to be wrapped. 3853 verifyFormat("void someLongFunction(\n" 3854 " int someLongParameter) const {}", 3855 getLLVMStyleWithColumns(46)); 3856 verifyFormat("void someLongFunction(\n" 3857 " int someLongParameter) const {}", 3858 Style); 3859 verifyFormat("void someLongFunction(\n" 3860 " int someLongParameter) override {}", 3861 Style); 3862 verifyFormat("void someLongFunction(\n" 3863 " int someLongParameter) OVERRIDE {}", 3864 Style); 3865 verifyFormat("void someLongFunction(\n" 3866 " int someLongParameter) final {}", 3867 Style); 3868 verifyFormat("void someLongFunction(\n" 3869 " int someLongParameter) FINAL {}", 3870 Style); 3871 verifyFormat("void someLongFunction(\n" 3872 " int parameter) const override {}", 3873 Style); 3874 3875 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 3876 verifyFormat("void someLongFunction(\n" 3877 " int someLongParameter) const\n" 3878 "{\n" 3879 "}", 3880 Style); 3881 3882 // Unless these are unknown annotations. 3883 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n" 3884 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3885 " LONG_AND_UGLY_ANNOTATION;"); 3886 3887 // Breaking before function-like trailing annotations is fine to keep them 3888 // close to their arguments. 3889 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3890 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 3891 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 3892 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 3893 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 3894 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}"); 3895 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n" 3896 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);"); 3897 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});"); 3898 3899 verifyFormat( 3900 "void aaaaaaaaaaaaaaaaaa()\n" 3901 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n" 3902 " aaaaaaaaaaaaaaaaaaaaaaaaa));"); 3903 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3904 " __attribute__((unused));"); 3905 verifyGoogleFormat( 3906 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3907 " GUARDED_BY(aaaaaaaaaaaa);"); 3908 verifyGoogleFormat( 3909 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3910 " GUARDED_BY(aaaaaaaaaaaa);"); 3911 verifyGoogleFormat( 3912 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 3913 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 3914 verifyGoogleFormat( 3915 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 3916 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 3917 } 3918 3919 TEST_F(FormatTest, FunctionAnnotations) { 3920 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 3921 "int OldFunction(const string ¶meter) {}"); 3922 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 3923 "string OldFunction(const string ¶meter) {}"); 3924 verifyFormat("template <typename T>\n" 3925 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 3926 "string OldFunction(const string ¶meter) {}"); 3927 3928 // Not function annotations. 3929 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3930 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); 3931 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n" 3932 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}"); 3933 verifyFormat("MACRO(abc).function() // wrap\n" 3934 " << abc;"); 3935 verifyFormat("MACRO(abc)->function() // wrap\n" 3936 " << abc;"); 3937 verifyFormat("MACRO(abc)::function() // wrap\n" 3938 " << abc;"); 3939 } 3940 3941 TEST_F(FormatTest, BreaksDesireably) { 3942 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 3943 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 3944 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); 3945 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3946 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 3947 "}"); 3948 3949 verifyFormat( 3950 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3951 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 3952 3953 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3954 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3955 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 3956 3957 verifyFormat( 3958 "aaaaaaaa(aaaaaaaaaaaaa,\n" 3959 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3960 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 3961 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3962 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 3963 3964 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3965 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3966 3967 verifyFormat( 3968 "void f() {\n" 3969 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 3970 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 3971 "}"); 3972 verifyFormat( 3973 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3974 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 3975 verifyFormat( 3976 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3977 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 3978 verifyFormat( 3979 "aaaaaa(aaa,\n" 3980 " new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3981 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3982 " aaaa);"); 3983 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3984 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3985 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3986 3987 // Indent consistently independent of call expression and unary operator. 3988 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 3989 " dddddddddddddddddddddddddddddd));"); 3990 verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 3991 " dddddddddddddddddddddddddddddd));"); 3992 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n" 3993 " dddddddddddddddddddddddddddddd));"); 3994 3995 // This test case breaks on an incorrect memoization, i.e. an optimization not 3996 // taking into account the StopAt value. 3997 verifyFormat( 3998 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 3999 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4000 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4001 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4002 4003 verifyFormat("{\n {\n {\n" 4004 " Annotation.SpaceRequiredBefore =\n" 4005 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" 4006 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" 4007 " }\n }\n}"); 4008 4009 // Break on an outer level if there was a break on an inner level. 4010 EXPECT_EQ("f(g(h(a, // comment\n" 4011 " b, c),\n" 4012 " d, e),\n" 4013 " x, y);", 4014 format("f(g(h(a, // comment\n" 4015 " b, c), d, e), x, y);")); 4016 4017 // Prefer breaking similar line breaks. 4018 verifyFormat( 4019 "const int kTrackingOptions = NSTrackingMouseMoved |\n" 4020 " NSTrackingMouseEnteredAndExited |\n" 4021 " NSTrackingActiveAlways;"); 4022 } 4023 4024 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) { 4025 FormatStyle NoBinPacking = getGoogleStyle(); 4026 NoBinPacking.BinPackParameters = false; 4027 NoBinPacking.BinPackArguments = true; 4028 verifyFormat("void f() {\n" 4029 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n" 4030 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 4031 "}", 4032 NoBinPacking); 4033 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n" 4034 " int aaaaaaaaaaaaaaaaaaaa,\n" 4035 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4036 NoBinPacking); 4037 4038 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 4039 verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4040 " vector<int> bbbbbbbbbbbbbbb);", 4041 NoBinPacking); 4042 // FIXME: This behavior difference is probably not wanted. However, currently 4043 // we cannot distinguish BreakBeforeParameter being set because of the wrapped 4044 // template arguments from BreakBeforeParameter being set because of the 4045 // one-per-line formatting. 4046 verifyFormat( 4047 "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n" 4048 " aaaaaaaaaa> aaaaaaaaaa);", 4049 NoBinPacking); 4050 verifyFormat( 4051 "void fffffffffff(\n" 4052 " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n" 4053 " aaaaaaaaaa);"); 4054 } 4055 4056 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { 4057 FormatStyle NoBinPacking = getGoogleStyle(); 4058 NoBinPacking.BinPackParameters = false; 4059 NoBinPacking.BinPackArguments = false; 4060 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" 4061 " aaaaaaaaaaaaaaaaaaaa,\n" 4062 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);", 4063 NoBinPacking); 4064 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n" 4065 " aaaaaaaaaaaaa,\n" 4066 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));", 4067 NoBinPacking); 4068 verifyFormat( 4069 "aaaaaaaa(aaaaaaaaaaaaa,\n" 4070 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4071 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 4072 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4073 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));", 4074 NoBinPacking); 4075 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 4076 " .aaaaaaaaaaaaaaaaaa();", 4077 NoBinPacking); 4078 verifyFormat("void f() {\n" 4079 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4080 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n" 4081 "}", 4082 NoBinPacking); 4083 4084 verifyFormat( 4085 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4086 " aaaaaaaaaaaa,\n" 4087 " aaaaaaaaaaaa);", 4088 NoBinPacking); 4089 verifyFormat( 4090 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" 4091 " ddddddddddddddddddddddddddddd),\n" 4092 " test);", 4093 NoBinPacking); 4094 4095 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" 4096 " aaaaaaaaaaaaaaaaaaaaaaa,\n" 4097 " aaaaaaaaaaaaaaaaaaaaaaa>\n" 4098 " aaaaaaaaaaaaaaaaaa;", 4099 NoBinPacking); 4100 verifyFormat("a(\"a\"\n" 4101 " \"a\",\n" 4102 " a);"); 4103 4104 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 4105 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" 4106 " aaaaaaaaa,\n" 4107 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4108 NoBinPacking); 4109 verifyFormat( 4110 "void f() {\n" 4111 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 4112 " .aaaaaaa();\n" 4113 "}", 4114 NoBinPacking); 4115 verifyFormat( 4116 "template <class SomeType, class SomeOtherType>\n" 4117 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}", 4118 NoBinPacking); 4119 } 4120 4121 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) { 4122 FormatStyle Style = getLLVMStyleWithColumns(15); 4123 Style.ExperimentalAutoDetectBinPacking = true; 4124 EXPECT_EQ("aaa(aaaa,\n" 4125 " aaaa,\n" 4126 " aaaa);\n" 4127 "aaa(aaaa,\n" 4128 " aaaa,\n" 4129 " aaaa);", 4130 format("aaa(aaaa,\n" // one-per-line 4131 " aaaa,\n" 4132 " aaaa );\n" 4133 "aaa(aaaa, aaaa, aaaa);", // inconclusive 4134 Style)); 4135 EXPECT_EQ("aaa(aaaa, aaaa,\n" 4136 " aaaa);\n" 4137 "aaa(aaaa, aaaa,\n" 4138 " aaaa);", 4139 format("aaa(aaaa, aaaa,\n" // bin-packed 4140 " aaaa );\n" 4141 "aaa(aaaa, aaaa, aaaa);", // inconclusive 4142 Style)); 4143 } 4144 4145 TEST_F(FormatTest, FormatsBuilderPattern) { 4146 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n" 4147 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" 4148 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n" 4149 " .StartsWith(\".init\", ORDER_INIT)\n" 4150 " .StartsWith(\".fini\", ORDER_FINI)\n" 4151 " .StartsWith(\".hash\", ORDER_HASH)\n" 4152 " .Default(ORDER_TEXT);\n"); 4153 4154 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" 4155 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); 4156 verifyFormat( 4157 "aaaaaaa->aaaaaaa\n" 4158 " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4159 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4160 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 4161 verifyFormat( 4162 "aaaaaaa->aaaaaaa\n" 4163 " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4164 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 4165 verifyFormat( 4166 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" 4167 " aaaaaaaaaaaaaa);"); 4168 verifyFormat( 4169 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n" 4170 " aaaaaa->aaaaaaaaaaaa()\n" 4171 " ->aaaaaaaaaaaaaaaa(\n" 4172 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4173 " ->aaaaaaaaaaaaaaaaa();"); 4174 verifyGoogleFormat( 4175 "void f() {\n" 4176 " someo->Add((new util::filetools::Handler(dir))\n" 4177 " ->OnEvent1(NewPermanentCallback(\n" 4178 " this, &HandlerHolderClass::EventHandlerCBA))\n" 4179 " ->OnEvent2(NewPermanentCallback(\n" 4180 " this, &HandlerHolderClass::EventHandlerCBB))\n" 4181 " ->OnEvent3(NewPermanentCallback(\n" 4182 " this, &HandlerHolderClass::EventHandlerCBC))\n" 4183 " ->OnEvent5(NewPermanentCallback(\n" 4184 " this, &HandlerHolderClass::EventHandlerCBD))\n" 4185 " ->OnEvent6(NewPermanentCallback(\n" 4186 " this, &HandlerHolderClass::EventHandlerCBE)));\n" 4187 "}"); 4188 4189 verifyFormat( 4190 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();"); 4191 verifyFormat("aaaaaaaaaaaaaaa()\n" 4192 " .aaaaaaaaaaaaaaa()\n" 4193 " .aaaaaaaaaaaaaaa()\n" 4194 " .aaaaaaaaaaaaaaa()\n" 4195 " .aaaaaaaaaaaaaaa();"); 4196 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4197 " .aaaaaaaaaaaaaaa()\n" 4198 " .aaaaaaaaaaaaaaa()\n" 4199 " .aaaaaaaaaaaaaaa();"); 4200 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4201 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4202 " .aaaaaaaaaaaaaaa();"); 4203 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n" 4204 " ->aaaaaaaaaaaaaae(0)\n" 4205 " ->aaaaaaaaaaaaaaa();"); 4206 4207 // Don't linewrap after very short segments. 4208 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4209 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4210 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4211 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4212 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4213 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4214 verifyFormat("aaa()\n" 4215 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4216 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4217 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4218 4219 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 4220 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4221 " .has<bbbbbbbbbbbbbbbbbbbbb>();"); 4222 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 4223 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 4224 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();"); 4225 4226 // Prefer not to break after empty parentheses. 4227 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n" 4228 " First->LastNewlineOffset);"); 4229 4230 // Prefer not to create "hanging" indents. 4231 verifyFormat( 4232 "return !soooooooooooooome_map\n" 4233 " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4234 " .second;"); 4235 verifyFormat( 4236 "return aaaaaaaaaaaaaaaa\n" 4237 " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n" 4238 " .aaaa(aaaaaaaaaaaaaa);"); 4239 // No hanging indent here. 4240 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n" 4241 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4242 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n" 4243 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4244 verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 4245 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4246 getLLVMStyleWithColumns(60)); 4247 verifyFormat("aaaaaaaaaaaaaaaaaa\n" 4248 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 4249 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4250 getLLVMStyleWithColumns(59)); 4251 verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4252 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4253 " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4254 } 4255 4256 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { 4257 verifyFormat( 4258 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4259 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); 4260 verifyFormat( 4261 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n" 4262 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}"); 4263 4264 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 4265 " ccccccccccccccccccccccccc) {\n}"); 4266 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n" 4267 " ccccccccccccccccccccccccc) {\n}"); 4268 4269 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 4270 " ccccccccccccccccccccccccc) {\n}"); 4271 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n" 4272 " ccccccccccccccccccccccccc) {\n}"); 4273 4274 verifyFormat( 4275 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" 4276 " ccccccccccccccccccccccccc) {\n}"); 4277 verifyFormat( 4278 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n" 4279 " ccccccccccccccccccccccccc) {\n}"); 4280 4281 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" 4282 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" 4283 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" 4284 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 4285 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n" 4286 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n" 4287 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n" 4288 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 4289 4290 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" 4291 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" 4292 " aaaaaaaaaaaaaaa != aa) {\n}"); 4293 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n" 4294 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n" 4295 " aaaaaaaaaaaaaaa != aa) {\n}"); 4296 } 4297 4298 TEST_F(FormatTest, BreaksAfterAssignments) { 4299 verifyFormat( 4300 "unsigned Cost =\n" 4301 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" 4302 " SI->getPointerAddressSpaceee());\n"); 4303 verifyFormat( 4304 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" 4305 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); 4306 4307 verifyFormat( 4308 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n" 4309 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); 4310 verifyFormat("unsigned OriginalStartColumn =\n" 4311 " SourceMgr.getSpellingColumnNumber(\n" 4312 " Current.FormatTok.getStartOfNonWhitespace()) -\n" 4313 " 1;"); 4314 } 4315 4316 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) { 4317 FormatStyle Style = getLLVMStyle(); 4318 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 4319 " bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;", 4320 Style); 4321 4322 Style.PenaltyBreakAssignment = 20; 4323 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 4324 " cccccccccccccccccccccccccc;", 4325 Style); 4326 } 4327 4328 TEST_F(FormatTest, AlignsAfterAssignments) { 4329 verifyFormat( 4330 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4331 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4332 verifyFormat( 4333 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4334 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4335 verifyFormat( 4336 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4337 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4338 verifyFormat( 4339 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4340 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 4341 verifyFormat( 4342 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" 4343 " aaaaaaaaaaaaaaaaaaaaaaaa +\n" 4344 " aaaaaaaaaaaaaaaaaaaaaaaa;"); 4345 } 4346 4347 TEST_F(FormatTest, AlignsAfterReturn) { 4348 verifyFormat( 4349 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4350 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4351 verifyFormat( 4352 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4353 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 4354 verifyFormat( 4355 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 4356 " aaaaaaaaaaaaaaaaaaaaaa();"); 4357 verifyFormat( 4358 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 4359 " aaaaaaaaaaaaaaaaaaaaaa());"); 4360 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4361 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4362 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4363 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n" 4364 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4365 verifyFormat("return\n" 4366 " // true if code is one of a or b.\n" 4367 " code == a || code == b;"); 4368 } 4369 4370 TEST_F(FormatTest, AlignsAfterOpenBracket) { 4371 verifyFormat( 4372 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 4373 " aaaaaaaaa aaaaaaa) {}"); 4374 verifyFormat( 4375 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 4376 " aaaaaaaaaaa aaaaaaaaa);"); 4377 verifyFormat( 4378 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 4379 " aaaaaaaaaaaaaaaaaaaaa));"); 4380 FormatStyle Style = getLLVMStyle(); 4381 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4382 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4383 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}", 4384 Style); 4385 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 4386 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);", 4387 Style); 4388 verifyFormat("SomeLongVariableName->someFunction(\n" 4389 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));", 4390 Style); 4391 verifyFormat( 4392 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 4393 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4394 Style); 4395 verifyFormat( 4396 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 4397 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4398 Style); 4399 verifyFormat( 4400 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 4401 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 4402 Style); 4403 4404 verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n" 4405 " ccccccc(aaaaaaaaaaaaaaaaa, //\n" 4406 " b));", 4407 Style); 4408 4409 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 4410 Style.BinPackArguments = false; 4411 Style.BinPackParameters = false; 4412 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4413 " aaaaaaaaaaa aaaaaaaa,\n" 4414 " aaaaaaaaa aaaaaaa,\n" 4415 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4416 Style); 4417 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 4418 " aaaaaaaaaaa aaaaaaaaa,\n" 4419 " aaaaaaaaaaa aaaaaaaaa,\n" 4420 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4421 Style); 4422 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n" 4423 " aaaaaaaaaaaaaaa,\n" 4424 " aaaaaaaaaaaaaaaaaaaaa,\n" 4425 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 4426 Style); 4427 verifyFormat( 4428 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n" 4429 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 4430 Style); 4431 verifyFormat( 4432 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n" 4433 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 4434 Style); 4435 verifyFormat( 4436 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 4437 " aaaaaaaaaaaaaaaaaaaaa(\n" 4438 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n" 4439 " aaaaaaaaaaaaaaaa);", 4440 Style); 4441 verifyFormat( 4442 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 4443 " aaaaaaaaaaaaaaaaaaaaa(\n" 4444 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n" 4445 " aaaaaaaaaaaaaaaa);", 4446 Style); 4447 } 4448 4449 TEST_F(FormatTest, ParenthesesAndOperandAlignment) { 4450 FormatStyle Style = getLLVMStyleWithColumns(40); 4451 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4452 " bbbbbbbbbbbbbbbbbbbbbb);", 4453 Style); 4454 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 4455 Style.AlignOperands = false; 4456 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4457 " bbbbbbbbbbbbbbbbbbbbbb);", 4458 Style); 4459 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4460 Style.AlignOperands = true; 4461 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4462 " bbbbbbbbbbbbbbbbbbbbbb);", 4463 Style); 4464 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4465 Style.AlignOperands = false; 4466 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4467 " bbbbbbbbbbbbbbbbbbbbbb);", 4468 Style); 4469 } 4470 4471 TEST_F(FormatTest, BreaksConditionalExpressions) { 4472 verifyFormat( 4473 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4474 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4475 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4476 verifyFormat( 4477 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 4478 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4479 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4480 verifyFormat( 4481 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4482 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4483 verifyFormat( 4484 "aaaa(aaaaaaaaa, aaaaaaaaa,\n" 4485 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4486 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4487 verifyFormat( 4488 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" 4489 " : aaaaaaaaaaaaa);"); 4490 verifyFormat( 4491 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4492 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4493 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4494 " aaaaaaaaaaaaa);"); 4495 verifyFormat( 4496 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4497 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4498 " aaaaaaaaaaaaa);"); 4499 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4500 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4501 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4502 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4503 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4504 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4505 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4506 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4507 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4508 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4509 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4510 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4511 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4512 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4513 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4514 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4515 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4516 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4517 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4518 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4519 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 4520 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4521 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4522 " : aaaaaaaaaaaaaaaa;"); 4523 verifyFormat( 4524 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4525 " ? aaaaaaaaaaaaaaa\n" 4526 " : aaaaaaaaaaaaaaa;"); 4527 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 4528 " aaaaaaaaa\n" 4529 " ? b\n" 4530 " : c);"); 4531 verifyFormat("return aaaa == bbbb\n" 4532 " // comment\n" 4533 " ? aaaa\n" 4534 " : bbbb;"); 4535 verifyFormat("unsigned Indent =\n" 4536 " format(TheLine.First,\n" 4537 " IndentForLevel[TheLine.Level] >= 0\n" 4538 " ? IndentForLevel[TheLine.Level]\n" 4539 " : TheLine * 2,\n" 4540 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 4541 getLLVMStyleWithColumns(60)); 4542 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 4543 " ? aaaaaaaaaaaaaaa\n" 4544 " : bbbbbbbbbbbbbbb //\n" 4545 " ? ccccccccccccccc\n" 4546 " : ddddddddddddddd;"); 4547 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 4548 " ? aaaaaaaaaaaaaaa\n" 4549 " : (bbbbbbbbbbbbbbb //\n" 4550 " ? ccccccccccccccc\n" 4551 " : ddddddddddddddd);"); 4552 verifyFormat( 4553 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4554 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4555 " aaaaaaaaaaaaaaaaaaaaa +\n" 4556 " aaaaaaaaaaaaaaaaaaaaa\n" 4557 " : aaaaaaaaaa;"); 4558 verifyFormat( 4559 "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4560 " : aaaaaaaaaaaaaaaaaaaaaa\n" 4561 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4562 4563 FormatStyle NoBinPacking = getLLVMStyle(); 4564 NoBinPacking.BinPackArguments = false; 4565 verifyFormat( 4566 "void f() {\n" 4567 " g(aaa,\n" 4568 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 4569 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4570 " ? aaaaaaaaaaaaaaa\n" 4571 " : aaaaaaaaaaaaaaa);\n" 4572 "}", 4573 NoBinPacking); 4574 verifyFormat( 4575 "void f() {\n" 4576 " g(aaa,\n" 4577 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 4578 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4579 " ?: aaaaaaaaaaaaaaa);\n" 4580 "}", 4581 NoBinPacking); 4582 4583 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n" 4584 " // comment.\n" 4585 " ccccccccccccccccccccccccccccccccccccccc\n" 4586 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4587 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);"); 4588 4589 // Assignments in conditional expressions. Apparently not uncommon :-(. 4590 verifyFormat("return a != b\n" 4591 " // comment\n" 4592 " ? a = b\n" 4593 " : a = b;"); 4594 verifyFormat("return a != b\n" 4595 " // comment\n" 4596 " ? a = a != b\n" 4597 " // comment\n" 4598 " ? a = b\n" 4599 " : a\n" 4600 " : a;\n"); 4601 verifyFormat("return a != b\n" 4602 " // comment\n" 4603 " ? a\n" 4604 " : a = a != b\n" 4605 " // comment\n" 4606 " ? a = b\n" 4607 " : a;"); 4608 } 4609 4610 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) { 4611 FormatStyle Style = getLLVMStyle(); 4612 Style.BreakBeforeTernaryOperators = false; 4613 Style.ColumnLimit = 70; 4614 verifyFormat( 4615 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4616 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4617 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4618 Style); 4619 verifyFormat( 4620 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 4621 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4622 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4623 Style); 4624 verifyFormat( 4625 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4626 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4627 Style); 4628 verifyFormat( 4629 "aaaa(aaaaaaaa, aaaaaaaaaa,\n" 4630 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4631 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4632 Style); 4633 verifyFormat( 4634 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n" 4635 " aaaaaaaaaaaaa);", 4636 Style); 4637 verifyFormat( 4638 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4639 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4640 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4641 " aaaaaaaaaaaaa);", 4642 Style); 4643 verifyFormat( 4644 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4645 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4646 " aaaaaaaaaaaaa);", 4647 Style); 4648 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4649 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4650 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 4651 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4652 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4653 Style); 4654 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4655 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4656 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4657 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 4658 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4659 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4660 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4661 Style); 4662 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4663 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n" 4664 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4665 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4666 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4667 Style); 4668 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4669 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4670 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4671 Style); 4672 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 4673 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4674 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4675 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4676 Style); 4677 verifyFormat( 4678 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4679 " aaaaaaaaaaaaaaa :\n" 4680 " aaaaaaaaaaaaaaa;", 4681 Style); 4682 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 4683 " aaaaaaaaa ?\n" 4684 " b :\n" 4685 " c);", 4686 Style); 4687 verifyFormat("unsigned Indent =\n" 4688 " format(TheLine.First,\n" 4689 " IndentForLevel[TheLine.Level] >= 0 ?\n" 4690 " IndentForLevel[TheLine.Level] :\n" 4691 " TheLine * 2,\n" 4692 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 4693 Style); 4694 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 4695 " aaaaaaaaaaaaaaa :\n" 4696 " bbbbbbbbbbbbbbb ? //\n" 4697 " ccccccccccccccc :\n" 4698 " ddddddddddddddd;", 4699 Style); 4700 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 4701 " aaaaaaaaaaaaaaa :\n" 4702 " (bbbbbbbbbbbbbbb ? //\n" 4703 " ccccccccccccccc :\n" 4704 " ddddddddddddddd);", 4705 Style); 4706 verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4707 " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n" 4708 " ccccccccccccccccccccccccccc;", 4709 Style); 4710 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4711 " aaaaa :\n" 4712 " bbbbbbbbbbbbbbb + cccccccccccccccc;", 4713 Style); 4714 } 4715 4716 TEST_F(FormatTest, DeclarationsOfMultipleVariables) { 4717 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" 4718 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); 4719 verifyFormat("bool a = true, b = false;"); 4720 4721 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" 4722 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" 4723 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" 4724 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); 4725 verifyFormat( 4726 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 4727 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n" 4728 " d = e && f;"); 4729 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n" 4730 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;"); 4731 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 4732 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;"); 4733 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n" 4734 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;"); 4735 4736 FormatStyle Style = getGoogleStyle(); 4737 Style.PointerAlignment = FormatStyle::PAS_Left; 4738 Style.DerivePointerAlignment = false; 4739 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4740 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" 4741 " *b = bbbbbbbbbbbbbbbbbbb;", 4742 Style); 4743 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 4744 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;", 4745 Style); 4746 verifyFormat("vector<int*> a, b;", Style); 4747 verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style); 4748 } 4749 4750 TEST_F(FormatTest, ConditionalExpressionsInBrackets) { 4751 verifyFormat("arr[foo ? bar : baz];"); 4752 verifyFormat("f()[foo ? bar : baz];"); 4753 verifyFormat("(a + b)[foo ? bar : baz];"); 4754 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); 4755 } 4756 4757 TEST_F(FormatTest, AlignsStringLiterals) { 4758 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" 4759 " \"short literal\");"); 4760 verifyFormat( 4761 "looooooooooooooooooooooooongFunction(\n" 4762 " \"short literal\"\n" 4763 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); 4764 verifyFormat("someFunction(\"Always break between multi-line\"\n" 4765 " \" string literals\",\n" 4766 " and, other, parameters);"); 4767 EXPECT_EQ("fun + \"1243\" /* comment */\n" 4768 " \"5678\";", 4769 format("fun + \"1243\" /* comment */\n" 4770 " \"5678\";", 4771 getLLVMStyleWithColumns(28))); 4772 EXPECT_EQ( 4773 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 4774 " \"aaaaaaaaaaaaaaaaaaaaa\"\n" 4775 " \"aaaaaaaaaaaaaaaa\";", 4776 format("aaaaaa =" 4777 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " 4778 "aaaaaaaaaaaaaaaaaaaaa\" " 4779 "\"aaaaaaaaaaaaaaaa\";")); 4780 verifyFormat("a = a + \"a\"\n" 4781 " \"a\"\n" 4782 " \"a\";"); 4783 verifyFormat("f(\"a\", \"b\"\n" 4784 " \"c\");"); 4785 4786 verifyFormat( 4787 "#define LL_FORMAT \"ll\"\n" 4788 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n" 4789 " \"d, ddddddddd: %\" LL_FORMAT \"d\");"); 4790 4791 verifyFormat("#define A(X) \\\n" 4792 " \"aaaaa\" #X \"bbbbbb\" \\\n" 4793 " \"ccccc\"", 4794 getLLVMStyleWithColumns(23)); 4795 verifyFormat("#define A \"def\"\n" 4796 "f(\"abc\" A \"ghi\"\n" 4797 " \"jkl\");"); 4798 4799 verifyFormat("f(L\"a\"\n" 4800 " L\"b\");"); 4801 verifyFormat("#define A(X) \\\n" 4802 " L\"aaaaa\" #X L\"bbbbbb\" \\\n" 4803 " L\"ccccc\"", 4804 getLLVMStyleWithColumns(25)); 4805 4806 verifyFormat("f(@\"a\"\n" 4807 " @\"b\");"); 4808 verifyFormat("NSString s = @\"a\"\n" 4809 " @\"b\"\n" 4810 " @\"c\";"); 4811 verifyFormat("NSString s = @\"a\"\n" 4812 " \"b\"\n" 4813 " \"c\";"); 4814 } 4815 4816 TEST_F(FormatTest, ReturnTypeBreakingStyle) { 4817 FormatStyle Style = getLLVMStyle(); 4818 // No declarations or definitions should be moved to own line. 4819 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None; 4820 verifyFormat("class A {\n" 4821 " int f() { return 1; }\n" 4822 " int g();\n" 4823 "};\n" 4824 "int f() { return 1; }\n" 4825 "int g();\n", 4826 Style); 4827 4828 // All declarations and definitions should have the return type moved to its 4829 // own 4830 // line. 4831 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 4832 verifyFormat("class E {\n" 4833 " int\n" 4834 " f() {\n" 4835 " return 1;\n" 4836 " }\n" 4837 " int\n" 4838 " g();\n" 4839 "};\n" 4840 "int\n" 4841 "f() {\n" 4842 " return 1;\n" 4843 "}\n" 4844 "int\n" 4845 "g();\n", 4846 Style); 4847 4848 // Top-level definitions, and no kinds of declarations should have the 4849 // return type moved to its own line. 4850 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions; 4851 verifyFormat("class B {\n" 4852 " int f() { return 1; }\n" 4853 " int g();\n" 4854 "};\n" 4855 "int\n" 4856 "f() {\n" 4857 " return 1;\n" 4858 "}\n" 4859 "int g();\n", 4860 Style); 4861 4862 // Top-level definitions and declarations should have the return type moved 4863 // to its own line. 4864 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel; 4865 verifyFormat("class C {\n" 4866 " int f() { return 1; }\n" 4867 " int g();\n" 4868 "};\n" 4869 "int\n" 4870 "f() {\n" 4871 " return 1;\n" 4872 "}\n" 4873 "int\n" 4874 "g();\n", 4875 Style); 4876 4877 // All definitions should have the return type moved to its own line, but no 4878 // kinds of declarations. 4879 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 4880 verifyFormat("class D {\n" 4881 " int\n" 4882 " f() {\n" 4883 " return 1;\n" 4884 " }\n" 4885 " int g();\n" 4886 "};\n" 4887 "int\n" 4888 "f() {\n" 4889 " return 1;\n" 4890 "}\n" 4891 "int g();\n", 4892 Style); 4893 verifyFormat("const char *\n" 4894 "f(void) {\n" // Break here. 4895 " return \"\";\n" 4896 "}\n" 4897 "const char *bar(void);\n", // No break here. 4898 Style); 4899 verifyFormat("template <class T>\n" 4900 "T *\n" 4901 "f(T &c) {\n" // Break here. 4902 " return NULL;\n" 4903 "}\n" 4904 "template <class T> T *f(T &c);\n", // No break here. 4905 Style); 4906 verifyFormat("class C {\n" 4907 " int\n" 4908 " operator+() {\n" 4909 " return 1;\n" 4910 " }\n" 4911 " int\n" 4912 " operator()() {\n" 4913 " return 1;\n" 4914 " }\n" 4915 "};\n", 4916 Style); 4917 verifyFormat("void\n" 4918 "A::operator()() {}\n" 4919 "void\n" 4920 "A::operator>>() {}\n" 4921 "void\n" 4922 "A::operator+() {}\n", 4923 Style); 4924 verifyFormat("void *operator new(std::size_t s);", // No break here. 4925 Style); 4926 verifyFormat("void *\n" 4927 "operator new(std::size_t s) {}", 4928 Style); 4929 verifyFormat("void *\n" 4930 "operator delete[](void *ptr) {}", 4931 Style); 4932 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 4933 verifyFormat("const char *\n" 4934 "f(void)\n" // Break here. 4935 "{\n" 4936 " return \"\";\n" 4937 "}\n" 4938 "const char *bar(void);\n", // No break here. 4939 Style); 4940 verifyFormat("template <class T>\n" 4941 "T *\n" // Problem here: no line break 4942 "f(T &c)\n" // Break here. 4943 "{\n" 4944 " return NULL;\n" 4945 "}\n" 4946 "template <class T> T *f(T &c);\n", // No break here. 4947 Style); 4948 } 4949 4950 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) { 4951 FormatStyle NoBreak = getLLVMStyle(); 4952 NoBreak.AlwaysBreakBeforeMultilineStrings = false; 4953 FormatStyle Break = getLLVMStyle(); 4954 Break.AlwaysBreakBeforeMultilineStrings = true; 4955 verifyFormat("aaaa = \"bbbb\"\n" 4956 " \"cccc\";", 4957 NoBreak); 4958 verifyFormat("aaaa =\n" 4959 " \"bbbb\"\n" 4960 " \"cccc\";", 4961 Break); 4962 verifyFormat("aaaa(\"bbbb\"\n" 4963 " \"cccc\");", 4964 NoBreak); 4965 verifyFormat("aaaa(\n" 4966 " \"bbbb\"\n" 4967 " \"cccc\");", 4968 Break); 4969 verifyFormat("aaaa(qqq, \"bbbb\"\n" 4970 " \"cccc\");", 4971 NoBreak); 4972 verifyFormat("aaaa(qqq,\n" 4973 " \"bbbb\"\n" 4974 " \"cccc\");", 4975 Break); 4976 verifyFormat("aaaa(qqq,\n" 4977 " L\"bbbb\"\n" 4978 " L\"cccc\");", 4979 Break); 4980 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n" 4981 " \"bbbb\"));", 4982 Break); 4983 verifyFormat("string s = someFunction(\n" 4984 " \"abc\"\n" 4985 " \"abc\");", 4986 Break); 4987 4988 // As we break before unary operators, breaking right after them is bad. 4989 verifyFormat("string foo = abc ? \"x\"\n" 4990 " \"blah blah blah blah blah blah\"\n" 4991 " : \"y\";", 4992 Break); 4993 4994 // Don't break if there is no column gain. 4995 verifyFormat("f(\"aaaa\"\n" 4996 " \"bbbb\");", 4997 Break); 4998 4999 // Treat literals with escaped newlines like multi-line string literals. 5000 EXPECT_EQ("x = \"a\\\n" 5001 "b\\\n" 5002 "c\";", 5003 format("x = \"a\\\n" 5004 "b\\\n" 5005 "c\";", 5006 NoBreak)); 5007 EXPECT_EQ("xxxx =\n" 5008 " \"a\\\n" 5009 "b\\\n" 5010 "c\";", 5011 format("xxxx = \"a\\\n" 5012 "b\\\n" 5013 "c\";", 5014 Break)); 5015 5016 EXPECT_EQ("NSString *const kString =\n" 5017 " @\"aaaa\"\n" 5018 " @\"bbbb\";", 5019 format("NSString *const kString = @\"aaaa\"\n" 5020 "@\"bbbb\";", 5021 Break)); 5022 5023 Break.ColumnLimit = 0; 5024 verifyFormat("const char *hello = \"hello llvm\";", Break); 5025 } 5026 5027 TEST_F(FormatTest, AlignsPipes) { 5028 verifyFormat( 5029 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5030 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5031 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5032 verifyFormat( 5033 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 5034 " << aaaaaaaaaaaaaaaaaaaa;"); 5035 verifyFormat( 5036 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5037 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5038 verifyFormat( 5039 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 5040 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5041 verifyFormat( 5042 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 5043 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 5044 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 5045 verifyFormat( 5046 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5047 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5048 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5049 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5050 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5051 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5052 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 5053 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n" 5054 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);"); 5055 verifyFormat( 5056 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5057 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5058 verifyFormat( 5059 "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n" 5060 " aaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5061 5062 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n" 5063 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();"); 5064 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5065 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5066 " aaaaaaaaaaaaaaaaaaaaa)\n" 5067 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5068 verifyFormat("LOG_IF(aaa == //\n" 5069 " bbb)\n" 5070 " << a << b;"); 5071 5072 // But sometimes, breaking before the first "<<" is desirable. 5073 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5074 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);"); 5075 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n" 5076 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5077 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5078 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n" 5079 " << BEF << IsTemplate << Description << E->getType();"); 5080 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5081 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5082 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5083 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5084 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5085 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5086 " << aaa;"); 5087 5088 verifyFormat( 5089 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5090 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5091 5092 // Incomplete string literal. 5093 EXPECT_EQ("llvm::errs() << \"\n" 5094 " << a;", 5095 format("llvm::errs() << \"\n<<a;")); 5096 5097 verifyFormat("void f() {\n" 5098 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n" 5099 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n" 5100 "}"); 5101 5102 // Handle 'endl'. 5103 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n" 5104 " << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 5105 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 5106 5107 // Handle '\n'. 5108 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n" 5109 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 5110 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n" 5111 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';"); 5112 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n" 5113 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";"); 5114 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 5115 } 5116 5117 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) { 5118 verifyFormat("return out << \"somepacket = {\\n\"\n" 5119 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" 5120 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" 5121 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" 5122 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" 5123 " << \"}\";"); 5124 5125 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 5126 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 5127 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;"); 5128 verifyFormat( 5129 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" 5130 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" 5131 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" 5132 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" 5133 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); 5134 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" 5135 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 5136 verifyFormat( 5137 "void f() {\n" 5138 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n" 5139 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 5140 "}"); 5141 5142 // Breaking before the first "<<" is generally not desirable. 5143 verifyFormat( 5144 "llvm::errs()\n" 5145 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5146 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5147 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5148 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5149 getLLVMStyleWithColumns(70)); 5150 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5151 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5152 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5153 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5154 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5155 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5156 getLLVMStyleWithColumns(70)); 5157 5158 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 5159 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 5160 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;"); 5161 verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 5162 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 5163 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);"); 5164 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n" 5165 " (aaaa + aaaa);", 5166 getLLVMStyleWithColumns(40)); 5167 verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n" 5168 " (aaaaaaa + aaaaa));", 5169 getLLVMStyleWithColumns(40)); 5170 verifyFormat( 5171 "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n" 5172 " SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n" 5173 " bbbbbbbbbbbbbbbbbbbbbbb);"); 5174 } 5175 5176 TEST_F(FormatTest, UnderstandsEquals) { 5177 verifyFormat( 5178 "aaaaaaaaaaaaaaaaa =\n" 5179 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5180 verifyFormat( 5181 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5182 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 5183 verifyFormat( 5184 "if (a) {\n" 5185 " f();\n" 5186 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5187 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 5188 "}"); 5189 5190 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5191 " 100000000 + 10000000) {\n}"); 5192 } 5193 5194 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 5195 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 5196 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 5197 5198 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 5199 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 5200 5201 verifyFormat( 5202 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 5203 " Parameter2);"); 5204 5205 verifyFormat( 5206 "ShortObject->shortFunction(\n" 5207 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 5208 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 5209 5210 verifyFormat("loooooooooooooongFunction(\n" 5211 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 5212 5213 verifyFormat( 5214 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 5215 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 5216 5217 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 5218 " .WillRepeatedly(Return(SomeValue));"); 5219 verifyFormat("void f() {\n" 5220 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 5221 " .Times(2)\n" 5222 " .WillRepeatedly(Return(SomeValue));\n" 5223 "}"); 5224 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n" 5225 " ccccccccccccccccccccccc);"); 5226 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5227 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5228 " .aaaaa(aaaaa),\n" 5229 " aaaaaaaaaaaaaaaaaaaaa);"); 5230 verifyFormat("void f() {\n" 5231 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5232 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n" 5233 "}"); 5234 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5235 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5236 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5237 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5238 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 5239 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5240 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5241 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5242 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n" 5243 "}"); 5244 5245 // Here, it is not necessary to wrap at "." or "->". 5246 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 5247 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 5248 verifyFormat( 5249 "aaaaaaaaaaa->aaaaaaaaa(\n" 5250 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5251 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 5252 5253 verifyFormat( 5254 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5255 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); 5256 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n" 5257 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 5258 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" 5259 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 5260 5261 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5262 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5263 " .a();"); 5264 5265 FormatStyle NoBinPacking = getLLVMStyle(); 5266 NoBinPacking.BinPackParameters = false; 5267 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 5268 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 5269 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" 5270 " aaaaaaaaaaaaaaaaaaa,\n" 5271 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5272 NoBinPacking); 5273 5274 // If there is a subsequent call, change to hanging indentation. 5275 verifyFormat( 5276 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5277 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n" 5278 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5279 verifyFormat( 5280 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5281 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));"); 5282 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5283 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5284 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5285 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5286 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5287 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 5288 } 5289 5290 TEST_F(FormatTest, WrapsTemplateDeclarations) { 5291 verifyFormat("template <typename T>\n" 5292 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 5293 verifyFormat("template <typename T>\n" 5294 "// T should be one of {A, B}.\n" 5295 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 5296 verifyFormat( 5297 "template <typename T>\n" 5298 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); 5299 verifyFormat("template <typename T>\n" 5300 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 5301 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 5302 verifyFormat( 5303 "template <typename T>\n" 5304 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 5305 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 5306 verifyFormat( 5307 "template <typename T>\n" 5308 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 5309 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 5310 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5311 verifyFormat("template <typename T>\n" 5312 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5313 " int aaaaaaaaaaaaaaaaaaaaaa);"); 5314 verifyFormat( 5315 "template <typename T1, typename T2 = char, typename T3 = char,\n" 5316 " typename T4 = char>\n" 5317 "void f();"); 5318 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n" 5319 " template <typename> class cccccccccccccccccccccc,\n" 5320 " typename ddddddddddddd>\n" 5321 "class C {};"); 5322 verifyFormat( 5323 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 5324 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5325 5326 verifyFormat("void f() {\n" 5327 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 5328 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n" 5329 "}"); 5330 5331 verifyFormat("template <typename T> class C {};"); 5332 verifyFormat("template <typename T> void f();"); 5333 verifyFormat("template <typename T> void f() {}"); 5334 verifyFormat( 5335 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 5336 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5337 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n" 5338 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 5339 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5340 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n" 5341 " bbbbbbbbbbbbbbbbbbbbbbbb);", 5342 getLLVMStyleWithColumns(72)); 5343 EXPECT_EQ("static_cast<A< //\n" 5344 " B> *>(\n" 5345 "\n" 5346 ");", 5347 format("static_cast<A<//\n" 5348 " B>*>(\n" 5349 "\n" 5350 " );")); 5351 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5352 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);"); 5353 5354 FormatStyle AlwaysBreak = getLLVMStyle(); 5355 AlwaysBreak.AlwaysBreakTemplateDeclarations = true; 5356 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak); 5357 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak); 5358 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak); 5359 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5360 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 5361 " ccccccccccccccccccccccccccccccccccccccccccccccc);"); 5362 verifyFormat("template <template <typename> class Fooooooo,\n" 5363 " template <typename> class Baaaaaaar>\n" 5364 "struct C {};", 5365 AlwaysBreak); 5366 verifyFormat("template <typename T> // T can be A, B or C.\n" 5367 "struct C {};", 5368 AlwaysBreak); 5369 verifyFormat("template <enum E> class A {\n" 5370 "public:\n" 5371 " E *f();\n" 5372 "};"); 5373 } 5374 5375 TEST_F(FormatTest, WrapsTemplateParameters) { 5376 FormatStyle Style = getLLVMStyle(); 5377 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 5378 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 5379 verifyFormat( 5380 "template <typename... a> struct q {};\n" 5381 "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 5382 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 5383 " y;", 5384 Style); 5385 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 5386 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 5387 verifyFormat( 5388 "template <typename... a> struct r {};\n" 5389 "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 5390 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 5391 " y;", 5392 Style); 5393 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 5394 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 5395 verifyFormat( 5396 "template <typename... a> struct s {};\n" 5397 "extern s<\n" 5398 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 5399 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n" 5400 " y;", 5401 Style); 5402 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 5403 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 5404 verifyFormat( 5405 "template <typename... a> struct t {};\n" 5406 "extern t<\n" 5407 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 5408 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n" 5409 " y;", 5410 Style); 5411 } 5412 5413 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 5414 verifyFormat( 5415 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5416 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5417 verifyFormat( 5418 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5419 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5420 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 5421 5422 // FIXME: Should we have the extra indent after the second break? 5423 verifyFormat( 5424 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5425 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5426 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5427 5428 verifyFormat( 5429 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 5430 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 5431 5432 // Breaking at nested name specifiers is generally not desirable. 5433 verifyFormat( 5434 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5435 " aaaaaaaaaaaaaaaaaaaaaaa);"); 5436 5437 verifyFormat( 5438 "aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n" 5439 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5440 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5441 " aaaaaaaaaaaaaaaaaaaaa);", 5442 getLLVMStyleWithColumns(74)); 5443 5444 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5445 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5446 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5447 } 5448 5449 TEST_F(FormatTest, UnderstandsTemplateParameters) { 5450 verifyFormat("A<int> a;"); 5451 verifyFormat("A<A<A<int>>> a;"); 5452 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 5453 verifyFormat("bool x = a < 1 || 2 > a;"); 5454 verifyFormat("bool x = 5 < f<int>();"); 5455 verifyFormat("bool x = f<int>() > 5;"); 5456 verifyFormat("bool x = 5 < a<int>::x;"); 5457 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 5458 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 5459 5460 verifyGoogleFormat("A<A<int>> a;"); 5461 verifyGoogleFormat("A<A<A<int>>> a;"); 5462 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 5463 verifyGoogleFormat("A<A<int> > a;"); 5464 verifyGoogleFormat("A<A<A<int> > > a;"); 5465 verifyGoogleFormat("A<A<A<A<int> > > > a;"); 5466 verifyGoogleFormat("A<::A<int>> a;"); 5467 verifyGoogleFormat("A<::A> a;"); 5468 verifyGoogleFormat("A< ::A> a;"); 5469 verifyGoogleFormat("A< ::A<int> > a;"); 5470 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); 5471 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); 5472 EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle())); 5473 EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle())); 5474 EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };", 5475 format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle())); 5476 5477 verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp)); 5478 5479 verifyFormat("test >> a >> b;"); 5480 verifyFormat("test << a >> b;"); 5481 5482 verifyFormat("f<int>();"); 5483 verifyFormat("template <typename T> void f() {}"); 5484 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;"); 5485 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : " 5486 "sizeof(char)>::type>;"); 5487 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};"); 5488 verifyFormat("f(a.operator()<A>());"); 5489 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5490 " .template operator()<A>());", 5491 getLLVMStyleWithColumns(35)); 5492 5493 // Not template parameters. 5494 verifyFormat("return a < b && c > d;"); 5495 verifyFormat("void f() {\n" 5496 " while (a < b && c > d) {\n" 5497 " }\n" 5498 "}"); 5499 verifyFormat("template <typename... Types>\n" 5500 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}"); 5501 5502 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5503 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);", 5504 getLLVMStyleWithColumns(60)); 5505 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");"); 5506 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}"); 5507 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <"); 5508 } 5509 5510 TEST_F(FormatTest, BitshiftOperatorWidth) { 5511 EXPECT_EQ("int a = 1 << 2; /* foo\n" 5512 " bar */", 5513 format("int a=1<<2; /* foo\n" 5514 " bar */")); 5515 5516 EXPECT_EQ("int b = 256 >> 1; /* foo\n" 5517 " bar */", 5518 format("int b =256>>1 ; /* foo\n" 5519 " bar */")); 5520 } 5521 5522 TEST_F(FormatTest, UnderstandsBinaryOperators) { 5523 verifyFormat("COMPARE(a, ==, b);"); 5524 verifyFormat("auto s = sizeof...(Ts) - 1;"); 5525 } 5526 5527 TEST_F(FormatTest, UnderstandsPointersToMembers) { 5528 verifyFormat("int A::*x;"); 5529 verifyFormat("int (S::*func)(void *);"); 5530 verifyFormat("void f() { int (S::*func)(void *); }"); 5531 verifyFormat("typedef bool *(Class::*Member)() const;"); 5532 verifyFormat("void f() {\n" 5533 " (a->*f)();\n" 5534 " a->*x;\n" 5535 " (a.*f)();\n" 5536 " ((*a).*f)();\n" 5537 " a.*x;\n" 5538 "}"); 5539 verifyFormat("void f() {\n" 5540 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 5541 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n" 5542 "}"); 5543 verifyFormat( 5544 "(aaaaaaaaaa->*bbbbbbb)(\n" 5545 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 5546 FormatStyle Style = getLLVMStyle(); 5547 Style.PointerAlignment = FormatStyle::PAS_Left; 5548 verifyFormat("typedef bool* (Class::*Member)() const;", Style); 5549 } 5550 5551 TEST_F(FormatTest, UnderstandsUnaryOperators) { 5552 verifyFormat("int a = -2;"); 5553 verifyFormat("f(-1, -2, -3);"); 5554 verifyFormat("a[-1] = 5;"); 5555 verifyFormat("int a = 5 + -2;"); 5556 verifyFormat("if (i == -1) {\n}"); 5557 verifyFormat("if (i != -1) {\n}"); 5558 verifyFormat("if (i > -1) {\n}"); 5559 verifyFormat("if (i < -1) {\n}"); 5560 verifyFormat("++(a->f());"); 5561 verifyFormat("--(a->f());"); 5562 verifyFormat("(a->f())++;"); 5563 verifyFormat("a[42]++;"); 5564 verifyFormat("if (!(a->f())) {\n}"); 5565 5566 verifyFormat("a-- > b;"); 5567 verifyFormat("b ? -a : c;"); 5568 verifyFormat("n * sizeof char16;"); 5569 verifyFormat("n * alignof char16;", getGoogleStyle()); 5570 verifyFormat("sizeof(char);"); 5571 verifyFormat("alignof(char);", getGoogleStyle()); 5572 5573 verifyFormat("return -1;"); 5574 verifyFormat("switch (a) {\n" 5575 "case -1:\n" 5576 " break;\n" 5577 "}"); 5578 verifyFormat("#define X -1"); 5579 verifyFormat("#define X -kConstant"); 5580 5581 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};"); 5582 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};"); 5583 5584 verifyFormat("int a = /* confusing comment */ -1;"); 5585 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 5586 verifyFormat("int a = i /* confusing comment */++;"); 5587 } 5588 5589 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) { 5590 verifyFormat("if (!aaaaaaaaaa( // break\n" 5591 " aaaaa)) {\n" 5592 "}"); 5593 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n" 5594 " aaaaa));"); 5595 verifyFormat("*aaa = aaaaaaa( // break\n" 5596 " bbbbbb);"); 5597 } 5598 5599 TEST_F(FormatTest, UnderstandsOverloadedOperators) { 5600 verifyFormat("bool operator<();"); 5601 verifyFormat("bool operator>();"); 5602 verifyFormat("bool operator=();"); 5603 verifyFormat("bool operator==();"); 5604 verifyFormat("bool operator!=();"); 5605 verifyFormat("int operator+();"); 5606 verifyFormat("int operator++();"); 5607 verifyFormat("int operator++(int) volatile noexcept;"); 5608 verifyFormat("bool operator,();"); 5609 verifyFormat("bool operator();"); 5610 verifyFormat("bool operator()();"); 5611 verifyFormat("bool operator[]();"); 5612 verifyFormat("operator bool();"); 5613 verifyFormat("operator int();"); 5614 verifyFormat("operator void *();"); 5615 verifyFormat("operator SomeType<int>();"); 5616 verifyFormat("operator SomeType<int, int>();"); 5617 verifyFormat("operator SomeType<SomeType<int>>();"); 5618 verifyFormat("void *operator new(std::size_t size);"); 5619 verifyFormat("void *operator new[](std::size_t size);"); 5620 verifyFormat("void operator delete(void *ptr);"); 5621 verifyFormat("void operator delete[](void *ptr);"); 5622 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n" 5623 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);"); 5624 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n" 5625 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;"); 5626 5627 verifyFormat( 5628 "ostream &operator<<(ostream &OutputStream,\n" 5629 " SomeReallyLongType WithSomeReallyLongValue);"); 5630 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n" 5631 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n" 5632 " return left.group < right.group;\n" 5633 "}"); 5634 verifyFormat("SomeType &operator=(const SomeType &S);"); 5635 verifyFormat("f.template operator()<int>();"); 5636 5637 verifyGoogleFormat("operator void*();"); 5638 verifyGoogleFormat("operator SomeType<SomeType<int>>();"); 5639 verifyGoogleFormat("operator ::A();"); 5640 5641 verifyFormat("using A::operator+;"); 5642 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n" 5643 "int i;"); 5644 } 5645 5646 TEST_F(FormatTest, UnderstandsFunctionRefQualification) { 5647 verifyFormat("Deleted &operator=(const Deleted &) & = default;"); 5648 verifyFormat("Deleted &operator=(const Deleted &) && = delete;"); 5649 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;"); 5650 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;"); 5651 verifyFormat("Deleted &operator=(const Deleted &) &;"); 5652 verifyFormat("Deleted &operator=(const Deleted &) &&;"); 5653 verifyFormat("SomeType MemberFunction(const Deleted &) &;"); 5654 verifyFormat("SomeType MemberFunction(const Deleted &) &&;"); 5655 verifyFormat("SomeType MemberFunction(const Deleted &) && {}"); 5656 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}"); 5657 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}"); 5658 verifyFormat("void Fn(T const &) const &;"); 5659 verifyFormat("void Fn(T const volatile &&) const volatile &&;"); 5660 verifyFormat("template <typename T>\n" 5661 "void F(T) && = delete;", 5662 getGoogleStyle()); 5663 5664 FormatStyle AlignLeft = getLLVMStyle(); 5665 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 5666 verifyFormat("void A::b() && {}", AlignLeft); 5667 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft); 5668 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;", 5669 AlignLeft); 5670 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft); 5671 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft); 5672 verifyFormat("auto Function(T t) & -> void {}", AlignLeft); 5673 verifyFormat("auto Function(T... t) & -> void {}", AlignLeft); 5674 verifyFormat("auto Function(T) & -> void {}", AlignLeft); 5675 verifyFormat("auto Function(T) & -> void;", AlignLeft); 5676 verifyFormat("void Fn(T const&) const&;", AlignLeft); 5677 verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft); 5678 5679 FormatStyle Spaces = getLLVMStyle(); 5680 Spaces.SpacesInCStyleCastParentheses = true; 5681 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces); 5682 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces); 5683 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces); 5684 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces); 5685 5686 Spaces.SpacesInCStyleCastParentheses = false; 5687 Spaces.SpacesInParentheses = true; 5688 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces); 5689 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces); 5690 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces); 5691 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces); 5692 } 5693 5694 TEST_F(FormatTest, UnderstandsNewAndDelete) { 5695 verifyFormat("void f() {\n" 5696 " A *a = new A;\n" 5697 " A *a = new (placement) A;\n" 5698 " delete a;\n" 5699 " delete (A *)a;\n" 5700 "}"); 5701 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 5702 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 5703 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5704 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 5705 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 5706 verifyFormat("delete[] h->p;"); 5707 } 5708 5709 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 5710 verifyFormat("int *f(int *a) {}"); 5711 verifyFormat("int main(int argc, char **argv) {}"); 5712 verifyFormat("Test::Test(int b) : a(b * b) {}"); 5713 verifyIndependentOfContext("f(a, *a);"); 5714 verifyFormat("void g() { f(*a); }"); 5715 verifyIndependentOfContext("int a = b * 10;"); 5716 verifyIndependentOfContext("int a = 10 * b;"); 5717 verifyIndependentOfContext("int a = b * c;"); 5718 verifyIndependentOfContext("int a += b * c;"); 5719 verifyIndependentOfContext("int a -= b * c;"); 5720 verifyIndependentOfContext("int a *= b * c;"); 5721 verifyIndependentOfContext("int a /= b * c;"); 5722 verifyIndependentOfContext("int a = *b;"); 5723 verifyIndependentOfContext("int a = *b * c;"); 5724 verifyIndependentOfContext("int a = b * *c;"); 5725 verifyIndependentOfContext("int a = b * (10);"); 5726 verifyIndependentOfContext("S << b * (10);"); 5727 verifyIndependentOfContext("return 10 * b;"); 5728 verifyIndependentOfContext("return *b * *c;"); 5729 verifyIndependentOfContext("return a & ~b;"); 5730 verifyIndependentOfContext("f(b ? *c : *d);"); 5731 verifyIndependentOfContext("int a = b ? *c : *d;"); 5732 verifyIndependentOfContext("*b = a;"); 5733 verifyIndependentOfContext("a * ~b;"); 5734 verifyIndependentOfContext("a * !b;"); 5735 verifyIndependentOfContext("a * +b;"); 5736 verifyIndependentOfContext("a * -b;"); 5737 verifyIndependentOfContext("a * ++b;"); 5738 verifyIndependentOfContext("a * --b;"); 5739 verifyIndependentOfContext("a[4] * b;"); 5740 verifyIndependentOfContext("a[a * a] = 1;"); 5741 verifyIndependentOfContext("f() * b;"); 5742 verifyIndependentOfContext("a * [self dostuff];"); 5743 verifyIndependentOfContext("int x = a * (a + b);"); 5744 verifyIndependentOfContext("(a *)(a + b);"); 5745 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;"); 5746 verifyIndependentOfContext("int *pa = (int *)&a;"); 5747 verifyIndependentOfContext("return sizeof(int **);"); 5748 verifyIndependentOfContext("return sizeof(int ******);"); 5749 verifyIndependentOfContext("return (int **&)a;"); 5750 verifyIndependentOfContext("f((*PointerToArray)[10]);"); 5751 verifyFormat("void f(Type (*parameter)[10]) {}"); 5752 verifyFormat("void f(Type (¶meter)[10]) {}"); 5753 verifyGoogleFormat("return sizeof(int**);"); 5754 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); 5755 verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); 5756 verifyFormat("auto a = [](int **&, int ***) {};"); 5757 verifyFormat("auto PointerBinding = [](const char *S) {};"); 5758 verifyFormat("typedef typeof(int(int, int)) *MyFunc;"); 5759 verifyFormat("[](const decltype(*a) &value) {}"); 5760 verifyFormat("decltype(a * b) F();"); 5761 verifyFormat("#define MACRO() [](A *a) { return 1; }"); 5762 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}"); 5763 verifyIndependentOfContext("typedef void (*f)(int *a);"); 5764 verifyIndependentOfContext("int i{a * b};"); 5765 verifyIndependentOfContext("aaa && aaa->f();"); 5766 verifyIndependentOfContext("int x = ~*p;"); 5767 verifyFormat("Constructor() : a(a), area(width * height) {}"); 5768 verifyFormat("Constructor() : a(a), area(a, width * height) {}"); 5769 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}"); 5770 verifyFormat("void f() { f(a, c * d); }"); 5771 verifyFormat("void f() { f(new a(), c * d); }"); 5772 verifyFormat("void f(const MyOverride &override);"); 5773 verifyFormat("void f(const MyFinal &final);"); 5774 verifyIndependentOfContext("bool a = f() && override.f();"); 5775 verifyIndependentOfContext("bool a = f() && final.f();"); 5776 5777 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 5778 5779 verifyIndependentOfContext("A<int *> a;"); 5780 verifyIndependentOfContext("A<int **> a;"); 5781 verifyIndependentOfContext("A<int *, int *> a;"); 5782 verifyIndependentOfContext("A<int *[]> a;"); 5783 verifyIndependentOfContext( 5784 "const char *const p = reinterpret_cast<const char *const>(q);"); 5785 verifyIndependentOfContext("A<int **, int **> a;"); 5786 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); 5787 verifyFormat("for (char **a = b; *a; ++a) {\n}"); 5788 verifyFormat("for (; a && b;) {\n}"); 5789 verifyFormat("bool foo = true && [] { return false; }();"); 5790 5791 verifyFormat( 5792 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5793 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5794 5795 verifyGoogleFormat("int const* a = &b;"); 5796 verifyGoogleFormat("**outparam = 1;"); 5797 verifyGoogleFormat("*outparam = a * b;"); 5798 verifyGoogleFormat("int main(int argc, char** argv) {}"); 5799 verifyGoogleFormat("A<int*> a;"); 5800 verifyGoogleFormat("A<int**> a;"); 5801 verifyGoogleFormat("A<int*, int*> a;"); 5802 verifyGoogleFormat("A<int**, int**> a;"); 5803 verifyGoogleFormat("f(b ? *c : *d);"); 5804 verifyGoogleFormat("int a = b ? *c : *d;"); 5805 verifyGoogleFormat("Type* t = **x;"); 5806 verifyGoogleFormat("Type* t = *++*x;"); 5807 verifyGoogleFormat("*++*x;"); 5808 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 5809 verifyGoogleFormat("Type* t = x++ * y;"); 5810 verifyGoogleFormat( 5811 "const char* const p = reinterpret_cast<const char* const>(q);"); 5812 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);"); 5813 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);"); 5814 verifyGoogleFormat("template <typename T>\n" 5815 "void f(int i = 0, SomeType** temps = NULL);"); 5816 5817 FormatStyle Left = getLLVMStyle(); 5818 Left.PointerAlignment = FormatStyle::PAS_Left; 5819 verifyFormat("x = *a(x) = *a(y);", Left); 5820 verifyFormat("for (;; *a = b) {\n}", Left); 5821 verifyFormat("return *this += 1;", Left); 5822 verifyFormat("throw *x;", Left); 5823 verifyFormat("delete *x;", Left); 5824 verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left); 5825 verifyFormat("[](const decltype(*a)* ptr) {}", Left); 5826 verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left); 5827 5828 verifyIndependentOfContext("a = *(x + y);"); 5829 verifyIndependentOfContext("a = &(x + y);"); 5830 verifyIndependentOfContext("*(x + y).call();"); 5831 verifyIndependentOfContext("&(x + y)->call();"); 5832 verifyFormat("void f() { &(*I).first; }"); 5833 5834 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 5835 verifyFormat( 5836 "int *MyValues = {\n" 5837 " *A, // Operator detection might be confused by the '{'\n" 5838 " *BB // Operator detection might be confused by previous comment\n" 5839 "};"); 5840 5841 verifyIndependentOfContext("if (int *a = &b)"); 5842 verifyIndependentOfContext("if (int &a = *b)"); 5843 verifyIndependentOfContext("if (a & b[i])"); 5844 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 5845 verifyIndependentOfContext("if (*b[i])"); 5846 verifyIndependentOfContext("if (int *a = (&b))"); 5847 verifyIndependentOfContext("while (int *a = &b)"); 5848 verifyIndependentOfContext("size = sizeof *a;"); 5849 verifyIndependentOfContext("if (a && (b = c))"); 5850 verifyFormat("void f() {\n" 5851 " for (const int &v : Values) {\n" 5852 " }\n" 5853 "}"); 5854 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); 5855 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); 5856 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}"); 5857 5858 verifyFormat("#define A (!a * b)"); 5859 verifyFormat("#define MACRO \\\n" 5860 " int *i = a * b; \\\n" 5861 " void f(a *b);", 5862 getLLVMStyleWithColumns(19)); 5863 5864 verifyIndependentOfContext("A = new SomeType *[Length];"); 5865 verifyIndependentOfContext("A = new SomeType *[Length]();"); 5866 verifyIndependentOfContext("T **t = new T *;"); 5867 verifyIndependentOfContext("T **t = new T *();"); 5868 verifyGoogleFormat("A = new SomeType*[Length]();"); 5869 verifyGoogleFormat("A = new SomeType*[Length];"); 5870 verifyGoogleFormat("T** t = new T*;"); 5871 verifyGoogleFormat("T** t = new T*();"); 5872 5873 verifyFormat("STATIC_ASSERT((a & b) == 0);"); 5874 verifyFormat("STATIC_ASSERT(0 == (a & b));"); 5875 verifyFormat("template <bool a, bool b> " 5876 "typename t::if<x && y>::type f() {}"); 5877 verifyFormat("template <int *y> f() {}"); 5878 verifyFormat("vector<int *> v;"); 5879 verifyFormat("vector<int *const> v;"); 5880 verifyFormat("vector<int *const **const *> v;"); 5881 verifyFormat("vector<int *volatile> v;"); 5882 verifyFormat("vector<a * b> v;"); 5883 verifyFormat("foo<b && false>();"); 5884 verifyFormat("foo<b & 1>();"); 5885 verifyFormat("decltype(*::std::declval<const T &>()) void F();"); 5886 verifyFormat( 5887 "template <class T, class = typename std::enable_if<\n" 5888 " std::is_integral<T>::value &&\n" 5889 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n" 5890 "void F();", 5891 getLLVMStyleWithColumns(70)); 5892 verifyFormat( 5893 "template <class T,\n" 5894 " class = typename std::enable_if<\n" 5895 " std::is_integral<T>::value &&\n" 5896 " (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n" 5897 " class U>\n" 5898 "void F();", 5899 getLLVMStyleWithColumns(70)); 5900 verifyFormat( 5901 "template <class T,\n" 5902 " class = typename ::std::enable_if<\n" 5903 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n" 5904 "void F();", 5905 getGoogleStyleWithColumns(68)); 5906 5907 verifyIndependentOfContext("MACRO(int *i);"); 5908 verifyIndependentOfContext("MACRO(auto *a);"); 5909 verifyIndependentOfContext("MACRO(const A *a);"); 5910 verifyIndependentOfContext("MACRO(A *const a);"); 5911 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');"); 5912 verifyFormat("void f() { f(float{1}, a * a); }"); 5913 // FIXME: Is there a way to make this work? 5914 // verifyIndependentOfContext("MACRO(A *a);"); 5915 5916 verifyFormat("DatumHandle const *operator->() const { return input_; }"); 5917 verifyFormat("return options != nullptr && operator==(*options);"); 5918 5919 EXPECT_EQ("#define OP(x) \\\n" 5920 " ostream &operator<<(ostream &s, const A &a) { \\\n" 5921 " return s << a.DebugString(); \\\n" 5922 " }", 5923 format("#define OP(x) \\\n" 5924 " ostream &operator<<(ostream &s, const A &a) { \\\n" 5925 " return s << a.DebugString(); \\\n" 5926 " }", 5927 getLLVMStyleWithColumns(50))); 5928 5929 // FIXME: We cannot handle this case yet; we might be able to figure out that 5930 // foo<x> d > v; doesn't make sense. 5931 verifyFormat("foo<a<b && c> d> v;"); 5932 5933 FormatStyle PointerMiddle = getLLVMStyle(); 5934 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 5935 verifyFormat("delete *x;", PointerMiddle); 5936 verifyFormat("int * x;", PointerMiddle); 5937 verifyFormat("template <int * y> f() {}", PointerMiddle); 5938 verifyFormat("int * f(int * a) {}", PointerMiddle); 5939 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle); 5940 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle); 5941 verifyFormat("A<int *> a;", PointerMiddle); 5942 verifyFormat("A<int **> a;", PointerMiddle); 5943 verifyFormat("A<int *, int *> a;", PointerMiddle); 5944 verifyFormat("A<int * []> a;", PointerMiddle); 5945 verifyFormat("A = new SomeType *[Length]();", PointerMiddle); 5946 verifyFormat("A = new SomeType *[Length];", PointerMiddle); 5947 verifyFormat("T ** t = new T *;", PointerMiddle); 5948 5949 // Member function reference qualifiers aren't binary operators. 5950 verifyFormat("string // break\n" 5951 "operator()() & {}"); 5952 verifyFormat("string // break\n" 5953 "operator()() && {}"); 5954 verifyGoogleFormat("template <typename T>\n" 5955 "auto x() & -> int {}"); 5956 } 5957 5958 TEST_F(FormatTest, UnderstandsAttributes) { 5959 verifyFormat("SomeType s __attribute__((unused)) (InitValue);"); 5960 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n" 5961 "aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 5962 FormatStyle AfterType = getLLVMStyle(); 5963 AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 5964 verifyFormat("__attribute__((nodebug)) void\n" 5965 "foo() {}\n", 5966 AfterType); 5967 } 5968 5969 TEST_F(FormatTest, UnderstandsEllipsis) { 5970 verifyFormat("int printf(const char *fmt, ...);"); 5971 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); 5972 verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}"); 5973 5974 FormatStyle PointersLeft = getLLVMStyle(); 5975 PointersLeft.PointerAlignment = FormatStyle::PAS_Left; 5976 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft); 5977 } 5978 5979 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { 5980 EXPECT_EQ("int *a;\n" 5981 "int *a;\n" 5982 "int *a;", 5983 format("int *a;\n" 5984 "int* a;\n" 5985 "int *a;", 5986 getGoogleStyle())); 5987 EXPECT_EQ("int* a;\n" 5988 "int* a;\n" 5989 "int* a;", 5990 format("int* a;\n" 5991 "int* a;\n" 5992 "int *a;", 5993 getGoogleStyle())); 5994 EXPECT_EQ("int *a;\n" 5995 "int *a;\n" 5996 "int *a;", 5997 format("int *a;\n" 5998 "int * a;\n" 5999 "int * a;", 6000 getGoogleStyle())); 6001 EXPECT_EQ("auto x = [] {\n" 6002 " int *a;\n" 6003 " int *a;\n" 6004 " int *a;\n" 6005 "};", 6006 format("auto x=[]{int *a;\n" 6007 "int * a;\n" 6008 "int * a;};", 6009 getGoogleStyle())); 6010 } 6011 6012 TEST_F(FormatTest, UnderstandsRvalueReferences) { 6013 verifyFormat("int f(int &&a) {}"); 6014 verifyFormat("int f(int a, char &&b) {}"); 6015 verifyFormat("void f() { int &&a = b; }"); 6016 verifyGoogleFormat("int f(int a, char&& b) {}"); 6017 verifyGoogleFormat("void f() { int&& a = b; }"); 6018 6019 verifyIndependentOfContext("A<int &&> a;"); 6020 verifyIndependentOfContext("A<int &&, int &&> a;"); 6021 verifyGoogleFormat("A<int&&> a;"); 6022 verifyGoogleFormat("A<int&&, int&&> a;"); 6023 6024 // Not rvalue references: 6025 verifyFormat("template <bool B, bool C> class A {\n" 6026 " static_assert(B && C, \"Something is wrong\");\n" 6027 "};"); 6028 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))"); 6029 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))"); 6030 verifyFormat("#define A(a, b) (a && b)"); 6031 } 6032 6033 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 6034 verifyFormat("void f() {\n" 6035 " x[aaaaaaaaa -\n" 6036 " b] = 23;\n" 6037 "}", 6038 getLLVMStyleWithColumns(15)); 6039 } 6040 6041 TEST_F(FormatTest, FormatsCasts) { 6042 verifyFormat("Type *A = static_cast<Type *>(P);"); 6043 verifyFormat("Type *A = (Type *)P;"); 6044 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 6045 verifyFormat("int a = (int)(2.0f);"); 6046 verifyFormat("int a = (int)2.0f;"); 6047 verifyFormat("x[(int32)y];"); 6048 verifyFormat("x = (int32)y;"); 6049 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)"); 6050 verifyFormat("int a = (int)*b;"); 6051 verifyFormat("int a = (int)2.0f;"); 6052 verifyFormat("int a = (int)~0;"); 6053 verifyFormat("int a = (int)++a;"); 6054 verifyFormat("int a = (int)sizeof(int);"); 6055 verifyFormat("int a = (int)+2;"); 6056 verifyFormat("my_int a = (my_int)2.0f;"); 6057 verifyFormat("my_int a = (my_int)sizeof(int);"); 6058 verifyFormat("return (my_int)aaa;"); 6059 verifyFormat("#define x ((int)-1)"); 6060 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1"); 6061 verifyFormat("#define p(q) ((int *)&q)"); 6062 verifyFormat("fn(a)(b) + 1;"); 6063 6064 verifyFormat("void f() { my_int a = (my_int)*b; }"); 6065 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }"); 6066 verifyFormat("my_int a = (my_int)~0;"); 6067 verifyFormat("my_int a = (my_int)++a;"); 6068 verifyFormat("my_int a = (my_int)-2;"); 6069 verifyFormat("my_int a = (my_int)1;"); 6070 verifyFormat("my_int a = (my_int *)1;"); 6071 verifyFormat("my_int a = (const my_int)-1;"); 6072 verifyFormat("my_int a = (const my_int *)-1;"); 6073 verifyFormat("my_int a = (my_int)(my_int)-1;"); 6074 verifyFormat("my_int a = (ns::my_int)-2;"); 6075 verifyFormat("case (my_int)ONE:"); 6076 verifyFormat("auto x = (X)this;"); 6077 6078 // FIXME: single value wrapped with paren will be treated as cast. 6079 verifyFormat("void f(int i = (kValue)*kMask) {}"); 6080 6081 verifyFormat("{ (void)F; }"); 6082 6083 // Don't break after a cast's 6084 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 6085 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n" 6086 " bbbbbbbbbbbbbbbbbbbbbb);"); 6087 6088 // These are not casts. 6089 verifyFormat("void f(int *) {}"); 6090 verifyFormat("f(foo)->b;"); 6091 verifyFormat("f(foo).b;"); 6092 verifyFormat("f(foo)(b);"); 6093 verifyFormat("f(foo)[b];"); 6094 verifyFormat("[](foo) { return 4; }(bar);"); 6095 verifyFormat("(*funptr)(foo)[4];"); 6096 verifyFormat("funptrs[4](foo)[4];"); 6097 verifyFormat("void f(int *);"); 6098 verifyFormat("void f(int *) = 0;"); 6099 verifyFormat("void f(SmallVector<int>) {}"); 6100 verifyFormat("void f(SmallVector<int>);"); 6101 verifyFormat("void f(SmallVector<int>) = 0;"); 6102 verifyFormat("void f(int i = (kA * kB) & kMask) {}"); 6103 verifyFormat("int a = sizeof(int) * b;"); 6104 verifyFormat("int a = alignof(int) * b;", getGoogleStyle()); 6105 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;"); 6106 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");"); 6107 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;"); 6108 6109 // These are not casts, but at some point were confused with casts. 6110 verifyFormat("virtual void foo(int *) override;"); 6111 verifyFormat("virtual void foo(char &) const;"); 6112 verifyFormat("virtual void foo(int *a, char *) const;"); 6113 verifyFormat("int a = sizeof(int *) + b;"); 6114 verifyFormat("int a = alignof(int *) + b;", getGoogleStyle()); 6115 verifyFormat("bool b = f(g<int>) && c;"); 6116 verifyFormat("typedef void (*f)(int i) func;"); 6117 6118 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n" 6119 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 6120 // FIXME: The indentation here is not ideal. 6121 verifyFormat( 6122 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6123 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n" 6124 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];"); 6125 } 6126 6127 TEST_F(FormatTest, FormatsFunctionTypes) { 6128 verifyFormat("A<bool()> a;"); 6129 verifyFormat("A<SomeType()> a;"); 6130 verifyFormat("A<void (*)(int, std::string)> a;"); 6131 verifyFormat("A<void *(int)>;"); 6132 verifyFormat("void *(*a)(int *, SomeType *);"); 6133 verifyFormat("int (*func)(void *);"); 6134 verifyFormat("void f() { int (*func)(void *); }"); 6135 verifyFormat("template <class CallbackClass>\n" 6136 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);"); 6137 6138 verifyGoogleFormat("A<void*(int*, SomeType*)>;"); 6139 verifyGoogleFormat("void* (*a)(int);"); 6140 verifyGoogleFormat( 6141 "template <class CallbackClass>\n" 6142 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);"); 6143 6144 // Other constructs can look somewhat like function types: 6145 verifyFormat("A<sizeof(*x)> a;"); 6146 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)"); 6147 verifyFormat("some_var = function(*some_pointer_var)[0];"); 6148 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }"); 6149 verifyFormat("int x = f(&h)();"); 6150 verifyFormat("returnsFunction(¶m1, ¶m2)(param);"); 6151 verifyFormat("std::function<\n" 6152 " LooooooooooongTemplatedType<\n" 6153 " SomeType>*(\n" 6154 " LooooooooooooooooongType type)>\n" 6155 " function;", 6156 getGoogleStyleWithColumns(40)); 6157 } 6158 6159 TEST_F(FormatTest, FormatsPointersToArrayTypes) { 6160 verifyFormat("A (*foo_)[6];"); 6161 verifyFormat("vector<int> (*foo_)[6];"); 6162 } 6163 6164 TEST_F(FormatTest, BreaksLongVariableDeclarations) { 6165 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6166 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6167 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n" 6168 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6169 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6170 " *LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6171 6172 // Different ways of ()-initializiation. 6173 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6174 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);"); 6175 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6176 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);"); 6177 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6178 " LoooooooooooooooooooooooooooooooooooooooongVariable({});"); 6179 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6180 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);"); 6181 6182 // Lambdas should not confuse the variable declaration heuristic. 6183 verifyFormat("LooooooooooooooooongType\n" 6184 " variable(nullptr, [](A *a) {});", 6185 getLLVMStyleWithColumns(40)); 6186 } 6187 6188 TEST_F(FormatTest, BreaksLongDeclarations) { 6189 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n" 6190 " AnotherNameForTheLongType;"); 6191 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n" 6192 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 6193 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6194 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 6195 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n" 6196 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 6197 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6198 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6199 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n" 6200 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6201 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 6202 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6203 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 6204 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6205 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6206 "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);"); 6207 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6208 "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}"); 6209 FormatStyle Indented = getLLVMStyle(); 6210 Indented.IndentWrappedFunctionNames = true; 6211 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6212 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();", 6213 Indented); 6214 verifyFormat( 6215 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6216 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6217 Indented); 6218 verifyFormat( 6219 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 6220 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6221 Indented); 6222 verifyFormat( 6223 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 6224 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6225 Indented); 6226 6227 // FIXME: Without the comment, this breaks after "(". 6228 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n" 6229 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();", 6230 getGoogleStyle()); 6231 6232 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 6233 " int LoooooooooooooooooooongParam2) {}"); 6234 verifyFormat( 6235 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" 6236 " SourceLocation L, IdentifierIn *II,\n" 6237 " Type *T) {}"); 6238 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" 6239 "ReallyReaaallyLongFunctionName(\n" 6240 " const std::string &SomeParameter,\n" 6241 " const SomeType<string, SomeOtherTemplateParameter>\n" 6242 " &ReallyReallyLongParameterName,\n" 6243 " const SomeType<string, SomeOtherTemplateParameter>\n" 6244 " &AnotherLongParameterName) {}"); 6245 verifyFormat("template <typename A>\n" 6246 "SomeLoooooooooooooooooooooongType<\n" 6247 " typename some_namespace::SomeOtherType<A>::Type>\n" 6248 "Function() {}"); 6249 6250 verifyGoogleFormat( 6251 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" 6252 " aaaaaaaaaaaaaaaaaaaaaaa;"); 6253 verifyGoogleFormat( 6254 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" 6255 " SourceLocation L) {}"); 6256 verifyGoogleFormat( 6257 "some_namespace::LongReturnType\n" 6258 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" 6259 " int first_long_parameter, int second_parameter) {}"); 6260 6261 verifyGoogleFormat("template <typename T>\n" 6262 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" 6263 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); 6264 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6265 " int aaaaaaaaaaaaaaaaaaaaaaa);"); 6266 6267 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 6268 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6269 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6270 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6271 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 6272 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 6273 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6274 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 6275 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n" 6276 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6277 6278 verifyFormat("template <typename T> // Templates on own line.\n" 6279 "static int // Some comment.\n" 6280 "MyFunction(int a);", 6281 getLLVMStyle()); 6282 } 6283 6284 TEST_F(FormatTest, FormatsArrays) { 6285 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6286 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;"); 6287 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n" 6288 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;"); 6289 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n" 6290 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}"); 6291 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6292 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 6293 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6294 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;"); 6295 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6296 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6297 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 6298 verifyFormat( 6299 "llvm::outs() << \"aaaaaaaaaaaa: \"\n" 6300 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6301 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 6302 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n" 6303 " .aaaaaaaaaaaaaaaaaaaaaa();"); 6304 6305 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n" 6306 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];"); 6307 verifyFormat( 6308 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n" 6309 " .aaaaaaa[0]\n" 6310 " .aaaaaaaaaaaaaaaaaaaaaa();"); 6311 verifyFormat("a[::b::c];"); 6312 6313 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10)); 6314 6315 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0); 6316 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit); 6317 } 6318 6319 TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 6320 verifyFormat("(a)->b();"); 6321 verifyFormat("--a;"); 6322 } 6323 6324 TEST_F(FormatTest, HandlesIncludeDirectives) { 6325 verifyFormat("#include <string>\n" 6326 "#include <a/b/c.h>\n" 6327 "#include \"a/b/string\"\n" 6328 "#include \"string.h\"\n" 6329 "#include \"string.h\"\n" 6330 "#include <a-a>\n" 6331 "#include < path with space >\n" 6332 "#include_next <test.h>" 6333 "#include \"abc.h\" // this is included for ABC\n" 6334 "#include \"some long include\" // with a comment\n" 6335 "#include \"some very long include path\"\n" 6336 "#include <some/very/long/include/path>\n", 6337 getLLVMStyleWithColumns(35)); 6338 EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\"")); 6339 EXPECT_EQ("#include <a>", format("#include<a>")); 6340 6341 verifyFormat("#import <string>"); 6342 verifyFormat("#import <a/b/c.h>"); 6343 verifyFormat("#import \"a/b/string\""); 6344 verifyFormat("#import \"string.h\""); 6345 verifyFormat("#import \"string.h\""); 6346 verifyFormat("#if __has_include(<strstream>)\n" 6347 "#include <strstream>\n" 6348 "#endif"); 6349 6350 verifyFormat("#define MY_IMPORT <a/b>"); 6351 6352 verifyFormat("#if __has_include(<a/b>)"); 6353 verifyFormat("#if __has_include_next(<a/b>)"); 6354 verifyFormat("#define F __has_include(<a/b>)"); 6355 verifyFormat("#define F __has_include_next(<a/b>)"); 6356 6357 // Protocol buffer definition or missing "#". 6358 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";", 6359 getLLVMStyleWithColumns(30)); 6360 6361 FormatStyle Style = getLLVMStyle(); 6362 Style.AlwaysBreakBeforeMultilineStrings = true; 6363 Style.ColumnLimit = 0; 6364 verifyFormat("#import \"abc.h\"", Style); 6365 6366 // But 'import' might also be a regular C++ namespace. 6367 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6368 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6369 } 6370 6371 //===----------------------------------------------------------------------===// 6372 // Error recovery tests. 6373 //===----------------------------------------------------------------------===// 6374 6375 TEST_F(FormatTest, IncompleteParameterLists) { 6376 FormatStyle NoBinPacking = getLLVMStyle(); 6377 NoBinPacking.BinPackParameters = false; 6378 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" 6379 " double *min_x,\n" 6380 " double *max_x,\n" 6381 " double *min_y,\n" 6382 " double *max_y,\n" 6383 " double *min_z,\n" 6384 " double *max_z, ) {}", 6385 NoBinPacking); 6386 } 6387 6388 TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 6389 verifyFormat("void f() { return; }\n42"); 6390 verifyFormat("void f() {\n" 6391 " if (0)\n" 6392 " return;\n" 6393 "}\n" 6394 "42"); 6395 verifyFormat("void f() { return }\n42"); 6396 verifyFormat("void f() {\n" 6397 " if (0)\n" 6398 " return\n" 6399 "}\n" 6400 "42"); 6401 } 6402 6403 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 6404 EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); 6405 EXPECT_EQ("void f() {\n" 6406 " if (a)\n" 6407 " return\n" 6408 "}", 6409 format("void f ( ) { if ( a ) return }")); 6410 EXPECT_EQ("namespace N {\n" 6411 "void f()\n" 6412 "}", 6413 format("namespace N { void f() }")); 6414 EXPECT_EQ("namespace N {\n" 6415 "void f() {}\n" 6416 "void g()\n" 6417 "} // namespace N", 6418 format("namespace N { void f( ) { } void g( ) }")); 6419 } 6420 6421 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 6422 verifyFormat("int aaaaaaaa =\n" 6423 " // Overlylongcomment\n" 6424 " b;", 6425 getLLVMStyleWithColumns(20)); 6426 verifyFormat("function(\n" 6427 " ShortArgument,\n" 6428 " LoooooooooooongArgument);\n", 6429 getLLVMStyleWithColumns(20)); 6430 } 6431 6432 TEST_F(FormatTest, IncorrectAccessSpecifier) { 6433 verifyFormat("public:"); 6434 verifyFormat("class A {\n" 6435 "public\n" 6436 " void f() {}\n" 6437 "};"); 6438 verifyFormat("public\n" 6439 "int qwerty;"); 6440 verifyFormat("public\n" 6441 "B {}"); 6442 verifyFormat("public\n" 6443 "{}"); 6444 verifyFormat("public\n" 6445 "B { int x; }"); 6446 } 6447 6448 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 6449 verifyFormat("{"); 6450 verifyFormat("#})"); 6451 verifyNoCrash("(/**/[:!] ?[)."); 6452 } 6453 6454 TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 6455 verifyFormat("do {\n}"); 6456 verifyFormat("do {\n}\n" 6457 "f();"); 6458 verifyFormat("do {\n}\n" 6459 "wheeee(fun);"); 6460 verifyFormat("do {\n" 6461 " f();\n" 6462 "}"); 6463 } 6464 6465 TEST_F(FormatTest, IncorrectCodeMissingParens) { 6466 verifyFormat("if {\n foo;\n foo();\n}"); 6467 verifyFormat("switch {\n foo;\n foo();\n}"); 6468 verifyIncompleteFormat("for {\n foo;\n foo();\n}"); 6469 verifyFormat("while {\n foo;\n foo();\n}"); 6470 verifyFormat("do {\n foo;\n foo();\n} while;"); 6471 } 6472 6473 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 6474 verifyIncompleteFormat("namespace {\n" 6475 "class Foo { Foo (\n" 6476 "};\n" 6477 "} // namespace"); 6478 } 6479 6480 TEST_F(FormatTest, IncorrectCodeErrorDetection) { 6481 EXPECT_EQ("{\n {}\n", format("{\n{\n}\n")); 6482 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 6483 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 6484 EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n")); 6485 6486 EXPECT_EQ("{\n" 6487 " {\n" 6488 " breakme(\n" 6489 " qwe);\n" 6490 " }\n", 6491 format("{\n" 6492 " {\n" 6493 " breakme(qwe);\n" 6494 "}\n", 6495 getLLVMStyleWithColumns(10))); 6496 } 6497 6498 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 6499 verifyFormat("int x = {\n" 6500 " avariable,\n" 6501 " b(alongervariable)};", 6502 getLLVMStyleWithColumns(25)); 6503 } 6504 6505 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 6506 verifyFormat("return (a)(b){1, 2, 3};"); 6507 } 6508 6509 TEST_F(FormatTest, LayoutCxx11BraceInitializers) { 6510 verifyFormat("vector<int> x{1, 2, 3, 4};"); 6511 verifyFormat("vector<int> x{\n" 6512 " 1,\n" 6513 " 2,\n" 6514 " 3,\n" 6515 " 4,\n" 6516 "};"); 6517 verifyFormat("vector<T> x{{}, {}, {}, {}};"); 6518 verifyFormat("f({1, 2});"); 6519 verifyFormat("auto v = Foo{-1};"); 6520 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});"); 6521 verifyFormat("Class::Class : member{1, 2, 3} {}"); 6522 verifyFormat("new vector<int>{1, 2, 3};"); 6523 verifyFormat("new int[3]{1, 2, 3};"); 6524 verifyFormat("new int{1};"); 6525 verifyFormat("return {arg1, arg2};"); 6526 verifyFormat("return {arg1, SomeType{parameter}};"); 6527 verifyFormat("int count = set<int>{f(), g(), h()}.size();"); 6528 verifyFormat("new T{arg1, arg2};"); 6529 verifyFormat("f(MyMap[{composite, key}]);"); 6530 verifyFormat("class Class {\n" 6531 " T member = {arg1, arg2};\n" 6532 "};"); 6533 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};"); 6534 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 6535 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 6536 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");"); 6537 verifyFormat("int a = std::is_integral<int>{} + 0;"); 6538 6539 verifyFormat("int foo(int i) { return fo1{}(i); }"); 6540 verifyFormat("int foo(int i) { return fo1{}(i); }"); 6541 verifyFormat("auto i = decltype(x){};"); 6542 verifyFormat("std::vector<int> v = {1, 0 /* comment */};"); 6543 verifyFormat("Node n{1, Node{1000}, //\n" 6544 " 2};"); 6545 verifyFormat("Aaaa aaaaaaa{\n" 6546 " {\n" 6547 " aaaa,\n" 6548 " },\n" 6549 "};"); 6550 verifyFormat("class C : public D {\n" 6551 " SomeClass SC{2};\n" 6552 "};"); 6553 verifyFormat("class C : public A {\n" 6554 " class D : public B {\n" 6555 " void f() { int i{2}; }\n" 6556 " };\n" 6557 "};"); 6558 verifyFormat("#define A {a, a},"); 6559 6560 // Binpacking only if there is no trailing comma 6561 verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n" 6562 " cccccccccc, dddddddddd};", 6563 getLLVMStyleWithColumns(50)); 6564 verifyFormat("const Aaaaaa aaaaa = {\n" 6565 " aaaaaaaaaaa,\n" 6566 " bbbbbbbbbbb,\n" 6567 " ccccccccccc,\n" 6568 " ddddddddddd,\n" 6569 "};", getLLVMStyleWithColumns(50)); 6570 6571 // Cases where distinguising braced lists and blocks is hard. 6572 verifyFormat("vector<int> v{12} GUARDED_BY(mutex);"); 6573 verifyFormat("void f() {\n" 6574 " return; // comment\n" 6575 "}\n" 6576 "SomeType t;"); 6577 verifyFormat("void f() {\n" 6578 " if (a) {\n" 6579 " f();\n" 6580 " }\n" 6581 "}\n" 6582 "SomeType t;"); 6583 6584 // In combination with BinPackArguments = false. 6585 FormatStyle NoBinPacking = getLLVMStyle(); 6586 NoBinPacking.BinPackArguments = false; 6587 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n" 6588 " bbbbb,\n" 6589 " ccccc,\n" 6590 " ddddd,\n" 6591 " eeeee,\n" 6592 " ffffff,\n" 6593 " ggggg,\n" 6594 " hhhhhh,\n" 6595 " iiiiii,\n" 6596 " jjjjjj,\n" 6597 " kkkkkk};", 6598 NoBinPacking); 6599 verifyFormat("const Aaaaaa aaaaa = {\n" 6600 " aaaaa,\n" 6601 " bbbbb,\n" 6602 " ccccc,\n" 6603 " ddddd,\n" 6604 " eeeee,\n" 6605 " ffffff,\n" 6606 " ggggg,\n" 6607 " hhhhhh,\n" 6608 " iiiiii,\n" 6609 " jjjjjj,\n" 6610 " kkkkkk,\n" 6611 "};", 6612 NoBinPacking); 6613 verifyFormat( 6614 "const Aaaaaa aaaaa = {\n" 6615 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n" 6616 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n" 6617 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n" 6618 "};", 6619 NoBinPacking); 6620 6621 // FIXME: The alignment of these trailing comments might be bad. Then again, 6622 // this might be utterly useless in real code. 6623 verifyFormat("Constructor::Constructor()\n" 6624 " : some_value{ //\n" 6625 " aaaaaaa, //\n" 6626 " bbbbbbb} {}"); 6627 6628 // In braced lists, the first comment is always assumed to belong to the 6629 // first element. Thus, it can be moved to the next or previous line as 6630 // appropriate. 6631 EXPECT_EQ("function({// First element:\n" 6632 " 1,\n" 6633 " // Second element:\n" 6634 " 2});", 6635 format("function({\n" 6636 " // First element:\n" 6637 " 1,\n" 6638 " // Second element:\n" 6639 " 2});")); 6640 EXPECT_EQ("std::vector<int> MyNumbers{\n" 6641 " // First element:\n" 6642 " 1,\n" 6643 " // Second element:\n" 6644 " 2};", 6645 format("std::vector<int> MyNumbers{// First element:\n" 6646 " 1,\n" 6647 " // Second element:\n" 6648 " 2};", 6649 getLLVMStyleWithColumns(30))); 6650 // A trailing comma should still lead to an enforced line break and no 6651 // binpacking. 6652 EXPECT_EQ("vector<int> SomeVector = {\n" 6653 " // aaa\n" 6654 " 1,\n" 6655 " 2,\n" 6656 "};", 6657 format("vector<int> SomeVector = { // aaa\n" 6658 " 1, 2, };")); 6659 6660 FormatStyle ExtraSpaces = getLLVMStyle(); 6661 ExtraSpaces.Cpp11BracedListStyle = false; 6662 ExtraSpaces.ColumnLimit = 75; 6663 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces); 6664 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces); 6665 verifyFormat("f({ 1, 2 });", ExtraSpaces); 6666 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces); 6667 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces); 6668 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces); 6669 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces); 6670 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces); 6671 verifyFormat("return { arg1, arg2 };", ExtraSpaces); 6672 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces); 6673 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces); 6674 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces); 6675 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces); 6676 verifyFormat("class Class {\n" 6677 " T member = { arg1, arg2 };\n" 6678 "};", 6679 ExtraSpaces); 6680 verifyFormat( 6681 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6682 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n" 6683 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 6684 " bbbbbbbbbbbbbbbbbbbb, bbbbb };", 6685 ExtraSpaces); 6686 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces); 6687 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });", 6688 ExtraSpaces); 6689 verifyFormat( 6690 "someFunction(OtherParam,\n" 6691 " BracedList{ // comment 1 (Forcing interesting break)\n" 6692 " param1, param2,\n" 6693 " // comment 2\n" 6694 " param3, param4 });", 6695 ExtraSpaces); 6696 verifyFormat( 6697 "std::this_thread::sleep_for(\n" 6698 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);", 6699 ExtraSpaces); 6700 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n" 6701 " aaaaaaa,\n" 6702 " aaaaaaaaaa,\n" 6703 " aaaaa,\n" 6704 " aaaaaaaaaaaaaaa,\n" 6705 " aaa,\n" 6706 " aaaaaaaaaa,\n" 6707 " a,\n" 6708 " aaaaaaaaaaaaaaaaaaaaa,\n" 6709 " aaaaaaaaaaaa,\n" 6710 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n" 6711 " aaaaaaa,\n" 6712 " a};"); 6713 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces); 6714 verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces); 6715 verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces); 6716 } 6717 6718 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { 6719 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6720 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6721 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6722 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6723 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6724 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 6725 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n" 6726 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6727 " 1, 22, 333, 4444, 55555, //\n" 6728 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6729 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 6730 verifyFormat( 6731 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6732 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6733 " 1, 22, 333, 4444, 55555, 666666, // comment\n" 6734 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6735 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6736 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6737 " 7777777};"); 6738 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6739 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6740 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6741 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6742 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6743 " // Separating comment.\n" 6744 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6745 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6746 " // Leading comment\n" 6747 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6748 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6749 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6750 " 1, 1, 1, 1};", 6751 getLLVMStyleWithColumns(39)); 6752 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6753 " 1, 1, 1, 1};", 6754 getLLVMStyleWithColumns(38)); 6755 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n" 6756 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};", 6757 getLLVMStyleWithColumns(43)); 6758 verifyFormat( 6759 "static unsigned SomeValues[10][3] = {\n" 6760 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n" 6761 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};"); 6762 verifyFormat("static auto fields = new vector<string>{\n" 6763 " \"aaaaaaaaaaaaa\",\n" 6764 " \"aaaaaaaaaaaaa\",\n" 6765 " \"aaaaaaaaaaaa\",\n" 6766 " \"aaaaaaaaaaaaaa\",\n" 6767 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 6768 " \"aaaaaaaaaaaa\",\n" 6769 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 6770 "};"); 6771 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};"); 6772 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n" 6773 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n" 6774 " 3, cccccccccccccccccccccc};", 6775 getLLVMStyleWithColumns(60)); 6776 6777 // Trailing commas. 6778 verifyFormat("vector<int> x = {\n" 6779 " 1, 1, 1, 1, 1, 1, 1, 1,\n" 6780 "};", 6781 getLLVMStyleWithColumns(39)); 6782 verifyFormat("vector<int> x = {\n" 6783 " 1, 1, 1, 1, 1, 1, 1, 1, //\n" 6784 "};", 6785 getLLVMStyleWithColumns(39)); 6786 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6787 " 1, 1, 1, 1,\n" 6788 " /**/ /**/};", 6789 getLLVMStyleWithColumns(39)); 6790 6791 // Trailing comment in the first line. 6792 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n" 6793 " 1111111111, 2222222222, 33333333333, 4444444444, //\n" 6794 " 111111111, 222222222, 3333333333, 444444444, //\n" 6795 " 11111111, 22222222, 333333333, 44444444};"); 6796 // Trailing comment in the last line. 6797 verifyFormat("int aaaaa[] = {\n" 6798 " 1, 2, 3, // comment\n" 6799 " 4, 5, 6 // comment\n" 6800 "};"); 6801 6802 // With nested lists, we should either format one item per line or all nested 6803 // lists one on line. 6804 // FIXME: For some nested lists, we can do better. 6805 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n" 6806 " {aaaaaaaaaaaaaaaaaaa},\n" 6807 " {aaaaaaaaaaaaaaaaaaaaa},\n" 6808 " {aaaaaaaaaaaaaaaaa}};", 6809 getLLVMStyleWithColumns(60)); 6810 verifyFormat( 6811 "SomeStruct my_struct_array = {\n" 6812 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n" 6813 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n" 6814 " {aaa, aaa},\n" 6815 " {aaa, aaa},\n" 6816 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n" 6817 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6818 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};"); 6819 6820 // No column layout should be used here. 6821 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n" 6822 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};"); 6823 6824 verifyNoCrash("a<,"); 6825 6826 // No braced initializer here. 6827 verifyFormat("void f() {\n" 6828 " struct Dummy {};\n" 6829 " f(v);\n" 6830 "}"); 6831 6832 // Long lists should be formatted in columns even if they are nested. 6833 verifyFormat( 6834 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6835 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6836 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6837 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6838 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6839 " 1, 22, 333, 4444, 55555, 666666, 7777777});"); 6840 6841 // Allow "single-column" layout even if that violates the column limit. There 6842 // isn't going to be a better way. 6843 verifyFormat("std::vector<int> a = {\n" 6844 " aaaaaaaa,\n" 6845 " aaaaaaaa,\n" 6846 " aaaaaaaa,\n" 6847 " aaaaaaaa,\n" 6848 " aaaaaaaaaa,\n" 6849 " aaaaaaaa,\n" 6850 " aaaaaaaaaaaaaaaaaaaaaaaaaaa};", 6851 getLLVMStyleWithColumns(30)); 6852 verifyFormat("vector<int> aaaa = {\n" 6853 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6854 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6855 " aaaaaa.aaaaaaa,\n" 6856 " aaaaaa.aaaaaaa,\n" 6857 " aaaaaa.aaaaaaa,\n" 6858 " aaaaaa.aaaaaaa,\n" 6859 "};"); 6860 6861 // Don't create hanging lists. 6862 verifyFormat("someFunction(Param, {List1, List2,\n" 6863 " List3});", 6864 getLLVMStyleWithColumns(35)); 6865 verifyFormat("someFunction(Param, Param,\n" 6866 " {List1, List2,\n" 6867 " List3});", 6868 getLLVMStyleWithColumns(35)); 6869 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n" 6870 " aaaaaaaaaaaaaaaaaaaaaaa);"); 6871 } 6872 6873 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 6874 FormatStyle DoNotMerge = getLLVMStyle(); 6875 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 6876 6877 verifyFormat("void f() { return 42; }"); 6878 verifyFormat("void f() {\n" 6879 " return 42;\n" 6880 "}", 6881 DoNotMerge); 6882 verifyFormat("void f() {\n" 6883 " // Comment\n" 6884 "}"); 6885 verifyFormat("{\n" 6886 "#error {\n" 6887 " int a;\n" 6888 "}"); 6889 verifyFormat("{\n" 6890 " int a;\n" 6891 "#error {\n" 6892 "}"); 6893 verifyFormat("void f() {} // comment"); 6894 verifyFormat("void f() { int a; } // comment"); 6895 verifyFormat("void f() {\n" 6896 "} // comment", 6897 DoNotMerge); 6898 verifyFormat("void f() {\n" 6899 " int a;\n" 6900 "} // comment", 6901 DoNotMerge); 6902 verifyFormat("void f() {\n" 6903 "} // comment", 6904 getLLVMStyleWithColumns(15)); 6905 6906 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 6907 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 6908 6909 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 6910 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 6911 verifyFormat("class C {\n" 6912 " C()\n" 6913 " : iiiiiiii(nullptr),\n" 6914 " kkkkkkk(nullptr),\n" 6915 " mmmmmmm(nullptr),\n" 6916 " nnnnnnn(nullptr) {}\n" 6917 "};", 6918 getGoogleStyle()); 6919 6920 FormatStyle NoColumnLimit = getLLVMStyle(); 6921 NoColumnLimit.ColumnLimit = 0; 6922 EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit)); 6923 EXPECT_EQ("class C {\n" 6924 " A() : b(0) {}\n" 6925 "};", 6926 format("class C{A():b(0){}};", NoColumnLimit)); 6927 EXPECT_EQ("A()\n" 6928 " : b(0) {\n" 6929 "}", 6930 format("A()\n:b(0)\n{\n}", NoColumnLimit)); 6931 6932 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit; 6933 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine = 6934 FormatStyle::SFS_None; 6935 EXPECT_EQ("A()\n" 6936 " : b(0) {\n" 6937 "}", 6938 format("A():b(0){}", DoNotMergeNoColumnLimit)); 6939 EXPECT_EQ("A()\n" 6940 " : b(0) {\n" 6941 "}", 6942 format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit)); 6943 6944 verifyFormat("#define A \\\n" 6945 " void f() { \\\n" 6946 " int i; \\\n" 6947 " }", 6948 getLLVMStyleWithColumns(20)); 6949 verifyFormat("#define A \\\n" 6950 " void f() { int i; }", 6951 getLLVMStyleWithColumns(21)); 6952 verifyFormat("#define A \\\n" 6953 " void f() { \\\n" 6954 " int i; \\\n" 6955 " } \\\n" 6956 " int j;", 6957 getLLVMStyleWithColumns(22)); 6958 verifyFormat("#define A \\\n" 6959 " void f() { int i; } \\\n" 6960 " int j;", 6961 getLLVMStyleWithColumns(23)); 6962 } 6963 6964 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) { 6965 FormatStyle MergeEmptyOnly = getLLVMStyle(); 6966 MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 6967 verifyFormat("class C {\n" 6968 " int f() {}\n" 6969 "};", 6970 MergeEmptyOnly); 6971 verifyFormat("class C {\n" 6972 " int f() {\n" 6973 " return 42;\n" 6974 " }\n" 6975 "};", 6976 MergeEmptyOnly); 6977 verifyFormat("int f() {}", MergeEmptyOnly); 6978 verifyFormat("int f() {\n" 6979 " return 42;\n" 6980 "}", 6981 MergeEmptyOnly); 6982 6983 // Also verify behavior when BraceWrapping.AfterFunction = true 6984 MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 6985 MergeEmptyOnly.BraceWrapping.AfterFunction = true; 6986 verifyFormat("int f() {}", MergeEmptyOnly); 6987 verifyFormat("class C {\n" 6988 " int f() {}\n" 6989 "};", 6990 MergeEmptyOnly); 6991 } 6992 6993 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) { 6994 FormatStyle MergeInlineOnly = getLLVMStyle(); 6995 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 6996 verifyFormat("class C {\n" 6997 " int f() { return 42; }\n" 6998 "};", 6999 MergeInlineOnly); 7000 verifyFormat("int f() {\n" 7001 " return 42;\n" 7002 "}", 7003 MergeInlineOnly); 7004 7005 // SFS_Inline implies SFS_Empty 7006 verifyFormat("class C {\n" 7007 " int f() {}\n" 7008 "};", 7009 MergeInlineOnly); 7010 verifyFormat("int f() {}", MergeInlineOnly); 7011 7012 // Also verify behavior when BraceWrapping.AfterFunction = true 7013 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 7014 MergeInlineOnly.BraceWrapping.AfterFunction = true; 7015 verifyFormat("class C {\n" 7016 " int f() { return 42; }\n" 7017 "};", 7018 MergeInlineOnly); 7019 verifyFormat("int f()\n" 7020 "{\n" 7021 " return 42;\n" 7022 "}", 7023 MergeInlineOnly); 7024 7025 // SFS_Inline implies SFS_Empty 7026 verifyFormat("int f() {}", MergeInlineOnly); 7027 verifyFormat("class C {\n" 7028 " int f() {}\n" 7029 "};", 7030 MergeInlineOnly); 7031 } 7032 7033 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) { 7034 FormatStyle MergeInlineOnly = getLLVMStyle(); 7035 MergeInlineOnly.AllowShortFunctionsOnASingleLine = 7036 FormatStyle::SFS_InlineOnly; 7037 verifyFormat("class C {\n" 7038 " int f() { return 42; }\n" 7039 "};", 7040 MergeInlineOnly); 7041 verifyFormat("int f() {\n" 7042 " return 42;\n" 7043 "}", 7044 MergeInlineOnly); 7045 7046 // SFS_InlineOnly does not imply SFS_Empty 7047 verifyFormat("class C {\n" 7048 " int f() {}\n" 7049 "};", 7050 MergeInlineOnly); 7051 verifyFormat("int f() {\n" 7052 "}", 7053 MergeInlineOnly); 7054 7055 // Also verify behavior when BraceWrapping.AfterFunction = true 7056 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 7057 MergeInlineOnly.BraceWrapping.AfterFunction = true; 7058 verifyFormat("class C {\n" 7059 " int f() { return 42; }\n" 7060 "};", 7061 MergeInlineOnly); 7062 verifyFormat("int f()\n" 7063 "{\n" 7064 " return 42;\n" 7065 "}", 7066 MergeInlineOnly); 7067 7068 // SFS_InlineOnly does not imply SFS_Empty 7069 verifyFormat("int f()\n" 7070 "{\n" 7071 "}", 7072 MergeInlineOnly); 7073 verifyFormat("class C {\n" 7074 " int f() {}\n" 7075 "};", 7076 MergeInlineOnly); 7077 } 7078 7079 TEST_F(FormatTest, SplitEmptyFunction) { 7080 FormatStyle Style = getLLVMStyle(); 7081 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 7082 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7083 Style.BraceWrapping.AfterFunction = true; 7084 Style.BraceWrapping.SplitEmptyFunction = false; 7085 Style.ColumnLimit = 40; 7086 7087 verifyFormat("int f()\n" 7088 "{}", 7089 Style); 7090 verifyFormat("int f()\n" 7091 "{\n" 7092 " return 42;\n" 7093 "}", 7094 Style); 7095 verifyFormat("int f()\n" 7096 "{\n" 7097 " // some comment\n" 7098 "}", 7099 Style); 7100 7101 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 7102 verifyFormat("int f() {}", Style); 7103 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 7104 "{}", 7105 Style); 7106 verifyFormat("int f()\n" 7107 "{\n" 7108 " return 0;\n" 7109 "}", 7110 Style); 7111 7112 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 7113 verifyFormat("class Foo {\n" 7114 " int f() {}\n" 7115 "};\n", 7116 Style); 7117 verifyFormat("class Foo {\n" 7118 " int f() { return 0; }\n" 7119 "};\n", 7120 Style); 7121 verifyFormat("class Foo {\n" 7122 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 7123 " {}\n" 7124 "};\n", 7125 Style); 7126 verifyFormat("class Foo {\n" 7127 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 7128 " {\n" 7129 " return 0;\n" 7130 " }\n" 7131 "};\n", 7132 Style); 7133 7134 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 7135 verifyFormat("int f() {}", Style); 7136 verifyFormat("int f() { return 0; }", Style); 7137 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 7138 "{}", 7139 Style); 7140 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 7141 "{\n" 7142 " return 0;\n" 7143 "}", 7144 Style); 7145 } 7146 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) { 7147 FormatStyle Style = getLLVMStyle(); 7148 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 7149 verifyFormat("#ifdef A\n" 7150 "int f() {}\n" 7151 "#else\n" 7152 "int g() {}\n" 7153 "#endif", 7154 Style); 7155 } 7156 7157 TEST_F(FormatTest, SplitEmptyClass) { 7158 FormatStyle Style = getLLVMStyle(); 7159 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7160 Style.BraceWrapping.AfterClass = true; 7161 Style.BraceWrapping.SplitEmptyRecord = false; 7162 7163 verifyFormat("class Foo\n" 7164 "{};", 7165 Style); 7166 verifyFormat("/* something */ class Foo\n" 7167 "{};", 7168 Style); 7169 verifyFormat("template <typename X> class Foo\n" 7170 "{};", 7171 Style); 7172 verifyFormat("class Foo\n" 7173 "{\n" 7174 " Foo();\n" 7175 "};", 7176 Style); 7177 verifyFormat("typedef class Foo\n" 7178 "{\n" 7179 "} Foo_t;", 7180 Style); 7181 } 7182 7183 TEST_F(FormatTest, SplitEmptyStruct) { 7184 FormatStyle Style = getLLVMStyle(); 7185 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7186 Style.BraceWrapping.AfterStruct = true; 7187 Style.BraceWrapping.SplitEmptyRecord = false; 7188 7189 verifyFormat("struct Foo\n" 7190 "{};", 7191 Style); 7192 verifyFormat("/* something */ struct Foo\n" 7193 "{};", 7194 Style); 7195 verifyFormat("template <typename X> struct Foo\n" 7196 "{};", 7197 Style); 7198 verifyFormat("struct Foo\n" 7199 "{\n" 7200 " Foo();\n" 7201 "};", 7202 Style); 7203 verifyFormat("typedef struct Foo\n" 7204 "{\n" 7205 "} Foo_t;", 7206 Style); 7207 //typedef struct Bar {} Bar_t; 7208 } 7209 7210 TEST_F(FormatTest, SplitEmptyUnion) { 7211 FormatStyle Style = getLLVMStyle(); 7212 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7213 Style.BraceWrapping.AfterUnion = true; 7214 Style.BraceWrapping.SplitEmptyRecord = false; 7215 7216 verifyFormat("union Foo\n" 7217 "{};", 7218 Style); 7219 verifyFormat("/* something */ union Foo\n" 7220 "{};", 7221 Style); 7222 verifyFormat("union Foo\n" 7223 "{\n" 7224 " A,\n" 7225 "};", 7226 Style); 7227 verifyFormat("typedef union Foo\n" 7228 "{\n" 7229 "} Foo_t;", 7230 Style); 7231 } 7232 7233 TEST_F(FormatTest, SplitEmptyNamespace) { 7234 FormatStyle Style = getLLVMStyle(); 7235 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7236 Style.BraceWrapping.AfterNamespace = true; 7237 Style.BraceWrapping.SplitEmptyNamespace = false; 7238 7239 verifyFormat("namespace Foo\n" 7240 "{};", 7241 Style); 7242 verifyFormat("/* something */ namespace Foo\n" 7243 "{};", 7244 Style); 7245 verifyFormat("inline namespace Foo\n" 7246 "{};", 7247 Style); 7248 verifyFormat("namespace Foo\n" 7249 "{\n" 7250 "void Bar();\n" 7251 "};", 7252 Style); 7253 } 7254 7255 TEST_F(FormatTest, NeverMergeShortRecords) { 7256 FormatStyle Style = getLLVMStyle(); 7257 7258 verifyFormat("class Foo {\n" 7259 " Foo();\n" 7260 "};", 7261 Style); 7262 verifyFormat("typedef class Foo {\n" 7263 " Foo();\n" 7264 "} Foo_t;", 7265 Style); 7266 verifyFormat("struct Foo {\n" 7267 " Foo();\n" 7268 "};", 7269 Style); 7270 verifyFormat("typedef struct Foo {\n" 7271 " Foo();\n" 7272 "} Foo_t;", 7273 Style); 7274 verifyFormat("union Foo {\n" 7275 " A,\n" 7276 "};", 7277 Style); 7278 verifyFormat("typedef union Foo {\n" 7279 " A,\n" 7280 "} Foo_t;", 7281 Style); 7282 verifyFormat("namespace Foo {\n" 7283 "void Bar();\n" 7284 "};", 7285 Style); 7286 7287 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7288 Style.BraceWrapping.AfterClass = true; 7289 Style.BraceWrapping.AfterStruct = true; 7290 Style.BraceWrapping.AfterUnion = true; 7291 Style.BraceWrapping.AfterNamespace = true; 7292 verifyFormat("class Foo\n" 7293 "{\n" 7294 " Foo();\n" 7295 "};", 7296 Style); 7297 verifyFormat("typedef class Foo\n" 7298 "{\n" 7299 " Foo();\n" 7300 "} Foo_t;", 7301 Style); 7302 verifyFormat("struct Foo\n" 7303 "{\n" 7304 " Foo();\n" 7305 "};", 7306 Style); 7307 verifyFormat("typedef struct Foo\n" 7308 "{\n" 7309 " Foo();\n" 7310 "} Foo_t;", 7311 Style); 7312 verifyFormat("union Foo\n" 7313 "{\n" 7314 " A,\n" 7315 "};", 7316 Style); 7317 verifyFormat("typedef union Foo\n" 7318 "{\n" 7319 " A,\n" 7320 "} Foo_t;", 7321 Style); 7322 verifyFormat("namespace Foo\n" 7323 "{\n" 7324 "void Bar();\n" 7325 "};", 7326 Style); 7327 } 7328 7329 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 7330 // Elaborate type variable declarations. 7331 verifyFormat("struct foo a = {bar};\nint n;"); 7332 verifyFormat("class foo a = {bar};\nint n;"); 7333 verifyFormat("union foo a = {bar};\nint n;"); 7334 7335 // Elaborate types inside function definitions. 7336 verifyFormat("struct foo f() {}\nint n;"); 7337 verifyFormat("class foo f() {}\nint n;"); 7338 verifyFormat("union foo f() {}\nint n;"); 7339 7340 // Templates. 7341 verifyFormat("template <class X> void f() {}\nint n;"); 7342 verifyFormat("template <struct X> void f() {}\nint n;"); 7343 verifyFormat("template <union X> void f() {}\nint n;"); 7344 7345 // Actual definitions... 7346 verifyFormat("struct {\n} n;"); 7347 verifyFormat( 7348 "template <template <class T, class Y>, class Z> class X {\n} n;"); 7349 verifyFormat("union Z {\n int n;\n} x;"); 7350 verifyFormat("class MACRO Z {\n} n;"); 7351 verifyFormat("class MACRO(X) Z {\n} n;"); 7352 verifyFormat("class __attribute__(X) Z {\n} n;"); 7353 verifyFormat("class __declspec(X) Z {\n} n;"); 7354 verifyFormat("class A##B##C {\n} n;"); 7355 verifyFormat("class alignas(16) Z {\n} n;"); 7356 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;"); 7357 verifyFormat("class MACROA MACRO(X) Z {\n} n;"); 7358 7359 // Redefinition from nested context: 7360 verifyFormat("class A::B::C {\n} n;"); 7361 7362 // Template definitions. 7363 verifyFormat( 7364 "template <typename F>\n" 7365 "Matcher(const Matcher<F> &Other,\n" 7366 " typename enable_if_c<is_base_of<F, T>::value &&\n" 7367 " !is_same<F, T>::value>::type * = 0)\n" 7368 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}"); 7369 7370 // FIXME: This is still incorrectly handled at the formatter side. 7371 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};"); 7372 verifyFormat("int i = SomeFunction(a<b, a> b);"); 7373 7374 // FIXME: 7375 // This now gets parsed incorrectly as class definition. 7376 // verifyFormat("class A<int> f() {\n}\nint n;"); 7377 7378 // Elaborate types where incorrectly parsing the structural element would 7379 // break the indent. 7380 verifyFormat("if (true)\n" 7381 " class X x;\n" 7382 "else\n" 7383 " f();\n"); 7384 7385 // This is simply incomplete. Formatting is not important, but must not crash. 7386 verifyFormat("class A:"); 7387 } 7388 7389 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 7390 EXPECT_EQ("#error Leave all white!!!!! space* alone!\n", 7391 format("#error Leave all white!!!!! space* alone!\n")); 7392 EXPECT_EQ( 7393 "#warning Leave all white!!!!! space* alone!\n", 7394 format("#warning Leave all white!!!!! space* alone!\n")); 7395 EXPECT_EQ("#error 1", format(" # error 1")); 7396 EXPECT_EQ("#warning 1", format(" # warning 1")); 7397 } 7398 7399 TEST_F(FormatTest, FormatHashIfExpressions) { 7400 verifyFormat("#if AAAA && BBBB"); 7401 verifyFormat("#if (AAAA && BBBB)"); 7402 verifyFormat("#elif (AAAA && BBBB)"); 7403 // FIXME: Come up with a better indentation for #elif. 7404 verifyFormat( 7405 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" 7406 " defined(BBBBBBBB)\n" 7407 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" 7408 " defined(BBBBBBBB)\n" 7409 "#endif", 7410 getLLVMStyleWithColumns(65)); 7411 } 7412 7413 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 7414 FormatStyle AllowsMergedIf = getGoogleStyle(); 7415 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 7416 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 7417 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 7418 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 7419 EXPECT_EQ("if (true) return 42;", 7420 format("if (true)\nreturn 42;", AllowsMergedIf)); 7421 FormatStyle ShortMergedIf = AllowsMergedIf; 7422 ShortMergedIf.ColumnLimit = 25; 7423 verifyFormat("#define A \\\n" 7424 " if (true) return 42;", 7425 ShortMergedIf); 7426 verifyFormat("#define A \\\n" 7427 " f(); \\\n" 7428 " if (true)\n" 7429 "#define B", 7430 ShortMergedIf); 7431 verifyFormat("#define A \\\n" 7432 " f(); \\\n" 7433 " if (true)\n" 7434 "g();", 7435 ShortMergedIf); 7436 verifyFormat("{\n" 7437 "#ifdef A\n" 7438 " // Comment\n" 7439 " if (true) continue;\n" 7440 "#endif\n" 7441 " // Comment\n" 7442 " if (true) continue;\n" 7443 "}", 7444 ShortMergedIf); 7445 ShortMergedIf.ColumnLimit = 33; 7446 verifyFormat("#define A \\\n" 7447 " if constexpr (true) return 42;", 7448 ShortMergedIf); 7449 ShortMergedIf.ColumnLimit = 29; 7450 verifyFormat("#define A \\\n" 7451 " if (aaaaaaaaaa) return 1; \\\n" 7452 " return 2;", 7453 ShortMergedIf); 7454 ShortMergedIf.ColumnLimit = 28; 7455 verifyFormat("#define A \\\n" 7456 " if (aaaaaaaaaa) \\\n" 7457 " return 1; \\\n" 7458 " return 2;", 7459 ShortMergedIf); 7460 verifyFormat("#define A \\\n" 7461 " if constexpr (aaaaaaa) \\\n" 7462 " return 1; \\\n" 7463 " return 2;", 7464 ShortMergedIf); 7465 } 7466 7467 TEST_F(FormatTest, FormatStarDependingOnContext) { 7468 verifyFormat("void f(int *a);"); 7469 verifyFormat("void f() { f(fint * b); }"); 7470 verifyFormat("class A {\n void f(int *a);\n};"); 7471 verifyFormat("class A {\n int *a;\n};"); 7472 verifyFormat("namespace a {\n" 7473 "namespace b {\n" 7474 "class A {\n" 7475 " void f() {}\n" 7476 " int *a;\n" 7477 "};\n" 7478 "} // namespace b\n" 7479 "} // namespace a"); 7480 } 7481 7482 TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 7483 verifyFormat("while"); 7484 verifyFormat("operator"); 7485 } 7486 7487 TEST_F(FormatTest, SkipsDeeplyNestedLines) { 7488 // This code would be painfully slow to format if we didn't skip it. 7489 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 7490 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 7491 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 7492 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 7493 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 7494 "A(1, 1)\n" 7495 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x 7496 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7497 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7498 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7499 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7500 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7501 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7502 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7503 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7504 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n"); 7505 // Deeply nested part is untouched, rest is formatted. 7506 EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n", 7507 format(std::string("int i;\n") + Code + "int j;\n", 7508 getLLVMStyle(), SC_ExpectIncomplete)); 7509 } 7510 7511 //===----------------------------------------------------------------------===// 7512 // Objective-C tests. 7513 //===----------------------------------------------------------------------===// 7514 7515 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 7516 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 7517 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 7518 format("-(NSUInteger)indexOfObject:(id)anObject;")); 7519 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 7520 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 7521 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 7522 format("-(NSInteger)Method3:(id)anObject;")); 7523 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 7524 format("-(NSInteger)Method4:(id)anObject;")); 7525 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 7526 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 7527 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 7528 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 7529 EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject " 7530 "forAllCells:(BOOL)flag;", 7531 format("- (void)sendAction:(SEL)aSelector to:(id)anObject " 7532 "forAllCells:(BOOL)flag;")); 7533 7534 // Very long objectiveC method declaration. 7535 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 7536 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;"); 7537 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 7538 " inRange:(NSRange)range\n" 7539 " outRange:(NSRange)out_range\n" 7540 " outRange1:(NSRange)out_range1\n" 7541 " outRange2:(NSRange)out_range2\n" 7542 " outRange3:(NSRange)out_range3\n" 7543 " outRange4:(NSRange)out_range4\n" 7544 " outRange5:(NSRange)out_range5\n" 7545 " outRange6:(NSRange)out_range6\n" 7546 " outRange7:(NSRange)out_range7\n" 7547 " outRange8:(NSRange)out_range8\n" 7548 " outRange9:(NSRange)out_range9;"); 7549 7550 // When the function name has to be wrapped. 7551 FormatStyle Style = getLLVMStyle(); 7552 Style.IndentWrappedFunctionNames = false; 7553 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 7554 "veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 7555 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 7556 "}", 7557 Style); 7558 Style.IndentWrappedFunctionNames = true; 7559 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 7560 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 7561 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 7562 "}", 7563 Style); 7564 7565 verifyFormat("- (int)sum:(vector<int>)numbers;"); 7566 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 7567 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 7568 // protocol lists (but not for template classes): 7569 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 7570 7571 verifyFormat("- (int (*)())foo:(int (*)())f;"); 7572 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); 7573 7574 // If there's no return type (very rare in practice!), LLVM and Google style 7575 // agree. 7576 verifyFormat("- foo;"); 7577 verifyFormat("- foo:(int)f;"); 7578 verifyGoogleFormat("- foo:(int)foo;"); 7579 } 7580 7581 7582 TEST_F(FormatTest, BreaksStringLiterals) { 7583 EXPECT_EQ("\"some text \"\n" 7584 "\"other\";", 7585 format("\"some text other\";", getLLVMStyleWithColumns(12))); 7586 EXPECT_EQ("\"some text \"\n" 7587 "\"other\";", 7588 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); 7589 EXPECT_EQ( 7590 "#define A \\\n" 7591 " \"some \" \\\n" 7592 " \"text \" \\\n" 7593 " \"other\";", 7594 format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); 7595 EXPECT_EQ( 7596 "#define A \\\n" 7597 " \"so \" \\\n" 7598 " \"text \" \\\n" 7599 " \"other\";", 7600 format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); 7601 7602 EXPECT_EQ("\"some text\"", 7603 format("\"some text\"", getLLVMStyleWithColumns(1))); 7604 EXPECT_EQ("\"some text\"", 7605 format("\"some text\"", getLLVMStyleWithColumns(11))); 7606 EXPECT_EQ("\"some \"\n" 7607 "\"text\"", 7608 format("\"some text\"", getLLVMStyleWithColumns(10))); 7609 EXPECT_EQ("\"some \"\n" 7610 "\"text\"", 7611 format("\"some text\"", getLLVMStyleWithColumns(7))); 7612 EXPECT_EQ("\"some\"\n" 7613 "\" tex\"\n" 7614 "\"t\"", 7615 format("\"some text\"", getLLVMStyleWithColumns(6))); 7616 EXPECT_EQ("\"some\"\n" 7617 "\" tex\"\n" 7618 "\" and\"", 7619 format("\"some tex and\"", getLLVMStyleWithColumns(6))); 7620 EXPECT_EQ("\"some\"\n" 7621 "\"/tex\"\n" 7622 "\"/and\"", 7623 format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); 7624 7625 EXPECT_EQ("variable =\n" 7626 " \"long string \"\n" 7627 " \"literal\";", 7628 format("variable = \"long string literal\";", 7629 getLLVMStyleWithColumns(20))); 7630 7631 EXPECT_EQ("variable = f(\n" 7632 " \"long string \"\n" 7633 " \"literal\",\n" 7634 " short,\n" 7635 " loooooooooooooooooooong);", 7636 format("variable = f(\"long string literal\", short, " 7637 "loooooooooooooooooooong);", 7638 getLLVMStyleWithColumns(20))); 7639 7640 EXPECT_EQ( 7641 "f(g(\"long string \"\n" 7642 " \"literal\"),\n" 7643 " b);", 7644 format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20))); 7645 EXPECT_EQ("f(g(\"long string \"\n" 7646 " \"literal\",\n" 7647 " a),\n" 7648 " b);", 7649 format("f(g(\"long string literal\", a), b);", 7650 getLLVMStyleWithColumns(20))); 7651 EXPECT_EQ( 7652 "f(\"one two\".split(\n" 7653 " variable));", 7654 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); 7655 EXPECT_EQ("f(\"one two three four five six \"\n" 7656 " \"seven\".split(\n" 7657 " really_looooong_variable));", 7658 format("f(\"one two three four five six seven\"." 7659 "split(really_looooong_variable));", 7660 getLLVMStyleWithColumns(33))); 7661 7662 EXPECT_EQ("f(\"some \"\n" 7663 " \"text\",\n" 7664 " other);", 7665 format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); 7666 7667 // Only break as a last resort. 7668 verifyFormat( 7669 "aaaaaaaaaaaaaaaaaaaa(\n" 7670 " aaaaaaaaaaaaaaaaaaaa,\n" 7671 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); 7672 7673 EXPECT_EQ("\"splitmea\"\n" 7674 "\"trandomp\"\n" 7675 "\"oint\"", 7676 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); 7677 7678 EXPECT_EQ("\"split/\"\n" 7679 "\"pathat/\"\n" 7680 "\"slashes\"", 7681 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 7682 7683 EXPECT_EQ("\"split/\"\n" 7684 "\"pathat/\"\n" 7685 "\"slashes\"", 7686 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 7687 EXPECT_EQ("\"split at \"\n" 7688 "\"spaces/at/\"\n" 7689 "\"slashes.at.any$\"\n" 7690 "\"non-alphanumeric%\"\n" 7691 "\"1111111111characte\"\n" 7692 "\"rs\"", 7693 format("\"split at " 7694 "spaces/at/" 7695 "slashes.at." 7696 "any$non-" 7697 "alphanumeric%" 7698 "1111111111characte" 7699 "rs\"", 7700 getLLVMStyleWithColumns(20))); 7701 7702 // Verify that splitting the strings understands 7703 // Style::AlwaysBreakBeforeMultilineStrings. 7704 EXPECT_EQ( 7705 "aaaaaaaaaaaa(\n" 7706 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" 7707 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", 7708 format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa " 7709 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 7710 "aaaaaaaaaaaaaaaaaaaaaa\");", 7711 getGoogleStyle())); 7712 EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7713 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", 7714 format("return \"aaaaaaaaaaaaaaaaaaaaaa " 7715 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 7716 "aaaaaaaaaaaaaaaaaaaaaa\";", 7717 getGoogleStyle())); 7718 EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7719 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 7720 format("llvm::outs() << " 7721 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa" 7722 "aaaaaaaaaaaaaaaaaaa\";")); 7723 EXPECT_EQ("ffff(\n" 7724 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7725 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 7726 format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " 7727 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 7728 getGoogleStyle())); 7729 7730 FormatStyle Style = getLLVMStyleWithColumns(12); 7731 Style.BreakStringLiterals = false; 7732 EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style)); 7733 7734 FormatStyle AlignLeft = getLLVMStyleWithColumns(12); 7735 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 7736 EXPECT_EQ("#define A \\\n" 7737 " \"some \" \\\n" 7738 " \"text \" \\\n" 7739 " \"other\";", 7740 format("#define A \"some text other\";", AlignLeft)); 7741 } 7742 7743 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) { 7744 EXPECT_EQ("C a = \"some more \"\n" 7745 " \"text\";", 7746 format("C a = \"some more text\";", getLLVMStyleWithColumns(18))); 7747 } 7748 7749 TEST_F(FormatTest, FullyRemoveEmptyLines) { 7750 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80); 7751 NoEmptyLines.MaxEmptyLinesToKeep = 0; 7752 EXPECT_EQ("int i = a(b());", 7753 format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines)); 7754 } 7755 7756 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) { 7757 EXPECT_EQ( 7758 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 7759 "(\n" 7760 " \"x\t\");", 7761 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 7762 "aaaaaaa(" 7763 "\"x\t\");")); 7764 } 7765 7766 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) { 7767 EXPECT_EQ( 7768 "u8\"utf8 string \"\n" 7769 "u8\"literal\";", 7770 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16))); 7771 EXPECT_EQ( 7772 "u\"utf16 string \"\n" 7773 "u\"literal\";", 7774 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16))); 7775 EXPECT_EQ( 7776 "U\"utf32 string \"\n" 7777 "U\"literal\";", 7778 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16))); 7779 EXPECT_EQ("L\"wide string \"\n" 7780 "L\"literal\";", 7781 format("L\"wide string literal\";", getGoogleStyleWithColumns(16))); 7782 EXPECT_EQ("@\"NSString \"\n" 7783 "@\"literal\";", 7784 format("@\"NSString literal\";", getGoogleStyleWithColumns(19))); 7785 verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26)); 7786 7787 // This input makes clang-format try to split the incomplete unicode escape 7788 // sequence, which used to lead to a crasher. 7789 verifyNoCrash( 7790 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 7791 getLLVMStyleWithColumns(60)); 7792 } 7793 7794 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) { 7795 FormatStyle Style = getGoogleStyleWithColumns(15); 7796 EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style)); 7797 EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style)); 7798 EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style)); 7799 EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style)); 7800 EXPECT_EQ("u8R\"x(raw literal)x\";", 7801 format("u8R\"x(raw literal)x\";", Style)); 7802 } 7803 7804 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { 7805 FormatStyle Style = getLLVMStyleWithColumns(20); 7806 EXPECT_EQ( 7807 "_T(\"aaaaaaaaaaaaaa\")\n" 7808 "_T(\"aaaaaaaaaaaaaa\")\n" 7809 "_T(\"aaaaaaaaaaaa\")", 7810 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style)); 7811 EXPECT_EQ("f(x,\n" 7812 " _T(\"aaaaaaaaaaaa\")\n" 7813 " _T(\"aaa\"),\n" 7814 " z);", 7815 format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style)); 7816 7817 // FIXME: Handle embedded spaces in one iteration. 7818 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n" 7819 // "_T(\"aaaaaaaaaaaaa\")\n" 7820 // "_T(\"aaaaaaaaaaaaa\")\n" 7821 // "_T(\"a\")", 7822 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 7823 // getLLVMStyleWithColumns(20))); 7824 EXPECT_EQ( 7825 "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 7826 format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style)); 7827 EXPECT_EQ("f(\n" 7828 "#if !TEST\n" 7829 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 7830 "#endif\n" 7831 ");", 7832 format("f(\n" 7833 "#if !TEST\n" 7834 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 7835 "#endif\n" 7836 ");")); 7837 EXPECT_EQ("f(\n" 7838 "\n" 7839 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));", 7840 format("f(\n" 7841 "\n" 7842 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));")); 7843 } 7844 7845 TEST_F(FormatTest, BreaksStringLiteralOperands) { 7846 // In a function call with two operands, the second can be broken with no line 7847 // break before it. 7848 EXPECT_EQ("func(a, \"long long \"\n" 7849 " \"long long\");", 7850 format("func(a, \"long long long long\");", 7851 getLLVMStyleWithColumns(24))); 7852 // In a function call with three operands, the second must be broken with a 7853 // line break before it. 7854 EXPECT_EQ("func(a,\n" 7855 " \"long long long \"\n" 7856 " \"long\",\n" 7857 " c);", 7858 format("func(a, \"long long long long\", c);", 7859 getLLVMStyleWithColumns(24))); 7860 // In a function call with three operands, the third must be broken with a 7861 // line break before it. 7862 EXPECT_EQ("func(a, b,\n" 7863 " \"long long long \"\n" 7864 " \"long\");", 7865 format("func(a, b, \"long long long long\");", 7866 getLLVMStyleWithColumns(24))); 7867 // In a function call with three operands, both the second and the third must 7868 // be broken with a line break before them. 7869 EXPECT_EQ("func(a,\n" 7870 " \"long long long \"\n" 7871 " \"long\",\n" 7872 " \"long long long \"\n" 7873 " \"long\");", 7874 format("func(a, \"long long long long\", \"long long long long\");", 7875 getLLVMStyleWithColumns(24))); 7876 // In a chain of << with two operands, the second can be broken with no line 7877 // break before it. 7878 EXPECT_EQ("a << \"line line \"\n" 7879 " \"line\";", 7880 format("a << \"line line line\";", 7881 getLLVMStyleWithColumns(20))); 7882 // In a chain of << with three operands, the second can be broken with no line 7883 // break before it. 7884 EXPECT_EQ("abcde << \"line \"\n" 7885 " \"line line\"\n" 7886 " << c;", 7887 format("abcde << \"line line line\" << c;", 7888 getLLVMStyleWithColumns(20))); 7889 // In a chain of << with three operands, the third must be broken with a line 7890 // break before it. 7891 EXPECT_EQ("a << b\n" 7892 " << \"line line \"\n" 7893 " \"line\";", 7894 format("a << b << \"line line line\";", 7895 getLLVMStyleWithColumns(20))); 7896 // In a chain of << with three operands, the second can be broken with no line 7897 // break before it and the third must be broken with a line break before it. 7898 EXPECT_EQ("abcd << \"line line \"\n" 7899 " \"line\"\n" 7900 " << \"line line \"\n" 7901 " \"line\";", 7902 format("abcd << \"line line line\" << \"line line line\";", 7903 getLLVMStyleWithColumns(20))); 7904 // In a chain of binary operators with two operands, the second can be broken 7905 // with no line break before it. 7906 EXPECT_EQ("abcd + \"line line \"\n" 7907 " \"line line\";", 7908 format("abcd + \"line line line line\";", 7909 getLLVMStyleWithColumns(20))); 7910 // In a chain of binary operators with three operands, the second must be 7911 // broken with a line break before it. 7912 EXPECT_EQ("abcd +\n" 7913 " \"line line \"\n" 7914 " \"line line\" +\n" 7915 " e;", 7916 format("abcd + \"line line line line\" + e;", 7917 getLLVMStyleWithColumns(20))); 7918 // In a function call with two operands, with AlignAfterOpenBracket enabled, 7919 // the first must be broken with a line break before it. 7920 FormatStyle Style = getLLVMStyleWithColumns(25); 7921 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 7922 EXPECT_EQ("someFunction(\n" 7923 " \"long long long \"\n" 7924 " \"long\",\n" 7925 " a);", 7926 format("someFunction(\"long long long long\", a);", Style)); 7927 } 7928 7929 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { 7930 EXPECT_EQ( 7931 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 7932 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 7933 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 7934 format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 7935 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 7936 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";")); 7937 } 7938 7939 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { 7940 EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);", 7941 format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle())); 7942 EXPECT_EQ("fffffffffff(g(R\"x(\n" 7943 "multiline raw string literal xxxxxxxxxxxxxx\n" 7944 ")x\",\n" 7945 " a),\n" 7946 " b);", 7947 format("fffffffffff(g(R\"x(\n" 7948 "multiline raw string literal xxxxxxxxxxxxxx\n" 7949 ")x\", a), b);", 7950 getGoogleStyleWithColumns(20))); 7951 EXPECT_EQ("fffffffffff(\n" 7952 " g(R\"x(qqq\n" 7953 "multiline raw string literal xxxxxxxxxxxxxx\n" 7954 ")x\",\n" 7955 " a),\n" 7956 " b);", 7957 format("fffffffffff(g(R\"x(qqq\n" 7958 "multiline raw string literal xxxxxxxxxxxxxx\n" 7959 ")x\", a), b);", 7960 getGoogleStyleWithColumns(20))); 7961 7962 EXPECT_EQ("fffffffffff(R\"x(\n" 7963 "multiline raw string literal xxxxxxxxxxxxxx\n" 7964 ")x\");", 7965 format("fffffffffff(R\"x(\n" 7966 "multiline raw string literal xxxxxxxxxxxxxx\n" 7967 ")x\");", 7968 getGoogleStyleWithColumns(20))); 7969 EXPECT_EQ("fffffffffff(R\"x(\n" 7970 "multiline raw string literal xxxxxxxxxxxxxx\n" 7971 ")x\" + bbbbbb);", 7972 format("fffffffffff(R\"x(\n" 7973 "multiline raw string literal xxxxxxxxxxxxxx\n" 7974 ")x\" + bbbbbb);", 7975 getGoogleStyleWithColumns(20))); 7976 EXPECT_EQ("fffffffffff(\n" 7977 " R\"x(\n" 7978 "multiline raw string literal xxxxxxxxxxxxxx\n" 7979 ")x\" +\n" 7980 " bbbbbb);", 7981 format("fffffffffff(\n" 7982 " R\"x(\n" 7983 "multiline raw string literal xxxxxxxxxxxxxx\n" 7984 ")x\" + bbbbbb);", 7985 getGoogleStyleWithColumns(20))); 7986 } 7987 7988 TEST_F(FormatTest, SkipsUnknownStringLiterals) { 7989 verifyFormat("string a = \"unterminated;"); 7990 EXPECT_EQ("function(\"unterminated,\n" 7991 " OtherParameter);", 7992 format("function( \"unterminated,\n" 7993 " OtherParameter);")); 7994 } 7995 7996 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { 7997 FormatStyle Style = getLLVMStyle(); 7998 Style.Standard = FormatStyle::LS_Cpp03; 7999 EXPECT_EQ("#define x(_a) printf(\"foo\" _a);", 8000 format("#define x(_a) printf(\"foo\"_a);", Style)); 8001 } 8002 8003 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); } 8004 8005 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { 8006 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n" 8007 " \"ddeeefff\");", 8008 format("someFunction(\"aaabbbcccdddeeefff\");", 8009 getLLVMStyleWithColumns(25))); 8010 EXPECT_EQ("someFunction1234567890(\n" 8011 " \"aaabbbcccdddeeefff\");", 8012 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 8013 getLLVMStyleWithColumns(26))); 8014 EXPECT_EQ("someFunction1234567890(\n" 8015 " \"aaabbbcccdddeeeff\"\n" 8016 " \"f\");", 8017 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 8018 getLLVMStyleWithColumns(25))); 8019 EXPECT_EQ("someFunction1234567890(\n" 8020 " \"aaabbbcccdddeeeff\"\n" 8021 " \"f\");", 8022 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 8023 getLLVMStyleWithColumns(24))); 8024 EXPECT_EQ("someFunction(\n" 8025 " \"aaabbbcc ddde \"\n" 8026 " \"efff\");", 8027 format("someFunction(\"aaabbbcc ddde efff\");", 8028 getLLVMStyleWithColumns(25))); 8029 EXPECT_EQ("someFunction(\"aaabbbccc \"\n" 8030 " \"ddeeefff\");", 8031 format("someFunction(\"aaabbbccc ddeeefff\");", 8032 getLLVMStyleWithColumns(25))); 8033 EXPECT_EQ("someFunction1234567890(\n" 8034 " \"aaabb \"\n" 8035 " \"cccdddeeefff\");", 8036 format("someFunction1234567890(\"aaabb cccdddeeefff\");", 8037 getLLVMStyleWithColumns(25))); 8038 EXPECT_EQ("#define A \\\n" 8039 " string s = \\\n" 8040 " \"123456789\" \\\n" 8041 " \"0\"; \\\n" 8042 " int i;", 8043 format("#define A string s = \"1234567890\"; int i;", 8044 getLLVMStyleWithColumns(20))); 8045 EXPECT_EQ("someFunction(\n" 8046 " \"aaabbbcc \"\n" 8047 " \"dddeeefff\");", 8048 format("someFunction(\"aaabbbcc dddeeefff\");", 8049 getLLVMStyleWithColumns(25))); 8050 } 8051 8052 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { 8053 EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3))); 8054 EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2))); 8055 EXPECT_EQ("\"test\"\n" 8056 "\"\\n\"", 8057 format("\"test\\n\"", getLLVMStyleWithColumns(7))); 8058 EXPECT_EQ("\"tes\\\\\"\n" 8059 "\"n\"", 8060 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); 8061 EXPECT_EQ("\"\\\\\\\\\"\n" 8062 "\"\\n\"", 8063 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); 8064 EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7))); 8065 EXPECT_EQ("\"\\uff01\"\n" 8066 "\"test\"", 8067 format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); 8068 EXPECT_EQ("\"\\Uff01ff02\"", 8069 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); 8070 EXPECT_EQ("\"\\x000000000001\"\n" 8071 "\"next\"", 8072 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); 8073 EXPECT_EQ("\"\\x000000000001next\"", 8074 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); 8075 EXPECT_EQ("\"\\x000000000001\"", 8076 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); 8077 EXPECT_EQ("\"test\"\n" 8078 "\"\\000000\"\n" 8079 "\"000001\"", 8080 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); 8081 EXPECT_EQ("\"test\\000\"\n" 8082 "\"00000000\"\n" 8083 "\"1\"", 8084 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); 8085 } 8086 8087 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { 8088 verifyFormat("void f() {\n" 8089 " return g() {}\n" 8090 " void h() {}"); 8091 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n" 8092 "g();\n" 8093 "}"); 8094 } 8095 8096 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) { 8097 verifyFormat( 8098 "void f() { return C{param1, param2}.SomeCall(param1, param2); }"); 8099 } 8100 8101 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { 8102 verifyFormat("class X {\n" 8103 " void f() {\n" 8104 " }\n" 8105 "};", 8106 getLLVMStyleWithColumns(12)); 8107 } 8108 8109 TEST_F(FormatTest, ConfigurableIndentWidth) { 8110 FormatStyle EightIndent = getLLVMStyleWithColumns(18); 8111 EightIndent.IndentWidth = 8; 8112 EightIndent.ContinuationIndentWidth = 8; 8113 verifyFormat("void f() {\n" 8114 " someFunction();\n" 8115 " if (true) {\n" 8116 " f();\n" 8117 " }\n" 8118 "}", 8119 EightIndent); 8120 verifyFormat("class X {\n" 8121 " void f() {\n" 8122 " }\n" 8123 "};", 8124 EightIndent); 8125 verifyFormat("int x[] = {\n" 8126 " call(),\n" 8127 " call()};", 8128 EightIndent); 8129 } 8130 8131 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) { 8132 verifyFormat("double\n" 8133 "f();", 8134 getLLVMStyleWithColumns(8)); 8135 } 8136 8137 TEST_F(FormatTest, ConfigurableUseOfTab) { 8138 FormatStyle Tab = getLLVMStyleWithColumns(42); 8139 Tab.IndentWidth = 8; 8140 Tab.UseTab = FormatStyle::UT_Always; 8141 Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left; 8142 8143 EXPECT_EQ("if (aaaaaaaa && // q\n" 8144 " bb)\t\t// w\n" 8145 "\t;", 8146 format("if (aaaaaaaa &&// q\n" 8147 "bb)// w\n" 8148 ";", 8149 Tab)); 8150 EXPECT_EQ("if (aaa && bbb) // w\n" 8151 "\t;", 8152 format("if(aaa&&bbb)// w\n" 8153 ";", 8154 Tab)); 8155 8156 verifyFormat("class X {\n" 8157 "\tvoid f() {\n" 8158 "\t\tsomeFunction(parameter1,\n" 8159 "\t\t\t parameter2);\n" 8160 "\t}\n" 8161 "};", 8162 Tab); 8163 verifyFormat("#define A \\\n" 8164 "\tvoid f() { \\\n" 8165 "\t\tsomeFunction( \\\n" 8166 "\t\t parameter1, \\\n" 8167 "\t\t parameter2); \\\n" 8168 "\t}", 8169 Tab); 8170 8171 Tab.TabWidth = 4; 8172 Tab.IndentWidth = 8; 8173 verifyFormat("class TabWidth4Indent8 {\n" 8174 "\t\tvoid f() {\n" 8175 "\t\t\t\tsomeFunction(parameter1,\n" 8176 "\t\t\t\t\t\t\t parameter2);\n" 8177 "\t\t}\n" 8178 "};", 8179 Tab); 8180 8181 Tab.TabWidth = 4; 8182 Tab.IndentWidth = 4; 8183 verifyFormat("class TabWidth4Indent4 {\n" 8184 "\tvoid f() {\n" 8185 "\t\tsomeFunction(parameter1,\n" 8186 "\t\t\t\t\t parameter2);\n" 8187 "\t}\n" 8188 "};", 8189 Tab); 8190 8191 Tab.TabWidth = 8; 8192 Tab.IndentWidth = 4; 8193 verifyFormat("class TabWidth8Indent4 {\n" 8194 " void f() {\n" 8195 "\tsomeFunction(parameter1,\n" 8196 "\t\t parameter2);\n" 8197 " }\n" 8198 "};", 8199 Tab); 8200 8201 Tab.TabWidth = 8; 8202 Tab.IndentWidth = 8; 8203 EXPECT_EQ("/*\n" 8204 "\t a\t\tcomment\n" 8205 "\t in multiple lines\n" 8206 " */", 8207 format(" /*\t \t \n" 8208 " \t \t a\t\tcomment\t \t\n" 8209 " \t \t in multiple lines\t\n" 8210 " \t */", 8211 Tab)); 8212 8213 Tab.UseTab = FormatStyle::UT_ForIndentation; 8214 verifyFormat("{\n" 8215 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8216 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8217 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8218 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8219 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8220 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8221 "};", 8222 Tab); 8223 verifyFormat("enum AA {\n" 8224 "\ta1, // Force multiple lines\n" 8225 "\ta2,\n" 8226 "\ta3\n" 8227 "};", 8228 Tab); 8229 EXPECT_EQ("if (aaaaaaaa && // q\n" 8230 " bb) // w\n" 8231 "\t;", 8232 format("if (aaaaaaaa &&// q\n" 8233 "bb)// w\n" 8234 ";", 8235 Tab)); 8236 verifyFormat("class X {\n" 8237 "\tvoid f() {\n" 8238 "\t\tsomeFunction(parameter1,\n" 8239 "\t\t parameter2);\n" 8240 "\t}\n" 8241 "};", 8242 Tab); 8243 verifyFormat("{\n" 8244 "\tQ(\n" 8245 "\t {\n" 8246 "\t\t int a;\n" 8247 "\t\t someFunction(aaaaaaaa,\n" 8248 "\t\t bbbbbbb);\n" 8249 "\t },\n" 8250 "\t p);\n" 8251 "}", 8252 Tab); 8253 EXPECT_EQ("{\n" 8254 "\t/* aaaa\n" 8255 "\t bbbb */\n" 8256 "}", 8257 format("{\n" 8258 "/* aaaa\n" 8259 " bbbb */\n" 8260 "}", 8261 Tab)); 8262 EXPECT_EQ("{\n" 8263 "\t/*\n" 8264 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8265 "\t bbbbbbbbbbbbb\n" 8266 "\t*/\n" 8267 "}", 8268 format("{\n" 8269 "/*\n" 8270 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8271 "*/\n" 8272 "}", 8273 Tab)); 8274 EXPECT_EQ("{\n" 8275 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8276 "\t// bbbbbbbbbbbbb\n" 8277 "}", 8278 format("{\n" 8279 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8280 "}", 8281 Tab)); 8282 EXPECT_EQ("{\n" 8283 "\t/*\n" 8284 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8285 "\t bbbbbbbbbbbbb\n" 8286 "\t*/\n" 8287 "}", 8288 format("{\n" 8289 "\t/*\n" 8290 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8291 "\t*/\n" 8292 "}", 8293 Tab)); 8294 EXPECT_EQ("{\n" 8295 "\t/*\n" 8296 "\n" 8297 "\t*/\n" 8298 "}", 8299 format("{\n" 8300 "\t/*\n" 8301 "\n" 8302 "\t*/\n" 8303 "}", 8304 Tab)); 8305 EXPECT_EQ("{\n" 8306 "\t/*\n" 8307 " asdf\n" 8308 "\t*/\n" 8309 "}", 8310 format("{\n" 8311 "\t/*\n" 8312 " asdf\n" 8313 "\t*/\n" 8314 "}", 8315 Tab)); 8316 8317 Tab.UseTab = FormatStyle::UT_Never; 8318 EXPECT_EQ("/*\n" 8319 " a\t\tcomment\n" 8320 " in multiple lines\n" 8321 " */", 8322 format(" /*\t \t \n" 8323 " \t \t a\t\tcomment\t \t\n" 8324 " \t \t in multiple lines\t\n" 8325 " \t */", 8326 Tab)); 8327 EXPECT_EQ("/* some\n" 8328 " comment */", 8329 format(" \t \t /* some\n" 8330 " \t \t comment */", 8331 Tab)); 8332 EXPECT_EQ("int a; /* some\n" 8333 " comment */", 8334 format(" \t \t int a; /* some\n" 8335 " \t \t comment */", 8336 Tab)); 8337 8338 EXPECT_EQ("int a; /* some\n" 8339 "comment */", 8340 format(" \t \t int\ta; /* some\n" 8341 " \t \t comment */", 8342 Tab)); 8343 EXPECT_EQ("f(\"\t\t\"); /* some\n" 8344 " comment */", 8345 format(" \t \t f(\"\t\t\"); /* some\n" 8346 " \t \t comment */", 8347 Tab)); 8348 EXPECT_EQ("{\n" 8349 " /*\n" 8350 " * Comment\n" 8351 " */\n" 8352 " int i;\n" 8353 "}", 8354 format("{\n" 8355 "\t/*\n" 8356 "\t * Comment\n" 8357 "\t */\n" 8358 "\t int i;\n" 8359 "}")); 8360 8361 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 8362 Tab.TabWidth = 8; 8363 Tab.IndentWidth = 8; 8364 EXPECT_EQ("if (aaaaaaaa && // q\n" 8365 " bb) // w\n" 8366 "\t;", 8367 format("if (aaaaaaaa &&// q\n" 8368 "bb)// w\n" 8369 ";", 8370 Tab)); 8371 EXPECT_EQ("if (aaa && bbb) // w\n" 8372 "\t;", 8373 format("if(aaa&&bbb)// w\n" 8374 ";", 8375 Tab)); 8376 verifyFormat("class X {\n" 8377 "\tvoid f() {\n" 8378 "\t\tsomeFunction(parameter1,\n" 8379 "\t\t\t parameter2);\n" 8380 "\t}\n" 8381 "};", 8382 Tab); 8383 verifyFormat("#define A \\\n" 8384 "\tvoid f() { \\\n" 8385 "\t\tsomeFunction( \\\n" 8386 "\t\t parameter1, \\\n" 8387 "\t\t parameter2); \\\n" 8388 "\t}", 8389 Tab); 8390 Tab.TabWidth = 4; 8391 Tab.IndentWidth = 8; 8392 verifyFormat("class TabWidth4Indent8 {\n" 8393 "\t\tvoid f() {\n" 8394 "\t\t\t\tsomeFunction(parameter1,\n" 8395 "\t\t\t\t\t\t\t parameter2);\n" 8396 "\t\t}\n" 8397 "};", 8398 Tab); 8399 Tab.TabWidth = 4; 8400 Tab.IndentWidth = 4; 8401 verifyFormat("class TabWidth4Indent4 {\n" 8402 "\tvoid f() {\n" 8403 "\t\tsomeFunction(parameter1,\n" 8404 "\t\t\t\t\t parameter2);\n" 8405 "\t}\n" 8406 "};", 8407 Tab); 8408 Tab.TabWidth = 8; 8409 Tab.IndentWidth = 4; 8410 verifyFormat("class TabWidth8Indent4 {\n" 8411 " void f() {\n" 8412 "\tsomeFunction(parameter1,\n" 8413 "\t\t parameter2);\n" 8414 " }\n" 8415 "};", 8416 Tab); 8417 Tab.TabWidth = 8; 8418 Tab.IndentWidth = 8; 8419 EXPECT_EQ("/*\n" 8420 "\t a\t\tcomment\n" 8421 "\t in multiple lines\n" 8422 " */", 8423 format(" /*\t \t \n" 8424 " \t \t a\t\tcomment\t \t\n" 8425 " \t \t in multiple lines\t\n" 8426 " \t */", 8427 Tab)); 8428 verifyFormat("{\n" 8429 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8430 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8431 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8432 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8433 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8434 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8435 "};", 8436 Tab); 8437 verifyFormat("enum AA {\n" 8438 "\ta1, // Force multiple lines\n" 8439 "\ta2,\n" 8440 "\ta3\n" 8441 "};", 8442 Tab); 8443 EXPECT_EQ("if (aaaaaaaa && // q\n" 8444 " bb) // w\n" 8445 "\t;", 8446 format("if (aaaaaaaa &&// q\n" 8447 "bb)// w\n" 8448 ";", 8449 Tab)); 8450 verifyFormat("class X {\n" 8451 "\tvoid f() {\n" 8452 "\t\tsomeFunction(parameter1,\n" 8453 "\t\t\t parameter2);\n" 8454 "\t}\n" 8455 "};", 8456 Tab); 8457 verifyFormat("{\n" 8458 "\tQ(\n" 8459 "\t {\n" 8460 "\t\t int a;\n" 8461 "\t\t someFunction(aaaaaaaa,\n" 8462 "\t\t\t\t bbbbbbb);\n" 8463 "\t },\n" 8464 "\t p);\n" 8465 "}", 8466 Tab); 8467 EXPECT_EQ("{\n" 8468 "\t/* aaaa\n" 8469 "\t bbbb */\n" 8470 "}", 8471 format("{\n" 8472 "/* aaaa\n" 8473 " bbbb */\n" 8474 "}", 8475 Tab)); 8476 EXPECT_EQ("{\n" 8477 "\t/*\n" 8478 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8479 "\t bbbbbbbbbbbbb\n" 8480 "\t*/\n" 8481 "}", 8482 format("{\n" 8483 "/*\n" 8484 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8485 "*/\n" 8486 "}", 8487 Tab)); 8488 EXPECT_EQ("{\n" 8489 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8490 "\t// bbbbbbbbbbbbb\n" 8491 "}", 8492 format("{\n" 8493 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8494 "}", 8495 Tab)); 8496 EXPECT_EQ("{\n" 8497 "\t/*\n" 8498 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8499 "\t bbbbbbbbbbbbb\n" 8500 "\t*/\n" 8501 "}", 8502 format("{\n" 8503 "\t/*\n" 8504 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8505 "\t*/\n" 8506 "}", 8507 Tab)); 8508 EXPECT_EQ("{\n" 8509 "\t/*\n" 8510 "\n" 8511 "\t*/\n" 8512 "}", 8513 format("{\n" 8514 "\t/*\n" 8515 "\n" 8516 "\t*/\n" 8517 "}", 8518 Tab)); 8519 EXPECT_EQ("{\n" 8520 "\t/*\n" 8521 " asdf\n" 8522 "\t*/\n" 8523 "}", 8524 format("{\n" 8525 "\t/*\n" 8526 " asdf\n" 8527 "\t*/\n" 8528 "}", 8529 Tab)); 8530 EXPECT_EQ("/*\n" 8531 "\t a\t\tcomment\n" 8532 "\t in multiple lines\n" 8533 " */", 8534 format(" /*\t \t \n" 8535 " \t \t a\t\tcomment\t \t\n" 8536 " \t \t in multiple lines\t\n" 8537 " \t */", 8538 Tab)); 8539 EXPECT_EQ("/* some\n" 8540 " comment */", 8541 format(" \t \t /* some\n" 8542 " \t \t comment */", 8543 Tab)); 8544 EXPECT_EQ("int a; /* some\n" 8545 " comment */", 8546 format(" \t \t int a; /* some\n" 8547 " \t \t comment */", 8548 Tab)); 8549 EXPECT_EQ("int a; /* some\n" 8550 "comment */", 8551 format(" \t \t int\ta; /* some\n" 8552 " \t \t comment */", 8553 Tab)); 8554 EXPECT_EQ("f(\"\t\t\"); /* some\n" 8555 " comment */", 8556 format(" \t \t f(\"\t\t\"); /* some\n" 8557 " \t \t comment */", 8558 Tab)); 8559 EXPECT_EQ("{\n" 8560 " /*\n" 8561 " * Comment\n" 8562 " */\n" 8563 " int i;\n" 8564 "}", 8565 format("{\n" 8566 "\t/*\n" 8567 "\t * Comment\n" 8568 "\t */\n" 8569 "\t int i;\n" 8570 "}")); 8571 Tab.AlignConsecutiveAssignments = true; 8572 Tab.AlignConsecutiveDeclarations = true; 8573 Tab.TabWidth = 4; 8574 Tab.IndentWidth = 4; 8575 verifyFormat("class Assign {\n" 8576 "\tvoid f() {\n" 8577 "\t\tint x = 123;\n" 8578 "\t\tint random = 4;\n" 8579 "\t\tstd::string alphabet =\n" 8580 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 8581 "\t}\n" 8582 "};", 8583 Tab); 8584 } 8585 8586 TEST_F(FormatTest, CalculatesOriginalColumn) { 8587 EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8588 "q\"; /* some\n" 8589 " comment */", 8590 format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8591 "q\"; /* some\n" 8592 " comment */", 8593 getLLVMStyle())); 8594 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 8595 "/* some\n" 8596 " comment */", 8597 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 8598 " /* some\n" 8599 " comment */", 8600 getLLVMStyle())); 8601 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8602 "qqq\n" 8603 "/* some\n" 8604 " comment */", 8605 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8606 "qqq\n" 8607 " /* some\n" 8608 " comment */", 8609 getLLVMStyle())); 8610 EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8611 "wwww; /* some\n" 8612 " comment */", 8613 format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8614 "wwww; /* some\n" 8615 " comment */", 8616 getLLVMStyle())); 8617 } 8618 8619 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { 8620 FormatStyle NoSpace = getLLVMStyle(); 8621 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never; 8622 8623 verifyFormat("while(true)\n" 8624 " continue;", 8625 NoSpace); 8626 verifyFormat("for(;;)\n" 8627 " continue;", 8628 NoSpace); 8629 verifyFormat("if(true)\n" 8630 " f();\n" 8631 "else if(true)\n" 8632 " f();", 8633 NoSpace); 8634 verifyFormat("do {\n" 8635 " do_something();\n" 8636 "} while(something());", 8637 NoSpace); 8638 verifyFormat("switch(x) {\n" 8639 "default:\n" 8640 " break;\n" 8641 "}", 8642 NoSpace); 8643 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace); 8644 verifyFormat("size_t x = sizeof(x);", NoSpace); 8645 verifyFormat("auto f(int x) -> decltype(x);", NoSpace); 8646 verifyFormat("int f(T x) noexcept(x.create());", NoSpace); 8647 verifyFormat("alignas(128) char a[128];", NoSpace); 8648 verifyFormat("size_t x = alignof(MyType);", NoSpace); 8649 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace); 8650 verifyFormat("int f() throw(Deprecated);", NoSpace); 8651 verifyFormat("typedef void (*cb)(int);", NoSpace); 8652 verifyFormat("T A::operator()();", NoSpace); 8653 verifyFormat("X A::operator++(T);", NoSpace); 8654 8655 FormatStyle Space = getLLVMStyle(); 8656 Space.SpaceBeforeParens = FormatStyle::SBPO_Always; 8657 8658 verifyFormat("int f ();", Space); 8659 verifyFormat("void f (int a, T b) {\n" 8660 " while (true)\n" 8661 " continue;\n" 8662 "}", 8663 Space); 8664 verifyFormat("if (true)\n" 8665 " f ();\n" 8666 "else if (true)\n" 8667 " f ();", 8668 Space); 8669 verifyFormat("do {\n" 8670 " do_something ();\n" 8671 "} while (something ());", 8672 Space); 8673 verifyFormat("switch (x) {\n" 8674 "default:\n" 8675 " break;\n" 8676 "}", 8677 Space); 8678 verifyFormat("A::A () : a (1) {}", Space); 8679 verifyFormat("void f () __attribute__ ((asdf));", Space); 8680 verifyFormat("*(&a + 1);\n" 8681 "&((&a)[1]);\n" 8682 "a[(b + c) * d];\n" 8683 "(((a + 1) * 2) + 3) * 4;", 8684 Space); 8685 verifyFormat("#define A(x) x", Space); 8686 verifyFormat("#define A (x) x", Space); 8687 verifyFormat("#if defined(x)\n" 8688 "#endif", 8689 Space); 8690 verifyFormat("auto i = std::make_unique<int> (5);", Space); 8691 verifyFormat("size_t x = sizeof (x);", Space); 8692 verifyFormat("auto f (int x) -> decltype (x);", Space); 8693 verifyFormat("int f (T x) noexcept (x.create ());", Space); 8694 verifyFormat("alignas (128) char a[128];", Space); 8695 verifyFormat("size_t x = alignof (MyType);", Space); 8696 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space); 8697 verifyFormat("int f () throw (Deprecated);", Space); 8698 verifyFormat("typedef void (*cb) (int);", Space); 8699 verifyFormat("T A::operator() ();", Space); 8700 verifyFormat("X A::operator++ (T);", Space); 8701 } 8702 8703 TEST_F(FormatTest, ConfigurableSpacesInParentheses) { 8704 FormatStyle Spaces = getLLVMStyle(); 8705 8706 Spaces.SpacesInParentheses = true; 8707 verifyFormat("call( x, y, z );", Spaces); 8708 verifyFormat("call();", Spaces); 8709 verifyFormat("std::function<void( int, int )> callback;", Spaces); 8710 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }", 8711 Spaces); 8712 verifyFormat("while ( (bool)1 )\n" 8713 " continue;", 8714 Spaces); 8715 verifyFormat("for ( ;; )\n" 8716 " continue;", 8717 Spaces); 8718 verifyFormat("if ( true )\n" 8719 " f();\n" 8720 "else if ( true )\n" 8721 " f();", 8722 Spaces); 8723 verifyFormat("do {\n" 8724 " do_something( (int)i );\n" 8725 "} while ( something() );", 8726 Spaces); 8727 verifyFormat("switch ( x ) {\n" 8728 "default:\n" 8729 " break;\n" 8730 "}", 8731 Spaces); 8732 8733 Spaces.SpacesInParentheses = false; 8734 Spaces.SpacesInCStyleCastParentheses = true; 8735 verifyFormat("Type *A = ( Type * )P;", Spaces); 8736 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces); 8737 verifyFormat("x = ( int32 )y;", Spaces); 8738 verifyFormat("int a = ( int )(2.0f);", Spaces); 8739 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces); 8740 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces); 8741 verifyFormat("#define x (( int )-1)", Spaces); 8742 8743 // Run the first set of tests again with: 8744 Spaces.SpacesInParentheses = false; 8745 Spaces.SpaceInEmptyParentheses = true; 8746 Spaces.SpacesInCStyleCastParentheses = true; 8747 verifyFormat("call(x, y, z);", Spaces); 8748 verifyFormat("call( );", Spaces); 8749 verifyFormat("std::function<void(int, int)> callback;", Spaces); 8750 verifyFormat("while (( bool )1)\n" 8751 " continue;", 8752 Spaces); 8753 verifyFormat("for (;;)\n" 8754 " continue;", 8755 Spaces); 8756 verifyFormat("if (true)\n" 8757 " f( );\n" 8758 "else if (true)\n" 8759 " f( );", 8760 Spaces); 8761 verifyFormat("do {\n" 8762 " do_something(( int )i);\n" 8763 "} while (something( ));", 8764 Spaces); 8765 verifyFormat("switch (x) {\n" 8766 "default:\n" 8767 " break;\n" 8768 "}", 8769 Spaces); 8770 8771 // Run the first set of tests again with: 8772 Spaces.SpaceAfterCStyleCast = true; 8773 verifyFormat("call(x, y, z);", Spaces); 8774 verifyFormat("call( );", Spaces); 8775 verifyFormat("std::function<void(int, int)> callback;", Spaces); 8776 verifyFormat("while (( bool ) 1)\n" 8777 " continue;", 8778 Spaces); 8779 verifyFormat("for (;;)\n" 8780 " continue;", 8781 Spaces); 8782 verifyFormat("if (true)\n" 8783 " f( );\n" 8784 "else if (true)\n" 8785 " f( );", 8786 Spaces); 8787 verifyFormat("do {\n" 8788 " do_something(( int ) i);\n" 8789 "} while (something( ));", 8790 Spaces); 8791 verifyFormat("switch (x) {\n" 8792 "default:\n" 8793 " break;\n" 8794 "}", 8795 Spaces); 8796 8797 // Run subset of tests again with: 8798 Spaces.SpacesInCStyleCastParentheses = false; 8799 Spaces.SpaceAfterCStyleCast = true; 8800 verifyFormat("while ((bool) 1)\n" 8801 " continue;", 8802 Spaces); 8803 verifyFormat("do {\n" 8804 " do_something((int) i);\n" 8805 "} while (something( ));", 8806 Spaces); 8807 } 8808 8809 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) { 8810 verifyFormat("int a[5];"); 8811 verifyFormat("a[3] += 42;"); 8812 8813 FormatStyle Spaces = getLLVMStyle(); 8814 Spaces.SpacesInSquareBrackets = true; 8815 // Lambdas unchanged. 8816 verifyFormat("int c = []() -> int { return 2; }();\n", Spaces); 8817 verifyFormat("return [i, args...] {};", Spaces); 8818 8819 // Not lambdas. 8820 verifyFormat("int a[ 5 ];", Spaces); 8821 verifyFormat("a[ 3 ] += 42;", Spaces); 8822 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces); 8823 verifyFormat("double &operator[](int i) { return 0; }\n" 8824 "int i;", 8825 Spaces); 8826 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces); 8827 verifyFormat("int i = a[ a ][ a ]->f();", Spaces); 8828 verifyFormat("int i = (*b)[ a ]->f();", Spaces); 8829 } 8830 8831 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) { 8832 verifyFormat("int a = 5;"); 8833 verifyFormat("a += 42;"); 8834 verifyFormat("a or_eq 8;"); 8835 8836 FormatStyle Spaces = getLLVMStyle(); 8837 Spaces.SpaceBeforeAssignmentOperators = false; 8838 verifyFormat("int a= 5;", Spaces); 8839 verifyFormat("a+= 42;", Spaces); 8840 verifyFormat("a or_eq 8;", Spaces); 8841 } 8842 8843 TEST_F(FormatTest, AlignConsecutiveAssignments) { 8844 FormatStyle Alignment = getLLVMStyle(); 8845 Alignment.AlignConsecutiveAssignments = false; 8846 verifyFormat("int a = 5;\n" 8847 "int oneTwoThree = 123;", 8848 Alignment); 8849 verifyFormat("int a = 5;\n" 8850 "int oneTwoThree = 123;", 8851 Alignment); 8852 8853 Alignment.AlignConsecutiveAssignments = true; 8854 verifyFormat("int a = 5;\n" 8855 "int oneTwoThree = 123;", 8856 Alignment); 8857 verifyFormat("int a = method();\n" 8858 "int oneTwoThree = 133;", 8859 Alignment); 8860 verifyFormat("a &= 5;\n" 8861 "bcd *= 5;\n" 8862 "ghtyf += 5;\n" 8863 "dvfvdb -= 5;\n" 8864 "a /= 5;\n" 8865 "vdsvsv %= 5;\n" 8866 "sfdbddfbdfbb ^= 5;\n" 8867 "dvsdsv |= 5;\n" 8868 "int dsvvdvsdvvv = 123;", 8869 Alignment); 8870 verifyFormat("int i = 1, j = 10;\n" 8871 "something = 2000;", 8872 Alignment); 8873 verifyFormat("something = 2000;\n" 8874 "int i = 1, j = 10;\n", 8875 Alignment); 8876 verifyFormat("something = 2000;\n" 8877 "another = 911;\n" 8878 "int i = 1, j = 10;\n" 8879 "oneMore = 1;\n" 8880 "i = 2;", 8881 Alignment); 8882 verifyFormat("int a = 5;\n" 8883 "int one = 1;\n" 8884 "method();\n" 8885 "int oneTwoThree = 123;\n" 8886 "int oneTwo = 12;", 8887 Alignment); 8888 verifyFormat("int oneTwoThree = 123;\n" 8889 "int oneTwo = 12;\n" 8890 "method();\n", 8891 Alignment); 8892 verifyFormat("int oneTwoThree = 123; // comment\n" 8893 "int oneTwo = 12; // comment", 8894 Alignment); 8895 EXPECT_EQ("int a = 5;\n" 8896 "\n" 8897 "int oneTwoThree = 123;", 8898 format("int a = 5;\n" 8899 "\n" 8900 "int oneTwoThree= 123;", 8901 Alignment)); 8902 EXPECT_EQ("int a = 5;\n" 8903 "int one = 1;\n" 8904 "\n" 8905 "int oneTwoThree = 123;", 8906 format("int a = 5;\n" 8907 "int one = 1;\n" 8908 "\n" 8909 "int oneTwoThree = 123;", 8910 Alignment)); 8911 EXPECT_EQ("int a = 5;\n" 8912 "int one = 1;\n" 8913 "\n" 8914 "int oneTwoThree = 123;\n" 8915 "int oneTwo = 12;", 8916 format("int a = 5;\n" 8917 "int one = 1;\n" 8918 "\n" 8919 "int oneTwoThree = 123;\n" 8920 "int oneTwo = 12;", 8921 Alignment)); 8922 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 8923 verifyFormat("#define A \\\n" 8924 " int aaaa = 12; \\\n" 8925 " int b = 23; \\\n" 8926 " int ccc = 234; \\\n" 8927 " int dddddddddd = 2345;", 8928 Alignment); 8929 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 8930 verifyFormat("#define A \\\n" 8931 " int aaaa = 12; \\\n" 8932 " int b = 23; \\\n" 8933 " int ccc = 234; \\\n" 8934 " int dddddddddd = 2345;", 8935 Alignment); 8936 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 8937 verifyFormat("#define A " 8938 " \\\n" 8939 " int aaaa = 12; " 8940 " \\\n" 8941 " int b = 23; " 8942 " \\\n" 8943 " int ccc = 234; " 8944 " \\\n" 8945 " int dddddddddd = 2345;", 8946 Alignment); 8947 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 8948 "k = 4, int l = 5,\n" 8949 " int m = 6) {\n" 8950 " int j = 10;\n" 8951 " otherThing = 1;\n" 8952 "}", 8953 Alignment); 8954 verifyFormat("void SomeFunction(int parameter = 0) {\n" 8955 " int i = 1;\n" 8956 " int j = 2;\n" 8957 " int big = 10000;\n" 8958 "}", 8959 Alignment); 8960 verifyFormat("class C {\n" 8961 "public:\n" 8962 " int i = 1;\n" 8963 " virtual void f() = 0;\n" 8964 "};", 8965 Alignment); 8966 verifyFormat("int i = 1;\n" 8967 "if (SomeType t = getSomething()) {\n" 8968 "}\n" 8969 "int j = 2;\n" 8970 "int big = 10000;", 8971 Alignment); 8972 verifyFormat("int j = 7;\n" 8973 "for (int k = 0; k < N; ++k) {\n" 8974 "}\n" 8975 "int j = 2;\n" 8976 "int big = 10000;\n" 8977 "}", 8978 Alignment); 8979 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 8980 verifyFormat("int i = 1;\n" 8981 "LooooooooooongType loooooooooooooooooooooongVariable\n" 8982 " = someLooooooooooooooooongFunction();\n" 8983 "int j = 2;", 8984 Alignment); 8985 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 8986 verifyFormat("int i = 1;\n" 8987 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 8988 " someLooooooooooooooooongFunction();\n" 8989 "int j = 2;", 8990 Alignment); 8991 8992 verifyFormat("auto lambda = []() {\n" 8993 " auto i = 0;\n" 8994 " return 0;\n" 8995 "};\n" 8996 "int i = 0;\n" 8997 "auto v = type{\n" 8998 " i = 1, //\n" 8999 " (i = 2), //\n" 9000 " i = 3 //\n" 9001 "};", 9002 Alignment); 9003 9004 verifyFormat( 9005 "int i = 1;\n" 9006 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 9007 " loooooooooooooooooooooongParameterB);\n" 9008 "int j = 2;", 9009 Alignment); 9010 9011 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 9012 " typename B = very_long_type_name_1,\n" 9013 " typename T_2 = very_long_type_name_2>\n" 9014 "auto foo() {}\n", 9015 Alignment); 9016 verifyFormat("int a, b = 1;\n" 9017 "int c = 2;\n" 9018 "int dd = 3;\n", 9019 Alignment); 9020 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 9021 "float b[1][] = {{3.f}};\n", 9022 Alignment); 9023 verifyFormat("for (int i = 0; i < 1; i++)\n" 9024 " int x = 1;\n", 9025 Alignment); 9026 verifyFormat("for (i = 0; i < 1; i++)\n" 9027 " x = 1;\n" 9028 "y = 1;\n", 9029 Alignment); 9030 } 9031 9032 TEST_F(FormatTest, AlignConsecutiveDeclarations) { 9033 FormatStyle Alignment = getLLVMStyle(); 9034 Alignment.AlignConsecutiveDeclarations = false; 9035 verifyFormat("float const a = 5;\n" 9036 "int oneTwoThree = 123;", 9037 Alignment); 9038 verifyFormat("int a = 5;\n" 9039 "float const oneTwoThree = 123;", 9040 Alignment); 9041 9042 Alignment.AlignConsecutiveDeclarations = true; 9043 verifyFormat("float const a = 5;\n" 9044 "int oneTwoThree = 123;", 9045 Alignment); 9046 verifyFormat("int a = method();\n" 9047 "float const oneTwoThree = 133;", 9048 Alignment); 9049 verifyFormat("int i = 1, j = 10;\n" 9050 "something = 2000;", 9051 Alignment); 9052 verifyFormat("something = 2000;\n" 9053 "int i = 1, j = 10;\n", 9054 Alignment); 9055 verifyFormat("float something = 2000;\n" 9056 "double another = 911;\n" 9057 "int i = 1, j = 10;\n" 9058 "const int *oneMore = 1;\n" 9059 "unsigned i = 2;", 9060 Alignment); 9061 verifyFormat("float a = 5;\n" 9062 "int one = 1;\n" 9063 "method();\n" 9064 "const double oneTwoThree = 123;\n" 9065 "const unsigned int oneTwo = 12;", 9066 Alignment); 9067 verifyFormat("int oneTwoThree{0}; // comment\n" 9068 "unsigned oneTwo; // comment", 9069 Alignment); 9070 EXPECT_EQ("float const a = 5;\n" 9071 "\n" 9072 "int oneTwoThree = 123;", 9073 format("float const a = 5;\n" 9074 "\n" 9075 "int oneTwoThree= 123;", 9076 Alignment)); 9077 EXPECT_EQ("float a = 5;\n" 9078 "int one = 1;\n" 9079 "\n" 9080 "unsigned oneTwoThree = 123;", 9081 format("float a = 5;\n" 9082 "int one = 1;\n" 9083 "\n" 9084 "unsigned oneTwoThree = 123;", 9085 Alignment)); 9086 EXPECT_EQ("float a = 5;\n" 9087 "int one = 1;\n" 9088 "\n" 9089 "unsigned oneTwoThree = 123;\n" 9090 "int oneTwo = 12;", 9091 format("float a = 5;\n" 9092 "int one = 1;\n" 9093 "\n" 9094 "unsigned oneTwoThree = 123;\n" 9095 "int oneTwo = 12;", 9096 Alignment)); 9097 // Function prototype alignment 9098 verifyFormat("int a();\n" 9099 "double b();", 9100 Alignment); 9101 verifyFormat("int a(int x);\n" 9102 "double b();", 9103 Alignment); 9104 unsigned OldColumnLimit = Alignment.ColumnLimit; 9105 // We need to set ColumnLimit to zero, in order to stress nested alignments, 9106 // otherwise the function parameters will be re-flowed onto a single line. 9107 Alignment.ColumnLimit = 0; 9108 EXPECT_EQ("int a(int x,\n" 9109 " float y);\n" 9110 "double b(int x,\n" 9111 " double y);", 9112 format("int a(int x,\n" 9113 " float y);\n" 9114 "double b(int x,\n" 9115 " double y);", 9116 Alignment)); 9117 // This ensures that function parameters of function declarations are 9118 // correctly indented when their owning functions are indented. 9119 // The failure case here is for 'double y' to not be indented enough. 9120 EXPECT_EQ("double a(int x);\n" 9121 "int b(int y,\n" 9122 " double z);", 9123 format("double a(int x);\n" 9124 "int b(int y,\n" 9125 " double z);", 9126 Alignment)); 9127 // Set ColumnLimit low so that we induce wrapping immediately after 9128 // the function name and opening paren. 9129 Alignment.ColumnLimit = 13; 9130 verifyFormat("int function(\n" 9131 " int x,\n" 9132 " bool y);", 9133 Alignment); 9134 Alignment.ColumnLimit = OldColumnLimit; 9135 // Ensure function pointers don't screw up recursive alignment 9136 verifyFormat("int a(int x, void (*fp)(int y));\n" 9137 "double b();", 9138 Alignment); 9139 Alignment.AlignConsecutiveAssignments = true; 9140 // Ensure recursive alignment is broken by function braces, so that the 9141 // "a = 1" does not align with subsequent assignments inside the function 9142 // body. 9143 verifyFormat("int func(int a = 1) {\n" 9144 " int b = 2;\n" 9145 " int cc = 3;\n" 9146 "}", 9147 Alignment); 9148 verifyFormat("float something = 2000;\n" 9149 "double another = 911;\n" 9150 "int i = 1, j = 10;\n" 9151 "const int *oneMore = 1;\n" 9152 "unsigned i = 2;", 9153 Alignment); 9154 verifyFormat("int oneTwoThree = {0}; // comment\n" 9155 "unsigned oneTwo = 0; // comment", 9156 Alignment); 9157 // Make sure that scope is correctly tracked, in the absence of braces 9158 verifyFormat("for (int i = 0; i < n; i++)\n" 9159 " j = i;\n" 9160 "double x = 1;\n", 9161 Alignment); 9162 verifyFormat("if (int i = 0)\n" 9163 " j = i;\n" 9164 "double x = 1;\n", 9165 Alignment); 9166 // Ensure operator[] and operator() are comprehended 9167 verifyFormat("struct test {\n" 9168 " long long int foo();\n" 9169 " int operator[](int a);\n" 9170 " double bar();\n" 9171 "};\n", 9172 Alignment); 9173 verifyFormat("struct test {\n" 9174 " long long int foo();\n" 9175 " int operator()(int a);\n" 9176 " double bar();\n" 9177 "};\n", 9178 Alignment); 9179 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 9180 " int const i = 1;\n" 9181 " int * j = 2;\n" 9182 " int big = 10000;\n" 9183 "\n" 9184 " unsigned oneTwoThree = 123;\n" 9185 " int oneTwo = 12;\n" 9186 " method();\n" 9187 " float k = 2;\n" 9188 " int ll = 10000;\n" 9189 "}", 9190 format("void SomeFunction(int parameter= 0) {\n" 9191 " int const i= 1;\n" 9192 " int *j=2;\n" 9193 " int big = 10000;\n" 9194 "\n" 9195 "unsigned oneTwoThree =123;\n" 9196 "int oneTwo = 12;\n" 9197 " method();\n" 9198 "float k= 2;\n" 9199 "int ll=10000;\n" 9200 "}", 9201 Alignment)); 9202 Alignment.AlignConsecutiveAssignments = false; 9203 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 9204 verifyFormat("#define A \\\n" 9205 " int aaaa = 12; \\\n" 9206 " float b = 23; \\\n" 9207 " const int ccc = 234; \\\n" 9208 " unsigned dddddddddd = 2345;", 9209 Alignment); 9210 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 9211 verifyFormat("#define A \\\n" 9212 " int aaaa = 12; \\\n" 9213 " float b = 23; \\\n" 9214 " const int ccc = 234; \\\n" 9215 " unsigned dddddddddd = 2345;", 9216 Alignment); 9217 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 9218 Alignment.ColumnLimit = 30; 9219 verifyFormat("#define A \\\n" 9220 " int aaaa = 12; \\\n" 9221 " float b = 23; \\\n" 9222 " const int ccc = 234; \\\n" 9223 " int dddddddddd = 2345;", 9224 Alignment); 9225 Alignment.ColumnLimit = 80; 9226 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 9227 "k = 4, int l = 5,\n" 9228 " int m = 6) {\n" 9229 " const int j = 10;\n" 9230 " otherThing = 1;\n" 9231 "}", 9232 Alignment); 9233 verifyFormat("void SomeFunction(int parameter = 0) {\n" 9234 " int const i = 1;\n" 9235 " int * j = 2;\n" 9236 " int big = 10000;\n" 9237 "}", 9238 Alignment); 9239 verifyFormat("class C {\n" 9240 "public:\n" 9241 " int i = 1;\n" 9242 " virtual void f() = 0;\n" 9243 "};", 9244 Alignment); 9245 verifyFormat("float i = 1;\n" 9246 "if (SomeType t = getSomething()) {\n" 9247 "}\n" 9248 "const unsigned j = 2;\n" 9249 "int big = 10000;", 9250 Alignment); 9251 verifyFormat("float j = 7;\n" 9252 "for (int k = 0; k < N; ++k) {\n" 9253 "}\n" 9254 "unsigned j = 2;\n" 9255 "int big = 10000;\n" 9256 "}", 9257 Alignment); 9258 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 9259 verifyFormat("float i = 1;\n" 9260 "LooooooooooongType loooooooooooooooooooooongVariable\n" 9261 " = someLooooooooooooooooongFunction();\n" 9262 "int j = 2;", 9263 Alignment); 9264 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 9265 verifyFormat("int i = 1;\n" 9266 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 9267 " someLooooooooooooooooongFunction();\n" 9268 "int j = 2;", 9269 Alignment); 9270 9271 Alignment.AlignConsecutiveAssignments = true; 9272 verifyFormat("auto lambda = []() {\n" 9273 " auto ii = 0;\n" 9274 " float j = 0;\n" 9275 " return 0;\n" 9276 "};\n" 9277 "int i = 0;\n" 9278 "float i2 = 0;\n" 9279 "auto v = type{\n" 9280 " i = 1, //\n" 9281 " (i = 2), //\n" 9282 " i = 3 //\n" 9283 "};", 9284 Alignment); 9285 Alignment.AlignConsecutiveAssignments = false; 9286 9287 verifyFormat( 9288 "int i = 1;\n" 9289 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 9290 " loooooooooooooooooooooongParameterB);\n" 9291 "int j = 2;", 9292 Alignment); 9293 9294 // Test interactions with ColumnLimit and AlignConsecutiveAssignments: 9295 // We expect declarations and assignments to align, as long as it doesn't 9296 // exceed the column limit, starting a new alignment sequence whenever it 9297 // happens. 9298 Alignment.AlignConsecutiveAssignments = true; 9299 Alignment.ColumnLimit = 30; 9300 verifyFormat("float ii = 1;\n" 9301 "unsigned j = 2;\n" 9302 "int someVerylongVariable = 1;\n" 9303 "AnotherLongType ll = 123456;\n" 9304 "VeryVeryLongType k = 2;\n" 9305 "int myvar = 1;", 9306 Alignment); 9307 Alignment.ColumnLimit = 80; 9308 Alignment.AlignConsecutiveAssignments = false; 9309 9310 verifyFormat( 9311 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n" 9312 " typename LongType, typename B>\n" 9313 "auto foo() {}\n", 9314 Alignment); 9315 verifyFormat("float a, b = 1;\n" 9316 "int c = 2;\n" 9317 "int dd = 3;\n", 9318 Alignment); 9319 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 9320 "float b[1][] = {{3.f}};\n", 9321 Alignment); 9322 Alignment.AlignConsecutiveAssignments = true; 9323 verifyFormat("float a, b = 1;\n" 9324 "int c = 2;\n" 9325 "int dd = 3;\n", 9326 Alignment); 9327 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 9328 "float b[1][] = {{3.f}};\n", 9329 Alignment); 9330 Alignment.AlignConsecutiveAssignments = false; 9331 9332 Alignment.ColumnLimit = 30; 9333 Alignment.BinPackParameters = false; 9334 verifyFormat("void foo(float a,\n" 9335 " float b,\n" 9336 " int c,\n" 9337 " uint32_t *d) {\n" 9338 " int * e = 0;\n" 9339 " float f = 0;\n" 9340 " double g = 0;\n" 9341 "}\n" 9342 "void bar(ino_t a,\n" 9343 " int b,\n" 9344 " uint32_t *c,\n" 9345 " bool d) {}\n", 9346 Alignment); 9347 Alignment.BinPackParameters = true; 9348 Alignment.ColumnLimit = 80; 9349 9350 // Bug 33507 9351 Alignment.PointerAlignment = FormatStyle::PAS_Middle; 9352 verifyFormat( 9353 "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n" 9354 " static const Version verVs2017;\n" 9355 " return true;\n" 9356 "});\n", 9357 Alignment); 9358 Alignment.PointerAlignment = FormatStyle::PAS_Right; 9359 } 9360 9361 TEST_F(FormatTest, LinuxBraceBreaking) { 9362 FormatStyle LinuxBraceStyle = getLLVMStyle(); 9363 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux; 9364 verifyFormat("namespace a\n" 9365 "{\n" 9366 "class A\n" 9367 "{\n" 9368 " void f()\n" 9369 " {\n" 9370 " if (true) {\n" 9371 " a();\n" 9372 " b();\n" 9373 " } else {\n" 9374 " a();\n" 9375 " }\n" 9376 " }\n" 9377 " void g() { return; }\n" 9378 "};\n" 9379 "struct B {\n" 9380 " int x;\n" 9381 "};\n" 9382 "} // namespace a\n", 9383 LinuxBraceStyle); 9384 verifyFormat("enum X {\n" 9385 " Y = 0,\n" 9386 "}\n", 9387 LinuxBraceStyle); 9388 verifyFormat("struct S {\n" 9389 " int Type;\n" 9390 " union {\n" 9391 " int x;\n" 9392 " double y;\n" 9393 " } Value;\n" 9394 " class C\n" 9395 " {\n" 9396 " MyFavoriteType Value;\n" 9397 " } Class;\n" 9398 "}\n", 9399 LinuxBraceStyle); 9400 } 9401 9402 TEST_F(FormatTest, MozillaBraceBreaking) { 9403 FormatStyle MozillaBraceStyle = getLLVMStyle(); 9404 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; 9405 MozillaBraceStyle.FixNamespaceComments = false; 9406 verifyFormat("namespace a {\n" 9407 "class A\n" 9408 "{\n" 9409 " void f()\n" 9410 " {\n" 9411 " if (true) {\n" 9412 " a();\n" 9413 " b();\n" 9414 " }\n" 9415 " }\n" 9416 " void g() { return; }\n" 9417 "};\n" 9418 "enum E\n" 9419 "{\n" 9420 " A,\n" 9421 " // foo\n" 9422 " B,\n" 9423 " C\n" 9424 "};\n" 9425 "struct B\n" 9426 "{\n" 9427 " int x;\n" 9428 "};\n" 9429 "}\n", 9430 MozillaBraceStyle); 9431 verifyFormat("struct S\n" 9432 "{\n" 9433 " int Type;\n" 9434 " union\n" 9435 " {\n" 9436 " int x;\n" 9437 " double y;\n" 9438 " } Value;\n" 9439 " class C\n" 9440 " {\n" 9441 " MyFavoriteType Value;\n" 9442 " } Class;\n" 9443 "}\n", 9444 MozillaBraceStyle); 9445 } 9446 9447 TEST_F(FormatTest, StroustrupBraceBreaking) { 9448 FormatStyle StroustrupBraceStyle = getLLVMStyle(); 9449 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 9450 verifyFormat("namespace a {\n" 9451 "class A {\n" 9452 " void f()\n" 9453 " {\n" 9454 " if (true) {\n" 9455 " a();\n" 9456 " b();\n" 9457 " }\n" 9458 " }\n" 9459 " void g() { return; }\n" 9460 "};\n" 9461 "struct B {\n" 9462 " int x;\n" 9463 "};\n" 9464 "} // namespace a\n", 9465 StroustrupBraceStyle); 9466 9467 verifyFormat("void foo()\n" 9468 "{\n" 9469 " if (a) {\n" 9470 " a();\n" 9471 " }\n" 9472 " else {\n" 9473 " b();\n" 9474 " }\n" 9475 "}\n", 9476 StroustrupBraceStyle); 9477 9478 verifyFormat("#ifdef _DEBUG\n" 9479 "int foo(int i = 0)\n" 9480 "#else\n" 9481 "int foo(int i = 5)\n" 9482 "#endif\n" 9483 "{\n" 9484 " return i;\n" 9485 "}", 9486 StroustrupBraceStyle); 9487 9488 verifyFormat("void foo() {}\n" 9489 "void bar()\n" 9490 "#ifdef _DEBUG\n" 9491 "{\n" 9492 " foo();\n" 9493 "}\n" 9494 "#else\n" 9495 "{\n" 9496 "}\n" 9497 "#endif", 9498 StroustrupBraceStyle); 9499 9500 verifyFormat("void foobar() { int i = 5; }\n" 9501 "#ifdef _DEBUG\n" 9502 "void bar() {}\n" 9503 "#else\n" 9504 "void bar() { foobar(); }\n" 9505 "#endif", 9506 StroustrupBraceStyle); 9507 } 9508 9509 TEST_F(FormatTest, AllmanBraceBreaking) { 9510 FormatStyle AllmanBraceStyle = getLLVMStyle(); 9511 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman; 9512 9513 EXPECT_EQ("namespace a\n" 9514 "{\n" 9515 "void f();\n" 9516 "void g();\n" 9517 "} // namespace a\n", 9518 format("namespace a\n" 9519 "{\n" 9520 "void f();\n" 9521 "void g();\n" 9522 "}\n", 9523 AllmanBraceStyle)); 9524 9525 verifyFormat("namespace a\n" 9526 "{\n" 9527 "class A\n" 9528 "{\n" 9529 " void f()\n" 9530 " {\n" 9531 " if (true)\n" 9532 " {\n" 9533 " a();\n" 9534 " b();\n" 9535 " }\n" 9536 " }\n" 9537 " void g() { return; }\n" 9538 "};\n" 9539 "struct B\n" 9540 "{\n" 9541 " int x;\n" 9542 "};\n" 9543 "} // namespace a", 9544 AllmanBraceStyle); 9545 9546 verifyFormat("void f()\n" 9547 "{\n" 9548 " if (true)\n" 9549 " {\n" 9550 " a();\n" 9551 " }\n" 9552 " else if (false)\n" 9553 " {\n" 9554 " b();\n" 9555 " }\n" 9556 " else\n" 9557 " {\n" 9558 " c();\n" 9559 " }\n" 9560 "}\n", 9561 AllmanBraceStyle); 9562 9563 verifyFormat("void f()\n" 9564 "{\n" 9565 " for (int i = 0; i < 10; ++i)\n" 9566 " {\n" 9567 " a();\n" 9568 " }\n" 9569 " while (false)\n" 9570 " {\n" 9571 " b();\n" 9572 " }\n" 9573 " do\n" 9574 " {\n" 9575 " c();\n" 9576 " } while (false)\n" 9577 "}\n", 9578 AllmanBraceStyle); 9579 9580 verifyFormat("void f(int a)\n" 9581 "{\n" 9582 " switch (a)\n" 9583 " {\n" 9584 " case 0:\n" 9585 " break;\n" 9586 " case 1:\n" 9587 " {\n" 9588 " break;\n" 9589 " }\n" 9590 " case 2:\n" 9591 " {\n" 9592 " }\n" 9593 " break;\n" 9594 " default:\n" 9595 " break;\n" 9596 " }\n" 9597 "}\n", 9598 AllmanBraceStyle); 9599 9600 verifyFormat("enum X\n" 9601 "{\n" 9602 " Y = 0,\n" 9603 "}\n", 9604 AllmanBraceStyle); 9605 verifyFormat("enum X\n" 9606 "{\n" 9607 " Y = 0\n" 9608 "}\n", 9609 AllmanBraceStyle); 9610 9611 verifyFormat("@interface BSApplicationController ()\n" 9612 "{\n" 9613 "@private\n" 9614 " id _extraIvar;\n" 9615 "}\n" 9616 "@end\n", 9617 AllmanBraceStyle); 9618 9619 verifyFormat("#ifdef _DEBUG\n" 9620 "int foo(int i = 0)\n" 9621 "#else\n" 9622 "int foo(int i = 5)\n" 9623 "#endif\n" 9624 "{\n" 9625 " return i;\n" 9626 "}", 9627 AllmanBraceStyle); 9628 9629 verifyFormat("void foo() {}\n" 9630 "void bar()\n" 9631 "#ifdef _DEBUG\n" 9632 "{\n" 9633 " foo();\n" 9634 "}\n" 9635 "#else\n" 9636 "{\n" 9637 "}\n" 9638 "#endif", 9639 AllmanBraceStyle); 9640 9641 verifyFormat("void foobar() { int i = 5; }\n" 9642 "#ifdef _DEBUG\n" 9643 "void bar() {}\n" 9644 "#else\n" 9645 "void bar() { foobar(); }\n" 9646 "#endif", 9647 AllmanBraceStyle); 9648 9649 // This shouldn't affect ObjC blocks.. 9650 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 9651 " // ...\n" 9652 " int i;\n" 9653 "}];", 9654 AllmanBraceStyle); 9655 verifyFormat("void (^block)(void) = ^{\n" 9656 " // ...\n" 9657 " int i;\n" 9658 "};", 9659 AllmanBraceStyle); 9660 // .. or dict literals. 9661 verifyFormat("void f()\n" 9662 "{\n" 9663 " // ...\n" 9664 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 9665 "}", 9666 AllmanBraceStyle); 9667 verifyFormat("void f()\n" 9668 "{\n" 9669 " // ...\n" 9670 " [object someMethod:@{a : @\"b\"}];\n" 9671 "}", 9672 AllmanBraceStyle); 9673 verifyFormat("int f()\n" 9674 "{ // comment\n" 9675 " return 42;\n" 9676 "}", 9677 AllmanBraceStyle); 9678 9679 AllmanBraceStyle.ColumnLimit = 19; 9680 verifyFormat("void f() { int i; }", AllmanBraceStyle); 9681 AllmanBraceStyle.ColumnLimit = 18; 9682 verifyFormat("void f()\n" 9683 "{\n" 9684 " int i;\n" 9685 "}", 9686 AllmanBraceStyle); 9687 AllmanBraceStyle.ColumnLimit = 80; 9688 9689 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle; 9690 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true; 9691 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 9692 verifyFormat("void f(bool b)\n" 9693 "{\n" 9694 " if (b)\n" 9695 " {\n" 9696 " return;\n" 9697 " }\n" 9698 "}\n", 9699 BreakBeforeBraceShortIfs); 9700 verifyFormat("void f(bool b)\n" 9701 "{\n" 9702 " if constexpr (b)\n" 9703 " {\n" 9704 " return;\n" 9705 " }\n" 9706 "}\n", 9707 BreakBeforeBraceShortIfs); 9708 verifyFormat("void f(bool b)\n" 9709 "{\n" 9710 " if (b) return;\n" 9711 "}\n", 9712 BreakBeforeBraceShortIfs); 9713 verifyFormat("void f(bool b)\n" 9714 "{\n" 9715 " if constexpr (b) return;\n" 9716 "}\n", 9717 BreakBeforeBraceShortIfs); 9718 verifyFormat("void f(bool b)\n" 9719 "{\n" 9720 " while (b)\n" 9721 " {\n" 9722 " return;\n" 9723 " }\n" 9724 "}\n", 9725 BreakBeforeBraceShortIfs); 9726 } 9727 9728 TEST_F(FormatTest, GNUBraceBreaking) { 9729 FormatStyle GNUBraceStyle = getLLVMStyle(); 9730 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU; 9731 verifyFormat("namespace a\n" 9732 "{\n" 9733 "class A\n" 9734 "{\n" 9735 " void f()\n" 9736 " {\n" 9737 " int a;\n" 9738 " {\n" 9739 " int b;\n" 9740 " }\n" 9741 " if (true)\n" 9742 " {\n" 9743 " a();\n" 9744 " b();\n" 9745 " }\n" 9746 " }\n" 9747 " void g() { return; }\n" 9748 "}\n" 9749 "} // namespace a", 9750 GNUBraceStyle); 9751 9752 verifyFormat("void f()\n" 9753 "{\n" 9754 " if (true)\n" 9755 " {\n" 9756 " a();\n" 9757 " }\n" 9758 " else if (false)\n" 9759 " {\n" 9760 " b();\n" 9761 " }\n" 9762 " else\n" 9763 " {\n" 9764 " c();\n" 9765 " }\n" 9766 "}\n", 9767 GNUBraceStyle); 9768 9769 verifyFormat("void f()\n" 9770 "{\n" 9771 " for (int i = 0; i < 10; ++i)\n" 9772 " {\n" 9773 " a();\n" 9774 " }\n" 9775 " while (false)\n" 9776 " {\n" 9777 " b();\n" 9778 " }\n" 9779 " do\n" 9780 " {\n" 9781 " c();\n" 9782 " }\n" 9783 " while (false);\n" 9784 "}\n", 9785 GNUBraceStyle); 9786 9787 verifyFormat("void f(int a)\n" 9788 "{\n" 9789 " switch (a)\n" 9790 " {\n" 9791 " case 0:\n" 9792 " break;\n" 9793 " case 1:\n" 9794 " {\n" 9795 " break;\n" 9796 " }\n" 9797 " case 2:\n" 9798 " {\n" 9799 " }\n" 9800 " break;\n" 9801 " default:\n" 9802 " break;\n" 9803 " }\n" 9804 "}\n", 9805 GNUBraceStyle); 9806 9807 verifyFormat("enum X\n" 9808 "{\n" 9809 " Y = 0,\n" 9810 "}\n", 9811 GNUBraceStyle); 9812 9813 verifyFormat("@interface BSApplicationController ()\n" 9814 "{\n" 9815 "@private\n" 9816 " id _extraIvar;\n" 9817 "}\n" 9818 "@end\n", 9819 GNUBraceStyle); 9820 9821 verifyFormat("#ifdef _DEBUG\n" 9822 "int foo(int i = 0)\n" 9823 "#else\n" 9824 "int foo(int i = 5)\n" 9825 "#endif\n" 9826 "{\n" 9827 " return i;\n" 9828 "}", 9829 GNUBraceStyle); 9830 9831 verifyFormat("void foo() {}\n" 9832 "void bar()\n" 9833 "#ifdef _DEBUG\n" 9834 "{\n" 9835 " foo();\n" 9836 "}\n" 9837 "#else\n" 9838 "{\n" 9839 "}\n" 9840 "#endif", 9841 GNUBraceStyle); 9842 9843 verifyFormat("void foobar() { int i = 5; }\n" 9844 "#ifdef _DEBUG\n" 9845 "void bar() {}\n" 9846 "#else\n" 9847 "void bar() { foobar(); }\n" 9848 "#endif", 9849 GNUBraceStyle); 9850 } 9851 9852 TEST_F(FormatTest, WebKitBraceBreaking) { 9853 FormatStyle WebKitBraceStyle = getLLVMStyle(); 9854 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit; 9855 WebKitBraceStyle.FixNamespaceComments = false; 9856 verifyFormat("namespace a {\n" 9857 "class A {\n" 9858 " void f()\n" 9859 " {\n" 9860 " if (true) {\n" 9861 " a();\n" 9862 " b();\n" 9863 " }\n" 9864 " }\n" 9865 " void g() { return; }\n" 9866 "};\n" 9867 "enum E {\n" 9868 " A,\n" 9869 " // foo\n" 9870 " B,\n" 9871 " C\n" 9872 "};\n" 9873 "struct B {\n" 9874 " int x;\n" 9875 "};\n" 9876 "}\n", 9877 WebKitBraceStyle); 9878 verifyFormat("struct S {\n" 9879 " int Type;\n" 9880 " union {\n" 9881 " int x;\n" 9882 " double y;\n" 9883 " } Value;\n" 9884 " class C {\n" 9885 " MyFavoriteType Value;\n" 9886 " } Class;\n" 9887 "};\n", 9888 WebKitBraceStyle); 9889 } 9890 9891 TEST_F(FormatTest, CatchExceptionReferenceBinding) { 9892 verifyFormat("void f() {\n" 9893 " try {\n" 9894 " } catch (const Exception &e) {\n" 9895 " }\n" 9896 "}\n", 9897 getLLVMStyle()); 9898 } 9899 9900 TEST_F(FormatTest, UnderstandsPragmas) { 9901 verifyFormat("#pragma omp reduction(| : var)"); 9902 verifyFormat("#pragma omp reduction(+ : var)"); 9903 9904 EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string " 9905 "(including parentheses).", 9906 format("#pragma mark Any non-hyphenated or hyphenated string " 9907 "(including parentheses).")); 9908 } 9909 9910 TEST_F(FormatTest, UnderstandPragmaOption) { 9911 verifyFormat("#pragma option -C -A"); 9912 9913 EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A")); 9914 } 9915 9916 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) { 9917 FormatStyle Style = getLLVMStyle(); 9918 Style.ColumnLimit = 20; 9919 9920 verifyFormat("int a; // the\n" 9921 " // comment", Style); 9922 EXPECT_EQ("int a; /* first line\n" 9923 " * second\n" 9924 " * line third\n" 9925 " * line\n" 9926 " */", 9927 format("int a; /* first line\n" 9928 " * second\n" 9929 " * line third\n" 9930 " * line\n" 9931 " */", 9932 Style)); 9933 EXPECT_EQ("int a; // first line\n" 9934 " // second\n" 9935 " // line third\n" 9936 " // line", 9937 format("int a; // first line\n" 9938 " // second line\n" 9939 " // third line", 9940 Style)); 9941 9942 Style.PenaltyExcessCharacter = 90; 9943 verifyFormat("int a; // the comment", Style); 9944 EXPECT_EQ("int a; // the comment\n" 9945 " // aaa", 9946 format("int a; // the comment aaa", Style)); 9947 EXPECT_EQ("int a; /* first line\n" 9948 " * second line\n" 9949 " * third line\n" 9950 " */", 9951 format("int a; /* first line\n" 9952 " * second line\n" 9953 " * third line\n" 9954 " */", 9955 Style)); 9956 EXPECT_EQ("int a; // first line\n" 9957 " // second line\n" 9958 " // third line", 9959 format("int a; // first line\n" 9960 " // second line\n" 9961 " // third line", 9962 Style)); 9963 // FIXME: Investigate why this is not getting the same layout as the test 9964 // above. 9965 EXPECT_EQ("int a; /* first line\n" 9966 " * second line\n" 9967 " * third line\n" 9968 " */", 9969 format("int a; /* first line second line third line" 9970 "\n*/", 9971 Style)); 9972 9973 EXPECT_EQ("// foo bar baz bazfoo\n" 9974 "// foo bar foo bar\n", 9975 format("// foo bar baz bazfoo\n" 9976 "// foo bar foo bar\n", 9977 Style)); 9978 EXPECT_EQ("// foo bar baz bazfoo\n" 9979 "// foo bar foo bar\n", 9980 format("// foo bar baz bazfoo\n" 9981 "// foo bar foo bar\n", 9982 Style)); 9983 9984 // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the 9985 // next one. 9986 EXPECT_EQ("// foo bar baz bazfoo\n" 9987 "// bar foo bar\n", 9988 format("// foo bar baz bazfoo bar\n" 9989 "// foo bar\n", 9990 Style)); 9991 9992 EXPECT_EQ("// foo bar baz bazfoo\n" 9993 "// foo bar baz bazfoo\n" 9994 "// bar foo bar\n", 9995 format("// foo bar baz bazfoo\n" 9996 "// foo bar baz bazfoo bar\n" 9997 "// foo bar\n", 9998 Style)); 9999 10000 EXPECT_EQ("// foo bar baz bazfoo\n" 10001 "// foo bar baz bazfoo\n" 10002 "// bar foo bar\n", 10003 format("// foo bar baz bazfoo\n" 10004 "// foo bar baz bazfoo bar\n" 10005 "// foo bar\n", 10006 Style)); 10007 10008 // Make sure we do not keep protruding characters if strict mode reflow is 10009 // cheaper than keeping protruding characters. 10010 Style.ColumnLimit = 21; 10011 EXPECT_EQ("// foo foo foo foo\n" 10012 "// foo foo foo foo\n" 10013 "// foo foo foo foo\n", 10014 format("// foo foo foo foo foo foo foo foo foo foo foo foo\n", 10015 Style)); 10016 10017 EXPECT_EQ("int a = /* long block\n" 10018 " comment */\n" 10019 " 42;", 10020 format("int a = /* long block comment */ 42;", Style)); 10021 } 10022 10023 #define EXPECT_ALL_STYLES_EQUAL(Styles) \ 10024 for (size_t i = 1; i < Styles.size(); ++i) \ 10025 EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \ 10026 << " differs from Style #0" 10027 10028 TEST_F(FormatTest, GetsPredefinedStyleByName) { 10029 SmallVector<FormatStyle, 3> Styles; 10030 Styles.resize(3); 10031 10032 Styles[0] = getLLVMStyle(); 10033 EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1])); 10034 EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2])); 10035 EXPECT_ALL_STYLES_EQUAL(Styles); 10036 10037 Styles[0] = getGoogleStyle(); 10038 EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1])); 10039 EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2])); 10040 EXPECT_ALL_STYLES_EQUAL(Styles); 10041 10042 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 10043 EXPECT_TRUE( 10044 getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1])); 10045 EXPECT_TRUE( 10046 getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2])); 10047 EXPECT_ALL_STYLES_EQUAL(Styles); 10048 10049 Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp); 10050 EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1])); 10051 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2])); 10052 EXPECT_ALL_STYLES_EQUAL(Styles); 10053 10054 Styles[0] = getMozillaStyle(); 10055 EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1])); 10056 EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2])); 10057 EXPECT_ALL_STYLES_EQUAL(Styles); 10058 10059 Styles[0] = getWebKitStyle(); 10060 EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1])); 10061 EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2])); 10062 EXPECT_ALL_STYLES_EQUAL(Styles); 10063 10064 Styles[0] = getGNUStyle(); 10065 EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1])); 10066 EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2])); 10067 EXPECT_ALL_STYLES_EQUAL(Styles); 10068 10069 EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0])); 10070 } 10071 10072 TEST_F(FormatTest, GetsCorrectBasedOnStyle) { 10073 SmallVector<FormatStyle, 8> Styles; 10074 Styles.resize(2); 10075 10076 Styles[0] = getGoogleStyle(); 10077 Styles[1] = getLLVMStyle(); 10078 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 10079 EXPECT_ALL_STYLES_EQUAL(Styles); 10080 10081 Styles.resize(5); 10082 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 10083 Styles[1] = getLLVMStyle(); 10084 Styles[1].Language = FormatStyle::LK_JavaScript; 10085 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 10086 10087 Styles[2] = getLLVMStyle(); 10088 Styles[2].Language = FormatStyle::LK_JavaScript; 10089 EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n" 10090 "BasedOnStyle: Google", 10091 &Styles[2]) 10092 .value()); 10093 10094 Styles[3] = getLLVMStyle(); 10095 Styles[3].Language = FormatStyle::LK_JavaScript; 10096 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n" 10097 "Language: JavaScript", 10098 &Styles[3]) 10099 .value()); 10100 10101 Styles[4] = getLLVMStyle(); 10102 Styles[4].Language = FormatStyle::LK_JavaScript; 10103 EXPECT_EQ(0, parseConfiguration("---\n" 10104 "BasedOnStyle: LLVM\n" 10105 "IndentWidth: 123\n" 10106 "---\n" 10107 "BasedOnStyle: Google\n" 10108 "Language: JavaScript", 10109 &Styles[4]) 10110 .value()); 10111 EXPECT_ALL_STYLES_EQUAL(Styles); 10112 } 10113 10114 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \ 10115 Style.FIELD = false; \ 10116 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \ 10117 EXPECT_TRUE(Style.FIELD); \ 10118 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \ 10119 EXPECT_FALSE(Style.FIELD); 10120 10121 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD) 10122 10123 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \ 10124 Style.STRUCT.FIELD = false; \ 10125 EXPECT_EQ(0, \ 10126 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \ 10127 .value()); \ 10128 EXPECT_TRUE(Style.STRUCT.FIELD); \ 10129 EXPECT_EQ(0, \ 10130 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \ 10131 .value()); \ 10132 EXPECT_FALSE(Style.STRUCT.FIELD); 10133 10134 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \ 10135 CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD) 10136 10137 #define CHECK_PARSE(TEXT, FIELD, VALUE) \ 10138 EXPECT_NE(VALUE, Style.FIELD); \ 10139 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \ 10140 EXPECT_EQ(VALUE, Style.FIELD) 10141 10142 TEST_F(FormatTest, ParsesConfigurationBools) { 10143 FormatStyle Style = {}; 10144 Style.Language = FormatStyle::LK_Cpp; 10145 CHECK_PARSE_BOOL(AlignOperands); 10146 CHECK_PARSE_BOOL(AlignTrailingComments); 10147 CHECK_PARSE_BOOL(AlignConsecutiveAssignments); 10148 CHECK_PARSE_BOOL(AlignConsecutiveDeclarations); 10149 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); 10150 CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine); 10151 CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine); 10152 CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine); 10153 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); 10154 CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations); 10155 CHECK_PARSE_BOOL(BinPackArguments); 10156 CHECK_PARSE_BOOL(BinPackParameters); 10157 CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations); 10158 CHECK_PARSE_BOOL(BreakBeforeTernaryOperators); 10159 CHECK_PARSE_BOOL(BreakStringLiterals); 10160 CHECK_PARSE_BOOL(BreakBeforeInheritanceComma) 10161 CHECK_PARSE_BOOL(CompactNamespaces); 10162 CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine); 10163 CHECK_PARSE_BOOL(DerivePointerAlignment); 10164 CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding"); 10165 CHECK_PARSE_BOOL(DisableFormat); 10166 CHECK_PARSE_BOOL(IndentCaseLabels); 10167 CHECK_PARSE_BOOL(IndentWrappedFunctionNames); 10168 CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks); 10169 CHECK_PARSE_BOOL(ObjCSpaceAfterProperty); 10170 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList); 10171 CHECK_PARSE_BOOL(Cpp11BracedListStyle); 10172 CHECK_PARSE_BOOL(ReflowComments); 10173 CHECK_PARSE_BOOL(SortIncludes); 10174 CHECK_PARSE_BOOL(SortUsingDeclarations); 10175 CHECK_PARSE_BOOL(SpacesInParentheses); 10176 CHECK_PARSE_BOOL(SpacesInSquareBrackets); 10177 CHECK_PARSE_BOOL(SpacesInAngles); 10178 CHECK_PARSE_BOOL(SpaceInEmptyParentheses); 10179 CHECK_PARSE_BOOL(SpacesInContainerLiterals); 10180 CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); 10181 CHECK_PARSE_BOOL(SpaceAfterCStyleCast); 10182 CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword); 10183 CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); 10184 10185 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass); 10186 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement); 10187 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum); 10188 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction); 10189 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace); 10190 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration); 10191 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct); 10192 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion); 10193 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock); 10194 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch); 10195 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse); 10196 CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces); 10197 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction); 10198 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord); 10199 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace); 10200 } 10201 10202 #undef CHECK_PARSE_BOOL 10203 10204 TEST_F(FormatTest, ParsesConfiguration) { 10205 FormatStyle Style = {}; 10206 Style.Language = FormatStyle::LK_Cpp; 10207 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234); 10208 CHECK_PARSE("ConstructorInitializerIndentWidth: 1234", 10209 ConstructorInitializerIndentWidth, 1234u); 10210 CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u); 10211 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); 10212 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u); 10213 CHECK_PARSE("PenaltyBreakAssignment: 1234", 10214 PenaltyBreakAssignment, 1234u); 10215 CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234", 10216 PenaltyBreakBeforeFirstCallParameter, 1234u); 10217 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); 10218 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234", 10219 PenaltyReturnTypeOnItsOwnLine, 1234u); 10220 CHECK_PARSE("SpacesBeforeTrailingComments: 1234", 10221 SpacesBeforeTrailingComments, 1234u); 10222 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u); 10223 CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u); 10224 CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$"); 10225 10226 Style.PointerAlignment = FormatStyle::PAS_Middle; 10227 CHECK_PARSE("PointerAlignment: Left", PointerAlignment, 10228 FormatStyle::PAS_Left); 10229 CHECK_PARSE("PointerAlignment: Right", PointerAlignment, 10230 FormatStyle::PAS_Right); 10231 CHECK_PARSE("PointerAlignment: Middle", PointerAlignment, 10232 FormatStyle::PAS_Middle); 10233 // For backward compatibility: 10234 CHECK_PARSE("PointerBindsToType: Left", PointerAlignment, 10235 FormatStyle::PAS_Left); 10236 CHECK_PARSE("PointerBindsToType: Right", PointerAlignment, 10237 FormatStyle::PAS_Right); 10238 CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment, 10239 FormatStyle::PAS_Middle); 10240 10241 Style.Standard = FormatStyle::LS_Auto; 10242 CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03); 10243 CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11); 10244 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03); 10245 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11); 10246 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto); 10247 10248 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 10249 CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment", 10250 BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment); 10251 CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators, 10252 FormatStyle::BOS_None); 10253 CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators, 10254 FormatStyle::BOS_All); 10255 // For backward compatibility: 10256 CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators, 10257 FormatStyle::BOS_None); 10258 CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators, 10259 FormatStyle::BOS_All); 10260 10261 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 10262 CHECK_PARSE("BreakConstructorInitializers: BeforeComma", 10263 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 10264 CHECK_PARSE("BreakConstructorInitializers: AfterColon", 10265 BreakConstructorInitializers, FormatStyle::BCIS_AfterColon); 10266 CHECK_PARSE("BreakConstructorInitializers: BeforeColon", 10267 BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon); 10268 // For backward compatibility: 10269 CHECK_PARSE("BreakConstructorInitializersBeforeComma: true", 10270 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 10271 10272 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 10273 CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket, 10274 FormatStyle::BAS_Align); 10275 CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket, 10276 FormatStyle::BAS_DontAlign); 10277 CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket, 10278 FormatStyle::BAS_AlwaysBreak); 10279 // For backward compatibility: 10280 CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket, 10281 FormatStyle::BAS_DontAlign); 10282 CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket, 10283 FormatStyle::BAS_Align); 10284 10285 Style.AlignEscapedNewlines = FormatStyle::ENAS_Left; 10286 CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines, 10287 FormatStyle::ENAS_DontAlign); 10288 CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines, 10289 FormatStyle::ENAS_Left); 10290 CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines, 10291 FormatStyle::ENAS_Right); 10292 // For backward compatibility: 10293 CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines, 10294 FormatStyle::ENAS_Left); 10295 CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines, 10296 FormatStyle::ENAS_Right); 10297 10298 Style.UseTab = FormatStyle::UT_ForIndentation; 10299 CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never); 10300 CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation); 10301 CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always); 10302 CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab, 10303 FormatStyle::UT_ForContinuationAndIndentation); 10304 // For backward compatibility: 10305 CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never); 10306 CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always); 10307 10308 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 10309 CHECK_PARSE("AllowShortFunctionsOnASingleLine: None", 10310 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 10311 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline", 10312 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline); 10313 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty", 10314 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty); 10315 CHECK_PARSE("AllowShortFunctionsOnASingleLine: All", 10316 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 10317 // For backward compatibility: 10318 CHECK_PARSE("AllowShortFunctionsOnASingleLine: false", 10319 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 10320 CHECK_PARSE("AllowShortFunctionsOnASingleLine: true", 10321 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 10322 10323 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 10324 CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens, 10325 FormatStyle::SBPO_Never); 10326 CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens, 10327 FormatStyle::SBPO_Always); 10328 CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens, 10329 FormatStyle::SBPO_ControlStatements); 10330 // For backward compatibility: 10331 CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens, 10332 FormatStyle::SBPO_Never); 10333 CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens, 10334 FormatStyle::SBPO_ControlStatements); 10335 10336 Style.ColumnLimit = 123; 10337 FormatStyle BaseStyle = getLLVMStyle(); 10338 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit); 10339 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u); 10340 10341 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 10342 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces, 10343 FormatStyle::BS_Attach); 10344 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces, 10345 FormatStyle::BS_Linux); 10346 CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces, 10347 FormatStyle::BS_Mozilla); 10348 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces, 10349 FormatStyle::BS_Stroustrup); 10350 CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces, 10351 FormatStyle::BS_Allman); 10352 CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU); 10353 CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces, 10354 FormatStyle::BS_WebKit); 10355 CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces, 10356 FormatStyle::BS_Custom); 10357 10358 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 10359 CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType, 10360 FormatStyle::RTBS_None); 10361 CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType, 10362 FormatStyle::RTBS_All); 10363 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel", 10364 AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel); 10365 CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions", 10366 AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions); 10367 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions", 10368 AlwaysBreakAfterReturnType, 10369 FormatStyle::RTBS_TopLevelDefinitions); 10370 10371 Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All; 10372 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None", 10373 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None); 10374 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All", 10375 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All); 10376 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel", 10377 AlwaysBreakAfterDefinitionReturnType, 10378 FormatStyle::DRTBS_TopLevel); 10379 10380 Style.NamespaceIndentation = FormatStyle::NI_All; 10381 CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation, 10382 FormatStyle::NI_None); 10383 CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation, 10384 FormatStyle::NI_Inner); 10385 CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation, 10386 FormatStyle::NI_All); 10387 10388 // FIXME: This is required because parsing a configuration simply overwrites 10389 // the first N elements of the list instead of resetting it. 10390 Style.ForEachMacros.clear(); 10391 std::vector<std::string> BoostForeach; 10392 BoostForeach.push_back("BOOST_FOREACH"); 10393 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach); 10394 std::vector<std::string> BoostAndQForeach; 10395 BoostAndQForeach.push_back("BOOST_FOREACH"); 10396 BoostAndQForeach.push_back("Q_FOREACH"); 10397 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros, 10398 BoostAndQForeach); 10399 10400 Style.IncludeCategories.clear(); 10401 std::vector<FormatStyle::IncludeCategory> ExpectedCategories = {{"abc/.*", 2}, 10402 {".*", 1}}; 10403 CHECK_PARSE("IncludeCategories:\n" 10404 " - Regex: abc/.*\n" 10405 " Priority: 2\n" 10406 " - Regex: .*\n" 10407 " Priority: 1", 10408 IncludeCategories, ExpectedCategories); 10409 CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeIsMainRegex, "abc$"); 10410 10411 Style.RawStringFormats.clear(); 10412 std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = { 10413 {"pb", FormatStyle::LK_TextProto, "llvm"}, 10414 {"cpp", FormatStyle::LK_Cpp, "google"}}; 10415 10416 CHECK_PARSE("RawStringFormats:\n" 10417 " - Delimiter: 'pb'\n" 10418 " Language: TextProto\n" 10419 " BasedOnStyle: llvm\n" 10420 " - Delimiter: 'cpp'\n" 10421 " Language: Cpp\n" 10422 " BasedOnStyle: google", 10423 RawStringFormats, ExpectedRawStringFormats); 10424 } 10425 10426 TEST_F(FormatTest, ParsesConfigurationWithLanguages) { 10427 FormatStyle Style = {}; 10428 Style.Language = FormatStyle::LK_Cpp; 10429 CHECK_PARSE("Language: Cpp\n" 10430 "IndentWidth: 12", 10431 IndentWidth, 12u); 10432 EXPECT_EQ(parseConfiguration("Language: JavaScript\n" 10433 "IndentWidth: 34", 10434 &Style), 10435 ParseError::Unsuitable); 10436 EXPECT_EQ(12u, Style.IndentWidth); 10437 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 10438 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 10439 10440 Style.Language = FormatStyle::LK_JavaScript; 10441 CHECK_PARSE("Language: JavaScript\n" 10442 "IndentWidth: 12", 10443 IndentWidth, 12u); 10444 CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u); 10445 EXPECT_EQ(parseConfiguration("Language: Cpp\n" 10446 "IndentWidth: 34", 10447 &Style), 10448 ParseError::Unsuitable); 10449 EXPECT_EQ(23u, Style.IndentWidth); 10450 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 10451 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 10452 10453 CHECK_PARSE("BasedOnStyle: LLVM\n" 10454 "IndentWidth: 67", 10455 IndentWidth, 67u); 10456 10457 CHECK_PARSE("---\n" 10458 "Language: JavaScript\n" 10459 "IndentWidth: 12\n" 10460 "---\n" 10461 "Language: Cpp\n" 10462 "IndentWidth: 34\n" 10463 "...\n", 10464 IndentWidth, 12u); 10465 10466 Style.Language = FormatStyle::LK_Cpp; 10467 CHECK_PARSE("---\n" 10468 "Language: JavaScript\n" 10469 "IndentWidth: 12\n" 10470 "---\n" 10471 "Language: Cpp\n" 10472 "IndentWidth: 34\n" 10473 "...\n", 10474 IndentWidth, 34u); 10475 CHECK_PARSE("---\n" 10476 "IndentWidth: 78\n" 10477 "---\n" 10478 "Language: JavaScript\n" 10479 "IndentWidth: 56\n" 10480 "...\n", 10481 IndentWidth, 78u); 10482 10483 Style.ColumnLimit = 123; 10484 Style.IndentWidth = 234; 10485 Style.BreakBeforeBraces = FormatStyle::BS_Linux; 10486 Style.TabWidth = 345; 10487 EXPECT_FALSE(parseConfiguration("---\n" 10488 "IndentWidth: 456\n" 10489 "BreakBeforeBraces: Allman\n" 10490 "---\n" 10491 "Language: JavaScript\n" 10492 "IndentWidth: 111\n" 10493 "TabWidth: 111\n" 10494 "---\n" 10495 "Language: Cpp\n" 10496 "BreakBeforeBraces: Stroustrup\n" 10497 "TabWidth: 789\n" 10498 "...\n", 10499 &Style)); 10500 EXPECT_EQ(123u, Style.ColumnLimit); 10501 EXPECT_EQ(456u, Style.IndentWidth); 10502 EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces); 10503 EXPECT_EQ(789u, Style.TabWidth); 10504 10505 EXPECT_EQ(parseConfiguration("---\n" 10506 "Language: JavaScript\n" 10507 "IndentWidth: 56\n" 10508 "---\n" 10509 "IndentWidth: 78\n" 10510 "...\n", 10511 &Style), 10512 ParseError::Error); 10513 EXPECT_EQ(parseConfiguration("---\n" 10514 "Language: JavaScript\n" 10515 "IndentWidth: 56\n" 10516 "---\n" 10517 "Language: JavaScript\n" 10518 "IndentWidth: 78\n" 10519 "...\n", 10520 &Style), 10521 ParseError::Error); 10522 10523 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 10524 } 10525 10526 #undef CHECK_PARSE 10527 10528 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) { 10529 FormatStyle Style = {}; 10530 Style.Language = FormatStyle::LK_JavaScript; 10531 Style.BreakBeforeTernaryOperators = true; 10532 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value()); 10533 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 10534 10535 Style.BreakBeforeTernaryOperators = true; 10536 EXPECT_EQ(0, parseConfiguration("---\n" 10537 "BasedOnStyle: Google\n" 10538 "---\n" 10539 "Language: JavaScript\n" 10540 "IndentWidth: 76\n" 10541 "...\n", 10542 &Style) 10543 .value()); 10544 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 10545 EXPECT_EQ(76u, Style.IndentWidth); 10546 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 10547 } 10548 10549 TEST_F(FormatTest, ConfigurationRoundTripTest) { 10550 FormatStyle Style = getLLVMStyle(); 10551 std::string YAML = configurationAsText(Style); 10552 FormatStyle ParsedStyle = {}; 10553 ParsedStyle.Language = FormatStyle::LK_Cpp; 10554 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value()); 10555 EXPECT_EQ(Style, ParsedStyle); 10556 } 10557 10558 TEST_F(FormatTest, WorksFor8bitEncodings) { 10559 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" 10560 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" 10561 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" 10562 "\"\xef\xee\xf0\xf3...\"", 10563 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " 10564 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " 10565 "\xef\xee\xf0\xf3...\"", 10566 getLLVMStyleWithColumns(12))); 10567 } 10568 10569 TEST_F(FormatTest, HandlesUTF8BOM) { 10570 EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf")); 10571 EXPECT_EQ("\xef\xbb\xbf#include <iostream>", 10572 format("\xef\xbb\xbf#include <iostream>")); 10573 EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>", 10574 format("\xef\xbb\xbf\n#include <iostream>")); 10575 } 10576 10577 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. 10578 #if !defined(_MSC_VER) 10579 10580 TEST_F(FormatTest, CountsUTF8CharactersProperly) { 10581 verifyFormat("\"Однажды в студёную зимнюю пору...\"", 10582 getLLVMStyleWithColumns(35)); 10583 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", 10584 getLLVMStyleWithColumns(31)); 10585 verifyFormat("// Однажды в студёную зимнюю пору...", 10586 getLLVMStyleWithColumns(36)); 10587 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32)); 10588 verifyFormat("/* Однажды в студёную зимнюю пору... */", 10589 getLLVMStyleWithColumns(39)); 10590 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", 10591 getLLVMStyleWithColumns(35)); 10592 } 10593 10594 TEST_F(FormatTest, SplitsUTF8Strings) { 10595 // Non-printable characters' width is currently considered to be the length in 10596 // bytes in UTF8. The characters can be displayed in very different manner 10597 // (zero-width, single width with a substitution glyph, expanded to their code 10598 // (e.g. "<8d>"), so there's no single correct way to handle them. 10599 EXPECT_EQ("\"aaaaÄ\"\n" 10600 "\"\xc2\x8d\";", 10601 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 10602 EXPECT_EQ("\"aaaaaaaÄ\"\n" 10603 "\"\xc2\x8d\";", 10604 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 10605 EXPECT_EQ("\"Однажды, в \"\n" 10606 "\"студёную \"\n" 10607 "\"зимнюю \"\n" 10608 "\"пору,\"", 10609 format("\"Однажды, в студёную зимнюю пору,\"", 10610 getLLVMStyleWithColumns(13))); 10611 EXPECT_EQ( 10612 "\"一 二 三 \"\n" 10613 "\"四 五六 \"\n" 10614 "\"七 八 九 \"\n" 10615 "\"十\"", 10616 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); 10617 EXPECT_EQ("\"一\t\"\n" 10618 "\"二 \t\"\n" 10619 "\"三 四 \"\n" 10620 "\"五\t\"\n" 10621 "\"六 \t\"\n" 10622 "\"七 \"\n" 10623 "\"八九十\tqq\"", 10624 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", 10625 getLLVMStyleWithColumns(11))); 10626 10627 // UTF8 character in an escape sequence. 10628 EXPECT_EQ("\"aaaaaa\"\n" 10629 "\"\\\xC2\x8D\"", 10630 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10))); 10631 } 10632 10633 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { 10634 EXPECT_EQ("const char *sssss =\n" 10635 " \"一二三四五六七八\\\n" 10636 " 九 十\";", 10637 format("const char *sssss = \"一二三四五六七八\\\n" 10638 " 九 十\";", 10639 getLLVMStyleWithColumns(30))); 10640 } 10641 10642 TEST_F(FormatTest, SplitsUTF8LineComments) { 10643 EXPECT_EQ("// aaaaÄ\xc2\x8d", 10644 format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10))); 10645 EXPECT_EQ("// Я из лесу\n" 10646 "// вышел; был\n" 10647 "// сильный\n" 10648 "// мороз.", 10649 format("// Я из лесу вышел; был сильный мороз.", 10650 getLLVMStyleWithColumns(13))); 10651 EXPECT_EQ("// 一二三\n" 10652 "// 四五六七\n" 10653 "// 八 九\n" 10654 "// 十", 10655 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9))); 10656 } 10657 10658 TEST_F(FormatTest, SplitsUTF8BlockComments) { 10659 EXPECT_EQ("/* Гляжу,\n" 10660 " * поднимается\n" 10661 " * медленно в\n" 10662 " * гору\n" 10663 " * Лошадка,\n" 10664 " * везущая\n" 10665 " * хворосту\n" 10666 " * воз. */", 10667 format("/* Гляжу, поднимается медленно в гору\n" 10668 " * Лошадка, везущая хворосту воз. */", 10669 getLLVMStyleWithColumns(13))); 10670 EXPECT_EQ( 10671 "/* 一二三\n" 10672 " * 四五六七\n" 10673 " * 八 九\n" 10674 " * 十 */", 10675 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9))); 10676 EXPECT_EQ("/* \n" 10677 " * \n" 10678 " * - */", 10679 format("/* - */", getLLVMStyleWithColumns(12))); 10680 } 10681 10682 #endif // _MSC_VER 10683 10684 TEST_F(FormatTest, ConstructorInitializerIndentWidth) { 10685 FormatStyle Style = getLLVMStyle(); 10686 10687 Style.ConstructorInitializerIndentWidth = 4; 10688 verifyFormat( 10689 "SomeClass::Constructor()\n" 10690 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10691 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10692 Style); 10693 10694 Style.ConstructorInitializerIndentWidth = 2; 10695 verifyFormat( 10696 "SomeClass::Constructor()\n" 10697 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10698 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10699 Style); 10700 10701 Style.ConstructorInitializerIndentWidth = 0; 10702 verifyFormat( 10703 "SomeClass::Constructor()\n" 10704 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10705 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10706 Style); 10707 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 10708 verifyFormat( 10709 "SomeLongTemplateVariableName<\n" 10710 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>", 10711 Style); 10712 verifyFormat( 10713 "bool smaller = 1 < bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 10714 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 10715 Style); 10716 } 10717 10718 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) { 10719 FormatStyle Style = getLLVMStyle(); 10720 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 10721 Style.ConstructorInitializerIndentWidth = 4; 10722 verifyFormat("SomeClass::Constructor()\n" 10723 " : a(a)\n" 10724 " , b(b)\n" 10725 " , c(c) {}", 10726 Style); 10727 verifyFormat("SomeClass::Constructor()\n" 10728 " : a(a) {}", 10729 Style); 10730 10731 Style.ColumnLimit = 0; 10732 verifyFormat("SomeClass::Constructor()\n" 10733 " : a(a) {}", 10734 Style); 10735 verifyFormat("SomeClass::Constructor() noexcept\n" 10736 " : a(a) {}", 10737 Style); 10738 verifyFormat("SomeClass::Constructor()\n" 10739 " : a(a)\n" 10740 " , b(b)\n" 10741 " , c(c) {}", 10742 Style); 10743 verifyFormat("SomeClass::Constructor()\n" 10744 " : a(a) {\n" 10745 " foo();\n" 10746 " bar();\n" 10747 "}", 10748 Style); 10749 10750 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 10751 verifyFormat("SomeClass::Constructor()\n" 10752 " : a(a)\n" 10753 " , b(b)\n" 10754 " , c(c) {\n}", 10755 Style); 10756 verifyFormat("SomeClass::Constructor()\n" 10757 " : a(a) {\n}", 10758 Style); 10759 10760 Style.ColumnLimit = 80; 10761 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 10762 Style.ConstructorInitializerIndentWidth = 2; 10763 verifyFormat("SomeClass::Constructor()\n" 10764 " : a(a)\n" 10765 " , b(b)\n" 10766 " , c(c) {}", 10767 Style); 10768 10769 Style.ConstructorInitializerIndentWidth = 0; 10770 verifyFormat("SomeClass::Constructor()\n" 10771 ": a(a)\n" 10772 ", b(b)\n" 10773 ", c(c) {}", 10774 Style); 10775 10776 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 10777 Style.ConstructorInitializerIndentWidth = 4; 10778 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style); 10779 verifyFormat( 10780 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n", 10781 Style); 10782 verifyFormat( 10783 "SomeClass::Constructor()\n" 10784 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", 10785 Style); 10786 Style.ConstructorInitializerIndentWidth = 4; 10787 Style.ColumnLimit = 60; 10788 verifyFormat("SomeClass::Constructor()\n" 10789 " : aaaaaaaa(aaaaaaaa)\n" 10790 " , aaaaaaaa(aaaaaaaa)\n" 10791 " , aaaaaaaa(aaaaaaaa) {}", 10792 Style); 10793 } 10794 10795 TEST_F(FormatTest, Destructors) { 10796 verifyFormat("void F(int &i) { i.~int(); }"); 10797 verifyFormat("void F(int &i) { i->~int(); }"); 10798 } 10799 10800 TEST_F(FormatTest, FormatsWithWebKitStyle) { 10801 FormatStyle Style = getWebKitStyle(); 10802 10803 // Don't indent in outer namespaces. 10804 verifyFormat("namespace outer {\n" 10805 "int i;\n" 10806 "namespace inner {\n" 10807 " int i;\n" 10808 "} // namespace inner\n" 10809 "} // namespace outer\n" 10810 "namespace other_outer {\n" 10811 "int i;\n" 10812 "}", 10813 Style); 10814 10815 // Don't indent case labels. 10816 verifyFormat("switch (variable) {\n" 10817 "case 1:\n" 10818 "case 2:\n" 10819 " doSomething();\n" 10820 " break;\n" 10821 "default:\n" 10822 " ++variable;\n" 10823 "}", 10824 Style); 10825 10826 // Wrap before binary operators. 10827 EXPECT_EQ("void f()\n" 10828 "{\n" 10829 " if (aaaaaaaaaaaaaaaa\n" 10830 " && bbbbbbbbbbbbbbbbbbbbbbbb\n" 10831 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 10832 " return;\n" 10833 "}", 10834 format("void f() {\n" 10835 "if (aaaaaaaaaaaaaaaa\n" 10836 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" 10837 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 10838 "return;\n" 10839 "}", 10840 Style)); 10841 10842 // Allow functions on a single line. 10843 verifyFormat("void f() { return; }", Style); 10844 10845 // Constructor initializers are formatted one per line with the "," on the 10846 // new line. 10847 verifyFormat("Constructor()\n" 10848 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 10849 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" 10850 " aaaaaaaaaaaaaa)\n" 10851 " , aaaaaaaaaaaaaaaaaaaaaaa()\n" 10852 "{\n" 10853 "}", 10854 Style); 10855 verifyFormat("SomeClass::Constructor()\n" 10856 " : a(a)\n" 10857 "{\n" 10858 "}", 10859 Style); 10860 EXPECT_EQ("SomeClass::Constructor()\n" 10861 " : a(a)\n" 10862 "{\n" 10863 "}", 10864 format("SomeClass::Constructor():a(a){}", Style)); 10865 verifyFormat("SomeClass::Constructor()\n" 10866 " : a(a)\n" 10867 " , b(b)\n" 10868 " , c(c)\n" 10869 "{\n" 10870 "}", 10871 Style); 10872 verifyFormat("SomeClass::Constructor()\n" 10873 " : a(a)\n" 10874 "{\n" 10875 " foo();\n" 10876 " bar();\n" 10877 "}", 10878 Style); 10879 10880 // Access specifiers should be aligned left. 10881 verifyFormat("class C {\n" 10882 "public:\n" 10883 " int i;\n" 10884 "};", 10885 Style); 10886 10887 // Do not align comments. 10888 verifyFormat("int a; // Do not\n" 10889 "double b; // align comments.", 10890 Style); 10891 10892 // Do not align operands. 10893 EXPECT_EQ("ASSERT(aaaa\n" 10894 " || bbbb);", 10895 format("ASSERT ( aaaa\n||bbbb);", Style)); 10896 10897 // Accept input's line breaks. 10898 EXPECT_EQ("if (aaaaaaaaaaaaaaa\n" 10899 " || bbbbbbbbbbbbbbb) {\n" 10900 " i++;\n" 10901 "}", 10902 format("if (aaaaaaaaaaaaaaa\n" 10903 "|| bbbbbbbbbbbbbbb) { i++; }", 10904 Style)); 10905 EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n" 10906 " i++;\n" 10907 "}", 10908 format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style)); 10909 10910 // Don't automatically break all macro definitions (llvm.org/PR17842). 10911 verifyFormat("#define aNumber 10", Style); 10912 // However, generally keep the line breaks that the user authored. 10913 EXPECT_EQ("#define aNumber \\\n" 10914 " 10", 10915 format("#define aNumber \\\n" 10916 " 10", 10917 Style)); 10918 10919 // Keep empty and one-element array literals on a single line. 10920 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n" 10921 " copyItems:YES];", 10922 format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n" 10923 "copyItems:YES];", 10924 Style)); 10925 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n" 10926 " copyItems:YES];", 10927 format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n" 10928 " copyItems:YES];", 10929 Style)); 10930 // FIXME: This does not seem right, there should be more indentation before 10931 // the array literal's entries. Nested blocks have the same problem. 10932 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 10933 " @\"a\",\n" 10934 " @\"a\"\n" 10935 "]\n" 10936 " copyItems:YES];", 10937 format("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 10938 " @\"a\",\n" 10939 " @\"a\"\n" 10940 " ]\n" 10941 " copyItems:YES];", 10942 Style)); 10943 EXPECT_EQ( 10944 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 10945 " copyItems:YES];", 10946 format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 10947 " copyItems:YES];", 10948 Style)); 10949 10950 verifyFormat("[self.a b:c c:d];", Style); 10951 EXPECT_EQ("[self.a b:c\n" 10952 " c:d];", 10953 format("[self.a b:c\n" 10954 "c:d];", 10955 Style)); 10956 } 10957 10958 TEST_F(FormatTest, FormatsLambdas) { 10959 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n"); 10960 verifyFormat("int c = [&] { [=] { return b++; }(); }();\n"); 10961 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n"); 10962 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n"); 10963 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n"); 10964 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n"); 10965 verifyFormat("auto c = [a = [b = 42] {}] {};\n"); 10966 verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n"); 10967 verifyFormat("int x = f(*+[] {});"); 10968 verifyFormat("void f() {\n" 10969 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n" 10970 "}\n"); 10971 verifyFormat("void f() {\n" 10972 " other(x.begin(), //\n" 10973 " x.end(), //\n" 10974 " [&](int, int) { return 1; });\n" 10975 "}\n"); 10976 verifyFormat("SomeFunction([]() { // A cool function...\n" 10977 " return 43;\n" 10978 "});"); 10979 EXPECT_EQ("SomeFunction([]() {\n" 10980 "#define A a\n" 10981 " return 43;\n" 10982 "});", 10983 format("SomeFunction([](){\n" 10984 "#define A a\n" 10985 "return 43;\n" 10986 "});")); 10987 verifyFormat("void f() {\n" 10988 " SomeFunction([](decltype(x), A *a) {});\n" 10989 "}"); 10990 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10991 " [](const aaaaaaaaaa &a) { return a; });"); 10992 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n" 10993 " SomeOtherFunctioooooooooooooooooooooooooon();\n" 10994 "});"); 10995 verifyFormat("Constructor()\n" 10996 " : Field([] { // comment\n" 10997 " int i;\n" 10998 " }) {}"); 10999 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n" 11000 " return some_parameter.size();\n" 11001 "};"); 11002 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n" 11003 " [](const string &s) { return s; };"); 11004 verifyFormat("int i = aaaaaa ? 1 //\n" 11005 " : [] {\n" 11006 " return 2; //\n" 11007 " }();"); 11008 verifyFormat("llvm::errs() << \"number of twos is \"\n" 11009 " << std::count_if(v.begin(), v.end(), [](int x) {\n" 11010 " return x == 2; // force break\n" 11011 " });"); 11012 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 11013 " [=](int iiiiiiiiiiii) {\n" 11014 " return aaaaaaaaaaaaaaaaaaaaaaa !=\n" 11015 " aaaaaaaaaaaaaaaaaaaaaaa;\n" 11016 " });", 11017 getLLVMStyleWithColumns(60)); 11018 verifyFormat("SomeFunction({[&] {\n" 11019 " // comment\n" 11020 " },\n" 11021 " [&] {\n" 11022 " // comment\n" 11023 " }});"); 11024 verifyFormat("SomeFunction({[&] {\n" 11025 " // comment\n" 11026 "}});"); 11027 verifyFormat("virtual aaaaaaaaaaaaaaaa(std::function<bool()> bbbbbbbbbbbb =\n" 11028 " [&]() { return true; },\n" 11029 " aaaaa aaaaaaaaa);"); 11030 11031 // Lambdas with return types. 11032 verifyFormat("int c = []() -> int { return 2; }();\n"); 11033 verifyFormat("int c = []() -> int * { return 2; }();\n"); 11034 verifyFormat("int c = []() -> vector<int> { return {2}; }();\n"); 11035 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());"); 11036 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};"); 11037 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};"); 11038 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};"); 11039 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};"); 11040 verifyFormat("[a, a]() -> a<1> {};"); 11041 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n" 11042 " int j) -> int {\n" 11043 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n" 11044 "};"); 11045 verifyFormat( 11046 "aaaaaaaaaaaaaaaaaaaaaa(\n" 11047 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n" 11048 " return aaaaaaaaaaaaaaaaa;\n" 11049 " });", 11050 getLLVMStyleWithColumns(70)); 11051 verifyFormat("[]() //\n" 11052 " -> int {\n" 11053 " return 1; //\n" 11054 "};"); 11055 11056 // Multiple lambdas in the same parentheses change indentation rules. 11057 verifyFormat("SomeFunction(\n" 11058 " []() {\n" 11059 " int i = 42;\n" 11060 " return i;\n" 11061 " },\n" 11062 " []() {\n" 11063 " int j = 43;\n" 11064 " return j;\n" 11065 " });"); 11066 11067 // More complex introducers. 11068 verifyFormat("return [i, args...] {};"); 11069 11070 // Not lambdas. 11071 verifyFormat("constexpr char hello[]{\"hello\"};"); 11072 verifyFormat("double &operator[](int i) { return 0; }\n" 11073 "int i;"); 11074 verifyFormat("std::unique_ptr<int[]> foo() {}"); 11075 verifyFormat("int i = a[a][a]->f();"); 11076 verifyFormat("int i = (*b)[a]->f();"); 11077 11078 // Other corner cases. 11079 verifyFormat("void f() {\n" 11080 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n" 11081 " );\n" 11082 "}"); 11083 11084 // Lambdas created through weird macros. 11085 verifyFormat("void f() {\n" 11086 " MACRO((const AA &a) { return 1; });\n" 11087 " MACRO((AA &a) { return 1; });\n" 11088 "}"); 11089 11090 verifyFormat("if (blah_blah(whatever, whatever, [] {\n" 11091 " doo_dah();\n" 11092 " doo_dah();\n" 11093 " })) {\n" 11094 "}"); 11095 verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n" 11096 " doo_dah();\n" 11097 " doo_dah();\n" 11098 " })) {\n" 11099 "}"); 11100 verifyFormat("auto lambda = []() {\n" 11101 " int a = 2\n" 11102 "#if A\n" 11103 " + 2\n" 11104 "#endif\n" 11105 " ;\n" 11106 "};"); 11107 11108 // Lambdas with complex multiline introducers. 11109 verifyFormat( 11110 "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 11111 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n" 11112 " -> ::std::unordered_set<\n" 11113 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n" 11114 " //\n" 11115 " });"); 11116 } 11117 11118 TEST_F(FormatTest, EmptyLinesInLambdas) { 11119 verifyFormat("auto lambda = []() {\n" 11120 " x(); //\n" 11121 "};", 11122 "auto lambda = []() {\n" 11123 "\n" 11124 " x(); //\n" 11125 "\n" 11126 "};"); 11127 } 11128 11129 TEST_F(FormatTest, FormatsBlocks) { 11130 FormatStyle ShortBlocks = getLLVMStyle(); 11131 ShortBlocks.AllowShortBlocksOnASingleLine = true; 11132 verifyFormat("int (^Block)(int, int);", ShortBlocks); 11133 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks); 11134 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks); 11135 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks); 11136 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks); 11137 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks); 11138 11139 verifyFormat("foo(^{ bar(); });", ShortBlocks); 11140 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks); 11141 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks); 11142 11143 verifyFormat("[operation setCompletionBlock:^{\n" 11144 " [self onOperationDone];\n" 11145 "}];"); 11146 verifyFormat("int i = {[operation setCompletionBlock:^{\n" 11147 " [self onOperationDone];\n" 11148 "}]};"); 11149 verifyFormat("[operation setCompletionBlock:^(int *i) {\n" 11150 " f();\n" 11151 "}];"); 11152 verifyFormat("int a = [operation block:^int(int *i) {\n" 11153 " return 1;\n" 11154 "}];"); 11155 verifyFormat("[myObject doSomethingWith:arg1\n" 11156 " aaa:^int(int *a) {\n" 11157 " return 1;\n" 11158 " }\n" 11159 " bbb:f(a * bbbbbbbb)];"); 11160 11161 verifyFormat("[operation setCompletionBlock:^{\n" 11162 " [self.delegate newDataAvailable];\n" 11163 "}];", 11164 getLLVMStyleWithColumns(60)); 11165 verifyFormat("dispatch_async(_fileIOQueue, ^{\n" 11166 " NSString *path = [self sessionFilePath];\n" 11167 " if (path) {\n" 11168 " // ...\n" 11169 " }\n" 11170 "});"); 11171 verifyFormat("[[SessionService sharedService]\n" 11172 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 11173 " if (window) {\n" 11174 " [self windowDidLoad:window];\n" 11175 " } else {\n" 11176 " [self errorLoadingWindow];\n" 11177 " }\n" 11178 " }];"); 11179 verifyFormat("void (^largeBlock)(void) = ^{\n" 11180 " // ...\n" 11181 "};\n", 11182 getLLVMStyleWithColumns(40)); 11183 verifyFormat("[[SessionService sharedService]\n" 11184 " loadWindowWithCompletionBlock: //\n" 11185 " ^(SessionWindow *window) {\n" 11186 " if (window) {\n" 11187 " [self windowDidLoad:window];\n" 11188 " } else {\n" 11189 " [self errorLoadingWindow];\n" 11190 " }\n" 11191 " }];", 11192 getLLVMStyleWithColumns(60)); 11193 verifyFormat("[myObject doSomethingWith:arg1\n" 11194 " firstBlock:^(Foo *a) {\n" 11195 " // ...\n" 11196 " int i;\n" 11197 " }\n" 11198 " secondBlock:^(Bar *b) {\n" 11199 " // ...\n" 11200 " int i;\n" 11201 " }\n" 11202 " thirdBlock:^Foo(Bar *b) {\n" 11203 " // ...\n" 11204 " int i;\n" 11205 " }];"); 11206 verifyFormat("[myObject doSomethingWith:arg1\n" 11207 " firstBlock:-1\n" 11208 " secondBlock:^(Bar *b) {\n" 11209 " // ...\n" 11210 " int i;\n" 11211 " }];"); 11212 11213 verifyFormat("f(^{\n" 11214 " @autoreleasepool {\n" 11215 " if (a) {\n" 11216 " g();\n" 11217 " }\n" 11218 " }\n" 11219 "});"); 11220 verifyFormat("Block b = ^int *(A *a, B *b) {}"); 11221 verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n" 11222 "};"); 11223 11224 FormatStyle FourIndent = getLLVMStyle(); 11225 FourIndent.ObjCBlockIndentWidth = 4; 11226 verifyFormat("[operation setCompletionBlock:^{\n" 11227 " [self onOperationDone];\n" 11228 "}];", 11229 FourIndent); 11230 } 11231 11232 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) { 11233 FormatStyle ZeroColumn = getLLVMStyle(); 11234 ZeroColumn.ColumnLimit = 0; 11235 11236 verifyFormat("[[SessionService sharedService] " 11237 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 11238 " if (window) {\n" 11239 " [self windowDidLoad:window];\n" 11240 " } else {\n" 11241 " [self errorLoadingWindow];\n" 11242 " }\n" 11243 "}];", 11244 ZeroColumn); 11245 EXPECT_EQ("[[SessionService sharedService]\n" 11246 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 11247 " if (window) {\n" 11248 " [self windowDidLoad:window];\n" 11249 " } else {\n" 11250 " [self errorLoadingWindow];\n" 11251 " }\n" 11252 " }];", 11253 format("[[SessionService sharedService]\n" 11254 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 11255 " if (window) {\n" 11256 " [self windowDidLoad:window];\n" 11257 " } else {\n" 11258 " [self errorLoadingWindow];\n" 11259 " }\n" 11260 "}];", 11261 ZeroColumn)); 11262 verifyFormat("[myObject doSomethingWith:arg1\n" 11263 " firstBlock:^(Foo *a) {\n" 11264 " // ...\n" 11265 " int i;\n" 11266 " }\n" 11267 " secondBlock:^(Bar *b) {\n" 11268 " // ...\n" 11269 " int i;\n" 11270 " }\n" 11271 " thirdBlock:^Foo(Bar *b) {\n" 11272 " // ...\n" 11273 " int i;\n" 11274 " }];", 11275 ZeroColumn); 11276 verifyFormat("f(^{\n" 11277 " @autoreleasepool {\n" 11278 " if (a) {\n" 11279 " g();\n" 11280 " }\n" 11281 " }\n" 11282 "});", 11283 ZeroColumn); 11284 verifyFormat("void (^largeBlock)(void) = ^{\n" 11285 " // ...\n" 11286 "};", 11287 ZeroColumn); 11288 11289 ZeroColumn.AllowShortBlocksOnASingleLine = true; 11290 EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };", 11291 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 11292 ZeroColumn.AllowShortBlocksOnASingleLine = false; 11293 EXPECT_EQ("void (^largeBlock)(void) = ^{\n" 11294 " int i;\n" 11295 "};", 11296 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 11297 } 11298 11299 TEST_F(FormatTest, SupportsCRLF) { 11300 EXPECT_EQ("int a;\r\n" 11301 "int b;\r\n" 11302 "int c;\r\n", 11303 format("int a;\r\n" 11304 " int b;\r\n" 11305 " int c;\r\n", 11306 getLLVMStyle())); 11307 EXPECT_EQ("int a;\r\n" 11308 "int b;\r\n" 11309 "int c;\r\n", 11310 format("int a;\r\n" 11311 " int b;\n" 11312 " int c;\r\n", 11313 getLLVMStyle())); 11314 EXPECT_EQ("int a;\n" 11315 "int b;\n" 11316 "int c;\n", 11317 format("int a;\r\n" 11318 " int b;\n" 11319 " int c;\n", 11320 getLLVMStyle())); 11321 EXPECT_EQ("\"aaaaaaa \"\r\n" 11322 "\"bbbbbbb\";\r\n", 11323 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10))); 11324 EXPECT_EQ("#define A \\\r\n" 11325 " b; \\\r\n" 11326 " c; \\\r\n" 11327 " d;\r\n", 11328 format("#define A \\\r\n" 11329 " b; \\\r\n" 11330 " c; d; \r\n", 11331 getGoogleStyle())); 11332 11333 EXPECT_EQ("/*\r\n" 11334 "multi line block comments\r\n" 11335 "should not introduce\r\n" 11336 "an extra carriage return\r\n" 11337 "*/\r\n", 11338 format("/*\r\n" 11339 "multi line block comments\r\n" 11340 "should not introduce\r\n" 11341 "an extra carriage return\r\n" 11342 "*/\r\n")); 11343 } 11344 11345 TEST_F(FormatTest, MunchSemicolonAfterBlocks) { 11346 verifyFormat("MY_CLASS(C) {\n" 11347 " int i;\n" 11348 " int j;\n" 11349 "};"); 11350 } 11351 11352 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) { 11353 FormatStyle TwoIndent = getLLVMStyleWithColumns(15); 11354 TwoIndent.ContinuationIndentWidth = 2; 11355 11356 EXPECT_EQ("int i =\n" 11357 " longFunction(\n" 11358 " arg);", 11359 format("int i = longFunction(arg);", TwoIndent)); 11360 11361 FormatStyle SixIndent = getLLVMStyleWithColumns(20); 11362 SixIndent.ContinuationIndentWidth = 6; 11363 11364 EXPECT_EQ("int i =\n" 11365 " longFunction(\n" 11366 " arg);", 11367 format("int i = longFunction(arg);", SixIndent)); 11368 } 11369 11370 TEST_F(FormatTest, SpacesInAngles) { 11371 FormatStyle Spaces = getLLVMStyle(); 11372 Spaces.SpacesInAngles = true; 11373 11374 verifyFormat("static_cast< int >(arg);", Spaces); 11375 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces); 11376 verifyFormat("f< int, float >();", Spaces); 11377 verifyFormat("template <> g() {}", Spaces); 11378 verifyFormat("template < std::vector< int > > f() {}", Spaces); 11379 verifyFormat("std::function< void(int, int) > fct;", Spaces); 11380 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }", 11381 Spaces); 11382 11383 Spaces.Standard = FormatStyle::LS_Cpp03; 11384 Spaces.SpacesInAngles = true; 11385 verifyFormat("A< A< int > >();", Spaces); 11386 11387 Spaces.SpacesInAngles = false; 11388 verifyFormat("A<A<int> >();", Spaces); 11389 11390 Spaces.Standard = FormatStyle::LS_Cpp11; 11391 Spaces.SpacesInAngles = true; 11392 verifyFormat("A< A< int > >();", Spaces); 11393 11394 Spaces.SpacesInAngles = false; 11395 verifyFormat("A<A<int>>();", Spaces); 11396 } 11397 11398 TEST_F(FormatTest, SpaceAfterTemplateKeyword) { 11399 FormatStyle Style = getLLVMStyle(); 11400 Style.SpaceAfterTemplateKeyword = false; 11401 verifyFormat("template<int> void foo();", Style); 11402 } 11403 11404 TEST_F(FormatTest, TripleAngleBrackets) { 11405 verifyFormat("f<<<1, 1>>>();"); 11406 verifyFormat("f<<<1, 1, 1, s>>>();"); 11407 verifyFormat("f<<<a, b, c, d>>>();"); 11408 EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();")); 11409 verifyFormat("f<param><<<1, 1>>>();"); 11410 verifyFormat("f<1><<<1, 1>>>();"); 11411 EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();")); 11412 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 11413 "aaaaaaaaaaa<<<\n 1, 1>>>();"); 11414 verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n" 11415 " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();"); 11416 } 11417 11418 TEST_F(FormatTest, MergeLessLessAtEnd) { 11419 verifyFormat("<<"); 11420 EXPECT_EQ("< < <", format("\\\n<<<")); 11421 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 11422 "aaallvm::outs() <<"); 11423 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 11424 "aaaallvm::outs()\n <<"); 11425 } 11426 11427 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) { 11428 std::string code = "#if A\n" 11429 "#if B\n" 11430 "a.\n" 11431 "#endif\n" 11432 " a = 1;\n" 11433 "#else\n" 11434 "#endif\n" 11435 "#if C\n" 11436 "#else\n" 11437 "#endif\n"; 11438 EXPECT_EQ(code, format(code)); 11439 } 11440 11441 TEST_F(FormatTest, HandleConflictMarkers) { 11442 // Git/SVN conflict markers. 11443 EXPECT_EQ("int a;\n" 11444 "void f() {\n" 11445 " callme(some(parameter1,\n" 11446 "<<<<<<< text by the vcs\n" 11447 " parameter2),\n" 11448 "||||||| text by the vcs\n" 11449 " parameter2),\n" 11450 " parameter3,\n" 11451 "======= text by the vcs\n" 11452 " parameter2, parameter3),\n" 11453 ">>>>>>> text by the vcs\n" 11454 " otherparameter);\n", 11455 format("int a;\n" 11456 "void f() {\n" 11457 " callme(some(parameter1,\n" 11458 "<<<<<<< text by the vcs\n" 11459 " parameter2),\n" 11460 "||||||| text by the vcs\n" 11461 " parameter2),\n" 11462 " parameter3,\n" 11463 "======= text by the vcs\n" 11464 " parameter2,\n" 11465 " parameter3),\n" 11466 ">>>>>>> text by the vcs\n" 11467 " otherparameter);\n")); 11468 11469 // Perforce markers. 11470 EXPECT_EQ("void f() {\n" 11471 " function(\n" 11472 ">>>> text by the vcs\n" 11473 " parameter,\n" 11474 "==== text by the vcs\n" 11475 " parameter,\n" 11476 "==== text by the vcs\n" 11477 " parameter,\n" 11478 "<<<< text by the vcs\n" 11479 " parameter);\n", 11480 format("void f() {\n" 11481 " function(\n" 11482 ">>>> text by the vcs\n" 11483 " parameter,\n" 11484 "==== text by the vcs\n" 11485 " parameter,\n" 11486 "==== text by the vcs\n" 11487 " parameter,\n" 11488 "<<<< text by the vcs\n" 11489 " parameter);\n")); 11490 11491 EXPECT_EQ("<<<<<<<\n" 11492 "|||||||\n" 11493 "=======\n" 11494 ">>>>>>>", 11495 format("<<<<<<<\n" 11496 "|||||||\n" 11497 "=======\n" 11498 ">>>>>>>")); 11499 11500 EXPECT_EQ("<<<<<<<\n" 11501 "|||||||\n" 11502 "int i;\n" 11503 "=======\n" 11504 ">>>>>>>", 11505 format("<<<<<<<\n" 11506 "|||||||\n" 11507 "int i;\n" 11508 "=======\n" 11509 ">>>>>>>")); 11510 11511 // FIXME: Handle parsing of macros around conflict markers correctly: 11512 EXPECT_EQ("#define Macro \\\n" 11513 "<<<<<<<\n" 11514 "Something \\\n" 11515 "|||||||\n" 11516 "Else \\\n" 11517 "=======\n" 11518 "Other \\\n" 11519 ">>>>>>>\n" 11520 " End int i;\n", 11521 format("#define Macro \\\n" 11522 "<<<<<<<\n" 11523 " Something \\\n" 11524 "|||||||\n" 11525 " Else \\\n" 11526 "=======\n" 11527 " Other \\\n" 11528 ">>>>>>>\n" 11529 " End\n" 11530 "int i;\n")); 11531 } 11532 11533 TEST_F(FormatTest, DisableRegions) { 11534 EXPECT_EQ("int i;\n" 11535 "// clang-format off\n" 11536 " int j;\n" 11537 "// clang-format on\n" 11538 "int k;", 11539 format(" int i;\n" 11540 " // clang-format off\n" 11541 " int j;\n" 11542 " // clang-format on\n" 11543 " int k;")); 11544 EXPECT_EQ("int i;\n" 11545 "/* clang-format off */\n" 11546 " int j;\n" 11547 "/* clang-format on */\n" 11548 "int k;", 11549 format(" int i;\n" 11550 " /* clang-format off */\n" 11551 " int j;\n" 11552 " /* clang-format on */\n" 11553 " int k;")); 11554 11555 // Don't reflow comments within disabled regions. 11556 EXPECT_EQ( 11557 "// clang-format off\n" 11558 "// long long long long long long line\n" 11559 "/* clang-format on */\n" 11560 "/* long long long\n" 11561 " * long long long\n" 11562 " * line */\n" 11563 "int i;\n" 11564 "/* clang-format off */\n" 11565 "/* long long long long long long line */\n", 11566 format("// clang-format off\n" 11567 "// long long long long long long line\n" 11568 "/* clang-format on */\n" 11569 "/* long long long long long long line */\n" 11570 "int i;\n" 11571 "/* clang-format off */\n" 11572 "/* long long long long long long line */\n", 11573 getLLVMStyleWithColumns(20))); 11574 } 11575 11576 TEST_F(FormatTest, DoNotCrashOnInvalidInput) { 11577 format("? ) ="); 11578 verifyNoCrash("#define a\\\n /**/}"); 11579 } 11580 11581 TEST_F(FormatTest, FormatsTableGenCode) { 11582 FormatStyle Style = getLLVMStyle(); 11583 Style.Language = FormatStyle::LK_TableGen; 11584 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style); 11585 } 11586 11587 TEST_F(FormatTest, ArrayOfTemplates) { 11588 EXPECT_EQ("auto a = new unique_ptr<int>[10];", 11589 format("auto a = new unique_ptr<int > [ 10];")); 11590 11591 FormatStyle Spaces = getLLVMStyle(); 11592 Spaces.SpacesInSquareBrackets = true; 11593 EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];", 11594 format("auto a = new unique_ptr<int > [10];", Spaces)); 11595 } 11596 11597 TEST_F(FormatTest, ArrayAsTemplateType) { 11598 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;", 11599 format("auto a = unique_ptr < Foo < Bar>[ 10]> ;")); 11600 11601 FormatStyle Spaces = getLLVMStyle(); 11602 Spaces.SpacesInSquareBrackets = true; 11603 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;", 11604 format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces)); 11605 } 11606 11607 TEST_F(FormatTest, NoSpaceAfterSuper) { 11608 verifyFormat("__super::FooBar();"); 11609 } 11610 11611 TEST(FormatStyle, GetStyleOfFile) { 11612 vfs::InMemoryFileSystem FS; 11613 // Test 1: format file in the same directory. 11614 ASSERT_TRUE( 11615 FS.addFile("/a/.clang-format", 0, 11616 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM"))); 11617 ASSERT_TRUE( 11618 FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 11619 auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS); 11620 ASSERT_TRUE((bool)Style1); 11621 ASSERT_EQ(*Style1, getLLVMStyle()); 11622 11623 // Test 2.1: fallback to default. 11624 ASSERT_TRUE( 11625 FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 11626 auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS); 11627 ASSERT_TRUE((bool)Style2); 11628 ASSERT_EQ(*Style2, getMozillaStyle()); 11629 11630 // Test 2.2: no format on 'none' fallback style. 11631 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 11632 ASSERT_TRUE((bool)Style2); 11633 ASSERT_EQ(*Style2, getNoStyle()); 11634 11635 // Test 2.3: format if config is found with no based style while fallback is 11636 // 'none'. 11637 ASSERT_TRUE(FS.addFile("/b/.clang-format", 0, 11638 llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2"))); 11639 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 11640 ASSERT_TRUE((bool)Style2); 11641 ASSERT_EQ(*Style2, getLLVMStyle()); 11642 11643 // Test 2.4: format if yaml with no based style, while fallback is 'none'. 11644 Style2 = getStyle("{}", "a.h", "none", "", &FS); 11645 ASSERT_TRUE((bool)Style2); 11646 ASSERT_EQ(*Style2, getLLVMStyle()); 11647 11648 // Test 3: format file in parent directory. 11649 ASSERT_TRUE( 11650 FS.addFile("/c/.clang-format", 0, 11651 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 11652 ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0, 11653 llvm::MemoryBuffer::getMemBuffer("int i;"))); 11654 auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS); 11655 ASSERT_TRUE((bool)Style3); 11656 ASSERT_EQ(*Style3, getGoogleStyle()); 11657 11658 // Test 4: error on invalid fallback style 11659 auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS); 11660 ASSERT_FALSE((bool)Style4); 11661 llvm::consumeError(Style4.takeError()); 11662 11663 // Test 5: error on invalid yaml on command line 11664 auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS); 11665 ASSERT_FALSE((bool)Style5); 11666 llvm::consumeError(Style5.takeError()); 11667 11668 // Test 6: error on invalid style 11669 auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS); 11670 ASSERT_FALSE((bool)Style6); 11671 llvm::consumeError(Style6.takeError()); 11672 11673 // Test 7: found config file, error on parsing it 11674 ASSERT_TRUE( 11675 FS.addFile("/d/.clang-format", 0, 11676 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n" 11677 "InvalidKey: InvalidValue"))); 11678 ASSERT_TRUE( 11679 FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 11680 auto Style7 = getStyle("file", "/d/.clang-format", "LLVM", "", &FS); 11681 ASSERT_FALSE((bool)Style7); 11682 llvm::consumeError(Style7.takeError()); 11683 } 11684 11685 TEST_F(ReplacementTest, FormatCodeAfterReplacements) { 11686 // Column limit is 20. 11687 std::string Code = "Type *a =\n" 11688 " new Type();\n" 11689 "g(iiiii, 0, jjjjj,\n" 11690 " 0, kkkkk, 0, mm);\n" 11691 "int bad = format ;"; 11692 std::string Expected = "auto a = new Type();\n" 11693 "g(iiiii, nullptr,\n" 11694 " jjjjj, nullptr,\n" 11695 " kkkkk, nullptr,\n" 11696 " mm);\n" 11697 "int bad = format ;"; 11698 FileID ID = Context.createInMemoryFile("format.cpp", Code); 11699 tooling::Replacements Replaces = toReplacements( 11700 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6, 11701 "auto "), 11702 tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1, 11703 "nullptr"), 11704 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1, 11705 "nullptr"), 11706 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1, 11707 "nullptr")}); 11708 11709 format::FormatStyle Style = format::getLLVMStyle(); 11710 Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility. 11711 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 11712 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 11713 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 11714 auto Result = applyAllReplacements(Code, *FormattedReplaces); 11715 EXPECT_TRUE(static_cast<bool>(Result)); 11716 EXPECT_EQ(Expected, *Result); 11717 } 11718 11719 TEST_F(ReplacementTest, SortIncludesAfterReplacement) { 11720 std::string Code = "#include \"a.h\"\n" 11721 "#include \"c.h\"\n" 11722 "\n" 11723 "int main() {\n" 11724 " return 0;\n" 11725 "}"; 11726 std::string Expected = "#include \"a.h\"\n" 11727 "#include \"b.h\"\n" 11728 "#include \"c.h\"\n" 11729 "\n" 11730 "int main() {\n" 11731 " return 0;\n" 11732 "}"; 11733 FileID ID = Context.createInMemoryFile("fix.cpp", Code); 11734 tooling::Replacements Replaces = toReplacements( 11735 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0, 11736 "#include \"b.h\"\n")}); 11737 11738 format::FormatStyle Style = format::getLLVMStyle(); 11739 Style.SortIncludes = true; 11740 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 11741 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 11742 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 11743 auto Result = applyAllReplacements(Code, *FormattedReplaces); 11744 EXPECT_TRUE(static_cast<bool>(Result)); 11745 EXPECT_EQ(Expected, *Result); 11746 } 11747 11748 TEST_F(FormatTest, FormatSortsUsingDeclarations) { 11749 EXPECT_EQ("using std::cin;\n" 11750 "using std::cout;", 11751 format("using std::cout;\n" 11752 "using std::cin;", getGoogleStyle())); 11753 } 11754 11755 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) { 11756 format::FormatStyle Style = format::getLLVMStyle(); 11757 Style.Standard = FormatStyle::LS_Cpp03; 11758 // cpp03 recognize this string as identifier u8 and literal character 'a' 11759 EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style)); 11760 } 11761 11762 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) { 11763 // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers 11764 // all modes, including C++11, C++14 and C++17 11765 EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';")); 11766 } 11767 11768 TEST_F(FormatTest, DoNotFormatLikelyXml) { 11769 EXPECT_EQ("<!-- ;> -->", 11770 format("<!-- ;> -->", getGoogleStyle())); 11771 EXPECT_EQ(" <!-- >; -->", 11772 format(" <!-- >; -->", getGoogleStyle())); 11773 } 11774 11775 TEST_F(FormatTest, StructuredBindings) { 11776 // Structured bindings is a C++17 feature. 11777 // all modes, including C++11, C++14 and C++17 11778 verifyFormat("auto [a, b] = f();"); 11779 EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();")); 11780 EXPECT_EQ("const auto [a, b] = f();", format("const auto[a, b] = f();")); 11781 EXPECT_EQ("auto const [a, b] = f();", format("auto const[a, b] = f();")); 11782 EXPECT_EQ("auto const volatile [a, b] = f();", 11783 format("auto const volatile[a, b] = f();")); 11784 EXPECT_EQ("auto [a, b, c] = f();", format("auto [ a , b,c ] = f();")); 11785 EXPECT_EQ("auto &[a, b, c] = f();", 11786 format("auto &[ a , b,c ] = f();")); 11787 EXPECT_EQ("auto &&[a, b, c] = f();", 11788 format("auto &&[ a , b,c ] = f();")); 11789 EXPECT_EQ("auto const &[a, b] = f();", format("auto const&[a, b] = f();")); 11790 EXPECT_EQ("auto const volatile &&[a, b] = f();", 11791 format("auto const volatile &&[a, b] = f();")); 11792 EXPECT_EQ("auto const &&[a, b] = f();", format("auto const && [a, b] = f();")); 11793 EXPECT_EQ("const auto &[a, b] = f();", format("const auto & [a, b] = f();")); 11794 EXPECT_EQ("const auto volatile &&[a, b] = f();", 11795 format("const auto volatile &&[a, b] = f();")); 11796 EXPECT_EQ("volatile const auto &&[a, b] = f();", 11797 format("volatile const auto &&[a, b] = f();")); 11798 EXPECT_EQ("const auto &&[a, b] = f();", format("const auto && [a, b] = f();")); 11799 11800 // Make sure we don't mistake structured bindings for lambdas. 11801 FormatStyle PointerMiddle = getLLVMStyle(); 11802 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 11803 verifyFormat("auto [a1, b]{A * i};", getGoogleStyle()); 11804 verifyFormat("auto [a2, b]{A * i};", getLLVMStyle()); 11805 verifyFormat("auto [a3, b]{A * i};", PointerMiddle); 11806 verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle()); 11807 verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle()); 11808 verifyFormat("auto const [a3, b]{A * i};", PointerMiddle); 11809 verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle()); 11810 verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle()); 11811 verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle); 11812 verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle()); 11813 verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle()); 11814 verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle); 11815 11816 EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}", 11817 format("for (const auto && [a, b] : some_range) {\n}")); 11818 EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}", 11819 format("for (const auto & [a, b] : some_range) {\n}")); 11820 EXPECT_EQ("for (const auto [a, b] : some_range) {\n}", 11821 format("for (const auto[a, b] : some_range) {\n}")); 11822 EXPECT_EQ("auto [x, y](expr);", format("auto[x,y] (expr);")); 11823 EXPECT_EQ("auto &[x, y](expr);", format("auto & [x,y] (expr);")); 11824 EXPECT_EQ("auto &&[x, y](expr);", format("auto && [x,y] (expr);")); 11825 EXPECT_EQ("auto const &[x, y](expr);", format("auto const & [x,y] (expr);")); 11826 EXPECT_EQ("auto const &&[x, y](expr);", format("auto const && [x,y] (expr);")); 11827 EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y] {expr};")); 11828 EXPECT_EQ("auto const &[x, y]{expr};", format("auto const & [x,y] {expr};")); 11829 EXPECT_EQ("auto const &&[x, y]{expr};", format("auto const && [x,y] {expr};")); 11830 11831 format::FormatStyle Spaces = format::getLLVMStyle(); 11832 Spaces.SpacesInSquareBrackets = true; 11833 verifyFormat("auto [ a, b ] = f();", Spaces); 11834 verifyFormat("auto &&[ a, b ] = f();", Spaces); 11835 verifyFormat("auto &[ a, b ] = f();", Spaces); 11836 verifyFormat("auto const &&[ a, b ] = f();", Spaces); 11837 verifyFormat("auto const &[ a, b ] = f();", Spaces); 11838 } 11839 11840 } // end namespace 11841 } // end namespace format 11842 } // end namespace clang 11843