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 verifyFormat("#ifndef HEADER_H\n" 2488 "#define HEADER_H\n" 2489 "code();\n" 2490 "#endif", 2491 Style); 2492 // Include guards must have a #define with the same variable immediately 2493 // after #ifndef. 2494 verifyFormat("#ifndef NOT_GUARD\n" 2495 "# define FOO\n" 2496 "code();\n" 2497 "#endif", 2498 Style); 2499 2500 // Include guards must cover the entire file. 2501 verifyFormat("code();\n" 2502 "code();\n" 2503 "#ifndef NOT_GUARD\n" 2504 "# define NOT_GUARD\n" 2505 "code();\n" 2506 "#endif", 2507 Style); 2508 verifyFormat("#ifndef NOT_GUARD\n" 2509 "# define NOT_GUARD\n" 2510 "code();\n" 2511 "#endif\n" 2512 "code();", 2513 Style); 2514 // Test with trailing blank lines. 2515 verifyFormat("#ifndef HEADER_H\n" 2516 "#define HEADER_H\n" 2517 "code();\n" 2518 "#endif\n", 2519 Style); 2520 // Include guards don't have #else. 2521 verifyFormat("#ifndef NOT_GUARD\n" 2522 "# define NOT_GUARD\n" 2523 "code();\n" 2524 "#else\n" 2525 "#endif", 2526 Style); 2527 verifyFormat("#ifndef NOT_GUARD\n" 2528 "# define NOT_GUARD\n" 2529 "code();\n" 2530 "#elif FOO\n" 2531 "#endif", 2532 Style); 2533 // FIXME: This doesn't handle the case where there's code between the 2534 // #ifndef and #define but all other conditions hold. This is because when 2535 // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the 2536 // previous code line yet, so we can't detect it. 2537 EXPECT_EQ("#ifndef NOT_GUARD\n" 2538 "code();\n" 2539 "#define NOT_GUARD\n" 2540 "code();\n" 2541 "#endif", 2542 format("#ifndef NOT_GUARD\n" 2543 "code();\n" 2544 "# define NOT_GUARD\n" 2545 "code();\n" 2546 "#endif", 2547 Style)); 2548 // FIXME: This doesn't handle cases where legitimate preprocessor lines may 2549 // be outside an include guard. Examples are #pragma once and 2550 // #pragma GCC diagnostic, or anything else that does not change the meaning 2551 // of the file if it's included multiple times. 2552 EXPECT_EQ("#ifdef WIN32\n" 2553 "# pragma once\n" 2554 "#endif\n" 2555 "#ifndef HEADER_H\n" 2556 "# define HEADER_H\n" 2557 "code();\n" 2558 "#endif", 2559 format("#ifdef WIN32\n" 2560 "# pragma once\n" 2561 "#endif\n" 2562 "#ifndef HEADER_H\n" 2563 "#define HEADER_H\n" 2564 "code();\n" 2565 "#endif", 2566 Style)); 2567 // FIXME: This does not detect when there is a single non-preprocessor line 2568 // in front of an include-guard-like structure where other conditions hold 2569 // because ScopedLineState hides the line. 2570 EXPECT_EQ("code();\n" 2571 "#ifndef HEADER_H\n" 2572 "#define HEADER_H\n" 2573 "code();\n" 2574 "#endif", 2575 format("code();\n" 2576 "#ifndef HEADER_H\n" 2577 "# define HEADER_H\n" 2578 "code();\n" 2579 "#endif", 2580 Style)); 2581 // FIXME: The comment indent corrector in TokenAnnotator gets thrown off by 2582 // preprocessor indentation. 2583 EXPECT_EQ("#if 1\n" 2584 " // comment\n" 2585 "# define A 0\n" 2586 "// comment\n" 2587 "# define B 0\n" 2588 "#endif", 2589 format("#if 1\n" 2590 "// comment\n" 2591 "# define A 0\n" 2592 " // comment\n" 2593 "# define B 0\n" 2594 "#endif", 2595 Style)); 2596 // Test with tabs. 2597 Style.UseTab = FormatStyle::UT_Always; 2598 Style.IndentWidth = 8; 2599 Style.TabWidth = 8; 2600 verifyFormat("#ifdef _WIN32\n" 2601 "#\tdefine A 0\n" 2602 "#\tifdef VAR2\n" 2603 "#\t\tdefine B 1\n" 2604 "#\t\tinclude <someheader.h>\n" 2605 "#\t\tdefine MACRO \\\n" 2606 "\t\t\tsome_very_long_func_aaaaaaaaaa();\n" 2607 "#\tendif\n" 2608 "#else\n" 2609 "#\tdefine A 1\n" 2610 "#endif", 2611 Style); 2612 2613 // Regression test: Multiline-macro inside include guards. 2614 verifyFormat("#ifndef HEADER_H\n" 2615 "#define HEADER_H\n" 2616 "#define A() \\\n" 2617 " int i; \\\n" 2618 " int j;\n" 2619 "#endif // HEADER_H", 2620 getLLVMStyleWithColumns(20)); 2621 } 2622 2623 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { 2624 verifyFormat("{\n { a #c; }\n}"); 2625 } 2626 2627 TEST_F(FormatTest, FormatUnbalancedStructuralElements) { 2628 EXPECT_EQ("#define A \\\n { \\\n {\nint i;", 2629 format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); 2630 EXPECT_EQ("#define A \\\n } \\\n }\nint i;", 2631 format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); 2632 } 2633 2634 TEST_F(FormatTest, EscapedNewlines) { 2635 FormatStyle Narrow = getLLVMStyleWithColumns(11); 2636 EXPECT_EQ("#define A \\\n int i; \\\n int j;", 2637 format("#define A \\\nint i;\\\n int j;", Narrow)); 2638 EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;")); 2639 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 2640 EXPECT_EQ("/* \\ \\ \\\n */", format("\\\n/* \\ \\ \\\n */")); 2641 EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>")); 2642 2643 FormatStyle AlignLeft = getLLVMStyle(); 2644 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 2645 EXPECT_EQ("#define MACRO(x) \\\n" 2646 "private: \\\n" 2647 " int x(int a);\n", 2648 format("#define MACRO(x) \\\n" 2649 "private: \\\n" 2650 " int x(int a);\n", 2651 AlignLeft)); 2652 2653 // CRLF line endings 2654 EXPECT_EQ("#define A \\\r\n int i; \\\r\n int j;", 2655 format("#define A \\\r\nint i;\\\r\n int j;", Narrow)); 2656 EXPECT_EQ("#define A\r\n\r\nint i;", format("#define A \\\r\n\r\n int i;")); 2657 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 2658 EXPECT_EQ("/* \\ \\ \\\r\n */", format("\\\r\n/* \\ \\ \\\r\n */")); 2659 EXPECT_EQ("<a\r\n\\\\\r\n>", format("<a\r\n\\\\\r\n>")); 2660 EXPECT_EQ("#define MACRO(x) \\\r\n" 2661 "private: \\\r\n" 2662 " int x(int a);\r\n", 2663 format("#define MACRO(x) \\\r\n" 2664 "private: \\\r\n" 2665 " int x(int a);\r\n", 2666 AlignLeft)); 2667 2668 FormatStyle DontAlign = getLLVMStyle(); 2669 DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 2670 DontAlign.MaxEmptyLinesToKeep = 3; 2671 // FIXME: can't use verifyFormat here because the newline before 2672 // "public:" is not inserted the first time it's reformatted 2673 EXPECT_EQ("#define A \\\n" 2674 " class Foo { \\\n" 2675 " void bar(); \\\n" 2676 "\\\n" 2677 "\\\n" 2678 "\\\n" 2679 " public: \\\n" 2680 " void baz(); \\\n" 2681 " };", 2682 format("#define A \\\n" 2683 " class Foo { \\\n" 2684 " void bar(); \\\n" 2685 "\\\n" 2686 "\\\n" 2687 "\\\n" 2688 " public: \\\n" 2689 " void baz(); \\\n" 2690 " };", 2691 DontAlign)); 2692 } 2693 2694 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { 2695 verifyFormat("#define A \\\n" 2696 " int v( \\\n" 2697 " a); \\\n" 2698 " int i;", 2699 getLLVMStyleWithColumns(11)); 2700 } 2701 2702 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { 2703 EXPECT_EQ( 2704 "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 2705 " \\\n" 2706 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 2707 "\n" 2708 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 2709 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", 2710 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" 2711 "\\\n" 2712 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 2713 " \n" 2714 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 2715 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); 2716 } 2717 2718 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { 2719 EXPECT_EQ("int\n" 2720 "#define A\n" 2721 " a;", 2722 format("int\n#define A\na;")); 2723 verifyFormat("functionCallTo(\n" 2724 " someOtherFunction(\n" 2725 " withSomeParameters, whichInSequence,\n" 2726 " areLongerThanALine(andAnotherCall,\n" 2727 "#define A B\n" 2728 " withMoreParamters,\n" 2729 " whichStronglyInfluenceTheLayout),\n" 2730 " andMoreParameters),\n" 2731 " trailing);", 2732 getLLVMStyleWithColumns(69)); 2733 verifyFormat("Foo::Foo()\n" 2734 "#ifdef BAR\n" 2735 " : baz(0)\n" 2736 "#endif\n" 2737 "{\n" 2738 "}"); 2739 verifyFormat("void f() {\n" 2740 " if (true)\n" 2741 "#ifdef A\n" 2742 " f(42);\n" 2743 " x();\n" 2744 "#else\n" 2745 " g();\n" 2746 " x();\n" 2747 "#endif\n" 2748 "}"); 2749 verifyFormat("void f(param1, param2,\n" 2750 " param3,\n" 2751 "#ifdef A\n" 2752 " param4(param5,\n" 2753 "#ifdef A1\n" 2754 " param6,\n" 2755 "#ifdef A2\n" 2756 " param7),\n" 2757 "#else\n" 2758 " param8),\n" 2759 " param9,\n" 2760 "#endif\n" 2761 " param10,\n" 2762 "#endif\n" 2763 " param11)\n" 2764 "#else\n" 2765 " param12)\n" 2766 "#endif\n" 2767 "{\n" 2768 " x();\n" 2769 "}", 2770 getLLVMStyleWithColumns(28)); 2771 verifyFormat("#if 1\n" 2772 "int i;"); 2773 verifyFormat("#if 1\n" 2774 "#endif\n" 2775 "#if 1\n" 2776 "#else\n" 2777 "#endif\n"); 2778 verifyFormat("DEBUG({\n" 2779 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2780 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 2781 "});\n" 2782 "#if a\n" 2783 "#else\n" 2784 "#endif"); 2785 2786 verifyIncompleteFormat("void f(\n" 2787 "#if A\n" 2788 ");\n" 2789 "#else\n" 2790 "#endif"); 2791 } 2792 2793 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) { 2794 verifyFormat("#endif\n" 2795 "#if B"); 2796 } 2797 2798 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) { 2799 FormatStyle SingleLine = getLLVMStyle(); 2800 SingleLine.AllowShortIfStatementsOnASingleLine = true; 2801 verifyFormat("#if 0\n" 2802 "#elif 1\n" 2803 "#endif\n" 2804 "void foo() {\n" 2805 " if (test) foo2();\n" 2806 "}", 2807 SingleLine); 2808 } 2809 2810 TEST_F(FormatTest, LayoutBlockInsideParens) { 2811 verifyFormat("functionCall({ int i; });"); 2812 verifyFormat("functionCall({\n" 2813 " int i;\n" 2814 " int j;\n" 2815 "});"); 2816 verifyFormat("functionCall(\n" 2817 " {\n" 2818 " int i;\n" 2819 " int j;\n" 2820 " },\n" 2821 " aaaa, bbbb, cccc);"); 2822 verifyFormat("functionA(functionB({\n" 2823 " int i;\n" 2824 " int j;\n" 2825 " }),\n" 2826 " aaaa, bbbb, cccc);"); 2827 verifyFormat("functionCall(\n" 2828 " {\n" 2829 " int i;\n" 2830 " int j;\n" 2831 " },\n" 2832 " aaaa, bbbb, // comment\n" 2833 " cccc);"); 2834 verifyFormat("functionA(functionB({\n" 2835 " int i;\n" 2836 " int j;\n" 2837 " }),\n" 2838 " aaaa, bbbb, // comment\n" 2839 " cccc);"); 2840 verifyFormat("functionCall(aaaa, bbbb, { int i; });"); 2841 verifyFormat("functionCall(aaaa, bbbb, {\n" 2842 " int i;\n" 2843 " int j;\n" 2844 "});"); 2845 verifyFormat( 2846 "Aaa(\n" // FIXME: There shouldn't be a linebreak here. 2847 " {\n" 2848 " int i; // break\n" 2849 " },\n" 2850 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 2851 " ccccccccccccccccc));"); 2852 verifyFormat("DEBUG({\n" 2853 " if (a)\n" 2854 " f();\n" 2855 "});"); 2856 } 2857 2858 TEST_F(FormatTest, LayoutBlockInsideStatement) { 2859 EXPECT_EQ("SOME_MACRO { int i; }\n" 2860 "int i;", 2861 format(" SOME_MACRO {int i;} int i;")); 2862 } 2863 2864 TEST_F(FormatTest, LayoutNestedBlocks) { 2865 verifyFormat("void AddOsStrings(unsigned bitmask) {\n" 2866 " struct s {\n" 2867 " int i;\n" 2868 " };\n" 2869 " s kBitsToOs[] = {{10}};\n" 2870 " for (int i = 0; i < 10; ++i)\n" 2871 " return;\n" 2872 "}"); 2873 verifyFormat("call(parameter, {\n" 2874 " something();\n" 2875 " // Comment using all columns.\n" 2876 " somethingelse();\n" 2877 "});", 2878 getLLVMStyleWithColumns(40)); 2879 verifyFormat("DEBUG( //\n" 2880 " { f(); }, a);"); 2881 verifyFormat("DEBUG( //\n" 2882 " {\n" 2883 " f(); //\n" 2884 " },\n" 2885 " a);"); 2886 2887 EXPECT_EQ("call(parameter, {\n" 2888 " something();\n" 2889 " // Comment too\n" 2890 " // looooooooooong.\n" 2891 " somethingElse();\n" 2892 "});", 2893 format("call(parameter, {\n" 2894 " something();\n" 2895 " // Comment too looooooooooong.\n" 2896 " somethingElse();\n" 2897 "});", 2898 getLLVMStyleWithColumns(29))); 2899 EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });")); 2900 EXPECT_EQ("DEBUG({ // comment\n" 2901 " int i;\n" 2902 "});", 2903 format("DEBUG({ // comment\n" 2904 "int i;\n" 2905 "});")); 2906 EXPECT_EQ("DEBUG({\n" 2907 " int i;\n" 2908 "\n" 2909 " // comment\n" 2910 " int j;\n" 2911 "});", 2912 format("DEBUG({\n" 2913 " int i;\n" 2914 "\n" 2915 " // comment\n" 2916 " int j;\n" 2917 "});")); 2918 2919 verifyFormat("DEBUG({\n" 2920 " if (a)\n" 2921 " return;\n" 2922 "});"); 2923 verifyGoogleFormat("DEBUG({\n" 2924 " if (a) return;\n" 2925 "});"); 2926 FormatStyle Style = getGoogleStyle(); 2927 Style.ColumnLimit = 45; 2928 verifyFormat("Debug(aaaaa,\n" 2929 " {\n" 2930 " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n" 2931 " },\n" 2932 " a);", 2933 Style); 2934 2935 verifyFormat("SomeFunction({MACRO({ return output; }), b});"); 2936 2937 verifyNoCrash("^{v^{a}}"); 2938 } 2939 2940 TEST_F(FormatTest, FormatNestedBlocksInMacros) { 2941 EXPECT_EQ("#define MACRO() \\\n" 2942 " Debug(aaa, /* force line break */ \\\n" 2943 " { \\\n" 2944 " int i; \\\n" 2945 " int j; \\\n" 2946 " })", 2947 format("#define MACRO() Debug(aaa, /* force line break */ \\\n" 2948 " { int i; int j; })", 2949 getGoogleStyle())); 2950 2951 EXPECT_EQ("#define A \\\n" 2952 " [] { \\\n" 2953 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 2954 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 2955 " }", 2956 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 2957 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 2958 getGoogleStyle())); 2959 } 2960 2961 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { 2962 EXPECT_EQ("{}", format("{}")); 2963 verifyFormat("enum E {};"); 2964 verifyFormat("enum E {}"); 2965 } 2966 2967 TEST_F(FormatTest, FormatBeginBlockEndMacros) { 2968 FormatStyle Style = getLLVMStyle(); 2969 Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$"; 2970 Style.MacroBlockEnd = "^[A-Z_]+_END$"; 2971 verifyFormat("FOO_BEGIN\n" 2972 " FOO_ENTRY\n" 2973 "FOO_END", Style); 2974 verifyFormat("FOO_BEGIN\n" 2975 " NESTED_FOO_BEGIN\n" 2976 " NESTED_FOO_ENTRY\n" 2977 " NESTED_FOO_END\n" 2978 "FOO_END", Style); 2979 verifyFormat("FOO_BEGIN(Foo, Bar)\n" 2980 " int x;\n" 2981 " x = 1;\n" 2982 "FOO_END(Baz)", Style); 2983 } 2984 2985 //===----------------------------------------------------------------------===// 2986 // Line break tests. 2987 //===----------------------------------------------------------------------===// 2988 2989 TEST_F(FormatTest, PreventConfusingIndents) { 2990 verifyFormat( 2991 "void f() {\n" 2992 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" 2993 " parameter, parameter, parameter)),\n" 2994 " SecondLongCall(parameter));\n" 2995 "}"); 2996 verifyFormat( 2997 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2998 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 2999 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3000 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 3001 verifyFormat( 3002 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3003 " [aaaaaaaaaaaaaaaaaaaaaaaa\n" 3004 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 3005 " [aaaaaaaaaaaaaaaaaaaaaaaa]];"); 3006 verifyFormat( 3007 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 3008 " aaaaaaaaaaaaaaaaaaaaaaaa<\n" 3009 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n" 3010 " aaaaaaaaaaaaaaaaaaaaaaaa>;"); 3011 verifyFormat("int a = bbbb && ccc &&\n" 3012 " fffff(\n" 3013 "#define A Just forcing a new line\n" 3014 " ddd);"); 3015 } 3016 3017 TEST_F(FormatTest, LineBreakingInBinaryExpressions) { 3018 verifyFormat( 3019 "bool aaaaaaa =\n" 3020 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n" 3021 " bbbbbbbb();"); 3022 verifyFormat( 3023 "bool aaaaaaa =\n" 3024 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n" 3025 " bbbbbbbb();"); 3026 3027 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 3028 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n" 3029 " ccccccccc == ddddddddddd;"); 3030 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 3031 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n" 3032 " ccccccccc == ddddddddddd;"); 3033 verifyFormat( 3034 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 3035 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n" 3036 " ccccccccc == ddddddddddd;"); 3037 3038 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 3039 " aaaaaa) &&\n" 3040 " bbbbbb && cccccc;"); 3041 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 3042 " aaaaaa) >>\n" 3043 " bbbbbb;"); 3044 verifyFormat("aa = Whitespaces.addUntouchableComment(\n" 3045 " SourceMgr.getSpellingColumnNumber(\n" 3046 " TheLine.Last->FormatTok.Tok.getLocation()) -\n" 3047 " 1);"); 3048 3049 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3050 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n" 3051 " cccccc) {\n}"); 3052 verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3053 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n" 3054 " cccccc) {\n}"); 3055 verifyFormat("b = a &&\n" 3056 " // Comment\n" 3057 " b.c && d;"); 3058 3059 // If the LHS of a comparison is not a binary expression itself, the 3060 // additional linebreak confuses many people. 3061 verifyFormat( 3062 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3063 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n" 3064 "}"); 3065 verifyFormat( 3066 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3067 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3068 "}"); 3069 verifyFormat( 3070 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n" 3071 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3072 "}"); 3073 verifyFormat( 3074 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3075 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n" 3076 "}"); 3077 // Even explicit parentheses stress the precedence enough to make the 3078 // additional break unnecessary. 3079 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3080 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3081 "}"); 3082 // This cases is borderline, but with the indentation it is still readable. 3083 verifyFormat( 3084 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3085 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3086 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 3087 "}", 3088 getLLVMStyleWithColumns(75)); 3089 3090 // If the LHS is a binary expression, we should still use the additional break 3091 // as otherwise the formatting hides the operator precedence. 3092 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3093 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3094 " 5) {\n" 3095 "}"); 3096 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3097 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n" 3098 " 5) {\n" 3099 "}"); 3100 3101 FormatStyle OnePerLine = getLLVMStyle(); 3102 OnePerLine.BinPackParameters = false; 3103 verifyFormat( 3104 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3105 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3106 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}", 3107 OnePerLine); 3108 3109 verifyFormat("int i = someFunction(aaaaaaa, 0)\n" 3110 " .aaa(aaaaaaaaaaaaa) *\n" 3111 " aaaaaaa +\n" 3112 " aaaaaaa;", 3113 getLLVMStyleWithColumns(40)); 3114 } 3115 3116 TEST_F(FormatTest, ExpressionIndentation) { 3117 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3118 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3119 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3120 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3121 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 3122 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n" 3123 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3124 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n" 3125 " ccccccccccccccccccccccccccccccccccccccccc;"); 3126 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3127 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3128 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3129 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3130 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3131 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3132 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3133 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3134 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3135 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3136 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3137 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3138 verifyFormat("if () {\n" 3139 "} else if (aaaaa && bbbbb > // break\n" 3140 " ccccc) {\n" 3141 "}"); 3142 verifyFormat("if () {\n" 3143 "} else if (aaaaa &&\n" 3144 " bbbbb > // break\n" 3145 " ccccc &&\n" 3146 " ddddd) {\n" 3147 "}"); 3148 3149 // Presence of a trailing comment used to change indentation of b. 3150 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n" 3151 " b;\n" 3152 "return aaaaaaaaaaaaaaaaaaa +\n" 3153 " b; //", 3154 getLLVMStyleWithColumns(30)); 3155 } 3156 3157 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) { 3158 // Not sure what the best system is here. Like this, the LHS can be found 3159 // immediately above an operator (everything with the same or a higher 3160 // indent). The RHS is aligned right of the operator and so compasses 3161 // everything until something with the same indent as the operator is found. 3162 // FIXME: Is this a good system? 3163 FormatStyle Style = getLLVMStyle(); 3164 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 3165 verifyFormat( 3166 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3167 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3168 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3169 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3170 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3171 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3172 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3173 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3174 " > ccccccccccccccccccccccccccccccccccccccccc;", 3175 Style); 3176 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3177 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3178 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3179 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3180 Style); 3181 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3182 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3183 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3184 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3185 Style); 3186 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3187 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3188 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3189 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3190 Style); 3191 verifyFormat("if () {\n" 3192 "} else if (aaaaa\n" 3193 " && bbbbb // break\n" 3194 " > ccccc) {\n" 3195 "}", 3196 Style); 3197 verifyFormat("return (a)\n" 3198 " // comment\n" 3199 " + b;", 3200 Style); 3201 verifyFormat( 3202 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3203 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3204 " + cc;", 3205 Style); 3206 3207 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3208 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 3209 Style); 3210 3211 // Forced by comments. 3212 verifyFormat( 3213 "unsigned ContentSize =\n" 3214 " sizeof(int16_t) // DWARF ARange version number\n" 3215 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 3216 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 3217 " + sizeof(int8_t); // Segment Size (in bytes)"); 3218 3219 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 3220 " == boost::fusion::at_c<1>(iiii).second;", 3221 Style); 3222 3223 Style.ColumnLimit = 60; 3224 verifyFormat("zzzzzzzzzz\n" 3225 " = bbbbbbbbbbbbbbbbb\n" 3226 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 3227 Style); 3228 } 3229 3230 TEST_F(FormatTest, EnforcedOperatorWraps) { 3231 // Here we'd like to wrap after the || operators, but a comment is forcing an 3232 // earlier wrap. 3233 verifyFormat("bool x = aaaaa //\n" 3234 " || bbbbb\n" 3235 " //\n" 3236 " || cccc;"); 3237 } 3238 3239 TEST_F(FormatTest, NoOperandAlignment) { 3240 FormatStyle Style = getLLVMStyle(); 3241 Style.AlignOperands = false; 3242 verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n" 3243 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3244 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 3245 Style); 3246 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 3247 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3248 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3249 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3250 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3251 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3252 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3253 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3254 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3255 " > ccccccccccccccccccccccccccccccccccccccccc;", 3256 Style); 3257 3258 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3259 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3260 " + cc;", 3261 Style); 3262 verifyFormat("int a = aa\n" 3263 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3264 " * cccccccccccccccccccccccccccccccccccc;\n", 3265 Style); 3266 3267 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 3268 verifyFormat("return (a > b\n" 3269 " // comment1\n" 3270 " // comment2\n" 3271 " || c);", 3272 Style); 3273 } 3274 3275 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) { 3276 FormatStyle Style = getLLVMStyle(); 3277 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 3278 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 3279 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3280 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 3281 Style); 3282 } 3283 3284 TEST_F(FormatTest, AllowBinPackingInsideArguments) { 3285 FormatStyle Style = getLLVMStyle(); 3286 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 3287 Style.BinPackArguments = false; 3288 Style.ColumnLimit = 40; 3289 verifyFormat("void test() {\n" 3290 " someFunction(\n" 3291 " this + argument + is + quite\n" 3292 " + long + so + it + gets + wrapped\n" 3293 " + but + remains + bin - packed);\n" 3294 "}", 3295 Style); 3296 verifyFormat("void test() {\n" 3297 " someFunction(arg1,\n" 3298 " this + argument + is\n" 3299 " + quite + long + so\n" 3300 " + it + gets + wrapped\n" 3301 " + but + remains + bin\n" 3302 " - packed,\n" 3303 " arg3);\n" 3304 "}", 3305 Style); 3306 verifyFormat("void test() {\n" 3307 " someFunction(\n" 3308 " arg1,\n" 3309 " this + argument + has\n" 3310 " + anotherFunc(nested,\n" 3311 " calls + whose\n" 3312 " + arguments\n" 3313 " + are + also\n" 3314 " + wrapped,\n" 3315 " in + addition)\n" 3316 " + to + being + bin - packed,\n" 3317 " arg3);\n" 3318 "}", 3319 Style); 3320 3321 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 3322 verifyFormat("void test() {\n" 3323 " someFunction(\n" 3324 " arg1,\n" 3325 " this + argument + has +\n" 3326 " anotherFunc(nested,\n" 3327 " calls + whose +\n" 3328 " arguments +\n" 3329 " are + also +\n" 3330 " wrapped,\n" 3331 " in + addition) +\n" 3332 " to + being + bin - packed,\n" 3333 " arg3);\n" 3334 "}", 3335 Style); 3336 } 3337 3338 TEST_F(FormatTest, ConstructorInitializers) { 3339 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 3340 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", 3341 getLLVMStyleWithColumns(45)); 3342 verifyFormat("Constructor()\n" 3343 " : Inttializer(FitsOnTheLine) {}", 3344 getLLVMStyleWithColumns(44)); 3345 verifyFormat("Constructor()\n" 3346 " : Inttializer(FitsOnTheLine) {}", 3347 getLLVMStyleWithColumns(43)); 3348 3349 verifyFormat("template <typename T>\n" 3350 "Constructor() : Initializer(FitsOnTheLine) {}", 3351 getLLVMStyleWithColumns(45)); 3352 3353 verifyFormat( 3354 "SomeClass::Constructor()\n" 3355 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 3356 3357 verifyFormat( 3358 "SomeClass::Constructor()\n" 3359 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3360 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}"); 3361 verifyFormat( 3362 "SomeClass::Constructor()\n" 3363 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3364 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 3365 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3366 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3367 " : aaaaaaaaaa(aaaaaa) {}"); 3368 3369 verifyFormat("Constructor()\n" 3370 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3371 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3372 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3373 " aaaaaaaaaaaaaaaaaaaaaaa() {}"); 3374 3375 verifyFormat("Constructor()\n" 3376 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3377 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 3378 3379 verifyFormat("Constructor(int Parameter = 0)\n" 3380 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 3381 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}"); 3382 verifyFormat("Constructor()\n" 3383 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 3384 "}", 3385 getLLVMStyleWithColumns(60)); 3386 verifyFormat("Constructor()\n" 3387 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3388 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}"); 3389 3390 // Here a line could be saved by splitting the second initializer onto two 3391 // lines, but that is not desirable. 3392 verifyFormat("Constructor()\n" 3393 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 3394 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 3395 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 3396 3397 FormatStyle OnePerLine = getLLVMStyle(); 3398 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 3399 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false; 3400 verifyFormat("SomeClass::Constructor()\n" 3401 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3402 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3403 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3404 OnePerLine); 3405 verifyFormat("SomeClass::Constructor()\n" 3406 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 3407 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3408 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3409 OnePerLine); 3410 verifyFormat("MyClass::MyClass(int var)\n" 3411 " : some_var_(var), // 4 space indent\n" 3412 " some_other_var_(var + 1) { // lined up\n" 3413 "}", 3414 OnePerLine); 3415 verifyFormat("Constructor()\n" 3416 " : aaaaa(aaaaaa),\n" 3417 " aaaaa(aaaaaa),\n" 3418 " aaaaa(aaaaaa),\n" 3419 " aaaaa(aaaaaa),\n" 3420 " aaaaa(aaaaaa) {}", 3421 OnePerLine); 3422 verifyFormat("Constructor()\n" 3423 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 3424 " aaaaaaaaaaaaaaaaaaaaaa) {}", 3425 OnePerLine); 3426 OnePerLine.BinPackParameters = false; 3427 verifyFormat( 3428 "Constructor()\n" 3429 " : aaaaaaaaaaaaaaaaaaaaaaaa(\n" 3430 " aaaaaaaaaaa().aaa(),\n" 3431 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3432 OnePerLine); 3433 OnePerLine.ColumnLimit = 60; 3434 verifyFormat("Constructor()\n" 3435 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 3436 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 3437 OnePerLine); 3438 3439 EXPECT_EQ("Constructor()\n" 3440 " : // Comment forcing unwanted break.\n" 3441 " aaaa(aaaa) {}", 3442 format("Constructor() :\n" 3443 " // Comment forcing unwanted break.\n" 3444 " aaaa(aaaa) {}")); 3445 } 3446 3447 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) { 3448 FormatStyle Style = getLLVMStyle(); 3449 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 3450 3451 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 3452 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}", 3453 getStyleWithColumns(Style, 45)); 3454 verifyFormat("Constructor() :\n" 3455 " Initializer(FitsOnTheLine) {}", 3456 getStyleWithColumns(Style, 44)); 3457 verifyFormat("Constructor() :\n" 3458 " Initializer(FitsOnTheLine) {}", 3459 getStyleWithColumns(Style, 43)); 3460 3461 verifyFormat("template <typename T>\n" 3462 "Constructor() : Initializer(FitsOnTheLine) {}", 3463 getStyleWithColumns(Style, 50)); 3464 3465 verifyFormat( 3466 "SomeClass::Constructor() :\n" 3467 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 3468 Style); 3469 3470 verifyFormat( 3471 "SomeClass::Constructor() :\n" 3472 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3473 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3474 Style); 3475 verifyFormat( 3476 "SomeClass::Constructor() :\n" 3477 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3478 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 3479 Style); 3480 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3481 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 3482 " aaaaaaaaaa(aaaaaa) {}", 3483 Style); 3484 3485 verifyFormat("Constructor() :\n" 3486 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3487 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3488 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3489 " aaaaaaaaaaaaaaaaaaaaaaa() {}", 3490 Style); 3491 3492 verifyFormat("Constructor() :\n" 3493 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3494 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3495 Style); 3496 3497 verifyFormat("Constructor(int Parameter = 0) :\n" 3498 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 3499 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}", 3500 Style); 3501 verifyFormat("Constructor() :\n" 3502 " aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 3503 "}", 3504 getStyleWithColumns(Style, 60)); 3505 verifyFormat("Constructor() :\n" 3506 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3507 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}", 3508 Style); 3509 3510 // Here a line could be saved by splitting the second initializer onto two 3511 // lines, but that is not desirable. 3512 verifyFormat("Constructor() :\n" 3513 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 3514 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 3515 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3516 Style); 3517 3518 FormatStyle OnePerLine = Style; 3519 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 3520 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false; 3521 verifyFormat("SomeClass::Constructor() :\n" 3522 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3523 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3524 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3525 OnePerLine); 3526 verifyFormat("SomeClass::Constructor() :\n" 3527 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 3528 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3529 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3530 OnePerLine); 3531 verifyFormat("MyClass::MyClass(int var) :\n" 3532 " some_var_(var), // 4 space indent\n" 3533 " some_other_var_(var + 1) { // lined up\n" 3534 "}", 3535 OnePerLine); 3536 verifyFormat("Constructor() :\n" 3537 " aaaaa(aaaaaa),\n" 3538 " aaaaa(aaaaaa),\n" 3539 " aaaaa(aaaaaa),\n" 3540 " aaaaa(aaaaaa),\n" 3541 " aaaaa(aaaaaa) {}", 3542 OnePerLine); 3543 verifyFormat("Constructor() :\n" 3544 " aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 3545 " aaaaaaaaaaaaaaaaaaaaaa) {}", 3546 OnePerLine); 3547 OnePerLine.BinPackParameters = false; 3548 verifyFormat( 3549 "Constructor() :\n" 3550 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 3551 " aaaaaaaaaaa().aaa(),\n" 3552 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3553 OnePerLine); 3554 OnePerLine.ColumnLimit = 60; 3555 verifyFormat("Constructor() :\n" 3556 " aaaaaaaaaaaaaaaaaaaa(a),\n" 3557 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 3558 OnePerLine); 3559 3560 EXPECT_EQ("Constructor() :\n" 3561 " // Comment forcing unwanted break.\n" 3562 " aaaa(aaaa) {}", 3563 format("Constructor() :\n" 3564 " // Comment forcing unwanted break.\n" 3565 " aaaa(aaaa) {}", 3566 Style)); 3567 3568 Style.ColumnLimit = 0; 3569 verifyFormat("SomeClass::Constructor() :\n" 3570 " a(a) {}", 3571 Style); 3572 verifyFormat("SomeClass::Constructor() noexcept :\n" 3573 " a(a) {}", 3574 Style); 3575 verifyFormat("SomeClass::Constructor() :\n" 3576 " a(a), b(b), c(c) {}", 3577 Style); 3578 verifyFormat("SomeClass::Constructor() :\n" 3579 " a(a) {\n" 3580 " foo();\n" 3581 " bar();\n" 3582 "}", 3583 Style); 3584 3585 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 3586 verifyFormat("SomeClass::Constructor() :\n" 3587 " a(a), b(b), c(c) {\n" 3588 "}", 3589 Style); 3590 verifyFormat("SomeClass::Constructor() :\n" 3591 " a(a) {\n" 3592 "}", 3593 Style); 3594 3595 Style.ColumnLimit = 80; 3596 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 3597 Style.ConstructorInitializerIndentWidth = 2; 3598 verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", 3599 Style); 3600 verifyFormat("SomeClass::Constructor() :\n" 3601 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3602 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}", 3603 Style); 3604 } 3605 3606 #ifndef EXPENSIVE_CHECKS 3607 // Expensive checks enables libstdc++ checking which includes validating the 3608 // state of ranges used in std::priority_queue - this blows out the 3609 // runtime/scalability of the function and makes this test unacceptably slow. 3610 TEST_F(FormatTest, MemoizationTests) { 3611 // This breaks if the memoization lookup does not take \c Indent and 3612 // \c LastSpace into account. 3613 verifyFormat( 3614 "extern CFRunLoopTimerRef\n" 3615 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n" 3616 " CFTimeInterval interval, CFOptionFlags flags,\n" 3617 " CFIndex order, CFRunLoopTimerCallBack callout,\n" 3618 " CFRunLoopTimerContext *context) {}"); 3619 3620 // Deep nesting somewhat works around our memoization. 3621 verifyFormat( 3622 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3623 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3624 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3625 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3626 " aaaaa())))))))))))))))))))))))))))))))))))))));", 3627 getLLVMStyleWithColumns(65)); 3628 verifyFormat( 3629 "aaaaa(\n" 3630 " aaaaa,\n" 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))))))))))));", 3654 getLLVMStyleWithColumns(65)); 3655 verifyFormat( 3656 "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" 3657 " a),\n" 3658 " 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)", 3674 getLLVMStyleWithColumns(65)); 3675 3676 // This test takes VERY long when memoization is broken. 3677 FormatStyle OnePerLine = getLLVMStyle(); 3678 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 3679 OnePerLine.BinPackParameters = false; 3680 std::string input = "Constructor()\n" 3681 " : aaaa(a,\n"; 3682 for (unsigned i = 0, e = 80; i != e; ++i) { 3683 input += " a,\n"; 3684 } 3685 input += " a) {}"; 3686 verifyFormat(input, OnePerLine); 3687 } 3688 #endif 3689 3690 TEST_F(FormatTest, BreaksAsHighAsPossible) { 3691 verifyFormat( 3692 "void f() {\n" 3693 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" 3694 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" 3695 " f();\n" 3696 "}"); 3697 verifyFormat("if (Intervals[i].getRange().getFirst() <\n" 3698 " Intervals[i - 1].getRange().getLast()) {\n}"); 3699 } 3700 3701 TEST_F(FormatTest, BreaksFunctionDeclarations) { 3702 // Principially, we break function declarations in a certain order: 3703 // 1) break amongst arguments. 3704 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n" 3705 " Cccccccccccccc cccccccccccccc);"); 3706 verifyFormat("template <class TemplateIt>\n" 3707 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n" 3708 " TemplateIt *stop) {}"); 3709 3710 // 2) break after return type. 3711 verifyFormat( 3712 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3713 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);", 3714 getGoogleStyle()); 3715 3716 // 3) break after (. 3717 verifyFormat( 3718 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n" 3719 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);", 3720 getGoogleStyle()); 3721 3722 // 4) break before after nested name specifiers. 3723 verifyFormat( 3724 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3725 "SomeClasssssssssssssssssssssssssssssssssssssss::\n" 3726 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);", 3727 getGoogleStyle()); 3728 3729 // However, there are exceptions, if a sufficient amount of lines can be 3730 // saved. 3731 // FIXME: The precise cut-offs wrt. the number of saved lines might need some 3732 // more adjusting. 3733 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 3734 " Cccccccccccccc cccccccccc,\n" 3735 " Cccccccccccccc cccccccccc,\n" 3736 " Cccccccccccccc cccccccccc,\n" 3737 " Cccccccccccccc cccccccccc);"); 3738 verifyFormat( 3739 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3740 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3741 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3742 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);", 3743 getGoogleStyle()); 3744 verifyFormat( 3745 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 3746 " Cccccccccccccc cccccccccc,\n" 3747 " Cccccccccccccc cccccccccc,\n" 3748 " Cccccccccccccc cccccccccc,\n" 3749 " Cccccccccccccc cccccccccc,\n" 3750 " Cccccccccccccc cccccccccc,\n" 3751 " Cccccccccccccc cccccccccc);"); 3752 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 3753 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3754 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3755 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3756 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); 3757 3758 // Break after multi-line parameters. 3759 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3760 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3761 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3762 " bbbb bbbb);"); 3763 verifyFormat("void SomeLoooooooooooongFunction(\n" 3764 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 3765 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3766 " int bbbbbbbbbbbbb);"); 3767 3768 // Treat overloaded operators like other functions. 3769 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3770 "operator>(const SomeLoooooooooooooooooooooooooogType &other);"); 3771 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3772 "operator>>(const SomeLooooooooooooooooooooooooogType &other);"); 3773 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3774 "operator<<(const SomeLooooooooooooooooooooooooogType &other);"); 3775 verifyGoogleFormat( 3776 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n" 3777 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 3778 verifyGoogleFormat( 3779 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n" 3780 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 3781 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3782 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 3783 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n" 3784 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 3785 verifyGoogleFormat( 3786 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n" 3787 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3788 " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}"); 3789 verifyGoogleFormat( 3790 "template <typename T>\n" 3791 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3792 "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n" 3793 " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);"); 3794 3795 FormatStyle Style = getLLVMStyle(); 3796 Style.PointerAlignment = FormatStyle::PAS_Left; 3797 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3798 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}", 3799 Style); 3800 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n" 3801 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3802 Style); 3803 } 3804 3805 TEST_F(FormatTest, TrailingReturnType) { 3806 verifyFormat("auto foo() -> int;\n"); 3807 verifyFormat("struct S {\n" 3808 " auto bar() const -> int;\n" 3809 "};"); 3810 verifyFormat("template <size_t Order, typename T>\n" 3811 "auto load_img(const std::string &filename)\n" 3812 " -> alias::tensor<Order, T, mem::tag::cpu> {}"); 3813 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n" 3814 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}"); 3815 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}"); 3816 verifyFormat("template <typename T>\n" 3817 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n" 3818 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());"); 3819 3820 // Not trailing return types. 3821 verifyFormat("void f() { auto a = b->c(); }"); 3822 } 3823 3824 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { 3825 // Avoid breaking before trailing 'const' or other trailing annotations, if 3826 // they are not function-like. 3827 FormatStyle Style = getGoogleStyle(); 3828 Style.ColumnLimit = 47; 3829 verifyFormat("void someLongFunction(\n" 3830 " int someLoooooooooooooongParameter) const {\n}", 3831 getLLVMStyleWithColumns(47)); 3832 verifyFormat("LoooooongReturnType\n" 3833 "someLoooooooongFunction() const {}", 3834 getLLVMStyleWithColumns(47)); 3835 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n" 3836 " const {}", 3837 Style); 3838 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3839 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;"); 3840 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3841 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;"); 3842 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3843 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;"); 3844 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n" 3845 " aaaaaaaaaaa aaaaa) const override;"); 3846 verifyGoogleFormat( 3847 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 3848 " const override;"); 3849 3850 // Even if the first parameter has to be wrapped. 3851 verifyFormat("void someLongFunction(\n" 3852 " int someLongParameter) const {}", 3853 getLLVMStyleWithColumns(46)); 3854 verifyFormat("void someLongFunction(\n" 3855 " int someLongParameter) const {}", 3856 Style); 3857 verifyFormat("void someLongFunction(\n" 3858 " int someLongParameter) override {}", 3859 Style); 3860 verifyFormat("void someLongFunction(\n" 3861 " int someLongParameter) OVERRIDE {}", 3862 Style); 3863 verifyFormat("void someLongFunction(\n" 3864 " int someLongParameter) final {}", 3865 Style); 3866 verifyFormat("void someLongFunction(\n" 3867 " int someLongParameter) FINAL {}", 3868 Style); 3869 verifyFormat("void someLongFunction(\n" 3870 " int parameter) const override {}", 3871 Style); 3872 3873 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 3874 verifyFormat("void someLongFunction(\n" 3875 " int someLongParameter) const\n" 3876 "{\n" 3877 "}", 3878 Style); 3879 3880 // Unless these are unknown annotations. 3881 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n" 3882 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3883 " LONG_AND_UGLY_ANNOTATION;"); 3884 3885 // Breaking before function-like trailing annotations is fine to keep them 3886 // close to their arguments. 3887 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3888 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 3889 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 3890 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 3891 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 3892 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}"); 3893 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n" 3894 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);"); 3895 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});"); 3896 3897 verifyFormat( 3898 "void aaaaaaaaaaaaaaaaaa()\n" 3899 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n" 3900 " aaaaaaaaaaaaaaaaaaaaaaaaa));"); 3901 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3902 " __attribute__((unused));"); 3903 verifyGoogleFormat( 3904 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3905 " GUARDED_BY(aaaaaaaaaaaa);"); 3906 verifyGoogleFormat( 3907 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3908 " GUARDED_BY(aaaaaaaaaaaa);"); 3909 verifyGoogleFormat( 3910 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 3911 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 3912 verifyGoogleFormat( 3913 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 3914 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 3915 } 3916 3917 TEST_F(FormatTest, FunctionAnnotations) { 3918 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 3919 "int OldFunction(const string ¶meter) {}"); 3920 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 3921 "string OldFunction(const string ¶meter) {}"); 3922 verifyFormat("template <typename T>\n" 3923 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 3924 "string OldFunction(const string ¶meter) {}"); 3925 3926 // Not function annotations. 3927 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3928 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); 3929 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n" 3930 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}"); 3931 verifyFormat("MACRO(abc).function() // wrap\n" 3932 " << abc;"); 3933 verifyFormat("MACRO(abc)->function() // wrap\n" 3934 " << abc;"); 3935 verifyFormat("MACRO(abc)::function() // wrap\n" 3936 " << abc;"); 3937 } 3938 3939 TEST_F(FormatTest, BreaksDesireably) { 3940 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 3941 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 3942 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); 3943 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3944 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 3945 "}"); 3946 3947 verifyFormat( 3948 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3949 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 3950 3951 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3952 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3953 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 3954 3955 verifyFormat( 3956 "aaaaaaaa(aaaaaaaaaaaaa,\n" 3957 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3958 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 3959 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3960 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 3961 3962 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3963 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3964 3965 verifyFormat( 3966 "void f() {\n" 3967 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 3968 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 3969 "}"); 3970 verifyFormat( 3971 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3972 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 3973 verifyFormat( 3974 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3975 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 3976 verifyFormat( 3977 "aaaaaa(aaa,\n" 3978 " new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3979 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3980 " aaaa);"); 3981 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3982 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3983 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3984 3985 // Indent consistently independent of call expression and unary operator. 3986 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 3987 " dddddddddddddddddddddddddddddd));"); 3988 verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 3989 " dddddddddddddddddddddddddddddd));"); 3990 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n" 3991 " dddddddddddddddddddddddddddddd));"); 3992 3993 // This test case breaks on an incorrect memoization, i.e. an optimization not 3994 // taking into account the StopAt value. 3995 verifyFormat( 3996 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 3997 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 3998 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 3999 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4000 4001 verifyFormat("{\n {\n {\n" 4002 " Annotation.SpaceRequiredBefore =\n" 4003 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" 4004 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" 4005 " }\n }\n}"); 4006 4007 // Break on an outer level if there was a break on an inner level. 4008 EXPECT_EQ("f(g(h(a, // comment\n" 4009 " b, c),\n" 4010 " d, e),\n" 4011 " x, y);", 4012 format("f(g(h(a, // comment\n" 4013 " b, c), d, e), x, y);")); 4014 4015 // Prefer breaking similar line breaks. 4016 verifyFormat( 4017 "const int kTrackingOptions = NSTrackingMouseMoved |\n" 4018 " NSTrackingMouseEnteredAndExited |\n" 4019 " NSTrackingActiveAlways;"); 4020 } 4021 4022 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) { 4023 FormatStyle NoBinPacking = getGoogleStyle(); 4024 NoBinPacking.BinPackParameters = false; 4025 NoBinPacking.BinPackArguments = true; 4026 verifyFormat("void f() {\n" 4027 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n" 4028 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 4029 "}", 4030 NoBinPacking); 4031 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n" 4032 " int aaaaaaaaaaaaaaaaaaaa,\n" 4033 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4034 NoBinPacking); 4035 4036 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 4037 verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4038 " vector<int> bbbbbbbbbbbbbbb);", 4039 NoBinPacking); 4040 // FIXME: This behavior difference is probably not wanted. However, currently 4041 // we cannot distinguish BreakBeforeParameter being set because of the wrapped 4042 // template arguments from BreakBeforeParameter being set because of the 4043 // one-per-line formatting. 4044 verifyFormat( 4045 "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n" 4046 " aaaaaaaaaa> aaaaaaaaaa);", 4047 NoBinPacking); 4048 verifyFormat( 4049 "void fffffffffff(\n" 4050 " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n" 4051 " aaaaaaaaaa);"); 4052 } 4053 4054 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { 4055 FormatStyle NoBinPacking = getGoogleStyle(); 4056 NoBinPacking.BinPackParameters = false; 4057 NoBinPacking.BinPackArguments = false; 4058 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" 4059 " aaaaaaaaaaaaaaaaaaaa,\n" 4060 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);", 4061 NoBinPacking); 4062 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n" 4063 " aaaaaaaaaaaaa,\n" 4064 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));", 4065 NoBinPacking); 4066 verifyFormat( 4067 "aaaaaaaa(aaaaaaaaaaaaa,\n" 4068 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4069 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 4070 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4071 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));", 4072 NoBinPacking); 4073 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 4074 " .aaaaaaaaaaaaaaaaaa();", 4075 NoBinPacking); 4076 verifyFormat("void f() {\n" 4077 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4078 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n" 4079 "}", 4080 NoBinPacking); 4081 4082 verifyFormat( 4083 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4084 " aaaaaaaaaaaa,\n" 4085 " aaaaaaaaaaaa);", 4086 NoBinPacking); 4087 verifyFormat( 4088 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" 4089 " ddddddddddddddddddddddddddddd),\n" 4090 " test);", 4091 NoBinPacking); 4092 4093 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" 4094 " aaaaaaaaaaaaaaaaaaaaaaa,\n" 4095 " aaaaaaaaaaaaaaaaaaaaaaa>\n" 4096 " aaaaaaaaaaaaaaaaaa;", 4097 NoBinPacking); 4098 verifyFormat("a(\"a\"\n" 4099 " \"a\",\n" 4100 " a);"); 4101 4102 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 4103 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" 4104 " aaaaaaaaa,\n" 4105 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4106 NoBinPacking); 4107 verifyFormat( 4108 "void f() {\n" 4109 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 4110 " .aaaaaaa();\n" 4111 "}", 4112 NoBinPacking); 4113 verifyFormat( 4114 "template <class SomeType, class SomeOtherType>\n" 4115 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}", 4116 NoBinPacking); 4117 } 4118 4119 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) { 4120 FormatStyle Style = getLLVMStyleWithColumns(15); 4121 Style.ExperimentalAutoDetectBinPacking = true; 4122 EXPECT_EQ("aaa(aaaa,\n" 4123 " aaaa,\n" 4124 " aaaa);\n" 4125 "aaa(aaaa,\n" 4126 " aaaa,\n" 4127 " aaaa);", 4128 format("aaa(aaaa,\n" // one-per-line 4129 " aaaa,\n" 4130 " aaaa );\n" 4131 "aaa(aaaa, aaaa, aaaa);", // inconclusive 4132 Style)); 4133 EXPECT_EQ("aaa(aaaa, aaaa,\n" 4134 " aaaa);\n" 4135 "aaa(aaaa, aaaa,\n" 4136 " aaaa);", 4137 format("aaa(aaaa, aaaa,\n" // bin-packed 4138 " aaaa );\n" 4139 "aaa(aaaa, aaaa, aaaa);", // inconclusive 4140 Style)); 4141 } 4142 4143 TEST_F(FormatTest, FormatsBuilderPattern) { 4144 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n" 4145 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" 4146 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n" 4147 " .StartsWith(\".init\", ORDER_INIT)\n" 4148 " .StartsWith(\".fini\", ORDER_FINI)\n" 4149 " .StartsWith(\".hash\", ORDER_HASH)\n" 4150 " .Default(ORDER_TEXT);\n"); 4151 4152 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" 4153 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); 4154 verifyFormat( 4155 "aaaaaaa->aaaaaaa\n" 4156 " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4157 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4158 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 4159 verifyFormat( 4160 "aaaaaaa->aaaaaaa\n" 4161 " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4162 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 4163 verifyFormat( 4164 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" 4165 " aaaaaaaaaaaaaa);"); 4166 verifyFormat( 4167 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n" 4168 " aaaaaa->aaaaaaaaaaaa()\n" 4169 " ->aaaaaaaaaaaaaaaa(\n" 4170 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4171 " ->aaaaaaaaaaaaaaaaa();"); 4172 verifyGoogleFormat( 4173 "void f() {\n" 4174 " someo->Add((new util::filetools::Handler(dir))\n" 4175 " ->OnEvent1(NewPermanentCallback(\n" 4176 " this, &HandlerHolderClass::EventHandlerCBA))\n" 4177 " ->OnEvent2(NewPermanentCallback(\n" 4178 " this, &HandlerHolderClass::EventHandlerCBB))\n" 4179 " ->OnEvent3(NewPermanentCallback(\n" 4180 " this, &HandlerHolderClass::EventHandlerCBC))\n" 4181 " ->OnEvent5(NewPermanentCallback(\n" 4182 " this, &HandlerHolderClass::EventHandlerCBD))\n" 4183 " ->OnEvent6(NewPermanentCallback(\n" 4184 " this, &HandlerHolderClass::EventHandlerCBE)));\n" 4185 "}"); 4186 4187 verifyFormat( 4188 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();"); 4189 verifyFormat("aaaaaaaaaaaaaaa()\n" 4190 " .aaaaaaaaaaaaaaa()\n" 4191 " .aaaaaaaaaaaaaaa()\n" 4192 " .aaaaaaaaaaaaaaa()\n" 4193 " .aaaaaaaaaaaaaaa();"); 4194 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4195 " .aaaaaaaaaaaaaaa()\n" 4196 " .aaaaaaaaaaaaaaa()\n" 4197 " .aaaaaaaaaaaaaaa();"); 4198 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4199 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4200 " .aaaaaaaaaaaaaaa();"); 4201 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n" 4202 " ->aaaaaaaaaaaaaae(0)\n" 4203 " ->aaaaaaaaaaaaaaa();"); 4204 4205 // Don't linewrap after very short segments. 4206 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4207 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4208 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4209 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4210 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4211 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4212 verifyFormat("aaa()\n" 4213 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4214 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4215 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4216 4217 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 4218 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4219 " .has<bbbbbbbbbbbbbbbbbbbbb>();"); 4220 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 4221 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 4222 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();"); 4223 4224 // Prefer not to break after empty parentheses. 4225 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n" 4226 " First->LastNewlineOffset);"); 4227 4228 // Prefer not to create "hanging" indents. 4229 verifyFormat( 4230 "return !soooooooooooooome_map\n" 4231 " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4232 " .second;"); 4233 verifyFormat( 4234 "return aaaaaaaaaaaaaaaa\n" 4235 " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n" 4236 " .aaaa(aaaaaaaaaaaaaa);"); 4237 // No hanging indent here. 4238 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n" 4239 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4240 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n" 4241 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4242 verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 4243 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4244 getLLVMStyleWithColumns(60)); 4245 verifyFormat("aaaaaaaaaaaaaaaaaa\n" 4246 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 4247 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4248 getLLVMStyleWithColumns(59)); 4249 verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4250 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4251 " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4252 } 4253 4254 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { 4255 verifyFormat( 4256 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4257 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); 4258 verifyFormat( 4259 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n" 4260 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}"); 4261 4262 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 4263 " ccccccccccccccccccccccccc) {\n}"); 4264 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n" 4265 " ccccccccccccccccccccccccc) {\n}"); 4266 4267 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 4268 " ccccccccccccccccccccccccc) {\n}"); 4269 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n" 4270 " ccccccccccccccccccccccccc) {\n}"); 4271 4272 verifyFormat( 4273 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" 4274 " ccccccccccccccccccccccccc) {\n}"); 4275 verifyFormat( 4276 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n" 4277 " ccccccccccccccccccccccccc) {\n}"); 4278 4279 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" 4280 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" 4281 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" 4282 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 4283 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n" 4284 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n" 4285 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n" 4286 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 4287 4288 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" 4289 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" 4290 " aaaaaaaaaaaaaaa != aa) {\n}"); 4291 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n" 4292 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n" 4293 " aaaaaaaaaaaaaaa != aa) {\n}"); 4294 } 4295 4296 TEST_F(FormatTest, BreaksAfterAssignments) { 4297 verifyFormat( 4298 "unsigned Cost =\n" 4299 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" 4300 " SI->getPointerAddressSpaceee());\n"); 4301 verifyFormat( 4302 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" 4303 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); 4304 4305 verifyFormat( 4306 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n" 4307 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); 4308 verifyFormat("unsigned OriginalStartColumn =\n" 4309 " SourceMgr.getSpellingColumnNumber(\n" 4310 " Current.FormatTok.getStartOfNonWhitespace()) -\n" 4311 " 1;"); 4312 } 4313 4314 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) { 4315 FormatStyle Style = getLLVMStyle(); 4316 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 4317 " bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;", 4318 Style); 4319 4320 Style.PenaltyBreakAssignment = 20; 4321 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 4322 " cccccccccccccccccccccccccc;", 4323 Style); 4324 } 4325 4326 TEST_F(FormatTest, AlignsAfterAssignments) { 4327 verifyFormat( 4328 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4329 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4330 verifyFormat( 4331 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4332 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4333 verifyFormat( 4334 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4335 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4336 verifyFormat( 4337 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4338 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 4339 verifyFormat( 4340 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" 4341 " aaaaaaaaaaaaaaaaaaaaaaaa +\n" 4342 " aaaaaaaaaaaaaaaaaaaaaaaa;"); 4343 } 4344 4345 TEST_F(FormatTest, AlignsAfterReturn) { 4346 verifyFormat( 4347 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4348 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4349 verifyFormat( 4350 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4351 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 4352 verifyFormat( 4353 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 4354 " aaaaaaaaaaaaaaaaaaaaaa();"); 4355 verifyFormat( 4356 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 4357 " aaaaaaaaaaaaaaaaaaaaaa());"); 4358 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4359 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4360 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4361 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n" 4362 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4363 verifyFormat("return\n" 4364 " // true if code is one of a or b.\n" 4365 " code == a || code == b;"); 4366 } 4367 4368 TEST_F(FormatTest, AlignsAfterOpenBracket) { 4369 verifyFormat( 4370 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 4371 " aaaaaaaaa aaaaaaa) {}"); 4372 verifyFormat( 4373 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 4374 " aaaaaaaaaaa aaaaaaaaa);"); 4375 verifyFormat( 4376 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 4377 " aaaaaaaaaaaaaaaaaaaaa));"); 4378 FormatStyle Style = getLLVMStyle(); 4379 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4380 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4381 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}", 4382 Style); 4383 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 4384 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);", 4385 Style); 4386 verifyFormat("SomeLongVariableName->someFunction(\n" 4387 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));", 4388 Style); 4389 verifyFormat( 4390 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 4391 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4392 Style); 4393 verifyFormat( 4394 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 4395 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4396 Style); 4397 verifyFormat( 4398 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 4399 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 4400 Style); 4401 4402 verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n" 4403 " ccccccc(aaaaaaaaaaaaaaaaa, //\n" 4404 " b));", 4405 Style); 4406 4407 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 4408 Style.BinPackArguments = false; 4409 Style.BinPackParameters = false; 4410 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4411 " aaaaaaaaaaa aaaaaaaa,\n" 4412 " aaaaaaaaa aaaaaaa,\n" 4413 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4414 Style); 4415 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 4416 " aaaaaaaaaaa aaaaaaaaa,\n" 4417 " aaaaaaaaaaa aaaaaaaaa,\n" 4418 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4419 Style); 4420 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n" 4421 " aaaaaaaaaaaaaaa,\n" 4422 " aaaaaaaaaaaaaaaaaaaaa,\n" 4423 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 4424 Style); 4425 verifyFormat( 4426 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n" 4427 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 4428 Style); 4429 verifyFormat( 4430 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n" 4431 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 4432 Style); 4433 verifyFormat( 4434 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 4435 " aaaaaaaaaaaaaaaaaaaaa(\n" 4436 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n" 4437 " aaaaaaaaaaaaaaaa);", 4438 Style); 4439 verifyFormat( 4440 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 4441 " aaaaaaaaaaaaaaaaaaaaa(\n" 4442 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n" 4443 " aaaaaaaaaaaaaaaa);", 4444 Style); 4445 } 4446 4447 TEST_F(FormatTest, ParenthesesAndOperandAlignment) { 4448 FormatStyle Style = getLLVMStyleWithColumns(40); 4449 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4450 " bbbbbbbbbbbbbbbbbbbbbb);", 4451 Style); 4452 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 4453 Style.AlignOperands = false; 4454 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4455 " bbbbbbbbbbbbbbbbbbbbbb);", 4456 Style); 4457 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4458 Style.AlignOperands = true; 4459 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4460 " bbbbbbbbbbbbbbbbbbbbbb);", 4461 Style); 4462 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4463 Style.AlignOperands = false; 4464 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4465 " bbbbbbbbbbbbbbbbbbbbbb);", 4466 Style); 4467 } 4468 4469 TEST_F(FormatTest, BreaksConditionalExpressions) { 4470 verifyFormat( 4471 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4472 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4473 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4474 verifyFormat( 4475 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 4476 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4477 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4478 verifyFormat( 4479 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4480 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4481 verifyFormat( 4482 "aaaa(aaaaaaaaa, aaaaaaaaa,\n" 4483 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4484 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4485 verifyFormat( 4486 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" 4487 " : aaaaaaaaaaaaa);"); 4488 verifyFormat( 4489 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4490 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4491 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4492 " aaaaaaaaaaaaa);"); 4493 verifyFormat( 4494 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4495 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4496 " aaaaaaaaaaaaa);"); 4497 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4498 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4499 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4500 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4501 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4502 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4503 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4504 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4505 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4506 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4507 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4508 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4509 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4510 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4511 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4512 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4513 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4514 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4515 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4516 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4517 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 4518 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4519 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4520 " : aaaaaaaaaaaaaaaa;"); 4521 verifyFormat( 4522 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4523 " ? aaaaaaaaaaaaaaa\n" 4524 " : aaaaaaaaaaaaaaa;"); 4525 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 4526 " aaaaaaaaa\n" 4527 " ? b\n" 4528 " : c);"); 4529 verifyFormat("return aaaa == bbbb\n" 4530 " // comment\n" 4531 " ? aaaa\n" 4532 " : bbbb;"); 4533 verifyFormat("unsigned Indent =\n" 4534 " format(TheLine.First,\n" 4535 " IndentForLevel[TheLine.Level] >= 0\n" 4536 " ? IndentForLevel[TheLine.Level]\n" 4537 " : TheLine * 2,\n" 4538 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 4539 getLLVMStyleWithColumns(60)); 4540 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 4541 " ? aaaaaaaaaaaaaaa\n" 4542 " : bbbbbbbbbbbbbbb //\n" 4543 " ? ccccccccccccccc\n" 4544 " : ddddddddddddddd;"); 4545 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 4546 " ? aaaaaaaaaaaaaaa\n" 4547 " : (bbbbbbbbbbbbbbb //\n" 4548 " ? ccccccccccccccc\n" 4549 " : ddddddddddddddd);"); 4550 verifyFormat( 4551 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4552 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4553 " aaaaaaaaaaaaaaaaaaaaa +\n" 4554 " aaaaaaaaaaaaaaaaaaaaa\n" 4555 " : aaaaaaaaaa;"); 4556 verifyFormat( 4557 "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4558 " : aaaaaaaaaaaaaaaaaaaaaa\n" 4559 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4560 4561 FormatStyle NoBinPacking = getLLVMStyle(); 4562 NoBinPacking.BinPackArguments = false; 4563 verifyFormat( 4564 "void f() {\n" 4565 " g(aaa,\n" 4566 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 4567 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4568 " ? aaaaaaaaaaaaaaa\n" 4569 " : aaaaaaaaaaaaaaa);\n" 4570 "}", 4571 NoBinPacking); 4572 verifyFormat( 4573 "void f() {\n" 4574 " g(aaa,\n" 4575 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 4576 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4577 " ?: aaaaaaaaaaaaaaa);\n" 4578 "}", 4579 NoBinPacking); 4580 4581 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n" 4582 " // comment.\n" 4583 " ccccccccccccccccccccccccccccccccccccccc\n" 4584 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4585 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);"); 4586 4587 // Assignments in conditional expressions. Apparently not uncommon :-(. 4588 verifyFormat("return a != b\n" 4589 " // comment\n" 4590 " ? a = b\n" 4591 " : a = b;"); 4592 verifyFormat("return a != b\n" 4593 " // comment\n" 4594 " ? a = a != b\n" 4595 " // comment\n" 4596 " ? a = b\n" 4597 " : a\n" 4598 " : a;\n"); 4599 verifyFormat("return a != b\n" 4600 " // comment\n" 4601 " ? a\n" 4602 " : a = a != b\n" 4603 " // comment\n" 4604 " ? a = b\n" 4605 " : a;"); 4606 } 4607 4608 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) { 4609 FormatStyle Style = getLLVMStyle(); 4610 Style.BreakBeforeTernaryOperators = false; 4611 Style.ColumnLimit = 70; 4612 verifyFormat( 4613 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4614 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4615 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4616 Style); 4617 verifyFormat( 4618 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 4619 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4620 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4621 Style); 4622 verifyFormat( 4623 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4624 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4625 Style); 4626 verifyFormat( 4627 "aaaa(aaaaaaaa, aaaaaaaaaa,\n" 4628 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4629 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4630 Style); 4631 verifyFormat( 4632 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n" 4633 " aaaaaaaaaaaaa);", 4634 Style); 4635 verifyFormat( 4636 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4637 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4638 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4639 " aaaaaaaaaaaaa);", 4640 Style); 4641 verifyFormat( 4642 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4643 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4644 " aaaaaaaaaaaaa);", 4645 Style); 4646 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4647 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4648 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 4649 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4650 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4651 Style); 4652 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4653 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4654 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4655 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 4656 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4657 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4658 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4659 Style); 4660 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4661 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n" 4662 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4663 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4664 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4665 Style); 4666 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4667 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4668 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4669 Style); 4670 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 4671 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4672 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4673 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4674 Style); 4675 verifyFormat( 4676 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4677 " aaaaaaaaaaaaaaa :\n" 4678 " aaaaaaaaaaaaaaa;", 4679 Style); 4680 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 4681 " aaaaaaaaa ?\n" 4682 " b :\n" 4683 " c);", 4684 Style); 4685 verifyFormat("unsigned Indent =\n" 4686 " format(TheLine.First,\n" 4687 " IndentForLevel[TheLine.Level] >= 0 ?\n" 4688 " IndentForLevel[TheLine.Level] :\n" 4689 " TheLine * 2,\n" 4690 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 4691 Style); 4692 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 4693 " aaaaaaaaaaaaaaa :\n" 4694 " bbbbbbbbbbbbbbb ? //\n" 4695 " ccccccccccccccc :\n" 4696 " ddddddddddddddd;", 4697 Style); 4698 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 4699 " aaaaaaaaaaaaaaa :\n" 4700 " (bbbbbbbbbbbbbbb ? //\n" 4701 " ccccccccccccccc :\n" 4702 " ddddddddddddddd);", 4703 Style); 4704 verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4705 " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n" 4706 " ccccccccccccccccccccccccccc;", 4707 Style); 4708 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4709 " aaaaa :\n" 4710 " bbbbbbbbbbbbbbb + cccccccccccccccc;", 4711 Style); 4712 } 4713 4714 TEST_F(FormatTest, DeclarationsOfMultipleVariables) { 4715 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" 4716 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); 4717 verifyFormat("bool a = true, b = false;"); 4718 4719 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" 4720 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" 4721 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" 4722 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); 4723 verifyFormat( 4724 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 4725 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n" 4726 " d = e && f;"); 4727 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n" 4728 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;"); 4729 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 4730 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;"); 4731 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n" 4732 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;"); 4733 4734 FormatStyle Style = getGoogleStyle(); 4735 Style.PointerAlignment = FormatStyle::PAS_Left; 4736 Style.DerivePointerAlignment = false; 4737 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4738 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" 4739 " *b = bbbbbbbbbbbbbbbbbbb;", 4740 Style); 4741 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 4742 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;", 4743 Style); 4744 verifyFormat("vector<int*> a, b;", Style); 4745 verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style); 4746 } 4747 4748 TEST_F(FormatTest, ConditionalExpressionsInBrackets) { 4749 verifyFormat("arr[foo ? bar : baz];"); 4750 verifyFormat("f()[foo ? bar : baz];"); 4751 verifyFormat("(a + b)[foo ? bar : baz];"); 4752 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); 4753 } 4754 4755 TEST_F(FormatTest, AlignsStringLiterals) { 4756 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" 4757 " \"short literal\");"); 4758 verifyFormat( 4759 "looooooooooooooooooooooooongFunction(\n" 4760 " \"short literal\"\n" 4761 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); 4762 verifyFormat("someFunction(\"Always break between multi-line\"\n" 4763 " \" string literals\",\n" 4764 " and, other, parameters);"); 4765 EXPECT_EQ("fun + \"1243\" /* comment */\n" 4766 " \"5678\";", 4767 format("fun + \"1243\" /* comment */\n" 4768 " \"5678\";", 4769 getLLVMStyleWithColumns(28))); 4770 EXPECT_EQ( 4771 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 4772 " \"aaaaaaaaaaaaaaaaaaaaa\"\n" 4773 " \"aaaaaaaaaaaaaaaa\";", 4774 format("aaaaaa =" 4775 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " 4776 "aaaaaaaaaaaaaaaaaaaaa\" " 4777 "\"aaaaaaaaaaaaaaaa\";")); 4778 verifyFormat("a = a + \"a\"\n" 4779 " \"a\"\n" 4780 " \"a\";"); 4781 verifyFormat("f(\"a\", \"b\"\n" 4782 " \"c\");"); 4783 4784 verifyFormat( 4785 "#define LL_FORMAT \"ll\"\n" 4786 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n" 4787 " \"d, ddddddddd: %\" LL_FORMAT \"d\");"); 4788 4789 verifyFormat("#define A(X) \\\n" 4790 " \"aaaaa\" #X \"bbbbbb\" \\\n" 4791 " \"ccccc\"", 4792 getLLVMStyleWithColumns(23)); 4793 verifyFormat("#define A \"def\"\n" 4794 "f(\"abc\" A \"ghi\"\n" 4795 " \"jkl\");"); 4796 4797 verifyFormat("f(L\"a\"\n" 4798 " L\"b\");"); 4799 verifyFormat("#define A(X) \\\n" 4800 " L\"aaaaa\" #X L\"bbbbbb\" \\\n" 4801 " L\"ccccc\"", 4802 getLLVMStyleWithColumns(25)); 4803 4804 verifyFormat("f(@\"a\"\n" 4805 " @\"b\");"); 4806 verifyFormat("NSString s = @\"a\"\n" 4807 " @\"b\"\n" 4808 " @\"c\";"); 4809 verifyFormat("NSString s = @\"a\"\n" 4810 " \"b\"\n" 4811 " \"c\";"); 4812 } 4813 4814 TEST_F(FormatTest, ReturnTypeBreakingStyle) { 4815 FormatStyle Style = getLLVMStyle(); 4816 // No declarations or definitions should be moved to own line. 4817 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None; 4818 verifyFormat("class A {\n" 4819 " int f() { return 1; }\n" 4820 " int g();\n" 4821 "};\n" 4822 "int f() { return 1; }\n" 4823 "int g();\n", 4824 Style); 4825 4826 // All declarations and definitions should have the return type moved to its 4827 // own 4828 // line. 4829 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 4830 verifyFormat("class E {\n" 4831 " int\n" 4832 " f() {\n" 4833 " return 1;\n" 4834 " }\n" 4835 " int\n" 4836 " g();\n" 4837 "};\n" 4838 "int\n" 4839 "f() {\n" 4840 " return 1;\n" 4841 "}\n" 4842 "int\n" 4843 "g();\n", 4844 Style); 4845 4846 // Top-level definitions, and no kinds of declarations should have the 4847 // return type moved to its own line. 4848 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions; 4849 verifyFormat("class B {\n" 4850 " int f() { return 1; }\n" 4851 " int g();\n" 4852 "};\n" 4853 "int\n" 4854 "f() {\n" 4855 " return 1;\n" 4856 "}\n" 4857 "int g();\n", 4858 Style); 4859 4860 // Top-level definitions and declarations should have the return type moved 4861 // to its own line. 4862 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel; 4863 verifyFormat("class C {\n" 4864 " int f() { return 1; }\n" 4865 " int g();\n" 4866 "};\n" 4867 "int\n" 4868 "f() {\n" 4869 " return 1;\n" 4870 "}\n" 4871 "int\n" 4872 "g();\n", 4873 Style); 4874 4875 // All definitions should have the return type moved to its own line, but no 4876 // kinds of declarations. 4877 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 4878 verifyFormat("class D {\n" 4879 " int\n" 4880 " f() {\n" 4881 " return 1;\n" 4882 " }\n" 4883 " int g();\n" 4884 "};\n" 4885 "int\n" 4886 "f() {\n" 4887 " return 1;\n" 4888 "}\n" 4889 "int g();\n", 4890 Style); 4891 verifyFormat("const char *\n" 4892 "f(void) {\n" // Break here. 4893 " return \"\";\n" 4894 "}\n" 4895 "const char *bar(void);\n", // No break here. 4896 Style); 4897 verifyFormat("template <class T>\n" 4898 "T *\n" 4899 "f(T &c) {\n" // Break here. 4900 " return NULL;\n" 4901 "}\n" 4902 "template <class T> T *f(T &c);\n", // No break here. 4903 Style); 4904 verifyFormat("class C {\n" 4905 " int\n" 4906 " operator+() {\n" 4907 " return 1;\n" 4908 " }\n" 4909 " int\n" 4910 " operator()() {\n" 4911 " return 1;\n" 4912 " }\n" 4913 "};\n", 4914 Style); 4915 verifyFormat("void\n" 4916 "A::operator()() {}\n" 4917 "void\n" 4918 "A::operator>>() {}\n" 4919 "void\n" 4920 "A::operator+() {}\n", 4921 Style); 4922 verifyFormat("void *operator new(std::size_t s);", // No break here. 4923 Style); 4924 verifyFormat("void *\n" 4925 "operator new(std::size_t s) {}", 4926 Style); 4927 verifyFormat("void *\n" 4928 "operator delete[](void *ptr) {}", 4929 Style); 4930 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 4931 verifyFormat("const char *\n" 4932 "f(void)\n" // Break here. 4933 "{\n" 4934 " return \"\";\n" 4935 "}\n" 4936 "const char *bar(void);\n", // No break here. 4937 Style); 4938 verifyFormat("template <class T>\n" 4939 "T *\n" // Problem here: no line break 4940 "f(T &c)\n" // Break here. 4941 "{\n" 4942 " return NULL;\n" 4943 "}\n" 4944 "template <class T> T *f(T &c);\n", // No break here. 4945 Style); 4946 } 4947 4948 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) { 4949 FormatStyle NoBreak = getLLVMStyle(); 4950 NoBreak.AlwaysBreakBeforeMultilineStrings = false; 4951 FormatStyle Break = getLLVMStyle(); 4952 Break.AlwaysBreakBeforeMultilineStrings = true; 4953 verifyFormat("aaaa = \"bbbb\"\n" 4954 " \"cccc\";", 4955 NoBreak); 4956 verifyFormat("aaaa =\n" 4957 " \"bbbb\"\n" 4958 " \"cccc\";", 4959 Break); 4960 verifyFormat("aaaa(\"bbbb\"\n" 4961 " \"cccc\");", 4962 NoBreak); 4963 verifyFormat("aaaa(\n" 4964 " \"bbbb\"\n" 4965 " \"cccc\");", 4966 Break); 4967 verifyFormat("aaaa(qqq, \"bbbb\"\n" 4968 " \"cccc\");", 4969 NoBreak); 4970 verifyFormat("aaaa(qqq,\n" 4971 " \"bbbb\"\n" 4972 " \"cccc\");", 4973 Break); 4974 verifyFormat("aaaa(qqq,\n" 4975 " L\"bbbb\"\n" 4976 " L\"cccc\");", 4977 Break); 4978 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n" 4979 " \"bbbb\"));", 4980 Break); 4981 verifyFormat("string s = someFunction(\n" 4982 " \"abc\"\n" 4983 " \"abc\");", 4984 Break); 4985 4986 // As we break before unary operators, breaking right after them is bad. 4987 verifyFormat("string foo = abc ? \"x\"\n" 4988 " \"blah blah blah blah blah blah\"\n" 4989 " : \"y\";", 4990 Break); 4991 4992 // Don't break if there is no column gain. 4993 verifyFormat("f(\"aaaa\"\n" 4994 " \"bbbb\");", 4995 Break); 4996 4997 // Treat literals with escaped newlines like multi-line string literals. 4998 EXPECT_EQ("x = \"a\\\n" 4999 "b\\\n" 5000 "c\";", 5001 format("x = \"a\\\n" 5002 "b\\\n" 5003 "c\";", 5004 NoBreak)); 5005 EXPECT_EQ("xxxx =\n" 5006 " \"a\\\n" 5007 "b\\\n" 5008 "c\";", 5009 format("xxxx = \"a\\\n" 5010 "b\\\n" 5011 "c\";", 5012 Break)); 5013 5014 EXPECT_EQ("NSString *const kString =\n" 5015 " @\"aaaa\"\n" 5016 " @\"bbbb\";", 5017 format("NSString *const kString = @\"aaaa\"\n" 5018 "@\"bbbb\";", 5019 Break)); 5020 5021 Break.ColumnLimit = 0; 5022 verifyFormat("const char *hello = \"hello llvm\";", Break); 5023 } 5024 5025 TEST_F(FormatTest, AlignsPipes) { 5026 verifyFormat( 5027 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5028 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5029 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5030 verifyFormat( 5031 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 5032 " << aaaaaaaaaaaaaaaaaaaa;"); 5033 verifyFormat( 5034 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5035 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5036 verifyFormat( 5037 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 5038 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5039 verifyFormat( 5040 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 5041 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 5042 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 5043 verifyFormat( 5044 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5045 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5046 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5047 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5048 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5049 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5050 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 5051 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n" 5052 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);"); 5053 verifyFormat( 5054 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5055 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5056 verifyFormat( 5057 "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n" 5058 " aaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5059 5060 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n" 5061 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();"); 5062 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5063 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5064 " aaaaaaaaaaaaaaaaaaaaa)\n" 5065 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5066 verifyFormat("LOG_IF(aaa == //\n" 5067 " bbb)\n" 5068 " << a << b;"); 5069 5070 // But sometimes, breaking before the first "<<" is desirable. 5071 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5072 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);"); 5073 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n" 5074 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5075 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5076 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n" 5077 " << BEF << IsTemplate << Description << E->getType();"); 5078 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5079 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5080 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5081 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5082 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5083 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5084 " << aaa;"); 5085 5086 verifyFormat( 5087 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5088 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5089 5090 // Incomplete string literal. 5091 EXPECT_EQ("llvm::errs() << \"\n" 5092 " << a;", 5093 format("llvm::errs() << \"\n<<a;")); 5094 5095 verifyFormat("void f() {\n" 5096 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n" 5097 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n" 5098 "}"); 5099 5100 // Handle 'endl'. 5101 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n" 5102 " << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 5103 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 5104 5105 // Handle '\n'. 5106 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n" 5107 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 5108 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n" 5109 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';"); 5110 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n" 5111 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";"); 5112 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 5113 } 5114 5115 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) { 5116 verifyFormat("return out << \"somepacket = {\\n\"\n" 5117 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" 5118 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" 5119 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" 5120 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" 5121 " << \"}\";"); 5122 5123 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 5124 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 5125 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;"); 5126 verifyFormat( 5127 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" 5128 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" 5129 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" 5130 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" 5131 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); 5132 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" 5133 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 5134 verifyFormat( 5135 "void f() {\n" 5136 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n" 5137 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 5138 "}"); 5139 5140 // Breaking before the first "<<" is generally not desirable. 5141 verifyFormat( 5142 "llvm::errs()\n" 5143 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5144 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5145 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5146 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5147 getLLVMStyleWithColumns(70)); 5148 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5149 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5150 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5151 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5152 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5153 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5154 getLLVMStyleWithColumns(70)); 5155 5156 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 5157 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 5158 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;"); 5159 verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 5160 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 5161 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);"); 5162 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n" 5163 " (aaaa + aaaa);", 5164 getLLVMStyleWithColumns(40)); 5165 verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n" 5166 " (aaaaaaa + aaaaa));", 5167 getLLVMStyleWithColumns(40)); 5168 verifyFormat( 5169 "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n" 5170 " SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n" 5171 " bbbbbbbbbbbbbbbbbbbbbbb);"); 5172 } 5173 5174 TEST_F(FormatTest, UnderstandsEquals) { 5175 verifyFormat( 5176 "aaaaaaaaaaaaaaaaa =\n" 5177 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5178 verifyFormat( 5179 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5180 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 5181 verifyFormat( 5182 "if (a) {\n" 5183 " f();\n" 5184 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5185 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 5186 "}"); 5187 5188 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5189 " 100000000 + 10000000) {\n}"); 5190 } 5191 5192 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 5193 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 5194 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 5195 5196 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 5197 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 5198 5199 verifyFormat( 5200 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 5201 " Parameter2);"); 5202 5203 verifyFormat( 5204 "ShortObject->shortFunction(\n" 5205 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 5206 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 5207 5208 verifyFormat("loooooooooooooongFunction(\n" 5209 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 5210 5211 verifyFormat( 5212 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 5213 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 5214 5215 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 5216 " .WillRepeatedly(Return(SomeValue));"); 5217 verifyFormat("void f() {\n" 5218 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 5219 " .Times(2)\n" 5220 " .WillRepeatedly(Return(SomeValue));\n" 5221 "}"); 5222 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n" 5223 " ccccccccccccccccccccccc);"); 5224 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5225 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5226 " .aaaaa(aaaaa),\n" 5227 " aaaaaaaaaaaaaaaaaaaaa);"); 5228 verifyFormat("void f() {\n" 5229 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5230 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n" 5231 "}"); 5232 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5233 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5234 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5235 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5236 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 5237 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5238 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5239 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5240 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n" 5241 "}"); 5242 5243 // Here, it is not necessary to wrap at "." or "->". 5244 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 5245 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 5246 verifyFormat( 5247 "aaaaaaaaaaa->aaaaaaaaa(\n" 5248 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5249 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 5250 5251 verifyFormat( 5252 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5253 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); 5254 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n" 5255 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 5256 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" 5257 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 5258 5259 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5260 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5261 " .a();"); 5262 5263 FormatStyle NoBinPacking = getLLVMStyle(); 5264 NoBinPacking.BinPackParameters = false; 5265 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 5266 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 5267 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" 5268 " aaaaaaaaaaaaaaaaaaa,\n" 5269 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5270 NoBinPacking); 5271 5272 // If there is a subsequent call, change to hanging indentation. 5273 verifyFormat( 5274 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5275 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n" 5276 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5277 verifyFormat( 5278 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5279 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));"); 5280 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5281 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5282 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5283 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5284 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5285 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 5286 } 5287 5288 TEST_F(FormatTest, WrapsTemplateDeclarations) { 5289 verifyFormat("template <typename T>\n" 5290 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 5291 verifyFormat("template <typename T>\n" 5292 "// T should be one of {A, B}.\n" 5293 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 5294 verifyFormat( 5295 "template <typename T>\n" 5296 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); 5297 verifyFormat("template <typename T>\n" 5298 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 5299 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 5300 verifyFormat( 5301 "template <typename T>\n" 5302 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 5303 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 5304 verifyFormat( 5305 "template <typename T>\n" 5306 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 5307 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 5308 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5309 verifyFormat("template <typename T>\n" 5310 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5311 " int aaaaaaaaaaaaaaaaaaaaaa);"); 5312 verifyFormat( 5313 "template <typename T1, typename T2 = char, typename T3 = char,\n" 5314 " typename T4 = char>\n" 5315 "void f();"); 5316 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n" 5317 " template <typename> class cccccccccccccccccccccc,\n" 5318 " typename ddddddddddddd>\n" 5319 "class C {};"); 5320 verifyFormat( 5321 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 5322 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5323 5324 verifyFormat("void f() {\n" 5325 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 5326 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n" 5327 "}"); 5328 5329 verifyFormat("template <typename T> class C {};"); 5330 verifyFormat("template <typename T> void f();"); 5331 verifyFormat("template <typename T> void f() {}"); 5332 verifyFormat( 5333 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 5334 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5335 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n" 5336 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 5337 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5338 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n" 5339 " bbbbbbbbbbbbbbbbbbbbbbbb);", 5340 getLLVMStyleWithColumns(72)); 5341 EXPECT_EQ("static_cast<A< //\n" 5342 " B> *>(\n" 5343 "\n" 5344 ");", 5345 format("static_cast<A<//\n" 5346 " B>*>(\n" 5347 "\n" 5348 " );")); 5349 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5350 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);"); 5351 5352 FormatStyle AlwaysBreak = getLLVMStyle(); 5353 AlwaysBreak.AlwaysBreakTemplateDeclarations = true; 5354 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak); 5355 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak); 5356 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak); 5357 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5358 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 5359 " ccccccccccccccccccccccccccccccccccccccccccccccc);"); 5360 verifyFormat("template <template <typename> class Fooooooo,\n" 5361 " template <typename> class Baaaaaaar>\n" 5362 "struct C {};", 5363 AlwaysBreak); 5364 verifyFormat("template <typename T> // T can be A, B or C.\n" 5365 "struct C {};", 5366 AlwaysBreak); 5367 verifyFormat("template <enum E> class A {\n" 5368 "public:\n" 5369 " E *f();\n" 5370 "};"); 5371 } 5372 5373 TEST_F(FormatTest, WrapsTemplateParameters) { 5374 FormatStyle Style = getLLVMStyle(); 5375 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 5376 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 5377 verifyFormat( 5378 "template <typename... a> struct q {};\n" 5379 "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 5380 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 5381 " y;", 5382 Style); 5383 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 5384 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 5385 verifyFormat( 5386 "template <typename... a> struct r {};\n" 5387 "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 5388 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 5389 " y;", 5390 Style); 5391 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 5392 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 5393 verifyFormat( 5394 "template <typename... a> struct s {};\n" 5395 "extern s<\n" 5396 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 5397 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n" 5398 " y;", 5399 Style); 5400 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 5401 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 5402 verifyFormat( 5403 "template <typename... a> struct t {};\n" 5404 "extern t<\n" 5405 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 5406 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n" 5407 " y;", 5408 Style); 5409 } 5410 5411 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 5412 verifyFormat( 5413 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5414 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5415 verifyFormat( 5416 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5417 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5418 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 5419 5420 // FIXME: Should we have the extra indent after the second break? 5421 verifyFormat( 5422 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5423 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5424 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5425 5426 verifyFormat( 5427 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 5428 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 5429 5430 // Breaking at nested name specifiers is generally not desirable. 5431 verifyFormat( 5432 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5433 " aaaaaaaaaaaaaaaaaaaaaaa);"); 5434 5435 verifyFormat( 5436 "aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n" 5437 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5438 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5439 " aaaaaaaaaaaaaaaaaaaaa);", 5440 getLLVMStyleWithColumns(74)); 5441 5442 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5443 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5444 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5445 } 5446 5447 TEST_F(FormatTest, UnderstandsTemplateParameters) { 5448 verifyFormat("A<int> a;"); 5449 verifyFormat("A<A<A<int>>> a;"); 5450 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 5451 verifyFormat("bool x = a < 1 || 2 > a;"); 5452 verifyFormat("bool x = 5 < f<int>();"); 5453 verifyFormat("bool x = f<int>() > 5;"); 5454 verifyFormat("bool x = 5 < a<int>::x;"); 5455 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 5456 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 5457 5458 verifyGoogleFormat("A<A<int>> a;"); 5459 verifyGoogleFormat("A<A<A<int>>> a;"); 5460 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 5461 verifyGoogleFormat("A<A<int> > a;"); 5462 verifyGoogleFormat("A<A<A<int> > > a;"); 5463 verifyGoogleFormat("A<A<A<A<int> > > > a;"); 5464 verifyGoogleFormat("A<::A<int>> a;"); 5465 verifyGoogleFormat("A<::A> a;"); 5466 verifyGoogleFormat("A< ::A> a;"); 5467 verifyGoogleFormat("A< ::A<int> > a;"); 5468 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); 5469 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); 5470 EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle())); 5471 EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle())); 5472 EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };", 5473 format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle())); 5474 5475 verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp)); 5476 5477 verifyFormat("test >> a >> b;"); 5478 verifyFormat("test << a >> b;"); 5479 5480 verifyFormat("f<int>();"); 5481 verifyFormat("template <typename T> void f() {}"); 5482 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;"); 5483 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : " 5484 "sizeof(char)>::type>;"); 5485 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};"); 5486 verifyFormat("f(a.operator()<A>());"); 5487 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5488 " .template operator()<A>());", 5489 getLLVMStyleWithColumns(35)); 5490 5491 // Not template parameters. 5492 verifyFormat("return a < b && c > d;"); 5493 verifyFormat("void f() {\n" 5494 " while (a < b && c > d) {\n" 5495 " }\n" 5496 "}"); 5497 verifyFormat("template <typename... Types>\n" 5498 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}"); 5499 5500 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5501 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);", 5502 getLLVMStyleWithColumns(60)); 5503 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");"); 5504 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}"); 5505 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <"); 5506 } 5507 5508 TEST_F(FormatTest, BitshiftOperatorWidth) { 5509 EXPECT_EQ("int a = 1 << 2; /* foo\n" 5510 " bar */", 5511 format("int a=1<<2; /* foo\n" 5512 " bar */")); 5513 5514 EXPECT_EQ("int b = 256 >> 1; /* foo\n" 5515 " bar */", 5516 format("int b =256>>1 ; /* foo\n" 5517 " bar */")); 5518 } 5519 5520 TEST_F(FormatTest, UnderstandsBinaryOperators) { 5521 verifyFormat("COMPARE(a, ==, b);"); 5522 verifyFormat("auto s = sizeof...(Ts) - 1;"); 5523 } 5524 5525 TEST_F(FormatTest, UnderstandsPointersToMembers) { 5526 verifyFormat("int A::*x;"); 5527 verifyFormat("int (S::*func)(void *);"); 5528 verifyFormat("void f() { int (S::*func)(void *); }"); 5529 verifyFormat("typedef bool *(Class::*Member)() const;"); 5530 verifyFormat("void f() {\n" 5531 " (a->*f)();\n" 5532 " a->*x;\n" 5533 " (a.*f)();\n" 5534 " ((*a).*f)();\n" 5535 " a.*x;\n" 5536 "}"); 5537 verifyFormat("void f() {\n" 5538 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 5539 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n" 5540 "}"); 5541 verifyFormat( 5542 "(aaaaaaaaaa->*bbbbbbb)(\n" 5543 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 5544 FormatStyle Style = getLLVMStyle(); 5545 Style.PointerAlignment = FormatStyle::PAS_Left; 5546 verifyFormat("typedef bool* (Class::*Member)() const;", Style); 5547 } 5548 5549 TEST_F(FormatTest, UnderstandsUnaryOperators) { 5550 verifyFormat("int a = -2;"); 5551 verifyFormat("f(-1, -2, -3);"); 5552 verifyFormat("a[-1] = 5;"); 5553 verifyFormat("int a = 5 + -2;"); 5554 verifyFormat("if (i == -1) {\n}"); 5555 verifyFormat("if (i != -1) {\n}"); 5556 verifyFormat("if (i > -1) {\n}"); 5557 verifyFormat("if (i < -1) {\n}"); 5558 verifyFormat("++(a->f());"); 5559 verifyFormat("--(a->f());"); 5560 verifyFormat("(a->f())++;"); 5561 verifyFormat("a[42]++;"); 5562 verifyFormat("if (!(a->f())) {\n}"); 5563 5564 verifyFormat("a-- > b;"); 5565 verifyFormat("b ? -a : c;"); 5566 verifyFormat("n * sizeof char16;"); 5567 verifyFormat("n * alignof char16;", getGoogleStyle()); 5568 verifyFormat("sizeof(char);"); 5569 verifyFormat("alignof(char);", getGoogleStyle()); 5570 5571 verifyFormat("return -1;"); 5572 verifyFormat("switch (a) {\n" 5573 "case -1:\n" 5574 " break;\n" 5575 "}"); 5576 verifyFormat("#define X -1"); 5577 verifyFormat("#define X -kConstant"); 5578 5579 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};"); 5580 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};"); 5581 5582 verifyFormat("int a = /* confusing comment */ -1;"); 5583 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 5584 verifyFormat("int a = i /* confusing comment */++;"); 5585 } 5586 5587 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) { 5588 verifyFormat("if (!aaaaaaaaaa( // break\n" 5589 " aaaaa)) {\n" 5590 "}"); 5591 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n" 5592 " aaaaa));"); 5593 verifyFormat("*aaa = aaaaaaa( // break\n" 5594 " bbbbbb);"); 5595 } 5596 5597 TEST_F(FormatTest, UnderstandsOverloadedOperators) { 5598 verifyFormat("bool operator<();"); 5599 verifyFormat("bool operator>();"); 5600 verifyFormat("bool operator=();"); 5601 verifyFormat("bool operator==();"); 5602 verifyFormat("bool operator!=();"); 5603 verifyFormat("int operator+();"); 5604 verifyFormat("int operator++();"); 5605 verifyFormat("int operator++(int) volatile noexcept;"); 5606 verifyFormat("bool operator,();"); 5607 verifyFormat("bool operator();"); 5608 verifyFormat("bool operator()();"); 5609 verifyFormat("bool operator[]();"); 5610 verifyFormat("operator bool();"); 5611 verifyFormat("operator int();"); 5612 verifyFormat("operator void *();"); 5613 verifyFormat("operator SomeType<int>();"); 5614 verifyFormat("operator SomeType<int, int>();"); 5615 verifyFormat("operator SomeType<SomeType<int>>();"); 5616 verifyFormat("void *operator new(std::size_t size);"); 5617 verifyFormat("void *operator new[](std::size_t size);"); 5618 verifyFormat("void operator delete(void *ptr);"); 5619 verifyFormat("void operator delete[](void *ptr);"); 5620 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n" 5621 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);"); 5622 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n" 5623 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;"); 5624 5625 verifyFormat( 5626 "ostream &operator<<(ostream &OutputStream,\n" 5627 " SomeReallyLongType WithSomeReallyLongValue);"); 5628 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n" 5629 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n" 5630 " return left.group < right.group;\n" 5631 "}"); 5632 verifyFormat("SomeType &operator=(const SomeType &S);"); 5633 verifyFormat("f.template operator()<int>();"); 5634 5635 verifyGoogleFormat("operator void*();"); 5636 verifyGoogleFormat("operator SomeType<SomeType<int>>();"); 5637 verifyGoogleFormat("operator ::A();"); 5638 5639 verifyFormat("using A::operator+;"); 5640 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n" 5641 "int i;"); 5642 } 5643 5644 TEST_F(FormatTest, UnderstandsFunctionRefQualification) { 5645 verifyFormat("Deleted &operator=(const Deleted &) & = default;"); 5646 verifyFormat("Deleted &operator=(const Deleted &) && = delete;"); 5647 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;"); 5648 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;"); 5649 verifyFormat("Deleted &operator=(const Deleted &) &;"); 5650 verifyFormat("Deleted &operator=(const Deleted &) &&;"); 5651 verifyFormat("SomeType MemberFunction(const Deleted &) &;"); 5652 verifyFormat("SomeType MemberFunction(const Deleted &) &&;"); 5653 verifyFormat("SomeType MemberFunction(const Deleted &) && {}"); 5654 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}"); 5655 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}"); 5656 verifyFormat("void Fn(T const &) const &;"); 5657 verifyFormat("void Fn(T const volatile &&) const volatile &&;"); 5658 verifyFormat("template <typename T>\n" 5659 "void F(T) && = delete;", 5660 getGoogleStyle()); 5661 5662 FormatStyle AlignLeft = getLLVMStyle(); 5663 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 5664 verifyFormat("void A::b() && {}", AlignLeft); 5665 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft); 5666 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;", 5667 AlignLeft); 5668 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft); 5669 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft); 5670 verifyFormat("auto Function(T t) & -> void {}", AlignLeft); 5671 verifyFormat("auto Function(T... t) & -> void {}", AlignLeft); 5672 verifyFormat("auto Function(T) & -> void {}", AlignLeft); 5673 verifyFormat("auto Function(T) & -> void;", AlignLeft); 5674 verifyFormat("void Fn(T const&) const&;", AlignLeft); 5675 verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft); 5676 5677 FormatStyle Spaces = getLLVMStyle(); 5678 Spaces.SpacesInCStyleCastParentheses = true; 5679 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces); 5680 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces); 5681 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces); 5682 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces); 5683 5684 Spaces.SpacesInCStyleCastParentheses = false; 5685 Spaces.SpacesInParentheses = true; 5686 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces); 5687 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces); 5688 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces); 5689 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces); 5690 } 5691 5692 TEST_F(FormatTest, UnderstandsNewAndDelete) { 5693 verifyFormat("void f() {\n" 5694 " A *a = new A;\n" 5695 " A *a = new (placement) A;\n" 5696 " delete a;\n" 5697 " delete (A *)a;\n" 5698 "}"); 5699 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 5700 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 5701 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5702 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 5703 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 5704 verifyFormat("delete[] h->p;"); 5705 } 5706 5707 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 5708 verifyFormat("int *f(int *a) {}"); 5709 verifyFormat("int main(int argc, char **argv) {}"); 5710 verifyFormat("Test::Test(int b) : a(b * b) {}"); 5711 verifyIndependentOfContext("f(a, *a);"); 5712 verifyFormat("void g() { f(*a); }"); 5713 verifyIndependentOfContext("int a = b * 10;"); 5714 verifyIndependentOfContext("int a = 10 * b;"); 5715 verifyIndependentOfContext("int a = b * c;"); 5716 verifyIndependentOfContext("int a += b * c;"); 5717 verifyIndependentOfContext("int a -= b * c;"); 5718 verifyIndependentOfContext("int a *= b * c;"); 5719 verifyIndependentOfContext("int a /= b * c;"); 5720 verifyIndependentOfContext("int a = *b;"); 5721 verifyIndependentOfContext("int a = *b * c;"); 5722 verifyIndependentOfContext("int a = b * *c;"); 5723 verifyIndependentOfContext("int a = b * (10);"); 5724 verifyIndependentOfContext("S << b * (10);"); 5725 verifyIndependentOfContext("return 10 * b;"); 5726 verifyIndependentOfContext("return *b * *c;"); 5727 verifyIndependentOfContext("return a & ~b;"); 5728 verifyIndependentOfContext("f(b ? *c : *d);"); 5729 verifyIndependentOfContext("int a = b ? *c : *d;"); 5730 verifyIndependentOfContext("*b = a;"); 5731 verifyIndependentOfContext("a * ~b;"); 5732 verifyIndependentOfContext("a * !b;"); 5733 verifyIndependentOfContext("a * +b;"); 5734 verifyIndependentOfContext("a * -b;"); 5735 verifyIndependentOfContext("a * ++b;"); 5736 verifyIndependentOfContext("a * --b;"); 5737 verifyIndependentOfContext("a[4] * b;"); 5738 verifyIndependentOfContext("a[a * a] = 1;"); 5739 verifyIndependentOfContext("f() * b;"); 5740 verifyIndependentOfContext("a * [self dostuff];"); 5741 verifyIndependentOfContext("int x = a * (a + b);"); 5742 verifyIndependentOfContext("(a *)(a + b);"); 5743 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;"); 5744 verifyIndependentOfContext("int *pa = (int *)&a;"); 5745 verifyIndependentOfContext("return sizeof(int **);"); 5746 verifyIndependentOfContext("return sizeof(int ******);"); 5747 verifyIndependentOfContext("return (int **&)a;"); 5748 verifyIndependentOfContext("f((*PointerToArray)[10]);"); 5749 verifyFormat("void f(Type (*parameter)[10]) {}"); 5750 verifyFormat("void f(Type (¶meter)[10]) {}"); 5751 verifyGoogleFormat("return sizeof(int**);"); 5752 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); 5753 verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); 5754 verifyFormat("auto a = [](int **&, int ***) {};"); 5755 verifyFormat("auto PointerBinding = [](const char *S) {};"); 5756 verifyFormat("typedef typeof(int(int, int)) *MyFunc;"); 5757 verifyFormat("[](const decltype(*a) &value) {}"); 5758 verifyFormat("decltype(a * b) F();"); 5759 verifyFormat("#define MACRO() [](A *a) { return 1; }"); 5760 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}"); 5761 verifyIndependentOfContext("typedef void (*f)(int *a);"); 5762 verifyIndependentOfContext("int i{a * b};"); 5763 verifyIndependentOfContext("aaa && aaa->f();"); 5764 verifyIndependentOfContext("int x = ~*p;"); 5765 verifyFormat("Constructor() : a(a), area(width * height) {}"); 5766 verifyFormat("Constructor() : a(a), area(a, width * height) {}"); 5767 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}"); 5768 verifyFormat("void f() { f(a, c * d); }"); 5769 verifyFormat("void f() { f(new a(), c * d); }"); 5770 verifyFormat("void f(const MyOverride &override);"); 5771 verifyFormat("void f(const MyFinal &final);"); 5772 verifyIndependentOfContext("bool a = f() && override.f();"); 5773 verifyIndependentOfContext("bool a = f() && final.f();"); 5774 5775 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 5776 5777 verifyIndependentOfContext("A<int *> a;"); 5778 verifyIndependentOfContext("A<int **> a;"); 5779 verifyIndependentOfContext("A<int *, int *> a;"); 5780 verifyIndependentOfContext("A<int *[]> a;"); 5781 verifyIndependentOfContext( 5782 "const char *const p = reinterpret_cast<const char *const>(q);"); 5783 verifyIndependentOfContext("A<int **, int **> a;"); 5784 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); 5785 verifyFormat("for (char **a = b; *a; ++a) {\n}"); 5786 verifyFormat("for (; a && b;) {\n}"); 5787 verifyFormat("bool foo = true && [] { return false; }();"); 5788 5789 verifyFormat( 5790 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5791 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5792 5793 verifyGoogleFormat("int const* a = &b;"); 5794 verifyGoogleFormat("**outparam = 1;"); 5795 verifyGoogleFormat("*outparam = a * b;"); 5796 verifyGoogleFormat("int main(int argc, char** argv) {}"); 5797 verifyGoogleFormat("A<int*> a;"); 5798 verifyGoogleFormat("A<int**> a;"); 5799 verifyGoogleFormat("A<int*, int*> a;"); 5800 verifyGoogleFormat("A<int**, int**> a;"); 5801 verifyGoogleFormat("f(b ? *c : *d);"); 5802 verifyGoogleFormat("int a = b ? *c : *d;"); 5803 verifyGoogleFormat("Type* t = **x;"); 5804 verifyGoogleFormat("Type* t = *++*x;"); 5805 verifyGoogleFormat("*++*x;"); 5806 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 5807 verifyGoogleFormat("Type* t = x++ * y;"); 5808 verifyGoogleFormat( 5809 "const char* const p = reinterpret_cast<const char* const>(q);"); 5810 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);"); 5811 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);"); 5812 verifyGoogleFormat("template <typename T>\n" 5813 "void f(int i = 0, SomeType** temps = NULL);"); 5814 5815 FormatStyle Left = getLLVMStyle(); 5816 Left.PointerAlignment = FormatStyle::PAS_Left; 5817 verifyFormat("x = *a(x) = *a(y);", Left); 5818 verifyFormat("for (;; *a = b) {\n}", Left); 5819 verifyFormat("return *this += 1;", Left); 5820 verifyFormat("throw *x;", Left); 5821 verifyFormat("delete *x;", Left); 5822 verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left); 5823 verifyFormat("[](const decltype(*a)* ptr) {}", Left); 5824 verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left); 5825 5826 verifyIndependentOfContext("a = *(x + y);"); 5827 verifyIndependentOfContext("a = &(x + y);"); 5828 verifyIndependentOfContext("*(x + y).call();"); 5829 verifyIndependentOfContext("&(x + y)->call();"); 5830 verifyFormat("void f() { &(*I).first; }"); 5831 5832 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 5833 verifyFormat( 5834 "int *MyValues = {\n" 5835 " *A, // Operator detection might be confused by the '{'\n" 5836 " *BB // Operator detection might be confused by previous comment\n" 5837 "};"); 5838 5839 verifyIndependentOfContext("if (int *a = &b)"); 5840 verifyIndependentOfContext("if (int &a = *b)"); 5841 verifyIndependentOfContext("if (a & b[i])"); 5842 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 5843 verifyIndependentOfContext("if (*b[i])"); 5844 verifyIndependentOfContext("if (int *a = (&b))"); 5845 verifyIndependentOfContext("while (int *a = &b)"); 5846 verifyIndependentOfContext("size = sizeof *a;"); 5847 verifyIndependentOfContext("if (a && (b = c))"); 5848 verifyFormat("void f() {\n" 5849 " for (const int &v : Values) {\n" 5850 " }\n" 5851 "}"); 5852 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); 5853 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); 5854 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}"); 5855 5856 verifyFormat("#define A (!a * b)"); 5857 verifyFormat("#define MACRO \\\n" 5858 " int *i = a * b; \\\n" 5859 " void f(a *b);", 5860 getLLVMStyleWithColumns(19)); 5861 5862 verifyIndependentOfContext("A = new SomeType *[Length];"); 5863 verifyIndependentOfContext("A = new SomeType *[Length]();"); 5864 verifyIndependentOfContext("T **t = new T *;"); 5865 verifyIndependentOfContext("T **t = new T *();"); 5866 verifyGoogleFormat("A = new SomeType*[Length]();"); 5867 verifyGoogleFormat("A = new SomeType*[Length];"); 5868 verifyGoogleFormat("T** t = new T*;"); 5869 verifyGoogleFormat("T** t = new T*();"); 5870 5871 verifyFormat("STATIC_ASSERT((a & b) == 0);"); 5872 verifyFormat("STATIC_ASSERT(0 == (a & b));"); 5873 verifyFormat("template <bool a, bool b> " 5874 "typename t::if<x && y>::type f() {}"); 5875 verifyFormat("template <int *y> f() {}"); 5876 verifyFormat("vector<int *> v;"); 5877 verifyFormat("vector<int *const> v;"); 5878 verifyFormat("vector<int *const **const *> v;"); 5879 verifyFormat("vector<int *volatile> v;"); 5880 verifyFormat("vector<a * b> v;"); 5881 verifyFormat("foo<b && false>();"); 5882 verifyFormat("foo<b & 1>();"); 5883 verifyFormat("decltype(*::std::declval<const T &>()) void F();"); 5884 verifyFormat( 5885 "template <class T, class = typename std::enable_if<\n" 5886 " std::is_integral<T>::value &&\n" 5887 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n" 5888 "void F();", 5889 getLLVMStyleWithColumns(70)); 5890 verifyFormat( 5891 "template <class T,\n" 5892 " class = typename std::enable_if<\n" 5893 " std::is_integral<T>::value &&\n" 5894 " (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n" 5895 " class U>\n" 5896 "void F();", 5897 getLLVMStyleWithColumns(70)); 5898 verifyFormat( 5899 "template <class T,\n" 5900 " class = typename ::std::enable_if<\n" 5901 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n" 5902 "void F();", 5903 getGoogleStyleWithColumns(68)); 5904 5905 verifyIndependentOfContext("MACRO(int *i);"); 5906 verifyIndependentOfContext("MACRO(auto *a);"); 5907 verifyIndependentOfContext("MACRO(const A *a);"); 5908 verifyIndependentOfContext("MACRO(A *const a);"); 5909 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');"); 5910 verifyFormat("void f() { f(float{1}, a * a); }"); 5911 // FIXME: Is there a way to make this work? 5912 // verifyIndependentOfContext("MACRO(A *a);"); 5913 5914 verifyFormat("DatumHandle const *operator->() const { return input_; }"); 5915 verifyFormat("return options != nullptr && operator==(*options);"); 5916 5917 EXPECT_EQ("#define OP(x) \\\n" 5918 " ostream &operator<<(ostream &s, const A &a) { \\\n" 5919 " return s << a.DebugString(); \\\n" 5920 " }", 5921 format("#define OP(x) \\\n" 5922 " ostream &operator<<(ostream &s, const A &a) { \\\n" 5923 " return s << a.DebugString(); \\\n" 5924 " }", 5925 getLLVMStyleWithColumns(50))); 5926 5927 // FIXME: We cannot handle this case yet; we might be able to figure out that 5928 // foo<x> d > v; doesn't make sense. 5929 verifyFormat("foo<a<b && c> d> v;"); 5930 5931 FormatStyle PointerMiddle = getLLVMStyle(); 5932 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 5933 verifyFormat("delete *x;", PointerMiddle); 5934 verifyFormat("int * x;", PointerMiddle); 5935 verifyFormat("template <int * y> f() {}", PointerMiddle); 5936 verifyFormat("int * f(int * a) {}", PointerMiddle); 5937 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle); 5938 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle); 5939 verifyFormat("A<int *> a;", PointerMiddle); 5940 verifyFormat("A<int **> a;", PointerMiddle); 5941 verifyFormat("A<int *, int *> a;", PointerMiddle); 5942 verifyFormat("A<int * []> a;", PointerMiddle); 5943 verifyFormat("A = new SomeType *[Length]();", PointerMiddle); 5944 verifyFormat("A = new SomeType *[Length];", PointerMiddle); 5945 verifyFormat("T ** t = new T *;", PointerMiddle); 5946 5947 // Member function reference qualifiers aren't binary operators. 5948 verifyFormat("string // break\n" 5949 "operator()() & {}"); 5950 verifyFormat("string // break\n" 5951 "operator()() && {}"); 5952 verifyGoogleFormat("template <typename T>\n" 5953 "auto x() & -> int {}"); 5954 } 5955 5956 TEST_F(FormatTest, UnderstandsAttributes) { 5957 verifyFormat("SomeType s __attribute__((unused)) (InitValue);"); 5958 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n" 5959 "aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 5960 FormatStyle AfterType = getLLVMStyle(); 5961 AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 5962 verifyFormat("__attribute__((nodebug)) void\n" 5963 "foo() {}\n", 5964 AfterType); 5965 } 5966 5967 TEST_F(FormatTest, UnderstandsEllipsis) { 5968 verifyFormat("int printf(const char *fmt, ...);"); 5969 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); 5970 verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}"); 5971 5972 FormatStyle PointersLeft = getLLVMStyle(); 5973 PointersLeft.PointerAlignment = FormatStyle::PAS_Left; 5974 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft); 5975 } 5976 5977 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { 5978 EXPECT_EQ("int *a;\n" 5979 "int *a;\n" 5980 "int *a;", 5981 format("int *a;\n" 5982 "int* a;\n" 5983 "int *a;", 5984 getGoogleStyle())); 5985 EXPECT_EQ("int* a;\n" 5986 "int* a;\n" 5987 "int* a;", 5988 format("int* a;\n" 5989 "int* a;\n" 5990 "int *a;", 5991 getGoogleStyle())); 5992 EXPECT_EQ("int *a;\n" 5993 "int *a;\n" 5994 "int *a;", 5995 format("int *a;\n" 5996 "int * a;\n" 5997 "int * a;", 5998 getGoogleStyle())); 5999 EXPECT_EQ("auto x = [] {\n" 6000 " int *a;\n" 6001 " int *a;\n" 6002 " int *a;\n" 6003 "};", 6004 format("auto x=[]{int *a;\n" 6005 "int * a;\n" 6006 "int * a;};", 6007 getGoogleStyle())); 6008 } 6009 6010 TEST_F(FormatTest, UnderstandsRvalueReferences) { 6011 verifyFormat("int f(int &&a) {}"); 6012 verifyFormat("int f(int a, char &&b) {}"); 6013 verifyFormat("void f() { int &&a = b; }"); 6014 verifyGoogleFormat("int f(int a, char&& b) {}"); 6015 verifyGoogleFormat("void f() { int&& a = b; }"); 6016 6017 verifyIndependentOfContext("A<int &&> a;"); 6018 verifyIndependentOfContext("A<int &&, int &&> a;"); 6019 verifyGoogleFormat("A<int&&> a;"); 6020 verifyGoogleFormat("A<int&&, int&&> a;"); 6021 6022 // Not rvalue references: 6023 verifyFormat("template <bool B, bool C> class A {\n" 6024 " static_assert(B && C, \"Something is wrong\");\n" 6025 "};"); 6026 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))"); 6027 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))"); 6028 verifyFormat("#define A(a, b) (a && b)"); 6029 } 6030 6031 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 6032 verifyFormat("void f() {\n" 6033 " x[aaaaaaaaa -\n" 6034 " b] = 23;\n" 6035 "}", 6036 getLLVMStyleWithColumns(15)); 6037 } 6038 6039 TEST_F(FormatTest, FormatsCasts) { 6040 verifyFormat("Type *A = static_cast<Type *>(P);"); 6041 verifyFormat("Type *A = (Type *)P;"); 6042 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 6043 verifyFormat("int a = (int)(2.0f);"); 6044 verifyFormat("int a = (int)2.0f;"); 6045 verifyFormat("x[(int32)y];"); 6046 verifyFormat("x = (int32)y;"); 6047 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)"); 6048 verifyFormat("int a = (int)*b;"); 6049 verifyFormat("int a = (int)2.0f;"); 6050 verifyFormat("int a = (int)~0;"); 6051 verifyFormat("int a = (int)++a;"); 6052 verifyFormat("int a = (int)sizeof(int);"); 6053 verifyFormat("int a = (int)+2;"); 6054 verifyFormat("my_int a = (my_int)2.0f;"); 6055 verifyFormat("my_int a = (my_int)sizeof(int);"); 6056 verifyFormat("return (my_int)aaa;"); 6057 verifyFormat("#define x ((int)-1)"); 6058 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1"); 6059 verifyFormat("#define p(q) ((int *)&q)"); 6060 verifyFormat("fn(a)(b) + 1;"); 6061 6062 verifyFormat("void f() { my_int a = (my_int)*b; }"); 6063 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }"); 6064 verifyFormat("my_int a = (my_int)~0;"); 6065 verifyFormat("my_int a = (my_int)++a;"); 6066 verifyFormat("my_int a = (my_int)-2;"); 6067 verifyFormat("my_int a = (my_int)1;"); 6068 verifyFormat("my_int a = (my_int *)1;"); 6069 verifyFormat("my_int a = (const my_int)-1;"); 6070 verifyFormat("my_int a = (const my_int *)-1;"); 6071 verifyFormat("my_int a = (my_int)(my_int)-1;"); 6072 verifyFormat("my_int a = (ns::my_int)-2;"); 6073 verifyFormat("case (my_int)ONE:"); 6074 verifyFormat("auto x = (X)this;"); 6075 6076 // FIXME: single value wrapped with paren will be treated as cast. 6077 verifyFormat("void f(int i = (kValue)*kMask) {}"); 6078 6079 verifyFormat("{ (void)F; }"); 6080 6081 // Don't break after a cast's 6082 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 6083 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n" 6084 " bbbbbbbbbbbbbbbbbbbbbb);"); 6085 6086 // These are not casts. 6087 verifyFormat("void f(int *) {}"); 6088 verifyFormat("f(foo)->b;"); 6089 verifyFormat("f(foo).b;"); 6090 verifyFormat("f(foo)(b);"); 6091 verifyFormat("f(foo)[b];"); 6092 verifyFormat("[](foo) { return 4; }(bar);"); 6093 verifyFormat("(*funptr)(foo)[4];"); 6094 verifyFormat("funptrs[4](foo)[4];"); 6095 verifyFormat("void f(int *);"); 6096 verifyFormat("void f(int *) = 0;"); 6097 verifyFormat("void f(SmallVector<int>) {}"); 6098 verifyFormat("void f(SmallVector<int>);"); 6099 verifyFormat("void f(SmallVector<int>) = 0;"); 6100 verifyFormat("void f(int i = (kA * kB) & kMask) {}"); 6101 verifyFormat("int a = sizeof(int) * b;"); 6102 verifyFormat("int a = alignof(int) * b;", getGoogleStyle()); 6103 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;"); 6104 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");"); 6105 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;"); 6106 6107 // These are not casts, but at some point were confused with casts. 6108 verifyFormat("virtual void foo(int *) override;"); 6109 verifyFormat("virtual void foo(char &) const;"); 6110 verifyFormat("virtual void foo(int *a, char *) const;"); 6111 verifyFormat("int a = sizeof(int *) + b;"); 6112 verifyFormat("int a = alignof(int *) + b;", getGoogleStyle()); 6113 verifyFormat("bool b = f(g<int>) && c;"); 6114 verifyFormat("typedef void (*f)(int i) func;"); 6115 6116 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n" 6117 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 6118 // FIXME: The indentation here is not ideal. 6119 verifyFormat( 6120 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6121 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n" 6122 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];"); 6123 } 6124 6125 TEST_F(FormatTest, FormatsFunctionTypes) { 6126 verifyFormat("A<bool()> a;"); 6127 verifyFormat("A<SomeType()> a;"); 6128 verifyFormat("A<void (*)(int, std::string)> a;"); 6129 verifyFormat("A<void *(int)>;"); 6130 verifyFormat("void *(*a)(int *, SomeType *);"); 6131 verifyFormat("int (*func)(void *);"); 6132 verifyFormat("void f() { int (*func)(void *); }"); 6133 verifyFormat("template <class CallbackClass>\n" 6134 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);"); 6135 6136 verifyGoogleFormat("A<void*(int*, SomeType*)>;"); 6137 verifyGoogleFormat("void* (*a)(int);"); 6138 verifyGoogleFormat( 6139 "template <class CallbackClass>\n" 6140 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);"); 6141 6142 // Other constructs can look somewhat like function types: 6143 verifyFormat("A<sizeof(*x)> a;"); 6144 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)"); 6145 verifyFormat("some_var = function(*some_pointer_var)[0];"); 6146 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }"); 6147 verifyFormat("int x = f(&h)();"); 6148 verifyFormat("returnsFunction(¶m1, ¶m2)(param);"); 6149 verifyFormat("std::function<\n" 6150 " LooooooooooongTemplatedType<\n" 6151 " SomeType>*(\n" 6152 " LooooooooooooooooongType type)>\n" 6153 " function;", 6154 getGoogleStyleWithColumns(40)); 6155 } 6156 6157 TEST_F(FormatTest, FormatsPointersToArrayTypes) { 6158 verifyFormat("A (*foo_)[6];"); 6159 verifyFormat("vector<int> (*foo_)[6];"); 6160 } 6161 6162 TEST_F(FormatTest, BreaksLongVariableDeclarations) { 6163 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6164 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6165 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n" 6166 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6167 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6168 " *LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6169 6170 // Different ways of ()-initializiation. 6171 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6172 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);"); 6173 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6174 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);"); 6175 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6176 " LoooooooooooooooooooooooooooooooooooooooongVariable({});"); 6177 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6178 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);"); 6179 6180 // Lambdas should not confuse the variable declaration heuristic. 6181 verifyFormat("LooooooooooooooooongType\n" 6182 " variable(nullptr, [](A *a) {});", 6183 getLLVMStyleWithColumns(40)); 6184 } 6185 6186 TEST_F(FormatTest, BreaksLongDeclarations) { 6187 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n" 6188 " AnotherNameForTheLongType;"); 6189 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n" 6190 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 6191 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6192 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 6193 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n" 6194 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 6195 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6196 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6197 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n" 6198 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6199 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 6200 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6201 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 6202 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6203 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6204 "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);"); 6205 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6206 "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}"); 6207 FormatStyle Indented = getLLVMStyle(); 6208 Indented.IndentWrappedFunctionNames = true; 6209 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6210 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();", 6211 Indented); 6212 verifyFormat( 6213 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6214 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6215 Indented); 6216 verifyFormat( 6217 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 6218 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6219 Indented); 6220 verifyFormat( 6221 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 6222 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6223 Indented); 6224 6225 // FIXME: Without the comment, this breaks after "(". 6226 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n" 6227 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();", 6228 getGoogleStyle()); 6229 6230 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 6231 " int LoooooooooooooooooooongParam2) {}"); 6232 verifyFormat( 6233 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" 6234 " SourceLocation L, IdentifierIn *II,\n" 6235 " Type *T) {}"); 6236 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" 6237 "ReallyReaaallyLongFunctionName(\n" 6238 " const std::string &SomeParameter,\n" 6239 " const SomeType<string, SomeOtherTemplateParameter>\n" 6240 " &ReallyReallyLongParameterName,\n" 6241 " const SomeType<string, SomeOtherTemplateParameter>\n" 6242 " &AnotherLongParameterName) {}"); 6243 verifyFormat("template <typename A>\n" 6244 "SomeLoooooooooooooooooooooongType<\n" 6245 " typename some_namespace::SomeOtherType<A>::Type>\n" 6246 "Function() {}"); 6247 6248 verifyGoogleFormat( 6249 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" 6250 " aaaaaaaaaaaaaaaaaaaaaaa;"); 6251 verifyGoogleFormat( 6252 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" 6253 " SourceLocation L) {}"); 6254 verifyGoogleFormat( 6255 "some_namespace::LongReturnType\n" 6256 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" 6257 " int first_long_parameter, int second_parameter) {}"); 6258 6259 verifyGoogleFormat("template <typename T>\n" 6260 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" 6261 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); 6262 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6263 " int aaaaaaaaaaaaaaaaaaaaaaa);"); 6264 6265 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 6266 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6267 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6268 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6269 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 6270 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 6271 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6272 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 6273 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n" 6274 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6275 6276 verifyFormat("template <typename T> // Templates on own line.\n" 6277 "static int // Some comment.\n" 6278 "MyFunction(int a);", 6279 getLLVMStyle()); 6280 } 6281 6282 TEST_F(FormatTest, FormatsArrays) { 6283 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6284 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;"); 6285 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n" 6286 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;"); 6287 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n" 6288 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}"); 6289 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6290 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 6291 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6292 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;"); 6293 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6294 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6295 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 6296 verifyFormat( 6297 "llvm::outs() << \"aaaaaaaaaaaa: \"\n" 6298 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6299 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 6300 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n" 6301 " .aaaaaaaaaaaaaaaaaaaaaa();"); 6302 6303 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n" 6304 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];"); 6305 verifyFormat( 6306 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n" 6307 " .aaaaaaa[0]\n" 6308 " .aaaaaaaaaaaaaaaaaaaaaa();"); 6309 verifyFormat("a[::b::c];"); 6310 6311 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10)); 6312 6313 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0); 6314 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit); 6315 } 6316 6317 TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 6318 verifyFormat("(a)->b();"); 6319 verifyFormat("--a;"); 6320 } 6321 6322 TEST_F(FormatTest, HandlesIncludeDirectives) { 6323 verifyFormat("#include <string>\n" 6324 "#include <a/b/c.h>\n" 6325 "#include \"a/b/string\"\n" 6326 "#include \"string.h\"\n" 6327 "#include \"string.h\"\n" 6328 "#include <a-a>\n" 6329 "#include < path with space >\n" 6330 "#include_next <test.h>" 6331 "#include \"abc.h\" // this is included for ABC\n" 6332 "#include \"some long include\" // with a comment\n" 6333 "#include \"some very long include path\"\n" 6334 "#include <some/very/long/include/path>\n", 6335 getLLVMStyleWithColumns(35)); 6336 EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\"")); 6337 EXPECT_EQ("#include <a>", format("#include<a>")); 6338 6339 verifyFormat("#import <string>"); 6340 verifyFormat("#import <a/b/c.h>"); 6341 verifyFormat("#import \"a/b/string\""); 6342 verifyFormat("#import \"string.h\""); 6343 verifyFormat("#import \"string.h\""); 6344 verifyFormat("#if __has_include(<strstream>)\n" 6345 "#include <strstream>\n" 6346 "#endif"); 6347 6348 verifyFormat("#define MY_IMPORT <a/b>"); 6349 6350 verifyFormat("#if __has_include(<a/b>)"); 6351 verifyFormat("#if __has_include_next(<a/b>)"); 6352 verifyFormat("#define F __has_include(<a/b>)"); 6353 verifyFormat("#define F __has_include_next(<a/b>)"); 6354 6355 // Protocol buffer definition or missing "#". 6356 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";", 6357 getLLVMStyleWithColumns(30)); 6358 6359 FormatStyle Style = getLLVMStyle(); 6360 Style.AlwaysBreakBeforeMultilineStrings = true; 6361 Style.ColumnLimit = 0; 6362 verifyFormat("#import \"abc.h\"", Style); 6363 6364 // But 'import' might also be a regular C++ namespace. 6365 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6366 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6367 } 6368 6369 //===----------------------------------------------------------------------===// 6370 // Error recovery tests. 6371 //===----------------------------------------------------------------------===// 6372 6373 TEST_F(FormatTest, IncompleteParameterLists) { 6374 FormatStyle NoBinPacking = getLLVMStyle(); 6375 NoBinPacking.BinPackParameters = false; 6376 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" 6377 " double *min_x,\n" 6378 " double *max_x,\n" 6379 " double *min_y,\n" 6380 " double *max_y,\n" 6381 " double *min_z,\n" 6382 " double *max_z, ) {}", 6383 NoBinPacking); 6384 } 6385 6386 TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 6387 verifyFormat("void f() { return; }\n42"); 6388 verifyFormat("void f() {\n" 6389 " if (0)\n" 6390 " return;\n" 6391 "}\n" 6392 "42"); 6393 verifyFormat("void f() { return }\n42"); 6394 verifyFormat("void f() {\n" 6395 " if (0)\n" 6396 " return\n" 6397 "}\n" 6398 "42"); 6399 } 6400 6401 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 6402 EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); 6403 EXPECT_EQ("void f() {\n" 6404 " if (a)\n" 6405 " return\n" 6406 "}", 6407 format("void f ( ) { if ( a ) return }")); 6408 EXPECT_EQ("namespace N {\n" 6409 "void f()\n" 6410 "}", 6411 format("namespace N { void f() }")); 6412 EXPECT_EQ("namespace N {\n" 6413 "void f() {}\n" 6414 "void g()\n" 6415 "} // namespace N", 6416 format("namespace N { void f( ) { } void g( ) }")); 6417 } 6418 6419 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 6420 verifyFormat("int aaaaaaaa =\n" 6421 " // Overlylongcomment\n" 6422 " b;", 6423 getLLVMStyleWithColumns(20)); 6424 verifyFormat("function(\n" 6425 " ShortArgument,\n" 6426 " LoooooooooooongArgument);\n", 6427 getLLVMStyleWithColumns(20)); 6428 } 6429 6430 TEST_F(FormatTest, IncorrectAccessSpecifier) { 6431 verifyFormat("public:"); 6432 verifyFormat("class A {\n" 6433 "public\n" 6434 " void f() {}\n" 6435 "};"); 6436 verifyFormat("public\n" 6437 "int qwerty;"); 6438 verifyFormat("public\n" 6439 "B {}"); 6440 verifyFormat("public\n" 6441 "{}"); 6442 verifyFormat("public\n" 6443 "B { int x; }"); 6444 } 6445 6446 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 6447 verifyFormat("{"); 6448 verifyFormat("#})"); 6449 verifyNoCrash("(/**/[:!] ?[)."); 6450 } 6451 6452 TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 6453 verifyFormat("do {\n}"); 6454 verifyFormat("do {\n}\n" 6455 "f();"); 6456 verifyFormat("do {\n}\n" 6457 "wheeee(fun);"); 6458 verifyFormat("do {\n" 6459 " f();\n" 6460 "}"); 6461 } 6462 6463 TEST_F(FormatTest, IncorrectCodeMissingParens) { 6464 verifyFormat("if {\n foo;\n foo();\n}"); 6465 verifyFormat("switch {\n foo;\n foo();\n}"); 6466 verifyIncompleteFormat("for {\n foo;\n foo();\n}"); 6467 verifyFormat("while {\n foo;\n foo();\n}"); 6468 verifyFormat("do {\n foo;\n foo();\n} while;"); 6469 } 6470 6471 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 6472 verifyIncompleteFormat("namespace {\n" 6473 "class Foo { Foo (\n" 6474 "};\n" 6475 "} // namespace"); 6476 } 6477 6478 TEST_F(FormatTest, IncorrectCodeErrorDetection) { 6479 EXPECT_EQ("{\n {}\n", format("{\n{\n}\n")); 6480 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 6481 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 6482 EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n")); 6483 6484 EXPECT_EQ("{\n" 6485 " {\n" 6486 " breakme(\n" 6487 " qwe);\n" 6488 " }\n", 6489 format("{\n" 6490 " {\n" 6491 " breakme(qwe);\n" 6492 "}\n", 6493 getLLVMStyleWithColumns(10))); 6494 } 6495 6496 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 6497 verifyFormat("int x = {\n" 6498 " avariable,\n" 6499 " b(alongervariable)};", 6500 getLLVMStyleWithColumns(25)); 6501 } 6502 6503 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 6504 verifyFormat("return (a)(b){1, 2, 3};"); 6505 } 6506 6507 TEST_F(FormatTest, LayoutCxx11BraceInitializers) { 6508 verifyFormat("vector<int> x{1, 2, 3, 4};"); 6509 verifyFormat("vector<int> x{\n" 6510 " 1,\n" 6511 " 2,\n" 6512 " 3,\n" 6513 " 4,\n" 6514 "};"); 6515 verifyFormat("vector<T> x{{}, {}, {}, {}};"); 6516 verifyFormat("f({1, 2});"); 6517 verifyFormat("auto v = Foo{-1};"); 6518 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});"); 6519 verifyFormat("Class::Class : member{1, 2, 3} {}"); 6520 verifyFormat("new vector<int>{1, 2, 3};"); 6521 verifyFormat("new int[3]{1, 2, 3};"); 6522 verifyFormat("new int{1};"); 6523 verifyFormat("return {arg1, arg2};"); 6524 verifyFormat("return {arg1, SomeType{parameter}};"); 6525 verifyFormat("int count = set<int>{f(), g(), h()}.size();"); 6526 verifyFormat("new T{arg1, arg2};"); 6527 verifyFormat("f(MyMap[{composite, key}]);"); 6528 verifyFormat("class Class {\n" 6529 " T member = {arg1, arg2};\n" 6530 "};"); 6531 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};"); 6532 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 6533 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 6534 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");"); 6535 verifyFormat("int a = std::is_integral<int>{} + 0;"); 6536 6537 verifyFormat("int foo(int i) { return fo1{}(i); }"); 6538 verifyFormat("int foo(int i) { return fo1{}(i); }"); 6539 verifyFormat("auto i = decltype(x){};"); 6540 verifyFormat("std::vector<int> v = {1, 0 /* comment */};"); 6541 verifyFormat("Node n{1, Node{1000}, //\n" 6542 " 2};"); 6543 verifyFormat("Aaaa aaaaaaa{\n" 6544 " {\n" 6545 " aaaa,\n" 6546 " },\n" 6547 "};"); 6548 verifyFormat("class C : public D {\n" 6549 " SomeClass SC{2};\n" 6550 "};"); 6551 verifyFormat("class C : public A {\n" 6552 " class D : public B {\n" 6553 " void f() { int i{2}; }\n" 6554 " };\n" 6555 "};"); 6556 verifyFormat("#define A {a, a},"); 6557 6558 // Binpacking only if there is no trailing comma 6559 verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n" 6560 " cccccccccc, dddddddddd};", 6561 getLLVMStyleWithColumns(50)); 6562 verifyFormat("const Aaaaaa aaaaa = {\n" 6563 " aaaaaaaaaaa,\n" 6564 " bbbbbbbbbbb,\n" 6565 " ccccccccccc,\n" 6566 " ddddddddddd,\n" 6567 "};", getLLVMStyleWithColumns(50)); 6568 6569 // Cases where distinguising braced lists and blocks is hard. 6570 verifyFormat("vector<int> v{12} GUARDED_BY(mutex);"); 6571 verifyFormat("void f() {\n" 6572 " return; // comment\n" 6573 "}\n" 6574 "SomeType t;"); 6575 verifyFormat("void f() {\n" 6576 " if (a) {\n" 6577 " f();\n" 6578 " }\n" 6579 "}\n" 6580 "SomeType t;"); 6581 6582 // In combination with BinPackArguments = false. 6583 FormatStyle NoBinPacking = getLLVMStyle(); 6584 NoBinPacking.BinPackArguments = false; 6585 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n" 6586 " bbbbb,\n" 6587 " ccccc,\n" 6588 " ddddd,\n" 6589 " eeeee,\n" 6590 " ffffff,\n" 6591 " ggggg,\n" 6592 " hhhhhh,\n" 6593 " iiiiii,\n" 6594 " jjjjjj,\n" 6595 " kkkkkk};", 6596 NoBinPacking); 6597 verifyFormat("const Aaaaaa aaaaa = {\n" 6598 " aaaaa,\n" 6599 " bbbbb,\n" 6600 " ccccc,\n" 6601 " ddddd,\n" 6602 " eeeee,\n" 6603 " ffffff,\n" 6604 " ggggg,\n" 6605 " hhhhhh,\n" 6606 " iiiiii,\n" 6607 " jjjjjj,\n" 6608 " kkkkkk,\n" 6609 "};", 6610 NoBinPacking); 6611 verifyFormat( 6612 "const Aaaaaa aaaaa = {\n" 6613 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n" 6614 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n" 6615 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n" 6616 "};", 6617 NoBinPacking); 6618 6619 // FIXME: The alignment of these trailing comments might be bad. Then again, 6620 // this might be utterly useless in real code. 6621 verifyFormat("Constructor::Constructor()\n" 6622 " : some_value{ //\n" 6623 " aaaaaaa, //\n" 6624 " bbbbbbb} {}"); 6625 6626 // In braced lists, the first comment is always assumed to belong to the 6627 // first element. Thus, it can be moved to the next or previous line as 6628 // appropriate. 6629 EXPECT_EQ("function({// First element:\n" 6630 " 1,\n" 6631 " // Second element:\n" 6632 " 2});", 6633 format("function({\n" 6634 " // First element:\n" 6635 " 1,\n" 6636 " // Second element:\n" 6637 " 2});")); 6638 EXPECT_EQ("std::vector<int> MyNumbers{\n" 6639 " // First element:\n" 6640 " 1,\n" 6641 " // Second element:\n" 6642 " 2};", 6643 format("std::vector<int> MyNumbers{// First element:\n" 6644 " 1,\n" 6645 " // Second element:\n" 6646 " 2};", 6647 getLLVMStyleWithColumns(30))); 6648 // A trailing comma should still lead to an enforced line break and no 6649 // binpacking. 6650 EXPECT_EQ("vector<int> SomeVector = {\n" 6651 " // aaa\n" 6652 " 1,\n" 6653 " 2,\n" 6654 "};", 6655 format("vector<int> SomeVector = { // aaa\n" 6656 " 1, 2, };")); 6657 6658 FormatStyle ExtraSpaces = getLLVMStyle(); 6659 ExtraSpaces.Cpp11BracedListStyle = false; 6660 ExtraSpaces.ColumnLimit = 75; 6661 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces); 6662 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces); 6663 verifyFormat("f({ 1, 2 });", ExtraSpaces); 6664 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces); 6665 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces); 6666 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces); 6667 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces); 6668 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces); 6669 verifyFormat("return { arg1, arg2 };", ExtraSpaces); 6670 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces); 6671 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces); 6672 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces); 6673 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces); 6674 verifyFormat("class Class {\n" 6675 " T member = { arg1, arg2 };\n" 6676 "};", 6677 ExtraSpaces); 6678 verifyFormat( 6679 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6680 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n" 6681 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 6682 " bbbbbbbbbbbbbbbbbbbb, bbbbb };", 6683 ExtraSpaces); 6684 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces); 6685 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });", 6686 ExtraSpaces); 6687 verifyFormat( 6688 "someFunction(OtherParam,\n" 6689 " BracedList{ // comment 1 (Forcing interesting break)\n" 6690 " param1, param2,\n" 6691 " // comment 2\n" 6692 " param3, param4 });", 6693 ExtraSpaces); 6694 verifyFormat( 6695 "std::this_thread::sleep_for(\n" 6696 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);", 6697 ExtraSpaces); 6698 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n" 6699 " aaaaaaa,\n" 6700 " aaaaaaaaaa,\n" 6701 " aaaaa,\n" 6702 " aaaaaaaaaaaaaaa,\n" 6703 " aaa,\n" 6704 " aaaaaaaaaa,\n" 6705 " a,\n" 6706 " aaaaaaaaaaaaaaaaaaaaa,\n" 6707 " aaaaaaaaaaaa,\n" 6708 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n" 6709 " aaaaaaa,\n" 6710 " a};"); 6711 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces); 6712 verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces); 6713 verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces); 6714 } 6715 6716 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { 6717 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6718 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6719 " 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};"); 6723 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n" 6724 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6725 " 1, 22, 333, 4444, 55555, //\n" 6726 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6727 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 6728 verifyFormat( 6729 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6730 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6731 " 1, 22, 333, 4444, 55555, 666666, // comment\n" 6732 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6733 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6734 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6735 " 7777777};"); 6736 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6737 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6738 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6739 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6740 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6741 " // Separating comment.\n" 6742 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6743 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6744 " // Leading comment\n" 6745 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6746 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6747 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6748 " 1, 1, 1, 1};", 6749 getLLVMStyleWithColumns(39)); 6750 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6751 " 1, 1, 1, 1};", 6752 getLLVMStyleWithColumns(38)); 6753 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n" 6754 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};", 6755 getLLVMStyleWithColumns(43)); 6756 verifyFormat( 6757 "static unsigned SomeValues[10][3] = {\n" 6758 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n" 6759 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};"); 6760 verifyFormat("static auto fields = new vector<string>{\n" 6761 " \"aaaaaaaaaaaaa\",\n" 6762 " \"aaaaaaaaaaaaa\",\n" 6763 " \"aaaaaaaaaaaa\",\n" 6764 " \"aaaaaaaaaaaaaa\",\n" 6765 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 6766 " \"aaaaaaaaaaaa\",\n" 6767 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 6768 "};"); 6769 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};"); 6770 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n" 6771 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n" 6772 " 3, cccccccccccccccccccccc};", 6773 getLLVMStyleWithColumns(60)); 6774 6775 // Trailing commas. 6776 verifyFormat("vector<int> x = {\n" 6777 " 1, 1, 1, 1, 1, 1, 1, 1,\n" 6778 "};", 6779 getLLVMStyleWithColumns(39)); 6780 verifyFormat("vector<int> x = {\n" 6781 " 1, 1, 1, 1, 1, 1, 1, 1, //\n" 6782 "};", 6783 getLLVMStyleWithColumns(39)); 6784 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6785 " 1, 1, 1, 1,\n" 6786 " /**/ /**/};", 6787 getLLVMStyleWithColumns(39)); 6788 6789 // Trailing comment in the first line. 6790 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n" 6791 " 1111111111, 2222222222, 33333333333, 4444444444, //\n" 6792 " 111111111, 222222222, 3333333333, 444444444, //\n" 6793 " 11111111, 22222222, 333333333, 44444444};"); 6794 // Trailing comment in the last line. 6795 verifyFormat("int aaaaa[] = {\n" 6796 " 1, 2, 3, // comment\n" 6797 " 4, 5, 6 // comment\n" 6798 "};"); 6799 6800 // With nested lists, we should either format one item per line or all nested 6801 // lists one on line. 6802 // FIXME: For some nested lists, we can do better. 6803 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n" 6804 " {aaaaaaaaaaaaaaaaaaa},\n" 6805 " {aaaaaaaaaaaaaaaaaaaaa},\n" 6806 " {aaaaaaaaaaaaaaaaa}};", 6807 getLLVMStyleWithColumns(60)); 6808 verifyFormat( 6809 "SomeStruct my_struct_array = {\n" 6810 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n" 6811 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n" 6812 " {aaa, aaa},\n" 6813 " {aaa, aaa},\n" 6814 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n" 6815 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6816 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};"); 6817 6818 // No column layout should be used here. 6819 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n" 6820 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};"); 6821 6822 verifyNoCrash("a<,"); 6823 6824 // No braced initializer here. 6825 verifyFormat("void f() {\n" 6826 " struct Dummy {};\n" 6827 " f(v);\n" 6828 "}"); 6829 6830 // Long lists should be formatted in columns even if they are nested. 6831 verifyFormat( 6832 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6833 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6834 " 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});"); 6838 6839 // Allow "single-column" layout even if that violates the column limit. There 6840 // isn't going to be a better way. 6841 verifyFormat("std::vector<int> a = {\n" 6842 " aaaaaaaa,\n" 6843 " aaaaaaaa,\n" 6844 " aaaaaaaa,\n" 6845 " aaaaaaaa,\n" 6846 " aaaaaaaaaa,\n" 6847 " aaaaaaaa,\n" 6848 " aaaaaaaaaaaaaaaaaaaaaaaaaaa};", 6849 getLLVMStyleWithColumns(30)); 6850 verifyFormat("vector<int> aaaa = {\n" 6851 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6852 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6853 " aaaaaa.aaaaaaa,\n" 6854 " aaaaaa.aaaaaaa,\n" 6855 " aaaaaa.aaaaaaa,\n" 6856 " aaaaaa.aaaaaaa,\n" 6857 "};"); 6858 6859 // Don't create hanging lists. 6860 verifyFormat("someFunction(Param, {List1, List2,\n" 6861 " List3});", 6862 getLLVMStyleWithColumns(35)); 6863 verifyFormat("someFunction(Param, Param,\n" 6864 " {List1, List2,\n" 6865 " List3});", 6866 getLLVMStyleWithColumns(35)); 6867 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n" 6868 " aaaaaaaaaaaaaaaaaaaaaaa);"); 6869 } 6870 6871 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 6872 FormatStyle DoNotMerge = getLLVMStyle(); 6873 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 6874 6875 verifyFormat("void f() { return 42; }"); 6876 verifyFormat("void f() {\n" 6877 " return 42;\n" 6878 "}", 6879 DoNotMerge); 6880 verifyFormat("void f() {\n" 6881 " // Comment\n" 6882 "}"); 6883 verifyFormat("{\n" 6884 "#error {\n" 6885 " int a;\n" 6886 "}"); 6887 verifyFormat("{\n" 6888 " int a;\n" 6889 "#error {\n" 6890 "}"); 6891 verifyFormat("void f() {} // comment"); 6892 verifyFormat("void f() { int a; } // comment"); 6893 verifyFormat("void f() {\n" 6894 "} // comment", 6895 DoNotMerge); 6896 verifyFormat("void f() {\n" 6897 " int a;\n" 6898 "} // comment", 6899 DoNotMerge); 6900 verifyFormat("void f() {\n" 6901 "} // comment", 6902 getLLVMStyleWithColumns(15)); 6903 6904 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 6905 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 6906 6907 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 6908 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 6909 verifyFormat("class C {\n" 6910 " C()\n" 6911 " : iiiiiiii(nullptr),\n" 6912 " kkkkkkk(nullptr),\n" 6913 " mmmmmmm(nullptr),\n" 6914 " nnnnnnn(nullptr) {}\n" 6915 "};", 6916 getGoogleStyle()); 6917 6918 FormatStyle NoColumnLimit = getLLVMStyle(); 6919 NoColumnLimit.ColumnLimit = 0; 6920 EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit)); 6921 EXPECT_EQ("class C {\n" 6922 " A() : b(0) {}\n" 6923 "};", 6924 format("class C{A():b(0){}};", NoColumnLimit)); 6925 EXPECT_EQ("A()\n" 6926 " : b(0) {\n" 6927 "}", 6928 format("A()\n:b(0)\n{\n}", NoColumnLimit)); 6929 6930 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit; 6931 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine = 6932 FormatStyle::SFS_None; 6933 EXPECT_EQ("A()\n" 6934 " : b(0) {\n" 6935 "}", 6936 format("A():b(0){}", DoNotMergeNoColumnLimit)); 6937 EXPECT_EQ("A()\n" 6938 " : b(0) {\n" 6939 "}", 6940 format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit)); 6941 6942 verifyFormat("#define A \\\n" 6943 " void f() { \\\n" 6944 " int i; \\\n" 6945 " }", 6946 getLLVMStyleWithColumns(20)); 6947 verifyFormat("#define A \\\n" 6948 " void f() { int i; }", 6949 getLLVMStyleWithColumns(21)); 6950 verifyFormat("#define A \\\n" 6951 " void f() { \\\n" 6952 " int i; \\\n" 6953 " } \\\n" 6954 " int j;", 6955 getLLVMStyleWithColumns(22)); 6956 verifyFormat("#define A \\\n" 6957 " void f() { int i; } \\\n" 6958 " int j;", 6959 getLLVMStyleWithColumns(23)); 6960 } 6961 6962 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) { 6963 FormatStyle MergeEmptyOnly = getLLVMStyle(); 6964 MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 6965 verifyFormat("class C {\n" 6966 " int f() {}\n" 6967 "};", 6968 MergeEmptyOnly); 6969 verifyFormat("class C {\n" 6970 " int f() {\n" 6971 " return 42;\n" 6972 " }\n" 6973 "};", 6974 MergeEmptyOnly); 6975 verifyFormat("int f() {}", MergeEmptyOnly); 6976 verifyFormat("int f() {\n" 6977 " return 42;\n" 6978 "}", 6979 MergeEmptyOnly); 6980 6981 // Also verify behavior when BraceWrapping.AfterFunction = true 6982 MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 6983 MergeEmptyOnly.BraceWrapping.AfterFunction = true; 6984 verifyFormat("int f() {}", MergeEmptyOnly); 6985 verifyFormat("class C {\n" 6986 " int f() {}\n" 6987 "};", 6988 MergeEmptyOnly); 6989 } 6990 6991 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) { 6992 FormatStyle MergeInlineOnly = getLLVMStyle(); 6993 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 6994 verifyFormat("class C {\n" 6995 " int f() { return 42; }\n" 6996 "};", 6997 MergeInlineOnly); 6998 verifyFormat("int f() {\n" 6999 " return 42;\n" 7000 "}", 7001 MergeInlineOnly); 7002 7003 // SFS_Inline implies SFS_Empty 7004 verifyFormat("class C {\n" 7005 " int f() {}\n" 7006 "};", 7007 MergeInlineOnly); 7008 verifyFormat("int f() {}", MergeInlineOnly); 7009 7010 // Also verify behavior when BraceWrapping.AfterFunction = true 7011 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 7012 MergeInlineOnly.BraceWrapping.AfterFunction = true; 7013 verifyFormat("class C {\n" 7014 " int f() { return 42; }\n" 7015 "};", 7016 MergeInlineOnly); 7017 verifyFormat("int f()\n" 7018 "{\n" 7019 " return 42;\n" 7020 "}", 7021 MergeInlineOnly); 7022 7023 // SFS_Inline implies SFS_Empty 7024 verifyFormat("int f() {}", MergeInlineOnly); 7025 verifyFormat("class C {\n" 7026 " int f() {}\n" 7027 "};", 7028 MergeInlineOnly); 7029 } 7030 7031 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) { 7032 FormatStyle MergeInlineOnly = getLLVMStyle(); 7033 MergeInlineOnly.AllowShortFunctionsOnASingleLine = 7034 FormatStyle::SFS_InlineOnly; 7035 verifyFormat("class C {\n" 7036 " int f() { return 42; }\n" 7037 "};", 7038 MergeInlineOnly); 7039 verifyFormat("int f() {\n" 7040 " return 42;\n" 7041 "}", 7042 MergeInlineOnly); 7043 7044 // SFS_InlineOnly does not imply SFS_Empty 7045 verifyFormat("class C {\n" 7046 " int f() {}\n" 7047 "};", 7048 MergeInlineOnly); 7049 verifyFormat("int f() {\n" 7050 "}", 7051 MergeInlineOnly); 7052 7053 // Also verify behavior when BraceWrapping.AfterFunction = true 7054 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 7055 MergeInlineOnly.BraceWrapping.AfterFunction = true; 7056 verifyFormat("class C {\n" 7057 " int f() { return 42; }\n" 7058 "};", 7059 MergeInlineOnly); 7060 verifyFormat("int f()\n" 7061 "{\n" 7062 " return 42;\n" 7063 "}", 7064 MergeInlineOnly); 7065 7066 // SFS_InlineOnly does not imply SFS_Empty 7067 verifyFormat("int f()\n" 7068 "{\n" 7069 "}", 7070 MergeInlineOnly); 7071 verifyFormat("class C {\n" 7072 " int f() {}\n" 7073 "};", 7074 MergeInlineOnly); 7075 } 7076 7077 TEST_F(FormatTest, SplitEmptyFunction) { 7078 FormatStyle Style = getLLVMStyle(); 7079 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 7080 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7081 Style.BraceWrapping.AfterFunction = true; 7082 Style.BraceWrapping.SplitEmptyFunction = false; 7083 Style.ColumnLimit = 40; 7084 7085 verifyFormat("int f()\n" 7086 "{}", 7087 Style); 7088 verifyFormat("int f()\n" 7089 "{\n" 7090 " return 42;\n" 7091 "}", 7092 Style); 7093 verifyFormat("int f()\n" 7094 "{\n" 7095 " // some comment\n" 7096 "}", 7097 Style); 7098 7099 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 7100 verifyFormat("int f() {}", Style); 7101 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 7102 "{}", 7103 Style); 7104 verifyFormat("int f()\n" 7105 "{\n" 7106 " return 0;\n" 7107 "}", 7108 Style); 7109 7110 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 7111 verifyFormat("class Foo {\n" 7112 " int f() {}\n" 7113 "};\n", 7114 Style); 7115 verifyFormat("class Foo {\n" 7116 " int f() { return 0; }\n" 7117 "};\n", 7118 Style); 7119 verifyFormat("class Foo {\n" 7120 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 7121 " {}\n" 7122 "};\n", 7123 Style); 7124 verifyFormat("class Foo {\n" 7125 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 7126 " {\n" 7127 " return 0;\n" 7128 " }\n" 7129 "};\n", 7130 Style); 7131 7132 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 7133 verifyFormat("int f() {}", Style); 7134 verifyFormat("int f() { return 0; }", Style); 7135 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 7136 "{}", 7137 Style); 7138 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 7139 "{\n" 7140 " return 0;\n" 7141 "}", 7142 Style); 7143 } 7144 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) { 7145 FormatStyle Style = getLLVMStyle(); 7146 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 7147 verifyFormat("#ifdef A\n" 7148 "int f() {}\n" 7149 "#else\n" 7150 "int g() {}\n" 7151 "#endif", 7152 Style); 7153 } 7154 7155 TEST_F(FormatTest, SplitEmptyClass) { 7156 FormatStyle Style = getLLVMStyle(); 7157 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7158 Style.BraceWrapping.AfterClass = true; 7159 Style.BraceWrapping.SplitEmptyRecord = false; 7160 7161 verifyFormat("class Foo\n" 7162 "{};", 7163 Style); 7164 verifyFormat("/* something */ class Foo\n" 7165 "{};", 7166 Style); 7167 verifyFormat("template <typename X> class Foo\n" 7168 "{};", 7169 Style); 7170 verifyFormat("class Foo\n" 7171 "{\n" 7172 " Foo();\n" 7173 "};", 7174 Style); 7175 verifyFormat("typedef class Foo\n" 7176 "{\n" 7177 "} Foo_t;", 7178 Style); 7179 } 7180 7181 TEST_F(FormatTest, SplitEmptyStruct) { 7182 FormatStyle Style = getLLVMStyle(); 7183 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7184 Style.BraceWrapping.AfterStruct = true; 7185 Style.BraceWrapping.SplitEmptyRecord = false; 7186 7187 verifyFormat("struct Foo\n" 7188 "{};", 7189 Style); 7190 verifyFormat("/* something */ struct Foo\n" 7191 "{};", 7192 Style); 7193 verifyFormat("template <typename X> struct Foo\n" 7194 "{};", 7195 Style); 7196 verifyFormat("struct Foo\n" 7197 "{\n" 7198 " Foo();\n" 7199 "};", 7200 Style); 7201 verifyFormat("typedef struct Foo\n" 7202 "{\n" 7203 "} Foo_t;", 7204 Style); 7205 //typedef struct Bar {} Bar_t; 7206 } 7207 7208 TEST_F(FormatTest, SplitEmptyUnion) { 7209 FormatStyle Style = getLLVMStyle(); 7210 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7211 Style.BraceWrapping.AfterUnion = true; 7212 Style.BraceWrapping.SplitEmptyRecord = false; 7213 7214 verifyFormat("union Foo\n" 7215 "{};", 7216 Style); 7217 verifyFormat("/* something */ union Foo\n" 7218 "{};", 7219 Style); 7220 verifyFormat("union Foo\n" 7221 "{\n" 7222 " A,\n" 7223 "};", 7224 Style); 7225 verifyFormat("typedef union Foo\n" 7226 "{\n" 7227 "} Foo_t;", 7228 Style); 7229 } 7230 7231 TEST_F(FormatTest, SplitEmptyNamespace) { 7232 FormatStyle Style = getLLVMStyle(); 7233 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7234 Style.BraceWrapping.AfterNamespace = true; 7235 Style.BraceWrapping.SplitEmptyNamespace = false; 7236 7237 verifyFormat("namespace Foo\n" 7238 "{};", 7239 Style); 7240 verifyFormat("/* something */ namespace Foo\n" 7241 "{};", 7242 Style); 7243 verifyFormat("inline namespace Foo\n" 7244 "{};", 7245 Style); 7246 verifyFormat("namespace Foo\n" 7247 "{\n" 7248 "void Bar();\n" 7249 "};", 7250 Style); 7251 } 7252 7253 TEST_F(FormatTest, NeverMergeShortRecords) { 7254 FormatStyle Style = getLLVMStyle(); 7255 7256 verifyFormat("class Foo {\n" 7257 " Foo();\n" 7258 "};", 7259 Style); 7260 verifyFormat("typedef class Foo {\n" 7261 " Foo();\n" 7262 "} Foo_t;", 7263 Style); 7264 verifyFormat("struct Foo {\n" 7265 " Foo();\n" 7266 "};", 7267 Style); 7268 verifyFormat("typedef struct Foo {\n" 7269 " Foo();\n" 7270 "} Foo_t;", 7271 Style); 7272 verifyFormat("union Foo {\n" 7273 " A,\n" 7274 "};", 7275 Style); 7276 verifyFormat("typedef union Foo {\n" 7277 " A,\n" 7278 "} Foo_t;", 7279 Style); 7280 verifyFormat("namespace Foo {\n" 7281 "void Bar();\n" 7282 "};", 7283 Style); 7284 7285 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7286 Style.BraceWrapping.AfterClass = true; 7287 Style.BraceWrapping.AfterStruct = true; 7288 Style.BraceWrapping.AfterUnion = true; 7289 Style.BraceWrapping.AfterNamespace = true; 7290 verifyFormat("class Foo\n" 7291 "{\n" 7292 " Foo();\n" 7293 "};", 7294 Style); 7295 verifyFormat("typedef class Foo\n" 7296 "{\n" 7297 " Foo();\n" 7298 "} Foo_t;", 7299 Style); 7300 verifyFormat("struct Foo\n" 7301 "{\n" 7302 " Foo();\n" 7303 "};", 7304 Style); 7305 verifyFormat("typedef struct Foo\n" 7306 "{\n" 7307 " Foo();\n" 7308 "} Foo_t;", 7309 Style); 7310 verifyFormat("union Foo\n" 7311 "{\n" 7312 " A,\n" 7313 "};", 7314 Style); 7315 verifyFormat("typedef union Foo\n" 7316 "{\n" 7317 " A,\n" 7318 "} Foo_t;", 7319 Style); 7320 verifyFormat("namespace Foo\n" 7321 "{\n" 7322 "void Bar();\n" 7323 "};", 7324 Style); 7325 } 7326 7327 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 7328 // Elaborate type variable declarations. 7329 verifyFormat("struct foo a = {bar};\nint n;"); 7330 verifyFormat("class foo a = {bar};\nint n;"); 7331 verifyFormat("union foo a = {bar};\nint n;"); 7332 7333 // Elaborate types inside function definitions. 7334 verifyFormat("struct foo f() {}\nint n;"); 7335 verifyFormat("class foo f() {}\nint n;"); 7336 verifyFormat("union foo f() {}\nint n;"); 7337 7338 // Templates. 7339 verifyFormat("template <class X> void f() {}\nint n;"); 7340 verifyFormat("template <struct X> void f() {}\nint n;"); 7341 verifyFormat("template <union X> void f() {}\nint n;"); 7342 7343 // Actual definitions... 7344 verifyFormat("struct {\n} n;"); 7345 verifyFormat( 7346 "template <template <class T, class Y>, class Z> class X {\n} n;"); 7347 verifyFormat("union Z {\n int n;\n} x;"); 7348 verifyFormat("class MACRO Z {\n} n;"); 7349 verifyFormat("class MACRO(X) Z {\n} n;"); 7350 verifyFormat("class __attribute__(X) Z {\n} n;"); 7351 verifyFormat("class __declspec(X) Z {\n} n;"); 7352 verifyFormat("class A##B##C {\n} n;"); 7353 verifyFormat("class alignas(16) Z {\n} n;"); 7354 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;"); 7355 verifyFormat("class MACROA MACRO(X) Z {\n} n;"); 7356 7357 // Redefinition from nested context: 7358 verifyFormat("class A::B::C {\n} n;"); 7359 7360 // Template definitions. 7361 verifyFormat( 7362 "template <typename F>\n" 7363 "Matcher(const Matcher<F> &Other,\n" 7364 " typename enable_if_c<is_base_of<F, T>::value &&\n" 7365 " !is_same<F, T>::value>::type * = 0)\n" 7366 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}"); 7367 7368 // FIXME: This is still incorrectly handled at the formatter side. 7369 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};"); 7370 verifyFormat("int i = SomeFunction(a<b, a> b);"); 7371 7372 // FIXME: 7373 // This now gets parsed incorrectly as class definition. 7374 // verifyFormat("class A<int> f() {\n}\nint n;"); 7375 7376 // Elaborate types where incorrectly parsing the structural element would 7377 // break the indent. 7378 verifyFormat("if (true)\n" 7379 " class X x;\n" 7380 "else\n" 7381 " f();\n"); 7382 7383 // This is simply incomplete. Formatting is not important, but must not crash. 7384 verifyFormat("class A:"); 7385 } 7386 7387 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 7388 EXPECT_EQ("#error Leave all white!!!!! space* alone!\n", 7389 format("#error Leave all white!!!!! space* alone!\n")); 7390 EXPECT_EQ( 7391 "#warning Leave all white!!!!! space* alone!\n", 7392 format("#warning Leave all white!!!!! space* alone!\n")); 7393 EXPECT_EQ("#error 1", format(" # error 1")); 7394 EXPECT_EQ("#warning 1", format(" # warning 1")); 7395 } 7396 7397 TEST_F(FormatTest, FormatHashIfExpressions) { 7398 verifyFormat("#if AAAA && BBBB"); 7399 verifyFormat("#if (AAAA && BBBB)"); 7400 verifyFormat("#elif (AAAA && BBBB)"); 7401 // FIXME: Come up with a better indentation for #elif. 7402 verifyFormat( 7403 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" 7404 " defined(BBBBBBBB)\n" 7405 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" 7406 " defined(BBBBBBBB)\n" 7407 "#endif", 7408 getLLVMStyleWithColumns(65)); 7409 } 7410 7411 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 7412 FormatStyle AllowsMergedIf = getGoogleStyle(); 7413 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 7414 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 7415 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 7416 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 7417 EXPECT_EQ("if (true) return 42;", 7418 format("if (true)\nreturn 42;", AllowsMergedIf)); 7419 FormatStyle ShortMergedIf = AllowsMergedIf; 7420 ShortMergedIf.ColumnLimit = 25; 7421 verifyFormat("#define A \\\n" 7422 " if (true) return 42;", 7423 ShortMergedIf); 7424 verifyFormat("#define A \\\n" 7425 " f(); \\\n" 7426 " if (true)\n" 7427 "#define B", 7428 ShortMergedIf); 7429 verifyFormat("#define A \\\n" 7430 " f(); \\\n" 7431 " if (true)\n" 7432 "g();", 7433 ShortMergedIf); 7434 verifyFormat("{\n" 7435 "#ifdef A\n" 7436 " // Comment\n" 7437 " if (true) continue;\n" 7438 "#endif\n" 7439 " // Comment\n" 7440 " if (true) continue;\n" 7441 "}", 7442 ShortMergedIf); 7443 ShortMergedIf.ColumnLimit = 33; 7444 verifyFormat("#define A \\\n" 7445 " if constexpr (true) return 42;", 7446 ShortMergedIf); 7447 ShortMergedIf.ColumnLimit = 29; 7448 verifyFormat("#define A \\\n" 7449 " if (aaaaaaaaaa) return 1; \\\n" 7450 " return 2;", 7451 ShortMergedIf); 7452 ShortMergedIf.ColumnLimit = 28; 7453 verifyFormat("#define A \\\n" 7454 " if (aaaaaaaaaa) \\\n" 7455 " return 1; \\\n" 7456 " return 2;", 7457 ShortMergedIf); 7458 verifyFormat("#define A \\\n" 7459 " if constexpr (aaaaaaa) \\\n" 7460 " return 1; \\\n" 7461 " return 2;", 7462 ShortMergedIf); 7463 } 7464 7465 TEST_F(FormatTest, FormatStarDependingOnContext) { 7466 verifyFormat("void f(int *a);"); 7467 verifyFormat("void f() { f(fint * b); }"); 7468 verifyFormat("class A {\n void f(int *a);\n};"); 7469 verifyFormat("class A {\n int *a;\n};"); 7470 verifyFormat("namespace a {\n" 7471 "namespace b {\n" 7472 "class A {\n" 7473 " void f() {}\n" 7474 " int *a;\n" 7475 "};\n" 7476 "} // namespace b\n" 7477 "} // namespace a"); 7478 } 7479 7480 TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 7481 verifyFormat("while"); 7482 verifyFormat("operator"); 7483 } 7484 7485 TEST_F(FormatTest, SkipsDeeplyNestedLines) { 7486 // This code would be painfully slow to format if we didn't skip it. 7487 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 7488 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 7489 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 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(1, 1)\n" 7493 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x 7494 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7495 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 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 // Deeply nested part is untouched, rest is formatted. 7504 EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n", 7505 format(std::string("int i;\n") + Code + "int j;\n", 7506 getLLVMStyle(), SC_ExpectIncomplete)); 7507 } 7508 7509 //===----------------------------------------------------------------------===// 7510 // Objective-C tests. 7511 //===----------------------------------------------------------------------===// 7512 7513 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 7514 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 7515 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 7516 format("-(NSUInteger)indexOfObject:(id)anObject;")); 7517 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 7518 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 7519 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 7520 format("-(NSInteger)Method3:(id)anObject;")); 7521 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 7522 format("-(NSInteger)Method4:(id)anObject;")); 7523 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 7524 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 7525 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 7526 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 7527 EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject " 7528 "forAllCells:(BOOL)flag;", 7529 format("- (void)sendAction:(SEL)aSelector to:(id)anObject " 7530 "forAllCells:(BOOL)flag;")); 7531 7532 // Very long objectiveC method declaration. 7533 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 7534 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;"); 7535 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 7536 " inRange:(NSRange)range\n" 7537 " outRange:(NSRange)out_range\n" 7538 " outRange1:(NSRange)out_range1\n" 7539 " outRange2:(NSRange)out_range2\n" 7540 " outRange3:(NSRange)out_range3\n" 7541 " outRange4:(NSRange)out_range4\n" 7542 " outRange5:(NSRange)out_range5\n" 7543 " outRange6:(NSRange)out_range6\n" 7544 " outRange7:(NSRange)out_range7\n" 7545 " outRange8:(NSRange)out_range8\n" 7546 " outRange9:(NSRange)out_range9;"); 7547 7548 // When the function name has to be wrapped. 7549 FormatStyle Style = getLLVMStyle(); 7550 Style.IndentWrappedFunctionNames = false; 7551 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 7552 "veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 7553 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 7554 "}", 7555 Style); 7556 Style.IndentWrappedFunctionNames = true; 7557 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 7558 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 7559 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 7560 "}", 7561 Style); 7562 7563 verifyFormat("- (int)sum:(vector<int>)numbers;"); 7564 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 7565 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 7566 // protocol lists (but not for template classes): 7567 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 7568 7569 verifyFormat("- (int (*)())foo:(int (*)())f;"); 7570 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); 7571 7572 // If there's no return type (very rare in practice!), LLVM and Google style 7573 // agree. 7574 verifyFormat("- foo;"); 7575 verifyFormat("- foo:(int)f;"); 7576 verifyGoogleFormat("- foo:(int)foo;"); 7577 } 7578 7579 7580 TEST_F(FormatTest, BreaksStringLiterals) { 7581 EXPECT_EQ("\"some text \"\n" 7582 "\"other\";", 7583 format("\"some text other\";", getLLVMStyleWithColumns(12))); 7584 EXPECT_EQ("\"some text \"\n" 7585 "\"other\";", 7586 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); 7587 EXPECT_EQ( 7588 "#define A \\\n" 7589 " \"some \" \\\n" 7590 " \"text \" \\\n" 7591 " \"other\";", 7592 format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); 7593 EXPECT_EQ( 7594 "#define A \\\n" 7595 " \"so \" \\\n" 7596 " \"text \" \\\n" 7597 " \"other\";", 7598 format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); 7599 7600 EXPECT_EQ("\"some text\"", 7601 format("\"some text\"", getLLVMStyleWithColumns(1))); 7602 EXPECT_EQ("\"some text\"", 7603 format("\"some text\"", getLLVMStyleWithColumns(11))); 7604 EXPECT_EQ("\"some \"\n" 7605 "\"text\"", 7606 format("\"some text\"", getLLVMStyleWithColumns(10))); 7607 EXPECT_EQ("\"some \"\n" 7608 "\"text\"", 7609 format("\"some text\"", getLLVMStyleWithColumns(7))); 7610 EXPECT_EQ("\"some\"\n" 7611 "\" tex\"\n" 7612 "\"t\"", 7613 format("\"some text\"", getLLVMStyleWithColumns(6))); 7614 EXPECT_EQ("\"some\"\n" 7615 "\" tex\"\n" 7616 "\" and\"", 7617 format("\"some tex and\"", getLLVMStyleWithColumns(6))); 7618 EXPECT_EQ("\"some\"\n" 7619 "\"/tex\"\n" 7620 "\"/and\"", 7621 format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); 7622 7623 EXPECT_EQ("variable =\n" 7624 " \"long string \"\n" 7625 " \"literal\";", 7626 format("variable = \"long string literal\";", 7627 getLLVMStyleWithColumns(20))); 7628 7629 EXPECT_EQ("variable = f(\n" 7630 " \"long string \"\n" 7631 " \"literal\",\n" 7632 " short,\n" 7633 " loooooooooooooooooooong);", 7634 format("variable = f(\"long string literal\", short, " 7635 "loooooooooooooooooooong);", 7636 getLLVMStyleWithColumns(20))); 7637 7638 EXPECT_EQ( 7639 "f(g(\"long string \"\n" 7640 " \"literal\"),\n" 7641 " b);", 7642 format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20))); 7643 EXPECT_EQ("f(g(\"long string \"\n" 7644 " \"literal\",\n" 7645 " a),\n" 7646 " b);", 7647 format("f(g(\"long string literal\", a), b);", 7648 getLLVMStyleWithColumns(20))); 7649 EXPECT_EQ( 7650 "f(\"one two\".split(\n" 7651 " variable));", 7652 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); 7653 EXPECT_EQ("f(\"one two three four five six \"\n" 7654 " \"seven\".split(\n" 7655 " really_looooong_variable));", 7656 format("f(\"one two three four five six seven\"." 7657 "split(really_looooong_variable));", 7658 getLLVMStyleWithColumns(33))); 7659 7660 EXPECT_EQ("f(\"some \"\n" 7661 " \"text\",\n" 7662 " other);", 7663 format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); 7664 7665 // Only break as a last resort. 7666 verifyFormat( 7667 "aaaaaaaaaaaaaaaaaaaa(\n" 7668 " aaaaaaaaaaaaaaaaaaaa,\n" 7669 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); 7670 7671 EXPECT_EQ("\"splitmea\"\n" 7672 "\"trandomp\"\n" 7673 "\"oint\"", 7674 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); 7675 7676 EXPECT_EQ("\"split/\"\n" 7677 "\"pathat/\"\n" 7678 "\"slashes\"", 7679 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 7680 7681 EXPECT_EQ("\"split/\"\n" 7682 "\"pathat/\"\n" 7683 "\"slashes\"", 7684 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 7685 EXPECT_EQ("\"split at \"\n" 7686 "\"spaces/at/\"\n" 7687 "\"slashes.at.any$\"\n" 7688 "\"non-alphanumeric%\"\n" 7689 "\"1111111111characte\"\n" 7690 "\"rs\"", 7691 format("\"split at " 7692 "spaces/at/" 7693 "slashes.at." 7694 "any$non-" 7695 "alphanumeric%" 7696 "1111111111characte" 7697 "rs\"", 7698 getLLVMStyleWithColumns(20))); 7699 7700 // Verify that splitting the strings understands 7701 // Style::AlwaysBreakBeforeMultilineStrings. 7702 EXPECT_EQ( 7703 "aaaaaaaaaaaa(\n" 7704 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" 7705 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", 7706 format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa " 7707 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 7708 "aaaaaaaaaaaaaaaaaaaaaa\");", 7709 getGoogleStyle())); 7710 EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7711 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", 7712 format("return \"aaaaaaaaaaaaaaaaaaaaaa " 7713 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 7714 "aaaaaaaaaaaaaaaaaaaaaa\";", 7715 getGoogleStyle())); 7716 EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7717 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 7718 format("llvm::outs() << " 7719 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa" 7720 "aaaaaaaaaaaaaaaaaaa\";")); 7721 EXPECT_EQ("ffff(\n" 7722 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7723 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 7724 format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " 7725 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 7726 getGoogleStyle())); 7727 7728 FormatStyle Style = getLLVMStyleWithColumns(12); 7729 Style.BreakStringLiterals = false; 7730 EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style)); 7731 7732 FormatStyle AlignLeft = getLLVMStyleWithColumns(12); 7733 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 7734 EXPECT_EQ("#define A \\\n" 7735 " \"some \" \\\n" 7736 " \"text \" \\\n" 7737 " \"other\";", 7738 format("#define A \"some text other\";", AlignLeft)); 7739 } 7740 7741 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) { 7742 EXPECT_EQ("C a = \"some more \"\n" 7743 " \"text\";", 7744 format("C a = \"some more text\";", getLLVMStyleWithColumns(18))); 7745 } 7746 7747 TEST_F(FormatTest, FullyRemoveEmptyLines) { 7748 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80); 7749 NoEmptyLines.MaxEmptyLinesToKeep = 0; 7750 EXPECT_EQ("int i = a(b());", 7751 format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines)); 7752 } 7753 7754 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) { 7755 EXPECT_EQ( 7756 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 7757 "(\n" 7758 " \"x\t\");", 7759 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 7760 "aaaaaaa(" 7761 "\"x\t\");")); 7762 } 7763 7764 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) { 7765 EXPECT_EQ( 7766 "u8\"utf8 string \"\n" 7767 "u8\"literal\";", 7768 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16))); 7769 EXPECT_EQ( 7770 "u\"utf16 string \"\n" 7771 "u\"literal\";", 7772 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16))); 7773 EXPECT_EQ( 7774 "U\"utf32 string \"\n" 7775 "U\"literal\";", 7776 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16))); 7777 EXPECT_EQ("L\"wide string \"\n" 7778 "L\"literal\";", 7779 format("L\"wide string literal\";", getGoogleStyleWithColumns(16))); 7780 EXPECT_EQ("@\"NSString \"\n" 7781 "@\"literal\";", 7782 format("@\"NSString literal\";", getGoogleStyleWithColumns(19))); 7783 verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26)); 7784 7785 // This input makes clang-format try to split the incomplete unicode escape 7786 // sequence, which used to lead to a crasher. 7787 verifyNoCrash( 7788 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 7789 getLLVMStyleWithColumns(60)); 7790 } 7791 7792 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) { 7793 FormatStyle Style = getGoogleStyleWithColumns(15); 7794 EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style)); 7795 EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style)); 7796 EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style)); 7797 EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style)); 7798 EXPECT_EQ("u8R\"x(raw literal)x\";", 7799 format("u8R\"x(raw literal)x\";", Style)); 7800 } 7801 7802 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { 7803 FormatStyle Style = getLLVMStyleWithColumns(20); 7804 EXPECT_EQ( 7805 "_T(\"aaaaaaaaaaaaaa\")\n" 7806 "_T(\"aaaaaaaaaaaaaa\")\n" 7807 "_T(\"aaaaaaaaaaaa\")", 7808 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style)); 7809 EXPECT_EQ("f(x,\n" 7810 " _T(\"aaaaaaaaaaaa\")\n" 7811 " _T(\"aaa\"),\n" 7812 " z);", 7813 format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style)); 7814 7815 // FIXME: Handle embedded spaces in one iteration. 7816 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n" 7817 // "_T(\"aaaaaaaaaaaaa\")\n" 7818 // "_T(\"aaaaaaaaaaaaa\")\n" 7819 // "_T(\"a\")", 7820 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 7821 // getLLVMStyleWithColumns(20))); 7822 EXPECT_EQ( 7823 "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 7824 format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style)); 7825 EXPECT_EQ("f(\n" 7826 "#if !TEST\n" 7827 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 7828 "#endif\n" 7829 ");", 7830 format("f(\n" 7831 "#if !TEST\n" 7832 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 7833 "#endif\n" 7834 ");")); 7835 EXPECT_EQ("f(\n" 7836 "\n" 7837 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));", 7838 format("f(\n" 7839 "\n" 7840 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));")); 7841 } 7842 7843 TEST_F(FormatTest, BreaksStringLiteralOperands) { 7844 // In a function call with two operands, the second can be broken with no line 7845 // break before it. 7846 EXPECT_EQ("func(a, \"long long \"\n" 7847 " \"long long\");", 7848 format("func(a, \"long long long long\");", 7849 getLLVMStyleWithColumns(24))); 7850 // In a function call with three operands, the second must be broken with a 7851 // line break before it. 7852 EXPECT_EQ("func(a,\n" 7853 " \"long long long \"\n" 7854 " \"long\",\n" 7855 " c);", 7856 format("func(a, \"long long long long\", c);", 7857 getLLVMStyleWithColumns(24))); 7858 // In a function call with three operands, the third must be broken with a 7859 // line break before it. 7860 EXPECT_EQ("func(a, b,\n" 7861 " \"long long long \"\n" 7862 " \"long\");", 7863 format("func(a, b, \"long long long long\");", 7864 getLLVMStyleWithColumns(24))); 7865 // In a function call with three operands, both the second and the third must 7866 // be broken with a line break before them. 7867 EXPECT_EQ("func(a,\n" 7868 " \"long long long \"\n" 7869 " \"long\",\n" 7870 " \"long long long \"\n" 7871 " \"long\");", 7872 format("func(a, \"long long long long\", \"long long long long\");", 7873 getLLVMStyleWithColumns(24))); 7874 // In a chain of << with two operands, the second can be broken with no line 7875 // break before it. 7876 EXPECT_EQ("a << \"line line \"\n" 7877 " \"line\";", 7878 format("a << \"line line line\";", 7879 getLLVMStyleWithColumns(20))); 7880 // In a chain of << with three operands, the second can be broken with no line 7881 // break before it. 7882 EXPECT_EQ("abcde << \"line \"\n" 7883 " \"line line\"\n" 7884 " << c;", 7885 format("abcde << \"line line line\" << c;", 7886 getLLVMStyleWithColumns(20))); 7887 // In a chain of << with three operands, the third must be broken with a line 7888 // break before it. 7889 EXPECT_EQ("a << b\n" 7890 " << \"line line \"\n" 7891 " \"line\";", 7892 format("a << b << \"line line line\";", 7893 getLLVMStyleWithColumns(20))); 7894 // In a chain of << with three operands, the second can be broken with no line 7895 // break before it and the third must be broken with a line break before it. 7896 EXPECT_EQ("abcd << \"line line \"\n" 7897 " \"line\"\n" 7898 " << \"line line \"\n" 7899 " \"line\";", 7900 format("abcd << \"line line line\" << \"line line line\";", 7901 getLLVMStyleWithColumns(20))); 7902 // In a chain of binary operators with two operands, the second can be broken 7903 // with no line break before it. 7904 EXPECT_EQ("abcd + \"line line \"\n" 7905 " \"line line\";", 7906 format("abcd + \"line line line line\";", 7907 getLLVMStyleWithColumns(20))); 7908 // In a chain of binary operators with three operands, the second must be 7909 // broken with a line break before it. 7910 EXPECT_EQ("abcd +\n" 7911 " \"line line \"\n" 7912 " \"line line\" +\n" 7913 " e;", 7914 format("abcd + \"line line line line\" + e;", 7915 getLLVMStyleWithColumns(20))); 7916 // In a function call with two operands, with AlignAfterOpenBracket enabled, 7917 // the first must be broken with a line break before it. 7918 FormatStyle Style = getLLVMStyleWithColumns(25); 7919 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 7920 EXPECT_EQ("someFunction(\n" 7921 " \"long long long \"\n" 7922 " \"long\",\n" 7923 " a);", 7924 format("someFunction(\"long long long long\", a);", Style)); 7925 } 7926 7927 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { 7928 EXPECT_EQ( 7929 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 7930 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 7931 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 7932 format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 7933 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 7934 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";")); 7935 } 7936 7937 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { 7938 EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);", 7939 format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle())); 7940 EXPECT_EQ("fffffffffff(g(R\"x(\n" 7941 "multiline raw string literal xxxxxxxxxxxxxx\n" 7942 ")x\",\n" 7943 " a),\n" 7944 " b);", 7945 format("fffffffffff(g(R\"x(\n" 7946 "multiline raw string literal xxxxxxxxxxxxxx\n" 7947 ")x\", a), b);", 7948 getGoogleStyleWithColumns(20))); 7949 EXPECT_EQ("fffffffffff(\n" 7950 " g(R\"x(qqq\n" 7951 "multiline raw string literal xxxxxxxxxxxxxx\n" 7952 ")x\",\n" 7953 " a),\n" 7954 " b);", 7955 format("fffffffffff(g(R\"x(qqq\n" 7956 "multiline raw string literal xxxxxxxxxxxxxx\n" 7957 ")x\", a), b);", 7958 getGoogleStyleWithColumns(20))); 7959 7960 EXPECT_EQ("fffffffffff(R\"x(\n" 7961 "multiline raw string literal xxxxxxxxxxxxxx\n" 7962 ")x\");", 7963 format("fffffffffff(R\"x(\n" 7964 "multiline raw string literal xxxxxxxxxxxxxx\n" 7965 ")x\");", 7966 getGoogleStyleWithColumns(20))); 7967 EXPECT_EQ("fffffffffff(R\"x(\n" 7968 "multiline raw string literal xxxxxxxxxxxxxx\n" 7969 ")x\" + bbbbbb);", 7970 format("fffffffffff(R\"x(\n" 7971 "multiline raw string literal xxxxxxxxxxxxxx\n" 7972 ")x\" + bbbbbb);", 7973 getGoogleStyleWithColumns(20))); 7974 EXPECT_EQ("fffffffffff(\n" 7975 " R\"x(\n" 7976 "multiline raw string literal xxxxxxxxxxxxxx\n" 7977 ")x\" +\n" 7978 " bbbbbb);", 7979 format("fffffffffff(\n" 7980 " R\"x(\n" 7981 "multiline raw string literal xxxxxxxxxxxxxx\n" 7982 ")x\" + bbbbbb);", 7983 getGoogleStyleWithColumns(20))); 7984 } 7985 7986 TEST_F(FormatTest, SkipsUnknownStringLiterals) { 7987 verifyFormat("string a = \"unterminated;"); 7988 EXPECT_EQ("function(\"unterminated,\n" 7989 " OtherParameter);", 7990 format("function( \"unterminated,\n" 7991 " OtherParameter);")); 7992 } 7993 7994 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { 7995 FormatStyle Style = getLLVMStyle(); 7996 Style.Standard = FormatStyle::LS_Cpp03; 7997 EXPECT_EQ("#define x(_a) printf(\"foo\" _a);", 7998 format("#define x(_a) printf(\"foo\"_a);", Style)); 7999 } 8000 8001 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); } 8002 8003 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { 8004 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n" 8005 " \"ddeeefff\");", 8006 format("someFunction(\"aaabbbcccdddeeefff\");", 8007 getLLVMStyleWithColumns(25))); 8008 EXPECT_EQ("someFunction1234567890(\n" 8009 " \"aaabbbcccdddeeefff\");", 8010 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 8011 getLLVMStyleWithColumns(26))); 8012 EXPECT_EQ("someFunction1234567890(\n" 8013 " \"aaabbbcccdddeeeff\"\n" 8014 " \"f\");", 8015 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 8016 getLLVMStyleWithColumns(25))); 8017 EXPECT_EQ("someFunction1234567890(\n" 8018 " \"aaabbbcccdddeeeff\"\n" 8019 " \"f\");", 8020 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 8021 getLLVMStyleWithColumns(24))); 8022 EXPECT_EQ("someFunction(\n" 8023 " \"aaabbbcc ddde \"\n" 8024 " \"efff\");", 8025 format("someFunction(\"aaabbbcc ddde efff\");", 8026 getLLVMStyleWithColumns(25))); 8027 EXPECT_EQ("someFunction(\"aaabbbccc \"\n" 8028 " \"ddeeefff\");", 8029 format("someFunction(\"aaabbbccc ddeeefff\");", 8030 getLLVMStyleWithColumns(25))); 8031 EXPECT_EQ("someFunction1234567890(\n" 8032 " \"aaabb \"\n" 8033 " \"cccdddeeefff\");", 8034 format("someFunction1234567890(\"aaabb cccdddeeefff\");", 8035 getLLVMStyleWithColumns(25))); 8036 EXPECT_EQ("#define A \\\n" 8037 " string s = \\\n" 8038 " \"123456789\" \\\n" 8039 " \"0\"; \\\n" 8040 " int i;", 8041 format("#define A string s = \"1234567890\"; int i;", 8042 getLLVMStyleWithColumns(20))); 8043 EXPECT_EQ("someFunction(\n" 8044 " \"aaabbbcc \"\n" 8045 " \"dddeeefff\");", 8046 format("someFunction(\"aaabbbcc dddeeefff\");", 8047 getLLVMStyleWithColumns(25))); 8048 } 8049 8050 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { 8051 EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3))); 8052 EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2))); 8053 EXPECT_EQ("\"test\"\n" 8054 "\"\\n\"", 8055 format("\"test\\n\"", getLLVMStyleWithColumns(7))); 8056 EXPECT_EQ("\"tes\\\\\"\n" 8057 "\"n\"", 8058 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); 8059 EXPECT_EQ("\"\\\\\\\\\"\n" 8060 "\"\\n\"", 8061 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); 8062 EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7))); 8063 EXPECT_EQ("\"\\uff01\"\n" 8064 "\"test\"", 8065 format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); 8066 EXPECT_EQ("\"\\Uff01ff02\"", 8067 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); 8068 EXPECT_EQ("\"\\x000000000001\"\n" 8069 "\"next\"", 8070 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); 8071 EXPECT_EQ("\"\\x000000000001next\"", 8072 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); 8073 EXPECT_EQ("\"\\x000000000001\"", 8074 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); 8075 EXPECT_EQ("\"test\"\n" 8076 "\"\\000000\"\n" 8077 "\"000001\"", 8078 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); 8079 EXPECT_EQ("\"test\\000\"\n" 8080 "\"00000000\"\n" 8081 "\"1\"", 8082 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); 8083 } 8084 8085 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { 8086 verifyFormat("void f() {\n" 8087 " return g() {}\n" 8088 " void h() {}"); 8089 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n" 8090 "g();\n" 8091 "}"); 8092 } 8093 8094 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) { 8095 verifyFormat( 8096 "void f() { return C{param1, param2}.SomeCall(param1, param2); }"); 8097 } 8098 8099 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { 8100 verifyFormat("class X {\n" 8101 " void f() {\n" 8102 " }\n" 8103 "};", 8104 getLLVMStyleWithColumns(12)); 8105 } 8106 8107 TEST_F(FormatTest, ConfigurableIndentWidth) { 8108 FormatStyle EightIndent = getLLVMStyleWithColumns(18); 8109 EightIndent.IndentWidth = 8; 8110 EightIndent.ContinuationIndentWidth = 8; 8111 verifyFormat("void f() {\n" 8112 " someFunction();\n" 8113 " if (true) {\n" 8114 " f();\n" 8115 " }\n" 8116 "}", 8117 EightIndent); 8118 verifyFormat("class X {\n" 8119 " void f() {\n" 8120 " }\n" 8121 "};", 8122 EightIndent); 8123 verifyFormat("int x[] = {\n" 8124 " call(),\n" 8125 " call()};", 8126 EightIndent); 8127 } 8128 8129 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) { 8130 verifyFormat("double\n" 8131 "f();", 8132 getLLVMStyleWithColumns(8)); 8133 } 8134 8135 TEST_F(FormatTest, ConfigurableUseOfTab) { 8136 FormatStyle Tab = getLLVMStyleWithColumns(42); 8137 Tab.IndentWidth = 8; 8138 Tab.UseTab = FormatStyle::UT_Always; 8139 Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left; 8140 8141 EXPECT_EQ("if (aaaaaaaa && // q\n" 8142 " bb)\t\t// w\n" 8143 "\t;", 8144 format("if (aaaaaaaa &&// q\n" 8145 "bb)// w\n" 8146 ";", 8147 Tab)); 8148 EXPECT_EQ("if (aaa && bbb) // w\n" 8149 "\t;", 8150 format("if(aaa&&bbb)// w\n" 8151 ";", 8152 Tab)); 8153 8154 verifyFormat("class X {\n" 8155 "\tvoid f() {\n" 8156 "\t\tsomeFunction(parameter1,\n" 8157 "\t\t\t parameter2);\n" 8158 "\t}\n" 8159 "};", 8160 Tab); 8161 verifyFormat("#define A \\\n" 8162 "\tvoid f() { \\\n" 8163 "\t\tsomeFunction( \\\n" 8164 "\t\t parameter1, \\\n" 8165 "\t\t parameter2); \\\n" 8166 "\t}", 8167 Tab); 8168 8169 Tab.TabWidth = 4; 8170 Tab.IndentWidth = 8; 8171 verifyFormat("class TabWidth4Indent8 {\n" 8172 "\t\tvoid f() {\n" 8173 "\t\t\t\tsomeFunction(parameter1,\n" 8174 "\t\t\t\t\t\t\t parameter2);\n" 8175 "\t\t}\n" 8176 "};", 8177 Tab); 8178 8179 Tab.TabWidth = 4; 8180 Tab.IndentWidth = 4; 8181 verifyFormat("class TabWidth4Indent4 {\n" 8182 "\tvoid f() {\n" 8183 "\t\tsomeFunction(parameter1,\n" 8184 "\t\t\t\t\t parameter2);\n" 8185 "\t}\n" 8186 "};", 8187 Tab); 8188 8189 Tab.TabWidth = 8; 8190 Tab.IndentWidth = 4; 8191 verifyFormat("class TabWidth8Indent4 {\n" 8192 " void f() {\n" 8193 "\tsomeFunction(parameter1,\n" 8194 "\t\t parameter2);\n" 8195 " }\n" 8196 "};", 8197 Tab); 8198 8199 Tab.TabWidth = 8; 8200 Tab.IndentWidth = 8; 8201 EXPECT_EQ("/*\n" 8202 "\t a\t\tcomment\n" 8203 "\t in multiple lines\n" 8204 " */", 8205 format(" /*\t \t \n" 8206 " \t \t a\t\tcomment\t \t\n" 8207 " \t \t in multiple lines\t\n" 8208 " \t */", 8209 Tab)); 8210 8211 Tab.UseTab = FormatStyle::UT_ForIndentation; 8212 verifyFormat("{\n" 8213 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8214 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8215 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8216 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8217 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8218 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8219 "};", 8220 Tab); 8221 verifyFormat("enum AA {\n" 8222 "\ta1, // Force multiple lines\n" 8223 "\ta2,\n" 8224 "\ta3\n" 8225 "};", 8226 Tab); 8227 EXPECT_EQ("if (aaaaaaaa && // q\n" 8228 " bb) // w\n" 8229 "\t;", 8230 format("if (aaaaaaaa &&// q\n" 8231 "bb)// w\n" 8232 ";", 8233 Tab)); 8234 verifyFormat("class X {\n" 8235 "\tvoid f() {\n" 8236 "\t\tsomeFunction(parameter1,\n" 8237 "\t\t parameter2);\n" 8238 "\t}\n" 8239 "};", 8240 Tab); 8241 verifyFormat("{\n" 8242 "\tQ(\n" 8243 "\t {\n" 8244 "\t\t int a;\n" 8245 "\t\t someFunction(aaaaaaaa,\n" 8246 "\t\t bbbbbbb);\n" 8247 "\t },\n" 8248 "\t p);\n" 8249 "}", 8250 Tab); 8251 EXPECT_EQ("{\n" 8252 "\t/* aaaa\n" 8253 "\t bbbb */\n" 8254 "}", 8255 format("{\n" 8256 "/* aaaa\n" 8257 " bbbb */\n" 8258 "}", 8259 Tab)); 8260 EXPECT_EQ("{\n" 8261 "\t/*\n" 8262 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8263 "\t bbbbbbbbbbbbb\n" 8264 "\t*/\n" 8265 "}", 8266 format("{\n" 8267 "/*\n" 8268 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8269 "*/\n" 8270 "}", 8271 Tab)); 8272 EXPECT_EQ("{\n" 8273 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8274 "\t// bbbbbbbbbbbbb\n" 8275 "}", 8276 format("{\n" 8277 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8278 "}", 8279 Tab)); 8280 EXPECT_EQ("{\n" 8281 "\t/*\n" 8282 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8283 "\t bbbbbbbbbbbbb\n" 8284 "\t*/\n" 8285 "}", 8286 format("{\n" 8287 "\t/*\n" 8288 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8289 "\t*/\n" 8290 "}", 8291 Tab)); 8292 EXPECT_EQ("{\n" 8293 "\t/*\n" 8294 "\n" 8295 "\t*/\n" 8296 "}", 8297 format("{\n" 8298 "\t/*\n" 8299 "\n" 8300 "\t*/\n" 8301 "}", 8302 Tab)); 8303 EXPECT_EQ("{\n" 8304 "\t/*\n" 8305 " asdf\n" 8306 "\t*/\n" 8307 "}", 8308 format("{\n" 8309 "\t/*\n" 8310 " asdf\n" 8311 "\t*/\n" 8312 "}", 8313 Tab)); 8314 8315 Tab.UseTab = FormatStyle::UT_Never; 8316 EXPECT_EQ("/*\n" 8317 " a\t\tcomment\n" 8318 " in multiple lines\n" 8319 " */", 8320 format(" /*\t \t \n" 8321 " \t \t a\t\tcomment\t \t\n" 8322 " \t \t in multiple lines\t\n" 8323 " \t */", 8324 Tab)); 8325 EXPECT_EQ("/* some\n" 8326 " comment */", 8327 format(" \t \t /* some\n" 8328 " \t \t comment */", 8329 Tab)); 8330 EXPECT_EQ("int a; /* some\n" 8331 " comment */", 8332 format(" \t \t int a; /* some\n" 8333 " \t \t comment */", 8334 Tab)); 8335 8336 EXPECT_EQ("int a; /* some\n" 8337 "comment */", 8338 format(" \t \t int\ta; /* some\n" 8339 " \t \t comment */", 8340 Tab)); 8341 EXPECT_EQ("f(\"\t\t\"); /* some\n" 8342 " comment */", 8343 format(" \t \t f(\"\t\t\"); /* some\n" 8344 " \t \t comment */", 8345 Tab)); 8346 EXPECT_EQ("{\n" 8347 " /*\n" 8348 " * Comment\n" 8349 " */\n" 8350 " int i;\n" 8351 "}", 8352 format("{\n" 8353 "\t/*\n" 8354 "\t * Comment\n" 8355 "\t */\n" 8356 "\t int i;\n" 8357 "}")); 8358 8359 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 8360 Tab.TabWidth = 8; 8361 Tab.IndentWidth = 8; 8362 EXPECT_EQ("if (aaaaaaaa && // q\n" 8363 " bb) // w\n" 8364 "\t;", 8365 format("if (aaaaaaaa &&// q\n" 8366 "bb)// w\n" 8367 ";", 8368 Tab)); 8369 EXPECT_EQ("if (aaa && bbb) // w\n" 8370 "\t;", 8371 format("if(aaa&&bbb)// w\n" 8372 ";", 8373 Tab)); 8374 verifyFormat("class X {\n" 8375 "\tvoid f() {\n" 8376 "\t\tsomeFunction(parameter1,\n" 8377 "\t\t\t parameter2);\n" 8378 "\t}\n" 8379 "};", 8380 Tab); 8381 verifyFormat("#define A \\\n" 8382 "\tvoid f() { \\\n" 8383 "\t\tsomeFunction( \\\n" 8384 "\t\t parameter1, \\\n" 8385 "\t\t parameter2); \\\n" 8386 "\t}", 8387 Tab); 8388 Tab.TabWidth = 4; 8389 Tab.IndentWidth = 8; 8390 verifyFormat("class TabWidth4Indent8 {\n" 8391 "\t\tvoid f() {\n" 8392 "\t\t\t\tsomeFunction(parameter1,\n" 8393 "\t\t\t\t\t\t\t parameter2);\n" 8394 "\t\t}\n" 8395 "};", 8396 Tab); 8397 Tab.TabWidth = 4; 8398 Tab.IndentWidth = 4; 8399 verifyFormat("class TabWidth4Indent4 {\n" 8400 "\tvoid f() {\n" 8401 "\t\tsomeFunction(parameter1,\n" 8402 "\t\t\t\t\t parameter2);\n" 8403 "\t}\n" 8404 "};", 8405 Tab); 8406 Tab.TabWidth = 8; 8407 Tab.IndentWidth = 4; 8408 verifyFormat("class TabWidth8Indent4 {\n" 8409 " void f() {\n" 8410 "\tsomeFunction(parameter1,\n" 8411 "\t\t parameter2);\n" 8412 " }\n" 8413 "};", 8414 Tab); 8415 Tab.TabWidth = 8; 8416 Tab.IndentWidth = 8; 8417 EXPECT_EQ("/*\n" 8418 "\t a\t\tcomment\n" 8419 "\t in multiple lines\n" 8420 " */", 8421 format(" /*\t \t \n" 8422 " \t \t a\t\tcomment\t \t\n" 8423 " \t \t in multiple lines\t\n" 8424 " \t */", 8425 Tab)); 8426 verifyFormat("{\n" 8427 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8428 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8429 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8430 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8431 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8432 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8433 "};", 8434 Tab); 8435 verifyFormat("enum AA {\n" 8436 "\ta1, // Force multiple lines\n" 8437 "\ta2,\n" 8438 "\ta3\n" 8439 "};", 8440 Tab); 8441 EXPECT_EQ("if (aaaaaaaa && // q\n" 8442 " bb) // w\n" 8443 "\t;", 8444 format("if (aaaaaaaa &&// q\n" 8445 "bb)// w\n" 8446 ";", 8447 Tab)); 8448 verifyFormat("class X {\n" 8449 "\tvoid f() {\n" 8450 "\t\tsomeFunction(parameter1,\n" 8451 "\t\t\t parameter2);\n" 8452 "\t}\n" 8453 "};", 8454 Tab); 8455 verifyFormat("{\n" 8456 "\tQ(\n" 8457 "\t {\n" 8458 "\t\t int a;\n" 8459 "\t\t someFunction(aaaaaaaa,\n" 8460 "\t\t\t\t bbbbbbb);\n" 8461 "\t },\n" 8462 "\t p);\n" 8463 "}", 8464 Tab); 8465 EXPECT_EQ("{\n" 8466 "\t/* aaaa\n" 8467 "\t bbbb */\n" 8468 "}", 8469 format("{\n" 8470 "/* aaaa\n" 8471 " bbbb */\n" 8472 "}", 8473 Tab)); 8474 EXPECT_EQ("{\n" 8475 "\t/*\n" 8476 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8477 "\t bbbbbbbbbbbbb\n" 8478 "\t*/\n" 8479 "}", 8480 format("{\n" 8481 "/*\n" 8482 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8483 "*/\n" 8484 "}", 8485 Tab)); 8486 EXPECT_EQ("{\n" 8487 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8488 "\t// bbbbbbbbbbbbb\n" 8489 "}", 8490 format("{\n" 8491 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8492 "}", 8493 Tab)); 8494 EXPECT_EQ("{\n" 8495 "\t/*\n" 8496 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8497 "\t bbbbbbbbbbbbb\n" 8498 "\t*/\n" 8499 "}", 8500 format("{\n" 8501 "\t/*\n" 8502 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8503 "\t*/\n" 8504 "}", 8505 Tab)); 8506 EXPECT_EQ("{\n" 8507 "\t/*\n" 8508 "\n" 8509 "\t*/\n" 8510 "}", 8511 format("{\n" 8512 "\t/*\n" 8513 "\n" 8514 "\t*/\n" 8515 "}", 8516 Tab)); 8517 EXPECT_EQ("{\n" 8518 "\t/*\n" 8519 " asdf\n" 8520 "\t*/\n" 8521 "}", 8522 format("{\n" 8523 "\t/*\n" 8524 " asdf\n" 8525 "\t*/\n" 8526 "}", 8527 Tab)); 8528 EXPECT_EQ("/*\n" 8529 "\t a\t\tcomment\n" 8530 "\t in multiple lines\n" 8531 " */", 8532 format(" /*\t \t \n" 8533 " \t \t a\t\tcomment\t \t\n" 8534 " \t \t in multiple lines\t\n" 8535 " \t */", 8536 Tab)); 8537 EXPECT_EQ("/* some\n" 8538 " comment */", 8539 format(" \t \t /* some\n" 8540 " \t \t comment */", 8541 Tab)); 8542 EXPECT_EQ("int a; /* some\n" 8543 " comment */", 8544 format(" \t \t int a; /* some\n" 8545 " \t \t comment */", 8546 Tab)); 8547 EXPECT_EQ("int a; /* some\n" 8548 "comment */", 8549 format(" \t \t int\ta; /* some\n" 8550 " \t \t comment */", 8551 Tab)); 8552 EXPECT_EQ("f(\"\t\t\"); /* some\n" 8553 " comment */", 8554 format(" \t \t f(\"\t\t\"); /* some\n" 8555 " \t \t comment */", 8556 Tab)); 8557 EXPECT_EQ("{\n" 8558 " /*\n" 8559 " * Comment\n" 8560 " */\n" 8561 " int i;\n" 8562 "}", 8563 format("{\n" 8564 "\t/*\n" 8565 "\t * Comment\n" 8566 "\t */\n" 8567 "\t int i;\n" 8568 "}")); 8569 Tab.AlignConsecutiveAssignments = true; 8570 Tab.AlignConsecutiveDeclarations = true; 8571 Tab.TabWidth = 4; 8572 Tab.IndentWidth = 4; 8573 verifyFormat("class Assign {\n" 8574 "\tvoid f() {\n" 8575 "\t\tint x = 123;\n" 8576 "\t\tint random = 4;\n" 8577 "\t\tstd::string alphabet =\n" 8578 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 8579 "\t}\n" 8580 "};", 8581 Tab); 8582 } 8583 8584 TEST_F(FormatTest, CalculatesOriginalColumn) { 8585 EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8586 "q\"; /* some\n" 8587 " comment */", 8588 format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8589 "q\"; /* some\n" 8590 " comment */", 8591 getLLVMStyle())); 8592 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 8593 "/* some\n" 8594 " comment */", 8595 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 8596 " /* some\n" 8597 " comment */", 8598 getLLVMStyle())); 8599 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8600 "qqq\n" 8601 "/* some\n" 8602 " comment */", 8603 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8604 "qqq\n" 8605 " /* some\n" 8606 " comment */", 8607 getLLVMStyle())); 8608 EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8609 "wwww; /* some\n" 8610 " comment */", 8611 format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8612 "wwww; /* some\n" 8613 " comment */", 8614 getLLVMStyle())); 8615 } 8616 8617 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { 8618 FormatStyle NoSpace = getLLVMStyle(); 8619 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never; 8620 8621 verifyFormat("while(true)\n" 8622 " continue;", 8623 NoSpace); 8624 verifyFormat("for(;;)\n" 8625 " continue;", 8626 NoSpace); 8627 verifyFormat("if(true)\n" 8628 " f();\n" 8629 "else if(true)\n" 8630 " f();", 8631 NoSpace); 8632 verifyFormat("do {\n" 8633 " do_something();\n" 8634 "} while(something());", 8635 NoSpace); 8636 verifyFormat("switch(x) {\n" 8637 "default:\n" 8638 " break;\n" 8639 "}", 8640 NoSpace); 8641 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace); 8642 verifyFormat("size_t x = sizeof(x);", NoSpace); 8643 verifyFormat("auto f(int x) -> decltype(x);", NoSpace); 8644 verifyFormat("int f(T x) noexcept(x.create());", NoSpace); 8645 verifyFormat("alignas(128) char a[128];", NoSpace); 8646 verifyFormat("size_t x = alignof(MyType);", NoSpace); 8647 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace); 8648 verifyFormat("int f() throw(Deprecated);", NoSpace); 8649 verifyFormat("typedef void (*cb)(int);", NoSpace); 8650 verifyFormat("T A::operator()();", NoSpace); 8651 verifyFormat("X A::operator++(T);", NoSpace); 8652 8653 FormatStyle Space = getLLVMStyle(); 8654 Space.SpaceBeforeParens = FormatStyle::SBPO_Always; 8655 8656 verifyFormat("int f ();", Space); 8657 verifyFormat("void f (int a, T b) {\n" 8658 " while (true)\n" 8659 " continue;\n" 8660 "}", 8661 Space); 8662 verifyFormat("if (true)\n" 8663 " f ();\n" 8664 "else if (true)\n" 8665 " f ();", 8666 Space); 8667 verifyFormat("do {\n" 8668 " do_something ();\n" 8669 "} while (something ());", 8670 Space); 8671 verifyFormat("switch (x) {\n" 8672 "default:\n" 8673 " break;\n" 8674 "}", 8675 Space); 8676 verifyFormat("A::A () : a (1) {}", Space); 8677 verifyFormat("void f () __attribute__ ((asdf));", Space); 8678 verifyFormat("*(&a + 1);\n" 8679 "&((&a)[1]);\n" 8680 "a[(b + c) * d];\n" 8681 "(((a + 1) * 2) + 3) * 4;", 8682 Space); 8683 verifyFormat("#define A(x) x", Space); 8684 verifyFormat("#define A (x) x", Space); 8685 verifyFormat("#if defined(x)\n" 8686 "#endif", 8687 Space); 8688 verifyFormat("auto i = std::make_unique<int> (5);", Space); 8689 verifyFormat("size_t x = sizeof (x);", Space); 8690 verifyFormat("auto f (int x) -> decltype (x);", Space); 8691 verifyFormat("int f (T x) noexcept (x.create ());", Space); 8692 verifyFormat("alignas (128) char a[128];", Space); 8693 verifyFormat("size_t x = alignof (MyType);", Space); 8694 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space); 8695 verifyFormat("int f () throw (Deprecated);", Space); 8696 verifyFormat("typedef void (*cb) (int);", Space); 8697 verifyFormat("T A::operator() ();", Space); 8698 verifyFormat("X A::operator++ (T);", Space); 8699 } 8700 8701 TEST_F(FormatTest, ConfigurableSpacesInParentheses) { 8702 FormatStyle Spaces = getLLVMStyle(); 8703 8704 Spaces.SpacesInParentheses = true; 8705 verifyFormat("call( x, y, z );", Spaces); 8706 verifyFormat("call();", Spaces); 8707 verifyFormat("std::function<void( int, int )> callback;", Spaces); 8708 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }", 8709 Spaces); 8710 verifyFormat("while ( (bool)1 )\n" 8711 " continue;", 8712 Spaces); 8713 verifyFormat("for ( ;; )\n" 8714 " continue;", 8715 Spaces); 8716 verifyFormat("if ( true )\n" 8717 " f();\n" 8718 "else if ( true )\n" 8719 " f();", 8720 Spaces); 8721 verifyFormat("do {\n" 8722 " do_something( (int)i );\n" 8723 "} while ( something() );", 8724 Spaces); 8725 verifyFormat("switch ( x ) {\n" 8726 "default:\n" 8727 " break;\n" 8728 "}", 8729 Spaces); 8730 8731 Spaces.SpacesInParentheses = false; 8732 Spaces.SpacesInCStyleCastParentheses = true; 8733 verifyFormat("Type *A = ( Type * )P;", Spaces); 8734 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces); 8735 verifyFormat("x = ( int32 )y;", Spaces); 8736 verifyFormat("int a = ( int )(2.0f);", Spaces); 8737 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces); 8738 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces); 8739 verifyFormat("#define x (( int )-1)", Spaces); 8740 8741 // Run the first set of tests again with: 8742 Spaces.SpacesInParentheses = false; 8743 Spaces.SpaceInEmptyParentheses = true; 8744 Spaces.SpacesInCStyleCastParentheses = true; 8745 verifyFormat("call(x, y, z);", Spaces); 8746 verifyFormat("call( );", Spaces); 8747 verifyFormat("std::function<void(int, int)> callback;", Spaces); 8748 verifyFormat("while (( bool )1)\n" 8749 " continue;", 8750 Spaces); 8751 verifyFormat("for (;;)\n" 8752 " continue;", 8753 Spaces); 8754 verifyFormat("if (true)\n" 8755 " f( );\n" 8756 "else if (true)\n" 8757 " f( );", 8758 Spaces); 8759 verifyFormat("do {\n" 8760 " do_something(( int )i);\n" 8761 "} while (something( ));", 8762 Spaces); 8763 verifyFormat("switch (x) {\n" 8764 "default:\n" 8765 " break;\n" 8766 "}", 8767 Spaces); 8768 8769 // Run the first set of tests again with: 8770 Spaces.SpaceAfterCStyleCast = true; 8771 verifyFormat("call(x, y, z);", Spaces); 8772 verifyFormat("call( );", Spaces); 8773 verifyFormat("std::function<void(int, int)> callback;", Spaces); 8774 verifyFormat("while (( bool ) 1)\n" 8775 " continue;", 8776 Spaces); 8777 verifyFormat("for (;;)\n" 8778 " continue;", 8779 Spaces); 8780 verifyFormat("if (true)\n" 8781 " f( );\n" 8782 "else if (true)\n" 8783 " f( );", 8784 Spaces); 8785 verifyFormat("do {\n" 8786 " do_something(( int ) i);\n" 8787 "} while (something( ));", 8788 Spaces); 8789 verifyFormat("switch (x) {\n" 8790 "default:\n" 8791 " break;\n" 8792 "}", 8793 Spaces); 8794 8795 // Run subset of tests again with: 8796 Spaces.SpacesInCStyleCastParentheses = false; 8797 Spaces.SpaceAfterCStyleCast = true; 8798 verifyFormat("while ((bool) 1)\n" 8799 " continue;", 8800 Spaces); 8801 verifyFormat("do {\n" 8802 " do_something((int) i);\n" 8803 "} while (something( ));", 8804 Spaces); 8805 } 8806 8807 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) { 8808 verifyFormat("int a[5];"); 8809 verifyFormat("a[3] += 42;"); 8810 8811 FormatStyle Spaces = getLLVMStyle(); 8812 Spaces.SpacesInSquareBrackets = true; 8813 // Lambdas unchanged. 8814 verifyFormat("int c = []() -> int { return 2; }();\n", Spaces); 8815 verifyFormat("return [i, args...] {};", Spaces); 8816 8817 // Not lambdas. 8818 verifyFormat("int a[ 5 ];", Spaces); 8819 verifyFormat("a[ 3 ] += 42;", Spaces); 8820 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces); 8821 verifyFormat("double &operator[](int i) { return 0; }\n" 8822 "int i;", 8823 Spaces); 8824 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces); 8825 verifyFormat("int i = a[ a ][ a ]->f();", Spaces); 8826 verifyFormat("int i = (*b)[ a ]->f();", Spaces); 8827 } 8828 8829 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) { 8830 verifyFormat("int a = 5;"); 8831 verifyFormat("a += 42;"); 8832 verifyFormat("a or_eq 8;"); 8833 8834 FormatStyle Spaces = getLLVMStyle(); 8835 Spaces.SpaceBeforeAssignmentOperators = false; 8836 verifyFormat("int a= 5;", Spaces); 8837 verifyFormat("a+= 42;", Spaces); 8838 verifyFormat("a or_eq 8;", Spaces); 8839 } 8840 8841 TEST_F(FormatTest, AlignConsecutiveAssignments) { 8842 FormatStyle Alignment = getLLVMStyle(); 8843 Alignment.AlignConsecutiveAssignments = false; 8844 verifyFormat("int a = 5;\n" 8845 "int oneTwoThree = 123;", 8846 Alignment); 8847 verifyFormat("int a = 5;\n" 8848 "int oneTwoThree = 123;", 8849 Alignment); 8850 8851 Alignment.AlignConsecutiveAssignments = true; 8852 verifyFormat("int a = 5;\n" 8853 "int oneTwoThree = 123;", 8854 Alignment); 8855 verifyFormat("int a = method();\n" 8856 "int oneTwoThree = 133;", 8857 Alignment); 8858 verifyFormat("a &= 5;\n" 8859 "bcd *= 5;\n" 8860 "ghtyf += 5;\n" 8861 "dvfvdb -= 5;\n" 8862 "a /= 5;\n" 8863 "vdsvsv %= 5;\n" 8864 "sfdbddfbdfbb ^= 5;\n" 8865 "dvsdsv |= 5;\n" 8866 "int dsvvdvsdvvv = 123;", 8867 Alignment); 8868 verifyFormat("int i = 1, j = 10;\n" 8869 "something = 2000;", 8870 Alignment); 8871 verifyFormat("something = 2000;\n" 8872 "int i = 1, j = 10;\n", 8873 Alignment); 8874 verifyFormat("something = 2000;\n" 8875 "another = 911;\n" 8876 "int i = 1, j = 10;\n" 8877 "oneMore = 1;\n" 8878 "i = 2;", 8879 Alignment); 8880 verifyFormat("int a = 5;\n" 8881 "int one = 1;\n" 8882 "method();\n" 8883 "int oneTwoThree = 123;\n" 8884 "int oneTwo = 12;", 8885 Alignment); 8886 verifyFormat("int oneTwoThree = 123;\n" 8887 "int oneTwo = 12;\n" 8888 "method();\n", 8889 Alignment); 8890 verifyFormat("int oneTwoThree = 123; // comment\n" 8891 "int oneTwo = 12; // comment", 8892 Alignment); 8893 EXPECT_EQ("int a = 5;\n" 8894 "\n" 8895 "int oneTwoThree = 123;", 8896 format("int a = 5;\n" 8897 "\n" 8898 "int oneTwoThree= 123;", 8899 Alignment)); 8900 EXPECT_EQ("int a = 5;\n" 8901 "int one = 1;\n" 8902 "\n" 8903 "int oneTwoThree = 123;", 8904 format("int a = 5;\n" 8905 "int one = 1;\n" 8906 "\n" 8907 "int oneTwoThree = 123;", 8908 Alignment)); 8909 EXPECT_EQ("int a = 5;\n" 8910 "int one = 1;\n" 8911 "\n" 8912 "int oneTwoThree = 123;\n" 8913 "int oneTwo = 12;", 8914 format("int a = 5;\n" 8915 "int one = 1;\n" 8916 "\n" 8917 "int oneTwoThree = 123;\n" 8918 "int oneTwo = 12;", 8919 Alignment)); 8920 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 8921 verifyFormat("#define A \\\n" 8922 " int aaaa = 12; \\\n" 8923 " int b = 23; \\\n" 8924 " int ccc = 234; \\\n" 8925 " int dddddddddd = 2345;", 8926 Alignment); 8927 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 8928 verifyFormat("#define A \\\n" 8929 " int aaaa = 12; \\\n" 8930 " int b = 23; \\\n" 8931 " int ccc = 234; \\\n" 8932 " int dddddddddd = 2345;", 8933 Alignment); 8934 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 8935 verifyFormat("#define A " 8936 " \\\n" 8937 " int aaaa = 12; " 8938 " \\\n" 8939 " int b = 23; " 8940 " \\\n" 8941 " int ccc = 234; " 8942 " \\\n" 8943 " int dddddddddd = 2345;", 8944 Alignment); 8945 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 8946 "k = 4, int l = 5,\n" 8947 " int m = 6) {\n" 8948 " int j = 10;\n" 8949 " otherThing = 1;\n" 8950 "}", 8951 Alignment); 8952 verifyFormat("void SomeFunction(int parameter = 0) {\n" 8953 " int i = 1;\n" 8954 " int j = 2;\n" 8955 " int big = 10000;\n" 8956 "}", 8957 Alignment); 8958 verifyFormat("class C {\n" 8959 "public:\n" 8960 " int i = 1;\n" 8961 " virtual void f() = 0;\n" 8962 "};", 8963 Alignment); 8964 verifyFormat("int i = 1;\n" 8965 "if (SomeType t = getSomething()) {\n" 8966 "}\n" 8967 "int j = 2;\n" 8968 "int big = 10000;", 8969 Alignment); 8970 verifyFormat("int j = 7;\n" 8971 "for (int k = 0; k < N; ++k) {\n" 8972 "}\n" 8973 "int j = 2;\n" 8974 "int big = 10000;\n" 8975 "}", 8976 Alignment); 8977 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 8978 verifyFormat("int i = 1;\n" 8979 "LooooooooooongType loooooooooooooooooooooongVariable\n" 8980 " = someLooooooooooooooooongFunction();\n" 8981 "int j = 2;", 8982 Alignment); 8983 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 8984 verifyFormat("int i = 1;\n" 8985 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 8986 " someLooooooooooooooooongFunction();\n" 8987 "int j = 2;", 8988 Alignment); 8989 8990 verifyFormat("auto lambda = []() {\n" 8991 " auto i = 0;\n" 8992 " return 0;\n" 8993 "};\n" 8994 "int i = 0;\n" 8995 "auto v = type{\n" 8996 " i = 1, //\n" 8997 " (i = 2), //\n" 8998 " i = 3 //\n" 8999 "};", 9000 Alignment); 9001 9002 verifyFormat( 9003 "int i = 1;\n" 9004 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 9005 " loooooooooooooooooooooongParameterB);\n" 9006 "int j = 2;", 9007 Alignment); 9008 9009 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 9010 " typename B = very_long_type_name_1,\n" 9011 " typename T_2 = very_long_type_name_2>\n" 9012 "auto foo() {}\n", 9013 Alignment); 9014 verifyFormat("int a, b = 1;\n" 9015 "int c = 2;\n" 9016 "int dd = 3;\n", 9017 Alignment); 9018 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 9019 "float b[1][] = {{3.f}};\n", 9020 Alignment); 9021 verifyFormat("for (int i = 0; i < 1; i++)\n" 9022 " int x = 1;\n", 9023 Alignment); 9024 verifyFormat("for (i = 0; i < 1; i++)\n" 9025 " x = 1;\n" 9026 "y = 1;\n", 9027 Alignment); 9028 } 9029 9030 TEST_F(FormatTest, AlignConsecutiveDeclarations) { 9031 FormatStyle Alignment = getLLVMStyle(); 9032 Alignment.AlignConsecutiveDeclarations = false; 9033 verifyFormat("float const a = 5;\n" 9034 "int oneTwoThree = 123;", 9035 Alignment); 9036 verifyFormat("int a = 5;\n" 9037 "float const oneTwoThree = 123;", 9038 Alignment); 9039 9040 Alignment.AlignConsecutiveDeclarations = true; 9041 verifyFormat("float const a = 5;\n" 9042 "int oneTwoThree = 123;", 9043 Alignment); 9044 verifyFormat("int a = method();\n" 9045 "float const oneTwoThree = 133;", 9046 Alignment); 9047 verifyFormat("int i = 1, j = 10;\n" 9048 "something = 2000;", 9049 Alignment); 9050 verifyFormat("something = 2000;\n" 9051 "int i = 1, j = 10;\n", 9052 Alignment); 9053 verifyFormat("float something = 2000;\n" 9054 "double another = 911;\n" 9055 "int i = 1, j = 10;\n" 9056 "const int *oneMore = 1;\n" 9057 "unsigned i = 2;", 9058 Alignment); 9059 verifyFormat("float a = 5;\n" 9060 "int one = 1;\n" 9061 "method();\n" 9062 "const double oneTwoThree = 123;\n" 9063 "const unsigned int oneTwo = 12;", 9064 Alignment); 9065 verifyFormat("int oneTwoThree{0}; // comment\n" 9066 "unsigned oneTwo; // comment", 9067 Alignment); 9068 EXPECT_EQ("float const a = 5;\n" 9069 "\n" 9070 "int oneTwoThree = 123;", 9071 format("float const a = 5;\n" 9072 "\n" 9073 "int oneTwoThree= 123;", 9074 Alignment)); 9075 EXPECT_EQ("float a = 5;\n" 9076 "int one = 1;\n" 9077 "\n" 9078 "unsigned oneTwoThree = 123;", 9079 format("float a = 5;\n" 9080 "int one = 1;\n" 9081 "\n" 9082 "unsigned oneTwoThree = 123;", 9083 Alignment)); 9084 EXPECT_EQ("float a = 5;\n" 9085 "int one = 1;\n" 9086 "\n" 9087 "unsigned oneTwoThree = 123;\n" 9088 "int oneTwo = 12;", 9089 format("float a = 5;\n" 9090 "int one = 1;\n" 9091 "\n" 9092 "unsigned oneTwoThree = 123;\n" 9093 "int oneTwo = 12;", 9094 Alignment)); 9095 // Function prototype alignment 9096 verifyFormat("int a();\n" 9097 "double b();", 9098 Alignment); 9099 verifyFormat("int a(int x);\n" 9100 "double b();", 9101 Alignment); 9102 unsigned OldColumnLimit = Alignment.ColumnLimit; 9103 // We need to set ColumnLimit to zero, in order to stress nested alignments, 9104 // otherwise the function parameters will be re-flowed onto a single line. 9105 Alignment.ColumnLimit = 0; 9106 EXPECT_EQ("int a(int x,\n" 9107 " float y);\n" 9108 "double b(int x,\n" 9109 " double y);", 9110 format("int a(int x,\n" 9111 " float y);\n" 9112 "double b(int x,\n" 9113 " double y);", 9114 Alignment)); 9115 // This ensures that function parameters of function declarations are 9116 // correctly indented when their owning functions are indented. 9117 // The failure case here is for 'double y' to not be indented enough. 9118 EXPECT_EQ("double a(int x);\n" 9119 "int b(int y,\n" 9120 " double z);", 9121 format("double a(int x);\n" 9122 "int b(int y,\n" 9123 " double z);", 9124 Alignment)); 9125 // Set ColumnLimit low so that we induce wrapping immediately after 9126 // the function name and opening paren. 9127 Alignment.ColumnLimit = 13; 9128 verifyFormat("int function(\n" 9129 " int x,\n" 9130 " bool y);", 9131 Alignment); 9132 Alignment.ColumnLimit = OldColumnLimit; 9133 // Ensure function pointers don't screw up recursive alignment 9134 verifyFormat("int a(int x, void (*fp)(int y));\n" 9135 "double b();", 9136 Alignment); 9137 Alignment.AlignConsecutiveAssignments = true; 9138 // Ensure recursive alignment is broken by function braces, so that the 9139 // "a = 1" does not align with subsequent assignments inside the function 9140 // body. 9141 verifyFormat("int func(int a = 1) {\n" 9142 " int b = 2;\n" 9143 " int cc = 3;\n" 9144 "}", 9145 Alignment); 9146 verifyFormat("float something = 2000;\n" 9147 "double another = 911;\n" 9148 "int i = 1, j = 10;\n" 9149 "const int *oneMore = 1;\n" 9150 "unsigned i = 2;", 9151 Alignment); 9152 verifyFormat("int oneTwoThree = {0}; // comment\n" 9153 "unsigned oneTwo = 0; // comment", 9154 Alignment); 9155 // Make sure that scope is correctly tracked, in the absence of braces 9156 verifyFormat("for (int i = 0; i < n; i++)\n" 9157 " j = i;\n" 9158 "double x = 1;\n", 9159 Alignment); 9160 verifyFormat("if (int i = 0)\n" 9161 " j = i;\n" 9162 "double x = 1;\n", 9163 Alignment); 9164 // Ensure operator[] and operator() are comprehended 9165 verifyFormat("struct test {\n" 9166 " long long int foo();\n" 9167 " int operator[](int a);\n" 9168 " double bar();\n" 9169 "};\n", 9170 Alignment); 9171 verifyFormat("struct test {\n" 9172 " long long int foo();\n" 9173 " int operator()(int a);\n" 9174 " double bar();\n" 9175 "};\n", 9176 Alignment); 9177 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 9178 " int const i = 1;\n" 9179 " int * j = 2;\n" 9180 " int big = 10000;\n" 9181 "\n" 9182 " unsigned oneTwoThree = 123;\n" 9183 " int oneTwo = 12;\n" 9184 " method();\n" 9185 " float k = 2;\n" 9186 " int ll = 10000;\n" 9187 "}", 9188 format("void SomeFunction(int parameter= 0) {\n" 9189 " int const i= 1;\n" 9190 " int *j=2;\n" 9191 " int big = 10000;\n" 9192 "\n" 9193 "unsigned oneTwoThree =123;\n" 9194 "int oneTwo = 12;\n" 9195 " method();\n" 9196 "float k= 2;\n" 9197 "int ll=10000;\n" 9198 "}", 9199 Alignment)); 9200 Alignment.AlignConsecutiveAssignments = false; 9201 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 9202 verifyFormat("#define A \\\n" 9203 " int aaaa = 12; \\\n" 9204 " float b = 23; \\\n" 9205 " const int ccc = 234; \\\n" 9206 " unsigned dddddddddd = 2345;", 9207 Alignment); 9208 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 9209 verifyFormat("#define A \\\n" 9210 " int aaaa = 12; \\\n" 9211 " float b = 23; \\\n" 9212 " const int ccc = 234; \\\n" 9213 " unsigned dddddddddd = 2345;", 9214 Alignment); 9215 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 9216 Alignment.ColumnLimit = 30; 9217 verifyFormat("#define A \\\n" 9218 " int aaaa = 12; \\\n" 9219 " float b = 23; \\\n" 9220 " const int ccc = 234; \\\n" 9221 " int dddddddddd = 2345;", 9222 Alignment); 9223 Alignment.ColumnLimit = 80; 9224 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 9225 "k = 4, int l = 5,\n" 9226 " int m = 6) {\n" 9227 " const int j = 10;\n" 9228 " otherThing = 1;\n" 9229 "}", 9230 Alignment); 9231 verifyFormat("void SomeFunction(int parameter = 0) {\n" 9232 " int const i = 1;\n" 9233 " int * j = 2;\n" 9234 " int big = 10000;\n" 9235 "}", 9236 Alignment); 9237 verifyFormat("class C {\n" 9238 "public:\n" 9239 " int i = 1;\n" 9240 " virtual void f() = 0;\n" 9241 "};", 9242 Alignment); 9243 verifyFormat("float i = 1;\n" 9244 "if (SomeType t = getSomething()) {\n" 9245 "}\n" 9246 "const unsigned j = 2;\n" 9247 "int big = 10000;", 9248 Alignment); 9249 verifyFormat("float j = 7;\n" 9250 "for (int k = 0; k < N; ++k) {\n" 9251 "}\n" 9252 "unsigned j = 2;\n" 9253 "int big = 10000;\n" 9254 "}", 9255 Alignment); 9256 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 9257 verifyFormat("float i = 1;\n" 9258 "LooooooooooongType loooooooooooooooooooooongVariable\n" 9259 " = someLooooooooooooooooongFunction();\n" 9260 "int j = 2;", 9261 Alignment); 9262 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 9263 verifyFormat("int i = 1;\n" 9264 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 9265 " someLooooooooooooooooongFunction();\n" 9266 "int j = 2;", 9267 Alignment); 9268 9269 Alignment.AlignConsecutiveAssignments = true; 9270 verifyFormat("auto lambda = []() {\n" 9271 " auto ii = 0;\n" 9272 " float j = 0;\n" 9273 " return 0;\n" 9274 "};\n" 9275 "int i = 0;\n" 9276 "float i2 = 0;\n" 9277 "auto v = type{\n" 9278 " i = 1, //\n" 9279 " (i = 2), //\n" 9280 " i = 3 //\n" 9281 "};", 9282 Alignment); 9283 Alignment.AlignConsecutiveAssignments = false; 9284 9285 verifyFormat( 9286 "int i = 1;\n" 9287 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 9288 " loooooooooooooooooooooongParameterB);\n" 9289 "int j = 2;", 9290 Alignment); 9291 9292 // Test interactions with ColumnLimit and AlignConsecutiveAssignments: 9293 // We expect declarations and assignments to align, as long as it doesn't 9294 // exceed the column limit, starting a new alignment sequence whenever it 9295 // happens. 9296 Alignment.AlignConsecutiveAssignments = true; 9297 Alignment.ColumnLimit = 30; 9298 verifyFormat("float ii = 1;\n" 9299 "unsigned j = 2;\n" 9300 "int someVerylongVariable = 1;\n" 9301 "AnotherLongType ll = 123456;\n" 9302 "VeryVeryLongType k = 2;\n" 9303 "int myvar = 1;", 9304 Alignment); 9305 Alignment.ColumnLimit = 80; 9306 Alignment.AlignConsecutiveAssignments = false; 9307 9308 verifyFormat( 9309 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n" 9310 " typename LongType, typename B>\n" 9311 "auto foo() {}\n", 9312 Alignment); 9313 verifyFormat("float a, b = 1;\n" 9314 "int c = 2;\n" 9315 "int dd = 3;\n", 9316 Alignment); 9317 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 9318 "float b[1][] = {{3.f}};\n", 9319 Alignment); 9320 Alignment.AlignConsecutiveAssignments = true; 9321 verifyFormat("float a, b = 1;\n" 9322 "int c = 2;\n" 9323 "int dd = 3;\n", 9324 Alignment); 9325 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 9326 "float b[1][] = {{3.f}};\n", 9327 Alignment); 9328 Alignment.AlignConsecutiveAssignments = false; 9329 9330 Alignment.ColumnLimit = 30; 9331 Alignment.BinPackParameters = false; 9332 verifyFormat("void foo(float a,\n" 9333 " float b,\n" 9334 " int c,\n" 9335 " uint32_t *d) {\n" 9336 " int * e = 0;\n" 9337 " float f = 0;\n" 9338 " double g = 0;\n" 9339 "}\n" 9340 "void bar(ino_t a,\n" 9341 " int b,\n" 9342 " uint32_t *c,\n" 9343 " bool d) {}\n", 9344 Alignment); 9345 Alignment.BinPackParameters = true; 9346 Alignment.ColumnLimit = 80; 9347 9348 // Bug 33507 9349 Alignment.PointerAlignment = FormatStyle::PAS_Middle; 9350 verifyFormat( 9351 "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n" 9352 " static const Version verVs2017;\n" 9353 " return true;\n" 9354 "});\n", 9355 Alignment); 9356 Alignment.PointerAlignment = FormatStyle::PAS_Right; 9357 } 9358 9359 TEST_F(FormatTest, LinuxBraceBreaking) { 9360 FormatStyle LinuxBraceStyle = getLLVMStyle(); 9361 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux; 9362 verifyFormat("namespace a\n" 9363 "{\n" 9364 "class A\n" 9365 "{\n" 9366 " void f()\n" 9367 " {\n" 9368 " if (true) {\n" 9369 " a();\n" 9370 " b();\n" 9371 " } else {\n" 9372 " a();\n" 9373 " }\n" 9374 " }\n" 9375 " void g() { return; }\n" 9376 "};\n" 9377 "struct B {\n" 9378 " int x;\n" 9379 "};\n" 9380 "} // namespace a\n", 9381 LinuxBraceStyle); 9382 verifyFormat("enum X {\n" 9383 " Y = 0,\n" 9384 "}\n", 9385 LinuxBraceStyle); 9386 verifyFormat("struct S {\n" 9387 " int Type;\n" 9388 " union {\n" 9389 " int x;\n" 9390 " double y;\n" 9391 " } Value;\n" 9392 " class C\n" 9393 " {\n" 9394 " MyFavoriteType Value;\n" 9395 " } Class;\n" 9396 "}\n", 9397 LinuxBraceStyle); 9398 } 9399 9400 TEST_F(FormatTest, MozillaBraceBreaking) { 9401 FormatStyle MozillaBraceStyle = getLLVMStyle(); 9402 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; 9403 MozillaBraceStyle.FixNamespaceComments = false; 9404 verifyFormat("namespace a {\n" 9405 "class A\n" 9406 "{\n" 9407 " void f()\n" 9408 " {\n" 9409 " if (true) {\n" 9410 " a();\n" 9411 " b();\n" 9412 " }\n" 9413 " }\n" 9414 " void g() { return; }\n" 9415 "};\n" 9416 "enum E\n" 9417 "{\n" 9418 " A,\n" 9419 " // foo\n" 9420 " B,\n" 9421 " C\n" 9422 "};\n" 9423 "struct B\n" 9424 "{\n" 9425 " int x;\n" 9426 "};\n" 9427 "}\n", 9428 MozillaBraceStyle); 9429 verifyFormat("struct S\n" 9430 "{\n" 9431 " int Type;\n" 9432 " union\n" 9433 " {\n" 9434 " int x;\n" 9435 " double y;\n" 9436 " } Value;\n" 9437 " class C\n" 9438 " {\n" 9439 " MyFavoriteType Value;\n" 9440 " } Class;\n" 9441 "}\n", 9442 MozillaBraceStyle); 9443 } 9444 9445 TEST_F(FormatTest, StroustrupBraceBreaking) { 9446 FormatStyle StroustrupBraceStyle = getLLVMStyle(); 9447 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 9448 verifyFormat("namespace a {\n" 9449 "class A {\n" 9450 " void f()\n" 9451 " {\n" 9452 " if (true) {\n" 9453 " a();\n" 9454 " b();\n" 9455 " }\n" 9456 " }\n" 9457 " void g() { return; }\n" 9458 "};\n" 9459 "struct B {\n" 9460 " int x;\n" 9461 "};\n" 9462 "} // namespace a\n", 9463 StroustrupBraceStyle); 9464 9465 verifyFormat("void foo()\n" 9466 "{\n" 9467 " if (a) {\n" 9468 " a();\n" 9469 " }\n" 9470 " else {\n" 9471 " b();\n" 9472 " }\n" 9473 "}\n", 9474 StroustrupBraceStyle); 9475 9476 verifyFormat("#ifdef _DEBUG\n" 9477 "int foo(int i = 0)\n" 9478 "#else\n" 9479 "int foo(int i = 5)\n" 9480 "#endif\n" 9481 "{\n" 9482 " return i;\n" 9483 "}", 9484 StroustrupBraceStyle); 9485 9486 verifyFormat("void foo() {}\n" 9487 "void bar()\n" 9488 "#ifdef _DEBUG\n" 9489 "{\n" 9490 " foo();\n" 9491 "}\n" 9492 "#else\n" 9493 "{\n" 9494 "}\n" 9495 "#endif", 9496 StroustrupBraceStyle); 9497 9498 verifyFormat("void foobar() { int i = 5; }\n" 9499 "#ifdef _DEBUG\n" 9500 "void bar() {}\n" 9501 "#else\n" 9502 "void bar() { foobar(); }\n" 9503 "#endif", 9504 StroustrupBraceStyle); 9505 } 9506 9507 TEST_F(FormatTest, AllmanBraceBreaking) { 9508 FormatStyle AllmanBraceStyle = getLLVMStyle(); 9509 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman; 9510 9511 EXPECT_EQ("namespace a\n" 9512 "{\n" 9513 "void f();\n" 9514 "void g();\n" 9515 "} // namespace a\n", 9516 format("namespace a\n" 9517 "{\n" 9518 "void f();\n" 9519 "void g();\n" 9520 "}\n", 9521 AllmanBraceStyle)); 9522 9523 verifyFormat("namespace a\n" 9524 "{\n" 9525 "class A\n" 9526 "{\n" 9527 " void f()\n" 9528 " {\n" 9529 " if (true)\n" 9530 " {\n" 9531 " a();\n" 9532 " b();\n" 9533 " }\n" 9534 " }\n" 9535 " void g() { return; }\n" 9536 "};\n" 9537 "struct B\n" 9538 "{\n" 9539 " int x;\n" 9540 "};\n" 9541 "} // namespace a", 9542 AllmanBraceStyle); 9543 9544 verifyFormat("void f()\n" 9545 "{\n" 9546 " if (true)\n" 9547 " {\n" 9548 " a();\n" 9549 " }\n" 9550 " else if (false)\n" 9551 " {\n" 9552 " b();\n" 9553 " }\n" 9554 " else\n" 9555 " {\n" 9556 " c();\n" 9557 " }\n" 9558 "}\n", 9559 AllmanBraceStyle); 9560 9561 verifyFormat("void f()\n" 9562 "{\n" 9563 " for (int i = 0; i < 10; ++i)\n" 9564 " {\n" 9565 " a();\n" 9566 " }\n" 9567 " while (false)\n" 9568 " {\n" 9569 " b();\n" 9570 " }\n" 9571 " do\n" 9572 " {\n" 9573 " c();\n" 9574 " } while (false)\n" 9575 "}\n", 9576 AllmanBraceStyle); 9577 9578 verifyFormat("void f(int a)\n" 9579 "{\n" 9580 " switch (a)\n" 9581 " {\n" 9582 " case 0:\n" 9583 " break;\n" 9584 " case 1:\n" 9585 " {\n" 9586 " break;\n" 9587 " }\n" 9588 " case 2:\n" 9589 " {\n" 9590 " }\n" 9591 " break;\n" 9592 " default:\n" 9593 " break;\n" 9594 " }\n" 9595 "}\n", 9596 AllmanBraceStyle); 9597 9598 verifyFormat("enum X\n" 9599 "{\n" 9600 " Y = 0,\n" 9601 "}\n", 9602 AllmanBraceStyle); 9603 verifyFormat("enum X\n" 9604 "{\n" 9605 " Y = 0\n" 9606 "}\n", 9607 AllmanBraceStyle); 9608 9609 verifyFormat("@interface BSApplicationController ()\n" 9610 "{\n" 9611 "@private\n" 9612 " id _extraIvar;\n" 9613 "}\n" 9614 "@end\n", 9615 AllmanBraceStyle); 9616 9617 verifyFormat("#ifdef _DEBUG\n" 9618 "int foo(int i = 0)\n" 9619 "#else\n" 9620 "int foo(int i = 5)\n" 9621 "#endif\n" 9622 "{\n" 9623 " return i;\n" 9624 "}", 9625 AllmanBraceStyle); 9626 9627 verifyFormat("void foo() {}\n" 9628 "void bar()\n" 9629 "#ifdef _DEBUG\n" 9630 "{\n" 9631 " foo();\n" 9632 "}\n" 9633 "#else\n" 9634 "{\n" 9635 "}\n" 9636 "#endif", 9637 AllmanBraceStyle); 9638 9639 verifyFormat("void foobar() { int i = 5; }\n" 9640 "#ifdef _DEBUG\n" 9641 "void bar() {}\n" 9642 "#else\n" 9643 "void bar() { foobar(); }\n" 9644 "#endif", 9645 AllmanBraceStyle); 9646 9647 // This shouldn't affect ObjC blocks.. 9648 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 9649 " // ...\n" 9650 " int i;\n" 9651 "}];", 9652 AllmanBraceStyle); 9653 verifyFormat("void (^block)(void) = ^{\n" 9654 " // ...\n" 9655 " int i;\n" 9656 "};", 9657 AllmanBraceStyle); 9658 // .. or dict literals. 9659 verifyFormat("void f()\n" 9660 "{\n" 9661 " // ...\n" 9662 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 9663 "}", 9664 AllmanBraceStyle); 9665 verifyFormat("void f()\n" 9666 "{\n" 9667 " // ...\n" 9668 " [object someMethod:@{a : @\"b\"}];\n" 9669 "}", 9670 AllmanBraceStyle); 9671 verifyFormat("int f()\n" 9672 "{ // comment\n" 9673 " return 42;\n" 9674 "}", 9675 AllmanBraceStyle); 9676 9677 AllmanBraceStyle.ColumnLimit = 19; 9678 verifyFormat("void f() { int i; }", AllmanBraceStyle); 9679 AllmanBraceStyle.ColumnLimit = 18; 9680 verifyFormat("void f()\n" 9681 "{\n" 9682 " int i;\n" 9683 "}", 9684 AllmanBraceStyle); 9685 AllmanBraceStyle.ColumnLimit = 80; 9686 9687 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle; 9688 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true; 9689 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 9690 verifyFormat("void f(bool b)\n" 9691 "{\n" 9692 " if (b)\n" 9693 " {\n" 9694 " return;\n" 9695 " }\n" 9696 "}\n", 9697 BreakBeforeBraceShortIfs); 9698 verifyFormat("void f(bool b)\n" 9699 "{\n" 9700 " if constexpr (b)\n" 9701 " {\n" 9702 " return;\n" 9703 " }\n" 9704 "}\n", 9705 BreakBeforeBraceShortIfs); 9706 verifyFormat("void f(bool b)\n" 9707 "{\n" 9708 " if (b) return;\n" 9709 "}\n", 9710 BreakBeforeBraceShortIfs); 9711 verifyFormat("void f(bool b)\n" 9712 "{\n" 9713 " if constexpr (b) return;\n" 9714 "}\n", 9715 BreakBeforeBraceShortIfs); 9716 verifyFormat("void f(bool b)\n" 9717 "{\n" 9718 " while (b)\n" 9719 " {\n" 9720 " return;\n" 9721 " }\n" 9722 "}\n", 9723 BreakBeforeBraceShortIfs); 9724 } 9725 9726 TEST_F(FormatTest, GNUBraceBreaking) { 9727 FormatStyle GNUBraceStyle = getLLVMStyle(); 9728 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU; 9729 verifyFormat("namespace a\n" 9730 "{\n" 9731 "class A\n" 9732 "{\n" 9733 " void f()\n" 9734 " {\n" 9735 " int a;\n" 9736 " {\n" 9737 " int b;\n" 9738 " }\n" 9739 " if (true)\n" 9740 " {\n" 9741 " a();\n" 9742 " b();\n" 9743 " }\n" 9744 " }\n" 9745 " void g() { return; }\n" 9746 "}\n" 9747 "} // namespace a", 9748 GNUBraceStyle); 9749 9750 verifyFormat("void f()\n" 9751 "{\n" 9752 " if (true)\n" 9753 " {\n" 9754 " a();\n" 9755 " }\n" 9756 " else if (false)\n" 9757 " {\n" 9758 " b();\n" 9759 " }\n" 9760 " else\n" 9761 " {\n" 9762 " c();\n" 9763 " }\n" 9764 "}\n", 9765 GNUBraceStyle); 9766 9767 verifyFormat("void f()\n" 9768 "{\n" 9769 " for (int i = 0; i < 10; ++i)\n" 9770 " {\n" 9771 " a();\n" 9772 " }\n" 9773 " while (false)\n" 9774 " {\n" 9775 " b();\n" 9776 " }\n" 9777 " do\n" 9778 " {\n" 9779 " c();\n" 9780 " }\n" 9781 " while (false);\n" 9782 "}\n", 9783 GNUBraceStyle); 9784 9785 verifyFormat("void f(int a)\n" 9786 "{\n" 9787 " switch (a)\n" 9788 " {\n" 9789 " case 0:\n" 9790 " break;\n" 9791 " case 1:\n" 9792 " {\n" 9793 " break;\n" 9794 " }\n" 9795 " case 2:\n" 9796 " {\n" 9797 " }\n" 9798 " break;\n" 9799 " default:\n" 9800 " break;\n" 9801 " }\n" 9802 "}\n", 9803 GNUBraceStyle); 9804 9805 verifyFormat("enum X\n" 9806 "{\n" 9807 " Y = 0,\n" 9808 "}\n", 9809 GNUBraceStyle); 9810 9811 verifyFormat("@interface BSApplicationController ()\n" 9812 "{\n" 9813 "@private\n" 9814 " id _extraIvar;\n" 9815 "}\n" 9816 "@end\n", 9817 GNUBraceStyle); 9818 9819 verifyFormat("#ifdef _DEBUG\n" 9820 "int foo(int i = 0)\n" 9821 "#else\n" 9822 "int foo(int i = 5)\n" 9823 "#endif\n" 9824 "{\n" 9825 " return i;\n" 9826 "}", 9827 GNUBraceStyle); 9828 9829 verifyFormat("void foo() {}\n" 9830 "void bar()\n" 9831 "#ifdef _DEBUG\n" 9832 "{\n" 9833 " foo();\n" 9834 "}\n" 9835 "#else\n" 9836 "{\n" 9837 "}\n" 9838 "#endif", 9839 GNUBraceStyle); 9840 9841 verifyFormat("void foobar() { int i = 5; }\n" 9842 "#ifdef _DEBUG\n" 9843 "void bar() {}\n" 9844 "#else\n" 9845 "void bar() { foobar(); }\n" 9846 "#endif", 9847 GNUBraceStyle); 9848 } 9849 9850 TEST_F(FormatTest, WebKitBraceBreaking) { 9851 FormatStyle WebKitBraceStyle = getLLVMStyle(); 9852 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit; 9853 WebKitBraceStyle.FixNamespaceComments = false; 9854 verifyFormat("namespace a {\n" 9855 "class A {\n" 9856 " void f()\n" 9857 " {\n" 9858 " if (true) {\n" 9859 " a();\n" 9860 " b();\n" 9861 " }\n" 9862 " }\n" 9863 " void g() { return; }\n" 9864 "};\n" 9865 "enum E {\n" 9866 " A,\n" 9867 " // foo\n" 9868 " B,\n" 9869 " C\n" 9870 "};\n" 9871 "struct B {\n" 9872 " int x;\n" 9873 "};\n" 9874 "}\n", 9875 WebKitBraceStyle); 9876 verifyFormat("struct S {\n" 9877 " int Type;\n" 9878 " union {\n" 9879 " int x;\n" 9880 " double y;\n" 9881 " } Value;\n" 9882 " class C {\n" 9883 " MyFavoriteType Value;\n" 9884 " } Class;\n" 9885 "};\n", 9886 WebKitBraceStyle); 9887 } 9888 9889 TEST_F(FormatTest, CatchExceptionReferenceBinding) { 9890 verifyFormat("void f() {\n" 9891 " try {\n" 9892 " } catch (const Exception &e) {\n" 9893 " }\n" 9894 "}\n", 9895 getLLVMStyle()); 9896 } 9897 9898 TEST_F(FormatTest, UnderstandsPragmas) { 9899 verifyFormat("#pragma omp reduction(| : var)"); 9900 verifyFormat("#pragma omp reduction(+ : var)"); 9901 9902 EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string " 9903 "(including parentheses).", 9904 format("#pragma mark Any non-hyphenated or hyphenated string " 9905 "(including parentheses).")); 9906 } 9907 9908 TEST_F(FormatTest, UnderstandPragmaOption) { 9909 verifyFormat("#pragma option -C -A"); 9910 9911 EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A")); 9912 } 9913 9914 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) { 9915 FormatStyle Style = getLLVMStyle(); 9916 Style.ColumnLimit = 20; 9917 9918 verifyFormat("int a; // the\n" 9919 " // comment", Style); 9920 EXPECT_EQ("int a; /* first line\n" 9921 " * second\n" 9922 " * line third\n" 9923 " * line\n" 9924 " */", 9925 format("int a; /* first line\n" 9926 " * second\n" 9927 " * line third\n" 9928 " * line\n" 9929 " */", 9930 Style)); 9931 EXPECT_EQ("int a; // first line\n" 9932 " // second\n" 9933 " // line third\n" 9934 " // line", 9935 format("int a; // first line\n" 9936 " // second line\n" 9937 " // third line", 9938 Style)); 9939 9940 Style.PenaltyExcessCharacter = 90; 9941 verifyFormat("int a; // the comment", Style); 9942 EXPECT_EQ("int a; // the comment\n" 9943 " // aaa", 9944 format("int a; // the comment aaa", Style)); 9945 EXPECT_EQ("int a; /* first line\n" 9946 " * second line\n" 9947 " * third line\n" 9948 " */", 9949 format("int a; /* first line\n" 9950 " * second line\n" 9951 " * third line\n" 9952 " */", 9953 Style)); 9954 EXPECT_EQ("int a; // first line\n" 9955 " // second line\n" 9956 " // third line", 9957 format("int a; // first line\n" 9958 " // second line\n" 9959 " // third line", 9960 Style)); 9961 // FIXME: Investigate why this is not getting the same layout as the test 9962 // above. 9963 EXPECT_EQ("int a; /* first line\n" 9964 " * second line\n" 9965 " * third line\n" 9966 " */", 9967 format("int a; /* first line second line third line" 9968 "\n*/", 9969 Style)); 9970 9971 EXPECT_EQ("// foo bar baz bazfoo\n" 9972 "// foo bar foo bar\n", 9973 format("// foo bar baz bazfoo\n" 9974 "// foo bar foo bar\n", 9975 Style)); 9976 EXPECT_EQ("// foo bar baz bazfoo\n" 9977 "// foo bar foo bar\n", 9978 format("// foo bar baz bazfoo\n" 9979 "// foo bar foo bar\n", 9980 Style)); 9981 9982 // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the 9983 // next one. 9984 EXPECT_EQ("// foo bar baz bazfoo\n" 9985 "// bar foo bar\n", 9986 format("// foo bar baz bazfoo bar\n" 9987 "// foo bar\n", 9988 Style)); 9989 9990 EXPECT_EQ("// foo bar baz bazfoo\n" 9991 "// foo bar baz bazfoo\n" 9992 "// bar foo bar\n", 9993 format("// foo bar baz bazfoo\n" 9994 "// foo bar baz bazfoo bar\n" 9995 "// foo bar\n", 9996 Style)); 9997 9998 EXPECT_EQ("// foo bar baz bazfoo\n" 9999 "// foo bar baz bazfoo\n" 10000 "// bar foo bar\n", 10001 format("// foo bar baz bazfoo\n" 10002 "// foo bar baz bazfoo bar\n" 10003 "// foo bar\n", 10004 Style)); 10005 10006 // Make sure we do not keep protruding characters if strict mode reflow is 10007 // cheaper than keeping protruding characters. 10008 Style.ColumnLimit = 21; 10009 EXPECT_EQ("// foo foo foo foo\n" 10010 "// foo foo foo foo\n" 10011 "// foo foo foo foo\n", 10012 format("// foo foo foo foo foo foo foo foo foo foo foo foo\n", 10013 Style)); 10014 10015 EXPECT_EQ("int a = /* long block\n" 10016 " comment */\n" 10017 " 42;", 10018 format("int a = /* long block comment */ 42;", Style)); 10019 } 10020 10021 #define EXPECT_ALL_STYLES_EQUAL(Styles) \ 10022 for (size_t i = 1; i < Styles.size(); ++i) \ 10023 EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \ 10024 << " differs from Style #0" 10025 10026 TEST_F(FormatTest, GetsPredefinedStyleByName) { 10027 SmallVector<FormatStyle, 3> Styles; 10028 Styles.resize(3); 10029 10030 Styles[0] = getLLVMStyle(); 10031 EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1])); 10032 EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2])); 10033 EXPECT_ALL_STYLES_EQUAL(Styles); 10034 10035 Styles[0] = getGoogleStyle(); 10036 EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1])); 10037 EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2])); 10038 EXPECT_ALL_STYLES_EQUAL(Styles); 10039 10040 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 10041 EXPECT_TRUE( 10042 getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1])); 10043 EXPECT_TRUE( 10044 getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2])); 10045 EXPECT_ALL_STYLES_EQUAL(Styles); 10046 10047 Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp); 10048 EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1])); 10049 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2])); 10050 EXPECT_ALL_STYLES_EQUAL(Styles); 10051 10052 Styles[0] = getMozillaStyle(); 10053 EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1])); 10054 EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2])); 10055 EXPECT_ALL_STYLES_EQUAL(Styles); 10056 10057 Styles[0] = getWebKitStyle(); 10058 EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1])); 10059 EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2])); 10060 EXPECT_ALL_STYLES_EQUAL(Styles); 10061 10062 Styles[0] = getGNUStyle(); 10063 EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1])); 10064 EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2])); 10065 EXPECT_ALL_STYLES_EQUAL(Styles); 10066 10067 EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0])); 10068 } 10069 10070 TEST_F(FormatTest, GetsCorrectBasedOnStyle) { 10071 SmallVector<FormatStyle, 8> Styles; 10072 Styles.resize(2); 10073 10074 Styles[0] = getGoogleStyle(); 10075 Styles[1] = getLLVMStyle(); 10076 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 10077 EXPECT_ALL_STYLES_EQUAL(Styles); 10078 10079 Styles.resize(5); 10080 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 10081 Styles[1] = getLLVMStyle(); 10082 Styles[1].Language = FormatStyle::LK_JavaScript; 10083 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 10084 10085 Styles[2] = getLLVMStyle(); 10086 Styles[2].Language = FormatStyle::LK_JavaScript; 10087 EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n" 10088 "BasedOnStyle: Google", 10089 &Styles[2]) 10090 .value()); 10091 10092 Styles[3] = getLLVMStyle(); 10093 Styles[3].Language = FormatStyle::LK_JavaScript; 10094 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n" 10095 "Language: JavaScript", 10096 &Styles[3]) 10097 .value()); 10098 10099 Styles[4] = getLLVMStyle(); 10100 Styles[4].Language = FormatStyle::LK_JavaScript; 10101 EXPECT_EQ(0, parseConfiguration("---\n" 10102 "BasedOnStyle: LLVM\n" 10103 "IndentWidth: 123\n" 10104 "---\n" 10105 "BasedOnStyle: Google\n" 10106 "Language: JavaScript", 10107 &Styles[4]) 10108 .value()); 10109 EXPECT_ALL_STYLES_EQUAL(Styles); 10110 } 10111 10112 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \ 10113 Style.FIELD = false; \ 10114 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \ 10115 EXPECT_TRUE(Style.FIELD); \ 10116 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \ 10117 EXPECT_FALSE(Style.FIELD); 10118 10119 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD) 10120 10121 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \ 10122 Style.STRUCT.FIELD = false; \ 10123 EXPECT_EQ(0, \ 10124 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \ 10125 .value()); \ 10126 EXPECT_TRUE(Style.STRUCT.FIELD); \ 10127 EXPECT_EQ(0, \ 10128 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \ 10129 .value()); \ 10130 EXPECT_FALSE(Style.STRUCT.FIELD); 10131 10132 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \ 10133 CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD) 10134 10135 #define CHECK_PARSE(TEXT, FIELD, VALUE) \ 10136 EXPECT_NE(VALUE, Style.FIELD); \ 10137 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \ 10138 EXPECT_EQ(VALUE, Style.FIELD) 10139 10140 TEST_F(FormatTest, ParsesConfigurationBools) { 10141 FormatStyle Style = {}; 10142 Style.Language = FormatStyle::LK_Cpp; 10143 CHECK_PARSE_BOOL(AlignOperands); 10144 CHECK_PARSE_BOOL(AlignTrailingComments); 10145 CHECK_PARSE_BOOL(AlignConsecutiveAssignments); 10146 CHECK_PARSE_BOOL(AlignConsecutiveDeclarations); 10147 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); 10148 CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine); 10149 CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine); 10150 CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine); 10151 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); 10152 CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations); 10153 CHECK_PARSE_BOOL(BinPackArguments); 10154 CHECK_PARSE_BOOL(BinPackParameters); 10155 CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations); 10156 CHECK_PARSE_BOOL(BreakBeforeTernaryOperators); 10157 CHECK_PARSE_BOOL(BreakStringLiterals); 10158 CHECK_PARSE_BOOL(BreakBeforeInheritanceComma) 10159 CHECK_PARSE_BOOL(CompactNamespaces); 10160 CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine); 10161 CHECK_PARSE_BOOL(DerivePointerAlignment); 10162 CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding"); 10163 CHECK_PARSE_BOOL(DisableFormat); 10164 CHECK_PARSE_BOOL(IndentCaseLabels); 10165 CHECK_PARSE_BOOL(IndentWrappedFunctionNames); 10166 CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks); 10167 CHECK_PARSE_BOOL(ObjCSpaceAfterProperty); 10168 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList); 10169 CHECK_PARSE_BOOL(Cpp11BracedListStyle); 10170 CHECK_PARSE_BOOL(ReflowComments); 10171 CHECK_PARSE_BOOL(SortIncludes); 10172 CHECK_PARSE_BOOL(SortUsingDeclarations); 10173 CHECK_PARSE_BOOL(SpacesInParentheses); 10174 CHECK_PARSE_BOOL(SpacesInSquareBrackets); 10175 CHECK_PARSE_BOOL(SpacesInAngles); 10176 CHECK_PARSE_BOOL(SpaceInEmptyParentheses); 10177 CHECK_PARSE_BOOL(SpacesInContainerLiterals); 10178 CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); 10179 CHECK_PARSE_BOOL(SpaceAfterCStyleCast); 10180 CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword); 10181 CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); 10182 10183 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass); 10184 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement); 10185 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum); 10186 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction); 10187 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace); 10188 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration); 10189 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct); 10190 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion); 10191 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock); 10192 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch); 10193 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse); 10194 CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces); 10195 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction); 10196 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord); 10197 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace); 10198 } 10199 10200 #undef CHECK_PARSE_BOOL 10201 10202 TEST_F(FormatTest, ParsesConfiguration) { 10203 FormatStyle Style = {}; 10204 Style.Language = FormatStyle::LK_Cpp; 10205 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234); 10206 CHECK_PARSE("ConstructorInitializerIndentWidth: 1234", 10207 ConstructorInitializerIndentWidth, 1234u); 10208 CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u); 10209 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); 10210 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u); 10211 CHECK_PARSE("PenaltyBreakAssignment: 1234", 10212 PenaltyBreakAssignment, 1234u); 10213 CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234", 10214 PenaltyBreakBeforeFirstCallParameter, 1234u); 10215 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); 10216 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234", 10217 PenaltyReturnTypeOnItsOwnLine, 1234u); 10218 CHECK_PARSE("SpacesBeforeTrailingComments: 1234", 10219 SpacesBeforeTrailingComments, 1234u); 10220 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u); 10221 CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u); 10222 CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$"); 10223 10224 Style.PointerAlignment = FormatStyle::PAS_Middle; 10225 CHECK_PARSE("PointerAlignment: Left", PointerAlignment, 10226 FormatStyle::PAS_Left); 10227 CHECK_PARSE("PointerAlignment: Right", PointerAlignment, 10228 FormatStyle::PAS_Right); 10229 CHECK_PARSE("PointerAlignment: Middle", PointerAlignment, 10230 FormatStyle::PAS_Middle); 10231 // For backward compatibility: 10232 CHECK_PARSE("PointerBindsToType: Left", PointerAlignment, 10233 FormatStyle::PAS_Left); 10234 CHECK_PARSE("PointerBindsToType: Right", PointerAlignment, 10235 FormatStyle::PAS_Right); 10236 CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment, 10237 FormatStyle::PAS_Middle); 10238 10239 Style.Standard = FormatStyle::LS_Auto; 10240 CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03); 10241 CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11); 10242 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03); 10243 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11); 10244 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto); 10245 10246 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 10247 CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment", 10248 BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment); 10249 CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators, 10250 FormatStyle::BOS_None); 10251 CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators, 10252 FormatStyle::BOS_All); 10253 // For backward compatibility: 10254 CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators, 10255 FormatStyle::BOS_None); 10256 CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators, 10257 FormatStyle::BOS_All); 10258 10259 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 10260 CHECK_PARSE("BreakConstructorInitializers: BeforeComma", 10261 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 10262 CHECK_PARSE("BreakConstructorInitializers: AfterColon", 10263 BreakConstructorInitializers, FormatStyle::BCIS_AfterColon); 10264 CHECK_PARSE("BreakConstructorInitializers: BeforeColon", 10265 BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon); 10266 // For backward compatibility: 10267 CHECK_PARSE("BreakConstructorInitializersBeforeComma: true", 10268 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 10269 10270 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 10271 CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket, 10272 FormatStyle::BAS_Align); 10273 CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket, 10274 FormatStyle::BAS_DontAlign); 10275 CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket, 10276 FormatStyle::BAS_AlwaysBreak); 10277 // For backward compatibility: 10278 CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket, 10279 FormatStyle::BAS_DontAlign); 10280 CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket, 10281 FormatStyle::BAS_Align); 10282 10283 Style.AlignEscapedNewlines = FormatStyle::ENAS_Left; 10284 CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines, 10285 FormatStyle::ENAS_DontAlign); 10286 CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines, 10287 FormatStyle::ENAS_Left); 10288 CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines, 10289 FormatStyle::ENAS_Right); 10290 // For backward compatibility: 10291 CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines, 10292 FormatStyle::ENAS_Left); 10293 CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines, 10294 FormatStyle::ENAS_Right); 10295 10296 Style.UseTab = FormatStyle::UT_ForIndentation; 10297 CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never); 10298 CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation); 10299 CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always); 10300 CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab, 10301 FormatStyle::UT_ForContinuationAndIndentation); 10302 // For backward compatibility: 10303 CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never); 10304 CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always); 10305 10306 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 10307 CHECK_PARSE("AllowShortFunctionsOnASingleLine: None", 10308 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 10309 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline", 10310 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline); 10311 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty", 10312 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty); 10313 CHECK_PARSE("AllowShortFunctionsOnASingleLine: All", 10314 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 10315 // For backward compatibility: 10316 CHECK_PARSE("AllowShortFunctionsOnASingleLine: false", 10317 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 10318 CHECK_PARSE("AllowShortFunctionsOnASingleLine: true", 10319 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 10320 10321 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 10322 CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens, 10323 FormatStyle::SBPO_Never); 10324 CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens, 10325 FormatStyle::SBPO_Always); 10326 CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens, 10327 FormatStyle::SBPO_ControlStatements); 10328 // For backward compatibility: 10329 CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens, 10330 FormatStyle::SBPO_Never); 10331 CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens, 10332 FormatStyle::SBPO_ControlStatements); 10333 10334 Style.ColumnLimit = 123; 10335 FormatStyle BaseStyle = getLLVMStyle(); 10336 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit); 10337 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u); 10338 10339 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 10340 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces, 10341 FormatStyle::BS_Attach); 10342 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces, 10343 FormatStyle::BS_Linux); 10344 CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces, 10345 FormatStyle::BS_Mozilla); 10346 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces, 10347 FormatStyle::BS_Stroustrup); 10348 CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces, 10349 FormatStyle::BS_Allman); 10350 CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU); 10351 CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces, 10352 FormatStyle::BS_WebKit); 10353 CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces, 10354 FormatStyle::BS_Custom); 10355 10356 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 10357 CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType, 10358 FormatStyle::RTBS_None); 10359 CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType, 10360 FormatStyle::RTBS_All); 10361 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel", 10362 AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel); 10363 CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions", 10364 AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions); 10365 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions", 10366 AlwaysBreakAfterReturnType, 10367 FormatStyle::RTBS_TopLevelDefinitions); 10368 10369 Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All; 10370 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None", 10371 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None); 10372 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All", 10373 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All); 10374 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel", 10375 AlwaysBreakAfterDefinitionReturnType, 10376 FormatStyle::DRTBS_TopLevel); 10377 10378 Style.NamespaceIndentation = FormatStyle::NI_All; 10379 CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation, 10380 FormatStyle::NI_None); 10381 CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation, 10382 FormatStyle::NI_Inner); 10383 CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation, 10384 FormatStyle::NI_All); 10385 10386 // FIXME: This is required because parsing a configuration simply overwrites 10387 // the first N elements of the list instead of resetting it. 10388 Style.ForEachMacros.clear(); 10389 std::vector<std::string> BoostForeach; 10390 BoostForeach.push_back("BOOST_FOREACH"); 10391 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach); 10392 std::vector<std::string> BoostAndQForeach; 10393 BoostAndQForeach.push_back("BOOST_FOREACH"); 10394 BoostAndQForeach.push_back("Q_FOREACH"); 10395 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros, 10396 BoostAndQForeach); 10397 10398 Style.IncludeCategories.clear(); 10399 std::vector<FormatStyle::IncludeCategory> ExpectedCategories = {{"abc/.*", 2}, 10400 {".*", 1}}; 10401 CHECK_PARSE("IncludeCategories:\n" 10402 " - Regex: abc/.*\n" 10403 " Priority: 2\n" 10404 " - Regex: .*\n" 10405 " Priority: 1", 10406 IncludeCategories, ExpectedCategories); 10407 CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeIsMainRegex, "abc$"); 10408 10409 Style.RawStringFormats.clear(); 10410 std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = { 10411 {"pb", FormatStyle::LK_TextProto, "llvm"}, 10412 {"cpp", FormatStyle::LK_Cpp, "google"}}; 10413 10414 CHECK_PARSE("RawStringFormats:\n" 10415 " - Delimiter: 'pb'\n" 10416 " Language: TextProto\n" 10417 " BasedOnStyle: llvm\n" 10418 " - Delimiter: 'cpp'\n" 10419 " Language: Cpp\n" 10420 " BasedOnStyle: google", 10421 RawStringFormats, ExpectedRawStringFormats); 10422 } 10423 10424 TEST_F(FormatTest, ParsesConfigurationWithLanguages) { 10425 FormatStyle Style = {}; 10426 Style.Language = FormatStyle::LK_Cpp; 10427 CHECK_PARSE("Language: Cpp\n" 10428 "IndentWidth: 12", 10429 IndentWidth, 12u); 10430 EXPECT_EQ(parseConfiguration("Language: JavaScript\n" 10431 "IndentWidth: 34", 10432 &Style), 10433 ParseError::Unsuitable); 10434 EXPECT_EQ(12u, Style.IndentWidth); 10435 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 10436 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 10437 10438 Style.Language = FormatStyle::LK_JavaScript; 10439 CHECK_PARSE("Language: JavaScript\n" 10440 "IndentWidth: 12", 10441 IndentWidth, 12u); 10442 CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u); 10443 EXPECT_EQ(parseConfiguration("Language: Cpp\n" 10444 "IndentWidth: 34", 10445 &Style), 10446 ParseError::Unsuitable); 10447 EXPECT_EQ(23u, Style.IndentWidth); 10448 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 10449 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 10450 10451 CHECK_PARSE("BasedOnStyle: LLVM\n" 10452 "IndentWidth: 67", 10453 IndentWidth, 67u); 10454 10455 CHECK_PARSE("---\n" 10456 "Language: JavaScript\n" 10457 "IndentWidth: 12\n" 10458 "---\n" 10459 "Language: Cpp\n" 10460 "IndentWidth: 34\n" 10461 "...\n", 10462 IndentWidth, 12u); 10463 10464 Style.Language = FormatStyle::LK_Cpp; 10465 CHECK_PARSE("---\n" 10466 "Language: JavaScript\n" 10467 "IndentWidth: 12\n" 10468 "---\n" 10469 "Language: Cpp\n" 10470 "IndentWidth: 34\n" 10471 "...\n", 10472 IndentWidth, 34u); 10473 CHECK_PARSE("---\n" 10474 "IndentWidth: 78\n" 10475 "---\n" 10476 "Language: JavaScript\n" 10477 "IndentWidth: 56\n" 10478 "...\n", 10479 IndentWidth, 78u); 10480 10481 Style.ColumnLimit = 123; 10482 Style.IndentWidth = 234; 10483 Style.BreakBeforeBraces = FormatStyle::BS_Linux; 10484 Style.TabWidth = 345; 10485 EXPECT_FALSE(parseConfiguration("---\n" 10486 "IndentWidth: 456\n" 10487 "BreakBeforeBraces: Allman\n" 10488 "---\n" 10489 "Language: JavaScript\n" 10490 "IndentWidth: 111\n" 10491 "TabWidth: 111\n" 10492 "---\n" 10493 "Language: Cpp\n" 10494 "BreakBeforeBraces: Stroustrup\n" 10495 "TabWidth: 789\n" 10496 "...\n", 10497 &Style)); 10498 EXPECT_EQ(123u, Style.ColumnLimit); 10499 EXPECT_EQ(456u, Style.IndentWidth); 10500 EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces); 10501 EXPECT_EQ(789u, Style.TabWidth); 10502 10503 EXPECT_EQ(parseConfiguration("---\n" 10504 "Language: JavaScript\n" 10505 "IndentWidth: 56\n" 10506 "---\n" 10507 "IndentWidth: 78\n" 10508 "...\n", 10509 &Style), 10510 ParseError::Error); 10511 EXPECT_EQ(parseConfiguration("---\n" 10512 "Language: JavaScript\n" 10513 "IndentWidth: 56\n" 10514 "---\n" 10515 "Language: JavaScript\n" 10516 "IndentWidth: 78\n" 10517 "...\n", 10518 &Style), 10519 ParseError::Error); 10520 10521 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 10522 } 10523 10524 #undef CHECK_PARSE 10525 10526 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) { 10527 FormatStyle Style = {}; 10528 Style.Language = FormatStyle::LK_JavaScript; 10529 Style.BreakBeforeTernaryOperators = true; 10530 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value()); 10531 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 10532 10533 Style.BreakBeforeTernaryOperators = true; 10534 EXPECT_EQ(0, parseConfiguration("---\n" 10535 "BasedOnStyle: Google\n" 10536 "---\n" 10537 "Language: JavaScript\n" 10538 "IndentWidth: 76\n" 10539 "...\n", 10540 &Style) 10541 .value()); 10542 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 10543 EXPECT_EQ(76u, Style.IndentWidth); 10544 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 10545 } 10546 10547 TEST_F(FormatTest, ConfigurationRoundTripTest) { 10548 FormatStyle Style = getLLVMStyle(); 10549 std::string YAML = configurationAsText(Style); 10550 FormatStyle ParsedStyle = {}; 10551 ParsedStyle.Language = FormatStyle::LK_Cpp; 10552 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value()); 10553 EXPECT_EQ(Style, ParsedStyle); 10554 } 10555 10556 TEST_F(FormatTest, WorksFor8bitEncodings) { 10557 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" 10558 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" 10559 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" 10560 "\"\xef\xee\xf0\xf3...\"", 10561 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " 10562 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " 10563 "\xef\xee\xf0\xf3...\"", 10564 getLLVMStyleWithColumns(12))); 10565 } 10566 10567 TEST_F(FormatTest, HandlesUTF8BOM) { 10568 EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf")); 10569 EXPECT_EQ("\xef\xbb\xbf#include <iostream>", 10570 format("\xef\xbb\xbf#include <iostream>")); 10571 EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>", 10572 format("\xef\xbb\xbf\n#include <iostream>")); 10573 } 10574 10575 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. 10576 #if !defined(_MSC_VER) 10577 10578 TEST_F(FormatTest, CountsUTF8CharactersProperly) { 10579 verifyFormat("\"Однажды в студёную зимнюю пору...\"", 10580 getLLVMStyleWithColumns(35)); 10581 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", 10582 getLLVMStyleWithColumns(31)); 10583 verifyFormat("// Однажды в студёную зимнюю пору...", 10584 getLLVMStyleWithColumns(36)); 10585 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32)); 10586 verifyFormat("/* Однажды в студёную зимнюю пору... */", 10587 getLLVMStyleWithColumns(39)); 10588 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", 10589 getLLVMStyleWithColumns(35)); 10590 } 10591 10592 TEST_F(FormatTest, SplitsUTF8Strings) { 10593 // Non-printable characters' width is currently considered to be the length in 10594 // bytes in UTF8. The characters can be displayed in very different manner 10595 // (zero-width, single width with a substitution glyph, expanded to their code 10596 // (e.g. "<8d>"), so there's no single correct way to handle them. 10597 EXPECT_EQ("\"aaaaÄ\"\n" 10598 "\"\xc2\x8d\";", 10599 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 10600 EXPECT_EQ("\"aaaaaaaÄ\"\n" 10601 "\"\xc2\x8d\";", 10602 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 10603 EXPECT_EQ("\"Однажды, в \"\n" 10604 "\"студёную \"\n" 10605 "\"зимнюю \"\n" 10606 "\"пору,\"", 10607 format("\"Однажды, в студёную зимнюю пору,\"", 10608 getLLVMStyleWithColumns(13))); 10609 EXPECT_EQ( 10610 "\"一 二 三 \"\n" 10611 "\"四 五六 \"\n" 10612 "\"七 八 九 \"\n" 10613 "\"十\"", 10614 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); 10615 EXPECT_EQ("\"一\t\"\n" 10616 "\"二 \t\"\n" 10617 "\"三 四 \"\n" 10618 "\"五\t\"\n" 10619 "\"六 \t\"\n" 10620 "\"七 \"\n" 10621 "\"八九十\tqq\"", 10622 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", 10623 getLLVMStyleWithColumns(11))); 10624 10625 // UTF8 character in an escape sequence. 10626 EXPECT_EQ("\"aaaaaa\"\n" 10627 "\"\\\xC2\x8D\"", 10628 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10))); 10629 } 10630 10631 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { 10632 EXPECT_EQ("const char *sssss =\n" 10633 " \"一二三四五六七八\\\n" 10634 " 九 十\";", 10635 format("const char *sssss = \"一二三四五六七八\\\n" 10636 " 九 十\";", 10637 getLLVMStyleWithColumns(30))); 10638 } 10639 10640 TEST_F(FormatTest, SplitsUTF8LineComments) { 10641 EXPECT_EQ("// aaaaÄ\xc2\x8d", 10642 format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10))); 10643 EXPECT_EQ("// Я из лесу\n" 10644 "// вышел; был\n" 10645 "// сильный\n" 10646 "// мороз.", 10647 format("// Я из лесу вышел; был сильный мороз.", 10648 getLLVMStyleWithColumns(13))); 10649 EXPECT_EQ("// 一二三\n" 10650 "// 四五六七\n" 10651 "// 八 九\n" 10652 "// 十", 10653 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9))); 10654 } 10655 10656 TEST_F(FormatTest, SplitsUTF8BlockComments) { 10657 EXPECT_EQ("/* Гляжу,\n" 10658 " * поднимается\n" 10659 " * медленно в\n" 10660 " * гору\n" 10661 " * Лошадка,\n" 10662 " * везущая\n" 10663 " * хворосту\n" 10664 " * воз. */", 10665 format("/* Гляжу, поднимается медленно в гору\n" 10666 " * Лошадка, везущая хворосту воз. */", 10667 getLLVMStyleWithColumns(13))); 10668 EXPECT_EQ( 10669 "/* 一二三\n" 10670 " * 四五六七\n" 10671 " * 八 九\n" 10672 " * 十 */", 10673 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9))); 10674 EXPECT_EQ("/* \n" 10675 " * \n" 10676 " * - */", 10677 format("/* - */", getLLVMStyleWithColumns(12))); 10678 } 10679 10680 #endif // _MSC_VER 10681 10682 TEST_F(FormatTest, ConstructorInitializerIndentWidth) { 10683 FormatStyle Style = getLLVMStyle(); 10684 10685 Style.ConstructorInitializerIndentWidth = 4; 10686 verifyFormat( 10687 "SomeClass::Constructor()\n" 10688 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10689 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10690 Style); 10691 10692 Style.ConstructorInitializerIndentWidth = 2; 10693 verifyFormat( 10694 "SomeClass::Constructor()\n" 10695 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10696 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10697 Style); 10698 10699 Style.ConstructorInitializerIndentWidth = 0; 10700 verifyFormat( 10701 "SomeClass::Constructor()\n" 10702 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10703 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10704 Style); 10705 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 10706 verifyFormat( 10707 "SomeLongTemplateVariableName<\n" 10708 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>", 10709 Style); 10710 verifyFormat( 10711 "bool smaller = 1 < bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 10712 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 10713 Style); 10714 } 10715 10716 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) { 10717 FormatStyle Style = getLLVMStyle(); 10718 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 10719 Style.ConstructorInitializerIndentWidth = 4; 10720 verifyFormat("SomeClass::Constructor()\n" 10721 " : a(a)\n" 10722 " , b(b)\n" 10723 " , c(c) {}", 10724 Style); 10725 verifyFormat("SomeClass::Constructor()\n" 10726 " : a(a) {}", 10727 Style); 10728 10729 Style.ColumnLimit = 0; 10730 verifyFormat("SomeClass::Constructor()\n" 10731 " : a(a) {}", 10732 Style); 10733 verifyFormat("SomeClass::Constructor() noexcept\n" 10734 " : a(a) {}", 10735 Style); 10736 verifyFormat("SomeClass::Constructor()\n" 10737 " : a(a)\n" 10738 " , b(b)\n" 10739 " , c(c) {}", 10740 Style); 10741 verifyFormat("SomeClass::Constructor()\n" 10742 " : a(a) {\n" 10743 " foo();\n" 10744 " bar();\n" 10745 "}", 10746 Style); 10747 10748 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 10749 verifyFormat("SomeClass::Constructor()\n" 10750 " : a(a)\n" 10751 " , b(b)\n" 10752 " , c(c) {\n}", 10753 Style); 10754 verifyFormat("SomeClass::Constructor()\n" 10755 " : a(a) {\n}", 10756 Style); 10757 10758 Style.ColumnLimit = 80; 10759 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 10760 Style.ConstructorInitializerIndentWidth = 2; 10761 verifyFormat("SomeClass::Constructor()\n" 10762 " : a(a)\n" 10763 " , b(b)\n" 10764 " , c(c) {}", 10765 Style); 10766 10767 Style.ConstructorInitializerIndentWidth = 0; 10768 verifyFormat("SomeClass::Constructor()\n" 10769 ": a(a)\n" 10770 ", b(b)\n" 10771 ", c(c) {}", 10772 Style); 10773 10774 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 10775 Style.ConstructorInitializerIndentWidth = 4; 10776 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style); 10777 verifyFormat( 10778 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n", 10779 Style); 10780 verifyFormat( 10781 "SomeClass::Constructor()\n" 10782 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", 10783 Style); 10784 Style.ConstructorInitializerIndentWidth = 4; 10785 Style.ColumnLimit = 60; 10786 verifyFormat("SomeClass::Constructor()\n" 10787 " : aaaaaaaa(aaaaaaaa)\n" 10788 " , aaaaaaaa(aaaaaaaa)\n" 10789 " , aaaaaaaa(aaaaaaaa) {}", 10790 Style); 10791 } 10792 10793 TEST_F(FormatTest, Destructors) { 10794 verifyFormat("void F(int &i) { i.~int(); }"); 10795 verifyFormat("void F(int &i) { i->~int(); }"); 10796 } 10797 10798 TEST_F(FormatTest, FormatsWithWebKitStyle) { 10799 FormatStyle Style = getWebKitStyle(); 10800 10801 // Don't indent in outer namespaces. 10802 verifyFormat("namespace outer {\n" 10803 "int i;\n" 10804 "namespace inner {\n" 10805 " int i;\n" 10806 "} // namespace inner\n" 10807 "} // namespace outer\n" 10808 "namespace other_outer {\n" 10809 "int i;\n" 10810 "}", 10811 Style); 10812 10813 // Don't indent case labels. 10814 verifyFormat("switch (variable) {\n" 10815 "case 1:\n" 10816 "case 2:\n" 10817 " doSomething();\n" 10818 " break;\n" 10819 "default:\n" 10820 " ++variable;\n" 10821 "}", 10822 Style); 10823 10824 // Wrap before binary operators. 10825 EXPECT_EQ("void f()\n" 10826 "{\n" 10827 " if (aaaaaaaaaaaaaaaa\n" 10828 " && bbbbbbbbbbbbbbbbbbbbbbbb\n" 10829 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 10830 " return;\n" 10831 "}", 10832 format("void f() {\n" 10833 "if (aaaaaaaaaaaaaaaa\n" 10834 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" 10835 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 10836 "return;\n" 10837 "}", 10838 Style)); 10839 10840 // Allow functions on a single line. 10841 verifyFormat("void f() { return; }", Style); 10842 10843 // Constructor initializers are formatted one per line with the "," on the 10844 // new line. 10845 verifyFormat("Constructor()\n" 10846 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 10847 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" 10848 " aaaaaaaaaaaaaa)\n" 10849 " , aaaaaaaaaaaaaaaaaaaaaaa()\n" 10850 "{\n" 10851 "}", 10852 Style); 10853 verifyFormat("SomeClass::Constructor()\n" 10854 " : a(a)\n" 10855 "{\n" 10856 "}", 10857 Style); 10858 EXPECT_EQ("SomeClass::Constructor()\n" 10859 " : a(a)\n" 10860 "{\n" 10861 "}", 10862 format("SomeClass::Constructor():a(a){}", Style)); 10863 verifyFormat("SomeClass::Constructor()\n" 10864 " : a(a)\n" 10865 " , b(b)\n" 10866 " , c(c)\n" 10867 "{\n" 10868 "}", 10869 Style); 10870 verifyFormat("SomeClass::Constructor()\n" 10871 " : a(a)\n" 10872 "{\n" 10873 " foo();\n" 10874 " bar();\n" 10875 "}", 10876 Style); 10877 10878 // Access specifiers should be aligned left. 10879 verifyFormat("class C {\n" 10880 "public:\n" 10881 " int i;\n" 10882 "};", 10883 Style); 10884 10885 // Do not align comments. 10886 verifyFormat("int a; // Do not\n" 10887 "double b; // align comments.", 10888 Style); 10889 10890 // Do not align operands. 10891 EXPECT_EQ("ASSERT(aaaa\n" 10892 " || bbbb);", 10893 format("ASSERT ( aaaa\n||bbbb);", Style)); 10894 10895 // Accept input's line breaks. 10896 EXPECT_EQ("if (aaaaaaaaaaaaaaa\n" 10897 " || bbbbbbbbbbbbbbb) {\n" 10898 " i++;\n" 10899 "}", 10900 format("if (aaaaaaaaaaaaaaa\n" 10901 "|| bbbbbbbbbbbbbbb) { i++; }", 10902 Style)); 10903 EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n" 10904 " i++;\n" 10905 "}", 10906 format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style)); 10907 10908 // Don't automatically break all macro definitions (llvm.org/PR17842). 10909 verifyFormat("#define aNumber 10", Style); 10910 // However, generally keep the line breaks that the user authored. 10911 EXPECT_EQ("#define aNumber \\\n" 10912 " 10", 10913 format("#define aNumber \\\n" 10914 " 10", 10915 Style)); 10916 10917 // Keep empty and one-element array literals on a single line. 10918 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n" 10919 " copyItems:YES];", 10920 format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n" 10921 "copyItems:YES];", 10922 Style)); 10923 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n" 10924 " copyItems:YES];", 10925 format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n" 10926 " copyItems:YES];", 10927 Style)); 10928 // FIXME: This does not seem right, there should be more indentation before 10929 // the array literal's entries. Nested blocks have the same problem. 10930 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 10931 " @\"a\",\n" 10932 " @\"a\"\n" 10933 "]\n" 10934 " copyItems:YES];", 10935 format("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 10936 " @\"a\",\n" 10937 " @\"a\"\n" 10938 " ]\n" 10939 " copyItems:YES];", 10940 Style)); 10941 EXPECT_EQ( 10942 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 10943 " copyItems:YES];", 10944 format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 10945 " copyItems:YES];", 10946 Style)); 10947 10948 verifyFormat("[self.a b:c c:d];", Style); 10949 EXPECT_EQ("[self.a b:c\n" 10950 " c:d];", 10951 format("[self.a b:c\n" 10952 "c:d];", 10953 Style)); 10954 } 10955 10956 TEST_F(FormatTest, FormatsLambdas) { 10957 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n"); 10958 verifyFormat("int c = [&] { [=] { return b++; }(); }();\n"); 10959 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n"); 10960 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n"); 10961 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n"); 10962 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n"); 10963 verifyFormat("auto c = [a = [b = 42] {}] {};\n"); 10964 verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n"); 10965 verifyFormat("int x = f(*+[] {});"); 10966 verifyFormat("void f() {\n" 10967 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n" 10968 "}\n"); 10969 verifyFormat("void f() {\n" 10970 " other(x.begin(), //\n" 10971 " x.end(), //\n" 10972 " [&](int, int) { return 1; });\n" 10973 "}\n"); 10974 verifyFormat("SomeFunction([]() { // A cool function...\n" 10975 " return 43;\n" 10976 "});"); 10977 EXPECT_EQ("SomeFunction([]() {\n" 10978 "#define A a\n" 10979 " return 43;\n" 10980 "});", 10981 format("SomeFunction([](){\n" 10982 "#define A a\n" 10983 "return 43;\n" 10984 "});")); 10985 verifyFormat("void f() {\n" 10986 " SomeFunction([](decltype(x), A *a) {});\n" 10987 "}"); 10988 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10989 " [](const aaaaaaaaaa &a) { return a; });"); 10990 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n" 10991 " SomeOtherFunctioooooooooooooooooooooooooon();\n" 10992 "});"); 10993 verifyFormat("Constructor()\n" 10994 " : Field([] { // comment\n" 10995 " int i;\n" 10996 " }) {}"); 10997 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n" 10998 " return some_parameter.size();\n" 10999 "};"); 11000 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n" 11001 " [](const string &s) { return s; };"); 11002 verifyFormat("int i = aaaaaa ? 1 //\n" 11003 " : [] {\n" 11004 " return 2; //\n" 11005 " }();"); 11006 verifyFormat("llvm::errs() << \"number of twos is \"\n" 11007 " << std::count_if(v.begin(), v.end(), [](int x) {\n" 11008 " return x == 2; // force break\n" 11009 " });"); 11010 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 11011 " [=](int iiiiiiiiiiii) {\n" 11012 " return aaaaaaaaaaaaaaaaaaaaaaa !=\n" 11013 " aaaaaaaaaaaaaaaaaaaaaaa;\n" 11014 " });", 11015 getLLVMStyleWithColumns(60)); 11016 verifyFormat("SomeFunction({[&] {\n" 11017 " // comment\n" 11018 " },\n" 11019 " [&] {\n" 11020 " // comment\n" 11021 " }});"); 11022 verifyFormat("SomeFunction({[&] {\n" 11023 " // comment\n" 11024 "}});"); 11025 verifyFormat("virtual aaaaaaaaaaaaaaaa(std::function<bool()> bbbbbbbbbbbb =\n" 11026 " [&]() { return true; },\n" 11027 " aaaaa aaaaaaaaa);"); 11028 11029 // Lambdas with return types. 11030 verifyFormat("int c = []() -> int { return 2; }();\n"); 11031 verifyFormat("int c = []() -> int * { return 2; }();\n"); 11032 verifyFormat("int c = []() -> vector<int> { return {2}; }();\n"); 11033 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());"); 11034 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};"); 11035 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};"); 11036 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};"); 11037 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};"); 11038 verifyFormat("[a, a]() -> a<1> {};"); 11039 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n" 11040 " int j) -> int {\n" 11041 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n" 11042 "};"); 11043 verifyFormat( 11044 "aaaaaaaaaaaaaaaaaaaaaa(\n" 11045 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n" 11046 " return aaaaaaaaaaaaaaaaa;\n" 11047 " });", 11048 getLLVMStyleWithColumns(70)); 11049 verifyFormat("[]() //\n" 11050 " -> int {\n" 11051 " return 1; //\n" 11052 "};"); 11053 11054 // Multiple lambdas in the same parentheses change indentation rules. 11055 verifyFormat("SomeFunction(\n" 11056 " []() {\n" 11057 " int i = 42;\n" 11058 " return i;\n" 11059 " },\n" 11060 " []() {\n" 11061 " int j = 43;\n" 11062 " return j;\n" 11063 " });"); 11064 11065 // More complex introducers. 11066 verifyFormat("return [i, args...] {};"); 11067 11068 // Not lambdas. 11069 verifyFormat("constexpr char hello[]{\"hello\"};"); 11070 verifyFormat("double &operator[](int i) { return 0; }\n" 11071 "int i;"); 11072 verifyFormat("std::unique_ptr<int[]> foo() {}"); 11073 verifyFormat("int i = a[a][a]->f();"); 11074 verifyFormat("int i = (*b)[a]->f();"); 11075 11076 // Other corner cases. 11077 verifyFormat("void f() {\n" 11078 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n" 11079 " );\n" 11080 "}"); 11081 11082 // Lambdas created through weird macros. 11083 verifyFormat("void f() {\n" 11084 " MACRO((const AA &a) { return 1; });\n" 11085 " MACRO((AA &a) { return 1; });\n" 11086 "}"); 11087 11088 verifyFormat("if (blah_blah(whatever, whatever, [] {\n" 11089 " doo_dah();\n" 11090 " doo_dah();\n" 11091 " })) {\n" 11092 "}"); 11093 verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n" 11094 " doo_dah();\n" 11095 " doo_dah();\n" 11096 " })) {\n" 11097 "}"); 11098 verifyFormat("auto lambda = []() {\n" 11099 " int a = 2\n" 11100 "#if A\n" 11101 " + 2\n" 11102 "#endif\n" 11103 " ;\n" 11104 "};"); 11105 11106 // Lambdas with complex multiline introducers. 11107 verifyFormat( 11108 "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 11109 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n" 11110 " -> ::std::unordered_set<\n" 11111 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n" 11112 " //\n" 11113 " });"); 11114 } 11115 11116 TEST_F(FormatTest, EmptyLinesInLambdas) { 11117 verifyFormat("auto lambda = []() {\n" 11118 " x(); //\n" 11119 "};", 11120 "auto lambda = []() {\n" 11121 "\n" 11122 " x(); //\n" 11123 "\n" 11124 "};"); 11125 } 11126 11127 TEST_F(FormatTest, FormatsBlocks) { 11128 FormatStyle ShortBlocks = getLLVMStyle(); 11129 ShortBlocks.AllowShortBlocksOnASingleLine = true; 11130 verifyFormat("int (^Block)(int, int);", ShortBlocks); 11131 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks); 11132 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks); 11133 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks); 11134 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks); 11135 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks); 11136 11137 verifyFormat("foo(^{ bar(); });", ShortBlocks); 11138 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks); 11139 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks); 11140 11141 verifyFormat("[operation setCompletionBlock:^{\n" 11142 " [self onOperationDone];\n" 11143 "}];"); 11144 verifyFormat("int i = {[operation setCompletionBlock:^{\n" 11145 " [self onOperationDone];\n" 11146 "}]};"); 11147 verifyFormat("[operation setCompletionBlock:^(int *i) {\n" 11148 " f();\n" 11149 "}];"); 11150 verifyFormat("int a = [operation block:^int(int *i) {\n" 11151 " return 1;\n" 11152 "}];"); 11153 verifyFormat("[myObject doSomethingWith:arg1\n" 11154 " aaa:^int(int *a) {\n" 11155 " return 1;\n" 11156 " }\n" 11157 " bbb:f(a * bbbbbbbb)];"); 11158 11159 verifyFormat("[operation setCompletionBlock:^{\n" 11160 " [self.delegate newDataAvailable];\n" 11161 "}];", 11162 getLLVMStyleWithColumns(60)); 11163 verifyFormat("dispatch_async(_fileIOQueue, ^{\n" 11164 " NSString *path = [self sessionFilePath];\n" 11165 " if (path) {\n" 11166 " // ...\n" 11167 " }\n" 11168 "});"); 11169 verifyFormat("[[SessionService sharedService]\n" 11170 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 11171 " if (window) {\n" 11172 " [self windowDidLoad:window];\n" 11173 " } else {\n" 11174 " [self errorLoadingWindow];\n" 11175 " }\n" 11176 " }];"); 11177 verifyFormat("void (^largeBlock)(void) = ^{\n" 11178 " // ...\n" 11179 "};\n", 11180 getLLVMStyleWithColumns(40)); 11181 verifyFormat("[[SessionService sharedService]\n" 11182 " loadWindowWithCompletionBlock: //\n" 11183 " ^(SessionWindow *window) {\n" 11184 " if (window) {\n" 11185 " [self windowDidLoad:window];\n" 11186 " } else {\n" 11187 " [self errorLoadingWindow];\n" 11188 " }\n" 11189 " }];", 11190 getLLVMStyleWithColumns(60)); 11191 verifyFormat("[myObject doSomethingWith:arg1\n" 11192 " firstBlock:^(Foo *a) {\n" 11193 " // ...\n" 11194 " int i;\n" 11195 " }\n" 11196 " secondBlock:^(Bar *b) {\n" 11197 " // ...\n" 11198 " int i;\n" 11199 " }\n" 11200 " thirdBlock:^Foo(Bar *b) {\n" 11201 " // ...\n" 11202 " int i;\n" 11203 " }];"); 11204 verifyFormat("[myObject doSomethingWith:arg1\n" 11205 " firstBlock:-1\n" 11206 " secondBlock:^(Bar *b) {\n" 11207 " // ...\n" 11208 " int i;\n" 11209 " }];"); 11210 11211 verifyFormat("f(^{\n" 11212 " @autoreleasepool {\n" 11213 " if (a) {\n" 11214 " g();\n" 11215 " }\n" 11216 " }\n" 11217 "});"); 11218 verifyFormat("Block b = ^int *(A *a, B *b) {}"); 11219 verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n" 11220 "};"); 11221 11222 FormatStyle FourIndent = getLLVMStyle(); 11223 FourIndent.ObjCBlockIndentWidth = 4; 11224 verifyFormat("[operation setCompletionBlock:^{\n" 11225 " [self onOperationDone];\n" 11226 "}];", 11227 FourIndent); 11228 } 11229 11230 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) { 11231 FormatStyle ZeroColumn = getLLVMStyle(); 11232 ZeroColumn.ColumnLimit = 0; 11233 11234 verifyFormat("[[SessionService sharedService] " 11235 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 11236 " if (window) {\n" 11237 " [self windowDidLoad:window];\n" 11238 " } else {\n" 11239 " [self errorLoadingWindow];\n" 11240 " }\n" 11241 "}];", 11242 ZeroColumn); 11243 EXPECT_EQ("[[SessionService sharedService]\n" 11244 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 11245 " if (window) {\n" 11246 " [self windowDidLoad:window];\n" 11247 " } else {\n" 11248 " [self errorLoadingWindow];\n" 11249 " }\n" 11250 " }];", 11251 format("[[SessionService sharedService]\n" 11252 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 11253 " if (window) {\n" 11254 " [self windowDidLoad:window];\n" 11255 " } else {\n" 11256 " [self errorLoadingWindow];\n" 11257 " }\n" 11258 "}];", 11259 ZeroColumn)); 11260 verifyFormat("[myObject doSomethingWith:arg1\n" 11261 " firstBlock:^(Foo *a) {\n" 11262 " // ...\n" 11263 " int i;\n" 11264 " }\n" 11265 " secondBlock:^(Bar *b) {\n" 11266 " // ...\n" 11267 " int i;\n" 11268 " }\n" 11269 " thirdBlock:^Foo(Bar *b) {\n" 11270 " // ...\n" 11271 " int i;\n" 11272 " }];", 11273 ZeroColumn); 11274 verifyFormat("f(^{\n" 11275 " @autoreleasepool {\n" 11276 " if (a) {\n" 11277 " g();\n" 11278 " }\n" 11279 " }\n" 11280 "});", 11281 ZeroColumn); 11282 verifyFormat("void (^largeBlock)(void) = ^{\n" 11283 " // ...\n" 11284 "};", 11285 ZeroColumn); 11286 11287 ZeroColumn.AllowShortBlocksOnASingleLine = true; 11288 EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };", 11289 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 11290 ZeroColumn.AllowShortBlocksOnASingleLine = false; 11291 EXPECT_EQ("void (^largeBlock)(void) = ^{\n" 11292 " int i;\n" 11293 "};", 11294 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 11295 } 11296 11297 TEST_F(FormatTest, SupportsCRLF) { 11298 EXPECT_EQ("int a;\r\n" 11299 "int b;\r\n" 11300 "int c;\r\n", 11301 format("int a;\r\n" 11302 " int b;\r\n" 11303 " int c;\r\n", 11304 getLLVMStyle())); 11305 EXPECT_EQ("int a;\r\n" 11306 "int b;\r\n" 11307 "int c;\r\n", 11308 format("int a;\r\n" 11309 " int b;\n" 11310 " int c;\r\n", 11311 getLLVMStyle())); 11312 EXPECT_EQ("int a;\n" 11313 "int b;\n" 11314 "int c;\n", 11315 format("int a;\r\n" 11316 " int b;\n" 11317 " int c;\n", 11318 getLLVMStyle())); 11319 EXPECT_EQ("\"aaaaaaa \"\r\n" 11320 "\"bbbbbbb\";\r\n", 11321 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10))); 11322 EXPECT_EQ("#define A \\\r\n" 11323 " b; \\\r\n" 11324 " c; \\\r\n" 11325 " d;\r\n", 11326 format("#define A \\\r\n" 11327 " b; \\\r\n" 11328 " c; d; \r\n", 11329 getGoogleStyle())); 11330 11331 EXPECT_EQ("/*\r\n" 11332 "multi line block comments\r\n" 11333 "should not introduce\r\n" 11334 "an extra carriage return\r\n" 11335 "*/\r\n", 11336 format("/*\r\n" 11337 "multi line block comments\r\n" 11338 "should not introduce\r\n" 11339 "an extra carriage return\r\n" 11340 "*/\r\n")); 11341 } 11342 11343 TEST_F(FormatTest, MunchSemicolonAfterBlocks) { 11344 verifyFormat("MY_CLASS(C) {\n" 11345 " int i;\n" 11346 " int j;\n" 11347 "};"); 11348 } 11349 11350 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) { 11351 FormatStyle TwoIndent = getLLVMStyleWithColumns(15); 11352 TwoIndent.ContinuationIndentWidth = 2; 11353 11354 EXPECT_EQ("int i =\n" 11355 " longFunction(\n" 11356 " arg);", 11357 format("int i = longFunction(arg);", TwoIndent)); 11358 11359 FormatStyle SixIndent = getLLVMStyleWithColumns(20); 11360 SixIndent.ContinuationIndentWidth = 6; 11361 11362 EXPECT_EQ("int i =\n" 11363 " longFunction(\n" 11364 " arg);", 11365 format("int i = longFunction(arg);", SixIndent)); 11366 } 11367 11368 TEST_F(FormatTest, SpacesInAngles) { 11369 FormatStyle Spaces = getLLVMStyle(); 11370 Spaces.SpacesInAngles = true; 11371 11372 verifyFormat("static_cast< int >(arg);", Spaces); 11373 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces); 11374 verifyFormat("f< int, float >();", Spaces); 11375 verifyFormat("template <> g() {}", Spaces); 11376 verifyFormat("template < std::vector< int > > f() {}", Spaces); 11377 verifyFormat("std::function< void(int, int) > fct;", Spaces); 11378 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }", 11379 Spaces); 11380 11381 Spaces.Standard = FormatStyle::LS_Cpp03; 11382 Spaces.SpacesInAngles = true; 11383 verifyFormat("A< A< int > >();", Spaces); 11384 11385 Spaces.SpacesInAngles = false; 11386 verifyFormat("A<A<int> >();", Spaces); 11387 11388 Spaces.Standard = FormatStyle::LS_Cpp11; 11389 Spaces.SpacesInAngles = true; 11390 verifyFormat("A< A< int > >();", Spaces); 11391 11392 Spaces.SpacesInAngles = false; 11393 verifyFormat("A<A<int>>();", Spaces); 11394 } 11395 11396 TEST_F(FormatTest, SpaceAfterTemplateKeyword) { 11397 FormatStyle Style = getLLVMStyle(); 11398 Style.SpaceAfterTemplateKeyword = false; 11399 verifyFormat("template<int> void foo();", Style); 11400 } 11401 11402 TEST_F(FormatTest, TripleAngleBrackets) { 11403 verifyFormat("f<<<1, 1>>>();"); 11404 verifyFormat("f<<<1, 1, 1, s>>>();"); 11405 verifyFormat("f<<<a, b, c, d>>>();"); 11406 EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();")); 11407 verifyFormat("f<param><<<1, 1>>>();"); 11408 verifyFormat("f<1><<<1, 1>>>();"); 11409 EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();")); 11410 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 11411 "aaaaaaaaaaa<<<\n 1, 1>>>();"); 11412 verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n" 11413 " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();"); 11414 } 11415 11416 TEST_F(FormatTest, MergeLessLessAtEnd) { 11417 verifyFormat("<<"); 11418 EXPECT_EQ("< < <", format("\\\n<<<")); 11419 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 11420 "aaallvm::outs() <<"); 11421 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 11422 "aaaallvm::outs()\n <<"); 11423 } 11424 11425 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) { 11426 std::string code = "#if A\n" 11427 "#if B\n" 11428 "a.\n" 11429 "#endif\n" 11430 " a = 1;\n" 11431 "#else\n" 11432 "#endif\n" 11433 "#if C\n" 11434 "#else\n" 11435 "#endif\n"; 11436 EXPECT_EQ(code, format(code)); 11437 } 11438 11439 TEST_F(FormatTest, HandleConflictMarkers) { 11440 // Git/SVN conflict markers. 11441 EXPECT_EQ("int a;\n" 11442 "void f() {\n" 11443 " callme(some(parameter1,\n" 11444 "<<<<<<< text by the vcs\n" 11445 " parameter2),\n" 11446 "||||||| text by the vcs\n" 11447 " parameter2),\n" 11448 " parameter3,\n" 11449 "======= text by the vcs\n" 11450 " parameter2, parameter3),\n" 11451 ">>>>>>> text by the vcs\n" 11452 " otherparameter);\n", 11453 format("int a;\n" 11454 "void f() {\n" 11455 " callme(some(parameter1,\n" 11456 "<<<<<<< text by the vcs\n" 11457 " parameter2),\n" 11458 "||||||| text by the vcs\n" 11459 " parameter2),\n" 11460 " parameter3,\n" 11461 "======= text by the vcs\n" 11462 " parameter2,\n" 11463 " parameter3),\n" 11464 ">>>>>>> text by the vcs\n" 11465 " otherparameter);\n")); 11466 11467 // Perforce markers. 11468 EXPECT_EQ("void f() {\n" 11469 " function(\n" 11470 ">>>> text by the vcs\n" 11471 " parameter,\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 format("void f() {\n" 11479 " function(\n" 11480 ">>>> text by the vcs\n" 11481 " parameter,\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 11489 EXPECT_EQ("<<<<<<<\n" 11490 "|||||||\n" 11491 "=======\n" 11492 ">>>>>>>", 11493 format("<<<<<<<\n" 11494 "|||||||\n" 11495 "=======\n" 11496 ">>>>>>>")); 11497 11498 EXPECT_EQ("<<<<<<<\n" 11499 "|||||||\n" 11500 "int i;\n" 11501 "=======\n" 11502 ">>>>>>>", 11503 format("<<<<<<<\n" 11504 "|||||||\n" 11505 "int i;\n" 11506 "=======\n" 11507 ">>>>>>>")); 11508 11509 // FIXME: Handle parsing of macros around conflict markers correctly: 11510 EXPECT_EQ("#define Macro \\\n" 11511 "<<<<<<<\n" 11512 "Something \\\n" 11513 "|||||||\n" 11514 "Else \\\n" 11515 "=======\n" 11516 "Other \\\n" 11517 ">>>>>>>\n" 11518 " End int i;\n", 11519 format("#define Macro \\\n" 11520 "<<<<<<<\n" 11521 " Something \\\n" 11522 "|||||||\n" 11523 " Else \\\n" 11524 "=======\n" 11525 " Other \\\n" 11526 ">>>>>>>\n" 11527 " End\n" 11528 "int i;\n")); 11529 } 11530 11531 TEST_F(FormatTest, DisableRegions) { 11532 EXPECT_EQ("int i;\n" 11533 "// clang-format off\n" 11534 " int j;\n" 11535 "// clang-format on\n" 11536 "int k;", 11537 format(" int i;\n" 11538 " // clang-format off\n" 11539 " int j;\n" 11540 " // clang-format on\n" 11541 " int k;")); 11542 EXPECT_EQ("int i;\n" 11543 "/* clang-format off */\n" 11544 " int j;\n" 11545 "/* clang-format on */\n" 11546 "int k;", 11547 format(" int i;\n" 11548 " /* clang-format off */\n" 11549 " int j;\n" 11550 " /* clang-format on */\n" 11551 " int k;")); 11552 11553 // Don't reflow comments within disabled regions. 11554 EXPECT_EQ( 11555 "// clang-format off\n" 11556 "// long long long long long long line\n" 11557 "/* clang-format on */\n" 11558 "/* long long long\n" 11559 " * long long long\n" 11560 " * line */\n" 11561 "int i;\n" 11562 "/* clang-format off */\n" 11563 "/* long long long long long long line */\n", 11564 format("// clang-format off\n" 11565 "// long long long long long long line\n" 11566 "/* clang-format on */\n" 11567 "/* long long long long long long line */\n" 11568 "int i;\n" 11569 "/* clang-format off */\n" 11570 "/* long long long long long long line */\n", 11571 getLLVMStyleWithColumns(20))); 11572 } 11573 11574 TEST_F(FormatTest, DoNotCrashOnInvalidInput) { 11575 format("? ) ="); 11576 verifyNoCrash("#define a\\\n /**/}"); 11577 } 11578 11579 TEST_F(FormatTest, FormatsTableGenCode) { 11580 FormatStyle Style = getLLVMStyle(); 11581 Style.Language = FormatStyle::LK_TableGen; 11582 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style); 11583 } 11584 11585 TEST_F(FormatTest, ArrayOfTemplates) { 11586 EXPECT_EQ("auto a = new unique_ptr<int>[10];", 11587 format("auto a = new unique_ptr<int > [ 10];")); 11588 11589 FormatStyle Spaces = getLLVMStyle(); 11590 Spaces.SpacesInSquareBrackets = true; 11591 EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];", 11592 format("auto a = new unique_ptr<int > [10];", Spaces)); 11593 } 11594 11595 TEST_F(FormatTest, ArrayAsTemplateType) { 11596 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;", 11597 format("auto a = unique_ptr < Foo < Bar>[ 10]> ;")); 11598 11599 FormatStyle Spaces = getLLVMStyle(); 11600 Spaces.SpacesInSquareBrackets = true; 11601 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;", 11602 format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces)); 11603 } 11604 11605 TEST_F(FormatTest, NoSpaceAfterSuper) { 11606 verifyFormat("__super::FooBar();"); 11607 } 11608 11609 TEST(FormatStyle, GetStyleOfFile) { 11610 vfs::InMemoryFileSystem FS; 11611 // Test 1: format file in the same directory. 11612 ASSERT_TRUE( 11613 FS.addFile("/a/.clang-format", 0, 11614 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM"))); 11615 ASSERT_TRUE( 11616 FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 11617 auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS); 11618 ASSERT_TRUE((bool)Style1); 11619 ASSERT_EQ(*Style1, getLLVMStyle()); 11620 11621 // Test 2.1: fallback to default. 11622 ASSERT_TRUE( 11623 FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 11624 auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS); 11625 ASSERT_TRUE((bool)Style2); 11626 ASSERT_EQ(*Style2, getMozillaStyle()); 11627 11628 // Test 2.2: no format on 'none' fallback style. 11629 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 11630 ASSERT_TRUE((bool)Style2); 11631 ASSERT_EQ(*Style2, getNoStyle()); 11632 11633 // Test 2.3: format if config is found with no based style while fallback is 11634 // 'none'. 11635 ASSERT_TRUE(FS.addFile("/b/.clang-format", 0, 11636 llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2"))); 11637 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 11638 ASSERT_TRUE((bool)Style2); 11639 ASSERT_EQ(*Style2, getLLVMStyle()); 11640 11641 // Test 2.4: format if yaml with no based style, while fallback is 'none'. 11642 Style2 = getStyle("{}", "a.h", "none", "", &FS); 11643 ASSERT_TRUE((bool)Style2); 11644 ASSERT_EQ(*Style2, getLLVMStyle()); 11645 11646 // Test 3: format file in parent directory. 11647 ASSERT_TRUE( 11648 FS.addFile("/c/.clang-format", 0, 11649 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 11650 ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0, 11651 llvm::MemoryBuffer::getMemBuffer("int i;"))); 11652 auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS); 11653 ASSERT_TRUE((bool)Style3); 11654 ASSERT_EQ(*Style3, getGoogleStyle()); 11655 11656 // Test 4: error on invalid fallback style 11657 auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS); 11658 ASSERT_FALSE((bool)Style4); 11659 llvm::consumeError(Style4.takeError()); 11660 11661 // Test 5: error on invalid yaml on command line 11662 auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS); 11663 ASSERT_FALSE((bool)Style5); 11664 llvm::consumeError(Style5.takeError()); 11665 11666 // Test 6: error on invalid style 11667 auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS); 11668 ASSERT_FALSE((bool)Style6); 11669 llvm::consumeError(Style6.takeError()); 11670 11671 // Test 7: found config file, error on parsing it 11672 ASSERT_TRUE( 11673 FS.addFile("/d/.clang-format", 0, 11674 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n" 11675 "InvalidKey: InvalidValue"))); 11676 ASSERT_TRUE( 11677 FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 11678 auto Style7 = getStyle("file", "/d/.clang-format", "LLVM", "", &FS); 11679 ASSERT_FALSE((bool)Style7); 11680 llvm::consumeError(Style7.takeError()); 11681 } 11682 11683 TEST_F(ReplacementTest, FormatCodeAfterReplacements) { 11684 // Column limit is 20. 11685 std::string Code = "Type *a =\n" 11686 " new Type();\n" 11687 "g(iiiii, 0, jjjjj,\n" 11688 " 0, kkkkk, 0, mm);\n" 11689 "int bad = format ;"; 11690 std::string Expected = "auto a = new Type();\n" 11691 "g(iiiii, nullptr,\n" 11692 " jjjjj, nullptr,\n" 11693 " kkkkk, nullptr,\n" 11694 " mm);\n" 11695 "int bad = format ;"; 11696 FileID ID = Context.createInMemoryFile("format.cpp", Code); 11697 tooling::Replacements Replaces = toReplacements( 11698 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6, 11699 "auto "), 11700 tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1, 11701 "nullptr"), 11702 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1, 11703 "nullptr"), 11704 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1, 11705 "nullptr")}); 11706 11707 format::FormatStyle Style = format::getLLVMStyle(); 11708 Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility. 11709 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 11710 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 11711 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 11712 auto Result = applyAllReplacements(Code, *FormattedReplaces); 11713 EXPECT_TRUE(static_cast<bool>(Result)); 11714 EXPECT_EQ(Expected, *Result); 11715 } 11716 11717 TEST_F(ReplacementTest, SortIncludesAfterReplacement) { 11718 std::string Code = "#include \"a.h\"\n" 11719 "#include \"c.h\"\n" 11720 "\n" 11721 "int main() {\n" 11722 " return 0;\n" 11723 "}"; 11724 std::string Expected = "#include \"a.h\"\n" 11725 "#include \"b.h\"\n" 11726 "#include \"c.h\"\n" 11727 "\n" 11728 "int main() {\n" 11729 " return 0;\n" 11730 "}"; 11731 FileID ID = Context.createInMemoryFile("fix.cpp", Code); 11732 tooling::Replacements Replaces = toReplacements( 11733 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0, 11734 "#include \"b.h\"\n")}); 11735 11736 format::FormatStyle Style = format::getLLVMStyle(); 11737 Style.SortIncludes = true; 11738 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 11739 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 11740 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 11741 auto Result = applyAllReplacements(Code, *FormattedReplaces); 11742 EXPECT_TRUE(static_cast<bool>(Result)); 11743 EXPECT_EQ(Expected, *Result); 11744 } 11745 11746 TEST_F(FormatTest, FormatSortsUsingDeclarations) { 11747 EXPECT_EQ("using std::cin;\n" 11748 "using std::cout;", 11749 format("using std::cout;\n" 11750 "using std::cin;", getGoogleStyle())); 11751 } 11752 11753 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) { 11754 format::FormatStyle Style = format::getLLVMStyle(); 11755 Style.Standard = FormatStyle::LS_Cpp03; 11756 // cpp03 recognize this string as identifier u8 and literal character 'a' 11757 EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style)); 11758 } 11759 11760 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) { 11761 // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers 11762 // all modes, including C++11, C++14 and C++17 11763 EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';")); 11764 } 11765 11766 TEST_F(FormatTest, DoNotFormatLikelyXml) { 11767 EXPECT_EQ("<!-- ;> -->", 11768 format("<!-- ;> -->", getGoogleStyle())); 11769 EXPECT_EQ(" <!-- >; -->", 11770 format(" <!-- >; -->", getGoogleStyle())); 11771 } 11772 11773 TEST_F(FormatTest, StructuredBindings) { 11774 // Structured bindings is a C++17 feature. 11775 // all modes, including C++11, C++14 and C++17 11776 verifyFormat("auto [a, b] = f();"); 11777 EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();")); 11778 EXPECT_EQ("const auto [a, b] = f();", format("const auto[a, b] = f();")); 11779 EXPECT_EQ("auto const [a, b] = f();", format("auto const[a, b] = f();")); 11780 EXPECT_EQ("auto const volatile [a, b] = f();", 11781 format("auto const volatile[a, b] = f();")); 11782 EXPECT_EQ("auto [a, b, c] = f();", format("auto [ a , b,c ] = f();")); 11783 EXPECT_EQ("auto &[a, b, c] = f();", 11784 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 const &[a, b] = f();", format("auto const&[a, b] = f();")); 11788 EXPECT_EQ("auto const volatile &&[a, b] = f();", 11789 format("auto const volatile &&[a, b] = f();")); 11790 EXPECT_EQ("auto const &&[a, b] = f();", format("auto const && [a, b] = f();")); 11791 EXPECT_EQ("const auto &[a, b] = f();", format("const auto & [a, b] = f();")); 11792 EXPECT_EQ("const auto volatile &&[a, b] = f();", 11793 format("const auto volatile &&[a, b] = f();")); 11794 EXPECT_EQ("volatile const auto &&[a, b] = f();", 11795 format("volatile const auto &&[a, b] = f();")); 11796 EXPECT_EQ("const auto &&[a, b] = f();", format("const auto && [a, b] = f();")); 11797 11798 // Make sure we don't mistake structured bindings for lambdas. 11799 FormatStyle PointerMiddle = getLLVMStyle(); 11800 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 11801 verifyFormat("auto [a1, b]{A * i};", getGoogleStyle()); 11802 verifyFormat("auto [a2, b]{A * i};", getLLVMStyle()); 11803 verifyFormat("auto [a3, b]{A * i};", PointerMiddle); 11804 verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle()); 11805 verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle()); 11806 verifyFormat("auto const [a3, b]{A * i};", PointerMiddle); 11807 verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle()); 11808 verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle()); 11809 verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle); 11810 verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle()); 11811 verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle()); 11812 verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle); 11813 11814 EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}", 11815 format("for (const auto && [a, b] : some_range) {\n}")); 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("auto [x, y](expr);", format("auto[x,y] (expr);")); 11821 EXPECT_EQ("auto &[x, y](expr);", format("auto & [x,y] (expr);")); 11822 EXPECT_EQ("auto &&[x, y](expr);", format("auto && [x,y] (expr);")); 11823 EXPECT_EQ("auto const &[x, y](expr);", format("auto const & [x,y] (expr);")); 11824 EXPECT_EQ("auto const &&[x, y](expr);", format("auto const && [x,y] (expr);")); 11825 EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y] {expr};")); 11826 EXPECT_EQ("auto const &[x, y]{expr};", format("auto const & [x,y] {expr};")); 11827 EXPECT_EQ("auto const &&[x, y]{expr};", format("auto const && [x,y] {expr};")); 11828 11829 format::FormatStyle Spaces = format::getLLVMStyle(); 11830 Spaces.SpacesInSquareBrackets = true; 11831 verifyFormat("auto [ a, b ] = f();", Spaces); 11832 verifyFormat("auto &&[ a, b ] = f();", Spaces); 11833 verifyFormat("auto &[ a, b ] = f();", Spaces); 11834 verifyFormat("auto const &&[ a, b ] = f();", Spaces); 11835 verifyFormat("auto const &[ a, b ] = f();", Spaces); 11836 } 11837 11838 } // end namespace 11839 } // end namespace format 11840 } // end namespace clang 11841