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(Expected, Style)) 76 << "Expected code is not stable"; 77 EXPECT_EQ(Expected.str(), format(Code, Style)); 78 if (Style.Language == FormatStyle::LK_Cpp) { 79 // Objective-C++ is a superset of C++, so everything checked for C++ 80 // needs to be checked for Objective-C++ as well. 81 FormatStyle ObjCStyle = Style; 82 ObjCStyle.Language = FormatStyle::LK_ObjC; 83 EXPECT_EQ(Expected.str(), format(test::messUp(Code), ObjCStyle)); 84 } 85 } 86 87 void verifyFormat(llvm::StringRef Code, 88 const FormatStyle &Style = getLLVMStyle()) { 89 verifyFormat(Code, test::messUp(Code), Style); 90 } 91 92 void verifyIncompleteFormat(llvm::StringRef Code, 93 const FormatStyle &Style = getLLVMStyle()) { 94 EXPECT_EQ(Code.str(), 95 format(test::messUp(Code), Style, SC_ExpectIncomplete)); 96 } 97 98 void verifyGoogleFormat(llvm::StringRef Code) { 99 verifyFormat(Code, getGoogleStyle()); 100 } 101 102 void verifyIndependentOfContext(llvm::StringRef text) { 103 verifyFormat(text); 104 verifyFormat(llvm::Twine("void f() { " + text + " }").str()); 105 } 106 107 /// \brief Verify that clang-format does not crash on the given input. 108 void verifyNoCrash(llvm::StringRef Code, 109 const FormatStyle &Style = getLLVMStyle()) { 110 format(Code, Style, SC_DoNotCheck); 111 } 112 113 int ReplacementCount; 114 }; 115 116 TEST_F(FormatTest, MessUp) { 117 EXPECT_EQ("1 2 3", test::messUp("1 2 3")); 118 EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n")); 119 EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc")); 120 EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc")); 121 EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne")); 122 } 123 124 //===----------------------------------------------------------------------===// 125 // Basic function tests. 126 //===----------------------------------------------------------------------===// 127 128 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) { 129 EXPECT_EQ(";", format(";")); 130 } 131 132 TEST_F(FormatTest, FormatsGlobalStatementsAt0) { 133 EXPECT_EQ("int i;", format(" int i;")); 134 EXPECT_EQ("\nint i;", format(" \n\t \v \f int i;")); 135 EXPECT_EQ("int i;\nint j;", format(" int i; int j;")); 136 EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;")); 137 } 138 139 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) { 140 EXPECT_EQ("int i;", format("int\ni;")); 141 } 142 143 TEST_F(FormatTest, FormatsNestedBlockStatements) { 144 EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}")); 145 } 146 147 TEST_F(FormatTest, FormatsNestedCall) { 148 verifyFormat("Method(f1, f2(f3));"); 149 verifyFormat("Method(f1(f2, f3()));"); 150 verifyFormat("Method(f1(f2, (f3())));"); 151 } 152 153 TEST_F(FormatTest, NestedNameSpecifiers) { 154 verifyFormat("vector<::Type> v;"); 155 verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())"); 156 verifyFormat("static constexpr bool Bar = decltype(bar())::value;"); 157 verifyFormat("bool a = 2 < ::SomeFunction();"); 158 verifyFormat("ALWAYS_INLINE ::std::string getName();"); 159 verifyFormat("some::string getName();"); 160 } 161 162 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) { 163 EXPECT_EQ("if (a) {\n" 164 " f();\n" 165 "}", 166 format("if(a){f();}")); 167 EXPECT_EQ(4, ReplacementCount); 168 EXPECT_EQ("if (a) {\n" 169 " f();\n" 170 "}", 171 format("if (a) {\n" 172 " f();\n" 173 "}")); 174 EXPECT_EQ(0, ReplacementCount); 175 EXPECT_EQ("/*\r\n" 176 "\r\n" 177 "*/\r\n", 178 format("/*\r\n" 179 "\r\n" 180 "*/\r\n")); 181 EXPECT_EQ(0, ReplacementCount); 182 } 183 184 TEST_F(FormatTest, RemovesEmptyLines) { 185 EXPECT_EQ("class C {\n" 186 " int i;\n" 187 "};", 188 format("class C {\n" 189 " int i;\n" 190 "\n" 191 "};")); 192 193 // Don't remove empty lines at the start of namespaces or extern "C" blocks. 194 EXPECT_EQ("namespace N {\n" 195 "\n" 196 "int i;\n" 197 "}", 198 format("namespace N {\n" 199 "\n" 200 "int i;\n" 201 "}", 202 getGoogleStyle())); 203 EXPECT_EQ("extern /**/ \"C\" /**/ {\n" 204 "\n" 205 "int i;\n" 206 "}", 207 format("extern /**/ \"C\" /**/ {\n" 208 "\n" 209 "int i;\n" 210 "}", 211 getGoogleStyle())); 212 213 // ...but do keep inlining and removing empty lines for non-block extern "C" 214 // functions. 215 verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle()); 216 EXPECT_EQ("extern \"C\" int f() {\n" 217 " int i = 42;\n" 218 " return i;\n" 219 "}", 220 format("extern \"C\" int f() {\n" 221 "\n" 222 " int i = 42;\n" 223 " return i;\n" 224 "}", 225 getGoogleStyle())); 226 227 // Remove empty lines at the beginning and end of blocks. 228 EXPECT_EQ("void f() {\n" 229 "\n" 230 " if (a) {\n" 231 "\n" 232 " f();\n" 233 " }\n" 234 "}", 235 format("void f() {\n" 236 "\n" 237 " if (a) {\n" 238 "\n" 239 " f();\n" 240 "\n" 241 " }\n" 242 "\n" 243 "}", 244 getLLVMStyle())); 245 EXPECT_EQ("void f() {\n" 246 " if (a) {\n" 247 " f();\n" 248 " }\n" 249 "}", 250 format("void f() {\n" 251 "\n" 252 " if (a) {\n" 253 "\n" 254 " f();\n" 255 "\n" 256 " }\n" 257 "\n" 258 "}", 259 getGoogleStyle())); 260 261 // Don't remove empty lines in more complex control statements. 262 EXPECT_EQ("void f() {\n" 263 " if (a) {\n" 264 " f();\n" 265 "\n" 266 " } else if (b) {\n" 267 " f();\n" 268 " }\n" 269 "}", 270 format("void f() {\n" 271 " if (a) {\n" 272 " f();\n" 273 "\n" 274 " } else if (b) {\n" 275 " f();\n" 276 "\n" 277 " }\n" 278 "\n" 279 "}")); 280 281 // FIXME: This is slightly inconsistent. 282 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 283 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 284 EXPECT_EQ("namespace {\n" 285 "int i;\n" 286 "}", 287 format("namespace {\n" 288 "int i;\n" 289 "\n" 290 "}", LLVMWithNoNamespaceFix)); 291 EXPECT_EQ("namespace {\n" 292 "int i;\n" 293 "}", 294 format("namespace {\n" 295 "int i;\n" 296 "\n" 297 "}")); 298 EXPECT_EQ("namespace {\n" 299 "int i;\n" 300 "\n" 301 "} // namespace", 302 format("namespace {\n" 303 "int i;\n" 304 "\n" 305 "} // namespace")); 306 307 FormatStyle Style = getLLVMStyle(); 308 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 309 Style.MaxEmptyLinesToKeep = 2; 310 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 311 Style.BraceWrapping.AfterClass = true; 312 Style.BraceWrapping.AfterFunction = true; 313 Style.KeepEmptyLinesAtTheStartOfBlocks = false; 314 315 EXPECT_EQ("class Foo\n" 316 "{\n" 317 " Foo() {}\n" 318 "\n" 319 " void funk() {}\n" 320 "};", 321 format("class Foo\n" 322 "{\n" 323 " Foo()\n" 324 " {\n" 325 " }\n" 326 "\n" 327 " void funk() {}\n" 328 "};", 329 Style)); 330 } 331 332 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) { 333 verifyFormat("x = (a) and (b);"); 334 verifyFormat("x = (a) or (b);"); 335 verifyFormat("x = (a) bitand (b);"); 336 verifyFormat("x = (a) bitor (b);"); 337 verifyFormat("x = (a) not_eq (b);"); 338 verifyFormat("x = (a) and_eq (b);"); 339 verifyFormat("x = (a) or_eq (b);"); 340 verifyFormat("x = (a) xor (b);"); 341 } 342 343 TEST_F(FormatTest, RecognizesUnaryOperatorKeywords) { 344 verifyFormat("x = compl(a);"); 345 verifyFormat("x = not(a);"); 346 verifyFormat("x = bitand(a);"); 347 // Unary operator must not be merged with the next identifier 348 verifyFormat("x = compl a;"); 349 verifyFormat("x = not a;"); 350 verifyFormat("x = bitand a;"); 351 } 352 353 //===----------------------------------------------------------------------===// 354 // Tests for control statements. 355 //===----------------------------------------------------------------------===// 356 357 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) { 358 verifyFormat("if (true)\n f();\ng();"); 359 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); 360 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); 361 verifyFormat("if constexpr (true)\n" 362 " f();\ng();"); 363 verifyFormat("if constexpr (a)\n" 364 " if constexpr (b)\n" 365 " if constexpr (c)\n" 366 " g();\n" 367 "h();"); 368 verifyFormat("if constexpr (a)\n" 369 " if constexpr (b) {\n" 370 " f();\n" 371 " }\n" 372 "g();"); 373 374 FormatStyle AllowsMergedIf = getLLVMStyle(); 375 AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left; 376 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 377 verifyFormat("if (a)\n" 378 " // comment\n" 379 " f();", 380 AllowsMergedIf); 381 verifyFormat("{\n" 382 " if (a)\n" 383 " label:\n" 384 " f();\n" 385 "}", 386 AllowsMergedIf); 387 verifyFormat("#define A \\\n" 388 " if (a) \\\n" 389 " label: \\\n" 390 " f()", 391 AllowsMergedIf); 392 verifyFormat("if (a)\n" 393 " ;", 394 AllowsMergedIf); 395 verifyFormat("if (a)\n" 396 " if (b) return;", 397 AllowsMergedIf); 398 399 verifyFormat("if (a) // Can't merge this\n" 400 " f();\n", 401 AllowsMergedIf); 402 verifyFormat("if (a) /* still don't merge */\n" 403 " f();", 404 AllowsMergedIf); 405 verifyFormat("if (a) { // Never merge this\n" 406 " f();\n" 407 "}", 408 AllowsMergedIf); 409 verifyFormat("if (a) { /* Never merge this */\n" 410 " f();\n" 411 "}", 412 AllowsMergedIf); 413 414 AllowsMergedIf.ColumnLimit = 14; 415 verifyFormat("if (a) return;", AllowsMergedIf); 416 verifyFormat("if (aaaaaaaaa)\n" 417 " return;", 418 AllowsMergedIf); 419 420 AllowsMergedIf.ColumnLimit = 13; 421 verifyFormat("if (a)\n return;", AllowsMergedIf); 422 } 423 424 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) { 425 FormatStyle AllowsMergedLoops = getLLVMStyle(); 426 AllowsMergedLoops.AllowShortLoopsOnASingleLine = true; 427 verifyFormat("while (true) continue;", AllowsMergedLoops); 428 verifyFormat("for (;;) continue;", AllowsMergedLoops); 429 verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops); 430 verifyFormat("while (true)\n" 431 " ;", 432 AllowsMergedLoops); 433 verifyFormat("for (;;)\n" 434 " ;", 435 AllowsMergedLoops); 436 verifyFormat("for (;;)\n" 437 " for (;;) continue;", 438 AllowsMergedLoops); 439 verifyFormat("for (;;) // Can't merge this\n" 440 " continue;", 441 AllowsMergedLoops); 442 verifyFormat("for (;;) /* still don't merge */\n" 443 " continue;", 444 AllowsMergedLoops); 445 } 446 447 TEST_F(FormatTest, FormatShortBracedStatements) { 448 FormatStyle AllowSimpleBracedStatements = getLLVMStyle(); 449 AllowSimpleBracedStatements.ColumnLimit = 40; 450 AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true; 451 452 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true; 453 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 454 455 AllowSimpleBracedStatements.BreakBeforeBraces = FormatStyle::BS_Custom; 456 AllowSimpleBracedStatements.BraceWrapping.AfterFunction = true; 457 AllowSimpleBracedStatements.BraceWrapping.SplitEmptyRecord = false; 458 459 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 460 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements); 461 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 462 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 463 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 464 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements); 465 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 466 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 467 verifyFormat("if (true) {\n" 468 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 469 "}", 470 AllowSimpleBracedStatements); 471 verifyFormat("if (true) { //\n" 472 " f();\n" 473 "}", 474 AllowSimpleBracedStatements); 475 verifyFormat("if (true) {\n" 476 " f();\n" 477 " f();\n" 478 "}", 479 AllowSimpleBracedStatements); 480 verifyFormat("if (true) {\n" 481 " f();\n" 482 "} else {\n" 483 " f();\n" 484 "}", 485 AllowSimpleBracedStatements); 486 487 verifyFormat("struct A2 {\n" 488 " int X;\n" 489 "};", 490 AllowSimpleBracedStatements); 491 verifyFormat("typedef struct A2 {\n" 492 " int X;\n" 493 "} A2_t;", 494 AllowSimpleBracedStatements); 495 verifyFormat("template <int> struct A2 {\n" 496 " struct B {};\n" 497 "};", 498 AllowSimpleBracedStatements); 499 500 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false; 501 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 502 verifyFormat("if (true) {\n" 503 " f();\n" 504 "}", 505 AllowSimpleBracedStatements); 506 verifyFormat("if (true) {\n" 507 " f();\n" 508 "} else {\n" 509 " f();\n" 510 "}", 511 AllowSimpleBracedStatements); 512 513 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 514 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 515 verifyFormat("while (true) {\n" 516 " f();\n" 517 "}", 518 AllowSimpleBracedStatements); 519 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 520 verifyFormat("for (;;) {\n" 521 " f();\n" 522 "}", 523 AllowSimpleBracedStatements); 524 525 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true; 526 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 527 AllowSimpleBracedStatements.BraceWrapping.AfterControlStatement = true; 528 529 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 530 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements); 531 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 532 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 533 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 534 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements); 535 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 536 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 537 verifyFormat("if (true)\n" 538 "{\n" 539 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 540 "}", 541 AllowSimpleBracedStatements); 542 verifyFormat("if (true)\n" 543 "{ //\n" 544 " f();\n" 545 "}", 546 AllowSimpleBracedStatements); 547 verifyFormat("if (true)\n" 548 "{\n" 549 " f();\n" 550 " f();\n" 551 "}", 552 AllowSimpleBracedStatements); 553 verifyFormat("if (true)\n" 554 "{\n" 555 " f();\n" 556 "} else\n" 557 "{\n" 558 " f();\n" 559 "}", 560 AllowSimpleBracedStatements); 561 562 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false; 563 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 564 verifyFormat("if (true)\n" 565 "{\n" 566 " f();\n" 567 "}", 568 AllowSimpleBracedStatements); 569 verifyFormat("if (true)\n" 570 "{\n" 571 " f();\n" 572 "} else\n" 573 "{\n" 574 " f();\n" 575 "}", 576 AllowSimpleBracedStatements); 577 578 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 579 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 580 verifyFormat("while (true)\n" 581 "{\n" 582 " f();\n" 583 "}", 584 AllowSimpleBracedStatements); 585 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 586 verifyFormat("for (;;)\n" 587 "{\n" 588 " f();\n" 589 "}", 590 AllowSimpleBracedStatements); 591 } 592 593 TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) { 594 FormatStyle Style = getLLVMStyleWithColumns(60); 595 Style.AllowShortBlocksOnASingleLine = true; 596 Style.AllowShortIfStatementsOnASingleLine = true; 597 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 598 EXPECT_EQ("#define A \\\n" 599 " if (HANDLEwernufrnuLwrmviferuvnierv) \\\n" 600 " { RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; }\n" 601 "X;", 602 format("#define A \\\n" 603 " if (HANDLEwernufrnuLwrmviferuvnierv) { \\\n" 604 " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n" 605 " }\n" 606 "X;", 607 Style)); 608 } 609 610 TEST_F(FormatTest, ParseIfElse) { 611 verifyFormat("if (true)\n" 612 " if (true)\n" 613 " if (true)\n" 614 " f();\n" 615 " else\n" 616 " g();\n" 617 " else\n" 618 " h();\n" 619 "else\n" 620 " i();"); 621 verifyFormat("if (true)\n" 622 " if (true)\n" 623 " if (true) {\n" 624 " if (true)\n" 625 " f();\n" 626 " } else {\n" 627 " g();\n" 628 " }\n" 629 " else\n" 630 " h();\n" 631 "else {\n" 632 " i();\n" 633 "}"); 634 verifyFormat("if (true)\n" 635 " if constexpr (true)\n" 636 " if (true) {\n" 637 " if constexpr (true)\n" 638 " f();\n" 639 " } else {\n" 640 " g();\n" 641 " }\n" 642 " else\n" 643 " h();\n" 644 "else {\n" 645 " i();\n" 646 "}"); 647 verifyFormat("void f() {\n" 648 " if (a) {\n" 649 " } else {\n" 650 " }\n" 651 "}"); 652 } 653 654 TEST_F(FormatTest, ElseIf) { 655 verifyFormat("if (a) {\n} else if (b) {\n}"); 656 verifyFormat("if (a)\n" 657 " f();\n" 658 "else if (b)\n" 659 " g();\n" 660 "else\n" 661 " h();"); 662 verifyFormat("if constexpr (a)\n" 663 " f();\n" 664 "else if constexpr (b)\n" 665 " g();\n" 666 "else\n" 667 " h();"); 668 verifyFormat("if (a) {\n" 669 " f();\n" 670 "}\n" 671 "// or else ..\n" 672 "else {\n" 673 " g()\n" 674 "}"); 675 676 verifyFormat("if (a) {\n" 677 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 678 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 679 "}"); 680 verifyFormat("if (a) {\n" 681 "} else if (\n" 682 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 683 "}", 684 getLLVMStyleWithColumns(62)); 685 verifyFormat("if (a) {\n" 686 "} else if constexpr (\n" 687 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 688 "}", 689 getLLVMStyleWithColumns(62)); 690 } 691 692 TEST_F(FormatTest, FormatsForLoop) { 693 verifyFormat( 694 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" 695 " ++VeryVeryLongLoopVariable)\n" 696 " ;"); 697 verifyFormat("for (;;)\n" 698 " f();"); 699 verifyFormat("for (;;) {\n}"); 700 verifyFormat("for (;;) {\n" 701 " f();\n" 702 "}"); 703 verifyFormat("for (int i = 0; (i < 10); ++i) {\n}"); 704 705 verifyFormat( 706 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 707 " E = UnwrappedLines.end();\n" 708 " I != E; ++I) {\n}"); 709 710 verifyFormat( 711 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" 712 " ++IIIII) {\n}"); 713 verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n" 714 " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n" 715 " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}"); 716 verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n" 717 " I = FD->getDeclsInPrototypeScope().begin(),\n" 718 " E = FD->getDeclsInPrototypeScope().end();\n" 719 " I != E; ++I) {\n}"); 720 verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n" 721 " I = Container.begin(),\n" 722 " E = Container.end();\n" 723 " I != E; ++I) {\n}", 724 getLLVMStyleWithColumns(76)); 725 726 verifyFormat( 727 "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 728 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n" 729 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 730 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 731 " ++aaaaaaaaaaa) {\n}"); 732 verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 733 " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n" 734 " ++i) {\n}"); 735 verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n" 736 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 737 "}"); 738 verifyFormat("for (some_namespace::SomeIterator iter( // force break\n" 739 " aaaaaaaaaa);\n" 740 " iter; ++iter) {\n" 741 "}"); 742 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 743 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 744 " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n" 745 " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {"); 746 747 // These should not be formatted as Objective-C for-in loops. 748 verifyFormat("for (Foo *x = 0; x != in; x++) {\n}"); 749 verifyFormat("Foo *x;\nfor (x = 0; x != in; x++) {\n}"); 750 verifyFormat("Foo *x;\nfor (x in y) {\n}"); 751 verifyFormat("for (const Foo<Bar> &baz = in.value(); !baz.at_end(); ++baz) {\n}"); 752 753 FormatStyle NoBinPacking = getLLVMStyle(); 754 NoBinPacking.BinPackParameters = false; 755 verifyFormat("for (int aaaaaaaaaaa = 1;\n" 756 " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n" 757 " aaaaaaaaaaaaaaaa,\n" 758 " aaaaaaaaaaaaaaaa,\n" 759 " aaaaaaaaaaaaaaaa);\n" 760 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 761 "}", 762 NoBinPacking); 763 verifyFormat( 764 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 765 " E = UnwrappedLines.end();\n" 766 " I != E;\n" 767 " ++I) {\n}", 768 NoBinPacking); 769 770 FormatStyle AlignLeft = getLLVMStyle(); 771 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 772 verifyFormat("for (A* a = start; a < end; ++a, ++value) {\n}", AlignLeft); 773 } 774 775 TEST_F(FormatTest, RangeBasedForLoops) { 776 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 777 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 778 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n" 779 " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}"); 780 verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n" 781 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 782 verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n" 783 " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}"); 784 } 785 786 TEST_F(FormatTest, ForEachLoops) { 787 verifyFormat("void f() {\n" 788 " foreach (Item *item, itemlist) {}\n" 789 " Q_FOREACH (Item *item, itemlist) {}\n" 790 " BOOST_FOREACH (Item *item, itemlist) {}\n" 791 " UNKNOWN_FORACH(Item * item, itemlist) {}\n" 792 "}"); 793 794 // As function-like macros. 795 verifyFormat("#define foreach(x, y)\n" 796 "#define Q_FOREACH(x, y)\n" 797 "#define BOOST_FOREACH(x, y)\n" 798 "#define UNKNOWN_FOREACH(x, y)\n"); 799 800 // Not as function-like macros. 801 verifyFormat("#define foreach (x, y)\n" 802 "#define Q_FOREACH (x, y)\n" 803 "#define BOOST_FOREACH (x, y)\n" 804 "#define UNKNOWN_FOREACH (x, y)\n"); 805 } 806 807 TEST_F(FormatTest, FormatsWhileLoop) { 808 verifyFormat("while (true) {\n}"); 809 verifyFormat("while (true)\n" 810 " f();"); 811 verifyFormat("while () {\n}"); 812 verifyFormat("while () {\n" 813 " f();\n" 814 "}"); 815 } 816 817 TEST_F(FormatTest, FormatsDoWhile) { 818 verifyFormat("do {\n" 819 " do_something();\n" 820 "} while (something());"); 821 verifyFormat("do\n" 822 " do_something();\n" 823 "while (something());"); 824 } 825 826 TEST_F(FormatTest, FormatsSwitchStatement) { 827 verifyFormat("switch (x) {\n" 828 "case 1:\n" 829 " f();\n" 830 " break;\n" 831 "case kFoo:\n" 832 "case ns::kBar:\n" 833 "case kBaz:\n" 834 " break;\n" 835 "default:\n" 836 " g();\n" 837 " break;\n" 838 "}"); 839 verifyFormat("switch (x) {\n" 840 "case 1: {\n" 841 " f();\n" 842 " break;\n" 843 "}\n" 844 "case 2: {\n" 845 " break;\n" 846 "}\n" 847 "}"); 848 verifyFormat("switch (x) {\n" 849 "case 1: {\n" 850 " f();\n" 851 " {\n" 852 " g();\n" 853 " h();\n" 854 " }\n" 855 " break;\n" 856 "}\n" 857 "}"); 858 verifyFormat("switch (x) {\n" 859 "case 1: {\n" 860 " f();\n" 861 " if (foo) {\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 " g();\n" 872 "} break;\n" 873 "}"); 874 verifyFormat("switch (test)\n" 875 " ;"); 876 verifyFormat("switch (x) {\n" 877 "default: {\n" 878 " // Do nothing.\n" 879 "}\n" 880 "}"); 881 verifyFormat("switch (x) {\n" 882 "// comment\n" 883 "// if 1, do f()\n" 884 "case 1:\n" 885 " f();\n" 886 "}"); 887 verifyFormat("switch (x) {\n" 888 "case 1:\n" 889 " // Do amazing stuff\n" 890 " {\n" 891 " f();\n" 892 " g();\n" 893 " }\n" 894 " break;\n" 895 "}"); 896 verifyFormat("#define A \\\n" 897 " switch (x) { \\\n" 898 " case a: \\\n" 899 " foo = b; \\\n" 900 " }", 901 getLLVMStyleWithColumns(20)); 902 verifyFormat("#define OPERATION_CASE(name) \\\n" 903 " case OP_name: \\\n" 904 " return operations::Operation##name\n", 905 getLLVMStyleWithColumns(40)); 906 verifyFormat("switch (x) {\n" 907 "case 1:;\n" 908 "default:;\n" 909 " int i;\n" 910 "}"); 911 912 verifyGoogleFormat("switch (x) {\n" 913 " case 1:\n" 914 " f();\n" 915 " break;\n" 916 " case kFoo:\n" 917 " case ns::kBar:\n" 918 " case kBaz:\n" 919 " break;\n" 920 " default:\n" 921 " g();\n" 922 " break;\n" 923 "}"); 924 verifyGoogleFormat("switch (x) {\n" 925 " case 1: {\n" 926 " f();\n" 927 " break;\n" 928 " }\n" 929 "}"); 930 verifyGoogleFormat("switch (test)\n" 931 " ;"); 932 933 verifyGoogleFormat("#define OPERATION_CASE(name) \\\n" 934 " case OP_name: \\\n" 935 " return operations::Operation##name\n"); 936 verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n" 937 " // Get the correction operation class.\n" 938 " switch (OpCode) {\n" 939 " CASE(Add);\n" 940 " CASE(Subtract);\n" 941 " default:\n" 942 " return operations::Unknown;\n" 943 " }\n" 944 "#undef OPERATION_CASE\n" 945 "}"); 946 verifyFormat("DEBUG({\n" 947 " switch (x) {\n" 948 " case A:\n" 949 " f();\n" 950 " break;\n" 951 " // fallthrough\n" 952 " case B:\n" 953 " g();\n" 954 " break;\n" 955 " }\n" 956 "});"); 957 EXPECT_EQ("DEBUG({\n" 958 " switch (x) {\n" 959 " case A:\n" 960 " f();\n" 961 " break;\n" 962 " // On B:\n" 963 " case B:\n" 964 " g();\n" 965 " break;\n" 966 " }\n" 967 "});", 968 format("DEBUG({\n" 969 " switch (x) {\n" 970 " case A:\n" 971 " f();\n" 972 " break;\n" 973 " // On B:\n" 974 " case B:\n" 975 " g();\n" 976 " break;\n" 977 " }\n" 978 "});", 979 getLLVMStyle())); 980 verifyFormat("switch (a) {\n" 981 "case (b):\n" 982 " return;\n" 983 "}"); 984 985 verifyFormat("switch (a) {\n" 986 "case some_namespace::\n" 987 " some_constant:\n" 988 " return;\n" 989 "}", 990 getLLVMStyleWithColumns(34)); 991 } 992 993 TEST_F(FormatTest, CaseRanges) { 994 verifyFormat("switch (x) {\n" 995 "case 'A' ... 'Z':\n" 996 "case 1 ... 5:\n" 997 "case a ... b:\n" 998 " break;\n" 999 "}"); 1000 } 1001 1002 TEST_F(FormatTest, ShortCaseLabels) { 1003 FormatStyle Style = getLLVMStyle(); 1004 Style.AllowShortCaseLabelsOnASingleLine = true; 1005 verifyFormat("switch (a) {\n" 1006 "case 1: x = 1; break;\n" 1007 "case 2: return;\n" 1008 "case 3:\n" 1009 "case 4:\n" 1010 "case 5: return;\n" 1011 "case 6: // comment\n" 1012 " return;\n" 1013 "case 7:\n" 1014 " // comment\n" 1015 " return;\n" 1016 "case 8:\n" 1017 " x = 8; // comment\n" 1018 " break;\n" 1019 "default: y = 1; break;\n" 1020 "}", 1021 Style); 1022 verifyFormat("switch (a) {\n" 1023 "case 0: return; // comment\n" 1024 "case 1: break; // comment\n" 1025 "case 2: return;\n" 1026 "// comment\n" 1027 "case 3: return;\n" 1028 "// comment 1\n" 1029 "// comment 2\n" 1030 "// comment 3\n" 1031 "case 4: break; /* comment */\n" 1032 "case 5:\n" 1033 " // comment\n" 1034 " break;\n" 1035 "case 6: /* comment */ x = 1; break;\n" 1036 "case 7: x = /* comment */ 1; break;\n" 1037 "case 8:\n" 1038 " x = 1; /* comment */\n" 1039 " break;\n" 1040 "case 9:\n" 1041 " break; // comment line 1\n" 1042 " // comment line 2\n" 1043 "}", 1044 Style); 1045 EXPECT_EQ("switch (a) {\n" 1046 "case 1:\n" 1047 " x = 8;\n" 1048 " // fall through\n" 1049 "case 2: x = 8;\n" 1050 "// comment\n" 1051 "case 3:\n" 1052 " return; /* comment line 1\n" 1053 " * comment line 2 */\n" 1054 "case 4: i = 8;\n" 1055 "// something else\n" 1056 "#if FOO\n" 1057 "case 5: break;\n" 1058 "#endif\n" 1059 "}", 1060 format("switch (a) {\n" 1061 "case 1: x = 8;\n" 1062 " // fall through\n" 1063 "case 2:\n" 1064 " x = 8;\n" 1065 "// comment\n" 1066 "case 3:\n" 1067 " return; /* comment line 1\n" 1068 " * comment line 2 */\n" 1069 "case 4:\n" 1070 " i = 8;\n" 1071 "// something else\n" 1072 "#if FOO\n" 1073 "case 5: break;\n" 1074 "#endif\n" 1075 "}", 1076 Style)); 1077 EXPECT_EQ("switch (a) {\n" "case 0:\n" 1078 " return; // long long long long long long long long long long long long comment\n" 1079 " // line\n" "}", 1080 format("switch (a) {\n" 1081 "case 0: return; // long long long long long long long long long long long long comment line\n" 1082 "}", 1083 Style)); 1084 EXPECT_EQ("switch (a) {\n" 1085 "case 0:\n" 1086 " return; /* long long long long long long long long long long long long comment\n" 1087 " line */\n" 1088 "}", 1089 format("switch (a) {\n" 1090 "case 0: return; /* long long long long long long long long long long long long comment line */\n" 1091 "}", 1092 Style)); 1093 verifyFormat("switch (a) {\n" 1094 "#if FOO\n" 1095 "case 0: return 0;\n" 1096 "#endif\n" 1097 "}", 1098 Style); 1099 verifyFormat("switch (a) {\n" 1100 "case 1: {\n" 1101 "}\n" 1102 "case 2: {\n" 1103 " return;\n" 1104 "}\n" 1105 "case 3: {\n" 1106 " x = 1;\n" 1107 " return;\n" 1108 "}\n" 1109 "case 4:\n" 1110 " if (x)\n" 1111 " return;\n" 1112 "}", 1113 Style); 1114 Style.ColumnLimit = 21; 1115 verifyFormat("switch (a) {\n" 1116 "case 1: x = 1; break;\n" 1117 "case 2: return;\n" 1118 "case 3:\n" 1119 "case 4:\n" 1120 "case 5: return;\n" 1121 "default:\n" 1122 " y = 1;\n" 1123 " break;\n" 1124 "}", 1125 Style); 1126 } 1127 1128 TEST_F(FormatTest, FormatsLabels) { 1129 verifyFormat("void f() {\n" 1130 " some_code();\n" 1131 "test_label:\n" 1132 " some_other_code();\n" 1133 " {\n" 1134 " some_more_code();\n" 1135 " another_label:\n" 1136 " some_more_code();\n" 1137 " }\n" 1138 "}"); 1139 verifyFormat("{\n" 1140 " some_code();\n" 1141 "test_label:\n" 1142 " some_other_code();\n" 1143 "}"); 1144 verifyFormat("{\n" 1145 " some_code();\n" 1146 "test_label:;\n" 1147 " int i = 0;\n" 1148 "}"); 1149 } 1150 1151 //===----------------------------------------------------------------------===// 1152 // Tests for classes, namespaces, etc. 1153 //===----------------------------------------------------------------------===// 1154 1155 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { 1156 verifyFormat("class A {};"); 1157 } 1158 1159 TEST_F(FormatTest, UnderstandsAccessSpecifiers) { 1160 verifyFormat("class A {\n" 1161 "public:\n" 1162 "public: // comment\n" 1163 "protected:\n" 1164 "private:\n" 1165 " void f() {}\n" 1166 "};"); 1167 verifyGoogleFormat("class A {\n" 1168 " public:\n" 1169 " protected:\n" 1170 " private:\n" 1171 " void f() {}\n" 1172 "};"); 1173 verifyFormat("class A {\n" 1174 "public slots:\n" 1175 " void f1() {}\n" 1176 "public Q_SLOTS:\n" 1177 " void f2() {}\n" 1178 "protected slots:\n" 1179 " void f3() {}\n" 1180 "protected Q_SLOTS:\n" 1181 " void f4() {}\n" 1182 "private slots:\n" 1183 " void f5() {}\n" 1184 "private Q_SLOTS:\n" 1185 " void f6() {}\n" 1186 "signals:\n" 1187 " void g1();\n" 1188 "Q_SIGNALS:\n" 1189 " void g2();\n" 1190 "};"); 1191 1192 // Don't interpret 'signals' the wrong way. 1193 verifyFormat("signals.set();"); 1194 verifyFormat("for (Signals signals : f()) {\n}"); 1195 verifyFormat("{\n" 1196 " signals.set(); // This needs indentation.\n" 1197 "}"); 1198 verifyFormat("void f() {\n" 1199 "label:\n" 1200 " signals.baz();\n" 1201 "}"); 1202 } 1203 1204 TEST_F(FormatTest, SeparatesLogicalBlocks) { 1205 EXPECT_EQ("class A {\n" 1206 "public:\n" 1207 " void f();\n" 1208 "\n" 1209 "private:\n" 1210 " void g() {}\n" 1211 " // test\n" 1212 "protected:\n" 1213 " int h;\n" 1214 "};", 1215 format("class A {\n" 1216 "public:\n" 1217 "void f();\n" 1218 "private:\n" 1219 "void g() {}\n" 1220 "// test\n" 1221 "protected:\n" 1222 "int h;\n" 1223 "};")); 1224 EXPECT_EQ("class A {\n" 1225 "protected:\n" 1226 "public:\n" 1227 " void f();\n" 1228 "};", 1229 format("class A {\n" 1230 "protected:\n" 1231 "\n" 1232 "public:\n" 1233 "\n" 1234 " void f();\n" 1235 "};")); 1236 1237 // Even ensure proper spacing inside macros. 1238 EXPECT_EQ("#define B \\\n" 1239 " class A { \\\n" 1240 " protected: \\\n" 1241 " public: \\\n" 1242 " void f(); \\\n" 1243 " };", 1244 format("#define B \\\n" 1245 " class A { \\\n" 1246 " protected: \\\n" 1247 " \\\n" 1248 " public: \\\n" 1249 " \\\n" 1250 " void f(); \\\n" 1251 " };", 1252 getGoogleStyle())); 1253 // But don't remove empty lines after macros ending in access specifiers. 1254 EXPECT_EQ("#define A private:\n" 1255 "\n" 1256 "int i;", 1257 format("#define A private:\n" 1258 "\n" 1259 "int i;")); 1260 } 1261 1262 TEST_F(FormatTest, FormatsClasses) { 1263 verifyFormat("class A : public B {};"); 1264 verifyFormat("class A : public ::B {};"); 1265 1266 verifyFormat( 1267 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 1268 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 1269 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 1270 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 1271 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 1272 verifyFormat( 1273 "class A : public B, public C, public D, public E, public F {};"); 1274 verifyFormat("class AAAAAAAAAAAA : public B,\n" 1275 " public C,\n" 1276 " public D,\n" 1277 " public E,\n" 1278 " public F,\n" 1279 " public G {};"); 1280 1281 verifyFormat("class\n" 1282 " ReallyReallyLongClassName {\n" 1283 " int i;\n" 1284 "};", 1285 getLLVMStyleWithColumns(32)); 1286 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 1287 " aaaaaaaaaaaaaaaa> {};"); 1288 verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n" 1289 " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n" 1290 " aaaaaaaaaaaaaaaaaaaaaa> {};"); 1291 verifyFormat("template <class R, class C>\n" 1292 "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n" 1293 " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};"); 1294 verifyFormat("class ::A::B {};"); 1295 } 1296 1297 TEST_F(FormatTest, BreakBeforeInheritanceComma) { 1298 FormatStyle StyleWithInheritanceBreak = getLLVMStyle(); 1299 StyleWithInheritanceBreak.BreakBeforeInheritanceComma = true; 1300 1301 verifyFormat("class MyClass : public X {};", StyleWithInheritanceBreak); 1302 verifyFormat("class MyClass\n" 1303 " : public X\n" 1304 " , public Y {};", 1305 StyleWithInheritanceBreak); 1306 } 1307 1308 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) { 1309 verifyFormat("class A {\n} a, b;"); 1310 verifyFormat("struct A {\n} a, b;"); 1311 verifyFormat("union A {\n} a;"); 1312 } 1313 1314 TEST_F(FormatTest, FormatsEnum) { 1315 verifyFormat("enum {\n" 1316 " Zero,\n" 1317 " One = 1,\n" 1318 " Two = One + 1,\n" 1319 " Three = (One + Two),\n" 1320 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1321 " Five = (One, Two, Three, Four, 5)\n" 1322 "};"); 1323 verifyGoogleFormat("enum {\n" 1324 " Zero,\n" 1325 " One = 1,\n" 1326 " Two = One + 1,\n" 1327 " Three = (One + Two),\n" 1328 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1329 " Five = (One, Two, Three, Four, 5)\n" 1330 "};"); 1331 verifyFormat("enum Enum {};"); 1332 verifyFormat("enum {};"); 1333 verifyFormat("enum X E {} d;"); 1334 verifyFormat("enum __attribute__((...)) E {} d;"); 1335 verifyFormat("enum __declspec__((...)) E {} d;"); 1336 verifyFormat("enum {\n" 1337 " Bar = Foo<int, int>::value\n" 1338 "};", 1339 getLLVMStyleWithColumns(30)); 1340 1341 verifyFormat("enum ShortEnum { A, B, C };"); 1342 verifyGoogleFormat("enum ShortEnum { A, B, C };"); 1343 1344 EXPECT_EQ("enum KeepEmptyLines {\n" 1345 " ONE,\n" 1346 "\n" 1347 " TWO,\n" 1348 "\n" 1349 " THREE\n" 1350 "}", 1351 format("enum KeepEmptyLines {\n" 1352 " ONE,\n" 1353 "\n" 1354 " TWO,\n" 1355 "\n" 1356 "\n" 1357 " THREE\n" 1358 "}")); 1359 verifyFormat("enum E { // comment\n" 1360 " ONE,\n" 1361 " TWO\n" 1362 "};\n" 1363 "int i;"); 1364 // Not enums. 1365 verifyFormat("enum X f() {\n" 1366 " a();\n" 1367 " return 42;\n" 1368 "}"); 1369 verifyFormat("enum X Type::f() {\n" 1370 " a();\n" 1371 " return 42;\n" 1372 "}"); 1373 verifyFormat("enum ::X f() {\n" 1374 " a();\n" 1375 " return 42;\n" 1376 "}"); 1377 verifyFormat("enum ns::X f() {\n" 1378 " a();\n" 1379 " return 42;\n" 1380 "}"); 1381 } 1382 1383 TEST_F(FormatTest, FormatsEnumsWithErrors) { 1384 verifyFormat("enum Type {\n" 1385 " One = 0; // These semicolons should be commas.\n" 1386 " Two = 1;\n" 1387 "};"); 1388 verifyFormat("namespace n {\n" 1389 "enum Type {\n" 1390 " One,\n" 1391 " Two, // missing };\n" 1392 " int i;\n" 1393 "}\n" 1394 "void g() {}"); 1395 } 1396 1397 TEST_F(FormatTest, FormatsEnumStruct) { 1398 verifyFormat("enum struct {\n" 1399 " Zero,\n" 1400 " One = 1,\n" 1401 " Two = One + 1,\n" 1402 " Three = (One + Two),\n" 1403 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1404 " Five = (One, Two, Three, Four, 5)\n" 1405 "};"); 1406 verifyFormat("enum struct Enum {};"); 1407 verifyFormat("enum struct {};"); 1408 verifyFormat("enum struct X E {} d;"); 1409 verifyFormat("enum struct __attribute__((...)) E {} d;"); 1410 verifyFormat("enum struct __declspec__((...)) E {} d;"); 1411 verifyFormat("enum struct X f() {\n a();\n return 42;\n}"); 1412 } 1413 1414 TEST_F(FormatTest, FormatsEnumClass) { 1415 verifyFormat("enum class {\n" 1416 " Zero,\n" 1417 " One = 1,\n" 1418 " Two = One + 1,\n" 1419 " Three = (One + Two),\n" 1420 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1421 " Five = (One, Two, Three, Four, 5)\n" 1422 "};"); 1423 verifyFormat("enum class Enum {};"); 1424 verifyFormat("enum class {};"); 1425 verifyFormat("enum class X E {} d;"); 1426 verifyFormat("enum class __attribute__((...)) E {} d;"); 1427 verifyFormat("enum class __declspec__((...)) E {} d;"); 1428 verifyFormat("enum class X f() {\n a();\n return 42;\n}"); 1429 } 1430 1431 TEST_F(FormatTest, FormatsEnumTypes) { 1432 verifyFormat("enum X : int {\n" 1433 " A, // Force multiple lines.\n" 1434 " B\n" 1435 "};"); 1436 verifyFormat("enum X : int { A, B };"); 1437 verifyFormat("enum X : std::uint32_t { A, B };"); 1438 } 1439 1440 TEST_F(FormatTest, FormatsTypedefEnum) { 1441 FormatStyle Style = getLLVMStyle(); 1442 Style.ColumnLimit = 40; 1443 verifyFormat("typedef enum {} EmptyEnum;"); 1444 verifyFormat("typedef enum { A, B, C } ShortEnum;"); 1445 verifyFormat("typedef enum {\n" 1446 " ZERO = 0,\n" 1447 " ONE = 1,\n" 1448 " TWO = 2,\n" 1449 " THREE = 3\n" 1450 "} LongEnum;", 1451 Style); 1452 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 1453 Style.BraceWrapping.AfterEnum = true; 1454 verifyFormat("typedef enum {} EmptyEnum;"); 1455 verifyFormat("typedef enum { A, B, C } ShortEnum;"); 1456 verifyFormat("typedef enum\n" 1457 "{\n" 1458 " ZERO = 0,\n" 1459 " ONE = 1,\n" 1460 " TWO = 2,\n" 1461 " THREE = 3\n" 1462 "} LongEnum;", 1463 Style); 1464 } 1465 1466 TEST_F(FormatTest, FormatsNSEnums) { 1467 verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }"); 1468 verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n" 1469 " // Information about someDecentlyLongValue.\n" 1470 " someDecentlyLongValue,\n" 1471 " // Information about anotherDecentlyLongValue.\n" 1472 " anotherDecentlyLongValue,\n" 1473 " // Information about aThirdDecentlyLongValue.\n" 1474 " aThirdDecentlyLongValue\n" 1475 "};"); 1476 verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n" 1477 " a = 1,\n" 1478 " b = 2,\n" 1479 " c = 3,\n" 1480 "};"); 1481 verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n" 1482 " a = 1,\n" 1483 " b = 2,\n" 1484 " c = 3,\n" 1485 "};"); 1486 verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n" 1487 " a = 1,\n" 1488 " b = 2,\n" 1489 " c = 3,\n" 1490 "};"); 1491 } 1492 1493 TEST_F(FormatTest, FormatsBitfields) { 1494 verifyFormat("struct Bitfields {\n" 1495 " unsigned sClass : 8;\n" 1496 " unsigned ValueKind : 2;\n" 1497 "};"); 1498 verifyFormat("struct A {\n" 1499 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n" 1500 " bbbbbbbbbbbbbbbbbbbbbbbbb;\n" 1501 "};"); 1502 verifyFormat("struct MyStruct {\n" 1503 " uchar data;\n" 1504 " uchar : 8;\n" 1505 " uchar : 8;\n" 1506 " uchar other;\n" 1507 "};"); 1508 } 1509 1510 TEST_F(FormatTest, FormatsNamespaces) { 1511 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 1512 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 1513 1514 verifyFormat("namespace some_namespace {\n" 1515 "class A {};\n" 1516 "void f() { f(); }\n" 1517 "}", 1518 LLVMWithNoNamespaceFix); 1519 verifyFormat("namespace {\n" 1520 "class A {};\n" 1521 "void f() { f(); }\n" 1522 "}", 1523 LLVMWithNoNamespaceFix); 1524 verifyFormat("inline namespace X {\n" 1525 "class A {};\n" 1526 "void f() { f(); }\n" 1527 "}", 1528 LLVMWithNoNamespaceFix); 1529 verifyFormat("using namespace some_namespace;\n" 1530 "class A {};\n" 1531 "void f() { f(); }", 1532 LLVMWithNoNamespaceFix); 1533 1534 // This code is more common than we thought; if we 1535 // layout this correctly the semicolon will go into 1536 // its own line, which is undesirable. 1537 verifyFormat("namespace {};", 1538 LLVMWithNoNamespaceFix); 1539 verifyFormat("namespace {\n" 1540 "class A {};\n" 1541 "};", 1542 LLVMWithNoNamespaceFix); 1543 1544 verifyFormat("namespace {\n" 1545 "int SomeVariable = 0; // comment\n" 1546 "} // namespace", 1547 LLVMWithNoNamespaceFix); 1548 EXPECT_EQ("#ifndef HEADER_GUARD\n" 1549 "#define HEADER_GUARD\n" 1550 "namespace my_namespace {\n" 1551 "int i;\n" 1552 "} // my_namespace\n" 1553 "#endif // HEADER_GUARD", 1554 format("#ifndef HEADER_GUARD\n" 1555 " #define HEADER_GUARD\n" 1556 " namespace my_namespace {\n" 1557 "int i;\n" 1558 "} // my_namespace\n" 1559 "#endif // HEADER_GUARD", 1560 LLVMWithNoNamespaceFix)); 1561 1562 EXPECT_EQ("namespace A::B {\n" 1563 "class C {};\n" 1564 "}", 1565 format("namespace A::B {\n" 1566 "class C {};\n" 1567 "}", 1568 LLVMWithNoNamespaceFix)); 1569 1570 FormatStyle Style = getLLVMStyle(); 1571 Style.NamespaceIndentation = FormatStyle::NI_All; 1572 EXPECT_EQ("namespace out {\n" 1573 " int i;\n" 1574 " namespace in {\n" 1575 " int i;\n" 1576 " } // namespace in\n" 1577 "} // namespace out", 1578 format("namespace out {\n" 1579 "int i;\n" 1580 "namespace in {\n" 1581 "int i;\n" 1582 "} // namespace in\n" 1583 "} // namespace out", 1584 Style)); 1585 1586 Style.NamespaceIndentation = FormatStyle::NI_Inner; 1587 EXPECT_EQ("namespace out {\n" 1588 "int i;\n" 1589 "namespace in {\n" 1590 " int i;\n" 1591 "} // namespace in\n" 1592 "} // namespace out", 1593 format("namespace out {\n" 1594 "int i;\n" 1595 "namespace in {\n" 1596 "int i;\n" 1597 "} // namespace in\n" 1598 "} // namespace out", 1599 Style)); 1600 } 1601 1602 TEST_F(FormatTest, FormatsCompactNamespaces) { 1603 FormatStyle Style = getLLVMStyle(); 1604 Style.CompactNamespaces = true; 1605 1606 verifyFormat("namespace A { namespace B {\n" 1607 "}} // namespace A::B", 1608 Style); 1609 1610 EXPECT_EQ("namespace out { namespace in {\n" 1611 "}} // namespace out::in", 1612 format("namespace out {\n" 1613 "namespace in {\n" 1614 "} // namespace in\n" 1615 "} // namespace out", 1616 Style)); 1617 1618 // Only namespaces which have both consecutive opening and end get compacted 1619 EXPECT_EQ("namespace out {\n" 1620 "namespace in1 {\n" 1621 "} // namespace in1\n" 1622 "namespace in2 {\n" 1623 "} // namespace in2\n" 1624 "} // namespace out", 1625 format("namespace out {\n" 1626 "namespace in1 {\n" 1627 "} // namespace in1\n" 1628 "namespace in2 {\n" 1629 "} // namespace in2\n" 1630 "} // namespace out", 1631 Style)); 1632 1633 EXPECT_EQ("namespace out {\n" 1634 "int i;\n" 1635 "namespace in {\n" 1636 "int j;\n" 1637 "} // namespace in\n" 1638 "int k;\n" 1639 "} // namespace out", 1640 format("namespace out { int i;\n" 1641 "namespace in { int j; } // namespace in\n" 1642 "int k; } // namespace out", 1643 Style)); 1644 1645 EXPECT_EQ("namespace A { namespace B { namespace C {\n" 1646 "}}} // namespace A::B::C\n", 1647 format("namespace A { namespace B {\n" 1648 "namespace C {\n" 1649 "}} // namespace B::C\n" 1650 "} // namespace A\n", 1651 Style)); 1652 1653 Style.ColumnLimit = 40; 1654 EXPECT_EQ("namespace aaaaaaaaaa {\n" 1655 "namespace bbbbbbbbbb {\n" 1656 "}} // namespace aaaaaaaaaa::bbbbbbbbbb", 1657 format("namespace aaaaaaaaaa {\n" 1658 "namespace bbbbbbbbbb {\n" 1659 "} // namespace bbbbbbbbbb\n" 1660 "} // namespace aaaaaaaaaa", 1661 Style)); 1662 1663 EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n" 1664 "namespace cccccc {\n" 1665 "}}} // namespace aaaaaa::bbbbbb::cccccc", 1666 format("namespace aaaaaa {\n" 1667 "namespace bbbbbb {\n" 1668 "namespace cccccc {\n" 1669 "} // namespace cccccc\n" 1670 "} // namespace bbbbbb\n" 1671 "} // namespace aaaaaa", 1672 Style)); 1673 Style.ColumnLimit = 80; 1674 1675 // Extra semicolon after 'inner' closing brace prevents merging 1676 EXPECT_EQ("namespace out { namespace in {\n" 1677 "}; } // namespace out::in", 1678 format("namespace out {\n" 1679 "namespace in {\n" 1680 "}; // namespace in\n" 1681 "} // namespace out", 1682 Style)); 1683 1684 // Extra semicolon after 'outer' closing brace is conserved 1685 EXPECT_EQ("namespace out { namespace in {\n" 1686 "}}; // namespace out::in", 1687 format("namespace out {\n" 1688 "namespace in {\n" 1689 "} // namespace in\n" 1690 "}; // namespace out", 1691 Style)); 1692 1693 Style.NamespaceIndentation = FormatStyle::NI_All; 1694 EXPECT_EQ("namespace out { namespace in {\n" 1695 " int i;\n" 1696 "}} // namespace out::in", 1697 format("namespace out {\n" 1698 "namespace in {\n" 1699 "int i;\n" 1700 "} // namespace in\n" 1701 "} // namespace out", 1702 Style)); 1703 EXPECT_EQ("namespace out { namespace mid {\n" 1704 " namespace in {\n" 1705 " int j;\n" 1706 " } // namespace in\n" 1707 " int k;\n" 1708 "}} // namespace out::mid", 1709 format("namespace out { namespace mid {\n" 1710 "namespace in { int j; } // namespace in\n" 1711 "int k; }} // namespace out::mid", 1712 Style)); 1713 1714 Style.NamespaceIndentation = FormatStyle::NI_Inner; 1715 EXPECT_EQ("namespace out { namespace in {\n" 1716 " int i;\n" 1717 "}} // namespace out::in", 1718 format("namespace out {\n" 1719 "namespace in {\n" 1720 "int i;\n" 1721 "} // namespace in\n" 1722 "} // namespace out", 1723 Style)); 1724 EXPECT_EQ("namespace out { namespace mid { namespace in {\n" 1725 " int i;\n" 1726 "}}} // namespace out::mid::in", 1727 format("namespace out {\n" 1728 "namespace mid {\n" 1729 "namespace in {\n" 1730 "int i;\n" 1731 "} // namespace in\n" 1732 "} // namespace mid\n" 1733 "} // namespace out", 1734 Style)); 1735 } 1736 1737 TEST_F(FormatTest, FormatsExternC) { 1738 verifyFormat("extern \"C\" {\nint a;"); 1739 verifyFormat("extern \"C\" {}"); 1740 verifyFormat("extern \"C\" {\n" 1741 "int foo();\n" 1742 "}"); 1743 verifyFormat("extern \"C\" int foo() {}"); 1744 verifyFormat("extern \"C\" int foo();"); 1745 verifyFormat("extern \"C\" int foo() {\n" 1746 " int i = 42;\n" 1747 " return i;\n" 1748 "}"); 1749 1750 FormatStyle Style = getLLVMStyle(); 1751 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 1752 Style.BraceWrapping.AfterFunction = true; 1753 verifyFormat("extern \"C\" int foo() {}", Style); 1754 verifyFormat("extern \"C\" int foo();", Style); 1755 verifyFormat("extern \"C\" int foo()\n" 1756 "{\n" 1757 " int i = 42;\n" 1758 " return i;\n" 1759 "}", 1760 Style); 1761 1762 Style.BraceWrapping.AfterExternBlock = true; 1763 Style.BraceWrapping.SplitEmptyRecord = false; 1764 verifyFormat("extern \"C\"\n" 1765 "{}", 1766 Style); 1767 verifyFormat("extern \"C\"\n" 1768 "{\n" 1769 " int foo();\n" 1770 "}", 1771 Style); 1772 } 1773 1774 TEST_F(FormatTest, FormatsInlineASM) { 1775 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"); 1776 verifyFormat("asm(\"nop\" ::: \"memory\");"); 1777 verifyFormat( 1778 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" 1779 " \"cpuid\\n\\t\"\n" 1780 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n" 1781 " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n" 1782 " : \"a\"(value));"); 1783 EXPECT_EQ( 1784 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 1785 " __asm {\n" 1786 " mov edx,[that] // vtable in edx\n" 1787 " mov eax,methodIndex\n" 1788 " call [edx][eax*4] // stdcall\n" 1789 " }\n" 1790 "}", 1791 format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 1792 " __asm {\n" 1793 " mov edx,[that] // vtable in edx\n" 1794 " mov eax,methodIndex\n" 1795 " call [edx][eax*4] // stdcall\n" 1796 " }\n" 1797 "}")); 1798 EXPECT_EQ("_asm {\n" 1799 " xor eax, eax;\n" 1800 " cpuid;\n" 1801 "}", 1802 format("_asm {\n" 1803 " xor eax, eax;\n" 1804 " cpuid;\n" 1805 "}")); 1806 verifyFormat("void function() {\n" 1807 " // comment\n" 1808 " asm(\"\");\n" 1809 "}"); 1810 EXPECT_EQ("__asm {\n" 1811 "}\n" 1812 "int i;", 1813 format("__asm {\n" 1814 "}\n" 1815 "int i;")); 1816 } 1817 1818 TEST_F(FormatTest, FormatTryCatch) { 1819 verifyFormat("try {\n" 1820 " throw a * b;\n" 1821 "} catch (int a) {\n" 1822 " // Do nothing.\n" 1823 "} catch (...) {\n" 1824 " exit(42);\n" 1825 "}"); 1826 1827 // Function-level try statements. 1828 verifyFormat("int f() try { return 4; } catch (...) {\n" 1829 " return 5;\n" 1830 "}"); 1831 verifyFormat("class A {\n" 1832 " int a;\n" 1833 " A() try : a(0) {\n" 1834 " } catch (...) {\n" 1835 " throw;\n" 1836 " }\n" 1837 "};\n"); 1838 1839 // Incomplete try-catch blocks. 1840 verifyIncompleteFormat("try {} catch ("); 1841 } 1842 1843 TEST_F(FormatTest, FormatSEHTryCatch) { 1844 verifyFormat("__try {\n" 1845 " int a = b * c;\n" 1846 "} __except (EXCEPTION_EXECUTE_HANDLER) {\n" 1847 " // Do nothing.\n" 1848 "}"); 1849 1850 verifyFormat("__try {\n" 1851 " int a = b * c;\n" 1852 "} __finally {\n" 1853 " // Do nothing.\n" 1854 "}"); 1855 1856 verifyFormat("DEBUG({\n" 1857 " __try {\n" 1858 " } __finally {\n" 1859 " }\n" 1860 "});\n"); 1861 } 1862 1863 TEST_F(FormatTest, IncompleteTryCatchBlocks) { 1864 verifyFormat("try {\n" 1865 " f();\n" 1866 "} catch {\n" 1867 " g();\n" 1868 "}"); 1869 verifyFormat("try {\n" 1870 " f();\n" 1871 "} catch (A a) MACRO(x) {\n" 1872 " g();\n" 1873 "} catch (B b) MACRO(x) {\n" 1874 " g();\n" 1875 "}"); 1876 } 1877 1878 TEST_F(FormatTest, FormatTryCatchBraceStyles) { 1879 FormatStyle Style = getLLVMStyle(); 1880 for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla, 1881 FormatStyle::BS_WebKit}) { 1882 Style.BreakBeforeBraces = BraceStyle; 1883 verifyFormat("try {\n" 1884 " // something\n" 1885 "} catch (...) {\n" 1886 " // something\n" 1887 "}", 1888 Style); 1889 } 1890 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 1891 verifyFormat("try {\n" 1892 " // something\n" 1893 "}\n" 1894 "catch (...) {\n" 1895 " // something\n" 1896 "}", 1897 Style); 1898 verifyFormat("__try {\n" 1899 " // something\n" 1900 "}\n" 1901 "__finally {\n" 1902 " // something\n" 1903 "}", 1904 Style); 1905 verifyFormat("@try {\n" 1906 " // something\n" 1907 "}\n" 1908 "@finally {\n" 1909 " // something\n" 1910 "}", 1911 Style); 1912 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 1913 verifyFormat("try\n" 1914 "{\n" 1915 " // something\n" 1916 "}\n" 1917 "catch (...)\n" 1918 "{\n" 1919 " // something\n" 1920 "}", 1921 Style); 1922 Style.BreakBeforeBraces = FormatStyle::BS_GNU; 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_Custom; 1933 Style.BraceWrapping.BeforeCatch = true; 1934 verifyFormat("try {\n" 1935 " // something\n" 1936 "}\n" 1937 "catch (...) {\n" 1938 " // something\n" 1939 "}", 1940 Style); 1941 } 1942 1943 TEST_F(FormatTest, StaticInitializers) { 1944 verifyFormat("static SomeClass SC = {1, 'a'};"); 1945 1946 verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n" 1947 " 100000000, " 1948 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};"); 1949 1950 // Here, everything other than the "}" would fit on a line. 1951 verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n" 1952 " 10000000000000000000000000};"); 1953 EXPECT_EQ("S s = {a,\n" 1954 "\n" 1955 " b};", 1956 format("S s = {\n" 1957 " a,\n" 1958 "\n" 1959 " b\n" 1960 "};")); 1961 1962 // FIXME: This would fit into the column limit if we'd fit "{ {" on the first 1963 // line. However, the formatting looks a bit off and this probably doesn't 1964 // happen often in practice. 1965 verifyFormat("static int Variable[1] = {\n" 1966 " {1000000000000000000000000000000000000}};", 1967 getLLVMStyleWithColumns(40)); 1968 } 1969 1970 TEST_F(FormatTest, DesignatedInitializers) { 1971 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 1972 verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n" 1973 " .bbbbbbbbbb = 2,\n" 1974 " .cccccccccc = 3,\n" 1975 " .dddddddddd = 4,\n" 1976 " .eeeeeeeeee = 5};"); 1977 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 1978 " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n" 1979 " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n" 1980 " .ccccccccccccccccccccccccccc = 3,\n" 1981 " .ddddddddddddddddddddddddddd = 4,\n" 1982 " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};"); 1983 1984 verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};"); 1985 1986 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 1987 verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n" 1988 " [2] = bbbbbbbbbb,\n" 1989 " [3] = cccccccccc,\n" 1990 " [4] = dddddddddd,\n" 1991 " [5] = eeeeeeeeee};"); 1992 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 1993 " [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1994 " [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 1995 " [3] = cccccccccccccccccccccccccccccccccccccc,\n" 1996 " [4] = dddddddddddddddddddddddddddddddddddddd,\n" 1997 " [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};"); 1998 } 1999 2000 TEST_F(FormatTest, NestedStaticInitializers) { 2001 verifyFormat("static A x = {{{}}};\n"); 2002 verifyFormat("static A x = {{{init1, init2, init3, init4},\n" 2003 " {init1, init2, init3, init4}}};", 2004 getLLVMStyleWithColumns(50)); 2005 2006 verifyFormat("somes Status::global_reps[3] = {\n" 2007 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 2008 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 2009 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};", 2010 getLLVMStyleWithColumns(60)); 2011 verifyGoogleFormat("SomeType Status::global_reps[3] = {\n" 2012 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 2013 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 2014 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};"); 2015 verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n" 2016 " {rect.fRight - rect.fLeft, rect.fBottom - " 2017 "rect.fTop}};"); 2018 2019 verifyFormat( 2020 "SomeArrayOfSomeType a = {\n" 2021 " {{1, 2, 3},\n" 2022 " {1, 2, 3},\n" 2023 " {111111111111111111111111111111, 222222222222222222222222222222,\n" 2024 " 333333333333333333333333333333},\n" 2025 " {1, 2, 3},\n" 2026 " {1, 2, 3}}};"); 2027 verifyFormat( 2028 "SomeArrayOfSomeType a = {\n" 2029 " {{1, 2, 3}},\n" 2030 " {{1, 2, 3}},\n" 2031 " {{111111111111111111111111111111, 222222222222222222222222222222,\n" 2032 " 333333333333333333333333333333}},\n" 2033 " {{1, 2, 3}},\n" 2034 " {{1, 2, 3}}};"); 2035 2036 verifyFormat("struct {\n" 2037 " unsigned bit;\n" 2038 " const char *const name;\n" 2039 "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n" 2040 " {kOsWin, \"Windows\"},\n" 2041 " {kOsLinux, \"Linux\"},\n" 2042 " {kOsCrOS, \"Chrome OS\"}};"); 2043 verifyFormat("struct {\n" 2044 " unsigned bit;\n" 2045 " const char *const name;\n" 2046 "} kBitsToOs[] = {\n" 2047 " {kOsMac, \"Mac\"},\n" 2048 " {kOsWin, \"Windows\"},\n" 2049 " {kOsLinux, \"Linux\"},\n" 2050 " {kOsCrOS, \"Chrome OS\"},\n" 2051 "};"); 2052 } 2053 2054 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { 2055 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 2056 " \\\n" 2057 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); 2058 } 2059 2060 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { 2061 verifyFormat("virtual void write(ELFWriter *writerrr,\n" 2062 " OwningPtr<FileOutputBuffer> &buffer) = 0;"); 2063 2064 // Do break defaulted and deleted functions. 2065 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 2066 " default;", 2067 getLLVMStyleWithColumns(40)); 2068 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 2069 " delete;", 2070 getLLVMStyleWithColumns(40)); 2071 } 2072 2073 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) { 2074 verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3", 2075 getLLVMStyleWithColumns(40)); 2076 verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 2077 getLLVMStyleWithColumns(40)); 2078 EXPECT_EQ("#define Q \\\n" 2079 " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n" 2080 " \"aaaaaaaa.cpp\"", 2081 format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 2082 getLLVMStyleWithColumns(40))); 2083 } 2084 2085 TEST_F(FormatTest, UnderstandsLinePPDirective) { 2086 EXPECT_EQ("# 123 \"A string literal\"", 2087 format(" # 123 \"A string literal\"")); 2088 } 2089 2090 TEST_F(FormatTest, LayoutUnknownPPDirective) { 2091 EXPECT_EQ("#;", format("#;")); 2092 verifyFormat("#\n;\n;\n;"); 2093 } 2094 2095 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { 2096 EXPECT_EQ("#line 42 \"test\"\n", 2097 format("# \\\n line \\\n 42 \\\n \"test\"\n")); 2098 EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n", 2099 getLLVMStyleWithColumns(12))); 2100 } 2101 2102 TEST_F(FormatTest, EndOfFileEndsPPDirective) { 2103 EXPECT_EQ("#line 42 \"test\"", 2104 format("# \\\n line \\\n 42 \\\n \"test\"")); 2105 EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B")); 2106 } 2107 2108 TEST_F(FormatTest, DoesntRemoveUnknownTokens) { 2109 verifyFormat("#define A \\x20"); 2110 verifyFormat("#define A \\ x20"); 2111 EXPECT_EQ("#define A \\ x20", format("#define A \\ x20")); 2112 verifyFormat("#define A ''"); 2113 verifyFormat("#define A ''qqq"); 2114 verifyFormat("#define A `qqq"); 2115 verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");"); 2116 EXPECT_EQ("const char *c = STRINGIFY(\n" 2117 "\\na : b);", 2118 format("const char * c = STRINGIFY(\n" 2119 "\\na : b);")); 2120 2121 verifyFormat("a\r\\"); 2122 verifyFormat("a\v\\"); 2123 verifyFormat("a\f\\"); 2124 } 2125 2126 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { 2127 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13)); 2128 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12)); 2129 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); 2130 // FIXME: We never break before the macro name. 2131 verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12)); 2132 2133 verifyFormat("#define A A\n#define A A"); 2134 verifyFormat("#define A(X) A\n#define A A"); 2135 2136 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23)); 2137 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22)); 2138 } 2139 2140 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { 2141 EXPECT_EQ("// somecomment\n" 2142 "#include \"a.h\"\n" 2143 "#define A( \\\n" 2144 " A, B)\n" 2145 "#include \"b.h\"\n" 2146 "// somecomment\n", 2147 format(" // somecomment\n" 2148 " #include \"a.h\"\n" 2149 "#define A(A,\\\n" 2150 " B)\n" 2151 " #include \"b.h\"\n" 2152 " // somecomment\n", 2153 getLLVMStyleWithColumns(13))); 2154 } 2155 2156 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); } 2157 2158 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { 2159 EXPECT_EQ("#define A \\\n" 2160 " c; \\\n" 2161 " e;\n" 2162 "f;", 2163 format("#define A c; e;\n" 2164 "f;", 2165 getLLVMStyleWithColumns(14))); 2166 } 2167 2168 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); } 2169 2170 TEST_F(FormatTest, MacroDefinitionInsideStatement) { 2171 EXPECT_EQ("int x,\n" 2172 "#define A\n" 2173 " y;", 2174 format("int x,\n#define A\ny;")); 2175 } 2176 2177 TEST_F(FormatTest, HashInMacroDefinition) { 2178 EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle())); 2179 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); 2180 verifyFormat("#define A \\\n" 2181 " { \\\n" 2182 " f(#c); \\\n" 2183 " }", 2184 getLLVMStyleWithColumns(11)); 2185 2186 verifyFormat("#define A(X) \\\n" 2187 " void function##X()", 2188 getLLVMStyleWithColumns(22)); 2189 2190 verifyFormat("#define A(a, b, c) \\\n" 2191 " void a##b##c()", 2192 getLLVMStyleWithColumns(22)); 2193 2194 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22)); 2195 } 2196 2197 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { 2198 EXPECT_EQ("#define A (x)", format("#define A (x)")); 2199 EXPECT_EQ("#define A(x)", format("#define A(x)")); 2200 } 2201 2202 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) { 2203 EXPECT_EQ("#define A b;", format("#define A \\\n" 2204 " \\\n" 2205 " b;", 2206 getLLVMStyleWithColumns(25))); 2207 EXPECT_EQ("#define A \\\n" 2208 " \\\n" 2209 " a; \\\n" 2210 " b;", 2211 format("#define A \\\n" 2212 " \\\n" 2213 " a; \\\n" 2214 " b;", 2215 getLLVMStyleWithColumns(11))); 2216 EXPECT_EQ("#define A \\\n" 2217 " a; \\\n" 2218 " \\\n" 2219 " b;", 2220 format("#define A \\\n" 2221 " a; \\\n" 2222 " \\\n" 2223 " b;", 2224 getLLVMStyleWithColumns(11))); 2225 } 2226 2227 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { 2228 verifyIncompleteFormat("#define A :"); 2229 verifyFormat("#define SOMECASES \\\n" 2230 " case 1: \\\n" 2231 " case 2\n", 2232 getLLVMStyleWithColumns(20)); 2233 verifyFormat("#define MACRO(a) \\\n" 2234 " if (a) \\\n" 2235 " f(); \\\n" 2236 " else \\\n" 2237 " g()", 2238 getLLVMStyleWithColumns(18)); 2239 verifyFormat("#define A template <typename T>"); 2240 verifyIncompleteFormat("#define STR(x) #x\n" 2241 "f(STR(this_is_a_string_literal{));"); 2242 verifyFormat("#pragma omp threadprivate( \\\n" 2243 " y)), // expected-warning", 2244 getLLVMStyleWithColumns(28)); 2245 verifyFormat("#d, = };"); 2246 verifyFormat("#if \"a"); 2247 verifyIncompleteFormat("({\n" 2248 "#define b \\\n" 2249 " } \\\n" 2250 " a\n" 2251 "a", 2252 getLLVMStyleWithColumns(15)); 2253 verifyFormat("#define A \\\n" 2254 " { \\\n" 2255 " {\n" 2256 "#define B \\\n" 2257 " } \\\n" 2258 " }", 2259 getLLVMStyleWithColumns(15)); 2260 verifyNoCrash("#if a\na(\n#else\n#endif\n{a"); 2261 verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}"); 2262 verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};"); 2263 verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}"); 2264 } 2265 2266 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) { 2267 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline. 2268 EXPECT_EQ("class A : public QObject {\n" 2269 " Q_OBJECT\n" 2270 "\n" 2271 " A() {}\n" 2272 "};", 2273 format("class A : public QObject {\n" 2274 " Q_OBJECT\n" 2275 "\n" 2276 " A() {\n}\n" 2277 "} ;")); 2278 EXPECT_EQ("MACRO\n" 2279 "/*static*/ int i;", 2280 format("MACRO\n" 2281 " /*static*/ int i;")); 2282 EXPECT_EQ("SOME_MACRO\n" 2283 "namespace {\n" 2284 "void f();\n" 2285 "} // namespace", 2286 format("SOME_MACRO\n" 2287 " namespace {\n" 2288 "void f( );\n" 2289 "} // namespace")); 2290 // Only if the identifier contains at least 5 characters. 2291 EXPECT_EQ("HTTP f();", format("HTTP\nf();")); 2292 EXPECT_EQ("MACRO\nf();", format("MACRO\nf();")); 2293 // Only if everything is upper case. 2294 EXPECT_EQ("class A : public QObject {\n" 2295 " Q_Object A() {}\n" 2296 "};", 2297 format("class A : public QObject {\n" 2298 " Q_Object\n" 2299 " A() {\n}\n" 2300 "} ;")); 2301 2302 // Only if the next line can actually start an unwrapped line. 2303 EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;", 2304 format("SOME_WEIRD_LOG_MACRO\n" 2305 "<< SomeThing;")); 2306 2307 verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), " 2308 "(n, buffers))\n", 2309 getChromiumStyle(FormatStyle::LK_Cpp)); 2310 } 2311 2312 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) { 2313 EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 2314 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 2315 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 2316 "class X {};\n" 2317 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 2318 "int *createScopDetectionPass() { return 0; }", 2319 format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 2320 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 2321 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 2322 " class X {};\n" 2323 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 2324 " int *createScopDetectionPass() { return 0; }")); 2325 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as 2326 // braces, so that inner block is indented one level more. 2327 EXPECT_EQ("int q() {\n" 2328 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 2329 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 2330 " IPC_END_MESSAGE_MAP()\n" 2331 "}", 2332 format("int q() {\n" 2333 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 2334 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 2335 " IPC_END_MESSAGE_MAP()\n" 2336 "}")); 2337 2338 // Same inside macros. 2339 EXPECT_EQ("#define LIST(L) \\\n" 2340 " L(A) \\\n" 2341 " L(B) \\\n" 2342 " L(C)", 2343 format("#define LIST(L) \\\n" 2344 " L(A) \\\n" 2345 " L(B) \\\n" 2346 " L(C)", 2347 getGoogleStyle())); 2348 2349 // These must not be recognized as macros. 2350 EXPECT_EQ("int q() {\n" 2351 " f(x);\n" 2352 " f(x) {}\n" 2353 " f(x)->g();\n" 2354 " f(x)->*g();\n" 2355 " f(x).g();\n" 2356 " f(x) = x;\n" 2357 " f(x) += x;\n" 2358 " f(x) -= x;\n" 2359 " f(x) *= x;\n" 2360 " f(x) /= x;\n" 2361 " f(x) %= x;\n" 2362 " f(x) &= x;\n" 2363 " f(x) |= x;\n" 2364 " f(x) ^= x;\n" 2365 " f(x) >>= x;\n" 2366 " f(x) <<= x;\n" 2367 " f(x)[y].z();\n" 2368 " LOG(INFO) << x;\n" 2369 " ifstream(x) >> x;\n" 2370 "}\n", 2371 format("int q() {\n" 2372 " f(x)\n;\n" 2373 " f(x)\n {}\n" 2374 " f(x)\n->g();\n" 2375 " f(x)\n->*g();\n" 2376 " f(x)\n.g();\n" 2377 " f(x)\n = x;\n" 2378 " f(x)\n += x;\n" 2379 " f(x)\n -= x;\n" 2380 " f(x)\n *= x;\n" 2381 " f(x)\n /= x;\n" 2382 " f(x)\n %= x;\n" 2383 " f(x)\n &= x;\n" 2384 " f(x)\n |= x;\n" 2385 " f(x)\n ^= x;\n" 2386 " f(x)\n >>= x;\n" 2387 " f(x)\n <<= x;\n" 2388 " f(x)\n[y].z();\n" 2389 " LOG(INFO)\n << x;\n" 2390 " ifstream(x)\n >> x;\n" 2391 "}\n")); 2392 EXPECT_EQ("int q() {\n" 2393 " F(x)\n" 2394 " if (1) {\n" 2395 " }\n" 2396 " F(x)\n" 2397 " while (1) {\n" 2398 " }\n" 2399 " F(x)\n" 2400 " G(x);\n" 2401 " F(x)\n" 2402 " try {\n" 2403 " Q();\n" 2404 " } catch (...) {\n" 2405 " }\n" 2406 "}\n", 2407 format("int q() {\n" 2408 "F(x)\n" 2409 "if (1) {}\n" 2410 "F(x)\n" 2411 "while (1) {}\n" 2412 "F(x)\n" 2413 "G(x);\n" 2414 "F(x)\n" 2415 "try { Q(); } catch (...) {}\n" 2416 "}\n")); 2417 EXPECT_EQ("class A {\n" 2418 " A() : t(0) {}\n" 2419 " A(int i) noexcept() : {}\n" 2420 " A(X x)\n" // FIXME: function-level try blocks are broken. 2421 " try : t(0) {\n" 2422 " } catch (...) {\n" 2423 " }\n" 2424 "};", 2425 format("class A {\n" 2426 " A()\n : t(0) {}\n" 2427 " A(int i)\n noexcept() : {}\n" 2428 " A(X x)\n" 2429 " try : t(0) {} catch (...) {}\n" 2430 "};")); 2431 EXPECT_EQ("class SomeClass {\n" 2432 "public:\n" 2433 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2434 "};", 2435 format("class SomeClass {\n" 2436 "public:\n" 2437 " SomeClass()\n" 2438 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2439 "};")); 2440 EXPECT_EQ("class SomeClass {\n" 2441 "public:\n" 2442 " SomeClass()\n" 2443 " 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 getLLVMStyleWithColumns(40))); 2451 2452 verifyFormat("MACRO(>)"); 2453 } 2454 2455 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) { 2456 verifyFormat("#define A \\\n" 2457 " f({ \\\n" 2458 " g(); \\\n" 2459 " });", 2460 getLLVMStyleWithColumns(11)); 2461 } 2462 2463 TEST_F(FormatTest, IndentPreprocessorDirectives) { 2464 FormatStyle Style = getLLVMStyle(); 2465 Style.IndentPPDirectives = FormatStyle::PPDIS_None; 2466 Style.ColumnLimit = 40; 2467 verifyFormat("#ifdef _WIN32\n" 2468 "#define A 0\n" 2469 "#ifdef VAR2\n" 2470 "#define B 1\n" 2471 "#include <someheader.h>\n" 2472 "#define MACRO \\\n" 2473 " some_very_long_func_aaaaaaaaaa();\n" 2474 "#endif\n" 2475 "#else\n" 2476 "#define A 1\n" 2477 "#endif", 2478 Style); 2479 Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash; 2480 verifyFormat("#ifdef _WIN32\n" 2481 "# define A 0\n" 2482 "# ifdef VAR2\n" 2483 "# define B 1\n" 2484 "# include <someheader.h>\n" 2485 "# define MACRO \\\n" 2486 " some_very_long_func_aaaaaaaaaa();\n" 2487 "# endif\n" 2488 "#else\n" 2489 "# define A 1\n" 2490 "#endif", 2491 Style); 2492 verifyFormat("#if A\n" 2493 "# define MACRO \\\n" 2494 " void a(int x) { \\\n" 2495 " b(); \\\n" 2496 " c(); \\\n" 2497 " d(); \\\n" 2498 " e(); \\\n" 2499 " f(); \\\n" 2500 " }\n" 2501 "#endif", 2502 Style); 2503 // Comments before include guard. 2504 verifyFormat("// file comment\n" 2505 "// file comment\n" 2506 "#ifndef HEADER_H\n" 2507 "#define HEADER_H\n" 2508 "code();\n" 2509 "#endif", 2510 Style); 2511 // Test with include guards. 2512 verifyFormat("#ifndef HEADER_H\n" 2513 "#define HEADER_H\n" 2514 "code();\n" 2515 "#endif", 2516 Style); 2517 // Include guards must have a #define with the same variable immediately 2518 // after #ifndef. 2519 verifyFormat("#ifndef NOT_GUARD\n" 2520 "# define FOO\n" 2521 "code();\n" 2522 "#endif", 2523 Style); 2524 2525 // Include guards must cover the entire file. 2526 verifyFormat("code();\n" 2527 "code();\n" 2528 "#ifndef NOT_GUARD\n" 2529 "# define NOT_GUARD\n" 2530 "code();\n" 2531 "#endif", 2532 Style); 2533 verifyFormat("#ifndef NOT_GUARD\n" 2534 "# define NOT_GUARD\n" 2535 "code();\n" 2536 "#endif\n" 2537 "code();", 2538 Style); 2539 // Test with trailing blank lines. 2540 verifyFormat("#ifndef HEADER_H\n" 2541 "#define HEADER_H\n" 2542 "code();\n" 2543 "#endif\n", 2544 Style); 2545 // Include guards don't have #else. 2546 verifyFormat("#ifndef NOT_GUARD\n" 2547 "# define NOT_GUARD\n" 2548 "code();\n" 2549 "#else\n" 2550 "#endif", 2551 Style); 2552 verifyFormat("#ifndef NOT_GUARD\n" 2553 "# define NOT_GUARD\n" 2554 "code();\n" 2555 "#elif FOO\n" 2556 "#endif", 2557 Style); 2558 // Non-identifier #define after potential include guard. 2559 verifyFormat("#ifndef FOO\n" 2560 "# define 1\n" 2561 "#endif\n", 2562 Style); 2563 // #if closes past last non-preprocessor line. 2564 verifyFormat("#ifndef FOO\n" 2565 "#define FOO\n" 2566 "#if 1\n" 2567 "int i;\n" 2568 "# define A 0\n" 2569 "#endif\n" 2570 "#endif\n", 2571 Style); 2572 // FIXME: This doesn't handle the case where there's code between the 2573 // #ifndef and #define but all other conditions hold. This is because when 2574 // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the 2575 // previous code line yet, so we can't detect it. 2576 EXPECT_EQ("#ifndef NOT_GUARD\n" 2577 "code();\n" 2578 "#define NOT_GUARD\n" 2579 "code();\n" 2580 "#endif", 2581 format("#ifndef NOT_GUARD\n" 2582 "code();\n" 2583 "# define NOT_GUARD\n" 2584 "code();\n" 2585 "#endif", 2586 Style)); 2587 // FIXME: This doesn't handle cases where legitimate preprocessor lines may 2588 // be outside an include guard. Examples are #pragma once and 2589 // #pragma GCC diagnostic, or anything else that does not change the meaning 2590 // of the file if it's included multiple times. 2591 EXPECT_EQ("#ifdef WIN32\n" 2592 "# pragma once\n" 2593 "#endif\n" 2594 "#ifndef HEADER_H\n" 2595 "# define HEADER_H\n" 2596 "code();\n" 2597 "#endif", 2598 format("#ifdef WIN32\n" 2599 "# pragma once\n" 2600 "#endif\n" 2601 "#ifndef HEADER_H\n" 2602 "#define HEADER_H\n" 2603 "code();\n" 2604 "#endif", 2605 Style)); 2606 // FIXME: This does not detect when there is a single non-preprocessor line 2607 // in front of an include-guard-like structure where other conditions hold 2608 // because ScopedLineState hides the line. 2609 EXPECT_EQ("code();\n" 2610 "#ifndef HEADER_H\n" 2611 "#define HEADER_H\n" 2612 "code();\n" 2613 "#endif", 2614 format("code();\n" 2615 "#ifndef HEADER_H\n" 2616 "# define HEADER_H\n" 2617 "code();\n" 2618 "#endif", 2619 Style)); 2620 // Keep comments aligned with #, otherwise indent comments normally. These 2621 // tests cannot use verifyFormat because messUp manipulates leading 2622 // whitespace. 2623 { 2624 const char *Expected = "" 2625 "void f() {\n" 2626 "#if 1\n" 2627 "// Preprocessor aligned.\n" 2628 "# define A 0\n" 2629 " // Code. Separated by blank line.\n" 2630 "\n" 2631 "# define B 0\n" 2632 " // Code. Not aligned with #\n" 2633 "# define C 0\n" 2634 "#endif"; 2635 const char *ToFormat = "" 2636 "void f() {\n" 2637 "#if 1\n" 2638 "// Preprocessor aligned.\n" 2639 "# define A 0\n" 2640 "// Code. Separated by blank line.\n" 2641 "\n" 2642 "# define B 0\n" 2643 " // Code. Not aligned with #\n" 2644 "# define C 0\n" 2645 "#endif"; 2646 EXPECT_EQ(Expected, format(ToFormat, Style)); 2647 EXPECT_EQ(Expected, format(Expected, Style)); 2648 } 2649 // Keep block quotes aligned. 2650 { 2651 const char *Expected = "" 2652 "void f() {\n" 2653 "#if 1\n" 2654 "/* Preprocessor aligned. */\n" 2655 "# define A 0\n" 2656 " /* Code. Separated by blank line. */\n" 2657 "\n" 2658 "# define B 0\n" 2659 " /* Code. Not aligned with # */\n" 2660 "# define C 0\n" 2661 "#endif"; 2662 const char *ToFormat = "" 2663 "void f() {\n" 2664 "#if 1\n" 2665 "/* Preprocessor aligned. */\n" 2666 "# define A 0\n" 2667 "/* Code. Separated by blank line. */\n" 2668 "\n" 2669 "# define B 0\n" 2670 " /* Code. Not aligned with # */\n" 2671 "# define C 0\n" 2672 "#endif"; 2673 EXPECT_EQ(Expected, format(ToFormat, Style)); 2674 EXPECT_EQ(Expected, format(Expected, Style)); 2675 } 2676 // Keep comments aligned with un-indented directives. 2677 { 2678 const char *Expected = "" 2679 "void f() {\n" 2680 "// Preprocessor aligned.\n" 2681 "#define A 0\n" 2682 " // Code. Separated by blank line.\n" 2683 "\n" 2684 "#define B 0\n" 2685 " // Code. Not aligned with #\n" 2686 "#define C 0\n"; 2687 const char *ToFormat = "" 2688 "void f() {\n" 2689 "// Preprocessor aligned.\n" 2690 "#define A 0\n" 2691 "// Code. Separated by blank line.\n" 2692 "\n" 2693 "#define B 0\n" 2694 " // Code. Not aligned with #\n" 2695 "#define C 0\n"; 2696 EXPECT_EQ(Expected, format(ToFormat, Style)); 2697 EXPECT_EQ(Expected, format(Expected, Style)); 2698 } 2699 // Test with tabs. 2700 Style.UseTab = FormatStyle::UT_Always; 2701 Style.IndentWidth = 8; 2702 Style.TabWidth = 8; 2703 verifyFormat("#ifdef _WIN32\n" 2704 "#\tdefine A 0\n" 2705 "#\tifdef VAR2\n" 2706 "#\t\tdefine B 1\n" 2707 "#\t\tinclude <someheader.h>\n" 2708 "#\t\tdefine MACRO \\\n" 2709 "\t\t\tsome_very_long_func_aaaaaaaaaa();\n" 2710 "#\tendif\n" 2711 "#else\n" 2712 "#\tdefine A 1\n" 2713 "#endif", 2714 Style); 2715 2716 // Regression test: Multiline-macro inside include guards. 2717 verifyFormat("#ifndef HEADER_H\n" 2718 "#define HEADER_H\n" 2719 "#define A() \\\n" 2720 " int i; \\\n" 2721 " int j;\n" 2722 "#endif // HEADER_H", 2723 getLLVMStyleWithColumns(20)); 2724 } 2725 2726 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { 2727 verifyFormat("{\n { a #c; }\n}"); 2728 } 2729 2730 TEST_F(FormatTest, FormatUnbalancedStructuralElements) { 2731 EXPECT_EQ("#define A \\\n { \\\n {\nint i;", 2732 format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); 2733 EXPECT_EQ("#define A \\\n } \\\n }\nint i;", 2734 format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); 2735 } 2736 2737 TEST_F(FormatTest, EscapedNewlines) { 2738 FormatStyle Narrow = getLLVMStyleWithColumns(11); 2739 EXPECT_EQ("#define A \\\n int i; \\\n int j;", 2740 format("#define A \\\nint i;\\\n int j;", Narrow)); 2741 EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;")); 2742 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 2743 EXPECT_EQ("/* \\ \\ \\\n */", format("\\\n/* \\ \\ \\\n */")); 2744 EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>")); 2745 2746 FormatStyle AlignLeft = getLLVMStyle(); 2747 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 2748 EXPECT_EQ("#define MACRO(x) \\\n" 2749 "private: \\\n" 2750 " int x(int a);\n", 2751 format("#define MACRO(x) \\\n" 2752 "private: \\\n" 2753 " int x(int a);\n", 2754 AlignLeft)); 2755 2756 // CRLF line endings 2757 EXPECT_EQ("#define A \\\r\n int i; \\\r\n int j;", 2758 format("#define A \\\r\nint i;\\\r\n int j;", Narrow)); 2759 EXPECT_EQ("#define A\r\n\r\nint i;", format("#define A \\\r\n\r\n int i;")); 2760 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 2761 EXPECT_EQ("/* \\ \\ \\\r\n */", format("\\\r\n/* \\ \\ \\\r\n */")); 2762 EXPECT_EQ("<a\r\n\\\\\r\n>", format("<a\r\n\\\\\r\n>")); 2763 EXPECT_EQ("#define MACRO(x) \\\r\n" 2764 "private: \\\r\n" 2765 " int x(int a);\r\n", 2766 format("#define MACRO(x) \\\r\n" 2767 "private: \\\r\n" 2768 " int x(int a);\r\n", 2769 AlignLeft)); 2770 2771 FormatStyle DontAlign = getLLVMStyle(); 2772 DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 2773 DontAlign.MaxEmptyLinesToKeep = 3; 2774 // FIXME: can't use verifyFormat here because the newline before 2775 // "public:" is not inserted the first time it's reformatted 2776 EXPECT_EQ("#define A \\\n" 2777 " class Foo { \\\n" 2778 " void bar(); \\\n" 2779 "\\\n" 2780 "\\\n" 2781 "\\\n" 2782 " public: \\\n" 2783 " void baz(); \\\n" 2784 " };", 2785 format("#define A \\\n" 2786 " class Foo { \\\n" 2787 " void bar(); \\\n" 2788 "\\\n" 2789 "\\\n" 2790 "\\\n" 2791 " public: \\\n" 2792 " void baz(); \\\n" 2793 " };", 2794 DontAlign)); 2795 } 2796 2797 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { 2798 verifyFormat("#define A \\\n" 2799 " int v( \\\n" 2800 " a); \\\n" 2801 " int i;", 2802 getLLVMStyleWithColumns(11)); 2803 } 2804 2805 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { 2806 EXPECT_EQ( 2807 "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 2808 " \\\n" 2809 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 2810 "\n" 2811 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 2812 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", 2813 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" 2814 "\\\n" 2815 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 2816 " \n" 2817 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 2818 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); 2819 } 2820 2821 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { 2822 EXPECT_EQ("int\n" 2823 "#define A\n" 2824 " a;", 2825 format("int\n#define A\na;")); 2826 verifyFormat("functionCallTo(\n" 2827 " someOtherFunction(\n" 2828 " withSomeParameters, whichInSequence,\n" 2829 " areLongerThanALine(andAnotherCall,\n" 2830 "#define A B\n" 2831 " withMoreParamters,\n" 2832 " whichStronglyInfluenceTheLayout),\n" 2833 " andMoreParameters),\n" 2834 " trailing);", 2835 getLLVMStyleWithColumns(69)); 2836 verifyFormat("Foo::Foo()\n" 2837 "#ifdef BAR\n" 2838 " : baz(0)\n" 2839 "#endif\n" 2840 "{\n" 2841 "}"); 2842 verifyFormat("void f() {\n" 2843 " if (true)\n" 2844 "#ifdef A\n" 2845 " f(42);\n" 2846 " x();\n" 2847 "#else\n" 2848 " g();\n" 2849 " x();\n" 2850 "#endif\n" 2851 "}"); 2852 verifyFormat("void f(param1, param2,\n" 2853 " param3,\n" 2854 "#ifdef A\n" 2855 " param4(param5,\n" 2856 "#ifdef A1\n" 2857 " param6,\n" 2858 "#ifdef A2\n" 2859 " param7),\n" 2860 "#else\n" 2861 " param8),\n" 2862 " param9,\n" 2863 "#endif\n" 2864 " param10,\n" 2865 "#endif\n" 2866 " param11)\n" 2867 "#else\n" 2868 " param12)\n" 2869 "#endif\n" 2870 "{\n" 2871 " x();\n" 2872 "}", 2873 getLLVMStyleWithColumns(28)); 2874 verifyFormat("#if 1\n" 2875 "int i;"); 2876 verifyFormat("#if 1\n" 2877 "#endif\n" 2878 "#if 1\n" 2879 "#else\n" 2880 "#endif\n"); 2881 verifyFormat("DEBUG({\n" 2882 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2883 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 2884 "});\n" 2885 "#if a\n" 2886 "#else\n" 2887 "#endif"); 2888 2889 verifyIncompleteFormat("void f(\n" 2890 "#if A\n" 2891 ");\n" 2892 "#else\n" 2893 "#endif"); 2894 } 2895 2896 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) { 2897 verifyFormat("#endif\n" 2898 "#if B"); 2899 } 2900 2901 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) { 2902 FormatStyle SingleLine = getLLVMStyle(); 2903 SingleLine.AllowShortIfStatementsOnASingleLine = true; 2904 verifyFormat("#if 0\n" 2905 "#elif 1\n" 2906 "#endif\n" 2907 "void foo() {\n" 2908 " if (test) foo2();\n" 2909 "}", 2910 SingleLine); 2911 } 2912 2913 TEST_F(FormatTest, LayoutBlockInsideParens) { 2914 verifyFormat("functionCall({ int i; });"); 2915 verifyFormat("functionCall({\n" 2916 " int i;\n" 2917 " int j;\n" 2918 "});"); 2919 verifyFormat("functionCall(\n" 2920 " {\n" 2921 " int i;\n" 2922 " int j;\n" 2923 " },\n" 2924 " aaaa, bbbb, cccc);"); 2925 verifyFormat("functionA(functionB({\n" 2926 " int i;\n" 2927 " int j;\n" 2928 " }),\n" 2929 " aaaa, bbbb, cccc);"); 2930 verifyFormat("functionCall(\n" 2931 " {\n" 2932 " int i;\n" 2933 " int j;\n" 2934 " },\n" 2935 " aaaa, bbbb, // comment\n" 2936 " cccc);"); 2937 verifyFormat("functionA(functionB({\n" 2938 " int i;\n" 2939 " int j;\n" 2940 " }),\n" 2941 " aaaa, bbbb, // comment\n" 2942 " cccc);"); 2943 verifyFormat("functionCall(aaaa, bbbb, { int i; });"); 2944 verifyFormat("functionCall(aaaa, bbbb, {\n" 2945 " int i;\n" 2946 " int j;\n" 2947 "});"); 2948 verifyFormat( 2949 "Aaa(\n" // FIXME: There shouldn't be a linebreak here. 2950 " {\n" 2951 " int i; // break\n" 2952 " },\n" 2953 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 2954 " ccccccccccccccccc));"); 2955 verifyFormat("DEBUG({\n" 2956 " if (a)\n" 2957 " f();\n" 2958 "});"); 2959 } 2960 2961 TEST_F(FormatTest, LayoutBlockInsideStatement) { 2962 EXPECT_EQ("SOME_MACRO { int i; }\n" 2963 "int i;", 2964 format(" SOME_MACRO {int i;} int i;")); 2965 } 2966 2967 TEST_F(FormatTest, LayoutNestedBlocks) { 2968 verifyFormat("void AddOsStrings(unsigned bitmask) {\n" 2969 " struct s {\n" 2970 " int i;\n" 2971 " };\n" 2972 " s kBitsToOs[] = {{10}};\n" 2973 " for (int i = 0; i < 10; ++i)\n" 2974 " return;\n" 2975 "}"); 2976 verifyFormat("call(parameter, {\n" 2977 " something();\n" 2978 " // Comment using all columns.\n" 2979 " somethingelse();\n" 2980 "});", 2981 getLLVMStyleWithColumns(40)); 2982 verifyFormat("DEBUG( //\n" 2983 " { f(); }, a);"); 2984 verifyFormat("DEBUG( //\n" 2985 " {\n" 2986 " f(); //\n" 2987 " },\n" 2988 " a);"); 2989 2990 EXPECT_EQ("call(parameter, {\n" 2991 " something();\n" 2992 " // Comment too\n" 2993 " // looooooooooong.\n" 2994 " somethingElse();\n" 2995 "});", 2996 format("call(parameter, {\n" 2997 " something();\n" 2998 " // Comment too looooooooooong.\n" 2999 " somethingElse();\n" 3000 "});", 3001 getLLVMStyleWithColumns(29))); 3002 EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });")); 3003 EXPECT_EQ("DEBUG({ // comment\n" 3004 " int i;\n" 3005 "});", 3006 format("DEBUG({ // comment\n" 3007 "int i;\n" 3008 "});")); 3009 EXPECT_EQ("DEBUG({\n" 3010 " int i;\n" 3011 "\n" 3012 " // comment\n" 3013 " int j;\n" 3014 "});", 3015 format("DEBUG({\n" 3016 " int i;\n" 3017 "\n" 3018 " // comment\n" 3019 " int j;\n" 3020 "});")); 3021 3022 verifyFormat("DEBUG({\n" 3023 " if (a)\n" 3024 " return;\n" 3025 "});"); 3026 verifyGoogleFormat("DEBUG({\n" 3027 " if (a) return;\n" 3028 "});"); 3029 FormatStyle Style = getGoogleStyle(); 3030 Style.ColumnLimit = 45; 3031 verifyFormat("Debug(aaaaa,\n" 3032 " {\n" 3033 " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n" 3034 " },\n" 3035 " a);", 3036 Style); 3037 3038 verifyFormat("SomeFunction({MACRO({ return output; }), b});"); 3039 3040 verifyNoCrash("^{v^{a}}"); 3041 } 3042 3043 TEST_F(FormatTest, FormatNestedBlocksInMacros) { 3044 EXPECT_EQ("#define MACRO() \\\n" 3045 " Debug(aaa, /* force line break */ \\\n" 3046 " { \\\n" 3047 " int i; \\\n" 3048 " int j; \\\n" 3049 " })", 3050 format("#define MACRO() Debug(aaa, /* force line break */ \\\n" 3051 " { int i; int j; })", 3052 getGoogleStyle())); 3053 3054 EXPECT_EQ("#define A \\\n" 3055 " [] { \\\n" 3056 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 3057 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 3058 " }", 3059 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 3060 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 3061 getGoogleStyle())); 3062 } 3063 3064 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { 3065 EXPECT_EQ("{}", format("{}")); 3066 verifyFormat("enum E {};"); 3067 verifyFormat("enum E {}"); 3068 } 3069 3070 TEST_F(FormatTest, FormatBeginBlockEndMacros) { 3071 FormatStyle Style = getLLVMStyle(); 3072 Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$"; 3073 Style.MacroBlockEnd = "^[A-Z_]+_END$"; 3074 verifyFormat("FOO_BEGIN\n" 3075 " FOO_ENTRY\n" 3076 "FOO_END", Style); 3077 verifyFormat("FOO_BEGIN\n" 3078 " NESTED_FOO_BEGIN\n" 3079 " NESTED_FOO_ENTRY\n" 3080 " NESTED_FOO_END\n" 3081 "FOO_END", Style); 3082 verifyFormat("FOO_BEGIN(Foo, Bar)\n" 3083 " int x;\n" 3084 " x = 1;\n" 3085 "FOO_END(Baz)", Style); 3086 } 3087 3088 //===----------------------------------------------------------------------===// 3089 // Line break tests. 3090 //===----------------------------------------------------------------------===// 3091 3092 TEST_F(FormatTest, PreventConfusingIndents) { 3093 verifyFormat( 3094 "void f() {\n" 3095 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" 3096 " parameter, parameter, parameter)),\n" 3097 " SecondLongCall(parameter));\n" 3098 "}"); 3099 verifyFormat( 3100 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3101 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 3102 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3103 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 3104 verifyFormat( 3105 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3106 " [aaaaaaaaaaaaaaaaaaaaaaaa\n" 3107 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 3108 " [aaaaaaaaaaaaaaaaaaaaaaaa]];"); 3109 verifyFormat( 3110 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 3111 " aaaaaaaaaaaaaaaaaaaaaaaa<\n" 3112 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n" 3113 " aaaaaaaaaaaaaaaaaaaaaaaa>;"); 3114 verifyFormat("int a = bbbb && ccc &&\n" 3115 " fffff(\n" 3116 "#define A Just forcing a new line\n" 3117 " ddd);"); 3118 } 3119 3120 TEST_F(FormatTest, LineBreakingInBinaryExpressions) { 3121 verifyFormat( 3122 "bool aaaaaaa =\n" 3123 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n" 3124 " bbbbbbbb();"); 3125 verifyFormat( 3126 "bool aaaaaaa =\n" 3127 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n" 3128 " bbbbbbbb();"); 3129 3130 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 3131 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n" 3132 " ccccccccc == ddddddddddd;"); 3133 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 3134 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n" 3135 " ccccccccc == ddddddddddd;"); 3136 verifyFormat( 3137 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 3138 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n" 3139 " ccccccccc == ddddddddddd;"); 3140 3141 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 3142 " aaaaaa) &&\n" 3143 " bbbbbb && cccccc;"); 3144 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 3145 " aaaaaa) >>\n" 3146 " bbbbbb;"); 3147 verifyFormat("aa = Whitespaces.addUntouchableComment(\n" 3148 " SourceMgr.getSpellingColumnNumber(\n" 3149 " TheLine.Last->FormatTok.Tok.getLocation()) -\n" 3150 " 1);"); 3151 3152 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3153 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n" 3154 " cccccc) {\n}"); 3155 verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3156 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n" 3157 " cccccc) {\n}"); 3158 verifyFormat("b = a &&\n" 3159 " // Comment\n" 3160 " b.c && d;"); 3161 3162 // If the LHS of a comparison is not a binary expression itself, the 3163 // additional linebreak confuses many people. 3164 verifyFormat( 3165 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3166 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n" 3167 "}"); 3168 verifyFormat( 3169 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3170 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3171 "}"); 3172 verifyFormat( 3173 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n" 3174 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3175 "}"); 3176 verifyFormat( 3177 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3178 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n" 3179 "}"); 3180 // Even explicit parentheses stress the precedence enough to make the 3181 // additional break unnecessary. 3182 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3183 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3184 "}"); 3185 // This cases is borderline, but with the indentation it is still readable. 3186 verifyFormat( 3187 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3188 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3189 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 3190 "}", 3191 getLLVMStyleWithColumns(75)); 3192 3193 // If the LHS is a binary expression, we should still use the additional break 3194 // as otherwise the formatting hides the operator precedence. 3195 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3196 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3197 " 5) {\n" 3198 "}"); 3199 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3200 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n" 3201 " 5) {\n" 3202 "}"); 3203 3204 FormatStyle OnePerLine = getLLVMStyle(); 3205 OnePerLine.BinPackParameters = false; 3206 verifyFormat( 3207 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3208 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3209 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}", 3210 OnePerLine); 3211 3212 verifyFormat("int i = someFunction(aaaaaaa, 0)\n" 3213 " .aaa(aaaaaaaaaaaaa) *\n" 3214 " aaaaaaa +\n" 3215 " aaaaaaa;", 3216 getLLVMStyleWithColumns(40)); 3217 } 3218 3219 TEST_F(FormatTest, ExpressionIndentation) { 3220 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3221 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3222 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3223 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3224 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 3225 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n" 3226 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3227 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n" 3228 " ccccccccccccccccccccccccccccccccccccccccc;"); 3229 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3230 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3231 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3232 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3233 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3234 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3235 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3236 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3237 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3238 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3239 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3240 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3241 verifyFormat("if () {\n" 3242 "} else if (aaaaa && bbbbb > // break\n" 3243 " ccccc) {\n" 3244 "}"); 3245 verifyFormat("if () {\n" 3246 "} else if (aaaaa &&\n" 3247 " bbbbb > // break\n" 3248 " ccccc &&\n" 3249 " ddddd) {\n" 3250 "}"); 3251 3252 // Presence of a trailing comment used to change indentation of b. 3253 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n" 3254 " b;\n" 3255 "return aaaaaaaaaaaaaaaaaaa +\n" 3256 " b; //", 3257 getLLVMStyleWithColumns(30)); 3258 } 3259 3260 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) { 3261 // Not sure what the best system is here. Like this, the LHS can be found 3262 // immediately above an operator (everything with the same or a higher 3263 // indent). The RHS is aligned right of the operator and so compasses 3264 // everything until something with the same indent as the operator is found. 3265 // FIXME: Is this a good system? 3266 FormatStyle Style = getLLVMStyle(); 3267 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 3268 verifyFormat( 3269 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3270 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3271 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3272 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3273 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3274 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3275 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3276 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3277 " > ccccccccccccccccccccccccccccccccccccccccc;", 3278 Style); 3279 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3280 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3281 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3282 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3283 Style); 3284 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3285 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3286 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3287 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3288 Style); 3289 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3290 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3291 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3292 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3293 Style); 3294 verifyFormat("if () {\n" 3295 "} else if (aaaaa\n" 3296 " && bbbbb // break\n" 3297 " > ccccc) {\n" 3298 "}", 3299 Style); 3300 verifyFormat("return (a)\n" 3301 " // comment\n" 3302 " + b;", 3303 Style); 3304 verifyFormat( 3305 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3306 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3307 " + cc;", 3308 Style); 3309 3310 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3311 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 3312 Style); 3313 3314 // Forced by comments. 3315 verifyFormat( 3316 "unsigned ContentSize =\n" 3317 " sizeof(int16_t) // DWARF ARange version number\n" 3318 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 3319 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 3320 " + sizeof(int8_t); // Segment Size (in bytes)"); 3321 3322 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 3323 " == boost::fusion::at_c<1>(iiii).second;", 3324 Style); 3325 3326 Style.ColumnLimit = 60; 3327 verifyFormat("zzzzzzzzzz\n" 3328 " = bbbbbbbbbbbbbbbbb\n" 3329 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 3330 Style); 3331 } 3332 3333 TEST_F(FormatTest, EnforcedOperatorWraps) { 3334 // Here we'd like to wrap after the || operators, but a comment is forcing an 3335 // earlier wrap. 3336 verifyFormat("bool x = aaaaa //\n" 3337 " || bbbbb\n" 3338 " //\n" 3339 " || cccc;"); 3340 } 3341 3342 TEST_F(FormatTest, NoOperandAlignment) { 3343 FormatStyle Style = getLLVMStyle(); 3344 Style.AlignOperands = false; 3345 verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n" 3346 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3347 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 3348 Style); 3349 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 3350 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3351 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3352 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3353 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3354 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3355 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3356 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3357 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3358 " > ccccccccccccccccccccccccccccccccccccccccc;", 3359 Style); 3360 3361 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3362 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3363 " + cc;", 3364 Style); 3365 verifyFormat("int a = aa\n" 3366 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3367 " * cccccccccccccccccccccccccccccccccccc;\n", 3368 Style); 3369 3370 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 3371 verifyFormat("return (a > b\n" 3372 " // comment1\n" 3373 " // comment2\n" 3374 " || c);", 3375 Style); 3376 } 3377 3378 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) { 3379 FormatStyle Style = getLLVMStyle(); 3380 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 3381 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 3382 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3383 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 3384 Style); 3385 } 3386 3387 TEST_F(FormatTest, AllowBinPackingInsideArguments) { 3388 FormatStyle Style = getLLVMStyle(); 3389 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 3390 Style.BinPackArguments = false; 3391 Style.ColumnLimit = 40; 3392 verifyFormat("void test() {\n" 3393 " someFunction(\n" 3394 " this + argument + is + quite\n" 3395 " + long + so + it + gets + wrapped\n" 3396 " + but + remains + bin - packed);\n" 3397 "}", 3398 Style); 3399 verifyFormat("void test() {\n" 3400 " someFunction(arg1,\n" 3401 " this + argument + is\n" 3402 " + quite + long + so\n" 3403 " + it + gets + wrapped\n" 3404 " + but + remains + bin\n" 3405 " - packed,\n" 3406 " arg3);\n" 3407 "}", 3408 Style); 3409 verifyFormat("void test() {\n" 3410 " someFunction(\n" 3411 " arg1,\n" 3412 " this + argument + has\n" 3413 " + anotherFunc(nested,\n" 3414 " calls + whose\n" 3415 " + arguments\n" 3416 " + are + also\n" 3417 " + wrapped,\n" 3418 " in + addition)\n" 3419 " + to + being + bin - packed,\n" 3420 " arg3);\n" 3421 "}", 3422 Style); 3423 3424 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 3425 verifyFormat("void test() {\n" 3426 " someFunction(\n" 3427 " arg1,\n" 3428 " this + argument + has +\n" 3429 " anotherFunc(nested,\n" 3430 " calls + whose +\n" 3431 " arguments +\n" 3432 " are + also +\n" 3433 " wrapped,\n" 3434 " in + addition) +\n" 3435 " to + being + bin - packed,\n" 3436 " arg3);\n" 3437 "}", 3438 Style); 3439 } 3440 3441 TEST_F(FormatTest, ConstructorInitializers) { 3442 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 3443 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", 3444 getLLVMStyleWithColumns(45)); 3445 verifyFormat("Constructor()\n" 3446 " : Inttializer(FitsOnTheLine) {}", 3447 getLLVMStyleWithColumns(44)); 3448 verifyFormat("Constructor()\n" 3449 " : Inttializer(FitsOnTheLine) {}", 3450 getLLVMStyleWithColumns(43)); 3451 3452 verifyFormat("template <typename T>\n" 3453 "Constructor() : Initializer(FitsOnTheLine) {}", 3454 getLLVMStyleWithColumns(45)); 3455 3456 verifyFormat( 3457 "SomeClass::Constructor()\n" 3458 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 3459 3460 verifyFormat( 3461 "SomeClass::Constructor()\n" 3462 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3463 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}"); 3464 verifyFormat( 3465 "SomeClass::Constructor()\n" 3466 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3467 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 3468 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3469 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3470 " : aaaaaaaaaa(aaaaaa) {}"); 3471 3472 verifyFormat("Constructor()\n" 3473 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3474 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3475 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3476 " aaaaaaaaaaaaaaaaaaaaaaa() {}"); 3477 3478 verifyFormat("Constructor()\n" 3479 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3480 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 3481 3482 verifyFormat("Constructor(int Parameter = 0)\n" 3483 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 3484 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}"); 3485 verifyFormat("Constructor()\n" 3486 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 3487 "}", 3488 getLLVMStyleWithColumns(60)); 3489 verifyFormat("Constructor()\n" 3490 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3491 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}"); 3492 3493 // Here a line could be saved by splitting the second initializer onto two 3494 // lines, but that is not desirable. 3495 verifyFormat("Constructor()\n" 3496 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 3497 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 3498 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 3499 3500 FormatStyle OnePerLine = getLLVMStyle(); 3501 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 3502 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false; 3503 verifyFormat("SomeClass::Constructor()\n" 3504 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3505 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3506 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3507 OnePerLine); 3508 verifyFormat("SomeClass::Constructor()\n" 3509 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 3510 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3511 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3512 OnePerLine); 3513 verifyFormat("MyClass::MyClass(int var)\n" 3514 " : some_var_(var), // 4 space indent\n" 3515 " some_other_var_(var + 1) { // lined up\n" 3516 "}", 3517 OnePerLine); 3518 verifyFormat("Constructor()\n" 3519 " : aaaaa(aaaaaa),\n" 3520 " aaaaa(aaaaaa),\n" 3521 " aaaaa(aaaaaa),\n" 3522 " aaaaa(aaaaaa),\n" 3523 " aaaaa(aaaaaa) {}", 3524 OnePerLine); 3525 verifyFormat("Constructor()\n" 3526 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 3527 " aaaaaaaaaaaaaaaaaaaaaa) {}", 3528 OnePerLine); 3529 OnePerLine.BinPackParameters = false; 3530 verifyFormat( 3531 "Constructor()\n" 3532 " : aaaaaaaaaaaaaaaaaaaaaaaa(\n" 3533 " aaaaaaaaaaa().aaa(),\n" 3534 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3535 OnePerLine); 3536 OnePerLine.ColumnLimit = 60; 3537 verifyFormat("Constructor()\n" 3538 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 3539 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 3540 OnePerLine); 3541 3542 EXPECT_EQ("Constructor()\n" 3543 " : // Comment forcing unwanted break.\n" 3544 " aaaa(aaaa) {}", 3545 format("Constructor() :\n" 3546 " // Comment forcing unwanted break.\n" 3547 " aaaa(aaaa) {}")); 3548 } 3549 3550 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) { 3551 FormatStyle Style = getLLVMStyle(); 3552 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 3553 3554 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 3555 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}", 3556 getStyleWithColumns(Style, 45)); 3557 verifyFormat("Constructor() :\n" 3558 " Initializer(FitsOnTheLine) {}", 3559 getStyleWithColumns(Style, 44)); 3560 verifyFormat("Constructor() :\n" 3561 " Initializer(FitsOnTheLine) {}", 3562 getStyleWithColumns(Style, 43)); 3563 3564 verifyFormat("template <typename T>\n" 3565 "Constructor() : Initializer(FitsOnTheLine) {}", 3566 getStyleWithColumns(Style, 50)); 3567 3568 verifyFormat( 3569 "SomeClass::Constructor() :\n" 3570 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 3571 Style); 3572 3573 verifyFormat( 3574 "SomeClass::Constructor() :\n" 3575 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3576 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3577 Style); 3578 verifyFormat( 3579 "SomeClass::Constructor() :\n" 3580 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3581 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 3582 Style); 3583 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3584 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 3585 " aaaaaaaaaa(aaaaaa) {}", 3586 Style); 3587 3588 verifyFormat("Constructor() :\n" 3589 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3590 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3591 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3592 " aaaaaaaaaaaaaaaaaaaaaaa() {}", 3593 Style); 3594 3595 verifyFormat("Constructor() :\n" 3596 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3597 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3598 Style); 3599 3600 verifyFormat("Constructor(int Parameter = 0) :\n" 3601 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 3602 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}", 3603 Style); 3604 verifyFormat("Constructor() :\n" 3605 " aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 3606 "}", 3607 getStyleWithColumns(Style, 60)); 3608 verifyFormat("Constructor() :\n" 3609 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3610 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}", 3611 Style); 3612 3613 // Here a line could be saved by splitting the second initializer onto two 3614 // lines, but that is not desirable. 3615 verifyFormat("Constructor() :\n" 3616 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 3617 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 3618 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3619 Style); 3620 3621 FormatStyle OnePerLine = Style; 3622 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 3623 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false; 3624 verifyFormat("SomeClass::Constructor() :\n" 3625 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3626 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3627 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3628 OnePerLine); 3629 verifyFormat("SomeClass::Constructor() :\n" 3630 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 3631 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3632 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3633 OnePerLine); 3634 verifyFormat("MyClass::MyClass(int var) :\n" 3635 " some_var_(var), // 4 space indent\n" 3636 " some_other_var_(var + 1) { // lined up\n" 3637 "}", 3638 OnePerLine); 3639 verifyFormat("Constructor() :\n" 3640 " aaaaa(aaaaaa),\n" 3641 " aaaaa(aaaaaa),\n" 3642 " aaaaa(aaaaaa),\n" 3643 " aaaaa(aaaaaa),\n" 3644 " aaaaa(aaaaaa) {}", 3645 OnePerLine); 3646 verifyFormat("Constructor() :\n" 3647 " aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 3648 " aaaaaaaaaaaaaaaaaaaaaa) {}", 3649 OnePerLine); 3650 OnePerLine.BinPackParameters = false; 3651 verifyFormat( 3652 "Constructor() :\n" 3653 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 3654 " aaaaaaaaaaa().aaa(),\n" 3655 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3656 OnePerLine); 3657 OnePerLine.ColumnLimit = 60; 3658 verifyFormat("Constructor() :\n" 3659 " aaaaaaaaaaaaaaaaaaaa(a),\n" 3660 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 3661 OnePerLine); 3662 3663 EXPECT_EQ("Constructor() :\n" 3664 " // Comment forcing unwanted break.\n" 3665 " aaaa(aaaa) {}", 3666 format("Constructor() :\n" 3667 " // Comment forcing unwanted break.\n" 3668 " aaaa(aaaa) {}", 3669 Style)); 3670 3671 Style.ColumnLimit = 0; 3672 verifyFormat("SomeClass::Constructor() :\n" 3673 " a(a) {}", 3674 Style); 3675 verifyFormat("SomeClass::Constructor() noexcept :\n" 3676 " a(a) {}", 3677 Style); 3678 verifyFormat("SomeClass::Constructor() :\n" 3679 " a(a), b(b), c(c) {}", 3680 Style); 3681 verifyFormat("SomeClass::Constructor() :\n" 3682 " a(a) {\n" 3683 " foo();\n" 3684 " bar();\n" 3685 "}", 3686 Style); 3687 3688 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 3689 verifyFormat("SomeClass::Constructor() :\n" 3690 " a(a), b(b), c(c) {\n" 3691 "}", 3692 Style); 3693 verifyFormat("SomeClass::Constructor() :\n" 3694 " a(a) {\n" 3695 "}", 3696 Style); 3697 3698 Style.ColumnLimit = 80; 3699 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 3700 Style.ConstructorInitializerIndentWidth = 2; 3701 verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", 3702 Style); 3703 verifyFormat("SomeClass::Constructor() :\n" 3704 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3705 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}", 3706 Style); 3707 } 3708 3709 #ifndef EXPENSIVE_CHECKS 3710 // Expensive checks enables libstdc++ checking which includes validating the 3711 // state of ranges used in std::priority_queue - this blows out the 3712 // runtime/scalability of the function and makes this test unacceptably slow. 3713 TEST_F(FormatTest, MemoizationTests) { 3714 // This breaks if the memoization lookup does not take \c Indent and 3715 // \c LastSpace into account. 3716 verifyFormat( 3717 "extern CFRunLoopTimerRef\n" 3718 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n" 3719 " CFTimeInterval interval, CFOptionFlags flags,\n" 3720 " CFIndex order, CFRunLoopTimerCallBack callout,\n" 3721 " CFRunLoopTimerContext *context) {}"); 3722 3723 // Deep nesting somewhat works around our memoization. 3724 verifyFormat( 3725 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3726 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3727 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3728 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3729 " aaaaa())))))))))))))))))))))))))))))))))))))));", 3730 getLLVMStyleWithColumns(65)); 3731 verifyFormat( 3732 "aaaaa(\n" 3733 " aaaaa,\n" 3734 " aaaaa(\n" 3735 " aaaaa,\n" 3736 " aaaaa(\n" 3737 " aaaaa,\n" 3738 " aaaaa(\n" 3739 " aaaaa,\n" 3740 " aaaaa(\n" 3741 " aaaaa,\n" 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))))))))))));", 3757 getLLVMStyleWithColumns(65)); 3758 verifyFormat( 3759 "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" 3760 " a),\n" 3761 " a),\n" 3762 " a),\n" 3763 " a),\n" 3764 " a),\n" 3765 " a),\n" 3766 " a),\n" 3767 " a),\n" 3768 " a),\n" 3769 " a),\n" 3770 " a),\n" 3771 " a),\n" 3772 " a),\n" 3773 " a),\n" 3774 " a),\n" 3775 " a),\n" 3776 " a)", 3777 getLLVMStyleWithColumns(65)); 3778 3779 // This test takes VERY long when memoization is broken. 3780 FormatStyle OnePerLine = getLLVMStyle(); 3781 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 3782 OnePerLine.BinPackParameters = false; 3783 std::string input = "Constructor()\n" 3784 " : aaaa(a,\n"; 3785 for (unsigned i = 0, e = 80; i != e; ++i) { 3786 input += " a,\n"; 3787 } 3788 input += " a) {}"; 3789 verifyFormat(input, OnePerLine); 3790 } 3791 #endif 3792 3793 TEST_F(FormatTest, BreaksAsHighAsPossible) { 3794 verifyFormat( 3795 "void f() {\n" 3796 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" 3797 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" 3798 " f();\n" 3799 "}"); 3800 verifyFormat("if (Intervals[i].getRange().getFirst() <\n" 3801 " Intervals[i - 1].getRange().getLast()) {\n}"); 3802 } 3803 3804 TEST_F(FormatTest, BreaksFunctionDeclarations) { 3805 // Principially, we break function declarations in a certain order: 3806 // 1) break amongst arguments. 3807 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n" 3808 " Cccccccccccccc cccccccccccccc);"); 3809 verifyFormat("template <class TemplateIt>\n" 3810 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n" 3811 " TemplateIt *stop) {}"); 3812 3813 // 2) break after return type. 3814 verifyFormat( 3815 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3816 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);", 3817 getGoogleStyle()); 3818 3819 // 3) break after (. 3820 verifyFormat( 3821 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n" 3822 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);", 3823 getGoogleStyle()); 3824 3825 // 4) break before after nested name specifiers. 3826 verifyFormat( 3827 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3828 "SomeClasssssssssssssssssssssssssssssssssssssss::\n" 3829 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);", 3830 getGoogleStyle()); 3831 3832 // However, there are exceptions, if a sufficient amount of lines can be 3833 // saved. 3834 // FIXME: The precise cut-offs wrt. the number of saved lines might need some 3835 // more adjusting. 3836 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 3837 " Cccccccccccccc cccccccccc,\n" 3838 " Cccccccccccccc cccccccccc,\n" 3839 " Cccccccccccccc cccccccccc,\n" 3840 " Cccccccccccccc cccccccccc);"); 3841 verifyFormat( 3842 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3843 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3844 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3845 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);", 3846 getGoogleStyle()); 3847 verifyFormat( 3848 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 3849 " Cccccccccccccc cccccccccc,\n" 3850 " Cccccccccccccc cccccccccc,\n" 3851 " Cccccccccccccc cccccccccc,\n" 3852 " Cccccccccccccc cccccccccc,\n" 3853 " Cccccccccccccc cccccccccc,\n" 3854 " Cccccccccccccc cccccccccc);"); 3855 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 3856 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3857 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3858 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3859 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); 3860 3861 // Break after multi-line parameters. 3862 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3863 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3864 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3865 " bbbb bbbb);"); 3866 verifyFormat("void SomeLoooooooooooongFunction(\n" 3867 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 3868 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3869 " int bbbbbbbbbbbbb);"); 3870 3871 // Treat overloaded operators like other functions. 3872 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3873 "operator>(const SomeLoooooooooooooooooooooooooogType &other);"); 3874 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3875 "operator>>(const SomeLooooooooooooooooooooooooogType &other);"); 3876 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3877 "operator<<(const SomeLooooooooooooooooooooooooogType &other);"); 3878 verifyGoogleFormat( 3879 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n" 3880 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 3881 verifyGoogleFormat( 3882 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n" 3883 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 3884 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3885 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 3886 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n" 3887 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 3888 verifyGoogleFormat( 3889 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n" 3890 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3891 " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}"); 3892 verifyGoogleFormat( 3893 "template <typename T>\n" 3894 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3895 "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n" 3896 " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);"); 3897 3898 FormatStyle Style = getLLVMStyle(); 3899 Style.PointerAlignment = FormatStyle::PAS_Left; 3900 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3901 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}", 3902 Style); 3903 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n" 3904 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3905 Style); 3906 } 3907 3908 TEST_F(FormatTest, TrailingReturnType) { 3909 verifyFormat("auto foo() -> int;\n"); 3910 verifyFormat("struct S {\n" 3911 " auto bar() const -> int;\n" 3912 "};"); 3913 verifyFormat("template <size_t Order, typename T>\n" 3914 "auto load_img(const std::string &filename)\n" 3915 " -> alias::tensor<Order, T, mem::tag::cpu> {}"); 3916 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n" 3917 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}"); 3918 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}"); 3919 verifyFormat("template <typename T>\n" 3920 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n" 3921 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());"); 3922 3923 // Not trailing return types. 3924 verifyFormat("void f() { auto a = b->c(); }"); 3925 } 3926 3927 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { 3928 // Avoid breaking before trailing 'const' or other trailing annotations, if 3929 // they are not function-like. 3930 FormatStyle Style = getGoogleStyle(); 3931 Style.ColumnLimit = 47; 3932 verifyFormat("void someLongFunction(\n" 3933 " int someLoooooooooooooongParameter) const {\n}", 3934 getLLVMStyleWithColumns(47)); 3935 verifyFormat("LoooooongReturnType\n" 3936 "someLoooooooongFunction() const {}", 3937 getLLVMStyleWithColumns(47)); 3938 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n" 3939 " const {}", 3940 Style); 3941 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3942 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;"); 3943 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3944 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;"); 3945 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3946 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;"); 3947 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n" 3948 " aaaaaaaaaaa aaaaa) const override;"); 3949 verifyGoogleFormat( 3950 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 3951 " const override;"); 3952 3953 // Even if the first parameter has to be wrapped. 3954 verifyFormat("void someLongFunction(\n" 3955 " int someLongParameter) const {}", 3956 getLLVMStyleWithColumns(46)); 3957 verifyFormat("void someLongFunction(\n" 3958 " int someLongParameter) const {}", 3959 Style); 3960 verifyFormat("void someLongFunction(\n" 3961 " int someLongParameter) override {}", 3962 Style); 3963 verifyFormat("void someLongFunction(\n" 3964 " int someLongParameter) OVERRIDE {}", 3965 Style); 3966 verifyFormat("void someLongFunction(\n" 3967 " int someLongParameter) final {}", 3968 Style); 3969 verifyFormat("void someLongFunction(\n" 3970 " int someLongParameter) FINAL {}", 3971 Style); 3972 verifyFormat("void someLongFunction(\n" 3973 " int parameter) const override {}", 3974 Style); 3975 3976 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 3977 verifyFormat("void someLongFunction(\n" 3978 " int someLongParameter) const\n" 3979 "{\n" 3980 "}", 3981 Style); 3982 3983 // Unless these are unknown annotations. 3984 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n" 3985 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3986 " LONG_AND_UGLY_ANNOTATION;"); 3987 3988 // Breaking before function-like trailing annotations is fine to keep them 3989 // close to their arguments. 3990 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3991 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 3992 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 3993 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 3994 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 3995 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}"); 3996 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n" 3997 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);"); 3998 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});"); 3999 4000 verifyFormat( 4001 "void aaaaaaaaaaaaaaaaaa()\n" 4002 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n" 4003 " aaaaaaaaaaaaaaaaaaaaaaaaa));"); 4004 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4005 " __attribute__((unused));"); 4006 verifyGoogleFormat( 4007 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4008 " GUARDED_BY(aaaaaaaaaaaa);"); 4009 verifyGoogleFormat( 4010 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4011 " GUARDED_BY(aaaaaaaaaaaa);"); 4012 verifyGoogleFormat( 4013 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 4014 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4015 verifyGoogleFormat( 4016 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 4017 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4018 } 4019 4020 TEST_F(FormatTest, FunctionAnnotations) { 4021 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 4022 "int OldFunction(const string ¶meter) {}"); 4023 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 4024 "string OldFunction(const string ¶meter) {}"); 4025 verifyFormat("template <typename T>\n" 4026 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 4027 "string OldFunction(const string ¶meter) {}"); 4028 4029 // Not function annotations. 4030 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4031 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); 4032 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n" 4033 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}"); 4034 verifyFormat("MACRO(abc).function() // wrap\n" 4035 " << abc;"); 4036 verifyFormat("MACRO(abc)->function() // wrap\n" 4037 " << abc;"); 4038 verifyFormat("MACRO(abc)::function() // wrap\n" 4039 " << abc;"); 4040 } 4041 4042 TEST_F(FormatTest, BreaksDesireably) { 4043 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 4044 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 4045 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); 4046 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4047 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 4048 "}"); 4049 4050 verifyFormat( 4051 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4052 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 4053 4054 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4055 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4056 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4057 4058 verifyFormat( 4059 "aaaaaaaa(aaaaaaaaaaaaa,\n" 4060 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4061 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 4062 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4063 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 4064 4065 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4066 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4067 4068 verifyFormat( 4069 "void f() {\n" 4070 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 4071 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 4072 "}"); 4073 verifyFormat( 4074 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4075 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4076 verifyFormat( 4077 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4078 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4079 verifyFormat( 4080 "aaaaaa(aaa,\n" 4081 " new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4082 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4083 " aaaa);"); 4084 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4085 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4086 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4087 4088 // Indent consistently independent of call expression and unary operator. 4089 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 4090 " dddddddddddddddddddddddddddddd));"); 4091 verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 4092 " dddddddddddddddddddddddddddddd));"); 4093 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n" 4094 " dddddddddddddddddddddddddddddd));"); 4095 4096 // This test case breaks on an incorrect memoization, i.e. an optimization not 4097 // taking into account the StopAt value. 4098 verifyFormat( 4099 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4100 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4101 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4102 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4103 4104 verifyFormat("{\n {\n {\n" 4105 " Annotation.SpaceRequiredBefore =\n" 4106 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" 4107 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" 4108 " }\n }\n}"); 4109 4110 // Break on an outer level if there was a break on an inner level. 4111 EXPECT_EQ("f(g(h(a, // comment\n" 4112 " b, c),\n" 4113 " d, e),\n" 4114 " x, y);", 4115 format("f(g(h(a, // comment\n" 4116 " b, c), d, e), x, y);")); 4117 4118 // Prefer breaking similar line breaks. 4119 verifyFormat( 4120 "const int kTrackingOptions = NSTrackingMouseMoved |\n" 4121 " NSTrackingMouseEnteredAndExited |\n" 4122 " NSTrackingActiveAlways;"); 4123 } 4124 4125 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) { 4126 FormatStyle NoBinPacking = getGoogleStyle(); 4127 NoBinPacking.BinPackParameters = false; 4128 NoBinPacking.BinPackArguments = true; 4129 verifyFormat("void f() {\n" 4130 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n" 4131 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 4132 "}", 4133 NoBinPacking); 4134 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n" 4135 " int aaaaaaaaaaaaaaaaaaaa,\n" 4136 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4137 NoBinPacking); 4138 4139 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 4140 verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4141 " vector<int> bbbbbbbbbbbbbbb);", 4142 NoBinPacking); 4143 // FIXME: This behavior difference is probably not wanted. However, currently 4144 // we cannot distinguish BreakBeforeParameter being set because of the wrapped 4145 // template arguments from BreakBeforeParameter being set because of the 4146 // one-per-line formatting. 4147 verifyFormat( 4148 "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n" 4149 " aaaaaaaaaa> aaaaaaaaaa);", 4150 NoBinPacking); 4151 verifyFormat( 4152 "void fffffffffff(\n" 4153 " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n" 4154 " aaaaaaaaaa);"); 4155 } 4156 4157 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { 4158 FormatStyle NoBinPacking = getGoogleStyle(); 4159 NoBinPacking.BinPackParameters = false; 4160 NoBinPacking.BinPackArguments = false; 4161 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" 4162 " aaaaaaaaaaaaaaaaaaaa,\n" 4163 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);", 4164 NoBinPacking); 4165 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n" 4166 " aaaaaaaaaaaaa,\n" 4167 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));", 4168 NoBinPacking); 4169 verifyFormat( 4170 "aaaaaaaa(aaaaaaaaaaaaa,\n" 4171 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4172 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 4173 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4174 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));", 4175 NoBinPacking); 4176 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 4177 " .aaaaaaaaaaaaaaaaaa();", 4178 NoBinPacking); 4179 verifyFormat("void f() {\n" 4180 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4181 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n" 4182 "}", 4183 NoBinPacking); 4184 4185 verifyFormat( 4186 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4187 " aaaaaaaaaaaa,\n" 4188 " aaaaaaaaaaaa);", 4189 NoBinPacking); 4190 verifyFormat( 4191 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" 4192 " ddddddddddddddddddddddddddddd),\n" 4193 " test);", 4194 NoBinPacking); 4195 4196 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" 4197 " aaaaaaaaaaaaaaaaaaaaaaa,\n" 4198 " aaaaaaaaaaaaaaaaaaaaaaa>\n" 4199 " aaaaaaaaaaaaaaaaaa;", 4200 NoBinPacking); 4201 verifyFormat("a(\"a\"\n" 4202 " \"a\",\n" 4203 " a);"); 4204 4205 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 4206 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" 4207 " aaaaaaaaa,\n" 4208 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4209 NoBinPacking); 4210 verifyFormat( 4211 "void f() {\n" 4212 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 4213 " .aaaaaaa();\n" 4214 "}", 4215 NoBinPacking); 4216 verifyFormat( 4217 "template <class SomeType, class SomeOtherType>\n" 4218 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}", 4219 NoBinPacking); 4220 } 4221 4222 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) { 4223 FormatStyle Style = getLLVMStyleWithColumns(15); 4224 Style.ExperimentalAutoDetectBinPacking = true; 4225 EXPECT_EQ("aaa(aaaa,\n" 4226 " aaaa,\n" 4227 " aaaa);\n" 4228 "aaa(aaaa,\n" 4229 " aaaa,\n" 4230 " aaaa);", 4231 format("aaa(aaaa,\n" // one-per-line 4232 " aaaa,\n" 4233 " aaaa );\n" 4234 "aaa(aaaa, aaaa, aaaa);", // inconclusive 4235 Style)); 4236 EXPECT_EQ("aaa(aaaa, aaaa,\n" 4237 " aaaa);\n" 4238 "aaa(aaaa, aaaa,\n" 4239 " aaaa);", 4240 format("aaa(aaaa, aaaa,\n" // bin-packed 4241 " aaaa );\n" 4242 "aaa(aaaa, aaaa, aaaa);", // inconclusive 4243 Style)); 4244 } 4245 4246 TEST_F(FormatTest, FormatsBuilderPattern) { 4247 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n" 4248 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" 4249 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n" 4250 " .StartsWith(\".init\", ORDER_INIT)\n" 4251 " .StartsWith(\".fini\", ORDER_FINI)\n" 4252 " .StartsWith(\".hash\", ORDER_HASH)\n" 4253 " .Default(ORDER_TEXT);\n"); 4254 4255 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" 4256 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); 4257 verifyFormat( 4258 "aaaaaaa->aaaaaaa\n" 4259 " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4260 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4261 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 4262 verifyFormat( 4263 "aaaaaaa->aaaaaaa\n" 4264 " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4265 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 4266 verifyFormat( 4267 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" 4268 " aaaaaaaaaaaaaa);"); 4269 verifyFormat( 4270 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n" 4271 " aaaaaa->aaaaaaaaaaaa()\n" 4272 " ->aaaaaaaaaaaaaaaa(\n" 4273 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4274 " ->aaaaaaaaaaaaaaaaa();"); 4275 verifyGoogleFormat( 4276 "void f() {\n" 4277 " someo->Add((new util::filetools::Handler(dir))\n" 4278 " ->OnEvent1(NewPermanentCallback(\n" 4279 " this, &HandlerHolderClass::EventHandlerCBA))\n" 4280 " ->OnEvent2(NewPermanentCallback(\n" 4281 " this, &HandlerHolderClass::EventHandlerCBB))\n" 4282 " ->OnEvent3(NewPermanentCallback(\n" 4283 " this, &HandlerHolderClass::EventHandlerCBC))\n" 4284 " ->OnEvent5(NewPermanentCallback(\n" 4285 " this, &HandlerHolderClass::EventHandlerCBD))\n" 4286 " ->OnEvent6(NewPermanentCallback(\n" 4287 " this, &HandlerHolderClass::EventHandlerCBE)));\n" 4288 "}"); 4289 4290 verifyFormat( 4291 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();"); 4292 verifyFormat("aaaaaaaaaaaaaaa()\n" 4293 " .aaaaaaaaaaaaaaa()\n" 4294 " .aaaaaaaaaaaaaaa()\n" 4295 " .aaaaaaaaaaaaaaa()\n" 4296 " .aaaaaaaaaaaaaaa();"); 4297 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4298 " .aaaaaaaaaaaaaaa()\n" 4299 " .aaaaaaaaaaaaaaa()\n" 4300 " .aaaaaaaaaaaaaaa();"); 4301 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4302 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4303 " .aaaaaaaaaaaaaaa();"); 4304 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n" 4305 " ->aaaaaaaaaaaaaae(0)\n" 4306 " ->aaaaaaaaaaaaaaa();"); 4307 4308 // Don't linewrap after very short segments. 4309 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4310 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4311 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4312 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4313 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4314 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4315 verifyFormat("aaa()\n" 4316 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4317 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4318 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4319 4320 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 4321 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4322 " .has<bbbbbbbbbbbbbbbbbbbbb>();"); 4323 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 4324 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 4325 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();"); 4326 4327 // Prefer not to break after empty parentheses. 4328 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n" 4329 " First->LastNewlineOffset);"); 4330 4331 // Prefer not to create "hanging" indents. 4332 verifyFormat( 4333 "return !soooooooooooooome_map\n" 4334 " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4335 " .second;"); 4336 verifyFormat( 4337 "return aaaaaaaaaaaaaaaa\n" 4338 " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n" 4339 " .aaaa(aaaaaaaaaaaaaa);"); 4340 // No hanging indent here. 4341 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n" 4342 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4343 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n" 4344 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4345 verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 4346 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4347 getLLVMStyleWithColumns(60)); 4348 verifyFormat("aaaaaaaaaaaaaaaaaa\n" 4349 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 4350 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4351 getLLVMStyleWithColumns(59)); 4352 verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4353 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4354 " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4355 } 4356 4357 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { 4358 verifyFormat( 4359 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4360 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); 4361 verifyFormat( 4362 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n" 4363 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}"); 4364 4365 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 4366 " ccccccccccccccccccccccccc) {\n}"); 4367 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n" 4368 " ccccccccccccccccccccccccc) {\n}"); 4369 4370 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 4371 " ccccccccccccccccccccccccc) {\n}"); 4372 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n" 4373 " ccccccccccccccccccccccccc) {\n}"); 4374 4375 verifyFormat( 4376 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" 4377 " ccccccccccccccccccccccccc) {\n}"); 4378 verifyFormat( 4379 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n" 4380 " ccccccccccccccccccccccccc) {\n}"); 4381 4382 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" 4383 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" 4384 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" 4385 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 4386 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n" 4387 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n" 4388 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n" 4389 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 4390 4391 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" 4392 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" 4393 " aaaaaaaaaaaaaaa != aa) {\n}"); 4394 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n" 4395 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n" 4396 " aaaaaaaaaaaaaaa != aa) {\n}"); 4397 } 4398 4399 TEST_F(FormatTest, BreaksAfterAssignments) { 4400 verifyFormat( 4401 "unsigned Cost =\n" 4402 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" 4403 " SI->getPointerAddressSpaceee());\n"); 4404 verifyFormat( 4405 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" 4406 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); 4407 4408 verifyFormat( 4409 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n" 4410 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); 4411 verifyFormat("unsigned OriginalStartColumn =\n" 4412 " SourceMgr.getSpellingColumnNumber(\n" 4413 " Current.FormatTok.getStartOfNonWhitespace()) -\n" 4414 " 1;"); 4415 } 4416 4417 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) { 4418 FormatStyle Style = getLLVMStyle(); 4419 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 4420 " bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;", 4421 Style); 4422 4423 Style.PenaltyBreakAssignment = 20; 4424 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 4425 " cccccccccccccccccccccccccc;", 4426 Style); 4427 } 4428 4429 TEST_F(FormatTest, AlignsAfterAssignments) { 4430 verifyFormat( 4431 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4432 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4433 verifyFormat( 4434 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4435 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4436 verifyFormat( 4437 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4438 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4439 verifyFormat( 4440 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4441 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 4442 verifyFormat( 4443 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" 4444 " aaaaaaaaaaaaaaaaaaaaaaaa +\n" 4445 " aaaaaaaaaaaaaaaaaaaaaaaa;"); 4446 } 4447 4448 TEST_F(FormatTest, AlignsAfterReturn) { 4449 verifyFormat( 4450 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4451 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4452 verifyFormat( 4453 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4454 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 4455 verifyFormat( 4456 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 4457 " aaaaaaaaaaaaaaaaaaaaaa();"); 4458 verifyFormat( 4459 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 4460 " aaaaaaaaaaaaaaaaaaaaaa());"); 4461 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4462 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4463 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4464 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n" 4465 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4466 verifyFormat("return\n" 4467 " // true if code is one of a or b.\n" 4468 " code == a || code == b;"); 4469 } 4470 4471 TEST_F(FormatTest, AlignsAfterOpenBracket) { 4472 verifyFormat( 4473 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 4474 " aaaaaaaaa aaaaaaa) {}"); 4475 verifyFormat( 4476 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 4477 " aaaaaaaaaaa aaaaaaaaa);"); 4478 verifyFormat( 4479 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 4480 " aaaaaaaaaaaaaaaaaaaaa));"); 4481 FormatStyle Style = getLLVMStyle(); 4482 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4483 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4484 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}", 4485 Style); 4486 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 4487 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);", 4488 Style); 4489 verifyFormat("SomeLongVariableName->someFunction(\n" 4490 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));", 4491 Style); 4492 verifyFormat( 4493 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 4494 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4495 Style); 4496 verifyFormat( 4497 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 4498 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4499 Style); 4500 verifyFormat( 4501 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 4502 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 4503 Style); 4504 4505 verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n" 4506 " ccccccc(aaaaaaaaaaaaaaaaa, //\n" 4507 " b));", 4508 Style); 4509 4510 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 4511 Style.BinPackArguments = false; 4512 Style.BinPackParameters = false; 4513 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4514 " aaaaaaaaaaa aaaaaaaa,\n" 4515 " aaaaaaaaa aaaaaaa,\n" 4516 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4517 Style); 4518 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 4519 " aaaaaaaaaaa aaaaaaaaa,\n" 4520 " aaaaaaaaaaa aaaaaaaaa,\n" 4521 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4522 Style); 4523 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n" 4524 " aaaaaaaaaaaaaaa,\n" 4525 " aaaaaaaaaaaaaaaaaaaaa,\n" 4526 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 4527 Style); 4528 verifyFormat( 4529 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n" 4530 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 4531 Style); 4532 verifyFormat( 4533 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n" 4534 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 4535 Style); 4536 verifyFormat( 4537 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 4538 " aaaaaaaaaaaaaaaaaaaaa(\n" 4539 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n" 4540 " aaaaaaaaaaaaaaaa);", 4541 Style); 4542 verifyFormat( 4543 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 4544 " aaaaaaaaaaaaaaaaaaaaa(\n" 4545 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n" 4546 " aaaaaaaaaaaaaaaa);", 4547 Style); 4548 } 4549 4550 TEST_F(FormatTest, ParenthesesAndOperandAlignment) { 4551 FormatStyle Style = getLLVMStyleWithColumns(40); 4552 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4553 " bbbbbbbbbbbbbbbbbbbbbb);", 4554 Style); 4555 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 4556 Style.AlignOperands = false; 4557 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4558 " bbbbbbbbbbbbbbbbbbbbbb);", 4559 Style); 4560 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4561 Style.AlignOperands = true; 4562 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4563 " bbbbbbbbbbbbbbbbbbbbbb);", 4564 Style); 4565 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4566 Style.AlignOperands = false; 4567 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4568 " bbbbbbbbbbbbbbbbbbbbbb);", 4569 Style); 4570 } 4571 4572 TEST_F(FormatTest, BreaksConditionalExpressions) { 4573 verifyFormat( 4574 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4575 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4576 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4577 verifyFormat( 4578 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 4579 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4580 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4581 verifyFormat( 4582 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4583 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4584 verifyFormat( 4585 "aaaa(aaaaaaaaa, aaaaaaaaa,\n" 4586 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4587 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4588 verifyFormat( 4589 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" 4590 " : aaaaaaaaaaaaa);"); 4591 verifyFormat( 4592 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4593 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4594 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4595 " aaaaaaaaaaaaa);"); 4596 verifyFormat( 4597 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4598 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4599 " aaaaaaaaaaaaa);"); 4600 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4601 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4602 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4603 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4604 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4605 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4606 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4607 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4608 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4609 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4610 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4611 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4612 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4613 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4614 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4615 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4616 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4617 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4618 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4619 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4620 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 4621 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4622 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4623 " : aaaaaaaaaaaaaaaa;"); 4624 verifyFormat( 4625 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4626 " ? aaaaaaaaaaaaaaa\n" 4627 " : aaaaaaaaaaaaaaa;"); 4628 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 4629 " aaaaaaaaa\n" 4630 " ? b\n" 4631 " : c);"); 4632 verifyFormat("return aaaa == bbbb\n" 4633 " // comment\n" 4634 " ? aaaa\n" 4635 " : bbbb;"); 4636 verifyFormat("unsigned Indent =\n" 4637 " format(TheLine.First,\n" 4638 " IndentForLevel[TheLine.Level] >= 0\n" 4639 " ? IndentForLevel[TheLine.Level]\n" 4640 " : TheLine * 2,\n" 4641 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 4642 getLLVMStyleWithColumns(60)); 4643 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 4644 " ? aaaaaaaaaaaaaaa\n" 4645 " : bbbbbbbbbbbbbbb //\n" 4646 " ? ccccccccccccccc\n" 4647 " : ddddddddddddddd;"); 4648 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 4649 " ? aaaaaaaaaaaaaaa\n" 4650 " : (bbbbbbbbbbbbbbb //\n" 4651 " ? ccccccccccccccc\n" 4652 " : ddddddddddddddd);"); 4653 verifyFormat( 4654 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4655 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4656 " aaaaaaaaaaaaaaaaaaaaa +\n" 4657 " aaaaaaaaaaaaaaaaaaaaa\n" 4658 " : aaaaaaaaaa;"); 4659 verifyFormat( 4660 "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4661 " : aaaaaaaaaaaaaaaaaaaaaa\n" 4662 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4663 4664 FormatStyle NoBinPacking = getLLVMStyle(); 4665 NoBinPacking.BinPackArguments = false; 4666 verifyFormat( 4667 "void f() {\n" 4668 " g(aaa,\n" 4669 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 4670 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4671 " ? aaaaaaaaaaaaaaa\n" 4672 " : aaaaaaaaaaaaaaa);\n" 4673 "}", 4674 NoBinPacking); 4675 verifyFormat( 4676 "void f() {\n" 4677 " g(aaa,\n" 4678 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 4679 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4680 " ?: aaaaaaaaaaaaaaa);\n" 4681 "}", 4682 NoBinPacking); 4683 4684 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n" 4685 " // comment.\n" 4686 " ccccccccccccccccccccccccccccccccccccccc\n" 4687 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4688 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);"); 4689 4690 // Assignments in conditional expressions. Apparently not uncommon :-(. 4691 verifyFormat("return a != b\n" 4692 " // comment\n" 4693 " ? a = b\n" 4694 " : a = b;"); 4695 verifyFormat("return a != b\n" 4696 " // comment\n" 4697 " ? a = a != b\n" 4698 " // comment\n" 4699 " ? a = b\n" 4700 " : a\n" 4701 " : a;\n"); 4702 verifyFormat("return a != b\n" 4703 " // comment\n" 4704 " ? a\n" 4705 " : a = a != b\n" 4706 " // comment\n" 4707 " ? a = b\n" 4708 " : a;"); 4709 } 4710 4711 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) { 4712 FormatStyle Style = getLLVMStyle(); 4713 Style.BreakBeforeTernaryOperators = false; 4714 Style.ColumnLimit = 70; 4715 verifyFormat( 4716 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4717 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4718 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4719 Style); 4720 verifyFormat( 4721 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 4722 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4723 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4724 Style); 4725 verifyFormat( 4726 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4727 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4728 Style); 4729 verifyFormat( 4730 "aaaa(aaaaaaaa, aaaaaaaaaa,\n" 4731 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4732 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4733 Style); 4734 verifyFormat( 4735 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n" 4736 " aaaaaaaaaaaaa);", 4737 Style); 4738 verifyFormat( 4739 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4740 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4741 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4742 " aaaaaaaaaaaaa);", 4743 Style); 4744 verifyFormat( 4745 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4746 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4747 " aaaaaaaaaaaaa);", 4748 Style); 4749 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4750 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4751 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 4752 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4753 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4754 Style); 4755 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4756 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4757 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4758 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 4759 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4760 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4761 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4762 Style); 4763 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4764 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n" 4765 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4766 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4767 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4768 Style); 4769 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4770 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4771 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4772 Style); 4773 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 4774 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4775 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4776 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4777 Style); 4778 verifyFormat( 4779 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4780 " aaaaaaaaaaaaaaa :\n" 4781 " aaaaaaaaaaaaaaa;", 4782 Style); 4783 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 4784 " aaaaaaaaa ?\n" 4785 " b :\n" 4786 " c);", 4787 Style); 4788 verifyFormat("unsigned Indent =\n" 4789 " format(TheLine.First,\n" 4790 " IndentForLevel[TheLine.Level] >= 0 ?\n" 4791 " IndentForLevel[TheLine.Level] :\n" 4792 " TheLine * 2,\n" 4793 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 4794 Style); 4795 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 4796 " aaaaaaaaaaaaaaa :\n" 4797 " bbbbbbbbbbbbbbb ? //\n" 4798 " ccccccccccccccc :\n" 4799 " ddddddddddddddd;", 4800 Style); 4801 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 4802 " aaaaaaaaaaaaaaa :\n" 4803 " (bbbbbbbbbbbbbbb ? //\n" 4804 " ccccccccccccccc :\n" 4805 " ddddddddddddddd);", 4806 Style); 4807 verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4808 " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n" 4809 " ccccccccccccccccccccccccccc;", 4810 Style); 4811 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4812 " aaaaa :\n" 4813 " bbbbbbbbbbbbbbb + cccccccccccccccc;", 4814 Style); 4815 } 4816 4817 TEST_F(FormatTest, DeclarationsOfMultipleVariables) { 4818 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" 4819 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); 4820 verifyFormat("bool a = true, b = false;"); 4821 4822 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" 4823 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" 4824 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" 4825 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); 4826 verifyFormat( 4827 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 4828 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n" 4829 " d = e && f;"); 4830 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n" 4831 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;"); 4832 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 4833 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;"); 4834 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n" 4835 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;"); 4836 4837 FormatStyle Style = getGoogleStyle(); 4838 Style.PointerAlignment = FormatStyle::PAS_Left; 4839 Style.DerivePointerAlignment = false; 4840 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4841 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" 4842 " *b = bbbbbbbbbbbbbbbbbbb;", 4843 Style); 4844 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 4845 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;", 4846 Style); 4847 verifyFormat("vector<int*> a, b;", Style); 4848 verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style); 4849 } 4850 4851 TEST_F(FormatTest, ConditionalExpressionsInBrackets) { 4852 verifyFormat("arr[foo ? bar : baz];"); 4853 verifyFormat("f()[foo ? bar : baz];"); 4854 verifyFormat("(a + b)[foo ? bar : baz];"); 4855 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); 4856 } 4857 4858 TEST_F(FormatTest, AlignsStringLiterals) { 4859 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" 4860 " \"short literal\");"); 4861 verifyFormat( 4862 "looooooooooooooooooooooooongFunction(\n" 4863 " \"short literal\"\n" 4864 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); 4865 verifyFormat("someFunction(\"Always break between multi-line\"\n" 4866 " \" string literals\",\n" 4867 " and, other, parameters);"); 4868 EXPECT_EQ("fun + \"1243\" /* comment */\n" 4869 " \"5678\";", 4870 format("fun + \"1243\" /* comment */\n" 4871 " \"5678\";", 4872 getLLVMStyleWithColumns(28))); 4873 EXPECT_EQ( 4874 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 4875 " \"aaaaaaaaaaaaaaaaaaaaa\"\n" 4876 " \"aaaaaaaaaaaaaaaa\";", 4877 format("aaaaaa =" 4878 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " 4879 "aaaaaaaaaaaaaaaaaaaaa\" " 4880 "\"aaaaaaaaaaaaaaaa\";")); 4881 verifyFormat("a = a + \"a\"\n" 4882 " \"a\"\n" 4883 " \"a\";"); 4884 verifyFormat("f(\"a\", \"b\"\n" 4885 " \"c\");"); 4886 4887 verifyFormat( 4888 "#define LL_FORMAT \"ll\"\n" 4889 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n" 4890 " \"d, ddddddddd: %\" LL_FORMAT \"d\");"); 4891 4892 verifyFormat("#define A(X) \\\n" 4893 " \"aaaaa\" #X \"bbbbbb\" \\\n" 4894 " \"ccccc\"", 4895 getLLVMStyleWithColumns(23)); 4896 verifyFormat("#define A \"def\"\n" 4897 "f(\"abc\" A \"ghi\"\n" 4898 " \"jkl\");"); 4899 4900 verifyFormat("f(L\"a\"\n" 4901 " L\"b\");"); 4902 verifyFormat("#define A(X) \\\n" 4903 " L\"aaaaa\" #X L\"bbbbbb\" \\\n" 4904 " L\"ccccc\"", 4905 getLLVMStyleWithColumns(25)); 4906 4907 verifyFormat("f(@\"a\"\n" 4908 " @\"b\");"); 4909 verifyFormat("NSString s = @\"a\"\n" 4910 " @\"b\"\n" 4911 " @\"c\";"); 4912 verifyFormat("NSString s = @\"a\"\n" 4913 " \"b\"\n" 4914 " \"c\";"); 4915 } 4916 4917 TEST_F(FormatTest, ReturnTypeBreakingStyle) { 4918 FormatStyle Style = getLLVMStyle(); 4919 // No declarations or definitions should be moved to own line. 4920 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None; 4921 verifyFormat("class A {\n" 4922 " int f() { return 1; }\n" 4923 " int g();\n" 4924 "};\n" 4925 "int f() { return 1; }\n" 4926 "int g();\n", 4927 Style); 4928 4929 // All declarations and definitions should have the return type moved to its 4930 // own 4931 // line. 4932 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 4933 verifyFormat("class E {\n" 4934 " int\n" 4935 " f() {\n" 4936 " return 1;\n" 4937 " }\n" 4938 " int\n" 4939 " g();\n" 4940 "};\n" 4941 "int\n" 4942 "f() {\n" 4943 " return 1;\n" 4944 "}\n" 4945 "int\n" 4946 "g();\n", 4947 Style); 4948 4949 // Top-level definitions, and no kinds of declarations should have the 4950 // return type moved to its own line. 4951 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions; 4952 verifyFormat("class B {\n" 4953 " int f() { return 1; }\n" 4954 " int g();\n" 4955 "};\n" 4956 "int\n" 4957 "f() {\n" 4958 " return 1;\n" 4959 "}\n" 4960 "int g();\n", 4961 Style); 4962 4963 // Top-level definitions and declarations should have the return type moved 4964 // to its own line. 4965 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel; 4966 verifyFormat("class C {\n" 4967 " int f() { return 1; }\n" 4968 " int g();\n" 4969 "};\n" 4970 "int\n" 4971 "f() {\n" 4972 " return 1;\n" 4973 "}\n" 4974 "int\n" 4975 "g();\n", 4976 Style); 4977 4978 // All definitions should have the return type moved to its own line, but no 4979 // kinds of declarations. 4980 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 4981 verifyFormat("class D {\n" 4982 " int\n" 4983 " f() {\n" 4984 " return 1;\n" 4985 " }\n" 4986 " int g();\n" 4987 "};\n" 4988 "int\n" 4989 "f() {\n" 4990 " return 1;\n" 4991 "}\n" 4992 "int g();\n", 4993 Style); 4994 verifyFormat("const char *\n" 4995 "f(void) {\n" // Break here. 4996 " return \"\";\n" 4997 "}\n" 4998 "const char *bar(void);\n", // No break here. 4999 Style); 5000 verifyFormat("template <class T>\n" 5001 "T *\n" 5002 "f(T &c) {\n" // Break here. 5003 " return NULL;\n" 5004 "}\n" 5005 "template <class T> T *f(T &c);\n", // No break here. 5006 Style); 5007 verifyFormat("class C {\n" 5008 " int\n" 5009 " operator+() {\n" 5010 " return 1;\n" 5011 " }\n" 5012 " int\n" 5013 " operator()() {\n" 5014 " return 1;\n" 5015 " }\n" 5016 "};\n", 5017 Style); 5018 verifyFormat("void\n" 5019 "A::operator()() {}\n" 5020 "void\n" 5021 "A::operator>>() {}\n" 5022 "void\n" 5023 "A::operator+() {}\n", 5024 Style); 5025 verifyFormat("void *operator new(std::size_t s);", // No break here. 5026 Style); 5027 verifyFormat("void *\n" 5028 "operator new(std::size_t s) {}", 5029 Style); 5030 verifyFormat("void *\n" 5031 "operator delete[](void *ptr) {}", 5032 Style); 5033 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 5034 verifyFormat("const char *\n" 5035 "f(void)\n" // Break here. 5036 "{\n" 5037 " return \"\";\n" 5038 "}\n" 5039 "const char *bar(void);\n", // No break here. 5040 Style); 5041 verifyFormat("template <class T>\n" 5042 "T *\n" // Problem here: no line break 5043 "f(T &c)\n" // Break here. 5044 "{\n" 5045 " return NULL;\n" 5046 "}\n" 5047 "template <class T> T *f(T &c);\n", // No break here. 5048 Style); 5049 } 5050 5051 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) { 5052 FormatStyle NoBreak = getLLVMStyle(); 5053 NoBreak.AlwaysBreakBeforeMultilineStrings = false; 5054 FormatStyle Break = getLLVMStyle(); 5055 Break.AlwaysBreakBeforeMultilineStrings = true; 5056 verifyFormat("aaaa = \"bbbb\"\n" 5057 " \"cccc\";", 5058 NoBreak); 5059 verifyFormat("aaaa =\n" 5060 " \"bbbb\"\n" 5061 " \"cccc\";", 5062 Break); 5063 verifyFormat("aaaa(\"bbbb\"\n" 5064 " \"cccc\");", 5065 NoBreak); 5066 verifyFormat("aaaa(\n" 5067 " \"bbbb\"\n" 5068 " \"cccc\");", 5069 Break); 5070 verifyFormat("aaaa(qqq, \"bbbb\"\n" 5071 " \"cccc\");", 5072 NoBreak); 5073 verifyFormat("aaaa(qqq,\n" 5074 " \"bbbb\"\n" 5075 " \"cccc\");", 5076 Break); 5077 verifyFormat("aaaa(qqq,\n" 5078 " L\"bbbb\"\n" 5079 " L\"cccc\");", 5080 Break); 5081 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n" 5082 " \"bbbb\"));", 5083 Break); 5084 verifyFormat("string s = someFunction(\n" 5085 " \"abc\"\n" 5086 " \"abc\");", 5087 Break); 5088 5089 // As we break before unary operators, breaking right after them is bad. 5090 verifyFormat("string foo = abc ? \"x\"\n" 5091 " \"blah blah blah blah blah blah\"\n" 5092 " : \"y\";", 5093 Break); 5094 5095 // Don't break if there is no column gain. 5096 verifyFormat("f(\"aaaa\"\n" 5097 " \"bbbb\");", 5098 Break); 5099 5100 // Treat literals with escaped newlines like multi-line string literals. 5101 EXPECT_EQ("x = \"a\\\n" 5102 "b\\\n" 5103 "c\";", 5104 format("x = \"a\\\n" 5105 "b\\\n" 5106 "c\";", 5107 NoBreak)); 5108 EXPECT_EQ("xxxx =\n" 5109 " \"a\\\n" 5110 "b\\\n" 5111 "c\";", 5112 format("xxxx = \"a\\\n" 5113 "b\\\n" 5114 "c\";", 5115 Break)); 5116 5117 EXPECT_EQ("NSString *const kString =\n" 5118 " @\"aaaa\"\n" 5119 " @\"bbbb\";", 5120 format("NSString *const kString = @\"aaaa\"\n" 5121 "@\"bbbb\";", 5122 Break)); 5123 5124 Break.ColumnLimit = 0; 5125 verifyFormat("const char *hello = \"hello llvm\";", Break); 5126 } 5127 5128 TEST_F(FormatTest, AlignsPipes) { 5129 verifyFormat( 5130 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5131 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5132 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5133 verifyFormat( 5134 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 5135 " << aaaaaaaaaaaaaaaaaaaa;"); 5136 verifyFormat( 5137 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5138 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5139 verifyFormat( 5140 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 5141 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5142 verifyFormat( 5143 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 5144 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 5145 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 5146 verifyFormat( 5147 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5148 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5149 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5150 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5151 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5152 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5153 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 5154 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n" 5155 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);"); 5156 verifyFormat( 5157 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5158 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5159 verifyFormat( 5160 "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n" 5161 " aaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5162 5163 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n" 5164 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();"); 5165 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5166 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5167 " aaaaaaaaaaaaaaaaaaaaa)\n" 5168 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5169 verifyFormat("LOG_IF(aaa == //\n" 5170 " bbb)\n" 5171 " << a << b;"); 5172 5173 // But sometimes, breaking before the first "<<" is desirable. 5174 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5175 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);"); 5176 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n" 5177 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5178 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5179 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n" 5180 " << BEF << IsTemplate << Description << E->getType();"); 5181 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5182 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5183 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5184 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5185 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5186 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5187 " << aaa;"); 5188 5189 verifyFormat( 5190 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5191 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5192 5193 // Incomplete string literal. 5194 EXPECT_EQ("llvm::errs() << \"\n" 5195 " << a;", 5196 format("llvm::errs() << \"\n<<a;")); 5197 5198 verifyFormat("void f() {\n" 5199 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n" 5200 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n" 5201 "}"); 5202 5203 // Handle 'endl'. 5204 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n" 5205 " << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 5206 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 5207 5208 // Handle '\n'. 5209 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n" 5210 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 5211 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n" 5212 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';"); 5213 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n" 5214 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";"); 5215 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 5216 } 5217 5218 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) { 5219 verifyFormat("return out << \"somepacket = {\\n\"\n" 5220 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" 5221 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" 5222 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" 5223 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" 5224 " << \"}\";"); 5225 5226 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 5227 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 5228 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;"); 5229 verifyFormat( 5230 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" 5231 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" 5232 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" 5233 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" 5234 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); 5235 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" 5236 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 5237 verifyFormat( 5238 "void f() {\n" 5239 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n" 5240 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 5241 "}"); 5242 5243 // Breaking before the first "<<" is generally not desirable. 5244 verifyFormat( 5245 "llvm::errs()\n" 5246 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5247 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5248 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5249 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5250 getLLVMStyleWithColumns(70)); 5251 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5252 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5253 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5254 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5255 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5256 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5257 getLLVMStyleWithColumns(70)); 5258 5259 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 5260 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 5261 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;"); 5262 verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 5263 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 5264 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);"); 5265 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n" 5266 " (aaaa + aaaa);", 5267 getLLVMStyleWithColumns(40)); 5268 verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n" 5269 " (aaaaaaa + aaaaa));", 5270 getLLVMStyleWithColumns(40)); 5271 verifyFormat( 5272 "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n" 5273 " SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n" 5274 " bbbbbbbbbbbbbbbbbbbbbbb);"); 5275 } 5276 5277 TEST_F(FormatTest, UnderstandsEquals) { 5278 verifyFormat( 5279 "aaaaaaaaaaaaaaaaa =\n" 5280 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5281 verifyFormat( 5282 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5283 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 5284 verifyFormat( 5285 "if (a) {\n" 5286 " f();\n" 5287 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5288 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 5289 "}"); 5290 5291 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5292 " 100000000 + 10000000) {\n}"); 5293 } 5294 5295 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 5296 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 5297 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 5298 5299 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 5300 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 5301 5302 verifyFormat( 5303 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 5304 " Parameter2);"); 5305 5306 verifyFormat( 5307 "ShortObject->shortFunction(\n" 5308 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 5309 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 5310 5311 verifyFormat("loooooooooooooongFunction(\n" 5312 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 5313 5314 verifyFormat( 5315 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 5316 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 5317 5318 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 5319 " .WillRepeatedly(Return(SomeValue));"); 5320 verifyFormat("void f() {\n" 5321 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 5322 " .Times(2)\n" 5323 " .WillRepeatedly(Return(SomeValue));\n" 5324 "}"); 5325 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n" 5326 " ccccccccccccccccccccccc);"); 5327 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5328 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5329 " .aaaaa(aaaaa),\n" 5330 " aaaaaaaaaaaaaaaaaaaaa);"); 5331 verifyFormat("void f() {\n" 5332 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5333 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n" 5334 "}"); 5335 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5336 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5337 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5338 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5339 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 5340 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5341 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5342 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5343 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n" 5344 "}"); 5345 5346 // Here, it is not necessary to wrap at "." or "->". 5347 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 5348 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 5349 verifyFormat( 5350 "aaaaaaaaaaa->aaaaaaaaa(\n" 5351 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5352 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 5353 5354 verifyFormat( 5355 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5356 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); 5357 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n" 5358 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 5359 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" 5360 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 5361 5362 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5363 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5364 " .a();"); 5365 5366 FormatStyle NoBinPacking = getLLVMStyle(); 5367 NoBinPacking.BinPackParameters = false; 5368 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 5369 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 5370 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" 5371 " aaaaaaaaaaaaaaaaaaa,\n" 5372 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5373 NoBinPacking); 5374 5375 // If there is a subsequent call, change to hanging indentation. 5376 verifyFormat( 5377 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5378 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n" 5379 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5380 verifyFormat( 5381 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5382 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));"); 5383 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5384 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5385 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5386 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5387 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5388 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 5389 } 5390 5391 TEST_F(FormatTest, WrapsTemplateDeclarations) { 5392 verifyFormat("template <typename T>\n" 5393 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 5394 verifyFormat("template <typename T>\n" 5395 "// T should be one of {A, B}.\n" 5396 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 5397 verifyFormat( 5398 "template <typename T>\n" 5399 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); 5400 verifyFormat("template <typename T>\n" 5401 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 5402 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 5403 verifyFormat( 5404 "template <typename T>\n" 5405 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 5406 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 5407 verifyFormat( 5408 "template <typename T>\n" 5409 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 5410 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 5411 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5412 verifyFormat("template <typename T>\n" 5413 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5414 " int aaaaaaaaaaaaaaaaaaaaaa);"); 5415 verifyFormat( 5416 "template <typename T1, typename T2 = char, typename T3 = char,\n" 5417 " typename T4 = char>\n" 5418 "void f();"); 5419 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n" 5420 " template <typename> class cccccccccccccccccccccc,\n" 5421 " typename ddddddddddddd>\n" 5422 "class C {};"); 5423 verifyFormat( 5424 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 5425 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5426 5427 verifyFormat("void f() {\n" 5428 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 5429 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n" 5430 "}"); 5431 5432 verifyFormat("template <typename T> class C {};"); 5433 verifyFormat("template <typename T> void f();"); 5434 verifyFormat("template <typename T> void f() {}"); 5435 verifyFormat( 5436 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 5437 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5438 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n" 5439 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 5440 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5441 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n" 5442 " bbbbbbbbbbbbbbbbbbbbbbbb);", 5443 getLLVMStyleWithColumns(72)); 5444 EXPECT_EQ("static_cast<A< //\n" 5445 " B> *>(\n" 5446 "\n" 5447 ");", 5448 format("static_cast<A<//\n" 5449 " B>*>(\n" 5450 "\n" 5451 " );")); 5452 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5453 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);"); 5454 5455 FormatStyle AlwaysBreak = getLLVMStyle(); 5456 AlwaysBreak.AlwaysBreakTemplateDeclarations = true; 5457 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak); 5458 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak); 5459 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak); 5460 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5461 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 5462 " ccccccccccccccccccccccccccccccccccccccccccccccc);"); 5463 verifyFormat("template <template <typename> class Fooooooo,\n" 5464 " template <typename> class Baaaaaaar>\n" 5465 "struct C {};", 5466 AlwaysBreak); 5467 verifyFormat("template <typename T> // T can be A, B or C.\n" 5468 "struct C {};", 5469 AlwaysBreak); 5470 verifyFormat("template <enum E> class A {\n" 5471 "public:\n" 5472 " E *f();\n" 5473 "};"); 5474 } 5475 5476 TEST_F(FormatTest, WrapsTemplateParameters) { 5477 FormatStyle Style = getLLVMStyle(); 5478 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 5479 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 5480 verifyFormat( 5481 "template <typename... a> struct q {};\n" 5482 "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 5483 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 5484 " y;", 5485 Style); 5486 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 5487 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 5488 verifyFormat( 5489 "template <typename... a> struct r {};\n" 5490 "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 5491 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 5492 " y;", 5493 Style); 5494 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 5495 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 5496 verifyFormat( 5497 "template <typename... a> struct s {};\n" 5498 "extern s<\n" 5499 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 5500 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n" 5501 " y;", 5502 Style); 5503 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 5504 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 5505 verifyFormat( 5506 "template <typename... a> struct t {};\n" 5507 "extern t<\n" 5508 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 5509 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n" 5510 " y;", 5511 Style); 5512 } 5513 5514 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 5515 verifyFormat( 5516 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5517 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5518 verifyFormat( 5519 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5520 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5521 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 5522 5523 // FIXME: Should we have the extra indent after the second break? 5524 verifyFormat( 5525 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5526 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5527 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5528 5529 verifyFormat( 5530 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 5531 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 5532 5533 // Breaking at nested name specifiers is generally not desirable. 5534 verifyFormat( 5535 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5536 " aaaaaaaaaaaaaaaaaaaaaaa);"); 5537 5538 verifyFormat( 5539 "aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n" 5540 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5541 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5542 " aaaaaaaaaaaaaaaaaaaaa);", 5543 getLLVMStyleWithColumns(74)); 5544 5545 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5546 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5547 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5548 } 5549 5550 TEST_F(FormatTest, UnderstandsTemplateParameters) { 5551 verifyFormat("A<int> a;"); 5552 verifyFormat("A<A<A<int>>> a;"); 5553 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 5554 verifyFormat("bool x = a < 1 || 2 > a;"); 5555 verifyFormat("bool x = 5 < f<int>();"); 5556 verifyFormat("bool x = f<int>() > 5;"); 5557 verifyFormat("bool x = 5 < a<int>::x;"); 5558 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 5559 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 5560 5561 verifyGoogleFormat("A<A<int>> a;"); 5562 verifyGoogleFormat("A<A<A<int>>> a;"); 5563 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 5564 verifyGoogleFormat("A<A<int> > a;"); 5565 verifyGoogleFormat("A<A<A<int> > > a;"); 5566 verifyGoogleFormat("A<A<A<A<int> > > > a;"); 5567 verifyGoogleFormat("A<::A<int>> a;"); 5568 verifyGoogleFormat("A<::A> a;"); 5569 verifyGoogleFormat("A< ::A> a;"); 5570 verifyGoogleFormat("A< ::A<int> > a;"); 5571 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); 5572 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); 5573 EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle())); 5574 EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle())); 5575 EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };", 5576 format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle())); 5577 5578 verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp)); 5579 5580 verifyFormat("test >> a >> b;"); 5581 verifyFormat("test << a >> b;"); 5582 5583 verifyFormat("f<int>();"); 5584 verifyFormat("template <typename T> void f() {}"); 5585 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;"); 5586 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : " 5587 "sizeof(char)>::type>;"); 5588 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};"); 5589 verifyFormat("f(a.operator()<A>());"); 5590 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5591 " .template operator()<A>());", 5592 getLLVMStyleWithColumns(35)); 5593 5594 // Not template parameters. 5595 verifyFormat("return a < b && c > d;"); 5596 verifyFormat("void f() {\n" 5597 " while (a < b && c > d) {\n" 5598 " }\n" 5599 "}"); 5600 verifyFormat("template <typename... Types>\n" 5601 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}"); 5602 5603 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5604 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);", 5605 getLLVMStyleWithColumns(60)); 5606 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");"); 5607 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}"); 5608 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <"); 5609 } 5610 5611 TEST_F(FormatTest, BitshiftOperatorWidth) { 5612 EXPECT_EQ("int a = 1 << 2; /* foo\n" 5613 " bar */", 5614 format("int a=1<<2; /* foo\n" 5615 " bar */")); 5616 5617 EXPECT_EQ("int b = 256 >> 1; /* foo\n" 5618 " bar */", 5619 format("int b =256>>1 ; /* foo\n" 5620 " bar */")); 5621 } 5622 5623 TEST_F(FormatTest, UnderstandsBinaryOperators) { 5624 verifyFormat("COMPARE(a, ==, b);"); 5625 verifyFormat("auto s = sizeof...(Ts) - 1;"); 5626 } 5627 5628 TEST_F(FormatTest, UnderstandsPointersToMembers) { 5629 verifyFormat("int A::*x;"); 5630 verifyFormat("int (S::*func)(void *);"); 5631 verifyFormat("void f() { int (S::*func)(void *); }"); 5632 verifyFormat("typedef bool *(Class::*Member)() const;"); 5633 verifyFormat("void f() {\n" 5634 " (a->*f)();\n" 5635 " a->*x;\n" 5636 " (a.*f)();\n" 5637 " ((*a).*f)();\n" 5638 " a.*x;\n" 5639 "}"); 5640 verifyFormat("void f() {\n" 5641 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 5642 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n" 5643 "}"); 5644 verifyFormat( 5645 "(aaaaaaaaaa->*bbbbbbb)(\n" 5646 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 5647 FormatStyle Style = getLLVMStyle(); 5648 Style.PointerAlignment = FormatStyle::PAS_Left; 5649 verifyFormat("typedef bool* (Class::*Member)() const;", Style); 5650 } 5651 5652 TEST_F(FormatTest, UnderstandsUnaryOperators) { 5653 verifyFormat("int a = -2;"); 5654 verifyFormat("f(-1, -2, -3);"); 5655 verifyFormat("a[-1] = 5;"); 5656 verifyFormat("int a = 5 + -2;"); 5657 verifyFormat("if (i == -1) {\n}"); 5658 verifyFormat("if (i != -1) {\n}"); 5659 verifyFormat("if (i > -1) {\n}"); 5660 verifyFormat("if (i < -1) {\n}"); 5661 verifyFormat("++(a->f());"); 5662 verifyFormat("--(a->f());"); 5663 verifyFormat("(a->f())++;"); 5664 verifyFormat("a[42]++;"); 5665 verifyFormat("if (!(a->f())) {\n}"); 5666 verifyFormat("if (!+i) {\n}"); 5667 verifyFormat("~&a;"); 5668 5669 verifyFormat("a-- > b;"); 5670 verifyFormat("b ? -a : c;"); 5671 verifyFormat("n * sizeof char16;"); 5672 verifyFormat("n * alignof char16;", getGoogleStyle()); 5673 verifyFormat("sizeof(char);"); 5674 verifyFormat("alignof(char);", getGoogleStyle()); 5675 5676 verifyFormat("return -1;"); 5677 verifyFormat("switch (a) {\n" 5678 "case -1:\n" 5679 " break;\n" 5680 "}"); 5681 verifyFormat("#define X -1"); 5682 verifyFormat("#define X -kConstant"); 5683 5684 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};"); 5685 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};"); 5686 5687 verifyFormat("int a = /* confusing comment */ -1;"); 5688 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 5689 verifyFormat("int a = i /* confusing comment */++;"); 5690 } 5691 5692 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) { 5693 verifyFormat("if (!aaaaaaaaaa( // break\n" 5694 " aaaaa)) {\n" 5695 "}"); 5696 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n" 5697 " aaaaa));"); 5698 verifyFormat("*aaa = aaaaaaa( // break\n" 5699 " bbbbbb);"); 5700 } 5701 5702 TEST_F(FormatTest, UnderstandsOverloadedOperators) { 5703 verifyFormat("bool operator<();"); 5704 verifyFormat("bool operator>();"); 5705 verifyFormat("bool operator=();"); 5706 verifyFormat("bool operator==();"); 5707 verifyFormat("bool operator!=();"); 5708 verifyFormat("int operator+();"); 5709 verifyFormat("int operator++();"); 5710 verifyFormat("int operator++(int) volatile noexcept;"); 5711 verifyFormat("bool operator,();"); 5712 verifyFormat("bool operator();"); 5713 verifyFormat("bool operator()();"); 5714 verifyFormat("bool operator[]();"); 5715 verifyFormat("operator bool();"); 5716 verifyFormat("operator int();"); 5717 verifyFormat("operator void *();"); 5718 verifyFormat("operator SomeType<int>();"); 5719 verifyFormat("operator SomeType<int, int>();"); 5720 verifyFormat("operator SomeType<SomeType<int>>();"); 5721 verifyFormat("void *operator new(std::size_t size);"); 5722 verifyFormat("void *operator new[](std::size_t size);"); 5723 verifyFormat("void operator delete(void *ptr);"); 5724 verifyFormat("void operator delete[](void *ptr);"); 5725 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n" 5726 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);"); 5727 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n" 5728 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;"); 5729 5730 verifyFormat( 5731 "ostream &operator<<(ostream &OutputStream,\n" 5732 " SomeReallyLongType WithSomeReallyLongValue);"); 5733 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n" 5734 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n" 5735 " return left.group < right.group;\n" 5736 "}"); 5737 verifyFormat("SomeType &operator=(const SomeType &S);"); 5738 verifyFormat("f.template operator()<int>();"); 5739 5740 verifyGoogleFormat("operator void*();"); 5741 verifyGoogleFormat("operator SomeType<SomeType<int>>();"); 5742 verifyGoogleFormat("operator ::A();"); 5743 5744 verifyFormat("using A::operator+;"); 5745 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n" 5746 "int i;"); 5747 } 5748 5749 TEST_F(FormatTest, UnderstandsFunctionRefQualification) { 5750 verifyFormat("Deleted &operator=(const Deleted &) & = default;"); 5751 verifyFormat("Deleted &operator=(const Deleted &) && = delete;"); 5752 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;"); 5753 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;"); 5754 verifyFormat("Deleted &operator=(const Deleted &) &;"); 5755 verifyFormat("Deleted &operator=(const Deleted &) &&;"); 5756 verifyFormat("SomeType MemberFunction(const Deleted &) &;"); 5757 verifyFormat("SomeType MemberFunction(const Deleted &) &&;"); 5758 verifyFormat("SomeType MemberFunction(const Deleted &) && {}"); 5759 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}"); 5760 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}"); 5761 verifyFormat("void Fn(T const &) const &;"); 5762 verifyFormat("void Fn(T const volatile &&) const volatile &&;"); 5763 verifyFormat("template <typename T>\n" 5764 "void F(T) && = delete;", 5765 getGoogleStyle()); 5766 5767 FormatStyle AlignLeft = getLLVMStyle(); 5768 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 5769 verifyFormat("void A::b() && {}", AlignLeft); 5770 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft); 5771 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;", 5772 AlignLeft); 5773 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft); 5774 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft); 5775 verifyFormat("auto Function(T t) & -> void {}", AlignLeft); 5776 verifyFormat("auto Function(T... t) & -> void {}", AlignLeft); 5777 verifyFormat("auto Function(T) & -> void {}", AlignLeft); 5778 verifyFormat("auto Function(T) & -> void;", AlignLeft); 5779 verifyFormat("void Fn(T const&) const&;", AlignLeft); 5780 verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft); 5781 5782 FormatStyle Spaces = getLLVMStyle(); 5783 Spaces.SpacesInCStyleCastParentheses = true; 5784 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces); 5785 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces); 5786 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces); 5787 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces); 5788 5789 Spaces.SpacesInCStyleCastParentheses = false; 5790 Spaces.SpacesInParentheses = true; 5791 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces); 5792 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces); 5793 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces); 5794 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces); 5795 } 5796 5797 TEST_F(FormatTest, UnderstandsNewAndDelete) { 5798 verifyFormat("void f() {\n" 5799 " A *a = new A;\n" 5800 " A *a = new (placement) A;\n" 5801 " delete a;\n" 5802 " delete (A *)a;\n" 5803 "}"); 5804 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 5805 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 5806 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5807 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 5808 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 5809 verifyFormat("delete[] h->p;"); 5810 } 5811 5812 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 5813 verifyFormat("int *f(int *a) {}"); 5814 verifyFormat("int main(int argc, char **argv) {}"); 5815 verifyFormat("Test::Test(int b) : a(b * b) {}"); 5816 verifyIndependentOfContext("f(a, *a);"); 5817 verifyFormat("void g() { f(*a); }"); 5818 verifyIndependentOfContext("int a = b * 10;"); 5819 verifyIndependentOfContext("int a = 10 * b;"); 5820 verifyIndependentOfContext("int a = b * c;"); 5821 verifyIndependentOfContext("int a += b * c;"); 5822 verifyIndependentOfContext("int a -= b * c;"); 5823 verifyIndependentOfContext("int a *= b * c;"); 5824 verifyIndependentOfContext("int a /= b * c;"); 5825 verifyIndependentOfContext("int a = *b;"); 5826 verifyIndependentOfContext("int a = *b * c;"); 5827 verifyIndependentOfContext("int a = b * *c;"); 5828 verifyIndependentOfContext("int a = b * (10);"); 5829 verifyIndependentOfContext("S << b * (10);"); 5830 verifyIndependentOfContext("return 10 * b;"); 5831 verifyIndependentOfContext("return *b * *c;"); 5832 verifyIndependentOfContext("return a & ~b;"); 5833 verifyIndependentOfContext("f(b ? *c : *d);"); 5834 verifyIndependentOfContext("int a = b ? *c : *d;"); 5835 verifyIndependentOfContext("*b = a;"); 5836 verifyIndependentOfContext("a * ~b;"); 5837 verifyIndependentOfContext("a * !b;"); 5838 verifyIndependentOfContext("a * +b;"); 5839 verifyIndependentOfContext("a * -b;"); 5840 verifyIndependentOfContext("a * ++b;"); 5841 verifyIndependentOfContext("a * --b;"); 5842 verifyIndependentOfContext("a[4] * b;"); 5843 verifyIndependentOfContext("a[a * a] = 1;"); 5844 verifyIndependentOfContext("f() * b;"); 5845 verifyIndependentOfContext("a * [self dostuff];"); 5846 verifyIndependentOfContext("int x = a * (a + b);"); 5847 verifyIndependentOfContext("(a *)(a + b);"); 5848 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;"); 5849 verifyIndependentOfContext("int *pa = (int *)&a;"); 5850 verifyIndependentOfContext("return sizeof(int **);"); 5851 verifyIndependentOfContext("return sizeof(int ******);"); 5852 verifyIndependentOfContext("return (int **&)a;"); 5853 verifyIndependentOfContext("f((*PointerToArray)[10]);"); 5854 verifyFormat("void f(Type (*parameter)[10]) {}"); 5855 verifyFormat("void f(Type (¶meter)[10]) {}"); 5856 verifyGoogleFormat("return sizeof(int**);"); 5857 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); 5858 verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); 5859 verifyFormat("auto a = [](int **&, int ***) {};"); 5860 verifyFormat("auto PointerBinding = [](const char *S) {};"); 5861 verifyFormat("typedef typeof(int(int, int)) *MyFunc;"); 5862 verifyFormat("[](const decltype(*a) &value) {}"); 5863 verifyFormat("decltype(a * b) F();"); 5864 verifyFormat("#define MACRO() [](A *a) { return 1; }"); 5865 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}"); 5866 verifyIndependentOfContext("typedef void (*f)(int *a);"); 5867 verifyIndependentOfContext("int i{a * b};"); 5868 verifyIndependentOfContext("aaa && aaa->f();"); 5869 verifyIndependentOfContext("int x = ~*p;"); 5870 verifyFormat("Constructor() : a(a), area(width * height) {}"); 5871 verifyFormat("Constructor() : a(a), area(a, width * height) {}"); 5872 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}"); 5873 verifyFormat("void f() { f(a, c * d); }"); 5874 verifyFormat("void f() { f(new a(), c * d); }"); 5875 verifyFormat("void f(const MyOverride &override);"); 5876 verifyFormat("void f(const MyFinal &final);"); 5877 verifyIndependentOfContext("bool a = f() && override.f();"); 5878 verifyIndependentOfContext("bool a = f() && final.f();"); 5879 5880 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 5881 5882 verifyIndependentOfContext("A<int *> a;"); 5883 verifyIndependentOfContext("A<int **> a;"); 5884 verifyIndependentOfContext("A<int *, int *> a;"); 5885 verifyIndependentOfContext("A<int *[]> a;"); 5886 verifyIndependentOfContext( 5887 "const char *const p = reinterpret_cast<const char *const>(q);"); 5888 verifyIndependentOfContext("A<int **, int **> a;"); 5889 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); 5890 verifyFormat("for (char **a = b; *a; ++a) {\n}"); 5891 verifyFormat("for (; a && b;) {\n}"); 5892 verifyFormat("bool foo = true && [] { return false; }();"); 5893 5894 verifyFormat( 5895 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5896 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5897 5898 verifyGoogleFormat("int const* a = &b;"); 5899 verifyGoogleFormat("**outparam = 1;"); 5900 verifyGoogleFormat("*outparam = a * b;"); 5901 verifyGoogleFormat("int main(int argc, char** argv) {}"); 5902 verifyGoogleFormat("A<int*> a;"); 5903 verifyGoogleFormat("A<int**> a;"); 5904 verifyGoogleFormat("A<int*, int*> a;"); 5905 verifyGoogleFormat("A<int**, int**> a;"); 5906 verifyGoogleFormat("f(b ? *c : *d);"); 5907 verifyGoogleFormat("int a = b ? *c : *d;"); 5908 verifyGoogleFormat("Type* t = **x;"); 5909 verifyGoogleFormat("Type* t = *++*x;"); 5910 verifyGoogleFormat("*++*x;"); 5911 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 5912 verifyGoogleFormat("Type* t = x++ * y;"); 5913 verifyGoogleFormat( 5914 "const char* const p = reinterpret_cast<const char* const>(q);"); 5915 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);"); 5916 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);"); 5917 verifyGoogleFormat("template <typename T>\n" 5918 "void f(int i = 0, SomeType** temps = NULL);"); 5919 5920 FormatStyle Left = getLLVMStyle(); 5921 Left.PointerAlignment = FormatStyle::PAS_Left; 5922 verifyFormat("x = *a(x) = *a(y);", Left); 5923 verifyFormat("for (;; *a = b) {\n}", Left); 5924 verifyFormat("return *this += 1;", Left); 5925 verifyFormat("throw *x;", Left); 5926 verifyFormat("delete *x;", Left); 5927 verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left); 5928 verifyFormat("[](const decltype(*a)* ptr) {}", Left); 5929 verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left); 5930 5931 verifyIndependentOfContext("a = *(x + y);"); 5932 verifyIndependentOfContext("a = &(x + y);"); 5933 verifyIndependentOfContext("*(x + y).call();"); 5934 verifyIndependentOfContext("&(x + y)->call();"); 5935 verifyFormat("void f() { &(*I).first; }"); 5936 5937 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 5938 verifyFormat( 5939 "int *MyValues = {\n" 5940 " *A, // Operator detection might be confused by the '{'\n" 5941 " *BB // Operator detection might be confused by previous comment\n" 5942 "};"); 5943 5944 verifyIndependentOfContext("if (int *a = &b)"); 5945 verifyIndependentOfContext("if (int &a = *b)"); 5946 verifyIndependentOfContext("if (a & b[i])"); 5947 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 5948 verifyIndependentOfContext("if (*b[i])"); 5949 verifyIndependentOfContext("if (int *a = (&b))"); 5950 verifyIndependentOfContext("while (int *a = &b)"); 5951 verifyIndependentOfContext("size = sizeof *a;"); 5952 verifyIndependentOfContext("if (a && (b = c))"); 5953 verifyFormat("void f() {\n" 5954 " for (const int &v : Values) {\n" 5955 " }\n" 5956 "}"); 5957 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); 5958 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); 5959 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}"); 5960 5961 verifyFormat("#define A (!a * b)"); 5962 verifyFormat("#define MACRO \\\n" 5963 " int *i = a * b; \\\n" 5964 " void f(a *b);", 5965 getLLVMStyleWithColumns(19)); 5966 5967 verifyIndependentOfContext("A = new SomeType *[Length];"); 5968 verifyIndependentOfContext("A = new SomeType *[Length]();"); 5969 verifyIndependentOfContext("T **t = new T *;"); 5970 verifyIndependentOfContext("T **t = new T *();"); 5971 verifyGoogleFormat("A = new SomeType*[Length]();"); 5972 verifyGoogleFormat("A = new SomeType*[Length];"); 5973 verifyGoogleFormat("T** t = new T*;"); 5974 verifyGoogleFormat("T** t = new T*();"); 5975 5976 verifyFormat("STATIC_ASSERT((a & b) == 0);"); 5977 verifyFormat("STATIC_ASSERT(0 == (a & b));"); 5978 verifyFormat("template <bool a, bool b> " 5979 "typename t::if<x && y>::type f() {}"); 5980 verifyFormat("template <int *y> f() {}"); 5981 verifyFormat("vector<int *> v;"); 5982 verifyFormat("vector<int *const> v;"); 5983 verifyFormat("vector<int *const **const *> v;"); 5984 verifyFormat("vector<int *volatile> v;"); 5985 verifyFormat("vector<a * b> v;"); 5986 verifyFormat("foo<b && false>();"); 5987 verifyFormat("foo<b & 1>();"); 5988 verifyFormat("decltype(*::std::declval<const T &>()) void F();"); 5989 verifyFormat( 5990 "template <class T, class = typename std::enable_if<\n" 5991 " std::is_integral<T>::value &&\n" 5992 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n" 5993 "void F();", 5994 getLLVMStyleWithColumns(70)); 5995 verifyFormat( 5996 "template <class T,\n" 5997 " class = typename std::enable_if<\n" 5998 " std::is_integral<T>::value &&\n" 5999 " (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n" 6000 " class U>\n" 6001 "void F();", 6002 getLLVMStyleWithColumns(70)); 6003 verifyFormat( 6004 "template <class T,\n" 6005 " class = typename ::std::enable_if<\n" 6006 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n" 6007 "void F();", 6008 getGoogleStyleWithColumns(68)); 6009 6010 verifyIndependentOfContext("MACRO(int *i);"); 6011 verifyIndependentOfContext("MACRO(auto *a);"); 6012 verifyIndependentOfContext("MACRO(const A *a);"); 6013 verifyIndependentOfContext("MACRO(A *const a);"); 6014 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');"); 6015 verifyFormat("void f() { f(float{1}, a * a); }"); 6016 // FIXME: Is there a way to make this work? 6017 // verifyIndependentOfContext("MACRO(A *a);"); 6018 6019 verifyFormat("DatumHandle const *operator->() const { return input_; }"); 6020 verifyFormat("return options != nullptr && operator==(*options);"); 6021 6022 EXPECT_EQ("#define OP(x) \\\n" 6023 " ostream &operator<<(ostream &s, const A &a) { \\\n" 6024 " return s << a.DebugString(); \\\n" 6025 " }", 6026 format("#define OP(x) \\\n" 6027 " ostream &operator<<(ostream &s, const A &a) { \\\n" 6028 " return s << a.DebugString(); \\\n" 6029 " }", 6030 getLLVMStyleWithColumns(50))); 6031 6032 // FIXME: We cannot handle this case yet; we might be able to figure out that 6033 // foo<x> d > v; doesn't make sense. 6034 verifyFormat("foo<a<b && c> d> v;"); 6035 6036 FormatStyle PointerMiddle = getLLVMStyle(); 6037 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 6038 verifyFormat("delete *x;", PointerMiddle); 6039 verifyFormat("int * x;", PointerMiddle); 6040 verifyFormat("int *[] x;", PointerMiddle); 6041 verifyFormat("template <int * y> f() {}", PointerMiddle); 6042 verifyFormat("int * f(int * a) {}", PointerMiddle); 6043 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle); 6044 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle); 6045 verifyFormat("A<int *> a;", PointerMiddle); 6046 verifyFormat("A<int **> a;", PointerMiddle); 6047 verifyFormat("A<int *, int *> a;", PointerMiddle); 6048 verifyFormat("A<int *[]> a;", PointerMiddle); 6049 verifyFormat("A = new SomeType *[Length]();", PointerMiddle); 6050 verifyFormat("A = new SomeType *[Length];", PointerMiddle); 6051 verifyFormat("T ** t = new T *;", PointerMiddle); 6052 6053 // Member function reference qualifiers aren't binary operators. 6054 verifyFormat("string // break\n" 6055 "operator()() & {}"); 6056 verifyFormat("string // break\n" 6057 "operator()() && {}"); 6058 verifyGoogleFormat("template <typename T>\n" 6059 "auto x() & -> int {}"); 6060 } 6061 6062 TEST_F(FormatTest, UnderstandsAttributes) { 6063 verifyFormat("SomeType s __attribute__((unused)) (InitValue);"); 6064 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n" 6065 "aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 6066 FormatStyle AfterType = getLLVMStyle(); 6067 AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 6068 verifyFormat("__attribute__((nodebug)) void\n" 6069 "foo() {}\n", 6070 AfterType); 6071 } 6072 6073 TEST_F(FormatTest, UnderstandsSquareAttributes) { 6074 verifyFormat("SomeType s [[unused]] (InitValue);"); 6075 verifyFormat("SomeType s [[gnu::unused]] (InitValue);"); 6076 verifyFormat("SomeType s [[using gnu: unused]] (InitValue);"); 6077 verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}"); 6078 verifyFormat("void f() [[deprecated(\"so sorry\")]];"); 6079 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6080 " [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 6081 6082 // Make sure we do not mistake attributes for array subscripts. 6083 verifyFormat("int a() {}\n" 6084 "[[unused]] int b() {}\n"); 6085 6086 // On the other hand, we still need to correctly find array subscripts. 6087 verifyFormat("int a = std::vector<int>{1, 2, 3}[0];"); 6088 6089 // Make sure we do not parse attributes as lambda introducers. 6090 FormatStyle MultiLineFunctions = getLLVMStyle(); 6091 MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 6092 verifyFormat("[[unused]] int b() {\n" 6093 " return 42;\n" 6094 "}\n", 6095 MultiLineFunctions); 6096 } 6097 6098 TEST_F(FormatTest, UnderstandsEllipsis) { 6099 verifyFormat("int printf(const char *fmt, ...);"); 6100 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); 6101 verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}"); 6102 6103 FormatStyle PointersLeft = getLLVMStyle(); 6104 PointersLeft.PointerAlignment = FormatStyle::PAS_Left; 6105 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft); 6106 } 6107 6108 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { 6109 EXPECT_EQ("int *a;\n" 6110 "int *a;\n" 6111 "int *a;", 6112 format("int *a;\n" 6113 "int* a;\n" 6114 "int *a;", 6115 getGoogleStyle())); 6116 EXPECT_EQ("int* a;\n" 6117 "int* a;\n" 6118 "int* a;", 6119 format("int* a;\n" 6120 "int* a;\n" 6121 "int *a;", 6122 getGoogleStyle())); 6123 EXPECT_EQ("int *a;\n" 6124 "int *a;\n" 6125 "int *a;", 6126 format("int *a;\n" 6127 "int * a;\n" 6128 "int * a;", 6129 getGoogleStyle())); 6130 EXPECT_EQ("auto x = [] {\n" 6131 " int *a;\n" 6132 " int *a;\n" 6133 " int *a;\n" 6134 "};", 6135 format("auto x=[]{int *a;\n" 6136 "int * a;\n" 6137 "int * a;};", 6138 getGoogleStyle())); 6139 } 6140 6141 TEST_F(FormatTest, UnderstandsRvalueReferences) { 6142 verifyFormat("int f(int &&a) {}"); 6143 verifyFormat("int f(int a, char &&b) {}"); 6144 verifyFormat("void f() { int &&a = b; }"); 6145 verifyGoogleFormat("int f(int a, char&& b) {}"); 6146 verifyGoogleFormat("void f() { int&& a = b; }"); 6147 6148 verifyIndependentOfContext("A<int &&> a;"); 6149 verifyIndependentOfContext("A<int &&, int &&> a;"); 6150 verifyGoogleFormat("A<int&&> a;"); 6151 verifyGoogleFormat("A<int&&, int&&> a;"); 6152 6153 // Not rvalue references: 6154 verifyFormat("template <bool B, bool C> class A {\n" 6155 " static_assert(B && C, \"Something is wrong\");\n" 6156 "};"); 6157 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))"); 6158 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))"); 6159 verifyFormat("#define A(a, b) (a && b)"); 6160 } 6161 6162 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 6163 verifyFormat("void f() {\n" 6164 " x[aaaaaaaaa -\n" 6165 " b] = 23;\n" 6166 "}", 6167 getLLVMStyleWithColumns(15)); 6168 } 6169 6170 TEST_F(FormatTest, FormatsCasts) { 6171 verifyFormat("Type *A = static_cast<Type *>(P);"); 6172 verifyFormat("Type *A = (Type *)P;"); 6173 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 6174 verifyFormat("int a = (int)(2.0f);"); 6175 verifyFormat("int a = (int)2.0f;"); 6176 verifyFormat("x[(int32)y];"); 6177 verifyFormat("x = (int32)y;"); 6178 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)"); 6179 verifyFormat("int a = (int)*b;"); 6180 verifyFormat("int a = (int)2.0f;"); 6181 verifyFormat("int a = (int)~0;"); 6182 verifyFormat("int a = (int)++a;"); 6183 verifyFormat("int a = (int)sizeof(int);"); 6184 verifyFormat("int a = (int)+2;"); 6185 verifyFormat("my_int a = (my_int)2.0f;"); 6186 verifyFormat("my_int a = (my_int)sizeof(int);"); 6187 verifyFormat("return (my_int)aaa;"); 6188 verifyFormat("#define x ((int)-1)"); 6189 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1"); 6190 verifyFormat("#define p(q) ((int *)&q)"); 6191 verifyFormat("fn(a)(b) + 1;"); 6192 6193 verifyFormat("void f() { my_int a = (my_int)*b; }"); 6194 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }"); 6195 verifyFormat("my_int a = (my_int)~0;"); 6196 verifyFormat("my_int a = (my_int)++a;"); 6197 verifyFormat("my_int a = (my_int)-2;"); 6198 verifyFormat("my_int a = (my_int)1;"); 6199 verifyFormat("my_int a = (my_int *)1;"); 6200 verifyFormat("my_int a = (const my_int)-1;"); 6201 verifyFormat("my_int a = (const my_int *)-1;"); 6202 verifyFormat("my_int a = (my_int)(my_int)-1;"); 6203 verifyFormat("my_int a = (ns::my_int)-2;"); 6204 verifyFormat("case (my_int)ONE:"); 6205 verifyFormat("auto x = (X)this;"); 6206 6207 // FIXME: single value wrapped with paren will be treated as cast. 6208 verifyFormat("void f(int i = (kValue)*kMask) {}"); 6209 6210 verifyFormat("{ (void)F; }"); 6211 6212 // Don't break after a cast's 6213 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 6214 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n" 6215 " bbbbbbbbbbbbbbbbbbbbbb);"); 6216 6217 // These are not casts. 6218 verifyFormat("void f(int *) {}"); 6219 verifyFormat("f(foo)->b;"); 6220 verifyFormat("f(foo).b;"); 6221 verifyFormat("f(foo)(b);"); 6222 verifyFormat("f(foo)[b];"); 6223 verifyFormat("[](foo) { return 4; }(bar);"); 6224 verifyFormat("(*funptr)(foo)[4];"); 6225 verifyFormat("funptrs[4](foo)[4];"); 6226 verifyFormat("void f(int *);"); 6227 verifyFormat("void f(int *) = 0;"); 6228 verifyFormat("void f(SmallVector<int>) {}"); 6229 verifyFormat("void f(SmallVector<int>);"); 6230 verifyFormat("void f(SmallVector<int>) = 0;"); 6231 verifyFormat("void f(int i = (kA * kB) & kMask) {}"); 6232 verifyFormat("int a = sizeof(int) * b;"); 6233 verifyFormat("int a = alignof(int) * b;", getGoogleStyle()); 6234 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;"); 6235 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");"); 6236 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;"); 6237 6238 // These are not casts, but at some point were confused with casts. 6239 verifyFormat("virtual void foo(int *) override;"); 6240 verifyFormat("virtual void foo(char &) const;"); 6241 verifyFormat("virtual void foo(int *a, char *) const;"); 6242 verifyFormat("int a = sizeof(int *) + b;"); 6243 verifyFormat("int a = alignof(int *) + b;", getGoogleStyle()); 6244 verifyFormat("bool b = f(g<int>) && c;"); 6245 verifyFormat("typedef void (*f)(int i) func;"); 6246 6247 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n" 6248 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 6249 // FIXME: The indentation here is not ideal. 6250 verifyFormat( 6251 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6252 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n" 6253 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];"); 6254 } 6255 6256 TEST_F(FormatTest, FormatsFunctionTypes) { 6257 verifyFormat("A<bool()> a;"); 6258 verifyFormat("A<SomeType()> a;"); 6259 verifyFormat("A<void (*)(int, std::string)> a;"); 6260 verifyFormat("A<void *(int)>;"); 6261 verifyFormat("void *(*a)(int *, SomeType *);"); 6262 verifyFormat("int (*func)(void *);"); 6263 verifyFormat("void f() { int (*func)(void *); }"); 6264 verifyFormat("template <class CallbackClass>\n" 6265 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);"); 6266 6267 verifyGoogleFormat("A<void*(int*, SomeType*)>;"); 6268 verifyGoogleFormat("void* (*a)(int);"); 6269 verifyGoogleFormat( 6270 "template <class CallbackClass>\n" 6271 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);"); 6272 6273 // Other constructs can look somewhat like function types: 6274 verifyFormat("A<sizeof(*x)> a;"); 6275 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)"); 6276 verifyFormat("some_var = function(*some_pointer_var)[0];"); 6277 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }"); 6278 verifyFormat("int x = f(&h)();"); 6279 verifyFormat("returnsFunction(¶m1, ¶m2)(param);"); 6280 verifyFormat("std::function<\n" 6281 " LooooooooooongTemplatedType<\n" 6282 " SomeType>*(\n" 6283 " LooooooooooooooooongType type)>\n" 6284 " function;", 6285 getGoogleStyleWithColumns(40)); 6286 } 6287 6288 TEST_F(FormatTest, FormatsPointersToArrayTypes) { 6289 verifyFormat("A (*foo_)[6];"); 6290 verifyFormat("vector<int> (*foo_)[6];"); 6291 } 6292 6293 TEST_F(FormatTest, BreaksLongVariableDeclarations) { 6294 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6295 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6296 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n" 6297 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6298 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6299 " *LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6300 6301 // Different ways of ()-initializiation. 6302 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6303 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);"); 6304 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6305 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);"); 6306 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6307 " LoooooooooooooooooooooooooooooooooooooooongVariable({});"); 6308 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6309 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);"); 6310 6311 // Lambdas should not confuse the variable declaration heuristic. 6312 verifyFormat("LooooooooooooooooongType\n" 6313 " variable(nullptr, [](A *a) {});", 6314 getLLVMStyleWithColumns(40)); 6315 } 6316 6317 TEST_F(FormatTest, BreaksLongDeclarations) { 6318 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n" 6319 " AnotherNameForTheLongType;"); 6320 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n" 6321 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 6322 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6323 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 6324 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n" 6325 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 6326 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6327 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6328 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n" 6329 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6330 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 6331 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6332 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 6333 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6334 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6335 "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);"); 6336 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6337 "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}"); 6338 FormatStyle Indented = getLLVMStyle(); 6339 Indented.IndentWrappedFunctionNames = true; 6340 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6341 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();", 6342 Indented); 6343 verifyFormat( 6344 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6345 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6346 Indented); 6347 verifyFormat( 6348 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 6349 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6350 Indented); 6351 verifyFormat( 6352 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 6353 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6354 Indented); 6355 6356 // FIXME: Without the comment, this breaks after "(". 6357 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n" 6358 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();", 6359 getGoogleStyle()); 6360 6361 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 6362 " int LoooooooooooooooooooongParam2) {}"); 6363 verifyFormat( 6364 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" 6365 " SourceLocation L, IdentifierIn *II,\n" 6366 " Type *T) {}"); 6367 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" 6368 "ReallyReaaallyLongFunctionName(\n" 6369 " const std::string &SomeParameter,\n" 6370 " const SomeType<string, SomeOtherTemplateParameter>\n" 6371 " &ReallyReallyLongParameterName,\n" 6372 " const SomeType<string, SomeOtherTemplateParameter>\n" 6373 " &AnotherLongParameterName) {}"); 6374 verifyFormat("template <typename A>\n" 6375 "SomeLoooooooooooooooooooooongType<\n" 6376 " typename some_namespace::SomeOtherType<A>::Type>\n" 6377 "Function() {}"); 6378 6379 verifyGoogleFormat( 6380 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" 6381 " aaaaaaaaaaaaaaaaaaaaaaa;"); 6382 verifyGoogleFormat( 6383 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" 6384 " SourceLocation L) {}"); 6385 verifyGoogleFormat( 6386 "some_namespace::LongReturnType\n" 6387 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" 6388 " int first_long_parameter, int second_parameter) {}"); 6389 6390 verifyGoogleFormat("template <typename T>\n" 6391 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" 6392 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); 6393 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6394 " int aaaaaaaaaaaaaaaaaaaaaaa);"); 6395 6396 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 6397 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6398 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6399 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6400 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 6401 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 6402 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6403 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 6404 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n" 6405 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6406 6407 verifyFormat("template <typename T> // Templates on own line.\n" 6408 "static int // Some comment.\n" 6409 "MyFunction(int a);", 6410 getLLVMStyle()); 6411 } 6412 6413 TEST_F(FormatTest, FormatsArrays) { 6414 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6415 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;"); 6416 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n" 6417 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;"); 6418 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n" 6419 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}"); 6420 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6421 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 6422 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6423 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;"); 6424 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6425 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6426 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 6427 verifyFormat( 6428 "llvm::outs() << \"aaaaaaaaaaaa: \"\n" 6429 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6430 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 6431 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n" 6432 " .aaaaaaaaaaaaaaaaaaaaaa();"); 6433 6434 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n" 6435 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];"); 6436 verifyFormat( 6437 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n" 6438 " .aaaaaaa[0]\n" 6439 " .aaaaaaaaaaaaaaaaaaaaaa();"); 6440 verifyFormat("a[::b::c];"); 6441 6442 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10)); 6443 6444 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0); 6445 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit); 6446 } 6447 6448 TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 6449 verifyFormat("(a)->b();"); 6450 verifyFormat("--a;"); 6451 } 6452 6453 TEST_F(FormatTest, HandlesIncludeDirectives) { 6454 verifyFormat("#include <string>\n" 6455 "#include <a/b/c.h>\n" 6456 "#include \"a/b/string\"\n" 6457 "#include \"string.h\"\n" 6458 "#include \"string.h\"\n" 6459 "#include <a-a>\n" 6460 "#include < path with space >\n" 6461 "#include_next <test.h>" 6462 "#include \"abc.h\" // this is included for ABC\n" 6463 "#include \"some long include\" // with a comment\n" 6464 "#include \"some very long include path\"\n" 6465 "#include <some/very/long/include/path>\n", 6466 getLLVMStyleWithColumns(35)); 6467 EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\"")); 6468 EXPECT_EQ("#include <a>", format("#include<a>")); 6469 6470 verifyFormat("#import <string>"); 6471 verifyFormat("#import <a/b/c.h>"); 6472 verifyFormat("#import \"a/b/string\""); 6473 verifyFormat("#import \"string.h\""); 6474 verifyFormat("#import \"string.h\""); 6475 verifyFormat("#if __has_include(<strstream>)\n" 6476 "#include <strstream>\n" 6477 "#endif"); 6478 6479 verifyFormat("#define MY_IMPORT <a/b>"); 6480 6481 verifyFormat("#if __has_include(<a/b>)"); 6482 verifyFormat("#if __has_include_next(<a/b>)"); 6483 verifyFormat("#define F __has_include(<a/b>)"); 6484 verifyFormat("#define F __has_include_next(<a/b>)"); 6485 6486 // Protocol buffer definition or missing "#". 6487 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";", 6488 getLLVMStyleWithColumns(30)); 6489 6490 FormatStyle Style = getLLVMStyle(); 6491 Style.AlwaysBreakBeforeMultilineStrings = true; 6492 Style.ColumnLimit = 0; 6493 verifyFormat("#import \"abc.h\"", Style); 6494 6495 // But 'import' might also be a regular C++ namespace. 6496 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6497 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6498 } 6499 6500 //===----------------------------------------------------------------------===// 6501 // Error recovery tests. 6502 //===----------------------------------------------------------------------===// 6503 6504 TEST_F(FormatTest, IncompleteParameterLists) { 6505 FormatStyle NoBinPacking = getLLVMStyle(); 6506 NoBinPacking.BinPackParameters = false; 6507 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" 6508 " double *min_x,\n" 6509 " double *max_x,\n" 6510 " double *min_y,\n" 6511 " double *max_y,\n" 6512 " double *min_z,\n" 6513 " double *max_z, ) {}", 6514 NoBinPacking); 6515 } 6516 6517 TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 6518 verifyFormat("void f() { return; }\n42"); 6519 verifyFormat("void f() {\n" 6520 " if (0)\n" 6521 " return;\n" 6522 "}\n" 6523 "42"); 6524 verifyFormat("void f() { return }\n42"); 6525 verifyFormat("void f() {\n" 6526 " if (0)\n" 6527 " return\n" 6528 "}\n" 6529 "42"); 6530 } 6531 6532 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 6533 EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); 6534 EXPECT_EQ("void f() {\n" 6535 " if (a)\n" 6536 " return\n" 6537 "}", 6538 format("void f ( ) { if ( a ) return }")); 6539 EXPECT_EQ("namespace N {\n" 6540 "void f()\n" 6541 "}", 6542 format("namespace N { void f() }")); 6543 EXPECT_EQ("namespace N {\n" 6544 "void f() {}\n" 6545 "void g()\n" 6546 "} // namespace N", 6547 format("namespace N { void f( ) { } void g( ) }")); 6548 } 6549 6550 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 6551 verifyFormat("int aaaaaaaa =\n" 6552 " // Overlylongcomment\n" 6553 " b;", 6554 getLLVMStyleWithColumns(20)); 6555 verifyFormat("function(\n" 6556 " ShortArgument,\n" 6557 " LoooooooooooongArgument);\n", 6558 getLLVMStyleWithColumns(20)); 6559 } 6560 6561 TEST_F(FormatTest, IncorrectAccessSpecifier) { 6562 verifyFormat("public:"); 6563 verifyFormat("class A {\n" 6564 "public\n" 6565 " void f() {}\n" 6566 "};"); 6567 verifyFormat("public\n" 6568 "int qwerty;"); 6569 verifyFormat("public\n" 6570 "B {}"); 6571 verifyFormat("public\n" 6572 "{}"); 6573 verifyFormat("public\n" 6574 "B { int x; }"); 6575 } 6576 6577 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 6578 verifyFormat("{"); 6579 verifyFormat("#})"); 6580 verifyNoCrash("(/**/[:!] ?[)."); 6581 } 6582 6583 TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 6584 verifyFormat("do {\n}"); 6585 verifyFormat("do {\n}\n" 6586 "f();"); 6587 verifyFormat("do {\n}\n" 6588 "wheeee(fun);"); 6589 verifyFormat("do {\n" 6590 " f();\n" 6591 "}"); 6592 } 6593 6594 TEST_F(FormatTest, IncorrectCodeMissingParens) { 6595 verifyFormat("if {\n foo;\n foo();\n}"); 6596 verifyFormat("switch {\n foo;\n foo();\n}"); 6597 verifyIncompleteFormat("for {\n foo;\n foo();\n}"); 6598 verifyFormat("while {\n foo;\n foo();\n}"); 6599 verifyFormat("do {\n foo;\n foo();\n} while;"); 6600 } 6601 6602 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 6603 verifyIncompleteFormat("namespace {\n" 6604 "class Foo { Foo (\n" 6605 "};\n" 6606 "} // namespace"); 6607 } 6608 6609 TEST_F(FormatTest, IncorrectCodeErrorDetection) { 6610 EXPECT_EQ("{\n {}\n", format("{\n{\n}\n")); 6611 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 6612 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 6613 EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n")); 6614 6615 EXPECT_EQ("{\n" 6616 " {\n" 6617 " breakme(\n" 6618 " qwe);\n" 6619 " }\n", 6620 format("{\n" 6621 " {\n" 6622 " breakme(qwe);\n" 6623 "}\n", 6624 getLLVMStyleWithColumns(10))); 6625 } 6626 6627 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 6628 verifyFormat("int x = {\n" 6629 " avariable,\n" 6630 " b(alongervariable)};", 6631 getLLVMStyleWithColumns(25)); 6632 } 6633 6634 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 6635 verifyFormat("return (a)(b){1, 2, 3};"); 6636 } 6637 6638 TEST_F(FormatTest, LayoutCxx11BraceInitializers) { 6639 verifyFormat("vector<int> x{1, 2, 3, 4};"); 6640 verifyFormat("vector<int> x{\n" 6641 " 1,\n" 6642 " 2,\n" 6643 " 3,\n" 6644 " 4,\n" 6645 "};"); 6646 verifyFormat("vector<T> x{{}, {}, {}, {}};"); 6647 verifyFormat("f({1, 2});"); 6648 verifyFormat("auto v = Foo{-1};"); 6649 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});"); 6650 verifyFormat("Class::Class : member{1, 2, 3} {}"); 6651 verifyFormat("new vector<int>{1, 2, 3};"); 6652 verifyFormat("new int[3]{1, 2, 3};"); 6653 verifyFormat("new int{1};"); 6654 verifyFormat("return {arg1, arg2};"); 6655 verifyFormat("return {arg1, SomeType{parameter}};"); 6656 verifyFormat("int count = set<int>{f(), g(), h()}.size();"); 6657 verifyFormat("new T{arg1, arg2};"); 6658 verifyFormat("f(MyMap[{composite, key}]);"); 6659 verifyFormat("class Class {\n" 6660 " T member = {arg1, arg2};\n" 6661 "};"); 6662 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};"); 6663 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 6664 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 6665 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");"); 6666 verifyFormat("int a = std::is_integral<int>{} + 0;"); 6667 6668 verifyFormat("int foo(int i) { return fo1{}(i); }"); 6669 verifyFormat("int foo(int i) { return fo1{}(i); }"); 6670 verifyFormat("auto i = decltype(x){};"); 6671 verifyFormat("std::vector<int> v = {1, 0 /* comment */};"); 6672 verifyFormat("Node n{1, Node{1000}, //\n" 6673 " 2};"); 6674 verifyFormat("Aaaa aaaaaaa{\n" 6675 " {\n" 6676 " aaaa,\n" 6677 " },\n" 6678 "};"); 6679 verifyFormat("class C : public D {\n" 6680 " SomeClass SC{2};\n" 6681 "};"); 6682 verifyFormat("class C : public A {\n" 6683 " class D : public B {\n" 6684 " void f() { int i{2}; }\n" 6685 " };\n" 6686 "};"); 6687 verifyFormat("#define A {a, a},"); 6688 6689 // Binpacking only if there is no trailing comma 6690 verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n" 6691 " cccccccccc, dddddddddd};", 6692 getLLVMStyleWithColumns(50)); 6693 verifyFormat("const Aaaaaa aaaaa = {\n" 6694 " aaaaaaaaaaa,\n" 6695 " bbbbbbbbbbb,\n" 6696 " ccccccccccc,\n" 6697 " ddddddddddd,\n" 6698 "};", getLLVMStyleWithColumns(50)); 6699 6700 // Cases where distinguising braced lists and blocks is hard. 6701 verifyFormat("vector<int> v{12} GUARDED_BY(mutex);"); 6702 verifyFormat("void f() {\n" 6703 " return; // comment\n" 6704 "}\n" 6705 "SomeType t;"); 6706 verifyFormat("void f() {\n" 6707 " if (a) {\n" 6708 " f();\n" 6709 " }\n" 6710 "}\n" 6711 "SomeType t;"); 6712 6713 // In combination with BinPackArguments = false. 6714 FormatStyle NoBinPacking = getLLVMStyle(); 6715 NoBinPacking.BinPackArguments = false; 6716 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n" 6717 " bbbbb,\n" 6718 " ccccc,\n" 6719 " ddddd,\n" 6720 " eeeee,\n" 6721 " ffffff,\n" 6722 " ggggg,\n" 6723 " hhhhhh,\n" 6724 " iiiiii,\n" 6725 " jjjjjj,\n" 6726 " kkkkkk};", 6727 NoBinPacking); 6728 verifyFormat("const Aaaaaa aaaaa = {\n" 6729 " aaaaa,\n" 6730 " bbbbb,\n" 6731 " ccccc,\n" 6732 " ddddd,\n" 6733 " eeeee,\n" 6734 " ffffff,\n" 6735 " ggggg,\n" 6736 " hhhhhh,\n" 6737 " iiiiii,\n" 6738 " jjjjjj,\n" 6739 " kkkkkk,\n" 6740 "};", 6741 NoBinPacking); 6742 verifyFormat( 6743 "const Aaaaaa aaaaa = {\n" 6744 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n" 6745 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n" 6746 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n" 6747 "};", 6748 NoBinPacking); 6749 6750 // FIXME: The alignment of these trailing comments might be bad. Then again, 6751 // this might be utterly useless in real code. 6752 verifyFormat("Constructor::Constructor()\n" 6753 " : some_value{ //\n" 6754 " aaaaaaa, //\n" 6755 " bbbbbbb} {}"); 6756 6757 // In braced lists, the first comment is always assumed to belong to the 6758 // first element. Thus, it can be moved to the next or previous line as 6759 // appropriate. 6760 EXPECT_EQ("function({// First element:\n" 6761 " 1,\n" 6762 " // Second element:\n" 6763 " 2});", 6764 format("function({\n" 6765 " // First element:\n" 6766 " 1,\n" 6767 " // Second element:\n" 6768 " 2});")); 6769 EXPECT_EQ("std::vector<int> MyNumbers{\n" 6770 " // First element:\n" 6771 " 1,\n" 6772 " // Second element:\n" 6773 " 2};", 6774 format("std::vector<int> MyNumbers{// First element:\n" 6775 " 1,\n" 6776 " // Second element:\n" 6777 " 2};", 6778 getLLVMStyleWithColumns(30))); 6779 // A trailing comma should still lead to an enforced line break and no 6780 // binpacking. 6781 EXPECT_EQ("vector<int> SomeVector = {\n" 6782 " // aaa\n" 6783 " 1,\n" 6784 " 2,\n" 6785 "};", 6786 format("vector<int> SomeVector = { // aaa\n" 6787 " 1, 2, };")); 6788 6789 FormatStyle ExtraSpaces = getLLVMStyle(); 6790 ExtraSpaces.Cpp11BracedListStyle = false; 6791 ExtraSpaces.ColumnLimit = 75; 6792 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces); 6793 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces); 6794 verifyFormat("f({ 1, 2 });", ExtraSpaces); 6795 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces); 6796 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces); 6797 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces); 6798 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces); 6799 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces); 6800 verifyFormat("return { arg1, arg2 };", ExtraSpaces); 6801 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces); 6802 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces); 6803 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces); 6804 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces); 6805 verifyFormat("class Class {\n" 6806 " T member = { arg1, arg2 };\n" 6807 "};", 6808 ExtraSpaces); 6809 verifyFormat( 6810 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6811 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n" 6812 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 6813 " bbbbbbbbbbbbbbbbbbbb, bbbbb };", 6814 ExtraSpaces); 6815 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces); 6816 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });", 6817 ExtraSpaces); 6818 verifyFormat( 6819 "someFunction(OtherParam,\n" 6820 " BracedList{ // comment 1 (Forcing interesting break)\n" 6821 " param1, param2,\n" 6822 " // comment 2\n" 6823 " param3, param4 });", 6824 ExtraSpaces); 6825 verifyFormat( 6826 "std::this_thread::sleep_for(\n" 6827 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);", 6828 ExtraSpaces); 6829 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n" 6830 " aaaaaaa,\n" 6831 " aaaaaaaaaa,\n" 6832 " aaaaa,\n" 6833 " aaaaaaaaaaaaaaa,\n" 6834 " aaa,\n" 6835 " aaaaaaaaaa,\n" 6836 " a,\n" 6837 " aaaaaaaaaaaaaaaaaaaaa,\n" 6838 " aaaaaaaaaaaa,\n" 6839 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n" 6840 " aaaaaaa,\n" 6841 " a};"); 6842 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces); 6843 verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces); 6844 verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces); 6845 } 6846 6847 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { 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, 666666, 7777777,\n" 6851 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6852 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6853 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 6854 verifyFormat("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, //\n" 6857 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6858 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 6859 verifyFormat( 6860 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6861 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6862 " 1, 22, 333, 4444, 55555, 666666, // comment\n" 6863 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6864 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6865 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6866 " 7777777};"); 6867 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6868 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6869 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6870 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6871 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6872 " // Separating comment.\n" 6873 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6874 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6875 " // Leading comment\n" 6876 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6877 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6878 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6879 " 1, 1, 1, 1};", 6880 getLLVMStyleWithColumns(39)); 6881 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6882 " 1, 1, 1, 1};", 6883 getLLVMStyleWithColumns(38)); 6884 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n" 6885 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};", 6886 getLLVMStyleWithColumns(43)); 6887 verifyFormat( 6888 "static unsigned SomeValues[10][3] = {\n" 6889 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n" 6890 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};"); 6891 verifyFormat("static auto fields = new vector<string>{\n" 6892 " \"aaaaaaaaaaaaa\",\n" 6893 " \"aaaaaaaaaaaaa\",\n" 6894 " \"aaaaaaaaaaaa\",\n" 6895 " \"aaaaaaaaaaaaaa\",\n" 6896 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 6897 " \"aaaaaaaaaaaa\",\n" 6898 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 6899 "};"); 6900 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};"); 6901 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n" 6902 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n" 6903 " 3, cccccccccccccccccccccc};", 6904 getLLVMStyleWithColumns(60)); 6905 6906 // Trailing commas. 6907 verifyFormat("vector<int> x = {\n" 6908 " 1, 1, 1, 1, 1, 1, 1, 1,\n" 6909 "};", 6910 getLLVMStyleWithColumns(39)); 6911 verifyFormat("vector<int> x = {\n" 6912 " 1, 1, 1, 1, 1, 1, 1, 1, //\n" 6913 "};", 6914 getLLVMStyleWithColumns(39)); 6915 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6916 " 1, 1, 1, 1,\n" 6917 " /**/ /**/};", 6918 getLLVMStyleWithColumns(39)); 6919 6920 // Trailing comment in the first line. 6921 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n" 6922 " 1111111111, 2222222222, 33333333333, 4444444444, //\n" 6923 " 111111111, 222222222, 3333333333, 444444444, //\n" 6924 " 11111111, 22222222, 333333333, 44444444};"); 6925 // Trailing comment in the last line. 6926 verifyFormat("int aaaaa[] = {\n" 6927 " 1, 2, 3, // comment\n" 6928 " 4, 5, 6 // comment\n" 6929 "};"); 6930 6931 // With nested lists, we should either format one item per line or all nested 6932 // lists one on line. 6933 // FIXME: For some nested lists, we can do better. 6934 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n" 6935 " {aaaaaaaaaaaaaaaaaaa},\n" 6936 " {aaaaaaaaaaaaaaaaaaaaa},\n" 6937 " {aaaaaaaaaaaaaaaaa}};", 6938 getLLVMStyleWithColumns(60)); 6939 verifyFormat( 6940 "SomeStruct my_struct_array = {\n" 6941 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n" 6942 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n" 6943 " {aaa, aaa},\n" 6944 " {aaa, aaa},\n" 6945 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n" 6946 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6947 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};"); 6948 6949 // No column layout should be used here. 6950 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n" 6951 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};"); 6952 6953 verifyNoCrash("a<,"); 6954 6955 // No braced initializer here. 6956 verifyFormat("void f() {\n" 6957 " struct Dummy {};\n" 6958 " f(v);\n" 6959 "}"); 6960 6961 // Long lists should be formatted in columns even if they are nested. 6962 verifyFormat( 6963 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6964 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6965 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6966 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6967 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6968 " 1, 22, 333, 4444, 55555, 666666, 7777777});"); 6969 6970 // Allow "single-column" layout even if that violates the column limit. There 6971 // isn't going to be a better way. 6972 verifyFormat("std::vector<int> a = {\n" 6973 " aaaaaaaa,\n" 6974 " aaaaaaaa,\n" 6975 " aaaaaaaa,\n" 6976 " aaaaaaaa,\n" 6977 " aaaaaaaaaa,\n" 6978 " aaaaaaaa,\n" 6979 " aaaaaaaaaaaaaaaaaaaaaaaaaaa};", 6980 getLLVMStyleWithColumns(30)); 6981 verifyFormat("vector<int> aaaa = {\n" 6982 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6983 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6984 " aaaaaa.aaaaaaa,\n" 6985 " aaaaaa.aaaaaaa,\n" 6986 " aaaaaa.aaaaaaa,\n" 6987 " aaaaaa.aaaaaaa,\n" 6988 "};"); 6989 6990 // Don't create hanging lists. 6991 verifyFormat("someFunction(Param, {List1, List2,\n" 6992 " List3});", 6993 getLLVMStyleWithColumns(35)); 6994 verifyFormat("someFunction(Param, Param,\n" 6995 " {List1, List2,\n" 6996 " List3});", 6997 getLLVMStyleWithColumns(35)); 6998 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n" 6999 " aaaaaaaaaaaaaaaaaaaaaaa);"); 7000 } 7001 7002 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 7003 FormatStyle DoNotMerge = getLLVMStyle(); 7004 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 7005 7006 verifyFormat("void f() { return 42; }"); 7007 verifyFormat("void f() {\n" 7008 " return 42;\n" 7009 "}", 7010 DoNotMerge); 7011 verifyFormat("void f() {\n" 7012 " // Comment\n" 7013 "}"); 7014 verifyFormat("{\n" 7015 "#error {\n" 7016 " int a;\n" 7017 "}"); 7018 verifyFormat("{\n" 7019 " int a;\n" 7020 "#error {\n" 7021 "}"); 7022 verifyFormat("void f() {} // comment"); 7023 verifyFormat("void f() { int a; } // comment"); 7024 verifyFormat("void f() {\n" 7025 "} // comment", 7026 DoNotMerge); 7027 verifyFormat("void f() {\n" 7028 " int a;\n" 7029 "} // comment", 7030 DoNotMerge); 7031 verifyFormat("void f() {\n" 7032 "} // comment", 7033 getLLVMStyleWithColumns(15)); 7034 7035 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 7036 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 7037 7038 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 7039 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 7040 verifyFormat("class C {\n" 7041 " C()\n" 7042 " : iiiiiiii(nullptr),\n" 7043 " kkkkkkk(nullptr),\n" 7044 " mmmmmmm(nullptr),\n" 7045 " nnnnnnn(nullptr) {}\n" 7046 "};", 7047 getGoogleStyle()); 7048 7049 FormatStyle NoColumnLimit = getLLVMStyle(); 7050 NoColumnLimit.ColumnLimit = 0; 7051 EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit)); 7052 EXPECT_EQ("class C {\n" 7053 " A() : b(0) {}\n" 7054 "};", 7055 format("class C{A():b(0){}};", NoColumnLimit)); 7056 EXPECT_EQ("A()\n" 7057 " : b(0) {\n" 7058 "}", 7059 format("A()\n:b(0)\n{\n}", NoColumnLimit)); 7060 7061 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit; 7062 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine = 7063 FormatStyle::SFS_None; 7064 EXPECT_EQ("A()\n" 7065 " : b(0) {\n" 7066 "}", 7067 format("A():b(0){}", DoNotMergeNoColumnLimit)); 7068 EXPECT_EQ("A()\n" 7069 " : b(0) {\n" 7070 "}", 7071 format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit)); 7072 7073 verifyFormat("#define A \\\n" 7074 " void f() { \\\n" 7075 " int i; \\\n" 7076 " }", 7077 getLLVMStyleWithColumns(20)); 7078 verifyFormat("#define A \\\n" 7079 " void f() { int i; }", 7080 getLLVMStyleWithColumns(21)); 7081 verifyFormat("#define A \\\n" 7082 " void f() { \\\n" 7083 " int i; \\\n" 7084 " } \\\n" 7085 " int j;", 7086 getLLVMStyleWithColumns(22)); 7087 verifyFormat("#define A \\\n" 7088 " void f() { int i; } \\\n" 7089 " int j;", 7090 getLLVMStyleWithColumns(23)); 7091 } 7092 7093 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) { 7094 FormatStyle MergeEmptyOnly = getLLVMStyle(); 7095 MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 7096 verifyFormat("class C {\n" 7097 " int f() {}\n" 7098 "};", 7099 MergeEmptyOnly); 7100 verifyFormat("class C {\n" 7101 " int f() {\n" 7102 " return 42;\n" 7103 " }\n" 7104 "};", 7105 MergeEmptyOnly); 7106 verifyFormat("int f() {}", MergeEmptyOnly); 7107 verifyFormat("int f() {\n" 7108 " return 42;\n" 7109 "}", 7110 MergeEmptyOnly); 7111 7112 // Also verify behavior when BraceWrapping.AfterFunction = true 7113 MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 7114 MergeEmptyOnly.BraceWrapping.AfterFunction = true; 7115 verifyFormat("int f() {}", MergeEmptyOnly); 7116 verifyFormat("class C {\n" 7117 " int f() {}\n" 7118 "};", 7119 MergeEmptyOnly); 7120 } 7121 7122 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) { 7123 FormatStyle MergeInlineOnly = getLLVMStyle(); 7124 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 7125 verifyFormat("class C {\n" 7126 " int f() { return 42; }\n" 7127 "};", 7128 MergeInlineOnly); 7129 verifyFormat("int f() {\n" 7130 " return 42;\n" 7131 "}", 7132 MergeInlineOnly); 7133 7134 // SFS_Inline implies SFS_Empty 7135 verifyFormat("class C {\n" 7136 " int f() {}\n" 7137 "};", 7138 MergeInlineOnly); 7139 verifyFormat("int f() {}", MergeInlineOnly); 7140 7141 // Also verify behavior when BraceWrapping.AfterFunction = true 7142 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 7143 MergeInlineOnly.BraceWrapping.AfterFunction = true; 7144 verifyFormat("class C {\n" 7145 " int f() { return 42; }\n" 7146 "};", 7147 MergeInlineOnly); 7148 verifyFormat("int f()\n" 7149 "{\n" 7150 " return 42;\n" 7151 "}", 7152 MergeInlineOnly); 7153 7154 // SFS_Inline implies SFS_Empty 7155 verifyFormat("int f() {}", MergeInlineOnly); 7156 verifyFormat("class C {\n" 7157 " int f() {}\n" 7158 "};", 7159 MergeInlineOnly); 7160 } 7161 7162 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) { 7163 FormatStyle MergeInlineOnly = getLLVMStyle(); 7164 MergeInlineOnly.AllowShortFunctionsOnASingleLine = 7165 FormatStyle::SFS_InlineOnly; 7166 verifyFormat("class C {\n" 7167 " int f() { return 42; }\n" 7168 "};", 7169 MergeInlineOnly); 7170 verifyFormat("int f() {\n" 7171 " return 42;\n" 7172 "}", 7173 MergeInlineOnly); 7174 7175 // SFS_InlineOnly does not imply SFS_Empty 7176 verifyFormat("class C {\n" 7177 " int f() {}\n" 7178 "};", 7179 MergeInlineOnly); 7180 verifyFormat("int f() {\n" 7181 "}", 7182 MergeInlineOnly); 7183 7184 // Also verify behavior when BraceWrapping.AfterFunction = true 7185 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 7186 MergeInlineOnly.BraceWrapping.AfterFunction = true; 7187 verifyFormat("class C {\n" 7188 " int f() { return 42; }\n" 7189 "};", 7190 MergeInlineOnly); 7191 verifyFormat("int f()\n" 7192 "{\n" 7193 " return 42;\n" 7194 "}", 7195 MergeInlineOnly); 7196 7197 // SFS_InlineOnly does not imply SFS_Empty 7198 verifyFormat("int f()\n" 7199 "{\n" 7200 "}", 7201 MergeInlineOnly); 7202 verifyFormat("class C {\n" 7203 " int f() {}\n" 7204 "};", 7205 MergeInlineOnly); 7206 } 7207 7208 TEST_F(FormatTest, SplitEmptyFunction) { 7209 FormatStyle Style = getLLVMStyle(); 7210 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 7211 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7212 Style.BraceWrapping.AfterFunction = true; 7213 Style.BraceWrapping.SplitEmptyFunction = false; 7214 Style.ColumnLimit = 40; 7215 7216 verifyFormat("int f()\n" 7217 "{}", 7218 Style); 7219 verifyFormat("int f()\n" 7220 "{\n" 7221 " return 42;\n" 7222 "}", 7223 Style); 7224 verifyFormat("int f()\n" 7225 "{\n" 7226 " // some comment\n" 7227 "}", 7228 Style); 7229 7230 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 7231 verifyFormat("int f() {}", Style); 7232 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 7233 "{}", 7234 Style); 7235 verifyFormat("int f()\n" 7236 "{\n" 7237 " return 0;\n" 7238 "}", 7239 Style); 7240 7241 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 7242 verifyFormat("class Foo {\n" 7243 " int f() {}\n" 7244 "};\n", 7245 Style); 7246 verifyFormat("class Foo {\n" 7247 " int f() { return 0; }\n" 7248 "};\n", 7249 Style); 7250 verifyFormat("class Foo {\n" 7251 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 7252 " {}\n" 7253 "};\n", 7254 Style); 7255 verifyFormat("class Foo {\n" 7256 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 7257 " {\n" 7258 " return 0;\n" 7259 " }\n" 7260 "};\n", 7261 Style); 7262 7263 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 7264 verifyFormat("int f() {}", Style); 7265 verifyFormat("int f() { return 0; }", Style); 7266 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 7267 "{}", 7268 Style); 7269 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 7270 "{\n" 7271 " return 0;\n" 7272 "}", 7273 Style); 7274 } 7275 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) { 7276 FormatStyle Style = getLLVMStyle(); 7277 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 7278 verifyFormat("#ifdef A\n" 7279 "int f() {}\n" 7280 "#else\n" 7281 "int g() {}\n" 7282 "#endif", 7283 Style); 7284 } 7285 7286 TEST_F(FormatTest, SplitEmptyClass) { 7287 FormatStyle Style = getLLVMStyle(); 7288 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7289 Style.BraceWrapping.AfterClass = true; 7290 Style.BraceWrapping.SplitEmptyRecord = false; 7291 7292 verifyFormat("class Foo\n" 7293 "{};", 7294 Style); 7295 verifyFormat("/* something */ class Foo\n" 7296 "{};", 7297 Style); 7298 verifyFormat("template <typename X> class Foo\n" 7299 "{};", 7300 Style); 7301 verifyFormat("class Foo\n" 7302 "{\n" 7303 " Foo();\n" 7304 "};", 7305 Style); 7306 verifyFormat("typedef class Foo\n" 7307 "{\n" 7308 "} Foo_t;", 7309 Style); 7310 } 7311 7312 TEST_F(FormatTest, SplitEmptyStruct) { 7313 FormatStyle Style = getLLVMStyle(); 7314 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7315 Style.BraceWrapping.AfterStruct = true; 7316 Style.BraceWrapping.SplitEmptyRecord = false; 7317 7318 verifyFormat("struct Foo\n" 7319 "{};", 7320 Style); 7321 verifyFormat("/* something */ struct Foo\n" 7322 "{};", 7323 Style); 7324 verifyFormat("template <typename X> struct Foo\n" 7325 "{};", 7326 Style); 7327 verifyFormat("struct Foo\n" 7328 "{\n" 7329 " Foo();\n" 7330 "};", 7331 Style); 7332 verifyFormat("typedef struct Foo\n" 7333 "{\n" 7334 "} Foo_t;", 7335 Style); 7336 //typedef struct Bar {} Bar_t; 7337 } 7338 7339 TEST_F(FormatTest, SplitEmptyUnion) { 7340 FormatStyle Style = getLLVMStyle(); 7341 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7342 Style.BraceWrapping.AfterUnion = true; 7343 Style.BraceWrapping.SplitEmptyRecord = false; 7344 7345 verifyFormat("union Foo\n" 7346 "{};", 7347 Style); 7348 verifyFormat("/* something */ union Foo\n" 7349 "{};", 7350 Style); 7351 verifyFormat("union Foo\n" 7352 "{\n" 7353 " A,\n" 7354 "};", 7355 Style); 7356 verifyFormat("typedef union Foo\n" 7357 "{\n" 7358 "} Foo_t;", 7359 Style); 7360 } 7361 7362 TEST_F(FormatTest, SplitEmptyNamespace) { 7363 FormatStyle Style = getLLVMStyle(); 7364 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7365 Style.BraceWrapping.AfterNamespace = true; 7366 Style.BraceWrapping.SplitEmptyNamespace = false; 7367 7368 verifyFormat("namespace Foo\n" 7369 "{};", 7370 Style); 7371 verifyFormat("/* something */ namespace Foo\n" 7372 "{};", 7373 Style); 7374 verifyFormat("inline namespace Foo\n" 7375 "{};", 7376 Style); 7377 verifyFormat("namespace Foo\n" 7378 "{\n" 7379 "void Bar();\n" 7380 "};", 7381 Style); 7382 } 7383 7384 TEST_F(FormatTest, NeverMergeShortRecords) { 7385 FormatStyle Style = getLLVMStyle(); 7386 7387 verifyFormat("class Foo {\n" 7388 " Foo();\n" 7389 "};", 7390 Style); 7391 verifyFormat("typedef class Foo {\n" 7392 " Foo();\n" 7393 "} Foo_t;", 7394 Style); 7395 verifyFormat("struct Foo {\n" 7396 " Foo();\n" 7397 "};", 7398 Style); 7399 verifyFormat("typedef struct Foo {\n" 7400 " Foo();\n" 7401 "} Foo_t;", 7402 Style); 7403 verifyFormat("union Foo {\n" 7404 " A,\n" 7405 "};", 7406 Style); 7407 verifyFormat("typedef union Foo {\n" 7408 " A,\n" 7409 "} Foo_t;", 7410 Style); 7411 verifyFormat("namespace Foo {\n" 7412 "void Bar();\n" 7413 "};", 7414 Style); 7415 7416 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7417 Style.BraceWrapping.AfterClass = true; 7418 Style.BraceWrapping.AfterStruct = true; 7419 Style.BraceWrapping.AfterUnion = true; 7420 Style.BraceWrapping.AfterNamespace = true; 7421 verifyFormat("class Foo\n" 7422 "{\n" 7423 " Foo();\n" 7424 "};", 7425 Style); 7426 verifyFormat("typedef class Foo\n" 7427 "{\n" 7428 " Foo();\n" 7429 "} Foo_t;", 7430 Style); 7431 verifyFormat("struct Foo\n" 7432 "{\n" 7433 " Foo();\n" 7434 "};", 7435 Style); 7436 verifyFormat("typedef struct Foo\n" 7437 "{\n" 7438 " Foo();\n" 7439 "} Foo_t;", 7440 Style); 7441 verifyFormat("union Foo\n" 7442 "{\n" 7443 " A,\n" 7444 "};", 7445 Style); 7446 verifyFormat("typedef union Foo\n" 7447 "{\n" 7448 " A,\n" 7449 "} Foo_t;", 7450 Style); 7451 verifyFormat("namespace Foo\n" 7452 "{\n" 7453 "void Bar();\n" 7454 "};", 7455 Style); 7456 } 7457 7458 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 7459 // Elaborate type variable declarations. 7460 verifyFormat("struct foo a = {bar};\nint n;"); 7461 verifyFormat("class foo a = {bar};\nint n;"); 7462 verifyFormat("union foo a = {bar};\nint n;"); 7463 7464 // Elaborate types inside function definitions. 7465 verifyFormat("struct foo f() {}\nint n;"); 7466 verifyFormat("class foo f() {}\nint n;"); 7467 verifyFormat("union foo f() {}\nint n;"); 7468 7469 // Templates. 7470 verifyFormat("template <class X> void f() {}\nint n;"); 7471 verifyFormat("template <struct X> void f() {}\nint n;"); 7472 verifyFormat("template <union X> void f() {}\nint n;"); 7473 7474 // Actual definitions... 7475 verifyFormat("struct {\n} n;"); 7476 verifyFormat( 7477 "template <template <class T, class Y>, class Z> class X {\n} n;"); 7478 verifyFormat("union Z {\n int n;\n} x;"); 7479 verifyFormat("class MACRO Z {\n} n;"); 7480 verifyFormat("class MACRO(X) Z {\n} n;"); 7481 verifyFormat("class __attribute__(X) Z {\n} n;"); 7482 verifyFormat("class __declspec(X) Z {\n} n;"); 7483 verifyFormat("class A##B##C {\n} n;"); 7484 verifyFormat("class alignas(16) Z {\n} n;"); 7485 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;"); 7486 verifyFormat("class MACROA MACRO(X) Z {\n} n;"); 7487 7488 // Redefinition from nested context: 7489 verifyFormat("class A::B::C {\n} n;"); 7490 7491 // Template definitions. 7492 verifyFormat( 7493 "template <typename F>\n" 7494 "Matcher(const Matcher<F> &Other,\n" 7495 " typename enable_if_c<is_base_of<F, T>::value &&\n" 7496 " !is_same<F, T>::value>::type * = 0)\n" 7497 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}"); 7498 7499 // FIXME: This is still incorrectly handled at the formatter side. 7500 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};"); 7501 verifyFormat("int i = SomeFunction(a<b, a> b);"); 7502 7503 // FIXME: 7504 // This now gets parsed incorrectly as class definition. 7505 // verifyFormat("class A<int> f() {\n}\nint n;"); 7506 7507 // Elaborate types where incorrectly parsing the structural element would 7508 // break the indent. 7509 verifyFormat("if (true)\n" 7510 " class X x;\n" 7511 "else\n" 7512 " f();\n"); 7513 7514 // This is simply incomplete. Formatting is not important, but must not crash. 7515 verifyFormat("class A:"); 7516 } 7517 7518 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 7519 EXPECT_EQ("#error Leave all white!!!!! space* alone!\n", 7520 format("#error Leave all white!!!!! space* alone!\n")); 7521 EXPECT_EQ( 7522 "#warning Leave all white!!!!! space* alone!\n", 7523 format("#warning Leave all white!!!!! space* alone!\n")); 7524 EXPECT_EQ("#error 1", format(" # error 1")); 7525 EXPECT_EQ("#warning 1", format(" # warning 1")); 7526 } 7527 7528 TEST_F(FormatTest, FormatHashIfExpressions) { 7529 verifyFormat("#if AAAA && BBBB"); 7530 verifyFormat("#if (AAAA && BBBB)"); 7531 verifyFormat("#elif (AAAA && BBBB)"); 7532 // FIXME: Come up with a better indentation for #elif. 7533 verifyFormat( 7534 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" 7535 " defined(BBBBBBBB)\n" 7536 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" 7537 " defined(BBBBBBBB)\n" 7538 "#endif", 7539 getLLVMStyleWithColumns(65)); 7540 } 7541 7542 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 7543 FormatStyle AllowsMergedIf = getGoogleStyle(); 7544 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 7545 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 7546 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 7547 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 7548 EXPECT_EQ("if (true) return 42;", 7549 format("if (true)\nreturn 42;", AllowsMergedIf)); 7550 FormatStyle ShortMergedIf = AllowsMergedIf; 7551 ShortMergedIf.ColumnLimit = 25; 7552 verifyFormat("#define A \\\n" 7553 " if (true) return 42;", 7554 ShortMergedIf); 7555 verifyFormat("#define A \\\n" 7556 " f(); \\\n" 7557 " if (true)\n" 7558 "#define B", 7559 ShortMergedIf); 7560 verifyFormat("#define A \\\n" 7561 " f(); \\\n" 7562 " if (true)\n" 7563 "g();", 7564 ShortMergedIf); 7565 verifyFormat("{\n" 7566 "#ifdef A\n" 7567 " // Comment\n" 7568 " if (true) continue;\n" 7569 "#endif\n" 7570 " // Comment\n" 7571 " if (true) continue;\n" 7572 "}", 7573 ShortMergedIf); 7574 ShortMergedIf.ColumnLimit = 33; 7575 verifyFormat("#define A \\\n" 7576 " if constexpr (true) return 42;", 7577 ShortMergedIf); 7578 ShortMergedIf.ColumnLimit = 29; 7579 verifyFormat("#define A \\\n" 7580 " if (aaaaaaaaaa) return 1; \\\n" 7581 " return 2;", 7582 ShortMergedIf); 7583 ShortMergedIf.ColumnLimit = 28; 7584 verifyFormat("#define A \\\n" 7585 " if (aaaaaaaaaa) \\\n" 7586 " return 1; \\\n" 7587 " return 2;", 7588 ShortMergedIf); 7589 verifyFormat("#define A \\\n" 7590 " if constexpr (aaaaaaa) \\\n" 7591 " return 1; \\\n" 7592 " return 2;", 7593 ShortMergedIf); 7594 } 7595 7596 TEST_F(FormatTest, FormatStarDependingOnContext) { 7597 verifyFormat("void f(int *a);"); 7598 verifyFormat("void f() { f(fint * b); }"); 7599 verifyFormat("class A {\n void f(int *a);\n};"); 7600 verifyFormat("class A {\n int *a;\n};"); 7601 verifyFormat("namespace a {\n" 7602 "namespace b {\n" 7603 "class A {\n" 7604 " void f() {}\n" 7605 " int *a;\n" 7606 "};\n" 7607 "} // namespace b\n" 7608 "} // namespace a"); 7609 } 7610 7611 TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 7612 verifyFormat("while"); 7613 verifyFormat("operator"); 7614 } 7615 7616 TEST_F(FormatTest, SkipsDeeplyNestedLines) { 7617 // This code would be painfully slow to format if we didn't skip it. 7618 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 7619 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 7620 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 7621 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 7622 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 7623 "A(1, 1)\n" 7624 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x 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 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7629 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7630 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7631 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7632 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7633 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n"); 7634 // Deeply nested part is untouched, rest is formatted. 7635 EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n", 7636 format(std::string("int i;\n") + Code + "int j;\n", 7637 getLLVMStyle(), SC_ExpectIncomplete)); 7638 } 7639 7640 //===----------------------------------------------------------------------===// 7641 // Objective-C tests. 7642 //===----------------------------------------------------------------------===// 7643 7644 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 7645 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 7646 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 7647 format("-(NSUInteger)indexOfObject:(id)anObject;")); 7648 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 7649 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 7650 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 7651 format("-(NSInteger)Method3:(id)anObject;")); 7652 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 7653 format("-(NSInteger)Method4:(id)anObject;")); 7654 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 7655 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 7656 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 7657 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 7658 EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject " 7659 "forAllCells:(BOOL)flag;", 7660 format("- (void)sendAction:(SEL)aSelector to:(id)anObject " 7661 "forAllCells:(BOOL)flag;")); 7662 7663 // Very long objectiveC method declaration. 7664 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 7665 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;"); 7666 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 7667 " inRange:(NSRange)range\n" 7668 " outRange:(NSRange)out_range\n" 7669 " outRange1:(NSRange)out_range1\n" 7670 " outRange2:(NSRange)out_range2\n" 7671 " outRange3:(NSRange)out_range3\n" 7672 " outRange4:(NSRange)out_range4\n" 7673 " outRange5:(NSRange)out_range5\n" 7674 " outRange6:(NSRange)out_range6\n" 7675 " outRange7:(NSRange)out_range7\n" 7676 " outRange8:(NSRange)out_range8\n" 7677 " outRange9:(NSRange)out_range9;"); 7678 7679 // When the function name has to be wrapped. 7680 FormatStyle Style = getLLVMStyle(); 7681 // ObjC ignores IndentWrappedFunctionNames when wrapping methods 7682 // and always indents instead. 7683 Style.IndentWrappedFunctionNames = false; 7684 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 7685 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 7686 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 7687 "}", 7688 Style); 7689 Style.IndentWrappedFunctionNames = true; 7690 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 7691 " veryLooooooooooongName:(NSString)cccccccccccccc\n" 7692 " anotherName:(NSString)dddddddddddddd {\n" 7693 "}", 7694 Style); 7695 7696 verifyFormat("- (int)sum:(vector<int>)numbers;"); 7697 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 7698 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 7699 // protocol lists (but not for template classes): 7700 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 7701 7702 verifyFormat("- (int (*)())foo:(int (*)())f;"); 7703 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); 7704 7705 // If there's no return type (very rare in practice!), LLVM and Google style 7706 // agree. 7707 verifyFormat("- foo;"); 7708 verifyFormat("- foo:(int)f;"); 7709 verifyGoogleFormat("- foo:(int)foo;"); 7710 } 7711 7712 7713 TEST_F(FormatTest, BreaksStringLiterals) { 7714 EXPECT_EQ("\"some text \"\n" 7715 "\"other\";", 7716 format("\"some text other\";", getLLVMStyleWithColumns(12))); 7717 EXPECT_EQ("\"some text \"\n" 7718 "\"other\";", 7719 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); 7720 EXPECT_EQ( 7721 "#define A \\\n" 7722 " \"some \" \\\n" 7723 " \"text \" \\\n" 7724 " \"other\";", 7725 format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); 7726 EXPECT_EQ( 7727 "#define A \\\n" 7728 " \"so \" \\\n" 7729 " \"text \" \\\n" 7730 " \"other\";", 7731 format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); 7732 7733 EXPECT_EQ("\"some text\"", 7734 format("\"some text\"", getLLVMStyleWithColumns(1))); 7735 EXPECT_EQ("\"some text\"", 7736 format("\"some text\"", getLLVMStyleWithColumns(11))); 7737 EXPECT_EQ("\"some \"\n" 7738 "\"text\"", 7739 format("\"some text\"", getLLVMStyleWithColumns(10))); 7740 EXPECT_EQ("\"some \"\n" 7741 "\"text\"", 7742 format("\"some text\"", getLLVMStyleWithColumns(7))); 7743 EXPECT_EQ("\"some\"\n" 7744 "\" tex\"\n" 7745 "\"t\"", 7746 format("\"some text\"", getLLVMStyleWithColumns(6))); 7747 EXPECT_EQ("\"some\"\n" 7748 "\" tex\"\n" 7749 "\" and\"", 7750 format("\"some tex and\"", getLLVMStyleWithColumns(6))); 7751 EXPECT_EQ("\"some\"\n" 7752 "\"/tex\"\n" 7753 "\"/and\"", 7754 format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); 7755 7756 EXPECT_EQ("variable =\n" 7757 " \"long string \"\n" 7758 " \"literal\";", 7759 format("variable = \"long string literal\";", 7760 getLLVMStyleWithColumns(20))); 7761 7762 EXPECT_EQ("variable = f(\n" 7763 " \"long string \"\n" 7764 " \"literal\",\n" 7765 " short,\n" 7766 " loooooooooooooooooooong);", 7767 format("variable = f(\"long string literal\", short, " 7768 "loooooooooooooooooooong);", 7769 getLLVMStyleWithColumns(20))); 7770 7771 EXPECT_EQ( 7772 "f(g(\"long string \"\n" 7773 " \"literal\"),\n" 7774 " b);", 7775 format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20))); 7776 EXPECT_EQ("f(g(\"long string \"\n" 7777 " \"literal\",\n" 7778 " a),\n" 7779 " b);", 7780 format("f(g(\"long string literal\", a), b);", 7781 getLLVMStyleWithColumns(20))); 7782 EXPECT_EQ( 7783 "f(\"one two\".split(\n" 7784 " variable));", 7785 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); 7786 EXPECT_EQ("f(\"one two three four five six \"\n" 7787 " \"seven\".split(\n" 7788 " really_looooong_variable));", 7789 format("f(\"one two three four five six seven\"." 7790 "split(really_looooong_variable));", 7791 getLLVMStyleWithColumns(33))); 7792 7793 EXPECT_EQ("f(\"some \"\n" 7794 " \"text\",\n" 7795 " other);", 7796 format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); 7797 7798 // Only break as a last resort. 7799 verifyFormat( 7800 "aaaaaaaaaaaaaaaaaaaa(\n" 7801 " aaaaaaaaaaaaaaaaaaaa,\n" 7802 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); 7803 7804 EXPECT_EQ("\"splitmea\"\n" 7805 "\"trandomp\"\n" 7806 "\"oint\"", 7807 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); 7808 7809 EXPECT_EQ("\"split/\"\n" 7810 "\"pathat/\"\n" 7811 "\"slashes\"", 7812 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 7813 7814 EXPECT_EQ("\"split/\"\n" 7815 "\"pathat/\"\n" 7816 "\"slashes\"", 7817 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 7818 EXPECT_EQ("\"split at \"\n" 7819 "\"spaces/at/\"\n" 7820 "\"slashes.at.any$\"\n" 7821 "\"non-alphanumeric%\"\n" 7822 "\"1111111111characte\"\n" 7823 "\"rs\"", 7824 format("\"split at " 7825 "spaces/at/" 7826 "slashes.at." 7827 "any$non-" 7828 "alphanumeric%" 7829 "1111111111characte" 7830 "rs\"", 7831 getLLVMStyleWithColumns(20))); 7832 7833 // Verify that splitting the strings understands 7834 // Style::AlwaysBreakBeforeMultilineStrings. 7835 EXPECT_EQ( 7836 "aaaaaaaaaaaa(\n" 7837 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" 7838 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", 7839 format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa " 7840 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 7841 "aaaaaaaaaaaaaaaaaaaaaa\");", 7842 getGoogleStyle())); 7843 EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7844 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", 7845 format("return \"aaaaaaaaaaaaaaaaaaaaaa " 7846 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 7847 "aaaaaaaaaaaaaaaaaaaaaa\";", 7848 getGoogleStyle())); 7849 EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7850 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 7851 format("llvm::outs() << " 7852 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa" 7853 "aaaaaaaaaaaaaaaaaaa\";")); 7854 EXPECT_EQ("ffff(\n" 7855 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7856 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 7857 format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " 7858 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 7859 getGoogleStyle())); 7860 7861 FormatStyle Style = getLLVMStyleWithColumns(12); 7862 Style.BreakStringLiterals = false; 7863 EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style)); 7864 7865 FormatStyle AlignLeft = getLLVMStyleWithColumns(12); 7866 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 7867 EXPECT_EQ("#define A \\\n" 7868 " \"some \" \\\n" 7869 " \"text \" \\\n" 7870 " \"other\";", 7871 format("#define A \"some text other\";", AlignLeft)); 7872 } 7873 7874 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) { 7875 EXPECT_EQ("C a = \"some more \"\n" 7876 " \"text\";", 7877 format("C a = \"some more text\";", getLLVMStyleWithColumns(18))); 7878 } 7879 7880 TEST_F(FormatTest, FullyRemoveEmptyLines) { 7881 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80); 7882 NoEmptyLines.MaxEmptyLinesToKeep = 0; 7883 EXPECT_EQ("int i = a(b());", 7884 format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines)); 7885 } 7886 7887 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) { 7888 EXPECT_EQ( 7889 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 7890 "(\n" 7891 " \"x\t\");", 7892 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 7893 "aaaaaaa(" 7894 "\"x\t\");")); 7895 } 7896 7897 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) { 7898 EXPECT_EQ( 7899 "u8\"utf8 string \"\n" 7900 "u8\"literal\";", 7901 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16))); 7902 EXPECT_EQ( 7903 "u\"utf16 string \"\n" 7904 "u\"literal\";", 7905 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16))); 7906 EXPECT_EQ( 7907 "U\"utf32 string \"\n" 7908 "U\"literal\";", 7909 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16))); 7910 EXPECT_EQ("L\"wide string \"\n" 7911 "L\"literal\";", 7912 format("L\"wide string literal\";", getGoogleStyleWithColumns(16))); 7913 EXPECT_EQ("@\"NSString \"\n" 7914 "@\"literal\";", 7915 format("@\"NSString literal\";", getGoogleStyleWithColumns(19))); 7916 verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26)); 7917 7918 // This input makes clang-format try to split the incomplete unicode escape 7919 // sequence, which used to lead to a crasher. 7920 verifyNoCrash( 7921 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 7922 getLLVMStyleWithColumns(60)); 7923 } 7924 7925 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) { 7926 FormatStyle Style = getGoogleStyleWithColumns(15); 7927 EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style)); 7928 EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style)); 7929 EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style)); 7930 EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style)); 7931 EXPECT_EQ("u8R\"x(raw literal)x\";", 7932 format("u8R\"x(raw literal)x\";", Style)); 7933 } 7934 7935 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { 7936 FormatStyle Style = getLLVMStyleWithColumns(20); 7937 EXPECT_EQ( 7938 "_T(\"aaaaaaaaaaaaaa\")\n" 7939 "_T(\"aaaaaaaaaaaaaa\")\n" 7940 "_T(\"aaaaaaaaaaaa\")", 7941 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style)); 7942 EXPECT_EQ("f(x,\n" 7943 " _T(\"aaaaaaaaaaaa\")\n" 7944 " _T(\"aaa\"),\n" 7945 " z);", 7946 format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style)); 7947 7948 // FIXME: Handle embedded spaces in one iteration. 7949 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n" 7950 // "_T(\"aaaaaaaaaaaaa\")\n" 7951 // "_T(\"aaaaaaaaaaaaa\")\n" 7952 // "_T(\"a\")", 7953 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 7954 // getLLVMStyleWithColumns(20))); 7955 EXPECT_EQ( 7956 "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 7957 format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style)); 7958 EXPECT_EQ("f(\n" 7959 "#if !TEST\n" 7960 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 7961 "#endif\n" 7962 ");", 7963 format("f(\n" 7964 "#if !TEST\n" 7965 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 7966 "#endif\n" 7967 ");")); 7968 EXPECT_EQ("f(\n" 7969 "\n" 7970 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));", 7971 format("f(\n" 7972 "\n" 7973 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));")); 7974 } 7975 7976 TEST_F(FormatTest, BreaksStringLiteralOperands) { 7977 // In a function call with two operands, the second can be broken with no line 7978 // break before it. 7979 EXPECT_EQ("func(a, \"long long \"\n" 7980 " \"long long\");", 7981 format("func(a, \"long long long long\");", 7982 getLLVMStyleWithColumns(24))); 7983 // In a function call with three operands, the second must be broken with a 7984 // line break before it. 7985 EXPECT_EQ("func(a,\n" 7986 " \"long long long \"\n" 7987 " \"long\",\n" 7988 " c);", 7989 format("func(a, \"long long long long\", c);", 7990 getLLVMStyleWithColumns(24))); 7991 // In a function call with three operands, the third must be broken with a 7992 // line break before it. 7993 EXPECT_EQ("func(a, b,\n" 7994 " \"long long long \"\n" 7995 " \"long\");", 7996 format("func(a, b, \"long long long long\");", 7997 getLLVMStyleWithColumns(24))); 7998 // In a function call with three operands, both the second and the third must 7999 // be broken with a line break before them. 8000 EXPECT_EQ("func(a,\n" 8001 " \"long long long \"\n" 8002 " \"long\",\n" 8003 " \"long long long \"\n" 8004 " \"long\");", 8005 format("func(a, \"long long long long\", \"long long long long\");", 8006 getLLVMStyleWithColumns(24))); 8007 // In a chain of << with two operands, the second can be broken with no line 8008 // break before it. 8009 EXPECT_EQ("a << \"line line \"\n" 8010 " \"line\";", 8011 format("a << \"line line line\";", 8012 getLLVMStyleWithColumns(20))); 8013 // In a chain of << with three operands, the second can be broken with no line 8014 // break before it. 8015 EXPECT_EQ("abcde << \"line \"\n" 8016 " \"line line\"\n" 8017 " << c;", 8018 format("abcde << \"line line line\" << c;", 8019 getLLVMStyleWithColumns(20))); 8020 // In a chain of << with three operands, the third must be broken with a line 8021 // break before it. 8022 EXPECT_EQ("a << b\n" 8023 " << \"line line \"\n" 8024 " \"line\";", 8025 format("a << b << \"line line line\";", 8026 getLLVMStyleWithColumns(20))); 8027 // In a chain of << with three operands, the second can be broken with no line 8028 // break before it and the third must be broken with a line break before it. 8029 EXPECT_EQ("abcd << \"line line \"\n" 8030 " \"line\"\n" 8031 " << \"line line \"\n" 8032 " \"line\";", 8033 format("abcd << \"line line line\" << \"line line line\";", 8034 getLLVMStyleWithColumns(20))); 8035 // In a chain of binary operators with two operands, the second can be broken 8036 // with no line break before it. 8037 EXPECT_EQ("abcd + \"line line \"\n" 8038 " \"line line\";", 8039 format("abcd + \"line line line line\";", 8040 getLLVMStyleWithColumns(20))); 8041 // In a chain of binary operators with three operands, the second must be 8042 // broken with a line break before it. 8043 EXPECT_EQ("abcd +\n" 8044 " \"line line \"\n" 8045 " \"line line\" +\n" 8046 " e;", 8047 format("abcd + \"line line line line\" + e;", 8048 getLLVMStyleWithColumns(20))); 8049 // In a function call with two operands, with AlignAfterOpenBracket enabled, 8050 // the first must be broken with a line break before it. 8051 FormatStyle Style = getLLVMStyleWithColumns(25); 8052 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 8053 EXPECT_EQ("someFunction(\n" 8054 " \"long long long \"\n" 8055 " \"long\",\n" 8056 " a);", 8057 format("someFunction(\"long long long long\", a);", Style)); 8058 } 8059 8060 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { 8061 EXPECT_EQ( 8062 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8063 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8064 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 8065 format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8066 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8067 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";")); 8068 } 8069 8070 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { 8071 EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);", 8072 format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle())); 8073 EXPECT_EQ("fffffffffff(g(R\"x(\n" 8074 "multiline raw string literal xxxxxxxxxxxxxx\n" 8075 ")x\",\n" 8076 " a),\n" 8077 " b);", 8078 format("fffffffffff(g(R\"x(\n" 8079 "multiline raw string literal xxxxxxxxxxxxxx\n" 8080 ")x\", a), b);", 8081 getGoogleStyleWithColumns(20))); 8082 EXPECT_EQ("fffffffffff(\n" 8083 " g(R\"x(qqq\n" 8084 "multiline raw string literal xxxxxxxxxxxxxx\n" 8085 ")x\",\n" 8086 " a),\n" 8087 " b);", 8088 format("fffffffffff(g(R\"x(qqq\n" 8089 "multiline raw string literal xxxxxxxxxxxxxx\n" 8090 ")x\", a), b);", 8091 getGoogleStyleWithColumns(20))); 8092 8093 EXPECT_EQ("fffffffffff(R\"x(\n" 8094 "multiline raw string literal xxxxxxxxxxxxxx\n" 8095 ")x\");", 8096 format("fffffffffff(R\"x(\n" 8097 "multiline raw string literal xxxxxxxxxxxxxx\n" 8098 ")x\");", 8099 getGoogleStyleWithColumns(20))); 8100 EXPECT_EQ("fffffffffff(R\"x(\n" 8101 "multiline raw string literal xxxxxxxxxxxxxx\n" 8102 ")x\" + bbbbbb);", 8103 format("fffffffffff(R\"x(\n" 8104 "multiline raw string literal xxxxxxxxxxxxxx\n" 8105 ")x\" + bbbbbb);", 8106 getGoogleStyleWithColumns(20))); 8107 EXPECT_EQ("fffffffffff(\n" 8108 " R\"x(\n" 8109 "multiline raw string literal xxxxxxxxxxxxxx\n" 8110 ")x\" +\n" 8111 " bbbbbb);", 8112 format("fffffffffff(\n" 8113 " R\"x(\n" 8114 "multiline raw string literal xxxxxxxxxxxxxx\n" 8115 ")x\" + bbbbbb);", 8116 getGoogleStyleWithColumns(20))); 8117 EXPECT_EQ("fffffffffff(R\"(single line raw string)\" + bbbbbb);", 8118 format("fffffffffff(\n" 8119 " R\"(single line raw string)\" + bbbbbb);")); 8120 } 8121 8122 TEST_F(FormatTest, SkipsUnknownStringLiterals) { 8123 verifyFormat("string a = \"unterminated;"); 8124 EXPECT_EQ("function(\"unterminated,\n" 8125 " OtherParameter);", 8126 format("function( \"unterminated,\n" 8127 " OtherParameter);")); 8128 } 8129 8130 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { 8131 FormatStyle Style = getLLVMStyle(); 8132 Style.Standard = FormatStyle::LS_Cpp03; 8133 EXPECT_EQ("#define x(_a) printf(\"foo\" _a);", 8134 format("#define x(_a) printf(\"foo\"_a);", Style)); 8135 } 8136 8137 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); } 8138 8139 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { 8140 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n" 8141 " \"ddeeefff\");", 8142 format("someFunction(\"aaabbbcccdddeeefff\");", 8143 getLLVMStyleWithColumns(25))); 8144 EXPECT_EQ("someFunction1234567890(\n" 8145 " \"aaabbbcccdddeeefff\");", 8146 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 8147 getLLVMStyleWithColumns(26))); 8148 EXPECT_EQ("someFunction1234567890(\n" 8149 " \"aaabbbcccdddeeeff\"\n" 8150 " \"f\");", 8151 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 8152 getLLVMStyleWithColumns(25))); 8153 EXPECT_EQ("someFunction1234567890(\n" 8154 " \"aaabbbcccdddeeeff\"\n" 8155 " \"f\");", 8156 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 8157 getLLVMStyleWithColumns(24))); 8158 EXPECT_EQ("someFunction(\n" 8159 " \"aaabbbcc ddde \"\n" 8160 " \"efff\");", 8161 format("someFunction(\"aaabbbcc ddde efff\");", 8162 getLLVMStyleWithColumns(25))); 8163 EXPECT_EQ("someFunction(\"aaabbbccc \"\n" 8164 " \"ddeeefff\");", 8165 format("someFunction(\"aaabbbccc ddeeefff\");", 8166 getLLVMStyleWithColumns(25))); 8167 EXPECT_EQ("someFunction1234567890(\n" 8168 " \"aaabb \"\n" 8169 " \"cccdddeeefff\");", 8170 format("someFunction1234567890(\"aaabb cccdddeeefff\");", 8171 getLLVMStyleWithColumns(25))); 8172 EXPECT_EQ("#define A \\\n" 8173 " string s = \\\n" 8174 " \"123456789\" \\\n" 8175 " \"0\"; \\\n" 8176 " int i;", 8177 format("#define A string s = \"1234567890\"; int i;", 8178 getLLVMStyleWithColumns(20))); 8179 EXPECT_EQ("someFunction(\n" 8180 " \"aaabbbcc \"\n" 8181 " \"dddeeefff\");", 8182 format("someFunction(\"aaabbbcc dddeeefff\");", 8183 getLLVMStyleWithColumns(25))); 8184 } 8185 8186 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { 8187 EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3))); 8188 EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2))); 8189 EXPECT_EQ("\"test\"\n" 8190 "\"\\n\"", 8191 format("\"test\\n\"", getLLVMStyleWithColumns(7))); 8192 EXPECT_EQ("\"tes\\\\\"\n" 8193 "\"n\"", 8194 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); 8195 EXPECT_EQ("\"\\\\\\\\\"\n" 8196 "\"\\n\"", 8197 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); 8198 EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7))); 8199 EXPECT_EQ("\"\\uff01\"\n" 8200 "\"test\"", 8201 format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); 8202 EXPECT_EQ("\"\\Uff01ff02\"", 8203 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); 8204 EXPECT_EQ("\"\\x000000000001\"\n" 8205 "\"next\"", 8206 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); 8207 EXPECT_EQ("\"\\x000000000001next\"", 8208 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); 8209 EXPECT_EQ("\"\\x000000000001\"", 8210 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); 8211 EXPECT_EQ("\"test\"\n" 8212 "\"\\000000\"\n" 8213 "\"000001\"", 8214 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); 8215 EXPECT_EQ("\"test\\000\"\n" 8216 "\"00000000\"\n" 8217 "\"1\"", 8218 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); 8219 } 8220 8221 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { 8222 verifyFormat("void f() {\n" 8223 " return g() {}\n" 8224 " void h() {}"); 8225 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n" 8226 "g();\n" 8227 "}"); 8228 } 8229 8230 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) { 8231 verifyFormat( 8232 "void f() { return C{param1, param2}.SomeCall(param1, param2); }"); 8233 } 8234 8235 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { 8236 verifyFormat("class X {\n" 8237 " void f() {\n" 8238 " }\n" 8239 "};", 8240 getLLVMStyleWithColumns(12)); 8241 } 8242 8243 TEST_F(FormatTest, ConfigurableIndentWidth) { 8244 FormatStyle EightIndent = getLLVMStyleWithColumns(18); 8245 EightIndent.IndentWidth = 8; 8246 EightIndent.ContinuationIndentWidth = 8; 8247 verifyFormat("void f() {\n" 8248 " someFunction();\n" 8249 " if (true) {\n" 8250 " f();\n" 8251 " }\n" 8252 "}", 8253 EightIndent); 8254 verifyFormat("class X {\n" 8255 " void f() {\n" 8256 " }\n" 8257 "};", 8258 EightIndent); 8259 verifyFormat("int x[] = {\n" 8260 " call(),\n" 8261 " call()};", 8262 EightIndent); 8263 } 8264 8265 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) { 8266 verifyFormat("double\n" 8267 "f();", 8268 getLLVMStyleWithColumns(8)); 8269 } 8270 8271 TEST_F(FormatTest, ConfigurableUseOfTab) { 8272 FormatStyle Tab = getLLVMStyleWithColumns(42); 8273 Tab.IndentWidth = 8; 8274 Tab.UseTab = FormatStyle::UT_Always; 8275 Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left; 8276 8277 EXPECT_EQ("if (aaaaaaaa && // q\n" 8278 " bb)\t\t// w\n" 8279 "\t;", 8280 format("if (aaaaaaaa &&// q\n" 8281 "bb)// w\n" 8282 ";", 8283 Tab)); 8284 EXPECT_EQ("if (aaa && bbb) // w\n" 8285 "\t;", 8286 format("if(aaa&&bbb)// w\n" 8287 ";", 8288 Tab)); 8289 8290 verifyFormat("class X {\n" 8291 "\tvoid f() {\n" 8292 "\t\tsomeFunction(parameter1,\n" 8293 "\t\t\t parameter2);\n" 8294 "\t}\n" 8295 "};", 8296 Tab); 8297 verifyFormat("#define A \\\n" 8298 "\tvoid f() { \\\n" 8299 "\t\tsomeFunction( \\\n" 8300 "\t\t parameter1, \\\n" 8301 "\t\t parameter2); \\\n" 8302 "\t}", 8303 Tab); 8304 8305 Tab.TabWidth = 4; 8306 Tab.IndentWidth = 8; 8307 verifyFormat("class TabWidth4Indent8 {\n" 8308 "\t\tvoid f() {\n" 8309 "\t\t\t\tsomeFunction(parameter1,\n" 8310 "\t\t\t\t\t\t\t parameter2);\n" 8311 "\t\t}\n" 8312 "};", 8313 Tab); 8314 8315 Tab.TabWidth = 4; 8316 Tab.IndentWidth = 4; 8317 verifyFormat("class TabWidth4Indent4 {\n" 8318 "\tvoid f() {\n" 8319 "\t\tsomeFunction(parameter1,\n" 8320 "\t\t\t\t\t parameter2);\n" 8321 "\t}\n" 8322 "};", 8323 Tab); 8324 8325 Tab.TabWidth = 8; 8326 Tab.IndentWidth = 4; 8327 verifyFormat("class TabWidth8Indent4 {\n" 8328 " void f() {\n" 8329 "\tsomeFunction(parameter1,\n" 8330 "\t\t parameter2);\n" 8331 " }\n" 8332 "};", 8333 Tab); 8334 8335 Tab.TabWidth = 8; 8336 Tab.IndentWidth = 8; 8337 EXPECT_EQ("/*\n" 8338 "\t a\t\tcomment\n" 8339 "\t in multiple lines\n" 8340 " */", 8341 format(" /*\t \t \n" 8342 " \t \t a\t\tcomment\t \t\n" 8343 " \t \t in multiple lines\t\n" 8344 " \t */", 8345 Tab)); 8346 8347 Tab.UseTab = FormatStyle::UT_ForIndentation; 8348 verifyFormat("{\n" 8349 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8350 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8351 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8352 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8353 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8354 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8355 "};", 8356 Tab); 8357 verifyFormat("enum AA {\n" 8358 "\ta1, // Force multiple lines\n" 8359 "\ta2,\n" 8360 "\ta3\n" 8361 "};", 8362 Tab); 8363 EXPECT_EQ("if (aaaaaaaa && // q\n" 8364 " bb) // w\n" 8365 "\t;", 8366 format("if (aaaaaaaa &&// q\n" 8367 "bb)// w\n" 8368 ";", 8369 Tab)); 8370 verifyFormat("class X {\n" 8371 "\tvoid f() {\n" 8372 "\t\tsomeFunction(parameter1,\n" 8373 "\t\t parameter2);\n" 8374 "\t}\n" 8375 "};", 8376 Tab); 8377 verifyFormat("{\n" 8378 "\tQ(\n" 8379 "\t {\n" 8380 "\t\t int a;\n" 8381 "\t\t someFunction(aaaaaaaa,\n" 8382 "\t\t bbbbbbb);\n" 8383 "\t },\n" 8384 "\t p);\n" 8385 "}", 8386 Tab); 8387 EXPECT_EQ("{\n" 8388 "\t/* aaaa\n" 8389 "\t bbbb */\n" 8390 "}", 8391 format("{\n" 8392 "/* aaaa\n" 8393 " bbbb */\n" 8394 "}", 8395 Tab)); 8396 EXPECT_EQ("{\n" 8397 "\t/*\n" 8398 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8399 "\t bbbbbbbbbbbbb\n" 8400 "\t*/\n" 8401 "}", 8402 format("{\n" 8403 "/*\n" 8404 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8405 "*/\n" 8406 "}", 8407 Tab)); 8408 EXPECT_EQ("{\n" 8409 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8410 "\t// bbbbbbbbbbbbb\n" 8411 "}", 8412 format("{\n" 8413 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8414 "}", 8415 Tab)); 8416 EXPECT_EQ("{\n" 8417 "\t/*\n" 8418 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8419 "\t bbbbbbbbbbbbb\n" 8420 "\t*/\n" 8421 "}", 8422 format("{\n" 8423 "\t/*\n" 8424 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8425 "\t*/\n" 8426 "}", 8427 Tab)); 8428 EXPECT_EQ("{\n" 8429 "\t/*\n" 8430 "\n" 8431 "\t*/\n" 8432 "}", 8433 format("{\n" 8434 "\t/*\n" 8435 "\n" 8436 "\t*/\n" 8437 "}", 8438 Tab)); 8439 EXPECT_EQ("{\n" 8440 "\t/*\n" 8441 " asdf\n" 8442 "\t*/\n" 8443 "}", 8444 format("{\n" 8445 "\t/*\n" 8446 " asdf\n" 8447 "\t*/\n" 8448 "}", 8449 Tab)); 8450 8451 Tab.UseTab = FormatStyle::UT_Never; 8452 EXPECT_EQ("/*\n" 8453 " a\t\tcomment\n" 8454 " in multiple lines\n" 8455 " */", 8456 format(" /*\t \t \n" 8457 " \t \t a\t\tcomment\t \t\n" 8458 " \t \t in multiple lines\t\n" 8459 " \t */", 8460 Tab)); 8461 EXPECT_EQ("/* some\n" 8462 " comment */", 8463 format(" \t \t /* some\n" 8464 " \t \t comment */", 8465 Tab)); 8466 EXPECT_EQ("int a; /* some\n" 8467 " comment */", 8468 format(" \t \t int a; /* some\n" 8469 " \t \t comment */", 8470 Tab)); 8471 8472 EXPECT_EQ("int a; /* some\n" 8473 "comment */", 8474 format(" \t \t int\ta; /* some\n" 8475 " \t \t comment */", 8476 Tab)); 8477 EXPECT_EQ("f(\"\t\t\"); /* some\n" 8478 " comment */", 8479 format(" \t \t f(\"\t\t\"); /* some\n" 8480 " \t \t comment */", 8481 Tab)); 8482 EXPECT_EQ("{\n" 8483 " /*\n" 8484 " * Comment\n" 8485 " */\n" 8486 " int i;\n" 8487 "}", 8488 format("{\n" 8489 "\t/*\n" 8490 "\t * Comment\n" 8491 "\t */\n" 8492 "\t int i;\n" 8493 "}")); 8494 8495 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 8496 Tab.TabWidth = 8; 8497 Tab.IndentWidth = 8; 8498 EXPECT_EQ("if (aaaaaaaa && // q\n" 8499 " bb) // w\n" 8500 "\t;", 8501 format("if (aaaaaaaa &&// q\n" 8502 "bb)// w\n" 8503 ";", 8504 Tab)); 8505 EXPECT_EQ("if (aaa && bbb) // w\n" 8506 "\t;", 8507 format("if(aaa&&bbb)// w\n" 8508 ";", 8509 Tab)); 8510 verifyFormat("class X {\n" 8511 "\tvoid f() {\n" 8512 "\t\tsomeFunction(parameter1,\n" 8513 "\t\t\t parameter2);\n" 8514 "\t}\n" 8515 "};", 8516 Tab); 8517 verifyFormat("#define A \\\n" 8518 "\tvoid f() { \\\n" 8519 "\t\tsomeFunction( \\\n" 8520 "\t\t parameter1, \\\n" 8521 "\t\t parameter2); \\\n" 8522 "\t}", 8523 Tab); 8524 Tab.TabWidth = 4; 8525 Tab.IndentWidth = 8; 8526 verifyFormat("class TabWidth4Indent8 {\n" 8527 "\t\tvoid f() {\n" 8528 "\t\t\t\tsomeFunction(parameter1,\n" 8529 "\t\t\t\t\t\t\t parameter2);\n" 8530 "\t\t}\n" 8531 "};", 8532 Tab); 8533 Tab.TabWidth = 4; 8534 Tab.IndentWidth = 4; 8535 verifyFormat("class TabWidth4Indent4 {\n" 8536 "\tvoid f() {\n" 8537 "\t\tsomeFunction(parameter1,\n" 8538 "\t\t\t\t\t parameter2);\n" 8539 "\t}\n" 8540 "};", 8541 Tab); 8542 Tab.TabWidth = 8; 8543 Tab.IndentWidth = 4; 8544 verifyFormat("class TabWidth8Indent4 {\n" 8545 " void f() {\n" 8546 "\tsomeFunction(parameter1,\n" 8547 "\t\t parameter2);\n" 8548 " }\n" 8549 "};", 8550 Tab); 8551 Tab.TabWidth = 8; 8552 Tab.IndentWidth = 8; 8553 EXPECT_EQ("/*\n" 8554 "\t a\t\tcomment\n" 8555 "\t in multiple lines\n" 8556 " */", 8557 format(" /*\t \t \n" 8558 " \t \t a\t\tcomment\t \t\n" 8559 " \t \t in multiple lines\t\n" 8560 " \t */", 8561 Tab)); 8562 verifyFormat("{\n" 8563 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8564 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8565 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8566 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8567 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8568 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8569 "};", 8570 Tab); 8571 verifyFormat("enum AA {\n" 8572 "\ta1, // Force multiple lines\n" 8573 "\ta2,\n" 8574 "\ta3\n" 8575 "};", 8576 Tab); 8577 EXPECT_EQ("if (aaaaaaaa && // q\n" 8578 " bb) // w\n" 8579 "\t;", 8580 format("if (aaaaaaaa &&// q\n" 8581 "bb)// w\n" 8582 ";", 8583 Tab)); 8584 verifyFormat("class X {\n" 8585 "\tvoid f() {\n" 8586 "\t\tsomeFunction(parameter1,\n" 8587 "\t\t\t parameter2);\n" 8588 "\t}\n" 8589 "};", 8590 Tab); 8591 verifyFormat("{\n" 8592 "\tQ(\n" 8593 "\t {\n" 8594 "\t\t int a;\n" 8595 "\t\t someFunction(aaaaaaaa,\n" 8596 "\t\t\t\t bbbbbbb);\n" 8597 "\t },\n" 8598 "\t p);\n" 8599 "}", 8600 Tab); 8601 EXPECT_EQ("{\n" 8602 "\t/* aaaa\n" 8603 "\t bbbb */\n" 8604 "}", 8605 format("{\n" 8606 "/* aaaa\n" 8607 " bbbb */\n" 8608 "}", 8609 Tab)); 8610 EXPECT_EQ("{\n" 8611 "\t/*\n" 8612 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8613 "\t bbbbbbbbbbbbb\n" 8614 "\t*/\n" 8615 "}", 8616 format("{\n" 8617 "/*\n" 8618 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8619 "*/\n" 8620 "}", 8621 Tab)); 8622 EXPECT_EQ("{\n" 8623 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8624 "\t// bbbbbbbbbbbbb\n" 8625 "}", 8626 format("{\n" 8627 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8628 "}", 8629 Tab)); 8630 EXPECT_EQ("{\n" 8631 "\t/*\n" 8632 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8633 "\t bbbbbbbbbbbbb\n" 8634 "\t*/\n" 8635 "}", 8636 format("{\n" 8637 "\t/*\n" 8638 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8639 "\t*/\n" 8640 "}", 8641 Tab)); 8642 EXPECT_EQ("{\n" 8643 "\t/*\n" 8644 "\n" 8645 "\t*/\n" 8646 "}", 8647 format("{\n" 8648 "\t/*\n" 8649 "\n" 8650 "\t*/\n" 8651 "}", 8652 Tab)); 8653 EXPECT_EQ("{\n" 8654 "\t/*\n" 8655 " asdf\n" 8656 "\t*/\n" 8657 "}", 8658 format("{\n" 8659 "\t/*\n" 8660 " asdf\n" 8661 "\t*/\n" 8662 "}", 8663 Tab)); 8664 EXPECT_EQ("/*\n" 8665 "\t a\t\tcomment\n" 8666 "\t in multiple lines\n" 8667 " */", 8668 format(" /*\t \t \n" 8669 " \t \t a\t\tcomment\t \t\n" 8670 " \t \t in multiple lines\t\n" 8671 " \t */", 8672 Tab)); 8673 EXPECT_EQ("/* some\n" 8674 " comment */", 8675 format(" \t \t /* some\n" 8676 " \t \t comment */", 8677 Tab)); 8678 EXPECT_EQ("int a; /* some\n" 8679 " comment */", 8680 format(" \t \t int a; /* some\n" 8681 " \t \t comment */", 8682 Tab)); 8683 EXPECT_EQ("int a; /* some\n" 8684 "comment */", 8685 format(" \t \t int\ta; /* some\n" 8686 " \t \t comment */", 8687 Tab)); 8688 EXPECT_EQ("f(\"\t\t\"); /* some\n" 8689 " comment */", 8690 format(" \t \t f(\"\t\t\"); /* some\n" 8691 " \t \t comment */", 8692 Tab)); 8693 EXPECT_EQ("{\n" 8694 " /*\n" 8695 " * Comment\n" 8696 " */\n" 8697 " int i;\n" 8698 "}", 8699 format("{\n" 8700 "\t/*\n" 8701 "\t * Comment\n" 8702 "\t */\n" 8703 "\t int i;\n" 8704 "}")); 8705 Tab.AlignConsecutiveAssignments = true; 8706 Tab.AlignConsecutiveDeclarations = true; 8707 Tab.TabWidth = 4; 8708 Tab.IndentWidth = 4; 8709 verifyFormat("class Assign {\n" 8710 "\tvoid f() {\n" 8711 "\t\tint x = 123;\n" 8712 "\t\tint random = 4;\n" 8713 "\t\tstd::string alphabet =\n" 8714 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 8715 "\t}\n" 8716 "};", 8717 Tab); 8718 } 8719 8720 TEST_F(FormatTest, CalculatesOriginalColumn) { 8721 EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8722 "q\"; /* some\n" 8723 " comment */", 8724 format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8725 "q\"; /* some\n" 8726 " comment */", 8727 getLLVMStyle())); 8728 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 8729 "/* some\n" 8730 " comment */", 8731 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 8732 " /* some\n" 8733 " comment */", 8734 getLLVMStyle())); 8735 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8736 "qqq\n" 8737 "/* some\n" 8738 " comment */", 8739 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8740 "qqq\n" 8741 " /* some\n" 8742 " comment */", 8743 getLLVMStyle())); 8744 EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8745 "wwww; /* some\n" 8746 " comment */", 8747 format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8748 "wwww; /* some\n" 8749 " comment */", 8750 getLLVMStyle())); 8751 } 8752 8753 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { 8754 FormatStyle NoSpace = getLLVMStyle(); 8755 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never; 8756 8757 verifyFormat("while(true)\n" 8758 " continue;", 8759 NoSpace); 8760 verifyFormat("for(;;)\n" 8761 " continue;", 8762 NoSpace); 8763 verifyFormat("if(true)\n" 8764 " f();\n" 8765 "else if(true)\n" 8766 " f();", 8767 NoSpace); 8768 verifyFormat("do {\n" 8769 " do_something();\n" 8770 "} while(something());", 8771 NoSpace); 8772 verifyFormat("switch(x) {\n" 8773 "default:\n" 8774 " break;\n" 8775 "}", 8776 NoSpace); 8777 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace); 8778 verifyFormat("size_t x = sizeof(x);", NoSpace); 8779 verifyFormat("auto f(int x) -> decltype(x);", NoSpace); 8780 verifyFormat("int f(T x) noexcept(x.create());", NoSpace); 8781 verifyFormat("alignas(128) char a[128];", NoSpace); 8782 verifyFormat("size_t x = alignof(MyType);", NoSpace); 8783 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace); 8784 verifyFormat("int f() throw(Deprecated);", NoSpace); 8785 verifyFormat("typedef void (*cb)(int);", NoSpace); 8786 verifyFormat("T A::operator()();", NoSpace); 8787 verifyFormat("X A::operator++(T);", NoSpace); 8788 8789 FormatStyle Space = getLLVMStyle(); 8790 Space.SpaceBeforeParens = FormatStyle::SBPO_Always; 8791 8792 verifyFormat("int f ();", Space); 8793 verifyFormat("void f (int a, T b) {\n" 8794 " while (true)\n" 8795 " continue;\n" 8796 "}", 8797 Space); 8798 verifyFormat("if (true)\n" 8799 " f ();\n" 8800 "else if (true)\n" 8801 " f ();", 8802 Space); 8803 verifyFormat("do {\n" 8804 " do_something ();\n" 8805 "} while (something ());", 8806 Space); 8807 verifyFormat("switch (x) {\n" 8808 "default:\n" 8809 " break;\n" 8810 "}", 8811 Space); 8812 verifyFormat("A::A () : a (1) {}", Space); 8813 verifyFormat("void f () __attribute__ ((asdf));", Space); 8814 verifyFormat("*(&a + 1);\n" 8815 "&((&a)[1]);\n" 8816 "a[(b + c) * d];\n" 8817 "(((a + 1) * 2) + 3) * 4;", 8818 Space); 8819 verifyFormat("#define A(x) x", Space); 8820 verifyFormat("#define A (x) x", Space); 8821 verifyFormat("#if defined(x)\n" 8822 "#endif", 8823 Space); 8824 verifyFormat("auto i = std::make_unique<int> (5);", Space); 8825 verifyFormat("size_t x = sizeof (x);", Space); 8826 verifyFormat("auto f (int x) -> decltype (x);", Space); 8827 verifyFormat("int f (T x) noexcept (x.create ());", Space); 8828 verifyFormat("alignas (128) char a[128];", Space); 8829 verifyFormat("size_t x = alignof (MyType);", Space); 8830 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space); 8831 verifyFormat("int f () throw (Deprecated);", Space); 8832 verifyFormat("typedef void (*cb) (int);", Space); 8833 verifyFormat("T A::operator() ();", Space); 8834 verifyFormat("X A::operator++ (T);", Space); 8835 } 8836 8837 TEST_F(FormatTest, ConfigurableSpacesInParentheses) { 8838 FormatStyle Spaces = getLLVMStyle(); 8839 8840 Spaces.SpacesInParentheses = true; 8841 verifyFormat("do_something( ::globalVar );", Spaces); 8842 verifyFormat("call( x, y, z );", Spaces); 8843 verifyFormat("call();", Spaces); 8844 verifyFormat("std::function<void( int, int )> callback;", Spaces); 8845 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }", 8846 Spaces); 8847 verifyFormat("while ( (bool)1 )\n" 8848 " continue;", 8849 Spaces); 8850 verifyFormat("for ( ;; )\n" 8851 " continue;", 8852 Spaces); 8853 verifyFormat("if ( true )\n" 8854 " f();\n" 8855 "else if ( true )\n" 8856 " f();", 8857 Spaces); 8858 verifyFormat("do {\n" 8859 " do_something( (int)i );\n" 8860 "} while ( something() );", 8861 Spaces); 8862 verifyFormat("switch ( x ) {\n" 8863 "default:\n" 8864 " break;\n" 8865 "}", 8866 Spaces); 8867 8868 Spaces.SpacesInParentheses = false; 8869 Spaces.SpacesInCStyleCastParentheses = true; 8870 verifyFormat("Type *A = ( Type * )P;", Spaces); 8871 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces); 8872 verifyFormat("x = ( int32 )y;", Spaces); 8873 verifyFormat("int a = ( int )(2.0f);", Spaces); 8874 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces); 8875 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces); 8876 verifyFormat("#define x (( int )-1)", Spaces); 8877 8878 // Run the first set of tests again with: 8879 Spaces.SpacesInParentheses = false; 8880 Spaces.SpaceInEmptyParentheses = true; 8881 Spaces.SpacesInCStyleCastParentheses = true; 8882 verifyFormat("call(x, y, z);", Spaces); 8883 verifyFormat("call( );", Spaces); 8884 verifyFormat("std::function<void(int, int)> callback;", Spaces); 8885 verifyFormat("while (( bool )1)\n" 8886 " continue;", 8887 Spaces); 8888 verifyFormat("for (;;)\n" 8889 " continue;", 8890 Spaces); 8891 verifyFormat("if (true)\n" 8892 " f( );\n" 8893 "else if (true)\n" 8894 " f( );", 8895 Spaces); 8896 verifyFormat("do {\n" 8897 " do_something(( int )i);\n" 8898 "} while (something( ));", 8899 Spaces); 8900 verifyFormat("switch (x) {\n" 8901 "default:\n" 8902 " break;\n" 8903 "}", 8904 Spaces); 8905 8906 // Run the first set of tests again with: 8907 Spaces.SpaceAfterCStyleCast = true; 8908 verifyFormat("call(x, y, z);", Spaces); 8909 verifyFormat("call( );", Spaces); 8910 verifyFormat("std::function<void(int, int)> callback;", Spaces); 8911 verifyFormat("while (( bool ) 1)\n" 8912 " continue;", 8913 Spaces); 8914 verifyFormat("for (;;)\n" 8915 " continue;", 8916 Spaces); 8917 verifyFormat("if (true)\n" 8918 " f( );\n" 8919 "else if (true)\n" 8920 " f( );", 8921 Spaces); 8922 verifyFormat("do {\n" 8923 " do_something(( int ) i);\n" 8924 "} while (something( ));", 8925 Spaces); 8926 verifyFormat("switch (x) {\n" 8927 "default:\n" 8928 " break;\n" 8929 "}", 8930 Spaces); 8931 8932 // Run subset of tests again with: 8933 Spaces.SpacesInCStyleCastParentheses = false; 8934 Spaces.SpaceAfterCStyleCast = true; 8935 verifyFormat("while ((bool) 1)\n" 8936 " continue;", 8937 Spaces); 8938 verifyFormat("do {\n" 8939 " do_something((int) i);\n" 8940 "} while (something( ));", 8941 Spaces); 8942 } 8943 8944 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) { 8945 verifyFormat("int a[5];"); 8946 verifyFormat("a[3] += 42;"); 8947 8948 FormatStyle Spaces = getLLVMStyle(); 8949 Spaces.SpacesInSquareBrackets = true; 8950 // Lambdas unchanged. 8951 verifyFormat("int c = []() -> int { return 2; }();\n", Spaces); 8952 verifyFormat("return [i, args...] {};", Spaces); 8953 8954 // Not lambdas. 8955 verifyFormat("int a[ 5 ];", Spaces); 8956 verifyFormat("a[ 3 ] += 42;", Spaces); 8957 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces); 8958 verifyFormat("double &operator[](int i) { return 0; }\n" 8959 "int i;", 8960 Spaces); 8961 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces); 8962 verifyFormat("int i = a[ a ][ a ]->f();", Spaces); 8963 verifyFormat("int i = (*b)[ a ]->f();", Spaces); 8964 } 8965 8966 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) { 8967 verifyFormat("int a = 5;"); 8968 verifyFormat("a += 42;"); 8969 verifyFormat("a or_eq 8;"); 8970 8971 FormatStyle Spaces = getLLVMStyle(); 8972 Spaces.SpaceBeforeAssignmentOperators = false; 8973 verifyFormat("int a= 5;", Spaces); 8974 verifyFormat("a+= 42;", Spaces); 8975 verifyFormat("a or_eq 8;", Spaces); 8976 } 8977 8978 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) { 8979 verifyFormat("class Foo : public Bar {};"); 8980 verifyFormat("Foo::Foo() : foo(1) {}"); 8981 verifyFormat("for (auto a : b) {\n}"); 8982 verifyFormat("int x = a ? b : c;"); 8983 verifyFormat("{\n" 8984 "label0:\n" 8985 " int x = 0;\n" 8986 "}"); 8987 verifyFormat("switch (x) {\n" 8988 "case 1:\n" 8989 "default:\n" 8990 "}"); 8991 8992 FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30); 8993 CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false; 8994 verifyFormat("class Foo : public Bar {};", CtorInitializerStyle); 8995 verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle); 8996 verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle); 8997 verifyFormat("int x = a ? b : c;", CtorInitializerStyle); 8998 verifyFormat("{\n" 8999 "label1:\n" 9000 " int x = 0;\n" 9001 "}", 9002 CtorInitializerStyle); 9003 verifyFormat("switch (x) {\n" 9004 "case 1:\n" 9005 "default:\n" 9006 "}", 9007 CtorInitializerStyle); 9008 CtorInitializerStyle.BreakConstructorInitializers = 9009 FormatStyle::BCIS_AfterColon; 9010 verifyFormat("Fooooooooooo::Fooooooooooo():\n" 9011 " aaaaaaaaaaaaaaaa(1),\n" 9012 " bbbbbbbbbbbbbbbb(2) {}", 9013 CtorInitializerStyle); 9014 CtorInitializerStyle.BreakConstructorInitializers = 9015 FormatStyle::BCIS_BeforeComma; 9016 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 9017 " : aaaaaaaaaaaaaaaa(1)\n" 9018 " , bbbbbbbbbbbbbbbb(2) {}", 9019 CtorInitializerStyle); 9020 CtorInitializerStyle.BreakConstructorInitializers = 9021 FormatStyle::BCIS_BeforeColon; 9022 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 9023 " : aaaaaaaaaaaaaaaa(1),\n" 9024 " bbbbbbbbbbbbbbbb(2) {}", 9025 CtorInitializerStyle); 9026 CtorInitializerStyle.ConstructorInitializerIndentWidth = 0; 9027 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 9028 ": aaaaaaaaaaaaaaaa(1),\n" 9029 " bbbbbbbbbbbbbbbb(2) {}", 9030 CtorInitializerStyle); 9031 9032 FormatStyle InheritanceStyle = getLLVMStyle(); 9033 InheritanceStyle.SpaceBeforeInheritanceColon = false; 9034 verifyFormat("class Foo: public Bar {};", InheritanceStyle); 9035 verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle); 9036 verifyFormat("for (auto a : b) {\n}", InheritanceStyle); 9037 verifyFormat("int x = a ? b : c;", InheritanceStyle); 9038 verifyFormat("{\n" 9039 "label2:\n" 9040 " int x = 0;\n" 9041 "}", 9042 InheritanceStyle); 9043 verifyFormat("switch (x) {\n" 9044 "case 1:\n" 9045 "default:\n" 9046 "}", 9047 InheritanceStyle); 9048 9049 FormatStyle ForLoopStyle = getLLVMStyle(); 9050 ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false; 9051 verifyFormat("class Foo : public Bar {};", ForLoopStyle); 9052 verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle); 9053 verifyFormat("for (auto a: b) {\n}", ForLoopStyle); 9054 verifyFormat("int x = a ? b : c;", ForLoopStyle); 9055 verifyFormat("{\n" 9056 "label2:\n" 9057 " int x = 0;\n" 9058 "}", 9059 ForLoopStyle); 9060 verifyFormat("switch (x) {\n" 9061 "case 1:\n" 9062 "default:\n" 9063 "}", 9064 ForLoopStyle); 9065 9066 FormatStyle NoSpaceStyle = getLLVMStyle(); 9067 NoSpaceStyle.SpaceBeforeCtorInitializerColon = false; 9068 NoSpaceStyle.SpaceBeforeInheritanceColon = false; 9069 NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false; 9070 verifyFormat("class Foo: public Bar {};", NoSpaceStyle); 9071 verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle); 9072 verifyFormat("for (auto a: b) {\n}", NoSpaceStyle); 9073 verifyFormat("int x = a ? b : c;", NoSpaceStyle); 9074 verifyFormat("{\n" 9075 "label3:\n" 9076 " int x = 0;\n" 9077 "}", 9078 NoSpaceStyle); 9079 verifyFormat("switch (x) {\n" 9080 "case 1:\n" 9081 "default:\n" 9082 "}", 9083 NoSpaceStyle); 9084 } 9085 9086 TEST_F(FormatTest, AlignConsecutiveAssignments) { 9087 FormatStyle Alignment = getLLVMStyle(); 9088 Alignment.AlignConsecutiveAssignments = false; 9089 verifyFormat("int a = 5;\n" 9090 "int oneTwoThree = 123;", 9091 Alignment); 9092 verifyFormat("int a = 5;\n" 9093 "int oneTwoThree = 123;", 9094 Alignment); 9095 9096 Alignment.AlignConsecutiveAssignments = true; 9097 verifyFormat("int a = 5;\n" 9098 "int oneTwoThree = 123;", 9099 Alignment); 9100 verifyFormat("int a = method();\n" 9101 "int oneTwoThree = 133;", 9102 Alignment); 9103 verifyFormat("a &= 5;\n" 9104 "bcd *= 5;\n" 9105 "ghtyf += 5;\n" 9106 "dvfvdb -= 5;\n" 9107 "a /= 5;\n" 9108 "vdsvsv %= 5;\n" 9109 "sfdbddfbdfbb ^= 5;\n" 9110 "dvsdsv |= 5;\n" 9111 "int dsvvdvsdvvv = 123;", 9112 Alignment); 9113 verifyFormat("int i = 1, j = 10;\n" 9114 "something = 2000;", 9115 Alignment); 9116 verifyFormat("something = 2000;\n" 9117 "int i = 1, j = 10;\n", 9118 Alignment); 9119 verifyFormat("something = 2000;\n" 9120 "another = 911;\n" 9121 "int i = 1, j = 10;\n" 9122 "oneMore = 1;\n" 9123 "i = 2;", 9124 Alignment); 9125 verifyFormat("int a = 5;\n" 9126 "int one = 1;\n" 9127 "method();\n" 9128 "int oneTwoThree = 123;\n" 9129 "int oneTwo = 12;", 9130 Alignment); 9131 verifyFormat("int oneTwoThree = 123;\n" 9132 "int oneTwo = 12;\n" 9133 "method();\n", 9134 Alignment); 9135 verifyFormat("int oneTwoThree = 123; // comment\n" 9136 "int oneTwo = 12; // comment", 9137 Alignment); 9138 EXPECT_EQ("int a = 5;\n" 9139 "\n" 9140 "int oneTwoThree = 123;", 9141 format("int a = 5;\n" 9142 "\n" 9143 "int oneTwoThree= 123;", 9144 Alignment)); 9145 EXPECT_EQ("int a = 5;\n" 9146 "int one = 1;\n" 9147 "\n" 9148 "int oneTwoThree = 123;", 9149 format("int a = 5;\n" 9150 "int one = 1;\n" 9151 "\n" 9152 "int oneTwoThree = 123;", 9153 Alignment)); 9154 EXPECT_EQ("int a = 5;\n" 9155 "int one = 1;\n" 9156 "\n" 9157 "int oneTwoThree = 123;\n" 9158 "int oneTwo = 12;", 9159 format("int a = 5;\n" 9160 "int one = 1;\n" 9161 "\n" 9162 "int oneTwoThree = 123;\n" 9163 "int oneTwo = 12;", 9164 Alignment)); 9165 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 9166 verifyFormat("#define A \\\n" 9167 " int aaaa = 12; \\\n" 9168 " int b = 23; \\\n" 9169 " int ccc = 234; \\\n" 9170 " int dddddddddd = 2345;", 9171 Alignment); 9172 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 9173 verifyFormat("#define A \\\n" 9174 " int aaaa = 12; \\\n" 9175 " int b = 23; \\\n" 9176 " int ccc = 234; \\\n" 9177 " int dddddddddd = 2345;", 9178 Alignment); 9179 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 9180 verifyFormat("#define A " 9181 " \\\n" 9182 " int aaaa = 12; " 9183 " \\\n" 9184 " int b = 23; " 9185 " \\\n" 9186 " int ccc = 234; " 9187 " \\\n" 9188 " int dddddddddd = 2345;", 9189 Alignment); 9190 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 9191 "k = 4, int l = 5,\n" 9192 " int m = 6) {\n" 9193 " int j = 10;\n" 9194 " otherThing = 1;\n" 9195 "}", 9196 Alignment); 9197 verifyFormat("void SomeFunction(int parameter = 0) {\n" 9198 " int i = 1;\n" 9199 " int j = 2;\n" 9200 " int big = 10000;\n" 9201 "}", 9202 Alignment); 9203 verifyFormat("class C {\n" 9204 "public:\n" 9205 " int i = 1;\n" 9206 " virtual void f() = 0;\n" 9207 "};", 9208 Alignment); 9209 verifyFormat("int i = 1;\n" 9210 "if (SomeType t = getSomething()) {\n" 9211 "}\n" 9212 "int j = 2;\n" 9213 "int big = 10000;", 9214 Alignment); 9215 verifyFormat("int j = 7;\n" 9216 "for (int k = 0; k < N; ++k) {\n" 9217 "}\n" 9218 "int j = 2;\n" 9219 "int big = 10000;\n" 9220 "}", 9221 Alignment); 9222 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 9223 verifyFormat("int i = 1;\n" 9224 "LooooooooooongType loooooooooooooooooooooongVariable\n" 9225 " = someLooooooooooooooooongFunction();\n" 9226 "int j = 2;", 9227 Alignment); 9228 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 9229 verifyFormat("int i = 1;\n" 9230 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 9231 " someLooooooooooooooooongFunction();\n" 9232 "int j = 2;", 9233 Alignment); 9234 9235 verifyFormat("auto lambda = []() {\n" 9236 " auto i = 0;\n" 9237 " return 0;\n" 9238 "};\n" 9239 "int i = 0;\n" 9240 "auto v = type{\n" 9241 " i = 1, //\n" 9242 " (i = 2), //\n" 9243 " i = 3 //\n" 9244 "};", 9245 Alignment); 9246 9247 verifyFormat( 9248 "int i = 1;\n" 9249 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 9250 " loooooooooooooooooooooongParameterB);\n" 9251 "int j = 2;", 9252 Alignment); 9253 9254 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 9255 " typename B = very_long_type_name_1,\n" 9256 " typename T_2 = very_long_type_name_2>\n" 9257 "auto foo() {}\n", 9258 Alignment); 9259 verifyFormat("int a, b = 1;\n" 9260 "int c = 2;\n" 9261 "int dd = 3;\n", 9262 Alignment); 9263 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 9264 "float b[1][] = {{3.f}};\n", 9265 Alignment); 9266 verifyFormat("for (int i = 0; i < 1; i++)\n" 9267 " int x = 1;\n", 9268 Alignment); 9269 verifyFormat("for (i = 0; i < 1; i++)\n" 9270 " x = 1;\n" 9271 "y = 1;\n", 9272 Alignment); 9273 } 9274 9275 TEST_F(FormatTest, AlignConsecutiveDeclarations) { 9276 FormatStyle Alignment = getLLVMStyle(); 9277 Alignment.AlignConsecutiveDeclarations = false; 9278 verifyFormat("float const a = 5;\n" 9279 "int oneTwoThree = 123;", 9280 Alignment); 9281 verifyFormat("int a = 5;\n" 9282 "float const oneTwoThree = 123;", 9283 Alignment); 9284 9285 Alignment.AlignConsecutiveDeclarations = true; 9286 verifyFormat("float const a = 5;\n" 9287 "int oneTwoThree = 123;", 9288 Alignment); 9289 verifyFormat("int a = method();\n" 9290 "float const oneTwoThree = 133;", 9291 Alignment); 9292 verifyFormat("int i = 1, j = 10;\n" 9293 "something = 2000;", 9294 Alignment); 9295 verifyFormat("something = 2000;\n" 9296 "int i = 1, j = 10;\n", 9297 Alignment); 9298 verifyFormat("float something = 2000;\n" 9299 "double another = 911;\n" 9300 "int i = 1, j = 10;\n" 9301 "const int *oneMore = 1;\n" 9302 "unsigned i = 2;", 9303 Alignment); 9304 verifyFormat("float a = 5;\n" 9305 "int one = 1;\n" 9306 "method();\n" 9307 "const double oneTwoThree = 123;\n" 9308 "const unsigned int oneTwo = 12;", 9309 Alignment); 9310 verifyFormat("int oneTwoThree{0}; // comment\n" 9311 "unsigned oneTwo; // comment", 9312 Alignment); 9313 EXPECT_EQ("float const a = 5;\n" 9314 "\n" 9315 "int oneTwoThree = 123;", 9316 format("float const a = 5;\n" 9317 "\n" 9318 "int oneTwoThree= 123;", 9319 Alignment)); 9320 EXPECT_EQ("float a = 5;\n" 9321 "int one = 1;\n" 9322 "\n" 9323 "unsigned oneTwoThree = 123;", 9324 format("float a = 5;\n" 9325 "int one = 1;\n" 9326 "\n" 9327 "unsigned oneTwoThree = 123;", 9328 Alignment)); 9329 EXPECT_EQ("float a = 5;\n" 9330 "int one = 1;\n" 9331 "\n" 9332 "unsigned oneTwoThree = 123;\n" 9333 "int oneTwo = 12;", 9334 format("float a = 5;\n" 9335 "int one = 1;\n" 9336 "\n" 9337 "unsigned oneTwoThree = 123;\n" 9338 "int oneTwo = 12;", 9339 Alignment)); 9340 // Function prototype alignment 9341 verifyFormat("int a();\n" 9342 "double b();", 9343 Alignment); 9344 verifyFormat("int a(int x);\n" 9345 "double b();", 9346 Alignment); 9347 unsigned OldColumnLimit = Alignment.ColumnLimit; 9348 // We need to set ColumnLimit to zero, in order to stress nested alignments, 9349 // otherwise the function parameters will be re-flowed onto a single line. 9350 Alignment.ColumnLimit = 0; 9351 EXPECT_EQ("int a(int x,\n" 9352 " float y);\n" 9353 "double b(int x,\n" 9354 " double y);", 9355 format("int a(int x,\n" 9356 " float y);\n" 9357 "double b(int x,\n" 9358 " double y);", 9359 Alignment)); 9360 // This ensures that function parameters of function declarations are 9361 // correctly indented when their owning functions are indented. 9362 // The failure case here is for 'double y' to not be indented enough. 9363 EXPECT_EQ("double a(int x);\n" 9364 "int b(int y,\n" 9365 " double z);", 9366 format("double a(int x);\n" 9367 "int b(int y,\n" 9368 " double z);", 9369 Alignment)); 9370 // Set ColumnLimit low so that we induce wrapping immediately after 9371 // the function name and opening paren. 9372 Alignment.ColumnLimit = 13; 9373 verifyFormat("int function(\n" 9374 " int x,\n" 9375 " bool y);", 9376 Alignment); 9377 Alignment.ColumnLimit = OldColumnLimit; 9378 // Ensure function pointers don't screw up recursive alignment 9379 verifyFormat("int a(int x, void (*fp)(int y));\n" 9380 "double b();", 9381 Alignment); 9382 Alignment.AlignConsecutiveAssignments = true; 9383 // Ensure recursive alignment is broken by function braces, so that the 9384 // "a = 1" does not align with subsequent assignments inside the function 9385 // body. 9386 verifyFormat("int func(int a = 1) {\n" 9387 " int b = 2;\n" 9388 " int cc = 3;\n" 9389 "}", 9390 Alignment); 9391 verifyFormat("float something = 2000;\n" 9392 "double another = 911;\n" 9393 "int i = 1, j = 10;\n" 9394 "const int *oneMore = 1;\n" 9395 "unsigned i = 2;", 9396 Alignment); 9397 verifyFormat("int oneTwoThree = {0}; // comment\n" 9398 "unsigned oneTwo = 0; // comment", 9399 Alignment); 9400 // Make sure that scope is correctly tracked, in the absence of braces 9401 verifyFormat("for (int i = 0; i < n; i++)\n" 9402 " j = i;\n" 9403 "double x = 1;\n", 9404 Alignment); 9405 verifyFormat("if (int i = 0)\n" 9406 " j = i;\n" 9407 "double x = 1;\n", 9408 Alignment); 9409 // Ensure operator[] and operator() are comprehended 9410 verifyFormat("struct test {\n" 9411 " long long int foo();\n" 9412 " int operator[](int a);\n" 9413 " double bar();\n" 9414 "};\n", 9415 Alignment); 9416 verifyFormat("struct test {\n" 9417 " long long int foo();\n" 9418 " int operator()(int a);\n" 9419 " double bar();\n" 9420 "};\n", 9421 Alignment); 9422 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 9423 " int const i = 1;\n" 9424 " int * j = 2;\n" 9425 " int big = 10000;\n" 9426 "\n" 9427 " unsigned oneTwoThree = 123;\n" 9428 " int oneTwo = 12;\n" 9429 " method();\n" 9430 " float k = 2;\n" 9431 " int ll = 10000;\n" 9432 "}", 9433 format("void SomeFunction(int parameter= 0) {\n" 9434 " int const i= 1;\n" 9435 " int *j=2;\n" 9436 " int big = 10000;\n" 9437 "\n" 9438 "unsigned oneTwoThree =123;\n" 9439 "int oneTwo = 12;\n" 9440 " method();\n" 9441 "float k= 2;\n" 9442 "int ll=10000;\n" 9443 "}", 9444 Alignment)); 9445 Alignment.AlignConsecutiveAssignments = false; 9446 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 9447 verifyFormat("#define A \\\n" 9448 " int aaaa = 12; \\\n" 9449 " float b = 23; \\\n" 9450 " const int ccc = 234; \\\n" 9451 " unsigned dddddddddd = 2345;", 9452 Alignment); 9453 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 9454 verifyFormat("#define A \\\n" 9455 " int aaaa = 12; \\\n" 9456 " float b = 23; \\\n" 9457 " const int ccc = 234; \\\n" 9458 " unsigned dddddddddd = 2345;", 9459 Alignment); 9460 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 9461 Alignment.ColumnLimit = 30; 9462 verifyFormat("#define A \\\n" 9463 " int aaaa = 12; \\\n" 9464 " float b = 23; \\\n" 9465 " const int ccc = 234; \\\n" 9466 " int dddddddddd = 2345;", 9467 Alignment); 9468 Alignment.ColumnLimit = 80; 9469 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 9470 "k = 4, int l = 5,\n" 9471 " int m = 6) {\n" 9472 " const int j = 10;\n" 9473 " otherThing = 1;\n" 9474 "}", 9475 Alignment); 9476 verifyFormat("void SomeFunction(int parameter = 0) {\n" 9477 " int const i = 1;\n" 9478 " int * j = 2;\n" 9479 " int big = 10000;\n" 9480 "}", 9481 Alignment); 9482 verifyFormat("class C {\n" 9483 "public:\n" 9484 " int i = 1;\n" 9485 " virtual void f() = 0;\n" 9486 "};", 9487 Alignment); 9488 verifyFormat("float i = 1;\n" 9489 "if (SomeType t = getSomething()) {\n" 9490 "}\n" 9491 "const unsigned j = 2;\n" 9492 "int big = 10000;", 9493 Alignment); 9494 verifyFormat("float j = 7;\n" 9495 "for (int k = 0; k < N; ++k) {\n" 9496 "}\n" 9497 "unsigned j = 2;\n" 9498 "int big = 10000;\n" 9499 "}", 9500 Alignment); 9501 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 9502 verifyFormat("float i = 1;\n" 9503 "LooooooooooongType loooooooooooooooooooooongVariable\n" 9504 " = someLooooooooooooooooongFunction();\n" 9505 "int j = 2;", 9506 Alignment); 9507 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 9508 verifyFormat("int i = 1;\n" 9509 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 9510 " someLooooooooooooooooongFunction();\n" 9511 "int j = 2;", 9512 Alignment); 9513 9514 Alignment.AlignConsecutiveAssignments = true; 9515 verifyFormat("auto lambda = []() {\n" 9516 " auto ii = 0;\n" 9517 " float j = 0;\n" 9518 " return 0;\n" 9519 "};\n" 9520 "int i = 0;\n" 9521 "float i2 = 0;\n" 9522 "auto v = type{\n" 9523 " i = 1, //\n" 9524 " (i = 2), //\n" 9525 " i = 3 //\n" 9526 "};", 9527 Alignment); 9528 Alignment.AlignConsecutiveAssignments = false; 9529 9530 verifyFormat( 9531 "int i = 1;\n" 9532 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 9533 " loooooooooooooooooooooongParameterB);\n" 9534 "int j = 2;", 9535 Alignment); 9536 9537 // Test interactions with ColumnLimit and AlignConsecutiveAssignments: 9538 // We expect declarations and assignments to align, as long as it doesn't 9539 // exceed the column limit, starting a new alignment sequence whenever it 9540 // happens. 9541 Alignment.AlignConsecutiveAssignments = true; 9542 Alignment.ColumnLimit = 30; 9543 verifyFormat("float ii = 1;\n" 9544 "unsigned j = 2;\n" 9545 "int someVerylongVariable = 1;\n" 9546 "AnotherLongType ll = 123456;\n" 9547 "VeryVeryLongType k = 2;\n" 9548 "int myvar = 1;", 9549 Alignment); 9550 Alignment.ColumnLimit = 80; 9551 Alignment.AlignConsecutiveAssignments = false; 9552 9553 verifyFormat( 9554 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n" 9555 " typename LongType, typename B>\n" 9556 "auto foo() {}\n", 9557 Alignment); 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 = true; 9566 verifyFormat("float a, b = 1;\n" 9567 "int c = 2;\n" 9568 "int dd = 3;\n", 9569 Alignment); 9570 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 9571 "float b[1][] = {{3.f}};\n", 9572 Alignment); 9573 Alignment.AlignConsecutiveAssignments = false; 9574 9575 Alignment.ColumnLimit = 30; 9576 Alignment.BinPackParameters = false; 9577 verifyFormat("void foo(float a,\n" 9578 " float b,\n" 9579 " int c,\n" 9580 " uint32_t *d) {\n" 9581 " int * e = 0;\n" 9582 " float f = 0;\n" 9583 " double g = 0;\n" 9584 "}\n" 9585 "void bar(ino_t a,\n" 9586 " int b,\n" 9587 " uint32_t *c,\n" 9588 " bool d) {}\n", 9589 Alignment); 9590 Alignment.BinPackParameters = true; 9591 Alignment.ColumnLimit = 80; 9592 9593 // Bug 33507 9594 Alignment.PointerAlignment = FormatStyle::PAS_Middle; 9595 verifyFormat( 9596 "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n" 9597 " static const Version verVs2017;\n" 9598 " return true;\n" 9599 "});\n", 9600 Alignment); 9601 Alignment.PointerAlignment = FormatStyle::PAS_Right; 9602 } 9603 9604 TEST_F(FormatTest, LinuxBraceBreaking) { 9605 FormatStyle LinuxBraceStyle = getLLVMStyle(); 9606 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux; 9607 verifyFormat("namespace a\n" 9608 "{\n" 9609 "class A\n" 9610 "{\n" 9611 " void f()\n" 9612 " {\n" 9613 " if (true) {\n" 9614 " a();\n" 9615 " b();\n" 9616 " } else {\n" 9617 " a();\n" 9618 " }\n" 9619 " }\n" 9620 " void g() { return; }\n" 9621 "};\n" 9622 "struct B {\n" 9623 " int x;\n" 9624 "};\n" 9625 "} // namespace a\n", 9626 LinuxBraceStyle); 9627 verifyFormat("enum X {\n" 9628 " Y = 0,\n" 9629 "}\n", 9630 LinuxBraceStyle); 9631 verifyFormat("struct S {\n" 9632 " int Type;\n" 9633 " union {\n" 9634 " int x;\n" 9635 " double y;\n" 9636 " } Value;\n" 9637 " class C\n" 9638 " {\n" 9639 " MyFavoriteType Value;\n" 9640 " } Class;\n" 9641 "}\n", 9642 LinuxBraceStyle); 9643 } 9644 9645 TEST_F(FormatTest, MozillaBraceBreaking) { 9646 FormatStyle MozillaBraceStyle = getLLVMStyle(); 9647 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; 9648 MozillaBraceStyle.FixNamespaceComments = false; 9649 verifyFormat("namespace a {\n" 9650 "class A\n" 9651 "{\n" 9652 " void f()\n" 9653 " {\n" 9654 " if (true) {\n" 9655 " a();\n" 9656 " b();\n" 9657 " }\n" 9658 " }\n" 9659 " void g() { return; }\n" 9660 "};\n" 9661 "enum E\n" 9662 "{\n" 9663 " A,\n" 9664 " // foo\n" 9665 " B,\n" 9666 " C\n" 9667 "};\n" 9668 "struct B\n" 9669 "{\n" 9670 " int x;\n" 9671 "};\n" 9672 "}\n", 9673 MozillaBraceStyle); 9674 verifyFormat("struct S\n" 9675 "{\n" 9676 " int Type;\n" 9677 " union\n" 9678 " {\n" 9679 " int x;\n" 9680 " double y;\n" 9681 " } Value;\n" 9682 " class C\n" 9683 " {\n" 9684 " MyFavoriteType Value;\n" 9685 " } Class;\n" 9686 "}\n", 9687 MozillaBraceStyle); 9688 } 9689 9690 TEST_F(FormatTest, StroustrupBraceBreaking) { 9691 FormatStyle StroustrupBraceStyle = getLLVMStyle(); 9692 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 9693 verifyFormat("namespace a {\n" 9694 "class A {\n" 9695 " void f()\n" 9696 " {\n" 9697 " if (true) {\n" 9698 " a();\n" 9699 " b();\n" 9700 " }\n" 9701 " }\n" 9702 " void g() { return; }\n" 9703 "};\n" 9704 "struct B {\n" 9705 " int x;\n" 9706 "};\n" 9707 "} // namespace a\n", 9708 StroustrupBraceStyle); 9709 9710 verifyFormat("void foo()\n" 9711 "{\n" 9712 " if (a) {\n" 9713 " a();\n" 9714 " }\n" 9715 " else {\n" 9716 " b();\n" 9717 " }\n" 9718 "}\n", 9719 StroustrupBraceStyle); 9720 9721 verifyFormat("#ifdef _DEBUG\n" 9722 "int foo(int i = 0)\n" 9723 "#else\n" 9724 "int foo(int i = 5)\n" 9725 "#endif\n" 9726 "{\n" 9727 " return i;\n" 9728 "}", 9729 StroustrupBraceStyle); 9730 9731 verifyFormat("void foo() {}\n" 9732 "void bar()\n" 9733 "#ifdef _DEBUG\n" 9734 "{\n" 9735 " foo();\n" 9736 "}\n" 9737 "#else\n" 9738 "{\n" 9739 "}\n" 9740 "#endif", 9741 StroustrupBraceStyle); 9742 9743 verifyFormat("void foobar() { int i = 5; }\n" 9744 "#ifdef _DEBUG\n" 9745 "void bar() {}\n" 9746 "#else\n" 9747 "void bar() { foobar(); }\n" 9748 "#endif", 9749 StroustrupBraceStyle); 9750 } 9751 9752 TEST_F(FormatTest, AllmanBraceBreaking) { 9753 FormatStyle AllmanBraceStyle = getLLVMStyle(); 9754 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman; 9755 9756 EXPECT_EQ("namespace a\n" 9757 "{\n" 9758 "void f();\n" 9759 "void g();\n" 9760 "} // namespace a\n", 9761 format("namespace a\n" 9762 "{\n" 9763 "void f();\n" 9764 "void g();\n" 9765 "}\n", 9766 AllmanBraceStyle)); 9767 9768 verifyFormat("namespace a\n" 9769 "{\n" 9770 "class A\n" 9771 "{\n" 9772 " void f()\n" 9773 " {\n" 9774 " if (true)\n" 9775 " {\n" 9776 " a();\n" 9777 " b();\n" 9778 " }\n" 9779 " }\n" 9780 " void g() { return; }\n" 9781 "};\n" 9782 "struct B\n" 9783 "{\n" 9784 " int x;\n" 9785 "};\n" 9786 "} // namespace a", 9787 AllmanBraceStyle); 9788 9789 verifyFormat("void f()\n" 9790 "{\n" 9791 " if (true)\n" 9792 " {\n" 9793 " a();\n" 9794 " }\n" 9795 " else if (false)\n" 9796 " {\n" 9797 " b();\n" 9798 " }\n" 9799 " else\n" 9800 " {\n" 9801 " c();\n" 9802 " }\n" 9803 "}\n", 9804 AllmanBraceStyle); 9805 9806 verifyFormat("void f()\n" 9807 "{\n" 9808 " for (int i = 0; i < 10; ++i)\n" 9809 " {\n" 9810 " a();\n" 9811 " }\n" 9812 " while (false)\n" 9813 " {\n" 9814 " b();\n" 9815 " }\n" 9816 " do\n" 9817 " {\n" 9818 " c();\n" 9819 " } while (false)\n" 9820 "}\n", 9821 AllmanBraceStyle); 9822 9823 verifyFormat("void f(int a)\n" 9824 "{\n" 9825 " switch (a)\n" 9826 " {\n" 9827 " case 0:\n" 9828 " break;\n" 9829 " case 1:\n" 9830 " {\n" 9831 " break;\n" 9832 " }\n" 9833 " case 2:\n" 9834 " {\n" 9835 " }\n" 9836 " break;\n" 9837 " default:\n" 9838 " break;\n" 9839 " }\n" 9840 "}\n", 9841 AllmanBraceStyle); 9842 9843 verifyFormat("enum X\n" 9844 "{\n" 9845 " Y = 0,\n" 9846 "}\n", 9847 AllmanBraceStyle); 9848 verifyFormat("enum X\n" 9849 "{\n" 9850 " Y = 0\n" 9851 "}\n", 9852 AllmanBraceStyle); 9853 9854 verifyFormat("@interface BSApplicationController ()\n" 9855 "{\n" 9856 "@private\n" 9857 " id _extraIvar;\n" 9858 "}\n" 9859 "@end\n", 9860 AllmanBraceStyle); 9861 9862 verifyFormat("#ifdef _DEBUG\n" 9863 "int foo(int i = 0)\n" 9864 "#else\n" 9865 "int foo(int i = 5)\n" 9866 "#endif\n" 9867 "{\n" 9868 " return i;\n" 9869 "}", 9870 AllmanBraceStyle); 9871 9872 verifyFormat("void foo() {}\n" 9873 "void bar()\n" 9874 "#ifdef _DEBUG\n" 9875 "{\n" 9876 " foo();\n" 9877 "}\n" 9878 "#else\n" 9879 "{\n" 9880 "}\n" 9881 "#endif", 9882 AllmanBraceStyle); 9883 9884 verifyFormat("void foobar() { int i = 5; }\n" 9885 "#ifdef _DEBUG\n" 9886 "void bar() {}\n" 9887 "#else\n" 9888 "void bar() { foobar(); }\n" 9889 "#endif", 9890 AllmanBraceStyle); 9891 9892 // This shouldn't affect ObjC blocks.. 9893 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 9894 " // ...\n" 9895 " int i;\n" 9896 "}];", 9897 AllmanBraceStyle); 9898 verifyFormat("void (^block)(void) = ^{\n" 9899 " // ...\n" 9900 " int i;\n" 9901 "};", 9902 AllmanBraceStyle); 9903 // .. or dict literals. 9904 verifyFormat("void f()\n" 9905 "{\n" 9906 " // ...\n" 9907 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 9908 "}", 9909 AllmanBraceStyle); 9910 verifyFormat("void f()\n" 9911 "{\n" 9912 " // ...\n" 9913 " [object someMethod:@{a : @\"b\"}];\n" 9914 "}", 9915 AllmanBraceStyle); 9916 verifyFormat("int f()\n" 9917 "{ // comment\n" 9918 " return 42;\n" 9919 "}", 9920 AllmanBraceStyle); 9921 9922 AllmanBraceStyle.ColumnLimit = 19; 9923 verifyFormat("void f() { int i; }", AllmanBraceStyle); 9924 AllmanBraceStyle.ColumnLimit = 18; 9925 verifyFormat("void f()\n" 9926 "{\n" 9927 " int i;\n" 9928 "}", 9929 AllmanBraceStyle); 9930 AllmanBraceStyle.ColumnLimit = 80; 9931 9932 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle; 9933 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true; 9934 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 9935 verifyFormat("void f(bool b)\n" 9936 "{\n" 9937 " if (b)\n" 9938 " {\n" 9939 " return;\n" 9940 " }\n" 9941 "}\n", 9942 BreakBeforeBraceShortIfs); 9943 verifyFormat("void f(bool b)\n" 9944 "{\n" 9945 " if constexpr (b)\n" 9946 " {\n" 9947 " return;\n" 9948 " }\n" 9949 "}\n", 9950 BreakBeforeBraceShortIfs); 9951 verifyFormat("void f(bool b)\n" 9952 "{\n" 9953 " if (b) return;\n" 9954 "}\n", 9955 BreakBeforeBraceShortIfs); 9956 verifyFormat("void f(bool b)\n" 9957 "{\n" 9958 " if constexpr (b) return;\n" 9959 "}\n", 9960 BreakBeforeBraceShortIfs); 9961 verifyFormat("void f(bool b)\n" 9962 "{\n" 9963 " while (b)\n" 9964 " {\n" 9965 " return;\n" 9966 " }\n" 9967 "}\n", 9968 BreakBeforeBraceShortIfs); 9969 } 9970 9971 TEST_F(FormatTest, GNUBraceBreaking) { 9972 FormatStyle GNUBraceStyle = getLLVMStyle(); 9973 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU; 9974 verifyFormat("namespace a\n" 9975 "{\n" 9976 "class A\n" 9977 "{\n" 9978 " void f()\n" 9979 " {\n" 9980 " int a;\n" 9981 " {\n" 9982 " int b;\n" 9983 " }\n" 9984 " if (true)\n" 9985 " {\n" 9986 " a();\n" 9987 " b();\n" 9988 " }\n" 9989 " }\n" 9990 " void g() { return; }\n" 9991 "}\n" 9992 "} // namespace a", 9993 GNUBraceStyle); 9994 9995 verifyFormat("void f()\n" 9996 "{\n" 9997 " if (true)\n" 9998 " {\n" 9999 " a();\n" 10000 " }\n" 10001 " else if (false)\n" 10002 " {\n" 10003 " b();\n" 10004 " }\n" 10005 " else\n" 10006 " {\n" 10007 " c();\n" 10008 " }\n" 10009 "}\n", 10010 GNUBraceStyle); 10011 10012 verifyFormat("void f()\n" 10013 "{\n" 10014 " for (int i = 0; i < 10; ++i)\n" 10015 " {\n" 10016 " a();\n" 10017 " }\n" 10018 " while (false)\n" 10019 " {\n" 10020 " b();\n" 10021 " }\n" 10022 " do\n" 10023 " {\n" 10024 " c();\n" 10025 " }\n" 10026 " while (false);\n" 10027 "}\n", 10028 GNUBraceStyle); 10029 10030 verifyFormat("void f(int a)\n" 10031 "{\n" 10032 " switch (a)\n" 10033 " {\n" 10034 " case 0:\n" 10035 " break;\n" 10036 " case 1:\n" 10037 " {\n" 10038 " break;\n" 10039 " }\n" 10040 " case 2:\n" 10041 " {\n" 10042 " }\n" 10043 " break;\n" 10044 " default:\n" 10045 " break;\n" 10046 " }\n" 10047 "}\n", 10048 GNUBraceStyle); 10049 10050 verifyFormat("enum X\n" 10051 "{\n" 10052 " Y = 0,\n" 10053 "}\n", 10054 GNUBraceStyle); 10055 10056 verifyFormat("@interface BSApplicationController ()\n" 10057 "{\n" 10058 "@private\n" 10059 " id _extraIvar;\n" 10060 "}\n" 10061 "@end\n", 10062 GNUBraceStyle); 10063 10064 verifyFormat("#ifdef _DEBUG\n" 10065 "int foo(int i = 0)\n" 10066 "#else\n" 10067 "int foo(int i = 5)\n" 10068 "#endif\n" 10069 "{\n" 10070 " return i;\n" 10071 "}", 10072 GNUBraceStyle); 10073 10074 verifyFormat("void foo() {}\n" 10075 "void bar()\n" 10076 "#ifdef _DEBUG\n" 10077 "{\n" 10078 " foo();\n" 10079 "}\n" 10080 "#else\n" 10081 "{\n" 10082 "}\n" 10083 "#endif", 10084 GNUBraceStyle); 10085 10086 verifyFormat("void foobar() { int i = 5; }\n" 10087 "#ifdef _DEBUG\n" 10088 "void bar() {}\n" 10089 "#else\n" 10090 "void bar() { foobar(); }\n" 10091 "#endif", 10092 GNUBraceStyle); 10093 } 10094 10095 TEST_F(FormatTest, WebKitBraceBreaking) { 10096 FormatStyle WebKitBraceStyle = getLLVMStyle(); 10097 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit; 10098 WebKitBraceStyle.FixNamespaceComments = false; 10099 verifyFormat("namespace a {\n" 10100 "class A {\n" 10101 " void f()\n" 10102 " {\n" 10103 " if (true) {\n" 10104 " a();\n" 10105 " b();\n" 10106 " }\n" 10107 " }\n" 10108 " void g() { return; }\n" 10109 "};\n" 10110 "enum E {\n" 10111 " A,\n" 10112 " // foo\n" 10113 " B,\n" 10114 " C\n" 10115 "};\n" 10116 "struct B {\n" 10117 " int x;\n" 10118 "};\n" 10119 "}\n", 10120 WebKitBraceStyle); 10121 verifyFormat("struct S {\n" 10122 " int Type;\n" 10123 " union {\n" 10124 " int x;\n" 10125 " double y;\n" 10126 " } Value;\n" 10127 " class C {\n" 10128 " MyFavoriteType Value;\n" 10129 " } Class;\n" 10130 "};\n", 10131 WebKitBraceStyle); 10132 } 10133 10134 TEST_F(FormatTest, CatchExceptionReferenceBinding) { 10135 verifyFormat("void f() {\n" 10136 " try {\n" 10137 " } catch (const Exception &e) {\n" 10138 " }\n" 10139 "}\n", 10140 getLLVMStyle()); 10141 } 10142 10143 TEST_F(FormatTest, UnderstandsPragmas) { 10144 verifyFormat("#pragma omp reduction(| : var)"); 10145 verifyFormat("#pragma omp reduction(+ : var)"); 10146 10147 EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string " 10148 "(including parentheses).", 10149 format("#pragma mark Any non-hyphenated or hyphenated string " 10150 "(including parentheses).")); 10151 } 10152 10153 TEST_F(FormatTest, UnderstandPragmaOption) { 10154 verifyFormat("#pragma option -C -A"); 10155 10156 EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A")); 10157 } 10158 10159 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) { 10160 FormatStyle Style = getLLVMStyle(); 10161 Style.ColumnLimit = 20; 10162 10163 verifyFormat("int a; // the\n" 10164 " // comment", Style); 10165 EXPECT_EQ("int a; /* first line\n" 10166 " * second\n" 10167 " * line third\n" 10168 " * line\n" 10169 " */", 10170 format("int a; /* first line\n" 10171 " * second\n" 10172 " * line third\n" 10173 " * line\n" 10174 " */", 10175 Style)); 10176 EXPECT_EQ("int a; // first line\n" 10177 " // second\n" 10178 " // line third\n" 10179 " // line", 10180 format("int a; // first line\n" 10181 " // second line\n" 10182 " // third line", 10183 Style)); 10184 10185 Style.PenaltyExcessCharacter = 90; 10186 verifyFormat("int a; // the comment", Style); 10187 EXPECT_EQ("int a; // the comment\n" 10188 " // aaa", 10189 format("int a; // the comment aaa", Style)); 10190 EXPECT_EQ("int a; /* first line\n" 10191 " * second line\n" 10192 " * third line\n" 10193 " */", 10194 format("int a; /* first line\n" 10195 " * second line\n" 10196 " * third line\n" 10197 " */", 10198 Style)); 10199 EXPECT_EQ("int a; // first line\n" 10200 " // second line\n" 10201 " // third line", 10202 format("int a; // first line\n" 10203 " // second line\n" 10204 " // third line", 10205 Style)); 10206 // FIXME: Investigate why this is not getting the same layout as the test 10207 // above. 10208 EXPECT_EQ("int a; /* first line\n" 10209 " * second line\n" 10210 " * third line\n" 10211 " */", 10212 format("int a; /* first line second line third line" 10213 "\n*/", 10214 Style)); 10215 10216 EXPECT_EQ("// foo bar baz bazfoo\n" 10217 "// foo bar foo bar\n", 10218 format("// foo bar baz bazfoo\n" 10219 "// foo bar foo bar\n", 10220 Style)); 10221 EXPECT_EQ("// foo bar baz bazfoo\n" 10222 "// foo bar foo bar\n", 10223 format("// foo bar baz bazfoo\n" 10224 "// foo bar foo bar\n", 10225 Style)); 10226 10227 // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the 10228 // next one. 10229 EXPECT_EQ("// foo bar baz bazfoo\n" 10230 "// bar foo bar\n", 10231 format("// 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 EXPECT_EQ("// foo bar baz bazfoo\n" 10244 "// foo bar baz bazfoo\n" 10245 "// bar foo bar\n", 10246 format("// foo bar baz bazfoo\n" 10247 "// foo bar baz bazfoo bar\n" 10248 "// foo bar\n", 10249 Style)); 10250 10251 // Make sure we do not keep protruding characters if strict mode reflow is 10252 // cheaper than keeping protruding characters. 10253 Style.ColumnLimit = 21; 10254 EXPECT_EQ("// foo foo foo foo\n" 10255 "// foo foo foo foo\n" 10256 "// foo foo foo foo\n", 10257 format("// foo foo foo foo foo foo foo foo foo foo foo foo\n", 10258 Style)); 10259 10260 EXPECT_EQ("int a = /* long block\n" 10261 " comment */\n" 10262 " 42;", 10263 format("int a = /* long block comment */ 42;", Style)); 10264 } 10265 10266 #define EXPECT_ALL_STYLES_EQUAL(Styles) \ 10267 for (size_t i = 1; i < Styles.size(); ++i) \ 10268 EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \ 10269 << " differs from Style #0" 10270 10271 TEST_F(FormatTest, GetsPredefinedStyleByName) { 10272 SmallVector<FormatStyle, 3> Styles; 10273 Styles.resize(3); 10274 10275 Styles[0] = getLLVMStyle(); 10276 EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1])); 10277 EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2])); 10278 EXPECT_ALL_STYLES_EQUAL(Styles); 10279 10280 Styles[0] = getGoogleStyle(); 10281 EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1])); 10282 EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2])); 10283 EXPECT_ALL_STYLES_EQUAL(Styles); 10284 10285 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 10286 EXPECT_TRUE( 10287 getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1])); 10288 EXPECT_TRUE( 10289 getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2])); 10290 EXPECT_ALL_STYLES_EQUAL(Styles); 10291 10292 Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp); 10293 EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1])); 10294 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2])); 10295 EXPECT_ALL_STYLES_EQUAL(Styles); 10296 10297 Styles[0] = getMozillaStyle(); 10298 EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1])); 10299 EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2])); 10300 EXPECT_ALL_STYLES_EQUAL(Styles); 10301 10302 Styles[0] = getWebKitStyle(); 10303 EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1])); 10304 EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2])); 10305 EXPECT_ALL_STYLES_EQUAL(Styles); 10306 10307 Styles[0] = getGNUStyle(); 10308 EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1])); 10309 EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2])); 10310 EXPECT_ALL_STYLES_EQUAL(Styles); 10311 10312 EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0])); 10313 } 10314 10315 TEST_F(FormatTest, GetsCorrectBasedOnStyle) { 10316 SmallVector<FormatStyle, 8> Styles; 10317 Styles.resize(2); 10318 10319 Styles[0] = getGoogleStyle(); 10320 Styles[1] = getLLVMStyle(); 10321 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 10322 EXPECT_ALL_STYLES_EQUAL(Styles); 10323 10324 Styles.resize(5); 10325 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 10326 Styles[1] = getLLVMStyle(); 10327 Styles[1].Language = FormatStyle::LK_JavaScript; 10328 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 10329 10330 Styles[2] = getLLVMStyle(); 10331 Styles[2].Language = FormatStyle::LK_JavaScript; 10332 EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n" 10333 "BasedOnStyle: Google", 10334 &Styles[2]) 10335 .value()); 10336 10337 Styles[3] = getLLVMStyle(); 10338 Styles[3].Language = FormatStyle::LK_JavaScript; 10339 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n" 10340 "Language: JavaScript", 10341 &Styles[3]) 10342 .value()); 10343 10344 Styles[4] = getLLVMStyle(); 10345 Styles[4].Language = FormatStyle::LK_JavaScript; 10346 EXPECT_EQ(0, parseConfiguration("---\n" 10347 "BasedOnStyle: LLVM\n" 10348 "IndentWidth: 123\n" 10349 "---\n" 10350 "BasedOnStyle: Google\n" 10351 "Language: JavaScript", 10352 &Styles[4]) 10353 .value()); 10354 EXPECT_ALL_STYLES_EQUAL(Styles); 10355 } 10356 10357 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \ 10358 Style.FIELD = false; \ 10359 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \ 10360 EXPECT_TRUE(Style.FIELD); \ 10361 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \ 10362 EXPECT_FALSE(Style.FIELD); 10363 10364 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD) 10365 10366 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \ 10367 Style.STRUCT.FIELD = false; \ 10368 EXPECT_EQ(0, \ 10369 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \ 10370 .value()); \ 10371 EXPECT_TRUE(Style.STRUCT.FIELD); \ 10372 EXPECT_EQ(0, \ 10373 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \ 10374 .value()); \ 10375 EXPECT_FALSE(Style.STRUCT.FIELD); 10376 10377 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \ 10378 CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD) 10379 10380 #define CHECK_PARSE(TEXT, FIELD, VALUE) \ 10381 EXPECT_NE(VALUE, Style.FIELD); \ 10382 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \ 10383 EXPECT_EQ(VALUE, Style.FIELD) 10384 10385 TEST_F(FormatTest, ParsesConfigurationBools) { 10386 FormatStyle Style = {}; 10387 Style.Language = FormatStyle::LK_Cpp; 10388 CHECK_PARSE_BOOL(AlignOperands); 10389 CHECK_PARSE_BOOL(AlignTrailingComments); 10390 CHECK_PARSE_BOOL(AlignConsecutiveAssignments); 10391 CHECK_PARSE_BOOL(AlignConsecutiveDeclarations); 10392 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); 10393 CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine); 10394 CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine); 10395 CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine); 10396 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); 10397 CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations); 10398 CHECK_PARSE_BOOL(BinPackArguments); 10399 CHECK_PARSE_BOOL(BinPackParameters); 10400 CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations); 10401 CHECK_PARSE_BOOL(BreakBeforeTernaryOperators); 10402 CHECK_PARSE_BOOL(BreakStringLiterals); 10403 CHECK_PARSE_BOOL(BreakBeforeInheritanceComma) 10404 CHECK_PARSE_BOOL(CompactNamespaces); 10405 CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine); 10406 CHECK_PARSE_BOOL(DerivePointerAlignment); 10407 CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding"); 10408 CHECK_PARSE_BOOL(DisableFormat); 10409 CHECK_PARSE_BOOL(IndentCaseLabels); 10410 CHECK_PARSE_BOOL(IndentWrappedFunctionNames); 10411 CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks); 10412 CHECK_PARSE_BOOL(ObjCSpaceAfterProperty); 10413 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList); 10414 CHECK_PARSE_BOOL(Cpp11BracedListStyle); 10415 CHECK_PARSE_BOOL(ReflowComments); 10416 CHECK_PARSE_BOOL(SortIncludes); 10417 CHECK_PARSE_BOOL(SortUsingDeclarations); 10418 CHECK_PARSE_BOOL(SpacesInParentheses); 10419 CHECK_PARSE_BOOL(SpacesInSquareBrackets); 10420 CHECK_PARSE_BOOL(SpacesInAngles); 10421 CHECK_PARSE_BOOL(SpaceInEmptyParentheses); 10422 CHECK_PARSE_BOOL(SpacesInContainerLiterals); 10423 CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); 10424 CHECK_PARSE_BOOL(SpaceAfterCStyleCast); 10425 CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword); 10426 CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); 10427 CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon); 10428 CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon); 10429 CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon); 10430 10431 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass); 10432 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement); 10433 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum); 10434 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction); 10435 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace); 10436 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration); 10437 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct); 10438 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion); 10439 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock); 10440 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch); 10441 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse); 10442 CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces); 10443 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction); 10444 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord); 10445 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace); 10446 } 10447 10448 #undef CHECK_PARSE_BOOL 10449 10450 TEST_F(FormatTest, ParsesConfiguration) { 10451 FormatStyle Style = {}; 10452 Style.Language = FormatStyle::LK_Cpp; 10453 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234); 10454 CHECK_PARSE("ConstructorInitializerIndentWidth: 1234", 10455 ConstructorInitializerIndentWidth, 1234u); 10456 CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u); 10457 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); 10458 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u); 10459 CHECK_PARSE("PenaltyBreakAssignment: 1234", 10460 PenaltyBreakAssignment, 1234u); 10461 CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234", 10462 PenaltyBreakBeforeFirstCallParameter, 1234u); 10463 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); 10464 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234", 10465 PenaltyReturnTypeOnItsOwnLine, 1234u); 10466 CHECK_PARSE("SpacesBeforeTrailingComments: 1234", 10467 SpacesBeforeTrailingComments, 1234u); 10468 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u); 10469 CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u); 10470 CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$"); 10471 10472 Style.PointerAlignment = FormatStyle::PAS_Middle; 10473 CHECK_PARSE("PointerAlignment: Left", PointerAlignment, 10474 FormatStyle::PAS_Left); 10475 CHECK_PARSE("PointerAlignment: Right", PointerAlignment, 10476 FormatStyle::PAS_Right); 10477 CHECK_PARSE("PointerAlignment: Middle", PointerAlignment, 10478 FormatStyle::PAS_Middle); 10479 // For backward compatibility: 10480 CHECK_PARSE("PointerBindsToType: Left", PointerAlignment, 10481 FormatStyle::PAS_Left); 10482 CHECK_PARSE("PointerBindsToType: Right", PointerAlignment, 10483 FormatStyle::PAS_Right); 10484 CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment, 10485 FormatStyle::PAS_Middle); 10486 10487 Style.Standard = FormatStyle::LS_Auto; 10488 CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03); 10489 CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11); 10490 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03); 10491 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11); 10492 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto); 10493 10494 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 10495 CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment", 10496 BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment); 10497 CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators, 10498 FormatStyle::BOS_None); 10499 CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators, 10500 FormatStyle::BOS_All); 10501 // For backward compatibility: 10502 CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators, 10503 FormatStyle::BOS_None); 10504 CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators, 10505 FormatStyle::BOS_All); 10506 10507 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 10508 CHECK_PARSE("BreakConstructorInitializers: BeforeComma", 10509 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 10510 CHECK_PARSE("BreakConstructorInitializers: AfterColon", 10511 BreakConstructorInitializers, FormatStyle::BCIS_AfterColon); 10512 CHECK_PARSE("BreakConstructorInitializers: BeforeColon", 10513 BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon); 10514 // For backward compatibility: 10515 CHECK_PARSE("BreakConstructorInitializersBeforeComma: true", 10516 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 10517 10518 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 10519 CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket, 10520 FormatStyle::BAS_Align); 10521 CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket, 10522 FormatStyle::BAS_DontAlign); 10523 CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket, 10524 FormatStyle::BAS_AlwaysBreak); 10525 // For backward compatibility: 10526 CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket, 10527 FormatStyle::BAS_DontAlign); 10528 CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket, 10529 FormatStyle::BAS_Align); 10530 10531 Style.AlignEscapedNewlines = FormatStyle::ENAS_Left; 10532 CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines, 10533 FormatStyle::ENAS_DontAlign); 10534 CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines, 10535 FormatStyle::ENAS_Left); 10536 CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines, 10537 FormatStyle::ENAS_Right); 10538 // For backward compatibility: 10539 CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines, 10540 FormatStyle::ENAS_Left); 10541 CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines, 10542 FormatStyle::ENAS_Right); 10543 10544 Style.UseTab = FormatStyle::UT_ForIndentation; 10545 CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never); 10546 CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation); 10547 CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always); 10548 CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab, 10549 FormatStyle::UT_ForContinuationAndIndentation); 10550 // For backward compatibility: 10551 CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never); 10552 CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always); 10553 10554 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 10555 CHECK_PARSE("AllowShortFunctionsOnASingleLine: None", 10556 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 10557 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline", 10558 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline); 10559 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty", 10560 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty); 10561 CHECK_PARSE("AllowShortFunctionsOnASingleLine: All", 10562 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 10563 // For backward compatibility: 10564 CHECK_PARSE("AllowShortFunctionsOnASingleLine: false", 10565 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 10566 CHECK_PARSE("AllowShortFunctionsOnASingleLine: true", 10567 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 10568 10569 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 10570 CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens, 10571 FormatStyle::SBPO_Never); 10572 CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens, 10573 FormatStyle::SBPO_Always); 10574 CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens, 10575 FormatStyle::SBPO_ControlStatements); 10576 // For backward compatibility: 10577 CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens, 10578 FormatStyle::SBPO_Never); 10579 CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens, 10580 FormatStyle::SBPO_ControlStatements); 10581 10582 Style.ColumnLimit = 123; 10583 FormatStyle BaseStyle = getLLVMStyle(); 10584 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit); 10585 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u); 10586 10587 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 10588 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces, 10589 FormatStyle::BS_Attach); 10590 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces, 10591 FormatStyle::BS_Linux); 10592 CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces, 10593 FormatStyle::BS_Mozilla); 10594 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces, 10595 FormatStyle::BS_Stroustrup); 10596 CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces, 10597 FormatStyle::BS_Allman); 10598 CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU); 10599 CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces, 10600 FormatStyle::BS_WebKit); 10601 CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces, 10602 FormatStyle::BS_Custom); 10603 10604 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 10605 CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType, 10606 FormatStyle::RTBS_None); 10607 CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType, 10608 FormatStyle::RTBS_All); 10609 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel", 10610 AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel); 10611 CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions", 10612 AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions); 10613 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions", 10614 AlwaysBreakAfterReturnType, 10615 FormatStyle::RTBS_TopLevelDefinitions); 10616 10617 Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All; 10618 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None", 10619 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None); 10620 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All", 10621 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All); 10622 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel", 10623 AlwaysBreakAfterDefinitionReturnType, 10624 FormatStyle::DRTBS_TopLevel); 10625 10626 Style.NamespaceIndentation = FormatStyle::NI_All; 10627 CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation, 10628 FormatStyle::NI_None); 10629 CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation, 10630 FormatStyle::NI_Inner); 10631 CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation, 10632 FormatStyle::NI_All); 10633 10634 // FIXME: This is required because parsing a configuration simply overwrites 10635 // the first N elements of the list instead of resetting it. 10636 Style.ForEachMacros.clear(); 10637 std::vector<std::string> BoostForeach; 10638 BoostForeach.push_back("BOOST_FOREACH"); 10639 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach); 10640 std::vector<std::string> BoostAndQForeach; 10641 BoostAndQForeach.push_back("BOOST_FOREACH"); 10642 BoostAndQForeach.push_back("Q_FOREACH"); 10643 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros, 10644 BoostAndQForeach); 10645 10646 Style.IncludeCategories.clear(); 10647 std::vector<FormatStyle::IncludeCategory> ExpectedCategories = {{"abc/.*", 2}, 10648 {".*", 1}}; 10649 CHECK_PARSE("IncludeCategories:\n" 10650 " - Regex: abc/.*\n" 10651 " Priority: 2\n" 10652 " - Regex: .*\n" 10653 " Priority: 1", 10654 IncludeCategories, ExpectedCategories); 10655 CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeIsMainRegex, "abc$"); 10656 10657 Style.RawStringFormats.clear(); 10658 std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = { 10659 { 10660 FormatStyle::LK_TextProto, 10661 {"pb", "proto"}, 10662 {"PARSE_TEXT_PROTO"}, 10663 /*CanonicalDelimiter=*/"", 10664 "llvm", 10665 }, 10666 { 10667 FormatStyle::LK_Cpp, 10668 {"cc", "cpp"}, 10669 {"C_CODEBLOCK", "CPPEVAL"}, 10670 /*CanonicalDelimiter=*/"cc", 10671 /*BasedOnStyle=*/"", 10672 }, 10673 }; 10674 10675 CHECK_PARSE("RawStringFormats:\n" 10676 " - Language: TextProto\n" 10677 " Delimiters:\n" 10678 " - 'pb'\n" 10679 " - 'proto'\n" 10680 " EnclosingFunctions:\n" 10681 " - 'PARSE_TEXT_PROTO'\n" 10682 " BasedOnStyle: llvm\n" 10683 " - Language: Cpp\n" 10684 " Delimiters:\n" 10685 " - 'cc'\n" 10686 " - 'cpp'\n" 10687 " EnclosingFunctions:\n" 10688 " - 'C_CODEBLOCK'\n" 10689 " - 'CPPEVAL'\n" 10690 " CanonicalDelimiter: 'cc'", 10691 RawStringFormats, ExpectedRawStringFormats); 10692 } 10693 10694 TEST_F(FormatTest, ParsesConfigurationWithLanguages) { 10695 FormatStyle Style = {}; 10696 Style.Language = FormatStyle::LK_Cpp; 10697 CHECK_PARSE("Language: Cpp\n" 10698 "IndentWidth: 12", 10699 IndentWidth, 12u); 10700 EXPECT_EQ(parseConfiguration("Language: JavaScript\n" 10701 "IndentWidth: 34", 10702 &Style), 10703 ParseError::Unsuitable); 10704 EXPECT_EQ(12u, Style.IndentWidth); 10705 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 10706 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 10707 10708 Style.Language = FormatStyle::LK_JavaScript; 10709 CHECK_PARSE("Language: JavaScript\n" 10710 "IndentWidth: 12", 10711 IndentWidth, 12u); 10712 CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u); 10713 EXPECT_EQ(parseConfiguration("Language: Cpp\n" 10714 "IndentWidth: 34", 10715 &Style), 10716 ParseError::Unsuitable); 10717 EXPECT_EQ(23u, Style.IndentWidth); 10718 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 10719 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 10720 10721 CHECK_PARSE("BasedOnStyle: LLVM\n" 10722 "IndentWidth: 67", 10723 IndentWidth, 67u); 10724 10725 CHECK_PARSE("---\n" 10726 "Language: JavaScript\n" 10727 "IndentWidth: 12\n" 10728 "---\n" 10729 "Language: Cpp\n" 10730 "IndentWidth: 34\n" 10731 "...\n", 10732 IndentWidth, 12u); 10733 10734 Style.Language = FormatStyle::LK_Cpp; 10735 CHECK_PARSE("---\n" 10736 "Language: JavaScript\n" 10737 "IndentWidth: 12\n" 10738 "---\n" 10739 "Language: Cpp\n" 10740 "IndentWidth: 34\n" 10741 "...\n", 10742 IndentWidth, 34u); 10743 CHECK_PARSE("---\n" 10744 "IndentWidth: 78\n" 10745 "---\n" 10746 "Language: JavaScript\n" 10747 "IndentWidth: 56\n" 10748 "...\n", 10749 IndentWidth, 78u); 10750 10751 Style.ColumnLimit = 123; 10752 Style.IndentWidth = 234; 10753 Style.BreakBeforeBraces = FormatStyle::BS_Linux; 10754 Style.TabWidth = 345; 10755 EXPECT_FALSE(parseConfiguration("---\n" 10756 "IndentWidth: 456\n" 10757 "BreakBeforeBraces: Allman\n" 10758 "---\n" 10759 "Language: JavaScript\n" 10760 "IndentWidth: 111\n" 10761 "TabWidth: 111\n" 10762 "---\n" 10763 "Language: Cpp\n" 10764 "BreakBeforeBraces: Stroustrup\n" 10765 "TabWidth: 789\n" 10766 "...\n", 10767 &Style)); 10768 EXPECT_EQ(123u, Style.ColumnLimit); 10769 EXPECT_EQ(456u, Style.IndentWidth); 10770 EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces); 10771 EXPECT_EQ(789u, Style.TabWidth); 10772 10773 EXPECT_EQ(parseConfiguration("---\n" 10774 "Language: JavaScript\n" 10775 "IndentWidth: 56\n" 10776 "---\n" 10777 "IndentWidth: 78\n" 10778 "...\n", 10779 &Style), 10780 ParseError::Error); 10781 EXPECT_EQ(parseConfiguration("---\n" 10782 "Language: JavaScript\n" 10783 "IndentWidth: 56\n" 10784 "---\n" 10785 "Language: JavaScript\n" 10786 "IndentWidth: 78\n" 10787 "...\n", 10788 &Style), 10789 ParseError::Error); 10790 10791 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 10792 } 10793 10794 #undef CHECK_PARSE 10795 10796 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) { 10797 FormatStyle Style = {}; 10798 Style.Language = FormatStyle::LK_JavaScript; 10799 Style.BreakBeforeTernaryOperators = true; 10800 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value()); 10801 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 10802 10803 Style.BreakBeforeTernaryOperators = true; 10804 EXPECT_EQ(0, parseConfiguration("---\n" 10805 "BasedOnStyle: Google\n" 10806 "---\n" 10807 "Language: JavaScript\n" 10808 "IndentWidth: 76\n" 10809 "...\n", 10810 &Style) 10811 .value()); 10812 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 10813 EXPECT_EQ(76u, Style.IndentWidth); 10814 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 10815 } 10816 10817 TEST_F(FormatTest, ConfigurationRoundTripTest) { 10818 FormatStyle Style = getLLVMStyle(); 10819 std::string YAML = configurationAsText(Style); 10820 FormatStyle ParsedStyle = {}; 10821 ParsedStyle.Language = FormatStyle::LK_Cpp; 10822 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value()); 10823 EXPECT_EQ(Style, ParsedStyle); 10824 } 10825 10826 TEST_F(FormatTest, WorksFor8bitEncodings) { 10827 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" 10828 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" 10829 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" 10830 "\"\xef\xee\xf0\xf3...\"", 10831 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " 10832 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " 10833 "\xef\xee\xf0\xf3...\"", 10834 getLLVMStyleWithColumns(12))); 10835 } 10836 10837 TEST_F(FormatTest, HandlesUTF8BOM) { 10838 EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf")); 10839 EXPECT_EQ("\xef\xbb\xbf#include <iostream>", 10840 format("\xef\xbb\xbf#include <iostream>")); 10841 EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>", 10842 format("\xef\xbb\xbf\n#include <iostream>")); 10843 } 10844 10845 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. 10846 #if !defined(_MSC_VER) 10847 10848 TEST_F(FormatTest, CountsUTF8CharactersProperly) { 10849 verifyFormat("\"Однажды в студёную зимнюю пору...\"", 10850 getLLVMStyleWithColumns(35)); 10851 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", 10852 getLLVMStyleWithColumns(31)); 10853 verifyFormat("// Однажды в студёную зимнюю пору...", 10854 getLLVMStyleWithColumns(36)); 10855 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32)); 10856 verifyFormat("/* Однажды в студёную зимнюю пору... */", 10857 getLLVMStyleWithColumns(39)); 10858 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", 10859 getLLVMStyleWithColumns(35)); 10860 } 10861 10862 TEST_F(FormatTest, SplitsUTF8Strings) { 10863 // Non-printable characters' width is currently considered to be the length in 10864 // bytes in UTF8. The characters can be displayed in very different manner 10865 // (zero-width, single width with a substitution glyph, expanded to their code 10866 // (e.g. "<8d>"), so there's no single correct way to handle them. 10867 EXPECT_EQ("\"aaaaÄ\"\n" 10868 "\"\xc2\x8d\";", 10869 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 10870 EXPECT_EQ("\"aaaaaaaÄ\"\n" 10871 "\"\xc2\x8d\";", 10872 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 10873 EXPECT_EQ("\"Однажды, в \"\n" 10874 "\"студёную \"\n" 10875 "\"зимнюю \"\n" 10876 "\"пору,\"", 10877 format("\"Однажды, в студёную зимнюю пору,\"", 10878 getLLVMStyleWithColumns(13))); 10879 EXPECT_EQ( 10880 "\"一 二 三 \"\n" 10881 "\"四 五六 \"\n" 10882 "\"七 八 九 \"\n" 10883 "\"十\"", 10884 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); 10885 EXPECT_EQ("\"一\t\"\n" 10886 "\"二 \t\"\n" 10887 "\"三 四 \"\n" 10888 "\"五\t\"\n" 10889 "\"六 \t\"\n" 10890 "\"七 \"\n" 10891 "\"八九十\tqq\"", 10892 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", 10893 getLLVMStyleWithColumns(11))); 10894 10895 // UTF8 character in an escape sequence. 10896 EXPECT_EQ("\"aaaaaa\"\n" 10897 "\"\\\xC2\x8D\"", 10898 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10))); 10899 } 10900 10901 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { 10902 EXPECT_EQ("const char *sssss =\n" 10903 " \"一二三四五六七八\\\n" 10904 " 九 十\";", 10905 format("const char *sssss = \"一二三四五六七八\\\n" 10906 " 九 十\";", 10907 getLLVMStyleWithColumns(30))); 10908 } 10909 10910 TEST_F(FormatTest, SplitsUTF8LineComments) { 10911 EXPECT_EQ("// aaaaÄ\xc2\x8d", 10912 format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10))); 10913 EXPECT_EQ("// Я из лесу\n" 10914 "// вышел; был\n" 10915 "// сильный\n" 10916 "// мороз.", 10917 format("// Я из лесу вышел; был сильный мороз.", 10918 getLLVMStyleWithColumns(13))); 10919 EXPECT_EQ("// 一二三\n" 10920 "// 四五六七\n" 10921 "// 八 九\n" 10922 "// 十", 10923 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9))); 10924 } 10925 10926 TEST_F(FormatTest, SplitsUTF8BlockComments) { 10927 EXPECT_EQ("/* Гляжу,\n" 10928 " * поднимается\n" 10929 " * медленно в\n" 10930 " * гору\n" 10931 " * Лошадка,\n" 10932 " * везущая\n" 10933 " * хворосту\n" 10934 " * воз. */", 10935 format("/* Гляжу, поднимается медленно в гору\n" 10936 " * Лошадка, везущая хворосту воз. */", 10937 getLLVMStyleWithColumns(13))); 10938 EXPECT_EQ( 10939 "/* 一二三\n" 10940 " * 四五六七\n" 10941 " * 八 九\n" 10942 " * 十 */", 10943 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9))); 10944 EXPECT_EQ("/* \n" 10945 " * \n" 10946 " * - */", 10947 format("/* - */", getLLVMStyleWithColumns(12))); 10948 } 10949 10950 #endif // _MSC_VER 10951 10952 TEST_F(FormatTest, ConstructorInitializerIndentWidth) { 10953 FormatStyle Style = getLLVMStyle(); 10954 10955 Style.ConstructorInitializerIndentWidth = 4; 10956 verifyFormat( 10957 "SomeClass::Constructor()\n" 10958 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10959 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10960 Style); 10961 10962 Style.ConstructorInitializerIndentWidth = 2; 10963 verifyFormat( 10964 "SomeClass::Constructor()\n" 10965 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10966 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10967 Style); 10968 10969 Style.ConstructorInitializerIndentWidth = 0; 10970 verifyFormat( 10971 "SomeClass::Constructor()\n" 10972 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10973 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10974 Style); 10975 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 10976 verifyFormat( 10977 "SomeLongTemplateVariableName<\n" 10978 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>", 10979 Style); 10980 verifyFormat( 10981 "bool smaller = 1 < bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 10982 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 10983 Style); 10984 } 10985 10986 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) { 10987 FormatStyle Style = getLLVMStyle(); 10988 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 10989 Style.ConstructorInitializerIndentWidth = 4; 10990 verifyFormat("SomeClass::Constructor()\n" 10991 " : a(a)\n" 10992 " , b(b)\n" 10993 " , c(c) {}", 10994 Style); 10995 verifyFormat("SomeClass::Constructor()\n" 10996 " : a(a) {}", 10997 Style); 10998 10999 Style.ColumnLimit = 0; 11000 verifyFormat("SomeClass::Constructor()\n" 11001 " : a(a) {}", 11002 Style); 11003 verifyFormat("SomeClass::Constructor() noexcept\n" 11004 " : a(a) {}", 11005 Style); 11006 verifyFormat("SomeClass::Constructor()\n" 11007 " : a(a)\n" 11008 " , b(b)\n" 11009 " , c(c) {}", 11010 Style); 11011 verifyFormat("SomeClass::Constructor()\n" 11012 " : a(a) {\n" 11013 " foo();\n" 11014 " bar();\n" 11015 "}", 11016 Style); 11017 11018 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 11019 verifyFormat("SomeClass::Constructor()\n" 11020 " : a(a)\n" 11021 " , b(b)\n" 11022 " , c(c) {\n}", 11023 Style); 11024 verifyFormat("SomeClass::Constructor()\n" 11025 " : a(a) {\n}", 11026 Style); 11027 11028 Style.ColumnLimit = 80; 11029 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 11030 Style.ConstructorInitializerIndentWidth = 2; 11031 verifyFormat("SomeClass::Constructor()\n" 11032 " : a(a)\n" 11033 " , b(b)\n" 11034 " , c(c) {}", 11035 Style); 11036 11037 Style.ConstructorInitializerIndentWidth = 0; 11038 verifyFormat("SomeClass::Constructor()\n" 11039 ": a(a)\n" 11040 ", b(b)\n" 11041 ", c(c) {}", 11042 Style); 11043 11044 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 11045 Style.ConstructorInitializerIndentWidth = 4; 11046 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style); 11047 verifyFormat( 11048 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n", 11049 Style); 11050 verifyFormat( 11051 "SomeClass::Constructor()\n" 11052 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", 11053 Style); 11054 Style.ConstructorInitializerIndentWidth = 4; 11055 Style.ColumnLimit = 60; 11056 verifyFormat("SomeClass::Constructor()\n" 11057 " : aaaaaaaa(aaaaaaaa)\n" 11058 " , aaaaaaaa(aaaaaaaa)\n" 11059 " , aaaaaaaa(aaaaaaaa) {}", 11060 Style); 11061 } 11062 11063 TEST_F(FormatTest, Destructors) { 11064 verifyFormat("void F(int &i) { i.~int(); }"); 11065 verifyFormat("void F(int &i) { i->~int(); }"); 11066 } 11067 11068 TEST_F(FormatTest, FormatsWithWebKitStyle) { 11069 FormatStyle Style = getWebKitStyle(); 11070 11071 // Don't indent in outer namespaces. 11072 verifyFormat("namespace outer {\n" 11073 "int i;\n" 11074 "namespace inner {\n" 11075 " int i;\n" 11076 "} // namespace inner\n" 11077 "} // namespace outer\n" 11078 "namespace other_outer {\n" 11079 "int i;\n" 11080 "}", 11081 Style); 11082 11083 // Don't indent case labels. 11084 verifyFormat("switch (variable) {\n" 11085 "case 1:\n" 11086 "case 2:\n" 11087 " doSomething();\n" 11088 " break;\n" 11089 "default:\n" 11090 " ++variable;\n" 11091 "}", 11092 Style); 11093 11094 // Wrap before binary operators. 11095 EXPECT_EQ("void f()\n" 11096 "{\n" 11097 " if (aaaaaaaaaaaaaaaa\n" 11098 " && bbbbbbbbbbbbbbbbbbbbbbbb\n" 11099 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 11100 " return;\n" 11101 "}", 11102 format("void f() {\n" 11103 "if (aaaaaaaaaaaaaaaa\n" 11104 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" 11105 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 11106 "return;\n" 11107 "}", 11108 Style)); 11109 11110 // Allow functions on a single line. 11111 verifyFormat("void f() { return; }", Style); 11112 11113 // Constructor initializers are formatted one per line with the "," on the 11114 // new line. 11115 verifyFormat("Constructor()\n" 11116 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 11117 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" 11118 " aaaaaaaaaaaaaa)\n" 11119 " , aaaaaaaaaaaaaaaaaaaaaaa()\n" 11120 "{\n" 11121 "}", 11122 Style); 11123 verifyFormat("SomeClass::Constructor()\n" 11124 " : a(a)\n" 11125 "{\n" 11126 "}", 11127 Style); 11128 EXPECT_EQ("SomeClass::Constructor()\n" 11129 " : a(a)\n" 11130 "{\n" 11131 "}", 11132 format("SomeClass::Constructor():a(a){}", Style)); 11133 verifyFormat("SomeClass::Constructor()\n" 11134 " : a(a)\n" 11135 " , b(b)\n" 11136 " , c(c)\n" 11137 "{\n" 11138 "}", 11139 Style); 11140 verifyFormat("SomeClass::Constructor()\n" 11141 " : a(a)\n" 11142 "{\n" 11143 " foo();\n" 11144 " bar();\n" 11145 "}", 11146 Style); 11147 11148 // Access specifiers should be aligned left. 11149 verifyFormat("class C {\n" 11150 "public:\n" 11151 " int i;\n" 11152 "};", 11153 Style); 11154 11155 // Do not align comments. 11156 verifyFormat("int a; // Do not\n" 11157 "double b; // align comments.", 11158 Style); 11159 11160 // Do not align operands. 11161 EXPECT_EQ("ASSERT(aaaa\n" 11162 " || bbbb);", 11163 format("ASSERT ( aaaa\n||bbbb);", Style)); 11164 11165 // Accept input's line breaks. 11166 EXPECT_EQ("if (aaaaaaaaaaaaaaa\n" 11167 " || bbbbbbbbbbbbbbb) {\n" 11168 " i++;\n" 11169 "}", 11170 format("if (aaaaaaaaaaaaaaa\n" 11171 "|| bbbbbbbbbbbbbbb) { i++; }", 11172 Style)); 11173 EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n" 11174 " i++;\n" 11175 "}", 11176 format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style)); 11177 11178 // Don't automatically break all macro definitions (llvm.org/PR17842). 11179 verifyFormat("#define aNumber 10", Style); 11180 // However, generally keep the line breaks that the user authored. 11181 EXPECT_EQ("#define aNumber \\\n" 11182 " 10", 11183 format("#define aNumber \\\n" 11184 " 10", 11185 Style)); 11186 11187 // Keep empty and one-element array literals on a single line. 11188 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n" 11189 " copyItems:YES];", 11190 format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n" 11191 "copyItems:YES];", 11192 Style)); 11193 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n" 11194 " copyItems:YES];", 11195 format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n" 11196 " copyItems:YES];", 11197 Style)); 11198 // FIXME: This does not seem right, there should be more indentation before 11199 // the array literal's entries. Nested blocks have the same problem. 11200 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 11201 " @\"a\",\n" 11202 " @\"a\"\n" 11203 "]\n" 11204 " copyItems:YES];", 11205 format("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 11206 " @\"a\",\n" 11207 " @\"a\"\n" 11208 " ]\n" 11209 " copyItems:YES];", 11210 Style)); 11211 EXPECT_EQ( 11212 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 11213 " copyItems:YES];", 11214 format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 11215 " copyItems:YES];", 11216 Style)); 11217 11218 verifyFormat("[self.a b:c c:d];", Style); 11219 EXPECT_EQ("[self.a b:c\n" 11220 " c:d];", 11221 format("[self.a b:c\n" 11222 "c:d];", 11223 Style)); 11224 } 11225 11226 TEST_F(FormatTest, FormatsLambdas) { 11227 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n"); 11228 verifyFormat("int c = [&] { [=] { return b++; }(); }();\n"); 11229 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n"); 11230 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n"); 11231 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n"); 11232 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n"); 11233 verifyFormat("auto c = [a = [b = 42] {}] {};\n"); 11234 verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n"); 11235 verifyFormat("int x = f(*+[] {});"); 11236 verifyFormat("void f() {\n" 11237 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n" 11238 "}\n"); 11239 verifyFormat("void f() {\n" 11240 " other(x.begin(), //\n" 11241 " x.end(), //\n" 11242 " [&](int, int) { return 1; });\n" 11243 "}\n"); 11244 verifyFormat("SomeFunction([]() { // A cool function...\n" 11245 " return 43;\n" 11246 "});"); 11247 EXPECT_EQ("SomeFunction([]() {\n" 11248 "#define A a\n" 11249 " return 43;\n" 11250 "});", 11251 format("SomeFunction([](){\n" 11252 "#define A a\n" 11253 "return 43;\n" 11254 "});")); 11255 verifyFormat("void f() {\n" 11256 " SomeFunction([](decltype(x), A *a) {});\n" 11257 "}"); 11258 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 11259 " [](const aaaaaaaaaa &a) { return a; });"); 11260 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n" 11261 " SomeOtherFunctioooooooooooooooooooooooooon();\n" 11262 "});"); 11263 verifyFormat("Constructor()\n" 11264 " : Field([] { // comment\n" 11265 " int i;\n" 11266 " }) {}"); 11267 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n" 11268 " return some_parameter.size();\n" 11269 "};"); 11270 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n" 11271 " [](const string &s) { return s; };"); 11272 verifyFormat("int i = aaaaaa ? 1 //\n" 11273 " : [] {\n" 11274 " return 2; //\n" 11275 " }();"); 11276 verifyFormat("llvm::errs() << \"number of twos is \"\n" 11277 " << std::count_if(v.begin(), v.end(), [](int x) {\n" 11278 " return x == 2; // force break\n" 11279 " });"); 11280 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 11281 " [=](int iiiiiiiiiiii) {\n" 11282 " return aaaaaaaaaaaaaaaaaaaaaaa !=\n" 11283 " aaaaaaaaaaaaaaaaaaaaaaa;\n" 11284 " });", 11285 getLLVMStyleWithColumns(60)); 11286 verifyFormat("SomeFunction({[&] {\n" 11287 " // comment\n" 11288 " },\n" 11289 " [&] {\n" 11290 " // comment\n" 11291 " }});"); 11292 verifyFormat("SomeFunction({[&] {\n" 11293 " // comment\n" 11294 "}});"); 11295 verifyFormat("virtual aaaaaaaaaaaaaaaa(std::function<bool()> bbbbbbbbbbbb =\n" 11296 " [&]() { return true; },\n" 11297 " aaaaa aaaaaaaaa);"); 11298 11299 // Lambdas with return types. 11300 verifyFormat("int c = []() -> int { return 2; }();\n"); 11301 verifyFormat("int c = []() -> int * { return 2; }();\n"); 11302 verifyFormat("int c = []() -> vector<int> { return {2}; }();\n"); 11303 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());"); 11304 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};"); 11305 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};"); 11306 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};"); 11307 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};"); 11308 verifyFormat("[a, a]() -> a<1> {};"); 11309 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n" 11310 " int j) -> int {\n" 11311 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n" 11312 "};"); 11313 verifyFormat( 11314 "aaaaaaaaaaaaaaaaaaaaaa(\n" 11315 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n" 11316 " return aaaaaaaaaaaaaaaaa;\n" 11317 " });", 11318 getLLVMStyleWithColumns(70)); 11319 verifyFormat("[]() //\n" 11320 " -> int {\n" 11321 " return 1; //\n" 11322 "};"); 11323 11324 // Multiple lambdas in the same parentheses change indentation rules. 11325 verifyFormat("SomeFunction(\n" 11326 " []() {\n" 11327 " int i = 42;\n" 11328 " return i;\n" 11329 " },\n" 11330 " []() {\n" 11331 " int j = 43;\n" 11332 " return j;\n" 11333 " });"); 11334 11335 // More complex introducers. 11336 verifyFormat("return [i, args...] {};"); 11337 11338 // Not lambdas. 11339 verifyFormat("constexpr char hello[]{\"hello\"};"); 11340 verifyFormat("double &operator[](int i) { return 0; }\n" 11341 "int i;"); 11342 verifyFormat("std::unique_ptr<int[]> foo() {}"); 11343 verifyFormat("int i = a[a][a]->f();"); 11344 verifyFormat("int i = (*b)[a]->f();"); 11345 11346 // Other corner cases. 11347 verifyFormat("void f() {\n" 11348 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n" 11349 " );\n" 11350 "}"); 11351 11352 // Lambdas created through weird macros. 11353 verifyFormat("void f() {\n" 11354 " MACRO((const AA &a) { return 1; });\n" 11355 " MACRO((AA &a) { return 1; });\n" 11356 "}"); 11357 11358 verifyFormat("if (blah_blah(whatever, whatever, [] {\n" 11359 " doo_dah();\n" 11360 " doo_dah();\n" 11361 " })) {\n" 11362 "}"); 11363 verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n" 11364 " doo_dah();\n" 11365 " doo_dah();\n" 11366 " })) {\n" 11367 "}"); 11368 verifyFormat("auto lambda = []() {\n" 11369 " int a = 2\n" 11370 "#if A\n" 11371 " + 2\n" 11372 "#endif\n" 11373 " ;\n" 11374 "};"); 11375 11376 // Lambdas with complex multiline introducers. 11377 verifyFormat( 11378 "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 11379 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n" 11380 " -> ::std::unordered_set<\n" 11381 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n" 11382 " //\n" 11383 " });"); 11384 } 11385 11386 TEST_F(FormatTest, EmptyLinesInLambdas) { 11387 verifyFormat("auto lambda = []() {\n" 11388 " x(); //\n" 11389 "};", 11390 "auto lambda = []() {\n" 11391 "\n" 11392 " x(); //\n" 11393 "\n" 11394 "};"); 11395 } 11396 11397 TEST_F(FormatTest, FormatsBlocks) { 11398 FormatStyle ShortBlocks = getLLVMStyle(); 11399 ShortBlocks.AllowShortBlocksOnASingleLine = true; 11400 verifyFormat("int (^Block)(int, int);", ShortBlocks); 11401 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks); 11402 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks); 11403 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks); 11404 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks); 11405 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks); 11406 11407 verifyFormat("foo(^{ bar(); });", ShortBlocks); 11408 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks); 11409 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks); 11410 11411 verifyFormat("[operation setCompletionBlock:^{\n" 11412 " [self onOperationDone];\n" 11413 "}];"); 11414 verifyFormat("int i = {[operation setCompletionBlock:^{\n" 11415 " [self onOperationDone];\n" 11416 "}]};"); 11417 verifyFormat("[operation setCompletionBlock:^(int *i) {\n" 11418 " f();\n" 11419 "}];"); 11420 verifyFormat("int a = [operation block:^int(int *i) {\n" 11421 " return 1;\n" 11422 "}];"); 11423 verifyFormat("[myObject doSomethingWith:arg1\n" 11424 " aaa:^int(int *a) {\n" 11425 " return 1;\n" 11426 " }\n" 11427 " bbb:f(a * bbbbbbbb)];"); 11428 11429 verifyFormat("[operation setCompletionBlock:^{\n" 11430 " [self.delegate newDataAvailable];\n" 11431 "}];", 11432 getLLVMStyleWithColumns(60)); 11433 verifyFormat("dispatch_async(_fileIOQueue, ^{\n" 11434 " NSString *path = [self sessionFilePath];\n" 11435 " if (path) {\n" 11436 " // ...\n" 11437 " }\n" 11438 "});"); 11439 verifyFormat("[[SessionService sharedService]\n" 11440 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 11441 " if (window) {\n" 11442 " [self windowDidLoad:window];\n" 11443 " } else {\n" 11444 " [self errorLoadingWindow];\n" 11445 " }\n" 11446 " }];"); 11447 verifyFormat("void (^largeBlock)(void) = ^{\n" 11448 " // ...\n" 11449 "};\n", 11450 getLLVMStyleWithColumns(40)); 11451 verifyFormat("[[SessionService sharedService]\n" 11452 " loadWindowWithCompletionBlock: //\n" 11453 " ^(SessionWindow *window) {\n" 11454 " if (window) {\n" 11455 " [self windowDidLoad:window];\n" 11456 " } else {\n" 11457 " [self errorLoadingWindow];\n" 11458 " }\n" 11459 " }];", 11460 getLLVMStyleWithColumns(60)); 11461 verifyFormat("[myObject doSomethingWith:arg1\n" 11462 " firstBlock:^(Foo *a) {\n" 11463 " // ...\n" 11464 " int i;\n" 11465 " }\n" 11466 " secondBlock:^(Bar *b) {\n" 11467 " // ...\n" 11468 " int i;\n" 11469 " }\n" 11470 " thirdBlock:^Foo(Bar *b) {\n" 11471 " // ...\n" 11472 " int i;\n" 11473 " }];"); 11474 verifyFormat("[myObject doSomethingWith:arg1\n" 11475 " firstBlock:-1\n" 11476 " secondBlock:^(Bar *b) {\n" 11477 " // ...\n" 11478 " int i;\n" 11479 " }];"); 11480 11481 verifyFormat("f(^{\n" 11482 " @autoreleasepool {\n" 11483 " if (a) {\n" 11484 " g();\n" 11485 " }\n" 11486 " }\n" 11487 "});"); 11488 verifyFormat("Block b = ^int *(A *a, B *b) {}"); 11489 verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n" 11490 "};"); 11491 11492 FormatStyle FourIndent = getLLVMStyle(); 11493 FourIndent.ObjCBlockIndentWidth = 4; 11494 verifyFormat("[operation setCompletionBlock:^{\n" 11495 " [self onOperationDone];\n" 11496 "}];", 11497 FourIndent); 11498 } 11499 11500 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) { 11501 FormatStyle ZeroColumn = getLLVMStyle(); 11502 ZeroColumn.ColumnLimit = 0; 11503 11504 verifyFormat("[[SessionService sharedService] " 11505 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 11506 " if (window) {\n" 11507 " [self windowDidLoad:window];\n" 11508 " } else {\n" 11509 " [self errorLoadingWindow];\n" 11510 " }\n" 11511 "}];", 11512 ZeroColumn); 11513 EXPECT_EQ("[[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 format("[[SessionService sharedService]\n" 11522 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 11523 " if (window) {\n" 11524 " [self windowDidLoad:window];\n" 11525 " } else {\n" 11526 " [self errorLoadingWindow];\n" 11527 " }\n" 11528 "}];", 11529 ZeroColumn)); 11530 verifyFormat("[myObject doSomethingWith:arg1\n" 11531 " firstBlock:^(Foo *a) {\n" 11532 " // ...\n" 11533 " int i;\n" 11534 " }\n" 11535 " secondBlock:^(Bar *b) {\n" 11536 " // ...\n" 11537 " int i;\n" 11538 " }\n" 11539 " thirdBlock:^Foo(Bar *b) {\n" 11540 " // ...\n" 11541 " int i;\n" 11542 " }];", 11543 ZeroColumn); 11544 verifyFormat("f(^{\n" 11545 " @autoreleasepool {\n" 11546 " if (a) {\n" 11547 " g();\n" 11548 " }\n" 11549 " }\n" 11550 "});", 11551 ZeroColumn); 11552 verifyFormat("void (^largeBlock)(void) = ^{\n" 11553 " // ...\n" 11554 "};", 11555 ZeroColumn); 11556 11557 ZeroColumn.AllowShortBlocksOnASingleLine = true; 11558 EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };", 11559 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 11560 ZeroColumn.AllowShortBlocksOnASingleLine = false; 11561 EXPECT_EQ("void (^largeBlock)(void) = ^{\n" 11562 " int i;\n" 11563 "};", 11564 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 11565 } 11566 11567 TEST_F(FormatTest, SupportsCRLF) { 11568 EXPECT_EQ("int a;\r\n" 11569 "int b;\r\n" 11570 "int c;\r\n", 11571 format("int a;\r\n" 11572 " int b;\r\n" 11573 " int c;\r\n", 11574 getLLVMStyle())); 11575 EXPECT_EQ("int a;\r\n" 11576 "int b;\r\n" 11577 "int c;\r\n", 11578 format("int a;\r\n" 11579 " int b;\n" 11580 " int c;\r\n", 11581 getLLVMStyle())); 11582 EXPECT_EQ("int a;\n" 11583 "int b;\n" 11584 "int c;\n", 11585 format("int a;\r\n" 11586 " int b;\n" 11587 " int c;\n", 11588 getLLVMStyle())); 11589 EXPECT_EQ("\"aaaaaaa \"\r\n" 11590 "\"bbbbbbb\";\r\n", 11591 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10))); 11592 EXPECT_EQ("#define A \\\r\n" 11593 " b; \\\r\n" 11594 " c; \\\r\n" 11595 " d;\r\n", 11596 format("#define A \\\r\n" 11597 " b; \\\r\n" 11598 " c; d; \r\n", 11599 getGoogleStyle())); 11600 11601 EXPECT_EQ("/*\r\n" 11602 "multi line block comments\r\n" 11603 "should not introduce\r\n" 11604 "an extra carriage return\r\n" 11605 "*/\r\n", 11606 format("/*\r\n" 11607 "multi line block comments\r\n" 11608 "should not introduce\r\n" 11609 "an extra carriage return\r\n" 11610 "*/\r\n")); 11611 } 11612 11613 TEST_F(FormatTest, MunchSemicolonAfterBlocks) { 11614 verifyFormat("MY_CLASS(C) {\n" 11615 " int i;\n" 11616 " int j;\n" 11617 "};"); 11618 } 11619 11620 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) { 11621 FormatStyle TwoIndent = getLLVMStyleWithColumns(15); 11622 TwoIndent.ContinuationIndentWidth = 2; 11623 11624 EXPECT_EQ("int i =\n" 11625 " longFunction(\n" 11626 " arg);", 11627 format("int i = longFunction(arg);", TwoIndent)); 11628 11629 FormatStyle SixIndent = getLLVMStyleWithColumns(20); 11630 SixIndent.ContinuationIndentWidth = 6; 11631 11632 EXPECT_EQ("int i =\n" 11633 " longFunction(\n" 11634 " arg);", 11635 format("int i = longFunction(arg);", SixIndent)); 11636 } 11637 11638 TEST_F(FormatTest, SpacesInAngles) { 11639 FormatStyle Spaces = getLLVMStyle(); 11640 Spaces.SpacesInAngles = true; 11641 11642 verifyFormat("static_cast< int >(arg);", Spaces); 11643 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces); 11644 verifyFormat("f< int, float >();", Spaces); 11645 verifyFormat("template <> g() {}", Spaces); 11646 verifyFormat("template < std::vector< int > > f() {}", Spaces); 11647 verifyFormat("std::function< void(int, int) > fct;", Spaces); 11648 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }", 11649 Spaces); 11650 11651 Spaces.Standard = FormatStyle::LS_Cpp03; 11652 Spaces.SpacesInAngles = true; 11653 verifyFormat("A< A< int > >();", Spaces); 11654 11655 Spaces.SpacesInAngles = false; 11656 verifyFormat("A<A<int> >();", Spaces); 11657 11658 Spaces.Standard = FormatStyle::LS_Cpp11; 11659 Spaces.SpacesInAngles = true; 11660 verifyFormat("A< A< int > >();", Spaces); 11661 11662 Spaces.SpacesInAngles = false; 11663 verifyFormat("A<A<int>>();", Spaces); 11664 } 11665 11666 TEST_F(FormatTest, SpaceAfterTemplateKeyword) { 11667 FormatStyle Style = getLLVMStyle(); 11668 Style.SpaceAfterTemplateKeyword = false; 11669 verifyFormat("template<int> void foo();", Style); 11670 } 11671 11672 TEST_F(FormatTest, TripleAngleBrackets) { 11673 verifyFormat("f<<<1, 1>>>();"); 11674 verifyFormat("f<<<1, 1, 1, s>>>();"); 11675 verifyFormat("f<<<a, b, c, d>>>();"); 11676 EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();")); 11677 verifyFormat("f<param><<<1, 1>>>();"); 11678 verifyFormat("f<1><<<1, 1>>>();"); 11679 EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();")); 11680 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 11681 "aaaaaaaaaaa<<<\n 1, 1>>>();"); 11682 verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n" 11683 " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();"); 11684 } 11685 11686 TEST_F(FormatTest, MergeLessLessAtEnd) { 11687 verifyFormat("<<"); 11688 EXPECT_EQ("< < <", format("\\\n<<<")); 11689 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 11690 "aaallvm::outs() <<"); 11691 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 11692 "aaaallvm::outs()\n <<"); 11693 } 11694 11695 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) { 11696 std::string code = "#if A\n" 11697 "#if B\n" 11698 "a.\n" 11699 "#endif\n" 11700 " a = 1;\n" 11701 "#else\n" 11702 "#endif\n" 11703 "#if C\n" 11704 "#else\n" 11705 "#endif\n"; 11706 EXPECT_EQ(code, format(code)); 11707 } 11708 11709 TEST_F(FormatTest, HandleConflictMarkers) { 11710 // Git/SVN conflict markers. 11711 EXPECT_EQ("int a;\n" 11712 "void f() {\n" 11713 " callme(some(parameter1,\n" 11714 "<<<<<<< text by the vcs\n" 11715 " parameter2),\n" 11716 "||||||| text by the vcs\n" 11717 " parameter2),\n" 11718 " parameter3,\n" 11719 "======= text by the vcs\n" 11720 " parameter2, parameter3),\n" 11721 ">>>>>>> text by the vcs\n" 11722 " otherparameter);\n", 11723 format("int a;\n" 11724 "void f() {\n" 11725 " callme(some(parameter1,\n" 11726 "<<<<<<< text by the vcs\n" 11727 " parameter2),\n" 11728 "||||||| text by the vcs\n" 11729 " parameter2),\n" 11730 " parameter3,\n" 11731 "======= text by the vcs\n" 11732 " parameter2,\n" 11733 " parameter3),\n" 11734 ">>>>>>> text by the vcs\n" 11735 " otherparameter);\n")); 11736 11737 // Perforce markers. 11738 EXPECT_EQ("void f() {\n" 11739 " function(\n" 11740 ">>>> text by the vcs\n" 11741 " parameter,\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 format("void f() {\n" 11749 " function(\n" 11750 ">>>> text by the vcs\n" 11751 " parameter,\n" 11752 "==== text by the vcs\n" 11753 " parameter,\n" 11754 "==== text by the vcs\n" 11755 " parameter,\n" 11756 "<<<< text by the vcs\n" 11757 " parameter);\n")); 11758 11759 EXPECT_EQ("<<<<<<<\n" 11760 "|||||||\n" 11761 "=======\n" 11762 ">>>>>>>", 11763 format("<<<<<<<\n" 11764 "|||||||\n" 11765 "=======\n" 11766 ">>>>>>>")); 11767 11768 EXPECT_EQ("<<<<<<<\n" 11769 "|||||||\n" 11770 "int i;\n" 11771 "=======\n" 11772 ">>>>>>>", 11773 format("<<<<<<<\n" 11774 "|||||||\n" 11775 "int i;\n" 11776 "=======\n" 11777 ">>>>>>>")); 11778 11779 // FIXME: Handle parsing of macros around conflict markers correctly: 11780 EXPECT_EQ("#define Macro \\\n" 11781 "<<<<<<<\n" 11782 "Something \\\n" 11783 "|||||||\n" 11784 "Else \\\n" 11785 "=======\n" 11786 "Other \\\n" 11787 ">>>>>>>\n" 11788 " End int i;\n", 11789 format("#define Macro \\\n" 11790 "<<<<<<<\n" 11791 " Something \\\n" 11792 "|||||||\n" 11793 " Else \\\n" 11794 "=======\n" 11795 " Other \\\n" 11796 ">>>>>>>\n" 11797 " End\n" 11798 "int i;\n")); 11799 } 11800 11801 TEST_F(FormatTest, DisableRegions) { 11802 EXPECT_EQ("int i;\n" 11803 "// clang-format off\n" 11804 " int j;\n" 11805 "// clang-format on\n" 11806 "int k;", 11807 format(" int i;\n" 11808 " // clang-format off\n" 11809 " int j;\n" 11810 " // clang-format on\n" 11811 " int k;")); 11812 EXPECT_EQ("int i;\n" 11813 "/* clang-format off */\n" 11814 " int j;\n" 11815 "/* clang-format on */\n" 11816 "int k;", 11817 format(" int i;\n" 11818 " /* clang-format off */\n" 11819 " int j;\n" 11820 " /* clang-format on */\n" 11821 " int k;")); 11822 11823 // Don't reflow comments within disabled regions. 11824 EXPECT_EQ( 11825 "// clang-format off\n" 11826 "// long long long long long long line\n" 11827 "/* clang-format on */\n" 11828 "/* long long long\n" 11829 " * long long long\n" 11830 " * line */\n" 11831 "int i;\n" 11832 "/* clang-format off */\n" 11833 "/* long long long long long long line */\n", 11834 format("// clang-format off\n" 11835 "// long long long long long long line\n" 11836 "/* clang-format on */\n" 11837 "/* long long long long long long line */\n" 11838 "int i;\n" 11839 "/* clang-format off */\n" 11840 "/* long long long long long long line */\n", 11841 getLLVMStyleWithColumns(20))); 11842 } 11843 11844 TEST_F(FormatTest, DoNotCrashOnInvalidInput) { 11845 format("? ) ="); 11846 verifyNoCrash("#define a\\\n /**/}"); 11847 } 11848 11849 TEST_F(FormatTest, FormatsTableGenCode) { 11850 FormatStyle Style = getLLVMStyle(); 11851 Style.Language = FormatStyle::LK_TableGen; 11852 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style); 11853 } 11854 11855 TEST_F(FormatTest, ArrayOfTemplates) { 11856 EXPECT_EQ("auto a = new unique_ptr<int>[10];", 11857 format("auto a = new unique_ptr<int > [ 10];")); 11858 11859 FormatStyle Spaces = getLLVMStyle(); 11860 Spaces.SpacesInSquareBrackets = true; 11861 EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];", 11862 format("auto a = new unique_ptr<int > [10];", Spaces)); 11863 } 11864 11865 TEST_F(FormatTest, ArrayAsTemplateType) { 11866 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;", 11867 format("auto a = unique_ptr < Foo < Bar>[ 10]> ;")); 11868 11869 FormatStyle Spaces = getLLVMStyle(); 11870 Spaces.SpacesInSquareBrackets = true; 11871 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;", 11872 format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces)); 11873 } 11874 11875 TEST_F(FormatTest, NoSpaceAfterSuper) { 11876 verifyFormat("__super::FooBar();"); 11877 } 11878 11879 TEST(FormatStyle, GetStyleWithEmptyFileName) { 11880 vfs::InMemoryFileSystem FS; 11881 auto Style1 = getStyle("file", "", "Google", "", &FS); 11882 ASSERT_TRUE((bool)Style1); 11883 ASSERT_EQ(*Style1, getGoogleStyle()); 11884 } 11885 11886 TEST(FormatStyle, GetStyleOfFile) { 11887 vfs::InMemoryFileSystem FS; 11888 // Test 1: format file in the same directory. 11889 ASSERT_TRUE( 11890 FS.addFile("/a/.clang-format", 0, 11891 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM"))); 11892 ASSERT_TRUE( 11893 FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 11894 auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS); 11895 ASSERT_TRUE((bool)Style1); 11896 ASSERT_EQ(*Style1, getLLVMStyle()); 11897 11898 // Test 2.1: fallback to default. 11899 ASSERT_TRUE( 11900 FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 11901 auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS); 11902 ASSERT_TRUE((bool)Style2); 11903 ASSERT_EQ(*Style2, getMozillaStyle()); 11904 11905 // Test 2.2: no format on 'none' fallback style. 11906 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 11907 ASSERT_TRUE((bool)Style2); 11908 ASSERT_EQ(*Style2, getNoStyle()); 11909 11910 // Test 2.3: format if config is found with no based style while fallback is 11911 // 'none'. 11912 ASSERT_TRUE(FS.addFile("/b/.clang-format", 0, 11913 llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2"))); 11914 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 11915 ASSERT_TRUE((bool)Style2); 11916 ASSERT_EQ(*Style2, getLLVMStyle()); 11917 11918 // Test 2.4: format if yaml with no based style, while fallback is 'none'. 11919 Style2 = getStyle("{}", "a.h", "none", "", &FS); 11920 ASSERT_TRUE((bool)Style2); 11921 ASSERT_EQ(*Style2, getLLVMStyle()); 11922 11923 // Test 3: format file in parent directory. 11924 ASSERT_TRUE( 11925 FS.addFile("/c/.clang-format", 0, 11926 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 11927 ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0, 11928 llvm::MemoryBuffer::getMemBuffer("int i;"))); 11929 auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS); 11930 ASSERT_TRUE((bool)Style3); 11931 ASSERT_EQ(*Style3, getGoogleStyle()); 11932 11933 // Test 4: error on invalid fallback style 11934 auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS); 11935 ASSERT_FALSE((bool)Style4); 11936 llvm::consumeError(Style4.takeError()); 11937 11938 // Test 5: error on invalid yaml on command line 11939 auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS); 11940 ASSERT_FALSE((bool)Style5); 11941 llvm::consumeError(Style5.takeError()); 11942 11943 // Test 6: error on invalid style 11944 auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS); 11945 ASSERT_FALSE((bool)Style6); 11946 llvm::consumeError(Style6.takeError()); 11947 11948 // Test 7: found config file, error on parsing it 11949 ASSERT_TRUE( 11950 FS.addFile("/d/.clang-format", 0, 11951 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n" 11952 "InvalidKey: InvalidValue"))); 11953 ASSERT_TRUE( 11954 FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 11955 auto Style7 = getStyle("file", "/d/.clang-format", "LLVM", "", &FS); 11956 ASSERT_FALSE((bool)Style7); 11957 llvm::consumeError(Style7.takeError()); 11958 } 11959 11960 TEST_F(ReplacementTest, FormatCodeAfterReplacements) { 11961 // Column limit is 20. 11962 std::string Code = "Type *a =\n" 11963 " new Type();\n" 11964 "g(iiiii, 0, jjjjj,\n" 11965 " 0, kkkkk, 0, mm);\n" 11966 "int bad = format ;"; 11967 std::string Expected = "auto a = new Type();\n" 11968 "g(iiiii, nullptr,\n" 11969 " jjjjj, nullptr,\n" 11970 " kkkkk, nullptr,\n" 11971 " mm);\n" 11972 "int bad = format ;"; 11973 FileID ID = Context.createInMemoryFile("format.cpp", Code); 11974 tooling::Replacements Replaces = toReplacements( 11975 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6, 11976 "auto "), 11977 tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1, 11978 "nullptr"), 11979 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1, 11980 "nullptr"), 11981 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1, 11982 "nullptr")}); 11983 11984 format::FormatStyle Style = format::getLLVMStyle(); 11985 Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility. 11986 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 11987 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 11988 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 11989 auto Result = applyAllReplacements(Code, *FormattedReplaces); 11990 EXPECT_TRUE(static_cast<bool>(Result)); 11991 EXPECT_EQ(Expected, *Result); 11992 } 11993 11994 TEST_F(ReplacementTest, SortIncludesAfterReplacement) { 11995 std::string Code = "#include \"a.h\"\n" 11996 "#include \"c.h\"\n" 11997 "\n" 11998 "int main() {\n" 11999 " return 0;\n" 12000 "}"; 12001 std::string Expected = "#include \"a.h\"\n" 12002 "#include \"b.h\"\n" 12003 "#include \"c.h\"\n" 12004 "\n" 12005 "int main() {\n" 12006 " return 0;\n" 12007 "}"; 12008 FileID ID = Context.createInMemoryFile("fix.cpp", Code); 12009 tooling::Replacements Replaces = toReplacements( 12010 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0, 12011 "#include \"b.h\"\n")}); 12012 12013 format::FormatStyle Style = format::getLLVMStyle(); 12014 Style.SortIncludes = true; 12015 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 12016 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 12017 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 12018 auto Result = applyAllReplacements(Code, *FormattedReplaces); 12019 EXPECT_TRUE(static_cast<bool>(Result)); 12020 EXPECT_EQ(Expected, *Result); 12021 } 12022 12023 TEST_F(FormatTest, FormatSortsUsingDeclarations) { 12024 EXPECT_EQ("using std::cin;\n" 12025 "using std::cout;", 12026 format("using std::cout;\n" 12027 "using std::cin;", getGoogleStyle())); 12028 } 12029 12030 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) { 12031 format::FormatStyle Style = format::getLLVMStyle(); 12032 Style.Standard = FormatStyle::LS_Cpp03; 12033 // cpp03 recognize this string as identifier u8 and literal character 'a' 12034 EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style)); 12035 } 12036 12037 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) { 12038 // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers 12039 // all modes, including C++11, C++14 and C++17 12040 EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';")); 12041 } 12042 12043 TEST_F(FormatTest, DoNotFormatLikelyXml) { 12044 EXPECT_EQ("<!-- ;> -->", 12045 format("<!-- ;> -->", getGoogleStyle())); 12046 EXPECT_EQ(" <!-- >; -->", 12047 format(" <!-- >; -->", getGoogleStyle())); 12048 } 12049 12050 TEST_F(FormatTest, StructuredBindings) { 12051 // Structured bindings is a C++17 feature. 12052 // all modes, including C++11, C++14 and C++17 12053 verifyFormat("auto [a, b] = f();"); 12054 EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();")); 12055 EXPECT_EQ("const auto [a, b] = f();", format("const auto[a, b] = 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 [a, b, c] = f();", format("auto [ a , b,c ] = f();")); 12060 EXPECT_EQ("auto &[a, b, c] = f();", 12061 format("auto &[ a , b,c ] = f();")); 12062 EXPECT_EQ("auto &&[a, b, c] = f();", 12063 format("auto &&[ a , b,c ] = f();")); 12064 EXPECT_EQ("auto const &[a, b] = f();", format("auto const&[a, b] = f();")); 12065 EXPECT_EQ("auto const volatile &&[a, b] = f();", 12066 format("auto const volatile &&[a, b] = f();")); 12067 EXPECT_EQ("auto const &&[a, b] = f();", format("auto const && [a, b] = f();")); 12068 EXPECT_EQ("const auto &[a, b] = f();", format("const auto & [a, b] = f();")); 12069 EXPECT_EQ("const auto volatile &&[a, b] = f();", 12070 format("const auto volatile &&[a, b] = f();")); 12071 EXPECT_EQ("volatile const auto &&[a, b] = f();", 12072 format("volatile const auto &&[a, b] = f();")); 12073 EXPECT_EQ("const auto &&[a, b] = f();", format("const auto && [a, b] = f();")); 12074 12075 // Make sure we don't mistake structured bindings for lambdas. 12076 FormatStyle PointerMiddle = getLLVMStyle(); 12077 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 12078 verifyFormat("auto [a1, b]{A * i};", getGoogleStyle()); 12079 verifyFormat("auto [a2, b]{A * i};", getLLVMStyle()); 12080 verifyFormat("auto [a3, b]{A * i};", PointerMiddle); 12081 verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle()); 12082 verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle()); 12083 verifyFormat("auto const [a3, b]{A * i};", PointerMiddle); 12084 verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle()); 12085 verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle()); 12086 verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle); 12087 verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle()); 12088 verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle()); 12089 verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle); 12090 12091 EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}", 12092 format("for (const auto && [a, b] : some_range) {\n}")); 12093 EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}", 12094 format("for (const auto & [a, b] : some_range) {\n}")); 12095 EXPECT_EQ("for (const auto [a, b] : some_range) {\n}", 12096 format("for (const auto[a, b] : some_range) {\n}")); 12097 EXPECT_EQ("auto [x, y](expr);", format("auto[x,y] (expr);")); 12098 EXPECT_EQ("auto &[x, y](expr);", format("auto & [x,y] (expr);")); 12099 EXPECT_EQ("auto &&[x, y](expr);", format("auto && [x,y] (expr);")); 12100 EXPECT_EQ("auto const &[x, y](expr);", format("auto const & [x,y] (expr);")); 12101 EXPECT_EQ("auto const &&[x, y](expr);", format("auto const && [x,y] (expr);")); 12102 EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y] {expr};")); 12103 EXPECT_EQ("auto const &[x, y]{expr};", format("auto const & [x,y] {expr};")); 12104 EXPECT_EQ("auto const &&[x, y]{expr};", format("auto const && [x,y] {expr};")); 12105 12106 format::FormatStyle Spaces = format::getLLVMStyle(); 12107 Spaces.SpacesInSquareBrackets = true; 12108 verifyFormat("auto [ a, b ] = f();", Spaces); 12109 verifyFormat("auto &&[ a, b ] = f();", Spaces); 12110 verifyFormat("auto &[ a, b ] = f();", Spaces); 12111 verifyFormat("auto const &&[ a, b ] = f();", Spaces); 12112 verifyFormat("auto const &[ a, b ] = f();", Spaces); 12113 } 12114 12115 TEST_F(FormatTest, FileAndCode) { 12116 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", "")); 12117 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", "")); 12118 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", "")); 12119 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "")); 12120 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@interface Foo\n@end\n")); 12121 EXPECT_EQ( 12122 FormatStyle::LK_ObjC, 12123 guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }")); 12124 EXPECT_EQ(FormatStyle::LK_ObjC, 12125 guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))")); 12126 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;")); 12127 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", "")); 12128 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo", "@interface Foo\n@end\n")); 12129 EXPECT_EQ(FormatStyle::LK_ObjC, 12130 guessLanguage("foo.h", "int DoStuff(CGRect rect);\n")); 12131 EXPECT_EQ( 12132 FormatStyle::LK_ObjC, 12133 guessLanguage("foo.h", 12134 "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));\n")); 12135 EXPECT_EQ( 12136 FormatStyle::LK_Cpp, 12137 guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;")); 12138 } 12139 12140 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) { 12141 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];")); 12142 EXPECT_EQ(FormatStyle::LK_ObjC, 12143 guessLanguage("foo.h", "array[[calculator getIndex]];")); 12144 EXPECT_EQ(FormatStyle::LK_Cpp, 12145 guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];")); 12146 EXPECT_EQ( 12147 FormatStyle::LK_Cpp, 12148 guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];")); 12149 EXPECT_EQ(FormatStyle::LK_ObjC, 12150 guessLanguage("foo.h", "[[noreturn foo] bar];")); 12151 EXPECT_EQ(FormatStyle::LK_Cpp, 12152 guessLanguage("foo.h", "[[clang::fallthrough]];")); 12153 EXPECT_EQ(FormatStyle::LK_ObjC, 12154 guessLanguage("foo.h", "[[clang:fallthrough] foo];")); 12155 EXPECT_EQ(FormatStyle::LK_Cpp, 12156 guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];")); 12157 EXPECT_EQ(FormatStyle::LK_Cpp, 12158 guessLanguage("foo.h", "[[using clang: fallthrough]];")); 12159 EXPECT_EQ(FormatStyle::LK_ObjC, 12160 guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];")); 12161 EXPECT_EQ(FormatStyle::LK_Cpp, 12162 guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];")); 12163 EXPECT_EQ( 12164 FormatStyle::LK_Cpp, 12165 guessLanguage("foo.h", 12166 "[[clang::callable_when(\"unconsumed\", \"unknown\")]]")); 12167 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]")); 12168 } 12169 12170 TEST_F(FormatTest, GuessLanguageWithCaret) { 12171 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);")); 12172 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);")); 12173 EXPECT_EQ(FormatStyle::LK_ObjC, 12174 guessLanguage("foo.h", "int(^)(char, float);")); 12175 EXPECT_EQ(FormatStyle::LK_ObjC, 12176 guessLanguage("foo.h", "int(^foo)(char, float);")); 12177 EXPECT_EQ(FormatStyle::LK_ObjC, 12178 guessLanguage("foo.h", "int(^foo[10])(char, float);")); 12179 EXPECT_EQ(FormatStyle::LK_ObjC, 12180 guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);")); 12181 EXPECT_EQ( 12182 FormatStyle::LK_ObjC, 12183 guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);")); 12184 } 12185 12186 TEST_F(FormatTest, GuessLanguageWithChildLines) { 12187 EXPECT_EQ(FormatStyle::LK_Cpp, 12188 guessLanguage("foo.h", "#define FOO ({ std::string s; })")); 12189 EXPECT_EQ(FormatStyle::LK_ObjC, 12190 guessLanguage("foo.h", "#define FOO ({ NSString *s; })")); 12191 EXPECT_EQ( 12192 FormatStyle::LK_Cpp, 12193 guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })")); 12194 EXPECT_EQ( 12195 FormatStyle::LK_ObjC, 12196 guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })")); 12197 } 12198 12199 } // end namespace 12200 } // end namespace format 12201 } // end namespace clang 12202