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 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) { 7106 FormatStyle Style = getLLVMStyle(); 7107 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 7108 verifyFormat("#ifdef A\n" 7109 "int f() {}\n" 7110 "#else\n" 7111 "int g() {}\n" 7112 "#endif", 7113 Style); 7114 } 7115 7116 TEST_F(FormatTest, SplitEmptyClass) { 7117 FormatStyle Style = getLLVMStyle(); 7118 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7119 Style.BraceWrapping.AfterClass = true; 7120 Style.BraceWrapping.SplitEmptyRecord = false; 7121 7122 verifyFormat("class Foo\n" 7123 "{};", 7124 Style); 7125 verifyFormat("/* something */ class Foo\n" 7126 "{};", 7127 Style); 7128 verifyFormat("template <typename X> class Foo\n" 7129 "{};", 7130 Style); 7131 verifyFormat("class Foo\n" 7132 "{\n" 7133 " Foo();\n" 7134 "};", 7135 Style); 7136 verifyFormat("typedef class Foo\n" 7137 "{\n" 7138 "} Foo_t;", 7139 Style); 7140 } 7141 7142 TEST_F(FormatTest, SplitEmptyStruct) { 7143 FormatStyle Style = getLLVMStyle(); 7144 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7145 Style.BraceWrapping.AfterStruct = true; 7146 Style.BraceWrapping.SplitEmptyRecord = false; 7147 7148 verifyFormat("struct Foo\n" 7149 "{};", 7150 Style); 7151 verifyFormat("/* something */ struct Foo\n" 7152 "{};", 7153 Style); 7154 verifyFormat("template <typename X> struct Foo\n" 7155 "{};", 7156 Style); 7157 verifyFormat("struct Foo\n" 7158 "{\n" 7159 " Foo();\n" 7160 "};", 7161 Style); 7162 verifyFormat("typedef struct Foo\n" 7163 "{\n" 7164 "} Foo_t;", 7165 Style); 7166 //typedef struct Bar {} Bar_t; 7167 } 7168 7169 TEST_F(FormatTest, SplitEmptyUnion) { 7170 FormatStyle Style = getLLVMStyle(); 7171 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7172 Style.BraceWrapping.AfterUnion = true; 7173 Style.BraceWrapping.SplitEmptyRecord = false; 7174 7175 verifyFormat("union Foo\n" 7176 "{};", 7177 Style); 7178 verifyFormat("/* something */ union Foo\n" 7179 "{};", 7180 Style); 7181 verifyFormat("union Foo\n" 7182 "{\n" 7183 " A,\n" 7184 "};", 7185 Style); 7186 verifyFormat("typedef union Foo\n" 7187 "{\n" 7188 "} Foo_t;", 7189 Style); 7190 } 7191 7192 TEST_F(FormatTest, SplitEmptyNamespace) { 7193 FormatStyle Style = getLLVMStyle(); 7194 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7195 Style.BraceWrapping.AfterNamespace = true; 7196 Style.BraceWrapping.SplitEmptyNamespace = false; 7197 7198 verifyFormat("namespace Foo\n" 7199 "{};", 7200 Style); 7201 verifyFormat("/* something */ namespace Foo\n" 7202 "{};", 7203 Style); 7204 verifyFormat("inline namespace Foo\n" 7205 "{};", 7206 Style); 7207 verifyFormat("namespace Foo\n" 7208 "{\n" 7209 "void Bar();\n" 7210 "};", 7211 Style); 7212 } 7213 7214 TEST_F(FormatTest, NeverMergeShortRecords) { 7215 FormatStyle Style = getLLVMStyle(); 7216 7217 verifyFormat("class Foo {\n" 7218 " Foo();\n" 7219 "};", 7220 Style); 7221 verifyFormat("typedef class Foo {\n" 7222 " Foo();\n" 7223 "} Foo_t;", 7224 Style); 7225 verifyFormat("struct Foo {\n" 7226 " Foo();\n" 7227 "};", 7228 Style); 7229 verifyFormat("typedef struct Foo {\n" 7230 " Foo();\n" 7231 "} Foo_t;", 7232 Style); 7233 verifyFormat("union Foo {\n" 7234 " A,\n" 7235 "};", 7236 Style); 7237 verifyFormat("typedef union Foo {\n" 7238 " A,\n" 7239 "} Foo_t;", 7240 Style); 7241 verifyFormat("namespace Foo {\n" 7242 "void Bar();\n" 7243 "};", 7244 Style); 7245 7246 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7247 Style.BraceWrapping.AfterClass = true; 7248 Style.BraceWrapping.AfterStruct = true; 7249 Style.BraceWrapping.AfterUnion = true; 7250 Style.BraceWrapping.AfterNamespace = true; 7251 verifyFormat("class Foo\n" 7252 "{\n" 7253 " Foo();\n" 7254 "};", 7255 Style); 7256 verifyFormat("typedef class Foo\n" 7257 "{\n" 7258 " Foo();\n" 7259 "} Foo_t;", 7260 Style); 7261 verifyFormat("struct Foo\n" 7262 "{\n" 7263 " Foo();\n" 7264 "};", 7265 Style); 7266 verifyFormat("typedef struct Foo\n" 7267 "{\n" 7268 " Foo();\n" 7269 "} Foo_t;", 7270 Style); 7271 verifyFormat("union Foo\n" 7272 "{\n" 7273 " A,\n" 7274 "};", 7275 Style); 7276 verifyFormat("typedef union Foo\n" 7277 "{\n" 7278 " A,\n" 7279 "} Foo_t;", 7280 Style); 7281 verifyFormat("namespace Foo\n" 7282 "{\n" 7283 "void Bar();\n" 7284 "};", 7285 Style); 7286 } 7287 7288 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 7289 // Elaborate type variable declarations. 7290 verifyFormat("struct foo a = {bar};\nint n;"); 7291 verifyFormat("class foo a = {bar};\nint n;"); 7292 verifyFormat("union foo a = {bar};\nint n;"); 7293 7294 // Elaborate types inside function definitions. 7295 verifyFormat("struct foo f() {}\nint n;"); 7296 verifyFormat("class foo f() {}\nint n;"); 7297 verifyFormat("union foo f() {}\nint n;"); 7298 7299 // Templates. 7300 verifyFormat("template <class X> void f() {}\nint n;"); 7301 verifyFormat("template <struct X> void f() {}\nint n;"); 7302 verifyFormat("template <union X> void f() {}\nint n;"); 7303 7304 // Actual definitions... 7305 verifyFormat("struct {\n} n;"); 7306 verifyFormat( 7307 "template <template <class T, class Y>, class Z> class X {\n} n;"); 7308 verifyFormat("union Z {\n int n;\n} x;"); 7309 verifyFormat("class MACRO Z {\n} n;"); 7310 verifyFormat("class MACRO(X) Z {\n} n;"); 7311 verifyFormat("class __attribute__(X) Z {\n} n;"); 7312 verifyFormat("class __declspec(X) Z {\n} n;"); 7313 verifyFormat("class A##B##C {\n} n;"); 7314 verifyFormat("class alignas(16) Z {\n} n;"); 7315 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;"); 7316 verifyFormat("class MACROA MACRO(X) Z {\n} n;"); 7317 7318 // Redefinition from nested context: 7319 verifyFormat("class A::B::C {\n} n;"); 7320 7321 // Template definitions. 7322 verifyFormat( 7323 "template <typename F>\n" 7324 "Matcher(const Matcher<F> &Other,\n" 7325 " typename enable_if_c<is_base_of<F, T>::value &&\n" 7326 " !is_same<F, T>::value>::type * = 0)\n" 7327 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}"); 7328 7329 // FIXME: This is still incorrectly handled at the formatter side. 7330 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};"); 7331 verifyFormat("int i = SomeFunction(a<b, a> b);"); 7332 7333 // FIXME: 7334 // This now gets parsed incorrectly as class definition. 7335 // verifyFormat("class A<int> f() {\n}\nint n;"); 7336 7337 // Elaborate types where incorrectly parsing the structural element would 7338 // break the indent. 7339 verifyFormat("if (true)\n" 7340 " class X x;\n" 7341 "else\n" 7342 " f();\n"); 7343 7344 // This is simply incomplete. Formatting is not important, but must not crash. 7345 verifyFormat("class A:"); 7346 } 7347 7348 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 7349 EXPECT_EQ("#error Leave all white!!!!! space* alone!\n", 7350 format("#error Leave all white!!!!! space* alone!\n")); 7351 EXPECT_EQ( 7352 "#warning Leave all white!!!!! space* alone!\n", 7353 format("#warning Leave all white!!!!! space* alone!\n")); 7354 EXPECT_EQ("#error 1", format(" # error 1")); 7355 EXPECT_EQ("#warning 1", format(" # warning 1")); 7356 } 7357 7358 TEST_F(FormatTest, FormatHashIfExpressions) { 7359 verifyFormat("#if AAAA && BBBB"); 7360 verifyFormat("#if (AAAA && BBBB)"); 7361 verifyFormat("#elif (AAAA && BBBB)"); 7362 // FIXME: Come up with a better indentation for #elif. 7363 verifyFormat( 7364 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" 7365 " defined(BBBBBBBB)\n" 7366 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" 7367 " defined(BBBBBBBB)\n" 7368 "#endif", 7369 getLLVMStyleWithColumns(65)); 7370 } 7371 7372 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 7373 FormatStyle AllowsMergedIf = getGoogleStyle(); 7374 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 7375 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 7376 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 7377 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 7378 EXPECT_EQ("if (true) return 42;", 7379 format("if (true)\nreturn 42;", AllowsMergedIf)); 7380 FormatStyle ShortMergedIf = AllowsMergedIf; 7381 ShortMergedIf.ColumnLimit = 25; 7382 verifyFormat("#define A \\\n" 7383 " if (true) return 42;", 7384 ShortMergedIf); 7385 verifyFormat("#define A \\\n" 7386 " f(); \\\n" 7387 " if (true)\n" 7388 "#define B", 7389 ShortMergedIf); 7390 verifyFormat("#define A \\\n" 7391 " f(); \\\n" 7392 " if (true)\n" 7393 "g();", 7394 ShortMergedIf); 7395 verifyFormat("{\n" 7396 "#ifdef A\n" 7397 " // Comment\n" 7398 " if (true) continue;\n" 7399 "#endif\n" 7400 " // Comment\n" 7401 " if (true) continue;\n" 7402 "}", 7403 ShortMergedIf); 7404 ShortMergedIf.ColumnLimit = 33; 7405 verifyFormat("#define A \\\n" 7406 " if constexpr (true) return 42;", 7407 ShortMergedIf); 7408 ShortMergedIf.ColumnLimit = 29; 7409 verifyFormat("#define A \\\n" 7410 " if (aaaaaaaaaa) return 1; \\\n" 7411 " return 2;", 7412 ShortMergedIf); 7413 ShortMergedIf.ColumnLimit = 28; 7414 verifyFormat("#define A \\\n" 7415 " if (aaaaaaaaaa) \\\n" 7416 " return 1; \\\n" 7417 " return 2;", 7418 ShortMergedIf); 7419 verifyFormat("#define A \\\n" 7420 " if constexpr (aaaaaaa) \\\n" 7421 " return 1; \\\n" 7422 " return 2;", 7423 ShortMergedIf); 7424 } 7425 7426 TEST_F(FormatTest, FormatStarDependingOnContext) { 7427 verifyFormat("void f(int *a);"); 7428 verifyFormat("void f() { f(fint * b); }"); 7429 verifyFormat("class A {\n void f(int *a);\n};"); 7430 verifyFormat("class A {\n int *a;\n};"); 7431 verifyFormat("namespace a {\n" 7432 "namespace b {\n" 7433 "class A {\n" 7434 " void f() {}\n" 7435 " int *a;\n" 7436 "};\n" 7437 "} // namespace b\n" 7438 "} // namespace a"); 7439 } 7440 7441 TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 7442 verifyFormat("while"); 7443 verifyFormat("operator"); 7444 } 7445 7446 TEST_F(FormatTest, SkipsDeeplyNestedLines) { 7447 // This code would be painfully slow to format if we didn't skip it. 7448 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 7449 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 7450 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 7451 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 7452 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 7453 "A(1, 1)\n" 7454 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x 7455 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7456 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7457 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7458 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7459 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7460 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7461 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7462 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7463 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n"); 7464 // Deeply nested part is untouched, rest is formatted. 7465 EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n", 7466 format(std::string("int i;\n") + Code + "int j;\n", 7467 getLLVMStyle(), SC_ExpectIncomplete)); 7468 } 7469 7470 //===----------------------------------------------------------------------===// 7471 // Objective-C tests. 7472 //===----------------------------------------------------------------------===// 7473 7474 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 7475 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 7476 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 7477 format("-(NSUInteger)indexOfObject:(id)anObject;")); 7478 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 7479 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 7480 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 7481 format("-(NSInteger)Method3:(id)anObject;")); 7482 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 7483 format("-(NSInteger)Method4:(id)anObject;")); 7484 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 7485 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 7486 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 7487 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 7488 EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject " 7489 "forAllCells:(BOOL)flag;", 7490 format("- (void)sendAction:(SEL)aSelector to:(id)anObject " 7491 "forAllCells:(BOOL)flag;")); 7492 7493 // Very long objectiveC method declaration. 7494 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 7495 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;"); 7496 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 7497 " inRange:(NSRange)range\n" 7498 " outRange:(NSRange)out_range\n" 7499 " outRange1:(NSRange)out_range1\n" 7500 " outRange2:(NSRange)out_range2\n" 7501 " outRange3:(NSRange)out_range3\n" 7502 " outRange4:(NSRange)out_range4\n" 7503 " outRange5:(NSRange)out_range5\n" 7504 " outRange6:(NSRange)out_range6\n" 7505 " outRange7:(NSRange)out_range7\n" 7506 " outRange8:(NSRange)out_range8\n" 7507 " outRange9:(NSRange)out_range9;"); 7508 7509 // When the function name has to be wrapped. 7510 FormatStyle Style = getLLVMStyle(); 7511 Style.IndentWrappedFunctionNames = false; 7512 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 7513 "veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 7514 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 7515 "}", 7516 Style); 7517 Style.IndentWrappedFunctionNames = true; 7518 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 7519 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 7520 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 7521 "}", 7522 Style); 7523 7524 verifyFormat("- (int)sum:(vector<int>)numbers;"); 7525 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 7526 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 7527 // protocol lists (but not for template classes): 7528 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 7529 7530 verifyFormat("- (int (*)())foo:(int (*)())f;"); 7531 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); 7532 7533 // If there's no return type (very rare in practice!), LLVM and Google style 7534 // agree. 7535 verifyFormat("- foo;"); 7536 verifyFormat("- foo:(int)f;"); 7537 verifyGoogleFormat("- foo:(int)foo;"); 7538 } 7539 7540 7541 TEST_F(FormatTest, BreaksStringLiterals) { 7542 EXPECT_EQ("\"some text \"\n" 7543 "\"other\";", 7544 format("\"some text other\";", getLLVMStyleWithColumns(12))); 7545 EXPECT_EQ("\"some text \"\n" 7546 "\"other\";", 7547 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); 7548 EXPECT_EQ( 7549 "#define A \\\n" 7550 " \"some \" \\\n" 7551 " \"text \" \\\n" 7552 " \"other\";", 7553 format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); 7554 EXPECT_EQ( 7555 "#define A \\\n" 7556 " \"so \" \\\n" 7557 " \"text \" \\\n" 7558 " \"other\";", 7559 format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); 7560 7561 EXPECT_EQ("\"some text\"", 7562 format("\"some text\"", getLLVMStyleWithColumns(1))); 7563 EXPECT_EQ("\"some text\"", 7564 format("\"some text\"", getLLVMStyleWithColumns(11))); 7565 EXPECT_EQ("\"some \"\n" 7566 "\"text\"", 7567 format("\"some text\"", getLLVMStyleWithColumns(10))); 7568 EXPECT_EQ("\"some \"\n" 7569 "\"text\"", 7570 format("\"some text\"", getLLVMStyleWithColumns(7))); 7571 EXPECT_EQ("\"some\"\n" 7572 "\" tex\"\n" 7573 "\"t\"", 7574 format("\"some text\"", getLLVMStyleWithColumns(6))); 7575 EXPECT_EQ("\"some\"\n" 7576 "\" tex\"\n" 7577 "\" and\"", 7578 format("\"some tex and\"", getLLVMStyleWithColumns(6))); 7579 EXPECT_EQ("\"some\"\n" 7580 "\"/tex\"\n" 7581 "\"/and\"", 7582 format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); 7583 7584 EXPECT_EQ("variable =\n" 7585 " \"long string \"\n" 7586 " \"literal\";", 7587 format("variable = \"long string literal\";", 7588 getLLVMStyleWithColumns(20))); 7589 7590 EXPECT_EQ("variable = f(\n" 7591 " \"long string \"\n" 7592 " \"literal\",\n" 7593 " short,\n" 7594 " loooooooooooooooooooong);", 7595 format("variable = f(\"long string literal\", short, " 7596 "loooooooooooooooooooong);", 7597 getLLVMStyleWithColumns(20))); 7598 7599 EXPECT_EQ( 7600 "f(g(\"long string \"\n" 7601 " \"literal\"),\n" 7602 " b);", 7603 format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20))); 7604 EXPECT_EQ("f(g(\"long string \"\n" 7605 " \"literal\",\n" 7606 " a),\n" 7607 " b);", 7608 format("f(g(\"long string literal\", a), b);", 7609 getLLVMStyleWithColumns(20))); 7610 EXPECT_EQ( 7611 "f(\"one two\".split(\n" 7612 " variable));", 7613 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); 7614 EXPECT_EQ("f(\"one two three four five six \"\n" 7615 " \"seven\".split(\n" 7616 " really_looooong_variable));", 7617 format("f(\"one two three four five six seven\"." 7618 "split(really_looooong_variable));", 7619 getLLVMStyleWithColumns(33))); 7620 7621 EXPECT_EQ("f(\"some \"\n" 7622 " \"text\",\n" 7623 " other);", 7624 format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); 7625 7626 // Only break as a last resort. 7627 verifyFormat( 7628 "aaaaaaaaaaaaaaaaaaaa(\n" 7629 " aaaaaaaaaaaaaaaaaaaa,\n" 7630 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); 7631 7632 EXPECT_EQ("\"splitmea\"\n" 7633 "\"trandomp\"\n" 7634 "\"oint\"", 7635 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); 7636 7637 EXPECT_EQ("\"split/\"\n" 7638 "\"pathat/\"\n" 7639 "\"slashes\"", 7640 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 7641 7642 EXPECT_EQ("\"split/\"\n" 7643 "\"pathat/\"\n" 7644 "\"slashes\"", 7645 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 7646 EXPECT_EQ("\"split at \"\n" 7647 "\"spaces/at/\"\n" 7648 "\"slashes.at.any$\"\n" 7649 "\"non-alphanumeric%\"\n" 7650 "\"1111111111characte\"\n" 7651 "\"rs\"", 7652 format("\"split at " 7653 "spaces/at/" 7654 "slashes.at." 7655 "any$non-" 7656 "alphanumeric%" 7657 "1111111111characte" 7658 "rs\"", 7659 getLLVMStyleWithColumns(20))); 7660 7661 // Verify that splitting the strings understands 7662 // Style::AlwaysBreakBeforeMultilineStrings. 7663 EXPECT_EQ( 7664 "aaaaaaaaaaaa(\n" 7665 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" 7666 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", 7667 format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa " 7668 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 7669 "aaaaaaaaaaaaaaaaaaaaaa\");", 7670 getGoogleStyle())); 7671 EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7672 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", 7673 format("return \"aaaaaaaaaaaaaaaaaaaaaa " 7674 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 7675 "aaaaaaaaaaaaaaaaaaaaaa\";", 7676 getGoogleStyle())); 7677 EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7678 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 7679 format("llvm::outs() << " 7680 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa" 7681 "aaaaaaaaaaaaaaaaaaa\";")); 7682 EXPECT_EQ("ffff(\n" 7683 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7684 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 7685 format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " 7686 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 7687 getGoogleStyle())); 7688 7689 FormatStyle Style = getLLVMStyleWithColumns(12); 7690 Style.BreakStringLiterals = false; 7691 EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style)); 7692 7693 FormatStyle AlignLeft = getLLVMStyleWithColumns(12); 7694 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 7695 EXPECT_EQ("#define A \\\n" 7696 " \"some \" \\\n" 7697 " \"text \" \\\n" 7698 " \"other\";", 7699 format("#define A \"some text other\";", AlignLeft)); 7700 } 7701 7702 TEST_F(FormatTest, FullyRemoveEmptyLines) { 7703 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80); 7704 NoEmptyLines.MaxEmptyLinesToKeep = 0; 7705 EXPECT_EQ("int i = a(b());", 7706 format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines)); 7707 } 7708 7709 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) { 7710 EXPECT_EQ( 7711 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 7712 "(\n" 7713 " \"x\t\");", 7714 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 7715 "aaaaaaa(" 7716 "\"x\t\");")); 7717 } 7718 7719 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) { 7720 EXPECT_EQ( 7721 "u8\"utf8 string \"\n" 7722 "u8\"literal\";", 7723 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16))); 7724 EXPECT_EQ( 7725 "u\"utf16 string \"\n" 7726 "u\"literal\";", 7727 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16))); 7728 EXPECT_EQ( 7729 "U\"utf32 string \"\n" 7730 "U\"literal\";", 7731 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16))); 7732 EXPECT_EQ("L\"wide string \"\n" 7733 "L\"literal\";", 7734 format("L\"wide string literal\";", getGoogleStyleWithColumns(16))); 7735 EXPECT_EQ("@\"NSString \"\n" 7736 "@\"literal\";", 7737 format("@\"NSString literal\";", getGoogleStyleWithColumns(19))); 7738 verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26)); 7739 7740 // This input makes clang-format try to split the incomplete unicode escape 7741 // sequence, which used to lead to a crasher. 7742 verifyNoCrash( 7743 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 7744 getLLVMStyleWithColumns(60)); 7745 } 7746 7747 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) { 7748 FormatStyle Style = getGoogleStyleWithColumns(15); 7749 EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style)); 7750 EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style)); 7751 EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style)); 7752 EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style)); 7753 EXPECT_EQ("u8R\"x(raw literal)x\";", 7754 format("u8R\"x(raw literal)x\";", Style)); 7755 } 7756 7757 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { 7758 FormatStyle Style = getLLVMStyleWithColumns(20); 7759 EXPECT_EQ( 7760 "_T(\"aaaaaaaaaaaaaa\")\n" 7761 "_T(\"aaaaaaaaaaaaaa\")\n" 7762 "_T(\"aaaaaaaaaaaa\")", 7763 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style)); 7764 EXPECT_EQ("f(x,\n" 7765 " _T(\"aaaaaaaaaaaa\")\n" 7766 " _T(\"aaa\"),\n" 7767 " z);", 7768 format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style)); 7769 7770 // FIXME: Handle embedded spaces in one iteration. 7771 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n" 7772 // "_T(\"aaaaaaaaaaaaa\")\n" 7773 // "_T(\"aaaaaaaaaaaaa\")\n" 7774 // "_T(\"a\")", 7775 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 7776 // getLLVMStyleWithColumns(20))); 7777 EXPECT_EQ( 7778 "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 7779 format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style)); 7780 EXPECT_EQ("f(\n" 7781 "#if !TEST\n" 7782 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 7783 "#endif\n" 7784 ");", 7785 format("f(\n" 7786 "#if !TEST\n" 7787 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 7788 "#endif\n" 7789 ");")); 7790 EXPECT_EQ("f(\n" 7791 "\n" 7792 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));", 7793 format("f(\n" 7794 "\n" 7795 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));")); 7796 } 7797 7798 TEST_F(FormatTest, BreaksStringLiteralOperands) { 7799 // In a function call with two operands, the second can be broken with no line 7800 // break before it. 7801 EXPECT_EQ("func(a, \"long long \"\n" 7802 " \"long long\");", 7803 format("func(a, \"long long long long\");", 7804 getLLVMStyleWithColumns(24))); 7805 // In a function call with three operands, the second must be broken with a 7806 // line break before it. 7807 EXPECT_EQ("func(a,\n" 7808 " \"long long long \"\n" 7809 " \"long\",\n" 7810 " c);", 7811 format("func(a, \"long long long long\", c);", 7812 getLLVMStyleWithColumns(24))); 7813 // In a function call with three operands, the third must be broken with a 7814 // line break before it. 7815 EXPECT_EQ("func(a, b,\n" 7816 " \"long long long \"\n" 7817 " \"long\");", 7818 format("func(a, b, \"long long long long\");", 7819 getLLVMStyleWithColumns(24))); 7820 // In a function call with three operands, both the second and the third must 7821 // be broken with a line break before them. 7822 EXPECT_EQ("func(a,\n" 7823 " \"long long long \"\n" 7824 " \"long\",\n" 7825 " \"long long long \"\n" 7826 " \"long\");", 7827 format("func(a, \"long long long long\", \"long long long long\");", 7828 getLLVMStyleWithColumns(24))); 7829 // In a chain of << with two operands, the second can be broken with no line 7830 // break before it. 7831 EXPECT_EQ("a << \"line line \"\n" 7832 " \"line\";", 7833 format("a << \"line line line\";", 7834 getLLVMStyleWithColumns(20))); 7835 // In a chain of << with three operands, the second can be broken with no line 7836 // break before it. 7837 EXPECT_EQ("abcde << \"line \"\n" 7838 " \"line line\"\n" 7839 " << c;", 7840 format("abcde << \"line line line\" << c;", 7841 getLLVMStyleWithColumns(20))); 7842 // In a chain of << with three operands, the third must be broken with a line 7843 // break before it. 7844 EXPECT_EQ("a << b\n" 7845 " << \"line line \"\n" 7846 " \"line\";", 7847 format("a << b << \"line line line\";", 7848 getLLVMStyleWithColumns(20))); 7849 // In a chain of << with three operands, the second can be broken with no line 7850 // break before it and the third must be broken with a line break before it. 7851 EXPECT_EQ("abcd << \"line line \"\n" 7852 " \"line\"\n" 7853 " << \"line line \"\n" 7854 " \"line\";", 7855 format("abcd << \"line line line\" << \"line line line\";", 7856 getLLVMStyleWithColumns(20))); 7857 // In a chain of binary operators with two operands, the second can be broken 7858 // with no line break before it. 7859 EXPECT_EQ("abcd + \"line line \"\n" 7860 " \"line line\";", 7861 format("abcd + \"line line line line\";", 7862 getLLVMStyleWithColumns(20))); 7863 // In a chain of binary operators with three operands, the second must be 7864 // broken with a line break before it. 7865 EXPECT_EQ("abcd +\n" 7866 " \"line line \"\n" 7867 " \"line line\" +\n" 7868 " e;", 7869 format("abcd + \"line line line line\" + e;", 7870 getLLVMStyleWithColumns(20))); 7871 // In a function call with two operands, with AlignAfterOpenBracket enabled, 7872 // the first must be broken with a line break before it. 7873 FormatStyle Style = getLLVMStyleWithColumns(25); 7874 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 7875 EXPECT_EQ("someFunction(\n" 7876 " \"long long long \"\n" 7877 " \"long\",\n" 7878 " a);", 7879 format("someFunction(\"long long long long\", a);", Style)); 7880 } 7881 7882 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { 7883 EXPECT_EQ( 7884 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 7885 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 7886 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 7887 format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 7888 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 7889 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";")); 7890 } 7891 7892 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { 7893 EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);", 7894 format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle())); 7895 EXPECT_EQ("fffffffffff(g(R\"x(\n" 7896 "multiline raw string literal xxxxxxxxxxxxxx\n" 7897 ")x\",\n" 7898 " a),\n" 7899 " b);", 7900 format("fffffffffff(g(R\"x(\n" 7901 "multiline raw string literal xxxxxxxxxxxxxx\n" 7902 ")x\", a), b);", 7903 getGoogleStyleWithColumns(20))); 7904 EXPECT_EQ("fffffffffff(\n" 7905 " g(R\"x(qqq\n" 7906 "multiline raw string literal xxxxxxxxxxxxxx\n" 7907 ")x\",\n" 7908 " a),\n" 7909 " b);", 7910 format("fffffffffff(g(R\"x(qqq\n" 7911 "multiline raw string literal xxxxxxxxxxxxxx\n" 7912 ")x\", a), b);", 7913 getGoogleStyleWithColumns(20))); 7914 7915 EXPECT_EQ("fffffffffff(R\"x(\n" 7916 "multiline raw string literal xxxxxxxxxxxxxx\n" 7917 ")x\");", 7918 format("fffffffffff(R\"x(\n" 7919 "multiline raw string literal xxxxxxxxxxxxxx\n" 7920 ")x\");", 7921 getGoogleStyleWithColumns(20))); 7922 EXPECT_EQ("fffffffffff(R\"x(\n" 7923 "multiline raw string literal xxxxxxxxxxxxxx\n" 7924 ")x\" + bbbbbb);", 7925 format("fffffffffff(R\"x(\n" 7926 "multiline raw string literal xxxxxxxxxxxxxx\n" 7927 ")x\" + bbbbbb);", 7928 getGoogleStyleWithColumns(20))); 7929 EXPECT_EQ("fffffffffff(\n" 7930 " R\"x(\n" 7931 "multiline raw string literal xxxxxxxxxxxxxx\n" 7932 ")x\" +\n" 7933 " bbbbbb);", 7934 format("fffffffffff(\n" 7935 " R\"x(\n" 7936 "multiline raw string literal xxxxxxxxxxxxxx\n" 7937 ")x\" + bbbbbb);", 7938 getGoogleStyleWithColumns(20))); 7939 } 7940 7941 TEST_F(FormatTest, SkipsUnknownStringLiterals) { 7942 verifyFormat("string a = \"unterminated;"); 7943 EXPECT_EQ("function(\"unterminated,\n" 7944 " OtherParameter);", 7945 format("function( \"unterminated,\n" 7946 " OtherParameter);")); 7947 } 7948 7949 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { 7950 FormatStyle Style = getLLVMStyle(); 7951 Style.Standard = FormatStyle::LS_Cpp03; 7952 EXPECT_EQ("#define x(_a) printf(\"foo\" _a);", 7953 format("#define x(_a) printf(\"foo\"_a);", Style)); 7954 } 7955 7956 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); } 7957 7958 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { 7959 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n" 7960 " \"ddeeefff\");", 7961 format("someFunction(\"aaabbbcccdddeeefff\");", 7962 getLLVMStyleWithColumns(25))); 7963 EXPECT_EQ("someFunction1234567890(\n" 7964 " \"aaabbbcccdddeeefff\");", 7965 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 7966 getLLVMStyleWithColumns(26))); 7967 EXPECT_EQ("someFunction1234567890(\n" 7968 " \"aaabbbcccdddeeeff\"\n" 7969 " \"f\");", 7970 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 7971 getLLVMStyleWithColumns(25))); 7972 EXPECT_EQ("someFunction1234567890(\n" 7973 " \"aaabbbcccdddeeeff\"\n" 7974 " \"f\");", 7975 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 7976 getLLVMStyleWithColumns(24))); 7977 EXPECT_EQ("someFunction(\"aaabbbcc \"\n" 7978 " \"ddde \"\n" 7979 " \"efff\");", 7980 format("someFunction(\"aaabbbcc ddde efff\");", 7981 getLLVMStyleWithColumns(25))); 7982 EXPECT_EQ("someFunction(\"aaabbbccc \"\n" 7983 " \"ddeeefff\");", 7984 format("someFunction(\"aaabbbccc ddeeefff\");", 7985 getLLVMStyleWithColumns(25))); 7986 EXPECT_EQ("someFunction1234567890(\n" 7987 " \"aaabb \"\n" 7988 " \"cccdddeeefff\");", 7989 format("someFunction1234567890(\"aaabb cccdddeeefff\");", 7990 getLLVMStyleWithColumns(25))); 7991 EXPECT_EQ("#define A \\\n" 7992 " string s = \\\n" 7993 " \"123456789\" \\\n" 7994 " \"0\"; \\\n" 7995 " int i;", 7996 format("#define A string s = \"1234567890\"; int i;", 7997 getLLVMStyleWithColumns(20))); 7998 // FIXME: Put additional penalties on breaking at non-whitespace locations. 7999 EXPECT_EQ("someFunction(\"aaabbbcc \"\n" 8000 " \"dddeeeff\"\n" 8001 " \"f\");", 8002 format("someFunction(\"aaabbbcc dddeeefff\");", 8003 getLLVMStyleWithColumns(25))); 8004 } 8005 8006 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { 8007 EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3))); 8008 EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2))); 8009 EXPECT_EQ("\"test\"\n" 8010 "\"\\n\"", 8011 format("\"test\\n\"", getLLVMStyleWithColumns(7))); 8012 EXPECT_EQ("\"tes\\\\\"\n" 8013 "\"n\"", 8014 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); 8015 EXPECT_EQ("\"\\\\\\\\\"\n" 8016 "\"\\n\"", 8017 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); 8018 EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7))); 8019 EXPECT_EQ("\"\\uff01\"\n" 8020 "\"test\"", 8021 format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); 8022 EXPECT_EQ("\"\\Uff01ff02\"", 8023 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); 8024 EXPECT_EQ("\"\\x000000000001\"\n" 8025 "\"next\"", 8026 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); 8027 EXPECT_EQ("\"\\x000000000001next\"", 8028 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); 8029 EXPECT_EQ("\"\\x000000000001\"", 8030 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); 8031 EXPECT_EQ("\"test\"\n" 8032 "\"\\000000\"\n" 8033 "\"000001\"", 8034 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); 8035 EXPECT_EQ("\"test\\000\"\n" 8036 "\"00000000\"\n" 8037 "\"1\"", 8038 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); 8039 } 8040 8041 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { 8042 verifyFormat("void f() {\n" 8043 " return g() {}\n" 8044 " void h() {}"); 8045 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n" 8046 "g();\n" 8047 "}"); 8048 } 8049 8050 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) { 8051 verifyFormat( 8052 "void f() { return C{param1, param2}.SomeCall(param1, param2); }"); 8053 } 8054 8055 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { 8056 verifyFormat("class X {\n" 8057 " void f() {\n" 8058 " }\n" 8059 "};", 8060 getLLVMStyleWithColumns(12)); 8061 } 8062 8063 TEST_F(FormatTest, ConfigurableIndentWidth) { 8064 FormatStyle EightIndent = getLLVMStyleWithColumns(18); 8065 EightIndent.IndentWidth = 8; 8066 EightIndent.ContinuationIndentWidth = 8; 8067 verifyFormat("void f() {\n" 8068 " someFunction();\n" 8069 " if (true) {\n" 8070 " f();\n" 8071 " }\n" 8072 "}", 8073 EightIndent); 8074 verifyFormat("class X {\n" 8075 " void f() {\n" 8076 " }\n" 8077 "};", 8078 EightIndent); 8079 verifyFormat("int x[] = {\n" 8080 " call(),\n" 8081 " call()};", 8082 EightIndent); 8083 } 8084 8085 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) { 8086 verifyFormat("double\n" 8087 "f();", 8088 getLLVMStyleWithColumns(8)); 8089 } 8090 8091 TEST_F(FormatTest, ConfigurableUseOfTab) { 8092 FormatStyle Tab = getLLVMStyleWithColumns(42); 8093 Tab.IndentWidth = 8; 8094 Tab.UseTab = FormatStyle::UT_Always; 8095 Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left; 8096 8097 EXPECT_EQ("if (aaaaaaaa && // q\n" 8098 " bb)\t\t// w\n" 8099 "\t;", 8100 format("if (aaaaaaaa &&// q\n" 8101 "bb)// w\n" 8102 ";", 8103 Tab)); 8104 EXPECT_EQ("if (aaa && bbb) // w\n" 8105 "\t;", 8106 format("if(aaa&&bbb)// w\n" 8107 ";", 8108 Tab)); 8109 8110 verifyFormat("class X {\n" 8111 "\tvoid f() {\n" 8112 "\t\tsomeFunction(parameter1,\n" 8113 "\t\t\t parameter2);\n" 8114 "\t}\n" 8115 "};", 8116 Tab); 8117 verifyFormat("#define A \\\n" 8118 "\tvoid f() { \\\n" 8119 "\t\tsomeFunction( \\\n" 8120 "\t\t parameter1, \\\n" 8121 "\t\t parameter2); \\\n" 8122 "\t}", 8123 Tab); 8124 8125 Tab.TabWidth = 4; 8126 Tab.IndentWidth = 8; 8127 verifyFormat("class TabWidth4Indent8 {\n" 8128 "\t\tvoid f() {\n" 8129 "\t\t\t\tsomeFunction(parameter1,\n" 8130 "\t\t\t\t\t\t\t parameter2);\n" 8131 "\t\t}\n" 8132 "};", 8133 Tab); 8134 8135 Tab.TabWidth = 4; 8136 Tab.IndentWidth = 4; 8137 verifyFormat("class TabWidth4Indent4 {\n" 8138 "\tvoid f() {\n" 8139 "\t\tsomeFunction(parameter1,\n" 8140 "\t\t\t\t\t parameter2);\n" 8141 "\t}\n" 8142 "};", 8143 Tab); 8144 8145 Tab.TabWidth = 8; 8146 Tab.IndentWidth = 4; 8147 verifyFormat("class TabWidth8Indent4 {\n" 8148 " void f() {\n" 8149 "\tsomeFunction(parameter1,\n" 8150 "\t\t parameter2);\n" 8151 " }\n" 8152 "};", 8153 Tab); 8154 8155 Tab.TabWidth = 8; 8156 Tab.IndentWidth = 8; 8157 EXPECT_EQ("/*\n" 8158 "\t a\t\tcomment\n" 8159 "\t in multiple lines\n" 8160 " */", 8161 format(" /*\t \t \n" 8162 " \t \t a\t\tcomment\t \t\n" 8163 " \t \t in multiple lines\t\n" 8164 " \t */", 8165 Tab)); 8166 8167 Tab.UseTab = FormatStyle::UT_ForIndentation; 8168 verifyFormat("{\n" 8169 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8170 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8171 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8172 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8173 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8174 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8175 "};", 8176 Tab); 8177 verifyFormat("enum AA {\n" 8178 "\ta1, // Force multiple lines\n" 8179 "\ta2,\n" 8180 "\ta3\n" 8181 "};", 8182 Tab); 8183 EXPECT_EQ("if (aaaaaaaa && // q\n" 8184 " bb) // w\n" 8185 "\t;", 8186 format("if (aaaaaaaa &&// q\n" 8187 "bb)// w\n" 8188 ";", 8189 Tab)); 8190 verifyFormat("class X {\n" 8191 "\tvoid f() {\n" 8192 "\t\tsomeFunction(parameter1,\n" 8193 "\t\t parameter2);\n" 8194 "\t}\n" 8195 "};", 8196 Tab); 8197 verifyFormat("{\n" 8198 "\tQ(\n" 8199 "\t {\n" 8200 "\t\t int a;\n" 8201 "\t\t someFunction(aaaaaaaa,\n" 8202 "\t\t bbbbbbb);\n" 8203 "\t },\n" 8204 "\t p);\n" 8205 "}", 8206 Tab); 8207 EXPECT_EQ("{\n" 8208 "\t/* aaaa\n" 8209 "\t bbbb */\n" 8210 "}", 8211 format("{\n" 8212 "/* aaaa\n" 8213 " bbbb */\n" 8214 "}", 8215 Tab)); 8216 EXPECT_EQ("{\n" 8217 "\t/*\n" 8218 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8219 "\t bbbbbbbbbbbbb\n" 8220 "\t*/\n" 8221 "}", 8222 format("{\n" 8223 "/*\n" 8224 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8225 "*/\n" 8226 "}", 8227 Tab)); 8228 EXPECT_EQ("{\n" 8229 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8230 "\t// bbbbbbbbbbbbb\n" 8231 "}", 8232 format("{\n" 8233 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8234 "}", 8235 Tab)); 8236 EXPECT_EQ("{\n" 8237 "\t/*\n" 8238 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8239 "\t bbbbbbbbbbbbb\n" 8240 "\t*/\n" 8241 "}", 8242 format("{\n" 8243 "\t/*\n" 8244 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8245 "\t*/\n" 8246 "}", 8247 Tab)); 8248 EXPECT_EQ("{\n" 8249 "\t/*\n" 8250 "\n" 8251 "\t*/\n" 8252 "}", 8253 format("{\n" 8254 "\t/*\n" 8255 "\n" 8256 "\t*/\n" 8257 "}", 8258 Tab)); 8259 EXPECT_EQ("{\n" 8260 "\t/*\n" 8261 " asdf\n" 8262 "\t*/\n" 8263 "}", 8264 format("{\n" 8265 "\t/*\n" 8266 " asdf\n" 8267 "\t*/\n" 8268 "}", 8269 Tab)); 8270 8271 Tab.UseTab = FormatStyle::UT_Never; 8272 EXPECT_EQ("/*\n" 8273 " a\t\tcomment\n" 8274 " in multiple lines\n" 8275 " */", 8276 format(" /*\t \t \n" 8277 " \t \t a\t\tcomment\t \t\n" 8278 " \t \t in multiple lines\t\n" 8279 " \t */", 8280 Tab)); 8281 EXPECT_EQ("/* some\n" 8282 " comment */", 8283 format(" \t \t /* some\n" 8284 " \t \t comment */", 8285 Tab)); 8286 EXPECT_EQ("int a; /* some\n" 8287 " comment */", 8288 format(" \t \t int a; /* some\n" 8289 " \t \t comment */", 8290 Tab)); 8291 8292 EXPECT_EQ("int a; /* some\n" 8293 "comment */", 8294 format(" \t \t int\ta; /* some\n" 8295 " \t \t comment */", 8296 Tab)); 8297 EXPECT_EQ("f(\"\t\t\"); /* some\n" 8298 " comment */", 8299 format(" \t \t f(\"\t\t\"); /* some\n" 8300 " \t \t comment */", 8301 Tab)); 8302 EXPECT_EQ("{\n" 8303 " /*\n" 8304 " * Comment\n" 8305 " */\n" 8306 " int i;\n" 8307 "}", 8308 format("{\n" 8309 "\t/*\n" 8310 "\t * Comment\n" 8311 "\t */\n" 8312 "\t int i;\n" 8313 "}")); 8314 8315 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 8316 Tab.TabWidth = 8; 8317 Tab.IndentWidth = 8; 8318 EXPECT_EQ("if (aaaaaaaa && // q\n" 8319 " bb) // w\n" 8320 "\t;", 8321 format("if (aaaaaaaa &&// q\n" 8322 "bb)// w\n" 8323 ";", 8324 Tab)); 8325 EXPECT_EQ("if (aaa && bbb) // w\n" 8326 "\t;", 8327 format("if(aaa&&bbb)// w\n" 8328 ";", 8329 Tab)); 8330 verifyFormat("class X {\n" 8331 "\tvoid f() {\n" 8332 "\t\tsomeFunction(parameter1,\n" 8333 "\t\t\t parameter2);\n" 8334 "\t}\n" 8335 "};", 8336 Tab); 8337 verifyFormat("#define A \\\n" 8338 "\tvoid f() { \\\n" 8339 "\t\tsomeFunction( \\\n" 8340 "\t\t parameter1, \\\n" 8341 "\t\t parameter2); \\\n" 8342 "\t}", 8343 Tab); 8344 Tab.TabWidth = 4; 8345 Tab.IndentWidth = 8; 8346 verifyFormat("class TabWidth4Indent8 {\n" 8347 "\t\tvoid f() {\n" 8348 "\t\t\t\tsomeFunction(parameter1,\n" 8349 "\t\t\t\t\t\t\t parameter2);\n" 8350 "\t\t}\n" 8351 "};", 8352 Tab); 8353 Tab.TabWidth = 4; 8354 Tab.IndentWidth = 4; 8355 verifyFormat("class TabWidth4Indent4 {\n" 8356 "\tvoid f() {\n" 8357 "\t\tsomeFunction(parameter1,\n" 8358 "\t\t\t\t\t parameter2);\n" 8359 "\t}\n" 8360 "};", 8361 Tab); 8362 Tab.TabWidth = 8; 8363 Tab.IndentWidth = 4; 8364 verifyFormat("class TabWidth8Indent4 {\n" 8365 " void f() {\n" 8366 "\tsomeFunction(parameter1,\n" 8367 "\t\t parameter2);\n" 8368 " }\n" 8369 "};", 8370 Tab); 8371 Tab.TabWidth = 8; 8372 Tab.IndentWidth = 8; 8373 EXPECT_EQ("/*\n" 8374 "\t a\t\tcomment\n" 8375 "\t in multiple lines\n" 8376 " */", 8377 format(" /*\t \t \n" 8378 " \t \t a\t\tcomment\t \t\n" 8379 " \t \t in multiple lines\t\n" 8380 " \t */", 8381 Tab)); 8382 verifyFormat("{\n" 8383 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8384 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8385 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8386 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8387 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8388 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8389 "};", 8390 Tab); 8391 verifyFormat("enum AA {\n" 8392 "\ta1, // Force multiple lines\n" 8393 "\ta2,\n" 8394 "\ta3\n" 8395 "};", 8396 Tab); 8397 EXPECT_EQ("if (aaaaaaaa && // q\n" 8398 " bb) // w\n" 8399 "\t;", 8400 format("if (aaaaaaaa &&// q\n" 8401 "bb)// w\n" 8402 ";", 8403 Tab)); 8404 verifyFormat("class X {\n" 8405 "\tvoid f() {\n" 8406 "\t\tsomeFunction(parameter1,\n" 8407 "\t\t\t parameter2);\n" 8408 "\t}\n" 8409 "};", 8410 Tab); 8411 verifyFormat("{\n" 8412 "\tQ(\n" 8413 "\t {\n" 8414 "\t\t int a;\n" 8415 "\t\t someFunction(aaaaaaaa,\n" 8416 "\t\t\t\t bbbbbbb);\n" 8417 "\t },\n" 8418 "\t p);\n" 8419 "}", 8420 Tab); 8421 EXPECT_EQ("{\n" 8422 "\t/* aaaa\n" 8423 "\t bbbb */\n" 8424 "}", 8425 format("{\n" 8426 "/* aaaa\n" 8427 " bbbb */\n" 8428 "}", 8429 Tab)); 8430 EXPECT_EQ("{\n" 8431 "\t/*\n" 8432 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8433 "\t bbbbbbbbbbbbb\n" 8434 "\t*/\n" 8435 "}", 8436 format("{\n" 8437 "/*\n" 8438 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8439 "*/\n" 8440 "}", 8441 Tab)); 8442 EXPECT_EQ("{\n" 8443 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8444 "\t// bbbbbbbbbbbbb\n" 8445 "}", 8446 format("{\n" 8447 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8448 "}", 8449 Tab)); 8450 EXPECT_EQ("{\n" 8451 "\t/*\n" 8452 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8453 "\t bbbbbbbbbbbbb\n" 8454 "\t*/\n" 8455 "}", 8456 format("{\n" 8457 "\t/*\n" 8458 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8459 "\t*/\n" 8460 "}", 8461 Tab)); 8462 EXPECT_EQ("{\n" 8463 "\t/*\n" 8464 "\n" 8465 "\t*/\n" 8466 "}", 8467 format("{\n" 8468 "\t/*\n" 8469 "\n" 8470 "\t*/\n" 8471 "}", 8472 Tab)); 8473 EXPECT_EQ("{\n" 8474 "\t/*\n" 8475 " asdf\n" 8476 "\t*/\n" 8477 "}", 8478 format("{\n" 8479 "\t/*\n" 8480 " asdf\n" 8481 "\t*/\n" 8482 "}", 8483 Tab)); 8484 EXPECT_EQ("/*\n" 8485 "\t a\t\tcomment\n" 8486 "\t in multiple lines\n" 8487 " */", 8488 format(" /*\t \t \n" 8489 " \t \t a\t\tcomment\t \t\n" 8490 " \t \t in multiple lines\t\n" 8491 " \t */", 8492 Tab)); 8493 EXPECT_EQ("/* some\n" 8494 " comment */", 8495 format(" \t \t /* some\n" 8496 " \t \t comment */", 8497 Tab)); 8498 EXPECT_EQ("int a; /* some\n" 8499 " comment */", 8500 format(" \t \t int a; /* some\n" 8501 " \t \t comment */", 8502 Tab)); 8503 EXPECT_EQ("int a; /* some\n" 8504 "comment */", 8505 format(" \t \t int\ta; /* some\n" 8506 " \t \t comment */", 8507 Tab)); 8508 EXPECT_EQ("f(\"\t\t\"); /* some\n" 8509 " comment */", 8510 format(" \t \t f(\"\t\t\"); /* some\n" 8511 " \t \t comment */", 8512 Tab)); 8513 EXPECT_EQ("{\n" 8514 " /*\n" 8515 " * Comment\n" 8516 " */\n" 8517 " int i;\n" 8518 "}", 8519 format("{\n" 8520 "\t/*\n" 8521 "\t * Comment\n" 8522 "\t */\n" 8523 "\t int i;\n" 8524 "}")); 8525 Tab.AlignConsecutiveAssignments = true; 8526 Tab.AlignConsecutiveDeclarations = true; 8527 Tab.TabWidth = 4; 8528 Tab.IndentWidth = 4; 8529 verifyFormat("class Assign {\n" 8530 "\tvoid f() {\n" 8531 "\t\tint x = 123;\n" 8532 "\t\tint random = 4;\n" 8533 "\t\tstd::string alphabet =\n" 8534 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 8535 "\t}\n" 8536 "};", 8537 Tab); 8538 } 8539 8540 TEST_F(FormatTest, CalculatesOriginalColumn) { 8541 EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8542 "q\"; /* some\n" 8543 " comment */", 8544 format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8545 "q\"; /* some\n" 8546 " comment */", 8547 getLLVMStyle())); 8548 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 8549 "/* some\n" 8550 " comment */", 8551 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 8552 " /* some\n" 8553 " comment */", 8554 getLLVMStyle())); 8555 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8556 "qqq\n" 8557 "/* some\n" 8558 " comment */", 8559 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8560 "qqq\n" 8561 " /* some\n" 8562 " comment */", 8563 getLLVMStyle())); 8564 EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8565 "wwww; /* some\n" 8566 " comment */", 8567 format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8568 "wwww; /* some\n" 8569 " comment */", 8570 getLLVMStyle())); 8571 } 8572 8573 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { 8574 FormatStyle NoSpace = getLLVMStyle(); 8575 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never; 8576 8577 verifyFormat("while(true)\n" 8578 " continue;", 8579 NoSpace); 8580 verifyFormat("for(;;)\n" 8581 " continue;", 8582 NoSpace); 8583 verifyFormat("if(true)\n" 8584 " f();\n" 8585 "else if(true)\n" 8586 " f();", 8587 NoSpace); 8588 verifyFormat("do {\n" 8589 " do_something();\n" 8590 "} while(something());", 8591 NoSpace); 8592 verifyFormat("switch(x) {\n" 8593 "default:\n" 8594 " break;\n" 8595 "}", 8596 NoSpace); 8597 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace); 8598 verifyFormat("size_t x = sizeof(x);", NoSpace); 8599 verifyFormat("auto f(int x) -> decltype(x);", NoSpace); 8600 verifyFormat("int f(T x) noexcept(x.create());", NoSpace); 8601 verifyFormat("alignas(128) char a[128];", NoSpace); 8602 verifyFormat("size_t x = alignof(MyType);", NoSpace); 8603 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace); 8604 verifyFormat("int f() throw(Deprecated);", NoSpace); 8605 verifyFormat("typedef void (*cb)(int);", NoSpace); 8606 verifyFormat("T A::operator()();", NoSpace); 8607 verifyFormat("X A::operator++(T);", NoSpace); 8608 8609 FormatStyle Space = getLLVMStyle(); 8610 Space.SpaceBeforeParens = FormatStyle::SBPO_Always; 8611 8612 verifyFormat("int f ();", Space); 8613 verifyFormat("void f (int a, T b) {\n" 8614 " while (true)\n" 8615 " continue;\n" 8616 "}", 8617 Space); 8618 verifyFormat("if (true)\n" 8619 " f ();\n" 8620 "else if (true)\n" 8621 " f ();", 8622 Space); 8623 verifyFormat("do {\n" 8624 " do_something ();\n" 8625 "} while (something ());", 8626 Space); 8627 verifyFormat("switch (x) {\n" 8628 "default:\n" 8629 " break;\n" 8630 "}", 8631 Space); 8632 verifyFormat("A::A () : a (1) {}", Space); 8633 verifyFormat("void f () __attribute__ ((asdf));", Space); 8634 verifyFormat("*(&a + 1);\n" 8635 "&((&a)[1]);\n" 8636 "a[(b + c) * d];\n" 8637 "(((a + 1) * 2) + 3) * 4;", 8638 Space); 8639 verifyFormat("#define A(x) x", Space); 8640 verifyFormat("#define A (x) x", Space); 8641 verifyFormat("#if defined(x)\n" 8642 "#endif", 8643 Space); 8644 verifyFormat("auto i = std::make_unique<int> (5);", Space); 8645 verifyFormat("size_t x = sizeof (x);", Space); 8646 verifyFormat("auto f (int x) -> decltype (x);", Space); 8647 verifyFormat("int f (T x) noexcept (x.create ());", Space); 8648 verifyFormat("alignas (128) char a[128];", Space); 8649 verifyFormat("size_t x = alignof (MyType);", Space); 8650 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space); 8651 verifyFormat("int f () throw (Deprecated);", Space); 8652 verifyFormat("typedef void (*cb) (int);", Space); 8653 verifyFormat("T A::operator() ();", Space); 8654 verifyFormat("X A::operator++ (T);", Space); 8655 } 8656 8657 TEST_F(FormatTest, ConfigurableSpacesInParentheses) { 8658 FormatStyle Spaces = getLLVMStyle(); 8659 8660 Spaces.SpacesInParentheses = true; 8661 verifyFormat("call( x, y, z );", Spaces); 8662 verifyFormat("call();", Spaces); 8663 verifyFormat("std::function<void( int, int )> callback;", Spaces); 8664 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }", 8665 Spaces); 8666 verifyFormat("while ( (bool)1 )\n" 8667 " continue;", 8668 Spaces); 8669 verifyFormat("for ( ;; )\n" 8670 " continue;", 8671 Spaces); 8672 verifyFormat("if ( true )\n" 8673 " f();\n" 8674 "else if ( true )\n" 8675 " f();", 8676 Spaces); 8677 verifyFormat("do {\n" 8678 " do_something( (int)i );\n" 8679 "} while ( something() );", 8680 Spaces); 8681 verifyFormat("switch ( x ) {\n" 8682 "default:\n" 8683 " break;\n" 8684 "}", 8685 Spaces); 8686 8687 Spaces.SpacesInParentheses = false; 8688 Spaces.SpacesInCStyleCastParentheses = true; 8689 verifyFormat("Type *A = ( Type * )P;", Spaces); 8690 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces); 8691 verifyFormat("x = ( int32 )y;", Spaces); 8692 verifyFormat("int a = ( int )(2.0f);", Spaces); 8693 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces); 8694 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces); 8695 verifyFormat("#define x (( int )-1)", Spaces); 8696 8697 // Run the first set of tests again with: 8698 Spaces.SpacesInParentheses = false; 8699 Spaces.SpaceInEmptyParentheses = true; 8700 Spaces.SpacesInCStyleCastParentheses = true; 8701 verifyFormat("call(x, y, z);", Spaces); 8702 verifyFormat("call( );", Spaces); 8703 verifyFormat("std::function<void(int, int)> callback;", Spaces); 8704 verifyFormat("while (( bool )1)\n" 8705 " continue;", 8706 Spaces); 8707 verifyFormat("for (;;)\n" 8708 " continue;", 8709 Spaces); 8710 verifyFormat("if (true)\n" 8711 " f( );\n" 8712 "else if (true)\n" 8713 " f( );", 8714 Spaces); 8715 verifyFormat("do {\n" 8716 " do_something(( int )i);\n" 8717 "} while (something( ));", 8718 Spaces); 8719 verifyFormat("switch (x) {\n" 8720 "default:\n" 8721 " break;\n" 8722 "}", 8723 Spaces); 8724 8725 // Run the first set of tests again with: 8726 Spaces.SpaceAfterCStyleCast = true; 8727 verifyFormat("call(x, y, z);", Spaces); 8728 verifyFormat("call( );", Spaces); 8729 verifyFormat("std::function<void(int, int)> callback;", Spaces); 8730 verifyFormat("while (( bool ) 1)\n" 8731 " continue;", 8732 Spaces); 8733 verifyFormat("for (;;)\n" 8734 " continue;", 8735 Spaces); 8736 verifyFormat("if (true)\n" 8737 " f( );\n" 8738 "else if (true)\n" 8739 " f( );", 8740 Spaces); 8741 verifyFormat("do {\n" 8742 " do_something(( int ) i);\n" 8743 "} while (something( ));", 8744 Spaces); 8745 verifyFormat("switch (x) {\n" 8746 "default:\n" 8747 " break;\n" 8748 "}", 8749 Spaces); 8750 8751 // Run subset of tests again with: 8752 Spaces.SpacesInCStyleCastParentheses = false; 8753 Spaces.SpaceAfterCStyleCast = true; 8754 verifyFormat("while ((bool) 1)\n" 8755 " continue;", 8756 Spaces); 8757 verifyFormat("do {\n" 8758 " do_something((int) i);\n" 8759 "} while (something( ));", 8760 Spaces); 8761 } 8762 8763 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) { 8764 verifyFormat("int a[5];"); 8765 verifyFormat("a[3] += 42;"); 8766 8767 FormatStyle Spaces = getLLVMStyle(); 8768 Spaces.SpacesInSquareBrackets = true; 8769 // Lambdas unchanged. 8770 verifyFormat("int c = []() -> int { return 2; }();\n", Spaces); 8771 verifyFormat("return [i, args...] {};", Spaces); 8772 8773 // Not lambdas. 8774 verifyFormat("int a[ 5 ];", Spaces); 8775 verifyFormat("a[ 3 ] += 42;", Spaces); 8776 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces); 8777 verifyFormat("double &operator[](int i) { return 0; }\n" 8778 "int i;", 8779 Spaces); 8780 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces); 8781 verifyFormat("int i = a[ a ][ a ]->f();", Spaces); 8782 verifyFormat("int i = (*b)[ a ]->f();", Spaces); 8783 } 8784 8785 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) { 8786 verifyFormat("int a = 5;"); 8787 verifyFormat("a += 42;"); 8788 verifyFormat("a or_eq 8;"); 8789 8790 FormatStyle Spaces = getLLVMStyle(); 8791 Spaces.SpaceBeforeAssignmentOperators = false; 8792 verifyFormat("int a= 5;", Spaces); 8793 verifyFormat("a+= 42;", Spaces); 8794 verifyFormat("a or_eq 8;", Spaces); 8795 } 8796 8797 TEST_F(FormatTest, AlignConsecutiveAssignments) { 8798 FormatStyle Alignment = getLLVMStyle(); 8799 Alignment.AlignConsecutiveAssignments = false; 8800 verifyFormat("int a = 5;\n" 8801 "int oneTwoThree = 123;", 8802 Alignment); 8803 verifyFormat("int a = 5;\n" 8804 "int oneTwoThree = 123;", 8805 Alignment); 8806 8807 Alignment.AlignConsecutiveAssignments = true; 8808 verifyFormat("int a = 5;\n" 8809 "int oneTwoThree = 123;", 8810 Alignment); 8811 verifyFormat("int a = method();\n" 8812 "int oneTwoThree = 133;", 8813 Alignment); 8814 verifyFormat("a &= 5;\n" 8815 "bcd *= 5;\n" 8816 "ghtyf += 5;\n" 8817 "dvfvdb -= 5;\n" 8818 "a /= 5;\n" 8819 "vdsvsv %= 5;\n" 8820 "sfdbddfbdfbb ^= 5;\n" 8821 "dvsdsv |= 5;\n" 8822 "int dsvvdvsdvvv = 123;", 8823 Alignment); 8824 verifyFormat("int i = 1, j = 10;\n" 8825 "something = 2000;", 8826 Alignment); 8827 verifyFormat("something = 2000;\n" 8828 "int i = 1, j = 10;\n", 8829 Alignment); 8830 verifyFormat("something = 2000;\n" 8831 "another = 911;\n" 8832 "int i = 1, j = 10;\n" 8833 "oneMore = 1;\n" 8834 "i = 2;", 8835 Alignment); 8836 verifyFormat("int a = 5;\n" 8837 "int one = 1;\n" 8838 "method();\n" 8839 "int oneTwoThree = 123;\n" 8840 "int oneTwo = 12;", 8841 Alignment); 8842 verifyFormat("int oneTwoThree = 123;\n" 8843 "int oneTwo = 12;\n" 8844 "method();\n", 8845 Alignment); 8846 verifyFormat("int oneTwoThree = 123; // comment\n" 8847 "int oneTwo = 12; // comment", 8848 Alignment); 8849 EXPECT_EQ("int a = 5;\n" 8850 "\n" 8851 "int oneTwoThree = 123;", 8852 format("int a = 5;\n" 8853 "\n" 8854 "int oneTwoThree= 123;", 8855 Alignment)); 8856 EXPECT_EQ("int a = 5;\n" 8857 "int one = 1;\n" 8858 "\n" 8859 "int oneTwoThree = 123;", 8860 format("int a = 5;\n" 8861 "int one = 1;\n" 8862 "\n" 8863 "int oneTwoThree = 123;", 8864 Alignment)); 8865 EXPECT_EQ("int a = 5;\n" 8866 "int one = 1;\n" 8867 "\n" 8868 "int oneTwoThree = 123;\n" 8869 "int oneTwo = 12;", 8870 format("int a = 5;\n" 8871 "int one = 1;\n" 8872 "\n" 8873 "int oneTwoThree = 123;\n" 8874 "int oneTwo = 12;", 8875 Alignment)); 8876 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 8877 verifyFormat("#define A \\\n" 8878 " int aaaa = 12; \\\n" 8879 " int b = 23; \\\n" 8880 " int ccc = 234; \\\n" 8881 " int dddddddddd = 2345;", 8882 Alignment); 8883 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 8884 verifyFormat("#define A \\\n" 8885 " int aaaa = 12; \\\n" 8886 " int b = 23; \\\n" 8887 " int ccc = 234; \\\n" 8888 " int dddddddddd = 2345;", 8889 Alignment); 8890 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 8891 verifyFormat("#define A " 8892 " \\\n" 8893 " int aaaa = 12; " 8894 " \\\n" 8895 " int b = 23; " 8896 " \\\n" 8897 " int ccc = 234; " 8898 " \\\n" 8899 " int dddddddddd = 2345;", 8900 Alignment); 8901 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 8902 "k = 4, int l = 5,\n" 8903 " int m = 6) {\n" 8904 " int j = 10;\n" 8905 " otherThing = 1;\n" 8906 "}", 8907 Alignment); 8908 verifyFormat("void SomeFunction(int parameter = 0) {\n" 8909 " int i = 1;\n" 8910 " int j = 2;\n" 8911 " int big = 10000;\n" 8912 "}", 8913 Alignment); 8914 verifyFormat("class C {\n" 8915 "public:\n" 8916 " int i = 1;\n" 8917 " virtual void f() = 0;\n" 8918 "};", 8919 Alignment); 8920 verifyFormat("int i = 1;\n" 8921 "if (SomeType t = getSomething()) {\n" 8922 "}\n" 8923 "int j = 2;\n" 8924 "int big = 10000;", 8925 Alignment); 8926 verifyFormat("int j = 7;\n" 8927 "for (int k = 0; k < N; ++k) {\n" 8928 "}\n" 8929 "int j = 2;\n" 8930 "int big = 10000;\n" 8931 "}", 8932 Alignment); 8933 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 8934 verifyFormat("int i = 1;\n" 8935 "LooooooooooongType loooooooooooooooooooooongVariable\n" 8936 " = someLooooooooooooooooongFunction();\n" 8937 "int j = 2;", 8938 Alignment); 8939 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 8940 verifyFormat("int i = 1;\n" 8941 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 8942 " someLooooooooooooooooongFunction();\n" 8943 "int j = 2;", 8944 Alignment); 8945 8946 verifyFormat("auto lambda = []() {\n" 8947 " auto i = 0;\n" 8948 " return 0;\n" 8949 "};\n" 8950 "int i = 0;\n" 8951 "auto v = type{\n" 8952 " i = 1, //\n" 8953 " (i = 2), //\n" 8954 " i = 3 //\n" 8955 "};", 8956 Alignment); 8957 8958 verifyFormat( 8959 "int i = 1;\n" 8960 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 8961 " loooooooooooooooooooooongParameterB);\n" 8962 "int j = 2;", 8963 Alignment); 8964 8965 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 8966 " typename B = very_long_type_name_1,\n" 8967 " typename T_2 = very_long_type_name_2>\n" 8968 "auto foo() {}\n", 8969 Alignment); 8970 verifyFormat("int a, b = 1;\n" 8971 "int c = 2;\n" 8972 "int dd = 3;\n", 8973 Alignment); 8974 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 8975 "float b[1][] = {{3.f}};\n", 8976 Alignment); 8977 verifyFormat("for (int i = 0; i < 1; i++)\n" 8978 " int x = 1;\n", 8979 Alignment); 8980 verifyFormat("for (i = 0; i < 1; i++)\n" 8981 " x = 1;\n" 8982 "y = 1;\n", 8983 Alignment); 8984 } 8985 8986 TEST_F(FormatTest, AlignConsecutiveDeclarations) { 8987 FormatStyle Alignment = getLLVMStyle(); 8988 Alignment.AlignConsecutiveDeclarations = false; 8989 verifyFormat("float const a = 5;\n" 8990 "int oneTwoThree = 123;", 8991 Alignment); 8992 verifyFormat("int a = 5;\n" 8993 "float const oneTwoThree = 123;", 8994 Alignment); 8995 8996 Alignment.AlignConsecutiveDeclarations = true; 8997 verifyFormat("float const a = 5;\n" 8998 "int oneTwoThree = 123;", 8999 Alignment); 9000 verifyFormat("int a = method();\n" 9001 "float const oneTwoThree = 133;", 9002 Alignment); 9003 verifyFormat("int i = 1, j = 10;\n" 9004 "something = 2000;", 9005 Alignment); 9006 verifyFormat("something = 2000;\n" 9007 "int i = 1, j = 10;\n", 9008 Alignment); 9009 verifyFormat("float something = 2000;\n" 9010 "double another = 911;\n" 9011 "int i = 1, j = 10;\n" 9012 "const int *oneMore = 1;\n" 9013 "unsigned i = 2;", 9014 Alignment); 9015 verifyFormat("float a = 5;\n" 9016 "int one = 1;\n" 9017 "method();\n" 9018 "const double oneTwoThree = 123;\n" 9019 "const unsigned int oneTwo = 12;", 9020 Alignment); 9021 verifyFormat("int oneTwoThree{0}; // comment\n" 9022 "unsigned oneTwo; // comment", 9023 Alignment); 9024 EXPECT_EQ("float const a = 5;\n" 9025 "\n" 9026 "int oneTwoThree = 123;", 9027 format("float const a = 5;\n" 9028 "\n" 9029 "int oneTwoThree= 123;", 9030 Alignment)); 9031 EXPECT_EQ("float a = 5;\n" 9032 "int one = 1;\n" 9033 "\n" 9034 "unsigned oneTwoThree = 123;", 9035 format("float a = 5;\n" 9036 "int one = 1;\n" 9037 "\n" 9038 "unsigned oneTwoThree = 123;", 9039 Alignment)); 9040 EXPECT_EQ("float a = 5;\n" 9041 "int one = 1;\n" 9042 "\n" 9043 "unsigned oneTwoThree = 123;\n" 9044 "int oneTwo = 12;", 9045 format("float a = 5;\n" 9046 "int one = 1;\n" 9047 "\n" 9048 "unsigned oneTwoThree = 123;\n" 9049 "int oneTwo = 12;", 9050 Alignment)); 9051 // Function prototype alignment 9052 verifyFormat("int a();\n" 9053 "double b();", 9054 Alignment); 9055 verifyFormat("int a(int x);\n" 9056 "double b();", 9057 Alignment); 9058 unsigned OldColumnLimit = Alignment.ColumnLimit; 9059 // We need to set ColumnLimit to zero, in order to stress nested alignments, 9060 // otherwise the function parameters will be re-flowed onto a single line. 9061 Alignment.ColumnLimit = 0; 9062 EXPECT_EQ("int a(int x,\n" 9063 " float y);\n" 9064 "double b(int x,\n" 9065 " double y);", 9066 format("int a(int x,\n" 9067 " float y);\n" 9068 "double b(int x,\n" 9069 " double y);", 9070 Alignment)); 9071 // This ensures that function parameters of function declarations are 9072 // correctly indented when their owning functions are indented. 9073 // The failure case here is for 'double y' to not be indented enough. 9074 EXPECT_EQ("double a(int x);\n" 9075 "int b(int y,\n" 9076 " double z);", 9077 format("double a(int x);\n" 9078 "int b(int y,\n" 9079 " double z);", 9080 Alignment)); 9081 // Set ColumnLimit low so that we induce wrapping immediately after 9082 // the function name and opening paren. 9083 Alignment.ColumnLimit = 13; 9084 verifyFormat("int function(\n" 9085 " int x,\n" 9086 " bool y);", 9087 Alignment); 9088 Alignment.ColumnLimit = OldColumnLimit; 9089 // Ensure function pointers don't screw up recursive alignment 9090 verifyFormat("int a(int x, void (*fp)(int y));\n" 9091 "double b();", 9092 Alignment); 9093 Alignment.AlignConsecutiveAssignments = true; 9094 // Ensure recursive alignment is broken by function braces, so that the 9095 // "a = 1" does not align with subsequent assignments inside the function 9096 // body. 9097 verifyFormat("int func(int a = 1) {\n" 9098 " int b = 2;\n" 9099 " int cc = 3;\n" 9100 "}", 9101 Alignment); 9102 verifyFormat("float something = 2000;\n" 9103 "double another = 911;\n" 9104 "int i = 1, j = 10;\n" 9105 "const int *oneMore = 1;\n" 9106 "unsigned i = 2;", 9107 Alignment); 9108 verifyFormat("int oneTwoThree = {0}; // comment\n" 9109 "unsigned oneTwo = 0; // comment", 9110 Alignment); 9111 // Make sure that scope is correctly tracked, in the absence of braces 9112 verifyFormat("for (int i = 0; i < n; i++)\n" 9113 " j = i;\n" 9114 "double x = 1;\n", 9115 Alignment); 9116 verifyFormat("if (int i = 0)\n" 9117 " j = i;\n" 9118 "double x = 1;\n", 9119 Alignment); 9120 // Ensure operator[] and operator() are comprehended 9121 verifyFormat("struct test {\n" 9122 " long long int foo();\n" 9123 " int operator[](int a);\n" 9124 " double bar();\n" 9125 "};\n", 9126 Alignment); 9127 verifyFormat("struct test {\n" 9128 " long long int foo();\n" 9129 " int operator()(int a);\n" 9130 " double bar();\n" 9131 "};\n", 9132 Alignment); 9133 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 9134 " int const i = 1;\n" 9135 " int * j = 2;\n" 9136 " int big = 10000;\n" 9137 "\n" 9138 " unsigned oneTwoThree = 123;\n" 9139 " int oneTwo = 12;\n" 9140 " method();\n" 9141 " float k = 2;\n" 9142 " int ll = 10000;\n" 9143 "}", 9144 format("void SomeFunction(int parameter= 0) {\n" 9145 " int const i= 1;\n" 9146 " int *j=2;\n" 9147 " int big = 10000;\n" 9148 "\n" 9149 "unsigned oneTwoThree =123;\n" 9150 "int oneTwo = 12;\n" 9151 " method();\n" 9152 "float k= 2;\n" 9153 "int ll=10000;\n" 9154 "}", 9155 Alignment)); 9156 Alignment.AlignConsecutiveAssignments = false; 9157 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 9158 verifyFormat("#define A \\\n" 9159 " int aaaa = 12; \\\n" 9160 " float b = 23; \\\n" 9161 " const int ccc = 234; \\\n" 9162 " unsigned dddddddddd = 2345;", 9163 Alignment); 9164 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 9165 verifyFormat("#define A \\\n" 9166 " int aaaa = 12; \\\n" 9167 " float b = 23; \\\n" 9168 " const int ccc = 234; \\\n" 9169 " unsigned dddddddddd = 2345;", 9170 Alignment); 9171 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 9172 Alignment.ColumnLimit = 30; 9173 verifyFormat("#define A \\\n" 9174 " int aaaa = 12; \\\n" 9175 " float b = 23; \\\n" 9176 " const int ccc = 234; \\\n" 9177 " int dddddddddd = 2345;", 9178 Alignment); 9179 Alignment.ColumnLimit = 80; 9180 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 9181 "k = 4, int l = 5,\n" 9182 " int m = 6) {\n" 9183 " const int j = 10;\n" 9184 " otherThing = 1;\n" 9185 "}", 9186 Alignment); 9187 verifyFormat("void SomeFunction(int parameter = 0) {\n" 9188 " int const i = 1;\n" 9189 " int * j = 2;\n" 9190 " int big = 10000;\n" 9191 "}", 9192 Alignment); 9193 verifyFormat("class C {\n" 9194 "public:\n" 9195 " int i = 1;\n" 9196 " virtual void f() = 0;\n" 9197 "};", 9198 Alignment); 9199 verifyFormat("float i = 1;\n" 9200 "if (SomeType t = getSomething()) {\n" 9201 "}\n" 9202 "const unsigned j = 2;\n" 9203 "int big = 10000;", 9204 Alignment); 9205 verifyFormat("float j = 7;\n" 9206 "for (int k = 0; k < N; ++k) {\n" 9207 "}\n" 9208 "unsigned j = 2;\n" 9209 "int big = 10000;\n" 9210 "}", 9211 Alignment); 9212 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 9213 verifyFormat("float i = 1;\n" 9214 "LooooooooooongType loooooooooooooooooooooongVariable\n" 9215 " = someLooooooooooooooooongFunction();\n" 9216 "int j = 2;", 9217 Alignment); 9218 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 9219 verifyFormat("int i = 1;\n" 9220 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 9221 " someLooooooooooooooooongFunction();\n" 9222 "int j = 2;", 9223 Alignment); 9224 9225 Alignment.AlignConsecutiveAssignments = true; 9226 verifyFormat("auto lambda = []() {\n" 9227 " auto ii = 0;\n" 9228 " float j = 0;\n" 9229 " return 0;\n" 9230 "};\n" 9231 "int i = 0;\n" 9232 "float i2 = 0;\n" 9233 "auto v = type{\n" 9234 " i = 1, //\n" 9235 " (i = 2), //\n" 9236 " i = 3 //\n" 9237 "};", 9238 Alignment); 9239 Alignment.AlignConsecutiveAssignments = false; 9240 9241 verifyFormat( 9242 "int i = 1;\n" 9243 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 9244 " loooooooooooooooooooooongParameterB);\n" 9245 "int j = 2;", 9246 Alignment); 9247 9248 // Test interactions with ColumnLimit and AlignConsecutiveAssignments: 9249 // We expect declarations and assignments to align, as long as it doesn't 9250 // exceed the column limit, starting a new alignment sequence whenever it 9251 // happens. 9252 Alignment.AlignConsecutiveAssignments = true; 9253 Alignment.ColumnLimit = 30; 9254 verifyFormat("float ii = 1;\n" 9255 "unsigned j = 2;\n" 9256 "int someVerylongVariable = 1;\n" 9257 "AnotherLongType ll = 123456;\n" 9258 "VeryVeryLongType k = 2;\n" 9259 "int myvar = 1;", 9260 Alignment); 9261 Alignment.ColumnLimit = 80; 9262 Alignment.AlignConsecutiveAssignments = false; 9263 9264 verifyFormat( 9265 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n" 9266 " typename LongType, typename B>\n" 9267 "auto foo() {}\n", 9268 Alignment); 9269 verifyFormat("float a, b = 1;\n" 9270 "int c = 2;\n" 9271 "int dd = 3;\n", 9272 Alignment); 9273 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 9274 "float b[1][] = {{3.f}};\n", 9275 Alignment); 9276 Alignment.AlignConsecutiveAssignments = true; 9277 verifyFormat("float a, b = 1;\n" 9278 "int c = 2;\n" 9279 "int dd = 3;\n", 9280 Alignment); 9281 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 9282 "float b[1][] = {{3.f}};\n", 9283 Alignment); 9284 Alignment.AlignConsecutiveAssignments = false; 9285 9286 Alignment.ColumnLimit = 30; 9287 Alignment.BinPackParameters = false; 9288 verifyFormat("void foo(float a,\n" 9289 " float b,\n" 9290 " int c,\n" 9291 " uint32_t *d) {\n" 9292 " int * e = 0;\n" 9293 " float f = 0;\n" 9294 " double g = 0;\n" 9295 "}\n" 9296 "void bar(ino_t a,\n" 9297 " int b,\n" 9298 " uint32_t *c,\n" 9299 " bool d) {}\n", 9300 Alignment); 9301 Alignment.BinPackParameters = true; 9302 Alignment.ColumnLimit = 80; 9303 9304 // Bug 33507 9305 Alignment.PointerAlignment = FormatStyle::PAS_Middle; 9306 verifyFormat( 9307 "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n" 9308 " static const Version verVs2017;\n" 9309 " return true;\n" 9310 "});\n", 9311 Alignment); 9312 Alignment.PointerAlignment = FormatStyle::PAS_Right; 9313 } 9314 9315 TEST_F(FormatTest, LinuxBraceBreaking) { 9316 FormatStyle LinuxBraceStyle = getLLVMStyle(); 9317 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux; 9318 verifyFormat("namespace a\n" 9319 "{\n" 9320 "class A\n" 9321 "{\n" 9322 " void f()\n" 9323 " {\n" 9324 " if (true) {\n" 9325 " a();\n" 9326 " b();\n" 9327 " } else {\n" 9328 " a();\n" 9329 " }\n" 9330 " }\n" 9331 " void g() { return; }\n" 9332 "};\n" 9333 "struct B {\n" 9334 " int x;\n" 9335 "};\n" 9336 "} // namespace a\n", 9337 LinuxBraceStyle); 9338 verifyFormat("enum X {\n" 9339 " Y = 0,\n" 9340 "}\n", 9341 LinuxBraceStyle); 9342 verifyFormat("struct S {\n" 9343 " int Type;\n" 9344 " union {\n" 9345 " int x;\n" 9346 " double y;\n" 9347 " } Value;\n" 9348 " class C\n" 9349 " {\n" 9350 " MyFavoriteType Value;\n" 9351 " } Class;\n" 9352 "}\n", 9353 LinuxBraceStyle); 9354 } 9355 9356 TEST_F(FormatTest, MozillaBraceBreaking) { 9357 FormatStyle MozillaBraceStyle = getLLVMStyle(); 9358 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; 9359 MozillaBraceStyle.FixNamespaceComments = false; 9360 verifyFormat("namespace a {\n" 9361 "class A\n" 9362 "{\n" 9363 " void f()\n" 9364 " {\n" 9365 " if (true) {\n" 9366 " a();\n" 9367 " b();\n" 9368 " }\n" 9369 " }\n" 9370 " void g() { return; }\n" 9371 "};\n" 9372 "enum E\n" 9373 "{\n" 9374 " A,\n" 9375 " // foo\n" 9376 " B,\n" 9377 " C\n" 9378 "};\n" 9379 "struct B\n" 9380 "{\n" 9381 " int x;\n" 9382 "};\n" 9383 "}\n", 9384 MozillaBraceStyle); 9385 verifyFormat("struct S\n" 9386 "{\n" 9387 " int Type;\n" 9388 " union\n" 9389 " {\n" 9390 " int x;\n" 9391 " double y;\n" 9392 " } Value;\n" 9393 " class C\n" 9394 " {\n" 9395 " MyFavoriteType Value;\n" 9396 " } Class;\n" 9397 "}\n", 9398 MozillaBraceStyle); 9399 } 9400 9401 TEST_F(FormatTest, StroustrupBraceBreaking) { 9402 FormatStyle StroustrupBraceStyle = getLLVMStyle(); 9403 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 9404 verifyFormat("namespace a {\n" 9405 "class A {\n" 9406 " void f()\n" 9407 " {\n" 9408 " if (true) {\n" 9409 " a();\n" 9410 " b();\n" 9411 " }\n" 9412 " }\n" 9413 " void g() { return; }\n" 9414 "};\n" 9415 "struct B {\n" 9416 " int x;\n" 9417 "};\n" 9418 "} // namespace a\n", 9419 StroustrupBraceStyle); 9420 9421 verifyFormat("void foo()\n" 9422 "{\n" 9423 " if (a) {\n" 9424 " a();\n" 9425 " }\n" 9426 " else {\n" 9427 " b();\n" 9428 " }\n" 9429 "}\n", 9430 StroustrupBraceStyle); 9431 9432 verifyFormat("#ifdef _DEBUG\n" 9433 "int foo(int i = 0)\n" 9434 "#else\n" 9435 "int foo(int i = 5)\n" 9436 "#endif\n" 9437 "{\n" 9438 " return i;\n" 9439 "}", 9440 StroustrupBraceStyle); 9441 9442 verifyFormat("void foo() {}\n" 9443 "void bar()\n" 9444 "#ifdef _DEBUG\n" 9445 "{\n" 9446 " foo();\n" 9447 "}\n" 9448 "#else\n" 9449 "{\n" 9450 "}\n" 9451 "#endif", 9452 StroustrupBraceStyle); 9453 9454 verifyFormat("void foobar() { int i = 5; }\n" 9455 "#ifdef _DEBUG\n" 9456 "void bar() {}\n" 9457 "#else\n" 9458 "void bar() { foobar(); }\n" 9459 "#endif", 9460 StroustrupBraceStyle); 9461 } 9462 9463 TEST_F(FormatTest, AllmanBraceBreaking) { 9464 FormatStyle AllmanBraceStyle = getLLVMStyle(); 9465 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman; 9466 9467 EXPECT_EQ("namespace a\n" 9468 "{\n" 9469 "void f();\n" 9470 "void g();\n" 9471 "} // namespace a\n", 9472 format("namespace a\n" 9473 "{\n" 9474 "void f();\n" 9475 "void g();\n" 9476 "}\n", 9477 AllmanBraceStyle)); 9478 9479 verifyFormat("namespace a\n" 9480 "{\n" 9481 "class A\n" 9482 "{\n" 9483 " void f()\n" 9484 " {\n" 9485 " if (true)\n" 9486 " {\n" 9487 " a();\n" 9488 " b();\n" 9489 " }\n" 9490 " }\n" 9491 " void g() { return; }\n" 9492 "};\n" 9493 "struct B\n" 9494 "{\n" 9495 " int x;\n" 9496 "};\n" 9497 "} // namespace a", 9498 AllmanBraceStyle); 9499 9500 verifyFormat("void f()\n" 9501 "{\n" 9502 " if (true)\n" 9503 " {\n" 9504 " a();\n" 9505 " }\n" 9506 " else if (false)\n" 9507 " {\n" 9508 " b();\n" 9509 " }\n" 9510 " else\n" 9511 " {\n" 9512 " c();\n" 9513 " }\n" 9514 "}\n", 9515 AllmanBraceStyle); 9516 9517 verifyFormat("void f()\n" 9518 "{\n" 9519 " for (int i = 0; i < 10; ++i)\n" 9520 " {\n" 9521 " a();\n" 9522 " }\n" 9523 " while (false)\n" 9524 " {\n" 9525 " b();\n" 9526 " }\n" 9527 " do\n" 9528 " {\n" 9529 " c();\n" 9530 " } while (false)\n" 9531 "}\n", 9532 AllmanBraceStyle); 9533 9534 verifyFormat("void f(int a)\n" 9535 "{\n" 9536 " switch (a)\n" 9537 " {\n" 9538 " case 0:\n" 9539 " break;\n" 9540 " case 1:\n" 9541 " {\n" 9542 " break;\n" 9543 " }\n" 9544 " case 2:\n" 9545 " {\n" 9546 " }\n" 9547 " break;\n" 9548 " default:\n" 9549 " break;\n" 9550 " }\n" 9551 "}\n", 9552 AllmanBraceStyle); 9553 9554 verifyFormat("enum X\n" 9555 "{\n" 9556 " Y = 0,\n" 9557 "}\n", 9558 AllmanBraceStyle); 9559 verifyFormat("enum X\n" 9560 "{\n" 9561 " Y = 0\n" 9562 "}\n", 9563 AllmanBraceStyle); 9564 9565 verifyFormat("@interface BSApplicationController ()\n" 9566 "{\n" 9567 "@private\n" 9568 " id _extraIvar;\n" 9569 "}\n" 9570 "@end\n", 9571 AllmanBraceStyle); 9572 9573 verifyFormat("#ifdef _DEBUG\n" 9574 "int foo(int i = 0)\n" 9575 "#else\n" 9576 "int foo(int i = 5)\n" 9577 "#endif\n" 9578 "{\n" 9579 " return i;\n" 9580 "}", 9581 AllmanBraceStyle); 9582 9583 verifyFormat("void foo() {}\n" 9584 "void bar()\n" 9585 "#ifdef _DEBUG\n" 9586 "{\n" 9587 " foo();\n" 9588 "}\n" 9589 "#else\n" 9590 "{\n" 9591 "}\n" 9592 "#endif", 9593 AllmanBraceStyle); 9594 9595 verifyFormat("void foobar() { int i = 5; }\n" 9596 "#ifdef _DEBUG\n" 9597 "void bar() {}\n" 9598 "#else\n" 9599 "void bar() { foobar(); }\n" 9600 "#endif", 9601 AllmanBraceStyle); 9602 9603 // This shouldn't affect ObjC blocks.. 9604 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 9605 " // ...\n" 9606 " int i;\n" 9607 "}];", 9608 AllmanBraceStyle); 9609 verifyFormat("void (^block)(void) = ^{\n" 9610 " // ...\n" 9611 " int i;\n" 9612 "};", 9613 AllmanBraceStyle); 9614 // .. or dict literals. 9615 verifyFormat("void f()\n" 9616 "{\n" 9617 " // ...\n" 9618 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 9619 "}", 9620 AllmanBraceStyle); 9621 verifyFormat("void f()\n" 9622 "{\n" 9623 " // ...\n" 9624 " [object someMethod:@{a : @\"b\"}];\n" 9625 "}", 9626 AllmanBraceStyle); 9627 verifyFormat("int f()\n" 9628 "{ // comment\n" 9629 " return 42;\n" 9630 "}", 9631 AllmanBraceStyle); 9632 9633 AllmanBraceStyle.ColumnLimit = 19; 9634 verifyFormat("void f() { int i; }", AllmanBraceStyle); 9635 AllmanBraceStyle.ColumnLimit = 18; 9636 verifyFormat("void f()\n" 9637 "{\n" 9638 " int i;\n" 9639 "}", 9640 AllmanBraceStyle); 9641 AllmanBraceStyle.ColumnLimit = 80; 9642 9643 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle; 9644 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true; 9645 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 9646 verifyFormat("void f(bool b)\n" 9647 "{\n" 9648 " if (b)\n" 9649 " {\n" 9650 " return;\n" 9651 " }\n" 9652 "}\n", 9653 BreakBeforeBraceShortIfs); 9654 verifyFormat("void f(bool b)\n" 9655 "{\n" 9656 " if constexpr (b)\n" 9657 " {\n" 9658 " return;\n" 9659 " }\n" 9660 "}\n", 9661 BreakBeforeBraceShortIfs); 9662 verifyFormat("void f(bool b)\n" 9663 "{\n" 9664 " if (b) return;\n" 9665 "}\n", 9666 BreakBeforeBraceShortIfs); 9667 verifyFormat("void f(bool b)\n" 9668 "{\n" 9669 " if constexpr (b) return;\n" 9670 "}\n", 9671 BreakBeforeBraceShortIfs); 9672 verifyFormat("void f(bool b)\n" 9673 "{\n" 9674 " while (b)\n" 9675 " {\n" 9676 " return;\n" 9677 " }\n" 9678 "}\n", 9679 BreakBeforeBraceShortIfs); 9680 } 9681 9682 TEST_F(FormatTest, GNUBraceBreaking) { 9683 FormatStyle GNUBraceStyle = getLLVMStyle(); 9684 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU; 9685 verifyFormat("namespace a\n" 9686 "{\n" 9687 "class A\n" 9688 "{\n" 9689 " void f()\n" 9690 " {\n" 9691 " int a;\n" 9692 " {\n" 9693 " int b;\n" 9694 " }\n" 9695 " if (true)\n" 9696 " {\n" 9697 " a();\n" 9698 " b();\n" 9699 " }\n" 9700 " }\n" 9701 " void g() { return; }\n" 9702 "}\n" 9703 "} // namespace a", 9704 GNUBraceStyle); 9705 9706 verifyFormat("void f()\n" 9707 "{\n" 9708 " if (true)\n" 9709 " {\n" 9710 " a();\n" 9711 " }\n" 9712 " else if (false)\n" 9713 " {\n" 9714 " b();\n" 9715 " }\n" 9716 " else\n" 9717 " {\n" 9718 " c();\n" 9719 " }\n" 9720 "}\n", 9721 GNUBraceStyle); 9722 9723 verifyFormat("void f()\n" 9724 "{\n" 9725 " for (int i = 0; i < 10; ++i)\n" 9726 " {\n" 9727 " a();\n" 9728 " }\n" 9729 " while (false)\n" 9730 " {\n" 9731 " b();\n" 9732 " }\n" 9733 " do\n" 9734 " {\n" 9735 " c();\n" 9736 " }\n" 9737 " while (false);\n" 9738 "}\n", 9739 GNUBraceStyle); 9740 9741 verifyFormat("void f(int a)\n" 9742 "{\n" 9743 " switch (a)\n" 9744 " {\n" 9745 " case 0:\n" 9746 " break;\n" 9747 " case 1:\n" 9748 " {\n" 9749 " break;\n" 9750 " }\n" 9751 " case 2:\n" 9752 " {\n" 9753 " }\n" 9754 " break;\n" 9755 " default:\n" 9756 " break;\n" 9757 " }\n" 9758 "}\n", 9759 GNUBraceStyle); 9760 9761 verifyFormat("enum X\n" 9762 "{\n" 9763 " Y = 0,\n" 9764 "}\n", 9765 GNUBraceStyle); 9766 9767 verifyFormat("@interface BSApplicationController ()\n" 9768 "{\n" 9769 "@private\n" 9770 " id _extraIvar;\n" 9771 "}\n" 9772 "@end\n", 9773 GNUBraceStyle); 9774 9775 verifyFormat("#ifdef _DEBUG\n" 9776 "int foo(int i = 0)\n" 9777 "#else\n" 9778 "int foo(int i = 5)\n" 9779 "#endif\n" 9780 "{\n" 9781 " return i;\n" 9782 "}", 9783 GNUBraceStyle); 9784 9785 verifyFormat("void foo() {}\n" 9786 "void bar()\n" 9787 "#ifdef _DEBUG\n" 9788 "{\n" 9789 " foo();\n" 9790 "}\n" 9791 "#else\n" 9792 "{\n" 9793 "}\n" 9794 "#endif", 9795 GNUBraceStyle); 9796 9797 verifyFormat("void foobar() { int i = 5; }\n" 9798 "#ifdef _DEBUG\n" 9799 "void bar() {}\n" 9800 "#else\n" 9801 "void bar() { foobar(); }\n" 9802 "#endif", 9803 GNUBraceStyle); 9804 } 9805 9806 TEST_F(FormatTest, WebKitBraceBreaking) { 9807 FormatStyle WebKitBraceStyle = getLLVMStyle(); 9808 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit; 9809 WebKitBraceStyle.FixNamespaceComments = false; 9810 verifyFormat("namespace a {\n" 9811 "class A {\n" 9812 " void f()\n" 9813 " {\n" 9814 " if (true) {\n" 9815 " a();\n" 9816 " b();\n" 9817 " }\n" 9818 " }\n" 9819 " void g() { return; }\n" 9820 "};\n" 9821 "enum E {\n" 9822 " A,\n" 9823 " // foo\n" 9824 " B,\n" 9825 " C\n" 9826 "};\n" 9827 "struct B {\n" 9828 " int x;\n" 9829 "};\n" 9830 "}\n", 9831 WebKitBraceStyle); 9832 verifyFormat("struct S {\n" 9833 " int Type;\n" 9834 " union {\n" 9835 " int x;\n" 9836 " double y;\n" 9837 " } Value;\n" 9838 " class C {\n" 9839 " MyFavoriteType Value;\n" 9840 " } Class;\n" 9841 "};\n", 9842 WebKitBraceStyle); 9843 } 9844 9845 TEST_F(FormatTest, CatchExceptionReferenceBinding) { 9846 verifyFormat("void f() {\n" 9847 " try {\n" 9848 " } catch (const Exception &e) {\n" 9849 " }\n" 9850 "}\n", 9851 getLLVMStyle()); 9852 } 9853 9854 TEST_F(FormatTest, UnderstandsPragmas) { 9855 verifyFormat("#pragma omp reduction(| : var)"); 9856 verifyFormat("#pragma omp reduction(+ : var)"); 9857 9858 EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string " 9859 "(including parentheses).", 9860 format("#pragma mark Any non-hyphenated or hyphenated string " 9861 "(including parentheses).")); 9862 } 9863 9864 TEST_F(FormatTest, UnderstandPragmaOption) { 9865 verifyFormat("#pragma option -C -A"); 9866 9867 EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A")); 9868 } 9869 9870 #define EXPECT_ALL_STYLES_EQUAL(Styles) \ 9871 for (size_t i = 1; i < Styles.size(); ++i) \ 9872 EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \ 9873 << " differs from Style #0" 9874 9875 TEST_F(FormatTest, GetsPredefinedStyleByName) { 9876 SmallVector<FormatStyle, 3> Styles; 9877 Styles.resize(3); 9878 9879 Styles[0] = getLLVMStyle(); 9880 EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1])); 9881 EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2])); 9882 EXPECT_ALL_STYLES_EQUAL(Styles); 9883 9884 Styles[0] = getGoogleStyle(); 9885 EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1])); 9886 EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2])); 9887 EXPECT_ALL_STYLES_EQUAL(Styles); 9888 9889 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 9890 EXPECT_TRUE( 9891 getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1])); 9892 EXPECT_TRUE( 9893 getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2])); 9894 EXPECT_ALL_STYLES_EQUAL(Styles); 9895 9896 Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp); 9897 EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1])); 9898 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2])); 9899 EXPECT_ALL_STYLES_EQUAL(Styles); 9900 9901 Styles[0] = getMozillaStyle(); 9902 EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1])); 9903 EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2])); 9904 EXPECT_ALL_STYLES_EQUAL(Styles); 9905 9906 Styles[0] = getWebKitStyle(); 9907 EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1])); 9908 EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2])); 9909 EXPECT_ALL_STYLES_EQUAL(Styles); 9910 9911 Styles[0] = getGNUStyle(); 9912 EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1])); 9913 EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2])); 9914 EXPECT_ALL_STYLES_EQUAL(Styles); 9915 9916 EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0])); 9917 } 9918 9919 TEST_F(FormatTest, GetsCorrectBasedOnStyle) { 9920 SmallVector<FormatStyle, 8> Styles; 9921 Styles.resize(2); 9922 9923 Styles[0] = getGoogleStyle(); 9924 Styles[1] = getLLVMStyle(); 9925 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 9926 EXPECT_ALL_STYLES_EQUAL(Styles); 9927 9928 Styles.resize(5); 9929 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 9930 Styles[1] = getLLVMStyle(); 9931 Styles[1].Language = FormatStyle::LK_JavaScript; 9932 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 9933 9934 Styles[2] = getLLVMStyle(); 9935 Styles[2].Language = FormatStyle::LK_JavaScript; 9936 EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n" 9937 "BasedOnStyle: Google", 9938 &Styles[2]) 9939 .value()); 9940 9941 Styles[3] = getLLVMStyle(); 9942 Styles[3].Language = FormatStyle::LK_JavaScript; 9943 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n" 9944 "Language: JavaScript", 9945 &Styles[3]) 9946 .value()); 9947 9948 Styles[4] = getLLVMStyle(); 9949 Styles[4].Language = FormatStyle::LK_JavaScript; 9950 EXPECT_EQ(0, parseConfiguration("---\n" 9951 "BasedOnStyle: LLVM\n" 9952 "IndentWidth: 123\n" 9953 "---\n" 9954 "BasedOnStyle: Google\n" 9955 "Language: JavaScript", 9956 &Styles[4]) 9957 .value()); 9958 EXPECT_ALL_STYLES_EQUAL(Styles); 9959 } 9960 9961 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \ 9962 Style.FIELD = false; \ 9963 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \ 9964 EXPECT_TRUE(Style.FIELD); \ 9965 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \ 9966 EXPECT_FALSE(Style.FIELD); 9967 9968 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD) 9969 9970 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \ 9971 Style.STRUCT.FIELD = false; \ 9972 EXPECT_EQ(0, \ 9973 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \ 9974 .value()); \ 9975 EXPECT_TRUE(Style.STRUCT.FIELD); \ 9976 EXPECT_EQ(0, \ 9977 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \ 9978 .value()); \ 9979 EXPECT_FALSE(Style.STRUCT.FIELD); 9980 9981 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \ 9982 CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD) 9983 9984 #define CHECK_PARSE(TEXT, FIELD, VALUE) \ 9985 EXPECT_NE(VALUE, Style.FIELD); \ 9986 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \ 9987 EXPECT_EQ(VALUE, Style.FIELD) 9988 9989 TEST_F(FormatTest, ParsesConfigurationBools) { 9990 FormatStyle Style = {}; 9991 Style.Language = FormatStyle::LK_Cpp; 9992 CHECK_PARSE_BOOL(AlignOperands); 9993 CHECK_PARSE_BOOL(AlignTrailingComments); 9994 CHECK_PARSE_BOOL(AlignConsecutiveAssignments); 9995 CHECK_PARSE_BOOL(AlignConsecutiveDeclarations); 9996 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); 9997 CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine); 9998 CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine); 9999 CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine); 10000 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); 10001 CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations); 10002 CHECK_PARSE_BOOL(BinPackArguments); 10003 CHECK_PARSE_BOOL(BinPackParameters); 10004 CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations); 10005 CHECK_PARSE_BOOL(BreakBeforeTernaryOperators); 10006 CHECK_PARSE_BOOL(BreakStringLiterals); 10007 CHECK_PARSE_BOOL(BreakBeforeInheritanceComma) 10008 CHECK_PARSE_BOOL(CompactNamespaces); 10009 CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine); 10010 CHECK_PARSE_BOOL(DerivePointerAlignment); 10011 CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding"); 10012 CHECK_PARSE_BOOL(DisableFormat); 10013 CHECK_PARSE_BOOL(IndentCaseLabels); 10014 CHECK_PARSE_BOOL(IndentWrappedFunctionNames); 10015 CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks); 10016 CHECK_PARSE_BOOL(ObjCSpaceAfterProperty); 10017 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList); 10018 CHECK_PARSE_BOOL(Cpp11BracedListStyle); 10019 CHECK_PARSE_BOOL(ReflowComments); 10020 CHECK_PARSE_BOOL(SortIncludes); 10021 CHECK_PARSE_BOOL(SortUsingDeclarations); 10022 CHECK_PARSE_BOOL(SpacesInParentheses); 10023 CHECK_PARSE_BOOL(SpacesInSquareBrackets); 10024 CHECK_PARSE_BOOL(SpacesInAngles); 10025 CHECK_PARSE_BOOL(SpaceInEmptyParentheses); 10026 CHECK_PARSE_BOOL(SpacesInContainerLiterals); 10027 CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); 10028 CHECK_PARSE_BOOL(SpaceAfterCStyleCast); 10029 CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword); 10030 CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); 10031 10032 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass); 10033 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement); 10034 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum); 10035 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction); 10036 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace); 10037 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration); 10038 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct); 10039 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion); 10040 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock); 10041 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch); 10042 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse); 10043 CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces); 10044 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction); 10045 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord); 10046 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace); 10047 } 10048 10049 #undef CHECK_PARSE_BOOL 10050 10051 TEST_F(FormatTest, ParsesConfiguration) { 10052 FormatStyle Style = {}; 10053 Style.Language = FormatStyle::LK_Cpp; 10054 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234); 10055 CHECK_PARSE("ConstructorInitializerIndentWidth: 1234", 10056 ConstructorInitializerIndentWidth, 1234u); 10057 CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u); 10058 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); 10059 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u); 10060 CHECK_PARSE("PenaltyBreakAssignment: 1234", 10061 PenaltyBreakAssignment, 1234u); 10062 CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234", 10063 PenaltyBreakBeforeFirstCallParameter, 1234u); 10064 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); 10065 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234", 10066 PenaltyReturnTypeOnItsOwnLine, 1234u); 10067 CHECK_PARSE("SpacesBeforeTrailingComments: 1234", 10068 SpacesBeforeTrailingComments, 1234u); 10069 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u); 10070 CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u); 10071 CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$"); 10072 10073 Style.PointerAlignment = FormatStyle::PAS_Middle; 10074 CHECK_PARSE("PointerAlignment: Left", PointerAlignment, 10075 FormatStyle::PAS_Left); 10076 CHECK_PARSE("PointerAlignment: Right", PointerAlignment, 10077 FormatStyle::PAS_Right); 10078 CHECK_PARSE("PointerAlignment: Middle", PointerAlignment, 10079 FormatStyle::PAS_Middle); 10080 // For backward compatibility: 10081 CHECK_PARSE("PointerBindsToType: Left", PointerAlignment, 10082 FormatStyle::PAS_Left); 10083 CHECK_PARSE("PointerBindsToType: Right", PointerAlignment, 10084 FormatStyle::PAS_Right); 10085 CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment, 10086 FormatStyle::PAS_Middle); 10087 10088 Style.Standard = FormatStyle::LS_Auto; 10089 CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03); 10090 CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11); 10091 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03); 10092 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11); 10093 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto); 10094 10095 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 10096 CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment", 10097 BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment); 10098 CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators, 10099 FormatStyle::BOS_None); 10100 CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators, 10101 FormatStyle::BOS_All); 10102 // For backward compatibility: 10103 CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators, 10104 FormatStyle::BOS_None); 10105 CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators, 10106 FormatStyle::BOS_All); 10107 10108 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 10109 CHECK_PARSE("BreakConstructorInitializers: BeforeComma", 10110 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 10111 CHECK_PARSE("BreakConstructorInitializers: AfterColon", 10112 BreakConstructorInitializers, FormatStyle::BCIS_AfterColon); 10113 CHECK_PARSE("BreakConstructorInitializers: BeforeColon", 10114 BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon); 10115 // For backward compatibility: 10116 CHECK_PARSE("BreakConstructorInitializersBeforeComma: true", 10117 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 10118 10119 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 10120 CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket, 10121 FormatStyle::BAS_Align); 10122 CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket, 10123 FormatStyle::BAS_DontAlign); 10124 CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket, 10125 FormatStyle::BAS_AlwaysBreak); 10126 // For backward compatibility: 10127 CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket, 10128 FormatStyle::BAS_DontAlign); 10129 CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket, 10130 FormatStyle::BAS_Align); 10131 10132 Style.AlignEscapedNewlines = FormatStyle::ENAS_Left; 10133 CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines, 10134 FormatStyle::ENAS_DontAlign); 10135 CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines, 10136 FormatStyle::ENAS_Left); 10137 CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines, 10138 FormatStyle::ENAS_Right); 10139 // For backward compatibility: 10140 CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines, 10141 FormatStyle::ENAS_Left); 10142 CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines, 10143 FormatStyle::ENAS_Right); 10144 10145 Style.UseTab = FormatStyle::UT_ForIndentation; 10146 CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never); 10147 CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation); 10148 CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always); 10149 CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab, 10150 FormatStyle::UT_ForContinuationAndIndentation); 10151 // For backward compatibility: 10152 CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never); 10153 CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always); 10154 10155 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 10156 CHECK_PARSE("AllowShortFunctionsOnASingleLine: None", 10157 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 10158 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline", 10159 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline); 10160 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty", 10161 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty); 10162 CHECK_PARSE("AllowShortFunctionsOnASingleLine: All", 10163 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 10164 // For backward compatibility: 10165 CHECK_PARSE("AllowShortFunctionsOnASingleLine: false", 10166 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 10167 CHECK_PARSE("AllowShortFunctionsOnASingleLine: true", 10168 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 10169 10170 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 10171 CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens, 10172 FormatStyle::SBPO_Never); 10173 CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens, 10174 FormatStyle::SBPO_Always); 10175 CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens, 10176 FormatStyle::SBPO_ControlStatements); 10177 // For backward compatibility: 10178 CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens, 10179 FormatStyle::SBPO_Never); 10180 CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens, 10181 FormatStyle::SBPO_ControlStatements); 10182 10183 Style.ColumnLimit = 123; 10184 FormatStyle BaseStyle = getLLVMStyle(); 10185 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit); 10186 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u); 10187 10188 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 10189 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces, 10190 FormatStyle::BS_Attach); 10191 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces, 10192 FormatStyle::BS_Linux); 10193 CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces, 10194 FormatStyle::BS_Mozilla); 10195 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces, 10196 FormatStyle::BS_Stroustrup); 10197 CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces, 10198 FormatStyle::BS_Allman); 10199 CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU); 10200 CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces, 10201 FormatStyle::BS_WebKit); 10202 CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces, 10203 FormatStyle::BS_Custom); 10204 10205 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 10206 CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType, 10207 FormatStyle::RTBS_None); 10208 CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType, 10209 FormatStyle::RTBS_All); 10210 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel", 10211 AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel); 10212 CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions", 10213 AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions); 10214 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions", 10215 AlwaysBreakAfterReturnType, 10216 FormatStyle::RTBS_TopLevelDefinitions); 10217 10218 Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All; 10219 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None", 10220 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None); 10221 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All", 10222 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All); 10223 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel", 10224 AlwaysBreakAfterDefinitionReturnType, 10225 FormatStyle::DRTBS_TopLevel); 10226 10227 Style.NamespaceIndentation = FormatStyle::NI_All; 10228 CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation, 10229 FormatStyle::NI_None); 10230 CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation, 10231 FormatStyle::NI_Inner); 10232 CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation, 10233 FormatStyle::NI_All); 10234 10235 // FIXME: This is required because parsing a configuration simply overwrites 10236 // the first N elements of the list instead of resetting it. 10237 Style.ForEachMacros.clear(); 10238 std::vector<std::string> BoostForeach; 10239 BoostForeach.push_back("BOOST_FOREACH"); 10240 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach); 10241 std::vector<std::string> BoostAndQForeach; 10242 BoostAndQForeach.push_back("BOOST_FOREACH"); 10243 BoostAndQForeach.push_back("Q_FOREACH"); 10244 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros, 10245 BoostAndQForeach); 10246 10247 Style.IncludeCategories.clear(); 10248 std::vector<FormatStyle::IncludeCategory> ExpectedCategories = {{"abc/.*", 2}, 10249 {".*", 1}}; 10250 CHECK_PARSE("IncludeCategories:\n" 10251 " - Regex: abc/.*\n" 10252 " Priority: 2\n" 10253 " - Regex: .*\n" 10254 " Priority: 1", 10255 IncludeCategories, ExpectedCategories); 10256 CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeIsMainRegex, "abc$"); 10257 } 10258 10259 TEST_F(FormatTest, ParsesConfigurationWithLanguages) { 10260 FormatStyle Style = {}; 10261 Style.Language = FormatStyle::LK_Cpp; 10262 CHECK_PARSE("Language: Cpp\n" 10263 "IndentWidth: 12", 10264 IndentWidth, 12u); 10265 EXPECT_EQ(parseConfiguration("Language: JavaScript\n" 10266 "IndentWidth: 34", 10267 &Style), 10268 ParseError::Unsuitable); 10269 EXPECT_EQ(12u, Style.IndentWidth); 10270 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 10271 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 10272 10273 Style.Language = FormatStyle::LK_JavaScript; 10274 CHECK_PARSE("Language: JavaScript\n" 10275 "IndentWidth: 12", 10276 IndentWidth, 12u); 10277 CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u); 10278 EXPECT_EQ(parseConfiguration("Language: Cpp\n" 10279 "IndentWidth: 34", 10280 &Style), 10281 ParseError::Unsuitable); 10282 EXPECT_EQ(23u, Style.IndentWidth); 10283 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 10284 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 10285 10286 CHECK_PARSE("BasedOnStyle: LLVM\n" 10287 "IndentWidth: 67", 10288 IndentWidth, 67u); 10289 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, 12u); 10298 10299 Style.Language = FormatStyle::LK_Cpp; 10300 CHECK_PARSE("---\n" 10301 "Language: JavaScript\n" 10302 "IndentWidth: 12\n" 10303 "---\n" 10304 "Language: Cpp\n" 10305 "IndentWidth: 34\n" 10306 "...\n", 10307 IndentWidth, 34u); 10308 CHECK_PARSE("---\n" 10309 "IndentWidth: 78\n" 10310 "---\n" 10311 "Language: JavaScript\n" 10312 "IndentWidth: 56\n" 10313 "...\n", 10314 IndentWidth, 78u); 10315 10316 Style.ColumnLimit = 123; 10317 Style.IndentWidth = 234; 10318 Style.BreakBeforeBraces = FormatStyle::BS_Linux; 10319 Style.TabWidth = 345; 10320 EXPECT_FALSE(parseConfiguration("---\n" 10321 "IndentWidth: 456\n" 10322 "BreakBeforeBraces: Allman\n" 10323 "---\n" 10324 "Language: JavaScript\n" 10325 "IndentWidth: 111\n" 10326 "TabWidth: 111\n" 10327 "---\n" 10328 "Language: Cpp\n" 10329 "BreakBeforeBraces: Stroustrup\n" 10330 "TabWidth: 789\n" 10331 "...\n", 10332 &Style)); 10333 EXPECT_EQ(123u, Style.ColumnLimit); 10334 EXPECT_EQ(456u, Style.IndentWidth); 10335 EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces); 10336 EXPECT_EQ(789u, Style.TabWidth); 10337 10338 EXPECT_EQ(parseConfiguration("---\n" 10339 "Language: JavaScript\n" 10340 "IndentWidth: 56\n" 10341 "---\n" 10342 "IndentWidth: 78\n" 10343 "...\n", 10344 &Style), 10345 ParseError::Error); 10346 EXPECT_EQ(parseConfiguration("---\n" 10347 "Language: JavaScript\n" 10348 "IndentWidth: 56\n" 10349 "---\n" 10350 "Language: JavaScript\n" 10351 "IndentWidth: 78\n" 10352 "...\n", 10353 &Style), 10354 ParseError::Error); 10355 10356 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 10357 } 10358 10359 #undef CHECK_PARSE 10360 10361 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) { 10362 FormatStyle Style = {}; 10363 Style.Language = FormatStyle::LK_JavaScript; 10364 Style.BreakBeforeTernaryOperators = true; 10365 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value()); 10366 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 10367 10368 Style.BreakBeforeTernaryOperators = true; 10369 EXPECT_EQ(0, parseConfiguration("---\n" 10370 "BasedOnStyle: Google\n" 10371 "---\n" 10372 "Language: JavaScript\n" 10373 "IndentWidth: 76\n" 10374 "...\n", 10375 &Style) 10376 .value()); 10377 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 10378 EXPECT_EQ(76u, Style.IndentWidth); 10379 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 10380 } 10381 10382 TEST_F(FormatTest, ConfigurationRoundTripTest) { 10383 FormatStyle Style = getLLVMStyle(); 10384 std::string YAML = configurationAsText(Style); 10385 FormatStyle ParsedStyle = {}; 10386 ParsedStyle.Language = FormatStyle::LK_Cpp; 10387 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value()); 10388 EXPECT_EQ(Style, ParsedStyle); 10389 } 10390 10391 TEST_F(FormatTest, WorksFor8bitEncodings) { 10392 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" 10393 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" 10394 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" 10395 "\"\xef\xee\xf0\xf3...\"", 10396 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " 10397 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " 10398 "\xef\xee\xf0\xf3...\"", 10399 getLLVMStyleWithColumns(12))); 10400 } 10401 10402 TEST_F(FormatTest, HandlesUTF8BOM) { 10403 EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf")); 10404 EXPECT_EQ("\xef\xbb\xbf#include <iostream>", 10405 format("\xef\xbb\xbf#include <iostream>")); 10406 EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>", 10407 format("\xef\xbb\xbf\n#include <iostream>")); 10408 } 10409 10410 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. 10411 #if !defined(_MSC_VER) 10412 10413 TEST_F(FormatTest, CountsUTF8CharactersProperly) { 10414 verifyFormat("\"Однажды в студёную зимнюю пору...\"", 10415 getLLVMStyleWithColumns(35)); 10416 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", 10417 getLLVMStyleWithColumns(31)); 10418 verifyFormat("// Однажды в студёную зимнюю пору...", 10419 getLLVMStyleWithColumns(36)); 10420 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32)); 10421 verifyFormat("/* Однажды в студёную зимнюю пору... */", 10422 getLLVMStyleWithColumns(39)); 10423 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", 10424 getLLVMStyleWithColumns(35)); 10425 } 10426 10427 TEST_F(FormatTest, SplitsUTF8Strings) { 10428 // Non-printable characters' width is currently considered to be the length in 10429 // bytes in UTF8. The characters can be displayed in very different manner 10430 // (zero-width, single width with a substitution glyph, expanded to their code 10431 // (e.g. "<8d>"), so there's no single correct way to handle them. 10432 EXPECT_EQ("\"aaaaÄ\"\n" 10433 "\"\xc2\x8d\";", 10434 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 10435 EXPECT_EQ("\"aaaaaaaÄ\"\n" 10436 "\"\xc2\x8d\";", 10437 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 10438 EXPECT_EQ("\"Однажды, в \"\n" 10439 "\"студёную \"\n" 10440 "\"зимнюю \"\n" 10441 "\"пору,\"", 10442 format("\"Однажды, в студёную зимнюю пору,\"", 10443 getLLVMStyleWithColumns(13))); 10444 EXPECT_EQ( 10445 "\"一 二 三 \"\n" 10446 "\"四 五六 \"\n" 10447 "\"七 八 九 \"\n" 10448 "\"十\"", 10449 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); 10450 EXPECT_EQ("\"一\t二 \"\n" 10451 "\"\t三 \"\n" 10452 "\"四 五\t六 \"\n" 10453 "\"\t七 \"\n" 10454 "\"八九十\tqq\"", 10455 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", 10456 getLLVMStyleWithColumns(11))); 10457 10458 // UTF8 character in an escape sequence. 10459 EXPECT_EQ("\"aaaaaa\"\n" 10460 "\"\\\xC2\x8D\"", 10461 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10))); 10462 } 10463 10464 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { 10465 EXPECT_EQ("const char *sssss =\n" 10466 " \"一二三四五六七八\\\n" 10467 " 九 十\";", 10468 format("const char *sssss = \"一二三四五六七八\\\n" 10469 " 九 十\";", 10470 getLLVMStyleWithColumns(30))); 10471 } 10472 10473 TEST_F(FormatTest, SplitsUTF8LineComments) { 10474 EXPECT_EQ("// aaaaÄ\xc2\x8d", 10475 format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10))); 10476 EXPECT_EQ("// Я из лесу\n" 10477 "// вышел; был\n" 10478 "// сильный\n" 10479 "// мороз.", 10480 format("// Я из лесу вышел; был сильный мороз.", 10481 getLLVMStyleWithColumns(13))); 10482 EXPECT_EQ("// 一二三\n" 10483 "// 四五六七\n" 10484 "// 八 九\n" 10485 "// 十", 10486 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9))); 10487 } 10488 10489 TEST_F(FormatTest, SplitsUTF8BlockComments) { 10490 EXPECT_EQ("/* Гляжу,\n" 10491 " * поднимается\n" 10492 " * медленно в\n" 10493 " * гору\n" 10494 " * Лошадка,\n" 10495 " * везущая\n" 10496 " * хворосту\n" 10497 " * воз. */", 10498 format("/* Гляжу, поднимается медленно в гору\n" 10499 " * Лошадка, везущая хворосту воз. */", 10500 getLLVMStyleWithColumns(13))); 10501 EXPECT_EQ( 10502 "/* 一二三\n" 10503 " * 四五六七\n" 10504 " * 八 九\n" 10505 " * 十 */", 10506 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9))); 10507 EXPECT_EQ("/* \n" 10508 " * \n" 10509 " * - */", 10510 format("/* - */", getLLVMStyleWithColumns(12))); 10511 } 10512 10513 #endif // _MSC_VER 10514 10515 TEST_F(FormatTest, ConstructorInitializerIndentWidth) { 10516 FormatStyle Style = getLLVMStyle(); 10517 10518 Style.ConstructorInitializerIndentWidth = 4; 10519 verifyFormat( 10520 "SomeClass::Constructor()\n" 10521 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10522 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10523 Style); 10524 10525 Style.ConstructorInitializerIndentWidth = 2; 10526 verifyFormat( 10527 "SomeClass::Constructor()\n" 10528 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10529 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10530 Style); 10531 10532 Style.ConstructorInitializerIndentWidth = 0; 10533 verifyFormat( 10534 "SomeClass::Constructor()\n" 10535 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10536 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10537 Style); 10538 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 10539 verifyFormat( 10540 "SomeLongTemplateVariableName<\n" 10541 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>", 10542 Style); 10543 verifyFormat( 10544 "bool smaller = 1 < bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 10545 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 10546 Style); 10547 } 10548 10549 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) { 10550 FormatStyle Style = getLLVMStyle(); 10551 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 10552 Style.ConstructorInitializerIndentWidth = 4; 10553 verifyFormat("SomeClass::Constructor()\n" 10554 " : a(a)\n" 10555 " , b(b)\n" 10556 " , c(c) {}", 10557 Style); 10558 verifyFormat("SomeClass::Constructor()\n" 10559 " : a(a) {}", 10560 Style); 10561 10562 Style.ColumnLimit = 0; 10563 verifyFormat("SomeClass::Constructor()\n" 10564 " : a(a) {}", 10565 Style); 10566 verifyFormat("SomeClass::Constructor() noexcept\n" 10567 " : a(a) {}", 10568 Style); 10569 verifyFormat("SomeClass::Constructor()\n" 10570 " : a(a)\n" 10571 " , b(b)\n" 10572 " , c(c) {}", 10573 Style); 10574 verifyFormat("SomeClass::Constructor()\n" 10575 " : a(a) {\n" 10576 " foo();\n" 10577 " bar();\n" 10578 "}", 10579 Style); 10580 10581 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 10582 verifyFormat("SomeClass::Constructor()\n" 10583 " : a(a)\n" 10584 " , b(b)\n" 10585 " , c(c) {\n}", 10586 Style); 10587 verifyFormat("SomeClass::Constructor()\n" 10588 " : a(a) {\n}", 10589 Style); 10590 10591 Style.ColumnLimit = 80; 10592 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 10593 Style.ConstructorInitializerIndentWidth = 2; 10594 verifyFormat("SomeClass::Constructor()\n" 10595 " : a(a)\n" 10596 " , b(b)\n" 10597 " , c(c) {}", 10598 Style); 10599 10600 Style.ConstructorInitializerIndentWidth = 0; 10601 verifyFormat("SomeClass::Constructor()\n" 10602 ": a(a)\n" 10603 ", b(b)\n" 10604 ", c(c) {}", 10605 Style); 10606 10607 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 10608 Style.ConstructorInitializerIndentWidth = 4; 10609 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style); 10610 verifyFormat( 10611 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n", 10612 Style); 10613 verifyFormat( 10614 "SomeClass::Constructor()\n" 10615 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", 10616 Style); 10617 Style.ConstructorInitializerIndentWidth = 4; 10618 Style.ColumnLimit = 60; 10619 verifyFormat("SomeClass::Constructor()\n" 10620 " : aaaaaaaa(aaaaaaaa)\n" 10621 " , aaaaaaaa(aaaaaaaa)\n" 10622 " , aaaaaaaa(aaaaaaaa) {}", 10623 Style); 10624 } 10625 10626 TEST_F(FormatTest, Destructors) { 10627 verifyFormat("void F(int &i) { i.~int(); }"); 10628 verifyFormat("void F(int &i) { i->~int(); }"); 10629 } 10630 10631 TEST_F(FormatTest, FormatsWithWebKitStyle) { 10632 FormatStyle Style = getWebKitStyle(); 10633 10634 // Don't indent in outer namespaces. 10635 verifyFormat("namespace outer {\n" 10636 "int i;\n" 10637 "namespace inner {\n" 10638 " int i;\n" 10639 "} // namespace inner\n" 10640 "} // namespace outer\n" 10641 "namespace other_outer {\n" 10642 "int i;\n" 10643 "}", 10644 Style); 10645 10646 // Don't indent case labels. 10647 verifyFormat("switch (variable) {\n" 10648 "case 1:\n" 10649 "case 2:\n" 10650 " doSomething();\n" 10651 " break;\n" 10652 "default:\n" 10653 " ++variable;\n" 10654 "}", 10655 Style); 10656 10657 // Wrap before binary operators. 10658 EXPECT_EQ("void f()\n" 10659 "{\n" 10660 " if (aaaaaaaaaaaaaaaa\n" 10661 " && bbbbbbbbbbbbbbbbbbbbbbbb\n" 10662 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 10663 " return;\n" 10664 "}", 10665 format("void f() {\n" 10666 "if (aaaaaaaaaaaaaaaa\n" 10667 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" 10668 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 10669 "return;\n" 10670 "}", 10671 Style)); 10672 10673 // Allow functions on a single line. 10674 verifyFormat("void f() { return; }", Style); 10675 10676 // Constructor initializers are formatted one per line with the "," on the 10677 // new line. 10678 verifyFormat("Constructor()\n" 10679 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 10680 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" 10681 " aaaaaaaaaaaaaa)\n" 10682 " , aaaaaaaaaaaaaaaaaaaaaaa()\n" 10683 "{\n" 10684 "}", 10685 Style); 10686 verifyFormat("SomeClass::Constructor()\n" 10687 " : a(a)\n" 10688 "{\n" 10689 "}", 10690 Style); 10691 EXPECT_EQ("SomeClass::Constructor()\n" 10692 " : a(a)\n" 10693 "{\n" 10694 "}", 10695 format("SomeClass::Constructor():a(a){}", Style)); 10696 verifyFormat("SomeClass::Constructor()\n" 10697 " : a(a)\n" 10698 " , b(b)\n" 10699 " , c(c)\n" 10700 "{\n" 10701 "}", 10702 Style); 10703 verifyFormat("SomeClass::Constructor()\n" 10704 " : a(a)\n" 10705 "{\n" 10706 " foo();\n" 10707 " bar();\n" 10708 "}", 10709 Style); 10710 10711 // Access specifiers should be aligned left. 10712 verifyFormat("class C {\n" 10713 "public:\n" 10714 " int i;\n" 10715 "};", 10716 Style); 10717 10718 // Do not align comments. 10719 verifyFormat("int a; // Do not\n" 10720 "double b; // align comments.", 10721 Style); 10722 10723 // Do not align operands. 10724 EXPECT_EQ("ASSERT(aaaa\n" 10725 " || bbbb);", 10726 format("ASSERT ( aaaa\n||bbbb);", Style)); 10727 10728 // Accept input's line breaks. 10729 EXPECT_EQ("if (aaaaaaaaaaaaaaa\n" 10730 " || bbbbbbbbbbbbbbb) {\n" 10731 " i++;\n" 10732 "}", 10733 format("if (aaaaaaaaaaaaaaa\n" 10734 "|| bbbbbbbbbbbbbbb) { i++; }", 10735 Style)); 10736 EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n" 10737 " i++;\n" 10738 "}", 10739 format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style)); 10740 10741 // Don't automatically break all macro definitions (llvm.org/PR17842). 10742 verifyFormat("#define aNumber 10", Style); 10743 // However, generally keep the line breaks that the user authored. 10744 EXPECT_EQ("#define aNumber \\\n" 10745 " 10", 10746 format("#define aNumber \\\n" 10747 " 10", 10748 Style)); 10749 10750 // Keep empty and one-element array literals on a single line. 10751 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n" 10752 " copyItems:YES];", 10753 format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n" 10754 "copyItems:YES];", 10755 Style)); 10756 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n" 10757 " copyItems:YES];", 10758 format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n" 10759 " copyItems:YES];", 10760 Style)); 10761 // FIXME: This does not seem right, there should be more indentation before 10762 // the array literal's entries. Nested blocks have the same problem. 10763 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 10764 " @\"a\",\n" 10765 " @\"a\"\n" 10766 "]\n" 10767 " copyItems:YES];", 10768 format("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 10769 " @\"a\",\n" 10770 " @\"a\"\n" 10771 " ]\n" 10772 " copyItems:YES];", 10773 Style)); 10774 EXPECT_EQ( 10775 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 10776 " copyItems:YES];", 10777 format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 10778 " copyItems:YES];", 10779 Style)); 10780 10781 verifyFormat("[self.a b:c c:d];", Style); 10782 EXPECT_EQ("[self.a b:c\n" 10783 " c:d];", 10784 format("[self.a b:c\n" 10785 "c:d];", 10786 Style)); 10787 } 10788 10789 TEST_F(FormatTest, FormatsLambdas) { 10790 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n"); 10791 verifyFormat("int c = [&] { [=] { return b++; }(); }();\n"); 10792 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n"); 10793 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n"); 10794 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n"); 10795 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n"); 10796 verifyFormat("auto c = [a = [b = 42] {}] {};\n"); 10797 verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n"); 10798 verifyFormat("int x = f(*+[] {});"); 10799 verifyFormat("void f() {\n" 10800 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n" 10801 "}\n"); 10802 verifyFormat("void f() {\n" 10803 " other(x.begin(), //\n" 10804 " x.end(), //\n" 10805 " [&](int, int) { return 1; });\n" 10806 "}\n"); 10807 verifyFormat("SomeFunction([]() { // A cool function...\n" 10808 " return 43;\n" 10809 "});"); 10810 EXPECT_EQ("SomeFunction([]() {\n" 10811 "#define A a\n" 10812 " return 43;\n" 10813 "});", 10814 format("SomeFunction([](){\n" 10815 "#define A a\n" 10816 "return 43;\n" 10817 "});")); 10818 verifyFormat("void f() {\n" 10819 " SomeFunction([](decltype(x), A *a) {});\n" 10820 "}"); 10821 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10822 " [](const aaaaaaaaaa &a) { return a; });"); 10823 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n" 10824 " SomeOtherFunctioooooooooooooooooooooooooon();\n" 10825 "});"); 10826 verifyFormat("Constructor()\n" 10827 " : Field([] { // comment\n" 10828 " int i;\n" 10829 " }) {}"); 10830 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n" 10831 " return some_parameter.size();\n" 10832 "};"); 10833 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n" 10834 " [](const string &s) { return s; };"); 10835 verifyFormat("int i = aaaaaa ? 1 //\n" 10836 " : [] {\n" 10837 " return 2; //\n" 10838 " }();"); 10839 verifyFormat("llvm::errs() << \"number of twos is \"\n" 10840 " << std::count_if(v.begin(), v.end(), [](int x) {\n" 10841 " return x == 2; // force break\n" 10842 " });"); 10843 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10844 " [=](int iiiiiiiiiiii) {\n" 10845 " return aaaaaaaaaaaaaaaaaaaaaaa !=\n" 10846 " aaaaaaaaaaaaaaaaaaaaaaa;\n" 10847 " });", 10848 getLLVMStyleWithColumns(60)); 10849 verifyFormat("SomeFunction({[&] {\n" 10850 " // comment\n" 10851 " },\n" 10852 " [&] {\n" 10853 " // comment\n" 10854 " }});"); 10855 verifyFormat("SomeFunction({[&] {\n" 10856 " // comment\n" 10857 "}});"); 10858 verifyFormat("virtual aaaaaaaaaaaaaaaa(std::function<bool()> bbbbbbbbbbbb =\n" 10859 " [&]() { return true; },\n" 10860 " aaaaa aaaaaaaaa);"); 10861 10862 // Lambdas with return types. 10863 verifyFormat("int c = []() -> int { return 2; }();\n"); 10864 verifyFormat("int c = []() -> int * { return 2; }();\n"); 10865 verifyFormat("int c = []() -> vector<int> { return {2}; }();\n"); 10866 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());"); 10867 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};"); 10868 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};"); 10869 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};"); 10870 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};"); 10871 verifyFormat("[a, a]() -> a<1> {};"); 10872 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n" 10873 " int j) -> int {\n" 10874 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n" 10875 "};"); 10876 verifyFormat( 10877 "aaaaaaaaaaaaaaaaaaaaaa(\n" 10878 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n" 10879 " return aaaaaaaaaaaaaaaaa;\n" 10880 " });", 10881 getLLVMStyleWithColumns(70)); 10882 verifyFormat("[]() //\n" 10883 " -> int {\n" 10884 " return 1; //\n" 10885 "};"); 10886 10887 // Multiple lambdas in the same parentheses change indentation rules. 10888 verifyFormat("SomeFunction(\n" 10889 " []() {\n" 10890 " int i = 42;\n" 10891 " return i;\n" 10892 " },\n" 10893 " []() {\n" 10894 " int j = 43;\n" 10895 " return j;\n" 10896 " });"); 10897 10898 // More complex introducers. 10899 verifyFormat("return [i, args...] {};"); 10900 10901 // Not lambdas. 10902 verifyFormat("constexpr char hello[]{\"hello\"};"); 10903 verifyFormat("double &operator[](int i) { return 0; }\n" 10904 "int i;"); 10905 verifyFormat("std::unique_ptr<int[]> foo() {}"); 10906 verifyFormat("int i = a[a][a]->f();"); 10907 verifyFormat("int i = (*b)[a]->f();"); 10908 10909 // Other corner cases. 10910 verifyFormat("void f() {\n" 10911 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n" 10912 " );\n" 10913 "}"); 10914 10915 // Lambdas created through weird macros. 10916 verifyFormat("void f() {\n" 10917 " MACRO((const AA &a) { return 1; });\n" 10918 " MACRO((AA &a) { return 1; });\n" 10919 "}"); 10920 10921 verifyFormat("if (blah_blah(whatever, whatever, [] {\n" 10922 " doo_dah();\n" 10923 " doo_dah();\n" 10924 " })) {\n" 10925 "}"); 10926 verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n" 10927 " doo_dah();\n" 10928 " doo_dah();\n" 10929 " })) {\n" 10930 "}"); 10931 verifyFormat("auto lambda = []() {\n" 10932 " int a = 2\n" 10933 "#if A\n" 10934 " + 2\n" 10935 "#endif\n" 10936 " ;\n" 10937 "};"); 10938 10939 // Lambdas with complex multiline introducers. 10940 verifyFormat( 10941 "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10942 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n" 10943 " -> ::std::unordered_set<\n" 10944 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n" 10945 " //\n" 10946 " });"); 10947 } 10948 10949 TEST_F(FormatTest, FormatsBlocks) { 10950 FormatStyle ShortBlocks = getLLVMStyle(); 10951 ShortBlocks.AllowShortBlocksOnASingleLine = true; 10952 verifyFormat("int (^Block)(int, int);", ShortBlocks); 10953 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks); 10954 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks); 10955 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks); 10956 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks); 10957 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks); 10958 10959 verifyFormat("foo(^{ bar(); });", ShortBlocks); 10960 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks); 10961 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks); 10962 10963 verifyFormat("[operation setCompletionBlock:^{\n" 10964 " [self onOperationDone];\n" 10965 "}];"); 10966 verifyFormat("int i = {[operation setCompletionBlock:^{\n" 10967 " [self onOperationDone];\n" 10968 "}]};"); 10969 verifyFormat("[operation setCompletionBlock:^(int *i) {\n" 10970 " f();\n" 10971 "}];"); 10972 verifyFormat("int a = [operation block:^int(int *i) {\n" 10973 " return 1;\n" 10974 "}];"); 10975 verifyFormat("[myObject doSomethingWith:arg1\n" 10976 " aaa:^int(int *a) {\n" 10977 " return 1;\n" 10978 " }\n" 10979 " bbb:f(a * bbbbbbbb)];"); 10980 10981 verifyFormat("[operation setCompletionBlock:^{\n" 10982 " [self.delegate newDataAvailable];\n" 10983 "}];", 10984 getLLVMStyleWithColumns(60)); 10985 verifyFormat("dispatch_async(_fileIOQueue, ^{\n" 10986 " NSString *path = [self sessionFilePath];\n" 10987 " if (path) {\n" 10988 " // ...\n" 10989 " }\n" 10990 "});"); 10991 verifyFormat("[[SessionService sharedService]\n" 10992 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 10993 " if (window) {\n" 10994 " [self windowDidLoad:window];\n" 10995 " } else {\n" 10996 " [self errorLoadingWindow];\n" 10997 " }\n" 10998 " }];"); 10999 verifyFormat("void (^largeBlock)(void) = ^{\n" 11000 " // ...\n" 11001 "};\n", 11002 getLLVMStyleWithColumns(40)); 11003 verifyFormat("[[SessionService sharedService]\n" 11004 " loadWindowWithCompletionBlock: //\n" 11005 " ^(SessionWindow *window) {\n" 11006 " if (window) {\n" 11007 " [self windowDidLoad:window];\n" 11008 " } else {\n" 11009 " [self errorLoadingWindow];\n" 11010 " }\n" 11011 " }];", 11012 getLLVMStyleWithColumns(60)); 11013 verifyFormat("[myObject doSomethingWith:arg1\n" 11014 " firstBlock:^(Foo *a) {\n" 11015 " // ...\n" 11016 " int i;\n" 11017 " }\n" 11018 " secondBlock:^(Bar *b) {\n" 11019 " // ...\n" 11020 " int i;\n" 11021 " }\n" 11022 " thirdBlock:^Foo(Bar *b) {\n" 11023 " // ...\n" 11024 " int i;\n" 11025 " }];"); 11026 verifyFormat("[myObject doSomethingWith:arg1\n" 11027 " firstBlock:-1\n" 11028 " secondBlock:^(Bar *b) {\n" 11029 " // ...\n" 11030 " int i;\n" 11031 " }];"); 11032 11033 verifyFormat("f(^{\n" 11034 " @autoreleasepool {\n" 11035 " if (a) {\n" 11036 " g();\n" 11037 " }\n" 11038 " }\n" 11039 "});"); 11040 verifyFormat("Block b = ^int *(A *a, B *b) {}"); 11041 verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n" 11042 "};"); 11043 11044 FormatStyle FourIndent = getLLVMStyle(); 11045 FourIndent.ObjCBlockIndentWidth = 4; 11046 verifyFormat("[operation setCompletionBlock:^{\n" 11047 " [self onOperationDone];\n" 11048 "}];", 11049 FourIndent); 11050 } 11051 11052 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) { 11053 FormatStyle ZeroColumn = getLLVMStyle(); 11054 ZeroColumn.ColumnLimit = 0; 11055 11056 verifyFormat("[[SessionService sharedService] " 11057 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 11058 " if (window) {\n" 11059 " [self windowDidLoad:window];\n" 11060 " } else {\n" 11061 " [self errorLoadingWindow];\n" 11062 " }\n" 11063 "}];", 11064 ZeroColumn); 11065 EXPECT_EQ("[[SessionService sharedService]\n" 11066 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 11067 " if (window) {\n" 11068 " [self windowDidLoad:window];\n" 11069 " } else {\n" 11070 " [self errorLoadingWindow];\n" 11071 " }\n" 11072 " }];", 11073 format("[[SessionService sharedService]\n" 11074 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 11075 " if (window) {\n" 11076 " [self windowDidLoad:window];\n" 11077 " } else {\n" 11078 " [self errorLoadingWindow];\n" 11079 " }\n" 11080 "}];", 11081 ZeroColumn)); 11082 verifyFormat("[myObject doSomethingWith:arg1\n" 11083 " firstBlock:^(Foo *a) {\n" 11084 " // ...\n" 11085 " int i;\n" 11086 " }\n" 11087 " secondBlock:^(Bar *b) {\n" 11088 " // ...\n" 11089 " int i;\n" 11090 " }\n" 11091 " thirdBlock:^Foo(Bar *b) {\n" 11092 " // ...\n" 11093 " int i;\n" 11094 " }];", 11095 ZeroColumn); 11096 verifyFormat("f(^{\n" 11097 " @autoreleasepool {\n" 11098 " if (a) {\n" 11099 " g();\n" 11100 " }\n" 11101 " }\n" 11102 "});", 11103 ZeroColumn); 11104 verifyFormat("void (^largeBlock)(void) = ^{\n" 11105 " // ...\n" 11106 "};", 11107 ZeroColumn); 11108 11109 ZeroColumn.AllowShortBlocksOnASingleLine = true; 11110 EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };", 11111 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 11112 ZeroColumn.AllowShortBlocksOnASingleLine = false; 11113 EXPECT_EQ("void (^largeBlock)(void) = ^{\n" 11114 " int i;\n" 11115 "};", 11116 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 11117 } 11118 11119 TEST_F(FormatTest, SupportsCRLF) { 11120 EXPECT_EQ("int a;\r\n" 11121 "int b;\r\n" 11122 "int c;\r\n", 11123 format("int a;\r\n" 11124 " int b;\r\n" 11125 " int c;\r\n", 11126 getLLVMStyle())); 11127 EXPECT_EQ("int a;\r\n" 11128 "int b;\r\n" 11129 "int c;\r\n", 11130 format("int a;\r\n" 11131 " int b;\n" 11132 " int c;\r\n", 11133 getLLVMStyle())); 11134 EXPECT_EQ("int a;\n" 11135 "int b;\n" 11136 "int c;\n", 11137 format("int a;\r\n" 11138 " int b;\n" 11139 " int c;\n", 11140 getLLVMStyle())); 11141 EXPECT_EQ("\"aaaaaaa \"\r\n" 11142 "\"bbbbbbb\";\r\n", 11143 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10))); 11144 EXPECT_EQ("#define A \\\r\n" 11145 " b; \\\r\n" 11146 " c; \\\r\n" 11147 " d;\r\n", 11148 format("#define A \\\r\n" 11149 " b; \\\r\n" 11150 " c; d; \r\n", 11151 getGoogleStyle())); 11152 11153 EXPECT_EQ("/*\r\n" 11154 "multi line block comments\r\n" 11155 "should not introduce\r\n" 11156 "an extra carriage return\r\n" 11157 "*/\r\n", 11158 format("/*\r\n" 11159 "multi line block comments\r\n" 11160 "should not introduce\r\n" 11161 "an extra carriage return\r\n" 11162 "*/\r\n")); 11163 } 11164 11165 TEST_F(FormatTest, MunchSemicolonAfterBlocks) { 11166 verifyFormat("MY_CLASS(C) {\n" 11167 " int i;\n" 11168 " int j;\n" 11169 "};"); 11170 } 11171 11172 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) { 11173 FormatStyle TwoIndent = getLLVMStyleWithColumns(15); 11174 TwoIndent.ContinuationIndentWidth = 2; 11175 11176 EXPECT_EQ("int i =\n" 11177 " longFunction(\n" 11178 " arg);", 11179 format("int i = longFunction(arg);", TwoIndent)); 11180 11181 FormatStyle SixIndent = getLLVMStyleWithColumns(20); 11182 SixIndent.ContinuationIndentWidth = 6; 11183 11184 EXPECT_EQ("int i =\n" 11185 " longFunction(\n" 11186 " arg);", 11187 format("int i = longFunction(arg);", SixIndent)); 11188 } 11189 11190 TEST_F(FormatTest, SpacesInAngles) { 11191 FormatStyle Spaces = getLLVMStyle(); 11192 Spaces.SpacesInAngles = true; 11193 11194 verifyFormat("static_cast< int >(arg);", Spaces); 11195 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces); 11196 verifyFormat("f< int, float >();", Spaces); 11197 verifyFormat("template <> g() {}", Spaces); 11198 verifyFormat("template < std::vector< int > > f() {}", Spaces); 11199 verifyFormat("std::function< void(int, int) > fct;", Spaces); 11200 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }", 11201 Spaces); 11202 11203 Spaces.Standard = FormatStyle::LS_Cpp03; 11204 Spaces.SpacesInAngles = true; 11205 verifyFormat("A< A< int > >();", Spaces); 11206 11207 Spaces.SpacesInAngles = false; 11208 verifyFormat("A<A<int> >();", Spaces); 11209 11210 Spaces.Standard = FormatStyle::LS_Cpp11; 11211 Spaces.SpacesInAngles = true; 11212 verifyFormat("A< A< int > >();", Spaces); 11213 11214 Spaces.SpacesInAngles = false; 11215 verifyFormat("A<A<int>>();", Spaces); 11216 } 11217 11218 TEST_F(FormatTest, SpaceAfterTemplateKeyword) { 11219 FormatStyle Style = getLLVMStyle(); 11220 Style.SpaceAfterTemplateKeyword = false; 11221 verifyFormat("template<int> void foo();", Style); 11222 } 11223 11224 TEST_F(FormatTest, TripleAngleBrackets) { 11225 verifyFormat("f<<<1, 1>>>();"); 11226 verifyFormat("f<<<1, 1, 1, s>>>();"); 11227 verifyFormat("f<<<a, b, c, d>>>();"); 11228 EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();")); 11229 verifyFormat("f<param><<<1, 1>>>();"); 11230 verifyFormat("f<1><<<1, 1>>>();"); 11231 EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();")); 11232 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 11233 "aaaaaaaaaaa<<<\n 1, 1>>>();"); 11234 verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n" 11235 " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();"); 11236 } 11237 11238 TEST_F(FormatTest, MergeLessLessAtEnd) { 11239 verifyFormat("<<"); 11240 EXPECT_EQ("< < <", format("\\\n<<<")); 11241 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 11242 "aaallvm::outs() <<"); 11243 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 11244 "aaaallvm::outs()\n <<"); 11245 } 11246 11247 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) { 11248 std::string code = "#if A\n" 11249 "#if B\n" 11250 "a.\n" 11251 "#endif\n" 11252 " a = 1;\n" 11253 "#else\n" 11254 "#endif\n" 11255 "#if C\n" 11256 "#else\n" 11257 "#endif\n"; 11258 EXPECT_EQ(code, format(code)); 11259 } 11260 11261 TEST_F(FormatTest, HandleConflictMarkers) { 11262 // Git/SVN conflict markers. 11263 EXPECT_EQ("int a;\n" 11264 "void f() {\n" 11265 " callme(some(parameter1,\n" 11266 "<<<<<<< text by the vcs\n" 11267 " parameter2),\n" 11268 "||||||| text by the vcs\n" 11269 " parameter2),\n" 11270 " parameter3,\n" 11271 "======= text by the vcs\n" 11272 " parameter2, parameter3),\n" 11273 ">>>>>>> text by the vcs\n" 11274 " otherparameter);\n", 11275 format("int a;\n" 11276 "void f() {\n" 11277 " callme(some(parameter1,\n" 11278 "<<<<<<< text by the vcs\n" 11279 " parameter2),\n" 11280 "||||||| text by the vcs\n" 11281 " parameter2),\n" 11282 " parameter3,\n" 11283 "======= text by the vcs\n" 11284 " parameter2,\n" 11285 " parameter3),\n" 11286 ">>>>>>> text by the vcs\n" 11287 " otherparameter);\n")); 11288 11289 // Perforce markers. 11290 EXPECT_EQ("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 format("void f() {\n" 11301 " function(\n" 11302 ">>>> text by the vcs\n" 11303 " parameter,\n" 11304 "==== text by the vcs\n" 11305 " parameter,\n" 11306 "==== text by the vcs\n" 11307 " parameter,\n" 11308 "<<<< text by the vcs\n" 11309 " parameter);\n")); 11310 11311 EXPECT_EQ("<<<<<<<\n" 11312 "|||||||\n" 11313 "=======\n" 11314 ">>>>>>>", 11315 format("<<<<<<<\n" 11316 "|||||||\n" 11317 "=======\n" 11318 ">>>>>>>")); 11319 11320 EXPECT_EQ("<<<<<<<\n" 11321 "|||||||\n" 11322 "int i;\n" 11323 "=======\n" 11324 ">>>>>>>", 11325 format("<<<<<<<\n" 11326 "|||||||\n" 11327 "int i;\n" 11328 "=======\n" 11329 ">>>>>>>")); 11330 11331 // FIXME: Handle parsing of macros around conflict markers correctly: 11332 EXPECT_EQ("#define Macro \\\n" 11333 "<<<<<<<\n" 11334 "Something \\\n" 11335 "|||||||\n" 11336 "Else \\\n" 11337 "=======\n" 11338 "Other \\\n" 11339 ">>>>>>>\n" 11340 " End int i;\n", 11341 format("#define Macro \\\n" 11342 "<<<<<<<\n" 11343 " Something \\\n" 11344 "|||||||\n" 11345 " Else \\\n" 11346 "=======\n" 11347 " Other \\\n" 11348 ">>>>>>>\n" 11349 " End\n" 11350 "int i;\n")); 11351 } 11352 11353 TEST_F(FormatTest, DisableRegions) { 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 EXPECT_EQ("int i;\n" 11365 "/* clang-format off */\n" 11366 " int j;\n" 11367 "/* clang-format on */\n" 11368 "int k;", 11369 format(" int i;\n" 11370 " /* clang-format off */\n" 11371 " int j;\n" 11372 " /* clang-format on */\n" 11373 " int k;")); 11374 11375 // Don't reflow comments within disabled regions. 11376 EXPECT_EQ( 11377 "// clang-format off\n" 11378 "// long long long long long long line\n" 11379 "/* clang-format on */\n" 11380 "/* long long long\n" 11381 " * long long long\n" 11382 " * line */\n" 11383 "int i;\n" 11384 "/* clang-format off */\n" 11385 "/* long long long long long long line */\n", 11386 format("// clang-format off\n" 11387 "// long long long long long long line\n" 11388 "/* clang-format on */\n" 11389 "/* long long long long long long line */\n" 11390 "int i;\n" 11391 "/* clang-format off */\n" 11392 "/* long long long long long long line */\n", 11393 getLLVMStyleWithColumns(20))); 11394 } 11395 11396 TEST_F(FormatTest, DoNotCrashOnInvalidInput) { 11397 format("? ) ="); 11398 verifyNoCrash("#define a\\\n /**/}"); 11399 } 11400 11401 TEST_F(FormatTest, FormatsTableGenCode) { 11402 FormatStyle Style = getLLVMStyle(); 11403 Style.Language = FormatStyle::LK_TableGen; 11404 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style); 11405 } 11406 11407 TEST_F(FormatTest, ArrayOfTemplates) { 11408 EXPECT_EQ("auto a = new unique_ptr<int>[10];", 11409 format("auto a = new unique_ptr<int > [ 10];")); 11410 11411 FormatStyle Spaces = getLLVMStyle(); 11412 Spaces.SpacesInSquareBrackets = true; 11413 EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];", 11414 format("auto a = new unique_ptr<int > [10];", Spaces)); 11415 } 11416 11417 TEST_F(FormatTest, ArrayAsTemplateType) { 11418 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;", 11419 format("auto a = unique_ptr < Foo < Bar>[ 10]> ;")); 11420 11421 FormatStyle Spaces = getLLVMStyle(); 11422 Spaces.SpacesInSquareBrackets = true; 11423 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;", 11424 format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces)); 11425 } 11426 11427 TEST_F(FormatTest, NoSpaceAfterSuper) { 11428 verifyFormat("__super::FooBar();"); 11429 } 11430 11431 TEST(FormatStyle, GetStyleOfFile) { 11432 vfs::InMemoryFileSystem FS; 11433 // Test 1: format file in the same directory. 11434 ASSERT_TRUE( 11435 FS.addFile("/a/.clang-format", 0, 11436 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM"))); 11437 ASSERT_TRUE( 11438 FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 11439 auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS); 11440 ASSERT_TRUE((bool)Style1); 11441 ASSERT_EQ(*Style1, getLLVMStyle()); 11442 11443 // Test 2.1: fallback to default. 11444 ASSERT_TRUE( 11445 FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 11446 auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS); 11447 ASSERT_TRUE((bool)Style2); 11448 ASSERT_EQ(*Style2, getMozillaStyle()); 11449 11450 // Test 2.2: no format on 'none' fallback style. 11451 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 11452 ASSERT_TRUE((bool)Style2); 11453 ASSERT_EQ(*Style2, getNoStyle()); 11454 11455 // Test 2.3: format if config is found with no based style while fallback is 11456 // 'none'. 11457 ASSERT_TRUE(FS.addFile("/b/.clang-format", 0, 11458 llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2"))); 11459 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 11460 ASSERT_TRUE((bool)Style2); 11461 ASSERT_EQ(*Style2, getLLVMStyle()); 11462 11463 // Test 2.4: format if yaml with no based style, while fallback is 'none'. 11464 Style2 = getStyle("{}", "a.h", "none", "", &FS); 11465 ASSERT_TRUE((bool)Style2); 11466 ASSERT_EQ(*Style2, getLLVMStyle()); 11467 11468 // Test 3: format file in parent directory. 11469 ASSERT_TRUE( 11470 FS.addFile("/c/.clang-format", 0, 11471 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 11472 ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0, 11473 llvm::MemoryBuffer::getMemBuffer("int i;"))); 11474 auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS); 11475 ASSERT_TRUE((bool)Style3); 11476 ASSERT_EQ(*Style3, getGoogleStyle()); 11477 11478 // Test 4: error on invalid fallback style 11479 auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS); 11480 ASSERT_FALSE((bool)Style4); 11481 llvm::consumeError(Style4.takeError()); 11482 11483 // Test 5: error on invalid yaml on command line 11484 auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS); 11485 ASSERT_FALSE((bool)Style5); 11486 llvm::consumeError(Style5.takeError()); 11487 11488 // Test 6: error on invalid style 11489 auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS); 11490 ASSERT_FALSE((bool)Style6); 11491 llvm::consumeError(Style6.takeError()); 11492 11493 // Test 7: found config file, error on parsing it 11494 ASSERT_TRUE( 11495 FS.addFile("/d/.clang-format", 0, 11496 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n" 11497 "InvalidKey: InvalidValue"))); 11498 ASSERT_TRUE( 11499 FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 11500 auto Style7 = getStyle("file", "/d/.clang-format", "LLVM", "", &FS); 11501 ASSERT_FALSE((bool)Style7); 11502 llvm::consumeError(Style7.takeError()); 11503 } 11504 11505 TEST_F(ReplacementTest, FormatCodeAfterReplacements) { 11506 // Column limit is 20. 11507 std::string Code = "Type *a =\n" 11508 " new Type();\n" 11509 "g(iiiii, 0, jjjjj,\n" 11510 " 0, kkkkk, 0, mm);\n" 11511 "int bad = format ;"; 11512 std::string Expected = "auto a = new Type();\n" 11513 "g(iiiii, nullptr,\n" 11514 " jjjjj, nullptr,\n" 11515 " kkkkk, nullptr,\n" 11516 " mm);\n" 11517 "int bad = format ;"; 11518 FileID ID = Context.createInMemoryFile("format.cpp", Code); 11519 tooling::Replacements Replaces = toReplacements( 11520 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6, 11521 "auto "), 11522 tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1, 11523 "nullptr"), 11524 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1, 11525 "nullptr"), 11526 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1, 11527 "nullptr")}); 11528 11529 format::FormatStyle Style = format::getLLVMStyle(); 11530 Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility. 11531 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 11532 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 11533 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 11534 auto Result = applyAllReplacements(Code, *FormattedReplaces); 11535 EXPECT_TRUE(static_cast<bool>(Result)); 11536 EXPECT_EQ(Expected, *Result); 11537 } 11538 11539 TEST_F(ReplacementTest, SortIncludesAfterReplacement) { 11540 std::string Code = "#include \"a.h\"\n" 11541 "#include \"c.h\"\n" 11542 "\n" 11543 "int main() {\n" 11544 " return 0;\n" 11545 "}"; 11546 std::string Expected = "#include \"a.h\"\n" 11547 "#include \"b.h\"\n" 11548 "#include \"c.h\"\n" 11549 "\n" 11550 "int main() {\n" 11551 " return 0;\n" 11552 "}"; 11553 FileID ID = Context.createInMemoryFile("fix.cpp", Code); 11554 tooling::Replacements Replaces = toReplacements( 11555 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0, 11556 "#include \"b.h\"\n")}); 11557 11558 format::FormatStyle Style = format::getLLVMStyle(); 11559 Style.SortIncludes = true; 11560 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 11561 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 11562 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 11563 auto Result = applyAllReplacements(Code, *FormattedReplaces); 11564 EXPECT_TRUE(static_cast<bool>(Result)); 11565 EXPECT_EQ(Expected, *Result); 11566 } 11567 11568 TEST_F(FormatTest, FormatSortsUsingDeclarations) { 11569 EXPECT_EQ("using std::cin;\n" 11570 "using std::cout;", 11571 format("using std::cout;\n" 11572 "using std::cin;", getGoogleStyle())); 11573 } 11574 11575 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) { 11576 format::FormatStyle Style = format::getLLVMStyle(); 11577 Style.Standard = FormatStyle::LS_Cpp03; 11578 // cpp03 recognize this string as identifier u8 and literal character 'a' 11579 EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style)); 11580 } 11581 11582 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) { 11583 // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers 11584 // all modes, including C++11, C++14 and C++17 11585 EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';")); 11586 } 11587 11588 TEST_F(FormatTest, DoNotFormatLikelyXml) { 11589 EXPECT_EQ("<!-- ;> -->", 11590 format("<!-- ;> -->", getGoogleStyle())); 11591 EXPECT_EQ(" <!-- >; -->", 11592 format(" <!-- >; -->", getGoogleStyle())); 11593 } 11594 11595 TEST_F(FormatTest, StructuredBindings) { 11596 // Structured bindings is a C++17 feature. 11597 // all modes, including C++11, C++14 and C++17 11598 verifyFormat("auto [a, b] = f();"); 11599 EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();")); 11600 EXPECT_EQ("const auto [a, b] = f();", format("const auto[a, b] = f();")); 11601 EXPECT_EQ("auto const [a, b] = f();", format("auto const[a, b] = f();")); 11602 EXPECT_EQ("auto const volatile [a, b] = f();", 11603 format("auto const volatile[a, b] = f();")); 11604 EXPECT_EQ("auto [a, b, c] = f();", format("auto [ a , b,c ] = f();")); 11605 EXPECT_EQ("auto &[a, b, c] = f();", 11606 format("auto &[ a , b,c ] = f();")); 11607 EXPECT_EQ("auto &&[a, b, c] = f();", 11608 format("auto &&[ a , b,c ] = f();")); 11609 EXPECT_EQ("auto const &[a, b] = f();", format("auto const&[a, b] = f();")); 11610 EXPECT_EQ("auto const volatile &&[a, b] = f();", 11611 format("auto const volatile &&[a, b] = f();")); 11612 EXPECT_EQ("auto const &&[a, b] = f();", format("auto const && [a, b] = f();")); 11613 EXPECT_EQ("const auto &[a, b] = f();", format("const auto & [a, b] = f();")); 11614 EXPECT_EQ("const auto volatile &&[a, b] = f();", 11615 format("const auto volatile &&[a, b] = f();")); 11616 EXPECT_EQ("volatile const auto &&[a, b] = f();", 11617 format("volatile const auto &&[a, b] = f();")); 11618 EXPECT_EQ("const auto &&[a, b] = f();", format("const auto && [a, b] = f();")); 11619 11620 // Make sure we don't mistake structured bindings for lambdas. 11621 FormatStyle PointerMiddle = getLLVMStyle(); 11622 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 11623 verifyFormat("auto [a1, b]{A * i};", getGoogleStyle()); 11624 verifyFormat("auto [a2, b]{A * i};", getLLVMStyle()); 11625 verifyFormat("auto [a3, b]{A * i};", PointerMiddle); 11626 verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle()); 11627 verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle()); 11628 verifyFormat("auto const [a3, b]{A * i};", PointerMiddle); 11629 verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle()); 11630 verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle()); 11631 verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle); 11632 verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle()); 11633 verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle()); 11634 verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle); 11635 11636 EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}", 11637 format("for (const auto && [a, b] : some_range) {\n}")); 11638 EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}", 11639 format("for (const auto & [a, b] : some_range) {\n}")); 11640 EXPECT_EQ("for (const auto [a, b] : some_range) {\n}", 11641 format("for (const auto[a, b] : some_range) {\n}")); 11642 EXPECT_EQ("auto [x, y](expr);", format("auto[x,y] (expr);")); 11643 EXPECT_EQ("auto &[x, y](expr);", format("auto & [x,y] (expr);")); 11644 EXPECT_EQ("auto &&[x, y](expr);", format("auto && [x,y] (expr);")); 11645 EXPECT_EQ("auto const &[x, y](expr);", format("auto const & [x,y] (expr);")); 11646 EXPECT_EQ("auto const &&[x, y](expr);", format("auto const && [x,y] (expr);")); 11647 EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y] {expr};")); 11648 EXPECT_EQ("auto const &[x, y]{expr};", format("auto const & [x,y] {expr};")); 11649 EXPECT_EQ("auto const &&[x, y]{expr};", format("auto const && [x,y] {expr};")); 11650 11651 format::FormatStyle Spaces = format::getLLVMStyle(); 11652 Spaces.SpacesInSquareBrackets = true; 11653 verifyFormat("auto [ a, b ] = f();", Spaces); 11654 verifyFormat("auto &&[ a, b ] = f();", Spaces); 11655 verifyFormat("auto &[ a, b ] = f();", Spaces); 11656 verifyFormat("auto const &&[ a, b ] = f();", Spaces); 11657 verifyFormat("auto const &[ a, b ] = f();", Spaces); 11658 } 11659 11660 } // end namespace 11661 } // end namespace format 11662 } // end namespace clang 11663