1 //===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "clang/Format/Format.h" 11 12 #include "../Tooling/ReplacementTest.h" 13 #include "FormatTestUtils.h" 14 15 #include "clang/Frontend/TextDiagnosticPrinter.h" 16 #include "llvm/Support/Debug.h" 17 #include "llvm/Support/MemoryBuffer.h" 18 #include "gtest/gtest.h" 19 20 #define DEBUG_TYPE "format-test" 21 22 using clang::tooling::ReplacementTest; 23 using clang::tooling::toReplacements; 24 25 namespace clang { 26 namespace format { 27 namespace { 28 29 FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); } 30 31 class FormatTest : public ::testing::Test { 32 protected: 33 enum StatusCheck { 34 SC_ExpectComplete, 35 SC_ExpectIncomplete, 36 SC_DoNotCheck 37 }; 38 39 std::string format(llvm::StringRef Code, 40 const FormatStyle &Style = getLLVMStyle(), 41 StatusCheck CheckComplete = SC_ExpectComplete) { 42 DEBUG(llvm::errs() << "---\n"); 43 DEBUG(llvm::errs() << Code << "\n\n"); 44 std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size())); 45 FormattingAttemptStatus Status; 46 tooling::Replacements Replaces = 47 reformat(Style, Code, Ranges, "<stdin>", &Status); 48 if (CheckComplete != SC_DoNotCheck) { 49 bool ExpectedCompleteFormat = CheckComplete == SC_ExpectComplete; 50 EXPECT_EQ(ExpectedCompleteFormat, Status.FormatComplete) 51 << Code << "\n\n"; 52 } 53 ReplacementCount = Replaces.size(); 54 auto Result = applyAllReplacements(Code, Replaces); 55 EXPECT_TRUE(static_cast<bool>(Result)); 56 DEBUG(llvm::errs() << "\n" << *Result << "\n\n"); 57 return *Result; 58 } 59 60 FormatStyle getStyleWithColumns(FormatStyle Style, unsigned ColumnLimit) { 61 Style.ColumnLimit = ColumnLimit; 62 return Style; 63 } 64 65 FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) { 66 return getStyleWithColumns(getLLVMStyle(), ColumnLimit); 67 } 68 69 FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) { 70 return getStyleWithColumns(getGoogleStyle(), ColumnLimit); 71 } 72 73 void verifyFormat(llvm::StringRef Expected, llvm::StringRef Code, 74 const FormatStyle &Style = getLLVMStyle()) { 75 EXPECT_EQ(Expected.str(), format(Code, Style)); 76 if (Style.Language == FormatStyle::LK_Cpp) { 77 // Objective-C++ is a superset of C++, so everything checked for C++ 78 // needs to be checked for Objective-C++ as well. 79 FormatStyle ObjCStyle = Style; 80 ObjCStyle.Language = FormatStyle::LK_ObjC; 81 EXPECT_EQ(Expected.str(), format(test::messUp(Code), ObjCStyle)); 82 } 83 } 84 85 void verifyFormat(llvm::StringRef Code, 86 const FormatStyle &Style = getLLVMStyle()) { 87 verifyFormat(Code, test::messUp(Code), Style); 88 } 89 90 void verifyIncompleteFormat(llvm::StringRef Code, 91 const FormatStyle &Style = getLLVMStyle()) { 92 EXPECT_EQ(Code.str(), 93 format(test::messUp(Code), Style, SC_ExpectIncomplete)); 94 } 95 96 void verifyGoogleFormat(llvm::StringRef Code) { 97 verifyFormat(Code, getGoogleStyle()); 98 } 99 100 void verifyIndependentOfContext(llvm::StringRef text) { 101 verifyFormat(text); 102 verifyFormat(llvm::Twine("void f() { " + text + " }").str()); 103 } 104 105 /// \brief Verify that clang-format does not crash on the given input. 106 void verifyNoCrash(llvm::StringRef Code, 107 const FormatStyle &Style = getLLVMStyle()) { 108 format(Code, Style, SC_DoNotCheck); 109 } 110 111 int ReplacementCount; 112 }; 113 114 TEST_F(FormatTest, MessUp) { 115 EXPECT_EQ("1 2 3", test::messUp("1 2 3")); 116 EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n")); 117 EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc")); 118 EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc")); 119 EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne")); 120 } 121 122 //===----------------------------------------------------------------------===// 123 // Basic function tests. 124 //===----------------------------------------------------------------------===// 125 126 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) { 127 EXPECT_EQ(";", format(";")); 128 } 129 130 TEST_F(FormatTest, FormatsGlobalStatementsAt0) { 131 EXPECT_EQ("int i;", format(" int i;")); 132 EXPECT_EQ("\nint i;", format(" \n\t \v \f int i;")); 133 EXPECT_EQ("int i;\nint j;", format(" int i; int j;")); 134 EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;")); 135 } 136 137 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) { 138 EXPECT_EQ("int i;", format("int\ni;")); 139 } 140 141 TEST_F(FormatTest, FormatsNestedBlockStatements) { 142 EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}")); 143 } 144 145 TEST_F(FormatTest, FormatsNestedCall) { 146 verifyFormat("Method(f1, f2(f3));"); 147 verifyFormat("Method(f1(f2, f3()));"); 148 verifyFormat("Method(f1(f2, (f3())));"); 149 } 150 151 TEST_F(FormatTest, NestedNameSpecifiers) { 152 verifyFormat("vector<::Type> v;"); 153 verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())"); 154 verifyFormat("static constexpr bool Bar = decltype(bar())::value;"); 155 verifyFormat("bool a = 2 < ::SomeFunction();"); 156 verifyFormat("ALWAYS_INLINE ::std::string getName();"); 157 verifyFormat("some::string getName();"); 158 } 159 160 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) { 161 EXPECT_EQ("if (a) {\n" 162 " f();\n" 163 "}", 164 format("if(a){f();}")); 165 EXPECT_EQ(4, ReplacementCount); 166 EXPECT_EQ("if (a) {\n" 167 " f();\n" 168 "}", 169 format("if (a) {\n" 170 " f();\n" 171 "}")); 172 EXPECT_EQ(0, ReplacementCount); 173 EXPECT_EQ("/*\r\n" 174 "\r\n" 175 "*/\r\n", 176 format("/*\r\n" 177 "\r\n" 178 "*/\r\n")); 179 EXPECT_EQ(0, ReplacementCount); 180 } 181 182 TEST_F(FormatTest, RemovesEmptyLines) { 183 EXPECT_EQ("class C {\n" 184 " int i;\n" 185 "};", 186 format("class C {\n" 187 " int i;\n" 188 "\n" 189 "};")); 190 191 // Don't remove empty lines at the start of namespaces or extern "C" blocks. 192 EXPECT_EQ("namespace N {\n" 193 "\n" 194 "int i;\n" 195 "}", 196 format("namespace N {\n" 197 "\n" 198 "int i;\n" 199 "}", 200 getGoogleStyle())); 201 EXPECT_EQ("extern /**/ \"C\" /**/ {\n" 202 "\n" 203 "int i;\n" 204 "}", 205 format("extern /**/ \"C\" /**/ {\n" 206 "\n" 207 "int i;\n" 208 "}", 209 getGoogleStyle())); 210 211 // ...but do keep inlining and removing empty lines for non-block extern "C" 212 // functions. 213 verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle()); 214 EXPECT_EQ("extern \"C\" int f() {\n" 215 " int i = 42;\n" 216 " return i;\n" 217 "}", 218 format("extern \"C\" int f() {\n" 219 "\n" 220 " int i = 42;\n" 221 " return i;\n" 222 "}", 223 getGoogleStyle())); 224 225 // Remove empty lines at the beginning and end of blocks. 226 EXPECT_EQ("void f() {\n" 227 "\n" 228 " if (a) {\n" 229 "\n" 230 " f();\n" 231 " }\n" 232 "}", 233 format("void f() {\n" 234 "\n" 235 " if (a) {\n" 236 "\n" 237 " f();\n" 238 "\n" 239 " }\n" 240 "\n" 241 "}", 242 getLLVMStyle())); 243 EXPECT_EQ("void f() {\n" 244 " if (a) {\n" 245 " f();\n" 246 " }\n" 247 "}", 248 format("void f() {\n" 249 "\n" 250 " if (a) {\n" 251 "\n" 252 " f();\n" 253 "\n" 254 " }\n" 255 "\n" 256 "}", 257 getGoogleStyle())); 258 259 // Don't remove empty lines in more complex control statements. 260 EXPECT_EQ("void f() {\n" 261 " if (a) {\n" 262 " f();\n" 263 "\n" 264 " } else if (b) {\n" 265 " f();\n" 266 " }\n" 267 "}", 268 format("void f() {\n" 269 " if (a) {\n" 270 " f();\n" 271 "\n" 272 " } else if (b) {\n" 273 " f();\n" 274 "\n" 275 " }\n" 276 "\n" 277 "}")); 278 279 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 280 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 281 EXPECT_EQ("namespace {\n" 282 "int i;\n" 283 "}", 284 format("namespace {\n" 285 "int i;\n" 286 "\n" 287 "}", LLVMWithNoNamespaceFix)); 288 EXPECT_EQ("namespace {\n" 289 "int i;\n" 290 "}", 291 format("namespace {\n" 292 "int i;\n" 293 "\n" 294 "}")); 295 EXPECT_EQ("namespace {\n" 296 "int i;\n" 297 "};", 298 format("namespace {\n" 299 "int i;\n" 300 "\n" 301 "};")); 302 EXPECT_EQ("namespace {\n" 303 "int i;\n" 304 "} // namespace", 305 format("namespace {\n" 306 "int i;\n" 307 "\n" 308 "} // namespace")); 309 EXPECT_EQ("namespace {\n" 310 "int i;\n" 311 "}; // namespace", 312 format("namespace {\n" 313 "int i;\n" 314 "\n" 315 "}; // namespace")); 316 317 FormatStyle Style = getLLVMStyle(); 318 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 319 Style.MaxEmptyLinesToKeep = 2; 320 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 321 Style.BraceWrapping.AfterClass = true; 322 Style.BraceWrapping.AfterFunction = true; 323 Style.KeepEmptyLinesAtTheStartOfBlocks = false; 324 325 EXPECT_EQ("class Foo\n" 326 "{\n" 327 " Foo() {}\n" 328 "\n" 329 " void funk() {}\n" 330 "};", 331 format("class Foo\n" 332 "{\n" 333 " Foo()\n" 334 " {\n" 335 " }\n" 336 "\n" 337 " void funk() {}\n" 338 "};", 339 Style)); 340 } 341 342 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) { 343 verifyFormat("x = (a) and (b);"); 344 verifyFormat("x = (a) or (b);"); 345 verifyFormat("x = (a) bitand (b);"); 346 verifyFormat("x = (a) bitor (b);"); 347 verifyFormat("x = (a) not_eq (b);"); 348 verifyFormat("x = (a) and_eq (b);"); 349 verifyFormat("x = (a) or_eq (b);"); 350 verifyFormat("x = (a) xor (b);"); 351 } 352 353 TEST_F(FormatTest, RecognizesUnaryOperatorKeywords) { 354 verifyFormat("x = compl(a);"); 355 verifyFormat("x = not(a);"); 356 verifyFormat("x = bitand(a);"); 357 // Unary operator must not be merged with the next identifier 358 verifyFormat("x = compl a;"); 359 verifyFormat("x = not a;"); 360 verifyFormat("x = bitand a;"); 361 } 362 363 //===----------------------------------------------------------------------===// 364 // Tests for control statements. 365 //===----------------------------------------------------------------------===// 366 367 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) { 368 verifyFormat("if (true)\n f();\ng();"); 369 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); 370 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); 371 verifyFormat("if constexpr (true)\n" 372 " f();\ng();"); 373 verifyFormat("if constexpr (a)\n" 374 " if constexpr (b)\n" 375 " if constexpr (c)\n" 376 " g();\n" 377 "h();"); 378 verifyFormat("if constexpr (a)\n" 379 " if constexpr (b) {\n" 380 " f();\n" 381 " }\n" 382 "g();"); 383 384 FormatStyle AllowsMergedIf = getLLVMStyle(); 385 AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left; 386 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 387 verifyFormat("if (a)\n" 388 " // comment\n" 389 " f();", 390 AllowsMergedIf); 391 verifyFormat("{\n" 392 " if (a)\n" 393 " label:\n" 394 " f();\n" 395 "}", 396 AllowsMergedIf); 397 verifyFormat("#define A \\\n" 398 " if (a) \\\n" 399 " label: \\\n" 400 " f()", 401 AllowsMergedIf); 402 verifyFormat("if (a)\n" 403 " ;", 404 AllowsMergedIf); 405 verifyFormat("if (a)\n" 406 " if (b) return;", 407 AllowsMergedIf); 408 409 verifyFormat("if (a) // Can't merge this\n" 410 " f();\n", 411 AllowsMergedIf); 412 verifyFormat("if (a) /* still don't merge */\n" 413 " f();", 414 AllowsMergedIf); 415 verifyFormat("if (a) { // Never merge this\n" 416 " f();\n" 417 "}", 418 AllowsMergedIf); 419 verifyFormat("if (a) { /* Never merge this */\n" 420 " f();\n" 421 "}", 422 AllowsMergedIf); 423 424 AllowsMergedIf.ColumnLimit = 14; 425 verifyFormat("if (a) return;", AllowsMergedIf); 426 verifyFormat("if (aaaaaaaaa)\n" 427 " return;", 428 AllowsMergedIf); 429 430 AllowsMergedIf.ColumnLimit = 13; 431 verifyFormat("if (a)\n return;", AllowsMergedIf); 432 } 433 434 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) { 435 FormatStyle AllowsMergedLoops = getLLVMStyle(); 436 AllowsMergedLoops.AllowShortLoopsOnASingleLine = true; 437 verifyFormat("while (true) continue;", AllowsMergedLoops); 438 verifyFormat("for (;;) continue;", AllowsMergedLoops); 439 verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops); 440 verifyFormat("while (true)\n" 441 " ;", 442 AllowsMergedLoops); 443 verifyFormat("for (;;)\n" 444 " ;", 445 AllowsMergedLoops); 446 verifyFormat("for (;;)\n" 447 " for (;;) continue;", 448 AllowsMergedLoops); 449 verifyFormat("for (;;) // Can't merge this\n" 450 " continue;", 451 AllowsMergedLoops); 452 verifyFormat("for (;;) /* still don't merge */\n" 453 " continue;", 454 AllowsMergedLoops); 455 } 456 457 TEST_F(FormatTest, FormatShortBracedStatements) { 458 FormatStyle AllowSimpleBracedStatements = getLLVMStyle(); 459 AllowSimpleBracedStatements.ColumnLimit = 40; 460 AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true; 461 462 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true; 463 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 464 465 AllowSimpleBracedStatements.BreakBeforeBraces = FormatStyle::BS_Custom; 466 AllowSimpleBracedStatements.BraceWrapping.AfterFunction = true; 467 AllowSimpleBracedStatements.BraceWrapping.SplitEmptyRecord = false; 468 469 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 470 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements); 471 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 472 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 473 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 474 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements); 475 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 476 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 477 verifyFormat("if (true) {\n" 478 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 479 "}", 480 AllowSimpleBracedStatements); 481 verifyFormat("if (true) { //\n" 482 " f();\n" 483 "}", 484 AllowSimpleBracedStatements); 485 verifyFormat("if (true) {\n" 486 " f();\n" 487 " f();\n" 488 "}", 489 AllowSimpleBracedStatements); 490 verifyFormat("if (true) {\n" 491 " f();\n" 492 "} else {\n" 493 " f();\n" 494 "}", 495 AllowSimpleBracedStatements); 496 497 verifyFormat("struct A2 {\n" 498 " int X;\n" 499 "};", 500 AllowSimpleBracedStatements); 501 verifyFormat("typedef struct A2 {\n" 502 " int X;\n" 503 "} A2_t;", 504 AllowSimpleBracedStatements); 505 verifyFormat("template <int> struct A2 {\n" 506 " struct B {};\n" 507 "};", 508 AllowSimpleBracedStatements); 509 510 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false; 511 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 512 verifyFormat("if (true) {\n" 513 " f();\n" 514 "}", 515 AllowSimpleBracedStatements); 516 verifyFormat("if (true) {\n" 517 " f();\n" 518 "} else {\n" 519 " f();\n" 520 "}", 521 AllowSimpleBracedStatements); 522 523 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 524 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 525 verifyFormat("while (true) {\n" 526 " f();\n" 527 "}", 528 AllowSimpleBracedStatements); 529 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 530 verifyFormat("for (;;) {\n" 531 " f();\n" 532 "}", 533 AllowSimpleBracedStatements); 534 535 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true; 536 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 537 AllowSimpleBracedStatements.BraceWrapping.AfterControlStatement = true; 538 539 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 540 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements); 541 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 542 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 543 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 544 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements); 545 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 546 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 547 verifyFormat("if (true)\n" 548 "{\n" 549 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 550 "}", 551 AllowSimpleBracedStatements); 552 verifyFormat("if (true)\n" 553 "{ //\n" 554 " f();\n" 555 "}", 556 AllowSimpleBracedStatements); 557 verifyFormat("if (true)\n" 558 "{\n" 559 " f();\n" 560 " f();\n" 561 "}", 562 AllowSimpleBracedStatements); 563 verifyFormat("if (true)\n" 564 "{\n" 565 " f();\n" 566 "} else\n" 567 "{\n" 568 " f();\n" 569 "}", 570 AllowSimpleBracedStatements); 571 572 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false; 573 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 574 verifyFormat("if (true)\n" 575 "{\n" 576 " f();\n" 577 "}", 578 AllowSimpleBracedStatements); 579 verifyFormat("if (true)\n" 580 "{\n" 581 " f();\n" 582 "} else\n" 583 "{\n" 584 " f();\n" 585 "}", 586 AllowSimpleBracedStatements); 587 588 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 589 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 590 verifyFormat("while (true)\n" 591 "{\n" 592 " f();\n" 593 "}", 594 AllowSimpleBracedStatements); 595 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 596 verifyFormat("for (;;)\n" 597 "{\n" 598 " f();\n" 599 "}", 600 AllowSimpleBracedStatements); 601 } 602 603 TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) { 604 FormatStyle Style = getLLVMStyleWithColumns(60); 605 Style.AllowShortBlocksOnASingleLine = true; 606 Style.AllowShortIfStatementsOnASingleLine = true; 607 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 608 EXPECT_EQ("#define A \\\n" 609 " if (HANDLEwernufrnuLwrmviferuvnierv) \\\n" 610 " { RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; }\n" 611 "X;", 612 format("#define A \\\n" 613 " if (HANDLEwernufrnuLwrmviferuvnierv) { \\\n" 614 " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n" 615 " }\n" 616 "X;", 617 Style)); 618 } 619 620 TEST_F(FormatTest, ParseIfElse) { 621 verifyFormat("if (true)\n" 622 " if (true)\n" 623 " if (true)\n" 624 " f();\n" 625 " else\n" 626 " g();\n" 627 " else\n" 628 " h();\n" 629 "else\n" 630 " i();"); 631 verifyFormat("if (true)\n" 632 " if (true)\n" 633 " if (true) {\n" 634 " if (true)\n" 635 " f();\n" 636 " } else {\n" 637 " g();\n" 638 " }\n" 639 " else\n" 640 " h();\n" 641 "else {\n" 642 " i();\n" 643 "}"); 644 verifyFormat("if (true)\n" 645 " if constexpr (true)\n" 646 " if (true) {\n" 647 " if constexpr (true)\n" 648 " f();\n" 649 " } else {\n" 650 " g();\n" 651 " }\n" 652 " else\n" 653 " h();\n" 654 "else {\n" 655 " i();\n" 656 "}"); 657 verifyFormat("void f() {\n" 658 " if (a) {\n" 659 " } else {\n" 660 " }\n" 661 "}"); 662 } 663 664 TEST_F(FormatTest, ElseIf) { 665 verifyFormat("if (a) {\n} else if (b) {\n}"); 666 verifyFormat("if (a)\n" 667 " f();\n" 668 "else if (b)\n" 669 " g();\n" 670 "else\n" 671 " h();"); 672 verifyFormat("if constexpr (a)\n" 673 " f();\n" 674 "else if constexpr (b)\n" 675 " g();\n" 676 "else\n" 677 " h();"); 678 verifyFormat("if (a) {\n" 679 " f();\n" 680 "}\n" 681 "// or else ..\n" 682 "else {\n" 683 " g()\n" 684 "}"); 685 686 verifyFormat("if (a) {\n" 687 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 688 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 689 "}"); 690 verifyFormat("if (a) {\n" 691 "} else if (\n" 692 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 693 "}", 694 getLLVMStyleWithColumns(62)); 695 verifyFormat("if (a) {\n" 696 "} else if constexpr (\n" 697 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 698 "}", 699 getLLVMStyleWithColumns(62)); 700 } 701 702 TEST_F(FormatTest, FormatsForLoop) { 703 verifyFormat( 704 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" 705 " ++VeryVeryLongLoopVariable)\n" 706 " ;"); 707 verifyFormat("for (;;)\n" 708 " f();"); 709 verifyFormat("for (;;) {\n}"); 710 verifyFormat("for (;;) {\n" 711 " f();\n" 712 "}"); 713 verifyFormat("for (int i = 0; (i < 10); ++i) {\n}"); 714 715 verifyFormat( 716 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 717 " E = UnwrappedLines.end();\n" 718 " I != E; ++I) {\n}"); 719 720 verifyFormat( 721 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" 722 " ++IIIII) {\n}"); 723 verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n" 724 " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n" 725 " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}"); 726 verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n" 727 " I = FD->getDeclsInPrototypeScope().begin(),\n" 728 " E = FD->getDeclsInPrototypeScope().end();\n" 729 " I != E; ++I) {\n}"); 730 verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n" 731 " I = Container.begin(),\n" 732 " E = Container.end();\n" 733 " I != E; ++I) {\n}", 734 getLLVMStyleWithColumns(76)); 735 736 verifyFormat( 737 "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 738 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n" 739 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 740 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 741 " ++aaaaaaaaaaa) {\n}"); 742 verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 743 " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n" 744 " ++i) {\n}"); 745 verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n" 746 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 747 "}"); 748 verifyFormat("for (some_namespace::SomeIterator iter( // force break\n" 749 " aaaaaaaaaa);\n" 750 " iter; ++iter) {\n" 751 "}"); 752 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 753 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 754 " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n" 755 " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {"); 756 757 // These should not be formatted as Objective-C for-in loops. 758 verifyFormat("for (Foo *x = 0; x != in; x++) {\n}"); 759 verifyFormat("Foo *x;\nfor (x = 0; x != in; x++) {\n}"); 760 verifyFormat("Foo *x;\nfor (x in y) {\n}"); 761 verifyFormat("for (const Foo<Bar> &baz = in.value(); !baz.at_end(); ++baz) {\n}"); 762 763 FormatStyle NoBinPacking = getLLVMStyle(); 764 NoBinPacking.BinPackParameters = false; 765 verifyFormat("for (int aaaaaaaaaaa = 1;\n" 766 " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n" 767 " aaaaaaaaaaaaaaaa,\n" 768 " aaaaaaaaaaaaaaaa,\n" 769 " aaaaaaaaaaaaaaaa);\n" 770 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 771 "}", 772 NoBinPacking); 773 verifyFormat( 774 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 775 " E = UnwrappedLines.end();\n" 776 " I != E;\n" 777 " ++I) {\n}", 778 NoBinPacking); 779 780 FormatStyle AlignLeft = getLLVMStyle(); 781 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 782 verifyFormat("for (A* a = start; a < end; ++a, ++value) {\n}", AlignLeft); 783 } 784 785 TEST_F(FormatTest, RangeBasedForLoops) { 786 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 787 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 788 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n" 789 " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}"); 790 verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n" 791 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 792 verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n" 793 " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}"); 794 } 795 796 TEST_F(FormatTest, ForEachLoops) { 797 verifyFormat("void f() {\n" 798 " foreach (Item *item, itemlist) {}\n" 799 " Q_FOREACH (Item *item, itemlist) {}\n" 800 " BOOST_FOREACH (Item *item, itemlist) {}\n" 801 " UNKNOWN_FORACH(Item * item, itemlist) {}\n" 802 "}"); 803 804 // As function-like macros. 805 verifyFormat("#define foreach(x, y)\n" 806 "#define Q_FOREACH(x, y)\n" 807 "#define BOOST_FOREACH(x, y)\n" 808 "#define UNKNOWN_FOREACH(x, y)\n"); 809 810 // Not as function-like macros. 811 verifyFormat("#define foreach (x, y)\n" 812 "#define Q_FOREACH (x, y)\n" 813 "#define BOOST_FOREACH (x, y)\n" 814 "#define UNKNOWN_FOREACH (x, y)\n"); 815 } 816 817 TEST_F(FormatTest, FormatsWhileLoop) { 818 verifyFormat("while (true) {\n}"); 819 verifyFormat("while (true)\n" 820 " f();"); 821 verifyFormat("while () {\n}"); 822 verifyFormat("while () {\n" 823 " f();\n" 824 "}"); 825 } 826 827 TEST_F(FormatTest, FormatsDoWhile) { 828 verifyFormat("do {\n" 829 " do_something();\n" 830 "} while (something());"); 831 verifyFormat("do\n" 832 " do_something();\n" 833 "while (something());"); 834 } 835 836 TEST_F(FormatTest, FormatsSwitchStatement) { 837 verifyFormat("switch (x) {\n" 838 "case 1:\n" 839 " f();\n" 840 " break;\n" 841 "case kFoo:\n" 842 "case ns::kBar:\n" 843 "case kBaz:\n" 844 " break;\n" 845 "default:\n" 846 " g();\n" 847 " break;\n" 848 "}"); 849 verifyFormat("switch (x) {\n" 850 "case 1: {\n" 851 " f();\n" 852 " break;\n" 853 "}\n" 854 "case 2: {\n" 855 " break;\n" 856 "}\n" 857 "}"); 858 verifyFormat("switch (x) {\n" 859 "case 1: {\n" 860 " f();\n" 861 " {\n" 862 " g();\n" 863 " h();\n" 864 " }\n" 865 " break;\n" 866 "}\n" 867 "}"); 868 verifyFormat("switch (x) {\n" 869 "case 1: {\n" 870 " f();\n" 871 " if (foo) {\n" 872 " g();\n" 873 " h();\n" 874 " }\n" 875 " break;\n" 876 "}\n" 877 "}"); 878 verifyFormat("switch (x) {\n" 879 "case 1: {\n" 880 " f();\n" 881 " g();\n" 882 "} break;\n" 883 "}"); 884 verifyFormat("switch (test)\n" 885 " ;"); 886 verifyFormat("switch (x) {\n" 887 "default: {\n" 888 " // Do nothing.\n" 889 "}\n" 890 "}"); 891 verifyFormat("switch (x) {\n" 892 "// comment\n" 893 "// if 1, do f()\n" 894 "case 1:\n" 895 " f();\n" 896 "}"); 897 verifyFormat("switch (x) {\n" 898 "case 1:\n" 899 " // Do amazing stuff\n" 900 " {\n" 901 " f();\n" 902 " g();\n" 903 " }\n" 904 " break;\n" 905 "}"); 906 verifyFormat("#define A \\\n" 907 " switch (x) { \\\n" 908 " case a: \\\n" 909 " foo = b; \\\n" 910 " }", 911 getLLVMStyleWithColumns(20)); 912 verifyFormat("#define OPERATION_CASE(name) \\\n" 913 " case OP_name: \\\n" 914 " return operations::Operation##name\n", 915 getLLVMStyleWithColumns(40)); 916 verifyFormat("switch (x) {\n" 917 "case 1:;\n" 918 "default:;\n" 919 " int i;\n" 920 "}"); 921 922 verifyGoogleFormat("switch (x) {\n" 923 " case 1:\n" 924 " f();\n" 925 " break;\n" 926 " case kFoo:\n" 927 " case ns::kBar:\n" 928 " case kBaz:\n" 929 " break;\n" 930 " default:\n" 931 " g();\n" 932 " break;\n" 933 "}"); 934 verifyGoogleFormat("switch (x) {\n" 935 " case 1: {\n" 936 " f();\n" 937 " break;\n" 938 " }\n" 939 "}"); 940 verifyGoogleFormat("switch (test)\n" 941 " ;"); 942 943 verifyGoogleFormat("#define OPERATION_CASE(name) \\\n" 944 " case OP_name: \\\n" 945 " return operations::Operation##name\n"); 946 verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n" 947 " // Get the correction operation class.\n" 948 " switch (OpCode) {\n" 949 " CASE(Add);\n" 950 " CASE(Subtract);\n" 951 " default:\n" 952 " return operations::Unknown;\n" 953 " }\n" 954 "#undef OPERATION_CASE\n" 955 "}"); 956 verifyFormat("DEBUG({\n" 957 " switch (x) {\n" 958 " case A:\n" 959 " f();\n" 960 " break;\n" 961 " // fallthrough\n" 962 " case B:\n" 963 " g();\n" 964 " break;\n" 965 " }\n" 966 "});"); 967 EXPECT_EQ("DEBUG({\n" 968 " switch (x) {\n" 969 " case A:\n" 970 " f();\n" 971 " break;\n" 972 " // On B:\n" 973 " case B:\n" 974 " g();\n" 975 " break;\n" 976 " }\n" 977 "});", 978 format("DEBUG({\n" 979 " switch (x) {\n" 980 " case A:\n" 981 " f();\n" 982 " break;\n" 983 " // On B:\n" 984 " case B:\n" 985 " g();\n" 986 " break;\n" 987 " }\n" 988 "});", 989 getLLVMStyle())); 990 verifyFormat("switch (a) {\n" 991 "case (b):\n" 992 " return;\n" 993 "}"); 994 995 verifyFormat("switch (a) {\n" 996 "case some_namespace::\n" 997 " some_constant:\n" 998 " return;\n" 999 "}", 1000 getLLVMStyleWithColumns(34)); 1001 } 1002 1003 TEST_F(FormatTest, CaseRanges) { 1004 verifyFormat("switch (x) {\n" 1005 "case 'A' ... 'Z':\n" 1006 "case 1 ... 5:\n" 1007 "case a ... b:\n" 1008 " break;\n" 1009 "}"); 1010 } 1011 1012 TEST_F(FormatTest, ShortCaseLabels) { 1013 FormatStyle Style = getLLVMStyle(); 1014 Style.AllowShortCaseLabelsOnASingleLine = true; 1015 verifyFormat("switch (a) {\n" 1016 "case 1: x = 1; break;\n" 1017 "case 2: return;\n" 1018 "case 3:\n" 1019 "case 4:\n" 1020 "case 5: return;\n" 1021 "case 6: // comment\n" 1022 " return;\n" 1023 "case 7:\n" 1024 " // comment\n" 1025 " return;\n" 1026 "case 8:\n" 1027 " x = 8; // comment\n" 1028 " break;\n" 1029 "default: y = 1; break;\n" 1030 "}", 1031 Style); 1032 verifyFormat("switch (a) {\n" 1033 "case 0: return; // comment\n" 1034 "case 1: break; // comment\n" 1035 "case 2: return;\n" 1036 "// comment\n" 1037 "case 3: return;\n" 1038 "// comment 1\n" 1039 "// comment 2\n" 1040 "// comment 3\n" 1041 "case 4: break; /* comment */\n" 1042 "case 5:\n" 1043 " // comment\n" 1044 " break;\n" 1045 "case 6: /* comment */ x = 1; break;\n" 1046 "case 7: x = /* comment */ 1; break;\n" 1047 "case 8:\n" 1048 " x = 1; /* comment */\n" 1049 " break;\n" 1050 "case 9:\n" 1051 " break; // comment line 1\n" 1052 " // comment line 2\n" 1053 "}", 1054 Style); 1055 EXPECT_EQ("switch (a) {\n" 1056 "case 1:\n" 1057 " x = 8;\n" 1058 " // fall through\n" 1059 "case 2: x = 8;\n" 1060 "// comment\n" 1061 "case 3:\n" 1062 " return; /* comment line 1\n" 1063 " * comment line 2 */\n" 1064 "case 4: i = 8;\n" 1065 "// something else\n" 1066 "#if FOO\n" 1067 "case 5: break;\n" 1068 "#endif\n" 1069 "}", 1070 format("switch (a) {\n" 1071 "case 1: x = 8;\n" 1072 " // fall through\n" 1073 "case 2:\n" 1074 " x = 8;\n" 1075 "// comment\n" 1076 "case 3:\n" 1077 " return; /* comment line 1\n" 1078 " * comment line 2 */\n" 1079 "case 4:\n" 1080 " i = 8;\n" 1081 "// something else\n" 1082 "#if FOO\n" 1083 "case 5: break;\n" 1084 "#endif\n" 1085 "}", 1086 Style)); 1087 EXPECT_EQ("switch (a) {\n" "case 0:\n" 1088 " return; // long long long long long long long long long long long long comment\n" 1089 " // line\n" "}", 1090 format("switch (a) {\n" 1091 "case 0: return; // long long long long long long long long long long long long comment line\n" 1092 "}", 1093 Style)); 1094 EXPECT_EQ("switch (a) {\n" 1095 "case 0:\n" 1096 " return; /* long long long long long long long long long long long long comment\n" 1097 " line */\n" 1098 "}", 1099 format("switch (a) {\n" 1100 "case 0: return; /* long long long long long long long long long long long long comment line */\n" 1101 "}", 1102 Style)); 1103 verifyFormat("switch (a) {\n" 1104 "#if FOO\n" 1105 "case 0: return 0;\n" 1106 "#endif\n" 1107 "}", 1108 Style); 1109 verifyFormat("switch (a) {\n" 1110 "case 1: {\n" 1111 "}\n" 1112 "case 2: {\n" 1113 " return;\n" 1114 "}\n" 1115 "case 3: {\n" 1116 " x = 1;\n" 1117 " return;\n" 1118 "}\n" 1119 "case 4:\n" 1120 " if (x)\n" 1121 " return;\n" 1122 "}", 1123 Style); 1124 Style.ColumnLimit = 21; 1125 verifyFormat("switch (a) {\n" 1126 "case 1: x = 1; break;\n" 1127 "case 2: return;\n" 1128 "case 3:\n" 1129 "case 4:\n" 1130 "case 5: return;\n" 1131 "default:\n" 1132 " y = 1;\n" 1133 " break;\n" 1134 "}", 1135 Style); 1136 } 1137 1138 TEST_F(FormatTest, FormatsLabels) { 1139 verifyFormat("void f() {\n" 1140 " some_code();\n" 1141 "test_label:\n" 1142 " some_other_code();\n" 1143 " {\n" 1144 " some_more_code();\n" 1145 " another_label:\n" 1146 " some_more_code();\n" 1147 " }\n" 1148 "}"); 1149 verifyFormat("{\n" 1150 " some_code();\n" 1151 "test_label:\n" 1152 " some_other_code();\n" 1153 "}"); 1154 verifyFormat("{\n" 1155 " some_code();\n" 1156 "test_label:;\n" 1157 " int i = 0;\n" 1158 "}"); 1159 } 1160 1161 //===----------------------------------------------------------------------===// 1162 // Tests for classes, namespaces, etc. 1163 //===----------------------------------------------------------------------===// 1164 1165 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { 1166 verifyFormat("class A {};"); 1167 } 1168 1169 TEST_F(FormatTest, UnderstandsAccessSpecifiers) { 1170 verifyFormat("class A {\n" 1171 "public:\n" 1172 "public: // comment\n" 1173 "protected:\n" 1174 "private:\n" 1175 " void f() {}\n" 1176 "};"); 1177 verifyGoogleFormat("class A {\n" 1178 " public:\n" 1179 " protected:\n" 1180 " private:\n" 1181 " void f() {}\n" 1182 "};"); 1183 verifyFormat("class A {\n" 1184 "public slots:\n" 1185 " void f1() {}\n" 1186 "public Q_SLOTS:\n" 1187 " void f2() {}\n" 1188 "protected slots:\n" 1189 " void f3() {}\n" 1190 "protected Q_SLOTS:\n" 1191 " void f4() {}\n" 1192 "private slots:\n" 1193 " void f5() {}\n" 1194 "private Q_SLOTS:\n" 1195 " void f6() {}\n" 1196 "signals:\n" 1197 " void g1();\n" 1198 "Q_SIGNALS:\n" 1199 " void g2();\n" 1200 "};"); 1201 1202 // Don't interpret 'signals' the wrong way. 1203 verifyFormat("signals.set();"); 1204 verifyFormat("for (Signals signals : f()) {\n}"); 1205 verifyFormat("{\n" 1206 " signals.set(); // This needs indentation.\n" 1207 "}"); 1208 verifyFormat("void f() {\n" 1209 "label:\n" 1210 " signals.baz();\n" 1211 "}"); 1212 } 1213 1214 TEST_F(FormatTest, SeparatesLogicalBlocks) { 1215 EXPECT_EQ("class A {\n" 1216 "public:\n" 1217 " void f();\n" 1218 "\n" 1219 "private:\n" 1220 " void g() {}\n" 1221 " // test\n" 1222 "protected:\n" 1223 " int h;\n" 1224 "};", 1225 format("class A {\n" 1226 "public:\n" 1227 "void f();\n" 1228 "private:\n" 1229 "void g() {}\n" 1230 "// test\n" 1231 "protected:\n" 1232 "int h;\n" 1233 "};")); 1234 EXPECT_EQ("class A {\n" 1235 "protected:\n" 1236 "public:\n" 1237 " void f();\n" 1238 "};", 1239 format("class A {\n" 1240 "protected:\n" 1241 "\n" 1242 "public:\n" 1243 "\n" 1244 " void f();\n" 1245 "};")); 1246 1247 // Even ensure proper spacing inside macros. 1248 EXPECT_EQ("#define B \\\n" 1249 " class A { \\\n" 1250 " protected: \\\n" 1251 " public: \\\n" 1252 " void f(); \\\n" 1253 " };", 1254 format("#define B \\\n" 1255 " class A { \\\n" 1256 " protected: \\\n" 1257 " \\\n" 1258 " public: \\\n" 1259 " \\\n" 1260 " void f(); \\\n" 1261 " };", 1262 getGoogleStyle())); 1263 // But don't remove empty lines after macros ending in access specifiers. 1264 EXPECT_EQ("#define A private:\n" 1265 "\n" 1266 "int i;", 1267 format("#define A private:\n" 1268 "\n" 1269 "int i;")); 1270 } 1271 1272 TEST_F(FormatTest, FormatsClasses) { 1273 verifyFormat("class A : public B {};"); 1274 verifyFormat("class A : public ::B {};"); 1275 1276 verifyFormat( 1277 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 1278 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 1279 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 1280 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 1281 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 1282 verifyFormat( 1283 "class A : public B, public C, public D, public E, public F {};"); 1284 verifyFormat("class AAAAAAAAAAAA : public B,\n" 1285 " public C,\n" 1286 " public D,\n" 1287 " public E,\n" 1288 " public F,\n" 1289 " public G {};"); 1290 1291 verifyFormat("class\n" 1292 " ReallyReallyLongClassName {\n" 1293 " int i;\n" 1294 "};", 1295 getLLVMStyleWithColumns(32)); 1296 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 1297 " aaaaaaaaaaaaaaaa> {};"); 1298 verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n" 1299 " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n" 1300 " aaaaaaaaaaaaaaaaaaaaaa> {};"); 1301 verifyFormat("template <class R, class C>\n" 1302 "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n" 1303 " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};"); 1304 verifyFormat("class ::A::B {};"); 1305 } 1306 1307 TEST_F(FormatTest, BreakBeforeInheritanceComma) { 1308 FormatStyle StyleWithInheritanceBreak = getLLVMStyle(); 1309 StyleWithInheritanceBreak.BreakBeforeInheritanceComma = true; 1310 1311 verifyFormat("class MyClass : public X {};", StyleWithInheritanceBreak); 1312 verifyFormat("class MyClass\n" 1313 " : public X\n" 1314 " , public Y {};", 1315 StyleWithInheritanceBreak); 1316 } 1317 1318 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) { 1319 verifyFormat("class A {\n} a, b;"); 1320 verifyFormat("struct A {\n} a, b;"); 1321 verifyFormat("union A {\n} a;"); 1322 } 1323 1324 TEST_F(FormatTest, FormatsEnum) { 1325 verifyFormat("enum {\n" 1326 " Zero,\n" 1327 " One = 1,\n" 1328 " Two = One + 1,\n" 1329 " Three = (One + Two),\n" 1330 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1331 " Five = (One, Two, Three, Four, 5)\n" 1332 "};"); 1333 verifyGoogleFormat("enum {\n" 1334 " Zero,\n" 1335 " One = 1,\n" 1336 " Two = One + 1,\n" 1337 " Three = (One + Two),\n" 1338 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1339 " Five = (One, Two, Three, Four, 5)\n" 1340 "};"); 1341 verifyFormat("enum Enum {};"); 1342 verifyFormat("enum {};"); 1343 verifyFormat("enum X E {} d;"); 1344 verifyFormat("enum __attribute__((...)) E {} d;"); 1345 verifyFormat("enum __declspec__((...)) E {} d;"); 1346 verifyFormat("enum {\n" 1347 " Bar = Foo<int, int>::value\n" 1348 "};", 1349 getLLVMStyleWithColumns(30)); 1350 1351 verifyFormat("enum ShortEnum { A, B, C };"); 1352 verifyGoogleFormat("enum ShortEnum { A, B, C };"); 1353 1354 EXPECT_EQ("enum KeepEmptyLines {\n" 1355 " ONE,\n" 1356 "\n" 1357 " TWO,\n" 1358 "\n" 1359 " THREE\n" 1360 "}", 1361 format("enum KeepEmptyLines {\n" 1362 " ONE,\n" 1363 "\n" 1364 " TWO,\n" 1365 "\n" 1366 "\n" 1367 " THREE\n" 1368 "}")); 1369 verifyFormat("enum E { // comment\n" 1370 " ONE,\n" 1371 " TWO\n" 1372 "};\n" 1373 "int i;"); 1374 // Not enums. 1375 verifyFormat("enum X f() {\n" 1376 " a();\n" 1377 " return 42;\n" 1378 "}"); 1379 verifyFormat("enum X Type::f() {\n" 1380 " a();\n" 1381 " return 42;\n" 1382 "}"); 1383 verifyFormat("enum ::X f() {\n" 1384 " a();\n" 1385 " return 42;\n" 1386 "}"); 1387 verifyFormat("enum ns::X f() {\n" 1388 " a();\n" 1389 " return 42;\n" 1390 "}"); 1391 } 1392 1393 TEST_F(FormatTest, FormatsEnumsWithErrors) { 1394 verifyFormat("enum Type {\n" 1395 " One = 0; // These semicolons should be commas.\n" 1396 " Two = 1;\n" 1397 "};"); 1398 verifyFormat("namespace n {\n" 1399 "enum Type {\n" 1400 " One,\n" 1401 " Two, // missing };\n" 1402 " int i;\n" 1403 "}\n" 1404 "void g() {}"); 1405 } 1406 1407 TEST_F(FormatTest, FormatsEnumStruct) { 1408 verifyFormat("enum struct {\n" 1409 " Zero,\n" 1410 " One = 1,\n" 1411 " Two = One + 1,\n" 1412 " Three = (One + Two),\n" 1413 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1414 " Five = (One, Two, Three, Four, 5)\n" 1415 "};"); 1416 verifyFormat("enum struct Enum {};"); 1417 verifyFormat("enum struct {};"); 1418 verifyFormat("enum struct X E {} d;"); 1419 verifyFormat("enum struct __attribute__((...)) E {} d;"); 1420 verifyFormat("enum struct __declspec__((...)) E {} d;"); 1421 verifyFormat("enum struct X f() {\n a();\n return 42;\n}"); 1422 } 1423 1424 TEST_F(FormatTest, FormatsEnumClass) { 1425 verifyFormat("enum class {\n" 1426 " Zero,\n" 1427 " One = 1,\n" 1428 " Two = One + 1,\n" 1429 " Three = (One + Two),\n" 1430 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1431 " Five = (One, Two, Three, Four, 5)\n" 1432 "};"); 1433 verifyFormat("enum class Enum {};"); 1434 verifyFormat("enum class {};"); 1435 verifyFormat("enum class X E {} d;"); 1436 verifyFormat("enum class __attribute__((...)) E {} d;"); 1437 verifyFormat("enum class __declspec__((...)) E {} d;"); 1438 verifyFormat("enum class X f() {\n a();\n return 42;\n}"); 1439 } 1440 1441 TEST_F(FormatTest, FormatsEnumTypes) { 1442 verifyFormat("enum X : int {\n" 1443 " A, // Force multiple lines.\n" 1444 " B\n" 1445 "};"); 1446 verifyFormat("enum X : int { A, B };"); 1447 verifyFormat("enum X : std::uint32_t { A, B };"); 1448 } 1449 1450 TEST_F(FormatTest, FormatsTypedefEnum) { 1451 FormatStyle Style = getLLVMStyle(); 1452 Style.ColumnLimit = 40; 1453 verifyFormat("typedef enum {} EmptyEnum;"); 1454 verifyFormat("typedef enum { A, B, C } ShortEnum;"); 1455 verifyFormat("typedef enum {\n" 1456 " ZERO = 0,\n" 1457 " ONE = 1,\n" 1458 " TWO = 2,\n" 1459 " THREE = 3\n" 1460 "} LongEnum;", 1461 Style); 1462 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 1463 Style.BraceWrapping.AfterEnum = true; 1464 verifyFormat("typedef enum {} EmptyEnum;"); 1465 verifyFormat("typedef enum { A, B, C } ShortEnum;"); 1466 verifyFormat("typedef enum\n" 1467 "{\n" 1468 " ZERO = 0,\n" 1469 " ONE = 1,\n" 1470 " TWO = 2,\n" 1471 " THREE = 3\n" 1472 "} LongEnum;", 1473 Style); 1474 } 1475 1476 TEST_F(FormatTest, FormatsNSEnums) { 1477 verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }"); 1478 verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n" 1479 " // Information about someDecentlyLongValue.\n" 1480 " someDecentlyLongValue,\n" 1481 " // Information about anotherDecentlyLongValue.\n" 1482 " anotherDecentlyLongValue,\n" 1483 " // Information about aThirdDecentlyLongValue.\n" 1484 " aThirdDecentlyLongValue\n" 1485 "};"); 1486 verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n" 1487 " a = 1,\n" 1488 " b = 2,\n" 1489 " c = 3,\n" 1490 "};"); 1491 verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n" 1492 " a = 1,\n" 1493 " b = 2,\n" 1494 " c = 3,\n" 1495 "};"); 1496 verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n" 1497 " a = 1,\n" 1498 " b = 2,\n" 1499 " c = 3,\n" 1500 "};"); 1501 } 1502 1503 TEST_F(FormatTest, FormatsBitfields) { 1504 verifyFormat("struct Bitfields {\n" 1505 " unsigned sClass : 8;\n" 1506 " unsigned ValueKind : 2;\n" 1507 "};"); 1508 verifyFormat("struct A {\n" 1509 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n" 1510 " bbbbbbbbbbbbbbbbbbbbbbbbb;\n" 1511 "};"); 1512 verifyFormat("struct MyStruct {\n" 1513 " uchar data;\n" 1514 " uchar : 8;\n" 1515 " uchar : 8;\n" 1516 " uchar other;\n" 1517 "};"); 1518 } 1519 1520 TEST_F(FormatTest, FormatsNamespaces) { 1521 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 1522 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 1523 1524 verifyFormat("namespace some_namespace {\n" 1525 "class A {};\n" 1526 "void f() { f(); }\n" 1527 "}", 1528 LLVMWithNoNamespaceFix); 1529 verifyFormat("namespace {\n" 1530 "class A {};\n" 1531 "void f() { f(); }\n" 1532 "}", 1533 LLVMWithNoNamespaceFix); 1534 verifyFormat("inline namespace X {\n" 1535 "class A {};\n" 1536 "void f() { f(); }\n" 1537 "}", 1538 LLVMWithNoNamespaceFix); 1539 verifyFormat("using namespace some_namespace;\n" 1540 "class A {};\n" 1541 "void f() { f(); }", 1542 LLVMWithNoNamespaceFix); 1543 1544 // This code is more common than we thought; if we 1545 // layout this correctly the semicolon will go into 1546 // its own line, which is undesirable. 1547 verifyFormat("namespace {};", 1548 LLVMWithNoNamespaceFix); 1549 verifyFormat("namespace {\n" 1550 "class A {};\n" 1551 "};", 1552 LLVMWithNoNamespaceFix); 1553 1554 verifyFormat("namespace {\n" 1555 "int SomeVariable = 0; // comment\n" 1556 "} // namespace", 1557 LLVMWithNoNamespaceFix); 1558 EXPECT_EQ("#ifndef HEADER_GUARD\n" 1559 "#define HEADER_GUARD\n" 1560 "namespace my_namespace {\n" 1561 "int i;\n" 1562 "} // my_namespace\n" 1563 "#endif // HEADER_GUARD", 1564 format("#ifndef HEADER_GUARD\n" 1565 " #define HEADER_GUARD\n" 1566 " namespace my_namespace {\n" 1567 "int i;\n" 1568 "} // my_namespace\n" 1569 "#endif // HEADER_GUARD", 1570 LLVMWithNoNamespaceFix)); 1571 1572 EXPECT_EQ("namespace A::B {\n" 1573 "class C {};\n" 1574 "}", 1575 format("namespace A::B {\n" 1576 "class C {};\n" 1577 "}", 1578 LLVMWithNoNamespaceFix)); 1579 1580 FormatStyle Style = getLLVMStyle(); 1581 Style.NamespaceIndentation = FormatStyle::NI_All; 1582 EXPECT_EQ("namespace out {\n" 1583 " int i;\n" 1584 " namespace in {\n" 1585 " int i;\n" 1586 " } // namespace in\n" 1587 "} // namespace out", 1588 format("namespace out {\n" 1589 "int i;\n" 1590 "namespace in {\n" 1591 "int i;\n" 1592 "} // namespace in\n" 1593 "} // namespace out", 1594 Style)); 1595 1596 Style.NamespaceIndentation = FormatStyle::NI_Inner; 1597 EXPECT_EQ("namespace out {\n" 1598 "int i;\n" 1599 "namespace in {\n" 1600 " int i;\n" 1601 "} // namespace in\n" 1602 "} // namespace out", 1603 format("namespace out {\n" 1604 "int i;\n" 1605 "namespace in {\n" 1606 "int i;\n" 1607 "} // namespace in\n" 1608 "} // namespace out", 1609 Style)); 1610 } 1611 1612 TEST_F(FormatTest, FormatsCompactNamespaces) { 1613 FormatStyle Style = getLLVMStyle(); 1614 Style.CompactNamespaces = true; 1615 1616 verifyFormat("namespace A { namespace B {\n" 1617 "}} // namespace A::B", 1618 Style); 1619 1620 EXPECT_EQ("namespace out { namespace in {\n" 1621 "}} // namespace out::in", 1622 format("namespace out {\n" 1623 "namespace in {\n" 1624 "} // namespace in\n" 1625 "} // namespace out", 1626 Style)); 1627 1628 // Only namespaces which have both consecutive opening and end get compacted 1629 EXPECT_EQ("namespace out {\n" 1630 "namespace in1 {\n" 1631 "} // namespace in1\n" 1632 "namespace in2 {\n" 1633 "} // namespace in2\n" 1634 "} // namespace out", 1635 format("namespace out {\n" 1636 "namespace in1 {\n" 1637 "} // namespace in1\n" 1638 "namespace in2 {\n" 1639 "} // namespace in2\n" 1640 "} // namespace out", 1641 Style)); 1642 1643 EXPECT_EQ("namespace out {\n" 1644 "int i;\n" 1645 "namespace in {\n" 1646 "int j;\n" 1647 "} // namespace in\n" 1648 "int k;\n" 1649 "} // namespace out", 1650 format("namespace out { int i;\n" 1651 "namespace in { int j; } // namespace in\n" 1652 "int k; } // namespace out", 1653 Style)); 1654 1655 EXPECT_EQ("namespace A { namespace B { namespace C {\n" 1656 "}}} // namespace A::B::C\n", 1657 format("namespace A { namespace B {\n" 1658 "namespace C {\n" 1659 "}} // namespace B::C\n" 1660 "} // namespace A\n", 1661 Style)); 1662 1663 Style.ColumnLimit = 40; 1664 EXPECT_EQ("namespace aaaaaaaaaa {\n" 1665 "namespace bbbbbbbbbb {\n" 1666 "}} // namespace aaaaaaaaaa::bbbbbbbbbb", 1667 format("namespace aaaaaaaaaa {\n" 1668 "namespace bbbbbbbbbb {\n" 1669 "} // namespace bbbbbbbbbb\n" 1670 "} // namespace aaaaaaaaaa", 1671 Style)); 1672 1673 EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n" 1674 "namespace cccccc {\n" 1675 "}}} // namespace aaaaaa::bbbbbb::cccccc", 1676 format("namespace aaaaaa {\n" 1677 "namespace bbbbbb {\n" 1678 "namespace cccccc {\n" 1679 "} // namespace cccccc\n" 1680 "} // namespace bbbbbb\n" 1681 "} // namespace aaaaaa", 1682 Style)); 1683 Style.ColumnLimit = 80; 1684 1685 // Extra semicolon after 'inner' closing brace prevents merging 1686 EXPECT_EQ("namespace out { namespace in {\n" 1687 "}; } // namespace out::in", 1688 format("namespace out {\n" 1689 "namespace in {\n" 1690 "}; // namespace in\n" 1691 "} // namespace out", 1692 Style)); 1693 1694 // Extra semicolon after 'outer' closing brace is conserved 1695 EXPECT_EQ("namespace out { namespace in {\n" 1696 "}}; // namespace out::in", 1697 format("namespace out {\n" 1698 "namespace in {\n" 1699 "} // namespace in\n" 1700 "}; // namespace out", 1701 Style)); 1702 1703 Style.NamespaceIndentation = FormatStyle::NI_All; 1704 EXPECT_EQ("namespace out { namespace in {\n" 1705 " int i;\n" 1706 "}} // namespace out::in", 1707 format("namespace out {\n" 1708 "namespace in {\n" 1709 "int i;\n" 1710 "} // namespace in\n" 1711 "} // namespace out", 1712 Style)); 1713 EXPECT_EQ("namespace out { namespace mid {\n" 1714 " namespace in {\n" 1715 " int j;\n" 1716 " } // namespace in\n" 1717 " int k;\n" 1718 "}} // namespace out::mid", 1719 format("namespace out { namespace mid {\n" 1720 "namespace in { int j; } // namespace in\n" 1721 "int k; }} // namespace out::mid", 1722 Style)); 1723 1724 Style.NamespaceIndentation = FormatStyle::NI_Inner; 1725 EXPECT_EQ("namespace out { namespace in {\n" 1726 " int i;\n" 1727 "}} // namespace out::in", 1728 format("namespace out {\n" 1729 "namespace in {\n" 1730 "int i;\n" 1731 "} // namespace in\n" 1732 "} // namespace out", 1733 Style)); 1734 EXPECT_EQ("namespace out { namespace mid { namespace in {\n" 1735 " int i;\n" 1736 "}}} // namespace out::mid::in", 1737 format("namespace out {\n" 1738 "namespace mid {\n" 1739 "namespace in {\n" 1740 "int i;\n" 1741 "} // namespace in\n" 1742 "} // namespace mid\n" 1743 "} // namespace out", 1744 Style)); 1745 } 1746 1747 TEST_F(FormatTest, FormatsExternC) { 1748 verifyFormat("extern \"C\" {\nint a;"); 1749 verifyFormat("extern \"C\" {}"); 1750 verifyFormat("extern \"C\" {\n" 1751 "int foo();\n" 1752 "}"); 1753 verifyFormat("extern \"C\" int foo() {}"); 1754 verifyFormat("extern \"C\" int foo();"); 1755 verifyFormat("extern \"C\" int foo() {\n" 1756 " int i = 42;\n" 1757 " return i;\n" 1758 "}"); 1759 1760 FormatStyle Style = getLLVMStyle(); 1761 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 1762 Style.BraceWrapping.AfterFunction = true; 1763 verifyFormat("extern \"C\" int foo() {}", Style); 1764 verifyFormat("extern \"C\" int foo();", Style); 1765 verifyFormat("extern \"C\" int foo()\n" 1766 "{\n" 1767 " int i = 42;\n" 1768 " return i;\n" 1769 "}", 1770 Style); 1771 1772 Style.BraceWrapping.AfterExternBlock = true; 1773 Style.BraceWrapping.SplitEmptyRecord = false; 1774 verifyFormat("extern \"C\"\n" 1775 "{}", 1776 Style); 1777 verifyFormat("extern \"C\"\n" 1778 "{\n" 1779 " int foo();\n" 1780 "}", 1781 Style); 1782 } 1783 1784 TEST_F(FormatTest, FormatsInlineASM) { 1785 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"); 1786 verifyFormat("asm(\"nop\" ::: \"memory\");"); 1787 verifyFormat( 1788 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" 1789 " \"cpuid\\n\\t\"\n" 1790 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n" 1791 " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n" 1792 " : \"a\"(value));"); 1793 EXPECT_EQ( 1794 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 1795 " __asm {\n" 1796 " mov edx,[that] // vtable in edx\n" 1797 " mov eax,methodIndex\n" 1798 " call [edx][eax*4] // stdcall\n" 1799 " }\n" 1800 "}", 1801 format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 1802 " __asm {\n" 1803 " mov edx,[that] // vtable in edx\n" 1804 " mov eax,methodIndex\n" 1805 " call [edx][eax*4] // stdcall\n" 1806 " }\n" 1807 "}")); 1808 EXPECT_EQ("_asm {\n" 1809 " xor eax, eax;\n" 1810 " cpuid;\n" 1811 "}", 1812 format("_asm {\n" 1813 " xor eax, eax;\n" 1814 " cpuid;\n" 1815 "}")); 1816 verifyFormat("void function() {\n" 1817 " // comment\n" 1818 " asm(\"\");\n" 1819 "}"); 1820 EXPECT_EQ("__asm {\n" 1821 "}\n" 1822 "int i;", 1823 format("__asm {\n" 1824 "}\n" 1825 "int i;")); 1826 } 1827 1828 TEST_F(FormatTest, FormatTryCatch) { 1829 verifyFormat("try {\n" 1830 " throw a * b;\n" 1831 "} catch (int a) {\n" 1832 " // Do nothing.\n" 1833 "} catch (...) {\n" 1834 " exit(42);\n" 1835 "}"); 1836 1837 // Function-level try statements. 1838 verifyFormat("int f() try { return 4; } catch (...) {\n" 1839 " return 5;\n" 1840 "}"); 1841 verifyFormat("class A {\n" 1842 " int a;\n" 1843 " A() try : a(0) {\n" 1844 " } catch (...) {\n" 1845 " throw;\n" 1846 " }\n" 1847 "};\n"); 1848 1849 // Incomplete try-catch blocks. 1850 verifyIncompleteFormat("try {} catch ("); 1851 } 1852 1853 TEST_F(FormatTest, FormatSEHTryCatch) { 1854 verifyFormat("__try {\n" 1855 " int a = b * c;\n" 1856 "} __except (EXCEPTION_EXECUTE_HANDLER) {\n" 1857 " // Do nothing.\n" 1858 "}"); 1859 1860 verifyFormat("__try {\n" 1861 " int a = b * c;\n" 1862 "} __finally {\n" 1863 " // Do nothing.\n" 1864 "}"); 1865 1866 verifyFormat("DEBUG({\n" 1867 " __try {\n" 1868 " } __finally {\n" 1869 " }\n" 1870 "});\n"); 1871 } 1872 1873 TEST_F(FormatTest, IncompleteTryCatchBlocks) { 1874 verifyFormat("try {\n" 1875 " f();\n" 1876 "} catch {\n" 1877 " g();\n" 1878 "}"); 1879 verifyFormat("try {\n" 1880 " f();\n" 1881 "} catch (A a) MACRO(x) {\n" 1882 " g();\n" 1883 "} catch (B b) MACRO(x) {\n" 1884 " g();\n" 1885 "}"); 1886 } 1887 1888 TEST_F(FormatTest, FormatTryCatchBraceStyles) { 1889 FormatStyle Style = getLLVMStyle(); 1890 for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla, 1891 FormatStyle::BS_WebKit}) { 1892 Style.BreakBeforeBraces = BraceStyle; 1893 verifyFormat("try {\n" 1894 " // something\n" 1895 "} catch (...) {\n" 1896 " // something\n" 1897 "}", 1898 Style); 1899 } 1900 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 1901 verifyFormat("try {\n" 1902 " // something\n" 1903 "}\n" 1904 "catch (...) {\n" 1905 " // something\n" 1906 "}", 1907 Style); 1908 verifyFormat("__try {\n" 1909 " // something\n" 1910 "}\n" 1911 "__finally {\n" 1912 " // something\n" 1913 "}", 1914 Style); 1915 verifyFormat("@try {\n" 1916 " // something\n" 1917 "}\n" 1918 "@finally {\n" 1919 " // something\n" 1920 "}", 1921 Style); 1922 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 1923 verifyFormat("try\n" 1924 "{\n" 1925 " // something\n" 1926 "}\n" 1927 "catch (...)\n" 1928 "{\n" 1929 " // something\n" 1930 "}", 1931 Style); 1932 Style.BreakBeforeBraces = FormatStyle::BS_GNU; 1933 verifyFormat("try\n" 1934 " {\n" 1935 " // something\n" 1936 " }\n" 1937 "catch (...)\n" 1938 " {\n" 1939 " // something\n" 1940 " }", 1941 Style); 1942 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 1943 Style.BraceWrapping.BeforeCatch = true; 1944 verifyFormat("try {\n" 1945 " // something\n" 1946 "}\n" 1947 "catch (...) {\n" 1948 " // something\n" 1949 "}", 1950 Style); 1951 } 1952 1953 TEST_F(FormatTest, StaticInitializers) { 1954 verifyFormat("static SomeClass SC = {1, 'a'};"); 1955 1956 verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n" 1957 " 100000000, " 1958 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};"); 1959 1960 // Here, everything other than the "}" would fit on a line. 1961 verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n" 1962 " 10000000000000000000000000};"); 1963 EXPECT_EQ("S s = {a,\n" 1964 "\n" 1965 " b};", 1966 format("S s = {\n" 1967 " a,\n" 1968 "\n" 1969 " b\n" 1970 "};")); 1971 1972 // FIXME: This would fit into the column limit if we'd fit "{ {" on the first 1973 // line. However, the formatting looks a bit off and this probably doesn't 1974 // happen often in practice. 1975 verifyFormat("static int Variable[1] = {\n" 1976 " {1000000000000000000000000000000000000}};", 1977 getLLVMStyleWithColumns(40)); 1978 } 1979 1980 TEST_F(FormatTest, DesignatedInitializers) { 1981 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 1982 verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n" 1983 " .bbbbbbbbbb = 2,\n" 1984 " .cccccccccc = 3,\n" 1985 " .dddddddddd = 4,\n" 1986 " .eeeeeeeeee = 5};"); 1987 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 1988 " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n" 1989 " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n" 1990 " .ccccccccccccccccccccccccccc = 3,\n" 1991 " .ddddddddddddddddddddddddddd = 4,\n" 1992 " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};"); 1993 1994 verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};"); 1995 1996 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 1997 verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n" 1998 " [2] = bbbbbbbbbb,\n" 1999 " [3] = cccccccccc,\n" 2000 " [4] = dddddddddd,\n" 2001 " [5] = eeeeeeeeee};"); 2002 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 2003 " [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 2004 " [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 2005 " [3] = cccccccccccccccccccccccccccccccccccccc,\n" 2006 " [4] = dddddddddddddddddddddddddddddddddddddd,\n" 2007 " [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};"); 2008 } 2009 2010 TEST_F(FormatTest, NestedStaticInitializers) { 2011 verifyFormat("static A x = {{{}}};\n"); 2012 verifyFormat("static A x = {{{init1, init2, init3, init4},\n" 2013 " {init1, init2, init3, init4}}};", 2014 getLLVMStyleWithColumns(50)); 2015 2016 verifyFormat("somes Status::global_reps[3] = {\n" 2017 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 2018 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 2019 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};", 2020 getLLVMStyleWithColumns(60)); 2021 verifyGoogleFormat("SomeType Status::global_reps[3] = {\n" 2022 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 2023 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 2024 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};"); 2025 verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n" 2026 " {rect.fRight - rect.fLeft, rect.fBottom - " 2027 "rect.fTop}};"); 2028 2029 verifyFormat( 2030 "SomeArrayOfSomeType a = {\n" 2031 " {{1, 2, 3},\n" 2032 " {1, 2, 3},\n" 2033 " {111111111111111111111111111111, 222222222222222222222222222222,\n" 2034 " 333333333333333333333333333333},\n" 2035 " {1, 2, 3},\n" 2036 " {1, 2, 3}}};"); 2037 verifyFormat( 2038 "SomeArrayOfSomeType a = {\n" 2039 " {{1, 2, 3}},\n" 2040 " {{1, 2, 3}},\n" 2041 " {{111111111111111111111111111111, 222222222222222222222222222222,\n" 2042 " 333333333333333333333333333333}},\n" 2043 " {{1, 2, 3}},\n" 2044 " {{1, 2, 3}}};"); 2045 2046 verifyFormat("struct {\n" 2047 " unsigned bit;\n" 2048 " const char *const name;\n" 2049 "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n" 2050 " {kOsWin, \"Windows\"},\n" 2051 " {kOsLinux, \"Linux\"},\n" 2052 " {kOsCrOS, \"Chrome OS\"}};"); 2053 verifyFormat("struct {\n" 2054 " unsigned bit;\n" 2055 " const char *const name;\n" 2056 "} kBitsToOs[] = {\n" 2057 " {kOsMac, \"Mac\"},\n" 2058 " {kOsWin, \"Windows\"},\n" 2059 " {kOsLinux, \"Linux\"},\n" 2060 " {kOsCrOS, \"Chrome OS\"},\n" 2061 "};"); 2062 } 2063 2064 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { 2065 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 2066 " \\\n" 2067 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); 2068 } 2069 2070 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { 2071 verifyFormat("virtual void write(ELFWriter *writerrr,\n" 2072 " OwningPtr<FileOutputBuffer> &buffer) = 0;"); 2073 2074 // Do break defaulted and deleted functions. 2075 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 2076 " default;", 2077 getLLVMStyleWithColumns(40)); 2078 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 2079 " delete;", 2080 getLLVMStyleWithColumns(40)); 2081 } 2082 2083 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) { 2084 verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3", 2085 getLLVMStyleWithColumns(40)); 2086 verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 2087 getLLVMStyleWithColumns(40)); 2088 EXPECT_EQ("#define Q \\\n" 2089 " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n" 2090 " \"aaaaaaaa.cpp\"", 2091 format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 2092 getLLVMStyleWithColumns(40))); 2093 } 2094 2095 TEST_F(FormatTest, UnderstandsLinePPDirective) { 2096 EXPECT_EQ("# 123 \"A string literal\"", 2097 format(" # 123 \"A string literal\"")); 2098 } 2099 2100 TEST_F(FormatTest, LayoutUnknownPPDirective) { 2101 EXPECT_EQ("#;", format("#;")); 2102 verifyFormat("#\n;\n;\n;"); 2103 } 2104 2105 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { 2106 EXPECT_EQ("#line 42 \"test\"\n", 2107 format("# \\\n line \\\n 42 \\\n \"test\"\n")); 2108 EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n", 2109 getLLVMStyleWithColumns(12))); 2110 } 2111 2112 TEST_F(FormatTest, EndOfFileEndsPPDirective) { 2113 EXPECT_EQ("#line 42 \"test\"", 2114 format("# \\\n line \\\n 42 \\\n \"test\"")); 2115 EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B")); 2116 } 2117 2118 TEST_F(FormatTest, DoesntRemoveUnknownTokens) { 2119 verifyFormat("#define A \\x20"); 2120 verifyFormat("#define A \\ x20"); 2121 EXPECT_EQ("#define A \\ x20", format("#define A \\ x20")); 2122 verifyFormat("#define A ''"); 2123 verifyFormat("#define A ''qqq"); 2124 verifyFormat("#define A `qqq"); 2125 verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");"); 2126 EXPECT_EQ("const char *c = STRINGIFY(\n" 2127 "\\na : b);", 2128 format("const char * c = STRINGIFY(\n" 2129 "\\na : b);")); 2130 2131 verifyFormat("a\r\\"); 2132 verifyFormat("a\v\\"); 2133 verifyFormat("a\f\\"); 2134 } 2135 2136 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { 2137 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13)); 2138 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12)); 2139 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); 2140 // FIXME: We never break before the macro name. 2141 verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12)); 2142 2143 verifyFormat("#define A A\n#define A A"); 2144 verifyFormat("#define A(X) A\n#define A A"); 2145 2146 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23)); 2147 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22)); 2148 } 2149 2150 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { 2151 EXPECT_EQ("// somecomment\n" 2152 "#include \"a.h\"\n" 2153 "#define A( \\\n" 2154 " A, B)\n" 2155 "#include \"b.h\"\n" 2156 "// somecomment\n", 2157 format(" // somecomment\n" 2158 " #include \"a.h\"\n" 2159 "#define A(A,\\\n" 2160 " B)\n" 2161 " #include \"b.h\"\n" 2162 " // somecomment\n", 2163 getLLVMStyleWithColumns(13))); 2164 } 2165 2166 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); } 2167 2168 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { 2169 EXPECT_EQ("#define A \\\n" 2170 " c; \\\n" 2171 " e;\n" 2172 "f;", 2173 format("#define A c; e;\n" 2174 "f;", 2175 getLLVMStyleWithColumns(14))); 2176 } 2177 2178 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); } 2179 2180 TEST_F(FormatTest, MacroDefinitionInsideStatement) { 2181 EXPECT_EQ("int x,\n" 2182 "#define A\n" 2183 " y;", 2184 format("int x,\n#define A\ny;")); 2185 } 2186 2187 TEST_F(FormatTest, HashInMacroDefinition) { 2188 EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle())); 2189 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); 2190 verifyFormat("#define A \\\n" 2191 " { \\\n" 2192 " f(#c); \\\n" 2193 " }", 2194 getLLVMStyleWithColumns(11)); 2195 2196 verifyFormat("#define A(X) \\\n" 2197 " void function##X()", 2198 getLLVMStyleWithColumns(22)); 2199 2200 verifyFormat("#define A(a, b, c) \\\n" 2201 " void a##b##c()", 2202 getLLVMStyleWithColumns(22)); 2203 2204 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22)); 2205 } 2206 2207 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { 2208 EXPECT_EQ("#define A (x)", format("#define A (x)")); 2209 EXPECT_EQ("#define A(x)", format("#define A(x)")); 2210 } 2211 2212 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) { 2213 EXPECT_EQ("#define A b;", format("#define A \\\n" 2214 " \\\n" 2215 " b;", 2216 getLLVMStyleWithColumns(25))); 2217 EXPECT_EQ("#define A \\\n" 2218 " \\\n" 2219 " a; \\\n" 2220 " b;", 2221 format("#define A \\\n" 2222 " \\\n" 2223 " a; \\\n" 2224 " b;", 2225 getLLVMStyleWithColumns(11))); 2226 EXPECT_EQ("#define A \\\n" 2227 " a; \\\n" 2228 " \\\n" 2229 " b;", 2230 format("#define A \\\n" 2231 " a; \\\n" 2232 " \\\n" 2233 " b;", 2234 getLLVMStyleWithColumns(11))); 2235 } 2236 2237 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { 2238 verifyIncompleteFormat("#define A :"); 2239 verifyFormat("#define SOMECASES \\\n" 2240 " case 1: \\\n" 2241 " case 2\n", 2242 getLLVMStyleWithColumns(20)); 2243 verifyFormat("#define MACRO(a) \\\n" 2244 " if (a) \\\n" 2245 " f(); \\\n" 2246 " else \\\n" 2247 " g()", 2248 getLLVMStyleWithColumns(18)); 2249 verifyFormat("#define A template <typename T>"); 2250 verifyIncompleteFormat("#define STR(x) #x\n" 2251 "f(STR(this_is_a_string_literal{));"); 2252 verifyFormat("#pragma omp threadprivate( \\\n" 2253 " y)), // expected-warning", 2254 getLLVMStyleWithColumns(28)); 2255 verifyFormat("#d, = };"); 2256 verifyFormat("#if \"a"); 2257 verifyIncompleteFormat("({\n" 2258 "#define b \\\n" 2259 " } \\\n" 2260 " a\n" 2261 "a", 2262 getLLVMStyleWithColumns(15)); 2263 verifyFormat("#define A \\\n" 2264 " { \\\n" 2265 " {\n" 2266 "#define B \\\n" 2267 " } \\\n" 2268 " }", 2269 getLLVMStyleWithColumns(15)); 2270 verifyNoCrash("#if a\na(\n#else\n#endif\n{a"); 2271 verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}"); 2272 verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};"); 2273 verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}"); 2274 } 2275 2276 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) { 2277 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline. 2278 EXPECT_EQ("class A : public QObject {\n" 2279 " Q_OBJECT\n" 2280 "\n" 2281 " A() {}\n" 2282 "};", 2283 format("class A : public QObject {\n" 2284 " Q_OBJECT\n" 2285 "\n" 2286 " A() {\n}\n" 2287 "} ;")); 2288 EXPECT_EQ("MACRO\n" 2289 "/*static*/ int i;", 2290 format("MACRO\n" 2291 " /*static*/ int i;")); 2292 EXPECT_EQ("SOME_MACRO\n" 2293 "namespace {\n" 2294 "void f();\n" 2295 "} // namespace", 2296 format("SOME_MACRO\n" 2297 " namespace {\n" 2298 "void f( );\n" 2299 "} // namespace")); 2300 // Only if the identifier contains at least 5 characters. 2301 EXPECT_EQ("HTTP f();", format("HTTP\nf();")); 2302 EXPECT_EQ("MACRO\nf();", format("MACRO\nf();")); 2303 // Only if everything is upper case. 2304 EXPECT_EQ("class A : public QObject {\n" 2305 " Q_Object A() {}\n" 2306 "};", 2307 format("class A : public QObject {\n" 2308 " Q_Object\n" 2309 " A() {\n}\n" 2310 "} ;")); 2311 2312 // Only if the next line can actually start an unwrapped line. 2313 EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;", 2314 format("SOME_WEIRD_LOG_MACRO\n" 2315 "<< SomeThing;")); 2316 2317 verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), " 2318 "(n, buffers))\n", 2319 getChromiumStyle(FormatStyle::LK_Cpp)); 2320 } 2321 2322 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) { 2323 EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 2324 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 2325 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 2326 "class X {};\n" 2327 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 2328 "int *createScopDetectionPass() { return 0; }", 2329 format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 2330 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 2331 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 2332 " class X {};\n" 2333 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 2334 " int *createScopDetectionPass() { return 0; }")); 2335 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as 2336 // braces, so that inner block is indented one level more. 2337 EXPECT_EQ("int q() {\n" 2338 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 2339 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 2340 " IPC_END_MESSAGE_MAP()\n" 2341 "}", 2342 format("int q() {\n" 2343 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 2344 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 2345 " IPC_END_MESSAGE_MAP()\n" 2346 "}")); 2347 2348 // Same inside macros. 2349 EXPECT_EQ("#define LIST(L) \\\n" 2350 " L(A) \\\n" 2351 " L(B) \\\n" 2352 " L(C)", 2353 format("#define LIST(L) \\\n" 2354 " L(A) \\\n" 2355 " L(B) \\\n" 2356 " L(C)", 2357 getGoogleStyle())); 2358 2359 // These must not be recognized as macros. 2360 EXPECT_EQ("int q() {\n" 2361 " f(x);\n" 2362 " f(x) {}\n" 2363 " f(x)->g();\n" 2364 " f(x)->*g();\n" 2365 " f(x).g();\n" 2366 " f(x) = x;\n" 2367 " f(x) += x;\n" 2368 " f(x) -= x;\n" 2369 " f(x) *= x;\n" 2370 " f(x) /= x;\n" 2371 " f(x) %= x;\n" 2372 " f(x) &= x;\n" 2373 " f(x) |= x;\n" 2374 " f(x) ^= x;\n" 2375 " f(x) >>= x;\n" 2376 " f(x) <<= x;\n" 2377 " f(x)[y].z();\n" 2378 " LOG(INFO) << x;\n" 2379 " ifstream(x) >> x;\n" 2380 "}\n", 2381 format("int q() {\n" 2382 " f(x)\n;\n" 2383 " f(x)\n {}\n" 2384 " f(x)\n->g();\n" 2385 " f(x)\n->*g();\n" 2386 " f(x)\n.g();\n" 2387 " f(x)\n = x;\n" 2388 " f(x)\n += x;\n" 2389 " f(x)\n -= x;\n" 2390 " f(x)\n *= x;\n" 2391 " f(x)\n /= x;\n" 2392 " f(x)\n %= x;\n" 2393 " f(x)\n &= x;\n" 2394 " f(x)\n |= x;\n" 2395 " f(x)\n ^= x;\n" 2396 " f(x)\n >>= x;\n" 2397 " f(x)\n <<= x;\n" 2398 " f(x)\n[y].z();\n" 2399 " LOG(INFO)\n << x;\n" 2400 " ifstream(x)\n >> x;\n" 2401 "}\n")); 2402 EXPECT_EQ("int q() {\n" 2403 " F(x)\n" 2404 " if (1) {\n" 2405 " }\n" 2406 " F(x)\n" 2407 " while (1) {\n" 2408 " }\n" 2409 " F(x)\n" 2410 " G(x);\n" 2411 " F(x)\n" 2412 " try {\n" 2413 " Q();\n" 2414 " } catch (...) {\n" 2415 " }\n" 2416 "}\n", 2417 format("int q() {\n" 2418 "F(x)\n" 2419 "if (1) {}\n" 2420 "F(x)\n" 2421 "while (1) {}\n" 2422 "F(x)\n" 2423 "G(x);\n" 2424 "F(x)\n" 2425 "try { Q(); } catch (...) {}\n" 2426 "}\n")); 2427 EXPECT_EQ("class A {\n" 2428 " A() : t(0) {}\n" 2429 " A(int i) noexcept() : {}\n" 2430 " A(X x)\n" // FIXME: function-level try blocks are broken. 2431 " try : t(0) {\n" 2432 " } catch (...) {\n" 2433 " }\n" 2434 "};", 2435 format("class A {\n" 2436 " A()\n : t(0) {}\n" 2437 " A(int i)\n noexcept() : {}\n" 2438 " A(X x)\n" 2439 " try : t(0) {} catch (...) {}\n" 2440 "};")); 2441 EXPECT_EQ("class SomeClass {\n" 2442 "public:\n" 2443 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2444 "};", 2445 format("class SomeClass {\n" 2446 "public:\n" 2447 " SomeClass()\n" 2448 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2449 "};")); 2450 EXPECT_EQ("class SomeClass {\n" 2451 "public:\n" 2452 " SomeClass()\n" 2453 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2454 "};", 2455 format("class SomeClass {\n" 2456 "public:\n" 2457 " SomeClass()\n" 2458 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2459 "};", 2460 getLLVMStyleWithColumns(40))); 2461 2462 verifyFormat("MACRO(>)"); 2463 } 2464 2465 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) { 2466 verifyFormat("#define A \\\n" 2467 " f({ \\\n" 2468 " g(); \\\n" 2469 " });", 2470 getLLVMStyleWithColumns(11)); 2471 } 2472 2473 TEST_F(FormatTest, IndentPreprocessorDirectives) { 2474 FormatStyle Style = getLLVMStyle(); 2475 Style.IndentPPDirectives = FormatStyle::PPDIS_None; 2476 Style.ColumnLimit = 40; 2477 verifyFormat("#ifdef _WIN32\n" 2478 "#define A 0\n" 2479 "#ifdef VAR2\n" 2480 "#define B 1\n" 2481 "#include <someheader.h>\n" 2482 "#define MACRO \\\n" 2483 " some_very_long_func_aaaaaaaaaa();\n" 2484 "#endif\n" 2485 "#else\n" 2486 "#define A 1\n" 2487 "#endif", 2488 Style); 2489 Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash; 2490 verifyFormat("#ifdef _WIN32\n" 2491 "# define A 0\n" 2492 "# ifdef VAR2\n" 2493 "# define B 1\n" 2494 "# include <someheader.h>\n" 2495 "# define MACRO \\\n" 2496 " some_very_long_func_aaaaaaaaaa();\n" 2497 "# endif\n" 2498 "#else\n" 2499 "# define A 1\n" 2500 "#endif", 2501 Style); 2502 verifyFormat("#if A\n" 2503 "# define MACRO \\\n" 2504 " void a(int x) { \\\n" 2505 " b(); \\\n" 2506 " c(); \\\n" 2507 " d(); \\\n" 2508 " e(); \\\n" 2509 " f(); \\\n" 2510 " }\n" 2511 "#endif", 2512 Style); 2513 // Comments before include guard. 2514 verifyFormat("// file comment\n" 2515 "// file comment\n" 2516 "#ifndef HEADER_H\n" 2517 "#define HEADER_H\n" 2518 "code();\n" 2519 "#endif", 2520 Style); 2521 // Test with include guards. 2522 verifyFormat("#ifndef HEADER_H\n" 2523 "#define HEADER_H\n" 2524 "code();\n" 2525 "#endif", 2526 Style); 2527 // Include guards must have a #define with the same variable immediately 2528 // after #ifndef. 2529 verifyFormat("#ifndef NOT_GUARD\n" 2530 "# define FOO\n" 2531 "code();\n" 2532 "#endif", 2533 Style); 2534 2535 // Include guards must cover the entire file. 2536 verifyFormat("code();\n" 2537 "code();\n" 2538 "#ifndef NOT_GUARD\n" 2539 "# define NOT_GUARD\n" 2540 "code();\n" 2541 "#endif", 2542 Style); 2543 verifyFormat("#ifndef NOT_GUARD\n" 2544 "# define NOT_GUARD\n" 2545 "code();\n" 2546 "#endif\n" 2547 "code();", 2548 Style); 2549 // Test with trailing blank lines. 2550 verifyFormat("#ifndef HEADER_H\n" 2551 "#define HEADER_H\n" 2552 "code();\n" 2553 "#endif\n", 2554 Style); 2555 // Include guards don't have #else. 2556 verifyFormat("#ifndef NOT_GUARD\n" 2557 "# define NOT_GUARD\n" 2558 "code();\n" 2559 "#else\n" 2560 "#endif", 2561 Style); 2562 verifyFormat("#ifndef NOT_GUARD\n" 2563 "# define NOT_GUARD\n" 2564 "code();\n" 2565 "#elif FOO\n" 2566 "#endif", 2567 Style); 2568 // Non-identifier #define after potential include guard. 2569 verifyFormat("#ifndef FOO\n" 2570 "# define 1\n" 2571 "#endif\n", 2572 Style); 2573 // #if closes past last non-preprocessor line. 2574 verifyFormat("#ifndef FOO\n" 2575 "#define FOO\n" 2576 "#if 1\n" 2577 "int i;\n" 2578 "# define A 0\n" 2579 "#endif\n" 2580 "#endif\n", 2581 Style); 2582 // FIXME: This doesn't handle the case where there's code between the 2583 // #ifndef and #define but all other conditions hold. This is because when 2584 // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the 2585 // previous code line yet, so we can't detect it. 2586 EXPECT_EQ("#ifndef NOT_GUARD\n" 2587 "code();\n" 2588 "#define NOT_GUARD\n" 2589 "code();\n" 2590 "#endif", 2591 format("#ifndef NOT_GUARD\n" 2592 "code();\n" 2593 "# define NOT_GUARD\n" 2594 "code();\n" 2595 "#endif", 2596 Style)); 2597 // FIXME: This doesn't handle cases where legitimate preprocessor lines may 2598 // be outside an include guard. Examples are #pragma once and 2599 // #pragma GCC diagnostic, or anything else that does not change the meaning 2600 // of the file if it's included multiple times. 2601 EXPECT_EQ("#ifdef WIN32\n" 2602 "# pragma once\n" 2603 "#endif\n" 2604 "#ifndef HEADER_H\n" 2605 "# define HEADER_H\n" 2606 "code();\n" 2607 "#endif", 2608 format("#ifdef WIN32\n" 2609 "# pragma once\n" 2610 "#endif\n" 2611 "#ifndef HEADER_H\n" 2612 "#define HEADER_H\n" 2613 "code();\n" 2614 "#endif", 2615 Style)); 2616 // FIXME: This does not detect when there is a single non-preprocessor line 2617 // in front of an include-guard-like structure where other conditions hold 2618 // because ScopedLineState hides the line. 2619 EXPECT_EQ("code();\n" 2620 "#ifndef HEADER_H\n" 2621 "#define HEADER_H\n" 2622 "code();\n" 2623 "#endif", 2624 format("code();\n" 2625 "#ifndef HEADER_H\n" 2626 "# define HEADER_H\n" 2627 "code();\n" 2628 "#endif", 2629 Style)); 2630 // Keep comments aligned with #, otherwise indent comments normally. These 2631 // tests cannot use verifyFormat because messUp manipulates leading 2632 // whitespace. 2633 { 2634 const char *Expected = "" 2635 "void f() {\n" 2636 "#if 1\n" 2637 "// Preprocessor aligned.\n" 2638 "# define A 0\n" 2639 " // Code. Separated by blank line.\n" 2640 "\n" 2641 "# define B 0\n" 2642 " // Code. Not aligned with #\n" 2643 "# define C 0\n" 2644 "#endif"; 2645 const char *ToFormat = "" 2646 "void f() {\n" 2647 "#if 1\n" 2648 "// Preprocessor aligned.\n" 2649 "# define A 0\n" 2650 "// Code. Separated by blank line.\n" 2651 "\n" 2652 "# define B 0\n" 2653 " // Code. Not aligned with #\n" 2654 "# define C 0\n" 2655 "#endif"; 2656 EXPECT_EQ(Expected, format(ToFormat, Style)); 2657 EXPECT_EQ(Expected, format(Expected, Style)); 2658 } 2659 // Keep block quotes aligned. 2660 { 2661 const char *Expected = "" 2662 "void f() {\n" 2663 "#if 1\n" 2664 "/* Preprocessor aligned. */\n" 2665 "# define A 0\n" 2666 " /* Code. Separated by blank line. */\n" 2667 "\n" 2668 "# define B 0\n" 2669 " /* Code. Not aligned with # */\n" 2670 "# define C 0\n" 2671 "#endif"; 2672 const char *ToFormat = "" 2673 "void f() {\n" 2674 "#if 1\n" 2675 "/* Preprocessor aligned. */\n" 2676 "# define A 0\n" 2677 "/* Code. Separated by blank line. */\n" 2678 "\n" 2679 "# define B 0\n" 2680 " /* Code. Not aligned with # */\n" 2681 "# define C 0\n" 2682 "#endif"; 2683 EXPECT_EQ(Expected, format(ToFormat, Style)); 2684 EXPECT_EQ(Expected, format(Expected, Style)); 2685 } 2686 // Keep comments aligned with un-indented directives. 2687 { 2688 const char *Expected = "" 2689 "void f() {\n" 2690 "// Preprocessor aligned.\n" 2691 "#define A 0\n" 2692 " // Code. Separated by blank line.\n" 2693 "\n" 2694 "#define B 0\n" 2695 " // Code. Not aligned with #\n" 2696 "#define C 0\n"; 2697 const char *ToFormat = "" 2698 "void f() {\n" 2699 "// Preprocessor aligned.\n" 2700 "#define A 0\n" 2701 "// Code. Separated by blank line.\n" 2702 "\n" 2703 "#define B 0\n" 2704 " // Code. Not aligned with #\n" 2705 "#define C 0\n"; 2706 EXPECT_EQ(Expected, format(ToFormat, Style)); 2707 EXPECT_EQ(Expected, format(Expected, Style)); 2708 } 2709 // Test with tabs. 2710 Style.UseTab = FormatStyle::UT_Always; 2711 Style.IndentWidth = 8; 2712 Style.TabWidth = 8; 2713 verifyFormat("#ifdef _WIN32\n" 2714 "#\tdefine A 0\n" 2715 "#\tifdef VAR2\n" 2716 "#\t\tdefine B 1\n" 2717 "#\t\tinclude <someheader.h>\n" 2718 "#\t\tdefine MACRO \\\n" 2719 "\t\t\tsome_very_long_func_aaaaaaaaaa();\n" 2720 "#\tendif\n" 2721 "#else\n" 2722 "#\tdefine A 1\n" 2723 "#endif", 2724 Style); 2725 2726 // Regression test: Multiline-macro inside include guards. 2727 verifyFormat("#ifndef HEADER_H\n" 2728 "#define HEADER_H\n" 2729 "#define A() \\\n" 2730 " int i; \\\n" 2731 " int j;\n" 2732 "#endif // HEADER_H", 2733 getLLVMStyleWithColumns(20)); 2734 } 2735 2736 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { 2737 verifyFormat("{\n { a #c; }\n}"); 2738 } 2739 2740 TEST_F(FormatTest, FormatUnbalancedStructuralElements) { 2741 EXPECT_EQ("#define A \\\n { \\\n {\nint i;", 2742 format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); 2743 EXPECT_EQ("#define A \\\n } \\\n }\nint i;", 2744 format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); 2745 } 2746 2747 TEST_F(FormatTest, EscapedNewlines) { 2748 FormatStyle Narrow = getLLVMStyleWithColumns(11); 2749 EXPECT_EQ("#define A \\\n int i; \\\n int j;", 2750 format("#define A \\\nint i;\\\n int j;", Narrow)); 2751 EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;")); 2752 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 2753 EXPECT_EQ("/* \\ \\ \\\n */", format("\\\n/* \\ \\ \\\n */")); 2754 EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>")); 2755 2756 FormatStyle AlignLeft = getLLVMStyle(); 2757 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 2758 EXPECT_EQ("#define MACRO(x) \\\n" 2759 "private: \\\n" 2760 " int x(int a);\n", 2761 format("#define MACRO(x) \\\n" 2762 "private: \\\n" 2763 " int x(int a);\n", 2764 AlignLeft)); 2765 2766 // CRLF line endings 2767 EXPECT_EQ("#define A \\\r\n int i; \\\r\n int j;", 2768 format("#define A \\\r\nint i;\\\r\n int j;", Narrow)); 2769 EXPECT_EQ("#define A\r\n\r\nint i;", format("#define A \\\r\n\r\n int i;")); 2770 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 2771 EXPECT_EQ("/* \\ \\ \\\r\n */", format("\\\r\n/* \\ \\ \\\r\n */")); 2772 EXPECT_EQ("<a\r\n\\\\\r\n>", format("<a\r\n\\\\\r\n>")); 2773 EXPECT_EQ("#define MACRO(x) \\\r\n" 2774 "private: \\\r\n" 2775 " int x(int a);\r\n", 2776 format("#define MACRO(x) \\\r\n" 2777 "private: \\\r\n" 2778 " int x(int a);\r\n", 2779 AlignLeft)); 2780 2781 FormatStyle DontAlign = getLLVMStyle(); 2782 DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 2783 DontAlign.MaxEmptyLinesToKeep = 3; 2784 // FIXME: can't use verifyFormat here because the newline before 2785 // "public:" is not inserted the first time it's reformatted 2786 EXPECT_EQ("#define A \\\n" 2787 " class Foo { \\\n" 2788 " void bar(); \\\n" 2789 "\\\n" 2790 "\\\n" 2791 "\\\n" 2792 " public: \\\n" 2793 " void baz(); \\\n" 2794 " };", 2795 format("#define A \\\n" 2796 " class Foo { \\\n" 2797 " void bar(); \\\n" 2798 "\\\n" 2799 "\\\n" 2800 "\\\n" 2801 " public: \\\n" 2802 " void baz(); \\\n" 2803 " };", 2804 DontAlign)); 2805 } 2806 2807 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { 2808 verifyFormat("#define A \\\n" 2809 " int v( \\\n" 2810 " a); \\\n" 2811 " int i;", 2812 getLLVMStyleWithColumns(11)); 2813 } 2814 2815 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { 2816 EXPECT_EQ( 2817 "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 2818 " \\\n" 2819 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 2820 "\n" 2821 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 2822 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", 2823 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" 2824 "\\\n" 2825 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 2826 " \n" 2827 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 2828 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); 2829 } 2830 2831 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { 2832 EXPECT_EQ("int\n" 2833 "#define A\n" 2834 " a;", 2835 format("int\n#define A\na;")); 2836 verifyFormat("functionCallTo(\n" 2837 " someOtherFunction(\n" 2838 " withSomeParameters, whichInSequence,\n" 2839 " areLongerThanALine(andAnotherCall,\n" 2840 "#define A B\n" 2841 " withMoreParamters,\n" 2842 " whichStronglyInfluenceTheLayout),\n" 2843 " andMoreParameters),\n" 2844 " trailing);", 2845 getLLVMStyleWithColumns(69)); 2846 verifyFormat("Foo::Foo()\n" 2847 "#ifdef BAR\n" 2848 " : baz(0)\n" 2849 "#endif\n" 2850 "{\n" 2851 "}"); 2852 verifyFormat("void f() {\n" 2853 " if (true)\n" 2854 "#ifdef A\n" 2855 " f(42);\n" 2856 " x();\n" 2857 "#else\n" 2858 " g();\n" 2859 " x();\n" 2860 "#endif\n" 2861 "}"); 2862 verifyFormat("void f(param1, param2,\n" 2863 " param3,\n" 2864 "#ifdef A\n" 2865 " param4(param5,\n" 2866 "#ifdef A1\n" 2867 " param6,\n" 2868 "#ifdef A2\n" 2869 " param7),\n" 2870 "#else\n" 2871 " param8),\n" 2872 " param9,\n" 2873 "#endif\n" 2874 " param10,\n" 2875 "#endif\n" 2876 " param11)\n" 2877 "#else\n" 2878 " param12)\n" 2879 "#endif\n" 2880 "{\n" 2881 " x();\n" 2882 "}", 2883 getLLVMStyleWithColumns(28)); 2884 verifyFormat("#if 1\n" 2885 "int i;"); 2886 verifyFormat("#if 1\n" 2887 "#endif\n" 2888 "#if 1\n" 2889 "#else\n" 2890 "#endif\n"); 2891 verifyFormat("DEBUG({\n" 2892 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2893 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 2894 "});\n" 2895 "#if a\n" 2896 "#else\n" 2897 "#endif"); 2898 2899 verifyIncompleteFormat("void f(\n" 2900 "#if A\n" 2901 ");\n" 2902 "#else\n" 2903 "#endif"); 2904 } 2905 2906 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) { 2907 verifyFormat("#endif\n" 2908 "#if B"); 2909 } 2910 2911 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) { 2912 FormatStyle SingleLine = getLLVMStyle(); 2913 SingleLine.AllowShortIfStatementsOnASingleLine = true; 2914 verifyFormat("#if 0\n" 2915 "#elif 1\n" 2916 "#endif\n" 2917 "void foo() {\n" 2918 " if (test) foo2();\n" 2919 "}", 2920 SingleLine); 2921 } 2922 2923 TEST_F(FormatTest, LayoutBlockInsideParens) { 2924 verifyFormat("functionCall({ int i; });"); 2925 verifyFormat("functionCall({\n" 2926 " int i;\n" 2927 " int j;\n" 2928 "});"); 2929 verifyFormat("functionCall(\n" 2930 " {\n" 2931 " int i;\n" 2932 " int j;\n" 2933 " },\n" 2934 " aaaa, bbbb, cccc);"); 2935 verifyFormat("functionA(functionB({\n" 2936 " int i;\n" 2937 " int j;\n" 2938 " }),\n" 2939 " aaaa, bbbb, cccc);"); 2940 verifyFormat("functionCall(\n" 2941 " {\n" 2942 " int i;\n" 2943 " int j;\n" 2944 " },\n" 2945 " aaaa, bbbb, // comment\n" 2946 " cccc);"); 2947 verifyFormat("functionA(functionB({\n" 2948 " int i;\n" 2949 " int j;\n" 2950 " }),\n" 2951 " aaaa, bbbb, // comment\n" 2952 " cccc);"); 2953 verifyFormat("functionCall(aaaa, bbbb, { int i; });"); 2954 verifyFormat("functionCall(aaaa, bbbb, {\n" 2955 " int i;\n" 2956 " int j;\n" 2957 "});"); 2958 verifyFormat( 2959 "Aaa(\n" // FIXME: There shouldn't be a linebreak here. 2960 " {\n" 2961 " int i; // break\n" 2962 " },\n" 2963 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 2964 " ccccccccccccccccc));"); 2965 verifyFormat("DEBUG({\n" 2966 " if (a)\n" 2967 " f();\n" 2968 "});"); 2969 } 2970 2971 TEST_F(FormatTest, LayoutBlockInsideStatement) { 2972 EXPECT_EQ("SOME_MACRO { int i; }\n" 2973 "int i;", 2974 format(" SOME_MACRO {int i;} int i;")); 2975 } 2976 2977 TEST_F(FormatTest, LayoutNestedBlocks) { 2978 verifyFormat("void AddOsStrings(unsigned bitmask) {\n" 2979 " struct s {\n" 2980 " int i;\n" 2981 " };\n" 2982 " s kBitsToOs[] = {{10}};\n" 2983 " for (int i = 0; i < 10; ++i)\n" 2984 " return;\n" 2985 "}"); 2986 verifyFormat("call(parameter, {\n" 2987 " something();\n" 2988 " // Comment using all columns.\n" 2989 " somethingelse();\n" 2990 "});", 2991 getLLVMStyleWithColumns(40)); 2992 verifyFormat("DEBUG( //\n" 2993 " { f(); }, a);"); 2994 verifyFormat("DEBUG( //\n" 2995 " {\n" 2996 " f(); //\n" 2997 " },\n" 2998 " a);"); 2999 3000 EXPECT_EQ("call(parameter, {\n" 3001 " something();\n" 3002 " // Comment too\n" 3003 " // looooooooooong.\n" 3004 " somethingElse();\n" 3005 "});", 3006 format("call(parameter, {\n" 3007 " something();\n" 3008 " // Comment too looooooooooong.\n" 3009 " somethingElse();\n" 3010 "});", 3011 getLLVMStyleWithColumns(29))); 3012 EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });")); 3013 EXPECT_EQ("DEBUG({ // comment\n" 3014 " int i;\n" 3015 "});", 3016 format("DEBUG({ // comment\n" 3017 "int i;\n" 3018 "});")); 3019 EXPECT_EQ("DEBUG({\n" 3020 " int i;\n" 3021 "\n" 3022 " // comment\n" 3023 " int j;\n" 3024 "});", 3025 format("DEBUG({\n" 3026 " int i;\n" 3027 "\n" 3028 " // comment\n" 3029 " int j;\n" 3030 "});")); 3031 3032 verifyFormat("DEBUG({\n" 3033 " if (a)\n" 3034 " return;\n" 3035 "});"); 3036 verifyGoogleFormat("DEBUG({\n" 3037 " if (a) return;\n" 3038 "});"); 3039 FormatStyle Style = getGoogleStyle(); 3040 Style.ColumnLimit = 45; 3041 verifyFormat("Debug(aaaaa,\n" 3042 " {\n" 3043 " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n" 3044 " },\n" 3045 " a);", 3046 Style); 3047 3048 verifyFormat("SomeFunction({MACRO({ return output; }), b});"); 3049 3050 verifyNoCrash("^{v^{a}}"); 3051 } 3052 3053 TEST_F(FormatTest, FormatNestedBlocksInMacros) { 3054 EXPECT_EQ("#define MACRO() \\\n" 3055 " Debug(aaa, /* force line break */ \\\n" 3056 " { \\\n" 3057 " int i; \\\n" 3058 " int j; \\\n" 3059 " })", 3060 format("#define MACRO() Debug(aaa, /* force line break */ \\\n" 3061 " { int i; int j; })", 3062 getGoogleStyle())); 3063 3064 EXPECT_EQ("#define A \\\n" 3065 " [] { \\\n" 3066 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 3067 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 3068 " }", 3069 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 3070 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 3071 getGoogleStyle())); 3072 } 3073 3074 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { 3075 EXPECT_EQ("{}", format("{}")); 3076 verifyFormat("enum E {};"); 3077 verifyFormat("enum E {}"); 3078 } 3079 3080 TEST_F(FormatTest, FormatBeginBlockEndMacros) { 3081 FormatStyle Style = getLLVMStyle(); 3082 Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$"; 3083 Style.MacroBlockEnd = "^[A-Z_]+_END$"; 3084 verifyFormat("FOO_BEGIN\n" 3085 " FOO_ENTRY\n" 3086 "FOO_END", Style); 3087 verifyFormat("FOO_BEGIN\n" 3088 " NESTED_FOO_BEGIN\n" 3089 " NESTED_FOO_ENTRY\n" 3090 " NESTED_FOO_END\n" 3091 "FOO_END", Style); 3092 verifyFormat("FOO_BEGIN(Foo, Bar)\n" 3093 " int x;\n" 3094 " x = 1;\n" 3095 "FOO_END(Baz)", Style); 3096 } 3097 3098 //===----------------------------------------------------------------------===// 3099 // Line break tests. 3100 //===----------------------------------------------------------------------===// 3101 3102 TEST_F(FormatTest, PreventConfusingIndents) { 3103 verifyFormat( 3104 "void f() {\n" 3105 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" 3106 " parameter, parameter, parameter)),\n" 3107 " SecondLongCall(parameter));\n" 3108 "}"); 3109 verifyFormat( 3110 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3111 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 3112 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3113 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 3114 verifyFormat( 3115 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3116 " [aaaaaaaaaaaaaaaaaaaaaaaa\n" 3117 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 3118 " [aaaaaaaaaaaaaaaaaaaaaaaa]];"); 3119 verifyFormat( 3120 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 3121 " aaaaaaaaaaaaaaaaaaaaaaaa<\n" 3122 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n" 3123 " aaaaaaaaaaaaaaaaaaaaaaaa>;"); 3124 verifyFormat("int a = bbbb && ccc &&\n" 3125 " fffff(\n" 3126 "#define A Just forcing a new line\n" 3127 " ddd);"); 3128 } 3129 3130 TEST_F(FormatTest, LineBreakingInBinaryExpressions) { 3131 verifyFormat( 3132 "bool aaaaaaa =\n" 3133 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n" 3134 " bbbbbbbb();"); 3135 verifyFormat( 3136 "bool aaaaaaa =\n" 3137 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n" 3138 " bbbbbbbb();"); 3139 3140 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 3141 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n" 3142 " ccccccccc == ddddddddddd;"); 3143 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 3144 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n" 3145 " ccccccccc == ddddddddddd;"); 3146 verifyFormat( 3147 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 3148 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n" 3149 " ccccccccc == ddddddddddd;"); 3150 3151 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 3152 " aaaaaa) &&\n" 3153 " bbbbbb && cccccc;"); 3154 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 3155 " aaaaaa) >>\n" 3156 " bbbbbb;"); 3157 verifyFormat("aa = Whitespaces.addUntouchableComment(\n" 3158 " SourceMgr.getSpellingColumnNumber(\n" 3159 " TheLine.Last->FormatTok.Tok.getLocation()) -\n" 3160 " 1);"); 3161 3162 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3163 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n" 3164 " cccccc) {\n}"); 3165 verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3166 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n" 3167 " cccccc) {\n}"); 3168 verifyFormat("b = a &&\n" 3169 " // Comment\n" 3170 " b.c && d;"); 3171 3172 // If the LHS of a comparison is not a binary expression itself, the 3173 // additional linebreak confuses many people. 3174 verifyFormat( 3175 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3176 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n" 3177 "}"); 3178 verifyFormat( 3179 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3180 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3181 "}"); 3182 verifyFormat( 3183 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n" 3184 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3185 "}"); 3186 verifyFormat( 3187 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3188 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n" 3189 "}"); 3190 // Even explicit parentheses stress the precedence enough to make the 3191 // additional break unnecessary. 3192 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3193 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3194 "}"); 3195 // This cases is borderline, but with the indentation it is still readable. 3196 verifyFormat( 3197 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3198 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3199 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 3200 "}", 3201 getLLVMStyleWithColumns(75)); 3202 3203 // If the LHS is a binary expression, we should still use the additional break 3204 // as otherwise the formatting hides the operator precedence. 3205 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3206 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3207 " 5) {\n" 3208 "}"); 3209 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3210 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n" 3211 " 5) {\n" 3212 "}"); 3213 3214 FormatStyle OnePerLine = getLLVMStyle(); 3215 OnePerLine.BinPackParameters = false; 3216 verifyFormat( 3217 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3218 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3219 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}", 3220 OnePerLine); 3221 3222 verifyFormat("int i = someFunction(aaaaaaa, 0)\n" 3223 " .aaa(aaaaaaaaaaaaa) *\n" 3224 " aaaaaaa +\n" 3225 " aaaaaaa;", 3226 getLLVMStyleWithColumns(40)); 3227 } 3228 3229 TEST_F(FormatTest, ExpressionIndentation) { 3230 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3231 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3232 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3233 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3234 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 3235 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n" 3236 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3237 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n" 3238 " ccccccccccccccccccccccccccccccccccccccccc;"); 3239 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3240 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3241 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3242 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3243 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3244 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3245 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3246 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3247 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3248 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3249 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3250 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3251 verifyFormat("if () {\n" 3252 "} else if (aaaaa && bbbbb > // break\n" 3253 " ccccc) {\n" 3254 "}"); 3255 verifyFormat("if () {\n" 3256 "} else if (aaaaa &&\n" 3257 " bbbbb > // break\n" 3258 " ccccc &&\n" 3259 " ddddd) {\n" 3260 "}"); 3261 3262 // Presence of a trailing comment used to change indentation of b. 3263 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n" 3264 " b;\n" 3265 "return aaaaaaaaaaaaaaaaaaa +\n" 3266 " b; //", 3267 getLLVMStyleWithColumns(30)); 3268 } 3269 3270 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) { 3271 // Not sure what the best system is here. Like this, the LHS can be found 3272 // immediately above an operator (everything with the same or a higher 3273 // indent). The RHS is aligned right of the operator and so compasses 3274 // everything until something with the same indent as the operator is found. 3275 // FIXME: Is this a good system? 3276 FormatStyle Style = getLLVMStyle(); 3277 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 3278 verifyFormat( 3279 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3280 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3281 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3282 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3283 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3284 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3285 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3286 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3287 " > ccccccccccccccccccccccccccccccccccccccccc;", 3288 Style); 3289 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3290 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3291 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3292 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3293 Style); 3294 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3295 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3296 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3297 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3298 Style); 3299 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3300 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3301 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3302 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3303 Style); 3304 verifyFormat("if () {\n" 3305 "} else if (aaaaa\n" 3306 " && bbbbb // break\n" 3307 " > ccccc) {\n" 3308 "}", 3309 Style); 3310 verifyFormat("return (a)\n" 3311 " // comment\n" 3312 " + b;", 3313 Style); 3314 verifyFormat( 3315 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3316 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3317 " + cc;", 3318 Style); 3319 3320 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3321 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 3322 Style); 3323 3324 // Forced by comments. 3325 verifyFormat( 3326 "unsigned ContentSize =\n" 3327 " sizeof(int16_t) // DWARF ARange version number\n" 3328 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 3329 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 3330 " + sizeof(int8_t); // Segment Size (in bytes)"); 3331 3332 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 3333 " == boost::fusion::at_c<1>(iiii).second;", 3334 Style); 3335 3336 Style.ColumnLimit = 60; 3337 verifyFormat("zzzzzzzzzz\n" 3338 " = bbbbbbbbbbbbbbbbb\n" 3339 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 3340 Style); 3341 } 3342 3343 TEST_F(FormatTest, EnforcedOperatorWraps) { 3344 // Here we'd like to wrap after the || operators, but a comment is forcing an 3345 // earlier wrap. 3346 verifyFormat("bool x = aaaaa //\n" 3347 " || bbbbb\n" 3348 " //\n" 3349 " || cccc;"); 3350 } 3351 3352 TEST_F(FormatTest, NoOperandAlignment) { 3353 FormatStyle Style = getLLVMStyle(); 3354 Style.AlignOperands = false; 3355 verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n" 3356 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3357 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 3358 Style); 3359 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 3360 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3361 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3362 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3363 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3364 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3365 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3366 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3367 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3368 " > ccccccccccccccccccccccccccccccccccccccccc;", 3369 Style); 3370 3371 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3372 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3373 " + cc;", 3374 Style); 3375 verifyFormat("int a = aa\n" 3376 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3377 " * cccccccccccccccccccccccccccccccccccc;\n", 3378 Style); 3379 3380 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 3381 verifyFormat("return (a > b\n" 3382 " // comment1\n" 3383 " // comment2\n" 3384 " || c);", 3385 Style); 3386 } 3387 3388 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) { 3389 FormatStyle Style = getLLVMStyle(); 3390 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 3391 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 3392 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3393 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 3394 Style); 3395 } 3396 3397 TEST_F(FormatTest, AllowBinPackingInsideArguments) { 3398 FormatStyle Style = getLLVMStyle(); 3399 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 3400 Style.BinPackArguments = false; 3401 Style.ColumnLimit = 40; 3402 verifyFormat("void test() {\n" 3403 " someFunction(\n" 3404 " this + argument + is + quite\n" 3405 " + long + so + it + gets + wrapped\n" 3406 " + but + remains + bin - packed);\n" 3407 "}", 3408 Style); 3409 verifyFormat("void test() {\n" 3410 " someFunction(arg1,\n" 3411 " this + argument + is\n" 3412 " + quite + long + so\n" 3413 " + it + gets + wrapped\n" 3414 " + but + remains + bin\n" 3415 " - packed,\n" 3416 " arg3);\n" 3417 "}", 3418 Style); 3419 verifyFormat("void test() {\n" 3420 " someFunction(\n" 3421 " arg1,\n" 3422 " this + argument + has\n" 3423 " + anotherFunc(nested,\n" 3424 " calls + whose\n" 3425 " + arguments\n" 3426 " + are + also\n" 3427 " + wrapped,\n" 3428 " in + addition)\n" 3429 " + to + being + bin - packed,\n" 3430 " arg3);\n" 3431 "}", 3432 Style); 3433 3434 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 3435 verifyFormat("void test() {\n" 3436 " someFunction(\n" 3437 " arg1,\n" 3438 " this + argument + has +\n" 3439 " anotherFunc(nested,\n" 3440 " calls + whose +\n" 3441 " arguments +\n" 3442 " are + also +\n" 3443 " wrapped,\n" 3444 " in + addition) +\n" 3445 " to + being + bin - packed,\n" 3446 " arg3);\n" 3447 "}", 3448 Style); 3449 } 3450 3451 TEST_F(FormatTest, ConstructorInitializers) { 3452 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 3453 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", 3454 getLLVMStyleWithColumns(45)); 3455 verifyFormat("Constructor()\n" 3456 " : Inttializer(FitsOnTheLine) {}", 3457 getLLVMStyleWithColumns(44)); 3458 verifyFormat("Constructor()\n" 3459 " : Inttializer(FitsOnTheLine) {}", 3460 getLLVMStyleWithColumns(43)); 3461 3462 verifyFormat("template <typename T>\n" 3463 "Constructor() : Initializer(FitsOnTheLine) {}", 3464 getLLVMStyleWithColumns(45)); 3465 3466 verifyFormat( 3467 "SomeClass::Constructor()\n" 3468 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 3469 3470 verifyFormat( 3471 "SomeClass::Constructor()\n" 3472 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3473 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}"); 3474 verifyFormat( 3475 "SomeClass::Constructor()\n" 3476 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3477 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 3478 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3479 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3480 " : aaaaaaaaaa(aaaaaa) {}"); 3481 3482 verifyFormat("Constructor()\n" 3483 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3484 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3485 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3486 " aaaaaaaaaaaaaaaaaaaaaaa() {}"); 3487 3488 verifyFormat("Constructor()\n" 3489 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3490 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 3491 3492 verifyFormat("Constructor(int Parameter = 0)\n" 3493 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 3494 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}"); 3495 verifyFormat("Constructor()\n" 3496 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 3497 "}", 3498 getLLVMStyleWithColumns(60)); 3499 verifyFormat("Constructor()\n" 3500 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3501 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}"); 3502 3503 // Here a line could be saved by splitting the second initializer onto two 3504 // lines, but that is not desirable. 3505 verifyFormat("Constructor()\n" 3506 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 3507 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 3508 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 3509 3510 FormatStyle OnePerLine = getLLVMStyle(); 3511 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 3512 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false; 3513 verifyFormat("SomeClass::Constructor()\n" 3514 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3515 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3516 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3517 OnePerLine); 3518 verifyFormat("SomeClass::Constructor()\n" 3519 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 3520 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3521 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3522 OnePerLine); 3523 verifyFormat("MyClass::MyClass(int var)\n" 3524 " : some_var_(var), // 4 space indent\n" 3525 " some_other_var_(var + 1) { // lined up\n" 3526 "}", 3527 OnePerLine); 3528 verifyFormat("Constructor()\n" 3529 " : aaaaa(aaaaaa),\n" 3530 " aaaaa(aaaaaa),\n" 3531 " aaaaa(aaaaaa),\n" 3532 " aaaaa(aaaaaa),\n" 3533 " aaaaa(aaaaaa) {}", 3534 OnePerLine); 3535 verifyFormat("Constructor()\n" 3536 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 3537 " aaaaaaaaaaaaaaaaaaaaaa) {}", 3538 OnePerLine); 3539 OnePerLine.BinPackParameters = false; 3540 verifyFormat( 3541 "Constructor()\n" 3542 " : aaaaaaaaaaaaaaaaaaaaaaaa(\n" 3543 " aaaaaaaaaaa().aaa(),\n" 3544 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3545 OnePerLine); 3546 OnePerLine.ColumnLimit = 60; 3547 verifyFormat("Constructor()\n" 3548 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 3549 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 3550 OnePerLine); 3551 3552 EXPECT_EQ("Constructor()\n" 3553 " : // Comment forcing unwanted break.\n" 3554 " aaaa(aaaa) {}", 3555 format("Constructor() :\n" 3556 " // Comment forcing unwanted break.\n" 3557 " aaaa(aaaa) {}")); 3558 } 3559 3560 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) { 3561 FormatStyle Style = getLLVMStyle(); 3562 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 3563 3564 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 3565 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}", 3566 getStyleWithColumns(Style, 45)); 3567 verifyFormat("Constructor() :\n" 3568 " Initializer(FitsOnTheLine) {}", 3569 getStyleWithColumns(Style, 44)); 3570 verifyFormat("Constructor() :\n" 3571 " Initializer(FitsOnTheLine) {}", 3572 getStyleWithColumns(Style, 43)); 3573 3574 verifyFormat("template <typename T>\n" 3575 "Constructor() : Initializer(FitsOnTheLine) {}", 3576 getStyleWithColumns(Style, 50)); 3577 3578 verifyFormat( 3579 "SomeClass::Constructor() :\n" 3580 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 3581 Style); 3582 3583 verifyFormat( 3584 "SomeClass::Constructor() :\n" 3585 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3586 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3587 Style); 3588 verifyFormat( 3589 "SomeClass::Constructor() :\n" 3590 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3591 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 3592 Style); 3593 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3594 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 3595 " aaaaaaaaaa(aaaaaa) {}", 3596 Style); 3597 3598 verifyFormat("Constructor() :\n" 3599 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3600 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3601 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3602 " aaaaaaaaaaaaaaaaaaaaaaa() {}", 3603 Style); 3604 3605 verifyFormat("Constructor() :\n" 3606 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3607 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3608 Style); 3609 3610 verifyFormat("Constructor(int Parameter = 0) :\n" 3611 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 3612 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}", 3613 Style); 3614 verifyFormat("Constructor() :\n" 3615 " aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 3616 "}", 3617 getStyleWithColumns(Style, 60)); 3618 verifyFormat("Constructor() :\n" 3619 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3620 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}", 3621 Style); 3622 3623 // Here a line could be saved by splitting the second initializer onto two 3624 // lines, but that is not desirable. 3625 verifyFormat("Constructor() :\n" 3626 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 3627 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 3628 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3629 Style); 3630 3631 FormatStyle OnePerLine = Style; 3632 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 3633 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false; 3634 verifyFormat("SomeClass::Constructor() :\n" 3635 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3636 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3637 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3638 OnePerLine); 3639 verifyFormat("SomeClass::Constructor() :\n" 3640 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 3641 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3642 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3643 OnePerLine); 3644 verifyFormat("MyClass::MyClass(int var) :\n" 3645 " some_var_(var), // 4 space indent\n" 3646 " some_other_var_(var + 1) { // lined up\n" 3647 "}", 3648 OnePerLine); 3649 verifyFormat("Constructor() :\n" 3650 " aaaaa(aaaaaa),\n" 3651 " aaaaa(aaaaaa),\n" 3652 " aaaaa(aaaaaa),\n" 3653 " aaaaa(aaaaaa),\n" 3654 " aaaaa(aaaaaa) {}", 3655 OnePerLine); 3656 verifyFormat("Constructor() :\n" 3657 " aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 3658 " aaaaaaaaaaaaaaaaaaaaaa) {}", 3659 OnePerLine); 3660 OnePerLine.BinPackParameters = false; 3661 verifyFormat( 3662 "Constructor() :\n" 3663 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 3664 " aaaaaaaaaaa().aaa(),\n" 3665 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3666 OnePerLine); 3667 OnePerLine.ColumnLimit = 60; 3668 verifyFormat("Constructor() :\n" 3669 " aaaaaaaaaaaaaaaaaaaa(a),\n" 3670 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 3671 OnePerLine); 3672 3673 EXPECT_EQ("Constructor() :\n" 3674 " // Comment forcing unwanted break.\n" 3675 " aaaa(aaaa) {}", 3676 format("Constructor() :\n" 3677 " // Comment forcing unwanted break.\n" 3678 " aaaa(aaaa) {}", 3679 Style)); 3680 3681 Style.ColumnLimit = 0; 3682 verifyFormat("SomeClass::Constructor() :\n" 3683 " a(a) {}", 3684 Style); 3685 verifyFormat("SomeClass::Constructor() noexcept :\n" 3686 " a(a) {}", 3687 Style); 3688 verifyFormat("SomeClass::Constructor() :\n" 3689 " a(a), b(b), c(c) {}", 3690 Style); 3691 verifyFormat("SomeClass::Constructor() :\n" 3692 " a(a) {\n" 3693 " foo();\n" 3694 " bar();\n" 3695 "}", 3696 Style); 3697 3698 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 3699 verifyFormat("SomeClass::Constructor() :\n" 3700 " a(a), b(b), c(c) {\n" 3701 "}", 3702 Style); 3703 verifyFormat("SomeClass::Constructor() :\n" 3704 " a(a) {\n" 3705 "}", 3706 Style); 3707 3708 Style.ColumnLimit = 80; 3709 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 3710 Style.ConstructorInitializerIndentWidth = 2; 3711 verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", 3712 Style); 3713 verifyFormat("SomeClass::Constructor() :\n" 3714 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3715 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}", 3716 Style); 3717 } 3718 3719 #ifndef EXPENSIVE_CHECKS 3720 // Expensive checks enables libstdc++ checking which includes validating the 3721 // state of ranges used in std::priority_queue - this blows out the 3722 // runtime/scalability of the function and makes this test unacceptably slow. 3723 TEST_F(FormatTest, MemoizationTests) { 3724 // This breaks if the memoization lookup does not take \c Indent and 3725 // \c LastSpace into account. 3726 verifyFormat( 3727 "extern CFRunLoopTimerRef\n" 3728 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n" 3729 " CFTimeInterval interval, CFOptionFlags flags,\n" 3730 " CFIndex order, CFRunLoopTimerCallBack callout,\n" 3731 " CFRunLoopTimerContext *context) {}"); 3732 3733 // Deep nesting somewhat works around our memoization. 3734 verifyFormat( 3735 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3736 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3737 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3738 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3739 " aaaaa())))))))))))))))))))))))))))))))))))))));", 3740 getLLVMStyleWithColumns(65)); 3741 verifyFormat( 3742 "aaaaa(\n" 3743 " aaaaa,\n" 3744 " aaaaa(\n" 3745 " aaaaa,\n" 3746 " aaaaa(\n" 3747 " aaaaa,\n" 3748 " aaaaa(\n" 3749 " aaaaa,\n" 3750 " aaaaa(\n" 3751 " aaaaa,\n" 3752 " aaaaa(\n" 3753 " aaaaa,\n" 3754 " aaaaa(\n" 3755 " aaaaa,\n" 3756 " aaaaa(\n" 3757 " aaaaa,\n" 3758 " aaaaa(\n" 3759 " aaaaa,\n" 3760 " aaaaa(\n" 3761 " aaaaa,\n" 3762 " aaaaa(\n" 3763 " aaaaa,\n" 3764 " aaaaa(\n" 3765 " aaaaa,\n" 3766 " aaaaa))))))))))));", 3767 getLLVMStyleWithColumns(65)); 3768 verifyFormat( 3769 "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" 3770 " a),\n" 3771 " a),\n" 3772 " a),\n" 3773 " a),\n" 3774 " a),\n" 3775 " a),\n" 3776 " a),\n" 3777 " a),\n" 3778 " a),\n" 3779 " a),\n" 3780 " a),\n" 3781 " a),\n" 3782 " a),\n" 3783 " a),\n" 3784 " a),\n" 3785 " a),\n" 3786 " a)", 3787 getLLVMStyleWithColumns(65)); 3788 3789 // This test takes VERY long when memoization is broken. 3790 FormatStyle OnePerLine = getLLVMStyle(); 3791 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 3792 OnePerLine.BinPackParameters = false; 3793 std::string input = "Constructor()\n" 3794 " : aaaa(a,\n"; 3795 for (unsigned i = 0, e = 80; i != e; ++i) { 3796 input += " a,\n"; 3797 } 3798 input += " a) {}"; 3799 verifyFormat(input, OnePerLine); 3800 } 3801 #endif 3802 3803 TEST_F(FormatTest, BreaksAsHighAsPossible) { 3804 verifyFormat( 3805 "void f() {\n" 3806 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" 3807 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" 3808 " f();\n" 3809 "}"); 3810 verifyFormat("if (Intervals[i].getRange().getFirst() <\n" 3811 " Intervals[i - 1].getRange().getLast()) {\n}"); 3812 } 3813 3814 TEST_F(FormatTest, BreaksFunctionDeclarations) { 3815 // Principially, we break function declarations in a certain order: 3816 // 1) break amongst arguments. 3817 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n" 3818 " Cccccccccccccc cccccccccccccc);"); 3819 verifyFormat("template <class TemplateIt>\n" 3820 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n" 3821 " TemplateIt *stop) {}"); 3822 3823 // 2) break after return type. 3824 verifyFormat( 3825 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3826 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);", 3827 getGoogleStyle()); 3828 3829 // 3) break after (. 3830 verifyFormat( 3831 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n" 3832 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);", 3833 getGoogleStyle()); 3834 3835 // 4) break before after nested name specifiers. 3836 verifyFormat( 3837 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3838 "SomeClasssssssssssssssssssssssssssssssssssssss::\n" 3839 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);", 3840 getGoogleStyle()); 3841 3842 // However, there are exceptions, if a sufficient amount of lines can be 3843 // saved. 3844 // FIXME: The precise cut-offs wrt. the number of saved lines might need some 3845 // more adjusting. 3846 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 3847 " Cccccccccccccc cccccccccc,\n" 3848 " Cccccccccccccc cccccccccc,\n" 3849 " Cccccccccccccc cccccccccc,\n" 3850 " Cccccccccccccc cccccccccc);"); 3851 verifyFormat( 3852 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3853 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3854 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3855 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);", 3856 getGoogleStyle()); 3857 verifyFormat( 3858 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 3859 " Cccccccccccccc cccccccccc,\n" 3860 " Cccccccccccccc cccccccccc,\n" 3861 " Cccccccccccccc cccccccccc,\n" 3862 " Cccccccccccccc cccccccccc,\n" 3863 " Cccccccccccccc cccccccccc,\n" 3864 " Cccccccccccccc cccccccccc);"); 3865 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 3866 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3867 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3868 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3869 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); 3870 3871 // Break after multi-line parameters. 3872 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3873 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3874 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3875 " bbbb bbbb);"); 3876 verifyFormat("void SomeLoooooooooooongFunction(\n" 3877 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 3878 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3879 " int bbbbbbbbbbbbb);"); 3880 3881 // Treat overloaded operators like other functions. 3882 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3883 "operator>(const SomeLoooooooooooooooooooooooooogType &other);"); 3884 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3885 "operator>>(const SomeLooooooooooooooooooooooooogType &other);"); 3886 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3887 "operator<<(const SomeLooooooooooooooooooooooooogType &other);"); 3888 verifyGoogleFormat( 3889 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n" 3890 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 3891 verifyGoogleFormat( 3892 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n" 3893 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 3894 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3895 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 3896 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n" 3897 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 3898 verifyGoogleFormat( 3899 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n" 3900 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3901 " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}"); 3902 verifyGoogleFormat( 3903 "template <typename T>\n" 3904 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3905 "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n" 3906 " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);"); 3907 3908 FormatStyle Style = getLLVMStyle(); 3909 Style.PointerAlignment = FormatStyle::PAS_Left; 3910 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3911 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}", 3912 Style); 3913 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n" 3914 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3915 Style); 3916 } 3917 3918 TEST_F(FormatTest, TrailingReturnType) { 3919 verifyFormat("auto foo() -> int;\n"); 3920 verifyFormat("struct S {\n" 3921 " auto bar() const -> int;\n" 3922 "};"); 3923 verifyFormat("template <size_t Order, typename T>\n" 3924 "auto load_img(const std::string &filename)\n" 3925 " -> alias::tensor<Order, T, mem::tag::cpu> {}"); 3926 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n" 3927 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}"); 3928 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}"); 3929 verifyFormat("template <typename T>\n" 3930 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n" 3931 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());"); 3932 3933 // Not trailing return types. 3934 verifyFormat("void f() { auto a = b->c(); }"); 3935 } 3936 3937 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { 3938 // Avoid breaking before trailing 'const' or other trailing annotations, if 3939 // they are not function-like. 3940 FormatStyle Style = getGoogleStyle(); 3941 Style.ColumnLimit = 47; 3942 verifyFormat("void someLongFunction(\n" 3943 " int someLoooooooooooooongParameter) const {\n}", 3944 getLLVMStyleWithColumns(47)); 3945 verifyFormat("LoooooongReturnType\n" 3946 "someLoooooooongFunction() const {}", 3947 getLLVMStyleWithColumns(47)); 3948 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n" 3949 " const {}", 3950 Style); 3951 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3952 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;"); 3953 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3954 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;"); 3955 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3956 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;"); 3957 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n" 3958 " aaaaaaaaaaa aaaaa) const override;"); 3959 verifyGoogleFormat( 3960 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 3961 " const override;"); 3962 3963 // Even if the first parameter has to be wrapped. 3964 verifyFormat("void someLongFunction(\n" 3965 " int someLongParameter) const {}", 3966 getLLVMStyleWithColumns(46)); 3967 verifyFormat("void someLongFunction(\n" 3968 " int someLongParameter) const {}", 3969 Style); 3970 verifyFormat("void someLongFunction(\n" 3971 " int someLongParameter) override {}", 3972 Style); 3973 verifyFormat("void someLongFunction(\n" 3974 " int someLongParameter) OVERRIDE {}", 3975 Style); 3976 verifyFormat("void someLongFunction(\n" 3977 " int someLongParameter) final {}", 3978 Style); 3979 verifyFormat("void someLongFunction(\n" 3980 " int someLongParameter) FINAL {}", 3981 Style); 3982 verifyFormat("void someLongFunction(\n" 3983 " int parameter) const override {}", 3984 Style); 3985 3986 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 3987 verifyFormat("void someLongFunction(\n" 3988 " int someLongParameter) const\n" 3989 "{\n" 3990 "}", 3991 Style); 3992 3993 // Unless these are unknown annotations. 3994 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n" 3995 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3996 " LONG_AND_UGLY_ANNOTATION;"); 3997 3998 // Breaking before function-like trailing annotations is fine to keep them 3999 // close to their arguments. 4000 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4001 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 4002 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 4003 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 4004 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 4005 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}"); 4006 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n" 4007 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);"); 4008 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});"); 4009 4010 verifyFormat( 4011 "void aaaaaaaaaaaaaaaaaa()\n" 4012 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n" 4013 " aaaaaaaaaaaaaaaaaaaaaaaaa));"); 4014 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4015 " __attribute__((unused));"); 4016 verifyGoogleFormat( 4017 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4018 " GUARDED_BY(aaaaaaaaaaaa);"); 4019 verifyGoogleFormat( 4020 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4021 " GUARDED_BY(aaaaaaaaaaaa);"); 4022 verifyGoogleFormat( 4023 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 4024 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4025 verifyGoogleFormat( 4026 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 4027 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4028 } 4029 4030 TEST_F(FormatTest, FunctionAnnotations) { 4031 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 4032 "int OldFunction(const string ¶meter) {}"); 4033 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 4034 "string OldFunction(const string ¶meter) {}"); 4035 verifyFormat("template <typename T>\n" 4036 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 4037 "string OldFunction(const string ¶meter) {}"); 4038 4039 // Not function annotations. 4040 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4041 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); 4042 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n" 4043 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}"); 4044 verifyFormat("MACRO(abc).function() // wrap\n" 4045 " << abc;"); 4046 verifyFormat("MACRO(abc)->function() // wrap\n" 4047 " << abc;"); 4048 verifyFormat("MACRO(abc)::function() // wrap\n" 4049 " << abc;"); 4050 } 4051 4052 TEST_F(FormatTest, BreaksDesireably) { 4053 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 4054 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 4055 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); 4056 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4057 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 4058 "}"); 4059 4060 verifyFormat( 4061 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4062 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 4063 4064 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4065 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4066 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4067 4068 verifyFormat( 4069 "aaaaaaaa(aaaaaaaaaaaaa,\n" 4070 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4071 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 4072 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4073 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 4074 4075 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4076 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4077 4078 verifyFormat( 4079 "void f() {\n" 4080 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 4081 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 4082 "}"); 4083 verifyFormat( 4084 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4085 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4086 verifyFormat( 4087 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4088 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4089 verifyFormat( 4090 "aaaaaa(aaa,\n" 4091 " new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4092 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4093 " aaaa);"); 4094 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4095 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4096 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4097 4098 // Indent consistently independent of call expression and unary operator. 4099 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 4100 " dddddddddddddddddddddddddddddd));"); 4101 verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 4102 " dddddddddddddddddddddddddddddd));"); 4103 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n" 4104 " dddddddddddddddddddddddddddddd));"); 4105 4106 // This test case breaks on an incorrect memoization, i.e. an optimization not 4107 // taking into account the StopAt value. 4108 verifyFormat( 4109 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4110 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4111 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4112 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4113 4114 verifyFormat("{\n {\n {\n" 4115 " Annotation.SpaceRequiredBefore =\n" 4116 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" 4117 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" 4118 " }\n }\n}"); 4119 4120 // Break on an outer level if there was a break on an inner level. 4121 EXPECT_EQ("f(g(h(a, // comment\n" 4122 " b, c),\n" 4123 " d, e),\n" 4124 " x, y);", 4125 format("f(g(h(a, // comment\n" 4126 " b, c), d, e), x, y);")); 4127 4128 // Prefer breaking similar line breaks. 4129 verifyFormat( 4130 "const int kTrackingOptions = NSTrackingMouseMoved |\n" 4131 " NSTrackingMouseEnteredAndExited |\n" 4132 " NSTrackingActiveAlways;"); 4133 } 4134 4135 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) { 4136 FormatStyle NoBinPacking = getGoogleStyle(); 4137 NoBinPacking.BinPackParameters = false; 4138 NoBinPacking.BinPackArguments = true; 4139 verifyFormat("void f() {\n" 4140 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n" 4141 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 4142 "}", 4143 NoBinPacking); 4144 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n" 4145 " int aaaaaaaaaaaaaaaaaaaa,\n" 4146 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4147 NoBinPacking); 4148 4149 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 4150 verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4151 " vector<int> bbbbbbbbbbbbbbb);", 4152 NoBinPacking); 4153 // FIXME: This behavior difference is probably not wanted. However, currently 4154 // we cannot distinguish BreakBeforeParameter being set because of the wrapped 4155 // template arguments from BreakBeforeParameter being set because of the 4156 // one-per-line formatting. 4157 verifyFormat( 4158 "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n" 4159 " aaaaaaaaaa> aaaaaaaaaa);", 4160 NoBinPacking); 4161 verifyFormat( 4162 "void fffffffffff(\n" 4163 " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n" 4164 " aaaaaaaaaa);"); 4165 } 4166 4167 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { 4168 FormatStyle NoBinPacking = getGoogleStyle(); 4169 NoBinPacking.BinPackParameters = false; 4170 NoBinPacking.BinPackArguments = false; 4171 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" 4172 " aaaaaaaaaaaaaaaaaaaa,\n" 4173 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);", 4174 NoBinPacking); 4175 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n" 4176 " aaaaaaaaaaaaa,\n" 4177 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));", 4178 NoBinPacking); 4179 verifyFormat( 4180 "aaaaaaaa(aaaaaaaaaaaaa,\n" 4181 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4182 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 4183 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4184 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));", 4185 NoBinPacking); 4186 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 4187 " .aaaaaaaaaaaaaaaaaa();", 4188 NoBinPacking); 4189 verifyFormat("void f() {\n" 4190 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4191 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n" 4192 "}", 4193 NoBinPacking); 4194 4195 verifyFormat( 4196 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4197 " aaaaaaaaaaaa,\n" 4198 " aaaaaaaaaaaa);", 4199 NoBinPacking); 4200 verifyFormat( 4201 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" 4202 " ddddddddddddddddddddddddddddd),\n" 4203 " test);", 4204 NoBinPacking); 4205 4206 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" 4207 " aaaaaaaaaaaaaaaaaaaaaaa,\n" 4208 " aaaaaaaaaaaaaaaaaaaaaaa>\n" 4209 " aaaaaaaaaaaaaaaaaa;", 4210 NoBinPacking); 4211 verifyFormat("a(\"a\"\n" 4212 " \"a\",\n" 4213 " a);"); 4214 4215 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 4216 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" 4217 " aaaaaaaaa,\n" 4218 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4219 NoBinPacking); 4220 verifyFormat( 4221 "void f() {\n" 4222 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 4223 " .aaaaaaa();\n" 4224 "}", 4225 NoBinPacking); 4226 verifyFormat( 4227 "template <class SomeType, class SomeOtherType>\n" 4228 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}", 4229 NoBinPacking); 4230 } 4231 4232 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) { 4233 FormatStyle Style = getLLVMStyleWithColumns(15); 4234 Style.ExperimentalAutoDetectBinPacking = true; 4235 EXPECT_EQ("aaa(aaaa,\n" 4236 " aaaa,\n" 4237 " aaaa);\n" 4238 "aaa(aaaa,\n" 4239 " aaaa,\n" 4240 " aaaa);", 4241 format("aaa(aaaa,\n" // one-per-line 4242 " aaaa,\n" 4243 " aaaa );\n" 4244 "aaa(aaaa, aaaa, aaaa);", // inconclusive 4245 Style)); 4246 EXPECT_EQ("aaa(aaaa, aaaa,\n" 4247 " aaaa);\n" 4248 "aaa(aaaa, aaaa,\n" 4249 " aaaa);", 4250 format("aaa(aaaa, aaaa,\n" // bin-packed 4251 " aaaa );\n" 4252 "aaa(aaaa, aaaa, aaaa);", // inconclusive 4253 Style)); 4254 } 4255 4256 TEST_F(FormatTest, FormatsBuilderPattern) { 4257 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n" 4258 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" 4259 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n" 4260 " .StartsWith(\".init\", ORDER_INIT)\n" 4261 " .StartsWith(\".fini\", ORDER_FINI)\n" 4262 " .StartsWith(\".hash\", ORDER_HASH)\n" 4263 " .Default(ORDER_TEXT);\n"); 4264 4265 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" 4266 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); 4267 verifyFormat( 4268 "aaaaaaa->aaaaaaa\n" 4269 " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4270 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4271 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 4272 verifyFormat( 4273 "aaaaaaa->aaaaaaa\n" 4274 " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4275 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 4276 verifyFormat( 4277 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" 4278 " aaaaaaaaaaaaaa);"); 4279 verifyFormat( 4280 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n" 4281 " aaaaaa->aaaaaaaaaaaa()\n" 4282 " ->aaaaaaaaaaaaaaaa(\n" 4283 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4284 " ->aaaaaaaaaaaaaaaaa();"); 4285 verifyGoogleFormat( 4286 "void f() {\n" 4287 " someo->Add((new util::filetools::Handler(dir))\n" 4288 " ->OnEvent1(NewPermanentCallback(\n" 4289 " this, &HandlerHolderClass::EventHandlerCBA))\n" 4290 " ->OnEvent2(NewPermanentCallback(\n" 4291 " this, &HandlerHolderClass::EventHandlerCBB))\n" 4292 " ->OnEvent3(NewPermanentCallback(\n" 4293 " this, &HandlerHolderClass::EventHandlerCBC))\n" 4294 " ->OnEvent5(NewPermanentCallback(\n" 4295 " this, &HandlerHolderClass::EventHandlerCBD))\n" 4296 " ->OnEvent6(NewPermanentCallback(\n" 4297 " this, &HandlerHolderClass::EventHandlerCBE)));\n" 4298 "}"); 4299 4300 verifyFormat( 4301 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();"); 4302 verifyFormat("aaaaaaaaaaaaaaa()\n" 4303 " .aaaaaaaaaaaaaaa()\n" 4304 " .aaaaaaaaaaaaaaa()\n" 4305 " .aaaaaaaaaaaaaaa()\n" 4306 " .aaaaaaaaaaaaaaa();"); 4307 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4308 " .aaaaaaaaaaaaaaa()\n" 4309 " .aaaaaaaaaaaaaaa()\n" 4310 " .aaaaaaaaaaaaaaa();"); 4311 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4312 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4313 " .aaaaaaaaaaaaaaa();"); 4314 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n" 4315 " ->aaaaaaaaaaaaaae(0)\n" 4316 " ->aaaaaaaaaaaaaaa();"); 4317 4318 // Don't linewrap after very short segments. 4319 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4320 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4321 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4322 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4323 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4324 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4325 verifyFormat("aaa()\n" 4326 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4327 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4328 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4329 4330 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 4331 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4332 " .has<bbbbbbbbbbbbbbbbbbbbb>();"); 4333 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 4334 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 4335 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();"); 4336 4337 // Prefer not to break after empty parentheses. 4338 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n" 4339 " First->LastNewlineOffset);"); 4340 4341 // Prefer not to create "hanging" indents. 4342 verifyFormat( 4343 "return !soooooooooooooome_map\n" 4344 " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4345 " .second;"); 4346 verifyFormat( 4347 "return aaaaaaaaaaaaaaaa\n" 4348 " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n" 4349 " .aaaa(aaaaaaaaaaaaaa);"); 4350 // No hanging indent here. 4351 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n" 4352 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4353 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n" 4354 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4355 verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 4356 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4357 getLLVMStyleWithColumns(60)); 4358 verifyFormat("aaaaaaaaaaaaaaaaaa\n" 4359 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 4360 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4361 getLLVMStyleWithColumns(59)); 4362 verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4363 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4364 " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4365 } 4366 4367 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { 4368 verifyFormat( 4369 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4370 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); 4371 verifyFormat( 4372 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n" 4373 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}"); 4374 4375 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 4376 " ccccccccccccccccccccccccc) {\n}"); 4377 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n" 4378 " ccccccccccccccccccccccccc) {\n}"); 4379 4380 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 4381 " ccccccccccccccccccccccccc) {\n}"); 4382 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n" 4383 " ccccccccccccccccccccccccc) {\n}"); 4384 4385 verifyFormat( 4386 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" 4387 " ccccccccccccccccccccccccc) {\n}"); 4388 verifyFormat( 4389 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n" 4390 " ccccccccccccccccccccccccc) {\n}"); 4391 4392 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" 4393 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" 4394 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" 4395 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 4396 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n" 4397 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n" 4398 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n" 4399 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 4400 4401 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" 4402 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" 4403 " aaaaaaaaaaaaaaa != aa) {\n}"); 4404 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n" 4405 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n" 4406 " aaaaaaaaaaaaaaa != aa) {\n}"); 4407 } 4408 4409 TEST_F(FormatTest, BreaksAfterAssignments) { 4410 verifyFormat( 4411 "unsigned Cost =\n" 4412 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" 4413 " SI->getPointerAddressSpaceee());\n"); 4414 verifyFormat( 4415 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" 4416 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); 4417 4418 verifyFormat( 4419 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n" 4420 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); 4421 verifyFormat("unsigned OriginalStartColumn =\n" 4422 " SourceMgr.getSpellingColumnNumber(\n" 4423 " Current.FormatTok.getStartOfNonWhitespace()) -\n" 4424 " 1;"); 4425 } 4426 4427 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) { 4428 FormatStyle Style = getLLVMStyle(); 4429 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 4430 " bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;", 4431 Style); 4432 4433 Style.PenaltyBreakAssignment = 20; 4434 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 4435 " cccccccccccccccccccccccccc;", 4436 Style); 4437 } 4438 4439 TEST_F(FormatTest, AlignsAfterAssignments) { 4440 verifyFormat( 4441 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4442 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4443 verifyFormat( 4444 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4445 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4446 verifyFormat( 4447 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4448 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4449 verifyFormat( 4450 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4451 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 4452 verifyFormat( 4453 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" 4454 " aaaaaaaaaaaaaaaaaaaaaaaa +\n" 4455 " aaaaaaaaaaaaaaaaaaaaaaaa;"); 4456 } 4457 4458 TEST_F(FormatTest, AlignsAfterReturn) { 4459 verifyFormat( 4460 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4461 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4462 verifyFormat( 4463 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4464 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 4465 verifyFormat( 4466 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 4467 " aaaaaaaaaaaaaaaaaaaaaa();"); 4468 verifyFormat( 4469 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 4470 " aaaaaaaaaaaaaaaaaaaaaa());"); 4471 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4472 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4473 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4474 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n" 4475 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4476 verifyFormat("return\n" 4477 " // true if code is one of a or b.\n" 4478 " code == a || code == b;"); 4479 } 4480 4481 TEST_F(FormatTest, AlignsAfterOpenBracket) { 4482 verifyFormat( 4483 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 4484 " aaaaaaaaa aaaaaaa) {}"); 4485 verifyFormat( 4486 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 4487 " aaaaaaaaaaa aaaaaaaaa);"); 4488 verifyFormat( 4489 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 4490 " aaaaaaaaaaaaaaaaaaaaa));"); 4491 FormatStyle Style = getLLVMStyle(); 4492 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4493 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4494 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}", 4495 Style); 4496 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 4497 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);", 4498 Style); 4499 verifyFormat("SomeLongVariableName->someFunction(\n" 4500 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));", 4501 Style); 4502 verifyFormat( 4503 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 4504 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4505 Style); 4506 verifyFormat( 4507 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 4508 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4509 Style); 4510 verifyFormat( 4511 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 4512 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 4513 Style); 4514 4515 verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n" 4516 " ccccccc(aaaaaaaaaaaaaaaaa, //\n" 4517 " b));", 4518 Style); 4519 4520 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 4521 Style.BinPackArguments = false; 4522 Style.BinPackParameters = false; 4523 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4524 " aaaaaaaaaaa aaaaaaaa,\n" 4525 " aaaaaaaaa aaaaaaa,\n" 4526 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4527 Style); 4528 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 4529 " aaaaaaaaaaa aaaaaaaaa,\n" 4530 " aaaaaaaaaaa aaaaaaaaa,\n" 4531 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4532 Style); 4533 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n" 4534 " aaaaaaaaaaaaaaa,\n" 4535 " aaaaaaaaaaaaaaaaaaaaa,\n" 4536 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 4537 Style); 4538 verifyFormat( 4539 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n" 4540 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 4541 Style); 4542 verifyFormat( 4543 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n" 4544 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 4545 Style); 4546 verifyFormat( 4547 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 4548 " aaaaaaaaaaaaaaaaaaaaa(\n" 4549 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n" 4550 " aaaaaaaaaaaaaaaa);", 4551 Style); 4552 verifyFormat( 4553 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 4554 " aaaaaaaaaaaaaaaaaaaaa(\n" 4555 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n" 4556 " aaaaaaaaaaaaaaaa);", 4557 Style); 4558 } 4559 4560 TEST_F(FormatTest, ParenthesesAndOperandAlignment) { 4561 FormatStyle Style = getLLVMStyleWithColumns(40); 4562 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4563 " bbbbbbbbbbbbbbbbbbbbbb);", 4564 Style); 4565 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 4566 Style.AlignOperands = false; 4567 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4568 " bbbbbbbbbbbbbbbbbbbbbb);", 4569 Style); 4570 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4571 Style.AlignOperands = true; 4572 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4573 " bbbbbbbbbbbbbbbbbbbbbb);", 4574 Style); 4575 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4576 Style.AlignOperands = false; 4577 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4578 " bbbbbbbbbbbbbbbbbbbbbb);", 4579 Style); 4580 } 4581 4582 TEST_F(FormatTest, BreaksConditionalExpressions) { 4583 verifyFormat( 4584 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4585 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4586 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4587 verifyFormat( 4588 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 4589 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4590 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4591 verifyFormat( 4592 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4593 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4594 verifyFormat( 4595 "aaaa(aaaaaaaaa, aaaaaaaaa,\n" 4596 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4597 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4598 verifyFormat( 4599 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" 4600 " : aaaaaaaaaaaaa);"); 4601 verifyFormat( 4602 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4603 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4604 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4605 " aaaaaaaaaaaaa);"); 4606 verifyFormat( 4607 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4608 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4609 " aaaaaaaaaaaaa);"); 4610 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4611 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4612 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4613 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4614 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4615 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4616 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4617 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4618 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4619 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4620 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4621 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4622 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4623 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4624 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4625 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4626 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4627 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4628 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4629 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4630 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 4631 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4632 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4633 " : aaaaaaaaaaaaaaaa;"); 4634 verifyFormat( 4635 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4636 " ? aaaaaaaaaaaaaaa\n" 4637 " : aaaaaaaaaaaaaaa;"); 4638 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 4639 " aaaaaaaaa\n" 4640 " ? b\n" 4641 " : c);"); 4642 verifyFormat("return aaaa == bbbb\n" 4643 " // comment\n" 4644 " ? aaaa\n" 4645 " : bbbb;"); 4646 verifyFormat("unsigned Indent =\n" 4647 " format(TheLine.First,\n" 4648 " IndentForLevel[TheLine.Level] >= 0\n" 4649 " ? IndentForLevel[TheLine.Level]\n" 4650 " : TheLine * 2,\n" 4651 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 4652 getLLVMStyleWithColumns(60)); 4653 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 4654 " ? aaaaaaaaaaaaaaa\n" 4655 " : bbbbbbbbbbbbbbb //\n" 4656 " ? ccccccccccccccc\n" 4657 " : ddddddddddddddd;"); 4658 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 4659 " ? aaaaaaaaaaaaaaa\n" 4660 " : (bbbbbbbbbbbbbbb //\n" 4661 " ? ccccccccccccccc\n" 4662 " : ddddddddddddddd);"); 4663 verifyFormat( 4664 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4665 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4666 " aaaaaaaaaaaaaaaaaaaaa +\n" 4667 " aaaaaaaaaaaaaaaaaaaaa\n" 4668 " : aaaaaaaaaa;"); 4669 verifyFormat( 4670 "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4671 " : aaaaaaaaaaaaaaaaaaaaaa\n" 4672 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4673 4674 FormatStyle NoBinPacking = getLLVMStyle(); 4675 NoBinPacking.BinPackArguments = false; 4676 verifyFormat( 4677 "void f() {\n" 4678 " g(aaa,\n" 4679 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 4680 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4681 " ? aaaaaaaaaaaaaaa\n" 4682 " : aaaaaaaaaaaaaaa);\n" 4683 "}", 4684 NoBinPacking); 4685 verifyFormat( 4686 "void f() {\n" 4687 " g(aaa,\n" 4688 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 4689 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4690 " ?: aaaaaaaaaaaaaaa);\n" 4691 "}", 4692 NoBinPacking); 4693 4694 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n" 4695 " // comment.\n" 4696 " ccccccccccccccccccccccccccccccccccccccc\n" 4697 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4698 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);"); 4699 4700 // Assignments in conditional expressions. Apparently not uncommon :-(. 4701 verifyFormat("return a != b\n" 4702 " // comment\n" 4703 " ? a = b\n" 4704 " : a = b;"); 4705 verifyFormat("return a != b\n" 4706 " // comment\n" 4707 " ? a = a != b\n" 4708 " // comment\n" 4709 " ? a = b\n" 4710 " : a\n" 4711 " : a;\n"); 4712 verifyFormat("return a != b\n" 4713 " // comment\n" 4714 " ? a\n" 4715 " : a = a != b\n" 4716 " // comment\n" 4717 " ? a = b\n" 4718 " : a;"); 4719 } 4720 4721 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) { 4722 FormatStyle Style = getLLVMStyle(); 4723 Style.BreakBeforeTernaryOperators = false; 4724 Style.ColumnLimit = 70; 4725 verifyFormat( 4726 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4727 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4728 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4729 Style); 4730 verifyFormat( 4731 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 4732 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4733 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4734 Style); 4735 verifyFormat( 4736 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4737 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4738 Style); 4739 verifyFormat( 4740 "aaaa(aaaaaaaa, aaaaaaaaaa,\n" 4741 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4742 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4743 Style); 4744 verifyFormat( 4745 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n" 4746 " aaaaaaaaaaaaa);", 4747 Style); 4748 verifyFormat( 4749 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4750 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4751 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4752 " aaaaaaaaaaaaa);", 4753 Style); 4754 verifyFormat( 4755 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4756 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4757 " aaaaaaaaaaaaa);", 4758 Style); 4759 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4760 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4761 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 4762 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4763 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4764 Style); 4765 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4766 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4767 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4768 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 4769 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4770 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4771 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4772 Style); 4773 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4774 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n" 4775 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4776 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4777 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4778 Style); 4779 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4780 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4781 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4782 Style); 4783 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 4784 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4785 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4786 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4787 Style); 4788 verifyFormat( 4789 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4790 " aaaaaaaaaaaaaaa :\n" 4791 " aaaaaaaaaaaaaaa;", 4792 Style); 4793 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 4794 " aaaaaaaaa ?\n" 4795 " b :\n" 4796 " c);", 4797 Style); 4798 verifyFormat("unsigned Indent =\n" 4799 " format(TheLine.First,\n" 4800 " IndentForLevel[TheLine.Level] >= 0 ?\n" 4801 " IndentForLevel[TheLine.Level] :\n" 4802 " TheLine * 2,\n" 4803 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 4804 Style); 4805 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 4806 " aaaaaaaaaaaaaaa :\n" 4807 " bbbbbbbbbbbbbbb ? //\n" 4808 " ccccccccccccccc :\n" 4809 " ddddddddddddddd;", 4810 Style); 4811 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 4812 " aaaaaaaaaaaaaaa :\n" 4813 " (bbbbbbbbbbbbbbb ? //\n" 4814 " ccccccccccccccc :\n" 4815 " ddddddddddddddd);", 4816 Style); 4817 verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4818 " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n" 4819 " ccccccccccccccccccccccccccc;", 4820 Style); 4821 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4822 " aaaaa :\n" 4823 " bbbbbbbbbbbbbbb + cccccccccccccccc;", 4824 Style); 4825 } 4826 4827 TEST_F(FormatTest, DeclarationsOfMultipleVariables) { 4828 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" 4829 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); 4830 verifyFormat("bool a = true, b = false;"); 4831 4832 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" 4833 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" 4834 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" 4835 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); 4836 verifyFormat( 4837 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 4838 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n" 4839 " d = e && f;"); 4840 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n" 4841 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;"); 4842 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 4843 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;"); 4844 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n" 4845 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;"); 4846 4847 FormatStyle Style = getGoogleStyle(); 4848 Style.PointerAlignment = FormatStyle::PAS_Left; 4849 Style.DerivePointerAlignment = false; 4850 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4851 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" 4852 " *b = bbbbbbbbbbbbbbbbbbb;", 4853 Style); 4854 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 4855 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;", 4856 Style); 4857 verifyFormat("vector<int*> a, b;", Style); 4858 verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style); 4859 } 4860 4861 TEST_F(FormatTest, ConditionalExpressionsInBrackets) { 4862 verifyFormat("arr[foo ? bar : baz];"); 4863 verifyFormat("f()[foo ? bar : baz];"); 4864 verifyFormat("(a + b)[foo ? bar : baz];"); 4865 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); 4866 } 4867 4868 TEST_F(FormatTest, AlignsStringLiterals) { 4869 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" 4870 " \"short literal\");"); 4871 verifyFormat( 4872 "looooooooooooooooooooooooongFunction(\n" 4873 " \"short literal\"\n" 4874 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); 4875 verifyFormat("someFunction(\"Always break between multi-line\"\n" 4876 " \" string literals\",\n" 4877 " and, other, parameters);"); 4878 EXPECT_EQ("fun + \"1243\" /* comment */\n" 4879 " \"5678\";", 4880 format("fun + \"1243\" /* comment */\n" 4881 " \"5678\";", 4882 getLLVMStyleWithColumns(28))); 4883 EXPECT_EQ( 4884 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 4885 " \"aaaaaaaaaaaaaaaaaaaaa\"\n" 4886 " \"aaaaaaaaaaaaaaaa\";", 4887 format("aaaaaa =" 4888 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " 4889 "aaaaaaaaaaaaaaaaaaaaa\" " 4890 "\"aaaaaaaaaaaaaaaa\";")); 4891 verifyFormat("a = a + \"a\"\n" 4892 " \"a\"\n" 4893 " \"a\";"); 4894 verifyFormat("f(\"a\", \"b\"\n" 4895 " \"c\");"); 4896 4897 verifyFormat( 4898 "#define LL_FORMAT \"ll\"\n" 4899 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n" 4900 " \"d, ddddddddd: %\" LL_FORMAT \"d\");"); 4901 4902 verifyFormat("#define A(X) \\\n" 4903 " \"aaaaa\" #X \"bbbbbb\" \\\n" 4904 " \"ccccc\"", 4905 getLLVMStyleWithColumns(23)); 4906 verifyFormat("#define A \"def\"\n" 4907 "f(\"abc\" A \"ghi\"\n" 4908 " \"jkl\");"); 4909 4910 verifyFormat("f(L\"a\"\n" 4911 " L\"b\");"); 4912 verifyFormat("#define A(X) \\\n" 4913 " L\"aaaaa\" #X L\"bbbbbb\" \\\n" 4914 " L\"ccccc\"", 4915 getLLVMStyleWithColumns(25)); 4916 4917 verifyFormat("f(@\"a\"\n" 4918 " @\"b\");"); 4919 verifyFormat("NSString s = @\"a\"\n" 4920 " @\"b\"\n" 4921 " @\"c\";"); 4922 verifyFormat("NSString s = @\"a\"\n" 4923 " \"b\"\n" 4924 " \"c\";"); 4925 } 4926 4927 TEST_F(FormatTest, ReturnTypeBreakingStyle) { 4928 FormatStyle Style = getLLVMStyle(); 4929 // No declarations or definitions should be moved to own line. 4930 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None; 4931 verifyFormat("class A {\n" 4932 " int f() { return 1; }\n" 4933 " int g();\n" 4934 "};\n" 4935 "int f() { return 1; }\n" 4936 "int g();\n", 4937 Style); 4938 4939 // All declarations and definitions should have the return type moved to its 4940 // own 4941 // line. 4942 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 4943 verifyFormat("class E {\n" 4944 " int\n" 4945 " f() {\n" 4946 " return 1;\n" 4947 " }\n" 4948 " int\n" 4949 " g();\n" 4950 "};\n" 4951 "int\n" 4952 "f() {\n" 4953 " return 1;\n" 4954 "}\n" 4955 "int\n" 4956 "g();\n", 4957 Style); 4958 4959 // Top-level definitions, and no kinds of declarations should have the 4960 // return type moved to its own line. 4961 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions; 4962 verifyFormat("class B {\n" 4963 " int f() { return 1; }\n" 4964 " int g();\n" 4965 "};\n" 4966 "int\n" 4967 "f() {\n" 4968 " return 1;\n" 4969 "}\n" 4970 "int g();\n", 4971 Style); 4972 4973 // Top-level definitions and declarations should have the return type moved 4974 // to its own line. 4975 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel; 4976 verifyFormat("class C {\n" 4977 " int f() { return 1; }\n" 4978 " int g();\n" 4979 "};\n" 4980 "int\n" 4981 "f() {\n" 4982 " return 1;\n" 4983 "}\n" 4984 "int\n" 4985 "g();\n", 4986 Style); 4987 4988 // All definitions should have the return type moved to its own line, but no 4989 // kinds of declarations. 4990 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 4991 verifyFormat("class D {\n" 4992 " int\n" 4993 " f() {\n" 4994 " return 1;\n" 4995 " }\n" 4996 " int g();\n" 4997 "};\n" 4998 "int\n" 4999 "f() {\n" 5000 " return 1;\n" 5001 "}\n" 5002 "int g();\n", 5003 Style); 5004 verifyFormat("const char *\n" 5005 "f(void) {\n" // Break here. 5006 " return \"\";\n" 5007 "}\n" 5008 "const char *bar(void);\n", // No break here. 5009 Style); 5010 verifyFormat("template <class T>\n" 5011 "T *\n" 5012 "f(T &c) {\n" // Break here. 5013 " return NULL;\n" 5014 "}\n" 5015 "template <class T> T *f(T &c);\n", // No break here. 5016 Style); 5017 verifyFormat("class C {\n" 5018 " int\n" 5019 " operator+() {\n" 5020 " return 1;\n" 5021 " }\n" 5022 " int\n" 5023 " operator()() {\n" 5024 " return 1;\n" 5025 " }\n" 5026 "};\n", 5027 Style); 5028 verifyFormat("void\n" 5029 "A::operator()() {}\n" 5030 "void\n" 5031 "A::operator>>() {}\n" 5032 "void\n" 5033 "A::operator+() {}\n", 5034 Style); 5035 verifyFormat("void *operator new(std::size_t s);", // No break here. 5036 Style); 5037 verifyFormat("void *\n" 5038 "operator new(std::size_t s) {}", 5039 Style); 5040 verifyFormat("void *\n" 5041 "operator delete[](void *ptr) {}", 5042 Style); 5043 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 5044 verifyFormat("const char *\n" 5045 "f(void)\n" // Break here. 5046 "{\n" 5047 " return \"\";\n" 5048 "}\n" 5049 "const char *bar(void);\n", // No break here. 5050 Style); 5051 verifyFormat("template <class T>\n" 5052 "T *\n" // Problem here: no line break 5053 "f(T &c)\n" // Break here. 5054 "{\n" 5055 " return NULL;\n" 5056 "}\n" 5057 "template <class T> T *f(T &c);\n", // No break here. 5058 Style); 5059 } 5060 5061 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) { 5062 FormatStyle NoBreak = getLLVMStyle(); 5063 NoBreak.AlwaysBreakBeforeMultilineStrings = false; 5064 FormatStyle Break = getLLVMStyle(); 5065 Break.AlwaysBreakBeforeMultilineStrings = true; 5066 verifyFormat("aaaa = \"bbbb\"\n" 5067 " \"cccc\";", 5068 NoBreak); 5069 verifyFormat("aaaa =\n" 5070 " \"bbbb\"\n" 5071 " \"cccc\";", 5072 Break); 5073 verifyFormat("aaaa(\"bbbb\"\n" 5074 " \"cccc\");", 5075 NoBreak); 5076 verifyFormat("aaaa(\n" 5077 " \"bbbb\"\n" 5078 " \"cccc\");", 5079 Break); 5080 verifyFormat("aaaa(qqq, \"bbbb\"\n" 5081 " \"cccc\");", 5082 NoBreak); 5083 verifyFormat("aaaa(qqq,\n" 5084 " \"bbbb\"\n" 5085 " \"cccc\");", 5086 Break); 5087 verifyFormat("aaaa(qqq,\n" 5088 " L\"bbbb\"\n" 5089 " L\"cccc\");", 5090 Break); 5091 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n" 5092 " \"bbbb\"));", 5093 Break); 5094 verifyFormat("string s = someFunction(\n" 5095 " \"abc\"\n" 5096 " \"abc\");", 5097 Break); 5098 5099 // As we break before unary operators, breaking right after them is bad. 5100 verifyFormat("string foo = abc ? \"x\"\n" 5101 " \"blah blah blah blah blah blah\"\n" 5102 " : \"y\";", 5103 Break); 5104 5105 // Don't break if there is no column gain. 5106 verifyFormat("f(\"aaaa\"\n" 5107 " \"bbbb\");", 5108 Break); 5109 5110 // Treat literals with escaped newlines like multi-line string literals. 5111 EXPECT_EQ("x = \"a\\\n" 5112 "b\\\n" 5113 "c\";", 5114 format("x = \"a\\\n" 5115 "b\\\n" 5116 "c\";", 5117 NoBreak)); 5118 EXPECT_EQ("xxxx =\n" 5119 " \"a\\\n" 5120 "b\\\n" 5121 "c\";", 5122 format("xxxx = \"a\\\n" 5123 "b\\\n" 5124 "c\";", 5125 Break)); 5126 5127 EXPECT_EQ("NSString *const kString =\n" 5128 " @\"aaaa\"\n" 5129 " @\"bbbb\";", 5130 format("NSString *const kString = @\"aaaa\"\n" 5131 "@\"bbbb\";", 5132 Break)); 5133 5134 Break.ColumnLimit = 0; 5135 verifyFormat("const char *hello = \"hello llvm\";", Break); 5136 } 5137 5138 TEST_F(FormatTest, AlignsPipes) { 5139 verifyFormat( 5140 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5141 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5142 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5143 verifyFormat( 5144 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 5145 " << aaaaaaaaaaaaaaaaaaaa;"); 5146 verifyFormat( 5147 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5148 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5149 verifyFormat( 5150 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 5151 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5152 verifyFormat( 5153 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 5154 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 5155 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 5156 verifyFormat( 5157 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5158 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5159 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5160 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5161 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5162 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5163 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 5164 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n" 5165 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);"); 5166 verifyFormat( 5167 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5168 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5169 verifyFormat( 5170 "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n" 5171 " aaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5172 5173 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n" 5174 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();"); 5175 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5176 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5177 " aaaaaaaaaaaaaaaaaaaaa)\n" 5178 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5179 verifyFormat("LOG_IF(aaa == //\n" 5180 " bbb)\n" 5181 " << a << b;"); 5182 5183 // But sometimes, breaking before the first "<<" is desirable. 5184 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5185 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);"); 5186 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n" 5187 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5188 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5189 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n" 5190 " << BEF << IsTemplate << Description << E->getType();"); 5191 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5192 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5193 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5194 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5195 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5196 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5197 " << aaa;"); 5198 5199 verifyFormat( 5200 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5201 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5202 5203 // Incomplete string literal. 5204 EXPECT_EQ("llvm::errs() << \"\n" 5205 " << a;", 5206 format("llvm::errs() << \"\n<<a;")); 5207 5208 verifyFormat("void f() {\n" 5209 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n" 5210 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n" 5211 "}"); 5212 5213 // Handle 'endl'. 5214 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n" 5215 " << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 5216 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 5217 5218 // Handle '\n'. 5219 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n" 5220 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 5221 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n" 5222 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';"); 5223 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n" 5224 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";"); 5225 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 5226 } 5227 5228 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) { 5229 verifyFormat("return out << \"somepacket = {\\n\"\n" 5230 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" 5231 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" 5232 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" 5233 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" 5234 " << \"}\";"); 5235 5236 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 5237 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 5238 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;"); 5239 verifyFormat( 5240 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" 5241 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" 5242 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" 5243 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" 5244 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); 5245 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" 5246 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 5247 verifyFormat( 5248 "void f() {\n" 5249 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n" 5250 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 5251 "}"); 5252 5253 // Breaking before the first "<<" is generally not desirable. 5254 verifyFormat( 5255 "llvm::errs()\n" 5256 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5257 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5258 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5259 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5260 getLLVMStyleWithColumns(70)); 5261 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5262 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5263 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5264 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5265 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5266 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5267 getLLVMStyleWithColumns(70)); 5268 5269 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 5270 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 5271 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;"); 5272 verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 5273 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 5274 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);"); 5275 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n" 5276 " (aaaa + aaaa);", 5277 getLLVMStyleWithColumns(40)); 5278 verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n" 5279 " (aaaaaaa + aaaaa));", 5280 getLLVMStyleWithColumns(40)); 5281 verifyFormat( 5282 "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n" 5283 " SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n" 5284 " bbbbbbbbbbbbbbbbbbbbbbb);"); 5285 } 5286 5287 TEST_F(FormatTest, UnderstandsEquals) { 5288 verifyFormat( 5289 "aaaaaaaaaaaaaaaaa =\n" 5290 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5291 verifyFormat( 5292 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5293 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 5294 verifyFormat( 5295 "if (a) {\n" 5296 " f();\n" 5297 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5298 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 5299 "}"); 5300 5301 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5302 " 100000000 + 10000000) {\n}"); 5303 } 5304 5305 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 5306 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 5307 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 5308 5309 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 5310 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 5311 5312 verifyFormat( 5313 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 5314 " Parameter2);"); 5315 5316 verifyFormat( 5317 "ShortObject->shortFunction(\n" 5318 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 5319 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 5320 5321 verifyFormat("loooooooooooooongFunction(\n" 5322 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 5323 5324 verifyFormat( 5325 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 5326 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 5327 5328 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 5329 " .WillRepeatedly(Return(SomeValue));"); 5330 verifyFormat("void f() {\n" 5331 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 5332 " .Times(2)\n" 5333 " .WillRepeatedly(Return(SomeValue));\n" 5334 "}"); 5335 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n" 5336 " ccccccccccccccccccccccc);"); 5337 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5338 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5339 " .aaaaa(aaaaa),\n" 5340 " aaaaaaaaaaaaaaaaaaaaa);"); 5341 verifyFormat("void f() {\n" 5342 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5343 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n" 5344 "}"); 5345 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5346 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5347 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5348 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5349 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 5350 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5351 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5352 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5353 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n" 5354 "}"); 5355 5356 // Here, it is not necessary to wrap at "." or "->". 5357 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 5358 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 5359 verifyFormat( 5360 "aaaaaaaaaaa->aaaaaaaaa(\n" 5361 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5362 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 5363 5364 verifyFormat( 5365 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5366 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); 5367 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n" 5368 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 5369 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" 5370 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 5371 5372 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5373 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5374 " .a();"); 5375 5376 FormatStyle NoBinPacking = getLLVMStyle(); 5377 NoBinPacking.BinPackParameters = false; 5378 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 5379 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 5380 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" 5381 " aaaaaaaaaaaaaaaaaaa,\n" 5382 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5383 NoBinPacking); 5384 5385 // If there is a subsequent call, change to hanging indentation. 5386 verifyFormat( 5387 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5388 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n" 5389 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5390 verifyFormat( 5391 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5392 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));"); 5393 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5394 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5395 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5396 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5397 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5398 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 5399 } 5400 5401 TEST_F(FormatTest, WrapsTemplateDeclarations) { 5402 verifyFormat("template <typename T>\n" 5403 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 5404 verifyFormat("template <typename T>\n" 5405 "// T should be one of {A, B}.\n" 5406 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 5407 verifyFormat( 5408 "template <typename T>\n" 5409 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); 5410 verifyFormat("template <typename T>\n" 5411 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 5412 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 5413 verifyFormat( 5414 "template <typename T>\n" 5415 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 5416 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 5417 verifyFormat( 5418 "template <typename T>\n" 5419 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 5420 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 5421 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5422 verifyFormat("template <typename T>\n" 5423 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5424 " int aaaaaaaaaaaaaaaaaaaaaa);"); 5425 verifyFormat( 5426 "template <typename T1, typename T2 = char, typename T3 = char,\n" 5427 " typename T4 = char>\n" 5428 "void f();"); 5429 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n" 5430 " template <typename> class cccccccccccccccccccccc,\n" 5431 " typename ddddddddddddd>\n" 5432 "class C {};"); 5433 verifyFormat( 5434 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 5435 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5436 5437 verifyFormat("void f() {\n" 5438 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 5439 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n" 5440 "}"); 5441 5442 verifyFormat("template <typename T> class C {};"); 5443 verifyFormat("template <typename T> void f();"); 5444 verifyFormat("template <typename T> void f() {}"); 5445 verifyFormat( 5446 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 5447 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5448 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n" 5449 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 5450 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5451 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n" 5452 " bbbbbbbbbbbbbbbbbbbbbbbb);", 5453 getLLVMStyleWithColumns(72)); 5454 EXPECT_EQ("static_cast<A< //\n" 5455 " B> *>(\n" 5456 "\n" 5457 ");", 5458 format("static_cast<A<//\n" 5459 " B>*>(\n" 5460 "\n" 5461 " );")); 5462 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5463 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);"); 5464 5465 FormatStyle AlwaysBreak = getLLVMStyle(); 5466 AlwaysBreak.AlwaysBreakTemplateDeclarations = true; 5467 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak); 5468 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak); 5469 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak); 5470 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5471 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 5472 " ccccccccccccccccccccccccccccccccccccccccccccccc);"); 5473 verifyFormat("template <template <typename> class Fooooooo,\n" 5474 " template <typename> class Baaaaaaar>\n" 5475 "struct C {};", 5476 AlwaysBreak); 5477 verifyFormat("template <typename T> // T can be A, B or C.\n" 5478 "struct C {};", 5479 AlwaysBreak); 5480 verifyFormat("template <enum E> class A {\n" 5481 "public:\n" 5482 " E *f();\n" 5483 "};"); 5484 } 5485 5486 TEST_F(FormatTest, WrapsTemplateParameters) { 5487 FormatStyle Style = getLLVMStyle(); 5488 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 5489 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 5490 verifyFormat( 5491 "template <typename... a> struct q {};\n" 5492 "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 5493 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 5494 " y;", 5495 Style); 5496 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 5497 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 5498 verifyFormat( 5499 "template <typename... a> struct r {};\n" 5500 "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 5501 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 5502 " y;", 5503 Style); 5504 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 5505 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 5506 verifyFormat( 5507 "template <typename... a> struct s {};\n" 5508 "extern s<\n" 5509 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 5510 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n" 5511 " y;", 5512 Style); 5513 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 5514 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 5515 verifyFormat( 5516 "template <typename... a> struct t {};\n" 5517 "extern t<\n" 5518 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 5519 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n" 5520 " y;", 5521 Style); 5522 } 5523 5524 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 5525 verifyFormat( 5526 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5527 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5528 verifyFormat( 5529 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5530 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5531 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 5532 5533 // FIXME: Should we have the extra indent after the second break? 5534 verifyFormat( 5535 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5536 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5537 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5538 5539 verifyFormat( 5540 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 5541 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 5542 5543 // Breaking at nested name specifiers is generally not desirable. 5544 verifyFormat( 5545 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5546 " aaaaaaaaaaaaaaaaaaaaaaa);"); 5547 5548 verifyFormat( 5549 "aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n" 5550 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5551 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5552 " aaaaaaaaaaaaaaaaaaaaa);", 5553 getLLVMStyleWithColumns(74)); 5554 5555 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5556 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5557 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5558 } 5559 5560 TEST_F(FormatTest, UnderstandsTemplateParameters) { 5561 verifyFormat("A<int> a;"); 5562 verifyFormat("A<A<A<int>>> a;"); 5563 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 5564 verifyFormat("bool x = a < 1 || 2 > a;"); 5565 verifyFormat("bool x = 5 < f<int>();"); 5566 verifyFormat("bool x = f<int>() > 5;"); 5567 verifyFormat("bool x = 5 < a<int>::x;"); 5568 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 5569 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 5570 5571 verifyGoogleFormat("A<A<int>> a;"); 5572 verifyGoogleFormat("A<A<A<int>>> a;"); 5573 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 5574 verifyGoogleFormat("A<A<int> > a;"); 5575 verifyGoogleFormat("A<A<A<int> > > a;"); 5576 verifyGoogleFormat("A<A<A<A<int> > > > a;"); 5577 verifyGoogleFormat("A<::A<int>> a;"); 5578 verifyGoogleFormat("A<::A> a;"); 5579 verifyGoogleFormat("A< ::A> a;"); 5580 verifyGoogleFormat("A< ::A<int> > a;"); 5581 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); 5582 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); 5583 EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle())); 5584 EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle())); 5585 EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };", 5586 format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle())); 5587 5588 verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp)); 5589 5590 verifyFormat("test >> a >> b;"); 5591 verifyFormat("test << a >> b;"); 5592 5593 verifyFormat("f<int>();"); 5594 verifyFormat("template <typename T> void f() {}"); 5595 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;"); 5596 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : " 5597 "sizeof(char)>::type>;"); 5598 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};"); 5599 verifyFormat("f(a.operator()<A>());"); 5600 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5601 " .template operator()<A>());", 5602 getLLVMStyleWithColumns(35)); 5603 5604 // Not template parameters. 5605 verifyFormat("return a < b && c > d;"); 5606 verifyFormat("void f() {\n" 5607 " while (a < b && c > d) {\n" 5608 " }\n" 5609 "}"); 5610 verifyFormat("template <typename... Types>\n" 5611 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}"); 5612 5613 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5614 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);", 5615 getLLVMStyleWithColumns(60)); 5616 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");"); 5617 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}"); 5618 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <"); 5619 } 5620 5621 TEST_F(FormatTest, BitshiftOperatorWidth) { 5622 EXPECT_EQ("int a = 1 << 2; /* foo\n" 5623 " bar */", 5624 format("int a=1<<2; /* foo\n" 5625 " bar */")); 5626 5627 EXPECT_EQ("int b = 256 >> 1; /* foo\n" 5628 " bar */", 5629 format("int b =256>>1 ; /* foo\n" 5630 " bar */")); 5631 } 5632 5633 TEST_F(FormatTest, UnderstandsBinaryOperators) { 5634 verifyFormat("COMPARE(a, ==, b);"); 5635 verifyFormat("auto s = sizeof...(Ts) - 1;"); 5636 } 5637 5638 TEST_F(FormatTest, UnderstandsPointersToMembers) { 5639 verifyFormat("int A::*x;"); 5640 verifyFormat("int (S::*func)(void *);"); 5641 verifyFormat("void f() { int (S::*func)(void *); }"); 5642 verifyFormat("typedef bool *(Class::*Member)() const;"); 5643 verifyFormat("void f() {\n" 5644 " (a->*f)();\n" 5645 " a->*x;\n" 5646 " (a.*f)();\n" 5647 " ((*a).*f)();\n" 5648 " a.*x;\n" 5649 "}"); 5650 verifyFormat("void f() {\n" 5651 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 5652 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n" 5653 "}"); 5654 verifyFormat( 5655 "(aaaaaaaaaa->*bbbbbbb)(\n" 5656 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 5657 FormatStyle Style = getLLVMStyle(); 5658 Style.PointerAlignment = FormatStyle::PAS_Left; 5659 verifyFormat("typedef bool* (Class::*Member)() const;", Style); 5660 } 5661 5662 TEST_F(FormatTest, UnderstandsUnaryOperators) { 5663 verifyFormat("int a = -2;"); 5664 verifyFormat("f(-1, -2, -3);"); 5665 verifyFormat("a[-1] = 5;"); 5666 verifyFormat("int a = 5 + -2;"); 5667 verifyFormat("if (i == -1) {\n}"); 5668 verifyFormat("if (i != -1) {\n}"); 5669 verifyFormat("if (i > -1) {\n}"); 5670 verifyFormat("if (i < -1) {\n}"); 5671 verifyFormat("++(a->f());"); 5672 verifyFormat("--(a->f());"); 5673 verifyFormat("(a->f())++;"); 5674 verifyFormat("a[42]++;"); 5675 verifyFormat("if (!(a->f())) {\n}"); 5676 verifyFormat("if (!+i) {\n}"); 5677 verifyFormat("~&a;"); 5678 5679 verifyFormat("a-- > b;"); 5680 verifyFormat("b ? -a : c;"); 5681 verifyFormat("n * sizeof char16;"); 5682 verifyFormat("n * alignof char16;", getGoogleStyle()); 5683 verifyFormat("sizeof(char);"); 5684 verifyFormat("alignof(char);", getGoogleStyle()); 5685 5686 verifyFormat("return -1;"); 5687 verifyFormat("switch (a) {\n" 5688 "case -1:\n" 5689 " break;\n" 5690 "}"); 5691 verifyFormat("#define X -1"); 5692 verifyFormat("#define X -kConstant"); 5693 5694 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};"); 5695 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};"); 5696 5697 verifyFormat("int a = /* confusing comment */ -1;"); 5698 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 5699 verifyFormat("int a = i /* confusing comment */++;"); 5700 } 5701 5702 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) { 5703 verifyFormat("if (!aaaaaaaaaa( // break\n" 5704 " aaaaa)) {\n" 5705 "}"); 5706 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n" 5707 " aaaaa));"); 5708 verifyFormat("*aaa = aaaaaaa( // break\n" 5709 " bbbbbb);"); 5710 } 5711 5712 TEST_F(FormatTest, UnderstandsOverloadedOperators) { 5713 verifyFormat("bool operator<();"); 5714 verifyFormat("bool operator>();"); 5715 verifyFormat("bool operator=();"); 5716 verifyFormat("bool operator==();"); 5717 verifyFormat("bool operator!=();"); 5718 verifyFormat("int operator+();"); 5719 verifyFormat("int operator++();"); 5720 verifyFormat("int operator++(int) volatile noexcept;"); 5721 verifyFormat("bool operator,();"); 5722 verifyFormat("bool operator();"); 5723 verifyFormat("bool operator()();"); 5724 verifyFormat("bool operator[]();"); 5725 verifyFormat("operator bool();"); 5726 verifyFormat("operator int();"); 5727 verifyFormat("operator void *();"); 5728 verifyFormat("operator SomeType<int>();"); 5729 verifyFormat("operator SomeType<int, int>();"); 5730 verifyFormat("operator SomeType<SomeType<int>>();"); 5731 verifyFormat("void *operator new(std::size_t size);"); 5732 verifyFormat("void *operator new[](std::size_t size);"); 5733 verifyFormat("void operator delete(void *ptr);"); 5734 verifyFormat("void operator delete[](void *ptr);"); 5735 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n" 5736 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);"); 5737 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n" 5738 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;"); 5739 5740 verifyFormat( 5741 "ostream &operator<<(ostream &OutputStream,\n" 5742 " SomeReallyLongType WithSomeReallyLongValue);"); 5743 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n" 5744 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n" 5745 " return left.group < right.group;\n" 5746 "}"); 5747 verifyFormat("SomeType &operator=(const SomeType &S);"); 5748 verifyFormat("f.template operator()<int>();"); 5749 5750 verifyGoogleFormat("operator void*();"); 5751 verifyGoogleFormat("operator SomeType<SomeType<int>>();"); 5752 verifyGoogleFormat("operator ::A();"); 5753 5754 verifyFormat("using A::operator+;"); 5755 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n" 5756 "int i;"); 5757 } 5758 5759 TEST_F(FormatTest, UnderstandsFunctionRefQualification) { 5760 verifyFormat("Deleted &operator=(const Deleted &) & = default;"); 5761 verifyFormat("Deleted &operator=(const Deleted &) && = delete;"); 5762 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;"); 5763 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;"); 5764 verifyFormat("Deleted &operator=(const Deleted &) &;"); 5765 verifyFormat("Deleted &operator=(const Deleted &) &&;"); 5766 verifyFormat("SomeType MemberFunction(const Deleted &) &;"); 5767 verifyFormat("SomeType MemberFunction(const Deleted &) &&;"); 5768 verifyFormat("SomeType MemberFunction(const Deleted &) && {}"); 5769 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}"); 5770 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}"); 5771 verifyFormat("void Fn(T const &) const &;"); 5772 verifyFormat("void Fn(T const volatile &&) const volatile &&;"); 5773 verifyFormat("template <typename T>\n" 5774 "void F(T) && = delete;", 5775 getGoogleStyle()); 5776 5777 FormatStyle AlignLeft = getLLVMStyle(); 5778 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 5779 verifyFormat("void A::b() && {}", AlignLeft); 5780 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft); 5781 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;", 5782 AlignLeft); 5783 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft); 5784 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft); 5785 verifyFormat("auto Function(T t) & -> void {}", AlignLeft); 5786 verifyFormat("auto Function(T... t) & -> void {}", AlignLeft); 5787 verifyFormat("auto Function(T) & -> void {}", AlignLeft); 5788 verifyFormat("auto Function(T) & -> void;", AlignLeft); 5789 verifyFormat("void Fn(T const&) const&;", AlignLeft); 5790 verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft); 5791 5792 FormatStyle Spaces = getLLVMStyle(); 5793 Spaces.SpacesInCStyleCastParentheses = true; 5794 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces); 5795 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces); 5796 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces); 5797 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces); 5798 5799 Spaces.SpacesInCStyleCastParentheses = false; 5800 Spaces.SpacesInParentheses = true; 5801 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces); 5802 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces); 5803 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces); 5804 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces); 5805 } 5806 5807 TEST_F(FormatTest, UnderstandsNewAndDelete) { 5808 verifyFormat("void f() {\n" 5809 " A *a = new A;\n" 5810 " A *a = new (placement) A;\n" 5811 " delete a;\n" 5812 " delete (A *)a;\n" 5813 "}"); 5814 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 5815 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 5816 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5817 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 5818 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 5819 verifyFormat("delete[] h->p;"); 5820 } 5821 5822 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 5823 verifyFormat("int *f(int *a) {}"); 5824 verifyFormat("int main(int argc, char **argv) {}"); 5825 verifyFormat("Test::Test(int b) : a(b * b) {}"); 5826 verifyIndependentOfContext("f(a, *a);"); 5827 verifyFormat("void g() { f(*a); }"); 5828 verifyIndependentOfContext("int a = b * 10;"); 5829 verifyIndependentOfContext("int a = 10 * b;"); 5830 verifyIndependentOfContext("int a = b * c;"); 5831 verifyIndependentOfContext("int a += b * c;"); 5832 verifyIndependentOfContext("int a -= b * c;"); 5833 verifyIndependentOfContext("int a *= b * c;"); 5834 verifyIndependentOfContext("int a /= b * c;"); 5835 verifyIndependentOfContext("int a = *b;"); 5836 verifyIndependentOfContext("int a = *b * c;"); 5837 verifyIndependentOfContext("int a = b * *c;"); 5838 verifyIndependentOfContext("int a = b * (10);"); 5839 verifyIndependentOfContext("S << b * (10);"); 5840 verifyIndependentOfContext("return 10 * b;"); 5841 verifyIndependentOfContext("return *b * *c;"); 5842 verifyIndependentOfContext("return a & ~b;"); 5843 verifyIndependentOfContext("f(b ? *c : *d);"); 5844 verifyIndependentOfContext("int a = b ? *c : *d;"); 5845 verifyIndependentOfContext("*b = a;"); 5846 verifyIndependentOfContext("a * ~b;"); 5847 verifyIndependentOfContext("a * !b;"); 5848 verifyIndependentOfContext("a * +b;"); 5849 verifyIndependentOfContext("a * -b;"); 5850 verifyIndependentOfContext("a * ++b;"); 5851 verifyIndependentOfContext("a * --b;"); 5852 verifyIndependentOfContext("a[4] * b;"); 5853 verifyIndependentOfContext("a[a * a] = 1;"); 5854 verifyIndependentOfContext("f() * b;"); 5855 verifyIndependentOfContext("a * [self dostuff];"); 5856 verifyIndependentOfContext("int x = a * (a + b);"); 5857 verifyIndependentOfContext("(a *)(a + b);"); 5858 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;"); 5859 verifyIndependentOfContext("int *pa = (int *)&a;"); 5860 verifyIndependentOfContext("return sizeof(int **);"); 5861 verifyIndependentOfContext("return sizeof(int ******);"); 5862 verifyIndependentOfContext("return (int **&)a;"); 5863 verifyIndependentOfContext("f((*PointerToArray)[10]);"); 5864 verifyFormat("void f(Type (*parameter)[10]) {}"); 5865 verifyFormat("void f(Type (¶meter)[10]) {}"); 5866 verifyGoogleFormat("return sizeof(int**);"); 5867 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); 5868 verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); 5869 verifyFormat("auto a = [](int **&, int ***) {};"); 5870 verifyFormat("auto PointerBinding = [](const char *S) {};"); 5871 verifyFormat("typedef typeof(int(int, int)) *MyFunc;"); 5872 verifyFormat("[](const decltype(*a) &value) {}"); 5873 verifyFormat("decltype(a * b) F();"); 5874 verifyFormat("#define MACRO() [](A *a) { return 1; }"); 5875 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}"); 5876 verifyIndependentOfContext("typedef void (*f)(int *a);"); 5877 verifyIndependentOfContext("int i{a * b};"); 5878 verifyIndependentOfContext("aaa && aaa->f();"); 5879 verifyIndependentOfContext("int x = ~*p;"); 5880 verifyFormat("Constructor() : a(a), area(width * height) {}"); 5881 verifyFormat("Constructor() : a(a), area(a, width * height) {}"); 5882 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}"); 5883 verifyFormat("void f() { f(a, c * d); }"); 5884 verifyFormat("void f() { f(new a(), c * d); }"); 5885 verifyFormat("void f(const MyOverride &override);"); 5886 verifyFormat("void f(const MyFinal &final);"); 5887 verifyIndependentOfContext("bool a = f() && override.f();"); 5888 verifyIndependentOfContext("bool a = f() && final.f();"); 5889 5890 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 5891 5892 verifyIndependentOfContext("A<int *> a;"); 5893 verifyIndependentOfContext("A<int **> a;"); 5894 verifyIndependentOfContext("A<int *, int *> a;"); 5895 verifyIndependentOfContext("A<int *[]> a;"); 5896 verifyIndependentOfContext( 5897 "const char *const p = reinterpret_cast<const char *const>(q);"); 5898 verifyIndependentOfContext("A<int **, int **> a;"); 5899 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); 5900 verifyFormat("for (char **a = b; *a; ++a) {\n}"); 5901 verifyFormat("for (; a && b;) {\n}"); 5902 verifyFormat("bool foo = true && [] { return false; }();"); 5903 5904 verifyFormat( 5905 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5906 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5907 5908 verifyGoogleFormat("int const* a = &b;"); 5909 verifyGoogleFormat("**outparam = 1;"); 5910 verifyGoogleFormat("*outparam = a * b;"); 5911 verifyGoogleFormat("int main(int argc, char** argv) {}"); 5912 verifyGoogleFormat("A<int*> a;"); 5913 verifyGoogleFormat("A<int**> a;"); 5914 verifyGoogleFormat("A<int*, int*> a;"); 5915 verifyGoogleFormat("A<int**, int**> a;"); 5916 verifyGoogleFormat("f(b ? *c : *d);"); 5917 verifyGoogleFormat("int a = b ? *c : *d;"); 5918 verifyGoogleFormat("Type* t = **x;"); 5919 verifyGoogleFormat("Type* t = *++*x;"); 5920 verifyGoogleFormat("*++*x;"); 5921 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 5922 verifyGoogleFormat("Type* t = x++ * y;"); 5923 verifyGoogleFormat( 5924 "const char* const p = reinterpret_cast<const char* const>(q);"); 5925 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);"); 5926 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);"); 5927 verifyGoogleFormat("template <typename T>\n" 5928 "void f(int i = 0, SomeType** temps = NULL);"); 5929 5930 FormatStyle Left = getLLVMStyle(); 5931 Left.PointerAlignment = FormatStyle::PAS_Left; 5932 verifyFormat("x = *a(x) = *a(y);", Left); 5933 verifyFormat("for (;; *a = b) {\n}", Left); 5934 verifyFormat("return *this += 1;", Left); 5935 verifyFormat("throw *x;", Left); 5936 verifyFormat("delete *x;", Left); 5937 verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left); 5938 verifyFormat("[](const decltype(*a)* ptr) {}", Left); 5939 verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left); 5940 5941 verifyIndependentOfContext("a = *(x + y);"); 5942 verifyIndependentOfContext("a = &(x + y);"); 5943 verifyIndependentOfContext("*(x + y).call();"); 5944 verifyIndependentOfContext("&(x + y)->call();"); 5945 verifyFormat("void f() { &(*I).first; }"); 5946 5947 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 5948 verifyFormat( 5949 "int *MyValues = {\n" 5950 " *A, // Operator detection might be confused by the '{'\n" 5951 " *BB // Operator detection might be confused by previous comment\n" 5952 "};"); 5953 5954 verifyIndependentOfContext("if (int *a = &b)"); 5955 verifyIndependentOfContext("if (int &a = *b)"); 5956 verifyIndependentOfContext("if (a & b[i])"); 5957 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 5958 verifyIndependentOfContext("if (*b[i])"); 5959 verifyIndependentOfContext("if (int *a = (&b))"); 5960 verifyIndependentOfContext("while (int *a = &b)"); 5961 verifyIndependentOfContext("size = sizeof *a;"); 5962 verifyIndependentOfContext("if (a && (b = c))"); 5963 verifyFormat("void f() {\n" 5964 " for (const int &v : Values) {\n" 5965 " }\n" 5966 "}"); 5967 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); 5968 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); 5969 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}"); 5970 5971 verifyFormat("#define A (!a * b)"); 5972 verifyFormat("#define MACRO \\\n" 5973 " int *i = a * b; \\\n" 5974 " void f(a *b);", 5975 getLLVMStyleWithColumns(19)); 5976 5977 verifyIndependentOfContext("A = new SomeType *[Length];"); 5978 verifyIndependentOfContext("A = new SomeType *[Length]();"); 5979 verifyIndependentOfContext("T **t = new T *;"); 5980 verifyIndependentOfContext("T **t = new T *();"); 5981 verifyGoogleFormat("A = new SomeType*[Length]();"); 5982 verifyGoogleFormat("A = new SomeType*[Length];"); 5983 verifyGoogleFormat("T** t = new T*;"); 5984 verifyGoogleFormat("T** t = new T*();"); 5985 5986 verifyFormat("STATIC_ASSERT((a & b) == 0);"); 5987 verifyFormat("STATIC_ASSERT(0 == (a & b));"); 5988 verifyFormat("template <bool a, bool b> " 5989 "typename t::if<x && y>::type f() {}"); 5990 verifyFormat("template <int *y> f() {}"); 5991 verifyFormat("vector<int *> v;"); 5992 verifyFormat("vector<int *const> v;"); 5993 verifyFormat("vector<int *const **const *> v;"); 5994 verifyFormat("vector<int *volatile> v;"); 5995 verifyFormat("vector<a * b> v;"); 5996 verifyFormat("foo<b && false>();"); 5997 verifyFormat("foo<b & 1>();"); 5998 verifyFormat("decltype(*::std::declval<const T &>()) void F();"); 5999 verifyFormat( 6000 "template <class T, class = typename std::enable_if<\n" 6001 " std::is_integral<T>::value &&\n" 6002 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n" 6003 "void F();", 6004 getLLVMStyleWithColumns(70)); 6005 verifyFormat( 6006 "template <class T,\n" 6007 " class = typename std::enable_if<\n" 6008 " std::is_integral<T>::value &&\n" 6009 " (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n" 6010 " class U>\n" 6011 "void F();", 6012 getLLVMStyleWithColumns(70)); 6013 verifyFormat( 6014 "template <class T,\n" 6015 " class = typename ::std::enable_if<\n" 6016 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n" 6017 "void F();", 6018 getGoogleStyleWithColumns(68)); 6019 6020 verifyIndependentOfContext("MACRO(int *i);"); 6021 verifyIndependentOfContext("MACRO(auto *a);"); 6022 verifyIndependentOfContext("MACRO(const A *a);"); 6023 verifyIndependentOfContext("MACRO(A *const a);"); 6024 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');"); 6025 verifyFormat("void f() { f(float{1}, a * a); }"); 6026 // FIXME: Is there a way to make this work? 6027 // verifyIndependentOfContext("MACRO(A *a);"); 6028 6029 verifyFormat("DatumHandle const *operator->() const { return input_; }"); 6030 verifyFormat("return options != nullptr && operator==(*options);"); 6031 6032 EXPECT_EQ("#define OP(x) \\\n" 6033 " ostream &operator<<(ostream &s, const A &a) { \\\n" 6034 " return s << a.DebugString(); \\\n" 6035 " }", 6036 format("#define OP(x) \\\n" 6037 " ostream &operator<<(ostream &s, const A &a) { \\\n" 6038 " return s << a.DebugString(); \\\n" 6039 " }", 6040 getLLVMStyleWithColumns(50))); 6041 6042 // FIXME: We cannot handle this case yet; we might be able to figure out that 6043 // foo<x> d > v; doesn't make sense. 6044 verifyFormat("foo<a<b && c> d> v;"); 6045 6046 FormatStyle PointerMiddle = getLLVMStyle(); 6047 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 6048 verifyFormat("delete *x;", PointerMiddle); 6049 verifyFormat("int * x;", PointerMiddle); 6050 verifyFormat("template <int * y> f() {}", PointerMiddle); 6051 verifyFormat("int * f(int * a) {}", PointerMiddle); 6052 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle); 6053 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle); 6054 verifyFormat("A<int *> a;", PointerMiddle); 6055 verifyFormat("A<int **> a;", PointerMiddle); 6056 verifyFormat("A<int *, int *> a;", PointerMiddle); 6057 verifyFormat("A<int * []> a;", PointerMiddle); 6058 verifyFormat("A = new SomeType *[Length]();", PointerMiddle); 6059 verifyFormat("A = new SomeType *[Length];", PointerMiddle); 6060 verifyFormat("T ** t = new T *;", PointerMiddle); 6061 6062 // Member function reference qualifiers aren't binary operators. 6063 verifyFormat("string // break\n" 6064 "operator()() & {}"); 6065 verifyFormat("string // break\n" 6066 "operator()() && {}"); 6067 verifyGoogleFormat("template <typename T>\n" 6068 "auto x() & -> int {}"); 6069 } 6070 6071 TEST_F(FormatTest, UnderstandsAttributes) { 6072 verifyFormat("SomeType s __attribute__((unused)) (InitValue);"); 6073 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n" 6074 "aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 6075 FormatStyle AfterType = getLLVMStyle(); 6076 AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 6077 verifyFormat("__attribute__((nodebug)) void\n" 6078 "foo() {}\n", 6079 AfterType); 6080 } 6081 6082 TEST_F(FormatTest, UnderstandsSquareAttributes) { 6083 verifyFormat("SomeType s [[unused]] (InitValue);"); 6084 verifyFormat("SomeType s [[gnu::unused]] (InitValue);"); 6085 verifyFormat("SomeType s [[using gnu: unused]] (InitValue);"); 6086 verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}"); 6087 verifyFormat("void f() [[deprecated(\"so sorry\")]];"); 6088 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6089 " [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 6090 } 6091 6092 TEST_F(FormatTest, UnderstandsEllipsis) { 6093 verifyFormat("int printf(const char *fmt, ...);"); 6094 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); 6095 verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}"); 6096 6097 FormatStyle PointersLeft = getLLVMStyle(); 6098 PointersLeft.PointerAlignment = FormatStyle::PAS_Left; 6099 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft); 6100 } 6101 6102 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { 6103 EXPECT_EQ("int *a;\n" 6104 "int *a;\n" 6105 "int *a;", 6106 format("int *a;\n" 6107 "int* a;\n" 6108 "int *a;", 6109 getGoogleStyle())); 6110 EXPECT_EQ("int* a;\n" 6111 "int* a;\n" 6112 "int* a;", 6113 format("int* a;\n" 6114 "int* a;\n" 6115 "int *a;", 6116 getGoogleStyle())); 6117 EXPECT_EQ("int *a;\n" 6118 "int *a;\n" 6119 "int *a;", 6120 format("int *a;\n" 6121 "int * a;\n" 6122 "int * a;", 6123 getGoogleStyle())); 6124 EXPECT_EQ("auto x = [] {\n" 6125 " int *a;\n" 6126 " int *a;\n" 6127 " int *a;\n" 6128 "};", 6129 format("auto x=[]{int *a;\n" 6130 "int * a;\n" 6131 "int * a;};", 6132 getGoogleStyle())); 6133 } 6134 6135 TEST_F(FormatTest, UnderstandsRvalueReferences) { 6136 verifyFormat("int f(int &&a) {}"); 6137 verifyFormat("int f(int a, char &&b) {}"); 6138 verifyFormat("void f() { int &&a = b; }"); 6139 verifyGoogleFormat("int f(int a, char&& b) {}"); 6140 verifyGoogleFormat("void f() { int&& a = b; }"); 6141 6142 verifyIndependentOfContext("A<int &&> a;"); 6143 verifyIndependentOfContext("A<int &&, int &&> a;"); 6144 verifyGoogleFormat("A<int&&> a;"); 6145 verifyGoogleFormat("A<int&&, int&&> a;"); 6146 6147 // Not rvalue references: 6148 verifyFormat("template <bool B, bool C> class A {\n" 6149 " static_assert(B && C, \"Something is wrong\");\n" 6150 "};"); 6151 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))"); 6152 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))"); 6153 verifyFormat("#define A(a, b) (a && b)"); 6154 } 6155 6156 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 6157 verifyFormat("void f() {\n" 6158 " x[aaaaaaaaa -\n" 6159 " b] = 23;\n" 6160 "}", 6161 getLLVMStyleWithColumns(15)); 6162 } 6163 6164 TEST_F(FormatTest, FormatsCasts) { 6165 verifyFormat("Type *A = static_cast<Type *>(P);"); 6166 verifyFormat("Type *A = (Type *)P;"); 6167 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 6168 verifyFormat("int a = (int)(2.0f);"); 6169 verifyFormat("int a = (int)2.0f;"); 6170 verifyFormat("x[(int32)y];"); 6171 verifyFormat("x = (int32)y;"); 6172 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)"); 6173 verifyFormat("int a = (int)*b;"); 6174 verifyFormat("int a = (int)2.0f;"); 6175 verifyFormat("int a = (int)~0;"); 6176 verifyFormat("int a = (int)++a;"); 6177 verifyFormat("int a = (int)sizeof(int);"); 6178 verifyFormat("int a = (int)+2;"); 6179 verifyFormat("my_int a = (my_int)2.0f;"); 6180 verifyFormat("my_int a = (my_int)sizeof(int);"); 6181 verifyFormat("return (my_int)aaa;"); 6182 verifyFormat("#define x ((int)-1)"); 6183 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1"); 6184 verifyFormat("#define p(q) ((int *)&q)"); 6185 verifyFormat("fn(a)(b) + 1;"); 6186 6187 verifyFormat("void f() { my_int a = (my_int)*b; }"); 6188 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }"); 6189 verifyFormat("my_int a = (my_int)~0;"); 6190 verifyFormat("my_int a = (my_int)++a;"); 6191 verifyFormat("my_int a = (my_int)-2;"); 6192 verifyFormat("my_int a = (my_int)1;"); 6193 verifyFormat("my_int a = (my_int *)1;"); 6194 verifyFormat("my_int a = (const my_int)-1;"); 6195 verifyFormat("my_int a = (const my_int *)-1;"); 6196 verifyFormat("my_int a = (my_int)(my_int)-1;"); 6197 verifyFormat("my_int a = (ns::my_int)-2;"); 6198 verifyFormat("case (my_int)ONE:"); 6199 verifyFormat("auto x = (X)this;"); 6200 6201 // FIXME: single value wrapped with paren will be treated as cast. 6202 verifyFormat("void f(int i = (kValue)*kMask) {}"); 6203 6204 verifyFormat("{ (void)F; }"); 6205 6206 // Don't break after a cast's 6207 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 6208 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n" 6209 " bbbbbbbbbbbbbbbbbbbbbb);"); 6210 6211 // These are not casts. 6212 verifyFormat("void f(int *) {}"); 6213 verifyFormat("f(foo)->b;"); 6214 verifyFormat("f(foo).b;"); 6215 verifyFormat("f(foo)(b);"); 6216 verifyFormat("f(foo)[b];"); 6217 verifyFormat("[](foo) { return 4; }(bar);"); 6218 verifyFormat("(*funptr)(foo)[4];"); 6219 verifyFormat("funptrs[4](foo)[4];"); 6220 verifyFormat("void f(int *);"); 6221 verifyFormat("void f(int *) = 0;"); 6222 verifyFormat("void f(SmallVector<int>) {}"); 6223 verifyFormat("void f(SmallVector<int>);"); 6224 verifyFormat("void f(SmallVector<int>) = 0;"); 6225 verifyFormat("void f(int i = (kA * kB) & kMask) {}"); 6226 verifyFormat("int a = sizeof(int) * b;"); 6227 verifyFormat("int a = alignof(int) * b;", getGoogleStyle()); 6228 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;"); 6229 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");"); 6230 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;"); 6231 6232 // These are not casts, but at some point were confused with casts. 6233 verifyFormat("virtual void foo(int *) override;"); 6234 verifyFormat("virtual void foo(char &) const;"); 6235 verifyFormat("virtual void foo(int *a, char *) const;"); 6236 verifyFormat("int a = sizeof(int *) + b;"); 6237 verifyFormat("int a = alignof(int *) + b;", getGoogleStyle()); 6238 verifyFormat("bool b = f(g<int>) && c;"); 6239 verifyFormat("typedef void (*f)(int i) func;"); 6240 6241 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n" 6242 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 6243 // FIXME: The indentation here is not ideal. 6244 verifyFormat( 6245 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6246 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n" 6247 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];"); 6248 } 6249 6250 TEST_F(FormatTest, FormatsFunctionTypes) { 6251 verifyFormat("A<bool()> a;"); 6252 verifyFormat("A<SomeType()> a;"); 6253 verifyFormat("A<void (*)(int, std::string)> a;"); 6254 verifyFormat("A<void *(int)>;"); 6255 verifyFormat("void *(*a)(int *, SomeType *);"); 6256 verifyFormat("int (*func)(void *);"); 6257 verifyFormat("void f() { int (*func)(void *); }"); 6258 verifyFormat("template <class CallbackClass>\n" 6259 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);"); 6260 6261 verifyGoogleFormat("A<void*(int*, SomeType*)>;"); 6262 verifyGoogleFormat("void* (*a)(int);"); 6263 verifyGoogleFormat( 6264 "template <class CallbackClass>\n" 6265 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);"); 6266 6267 // Other constructs can look somewhat like function types: 6268 verifyFormat("A<sizeof(*x)> a;"); 6269 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)"); 6270 verifyFormat("some_var = function(*some_pointer_var)[0];"); 6271 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }"); 6272 verifyFormat("int x = f(&h)();"); 6273 verifyFormat("returnsFunction(¶m1, ¶m2)(param);"); 6274 verifyFormat("std::function<\n" 6275 " LooooooooooongTemplatedType<\n" 6276 " SomeType>*(\n" 6277 " LooooooooooooooooongType type)>\n" 6278 " function;", 6279 getGoogleStyleWithColumns(40)); 6280 } 6281 6282 TEST_F(FormatTest, FormatsPointersToArrayTypes) { 6283 verifyFormat("A (*foo_)[6];"); 6284 verifyFormat("vector<int> (*foo_)[6];"); 6285 } 6286 6287 TEST_F(FormatTest, BreaksLongVariableDeclarations) { 6288 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6289 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6290 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n" 6291 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6292 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6293 " *LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6294 6295 // Different ways of ()-initializiation. 6296 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6297 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);"); 6298 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6299 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);"); 6300 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6301 " LoooooooooooooooooooooooooooooooooooooooongVariable({});"); 6302 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6303 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);"); 6304 6305 // Lambdas should not confuse the variable declaration heuristic. 6306 verifyFormat("LooooooooooooooooongType\n" 6307 " variable(nullptr, [](A *a) {});", 6308 getLLVMStyleWithColumns(40)); 6309 } 6310 6311 TEST_F(FormatTest, BreaksLongDeclarations) { 6312 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n" 6313 " AnotherNameForTheLongType;"); 6314 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n" 6315 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 6316 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6317 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 6318 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n" 6319 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 6320 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6321 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6322 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n" 6323 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6324 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 6325 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6326 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 6327 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6328 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6329 "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);"); 6330 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6331 "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}"); 6332 FormatStyle Indented = getLLVMStyle(); 6333 Indented.IndentWrappedFunctionNames = true; 6334 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6335 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();", 6336 Indented); 6337 verifyFormat( 6338 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6339 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6340 Indented); 6341 verifyFormat( 6342 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 6343 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6344 Indented); 6345 verifyFormat( 6346 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 6347 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6348 Indented); 6349 6350 // FIXME: Without the comment, this breaks after "(". 6351 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n" 6352 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();", 6353 getGoogleStyle()); 6354 6355 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 6356 " int LoooooooooooooooooooongParam2) {}"); 6357 verifyFormat( 6358 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" 6359 " SourceLocation L, IdentifierIn *II,\n" 6360 " Type *T) {}"); 6361 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" 6362 "ReallyReaaallyLongFunctionName(\n" 6363 " const std::string &SomeParameter,\n" 6364 " const SomeType<string, SomeOtherTemplateParameter>\n" 6365 " &ReallyReallyLongParameterName,\n" 6366 " const SomeType<string, SomeOtherTemplateParameter>\n" 6367 " &AnotherLongParameterName) {}"); 6368 verifyFormat("template <typename A>\n" 6369 "SomeLoooooooooooooooooooooongType<\n" 6370 " typename some_namespace::SomeOtherType<A>::Type>\n" 6371 "Function() {}"); 6372 6373 verifyGoogleFormat( 6374 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" 6375 " aaaaaaaaaaaaaaaaaaaaaaa;"); 6376 verifyGoogleFormat( 6377 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" 6378 " SourceLocation L) {}"); 6379 verifyGoogleFormat( 6380 "some_namespace::LongReturnType\n" 6381 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" 6382 " int first_long_parameter, int second_parameter) {}"); 6383 6384 verifyGoogleFormat("template <typename T>\n" 6385 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" 6386 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); 6387 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6388 " int aaaaaaaaaaaaaaaaaaaaaaa);"); 6389 6390 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 6391 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6392 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6393 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6394 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 6395 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 6396 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6397 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 6398 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n" 6399 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6400 6401 verifyFormat("template <typename T> // Templates on own line.\n" 6402 "static int // Some comment.\n" 6403 "MyFunction(int a);", 6404 getLLVMStyle()); 6405 } 6406 6407 TEST_F(FormatTest, FormatsArrays) { 6408 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6409 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;"); 6410 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n" 6411 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;"); 6412 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n" 6413 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}"); 6414 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6415 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 6416 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6417 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;"); 6418 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6419 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6420 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 6421 verifyFormat( 6422 "llvm::outs() << \"aaaaaaaaaaaa: \"\n" 6423 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6424 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 6425 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n" 6426 " .aaaaaaaaaaaaaaaaaaaaaa();"); 6427 6428 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n" 6429 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];"); 6430 verifyFormat( 6431 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n" 6432 " .aaaaaaa[0]\n" 6433 " .aaaaaaaaaaaaaaaaaaaaaa();"); 6434 verifyFormat("a[::b::c];"); 6435 6436 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10)); 6437 6438 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0); 6439 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit); 6440 } 6441 6442 TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 6443 verifyFormat("(a)->b();"); 6444 verifyFormat("--a;"); 6445 } 6446 6447 TEST_F(FormatTest, HandlesIncludeDirectives) { 6448 verifyFormat("#include <string>\n" 6449 "#include <a/b/c.h>\n" 6450 "#include \"a/b/string\"\n" 6451 "#include \"string.h\"\n" 6452 "#include \"string.h\"\n" 6453 "#include <a-a>\n" 6454 "#include < path with space >\n" 6455 "#include_next <test.h>" 6456 "#include \"abc.h\" // this is included for ABC\n" 6457 "#include \"some long include\" // with a comment\n" 6458 "#include \"some very long include path\"\n" 6459 "#include <some/very/long/include/path>\n", 6460 getLLVMStyleWithColumns(35)); 6461 EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\"")); 6462 EXPECT_EQ("#include <a>", format("#include<a>")); 6463 6464 verifyFormat("#import <string>"); 6465 verifyFormat("#import <a/b/c.h>"); 6466 verifyFormat("#import \"a/b/string\""); 6467 verifyFormat("#import \"string.h\""); 6468 verifyFormat("#import \"string.h\""); 6469 verifyFormat("#if __has_include(<strstream>)\n" 6470 "#include <strstream>\n" 6471 "#endif"); 6472 6473 verifyFormat("#define MY_IMPORT <a/b>"); 6474 6475 verifyFormat("#if __has_include(<a/b>)"); 6476 verifyFormat("#if __has_include_next(<a/b>)"); 6477 verifyFormat("#define F __has_include(<a/b>)"); 6478 verifyFormat("#define F __has_include_next(<a/b>)"); 6479 6480 // Protocol buffer definition or missing "#". 6481 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";", 6482 getLLVMStyleWithColumns(30)); 6483 6484 FormatStyle Style = getLLVMStyle(); 6485 Style.AlwaysBreakBeforeMultilineStrings = true; 6486 Style.ColumnLimit = 0; 6487 verifyFormat("#import \"abc.h\"", Style); 6488 6489 // But 'import' might also be a regular C++ namespace. 6490 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6491 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6492 } 6493 6494 //===----------------------------------------------------------------------===// 6495 // Error recovery tests. 6496 //===----------------------------------------------------------------------===// 6497 6498 TEST_F(FormatTest, IncompleteParameterLists) { 6499 FormatStyle NoBinPacking = getLLVMStyle(); 6500 NoBinPacking.BinPackParameters = false; 6501 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" 6502 " double *min_x,\n" 6503 " double *max_x,\n" 6504 " double *min_y,\n" 6505 " double *max_y,\n" 6506 " double *min_z,\n" 6507 " double *max_z, ) {}", 6508 NoBinPacking); 6509 } 6510 6511 TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 6512 verifyFormat("void f() { return; }\n42"); 6513 verifyFormat("void f() {\n" 6514 " if (0)\n" 6515 " return;\n" 6516 "}\n" 6517 "42"); 6518 verifyFormat("void f() { return }\n42"); 6519 verifyFormat("void f() {\n" 6520 " if (0)\n" 6521 " return\n" 6522 "}\n" 6523 "42"); 6524 } 6525 6526 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 6527 EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); 6528 EXPECT_EQ("void f() {\n" 6529 " if (a)\n" 6530 " return\n" 6531 "}", 6532 format("void f ( ) { if ( a ) return }")); 6533 EXPECT_EQ("namespace N {\n" 6534 "void f()\n" 6535 "}", 6536 format("namespace N { void f() }")); 6537 EXPECT_EQ("namespace N {\n" 6538 "void f() {}\n" 6539 "void g()\n" 6540 "} // namespace N", 6541 format("namespace N { void f( ) { } void g( ) }")); 6542 } 6543 6544 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 6545 verifyFormat("int aaaaaaaa =\n" 6546 " // Overlylongcomment\n" 6547 " b;", 6548 getLLVMStyleWithColumns(20)); 6549 verifyFormat("function(\n" 6550 " ShortArgument,\n" 6551 " LoooooooooooongArgument);\n", 6552 getLLVMStyleWithColumns(20)); 6553 } 6554 6555 TEST_F(FormatTest, IncorrectAccessSpecifier) { 6556 verifyFormat("public:"); 6557 verifyFormat("class A {\n" 6558 "public\n" 6559 " void f() {}\n" 6560 "};"); 6561 verifyFormat("public\n" 6562 "int qwerty;"); 6563 verifyFormat("public\n" 6564 "B {}"); 6565 verifyFormat("public\n" 6566 "{}"); 6567 verifyFormat("public\n" 6568 "B { int x; }"); 6569 } 6570 6571 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 6572 verifyFormat("{"); 6573 verifyFormat("#})"); 6574 verifyNoCrash("(/**/[:!] ?[)."); 6575 } 6576 6577 TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 6578 verifyFormat("do {\n}"); 6579 verifyFormat("do {\n}\n" 6580 "f();"); 6581 verifyFormat("do {\n}\n" 6582 "wheeee(fun);"); 6583 verifyFormat("do {\n" 6584 " f();\n" 6585 "}"); 6586 } 6587 6588 TEST_F(FormatTest, IncorrectCodeMissingParens) { 6589 verifyFormat("if {\n foo;\n foo();\n}"); 6590 verifyFormat("switch {\n foo;\n foo();\n}"); 6591 verifyIncompleteFormat("for {\n foo;\n foo();\n}"); 6592 verifyFormat("while {\n foo;\n foo();\n}"); 6593 verifyFormat("do {\n foo;\n foo();\n} while;"); 6594 } 6595 6596 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 6597 verifyIncompleteFormat("namespace {\n" 6598 "class Foo { Foo (\n" 6599 "};\n" 6600 "} // namespace"); 6601 } 6602 6603 TEST_F(FormatTest, IncorrectCodeErrorDetection) { 6604 EXPECT_EQ("{\n {}\n", format("{\n{\n}\n")); 6605 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 6606 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 6607 EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n")); 6608 6609 EXPECT_EQ("{\n" 6610 " {\n" 6611 " breakme(\n" 6612 " qwe);\n" 6613 " }\n", 6614 format("{\n" 6615 " {\n" 6616 " breakme(qwe);\n" 6617 "}\n", 6618 getLLVMStyleWithColumns(10))); 6619 } 6620 6621 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 6622 verifyFormat("int x = {\n" 6623 " avariable,\n" 6624 " b(alongervariable)};", 6625 getLLVMStyleWithColumns(25)); 6626 } 6627 6628 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 6629 verifyFormat("return (a)(b){1, 2, 3};"); 6630 } 6631 6632 TEST_F(FormatTest, LayoutCxx11BraceInitializers) { 6633 verifyFormat("vector<int> x{1, 2, 3, 4};"); 6634 verifyFormat("vector<int> x{\n" 6635 " 1,\n" 6636 " 2,\n" 6637 " 3,\n" 6638 " 4,\n" 6639 "};"); 6640 verifyFormat("vector<T> x{{}, {}, {}, {}};"); 6641 verifyFormat("f({1, 2});"); 6642 verifyFormat("auto v = Foo{-1};"); 6643 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});"); 6644 verifyFormat("Class::Class : member{1, 2, 3} {}"); 6645 verifyFormat("new vector<int>{1, 2, 3};"); 6646 verifyFormat("new int[3]{1, 2, 3};"); 6647 verifyFormat("new int{1};"); 6648 verifyFormat("return {arg1, arg2};"); 6649 verifyFormat("return {arg1, SomeType{parameter}};"); 6650 verifyFormat("int count = set<int>{f(), g(), h()}.size();"); 6651 verifyFormat("new T{arg1, arg2};"); 6652 verifyFormat("f(MyMap[{composite, key}]);"); 6653 verifyFormat("class Class {\n" 6654 " T member = {arg1, arg2};\n" 6655 "};"); 6656 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};"); 6657 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 6658 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 6659 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");"); 6660 verifyFormat("int a = std::is_integral<int>{} + 0;"); 6661 6662 verifyFormat("int foo(int i) { return fo1{}(i); }"); 6663 verifyFormat("int foo(int i) { return fo1{}(i); }"); 6664 verifyFormat("auto i = decltype(x){};"); 6665 verifyFormat("std::vector<int> v = {1, 0 /* comment */};"); 6666 verifyFormat("Node n{1, Node{1000}, //\n" 6667 " 2};"); 6668 verifyFormat("Aaaa aaaaaaa{\n" 6669 " {\n" 6670 " aaaa,\n" 6671 " },\n" 6672 "};"); 6673 verifyFormat("class C : public D {\n" 6674 " SomeClass SC{2};\n" 6675 "};"); 6676 verifyFormat("class C : public A {\n" 6677 " class D : public B {\n" 6678 " void f() { int i{2}; }\n" 6679 " };\n" 6680 "};"); 6681 verifyFormat("#define A {a, a},"); 6682 6683 // Binpacking only if there is no trailing comma 6684 verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n" 6685 " cccccccccc, dddddddddd};", 6686 getLLVMStyleWithColumns(50)); 6687 verifyFormat("const Aaaaaa aaaaa = {\n" 6688 " aaaaaaaaaaa,\n" 6689 " bbbbbbbbbbb,\n" 6690 " ccccccccccc,\n" 6691 " ddddddddddd,\n" 6692 "};", getLLVMStyleWithColumns(50)); 6693 6694 // Cases where distinguising braced lists and blocks is hard. 6695 verifyFormat("vector<int> v{12} GUARDED_BY(mutex);"); 6696 verifyFormat("void f() {\n" 6697 " return; // comment\n" 6698 "}\n" 6699 "SomeType t;"); 6700 verifyFormat("void f() {\n" 6701 " if (a) {\n" 6702 " f();\n" 6703 " }\n" 6704 "}\n" 6705 "SomeType t;"); 6706 6707 // In combination with BinPackArguments = false. 6708 FormatStyle NoBinPacking = getLLVMStyle(); 6709 NoBinPacking.BinPackArguments = false; 6710 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n" 6711 " bbbbb,\n" 6712 " ccccc,\n" 6713 " ddddd,\n" 6714 " eeeee,\n" 6715 " ffffff,\n" 6716 " ggggg,\n" 6717 " hhhhhh,\n" 6718 " iiiiii,\n" 6719 " jjjjjj,\n" 6720 " kkkkkk};", 6721 NoBinPacking); 6722 verifyFormat("const Aaaaaa aaaaa = {\n" 6723 " aaaaa,\n" 6724 " bbbbb,\n" 6725 " ccccc,\n" 6726 " ddddd,\n" 6727 " eeeee,\n" 6728 " ffffff,\n" 6729 " ggggg,\n" 6730 " hhhhhh,\n" 6731 " iiiiii,\n" 6732 " jjjjjj,\n" 6733 " kkkkkk,\n" 6734 "};", 6735 NoBinPacking); 6736 verifyFormat( 6737 "const Aaaaaa aaaaa = {\n" 6738 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n" 6739 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n" 6740 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n" 6741 "};", 6742 NoBinPacking); 6743 6744 // FIXME: The alignment of these trailing comments might be bad. Then again, 6745 // this might be utterly useless in real code. 6746 verifyFormat("Constructor::Constructor()\n" 6747 " : some_value{ //\n" 6748 " aaaaaaa, //\n" 6749 " bbbbbbb} {}"); 6750 6751 // In braced lists, the first comment is always assumed to belong to the 6752 // first element. Thus, it can be moved to the next or previous line as 6753 // appropriate. 6754 EXPECT_EQ("function({// First element:\n" 6755 " 1,\n" 6756 " // Second element:\n" 6757 " 2});", 6758 format("function({\n" 6759 " // First element:\n" 6760 " 1,\n" 6761 " // Second element:\n" 6762 " 2});")); 6763 EXPECT_EQ("std::vector<int> MyNumbers{\n" 6764 " // First element:\n" 6765 " 1,\n" 6766 " // Second element:\n" 6767 " 2};", 6768 format("std::vector<int> MyNumbers{// First element:\n" 6769 " 1,\n" 6770 " // Second element:\n" 6771 " 2};", 6772 getLLVMStyleWithColumns(30))); 6773 // A trailing comma should still lead to an enforced line break and no 6774 // binpacking. 6775 EXPECT_EQ("vector<int> SomeVector = {\n" 6776 " // aaa\n" 6777 " 1,\n" 6778 " 2,\n" 6779 "};", 6780 format("vector<int> SomeVector = { // aaa\n" 6781 " 1, 2, };")); 6782 6783 FormatStyle ExtraSpaces = getLLVMStyle(); 6784 ExtraSpaces.Cpp11BracedListStyle = false; 6785 ExtraSpaces.ColumnLimit = 75; 6786 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces); 6787 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces); 6788 verifyFormat("f({ 1, 2 });", ExtraSpaces); 6789 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces); 6790 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces); 6791 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces); 6792 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces); 6793 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces); 6794 verifyFormat("return { arg1, arg2 };", ExtraSpaces); 6795 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces); 6796 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces); 6797 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces); 6798 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces); 6799 verifyFormat("class Class {\n" 6800 " T member = { arg1, arg2 };\n" 6801 "};", 6802 ExtraSpaces); 6803 verifyFormat( 6804 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6805 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n" 6806 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 6807 " bbbbbbbbbbbbbbbbbbbb, bbbbb };", 6808 ExtraSpaces); 6809 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces); 6810 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });", 6811 ExtraSpaces); 6812 verifyFormat( 6813 "someFunction(OtherParam,\n" 6814 " BracedList{ // comment 1 (Forcing interesting break)\n" 6815 " param1, param2,\n" 6816 " // comment 2\n" 6817 " param3, param4 });", 6818 ExtraSpaces); 6819 verifyFormat( 6820 "std::this_thread::sleep_for(\n" 6821 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);", 6822 ExtraSpaces); 6823 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n" 6824 " aaaaaaa,\n" 6825 " aaaaaaaaaa,\n" 6826 " aaaaa,\n" 6827 " aaaaaaaaaaaaaaa,\n" 6828 " aaa,\n" 6829 " aaaaaaaaaa,\n" 6830 " a,\n" 6831 " aaaaaaaaaaaaaaaaaaaaa,\n" 6832 " aaaaaaaaaaaa,\n" 6833 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n" 6834 " aaaaaaa,\n" 6835 " a};"); 6836 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces); 6837 verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces); 6838 verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces); 6839 } 6840 6841 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { 6842 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6843 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6844 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6845 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6846 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6847 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 6848 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n" 6849 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6850 " 1, 22, 333, 4444, 55555, //\n" 6851 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6852 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 6853 verifyFormat( 6854 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6855 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6856 " 1, 22, 333, 4444, 55555, 666666, // comment\n" 6857 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6858 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6859 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6860 " 7777777};"); 6861 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6862 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6863 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6864 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6865 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6866 " // Separating comment.\n" 6867 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6868 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6869 " // Leading comment\n" 6870 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6871 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6872 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6873 " 1, 1, 1, 1};", 6874 getLLVMStyleWithColumns(39)); 6875 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6876 " 1, 1, 1, 1};", 6877 getLLVMStyleWithColumns(38)); 6878 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n" 6879 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};", 6880 getLLVMStyleWithColumns(43)); 6881 verifyFormat( 6882 "static unsigned SomeValues[10][3] = {\n" 6883 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n" 6884 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};"); 6885 verifyFormat("static auto fields = new vector<string>{\n" 6886 " \"aaaaaaaaaaaaa\",\n" 6887 " \"aaaaaaaaaaaaa\",\n" 6888 " \"aaaaaaaaaaaa\",\n" 6889 " \"aaaaaaaaaaaaaa\",\n" 6890 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 6891 " \"aaaaaaaaaaaa\",\n" 6892 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 6893 "};"); 6894 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};"); 6895 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n" 6896 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n" 6897 " 3, cccccccccccccccccccccc};", 6898 getLLVMStyleWithColumns(60)); 6899 6900 // Trailing commas. 6901 verifyFormat("vector<int> x = {\n" 6902 " 1, 1, 1, 1, 1, 1, 1, 1,\n" 6903 "};", 6904 getLLVMStyleWithColumns(39)); 6905 verifyFormat("vector<int> x = {\n" 6906 " 1, 1, 1, 1, 1, 1, 1, 1, //\n" 6907 "};", 6908 getLLVMStyleWithColumns(39)); 6909 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6910 " 1, 1, 1, 1,\n" 6911 " /**/ /**/};", 6912 getLLVMStyleWithColumns(39)); 6913 6914 // Trailing comment in the first line. 6915 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n" 6916 " 1111111111, 2222222222, 33333333333, 4444444444, //\n" 6917 " 111111111, 222222222, 3333333333, 444444444, //\n" 6918 " 11111111, 22222222, 333333333, 44444444};"); 6919 // Trailing comment in the last line. 6920 verifyFormat("int aaaaa[] = {\n" 6921 " 1, 2, 3, // comment\n" 6922 " 4, 5, 6 // comment\n" 6923 "};"); 6924 6925 // With nested lists, we should either format one item per line or all nested 6926 // lists one on line. 6927 // FIXME: For some nested lists, we can do better. 6928 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n" 6929 " {aaaaaaaaaaaaaaaaaaa},\n" 6930 " {aaaaaaaaaaaaaaaaaaaaa},\n" 6931 " {aaaaaaaaaaaaaaaaa}};", 6932 getLLVMStyleWithColumns(60)); 6933 verifyFormat( 6934 "SomeStruct my_struct_array = {\n" 6935 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n" 6936 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n" 6937 " {aaa, aaa},\n" 6938 " {aaa, aaa},\n" 6939 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n" 6940 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6941 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};"); 6942 6943 // No column layout should be used here. 6944 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n" 6945 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};"); 6946 6947 verifyNoCrash("a<,"); 6948 6949 // No braced initializer here. 6950 verifyFormat("void f() {\n" 6951 " struct Dummy {};\n" 6952 " f(v);\n" 6953 "}"); 6954 6955 // Long lists should be formatted in columns even if they are nested. 6956 verifyFormat( 6957 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6958 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6959 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6960 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6961 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6962 " 1, 22, 333, 4444, 55555, 666666, 7777777});"); 6963 6964 // Allow "single-column" layout even if that violates the column limit. There 6965 // isn't going to be a better way. 6966 verifyFormat("std::vector<int> a = {\n" 6967 " aaaaaaaa,\n" 6968 " aaaaaaaa,\n" 6969 " aaaaaaaa,\n" 6970 " aaaaaaaa,\n" 6971 " aaaaaaaaaa,\n" 6972 " aaaaaaaa,\n" 6973 " aaaaaaaaaaaaaaaaaaaaaaaaaaa};", 6974 getLLVMStyleWithColumns(30)); 6975 verifyFormat("vector<int> aaaa = {\n" 6976 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6977 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6978 " aaaaaa.aaaaaaa,\n" 6979 " aaaaaa.aaaaaaa,\n" 6980 " aaaaaa.aaaaaaa,\n" 6981 " aaaaaa.aaaaaaa,\n" 6982 "};"); 6983 6984 // Don't create hanging lists. 6985 verifyFormat("someFunction(Param, {List1, List2,\n" 6986 " List3});", 6987 getLLVMStyleWithColumns(35)); 6988 verifyFormat("someFunction(Param, Param,\n" 6989 " {List1, List2,\n" 6990 " List3});", 6991 getLLVMStyleWithColumns(35)); 6992 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n" 6993 " aaaaaaaaaaaaaaaaaaaaaaa);"); 6994 } 6995 6996 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 6997 FormatStyle DoNotMerge = getLLVMStyle(); 6998 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 6999 7000 verifyFormat("void f() { return 42; }"); 7001 verifyFormat("void f() {\n" 7002 " return 42;\n" 7003 "}", 7004 DoNotMerge); 7005 verifyFormat("void f() {\n" 7006 " // Comment\n" 7007 "}"); 7008 verifyFormat("{\n" 7009 "#error {\n" 7010 " int a;\n" 7011 "}"); 7012 verifyFormat("{\n" 7013 " int a;\n" 7014 "#error {\n" 7015 "}"); 7016 verifyFormat("void f() {} // comment"); 7017 verifyFormat("void f() { int a; } // comment"); 7018 verifyFormat("void f() {\n" 7019 "} // comment", 7020 DoNotMerge); 7021 verifyFormat("void f() {\n" 7022 " int a;\n" 7023 "} // comment", 7024 DoNotMerge); 7025 verifyFormat("void f() {\n" 7026 "} // comment", 7027 getLLVMStyleWithColumns(15)); 7028 7029 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 7030 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 7031 7032 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 7033 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 7034 verifyFormat("class C {\n" 7035 " C()\n" 7036 " : iiiiiiii(nullptr),\n" 7037 " kkkkkkk(nullptr),\n" 7038 " mmmmmmm(nullptr),\n" 7039 " nnnnnnn(nullptr) {}\n" 7040 "};", 7041 getGoogleStyle()); 7042 7043 FormatStyle NoColumnLimit = getLLVMStyle(); 7044 NoColumnLimit.ColumnLimit = 0; 7045 EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit)); 7046 EXPECT_EQ("class C {\n" 7047 " A() : b(0) {}\n" 7048 "};", 7049 format("class C{A():b(0){}};", NoColumnLimit)); 7050 EXPECT_EQ("A()\n" 7051 " : b(0) {\n" 7052 "}", 7053 format("A()\n:b(0)\n{\n}", NoColumnLimit)); 7054 7055 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit; 7056 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine = 7057 FormatStyle::SFS_None; 7058 EXPECT_EQ("A()\n" 7059 " : b(0) {\n" 7060 "}", 7061 format("A():b(0){}", DoNotMergeNoColumnLimit)); 7062 EXPECT_EQ("A()\n" 7063 " : b(0) {\n" 7064 "}", 7065 format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit)); 7066 7067 verifyFormat("#define A \\\n" 7068 " void f() { \\\n" 7069 " int i; \\\n" 7070 " }", 7071 getLLVMStyleWithColumns(20)); 7072 verifyFormat("#define A \\\n" 7073 " void f() { int i; }", 7074 getLLVMStyleWithColumns(21)); 7075 verifyFormat("#define A \\\n" 7076 " void f() { \\\n" 7077 " int i; \\\n" 7078 " } \\\n" 7079 " int j;", 7080 getLLVMStyleWithColumns(22)); 7081 verifyFormat("#define A \\\n" 7082 " void f() { int i; } \\\n" 7083 " int j;", 7084 getLLVMStyleWithColumns(23)); 7085 } 7086 7087 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) { 7088 FormatStyle MergeEmptyOnly = getLLVMStyle(); 7089 MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 7090 verifyFormat("class C {\n" 7091 " int f() {}\n" 7092 "};", 7093 MergeEmptyOnly); 7094 verifyFormat("class C {\n" 7095 " int f() {\n" 7096 " return 42;\n" 7097 " }\n" 7098 "};", 7099 MergeEmptyOnly); 7100 verifyFormat("int f() {}", MergeEmptyOnly); 7101 verifyFormat("int f() {\n" 7102 " return 42;\n" 7103 "}", 7104 MergeEmptyOnly); 7105 7106 // Also verify behavior when BraceWrapping.AfterFunction = true 7107 MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 7108 MergeEmptyOnly.BraceWrapping.AfterFunction = true; 7109 verifyFormat("int f() {}", MergeEmptyOnly); 7110 verifyFormat("class C {\n" 7111 " int f() {}\n" 7112 "};", 7113 MergeEmptyOnly); 7114 } 7115 7116 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) { 7117 FormatStyle MergeInlineOnly = getLLVMStyle(); 7118 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 7119 verifyFormat("class C {\n" 7120 " int f() { return 42; }\n" 7121 "};", 7122 MergeInlineOnly); 7123 verifyFormat("int f() {\n" 7124 " return 42;\n" 7125 "}", 7126 MergeInlineOnly); 7127 7128 // SFS_Inline implies SFS_Empty 7129 verifyFormat("class C {\n" 7130 " int f() {}\n" 7131 "};", 7132 MergeInlineOnly); 7133 verifyFormat("int f() {}", MergeInlineOnly); 7134 7135 // Also verify behavior when BraceWrapping.AfterFunction = true 7136 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 7137 MergeInlineOnly.BraceWrapping.AfterFunction = true; 7138 verifyFormat("class C {\n" 7139 " int f() { return 42; }\n" 7140 "};", 7141 MergeInlineOnly); 7142 verifyFormat("int f()\n" 7143 "{\n" 7144 " return 42;\n" 7145 "}", 7146 MergeInlineOnly); 7147 7148 // SFS_Inline implies SFS_Empty 7149 verifyFormat("int f() {}", MergeInlineOnly); 7150 verifyFormat("class C {\n" 7151 " int f() {}\n" 7152 "};", 7153 MergeInlineOnly); 7154 } 7155 7156 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) { 7157 FormatStyle MergeInlineOnly = getLLVMStyle(); 7158 MergeInlineOnly.AllowShortFunctionsOnASingleLine = 7159 FormatStyle::SFS_InlineOnly; 7160 verifyFormat("class C {\n" 7161 " int f() { return 42; }\n" 7162 "};", 7163 MergeInlineOnly); 7164 verifyFormat("int f() {\n" 7165 " return 42;\n" 7166 "}", 7167 MergeInlineOnly); 7168 7169 // SFS_InlineOnly does not imply SFS_Empty 7170 verifyFormat("class C {\n" 7171 " int f() {}\n" 7172 "};", 7173 MergeInlineOnly); 7174 verifyFormat("int f() {\n" 7175 "}", 7176 MergeInlineOnly); 7177 7178 // Also verify behavior when BraceWrapping.AfterFunction = true 7179 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 7180 MergeInlineOnly.BraceWrapping.AfterFunction = true; 7181 verifyFormat("class C {\n" 7182 " int f() { return 42; }\n" 7183 "};", 7184 MergeInlineOnly); 7185 verifyFormat("int f()\n" 7186 "{\n" 7187 " return 42;\n" 7188 "}", 7189 MergeInlineOnly); 7190 7191 // SFS_InlineOnly does not imply SFS_Empty 7192 verifyFormat("int f()\n" 7193 "{\n" 7194 "}", 7195 MergeInlineOnly); 7196 verifyFormat("class C {\n" 7197 " int f() {}\n" 7198 "};", 7199 MergeInlineOnly); 7200 } 7201 7202 TEST_F(FormatTest, SplitEmptyFunction) { 7203 FormatStyle Style = getLLVMStyle(); 7204 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 7205 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7206 Style.BraceWrapping.AfterFunction = true; 7207 Style.BraceWrapping.SplitEmptyFunction = false; 7208 Style.ColumnLimit = 40; 7209 7210 verifyFormat("int f()\n" 7211 "{}", 7212 Style); 7213 verifyFormat("int f()\n" 7214 "{\n" 7215 " return 42;\n" 7216 "}", 7217 Style); 7218 verifyFormat("int f()\n" 7219 "{\n" 7220 " // some comment\n" 7221 "}", 7222 Style); 7223 7224 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 7225 verifyFormat("int f() {}", Style); 7226 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 7227 "{}", 7228 Style); 7229 verifyFormat("int f()\n" 7230 "{\n" 7231 " return 0;\n" 7232 "}", 7233 Style); 7234 7235 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 7236 verifyFormat("class Foo {\n" 7237 " int f() {}\n" 7238 "};\n", 7239 Style); 7240 verifyFormat("class Foo {\n" 7241 " int f() { return 0; }\n" 7242 "};\n", 7243 Style); 7244 verifyFormat("class Foo {\n" 7245 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 7246 " {}\n" 7247 "};\n", 7248 Style); 7249 verifyFormat("class Foo {\n" 7250 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 7251 " {\n" 7252 " return 0;\n" 7253 " }\n" 7254 "};\n", 7255 Style); 7256 7257 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 7258 verifyFormat("int f() {}", Style); 7259 verifyFormat("int f() { return 0; }", Style); 7260 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 7261 "{}", 7262 Style); 7263 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 7264 "{\n" 7265 " return 0;\n" 7266 "}", 7267 Style); 7268 } 7269 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) { 7270 FormatStyle Style = getLLVMStyle(); 7271 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 7272 verifyFormat("#ifdef A\n" 7273 "int f() {}\n" 7274 "#else\n" 7275 "int g() {}\n" 7276 "#endif", 7277 Style); 7278 } 7279 7280 TEST_F(FormatTest, SplitEmptyClass) { 7281 FormatStyle Style = getLLVMStyle(); 7282 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7283 Style.BraceWrapping.AfterClass = true; 7284 Style.BraceWrapping.SplitEmptyRecord = false; 7285 7286 verifyFormat("class Foo\n" 7287 "{};", 7288 Style); 7289 verifyFormat("/* something */ class Foo\n" 7290 "{};", 7291 Style); 7292 verifyFormat("template <typename X> class Foo\n" 7293 "{};", 7294 Style); 7295 verifyFormat("class Foo\n" 7296 "{\n" 7297 " Foo();\n" 7298 "};", 7299 Style); 7300 verifyFormat("typedef class Foo\n" 7301 "{\n" 7302 "} Foo_t;", 7303 Style); 7304 } 7305 7306 TEST_F(FormatTest, SplitEmptyStruct) { 7307 FormatStyle Style = getLLVMStyle(); 7308 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7309 Style.BraceWrapping.AfterStruct = true; 7310 Style.BraceWrapping.SplitEmptyRecord = false; 7311 7312 verifyFormat("struct Foo\n" 7313 "{};", 7314 Style); 7315 verifyFormat("/* something */ struct Foo\n" 7316 "{};", 7317 Style); 7318 verifyFormat("template <typename X> struct Foo\n" 7319 "{};", 7320 Style); 7321 verifyFormat("struct Foo\n" 7322 "{\n" 7323 " Foo();\n" 7324 "};", 7325 Style); 7326 verifyFormat("typedef struct Foo\n" 7327 "{\n" 7328 "} Foo_t;", 7329 Style); 7330 //typedef struct Bar {} Bar_t; 7331 } 7332 7333 TEST_F(FormatTest, SplitEmptyUnion) { 7334 FormatStyle Style = getLLVMStyle(); 7335 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7336 Style.BraceWrapping.AfterUnion = true; 7337 Style.BraceWrapping.SplitEmptyRecord = false; 7338 7339 verifyFormat("union Foo\n" 7340 "{};", 7341 Style); 7342 verifyFormat("/* something */ union Foo\n" 7343 "{};", 7344 Style); 7345 verifyFormat("union Foo\n" 7346 "{\n" 7347 " A,\n" 7348 "};", 7349 Style); 7350 verifyFormat("typedef union Foo\n" 7351 "{\n" 7352 "} Foo_t;", 7353 Style); 7354 } 7355 7356 TEST_F(FormatTest, SplitEmptyNamespace) { 7357 FormatStyle Style = getLLVMStyle(); 7358 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7359 Style.BraceWrapping.AfterNamespace = true; 7360 Style.BraceWrapping.SplitEmptyNamespace = false; 7361 7362 verifyFormat("namespace Foo\n" 7363 "{};", 7364 Style); 7365 verifyFormat("/* something */ namespace Foo\n" 7366 "{};", 7367 Style); 7368 verifyFormat("inline namespace Foo\n" 7369 "{};", 7370 Style); 7371 verifyFormat("namespace Foo\n" 7372 "{\n" 7373 "void Bar();\n" 7374 "};", 7375 Style); 7376 } 7377 7378 TEST_F(FormatTest, NeverMergeShortRecords) { 7379 FormatStyle Style = getLLVMStyle(); 7380 7381 verifyFormat("class Foo {\n" 7382 " Foo();\n" 7383 "};", 7384 Style); 7385 verifyFormat("typedef class Foo {\n" 7386 " Foo();\n" 7387 "} Foo_t;", 7388 Style); 7389 verifyFormat("struct Foo {\n" 7390 " Foo();\n" 7391 "};", 7392 Style); 7393 verifyFormat("typedef struct Foo {\n" 7394 " Foo();\n" 7395 "} Foo_t;", 7396 Style); 7397 verifyFormat("union Foo {\n" 7398 " A,\n" 7399 "};", 7400 Style); 7401 verifyFormat("typedef union Foo {\n" 7402 " A,\n" 7403 "} Foo_t;", 7404 Style); 7405 verifyFormat("namespace Foo {\n" 7406 "void Bar();\n" 7407 "};", 7408 Style); 7409 7410 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7411 Style.BraceWrapping.AfterClass = true; 7412 Style.BraceWrapping.AfterStruct = true; 7413 Style.BraceWrapping.AfterUnion = true; 7414 Style.BraceWrapping.AfterNamespace = true; 7415 verifyFormat("class Foo\n" 7416 "{\n" 7417 " Foo();\n" 7418 "};", 7419 Style); 7420 verifyFormat("typedef class Foo\n" 7421 "{\n" 7422 " Foo();\n" 7423 "} Foo_t;", 7424 Style); 7425 verifyFormat("struct Foo\n" 7426 "{\n" 7427 " Foo();\n" 7428 "};", 7429 Style); 7430 verifyFormat("typedef struct Foo\n" 7431 "{\n" 7432 " Foo();\n" 7433 "} Foo_t;", 7434 Style); 7435 verifyFormat("union Foo\n" 7436 "{\n" 7437 " A,\n" 7438 "};", 7439 Style); 7440 verifyFormat("typedef union Foo\n" 7441 "{\n" 7442 " A,\n" 7443 "} Foo_t;", 7444 Style); 7445 verifyFormat("namespace Foo\n" 7446 "{\n" 7447 "void Bar();\n" 7448 "};", 7449 Style); 7450 } 7451 7452 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 7453 // Elaborate type variable declarations. 7454 verifyFormat("struct foo a = {bar};\nint n;"); 7455 verifyFormat("class foo a = {bar};\nint n;"); 7456 verifyFormat("union foo a = {bar};\nint n;"); 7457 7458 // Elaborate types inside function definitions. 7459 verifyFormat("struct foo f() {}\nint n;"); 7460 verifyFormat("class foo f() {}\nint n;"); 7461 verifyFormat("union foo f() {}\nint n;"); 7462 7463 // Templates. 7464 verifyFormat("template <class X> void f() {}\nint n;"); 7465 verifyFormat("template <struct X> void f() {}\nint n;"); 7466 verifyFormat("template <union X> void f() {}\nint n;"); 7467 7468 // Actual definitions... 7469 verifyFormat("struct {\n} n;"); 7470 verifyFormat( 7471 "template <template <class T, class Y>, class Z> class X {\n} n;"); 7472 verifyFormat("union Z {\n int n;\n} x;"); 7473 verifyFormat("class MACRO Z {\n} n;"); 7474 verifyFormat("class MACRO(X) Z {\n} n;"); 7475 verifyFormat("class __attribute__(X) Z {\n} n;"); 7476 verifyFormat("class __declspec(X) Z {\n} n;"); 7477 verifyFormat("class A##B##C {\n} n;"); 7478 verifyFormat("class alignas(16) Z {\n} n;"); 7479 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;"); 7480 verifyFormat("class MACROA MACRO(X) Z {\n} n;"); 7481 7482 // Redefinition from nested context: 7483 verifyFormat("class A::B::C {\n} n;"); 7484 7485 // Template definitions. 7486 verifyFormat( 7487 "template <typename F>\n" 7488 "Matcher(const Matcher<F> &Other,\n" 7489 " typename enable_if_c<is_base_of<F, T>::value &&\n" 7490 " !is_same<F, T>::value>::type * = 0)\n" 7491 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}"); 7492 7493 // FIXME: This is still incorrectly handled at the formatter side. 7494 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};"); 7495 verifyFormat("int i = SomeFunction(a<b, a> b);"); 7496 7497 // FIXME: 7498 // This now gets parsed incorrectly as class definition. 7499 // verifyFormat("class A<int> f() {\n}\nint n;"); 7500 7501 // Elaborate types where incorrectly parsing the structural element would 7502 // break the indent. 7503 verifyFormat("if (true)\n" 7504 " class X x;\n" 7505 "else\n" 7506 " f();\n"); 7507 7508 // This is simply incomplete. Formatting is not important, but must not crash. 7509 verifyFormat("class A:"); 7510 } 7511 7512 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 7513 EXPECT_EQ("#error Leave all white!!!!! space* alone!\n", 7514 format("#error Leave all white!!!!! space* alone!\n")); 7515 EXPECT_EQ( 7516 "#warning Leave all white!!!!! space* alone!\n", 7517 format("#warning Leave all white!!!!! space* alone!\n")); 7518 EXPECT_EQ("#error 1", format(" # error 1")); 7519 EXPECT_EQ("#warning 1", format(" # warning 1")); 7520 } 7521 7522 TEST_F(FormatTest, FormatHashIfExpressions) { 7523 verifyFormat("#if AAAA && BBBB"); 7524 verifyFormat("#if (AAAA && BBBB)"); 7525 verifyFormat("#elif (AAAA && BBBB)"); 7526 // FIXME: Come up with a better indentation for #elif. 7527 verifyFormat( 7528 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" 7529 " defined(BBBBBBBB)\n" 7530 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" 7531 " defined(BBBBBBBB)\n" 7532 "#endif", 7533 getLLVMStyleWithColumns(65)); 7534 } 7535 7536 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 7537 FormatStyle AllowsMergedIf = getGoogleStyle(); 7538 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 7539 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 7540 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 7541 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 7542 EXPECT_EQ("if (true) return 42;", 7543 format("if (true)\nreturn 42;", AllowsMergedIf)); 7544 FormatStyle ShortMergedIf = AllowsMergedIf; 7545 ShortMergedIf.ColumnLimit = 25; 7546 verifyFormat("#define A \\\n" 7547 " if (true) return 42;", 7548 ShortMergedIf); 7549 verifyFormat("#define A \\\n" 7550 " f(); \\\n" 7551 " if (true)\n" 7552 "#define B", 7553 ShortMergedIf); 7554 verifyFormat("#define A \\\n" 7555 " f(); \\\n" 7556 " if (true)\n" 7557 "g();", 7558 ShortMergedIf); 7559 verifyFormat("{\n" 7560 "#ifdef A\n" 7561 " // Comment\n" 7562 " if (true) continue;\n" 7563 "#endif\n" 7564 " // Comment\n" 7565 " if (true) continue;\n" 7566 "}", 7567 ShortMergedIf); 7568 ShortMergedIf.ColumnLimit = 33; 7569 verifyFormat("#define A \\\n" 7570 " if constexpr (true) return 42;", 7571 ShortMergedIf); 7572 ShortMergedIf.ColumnLimit = 29; 7573 verifyFormat("#define A \\\n" 7574 " if (aaaaaaaaaa) return 1; \\\n" 7575 " return 2;", 7576 ShortMergedIf); 7577 ShortMergedIf.ColumnLimit = 28; 7578 verifyFormat("#define A \\\n" 7579 " if (aaaaaaaaaa) \\\n" 7580 " return 1; \\\n" 7581 " return 2;", 7582 ShortMergedIf); 7583 verifyFormat("#define A \\\n" 7584 " if constexpr (aaaaaaa) \\\n" 7585 " return 1; \\\n" 7586 " return 2;", 7587 ShortMergedIf); 7588 } 7589 7590 TEST_F(FormatTest, FormatStarDependingOnContext) { 7591 verifyFormat("void f(int *a);"); 7592 verifyFormat("void f() { f(fint * b); }"); 7593 verifyFormat("class A {\n void f(int *a);\n};"); 7594 verifyFormat("class A {\n int *a;\n};"); 7595 verifyFormat("namespace a {\n" 7596 "namespace b {\n" 7597 "class A {\n" 7598 " void f() {}\n" 7599 " int *a;\n" 7600 "};\n" 7601 "} // namespace b\n" 7602 "} // namespace a"); 7603 } 7604 7605 TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 7606 verifyFormat("while"); 7607 verifyFormat("operator"); 7608 } 7609 7610 TEST_F(FormatTest, SkipsDeeplyNestedLines) { 7611 // This code would be painfully slow to format if we didn't skip it. 7612 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 7613 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 7614 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 7615 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 7616 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 7617 "A(1, 1)\n" 7618 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x 7619 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7620 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7621 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7622 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7623 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7624 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7625 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7626 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7627 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n"); 7628 // Deeply nested part is untouched, rest is formatted. 7629 EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n", 7630 format(std::string("int i;\n") + Code + "int j;\n", 7631 getLLVMStyle(), SC_ExpectIncomplete)); 7632 } 7633 7634 //===----------------------------------------------------------------------===// 7635 // Objective-C tests. 7636 //===----------------------------------------------------------------------===// 7637 7638 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 7639 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 7640 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 7641 format("-(NSUInteger)indexOfObject:(id)anObject;")); 7642 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 7643 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 7644 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 7645 format("-(NSInteger)Method3:(id)anObject;")); 7646 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 7647 format("-(NSInteger)Method4:(id)anObject;")); 7648 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 7649 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 7650 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 7651 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 7652 EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject " 7653 "forAllCells:(BOOL)flag;", 7654 format("- (void)sendAction:(SEL)aSelector to:(id)anObject " 7655 "forAllCells:(BOOL)flag;")); 7656 7657 // Very long objectiveC method declaration. 7658 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 7659 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;"); 7660 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 7661 " inRange:(NSRange)range\n" 7662 " outRange:(NSRange)out_range\n" 7663 " outRange1:(NSRange)out_range1\n" 7664 " outRange2:(NSRange)out_range2\n" 7665 " outRange3:(NSRange)out_range3\n" 7666 " outRange4:(NSRange)out_range4\n" 7667 " outRange5:(NSRange)out_range5\n" 7668 " outRange6:(NSRange)out_range6\n" 7669 " outRange7:(NSRange)out_range7\n" 7670 " outRange8:(NSRange)out_range8\n" 7671 " outRange9:(NSRange)out_range9;"); 7672 7673 // When the function name has to be wrapped. 7674 FormatStyle Style = getLLVMStyle(); 7675 Style.IndentWrappedFunctionNames = false; 7676 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 7677 "veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 7678 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 7679 "}", 7680 Style); 7681 Style.IndentWrappedFunctionNames = true; 7682 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 7683 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 7684 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 7685 "}", 7686 Style); 7687 7688 verifyFormat("- (int)sum:(vector<int>)numbers;"); 7689 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 7690 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 7691 // protocol lists (but not for template classes): 7692 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 7693 7694 verifyFormat("- (int (*)())foo:(int (*)())f;"); 7695 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); 7696 7697 // If there's no return type (very rare in practice!), LLVM and Google style 7698 // agree. 7699 verifyFormat("- foo;"); 7700 verifyFormat("- foo:(int)f;"); 7701 verifyGoogleFormat("- foo:(int)foo;"); 7702 } 7703 7704 7705 TEST_F(FormatTest, BreaksStringLiterals) { 7706 EXPECT_EQ("\"some text \"\n" 7707 "\"other\";", 7708 format("\"some text other\";", getLLVMStyleWithColumns(12))); 7709 EXPECT_EQ("\"some text \"\n" 7710 "\"other\";", 7711 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); 7712 EXPECT_EQ( 7713 "#define A \\\n" 7714 " \"some \" \\\n" 7715 " \"text \" \\\n" 7716 " \"other\";", 7717 format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); 7718 EXPECT_EQ( 7719 "#define A \\\n" 7720 " \"so \" \\\n" 7721 " \"text \" \\\n" 7722 " \"other\";", 7723 format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); 7724 7725 EXPECT_EQ("\"some text\"", 7726 format("\"some text\"", getLLVMStyleWithColumns(1))); 7727 EXPECT_EQ("\"some text\"", 7728 format("\"some text\"", getLLVMStyleWithColumns(11))); 7729 EXPECT_EQ("\"some \"\n" 7730 "\"text\"", 7731 format("\"some text\"", getLLVMStyleWithColumns(10))); 7732 EXPECT_EQ("\"some \"\n" 7733 "\"text\"", 7734 format("\"some text\"", getLLVMStyleWithColumns(7))); 7735 EXPECT_EQ("\"some\"\n" 7736 "\" tex\"\n" 7737 "\"t\"", 7738 format("\"some text\"", getLLVMStyleWithColumns(6))); 7739 EXPECT_EQ("\"some\"\n" 7740 "\" tex\"\n" 7741 "\" and\"", 7742 format("\"some tex and\"", getLLVMStyleWithColumns(6))); 7743 EXPECT_EQ("\"some\"\n" 7744 "\"/tex\"\n" 7745 "\"/and\"", 7746 format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); 7747 7748 EXPECT_EQ("variable =\n" 7749 " \"long string \"\n" 7750 " \"literal\";", 7751 format("variable = \"long string literal\";", 7752 getLLVMStyleWithColumns(20))); 7753 7754 EXPECT_EQ("variable = f(\n" 7755 " \"long string \"\n" 7756 " \"literal\",\n" 7757 " short,\n" 7758 " loooooooooooooooooooong);", 7759 format("variable = f(\"long string literal\", short, " 7760 "loooooooooooooooooooong);", 7761 getLLVMStyleWithColumns(20))); 7762 7763 EXPECT_EQ( 7764 "f(g(\"long string \"\n" 7765 " \"literal\"),\n" 7766 " b);", 7767 format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20))); 7768 EXPECT_EQ("f(g(\"long string \"\n" 7769 " \"literal\",\n" 7770 " a),\n" 7771 " b);", 7772 format("f(g(\"long string literal\", a), b);", 7773 getLLVMStyleWithColumns(20))); 7774 EXPECT_EQ( 7775 "f(\"one two\".split(\n" 7776 " variable));", 7777 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); 7778 EXPECT_EQ("f(\"one two three four five six \"\n" 7779 " \"seven\".split(\n" 7780 " really_looooong_variable));", 7781 format("f(\"one two three four five six seven\"." 7782 "split(really_looooong_variable));", 7783 getLLVMStyleWithColumns(33))); 7784 7785 EXPECT_EQ("f(\"some \"\n" 7786 " \"text\",\n" 7787 " other);", 7788 format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); 7789 7790 // Only break as a last resort. 7791 verifyFormat( 7792 "aaaaaaaaaaaaaaaaaaaa(\n" 7793 " aaaaaaaaaaaaaaaaaaaa,\n" 7794 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); 7795 7796 EXPECT_EQ("\"splitmea\"\n" 7797 "\"trandomp\"\n" 7798 "\"oint\"", 7799 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); 7800 7801 EXPECT_EQ("\"split/\"\n" 7802 "\"pathat/\"\n" 7803 "\"slashes\"", 7804 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 7805 7806 EXPECT_EQ("\"split/\"\n" 7807 "\"pathat/\"\n" 7808 "\"slashes\"", 7809 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 7810 EXPECT_EQ("\"split at \"\n" 7811 "\"spaces/at/\"\n" 7812 "\"slashes.at.any$\"\n" 7813 "\"non-alphanumeric%\"\n" 7814 "\"1111111111characte\"\n" 7815 "\"rs\"", 7816 format("\"split at " 7817 "spaces/at/" 7818 "slashes.at." 7819 "any$non-" 7820 "alphanumeric%" 7821 "1111111111characte" 7822 "rs\"", 7823 getLLVMStyleWithColumns(20))); 7824 7825 // Verify that splitting the strings understands 7826 // Style::AlwaysBreakBeforeMultilineStrings. 7827 EXPECT_EQ( 7828 "aaaaaaaaaaaa(\n" 7829 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" 7830 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", 7831 format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa " 7832 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 7833 "aaaaaaaaaaaaaaaaaaaaaa\");", 7834 getGoogleStyle())); 7835 EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7836 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", 7837 format("return \"aaaaaaaaaaaaaaaaaaaaaa " 7838 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 7839 "aaaaaaaaaaaaaaaaaaaaaa\";", 7840 getGoogleStyle())); 7841 EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7842 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 7843 format("llvm::outs() << " 7844 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa" 7845 "aaaaaaaaaaaaaaaaaaa\";")); 7846 EXPECT_EQ("ffff(\n" 7847 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7848 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 7849 format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " 7850 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 7851 getGoogleStyle())); 7852 7853 FormatStyle Style = getLLVMStyleWithColumns(12); 7854 Style.BreakStringLiterals = false; 7855 EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style)); 7856 7857 FormatStyle AlignLeft = getLLVMStyleWithColumns(12); 7858 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 7859 EXPECT_EQ("#define A \\\n" 7860 " \"some \" \\\n" 7861 " \"text \" \\\n" 7862 " \"other\";", 7863 format("#define A \"some text other\";", AlignLeft)); 7864 } 7865 7866 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) { 7867 EXPECT_EQ("C a = \"some more \"\n" 7868 " \"text\";", 7869 format("C a = \"some more text\";", getLLVMStyleWithColumns(18))); 7870 } 7871 7872 TEST_F(FormatTest, FullyRemoveEmptyLines) { 7873 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80); 7874 NoEmptyLines.MaxEmptyLinesToKeep = 0; 7875 EXPECT_EQ("int i = a(b());", 7876 format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines)); 7877 } 7878 7879 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) { 7880 EXPECT_EQ( 7881 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 7882 "(\n" 7883 " \"x\t\");", 7884 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 7885 "aaaaaaa(" 7886 "\"x\t\");")); 7887 } 7888 7889 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) { 7890 EXPECT_EQ( 7891 "u8\"utf8 string \"\n" 7892 "u8\"literal\";", 7893 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16))); 7894 EXPECT_EQ( 7895 "u\"utf16 string \"\n" 7896 "u\"literal\";", 7897 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16))); 7898 EXPECT_EQ( 7899 "U\"utf32 string \"\n" 7900 "U\"literal\";", 7901 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16))); 7902 EXPECT_EQ("L\"wide string \"\n" 7903 "L\"literal\";", 7904 format("L\"wide string literal\";", getGoogleStyleWithColumns(16))); 7905 EXPECT_EQ("@\"NSString \"\n" 7906 "@\"literal\";", 7907 format("@\"NSString literal\";", getGoogleStyleWithColumns(19))); 7908 verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26)); 7909 7910 // This input makes clang-format try to split the incomplete unicode escape 7911 // sequence, which used to lead to a crasher. 7912 verifyNoCrash( 7913 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 7914 getLLVMStyleWithColumns(60)); 7915 } 7916 7917 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) { 7918 FormatStyle Style = getGoogleStyleWithColumns(15); 7919 EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style)); 7920 EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style)); 7921 EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style)); 7922 EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style)); 7923 EXPECT_EQ("u8R\"x(raw literal)x\";", 7924 format("u8R\"x(raw literal)x\";", Style)); 7925 } 7926 7927 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { 7928 FormatStyle Style = getLLVMStyleWithColumns(20); 7929 EXPECT_EQ( 7930 "_T(\"aaaaaaaaaaaaaa\")\n" 7931 "_T(\"aaaaaaaaaaaaaa\")\n" 7932 "_T(\"aaaaaaaaaaaa\")", 7933 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style)); 7934 EXPECT_EQ("f(x,\n" 7935 " _T(\"aaaaaaaaaaaa\")\n" 7936 " _T(\"aaa\"),\n" 7937 " z);", 7938 format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style)); 7939 7940 // FIXME: Handle embedded spaces in one iteration. 7941 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n" 7942 // "_T(\"aaaaaaaaaaaaa\")\n" 7943 // "_T(\"aaaaaaaaaaaaa\")\n" 7944 // "_T(\"a\")", 7945 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 7946 // getLLVMStyleWithColumns(20))); 7947 EXPECT_EQ( 7948 "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 7949 format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style)); 7950 EXPECT_EQ("f(\n" 7951 "#if !TEST\n" 7952 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 7953 "#endif\n" 7954 ");", 7955 format("f(\n" 7956 "#if !TEST\n" 7957 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 7958 "#endif\n" 7959 ");")); 7960 EXPECT_EQ("f(\n" 7961 "\n" 7962 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));", 7963 format("f(\n" 7964 "\n" 7965 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));")); 7966 } 7967 7968 TEST_F(FormatTest, BreaksStringLiteralOperands) { 7969 // In a function call with two operands, the second can be broken with no line 7970 // break before it. 7971 EXPECT_EQ("func(a, \"long long \"\n" 7972 " \"long long\");", 7973 format("func(a, \"long long long long\");", 7974 getLLVMStyleWithColumns(24))); 7975 // In a function call with three operands, the second must be broken with a 7976 // line break before it. 7977 EXPECT_EQ("func(a,\n" 7978 " \"long long long \"\n" 7979 " \"long\",\n" 7980 " c);", 7981 format("func(a, \"long long long long\", c);", 7982 getLLVMStyleWithColumns(24))); 7983 // In a function call with three operands, the third must be broken with a 7984 // line break before it. 7985 EXPECT_EQ("func(a, b,\n" 7986 " \"long long long \"\n" 7987 " \"long\");", 7988 format("func(a, b, \"long long long long\");", 7989 getLLVMStyleWithColumns(24))); 7990 // In a function call with three operands, both the second and the third must 7991 // be broken with a line break before them. 7992 EXPECT_EQ("func(a,\n" 7993 " \"long long long \"\n" 7994 " \"long\",\n" 7995 " \"long long long \"\n" 7996 " \"long\");", 7997 format("func(a, \"long long long long\", \"long long long long\");", 7998 getLLVMStyleWithColumns(24))); 7999 // In a chain of << with two operands, the second can be broken with no line 8000 // break before it. 8001 EXPECT_EQ("a << \"line line \"\n" 8002 " \"line\";", 8003 format("a << \"line line line\";", 8004 getLLVMStyleWithColumns(20))); 8005 // In a chain of << with three operands, the second can be broken with no line 8006 // break before it. 8007 EXPECT_EQ("abcde << \"line \"\n" 8008 " \"line line\"\n" 8009 " << c;", 8010 format("abcde << \"line line line\" << c;", 8011 getLLVMStyleWithColumns(20))); 8012 // In a chain of << with three operands, the third must be broken with a line 8013 // break before it. 8014 EXPECT_EQ("a << b\n" 8015 " << \"line line \"\n" 8016 " \"line\";", 8017 format("a << b << \"line line line\";", 8018 getLLVMStyleWithColumns(20))); 8019 // In a chain of << with three operands, the second can be broken with no line 8020 // break before it and the third must be broken with a line break before it. 8021 EXPECT_EQ("abcd << \"line line \"\n" 8022 " \"line\"\n" 8023 " << \"line line \"\n" 8024 " \"line\";", 8025 format("abcd << \"line line line\" << \"line line line\";", 8026 getLLVMStyleWithColumns(20))); 8027 // In a chain of binary operators with two operands, the second can be broken 8028 // with no line break before it. 8029 EXPECT_EQ("abcd + \"line line \"\n" 8030 " \"line line\";", 8031 format("abcd + \"line line line line\";", 8032 getLLVMStyleWithColumns(20))); 8033 // In a chain of binary operators with three operands, the second must be 8034 // broken with a line break before it. 8035 EXPECT_EQ("abcd +\n" 8036 " \"line line \"\n" 8037 " \"line line\" +\n" 8038 " e;", 8039 format("abcd + \"line line line line\" + e;", 8040 getLLVMStyleWithColumns(20))); 8041 // In a function call with two operands, with AlignAfterOpenBracket enabled, 8042 // the first must be broken with a line break before it. 8043 FormatStyle Style = getLLVMStyleWithColumns(25); 8044 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 8045 EXPECT_EQ("someFunction(\n" 8046 " \"long long long \"\n" 8047 " \"long\",\n" 8048 " a);", 8049 format("someFunction(\"long long long long\", a);", Style)); 8050 } 8051 8052 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { 8053 EXPECT_EQ( 8054 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8055 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8056 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 8057 format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8058 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8059 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";")); 8060 } 8061 8062 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { 8063 EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);", 8064 format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle())); 8065 EXPECT_EQ("fffffffffff(g(R\"x(\n" 8066 "multiline raw string literal xxxxxxxxxxxxxx\n" 8067 ")x\",\n" 8068 " a),\n" 8069 " b);", 8070 format("fffffffffff(g(R\"x(\n" 8071 "multiline raw string literal xxxxxxxxxxxxxx\n" 8072 ")x\", a), b);", 8073 getGoogleStyleWithColumns(20))); 8074 EXPECT_EQ("fffffffffff(\n" 8075 " g(R\"x(qqq\n" 8076 "multiline raw string literal xxxxxxxxxxxxxx\n" 8077 ")x\",\n" 8078 " a),\n" 8079 " b);", 8080 format("fffffffffff(g(R\"x(qqq\n" 8081 "multiline raw string literal xxxxxxxxxxxxxx\n" 8082 ")x\", a), b);", 8083 getGoogleStyleWithColumns(20))); 8084 8085 EXPECT_EQ("fffffffffff(R\"x(\n" 8086 "multiline raw string literal xxxxxxxxxxxxxx\n" 8087 ")x\");", 8088 format("fffffffffff(R\"x(\n" 8089 "multiline raw string literal xxxxxxxxxxxxxx\n" 8090 ")x\");", 8091 getGoogleStyleWithColumns(20))); 8092 EXPECT_EQ("fffffffffff(R\"x(\n" 8093 "multiline raw string literal xxxxxxxxxxxxxx\n" 8094 ")x\" + bbbbbb);", 8095 format("fffffffffff(R\"x(\n" 8096 "multiline raw string literal xxxxxxxxxxxxxx\n" 8097 ")x\" + bbbbbb);", 8098 getGoogleStyleWithColumns(20))); 8099 EXPECT_EQ("fffffffffff(\n" 8100 " R\"x(\n" 8101 "multiline raw string literal xxxxxxxxxxxxxx\n" 8102 ")x\" +\n" 8103 " bbbbbb);", 8104 format("fffffffffff(\n" 8105 " R\"x(\n" 8106 "multiline raw string literal xxxxxxxxxxxxxx\n" 8107 ")x\" + bbbbbb);", 8108 getGoogleStyleWithColumns(20))); 8109 EXPECT_EQ("fffffffffff(R\"(single line raw string)\" + bbbbbb);", 8110 format("fffffffffff(\n" 8111 " R\"(single line raw string)\" + bbbbbb);")); 8112 } 8113 8114 TEST_F(FormatTest, SkipsUnknownStringLiterals) { 8115 verifyFormat("string a = \"unterminated;"); 8116 EXPECT_EQ("function(\"unterminated,\n" 8117 " OtherParameter);", 8118 format("function( \"unterminated,\n" 8119 " OtherParameter);")); 8120 } 8121 8122 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { 8123 FormatStyle Style = getLLVMStyle(); 8124 Style.Standard = FormatStyle::LS_Cpp03; 8125 EXPECT_EQ("#define x(_a) printf(\"foo\" _a);", 8126 format("#define x(_a) printf(\"foo\"_a);", Style)); 8127 } 8128 8129 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); } 8130 8131 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { 8132 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n" 8133 " \"ddeeefff\");", 8134 format("someFunction(\"aaabbbcccdddeeefff\");", 8135 getLLVMStyleWithColumns(25))); 8136 EXPECT_EQ("someFunction1234567890(\n" 8137 " \"aaabbbcccdddeeefff\");", 8138 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 8139 getLLVMStyleWithColumns(26))); 8140 EXPECT_EQ("someFunction1234567890(\n" 8141 " \"aaabbbcccdddeeeff\"\n" 8142 " \"f\");", 8143 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 8144 getLLVMStyleWithColumns(25))); 8145 EXPECT_EQ("someFunction1234567890(\n" 8146 " \"aaabbbcccdddeeeff\"\n" 8147 " \"f\");", 8148 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 8149 getLLVMStyleWithColumns(24))); 8150 EXPECT_EQ("someFunction(\n" 8151 " \"aaabbbcc ddde \"\n" 8152 " \"efff\");", 8153 format("someFunction(\"aaabbbcc ddde efff\");", 8154 getLLVMStyleWithColumns(25))); 8155 EXPECT_EQ("someFunction(\"aaabbbccc \"\n" 8156 " \"ddeeefff\");", 8157 format("someFunction(\"aaabbbccc ddeeefff\");", 8158 getLLVMStyleWithColumns(25))); 8159 EXPECT_EQ("someFunction1234567890(\n" 8160 " \"aaabb \"\n" 8161 " \"cccdddeeefff\");", 8162 format("someFunction1234567890(\"aaabb cccdddeeefff\");", 8163 getLLVMStyleWithColumns(25))); 8164 EXPECT_EQ("#define A \\\n" 8165 " string s = \\\n" 8166 " \"123456789\" \\\n" 8167 " \"0\"; \\\n" 8168 " int i;", 8169 format("#define A string s = \"1234567890\"; int i;", 8170 getLLVMStyleWithColumns(20))); 8171 EXPECT_EQ("someFunction(\n" 8172 " \"aaabbbcc \"\n" 8173 " \"dddeeefff\");", 8174 format("someFunction(\"aaabbbcc dddeeefff\");", 8175 getLLVMStyleWithColumns(25))); 8176 } 8177 8178 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { 8179 EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3))); 8180 EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2))); 8181 EXPECT_EQ("\"test\"\n" 8182 "\"\\n\"", 8183 format("\"test\\n\"", getLLVMStyleWithColumns(7))); 8184 EXPECT_EQ("\"tes\\\\\"\n" 8185 "\"n\"", 8186 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); 8187 EXPECT_EQ("\"\\\\\\\\\"\n" 8188 "\"\\n\"", 8189 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); 8190 EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7))); 8191 EXPECT_EQ("\"\\uff01\"\n" 8192 "\"test\"", 8193 format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); 8194 EXPECT_EQ("\"\\Uff01ff02\"", 8195 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); 8196 EXPECT_EQ("\"\\x000000000001\"\n" 8197 "\"next\"", 8198 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); 8199 EXPECT_EQ("\"\\x000000000001next\"", 8200 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); 8201 EXPECT_EQ("\"\\x000000000001\"", 8202 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); 8203 EXPECT_EQ("\"test\"\n" 8204 "\"\\000000\"\n" 8205 "\"000001\"", 8206 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); 8207 EXPECT_EQ("\"test\\000\"\n" 8208 "\"00000000\"\n" 8209 "\"1\"", 8210 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); 8211 } 8212 8213 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { 8214 verifyFormat("void f() {\n" 8215 " return g() {}\n" 8216 " void h() {}"); 8217 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n" 8218 "g();\n" 8219 "}"); 8220 } 8221 8222 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) { 8223 verifyFormat( 8224 "void f() { return C{param1, param2}.SomeCall(param1, param2); }"); 8225 } 8226 8227 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { 8228 verifyFormat("class X {\n" 8229 " void f() {\n" 8230 " }\n" 8231 "};", 8232 getLLVMStyleWithColumns(12)); 8233 } 8234 8235 TEST_F(FormatTest, ConfigurableIndentWidth) { 8236 FormatStyle EightIndent = getLLVMStyleWithColumns(18); 8237 EightIndent.IndentWidth = 8; 8238 EightIndent.ContinuationIndentWidth = 8; 8239 verifyFormat("void f() {\n" 8240 " someFunction();\n" 8241 " if (true) {\n" 8242 " f();\n" 8243 " }\n" 8244 "}", 8245 EightIndent); 8246 verifyFormat("class X {\n" 8247 " void f() {\n" 8248 " }\n" 8249 "};", 8250 EightIndent); 8251 verifyFormat("int x[] = {\n" 8252 " call(),\n" 8253 " call()};", 8254 EightIndent); 8255 } 8256 8257 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) { 8258 verifyFormat("double\n" 8259 "f();", 8260 getLLVMStyleWithColumns(8)); 8261 } 8262 8263 TEST_F(FormatTest, ConfigurableUseOfTab) { 8264 FormatStyle Tab = getLLVMStyleWithColumns(42); 8265 Tab.IndentWidth = 8; 8266 Tab.UseTab = FormatStyle::UT_Always; 8267 Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left; 8268 8269 EXPECT_EQ("if (aaaaaaaa && // q\n" 8270 " bb)\t\t// w\n" 8271 "\t;", 8272 format("if (aaaaaaaa &&// q\n" 8273 "bb)// w\n" 8274 ";", 8275 Tab)); 8276 EXPECT_EQ("if (aaa && bbb) // w\n" 8277 "\t;", 8278 format("if(aaa&&bbb)// w\n" 8279 ";", 8280 Tab)); 8281 8282 verifyFormat("class X {\n" 8283 "\tvoid f() {\n" 8284 "\t\tsomeFunction(parameter1,\n" 8285 "\t\t\t parameter2);\n" 8286 "\t}\n" 8287 "};", 8288 Tab); 8289 verifyFormat("#define A \\\n" 8290 "\tvoid f() { \\\n" 8291 "\t\tsomeFunction( \\\n" 8292 "\t\t parameter1, \\\n" 8293 "\t\t parameter2); \\\n" 8294 "\t}", 8295 Tab); 8296 8297 Tab.TabWidth = 4; 8298 Tab.IndentWidth = 8; 8299 verifyFormat("class TabWidth4Indent8 {\n" 8300 "\t\tvoid f() {\n" 8301 "\t\t\t\tsomeFunction(parameter1,\n" 8302 "\t\t\t\t\t\t\t parameter2);\n" 8303 "\t\t}\n" 8304 "};", 8305 Tab); 8306 8307 Tab.TabWidth = 4; 8308 Tab.IndentWidth = 4; 8309 verifyFormat("class TabWidth4Indent4 {\n" 8310 "\tvoid f() {\n" 8311 "\t\tsomeFunction(parameter1,\n" 8312 "\t\t\t\t\t parameter2);\n" 8313 "\t}\n" 8314 "};", 8315 Tab); 8316 8317 Tab.TabWidth = 8; 8318 Tab.IndentWidth = 4; 8319 verifyFormat("class TabWidth8Indent4 {\n" 8320 " void f() {\n" 8321 "\tsomeFunction(parameter1,\n" 8322 "\t\t parameter2);\n" 8323 " }\n" 8324 "};", 8325 Tab); 8326 8327 Tab.TabWidth = 8; 8328 Tab.IndentWidth = 8; 8329 EXPECT_EQ("/*\n" 8330 "\t a\t\tcomment\n" 8331 "\t in multiple lines\n" 8332 " */", 8333 format(" /*\t \t \n" 8334 " \t \t a\t\tcomment\t \t\n" 8335 " \t \t in multiple lines\t\n" 8336 " \t */", 8337 Tab)); 8338 8339 Tab.UseTab = FormatStyle::UT_ForIndentation; 8340 verifyFormat("{\n" 8341 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8342 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8343 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8344 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8345 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8346 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8347 "};", 8348 Tab); 8349 verifyFormat("enum AA {\n" 8350 "\ta1, // Force multiple lines\n" 8351 "\ta2,\n" 8352 "\ta3\n" 8353 "};", 8354 Tab); 8355 EXPECT_EQ("if (aaaaaaaa && // q\n" 8356 " bb) // w\n" 8357 "\t;", 8358 format("if (aaaaaaaa &&// q\n" 8359 "bb)// w\n" 8360 ";", 8361 Tab)); 8362 verifyFormat("class X {\n" 8363 "\tvoid f() {\n" 8364 "\t\tsomeFunction(parameter1,\n" 8365 "\t\t parameter2);\n" 8366 "\t}\n" 8367 "};", 8368 Tab); 8369 verifyFormat("{\n" 8370 "\tQ(\n" 8371 "\t {\n" 8372 "\t\t int a;\n" 8373 "\t\t someFunction(aaaaaaaa,\n" 8374 "\t\t bbbbbbb);\n" 8375 "\t },\n" 8376 "\t p);\n" 8377 "}", 8378 Tab); 8379 EXPECT_EQ("{\n" 8380 "\t/* aaaa\n" 8381 "\t bbbb */\n" 8382 "}", 8383 format("{\n" 8384 "/* aaaa\n" 8385 " bbbb */\n" 8386 "}", 8387 Tab)); 8388 EXPECT_EQ("{\n" 8389 "\t/*\n" 8390 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8391 "\t bbbbbbbbbbbbb\n" 8392 "\t*/\n" 8393 "}", 8394 format("{\n" 8395 "/*\n" 8396 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8397 "*/\n" 8398 "}", 8399 Tab)); 8400 EXPECT_EQ("{\n" 8401 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8402 "\t// bbbbbbbbbbbbb\n" 8403 "}", 8404 format("{\n" 8405 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8406 "}", 8407 Tab)); 8408 EXPECT_EQ("{\n" 8409 "\t/*\n" 8410 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8411 "\t bbbbbbbbbbbbb\n" 8412 "\t*/\n" 8413 "}", 8414 format("{\n" 8415 "\t/*\n" 8416 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8417 "\t*/\n" 8418 "}", 8419 Tab)); 8420 EXPECT_EQ("{\n" 8421 "\t/*\n" 8422 "\n" 8423 "\t*/\n" 8424 "}", 8425 format("{\n" 8426 "\t/*\n" 8427 "\n" 8428 "\t*/\n" 8429 "}", 8430 Tab)); 8431 EXPECT_EQ("{\n" 8432 "\t/*\n" 8433 " asdf\n" 8434 "\t*/\n" 8435 "}", 8436 format("{\n" 8437 "\t/*\n" 8438 " asdf\n" 8439 "\t*/\n" 8440 "}", 8441 Tab)); 8442 8443 Tab.UseTab = FormatStyle::UT_Never; 8444 EXPECT_EQ("/*\n" 8445 " a\t\tcomment\n" 8446 " in multiple lines\n" 8447 " */", 8448 format(" /*\t \t \n" 8449 " \t \t a\t\tcomment\t \t\n" 8450 " \t \t in multiple lines\t\n" 8451 " \t */", 8452 Tab)); 8453 EXPECT_EQ("/* some\n" 8454 " comment */", 8455 format(" \t \t /* some\n" 8456 " \t \t comment */", 8457 Tab)); 8458 EXPECT_EQ("int a; /* some\n" 8459 " comment */", 8460 format(" \t \t int a; /* some\n" 8461 " \t \t comment */", 8462 Tab)); 8463 8464 EXPECT_EQ("int a; /* some\n" 8465 "comment */", 8466 format(" \t \t int\ta; /* some\n" 8467 " \t \t comment */", 8468 Tab)); 8469 EXPECT_EQ("f(\"\t\t\"); /* some\n" 8470 " comment */", 8471 format(" \t \t f(\"\t\t\"); /* some\n" 8472 " \t \t comment */", 8473 Tab)); 8474 EXPECT_EQ("{\n" 8475 " /*\n" 8476 " * Comment\n" 8477 " */\n" 8478 " int i;\n" 8479 "}", 8480 format("{\n" 8481 "\t/*\n" 8482 "\t * Comment\n" 8483 "\t */\n" 8484 "\t int i;\n" 8485 "}")); 8486 8487 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 8488 Tab.TabWidth = 8; 8489 Tab.IndentWidth = 8; 8490 EXPECT_EQ("if (aaaaaaaa && // q\n" 8491 " bb) // w\n" 8492 "\t;", 8493 format("if (aaaaaaaa &&// q\n" 8494 "bb)// w\n" 8495 ";", 8496 Tab)); 8497 EXPECT_EQ("if (aaa && bbb) // w\n" 8498 "\t;", 8499 format("if(aaa&&bbb)// w\n" 8500 ";", 8501 Tab)); 8502 verifyFormat("class X {\n" 8503 "\tvoid f() {\n" 8504 "\t\tsomeFunction(parameter1,\n" 8505 "\t\t\t parameter2);\n" 8506 "\t}\n" 8507 "};", 8508 Tab); 8509 verifyFormat("#define A \\\n" 8510 "\tvoid f() { \\\n" 8511 "\t\tsomeFunction( \\\n" 8512 "\t\t parameter1, \\\n" 8513 "\t\t parameter2); \\\n" 8514 "\t}", 8515 Tab); 8516 Tab.TabWidth = 4; 8517 Tab.IndentWidth = 8; 8518 verifyFormat("class TabWidth4Indent8 {\n" 8519 "\t\tvoid f() {\n" 8520 "\t\t\t\tsomeFunction(parameter1,\n" 8521 "\t\t\t\t\t\t\t parameter2);\n" 8522 "\t\t}\n" 8523 "};", 8524 Tab); 8525 Tab.TabWidth = 4; 8526 Tab.IndentWidth = 4; 8527 verifyFormat("class TabWidth4Indent4 {\n" 8528 "\tvoid f() {\n" 8529 "\t\tsomeFunction(parameter1,\n" 8530 "\t\t\t\t\t parameter2);\n" 8531 "\t}\n" 8532 "};", 8533 Tab); 8534 Tab.TabWidth = 8; 8535 Tab.IndentWidth = 4; 8536 verifyFormat("class TabWidth8Indent4 {\n" 8537 " void f() {\n" 8538 "\tsomeFunction(parameter1,\n" 8539 "\t\t parameter2);\n" 8540 " }\n" 8541 "};", 8542 Tab); 8543 Tab.TabWidth = 8; 8544 Tab.IndentWidth = 8; 8545 EXPECT_EQ("/*\n" 8546 "\t a\t\tcomment\n" 8547 "\t in multiple lines\n" 8548 " */", 8549 format(" /*\t \t \n" 8550 " \t \t a\t\tcomment\t \t\n" 8551 " \t \t in multiple lines\t\n" 8552 " \t */", 8553 Tab)); 8554 verifyFormat("{\n" 8555 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8556 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8557 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8558 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8559 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8560 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8561 "};", 8562 Tab); 8563 verifyFormat("enum AA {\n" 8564 "\ta1, // Force multiple lines\n" 8565 "\ta2,\n" 8566 "\ta3\n" 8567 "};", 8568 Tab); 8569 EXPECT_EQ("if (aaaaaaaa && // q\n" 8570 " bb) // w\n" 8571 "\t;", 8572 format("if (aaaaaaaa &&// q\n" 8573 "bb)// w\n" 8574 ";", 8575 Tab)); 8576 verifyFormat("class X {\n" 8577 "\tvoid f() {\n" 8578 "\t\tsomeFunction(parameter1,\n" 8579 "\t\t\t parameter2);\n" 8580 "\t}\n" 8581 "};", 8582 Tab); 8583 verifyFormat("{\n" 8584 "\tQ(\n" 8585 "\t {\n" 8586 "\t\t int a;\n" 8587 "\t\t someFunction(aaaaaaaa,\n" 8588 "\t\t\t\t bbbbbbb);\n" 8589 "\t },\n" 8590 "\t p);\n" 8591 "}", 8592 Tab); 8593 EXPECT_EQ("{\n" 8594 "\t/* aaaa\n" 8595 "\t bbbb */\n" 8596 "}", 8597 format("{\n" 8598 "/* aaaa\n" 8599 " bbbb */\n" 8600 "}", 8601 Tab)); 8602 EXPECT_EQ("{\n" 8603 "\t/*\n" 8604 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8605 "\t bbbbbbbbbbbbb\n" 8606 "\t*/\n" 8607 "}", 8608 format("{\n" 8609 "/*\n" 8610 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8611 "*/\n" 8612 "}", 8613 Tab)); 8614 EXPECT_EQ("{\n" 8615 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8616 "\t// bbbbbbbbbbbbb\n" 8617 "}", 8618 format("{\n" 8619 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8620 "}", 8621 Tab)); 8622 EXPECT_EQ("{\n" 8623 "\t/*\n" 8624 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8625 "\t bbbbbbbbbbbbb\n" 8626 "\t*/\n" 8627 "}", 8628 format("{\n" 8629 "\t/*\n" 8630 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8631 "\t*/\n" 8632 "}", 8633 Tab)); 8634 EXPECT_EQ("{\n" 8635 "\t/*\n" 8636 "\n" 8637 "\t*/\n" 8638 "}", 8639 format("{\n" 8640 "\t/*\n" 8641 "\n" 8642 "\t*/\n" 8643 "}", 8644 Tab)); 8645 EXPECT_EQ("{\n" 8646 "\t/*\n" 8647 " asdf\n" 8648 "\t*/\n" 8649 "}", 8650 format("{\n" 8651 "\t/*\n" 8652 " asdf\n" 8653 "\t*/\n" 8654 "}", 8655 Tab)); 8656 EXPECT_EQ("/*\n" 8657 "\t a\t\tcomment\n" 8658 "\t in multiple lines\n" 8659 " */", 8660 format(" /*\t \t \n" 8661 " \t \t a\t\tcomment\t \t\n" 8662 " \t \t in multiple lines\t\n" 8663 " \t */", 8664 Tab)); 8665 EXPECT_EQ("/* some\n" 8666 " comment */", 8667 format(" \t \t /* some\n" 8668 " \t \t comment */", 8669 Tab)); 8670 EXPECT_EQ("int a; /* some\n" 8671 " comment */", 8672 format(" \t \t int a; /* some\n" 8673 " \t \t comment */", 8674 Tab)); 8675 EXPECT_EQ("int a; /* some\n" 8676 "comment */", 8677 format(" \t \t int\ta; /* some\n" 8678 " \t \t comment */", 8679 Tab)); 8680 EXPECT_EQ("f(\"\t\t\"); /* some\n" 8681 " comment */", 8682 format(" \t \t f(\"\t\t\"); /* some\n" 8683 " \t \t comment */", 8684 Tab)); 8685 EXPECT_EQ("{\n" 8686 " /*\n" 8687 " * Comment\n" 8688 " */\n" 8689 " int i;\n" 8690 "}", 8691 format("{\n" 8692 "\t/*\n" 8693 "\t * Comment\n" 8694 "\t */\n" 8695 "\t int i;\n" 8696 "}")); 8697 Tab.AlignConsecutiveAssignments = true; 8698 Tab.AlignConsecutiveDeclarations = true; 8699 Tab.TabWidth = 4; 8700 Tab.IndentWidth = 4; 8701 verifyFormat("class Assign {\n" 8702 "\tvoid f() {\n" 8703 "\t\tint x = 123;\n" 8704 "\t\tint random = 4;\n" 8705 "\t\tstd::string alphabet =\n" 8706 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 8707 "\t}\n" 8708 "};", 8709 Tab); 8710 } 8711 8712 TEST_F(FormatTest, CalculatesOriginalColumn) { 8713 EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8714 "q\"; /* some\n" 8715 " comment */", 8716 format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8717 "q\"; /* some\n" 8718 " comment */", 8719 getLLVMStyle())); 8720 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 8721 "/* some\n" 8722 " comment */", 8723 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 8724 " /* some\n" 8725 " comment */", 8726 getLLVMStyle())); 8727 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8728 "qqq\n" 8729 "/* some\n" 8730 " comment */", 8731 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8732 "qqq\n" 8733 " /* some\n" 8734 " comment */", 8735 getLLVMStyle())); 8736 EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8737 "wwww; /* some\n" 8738 " comment */", 8739 format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8740 "wwww; /* some\n" 8741 " comment */", 8742 getLLVMStyle())); 8743 } 8744 8745 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { 8746 FormatStyle NoSpace = getLLVMStyle(); 8747 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never; 8748 8749 verifyFormat("while(true)\n" 8750 " continue;", 8751 NoSpace); 8752 verifyFormat("for(;;)\n" 8753 " continue;", 8754 NoSpace); 8755 verifyFormat("if(true)\n" 8756 " f();\n" 8757 "else if(true)\n" 8758 " f();", 8759 NoSpace); 8760 verifyFormat("do {\n" 8761 " do_something();\n" 8762 "} while(something());", 8763 NoSpace); 8764 verifyFormat("switch(x) {\n" 8765 "default:\n" 8766 " break;\n" 8767 "}", 8768 NoSpace); 8769 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace); 8770 verifyFormat("size_t x = sizeof(x);", NoSpace); 8771 verifyFormat("auto f(int x) -> decltype(x);", NoSpace); 8772 verifyFormat("int f(T x) noexcept(x.create());", NoSpace); 8773 verifyFormat("alignas(128) char a[128];", NoSpace); 8774 verifyFormat("size_t x = alignof(MyType);", NoSpace); 8775 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace); 8776 verifyFormat("int f() throw(Deprecated);", NoSpace); 8777 verifyFormat("typedef void (*cb)(int);", NoSpace); 8778 verifyFormat("T A::operator()();", NoSpace); 8779 verifyFormat("X A::operator++(T);", NoSpace); 8780 8781 FormatStyle Space = getLLVMStyle(); 8782 Space.SpaceBeforeParens = FormatStyle::SBPO_Always; 8783 8784 verifyFormat("int f ();", Space); 8785 verifyFormat("void f (int a, T b) {\n" 8786 " while (true)\n" 8787 " continue;\n" 8788 "}", 8789 Space); 8790 verifyFormat("if (true)\n" 8791 " f ();\n" 8792 "else if (true)\n" 8793 " f ();", 8794 Space); 8795 verifyFormat("do {\n" 8796 " do_something ();\n" 8797 "} while (something ());", 8798 Space); 8799 verifyFormat("switch (x) {\n" 8800 "default:\n" 8801 " break;\n" 8802 "}", 8803 Space); 8804 verifyFormat("A::A () : a (1) {}", Space); 8805 verifyFormat("void f () __attribute__ ((asdf));", Space); 8806 verifyFormat("*(&a + 1);\n" 8807 "&((&a)[1]);\n" 8808 "a[(b + c) * d];\n" 8809 "(((a + 1) * 2) + 3) * 4;", 8810 Space); 8811 verifyFormat("#define A(x) x", Space); 8812 verifyFormat("#define A (x) x", Space); 8813 verifyFormat("#if defined(x)\n" 8814 "#endif", 8815 Space); 8816 verifyFormat("auto i = std::make_unique<int> (5);", Space); 8817 verifyFormat("size_t x = sizeof (x);", Space); 8818 verifyFormat("auto f (int x) -> decltype (x);", Space); 8819 verifyFormat("int f (T x) noexcept (x.create ());", Space); 8820 verifyFormat("alignas (128) char a[128];", Space); 8821 verifyFormat("size_t x = alignof (MyType);", Space); 8822 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space); 8823 verifyFormat("int f () throw (Deprecated);", Space); 8824 verifyFormat("typedef void (*cb) (int);", Space); 8825 verifyFormat("T A::operator() ();", Space); 8826 verifyFormat("X A::operator++ (T);", Space); 8827 } 8828 8829 TEST_F(FormatTest, ConfigurableSpacesInParentheses) { 8830 FormatStyle Spaces = getLLVMStyle(); 8831 8832 Spaces.SpacesInParentheses = true; 8833 verifyFormat("do_something( ::globalVar );", Spaces); 8834 verifyFormat("call( x, y, z );", Spaces); 8835 verifyFormat("call();", Spaces); 8836 verifyFormat("std::function<void( int, int )> callback;", Spaces); 8837 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }", 8838 Spaces); 8839 verifyFormat("while ( (bool)1 )\n" 8840 " continue;", 8841 Spaces); 8842 verifyFormat("for ( ;; )\n" 8843 " continue;", 8844 Spaces); 8845 verifyFormat("if ( true )\n" 8846 " f();\n" 8847 "else if ( true )\n" 8848 " f();", 8849 Spaces); 8850 verifyFormat("do {\n" 8851 " do_something( (int)i );\n" 8852 "} while ( something() );", 8853 Spaces); 8854 verifyFormat("switch ( x ) {\n" 8855 "default:\n" 8856 " break;\n" 8857 "}", 8858 Spaces); 8859 8860 Spaces.SpacesInParentheses = false; 8861 Spaces.SpacesInCStyleCastParentheses = true; 8862 verifyFormat("Type *A = ( Type * )P;", Spaces); 8863 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces); 8864 verifyFormat("x = ( int32 )y;", Spaces); 8865 verifyFormat("int a = ( int )(2.0f);", Spaces); 8866 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces); 8867 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces); 8868 verifyFormat("#define x (( int )-1)", Spaces); 8869 8870 // Run the first set of tests again with: 8871 Spaces.SpacesInParentheses = false; 8872 Spaces.SpaceInEmptyParentheses = true; 8873 Spaces.SpacesInCStyleCastParentheses = true; 8874 verifyFormat("call(x, y, z);", Spaces); 8875 verifyFormat("call( );", Spaces); 8876 verifyFormat("std::function<void(int, int)> callback;", Spaces); 8877 verifyFormat("while (( bool )1)\n" 8878 " continue;", 8879 Spaces); 8880 verifyFormat("for (;;)\n" 8881 " continue;", 8882 Spaces); 8883 verifyFormat("if (true)\n" 8884 " f( );\n" 8885 "else if (true)\n" 8886 " f( );", 8887 Spaces); 8888 verifyFormat("do {\n" 8889 " do_something(( int )i);\n" 8890 "} while (something( ));", 8891 Spaces); 8892 verifyFormat("switch (x) {\n" 8893 "default:\n" 8894 " break;\n" 8895 "}", 8896 Spaces); 8897 8898 // Run the first set of tests again with: 8899 Spaces.SpaceAfterCStyleCast = true; 8900 verifyFormat("call(x, y, z);", Spaces); 8901 verifyFormat("call( );", Spaces); 8902 verifyFormat("std::function<void(int, int)> callback;", Spaces); 8903 verifyFormat("while (( bool ) 1)\n" 8904 " continue;", 8905 Spaces); 8906 verifyFormat("for (;;)\n" 8907 " continue;", 8908 Spaces); 8909 verifyFormat("if (true)\n" 8910 " f( );\n" 8911 "else if (true)\n" 8912 " f( );", 8913 Spaces); 8914 verifyFormat("do {\n" 8915 " do_something(( int ) i);\n" 8916 "} while (something( ));", 8917 Spaces); 8918 verifyFormat("switch (x) {\n" 8919 "default:\n" 8920 " break;\n" 8921 "}", 8922 Spaces); 8923 8924 // Run subset of tests again with: 8925 Spaces.SpacesInCStyleCastParentheses = false; 8926 Spaces.SpaceAfterCStyleCast = true; 8927 verifyFormat("while ((bool) 1)\n" 8928 " continue;", 8929 Spaces); 8930 verifyFormat("do {\n" 8931 " do_something((int) i);\n" 8932 "} while (something( ));", 8933 Spaces); 8934 } 8935 8936 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) { 8937 verifyFormat("int a[5];"); 8938 verifyFormat("a[3] += 42;"); 8939 8940 FormatStyle Spaces = getLLVMStyle(); 8941 Spaces.SpacesInSquareBrackets = true; 8942 // Lambdas unchanged. 8943 verifyFormat("int c = []() -> int { return 2; }();\n", Spaces); 8944 verifyFormat("return [i, args...] {};", Spaces); 8945 8946 // Not lambdas. 8947 verifyFormat("int a[ 5 ];", Spaces); 8948 verifyFormat("a[ 3 ] += 42;", Spaces); 8949 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces); 8950 verifyFormat("double &operator[](int i) { return 0; }\n" 8951 "int i;", 8952 Spaces); 8953 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces); 8954 verifyFormat("int i = a[ a ][ a ]->f();", Spaces); 8955 verifyFormat("int i = (*b)[ a ]->f();", Spaces); 8956 } 8957 8958 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) { 8959 verifyFormat("int a = 5;"); 8960 verifyFormat("a += 42;"); 8961 verifyFormat("a or_eq 8;"); 8962 8963 FormatStyle Spaces = getLLVMStyle(); 8964 Spaces.SpaceBeforeAssignmentOperators = false; 8965 verifyFormat("int a= 5;", Spaces); 8966 verifyFormat("a+= 42;", Spaces); 8967 verifyFormat("a or_eq 8;", Spaces); 8968 } 8969 8970 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) { 8971 verifyFormat("class Foo : public Bar {};"); 8972 verifyFormat("Foo::Foo() : foo(1) {}"); 8973 verifyFormat("for (auto a : b) {\n}"); 8974 verifyFormat("int x = a ? b : c;"); 8975 verifyFormat("{\n" 8976 "label0:\n" 8977 " int x = 0;\n" 8978 "}"); 8979 verifyFormat("switch (x) {\n" 8980 "case 1:\n" 8981 "default:\n" 8982 "}"); 8983 8984 FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30); 8985 CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false; 8986 verifyFormat("class Foo : public Bar {};", CtorInitializerStyle); 8987 verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle); 8988 verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle); 8989 verifyFormat("int x = a ? b : c;", CtorInitializerStyle); 8990 verifyFormat("{\n" 8991 "label1:\n" 8992 " int x = 0;\n" 8993 "}", 8994 CtorInitializerStyle); 8995 verifyFormat("switch (x) {\n" 8996 "case 1:\n" 8997 "default:\n" 8998 "}", 8999 CtorInitializerStyle); 9000 CtorInitializerStyle.BreakConstructorInitializers = 9001 FormatStyle::BCIS_AfterColon; 9002 verifyFormat("Fooooooooooo::Fooooooooooo():\n" 9003 " aaaaaaaaaaaaaaaa(1),\n" 9004 " bbbbbbbbbbbbbbbb(2) {}", 9005 CtorInitializerStyle); 9006 CtorInitializerStyle.BreakConstructorInitializers = 9007 FormatStyle::BCIS_BeforeComma; 9008 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 9009 " : aaaaaaaaaaaaaaaa(1)\n" 9010 " , bbbbbbbbbbbbbbbb(2) {}", 9011 CtorInitializerStyle); 9012 CtorInitializerStyle.BreakConstructorInitializers = 9013 FormatStyle::BCIS_BeforeColon; 9014 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 9015 " : aaaaaaaaaaaaaaaa(1),\n" 9016 " bbbbbbbbbbbbbbbb(2) {}", 9017 CtorInitializerStyle); 9018 CtorInitializerStyle.ConstructorInitializerIndentWidth = 0; 9019 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 9020 ": aaaaaaaaaaaaaaaa(1),\n" 9021 " bbbbbbbbbbbbbbbb(2) {}", 9022 CtorInitializerStyle); 9023 9024 FormatStyle InheritanceStyle = getLLVMStyle(); 9025 InheritanceStyle.SpaceBeforeInheritanceColon = false; 9026 verifyFormat("class Foo: public Bar {};", InheritanceStyle); 9027 verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle); 9028 verifyFormat("for (auto a : b) {\n}", InheritanceStyle); 9029 verifyFormat("int x = a ? b : c;", InheritanceStyle); 9030 verifyFormat("{\n" 9031 "label2:\n" 9032 " int x = 0;\n" 9033 "}", 9034 InheritanceStyle); 9035 verifyFormat("switch (x) {\n" 9036 "case 1:\n" 9037 "default:\n" 9038 "}", 9039 InheritanceStyle); 9040 9041 FormatStyle ForLoopStyle = getLLVMStyle(); 9042 ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false; 9043 verifyFormat("class Foo : public Bar {};", ForLoopStyle); 9044 verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle); 9045 verifyFormat("for (auto a: b) {\n}", ForLoopStyle); 9046 verifyFormat("int x = a ? b : c;", ForLoopStyle); 9047 verifyFormat("{\n" 9048 "label2:\n" 9049 " int x = 0;\n" 9050 "}", 9051 ForLoopStyle); 9052 verifyFormat("switch (x) {\n" 9053 "case 1:\n" 9054 "default:\n" 9055 "}", 9056 ForLoopStyle); 9057 9058 FormatStyle NoSpaceStyle = getLLVMStyle(); 9059 NoSpaceStyle.SpaceBeforeCtorInitializerColon = false; 9060 NoSpaceStyle.SpaceBeforeInheritanceColon = false; 9061 NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false; 9062 verifyFormat("class Foo: public Bar {};", NoSpaceStyle); 9063 verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle); 9064 verifyFormat("for (auto a: b) {\n}", NoSpaceStyle); 9065 verifyFormat("int x = a ? b : c;", NoSpaceStyle); 9066 verifyFormat("{\n" 9067 "label3:\n" 9068 " int x = 0;\n" 9069 "}", 9070 NoSpaceStyle); 9071 verifyFormat("switch (x) {\n" 9072 "case 1:\n" 9073 "default:\n" 9074 "}", 9075 NoSpaceStyle); 9076 } 9077 9078 TEST_F(FormatTest, AlignConsecutiveAssignments) { 9079 FormatStyle Alignment = getLLVMStyle(); 9080 Alignment.AlignConsecutiveAssignments = false; 9081 verifyFormat("int a = 5;\n" 9082 "int oneTwoThree = 123;", 9083 Alignment); 9084 verifyFormat("int a = 5;\n" 9085 "int oneTwoThree = 123;", 9086 Alignment); 9087 9088 Alignment.AlignConsecutiveAssignments = true; 9089 verifyFormat("int a = 5;\n" 9090 "int oneTwoThree = 123;", 9091 Alignment); 9092 verifyFormat("int a = method();\n" 9093 "int oneTwoThree = 133;", 9094 Alignment); 9095 verifyFormat("a &= 5;\n" 9096 "bcd *= 5;\n" 9097 "ghtyf += 5;\n" 9098 "dvfvdb -= 5;\n" 9099 "a /= 5;\n" 9100 "vdsvsv %= 5;\n" 9101 "sfdbddfbdfbb ^= 5;\n" 9102 "dvsdsv |= 5;\n" 9103 "int dsvvdvsdvvv = 123;", 9104 Alignment); 9105 verifyFormat("int i = 1, j = 10;\n" 9106 "something = 2000;", 9107 Alignment); 9108 verifyFormat("something = 2000;\n" 9109 "int i = 1, j = 10;\n", 9110 Alignment); 9111 verifyFormat("something = 2000;\n" 9112 "another = 911;\n" 9113 "int i = 1, j = 10;\n" 9114 "oneMore = 1;\n" 9115 "i = 2;", 9116 Alignment); 9117 verifyFormat("int a = 5;\n" 9118 "int one = 1;\n" 9119 "method();\n" 9120 "int oneTwoThree = 123;\n" 9121 "int oneTwo = 12;", 9122 Alignment); 9123 verifyFormat("int oneTwoThree = 123;\n" 9124 "int oneTwo = 12;\n" 9125 "method();\n", 9126 Alignment); 9127 verifyFormat("int oneTwoThree = 123; // comment\n" 9128 "int oneTwo = 12; // comment", 9129 Alignment); 9130 EXPECT_EQ("int a = 5;\n" 9131 "\n" 9132 "int oneTwoThree = 123;", 9133 format("int a = 5;\n" 9134 "\n" 9135 "int oneTwoThree= 123;", 9136 Alignment)); 9137 EXPECT_EQ("int a = 5;\n" 9138 "int one = 1;\n" 9139 "\n" 9140 "int oneTwoThree = 123;", 9141 format("int a = 5;\n" 9142 "int one = 1;\n" 9143 "\n" 9144 "int oneTwoThree = 123;", 9145 Alignment)); 9146 EXPECT_EQ("int a = 5;\n" 9147 "int one = 1;\n" 9148 "\n" 9149 "int oneTwoThree = 123;\n" 9150 "int oneTwo = 12;", 9151 format("int a = 5;\n" 9152 "int one = 1;\n" 9153 "\n" 9154 "int oneTwoThree = 123;\n" 9155 "int oneTwo = 12;", 9156 Alignment)); 9157 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 9158 verifyFormat("#define A \\\n" 9159 " int aaaa = 12; \\\n" 9160 " int b = 23; \\\n" 9161 " int ccc = 234; \\\n" 9162 " int dddddddddd = 2345;", 9163 Alignment); 9164 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 9165 verifyFormat("#define A \\\n" 9166 " int aaaa = 12; \\\n" 9167 " int b = 23; \\\n" 9168 " int ccc = 234; \\\n" 9169 " int dddddddddd = 2345;", 9170 Alignment); 9171 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 9172 verifyFormat("#define A " 9173 " \\\n" 9174 " int aaaa = 12; " 9175 " \\\n" 9176 " int b = 23; " 9177 " \\\n" 9178 " int ccc = 234; " 9179 " \\\n" 9180 " int dddddddddd = 2345;", 9181 Alignment); 9182 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 9183 "k = 4, int l = 5,\n" 9184 " int m = 6) {\n" 9185 " int j = 10;\n" 9186 " otherThing = 1;\n" 9187 "}", 9188 Alignment); 9189 verifyFormat("void SomeFunction(int parameter = 0) {\n" 9190 " int i = 1;\n" 9191 " int j = 2;\n" 9192 " int big = 10000;\n" 9193 "}", 9194 Alignment); 9195 verifyFormat("class C {\n" 9196 "public:\n" 9197 " int i = 1;\n" 9198 " virtual void f() = 0;\n" 9199 "};", 9200 Alignment); 9201 verifyFormat("int i = 1;\n" 9202 "if (SomeType t = getSomething()) {\n" 9203 "}\n" 9204 "int j = 2;\n" 9205 "int big = 10000;", 9206 Alignment); 9207 verifyFormat("int j = 7;\n" 9208 "for (int k = 0; k < N; ++k) {\n" 9209 "}\n" 9210 "int j = 2;\n" 9211 "int big = 10000;\n" 9212 "}", 9213 Alignment); 9214 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 9215 verifyFormat("int i = 1;\n" 9216 "LooooooooooongType loooooooooooooooooooooongVariable\n" 9217 " = someLooooooooooooooooongFunction();\n" 9218 "int j = 2;", 9219 Alignment); 9220 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 9221 verifyFormat("int i = 1;\n" 9222 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 9223 " someLooooooooooooooooongFunction();\n" 9224 "int j = 2;", 9225 Alignment); 9226 9227 verifyFormat("auto lambda = []() {\n" 9228 " auto i = 0;\n" 9229 " return 0;\n" 9230 "};\n" 9231 "int i = 0;\n" 9232 "auto v = type{\n" 9233 " i = 1, //\n" 9234 " (i = 2), //\n" 9235 " i = 3 //\n" 9236 "};", 9237 Alignment); 9238 9239 verifyFormat( 9240 "int i = 1;\n" 9241 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 9242 " loooooooooooooooooooooongParameterB);\n" 9243 "int j = 2;", 9244 Alignment); 9245 9246 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 9247 " typename B = very_long_type_name_1,\n" 9248 " typename T_2 = very_long_type_name_2>\n" 9249 "auto foo() {}\n", 9250 Alignment); 9251 verifyFormat("int a, b = 1;\n" 9252 "int c = 2;\n" 9253 "int dd = 3;\n", 9254 Alignment); 9255 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 9256 "float b[1][] = {{3.f}};\n", 9257 Alignment); 9258 verifyFormat("for (int i = 0; i < 1; i++)\n" 9259 " int x = 1;\n", 9260 Alignment); 9261 verifyFormat("for (i = 0; i < 1; i++)\n" 9262 " x = 1;\n" 9263 "y = 1;\n", 9264 Alignment); 9265 } 9266 9267 TEST_F(FormatTest, AlignConsecutiveDeclarations) { 9268 FormatStyle Alignment = getLLVMStyle(); 9269 Alignment.AlignConsecutiveDeclarations = false; 9270 verifyFormat("float const a = 5;\n" 9271 "int oneTwoThree = 123;", 9272 Alignment); 9273 verifyFormat("int a = 5;\n" 9274 "float const oneTwoThree = 123;", 9275 Alignment); 9276 9277 Alignment.AlignConsecutiveDeclarations = true; 9278 verifyFormat("float const a = 5;\n" 9279 "int oneTwoThree = 123;", 9280 Alignment); 9281 verifyFormat("int a = method();\n" 9282 "float const oneTwoThree = 133;", 9283 Alignment); 9284 verifyFormat("int i = 1, j = 10;\n" 9285 "something = 2000;", 9286 Alignment); 9287 verifyFormat("something = 2000;\n" 9288 "int i = 1, j = 10;\n", 9289 Alignment); 9290 verifyFormat("float something = 2000;\n" 9291 "double another = 911;\n" 9292 "int i = 1, j = 10;\n" 9293 "const int *oneMore = 1;\n" 9294 "unsigned i = 2;", 9295 Alignment); 9296 verifyFormat("float a = 5;\n" 9297 "int one = 1;\n" 9298 "method();\n" 9299 "const double oneTwoThree = 123;\n" 9300 "const unsigned int oneTwo = 12;", 9301 Alignment); 9302 verifyFormat("int oneTwoThree{0}; // comment\n" 9303 "unsigned oneTwo; // comment", 9304 Alignment); 9305 EXPECT_EQ("float const a = 5;\n" 9306 "\n" 9307 "int oneTwoThree = 123;", 9308 format("float const a = 5;\n" 9309 "\n" 9310 "int oneTwoThree= 123;", 9311 Alignment)); 9312 EXPECT_EQ("float a = 5;\n" 9313 "int one = 1;\n" 9314 "\n" 9315 "unsigned oneTwoThree = 123;", 9316 format("float a = 5;\n" 9317 "int one = 1;\n" 9318 "\n" 9319 "unsigned oneTwoThree = 123;", 9320 Alignment)); 9321 EXPECT_EQ("float a = 5;\n" 9322 "int one = 1;\n" 9323 "\n" 9324 "unsigned oneTwoThree = 123;\n" 9325 "int oneTwo = 12;", 9326 format("float a = 5;\n" 9327 "int one = 1;\n" 9328 "\n" 9329 "unsigned oneTwoThree = 123;\n" 9330 "int oneTwo = 12;", 9331 Alignment)); 9332 // Function prototype alignment 9333 verifyFormat("int a();\n" 9334 "double b();", 9335 Alignment); 9336 verifyFormat("int a(int x);\n" 9337 "double b();", 9338 Alignment); 9339 unsigned OldColumnLimit = Alignment.ColumnLimit; 9340 // We need to set ColumnLimit to zero, in order to stress nested alignments, 9341 // otherwise the function parameters will be re-flowed onto a single line. 9342 Alignment.ColumnLimit = 0; 9343 EXPECT_EQ("int a(int x,\n" 9344 " float y);\n" 9345 "double b(int x,\n" 9346 " double y);", 9347 format("int a(int x,\n" 9348 " float y);\n" 9349 "double b(int x,\n" 9350 " double y);", 9351 Alignment)); 9352 // This ensures that function parameters of function declarations are 9353 // correctly indented when their owning functions are indented. 9354 // The failure case here is for 'double y' to not be indented enough. 9355 EXPECT_EQ("double a(int x);\n" 9356 "int b(int y,\n" 9357 " double z);", 9358 format("double a(int x);\n" 9359 "int b(int y,\n" 9360 " double z);", 9361 Alignment)); 9362 // Set ColumnLimit low so that we induce wrapping immediately after 9363 // the function name and opening paren. 9364 Alignment.ColumnLimit = 13; 9365 verifyFormat("int function(\n" 9366 " int x,\n" 9367 " bool y);", 9368 Alignment); 9369 Alignment.ColumnLimit = OldColumnLimit; 9370 // Ensure function pointers don't screw up recursive alignment 9371 verifyFormat("int a(int x, void (*fp)(int y));\n" 9372 "double b();", 9373 Alignment); 9374 Alignment.AlignConsecutiveAssignments = true; 9375 // Ensure recursive alignment is broken by function braces, so that the 9376 // "a = 1" does not align with subsequent assignments inside the function 9377 // body. 9378 verifyFormat("int func(int a = 1) {\n" 9379 " int b = 2;\n" 9380 " int cc = 3;\n" 9381 "}", 9382 Alignment); 9383 verifyFormat("float something = 2000;\n" 9384 "double another = 911;\n" 9385 "int i = 1, j = 10;\n" 9386 "const int *oneMore = 1;\n" 9387 "unsigned i = 2;", 9388 Alignment); 9389 verifyFormat("int oneTwoThree = {0}; // comment\n" 9390 "unsigned oneTwo = 0; // comment", 9391 Alignment); 9392 // Make sure that scope is correctly tracked, in the absence of braces 9393 verifyFormat("for (int i = 0; i < n; i++)\n" 9394 " j = i;\n" 9395 "double x = 1;\n", 9396 Alignment); 9397 verifyFormat("if (int i = 0)\n" 9398 " j = i;\n" 9399 "double x = 1;\n", 9400 Alignment); 9401 // Ensure operator[] and operator() are comprehended 9402 verifyFormat("struct test {\n" 9403 " long long int foo();\n" 9404 " int operator[](int a);\n" 9405 " double bar();\n" 9406 "};\n", 9407 Alignment); 9408 verifyFormat("struct test {\n" 9409 " long long int foo();\n" 9410 " int operator()(int a);\n" 9411 " double bar();\n" 9412 "};\n", 9413 Alignment); 9414 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 9415 " int const i = 1;\n" 9416 " int * j = 2;\n" 9417 " int big = 10000;\n" 9418 "\n" 9419 " unsigned oneTwoThree = 123;\n" 9420 " int oneTwo = 12;\n" 9421 " method();\n" 9422 " float k = 2;\n" 9423 " int ll = 10000;\n" 9424 "}", 9425 format("void SomeFunction(int parameter= 0) {\n" 9426 " int const i= 1;\n" 9427 " int *j=2;\n" 9428 " int big = 10000;\n" 9429 "\n" 9430 "unsigned oneTwoThree =123;\n" 9431 "int oneTwo = 12;\n" 9432 " method();\n" 9433 "float k= 2;\n" 9434 "int ll=10000;\n" 9435 "}", 9436 Alignment)); 9437 Alignment.AlignConsecutiveAssignments = false; 9438 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 9439 verifyFormat("#define A \\\n" 9440 " int aaaa = 12; \\\n" 9441 " float b = 23; \\\n" 9442 " const int ccc = 234; \\\n" 9443 " unsigned dddddddddd = 2345;", 9444 Alignment); 9445 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 9446 verifyFormat("#define A \\\n" 9447 " int aaaa = 12; \\\n" 9448 " float b = 23; \\\n" 9449 " const int ccc = 234; \\\n" 9450 " unsigned dddddddddd = 2345;", 9451 Alignment); 9452 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 9453 Alignment.ColumnLimit = 30; 9454 verifyFormat("#define A \\\n" 9455 " int aaaa = 12; \\\n" 9456 " float b = 23; \\\n" 9457 " const int ccc = 234; \\\n" 9458 " int dddddddddd = 2345;", 9459 Alignment); 9460 Alignment.ColumnLimit = 80; 9461 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 9462 "k = 4, int l = 5,\n" 9463 " int m = 6) {\n" 9464 " const int j = 10;\n" 9465 " otherThing = 1;\n" 9466 "}", 9467 Alignment); 9468 verifyFormat("void SomeFunction(int parameter = 0) {\n" 9469 " int const i = 1;\n" 9470 " int * j = 2;\n" 9471 " int big = 10000;\n" 9472 "}", 9473 Alignment); 9474 verifyFormat("class C {\n" 9475 "public:\n" 9476 " int i = 1;\n" 9477 " virtual void f() = 0;\n" 9478 "};", 9479 Alignment); 9480 verifyFormat("float i = 1;\n" 9481 "if (SomeType t = getSomething()) {\n" 9482 "}\n" 9483 "const unsigned j = 2;\n" 9484 "int big = 10000;", 9485 Alignment); 9486 verifyFormat("float j = 7;\n" 9487 "for (int k = 0; k < N; ++k) {\n" 9488 "}\n" 9489 "unsigned j = 2;\n" 9490 "int big = 10000;\n" 9491 "}", 9492 Alignment); 9493 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 9494 verifyFormat("float i = 1;\n" 9495 "LooooooooooongType loooooooooooooooooooooongVariable\n" 9496 " = someLooooooooooooooooongFunction();\n" 9497 "int j = 2;", 9498 Alignment); 9499 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 9500 verifyFormat("int i = 1;\n" 9501 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 9502 " someLooooooooooooooooongFunction();\n" 9503 "int j = 2;", 9504 Alignment); 9505 9506 Alignment.AlignConsecutiveAssignments = true; 9507 verifyFormat("auto lambda = []() {\n" 9508 " auto ii = 0;\n" 9509 " float j = 0;\n" 9510 " return 0;\n" 9511 "};\n" 9512 "int i = 0;\n" 9513 "float i2 = 0;\n" 9514 "auto v = type{\n" 9515 " i = 1, //\n" 9516 " (i = 2), //\n" 9517 " i = 3 //\n" 9518 "};", 9519 Alignment); 9520 Alignment.AlignConsecutiveAssignments = false; 9521 9522 verifyFormat( 9523 "int i = 1;\n" 9524 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 9525 " loooooooooooooooooooooongParameterB);\n" 9526 "int j = 2;", 9527 Alignment); 9528 9529 // Test interactions with ColumnLimit and AlignConsecutiveAssignments: 9530 // We expect declarations and assignments to align, as long as it doesn't 9531 // exceed the column limit, starting a new alignment sequence whenever it 9532 // happens. 9533 Alignment.AlignConsecutiveAssignments = true; 9534 Alignment.ColumnLimit = 30; 9535 verifyFormat("float ii = 1;\n" 9536 "unsigned j = 2;\n" 9537 "int someVerylongVariable = 1;\n" 9538 "AnotherLongType ll = 123456;\n" 9539 "VeryVeryLongType k = 2;\n" 9540 "int myvar = 1;", 9541 Alignment); 9542 Alignment.ColumnLimit = 80; 9543 Alignment.AlignConsecutiveAssignments = false; 9544 9545 verifyFormat( 9546 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n" 9547 " typename LongType, typename B>\n" 9548 "auto foo() {}\n", 9549 Alignment); 9550 verifyFormat("float a, b = 1;\n" 9551 "int c = 2;\n" 9552 "int dd = 3;\n", 9553 Alignment); 9554 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 9555 "float b[1][] = {{3.f}};\n", 9556 Alignment); 9557 Alignment.AlignConsecutiveAssignments = true; 9558 verifyFormat("float a, b = 1;\n" 9559 "int c = 2;\n" 9560 "int dd = 3;\n", 9561 Alignment); 9562 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 9563 "float b[1][] = {{3.f}};\n", 9564 Alignment); 9565 Alignment.AlignConsecutiveAssignments = false; 9566 9567 Alignment.ColumnLimit = 30; 9568 Alignment.BinPackParameters = false; 9569 verifyFormat("void foo(float a,\n" 9570 " float b,\n" 9571 " int c,\n" 9572 " uint32_t *d) {\n" 9573 " int * e = 0;\n" 9574 " float f = 0;\n" 9575 " double g = 0;\n" 9576 "}\n" 9577 "void bar(ino_t a,\n" 9578 " int b,\n" 9579 " uint32_t *c,\n" 9580 " bool d) {}\n", 9581 Alignment); 9582 Alignment.BinPackParameters = true; 9583 Alignment.ColumnLimit = 80; 9584 9585 // Bug 33507 9586 Alignment.PointerAlignment = FormatStyle::PAS_Middle; 9587 verifyFormat( 9588 "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n" 9589 " static const Version verVs2017;\n" 9590 " return true;\n" 9591 "});\n", 9592 Alignment); 9593 Alignment.PointerAlignment = FormatStyle::PAS_Right; 9594 } 9595 9596 TEST_F(FormatTest, LinuxBraceBreaking) { 9597 FormatStyle LinuxBraceStyle = getLLVMStyle(); 9598 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux; 9599 verifyFormat("namespace a\n" 9600 "{\n" 9601 "class A\n" 9602 "{\n" 9603 " void f()\n" 9604 " {\n" 9605 " if (true) {\n" 9606 " a();\n" 9607 " b();\n" 9608 " } else {\n" 9609 " a();\n" 9610 " }\n" 9611 " }\n" 9612 " void g() { return; }\n" 9613 "};\n" 9614 "struct B {\n" 9615 " int x;\n" 9616 "};\n" 9617 "} // namespace a\n", 9618 LinuxBraceStyle); 9619 verifyFormat("enum X {\n" 9620 " Y = 0,\n" 9621 "}\n", 9622 LinuxBraceStyle); 9623 verifyFormat("struct S {\n" 9624 " int Type;\n" 9625 " union {\n" 9626 " int x;\n" 9627 " double y;\n" 9628 " } Value;\n" 9629 " class C\n" 9630 " {\n" 9631 " MyFavoriteType Value;\n" 9632 " } Class;\n" 9633 "}\n", 9634 LinuxBraceStyle); 9635 } 9636 9637 TEST_F(FormatTest, MozillaBraceBreaking) { 9638 FormatStyle MozillaBraceStyle = getLLVMStyle(); 9639 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; 9640 MozillaBraceStyle.FixNamespaceComments = false; 9641 verifyFormat("namespace a {\n" 9642 "class A\n" 9643 "{\n" 9644 " void f()\n" 9645 " {\n" 9646 " if (true) {\n" 9647 " a();\n" 9648 " b();\n" 9649 " }\n" 9650 " }\n" 9651 " void g() { return; }\n" 9652 "};\n" 9653 "enum E\n" 9654 "{\n" 9655 " A,\n" 9656 " // foo\n" 9657 " B,\n" 9658 " C\n" 9659 "};\n" 9660 "struct B\n" 9661 "{\n" 9662 " int x;\n" 9663 "};\n" 9664 "}\n", 9665 MozillaBraceStyle); 9666 verifyFormat("struct S\n" 9667 "{\n" 9668 " int Type;\n" 9669 " union\n" 9670 " {\n" 9671 " int x;\n" 9672 " double y;\n" 9673 " } Value;\n" 9674 " class C\n" 9675 " {\n" 9676 " MyFavoriteType Value;\n" 9677 " } Class;\n" 9678 "}\n", 9679 MozillaBraceStyle); 9680 } 9681 9682 TEST_F(FormatTest, StroustrupBraceBreaking) { 9683 FormatStyle StroustrupBraceStyle = getLLVMStyle(); 9684 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 9685 verifyFormat("namespace a {\n" 9686 "class A {\n" 9687 " void f()\n" 9688 " {\n" 9689 " if (true) {\n" 9690 " a();\n" 9691 " b();\n" 9692 " }\n" 9693 " }\n" 9694 " void g() { return; }\n" 9695 "};\n" 9696 "struct B {\n" 9697 " int x;\n" 9698 "};\n" 9699 "} // namespace a\n", 9700 StroustrupBraceStyle); 9701 9702 verifyFormat("void foo()\n" 9703 "{\n" 9704 " if (a) {\n" 9705 " a();\n" 9706 " }\n" 9707 " else {\n" 9708 " b();\n" 9709 " }\n" 9710 "}\n", 9711 StroustrupBraceStyle); 9712 9713 verifyFormat("#ifdef _DEBUG\n" 9714 "int foo(int i = 0)\n" 9715 "#else\n" 9716 "int foo(int i = 5)\n" 9717 "#endif\n" 9718 "{\n" 9719 " return i;\n" 9720 "}", 9721 StroustrupBraceStyle); 9722 9723 verifyFormat("void foo() {}\n" 9724 "void bar()\n" 9725 "#ifdef _DEBUG\n" 9726 "{\n" 9727 " foo();\n" 9728 "}\n" 9729 "#else\n" 9730 "{\n" 9731 "}\n" 9732 "#endif", 9733 StroustrupBraceStyle); 9734 9735 verifyFormat("void foobar() { int i = 5; }\n" 9736 "#ifdef _DEBUG\n" 9737 "void bar() {}\n" 9738 "#else\n" 9739 "void bar() { foobar(); }\n" 9740 "#endif", 9741 StroustrupBraceStyle); 9742 } 9743 9744 TEST_F(FormatTest, AllmanBraceBreaking) { 9745 FormatStyle AllmanBraceStyle = getLLVMStyle(); 9746 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman; 9747 9748 EXPECT_EQ("namespace a\n" 9749 "{\n" 9750 "void f();\n" 9751 "void g();\n" 9752 "} // namespace a\n", 9753 format("namespace a\n" 9754 "{\n" 9755 "void f();\n" 9756 "void g();\n" 9757 "}\n", 9758 AllmanBraceStyle)); 9759 9760 verifyFormat("namespace a\n" 9761 "{\n" 9762 "class A\n" 9763 "{\n" 9764 " void f()\n" 9765 " {\n" 9766 " if (true)\n" 9767 " {\n" 9768 " a();\n" 9769 " b();\n" 9770 " }\n" 9771 " }\n" 9772 " void g() { return; }\n" 9773 "};\n" 9774 "struct B\n" 9775 "{\n" 9776 " int x;\n" 9777 "};\n" 9778 "} // namespace a", 9779 AllmanBraceStyle); 9780 9781 verifyFormat("void f()\n" 9782 "{\n" 9783 " if (true)\n" 9784 " {\n" 9785 " a();\n" 9786 " }\n" 9787 " else if (false)\n" 9788 " {\n" 9789 " b();\n" 9790 " }\n" 9791 " else\n" 9792 " {\n" 9793 " c();\n" 9794 " }\n" 9795 "}\n", 9796 AllmanBraceStyle); 9797 9798 verifyFormat("void f()\n" 9799 "{\n" 9800 " for (int i = 0; i < 10; ++i)\n" 9801 " {\n" 9802 " a();\n" 9803 " }\n" 9804 " while (false)\n" 9805 " {\n" 9806 " b();\n" 9807 " }\n" 9808 " do\n" 9809 " {\n" 9810 " c();\n" 9811 " } while (false)\n" 9812 "}\n", 9813 AllmanBraceStyle); 9814 9815 verifyFormat("void f(int a)\n" 9816 "{\n" 9817 " switch (a)\n" 9818 " {\n" 9819 " case 0:\n" 9820 " break;\n" 9821 " case 1:\n" 9822 " {\n" 9823 " break;\n" 9824 " }\n" 9825 " case 2:\n" 9826 " {\n" 9827 " }\n" 9828 " break;\n" 9829 " default:\n" 9830 " break;\n" 9831 " }\n" 9832 "}\n", 9833 AllmanBraceStyle); 9834 9835 verifyFormat("enum X\n" 9836 "{\n" 9837 " Y = 0,\n" 9838 "}\n", 9839 AllmanBraceStyle); 9840 verifyFormat("enum X\n" 9841 "{\n" 9842 " Y = 0\n" 9843 "}\n", 9844 AllmanBraceStyle); 9845 9846 verifyFormat("@interface BSApplicationController ()\n" 9847 "{\n" 9848 "@private\n" 9849 " id _extraIvar;\n" 9850 "}\n" 9851 "@end\n", 9852 AllmanBraceStyle); 9853 9854 verifyFormat("#ifdef _DEBUG\n" 9855 "int foo(int i = 0)\n" 9856 "#else\n" 9857 "int foo(int i = 5)\n" 9858 "#endif\n" 9859 "{\n" 9860 " return i;\n" 9861 "}", 9862 AllmanBraceStyle); 9863 9864 verifyFormat("void foo() {}\n" 9865 "void bar()\n" 9866 "#ifdef _DEBUG\n" 9867 "{\n" 9868 " foo();\n" 9869 "}\n" 9870 "#else\n" 9871 "{\n" 9872 "}\n" 9873 "#endif", 9874 AllmanBraceStyle); 9875 9876 verifyFormat("void foobar() { int i = 5; }\n" 9877 "#ifdef _DEBUG\n" 9878 "void bar() {}\n" 9879 "#else\n" 9880 "void bar() { foobar(); }\n" 9881 "#endif", 9882 AllmanBraceStyle); 9883 9884 // This shouldn't affect ObjC blocks.. 9885 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 9886 " // ...\n" 9887 " int i;\n" 9888 "}];", 9889 AllmanBraceStyle); 9890 verifyFormat("void (^block)(void) = ^{\n" 9891 " // ...\n" 9892 " int i;\n" 9893 "};", 9894 AllmanBraceStyle); 9895 // .. or dict literals. 9896 verifyFormat("void f()\n" 9897 "{\n" 9898 " // ...\n" 9899 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 9900 "}", 9901 AllmanBraceStyle); 9902 verifyFormat("void f()\n" 9903 "{\n" 9904 " // ...\n" 9905 " [object someMethod:@{a : @\"b\"}];\n" 9906 "}", 9907 AllmanBraceStyle); 9908 verifyFormat("int f()\n" 9909 "{ // comment\n" 9910 " return 42;\n" 9911 "}", 9912 AllmanBraceStyle); 9913 9914 AllmanBraceStyle.ColumnLimit = 19; 9915 verifyFormat("void f() { int i; }", AllmanBraceStyle); 9916 AllmanBraceStyle.ColumnLimit = 18; 9917 verifyFormat("void f()\n" 9918 "{\n" 9919 " int i;\n" 9920 "}", 9921 AllmanBraceStyle); 9922 AllmanBraceStyle.ColumnLimit = 80; 9923 9924 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle; 9925 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true; 9926 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 9927 verifyFormat("void f(bool b)\n" 9928 "{\n" 9929 " if (b)\n" 9930 " {\n" 9931 " return;\n" 9932 " }\n" 9933 "}\n", 9934 BreakBeforeBraceShortIfs); 9935 verifyFormat("void f(bool b)\n" 9936 "{\n" 9937 " if constexpr (b)\n" 9938 " {\n" 9939 " return;\n" 9940 " }\n" 9941 "}\n", 9942 BreakBeforeBraceShortIfs); 9943 verifyFormat("void f(bool b)\n" 9944 "{\n" 9945 " if (b) return;\n" 9946 "}\n", 9947 BreakBeforeBraceShortIfs); 9948 verifyFormat("void f(bool b)\n" 9949 "{\n" 9950 " if constexpr (b) return;\n" 9951 "}\n", 9952 BreakBeforeBraceShortIfs); 9953 verifyFormat("void f(bool b)\n" 9954 "{\n" 9955 " while (b)\n" 9956 " {\n" 9957 " return;\n" 9958 " }\n" 9959 "}\n", 9960 BreakBeforeBraceShortIfs); 9961 } 9962 9963 TEST_F(FormatTest, GNUBraceBreaking) { 9964 FormatStyle GNUBraceStyle = getLLVMStyle(); 9965 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU; 9966 verifyFormat("namespace a\n" 9967 "{\n" 9968 "class A\n" 9969 "{\n" 9970 " void f()\n" 9971 " {\n" 9972 " int a;\n" 9973 " {\n" 9974 " int b;\n" 9975 " }\n" 9976 " if (true)\n" 9977 " {\n" 9978 " a();\n" 9979 " b();\n" 9980 " }\n" 9981 " }\n" 9982 " void g() { return; }\n" 9983 "}\n" 9984 "} // namespace a", 9985 GNUBraceStyle); 9986 9987 verifyFormat("void f()\n" 9988 "{\n" 9989 " if (true)\n" 9990 " {\n" 9991 " a();\n" 9992 " }\n" 9993 " else if (false)\n" 9994 " {\n" 9995 " b();\n" 9996 " }\n" 9997 " else\n" 9998 " {\n" 9999 " c();\n" 10000 " }\n" 10001 "}\n", 10002 GNUBraceStyle); 10003 10004 verifyFormat("void f()\n" 10005 "{\n" 10006 " for (int i = 0; i < 10; ++i)\n" 10007 " {\n" 10008 " a();\n" 10009 " }\n" 10010 " while (false)\n" 10011 " {\n" 10012 " b();\n" 10013 " }\n" 10014 " do\n" 10015 " {\n" 10016 " c();\n" 10017 " }\n" 10018 " while (false);\n" 10019 "}\n", 10020 GNUBraceStyle); 10021 10022 verifyFormat("void f(int a)\n" 10023 "{\n" 10024 " switch (a)\n" 10025 " {\n" 10026 " case 0:\n" 10027 " break;\n" 10028 " case 1:\n" 10029 " {\n" 10030 " break;\n" 10031 " }\n" 10032 " case 2:\n" 10033 " {\n" 10034 " }\n" 10035 " break;\n" 10036 " default:\n" 10037 " break;\n" 10038 " }\n" 10039 "}\n", 10040 GNUBraceStyle); 10041 10042 verifyFormat("enum X\n" 10043 "{\n" 10044 " Y = 0,\n" 10045 "}\n", 10046 GNUBraceStyle); 10047 10048 verifyFormat("@interface BSApplicationController ()\n" 10049 "{\n" 10050 "@private\n" 10051 " id _extraIvar;\n" 10052 "}\n" 10053 "@end\n", 10054 GNUBraceStyle); 10055 10056 verifyFormat("#ifdef _DEBUG\n" 10057 "int foo(int i = 0)\n" 10058 "#else\n" 10059 "int foo(int i = 5)\n" 10060 "#endif\n" 10061 "{\n" 10062 " return i;\n" 10063 "}", 10064 GNUBraceStyle); 10065 10066 verifyFormat("void foo() {}\n" 10067 "void bar()\n" 10068 "#ifdef _DEBUG\n" 10069 "{\n" 10070 " foo();\n" 10071 "}\n" 10072 "#else\n" 10073 "{\n" 10074 "}\n" 10075 "#endif", 10076 GNUBraceStyle); 10077 10078 verifyFormat("void foobar() { int i = 5; }\n" 10079 "#ifdef _DEBUG\n" 10080 "void bar() {}\n" 10081 "#else\n" 10082 "void bar() { foobar(); }\n" 10083 "#endif", 10084 GNUBraceStyle); 10085 } 10086 10087 TEST_F(FormatTest, WebKitBraceBreaking) { 10088 FormatStyle WebKitBraceStyle = getLLVMStyle(); 10089 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit; 10090 WebKitBraceStyle.FixNamespaceComments = false; 10091 verifyFormat("namespace a {\n" 10092 "class A {\n" 10093 " void f()\n" 10094 " {\n" 10095 " if (true) {\n" 10096 " a();\n" 10097 " b();\n" 10098 " }\n" 10099 " }\n" 10100 " void g() { return; }\n" 10101 "};\n" 10102 "enum E {\n" 10103 " A,\n" 10104 " // foo\n" 10105 " B,\n" 10106 " C\n" 10107 "};\n" 10108 "struct B {\n" 10109 " int x;\n" 10110 "};\n" 10111 "}\n", 10112 WebKitBraceStyle); 10113 verifyFormat("struct S {\n" 10114 " int Type;\n" 10115 " union {\n" 10116 " int x;\n" 10117 " double y;\n" 10118 " } Value;\n" 10119 " class C {\n" 10120 " MyFavoriteType Value;\n" 10121 " } Class;\n" 10122 "};\n", 10123 WebKitBraceStyle); 10124 } 10125 10126 TEST_F(FormatTest, CatchExceptionReferenceBinding) { 10127 verifyFormat("void f() {\n" 10128 " try {\n" 10129 " } catch (const Exception &e) {\n" 10130 " }\n" 10131 "}\n", 10132 getLLVMStyle()); 10133 } 10134 10135 TEST_F(FormatTest, UnderstandsPragmas) { 10136 verifyFormat("#pragma omp reduction(| : var)"); 10137 verifyFormat("#pragma omp reduction(+ : var)"); 10138 10139 EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string " 10140 "(including parentheses).", 10141 format("#pragma mark Any non-hyphenated or hyphenated string " 10142 "(including parentheses).")); 10143 } 10144 10145 TEST_F(FormatTest, UnderstandPragmaOption) { 10146 verifyFormat("#pragma option -C -A"); 10147 10148 EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A")); 10149 } 10150 10151 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) { 10152 FormatStyle Style = getLLVMStyle(); 10153 Style.ColumnLimit = 20; 10154 10155 verifyFormat("int a; // the\n" 10156 " // comment", Style); 10157 EXPECT_EQ("int a; /* first line\n" 10158 " * second\n" 10159 " * line third\n" 10160 " * line\n" 10161 " */", 10162 format("int a; /* first line\n" 10163 " * second\n" 10164 " * line third\n" 10165 " * line\n" 10166 " */", 10167 Style)); 10168 EXPECT_EQ("int a; // first line\n" 10169 " // second\n" 10170 " // line third\n" 10171 " // line", 10172 format("int a; // first line\n" 10173 " // second line\n" 10174 " // third line", 10175 Style)); 10176 10177 Style.PenaltyExcessCharacter = 90; 10178 verifyFormat("int a; // the comment", Style); 10179 EXPECT_EQ("int a; // the comment\n" 10180 " // aaa", 10181 format("int a; // the comment aaa", Style)); 10182 EXPECT_EQ("int a; /* first line\n" 10183 " * second line\n" 10184 " * third line\n" 10185 " */", 10186 format("int a; /* first line\n" 10187 " * second line\n" 10188 " * third line\n" 10189 " */", 10190 Style)); 10191 EXPECT_EQ("int a; // first line\n" 10192 " // second line\n" 10193 " // third line", 10194 format("int a; // first line\n" 10195 " // second line\n" 10196 " // third line", 10197 Style)); 10198 // FIXME: Investigate why this is not getting the same layout as the test 10199 // above. 10200 EXPECT_EQ("int a; /* first line\n" 10201 " * second line\n" 10202 " * third line\n" 10203 " */", 10204 format("int a; /* first line second line third line" 10205 "\n*/", 10206 Style)); 10207 10208 EXPECT_EQ("// foo bar baz bazfoo\n" 10209 "// foo bar foo bar\n", 10210 format("// foo bar baz bazfoo\n" 10211 "// foo bar foo bar\n", 10212 Style)); 10213 EXPECT_EQ("// foo bar baz bazfoo\n" 10214 "// foo bar foo bar\n", 10215 format("// foo bar baz bazfoo\n" 10216 "// foo bar foo bar\n", 10217 Style)); 10218 10219 // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the 10220 // next one. 10221 EXPECT_EQ("// foo bar baz bazfoo\n" 10222 "// bar foo bar\n", 10223 format("// foo bar baz bazfoo bar\n" 10224 "// foo bar\n", 10225 Style)); 10226 10227 EXPECT_EQ("// foo bar baz bazfoo\n" 10228 "// foo bar baz bazfoo\n" 10229 "// bar foo bar\n", 10230 format("// foo bar baz bazfoo\n" 10231 "// foo bar baz bazfoo bar\n" 10232 "// foo bar\n", 10233 Style)); 10234 10235 EXPECT_EQ("// foo bar baz bazfoo\n" 10236 "// foo bar baz bazfoo\n" 10237 "// bar foo bar\n", 10238 format("// foo bar baz bazfoo\n" 10239 "// foo bar baz bazfoo bar\n" 10240 "// foo bar\n", 10241 Style)); 10242 10243 // Make sure we do not keep protruding characters if strict mode reflow is 10244 // cheaper than keeping protruding characters. 10245 Style.ColumnLimit = 21; 10246 EXPECT_EQ("// foo foo foo foo\n" 10247 "// foo foo foo foo\n" 10248 "// foo foo foo foo\n", 10249 format("// foo foo foo foo foo foo foo foo foo foo foo foo\n", 10250 Style)); 10251 10252 EXPECT_EQ("int a = /* long block\n" 10253 " comment */\n" 10254 " 42;", 10255 format("int a = /* long block comment */ 42;", Style)); 10256 } 10257 10258 #define EXPECT_ALL_STYLES_EQUAL(Styles) \ 10259 for (size_t i = 1; i < Styles.size(); ++i) \ 10260 EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \ 10261 << " differs from Style #0" 10262 10263 TEST_F(FormatTest, GetsPredefinedStyleByName) { 10264 SmallVector<FormatStyle, 3> Styles; 10265 Styles.resize(3); 10266 10267 Styles[0] = getLLVMStyle(); 10268 EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1])); 10269 EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2])); 10270 EXPECT_ALL_STYLES_EQUAL(Styles); 10271 10272 Styles[0] = getGoogleStyle(); 10273 EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1])); 10274 EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2])); 10275 EXPECT_ALL_STYLES_EQUAL(Styles); 10276 10277 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 10278 EXPECT_TRUE( 10279 getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1])); 10280 EXPECT_TRUE( 10281 getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2])); 10282 EXPECT_ALL_STYLES_EQUAL(Styles); 10283 10284 Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp); 10285 EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1])); 10286 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2])); 10287 EXPECT_ALL_STYLES_EQUAL(Styles); 10288 10289 Styles[0] = getMozillaStyle(); 10290 EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1])); 10291 EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2])); 10292 EXPECT_ALL_STYLES_EQUAL(Styles); 10293 10294 Styles[0] = getWebKitStyle(); 10295 EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1])); 10296 EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2])); 10297 EXPECT_ALL_STYLES_EQUAL(Styles); 10298 10299 Styles[0] = getGNUStyle(); 10300 EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1])); 10301 EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2])); 10302 EXPECT_ALL_STYLES_EQUAL(Styles); 10303 10304 EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0])); 10305 } 10306 10307 TEST_F(FormatTest, GetsCorrectBasedOnStyle) { 10308 SmallVector<FormatStyle, 8> Styles; 10309 Styles.resize(2); 10310 10311 Styles[0] = getGoogleStyle(); 10312 Styles[1] = getLLVMStyle(); 10313 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 10314 EXPECT_ALL_STYLES_EQUAL(Styles); 10315 10316 Styles.resize(5); 10317 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 10318 Styles[1] = getLLVMStyle(); 10319 Styles[1].Language = FormatStyle::LK_JavaScript; 10320 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 10321 10322 Styles[2] = getLLVMStyle(); 10323 Styles[2].Language = FormatStyle::LK_JavaScript; 10324 EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n" 10325 "BasedOnStyle: Google", 10326 &Styles[2]) 10327 .value()); 10328 10329 Styles[3] = getLLVMStyle(); 10330 Styles[3].Language = FormatStyle::LK_JavaScript; 10331 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n" 10332 "Language: JavaScript", 10333 &Styles[3]) 10334 .value()); 10335 10336 Styles[4] = getLLVMStyle(); 10337 Styles[4].Language = FormatStyle::LK_JavaScript; 10338 EXPECT_EQ(0, parseConfiguration("---\n" 10339 "BasedOnStyle: LLVM\n" 10340 "IndentWidth: 123\n" 10341 "---\n" 10342 "BasedOnStyle: Google\n" 10343 "Language: JavaScript", 10344 &Styles[4]) 10345 .value()); 10346 EXPECT_ALL_STYLES_EQUAL(Styles); 10347 } 10348 10349 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \ 10350 Style.FIELD = false; \ 10351 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \ 10352 EXPECT_TRUE(Style.FIELD); \ 10353 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \ 10354 EXPECT_FALSE(Style.FIELD); 10355 10356 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD) 10357 10358 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \ 10359 Style.STRUCT.FIELD = false; \ 10360 EXPECT_EQ(0, \ 10361 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \ 10362 .value()); \ 10363 EXPECT_TRUE(Style.STRUCT.FIELD); \ 10364 EXPECT_EQ(0, \ 10365 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \ 10366 .value()); \ 10367 EXPECT_FALSE(Style.STRUCT.FIELD); 10368 10369 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \ 10370 CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD) 10371 10372 #define CHECK_PARSE(TEXT, FIELD, VALUE) \ 10373 EXPECT_NE(VALUE, Style.FIELD); \ 10374 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \ 10375 EXPECT_EQ(VALUE, Style.FIELD) 10376 10377 TEST_F(FormatTest, ParsesConfigurationBools) { 10378 FormatStyle Style = {}; 10379 Style.Language = FormatStyle::LK_Cpp; 10380 CHECK_PARSE_BOOL(AlignOperands); 10381 CHECK_PARSE_BOOL(AlignTrailingComments); 10382 CHECK_PARSE_BOOL(AlignConsecutiveAssignments); 10383 CHECK_PARSE_BOOL(AlignConsecutiveDeclarations); 10384 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); 10385 CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine); 10386 CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine); 10387 CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine); 10388 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); 10389 CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations); 10390 CHECK_PARSE_BOOL(BinPackArguments); 10391 CHECK_PARSE_BOOL(BinPackParameters); 10392 CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations); 10393 CHECK_PARSE_BOOL(BreakBeforeTernaryOperators); 10394 CHECK_PARSE_BOOL(BreakStringLiterals); 10395 CHECK_PARSE_BOOL(BreakBeforeInheritanceComma) 10396 CHECK_PARSE_BOOL(CompactNamespaces); 10397 CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine); 10398 CHECK_PARSE_BOOL(DerivePointerAlignment); 10399 CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding"); 10400 CHECK_PARSE_BOOL(DisableFormat); 10401 CHECK_PARSE_BOOL(IndentCaseLabels); 10402 CHECK_PARSE_BOOL(IndentWrappedFunctionNames); 10403 CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks); 10404 CHECK_PARSE_BOOL(ObjCSpaceAfterProperty); 10405 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList); 10406 CHECK_PARSE_BOOL(Cpp11BracedListStyle); 10407 CHECK_PARSE_BOOL(ReflowComments); 10408 CHECK_PARSE_BOOL(SortIncludes); 10409 CHECK_PARSE_BOOL(SortUsingDeclarations); 10410 CHECK_PARSE_BOOL(SpacesInParentheses); 10411 CHECK_PARSE_BOOL(SpacesInSquareBrackets); 10412 CHECK_PARSE_BOOL(SpacesInAngles); 10413 CHECK_PARSE_BOOL(SpaceInEmptyParentheses); 10414 CHECK_PARSE_BOOL(SpacesInContainerLiterals); 10415 CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); 10416 CHECK_PARSE_BOOL(SpaceAfterCStyleCast); 10417 CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword); 10418 CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); 10419 CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon); 10420 CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon); 10421 CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon); 10422 10423 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass); 10424 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement); 10425 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum); 10426 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction); 10427 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace); 10428 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration); 10429 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct); 10430 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion); 10431 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock); 10432 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch); 10433 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse); 10434 CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces); 10435 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction); 10436 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord); 10437 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace); 10438 } 10439 10440 #undef CHECK_PARSE_BOOL 10441 10442 TEST_F(FormatTest, ParsesConfiguration) { 10443 FormatStyle Style = {}; 10444 Style.Language = FormatStyle::LK_Cpp; 10445 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234); 10446 CHECK_PARSE("ConstructorInitializerIndentWidth: 1234", 10447 ConstructorInitializerIndentWidth, 1234u); 10448 CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u); 10449 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); 10450 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u); 10451 CHECK_PARSE("PenaltyBreakAssignment: 1234", 10452 PenaltyBreakAssignment, 1234u); 10453 CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234", 10454 PenaltyBreakBeforeFirstCallParameter, 1234u); 10455 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); 10456 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234", 10457 PenaltyReturnTypeOnItsOwnLine, 1234u); 10458 CHECK_PARSE("SpacesBeforeTrailingComments: 1234", 10459 SpacesBeforeTrailingComments, 1234u); 10460 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u); 10461 CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u); 10462 CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$"); 10463 10464 Style.PointerAlignment = FormatStyle::PAS_Middle; 10465 CHECK_PARSE("PointerAlignment: Left", PointerAlignment, 10466 FormatStyle::PAS_Left); 10467 CHECK_PARSE("PointerAlignment: Right", PointerAlignment, 10468 FormatStyle::PAS_Right); 10469 CHECK_PARSE("PointerAlignment: Middle", PointerAlignment, 10470 FormatStyle::PAS_Middle); 10471 // For backward compatibility: 10472 CHECK_PARSE("PointerBindsToType: Left", PointerAlignment, 10473 FormatStyle::PAS_Left); 10474 CHECK_PARSE("PointerBindsToType: Right", PointerAlignment, 10475 FormatStyle::PAS_Right); 10476 CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment, 10477 FormatStyle::PAS_Middle); 10478 10479 Style.Standard = FormatStyle::LS_Auto; 10480 CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03); 10481 CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11); 10482 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03); 10483 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11); 10484 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto); 10485 10486 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 10487 CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment", 10488 BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment); 10489 CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators, 10490 FormatStyle::BOS_None); 10491 CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators, 10492 FormatStyle::BOS_All); 10493 // For backward compatibility: 10494 CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators, 10495 FormatStyle::BOS_None); 10496 CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators, 10497 FormatStyle::BOS_All); 10498 10499 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 10500 CHECK_PARSE("BreakConstructorInitializers: BeforeComma", 10501 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 10502 CHECK_PARSE("BreakConstructorInitializers: AfterColon", 10503 BreakConstructorInitializers, FormatStyle::BCIS_AfterColon); 10504 CHECK_PARSE("BreakConstructorInitializers: BeforeColon", 10505 BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon); 10506 // For backward compatibility: 10507 CHECK_PARSE("BreakConstructorInitializersBeforeComma: true", 10508 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 10509 10510 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 10511 CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket, 10512 FormatStyle::BAS_Align); 10513 CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket, 10514 FormatStyle::BAS_DontAlign); 10515 CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket, 10516 FormatStyle::BAS_AlwaysBreak); 10517 // For backward compatibility: 10518 CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket, 10519 FormatStyle::BAS_DontAlign); 10520 CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket, 10521 FormatStyle::BAS_Align); 10522 10523 Style.AlignEscapedNewlines = FormatStyle::ENAS_Left; 10524 CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines, 10525 FormatStyle::ENAS_DontAlign); 10526 CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines, 10527 FormatStyle::ENAS_Left); 10528 CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines, 10529 FormatStyle::ENAS_Right); 10530 // For backward compatibility: 10531 CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines, 10532 FormatStyle::ENAS_Left); 10533 CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines, 10534 FormatStyle::ENAS_Right); 10535 10536 Style.UseTab = FormatStyle::UT_ForIndentation; 10537 CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never); 10538 CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation); 10539 CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always); 10540 CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab, 10541 FormatStyle::UT_ForContinuationAndIndentation); 10542 // For backward compatibility: 10543 CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never); 10544 CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always); 10545 10546 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 10547 CHECK_PARSE("AllowShortFunctionsOnASingleLine: None", 10548 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 10549 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline", 10550 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline); 10551 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty", 10552 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty); 10553 CHECK_PARSE("AllowShortFunctionsOnASingleLine: All", 10554 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 10555 // For backward compatibility: 10556 CHECK_PARSE("AllowShortFunctionsOnASingleLine: false", 10557 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 10558 CHECK_PARSE("AllowShortFunctionsOnASingleLine: true", 10559 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 10560 10561 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 10562 CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens, 10563 FormatStyle::SBPO_Never); 10564 CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens, 10565 FormatStyle::SBPO_Always); 10566 CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens, 10567 FormatStyle::SBPO_ControlStatements); 10568 // For backward compatibility: 10569 CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens, 10570 FormatStyle::SBPO_Never); 10571 CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens, 10572 FormatStyle::SBPO_ControlStatements); 10573 10574 Style.ColumnLimit = 123; 10575 FormatStyle BaseStyle = getLLVMStyle(); 10576 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit); 10577 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u); 10578 10579 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 10580 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces, 10581 FormatStyle::BS_Attach); 10582 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces, 10583 FormatStyle::BS_Linux); 10584 CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces, 10585 FormatStyle::BS_Mozilla); 10586 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces, 10587 FormatStyle::BS_Stroustrup); 10588 CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces, 10589 FormatStyle::BS_Allman); 10590 CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU); 10591 CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces, 10592 FormatStyle::BS_WebKit); 10593 CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces, 10594 FormatStyle::BS_Custom); 10595 10596 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 10597 CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType, 10598 FormatStyle::RTBS_None); 10599 CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType, 10600 FormatStyle::RTBS_All); 10601 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel", 10602 AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel); 10603 CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions", 10604 AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions); 10605 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions", 10606 AlwaysBreakAfterReturnType, 10607 FormatStyle::RTBS_TopLevelDefinitions); 10608 10609 Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All; 10610 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None", 10611 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None); 10612 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All", 10613 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All); 10614 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel", 10615 AlwaysBreakAfterDefinitionReturnType, 10616 FormatStyle::DRTBS_TopLevel); 10617 10618 Style.NamespaceIndentation = FormatStyle::NI_All; 10619 CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation, 10620 FormatStyle::NI_None); 10621 CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation, 10622 FormatStyle::NI_Inner); 10623 CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation, 10624 FormatStyle::NI_All); 10625 10626 // FIXME: This is required because parsing a configuration simply overwrites 10627 // the first N elements of the list instead of resetting it. 10628 Style.ForEachMacros.clear(); 10629 std::vector<std::string> BoostForeach; 10630 BoostForeach.push_back("BOOST_FOREACH"); 10631 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach); 10632 std::vector<std::string> BoostAndQForeach; 10633 BoostAndQForeach.push_back("BOOST_FOREACH"); 10634 BoostAndQForeach.push_back("Q_FOREACH"); 10635 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros, 10636 BoostAndQForeach); 10637 10638 Style.IncludeCategories.clear(); 10639 std::vector<FormatStyle::IncludeCategory> ExpectedCategories = {{"abc/.*", 2}, 10640 {".*", 1}}; 10641 CHECK_PARSE("IncludeCategories:\n" 10642 " - Regex: abc/.*\n" 10643 " Priority: 2\n" 10644 " - Regex: .*\n" 10645 " Priority: 1", 10646 IncludeCategories, ExpectedCategories); 10647 CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeIsMainRegex, "abc$"); 10648 10649 Style.RawStringFormats.clear(); 10650 std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = { 10651 { 10652 FormatStyle::LK_TextProto, 10653 {"pb", "proto"}, 10654 {"PARSE_TEXT_PROTO"}, 10655 /*CanonicalDelimiter=*/"", 10656 "llvm", 10657 }, 10658 { 10659 FormatStyle::LK_Cpp, 10660 {"cc", "cpp"}, 10661 {"C_CODEBLOCK", "CPPEVAL"}, 10662 /*CanonicalDelimiter=*/"cc", 10663 /*BasedOnStyle=*/"", 10664 }, 10665 }; 10666 10667 CHECK_PARSE("RawStringFormats:\n" 10668 " - Language: TextProto\n" 10669 " Delimiters:\n" 10670 " - 'pb'\n" 10671 " - 'proto'\n" 10672 " EnclosingFunctions:\n" 10673 " - 'PARSE_TEXT_PROTO'\n" 10674 " BasedOnStyle: llvm\n" 10675 " - Language: Cpp\n" 10676 " Delimiters:\n" 10677 " - 'cc'\n" 10678 " - 'cpp'\n" 10679 " EnclosingFunctions:\n" 10680 " - 'C_CODEBLOCK'\n" 10681 " - 'CPPEVAL'\n" 10682 " CanonicalDelimiter: 'cc'", 10683 RawStringFormats, ExpectedRawStringFormats); 10684 } 10685 10686 TEST_F(FormatTest, ParsesConfigurationWithLanguages) { 10687 FormatStyle Style = {}; 10688 Style.Language = FormatStyle::LK_Cpp; 10689 CHECK_PARSE("Language: Cpp\n" 10690 "IndentWidth: 12", 10691 IndentWidth, 12u); 10692 EXPECT_EQ(parseConfiguration("Language: JavaScript\n" 10693 "IndentWidth: 34", 10694 &Style), 10695 ParseError::Unsuitable); 10696 EXPECT_EQ(12u, Style.IndentWidth); 10697 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 10698 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 10699 10700 Style.Language = FormatStyle::LK_JavaScript; 10701 CHECK_PARSE("Language: JavaScript\n" 10702 "IndentWidth: 12", 10703 IndentWidth, 12u); 10704 CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u); 10705 EXPECT_EQ(parseConfiguration("Language: Cpp\n" 10706 "IndentWidth: 34", 10707 &Style), 10708 ParseError::Unsuitable); 10709 EXPECT_EQ(23u, Style.IndentWidth); 10710 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 10711 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 10712 10713 CHECK_PARSE("BasedOnStyle: LLVM\n" 10714 "IndentWidth: 67", 10715 IndentWidth, 67u); 10716 10717 CHECK_PARSE("---\n" 10718 "Language: JavaScript\n" 10719 "IndentWidth: 12\n" 10720 "---\n" 10721 "Language: Cpp\n" 10722 "IndentWidth: 34\n" 10723 "...\n", 10724 IndentWidth, 12u); 10725 10726 Style.Language = FormatStyle::LK_Cpp; 10727 CHECK_PARSE("---\n" 10728 "Language: JavaScript\n" 10729 "IndentWidth: 12\n" 10730 "---\n" 10731 "Language: Cpp\n" 10732 "IndentWidth: 34\n" 10733 "...\n", 10734 IndentWidth, 34u); 10735 CHECK_PARSE("---\n" 10736 "IndentWidth: 78\n" 10737 "---\n" 10738 "Language: JavaScript\n" 10739 "IndentWidth: 56\n" 10740 "...\n", 10741 IndentWidth, 78u); 10742 10743 Style.ColumnLimit = 123; 10744 Style.IndentWidth = 234; 10745 Style.BreakBeforeBraces = FormatStyle::BS_Linux; 10746 Style.TabWidth = 345; 10747 EXPECT_FALSE(parseConfiguration("---\n" 10748 "IndentWidth: 456\n" 10749 "BreakBeforeBraces: Allman\n" 10750 "---\n" 10751 "Language: JavaScript\n" 10752 "IndentWidth: 111\n" 10753 "TabWidth: 111\n" 10754 "---\n" 10755 "Language: Cpp\n" 10756 "BreakBeforeBraces: Stroustrup\n" 10757 "TabWidth: 789\n" 10758 "...\n", 10759 &Style)); 10760 EXPECT_EQ(123u, Style.ColumnLimit); 10761 EXPECT_EQ(456u, Style.IndentWidth); 10762 EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces); 10763 EXPECT_EQ(789u, Style.TabWidth); 10764 10765 EXPECT_EQ(parseConfiguration("---\n" 10766 "Language: JavaScript\n" 10767 "IndentWidth: 56\n" 10768 "---\n" 10769 "IndentWidth: 78\n" 10770 "...\n", 10771 &Style), 10772 ParseError::Error); 10773 EXPECT_EQ(parseConfiguration("---\n" 10774 "Language: JavaScript\n" 10775 "IndentWidth: 56\n" 10776 "---\n" 10777 "Language: JavaScript\n" 10778 "IndentWidth: 78\n" 10779 "...\n", 10780 &Style), 10781 ParseError::Error); 10782 10783 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 10784 } 10785 10786 #undef CHECK_PARSE 10787 10788 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) { 10789 FormatStyle Style = {}; 10790 Style.Language = FormatStyle::LK_JavaScript; 10791 Style.BreakBeforeTernaryOperators = true; 10792 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value()); 10793 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 10794 10795 Style.BreakBeforeTernaryOperators = true; 10796 EXPECT_EQ(0, parseConfiguration("---\n" 10797 "BasedOnStyle: Google\n" 10798 "---\n" 10799 "Language: JavaScript\n" 10800 "IndentWidth: 76\n" 10801 "...\n", 10802 &Style) 10803 .value()); 10804 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 10805 EXPECT_EQ(76u, Style.IndentWidth); 10806 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 10807 } 10808 10809 TEST_F(FormatTest, ConfigurationRoundTripTest) { 10810 FormatStyle Style = getLLVMStyle(); 10811 std::string YAML = configurationAsText(Style); 10812 FormatStyle ParsedStyle = {}; 10813 ParsedStyle.Language = FormatStyle::LK_Cpp; 10814 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value()); 10815 EXPECT_EQ(Style, ParsedStyle); 10816 } 10817 10818 TEST_F(FormatTest, WorksFor8bitEncodings) { 10819 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" 10820 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" 10821 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" 10822 "\"\xef\xee\xf0\xf3...\"", 10823 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " 10824 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " 10825 "\xef\xee\xf0\xf3...\"", 10826 getLLVMStyleWithColumns(12))); 10827 } 10828 10829 TEST_F(FormatTest, HandlesUTF8BOM) { 10830 EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf")); 10831 EXPECT_EQ("\xef\xbb\xbf#include <iostream>", 10832 format("\xef\xbb\xbf#include <iostream>")); 10833 EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>", 10834 format("\xef\xbb\xbf\n#include <iostream>")); 10835 } 10836 10837 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. 10838 #if !defined(_MSC_VER) 10839 10840 TEST_F(FormatTest, CountsUTF8CharactersProperly) { 10841 verifyFormat("\"Однажды в студёную зимнюю пору...\"", 10842 getLLVMStyleWithColumns(35)); 10843 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", 10844 getLLVMStyleWithColumns(31)); 10845 verifyFormat("// Однажды в студёную зимнюю пору...", 10846 getLLVMStyleWithColumns(36)); 10847 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32)); 10848 verifyFormat("/* Однажды в студёную зимнюю пору... */", 10849 getLLVMStyleWithColumns(39)); 10850 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", 10851 getLLVMStyleWithColumns(35)); 10852 } 10853 10854 TEST_F(FormatTest, SplitsUTF8Strings) { 10855 // Non-printable characters' width is currently considered to be the length in 10856 // bytes in UTF8. The characters can be displayed in very different manner 10857 // (zero-width, single width with a substitution glyph, expanded to their code 10858 // (e.g. "<8d>"), so there's no single correct way to handle them. 10859 EXPECT_EQ("\"aaaaÄ\"\n" 10860 "\"\xc2\x8d\";", 10861 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 10862 EXPECT_EQ("\"aaaaaaaÄ\"\n" 10863 "\"\xc2\x8d\";", 10864 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 10865 EXPECT_EQ("\"Однажды, в \"\n" 10866 "\"студёную \"\n" 10867 "\"зимнюю \"\n" 10868 "\"пору,\"", 10869 format("\"Однажды, в студёную зимнюю пору,\"", 10870 getLLVMStyleWithColumns(13))); 10871 EXPECT_EQ( 10872 "\"一 二 三 \"\n" 10873 "\"四 五六 \"\n" 10874 "\"七 八 九 \"\n" 10875 "\"十\"", 10876 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); 10877 EXPECT_EQ("\"一\t\"\n" 10878 "\"二 \t\"\n" 10879 "\"三 四 \"\n" 10880 "\"五\t\"\n" 10881 "\"六 \t\"\n" 10882 "\"七 \"\n" 10883 "\"八九十\tqq\"", 10884 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", 10885 getLLVMStyleWithColumns(11))); 10886 10887 // UTF8 character in an escape sequence. 10888 EXPECT_EQ("\"aaaaaa\"\n" 10889 "\"\\\xC2\x8D\"", 10890 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10))); 10891 } 10892 10893 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { 10894 EXPECT_EQ("const char *sssss =\n" 10895 " \"一二三四五六七八\\\n" 10896 " 九 十\";", 10897 format("const char *sssss = \"一二三四五六七八\\\n" 10898 " 九 十\";", 10899 getLLVMStyleWithColumns(30))); 10900 } 10901 10902 TEST_F(FormatTest, SplitsUTF8LineComments) { 10903 EXPECT_EQ("// aaaaÄ\xc2\x8d", 10904 format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10))); 10905 EXPECT_EQ("// Я из лесу\n" 10906 "// вышел; был\n" 10907 "// сильный\n" 10908 "// мороз.", 10909 format("// Я из лесу вышел; был сильный мороз.", 10910 getLLVMStyleWithColumns(13))); 10911 EXPECT_EQ("// 一二三\n" 10912 "// 四五六七\n" 10913 "// 八 九\n" 10914 "// 十", 10915 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9))); 10916 } 10917 10918 TEST_F(FormatTest, SplitsUTF8BlockComments) { 10919 EXPECT_EQ("/* Гляжу,\n" 10920 " * поднимается\n" 10921 " * медленно в\n" 10922 " * гору\n" 10923 " * Лошадка,\n" 10924 " * везущая\n" 10925 " * хворосту\n" 10926 " * воз. */", 10927 format("/* Гляжу, поднимается медленно в гору\n" 10928 " * Лошадка, везущая хворосту воз. */", 10929 getLLVMStyleWithColumns(13))); 10930 EXPECT_EQ( 10931 "/* 一二三\n" 10932 " * 四五六七\n" 10933 " * 八 九\n" 10934 " * 十 */", 10935 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9))); 10936 EXPECT_EQ("/* \n" 10937 " * \n" 10938 " * - */", 10939 format("/* - */", getLLVMStyleWithColumns(12))); 10940 } 10941 10942 #endif // _MSC_VER 10943 10944 TEST_F(FormatTest, ConstructorInitializerIndentWidth) { 10945 FormatStyle Style = getLLVMStyle(); 10946 10947 Style.ConstructorInitializerIndentWidth = 4; 10948 verifyFormat( 10949 "SomeClass::Constructor()\n" 10950 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10951 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10952 Style); 10953 10954 Style.ConstructorInitializerIndentWidth = 2; 10955 verifyFormat( 10956 "SomeClass::Constructor()\n" 10957 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10958 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10959 Style); 10960 10961 Style.ConstructorInitializerIndentWidth = 0; 10962 verifyFormat( 10963 "SomeClass::Constructor()\n" 10964 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10965 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10966 Style); 10967 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 10968 verifyFormat( 10969 "SomeLongTemplateVariableName<\n" 10970 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>", 10971 Style); 10972 verifyFormat( 10973 "bool smaller = 1 < bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 10974 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 10975 Style); 10976 } 10977 10978 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) { 10979 FormatStyle Style = getLLVMStyle(); 10980 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 10981 Style.ConstructorInitializerIndentWidth = 4; 10982 verifyFormat("SomeClass::Constructor()\n" 10983 " : a(a)\n" 10984 " , b(b)\n" 10985 " , c(c) {}", 10986 Style); 10987 verifyFormat("SomeClass::Constructor()\n" 10988 " : a(a) {}", 10989 Style); 10990 10991 Style.ColumnLimit = 0; 10992 verifyFormat("SomeClass::Constructor()\n" 10993 " : a(a) {}", 10994 Style); 10995 verifyFormat("SomeClass::Constructor() noexcept\n" 10996 " : a(a) {}", 10997 Style); 10998 verifyFormat("SomeClass::Constructor()\n" 10999 " : a(a)\n" 11000 " , b(b)\n" 11001 " , c(c) {}", 11002 Style); 11003 verifyFormat("SomeClass::Constructor()\n" 11004 " : a(a) {\n" 11005 " foo();\n" 11006 " bar();\n" 11007 "}", 11008 Style); 11009 11010 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 11011 verifyFormat("SomeClass::Constructor()\n" 11012 " : a(a)\n" 11013 " , b(b)\n" 11014 " , c(c) {\n}", 11015 Style); 11016 verifyFormat("SomeClass::Constructor()\n" 11017 " : a(a) {\n}", 11018 Style); 11019 11020 Style.ColumnLimit = 80; 11021 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 11022 Style.ConstructorInitializerIndentWidth = 2; 11023 verifyFormat("SomeClass::Constructor()\n" 11024 " : a(a)\n" 11025 " , b(b)\n" 11026 " , c(c) {}", 11027 Style); 11028 11029 Style.ConstructorInitializerIndentWidth = 0; 11030 verifyFormat("SomeClass::Constructor()\n" 11031 ": a(a)\n" 11032 ", b(b)\n" 11033 ", c(c) {}", 11034 Style); 11035 11036 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 11037 Style.ConstructorInitializerIndentWidth = 4; 11038 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style); 11039 verifyFormat( 11040 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n", 11041 Style); 11042 verifyFormat( 11043 "SomeClass::Constructor()\n" 11044 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", 11045 Style); 11046 Style.ConstructorInitializerIndentWidth = 4; 11047 Style.ColumnLimit = 60; 11048 verifyFormat("SomeClass::Constructor()\n" 11049 " : aaaaaaaa(aaaaaaaa)\n" 11050 " , aaaaaaaa(aaaaaaaa)\n" 11051 " , aaaaaaaa(aaaaaaaa) {}", 11052 Style); 11053 } 11054 11055 TEST_F(FormatTest, Destructors) { 11056 verifyFormat("void F(int &i) { i.~int(); }"); 11057 verifyFormat("void F(int &i) { i->~int(); }"); 11058 } 11059 11060 TEST_F(FormatTest, FormatsWithWebKitStyle) { 11061 FormatStyle Style = getWebKitStyle(); 11062 11063 // Don't indent in outer namespaces. 11064 verifyFormat("namespace outer {\n" 11065 "int i;\n" 11066 "namespace inner {\n" 11067 " int i;\n" 11068 "} // namespace inner\n" 11069 "} // namespace outer\n" 11070 "namespace other_outer {\n" 11071 "int i;\n" 11072 "}", 11073 Style); 11074 11075 // Don't indent case labels. 11076 verifyFormat("switch (variable) {\n" 11077 "case 1:\n" 11078 "case 2:\n" 11079 " doSomething();\n" 11080 " break;\n" 11081 "default:\n" 11082 " ++variable;\n" 11083 "}", 11084 Style); 11085 11086 // Wrap before binary operators. 11087 EXPECT_EQ("void f()\n" 11088 "{\n" 11089 " if (aaaaaaaaaaaaaaaa\n" 11090 " && bbbbbbbbbbbbbbbbbbbbbbbb\n" 11091 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 11092 " return;\n" 11093 "}", 11094 format("void f() {\n" 11095 "if (aaaaaaaaaaaaaaaa\n" 11096 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" 11097 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 11098 "return;\n" 11099 "}", 11100 Style)); 11101 11102 // Allow functions on a single line. 11103 verifyFormat("void f() { return; }", Style); 11104 11105 // Constructor initializers are formatted one per line with the "," on the 11106 // new line. 11107 verifyFormat("Constructor()\n" 11108 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 11109 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" 11110 " aaaaaaaaaaaaaa)\n" 11111 " , aaaaaaaaaaaaaaaaaaaaaaa()\n" 11112 "{\n" 11113 "}", 11114 Style); 11115 verifyFormat("SomeClass::Constructor()\n" 11116 " : a(a)\n" 11117 "{\n" 11118 "}", 11119 Style); 11120 EXPECT_EQ("SomeClass::Constructor()\n" 11121 " : a(a)\n" 11122 "{\n" 11123 "}", 11124 format("SomeClass::Constructor():a(a){}", Style)); 11125 verifyFormat("SomeClass::Constructor()\n" 11126 " : a(a)\n" 11127 " , b(b)\n" 11128 " , c(c)\n" 11129 "{\n" 11130 "}", 11131 Style); 11132 verifyFormat("SomeClass::Constructor()\n" 11133 " : a(a)\n" 11134 "{\n" 11135 " foo();\n" 11136 " bar();\n" 11137 "}", 11138 Style); 11139 11140 // Access specifiers should be aligned left. 11141 verifyFormat("class C {\n" 11142 "public:\n" 11143 " int i;\n" 11144 "};", 11145 Style); 11146 11147 // Do not align comments. 11148 verifyFormat("int a; // Do not\n" 11149 "double b; // align comments.", 11150 Style); 11151 11152 // Do not align operands. 11153 EXPECT_EQ("ASSERT(aaaa\n" 11154 " || bbbb);", 11155 format("ASSERT ( aaaa\n||bbbb);", Style)); 11156 11157 // Accept input's line breaks. 11158 EXPECT_EQ("if (aaaaaaaaaaaaaaa\n" 11159 " || bbbbbbbbbbbbbbb) {\n" 11160 " i++;\n" 11161 "}", 11162 format("if (aaaaaaaaaaaaaaa\n" 11163 "|| bbbbbbbbbbbbbbb) { i++; }", 11164 Style)); 11165 EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n" 11166 " i++;\n" 11167 "}", 11168 format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style)); 11169 11170 // Don't automatically break all macro definitions (llvm.org/PR17842). 11171 verifyFormat("#define aNumber 10", Style); 11172 // However, generally keep the line breaks that the user authored. 11173 EXPECT_EQ("#define aNumber \\\n" 11174 " 10", 11175 format("#define aNumber \\\n" 11176 " 10", 11177 Style)); 11178 11179 // Keep empty and one-element array literals on a single line. 11180 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n" 11181 " copyItems:YES];", 11182 format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n" 11183 "copyItems:YES];", 11184 Style)); 11185 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n" 11186 " copyItems:YES];", 11187 format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n" 11188 " copyItems:YES];", 11189 Style)); 11190 // FIXME: This does not seem right, there should be more indentation before 11191 // the array literal's entries. Nested blocks have the same problem. 11192 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 11193 " @\"a\",\n" 11194 " @\"a\"\n" 11195 "]\n" 11196 " copyItems:YES];", 11197 format("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 11198 " @\"a\",\n" 11199 " @\"a\"\n" 11200 " ]\n" 11201 " copyItems:YES];", 11202 Style)); 11203 EXPECT_EQ( 11204 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 11205 " copyItems:YES];", 11206 format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 11207 " copyItems:YES];", 11208 Style)); 11209 11210 verifyFormat("[self.a b:c c:d];", Style); 11211 EXPECT_EQ("[self.a b:c\n" 11212 " c:d];", 11213 format("[self.a b:c\n" 11214 "c:d];", 11215 Style)); 11216 } 11217 11218 TEST_F(FormatTest, FormatsLambdas) { 11219 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n"); 11220 verifyFormat("int c = [&] { [=] { return b++; }(); }();\n"); 11221 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n"); 11222 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n"); 11223 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n"); 11224 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n"); 11225 verifyFormat("auto c = [a = [b = 42] {}] {};\n"); 11226 verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n"); 11227 verifyFormat("int x = f(*+[] {});"); 11228 verifyFormat("void f() {\n" 11229 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n" 11230 "}\n"); 11231 verifyFormat("void f() {\n" 11232 " other(x.begin(), //\n" 11233 " x.end(), //\n" 11234 " [&](int, int) { return 1; });\n" 11235 "}\n"); 11236 verifyFormat("SomeFunction([]() { // A cool function...\n" 11237 " return 43;\n" 11238 "});"); 11239 EXPECT_EQ("SomeFunction([]() {\n" 11240 "#define A a\n" 11241 " return 43;\n" 11242 "});", 11243 format("SomeFunction([](){\n" 11244 "#define A a\n" 11245 "return 43;\n" 11246 "});")); 11247 verifyFormat("void f() {\n" 11248 " SomeFunction([](decltype(x), A *a) {});\n" 11249 "}"); 11250 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 11251 " [](const aaaaaaaaaa &a) { return a; });"); 11252 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n" 11253 " SomeOtherFunctioooooooooooooooooooooooooon();\n" 11254 "});"); 11255 verifyFormat("Constructor()\n" 11256 " : Field([] { // comment\n" 11257 " int i;\n" 11258 " }) {}"); 11259 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n" 11260 " return some_parameter.size();\n" 11261 "};"); 11262 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n" 11263 " [](const string &s) { return s; };"); 11264 verifyFormat("int i = aaaaaa ? 1 //\n" 11265 " : [] {\n" 11266 " return 2; //\n" 11267 " }();"); 11268 verifyFormat("llvm::errs() << \"number of twos is \"\n" 11269 " << std::count_if(v.begin(), v.end(), [](int x) {\n" 11270 " return x == 2; // force break\n" 11271 " });"); 11272 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 11273 " [=](int iiiiiiiiiiii) {\n" 11274 " return aaaaaaaaaaaaaaaaaaaaaaa !=\n" 11275 " aaaaaaaaaaaaaaaaaaaaaaa;\n" 11276 " });", 11277 getLLVMStyleWithColumns(60)); 11278 verifyFormat("SomeFunction({[&] {\n" 11279 " // comment\n" 11280 " },\n" 11281 " [&] {\n" 11282 " // comment\n" 11283 " }});"); 11284 verifyFormat("SomeFunction({[&] {\n" 11285 " // comment\n" 11286 "}});"); 11287 verifyFormat("virtual aaaaaaaaaaaaaaaa(std::function<bool()> bbbbbbbbbbbb =\n" 11288 " [&]() { return true; },\n" 11289 " aaaaa aaaaaaaaa);"); 11290 11291 // Lambdas with return types. 11292 verifyFormat("int c = []() -> int { return 2; }();\n"); 11293 verifyFormat("int c = []() -> int * { return 2; }();\n"); 11294 verifyFormat("int c = []() -> vector<int> { return {2}; }();\n"); 11295 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());"); 11296 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};"); 11297 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};"); 11298 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};"); 11299 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};"); 11300 verifyFormat("[a, a]() -> a<1> {};"); 11301 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n" 11302 " int j) -> int {\n" 11303 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n" 11304 "};"); 11305 verifyFormat( 11306 "aaaaaaaaaaaaaaaaaaaaaa(\n" 11307 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n" 11308 " return aaaaaaaaaaaaaaaaa;\n" 11309 " });", 11310 getLLVMStyleWithColumns(70)); 11311 verifyFormat("[]() //\n" 11312 " -> int {\n" 11313 " return 1; //\n" 11314 "};"); 11315 11316 // Multiple lambdas in the same parentheses change indentation rules. 11317 verifyFormat("SomeFunction(\n" 11318 " []() {\n" 11319 " int i = 42;\n" 11320 " return i;\n" 11321 " },\n" 11322 " []() {\n" 11323 " int j = 43;\n" 11324 " return j;\n" 11325 " });"); 11326 11327 // More complex introducers. 11328 verifyFormat("return [i, args...] {};"); 11329 11330 // Not lambdas. 11331 verifyFormat("constexpr char hello[]{\"hello\"};"); 11332 verifyFormat("double &operator[](int i) { return 0; }\n" 11333 "int i;"); 11334 verifyFormat("std::unique_ptr<int[]> foo() {}"); 11335 verifyFormat("int i = a[a][a]->f();"); 11336 verifyFormat("int i = (*b)[a]->f();"); 11337 11338 // Other corner cases. 11339 verifyFormat("void f() {\n" 11340 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n" 11341 " );\n" 11342 "}"); 11343 11344 // Lambdas created through weird macros. 11345 verifyFormat("void f() {\n" 11346 " MACRO((const AA &a) { return 1; });\n" 11347 " MACRO((AA &a) { return 1; });\n" 11348 "}"); 11349 11350 verifyFormat("if (blah_blah(whatever, whatever, [] {\n" 11351 " doo_dah();\n" 11352 " doo_dah();\n" 11353 " })) {\n" 11354 "}"); 11355 verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n" 11356 " doo_dah();\n" 11357 " doo_dah();\n" 11358 " })) {\n" 11359 "}"); 11360 verifyFormat("auto lambda = []() {\n" 11361 " int a = 2\n" 11362 "#if A\n" 11363 " + 2\n" 11364 "#endif\n" 11365 " ;\n" 11366 "};"); 11367 11368 // Lambdas with complex multiline introducers. 11369 verifyFormat( 11370 "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 11371 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n" 11372 " -> ::std::unordered_set<\n" 11373 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n" 11374 " //\n" 11375 " });"); 11376 } 11377 11378 TEST_F(FormatTest, EmptyLinesInLambdas) { 11379 verifyFormat("auto lambda = []() {\n" 11380 " x(); //\n" 11381 "};", 11382 "auto lambda = []() {\n" 11383 "\n" 11384 " x(); //\n" 11385 "\n" 11386 "};"); 11387 } 11388 11389 TEST_F(FormatTest, FormatsBlocks) { 11390 FormatStyle ShortBlocks = getLLVMStyle(); 11391 ShortBlocks.AllowShortBlocksOnASingleLine = true; 11392 verifyFormat("int (^Block)(int, int);", ShortBlocks); 11393 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks); 11394 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks); 11395 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks); 11396 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks); 11397 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks); 11398 11399 verifyFormat("foo(^{ bar(); });", ShortBlocks); 11400 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks); 11401 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks); 11402 11403 verifyFormat("[operation setCompletionBlock:^{\n" 11404 " [self onOperationDone];\n" 11405 "}];"); 11406 verifyFormat("int i = {[operation setCompletionBlock:^{\n" 11407 " [self onOperationDone];\n" 11408 "}]};"); 11409 verifyFormat("[operation setCompletionBlock:^(int *i) {\n" 11410 " f();\n" 11411 "}];"); 11412 verifyFormat("int a = [operation block:^int(int *i) {\n" 11413 " return 1;\n" 11414 "}];"); 11415 verifyFormat("[myObject doSomethingWith:arg1\n" 11416 " aaa:^int(int *a) {\n" 11417 " return 1;\n" 11418 " }\n" 11419 " bbb:f(a * bbbbbbbb)];"); 11420 11421 verifyFormat("[operation setCompletionBlock:^{\n" 11422 " [self.delegate newDataAvailable];\n" 11423 "}];", 11424 getLLVMStyleWithColumns(60)); 11425 verifyFormat("dispatch_async(_fileIOQueue, ^{\n" 11426 " NSString *path = [self sessionFilePath];\n" 11427 " if (path) {\n" 11428 " // ...\n" 11429 " }\n" 11430 "});"); 11431 verifyFormat("[[SessionService sharedService]\n" 11432 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 11433 " if (window) {\n" 11434 " [self windowDidLoad:window];\n" 11435 " } else {\n" 11436 " [self errorLoadingWindow];\n" 11437 " }\n" 11438 " }];"); 11439 verifyFormat("void (^largeBlock)(void) = ^{\n" 11440 " // ...\n" 11441 "};\n", 11442 getLLVMStyleWithColumns(40)); 11443 verifyFormat("[[SessionService sharedService]\n" 11444 " loadWindowWithCompletionBlock: //\n" 11445 " ^(SessionWindow *window) {\n" 11446 " if (window) {\n" 11447 " [self windowDidLoad:window];\n" 11448 " } else {\n" 11449 " [self errorLoadingWindow];\n" 11450 " }\n" 11451 " }];", 11452 getLLVMStyleWithColumns(60)); 11453 verifyFormat("[myObject doSomethingWith:arg1\n" 11454 " firstBlock:^(Foo *a) {\n" 11455 " // ...\n" 11456 " int i;\n" 11457 " }\n" 11458 " secondBlock:^(Bar *b) {\n" 11459 " // ...\n" 11460 " int i;\n" 11461 " }\n" 11462 " thirdBlock:^Foo(Bar *b) {\n" 11463 " // ...\n" 11464 " int i;\n" 11465 " }];"); 11466 verifyFormat("[myObject doSomethingWith:arg1\n" 11467 " firstBlock:-1\n" 11468 " secondBlock:^(Bar *b) {\n" 11469 " // ...\n" 11470 " int i;\n" 11471 " }];"); 11472 11473 verifyFormat("f(^{\n" 11474 " @autoreleasepool {\n" 11475 " if (a) {\n" 11476 " g();\n" 11477 " }\n" 11478 " }\n" 11479 "});"); 11480 verifyFormat("Block b = ^int *(A *a, B *b) {}"); 11481 verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n" 11482 "};"); 11483 11484 FormatStyle FourIndent = getLLVMStyle(); 11485 FourIndent.ObjCBlockIndentWidth = 4; 11486 verifyFormat("[operation setCompletionBlock:^{\n" 11487 " [self onOperationDone];\n" 11488 "}];", 11489 FourIndent); 11490 } 11491 11492 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) { 11493 FormatStyle ZeroColumn = getLLVMStyle(); 11494 ZeroColumn.ColumnLimit = 0; 11495 11496 verifyFormat("[[SessionService sharedService] " 11497 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 11498 " if (window) {\n" 11499 " [self windowDidLoad:window];\n" 11500 " } else {\n" 11501 " [self errorLoadingWindow];\n" 11502 " }\n" 11503 "}];", 11504 ZeroColumn); 11505 EXPECT_EQ("[[SessionService sharedService]\n" 11506 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 11507 " if (window) {\n" 11508 " [self windowDidLoad:window];\n" 11509 " } else {\n" 11510 " [self errorLoadingWindow];\n" 11511 " }\n" 11512 " }];", 11513 format("[[SessionService sharedService]\n" 11514 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 11515 " if (window) {\n" 11516 " [self windowDidLoad:window];\n" 11517 " } else {\n" 11518 " [self errorLoadingWindow];\n" 11519 " }\n" 11520 "}];", 11521 ZeroColumn)); 11522 verifyFormat("[myObject doSomethingWith:arg1\n" 11523 " firstBlock:^(Foo *a) {\n" 11524 " // ...\n" 11525 " int i;\n" 11526 " }\n" 11527 " secondBlock:^(Bar *b) {\n" 11528 " // ...\n" 11529 " int i;\n" 11530 " }\n" 11531 " thirdBlock:^Foo(Bar *b) {\n" 11532 " // ...\n" 11533 " int i;\n" 11534 " }];", 11535 ZeroColumn); 11536 verifyFormat("f(^{\n" 11537 " @autoreleasepool {\n" 11538 " if (a) {\n" 11539 " g();\n" 11540 " }\n" 11541 " }\n" 11542 "});", 11543 ZeroColumn); 11544 verifyFormat("void (^largeBlock)(void) = ^{\n" 11545 " // ...\n" 11546 "};", 11547 ZeroColumn); 11548 11549 ZeroColumn.AllowShortBlocksOnASingleLine = true; 11550 EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };", 11551 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 11552 ZeroColumn.AllowShortBlocksOnASingleLine = false; 11553 EXPECT_EQ("void (^largeBlock)(void) = ^{\n" 11554 " int i;\n" 11555 "};", 11556 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 11557 } 11558 11559 TEST_F(FormatTest, SupportsCRLF) { 11560 EXPECT_EQ("int a;\r\n" 11561 "int b;\r\n" 11562 "int c;\r\n", 11563 format("int a;\r\n" 11564 " int b;\r\n" 11565 " int c;\r\n", 11566 getLLVMStyle())); 11567 EXPECT_EQ("int a;\r\n" 11568 "int b;\r\n" 11569 "int c;\r\n", 11570 format("int a;\r\n" 11571 " int b;\n" 11572 " int c;\r\n", 11573 getLLVMStyle())); 11574 EXPECT_EQ("int a;\n" 11575 "int b;\n" 11576 "int c;\n", 11577 format("int a;\r\n" 11578 " int b;\n" 11579 " int c;\n", 11580 getLLVMStyle())); 11581 EXPECT_EQ("\"aaaaaaa \"\r\n" 11582 "\"bbbbbbb\";\r\n", 11583 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10))); 11584 EXPECT_EQ("#define A \\\r\n" 11585 " b; \\\r\n" 11586 " c; \\\r\n" 11587 " d;\r\n", 11588 format("#define A \\\r\n" 11589 " b; \\\r\n" 11590 " c; d; \r\n", 11591 getGoogleStyle())); 11592 11593 EXPECT_EQ("/*\r\n" 11594 "multi line block comments\r\n" 11595 "should not introduce\r\n" 11596 "an extra carriage return\r\n" 11597 "*/\r\n", 11598 format("/*\r\n" 11599 "multi line block comments\r\n" 11600 "should not introduce\r\n" 11601 "an extra carriage return\r\n" 11602 "*/\r\n")); 11603 } 11604 11605 TEST_F(FormatTest, MunchSemicolonAfterBlocks) { 11606 verifyFormat("MY_CLASS(C) {\n" 11607 " int i;\n" 11608 " int j;\n" 11609 "};"); 11610 } 11611 11612 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) { 11613 FormatStyle TwoIndent = getLLVMStyleWithColumns(15); 11614 TwoIndent.ContinuationIndentWidth = 2; 11615 11616 EXPECT_EQ("int i =\n" 11617 " longFunction(\n" 11618 " arg);", 11619 format("int i = longFunction(arg);", TwoIndent)); 11620 11621 FormatStyle SixIndent = getLLVMStyleWithColumns(20); 11622 SixIndent.ContinuationIndentWidth = 6; 11623 11624 EXPECT_EQ("int i =\n" 11625 " longFunction(\n" 11626 " arg);", 11627 format("int i = longFunction(arg);", SixIndent)); 11628 } 11629 11630 TEST_F(FormatTest, SpacesInAngles) { 11631 FormatStyle Spaces = getLLVMStyle(); 11632 Spaces.SpacesInAngles = true; 11633 11634 verifyFormat("static_cast< int >(arg);", Spaces); 11635 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces); 11636 verifyFormat("f< int, float >();", Spaces); 11637 verifyFormat("template <> g() {}", Spaces); 11638 verifyFormat("template < std::vector< int > > f() {}", Spaces); 11639 verifyFormat("std::function< void(int, int) > fct;", Spaces); 11640 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }", 11641 Spaces); 11642 11643 Spaces.Standard = FormatStyle::LS_Cpp03; 11644 Spaces.SpacesInAngles = true; 11645 verifyFormat("A< A< int > >();", Spaces); 11646 11647 Spaces.SpacesInAngles = false; 11648 verifyFormat("A<A<int> >();", Spaces); 11649 11650 Spaces.Standard = FormatStyle::LS_Cpp11; 11651 Spaces.SpacesInAngles = true; 11652 verifyFormat("A< A< int > >();", Spaces); 11653 11654 Spaces.SpacesInAngles = false; 11655 verifyFormat("A<A<int>>();", Spaces); 11656 } 11657 11658 TEST_F(FormatTest, SpaceAfterTemplateKeyword) { 11659 FormatStyle Style = getLLVMStyle(); 11660 Style.SpaceAfterTemplateKeyword = false; 11661 verifyFormat("template<int> void foo();", Style); 11662 } 11663 11664 TEST_F(FormatTest, TripleAngleBrackets) { 11665 verifyFormat("f<<<1, 1>>>();"); 11666 verifyFormat("f<<<1, 1, 1, s>>>();"); 11667 verifyFormat("f<<<a, b, c, d>>>();"); 11668 EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();")); 11669 verifyFormat("f<param><<<1, 1>>>();"); 11670 verifyFormat("f<1><<<1, 1>>>();"); 11671 EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();")); 11672 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 11673 "aaaaaaaaaaa<<<\n 1, 1>>>();"); 11674 verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n" 11675 " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();"); 11676 } 11677 11678 TEST_F(FormatTest, MergeLessLessAtEnd) { 11679 verifyFormat("<<"); 11680 EXPECT_EQ("< < <", format("\\\n<<<")); 11681 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 11682 "aaallvm::outs() <<"); 11683 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 11684 "aaaallvm::outs()\n <<"); 11685 } 11686 11687 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) { 11688 std::string code = "#if A\n" 11689 "#if B\n" 11690 "a.\n" 11691 "#endif\n" 11692 " a = 1;\n" 11693 "#else\n" 11694 "#endif\n" 11695 "#if C\n" 11696 "#else\n" 11697 "#endif\n"; 11698 EXPECT_EQ(code, format(code)); 11699 } 11700 11701 TEST_F(FormatTest, HandleConflictMarkers) { 11702 // Git/SVN conflict markers. 11703 EXPECT_EQ("int a;\n" 11704 "void f() {\n" 11705 " callme(some(parameter1,\n" 11706 "<<<<<<< text by the vcs\n" 11707 " parameter2),\n" 11708 "||||||| text by the vcs\n" 11709 " parameter2),\n" 11710 " parameter3,\n" 11711 "======= text by the vcs\n" 11712 " parameter2, parameter3),\n" 11713 ">>>>>>> text by the vcs\n" 11714 " otherparameter);\n", 11715 format("int a;\n" 11716 "void f() {\n" 11717 " callme(some(parameter1,\n" 11718 "<<<<<<< text by the vcs\n" 11719 " parameter2),\n" 11720 "||||||| text by the vcs\n" 11721 " parameter2),\n" 11722 " parameter3,\n" 11723 "======= text by the vcs\n" 11724 " parameter2,\n" 11725 " parameter3),\n" 11726 ">>>>>>> text by the vcs\n" 11727 " otherparameter);\n")); 11728 11729 // Perforce markers. 11730 EXPECT_EQ("void f() {\n" 11731 " function(\n" 11732 ">>>> text by the vcs\n" 11733 " parameter,\n" 11734 "==== text by the vcs\n" 11735 " parameter,\n" 11736 "==== text by the vcs\n" 11737 " parameter,\n" 11738 "<<<< text by the vcs\n" 11739 " parameter);\n", 11740 format("void f() {\n" 11741 " function(\n" 11742 ">>>> text by the vcs\n" 11743 " parameter,\n" 11744 "==== text by the vcs\n" 11745 " parameter,\n" 11746 "==== text by the vcs\n" 11747 " parameter,\n" 11748 "<<<< text by the vcs\n" 11749 " parameter);\n")); 11750 11751 EXPECT_EQ("<<<<<<<\n" 11752 "|||||||\n" 11753 "=======\n" 11754 ">>>>>>>", 11755 format("<<<<<<<\n" 11756 "|||||||\n" 11757 "=======\n" 11758 ">>>>>>>")); 11759 11760 EXPECT_EQ("<<<<<<<\n" 11761 "|||||||\n" 11762 "int i;\n" 11763 "=======\n" 11764 ">>>>>>>", 11765 format("<<<<<<<\n" 11766 "|||||||\n" 11767 "int i;\n" 11768 "=======\n" 11769 ">>>>>>>")); 11770 11771 // FIXME: Handle parsing of macros around conflict markers correctly: 11772 EXPECT_EQ("#define Macro \\\n" 11773 "<<<<<<<\n" 11774 "Something \\\n" 11775 "|||||||\n" 11776 "Else \\\n" 11777 "=======\n" 11778 "Other \\\n" 11779 ">>>>>>>\n" 11780 " End int i;\n", 11781 format("#define Macro \\\n" 11782 "<<<<<<<\n" 11783 " Something \\\n" 11784 "|||||||\n" 11785 " Else \\\n" 11786 "=======\n" 11787 " Other \\\n" 11788 ">>>>>>>\n" 11789 " End\n" 11790 "int i;\n")); 11791 } 11792 11793 TEST_F(FormatTest, DisableRegions) { 11794 EXPECT_EQ("int i;\n" 11795 "// clang-format off\n" 11796 " int j;\n" 11797 "// clang-format on\n" 11798 "int k;", 11799 format(" int i;\n" 11800 " // clang-format off\n" 11801 " int j;\n" 11802 " // clang-format on\n" 11803 " int k;")); 11804 EXPECT_EQ("int i;\n" 11805 "/* clang-format off */\n" 11806 " int j;\n" 11807 "/* clang-format on */\n" 11808 "int k;", 11809 format(" int i;\n" 11810 " /* clang-format off */\n" 11811 " int j;\n" 11812 " /* clang-format on */\n" 11813 " int k;")); 11814 11815 // Don't reflow comments within disabled regions. 11816 EXPECT_EQ( 11817 "// clang-format off\n" 11818 "// long long long long long long line\n" 11819 "/* clang-format on */\n" 11820 "/* long long long\n" 11821 " * long long long\n" 11822 " * line */\n" 11823 "int i;\n" 11824 "/* clang-format off */\n" 11825 "/* long long long long long long line */\n", 11826 format("// clang-format off\n" 11827 "// long long long long long long line\n" 11828 "/* clang-format on */\n" 11829 "/* long long long long long long line */\n" 11830 "int i;\n" 11831 "/* clang-format off */\n" 11832 "/* long long long long long long line */\n", 11833 getLLVMStyleWithColumns(20))); 11834 } 11835 11836 TEST_F(FormatTest, DoNotCrashOnInvalidInput) { 11837 format("? ) ="); 11838 verifyNoCrash("#define a\\\n /**/}"); 11839 } 11840 11841 TEST_F(FormatTest, FormatsTableGenCode) { 11842 FormatStyle Style = getLLVMStyle(); 11843 Style.Language = FormatStyle::LK_TableGen; 11844 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style); 11845 } 11846 11847 TEST_F(FormatTest, ArrayOfTemplates) { 11848 EXPECT_EQ("auto a = new unique_ptr<int>[10];", 11849 format("auto a = new unique_ptr<int > [ 10];")); 11850 11851 FormatStyle Spaces = getLLVMStyle(); 11852 Spaces.SpacesInSquareBrackets = true; 11853 EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];", 11854 format("auto a = new unique_ptr<int > [10];", Spaces)); 11855 } 11856 11857 TEST_F(FormatTest, ArrayAsTemplateType) { 11858 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;", 11859 format("auto a = unique_ptr < Foo < Bar>[ 10]> ;")); 11860 11861 FormatStyle Spaces = getLLVMStyle(); 11862 Spaces.SpacesInSquareBrackets = true; 11863 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;", 11864 format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces)); 11865 } 11866 11867 TEST_F(FormatTest, NoSpaceAfterSuper) { 11868 verifyFormat("__super::FooBar();"); 11869 } 11870 11871 TEST(FormatStyle, GetStyleWithEmptyFileName) { 11872 vfs::InMemoryFileSystem FS; 11873 auto Style1 = getStyle("file", "", "Google", "", &FS); 11874 ASSERT_TRUE((bool)Style1); 11875 ASSERT_EQ(*Style1, getGoogleStyle()); 11876 } 11877 11878 TEST(FormatStyle, GetStyleOfFile) { 11879 vfs::InMemoryFileSystem FS; 11880 // Test 1: format file in the same directory. 11881 ASSERT_TRUE( 11882 FS.addFile("/a/.clang-format", 0, 11883 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM"))); 11884 ASSERT_TRUE( 11885 FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 11886 auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS); 11887 ASSERT_TRUE((bool)Style1); 11888 ASSERT_EQ(*Style1, getLLVMStyle()); 11889 11890 // Test 2.1: fallback to default. 11891 ASSERT_TRUE( 11892 FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 11893 auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS); 11894 ASSERT_TRUE((bool)Style2); 11895 ASSERT_EQ(*Style2, getMozillaStyle()); 11896 11897 // Test 2.2: no format on 'none' fallback style. 11898 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 11899 ASSERT_TRUE((bool)Style2); 11900 ASSERT_EQ(*Style2, getNoStyle()); 11901 11902 // Test 2.3: format if config is found with no based style while fallback is 11903 // 'none'. 11904 ASSERT_TRUE(FS.addFile("/b/.clang-format", 0, 11905 llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2"))); 11906 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 11907 ASSERT_TRUE((bool)Style2); 11908 ASSERT_EQ(*Style2, getLLVMStyle()); 11909 11910 // Test 2.4: format if yaml with no based style, while fallback is 'none'. 11911 Style2 = getStyle("{}", "a.h", "none", "", &FS); 11912 ASSERT_TRUE((bool)Style2); 11913 ASSERT_EQ(*Style2, getLLVMStyle()); 11914 11915 // Test 3: format file in parent directory. 11916 ASSERT_TRUE( 11917 FS.addFile("/c/.clang-format", 0, 11918 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 11919 ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0, 11920 llvm::MemoryBuffer::getMemBuffer("int i;"))); 11921 auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS); 11922 ASSERT_TRUE((bool)Style3); 11923 ASSERT_EQ(*Style3, getGoogleStyle()); 11924 11925 // Test 4: error on invalid fallback style 11926 auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS); 11927 ASSERT_FALSE((bool)Style4); 11928 llvm::consumeError(Style4.takeError()); 11929 11930 // Test 5: error on invalid yaml on command line 11931 auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS); 11932 ASSERT_FALSE((bool)Style5); 11933 llvm::consumeError(Style5.takeError()); 11934 11935 // Test 6: error on invalid style 11936 auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS); 11937 ASSERT_FALSE((bool)Style6); 11938 llvm::consumeError(Style6.takeError()); 11939 11940 // Test 7: found config file, error on parsing it 11941 ASSERT_TRUE( 11942 FS.addFile("/d/.clang-format", 0, 11943 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n" 11944 "InvalidKey: InvalidValue"))); 11945 ASSERT_TRUE( 11946 FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 11947 auto Style7 = getStyle("file", "/d/.clang-format", "LLVM", "", &FS); 11948 ASSERT_FALSE((bool)Style7); 11949 llvm::consumeError(Style7.takeError()); 11950 } 11951 11952 TEST_F(ReplacementTest, FormatCodeAfterReplacements) { 11953 // Column limit is 20. 11954 std::string Code = "Type *a =\n" 11955 " new Type();\n" 11956 "g(iiiii, 0, jjjjj,\n" 11957 " 0, kkkkk, 0, mm);\n" 11958 "int bad = format ;"; 11959 std::string Expected = "auto a = new Type();\n" 11960 "g(iiiii, nullptr,\n" 11961 " jjjjj, nullptr,\n" 11962 " kkkkk, nullptr,\n" 11963 " mm);\n" 11964 "int bad = format ;"; 11965 FileID ID = Context.createInMemoryFile("format.cpp", Code); 11966 tooling::Replacements Replaces = toReplacements( 11967 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6, 11968 "auto "), 11969 tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1, 11970 "nullptr"), 11971 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1, 11972 "nullptr"), 11973 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1, 11974 "nullptr")}); 11975 11976 format::FormatStyle Style = format::getLLVMStyle(); 11977 Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility. 11978 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 11979 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 11980 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 11981 auto Result = applyAllReplacements(Code, *FormattedReplaces); 11982 EXPECT_TRUE(static_cast<bool>(Result)); 11983 EXPECT_EQ(Expected, *Result); 11984 } 11985 11986 TEST_F(ReplacementTest, SortIncludesAfterReplacement) { 11987 std::string Code = "#include \"a.h\"\n" 11988 "#include \"c.h\"\n" 11989 "\n" 11990 "int main() {\n" 11991 " return 0;\n" 11992 "}"; 11993 std::string Expected = "#include \"a.h\"\n" 11994 "#include \"b.h\"\n" 11995 "#include \"c.h\"\n" 11996 "\n" 11997 "int main() {\n" 11998 " return 0;\n" 11999 "}"; 12000 FileID ID = Context.createInMemoryFile("fix.cpp", Code); 12001 tooling::Replacements Replaces = toReplacements( 12002 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0, 12003 "#include \"b.h\"\n")}); 12004 12005 format::FormatStyle Style = format::getLLVMStyle(); 12006 Style.SortIncludes = true; 12007 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 12008 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 12009 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 12010 auto Result = applyAllReplacements(Code, *FormattedReplaces); 12011 EXPECT_TRUE(static_cast<bool>(Result)); 12012 EXPECT_EQ(Expected, *Result); 12013 } 12014 12015 TEST_F(FormatTest, FormatSortsUsingDeclarations) { 12016 EXPECT_EQ("using std::cin;\n" 12017 "using std::cout;", 12018 format("using std::cout;\n" 12019 "using std::cin;", getGoogleStyle())); 12020 } 12021 12022 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) { 12023 format::FormatStyle Style = format::getLLVMStyle(); 12024 Style.Standard = FormatStyle::LS_Cpp03; 12025 // cpp03 recognize this string as identifier u8 and literal character 'a' 12026 EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style)); 12027 } 12028 12029 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) { 12030 // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers 12031 // all modes, including C++11, C++14 and C++17 12032 EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';")); 12033 } 12034 12035 TEST_F(FormatTest, DoNotFormatLikelyXml) { 12036 EXPECT_EQ("<!-- ;> -->", 12037 format("<!-- ;> -->", getGoogleStyle())); 12038 EXPECT_EQ(" <!-- >; -->", 12039 format(" <!-- >; -->", getGoogleStyle())); 12040 } 12041 12042 TEST_F(FormatTest, StructuredBindings) { 12043 // Structured bindings is a C++17 feature. 12044 // all modes, including C++11, C++14 and C++17 12045 verifyFormat("auto [a, b] = f();"); 12046 EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();")); 12047 EXPECT_EQ("const auto [a, b] = f();", format("const auto[a, b] = f();")); 12048 EXPECT_EQ("auto const [a, b] = f();", format("auto const[a, b] = f();")); 12049 EXPECT_EQ("auto const volatile [a, b] = f();", 12050 format("auto const volatile[a, b] = f();")); 12051 EXPECT_EQ("auto [a, b, c] = f();", format("auto [ a , b,c ] = f();")); 12052 EXPECT_EQ("auto &[a, b, c] = f();", 12053 format("auto &[ a , b,c ] = f();")); 12054 EXPECT_EQ("auto &&[a, b, c] = f();", 12055 format("auto &&[ a , b,c ] = f();")); 12056 EXPECT_EQ("auto const &[a, b] = f();", format("auto const&[a, b] = f();")); 12057 EXPECT_EQ("auto const volatile &&[a, b] = f();", 12058 format("auto const volatile &&[a, b] = f();")); 12059 EXPECT_EQ("auto const &&[a, b] = f();", format("auto const && [a, b] = f();")); 12060 EXPECT_EQ("const auto &[a, b] = f();", format("const auto & [a, b] = f();")); 12061 EXPECT_EQ("const auto volatile &&[a, b] = f();", 12062 format("const auto volatile &&[a, b] = f();")); 12063 EXPECT_EQ("volatile const auto &&[a, b] = f();", 12064 format("volatile const auto &&[a, b] = f();")); 12065 EXPECT_EQ("const auto &&[a, b] = f();", format("const auto && [a, b] = f();")); 12066 12067 // Make sure we don't mistake structured bindings for lambdas. 12068 FormatStyle PointerMiddle = getLLVMStyle(); 12069 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 12070 verifyFormat("auto [a1, b]{A * i};", getGoogleStyle()); 12071 verifyFormat("auto [a2, b]{A * i};", getLLVMStyle()); 12072 verifyFormat("auto [a3, b]{A * i};", PointerMiddle); 12073 verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle()); 12074 verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle()); 12075 verifyFormat("auto const [a3, b]{A * i};", PointerMiddle); 12076 verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle()); 12077 verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle()); 12078 verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle); 12079 verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle()); 12080 verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle()); 12081 verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle); 12082 12083 EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}", 12084 format("for (const auto && [a, b] : some_range) {\n}")); 12085 EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}", 12086 format("for (const auto & [a, b] : some_range) {\n}")); 12087 EXPECT_EQ("for (const auto [a, b] : some_range) {\n}", 12088 format("for (const auto[a, b] : some_range) {\n}")); 12089 EXPECT_EQ("auto [x, y](expr);", format("auto[x,y] (expr);")); 12090 EXPECT_EQ("auto &[x, y](expr);", format("auto & [x,y] (expr);")); 12091 EXPECT_EQ("auto &&[x, y](expr);", format("auto && [x,y] (expr);")); 12092 EXPECT_EQ("auto const &[x, y](expr);", format("auto const & [x,y] (expr);")); 12093 EXPECT_EQ("auto const &&[x, y](expr);", format("auto const && [x,y] (expr);")); 12094 EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y] {expr};")); 12095 EXPECT_EQ("auto const &[x, y]{expr};", format("auto const & [x,y] {expr};")); 12096 EXPECT_EQ("auto const &&[x, y]{expr};", format("auto const && [x,y] {expr};")); 12097 12098 format::FormatStyle Spaces = format::getLLVMStyle(); 12099 Spaces.SpacesInSquareBrackets = true; 12100 verifyFormat("auto [ a, b ] = f();", Spaces); 12101 verifyFormat("auto &&[ a, b ] = f();", Spaces); 12102 verifyFormat("auto &[ a, b ] = f();", Spaces); 12103 verifyFormat("auto const &&[ a, b ] = f();", Spaces); 12104 verifyFormat("auto const &[ a, b ] = f();", Spaces); 12105 } 12106 12107 TEST_F(FormatTest, FileAndCode) { 12108 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", "")); 12109 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", "")); 12110 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", "")); 12111 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "")); 12112 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@interface Foo\n@end\n")); 12113 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", "")); 12114 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo", "@interface Foo\n@end\n")); 12115 EXPECT_EQ(FormatStyle::LK_ObjC, 12116 guessLanguage("foo.h", "int DoStuff(CGRect rect);\n")); 12117 EXPECT_EQ( 12118 FormatStyle::LK_ObjC, 12119 guessLanguage("foo.h", 12120 "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));\n")); 12121 } 12122 12123 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) { 12124 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];")); 12125 EXPECT_EQ(FormatStyle::LK_ObjC, 12126 guessLanguage("foo.h", "array[[calculator getIndex]];")); 12127 EXPECT_EQ(FormatStyle::LK_Cpp, 12128 guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];")); 12129 EXPECT_EQ( 12130 FormatStyle::LK_Cpp, 12131 guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];")); 12132 EXPECT_EQ(FormatStyle::LK_ObjC, 12133 guessLanguage("foo.h", "[[noreturn foo] bar];")); 12134 EXPECT_EQ(FormatStyle::LK_Cpp, 12135 guessLanguage("foo.h", "[[clang::fallthrough]];")); 12136 EXPECT_EQ(FormatStyle::LK_ObjC, 12137 guessLanguage("foo.h", "[[clang:fallthrough] foo];")); 12138 EXPECT_EQ(FormatStyle::LK_Cpp, 12139 guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];")); 12140 EXPECT_EQ(FormatStyle::LK_Cpp, 12141 guessLanguage("foo.h", "[[using clang: fallthrough]];")); 12142 EXPECT_EQ(FormatStyle::LK_ObjC, 12143 guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];")); 12144 EXPECT_EQ(FormatStyle::LK_Cpp, 12145 guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];")); 12146 EXPECT_EQ( 12147 FormatStyle::LK_Cpp, 12148 guessLanguage("foo.h", 12149 "[[clang::callable_when(\"unconsumed\", \"unknown\")]]")); 12150 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]")); 12151 } 12152 12153 TEST_F(FormatTest, GuessLanguageWithCaret) { 12154 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);")); 12155 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);")); 12156 EXPECT_EQ(FormatStyle::LK_ObjC, 12157 guessLanguage("foo.h", "int(^)(char, float);")); 12158 EXPECT_EQ(FormatStyle::LK_ObjC, 12159 guessLanguage("foo.h", "int(^foo)(char, float);")); 12160 EXPECT_EQ(FormatStyle::LK_ObjC, 12161 guessLanguage("foo.h", "int(^foo[10])(char, float);")); 12162 EXPECT_EQ(FormatStyle::LK_ObjC, 12163 guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);")); 12164 EXPECT_EQ( 12165 FormatStyle::LK_ObjC, 12166 guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);")); 12167 } 12168 12169 TEST_F(FormatTest, GuessLanguageWithChildLines) { 12170 EXPECT_EQ(FormatStyle::LK_Cpp, 12171 guessLanguage("foo.h", "#define FOO ({ std::string s; })")); 12172 EXPECT_EQ(FormatStyle::LK_ObjC, 12173 guessLanguage("foo.h", "#define FOO ({ NSString *s; })")); 12174 } 12175 12176 } // end namespace 12177 } // end namespace format 12178 } // end namespace clang 12179