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 Code, 74 const FormatStyle &Style = getLLVMStyle()) { 75 EXPECT_EQ(Code.str(), format(test::messUp(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(Code.str(), format(test::messUp(Code), ObjCStyle)); 82 } 83 } 84 85 void verifyIncompleteFormat(llvm::StringRef Code, 86 const FormatStyle &Style = getLLVMStyle()) { 87 EXPECT_EQ(Code.str(), 88 format(test::messUp(Code), Style, SC_ExpectIncomplete)); 89 } 90 91 void verifyGoogleFormat(llvm::StringRef Code) { 92 verifyFormat(Code, getGoogleStyle()); 93 } 94 95 void verifyIndependentOfContext(llvm::StringRef text) { 96 verifyFormat(text); 97 verifyFormat(llvm::Twine("void f() { " + text + " }").str()); 98 } 99 100 /// \brief Verify that clang-format does not crash on the given input. 101 void verifyNoCrash(llvm::StringRef Code, 102 const FormatStyle &Style = getLLVMStyle()) { 103 format(Code, Style, SC_DoNotCheck); 104 } 105 106 int ReplacementCount; 107 }; 108 109 TEST_F(FormatTest, MessUp) { 110 EXPECT_EQ("1 2 3", test::messUp("1 2 3")); 111 EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n")); 112 EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc")); 113 EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc")); 114 EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne")); 115 } 116 117 //===----------------------------------------------------------------------===// 118 // Basic function tests. 119 //===----------------------------------------------------------------------===// 120 121 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) { 122 EXPECT_EQ(";", format(";")); 123 } 124 125 TEST_F(FormatTest, FormatsGlobalStatementsAt0) { 126 EXPECT_EQ("int i;", format(" int i;")); 127 EXPECT_EQ("\nint i;", format(" \n\t \v \f int i;")); 128 EXPECT_EQ("int i;\nint j;", format(" int i; int j;")); 129 EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;")); 130 } 131 132 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) { 133 EXPECT_EQ("int i;", format("int\ni;")); 134 } 135 136 TEST_F(FormatTest, FormatsNestedBlockStatements) { 137 EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}")); 138 } 139 140 TEST_F(FormatTest, FormatsNestedCall) { 141 verifyFormat("Method(f1, f2(f3));"); 142 verifyFormat("Method(f1(f2, f3()));"); 143 verifyFormat("Method(f1(f2, (f3())));"); 144 } 145 146 TEST_F(FormatTest, NestedNameSpecifiers) { 147 verifyFormat("vector<::Type> v;"); 148 verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())"); 149 verifyFormat("static constexpr bool Bar = decltype(bar())::value;"); 150 verifyFormat("bool a = 2 < ::SomeFunction();"); 151 verifyFormat("ALWAYS_INLINE ::std::string getName();"); 152 verifyFormat("some::string getName();"); 153 } 154 155 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) { 156 EXPECT_EQ("if (a) {\n" 157 " f();\n" 158 "}", 159 format("if(a){f();}")); 160 EXPECT_EQ(4, ReplacementCount); 161 EXPECT_EQ("if (a) {\n" 162 " f();\n" 163 "}", 164 format("if (a) {\n" 165 " f();\n" 166 "}")); 167 EXPECT_EQ(0, ReplacementCount); 168 EXPECT_EQ("/*\r\n" 169 "\r\n" 170 "*/\r\n", 171 format("/*\r\n" 172 "\r\n" 173 "*/\r\n")); 174 EXPECT_EQ(0, ReplacementCount); 175 } 176 177 TEST_F(FormatTest, RemovesEmptyLines) { 178 EXPECT_EQ("class C {\n" 179 " int i;\n" 180 "};", 181 format("class C {\n" 182 " int i;\n" 183 "\n" 184 "};")); 185 186 // Don't remove empty lines at the start of namespaces or extern "C" blocks. 187 EXPECT_EQ("namespace N {\n" 188 "\n" 189 "int i;\n" 190 "}", 191 format("namespace N {\n" 192 "\n" 193 "int i;\n" 194 "}", 195 getGoogleStyle())); 196 EXPECT_EQ("extern /**/ \"C\" /**/ {\n" 197 "\n" 198 "int i;\n" 199 "}", 200 format("extern /**/ \"C\" /**/ {\n" 201 "\n" 202 "int i;\n" 203 "}", 204 getGoogleStyle())); 205 206 // ...but do keep inlining and removing empty lines for non-block extern "C" 207 // functions. 208 verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle()); 209 EXPECT_EQ("extern \"C\" int f() {\n" 210 " int i = 42;\n" 211 " return i;\n" 212 "}", 213 format("extern \"C\" int f() {\n" 214 "\n" 215 " int i = 42;\n" 216 " return i;\n" 217 "}", 218 getGoogleStyle())); 219 220 // Remove empty lines at the beginning and end of blocks. 221 EXPECT_EQ("void f() {\n" 222 "\n" 223 " if (a) {\n" 224 "\n" 225 " f();\n" 226 " }\n" 227 "}", 228 format("void f() {\n" 229 "\n" 230 " if (a) {\n" 231 "\n" 232 " f();\n" 233 "\n" 234 " }\n" 235 "\n" 236 "}", 237 getLLVMStyle())); 238 EXPECT_EQ("void f() {\n" 239 " if (a) {\n" 240 " f();\n" 241 " }\n" 242 "}", 243 format("void f() {\n" 244 "\n" 245 " if (a) {\n" 246 "\n" 247 " f();\n" 248 "\n" 249 " }\n" 250 "\n" 251 "}", 252 getGoogleStyle())); 253 254 // Don't remove empty lines in more complex control statements. 255 EXPECT_EQ("void f() {\n" 256 " if (a) {\n" 257 " f();\n" 258 "\n" 259 " } else if (b) {\n" 260 " f();\n" 261 " }\n" 262 "}", 263 format("void f() {\n" 264 " if (a) {\n" 265 " f();\n" 266 "\n" 267 " } else if (b) {\n" 268 " f();\n" 269 "\n" 270 " }\n" 271 "\n" 272 "}")); 273 274 // FIXME: This is slightly inconsistent. 275 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 276 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 277 EXPECT_EQ("namespace {\n" 278 "int i;\n" 279 "}", 280 format("namespace {\n" 281 "int i;\n" 282 "\n" 283 "}", LLVMWithNoNamespaceFix)); 284 EXPECT_EQ("namespace {\n" 285 "int i;\n" 286 "}", 287 format("namespace {\n" 288 "int i;\n" 289 "\n" 290 "}")); 291 EXPECT_EQ("namespace {\n" 292 "int i;\n" 293 "\n" 294 "} // namespace", 295 format("namespace {\n" 296 "int i;\n" 297 "\n" 298 "} // namespace")); 299 300 FormatStyle Style = getLLVMStyle(); 301 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 302 Style.MaxEmptyLinesToKeep = 2; 303 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 304 Style.BraceWrapping.AfterClass = true; 305 Style.BraceWrapping.AfterFunction = true; 306 Style.KeepEmptyLinesAtTheStartOfBlocks = false; 307 308 EXPECT_EQ("class Foo\n" 309 "{\n" 310 " Foo() {}\n" 311 "\n" 312 " void funk() {}\n" 313 "};", 314 format("class Foo\n" 315 "{\n" 316 " Foo()\n" 317 " {\n" 318 " }\n" 319 "\n" 320 " void funk() {}\n" 321 "};", 322 Style)); 323 } 324 325 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) { 326 verifyFormat("x = (a) and (b);"); 327 verifyFormat("x = (a) or (b);"); 328 verifyFormat("x = (a) bitand (b);"); 329 verifyFormat("x = (a) bitor (b);"); 330 verifyFormat("x = (a) not_eq (b);"); 331 verifyFormat("x = (a) and_eq (b);"); 332 verifyFormat("x = (a) or_eq (b);"); 333 verifyFormat("x = (a) xor (b);"); 334 } 335 336 TEST_F(FormatTest, RecognizesUnaryOperatorKeywords) { 337 verifyFormat("x = compl(a);"); 338 verifyFormat("x = not(a);"); 339 verifyFormat("x = bitand(a);"); 340 // Unary operator must not be merged with the next identifier 341 verifyFormat("x = compl a;"); 342 verifyFormat("x = not a;"); 343 verifyFormat("x = bitand a;"); 344 } 345 346 //===----------------------------------------------------------------------===// 347 // Tests for control statements. 348 //===----------------------------------------------------------------------===// 349 350 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) { 351 verifyFormat("if (true)\n f();\ng();"); 352 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); 353 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); 354 verifyFormat("if constexpr (true)\n" 355 " f();\ng();"); 356 verifyFormat("if constexpr (a)\n" 357 " if constexpr (b)\n" 358 " if constexpr (c)\n" 359 " g();\n" 360 "h();"); 361 verifyFormat("if constexpr (a)\n" 362 " if constexpr (b) {\n" 363 " f();\n" 364 " }\n" 365 "g();"); 366 367 FormatStyle AllowsMergedIf = getLLVMStyle(); 368 AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left; 369 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 370 verifyFormat("if (a)\n" 371 " // comment\n" 372 " f();", 373 AllowsMergedIf); 374 verifyFormat("{\n" 375 " if (a)\n" 376 " label:\n" 377 " f();\n" 378 "}", 379 AllowsMergedIf); 380 verifyFormat("#define A \\\n" 381 " if (a) \\\n" 382 " label: \\\n" 383 " f()", 384 AllowsMergedIf); 385 verifyFormat("if (a)\n" 386 " ;", 387 AllowsMergedIf); 388 verifyFormat("if (a)\n" 389 " if (b) return;", 390 AllowsMergedIf); 391 392 verifyFormat("if (a) // Can't merge this\n" 393 " f();\n", 394 AllowsMergedIf); 395 verifyFormat("if (a) /* still don't merge */\n" 396 " f();", 397 AllowsMergedIf); 398 verifyFormat("if (a) { // Never merge this\n" 399 " f();\n" 400 "}", 401 AllowsMergedIf); 402 verifyFormat("if (a) { /* Never merge this */\n" 403 " f();\n" 404 "}", 405 AllowsMergedIf); 406 407 AllowsMergedIf.ColumnLimit = 14; 408 verifyFormat("if (a) return;", AllowsMergedIf); 409 verifyFormat("if (aaaaaaaaa)\n" 410 " return;", 411 AllowsMergedIf); 412 413 AllowsMergedIf.ColumnLimit = 13; 414 verifyFormat("if (a)\n return;", AllowsMergedIf); 415 } 416 417 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) { 418 FormatStyle AllowsMergedLoops = getLLVMStyle(); 419 AllowsMergedLoops.AllowShortLoopsOnASingleLine = true; 420 verifyFormat("while (true) continue;", AllowsMergedLoops); 421 verifyFormat("for (;;) continue;", AllowsMergedLoops); 422 verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops); 423 verifyFormat("while (true)\n" 424 " ;", 425 AllowsMergedLoops); 426 verifyFormat("for (;;)\n" 427 " ;", 428 AllowsMergedLoops); 429 verifyFormat("for (;;)\n" 430 " for (;;) continue;", 431 AllowsMergedLoops); 432 verifyFormat("for (;;) // Can't merge this\n" 433 " continue;", 434 AllowsMergedLoops); 435 verifyFormat("for (;;) /* still don't merge */\n" 436 " continue;", 437 AllowsMergedLoops); 438 } 439 440 TEST_F(FormatTest, FormatShortBracedStatements) { 441 FormatStyle AllowSimpleBracedStatements = getLLVMStyle(); 442 AllowSimpleBracedStatements.ColumnLimit = 40; 443 AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true; 444 445 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true; 446 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 447 448 AllowSimpleBracedStatements.BreakBeforeBraces = FormatStyle::BS_Custom; 449 AllowSimpleBracedStatements.BraceWrapping.AfterFunction = true; 450 AllowSimpleBracedStatements.BraceWrapping.SplitEmptyRecord = false; 451 452 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 453 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements); 454 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 455 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 456 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 457 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements); 458 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 459 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 460 verifyFormat("if (true) {\n" 461 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 462 "}", 463 AllowSimpleBracedStatements); 464 verifyFormat("if (true) { //\n" 465 " f();\n" 466 "}", 467 AllowSimpleBracedStatements); 468 verifyFormat("if (true) {\n" 469 " f();\n" 470 " f();\n" 471 "}", 472 AllowSimpleBracedStatements); 473 verifyFormat("if (true) {\n" 474 " f();\n" 475 "} else {\n" 476 " f();\n" 477 "}", 478 AllowSimpleBracedStatements); 479 480 verifyFormat("struct A2 {\n" 481 " int X;\n" 482 "};", 483 AllowSimpleBracedStatements); 484 verifyFormat("typedef struct A2 {\n" 485 " int X;\n" 486 "} A2_t;", 487 AllowSimpleBracedStatements); 488 verifyFormat("template <int> struct A2 {\n" 489 " struct B {};\n" 490 "};", 491 AllowSimpleBracedStatements); 492 493 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false; 494 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 495 verifyFormat("if (true) {\n" 496 " f();\n" 497 "}", 498 AllowSimpleBracedStatements); 499 verifyFormat("if (true) {\n" 500 " f();\n" 501 "} else {\n" 502 " f();\n" 503 "}", 504 AllowSimpleBracedStatements); 505 506 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 507 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 508 verifyFormat("while (true) {\n" 509 " f();\n" 510 "}", 511 AllowSimpleBracedStatements); 512 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 513 verifyFormat("for (;;) {\n" 514 " f();\n" 515 "}", 516 AllowSimpleBracedStatements); 517 518 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true; 519 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 520 AllowSimpleBracedStatements.BraceWrapping.AfterControlStatement = true; 521 522 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 523 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements); 524 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 525 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 526 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 527 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements); 528 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 529 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 530 verifyFormat("if (true)\n" 531 "{\n" 532 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 533 "}", 534 AllowSimpleBracedStatements); 535 verifyFormat("if (true)\n" 536 "{ //\n" 537 " f();\n" 538 "}", 539 AllowSimpleBracedStatements); 540 verifyFormat("if (true)\n" 541 "{\n" 542 " f();\n" 543 " f();\n" 544 "}", 545 AllowSimpleBracedStatements); 546 verifyFormat("if (true)\n" 547 "{\n" 548 " f();\n" 549 "} else\n" 550 "{\n" 551 " f();\n" 552 "}", 553 AllowSimpleBracedStatements); 554 555 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false; 556 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 557 verifyFormat("if (true)\n" 558 "{\n" 559 " f();\n" 560 "}", 561 AllowSimpleBracedStatements); 562 verifyFormat("if (true)\n" 563 "{\n" 564 " f();\n" 565 "} else\n" 566 "{\n" 567 " f();\n" 568 "}", 569 AllowSimpleBracedStatements); 570 571 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 572 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 573 verifyFormat("while (true)\n" 574 "{\n" 575 " f();\n" 576 "}", 577 AllowSimpleBracedStatements); 578 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 579 verifyFormat("for (;;)\n" 580 "{\n" 581 " f();\n" 582 "}", 583 AllowSimpleBracedStatements); 584 } 585 586 TEST_F(FormatTest, ParseIfElse) { 587 verifyFormat("if (true)\n" 588 " if (true)\n" 589 " if (true)\n" 590 " f();\n" 591 " else\n" 592 " g();\n" 593 " else\n" 594 " h();\n" 595 "else\n" 596 " i();"); 597 verifyFormat("if (true)\n" 598 " if (true)\n" 599 " if (true) {\n" 600 " if (true)\n" 601 " f();\n" 602 " } else {\n" 603 " g();\n" 604 " }\n" 605 " else\n" 606 " h();\n" 607 "else {\n" 608 " i();\n" 609 "}"); 610 verifyFormat("if (true)\n" 611 " if constexpr (true)\n" 612 " if (true) {\n" 613 " if constexpr (true)\n" 614 " f();\n" 615 " } else {\n" 616 " g();\n" 617 " }\n" 618 " else\n" 619 " h();\n" 620 "else {\n" 621 " i();\n" 622 "}"); 623 verifyFormat("void f() {\n" 624 " if (a) {\n" 625 " } else {\n" 626 " }\n" 627 "}"); 628 } 629 630 TEST_F(FormatTest, ElseIf) { 631 verifyFormat("if (a) {\n} else if (b) {\n}"); 632 verifyFormat("if (a)\n" 633 " f();\n" 634 "else if (b)\n" 635 " g();\n" 636 "else\n" 637 " h();"); 638 verifyFormat("if constexpr (a)\n" 639 " f();\n" 640 "else if constexpr (b)\n" 641 " g();\n" 642 "else\n" 643 " h();"); 644 verifyFormat("if (a) {\n" 645 " f();\n" 646 "}\n" 647 "// or else ..\n" 648 "else {\n" 649 " g()\n" 650 "}"); 651 652 verifyFormat("if (a) {\n" 653 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 654 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 655 "}"); 656 verifyFormat("if (a) {\n" 657 "} else if (\n" 658 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 659 "}", 660 getLLVMStyleWithColumns(62)); 661 verifyFormat("if (a) {\n" 662 "} else if constexpr (\n" 663 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 664 "}", 665 getLLVMStyleWithColumns(62)); 666 } 667 668 TEST_F(FormatTest, FormatsForLoop) { 669 verifyFormat( 670 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" 671 " ++VeryVeryLongLoopVariable)\n" 672 " ;"); 673 verifyFormat("for (;;)\n" 674 " f();"); 675 verifyFormat("for (;;) {\n}"); 676 verifyFormat("for (;;) {\n" 677 " f();\n" 678 "}"); 679 verifyFormat("for (int i = 0; (i < 10); ++i) {\n}"); 680 681 verifyFormat( 682 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 683 " E = UnwrappedLines.end();\n" 684 " I != E; ++I) {\n}"); 685 686 verifyFormat( 687 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" 688 " ++IIIII) {\n}"); 689 verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n" 690 " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n" 691 " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}"); 692 verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n" 693 " I = FD->getDeclsInPrototypeScope().begin(),\n" 694 " E = FD->getDeclsInPrototypeScope().end();\n" 695 " I != E; ++I) {\n}"); 696 verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n" 697 " I = Container.begin(),\n" 698 " E = Container.end();\n" 699 " I != E; ++I) {\n}", 700 getLLVMStyleWithColumns(76)); 701 702 verifyFormat( 703 "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 704 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n" 705 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 706 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 707 " ++aaaaaaaaaaa) {\n}"); 708 verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 709 " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n" 710 " ++i) {\n}"); 711 verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n" 712 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 713 "}"); 714 verifyFormat("for (some_namespace::SomeIterator iter( // force break\n" 715 " aaaaaaaaaa);\n" 716 " iter; ++iter) {\n" 717 "}"); 718 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 719 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 720 " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n" 721 " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {"); 722 723 FormatStyle NoBinPacking = getLLVMStyle(); 724 NoBinPacking.BinPackParameters = false; 725 verifyFormat("for (int aaaaaaaaaaa = 1;\n" 726 " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n" 727 " aaaaaaaaaaaaaaaa,\n" 728 " aaaaaaaaaaaaaaaa,\n" 729 " aaaaaaaaaaaaaaaa);\n" 730 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 731 "}", 732 NoBinPacking); 733 verifyFormat( 734 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 735 " E = UnwrappedLines.end();\n" 736 " I != E;\n" 737 " ++I) {\n}", 738 NoBinPacking); 739 740 FormatStyle AlignLeft = getLLVMStyle(); 741 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 742 verifyFormat("for (A* a = start; a < end; ++a, ++value) {\n}", AlignLeft); 743 } 744 745 TEST_F(FormatTest, RangeBasedForLoops) { 746 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 747 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 748 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n" 749 " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}"); 750 verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n" 751 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 752 verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n" 753 " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}"); 754 } 755 756 TEST_F(FormatTest, ForEachLoops) { 757 verifyFormat("void f() {\n" 758 " foreach (Item *item, itemlist) {}\n" 759 " Q_FOREACH (Item *item, itemlist) {}\n" 760 " BOOST_FOREACH (Item *item, itemlist) {}\n" 761 " UNKNOWN_FORACH(Item * item, itemlist) {}\n" 762 "}"); 763 764 // As function-like macros. 765 verifyFormat("#define foreach(x, y)\n" 766 "#define Q_FOREACH(x, y)\n" 767 "#define BOOST_FOREACH(x, y)\n" 768 "#define UNKNOWN_FOREACH(x, y)\n"); 769 770 // Not as function-like macros. 771 verifyFormat("#define foreach (x, y)\n" 772 "#define Q_FOREACH (x, y)\n" 773 "#define BOOST_FOREACH (x, y)\n" 774 "#define UNKNOWN_FOREACH (x, y)\n"); 775 } 776 777 TEST_F(FormatTest, FormatsWhileLoop) { 778 verifyFormat("while (true) {\n}"); 779 verifyFormat("while (true)\n" 780 " f();"); 781 verifyFormat("while () {\n}"); 782 verifyFormat("while () {\n" 783 " f();\n" 784 "}"); 785 } 786 787 TEST_F(FormatTest, FormatsDoWhile) { 788 verifyFormat("do {\n" 789 " do_something();\n" 790 "} while (something());"); 791 verifyFormat("do\n" 792 " do_something();\n" 793 "while (something());"); 794 } 795 796 TEST_F(FormatTest, FormatsSwitchStatement) { 797 verifyFormat("switch (x) {\n" 798 "case 1:\n" 799 " f();\n" 800 " break;\n" 801 "case kFoo:\n" 802 "case ns::kBar:\n" 803 "case kBaz:\n" 804 " break;\n" 805 "default:\n" 806 " g();\n" 807 " break;\n" 808 "}"); 809 verifyFormat("switch (x) {\n" 810 "case 1: {\n" 811 " f();\n" 812 " break;\n" 813 "}\n" 814 "case 2: {\n" 815 " break;\n" 816 "}\n" 817 "}"); 818 verifyFormat("switch (x) {\n" 819 "case 1: {\n" 820 " f();\n" 821 " {\n" 822 " g();\n" 823 " h();\n" 824 " }\n" 825 " break;\n" 826 "}\n" 827 "}"); 828 verifyFormat("switch (x) {\n" 829 "case 1: {\n" 830 " f();\n" 831 " if (foo) {\n" 832 " g();\n" 833 " h();\n" 834 " }\n" 835 " break;\n" 836 "}\n" 837 "}"); 838 verifyFormat("switch (x) {\n" 839 "case 1: {\n" 840 " f();\n" 841 " g();\n" 842 "} break;\n" 843 "}"); 844 verifyFormat("switch (test)\n" 845 " ;"); 846 verifyFormat("switch (x) {\n" 847 "default: {\n" 848 " // Do nothing.\n" 849 "}\n" 850 "}"); 851 verifyFormat("switch (x) {\n" 852 "// comment\n" 853 "// if 1, do f()\n" 854 "case 1:\n" 855 " f();\n" 856 "}"); 857 verifyFormat("switch (x) {\n" 858 "case 1:\n" 859 " // Do amazing stuff\n" 860 " {\n" 861 " f();\n" 862 " g();\n" 863 " }\n" 864 " break;\n" 865 "}"); 866 verifyFormat("#define A \\\n" 867 " switch (x) { \\\n" 868 " case a: \\\n" 869 " foo = b; \\\n" 870 " }", 871 getLLVMStyleWithColumns(20)); 872 verifyFormat("#define OPERATION_CASE(name) \\\n" 873 " case OP_name: \\\n" 874 " return operations::Operation##name\n", 875 getLLVMStyleWithColumns(40)); 876 verifyFormat("switch (x) {\n" 877 "case 1:;\n" 878 "default:;\n" 879 " int i;\n" 880 "}"); 881 882 verifyGoogleFormat("switch (x) {\n" 883 " case 1:\n" 884 " f();\n" 885 " break;\n" 886 " case kFoo:\n" 887 " case ns::kBar:\n" 888 " case kBaz:\n" 889 " break;\n" 890 " default:\n" 891 " g();\n" 892 " break;\n" 893 "}"); 894 verifyGoogleFormat("switch (x) {\n" 895 " case 1: {\n" 896 " f();\n" 897 " break;\n" 898 " }\n" 899 "}"); 900 verifyGoogleFormat("switch (test)\n" 901 " ;"); 902 903 verifyGoogleFormat("#define OPERATION_CASE(name) \\\n" 904 " case OP_name: \\\n" 905 " return operations::Operation##name\n"); 906 verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n" 907 " // Get the correction operation class.\n" 908 " switch (OpCode) {\n" 909 " CASE(Add);\n" 910 " CASE(Subtract);\n" 911 " default:\n" 912 " return operations::Unknown;\n" 913 " }\n" 914 "#undef OPERATION_CASE\n" 915 "}"); 916 verifyFormat("DEBUG({\n" 917 " switch (x) {\n" 918 " case A:\n" 919 " f();\n" 920 " break;\n" 921 " // fallthrough\n" 922 " case B:\n" 923 " g();\n" 924 " break;\n" 925 " }\n" 926 "});"); 927 EXPECT_EQ("DEBUG({\n" 928 " switch (x) {\n" 929 " case A:\n" 930 " f();\n" 931 " break;\n" 932 " // On B:\n" 933 " case B:\n" 934 " g();\n" 935 " break;\n" 936 " }\n" 937 "});", 938 format("DEBUG({\n" 939 " switch (x) {\n" 940 " case A:\n" 941 " f();\n" 942 " break;\n" 943 " // On B:\n" 944 " case B:\n" 945 " g();\n" 946 " break;\n" 947 " }\n" 948 "});", 949 getLLVMStyle())); 950 verifyFormat("switch (a) {\n" 951 "case (b):\n" 952 " return;\n" 953 "}"); 954 955 verifyFormat("switch (a) {\n" 956 "case some_namespace::\n" 957 " some_constant:\n" 958 " return;\n" 959 "}", 960 getLLVMStyleWithColumns(34)); 961 } 962 963 TEST_F(FormatTest, CaseRanges) { 964 verifyFormat("switch (x) {\n" 965 "case 'A' ... 'Z':\n" 966 "case 1 ... 5:\n" 967 "case a ... b:\n" 968 " break;\n" 969 "}"); 970 } 971 972 TEST_F(FormatTest, ShortCaseLabels) { 973 FormatStyle Style = getLLVMStyle(); 974 Style.AllowShortCaseLabelsOnASingleLine = true; 975 verifyFormat("switch (a) {\n" 976 "case 1: x = 1; break;\n" 977 "case 2: return;\n" 978 "case 3:\n" 979 "case 4:\n" 980 "case 5: return;\n" 981 "case 6: // comment\n" 982 " return;\n" 983 "case 7:\n" 984 " // comment\n" 985 " return;\n" 986 "case 8:\n" 987 " x = 8; // comment\n" 988 " break;\n" 989 "default: y = 1; break;\n" 990 "}", 991 Style); 992 verifyFormat("switch (a) {\n" 993 "case 0: return; // comment\n" 994 "case 1: break; // comment\n" 995 "case 2: return;\n" 996 "// comment\n" 997 "case 3: return;\n" 998 "// comment 1\n" 999 "// comment 2\n" 1000 "// comment 3\n" 1001 "case 4: break; /* comment */\n" 1002 "case 5:\n" 1003 " // comment\n" 1004 " break;\n" 1005 "case 6: /* comment */ x = 1; break;\n" 1006 "case 7: x = /* comment */ 1; break;\n" 1007 "case 8:\n" 1008 " x = 1; /* comment */\n" 1009 " break;\n" 1010 "case 9:\n" 1011 " break; // comment line 1\n" 1012 " // comment line 2\n" 1013 "}", 1014 Style); 1015 EXPECT_EQ("switch (a) {\n" 1016 "case 1:\n" 1017 " x = 8;\n" 1018 " // fall through\n" 1019 "case 2: x = 8;\n" 1020 "// comment\n" 1021 "case 3:\n" 1022 " return; /* comment line 1\n" 1023 " * comment line 2 */\n" 1024 "case 4: i = 8;\n" 1025 "// something else\n" 1026 "#if FOO\n" 1027 "case 5: break;\n" 1028 "#endif\n" 1029 "}", 1030 format("switch (a) {\n" 1031 "case 1: x = 8;\n" 1032 " // fall through\n" 1033 "case 2:\n" 1034 " x = 8;\n" 1035 "// comment\n" 1036 "case 3:\n" 1037 " return; /* comment line 1\n" 1038 " * comment line 2 */\n" 1039 "case 4:\n" 1040 " i = 8;\n" 1041 "// something else\n" 1042 "#if FOO\n" 1043 "case 5: break;\n" 1044 "#endif\n" 1045 "}", 1046 Style)); 1047 EXPECT_EQ("switch (a) {\n" "case 0:\n" 1048 " return; // long long long long long long long long long long long long comment\n" 1049 " // line\n" "}", 1050 format("switch (a) {\n" 1051 "case 0: return; // long long long long long long long long long long long long comment line\n" 1052 "}", 1053 Style)); 1054 EXPECT_EQ("switch (a) {\n" 1055 "case 0:\n" 1056 " return; /* long long long long long long long long long long long long comment\n" 1057 " line */\n" 1058 "}", 1059 format("switch (a) {\n" 1060 "case 0: return; /* long long long long long long long long long long long long comment line */\n" 1061 "}", 1062 Style)); 1063 verifyFormat("switch (a) {\n" 1064 "#if FOO\n" 1065 "case 0: return 0;\n" 1066 "#endif\n" 1067 "}", 1068 Style); 1069 verifyFormat("switch (a) {\n" 1070 "case 1: {\n" 1071 "}\n" 1072 "case 2: {\n" 1073 " return;\n" 1074 "}\n" 1075 "case 3: {\n" 1076 " x = 1;\n" 1077 " return;\n" 1078 "}\n" 1079 "case 4:\n" 1080 " if (x)\n" 1081 " return;\n" 1082 "}", 1083 Style); 1084 Style.ColumnLimit = 21; 1085 verifyFormat("switch (a) {\n" 1086 "case 1: x = 1; break;\n" 1087 "case 2: return;\n" 1088 "case 3:\n" 1089 "case 4:\n" 1090 "case 5: return;\n" 1091 "default:\n" 1092 " y = 1;\n" 1093 " break;\n" 1094 "}", 1095 Style); 1096 } 1097 1098 TEST_F(FormatTest, FormatsLabels) { 1099 verifyFormat("void f() {\n" 1100 " some_code();\n" 1101 "test_label:\n" 1102 " some_other_code();\n" 1103 " {\n" 1104 " some_more_code();\n" 1105 " another_label:\n" 1106 " some_more_code();\n" 1107 " }\n" 1108 "}"); 1109 verifyFormat("{\n" 1110 " some_code();\n" 1111 "test_label:\n" 1112 " some_other_code();\n" 1113 "}"); 1114 verifyFormat("{\n" 1115 " some_code();\n" 1116 "test_label:;\n" 1117 " int i = 0;\n" 1118 "}"); 1119 } 1120 1121 //===----------------------------------------------------------------------===// 1122 // Tests for classes, namespaces, etc. 1123 //===----------------------------------------------------------------------===// 1124 1125 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { 1126 verifyFormat("class A {};"); 1127 } 1128 1129 TEST_F(FormatTest, UnderstandsAccessSpecifiers) { 1130 verifyFormat("class A {\n" 1131 "public:\n" 1132 "public: // comment\n" 1133 "protected:\n" 1134 "private:\n" 1135 " void f() {}\n" 1136 "};"); 1137 verifyGoogleFormat("class A {\n" 1138 " public:\n" 1139 " protected:\n" 1140 " private:\n" 1141 " void f() {}\n" 1142 "};"); 1143 verifyFormat("class A {\n" 1144 "public slots:\n" 1145 " void f1() {}\n" 1146 "public Q_SLOTS:\n" 1147 " void f2() {}\n" 1148 "protected slots:\n" 1149 " void f3() {}\n" 1150 "protected Q_SLOTS:\n" 1151 " void f4() {}\n" 1152 "private slots:\n" 1153 " void f5() {}\n" 1154 "private Q_SLOTS:\n" 1155 " void f6() {}\n" 1156 "signals:\n" 1157 " void g1();\n" 1158 "Q_SIGNALS:\n" 1159 " void g2();\n" 1160 "};"); 1161 1162 // Don't interpret 'signals' the wrong way. 1163 verifyFormat("signals.set();"); 1164 verifyFormat("for (Signals signals : f()) {\n}"); 1165 verifyFormat("{\n" 1166 " signals.set(); // This needs indentation.\n" 1167 "}"); 1168 verifyFormat("void f() {\n" 1169 "label:\n" 1170 " signals.baz();\n" 1171 "}"); 1172 } 1173 1174 TEST_F(FormatTest, SeparatesLogicalBlocks) { 1175 EXPECT_EQ("class A {\n" 1176 "public:\n" 1177 " void f();\n" 1178 "\n" 1179 "private:\n" 1180 " void g() {}\n" 1181 " // test\n" 1182 "protected:\n" 1183 " int h;\n" 1184 "};", 1185 format("class A {\n" 1186 "public:\n" 1187 "void f();\n" 1188 "private:\n" 1189 "void g() {}\n" 1190 "// test\n" 1191 "protected:\n" 1192 "int h;\n" 1193 "};")); 1194 EXPECT_EQ("class A {\n" 1195 "protected:\n" 1196 "public:\n" 1197 " void f();\n" 1198 "};", 1199 format("class A {\n" 1200 "protected:\n" 1201 "\n" 1202 "public:\n" 1203 "\n" 1204 " void f();\n" 1205 "};")); 1206 1207 // Even ensure proper spacing inside macros. 1208 EXPECT_EQ("#define B \\\n" 1209 " class A { \\\n" 1210 " protected: \\\n" 1211 " public: \\\n" 1212 " void f(); \\\n" 1213 " };", 1214 format("#define B \\\n" 1215 " class A { \\\n" 1216 " protected: \\\n" 1217 " \\\n" 1218 " public: \\\n" 1219 " \\\n" 1220 " void f(); \\\n" 1221 " };", 1222 getGoogleStyle())); 1223 // But don't remove empty lines after macros ending in access specifiers. 1224 EXPECT_EQ("#define A private:\n" 1225 "\n" 1226 "int i;", 1227 format("#define A private:\n" 1228 "\n" 1229 "int i;")); 1230 } 1231 1232 TEST_F(FormatTest, FormatsClasses) { 1233 verifyFormat("class A : public B {};"); 1234 verifyFormat("class A : public ::B {};"); 1235 1236 verifyFormat( 1237 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 1238 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 1239 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 1240 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 1241 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 1242 verifyFormat( 1243 "class A : public B, public C, public D, public E, public F {};"); 1244 verifyFormat("class AAAAAAAAAAAA : public B,\n" 1245 " public C,\n" 1246 " public D,\n" 1247 " public E,\n" 1248 " public F,\n" 1249 " public G {};"); 1250 1251 verifyFormat("class\n" 1252 " ReallyReallyLongClassName {\n" 1253 " int i;\n" 1254 "};", 1255 getLLVMStyleWithColumns(32)); 1256 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 1257 " aaaaaaaaaaaaaaaa> {};"); 1258 verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n" 1259 " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n" 1260 " aaaaaaaaaaaaaaaaaaaaaa> {};"); 1261 verifyFormat("template <class R, class C>\n" 1262 "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n" 1263 " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};"); 1264 verifyFormat("class ::A::B {};"); 1265 } 1266 1267 TEST_F(FormatTest, BreakBeforeInheritanceComma) { 1268 FormatStyle StyleWithInheritanceBreak = getLLVMStyle(); 1269 StyleWithInheritanceBreak.BreakBeforeInheritanceComma = true; 1270 1271 verifyFormat("class MyClass : public X {};", StyleWithInheritanceBreak); 1272 verifyFormat("class MyClass\n" 1273 " : public X\n" 1274 " , public Y {};", 1275 StyleWithInheritanceBreak); 1276 } 1277 1278 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) { 1279 verifyFormat("class A {\n} a, b;"); 1280 verifyFormat("struct A {\n} a, b;"); 1281 verifyFormat("union A {\n} a;"); 1282 } 1283 1284 TEST_F(FormatTest, FormatsEnum) { 1285 verifyFormat("enum {\n" 1286 " Zero,\n" 1287 " One = 1,\n" 1288 " Two = One + 1,\n" 1289 " Three = (One + Two),\n" 1290 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1291 " Five = (One, Two, Three, Four, 5)\n" 1292 "};"); 1293 verifyGoogleFormat("enum {\n" 1294 " Zero,\n" 1295 " One = 1,\n" 1296 " Two = One + 1,\n" 1297 " Three = (One + Two),\n" 1298 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1299 " Five = (One, Two, Three, Four, 5)\n" 1300 "};"); 1301 verifyFormat("enum Enum {};"); 1302 verifyFormat("enum {};"); 1303 verifyFormat("enum X E {} d;"); 1304 verifyFormat("enum __attribute__((...)) E {} d;"); 1305 verifyFormat("enum __declspec__((...)) E {} d;"); 1306 verifyFormat("enum {\n" 1307 " Bar = Foo<int, int>::value\n" 1308 "};", 1309 getLLVMStyleWithColumns(30)); 1310 1311 verifyFormat("enum ShortEnum { A, B, C };"); 1312 verifyGoogleFormat("enum ShortEnum { A, B, C };"); 1313 1314 EXPECT_EQ("enum KeepEmptyLines {\n" 1315 " ONE,\n" 1316 "\n" 1317 " TWO,\n" 1318 "\n" 1319 " THREE\n" 1320 "}", 1321 format("enum KeepEmptyLines {\n" 1322 " ONE,\n" 1323 "\n" 1324 " TWO,\n" 1325 "\n" 1326 "\n" 1327 " THREE\n" 1328 "}")); 1329 verifyFormat("enum E { // comment\n" 1330 " ONE,\n" 1331 " TWO\n" 1332 "};\n" 1333 "int i;"); 1334 // Not enums. 1335 verifyFormat("enum X f() {\n" 1336 " a();\n" 1337 " return 42;\n" 1338 "}"); 1339 verifyFormat("enum X Type::f() {\n" 1340 " a();\n" 1341 " return 42;\n" 1342 "}"); 1343 verifyFormat("enum ::X f() {\n" 1344 " a();\n" 1345 " return 42;\n" 1346 "}"); 1347 verifyFormat("enum ns::X f() {\n" 1348 " a();\n" 1349 " return 42;\n" 1350 "}"); 1351 } 1352 1353 TEST_F(FormatTest, FormatsEnumsWithErrors) { 1354 verifyFormat("enum Type {\n" 1355 " One = 0; // These semicolons should be commas.\n" 1356 " Two = 1;\n" 1357 "};"); 1358 verifyFormat("namespace n {\n" 1359 "enum Type {\n" 1360 " One,\n" 1361 " Two, // missing };\n" 1362 " int i;\n" 1363 "}\n" 1364 "void g() {}"); 1365 } 1366 1367 TEST_F(FormatTest, FormatsEnumStruct) { 1368 verifyFormat("enum struct {\n" 1369 " Zero,\n" 1370 " One = 1,\n" 1371 " Two = One + 1,\n" 1372 " Three = (One + Two),\n" 1373 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1374 " Five = (One, Two, Three, Four, 5)\n" 1375 "};"); 1376 verifyFormat("enum struct Enum {};"); 1377 verifyFormat("enum struct {};"); 1378 verifyFormat("enum struct X E {} d;"); 1379 verifyFormat("enum struct __attribute__((...)) E {} d;"); 1380 verifyFormat("enum struct __declspec__((...)) E {} d;"); 1381 verifyFormat("enum struct X f() {\n a();\n return 42;\n}"); 1382 } 1383 1384 TEST_F(FormatTest, FormatsEnumClass) { 1385 verifyFormat("enum class {\n" 1386 " Zero,\n" 1387 " One = 1,\n" 1388 " Two = One + 1,\n" 1389 " Three = (One + Two),\n" 1390 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1391 " Five = (One, Two, Three, Four, 5)\n" 1392 "};"); 1393 verifyFormat("enum class Enum {};"); 1394 verifyFormat("enum class {};"); 1395 verifyFormat("enum class X E {} d;"); 1396 verifyFormat("enum class __attribute__((...)) E {} d;"); 1397 verifyFormat("enum class __declspec__((...)) E {} d;"); 1398 verifyFormat("enum class X f() {\n a();\n return 42;\n}"); 1399 } 1400 1401 TEST_F(FormatTest, FormatsEnumTypes) { 1402 verifyFormat("enum X : int {\n" 1403 " A, // Force multiple lines.\n" 1404 " B\n" 1405 "};"); 1406 verifyFormat("enum X : int { A, B };"); 1407 verifyFormat("enum X : std::uint32_t { A, B };"); 1408 } 1409 1410 TEST_F(FormatTest, FormatsTypedefEnum) { 1411 FormatStyle Style = getLLVMStyle(); 1412 Style.ColumnLimit = 40; 1413 verifyFormat("typedef enum {} EmptyEnum;"); 1414 verifyFormat("typedef enum { A, B, C } ShortEnum;"); 1415 verifyFormat("typedef enum {\n" 1416 " ZERO = 0,\n" 1417 " ONE = 1,\n" 1418 " TWO = 2,\n" 1419 " THREE = 3\n" 1420 "} LongEnum;", 1421 Style); 1422 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 1423 Style.BraceWrapping.AfterEnum = true; 1424 verifyFormat("typedef enum {} EmptyEnum;"); 1425 verifyFormat("typedef enum { A, B, C } ShortEnum;"); 1426 verifyFormat("typedef enum\n" 1427 "{\n" 1428 " ZERO = 0,\n" 1429 " ONE = 1,\n" 1430 " TWO = 2,\n" 1431 " THREE = 3\n" 1432 "} LongEnum;", 1433 Style); 1434 } 1435 1436 TEST_F(FormatTest, FormatsNSEnums) { 1437 verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }"); 1438 verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n" 1439 " // Information about someDecentlyLongValue.\n" 1440 " someDecentlyLongValue,\n" 1441 " // Information about anotherDecentlyLongValue.\n" 1442 " anotherDecentlyLongValue,\n" 1443 " // Information about aThirdDecentlyLongValue.\n" 1444 " aThirdDecentlyLongValue\n" 1445 "};"); 1446 verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n" 1447 " a = 1,\n" 1448 " b = 2,\n" 1449 " c = 3,\n" 1450 "};"); 1451 verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n" 1452 " a = 1,\n" 1453 " b = 2,\n" 1454 " c = 3,\n" 1455 "};"); 1456 verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n" 1457 " a = 1,\n" 1458 " b = 2,\n" 1459 " c = 3,\n" 1460 "};"); 1461 } 1462 1463 TEST_F(FormatTest, FormatsBitfields) { 1464 verifyFormat("struct Bitfields {\n" 1465 " unsigned sClass : 8;\n" 1466 " unsigned ValueKind : 2;\n" 1467 "};"); 1468 verifyFormat("struct A {\n" 1469 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n" 1470 " bbbbbbbbbbbbbbbbbbbbbbbbb;\n" 1471 "};"); 1472 verifyFormat("struct MyStruct {\n" 1473 " uchar data;\n" 1474 " uchar : 8;\n" 1475 " uchar : 8;\n" 1476 " uchar other;\n" 1477 "};"); 1478 } 1479 1480 TEST_F(FormatTest, FormatsNamespaces) { 1481 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 1482 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 1483 1484 verifyFormat("namespace some_namespace {\n" 1485 "class A {};\n" 1486 "void f() { f(); }\n" 1487 "}", 1488 LLVMWithNoNamespaceFix); 1489 verifyFormat("namespace {\n" 1490 "class A {};\n" 1491 "void f() { f(); }\n" 1492 "}", 1493 LLVMWithNoNamespaceFix); 1494 verifyFormat("inline namespace X {\n" 1495 "class A {};\n" 1496 "void f() { f(); }\n" 1497 "}", 1498 LLVMWithNoNamespaceFix); 1499 verifyFormat("using namespace some_namespace;\n" 1500 "class A {};\n" 1501 "void f() { f(); }", 1502 LLVMWithNoNamespaceFix); 1503 1504 // This code is more common than we thought; if we 1505 // layout this correctly the semicolon will go into 1506 // its own line, which is undesirable. 1507 verifyFormat("namespace {};", 1508 LLVMWithNoNamespaceFix); 1509 verifyFormat("namespace {\n" 1510 "class A {};\n" 1511 "};", 1512 LLVMWithNoNamespaceFix); 1513 1514 verifyFormat("namespace {\n" 1515 "int SomeVariable = 0; // comment\n" 1516 "} // namespace", 1517 LLVMWithNoNamespaceFix); 1518 EXPECT_EQ("#ifndef HEADER_GUARD\n" 1519 "#define HEADER_GUARD\n" 1520 "namespace my_namespace {\n" 1521 "int i;\n" 1522 "} // my_namespace\n" 1523 "#endif // HEADER_GUARD", 1524 format("#ifndef HEADER_GUARD\n" 1525 " #define HEADER_GUARD\n" 1526 " namespace my_namespace {\n" 1527 "int i;\n" 1528 "} // my_namespace\n" 1529 "#endif // HEADER_GUARD", 1530 LLVMWithNoNamespaceFix)); 1531 1532 EXPECT_EQ("namespace A::B {\n" 1533 "class C {};\n" 1534 "}", 1535 format("namespace A::B {\n" 1536 "class C {};\n" 1537 "}", 1538 LLVMWithNoNamespaceFix)); 1539 1540 FormatStyle Style = getLLVMStyle(); 1541 Style.NamespaceIndentation = FormatStyle::NI_All; 1542 EXPECT_EQ("namespace out {\n" 1543 " int i;\n" 1544 " namespace in {\n" 1545 " int i;\n" 1546 " } // namespace in\n" 1547 "} // namespace out", 1548 format("namespace out {\n" 1549 "int i;\n" 1550 "namespace in {\n" 1551 "int i;\n" 1552 "} // namespace in\n" 1553 "} // namespace out", 1554 Style)); 1555 1556 Style.NamespaceIndentation = FormatStyle::NI_Inner; 1557 EXPECT_EQ("namespace out {\n" 1558 "int i;\n" 1559 "namespace in {\n" 1560 " int i;\n" 1561 "} // namespace in\n" 1562 "} // namespace out", 1563 format("namespace out {\n" 1564 "int i;\n" 1565 "namespace in {\n" 1566 "int i;\n" 1567 "} // namespace in\n" 1568 "} // namespace out", 1569 Style)); 1570 } 1571 1572 TEST_F(FormatTest, FormatsCompactNamespaces) { 1573 FormatStyle Style = getLLVMStyle(); 1574 Style.CompactNamespaces = true; 1575 1576 verifyFormat("namespace A { namespace B {\n" 1577 "}} // namespace A::B", 1578 Style); 1579 1580 EXPECT_EQ("namespace out { namespace in {\n" 1581 "}} // namespace out::in", 1582 format("namespace out {\n" 1583 "namespace in {\n" 1584 "} // namespace in\n" 1585 "} // namespace out", 1586 Style)); 1587 1588 // Only namespaces which have both consecutive opening and end get compacted 1589 EXPECT_EQ("namespace out {\n" 1590 "namespace in1 {\n" 1591 "} // namespace in1\n" 1592 "namespace in2 {\n" 1593 "} // namespace in2\n" 1594 "} // namespace out", 1595 format("namespace out {\n" 1596 "namespace in1 {\n" 1597 "} // namespace in1\n" 1598 "namespace in2 {\n" 1599 "} // namespace in2\n" 1600 "} // namespace out", 1601 Style)); 1602 1603 EXPECT_EQ("namespace out {\n" 1604 "int i;\n" 1605 "namespace in {\n" 1606 "int j;\n" 1607 "} // namespace in\n" 1608 "int k;\n" 1609 "} // namespace out", 1610 format("namespace out { int i;\n" 1611 "namespace in { int j; } // namespace in\n" 1612 "int k; } // namespace out", 1613 Style)); 1614 1615 EXPECT_EQ("namespace A { namespace B { namespace C {\n" 1616 "}}} // namespace A::B::C\n", 1617 format("namespace A { namespace B {\n" 1618 "namespace C {\n" 1619 "}} // namespace B::C\n" 1620 "} // namespace A\n", 1621 Style)); 1622 1623 Style.ColumnLimit = 40; 1624 EXPECT_EQ("namespace aaaaaaaaaa {\n" 1625 "namespace bbbbbbbbbb {\n" 1626 "}} // namespace aaaaaaaaaa::bbbbbbbbbb", 1627 format("namespace aaaaaaaaaa {\n" 1628 "namespace bbbbbbbbbb {\n" 1629 "} // namespace bbbbbbbbbb\n" 1630 "} // namespace aaaaaaaaaa", 1631 Style)); 1632 1633 EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n" 1634 "namespace cccccc {\n" 1635 "}}} // namespace aaaaaa::bbbbbb::cccccc", 1636 format("namespace aaaaaa {\n" 1637 "namespace bbbbbb {\n" 1638 "namespace cccccc {\n" 1639 "} // namespace cccccc\n" 1640 "} // namespace bbbbbb\n" 1641 "} // namespace aaaaaa", 1642 Style)); 1643 Style.ColumnLimit = 80; 1644 1645 // Extra semicolon after 'inner' closing brace prevents merging 1646 EXPECT_EQ("namespace out { namespace in {\n" 1647 "}; } // namespace out::in", 1648 format("namespace out {\n" 1649 "namespace in {\n" 1650 "}; // namespace in\n" 1651 "} // namespace out", 1652 Style)); 1653 1654 // Extra semicolon after 'outer' closing brace is conserved 1655 EXPECT_EQ("namespace out { namespace in {\n" 1656 "}}; // namespace out::in", 1657 format("namespace out {\n" 1658 "namespace in {\n" 1659 "} // namespace in\n" 1660 "}; // namespace out", 1661 Style)); 1662 1663 Style.NamespaceIndentation = FormatStyle::NI_All; 1664 EXPECT_EQ("namespace out { namespace in {\n" 1665 " int i;\n" 1666 "}} // namespace out::in", 1667 format("namespace out {\n" 1668 "namespace in {\n" 1669 "int i;\n" 1670 "} // namespace in\n" 1671 "} // namespace out", 1672 Style)); 1673 EXPECT_EQ("namespace out { namespace mid {\n" 1674 " namespace in {\n" 1675 " int j;\n" 1676 " } // namespace in\n" 1677 " int k;\n" 1678 "}} // namespace out::mid", 1679 format("namespace out { namespace mid {\n" 1680 "namespace in { int j; } // namespace in\n" 1681 "int k; }} // namespace out::mid", 1682 Style)); 1683 1684 Style.NamespaceIndentation = FormatStyle::NI_Inner; 1685 EXPECT_EQ("namespace out { namespace in {\n" 1686 " int i;\n" 1687 "}} // namespace out::in", 1688 format("namespace out {\n" 1689 "namespace in {\n" 1690 "int i;\n" 1691 "} // namespace in\n" 1692 "} // namespace out", 1693 Style)); 1694 EXPECT_EQ("namespace out { namespace mid { namespace in {\n" 1695 " int i;\n" 1696 "}}} // namespace out::mid::in", 1697 format("namespace out {\n" 1698 "namespace mid {\n" 1699 "namespace in {\n" 1700 "int i;\n" 1701 "} // namespace in\n" 1702 "} // namespace mid\n" 1703 "} // namespace out", 1704 Style)); 1705 } 1706 1707 TEST_F(FormatTest, FormatsExternC) { 1708 verifyFormat("extern \"C\" {\nint a;"); 1709 verifyFormat("extern \"C\" {}"); 1710 verifyFormat("extern \"C\" {\n" 1711 "int foo();\n" 1712 "}"); 1713 verifyFormat("extern \"C\" int foo() {}"); 1714 verifyFormat("extern \"C\" int foo();"); 1715 verifyFormat("extern \"C\" int foo() {\n" 1716 " int i = 42;\n" 1717 " return i;\n" 1718 "}"); 1719 1720 FormatStyle Style = getLLVMStyle(); 1721 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 1722 Style.BraceWrapping.AfterFunction = true; 1723 verifyFormat("extern \"C\" int foo() {}", Style); 1724 verifyFormat("extern \"C\" int foo();", Style); 1725 verifyFormat("extern \"C\" int foo()\n" 1726 "{\n" 1727 " int i = 42;\n" 1728 " return i;\n" 1729 "}", 1730 Style); 1731 1732 Style.BraceWrapping.AfterExternBlock = true; 1733 Style.BraceWrapping.SplitEmptyRecord = false; 1734 verifyFormat("extern \"C\"\n" 1735 "{}", 1736 Style); 1737 verifyFormat("extern \"C\"\n" 1738 "{\n" 1739 " int foo();\n" 1740 "}", 1741 Style); 1742 } 1743 1744 TEST_F(FormatTest, FormatsInlineASM) { 1745 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"); 1746 verifyFormat("asm(\"nop\" ::: \"memory\");"); 1747 verifyFormat( 1748 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" 1749 " \"cpuid\\n\\t\"\n" 1750 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n" 1751 " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n" 1752 " : \"a\"(value));"); 1753 EXPECT_EQ( 1754 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 1755 " __asm {\n" 1756 " mov edx,[that] // vtable in edx\n" 1757 " mov eax,methodIndex\n" 1758 " call [edx][eax*4] // stdcall\n" 1759 " }\n" 1760 "}", 1761 format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 1762 " __asm {\n" 1763 " mov edx,[that] // vtable in edx\n" 1764 " mov eax,methodIndex\n" 1765 " call [edx][eax*4] // stdcall\n" 1766 " }\n" 1767 "}")); 1768 EXPECT_EQ("_asm {\n" 1769 " xor eax, eax;\n" 1770 " cpuid;\n" 1771 "}", 1772 format("_asm {\n" 1773 " xor eax, eax;\n" 1774 " cpuid;\n" 1775 "}")); 1776 verifyFormat("void function() {\n" 1777 " // comment\n" 1778 " asm(\"\");\n" 1779 "}"); 1780 EXPECT_EQ("__asm {\n" 1781 "}\n" 1782 "int i;", 1783 format("__asm {\n" 1784 "}\n" 1785 "int i;")); 1786 } 1787 1788 TEST_F(FormatTest, FormatTryCatch) { 1789 verifyFormat("try {\n" 1790 " throw a * b;\n" 1791 "} catch (int a) {\n" 1792 " // Do nothing.\n" 1793 "} catch (...) {\n" 1794 " exit(42);\n" 1795 "}"); 1796 1797 // Function-level try statements. 1798 verifyFormat("int f() try { return 4; } catch (...) {\n" 1799 " return 5;\n" 1800 "}"); 1801 verifyFormat("class A {\n" 1802 " int a;\n" 1803 " A() try : a(0) {\n" 1804 " } catch (...) {\n" 1805 " throw;\n" 1806 " }\n" 1807 "};\n"); 1808 1809 // Incomplete try-catch blocks. 1810 verifyIncompleteFormat("try {} catch ("); 1811 } 1812 1813 TEST_F(FormatTest, FormatSEHTryCatch) { 1814 verifyFormat("__try {\n" 1815 " int a = b * c;\n" 1816 "} __except (EXCEPTION_EXECUTE_HANDLER) {\n" 1817 " // Do nothing.\n" 1818 "}"); 1819 1820 verifyFormat("__try {\n" 1821 " int a = b * c;\n" 1822 "} __finally {\n" 1823 " // Do nothing.\n" 1824 "}"); 1825 1826 verifyFormat("DEBUG({\n" 1827 " __try {\n" 1828 " } __finally {\n" 1829 " }\n" 1830 "});\n"); 1831 } 1832 1833 TEST_F(FormatTest, IncompleteTryCatchBlocks) { 1834 verifyFormat("try {\n" 1835 " f();\n" 1836 "} catch {\n" 1837 " g();\n" 1838 "}"); 1839 verifyFormat("try {\n" 1840 " f();\n" 1841 "} catch (A a) MACRO(x) {\n" 1842 " g();\n" 1843 "} catch (B b) MACRO(x) {\n" 1844 " g();\n" 1845 "}"); 1846 } 1847 1848 TEST_F(FormatTest, FormatTryCatchBraceStyles) { 1849 FormatStyle Style = getLLVMStyle(); 1850 for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla, 1851 FormatStyle::BS_WebKit}) { 1852 Style.BreakBeforeBraces = BraceStyle; 1853 verifyFormat("try {\n" 1854 " // something\n" 1855 "} catch (...) {\n" 1856 " // something\n" 1857 "}", 1858 Style); 1859 } 1860 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 1861 verifyFormat("try {\n" 1862 " // something\n" 1863 "}\n" 1864 "catch (...) {\n" 1865 " // something\n" 1866 "}", 1867 Style); 1868 verifyFormat("__try {\n" 1869 " // something\n" 1870 "}\n" 1871 "__finally {\n" 1872 " // something\n" 1873 "}", 1874 Style); 1875 verifyFormat("@try {\n" 1876 " // something\n" 1877 "}\n" 1878 "@finally {\n" 1879 " // something\n" 1880 "}", 1881 Style); 1882 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 1883 verifyFormat("try\n" 1884 "{\n" 1885 " // something\n" 1886 "}\n" 1887 "catch (...)\n" 1888 "{\n" 1889 " // something\n" 1890 "}", 1891 Style); 1892 Style.BreakBeforeBraces = FormatStyle::BS_GNU; 1893 verifyFormat("try\n" 1894 " {\n" 1895 " // something\n" 1896 " }\n" 1897 "catch (...)\n" 1898 " {\n" 1899 " // something\n" 1900 " }", 1901 Style); 1902 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 1903 Style.BraceWrapping.BeforeCatch = true; 1904 verifyFormat("try {\n" 1905 " // something\n" 1906 "}\n" 1907 "catch (...) {\n" 1908 " // something\n" 1909 "}", 1910 Style); 1911 } 1912 1913 TEST_F(FormatTest, StaticInitializers) { 1914 verifyFormat("static SomeClass SC = {1, 'a'};"); 1915 1916 verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n" 1917 " 100000000, " 1918 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};"); 1919 1920 // Here, everything other than the "}" would fit on a line. 1921 verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n" 1922 " 10000000000000000000000000};"); 1923 EXPECT_EQ("S s = {a,\n" 1924 "\n" 1925 " b};", 1926 format("S s = {\n" 1927 " a,\n" 1928 "\n" 1929 " b\n" 1930 "};")); 1931 1932 // FIXME: This would fit into the column limit if we'd fit "{ {" on the first 1933 // line. However, the formatting looks a bit off and this probably doesn't 1934 // happen often in practice. 1935 verifyFormat("static int Variable[1] = {\n" 1936 " {1000000000000000000000000000000000000}};", 1937 getLLVMStyleWithColumns(40)); 1938 } 1939 1940 TEST_F(FormatTest, DesignatedInitializers) { 1941 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 1942 verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n" 1943 " .bbbbbbbbbb = 2,\n" 1944 " .cccccccccc = 3,\n" 1945 " .dddddddddd = 4,\n" 1946 " .eeeeeeeeee = 5};"); 1947 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 1948 " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n" 1949 " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n" 1950 " .ccccccccccccccccccccccccccc = 3,\n" 1951 " .ddddddddddddddddddddddddddd = 4,\n" 1952 " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};"); 1953 1954 verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};"); 1955 1956 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 1957 verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n" 1958 " [2] = bbbbbbbbbb,\n" 1959 " [3] = cccccccccc,\n" 1960 " [4] = dddddddddd,\n" 1961 " [5] = eeeeeeeeee};"); 1962 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 1963 " [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1964 " [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 1965 " [3] = cccccccccccccccccccccccccccccccccccccc,\n" 1966 " [4] = dddddddddddddddddddddddddddddddddddddd,\n" 1967 " [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};"); 1968 } 1969 1970 TEST_F(FormatTest, NestedStaticInitializers) { 1971 verifyFormat("static A x = {{{}}};\n"); 1972 verifyFormat("static A x = {{{init1, init2, init3, init4},\n" 1973 " {init1, init2, init3, init4}}};", 1974 getLLVMStyleWithColumns(50)); 1975 1976 verifyFormat("somes Status::global_reps[3] = {\n" 1977 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 1978 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 1979 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};", 1980 getLLVMStyleWithColumns(60)); 1981 verifyGoogleFormat("SomeType 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 verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n" 1986 " {rect.fRight - rect.fLeft, rect.fBottom - " 1987 "rect.fTop}};"); 1988 1989 verifyFormat( 1990 "SomeArrayOfSomeType a = {\n" 1991 " {{1, 2, 3},\n" 1992 " {1, 2, 3},\n" 1993 " {111111111111111111111111111111, 222222222222222222222222222222,\n" 1994 " 333333333333333333333333333333},\n" 1995 " {1, 2, 3},\n" 1996 " {1, 2, 3}}};"); 1997 verifyFormat( 1998 "SomeArrayOfSomeType a = {\n" 1999 " {{1, 2, 3}},\n" 2000 " {{1, 2, 3}},\n" 2001 " {{111111111111111111111111111111, 222222222222222222222222222222,\n" 2002 " 333333333333333333333333333333}},\n" 2003 " {{1, 2, 3}},\n" 2004 " {{1, 2, 3}}};"); 2005 2006 verifyFormat("struct {\n" 2007 " unsigned bit;\n" 2008 " const char *const name;\n" 2009 "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n" 2010 " {kOsWin, \"Windows\"},\n" 2011 " {kOsLinux, \"Linux\"},\n" 2012 " {kOsCrOS, \"Chrome OS\"}};"); 2013 verifyFormat("struct {\n" 2014 " unsigned bit;\n" 2015 " const char *const name;\n" 2016 "} kBitsToOs[] = {\n" 2017 " {kOsMac, \"Mac\"},\n" 2018 " {kOsWin, \"Windows\"},\n" 2019 " {kOsLinux, \"Linux\"},\n" 2020 " {kOsCrOS, \"Chrome OS\"},\n" 2021 "};"); 2022 } 2023 2024 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { 2025 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 2026 " \\\n" 2027 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); 2028 } 2029 2030 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { 2031 verifyFormat("virtual void write(ELFWriter *writerrr,\n" 2032 " OwningPtr<FileOutputBuffer> &buffer) = 0;"); 2033 2034 // Do break defaulted and deleted functions. 2035 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 2036 " default;", 2037 getLLVMStyleWithColumns(40)); 2038 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 2039 " delete;", 2040 getLLVMStyleWithColumns(40)); 2041 } 2042 2043 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) { 2044 verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3", 2045 getLLVMStyleWithColumns(40)); 2046 verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 2047 getLLVMStyleWithColumns(40)); 2048 EXPECT_EQ("#define Q \\\n" 2049 " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n" 2050 " \"aaaaaaaa.cpp\"", 2051 format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 2052 getLLVMStyleWithColumns(40))); 2053 } 2054 2055 TEST_F(FormatTest, UnderstandsLinePPDirective) { 2056 EXPECT_EQ("# 123 \"A string literal\"", 2057 format(" # 123 \"A string literal\"")); 2058 } 2059 2060 TEST_F(FormatTest, LayoutUnknownPPDirective) { 2061 EXPECT_EQ("#;", format("#;")); 2062 verifyFormat("#\n;\n;\n;"); 2063 } 2064 2065 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { 2066 EXPECT_EQ("#line 42 \"test\"\n", 2067 format("# \\\n line \\\n 42 \\\n \"test\"\n")); 2068 EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n", 2069 getLLVMStyleWithColumns(12))); 2070 } 2071 2072 TEST_F(FormatTest, EndOfFileEndsPPDirective) { 2073 EXPECT_EQ("#line 42 \"test\"", 2074 format("# \\\n line \\\n 42 \\\n \"test\"")); 2075 EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B")); 2076 } 2077 2078 TEST_F(FormatTest, DoesntRemoveUnknownTokens) { 2079 verifyFormat("#define A \\x20"); 2080 verifyFormat("#define A \\ x20"); 2081 EXPECT_EQ("#define A \\ x20", format("#define A \\ x20")); 2082 verifyFormat("#define A ''"); 2083 verifyFormat("#define A ''qqq"); 2084 verifyFormat("#define A `qqq"); 2085 verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");"); 2086 EXPECT_EQ("const char *c = STRINGIFY(\n" 2087 "\\na : b);", 2088 format("const char * c = STRINGIFY(\n" 2089 "\\na : b);")); 2090 2091 verifyFormat("a\r\\"); 2092 verifyFormat("a\v\\"); 2093 verifyFormat("a\f\\"); 2094 } 2095 2096 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { 2097 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13)); 2098 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12)); 2099 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); 2100 // FIXME: We never break before the macro name. 2101 verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12)); 2102 2103 verifyFormat("#define A A\n#define A A"); 2104 verifyFormat("#define A(X) A\n#define A A"); 2105 2106 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23)); 2107 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22)); 2108 } 2109 2110 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { 2111 EXPECT_EQ("// somecomment\n" 2112 "#include \"a.h\"\n" 2113 "#define A( \\\n" 2114 " A, B)\n" 2115 "#include \"b.h\"\n" 2116 "// somecomment\n", 2117 format(" // somecomment\n" 2118 " #include \"a.h\"\n" 2119 "#define A(A,\\\n" 2120 " B)\n" 2121 " #include \"b.h\"\n" 2122 " // somecomment\n", 2123 getLLVMStyleWithColumns(13))); 2124 } 2125 2126 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); } 2127 2128 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { 2129 EXPECT_EQ("#define A \\\n" 2130 " c; \\\n" 2131 " e;\n" 2132 "f;", 2133 format("#define A c; e;\n" 2134 "f;", 2135 getLLVMStyleWithColumns(14))); 2136 } 2137 2138 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); } 2139 2140 TEST_F(FormatTest, MacroDefinitionInsideStatement) { 2141 EXPECT_EQ("int x,\n" 2142 "#define A\n" 2143 " y;", 2144 format("int x,\n#define A\ny;")); 2145 } 2146 2147 TEST_F(FormatTest, HashInMacroDefinition) { 2148 EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle())); 2149 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); 2150 verifyFormat("#define A \\\n" 2151 " { \\\n" 2152 " f(#c); \\\n" 2153 " }", 2154 getLLVMStyleWithColumns(11)); 2155 2156 verifyFormat("#define A(X) \\\n" 2157 " void function##X()", 2158 getLLVMStyleWithColumns(22)); 2159 2160 verifyFormat("#define A(a, b, c) \\\n" 2161 " void a##b##c()", 2162 getLLVMStyleWithColumns(22)); 2163 2164 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22)); 2165 } 2166 2167 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { 2168 EXPECT_EQ("#define A (x)", format("#define A (x)")); 2169 EXPECT_EQ("#define A(x)", format("#define A(x)")); 2170 } 2171 2172 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) { 2173 EXPECT_EQ("#define A b;", format("#define A \\\n" 2174 " \\\n" 2175 " b;", 2176 getLLVMStyleWithColumns(25))); 2177 EXPECT_EQ("#define A \\\n" 2178 " \\\n" 2179 " a; \\\n" 2180 " b;", 2181 format("#define A \\\n" 2182 " \\\n" 2183 " a; \\\n" 2184 " b;", 2185 getLLVMStyleWithColumns(11))); 2186 EXPECT_EQ("#define A \\\n" 2187 " a; \\\n" 2188 " \\\n" 2189 " b;", 2190 format("#define A \\\n" 2191 " a; \\\n" 2192 " \\\n" 2193 " b;", 2194 getLLVMStyleWithColumns(11))); 2195 } 2196 2197 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { 2198 verifyIncompleteFormat("#define A :"); 2199 verifyFormat("#define SOMECASES \\\n" 2200 " case 1: \\\n" 2201 " case 2\n", 2202 getLLVMStyleWithColumns(20)); 2203 verifyFormat("#define MACRO(a) \\\n" 2204 " if (a) \\\n" 2205 " f(); \\\n" 2206 " else \\\n" 2207 " g()", 2208 getLLVMStyleWithColumns(18)); 2209 verifyFormat("#define A template <typename T>"); 2210 verifyIncompleteFormat("#define STR(x) #x\n" 2211 "f(STR(this_is_a_string_literal{));"); 2212 verifyFormat("#pragma omp threadprivate( \\\n" 2213 " y)), // expected-warning", 2214 getLLVMStyleWithColumns(28)); 2215 verifyFormat("#d, = };"); 2216 verifyFormat("#if \"a"); 2217 verifyIncompleteFormat("({\n" 2218 "#define b \\\n" 2219 " } \\\n" 2220 " a\n" 2221 "a", 2222 getLLVMStyleWithColumns(15)); 2223 verifyFormat("#define A \\\n" 2224 " { \\\n" 2225 " {\n" 2226 "#define B \\\n" 2227 " } \\\n" 2228 " }", 2229 getLLVMStyleWithColumns(15)); 2230 verifyNoCrash("#if a\na(\n#else\n#endif\n{a"); 2231 verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}"); 2232 verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};"); 2233 verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}"); 2234 } 2235 2236 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) { 2237 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline. 2238 EXPECT_EQ("class A : public QObject {\n" 2239 " Q_OBJECT\n" 2240 "\n" 2241 " A() {}\n" 2242 "};", 2243 format("class A : public QObject {\n" 2244 " Q_OBJECT\n" 2245 "\n" 2246 " A() {\n}\n" 2247 "} ;")); 2248 EXPECT_EQ("MACRO\n" 2249 "/*static*/ int i;", 2250 format("MACRO\n" 2251 " /*static*/ int i;")); 2252 EXPECT_EQ("SOME_MACRO\n" 2253 "namespace {\n" 2254 "void f();\n" 2255 "} // namespace", 2256 format("SOME_MACRO\n" 2257 " namespace {\n" 2258 "void f( );\n" 2259 "} // namespace")); 2260 // Only if the identifier contains at least 5 characters. 2261 EXPECT_EQ("HTTP f();", format("HTTP\nf();")); 2262 EXPECT_EQ("MACRO\nf();", format("MACRO\nf();")); 2263 // Only if everything is upper case. 2264 EXPECT_EQ("class A : public QObject {\n" 2265 " Q_Object A() {}\n" 2266 "};", 2267 format("class A : public QObject {\n" 2268 " Q_Object\n" 2269 " A() {\n}\n" 2270 "} ;")); 2271 2272 // Only if the next line can actually start an unwrapped line. 2273 EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;", 2274 format("SOME_WEIRD_LOG_MACRO\n" 2275 "<< SomeThing;")); 2276 2277 verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), " 2278 "(n, buffers))\n", 2279 getChromiumStyle(FormatStyle::LK_Cpp)); 2280 } 2281 2282 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) { 2283 EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 2284 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 2285 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 2286 "class X {};\n" 2287 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 2288 "int *createScopDetectionPass() { return 0; }", 2289 format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 2290 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 2291 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 2292 " class X {};\n" 2293 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 2294 " int *createScopDetectionPass() { return 0; }")); 2295 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as 2296 // braces, so that inner block is indented one level more. 2297 EXPECT_EQ("int q() {\n" 2298 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 2299 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 2300 " IPC_END_MESSAGE_MAP()\n" 2301 "}", 2302 format("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 2308 // Same inside macros. 2309 EXPECT_EQ("#define LIST(L) \\\n" 2310 " L(A) \\\n" 2311 " L(B) \\\n" 2312 " L(C)", 2313 format("#define LIST(L) \\\n" 2314 " L(A) \\\n" 2315 " L(B) \\\n" 2316 " L(C)", 2317 getGoogleStyle())); 2318 2319 // These must not be recognized as macros. 2320 EXPECT_EQ("int q() {\n" 2321 " f(x);\n" 2322 " f(x) {}\n" 2323 " f(x)->g();\n" 2324 " f(x)->*g();\n" 2325 " f(x).g();\n" 2326 " f(x) = x;\n" 2327 " f(x) += x;\n" 2328 " f(x) -= x;\n" 2329 " f(x) *= x;\n" 2330 " f(x) /= x;\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)[y].z();\n" 2338 " LOG(INFO) << x;\n" 2339 " ifstream(x) >> x;\n" 2340 "}\n", 2341 format("int q() {\n" 2342 " f(x)\n;\n" 2343 " f(x)\n {}\n" 2344 " f(x)\n->g();\n" 2345 " f(x)\n->*g();\n" 2346 " f(x)\n.g();\n" 2347 " f(x)\n = x;\n" 2348 " f(x)\n += x;\n" 2349 " f(x)\n -= x;\n" 2350 " f(x)\n *= x;\n" 2351 " f(x)\n /= x;\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[y].z();\n" 2359 " LOG(INFO)\n << x;\n" 2360 " ifstream(x)\n >> x;\n" 2361 "}\n")); 2362 EXPECT_EQ("int q() {\n" 2363 " F(x)\n" 2364 " if (1) {\n" 2365 " }\n" 2366 " F(x)\n" 2367 " while (1) {\n" 2368 " }\n" 2369 " F(x)\n" 2370 " G(x);\n" 2371 " F(x)\n" 2372 " try {\n" 2373 " Q();\n" 2374 " } catch (...) {\n" 2375 " }\n" 2376 "}\n", 2377 format("int q() {\n" 2378 "F(x)\n" 2379 "if (1) {}\n" 2380 "F(x)\n" 2381 "while (1) {}\n" 2382 "F(x)\n" 2383 "G(x);\n" 2384 "F(x)\n" 2385 "try { Q(); } catch (...) {}\n" 2386 "}\n")); 2387 EXPECT_EQ("class A {\n" 2388 " A() : t(0) {}\n" 2389 " A(int i) noexcept() : {}\n" 2390 " A(X x)\n" // FIXME: function-level try blocks are broken. 2391 " try : t(0) {\n" 2392 " } catch (...) {\n" 2393 " }\n" 2394 "};", 2395 format("class A {\n" 2396 " A()\n : t(0) {}\n" 2397 " A(int i)\n noexcept() : {}\n" 2398 " A(X x)\n" 2399 " try : t(0) {} catch (...) {}\n" 2400 "};")); 2401 EXPECT_EQ("class SomeClass {\n" 2402 "public:\n" 2403 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2404 "};", 2405 format("class SomeClass {\n" 2406 "public:\n" 2407 " SomeClass()\n" 2408 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2409 "};")); 2410 EXPECT_EQ("class SomeClass {\n" 2411 "public:\n" 2412 " SomeClass()\n" 2413 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2414 "};", 2415 format("class SomeClass {\n" 2416 "public:\n" 2417 " SomeClass()\n" 2418 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2419 "};", 2420 getLLVMStyleWithColumns(40))); 2421 2422 verifyFormat("MACRO(>)"); 2423 } 2424 2425 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) { 2426 verifyFormat("#define A \\\n" 2427 " f({ \\\n" 2428 " g(); \\\n" 2429 " });", 2430 getLLVMStyleWithColumns(11)); 2431 } 2432 2433 TEST_F(FormatTest, IndentPreprocessorDirectives) { 2434 FormatStyle Style = getLLVMStyle(); 2435 Style.IndentPPDirectives = FormatStyle::PPDIS_None; 2436 Style.ColumnLimit = 40; 2437 verifyFormat("#ifdef _WIN32\n" 2438 "#define A 0\n" 2439 "#ifdef VAR2\n" 2440 "#define B 1\n" 2441 "#include <someheader.h>\n" 2442 "#define MACRO \\\n" 2443 " some_very_long_func_aaaaaaaaaa();\n" 2444 "#endif\n" 2445 "#else\n" 2446 "#define A 1\n" 2447 "#endif", 2448 Style); 2449 Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash; 2450 verifyFormat("#ifdef _WIN32\n" 2451 "# define A 0\n" 2452 "# ifdef VAR2\n" 2453 "# define B 1\n" 2454 "# include <someheader.h>\n" 2455 "# define MACRO \\\n" 2456 " some_very_long_func_aaaaaaaaaa();\n" 2457 "# endif\n" 2458 "#else\n" 2459 "# define A 1\n" 2460 "#endif", 2461 Style); 2462 verifyFormat("#if A\n" 2463 "# define MACRO \\\n" 2464 " void a(int x) { \\\n" 2465 " b(); \\\n" 2466 " c(); \\\n" 2467 " d(); \\\n" 2468 " e(); \\\n" 2469 " f(); \\\n" 2470 " }\n" 2471 "#endif", 2472 Style); 2473 // Comments before include guard. 2474 verifyFormat("// file comment\n" 2475 "// file comment\n" 2476 "#ifndef HEADER_H\n" 2477 "#define HEADER_H\n" 2478 "code();\n" 2479 "#endif", 2480 Style); 2481 // Test with include guards. 2482 // EXPECT_EQ is used because verifyFormat() calls messUp() which incorrectly 2483 // merges lines. 2484 verifyFormat("#ifndef HEADER_H\n" 2485 "#define HEADER_H\n" 2486 "code();\n" 2487 "#endif", 2488 Style); 2489 // Include guards must have a #define with the same variable immediately 2490 // after #ifndef. 2491 verifyFormat("#ifndef NOT_GUARD\n" 2492 "# define FOO\n" 2493 "code();\n" 2494 "#endif", 2495 Style); 2496 2497 // Include guards must cover the entire file. 2498 verifyFormat("code();\n" 2499 "code();\n" 2500 "#ifndef NOT_GUARD\n" 2501 "# define NOT_GUARD\n" 2502 "code();\n" 2503 "#endif", 2504 Style); 2505 verifyFormat("#ifndef NOT_GUARD\n" 2506 "# define NOT_GUARD\n" 2507 "code();\n" 2508 "#endif\n" 2509 "code();", 2510 Style); 2511 // Test with trailing blank lines. 2512 verifyFormat("#ifndef HEADER_H\n" 2513 "#define HEADER_H\n" 2514 "code();\n" 2515 "#endif\n", 2516 Style); 2517 // Include guards don't have #else. 2518 verifyFormat("#ifndef NOT_GUARD\n" 2519 "# define NOT_GUARD\n" 2520 "code();\n" 2521 "#else\n" 2522 "#endif", 2523 Style); 2524 verifyFormat("#ifndef NOT_GUARD\n" 2525 "# define NOT_GUARD\n" 2526 "code();\n" 2527 "#elif FOO\n" 2528 "#endif", 2529 Style); 2530 // FIXME: This doesn't handle the case where there's code between the 2531 // #ifndef and #define but all other conditions hold. This is because when 2532 // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the 2533 // previous code line yet, so we can't detect it. 2534 EXPECT_EQ("#ifndef NOT_GUARD\n" 2535 "code();\n" 2536 "#define NOT_GUARD\n" 2537 "code();\n" 2538 "#endif", 2539 format("#ifndef NOT_GUARD\n" 2540 "code();\n" 2541 "# define NOT_GUARD\n" 2542 "code();\n" 2543 "#endif", 2544 Style)); 2545 // FIXME: This doesn't handle cases where legitimate preprocessor lines may 2546 // be outside an include guard. Examples are #pragma once and 2547 // #pragma GCC diagnostic, or anything else that does not change the meaning 2548 // of the file if it's included multiple times. 2549 EXPECT_EQ("#ifdef WIN32\n" 2550 "# pragma once\n" 2551 "#endif\n" 2552 "#ifndef HEADER_H\n" 2553 "# define HEADER_H\n" 2554 "code();\n" 2555 "#endif", 2556 format("#ifdef WIN32\n" 2557 "# pragma once\n" 2558 "#endif\n" 2559 "#ifndef HEADER_H\n" 2560 "#define HEADER_H\n" 2561 "code();\n" 2562 "#endif", 2563 Style)); 2564 // FIXME: This does not detect when there is a single non-preprocessor line 2565 // in front of an include-guard-like structure where other conditions hold 2566 // because ScopedLineState hides the line. 2567 EXPECT_EQ("code();\n" 2568 "#ifndef HEADER_H\n" 2569 "#define HEADER_H\n" 2570 "code();\n" 2571 "#endif", 2572 format("code();\n" 2573 "#ifndef HEADER_H\n" 2574 "# define HEADER_H\n" 2575 "code();\n" 2576 "#endif", 2577 Style)); 2578 // FIXME: The comment indent corrector in TokenAnnotator gets thrown off by 2579 // preprocessor indentation. 2580 EXPECT_EQ("#if 1\n" 2581 " // comment\n" 2582 "# define A 0\n" 2583 "// comment\n" 2584 "# define B 0\n" 2585 "#endif", 2586 format("#if 1\n" 2587 "// comment\n" 2588 "# define A 0\n" 2589 " // comment\n" 2590 "# define B 0\n" 2591 "#endif", 2592 Style)); 2593 // Test with tabs. 2594 Style.UseTab = FormatStyle::UT_Always; 2595 Style.IndentWidth = 8; 2596 Style.TabWidth = 8; 2597 verifyFormat("#ifdef _WIN32\n" 2598 "#\tdefine A 0\n" 2599 "#\tifdef VAR2\n" 2600 "#\t\tdefine B 1\n" 2601 "#\t\tinclude <someheader.h>\n" 2602 "#\t\tdefine MACRO \\\n" 2603 "\t\t\tsome_very_long_func_aaaaaaaaaa();\n" 2604 "#\tendif\n" 2605 "#else\n" 2606 "#\tdefine A 1\n" 2607 "#endif", 2608 Style); 2609 2610 // Regression test: Multiline-macro inside include guards. 2611 verifyFormat("#ifndef HEADER_H\n" 2612 "#define HEADER_H\n" 2613 "#define A() \\\n" 2614 " int i; \\\n" 2615 " int j;\n" 2616 "#endif // HEADER_H", 2617 getLLVMStyleWithColumns(20)); 2618 } 2619 2620 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { 2621 verifyFormat("{\n { a #c; }\n}"); 2622 } 2623 2624 TEST_F(FormatTest, FormatUnbalancedStructuralElements) { 2625 EXPECT_EQ("#define A \\\n { \\\n {\nint i;", 2626 format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); 2627 EXPECT_EQ("#define A \\\n } \\\n }\nint i;", 2628 format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); 2629 } 2630 2631 TEST_F(FormatTest, EscapedNewlines) { 2632 EXPECT_EQ( 2633 "#define A \\\n int i; \\\n int j;", 2634 format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11))); 2635 EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;")); 2636 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 2637 EXPECT_EQ("/* \\ \\ \\\n */", format("\\\n/* \\ \\ \\\n */")); 2638 EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>")); 2639 2640 FormatStyle DontAlign = getLLVMStyle(); 2641 DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 2642 DontAlign.MaxEmptyLinesToKeep = 3; 2643 // FIXME: can't use verifyFormat here because the newline before 2644 // "public:" is not inserted the first time it's reformatted 2645 EXPECT_EQ("#define A \\\n" 2646 " class Foo { \\\n" 2647 " void bar(); \\\n" 2648 "\\\n" 2649 "\\\n" 2650 "\\\n" 2651 " public: \\\n" 2652 " void baz(); \\\n" 2653 " };", 2654 format("#define A \\\n" 2655 " class Foo { \\\n" 2656 " void bar(); \\\n" 2657 "\\\n" 2658 "\\\n" 2659 "\\\n" 2660 " public: \\\n" 2661 " void baz(); \\\n" 2662 " };", DontAlign)); 2663 } 2664 2665 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { 2666 verifyFormat("#define A \\\n" 2667 " int v( \\\n" 2668 " a); \\\n" 2669 " int i;", 2670 getLLVMStyleWithColumns(11)); 2671 } 2672 2673 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { 2674 EXPECT_EQ( 2675 "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 2676 " \\\n" 2677 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 2678 "\n" 2679 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 2680 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", 2681 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" 2682 "\\\n" 2683 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 2684 " \n" 2685 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 2686 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); 2687 } 2688 2689 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { 2690 EXPECT_EQ("int\n" 2691 "#define A\n" 2692 " a;", 2693 format("int\n#define A\na;")); 2694 verifyFormat("functionCallTo(\n" 2695 " someOtherFunction(\n" 2696 " withSomeParameters, whichInSequence,\n" 2697 " areLongerThanALine(andAnotherCall,\n" 2698 "#define A B\n" 2699 " withMoreParamters,\n" 2700 " whichStronglyInfluenceTheLayout),\n" 2701 " andMoreParameters),\n" 2702 " trailing);", 2703 getLLVMStyleWithColumns(69)); 2704 verifyFormat("Foo::Foo()\n" 2705 "#ifdef BAR\n" 2706 " : baz(0)\n" 2707 "#endif\n" 2708 "{\n" 2709 "}"); 2710 verifyFormat("void f() {\n" 2711 " if (true)\n" 2712 "#ifdef A\n" 2713 " f(42);\n" 2714 " x();\n" 2715 "#else\n" 2716 " g();\n" 2717 " x();\n" 2718 "#endif\n" 2719 "}"); 2720 verifyFormat("void f(param1, param2,\n" 2721 " param3,\n" 2722 "#ifdef A\n" 2723 " param4(param5,\n" 2724 "#ifdef A1\n" 2725 " param6,\n" 2726 "#ifdef A2\n" 2727 " param7),\n" 2728 "#else\n" 2729 " param8),\n" 2730 " param9,\n" 2731 "#endif\n" 2732 " param10,\n" 2733 "#endif\n" 2734 " param11)\n" 2735 "#else\n" 2736 " param12)\n" 2737 "#endif\n" 2738 "{\n" 2739 " x();\n" 2740 "}", 2741 getLLVMStyleWithColumns(28)); 2742 verifyFormat("#if 1\n" 2743 "int i;"); 2744 verifyFormat("#if 1\n" 2745 "#endif\n" 2746 "#if 1\n" 2747 "#else\n" 2748 "#endif\n"); 2749 verifyFormat("DEBUG({\n" 2750 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2751 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 2752 "});\n" 2753 "#if a\n" 2754 "#else\n" 2755 "#endif"); 2756 2757 verifyIncompleteFormat("void f(\n" 2758 "#if A\n" 2759 ");\n" 2760 "#else\n" 2761 "#endif"); 2762 } 2763 2764 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) { 2765 verifyFormat("#endif\n" 2766 "#if B"); 2767 } 2768 2769 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) { 2770 FormatStyle SingleLine = getLLVMStyle(); 2771 SingleLine.AllowShortIfStatementsOnASingleLine = true; 2772 verifyFormat("#if 0\n" 2773 "#elif 1\n" 2774 "#endif\n" 2775 "void foo() {\n" 2776 " if (test) foo2();\n" 2777 "}", 2778 SingleLine); 2779 } 2780 2781 TEST_F(FormatTest, LayoutBlockInsideParens) { 2782 verifyFormat("functionCall({ int i; });"); 2783 verifyFormat("functionCall({\n" 2784 " int i;\n" 2785 " int j;\n" 2786 "});"); 2787 verifyFormat("functionCall(\n" 2788 " {\n" 2789 " int i;\n" 2790 " int j;\n" 2791 " },\n" 2792 " aaaa, bbbb, cccc);"); 2793 verifyFormat("functionA(functionB({\n" 2794 " int i;\n" 2795 " int j;\n" 2796 " }),\n" 2797 " aaaa, bbbb, cccc);"); 2798 verifyFormat("functionCall(\n" 2799 " {\n" 2800 " int i;\n" 2801 " int j;\n" 2802 " },\n" 2803 " aaaa, bbbb, // comment\n" 2804 " cccc);"); 2805 verifyFormat("functionA(functionB({\n" 2806 " int i;\n" 2807 " int j;\n" 2808 " }),\n" 2809 " aaaa, bbbb, // comment\n" 2810 " cccc);"); 2811 verifyFormat("functionCall(aaaa, bbbb, { int i; });"); 2812 verifyFormat("functionCall(aaaa, bbbb, {\n" 2813 " int i;\n" 2814 " int j;\n" 2815 "});"); 2816 verifyFormat( 2817 "Aaa(\n" // FIXME: There shouldn't be a linebreak here. 2818 " {\n" 2819 " int i; // break\n" 2820 " },\n" 2821 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 2822 " ccccccccccccccccc));"); 2823 verifyFormat("DEBUG({\n" 2824 " if (a)\n" 2825 " f();\n" 2826 "});"); 2827 } 2828 2829 TEST_F(FormatTest, LayoutBlockInsideStatement) { 2830 EXPECT_EQ("SOME_MACRO { int i; }\n" 2831 "int i;", 2832 format(" SOME_MACRO {int i;} int i;")); 2833 } 2834 2835 TEST_F(FormatTest, LayoutNestedBlocks) { 2836 verifyFormat("void AddOsStrings(unsigned bitmask) {\n" 2837 " struct s {\n" 2838 " int i;\n" 2839 " };\n" 2840 " s kBitsToOs[] = {{10}};\n" 2841 " for (int i = 0; i < 10; ++i)\n" 2842 " return;\n" 2843 "}"); 2844 verifyFormat("call(parameter, {\n" 2845 " something();\n" 2846 " // Comment using all columns.\n" 2847 " somethingelse();\n" 2848 "});", 2849 getLLVMStyleWithColumns(40)); 2850 verifyFormat("DEBUG( //\n" 2851 " { f(); }, a);"); 2852 verifyFormat("DEBUG( //\n" 2853 " {\n" 2854 " f(); //\n" 2855 " },\n" 2856 " a);"); 2857 2858 EXPECT_EQ("call(parameter, {\n" 2859 " something();\n" 2860 " // Comment too\n" 2861 " // looooooooooong.\n" 2862 " somethingElse();\n" 2863 "});", 2864 format("call(parameter, {\n" 2865 " something();\n" 2866 " // Comment too looooooooooong.\n" 2867 " somethingElse();\n" 2868 "});", 2869 getLLVMStyleWithColumns(29))); 2870 EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });")); 2871 EXPECT_EQ("DEBUG({ // comment\n" 2872 " int i;\n" 2873 "});", 2874 format("DEBUG({ // comment\n" 2875 "int i;\n" 2876 "});")); 2877 EXPECT_EQ("DEBUG({\n" 2878 " int i;\n" 2879 "\n" 2880 " // comment\n" 2881 " int j;\n" 2882 "});", 2883 format("DEBUG({\n" 2884 " int i;\n" 2885 "\n" 2886 " // comment\n" 2887 " int j;\n" 2888 "});")); 2889 2890 verifyFormat("DEBUG({\n" 2891 " if (a)\n" 2892 " return;\n" 2893 "});"); 2894 verifyGoogleFormat("DEBUG({\n" 2895 " if (a) return;\n" 2896 "});"); 2897 FormatStyle Style = getGoogleStyle(); 2898 Style.ColumnLimit = 45; 2899 verifyFormat("Debug(aaaaa,\n" 2900 " {\n" 2901 " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n" 2902 " },\n" 2903 " a);", 2904 Style); 2905 2906 verifyFormat("SomeFunction({MACRO({ return output; }), b});"); 2907 2908 verifyNoCrash("^{v^{a}}"); 2909 } 2910 2911 TEST_F(FormatTest, FormatNestedBlocksInMacros) { 2912 EXPECT_EQ("#define MACRO() \\\n" 2913 " Debug(aaa, /* force line break */ \\\n" 2914 " { \\\n" 2915 " int i; \\\n" 2916 " int j; \\\n" 2917 " })", 2918 format("#define MACRO() Debug(aaa, /* force line break */ \\\n" 2919 " { int i; int j; })", 2920 getGoogleStyle())); 2921 2922 EXPECT_EQ("#define A \\\n" 2923 " [] { \\\n" 2924 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 2925 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 2926 " }", 2927 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 2928 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 2929 getGoogleStyle())); 2930 } 2931 2932 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { 2933 EXPECT_EQ("{}", format("{}")); 2934 verifyFormat("enum E {};"); 2935 verifyFormat("enum E {}"); 2936 } 2937 2938 TEST_F(FormatTest, FormatBeginBlockEndMacros) { 2939 FormatStyle Style = getLLVMStyle(); 2940 Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$"; 2941 Style.MacroBlockEnd = "^[A-Z_]+_END$"; 2942 verifyFormat("FOO_BEGIN\n" 2943 " FOO_ENTRY\n" 2944 "FOO_END", Style); 2945 verifyFormat("FOO_BEGIN\n" 2946 " NESTED_FOO_BEGIN\n" 2947 " NESTED_FOO_ENTRY\n" 2948 " NESTED_FOO_END\n" 2949 "FOO_END", Style); 2950 verifyFormat("FOO_BEGIN(Foo, Bar)\n" 2951 " int x;\n" 2952 " x = 1;\n" 2953 "FOO_END(Baz)", Style); 2954 } 2955 2956 //===----------------------------------------------------------------------===// 2957 // Line break tests. 2958 //===----------------------------------------------------------------------===// 2959 2960 TEST_F(FormatTest, PreventConfusingIndents) { 2961 verifyFormat( 2962 "void f() {\n" 2963 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" 2964 " parameter, parameter, parameter)),\n" 2965 " SecondLongCall(parameter));\n" 2966 "}"); 2967 verifyFormat( 2968 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2969 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 2970 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 2971 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 2972 verifyFormat( 2973 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2974 " [aaaaaaaaaaaaaaaaaaaaaaaa\n" 2975 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 2976 " [aaaaaaaaaaaaaaaaaaaaaaaa]];"); 2977 verifyFormat( 2978 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 2979 " aaaaaaaaaaaaaaaaaaaaaaaa<\n" 2980 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n" 2981 " aaaaaaaaaaaaaaaaaaaaaaaa>;"); 2982 verifyFormat("int a = bbbb && ccc &&\n" 2983 " fffff(\n" 2984 "#define A Just forcing a new line\n" 2985 " ddd);"); 2986 } 2987 2988 TEST_F(FormatTest, LineBreakingInBinaryExpressions) { 2989 verifyFormat( 2990 "bool aaaaaaa =\n" 2991 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n" 2992 " bbbbbbbb();"); 2993 verifyFormat( 2994 "bool aaaaaaa =\n" 2995 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n" 2996 " bbbbbbbb();"); 2997 2998 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 2999 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n" 3000 " ccccccccc == ddddddddddd;"); 3001 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 3002 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n" 3003 " ccccccccc == ddddddddddd;"); 3004 verifyFormat( 3005 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 3006 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n" 3007 " ccccccccc == ddddddddddd;"); 3008 3009 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 3010 " aaaaaa) &&\n" 3011 " bbbbbb && cccccc;"); 3012 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 3013 " aaaaaa) >>\n" 3014 " bbbbbb;"); 3015 verifyFormat("aa = Whitespaces.addUntouchableComment(\n" 3016 " SourceMgr.getSpellingColumnNumber(\n" 3017 " TheLine.Last->FormatTok.Tok.getLocation()) -\n" 3018 " 1);"); 3019 3020 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3021 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n" 3022 " cccccc) {\n}"); 3023 verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3024 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n" 3025 " cccccc) {\n}"); 3026 verifyFormat("b = a &&\n" 3027 " // Comment\n" 3028 " b.c && d;"); 3029 3030 // If the LHS of a comparison is not a binary expression itself, the 3031 // additional linebreak confuses many people. 3032 verifyFormat( 3033 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3034 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n" 3035 "}"); 3036 verifyFormat( 3037 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3038 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3039 "}"); 3040 verifyFormat( 3041 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n" 3042 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3043 "}"); 3044 // Even explicit parentheses stress the precedence enough to make the 3045 // additional break unnecessary. 3046 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3047 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3048 "}"); 3049 // This cases is borderline, but with the indentation it is still readable. 3050 verifyFormat( 3051 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3052 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3053 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 3054 "}", 3055 getLLVMStyleWithColumns(75)); 3056 3057 // If the LHS is a binary expression, we should still use the additional break 3058 // as otherwise the formatting hides the operator precedence. 3059 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3060 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3061 " 5) {\n" 3062 "}"); 3063 3064 FormatStyle OnePerLine = getLLVMStyle(); 3065 OnePerLine.BinPackParameters = false; 3066 verifyFormat( 3067 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3068 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3069 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}", 3070 OnePerLine); 3071 3072 verifyFormat("int i = someFunction(aaaaaaa, 0)\n" 3073 " .aaa(aaaaaaaaaaaaa) *\n" 3074 " aaaaaaa +\n" 3075 " aaaaaaa;", 3076 getLLVMStyleWithColumns(40)); 3077 } 3078 3079 TEST_F(FormatTest, ExpressionIndentation) { 3080 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3081 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3082 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3083 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3084 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 3085 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n" 3086 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3087 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n" 3088 " ccccccccccccccccccccccccccccccccccccccccc;"); 3089 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3090 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3091 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3092 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3093 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3094 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3095 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3096 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3097 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3098 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3099 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3100 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3101 verifyFormat("if () {\n" 3102 "} else if (aaaaa && bbbbb > // break\n" 3103 " ccccc) {\n" 3104 "}"); 3105 verifyFormat("if () {\n" 3106 "} else if (aaaaa &&\n" 3107 " bbbbb > // break\n" 3108 " ccccc &&\n" 3109 " ddddd) {\n" 3110 "}"); 3111 3112 // Presence of a trailing comment used to change indentation of b. 3113 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n" 3114 " b;\n" 3115 "return aaaaaaaaaaaaaaaaaaa +\n" 3116 " b; //", 3117 getLLVMStyleWithColumns(30)); 3118 } 3119 3120 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) { 3121 // Not sure what the best system is here. Like this, the LHS can be found 3122 // immediately above an operator (everything with the same or a higher 3123 // indent). The RHS is aligned right of the operator and so compasses 3124 // everything until something with the same indent as the operator is found. 3125 // FIXME: Is this a good system? 3126 FormatStyle Style = getLLVMStyle(); 3127 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 3128 verifyFormat( 3129 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3130 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3131 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3132 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3133 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3134 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3135 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3136 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3137 " > ccccccccccccccccccccccccccccccccccccccccc;", 3138 Style); 3139 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3140 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3141 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3142 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3143 Style); 3144 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3145 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3146 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3147 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3148 Style); 3149 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3150 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3151 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3152 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3153 Style); 3154 verifyFormat("if () {\n" 3155 "} else if (aaaaa\n" 3156 " && bbbbb // break\n" 3157 " > ccccc) {\n" 3158 "}", 3159 Style); 3160 verifyFormat("return (a)\n" 3161 " // comment\n" 3162 " + b;", 3163 Style); 3164 verifyFormat( 3165 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3166 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3167 " + cc;", 3168 Style); 3169 3170 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3171 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 3172 Style); 3173 3174 // Forced by comments. 3175 verifyFormat( 3176 "unsigned ContentSize =\n" 3177 " sizeof(int16_t) // DWARF ARange version number\n" 3178 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 3179 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 3180 " + sizeof(int8_t); // Segment Size (in bytes)"); 3181 3182 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 3183 " == boost::fusion::at_c<1>(iiii).second;", 3184 Style); 3185 3186 Style.ColumnLimit = 60; 3187 verifyFormat("zzzzzzzzzz\n" 3188 " = bbbbbbbbbbbbbbbbb\n" 3189 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 3190 Style); 3191 } 3192 3193 TEST_F(FormatTest, EnforcedOperatorWraps) { 3194 // Here we'd like to wrap after the || operators, but a comment is forcing an 3195 // earlier wrap. 3196 verifyFormat("bool x = aaaaa //\n" 3197 " || bbbbb\n" 3198 " //\n" 3199 " || cccc;"); 3200 } 3201 3202 TEST_F(FormatTest, NoOperandAlignment) { 3203 FormatStyle Style = getLLVMStyle(); 3204 Style.AlignOperands = false; 3205 verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n" 3206 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3207 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 3208 Style); 3209 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 3210 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3211 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3212 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3213 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3214 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3215 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3216 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3217 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3218 " > ccccccccccccccccccccccccccccccccccccccccc;", 3219 Style); 3220 3221 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3222 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3223 " + cc;", 3224 Style); 3225 verifyFormat("int a = aa\n" 3226 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3227 " * cccccccccccccccccccccccccccccccccccc;\n", 3228 Style); 3229 3230 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 3231 verifyFormat("return (a > b\n" 3232 " // comment1\n" 3233 " // comment2\n" 3234 " || c);", 3235 Style); 3236 } 3237 3238 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) { 3239 FormatStyle Style = getLLVMStyle(); 3240 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 3241 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 3242 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3243 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 3244 Style); 3245 } 3246 3247 TEST_F(FormatTest, AllowBinPackingInsideArguments) { 3248 FormatStyle Style = getLLVMStyle(); 3249 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 3250 Style.BinPackArguments = false; 3251 Style.ColumnLimit = 40; 3252 verifyFormat("void test() {\n" 3253 " someFunction(\n" 3254 " this + argument + is + quite\n" 3255 " + long + so + it + gets + wrapped\n" 3256 " + but + remains + bin - packed);\n" 3257 "}", 3258 Style); 3259 verifyFormat("void test() {\n" 3260 " someFunction(arg1,\n" 3261 " this + argument + is\n" 3262 " + quite + long + so\n" 3263 " + it + gets + wrapped\n" 3264 " + but + remains + bin\n" 3265 " - packed,\n" 3266 " arg3);\n" 3267 "}", 3268 Style); 3269 verifyFormat("void test() {\n" 3270 " someFunction(\n" 3271 " arg1,\n" 3272 " this + argument + has\n" 3273 " + anotherFunc(nested,\n" 3274 " calls + whose\n" 3275 " + arguments\n" 3276 " + are + also\n" 3277 " + wrapped,\n" 3278 " in + addition)\n" 3279 " + to + being + bin - packed,\n" 3280 " arg3);\n" 3281 "}", 3282 Style); 3283 3284 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 3285 verifyFormat("void test() {\n" 3286 " someFunction(\n" 3287 " arg1,\n" 3288 " this + argument + has +\n" 3289 " anotherFunc(nested,\n" 3290 " calls + whose +\n" 3291 " arguments +\n" 3292 " are + also +\n" 3293 " wrapped,\n" 3294 " in + addition) +\n" 3295 " to + being + bin - packed,\n" 3296 " arg3);\n" 3297 "}", 3298 Style); 3299 } 3300 3301 TEST_F(FormatTest, ConstructorInitializers) { 3302 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 3303 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", 3304 getLLVMStyleWithColumns(45)); 3305 verifyFormat("Constructor()\n" 3306 " : Inttializer(FitsOnTheLine) {}", 3307 getLLVMStyleWithColumns(44)); 3308 verifyFormat("Constructor()\n" 3309 " : Inttializer(FitsOnTheLine) {}", 3310 getLLVMStyleWithColumns(43)); 3311 3312 verifyFormat("template <typename T>\n" 3313 "Constructor() : Initializer(FitsOnTheLine) {}", 3314 getLLVMStyleWithColumns(45)); 3315 3316 verifyFormat( 3317 "SomeClass::Constructor()\n" 3318 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 3319 3320 verifyFormat( 3321 "SomeClass::Constructor()\n" 3322 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3323 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}"); 3324 verifyFormat( 3325 "SomeClass::Constructor()\n" 3326 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3327 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 3328 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3329 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3330 " : aaaaaaaaaa(aaaaaa) {}"); 3331 3332 verifyFormat("Constructor()\n" 3333 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3334 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3335 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3336 " aaaaaaaaaaaaaaaaaaaaaaa() {}"); 3337 3338 verifyFormat("Constructor()\n" 3339 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3340 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 3341 3342 verifyFormat("Constructor(int Parameter = 0)\n" 3343 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 3344 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}"); 3345 verifyFormat("Constructor()\n" 3346 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 3347 "}", 3348 getLLVMStyleWithColumns(60)); 3349 verifyFormat("Constructor()\n" 3350 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3351 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}"); 3352 3353 // Here a line could be saved by splitting the second initializer onto two 3354 // lines, but that is not desirable. 3355 verifyFormat("Constructor()\n" 3356 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 3357 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 3358 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 3359 3360 FormatStyle OnePerLine = getLLVMStyle(); 3361 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 3362 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false; 3363 verifyFormat("SomeClass::Constructor()\n" 3364 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3365 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3366 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3367 OnePerLine); 3368 verifyFormat("SomeClass::Constructor()\n" 3369 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 3370 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3371 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3372 OnePerLine); 3373 verifyFormat("MyClass::MyClass(int var)\n" 3374 " : some_var_(var), // 4 space indent\n" 3375 " some_other_var_(var + 1) { // lined up\n" 3376 "}", 3377 OnePerLine); 3378 verifyFormat("Constructor()\n" 3379 " : aaaaa(aaaaaa),\n" 3380 " aaaaa(aaaaaa),\n" 3381 " aaaaa(aaaaaa),\n" 3382 " aaaaa(aaaaaa),\n" 3383 " aaaaa(aaaaaa) {}", 3384 OnePerLine); 3385 verifyFormat("Constructor()\n" 3386 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 3387 " aaaaaaaaaaaaaaaaaaaaaa) {}", 3388 OnePerLine); 3389 OnePerLine.BinPackParameters = false; 3390 verifyFormat( 3391 "Constructor()\n" 3392 " : aaaaaaaaaaaaaaaaaaaaaaaa(\n" 3393 " aaaaaaaaaaa().aaa(),\n" 3394 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3395 OnePerLine); 3396 OnePerLine.ColumnLimit = 60; 3397 verifyFormat("Constructor()\n" 3398 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 3399 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 3400 OnePerLine); 3401 3402 EXPECT_EQ("Constructor()\n" 3403 " : // Comment forcing unwanted break.\n" 3404 " aaaa(aaaa) {}", 3405 format("Constructor() :\n" 3406 " // Comment forcing unwanted break.\n" 3407 " aaaa(aaaa) {}")); 3408 } 3409 3410 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) { 3411 FormatStyle Style = getLLVMStyle(); 3412 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 3413 3414 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 3415 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}", 3416 getStyleWithColumns(Style, 45)); 3417 verifyFormat("Constructor() :\n" 3418 " Initializer(FitsOnTheLine) {}", 3419 getStyleWithColumns(Style, 44)); 3420 verifyFormat("Constructor() :\n" 3421 " Initializer(FitsOnTheLine) {}", 3422 getStyleWithColumns(Style, 43)); 3423 3424 verifyFormat("template <typename T>\n" 3425 "Constructor() : Initializer(FitsOnTheLine) {}", 3426 getStyleWithColumns(Style, 50)); 3427 3428 verifyFormat( 3429 "SomeClass::Constructor() :\n" 3430 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 3431 Style); 3432 3433 verifyFormat( 3434 "SomeClass::Constructor() :\n" 3435 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3436 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3437 Style); 3438 verifyFormat( 3439 "SomeClass::Constructor() :\n" 3440 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3441 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 3442 Style); 3443 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3444 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 3445 " aaaaaaaaaa(aaaaaa) {}", 3446 Style); 3447 3448 verifyFormat("Constructor() :\n" 3449 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3450 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3451 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3452 " aaaaaaaaaaaaaaaaaaaaaaa() {}", 3453 Style); 3454 3455 verifyFormat("Constructor() :\n" 3456 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3457 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3458 Style); 3459 3460 verifyFormat("Constructor(int Parameter = 0) :\n" 3461 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 3462 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}", 3463 Style); 3464 verifyFormat("Constructor() :\n" 3465 " aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 3466 "}", 3467 getStyleWithColumns(Style, 60)); 3468 verifyFormat("Constructor() :\n" 3469 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3470 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}", 3471 Style); 3472 3473 // Here a line could be saved by splitting the second initializer onto two 3474 // lines, but that is not desirable. 3475 verifyFormat("Constructor() :\n" 3476 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 3477 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 3478 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3479 Style); 3480 3481 FormatStyle OnePerLine = Style; 3482 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 3483 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false; 3484 verifyFormat("SomeClass::Constructor() :\n" 3485 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3486 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3487 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3488 OnePerLine); 3489 verifyFormat("SomeClass::Constructor() :\n" 3490 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 3491 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3492 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3493 OnePerLine); 3494 verifyFormat("MyClass::MyClass(int var) :\n" 3495 " some_var_(var), // 4 space indent\n" 3496 " some_other_var_(var + 1) { // lined up\n" 3497 "}", 3498 OnePerLine); 3499 verifyFormat("Constructor() :\n" 3500 " aaaaa(aaaaaa),\n" 3501 " aaaaa(aaaaaa),\n" 3502 " aaaaa(aaaaaa),\n" 3503 " aaaaa(aaaaaa),\n" 3504 " aaaaa(aaaaaa) {}", 3505 OnePerLine); 3506 verifyFormat("Constructor() :\n" 3507 " aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 3508 " aaaaaaaaaaaaaaaaaaaaaa) {}", 3509 OnePerLine); 3510 OnePerLine.BinPackParameters = false; 3511 verifyFormat( 3512 "Constructor() :\n" 3513 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 3514 " aaaaaaaaaaa().aaa(),\n" 3515 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3516 OnePerLine); 3517 OnePerLine.ColumnLimit = 60; 3518 verifyFormat("Constructor() :\n" 3519 " aaaaaaaaaaaaaaaaaaaa(a),\n" 3520 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 3521 OnePerLine); 3522 3523 EXPECT_EQ("Constructor() :\n" 3524 " // Comment forcing unwanted break.\n" 3525 " aaaa(aaaa) {}", 3526 format("Constructor() :\n" 3527 " // Comment forcing unwanted break.\n" 3528 " aaaa(aaaa) {}", 3529 Style)); 3530 3531 Style.ColumnLimit = 0; 3532 verifyFormat("SomeClass::Constructor() :\n" 3533 " a(a) {}", 3534 Style); 3535 verifyFormat("SomeClass::Constructor() noexcept :\n" 3536 " a(a) {}", 3537 Style); 3538 verifyFormat("SomeClass::Constructor() :\n" 3539 " a(a), b(b), c(c) {}", 3540 Style); 3541 verifyFormat("SomeClass::Constructor() :\n" 3542 " a(a) {\n" 3543 " foo();\n" 3544 " bar();\n" 3545 "}", 3546 Style); 3547 3548 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 3549 verifyFormat("SomeClass::Constructor() :\n" 3550 " a(a), b(b), c(c) {\n" 3551 "}", 3552 Style); 3553 verifyFormat("SomeClass::Constructor() :\n" 3554 " a(a) {\n" 3555 "}", 3556 Style); 3557 3558 Style.ColumnLimit = 80; 3559 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 3560 Style.ConstructorInitializerIndentWidth = 2; 3561 verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", 3562 Style); 3563 verifyFormat("SomeClass::Constructor() :\n" 3564 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3565 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}", 3566 Style); 3567 } 3568 3569 #ifndef EXPENSIVE_CHECKS 3570 // Expensive checks enables libstdc++ checking which includes validating the 3571 // state of ranges used in std::priority_queue - this blows out the 3572 // runtime/scalability of the function and makes this test unacceptably slow. 3573 TEST_F(FormatTest, MemoizationTests) { 3574 // This breaks if the memoization lookup does not take \c Indent and 3575 // \c LastSpace into account. 3576 verifyFormat( 3577 "extern CFRunLoopTimerRef\n" 3578 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n" 3579 " CFTimeInterval interval, CFOptionFlags flags,\n" 3580 " CFIndex order, CFRunLoopTimerCallBack callout,\n" 3581 " CFRunLoopTimerContext *context) {}"); 3582 3583 // Deep nesting somewhat works around our memoization. 3584 verifyFormat( 3585 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3586 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3587 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3588 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3589 " aaaaa())))))))))))))))))))))))))))))))))))))));", 3590 getLLVMStyleWithColumns(65)); 3591 verifyFormat( 3592 "aaaaa(\n" 3593 " aaaaa,\n" 3594 " aaaaa(\n" 3595 " aaaaa,\n" 3596 " aaaaa(\n" 3597 " aaaaa,\n" 3598 " aaaaa(\n" 3599 " aaaaa,\n" 3600 " aaaaa(\n" 3601 " aaaaa,\n" 3602 " aaaaa(\n" 3603 " aaaaa,\n" 3604 " aaaaa(\n" 3605 " aaaaa,\n" 3606 " aaaaa(\n" 3607 " aaaaa,\n" 3608 " aaaaa(\n" 3609 " aaaaa,\n" 3610 " aaaaa(\n" 3611 " aaaaa,\n" 3612 " aaaaa(\n" 3613 " aaaaa,\n" 3614 " aaaaa(\n" 3615 " aaaaa,\n" 3616 " aaaaa))))))))))));", 3617 getLLVMStyleWithColumns(65)); 3618 verifyFormat( 3619 "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" 3620 " a),\n" 3621 " a),\n" 3622 " a),\n" 3623 " a),\n" 3624 " a),\n" 3625 " a),\n" 3626 " a),\n" 3627 " a),\n" 3628 " a),\n" 3629 " a),\n" 3630 " a),\n" 3631 " a),\n" 3632 " a),\n" 3633 " a),\n" 3634 " a),\n" 3635 " a),\n" 3636 " a)", 3637 getLLVMStyleWithColumns(65)); 3638 3639 // This test takes VERY long when memoization is broken. 3640 FormatStyle OnePerLine = getLLVMStyle(); 3641 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 3642 OnePerLine.BinPackParameters = false; 3643 std::string input = "Constructor()\n" 3644 " : aaaa(a,\n"; 3645 for (unsigned i = 0, e = 80; i != e; ++i) { 3646 input += " a,\n"; 3647 } 3648 input += " a) {}"; 3649 verifyFormat(input, OnePerLine); 3650 } 3651 #endif 3652 3653 TEST_F(FormatTest, BreaksAsHighAsPossible) { 3654 verifyFormat( 3655 "void f() {\n" 3656 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" 3657 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" 3658 " f();\n" 3659 "}"); 3660 verifyFormat("if (Intervals[i].getRange().getFirst() <\n" 3661 " Intervals[i - 1].getRange().getLast()) {\n}"); 3662 } 3663 3664 TEST_F(FormatTest, BreaksFunctionDeclarations) { 3665 // Principially, we break function declarations in a certain order: 3666 // 1) break amongst arguments. 3667 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n" 3668 " Cccccccccccccc cccccccccccccc);"); 3669 verifyFormat("template <class TemplateIt>\n" 3670 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n" 3671 " TemplateIt *stop) {}"); 3672 3673 // 2) break after return type. 3674 verifyFormat( 3675 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3676 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);", 3677 getGoogleStyle()); 3678 3679 // 3) break after (. 3680 verifyFormat( 3681 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n" 3682 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);", 3683 getGoogleStyle()); 3684 3685 // 4) break before after nested name specifiers. 3686 verifyFormat( 3687 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3688 "SomeClasssssssssssssssssssssssssssssssssssssss::\n" 3689 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);", 3690 getGoogleStyle()); 3691 3692 // However, there are exceptions, if a sufficient amount of lines can be 3693 // saved. 3694 // FIXME: The precise cut-offs wrt. the number of saved lines might need some 3695 // more adjusting. 3696 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 3697 " Cccccccccccccc cccccccccc,\n" 3698 " Cccccccccccccc cccccccccc,\n" 3699 " Cccccccccccccc cccccccccc,\n" 3700 " Cccccccccccccc cccccccccc);"); 3701 verifyFormat( 3702 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3703 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3704 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3705 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);", 3706 getGoogleStyle()); 3707 verifyFormat( 3708 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 3709 " Cccccccccccccc cccccccccc,\n" 3710 " Cccccccccccccc cccccccccc,\n" 3711 " Cccccccccccccc cccccccccc,\n" 3712 " Cccccccccccccc cccccccccc,\n" 3713 " Cccccccccccccc cccccccccc,\n" 3714 " Cccccccccccccc cccccccccc);"); 3715 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 3716 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3717 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3718 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3719 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); 3720 3721 // Break after multi-line parameters. 3722 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3723 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3724 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3725 " bbbb bbbb);"); 3726 verifyFormat("void SomeLoooooooooooongFunction(\n" 3727 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 3728 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3729 " int bbbbbbbbbbbbb);"); 3730 3731 // Treat overloaded operators like other functions. 3732 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3733 "operator>(const SomeLoooooooooooooooooooooooooogType &other);"); 3734 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3735 "operator>>(const SomeLooooooooooooooooooooooooogType &other);"); 3736 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3737 "operator<<(const SomeLooooooooooooooooooooooooogType &other);"); 3738 verifyGoogleFormat( 3739 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n" 3740 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 3741 verifyGoogleFormat( 3742 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n" 3743 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 3744 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3745 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 3746 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n" 3747 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 3748 verifyGoogleFormat( 3749 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n" 3750 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3751 " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}"); 3752 verifyGoogleFormat( 3753 "template <typename T>\n" 3754 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3755 "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n" 3756 " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);"); 3757 3758 FormatStyle Style = getLLVMStyle(); 3759 Style.PointerAlignment = FormatStyle::PAS_Left; 3760 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3761 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}", 3762 Style); 3763 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n" 3764 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3765 Style); 3766 } 3767 3768 TEST_F(FormatTest, TrailingReturnType) { 3769 verifyFormat("auto foo() -> int;\n"); 3770 verifyFormat("struct S {\n" 3771 " auto bar() const -> int;\n" 3772 "};"); 3773 verifyFormat("template <size_t Order, typename T>\n" 3774 "auto load_img(const std::string &filename)\n" 3775 " -> alias::tensor<Order, T, mem::tag::cpu> {}"); 3776 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n" 3777 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}"); 3778 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}"); 3779 verifyFormat("template <typename T>\n" 3780 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n" 3781 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());"); 3782 3783 // Not trailing return types. 3784 verifyFormat("void f() { auto a = b->c(); }"); 3785 } 3786 3787 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { 3788 // Avoid breaking before trailing 'const' or other trailing annotations, if 3789 // they are not function-like. 3790 FormatStyle Style = getGoogleStyle(); 3791 Style.ColumnLimit = 47; 3792 verifyFormat("void someLongFunction(\n" 3793 " int someLoooooooooooooongParameter) const {\n}", 3794 getLLVMStyleWithColumns(47)); 3795 verifyFormat("LoooooongReturnType\n" 3796 "someLoooooooongFunction() const {}", 3797 getLLVMStyleWithColumns(47)); 3798 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n" 3799 " const {}", 3800 Style); 3801 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3802 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;"); 3803 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3804 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;"); 3805 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3806 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;"); 3807 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n" 3808 " aaaaaaaaaaa aaaaa) const override;"); 3809 verifyGoogleFormat( 3810 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 3811 " const override;"); 3812 3813 // Even if the first parameter has to be wrapped. 3814 verifyFormat("void someLongFunction(\n" 3815 " int someLongParameter) const {}", 3816 getLLVMStyleWithColumns(46)); 3817 verifyFormat("void someLongFunction(\n" 3818 " int someLongParameter) const {}", 3819 Style); 3820 verifyFormat("void someLongFunction(\n" 3821 " int someLongParameter) override {}", 3822 Style); 3823 verifyFormat("void someLongFunction(\n" 3824 " int someLongParameter) OVERRIDE {}", 3825 Style); 3826 verifyFormat("void someLongFunction(\n" 3827 " int someLongParameter) final {}", 3828 Style); 3829 verifyFormat("void someLongFunction(\n" 3830 " int someLongParameter) FINAL {}", 3831 Style); 3832 verifyFormat("void someLongFunction(\n" 3833 " int parameter) const override {}", 3834 Style); 3835 3836 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 3837 verifyFormat("void someLongFunction(\n" 3838 " int someLongParameter) const\n" 3839 "{\n" 3840 "}", 3841 Style); 3842 3843 // Unless these are unknown annotations. 3844 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n" 3845 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3846 " LONG_AND_UGLY_ANNOTATION;"); 3847 3848 // Breaking before function-like trailing annotations is fine to keep them 3849 // close to their arguments. 3850 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3851 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 3852 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 3853 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 3854 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 3855 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}"); 3856 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n" 3857 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);"); 3858 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});"); 3859 3860 verifyFormat( 3861 "void aaaaaaaaaaaaaaaaaa()\n" 3862 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n" 3863 " aaaaaaaaaaaaaaaaaaaaaaaaa));"); 3864 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3865 " __attribute__((unused));"); 3866 verifyGoogleFormat( 3867 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3868 " GUARDED_BY(aaaaaaaaaaaa);"); 3869 verifyGoogleFormat( 3870 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3871 " GUARDED_BY(aaaaaaaaaaaa);"); 3872 verifyGoogleFormat( 3873 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 3874 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 3875 verifyGoogleFormat( 3876 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 3877 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 3878 } 3879 3880 TEST_F(FormatTest, FunctionAnnotations) { 3881 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 3882 "int OldFunction(const string ¶meter) {}"); 3883 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 3884 "string OldFunction(const string ¶meter) {}"); 3885 verifyFormat("template <typename T>\n" 3886 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 3887 "string OldFunction(const string ¶meter) {}"); 3888 3889 // Not function annotations. 3890 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3891 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); 3892 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n" 3893 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}"); 3894 verifyFormat("MACRO(abc).function() // wrap\n" 3895 " << abc;"); 3896 verifyFormat("MACRO(abc)->function() // wrap\n" 3897 " << abc;"); 3898 verifyFormat("MACRO(abc)::function() // wrap\n" 3899 " << abc;"); 3900 } 3901 3902 TEST_F(FormatTest, BreaksDesireably) { 3903 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 3904 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 3905 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); 3906 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3907 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 3908 "}"); 3909 3910 verifyFormat( 3911 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3912 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 3913 3914 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3915 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3916 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 3917 3918 verifyFormat( 3919 "aaaaaaaa(aaaaaaaaaaaaa,\n" 3920 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3921 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 3922 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3923 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 3924 3925 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3926 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3927 3928 verifyFormat( 3929 "void f() {\n" 3930 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 3931 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 3932 "}"); 3933 verifyFormat( 3934 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3935 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 3936 verifyFormat( 3937 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3938 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 3939 verifyFormat( 3940 "aaaaaa(aaa,\n" 3941 " new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3942 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3943 " aaaa);"); 3944 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3945 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3946 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3947 3948 // Indent consistently independent of call expression and unary operator. 3949 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 3950 " dddddddddddddddddddddddddddddd));"); 3951 verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 3952 " dddddddddddddddddddddddddddddd));"); 3953 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n" 3954 " dddddddddddddddddddddddddddddd));"); 3955 3956 // This test case breaks on an incorrect memoization, i.e. an optimization not 3957 // taking into account the StopAt value. 3958 verifyFormat( 3959 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 3960 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 3961 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 3962 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3963 3964 verifyFormat("{\n {\n {\n" 3965 " Annotation.SpaceRequiredBefore =\n" 3966 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" 3967 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" 3968 " }\n }\n}"); 3969 3970 // Break on an outer level if there was a break on an inner level. 3971 EXPECT_EQ("f(g(h(a, // comment\n" 3972 " b, c),\n" 3973 " d, e),\n" 3974 " x, y);", 3975 format("f(g(h(a, // comment\n" 3976 " b, c), d, e), x, y);")); 3977 3978 // Prefer breaking similar line breaks. 3979 verifyFormat( 3980 "const int kTrackingOptions = NSTrackingMouseMoved |\n" 3981 " NSTrackingMouseEnteredAndExited |\n" 3982 " NSTrackingActiveAlways;"); 3983 } 3984 3985 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) { 3986 FormatStyle NoBinPacking = getGoogleStyle(); 3987 NoBinPacking.BinPackParameters = false; 3988 NoBinPacking.BinPackArguments = true; 3989 verifyFormat("void f() {\n" 3990 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n" 3991 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 3992 "}", 3993 NoBinPacking); 3994 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n" 3995 " int aaaaaaaaaaaaaaaaaaaa,\n" 3996 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3997 NoBinPacking); 3998 3999 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 4000 verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4001 " vector<int> bbbbbbbbbbbbbbb);", 4002 NoBinPacking); 4003 // FIXME: This behavior difference is probably not wanted. However, currently 4004 // we cannot distinguish BreakBeforeParameter being set because of the wrapped 4005 // template arguments from BreakBeforeParameter being set because of the 4006 // one-per-line formatting. 4007 verifyFormat( 4008 "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n" 4009 " aaaaaaaaaa> aaaaaaaaaa);", 4010 NoBinPacking); 4011 verifyFormat( 4012 "void fffffffffff(\n" 4013 " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n" 4014 " aaaaaaaaaa);"); 4015 } 4016 4017 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { 4018 FormatStyle NoBinPacking = getGoogleStyle(); 4019 NoBinPacking.BinPackParameters = false; 4020 NoBinPacking.BinPackArguments = false; 4021 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" 4022 " aaaaaaaaaaaaaaaaaaaa,\n" 4023 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);", 4024 NoBinPacking); 4025 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n" 4026 " aaaaaaaaaaaaa,\n" 4027 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));", 4028 NoBinPacking); 4029 verifyFormat( 4030 "aaaaaaaa(aaaaaaaaaaaaa,\n" 4031 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4032 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 4033 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4034 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));", 4035 NoBinPacking); 4036 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 4037 " .aaaaaaaaaaaaaaaaaa();", 4038 NoBinPacking); 4039 verifyFormat("void f() {\n" 4040 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4041 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n" 4042 "}", 4043 NoBinPacking); 4044 4045 verifyFormat( 4046 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4047 " aaaaaaaaaaaa,\n" 4048 " aaaaaaaaaaaa);", 4049 NoBinPacking); 4050 verifyFormat( 4051 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" 4052 " ddddddddddddddddddddddddddddd),\n" 4053 " test);", 4054 NoBinPacking); 4055 4056 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" 4057 " aaaaaaaaaaaaaaaaaaaaaaa,\n" 4058 " aaaaaaaaaaaaaaaaaaaaaaa>\n" 4059 " aaaaaaaaaaaaaaaaaa;", 4060 NoBinPacking); 4061 verifyFormat("a(\"a\"\n" 4062 " \"a\",\n" 4063 " a);"); 4064 4065 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 4066 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" 4067 " aaaaaaaaa,\n" 4068 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4069 NoBinPacking); 4070 verifyFormat( 4071 "void f() {\n" 4072 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 4073 " .aaaaaaa();\n" 4074 "}", 4075 NoBinPacking); 4076 verifyFormat( 4077 "template <class SomeType, class SomeOtherType>\n" 4078 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}", 4079 NoBinPacking); 4080 } 4081 4082 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) { 4083 FormatStyle Style = getLLVMStyleWithColumns(15); 4084 Style.ExperimentalAutoDetectBinPacking = true; 4085 EXPECT_EQ("aaa(aaaa,\n" 4086 " aaaa,\n" 4087 " aaaa);\n" 4088 "aaa(aaaa,\n" 4089 " aaaa,\n" 4090 " aaaa);", 4091 format("aaa(aaaa,\n" // one-per-line 4092 " aaaa,\n" 4093 " aaaa );\n" 4094 "aaa(aaaa, aaaa, aaaa);", // inconclusive 4095 Style)); 4096 EXPECT_EQ("aaa(aaaa, aaaa,\n" 4097 " aaaa);\n" 4098 "aaa(aaaa, aaaa,\n" 4099 " aaaa);", 4100 format("aaa(aaaa, aaaa,\n" // bin-packed 4101 " aaaa );\n" 4102 "aaa(aaaa, aaaa, aaaa);", // inconclusive 4103 Style)); 4104 } 4105 4106 TEST_F(FormatTest, FormatsBuilderPattern) { 4107 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n" 4108 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" 4109 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n" 4110 " .StartsWith(\".init\", ORDER_INIT)\n" 4111 " .StartsWith(\".fini\", ORDER_FINI)\n" 4112 " .StartsWith(\".hash\", ORDER_HASH)\n" 4113 " .Default(ORDER_TEXT);\n"); 4114 4115 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" 4116 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); 4117 verifyFormat( 4118 "aaaaaaa->aaaaaaa\n" 4119 " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4120 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4121 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 4122 verifyFormat( 4123 "aaaaaaa->aaaaaaa\n" 4124 " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4125 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 4126 verifyFormat( 4127 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" 4128 " aaaaaaaaaaaaaa);"); 4129 verifyFormat( 4130 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n" 4131 " aaaaaa->aaaaaaaaaaaa()\n" 4132 " ->aaaaaaaaaaaaaaaa(\n" 4133 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4134 " ->aaaaaaaaaaaaaaaaa();"); 4135 verifyGoogleFormat( 4136 "void f() {\n" 4137 " someo->Add((new util::filetools::Handler(dir))\n" 4138 " ->OnEvent1(NewPermanentCallback(\n" 4139 " this, &HandlerHolderClass::EventHandlerCBA))\n" 4140 " ->OnEvent2(NewPermanentCallback(\n" 4141 " this, &HandlerHolderClass::EventHandlerCBB))\n" 4142 " ->OnEvent3(NewPermanentCallback(\n" 4143 " this, &HandlerHolderClass::EventHandlerCBC))\n" 4144 " ->OnEvent5(NewPermanentCallback(\n" 4145 " this, &HandlerHolderClass::EventHandlerCBD))\n" 4146 " ->OnEvent6(NewPermanentCallback(\n" 4147 " this, &HandlerHolderClass::EventHandlerCBE)));\n" 4148 "}"); 4149 4150 verifyFormat( 4151 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();"); 4152 verifyFormat("aaaaaaaaaaaaaaa()\n" 4153 " .aaaaaaaaaaaaaaa()\n" 4154 " .aaaaaaaaaaaaaaa()\n" 4155 " .aaaaaaaaaaaaaaa()\n" 4156 " .aaaaaaaaaaaaaaa();"); 4157 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4158 " .aaaaaaaaaaaaaaa()\n" 4159 " .aaaaaaaaaaaaaaa()\n" 4160 " .aaaaaaaaaaaaaaa();"); 4161 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4162 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4163 " .aaaaaaaaaaaaaaa();"); 4164 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n" 4165 " ->aaaaaaaaaaaaaae(0)\n" 4166 " ->aaaaaaaaaaaaaaa();"); 4167 4168 // Don't linewrap after very short segments. 4169 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4170 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4171 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4172 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4173 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4174 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4175 verifyFormat("aaa()\n" 4176 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4177 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4178 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4179 4180 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 4181 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4182 " .has<bbbbbbbbbbbbbbbbbbbbb>();"); 4183 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 4184 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 4185 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();"); 4186 4187 // Prefer not to break after empty parentheses. 4188 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n" 4189 " First->LastNewlineOffset);"); 4190 4191 // Prefer not to create "hanging" indents. 4192 verifyFormat( 4193 "return !soooooooooooooome_map\n" 4194 " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4195 " .second;"); 4196 verifyFormat( 4197 "return aaaaaaaaaaaaaaaa\n" 4198 " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n" 4199 " .aaaa(aaaaaaaaaaaaaa);"); 4200 // No hanging indent here. 4201 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n" 4202 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4203 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n" 4204 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4205 verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 4206 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4207 getLLVMStyleWithColumns(60)); 4208 verifyFormat("aaaaaaaaaaaaaaaaaa\n" 4209 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 4210 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4211 getLLVMStyleWithColumns(59)); 4212 verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4213 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4214 " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4215 } 4216 4217 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { 4218 verifyFormat( 4219 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4220 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); 4221 verifyFormat( 4222 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n" 4223 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}"); 4224 4225 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 4226 " ccccccccccccccccccccccccc) {\n}"); 4227 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n" 4228 " ccccccccccccccccccccccccc) {\n}"); 4229 4230 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 4231 " ccccccccccccccccccccccccc) {\n}"); 4232 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n" 4233 " ccccccccccccccccccccccccc) {\n}"); 4234 4235 verifyFormat( 4236 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" 4237 " ccccccccccccccccccccccccc) {\n}"); 4238 verifyFormat( 4239 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n" 4240 " ccccccccccccccccccccccccc) {\n}"); 4241 4242 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" 4243 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" 4244 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" 4245 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 4246 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n" 4247 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n" 4248 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n" 4249 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 4250 4251 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" 4252 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" 4253 " aaaaaaaaaaaaaaa != aa) {\n}"); 4254 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n" 4255 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n" 4256 " aaaaaaaaaaaaaaa != aa) {\n}"); 4257 } 4258 4259 TEST_F(FormatTest, BreaksAfterAssignments) { 4260 verifyFormat( 4261 "unsigned Cost =\n" 4262 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" 4263 " SI->getPointerAddressSpaceee());\n"); 4264 verifyFormat( 4265 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" 4266 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); 4267 4268 verifyFormat( 4269 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n" 4270 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); 4271 verifyFormat("unsigned OriginalStartColumn =\n" 4272 " SourceMgr.getSpellingColumnNumber(\n" 4273 " Current.FormatTok.getStartOfNonWhitespace()) -\n" 4274 " 1;"); 4275 } 4276 4277 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) { 4278 FormatStyle Style = getLLVMStyle(); 4279 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 4280 " bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;", 4281 Style); 4282 4283 Style.PenaltyBreakAssignment = 20; 4284 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 4285 " cccccccccccccccccccccccccc;", 4286 Style); 4287 } 4288 4289 TEST_F(FormatTest, AlignsAfterAssignments) { 4290 verifyFormat( 4291 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4292 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4293 verifyFormat( 4294 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4295 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4296 verifyFormat( 4297 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4298 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4299 verifyFormat( 4300 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4301 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 4302 verifyFormat( 4303 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" 4304 " aaaaaaaaaaaaaaaaaaaaaaaa +\n" 4305 " aaaaaaaaaaaaaaaaaaaaaaaa;"); 4306 } 4307 4308 TEST_F(FormatTest, AlignsAfterReturn) { 4309 verifyFormat( 4310 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4311 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4312 verifyFormat( 4313 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4314 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 4315 verifyFormat( 4316 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 4317 " aaaaaaaaaaaaaaaaaaaaaa();"); 4318 verifyFormat( 4319 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 4320 " aaaaaaaaaaaaaaaaaaaaaa());"); 4321 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4322 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4323 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4324 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n" 4325 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4326 verifyFormat("return\n" 4327 " // true if code is one of a or b.\n" 4328 " code == a || code == b;"); 4329 } 4330 4331 TEST_F(FormatTest, AlignsAfterOpenBracket) { 4332 verifyFormat( 4333 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 4334 " aaaaaaaaa aaaaaaa) {}"); 4335 verifyFormat( 4336 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 4337 " aaaaaaaaaaa aaaaaaaaa);"); 4338 verifyFormat( 4339 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 4340 " aaaaaaaaaaaaaaaaaaaaa));"); 4341 FormatStyle Style = getLLVMStyle(); 4342 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4343 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4344 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}", 4345 Style); 4346 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 4347 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);", 4348 Style); 4349 verifyFormat("SomeLongVariableName->someFunction(\n" 4350 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));", 4351 Style); 4352 verifyFormat( 4353 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 4354 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4355 Style); 4356 verifyFormat( 4357 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 4358 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4359 Style); 4360 verifyFormat( 4361 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 4362 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 4363 Style); 4364 4365 verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n" 4366 " ccccccc(aaaaaaaaaaaaaaaaa, //\n" 4367 " b));", 4368 Style); 4369 4370 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 4371 Style.BinPackArguments = false; 4372 Style.BinPackParameters = false; 4373 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4374 " aaaaaaaaaaa aaaaaaaa,\n" 4375 " aaaaaaaaa aaaaaaa,\n" 4376 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4377 Style); 4378 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 4379 " aaaaaaaaaaa aaaaaaaaa,\n" 4380 " aaaaaaaaaaa aaaaaaaaa,\n" 4381 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4382 Style); 4383 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n" 4384 " aaaaaaaaaaaaaaa,\n" 4385 " aaaaaaaaaaaaaaaaaaaaa,\n" 4386 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 4387 Style); 4388 verifyFormat( 4389 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n" 4390 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 4391 Style); 4392 verifyFormat( 4393 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n" 4394 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 4395 Style); 4396 verifyFormat( 4397 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 4398 " aaaaaaaaaaaaaaaaaaaaa(\n" 4399 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n" 4400 " aaaaaaaaaaaaaaaa);", 4401 Style); 4402 verifyFormat( 4403 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 4404 " aaaaaaaaaaaaaaaaaaaaa(\n" 4405 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n" 4406 " aaaaaaaaaaaaaaaa);", 4407 Style); 4408 } 4409 4410 TEST_F(FormatTest, ParenthesesAndOperandAlignment) { 4411 FormatStyle Style = getLLVMStyleWithColumns(40); 4412 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4413 " bbbbbbbbbbbbbbbbbbbbbb);", 4414 Style); 4415 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 4416 Style.AlignOperands = false; 4417 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4418 " bbbbbbbbbbbbbbbbbbbbbb);", 4419 Style); 4420 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4421 Style.AlignOperands = true; 4422 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4423 " bbbbbbbbbbbbbbbbbbbbbb);", 4424 Style); 4425 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4426 Style.AlignOperands = false; 4427 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4428 " bbbbbbbbbbbbbbbbbbbbbb);", 4429 Style); 4430 } 4431 4432 TEST_F(FormatTest, BreaksConditionalExpressions) { 4433 verifyFormat( 4434 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4435 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4436 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4437 verifyFormat( 4438 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 4439 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4440 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4441 verifyFormat( 4442 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4443 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4444 verifyFormat( 4445 "aaaa(aaaaaaaaa, aaaaaaaaa,\n" 4446 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4447 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4448 verifyFormat( 4449 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" 4450 " : aaaaaaaaaaaaa);"); 4451 verifyFormat( 4452 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4453 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4454 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4455 " aaaaaaaaaaaaa);"); 4456 verifyFormat( 4457 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4458 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4459 " aaaaaaaaaaaaa);"); 4460 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4461 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4462 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4463 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4464 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4465 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4466 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4467 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4468 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4469 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4470 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4471 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4472 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4473 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4474 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4475 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4476 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4477 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4478 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4479 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4480 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 4481 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4482 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4483 " : aaaaaaaaaaaaaaaa;"); 4484 verifyFormat( 4485 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4486 " ? aaaaaaaaaaaaaaa\n" 4487 " : aaaaaaaaaaaaaaa;"); 4488 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 4489 " aaaaaaaaa\n" 4490 " ? b\n" 4491 " : c);"); 4492 verifyFormat("return aaaa == bbbb\n" 4493 " // comment\n" 4494 " ? aaaa\n" 4495 " : bbbb;"); 4496 verifyFormat("unsigned Indent =\n" 4497 " format(TheLine.First,\n" 4498 " IndentForLevel[TheLine.Level] >= 0\n" 4499 " ? IndentForLevel[TheLine.Level]\n" 4500 " : TheLine * 2,\n" 4501 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 4502 getLLVMStyleWithColumns(60)); 4503 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 4504 " ? aaaaaaaaaaaaaaa\n" 4505 " : bbbbbbbbbbbbbbb //\n" 4506 " ? ccccccccccccccc\n" 4507 " : ddddddddddddddd;"); 4508 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 4509 " ? aaaaaaaaaaaaaaa\n" 4510 " : (bbbbbbbbbbbbbbb //\n" 4511 " ? ccccccccccccccc\n" 4512 " : ddddddddddddddd);"); 4513 verifyFormat( 4514 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4515 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4516 " aaaaaaaaaaaaaaaaaaaaa +\n" 4517 " aaaaaaaaaaaaaaaaaaaaa\n" 4518 " : aaaaaaaaaa;"); 4519 verifyFormat( 4520 "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4521 " : aaaaaaaaaaaaaaaaaaaaaa\n" 4522 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4523 4524 FormatStyle NoBinPacking = getLLVMStyle(); 4525 NoBinPacking.BinPackArguments = false; 4526 verifyFormat( 4527 "void f() {\n" 4528 " g(aaa,\n" 4529 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 4530 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4531 " ? aaaaaaaaaaaaaaa\n" 4532 " : aaaaaaaaaaaaaaa);\n" 4533 "}", 4534 NoBinPacking); 4535 verifyFormat( 4536 "void f() {\n" 4537 " g(aaa,\n" 4538 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 4539 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4540 " ?: aaaaaaaaaaaaaaa);\n" 4541 "}", 4542 NoBinPacking); 4543 4544 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n" 4545 " // comment.\n" 4546 " ccccccccccccccccccccccccccccccccccccccc\n" 4547 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4548 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);"); 4549 4550 // Assignments in conditional expressions. Apparently not uncommon :-(. 4551 verifyFormat("return a != b\n" 4552 " // comment\n" 4553 " ? a = b\n" 4554 " : a = b;"); 4555 verifyFormat("return a != b\n" 4556 " // comment\n" 4557 " ? a = a != b\n" 4558 " // comment\n" 4559 " ? a = b\n" 4560 " : a\n" 4561 " : a;\n"); 4562 verifyFormat("return a != b\n" 4563 " // comment\n" 4564 " ? a\n" 4565 " : a = a != b\n" 4566 " // comment\n" 4567 " ? a = b\n" 4568 " : a;"); 4569 } 4570 4571 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) { 4572 FormatStyle Style = getLLVMStyle(); 4573 Style.BreakBeforeTernaryOperators = false; 4574 Style.ColumnLimit = 70; 4575 verifyFormat( 4576 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4577 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4578 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4579 Style); 4580 verifyFormat( 4581 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 4582 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4583 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4584 Style); 4585 verifyFormat( 4586 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4587 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4588 Style); 4589 verifyFormat( 4590 "aaaa(aaaaaaaa, aaaaaaaaaa,\n" 4591 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4592 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4593 Style); 4594 verifyFormat( 4595 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n" 4596 " aaaaaaaaaaaaa);", 4597 Style); 4598 verifyFormat( 4599 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4600 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4601 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4602 " aaaaaaaaaaaaa);", 4603 Style); 4604 verifyFormat( 4605 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4606 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4607 " aaaaaaaaaaaaa);", 4608 Style); 4609 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4610 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4611 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 4612 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4613 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4614 Style); 4615 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4616 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4617 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4618 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 4619 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4620 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4621 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4622 Style); 4623 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4624 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n" 4625 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4626 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4627 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4628 Style); 4629 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4630 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4631 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4632 Style); 4633 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 4634 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4635 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4636 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4637 Style); 4638 verifyFormat( 4639 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4640 " aaaaaaaaaaaaaaa :\n" 4641 " aaaaaaaaaaaaaaa;", 4642 Style); 4643 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 4644 " aaaaaaaaa ?\n" 4645 " b :\n" 4646 " c);", 4647 Style); 4648 verifyFormat("unsigned Indent =\n" 4649 " format(TheLine.First,\n" 4650 " IndentForLevel[TheLine.Level] >= 0 ?\n" 4651 " IndentForLevel[TheLine.Level] :\n" 4652 " TheLine * 2,\n" 4653 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 4654 Style); 4655 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 4656 " aaaaaaaaaaaaaaa :\n" 4657 " bbbbbbbbbbbbbbb ? //\n" 4658 " ccccccccccccccc :\n" 4659 " ddddddddddddddd;", 4660 Style); 4661 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 4662 " aaaaaaaaaaaaaaa :\n" 4663 " (bbbbbbbbbbbbbbb ? //\n" 4664 " ccccccccccccccc :\n" 4665 " ddddddddddddddd);", 4666 Style); 4667 verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4668 " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n" 4669 " ccccccccccccccccccccccccccc;", 4670 Style); 4671 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4672 " aaaaa :\n" 4673 " bbbbbbbbbbbbbbb + cccccccccccccccc;", 4674 Style); 4675 } 4676 4677 TEST_F(FormatTest, DeclarationsOfMultipleVariables) { 4678 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" 4679 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); 4680 verifyFormat("bool a = true, b = false;"); 4681 4682 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" 4683 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" 4684 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" 4685 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); 4686 verifyFormat( 4687 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 4688 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n" 4689 " d = e && f;"); 4690 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n" 4691 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;"); 4692 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 4693 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;"); 4694 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n" 4695 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;"); 4696 4697 FormatStyle Style = getGoogleStyle(); 4698 Style.PointerAlignment = FormatStyle::PAS_Left; 4699 Style.DerivePointerAlignment = false; 4700 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4701 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" 4702 " *b = bbbbbbbbbbbbbbbbbbb;", 4703 Style); 4704 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 4705 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;", 4706 Style); 4707 verifyFormat("vector<int*> a, b;", Style); 4708 verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style); 4709 } 4710 4711 TEST_F(FormatTest, ConditionalExpressionsInBrackets) { 4712 verifyFormat("arr[foo ? bar : baz];"); 4713 verifyFormat("f()[foo ? bar : baz];"); 4714 verifyFormat("(a + b)[foo ? bar : baz];"); 4715 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); 4716 } 4717 4718 TEST_F(FormatTest, AlignsStringLiterals) { 4719 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" 4720 " \"short literal\");"); 4721 verifyFormat( 4722 "looooooooooooooooooooooooongFunction(\n" 4723 " \"short literal\"\n" 4724 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); 4725 verifyFormat("someFunction(\"Always break between multi-line\"\n" 4726 " \" string literals\",\n" 4727 " and, other, parameters);"); 4728 EXPECT_EQ("fun + \"1243\" /* comment */\n" 4729 " \"5678\";", 4730 format("fun + \"1243\" /* comment */\n" 4731 " \"5678\";", 4732 getLLVMStyleWithColumns(28))); 4733 EXPECT_EQ( 4734 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 4735 " \"aaaaaaaaaaaaaaaaaaaaa\"\n" 4736 " \"aaaaaaaaaaaaaaaa\";", 4737 format("aaaaaa =" 4738 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " 4739 "aaaaaaaaaaaaaaaaaaaaa\" " 4740 "\"aaaaaaaaaaaaaaaa\";")); 4741 verifyFormat("a = a + \"a\"\n" 4742 " \"a\"\n" 4743 " \"a\";"); 4744 verifyFormat("f(\"a\", \"b\"\n" 4745 " \"c\");"); 4746 4747 verifyFormat( 4748 "#define LL_FORMAT \"ll\"\n" 4749 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n" 4750 " \"d, ddddddddd: %\" LL_FORMAT \"d\");"); 4751 4752 verifyFormat("#define A(X) \\\n" 4753 " \"aaaaa\" #X \"bbbbbb\" \\\n" 4754 " \"ccccc\"", 4755 getLLVMStyleWithColumns(23)); 4756 verifyFormat("#define A \"def\"\n" 4757 "f(\"abc\" A \"ghi\"\n" 4758 " \"jkl\");"); 4759 4760 verifyFormat("f(L\"a\"\n" 4761 " L\"b\");"); 4762 verifyFormat("#define A(X) \\\n" 4763 " L\"aaaaa\" #X L\"bbbbbb\" \\\n" 4764 " L\"ccccc\"", 4765 getLLVMStyleWithColumns(25)); 4766 4767 verifyFormat("f(@\"a\"\n" 4768 " @\"b\");"); 4769 verifyFormat("NSString s = @\"a\"\n" 4770 " @\"b\"\n" 4771 " @\"c\";"); 4772 verifyFormat("NSString s = @\"a\"\n" 4773 " \"b\"\n" 4774 " \"c\";"); 4775 } 4776 4777 TEST_F(FormatTest, ReturnTypeBreakingStyle) { 4778 FormatStyle Style = getLLVMStyle(); 4779 // No declarations or definitions should be moved to own line. 4780 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None; 4781 verifyFormat("class A {\n" 4782 " int f() { return 1; }\n" 4783 " int g();\n" 4784 "};\n" 4785 "int f() { return 1; }\n" 4786 "int g();\n", 4787 Style); 4788 4789 // All declarations and definitions should have the return type moved to its 4790 // own 4791 // line. 4792 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 4793 verifyFormat("class E {\n" 4794 " int\n" 4795 " f() {\n" 4796 " return 1;\n" 4797 " }\n" 4798 " int\n" 4799 " g();\n" 4800 "};\n" 4801 "int\n" 4802 "f() {\n" 4803 " return 1;\n" 4804 "}\n" 4805 "int\n" 4806 "g();\n", 4807 Style); 4808 4809 // Top-level definitions, and no kinds of declarations should have the 4810 // return type moved to its own line. 4811 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions; 4812 verifyFormat("class B {\n" 4813 " int f() { return 1; }\n" 4814 " int g();\n" 4815 "};\n" 4816 "int\n" 4817 "f() {\n" 4818 " return 1;\n" 4819 "}\n" 4820 "int g();\n", 4821 Style); 4822 4823 // Top-level definitions and declarations should have the return type moved 4824 // to its own line. 4825 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel; 4826 verifyFormat("class C {\n" 4827 " int f() { return 1; }\n" 4828 " int g();\n" 4829 "};\n" 4830 "int\n" 4831 "f() {\n" 4832 " return 1;\n" 4833 "}\n" 4834 "int\n" 4835 "g();\n", 4836 Style); 4837 4838 // All definitions should have the return type moved to its own line, but no 4839 // kinds of declarations. 4840 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 4841 verifyFormat("class D {\n" 4842 " int\n" 4843 " f() {\n" 4844 " return 1;\n" 4845 " }\n" 4846 " int g();\n" 4847 "};\n" 4848 "int\n" 4849 "f() {\n" 4850 " return 1;\n" 4851 "}\n" 4852 "int g();\n", 4853 Style); 4854 verifyFormat("const char *\n" 4855 "f(void) {\n" // Break here. 4856 " return \"\";\n" 4857 "}\n" 4858 "const char *bar(void);\n", // No break here. 4859 Style); 4860 verifyFormat("template <class T>\n" 4861 "T *\n" 4862 "f(T &c) {\n" // Break here. 4863 " return NULL;\n" 4864 "}\n" 4865 "template <class T> T *f(T &c);\n", // No break here. 4866 Style); 4867 verifyFormat("class C {\n" 4868 " int\n" 4869 " operator+() {\n" 4870 " return 1;\n" 4871 " }\n" 4872 " int\n" 4873 " operator()() {\n" 4874 " return 1;\n" 4875 " }\n" 4876 "};\n", 4877 Style); 4878 verifyFormat("void\n" 4879 "A::operator()() {}\n" 4880 "void\n" 4881 "A::operator>>() {}\n" 4882 "void\n" 4883 "A::operator+() {}\n", 4884 Style); 4885 verifyFormat("void *operator new(std::size_t s);", // No break here. 4886 Style); 4887 verifyFormat("void *\n" 4888 "operator new(std::size_t s) {}", 4889 Style); 4890 verifyFormat("void *\n" 4891 "operator delete[](void *ptr) {}", 4892 Style); 4893 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 4894 verifyFormat("const char *\n" 4895 "f(void)\n" // Break here. 4896 "{\n" 4897 " return \"\";\n" 4898 "}\n" 4899 "const char *bar(void);\n", // No break here. 4900 Style); 4901 verifyFormat("template <class T>\n" 4902 "T *\n" // Problem here: no line break 4903 "f(T &c)\n" // Break here. 4904 "{\n" 4905 " return NULL;\n" 4906 "}\n" 4907 "template <class T> T *f(T &c);\n", // No break here. 4908 Style); 4909 } 4910 4911 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) { 4912 FormatStyle NoBreak = getLLVMStyle(); 4913 NoBreak.AlwaysBreakBeforeMultilineStrings = false; 4914 FormatStyle Break = getLLVMStyle(); 4915 Break.AlwaysBreakBeforeMultilineStrings = true; 4916 verifyFormat("aaaa = \"bbbb\"\n" 4917 " \"cccc\";", 4918 NoBreak); 4919 verifyFormat("aaaa =\n" 4920 " \"bbbb\"\n" 4921 " \"cccc\";", 4922 Break); 4923 verifyFormat("aaaa(\"bbbb\"\n" 4924 " \"cccc\");", 4925 NoBreak); 4926 verifyFormat("aaaa(\n" 4927 " \"bbbb\"\n" 4928 " \"cccc\");", 4929 Break); 4930 verifyFormat("aaaa(qqq, \"bbbb\"\n" 4931 " \"cccc\");", 4932 NoBreak); 4933 verifyFormat("aaaa(qqq,\n" 4934 " \"bbbb\"\n" 4935 " \"cccc\");", 4936 Break); 4937 verifyFormat("aaaa(qqq,\n" 4938 " L\"bbbb\"\n" 4939 " L\"cccc\");", 4940 Break); 4941 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n" 4942 " \"bbbb\"));", 4943 Break); 4944 verifyFormat("string s = someFunction(\n" 4945 " \"abc\"\n" 4946 " \"abc\");", 4947 Break); 4948 4949 // As we break before unary operators, breaking right after them is bad. 4950 verifyFormat("string foo = abc ? \"x\"\n" 4951 " \"blah blah blah blah blah blah\"\n" 4952 " : \"y\";", 4953 Break); 4954 4955 // Don't break if there is no column gain. 4956 verifyFormat("f(\"aaaa\"\n" 4957 " \"bbbb\");", 4958 Break); 4959 4960 // Treat literals with escaped newlines like multi-line string literals. 4961 EXPECT_EQ("x = \"a\\\n" 4962 "b\\\n" 4963 "c\";", 4964 format("x = \"a\\\n" 4965 "b\\\n" 4966 "c\";", 4967 NoBreak)); 4968 EXPECT_EQ("xxxx =\n" 4969 " \"a\\\n" 4970 "b\\\n" 4971 "c\";", 4972 format("xxxx = \"a\\\n" 4973 "b\\\n" 4974 "c\";", 4975 Break)); 4976 4977 EXPECT_EQ("NSString *const kString =\n" 4978 " @\"aaaa\"\n" 4979 " @\"bbbb\";", 4980 format("NSString *const kString = @\"aaaa\"\n" 4981 "@\"bbbb\";", 4982 Break)); 4983 4984 Break.ColumnLimit = 0; 4985 verifyFormat("const char *hello = \"hello llvm\";", Break); 4986 } 4987 4988 TEST_F(FormatTest, AlignsPipes) { 4989 verifyFormat( 4990 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4991 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4992 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4993 verifyFormat( 4994 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 4995 " << aaaaaaaaaaaaaaaaaaaa;"); 4996 verifyFormat( 4997 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4998 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4999 verifyFormat( 5000 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 5001 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5002 verifyFormat( 5003 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 5004 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 5005 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 5006 verifyFormat( 5007 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5008 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5009 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5010 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5011 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5012 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5013 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 5014 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n" 5015 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);"); 5016 verifyFormat( 5017 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5018 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5019 verifyFormat( 5020 "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n" 5021 " aaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5022 5023 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n" 5024 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();"); 5025 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5026 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5027 " aaaaaaaaaaaaaaaaaaaaa)\n" 5028 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5029 verifyFormat("LOG_IF(aaa == //\n" 5030 " bbb)\n" 5031 " << a << b;"); 5032 5033 // But sometimes, breaking before the first "<<" is desirable. 5034 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5035 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);"); 5036 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n" 5037 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5038 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5039 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n" 5040 " << BEF << IsTemplate << Description << E->getType();"); 5041 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5042 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5043 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5044 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5045 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5046 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5047 " << aaa;"); 5048 5049 verifyFormat( 5050 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5051 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5052 5053 // Incomplete string literal. 5054 EXPECT_EQ("llvm::errs() << \"\n" 5055 " << a;", 5056 format("llvm::errs() << \"\n<<a;")); 5057 5058 verifyFormat("void f() {\n" 5059 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n" 5060 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n" 5061 "}"); 5062 5063 // Handle 'endl'. 5064 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n" 5065 " << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 5066 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 5067 5068 // Handle '\n'. 5069 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n" 5070 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 5071 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n" 5072 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';"); 5073 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n" 5074 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";"); 5075 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 5076 } 5077 5078 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) { 5079 verifyFormat("return out << \"somepacket = {\\n\"\n" 5080 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" 5081 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" 5082 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" 5083 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" 5084 " << \"}\";"); 5085 5086 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 5087 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 5088 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;"); 5089 verifyFormat( 5090 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" 5091 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" 5092 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" 5093 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" 5094 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); 5095 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" 5096 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 5097 verifyFormat( 5098 "void f() {\n" 5099 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n" 5100 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 5101 "}"); 5102 5103 // Breaking before the first "<<" is generally not desirable. 5104 verifyFormat( 5105 "llvm::errs()\n" 5106 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5107 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5108 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5109 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5110 getLLVMStyleWithColumns(70)); 5111 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5112 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5113 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5114 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5115 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5116 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5117 getLLVMStyleWithColumns(70)); 5118 5119 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 5120 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 5121 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;"); 5122 verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 5123 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 5124 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);"); 5125 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n" 5126 " (aaaa + aaaa);", 5127 getLLVMStyleWithColumns(40)); 5128 verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n" 5129 " (aaaaaaa + aaaaa));", 5130 getLLVMStyleWithColumns(40)); 5131 verifyFormat( 5132 "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n" 5133 " SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n" 5134 " bbbbbbbbbbbbbbbbbbbbbbb);"); 5135 } 5136 5137 TEST_F(FormatTest, UnderstandsEquals) { 5138 verifyFormat( 5139 "aaaaaaaaaaaaaaaaa =\n" 5140 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5141 verifyFormat( 5142 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5143 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 5144 verifyFormat( 5145 "if (a) {\n" 5146 " f();\n" 5147 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5148 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 5149 "}"); 5150 5151 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5152 " 100000000 + 10000000) {\n}"); 5153 } 5154 5155 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 5156 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 5157 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 5158 5159 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 5160 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 5161 5162 verifyFormat( 5163 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 5164 " Parameter2);"); 5165 5166 verifyFormat( 5167 "ShortObject->shortFunction(\n" 5168 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 5169 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 5170 5171 verifyFormat("loooooooooooooongFunction(\n" 5172 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 5173 5174 verifyFormat( 5175 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 5176 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 5177 5178 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 5179 " .WillRepeatedly(Return(SomeValue));"); 5180 verifyFormat("void f() {\n" 5181 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 5182 " .Times(2)\n" 5183 " .WillRepeatedly(Return(SomeValue));\n" 5184 "}"); 5185 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n" 5186 " ccccccccccccccccccccccc);"); 5187 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5188 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5189 " .aaaaa(aaaaa),\n" 5190 " aaaaaaaaaaaaaaaaaaaaa);"); 5191 verifyFormat("void f() {\n" 5192 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5193 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n" 5194 "}"); 5195 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5196 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5197 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5198 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5199 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 5200 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5201 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5202 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5203 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n" 5204 "}"); 5205 5206 // Here, it is not necessary to wrap at "." or "->". 5207 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 5208 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 5209 verifyFormat( 5210 "aaaaaaaaaaa->aaaaaaaaa(\n" 5211 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5212 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 5213 5214 verifyFormat( 5215 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5216 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); 5217 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n" 5218 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 5219 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" 5220 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 5221 5222 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5223 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5224 " .a();"); 5225 5226 FormatStyle NoBinPacking = getLLVMStyle(); 5227 NoBinPacking.BinPackParameters = false; 5228 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 5229 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 5230 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" 5231 " aaaaaaaaaaaaaaaaaaa,\n" 5232 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5233 NoBinPacking); 5234 5235 // If there is a subsequent call, change to hanging indentation. 5236 verifyFormat( 5237 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5238 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n" 5239 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5240 verifyFormat( 5241 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5242 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));"); 5243 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5244 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5245 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5246 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5247 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5248 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 5249 } 5250 5251 TEST_F(FormatTest, WrapsTemplateDeclarations) { 5252 verifyFormat("template <typename T>\n" 5253 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 5254 verifyFormat("template <typename T>\n" 5255 "// T should be one of {A, B}.\n" 5256 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 5257 verifyFormat( 5258 "template <typename T>\n" 5259 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); 5260 verifyFormat("template <typename T>\n" 5261 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 5262 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 5263 verifyFormat( 5264 "template <typename T>\n" 5265 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 5266 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 5267 verifyFormat( 5268 "template <typename T>\n" 5269 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 5270 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 5271 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5272 verifyFormat("template <typename T>\n" 5273 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5274 " int aaaaaaaaaaaaaaaaaaaaaa);"); 5275 verifyFormat( 5276 "template <typename T1, typename T2 = char, typename T3 = char,\n" 5277 " typename T4 = char>\n" 5278 "void f();"); 5279 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n" 5280 " template <typename> class cccccccccccccccccccccc,\n" 5281 " typename ddddddddddddd>\n" 5282 "class C {};"); 5283 verifyFormat( 5284 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 5285 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5286 5287 verifyFormat("void f() {\n" 5288 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 5289 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n" 5290 "}"); 5291 5292 verifyFormat("template <typename T> class C {};"); 5293 verifyFormat("template <typename T> void f();"); 5294 verifyFormat("template <typename T> void f() {}"); 5295 verifyFormat( 5296 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 5297 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5298 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n" 5299 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 5300 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5301 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n" 5302 " bbbbbbbbbbbbbbbbbbbbbbbb);", 5303 getLLVMStyleWithColumns(72)); 5304 EXPECT_EQ("static_cast<A< //\n" 5305 " B> *>(\n" 5306 "\n" 5307 ");", 5308 format("static_cast<A<//\n" 5309 " B>*>(\n" 5310 "\n" 5311 " );")); 5312 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5313 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);"); 5314 5315 FormatStyle AlwaysBreak = getLLVMStyle(); 5316 AlwaysBreak.AlwaysBreakTemplateDeclarations = true; 5317 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak); 5318 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak); 5319 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak); 5320 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5321 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 5322 " ccccccccccccccccccccccccccccccccccccccccccccccc);"); 5323 verifyFormat("template <template <typename> class Fooooooo,\n" 5324 " template <typename> class Baaaaaaar>\n" 5325 "struct C {};", 5326 AlwaysBreak); 5327 verifyFormat("template <typename T> // T can be A, B or C.\n" 5328 "struct C {};", 5329 AlwaysBreak); 5330 verifyFormat("template <enum E> class A {\n" 5331 "public:\n" 5332 " E *f();\n" 5333 "};"); 5334 } 5335 5336 TEST_F(FormatTest, WrapsTemplateParameters) { 5337 FormatStyle Style = getLLVMStyle(); 5338 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 5339 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 5340 verifyFormat( 5341 "template <typename... a> struct q {};\n" 5342 "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 5343 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 5344 " y;", 5345 Style); 5346 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 5347 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 5348 verifyFormat( 5349 "template <typename... a> struct r {};\n" 5350 "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 5351 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 5352 " y;", 5353 Style); 5354 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 5355 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 5356 verifyFormat( 5357 "template <typename... a> struct s {};\n" 5358 "extern s<\n" 5359 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 5360 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n" 5361 " y;", 5362 Style); 5363 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 5364 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 5365 verifyFormat( 5366 "template <typename... a> struct t {};\n" 5367 "extern t<\n" 5368 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 5369 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n" 5370 " y;", 5371 Style); 5372 } 5373 5374 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 5375 verifyFormat( 5376 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5377 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5378 verifyFormat( 5379 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5380 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5381 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 5382 5383 // FIXME: Should we have the extra indent after the second break? 5384 verifyFormat( 5385 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5386 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5387 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5388 5389 verifyFormat( 5390 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 5391 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 5392 5393 // Breaking at nested name specifiers is generally not desirable. 5394 verifyFormat( 5395 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5396 " aaaaaaaaaaaaaaaaaaaaaaa);"); 5397 5398 verifyFormat( 5399 "aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n" 5400 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5401 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5402 " aaaaaaaaaaaaaaaaaaaaa);", 5403 getLLVMStyleWithColumns(74)); 5404 5405 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5406 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5407 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5408 } 5409 5410 TEST_F(FormatTest, UnderstandsTemplateParameters) { 5411 verifyFormat("A<int> a;"); 5412 verifyFormat("A<A<A<int>>> a;"); 5413 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 5414 verifyFormat("bool x = a < 1 || 2 > a;"); 5415 verifyFormat("bool x = 5 < f<int>();"); 5416 verifyFormat("bool x = f<int>() > 5;"); 5417 verifyFormat("bool x = 5 < a<int>::x;"); 5418 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 5419 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 5420 5421 verifyGoogleFormat("A<A<int>> a;"); 5422 verifyGoogleFormat("A<A<A<int>>> a;"); 5423 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 5424 verifyGoogleFormat("A<A<int> > a;"); 5425 verifyGoogleFormat("A<A<A<int> > > a;"); 5426 verifyGoogleFormat("A<A<A<A<int> > > > a;"); 5427 verifyGoogleFormat("A<::A<int>> a;"); 5428 verifyGoogleFormat("A<::A> a;"); 5429 verifyGoogleFormat("A< ::A> a;"); 5430 verifyGoogleFormat("A< ::A<int> > a;"); 5431 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); 5432 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); 5433 EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle())); 5434 EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle())); 5435 EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };", 5436 format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle())); 5437 5438 verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp)); 5439 5440 verifyFormat("test >> a >> b;"); 5441 verifyFormat("test << a >> b;"); 5442 5443 verifyFormat("f<int>();"); 5444 verifyFormat("template <typename T> void f() {}"); 5445 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;"); 5446 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : " 5447 "sizeof(char)>::type>;"); 5448 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};"); 5449 verifyFormat("f(a.operator()<A>());"); 5450 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5451 " .template operator()<A>());", 5452 getLLVMStyleWithColumns(35)); 5453 5454 // Not template parameters. 5455 verifyFormat("return a < b && c > d;"); 5456 verifyFormat("void f() {\n" 5457 " while (a < b && c > d) {\n" 5458 " }\n" 5459 "}"); 5460 verifyFormat("template <typename... Types>\n" 5461 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}"); 5462 5463 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5464 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);", 5465 getLLVMStyleWithColumns(60)); 5466 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");"); 5467 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}"); 5468 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <"); 5469 } 5470 5471 TEST_F(FormatTest, BitshiftOperatorWidth) { 5472 EXPECT_EQ("int a = 1 << 2; /* foo\n" 5473 " bar */", 5474 format("int a=1<<2; /* foo\n" 5475 " bar */")); 5476 5477 EXPECT_EQ("int b = 256 >> 1; /* foo\n" 5478 " bar */", 5479 format("int b =256>>1 ; /* foo\n" 5480 " bar */")); 5481 } 5482 5483 TEST_F(FormatTest, UnderstandsBinaryOperators) { 5484 verifyFormat("COMPARE(a, ==, b);"); 5485 verifyFormat("auto s = sizeof...(Ts) - 1;"); 5486 } 5487 5488 TEST_F(FormatTest, UnderstandsPointersToMembers) { 5489 verifyFormat("int A::*x;"); 5490 verifyFormat("int (S::*func)(void *);"); 5491 verifyFormat("void f() { int (S::*func)(void *); }"); 5492 verifyFormat("typedef bool *(Class::*Member)() const;"); 5493 verifyFormat("void f() {\n" 5494 " (a->*f)();\n" 5495 " a->*x;\n" 5496 " (a.*f)();\n" 5497 " ((*a).*f)();\n" 5498 " a.*x;\n" 5499 "}"); 5500 verifyFormat("void f() {\n" 5501 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 5502 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n" 5503 "}"); 5504 verifyFormat( 5505 "(aaaaaaaaaa->*bbbbbbb)(\n" 5506 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 5507 FormatStyle Style = getLLVMStyle(); 5508 Style.PointerAlignment = FormatStyle::PAS_Left; 5509 verifyFormat("typedef bool* (Class::*Member)() const;", Style); 5510 } 5511 5512 TEST_F(FormatTest, UnderstandsUnaryOperators) { 5513 verifyFormat("int a = -2;"); 5514 verifyFormat("f(-1, -2, -3);"); 5515 verifyFormat("a[-1] = 5;"); 5516 verifyFormat("int a = 5 + -2;"); 5517 verifyFormat("if (i == -1) {\n}"); 5518 verifyFormat("if (i != -1) {\n}"); 5519 verifyFormat("if (i > -1) {\n}"); 5520 verifyFormat("if (i < -1) {\n}"); 5521 verifyFormat("++(a->f());"); 5522 verifyFormat("--(a->f());"); 5523 verifyFormat("(a->f())++;"); 5524 verifyFormat("a[42]++;"); 5525 verifyFormat("if (!(a->f())) {\n}"); 5526 5527 verifyFormat("a-- > b;"); 5528 verifyFormat("b ? -a : c;"); 5529 verifyFormat("n * sizeof char16;"); 5530 verifyFormat("n * alignof char16;", getGoogleStyle()); 5531 verifyFormat("sizeof(char);"); 5532 verifyFormat("alignof(char);", getGoogleStyle()); 5533 5534 verifyFormat("return -1;"); 5535 verifyFormat("switch (a) {\n" 5536 "case -1:\n" 5537 " break;\n" 5538 "}"); 5539 verifyFormat("#define X -1"); 5540 verifyFormat("#define X -kConstant"); 5541 5542 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};"); 5543 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};"); 5544 5545 verifyFormat("int a = /* confusing comment */ -1;"); 5546 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 5547 verifyFormat("int a = i /* confusing comment */++;"); 5548 } 5549 5550 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) { 5551 verifyFormat("if (!aaaaaaaaaa( // break\n" 5552 " aaaaa)) {\n" 5553 "}"); 5554 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n" 5555 " aaaaa));"); 5556 verifyFormat("*aaa = aaaaaaa( // break\n" 5557 " bbbbbb);"); 5558 } 5559 5560 TEST_F(FormatTest, UnderstandsOverloadedOperators) { 5561 verifyFormat("bool operator<();"); 5562 verifyFormat("bool operator>();"); 5563 verifyFormat("bool operator=();"); 5564 verifyFormat("bool operator==();"); 5565 verifyFormat("bool operator!=();"); 5566 verifyFormat("int operator+();"); 5567 verifyFormat("int operator++();"); 5568 verifyFormat("bool operator,();"); 5569 verifyFormat("bool operator();"); 5570 verifyFormat("bool operator()();"); 5571 verifyFormat("bool operator[]();"); 5572 verifyFormat("operator bool();"); 5573 verifyFormat("operator int();"); 5574 verifyFormat("operator void *();"); 5575 verifyFormat("operator SomeType<int>();"); 5576 verifyFormat("operator SomeType<int, int>();"); 5577 verifyFormat("operator SomeType<SomeType<int>>();"); 5578 verifyFormat("void *operator new(std::size_t size);"); 5579 verifyFormat("void *operator new[](std::size_t size);"); 5580 verifyFormat("void operator delete(void *ptr);"); 5581 verifyFormat("void operator delete[](void *ptr);"); 5582 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n" 5583 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);"); 5584 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n" 5585 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;"); 5586 5587 verifyFormat( 5588 "ostream &operator<<(ostream &OutputStream,\n" 5589 " SomeReallyLongType WithSomeReallyLongValue);"); 5590 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n" 5591 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n" 5592 " return left.group < right.group;\n" 5593 "}"); 5594 verifyFormat("SomeType &operator=(const SomeType &S);"); 5595 verifyFormat("f.template operator()<int>();"); 5596 5597 verifyGoogleFormat("operator void*();"); 5598 verifyGoogleFormat("operator SomeType<SomeType<int>>();"); 5599 verifyGoogleFormat("operator ::A();"); 5600 5601 verifyFormat("using A::operator+;"); 5602 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n" 5603 "int i;"); 5604 } 5605 5606 TEST_F(FormatTest, UnderstandsFunctionRefQualification) { 5607 verifyFormat("Deleted &operator=(const Deleted &) & = default;"); 5608 verifyFormat("Deleted &operator=(const Deleted &) && = delete;"); 5609 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;"); 5610 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;"); 5611 verifyFormat("Deleted &operator=(const Deleted &) &;"); 5612 verifyFormat("Deleted &operator=(const Deleted &) &&;"); 5613 verifyFormat("SomeType MemberFunction(const Deleted &) &;"); 5614 verifyFormat("SomeType MemberFunction(const Deleted &) &&;"); 5615 verifyFormat("SomeType MemberFunction(const Deleted &) && {}"); 5616 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}"); 5617 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}"); 5618 verifyFormat("void Fn(T const &) const &;"); 5619 verifyFormat("void Fn(T const volatile &&) const volatile &&;"); 5620 verifyFormat("template <typename T>\n" 5621 "void F(T) && = delete;", 5622 getGoogleStyle()); 5623 5624 FormatStyle AlignLeft = getLLVMStyle(); 5625 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 5626 verifyFormat("void A::b() && {}", AlignLeft); 5627 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft); 5628 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;", 5629 AlignLeft); 5630 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft); 5631 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft); 5632 verifyFormat("auto Function(T t) & -> void {}", AlignLeft); 5633 verifyFormat("auto Function(T... t) & -> void {}", AlignLeft); 5634 verifyFormat("auto Function(T) & -> void {}", AlignLeft); 5635 verifyFormat("auto Function(T) & -> void;", AlignLeft); 5636 verifyFormat("void Fn(T const&) const&;", AlignLeft); 5637 verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft); 5638 5639 FormatStyle Spaces = getLLVMStyle(); 5640 Spaces.SpacesInCStyleCastParentheses = true; 5641 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces); 5642 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces); 5643 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces); 5644 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces); 5645 5646 Spaces.SpacesInCStyleCastParentheses = false; 5647 Spaces.SpacesInParentheses = true; 5648 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces); 5649 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces); 5650 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces); 5651 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces); 5652 } 5653 5654 TEST_F(FormatTest, UnderstandsNewAndDelete) { 5655 verifyFormat("void f() {\n" 5656 " A *a = new A;\n" 5657 " A *a = new (placement) A;\n" 5658 " delete a;\n" 5659 " delete (A *)a;\n" 5660 "}"); 5661 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 5662 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 5663 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5664 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 5665 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 5666 verifyFormat("delete[] h->p;"); 5667 } 5668 5669 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 5670 verifyFormat("int *f(int *a) {}"); 5671 verifyFormat("int main(int argc, char **argv) {}"); 5672 verifyFormat("Test::Test(int b) : a(b * b) {}"); 5673 verifyIndependentOfContext("f(a, *a);"); 5674 verifyFormat("void g() { f(*a); }"); 5675 verifyIndependentOfContext("int a = b * 10;"); 5676 verifyIndependentOfContext("int a = 10 * b;"); 5677 verifyIndependentOfContext("int a = b * c;"); 5678 verifyIndependentOfContext("int a += b * c;"); 5679 verifyIndependentOfContext("int a -= b * c;"); 5680 verifyIndependentOfContext("int a *= b * c;"); 5681 verifyIndependentOfContext("int a /= b * c;"); 5682 verifyIndependentOfContext("int a = *b;"); 5683 verifyIndependentOfContext("int a = *b * c;"); 5684 verifyIndependentOfContext("int a = b * *c;"); 5685 verifyIndependentOfContext("int a = b * (10);"); 5686 verifyIndependentOfContext("S << b * (10);"); 5687 verifyIndependentOfContext("return 10 * b;"); 5688 verifyIndependentOfContext("return *b * *c;"); 5689 verifyIndependentOfContext("return a & ~b;"); 5690 verifyIndependentOfContext("f(b ? *c : *d);"); 5691 verifyIndependentOfContext("int a = b ? *c : *d;"); 5692 verifyIndependentOfContext("*b = a;"); 5693 verifyIndependentOfContext("a * ~b;"); 5694 verifyIndependentOfContext("a * !b;"); 5695 verifyIndependentOfContext("a * +b;"); 5696 verifyIndependentOfContext("a * -b;"); 5697 verifyIndependentOfContext("a * ++b;"); 5698 verifyIndependentOfContext("a * --b;"); 5699 verifyIndependentOfContext("a[4] * b;"); 5700 verifyIndependentOfContext("a[a * a] = 1;"); 5701 verifyIndependentOfContext("f() * b;"); 5702 verifyIndependentOfContext("a * [self dostuff];"); 5703 verifyIndependentOfContext("int x = a * (a + b);"); 5704 verifyIndependentOfContext("(a *)(a + b);"); 5705 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;"); 5706 verifyIndependentOfContext("int *pa = (int *)&a;"); 5707 verifyIndependentOfContext("return sizeof(int **);"); 5708 verifyIndependentOfContext("return sizeof(int ******);"); 5709 verifyIndependentOfContext("return (int **&)a;"); 5710 verifyIndependentOfContext("f((*PointerToArray)[10]);"); 5711 verifyFormat("void f(Type (*parameter)[10]) {}"); 5712 verifyFormat("void f(Type (¶meter)[10]) {}"); 5713 verifyGoogleFormat("return sizeof(int**);"); 5714 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); 5715 verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); 5716 verifyFormat("auto a = [](int **&, int ***) {};"); 5717 verifyFormat("auto PointerBinding = [](const char *S) {};"); 5718 verifyFormat("typedef typeof(int(int, int)) *MyFunc;"); 5719 verifyFormat("[](const decltype(*a) &value) {}"); 5720 verifyFormat("decltype(a * b) F();"); 5721 verifyFormat("#define MACRO() [](A *a) { return 1; }"); 5722 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}"); 5723 verifyIndependentOfContext("typedef void (*f)(int *a);"); 5724 verifyIndependentOfContext("int i{a * b};"); 5725 verifyIndependentOfContext("aaa && aaa->f();"); 5726 verifyIndependentOfContext("int x = ~*p;"); 5727 verifyFormat("Constructor() : a(a), area(width * height) {}"); 5728 verifyFormat("Constructor() : a(a), area(a, width * height) {}"); 5729 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}"); 5730 verifyFormat("void f() { f(a, c * d); }"); 5731 verifyFormat("void f() { f(new a(), c * d); }"); 5732 verifyFormat("void f(const MyOverride &override);"); 5733 verifyFormat("void f(const MyFinal &final);"); 5734 verifyIndependentOfContext("bool a = f() && override.f();"); 5735 verifyIndependentOfContext("bool a = f() && final.f();"); 5736 5737 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 5738 5739 verifyIndependentOfContext("A<int *> a;"); 5740 verifyIndependentOfContext("A<int **> a;"); 5741 verifyIndependentOfContext("A<int *, int *> a;"); 5742 verifyIndependentOfContext("A<int *[]> a;"); 5743 verifyIndependentOfContext( 5744 "const char *const p = reinterpret_cast<const char *const>(q);"); 5745 verifyIndependentOfContext("A<int **, int **> a;"); 5746 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); 5747 verifyFormat("for (char **a = b; *a; ++a) {\n}"); 5748 verifyFormat("for (; a && b;) {\n}"); 5749 verifyFormat("bool foo = true && [] { return false; }();"); 5750 5751 verifyFormat( 5752 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5753 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5754 5755 verifyGoogleFormat("int const* a = &b;"); 5756 verifyGoogleFormat("**outparam = 1;"); 5757 verifyGoogleFormat("*outparam = a * b;"); 5758 verifyGoogleFormat("int main(int argc, char** argv) {}"); 5759 verifyGoogleFormat("A<int*> a;"); 5760 verifyGoogleFormat("A<int**> a;"); 5761 verifyGoogleFormat("A<int*, int*> a;"); 5762 verifyGoogleFormat("A<int**, int**> a;"); 5763 verifyGoogleFormat("f(b ? *c : *d);"); 5764 verifyGoogleFormat("int a = b ? *c : *d;"); 5765 verifyGoogleFormat("Type* t = **x;"); 5766 verifyGoogleFormat("Type* t = *++*x;"); 5767 verifyGoogleFormat("*++*x;"); 5768 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 5769 verifyGoogleFormat("Type* t = x++ * y;"); 5770 verifyGoogleFormat( 5771 "const char* const p = reinterpret_cast<const char* const>(q);"); 5772 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);"); 5773 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);"); 5774 verifyGoogleFormat("template <typename T>\n" 5775 "void f(int i = 0, SomeType** temps = NULL);"); 5776 5777 FormatStyle Left = getLLVMStyle(); 5778 Left.PointerAlignment = FormatStyle::PAS_Left; 5779 verifyFormat("x = *a(x) = *a(y);", Left); 5780 verifyFormat("for (;; *a = b) {\n}", Left); 5781 verifyFormat("return *this += 1;", Left); 5782 verifyFormat("throw *x;", Left); 5783 verifyFormat("delete *x;", Left); 5784 verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left); 5785 verifyFormat("[](const decltype(*a)* ptr) {}", Left); 5786 verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left); 5787 5788 verifyIndependentOfContext("a = *(x + y);"); 5789 verifyIndependentOfContext("a = &(x + y);"); 5790 verifyIndependentOfContext("*(x + y).call();"); 5791 verifyIndependentOfContext("&(x + y)->call();"); 5792 verifyFormat("void f() { &(*I).first; }"); 5793 5794 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 5795 verifyFormat( 5796 "int *MyValues = {\n" 5797 " *A, // Operator detection might be confused by the '{'\n" 5798 " *BB // Operator detection might be confused by previous comment\n" 5799 "};"); 5800 5801 verifyIndependentOfContext("if (int *a = &b)"); 5802 verifyIndependentOfContext("if (int &a = *b)"); 5803 verifyIndependentOfContext("if (a & b[i])"); 5804 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 5805 verifyIndependentOfContext("if (*b[i])"); 5806 verifyIndependentOfContext("if (int *a = (&b))"); 5807 verifyIndependentOfContext("while (int *a = &b)"); 5808 verifyIndependentOfContext("size = sizeof *a;"); 5809 verifyIndependentOfContext("if (a && (b = c))"); 5810 verifyFormat("void f() {\n" 5811 " for (const int &v : Values) {\n" 5812 " }\n" 5813 "}"); 5814 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); 5815 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); 5816 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}"); 5817 5818 verifyFormat("#define A (!a * b)"); 5819 verifyFormat("#define MACRO \\\n" 5820 " int *i = a * b; \\\n" 5821 " void f(a *b);", 5822 getLLVMStyleWithColumns(19)); 5823 5824 verifyIndependentOfContext("A = new SomeType *[Length];"); 5825 verifyIndependentOfContext("A = new SomeType *[Length]();"); 5826 verifyIndependentOfContext("T **t = new T *;"); 5827 verifyIndependentOfContext("T **t = new T *();"); 5828 verifyGoogleFormat("A = new SomeType*[Length]();"); 5829 verifyGoogleFormat("A = new SomeType*[Length];"); 5830 verifyGoogleFormat("T** t = new T*;"); 5831 verifyGoogleFormat("T** t = new T*();"); 5832 5833 verifyFormat("STATIC_ASSERT((a & b) == 0);"); 5834 verifyFormat("STATIC_ASSERT(0 == (a & b));"); 5835 verifyFormat("template <bool a, bool b> " 5836 "typename t::if<x && y>::type f() {}"); 5837 verifyFormat("template <int *y> f() {}"); 5838 verifyFormat("vector<int *> v;"); 5839 verifyFormat("vector<int *const> v;"); 5840 verifyFormat("vector<int *const **const *> v;"); 5841 verifyFormat("vector<int *volatile> v;"); 5842 verifyFormat("vector<a * b> v;"); 5843 verifyFormat("foo<b && false>();"); 5844 verifyFormat("foo<b & 1>();"); 5845 verifyFormat("decltype(*::std::declval<const T &>()) void F();"); 5846 verifyFormat( 5847 "template <class T, class = typename std::enable_if<\n" 5848 " std::is_integral<T>::value &&\n" 5849 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n" 5850 "void F();", 5851 getLLVMStyleWithColumns(70)); 5852 verifyFormat( 5853 "template <class T,\n" 5854 " class = typename std::enable_if<\n" 5855 " std::is_integral<T>::value &&\n" 5856 " (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n" 5857 " class U>\n" 5858 "void F();", 5859 getLLVMStyleWithColumns(70)); 5860 verifyFormat( 5861 "template <class T,\n" 5862 " class = typename ::std::enable_if<\n" 5863 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n" 5864 "void F();", 5865 getGoogleStyleWithColumns(68)); 5866 5867 verifyIndependentOfContext("MACRO(int *i);"); 5868 verifyIndependentOfContext("MACRO(auto *a);"); 5869 verifyIndependentOfContext("MACRO(const A *a);"); 5870 verifyIndependentOfContext("MACRO(A *const a);"); 5871 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');"); 5872 verifyFormat("void f() { f(float{1}, a * a); }"); 5873 // FIXME: Is there a way to make this work? 5874 // verifyIndependentOfContext("MACRO(A *a);"); 5875 5876 verifyFormat("DatumHandle const *operator->() const { return input_; }"); 5877 verifyFormat("return options != nullptr && operator==(*options);"); 5878 5879 EXPECT_EQ("#define OP(x) \\\n" 5880 " ostream &operator<<(ostream &s, const A &a) { \\\n" 5881 " return s << a.DebugString(); \\\n" 5882 " }", 5883 format("#define OP(x) \\\n" 5884 " ostream &operator<<(ostream &s, const A &a) { \\\n" 5885 " return s << a.DebugString(); \\\n" 5886 " }", 5887 getLLVMStyleWithColumns(50))); 5888 5889 // FIXME: We cannot handle this case yet; we might be able to figure out that 5890 // foo<x> d > v; doesn't make sense. 5891 verifyFormat("foo<a<b && c> d> v;"); 5892 5893 FormatStyle PointerMiddle = getLLVMStyle(); 5894 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 5895 verifyFormat("delete *x;", PointerMiddle); 5896 verifyFormat("int * x;", PointerMiddle); 5897 verifyFormat("template <int * y> f() {}", PointerMiddle); 5898 verifyFormat("int * f(int * a) {}", PointerMiddle); 5899 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle); 5900 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle); 5901 verifyFormat("A<int *> a;", PointerMiddle); 5902 verifyFormat("A<int **> a;", PointerMiddle); 5903 verifyFormat("A<int *, int *> a;", PointerMiddle); 5904 verifyFormat("A<int * []> a;", PointerMiddle); 5905 verifyFormat("A = new SomeType *[Length]();", PointerMiddle); 5906 verifyFormat("A = new SomeType *[Length];", PointerMiddle); 5907 verifyFormat("T ** t = new T *;", PointerMiddle); 5908 5909 // Member function reference qualifiers aren't binary operators. 5910 verifyFormat("string // break\n" 5911 "operator()() & {}"); 5912 verifyFormat("string // break\n" 5913 "operator()() && {}"); 5914 verifyGoogleFormat("template <typename T>\n" 5915 "auto x() & -> int {}"); 5916 } 5917 5918 TEST_F(FormatTest, UnderstandsAttributes) { 5919 verifyFormat("SomeType s __attribute__((unused)) (InitValue);"); 5920 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n" 5921 "aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 5922 FormatStyle AfterType = getLLVMStyle(); 5923 AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 5924 verifyFormat("__attribute__((nodebug)) void\n" 5925 "foo() {}\n", 5926 AfterType); 5927 } 5928 5929 TEST_F(FormatTest, UnderstandsEllipsis) { 5930 verifyFormat("int printf(const char *fmt, ...);"); 5931 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); 5932 verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}"); 5933 5934 FormatStyle PointersLeft = getLLVMStyle(); 5935 PointersLeft.PointerAlignment = FormatStyle::PAS_Left; 5936 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft); 5937 } 5938 5939 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { 5940 EXPECT_EQ("int *a;\n" 5941 "int *a;\n" 5942 "int *a;", 5943 format("int *a;\n" 5944 "int* a;\n" 5945 "int *a;", 5946 getGoogleStyle())); 5947 EXPECT_EQ("int* a;\n" 5948 "int* a;\n" 5949 "int* a;", 5950 format("int* a;\n" 5951 "int* a;\n" 5952 "int *a;", 5953 getGoogleStyle())); 5954 EXPECT_EQ("int *a;\n" 5955 "int *a;\n" 5956 "int *a;", 5957 format("int *a;\n" 5958 "int * a;\n" 5959 "int * a;", 5960 getGoogleStyle())); 5961 EXPECT_EQ("auto x = [] {\n" 5962 " int *a;\n" 5963 " int *a;\n" 5964 " int *a;\n" 5965 "};", 5966 format("auto x=[]{int *a;\n" 5967 "int * a;\n" 5968 "int * a;};", 5969 getGoogleStyle())); 5970 } 5971 5972 TEST_F(FormatTest, UnderstandsRvalueReferences) { 5973 verifyFormat("int f(int &&a) {}"); 5974 verifyFormat("int f(int a, char &&b) {}"); 5975 verifyFormat("void f() { int &&a = b; }"); 5976 verifyGoogleFormat("int f(int a, char&& b) {}"); 5977 verifyGoogleFormat("void f() { int&& a = b; }"); 5978 5979 verifyIndependentOfContext("A<int &&> a;"); 5980 verifyIndependentOfContext("A<int &&, int &&> a;"); 5981 verifyGoogleFormat("A<int&&> a;"); 5982 verifyGoogleFormat("A<int&&, int&&> a;"); 5983 5984 // Not rvalue references: 5985 verifyFormat("template <bool B, bool C> class A {\n" 5986 " static_assert(B && C, \"Something is wrong\");\n" 5987 "};"); 5988 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))"); 5989 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))"); 5990 verifyFormat("#define A(a, b) (a && b)"); 5991 } 5992 5993 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 5994 verifyFormat("void f() {\n" 5995 " x[aaaaaaaaa -\n" 5996 " b] = 23;\n" 5997 "}", 5998 getLLVMStyleWithColumns(15)); 5999 } 6000 6001 TEST_F(FormatTest, FormatsCasts) { 6002 verifyFormat("Type *A = static_cast<Type *>(P);"); 6003 verifyFormat("Type *A = (Type *)P;"); 6004 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 6005 verifyFormat("int a = (int)(2.0f);"); 6006 verifyFormat("int a = (int)2.0f;"); 6007 verifyFormat("x[(int32)y];"); 6008 verifyFormat("x = (int32)y;"); 6009 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)"); 6010 verifyFormat("int a = (int)*b;"); 6011 verifyFormat("int a = (int)2.0f;"); 6012 verifyFormat("int a = (int)~0;"); 6013 verifyFormat("int a = (int)++a;"); 6014 verifyFormat("int a = (int)sizeof(int);"); 6015 verifyFormat("int a = (int)+2;"); 6016 verifyFormat("my_int a = (my_int)2.0f;"); 6017 verifyFormat("my_int a = (my_int)sizeof(int);"); 6018 verifyFormat("return (my_int)aaa;"); 6019 verifyFormat("#define x ((int)-1)"); 6020 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1"); 6021 verifyFormat("#define p(q) ((int *)&q)"); 6022 verifyFormat("fn(a)(b) + 1;"); 6023 6024 verifyFormat("void f() { my_int a = (my_int)*b; }"); 6025 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }"); 6026 verifyFormat("my_int a = (my_int)~0;"); 6027 verifyFormat("my_int a = (my_int)++a;"); 6028 verifyFormat("my_int a = (my_int)-2;"); 6029 verifyFormat("my_int a = (my_int)1;"); 6030 verifyFormat("my_int a = (my_int *)1;"); 6031 verifyFormat("my_int a = (const my_int)-1;"); 6032 verifyFormat("my_int a = (const my_int *)-1;"); 6033 verifyFormat("my_int a = (my_int)(my_int)-1;"); 6034 verifyFormat("my_int a = (ns::my_int)-2;"); 6035 verifyFormat("case (my_int)ONE:"); 6036 verifyFormat("auto x = (X)this;"); 6037 6038 // FIXME: single value wrapped with paren will be treated as cast. 6039 verifyFormat("void f(int i = (kValue)*kMask) {}"); 6040 6041 verifyFormat("{ (void)F; }"); 6042 6043 // Don't break after a cast's 6044 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 6045 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n" 6046 " bbbbbbbbbbbbbbbbbbbbbb);"); 6047 6048 // These are not casts. 6049 verifyFormat("void f(int *) {}"); 6050 verifyFormat("f(foo)->b;"); 6051 verifyFormat("f(foo).b;"); 6052 verifyFormat("f(foo)(b);"); 6053 verifyFormat("f(foo)[b];"); 6054 verifyFormat("[](foo) { return 4; }(bar);"); 6055 verifyFormat("(*funptr)(foo)[4];"); 6056 verifyFormat("funptrs[4](foo)[4];"); 6057 verifyFormat("void f(int *);"); 6058 verifyFormat("void f(int *) = 0;"); 6059 verifyFormat("void f(SmallVector<int>) {}"); 6060 verifyFormat("void f(SmallVector<int>);"); 6061 verifyFormat("void f(SmallVector<int>) = 0;"); 6062 verifyFormat("void f(int i = (kA * kB) & kMask) {}"); 6063 verifyFormat("int a = sizeof(int) * b;"); 6064 verifyFormat("int a = alignof(int) * b;", getGoogleStyle()); 6065 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;"); 6066 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");"); 6067 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;"); 6068 6069 // These are not casts, but at some point were confused with casts. 6070 verifyFormat("virtual void foo(int *) override;"); 6071 verifyFormat("virtual void foo(char &) const;"); 6072 verifyFormat("virtual void foo(int *a, char *) const;"); 6073 verifyFormat("int a = sizeof(int *) + b;"); 6074 verifyFormat("int a = alignof(int *) + b;", getGoogleStyle()); 6075 verifyFormat("bool b = f(g<int>) && c;"); 6076 verifyFormat("typedef void (*f)(int i) func;"); 6077 6078 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n" 6079 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 6080 // FIXME: The indentation here is not ideal. 6081 verifyFormat( 6082 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6083 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n" 6084 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];"); 6085 } 6086 6087 TEST_F(FormatTest, FormatsFunctionTypes) { 6088 verifyFormat("A<bool()> a;"); 6089 verifyFormat("A<SomeType()> a;"); 6090 verifyFormat("A<void (*)(int, std::string)> a;"); 6091 verifyFormat("A<void *(int)>;"); 6092 verifyFormat("void *(*a)(int *, SomeType *);"); 6093 verifyFormat("int (*func)(void *);"); 6094 verifyFormat("void f() { int (*func)(void *); }"); 6095 verifyFormat("template <class CallbackClass>\n" 6096 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);"); 6097 6098 verifyGoogleFormat("A<void*(int*, SomeType*)>;"); 6099 verifyGoogleFormat("void* (*a)(int);"); 6100 verifyGoogleFormat( 6101 "template <class CallbackClass>\n" 6102 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);"); 6103 6104 // Other constructs can look somewhat like function types: 6105 verifyFormat("A<sizeof(*x)> a;"); 6106 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)"); 6107 verifyFormat("some_var = function(*some_pointer_var)[0];"); 6108 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }"); 6109 verifyFormat("int x = f(&h)();"); 6110 verifyFormat("returnsFunction(¶m1, ¶m2)(param);"); 6111 verifyFormat("std::function<\n" 6112 " LooooooooooongTemplatedType<\n" 6113 " SomeType>*(\n" 6114 " LooooooooooooooooongType type)>\n" 6115 " function;", 6116 getGoogleStyleWithColumns(40)); 6117 } 6118 6119 TEST_F(FormatTest, FormatsPointersToArrayTypes) { 6120 verifyFormat("A (*foo_)[6];"); 6121 verifyFormat("vector<int> (*foo_)[6];"); 6122 } 6123 6124 TEST_F(FormatTest, BreaksLongVariableDeclarations) { 6125 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6126 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6127 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n" 6128 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6129 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6130 " *LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6131 6132 // Different ways of ()-initializiation. 6133 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6134 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);"); 6135 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6136 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);"); 6137 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6138 " LoooooooooooooooooooooooooooooooooooooooongVariable({});"); 6139 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6140 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);"); 6141 6142 // Lambdas should not confuse the variable declaration heuristic. 6143 verifyFormat("LooooooooooooooooongType\n" 6144 " variable(nullptr, [](A *a) {});", 6145 getLLVMStyleWithColumns(40)); 6146 } 6147 6148 TEST_F(FormatTest, BreaksLongDeclarations) { 6149 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n" 6150 " AnotherNameForTheLongType;"); 6151 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n" 6152 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 6153 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6154 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 6155 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n" 6156 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 6157 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6158 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6159 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n" 6160 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6161 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 6162 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6163 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 6164 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6165 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6166 "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);"); 6167 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6168 "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}"); 6169 FormatStyle Indented = getLLVMStyle(); 6170 Indented.IndentWrappedFunctionNames = true; 6171 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6172 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();", 6173 Indented); 6174 verifyFormat( 6175 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6176 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6177 Indented); 6178 verifyFormat( 6179 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 6180 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6181 Indented); 6182 verifyFormat( 6183 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 6184 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6185 Indented); 6186 6187 // FIXME: Without the comment, this breaks after "(". 6188 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n" 6189 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();", 6190 getGoogleStyle()); 6191 6192 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 6193 " int LoooooooooooooooooooongParam2) {}"); 6194 verifyFormat( 6195 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" 6196 " SourceLocation L, IdentifierIn *II,\n" 6197 " Type *T) {}"); 6198 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" 6199 "ReallyReaaallyLongFunctionName(\n" 6200 " const std::string &SomeParameter,\n" 6201 " const SomeType<string, SomeOtherTemplateParameter>\n" 6202 " &ReallyReallyLongParameterName,\n" 6203 " const SomeType<string, SomeOtherTemplateParameter>\n" 6204 " &AnotherLongParameterName) {}"); 6205 verifyFormat("template <typename A>\n" 6206 "SomeLoooooooooooooooooooooongType<\n" 6207 " typename some_namespace::SomeOtherType<A>::Type>\n" 6208 "Function() {}"); 6209 6210 verifyGoogleFormat( 6211 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" 6212 " aaaaaaaaaaaaaaaaaaaaaaa;"); 6213 verifyGoogleFormat( 6214 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" 6215 " SourceLocation L) {}"); 6216 verifyGoogleFormat( 6217 "some_namespace::LongReturnType\n" 6218 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" 6219 " int first_long_parameter, int second_parameter) {}"); 6220 6221 verifyGoogleFormat("template <typename T>\n" 6222 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" 6223 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); 6224 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6225 " int aaaaaaaaaaaaaaaaaaaaaaa);"); 6226 6227 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 6228 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6229 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6230 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6231 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 6232 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 6233 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6234 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 6235 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n" 6236 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6237 6238 verifyFormat("template <typename T> // Templates on own line.\n" 6239 "static int // Some comment.\n" 6240 "MyFunction(int a);", 6241 getLLVMStyle()); 6242 } 6243 6244 TEST_F(FormatTest, FormatsArrays) { 6245 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6246 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;"); 6247 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n" 6248 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;"); 6249 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n" 6250 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}"); 6251 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6252 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 6253 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6254 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;"); 6255 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6256 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6257 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 6258 verifyFormat( 6259 "llvm::outs() << \"aaaaaaaaaaaa: \"\n" 6260 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6261 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 6262 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n" 6263 " .aaaaaaaaaaaaaaaaaaaaaa();"); 6264 6265 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n" 6266 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];"); 6267 verifyFormat( 6268 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n" 6269 " .aaaaaaa[0]\n" 6270 " .aaaaaaaaaaaaaaaaaaaaaa();"); 6271 verifyFormat("a[::b::c];"); 6272 6273 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10)); 6274 6275 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0); 6276 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit); 6277 } 6278 6279 TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 6280 verifyFormat("(a)->b();"); 6281 verifyFormat("--a;"); 6282 } 6283 6284 TEST_F(FormatTest, HandlesIncludeDirectives) { 6285 verifyFormat("#include <string>\n" 6286 "#include <a/b/c.h>\n" 6287 "#include \"a/b/string\"\n" 6288 "#include \"string.h\"\n" 6289 "#include \"string.h\"\n" 6290 "#include <a-a>\n" 6291 "#include < path with space >\n" 6292 "#include_next <test.h>" 6293 "#include \"abc.h\" // this is included for ABC\n" 6294 "#include \"some long include\" // with a comment\n" 6295 "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"", 6296 getLLVMStyleWithColumns(35)); 6297 EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\"")); 6298 EXPECT_EQ("#include <a>", format("#include<a>")); 6299 6300 verifyFormat("#import <string>"); 6301 verifyFormat("#import <a/b/c.h>"); 6302 verifyFormat("#import \"a/b/string\""); 6303 verifyFormat("#import \"string.h\""); 6304 verifyFormat("#import \"string.h\""); 6305 verifyFormat("#if __has_include(<strstream>)\n" 6306 "#include <strstream>\n" 6307 "#endif"); 6308 6309 verifyFormat("#define MY_IMPORT <a/b>"); 6310 6311 verifyFormat("#if __has_include(<a/b>)"); 6312 verifyFormat("#if __has_include_next(<a/b>)"); 6313 verifyFormat("#define F __has_include(<a/b>)"); 6314 verifyFormat("#define F __has_include_next(<a/b>)"); 6315 6316 // Protocol buffer definition or missing "#". 6317 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";", 6318 getLLVMStyleWithColumns(30)); 6319 6320 FormatStyle Style = getLLVMStyle(); 6321 Style.AlwaysBreakBeforeMultilineStrings = true; 6322 Style.ColumnLimit = 0; 6323 verifyFormat("#import \"abc.h\"", Style); 6324 6325 // But 'import' might also be a regular C++ namespace. 6326 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6327 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6328 } 6329 6330 //===----------------------------------------------------------------------===// 6331 // Error recovery tests. 6332 //===----------------------------------------------------------------------===// 6333 6334 TEST_F(FormatTest, IncompleteParameterLists) { 6335 FormatStyle NoBinPacking = getLLVMStyle(); 6336 NoBinPacking.BinPackParameters = false; 6337 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" 6338 " double *min_x,\n" 6339 " double *max_x,\n" 6340 " double *min_y,\n" 6341 " double *max_y,\n" 6342 " double *min_z,\n" 6343 " double *max_z, ) {}", 6344 NoBinPacking); 6345 } 6346 6347 TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 6348 verifyFormat("void f() { return; }\n42"); 6349 verifyFormat("void f() {\n" 6350 " if (0)\n" 6351 " return;\n" 6352 "}\n" 6353 "42"); 6354 verifyFormat("void f() { return }\n42"); 6355 verifyFormat("void f() {\n" 6356 " if (0)\n" 6357 " return\n" 6358 "}\n" 6359 "42"); 6360 } 6361 6362 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 6363 EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); 6364 EXPECT_EQ("void f() {\n" 6365 " if (a)\n" 6366 " return\n" 6367 "}", 6368 format("void f ( ) { if ( a ) return }")); 6369 EXPECT_EQ("namespace N {\n" 6370 "void f()\n" 6371 "}", 6372 format("namespace N { void f() }")); 6373 EXPECT_EQ("namespace N {\n" 6374 "void f() {}\n" 6375 "void g()\n" 6376 "} // namespace N", 6377 format("namespace N { void f( ) { } void g( ) }")); 6378 } 6379 6380 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 6381 verifyFormat("int aaaaaaaa =\n" 6382 " // Overlylongcomment\n" 6383 " b;", 6384 getLLVMStyleWithColumns(20)); 6385 verifyFormat("function(\n" 6386 " ShortArgument,\n" 6387 " LoooooooooooongArgument);\n", 6388 getLLVMStyleWithColumns(20)); 6389 } 6390 6391 TEST_F(FormatTest, IncorrectAccessSpecifier) { 6392 verifyFormat("public:"); 6393 verifyFormat("class A {\n" 6394 "public\n" 6395 " void f() {}\n" 6396 "};"); 6397 verifyFormat("public\n" 6398 "int qwerty;"); 6399 verifyFormat("public\n" 6400 "B {}"); 6401 verifyFormat("public\n" 6402 "{}"); 6403 verifyFormat("public\n" 6404 "B { int x; }"); 6405 } 6406 6407 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 6408 verifyFormat("{"); 6409 verifyFormat("#})"); 6410 verifyNoCrash("(/**/[:!] ?[)."); 6411 } 6412 6413 TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 6414 verifyFormat("do {\n}"); 6415 verifyFormat("do {\n}\n" 6416 "f();"); 6417 verifyFormat("do {\n}\n" 6418 "wheeee(fun);"); 6419 verifyFormat("do {\n" 6420 " f();\n" 6421 "}"); 6422 } 6423 6424 TEST_F(FormatTest, IncorrectCodeMissingParens) { 6425 verifyFormat("if {\n foo;\n foo();\n}"); 6426 verifyFormat("switch {\n foo;\n foo();\n}"); 6427 verifyIncompleteFormat("for {\n foo;\n foo();\n}"); 6428 verifyFormat("while {\n foo;\n foo();\n}"); 6429 verifyFormat("do {\n foo;\n foo();\n} while;"); 6430 } 6431 6432 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 6433 verifyIncompleteFormat("namespace {\n" 6434 "class Foo { Foo (\n" 6435 "};\n" 6436 "} // namespace"); 6437 } 6438 6439 TEST_F(FormatTest, IncorrectCodeErrorDetection) { 6440 EXPECT_EQ("{\n {}\n", format("{\n{\n}\n")); 6441 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 6442 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 6443 EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n")); 6444 6445 EXPECT_EQ("{\n" 6446 " {\n" 6447 " breakme(\n" 6448 " qwe);\n" 6449 " }\n", 6450 format("{\n" 6451 " {\n" 6452 " breakme(qwe);\n" 6453 "}\n", 6454 getLLVMStyleWithColumns(10))); 6455 } 6456 6457 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 6458 verifyFormat("int x = {\n" 6459 " avariable,\n" 6460 " b(alongervariable)};", 6461 getLLVMStyleWithColumns(25)); 6462 } 6463 6464 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 6465 verifyFormat("return (a)(b){1, 2, 3};"); 6466 } 6467 6468 TEST_F(FormatTest, LayoutCxx11BraceInitializers) { 6469 verifyFormat("vector<int> x{1, 2, 3, 4};"); 6470 verifyFormat("vector<int> x{\n" 6471 " 1,\n" 6472 " 2,\n" 6473 " 3,\n" 6474 " 4,\n" 6475 "};"); 6476 verifyFormat("vector<T> x{{}, {}, {}, {}};"); 6477 verifyFormat("f({1, 2});"); 6478 verifyFormat("auto v = Foo{-1};"); 6479 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});"); 6480 verifyFormat("Class::Class : member{1, 2, 3} {}"); 6481 verifyFormat("new vector<int>{1, 2, 3};"); 6482 verifyFormat("new int[3]{1, 2, 3};"); 6483 verifyFormat("new int{1};"); 6484 verifyFormat("return {arg1, arg2};"); 6485 verifyFormat("return {arg1, SomeType{parameter}};"); 6486 verifyFormat("int count = set<int>{f(), g(), h()}.size();"); 6487 verifyFormat("new T{arg1, arg2};"); 6488 verifyFormat("f(MyMap[{composite, key}]);"); 6489 verifyFormat("class Class {\n" 6490 " T member = {arg1, arg2};\n" 6491 "};"); 6492 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};"); 6493 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 6494 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 6495 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");"); 6496 verifyFormat("int a = std::is_integral<int>{} + 0;"); 6497 6498 verifyFormat("int foo(int i) { return fo1{}(i); }"); 6499 verifyFormat("int foo(int i) { return fo1{}(i); }"); 6500 verifyFormat("auto i = decltype(x){};"); 6501 verifyFormat("std::vector<int> v = {1, 0 /* comment */};"); 6502 verifyFormat("Node n{1, Node{1000}, //\n" 6503 " 2};"); 6504 verifyFormat("Aaaa aaaaaaa{\n" 6505 " {\n" 6506 " aaaa,\n" 6507 " },\n" 6508 "};"); 6509 verifyFormat("class C : public D {\n" 6510 " SomeClass SC{2};\n" 6511 "};"); 6512 verifyFormat("class C : public A {\n" 6513 " class D : public B {\n" 6514 " void f() { int i{2}; }\n" 6515 " };\n" 6516 "};"); 6517 verifyFormat("#define A {a, a},"); 6518 6519 // Binpacking only if there is no trailing comma 6520 verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n" 6521 " cccccccccc, dddddddddd};", 6522 getLLVMStyleWithColumns(50)); 6523 verifyFormat("const Aaaaaa aaaaa = {\n" 6524 " aaaaaaaaaaa,\n" 6525 " bbbbbbbbbbb,\n" 6526 " ccccccccccc,\n" 6527 " ddddddddddd,\n" 6528 "};", getLLVMStyleWithColumns(50)); 6529 6530 // Cases where distinguising braced lists and blocks is hard. 6531 verifyFormat("vector<int> v{12} GUARDED_BY(mutex);"); 6532 verifyFormat("void f() {\n" 6533 " return; // comment\n" 6534 "}\n" 6535 "SomeType t;"); 6536 verifyFormat("void f() {\n" 6537 " if (a) {\n" 6538 " f();\n" 6539 " }\n" 6540 "}\n" 6541 "SomeType t;"); 6542 6543 // In combination with BinPackArguments = false. 6544 FormatStyle NoBinPacking = getLLVMStyle(); 6545 NoBinPacking.BinPackArguments = false; 6546 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n" 6547 " bbbbb,\n" 6548 " ccccc,\n" 6549 " ddddd,\n" 6550 " eeeee,\n" 6551 " ffffff,\n" 6552 " ggggg,\n" 6553 " hhhhhh,\n" 6554 " iiiiii,\n" 6555 " jjjjjj,\n" 6556 " kkkkkk};", 6557 NoBinPacking); 6558 verifyFormat("const Aaaaaa aaaaa = {\n" 6559 " aaaaa,\n" 6560 " bbbbb,\n" 6561 " ccccc,\n" 6562 " ddddd,\n" 6563 " eeeee,\n" 6564 " ffffff,\n" 6565 " ggggg,\n" 6566 " hhhhhh,\n" 6567 " iiiiii,\n" 6568 " jjjjjj,\n" 6569 " kkkkkk,\n" 6570 "};", 6571 NoBinPacking); 6572 verifyFormat( 6573 "const Aaaaaa aaaaa = {\n" 6574 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n" 6575 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n" 6576 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n" 6577 "};", 6578 NoBinPacking); 6579 6580 // FIXME: The alignment of these trailing comments might be bad. Then again, 6581 // this might be utterly useless in real code. 6582 verifyFormat("Constructor::Constructor()\n" 6583 " : some_value{ //\n" 6584 " aaaaaaa, //\n" 6585 " bbbbbbb} {}"); 6586 6587 // In braced lists, the first comment is always assumed to belong to the 6588 // first element. Thus, it can be moved to the next or previous line as 6589 // appropriate. 6590 EXPECT_EQ("function({// First element:\n" 6591 " 1,\n" 6592 " // Second element:\n" 6593 " 2});", 6594 format("function({\n" 6595 " // First element:\n" 6596 " 1,\n" 6597 " // Second element:\n" 6598 " 2});")); 6599 EXPECT_EQ("std::vector<int> MyNumbers{\n" 6600 " // First element:\n" 6601 " 1,\n" 6602 " // Second element:\n" 6603 " 2};", 6604 format("std::vector<int> MyNumbers{// First element:\n" 6605 " 1,\n" 6606 " // Second element:\n" 6607 " 2};", 6608 getLLVMStyleWithColumns(30))); 6609 // A trailing comma should still lead to an enforced line break and no 6610 // binpacking. 6611 EXPECT_EQ("vector<int> SomeVector = {\n" 6612 " // aaa\n" 6613 " 1,\n" 6614 " 2,\n" 6615 "};", 6616 format("vector<int> SomeVector = { // aaa\n" 6617 " 1, 2, };")); 6618 6619 FormatStyle ExtraSpaces = getLLVMStyle(); 6620 ExtraSpaces.Cpp11BracedListStyle = false; 6621 ExtraSpaces.ColumnLimit = 75; 6622 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces); 6623 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces); 6624 verifyFormat("f({ 1, 2 });", ExtraSpaces); 6625 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces); 6626 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces); 6627 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces); 6628 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces); 6629 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces); 6630 verifyFormat("return { arg1, arg2 };", ExtraSpaces); 6631 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces); 6632 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces); 6633 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces); 6634 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces); 6635 verifyFormat("class Class {\n" 6636 " T member = { arg1, arg2 };\n" 6637 "};", 6638 ExtraSpaces); 6639 verifyFormat( 6640 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6641 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n" 6642 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 6643 " bbbbbbbbbbbbbbbbbbbb, bbbbb };", 6644 ExtraSpaces); 6645 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces); 6646 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });", 6647 ExtraSpaces); 6648 verifyFormat( 6649 "someFunction(OtherParam,\n" 6650 " BracedList{ // comment 1 (Forcing interesting break)\n" 6651 " param1, param2,\n" 6652 " // comment 2\n" 6653 " param3, param4 });", 6654 ExtraSpaces); 6655 verifyFormat( 6656 "std::this_thread::sleep_for(\n" 6657 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);", 6658 ExtraSpaces); 6659 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n" 6660 " aaaaaaa,\n" 6661 " aaaaaaaaaa,\n" 6662 " aaaaa,\n" 6663 " aaaaaaaaaaaaaaa,\n" 6664 " aaa,\n" 6665 " aaaaaaaaaa,\n" 6666 " a,\n" 6667 " aaaaaaaaaaaaaaaaaaaaa,\n" 6668 " aaaaaaaaaaaa,\n" 6669 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n" 6670 " aaaaaaa,\n" 6671 " a};"); 6672 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces); 6673 verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces); 6674 verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces); 6675 } 6676 6677 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { 6678 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6679 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6680 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6681 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6682 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6683 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 6684 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n" 6685 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6686 " 1, 22, 333, 4444, 55555, //\n" 6687 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6688 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 6689 verifyFormat( 6690 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6691 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6692 " 1, 22, 333, 4444, 55555, 666666, // comment\n" 6693 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6694 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6695 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6696 " 7777777};"); 6697 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6698 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6699 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6700 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6701 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6702 " // Separating comment.\n" 6703 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6704 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6705 " // Leading comment\n" 6706 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6707 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6708 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6709 " 1, 1, 1, 1};", 6710 getLLVMStyleWithColumns(39)); 6711 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6712 " 1, 1, 1, 1};", 6713 getLLVMStyleWithColumns(38)); 6714 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n" 6715 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};", 6716 getLLVMStyleWithColumns(43)); 6717 verifyFormat( 6718 "static unsigned SomeValues[10][3] = {\n" 6719 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n" 6720 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};"); 6721 verifyFormat("static auto fields = new vector<string>{\n" 6722 " \"aaaaaaaaaaaaa\",\n" 6723 " \"aaaaaaaaaaaaa\",\n" 6724 " \"aaaaaaaaaaaa\",\n" 6725 " \"aaaaaaaaaaaaaa\",\n" 6726 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 6727 " \"aaaaaaaaaaaa\",\n" 6728 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 6729 "};"); 6730 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};"); 6731 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n" 6732 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n" 6733 " 3, cccccccccccccccccccccc};", 6734 getLLVMStyleWithColumns(60)); 6735 6736 // Trailing commas. 6737 verifyFormat("vector<int> x = {\n" 6738 " 1, 1, 1, 1, 1, 1, 1, 1,\n" 6739 "};", 6740 getLLVMStyleWithColumns(39)); 6741 verifyFormat("vector<int> x = {\n" 6742 " 1, 1, 1, 1, 1, 1, 1, 1, //\n" 6743 "};", 6744 getLLVMStyleWithColumns(39)); 6745 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6746 " 1, 1, 1, 1,\n" 6747 " /**/ /**/};", 6748 getLLVMStyleWithColumns(39)); 6749 6750 // Trailing comment in the first line. 6751 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n" 6752 " 1111111111, 2222222222, 33333333333, 4444444444, //\n" 6753 " 111111111, 222222222, 3333333333, 444444444, //\n" 6754 " 11111111, 22222222, 333333333, 44444444};"); 6755 // Trailing comment in the last line. 6756 verifyFormat("int aaaaa[] = {\n" 6757 " 1, 2, 3, // comment\n" 6758 " 4, 5, 6 // comment\n" 6759 "};"); 6760 6761 // With nested lists, we should either format one item per line or all nested 6762 // lists one on line. 6763 // FIXME: For some nested lists, we can do better. 6764 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n" 6765 " {aaaaaaaaaaaaaaaaaaa},\n" 6766 " {aaaaaaaaaaaaaaaaaaaaa},\n" 6767 " {aaaaaaaaaaaaaaaaa}};", 6768 getLLVMStyleWithColumns(60)); 6769 verifyFormat( 6770 "SomeStruct my_struct_array = {\n" 6771 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n" 6772 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n" 6773 " {aaa, aaa},\n" 6774 " {aaa, aaa},\n" 6775 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n" 6776 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6777 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};"); 6778 6779 // No column layout should be used here. 6780 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n" 6781 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};"); 6782 6783 verifyNoCrash("a<,"); 6784 6785 // No braced initializer here. 6786 verifyFormat("void f() {\n" 6787 " struct Dummy {};\n" 6788 " f(v);\n" 6789 "}"); 6790 6791 // Long lists should be formatted in columns even if they are nested. 6792 verifyFormat( 6793 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6794 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6795 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6796 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6797 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6798 " 1, 22, 333, 4444, 55555, 666666, 7777777});"); 6799 6800 // Allow "single-column" layout even if that violates the column limit. There 6801 // isn't going to be a better way. 6802 verifyFormat("std::vector<int> a = {\n" 6803 " aaaaaaaa,\n" 6804 " aaaaaaaa,\n" 6805 " aaaaaaaa,\n" 6806 " aaaaaaaa,\n" 6807 " aaaaaaaaaa,\n" 6808 " aaaaaaaa,\n" 6809 " aaaaaaaaaaaaaaaaaaaaaaaaaaa};", 6810 getLLVMStyleWithColumns(30)); 6811 verifyFormat("vector<int> aaaa = {\n" 6812 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6813 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6814 " aaaaaa.aaaaaaa,\n" 6815 " aaaaaa.aaaaaaa,\n" 6816 " aaaaaa.aaaaaaa,\n" 6817 " aaaaaa.aaaaaaa,\n" 6818 "};"); 6819 6820 // Don't create hanging lists. 6821 verifyFormat("someFunction(Param, {List1, List2,\n" 6822 " List3});", 6823 getLLVMStyleWithColumns(35)); 6824 verifyFormat("someFunction(Param, Param,\n" 6825 " {List1, List2,\n" 6826 " List3});", 6827 getLLVMStyleWithColumns(35)); 6828 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n" 6829 " aaaaaaaaaaaaaaaaaaaaaaa);"); 6830 } 6831 6832 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 6833 FormatStyle DoNotMerge = getLLVMStyle(); 6834 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 6835 6836 verifyFormat("void f() { return 42; }"); 6837 verifyFormat("void f() {\n" 6838 " return 42;\n" 6839 "}", 6840 DoNotMerge); 6841 verifyFormat("void f() {\n" 6842 " // Comment\n" 6843 "}"); 6844 verifyFormat("{\n" 6845 "#error {\n" 6846 " int a;\n" 6847 "}"); 6848 verifyFormat("{\n" 6849 " int a;\n" 6850 "#error {\n" 6851 "}"); 6852 verifyFormat("void f() {} // comment"); 6853 verifyFormat("void f() { int a; } // comment"); 6854 verifyFormat("void f() {\n" 6855 "} // comment", 6856 DoNotMerge); 6857 verifyFormat("void f() {\n" 6858 " int a;\n" 6859 "} // comment", 6860 DoNotMerge); 6861 verifyFormat("void f() {\n" 6862 "} // comment", 6863 getLLVMStyleWithColumns(15)); 6864 6865 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 6866 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 6867 6868 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 6869 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 6870 verifyFormat("class C {\n" 6871 " C()\n" 6872 " : iiiiiiii(nullptr),\n" 6873 " kkkkkkk(nullptr),\n" 6874 " mmmmmmm(nullptr),\n" 6875 " nnnnnnn(nullptr) {}\n" 6876 "};", 6877 getGoogleStyle()); 6878 6879 FormatStyle NoColumnLimit = getLLVMStyle(); 6880 NoColumnLimit.ColumnLimit = 0; 6881 EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit)); 6882 EXPECT_EQ("class C {\n" 6883 " A() : b(0) {}\n" 6884 "};", 6885 format("class C{A():b(0){}};", NoColumnLimit)); 6886 EXPECT_EQ("A()\n" 6887 " : b(0) {\n" 6888 "}", 6889 format("A()\n:b(0)\n{\n}", NoColumnLimit)); 6890 6891 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit; 6892 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine = 6893 FormatStyle::SFS_None; 6894 EXPECT_EQ("A()\n" 6895 " : b(0) {\n" 6896 "}", 6897 format("A():b(0){}", DoNotMergeNoColumnLimit)); 6898 EXPECT_EQ("A()\n" 6899 " : b(0) {\n" 6900 "}", 6901 format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit)); 6902 6903 verifyFormat("#define A \\\n" 6904 " void f() { \\\n" 6905 " int i; \\\n" 6906 " }", 6907 getLLVMStyleWithColumns(20)); 6908 verifyFormat("#define A \\\n" 6909 " void f() { int i; }", 6910 getLLVMStyleWithColumns(21)); 6911 verifyFormat("#define A \\\n" 6912 " void f() { \\\n" 6913 " int i; \\\n" 6914 " } \\\n" 6915 " int j;", 6916 getLLVMStyleWithColumns(22)); 6917 verifyFormat("#define A \\\n" 6918 " void f() { int i; } \\\n" 6919 " int j;", 6920 getLLVMStyleWithColumns(23)); 6921 } 6922 6923 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) { 6924 FormatStyle MergeEmptyOnly = getLLVMStyle(); 6925 MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 6926 verifyFormat("class C {\n" 6927 " int f() {}\n" 6928 "};", 6929 MergeEmptyOnly); 6930 verifyFormat("class C {\n" 6931 " int f() {\n" 6932 " return 42;\n" 6933 " }\n" 6934 "};", 6935 MergeEmptyOnly); 6936 verifyFormat("int f() {}", MergeEmptyOnly); 6937 verifyFormat("int f() {\n" 6938 " return 42;\n" 6939 "}", 6940 MergeEmptyOnly); 6941 6942 // Also verify behavior when BraceWrapping.AfterFunction = true 6943 MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 6944 MergeEmptyOnly.BraceWrapping.AfterFunction = true; 6945 verifyFormat("int f() {}", MergeEmptyOnly); 6946 verifyFormat("class C {\n" 6947 " int f() {}\n" 6948 "};", 6949 MergeEmptyOnly); 6950 } 6951 6952 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) { 6953 FormatStyle MergeInlineOnly = getLLVMStyle(); 6954 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 6955 verifyFormat("class C {\n" 6956 " int f() { return 42; }\n" 6957 "};", 6958 MergeInlineOnly); 6959 verifyFormat("int f() {\n" 6960 " return 42;\n" 6961 "}", 6962 MergeInlineOnly); 6963 6964 // SFS_Inline implies SFS_Empty 6965 verifyFormat("class C {\n" 6966 " int f() {}\n" 6967 "};", 6968 MergeInlineOnly); 6969 verifyFormat("int f() {}", MergeInlineOnly); 6970 6971 // Also verify behavior when BraceWrapping.AfterFunction = true 6972 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 6973 MergeInlineOnly.BraceWrapping.AfterFunction = true; 6974 verifyFormat("class C {\n" 6975 " int f() { return 42; }\n" 6976 "};", 6977 MergeInlineOnly); 6978 verifyFormat("int f()\n" 6979 "{\n" 6980 " return 42;\n" 6981 "}", 6982 MergeInlineOnly); 6983 6984 // SFS_Inline implies SFS_Empty 6985 verifyFormat("int f() {}", MergeInlineOnly); 6986 verifyFormat("class C {\n" 6987 " int f() {}\n" 6988 "};", 6989 MergeInlineOnly); 6990 } 6991 6992 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) { 6993 FormatStyle MergeInlineOnly = getLLVMStyle(); 6994 MergeInlineOnly.AllowShortFunctionsOnASingleLine = 6995 FormatStyle::SFS_InlineOnly; 6996 verifyFormat("class C {\n" 6997 " int f() { return 42; }\n" 6998 "};", 6999 MergeInlineOnly); 7000 verifyFormat("int f() {\n" 7001 " return 42;\n" 7002 "}", 7003 MergeInlineOnly); 7004 7005 // SFS_InlineOnly does not imply SFS_Empty 7006 verifyFormat("class C {\n" 7007 " int f() {}\n" 7008 "};", 7009 MergeInlineOnly); 7010 verifyFormat("int f() {\n" 7011 "}", 7012 MergeInlineOnly); 7013 7014 // Also verify behavior when BraceWrapping.AfterFunction = true 7015 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 7016 MergeInlineOnly.BraceWrapping.AfterFunction = true; 7017 verifyFormat("class C {\n" 7018 " int f() { return 42; }\n" 7019 "};", 7020 MergeInlineOnly); 7021 verifyFormat("int f()\n" 7022 "{\n" 7023 " return 42;\n" 7024 "}", 7025 MergeInlineOnly); 7026 7027 // SFS_InlineOnly does not imply SFS_Empty 7028 verifyFormat("int f()\n" 7029 "{\n" 7030 "}", 7031 MergeInlineOnly); 7032 verifyFormat("class C {\n" 7033 " int f() {}\n" 7034 "};", 7035 MergeInlineOnly); 7036 } 7037 7038 TEST_F(FormatTest, SplitEmptyFunction) { 7039 FormatStyle Style = getLLVMStyle(); 7040 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 7041 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7042 Style.BraceWrapping.AfterFunction = true; 7043 Style.BraceWrapping.SplitEmptyFunction = false; 7044 Style.ColumnLimit = 40; 7045 7046 verifyFormat("int f()\n" 7047 "{}", 7048 Style); 7049 verifyFormat("int f()\n" 7050 "{\n" 7051 " return 42;\n" 7052 "}", 7053 Style); 7054 verifyFormat("int f()\n" 7055 "{\n" 7056 " // some comment\n" 7057 "}", 7058 Style); 7059 7060 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 7061 verifyFormat("int f() {}", Style); 7062 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 7063 "{}", 7064 Style); 7065 verifyFormat("int f()\n" 7066 "{\n" 7067 " return 0;\n" 7068 "}", 7069 Style); 7070 7071 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 7072 verifyFormat("class Foo {\n" 7073 " int f() {}\n" 7074 "};\n", 7075 Style); 7076 verifyFormat("class Foo {\n" 7077 " int f() { return 0; }\n" 7078 "};\n", 7079 Style); 7080 verifyFormat("class Foo {\n" 7081 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 7082 " {}\n" 7083 "};\n", 7084 Style); 7085 verifyFormat("class Foo {\n" 7086 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 7087 " {\n" 7088 " return 0;\n" 7089 " }\n" 7090 "};\n", 7091 Style); 7092 7093 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 7094 verifyFormat("int f() {}", Style); 7095 verifyFormat("int f() { return 0; }", Style); 7096 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 7097 "{}", 7098 Style); 7099 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 7100 "{\n" 7101 " return 0;\n" 7102 "}", 7103 Style); 7104 } 7105 7106 TEST_F(FormatTest, SplitEmptyClass) { 7107 FormatStyle Style = getLLVMStyle(); 7108 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7109 Style.BraceWrapping.AfterClass = true; 7110 Style.BraceWrapping.SplitEmptyRecord = false; 7111 7112 verifyFormat("class Foo\n" 7113 "{};", 7114 Style); 7115 verifyFormat("/* something */ class Foo\n" 7116 "{};", 7117 Style); 7118 verifyFormat("template <typename X> class Foo\n" 7119 "{};", 7120 Style); 7121 verifyFormat("class Foo\n" 7122 "{\n" 7123 " Foo();\n" 7124 "};", 7125 Style); 7126 verifyFormat("typedef class Foo\n" 7127 "{\n" 7128 "} Foo_t;", 7129 Style); 7130 } 7131 7132 TEST_F(FormatTest, SplitEmptyStruct) { 7133 FormatStyle Style = getLLVMStyle(); 7134 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7135 Style.BraceWrapping.AfterStruct = true; 7136 Style.BraceWrapping.SplitEmptyRecord = false; 7137 7138 verifyFormat("struct Foo\n" 7139 "{};", 7140 Style); 7141 verifyFormat("/* something */ struct Foo\n" 7142 "{};", 7143 Style); 7144 verifyFormat("template <typename X> struct Foo\n" 7145 "{};", 7146 Style); 7147 verifyFormat("struct Foo\n" 7148 "{\n" 7149 " Foo();\n" 7150 "};", 7151 Style); 7152 verifyFormat("typedef struct Foo\n" 7153 "{\n" 7154 "} Foo_t;", 7155 Style); 7156 //typedef struct Bar {} Bar_t; 7157 } 7158 7159 TEST_F(FormatTest, SplitEmptyUnion) { 7160 FormatStyle Style = getLLVMStyle(); 7161 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7162 Style.BraceWrapping.AfterUnion = true; 7163 Style.BraceWrapping.SplitEmptyRecord = false; 7164 7165 verifyFormat("union Foo\n" 7166 "{};", 7167 Style); 7168 verifyFormat("/* something */ union Foo\n" 7169 "{};", 7170 Style); 7171 verifyFormat("union Foo\n" 7172 "{\n" 7173 " A,\n" 7174 "};", 7175 Style); 7176 verifyFormat("typedef union Foo\n" 7177 "{\n" 7178 "} Foo_t;", 7179 Style); 7180 } 7181 7182 TEST_F(FormatTest, SplitEmptyNamespace) { 7183 FormatStyle Style = getLLVMStyle(); 7184 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7185 Style.BraceWrapping.AfterNamespace = true; 7186 Style.BraceWrapping.SplitEmptyNamespace = false; 7187 7188 verifyFormat("namespace Foo\n" 7189 "{};", 7190 Style); 7191 verifyFormat("/* something */ namespace Foo\n" 7192 "{};", 7193 Style); 7194 verifyFormat("inline namespace Foo\n" 7195 "{};", 7196 Style); 7197 verifyFormat("namespace Foo\n" 7198 "{\n" 7199 "void Bar();\n" 7200 "};", 7201 Style); 7202 } 7203 7204 TEST_F(FormatTest, NeverMergeShortRecords) { 7205 FormatStyle Style = getLLVMStyle(); 7206 7207 verifyFormat("class Foo {\n" 7208 " Foo();\n" 7209 "};", 7210 Style); 7211 verifyFormat("typedef class Foo {\n" 7212 " Foo();\n" 7213 "} Foo_t;", 7214 Style); 7215 verifyFormat("struct Foo {\n" 7216 " Foo();\n" 7217 "};", 7218 Style); 7219 verifyFormat("typedef struct Foo {\n" 7220 " Foo();\n" 7221 "} Foo_t;", 7222 Style); 7223 verifyFormat("union Foo {\n" 7224 " A,\n" 7225 "};", 7226 Style); 7227 verifyFormat("typedef union Foo {\n" 7228 " A,\n" 7229 "} Foo_t;", 7230 Style); 7231 verifyFormat("namespace Foo {\n" 7232 "void Bar();\n" 7233 "};", 7234 Style); 7235 7236 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7237 Style.BraceWrapping.AfterClass = true; 7238 Style.BraceWrapping.AfterStruct = true; 7239 Style.BraceWrapping.AfterUnion = true; 7240 Style.BraceWrapping.AfterNamespace = true; 7241 verifyFormat("class Foo\n" 7242 "{\n" 7243 " Foo();\n" 7244 "};", 7245 Style); 7246 verifyFormat("typedef class Foo\n" 7247 "{\n" 7248 " Foo();\n" 7249 "} Foo_t;", 7250 Style); 7251 verifyFormat("struct Foo\n" 7252 "{\n" 7253 " Foo();\n" 7254 "};", 7255 Style); 7256 verifyFormat("typedef struct Foo\n" 7257 "{\n" 7258 " Foo();\n" 7259 "} Foo_t;", 7260 Style); 7261 verifyFormat("union Foo\n" 7262 "{\n" 7263 " A,\n" 7264 "};", 7265 Style); 7266 verifyFormat("typedef union Foo\n" 7267 "{\n" 7268 " A,\n" 7269 "} Foo_t;", 7270 Style); 7271 verifyFormat("namespace Foo\n" 7272 "{\n" 7273 "void Bar();\n" 7274 "};", 7275 Style); 7276 } 7277 7278 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 7279 // Elaborate type variable declarations. 7280 verifyFormat("struct foo a = {bar};\nint n;"); 7281 verifyFormat("class foo a = {bar};\nint n;"); 7282 verifyFormat("union foo a = {bar};\nint n;"); 7283 7284 // Elaborate types inside function definitions. 7285 verifyFormat("struct foo f() {}\nint n;"); 7286 verifyFormat("class foo f() {}\nint n;"); 7287 verifyFormat("union foo f() {}\nint n;"); 7288 7289 // Templates. 7290 verifyFormat("template <class X> void f() {}\nint n;"); 7291 verifyFormat("template <struct X> void f() {}\nint n;"); 7292 verifyFormat("template <union X> void f() {}\nint n;"); 7293 7294 // Actual definitions... 7295 verifyFormat("struct {\n} n;"); 7296 verifyFormat( 7297 "template <template <class T, class Y>, class Z> class X {\n} n;"); 7298 verifyFormat("union Z {\n int n;\n} x;"); 7299 verifyFormat("class MACRO Z {\n} n;"); 7300 verifyFormat("class MACRO(X) Z {\n} n;"); 7301 verifyFormat("class __attribute__(X) Z {\n} n;"); 7302 verifyFormat("class __declspec(X) Z {\n} n;"); 7303 verifyFormat("class A##B##C {\n} n;"); 7304 verifyFormat("class alignas(16) Z {\n} n;"); 7305 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;"); 7306 verifyFormat("class MACROA MACRO(X) Z {\n} n;"); 7307 7308 // Redefinition from nested context: 7309 verifyFormat("class A::B::C {\n} n;"); 7310 7311 // Template definitions. 7312 verifyFormat( 7313 "template <typename F>\n" 7314 "Matcher(const Matcher<F> &Other,\n" 7315 " typename enable_if_c<is_base_of<F, T>::value &&\n" 7316 " !is_same<F, T>::value>::type * = 0)\n" 7317 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}"); 7318 7319 // FIXME: This is still incorrectly handled at the formatter side. 7320 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};"); 7321 verifyFormat("int i = SomeFunction(a<b, a> b);"); 7322 7323 // FIXME: 7324 // This now gets parsed incorrectly as class definition. 7325 // verifyFormat("class A<int> f() {\n}\nint n;"); 7326 7327 // Elaborate types where incorrectly parsing the structural element would 7328 // break the indent. 7329 verifyFormat("if (true)\n" 7330 " class X x;\n" 7331 "else\n" 7332 " f();\n"); 7333 7334 // This is simply incomplete. Formatting is not important, but must not crash. 7335 verifyFormat("class A:"); 7336 } 7337 7338 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 7339 EXPECT_EQ("#error Leave all white!!!!! space* alone!\n", 7340 format("#error Leave all white!!!!! space* alone!\n")); 7341 EXPECT_EQ( 7342 "#warning Leave all white!!!!! space* alone!\n", 7343 format("#warning Leave all white!!!!! space* alone!\n")); 7344 EXPECT_EQ("#error 1", format(" # error 1")); 7345 EXPECT_EQ("#warning 1", format(" # warning 1")); 7346 } 7347 7348 TEST_F(FormatTest, FormatHashIfExpressions) { 7349 verifyFormat("#if AAAA && BBBB"); 7350 verifyFormat("#if (AAAA && BBBB)"); 7351 verifyFormat("#elif (AAAA && BBBB)"); 7352 // FIXME: Come up with a better indentation for #elif. 7353 verifyFormat( 7354 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" 7355 " defined(BBBBBBBB)\n" 7356 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" 7357 " defined(BBBBBBBB)\n" 7358 "#endif", 7359 getLLVMStyleWithColumns(65)); 7360 } 7361 7362 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 7363 FormatStyle AllowsMergedIf = getGoogleStyle(); 7364 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 7365 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 7366 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 7367 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 7368 EXPECT_EQ("if (true) return 42;", 7369 format("if (true)\nreturn 42;", AllowsMergedIf)); 7370 FormatStyle ShortMergedIf = AllowsMergedIf; 7371 ShortMergedIf.ColumnLimit = 25; 7372 verifyFormat("#define A \\\n" 7373 " if (true) return 42;", 7374 ShortMergedIf); 7375 verifyFormat("#define A \\\n" 7376 " f(); \\\n" 7377 " if (true)\n" 7378 "#define B", 7379 ShortMergedIf); 7380 verifyFormat("#define A \\\n" 7381 " f(); \\\n" 7382 " if (true)\n" 7383 "g();", 7384 ShortMergedIf); 7385 verifyFormat("{\n" 7386 "#ifdef A\n" 7387 " // Comment\n" 7388 " if (true) continue;\n" 7389 "#endif\n" 7390 " // Comment\n" 7391 " if (true) continue;\n" 7392 "}", 7393 ShortMergedIf); 7394 ShortMergedIf.ColumnLimit = 33; 7395 verifyFormat("#define A \\\n" 7396 " if constexpr (true) return 42;", 7397 ShortMergedIf); 7398 ShortMergedIf.ColumnLimit = 29; 7399 verifyFormat("#define A \\\n" 7400 " if (aaaaaaaaaa) return 1; \\\n" 7401 " return 2;", 7402 ShortMergedIf); 7403 ShortMergedIf.ColumnLimit = 28; 7404 verifyFormat("#define A \\\n" 7405 " if (aaaaaaaaaa) \\\n" 7406 " return 1; \\\n" 7407 " return 2;", 7408 ShortMergedIf); 7409 verifyFormat("#define A \\\n" 7410 " if constexpr (aaaaaaa) \\\n" 7411 " return 1; \\\n" 7412 " return 2;", 7413 ShortMergedIf); 7414 } 7415 7416 TEST_F(FormatTest, FormatStarDependingOnContext) { 7417 verifyFormat("void f(int *a);"); 7418 verifyFormat("void f() { f(fint * b); }"); 7419 verifyFormat("class A {\n void f(int *a);\n};"); 7420 verifyFormat("class A {\n int *a;\n};"); 7421 verifyFormat("namespace a {\n" 7422 "namespace b {\n" 7423 "class A {\n" 7424 " void f() {}\n" 7425 " int *a;\n" 7426 "};\n" 7427 "} // namespace b\n" 7428 "} // namespace a"); 7429 } 7430 7431 TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 7432 verifyFormat("while"); 7433 verifyFormat("operator"); 7434 } 7435 7436 TEST_F(FormatTest, SkipsDeeplyNestedLines) { 7437 // This code would be painfully slow to format if we didn't skip it. 7438 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 7439 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 7440 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 7441 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 7442 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 7443 "A(1, 1)\n" 7444 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x 7445 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7446 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7447 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7448 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7449 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7450 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7451 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7452 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7453 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n"); 7454 // Deeply nested part is untouched, rest is formatted. 7455 EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n", 7456 format(std::string("int i;\n") + Code + "int j;\n", 7457 getLLVMStyle(), SC_ExpectIncomplete)); 7458 } 7459 7460 //===----------------------------------------------------------------------===// 7461 // Objective-C tests. 7462 //===----------------------------------------------------------------------===// 7463 7464 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 7465 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 7466 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 7467 format("-(NSUInteger)indexOfObject:(id)anObject;")); 7468 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 7469 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 7470 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 7471 format("-(NSInteger)Method3:(id)anObject;")); 7472 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 7473 format("-(NSInteger)Method4:(id)anObject;")); 7474 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 7475 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 7476 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 7477 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 7478 EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject " 7479 "forAllCells:(BOOL)flag;", 7480 format("- (void)sendAction:(SEL)aSelector to:(id)anObject " 7481 "forAllCells:(BOOL)flag;")); 7482 7483 // Very long objectiveC method declaration. 7484 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 7485 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;"); 7486 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 7487 " inRange:(NSRange)range\n" 7488 " outRange:(NSRange)out_range\n" 7489 " outRange1:(NSRange)out_range1\n" 7490 " outRange2:(NSRange)out_range2\n" 7491 " outRange3:(NSRange)out_range3\n" 7492 " outRange4:(NSRange)out_range4\n" 7493 " outRange5:(NSRange)out_range5\n" 7494 " outRange6:(NSRange)out_range6\n" 7495 " outRange7:(NSRange)out_range7\n" 7496 " outRange8:(NSRange)out_range8\n" 7497 " outRange9:(NSRange)out_range9;"); 7498 7499 // When the function name has to be wrapped. 7500 FormatStyle Style = getLLVMStyle(); 7501 Style.IndentWrappedFunctionNames = false; 7502 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 7503 "veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 7504 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 7505 "}", 7506 Style); 7507 Style.IndentWrappedFunctionNames = true; 7508 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 7509 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 7510 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 7511 "}", 7512 Style); 7513 7514 verifyFormat("- (int)sum:(vector<int>)numbers;"); 7515 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 7516 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 7517 // protocol lists (but not for template classes): 7518 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 7519 7520 verifyFormat("- (int (*)())foo:(int (*)())f;"); 7521 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); 7522 7523 // If there's no return type (very rare in practice!), LLVM and Google style 7524 // agree. 7525 verifyFormat("- foo;"); 7526 verifyFormat("- foo:(int)f;"); 7527 verifyGoogleFormat("- foo:(int)foo;"); 7528 } 7529 7530 7531 TEST_F(FormatTest, BreaksStringLiterals) { 7532 EXPECT_EQ("\"some text \"\n" 7533 "\"other\";", 7534 format("\"some text other\";", getLLVMStyleWithColumns(12))); 7535 EXPECT_EQ("\"some text \"\n" 7536 "\"other\";", 7537 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); 7538 EXPECT_EQ( 7539 "#define A \\\n" 7540 " \"some \" \\\n" 7541 " \"text \" \\\n" 7542 " \"other\";", 7543 format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); 7544 EXPECT_EQ( 7545 "#define A \\\n" 7546 " \"so \" \\\n" 7547 " \"text \" \\\n" 7548 " \"other\";", 7549 format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); 7550 7551 EXPECT_EQ("\"some text\"", 7552 format("\"some text\"", getLLVMStyleWithColumns(1))); 7553 EXPECT_EQ("\"some text\"", 7554 format("\"some text\"", getLLVMStyleWithColumns(11))); 7555 EXPECT_EQ("\"some \"\n" 7556 "\"text\"", 7557 format("\"some text\"", getLLVMStyleWithColumns(10))); 7558 EXPECT_EQ("\"some \"\n" 7559 "\"text\"", 7560 format("\"some text\"", getLLVMStyleWithColumns(7))); 7561 EXPECT_EQ("\"some\"\n" 7562 "\" tex\"\n" 7563 "\"t\"", 7564 format("\"some text\"", getLLVMStyleWithColumns(6))); 7565 EXPECT_EQ("\"some\"\n" 7566 "\" tex\"\n" 7567 "\" and\"", 7568 format("\"some tex and\"", getLLVMStyleWithColumns(6))); 7569 EXPECT_EQ("\"some\"\n" 7570 "\"/tex\"\n" 7571 "\"/and\"", 7572 format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); 7573 7574 EXPECT_EQ("variable =\n" 7575 " \"long string \"\n" 7576 " \"literal\";", 7577 format("variable = \"long string literal\";", 7578 getLLVMStyleWithColumns(20))); 7579 7580 EXPECT_EQ("variable = f(\n" 7581 " \"long string \"\n" 7582 " \"literal\",\n" 7583 " short,\n" 7584 " loooooooooooooooooooong);", 7585 format("variable = f(\"long string literal\", short, " 7586 "loooooooooooooooooooong);", 7587 getLLVMStyleWithColumns(20))); 7588 7589 EXPECT_EQ( 7590 "f(g(\"long string \"\n" 7591 " \"literal\"),\n" 7592 " b);", 7593 format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20))); 7594 EXPECT_EQ("f(g(\"long string \"\n" 7595 " \"literal\",\n" 7596 " a),\n" 7597 " b);", 7598 format("f(g(\"long string literal\", a), b);", 7599 getLLVMStyleWithColumns(20))); 7600 EXPECT_EQ( 7601 "f(\"one two\".split(\n" 7602 " variable));", 7603 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); 7604 EXPECT_EQ("f(\"one two three four five six \"\n" 7605 " \"seven\".split(\n" 7606 " really_looooong_variable));", 7607 format("f(\"one two three four five six seven\"." 7608 "split(really_looooong_variable));", 7609 getLLVMStyleWithColumns(33))); 7610 7611 EXPECT_EQ("f(\"some \"\n" 7612 " \"text\",\n" 7613 " other);", 7614 format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); 7615 7616 // Only break as a last resort. 7617 verifyFormat( 7618 "aaaaaaaaaaaaaaaaaaaa(\n" 7619 " aaaaaaaaaaaaaaaaaaaa,\n" 7620 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); 7621 7622 EXPECT_EQ("\"splitmea\"\n" 7623 "\"trandomp\"\n" 7624 "\"oint\"", 7625 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); 7626 7627 EXPECT_EQ("\"split/\"\n" 7628 "\"pathat/\"\n" 7629 "\"slashes\"", 7630 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 7631 7632 EXPECT_EQ("\"split/\"\n" 7633 "\"pathat/\"\n" 7634 "\"slashes\"", 7635 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 7636 EXPECT_EQ("\"split at \"\n" 7637 "\"spaces/at/\"\n" 7638 "\"slashes.at.any$\"\n" 7639 "\"non-alphanumeric%\"\n" 7640 "\"1111111111characte\"\n" 7641 "\"rs\"", 7642 format("\"split at " 7643 "spaces/at/" 7644 "slashes.at." 7645 "any$non-" 7646 "alphanumeric%" 7647 "1111111111characte" 7648 "rs\"", 7649 getLLVMStyleWithColumns(20))); 7650 7651 // Verify that splitting the strings understands 7652 // Style::AlwaysBreakBeforeMultilineStrings. 7653 EXPECT_EQ( 7654 "aaaaaaaaaaaa(\n" 7655 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" 7656 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", 7657 format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa " 7658 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 7659 "aaaaaaaaaaaaaaaaaaaaaa\");", 7660 getGoogleStyle())); 7661 EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7662 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", 7663 format("return \"aaaaaaaaaaaaaaaaaaaaaa " 7664 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 7665 "aaaaaaaaaaaaaaaaaaaaaa\";", 7666 getGoogleStyle())); 7667 EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7668 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 7669 format("llvm::outs() << " 7670 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa" 7671 "aaaaaaaaaaaaaaaaaaa\";")); 7672 EXPECT_EQ("ffff(\n" 7673 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7674 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 7675 format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " 7676 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 7677 getGoogleStyle())); 7678 7679 FormatStyle Style = getLLVMStyleWithColumns(12); 7680 Style.BreakStringLiterals = false; 7681 EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style)); 7682 7683 FormatStyle AlignLeft = getLLVMStyleWithColumns(12); 7684 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 7685 EXPECT_EQ("#define A \\\n" 7686 " \"some \" \\\n" 7687 " \"text \" \\\n" 7688 " \"other\";", 7689 format("#define A \"some text other\";", AlignLeft)); 7690 } 7691 7692 TEST_F(FormatTest, FullyRemoveEmptyLines) { 7693 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80); 7694 NoEmptyLines.MaxEmptyLinesToKeep = 0; 7695 EXPECT_EQ("int i = a(b());", 7696 format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines)); 7697 } 7698 7699 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) { 7700 EXPECT_EQ( 7701 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 7702 "(\n" 7703 " \"x\t\");", 7704 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 7705 "aaaaaaa(" 7706 "\"x\t\");")); 7707 } 7708 7709 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) { 7710 EXPECT_EQ( 7711 "u8\"utf8 string \"\n" 7712 "u8\"literal\";", 7713 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16))); 7714 EXPECT_EQ( 7715 "u\"utf16 string \"\n" 7716 "u\"literal\";", 7717 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16))); 7718 EXPECT_EQ( 7719 "U\"utf32 string \"\n" 7720 "U\"literal\";", 7721 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16))); 7722 EXPECT_EQ("L\"wide string \"\n" 7723 "L\"literal\";", 7724 format("L\"wide string literal\";", getGoogleStyleWithColumns(16))); 7725 EXPECT_EQ("@\"NSString \"\n" 7726 "@\"literal\";", 7727 format("@\"NSString literal\";", getGoogleStyleWithColumns(19))); 7728 verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26)); 7729 7730 // This input makes clang-format try to split the incomplete unicode escape 7731 // sequence, which used to lead to a crasher. 7732 verifyNoCrash( 7733 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 7734 getLLVMStyleWithColumns(60)); 7735 } 7736 7737 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) { 7738 FormatStyle Style = getGoogleStyleWithColumns(15); 7739 EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style)); 7740 EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style)); 7741 EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style)); 7742 EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style)); 7743 EXPECT_EQ("u8R\"x(raw literal)x\";", 7744 format("u8R\"x(raw literal)x\";", Style)); 7745 } 7746 7747 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { 7748 FormatStyle Style = getLLVMStyleWithColumns(20); 7749 EXPECT_EQ( 7750 "_T(\"aaaaaaaaaaaaaa\")\n" 7751 "_T(\"aaaaaaaaaaaaaa\")\n" 7752 "_T(\"aaaaaaaaaaaa\")", 7753 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style)); 7754 EXPECT_EQ("f(x,\n" 7755 " _T(\"aaaaaaaaaaaa\")\n" 7756 " _T(\"aaa\"),\n" 7757 " z);", 7758 format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style)); 7759 7760 // FIXME: Handle embedded spaces in one iteration. 7761 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n" 7762 // "_T(\"aaaaaaaaaaaaa\")\n" 7763 // "_T(\"aaaaaaaaaaaaa\")\n" 7764 // "_T(\"a\")", 7765 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 7766 // getLLVMStyleWithColumns(20))); 7767 EXPECT_EQ( 7768 "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 7769 format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style)); 7770 EXPECT_EQ("f(\n" 7771 "#if !TEST\n" 7772 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 7773 "#endif\n" 7774 ");", 7775 format("f(\n" 7776 "#if !TEST\n" 7777 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 7778 "#endif\n" 7779 ");")); 7780 EXPECT_EQ("f(\n" 7781 "\n" 7782 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));", 7783 format("f(\n" 7784 "\n" 7785 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));")); 7786 } 7787 7788 TEST_F(FormatTest, BreaksStringLiteralOperands) { 7789 // In a function call with two operands, the second can be broken with no line 7790 // break before it. 7791 EXPECT_EQ("func(a, \"long long \"\n" 7792 " \"long long\");", 7793 format("func(a, \"long long long long\");", 7794 getLLVMStyleWithColumns(24))); 7795 // In a function call with three operands, the second must be broken with a 7796 // line break before it. 7797 EXPECT_EQ("func(a,\n" 7798 " \"long long long \"\n" 7799 " \"long\",\n" 7800 " c);", 7801 format("func(a, \"long long long long\", c);", 7802 getLLVMStyleWithColumns(24))); 7803 // In a function call with three operands, the third must be broken with a 7804 // line break before it. 7805 EXPECT_EQ("func(a, b,\n" 7806 " \"long long long \"\n" 7807 " \"long\");", 7808 format("func(a, b, \"long long long long\");", 7809 getLLVMStyleWithColumns(24))); 7810 // In a function call with three operands, both the second and the third must 7811 // be broken with a line break before them. 7812 EXPECT_EQ("func(a,\n" 7813 " \"long long long \"\n" 7814 " \"long\",\n" 7815 " \"long long long \"\n" 7816 " \"long\");", 7817 format("func(a, \"long long long long\", \"long long long long\");", 7818 getLLVMStyleWithColumns(24))); 7819 // In a chain of << with two operands, the second can be broken with no line 7820 // break before it. 7821 EXPECT_EQ("a << \"line line \"\n" 7822 " \"line\";", 7823 format("a << \"line line line\";", 7824 getLLVMStyleWithColumns(20))); 7825 // In a chain of << with three operands, the second can be broken with no line 7826 // break before it. 7827 EXPECT_EQ("abcde << \"line \"\n" 7828 " \"line line\"\n" 7829 " << c;", 7830 format("abcde << \"line line line\" << c;", 7831 getLLVMStyleWithColumns(20))); 7832 // In a chain of << with three operands, the third must be broken with a line 7833 // break before it. 7834 EXPECT_EQ("a << b\n" 7835 " << \"line line \"\n" 7836 " \"line\";", 7837 format("a << b << \"line line line\";", 7838 getLLVMStyleWithColumns(20))); 7839 // In a chain of << with three operands, the second can be broken with no line 7840 // break before it and the third must be broken with a line break before it. 7841 EXPECT_EQ("abcd << \"line line \"\n" 7842 " \"line\"\n" 7843 " << \"line line \"\n" 7844 " \"line\";", 7845 format("abcd << \"line line line\" << \"line line line\";", 7846 getLLVMStyleWithColumns(20))); 7847 // In a chain of binary operators with two operands, the second can be broken 7848 // with no line break before it. 7849 EXPECT_EQ("abcd + \"line line \"\n" 7850 " \"line line\";", 7851 format("abcd + \"line line line line\";", 7852 getLLVMStyleWithColumns(20))); 7853 // In a chain of binary operators with three operands, the second must be 7854 // broken with a line break before it. 7855 EXPECT_EQ("abcd +\n" 7856 " \"line line \"\n" 7857 " \"line line\" +\n" 7858 " e;", 7859 format("abcd + \"line line line line\" + e;", 7860 getLLVMStyleWithColumns(20))); 7861 // In a function call with two operands, with AlignAfterOpenBracket enabled, 7862 // the first must be broken with a line break before it. 7863 FormatStyle Style = getLLVMStyleWithColumns(25); 7864 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 7865 EXPECT_EQ("someFunction(\n" 7866 " \"long long long \"\n" 7867 " \"long\",\n" 7868 " a);", 7869 format("someFunction(\"long long long long\", a);", Style)); 7870 } 7871 7872 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { 7873 EXPECT_EQ( 7874 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 7875 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 7876 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 7877 format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 7878 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 7879 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";")); 7880 } 7881 7882 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { 7883 EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);", 7884 format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle())); 7885 EXPECT_EQ("fffffffffff(g(R\"x(\n" 7886 "multiline raw string literal xxxxxxxxxxxxxx\n" 7887 ")x\",\n" 7888 " a),\n" 7889 " b);", 7890 format("fffffffffff(g(R\"x(\n" 7891 "multiline raw string literal xxxxxxxxxxxxxx\n" 7892 ")x\", a), b);", 7893 getGoogleStyleWithColumns(20))); 7894 EXPECT_EQ("fffffffffff(\n" 7895 " g(R\"x(qqq\n" 7896 "multiline raw string literal xxxxxxxxxxxxxx\n" 7897 ")x\",\n" 7898 " a),\n" 7899 " b);", 7900 format("fffffffffff(g(R\"x(qqq\n" 7901 "multiline raw string literal xxxxxxxxxxxxxx\n" 7902 ")x\", a), b);", 7903 getGoogleStyleWithColumns(20))); 7904 7905 EXPECT_EQ("fffffffffff(R\"x(\n" 7906 "multiline raw string literal xxxxxxxxxxxxxx\n" 7907 ")x\");", 7908 format("fffffffffff(R\"x(\n" 7909 "multiline raw string literal xxxxxxxxxxxxxx\n" 7910 ")x\");", 7911 getGoogleStyleWithColumns(20))); 7912 EXPECT_EQ("fffffffffff(R\"x(\n" 7913 "multiline raw string literal xxxxxxxxxxxxxx\n" 7914 ")x\" + bbbbbb);", 7915 format("fffffffffff(R\"x(\n" 7916 "multiline raw string literal xxxxxxxxxxxxxx\n" 7917 ")x\" + bbbbbb);", 7918 getGoogleStyleWithColumns(20))); 7919 EXPECT_EQ("fffffffffff(\n" 7920 " R\"x(\n" 7921 "multiline raw string literal xxxxxxxxxxxxxx\n" 7922 ")x\" +\n" 7923 " bbbbbb);", 7924 format("fffffffffff(\n" 7925 " R\"x(\n" 7926 "multiline raw string literal xxxxxxxxxxxxxx\n" 7927 ")x\" + bbbbbb);", 7928 getGoogleStyleWithColumns(20))); 7929 } 7930 7931 TEST_F(FormatTest, SkipsUnknownStringLiterals) { 7932 verifyFormat("string a = \"unterminated;"); 7933 EXPECT_EQ("function(\"unterminated,\n" 7934 " OtherParameter);", 7935 format("function( \"unterminated,\n" 7936 " OtherParameter);")); 7937 } 7938 7939 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { 7940 FormatStyle Style = getLLVMStyle(); 7941 Style.Standard = FormatStyle::LS_Cpp03; 7942 EXPECT_EQ("#define x(_a) printf(\"foo\" _a);", 7943 format("#define x(_a) printf(\"foo\"_a);", Style)); 7944 } 7945 7946 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); } 7947 7948 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { 7949 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n" 7950 " \"ddeeefff\");", 7951 format("someFunction(\"aaabbbcccdddeeefff\");", 7952 getLLVMStyleWithColumns(25))); 7953 EXPECT_EQ("someFunction1234567890(\n" 7954 " \"aaabbbcccdddeeefff\");", 7955 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 7956 getLLVMStyleWithColumns(26))); 7957 EXPECT_EQ("someFunction1234567890(\n" 7958 " \"aaabbbcccdddeeeff\"\n" 7959 " \"f\");", 7960 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 7961 getLLVMStyleWithColumns(25))); 7962 EXPECT_EQ("someFunction1234567890(\n" 7963 " \"aaabbbcccdddeeeff\"\n" 7964 " \"f\");", 7965 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 7966 getLLVMStyleWithColumns(24))); 7967 EXPECT_EQ("someFunction(\"aaabbbcc \"\n" 7968 " \"ddde \"\n" 7969 " \"efff\");", 7970 format("someFunction(\"aaabbbcc ddde efff\");", 7971 getLLVMStyleWithColumns(25))); 7972 EXPECT_EQ("someFunction(\"aaabbbccc \"\n" 7973 " \"ddeeefff\");", 7974 format("someFunction(\"aaabbbccc ddeeefff\");", 7975 getLLVMStyleWithColumns(25))); 7976 EXPECT_EQ("someFunction1234567890(\n" 7977 " \"aaabb \"\n" 7978 " \"cccdddeeefff\");", 7979 format("someFunction1234567890(\"aaabb cccdddeeefff\");", 7980 getLLVMStyleWithColumns(25))); 7981 EXPECT_EQ("#define A \\\n" 7982 " string s = \\\n" 7983 " \"123456789\" \\\n" 7984 " \"0\"; \\\n" 7985 " int i;", 7986 format("#define A string s = \"1234567890\"; int i;", 7987 getLLVMStyleWithColumns(20))); 7988 // FIXME: Put additional penalties on breaking at non-whitespace locations. 7989 EXPECT_EQ("someFunction(\"aaabbbcc \"\n" 7990 " \"dddeeeff\"\n" 7991 " \"f\");", 7992 format("someFunction(\"aaabbbcc dddeeefff\");", 7993 getLLVMStyleWithColumns(25))); 7994 } 7995 7996 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { 7997 EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3))); 7998 EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2))); 7999 EXPECT_EQ("\"test\"\n" 8000 "\"\\n\"", 8001 format("\"test\\n\"", getLLVMStyleWithColumns(7))); 8002 EXPECT_EQ("\"tes\\\\\"\n" 8003 "\"n\"", 8004 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); 8005 EXPECT_EQ("\"\\\\\\\\\"\n" 8006 "\"\\n\"", 8007 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); 8008 EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7))); 8009 EXPECT_EQ("\"\\uff01\"\n" 8010 "\"test\"", 8011 format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); 8012 EXPECT_EQ("\"\\Uff01ff02\"", 8013 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); 8014 EXPECT_EQ("\"\\x000000000001\"\n" 8015 "\"next\"", 8016 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); 8017 EXPECT_EQ("\"\\x000000000001next\"", 8018 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); 8019 EXPECT_EQ("\"\\x000000000001\"", 8020 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); 8021 EXPECT_EQ("\"test\"\n" 8022 "\"\\000000\"\n" 8023 "\"000001\"", 8024 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); 8025 EXPECT_EQ("\"test\\000\"\n" 8026 "\"00000000\"\n" 8027 "\"1\"", 8028 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); 8029 } 8030 8031 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { 8032 verifyFormat("void f() {\n" 8033 " return g() {}\n" 8034 " void h() {}"); 8035 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n" 8036 "g();\n" 8037 "}"); 8038 } 8039 8040 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) { 8041 verifyFormat( 8042 "void f() { return C{param1, param2}.SomeCall(param1, param2); }"); 8043 } 8044 8045 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { 8046 verifyFormat("class X {\n" 8047 " void f() {\n" 8048 " }\n" 8049 "};", 8050 getLLVMStyleWithColumns(12)); 8051 } 8052 8053 TEST_F(FormatTest, ConfigurableIndentWidth) { 8054 FormatStyle EightIndent = getLLVMStyleWithColumns(18); 8055 EightIndent.IndentWidth = 8; 8056 EightIndent.ContinuationIndentWidth = 8; 8057 verifyFormat("void f() {\n" 8058 " someFunction();\n" 8059 " if (true) {\n" 8060 " f();\n" 8061 " }\n" 8062 "}", 8063 EightIndent); 8064 verifyFormat("class X {\n" 8065 " void f() {\n" 8066 " }\n" 8067 "};", 8068 EightIndent); 8069 verifyFormat("int x[] = {\n" 8070 " call(),\n" 8071 " call()};", 8072 EightIndent); 8073 } 8074 8075 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) { 8076 verifyFormat("double\n" 8077 "f();", 8078 getLLVMStyleWithColumns(8)); 8079 } 8080 8081 TEST_F(FormatTest, ConfigurableUseOfTab) { 8082 FormatStyle Tab = getLLVMStyleWithColumns(42); 8083 Tab.IndentWidth = 8; 8084 Tab.UseTab = FormatStyle::UT_Always; 8085 Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left; 8086 8087 EXPECT_EQ("if (aaaaaaaa && // q\n" 8088 " bb)\t\t// w\n" 8089 "\t;", 8090 format("if (aaaaaaaa &&// q\n" 8091 "bb)// w\n" 8092 ";", 8093 Tab)); 8094 EXPECT_EQ("if (aaa && bbb) // w\n" 8095 "\t;", 8096 format("if(aaa&&bbb)// w\n" 8097 ";", 8098 Tab)); 8099 8100 verifyFormat("class X {\n" 8101 "\tvoid f() {\n" 8102 "\t\tsomeFunction(parameter1,\n" 8103 "\t\t\t parameter2);\n" 8104 "\t}\n" 8105 "};", 8106 Tab); 8107 verifyFormat("#define A \\\n" 8108 "\tvoid f() { \\\n" 8109 "\t\tsomeFunction( \\\n" 8110 "\t\t parameter1, \\\n" 8111 "\t\t parameter2); \\\n" 8112 "\t}", 8113 Tab); 8114 8115 Tab.TabWidth = 4; 8116 Tab.IndentWidth = 8; 8117 verifyFormat("class TabWidth4Indent8 {\n" 8118 "\t\tvoid f() {\n" 8119 "\t\t\t\tsomeFunction(parameter1,\n" 8120 "\t\t\t\t\t\t\t parameter2);\n" 8121 "\t\t}\n" 8122 "};", 8123 Tab); 8124 8125 Tab.TabWidth = 4; 8126 Tab.IndentWidth = 4; 8127 verifyFormat("class TabWidth4Indent4 {\n" 8128 "\tvoid f() {\n" 8129 "\t\tsomeFunction(parameter1,\n" 8130 "\t\t\t\t\t parameter2);\n" 8131 "\t}\n" 8132 "};", 8133 Tab); 8134 8135 Tab.TabWidth = 8; 8136 Tab.IndentWidth = 4; 8137 verifyFormat("class TabWidth8Indent4 {\n" 8138 " void f() {\n" 8139 "\tsomeFunction(parameter1,\n" 8140 "\t\t parameter2);\n" 8141 " }\n" 8142 "};", 8143 Tab); 8144 8145 Tab.TabWidth = 8; 8146 Tab.IndentWidth = 8; 8147 EXPECT_EQ("/*\n" 8148 "\t a\t\tcomment\n" 8149 "\t in multiple lines\n" 8150 " */", 8151 format(" /*\t \t \n" 8152 " \t \t a\t\tcomment\t \t\n" 8153 " \t \t in multiple lines\t\n" 8154 " \t */", 8155 Tab)); 8156 8157 Tab.UseTab = FormatStyle::UT_ForIndentation; 8158 verifyFormat("{\n" 8159 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8160 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8161 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8162 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8163 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8164 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8165 "};", 8166 Tab); 8167 verifyFormat("enum AA {\n" 8168 "\ta1, // Force multiple lines\n" 8169 "\ta2,\n" 8170 "\ta3\n" 8171 "};", 8172 Tab); 8173 EXPECT_EQ("if (aaaaaaaa && // q\n" 8174 " bb) // w\n" 8175 "\t;", 8176 format("if (aaaaaaaa &&// q\n" 8177 "bb)// w\n" 8178 ";", 8179 Tab)); 8180 verifyFormat("class X {\n" 8181 "\tvoid f() {\n" 8182 "\t\tsomeFunction(parameter1,\n" 8183 "\t\t parameter2);\n" 8184 "\t}\n" 8185 "};", 8186 Tab); 8187 verifyFormat("{\n" 8188 "\tQ(\n" 8189 "\t {\n" 8190 "\t\t int a;\n" 8191 "\t\t someFunction(aaaaaaaa,\n" 8192 "\t\t bbbbbbb);\n" 8193 "\t },\n" 8194 "\t p);\n" 8195 "}", 8196 Tab); 8197 EXPECT_EQ("{\n" 8198 "\t/* aaaa\n" 8199 "\t bbbb */\n" 8200 "}", 8201 format("{\n" 8202 "/* aaaa\n" 8203 " bbbb */\n" 8204 "}", 8205 Tab)); 8206 EXPECT_EQ("{\n" 8207 "\t/*\n" 8208 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8209 "\t bbbbbbbbbbbbb\n" 8210 "\t*/\n" 8211 "}", 8212 format("{\n" 8213 "/*\n" 8214 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8215 "*/\n" 8216 "}", 8217 Tab)); 8218 EXPECT_EQ("{\n" 8219 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8220 "\t// bbbbbbbbbbbbb\n" 8221 "}", 8222 format("{\n" 8223 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8224 "}", 8225 Tab)); 8226 EXPECT_EQ("{\n" 8227 "\t/*\n" 8228 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8229 "\t bbbbbbbbbbbbb\n" 8230 "\t*/\n" 8231 "}", 8232 format("{\n" 8233 "\t/*\n" 8234 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8235 "\t*/\n" 8236 "}", 8237 Tab)); 8238 EXPECT_EQ("{\n" 8239 "\t/*\n" 8240 "\n" 8241 "\t*/\n" 8242 "}", 8243 format("{\n" 8244 "\t/*\n" 8245 "\n" 8246 "\t*/\n" 8247 "}", 8248 Tab)); 8249 EXPECT_EQ("{\n" 8250 "\t/*\n" 8251 " asdf\n" 8252 "\t*/\n" 8253 "}", 8254 format("{\n" 8255 "\t/*\n" 8256 " asdf\n" 8257 "\t*/\n" 8258 "}", 8259 Tab)); 8260 8261 Tab.UseTab = FormatStyle::UT_Never; 8262 EXPECT_EQ("/*\n" 8263 " a\t\tcomment\n" 8264 " in multiple lines\n" 8265 " */", 8266 format(" /*\t \t \n" 8267 " \t \t a\t\tcomment\t \t\n" 8268 " \t \t in multiple lines\t\n" 8269 " \t */", 8270 Tab)); 8271 EXPECT_EQ("/* some\n" 8272 " comment */", 8273 format(" \t \t /* some\n" 8274 " \t \t comment */", 8275 Tab)); 8276 EXPECT_EQ("int a; /* some\n" 8277 " comment */", 8278 format(" \t \t int a; /* some\n" 8279 " \t \t comment */", 8280 Tab)); 8281 8282 EXPECT_EQ("int a; /* some\n" 8283 "comment */", 8284 format(" \t \t int\ta; /* some\n" 8285 " \t \t comment */", 8286 Tab)); 8287 EXPECT_EQ("f(\"\t\t\"); /* some\n" 8288 " comment */", 8289 format(" \t \t f(\"\t\t\"); /* some\n" 8290 " \t \t comment */", 8291 Tab)); 8292 EXPECT_EQ("{\n" 8293 " /*\n" 8294 " * Comment\n" 8295 " */\n" 8296 " int i;\n" 8297 "}", 8298 format("{\n" 8299 "\t/*\n" 8300 "\t * Comment\n" 8301 "\t */\n" 8302 "\t int i;\n" 8303 "}")); 8304 8305 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 8306 Tab.TabWidth = 8; 8307 Tab.IndentWidth = 8; 8308 EXPECT_EQ("if (aaaaaaaa && // q\n" 8309 " bb) // w\n" 8310 "\t;", 8311 format("if (aaaaaaaa &&// q\n" 8312 "bb)// w\n" 8313 ";", 8314 Tab)); 8315 EXPECT_EQ("if (aaa && bbb) // w\n" 8316 "\t;", 8317 format("if(aaa&&bbb)// w\n" 8318 ";", 8319 Tab)); 8320 verifyFormat("class X {\n" 8321 "\tvoid f() {\n" 8322 "\t\tsomeFunction(parameter1,\n" 8323 "\t\t\t parameter2);\n" 8324 "\t}\n" 8325 "};", 8326 Tab); 8327 verifyFormat("#define A \\\n" 8328 "\tvoid f() { \\\n" 8329 "\t\tsomeFunction( \\\n" 8330 "\t\t parameter1, \\\n" 8331 "\t\t parameter2); \\\n" 8332 "\t}", 8333 Tab); 8334 Tab.TabWidth = 4; 8335 Tab.IndentWidth = 8; 8336 verifyFormat("class TabWidth4Indent8 {\n" 8337 "\t\tvoid f() {\n" 8338 "\t\t\t\tsomeFunction(parameter1,\n" 8339 "\t\t\t\t\t\t\t parameter2);\n" 8340 "\t\t}\n" 8341 "};", 8342 Tab); 8343 Tab.TabWidth = 4; 8344 Tab.IndentWidth = 4; 8345 verifyFormat("class TabWidth4Indent4 {\n" 8346 "\tvoid f() {\n" 8347 "\t\tsomeFunction(parameter1,\n" 8348 "\t\t\t\t\t parameter2);\n" 8349 "\t}\n" 8350 "};", 8351 Tab); 8352 Tab.TabWidth = 8; 8353 Tab.IndentWidth = 4; 8354 verifyFormat("class TabWidth8Indent4 {\n" 8355 " void f() {\n" 8356 "\tsomeFunction(parameter1,\n" 8357 "\t\t parameter2);\n" 8358 " }\n" 8359 "};", 8360 Tab); 8361 Tab.TabWidth = 8; 8362 Tab.IndentWidth = 8; 8363 EXPECT_EQ("/*\n" 8364 "\t a\t\tcomment\n" 8365 "\t in multiple lines\n" 8366 " */", 8367 format(" /*\t \t \n" 8368 " \t \t a\t\tcomment\t \t\n" 8369 " \t \t in multiple lines\t\n" 8370 " \t */", 8371 Tab)); 8372 verifyFormat("{\n" 8373 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8374 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8375 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8376 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8377 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8378 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8379 "};", 8380 Tab); 8381 verifyFormat("enum AA {\n" 8382 "\ta1, // Force multiple lines\n" 8383 "\ta2,\n" 8384 "\ta3\n" 8385 "};", 8386 Tab); 8387 EXPECT_EQ("if (aaaaaaaa && // q\n" 8388 " bb) // w\n" 8389 "\t;", 8390 format("if (aaaaaaaa &&// q\n" 8391 "bb)// w\n" 8392 ";", 8393 Tab)); 8394 verifyFormat("class X {\n" 8395 "\tvoid f() {\n" 8396 "\t\tsomeFunction(parameter1,\n" 8397 "\t\t\t parameter2);\n" 8398 "\t}\n" 8399 "};", 8400 Tab); 8401 verifyFormat("{\n" 8402 "\tQ(\n" 8403 "\t {\n" 8404 "\t\t int a;\n" 8405 "\t\t someFunction(aaaaaaaa,\n" 8406 "\t\t\t\t bbbbbbb);\n" 8407 "\t },\n" 8408 "\t p);\n" 8409 "}", 8410 Tab); 8411 EXPECT_EQ("{\n" 8412 "\t/* aaaa\n" 8413 "\t bbbb */\n" 8414 "}", 8415 format("{\n" 8416 "/* aaaa\n" 8417 " bbbb */\n" 8418 "}", 8419 Tab)); 8420 EXPECT_EQ("{\n" 8421 "\t/*\n" 8422 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8423 "\t bbbbbbbbbbbbb\n" 8424 "\t*/\n" 8425 "}", 8426 format("{\n" 8427 "/*\n" 8428 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8429 "*/\n" 8430 "}", 8431 Tab)); 8432 EXPECT_EQ("{\n" 8433 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8434 "\t// bbbbbbbbbbbbb\n" 8435 "}", 8436 format("{\n" 8437 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8438 "}", 8439 Tab)); 8440 EXPECT_EQ("{\n" 8441 "\t/*\n" 8442 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8443 "\t bbbbbbbbbbbbb\n" 8444 "\t*/\n" 8445 "}", 8446 format("{\n" 8447 "\t/*\n" 8448 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8449 "\t*/\n" 8450 "}", 8451 Tab)); 8452 EXPECT_EQ("{\n" 8453 "\t/*\n" 8454 "\n" 8455 "\t*/\n" 8456 "}", 8457 format("{\n" 8458 "\t/*\n" 8459 "\n" 8460 "\t*/\n" 8461 "}", 8462 Tab)); 8463 EXPECT_EQ("{\n" 8464 "\t/*\n" 8465 " asdf\n" 8466 "\t*/\n" 8467 "}", 8468 format("{\n" 8469 "\t/*\n" 8470 " asdf\n" 8471 "\t*/\n" 8472 "}", 8473 Tab)); 8474 EXPECT_EQ("/*\n" 8475 "\t a\t\tcomment\n" 8476 "\t in multiple lines\n" 8477 " */", 8478 format(" /*\t \t \n" 8479 " \t \t a\t\tcomment\t \t\n" 8480 " \t \t in multiple lines\t\n" 8481 " \t */", 8482 Tab)); 8483 EXPECT_EQ("/* some\n" 8484 " comment */", 8485 format(" \t \t /* some\n" 8486 " \t \t comment */", 8487 Tab)); 8488 EXPECT_EQ("int a; /* some\n" 8489 " comment */", 8490 format(" \t \t int a; /* some\n" 8491 " \t \t comment */", 8492 Tab)); 8493 EXPECT_EQ("int a; /* some\n" 8494 "comment */", 8495 format(" \t \t int\ta; /* some\n" 8496 " \t \t comment */", 8497 Tab)); 8498 EXPECT_EQ("f(\"\t\t\"); /* some\n" 8499 " comment */", 8500 format(" \t \t f(\"\t\t\"); /* some\n" 8501 " \t \t comment */", 8502 Tab)); 8503 EXPECT_EQ("{\n" 8504 " /*\n" 8505 " * Comment\n" 8506 " */\n" 8507 " int i;\n" 8508 "}", 8509 format("{\n" 8510 "\t/*\n" 8511 "\t * Comment\n" 8512 "\t */\n" 8513 "\t int i;\n" 8514 "}")); 8515 Tab.AlignConsecutiveAssignments = true; 8516 Tab.AlignConsecutiveDeclarations = true; 8517 Tab.TabWidth = 4; 8518 Tab.IndentWidth = 4; 8519 verifyFormat("class Assign {\n" 8520 "\tvoid f() {\n" 8521 "\t\tint x = 123;\n" 8522 "\t\tint random = 4;\n" 8523 "\t\tstd::string alphabet =\n" 8524 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 8525 "\t}\n" 8526 "};", 8527 Tab); 8528 } 8529 8530 TEST_F(FormatTest, CalculatesOriginalColumn) { 8531 EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8532 "q\"; /* some\n" 8533 " comment */", 8534 format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8535 "q\"; /* some\n" 8536 " comment */", 8537 getLLVMStyle())); 8538 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 8539 "/* some\n" 8540 " comment */", 8541 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 8542 " /* some\n" 8543 " comment */", 8544 getLLVMStyle())); 8545 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8546 "qqq\n" 8547 "/* some\n" 8548 " comment */", 8549 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8550 "qqq\n" 8551 " /* some\n" 8552 " comment */", 8553 getLLVMStyle())); 8554 EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8555 "wwww; /* some\n" 8556 " comment */", 8557 format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8558 "wwww; /* some\n" 8559 " comment */", 8560 getLLVMStyle())); 8561 } 8562 8563 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { 8564 FormatStyle NoSpace = getLLVMStyle(); 8565 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never; 8566 8567 verifyFormat("while(true)\n" 8568 " continue;", 8569 NoSpace); 8570 verifyFormat("for(;;)\n" 8571 " continue;", 8572 NoSpace); 8573 verifyFormat("if(true)\n" 8574 " f();\n" 8575 "else if(true)\n" 8576 " f();", 8577 NoSpace); 8578 verifyFormat("do {\n" 8579 " do_something();\n" 8580 "} while(something());", 8581 NoSpace); 8582 verifyFormat("switch(x) {\n" 8583 "default:\n" 8584 " break;\n" 8585 "}", 8586 NoSpace); 8587 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace); 8588 verifyFormat("size_t x = sizeof(x);", NoSpace); 8589 verifyFormat("auto f(int x) -> decltype(x);", NoSpace); 8590 verifyFormat("int f(T x) noexcept(x.create());", NoSpace); 8591 verifyFormat("alignas(128) char a[128];", NoSpace); 8592 verifyFormat("size_t x = alignof(MyType);", NoSpace); 8593 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace); 8594 verifyFormat("int f() throw(Deprecated);", NoSpace); 8595 verifyFormat("typedef void (*cb)(int);", NoSpace); 8596 verifyFormat("T A::operator()();", NoSpace); 8597 verifyFormat("X A::operator++(T);", NoSpace); 8598 8599 FormatStyle Space = getLLVMStyle(); 8600 Space.SpaceBeforeParens = FormatStyle::SBPO_Always; 8601 8602 verifyFormat("int f ();", Space); 8603 verifyFormat("void f (int a, T b) {\n" 8604 " while (true)\n" 8605 " continue;\n" 8606 "}", 8607 Space); 8608 verifyFormat("if (true)\n" 8609 " f ();\n" 8610 "else if (true)\n" 8611 " f ();", 8612 Space); 8613 verifyFormat("do {\n" 8614 " do_something ();\n" 8615 "} while (something ());", 8616 Space); 8617 verifyFormat("switch (x) {\n" 8618 "default:\n" 8619 " break;\n" 8620 "}", 8621 Space); 8622 verifyFormat("A::A () : a (1) {}", Space); 8623 verifyFormat("void f () __attribute__ ((asdf));", Space); 8624 verifyFormat("*(&a + 1);\n" 8625 "&((&a)[1]);\n" 8626 "a[(b + c) * d];\n" 8627 "(((a + 1) * 2) + 3) * 4;", 8628 Space); 8629 verifyFormat("#define A(x) x", Space); 8630 verifyFormat("#define A (x) x", Space); 8631 verifyFormat("#if defined(x)\n" 8632 "#endif", 8633 Space); 8634 verifyFormat("auto i = std::make_unique<int> (5);", Space); 8635 verifyFormat("size_t x = sizeof (x);", Space); 8636 verifyFormat("auto f (int x) -> decltype (x);", Space); 8637 verifyFormat("int f (T x) noexcept (x.create ());", Space); 8638 verifyFormat("alignas (128) char a[128];", Space); 8639 verifyFormat("size_t x = alignof (MyType);", Space); 8640 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space); 8641 verifyFormat("int f () throw (Deprecated);", Space); 8642 verifyFormat("typedef void (*cb) (int);", Space); 8643 verifyFormat("T A::operator() ();", Space); 8644 verifyFormat("X A::operator++ (T);", Space); 8645 } 8646 8647 TEST_F(FormatTest, ConfigurableSpacesInParentheses) { 8648 FormatStyle Spaces = getLLVMStyle(); 8649 8650 Spaces.SpacesInParentheses = true; 8651 verifyFormat("call( x, y, z );", Spaces); 8652 verifyFormat("call();", Spaces); 8653 verifyFormat("std::function<void( int, int )> callback;", Spaces); 8654 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }", 8655 Spaces); 8656 verifyFormat("while ( (bool)1 )\n" 8657 " continue;", 8658 Spaces); 8659 verifyFormat("for ( ;; )\n" 8660 " continue;", 8661 Spaces); 8662 verifyFormat("if ( true )\n" 8663 " f();\n" 8664 "else if ( true )\n" 8665 " f();", 8666 Spaces); 8667 verifyFormat("do {\n" 8668 " do_something( (int)i );\n" 8669 "} while ( something() );", 8670 Spaces); 8671 verifyFormat("switch ( x ) {\n" 8672 "default:\n" 8673 " break;\n" 8674 "}", 8675 Spaces); 8676 8677 Spaces.SpacesInParentheses = false; 8678 Spaces.SpacesInCStyleCastParentheses = true; 8679 verifyFormat("Type *A = ( Type * )P;", Spaces); 8680 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces); 8681 verifyFormat("x = ( int32 )y;", Spaces); 8682 verifyFormat("int a = ( int )(2.0f);", Spaces); 8683 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces); 8684 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces); 8685 verifyFormat("#define x (( int )-1)", Spaces); 8686 8687 // Run the first set of tests again with: 8688 Spaces.SpacesInParentheses = false; 8689 Spaces.SpaceInEmptyParentheses = true; 8690 Spaces.SpacesInCStyleCastParentheses = true; 8691 verifyFormat("call(x, y, z);", Spaces); 8692 verifyFormat("call( );", Spaces); 8693 verifyFormat("std::function<void(int, int)> callback;", Spaces); 8694 verifyFormat("while (( bool )1)\n" 8695 " continue;", 8696 Spaces); 8697 verifyFormat("for (;;)\n" 8698 " continue;", 8699 Spaces); 8700 verifyFormat("if (true)\n" 8701 " f( );\n" 8702 "else if (true)\n" 8703 " f( );", 8704 Spaces); 8705 verifyFormat("do {\n" 8706 " do_something(( int )i);\n" 8707 "} while (something( ));", 8708 Spaces); 8709 verifyFormat("switch (x) {\n" 8710 "default:\n" 8711 " break;\n" 8712 "}", 8713 Spaces); 8714 8715 // Run the first set of tests again with: 8716 Spaces.SpaceAfterCStyleCast = true; 8717 verifyFormat("call(x, y, z);", Spaces); 8718 verifyFormat("call( );", Spaces); 8719 verifyFormat("std::function<void(int, int)> callback;", Spaces); 8720 verifyFormat("while (( bool ) 1)\n" 8721 " continue;", 8722 Spaces); 8723 verifyFormat("for (;;)\n" 8724 " continue;", 8725 Spaces); 8726 verifyFormat("if (true)\n" 8727 " f( );\n" 8728 "else if (true)\n" 8729 " f( );", 8730 Spaces); 8731 verifyFormat("do {\n" 8732 " do_something(( int ) i);\n" 8733 "} while (something( ));", 8734 Spaces); 8735 verifyFormat("switch (x) {\n" 8736 "default:\n" 8737 " break;\n" 8738 "}", 8739 Spaces); 8740 8741 // Run subset of tests again with: 8742 Spaces.SpacesInCStyleCastParentheses = false; 8743 Spaces.SpaceAfterCStyleCast = true; 8744 verifyFormat("while ((bool) 1)\n" 8745 " continue;", 8746 Spaces); 8747 verifyFormat("do {\n" 8748 " do_something((int) i);\n" 8749 "} while (something( ));", 8750 Spaces); 8751 } 8752 8753 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) { 8754 verifyFormat("int a[5];"); 8755 verifyFormat("a[3] += 42;"); 8756 8757 FormatStyle Spaces = getLLVMStyle(); 8758 Spaces.SpacesInSquareBrackets = true; 8759 // Lambdas unchanged. 8760 verifyFormat("int c = []() -> int { return 2; }();\n", Spaces); 8761 verifyFormat("return [i, args...] {};", Spaces); 8762 8763 // Not lambdas. 8764 verifyFormat("int a[ 5 ];", Spaces); 8765 verifyFormat("a[ 3 ] += 42;", Spaces); 8766 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces); 8767 verifyFormat("double &operator[](int i) { return 0; }\n" 8768 "int i;", 8769 Spaces); 8770 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces); 8771 verifyFormat("int i = a[ a ][ a ]->f();", Spaces); 8772 verifyFormat("int i = (*b)[ a ]->f();", Spaces); 8773 } 8774 8775 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) { 8776 verifyFormat("int a = 5;"); 8777 verifyFormat("a += 42;"); 8778 verifyFormat("a or_eq 8;"); 8779 8780 FormatStyle Spaces = getLLVMStyle(); 8781 Spaces.SpaceBeforeAssignmentOperators = false; 8782 verifyFormat("int a= 5;", Spaces); 8783 verifyFormat("a+= 42;", Spaces); 8784 verifyFormat("a or_eq 8;", Spaces); 8785 } 8786 8787 TEST_F(FormatTest, AlignConsecutiveAssignments) { 8788 FormatStyle Alignment = getLLVMStyle(); 8789 Alignment.AlignConsecutiveAssignments = false; 8790 verifyFormat("int a = 5;\n" 8791 "int oneTwoThree = 123;", 8792 Alignment); 8793 verifyFormat("int a = 5;\n" 8794 "int oneTwoThree = 123;", 8795 Alignment); 8796 8797 Alignment.AlignConsecutiveAssignments = true; 8798 verifyFormat("int a = 5;\n" 8799 "int oneTwoThree = 123;", 8800 Alignment); 8801 verifyFormat("int a = method();\n" 8802 "int oneTwoThree = 133;", 8803 Alignment); 8804 verifyFormat("a &= 5;\n" 8805 "bcd *= 5;\n" 8806 "ghtyf += 5;\n" 8807 "dvfvdb -= 5;\n" 8808 "a /= 5;\n" 8809 "vdsvsv %= 5;\n" 8810 "sfdbddfbdfbb ^= 5;\n" 8811 "dvsdsv |= 5;\n" 8812 "int dsvvdvsdvvv = 123;", 8813 Alignment); 8814 verifyFormat("int i = 1, j = 10;\n" 8815 "something = 2000;", 8816 Alignment); 8817 verifyFormat("something = 2000;\n" 8818 "int i = 1, j = 10;\n", 8819 Alignment); 8820 verifyFormat("something = 2000;\n" 8821 "another = 911;\n" 8822 "int i = 1, j = 10;\n" 8823 "oneMore = 1;\n" 8824 "i = 2;", 8825 Alignment); 8826 verifyFormat("int a = 5;\n" 8827 "int one = 1;\n" 8828 "method();\n" 8829 "int oneTwoThree = 123;\n" 8830 "int oneTwo = 12;", 8831 Alignment); 8832 verifyFormat("int oneTwoThree = 123;\n" 8833 "int oneTwo = 12;\n" 8834 "method();\n", 8835 Alignment); 8836 verifyFormat("int oneTwoThree = 123; // comment\n" 8837 "int oneTwo = 12; // comment", 8838 Alignment); 8839 EXPECT_EQ("int a = 5;\n" 8840 "\n" 8841 "int oneTwoThree = 123;", 8842 format("int a = 5;\n" 8843 "\n" 8844 "int oneTwoThree= 123;", 8845 Alignment)); 8846 EXPECT_EQ("int a = 5;\n" 8847 "int one = 1;\n" 8848 "\n" 8849 "int oneTwoThree = 123;", 8850 format("int a = 5;\n" 8851 "int one = 1;\n" 8852 "\n" 8853 "int oneTwoThree = 123;", 8854 Alignment)); 8855 EXPECT_EQ("int a = 5;\n" 8856 "int one = 1;\n" 8857 "\n" 8858 "int oneTwoThree = 123;\n" 8859 "int oneTwo = 12;", 8860 format("int a = 5;\n" 8861 "int one = 1;\n" 8862 "\n" 8863 "int oneTwoThree = 123;\n" 8864 "int oneTwo = 12;", 8865 Alignment)); 8866 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 8867 verifyFormat("#define A \\\n" 8868 " int aaaa = 12; \\\n" 8869 " int b = 23; \\\n" 8870 " int ccc = 234; \\\n" 8871 " int dddddddddd = 2345;", 8872 Alignment); 8873 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 8874 verifyFormat("#define A \\\n" 8875 " int aaaa = 12; \\\n" 8876 " int b = 23; \\\n" 8877 " int ccc = 234; \\\n" 8878 " int dddddddddd = 2345;", 8879 Alignment); 8880 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 8881 verifyFormat("#define A " 8882 " \\\n" 8883 " int aaaa = 12; " 8884 " \\\n" 8885 " int b = 23; " 8886 " \\\n" 8887 " int ccc = 234; " 8888 " \\\n" 8889 " int dddddddddd = 2345;", 8890 Alignment); 8891 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 8892 "k = 4, int l = 5,\n" 8893 " int m = 6) {\n" 8894 " int j = 10;\n" 8895 " otherThing = 1;\n" 8896 "}", 8897 Alignment); 8898 verifyFormat("void SomeFunction(int parameter = 0) {\n" 8899 " int i = 1;\n" 8900 " int j = 2;\n" 8901 " int big = 10000;\n" 8902 "}", 8903 Alignment); 8904 verifyFormat("class C {\n" 8905 "public:\n" 8906 " int i = 1;\n" 8907 " virtual void f() = 0;\n" 8908 "};", 8909 Alignment); 8910 verifyFormat("int i = 1;\n" 8911 "if (SomeType t = getSomething()) {\n" 8912 "}\n" 8913 "int j = 2;\n" 8914 "int big = 10000;", 8915 Alignment); 8916 verifyFormat("int j = 7;\n" 8917 "for (int k = 0; k < N; ++k) {\n" 8918 "}\n" 8919 "int j = 2;\n" 8920 "int big = 10000;\n" 8921 "}", 8922 Alignment); 8923 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 8924 verifyFormat("int i = 1;\n" 8925 "LooooooooooongType loooooooooooooooooooooongVariable\n" 8926 " = someLooooooooooooooooongFunction();\n" 8927 "int j = 2;", 8928 Alignment); 8929 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 8930 verifyFormat("int i = 1;\n" 8931 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 8932 " someLooooooooooooooooongFunction();\n" 8933 "int j = 2;", 8934 Alignment); 8935 8936 verifyFormat("auto lambda = []() {\n" 8937 " auto i = 0;\n" 8938 " return 0;\n" 8939 "};\n" 8940 "int i = 0;\n" 8941 "auto v = type{\n" 8942 " i = 1, //\n" 8943 " (i = 2), //\n" 8944 " i = 3 //\n" 8945 "};", 8946 Alignment); 8947 8948 verifyFormat( 8949 "int i = 1;\n" 8950 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 8951 " loooooooooooooooooooooongParameterB);\n" 8952 "int j = 2;", 8953 Alignment); 8954 8955 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 8956 " typename B = very_long_type_name_1,\n" 8957 " typename T_2 = very_long_type_name_2>\n" 8958 "auto foo() {}\n", 8959 Alignment); 8960 verifyFormat("int a, b = 1;\n" 8961 "int c = 2;\n" 8962 "int dd = 3;\n", 8963 Alignment); 8964 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 8965 "float b[1][] = {{3.f}};\n", 8966 Alignment); 8967 verifyFormat("for (int i = 0; i < 1; i++)\n" 8968 " int x = 1;\n", 8969 Alignment); 8970 verifyFormat("for (i = 0; i < 1; i++)\n" 8971 " x = 1;\n" 8972 "y = 1;\n", 8973 Alignment); 8974 } 8975 8976 TEST_F(FormatTest, AlignConsecutiveDeclarations) { 8977 FormatStyle Alignment = getLLVMStyle(); 8978 Alignment.AlignConsecutiveDeclarations = false; 8979 verifyFormat("float const a = 5;\n" 8980 "int oneTwoThree = 123;", 8981 Alignment); 8982 verifyFormat("int a = 5;\n" 8983 "float const oneTwoThree = 123;", 8984 Alignment); 8985 8986 Alignment.AlignConsecutiveDeclarations = true; 8987 verifyFormat("float const a = 5;\n" 8988 "int oneTwoThree = 123;", 8989 Alignment); 8990 verifyFormat("int a = method();\n" 8991 "float const oneTwoThree = 133;", 8992 Alignment); 8993 verifyFormat("int i = 1, j = 10;\n" 8994 "something = 2000;", 8995 Alignment); 8996 verifyFormat("something = 2000;\n" 8997 "int i = 1, j = 10;\n", 8998 Alignment); 8999 verifyFormat("float something = 2000;\n" 9000 "double another = 911;\n" 9001 "int i = 1, j = 10;\n" 9002 "const int *oneMore = 1;\n" 9003 "unsigned i = 2;", 9004 Alignment); 9005 verifyFormat("float a = 5;\n" 9006 "int one = 1;\n" 9007 "method();\n" 9008 "const double oneTwoThree = 123;\n" 9009 "const unsigned int oneTwo = 12;", 9010 Alignment); 9011 verifyFormat("int oneTwoThree{0}; // comment\n" 9012 "unsigned oneTwo; // comment", 9013 Alignment); 9014 EXPECT_EQ("float const a = 5;\n" 9015 "\n" 9016 "int oneTwoThree = 123;", 9017 format("float const a = 5;\n" 9018 "\n" 9019 "int oneTwoThree= 123;", 9020 Alignment)); 9021 EXPECT_EQ("float a = 5;\n" 9022 "int one = 1;\n" 9023 "\n" 9024 "unsigned oneTwoThree = 123;", 9025 format("float a = 5;\n" 9026 "int one = 1;\n" 9027 "\n" 9028 "unsigned oneTwoThree = 123;", 9029 Alignment)); 9030 EXPECT_EQ("float a = 5;\n" 9031 "int one = 1;\n" 9032 "\n" 9033 "unsigned oneTwoThree = 123;\n" 9034 "int oneTwo = 12;", 9035 format("float a = 5;\n" 9036 "int one = 1;\n" 9037 "\n" 9038 "unsigned oneTwoThree = 123;\n" 9039 "int oneTwo = 12;", 9040 Alignment)); 9041 // Function prototype alignment 9042 verifyFormat("int a();\n" 9043 "double b();", 9044 Alignment); 9045 verifyFormat("int a(int x);\n" 9046 "double b();", 9047 Alignment); 9048 unsigned OldColumnLimit = Alignment.ColumnLimit; 9049 // We need to set ColumnLimit to zero, in order to stress nested alignments, 9050 // otherwise the function parameters will be re-flowed onto a single line. 9051 Alignment.ColumnLimit = 0; 9052 EXPECT_EQ("int a(int x,\n" 9053 " float y);\n" 9054 "double b(int x,\n" 9055 " double y);", 9056 format("int a(int x,\n" 9057 " float y);\n" 9058 "double b(int x,\n" 9059 " double y);", 9060 Alignment)); 9061 // This ensures that function parameters of function declarations are 9062 // correctly indented when their owning functions are indented. 9063 // The failure case here is for 'double y' to not be indented enough. 9064 EXPECT_EQ("double a(int x);\n" 9065 "int b(int y,\n" 9066 " double z);", 9067 format("double a(int x);\n" 9068 "int b(int y,\n" 9069 " double z);", 9070 Alignment)); 9071 // Set ColumnLimit low so that we induce wrapping immediately after 9072 // the function name and opening paren. 9073 Alignment.ColumnLimit = 13; 9074 verifyFormat("int function(\n" 9075 " int x,\n" 9076 " bool y);", 9077 Alignment); 9078 Alignment.ColumnLimit = OldColumnLimit; 9079 // Ensure function pointers don't screw up recursive alignment 9080 verifyFormat("int a(int x, void (*fp)(int y));\n" 9081 "double b();", 9082 Alignment); 9083 Alignment.AlignConsecutiveAssignments = true; 9084 // Ensure recursive alignment is broken by function braces, so that the 9085 // "a = 1" does not align with subsequent assignments inside the function 9086 // body. 9087 verifyFormat("int func(int a = 1) {\n" 9088 " int b = 2;\n" 9089 " int cc = 3;\n" 9090 "}", 9091 Alignment); 9092 verifyFormat("float something = 2000;\n" 9093 "double another = 911;\n" 9094 "int i = 1, j = 10;\n" 9095 "const int *oneMore = 1;\n" 9096 "unsigned i = 2;", 9097 Alignment); 9098 verifyFormat("int oneTwoThree = {0}; // comment\n" 9099 "unsigned oneTwo = 0; // comment", 9100 Alignment); 9101 // Make sure that scope is correctly tracked, in the absence of braces 9102 verifyFormat("for (int i = 0; i < n; i++)\n" 9103 " j = i;\n" 9104 "double x = 1;\n", 9105 Alignment); 9106 verifyFormat("if (int i = 0)\n" 9107 " j = i;\n" 9108 "double x = 1;\n", 9109 Alignment); 9110 // Ensure operator[] and operator() are comprehended 9111 verifyFormat("struct test {\n" 9112 " long long int foo();\n" 9113 " int operator[](int a);\n" 9114 " double bar();\n" 9115 "};\n", 9116 Alignment); 9117 verifyFormat("struct test {\n" 9118 " long long int foo();\n" 9119 " int operator()(int a);\n" 9120 " double bar();\n" 9121 "};\n", 9122 Alignment); 9123 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 9124 " int const i = 1;\n" 9125 " int * j = 2;\n" 9126 " int big = 10000;\n" 9127 "\n" 9128 " unsigned oneTwoThree = 123;\n" 9129 " int oneTwo = 12;\n" 9130 " method();\n" 9131 " float k = 2;\n" 9132 " int ll = 10000;\n" 9133 "}", 9134 format("void SomeFunction(int parameter= 0) {\n" 9135 " int const i= 1;\n" 9136 " int *j=2;\n" 9137 " int big = 10000;\n" 9138 "\n" 9139 "unsigned oneTwoThree =123;\n" 9140 "int oneTwo = 12;\n" 9141 " method();\n" 9142 "float k= 2;\n" 9143 "int ll=10000;\n" 9144 "}", 9145 Alignment)); 9146 Alignment.AlignConsecutiveAssignments = false; 9147 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 9148 verifyFormat("#define A \\\n" 9149 " int aaaa = 12; \\\n" 9150 " float b = 23; \\\n" 9151 " const int ccc = 234; \\\n" 9152 " unsigned dddddddddd = 2345;", 9153 Alignment); 9154 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 9155 verifyFormat("#define A \\\n" 9156 " int aaaa = 12; \\\n" 9157 " float b = 23; \\\n" 9158 " const int ccc = 234; \\\n" 9159 " unsigned dddddddddd = 2345;", 9160 Alignment); 9161 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 9162 Alignment.ColumnLimit = 30; 9163 verifyFormat("#define A \\\n" 9164 " int aaaa = 12; \\\n" 9165 " float b = 23; \\\n" 9166 " const int ccc = 234; \\\n" 9167 " int dddddddddd = 2345;", 9168 Alignment); 9169 Alignment.ColumnLimit = 80; 9170 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 9171 "k = 4, int l = 5,\n" 9172 " int m = 6) {\n" 9173 " const int j = 10;\n" 9174 " otherThing = 1;\n" 9175 "}", 9176 Alignment); 9177 verifyFormat("void SomeFunction(int parameter = 0) {\n" 9178 " int const i = 1;\n" 9179 " int * j = 2;\n" 9180 " int big = 10000;\n" 9181 "}", 9182 Alignment); 9183 verifyFormat("class C {\n" 9184 "public:\n" 9185 " int i = 1;\n" 9186 " virtual void f() = 0;\n" 9187 "};", 9188 Alignment); 9189 verifyFormat("float i = 1;\n" 9190 "if (SomeType t = getSomething()) {\n" 9191 "}\n" 9192 "const unsigned j = 2;\n" 9193 "int big = 10000;", 9194 Alignment); 9195 verifyFormat("float j = 7;\n" 9196 "for (int k = 0; k < N; ++k) {\n" 9197 "}\n" 9198 "unsigned j = 2;\n" 9199 "int big = 10000;\n" 9200 "}", 9201 Alignment); 9202 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 9203 verifyFormat("float i = 1;\n" 9204 "LooooooooooongType loooooooooooooooooooooongVariable\n" 9205 " = someLooooooooooooooooongFunction();\n" 9206 "int j = 2;", 9207 Alignment); 9208 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 9209 verifyFormat("int i = 1;\n" 9210 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 9211 " someLooooooooooooooooongFunction();\n" 9212 "int j = 2;", 9213 Alignment); 9214 9215 Alignment.AlignConsecutiveAssignments = true; 9216 verifyFormat("auto lambda = []() {\n" 9217 " auto ii = 0;\n" 9218 " float j = 0;\n" 9219 " return 0;\n" 9220 "};\n" 9221 "int i = 0;\n" 9222 "float i2 = 0;\n" 9223 "auto v = type{\n" 9224 " i = 1, //\n" 9225 " (i = 2), //\n" 9226 " i = 3 //\n" 9227 "};", 9228 Alignment); 9229 Alignment.AlignConsecutiveAssignments = false; 9230 9231 verifyFormat( 9232 "int i = 1;\n" 9233 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 9234 " loooooooooooooooooooooongParameterB);\n" 9235 "int j = 2;", 9236 Alignment); 9237 9238 // Test interactions with ColumnLimit and AlignConsecutiveAssignments: 9239 // We expect declarations and assignments to align, as long as it doesn't 9240 // exceed the column limit, starting a new alignment sequence whenever it 9241 // happens. 9242 Alignment.AlignConsecutiveAssignments = true; 9243 Alignment.ColumnLimit = 30; 9244 verifyFormat("float ii = 1;\n" 9245 "unsigned j = 2;\n" 9246 "int someVerylongVariable = 1;\n" 9247 "AnotherLongType ll = 123456;\n" 9248 "VeryVeryLongType k = 2;\n" 9249 "int myvar = 1;", 9250 Alignment); 9251 Alignment.ColumnLimit = 80; 9252 Alignment.AlignConsecutiveAssignments = false; 9253 9254 verifyFormat( 9255 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n" 9256 " typename LongType, typename B>\n" 9257 "auto foo() {}\n", 9258 Alignment); 9259 verifyFormat("float a, b = 1;\n" 9260 "int c = 2;\n" 9261 "int dd = 3;\n", 9262 Alignment); 9263 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 9264 "float b[1][] = {{3.f}};\n", 9265 Alignment); 9266 Alignment.AlignConsecutiveAssignments = true; 9267 verifyFormat("float a, b = 1;\n" 9268 "int c = 2;\n" 9269 "int dd = 3;\n", 9270 Alignment); 9271 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 9272 "float b[1][] = {{3.f}};\n", 9273 Alignment); 9274 Alignment.AlignConsecutiveAssignments = false; 9275 9276 Alignment.ColumnLimit = 30; 9277 Alignment.BinPackParameters = false; 9278 verifyFormat("void foo(float a,\n" 9279 " float b,\n" 9280 " int c,\n" 9281 " uint32_t *d) {\n" 9282 " int * e = 0;\n" 9283 " float f = 0;\n" 9284 " double g = 0;\n" 9285 "}\n" 9286 "void bar(ino_t a,\n" 9287 " int b,\n" 9288 " uint32_t *c,\n" 9289 " bool d) {}\n", 9290 Alignment); 9291 Alignment.BinPackParameters = true; 9292 Alignment.ColumnLimit = 80; 9293 9294 // Bug 33507 9295 Alignment.PointerAlignment = FormatStyle::PAS_Middle; 9296 verifyFormat( 9297 "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n" 9298 " static const Version verVs2017;\n" 9299 " return true;\n" 9300 "});\n", 9301 Alignment); 9302 Alignment.PointerAlignment = FormatStyle::PAS_Right; 9303 } 9304 9305 TEST_F(FormatTest, LinuxBraceBreaking) { 9306 FormatStyle LinuxBraceStyle = getLLVMStyle(); 9307 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux; 9308 verifyFormat("namespace a\n" 9309 "{\n" 9310 "class A\n" 9311 "{\n" 9312 " void f()\n" 9313 " {\n" 9314 " if (true) {\n" 9315 " a();\n" 9316 " b();\n" 9317 " } else {\n" 9318 " a();\n" 9319 " }\n" 9320 " }\n" 9321 " void g() { return; }\n" 9322 "};\n" 9323 "struct B {\n" 9324 " int x;\n" 9325 "};\n" 9326 "} // namespace a\n", 9327 LinuxBraceStyle); 9328 verifyFormat("enum X {\n" 9329 " Y = 0,\n" 9330 "}\n", 9331 LinuxBraceStyle); 9332 verifyFormat("struct S {\n" 9333 " int Type;\n" 9334 " union {\n" 9335 " int x;\n" 9336 " double y;\n" 9337 " } Value;\n" 9338 " class C\n" 9339 " {\n" 9340 " MyFavoriteType Value;\n" 9341 " } Class;\n" 9342 "}\n", 9343 LinuxBraceStyle); 9344 } 9345 9346 TEST_F(FormatTest, MozillaBraceBreaking) { 9347 FormatStyle MozillaBraceStyle = getLLVMStyle(); 9348 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; 9349 MozillaBraceStyle.FixNamespaceComments = false; 9350 verifyFormat("namespace a {\n" 9351 "class A\n" 9352 "{\n" 9353 " void f()\n" 9354 " {\n" 9355 " if (true) {\n" 9356 " a();\n" 9357 " b();\n" 9358 " }\n" 9359 " }\n" 9360 " void g() { return; }\n" 9361 "};\n" 9362 "enum E\n" 9363 "{\n" 9364 " A,\n" 9365 " // foo\n" 9366 " B,\n" 9367 " C\n" 9368 "};\n" 9369 "struct B\n" 9370 "{\n" 9371 " int x;\n" 9372 "};\n" 9373 "}\n", 9374 MozillaBraceStyle); 9375 verifyFormat("struct S\n" 9376 "{\n" 9377 " int Type;\n" 9378 " union\n" 9379 " {\n" 9380 " int x;\n" 9381 " double y;\n" 9382 " } Value;\n" 9383 " class C\n" 9384 " {\n" 9385 " MyFavoriteType Value;\n" 9386 " } Class;\n" 9387 "}\n", 9388 MozillaBraceStyle); 9389 } 9390 9391 TEST_F(FormatTest, StroustrupBraceBreaking) { 9392 FormatStyle StroustrupBraceStyle = getLLVMStyle(); 9393 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 9394 verifyFormat("namespace a {\n" 9395 "class A {\n" 9396 " void f()\n" 9397 " {\n" 9398 " if (true) {\n" 9399 " a();\n" 9400 " b();\n" 9401 " }\n" 9402 " }\n" 9403 " void g() { return; }\n" 9404 "};\n" 9405 "struct B {\n" 9406 " int x;\n" 9407 "};\n" 9408 "} // namespace a\n", 9409 StroustrupBraceStyle); 9410 9411 verifyFormat("void foo()\n" 9412 "{\n" 9413 " if (a) {\n" 9414 " a();\n" 9415 " }\n" 9416 " else {\n" 9417 " b();\n" 9418 " }\n" 9419 "}\n", 9420 StroustrupBraceStyle); 9421 9422 verifyFormat("#ifdef _DEBUG\n" 9423 "int foo(int i = 0)\n" 9424 "#else\n" 9425 "int foo(int i = 5)\n" 9426 "#endif\n" 9427 "{\n" 9428 " return i;\n" 9429 "}", 9430 StroustrupBraceStyle); 9431 9432 verifyFormat("void foo() {}\n" 9433 "void bar()\n" 9434 "#ifdef _DEBUG\n" 9435 "{\n" 9436 " foo();\n" 9437 "}\n" 9438 "#else\n" 9439 "{\n" 9440 "}\n" 9441 "#endif", 9442 StroustrupBraceStyle); 9443 9444 verifyFormat("void foobar() { int i = 5; }\n" 9445 "#ifdef _DEBUG\n" 9446 "void bar() {}\n" 9447 "#else\n" 9448 "void bar() { foobar(); }\n" 9449 "#endif", 9450 StroustrupBraceStyle); 9451 } 9452 9453 TEST_F(FormatTest, AllmanBraceBreaking) { 9454 FormatStyle AllmanBraceStyle = getLLVMStyle(); 9455 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman; 9456 9457 EXPECT_EQ("namespace a\n" 9458 "{\n" 9459 "void f();\n" 9460 "void g();\n" 9461 "} // namespace a\n", 9462 format("namespace a\n" 9463 "{\n" 9464 "void f();\n" 9465 "void g();\n" 9466 "}\n", 9467 AllmanBraceStyle)); 9468 9469 verifyFormat("namespace a\n" 9470 "{\n" 9471 "class A\n" 9472 "{\n" 9473 " void f()\n" 9474 " {\n" 9475 " if (true)\n" 9476 " {\n" 9477 " a();\n" 9478 " b();\n" 9479 " }\n" 9480 " }\n" 9481 " void g() { return; }\n" 9482 "};\n" 9483 "struct B\n" 9484 "{\n" 9485 " int x;\n" 9486 "};\n" 9487 "} // namespace a", 9488 AllmanBraceStyle); 9489 9490 verifyFormat("void f()\n" 9491 "{\n" 9492 " if (true)\n" 9493 " {\n" 9494 " a();\n" 9495 " }\n" 9496 " else if (false)\n" 9497 " {\n" 9498 " b();\n" 9499 " }\n" 9500 " else\n" 9501 " {\n" 9502 " c();\n" 9503 " }\n" 9504 "}\n", 9505 AllmanBraceStyle); 9506 9507 verifyFormat("void f()\n" 9508 "{\n" 9509 " for (int i = 0; i < 10; ++i)\n" 9510 " {\n" 9511 " a();\n" 9512 " }\n" 9513 " while (false)\n" 9514 " {\n" 9515 " b();\n" 9516 " }\n" 9517 " do\n" 9518 " {\n" 9519 " c();\n" 9520 " } while (false)\n" 9521 "}\n", 9522 AllmanBraceStyle); 9523 9524 verifyFormat("void f(int a)\n" 9525 "{\n" 9526 " switch (a)\n" 9527 " {\n" 9528 " case 0:\n" 9529 " break;\n" 9530 " case 1:\n" 9531 " {\n" 9532 " break;\n" 9533 " }\n" 9534 " case 2:\n" 9535 " {\n" 9536 " }\n" 9537 " break;\n" 9538 " default:\n" 9539 " break;\n" 9540 " }\n" 9541 "}\n", 9542 AllmanBraceStyle); 9543 9544 verifyFormat("enum X\n" 9545 "{\n" 9546 " Y = 0,\n" 9547 "}\n", 9548 AllmanBraceStyle); 9549 verifyFormat("enum X\n" 9550 "{\n" 9551 " Y = 0\n" 9552 "}\n", 9553 AllmanBraceStyle); 9554 9555 verifyFormat("@interface BSApplicationController ()\n" 9556 "{\n" 9557 "@private\n" 9558 " id _extraIvar;\n" 9559 "}\n" 9560 "@end\n", 9561 AllmanBraceStyle); 9562 9563 verifyFormat("#ifdef _DEBUG\n" 9564 "int foo(int i = 0)\n" 9565 "#else\n" 9566 "int foo(int i = 5)\n" 9567 "#endif\n" 9568 "{\n" 9569 " return i;\n" 9570 "}", 9571 AllmanBraceStyle); 9572 9573 verifyFormat("void foo() {}\n" 9574 "void bar()\n" 9575 "#ifdef _DEBUG\n" 9576 "{\n" 9577 " foo();\n" 9578 "}\n" 9579 "#else\n" 9580 "{\n" 9581 "}\n" 9582 "#endif", 9583 AllmanBraceStyle); 9584 9585 verifyFormat("void foobar() { int i = 5; }\n" 9586 "#ifdef _DEBUG\n" 9587 "void bar() {}\n" 9588 "#else\n" 9589 "void bar() { foobar(); }\n" 9590 "#endif", 9591 AllmanBraceStyle); 9592 9593 // This shouldn't affect ObjC blocks.. 9594 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 9595 " // ...\n" 9596 " int i;\n" 9597 "}];", 9598 AllmanBraceStyle); 9599 verifyFormat("void (^block)(void) = ^{\n" 9600 " // ...\n" 9601 " int i;\n" 9602 "};", 9603 AllmanBraceStyle); 9604 // .. or dict literals. 9605 verifyFormat("void f()\n" 9606 "{\n" 9607 " // ...\n" 9608 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 9609 "}", 9610 AllmanBraceStyle); 9611 verifyFormat("void f()\n" 9612 "{\n" 9613 " // ...\n" 9614 " [object someMethod:@{a : @\"b\"}];\n" 9615 "}", 9616 AllmanBraceStyle); 9617 verifyFormat("int f()\n" 9618 "{ // comment\n" 9619 " return 42;\n" 9620 "}", 9621 AllmanBraceStyle); 9622 9623 AllmanBraceStyle.ColumnLimit = 19; 9624 verifyFormat("void f() { int i; }", AllmanBraceStyle); 9625 AllmanBraceStyle.ColumnLimit = 18; 9626 verifyFormat("void f()\n" 9627 "{\n" 9628 " int i;\n" 9629 "}", 9630 AllmanBraceStyle); 9631 AllmanBraceStyle.ColumnLimit = 80; 9632 9633 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle; 9634 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true; 9635 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 9636 verifyFormat("void f(bool b)\n" 9637 "{\n" 9638 " if (b)\n" 9639 " {\n" 9640 " return;\n" 9641 " }\n" 9642 "}\n", 9643 BreakBeforeBraceShortIfs); 9644 verifyFormat("void f(bool b)\n" 9645 "{\n" 9646 " if constexpr (b)\n" 9647 " {\n" 9648 " return;\n" 9649 " }\n" 9650 "}\n", 9651 BreakBeforeBraceShortIfs); 9652 verifyFormat("void f(bool b)\n" 9653 "{\n" 9654 " if (b) return;\n" 9655 "}\n", 9656 BreakBeforeBraceShortIfs); 9657 verifyFormat("void f(bool b)\n" 9658 "{\n" 9659 " if constexpr (b) return;\n" 9660 "}\n", 9661 BreakBeforeBraceShortIfs); 9662 verifyFormat("void f(bool b)\n" 9663 "{\n" 9664 " while (b)\n" 9665 " {\n" 9666 " return;\n" 9667 " }\n" 9668 "}\n", 9669 BreakBeforeBraceShortIfs); 9670 } 9671 9672 TEST_F(FormatTest, GNUBraceBreaking) { 9673 FormatStyle GNUBraceStyle = getLLVMStyle(); 9674 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU; 9675 verifyFormat("namespace a\n" 9676 "{\n" 9677 "class A\n" 9678 "{\n" 9679 " void f()\n" 9680 " {\n" 9681 " int a;\n" 9682 " {\n" 9683 " int b;\n" 9684 " }\n" 9685 " if (true)\n" 9686 " {\n" 9687 " a();\n" 9688 " b();\n" 9689 " }\n" 9690 " }\n" 9691 " void g() { return; }\n" 9692 "}\n" 9693 "} // namespace a", 9694 GNUBraceStyle); 9695 9696 verifyFormat("void f()\n" 9697 "{\n" 9698 " if (true)\n" 9699 " {\n" 9700 " a();\n" 9701 " }\n" 9702 " else if (false)\n" 9703 " {\n" 9704 " b();\n" 9705 " }\n" 9706 " else\n" 9707 " {\n" 9708 " c();\n" 9709 " }\n" 9710 "}\n", 9711 GNUBraceStyle); 9712 9713 verifyFormat("void f()\n" 9714 "{\n" 9715 " for (int i = 0; i < 10; ++i)\n" 9716 " {\n" 9717 " a();\n" 9718 " }\n" 9719 " while (false)\n" 9720 " {\n" 9721 " b();\n" 9722 " }\n" 9723 " do\n" 9724 " {\n" 9725 " c();\n" 9726 " }\n" 9727 " while (false);\n" 9728 "}\n", 9729 GNUBraceStyle); 9730 9731 verifyFormat("void f(int a)\n" 9732 "{\n" 9733 " switch (a)\n" 9734 " {\n" 9735 " case 0:\n" 9736 " break;\n" 9737 " case 1:\n" 9738 " {\n" 9739 " break;\n" 9740 " }\n" 9741 " case 2:\n" 9742 " {\n" 9743 " }\n" 9744 " break;\n" 9745 " default:\n" 9746 " break;\n" 9747 " }\n" 9748 "}\n", 9749 GNUBraceStyle); 9750 9751 verifyFormat("enum X\n" 9752 "{\n" 9753 " Y = 0,\n" 9754 "}\n", 9755 GNUBraceStyle); 9756 9757 verifyFormat("@interface BSApplicationController ()\n" 9758 "{\n" 9759 "@private\n" 9760 " id _extraIvar;\n" 9761 "}\n" 9762 "@end\n", 9763 GNUBraceStyle); 9764 9765 verifyFormat("#ifdef _DEBUG\n" 9766 "int foo(int i = 0)\n" 9767 "#else\n" 9768 "int foo(int i = 5)\n" 9769 "#endif\n" 9770 "{\n" 9771 " return i;\n" 9772 "}", 9773 GNUBraceStyle); 9774 9775 verifyFormat("void foo() {}\n" 9776 "void bar()\n" 9777 "#ifdef _DEBUG\n" 9778 "{\n" 9779 " foo();\n" 9780 "}\n" 9781 "#else\n" 9782 "{\n" 9783 "}\n" 9784 "#endif", 9785 GNUBraceStyle); 9786 9787 verifyFormat("void foobar() { int i = 5; }\n" 9788 "#ifdef _DEBUG\n" 9789 "void bar() {}\n" 9790 "#else\n" 9791 "void bar() { foobar(); }\n" 9792 "#endif", 9793 GNUBraceStyle); 9794 } 9795 9796 TEST_F(FormatTest, WebKitBraceBreaking) { 9797 FormatStyle WebKitBraceStyle = getLLVMStyle(); 9798 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit; 9799 WebKitBraceStyle.FixNamespaceComments = false; 9800 verifyFormat("namespace a {\n" 9801 "class A {\n" 9802 " void f()\n" 9803 " {\n" 9804 " if (true) {\n" 9805 " a();\n" 9806 " b();\n" 9807 " }\n" 9808 " }\n" 9809 " void g() { return; }\n" 9810 "};\n" 9811 "enum E {\n" 9812 " A,\n" 9813 " // foo\n" 9814 " B,\n" 9815 " C\n" 9816 "};\n" 9817 "struct B {\n" 9818 " int x;\n" 9819 "};\n" 9820 "}\n", 9821 WebKitBraceStyle); 9822 verifyFormat("struct S {\n" 9823 " int Type;\n" 9824 " union {\n" 9825 " int x;\n" 9826 " double y;\n" 9827 " } Value;\n" 9828 " class C {\n" 9829 " MyFavoriteType Value;\n" 9830 " } Class;\n" 9831 "};\n", 9832 WebKitBraceStyle); 9833 } 9834 9835 TEST_F(FormatTest, CatchExceptionReferenceBinding) { 9836 verifyFormat("void f() {\n" 9837 " try {\n" 9838 " } catch (const Exception &e) {\n" 9839 " }\n" 9840 "}\n", 9841 getLLVMStyle()); 9842 } 9843 9844 TEST_F(FormatTest, UnderstandsPragmas) { 9845 verifyFormat("#pragma omp reduction(| : var)"); 9846 verifyFormat("#pragma omp reduction(+ : var)"); 9847 9848 EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string " 9849 "(including parentheses).", 9850 format("#pragma mark Any non-hyphenated or hyphenated string " 9851 "(including parentheses).")); 9852 } 9853 9854 TEST_F(FormatTest, UnderstandPragmaOption) { 9855 verifyFormat("#pragma option -C -A"); 9856 9857 EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A")); 9858 } 9859 9860 #define EXPECT_ALL_STYLES_EQUAL(Styles) \ 9861 for (size_t i = 1; i < Styles.size(); ++i) \ 9862 EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \ 9863 << " differs from Style #0" 9864 9865 TEST_F(FormatTest, GetsPredefinedStyleByName) { 9866 SmallVector<FormatStyle, 3> Styles; 9867 Styles.resize(3); 9868 9869 Styles[0] = getLLVMStyle(); 9870 EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1])); 9871 EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2])); 9872 EXPECT_ALL_STYLES_EQUAL(Styles); 9873 9874 Styles[0] = getGoogleStyle(); 9875 EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1])); 9876 EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2])); 9877 EXPECT_ALL_STYLES_EQUAL(Styles); 9878 9879 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 9880 EXPECT_TRUE( 9881 getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1])); 9882 EXPECT_TRUE( 9883 getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2])); 9884 EXPECT_ALL_STYLES_EQUAL(Styles); 9885 9886 Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp); 9887 EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1])); 9888 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2])); 9889 EXPECT_ALL_STYLES_EQUAL(Styles); 9890 9891 Styles[0] = getMozillaStyle(); 9892 EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1])); 9893 EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2])); 9894 EXPECT_ALL_STYLES_EQUAL(Styles); 9895 9896 Styles[0] = getWebKitStyle(); 9897 EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1])); 9898 EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2])); 9899 EXPECT_ALL_STYLES_EQUAL(Styles); 9900 9901 Styles[0] = getGNUStyle(); 9902 EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1])); 9903 EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2])); 9904 EXPECT_ALL_STYLES_EQUAL(Styles); 9905 9906 EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0])); 9907 } 9908 9909 TEST_F(FormatTest, GetsCorrectBasedOnStyle) { 9910 SmallVector<FormatStyle, 8> Styles; 9911 Styles.resize(2); 9912 9913 Styles[0] = getGoogleStyle(); 9914 Styles[1] = getLLVMStyle(); 9915 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 9916 EXPECT_ALL_STYLES_EQUAL(Styles); 9917 9918 Styles.resize(5); 9919 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 9920 Styles[1] = getLLVMStyle(); 9921 Styles[1].Language = FormatStyle::LK_JavaScript; 9922 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 9923 9924 Styles[2] = getLLVMStyle(); 9925 Styles[2].Language = FormatStyle::LK_JavaScript; 9926 EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n" 9927 "BasedOnStyle: Google", 9928 &Styles[2]) 9929 .value()); 9930 9931 Styles[3] = getLLVMStyle(); 9932 Styles[3].Language = FormatStyle::LK_JavaScript; 9933 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n" 9934 "Language: JavaScript", 9935 &Styles[3]) 9936 .value()); 9937 9938 Styles[4] = getLLVMStyle(); 9939 Styles[4].Language = FormatStyle::LK_JavaScript; 9940 EXPECT_EQ(0, parseConfiguration("---\n" 9941 "BasedOnStyle: LLVM\n" 9942 "IndentWidth: 123\n" 9943 "---\n" 9944 "BasedOnStyle: Google\n" 9945 "Language: JavaScript", 9946 &Styles[4]) 9947 .value()); 9948 EXPECT_ALL_STYLES_EQUAL(Styles); 9949 } 9950 9951 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \ 9952 Style.FIELD = false; \ 9953 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \ 9954 EXPECT_TRUE(Style.FIELD); \ 9955 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \ 9956 EXPECT_FALSE(Style.FIELD); 9957 9958 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD) 9959 9960 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \ 9961 Style.STRUCT.FIELD = false; \ 9962 EXPECT_EQ(0, \ 9963 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \ 9964 .value()); \ 9965 EXPECT_TRUE(Style.STRUCT.FIELD); \ 9966 EXPECT_EQ(0, \ 9967 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \ 9968 .value()); \ 9969 EXPECT_FALSE(Style.STRUCT.FIELD); 9970 9971 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \ 9972 CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD) 9973 9974 #define CHECK_PARSE(TEXT, FIELD, VALUE) \ 9975 EXPECT_NE(VALUE, Style.FIELD); \ 9976 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \ 9977 EXPECT_EQ(VALUE, Style.FIELD) 9978 9979 TEST_F(FormatTest, ParsesConfigurationBools) { 9980 FormatStyle Style = {}; 9981 Style.Language = FormatStyle::LK_Cpp; 9982 CHECK_PARSE_BOOL(AlignOperands); 9983 CHECK_PARSE_BOOL(AlignTrailingComments); 9984 CHECK_PARSE_BOOL(AlignConsecutiveAssignments); 9985 CHECK_PARSE_BOOL(AlignConsecutiveDeclarations); 9986 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); 9987 CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine); 9988 CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine); 9989 CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine); 9990 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); 9991 CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations); 9992 CHECK_PARSE_BOOL(BinPackArguments); 9993 CHECK_PARSE_BOOL(BinPackParameters); 9994 CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations); 9995 CHECK_PARSE_BOOL(BreakBeforeTernaryOperators); 9996 CHECK_PARSE_BOOL(BreakStringLiterals); 9997 CHECK_PARSE_BOOL(BreakBeforeInheritanceComma) 9998 CHECK_PARSE_BOOL(CompactNamespaces); 9999 CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine); 10000 CHECK_PARSE_BOOL(DerivePointerAlignment); 10001 CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding"); 10002 CHECK_PARSE_BOOL(DisableFormat); 10003 CHECK_PARSE_BOOL(IndentCaseLabels); 10004 CHECK_PARSE_BOOL(IndentWrappedFunctionNames); 10005 CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks); 10006 CHECK_PARSE_BOOL(ObjCSpaceAfterProperty); 10007 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList); 10008 CHECK_PARSE_BOOL(Cpp11BracedListStyle); 10009 CHECK_PARSE_BOOL(ReflowComments); 10010 CHECK_PARSE_BOOL(SortIncludes); 10011 CHECK_PARSE_BOOL(SortUsingDeclarations); 10012 CHECK_PARSE_BOOL(SpacesInParentheses); 10013 CHECK_PARSE_BOOL(SpacesInSquareBrackets); 10014 CHECK_PARSE_BOOL(SpacesInAngles); 10015 CHECK_PARSE_BOOL(SpaceInEmptyParentheses); 10016 CHECK_PARSE_BOOL(SpacesInContainerLiterals); 10017 CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); 10018 CHECK_PARSE_BOOL(SpaceAfterCStyleCast); 10019 CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword); 10020 CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); 10021 10022 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass); 10023 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement); 10024 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum); 10025 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction); 10026 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace); 10027 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration); 10028 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct); 10029 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion); 10030 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock); 10031 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch); 10032 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse); 10033 CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces); 10034 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction); 10035 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord); 10036 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace); 10037 } 10038 10039 #undef CHECK_PARSE_BOOL 10040 10041 TEST_F(FormatTest, ParsesConfiguration) { 10042 FormatStyle Style = {}; 10043 Style.Language = FormatStyle::LK_Cpp; 10044 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234); 10045 CHECK_PARSE("ConstructorInitializerIndentWidth: 1234", 10046 ConstructorInitializerIndentWidth, 1234u); 10047 CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u); 10048 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); 10049 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u); 10050 CHECK_PARSE("PenaltyBreakAssignment: 1234", 10051 PenaltyBreakAssignment, 1234u); 10052 CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234", 10053 PenaltyBreakBeforeFirstCallParameter, 1234u); 10054 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); 10055 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234", 10056 PenaltyReturnTypeOnItsOwnLine, 1234u); 10057 CHECK_PARSE("SpacesBeforeTrailingComments: 1234", 10058 SpacesBeforeTrailingComments, 1234u); 10059 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u); 10060 CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u); 10061 CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$"); 10062 10063 Style.PointerAlignment = FormatStyle::PAS_Middle; 10064 CHECK_PARSE("PointerAlignment: Left", PointerAlignment, 10065 FormatStyle::PAS_Left); 10066 CHECK_PARSE("PointerAlignment: Right", PointerAlignment, 10067 FormatStyle::PAS_Right); 10068 CHECK_PARSE("PointerAlignment: Middle", PointerAlignment, 10069 FormatStyle::PAS_Middle); 10070 // For backward compatibility: 10071 CHECK_PARSE("PointerBindsToType: Left", PointerAlignment, 10072 FormatStyle::PAS_Left); 10073 CHECK_PARSE("PointerBindsToType: Right", PointerAlignment, 10074 FormatStyle::PAS_Right); 10075 CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment, 10076 FormatStyle::PAS_Middle); 10077 10078 Style.Standard = FormatStyle::LS_Auto; 10079 CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03); 10080 CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11); 10081 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03); 10082 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11); 10083 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto); 10084 10085 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 10086 CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment", 10087 BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment); 10088 CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators, 10089 FormatStyle::BOS_None); 10090 CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators, 10091 FormatStyle::BOS_All); 10092 // For backward compatibility: 10093 CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators, 10094 FormatStyle::BOS_None); 10095 CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators, 10096 FormatStyle::BOS_All); 10097 10098 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 10099 CHECK_PARSE("BreakConstructorInitializers: BeforeComma", 10100 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 10101 CHECK_PARSE("BreakConstructorInitializers: AfterColon", 10102 BreakConstructorInitializers, FormatStyle::BCIS_AfterColon); 10103 CHECK_PARSE("BreakConstructorInitializers: BeforeColon", 10104 BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon); 10105 // For backward compatibility: 10106 CHECK_PARSE("BreakConstructorInitializersBeforeComma: true", 10107 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 10108 10109 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 10110 CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket, 10111 FormatStyle::BAS_Align); 10112 CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket, 10113 FormatStyle::BAS_DontAlign); 10114 CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket, 10115 FormatStyle::BAS_AlwaysBreak); 10116 // For backward compatibility: 10117 CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket, 10118 FormatStyle::BAS_DontAlign); 10119 CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket, 10120 FormatStyle::BAS_Align); 10121 10122 Style.AlignEscapedNewlines = FormatStyle::ENAS_Left; 10123 CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines, 10124 FormatStyle::ENAS_DontAlign); 10125 CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines, 10126 FormatStyle::ENAS_Left); 10127 CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines, 10128 FormatStyle::ENAS_Right); 10129 // For backward compatibility: 10130 CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines, 10131 FormatStyle::ENAS_Left); 10132 CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines, 10133 FormatStyle::ENAS_Right); 10134 10135 Style.UseTab = FormatStyle::UT_ForIndentation; 10136 CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never); 10137 CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation); 10138 CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always); 10139 CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab, 10140 FormatStyle::UT_ForContinuationAndIndentation); 10141 // For backward compatibility: 10142 CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never); 10143 CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always); 10144 10145 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 10146 CHECK_PARSE("AllowShortFunctionsOnASingleLine: None", 10147 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 10148 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline", 10149 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline); 10150 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty", 10151 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty); 10152 CHECK_PARSE("AllowShortFunctionsOnASingleLine: All", 10153 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 10154 // For backward compatibility: 10155 CHECK_PARSE("AllowShortFunctionsOnASingleLine: false", 10156 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 10157 CHECK_PARSE("AllowShortFunctionsOnASingleLine: true", 10158 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 10159 10160 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 10161 CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens, 10162 FormatStyle::SBPO_Never); 10163 CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens, 10164 FormatStyle::SBPO_Always); 10165 CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens, 10166 FormatStyle::SBPO_ControlStatements); 10167 // For backward compatibility: 10168 CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens, 10169 FormatStyle::SBPO_Never); 10170 CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens, 10171 FormatStyle::SBPO_ControlStatements); 10172 10173 Style.ColumnLimit = 123; 10174 FormatStyle BaseStyle = getLLVMStyle(); 10175 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit); 10176 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u); 10177 10178 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 10179 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces, 10180 FormatStyle::BS_Attach); 10181 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces, 10182 FormatStyle::BS_Linux); 10183 CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces, 10184 FormatStyle::BS_Mozilla); 10185 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces, 10186 FormatStyle::BS_Stroustrup); 10187 CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces, 10188 FormatStyle::BS_Allman); 10189 CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU); 10190 CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces, 10191 FormatStyle::BS_WebKit); 10192 CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces, 10193 FormatStyle::BS_Custom); 10194 10195 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 10196 CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType, 10197 FormatStyle::RTBS_None); 10198 CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType, 10199 FormatStyle::RTBS_All); 10200 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel", 10201 AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel); 10202 CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions", 10203 AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions); 10204 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions", 10205 AlwaysBreakAfterReturnType, 10206 FormatStyle::RTBS_TopLevelDefinitions); 10207 10208 Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All; 10209 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None", 10210 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None); 10211 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All", 10212 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All); 10213 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel", 10214 AlwaysBreakAfterDefinitionReturnType, 10215 FormatStyle::DRTBS_TopLevel); 10216 10217 Style.NamespaceIndentation = FormatStyle::NI_All; 10218 CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation, 10219 FormatStyle::NI_None); 10220 CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation, 10221 FormatStyle::NI_Inner); 10222 CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation, 10223 FormatStyle::NI_All); 10224 10225 // FIXME: This is required because parsing a configuration simply overwrites 10226 // the first N elements of the list instead of resetting it. 10227 Style.ForEachMacros.clear(); 10228 std::vector<std::string> BoostForeach; 10229 BoostForeach.push_back("BOOST_FOREACH"); 10230 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach); 10231 std::vector<std::string> BoostAndQForeach; 10232 BoostAndQForeach.push_back("BOOST_FOREACH"); 10233 BoostAndQForeach.push_back("Q_FOREACH"); 10234 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros, 10235 BoostAndQForeach); 10236 10237 Style.IncludeCategories.clear(); 10238 std::vector<FormatStyle::IncludeCategory> ExpectedCategories = {{"abc/.*", 2}, 10239 {".*", 1}}; 10240 CHECK_PARSE("IncludeCategories:\n" 10241 " - Regex: abc/.*\n" 10242 " Priority: 2\n" 10243 " - Regex: .*\n" 10244 " Priority: 1", 10245 IncludeCategories, ExpectedCategories); 10246 CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeIsMainRegex, "abc$"); 10247 } 10248 10249 TEST_F(FormatTest, ParsesConfigurationWithLanguages) { 10250 FormatStyle Style = {}; 10251 Style.Language = FormatStyle::LK_Cpp; 10252 CHECK_PARSE("Language: Cpp\n" 10253 "IndentWidth: 12", 10254 IndentWidth, 12u); 10255 EXPECT_EQ(parseConfiguration("Language: JavaScript\n" 10256 "IndentWidth: 34", 10257 &Style), 10258 ParseError::Unsuitable); 10259 EXPECT_EQ(12u, Style.IndentWidth); 10260 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 10261 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 10262 10263 Style.Language = FormatStyle::LK_JavaScript; 10264 CHECK_PARSE("Language: JavaScript\n" 10265 "IndentWidth: 12", 10266 IndentWidth, 12u); 10267 CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u); 10268 EXPECT_EQ(parseConfiguration("Language: Cpp\n" 10269 "IndentWidth: 34", 10270 &Style), 10271 ParseError::Unsuitable); 10272 EXPECT_EQ(23u, Style.IndentWidth); 10273 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 10274 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 10275 10276 CHECK_PARSE("BasedOnStyle: LLVM\n" 10277 "IndentWidth: 67", 10278 IndentWidth, 67u); 10279 10280 CHECK_PARSE("---\n" 10281 "Language: JavaScript\n" 10282 "IndentWidth: 12\n" 10283 "---\n" 10284 "Language: Cpp\n" 10285 "IndentWidth: 34\n" 10286 "...\n", 10287 IndentWidth, 12u); 10288 10289 Style.Language = FormatStyle::LK_Cpp; 10290 CHECK_PARSE("---\n" 10291 "Language: JavaScript\n" 10292 "IndentWidth: 12\n" 10293 "---\n" 10294 "Language: Cpp\n" 10295 "IndentWidth: 34\n" 10296 "...\n", 10297 IndentWidth, 34u); 10298 CHECK_PARSE("---\n" 10299 "IndentWidth: 78\n" 10300 "---\n" 10301 "Language: JavaScript\n" 10302 "IndentWidth: 56\n" 10303 "...\n", 10304 IndentWidth, 78u); 10305 10306 Style.ColumnLimit = 123; 10307 Style.IndentWidth = 234; 10308 Style.BreakBeforeBraces = FormatStyle::BS_Linux; 10309 Style.TabWidth = 345; 10310 EXPECT_FALSE(parseConfiguration("---\n" 10311 "IndentWidth: 456\n" 10312 "BreakBeforeBraces: Allman\n" 10313 "---\n" 10314 "Language: JavaScript\n" 10315 "IndentWidth: 111\n" 10316 "TabWidth: 111\n" 10317 "---\n" 10318 "Language: Cpp\n" 10319 "BreakBeforeBraces: Stroustrup\n" 10320 "TabWidth: 789\n" 10321 "...\n", 10322 &Style)); 10323 EXPECT_EQ(123u, Style.ColumnLimit); 10324 EXPECT_EQ(456u, Style.IndentWidth); 10325 EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces); 10326 EXPECT_EQ(789u, Style.TabWidth); 10327 10328 EXPECT_EQ(parseConfiguration("---\n" 10329 "Language: JavaScript\n" 10330 "IndentWidth: 56\n" 10331 "---\n" 10332 "IndentWidth: 78\n" 10333 "...\n", 10334 &Style), 10335 ParseError::Error); 10336 EXPECT_EQ(parseConfiguration("---\n" 10337 "Language: JavaScript\n" 10338 "IndentWidth: 56\n" 10339 "---\n" 10340 "Language: JavaScript\n" 10341 "IndentWidth: 78\n" 10342 "...\n", 10343 &Style), 10344 ParseError::Error); 10345 10346 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 10347 } 10348 10349 #undef CHECK_PARSE 10350 10351 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) { 10352 FormatStyle Style = {}; 10353 Style.Language = FormatStyle::LK_JavaScript; 10354 Style.BreakBeforeTernaryOperators = true; 10355 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value()); 10356 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 10357 10358 Style.BreakBeforeTernaryOperators = true; 10359 EXPECT_EQ(0, parseConfiguration("---\n" 10360 "BasedOnStyle: Google\n" 10361 "---\n" 10362 "Language: JavaScript\n" 10363 "IndentWidth: 76\n" 10364 "...\n", 10365 &Style) 10366 .value()); 10367 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 10368 EXPECT_EQ(76u, Style.IndentWidth); 10369 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 10370 } 10371 10372 TEST_F(FormatTest, ConfigurationRoundTripTest) { 10373 FormatStyle Style = getLLVMStyle(); 10374 std::string YAML = configurationAsText(Style); 10375 FormatStyle ParsedStyle = {}; 10376 ParsedStyle.Language = FormatStyle::LK_Cpp; 10377 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value()); 10378 EXPECT_EQ(Style, ParsedStyle); 10379 } 10380 10381 TEST_F(FormatTest, WorksFor8bitEncodings) { 10382 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" 10383 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" 10384 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" 10385 "\"\xef\xee\xf0\xf3...\"", 10386 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " 10387 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " 10388 "\xef\xee\xf0\xf3...\"", 10389 getLLVMStyleWithColumns(12))); 10390 } 10391 10392 TEST_F(FormatTest, HandlesUTF8BOM) { 10393 EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf")); 10394 EXPECT_EQ("\xef\xbb\xbf#include <iostream>", 10395 format("\xef\xbb\xbf#include <iostream>")); 10396 EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>", 10397 format("\xef\xbb\xbf\n#include <iostream>")); 10398 } 10399 10400 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. 10401 #if !defined(_MSC_VER) 10402 10403 TEST_F(FormatTest, CountsUTF8CharactersProperly) { 10404 verifyFormat("\"Однажды в студёную зимнюю пору...\"", 10405 getLLVMStyleWithColumns(35)); 10406 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", 10407 getLLVMStyleWithColumns(31)); 10408 verifyFormat("// Однажды в студёную зимнюю пору...", 10409 getLLVMStyleWithColumns(36)); 10410 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32)); 10411 verifyFormat("/* Однажды в студёную зимнюю пору... */", 10412 getLLVMStyleWithColumns(39)); 10413 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", 10414 getLLVMStyleWithColumns(35)); 10415 } 10416 10417 TEST_F(FormatTest, SplitsUTF8Strings) { 10418 // Non-printable characters' width is currently considered to be the length in 10419 // bytes in UTF8. The characters can be displayed in very different manner 10420 // (zero-width, single width with a substitution glyph, expanded to their code 10421 // (e.g. "<8d>"), so there's no single correct way to handle them. 10422 EXPECT_EQ("\"aaaaÄ\"\n" 10423 "\"\xc2\x8d\";", 10424 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 10425 EXPECT_EQ("\"aaaaaaaÄ\"\n" 10426 "\"\xc2\x8d\";", 10427 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 10428 EXPECT_EQ("\"Однажды, в \"\n" 10429 "\"студёную \"\n" 10430 "\"зимнюю \"\n" 10431 "\"пору,\"", 10432 format("\"Однажды, в студёную зимнюю пору,\"", 10433 getLLVMStyleWithColumns(13))); 10434 EXPECT_EQ( 10435 "\"一 二 三 \"\n" 10436 "\"四 五六 \"\n" 10437 "\"七 八 九 \"\n" 10438 "\"十\"", 10439 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); 10440 EXPECT_EQ("\"一\t二 \"\n" 10441 "\"\t三 \"\n" 10442 "\"四 五\t六 \"\n" 10443 "\"\t七 \"\n" 10444 "\"八九十\tqq\"", 10445 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", 10446 getLLVMStyleWithColumns(11))); 10447 10448 // UTF8 character in an escape sequence. 10449 EXPECT_EQ("\"aaaaaa\"\n" 10450 "\"\\\xC2\x8D\"", 10451 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10))); 10452 } 10453 10454 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { 10455 EXPECT_EQ("const char *sssss =\n" 10456 " \"一二三四五六七八\\\n" 10457 " 九 十\";", 10458 format("const char *sssss = \"一二三四五六七八\\\n" 10459 " 九 十\";", 10460 getLLVMStyleWithColumns(30))); 10461 } 10462 10463 TEST_F(FormatTest, SplitsUTF8LineComments) { 10464 EXPECT_EQ("// aaaaÄ\xc2\x8d", 10465 format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10))); 10466 EXPECT_EQ("// Я из лесу\n" 10467 "// вышел; был\n" 10468 "// сильный\n" 10469 "// мороз.", 10470 format("// Я из лесу вышел; был сильный мороз.", 10471 getLLVMStyleWithColumns(13))); 10472 EXPECT_EQ("// 一二三\n" 10473 "// 四五六七\n" 10474 "// 八 九\n" 10475 "// 十", 10476 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9))); 10477 } 10478 10479 TEST_F(FormatTest, SplitsUTF8BlockComments) { 10480 EXPECT_EQ("/* Гляжу,\n" 10481 " * поднимается\n" 10482 " * медленно в\n" 10483 " * гору\n" 10484 " * Лошадка,\n" 10485 " * везущая\n" 10486 " * хворосту\n" 10487 " * воз. */", 10488 format("/* Гляжу, поднимается медленно в гору\n" 10489 " * Лошадка, везущая хворосту воз. */", 10490 getLLVMStyleWithColumns(13))); 10491 EXPECT_EQ( 10492 "/* 一二三\n" 10493 " * 四五六七\n" 10494 " * 八 九\n" 10495 " * 十 */", 10496 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9))); 10497 EXPECT_EQ("/* \n" 10498 " * \n" 10499 " * - */", 10500 format("/* - */", getLLVMStyleWithColumns(12))); 10501 } 10502 10503 #endif // _MSC_VER 10504 10505 TEST_F(FormatTest, ConstructorInitializerIndentWidth) { 10506 FormatStyle Style = getLLVMStyle(); 10507 10508 Style.ConstructorInitializerIndentWidth = 4; 10509 verifyFormat( 10510 "SomeClass::Constructor()\n" 10511 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10512 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10513 Style); 10514 10515 Style.ConstructorInitializerIndentWidth = 2; 10516 verifyFormat( 10517 "SomeClass::Constructor()\n" 10518 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10519 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10520 Style); 10521 10522 Style.ConstructorInitializerIndentWidth = 0; 10523 verifyFormat( 10524 "SomeClass::Constructor()\n" 10525 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10526 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10527 Style); 10528 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 10529 verifyFormat( 10530 "SomeLongTemplateVariableName<\n" 10531 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>", 10532 Style); 10533 verifyFormat( 10534 "bool smaller = 1 < bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 10535 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 10536 Style); 10537 } 10538 10539 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) { 10540 FormatStyle Style = getLLVMStyle(); 10541 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 10542 Style.ConstructorInitializerIndentWidth = 4; 10543 verifyFormat("SomeClass::Constructor()\n" 10544 " : a(a)\n" 10545 " , b(b)\n" 10546 " , c(c) {}", 10547 Style); 10548 verifyFormat("SomeClass::Constructor()\n" 10549 " : a(a) {}", 10550 Style); 10551 10552 Style.ColumnLimit = 0; 10553 verifyFormat("SomeClass::Constructor()\n" 10554 " : a(a) {}", 10555 Style); 10556 verifyFormat("SomeClass::Constructor() noexcept\n" 10557 " : a(a) {}", 10558 Style); 10559 verifyFormat("SomeClass::Constructor()\n" 10560 " : a(a)\n" 10561 " , b(b)\n" 10562 " , c(c) {}", 10563 Style); 10564 verifyFormat("SomeClass::Constructor()\n" 10565 " : a(a) {\n" 10566 " foo();\n" 10567 " bar();\n" 10568 "}", 10569 Style); 10570 10571 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 10572 verifyFormat("SomeClass::Constructor()\n" 10573 " : a(a)\n" 10574 " , b(b)\n" 10575 " , c(c) {\n}", 10576 Style); 10577 verifyFormat("SomeClass::Constructor()\n" 10578 " : a(a) {\n}", 10579 Style); 10580 10581 Style.ColumnLimit = 80; 10582 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 10583 Style.ConstructorInitializerIndentWidth = 2; 10584 verifyFormat("SomeClass::Constructor()\n" 10585 " : a(a)\n" 10586 " , b(b)\n" 10587 " , c(c) {}", 10588 Style); 10589 10590 Style.ConstructorInitializerIndentWidth = 0; 10591 verifyFormat("SomeClass::Constructor()\n" 10592 ": a(a)\n" 10593 ", b(b)\n" 10594 ", c(c) {}", 10595 Style); 10596 10597 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 10598 Style.ConstructorInitializerIndentWidth = 4; 10599 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style); 10600 verifyFormat( 10601 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n", 10602 Style); 10603 verifyFormat( 10604 "SomeClass::Constructor()\n" 10605 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", 10606 Style); 10607 Style.ConstructorInitializerIndentWidth = 4; 10608 Style.ColumnLimit = 60; 10609 verifyFormat("SomeClass::Constructor()\n" 10610 " : aaaaaaaa(aaaaaaaa)\n" 10611 " , aaaaaaaa(aaaaaaaa)\n" 10612 " , aaaaaaaa(aaaaaaaa) {}", 10613 Style); 10614 } 10615 10616 TEST_F(FormatTest, Destructors) { 10617 verifyFormat("void F(int &i) { i.~int(); }"); 10618 verifyFormat("void F(int &i) { i->~int(); }"); 10619 } 10620 10621 TEST_F(FormatTest, FormatsWithWebKitStyle) { 10622 FormatStyle Style = getWebKitStyle(); 10623 10624 // Don't indent in outer namespaces. 10625 verifyFormat("namespace outer {\n" 10626 "int i;\n" 10627 "namespace inner {\n" 10628 " int i;\n" 10629 "} // namespace inner\n" 10630 "} // namespace outer\n" 10631 "namespace other_outer {\n" 10632 "int i;\n" 10633 "}", 10634 Style); 10635 10636 // Don't indent case labels. 10637 verifyFormat("switch (variable) {\n" 10638 "case 1:\n" 10639 "case 2:\n" 10640 " doSomething();\n" 10641 " break;\n" 10642 "default:\n" 10643 " ++variable;\n" 10644 "}", 10645 Style); 10646 10647 // Wrap before binary operators. 10648 EXPECT_EQ("void f()\n" 10649 "{\n" 10650 " if (aaaaaaaaaaaaaaaa\n" 10651 " && bbbbbbbbbbbbbbbbbbbbbbbb\n" 10652 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 10653 " return;\n" 10654 "}", 10655 format("void f() {\n" 10656 "if (aaaaaaaaaaaaaaaa\n" 10657 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" 10658 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 10659 "return;\n" 10660 "}", 10661 Style)); 10662 10663 // Allow functions on a single line. 10664 verifyFormat("void f() { return; }", Style); 10665 10666 // Constructor initializers are formatted one per line with the "," on the 10667 // new line. 10668 verifyFormat("Constructor()\n" 10669 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 10670 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" 10671 " aaaaaaaaaaaaaa)\n" 10672 " , aaaaaaaaaaaaaaaaaaaaaaa()\n" 10673 "{\n" 10674 "}", 10675 Style); 10676 verifyFormat("SomeClass::Constructor()\n" 10677 " : a(a)\n" 10678 "{\n" 10679 "}", 10680 Style); 10681 EXPECT_EQ("SomeClass::Constructor()\n" 10682 " : a(a)\n" 10683 "{\n" 10684 "}", 10685 format("SomeClass::Constructor():a(a){}", Style)); 10686 verifyFormat("SomeClass::Constructor()\n" 10687 " : a(a)\n" 10688 " , b(b)\n" 10689 " , c(c)\n" 10690 "{\n" 10691 "}", 10692 Style); 10693 verifyFormat("SomeClass::Constructor()\n" 10694 " : a(a)\n" 10695 "{\n" 10696 " foo();\n" 10697 " bar();\n" 10698 "}", 10699 Style); 10700 10701 // Access specifiers should be aligned left. 10702 verifyFormat("class C {\n" 10703 "public:\n" 10704 " int i;\n" 10705 "};", 10706 Style); 10707 10708 // Do not align comments. 10709 verifyFormat("int a; // Do not\n" 10710 "double b; // align comments.", 10711 Style); 10712 10713 // Do not align operands. 10714 EXPECT_EQ("ASSERT(aaaa\n" 10715 " || bbbb);", 10716 format("ASSERT ( aaaa\n||bbbb);", Style)); 10717 10718 // Accept input's line breaks. 10719 EXPECT_EQ("if (aaaaaaaaaaaaaaa\n" 10720 " || bbbbbbbbbbbbbbb) {\n" 10721 " i++;\n" 10722 "}", 10723 format("if (aaaaaaaaaaaaaaa\n" 10724 "|| bbbbbbbbbbbbbbb) { i++; }", 10725 Style)); 10726 EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n" 10727 " i++;\n" 10728 "}", 10729 format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style)); 10730 10731 // Don't automatically break all macro definitions (llvm.org/PR17842). 10732 verifyFormat("#define aNumber 10", Style); 10733 // However, generally keep the line breaks that the user authored. 10734 EXPECT_EQ("#define aNumber \\\n" 10735 " 10", 10736 format("#define aNumber \\\n" 10737 " 10", 10738 Style)); 10739 10740 // Keep empty and one-element array literals on a single line. 10741 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n" 10742 " copyItems:YES];", 10743 format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n" 10744 "copyItems:YES];", 10745 Style)); 10746 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n" 10747 " copyItems:YES];", 10748 format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n" 10749 " copyItems:YES];", 10750 Style)); 10751 // FIXME: This does not seem right, there should be more indentation before 10752 // the array literal's entries. Nested blocks have the same problem. 10753 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 10754 " @\"a\",\n" 10755 " @\"a\"\n" 10756 "]\n" 10757 " copyItems:YES];", 10758 format("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 10759 " @\"a\",\n" 10760 " @\"a\"\n" 10761 " ]\n" 10762 " copyItems:YES];", 10763 Style)); 10764 EXPECT_EQ( 10765 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 10766 " copyItems:YES];", 10767 format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 10768 " copyItems:YES];", 10769 Style)); 10770 10771 verifyFormat("[self.a b:c c:d];", Style); 10772 EXPECT_EQ("[self.a b:c\n" 10773 " c:d];", 10774 format("[self.a b:c\n" 10775 "c:d];", 10776 Style)); 10777 } 10778 10779 TEST_F(FormatTest, FormatsLambdas) { 10780 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n"); 10781 verifyFormat("int c = [&] { [=] { return b++; }(); }();\n"); 10782 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n"); 10783 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n"); 10784 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n"); 10785 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n"); 10786 verifyFormat("auto c = [a = [b = 42] {}] {};\n"); 10787 verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n"); 10788 verifyFormat("int x = f(*+[] {});"); 10789 verifyFormat("void f() {\n" 10790 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n" 10791 "}\n"); 10792 verifyFormat("void f() {\n" 10793 " other(x.begin(), //\n" 10794 " x.end(), //\n" 10795 " [&](int, int) { return 1; });\n" 10796 "}\n"); 10797 verifyFormat("SomeFunction([]() { // A cool function...\n" 10798 " return 43;\n" 10799 "});"); 10800 EXPECT_EQ("SomeFunction([]() {\n" 10801 "#define A a\n" 10802 " return 43;\n" 10803 "});", 10804 format("SomeFunction([](){\n" 10805 "#define A a\n" 10806 "return 43;\n" 10807 "});")); 10808 verifyFormat("void f() {\n" 10809 " SomeFunction([](decltype(x), A *a) {});\n" 10810 "}"); 10811 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10812 " [](const aaaaaaaaaa &a) { return a; });"); 10813 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n" 10814 " SomeOtherFunctioooooooooooooooooooooooooon();\n" 10815 "});"); 10816 verifyFormat("Constructor()\n" 10817 " : Field([] { // comment\n" 10818 " int i;\n" 10819 " }) {}"); 10820 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n" 10821 " return some_parameter.size();\n" 10822 "};"); 10823 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n" 10824 " [](const string &s) { return s; };"); 10825 verifyFormat("int i = aaaaaa ? 1 //\n" 10826 " : [] {\n" 10827 " return 2; //\n" 10828 " }();"); 10829 verifyFormat("llvm::errs() << \"number of twos is \"\n" 10830 " << std::count_if(v.begin(), v.end(), [](int x) {\n" 10831 " return x == 2; // force break\n" 10832 " });"); 10833 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10834 " [=](int iiiiiiiiiiii) {\n" 10835 " return aaaaaaaaaaaaaaaaaaaaaaa !=\n" 10836 " aaaaaaaaaaaaaaaaaaaaaaa;\n" 10837 " });", 10838 getLLVMStyleWithColumns(60)); 10839 verifyFormat("SomeFunction({[&] {\n" 10840 " // comment\n" 10841 " },\n" 10842 " [&] {\n" 10843 " // comment\n" 10844 " }});"); 10845 verifyFormat("SomeFunction({[&] {\n" 10846 " // comment\n" 10847 "}});"); 10848 verifyFormat("virtual aaaaaaaaaaaaaaaa(std::function<bool()> bbbbbbbbbbbb =\n" 10849 " [&]() { return true; },\n" 10850 " aaaaa aaaaaaaaa);"); 10851 10852 // Lambdas with return types. 10853 verifyFormat("int c = []() -> int { return 2; }();\n"); 10854 verifyFormat("int c = []() -> int * { return 2; }();\n"); 10855 verifyFormat("int c = []() -> vector<int> { return {2}; }();\n"); 10856 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());"); 10857 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};"); 10858 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};"); 10859 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};"); 10860 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};"); 10861 verifyFormat("[a, a]() -> a<1> {};"); 10862 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n" 10863 " int j) -> int {\n" 10864 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n" 10865 "};"); 10866 verifyFormat( 10867 "aaaaaaaaaaaaaaaaaaaaaa(\n" 10868 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n" 10869 " return aaaaaaaaaaaaaaaaa;\n" 10870 " });", 10871 getLLVMStyleWithColumns(70)); 10872 verifyFormat("[]() //\n" 10873 " -> int {\n" 10874 " return 1; //\n" 10875 "};"); 10876 10877 // Multiple lambdas in the same parentheses change indentation rules. 10878 verifyFormat("SomeFunction(\n" 10879 " []() {\n" 10880 " int i = 42;\n" 10881 " return i;\n" 10882 " },\n" 10883 " []() {\n" 10884 " int j = 43;\n" 10885 " return j;\n" 10886 " });"); 10887 10888 // More complex introducers. 10889 verifyFormat("return [i, args...] {};"); 10890 10891 // Not lambdas. 10892 verifyFormat("constexpr char hello[]{\"hello\"};"); 10893 verifyFormat("double &operator[](int i) { return 0; }\n" 10894 "int i;"); 10895 verifyFormat("std::unique_ptr<int[]> foo() {}"); 10896 verifyFormat("int i = a[a][a]->f();"); 10897 verifyFormat("int i = (*b)[a]->f();"); 10898 10899 // Other corner cases. 10900 verifyFormat("void f() {\n" 10901 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n" 10902 " );\n" 10903 "}"); 10904 10905 // Lambdas created through weird macros. 10906 verifyFormat("void f() {\n" 10907 " MACRO((const AA &a) { return 1; });\n" 10908 " MACRO((AA &a) { return 1; });\n" 10909 "}"); 10910 10911 verifyFormat("if (blah_blah(whatever, whatever, [] {\n" 10912 " doo_dah();\n" 10913 " doo_dah();\n" 10914 " })) {\n" 10915 "}"); 10916 verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n" 10917 " doo_dah();\n" 10918 " doo_dah();\n" 10919 " })) {\n" 10920 "}"); 10921 verifyFormat("auto lambda = []() {\n" 10922 " int a = 2\n" 10923 "#if A\n" 10924 " + 2\n" 10925 "#endif\n" 10926 " ;\n" 10927 "};"); 10928 10929 // Lambdas with complex multiline introducers. 10930 verifyFormat( 10931 "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10932 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n" 10933 " -> ::std::unordered_set<\n" 10934 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n" 10935 " //\n" 10936 " });"); 10937 } 10938 10939 TEST_F(FormatTest, FormatsBlocks) { 10940 FormatStyle ShortBlocks = getLLVMStyle(); 10941 ShortBlocks.AllowShortBlocksOnASingleLine = true; 10942 verifyFormat("int (^Block)(int, int);", ShortBlocks); 10943 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks); 10944 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks); 10945 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks); 10946 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks); 10947 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks); 10948 10949 verifyFormat("foo(^{ bar(); });", ShortBlocks); 10950 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks); 10951 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks); 10952 10953 verifyFormat("[operation setCompletionBlock:^{\n" 10954 " [self onOperationDone];\n" 10955 "}];"); 10956 verifyFormat("int i = {[operation setCompletionBlock:^{\n" 10957 " [self onOperationDone];\n" 10958 "}]};"); 10959 verifyFormat("[operation setCompletionBlock:^(int *i) {\n" 10960 " f();\n" 10961 "}];"); 10962 verifyFormat("int a = [operation block:^int(int *i) {\n" 10963 " return 1;\n" 10964 "}];"); 10965 verifyFormat("[myObject doSomethingWith:arg1\n" 10966 " aaa:^int(int *a) {\n" 10967 " return 1;\n" 10968 " }\n" 10969 " bbb:f(a * bbbbbbbb)];"); 10970 10971 verifyFormat("[operation setCompletionBlock:^{\n" 10972 " [self.delegate newDataAvailable];\n" 10973 "}];", 10974 getLLVMStyleWithColumns(60)); 10975 verifyFormat("dispatch_async(_fileIOQueue, ^{\n" 10976 " NSString *path = [self sessionFilePath];\n" 10977 " if (path) {\n" 10978 " // ...\n" 10979 " }\n" 10980 "});"); 10981 verifyFormat("[[SessionService sharedService]\n" 10982 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 10983 " if (window) {\n" 10984 " [self windowDidLoad:window];\n" 10985 " } else {\n" 10986 " [self errorLoadingWindow];\n" 10987 " }\n" 10988 " }];"); 10989 verifyFormat("void (^largeBlock)(void) = ^{\n" 10990 " // ...\n" 10991 "};\n", 10992 getLLVMStyleWithColumns(40)); 10993 verifyFormat("[[SessionService sharedService]\n" 10994 " loadWindowWithCompletionBlock: //\n" 10995 " ^(SessionWindow *window) {\n" 10996 " if (window) {\n" 10997 " [self windowDidLoad:window];\n" 10998 " } else {\n" 10999 " [self errorLoadingWindow];\n" 11000 " }\n" 11001 " }];", 11002 getLLVMStyleWithColumns(60)); 11003 verifyFormat("[myObject doSomethingWith:arg1\n" 11004 " firstBlock:^(Foo *a) {\n" 11005 " // ...\n" 11006 " int i;\n" 11007 " }\n" 11008 " secondBlock:^(Bar *b) {\n" 11009 " // ...\n" 11010 " int i;\n" 11011 " }\n" 11012 " thirdBlock:^Foo(Bar *b) {\n" 11013 " // ...\n" 11014 " int i;\n" 11015 " }];"); 11016 verifyFormat("[myObject doSomethingWith:arg1\n" 11017 " firstBlock:-1\n" 11018 " secondBlock:^(Bar *b) {\n" 11019 " // ...\n" 11020 " int i;\n" 11021 " }];"); 11022 11023 verifyFormat("f(^{\n" 11024 " @autoreleasepool {\n" 11025 " if (a) {\n" 11026 " g();\n" 11027 " }\n" 11028 " }\n" 11029 "});"); 11030 verifyFormat("Block b = ^int *(A *a, B *b) {}"); 11031 verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n" 11032 "};"); 11033 11034 FormatStyle FourIndent = getLLVMStyle(); 11035 FourIndent.ObjCBlockIndentWidth = 4; 11036 verifyFormat("[operation setCompletionBlock:^{\n" 11037 " [self onOperationDone];\n" 11038 "}];", 11039 FourIndent); 11040 } 11041 11042 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) { 11043 FormatStyle ZeroColumn = getLLVMStyle(); 11044 ZeroColumn.ColumnLimit = 0; 11045 11046 verifyFormat("[[SessionService sharedService] " 11047 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 11048 " if (window) {\n" 11049 " [self windowDidLoad:window];\n" 11050 " } else {\n" 11051 " [self errorLoadingWindow];\n" 11052 " }\n" 11053 "}];", 11054 ZeroColumn); 11055 EXPECT_EQ("[[SessionService sharedService]\n" 11056 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 11057 " if (window) {\n" 11058 " [self windowDidLoad:window];\n" 11059 " } else {\n" 11060 " [self errorLoadingWindow];\n" 11061 " }\n" 11062 " }];", 11063 format("[[SessionService sharedService]\n" 11064 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 11065 " if (window) {\n" 11066 " [self windowDidLoad:window];\n" 11067 " } else {\n" 11068 " [self errorLoadingWindow];\n" 11069 " }\n" 11070 "}];", 11071 ZeroColumn)); 11072 verifyFormat("[myObject doSomethingWith:arg1\n" 11073 " firstBlock:^(Foo *a) {\n" 11074 " // ...\n" 11075 " int i;\n" 11076 " }\n" 11077 " secondBlock:^(Bar *b) {\n" 11078 " // ...\n" 11079 " int i;\n" 11080 " }\n" 11081 " thirdBlock:^Foo(Bar *b) {\n" 11082 " // ...\n" 11083 " int i;\n" 11084 " }];", 11085 ZeroColumn); 11086 verifyFormat("f(^{\n" 11087 " @autoreleasepool {\n" 11088 " if (a) {\n" 11089 " g();\n" 11090 " }\n" 11091 " }\n" 11092 "});", 11093 ZeroColumn); 11094 verifyFormat("void (^largeBlock)(void) = ^{\n" 11095 " // ...\n" 11096 "};", 11097 ZeroColumn); 11098 11099 ZeroColumn.AllowShortBlocksOnASingleLine = true; 11100 EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };", 11101 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 11102 ZeroColumn.AllowShortBlocksOnASingleLine = false; 11103 EXPECT_EQ("void (^largeBlock)(void) = ^{\n" 11104 " int i;\n" 11105 "};", 11106 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 11107 } 11108 11109 TEST_F(FormatTest, SupportsCRLF) { 11110 EXPECT_EQ("int a;\r\n" 11111 "int b;\r\n" 11112 "int c;\r\n", 11113 format("int a;\r\n" 11114 " int b;\r\n" 11115 " int c;\r\n", 11116 getLLVMStyle())); 11117 EXPECT_EQ("int a;\r\n" 11118 "int b;\r\n" 11119 "int c;\r\n", 11120 format("int a;\r\n" 11121 " int b;\n" 11122 " int c;\r\n", 11123 getLLVMStyle())); 11124 EXPECT_EQ("int a;\n" 11125 "int b;\n" 11126 "int c;\n", 11127 format("int a;\r\n" 11128 " int b;\n" 11129 " int c;\n", 11130 getLLVMStyle())); 11131 EXPECT_EQ("\"aaaaaaa \"\r\n" 11132 "\"bbbbbbb\";\r\n", 11133 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10))); 11134 EXPECT_EQ("#define A \\\r\n" 11135 " b; \\\r\n" 11136 " c; \\\r\n" 11137 " d;\r\n", 11138 format("#define A \\\r\n" 11139 " b; \\\r\n" 11140 " c; d; \r\n", 11141 getGoogleStyle())); 11142 11143 EXPECT_EQ("/*\r\n" 11144 "multi line block comments\r\n" 11145 "should not introduce\r\n" 11146 "an extra carriage return\r\n" 11147 "*/\r\n", 11148 format("/*\r\n" 11149 "multi line block comments\r\n" 11150 "should not introduce\r\n" 11151 "an extra carriage return\r\n" 11152 "*/\r\n")); 11153 } 11154 11155 TEST_F(FormatTest, MunchSemicolonAfterBlocks) { 11156 verifyFormat("MY_CLASS(C) {\n" 11157 " int i;\n" 11158 " int j;\n" 11159 "};"); 11160 } 11161 11162 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) { 11163 FormatStyle TwoIndent = getLLVMStyleWithColumns(15); 11164 TwoIndent.ContinuationIndentWidth = 2; 11165 11166 EXPECT_EQ("int i =\n" 11167 " longFunction(\n" 11168 " arg);", 11169 format("int i = longFunction(arg);", TwoIndent)); 11170 11171 FormatStyle SixIndent = getLLVMStyleWithColumns(20); 11172 SixIndent.ContinuationIndentWidth = 6; 11173 11174 EXPECT_EQ("int i =\n" 11175 " longFunction(\n" 11176 " arg);", 11177 format("int i = longFunction(arg);", SixIndent)); 11178 } 11179 11180 TEST_F(FormatTest, SpacesInAngles) { 11181 FormatStyle Spaces = getLLVMStyle(); 11182 Spaces.SpacesInAngles = true; 11183 11184 verifyFormat("static_cast< int >(arg);", Spaces); 11185 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces); 11186 verifyFormat("f< int, float >();", Spaces); 11187 verifyFormat("template <> g() {}", Spaces); 11188 verifyFormat("template < std::vector< int > > f() {}", Spaces); 11189 verifyFormat("std::function< void(int, int) > fct;", Spaces); 11190 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }", 11191 Spaces); 11192 11193 Spaces.Standard = FormatStyle::LS_Cpp03; 11194 Spaces.SpacesInAngles = true; 11195 verifyFormat("A< A< int > >();", Spaces); 11196 11197 Spaces.SpacesInAngles = false; 11198 verifyFormat("A<A<int> >();", Spaces); 11199 11200 Spaces.Standard = FormatStyle::LS_Cpp11; 11201 Spaces.SpacesInAngles = true; 11202 verifyFormat("A< A< int > >();", Spaces); 11203 11204 Spaces.SpacesInAngles = false; 11205 verifyFormat("A<A<int>>();", Spaces); 11206 } 11207 11208 TEST_F(FormatTest, SpaceAfterTemplateKeyword) { 11209 FormatStyle Style = getLLVMStyle(); 11210 Style.SpaceAfterTemplateKeyword = false; 11211 verifyFormat("template<int> void foo();", Style); 11212 } 11213 11214 TEST_F(FormatTest, TripleAngleBrackets) { 11215 verifyFormat("f<<<1, 1>>>();"); 11216 verifyFormat("f<<<1, 1, 1, s>>>();"); 11217 verifyFormat("f<<<a, b, c, d>>>();"); 11218 EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();")); 11219 verifyFormat("f<param><<<1, 1>>>();"); 11220 verifyFormat("f<1><<<1, 1>>>();"); 11221 EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();")); 11222 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 11223 "aaaaaaaaaaa<<<\n 1, 1>>>();"); 11224 verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n" 11225 " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();"); 11226 } 11227 11228 TEST_F(FormatTest, MergeLessLessAtEnd) { 11229 verifyFormat("<<"); 11230 EXPECT_EQ("< < <", format("\\\n<<<")); 11231 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 11232 "aaallvm::outs() <<"); 11233 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 11234 "aaaallvm::outs()\n <<"); 11235 } 11236 11237 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) { 11238 std::string code = "#if A\n" 11239 "#if B\n" 11240 "a.\n" 11241 "#endif\n" 11242 " a = 1;\n" 11243 "#else\n" 11244 "#endif\n" 11245 "#if C\n" 11246 "#else\n" 11247 "#endif\n"; 11248 EXPECT_EQ(code, format(code)); 11249 } 11250 11251 TEST_F(FormatTest, HandleConflictMarkers) { 11252 // Git/SVN conflict markers. 11253 EXPECT_EQ("int a;\n" 11254 "void f() {\n" 11255 " callme(some(parameter1,\n" 11256 "<<<<<<< text by the vcs\n" 11257 " parameter2),\n" 11258 "||||||| text by the vcs\n" 11259 " parameter2),\n" 11260 " parameter3,\n" 11261 "======= text by the vcs\n" 11262 " parameter2, parameter3),\n" 11263 ">>>>>>> text by the vcs\n" 11264 " otherparameter);\n", 11265 format("int a;\n" 11266 "void f() {\n" 11267 " callme(some(parameter1,\n" 11268 "<<<<<<< text by the vcs\n" 11269 " parameter2),\n" 11270 "||||||| text by the vcs\n" 11271 " parameter2),\n" 11272 " parameter3,\n" 11273 "======= text by the vcs\n" 11274 " parameter2,\n" 11275 " parameter3),\n" 11276 ">>>>>>> text by the vcs\n" 11277 " otherparameter);\n")); 11278 11279 // Perforce markers. 11280 EXPECT_EQ("void f() {\n" 11281 " function(\n" 11282 ">>>> text by the vcs\n" 11283 " parameter,\n" 11284 "==== text by the vcs\n" 11285 " parameter,\n" 11286 "==== text by the vcs\n" 11287 " parameter,\n" 11288 "<<<< text by the vcs\n" 11289 " parameter);\n", 11290 format("void f() {\n" 11291 " function(\n" 11292 ">>>> text by the vcs\n" 11293 " parameter,\n" 11294 "==== text by the vcs\n" 11295 " parameter,\n" 11296 "==== text by the vcs\n" 11297 " parameter,\n" 11298 "<<<< text by the vcs\n" 11299 " parameter);\n")); 11300 11301 EXPECT_EQ("<<<<<<<\n" 11302 "|||||||\n" 11303 "=======\n" 11304 ">>>>>>>", 11305 format("<<<<<<<\n" 11306 "|||||||\n" 11307 "=======\n" 11308 ">>>>>>>")); 11309 11310 EXPECT_EQ("<<<<<<<\n" 11311 "|||||||\n" 11312 "int i;\n" 11313 "=======\n" 11314 ">>>>>>>", 11315 format("<<<<<<<\n" 11316 "|||||||\n" 11317 "int i;\n" 11318 "=======\n" 11319 ">>>>>>>")); 11320 11321 // FIXME: Handle parsing of macros around conflict markers correctly: 11322 EXPECT_EQ("#define Macro \\\n" 11323 "<<<<<<<\n" 11324 "Something \\\n" 11325 "|||||||\n" 11326 "Else \\\n" 11327 "=======\n" 11328 "Other \\\n" 11329 ">>>>>>>\n" 11330 " End int i;\n", 11331 format("#define Macro \\\n" 11332 "<<<<<<<\n" 11333 " Something \\\n" 11334 "|||||||\n" 11335 " Else \\\n" 11336 "=======\n" 11337 " Other \\\n" 11338 ">>>>>>>\n" 11339 " End\n" 11340 "int i;\n")); 11341 } 11342 11343 TEST_F(FormatTest, DisableRegions) { 11344 EXPECT_EQ("int i;\n" 11345 "// clang-format off\n" 11346 " int j;\n" 11347 "// clang-format on\n" 11348 "int k;", 11349 format(" int i;\n" 11350 " // clang-format off\n" 11351 " int j;\n" 11352 " // clang-format on\n" 11353 " int k;")); 11354 EXPECT_EQ("int i;\n" 11355 "/* clang-format off */\n" 11356 " int j;\n" 11357 "/* clang-format on */\n" 11358 "int k;", 11359 format(" int i;\n" 11360 " /* clang-format off */\n" 11361 " int j;\n" 11362 " /* clang-format on */\n" 11363 " int k;")); 11364 11365 // Don't reflow comments within disabled regions. 11366 EXPECT_EQ( 11367 "// clang-format off\n" 11368 "// long long long long long long line\n" 11369 "/* clang-format on */\n" 11370 "/* long long long\n" 11371 " * long long long\n" 11372 " * line */\n" 11373 "int i;\n" 11374 "/* clang-format off */\n" 11375 "/* long long long long long long line */\n", 11376 format("// clang-format off\n" 11377 "// long long long long long long line\n" 11378 "/* clang-format on */\n" 11379 "/* long long long long long long line */\n" 11380 "int i;\n" 11381 "/* clang-format off */\n" 11382 "/* long long long long long long line */\n", 11383 getLLVMStyleWithColumns(20))); 11384 } 11385 11386 TEST_F(FormatTest, DoNotCrashOnInvalidInput) { 11387 format("? ) ="); 11388 verifyNoCrash("#define a\\\n /**/}"); 11389 } 11390 11391 TEST_F(FormatTest, FormatsTableGenCode) { 11392 FormatStyle Style = getLLVMStyle(); 11393 Style.Language = FormatStyle::LK_TableGen; 11394 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style); 11395 } 11396 11397 TEST_F(FormatTest, ArrayOfTemplates) { 11398 EXPECT_EQ("auto a = new unique_ptr<int>[10];", 11399 format("auto a = new unique_ptr<int > [ 10];")); 11400 11401 FormatStyle Spaces = getLLVMStyle(); 11402 Spaces.SpacesInSquareBrackets = true; 11403 EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];", 11404 format("auto a = new unique_ptr<int > [10];", Spaces)); 11405 } 11406 11407 TEST_F(FormatTest, ArrayAsTemplateType) { 11408 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;", 11409 format("auto a = unique_ptr < Foo < Bar>[ 10]> ;")); 11410 11411 FormatStyle Spaces = getLLVMStyle(); 11412 Spaces.SpacesInSquareBrackets = true; 11413 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;", 11414 format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces)); 11415 } 11416 11417 TEST_F(FormatTest, NoSpaceAfterSuper) { 11418 verifyFormat("__super::FooBar();"); 11419 } 11420 11421 TEST(FormatStyle, GetStyleOfFile) { 11422 vfs::InMemoryFileSystem FS; 11423 // Test 1: format file in the same directory. 11424 ASSERT_TRUE( 11425 FS.addFile("/a/.clang-format", 0, 11426 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM"))); 11427 ASSERT_TRUE( 11428 FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 11429 auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS); 11430 ASSERT_TRUE((bool)Style1); 11431 ASSERT_EQ(*Style1, getLLVMStyle()); 11432 11433 // Test 2.1: fallback to default. 11434 ASSERT_TRUE( 11435 FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 11436 auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS); 11437 ASSERT_TRUE((bool)Style2); 11438 ASSERT_EQ(*Style2, getMozillaStyle()); 11439 11440 // Test 2.2: no format on 'none' fallback style. 11441 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 11442 ASSERT_TRUE((bool)Style2); 11443 ASSERT_EQ(*Style2, getNoStyle()); 11444 11445 // Test 2.3: format if config is found with no based style while fallback is 11446 // 'none'. 11447 ASSERT_TRUE(FS.addFile("/b/.clang-format", 0, 11448 llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2"))); 11449 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 11450 ASSERT_TRUE((bool)Style2); 11451 ASSERT_EQ(*Style2, getLLVMStyle()); 11452 11453 // Test 2.4: format if yaml with no based style, while fallback is 'none'. 11454 Style2 = getStyle("{}", "a.h", "none", "", &FS); 11455 ASSERT_TRUE((bool)Style2); 11456 ASSERT_EQ(*Style2, getLLVMStyle()); 11457 11458 // Test 3: format file in parent directory. 11459 ASSERT_TRUE( 11460 FS.addFile("/c/.clang-format", 0, 11461 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 11462 ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0, 11463 llvm::MemoryBuffer::getMemBuffer("int i;"))); 11464 auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS); 11465 ASSERT_TRUE((bool)Style3); 11466 ASSERT_EQ(*Style3, getGoogleStyle()); 11467 11468 // Test 4: error on invalid fallback style 11469 auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS); 11470 ASSERT_FALSE((bool)Style4); 11471 llvm::consumeError(Style4.takeError()); 11472 11473 // Test 5: error on invalid yaml on command line 11474 auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS); 11475 ASSERT_FALSE((bool)Style5); 11476 llvm::consumeError(Style5.takeError()); 11477 11478 // Test 6: error on invalid style 11479 auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS); 11480 ASSERT_FALSE((bool)Style6); 11481 llvm::consumeError(Style6.takeError()); 11482 11483 // Test 7: found config file, error on parsing it 11484 ASSERT_TRUE( 11485 FS.addFile("/d/.clang-format", 0, 11486 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n" 11487 "InvalidKey: InvalidValue"))); 11488 ASSERT_TRUE( 11489 FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 11490 auto Style7 = getStyle("file", "/d/.clang-format", "LLVM", "", &FS); 11491 ASSERT_FALSE((bool)Style7); 11492 llvm::consumeError(Style7.takeError()); 11493 } 11494 11495 TEST_F(ReplacementTest, FormatCodeAfterReplacements) { 11496 // Column limit is 20. 11497 std::string Code = "Type *a =\n" 11498 " new Type();\n" 11499 "g(iiiii, 0, jjjjj,\n" 11500 " 0, kkkkk, 0, mm);\n" 11501 "int bad = format ;"; 11502 std::string Expected = "auto a = new Type();\n" 11503 "g(iiiii, nullptr,\n" 11504 " jjjjj, nullptr,\n" 11505 " kkkkk, nullptr,\n" 11506 " mm);\n" 11507 "int bad = format ;"; 11508 FileID ID = Context.createInMemoryFile("format.cpp", Code); 11509 tooling::Replacements Replaces = toReplacements( 11510 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6, 11511 "auto "), 11512 tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1, 11513 "nullptr"), 11514 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1, 11515 "nullptr"), 11516 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1, 11517 "nullptr")}); 11518 11519 format::FormatStyle Style = format::getLLVMStyle(); 11520 Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility. 11521 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 11522 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 11523 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 11524 auto Result = applyAllReplacements(Code, *FormattedReplaces); 11525 EXPECT_TRUE(static_cast<bool>(Result)); 11526 EXPECT_EQ(Expected, *Result); 11527 } 11528 11529 TEST_F(ReplacementTest, SortIncludesAfterReplacement) { 11530 std::string Code = "#include \"a.h\"\n" 11531 "#include \"c.h\"\n" 11532 "\n" 11533 "int main() {\n" 11534 " return 0;\n" 11535 "}"; 11536 std::string Expected = "#include \"a.h\"\n" 11537 "#include \"b.h\"\n" 11538 "#include \"c.h\"\n" 11539 "\n" 11540 "int main() {\n" 11541 " return 0;\n" 11542 "}"; 11543 FileID ID = Context.createInMemoryFile("fix.cpp", Code); 11544 tooling::Replacements Replaces = toReplacements( 11545 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0, 11546 "#include \"b.h\"\n")}); 11547 11548 format::FormatStyle Style = format::getLLVMStyle(); 11549 Style.SortIncludes = true; 11550 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 11551 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 11552 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 11553 auto Result = applyAllReplacements(Code, *FormattedReplaces); 11554 EXPECT_TRUE(static_cast<bool>(Result)); 11555 EXPECT_EQ(Expected, *Result); 11556 } 11557 11558 TEST_F(FormatTest, FormatSortsUsingDeclarations) { 11559 EXPECT_EQ("using std::cin;\n" 11560 "using std::cout;", 11561 format("using std::cout;\n" 11562 "using std::cin;", getGoogleStyle())); 11563 } 11564 11565 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) { 11566 format::FormatStyle Style = format::getLLVMStyle(); 11567 Style.Standard = FormatStyle::LS_Cpp03; 11568 // cpp03 recognize this string as identifier u8 and literal character 'a' 11569 EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style)); 11570 } 11571 11572 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) { 11573 // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers 11574 // all modes, including C++11, C++14 and C++17 11575 EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';")); 11576 } 11577 11578 TEST_F(FormatTest, DoNotFormatLikelyXml) { 11579 EXPECT_EQ("<!-- ;> -->", 11580 format("<!-- ;> -->", getGoogleStyle())); 11581 EXPECT_EQ(" <!-- >; -->", 11582 format(" <!-- >; -->", getGoogleStyle())); 11583 } 11584 11585 TEST_F(FormatTest, StructuredBindings) { 11586 // Structured bindings is a C++17 feature. 11587 // all modes, including C++11, C++14 and C++17 11588 verifyFormat("auto [a, b] = f();"); 11589 EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();")); 11590 EXPECT_EQ("const auto [a, b] = f();", format("const auto[a, b] = f();")); 11591 EXPECT_EQ("auto const [a, b] = f();", format("auto const[a, b] = f();")); 11592 EXPECT_EQ("auto const volatile [a, b] = f();", 11593 format("auto const volatile[a, b] = f();")); 11594 EXPECT_EQ("auto [a, b, c] = f();", format("auto [ a , b,c ] = f();")); 11595 EXPECT_EQ("auto &[a, b, c] = f();", 11596 format("auto &[ a , b,c ] = f();")); 11597 EXPECT_EQ("auto &&[a, b, c] = f();", 11598 format("auto &&[ a , b,c ] = f();")); 11599 EXPECT_EQ("auto const &[a, b] = f();", format("auto const&[a, b] = f();")); 11600 EXPECT_EQ("auto const volatile &&[a, b] = f();", 11601 format("auto const volatile &&[a, b] = f();")); 11602 EXPECT_EQ("auto const &&[a, b] = f();", format("auto const && [a, b] = f();")); 11603 EXPECT_EQ("const auto &[a, b] = f();", format("const auto & [a, b] = f();")); 11604 EXPECT_EQ("const auto volatile &&[a, b] = f();", 11605 format("const auto volatile &&[a, b] = f();")); 11606 EXPECT_EQ("volatile const auto &&[a, b] = f();", 11607 format("volatile const auto &&[a, b] = f();")); 11608 EXPECT_EQ("const auto &&[a, b] = f();", format("const auto && [a, b] = f();")); 11609 11610 // Make sure we don't mistake structured bindings for lambdas. 11611 FormatStyle PointerMiddle = getLLVMStyle(); 11612 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 11613 verifyFormat("auto [a1, b]{A * i};", getGoogleStyle()); 11614 verifyFormat("auto [a2, b]{A * i};", getLLVMStyle()); 11615 verifyFormat("auto [a3, b]{A * i};", PointerMiddle); 11616 verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle()); 11617 verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle()); 11618 verifyFormat("auto const [a3, b]{A * i};", PointerMiddle); 11619 verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle()); 11620 verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle()); 11621 verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle); 11622 verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle()); 11623 verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle()); 11624 verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle); 11625 11626 EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}", 11627 format("for (const auto && [a, b] : some_range) {\n}")); 11628 EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}", 11629 format("for (const auto & [a, b] : some_range) {\n}")); 11630 EXPECT_EQ("for (const auto [a, b] : some_range) {\n}", 11631 format("for (const auto[a, b] : some_range) {\n}")); 11632 EXPECT_EQ("auto [x, y](expr);", format("auto[x,y] (expr);")); 11633 EXPECT_EQ("auto &[x, y](expr);", format("auto & [x,y] (expr);")); 11634 EXPECT_EQ("auto &&[x, y](expr);", format("auto && [x,y] (expr);")); 11635 EXPECT_EQ("auto const &[x, y](expr);", format("auto const & [x,y] (expr);")); 11636 EXPECT_EQ("auto const &&[x, y](expr);", format("auto const && [x,y] (expr);")); 11637 EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y] {expr};")); 11638 EXPECT_EQ("auto const &[x, y]{expr};", format("auto const & [x,y] {expr};")); 11639 EXPECT_EQ("auto const &&[x, y]{expr};", format("auto const && [x,y] {expr};")); 11640 11641 format::FormatStyle Spaces = format::getLLVMStyle(); 11642 Spaces.SpacesInSquareBrackets = true; 11643 verifyFormat("auto [ a, b ] = f();", Spaces); 11644 verifyFormat("auto &&[ a, b ] = f();", Spaces); 11645 verifyFormat("auto &[ a, b ] = f();", Spaces); 11646 verifyFormat("auto const &&[ a, b ] = f();", Spaces); 11647 verifyFormat("auto const &[ a, b ] = f();", Spaces); 11648 } 11649 11650 } // end namespace 11651 } // end namespace format 11652 } // end namespace clang 11653