1 //===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "clang/Format/Format.h" 11 12 #include "../Tooling/ReplacementTest.h" 13 #include "FormatTestUtils.h" 14 15 #include "clang/Frontend/TextDiagnosticPrinter.h" 16 #include "llvm/Support/Debug.h" 17 #include "llvm/Support/MemoryBuffer.h" 18 #include "gtest/gtest.h" 19 20 #define DEBUG_TYPE "format-test" 21 22 using clang::tooling::ReplacementTest; 23 using clang::tooling::toReplacements; 24 25 namespace clang { 26 namespace format { 27 namespace { 28 29 FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); } 30 31 class FormatTest : public ::testing::Test { 32 protected: 33 enum StatusCheck { 34 SC_ExpectComplete, 35 SC_ExpectIncomplete, 36 SC_DoNotCheck 37 }; 38 39 std::string format(llvm::StringRef Code, 40 const FormatStyle &Style = getLLVMStyle(), 41 StatusCheck CheckComplete = SC_ExpectComplete) { 42 DEBUG(llvm::errs() << "---\n"); 43 DEBUG(llvm::errs() << Code << "\n\n"); 44 std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size())); 45 FormattingAttemptStatus Status; 46 tooling::Replacements Replaces = 47 reformat(Style, Code, Ranges, "<stdin>", &Status); 48 if (CheckComplete != SC_DoNotCheck) { 49 bool ExpectedCompleteFormat = CheckComplete == SC_ExpectComplete; 50 EXPECT_EQ(ExpectedCompleteFormat, Status.FormatComplete) 51 << Code << "\n\n"; 52 } 53 ReplacementCount = Replaces.size(); 54 auto Result = applyAllReplacements(Code, Replaces); 55 EXPECT_TRUE(static_cast<bool>(Result)); 56 DEBUG(llvm::errs() << "\n" << *Result << "\n\n"); 57 return *Result; 58 } 59 60 FormatStyle getStyleWithColumns(FormatStyle Style, unsigned ColumnLimit) { 61 Style.ColumnLimit = ColumnLimit; 62 return Style; 63 } 64 65 FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) { 66 return getStyleWithColumns(getLLVMStyle(), ColumnLimit); 67 } 68 69 FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) { 70 return getStyleWithColumns(getGoogleStyle(), ColumnLimit); 71 } 72 73 void verifyFormat(llvm::StringRef Expected, llvm::StringRef Code, 74 const FormatStyle &Style = getLLVMStyle()) { 75 EXPECT_EQ(Expected.str(), format(Code, Style)); 76 if (Style.Language == FormatStyle::LK_Cpp) { 77 // Objective-C++ is a superset of C++, so everything checked for C++ 78 // needs to be checked for Objective-C++ as well. 79 FormatStyle ObjCStyle = Style; 80 ObjCStyle.Language = FormatStyle::LK_ObjC; 81 EXPECT_EQ(Expected.str(), format(test::messUp(Code), ObjCStyle)); 82 } 83 } 84 85 void verifyFormat(llvm::StringRef Code, 86 const FormatStyle &Style = getLLVMStyle()) { 87 verifyFormat(Code, test::messUp(Code), Style); 88 } 89 90 void verifyIncompleteFormat(llvm::StringRef Code, 91 const FormatStyle &Style = getLLVMStyle()) { 92 EXPECT_EQ(Code.str(), 93 format(test::messUp(Code), Style, SC_ExpectIncomplete)); 94 } 95 96 void verifyGoogleFormat(llvm::StringRef Code) { 97 verifyFormat(Code, getGoogleStyle()); 98 } 99 100 void verifyIndependentOfContext(llvm::StringRef text) { 101 verifyFormat(text); 102 verifyFormat(llvm::Twine("void f() { " + text + " }").str()); 103 } 104 105 /// \brief Verify that clang-format does not crash on the given input. 106 void verifyNoCrash(llvm::StringRef Code, 107 const FormatStyle &Style = getLLVMStyle()) { 108 format(Code, Style, SC_DoNotCheck); 109 } 110 111 int ReplacementCount; 112 }; 113 114 TEST_F(FormatTest, MessUp) { 115 EXPECT_EQ("1 2 3", test::messUp("1 2 3")); 116 EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n")); 117 EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc")); 118 EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc")); 119 EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne")); 120 } 121 122 //===----------------------------------------------------------------------===// 123 // Basic function tests. 124 //===----------------------------------------------------------------------===// 125 126 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) { 127 EXPECT_EQ(";", format(";")); 128 } 129 130 TEST_F(FormatTest, FormatsGlobalStatementsAt0) { 131 EXPECT_EQ("int i;", format(" int i;")); 132 EXPECT_EQ("\nint i;", format(" \n\t \v \f int i;")); 133 EXPECT_EQ("int i;\nint j;", format(" int i; int j;")); 134 EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;")); 135 } 136 137 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) { 138 EXPECT_EQ("int i;", format("int\ni;")); 139 } 140 141 TEST_F(FormatTest, FormatsNestedBlockStatements) { 142 EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}")); 143 } 144 145 TEST_F(FormatTest, FormatsNestedCall) { 146 verifyFormat("Method(f1, f2(f3));"); 147 verifyFormat("Method(f1(f2, f3()));"); 148 verifyFormat("Method(f1(f2, (f3())));"); 149 } 150 151 TEST_F(FormatTest, NestedNameSpecifiers) { 152 verifyFormat("vector<::Type> v;"); 153 verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())"); 154 verifyFormat("static constexpr bool Bar = decltype(bar())::value;"); 155 verifyFormat("bool a = 2 < ::SomeFunction();"); 156 verifyFormat("ALWAYS_INLINE ::std::string getName();"); 157 verifyFormat("some::string getName();"); 158 } 159 160 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) { 161 EXPECT_EQ("if (a) {\n" 162 " f();\n" 163 "}", 164 format("if(a){f();}")); 165 EXPECT_EQ(4, ReplacementCount); 166 EXPECT_EQ("if (a) {\n" 167 " f();\n" 168 "}", 169 format("if (a) {\n" 170 " f();\n" 171 "}")); 172 EXPECT_EQ(0, ReplacementCount); 173 EXPECT_EQ("/*\r\n" 174 "\r\n" 175 "*/\r\n", 176 format("/*\r\n" 177 "\r\n" 178 "*/\r\n")); 179 EXPECT_EQ(0, ReplacementCount); 180 } 181 182 TEST_F(FormatTest, RemovesEmptyLines) { 183 EXPECT_EQ("class C {\n" 184 " int i;\n" 185 "};", 186 format("class C {\n" 187 " int i;\n" 188 "\n" 189 "};")); 190 191 // Don't remove empty lines at the start of namespaces or extern "C" blocks. 192 EXPECT_EQ("namespace N {\n" 193 "\n" 194 "int i;\n" 195 "}", 196 format("namespace N {\n" 197 "\n" 198 "int i;\n" 199 "}", 200 getGoogleStyle())); 201 EXPECT_EQ("extern /**/ \"C\" /**/ {\n" 202 "\n" 203 "int i;\n" 204 "}", 205 format("extern /**/ \"C\" /**/ {\n" 206 "\n" 207 "int i;\n" 208 "}", 209 getGoogleStyle())); 210 211 // ...but do keep inlining and removing empty lines for non-block extern "C" 212 // functions. 213 verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle()); 214 EXPECT_EQ("extern \"C\" int f() {\n" 215 " int i = 42;\n" 216 " return i;\n" 217 "}", 218 format("extern \"C\" int f() {\n" 219 "\n" 220 " int i = 42;\n" 221 " return i;\n" 222 "}", 223 getGoogleStyle())); 224 225 // Remove empty lines at the beginning and end of blocks. 226 EXPECT_EQ("void f() {\n" 227 "\n" 228 " if (a) {\n" 229 "\n" 230 " f();\n" 231 " }\n" 232 "}", 233 format("void f() {\n" 234 "\n" 235 " if (a) {\n" 236 "\n" 237 " f();\n" 238 "\n" 239 " }\n" 240 "\n" 241 "}", 242 getLLVMStyle())); 243 EXPECT_EQ("void f() {\n" 244 " if (a) {\n" 245 " f();\n" 246 " }\n" 247 "}", 248 format("void f() {\n" 249 "\n" 250 " if (a) {\n" 251 "\n" 252 " f();\n" 253 "\n" 254 " }\n" 255 "\n" 256 "}", 257 getGoogleStyle())); 258 259 // Don't remove empty lines in more complex control statements. 260 EXPECT_EQ("void f() {\n" 261 " if (a) {\n" 262 " f();\n" 263 "\n" 264 " } else if (b) {\n" 265 " f();\n" 266 " }\n" 267 "}", 268 format("void f() {\n" 269 " if (a) {\n" 270 " f();\n" 271 "\n" 272 " } else if (b) {\n" 273 " f();\n" 274 "\n" 275 " }\n" 276 "\n" 277 "}")); 278 279 // FIXME: This is slightly inconsistent. 280 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 281 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 282 EXPECT_EQ("namespace {\n" 283 "int i;\n" 284 "}", 285 format("namespace {\n" 286 "int i;\n" 287 "\n" 288 "}", LLVMWithNoNamespaceFix)); 289 EXPECT_EQ("namespace {\n" 290 "int i;\n" 291 "}", 292 format("namespace {\n" 293 "int i;\n" 294 "\n" 295 "}")); 296 EXPECT_EQ("namespace {\n" 297 "int i;\n" 298 "\n" 299 "} // namespace", 300 format("namespace {\n" 301 "int i;\n" 302 "\n" 303 "} // namespace")); 304 305 FormatStyle Style = getLLVMStyle(); 306 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 307 Style.MaxEmptyLinesToKeep = 2; 308 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 309 Style.BraceWrapping.AfterClass = true; 310 Style.BraceWrapping.AfterFunction = true; 311 Style.KeepEmptyLinesAtTheStartOfBlocks = false; 312 313 EXPECT_EQ("class Foo\n" 314 "{\n" 315 " Foo() {}\n" 316 "\n" 317 " void funk() {}\n" 318 "};", 319 format("class Foo\n" 320 "{\n" 321 " Foo()\n" 322 " {\n" 323 " }\n" 324 "\n" 325 " void funk() {}\n" 326 "};", 327 Style)); 328 } 329 330 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) { 331 verifyFormat("x = (a) and (b);"); 332 verifyFormat("x = (a) or (b);"); 333 verifyFormat("x = (a) bitand (b);"); 334 verifyFormat("x = (a) bitor (b);"); 335 verifyFormat("x = (a) not_eq (b);"); 336 verifyFormat("x = (a) and_eq (b);"); 337 verifyFormat("x = (a) or_eq (b);"); 338 verifyFormat("x = (a) xor (b);"); 339 } 340 341 TEST_F(FormatTest, RecognizesUnaryOperatorKeywords) { 342 verifyFormat("x = compl(a);"); 343 verifyFormat("x = not(a);"); 344 verifyFormat("x = bitand(a);"); 345 // Unary operator must not be merged with the next identifier 346 verifyFormat("x = compl a;"); 347 verifyFormat("x = not a;"); 348 verifyFormat("x = bitand a;"); 349 } 350 351 //===----------------------------------------------------------------------===// 352 // Tests for control statements. 353 //===----------------------------------------------------------------------===// 354 355 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) { 356 verifyFormat("if (true)\n f();\ng();"); 357 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); 358 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); 359 verifyFormat("if constexpr (true)\n" 360 " f();\ng();"); 361 verifyFormat("if constexpr (a)\n" 362 " if constexpr (b)\n" 363 " if constexpr (c)\n" 364 " g();\n" 365 "h();"); 366 verifyFormat("if constexpr (a)\n" 367 " if constexpr (b) {\n" 368 " f();\n" 369 " }\n" 370 "g();"); 371 372 FormatStyle AllowsMergedIf = getLLVMStyle(); 373 AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left; 374 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 375 verifyFormat("if (a)\n" 376 " // comment\n" 377 " f();", 378 AllowsMergedIf); 379 verifyFormat("{\n" 380 " if (a)\n" 381 " label:\n" 382 " f();\n" 383 "}", 384 AllowsMergedIf); 385 verifyFormat("#define A \\\n" 386 " if (a) \\\n" 387 " label: \\\n" 388 " f()", 389 AllowsMergedIf); 390 verifyFormat("if (a)\n" 391 " ;", 392 AllowsMergedIf); 393 verifyFormat("if (a)\n" 394 " if (b) return;", 395 AllowsMergedIf); 396 397 verifyFormat("if (a) // Can't merge this\n" 398 " f();\n", 399 AllowsMergedIf); 400 verifyFormat("if (a) /* still don't merge */\n" 401 " f();", 402 AllowsMergedIf); 403 verifyFormat("if (a) { // Never merge this\n" 404 " f();\n" 405 "}", 406 AllowsMergedIf); 407 verifyFormat("if (a) { /* Never merge this */\n" 408 " f();\n" 409 "}", 410 AllowsMergedIf); 411 412 AllowsMergedIf.ColumnLimit = 14; 413 verifyFormat("if (a) return;", AllowsMergedIf); 414 verifyFormat("if (aaaaaaaaa)\n" 415 " return;", 416 AllowsMergedIf); 417 418 AllowsMergedIf.ColumnLimit = 13; 419 verifyFormat("if (a)\n return;", AllowsMergedIf); 420 } 421 422 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) { 423 FormatStyle AllowsMergedLoops = getLLVMStyle(); 424 AllowsMergedLoops.AllowShortLoopsOnASingleLine = true; 425 verifyFormat("while (true) continue;", AllowsMergedLoops); 426 verifyFormat("for (;;) continue;", AllowsMergedLoops); 427 verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops); 428 verifyFormat("while (true)\n" 429 " ;", 430 AllowsMergedLoops); 431 verifyFormat("for (;;)\n" 432 " ;", 433 AllowsMergedLoops); 434 verifyFormat("for (;;)\n" 435 " for (;;) continue;", 436 AllowsMergedLoops); 437 verifyFormat("for (;;) // Can't merge this\n" 438 " continue;", 439 AllowsMergedLoops); 440 verifyFormat("for (;;) /* still don't merge */\n" 441 " continue;", 442 AllowsMergedLoops); 443 } 444 445 TEST_F(FormatTest, FormatShortBracedStatements) { 446 FormatStyle AllowSimpleBracedStatements = getLLVMStyle(); 447 AllowSimpleBracedStatements.ColumnLimit = 40; 448 AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true; 449 450 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true; 451 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 452 453 AllowSimpleBracedStatements.BreakBeforeBraces = FormatStyle::BS_Custom; 454 AllowSimpleBracedStatements.BraceWrapping.AfterFunction = true; 455 AllowSimpleBracedStatements.BraceWrapping.SplitEmptyRecord = false; 456 457 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 458 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements); 459 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 460 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 461 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 462 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements); 463 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 464 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 465 verifyFormat("if (true) {\n" 466 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 467 "}", 468 AllowSimpleBracedStatements); 469 verifyFormat("if (true) { //\n" 470 " f();\n" 471 "}", 472 AllowSimpleBracedStatements); 473 verifyFormat("if (true) {\n" 474 " f();\n" 475 " f();\n" 476 "}", 477 AllowSimpleBracedStatements); 478 verifyFormat("if (true) {\n" 479 " f();\n" 480 "} else {\n" 481 " f();\n" 482 "}", 483 AllowSimpleBracedStatements); 484 485 verifyFormat("struct A2 {\n" 486 " int X;\n" 487 "};", 488 AllowSimpleBracedStatements); 489 verifyFormat("typedef struct A2 {\n" 490 " int X;\n" 491 "} A2_t;", 492 AllowSimpleBracedStatements); 493 verifyFormat("template <int> struct A2 {\n" 494 " struct B {};\n" 495 "};", 496 AllowSimpleBracedStatements); 497 498 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false; 499 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 500 verifyFormat("if (true) {\n" 501 " f();\n" 502 "}", 503 AllowSimpleBracedStatements); 504 verifyFormat("if (true) {\n" 505 " f();\n" 506 "} else {\n" 507 " f();\n" 508 "}", 509 AllowSimpleBracedStatements); 510 511 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 512 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 513 verifyFormat("while (true) {\n" 514 " f();\n" 515 "}", 516 AllowSimpleBracedStatements); 517 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 518 verifyFormat("for (;;) {\n" 519 " f();\n" 520 "}", 521 AllowSimpleBracedStatements); 522 523 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true; 524 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 525 AllowSimpleBracedStatements.BraceWrapping.AfterControlStatement = true; 526 527 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 528 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements); 529 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 530 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 531 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 532 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements); 533 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 534 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 535 verifyFormat("if (true)\n" 536 "{\n" 537 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 538 "}", 539 AllowSimpleBracedStatements); 540 verifyFormat("if (true)\n" 541 "{ //\n" 542 " f();\n" 543 "}", 544 AllowSimpleBracedStatements); 545 verifyFormat("if (true)\n" 546 "{\n" 547 " f();\n" 548 " f();\n" 549 "}", 550 AllowSimpleBracedStatements); 551 verifyFormat("if (true)\n" 552 "{\n" 553 " f();\n" 554 "} else\n" 555 "{\n" 556 " f();\n" 557 "}", 558 AllowSimpleBracedStatements); 559 560 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false; 561 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 562 verifyFormat("if (true)\n" 563 "{\n" 564 " f();\n" 565 "}", 566 AllowSimpleBracedStatements); 567 verifyFormat("if (true)\n" 568 "{\n" 569 " f();\n" 570 "} else\n" 571 "{\n" 572 " f();\n" 573 "}", 574 AllowSimpleBracedStatements); 575 576 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 577 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 578 verifyFormat("while (true)\n" 579 "{\n" 580 " f();\n" 581 "}", 582 AllowSimpleBracedStatements); 583 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 584 verifyFormat("for (;;)\n" 585 "{\n" 586 " f();\n" 587 "}", 588 AllowSimpleBracedStatements); 589 } 590 591 TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) { 592 FormatStyle Style = getLLVMStyleWithColumns(60); 593 Style.AllowShortBlocksOnASingleLine = true; 594 Style.AllowShortIfStatementsOnASingleLine = true; 595 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 596 EXPECT_EQ("#define A \\\n" 597 " if (HANDLEwernufrnuLwrmviferuvnierv) \\\n" 598 " { RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; }\n" 599 "X;", 600 format("#define A \\\n" 601 " if (HANDLEwernufrnuLwrmviferuvnierv) { \\\n" 602 " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n" 603 " }\n" 604 "X;", 605 Style)); 606 } 607 608 TEST_F(FormatTest, ParseIfElse) { 609 verifyFormat("if (true)\n" 610 " if (true)\n" 611 " if (true)\n" 612 " f();\n" 613 " else\n" 614 " g();\n" 615 " else\n" 616 " h();\n" 617 "else\n" 618 " i();"); 619 verifyFormat("if (true)\n" 620 " if (true)\n" 621 " if (true) {\n" 622 " if (true)\n" 623 " f();\n" 624 " } else {\n" 625 " g();\n" 626 " }\n" 627 " else\n" 628 " h();\n" 629 "else {\n" 630 " i();\n" 631 "}"); 632 verifyFormat("if (true)\n" 633 " if constexpr (true)\n" 634 " if (true) {\n" 635 " if constexpr (true)\n" 636 " f();\n" 637 " } else {\n" 638 " g();\n" 639 " }\n" 640 " else\n" 641 " h();\n" 642 "else {\n" 643 " i();\n" 644 "}"); 645 verifyFormat("void f() {\n" 646 " if (a) {\n" 647 " } else {\n" 648 " }\n" 649 "}"); 650 } 651 652 TEST_F(FormatTest, ElseIf) { 653 verifyFormat("if (a) {\n} else if (b) {\n}"); 654 verifyFormat("if (a)\n" 655 " f();\n" 656 "else if (b)\n" 657 " g();\n" 658 "else\n" 659 " h();"); 660 verifyFormat("if constexpr (a)\n" 661 " f();\n" 662 "else if constexpr (b)\n" 663 " g();\n" 664 "else\n" 665 " h();"); 666 verifyFormat("if (a) {\n" 667 " f();\n" 668 "}\n" 669 "// or else ..\n" 670 "else {\n" 671 " g()\n" 672 "}"); 673 674 verifyFormat("if (a) {\n" 675 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 676 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 677 "}"); 678 verifyFormat("if (a) {\n" 679 "} else if (\n" 680 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 681 "}", 682 getLLVMStyleWithColumns(62)); 683 verifyFormat("if (a) {\n" 684 "} else if constexpr (\n" 685 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 686 "}", 687 getLLVMStyleWithColumns(62)); 688 } 689 690 TEST_F(FormatTest, FormatsForLoop) { 691 verifyFormat( 692 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" 693 " ++VeryVeryLongLoopVariable)\n" 694 " ;"); 695 verifyFormat("for (;;)\n" 696 " f();"); 697 verifyFormat("for (;;) {\n}"); 698 verifyFormat("for (;;) {\n" 699 " f();\n" 700 "}"); 701 verifyFormat("for (int i = 0; (i < 10); ++i) {\n}"); 702 703 verifyFormat( 704 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 705 " E = UnwrappedLines.end();\n" 706 " I != E; ++I) {\n}"); 707 708 verifyFormat( 709 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" 710 " ++IIIII) {\n}"); 711 verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n" 712 " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n" 713 " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}"); 714 verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n" 715 " I = FD->getDeclsInPrototypeScope().begin(),\n" 716 " E = FD->getDeclsInPrototypeScope().end();\n" 717 " I != E; ++I) {\n}"); 718 verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n" 719 " I = Container.begin(),\n" 720 " E = Container.end();\n" 721 " I != E; ++I) {\n}", 722 getLLVMStyleWithColumns(76)); 723 724 verifyFormat( 725 "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 726 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n" 727 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 728 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 729 " ++aaaaaaaaaaa) {\n}"); 730 verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 731 " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n" 732 " ++i) {\n}"); 733 verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n" 734 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 735 "}"); 736 verifyFormat("for (some_namespace::SomeIterator iter( // force break\n" 737 " aaaaaaaaaa);\n" 738 " iter; ++iter) {\n" 739 "}"); 740 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 741 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 742 " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n" 743 " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {"); 744 745 FormatStyle NoBinPacking = getLLVMStyle(); 746 NoBinPacking.BinPackParameters = false; 747 verifyFormat("for (int aaaaaaaaaaa = 1;\n" 748 " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n" 749 " aaaaaaaaaaaaaaaa,\n" 750 " aaaaaaaaaaaaaaaa,\n" 751 " aaaaaaaaaaaaaaaa);\n" 752 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 753 "}", 754 NoBinPacking); 755 verifyFormat( 756 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 757 " E = UnwrappedLines.end();\n" 758 " I != E;\n" 759 " ++I) {\n}", 760 NoBinPacking); 761 762 FormatStyle AlignLeft = getLLVMStyle(); 763 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 764 verifyFormat("for (A* a = start; a < end; ++a, ++value) {\n}", AlignLeft); 765 } 766 767 TEST_F(FormatTest, RangeBasedForLoops) { 768 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 769 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 770 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n" 771 " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}"); 772 verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n" 773 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 774 verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n" 775 " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}"); 776 } 777 778 TEST_F(FormatTest, ForEachLoops) { 779 verifyFormat("void f() {\n" 780 " foreach (Item *item, itemlist) {}\n" 781 " Q_FOREACH (Item *item, itemlist) {}\n" 782 " BOOST_FOREACH (Item *item, itemlist) {}\n" 783 " UNKNOWN_FORACH(Item * item, itemlist) {}\n" 784 "}"); 785 786 // As function-like macros. 787 verifyFormat("#define foreach(x, y)\n" 788 "#define Q_FOREACH(x, y)\n" 789 "#define BOOST_FOREACH(x, y)\n" 790 "#define UNKNOWN_FOREACH(x, y)\n"); 791 792 // Not as function-like macros. 793 verifyFormat("#define foreach (x, y)\n" 794 "#define Q_FOREACH (x, y)\n" 795 "#define BOOST_FOREACH (x, y)\n" 796 "#define UNKNOWN_FOREACH (x, y)\n"); 797 } 798 799 TEST_F(FormatTest, FormatsWhileLoop) { 800 verifyFormat("while (true) {\n}"); 801 verifyFormat("while (true)\n" 802 " f();"); 803 verifyFormat("while () {\n}"); 804 verifyFormat("while () {\n" 805 " f();\n" 806 "}"); 807 } 808 809 TEST_F(FormatTest, FormatsDoWhile) { 810 verifyFormat("do {\n" 811 " do_something();\n" 812 "} while (something());"); 813 verifyFormat("do\n" 814 " do_something();\n" 815 "while (something());"); 816 } 817 818 TEST_F(FormatTest, FormatsSwitchStatement) { 819 verifyFormat("switch (x) {\n" 820 "case 1:\n" 821 " f();\n" 822 " break;\n" 823 "case kFoo:\n" 824 "case ns::kBar:\n" 825 "case kBaz:\n" 826 " break;\n" 827 "default:\n" 828 " g();\n" 829 " break;\n" 830 "}"); 831 verifyFormat("switch (x) {\n" 832 "case 1: {\n" 833 " f();\n" 834 " break;\n" 835 "}\n" 836 "case 2: {\n" 837 " break;\n" 838 "}\n" 839 "}"); 840 verifyFormat("switch (x) {\n" 841 "case 1: {\n" 842 " f();\n" 843 " {\n" 844 " g();\n" 845 " h();\n" 846 " }\n" 847 " break;\n" 848 "}\n" 849 "}"); 850 verifyFormat("switch (x) {\n" 851 "case 1: {\n" 852 " f();\n" 853 " if (foo) {\n" 854 " g();\n" 855 " h();\n" 856 " }\n" 857 " break;\n" 858 "}\n" 859 "}"); 860 verifyFormat("switch (x) {\n" 861 "case 1: {\n" 862 " f();\n" 863 " g();\n" 864 "} break;\n" 865 "}"); 866 verifyFormat("switch (test)\n" 867 " ;"); 868 verifyFormat("switch (x) {\n" 869 "default: {\n" 870 " // Do nothing.\n" 871 "}\n" 872 "}"); 873 verifyFormat("switch (x) {\n" 874 "// comment\n" 875 "// if 1, do f()\n" 876 "case 1:\n" 877 " f();\n" 878 "}"); 879 verifyFormat("switch (x) {\n" 880 "case 1:\n" 881 " // Do amazing stuff\n" 882 " {\n" 883 " f();\n" 884 " g();\n" 885 " }\n" 886 " break;\n" 887 "}"); 888 verifyFormat("#define A \\\n" 889 " switch (x) { \\\n" 890 " case a: \\\n" 891 " foo = b; \\\n" 892 " }", 893 getLLVMStyleWithColumns(20)); 894 verifyFormat("#define OPERATION_CASE(name) \\\n" 895 " case OP_name: \\\n" 896 " return operations::Operation##name\n", 897 getLLVMStyleWithColumns(40)); 898 verifyFormat("switch (x) {\n" 899 "case 1:;\n" 900 "default:;\n" 901 " int i;\n" 902 "}"); 903 904 verifyGoogleFormat("switch (x) {\n" 905 " case 1:\n" 906 " f();\n" 907 " break;\n" 908 " case kFoo:\n" 909 " case ns::kBar:\n" 910 " case kBaz:\n" 911 " break;\n" 912 " default:\n" 913 " g();\n" 914 " break;\n" 915 "}"); 916 verifyGoogleFormat("switch (x) {\n" 917 " case 1: {\n" 918 " f();\n" 919 " break;\n" 920 " }\n" 921 "}"); 922 verifyGoogleFormat("switch (test)\n" 923 " ;"); 924 925 verifyGoogleFormat("#define OPERATION_CASE(name) \\\n" 926 " case OP_name: \\\n" 927 " return operations::Operation##name\n"); 928 verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n" 929 " // Get the correction operation class.\n" 930 " switch (OpCode) {\n" 931 " CASE(Add);\n" 932 " CASE(Subtract);\n" 933 " default:\n" 934 " return operations::Unknown;\n" 935 " }\n" 936 "#undef OPERATION_CASE\n" 937 "}"); 938 verifyFormat("DEBUG({\n" 939 " switch (x) {\n" 940 " case A:\n" 941 " f();\n" 942 " break;\n" 943 " // fallthrough\n" 944 " case B:\n" 945 " g();\n" 946 " break;\n" 947 " }\n" 948 "});"); 949 EXPECT_EQ("DEBUG({\n" 950 " switch (x) {\n" 951 " case A:\n" 952 " f();\n" 953 " break;\n" 954 " // On B:\n" 955 " case B:\n" 956 " g();\n" 957 " break;\n" 958 " }\n" 959 "});", 960 format("DEBUG({\n" 961 " switch (x) {\n" 962 " case A:\n" 963 " f();\n" 964 " break;\n" 965 " // On B:\n" 966 " case B:\n" 967 " g();\n" 968 " break;\n" 969 " }\n" 970 "});", 971 getLLVMStyle())); 972 verifyFormat("switch (a) {\n" 973 "case (b):\n" 974 " return;\n" 975 "}"); 976 977 verifyFormat("switch (a) {\n" 978 "case some_namespace::\n" 979 " some_constant:\n" 980 " return;\n" 981 "}", 982 getLLVMStyleWithColumns(34)); 983 } 984 985 TEST_F(FormatTest, CaseRanges) { 986 verifyFormat("switch (x) {\n" 987 "case 'A' ... 'Z':\n" 988 "case 1 ... 5:\n" 989 "case a ... b:\n" 990 " break;\n" 991 "}"); 992 } 993 994 TEST_F(FormatTest, ShortCaseLabels) { 995 FormatStyle Style = getLLVMStyle(); 996 Style.AllowShortCaseLabelsOnASingleLine = true; 997 verifyFormat("switch (a) {\n" 998 "case 1: x = 1; break;\n" 999 "case 2: return;\n" 1000 "case 3:\n" 1001 "case 4:\n" 1002 "case 5: return;\n" 1003 "case 6: // comment\n" 1004 " return;\n" 1005 "case 7:\n" 1006 " // comment\n" 1007 " return;\n" 1008 "case 8:\n" 1009 " x = 8; // comment\n" 1010 " break;\n" 1011 "default: y = 1; break;\n" 1012 "}", 1013 Style); 1014 verifyFormat("switch (a) {\n" 1015 "case 0: return; // comment\n" 1016 "case 1: break; // comment\n" 1017 "case 2: return;\n" 1018 "// comment\n" 1019 "case 3: return;\n" 1020 "// comment 1\n" 1021 "// comment 2\n" 1022 "// comment 3\n" 1023 "case 4: break; /* comment */\n" 1024 "case 5:\n" 1025 " // comment\n" 1026 " break;\n" 1027 "case 6: /* comment */ x = 1; break;\n" 1028 "case 7: x = /* comment */ 1; break;\n" 1029 "case 8:\n" 1030 " x = 1; /* comment */\n" 1031 " break;\n" 1032 "case 9:\n" 1033 " break; // comment line 1\n" 1034 " // comment line 2\n" 1035 "}", 1036 Style); 1037 EXPECT_EQ("switch (a) {\n" 1038 "case 1:\n" 1039 " x = 8;\n" 1040 " // fall through\n" 1041 "case 2: x = 8;\n" 1042 "// comment\n" 1043 "case 3:\n" 1044 " return; /* comment line 1\n" 1045 " * comment line 2 */\n" 1046 "case 4: i = 8;\n" 1047 "// something else\n" 1048 "#if FOO\n" 1049 "case 5: break;\n" 1050 "#endif\n" 1051 "}", 1052 format("switch (a) {\n" 1053 "case 1: x = 8;\n" 1054 " // fall through\n" 1055 "case 2:\n" 1056 " x = 8;\n" 1057 "// comment\n" 1058 "case 3:\n" 1059 " return; /* comment line 1\n" 1060 " * comment line 2 */\n" 1061 "case 4:\n" 1062 " i = 8;\n" 1063 "// something else\n" 1064 "#if FOO\n" 1065 "case 5: break;\n" 1066 "#endif\n" 1067 "}", 1068 Style)); 1069 EXPECT_EQ("switch (a) {\n" "case 0:\n" 1070 " return; // long long long long long long long long long long long long comment\n" 1071 " // line\n" "}", 1072 format("switch (a) {\n" 1073 "case 0: return; // long long long long long long long long long long long long comment line\n" 1074 "}", 1075 Style)); 1076 EXPECT_EQ("switch (a) {\n" 1077 "case 0:\n" 1078 " return; /* long long long long long long long long long long long long comment\n" 1079 " line */\n" 1080 "}", 1081 format("switch (a) {\n" 1082 "case 0: return; /* long long long long long long long long long long long long comment line */\n" 1083 "}", 1084 Style)); 1085 verifyFormat("switch (a) {\n" 1086 "#if FOO\n" 1087 "case 0: return 0;\n" 1088 "#endif\n" 1089 "}", 1090 Style); 1091 verifyFormat("switch (a) {\n" 1092 "case 1: {\n" 1093 "}\n" 1094 "case 2: {\n" 1095 " return;\n" 1096 "}\n" 1097 "case 3: {\n" 1098 " x = 1;\n" 1099 " return;\n" 1100 "}\n" 1101 "case 4:\n" 1102 " if (x)\n" 1103 " return;\n" 1104 "}", 1105 Style); 1106 Style.ColumnLimit = 21; 1107 verifyFormat("switch (a) {\n" 1108 "case 1: x = 1; break;\n" 1109 "case 2: return;\n" 1110 "case 3:\n" 1111 "case 4:\n" 1112 "case 5: return;\n" 1113 "default:\n" 1114 " y = 1;\n" 1115 " break;\n" 1116 "}", 1117 Style); 1118 } 1119 1120 TEST_F(FormatTest, FormatsLabels) { 1121 verifyFormat("void f() {\n" 1122 " some_code();\n" 1123 "test_label:\n" 1124 " some_other_code();\n" 1125 " {\n" 1126 " some_more_code();\n" 1127 " another_label:\n" 1128 " some_more_code();\n" 1129 " }\n" 1130 "}"); 1131 verifyFormat("{\n" 1132 " some_code();\n" 1133 "test_label:\n" 1134 " some_other_code();\n" 1135 "}"); 1136 verifyFormat("{\n" 1137 " some_code();\n" 1138 "test_label:;\n" 1139 " int i = 0;\n" 1140 "}"); 1141 } 1142 1143 //===----------------------------------------------------------------------===// 1144 // Tests for classes, namespaces, etc. 1145 //===----------------------------------------------------------------------===// 1146 1147 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { 1148 verifyFormat("class A {};"); 1149 } 1150 1151 TEST_F(FormatTest, UnderstandsAccessSpecifiers) { 1152 verifyFormat("class A {\n" 1153 "public:\n" 1154 "public: // comment\n" 1155 "protected:\n" 1156 "private:\n" 1157 " void f() {}\n" 1158 "};"); 1159 verifyGoogleFormat("class A {\n" 1160 " public:\n" 1161 " protected:\n" 1162 " private:\n" 1163 " void f() {}\n" 1164 "};"); 1165 verifyFormat("class A {\n" 1166 "public slots:\n" 1167 " void f1() {}\n" 1168 "public Q_SLOTS:\n" 1169 " void f2() {}\n" 1170 "protected slots:\n" 1171 " void f3() {}\n" 1172 "protected Q_SLOTS:\n" 1173 " void f4() {}\n" 1174 "private slots:\n" 1175 " void f5() {}\n" 1176 "private Q_SLOTS:\n" 1177 " void f6() {}\n" 1178 "signals:\n" 1179 " void g1();\n" 1180 "Q_SIGNALS:\n" 1181 " void g2();\n" 1182 "};"); 1183 1184 // Don't interpret 'signals' the wrong way. 1185 verifyFormat("signals.set();"); 1186 verifyFormat("for (Signals signals : f()) {\n}"); 1187 verifyFormat("{\n" 1188 " signals.set(); // This needs indentation.\n" 1189 "}"); 1190 verifyFormat("void f() {\n" 1191 "label:\n" 1192 " signals.baz();\n" 1193 "}"); 1194 } 1195 1196 TEST_F(FormatTest, SeparatesLogicalBlocks) { 1197 EXPECT_EQ("class A {\n" 1198 "public:\n" 1199 " void f();\n" 1200 "\n" 1201 "private:\n" 1202 " void g() {}\n" 1203 " // test\n" 1204 "protected:\n" 1205 " int h;\n" 1206 "};", 1207 format("class A {\n" 1208 "public:\n" 1209 "void f();\n" 1210 "private:\n" 1211 "void g() {}\n" 1212 "// test\n" 1213 "protected:\n" 1214 "int h;\n" 1215 "};")); 1216 EXPECT_EQ("class A {\n" 1217 "protected:\n" 1218 "public:\n" 1219 " void f();\n" 1220 "};", 1221 format("class A {\n" 1222 "protected:\n" 1223 "\n" 1224 "public:\n" 1225 "\n" 1226 " void f();\n" 1227 "};")); 1228 1229 // Even ensure proper spacing inside macros. 1230 EXPECT_EQ("#define B \\\n" 1231 " class A { \\\n" 1232 " protected: \\\n" 1233 " public: \\\n" 1234 " void f(); \\\n" 1235 " };", 1236 format("#define B \\\n" 1237 " class A { \\\n" 1238 " protected: \\\n" 1239 " \\\n" 1240 " public: \\\n" 1241 " \\\n" 1242 " void f(); \\\n" 1243 " };", 1244 getGoogleStyle())); 1245 // But don't remove empty lines after macros ending in access specifiers. 1246 EXPECT_EQ("#define A private:\n" 1247 "\n" 1248 "int i;", 1249 format("#define A private:\n" 1250 "\n" 1251 "int i;")); 1252 } 1253 1254 TEST_F(FormatTest, FormatsClasses) { 1255 verifyFormat("class A : public B {};"); 1256 verifyFormat("class A : public ::B {};"); 1257 1258 verifyFormat( 1259 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 1260 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 1261 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 1262 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 1263 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 1264 verifyFormat( 1265 "class A : public B, public C, public D, public E, public F {};"); 1266 verifyFormat("class AAAAAAAAAAAA : public B,\n" 1267 " public C,\n" 1268 " public D,\n" 1269 " public E,\n" 1270 " public F,\n" 1271 " public G {};"); 1272 1273 verifyFormat("class\n" 1274 " ReallyReallyLongClassName {\n" 1275 " int i;\n" 1276 "};", 1277 getLLVMStyleWithColumns(32)); 1278 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 1279 " aaaaaaaaaaaaaaaa> {};"); 1280 verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n" 1281 " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n" 1282 " aaaaaaaaaaaaaaaaaaaaaa> {};"); 1283 verifyFormat("template <class R, class C>\n" 1284 "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n" 1285 " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};"); 1286 verifyFormat("class ::A::B {};"); 1287 } 1288 1289 TEST_F(FormatTest, BreakBeforeInheritanceComma) { 1290 FormatStyle StyleWithInheritanceBreak = getLLVMStyle(); 1291 StyleWithInheritanceBreak.BreakBeforeInheritanceComma = true; 1292 1293 verifyFormat("class MyClass : public X {};", StyleWithInheritanceBreak); 1294 verifyFormat("class MyClass\n" 1295 " : public X\n" 1296 " , public Y {};", 1297 StyleWithInheritanceBreak); 1298 } 1299 1300 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) { 1301 verifyFormat("class A {\n} a, b;"); 1302 verifyFormat("struct A {\n} a, b;"); 1303 verifyFormat("union A {\n} a;"); 1304 } 1305 1306 TEST_F(FormatTest, FormatsEnum) { 1307 verifyFormat("enum {\n" 1308 " Zero,\n" 1309 " One = 1,\n" 1310 " Two = One + 1,\n" 1311 " Three = (One + Two),\n" 1312 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1313 " Five = (One, Two, Three, Four, 5)\n" 1314 "};"); 1315 verifyGoogleFormat("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 verifyFormat("enum Enum {};"); 1324 verifyFormat("enum {};"); 1325 verifyFormat("enum X E {} d;"); 1326 verifyFormat("enum __attribute__((...)) E {} d;"); 1327 verifyFormat("enum __declspec__((...)) E {} d;"); 1328 verifyFormat("enum {\n" 1329 " Bar = Foo<int, int>::value\n" 1330 "};", 1331 getLLVMStyleWithColumns(30)); 1332 1333 verifyFormat("enum ShortEnum { A, B, C };"); 1334 verifyGoogleFormat("enum ShortEnum { A, B, C };"); 1335 1336 EXPECT_EQ("enum KeepEmptyLines {\n" 1337 " ONE,\n" 1338 "\n" 1339 " TWO,\n" 1340 "\n" 1341 " THREE\n" 1342 "}", 1343 format("enum KeepEmptyLines {\n" 1344 " ONE,\n" 1345 "\n" 1346 " TWO,\n" 1347 "\n" 1348 "\n" 1349 " THREE\n" 1350 "}")); 1351 verifyFormat("enum E { // comment\n" 1352 " ONE,\n" 1353 " TWO\n" 1354 "};\n" 1355 "int i;"); 1356 // Not enums. 1357 verifyFormat("enum X f() {\n" 1358 " a();\n" 1359 " return 42;\n" 1360 "}"); 1361 verifyFormat("enum X Type::f() {\n" 1362 " a();\n" 1363 " return 42;\n" 1364 "}"); 1365 verifyFormat("enum ::X f() {\n" 1366 " a();\n" 1367 " return 42;\n" 1368 "}"); 1369 verifyFormat("enum ns::X f() {\n" 1370 " a();\n" 1371 " return 42;\n" 1372 "}"); 1373 } 1374 1375 TEST_F(FormatTest, FormatsEnumsWithErrors) { 1376 verifyFormat("enum Type {\n" 1377 " One = 0; // These semicolons should be commas.\n" 1378 " Two = 1;\n" 1379 "};"); 1380 verifyFormat("namespace n {\n" 1381 "enum Type {\n" 1382 " One,\n" 1383 " Two, // missing };\n" 1384 " int i;\n" 1385 "}\n" 1386 "void g() {}"); 1387 } 1388 1389 TEST_F(FormatTest, FormatsEnumStruct) { 1390 verifyFormat("enum struct {\n" 1391 " Zero,\n" 1392 " One = 1,\n" 1393 " Two = One + 1,\n" 1394 " Three = (One + Two),\n" 1395 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1396 " Five = (One, Two, Three, Four, 5)\n" 1397 "};"); 1398 verifyFormat("enum struct Enum {};"); 1399 verifyFormat("enum struct {};"); 1400 verifyFormat("enum struct X E {} d;"); 1401 verifyFormat("enum struct __attribute__((...)) E {} d;"); 1402 verifyFormat("enum struct __declspec__((...)) E {} d;"); 1403 verifyFormat("enum struct X f() {\n a();\n return 42;\n}"); 1404 } 1405 1406 TEST_F(FormatTest, FormatsEnumClass) { 1407 verifyFormat("enum class {\n" 1408 " Zero,\n" 1409 " One = 1,\n" 1410 " Two = One + 1,\n" 1411 " Three = (One + Two),\n" 1412 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1413 " Five = (One, Two, Three, Four, 5)\n" 1414 "};"); 1415 verifyFormat("enum class Enum {};"); 1416 verifyFormat("enum class {};"); 1417 verifyFormat("enum class X E {} d;"); 1418 verifyFormat("enum class __attribute__((...)) E {} d;"); 1419 verifyFormat("enum class __declspec__((...)) E {} d;"); 1420 verifyFormat("enum class X f() {\n a();\n return 42;\n}"); 1421 } 1422 1423 TEST_F(FormatTest, FormatsEnumTypes) { 1424 verifyFormat("enum X : int {\n" 1425 " A, // Force multiple lines.\n" 1426 " B\n" 1427 "};"); 1428 verifyFormat("enum X : int { A, B };"); 1429 verifyFormat("enum X : std::uint32_t { A, B };"); 1430 } 1431 1432 TEST_F(FormatTest, FormatsTypedefEnum) { 1433 FormatStyle Style = getLLVMStyle(); 1434 Style.ColumnLimit = 40; 1435 verifyFormat("typedef enum {} EmptyEnum;"); 1436 verifyFormat("typedef enum { A, B, C } ShortEnum;"); 1437 verifyFormat("typedef enum {\n" 1438 " ZERO = 0,\n" 1439 " ONE = 1,\n" 1440 " TWO = 2,\n" 1441 " THREE = 3\n" 1442 "} LongEnum;", 1443 Style); 1444 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 1445 Style.BraceWrapping.AfterEnum = true; 1446 verifyFormat("typedef enum {} EmptyEnum;"); 1447 verifyFormat("typedef enum { A, B, C } ShortEnum;"); 1448 verifyFormat("typedef enum\n" 1449 "{\n" 1450 " ZERO = 0,\n" 1451 " ONE = 1,\n" 1452 " TWO = 2,\n" 1453 " THREE = 3\n" 1454 "} LongEnum;", 1455 Style); 1456 } 1457 1458 TEST_F(FormatTest, FormatsNSEnums) { 1459 verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }"); 1460 verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n" 1461 " // Information about someDecentlyLongValue.\n" 1462 " someDecentlyLongValue,\n" 1463 " // Information about anotherDecentlyLongValue.\n" 1464 " anotherDecentlyLongValue,\n" 1465 " // Information about aThirdDecentlyLongValue.\n" 1466 " aThirdDecentlyLongValue\n" 1467 "};"); 1468 verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n" 1469 " a = 1,\n" 1470 " b = 2,\n" 1471 " c = 3,\n" 1472 "};"); 1473 verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n" 1474 " a = 1,\n" 1475 " b = 2,\n" 1476 " c = 3,\n" 1477 "};"); 1478 verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n" 1479 " a = 1,\n" 1480 " b = 2,\n" 1481 " c = 3,\n" 1482 "};"); 1483 } 1484 1485 TEST_F(FormatTest, FormatsBitfields) { 1486 verifyFormat("struct Bitfields {\n" 1487 " unsigned sClass : 8;\n" 1488 " unsigned ValueKind : 2;\n" 1489 "};"); 1490 verifyFormat("struct A {\n" 1491 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n" 1492 " bbbbbbbbbbbbbbbbbbbbbbbbb;\n" 1493 "};"); 1494 verifyFormat("struct MyStruct {\n" 1495 " uchar data;\n" 1496 " uchar : 8;\n" 1497 " uchar : 8;\n" 1498 " uchar other;\n" 1499 "};"); 1500 } 1501 1502 TEST_F(FormatTest, FormatsNamespaces) { 1503 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 1504 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 1505 1506 verifyFormat("namespace some_namespace {\n" 1507 "class A {};\n" 1508 "void f() { f(); }\n" 1509 "}", 1510 LLVMWithNoNamespaceFix); 1511 verifyFormat("namespace {\n" 1512 "class A {};\n" 1513 "void f() { f(); }\n" 1514 "}", 1515 LLVMWithNoNamespaceFix); 1516 verifyFormat("inline namespace X {\n" 1517 "class A {};\n" 1518 "void f() { f(); }\n" 1519 "}", 1520 LLVMWithNoNamespaceFix); 1521 verifyFormat("using namespace some_namespace;\n" 1522 "class A {};\n" 1523 "void f() { f(); }", 1524 LLVMWithNoNamespaceFix); 1525 1526 // This code is more common than we thought; if we 1527 // layout this correctly the semicolon will go into 1528 // its own line, which is undesirable. 1529 verifyFormat("namespace {};", 1530 LLVMWithNoNamespaceFix); 1531 verifyFormat("namespace {\n" 1532 "class A {};\n" 1533 "};", 1534 LLVMWithNoNamespaceFix); 1535 1536 verifyFormat("namespace {\n" 1537 "int SomeVariable = 0; // comment\n" 1538 "} // namespace", 1539 LLVMWithNoNamespaceFix); 1540 EXPECT_EQ("#ifndef HEADER_GUARD\n" 1541 "#define HEADER_GUARD\n" 1542 "namespace my_namespace {\n" 1543 "int i;\n" 1544 "} // my_namespace\n" 1545 "#endif // HEADER_GUARD", 1546 format("#ifndef HEADER_GUARD\n" 1547 " #define HEADER_GUARD\n" 1548 " namespace my_namespace {\n" 1549 "int i;\n" 1550 "} // my_namespace\n" 1551 "#endif // HEADER_GUARD", 1552 LLVMWithNoNamespaceFix)); 1553 1554 EXPECT_EQ("namespace A::B {\n" 1555 "class C {};\n" 1556 "}", 1557 format("namespace A::B {\n" 1558 "class C {};\n" 1559 "}", 1560 LLVMWithNoNamespaceFix)); 1561 1562 FormatStyle Style = getLLVMStyle(); 1563 Style.NamespaceIndentation = FormatStyle::NI_All; 1564 EXPECT_EQ("namespace out {\n" 1565 " int i;\n" 1566 " namespace in {\n" 1567 " int i;\n" 1568 " } // namespace in\n" 1569 "} // namespace out", 1570 format("namespace out {\n" 1571 "int i;\n" 1572 "namespace in {\n" 1573 "int i;\n" 1574 "} // namespace in\n" 1575 "} // namespace out", 1576 Style)); 1577 1578 Style.NamespaceIndentation = FormatStyle::NI_Inner; 1579 EXPECT_EQ("namespace out {\n" 1580 "int i;\n" 1581 "namespace in {\n" 1582 " int i;\n" 1583 "} // namespace in\n" 1584 "} // namespace out", 1585 format("namespace out {\n" 1586 "int i;\n" 1587 "namespace in {\n" 1588 "int i;\n" 1589 "} // namespace in\n" 1590 "} // namespace out", 1591 Style)); 1592 } 1593 1594 TEST_F(FormatTest, FormatsCompactNamespaces) { 1595 FormatStyle Style = getLLVMStyle(); 1596 Style.CompactNamespaces = true; 1597 1598 verifyFormat("namespace A { namespace B {\n" 1599 "}} // namespace A::B", 1600 Style); 1601 1602 EXPECT_EQ("namespace out { namespace in {\n" 1603 "}} // namespace out::in", 1604 format("namespace out {\n" 1605 "namespace in {\n" 1606 "} // namespace in\n" 1607 "} // namespace out", 1608 Style)); 1609 1610 // Only namespaces which have both consecutive opening and end get compacted 1611 EXPECT_EQ("namespace out {\n" 1612 "namespace in1 {\n" 1613 "} // namespace in1\n" 1614 "namespace in2 {\n" 1615 "} // namespace in2\n" 1616 "} // namespace out", 1617 format("namespace out {\n" 1618 "namespace in1 {\n" 1619 "} // namespace in1\n" 1620 "namespace in2 {\n" 1621 "} // namespace in2\n" 1622 "} // namespace out", 1623 Style)); 1624 1625 EXPECT_EQ("namespace out {\n" 1626 "int i;\n" 1627 "namespace in {\n" 1628 "int j;\n" 1629 "} // namespace in\n" 1630 "int k;\n" 1631 "} // namespace out", 1632 format("namespace out { int i;\n" 1633 "namespace in { int j; } // namespace in\n" 1634 "int k; } // namespace out", 1635 Style)); 1636 1637 EXPECT_EQ("namespace A { namespace B { namespace C {\n" 1638 "}}} // namespace A::B::C\n", 1639 format("namespace A { namespace B {\n" 1640 "namespace C {\n" 1641 "}} // namespace B::C\n" 1642 "} // namespace A\n", 1643 Style)); 1644 1645 Style.ColumnLimit = 40; 1646 EXPECT_EQ("namespace aaaaaaaaaa {\n" 1647 "namespace bbbbbbbbbb {\n" 1648 "}} // namespace aaaaaaaaaa::bbbbbbbbbb", 1649 format("namespace aaaaaaaaaa {\n" 1650 "namespace bbbbbbbbbb {\n" 1651 "} // namespace bbbbbbbbbb\n" 1652 "} // namespace aaaaaaaaaa", 1653 Style)); 1654 1655 EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n" 1656 "namespace cccccc {\n" 1657 "}}} // namespace aaaaaa::bbbbbb::cccccc", 1658 format("namespace aaaaaa {\n" 1659 "namespace bbbbbb {\n" 1660 "namespace cccccc {\n" 1661 "} // namespace cccccc\n" 1662 "} // namespace bbbbbb\n" 1663 "} // namespace aaaaaa", 1664 Style)); 1665 Style.ColumnLimit = 80; 1666 1667 // Extra semicolon after 'inner' closing brace prevents merging 1668 EXPECT_EQ("namespace out { namespace in {\n" 1669 "}; } // namespace out::in", 1670 format("namespace out {\n" 1671 "namespace in {\n" 1672 "}; // namespace in\n" 1673 "} // namespace out", 1674 Style)); 1675 1676 // Extra semicolon after 'outer' closing brace is conserved 1677 EXPECT_EQ("namespace out { namespace in {\n" 1678 "}}; // namespace out::in", 1679 format("namespace out {\n" 1680 "namespace in {\n" 1681 "} // namespace in\n" 1682 "}; // namespace out", 1683 Style)); 1684 1685 Style.NamespaceIndentation = FormatStyle::NI_All; 1686 EXPECT_EQ("namespace out { namespace in {\n" 1687 " int i;\n" 1688 "}} // namespace out::in", 1689 format("namespace out {\n" 1690 "namespace in {\n" 1691 "int i;\n" 1692 "} // namespace in\n" 1693 "} // namespace out", 1694 Style)); 1695 EXPECT_EQ("namespace out { namespace mid {\n" 1696 " namespace in {\n" 1697 " int j;\n" 1698 " } // namespace in\n" 1699 " int k;\n" 1700 "}} // namespace out::mid", 1701 format("namespace out { namespace mid {\n" 1702 "namespace in { int j; } // namespace in\n" 1703 "int k; }} // namespace out::mid", 1704 Style)); 1705 1706 Style.NamespaceIndentation = FormatStyle::NI_Inner; 1707 EXPECT_EQ("namespace out { namespace in {\n" 1708 " int i;\n" 1709 "}} // namespace out::in", 1710 format("namespace out {\n" 1711 "namespace in {\n" 1712 "int i;\n" 1713 "} // namespace in\n" 1714 "} // namespace out", 1715 Style)); 1716 EXPECT_EQ("namespace out { namespace mid { namespace in {\n" 1717 " int i;\n" 1718 "}}} // namespace out::mid::in", 1719 format("namespace out {\n" 1720 "namespace mid {\n" 1721 "namespace in {\n" 1722 "int i;\n" 1723 "} // namespace in\n" 1724 "} // namespace mid\n" 1725 "} // namespace out", 1726 Style)); 1727 } 1728 1729 TEST_F(FormatTest, FormatsExternC) { 1730 verifyFormat("extern \"C\" {\nint a;"); 1731 verifyFormat("extern \"C\" {}"); 1732 verifyFormat("extern \"C\" {\n" 1733 "int foo();\n" 1734 "}"); 1735 verifyFormat("extern \"C\" int foo() {}"); 1736 verifyFormat("extern \"C\" int foo();"); 1737 verifyFormat("extern \"C\" int foo() {\n" 1738 " int i = 42;\n" 1739 " return i;\n" 1740 "}"); 1741 1742 FormatStyle Style = getLLVMStyle(); 1743 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 1744 Style.BraceWrapping.AfterFunction = true; 1745 verifyFormat("extern \"C\" int foo() {}", Style); 1746 verifyFormat("extern \"C\" int foo();", Style); 1747 verifyFormat("extern \"C\" int foo()\n" 1748 "{\n" 1749 " int i = 42;\n" 1750 " return i;\n" 1751 "}", 1752 Style); 1753 1754 Style.BraceWrapping.AfterExternBlock = true; 1755 Style.BraceWrapping.SplitEmptyRecord = false; 1756 verifyFormat("extern \"C\"\n" 1757 "{}", 1758 Style); 1759 verifyFormat("extern \"C\"\n" 1760 "{\n" 1761 " int foo();\n" 1762 "}", 1763 Style); 1764 } 1765 1766 TEST_F(FormatTest, FormatsInlineASM) { 1767 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"); 1768 verifyFormat("asm(\"nop\" ::: \"memory\");"); 1769 verifyFormat( 1770 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" 1771 " \"cpuid\\n\\t\"\n" 1772 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n" 1773 " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n" 1774 " : \"a\"(value));"); 1775 EXPECT_EQ( 1776 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 1777 " __asm {\n" 1778 " mov edx,[that] // vtable in edx\n" 1779 " mov eax,methodIndex\n" 1780 " call [edx][eax*4] // stdcall\n" 1781 " }\n" 1782 "}", 1783 format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 1784 " __asm {\n" 1785 " mov edx,[that] // vtable in edx\n" 1786 " mov eax,methodIndex\n" 1787 " call [edx][eax*4] // stdcall\n" 1788 " }\n" 1789 "}")); 1790 EXPECT_EQ("_asm {\n" 1791 " xor eax, eax;\n" 1792 " cpuid;\n" 1793 "}", 1794 format("_asm {\n" 1795 " xor eax, eax;\n" 1796 " cpuid;\n" 1797 "}")); 1798 verifyFormat("void function() {\n" 1799 " // comment\n" 1800 " asm(\"\");\n" 1801 "}"); 1802 EXPECT_EQ("__asm {\n" 1803 "}\n" 1804 "int i;", 1805 format("__asm {\n" 1806 "}\n" 1807 "int i;")); 1808 } 1809 1810 TEST_F(FormatTest, FormatTryCatch) { 1811 verifyFormat("try {\n" 1812 " throw a * b;\n" 1813 "} catch (int a) {\n" 1814 " // Do nothing.\n" 1815 "} catch (...) {\n" 1816 " exit(42);\n" 1817 "}"); 1818 1819 // Function-level try statements. 1820 verifyFormat("int f() try { return 4; } catch (...) {\n" 1821 " return 5;\n" 1822 "}"); 1823 verifyFormat("class A {\n" 1824 " int a;\n" 1825 " A() try : a(0) {\n" 1826 " } catch (...) {\n" 1827 " throw;\n" 1828 " }\n" 1829 "};\n"); 1830 1831 // Incomplete try-catch blocks. 1832 verifyIncompleteFormat("try {} catch ("); 1833 } 1834 1835 TEST_F(FormatTest, FormatSEHTryCatch) { 1836 verifyFormat("__try {\n" 1837 " int a = b * c;\n" 1838 "} __except (EXCEPTION_EXECUTE_HANDLER) {\n" 1839 " // Do nothing.\n" 1840 "}"); 1841 1842 verifyFormat("__try {\n" 1843 " int a = b * c;\n" 1844 "} __finally {\n" 1845 " // Do nothing.\n" 1846 "}"); 1847 1848 verifyFormat("DEBUG({\n" 1849 " __try {\n" 1850 " } __finally {\n" 1851 " }\n" 1852 "});\n"); 1853 } 1854 1855 TEST_F(FormatTest, IncompleteTryCatchBlocks) { 1856 verifyFormat("try {\n" 1857 " f();\n" 1858 "} catch {\n" 1859 " g();\n" 1860 "}"); 1861 verifyFormat("try {\n" 1862 " f();\n" 1863 "} catch (A a) MACRO(x) {\n" 1864 " g();\n" 1865 "} catch (B b) MACRO(x) {\n" 1866 " g();\n" 1867 "}"); 1868 } 1869 1870 TEST_F(FormatTest, FormatTryCatchBraceStyles) { 1871 FormatStyle Style = getLLVMStyle(); 1872 for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla, 1873 FormatStyle::BS_WebKit}) { 1874 Style.BreakBeforeBraces = BraceStyle; 1875 verifyFormat("try {\n" 1876 " // something\n" 1877 "} catch (...) {\n" 1878 " // something\n" 1879 "}", 1880 Style); 1881 } 1882 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 1883 verifyFormat("try {\n" 1884 " // something\n" 1885 "}\n" 1886 "catch (...) {\n" 1887 " // something\n" 1888 "}", 1889 Style); 1890 verifyFormat("__try {\n" 1891 " // something\n" 1892 "}\n" 1893 "__finally {\n" 1894 " // something\n" 1895 "}", 1896 Style); 1897 verifyFormat("@try {\n" 1898 " // something\n" 1899 "}\n" 1900 "@finally {\n" 1901 " // something\n" 1902 "}", 1903 Style); 1904 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 1905 verifyFormat("try\n" 1906 "{\n" 1907 " // something\n" 1908 "}\n" 1909 "catch (...)\n" 1910 "{\n" 1911 " // something\n" 1912 "}", 1913 Style); 1914 Style.BreakBeforeBraces = FormatStyle::BS_GNU; 1915 verifyFormat("try\n" 1916 " {\n" 1917 " // something\n" 1918 " }\n" 1919 "catch (...)\n" 1920 " {\n" 1921 " // something\n" 1922 " }", 1923 Style); 1924 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 1925 Style.BraceWrapping.BeforeCatch = true; 1926 verifyFormat("try {\n" 1927 " // something\n" 1928 "}\n" 1929 "catch (...) {\n" 1930 " // something\n" 1931 "}", 1932 Style); 1933 } 1934 1935 TEST_F(FormatTest, StaticInitializers) { 1936 verifyFormat("static SomeClass SC = {1, 'a'};"); 1937 1938 verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n" 1939 " 100000000, " 1940 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};"); 1941 1942 // Here, everything other than the "}" would fit on a line. 1943 verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n" 1944 " 10000000000000000000000000};"); 1945 EXPECT_EQ("S s = {a,\n" 1946 "\n" 1947 " b};", 1948 format("S s = {\n" 1949 " a,\n" 1950 "\n" 1951 " b\n" 1952 "};")); 1953 1954 // FIXME: This would fit into the column limit if we'd fit "{ {" on the first 1955 // line. However, the formatting looks a bit off and this probably doesn't 1956 // happen often in practice. 1957 verifyFormat("static int Variable[1] = {\n" 1958 " {1000000000000000000000000000000000000}};", 1959 getLLVMStyleWithColumns(40)); 1960 } 1961 1962 TEST_F(FormatTest, DesignatedInitializers) { 1963 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 1964 verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n" 1965 " .bbbbbbbbbb = 2,\n" 1966 " .cccccccccc = 3,\n" 1967 " .dddddddddd = 4,\n" 1968 " .eeeeeeeeee = 5};"); 1969 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 1970 " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n" 1971 " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n" 1972 " .ccccccccccccccccccccccccccc = 3,\n" 1973 " .ddddddddddddddddddddddddddd = 4,\n" 1974 " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};"); 1975 1976 verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};"); 1977 1978 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 1979 verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n" 1980 " [2] = bbbbbbbbbb,\n" 1981 " [3] = cccccccccc,\n" 1982 " [4] = dddddddddd,\n" 1983 " [5] = eeeeeeeeee};"); 1984 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 1985 " [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1986 " [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 1987 " [3] = cccccccccccccccccccccccccccccccccccccc,\n" 1988 " [4] = dddddddddddddddddddddddddddddddddddddd,\n" 1989 " [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};"); 1990 } 1991 1992 TEST_F(FormatTest, NestedStaticInitializers) { 1993 verifyFormat("static A x = {{{}}};\n"); 1994 verifyFormat("static A x = {{{init1, init2, init3, init4},\n" 1995 " {init1, init2, init3, init4}}};", 1996 getLLVMStyleWithColumns(50)); 1997 1998 verifyFormat("somes Status::global_reps[3] = {\n" 1999 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 2000 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 2001 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};", 2002 getLLVMStyleWithColumns(60)); 2003 verifyGoogleFormat("SomeType Status::global_reps[3] = {\n" 2004 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 2005 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 2006 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};"); 2007 verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n" 2008 " {rect.fRight - rect.fLeft, rect.fBottom - " 2009 "rect.fTop}};"); 2010 2011 verifyFormat( 2012 "SomeArrayOfSomeType a = {\n" 2013 " {{1, 2, 3},\n" 2014 " {1, 2, 3},\n" 2015 " {111111111111111111111111111111, 222222222222222222222222222222,\n" 2016 " 333333333333333333333333333333},\n" 2017 " {1, 2, 3},\n" 2018 " {1, 2, 3}}};"); 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 2028 verifyFormat("struct {\n" 2029 " unsigned bit;\n" 2030 " const char *const name;\n" 2031 "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n" 2032 " {kOsWin, \"Windows\"},\n" 2033 " {kOsLinux, \"Linux\"},\n" 2034 " {kOsCrOS, \"Chrome OS\"}};"); 2035 verifyFormat("struct {\n" 2036 " unsigned bit;\n" 2037 " const char *const name;\n" 2038 "} kBitsToOs[] = {\n" 2039 " {kOsMac, \"Mac\"},\n" 2040 " {kOsWin, \"Windows\"},\n" 2041 " {kOsLinux, \"Linux\"},\n" 2042 " {kOsCrOS, \"Chrome OS\"},\n" 2043 "};"); 2044 } 2045 2046 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { 2047 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 2048 " \\\n" 2049 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); 2050 } 2051 2052 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { 2053 verifyFormat("virtual void write(ELFWriter *writerrr,\n" 2054 " OwningPtr<FileOutputBuffer> &buffer) = 0;"); 2055 2056 // Do break defaulted and deleted functions. 2057 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 2058 " default;", 2059 getLLVMStyleWithColumns(40)); 2060 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 2061 " delete;", 2062 getLLVMStyleWithColumns(40)); 2063 } 2064 2065 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) { 2066 verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3", 2067 getLLVMStyleWithColumns(40)); 2068 verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 2069 getLLVMStyleWithColumns(40)); 2070 EXPECT_EQ("#define Q \\\n" 2071 " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n" 2072 " \"aaaaaaaa.cpp\"", 2073 format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 2074 getLLVMStyleWithColumns(40))); 2075 } 2076 2077 TEST_F(FormatTest, UnderstandsLinePPDirective) { 2078 EXPECT_EQ("# 123 \"A string literal\"", 2079 format(" # 123 \"A string literal\"")); 2080 } 2081 2082 TEST_F(FormatTest, LayoutUnknownPPDirective) { 2083 EXPECT_EQ("#;", format("#;")); 2084 verifyFormat("#\n;\n;\n;"); 2085 } 2086 2087 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { 2088 EXPECT_EQ("#line 42 \"test\"\n", 2089 format("# \\\n line \\\n 42 \\\n \"test\"\n")); 2090 EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n", 2091 getLLVMStyleWithColumns(12))); 2092 } 2093 2094 TEST_F(FormatTest, EndOfFileEndsPPDirective) { 2095 EXPECT_EQ("#line 42 \"test\"", 2096 format("# \\\n line \\\n 42 \\\n \"test\"")); 2097 EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B")); 2098 } 2099 2100 TEST_F(FormatTest, DoesntRemoveUnknownTokens) { 2101 verifyFormat("#define A \\x20"); 2102 verifyFormat("#define A \\ x20"); 2103 EXPECT_EQ("#define A \\ x20", format("#define A \\ x20")); 2104 verifyFormat("#define A ''"); 2105 verifyFormat("#define A ''qqq"); 2106 verifyFormat("#define A `qqq"); 2107 verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");"); 2108 EXPECT_EQ("const char *c = STRINGIFY(\n" 2109 "\\na : b);", 2110 format("const char * c = STRINGIFY(\n" 2111 "\\na : b);")); 2112 2113 verifyFormat("a\r\\"); 2114 verifyFormat("a\v\\"); 2115 verifyFormat("a\f\\"); 2116 } 2117 2118 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { 2119 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13)); 2120 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12)); 2121 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); 2122 // FIXME: We never break before the macro name. 2123 verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12)); 2124 2125 verifyFormat("#define A A\n#define A A"); 2126 verifyFormat("#define A(X) A\n#define A A"); 2127 2128 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23)); 2129 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22)); 2130 } 2131 2132 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { 2133 EXPECT_EQ("// somecomment\n" 2134 "#include \"a.h\"\n" 2135 "#define A( \\\n" 2136 " A, B)\n" 2137 "#include \"b.h\"\n" 2138 "// somecomment\n", 2139 format(" // somecomment\n" 2140 " #include \"a.h\"\n" 2141 "#define A(A,\\\n" 2142 " B)\n" 2143 " #include \"b.h\"\n" 2144 " // somecomment\n", 2145 getLLVMStyleWithColumns(13))); 2146 } 2147 2148 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); } 2149 2150 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { 2151 EXPECT_EQ("#define A \\\n" 2152 " c; \\\n" 2153 " e;\n" 2154 "f;", 2155 format("#define A c; e;\n" 2156 "f;", 2157 getLLVMStyleWithColumns(14))); 2158 } 2159 2160 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); } 2161 2162 TEST_F(FormatTest, MacroDefinitionInsideStatement) { 2163 EXPECT_EQ("int x,\n" 2164 "#define A\n" 2165 " y;", 2166 format("int x,\n#define A\ny;")); 2167 } 2168 2169 TEST_F(FormatTest, HashInMacroDefinition) { 2170 EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle())); 2171 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); 2172 verifyFormat("#define A \\\n" 2173 " { \\\n" 2174 " f(#c); \\\n" 2175 " }", 2176 getLLVMStyleWithColumns(11)); 2177 2178 verifyFormat("#define A(X) \\\n" 2179 " void function##X()", 2180 getLLVMStyleWithColumns(22)); 2181 2182 verifyFormat("#define A(a, b, c) \\\n" 2183 " void a##b##c()", 2184 getLLVMStyleWithColumns(22)); 2185 2186 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22)); 2187 } 2188 2189 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { 2190 EXPECT_EQ("#define A (x)", format("#define A (x)")); 2191 EXPECT_EQ("#define A(x)", format("#define A(x)")); 2192 } 2193 2194 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) { 2195 EXPECT_EQ("#define A b;", format("#define A \\\n" 2196 " \\\n" 2197 " b;", 2198 getLLVMStyleWithColumns(25))); 2199 EXPECT_EQ("#define A \\\n" 2200 " \\\n" 2201 " a; \\\n" 2202 " b;", 2203 format("#define A \\\n" 2204 " \\\n" 2205 " a; \\\n" 2206 " b;", 2207 getLLVMStyleWithColumns(11))); 2208 EXPECT_EQ("#define A \\\n" 2209 " a; \\\n" 2210 " \\\n" 2211 " b;", 2212 format("#define A \\\n" 2213 " a; \\\n" 2214 " \\\n" 2215 " b;", 2216 getLLVMStyleWithColumns(11))); 2217 } 2218 2219 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { 2220 verifyIncompleteFormat("#define A :"); 2221 verifyFormat("#define SOMECASES \\\n" 2222 " case 1: \\\n" 2223 " case 2\n", 2224 getLLVMStyleWithColumns(20)); 2225 verifyFormat("#define MACRO(a) \\\n" 2226 " if (a) \\\n" 2227 " f(); \\\n" 2228 " else \\\n" 2229 " g()", 2230 getLLVMStyleWithColumns(18)); 2231 verifyFormat("#define A template <typename T>"); 2232 verifyIncompleteFormat("#define STR(x) #x\n" 2233 "f(STR(this_is_a_string_literal{));"); 2234 verifyFormat("#pragma omp threadprivate( \\\n" 2235 " y)), // expected-warning", 2236 getLLVMStyleWithColumns(28)); 2237 verifyFormat("#d, = };"); 2238 verifyFormat("#if \"a"); 2239 verifyIncompleteFormat("({\n" 2240 "#define b \\\n" 2241 " } \\\n" 2242 " a\n" 2243 "a", 2244 getLLVMStyleWithColumns(15)); 2245 verifyFormat("#define A \\\n" 2246 " { \\\n" 2247 " {\n" 2248 "#define B \\\n" 2249 " } \\\n" 2250 " }", 2251 getLLVMStyleWithColumns(15)); 2252 verifyNoCrash("#if a\na(\n#else\n#endif\n{a"); 2253 verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}"); 2254 verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};"); 2255 verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}"); 2256 } 2257 2258 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) { 2259 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline. 2260 EXPECT_EQ("class A : public QObject {\n" 2261 " Q_OBJECT\n" 2262 "\n" 2263 " A() {}\n" 2264 "};", 2265 format("class A : public QObject {\n" 2266 " Q_OBJECT\n" 2267 "\n" 2268 " A() {\n}\n" 2269 "} ;")); 2270 EXPECT_EQ("MACRO\n" 2271 "/*static*/ int i;", 2272 format("MACRO\n" 2273 " /*static*/ int i;")); 2274 EXPECT_EQ("SOME_MACRO\n" 2275 "namespace {\n" 2276 "void f();\n" 2277 "} // namespace", 2278 format("SOME_MACRO\n" 2279 " namespace {\n" 2280 "void f( );\n" 2281 "} // namespace")); 2282 // Only if the identifier contains at least 5 characters. 2283 EXPECT_EQ("HTTP f();", format("HTTP\nf();")); 2284 EXPECT_EQ("MACRO\nf();", format("MACRO\nf();")); 2285 // Only if everything is upper case. 2286 EXPECT_EQ("class A : public QObject {\n" 2287 " Q_Object A() {}\n" 2288 "};", 2289 format("class A : public QObject {\n" 2290 " Q_Object\n" 2291 " A() {\n}\n" 2292 "} ;")); 2293 2294 // Only if the next line can actually start an unwrapped line. 2295 EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;", 2296 format("SOME_WEIRD_LOG_MACRO\n" 2297 "<< SomeThing;")); 2298 2299 verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), " 2300 "(n, buffers))\n", 2301 getChromiumStyle(FormatStyle::LK_Cpp)); 2302 } 2303 2304 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) { 2305 EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 2306 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 2307 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 2308 "class X {};\n" 2309 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 2310 "int *createScopDetectionPass() { return 0; }", 2311 format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 2312 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 2313 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 2314 " class X {};\n" 2315 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 2316 " int *createScopDetectionPass() { return 0; }")); 2317 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as 2318 // braces, so that inner block is indented one level more. 2319 EXPECT_EQ("int q() {\n" 2320 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 2321 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 2322 " IPC_END_MESSAGE_MAP()\n" 2323 "}", 2324 format("int q() {\n" 2325 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 2326 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 2327 " IPC_END_MESSAGE_MAP()\n" 2328 "}")); 2329 2330 // Same inside macros. 2331 EXPECT_EQ("#define LIST(L) \\\n" 2332 " L(A) \\\n" 2333 " L(B) \\\n" 2334 " L(C)", 2335 format("#define LIST(L) \\\n" 2336 " L(A) \\\n" 2337 " L(B) \\\n" 2338 " L(C)", 2339 getGoogleStyle())); 2340 2341 // These must not be recognized as macros. 2342 EXPECT_EQ("int q() {\n" 2343 " f(x);\n" 2344 " f(x) {}\n" 2345 " f(x)->g();\n" 2346 " f(x)->*g();\n" 2347 " f(x).g();\n" 2348 " f(x) = x;\n" 2349 " f(x) += x;\n" 2350 " f(x) -= x;\n" 2351 " f(x) *= x;\n" 2352 " f(x) /= x;\n" 2353 " f(x) %= x;\n" 2354 " f(x) &= x;\n" 2355 " f(x) |= x;\n" 2356 " f(x) ^= x;\n" 2357 " f(x) >>= x;\n" 2358 " f(x) <<= x;\n" 2359 " f(x)[y].z();\n" 2360 " LOG(INFO) << x;\n" 2361 " ifstream(x) >> x;\n" 2362 "}\n", 2363 format("int q() {\n" 2364 " f(x)\n;\n" 2365 " f(x)\n {}\n" 2366 " f(x)\n->g();\n" 2367 " f(x)\n->*g();\n" 2368 " f(x)\n.g();\n" 2369 " f(x)\n = x;\n" 2370 " f(x)\n += x;\n" 2371 " f(x)\n -= x;\n" 2372 " f(x)\n *= x;\n" 2373 " f(x)\n /= x;\n" 2374 " f(x)\n %= x;\n" 2375 " f(x)\n &= x;\n" 2376 " f(x)\n |= x;\n" 2377 " f(x)\n ^= x;\n" 2378 " f(x)\n >>= x;\n" 2379 " f(x)\n <<= x;\n" 2380 " f(x)\n[y].z();\n" 2381 " LOG(INFO)\n << x;\n" 2382 " ifstream(x)\n >> x;\n" 2383 "}\n")); 2384 EXPECT_EQ("int q() {\n" 2385 " F(x)\n" 2386 " if (1) {\n" 2387 " }\n" 2388 " F(x)\n" 2389 " while (1) {\n" 2390 " }\n" 2391 " F(x)\n" 2392 " G(x);\n" 2393 " F(x)\n" 2394 " try {\n" 2395 " Q();\n" 2396 " } catch (...) {\n" 2397 " }\n" 2398 "}\n", 2399 format("int q() {\n" 2400 "F(x)\n" 2401 "if (1) {}\n" 2402 "F(x)\n" 2403 "while (1) {}\n" 2404 "F(x)\n" 2405 "G(x);\n" 2406 "F(x)\n" 2407 "try { Q(); } catch (...) {}\n" 2408 "}\n")); 2409 EXPECT_EQ("class A {\n" 2410 " A() : t(0) {}\n" 2411 " A(int i) noexcept() : {}\n" 2412 " A(X x)\n" // FIXME: function-level try blocks are broken. 2413 " try : t(0) {\n" 2414 " } catch (...) {\n" 2415 " }\n" 2416 "};", 2417 format("class A {\n" 2418 " A()\n : t(0) {}\n" 2419 " A(int i)\n noexcept() : {}\n" 2420 " A(X x)\n" 2421 " try : t(0) {} catch (...) {}\n" 2422 "};")); 2423 EXPECT_EQ("class SomeClass {\n" 2424 "public:\n" 2425 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2426 "};", 2427 format("class SomeClass {\n" 2428 "public:\n" 2429 " SomeClass()\n" 2430 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2431 "};")); 2432 EXPECT_EQ("class SomeClass {\n" 2433 "public:\n" 2434 " SomeClass()\n" 2435 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2436 "};", 2437 format("class SomeClass {\n" 2438 "public:\n" 2439 " SomeClass()\n" 2440 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2441 "};", 2442 getLLVMStyleWithColumns(40))); 2443 2444 verifyFormat("MACRO(>)"); 2445 } 2446 2447 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) { 2448 verifyFormat("#define A \\\n" 2449 " f({ \\\n" 2450 " g(); \\\n" 2451 " });", 2452 getLLVMStyleWithColumns(11)); 2453 } 2454 2455 TEST_F(FormatTest, IndentPreprocessorDirectives) { 2456 FormatStyle Style = getLLVMStyle(); 2457 Style.IndentPPDirectives = FormatStyle::PPDIS_None; 2458 Style.ColumnLimit = 40; 2459 verifyFormat("#ifdef _WIN32\n" 2460 "#define A 0\n" 2461 "#ifdef VAR2\n" 2462 "#define B 1\n" 2463 "#include <someheader.h>\n" 2464 "#define MACRO \\\n" 2465 " some_very_long_func_aaaaaaaaaa();\n" 2466 "#endif\n" 2467 "#else\n" 2468 "#define A 1\n" 2469 "#endif", 2470 Style); 2471 Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash; 2472 verifyFormat("#ifdef _WIN32\n" 2473 "# define A 0\n" 2474 "# ifdef VAR2\n" 2475 "# define B 1\n" 2476 "# include <someheader.h>\n" 2477 "# define MACRO \\\n" 2478 " some_very_long_func_aaaaaaaaaa();\n" 2479 "# endif\n" 2480 "#else\n" 2481 "# define A 1\n" 2482 "#endif", 2483 Style); 2484 verifyFormat("#if A\n" 2485 "# define MACRO \\\n" 2486 " void a(int x) { \\\n" 2487 " b(); \\\n" 2488 " c(); \\\n" 2489 " d(); \\\n" 2490 " e(); \\\n" 2491 " f(); \\\n" 2492 " }\n" 2493 "#endif", 2494 Style); 2495 // Comments before include guard. 2496 verifyFormat("// file comment\n" 2497 "// file comment\n" 2498 "#ifndef HEADER_H\n" 2499 "#define HEADER_H\n" 2500 "code();\n" 2501 "#endif", 2502 Style); 2503 // Test with include guards. 2504 verifyFormat("#ifndef HEADER_H\n" 2505 "#define HEADER_H\n" 2506 "code();\n" 2507 "#endif", 2508 Style); 2509 // Include guards must have a #define with the same variable immediately 2510 // after #ifndef. 2511 verifyFormat("#ifndef NOT_GUARD\n" 2512 "# define FOO\n" 2513 "code();\n" 2514 "#endif", 2515 Style); 2516 2517 // Include guards must cover the entire file. 2518 verifyFormat("code();\n" 2519 "code();\n" 2520 "#ifndef NOT_GUARD\n" 2521 "# define NOT_GUARD\n" 2522 "code();\n" 2523 "#endif", 2524 Style); 2525 verifyFormat("#ifndef NOT_GUARD\n" 2526 "# define NOT_GUARD\n" 2527 "code();\n" 2528 "#endif\n" 2529 "code();", 2530 Style); 2531 // Test with trailing blank lines. 2532 verifyFormat("#ifndef HEADER_H\n" 2533 "#define HEADER_H\n" 2534 "code();\n" 2535 "#endif\n", 2536 Style); 2537 // Include guards don't have #else. 2538 verifyFormat("#ifndef NOT_GUARD\n" 2539 "# define NOT_GUARD\n" 2540 "code();\n" 2541 "#else\n" 2542 "#endif", 2543 Style); 2544 verifyFormat("#ifndef NOT_GUARD\n" 2545 "# define NOT_GUARD\n" 2546 "code();\n" 2547 "#elif FOO\n" 2548 "#endif", 2549 Style); 2550 // FIXME: This doesn't handle the case where there's code between the 2551 // #ifndef and #define but all other conditions hold. This is because when 2552 // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the 2553 // previous code line yet, so we can't detect it. 2554 EXPECT_EQ("#ifndef NOT_GUARD\n" 2555 "code();\n" 2556 "#define NOT_GUARD\n" 2557 "code();\n" 2558 "#endif", 2559 format("#ifndef NOT_GUARD\n" 2560 "code();\n" 2561 "# define NOT_GUARD\n" 2562 "code();\n" 2563 "#endif", 2564 Style)); 2565 // FIXME: This doesn't handle cases where legitimate preprocessor lines may 2566 // be outside an include guard. Examples are #pragma once and 2567 // #pragma GCC diagnostic, or anything else that does not change the meaning 2568 // of the file if it's included multiple times. 2569 EXPECT_EQ("#ifdef WIN32\n" 2570 "# pragma once\n" 2571 "#endif\n" 2572 "#ifndef HEADER_H\n" 2573 "# define HEADER_H\n" 2574 "code();\n" 2575 "#endif", 2576 format("#ifdef WIN32\n" 2577 "# pragma once\n" 2578 "#endif\n" 2579 "#ifndef HEADER_H\n" 2580 "#define HEADER_H\n" 2581 "code();\n" 2582 "#endif", 2583 Style)); 2584 // FIXME: This does not detect when there is a single non-preprocessor line 2585 // in front of an include-guard-like structure where other conditions hold 2586 // because ScopedLineState hides the line. 2587 EXPECT_EQ("code();\n" 2588 "#ifndef HEADER_H\n" 2589 "#define HEADER_H\n" 2590 "code();\n" 2591 "#endif", 2592 format("code();\n" 2593 "#ifndef HEADER_H\n" 2594 "# define HEADER_H\n" 2595 "code();\n" 2596 "#endif", 2597 Style)); 2598 // Keep comments aligned with #, otherwise indent comments normally. These 2599 // tests cannot use verifyFormat because messUp manipulates leading 2600 // whitespace. 2601 { 2602 const char *Expected = "" 2603 "void f() {\n" 2604 "#if 1\n" 2605 "// Preprocessor aligned.\n" 2606 "# define A 0\n" 2607 " // Code. Separated by blank line.\n" 2608 "\n" 2609 "# define B 0\n" 2610 " // Code. Not aligned with #\n" 2611 "# define C 0\n" 2612 "#endif"; 2613 const char *ToFormat = "" 2614 "void f() {\n" 2615 "#if 1\n" 2616 "// Preprocessor aligned.\n" 2617 "# define A 0\n" 2618 "// Code. Separated by blank line.\n" 2619 "\n" 2620 "# define B 0\n" 2621 " // Code. Not aligned with #\n" 2622 "# define C 0\n" 2623 "#endif"; 2624 EXPECT_EQ(Expected, format(ToFormat, Style)); 2625 EXPECT_EQ(Expected, format(Expected, Style)); 2626 } 2627 // Keep block quotes aligned. 2628 { 2629 const char *Expected = "" 2630 "void f() {\n" 2631 "#if 1\n" 2632 "/* Preprocessor aligned. */\n" 2633 "# define A 0\n" 2634 " /* Code. Separated by blank line. */\n" 2635 "\n" 2636 "# define B 0\n" 2637 " /* Code. Not aligned with # */\n" 2638 "# define C 0\n" 2639 "#endif"; 2640 const char *ToFormat = "" 2641 "void f() {\n" 2642 "#if 1\n" 2643 "/* Preprocessor aligned. */\n" 2644 "# define A 0\n" 2645 "/* Code. Separated by blank line. */\n" 2646 "\n" 2647 "# define B 0\n" 2648 " /* Code. Not aligned with # */\n" 2649 "# define C 0\n" 2650 "#endif"; 2651 EXPECT_EQ(Expected, format(ToFormat, Style)); 2652 EXPECT_EQ(Expected, format(Expected, Style)); 2653 } 2654 // Keep comments aligned with un-indented directives. 2655 { 2656 const char *Expected = "" 2657 "void f() {\n" 2658 "// Preprocessor aligned.\n" 2659 "#define A 0\n" 2660 " // Code. Separated by blank line.\n" 2661 "\n" 2662 "#define B 0\n" 2663 " // Code. Not aligned with #\n" 2664 "#define C 0\n"; 2665 const char *ToFormat = "" 2666 "void f() {\n" 2667 "// Preprocessor aligned.\n" 2668 "#define A 0\n" 2669 "// Code. Separated by blank line.\n" 2670 "\n" 2671 "#define B 0\n" 2672 " // Code. Not aligned with #\n" 2673 "#define C 0\n"; 2674 EXPECT_EQ(Expected, format(ToFormat, Style)); 2675 EXPECT_EQ(Expected, format(Expected, Style)); 2676 } 2677 // Test with tabs. 2678 Style.UseTab = FormatStyle::UT_Always; 2679 Style.IndentWidth = 8; 2680 Style.TabWidth = 8; 2681 verifyFormat("#ifdef _WIN32\n" 2682 "#\tdefine A 0\n" 2683 "#\tifdef VAR2\n" 2684 "#\t\tdefine B 1\n" 2685 "#\t\tinclude <someheader.h>\n" 2686 "#\t\tdefine MACRO \\\n" 2687 "\t\t\tsome_very_long_func_aaaaaaaaaa();\n" 2688 "#\tendif\n" 2689 "#else\n" 2690 "#\tdefine A 1\n" 2691 "#endif", 2692 Style); 2693 2694 // Regression test: Multiline-macro inside include guards. 2695 verifyFormat("#ifndef HEADER_H\n" 2696 "#define HEADER_H\n" 2697 "#define A() \\\n" 2698 " int i; \\\n" 2699 " int j;\n" 2700 "#endif // HEADER_H", 2701 getLLVMStyleWithColumns(20)); 2702 } 2703 2704 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { 2705 verifyFormat("{\n { a #c; }\n}"); 2706 } 2707 2708 TEST_F(FormatTest, FormatUnbalancedStructuralElements) { 2709 EXPECT_EQ("#define A \\\n { \\\n {\nint i;", 2710 format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); 2711 EXPECT_EQ("#define A \\\n } \\\n }\nint i;", 2712 format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); 2713 } 2714 2715 TEST_F(FormatTest, EscapedNewlines) { 2716 FormatStyle Narrow = getLLVMStyleWithColumns(11); 2717 EXPECT_EQ("#define A \\\n int i; \\\n int j;", 2718 format("#define A \\\nint i;\\\n int j;", Narrow)); 2719 EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;")); 2720 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 2721 EXPECT_EQ("/* \\ \\ \\\n */", format("\\\n/* \\ \\ \\\n */")); 2722 EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>")); 2723 2724 FormatStyle AlignLeft = getLLVMStyle(); 2725 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 2726 EXPECT_EQ("#define MACRO(x) \\\n" 2727 "private: \\\n" 2728 " int x(int a);\n", 2729 format("#define MACRO(x) \\\n" 2730 "private: \\\n" 2731 " int x(int a);\n", 2732 AlignLeft)); 2733 2734 // CRLF line endings 2735 EXPECT_EQ("#define A \\\r\n int i; \\\r\n int j;", 2736 format("#define A \\\r\nint i;\\\r\n int j;", Narrow)); 2737 EXPECT_EQ("#define A\r\n\r\nint i;", format("#define A \\\r\n\r\n int i;")); 2738 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 2739 EXPECT_EQ("/* \\ \\ \\\r\n */", format("\\\r\n/* \\ \\ \\\r\n */")); 2740 EXPECT_EQ("<a\r\n\\\\\r\n>", format("<a\r\n\\\\\r\n>")); 2741 EXPECT_EQ("#define MACRO(x) \\\r\n" 2742 "private: \\\r\n" 2743 " int x(int a);\r\n", 2744 format("#define MACRO(x) \\\r\n" 2745 "private: \\\r\n" 2746 " int x(int a);\r\n", 2747 AlignLeft)); 2748 2749 FormatStyle DontAlign = getLLVMStyle(); 2750 DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 2751 DontAlign.MaxEmptyLinesToKeep = 3; 2752 // FIXME: can't use verifyFormat here because the newline before 2753 // "public:" is not inserted the first time it's reformatted 2754 EXPECT_EQ("#define A \\\n" 2755 " class Foo { \\\n" 2756 " void bar(); \\\n" 2757 "\\\n" 2758 "\\\n" 2759 "\\\n" 2760 " public: \\\n" 2761 " void baz(); \\\n" 2762 " };", 2763 format("#define A \\\n" 2764 " class Foo { \\\n" 2765 " void bar(); \\\n" 2766 "\\\n" 2767 "\\\n" 2768 "\\\n" 2769 " public: \\\n" 2770 " void baz(); \\\n" 2771 " };", 2772 DontAlign)); 2773 } 2774 2775 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { 2776 verifyFormat("#define A \\\n" 2777 " int v( \\\n" 2778 " a); \\\n" 2779 " int i;", 2780 getLLVMStyleWithColumns(11)); 2781 } 2782 2783 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { 2784 EXPECT_EQ( 2785 "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 2786 " \\\n" 2787 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 2788 "\n" 2789 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 2790 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", 2791 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" 2792 "\\\n" 2793 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 2794 " \n" 2795 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 2796 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); 2797 } 2798 2799 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { 2800 EXPECT_EQ("int\n" 2801 "#define A\n" 2802 " a;", 2803 format("int\n#define A\na;")); 2804 verifyFormat("functionCallTo(\n" 2805 " someOtherFunction(\n" 2806 " withSomeParameters, whichInSequence,\n" 2807 " areLongerThanALine(andAnotherCall,\n" 2808 "#define A B\n" 2809 " withMoreParamters,\n" 2810 " whichStronglyInfluenceTheLayout),\n" 2811 " andMoreParameters),\n" 2812 " trailing);", 2813 getLLVMStyleWithColumns(69)); 2814 verifyFormat("Foo::Foo()\n" 2815 "#ifdef BAR\n" 2816 " : baz(0)\n" 2817 "#endif\n" 2818 "{\n" 2819 "}"); 2820 verifyFormat("void f() {\n" 2821 " if (true)\n" 2822 "#ifdef A\n" 2823 " f(42);\n" 2824 " x();\n" 2825 "#else\n" 2826 " g();\n" 2827 " x();\n" 2828 "#endif\n" 2829 "}"); 2830 verifyFormat("void f(param1, param2,\n" 2831 " param3,\n" 2832 "#ifdef A\n" 2833 " param4(param5,\n" 2834 "#ifdef A1\n" 2835 " param6,\n" 2836 "#ifdef A2\n" 2837 " param7),\n" 2838 "#else\n" 2839 " param8),\n" 2840 " param9,\n" 2841 "#endif\n" 2842 " param10,\n" 2843 "#endif\n" 2844 " param11)\n" 2845 "#else\n" 2846 " param12)\n" 2847 "#endif\n" 2848 "{\n" 2849 " x();\n" 2850 "}", 2851 getLLVMStyleWithColumns(28)); 2852 verifyFormat("#if 1\n" 2853 "int i;"); 2854 verifyFormat("#if 1\n" 2855 "#endif\n" 2856 "#if 1\n" 2857 "#else\n" 2858 "#endif\n"); 2859 verifyFormat("DEBUG({\n" 2860 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2861 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 2862 "});\n" 2863 "#if a\n" 2864 "#else\n" 2865 "#endif"); 2866 2867 verifyIncompleteFormat("void f(\n" 2868 "#if A\n" 2869 ");\n" 2870 "#else\n" 2871 "#endif"); 2872 } 2873 2874 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) { 2875 verifyFormat("#endif\n" 2876 "#if B"); 2877 } 2878 2879 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) { 2880 FormatStyle SingleLine = getLLVMStyle(); 2881 SingleLine.AllowShortIfStatementsOnASingleLine = true; 2882 verifyFormat("#if 0\n" 2883 "#elif 1\n" 2884 "#endif\n" 2885 "void foo() {\n" 2886 " if (test) foo2();\n" 2887 "}", 2888 SingleLine); 2889 } 2890 2891 TEST_F(FormatTest, LayoutBlockInsideParens) { 2892 verifyFormat("functionCall({ int i; });"); 2893 verifyFormat("functionCall({\n" 2894 " int i;\n" 2895 " int j;\n" 2896 "});"); 2897 verifyFormat("functionCall(\n" 2898 " {\n" 2899 " int i;\n" 2900 " int j;\n" 2901 " },\n" 2902 " aaaa, bbbb, cccc);"); 2903 verifyFormat("functionA(functionB({\n" 2904 " int i;\n" 2905 " int j;\n" 2906 " }),\n" 2907 " aaaa, bbbb, cccc);"); 2908 verifyFormat("functionCall(\n" 2909 " {\n" 2910 " int i;\n" 2911 " int j;\n" 2912 " },\n" 2913 " aaaa, bbbb, // comment\n" 2914 " cccc);"); 2915 verifyFormat("functionA(functionB({\n" 2916 " int i;\n" 2917 " int j;\n" 2918 " }),\n" 2919 " aaaa, bbbb, // comment\n" 2920 " cccc);"); 2921 verifyFormat("functionCall(aaaa, bbbb, { int i; });"); 2922 verifyFormat("functionCall(aaaa, bbbb, {\n" 2923 " int i;\n" 2924 " int j;\n" 2925 "});"); 2926 verifyFormat( 2927 "Aaa(\n" // FIXME: There shouldn't be a linebreak here. 2928 " {\n" 2929 " int i; // break\n" 2930 " },\n" 2931 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 2932 " ccccccccccccccccc));"); 2933 verifyFormat("DEBUG({\n" 2934 " if (a)\n" 2935 " f();\n" 2936 "});"); 2937 } 2938 2939 TEST_F(FormatTest, LayoutBlockInsideStatement) { 2940 EXPECT_EQ("SOME_MACRO { int i; }\n" 2941 "int i;", 2942 format(" SOME_MACRO {int i;} int i;")); 2943 } 2944 2945 TEST_F(FormatTest, LayoutNestedBlocks) { 2946 verifyFormat("void AddOsStrings(unsigned bitmask) {\n" 2947 " struct s {\n" 2948 " int i;\n" 2949 " };\n" 2950 " s kBitsToOs[] = {{10}};\n" 2951 " for (int i = 0; i < 10; ++i)\n" 2952 " return;\n" 2953 "}"); 2954 verifyFormat("call(parameter, {\n" 2955 " something();\n" 2956 " // Comment using all columns.\n" 2957 " somethingelse();\n" 2958 "});", 2959 getLLVMStyleWithColumns(40)); 2960 verifyFormat("DEBUG( //\n" 2961 " { f(); }, a);"); 2962 verifyFormat("DEBUG( //\n" 2963 " {\n" 2964 " f(); //\n" 2965 " },\n" 2966 " a);"); 2967 2968 EXPECT_EQ("call(parameter, {\n" 2969 " something();\n" 2970 " // Comment too\n" 2971 " // looooooooooong.\n" 2972 " somethingElse();\n" 2973 "});", 2974 format("call(parameter, {\n" 2975 " something();\n" 2976 " // Comment too looooooooooong.\n" 2977 " somethingElse();\n" 2978 "});", 2979 getLLVMStyleWithColumns(29))); 2980 EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });")); 2981 EXPECT_EQ("DEBUG({ // comment\n" 2982 " int i;\n" 2983 "});", 2984 format("DEBUG({ // comment\n" 2985 "int i;\n" 2986 "});")); 2987 EXPECT_EQ("DEBUG({\n" 2988 " int i;\n" 2989 "\n" 2990 " // comment\n" 2991 " int j;\n" 2992 "});", 2993 format("DEBUG({\n" 2994 " int i;\n" 2995 "\n" 2996 " // comment\n" 2997 " int j;\n" 2998 "});")); 2999 3000 verifyFormat("DEBUG({\n" 3001 " if (a)\n" 3002 " return;\n" 3003 "});"); 3004 verifyGoogleFormat("DEBUG({\n" 3005 " if (a) return;\n" 3006 "});"); 3007 FormatStyle Style = getGoogleStyle(); 3008 Style.ColumnLimit = 45; 3009 verifyFormat("Debug(aaaaa,\n" 3010 " {\n" 3011 " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n" 3012 " },\n" 3013 " a);", 3014 Style); 3015 3016 verifyFormat("SomeFunction({MACRO({ return output; }), b});"); 3017 3018 verifyNoCrash("^{v^{a}}"); 3019 } 3020 3021 TEST_F(FormatTest, FormatNestedBlocksInMacros) { 3022 EXPECT_EQ("#define MACRO() \\\n" 3023 " Debug(aaa, /* force line break */ \\\n" 3024 " { \\\n" 3025 " int i; \\\n" 3026 " int j; \\\n" 3027 " })", 3028 format("#define MACRO() Debug(aaa, /* force line break */ \\\n" 3029 " { int i; int j; })", 3030 getGoogleStyle())); 3031 3032 EXPECT_EQ("#define A \\\n" 3033 " [] { \\\n" 3034 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 3035 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 3036 " }", 3037 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 3038 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 3039 getGoogleStyle())); 3040 } 3041 3042 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { 3043 EXPECT_EQ("{}", format("{}")); 3044 verifyFormat("enum E {};"); 3045 verifyFormat("enum E {}"); 3046 } 3047 3048 TEST_F(FormatTest, FormatBeginBlockEndMacros) { 3049 FormatStyle Style = getLLVMStyle(); 3050 Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$"; 3051 Style.MacroBlockEnd = "^[A-Z_]+_END$"; 3052 verifyFormat("FOO_BEGIN\n" 3053 " FOO_ENTRY\n" 3054 "FOO_END", Style); 3055 verifyFormat("FOO_BEGIN\n" 3056 " NESTED_FOO_BEGIN\n" 3057 " NESTED_FOO_ENTRY\n" 3058 " NESTED_FOO_END\n" 3059 "FOO_END", Style); 3060 verifyFormat("FOO_BEGIN(Foo, Bar)\n" 3061 " int x;\n" 3062 " x = 1;\n" 3063 "FOO_END(Baz)", Style); 3064 } 3065 3066 //===----------------------------------------------------------------------===// 3067 // Line break tests. 3068 //===----------------------------------------------------------------------===// 3069 3070 TEST_F(FormatTest, PreventConfusingIndents) { 3071 verifyFormat( 3072 "void f() {\n" 3073 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" 3074 " parameter, parameter, parameter)),\n" 3075 " SecondLongCall(parameter));\n" 3076 "}"); 3077 verifyFormat( 3078 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3079 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 3080 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3081 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 3082 verifyFormat( 3083 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3084 " [aaaaaaaaaaaaaaaaaaaaaaaa\n" 3085 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 3086 " [aaaaaaaaaaaaaaaaaaaaaaaa]];"); 3087 verifyFormat( 3088 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 3089 " aaaaaaaaaaaaaaaaaaaaaaaa<\n" 3090 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n" 3091 " aaaaaaaaaaaaaaaaaaaaaaaa>;"); 3092 verifyFormat("int a = bbbb && ccc &&\n" 3093 " fffff(\n" 3094 "#define A Just forcing a new line\n" 3095 " ddd);"); 3096 } 3097 3098 TEST_F(FormatTest, LineBreakingInBinaryExpressions) { 3099 verifyFormat( 3100 "bool aaaaaaa =\n" 3101 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n" 3102 " bbbbbbbb();"); 3103 verifyFormat( 3104 "bool aaaaaaa =\n" 3105 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n" 3106 " bbbbbbbb();"); 3107 3108 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 3109 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n" 3110 " ccccccccc == ddddddddddd;"); 3111 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 3112 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n" 3113 " ccccccccc == ddddddddddd;"); 3114 verifyFormat( 3115 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 3116 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n" 3117 " ccccccccc == ddddddddddd;"); 3118 3119 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 3120 " aaaaaa) &&\n" 3121 " bbbbbb && cccccc;"); 3122 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 3123 " aaaaaa) >>\n" 3124 " bbbbbb;"); 3125 verifyFormat("aa = Whitespaces.addUntouchableComment(\n" 3126 " SourceMgr.getSpellingColumnNumber(\n" 3127 " TheLine.Last->FormatTok.Tok.getLocation()) -\n" 3128 " 1);"); 3129 3130 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3131 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n" 3132 " cccccc) {\n}"); 3133 verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3134 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n" 3135 " cccccc) {\n}"); 3136 verifyFormat("b = a &&\n" 3137 " // Comment\n" 3138 " b.c && d;"); 3139 3140 // If the LHS of a comparison is not a binary expression itself, the 3141 // additional linebreak confuses many people. 3142 verifyFormat( 3143 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3144 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n" 3145 "}"); 3146 verifyFormat( 3147 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3148 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3149 "}"); 3150 verifyFormat( 3151 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n" 3152 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3153 "}"); 3154 verifyFormat( 3155 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3156 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n" 3157 "}"); 3158 // Even explicit parentheses stress the precedence enough to make the 3159 // additional break unnecessary. 3160 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3161 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3162 "}"); 3163 // This cases is borderline, but with the indentation it is still readable. 3164 verifyFormat( 3165 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3166 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3167 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 3168 "}", 3169 getLLVMStyleWithColumns(75)); 3170 3171 // If the LHS is a binary expression, we should still use the additional break 3172 // as otherwise the formatting hides the operator precedence. 3173 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3174 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3175 " 5) {\n" 3176 "}"); 3177 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3178 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n" 3179 " 5) {\n" 3180 "}"); 3181 3182 FormatStyle OnePerLine = getLLVMStyle(); 3183 OnePerLine.BinPackParameters = false; 3184 verifyFormat( 3185 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3186 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3187 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}", 3188 OnePerLine); 3189 3190 verifyFormat("int i = someFunction(aaaaaaa, 0)\n" 3191 " .aaa(aaaaaaaaaaaaa) *\n" 3192 " aaaaaaa +\n" 3193 " aaaaaaa;", 3194 getLLVMStyleWithColumns(40)); 3195 } 3196 3197 TEST_F(FormatTest, ExpressionIndentation) { 3198 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3199 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3200 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3201 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3202 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 3203 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n" 3204 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3205 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n" 3206 " ccccccccccccccccccccccccccccccccccccccccc;"); 3207 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3208 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3209 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3210 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3211 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3212 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3213 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3214 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3215 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3216 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3217 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3218 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3219 verifyFormat("if () {\n" 3220 "} else if (aaaaa && bbbbb > // break\n" 3221 " ccccc) {\n" 3222 "}"); 3223 verifyFormat("if () {\n" 3224 "} else if (aaaaa &&\n" 3225 " bbbbb > // break\n" 3226 " ccccc &&\n" 3227 " ddddd) {\n" 3228 "}"); 3229 3230 // Presence of a trailing comment used to change indentation of b. 3231 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n" 3232 " b;\n" 3233 "return aaaaaaaaaaaaaaaaaaa +\n" 3234 " b; //", 3235 getLLVMStyleWithColumns(30)); 3236 } 3237 3238 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) { 3239 // Not sure what the best system is here. Like this, the LHS can be found 3240 // immediately above an operator (everything with the same or a higher 3241 // indent). The RHS is aligned right of the operator and so compasses 3242 // everything until something with the same indent as the operator is found. 3243 // FIXME: Is this a good system? 3244 FormatStyle Style = getLLVMStyle(); 3245 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 3246 verifyFormat( 3247 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3248 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3249 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3250 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3251 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3252 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3253 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3254 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3255 " > ccccccccccccccccccccccccccccccccccccccccc;", 3256 Style); 3257 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3258 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3259 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3260 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3261 Style); 3262 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3263 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3264 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3265 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3266 Style); 3267 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3268 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3269 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3270 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3271 Style); 3272 verifyFormat("if () {\n" 3273 "} else if (aaaaa\n" 3274 " && bbbbb // break\n" 3275 " > ccccc) {\n" 3276 "}", 3277 Style); 3278 verifyFormat("return (a)\n" 3279 " // comment\n" 3280 " + b;", 3281 Style); 3282 verifyFormat( 3283 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3284 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3285 " + cc;", 3286 Style); 3287 3288 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3289 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 3290 Style); 3291 3292 // Forced by comments. 3293 verifyFormat( 3294 "unsigned ContentSize =\n" 3295 " sizeof(int16_t) // DWARF ARange version number\n" 3296 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 3297 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 3298 " + sizeof(int8_t); // Segment Size (in bytes)"); 3299 3300 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 3301 " == boost::fusion::at_c<1>(iiii).second;", 3302 Style); 3303 3304 Style.ColumnLimit = 60; 3305 verifyFormat("zzzzzzzzzz\n" 3306 " = bbbbbbbbbbbbbbbbb\n" 3307 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 3308 Style); 3309 } 3310 3311 TEST_F(FormatTest, EnforcedOperatorWraps) { 3312 // Here we'd like to wrap after the || operators, but a comment is forcing an 3313 // earlier wrap. 3314 verifyFormat("bool x = aaaaa //\n" 3315 " || bbbbb\n" 3316 " //\n" 3317 " || cccc;"); 3318 } 3319 3320 TEST_F(FormatTest, NoOperandAlignment) { 3321 FormatStyle Style = getLLVMStyle(); 3322 Style.AlignOperands = false; 3323 verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n" 3324 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3325 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 3326 Style); 3327 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 3328 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3329 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3330 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3331 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3332 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3333 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3334 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3335 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3336 " > ccccccccccccccccccccccccccccccccccccccccc;", 3337 Style); 3338 3339 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3340 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3341 " + cc;", 3342 Style); 3343 verifyFormat("int a = aa\n" 3344 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3345 " * cccccccccccccccccccccccccccccccccccc;\n", 3346 Style); 3347 3348 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 3349 verifyFormat("return (a > b\n" 3350 " // comment1\n" 3351 " // comment2\n" 3352 " || c);", 3353 Style); 3354 } 3355 3356 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) { 3357 FormatStyle Style = getLLVMStyle(); 3358 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 3359 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 3360 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3361 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 3362 Style); 3363 } 3364 3365 TEST_F(FormatTest, AllowBinPackingInsideArguments) { 3366 FormatStyle Style = getLLVMStyle(); 3367 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 3368 Style.BinPackArguments = false; 3369 Style.ColumnLimit = 40; 3370 verifyFormat("void test() {\n" 3371 " someFunction(\n" 3372 " this + argument + is + quite\n" 3373 " + long + so + it + gets + wrapped\n" 3374 " + but + remains + bin - packed);\n" 3375 "}", 3376 Style); 3377 verifyFormat("void test() {\n" 3378 " someFunction(arg1,\n" 3379 " this + argument + is\n" 3380 " + quite + long + so\n" 3381 " + it + gets + wrapped\n" 3382 " + but + remains + bin\n" 3383 " - packed,\n" 3384 " arg3);\n" 3385 "}", 3386 Style); 3387 verifyFormat("void test() {\n" 3388 " someFunction(\n" 3389 " arg1,\n" 3390 " this + argument + has\n" 3391 " + anotherFunc(nested,\n" 3392 " calls + whose\n" 3393 " + arguments\n" 3394 " + are + also\n" 3395 " + wrapped,\n" 3396 " in + addition)\n" 3397 " + to + being + bin - packed,\n" 3398 " arg3);\n" 3399 "}", 3400 Style); 3401 3402 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 3403 verifyFormat("void test() {\n" 3404 " someFunction(\n" 3405 " arg1,\n" 3406 " this + argument + has +\n" 3407 " anotherFunc(nested,\n" 3408 " calls + whose +\n" 3409 " arguments +\n" 3410 " are + also +\n" 3411 " wrapped,\n" 3412 " in + addition) +\n" 3413 " to + being + bin - packed,\n" 3414 " arg3);\n" 3415 "}", 3416 Style); 3417 } 3418 3419 TEST_F(FormatTest, ConstructorInitializers) { 3420 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 3421 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", 3422 getLLVMStyleWithColumns(45)); 3423 verifyFormat("Constructor()\n" 3424 " : Inttializer(FitsOnTheLine) {}", 3425 getLLVMStyleWithColumns(44)); 3426 verifyFormat("Constructor()\n" 3427 " : Inttializer(FitsOnTheLine) {}", 3428 getLLVMStyleWithColumns(43)); 3429 3430 verifyFormat("template <typename T>\n" 3431 "Constructor() : Initializer(FitsOnTheLine) {}", 3432 getLLVMStyleWithColumns(45)); 3433 3434 verifyFormat( 3435 "SomeClass::Constructor()\n" 3436 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 3437 3438 verifyFormat( 3439 "SomeClass::Constructor()\n" 3440 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3441 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}"); 3442 verifyFormat( 3443 "SomeClass::Constructor()\n" 3444 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3445 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 3446 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3447 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3448 " : aaaaaaaaaa(aaaaaa) {}"); 3449 3450 verifyFormat("Constructor()\n" 3451 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3452 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3453 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3454 " aaaaaaaaaaaaaaaaaaaaaaa() {}"); 3455 3456 verifyFormat("Constructor()\n" 3457 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3458 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 3459 3460 verifyFormat("Constructor(int Parameter = 0)\n" 3461 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 3462 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}"); 3463 verifyFormat("Constructor()\n" 3464 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 3465 "}", 3466 getLLVMStyleWithColumns(60)); 3467 verifyFormat("Constructor()\n" 3468 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3469 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}"); 3470 3471 // Here a line could be saved by splitting the second initializer onto two 3472 // lines, but that is not desirable. 3473 verifyFormat("Constructor()\n" 3474 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 3475 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 3476 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 3477 3478 FormatStyle OnePerLine = getLLVMStyle(); 3479 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 3480 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false; 3481 verifyFormat("SomeClass::Constructor()\n" 3482 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3483 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3484 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3485 OnePerLine); 3486 verifyFormat("SomeClass::Constructor()\n" 3487 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 3488 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3489 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3490 OnePerLine); 3491 verifyFormat("MyClass::MyClass(int var)\n" 3492 " : some_var_(var), // 4 space indent\n" 3493 " some_other_var_(var + 1) { // lined up\n" 3494 "}", 3495 OnePerLine); 3496 verifyFormat("Constructor()\n" 3497 " : aaaaa(aaaaaa),\n" 3498 " aaaaa(aaaaaa),\n" 3499 " aaaaa(aaaaaa),\n" 3500 " aaaaa(aaaaaa),\n" 3501 " aaaaa(aaaaaa) {}", 3502 OnePerLine); 3503 verifyFormat("Constructor()\n" 3504 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 3505 " aaaaaaaaaaaaaaaaaaaaaa) {}", 3506 OnePerLine); 3507 OnePerLine.BinPackParameters = false; 3508 verifyFormat( 3509 "Constructor()\n" 3510 " : aaaaaaaaaaaaaaaaaaaaaaaa(\n" 3511 " aaaaaaaaaaa().aaa(),\n" 3512 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3513 OnePerLine); 3514 OnePerLine.ColumnLimit = 60; 3515 verifyFormat("Constructor()\n" 3516 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 3517 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 3518 OnePerLine); 3519 3520 EXPECT_EQ("Constructor()\n" 3521 " : // Comment forcing unwanted break.\n" 3522 " aaaa(aaaa) {}", 3523 format("Constructor() :\n" 3524 " // Comment forcing unwanted break.\n" 3525 " aaaa(aaaa) {}")); 3526 } 3527 3528 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) { 3529 FormatStyle Style = getLLVMStyle(); 3530 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 3531 3532 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 3533 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}", 3534 getStyleWithColumns(Style, 45)); 3535 verifyFormat("Constructor() :\n" 3536 " Initializer(FitsOnTheLine) {}", 3537 getStyleWithColumns(Style, 44)); 3538 verifyFormat("Constructor() :\n" 3539 " Initializer(FitsOnTheLine) {}", 3540 getStyleWithColumns(Style, 43)); 3541 3542 verifyFormat("template <typename T>\n" 3543 "Constructor() : Initializer(FitsOnTheLine) {}", 3544 getStyleWithColumns(Style, 50)); 3545 3546 verifyFormat( 3547 "SomeClass::Constructor() :\n" 3548 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 3549 Style); 3550 3551 verifyFormat( 3552 "SomeClass::Constructor() :\n" 3553 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3554 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3555 Style); 3556 verifyFormat( 3557 "SomeClass::Constructor() :\n" 3558 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3559 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 3560 Style); 3561 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3562 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 3563 " aaaaaaaaaa(aaaaaa) {}", 3564 Style); 3565 3566 verifyFormat("Constructor() :\n" 3567 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3568 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3569 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3570 " aaaaaaaaaaaaaaaaaaaaaaa() {}", 3571 Style); 3572 3573 verifyFormat("Constructor() :\n" 3574 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3575 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3576 Style); 3577 3578 verifyFormat("Constructor(int Parameter = 0) :\n" 3579 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 3580 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}", 3581 Style); 3582 verifyFormat("Constructor() :\n" 3583 " aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 3584 "}", 3585 getStyleWithColumns(Style, 60)); 3586 verifyFormat("Constructor() :\n" 3587 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3588 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}", 3589 Style); 3590 3591 // Here a line could be saved by splitting the second initializer onto two 3592 // lines, but that is not desirable. 3593 verifyFormat("Constructor() :\n" 3594 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 3595 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 3596 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3597 Style); 3598 3599 FormatStyle OnePerLine = Style; 3600 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 3601 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false; 3602 verifyFormat("SomeClass::Constructor() :\n" 3603 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3604 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3605 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3606 OnePerLine); 3607 verifyFormat("SomeClass::Constructor() :\n" 3608 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 3609 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3610 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3611 OnePerLine); 3612 verifyFormat("MyClass::MyClass(int var) :\n" 3613 " some_var_(var), // 4 space indent\n" 3614 " some_other_var_(var + 1) { // lined up\n" 3615 "}", 3616 OnePerLine); 3617 verifyFormat("Constructor() :\n" 3618 " aaaaa(aaaaaa),\n" 3619 " aaaaa(aaaaaa),\n" 3620 " aaaaa(aaaaaa),\n" 3621 " aaaaa(aaaaaa),\n" 3622 " aaaaa(aaaaaa) {}", 3623 OnePerLine); 3624 verifyFormat("Constructor() :\n" 3625 " aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 3626 " aaaaaaaaaaaaaaaaaaaaaa) {}", 3627 OnePerLine); 3628 OnePerLine.BinPackParameters = false; 3629 verifyFormat( 3630 "Constructor() :\n" 3631 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 3632 " aaaaaaaaaaa().aaa(),\n" 3633 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3634 OnePerLine); 3635 OnePerLine.ColumnLimit = 60; 3636 verifyFormat("Constructor() :\n" 3637 " aaaaaaaaaaaaaaaaaaaa(a),\n" 3638 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 3639 OnePerLine); 3640 3641 EXPECT_EQ("Constructor() :\n" 3642 " // Comment forcing unwanted break.\n" 3643 " aaaa(aaaa) {}", 3644 format("Constructor() :\n" 3645 " // Comment forcing unwanted break.\n" 3646 " aaaa(aaaa) {}", 3647 Style)); 3648 3649 Style.ColumnLimit = 0; 3650 verifyFormat("SomeClass::Constructor() :\n" 3651 " a(a) {}", 3652 Style); 3653 verifyFormat("SomeClass::Constructor() noexcept :\n" 3654 " a(a) {}", 3655 Style); 3656 verifyFormat("SomeClass::Constructor() :\n" 3657 " a(a), b(b), c(c) {}", 3658 Style); 3659 verifyFormat("SomeClass::Constructor() :\n" 3660 " a(a) {\n" 3661 " foo();\n" 3662 " bar();\n" 3663 "}", 3664 Style); 3665 3666 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 3667 verifyFormat("SomeClass::Constructor() :\n" 3668 " a(a), b(b), c(c) {\n" 3669 "}", 3670 Style); 3671 verifyFormat("SomeClass::Constructor() :\n" 3672 " a(a) {\n" 3673 "}", 3674 Style); 3675 3676 Style.ColumnLimit = 80; 3677 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 3678 Style.ConstructorInitializerIndentWidth = 2; 3679 verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", 3680 Style); 3681 verifyFormat("SomeClass::Constructor() :\n" 3682 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3683 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}", 3684 Style); 3685 } 3686 3687 #ifndef EXPENSIVE_CHECKS 3688 // Expensive checks enables libstdc++ checking which includes validating the 3689 // state of ranges used in std::priority_queue - this blows out the 3690 // runtime/scalability of the function and makes this test unacceptably slow. 3691 TEST_F(FormatTest, MemoizationTests) { 3692 // This breaks if the memoization lookup does not take \c Indent and 3693 // \c LastSpace into account. 3694 verifyFormat( 3695 "extern CFRunLoopTimerRef\n" 3696 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n" 3697 " CFTimeInterval interval, CFOptionFlags flags,\n" 3698 " CFIndex order, CFRunLoopTimerCallBack callout,\n" 3699 " CFRunLoopTimerContext *context) {}"); 3700 3701 // Deep nesting somewhat works around our memoization. 3702 verifyFormat( 3703 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3704 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3705 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3706 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3707 " aaaaa())))))))))))))))))))))))))))))))))))))));", 3708 getLLVMStyleWithColumns(65)); 3709 verifyFormat( 3710 "aaaaa(\n" 3711 " aaaaa,\n" 3712 " aaaaa(\n" 3713 " aaaaa,\n" 3714 " aaaaa(\n" 3715 " aaaaa,\n" 3716 " aaaaa(\n" 3717 " aaaaa,\n" 3718 " aaaaa(\n" 3719 " aaaaa,\n" 3720 " aaaaa(\n" 3721 " aaaaa,\n" 3722 " aaaaa(\n" 3723 " aaaaa,\n" 3724 " aaaaa(\n" 3725 " aaaaa,\n" 3726 " aaaaa(\n" 3727 " aaaaa,\n" 3728 " aaaaa(\n" 3729 " aaaaa,\n" 3730 " aaaaa(\n" 3731 " aaaaa,\n" 3732 " aaaaa(\n" 3733 " aaaaa,\n" 3734 " aaaaa))))))))))));", 3735 getLLVMStyleWithColumns(65)); 3736 verifyFormat( 3737 "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" 3738 " a),\n" 3739 " a),\n" 3740 " a),\n" 3741 " a),\n" 3742 " a),\n" 3743 " a),\n" 3744 " a),\n" 3745 " a),\n" 3746 " a),\n" 3747 " a),\n" 3748 " a),\n" 3749 " a),\n" 3750 " a),\n" 3751 " a),\n" 3752 " a),\n" 3753 " a),\n" 3754 " a)", 3755 getLLVMStyleWithColumns(65)); 3756 3757 // This test takes VERY long when memoization is broken. 3758 FormatStyle OnePerLine = getLLVMStyle(); 3759 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 3760 OnePerLine.BinPackParameters = false; 3761 std::string input = "Constructor()\n" 3762 " : aaaa(a,\n"; 3763 for (unsigned i = 0, e = 80; i != e; ++i) { 3764 input += " a,\n"; 3765 } 3766 input += " a) {}"; 3767 verifyFormat(input, OnePerLine); 3768 } 3769 #endif 3770 3771 TEST_F(FormatTest, BreaksAsHighAsPossible) { 3772 verifyFormat( 3773 "void f() {\n" 3774 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" 3775 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" 3776 " f();\n" 3777 "}"); 3778 verifyFormat("if (Intervals[i].getRange().getFirst() <\n" 3779 " Intervals[i - 1].getRange().getLast()) {\n}"); 3780 } 3781 3782 TEST_F(FormatTest, BreaksFunctionDeclarations) { 3783 // Principially, we break function declarations in a certain order: 3784 // 1) break amongst arguments. 3785 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n" 3786 " Cccccccccccccc cccccccccccccc);"); 3787 verifyFormat("template <class TemplateIt>\n" 3788 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n" 3789 " TemplateIt *stop) {}"); 3790 3791 // 2) break after return type. 3792 verifyFormat( 3793 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3794 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);", 3795 getGoogleStyle()); 3796 3797 // 3) break after (. 3798 verifyFormat( 3799 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n" 3800 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);", 3801 getGoogleStyle()); 3802 3803 // 4) break before after nested name specifiers. 3804 verifyFormat( 3805 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3806 "SomeClasssssssssssssssssssssssssssssssssssssss::\n" 3807 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);", 3808 getGoogleStyle()); 3809 3810 // However, there are exceptions, if a sufficient amount of lines can be 3811 // saved. 3812 // FIXME: The precise cut-offs wrt. the number of saved lines might need some 3813 // more adjusting. 3814 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 3815 " Cccccccccccccc cccccccccc,\n" 3816 " Cccccccccccccc cccccccccc,\n" 3817 " Cccccccccccccc cccccccccc,\n" 3818 " Cccccccccccccc cccccccccc);"); 3819 verifyFormat( 3820 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3821 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3822 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3823 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);", 3824 getGoogleStyle()); 3825 verifyFormat( 3826 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 3827 " Cccccccccccccc cccccccccc,\n" 3828 " Cccccccccccccc cccccccccc,\n" 3829 " Cccccccccccccc cccccccccc,\n" 3830 " Cccccccccccccc cccccccccc,\n" 3831 " Cccccccccccccc cccccccccc,\n" 3832 " Cccccccccccccc cccccccccc);"); 3833 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 3834 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3835 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3836 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3837 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); 3838 3839 // Break after multi-line parameters. 3840 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3841 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3842 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3843 " bbbb bbbb);"); 3844 verifyFormat("void SomeLoooooooooooongFunction(\n" 3845 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 3846 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3847 " int bbbbbbbbbbbbb);"); 3848 3849 // Treat overloaded operators like other functions. 3850 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3851 "operator>(const SomeLoooooooooooooooooooooooooogType &other);"); 3852 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3853 "operator>>(const SomeLooooooooooooooooooooooooogType &other);"); 3854 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3855 "operator<<(const SomeLooooooooooooooooooooooooogType &other);"); 3856 verifyGoogleFormat( 3857 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n" 3858 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 3859 verifyGoogleFormat( 3860 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n" 3861 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 3862 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3863 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 3864 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n" 3865 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 3866 verifyGoogleFormat( 3867 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n" 3868 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3869 " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}"); 3870 verifyGoogleFormat( 3871 "template <typename T>\n" 3872 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3873 "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n" 3874 " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);"); 3875 3876 FormatStyle Style = getLLVMStyle(); 3877 Style.PointerAlignment = FormatStyle::PAS_Left; 3878 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3879 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}", 3880 Style); 3881 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n" 3882 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3883 Style); 3884 } 3885 3886 TEST_F(FormatTest, TrailingReturnType) { 3887 verifyFormat("auto foo() -> int;\n"); 3888 verifyFormat("struct S {\n" 3889 " auto bar() const -> int;\n" 3890 "};"); 3891 verifyFormat("template <size_t Order, typename T>\n" 3892 "auto load_img(const std::string &filename)\n" 3893 " -> alias::tensor<Order, T, mem::tag::cpu> {}"); 3894 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n" 3895 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}"); 3896 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}"); 3897 verifyFormat("template <typename T>\n" 3898 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n" 3899 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());"); 3900 3901 // Not trailing return types. 3902 verifyFormat("void f() { auto a = b->c(); }"); 3903 } 3904 3905 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { 3906 // Avoid breaking before trailing 'const' or other trailing annotations, if 3907 // they are not function-like. 3908 FormatStyle Style = getGoogleStyle(); 3909 Style.ColumnLimit = 47; 3910 verifyFormat("void someLongFunction(\n" 3911 " int someLoooooooooooooongParameter) const {\n}", 3912 getLLVMStyleWithColumns(47)); 3913 verifyFormat("LoooooongReturnType\n" 3914 "someLoooooooongFunction() const {}", 3915 getLLVMStyleWithColumns(47)); 3916 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n" 3917 " const {}", 3918 Style); 3919 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3920 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;"); 3921 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3922 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;"); 3923 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3924 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;"); 3925 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n" 3926 " aaaaaaaaaaa aaaaa) const override;"); 3927 verifyGoogleFormat( 3928 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 3929 " const override;"); 3930 3931 // Even if the first parameter has to be wrapped. 3932 verifyFormat("void someLongFunction(\n" 3933 " int someLongParameter) const {}", 3934 getLLVMStyleWithColumns(46)); 3935 verifyFormat("void someLongFunction(\n" 3936 " int someLongParameter) const {}", 3937 Style); 3938 verifyFormat("void someLongFunction(\n" 3939 " int someLongParameter) override {}", 3940 Style); 3941 verifyFormat("void someLongFunction(\n" 3942 " int someLongParameter) OVERRIDE {}", 3943 Style); 3944 verifyFormat("void someLongFunction(\n" 3945 " int someLongParameter) final {}", 3946 Style); 3947 verifyFormat("void someLongFunction(\n" 3948 " int someLongParameter) FINAL {}", 3949 Style); 3950 verifyFormat("void someLongFunction(\n" 3951 " int parameter) const override {}", 3952 Style); 3953 3954 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 3955 verifyFormat("void someLongFunction(\n" 3956 " int someLongParameter) const\n" 3957 "{\n" 3958 "}", 3959 Style); 3960 3961 // Unless these are unknown annotations. 3962 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n" 3963 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3964 " LONG_AND_UGLY_ANNOTATION;"); 3965 3966 // Breaking before function-like trailing annotations is fine to keep them 3967 // close to their arguments. 3968 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3969 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 3970 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 3971 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 3972 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 3973 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}"); 3974 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n" 3975 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);"); 3976 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});"); 3977 3978 verifyFormat( 3979 "void aaaaaaaaaaaaaaaaaa()\n" 3980 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n" 3981 " aaaaaaaaaaaaaaaaaaaaaaaaa));"); 3982 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3983 " __attribute__((unused));"); 3984 verifyGoogleFormat( 3985 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3986 " GUARDED_BY(aaaaaaaaaaaa);"); 3987 verifyGoogleFormat( 3988 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3989 " GUARDED_BY(aaaaaaaaaaaa);"); 3990 verifyGoogleFormat( 3991 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 3992 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 3993 verifyGoogleFormat( 3994 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 3995 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 3996 } 3997 3998 TEST_F(FormatTest, FunctionAnnotations) { 3999 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 4000 "int OldFunction(const string ¶meter) {}"); 4001 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 4002 "string OldFunction(const string ¶meter) {}"); 4003 verifyFormat("template <typename T>\n" 4004 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 4005 "string OldFunction(const string ¶meter) {}"); 4006 4007 // Not function annotations. 4008 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4009 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); 4010 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n" 4011 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}"); 4012 verifyFormat("MACRO(abc).function() // wrap\n" 4013 " << abc;"); 4014 verifyFormat("MACRO(abc)->function() // wrap\n" 4015 " << abc;"); 4016 verifyFormat("MACRO(abc)::function() // wrap\n" 4017 " << abc;"); 4018 } 4019 4020 TEST_F(FormatTest, BreaksDesireably) { 4021 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 4022 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 4023 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); 4024 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4025 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 4026 "}"); 4027 4028 verifyFormat( 4029 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4030 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 4031 4032 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4033 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4034 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4035 4036 verifyFormat( 4037 "aaaaaaaa(aaaaaaaaaaaaa,\n" 4038 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4039 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 4040 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4041 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 4042 4043 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4044 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4045 4046 verifyFormat( 4047 "void f() {\n" 4048 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 4049 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 4050 "}"); 4051 verifyFormat( 4052 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4053 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4054 verifyFormat( 4055 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4056 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4057 verifyFormat( 4058 "aaaaaa(aaa,\n" 4059 " new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4060 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4061 " aaaa);"); 4062 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4063 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4064 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4065 4066 // Indent consistently independent of call expression and unary operator. 4067 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 4068 " dddddddddddddddddddddddddddddd));"); 4069 verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 4070 " dddddddddddddddddddddddddddddd));"); 4071 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n" 4072 " dddddddddddddddddddddddddddddd));"); 4073 4074 // This test case breaks on an incorrect memoization, i.e. an optimization not 4075 // taking into account the StopAt value. 4076 verifyFormat( 4077 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4078 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4079 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4080 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4081 4082 verifyFormat("{\n {\n {\n" 4083 " Annotation.SpaceRequiredBefore =\n" 4084 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" 4085 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" 4086 " }\n }\n}"); 4087 4088 // Break on an outer level if there was a break on an inner level. 4089 EXPECT_EQ("f(g(h(a, // comment\n" 4090 " b, c),\n" 4091 " d, e),\n" 4092 " x, y);", 4093 format("f(g(h(a, // comment\n" 4094 " b, c), d, e), x, y);")); 4095 4096 // Prefer breaking similar line breaks. 4097 verifyFormat( 4098 "const int kTrackingOptions = NSTrackingMouseMoved |\n" 4099 " NSTrackingMouseEnteredAndExited |\n" 4100 " NSTrackingActiveAlways;"); 4101 } 4102 4103 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) { 4104 FormatStyle NoBinPacking = getGoogleStyle(); 4105 NoBinPacking.BinPackParameters = false; 4106 NoBinPacking.BinPackArguments = true; 4107 verifyFormat("void f() {\n" 4108 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n" 4109 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 4110 "}", 4111 NoBinPacking); 4112 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n" 4113 " int aaaaaaaaaaaaaaaaaaaa,\n" 4114 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4115 NoBinPacking); 4116 4117 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 4118 verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4119 " vector<int> bbbbbbbbbbbbbbb);", 4120 NoBinPacking); 4121 // FIXME: This behavior difference is probably not wanted. However, currently 4122 // we cannot distinguish BreakBeforeParameter being set because of the wrapped 4123 // template arguments from BreakBeforeParameter being set because of the 4124 // one-per-line formatting. 4125 verifyFormat( 4126 "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n" 4127 " aaaaaaaaaa> aaaaaaaaaa);", 4128 NoBinPacking); 4129 verifyFormat( 4130 "void fffffffffff(\n" 4131 " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n" 4132 " aaaaaaaaaa);"); 4133 } 4134 4135 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { 4136 FormatStyle NoBinPacking = getGoogleStyle(); 4137 NoBinPacking.BinPackParameters = false; 4138 NoBinPacking.BinPackArguments = false; 4139 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" 4140 " aaaaaaaaaaaaaaaaaaaa,\n" 4141 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);", 4142 NoBinPacking); 4143 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n" 4144 " aaaaaaaaaaaaa,\n" 4145 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));", 4146 NoBinPacking); 4147 verifyFormat( 4148 "aaaaaaaa(aaaaaaaaaaaaa,\n" 4149 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4150 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 4151 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4152 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));", 4153 NoBinPacking); 4154 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 4155 " .aaaaaaaaaaaaaaaaaa();", 4156 NoBinPacking); 4157 verifyFormat("void f() {\n" 4158 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4159 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n" 4160 "}", 4161 NoBinPacking); 4162 4163 verifyFormat( 4164 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4165 " aaaaaaaaaaaa,\n" 4166 " aaaaaaaaaaaa);", 4167 NoBinPacking); 4168 verifyFormat( 4169 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" 4170 " ddddddddddddddddddddddddddddd),\n" 4171 " test);", 4172 NoBinPacking); 4173 4174 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" 4175 " aaaaaaaaaaaaaaaaaaaaaaa,\n" 4176 " aaaaaaaaaaaaaaaaaaaaaaa>\n" 4177 " aaaaaaaaaaaaaaaaaa;", 4178 NoBinPacking); 4179 verifyFormat("a(\"a\"\n" 4180 " \"a\",\n" 4181 " a);"); 4182 4183 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 4184 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" 4185 " aaaaaaaaa,\n" 4186 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4187 NoBinPacking); 4188 verifyFormat( 4189 "void f() {\n" 4190 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 4191 " .aaaaaaa();\n" 4192 "}", 4193 NoBinPacking); 4194 verifyFormat( 4195 "template <class SomeType, class SomeOtherType>\n" 4196 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}", 4197 NoBinPacking); 4198 } 4199 4200 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) { 4201 FormatStyle Style = getLLVMStyleWithColumns(15); 4202 Style.ExperimentalAutoDetectBinPacking = true; 4203 EXPECT_EQ("aaa(aaaa,\n" 4204 " aaaa,\n" 4205 " aaaa);\n" 4206 "aaa(aaaa,\n" 4207 " aaaa,\n" 4208 " aaaa);", 4209 format("aaa(aaaa,\n" // one-per-line 4210 " aaaa,\n" 4211 " aaaa );\n" 4212 "aaa(aaaa, aaaa, aaaa);", // inconclusive 4213 Style)); 4214 EXPECT_EQ("aaa(aaaa, aaaa,\n" 4215 " aaaa);\n" 4216 "aaa(aaaa, aaaa,\n" 4217 " aaaa);", 4218 format("aaa(aaaa, aaaa,\n" // bin-packed 4219 " aaaa );\n" 4220 "aaa(aaaa, aaaa, aaaa);", // inconclusive 4221 Style)); 4222 } 4223 4224 TEST_F(FormatTest, FormatsBuilderPattern) { 4225 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n" 4226 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" 4227 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n" 4228 " .StartsWith(\".init\", ORDER_INIT)\n" 4229 " .StartsWith(\".fini\", ORDER_FINI)\n" 4230 " .StartsWith(\".hash\", ORDER_HASH)\n" 4231 " .Default(ORDER_TEXT);\n"); 4232 4233 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" 4234 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); 4235 verifyFormat( 4236 "aaaaaaa->aaaaaaa\n" 4237 " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4238 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4239 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 4240 verifyFormat( 4241 "aaaaaaa->aaaaaaa\n" 4242 " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4243 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 4244 verifyFormat( 4245 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" 4246 " aaaaaaaaaaaaaa);"); 4247 verifyFormat( 4248 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n" 4249 " aaaaaa->aaaaaaaaaaaa()\n" 4250 " ->aaaaaaaaaaaaaaaa(\n" 4251 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4252 " ->aaaaaaaaaaaaaaaaa();"); 4253 verifyGoogleFormat( 4254 "void f() {\n" 4255 " someo->Add((new util::filetools::Handler(dir))\n" 4256 " ->OnEvent1(NewPermanentCallback(\n" 4257 " this, &HandlerHolderClass::EventHandlerCBA))\n" 4258 " ->OnEvent2(NewPermanentCallback(\n" 4259 " this, &HandlerHolderClass::EventHandlerCBB))\n" 4260 " ->OnEvent3(NewPermanentCallback(\n" 4261 " this, &HandlerHolderClass::EventHandlerCBC))\n" 4262 " ->OnEvent5(NewPermanentCallback(\n" 4263 " this, &HandlerHolderClass::EventHandlerCBD))\n" 4264 " ->OnEvent6(NewPermanentCallback(\n" 4265 " this, &HandlerHolderClass::EventHandlerCBE)));\n" 4266 "}"); 4267 4268 verifyFormat( 4269 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();"); 4270 verifyFormat("aaaaaaaaaaaaaaa()\n" 4271 " .aaaaaaaaaaaaaaa()\n" 4272 " .aaaaaaaaaaaaaaa()\n" 4273 " .aaaaaaaaaaaaaaa()\n" 4274 " .aaaaaaaaaaaaaaa();"); 4275 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4276 " .aaaaaaaaaaaaaaa()\n" 4277 " .aaaaaaaaaaaaaaa()\n" 4278 " .aaaaaaaaaaaaaaa();"); 4279 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4280 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4281 " .aaaaaaaaaaaaaaa();"); 4282 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n" 4283 " ->aaaaaaaaaaaaaae(0)\n" 4284 " ->aaaaaaaaaaaaaaa();"); 4285 4286 // Don't linewrap after very short segments. 4287 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4288 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4289 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4290 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4291 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4292 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4293 verifyFormat("aaa()\n" 4294 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4295 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4296 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4297 4298 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 4299 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4300 " .has<bbbbbbbbbbbbbbbbbbbbb>();"); 4301 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 4302 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 4303 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();"); 4304 4305 // Prefer not to break after empty parentheses. 4306 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n" 4307 " First->LastNewlineOffset);"); 4308 4309 // Prefer not to create "hanging" indents. 4310 verifyFormat( 4311 "return !soooooooooooooome_map\n" 4312 " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4313 " .second;"); 4314 verifyFormat( 4315 "return aaaaaaaaaaaaaaaa\n" 4316 " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n" 4317 " .aaaa(aaaaaaaaaaaaaa);"); 4318 // No hanging indent here. 4319 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n" 4320 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4321 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n" 4322 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4323 verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 4324 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4325 getLLVMStyleWithColumns(60)); 4326 verifyFormat("aaaaaaaaaaaaaaaaaa\n" 4327 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 4328 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4329 getLLVMStyleWithColumns(59)); 4330 verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4331 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4332 " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4333 } 4334 4335 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { 4336 verifyFormat( 4337 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4338 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); 4339 verifyFormat( 4340 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n" 4341 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}"); 4342 4343 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 4344 " ccccccccccccccccccccccccc) {\n}"); 4345 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n" 4346 " ccccccccccccccccccccccccc) {\n}"); 4347 4348 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 4349 " ccccccccccccccccccccccccc) {\n}"); 4350 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n" 4351 " ccccccccccccccccccccccccc) {\n}"); 4352 4353 verifyFormat( 4354 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" 4355 " ccccccccccccccccccccccccc) {\n}"); 4356 verifyFormat( 4357 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n" 4358 " ccccccccccccccccccccccccc) {\n}"); 4359 4360 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" 4361 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" 4362 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" 4363 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 4364 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n" 4365 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n" 4366 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n" 4367 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 4368 4369 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" 4370 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" 4371 " aaaaaaaaaaaaaaa != aa) {\n}"); 4372 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n" 4373 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n" 4374 " aaaaaaaaaaaaaaa != aa) {\n}"); 4375 } 4376 4377 TEST_F(FormatTest, BreaksAfterAssignments) { 4378 verifyFormat( 4379 "unsigned Cost =\n" 4380 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" 4381 " SI->getPointerAddressSpaceee());\n"); 4382 verifyFormat( 4383 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" 4384 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); 4385 4386 verifyFormat( 4387 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n" 4388 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); 4389 verifyFormat("unsigned OriginalStartColumn =\n" 4390 " SourceMgr.getSpellingColumnNumber(\n" 4391 " Current.FormatTok.getStartOfNonWhitespace()) -\n" 4392 " 1;"); 4393 } 4394 4395 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) { 4396 FormatStyle Style = getLLVMStyle(); 4397 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 4398 " bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;", 4399 Style); 4400 4401 Style.PenaltyBreakAssignment = 20; 4402 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 4403 " cccccccccccccccccccccccccc;", 4404 Style); 4405 } 4406 4407 TEST_F(FormatTest, AlignsAfterAssignments) { 4408 verifyFormat( 4409 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4410 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4411 verifyFormat( 4412 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4413 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4414 verifyFormat( 4415 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4416 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4417 verifyFormat( 4418 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4419 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 4420 verifyFormat( 4421 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" 4422 " aaaaaaaaaaaaaaaaaaaaaaaa +\n" 4423 " aaaaaaaaaaaaaaaaaaaaaaaa;"); 4424 } 4425 4426 TEST_F(FormatTest, AlignsAfterReturn) { 4427 verifyFormat( 4428 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4429 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4430 verifyFormat( 4431 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4432 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 4433 verifyFormat( 4434 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 4435 " aaaaaaaaaaaaaaaaaaaaaa();"); 4436 verifyFormat( 4437 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 4438 " aaaaaaaaaaaaaaaaaaaaaa());"); 4439 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4440 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4441 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4442 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n" 4443 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4444 verifyFormat("return\n" 4445 " // true if code is one of a or b.\n" 4446 " code == a || code == b;"); 4447 } 4448 4449 TEST_F(FormatTest, AlignsAfterOpenBracket) { 4450 verifyFormat( 4451 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 4452 " aaaaaaaaa aaaaaaa) {}"); 4453 verifyFormat( 4454 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 4455 " aaaaaaaaaaa aaaaaaaaa);"); 4456 verifyFormat( 4457 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 4458 " aaaaaaaaaaaaaaaaaaaaa));"); 4459 FormatStyle Style = getLLVMStyle(); 4460 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4461 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4462 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}", 4463 Style); 4464 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 4465 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);", 4466 Style); 4467 verifyFormat("SomeLongVariableName->someFunction(\n" 4468 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));", 4469 Style); 4470 verifyFormat( 4471 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 4472 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4473 Style); 4474 verifyFormat( 4475 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 4476 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4477 Style); 4478 verifyFormat( 4479 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 4480 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 4481 Style); 4482 4483 verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n" 4484 " ccccccc(aaaaaaaaaaaaaaaaa, //\n" 4485 " b));", 4486 Style); 4487 4488 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 4489 Style.BinPackArguments = false; 4490 Style.BinPackParameters = false; 4491 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4492 " aaaaaaaaaaa aaaaaaaa,\n" 4493 " aaaaaaaaa aaaaaaa,\n" 4494 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4495 Style); 4496 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 4497 " aaaaaaaaaaa aaaaaaaaa,\n" 4498 " aaaaaaaaaaa aaaaaaaaa,\n" 4499 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4500 Style); 4501 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n" 4502 " aaaaaaaaaaaaaaa,\n" 4503 " aaaaaaaaaaaaaaaaaaaaa,\n" 4504 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 4505 Style); 4506 verifyFormat( 4507 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n" 4508 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 4509 Style); 4510 verifyFormat( 4511 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n" 4512 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 4513 Style); 4514 verifyFormat( 4515 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 4516 " aaaaaaaaaaaaaaaaaaaaa(\n" 4517 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n" 4518 " aaaaaaaaaaaaaaaa);", 4519 Style); 4520 verifyFormat( 4521 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 4522 " aaaaaaaaaaaaaaaaaaaaa(\n" 4523 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n" 4524 " aaaaaaaaaaaaaaaa);", 4525 Style); 4526 } 4527 4528 TEST_F(FormatTest, ParenthesesAndOperandAlignment) { 4529 FormatStyle Style = getLLVMStyleWithColumns(40); 4530 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4531 " bbbbbbbbbbbbbbbbbbbbbb);", 4532 Style); 4533 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 4534 Style.AlignOperands = false; 4535 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4536 " bbbbbbbbbbbbbbbbbbbbbb);", 4537 Style); 4538 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4539 Style.AlignOperands = true; 4540 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4541 " bbbbbbbbbbbbbbbbbbbbbb);", 4542 Style); 4543 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4544 Style.AlignOperands = false; 4545 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4546 " bbbbbbbbbbbbbbbbbbbbbb);", 4547 Style); 4548 } 4549 4550 TEST_F(FormatTest, BreaksConditionalExpressions) { 4551 verifyFormat( 4552 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4553 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4554 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4555 verifyFormat( 4556 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 4557 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4558 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4559 verifyFormat( 4560 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4561 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4562 verifyFormat( 4563 "aaaa(aaaaaaaaa, aaaaaaaaa,\n" 4564 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4565 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4566 verifyFormat( 4567 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" 4568 " : aaaaaaaaaaaaa);"); 4569 verifyFormat( 4570 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4571 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4572 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4573 " aaaaaaaaaaaaa);"); 4574 verifyFormat( 4575 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4576 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4577 " aaaaaaaaaaaaa);"); 4578 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4579 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4580 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4581 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4582 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4583 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4584 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4585 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4586 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4587 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4588 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4589 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4590 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4591 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4592 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4593 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4594 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4595 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4596 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4597 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4598 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 4599 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4600 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4601 " : aaaaaaaaaaaaaaaa;"); 4602 verifyFormat( 4603 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4604 " ? aaaaaaaaaaaaaaa\n" 4605 " : aaaaaaaaaaaaaaa;"); 4606 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 4607 " aaaaaaaaa\n" 4608 " ? b\n" 4609 " : c);"); 4610 verifyFormat("return aaaa == bbbb\n" 4611 " // comment\n" 4612 " ? aaaa\n" 4613 " : bbbb;"); 4614 verifyFormat("unsigned Indent =\n" 4615 " format(TheLine.First,\n" 4616 " IndentForLevel[TheLine.Level] >= 0\n" 4617 " ? IndentForLevel[TheLine.Level]\n" 4618 " : TheLine * 2,\n" 4619 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 4620 getLLVMStyleWithColumns(60)); 4621 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 4622 " ? aaaaaaaaaaaaaaa\n" 4623 " : bbbbbbbbbbbbbbb //\n" 4624 " ? ccccccccccccccc\n" 4625 " : ddddddddddddddd;"); 4626 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 4627 " ? aaaaaaaaaaaaaaa\n" 4628 " : (bbbbbbbbbbbbbbb //\n" 4629 " ? ccccccccccccccc\n" 4630 " : ddddddddddddddd);"); 4631 verifyFormat( 4632 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4633 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4634 " aaaaaaaaaaaaaaaaaaaaa +\n" 4635 " aaaaaaaaaaaaaaaaaaaaa\n" 4636 " : aaaaaaaaaa;"); 4637 verifyFormat( 4638 "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4639 " : aaaaaaaaaaaaaaaaaaaaaa\n" 4640 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4641 4642 FormatStyle NoBinPacking = getLLVMStyle(); 4643 NoBinPacking.BinPackArguments = false; 4644 verifyFormat( 4645 "void f() {\n" 4646 " g(aaa,\n" 4647 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 4648 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4649 " ? aaaaaaaaaaaaaaa\n" 4650 " : aaaaaaaaaaaaaaa);\n" 4651 "}", 4652 NoBinPacking); 4653 verifyFormat( 4654 "void f() {\n" 4655 " g(aaa,\n" 4656 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 4657 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4658 " ?: aaaaaaaaaaaaaaa);\n" 4659 "}", 4660 NoBinPacking); 4661 4662 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n" 4663 " // comment.\n" 4664 " ccccccccccccccccccccccccccccccccccccccc\n" 4665 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4666 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);"); 4667 4668 // Assignments in conditional expressions. Apparently not uncommon :-(. 4669 verifyFormat("return a != b\n" 4670 " // comment\n" 4671 " ? a = b\n" 4672 " : a = b;"); 4673 verifyFormat("return a != b\n" 4674 " // comment\n" 4675 " ? a = a != b\n" 4676 " // comment\n" 4677 " ? a = b\n" 4678 " : a\n" 4679 " : a;\n"); 4680 verifyFormat("return a != b\n" 4681 " // comment\n" 4682 " ? a\n" 4683 " : a = a != b\n" 4684 " // comment\n" 4685 " ? a = b\n" 4686 " : a;"); 4687 } 4688 4689 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) { 4690 FormatStyle Style = getLLVMStyle(); 4691 Style.BreakBeforeTernaryOperators = false; 4692 Style.ColumnLimit = 70; 4693 verifyFormat( 4694 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4695 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4696 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4697 Style); 4698 verifyFormat( 4699 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 4700 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4701 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4702 Style); 4703 verifyFormat( 4704 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4705 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4706 Style); 4707 verifyFormat( 4708 "aaaa(aaaaaaaa, aaaaaaaaaa,\n" 4709 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4710 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4711 Style); 4712 verifyFormat( 4713 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n" 4714 " aaaaaaaaaaaaa);", 4715 Style); 4716 verifyFormat( 4717 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4718 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4719 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4720 " aaaaaaaaaaaaa);", 4721 Style); 4722 verifyFormat( 4723 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4724 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4725 " aaaaaaaaaaaaa);", 4726 Style); 4727 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4728 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4729 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 4730 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4731 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4732 Style); 4733 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4734 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4735 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4736 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 4737 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4738 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4739 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4740 Style); 4741 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4742 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n" 4743 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4744 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4745 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4746 Style); 4747 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4748 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4749 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4750 Style); 4751 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 4752 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4753 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4754 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4755 Style); 4756 verifyFormat( 4757 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4758 " aaaaaaaaaaaaaaa :\n" 4759 " aaaaaaaaaaaaaaa;", 4760 Style); 4761 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 4762 " aaaaaaaaa ?\n" 4763 " b :\n" 4764 " c);", 4765 Style); 4766 verifyFormat("unsigned Indent =\n" 4767 " format(TheLine.First,\n" 4768 " IndentForLevel[TheLine.Level] >= 0 ?\n" 4769 " IndentForLevel[TheLine.Level] :\n" 4770 " TheLine * 2,\n" 4771 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 4772 Style); 4773 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 4774 " aaaaaaaaaaaaaaa :\n" 4775 " bbbbbbbbbbbbbbb ? //\n" 4776 " ccccccccccccccc :\n" 4777 " ddddddddddddddd;", 4778 Style); 4779 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 4780 " aaaaaaaaaaaaaaa :\n" 4781 " (bbbbbbbbbbbbbbb ? //\n" 4782 " ccccccccccccccc :\n" 4783 " ddddddddddddddd);", 4784 Style); 4785 verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4786 " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n" 4787 " ccccccccccccccccccccccccccc;", 4788 Style); 4789 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4790 " aaaaa :\n" 4791 " bbbbbbbbbbbbbbb + cccccccccccccccc;", 4792 Style); 4793 } 4794 4795 TEST_F(FormatTest, DeclarationsOfMultipleVariables) { 4796 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" 4797 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); 4798 verifyFormat("bool a = true, b = false;"); 4799 4800 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" 4801 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" 4802 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" 4803 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); 4804 verifyFormat( 4805 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 4806 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n" 4807 " d = e && f;"); 4808 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n" 4809 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;"); 4810 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 4811 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;"); 4812 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n" 4813 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;"); 4814 4815 FormatStyle Style = getGoogleStyle(); 4816 Style.PointerAlignment = FormatStyle::PAS_Left; 4817 Style.DerivePointerAlignment = false; 4818 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4819 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" 4820 " *b = bbbbbbbbbbbbbbbbbbb;", 4821 Style); 4822 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 4823 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;", 4824 Style); 4825 verifyFormat("vector<int*> a, b;", Style); 4826 verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style); 4827 } 4828 4829 TEST_F(FormatTest, ConditionalExpressionsInBrackets) { 4830 verifyFormat("arr[foo ? bar : baz];"); 4831 verifyFormat("f()[foo ? bar : baz];"); 4832 verifyFormat("(a + b)[foo ? bar : baz];"); 4833 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); 4834 } 4835 4836 TEST_F(FormatTest, AlignsStringLiterals) { 4837 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" 4838 " \"short literal\");"); 4839 verifyFormat( 4840 "looooooooooooooooooooooooongFunction(\n" 4841 " \"short literal\"\n" 4842 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); 4843 verifyFormat("someFunction(\"Always break between multi-line\"\n" 4844 " \" string literals\",\n" 4845 " and, other, parameters);"); 4846 EXPECT_EQ("fun + \"1243\" /* comment */\n" 4847 " \"5678\";", 4848 format("fun + \"1243\" /* comment */\n" 4849 " \"5678\";", 4850 getLLVMStyleWithColumns(28))); 4851 EXPECT_EQ( 4852 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 4853 " \"aaaaaaaaaaaaaaaaaaaaa\"\n" 4854 " \"aaaaaaaaaaaaaaaa\";", 4855 format("aaaaaa =" 4856 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " 4857 "aaaaaaaaaaaaaaaaaaaaa\" " 4858 "\"aaaaaaaaaaaaaaaa\";")); 4859 verifyFormat("a = a + \"a\"\n" 4860 " \"a\"\n" 4861 " \"a\";"); 4862 verifyFormat("f(\"a\", \"b\"\n" 4863 " \"c\");"); 4864 4865 verifyFormat( 4866 "#define LL_FORMAT \"ll\"\n" 4867 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n" 4868 " \"d, ddddddddd: %\" LL_FORMAT \"d\");"); 4869 4870 verifyFormat("#define A(X) \\\n" 4871 " \"aaaaa\" #X \"bbbbbb\" \\\n" 4872 " \"ccccc\"", 4873 getLLVMStyleWithColumns(23)); 4874 verifyFormat("#define A \"def\"\n" 4875 "f(\"abc\" A \"ghi\"\n" 4876 " \"jkl\");"); 4877 4878 verifyFormat("f(L\"a\"\n" 4879 " L\"b\");"); 4880 verifyFormat("#define A(X) \\\n" 4881 " L\"aaaaa\" #X L\"bbbbbb\" \\\n" 4882 " L\"ccccc\"", 4883 getLLVMStyleWithColumns(25)); 4884 4885 verifyFormat("f(@\"a\"\n" 4886 " @\"b\");"); 4887 verifyFormat("NSString s = @\"a\"\n" 4888 " @\"b\"\n" 4889 " @\"c\";"); 4890 verifyFormat("NSString s = @\"a\"\n" 4891 " \"b\"\n" 4892 " \"c\";"); 4893 } 4894 4895 TEST_F(FormatTest, ReturnTypeBreakingStyle) { 4896 FormatStyle Style = getLLVMStyle(); 4897 // No declarations or definitions should be moved to own line. 4898 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None; 4899 verifyFormat("class A {\n" 4900 " int f() { return 1; }\n" 4901 " int g();\n" 4902 "};\n" 4903 "int f() { return 1; }\n" 4904 "int g();\n", 4905 Style); 4906 4907 // All declarations and definitions should have the return type moved to its 4908 // own 4909 // line. 4910 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 4911 verifyFormat("class E {\n" 4912 " int\n" 4913 " f() {\n" 4914 " return 1;\n" 4915 " }\n" 4916 " int\n" 4917 " g();\n" 4918 "};\n" 4919 "int\n" 4920 "f() {\n" 4921 " return 1;\n" 4922 "}\n" 4923 "int\n" 4924 "g();\n", 4925 Style); 4926 4927 // Top-level definitions, and no kinds of declarations should have the 4928 // return type moved to its own line. 4929 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions; 4930 verifyFormat("class B {\n" 4931 " int f() { return 1; }\n" 4932 " int g();\n" 4933 "};\n" 4934 "int\n" 4935 "f() {\n" 4936 " return 1;\n" 4937 "}\n" 4938 "int g();\n", 4939 Style); 4940 4941 // Top-level definitions and declarations should have the return type moved 4942 // to its own line. 4943 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel; 4944 verifyFormat("class C {\n" 4945 " int f() { return 1; }\n" 4946 " int g();\n" 4947 "};\n" 4948 "int\n" 4949 "f() {\n" 4950 " return 1;\n" 4951 "}\n" 4952 "int\n" 4953 "g();\n", 4954 Style); 4955 4956 // All definitions should have the return type moved to its own line, but no 4957 // kinds of declarations. 4958 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 4959 verifyFormat("class D {\n" 4960 " int\n" 4961 " f() {\n" 4962 " return 1;\n" 4963 " }\n" 4964 " int g();\n" 4965 "};\n" 4966 "int\n" 4967 "f() {\n" 4968 " return 1;\n" 4969 "}\n" 4970 "int g();\n", 4971 Style); 4972 verifyFormat("const char *\n" 4973 "f(void) {\n" // Break here. 4974 " return \"\";\n" 4975 "}\n" 4976 "const char *bar(void);\n", // No break here. 4977 Style); 4978 verifyFormat("template <class T>\n" 4979 "T *\n" 4980 "f(T &c) {\n" // Break here. 4981 " return NULL;\n" 4982 "}\n" 4983 "template <class T> T *f(T &c);\n", // No break here. 4984 Style); 4985 verifyFormat("class C {\n" 4986 " int\n" 4987 " operator+() {\n" 4988 " return 1;\n" 4989 " }\n" 4990 " int\n" 4991 " operator()() {\n" 4992 " return 1;\n" 4993 " }\n" 4994 "};\n", 4995 Style); 4996 verifyFormat("void\n" 4997 "A::operator()() {}\n" 4998 "void\n" 4999 "A::operator>>() {}\n" 5000 "void\n" 5001 "A::operator+() {}\n", 5002 Style); 5003 verifyFormat("void *operator new(std::size_t s);", // No break here. 5004 Style); 5005 verifyFormat("void *\n" 5006 "operator new(std::size_t s) {}", 5007 Style); 5008 verifyFormat("void *\n" 5009 "operator delete[](void *ptr) {}", 5010 Style); 5011 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 5012 verifyFormat("const char *\n" 5013 "f(void)\n" // Break here. 5014 "{\n" 5015 " return \"\";\n" 5016 "}\n" 5017 "const char *bar(void);\n", // No break here. 5018 Style); 5019 verifyFormat("template <class T>\n" 5020 "T *\n" // Problem here: no line break 5021 "f(T &c)\n" // Break here. 5022 "{\n" 5023 " return NULL;\n" 5024 "}\n" 5025 "template <class T> T *f(T &c);\n", // No break here. 5026 Style); 5027 } 5028 5029 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) { 5030 FormatStyle NoBreak = getLLVMStyle(); 5031 NoBreak.AlwaysBreakBeforeMultilineStrings = false; 5032 FormatStyle Break = getLLVMStyle(); 5033 Break.AlwaysBreakBeforeMultilineStrings = true; 5034 verifyFormat("aaaa = \"bbbb\"\n" 5035 " \"cccc\";", 5036 NoBreak); 5037 verifyFormat("aaaa =\n" 5038 " \"bbbb\"\n" 5039 " \"cccc\";", 5040 Break); 5041 verifyFormat("aaaa(\"bbbb\"\n" 5042 " \"cccc\");", 5043 NoBreak); 5044 verifyFormat("aaaa(\n" 5045 " \"bbbb\"\n" 5046 " \"cccc\");", 5047 Break); 5048 verifyFormat("aaaa(qqq, \"bbbb\"\n" 5049 " \"cccc\");", 5050 NoBreak); 5051 verifyFormat("aaaa(qqq,\n" 5052 " \"bbbb\"\n" 5053 " \"cccc\");", 5054 Break); 5055 verifyFormat("aaaa(qqq,\n" 5056 " L\"bbbb\"\n" 5057 " L\"cccc\");", 5058 Break); 5059 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n" 5060 " \"bbbb\"));", 5061 Break); 5062 verifyFormat("string s = someFunction(\n" 5063 " \"abc\"\n" 5064 " \"abc\");", 5065 Break); 5066 5067 // As we break before unary operators, breaking right after them is bad. 5068 verifyFormat("string foo = abc ? \"x\"\n" 5069 " \"blah blah blah blah blah blah\"\n" 5070 " : \"y\";", 5071 Break); 5072 5073 // Don't break if there is no column gain. 5074 verifyFormat("f(\"aaaa\"\n" 5075 " \"bbbb\");", 5076 Break); 5077 5078 // Treat literals with escaped newlines like multi-line string literals. 5079 EXPECT_EQ("x = \"a\\\n" 5080 "b\\\n" 5081 "c\";", 5082 format("x = \"a\\\n" 5083 "b\\\n" 5084 "c\";", 5085 NoBreak)); 5086 EXPECT_EQ("xxxx =\n" 5087 " \"a\\\n" 5088 "b\\\n" 5089 "c\";", 5090 format("xxxx = \"a\\\n" 5091 "b\\\n" 5092 "c\";", 5093 Break)); 5094 5095 EXPECT_EQ("NSString *const kString =\n" 5096 " @\"aaaa\"\n" 5097 " @\"bbbb\";", 5098 format("NSString *const kString = @\"aaaa\"\n" 5099 "@\"bbbb\";", 5100 Break)); 5101 5102 Break.ColumnLimit = 0; 5103 verifyFormat("const char *hello = \"hello llvm\";", Break); 5104 } 5105 5106 TEST_F(FormatTest, AlignsPipes) { 5107 verifyFormat( 5108 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5109 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5110 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5111 verifyFormat( 5112 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 5113 " << aaaaaaaaaaaaaaaaaaaa;"); 5114 verifyFormat( 5115 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5116 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5117 verifyFormat( 5118 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 5119 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5120 verifyFormat( 5121 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 5122 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 5123 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 5124 verifyFormat( 5125 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5126 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5127 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5128 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5129 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5130 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5131 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 5132 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n" 5133 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);"); 5134 verifyFormat( 5135 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5136 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5137 verifyFormat( 5138 "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n" 5139 " aaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5140 5141 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n" 5142 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();"); 5143 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5144 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5145 " aaaaaaaaaaaaaaaaaaaaa)\n" 5146 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5147 verifyFormat("LOG_IF(aaa == //\n" 5148 " bbb)\n" 5149 " << a << b;"); 5150 5151 // But sometimes, breaking before the first "<<" is desirable. 5152 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5153 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);"); 5154 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n" 5155 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5156 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5157 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n" 5158 " << BEF << IsTemplate << Description << E->getType();"); 5159 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5160 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5161 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5162 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5163 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5164 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5165 " << aaa;"); 5166 5167 verifyFormat( 5168 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5169 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5170 5171 // Incomplete string literal. 5172 EXPECT_EQ("llvm::errs() << \"\n" 5173 " << a;", 5174 format("llvm::errs() << \"\n<<a;")); 5175 5176 verifyFormat("void f() {\n" 5177 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n" 5178 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n" 5179 "}"); 5180 5181 // Handle 'endl'. 5182 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n" 5183 " << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 5184 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 5185 5186 // Handle '\n'. 5187 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n" 5188 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 5189 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n" 5190 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';"); 5191 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n" 5192 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";"); 5193 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 5194 } 5195 5196 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) { 5197 verifyFormat("return out << \"somepacket = {\\n\"\n" 5198 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" 5199 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" 5200 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" 5201 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" 5202 " << \"}\";"); 5203 5204 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 5205 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 5206 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;"); 5207 verifyFormat( 5208 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" 5209 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" 5210 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" 5211 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" 5212 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); 5213 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" 5214 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 5215 verifyFormat( 5216 "void f() {\n" 5217 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n" 5218 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 5219 "}"); 5220 5221 // Breaking before the first "<<" is generally not desirable. 5222 verifyFormat( 5223 "llvm::errs()\n" 5224 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5225 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5226 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5227 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5228 getLLVMStyleWithColumns(70)); 5229 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5230 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5231 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5232 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5233 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5234 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5235 getLLVMStyleWithColumns(70)); 5236 5237 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 5238 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 5239 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;"); 5240 verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 5241 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 5242 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);"); 5243 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n" 5244 " (aaaa + aaaa);", 5245 getLLVMStyleWithColumns(40)); 5246 verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n" 5247 " (aaaaaaa + aaaaa));", 5248 getLLVMStyleWithColumns(40)); 5249 verifyFormat( 5250 "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n" 5251 " SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n" 5252 " bbbbbbbbbbbbbbbbbbbbbbb);"); 5253 } 5254 5255 TEST_F(FormatTest, UnderstandsEquals) { 5256 verifyFormat( 5257 "aaaaaaaaaaaaaaaaa =\n" 5258 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5259 verifyFormat( 5260 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5261 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 5262 verifyFormat( 5263 "if (a) {\n" 5264 " f();\n" 5265 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5266 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 5267 "}"); 5268 5269 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5270 " 100000000 + 10000000) {\n}"); 5271 } 5272 5273 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 5274 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 5275 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 5276 5277 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 5278 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 5279 5280 verifyFormat( 5281 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 5282 " Parameter2);"); 5283 5284 verifyFormat( 5285 "ShortObject->shortFunction(\n" 5286 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 5287 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 5288 5289 verifyFormat("loooooooooooooongFunction(\n" 5290 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 5291 5292 verifyFormat( 5293 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 5294 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 5295 5296 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 5297 " .WillRepeatedly(Return(SomeValue));"); 5298 verifyFormat("void f() {\n" 5299 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 5300 " .Times(2)\n" 5301 " .WillRepeatedly(Return(SomeValue));\n" 5302 "}"); 5303 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n" 5304 " ccccccccccccccccccccccc);"); 5305 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5306 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5307 " .aaaaa(aaaaa),\n" 5308 " aaaaaaaaaaaaaaaaaaaaa);"); 5309 verifyFormat("void f() {\n" 5310 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5311 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n" 5312 "}"); 5313 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5314 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5315 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5316 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5317 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 5318 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5319 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5320 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5321 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n" 5322 "}"); 5323 5324 // Here, it is not necessary to wrap at "." or "->". 5325 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 5326 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 5327 verifyFormat( 5328 "aaaaaaaaaaa->aaaaaaaaa(\n" 5329 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5330 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 5331 5332 verifyFormat( 5333 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5334 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); 5335 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n" 5336 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 5337 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" 5338 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 5339 5340 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5341 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5342 " .a();"); 5343 5344 FormatStyle NoBinPacking = getLLVMStyle(); 5345 NoBinPacking.BinPackParameters = false; 5346 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 5347 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 5348 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" 5349 " aaaaaaaaaaaaaaaaaaa,\n" 5350 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5351 NoBinPacking); 5352 5353 // If there is a subsequent call, change to hanging indentation. 5354 verifyFormat( 5355 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5356 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n" 5357 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5358 verifyFormat( 5359 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5360 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));"); 5361 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5362 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5363 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5364 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5365 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5366 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 5367 } 5368 5369 TEST_F(FormatTest, WrapsTemplateDeclarations) { 5370 verifyFormat("template <typename T>\n" 5371 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 5372 verifyFormat("template <typename T>\n" 5373 "// T should be one of {A, B}.\n" 5374 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 5375 verifyFormat( 5376 "template <typename T>\n" 5377 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); 5378 verifyFormat("template <typename T>\n" 5379 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 5380 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 5381 verifyFormat( 5382 "template <typename T>\n" 5383 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 5384 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 5385 verifyFormat( 5386 "template <typename T>\n" 5387 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 5388 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 5389 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5390 verifyFormat("template <typename T>\n" 5391 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5392 " int aaaaaaaaaaaaaaaaaaaaaa);"); 5393 verifyFormat( 5394 "template <typename T1, typename T2 = char, typename T3 = char,\n" 5395 " typename T4 = char>\n" 5396 "void f();"); 5397 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n" 5398 " template <typename> class cccccccccccccccccccccc,\n" 5399 " typename ddddddddddddd>\n" 5400 "class C {};"); 5401 verifyFormat( 5402 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 5403 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5404 5405 verifyFormat("void f() {\n" 5406 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 5407 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n" 5408 "}"); 5409 5410 verifyFormat("template <typename T> class C {};"); 5411 verifyFormat("template <typename T> void f();"); 5412 verifyFormat("template <typename T> void f() {}"); 5413 verifyFormat( 5414 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 5415 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5416 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n" 5417 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 5418 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5419 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n" 5420 " bbbbbbbbbbbbbbbbbbbbbbbb);", 5421 getLLVMStyleWithColumns(72)); 5422 EXPECT_EQ("static_cast<A< //\n" 5423 " B> *>(\n" 5424 "\n" 5425 ");", 5426 format("static_cast<A<//\n" 5427 " B>*>(\n" 5428 "\n" 5429 " );")); 5430 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5431 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);"); 5432 5433 FormatStyle AlwaysBreak = getLLVMStyle(); 5434 AlwaysBreak.AlwaysBreakTemplateDeclarations = true; 5435 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak); 5436 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak); 5437 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak); 5438 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5439 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 5440 " ccccccccccccccccccccccccccccccccccccccccccccccc);"); 5441 verifyFormat("template <template <typename> class Fooooooo,\n" 5442 " template <typename> class Baaaaaaar>\n" 5443 "struct C {};", 5444 AlwaysBreak); 5445 verifyFormat("template <typename T> // T can be A, B or C.\n" 5446 "struct C {};", 5447 AlwaysBreak); 5448 verifyFormat("template <enum E> class A {\n" 5449 "public:\n" 5450 " E *f();\n" 5451 "};"); 5452 } 5453 5454 TEST_F(FormatTest, WrapsTemplateParameters) { 5455 FormatStyle Style = getLLVMStyle(); 5456 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 5457 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 5458 verifyFormat( 5459 "template <typename... a> struct q {};\n" 5460 "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 5461 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 5462 " y;", 5463 Style); 5464 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 5465 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 5466 verifyFormat( 5467 "template <typename... a> struct r {};\n" 5468 "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 5469 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 5470 " y;", 5471 Style); 5472 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 5473 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 5474 verifyFormat( 5475 "template <typename... a> struct s {};\n" 5476 "extern s<\n" 5477 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 5478 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n" 5479 " y;", 5480 Style); 5481 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 5482 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 5483 verifyFormat( 5484 "template <typename... a> struct t {};\n" 5485 "extern t<\n" 5486 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 5487 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n" 5488 " y;", 5489 Style); 5490 } 5491 5492 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 5493 verifyFormat( 5494 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5495 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5496 verifyFormat( 5497 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5498 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5499 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 5500 5501 // FIXME: Should we have the extra indent after the second break? 5502 verifyFormat( 5503 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5504 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5505 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5506 5507 verifyFormat( 5508 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 5509 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 5510 5511 // Breaking at nested name specifiers is generally not desirable. 5512 verifyFormat( 5513 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5514 " aaaaaaaaaaaaaaaaaaaaaaa);"); 5515 5516 verifyFormat( 5517 "aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n" 5518 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5519 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5520 " aaaaaaaaaaaaaaaaaaaaa);", 5521 getLLVMStyleWithColumns(74)); 5522 5523 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5524 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5525 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5526 } 5527 5528 TEST_F(FormatTest, UnderstandsTemplateParameters) { 5529 verifyFormat("A<int> a;"); 5530 verifyFormat("A<A<A<int>>> a;"); 5531 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 5532 verifyFormat("bool x = a < 1 || 2 > a;"); 5533 verifyFormat("bool x = 5 < f<int>();"); 5534 verifyFormat("bool x = f<int>() > 5;"); 5535 verifyFormat("bool x = 5 < a<int>::x;"); 5536 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 5537 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 5538 5539 verifyGoogleFormat("A<A<int>> a;"); 5540 verifyGoogleFormat("A<A<A<int>>> a;"); 5541 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 5542 verifyGoogleFormat("A<A<int> > a;"); 5543 verifyGoogleFormat("A<A<A<int> > > a;"); 5544 verifyGoogleFormat("A<A<A<A<int> > > > a;"); 5545 verifyGoogleFormat("A<::A<int>> a;"); 5546 verifyGoogleFormat("A<::A> a;"); 5547 verifyGoogleFormat("A< ::A> a;"); 5548 verifyGoogleFormat("A< ::A<int> > a;"); 5549 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); 5550 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); 5551 EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle())); 5552 EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle())); 5553 EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };", 5554 format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle())); 5555 5556 verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp)); 5557 5558 verifyFormat("test >> a >> b;"); 5559 verifyFormat("test << a >> b;"); 5560 5561 verifyFormat("f<int>();"); 5562 verifyFormat("template <typename T> void f() {}"); 5563 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;"); 5564 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : " 5565 "sizeof(char)>::type>;"); 5566 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};"); 5567 verifyFormat("f(a.operator()<A>());"); 5568 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5569 " .template operator()<A>());", 5570 getLLVMStyleWithColumns(35)); 5571 5572 // Not template parameters. 5573 verifyFormat("return a < b && c > d;"); 5574 verifyFormat("void f() {\n" 5575 " while (a < b && c > d) {\n" 5576 " }\n" 5577 "}"); 5578 verifyFormat("template <typename... Types>\n" 5579 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}"); 5580 5581 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5582 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);", 5583 getLLVMStyleWithColumns(60)); 5584 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");"); 5585 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}"); 5586 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <"); 5587 } 5588 5589 TEST_F(FormatTest, BitshiftOperatorWidth) { 5590 EXPECT_EQ("int a = 1 << 2; /* foo\n" 5591 " bar */", 5592 format("int a=1<<2; /* foo\n" 5593 " bar */")); 5594 5595 EXPECT_EQ("int b = 256 >> 1; /* foo\n" 5596 " bar */", 5597 format("int b =256>>1 ; /* foo\n" 5598 " bar */")); 5599 } 5600 5601 TEST_F(FormatTest, UnderstandsBinaryOperators) { 5602 verifyFormat("COMPARE(a, ==, b);"); 5603 verifyFormat("auto s = sizeof...(Ts) - 1;"); 5604 } 5605 5606 TEST_F(FormatTest, UnderstandsPointersToMembers) { 5607 verifyFormat("int A::*x;"); 5608 verifyFormat("int (S::*func)(void *);"); 5609 verifyFormat("void f() { int (S::*func)(void *); }"); 5610 verifyFormat("typedef bool *(Class::*Member)() const;"); 5611 verifyFormat("void f() {\n" 5612 " (a->*f)();\n" 5613 " a->*x;\n" 5614 " (a.*f)();\n" 5615 " ((*a).*f)();\n" 5616 " a.*x;\n" 5617 "}"); 5618 verifyFormat("void f() {\n" 5619 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 5620 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n" 5621 "}"); 5622 verifyFormat( 5623 "(aaaaaaaaaa->*bbbbbbb)(\n" 5624 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 5625 FormatStyle Style = getLLVMStyle(); 5626 Style.PointerAlignment = FormatStyle::PAS_Left; 5627 verifyFormat("typedef bool* (Class::*Member)() const;", Style); 5628 } 5629 5630 TEST_F(FormatTest, UnderstandsUnaryOperators) { 5631 verifyFormat("int a = -2;"); 5632 verifyFormat("f(-1, -2, -3);"); 5633 verifyFormat("a[-1] = 5;"); 5634 verifyFormat("int a = 5 + -2;"); 5635 verifyFormat("if (i == -1) {\n}"); 5636 verifyFormat("if (i != -1) {\n}"); 5637 verifyFormat("if (i > -1) {\n}"); 5638 verifyFormat("if (i < -1) {\n}"); 5639 verifyFormat("++(a->f());"); 5640 verifyFormat("--(a->f());"); 5641 verifyFormat("(a->f())++;"); 5642 verifyFormat("a[42]++;"); 5643 verifyFormat("if (!(a->f())) {\n}"); 5644 5645 verifyFormat("a-- > b;"); 5646 verifyFormat("b ? -a : c;"); 5647 verifyFormat("n * sizeof char16;"); 5648 verifyFormat("n * alignof char16;", getGoogleStyle()); 5649 verifyFormat("sizeof(char);"); 5650 verifyFormat("alignof(char);", getGoogleStyle()); 5651 5652 verifyFormat("return -1;"); 5653 verifyFormat("switch (a) {\n" 5654 "case -1:\n" 5655 " break;\n" 5656 "}"); 5657 verifyFormat("#define X -1"); 5658 verifyFormat("#define X -kConstant"); 5659 5660 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};"); 5661 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};"); 5662 5663 verifyFormat("int a = /* confusing comment */ -1;"); 5664 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 5665 verifyFormat("int a = i /* confusing comment */++;"); 5666 } 5667 5668 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) { 5669 verifyFormat("if (!aaaaaaaaaa( // break\n" 5670 " aaaaa)) {\n" 5671 "}"); 5672 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n" 5673 " aaaaa));"); 5674 verifyFormat("*aaa = aaaaaaa( // break\n" 5675 " bbbbbb);"); 5676 } 5677 5678 TEST_F(FormatTest, UnderstandsOverloadedOperators) { 5679 verifyFormat("bool operator<();"); 5680 verifyFormat("bool operator>();"); 5681 verifyFormat("bool operator=();"); 5682 verifyFormat("bool operator==();"); 5683 verifyFormat("bool operator!=();"); 5684 verifyFormat("int operator+();"); 5685 verifyFormat("int operator++();"); 5686 verifyFormat("int operator++(int) volatile noexcept;"); 5687 verifyFormat("bool operator,();"); 5688 verifyFormat("bool operator();"); 5689 verifyFormat("bool operator()();"); 5690 verifyFormat("bool operator[]();"); 5691 verifyFormat("operator bool();"); 5692 verifyFormat("operator int();"); 5693 verifyFormat("operator void *();"); 5694 verifyFormat("operator SomeType<int>();"); 5695 verifyFormat("operator SomeType<int, int>();"); 5696 verifyFormat("operator SomeType<SomeType<int>>();"); 5697 verifyFormat("void *operator new(std::size_t size);"); 5698 verifyFormat("void *operator new[](std::size_t size);"); 5699 verifyFormat("void operator delete(void *ptr);"); 5700 verifyFormat("void operator delete[](void *ptr);"); 5701 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n" 5702 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);"); 5703 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n" 5704 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;"); 5705 5706 verifyFormat( 5707 "ostream &operator<<(ostream &OutputStream,\n" 5708 " SomeReallyLongType WithSomeReallyLongValue);"); 5709 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n" 5710 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n" 5711 " return left.group < right.group;\n" 5712 "}"); 5713 verifyFormat("SomeType &operator=(const SomeType &S);"); 5714 verifyFormat("f.template operator()<int>();"); 5715 5716 verifyGoogleFormat("operator void*();"); 5717 verifyGoogleFormat("operator SomeType<SomeType<int>>();"); 5718 verifyGoogleFormat("operator ::A();"); 5719 5720 verifyFormat("using A::operator+;"); 5721 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n" 5722 "int i;"); 5723 } 5724 5725 TEST_F(FormatTest, UnderstandsFunctionRefQualification) { 5726 verifyFormat("Deleted &operator=(const Deleted &) & = default;"); 5727 verifyFormat("Deleted &operator=(const Deleted &) && = delete;"); 5728 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;"); 5729 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;"); 5730 verifyFormat("Deleted &operator=(const Deleted &) &;"); 5731 verifyFormat("Deleted &operator=(const Deleted &) &&;"); 5732 verifyFormat("SomeType MemberFunction(const Deleted &) &;"); 5733 verifyFormat("SomeType MemberFunction(const Deleted &) &&;"); 5734 verifyFormat("SomeType MemberFunction(const Deleted &) && {}"); 5735 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}"); 5736 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}"); 5737 verifyFormat("void Fn(T const &) const &;"); 5738 verifyFormat("void Fn(T const volatile &&) const volatile &&;"); 5739 verifyFormat("template <typename T>\n" 5740 "void F(T) && = delete;", 5741 getGoogleStyle()); 5742 5743 FormatStyle AlignLeft = getLLVMStyle(); 5744 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 5745 verifyFormat("void A::b() && {}", AlignLeft); 5746 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft); 5747 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;", 5748 AlignLeft); 5749 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft); 5750 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft); 5751 verifyFormat("auto Function(T t) & -> void {}", AlignLeft); 5752 verifyFormat("auto Function(T... t) & -> void {}", AlignLeft); 5753 verifyFormat("auto Function(T) & -> void {}", AlignLeft); 5754 verifyFormat("auto Function(T) & -> void;", AlignLeft); 5755 verifyFormat("void Fn(T const&) const&;", AlignLeft); 5756 verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft); 5757 5758 FormatStyle Spaces = getLLVMStyle(); 5759 Spaces.SpacesInCStyleCastParentheses = true; 5760 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces); 5761 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces); 5762 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces); 5763 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces); 5764 5765 Spaces.SpacesInCStyleCastParentheses = false; 5766 Spaces.SpacesInParentheses = true; 5767 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces); 5768 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces); 5769 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces); 5770 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces); 5771 } 5772 5773 TEST_F(FormatTest, UnderstandsNewAndDelete) { 5774 verifyFormat("void f() {\n" 5775 " A *a = new A;\n" 5776 " A *a = new (placement) A;\n" 5777 " delete a;\n" 5778 " delete (A *)a;\n" 5779 "}"); 5780 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 5781 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 5782 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5783 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 5784 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 5785 verifyFormat("delete[] h->p;"); 5786 } 5787 5788 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 5789 verifyFormat("int *f(int *a) {}"); 5790 verifyFormat("int main(int argc, char **argv) {}"); 5791 verifyFormat("Test::Test(int b) : a(b * b) {}"); 5792 verifyIndependentOfContext("f(a, *a);"); 5793 verifyFormat("void g() { f(*a); }"); 5794 verifyIndependentOfContext("int a = b * 10;"); 5795 verifyIndependentOfContext("int a = 10 * b;"); 5796 verifyIndependentOfContext("int a = b * c;"); 5797 verifyIndependentOfContext("int a += b * c;"); 5798 verifyIndependentOfContext("int a -= b * c;"); 5799 verifyIndependentOfContext("int a *= b * c;"); 5800 verifyIndependentOfContext("int a /= b * c;"); 5801 verifyIndependentOfContext("int a = *b;"); 5802 verifyIndependentOfContext("int a = *b * c;"); 5803 verifyIndependentOfContext("int a = b * *c;"); 5804 verifyIndependentOfContext("int a = b * (10);"); 5805 verifyIndependentOfContext("S << b * (10);"); 5806 verifyIndependentOfContext("return 10 * b;"); 5807 verifyIndependentOfContext("return *b * *c;"); 5808 verifyIndependentOfContext("return a & ~b;"); 5809 verifyIndependentOfContext("f(b ? *c : *d);"); 5810 verifyIndependentOfContext("int a = b ? *c : *d;"); 5811 verifyIndependentOfContext("*b = a;"); 5812 verifyIndependentOfContext("a * ~b;"); 5813 verifyIndependentOfContext("a * !b;"); 5814 verifyIndependentOfContext("a * +b;"); 5815 verifyIndependentOfContext("a * -b;"); 5816 verifyIndependentOfContext("a * ++b;"); 5817 verifyIndependentOfContext("a * --b;"); 5818 verifyIndependentOfContext("a[4] * b;"); 5819 verifyIndependentOfContext("a[a * a] = 1;"); 5820 verifyIndependentOfContext("f() * b;"); 5821 verifyIndependentOfContext("a * [self dostuff];"); 5822 verifyIndependentOfContext("int x = a * (a + b);"); 5823 verifyIndependentOfContext("(a *)(a + b);"); 5824 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;"); 5825 verifyIndependentOfContext("int *pa = (int *)&a;"); 5826 verifyIndependentOfContext("return sizeof(int **);"); 5827 verifyIndependentOfContext("return sizeof(int ******);"); 5828 verifyIndependentOfContext("return (int **&)a;"); 5829 verifyIndependentOfContext("f((*PointerToArray)[10]);"); 5830 verifyFormat("void f(Type (*parameter)[10]) {}"); 5831 verifyFormat("void f(Type (¶meter)[10]) {}"); 5832 verifyGoogleFormat("return sizeof(int**);"); 5833 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); 5834 verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); 5835 verifyFormat("auto a = [](int **&, int ***) {};"); 5836 verifyFormat("auto PointerBinding = [](const char *S) {};"); 5837 verifyFormat("typedef typeof(int(int, int)) *MyFunc;"); 5838 verifyFormat("[](const decltype(*a) &value) {}"); 5839 verifyFormat("decltype(a * b) F();"); 5840 verifyFormat("#define MACRO() [](A *a) { return 1; }"); 5841 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}"); 5842 verifyIndependentOfContext("typedef void (*f)(int *a);"); 5843 verifyIndependentOfContext("int i{a * b};"); 5844 verifyIndependentOfContext("aaa && aaa->f();"); 5845 verifyIndependentOfContext("int x = ~*p;"); 5846 verifyFormat("Constructor() : a(a), area(width * height) {}"); 5847 verifyFormat("Constructor() : a(a), area(a, width * height) {}"); 5848 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}"); 5849 verifyFormat("void f() { f(a, c * d); }"); 5850 verifyFormat("void f() { f(new a(), c * d); }"); 5851 verifyFormat("void f(const MyOverride &override);"); 5852 verifyFormat("void f(const MyFinal &final);"); 5853 verifyIndependentOfContext("bool a = f() && override.f();"); 5854 verifyIndependentOfContext("bool a = f() && final.f();"); 5855 5856 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 5857 5858 verifyIndependentOfContext("A<int *> a;"); 5859 verifyIndependentOfContext("A<int **> a;"); 5860 verifyIndependentOfContext("A<int *, int *> a;"); 5861 verifyIndependentOfContext("A<int *[]> a;"); 5862 verifyIndependentOfContext( 5863 "const char *const p = reinterpret_cast<const char *const>(q);"); 5864 verifyIndependentOfContext("A<int **, int **> a;"); 5865 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); 5866 verifyFormat("for (char **a = b; *a; ++a) {\n}"); 5867 verifyFormat("for (; a && b;) {\n}"); 5868 verifyFormat("bool foo = true && [] { return false; }();"); 5869 5870 verifyFormat( 5871 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5872 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5873 5874 verifyGoogleFormat("int const* a = &b;"); 5875 verifyGoogleFormat("**outparam = 1;"); 5876 verifyGoogleFormat("*outparam = a * b;"); 5877 verifyGoogleFormat("int main(int argc, char** argv) {}"); 5878 verifyGoogleFormat("A<int*> a;"); 5879 verifyGoogleFormat("A<int**> a;"); 5880 verifyGoogleFormat("A<int*, int*> a;"); 5881 verifyGoogleFormat("A<int**, int**> a;"); 5882 verifyGoogleFormat("f(b ? *c : *d);"); 5883 verifyGoogleFormat("int a = b ? *c : *d;"); 5884 verifyGoogleFormat("Type* t = **x;"); 5885 verifyGoogleFormat("Type* t = *++*x;"); 5886 verifyGoogleFormat("*++*x;"); 5887 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 5888 verifyGoogleFormat("Type* t = x++ * y;"); 5889 verifyGoogleFormat( 5890 "const char* const p = reinterpret_cast<const char* const>(q);"); 5891 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);"); 5892 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);"); 5893 verifyGoogleFormat("template <typename T>\n" 5894 "void f(int i = 0, SomeType** temps = NULL);"); 5895 5896 FormatStyle Left = getLLVMStyle(); 5897 Left.PointerAlignment = FormatStyle::PAS_Left; 5898 verifyFormat("x = *a(x) = *a(y);", Left); 5899 verifyFormat("for (;; *a = b) {\n}", Left); 5900 verifyFormat("return *this += 1;", Left); 5901 verifyFormat("throw *x;", Left); 5902 verifyFormat("delete *x;", Left); 5903 verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left); 5904 verifyFormat("[](const decltype(*a)* ptr) {}", Left); 5905 verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left); 5906 5907 verifyIndependentOfContext("a = *(x + y);"); 5908 verifyIndependentOfContext("a = &(x + y);"); 5909 verifyIndependentOfContext("*(x + y).call();"); 5910 verifyIndependentOfContext("&(x + y)->call();"); 5911 verifyFormat("void f() { &(*I).first; }"); 5912 5913 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 5914 verifyFormat( 5915 "int *MyValues = {\n" 5916 " *A, // Operator detection might be confused by the '{'\n" 5917 " *BB // Operator detection might be confused by previous comment\n" 5918 "};"); 5919 5920 verifyIndependentOfContext("if (int *a = &b)"); 5921 verifyIndependentOfContext("if (int &a = *b)"); 5922 verifyIndependentOfContext("if (a & b[i])"); 5923 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 5924 verifyIndependentOfContext("if (*b[i])"); 5925 verifyIndependentOfContext("if (int *a = (&b))"); 5926 verifyIndependentOfContext("while (int *a = &b)"); 5927 verifyIndependentOfContext("size = sizeof *a;"); 5928 verifyIndependentOfContext("if (a && (b = c))"); 5929 verifyFormat("void f() {\n" 5930 " for (const int &v : Values) {\n" 5931 " }\n" 5932 "}"); 5933 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); 5934 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); 5935 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}"); 5936 5937 verifyFormat("#define A (!a * b)"); 5938 verifyFormat("#define MACRO \\\n" 5939 " int *i = a * b; \\\n" 5940 " void f(a *b);", 5941 getLLVMStyleWithColumns(19)); 5942 5943 verifyIndependentOfContext("A = new SomeType *[Length];"); 5944 verifyIndependentOfContext("A = new SomeType *[Length]();"); 5945 verifyIndependentOfContext("T **t = new T *;"); 5946 verifyIndependentOfContext("T **t = new T *();"); 5947 verifyGoogleFormat("A = new SomeType*[Length]();"); 5948 verifyGoogleFormat("A = new SomeType*[Length];"); 5949 verifyGoogleFormat("T** t = new T*;"); 5950 verifyGoogleFormat("T** t = new T*();"); 5951 5952 verifyFormat("STATIC_ASSERT((a & b) == 0);"); 5953 verifyFormat("STATIC_ASSERT(0 == (a & b));"); 5954 verifyFormat("template <bool a, bool b> " 5955 "typename t::if<x && y>::type f() {}"); 5956 verifyFormat("template <int *y> f() {}"); 5957 verifyFormat("vector<int *> v;"); 5958 verifyFormat("vector<int *const> v;"); 5959 verifyFormat("vector<int *const **const *> v;"); 5960 verifyFormat("vector<int *volatile> v;"); 5961 verifyFormat("vector<a * b> v;"); 5962 verifyFormat("foo<b && false>();"); 5963 verifyFormat("foo<b & 1>();"); 5964 verifyFormat("decltype(*::std::declval<const T &>()) void F();"); 5965 verifyFormat( 5966 "template <class T, class = typename std::enable_if<\n" 5967 " std::is_integral<T>::value &&\n" 5968 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n" 5969 "void F();", 5970 getLLVMStyleWithColumns(70)); 5971 verifyFormat( 5972 "template <class T,\n" 5973 " class = typename std::enable_if<\n" 5974 " std::is_integral<T>::value &&\n" 5975 " (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n" 5976 " class U>\n" 5977 "void F();", 5978 getLLVMStyleWithColumns(70)); 5979 verifyFormat( 5980 "template <class T,\n" 5981 " class = typename ::std::enable_if<\n" 5982 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n" 5983 "void F();", 5984 getGoogleStyleWithColumns(68)); 5985 5986 verifyIndependentOfContext("MACRO(int *i);"); 5987 verifyIndependentOfContext("MACRO(auto *a);"); 5988 verifyIndependentOfContext("MACRO(const A *a);"); 5989 verifyIndependentOfContext("MACRO(A *const a);"); 5990 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');"); 5991 verifyFormat("void f() { f(float{1}, a * a); }"); 5992 // FIXME: Is there a way to make this work? 5993 // verifyIndependentOfContext("MACRO(A *a);"); 5994 5995 verifyFormat("DatumHandle const *operator->() const { return input_; }"); 5996 verifyFormat("return options != nullptr && operator==(*options);"); 5997 5998 EXPECT_EQ("#define OP(x) \\\n" 5999 " ostream &operator<<(ostream &s, const A &a) { \\\n" 6000 " return s << a.DebugString(); \\\n" 6001 " }", 6002 format("#define OP(x) \\\n" 6003 " ostream &operator<<(ostream &s, const A &a) { \\\n" 6004 " return s << a.DebugString(); \\\n" 6005 " }", 6006 getLLVMStyleWithColumns(50))); 6007 6008 // FIXME: We cannot handle this case yet; we might be able to figure out that 6009 // foo<x> d > v; doesn't make sense. 6010 verifyFormat("foo<a<b && c> d> v;"); 6011 6012 FormatStyle PointerMiddle = getLLVMStyle(); 6013 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 6014 verifyFormat("delete *x;", PointerMiddle); 6015 verifyFormat("int * x;", PointerMiddle); 6016 verifyFormat("template <int * y> f() {}", PointerMiddle); 6017 verifyFormat("int * f(int * a) {}", PointerMiddle); 6018 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle); 6019 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle); 6020 verifyFormat("A<int *> a;", PointerMiddle); 6021 verifyFormat("A<int **> a;", PointerMiddle); 6022 verifyFormat("A<int *, int *> a;", PointerMiddle); 6023 verifyFormat("A<int * []> a;", PointerMiddle); 6024 verifyFormat("A = new SomeType *[Length]();", PointerMiddle); 6025 verifyFormat("A = new SomeType *[Length];", PointerMiddle); 6026 verifyFormat("T ** t = new T *;", PointerMiddle); 6027 6028 // Member function reference qualifiers aren't binary operators. 6029 verifyFormat("string // break\n" 6030 "operator()() & {}"); 6031 verifyFormat("string // break\n" 6032 "operator()() && {}"); 6033 verifyGoogleFormat("template <typename T>\n" 6034 "auto x() & -> int {}"); 6035 } 6036 6037 TEST_F(FormatTest, UnderstandsAttributes) { 6038 verifyFormat("SomeType s __attribute__((unused)) (InitValue);"); 6039 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n" 6040 "aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 6041 FormatStyle AfterType = getLLVMStyle(); 6042 AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 6043 verifyFormat("__attribute__((nodebug)) void\n" 6044 "foo() {}\n", 6045 AfterType); 6046 } 6047 6048 TEST_F(FormatTest, UnderstandsEllipsis) { 6049 verifyFormat("int printf(const char *fmt, ...);"); 6050 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); 6051 verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}"); 6052 6053 FormatStyle PointersLeft = getLLVMStyle(); 6054 PointersLeft.PointerAlignment = FormatStyle::PAS_Left; 6055 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft); 6056 } 6057 6058 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { 6059 EXPECT_EQ("int *a;\n" 6060 "int *a;\n" 6061 "int *a;", 6062 format("int *a;\n" 6063 "int* a;\n" 6064 "int *a;", 6065 getGoogleStyle())); 6066 EXPECT_EQ("int* a;\n" 6067 "int* a;\n" 6068 "int* a;", 6069 format("int* a;\n" 6070 "int* a;\n" 6071 "int *a;", 6072 getGoogleStyle())); 6073 EXPECT_EQ("int *a;\n" 6074 "int *a;\n" 6075 "int *a;", 6076 format("int *a;\n" 6077 "int * a;\n" 6078 "int * a;", 6079 getGoogleStyle())); 6080 EXPECT_EQ("auto x = [] {\n" 6081 " int *a;\n" 6082 " int *a;\n" 6083 " int *a;\n" 6084 "};", 6085 format("auto x=[]{int *a;\n" 6086 "int * a;\n" 6087 "int * a;};", 6088 getGoogleStyle())); 6089 } 6090 6091 TEST_F(FormatTest, UnderstandsRvalueReferences) { 6092 verifyFormat("int f(int &&a) {}"); 6093 verifyFormat("int f(int a, char &&b) {}"); 6094 verifyFormat("void f() { int &&a = b; }"); 6095 verifyGoogleFormat("int f(int a, char&& b) {}"); 6096 verifyGoogleFormat("void f() { int&& a = b; }"); 6097 6098 verifyIndependentOfContext("A<int &&> a;"); 6099 verifyIndependentOfContext("A<int &&, int &&> a;"); 6100 verifyGoogleFormat("A<int&&> a;"); 6101 verifyGoogleFormat("A<int&&, int&&> a;"); 6102 6103 // Not rvalue references: 6104 verifyFormat("template <bool B, bool C> class A {\n" 6105 " static_assert(B && C, \"Something is wrong\");\n" 6106 "};"); 6107 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))"); 6108 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))"); 6109 verifyFormat("#define A(a, b) (a && b)"); 6110 } 6111 6112 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 6113 verifyFormat("void f() {\n" 6114 " x[aaaaaaaaa -\n" 6115 " b] = 23;\n" 6116 "}", 6117 getLLVMStyleWithColumns(15)); 6118 } 6119 6120 TEST_F(FormatTest, FormatsCasts) { 6121 verifyFormat("Type *A = static_cast<Type *>(P);"); 6122 verifyFormat("Type *A = (Type *)P;"); 6123 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 6124 verifyFormat("int a = (int)(2.0f);"); 6125 verifyFormat("int a = (int)2.0f;"); 6126 verifyFormat("x[(int32)y];"); 6127 verifyFormat("x = (int32)y;"); 6128 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)"); 6129 verifyFormat("int a = (int)*b;"); 6130 verifyFormat("int a = (int)2.0f;"); 6131 verifyFormat("int a = (int)~0;"); 6132 verifyFormat("int a = (int)++a;"); 6133 verifyFormat("int a = (int)sizeof(int);"); 6134 verifyFormat("int a = (int)+2;"); 6135 verifyFormat("my_int a = (my_int)2.0f;"); 6136 verifyFormat("my_int a = (my_int)sizeof(int);"); 6137 verifyFormat("return (my_int)aaa;"); 6138 verifyFormat("#define x ((int)-1)"); 6139 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1"); 6140 verifyFormat("#define p(q) ((int *)&q)"); 6141 verifyFormat("fn(a)(b) + 1;"); 6142 6143 verifyFormat("void f() { my_int a = (my_int)*b; }"); 6144 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }"); 6145 verifyFormat("my_int a = (my_int)~0;"); 6146 verifyFormat("my_int a = (my_int)++a;"); 6147 verifyFormat("my_int a = (my_int)-2;"); 6148 verifyFormat("my_int a = (my_int)1;"); 6149 verifyFormat("my_int a = (my_int *)1;"); 6150 verifyFormat("my_int a = (const my_int)-1;"); 6151 verifyFormat("my_int a = (const my_int *)-1;"); 6152 verifyFormat("my_int a = (my_int)(my_int)-1;"); 6153 verifyFormat("my_int a = (ns::my_int)-2;"); 6154 verifyFormat("case (my_int)ONE:"); 6155 verifyFormat("auto x = (X)this;"); 6156 6157 // FIXME: single value wrapped with paren will be treated as cast. 6158 verifyFormat("void f(int i = (kValue)*kMask) {}"); 6159 6160 verifyFormat("{ (void)F; }"); 6161 6162 // Don't break after a cast's 6163 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 6164 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n" 6165 " bbbbbbbbbbbbbbbbbbbbbb);"); 6166 6167 // These are not casts. 6168 verifyFormat("void f(int *) {}"); 6169 verifyFormat("f(foo)->b;"); 6170 verifyFormat("f(foo).b;"); 6171 verifyFormat("f(foo)(b);"); 6172 verifyFormat("f(foo)[b];"); 6173 verifyFormat("[](foo) { return 4; }(bar);"); 6174 verifyFormat("(*funptr)(foo)[4];"); 6175 verifyFormat("funptrs[4](foo)[4];"); 6176 verifyFormat("void f(int *);"); 6177 verifyFormat("void f(int *) = 0;"); 6178 verifyFormat("void f(SmallVector<int>) {}"); 6179 verifyFormat("void f(SmallVector<int>);"); 6180 verifyFormat("void f(SmallVector<int>) = 0;"); 6181 verifyFormat("void f(int i = (kA * kB) & kMask) {}"); 6182 verifyFormat("int a = sizeof(int) * b;"); 6183 verifyFormat("int a = alignof(int) * b;", getGoogleStyle()); 6184 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;"); 6185 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");"); 6186 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;"); 6187 6188 // These are not casts, but at some point were confused with casts. 6189 verifyFormat("virtual void foo(int *) override;"); 6190 verifyFormat("virtual void foo(char &) const;"); 6191 verifyFormat("virtual void foo(int *a, char *) const;"); 6192 verifyFormat("int a = sizeof(int *) + b;"); 6193 verifyFormat("int a = alignof(int *) + b;", getGoogleStyle()); 6194 verifyFormat("bool b = f(g<int>) && c;"); 6195 verifyFormat("typedef void (*f)(int i) func;"); 6196 6197 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n" 6198 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 6199 // FIXME: The indentation here is not ideal. 6200 verifyFormat( 6201 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6202 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n" 6203 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];"); 6204 } 6205 6206 TEST_F(FormatTest, FormatsFunctionTypes) { 6207 verifyFormat("A<bool()> a;"); 6208 verifyFormat("A<SomeType()> a;"); 6209 verifyFormat("A<void (*)(int, std::string)> a;"); 6210 verifyFormat("A<void *(int)>;"); 6211 verifyFormat("void *(*a)(int *, SomeType *);"); 6212 verifyFormat("int (*func)(void *);"); 6213 verifyFormat("void f() { int (*func)(void *); }"); 6214 verifyFormat("template <class CallbackClass>\n" 6215 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);"); 6216 6217 verifyGoogleFormat("A<void*(int*, SomeType*)>;"); 6218 verifyGoogleFormat("void* (*a)(int);"); 6219 verifyGoogleFormat( 6220 "template <class CallbackClass>\n" 6221 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);"); 6222 6223 // Other constructs can look somewhat like function types: 6224 verifyFormat("A<sizeof(*x)> a;"); 6225 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)"); 6226 verifyFormat("some_var = function(*some_pointer_var)[0];"); 6227 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }"); 6228 verifyFormat("int x = f(&h)();"); 6229 verifyFormat("returnsFunction(¶m1, ¶m2)(param);"); 6230 verifyFormat("std::function<\n" 6231 " LooooooooooongTemplatedType<\n" 6232 " SomeType>*(\n" 6233 " LooooooooooooooooongType type)>\n" 6234 " function;", 6235 getGoogleStyleWithColumns(40)); 6236 } 6237 6238 TEST_F(FormatTest, FormatsPointersToArrayTypes) { 6239 verifyFormat("A (*foo_)[6];"); 6240 verifyFormat("vector<int> (*foo_)[6];"); 6241 } 6242 6243 TEST_F(FormatTest, BreaksLongVariableDeclarations) { 6244 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6245 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6246 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n" 6247 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6248 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6249 " *LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6250 6251 // Different ways of ()-initializiation. 6252 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6253 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);"); 6254 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6255 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);"); 6256 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6257 " LoooooooooooooooooooooooooooooooooooooooongVariable({});"); 6258 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6259 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);"); 6260 6261 // Lambdas should not confuse the variable declaration heuristic. 6262 verifyFormat("LooooooooooooooooongType\n" 6263 " variable(nullptr, [](A *a) {});", 6264 getLLVMStyleWithColumns(40)); 6265 } 6266 6267 TEST_F(FormatTest, BreaksLongDeclarations) { 6268 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n" 6269 " AnotherNameForTheLongType;"); 6270 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n" 6271 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 6272 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6273 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 6274 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n" 6275 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 6276 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6277 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6278 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n" 6279 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6280 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 6281 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6282 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 6283 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6284 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6285 "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);"); 6286 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6287 "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}"); 6288 FormatStyle Indented = getLLVMStyle(); 6289 Indented.IndentWrappedFunctionNames = true; 6290 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6291 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();", 6292 Indented); 6293 verifyFormat( 6294 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6295 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6296 Indented); 6297 verifyFormat( 6298 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 6299 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6300 Indented); 6301 verifyFormat( 6302 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 6303 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6304 Indented); 6305 6306 // FIXME: Without the comment, this breaks after "(". 6307 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n" 6308 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();", 6309 getGoogleStyle()); 6310 6311 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 6312 " int LoooooooooooooooooooongParam2) {}"); 6313 verifyFormat( 6314 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" 6315 " SourceLocation L, IdentifierIn *II,\n" 6316 " Type *T) {}"); 6317 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" 6318 "ReallyReaaallyLongFunctionName(\n" 6319 " const std::string &SomeParameter,\n" 6320 " const SomeType<string, SomeOtherTemplateParameter>\n" 6321 " &ReallyReallyLongParameterName,\n" 6322 " const SomeType<string, SomeOtherTemplateParameter>\n" 6323 " &AnotherLongParameterName) {}"); 6324 verifyFormat("template <typename A>\n" 6325 "SomeLoooooooooooooooooooooongType<\n" 6326 " typename some_namespace::SomeOtherType<A>::Type>\n" 6327 "Function() {}"); 6328 6329 verifyGoogleFormat( 6330 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" 6331 " aaaaaaaaaaaaaaaaaaaaaaa;"); 6332 verifyGoogleFormat( 6333 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" 6334 " SourceLocation L) {}"); 6335 verifyGoogleFormat( 6336 "some_namespace::LongReturnType\n" 6337 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" 6338 " int first_long_parameter, int second_parameter) {}"); 6339 6340 verifyGoogleFormat("template <typename T>\n" 6341 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" 6342 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); 6343 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6344 " int aaaaaaaaaaaaaaaaaaaaaaa);"); 6345 6346 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 6347 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6348 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6349 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6350 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 6351 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 6352 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6353 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 6354 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n" 6355 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6356 6357 verifyFormat("template <typename T> // Templates on own line.\n" 6358 "static int // Some comment.\n" 6359 "MyFunction(int a);", 6360 getLLVMStyle()); 6361 } 6362 6363 TEST_F(FormatTest, FormatsArrays) { 6364 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6365 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;"); 6366 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n" 6367 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;"); 6368 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n" 6369 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}"); 6370 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6371 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 6372 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6373 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;"); 6374 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6375 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6376 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 6377 verifyFormat( 6378 "llvm::outs() << \"aaaaaaaaaaaa: \"\n" 6379 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6380 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 6381 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n" 6382 " .aaaaaaaaaaaaaaaaaaaaaa();"); 6383 6384 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n" 6385 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];"); 6386 verifyFormat( 6387 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n" 6388 " .aaaaaaa[0]\n" 6389 " .aaaaaaaaaaaaaaaaaaaaaa();"); 6390 verifyFormat("a[::b::c];"); 6391 6392 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10)); 6393 6394 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0); 6395 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit); 6396 } 6397 6398 TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 6399 verifyFormat("(a)->b();"); 6400 verifyFormat("--a;"); 6401 } 6402 6403 TEST_F(FormatTest, HandlesIncludeDirectives) { 6404 verifyFormat("#include <string>\n" 6405 "#include <a/b/c.h>\n" 6406 "#include \"a/b/string\"\n" 6407 "#include \"string.h\"\n" 6408 "#include \"string.h\"\n" 6409 "#include <a-a>\n" 6410 "#include < path with space >\n" 6411 "#include_next <test.h>" 6412 "#include \"abc.h\" // this is included for ABC\n" 6413 "#include \"some long include\" // with a comment\n" 6414 "#include \"some very long include path\"\n" 6415 "#include <some/very/long/include/path>\n", 6416 getLLVMStyleWithColumns(35)); 6417 EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\"")); 6418 EXPECT_EQ("#include <a>", format("#include<a>")); 6419 6420 verifyFormat("#import <string>"); 6421 verifyFormat("#import <a/b/c.h>"); 6422 verifyFormat("#import \"a/b/string\""); 6423 verifyFormat("#import \"string.h\""); 6424 verifyFormat("#import \"string.h\""); 6425 verifyFormat("#if __has_include(<strstream>)\n" 6426 "#include <strstream>\n" 6427 "#endif"); 6428 6429 verifyFormat("#define MY_IMPORT <a/b>"); 6430 6431 verifyFormat("#if __has_include(<a/b>)"); 6432 verifyFormat("#if __has_include_next(<a/b>)"); 6433 verifyFormat("#define F __has_include(<a/b>)"); 6434 verifyFormat("#define F __has_include_next(<a/b>)"); 6435 6436 // Protocol buffer definition or missing "#". 6437 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";", 6438 getLLVMStyleWithColumns(30)); 6439 6440 FormatStyle Style = getLLVMStyle(); 6441 Style.AlwaysBreakBeforeMultilineStrings = true; 6442 Style.ColumnLimit = 0; 6443 verifyFormat("#import \"abc.h\"", Style); 6444 6445 // But 'import' might also be a regular C++ namespace. 6446 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6447 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6448 } 6449 6450 //===----------------------------------------------------------------------===// 6451 // Error recovery tests. 6452 //===----------------------------------------------------------------------===// 6453 6454 TEST_F(FormatTest, IncompleteParameterLists) { 6455 FormatStyle NoBinPacking = getLLVMStyle(); 6456 NoBinPacking.BinPackParameters = false; 6457 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" 6458 " double *min_x,\n" 6459 " double *max_x,\n" 6460 " double *min_y,\n" 6461 " double *max_y,\n" 6462 " double *min_z,\n" 6463 " double *max_z, ) {}", 6464 NoBinPacking); 6465 } 6466 6467 TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 6468 verifyFormat("void f() { return; }\n42"); 6469 verifyFormat("void f() {\n" 6470 " if (0)\n" 6471 " return;\n" 6472 "}\n" 6473 "42"); 6474 verifyFormat("void f() { return }\n42"); 6475 verifyFormat("void f() {\n" 6476 " if (0)\n" 6477 " return\n" 6478 "}\n" 6479 "42"); 6480 } 6481 6482 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 6483 EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); 6484 EXPECT_EQ("void f() {\n" 6485 " if (a)\n" 6486 " return\n" 6487 "}", 6488 format("void f ( ) { if ( a ) return }")); 6489 EXPECT_EQ("namespace N {\n" 6490 "void f()\n" 6491 "}", 6492 format("namespace N { void f() }")); 6493 EXPECT_EQ("namespace N {\n" 6494 "void f() {}\n" 6495 "void g()\n" 6496 "} // namespace N", 6497 format("namespace N { void f( ) { } void g( ) }")); 6498 } 6499 6500 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 6501 verifyFormat("int aaaaaaaa =\n" 6502 " // Overlylongcomment\n" 6503 " b;", 6504 getLLVMStyleWithColumns(20)); 6505 verifyFormat("function(\n" 6506 " ShortArgument,\n" 6507 " LoooooooooooongArgument);\n", 6508 getLLVMStyleWithColumns(20)); 6509 } 6510 6511 TEST_F(FormatTest, IncorrectAccessSpecifier) { 6512 verifyFormat("public:"); 6513 verifyFormat("class A {\n" 6514 "public\n" 6515 " void f() {}\n" 6516 "};"); 6517 verifyFormat("public\n" 6518 "int qwerty;"); 6519 verifyFormat("public\n" 6520 "B {}"); 6521 verifyFormat("public\n" 6522 "{}"); 6523 verifyFormat("public\n" 6524 "B { int x; }"); 6525 } 6526 6527 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 6528 verifyFormat("{"); 6529 verifyFormat("#})"); 6530 verifyNoCrash("(/**/[:!] ?[)."); 6531 } 6532 6533 TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 6534 verifyFormat("do {\n}"); 6535 verifyFormat("do {\n}\n" 6536 "f();"); 6537 verifyFormat("do {\n}\n" 6538 "wheeee(fun);"); 6539 verifyFormat("do {\n" 6540 " f();\n" 6541 "}"); 6542 } 6543 6544 TEST_F(FormatTest, IncorrectCodeMissingParens) { 6545 verifyFormat("if {\n foo;\n foo();\n}"); 6546 verifyFormat("switch {\n foo;\n foo();\n}"); 6547 verifyIncompleteFormat("for {\n foo;\n foo();\n}"); 6548 verifyFormat("while {\n foo;\n foo();\n}"); 6549 verifyFormat("do {\n foo;\n foo();\n} while;"); 6550 } 6551 6552 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 6553 verifyIncompleteFormat("namespace {\n" 6554 "class Foo { Foo (\n" 6555 "};\n" 6556 "} // namespace"); 6557 } 6558 6559 TEST_F(FormatTest, IncorrectCodeErrorDetection) { 6560 EXPECT_EQ("{\n {}\n", format("{\n{\n}\n")); 6561 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 6562 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 6563 EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n")); 6564 6565 EXPECT_EQ("{\n" 6566 " {\n" 6567 " breakme(\n" 6568 " qwe);\n" 6569 " }\n", 6570 format("{\n" 6571 " {\n" 6572 " breakme(qwe);\n" 6573 "}\n", 6574 getLLVMStyleWithColumns(10))); 6575 } 6576 6577 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 6578 verifyFormat("int x = {\n" 6579 " avariable,\n" 6580 " b(alongervariable)};", 6581 getLLVMStyleWithColumns(25)); 6582 } 6583 6584 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 6585 verifyFormat("return (a)(b){1, 2, 3};"); 6586 } 6587 6588 TEST_F(FormatTest, LayoutCxx11BraceInitializers) { 6589 verifyFormat("vector<int> x{1, 2, 3, 4};"); 6590 verifyFormat("vector<int> x{\n" 6591 " 1,\n" 6592 " 2,\n" 6593 " 3,\n" 6594 " 4,\n" 6595 "};"); 6596 verifyFormat("vector<T> x{{}, {}, {}, {}};"); 6597 verifyFormat("f({1, 2});"); 6598 verifyFormat("auto v = Foo{-1};"); 6599 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});"); 6600 verifyFormat("Class::Class : member{1, 2, 3} {}"); 6601 verifyFormat("new vector<int>{1, 2, 3};"); 6602 verifyFormat("new int[3]{1, 2, 3};"); 6603 verifyFormat("new int{1};"); 6604 verifyFormat("return {arg1, arg2};"); 6605 verifyFormat("return {arg1, SomeType{parameter}};"); 6606 verifyFormat("int count = set<int>{f(), g(), h()}.size();"); 6607 verifyFormat("new T{arg1, arg2};"); 6608 verifyFormat("f(MyMap[{composite, key}]);"); 6609 verifyFormat("class Class {\n" 6610 " T member = {arg1, arg2};\n" 6611 "};"); 6612 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};"); 6613 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 6614 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 6615 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");"); 6616 verifyFormat("int a = std::is_integral<int>{} + 0;"); 6617 6618 verifyFormat("int foo(int i) { return fo1{}(i); }"); 6619 verifyFormat("int foo(int i) { return fo1{}(i); }"); 6620 verifyFormat("auto i = decltype(x){};"); 6621 verifyFormat("std::vector<int> v = {1, 0 /* comment */};"); 6622 verifyFormat("Node n{1, Node{1000}, //\n" 6623 " 2};"); 6624 verifyFormat("Aaaa aaaaaaa{\n" 6625 " {\n" 6626 " aaaa,\n" 6627 " },\n" 6628 "};"); 6629 verifyFormat("class C : public D {\n" 6630 " SomeClass SC{2};\n" 6631 "};"); 6632 verifyFormat("class C : public A {\n" 6633 " class D : public B {\n" 6634 " void f() { int i{2}; }\n" 6635 " };\n" 6636 "};"); 6637 verifyFormat("#define A {a, a},"); 6638 6639 // Binpacking only if there is no trailing comma 6640 verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n" 6641 " cccccccccc, dddddddddd};", 6642 getLLVMStyleWithColumns(50)); 6643 verifyFormat("const Aaaaaa aaaaa = {\n" 6644 " aaaaaaaaaaa,\n" 6645 " bbbbbbbbbbb,\n" 6646 " ccccccccccc,\n" 6647 " ddddddddddd,\n" 6648 "};", getLLVMStyleWithColumns(50)); 6649 6650 // Cases where distinguising braced lists and blocks is hard. 6651 verifyFormat("vector<int> v{12} GUARDED_BY(mutex);"); 6652 verifyFormat("void f() {\n" 6653 " return; // comment\n" 6654 "}\n" 6655 "SomeType t;"); 6656 verifyFormat("void f() {\n" 6657 " if (a) {\n" 6658 " f();\n" 6659 " }\n" 6660 "}\n" 6661 "SomeType t;"); 6662 6663 // In combination with BinPackArguments = false. 6664 FormatStyle NoBinPacking = getLLVMStyle(); 6665 NoBinPacking.BinPackArguments = false; 6666 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n" 6667 " bbbbb,\n" 6668 " ccccc,\n" 6669 " ddddd,\n" 6670 " eeeee,\n" 6671 " ffffff,\n" 6672 " ggggg,\n" 6673 " hhhhhh,\n" 6674 " iiiiii,\n" 6675 " jjjjjj,\n" 6676 " kkkkkk};", 6677 NoBinPacking); 6678 verifyFormat("const Aaaaaa aaaaa = {\n" 6679 " aaaaa,\n" 6680 " bbbbb,\n" 6681 " ccccc,\n" 6682 " ddddd,\n" 6683 " eeeee,\n" 6684 " ffffff,\n" 6685 " ggggg,\n" 6686 " hhhhhh,\n" 6687 " iiiiii,\n" 6688 " jjjjjj,\n" 6689 " kkkkkk,\n" 6690 "};", 6691 NoBinPacking); 6692 verifyFormat( 6693 "const Aaaaaa aaaaa = {\n" 6694 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n" 6695 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n" 6696 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n" 6697 "};", 6698 NoBinPacking); 6699 6700 // FIXME: The alignment of these trailing comments might be bad. Then again, 6701 // this might be utterly useless in real code. 6702 verifyFormat("Constructor::Constructor()\n" 6703 " : some_value{ //\n" 6704 " aaaaaaa, //\n" 6705 " bbbbbbb} {}"); 6706 6707 // In braced lists, the first comment is always assumed to belong to the 6708 // first element. Thus, it can be moved to the next or previous line as 6709 // appropriate. 6710 EXPECT_EQ("function({// First element:\n" 6711 " 1,\n" 6712 " // Second element:\n" 6713 " 2});", 6714 format("function({\n" 6715 " // First element:\n" 6716 " 1,\n" 6717 " // Second element:\n" 6718 " 2});")); 6719 EXPECT_EQ("std::vector<int> MyNumbers{\n" 6720 " // First element:\n" 6721 " 1,\n" 6722 " // Second element:\n" 6723 " 2};", 6724 format("std::vector<int> MyNumbers{// First element:\n" 6725 " 1,\n" 6726 " // Second element:\n" 6727 " 2};", 6728 getLLVMStyleWithColumns(30))); 6729 // A trailing comma should still lead to an enforced line break and no 6730 // binpacking. 6731 EXPECT_EQ("vector<int> SomeVector = {\n" 6732 " // aaa\n" 6733 " 1,\n" 6734 " 2,\n" 6735 "};", 6736 format("vector<int> SomeVector = { // aaa\n" 6737 " 1, 2, };")); 6738 6739 FormatStyle ExtraSpaces = getLLVMStyle(); 6740 ExtraSpaces.Cpp11BracedListStyle = false; 6741 ExtraSpaces.ColumnLimit = 75; 6742 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces); 6743 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces); 6744 verifyFormat("f({ 1, 2 });", ExtraSpaces); 6745 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces); 6746 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces); 6747 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces); 6748 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces); 6749 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces); 6750 verifyFormat("return { arg1, arg2 };", ExtraSpaces); 6751 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces); 6752 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces); 6753 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces); 6754 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces); 6755 verifyFormat("class Class {\n" 6756 " T member = { arg1, arg2 };\n" 6757 "};", 6758 ExtraSpaces); 6759 verifyFormat( 6760 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6761 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n" 6762 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 6763 " bbbbbbbbbbbbbbbbbbbb, bbbbb };", 6764 ExtraSpaces); 6765 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces); 6766 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });", 6767 ExtraSpaces); 6768 verifyFormat( 6769 "someFunction(OtherParam,\n" 6770 " BracedList{ // comment 1 (Forcing interesting break)\n" 6771 " param1, param2,\n" 6772 " // comment 2\n" 6773 " param3, param4 });", 6774 ExtraSpaces); 6775 verifyFormat( 6776 "std::this_thread::sleep_for(\n" 6777 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);", 6778 ExtraSpaces); 6779 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n" 6780 " aaaaaaa,\n" 6781 " aaaaaaaaaa,\n" 6782 " aaaaa,\n" 6783 " aaaaaaaaaaaaaaa,\n" 6784 " aaa,\n" 6785 " aaaaaaaaaa,\n" 6786 " a,\n" 6787 " aaaaaaaaaaaaaaaaaaaaa,\n" 6788 " aaaaaaaaaaaa,\n" 6789 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n" 6790 " aaaaaaa,\n" 6791 " a};"); 6792 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces); 6793 verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces); 6794 verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces); 6795 } 6796 6797 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { 6798 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6799 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6800 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6801 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6802 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6803 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 6804 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n" 6805 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6806 " 1, 22, 333, 4444, 55555, //\n" 6807 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6808 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 6809 verifyFormat( 6810 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6811 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6812 " 1, 22, 333, 4444, 55555, 666666, // comment\n" 6813 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6814 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6815 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6816 " 7777777};"); 6817 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6818 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6819 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6820 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6821 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6822 " // Separating comment.\n" 6823 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6824 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6825 " // Leading comment\n" 6826 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6827 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6828 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6829 " 1, 1, 1, 1};", 6830 getLLVMStyleWithColumns(39)); 6831 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6832 " 1, 1, 1, 1};", 6833 getLLVMStyleWithColumns(38)); 6834 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n" 6835 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};", 6836 getLLVMStyleWithColumns(43)); 6837 verifyFormat( 6838 "static unsigned SomeValues[10][3] = {\n" 6839 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n" 6840 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};"); 6841 verifyFormat("static auto fields = new vector<string>{\n" 6842 " \"aaaaaaaaaaaaa\",\n" 6843 " \"aaaaaaaaaaaaa\",\n" 6844 " \"aaaaaaaaaaaa\",\n" 6845 " \"aaaaaaaaaaaaaa\",\n" 6846 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 6847 " \"aaaaaaaaaaaa\",\n" 6848 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 6849 "};"); 6850 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};"); 6851 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n" 6852 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n" 6853 " 3, cccccccccccccccccccccc};", 6854 getLLVMStyleWithColumns(60)); 6855 6856 // Trailing commas. 6857 verifyFormat("vector<int> x = {\n" 6858 " 1, 1, 1, 1, 1, 1, 1, 1,\n" 6859 "};", 6860 getLLVMStyleWithColumns(39)); 6861 verifyFormat("vector<int> x = {\n" 6862 " 1, 1, 1, 1, 1, 1, 1, 1, //\n" 6863 "};", 6864 getLLVMStyleWithColumns(39)); 6865 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6866 " 1, 1, 1, 1,\n" 6867 " /**/ /**/};", 6868 getLLVMStyleWithColumns(39)); 6869 6870 // Trailing comment in the first line. 6871 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n" 6872 " 1111111111, 2222222222, 33333333333, 4444444444, //\n" 6873 " 111111111, 222222222, 3333333333, 444444444, //\n" 6874 " 11111111, 22222222, 333333333, 44444444};"); 6875 // Trailing comment in the last line. 6876 verifyFormat("int aaaaa[] = {\n" 6877 " 1, 2, 3, // comment\n" 6878 " 4, 5, 6 // comment\n" 6879 "};"); 6880 6881 // With nested lists, we should either format one item per line or all nested 6882 // lists one on line. 6883 // FIXME: For some nested lists, we can do better. 6884 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n" 6885 " {aaaaaaaaaaaaaaaaaaa},\n" 6886 " {aaaaaaaaaaaaaaaaaaaaa},\n" 6887 " {aaaaaaaaaaaaaaaaa}};", 6888 getLLVMStyleWithColumns(60)); 6889 verifyFormat( 6890 "SomeStruct my_struct_array = {\n" 6891 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n" 6892 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n" 6893 " {aaa, aaa},\n" 6894 " {aaa, aaa},\n" 6895 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n" 6896 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6897 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};"); 6898 6899 // No column layout should be used here. 6900 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n" 6901 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};"); 6902 6903 verifyNoCrash("a<,"); 6904 6905 // No braced initializer here. 6906 verifyFormat("void f() {\n" 6907 " struct Dummy {};\n" 6908 " f(v);\n" 6909 "}"); 6910 6911 // Long lists should be formatted in columns even if they are nested. 6912 verifyFormat( 6913 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6914 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6915 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6916 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6917 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6918 " 1, 22, 333, 4444, 55555, 666666, 7777777});"); 6919 6920 // Allow "single-column" layout even if that violates the column limit. There 6921 // isn't going to be a better way. 6922 verifyFormat("std::vector<int> a = {\n" 6923 " aaaaaaaa,\n" 6924 " aaaaaaaa,\n" 6925 " aaaaaaaa,\n" 6926 " aaaaaaaa,\n" 6927 " aaaaaaaaaa,\n" 6928 " aaaaaaaa,\n" 6929 " aaaaaaaaaaaaaaaaaaaaaaaaaaa};", 6930 getLLVMStyleWithColumns(30)); 6931 verifyFormat("vector<int> aaaa = {\n" 6932 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6933 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6934 " aaaaaa.aaaaaaa,\n" 6935 " aaaaaa.aaaaaaa,\n" 6936 " aaaaaa.aaaaaaa,\n" 6937 " aaaaaa.aaaaaaa,\n" 6938 "};"); 6939 6940 // Don't create hanging lists. 6941 verifyFormat("someFunction(Param, {List1, List2,\n" 6942 " List3});", 6943 getLLVMStyleWithColumns(35)); 6944 verifyFormat("someFunction(Param, Param,\n" 6945 " {List1, List2,\n" 6946 " List3});", 6947 getLLVMStyleWithColumns(35)); 6948 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n" 6949 " aaaaaaaaaaaaaaaaaaaaaaa);"); 6950 } 6951 6952 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 6953 FormatStyle DoNotMerge = getLLVMStyle(); 6954 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 6955 6956 verifyFormat("void f() { return 42; }"); 6957 verifyFormat("void f() {\n" 6958 " return 42;\n" 6959 "}", 6960 DoNotMerge); 6961 verifyFormat("void f() {\n" 6962 " // Comment\n" 6963 "}"); 6964 verifyFormat("{\n" 6965 "#error {\n" 6966 " int a;\n" 6967 "}"); 6968 verifyFormat("{\n" 6969 " int a;\n" 6970 "#error {\n" 6971 "}"); 6972 verifyFormat("void f() {} // comment"); 6973 verifyFormat("void f() { int a; } // comment"); 6974 verifyFormat("void f() {\n" 6975 "} // comment", 6976 DoNotMerge); 6977 verifyFormat("void f() {\n" 6978 " int a;\n" 6979 "} // comment", 6980 DoNotMerge); 6981 verifyFormat("void f() {\n" 6982 "} // comment", 6983 getLLVMStyleWithColumns(15)); 6984 6985 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 6986 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 6987 6988 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 6989 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 6990 verifyFormat("class C {\n" 6991 " C()\n" 6992 " : iiiiiiii(nullptr),\n" 6993 " kkkkkkk(nullptr),\n" 6994 " mmmmmmm(nullptr),\n" 6995 " nnnnnnn(nullptr) {}\n" 6996 "};", 6997 getGoogleStyle()); 6998 6999 FormatStyle NoColumnLimit = getLLVMStyle(); 7000 NoColumnLimit.ColumnLimit = 0; 7001 EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit)); 7002 EXPECT_EQ("class C {\n" 7003 " A() : b(0) {}\n" 7004 "};", 7005 format("class C{A():b(0){}};", NoColumnLimit)); 7006 EXPECT_EQ("A()\n" 7007 " : b(0) {\n" 7008 "}", 7009 format("A()\n:b(0)\n{\n}", NoColumnLimit)); 7010 7011 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit; 7012 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine = 7013 FormatStyle::SFS_None; 7014 EXPECT_EQ("A()\n" 7015 " : b(0) {\n" 7016 "}", 7017 format("A():b(0){}", DoNotMergeNoColumnLimit)); 7018 EXPECT_EQ("A()\n" 7019 " : b(0) {\n" 7020 "}", 7021 format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit)); 7022 7023 verifyFormat("#define A \\\n" 7024 " void f() { \\\n" 7025 " int i; \\\n" 7026 " }", 7027 getLLVMStyleWithColumns(20)); 7028 verifyFormat("#define A \\\n" 7029 " void f() { int i; }", 7030 getLLVMStyleWithColumns(21)); 7031 verifyFormat("#define A \\\n" 7032 " void f() { \\\n" 7033 " int i; \\\n" 7034 " } \\\n" 7035 " int j;", 7036 getLLVMStyleWithColumns(22)); 7037 verifyFormat("#define A \\\n" 7038 " void f() { int i; } \\\n" 7039 " int j;", 7040 getLLVMStyleWithColumns(23)); 7041 } 7042 7043 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) { 7044 FormatStyle MergeEmptyOnly = getLLVMStyle(); 7045 MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 7046 verifyFormat("class C {\n" 7047 " int f() {}\n" 7048 "};", 7049 MergeEmptyOnly); 7050 verifyFormat("class C {\n" 7051 " int f() {\n" 7052 " return 42;\n" 7053 " }\n" 7054 "};", 7055 MergeEmptyOnly); 7056 verifyFormat("int f() {}", MergeEmptyOnly); 7057 verifyFormat("int f() {\n" 7058 " return 42;\n" 7059 "}", 7060 MergeEmptyOnly); 7061 7062 // Also verify behavior when BraceWrapping.AfterFunction = true 7063 MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 7064 MergeEmptyOnly.BraceWrapping.AfterFunction = true; 7065 verifyFormat("int f() {}", MergeEmptyOnly); 7066 verifyFormat("class C {\n" 7067 " int f() {}\n" 7068 "};", 7069 MergeEmptyOnly); 7070 } 7071 7072 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) { 7073 FormatStyle MergeInlineOnly = getLLVMStyle(); 7074 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 7075 verifyFormat("class C {\n" 7076 " int f() { return 42; }\n" 7077 "};", 7078 MergeInlineOnly); 7079 verifyFormat("int f() {\n" 7080 " return 42;\n" 7081 "}", 7082 MergeInlineOnly); 7083 7084 // SFS_Inline implies SFS_Empty 7085 verifyFormat("class C {\n" 7086 " int f() {}\n" 7087 "};", 7088 MergeInlineOnly); 7089 verifyFormat("int f() {}", MergeInlineOnly); 7090 7091 // Also verify behavior when BraceWrapping.AfterFunction = true 7092 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 7093 MergeInlineOnly.BraceWrapping.AfterFunction = true; 7094 verifyFormat("class C {\n" 7095 " int f() { return 42; }\n" 7096 "};", 7097 MergeInlineOnly); 7098 verifyFormat("int f()\n" 7099 "{\n" 7100 " return 42;\n" 7101 "}", 7102 MergeInlineOnly); 7103 7104 // SFS_Inline implies SFS_Empty 7105 verifyFormat("int f() {}", MergeInlineOnly); 7106 verifyFormat("class C {\n" 7107 " int f() {}\n" 7108 "};", 7109 MergeInlineOnly); 7110 } 7111 7112 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) { 7113 FormatStyle MergeInlineOnly = getLLVMStyle(); 7114 MergeInlineOnly.AllowShortFunctionsOnASingleLine = 7115 FormatStyle::SFS_InlineOnly; 7116 verifyFormat("class C {\n" 7117 " int f() { return 42; }\n" 7118 "};", 7119 MergeInlineOnly); 7120 verifyFormat("int f() {\n" 7121 " return 42;\n" 7122 "}", 7123 MergeInlineOnly); 7124 7125 // SFS_InlineOnly does not imply SFS_Empty 7126 verifyFormat("class C {\n" 7127 " int f() {}\n" 7128 "};", 7129 MergeInlineOnly); 7130 verifyFormat("int f() {\n" 7131 "}", 7132 MergeInlineOnly); 7133 7134 // Also verify behavior when BraceWrapping.AfterFunction = true 7135 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 7136 MergeInlineOnly.BraceWrapping.AfterFunction = true; 7137 verifyFormat("class C {\n" 7138 " int f() { return 42; }\n" 7139 "};", 7140 MergeInlineOnly); 7141 verifyFormat("int f()\n" 7142 "{\n" 7143 " return 42;\n" 7144 "}", 7145 MergeInlineOnly); 7146 7147 // SFS_InlineOnly does not imply SFS_Empty 7148 verifyFormat("int f()\n" 7149 "{\n" 7150 "}", 7151 MergeInlineOnly); 7152 verifyFormat("class C {\n" 7153 " int f() {}\n" 7154 "};", 7155 MergeInlineOnly); 7156 } 7157 7158 TEST_F(FormatTest, SplitEmptyFunction) { 7159 FormatStyle Style = getLLVMStyle(); 7160 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 7161 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7162 Style.BraceWrapping.AfterFunction = true; 7163 Style.BraceWrapping.SplitEmptyFunction = false; 7164 Style.ColumnLimit = 40; 7165 7166 verifyFormat("int f()\n" 7167 "{}", 7168 Style); 7169 verifyFormat("int f()\n" 7170 "{\n" 7171 " return 42;\n" 7172 "}", 7173 Style); 7174 verifyFormat("int f()\n" 7175 "{\n" 7176 " // some comment\n" 7177 "}", 7178 Style); 7179 7180 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 7181 verifyFormat("int f() {}", Style); 7182 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 7183 "{}", 7184 Style); 7185 verifyFormat("int f()\n" 7186 "{\n" 7187 " return 0;\n" 7188 "}", 7189 Style); 7190 7191 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 7192 verifyFormat("class Foo {\n" 7193 " int f() {}\n" 7194 "};\n", 7195 Style); 7196 verifyFormat("class Foo {\n" 7197 " int f() { return 0; }\n" 7198 "};\n", 7199 Style); 7200 verifyFormat("class Foo {\n" 7201 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 7202 " {}\n" 7203 "};\n", 7204 Style); 7205 verifyFormat("class Foo {\n" 7206 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 7207 " {\n" 7208 " return 0;\n" 7209 " }\n" 7210 "};\n", 7211 Style); 7212 7213 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 7214 verifyFormat("int f() {}", Style); 7215 verifyFormat("int f() { return 0; }", Style); 7216 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 7217 "{}", 7218 Style); 7219 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 7220 "{\n" 7221 " return 0;\n" 7222 "}", 7223 Style); 7224 } 7225 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) { 7226 FormatStyle Style = getLLVMStyle(); 7227 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 7228 verifyFormat("#ifdef A\n" 7229 "int f() {}\n" 7230 "#else\n" 7231 "int g() {}\n" 7232 "#endif", 7233 Style); 7234 } 7235 7236 TEST_F(FormatTest, SplitEmptyClass) { 7237 FormatStyle Style = getLLVMStyle(); 7238 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7239 Style.BraceWrapping.AfterClass = true; 7240 Style.BraceWrapping.SplitEmptyRecord = false; 7241 7242 verifyFormat("class Foo\n" 7243 "{};", 7244 Style); 7245 verifyFormat("/* something */ class Foo\n" 7246 "{};", 7247 Style); 7248 verifyFormat("template <typename X> class Foo\n" 7249 "{};", 7250 Style); 7251 verifyFormat("class Foo\n" 7252 "{\n" 7253 " Foo();\n" 7254 "};", 7255 Style); 7256 verifyFormat("typedef class Foo\n" 7257 "{\n" 7258 "} Foo_t;", 7259 Style); 7260 } 7261 7262 TEST_F(FormatTest, SplitEmptyStruct) { 7263 FormatStyle Style = getLLVMStyle(); 7264 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7265 Style.BraceWrapping.AfterStruct = true; 7266 Style.BraceWrapping.SplitEmptyRecord = false; 7267 7268 verifyFormat("struct Foo\n" 7269 "{};", 7270 Style); 7271 verifyFormat("/* something */ struct Foo\n" 7272 "{};", 7273 Style); 7274 verifyFormat("template <typename X> struct Foo\n" 7275 "{};", 7276 Style); 7277 verifyFormat("struct Foo\n" 7278 "{\n" 7279 " Foo();\n" 7280 "};", 7281 Style); 7282 verifyFormat("typedef struct Foo\n" 7283 "{\n" 7284 "} Foo_t;", 7285 Style); 7286 //typedef struct Bar {} Bar_t; 7287 } 7288 7289 TEST_F(FormatTest, SplitEmptyUnion) { 7290 FormatStyle Style = getLLVMStyle(); 7291 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7292 Style.BraceWrapping.AfterUnion = true; 7293 Style.BraceWrapping.SplitEmptyRecord = false; 7294 7295 verifyFormat("union Foo\n" 7296 "{};", 7297 Style); 7298 verifyFormat("/* something */ union Foo\n" 7299 "{};", 7300 Style); 7301 verifyFormat("union Foo\n" 7302 "{\n" 7303 " A,\n" 7304 "};", 7305 Style); 7306 verifyFormat("typedef union Foo\n" 7307 "{\n" 7308 "} Foo_t;", 7309 Style); 7310 } 7311 7312 TEST_F(FormatTest, SplitEmptyNamespace) { 7313 FormatStyle Style = getLLVMStyle(); 7314 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7315 Style.BraceWrapping.AfterNamespace = true; 7316 Style.BraceWrapping.SplitEmptyNamespace = false; 7317 7318 verifyFormat("namespace Foo\n" 7319 "{};", 7320 Style); 7321 verifyFormat("/* something */ namespace Foo\n" 7322 "{};", 7323 Style); 7324 verifyFormat("inline namespace Foo\n" 7325 "{};", 7326 Style); 7327 verifyFormat("namespace Foo\n" 7328 "{\n" 7329 "void Bar();\n" 7330 "};", 7331 Style); 7332 } 7333 7334 TEST_F(FormatTest, NeverMergeShortRecords) { 7335 FormatStyle Style = getLLVMStyle(); 7336 7337 verifyFormat("class Foo {\n" 7338 " Foo();\n" 7339 "};", 7340 Style); 7341 verifyFormat("typedef class Foo {\n" 7342 " Foo();\n" 7343 "} Foo_t;", 7344 Style); 7345 verifyFormat("struct Foo {\n" 7346 " Foo();\n" 7347 "};", 7348 Style); 7349 verifyFormat("typedef struct Foo {\n" 7350 " Foo();\n" 7351 "} Foo_t;", 7352 Style); 7353 verifyFormat("union Foo {\n" 7354 " A,\n" 7355 "};", 7356 Style); 7357 verifyFormat("typedef union Foo {\n" 7358 " A,\n" 7359 "} Foo_t;", 7360 Style); 7361 verifyFormat("namespace Foo {\n" 7362 "void Bar();\n" 7363 "};", 7364 Style); 7365 7366 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7367 Style.BraceWrapping.AfterClass = true; 7368 Style.BraceWrapping.AfterStruct = true; 7369 Style.BraceWrapping.AfterUnion = true; 7370 Style.BraceWrapping.AfterNamespace = true; 7371 verifyFormat("class Foo\n" 7372 "{\n" 7373 " Foo();\n" 7374 "};", 7375 Style); 7376 verifyFormat("typedef class Foo\n" 7377 "{\n" 7378 " Foo();\n" 7379 "} Foo_t;", 7380 Style); 7381 verifyFormat("struct Foo\n" 7382 "{\n" 7383 " Foo();\n" 7384 "};", 7385 Style); 7386 verifyFormat("typedef struct Foo\n" 7387 "{\n" 7388 " Foo();\n" 7389 "} Foo_t;", 7390 Style); 7391 verifyFormat("union Foo\n" 7392 "{\n" 7393 " A,\n" 7394 "};", 7395 Style); 7396 verifyFormat("typedef union Foo\n" 7397 "{\n" 7398 " A,\n" 7399 "} Foo_t;", 7400 Style); 7401 verifyFormat("namespace Foo\n" 7402 "{\n" 7403 "void Bar();\n" 7404 "};", 7405 Style); 7406 } 7407 7408 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 7409 // Elaborate type variable declarations. 7410 verifyFormat("struct foo a = {bar};\nint n;"); 7411 verifyFormat("class foo a = {bar};\nint n;"); 7412 verifyFormat("union foo a = {bar};\nint n;"); 7413 7414 // Elaborate types inside function definitions. 7415 verifyFormat("struct foo f() {}\nint n;"); 7416 verifyFormat("class foo f() {}\nint n;"); 7417 verifyFormat("union foo f() {}\nint n;"); 7418 7419 // Templates. 7420 verifyFormat("template <class X> void f() {}\nint n;"); 7421 verifyFormat("template <struct X> void f() {}\nint n;"); 7422 verifyFormat("template <union X> void f() {}\nint n;"); 7423 7424 // Actual definitions... 7425 verifyFormat("struct {\n} n;"); 7426 verifyFormat( 7427 "template <template <class T, class Y>, class Z> class X {\n} n;"); 7428 verifyFormat("union Z {\n int n;\n} x;"); 7429 verifyFormat("class MACRO Z {\n} n;"); 7430 verifyFormat("class MACRO(X) Z {\n} n;"); 7431 verifyFormat("class __attribute__(X) Z {\n} n;"); 7432 verifyFormat("class __declspec(X) Z {\n} n;"); 7433 verifyFormat("class A##B##C {\n} n;"); 7434 verifyFormat("class alignas(16) Z {\n} n;"); 7435 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;"); 7436 verifyFormat("class MACROA MACRO(X) Z {\n} n;"); 7437 7438 // Redefinition from nested context: 7439 verifyFormat("class A::B::C {\n} n;"); 7440 7441 // Template definitions. 7442 verifyFormat( 7443 "template <typename F>\n" 7444 "Matcher(const Matcher<F> &Other,\n" 7445 " typename enable_if_c<is_base_of<F, T>::value &&\n" 7446 " !is_same<F, T>::value>::type * = 0)\n" 7447 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}"); 7448 7449 // FIXME: This is still incorrectly handled at the formatter side. 7450 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};"); 7451 verifyFormat("int i = SomeFunction(a<b, a> b);"); 7452 7453 // FIXME: 7454 // This now gets parsed incorrectly as class definition. 7455 // verifyFormat("class A<int> f() {\n}\nint n;"); 7456 7457 // Elaborate types where incorrectly parsing the structural element would 7458 // break the indent. 7459 verifyFormat("if (true)\n" 7460 " class X x;\n" 7461 "else\n" 7462 " f();\n"); 7463 7464 // This is simply incomplete. Formatting is not important, but must not crash. 7465 verifyFormat("class A:"); 7466 } 7467 7468 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 7469 EXPECT_EQ("#error Leave all white!!!!! space* alone!\n", 7470 format("#error Leave all white!!!!! space* alone!\n")); 7471 EXPECT_EQ( 7472 "#warning Leave all white!!!!! space* alone!\n", 7473 format("#warning Leave all white!!!!! space* alone!\n")); 7474 EXPECT_EQ("#error 1", format(" # error 1")); 7475 EXPECT_EQ("#warning 1", format(" # warning 1")); 7476 } 7477 7478 TEST_F(FormatTest, FormatHashIfExpressions) { 7479 verifyFormat("#if AAAA && BBBB"); 7480 verifyFormat("#if (AAAA && BBBB)"); 7481 verifyFormat("#elif (AAAA && BBBB)"); 7482 // FIXME: Come up with a better indentation for #elif. 7483 verifyFormat( 7484 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" 7485 " defined(BBBBBBBB)\n" 7486 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" 7487 " defined(BBBBBBBB)\n" 7488 "#endif", 7489 getLLVMStyleWithColumns(65)); 7490 } 7491 7492 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 7493 FormatStyle AllowsMergedIf = getGoogleStyle(); 7494 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 7495 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 7496 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 7497 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 7498 EXPECT_EQ("if (true) return 42;", 7499 format("if (true)\nreturn 42;", AllowsMergedIf)); 7500 FormatStyle ShortMergedIf = AllowsMergedIf; 7501 ShortMergedIf.ColumnLimit = 25; 7502 verifyFormat("#define A \\\n" 7503 " if (true) return 42;", 7504 ShortMergedIf); 7505 verifyFormat("#define A \\\n" 7506 " f(); \\\n" 7507 " if (true)\n" 7508 "#define B", 7509 ShortMergedIf); 7510 verifyFormat("#define A \\\n" 7511 " f(); \\\n" 7512 " if (true)\n" 7513 "g();", 7514 ShortMergedIf); 7515 verifyFormat("{\n" 7516 "#ifdef A\n" 7517 " // Comment\n" 7518 " if (true) continue;\n" 7519 "#endif\n" 7520 " // Comment\n" 7521 " if (true) continue;\n" 7522 "}", 7523 ShortMergedIf); 7524 ShortMergedIf.ColumnLimit = 33; 7525 verifyFormat("#define A \\\n" 7526 " if constexpr (true) return 42;", 7527 ShortMergedIf); 7528 ShortMergedIf.ColumnLimit = 29; 7529 verifyFormat("#define A \\\n" 7530 " if (aaaaaaaaaa) return 1; \\\n" 7531 " return 2;", 7532 ShortMergedIf); 7533 ShortMergedIf.ColumnLimit = 28; 7534 verifyFormat("#define A \\\n" 7535 " if (aaaaaaaaaa) \\\n" 7536 " return 1; \\\n" 7537 " return 2;", 7538 ShortMergedIf); 7539 verifyFormat("#define A \\\n" 7540 " if constexpr (aaaaaaa) \\\n" 7541 " return 1; \\\n" 7542 " return 2;", 7543 ShortMergedIf); 7544 } 7545 7546 TEST_F(FormatTest, FormatStarDependingOnContext) { 7547 verifyFormat("void f(int *a);"); 7548 verifyFormat("void f() { f(fint * b); }"); 7549 verifyFormat("class A {\n void f(int *a);\n};"); 7550 verifyFormat("class A {\n int *a;\n};"); 7551 verifyFormat("namespace a {\n" 7552 "namespace b {\n" 7553 "class A {\n" 7554 " void f() {}\n" 7555 " int *a;\n" 7556 "};\n" 7557 "} // namespace b\n" 7558 "} // namespace a"); 7559 } 7560 7561 TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 7562 verifyFormat("while"); 7563 verifyFormat("operator"); 7564 } 7565 7566 TEST_F(FormatTest, SkipsDeeplyNestedLines) { 7567 // This code would be painfully slow to format if we didn't skip it. 7568 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 7569 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 7570 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 7571 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 7572 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 7573 "A(1, 1)\n" 7574 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x 7575 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7576 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7577 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7578 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7579 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7580 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7581 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7582 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7583 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n"); 7584 // Deeply nested part is untouched, rest is formatted. 7585 EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n", 7586 format(std::string("int i;\n") + Code + "int j;\n", 7587 getLLVMStyle(), SC_ExpectIncomplete)); 7588 } 7589 7590 //===----------------------------------------------------------------------===// 7591 // Objective-C tests. 7592 //===----------------------------------------------------------------------===// 7593 7594 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 7595 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 7596 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 7597 format("-(NSUInteger)indexOfObject:(id)anObject;")); 7598 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 7599 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 7600 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 7601 format("-(NSInteger)Method3:(id)anObject;")); 7602 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 7603 format("-(NSInteger)Method4:(id)anObject;")); 7604 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 7605 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 7606 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 7607 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 7608 EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject " 7609 "forAllCells:(BOOL)flag;", 7610 format("- (void)sendAction:(SEL)aSelector to:(id)anObject " 7611 "forAllCells:(BOOL)flag;")); 7612 7613 // Very long objectiveC method declaration. 7614 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 7615 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;"); 7616 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 7617 " inRange:(NSRange)range\n" 7618 " outRange:(NSRange)out_range\n" 7619 " outRange1:(NSRange)out_range1\n" 7620 " outRange2:(NSRange)out_range2\n" 7621 " outRange3:(NSRange)out_range3\n" 7622 " outRange4:(NSRange)out_range4\n" 7623 " outRange5:(NSRange)out_range5\n" 7624 " outRange6:(NSRange)out_range6\n" 7625 " outRange7:(NSRange)out_range7\n" 7626 " outRange8:(NSRange)out_range8\n" 7627 " outRange9:(NSRange)out_range9;"); 7628 7629 // When the function name has to be wrapped. 7630 FormatStyle Style = getLLVMStyle(); 7631 Style.IndentWrappedFunctionNames = false; 7632 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 7633 "veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 7634 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 7635 "}", 7636 Style); 7637 Style.IndentWrappedFunctionNames = true; 7638 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 7639 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 7640 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 7641 "}", 7642 Style); 7643 7644 verifyFormat("- (int)sum:(vector<int>)numbers;"); 7645 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 7646 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 7647 // protocol lists (but not for template classes): 7648 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 7649 7650 verifyFormat("- (int (*)())foo:(int (*)())f;"); 7651 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); 7652 7653 // If there's no return type (very rare in practice!), LLVM and Google style 7654 // agree. 7655 verifyFormat("- foo;"); 7656 verifyFormat("- foo:(int)f;"); 7657 verifyGoogleFormat("- foo:(int)foo;"); 7658 } 7659 7660 7661 TEST_F(FormatTest, BreaksStringLiterals) { 7662 EXPECT_EQ("\"some text \"\n" 7663 "\"other\";", 7664 format("\"some text other\";", getLLVMStyleWithColumns(12))); 7665 EXPECT_EQ("\"some text \"\n" 7666 "\"other\";", 7667 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); 7668 EXPECT_EQ( 7669 "#define A \\\n" 7670 " \"some \" \\\n" 7671 " \"text \" \\\n" 7672 " \"other\";", 7673 format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); 7674 EXPECT_EQ( 7675 "#define A \\\n" 7676 " \"so \" \\\n" 7677 " \"text \" \\\n" 7678 " \"other\";", 7679 format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); 7680 7681 EXPECT_EQ("\"some text\"", 7682 format("\"some text\"", getLLVMStyleWithColumns(1))); 7683 EXPECT_EQ("\"some text\"", 7684 format("\"some text\"", getLLVMStyleWithColumns(11))); 7685 EXPECT_EQ("\"some \"\n" 7686 "\"text\"", 7687 format("\"some text\"", getLLVMStyleWithColumns(10))); 7688 EXPECT_EQ("\"some \"\n" 7689 "\"text\"", 7690 format("\"some text\"", getLLVMStyleWithColumns(7))); 7691 EXPECT_EQ("\"some\"\n" 7692 "\" tex\"\n" 7693 "\"t\"", 7694 format("\"some text\"", getLLVMStyleWithColumns(6))); 7695 EXPECT_EQ("\"some\"\n" 7696 "\" tex\"\n" 7697 "\" and\"", 7698 format("\"some tex and\"", getLLVMStyleWithColumns(6))); 7699 EXPECT_EQ("\"some\"\n" 7700 "\"/tex\"\n" 7701 "\"/and\"", 7702 format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); 7703 7704 EXPECT_EQ("variable =\n" 7705 " \"long string \"\n" 7706 " \"literal\";", 7707 format("variable = \"long string literal\";", 7708 getLLVMStyleWithColumns(20))); 7709 7710 EXPECT_EQ("variable = f(\n" 7711 " \"long string \"\n" 7712 " \"literal\",\n" 7713 " short,\n" 7714 " loooooooooooooooooooong);", 7715 format("variable = f(\"long string literal\", short, " 7716 "loooooooooooooooooooong);", 7717 getLLVMStyleWithColumns(20))); 7718 7719 EXPECT_EQ( 7720 "f(g(\"long string \"\n" 7721 " \"literal\"),\n" 7722 " b);", 7723 format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20))); 7724 EXPECT_EQ("f(g(\"long string \"\n" 7725 " \"literal\",\n" 7726 " a),\n" 7727 " b);", 7728 format("f(g(\"long string literal\", a), b);", 7729 getLLVMStyleWithColumns(20))); 7730 EXPECT_EQ( 7731 "f(\"one two\".split(\n" 7732 " variable));", 7733 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); 7734 EXPECT_EQ("f(\"one two three four five six \"\n" 7735 " \"seven\".split(\n" 7736 " really_looooong_variable));", 7737 format("f(\"one two three four five six seven\"." 7738 "split(really_looooong_variable));", 7739 getLLVMStyleWithColumns(33))); 7740 7741 EXPECT_EQ("f(\"some \"\n" 7742 " \"text\",\n" 7743 " other);", 7744 format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); 7745 7746 // Only break as a last resort. 7747 verifyFormat( 7748 "aaaaaaaaaaaaaaaaaaaa(\n" 7749 " aaaaaaaaaaaaaaaaaaaa,\n" 7750 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); 7751 7752 EXPECT_EQ("\"splitmea\"\n" 7753 "\"trandomp\"\n" 7754 "\"oint\"", 7755 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); 7756 7757 EXPECT_EQ("\"split/\"\n" 7758 "\"pathat/\"\n" 7759 "\"slashes\"", 7760 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 7761 7762 EXPECT_EQ("\"split/\"\n" 7763 "\"pathat/\"\n" 7764 "\"slashes\"", 7765 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 7766 EXPECT_EQ("\"split at \"\n" 7767 "\"spaces/at/\"\n" 7768 "\"slashes.at.any$\"\n" 7769 "\"non-alphanumeric%\"\n" 7770 "\"1111111111characte\"\n" 7771 "\"rs\"", 7772 format("\"split at " 7773 "spaces/at/" 7774 "slashes.at." 7775 "any$non-" 7776 "alphanumeric%" 7777 "1111111111characte" 7778 "rs\"", 7779 getLLVMStyleWithColumns(20))); 7780 7781 // Verify that splitting the strings understands 7782 // Style::AlwaysBreakBeforeMultilineStrings. 7783 EXPECT_EQ( 7784 "aaaaaaaaaaaa(\n" 7785 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" 7786 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", 7787 format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa " 7788 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 7789 "aaaaaaaaaaaaaaaaaaaaaa\");", 7790 getGoogleStyle())); 7791 EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7792 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", 7793 format("return \"aaaaaaaaaaaaaaaaaaaaaa " 7794 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 7795 "aaaaaaaaaaaaaaaaaaaaaa\";", 7796 getGoogleStyle())); 7797 EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7798 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 7799 format("llvm::outs() << " 7800 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa" 7801 "aaaaaaaaaaaaaaaaaaa\";")); 7802 EXPECT_EQ("ffff(\n" 7803 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7804 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 7805 format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " 7806 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 7807 getGoogleStyle())); 7808 7809 FormatStyle Style = getLLVMStyleWithColumns(12); 7810 Style.BreakStringLiterals = false; 7811 EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style)); 7812 7813 FormatStyle AlignLeft = getLLVMStyleWithColumns(12); 7814 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 7815 EXPECT_EQ("#define A \\\n" 7816 " \"some \" \\\n" 7817 " \"text \" \\\n" 7818 " \"other\";", 7819 format("#define A \"some text other\";", AlignLeft)); 7820 } 7821 7822 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) { 7823 EXPECT_EQ("C a = \"some more \"\n" 7824 " \"text\";", 7825 format("C a = \"some more text\";", getLLVMStyleWithColumns(18))); 7826 } 7827 7828 TEST_F(FormatTest, FullyRemoveEmptyLines) { 7829 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80); 7830 NoEmptyLines.MaxEmptyLinesToKeep = 0; 7831 EXPECT_EQ("int i = a(b());", 7832 format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines)); 7833 } 7834 7835 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) { 7836 EXPECT_EQ( 7837 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 7838 "(\n" 7839 " \"x\t\");", 7840 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 7841 "aaaaaaa(" 7842 "\"x\t\");")); 7843 } 7844 7845 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) { 7846 EXPECT_EQ( 7847 "u8\"utf8 string \"\n" 7848 "u8\"literal\";", 7849 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16))); 7850 EXPECT_EQ( 7851 "u\"utf16 string \"\n" 7852 "u\"literal\";", 7853 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16))); 7854 EXPECT_EQ( 7855 "U\"utf32 string \"\n" 7856 "U\"literal\";", 7857 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16))); 7858 EXPECT_EQ("L\"wide string \"\n" 7859 "L\"literal\";", 7860 format("L\"wide string literal\";", getGoogleStyleWithColumns(16))); 7861 EXPECT_EQ("@\"NSString \"\n" 7862 "@\"literal\";", 7863 format("@\"NSString literal\";", getGoogleStyleWithColumns(19))); 7864 verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26)); 7865 7866 // This input makes clang-format try to split the incomplete unicode escape 7867 // sequence, which used to lead to a crasher. 7868 verifyNoCrash( 7869 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 7870 getLLVMStyleWithColumns(60)); 7871 } 7872 7873 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) { 7874 FormatStyle Style = getGoogleStyleWithColumns(15); 7875 EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style)); 7876 EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style)); 7877 EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style)); 7878 EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style)); 7879 EXPECT_EQ("u8R\"x(raw literal)x\";", 7880 format("u8R\"x(raw literal)x\";", Style)); 7881 } 7882 7883 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { 7884 FormatStyle Style = getLLVMStyleWithColumns(20); 7885 EXPECT_EQ( 7886 "_T(\"aaaaaaaaaaaaaa\")\n" 7887 "_T(\"aaaaaaaaaaaaaa\")\n" 7888 "_T(\"aaaaaaaaaaaa\")", 7889 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style)); 7890 EXPECT_EQ("f(x,\n" 7891 " _T(\"aaaaaaaaaaaa\")\n" 7892 " _T(\"aaa\"),\n" 7893 " z);", 7894 format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style)); 7895 7896 // FIXME: Handle embedded spaces in one iteration. 7897 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n" 7898 // "_T(\"aaaaaaaaaaaaa\")\n" 7899 // "_T(\"aaaaaaaaaaaaa\")\n" 7900 // "_T(\"a\")", 7901 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 7902 // getLLVMStyleWithColumns(20))); 7903 EXPECT_EQ( 7904 "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 7905 format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style)); 7906 EXPECT_EQ("f(\n" 7907 "#if !TEST\n" 7908 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 7909 "#endif\n" 7910 ");", 7911 format("f(\n" 7912 "#if !TEST\n" 7913 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 7914 "#endif\n" 7915 ");")); 7916 EXPECT_EQ("f(\n" 7917 "\n" 7918 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));", 7919 format("f(\n" 7920 "\n" 7921 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));")); 7922 } 7923 7924 TEST_F(FormatTest, BreaksStringLiteralOperands) { 7925 // In a function call with two operands, the second can be broken with no line 7926 // break before it. 7927 EXPECT_EQ("func(a, \"long long \"\n" 7928 " \"long long\");", 7929 format("func(a, \"long long long long\");", 7930 getLLVMStyleWithColumns(24))); 7931 // In a function call with three operands, the second must be broken with a 7932 // line break before it. 7933 EXPECT_EQ("func(a,\n" 7934 " \"long long long \"\n" 7935 " \"long\",\n" 7936 " c);", 7937 format("func(a, \"long long long long\", c);", 7938 getLLVMStyleWithColumns(24))); 7939 // In a function call with three operands, the third must be broken with a 7940 // line break before it. 7941 EXPECT_EQ("func(a, b,\n" 7942 " \"long long long \"\n" 7943 " \"long\");", 7944 format("func(a, b, \"long long long long\");", 7945 getLLVMStyleWithColumns(24))); 7946 // In a function call with three operands, both the second and the third must 7947 // be broken with a line break before them. 7948 EXPECT_EQ("func(a,\n" 7949 " \"long long long \"\n" 7950 " \"long\",\n" 7951 " \"long long long \"\n" 7952 " \"long\");", 7953 format("func(a, \"long long long long\", \"long long long long\");", 7954 getLLVMStyleWithColumns(24))); 7955 // In a chain of << with two operands, the second can be broken with no line 7956 // break before it. 7957 EXPECT_EQ("a << \"line line \"\n" 7958 " \"line\";", 7959 format("a << \"line line line\";", 7960 getLLVMStyleWithColumns(20))); 7961 // In a chain of << with three operands, the second can be broken with no line 7962 // break before it. 7963 EXPECT_EQ("abcde << \"line \"\n" 7964 " \"line line\"\n" 7965 " << c;", 7966 format("abcde << \"line line line\" << c;", 7967 getLLVMStyleWithColumns(20))); 7968 // In a chain of << with three operands, the third must be broken with a line 7969 // break before it. 7970 EXPECT_EQ("a << b\n" 7971 " << \"line line \"\n" 7972 " \"line\";", 7973 format("a << b << \"line line line\";", 7974 getLLVMStyleWithColumns(20))); 7975 // In a chain of << with three operands, the second can be broken with no line 7976 // break before it and the third must be broken with a line break before it. 7977 EXPECT_EQ("abcd << \"line line \"\n" 7978 " \"line\"\n" 7979 " << \"line line \"\n" 7980 " \"line\";", 7981 format("abcd << \"line line line\" << \"line line line\";", 7982 getLLVMStyleWithColumns(20))); 7983 // In a chain of binary operators with two operands, the second can be broken 7984 // with no line break before it. 7985 EXPECT_EQ("abcd + \"line line \"\n" 7986 " \"line line\";", 7987 format("abcd + \"line line line line\";", 7988 getLLVMStyleWithColumns(20))); 7989 // In a chain of binary operators with three operands, the second must be 7990 // broken with a line break before it. 7991 EXPECT_EQ("abcd +\n" 7992 " \"line line \"\n" 7993 " \"line line\" +\n" 7994 " e;", 7995 format("abcd + \"line line line line\" + e;", 7996 getLLVMStyleWithColumns(20))); 7997 // In a function call with two operands, with AlignAfterOpenBracket enabled, 7998 // the first must be broken with a line break before it. 7999 FormatStyle Style = getLLVMStyleWithColumns(25); 8000 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 8001 EXPECT_EQ("someFunction(\n" 8002 " \"long long long \"\n" 8003 " \"long\",\n" 8004 " a);", 8005 format("someFunction(\"long long long long\", a);", Style)); 8006 } 8007 8008 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { 8009 EXPECT_EQ( 8010 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8011 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8012 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 8013 format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8014 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8015 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";")); 8016 } 8017 8018 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { 8019 EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);", 8020 format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle())); 8021 EXPECT_EQ("fffffffffff(g(R\"x(\n" 8022 "multiline raw string literal xxxxxxxxxxxxxx\n" 8023 ")x\",\n" 8024 " a),\n" 8025 " b);", 8026 format("fffffffffff(g(R\"x(\n" 8027 "multiline raw string literal xxxxxxxxxxxxxx\n" 8028 ")x\", a), b);", 8029 getGoogleStyleWithColumns(20))); 8030 EXPECT_EQ("fffffffffff(\n" 8031 " g(R\"x(qqq\n" 8032 "multiline raw string literal xxxxxxxxxxxxxx\n" 8033 ")x\",\n" 8034 " a),\n" 8035 " b);", 8036 format("fffffffffff(g(R\"x(qqq\n" 8037 "multiline raw string literal xxxxxxxxxxxxxx\n" 8038 ")x\", a), b);", 8039 getGoogleStyleWithColumns(20))); 8040 8041 EXPECT_EQ("fffffffffff(R\"x(\n" 8042 "multiline raw string literal xxxxxxxxxxxxxx\n" 8043 ")x\");", 8044 format("fffffffffff(R\"x(\n" 8045 "multiline raw string literal xxxxxxxxxxxxxx\n" 8046 ")x\");", 8047 getGoogleStyleWithColumns(20))); 8048 EXPECT_EQ("fffffffffff(R\"x(\n" 8049 "multiline raw string literal xxxxxxxxxxxxxx\n" 8050 ")x\" + bbbbbb);", 8051 format("fffffffffff(R\"x(\n" 8052 "multiline raw string literal xxxxxxxxxxxxxx\n" 8053 ")x\" + bbbbbb);", 8054 getGoogleStyleWithColumns(20))); 8055 EXPECT_EQ("fffffffffff(\n" 8056 " R\"x(\n" 8057 "multiline raw string literal xxxxxxxxxxxxxx\n" 8058 ")x\" +\n" 8059 " bbbbbb);", 8060 format("fffffffffff(\n" 8061 " R\"x(\n" 8062 "multiline raw string literal xxxxxxxxxxxxxx\n" 8063 ")x\" + bbbbbb);", 8064 getGoogleStyleWithColumns(20))); 8065 } 8066 8067 TEST_F(FormatTest, SkipsUnknownStringLiterals) { 8068 verifyFormat("string a = \"unterminated;"); 8069 EXPECT_EQ("function(\"unterminated,\n" 8070 " OtherParameter);", 8071 format("function( \"unterminated,\n" 8072 " OtherParameter);")); 8073 } 8074 8075 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { 8076 FormatStyle Style = getLLVMStyle(); 8077 Style.Standard = FormatStyle::LS_Cpp03; 8078 EXPECT_EQ("#define x(_a) printf(\"foo\" _a);", 8079 format("#define x(_a) printf(\"foo\"_a);", Style)); 8080 } 8081 8082 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); } 8083 8084 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { 8085 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n" 8086 " \"ddeeefff\");", 8087 format("someFunction(\"aaabbbcccdddeeefff\");", 8088 getLLVMStyleWithColumns(25))); 8089 EXPECT_EQ("someFunction1234567890(\n" 8090 " \"aaabbbcccdddeeefff\");", 8091 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 8092 getLLVMStyleWithColumns(26))); 8093 EXPECT_EQ("someFunction1234567890(\n" 8094 " \"aaabbbcccdddeeeff\"\n" 8095 " \"f\");", 8096 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 8097 getLLVMStyleWithColumns(25))); 8098 EXPECT_EQ("someFunction1234567890(\n" 8099 " \"aaabbbcccdddeeeff\"\n" 8100 " \"f\");", 8101 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 8102 getLLVMStyleWithColumns(24))); 8103 EXPECT_EQ("someFunction(\n" 8104 " \"aaabbbcc ddde \"\n" 8105 " \"efff\");", 8106 format("someFunction(\"aaabbbcc ddde efff\");", 8107 getLLVMStyleWithColumns(25))); 8108 EXPECT_EQ("someFunction(\"aaabbbccc \"\n" 8109 " \"ddeeefff\");", 8110 format("someFunction(\"aaabbbccc ddeeefff\");", 8111 getLLVMStyleWithColumns(25))); 8112 EXPECT_EQ("someFunction1234567890(\n" 8113 " \"aaabb \"\n" 8114 " \"cccdddeeefff\");", 8115 format("someFunction1234567890(\"aaabb cccdddeeefff\");", 8116 getLLVMStyleWithColumns(25))); 8117 EXPECT_EQ("#define A \\\n" 8118 " string s = \\\n" 8119 " \"123456789\" \\\n" 8120 " \"0\"; \\\n" 8121 " int i;", 8122 format("#define A string s = \"1234567890\"; int i;", 8123 getLLVMStyleWithColumns(20))); 8124 EXPECT_EQ("someFunction(\n" 8125 " \"aaabbbcc \"\n" 8126 " \"dddeeefff\");", 8127 format("someFunction(\"aaabbbcc dddeeefff\");", 8128 getLLVMStyleWithColumns(25))); 8129 } 8130 8131 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { 8132 EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3))); 8133 EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2))); 8134 EXPECT_EQ("\"test\"\n" 8135 "\"\\n\"", 8136 format("\"test\\n\"", getLLVMStyleWithColumns(7))); 8137 EXPECT_EQ("\"tes\\\\\"\n" 8138 "\"n\"", 8139 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); 8140 EXPECT_EQ("\"\\\\\\\\\"\n" 8141 "\"\\n\"", 8142 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); 8143 EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7))); 8144 EXPECT_EQ("\"\\uff01\"\n" 8145 "\"test\"", 8146 format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); 8147 EXPECT_EQ("\"\\Uff01ff02\"", 8148 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); 8149 EXPECT_EQ("\"\\x000000000001\"\n" 8150 "\"next\"", 8151 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); 8152 EXPECT_EQ("\"\\x000000000001next\"", 8153 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); 8154 EXPECT_EQ("\"\\x000000000001\"", 8155 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); 8156 EXPECT_EQ("\"test\"\n" 8157 "\"\\000000\"\n" 8158 "\"000001\"", 8159 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); 8160 EXPECT_EQ("\"test\\000\"\n" 8161 "\"00000000\"\n" 8162 "\"1\"", 8163 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); 8164 } 8165 8166 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { 8167 verifyFormat("void f() {\n" 8168 " return g() {}\n" 8169 " void h() {}"); 8170 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n" 8171 "g();\n" 8172 "}"); 8173 } 8174 8175 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) { 8176 verifyFormat( 8177 "void f() { return C{param1, param2}.SomeCall(param1, param2); }"); 8178 } 8179 8180 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { 8181 verifyFormat("class X {\n" 8182 " void f() {\n" 8183 " }\n" 8184 "};", 8185 getLLVMStyleWithColumns(12)); 8186 } 8187 8188 TEST_F(FormatTest, ConfigurableIndentWidth) { 8189 FormatStyle EightIndent = getLLVMStyleWithColumns(18); 8190 EightIndent.IndentWidth = 8; 8191 EightIndent.ContinuationIndentWidth = 8; 8192 verifyFormat("void f() {\n" 8193 " someFunction();\n" 8194 " if (true) {\n" 8195 " f();\n" 8196 " }\n" 8197 "}", 8198 EightIndent); 8199 verifyFormat("class X {\n" 8200 " void f() {\n" 8201 " }\n" 8202 "};", 8203 EightIndent); 8204 verifyFormat("int x[] = {\n" 8205 " call(),\n" 8206 " call()};", 8207 EightIndent); 8208 } 8209 8210 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) { 8211 verifyFormat("double\n" 8212 "f();", 8213 getLLVMStyleWithColumns(8)); 8214 } 8215 8216 TEST_F(FormatTest, ConfigurableUseOfTab) { 8217 FormatStyle Tab = getLLVMStyleWithColumns(42); 8218 Tab.IndentWidth = 8; 8219 Tab.UseTab = FormatStyle::UT_Always; 8220 Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left; 8221 8222 EXPECT_EQ("if (aaaaaaaa && // q\n" 8223 " bb)\t\t// w\n" 8224 "\t;", 8225 format("if (aaaaaaaa &&// q\n" 8226 "bb)// w\n" 8227 ";", 8228 Tab)); 8229 EXPECT_EQ("if (aaa && bbb) // w\n" 8230 "\t;", 8231 format("if(aaa&&bbb)// w\n" 8232 ";", 8233 Tab)); 8234 8235 verifyFormat("class X {\n" 8236 "\tvoid f() {\n" 8237 "\t\tsomeFunction(parameter1,\n" 8238 "\t\t\t parameter2);\n" 8239 "\t}\n" 8240 "};", 8241 Tab); 8242 verifyFormat("#define A \\\n" 8243 "\tvoid f() { \\\n" 8244 "\t\tsomeFunction( \\\n" 8245 "\t\t parameter1, \\\n" 8246 "\t\t parameter2); \\\n" 8247 "\t}", 8248 Tab); 8249 8250 Tab.TabWidth = 4; 8251 Tab.IndentWidth = 8; 8252 verifyFormat("class TabWidth4Indent8 {\n" 8253 "\t\tvoid f() {\n" 8254 "\t\t\t\tsomeFunction(parameter1,\n" 8255 "\t\t\t\t\t\t\t parameter2);\n" 8256 "\t\t}\n" 8257 "};", 8258 Tab); 8259 8260 Tab.TabWidth = 4; 8261 Tab.IndentWidth = 4; 8262 verifyFormat("class TabWidth4Indent4 {\n" 8263 "\tvoid f() {\n" 8264 "\t\tsomeFunction(parameter1,\n" 8265 "\t\t\t\t\t parameter2);\n" 8266 "\t}\n" 8267 "};", 8268 Tab); 8269 8270 Tab.TabWidth = 8; 8271 Tab.IndentWidth = 4; 8272 verifyFormat("class TabWidth8Indent4 {\n" 8273 " void f() {\n" 8274 "\tsomeFunction(parameter1,\n" 8275 "\t\t parameter2);\n" 8276 " }\n" 8277 "};", 8278 Tab); 8279 8280 Tab.TabWidth = 8; 8281 Tab.IndentWidth = 8; 8282 EXPECT_EQ("/*\n" 8283 "\t a\t\tcomment\n" 8284 "\t in multiple lines\n" 8285 " */", 8286 format(" /*\t \t \n" 8287 " \t \t a\t\tcomment\t \t\n" 8288 " \t \t in multiple lines\t\n" 8289 " \t */", 8290 Tab)); 8291 8292 Tab.UseTab = FormatStyle::UT_ForIndentation; 8293 verifyFormat("{\n" 8294 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8295 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8296 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8297 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8298 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8299 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8300 "};", 8301 Tab); 8302 verifyFormat("enum AA {\n" 8303 "\ta1, // Force multiple lines\n" 8304 "\ta2,\n" 8305 "\ta3\n" 8306 "};", 8307 Tab); 8308 EXPECT_EQ("if (aaaaaaaa && // q\n" 8309 " bb) // w\n" 8310 "\t;", 8311 format("if (aaaaaaaa &&// q\n" 8312 "bb)// w\n" 8313 ";", 8314 Tab)); 8315 verifyFormat("class X {\n" 8316 "\tvoid f() {\n" 8317 "\t\tsomeFunction(parameter1,\n" 8318 "\t\t parameter2);\n" 8319 "\t}\n" 8320 "};", 8321 Tab); 8322 verifyFormat("{\n" 8323 "\tQ(\n" 8324 "\t {\n" 8325 "\t\t int a;\n" 8326 "\t\t someFunction(aaaaaaaa,\n" 8327 "\t\t bbbbbbb);\n" 8328 "\t },\n" 8329 "\t p);\n" 8330 "}", 8331 Tab); 8332 EXPECT_EQ("{\n" 8333 "\t/* aaaa\n" 8334 "\t bbbb */\n" 8335 "}", 8336 format("{\n" 8337 "/* aaaa\n" 8338 " bbbb */\n" 8339 "}", 8340 Tab)); 8341 EXPECT_EQ("{\n" 8342 "\t/*\n" 8343 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8344 "\t bbbbbbbbbbbbb\n" 8345 "\t*/\n" 8346 "}", 8347 format("{\n" 8348 "/*\n" 8349 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8350 "*/\n" 8351 "}", 8352 Tab)); 8353 EXPECT_EQ("{\n" 8354 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8355 "\t// bbbbbbbbbbbbb\n" 8356 "}", 8357 format("{\n" 8358 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8359 "}", 8360 Tab)); 8361 EXPECT_EQ("{\n" 8362 "\t/*\n" 8363 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8364 "\t bbbbbbbbbbbbb\n" 8365 "\t*/\n" 8366 "}", 8367 format("{\n" 8368 "\t/*\n" 8369 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8370 "\t*/\n" 8371 "}", 8372 Tab)); 8373 EXPECT_EQ("{\n" 8374 "\t/*\n" 8375 "\n" 8376 "\t*/\n" 8377 "}", 8378 format("{\n" 8379 "\t/*\n" 8380 "\n" 8381 "\t*/\n" 8382 "}", 8383 Tab)); 8384 EXPECT_EQ("{\n" 8385 "\t/*\n" 8386 " asdf\n" 8387 "\t*/\n" 8388 "}", 8389 format("{\n" 8390 "\t/*\n" 8391 " asdf\n" 8392 "\t*/\n" 8393 "}", 8394 Tab)); 8395 8396 Tab.UseTab = FormatStyle::UT_Never; 8397 EXPECT_EQ("/*\n" 8398 " a\t\tcomment\n" 8399 " in multiple lines\n" 8400 " */", 8401 format(" /*\t \t \n" 8402 " \t \t a\t\tcomment\t \t\n" 8403 " \t \t in multiple lines\t\n" 8404 " \t */", 8405 Tab)); 8406 EXPECT_EQ("/* some\n" 8407 " comment */", 8408 format(" \t \t /* some\n" 8409 " \t \t comment */", 8410 Tab)); 8411 EXPECT_EQ("int a; /* some\n" 8412 " comment */", 8413 format(" \t \t int a; /* some\n" 8414 " \t \t comment */", 8415 Tab)); 8416 8417 EXPECT_EQ("int a; /* some\n" 8418 "comment */", 8419 format(" \t \t int\ta; /* some\n" 8420 " \t \t comment */", 8421 Tab)); 8422 EXPECT_EQ("f(\"\t\t\"); /* some\n" 8423 " comment */", 8424 format(" \t \t f(\"\t\t\"); /* some\n" 8425 " \t \t comment */", 8426 Tab)); 8427 EXPECT_EQ("{\n" 8428 " /*\n" 8429 " * Comment\n" 8430 " */\n" 8431 " int i;\n" 8432 "}", 8433 format("{\n" 8434 "\t/*\n" 8435 "\t * Comment\n" 8436 "\t */\n" 8437 "\t int i;\n" 8438 "}")); 8439 8440 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 8441 Tab.TabWidth = 8; 8442 Tab.IndentWidth = 8; 8443 EXPECT_EQ("if (aaaaaaaa && // q\n" 8444 " bb) // w\n" 8445 "\t;", 8446 format("if (aaaaaaaa &&// q\n" 8447 "bb)// w\n" 8448 ";", 8449 Tab)); 8450 EXPECT_EQ("if (aaa && bbb) // w\n" 8451 "\t;", 8452 format("if(aaa&&bbb)// w\n" 8453 ";", 8454 Tab)); 8455 verifyFormat("class X {\n" 8456 "\tvoid f() {\n" 8457 "\t\tsomeFunction(parameter1,\n" 8458 "\t\t\t parameter2);\n" 8459 "\t}\n" 8460 "};", 8461 Tab); 8462 verifyFormat("#define A \\\n" 8463 "\tvoid f() { \\\n" 8464 "\t\tsomeFunction( \\\n" 8465 "\t\t parameter1, \\\n" 8466 "\t\t parameter2); \\\n" 8467 "\t}", 8468 Tab); 8469 Tab.TabWidth = 4; 8470 Tab.IndentWidth = 8; 8471 verifyFormat("class TabWidth4Indent8 {\n" 8472 "\t\tvoid f() {\n" 8473 "\t\t\t\tsomeFunction(parameter1,\n" 8474 "\t\t\t\t\t\t\t parameter2);\n" 8475 "\t\t}\n" 8476 "};", 8477 Tab); 8478 Tab.TabWidth = 4; 8479 Tab.IndentWidth = 4; 8480 verifyFormat("class TabWidth4Indent4 {\n" 8481 "\tvoid f() {\n" 8482 "\t\tsomeFunction(parameter1,\n" 8483 "\t\t\t\t\t parameter2);\n" 8484 "\t}\n" 8485 "};", 8486 Tab); 8487 Tab.TabWidth = 8; 8488 Tab.IndentWidth = 4; 8489 verifyFormat("class TabWidth8Indent4 {\n" 8490 " void f() {\n" 8491 "\tsomeFunction(parameter1,\n" 8492 "\t\t parameter2);\n" 8493 " }\n" 8494 "};", 8495 Tab); 8496 Tab.TabWidth = 8; 8497 Tab.IndentWidth = 8; 8498 EXPECT_EQ("/*\n" 8499 "\t a\t\tcomment\n" 8500 "\t in multiple lines\n" 8501 " */", 8502 format(" /*\t \t \n" 8503 " \t \t a\t\tcomment\t \t\n" 8504 " \t \t in multiple lines\t\n" 8505 " \t */", 8506 Tab)); 8507 verifyFormat("{\n" 8508 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8509 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8510 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8511 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8512 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8513 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8514 "};", 8515 Tab); 8516 verifyFormat("enum AA {\n" 8517 "\ta1, // Force multiple lines\n" 8518 "\ta2,\n" 8519 "\ta3\n" 8520 "};", 8521 Tab); 8522 EXPECT_EQ("if (aaaaaaaa && // q\n" 8523 " bb) // w\n" 8524 "\t;", 8525 format("if (aaaaaaaa &&// q\n" 8526 "bb)// w\n" 8527 ";", 8528 Tab)); 8529 verifyFormat("class X {\n" 8530 "\tvoid f() {\n" 8531 "\t\tsomeFunction(parameter1,\n" 8532 "\t\t\t parameter2);\n" 8533 "\t}\n" 8534 "};", 8535 Tab); 8536 verifyFormat("{\n" 8537 "\tQ(\n" 8538 "\t {\n" 8539 "\t\t int a;\n" 8540 "\t\t someFunction(aaaaaaaa,\n" 8541 "\t\t\t\t bbbbbbb);\n" 8542 "\t },\n" 8543 "\t p);\n" 8544 "}", 8545 Tab); 8546 EXPECT_EQ("{\n" 8547 "\t/* aaaa\n" 8548 "\t bbbb */\n" 8549 "}", 8550 format("{\n" 8551 "/* aaaa\n" 8552 " bbbb */\n" 8553 "}", 8554 Tab)); 8555 EXPECT_EQ("{\n" 8556 "\t/*\n" 8557 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8558 "\t bbbbbbbbbbbbb\n" 8559 "\t*/\n" 8560 "}", 8561 format("{\n" 8562 "/*\n" 8563 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8564 "*/\n" 8565 "}", 8566 Tab)); 8567 EXPECT_EQ("{\n" 8568 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8569 "\t// bbbbbbbbbbbbb\n" 8570 "}", 8571 format("{\n" 8572 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8573 "}", 8574 Tab)); 8575 EXPECT_EQ("{\n" 8576 "\t/*\n" 8577 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8578 "\t bbbbbbbbbbbbb\n" 8579 "\t*/\n" 8580 "}", 8581 format("{\n" 8582 "\t/*\n" 8583 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8584 "\t*/\n" 8585 "}", 8586 Tab)); 8587 EXPECT_EQ("{\n" 8588 "\t/*\n" 8589 "\n" 8590 "\t*/\n" 8591 "}", 8592 format("{\n" 8593 "\t/*\n" 8594 "\n" 8595 "\t*/\n" 8596 "}", 8597 Tab)); 8598 EXPECT_EQ("{\n" 8599 "\t/*\n" 8600 " asdf\n" 8601 "\t*/\n" 8602 "}", 8603 format("{\n" 8604 "\t/*\n" 8605 " asdf\n" 8606 "\t*/\n" 8607 "}", 8608 Tab)); 8609 EXPECT_EQ("/*\n" 8610 "\t a\t\tcomment\n" 8611 "\t in multiple lines\n" 8612 " */", 8613 format(" /*\t \t \n" 8614 " \t \t a\t\tcomment\t \t\n" 8615 " \t \t in multiple lines\t\n" 8616 " \t */", 8617 Tab)); 8618 EXPECT_EQ("/* some\n" 8619 " comment */", 8620 format(" \t \t /* some\n" 8621 " \t \t comment */", 8622 Tab)); 8623 EXPECT_EQ("int a; /* some\n" 8624 " comment */", 8625 format(" \t \t int a; /* some\n" 8626 " \t \t comment */", 8627 Tab)); 8628 EXPECT_EQ("int a; /* some\n" 8629 "comment */", 8630 format(" \t \t int\ta; /* some\n" 8631 " \t \t comment */", 8632 Tab)); 8633 EXPECT_EQ("f(\"\t\t\"); /* some\n" 8634 " comment */", 8635 format(" \t \t f(\"\t\t\"); /* some\n" 8636 " \t \t comment */", 8637 Tab)); 8638 EXPECT_EQ("{\n" 8639 " /*\n" 8640 " * Comment\n" 8641 " */\n" 8642 " int i;\n" 8643 "}", 8644 format("{\n" 8645 "\t/*\n" 8646 "\t * Comment\n" 8647 "\t */\n" 8648 "\t int i;\n" 8649 "}")); 8650 Tab.AlignConsecutiveAssignments = true; 8651 Tab.AlignConsecutiveDeclarations = true; 8652 Tab.TabWidth = 4; 8653 Tab.IndentWidth = 4; 8654 verifyFormat("class Assign {\n" 8655 "\tvoid f() {\n" 8656 "\t\tint x = 123;\n" 8657 "\t\tint random = 4;\n" 8658 "\t\tstd::string alphabet =\n" 8659 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 8660 "\t}\n" 8661 "};", 8662 Tab); 8663 } 8664 8665 TEST_F(FormatTest, CalculatesOriginalColumn) { 8666 EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8667 "q\"; /* some\n" 8668 " comment */", 8669 format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8670 "q\"; /* some\n" 8671 " comment */", 8672 getLLVMStyle())); 8673 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 8674 "/* some\n" 8675 " comment */", 8676 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 8677 " /* some\n" 8678 " comment */", 8679 getLLVMStyle())); 8680 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8681 "qqq\n" 8682 "/* some\n" 8683 " comment */", 8684 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8685 "qqq\n" 8686 " /* some\n" 8687 " comment */", 8688 getLLVMStyle())); 8689 EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8690 "wwww; /* some\n" 8691 " comment */", 8692 format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8693 "wwww; /* some\n" 8694 " comment */", 8695 getLLVMStyle())); 8696 } 8697 8698 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { 8699 FormatStyle NoSpace = getLLVMStyle(); 8700 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never; 8701 8702 verifyFormat("while(true)\n" 8703 " continue;", 8704 NoSpace); 8705 verifyFormat("for(;;)\n" 8706 " continue;", 8707 NoSpace); 8708 verifyFormat("if(true)\n" 8709 " f();\n" 8710 "else if(true)\n" 8711 " f();", 8712 NoSpace); 8713 verifyFormat("do {\n" 8714 " do_something();\n" 8715 "} while(something());", 8716 NoSpace); 8717 verifyFormat("switch(x) {\n" 8718 "default:\n" 8719 " break;\n" 8720 "}", 8721 NoSpace); 8722 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace); 8723 verifyFormat("size_t x = sizeof(x);", NoSpace); 8724 verifyFormat("auto f(int x) -> decltype(x);", NoSpace); 8725 verifyFormat("int f(T x) noexcept(x.create());", NoSpace); 8726 verifyFormat("alignas(128) char a[128];", NoSpace); 8727 verifyFormat("size_t x = alignof(MyType);", NoSpace); 8728 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace); 8729 verifyFormat("int f() throw(Deprecated);", NoSpace); 8730 verifyFormat("typedef void (*cb)(int);", NoSpace); 8731 verifyFormat("T A::operator()();", NoSpace); 8732 verifyFormat("X A::operator++(T);", NoSpace); 8733 8734 FormatStyle Space = getLLVMStyle(); 8735 Space.SpaceBeforeParens = FormatStyle::SBPO_Always; 8736 8737 verifyFormat("int f ();", Space); 8738 verifyFormat("void f (int a, T b) {\n" 8739 " while (true)\n" 8740 " continue;\n" 8741 "}", 8742 Space); 8743 verifyFormat("if (true)\n" 8744 " f ();\n" 8745 "else if (true)\n" 8746 " f ();", 8747 Space); 8748 verifyFormat("do {\n" 8749 " do_something ();\n" 8750 "} while (something ());", 8751 Space); 8752 verifyFormat("switch (x) {\n" 8753 "default:\n" 8754 " break;\n" 8755 "}", 8756 Space); 8757 verifyFormat("A::A () : a (1) {}", Space); 8758 verifyFormat("void f () __attribute__ ((asdf));", Space); 8759 verifyFormat("*(&a + 1);\n" 8760 "&((&a)[1]);\n" 8761 "a[(b + c) * d];\n" 8762 "(((a + 1) * 2) + 3) * 4;", 8763 Space); 8764 verifyFormat("#define A(x) x", Space); 8765 verifyFormat("#define A (x) x", Space); 8766 verifyFormat("#if defined(x)\n" 8767 "#endif", 8768 Space); 8769 verifyFormat("auto i = std::make_unique<int> (5);", Space); 8770 verifyFormat("size_t x = sizeof (x);", Space); 8771 verifyFormat("auto f (int x) -> decltype (x);", Space); 8772 verifyFormat("int f (T x) noexcept (x.create ());", Space); 8773 verifyFormat("alignas (128) char a[128];", Space); 8774 verifyFormat("size_t x = alignof (MyType);", Space); 8775 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space); 8776 verifyFormat("int f () throw (Deprecated);", Space); 8777 verifyFormat("typedef void (*cb) (int);", Space); 8778 verifyFormat("T A::operator() ();", Space); 8779 verifyFormat("X A::operator++ (T);", Space); 8780 } 8781 8782 TEST_F(FormatTest, ConfigurableSpacesInParentheses) { 8783 FormatStyle Spaces = getLLVMStyle(); 8784 8785 Spaces.SpacesInParentheses = true; 8786 verifyFormat("call( x, y, z );", Spaces); 8787 verifyFormat("call();", Spaces); 8788 verifyFormat("std::function<void( int, int )> callback;", Spaces); 8789 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }", 8790 Spaces); 8791 verifyFormat("while ( (bool)1 )\n" 8792 " continue;", 8793 Spaces); 8794 verifyFormat("for ( ;; )\n" 8795 " continue;", 8796 Spaces); 8797 verifyFormat("if ( true )\n" 8798 " f();\n" 8799 "else if ( true )\n" 8800 " f();", 8801 Spaces); 8802 verifyFormat("do {\n" 8803 " do_something( (int)i );\n" 8804 "} while ( something() );", 8805 Spaces); 8806 verifyFormat("switch ( x ) {\n" 8807 "default:\n" 8808 " break;\n" 8809 "}", 8810 Spaces); 8811 8812 Spaces.SpacesInParentheses = false; 8813 Spaces.SpacesInCStyleCastParentheses = true; 8814 verifyFormat("Type *A = ( Type * )P;", Spaces); 8815 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces); 8816 verifyFormat("x = ( int32 )y;", Spaces); 8817 verifyFormat("int a = ( int )(2.0f);", Spaces); 8818 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces); 8819 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces); 8820 verifyFormat("#define x (( int )-1)", Spaces); 8821 8822 // Run the first set of tests again with: 8823 Spaces.SpacesInParentheses = false; 8824 Spaces.SpaceInEmptyParentheses = true; 8825 Spaces.SpacesInCStyleCastParentheses = true; 8826 verifyFormat("call(x, y, z);", Spaces); 8827 verifyFormat("call( );", Spaces); 8828 verifyFormat("std::function<void(int, int)> callback;", Spaces); 8829 verifyFormat("while (( bool )1)\n" 8830 " continue;", 8831 Spaces); 8832 verifyFormat("for (;;)\n" 8833 " continue;", 8834 Spaces); 8835 verifyFormat("if (true)\n" 8836 " f( );\n" 8837 "else if (true)\n" 8838 " f( );", 8839 Spaces); 8840 verifyFormat("do {\n" 8841 " do_something(( int )i);\n" 8842 "} while (something( ));", 8843 Spaces); 8844 verifyFormat("switch (x) {\n" 8845 "default:\n" 8846 " break;\n" 8847 "}", 8848 Spaces); 8849 8850 // Run the first set of tests again with: 8851 Spaces.SpaceAfterCStyleCast = true; 8852 verifyFormat("call(x, y, z);", Spaces); 8853 verifyFormat("call( );", Spaces); 8854 verifyFormat("std::function<void(int, int)> callback;", Spaces); 8855 verifyFormat("while (( bool ) 1)\n" 8856 " continue;", 8857 Spaces); 8858 verifyFormat("for (;;)\n" 8859 " continue;", 8860 Spaces); 8861 verifyFormat("if (true)\n" 8862 " f( );\n" 8863 "else if (true)\n" 8864 " f( );", 8865 Spaces); 8866 verifyFormat("do {\n" 8867 " do_something(( int ) i);\n" 8868 "} while (something( ));", 8869 Spaces); 8870 verifyFormat("switch (x) {\n" 8871 "default:\n" 8872 " break;\n" 8873 "}", 8874 Spaces); 8875 8876 // Run subset of tests again with: 8877 Spaces.SpacesInCStyleCastParentheses = false; 8878 Spaces.SpaceAfterCStyleCast = true; 8879 verifyFormat("while ((bool) 1)\n" 8880 " continue;", 8881 Spaces); 8882 verifyFormat("do {\n" 8883 " do_something((int) i);\n" 8884 "} while (something( ));", 8885 Spaces); 8886 } 8887 8888 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) { 8889 verifyFormat("int a[5];"); 8890 verifyFormat("a[3] += 42;"); 8891 8892 FormatStyle Spaces = getLLVMStyle(); 8893 Spaces.SpacesInSquareBrackets = true; 8894 // Lambdas unchanged. 8895 verifyFormat("int c = []() -> int { return 2; }();\n", Spaces); 8896 verifyFormat("return [i, args...] {};", Spaces); 8897 8898 // Not lambdas. 8899 verifyFormat("int a[ 5 ];", Spaces); 8900 verifyFormat("a[ 3 ] += 42;", Spaces); 8901 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces); 8902 verifyFormat("double &operator[](int i) { return 0; }\n" 8903 "int i;", 8904 Spaces); 8905 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces); 8906 verifyFormat("int i = a[ a ][ a ]->f();", Spaces); 8907 verifyFormat("int i = (*b)[ a ]->f();", Spaces); 8908 } 8909 8910 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) { 8911 verifyFormat("int a = 5;"); 8912 verifyFormat("a += 42;"); 8913 verifyFormat("a or_eq 8;"); 8914 8915 FormatStyle Spaces = getLLVMStyle(); 8916 Spaces.SpaceBeforeAssignmentOperators = false; 8917 verifyFormat("int a= 5;", Spaces); 8918 verifyFormat("a+= 42;", Spaces); 8919 verifyFormat("a or_eq 8;", Spaces); 8920 } 8921 8922 TEST_F(FormatTest, AlignConsecutiveAssignments) { 8923 FormatStyle Alignment = getLLVMStyle(); 8924 Alignment.AlignConsecutiveAssignments = false; 8925 verifyFormat("int a = 5;\n" 8926 "int oneTwoThree = 123;", 8927 Alignment); 8928 verifyFormat("int a = 5;\n" 8929 "int oneTwoThree = 123;", 8930 Alignment); 8931 8932 Alignment.AlignConsecutiveAssignments = true; 8933 verifyFormat("int a = 5;\n" 8934 "int oneTwoThree = 123;", 8935 Alignment); 8936 verifyFormat("int a = method();\n" 8937 "int oneTwoThree = 133;", 8938 Alignment); 8939 verifyFormat("a &= 5;\n" 8940 "bcd *= 5;\n" 8941 "ghtyf += 5;\n" 8942 "dvfvdb -= 5;\n" 8943 "a /= 5;\n" 8944 "vdsvsv %= 5;\n" 8945 "sfdbddfbdfbb ^= 5;\n" 8946 "dvsdsv |= 5;\n" 8947 "int dsvvdvsdvvv = 123;", 8948 Alignment); 8949 verifyFormat("int i = 1, j = 10;\n" 8950 "something = 2000;", 8951 Alignment); 8952 verifyFormat("something = 2000;\n" 8953 "int i = 1, j = 10;\n", 8954 Alignment); 8955 verifyFormat("something = 2000;\n" 8956 "another = 911;\n" 8957 "int i = 1, j = 10;\n" 8958 "oneMore = 1;\n" 8959 "i = 2;", 8960 Alignment); 8961 verifyFormat("int a = 5;\n" 8962 "int one = 1;\n" 8963 "method();\n" 8964 "int oneTwoThree = 123;\n" 8965 "int oneTwo = 12;", 8966 Alignment); 8967 verifyFormat("int oneTwoThree = 123;\n" 8968 "int oneTwo = 12;\n" 8969 "method();\n", 8970 Alignment); 8971 verifyFormat("int oneTwoThree = 123; // comment\n" 8972 "int oneTwo = 12; // comment", 8973 Alignment); 8974 EXPECT_EQ("int a = 5;\n" 8975 "\n" 8976 "int oneTwoThree = 123;", 8977 format("int a = 5;\n" 8978 "\n" 8979 "int oneTwoThree= 123;", 8980 Alignment)); 8981 EXPECT_EQ("int a = 5;\n" 8982 "int one = 1;\n" 8983 "\n" 8984 "int oneTwoThree = 123;", 8985 format("int a = 5;\n" 8986 "int one = 1;\n" 8987 "\n" 8988 "int oneTwoThree = 123;", 8989 Alignment)); 8990 EXPECT_EQ("int a = 5;\n" 8991 "int one = 1;\n" 8992 "\n" 8993 "int oneTwoThree = 123;\n" 8994 "int oneTwo = 12;", 8995 format("int a = 5;\n" 8996 "int one = 1;\n" 8997 "\n" 8998 "int oneTwoThree = 123;\n" 8999 "int oneTwo = 12;", 9000 Alignment)); 9001 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 9002 verifyFormat("#define A \\\n" 9003 " int aaaa = 12; \\\n" 9004 " int b = 23; \\\n" 9005 " int ccc = 234; \\\n" 9006 " int dddddddddd = 2345;", 9007 Alignment); 9008 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 9009 verifyFormat("#define A \\\n" 9010 " int aaaa = 12; \\\n" 9011 " int b = 23; \\\n" 9012 " int ccc = 234; \\\n" 9013 " int dddddddddd = 2345;", 9014 Alignment); 9015 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 9016 verifyFormat("#define A " 9017 " \\\n" 9018 " int aaaa = 12; " 9019 " \\\n" 9020 " int b = 23; " 9021 " \\\n" 9022 " int ccc = 234; " 9023 " \\\n" 9024 " int dddddddddd = 2345;", 9025 Alignment); 9026 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 9027 "k = 4, int l = 5,\n" 9028 " int m = 6) {\n" 9029 " int j = 10;\n" 9030 " otherThing = 1;\n" 9031 "}", 9032 Alignment); 9033 verifyFormat("void SomeFunction(int parameter = 0) {\n" 9034 " int i = 1;\n" 9035 " int j = 2;\n" 9036 " int big = 10000;\n" 9037 "}", 9038 Alignment); 9039 verifyFormat("class C {\n" 9040 "public:\n" 9041 " int i = 1;\n" 9042 " virtual void f() = 0;\n" 9043 "};", 9044 Alignment); 9045 verifyFormat("int i = 1;\n" 9046 "if (SomeType t = getSomething()) {\n" 9047 "}\n" 9048 "int j = 2;\n" 9049 "int big = 10000;", 9050 Alignment); 9051 verifyFormat("int j = 7;\n" 9052 "for (int k = 0; k < N; ++k) {\n" 9053 "}\n" 9054 "int j = 2;\n" 9055 "int big = 10000;\n" 9056 "}", 9057 Alignment); 9058 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 9059 verifyFormat("int i = 1;\n" 9060 "LooooooooooongType loooooooooooooooooooooongVariable\n" 9061 " = someLooooooooooooooooongFunction();\n" 9062 "int j = 2;", 9063 Alignment); 9064 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 9065 verifyFormat("int i = 1;\n" 9066 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 9067 " someLooooooooooooooooongFunction();\n" 9068 "int j = 2;", 9069 Alignment); 9070 9071 verifyFormat("auto lambda = []() {\n" 9072 " auto i = 0;\n" 9073 " return 0;\n" 9074 "};\n" 9075 "int i = 0;\n" 9076 "auto v = type{\n" 9077 " i = 1, //\n" 9078 " (i = 2), //\n" 9079 " i = 3 //\n" 9080 "};", 9081 Alignment); 9082 9083 verifyFormat( 9084 "int i = 1;\n" 9085 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 9086 " loooooooooooooooooooooongParameterB);\n" 9087 "int j = 2;", 9088 Alignment); 9089 9090 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 9091 " typename B = very_long_type_name_1,\n" 9092 " typename T_2 = very_long_type_name_2>\n" 9093 "auto foo() {}\n", 9094 Alignment); 9095 verifyFormat("int a, b = 1;\n" 9096 "int c = 2;\n" 9097 "int dd = 3;\n", 9098 Alignment); 9099 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 9100 "float b[1][] = {{3.f}};\n", 9101 Alignment); 9102 verifyFormat("for (int i = 0; i < 1; i++)\n" 9103 " int x = 1;\n", 9104 Alignment); 9105 verifyFormat("for (i = 0; i < 1; i++)\n" 9106 " x = 1;\n" 9107 "y = 1;\n", 9108 Alignment); 9109 } 9110 9111 TEST_F(FormatTest, AlignConsecutiveDeclarations) { 9112 FormatStyle Alignment = getLLVMStyle(); 9113 Alignment.AlignConsecutiveDeclarations = false; 9114 verifyFormat("float const a = 5;\n" 9115 "int oneTwoThree = 123;", 9116 Alignment); 9117 verifyFormat("int a = 5;\n" 9118 "float const oneTwoThree = 123;", 9119 Alignment); 9120 9121 Alignment.AlignConsecutiveDeclarations = true; 9122 verifyFormat("float const a = 5;\n" 9123 "int oneTwoThree = 123;", 9124 Alignment); 9125 verifyFormat("int a = method();\n" 9126 "float const oneTwoThree = 133;", 9127 Alignment); 9128 verifyFormat("int i = 1, j = 10;\n" 9129 "something = 2000;", 9130 Alignment); 9131 verifyFormat("something = 2000;\n" 9132 "int i = 1, j = 10;\n", 9133 Alignment); 9134 verifyFormat("float something = 2000;\n" 9135 "double another = 911;\n" 9136 "int i = 1, j = 10;\n" 9137 "const int *oneMore = 1;\n" 9138 "unsigned i = 2;", 9139 Alignment); 9140 verifyFormat("float a = 5;\n" 9141 "int one = 1;\n" 9142 "method();\n" 9143 "const double oneTwoThree = 123;\n" 9144 "const unsigned int oneTwo = 12;", 9145 Alignment); 9146 verifyFormat("int oneTwoThree{0}; // comment\n" 9147 "unsigned oneTwo; // comment", 9148 Alignment); 9149 EXPECT_EQ("float const a = 5;\n" 9150 "\n" 9151 "int oneTwoThree = 123;", 9152 format("float const a = 5;\n" 9153 "\n" 9154 "int oneTwoThree= 123;", 9155 Alignment)); 9156 EXPECT_EQ("float a = 5;\n" 9157 "int one = 1;\n" 9158 "\n" 9159 "unsigned oneTwoThree = 123;", 9160 format("float a = 5;\n" 9161 "int one = 1;\n" 9162 "\n" 9163 "unsigned oneTwoThree = 123;", 9164 Alignment)); 9165 EXPECT_EQ("float a = 5;\n" 9166 "int one = 1;\n" 9167 "\n" 9168 "unsigned oneTwoThree = 123;\n" 9169 "int oneTwo = 12;", 9170 format("float a = 5;\n" 9171 "int one = 1;\n" 9172 "\n" 9173 "unsigned oneTwoThree = 123;\n" 9174 "int oneTwo = 12;", 9175 Alignment)); 9176 // Function prototype alignment 9177 verifyFormat("int a();\n" 9178 "double b();", 9179 Alignment); 9180 verifyFormat("int a(int x);\n" 9181 "double b();", 9182 Alignment); 9183 unsigned OldColumnLimit = Alignment.ColumnLimit; 9184 // We need to set ColumnLimit to zero, in order to stress nested alignments, 9185 // otherwise the function parameters will be re-flowed onto a single line. 9186 Alignment.ColumnLimit = 0; 9187 EXPECT_EQ("int a(int x,\n" 9188 " float y);\n" 9189 "double b(int x,\n" 9190 " double y);", 9191 format("int a(int x,\n" 9192 " float y);\n" 9193 "double b(int x,\n" 9194 " double y);", 9195 Alignment)); 9196 // This ensures that function parameters of function declarations are 9197 // correctly indented when their owning functions are indented. 9198 // The failure case here is for 'double y' to not be indented enough. 9199 EXPECT_EQ("double a(int x);\n" 9200 "int b(int y,\n" 9201 " double z);", 9202 format("double a(int x);\n" 9203 "int b(int y,\n" 9204 " double z);", 9205 Alignment)); 9206 // Set ColumnLimit low so that we induce wrapping immediately after 9207 // the function name and opening paren. 9208 Alignment.ColumnLimit = 13; 9209 verifyFormat("int function(\n" 9210 " int x,\n" 9211 " bool y);", 9212 Alignment); 9213 Alignment.ColumnLimit = OldColumnLimit; 9214 // Ensure function pointers don't screw up recursive alignment 9215 verifyFormat("int a(int x, void (*fp)(int y));\n" 9216 "double b();", 9217 Alignment); 9218 Alignment.AlignConsecutiveAssignments = true; 9219 // Ensure recursive alignment is broken by function braces, so that the 9220 // "a = 1" does not align with subsequent assignments inside the function 9221 // body. 9222 verifyFormat("int func(int a = 1) {\n" 9223 " int b = 2;\n" 9224 " int cc = 3;\n" 9225 "}", 9226 Alignment); 9227 verifyFormat("float something = 2000;\n" 9228 "double another = 911;\n" 9229 "int i = 1, j = 10;\n" 9230 "const int *oneMore = 1;\n" 9231 "unsigned i = 2;", 9232 Alignment); 9233 verifyFormat("int oneTwoThree = {0}; // comment\n" 9234 "unsigned oneTwo = 0; // comment", 9235 Alignment); 9236 // Make sure that scope is correctly tracked, in the absence of braces 9237 verifyFormat("for (int i = 0; i < n; i++)\n" 9238 " j = i;\n" 9239 "double x = 1;\n", 9240 Alignment); 9241 verifyFormat("if (int i = 0)\n" 9242 " j = i;\n" 9243 "double x = 1;\n", 9244 Alignment); 9245 // Ensure operator[] and operator() are comprehended 9246 verifyFormat("struct test {\n" 9247 " long long int foo();\n" 9248 " int operator[](int a);\n" 9249 " double bar();\n" 9250 "};\n", 9251 Alignment); 9252 verifyFormat("struct test {\n" 9253 " long long int foo();\n" 9254 " int operator()(int a);\n" 9255 " double bar();\n" 9256 "};\n", 9257 Alignment); 9258 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 9259 " int const i = 1;\n" 9260 " int * j = 2;\n" 9261 " int big = 10000;\n" 9262 "\n" 9263 " unsigned oneTwoThree = 123;\n" 9264 " int oneTwo = 12;\n" 9265 " method();\n" 9266 " float k = 2;\n" 9267 " int ll = 10000;\n" 9268 "}", 9269 format("void SomeFunction(int parameter= 0) {\n" 9270 " int const i= 1;\n" 9271 " int *j=2;\n" 9272 " int big = 10000;\n" 9273 "\n" 9274 "unsigned oneTwoThree =123;\n" 9275 "int oneTwo = 12;\n" 9276 " method();\n" 9277 "float k= 2;\n" 9278 "int ll=10000;\n" 9279 "}", 9280 Alignment)); 9281 Alignment.AlignConsecutiveAssignments = false; 9282 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 9283 verifyFormat("#define A \\\n" 9284 " int aaaa = 12; \\\n" 9285 " float b = 23; \\\n" 9286 " const int ccc = 234; \\\n" 9287 " unsigned dddddddddd = 2345;", 9288 Alignment); 9289 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 9290 verifyFormat("#define A \\\n" 9291 " int aaaa = 12; \\\n" 9292 " float b = 23; \\\n" 9293 " const int ccc = 234; \\\n" 9294 " unsigned dddddddddd = 2345;", 9295 Alignment); 9296 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 9297 Alignment.ColumnLimit = 30; 9298 verifyFormat("#define A \\\n" 9299 " int aaaa = 12; \\\n" 9300 " float b = 23; \\\n" 9301 " const int ccc = 234; \\\n" 9302 " int dddddddddd = 2345;", 9303 Alignment); 9304 Alignment.ColumnLimit = 80; 9305 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 9306 "k = 4, int l = 5,\n" 9307 " int m = 6) {\n" 9308 " const int j = 10;\n" 9309 " otherThing = 1;\n" 9310 "}", 9311 Alignment); 9312 verifyFormat("void SomeFunction(int parameter = 0) {\n" 9313 " int const i = 1;\n" 9314 " int * j = 2;\n" 9315 " int big = 10000;\n" 9316 "}", 9317 Alignment); 9318 verifyFormat("class C {\n" 9319 "public:\n" 9320 " int i = 1;\n" 9321 " virtual void f() = 0;\n" 9322 "};", 9323 Alignment); 9324 verifyFormat("float i = 1;\n" 9325 "if (SomeType t = getSomething()) {\n" 9326 "}\n" 9327 "const unsigned j = 2;\n" 9328 "int big = 10000;", 9329 Alignment); 9330 verifyFormat("float j = 7;\n" 9331 "for (int k = 0; k < N; ++k) {\n" 9332 "}\n" 9333 "unsigned j = 2;\n" 9334 "int big = 10000;\n" 9335 "}", 9336 Alignment); 9337 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 9338 verifyFormat("float i = 1;\n" 9339 "LooooooooooongType loooooooooooooooooooooongVariable\n" 9340 " = someLooooooooooooooooongFunction();\n" 9341 "int j = 2;", 9342 Alignment); 9343 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 9344 verifyFormat("int i = 1;\n" 9345 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 9346 " someLooooooooooooooooongFunction();\n" 9347 "int j = 2;", 9348 Alignment); 9349 9350 Alignment.AlignConsecutiveAssignments = true; 9351 verifyFormat("auto lambda = []() {\n" 9352 " auto ii = 0;\n" 9353 " float j = 0;\n" 9354 " return 0;\n" 9355 "};\n" 9356 "int i = 0;\n" 9357 "float i2 = 0;\n" 9358 "auto v = type{\n" 9359 " i = 1, //\n" 9360 " (i = 2), //\n" 9361 " i = 3 //\n" 9362 "};", 9363 Alignment); 9364 Alignment.AlignConsecutiveAssignments = false; 9365 9366 verifyFormat( 9367 "int i = 1;\n" 9368 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 9369 " loooooooooooooooooooooongParameterB);\n" 9370 "int j = 2;", 9371 Alignment); 9372 9373 // Test interactions with ColumnLimit and AlignConsecutiveAssignments: 9374 // We expect declarations and assignments to align, as long as it doesn't 9375 // exceed the column limit, starting a new alignment sequence whenever it 9376 // happens. 9377 Alignment.AlignConsecutiveAssignments = true; 9378 Alignment.ColumnLimit = 30; 9379 verifyFormat("float ii = 1;\n" 9380 "unsigned j = 2;\n" 9381 "int someVerylongVariable = 1;\n" 9382 "AnotherLongType ll = 123456;\n" 9383 "VeryVeryLongType k = 2;\n" 9384 "int myvar = 1;", 9385 Alignment); 9386 Alignment.ColumnLimit = 80; 9387 Alignment.AlignConsecutiveAssignments = false; 9388 9389 verifyFormat( 9390 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n" 9391 " typename LongType, typename B>\n" 9392 "auto foo() {}\n", 9393 Alignment); 9394 verifyFormat("float a, b = 1;\n" 9395 "int c = 2;\n" 9396 "int dd = 3;\n", 9397 Alignment); 9398 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 9399 "float b[1][] = {{3.f}};\n", 9400 Alignment); 9401 Alignment.AlignConsecutiveAssignments = true; 9402 verifyFormat("float a, b = 1;\n" 9403 "int c = 2;\n" 9404 "int dd = 3;\n", 9405 Alignment); 9406 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 9407 "float b[1][] = {{3.f}};\n", 9408 Alignment); 9409 Alignment.AlignConsecutiveAssignments = false; 9410 9411 Alignment.ColumnLimit = 30; 9412 Alignment.BinPackParameters = false; 9413 verifyFormat("void foo(float a,\n" 9414 " float b,\n" 9415 " int c,\n" 9416 " uint32_t *d) {\n" 9417 " int * e = 0;\n" 9418 " float f = 0;\n" 9419 " double g = 0;\n" 9420 "}\n" 9421 "void bar(ino_t a,\n" 9422 " int b,\n" 9423 " uint32_t *c,\n" 9424 " bool d) {}\n", 9425 Alignment); 9426 Alignment.BinPackParameters = true; 9427 Alignment.ColumnLimit = 80; 9428 9429 // Bug 33507 9430 Alignment.PointerAlignment = FormatStyle::PAS_Middle; 9431 verifyFormat( 9432 "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n" 9433 " static const Version verVs2017;\n" 9434 " return true;\n" 9435 "});\n", 9436 Alignment); 9437 Alignment.PointerAlignment = FormatStyle::PAS_Right; 9438 } 9439 9440 TEST_F(FormatTest, LinuxBraceBreaking) { 9441 FormatStyle LinuxBraceStyle = getLLVMStyle(); 9442 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux; 9443 verifyFormat("namespace a\n" 9444 "{\n" 9445 "class A\n" 9446 "{\n" 9447 " void f()\n" 9448 " {\n" 9449 " if (true) {\n" 9450 " a();\n" 9451 " b();\n" 9452 " } else {\n" 9453 " a();\n" 9454 " }\n" 9455 " }\n" 9456 " void g() { return; }\n" 9457 "};\n" 9458 "struct B {\n" 9459 " int x;\n" 9460 "};\n" 9461 "} // namespace a\n", 9462 LinuxBraceStyle); 9463 verifyFormat("enum X {\n" 9464 " Y = 0,\n" 9465 "}\n", 9466 LinuxBraceStyle); 9467 verifyFormat("struct S {\n" 9468 " int Type;\n" 9469 " union {\n" 9470 " int x;\n" 9471 " double y;\n" 9472 " } Value;\n" 9473 " class C\n" 9474 " {\n" 9475 " MyFavoriteType Value;\n" 9476 " } Class;\n" 9477 "}\n", 9478 LinuxBraceStyle); 9479 } 9480 9481 TEST_F(FormatTest, MozillaBraceBreaking) { 9482 FormatStyle MozillaBraceStyle = getLLVMStyle(); 9483 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; 9484 MozillaBraceStyle.FixNamespaceComments = false; 9485 verifyFormat("namespace a {\n" 9486 "class A\n" 9487 "{\n" 9488 " void f()\n" 9489 " {\n" 9490 " if (true) {\n" 9491 " a();\n" 9492 " b();\n" 9493 " }\n" 9494 " }\n" 9495 " void g() { return; }\n" 9496 "};\n" 9497 "enum E\n" 9498 "{\n" 9499 " A,\n" 9500 " // foo\n" 9501 " B,\n" 9502 " C\n" 9503 "};\n" 9504 "struct B\n" 9505 "{\n" 9506 " int x;\n" 9507 "};\n" 9508 "}\n", 9509 MozillaBraceStyle); 9510 verifyFormat("struct S\n" 9511 "{\n" 9512 " int Type;\n" 9513 " union\n" 9514 " {\n" 9515 " int x;\n" 9516 " double y;\n" 9517 " } Value;\n" 9518 " class C\n" 9519 " {\n" 9520 " MyFavoriteType Value;\n" 9521 " } Class;\n" 9522 "}\n", 9523 MozillaBraceStyle); 9524 } 9525 9526 TEST_F(FormatTest, StroustrupBraceBreaking) { 9527 FormatStyle StroustrupBraceStyle = getLLVMStyle(); 9528 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 9529 verifyFormat("namespace a {\n" 9530 "class A {\n" 9531 " void f()\n" 9532 " {\n" 9533 " if (true) {\n" 9534 " a();\n" 9535 " b();\n" 9536 " }\n" 9537 " }\n" 9538 " void g() { return; }\n" 9539 "};\n" 9540 "struct B {\n" 9541 " int x;\n" 9542 "};\n" 9543 "} // namespace a\n", 9544 StroustrupBraceStyle); 9545 9546 verifyFormat("void foo()\n" 9547 "{\n" 9548 " if (a) {\n" 9549 " a();\n" 9550 " }\n" 9551 " else {\n" 9552 " b();\n" 9553 " }\n" 9554 "}\n", 9555 StroustrupBraceStyle); 9556 9557 verifyFormat("#ifdef _DEBUG\n" 9558 "int foo(int i = 0)\n" 9559 "#else\n" 9560 "int foo(int i = 5)\n" 9561 "#endif\n" 9562 "{\n" 9563 " return i;\n" 9564 "}", 9565 StroustrupBraceStyle); 9566 9567 verifyFormat("void foo() {}\n" 9568 "void bar()\n" 9569 "#ifdef _DEBUG\n" 9570 "{\n" 9571 " foo();\n" 9572 "}\n" 9573 "#else\n" 9574 "{\n" 9575 "}\n" 9576 "#endif", 9577 StroustrupBraceStyle); 9578 9579 verifyFormat("void foobar() { int i = 5; }\n" 9580 "#ifdef _DEBUG\n" 9581 "void bar() {}\n" 9582 "#else\n" 9583 "void bar() { foobar(); }\n" 9584 "#endif", 9585 StroustrupBraceStyle); 9586 } 9587 9588 TEST_F(FormatTest, AllmanBraceBreaking) { 9589 FormatStyle AllmanBraceStyle = getLLVMStyle(); 9590 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman; 9591 9592 EXPECT_EQ("namespace a\n" 9593 "{\n" 9594 "void f();\n" 9595 "void g();\n" 9596 "} // namespace a\n", 9597 format("namespace a\n" 9598 "{\n" 9599 "void f();\n" 9600 "void g();\n" 9601 "}\n", 9602 AllmanBraceStyle)); 9603 9604 verifyFormat("namespace a\n" 9605 "{\n" 9606 "class A\n" 9607 "{\n" 9608 " void f()\n" 9609 " {\n" 9610 " if (true)\n" 9611 " {\n" 9612 " a();\n" 9613 " b();\n" 9614 " }\n" 9615 " }\n" 9616 " void g() { return; }\n" 9617 "};\n" 9618 "struct B\n" 9619 "{\n" 9620 " int x;\n" 9621 "};\n" 9622 "} // namespace a", 9623 AllmanBraceStyle); 9624 9625 verifyFormat("void f()\n" 9626 "{\n" 9627 " if (true)\n" 9628 " {\n" 9629 " a();\n" 9630 " }\n" 9631 " else if (false)\n" 9632 " {\n" 9633 " b();\n" 9634 " }\n" 9635 " else\n" 9636 " {\n" 9637 " c();\n" 9638 " }\n" 9639 "}\n", 9640 AllmanBraceStyle); 9641 9642 verifyFormat("void f()\n" 9643 "{\n" 9644 " for (int i = 0; i < 10; ++i)\n" 9645 " {\n" 9646 " a();\n" 9647 " }\n" 9648 " while (false)\n" 9649 " {\n" 9650 " b();\n" 9651 " }\n" 9652 " do\n" 9653 " {\n" 9654 " c();\n" 9655 " } while (false)\n" 9656 "}\n", 9657 AllmanBraceStyle); 9658 9659 verifyFormat("void f(int a)\n" 9660 "{\n" 9661 " switch (a)\n" 9662 " {\n" 9663 " case 0:\n" 9664 " break;\n" 9665 " case 1:\n" 9666 " {\n" 9667 " break;\n" 9668 " }\n" 9669 " case 2:\n" 9670 " {\n" 9671 " }\n" 9672 " break;\n" 9673 " default:\n" 9674 " break;\n" 9675 " }\n" 9676 "}\n", 9677 AllmanBraceStyle); 9678 9679 verifyFormat("enum X\n" 9680 "{\n" 9681 " Y = 0,\n" 9682 "}\n", 9683 AllmanBraceStyle); 9684 verifyFormat("enum X\n" 9685 "{\n" 9686 " Y = 0\n" 9687 "}\n", 9688 AllmanBraceStyle); 9689 9690 verifyFormat("@interface BSApplicationController ()\n" 9691 "{\n" 9692 "@private\n" 9693 " id _extraIvar;\n" 9694 "}\n" 9695 "@end\n", 9696 AllmanBraceStyle); 9697 9698 verifyFormat("#ifdef _DEBUG\n" 9699 "int foo(int i = 0)\n" 9700 "#else\n" 9701 "int foo(int i = 5)\n" 9702 "#endif\n" 9703 "{\n" 9704 " return i;\n" 9705 "}", 9706 AllmanBraceStyle); 9707 9708 verifyFormat("void foo() {}\n" 9709 "void bar()\n" 9710 "#ifdef _DEBUG\n" 9711 "{\n" 9712 " foo();\n" 9713 "}\n" 9714 "#else\n" 9715 "{\n" 9716 "}\n" 9717 "#endif", 9718 AllmanBraceStyle); 9719 9720 verifyFormat("void foobar() { int i = 5; }\n" 9721 "#ifdef _DEBUG\n" 9722 "void bar() {}\n" 9723 "#else\n" 9724 "void bar() { foobar(); }\n" 9725 "#endif", 9726 AllmanBraceStyle); 9727 9728 // This shouldn't affect ObjC blocks.. 9729 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 9730 " // ...\n" 9731 " int i;\n" 9732 "}];", 9733 AllmanBraceStyle); 9734 verifyFormat("void (^block)(void) = ^{\n" 9735 " // ...\n" 9736 " int i;\n" 9737 "};", 9738 AllmanBraceStyle); 9739 // .. or dict literals. 9740 verifyFormat("void f()\n" 9741 "{\n" 9742 " // ...\n" 9743 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 9744 "}", 9745 AllmanBraceStyle); 9746 verifyFormat("void f()\n" 9747 "{\n" 9748 " // ...\n" 9749 " [object someMethod:@{a : @\"b\"}];\n" 9750 "}", 9751 AllmanBraceStyle); 9752 verifyFormat("int f()\n" 9753 "{ // comment\n" 9754 " return 42;\n" 9755 "}", 9756 AllmanBraceStyle); 9757 9758 AllmanBraceStyle.ColumnLimit = 19; 9759 verifyFormat("void f() { int i; }", AllmanBraceStyle); 9760 AllmanBraceStyle.ColumnLimit = 18; 9761 verifyFormat("void f()\n" 9762 "{\n" 9763 " int i;\n" 9764 "}", 9765 AllmanBraceStyle); 9766 AllmanBraceStyle.ColumnLimit = 80; 9767 9768 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle; 9769 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true; 9770 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 9771 verifyFormat("void f(bool b)\n" 9772 "{\n" 9773 " if (b)\n" 9774 " {\n" 9775 " return;\n" 9776 " }\n" 9777 "}\n", 9778 BreakBeforeBraceShortIfs); 9779 verifyFormat("void f(bool b)\n" 9780 "{\n" 9781 " if constexpr (b)\n" 9782 " {\n" 9783 " return;\n" 9784 " }\n" 9785 "}\n", 9786 BreakBeforeBraceShortIfs); 9787 verifyFormat("void f(bool b)\n" 9788 "{\n" 9789 " if (b) return;\n" 9790 "}\n", 9791 BreakBeforeBraceShortIfs); 9792 verifyFormat("void f(bool b)\n" 9793 "{\n" 9794 " if constexpr (b) return;\n" 9795 "}\n", 9796 BreakBeforeBraceShortIfs); 9797 verifyFormat("void f(bool b)\n" 9798 "{\n" 9799 " while (b)\n" 9800 " {\n" 9801 " return;\n" 9802 " }\n" 9803 "}\n", 9804 BreakBeforeBraceShortIfs); 9805 } 9806 9807 TEST_F(FormatTest, GNUBraceBreaking) { 9808 FormatStyle GNUBraceStyle = getLLVMStyle(); 9809 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU; 9810 verifyFormat("namespace a\n" 9811 "{\n" 9812 "class A\n" 9813 "{\n" 9814 " void f()\n" 9815 " {\n" 9816 " int a;\n" 9817 " {\n" 9818 " int b;\n" 9819 " }\n" 9820 " if (true)\n" 9821 " {\n" 9822 " a();\n" 9823 " b();\n" 9824 " }\n" 9825 " }\n" 9826 " void g() { return; }\n" 9827 "}\n" 9828 "} // namespace a", 9829 GNUBraceStyle); 9830 9831 verifyFormat("void f()\n" 9832 "{\n" 9833 " if (true)\n" 9834 " {\n" 9835 " a();\n" 9836 " }\n" 9837 " else if (false)\n" 9838 " {\n" 9839 " b();\n" 9840 " }\n" 9841 " else\n" 9842 " {\n" 9843 " c();\n" 9844 " }\n" 9845 "}\n", 9846 GNUBraceStyle); 9847 9848 verifyFormat("void f()\n" 9849 "{\n" 9850 " for (int i = 0; i < 10; ++i)\n" 9851 " {\n" 9852 " a();\n" 9853 " }\n" 9854 " while (false)\n" 9855 " {\n" 9856 " b();\n" 9857 " }\n" 9858 " do\n" 9859 " {\n" 9860 " c();\n" 9861 " }\n" 9862 " while (false);\n" 9863 "}\n", 9864 GNUBraceStyle); 9865 9866 verifyFormat("void f(int a)\n" 9867 "{\n" 9868 " switch (a)\n" 9869 " {\n" 9870 " case 0:\n" 9871 " break;\n" 9872 " case 1:\n" 9873 " {\n" 9874 " break;\n" 9875 " }\n" 9876 " case 2:\n" 9877 " {\n" 9878 " }\n" 9879 " break;\n" 9880 " default:\n" 9881 " break;\n" 9882 " }\n" 9883 "}\n", 9884 GNUBraceStyle); 9885 9886 verifyFormat("enum X\n" 9887 "{\n" 9888 " Y = 0,\n" 9889 "}\n", 9890 GNUBraceStyle); 9891 9892 verifyFormat("@interface BSApplicationController ()\n" 9893 "{\n" 9894 "@private\n" 9895 " id _extraIvar;\n" 9896 "}\n" 9897 "@end\n", 9898 GNUBraceStyle); 9899 9900 verifyFormat("#ifdef _DEBUG\n" 9901 "int foo(int i = 0)\n" 9902 "#else\n" 9903 "int foo(int i = 5)\n" 9904 "#endif\n" 9905 "{\n" 9906 " return i;\n" 9907 "}", 9908 GNUBraceStyle); 9909 9910 verifyFormat("void foo() {}\n" 9911 "void bar()\n" 9912 "#ifdef _DEBUG\n" 9913 "{\n" 9914 " foo();\n" 9915 "}\n" 9916 "#else\n" 9917 "{\n" 9918 "}\n" 9919 "#endif", 9920 GNUBraceStyle); 9921 9922 verifyFormat("void foobar() { int i = 5; }\n" 9923 "#ifdef _DEBUG\n" 9924 "void bar() {}\n" 9925 "#else\n" 9926 "void bar() { foobar(); }\n" 9927 "#endif", 9928 GNUBraceStyle); 9929 } 9930 9931 TEST_F(FormatTest, WebKitBraceBreaking) { 9932 FormatStyle WebKitBraceStyle = getLLVMStyle(); 9933 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit; 9934 WebKitBraceStyle.FixNamespaceComments = false; 9935 verifyFormat("namespace a {\n" 9936 "class A {\n" 9937 " void f()\n" 9938 " {\n" 9939 " if (true) {\n" 9940 " a();\n" 9941 " b();\n" 9942 " }\n" 9943 " }\n" 9944 " void g() { return; }\n" 9945 "};\n" 9946 "enum E {\n" 9947 " A,\n" 9948 " // foo\n" 9949 " B,\n" 9950 " C\n" 9951 "};\n" 9952 "struct B {\n" 9953 " int x;\n" 9954 "};\n" 9955 "}\n", 9956 WebKitBraceStyle); 9957 verifyFormat("struct S {\n" 9958 " int Type;\n" 9959 " union {\n" 9960 " int x;\n" 9961 " double y;\n" 9962 " } Value;\n" 9963 " class C {\n" 9964 " MyFavoriteType Value;\n" 9965 " } Class;\n" 9966 "};\n", 9967 WebKitBraceStyle); 9968 } 9969 9970 TEST_F(FormatTest, CatchExceptionReferenceBinding) { 9971 verifyFormat("void f() {\n" 9972 " try {\n" 9973 " } catch (const Exception &e) {\n" 9974 " }\n" 9975 "}\n", 9976 getLLVMStyle()); 9977 } 9978 9979 TEST_F(FormatTest, UnderstandsPragmas) { 9980 verifyFormat("#pragma omp reduction(| : var)"); 9981 verifyFormat("#pragma omp reduction(+ : var)"); 9982 9983 EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string " 9984 "(including parentheses).", 9985 format("#pragma mark Any non-hyphenated or hyphenated string " 9986 "(including parentheses).")); 9987 } 9988 9989 TEST_F(FormatTest, UnderstandPragmaOption) { 9990 verifyFormat("#pragma option -C -A"); 9991 9992 EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A")); 9993 } 9994 9995 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) { 9996 FormatStyle Style = getLLVMStyle(); 9997 Style.ColumnLimit = 20; 9998 9999 verifyFormat("int a; // the\n" 10000 " // comment", Style); 10001 EXPECT_EQ("int a; /* first line\n" 10002 " * second\n" 10003 " * line third\n" 10004 " * line\n" 10005 " */", 10006 format("int a; /* first line\n" 10007 " * second\n" 10008 " * line third\n" 10009 " * line\n" 10010 " */", 10011 Style)); 10012 EXPECT_EQ("int a; // first line\n" 10013 " // second\n" 10014 " // line third\n" 10015 " // line", 10016 format("int a; // first line\n" 10017 " // second line\n" 10018 " // third line", 10019 Style)); 10020 10021 Style.PenaltyExcessCharacter = 90; 10022 verifyFormat("int a; // the comment", Style); 10023 EXPECT_EQ("int a; // the comment\n" 10024 " // aaa", 10025 format("int a; // the comment aaa", Style)); 10026 EXPECT_EQ("int a; /* first line\n" 10027 " * second line\n" 10028 " * third line\n" 10029 " */", 10030 format("int a; /* first line\n" 10031 " * second line\n" 10032 " * third line\n" 10033 " */", 10034 Style)); 10035 EXPECT_EQ("int a; // first line\n" 10036 " // second line\n" 10037 " // third line", 10038 format("int a; // first line\n" 10039 " // second line\n" 10040 " // third line", 10041 Style)); 10042 // FIXME: Investigate why this is not getting the same layout as the test 10043 // above. 10044 EXPECT_EQ("int a; /* first line\n" 10045 " * second line\n" 10046 " * third line\n" 10047 " */", 10048 format("int a; /* first line second line third line" 10049 "\n*/", 10050 Style)); 10051 10052 EXPECT_EQ("// foo bar baz bazfoo\n" 10053 "// foo bar foo bar\n", 10054 format("// foo bar baz bazfoo\n" 10055 "// foo bar foo bar\n", 10056 Style)); 10057 EXPECT_EQ("// foo bar baz bazfoo\n" 10058 "// foo bar foo bar\n", 10059 format("// foo bar baz bazfoo\n" 10060 "// foo bar foo bar\n", 10061 Style)); 10062 10063 // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the 10064 // next one. 10065 EXPECT_EQ("// foo bar baz bazfoo\n" 10066 "// bar foo bar\n", 10067 format("// foo bar baz bazfoo bar\n" 10068 "// foo bar\n", 10069 Style)); 10070 10071 EXPECT_EQ("// foo bar baz bazfoo\n" 10072 "// foo bar baz bazfoo\n" 10073 "// bar foo bar\n", 10074 format("// foo bar baz bazfoo\n" 10075 "// foo bar baz bazfoo bar\n" 10076 "// foo bar\n", 10077 Style)); 10078 10079 EXPECT_EQ("// foo bar baz bazfoo\n" 10080 "// foo bar baz bazfoo\n" 10081 "// bar foo bar\n", 10082 format("// foo bar baz bazfoo\n" 10083 "// foo bar baz bazfoo bar\n" 10084 "// foo bar\n", 10085 Style)); 10086 10087 // Make sure we do not keep protruding characters if strict mode reflow is 10088 // cheaper than keeping protruding characters. 10089 Style.ColumnLimit = 21; 10090 EXPECT_EQ("// foo foo foo foo\n" 10091 "// foo foo foo foo\n" 10092 "// foo foo foo foo\n", 10093 format("// foo foo foo foo foo foo foo foo foo foo foo foo\n", 10094 Style)); 10095 10096 EXPECT_EQ("int a = /* long block\n" 10097 " comment */\n" 10098 " 42;", 10099 format("int a = /* long block comment */ 42;", Style)); 10100 } 10101 10102 #define EXPECT_ALL_STYLES_EQUAL(Styles) \ 10103 for (size_t i = 1; i < Styles.size(); ++i) \ 10104 EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \ 10105 << " differs from Style #0" 10106 10107 TEST_F(FormatTest, GetsPredefinedStyleByName) { 10108 SmallVector<FormatStyle, 3> Styles; 10109 Styles.resize(3); 10110 10111 Styles[0] = getLLVMStyle(); 10112 EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1])); 10113 EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2])); 10114 EXPECT_ALL_STYLES_EQUAL(Styles); 10115 10116 Styles[0] = getGoogleStyle(); 10117 EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1])); 10118 EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2])); 10119 EXPECT_ALL_STYLES_EQUAL(Styles); 10120 10121 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 10122 EXPECT_TRUE( 10123 getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1])); 10124 EXPECT_TRUE( 10125 getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2])); 10126 EXPECT_ALL_STYLES_EQUAL(Styles); 10127 10128 Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp); 10129 EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1])); 10130 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2])); 10131 EXPECT_ALL_STYLES_EQUAL(Styles); 10132 10133 Styles[0] = getMozillaStyle(); 10134 EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1])); 10135 EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2])); 10136 EXPECT_ALL_STYLES_EQUAL(Styles); 10137 10138 Styles[0] = getWebKitStyle(); 10139 EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1])); 10140 EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2])); 10141 EXPECT_ALL_STYLES_EQUAL(Styles); 10142 10143 Styles[0] = getGNUStyle(); 10144 EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1])); 10145 EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2])); 10146 EXPECT_ALL_STYLES_EQUAL(Styles); 10147 10148 EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0])); 10149 } 10150 10151 TEST_F(FormatTest, GetsCorrectBasedOnStyle) { 10152 SmallVector<FormatStyle, 8> Styles; 10153 Styles.resize(2); 10154 10155 Styles[0] = getGoogleStyle(); 10156 Styles[1] = getLLVMStyle(); 10157 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 10158 EXPECT_ALL_STYLES_EQUAL(Styles); 10159 10160 Styles.resize(5); 10161 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 10162 Styles[1] = getLLVMStyle(); 10163 Styles[1].Language = FormatStyle::LK_JavaScript; 10164 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 10165 10166 Styles[2] = getLLVMStyle(); 10167 Styles[2].Language = FormatStyle::LK_JavaScript; 10168 EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n" 10169 "BasedOnStyle: Google", 10170 &Styles[2]) 10171 .value()); 10172 10173 Styles[3] = getLLVMStyle(); 10174 Styles[3].Language = FormatStyle::LK_JavaScript; 10175 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n" 10176 "Language: JavaScript", 10177 &Styles[3]) 10178 .value()); 10179 10180 Styles[4] = getLLVMStyle(); 10181 Styles[4].Language = FormatStyle::LK_JavaScript; 10182 EXPECT_EQ(0, parseConfiguration("---\n" 10183 "BasedOnStyle: LLVM\n" 10184 "IndentWidth: 123\n" 10185 "---\n" 10186 "BasedOnStyle: Google\n" 10187 "Language: JavaScript", 10188 &Styles[4]) 10189 .value()); 10190 EXPECT_ALL_STYLES_EQUAL(Styles); 10191 } 10192 10193 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \ 10194 Style.FIELD = false; \ 10195 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \ 10196 EXPECT_TRUE(Style.FIELD); \ 10197 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \ 10198 EXPECT_FALSE(Style.FIELD); 10199 10200 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD) 10201 10202 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \ 10203 Style.STRUCT.FIELD = false; \ 10204 EXPECT_EQ(0, \ 10205 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \ 10206 .value()); \ 10207 EXPECT_TRUE(Style.STRUCT.FIELD); \ 10208 EXPECT_EQ(0, \ 10209 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \ 10210 .value()); \ 10211 EXPECT_FALSE(Style.STRUCT.FIELD); 10212 10213 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \ 10214 CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD) 10215 10216 #define CHECK_PARSE(TEXT, FIELD, VALUE) \ 10217 EXPECT_NE(VALUE, Style.FIELD); \ 10218 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \ 10219 EXPECT_EQ(VALUE, Style.FIELD) 10220 10221 TEST_F(FormatTest, ParsesConfigurationBools) { 10222 FormatStyle Style = {}; 10223 Style.Language = FormatStyle::LK_Cpp; 10224 CHECK_PARSE_BOOL(AlignOperands); 10225 CHECK_PARSE_BOOL(AlignTrailingComments); 10226 CHECK_PARSE_BOOL(AlignConsecutiveAssignments); 10227 CHECK_PARSE_BOOL(AlignConsecutiveDeclarations); 10228 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); 10229 CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine); 10230 CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine); 10231 CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine); 10232 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); 10233 CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations); 10234 CHECK_PARSE_BOOL(BinPackArguments); 10235 CHECK_PARSE_BOOL(BinPackParameters); 10236 CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations); 10237 CHECK_PARSE_BOOL(BreakBeforeTernaryOperators); 10238 CHECK_PARSE_BOOL(BreakStringLiterals); 10239 CHECK_PARSE_BOOL(BreakBeforeInheritanceComma) 10240 CHECK_PARSE_BOOL(CompactNamespaces); 10241 CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine); 10242 CHECK_PARSE_BOOL(DerivePointerAlignment); 10243 CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding"); 10244 CHECK_PARSE_BOOL(DisableFormat); 10245 CHECK_PARSE_BOOL(IndentCaseLabels); 10246 CHECK_PARSE_BOOL(IndentWrappedFunctionNames); 10247 CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks); 10248 CHECK_PARSE_BOOL(ObjCSpaceAfterProperty); 10249 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList); 10250 CHECK_PARSE_BOOL(Cpp11BracedListStyle); 10251 CHECK_PARSE_BOOL(ReflowComments); 10252 CHECK_PARSE_BOOL(SortIncludes); 10253 CHECK_PARSE_BOOL(SortUsingDeclarations); 10254 CHECK_PARSE_BOOL(SpacesInParentheses); 10255 CHECK_PARSE_BOOL(SpacesInSquareBrackets); 10256 CHECK_PARSE_BOOL(SpacesInAngles); 10257 CHECK_PARSE_BOOL(SpaceInEmptyParentheses); 10258 CHECK_PARSE_BOOL(SpacesInContainerLiterals); 10259 CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); 10260 CHECK_PARSE_BOOL(SpaceAfterCStyleCast); 10261 CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword); 10262 CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); 10263 10264 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass); 10265 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement); 10266 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum); 10267 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction); 10268 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace); 10269 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration); 10270 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct); 10271 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion); 10272 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock); 10273 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch); 10274 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse); 10275 CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces); 10276 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction); 10277 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord); 10278 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace); 10279 } 10280 10281 #undef CHECK_PARSE_BOOL 10282 10283 TEST_F(FormatTest, ParsesConfiguration) { 10284 FormatStyle Style = {}; 10285 Style.Language = FormatStyle::LK_Cpp; 10286 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234); 10287 CHECK_PARSE("ConstructorInitializerIndentWidth: 1234", 10288 ConstructorInitializerIndentWidth, 1234u); 10289 CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u); 10290 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); 10291 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u); 10292 CHECK_PARSE("PenaltyBreakAssignment: 1234", 10293 PenaltyBreakAssignment, 1234u); 10294 CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234", 10295 PenaltyBreakBeforeFirstCallParameter, 1234u); 10296 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); 10297 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234", 10298 PenaltyReturnTypeOnItsOwnLine, 1234u); 10299 CHECK_PARSE("SpacesBeforeTrailingComments: 1234", 10300 SpacesBeforeTrailingComments, 1234u); 10301 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u); 10302 CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u); 10303 CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$"); 10304 10305 Style.PointerAlignment = FormatStyle::PAS_Middle; 10306 CHECK_PARSE("PointerAlignment: Left", PointerAlignment, 10307 FormatStyle::PAS_Left); 10308 CHECK_PARSE("PointerAlignment: Right", PointerAlignment, 10309 FormatStyle::PAS_Right); 10310 CHECK_PARSE("PointerAlignment: Middle", PointerAlignment, 10311 FormatStyle::PAS_Middle); 10312 // For backward compatibility: 10313 CHECK_PARSE("PointerBindsToType: Left", PointerAlignment, 10314 FormatStyle::PAS_Left); 10315 CHECK_PARSE("PointerBindsToType: Right", PointerAlignment, 10316 FormatStyle::PAS_Right); 10317 CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment, 10318 FormatStyle::PAS_Middle); 10319 10320 Style.Standard = FormatStyle::LS_Auto; 10321 CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03); 10322 CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11); 10323 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03); 10324 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11); 10325 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto); 10326 10327 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 10328 CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment", 10329 BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment); 10330 CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators, 10331 FormatStyle::BOS_None); 10332 CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators, 10333 FormatStyle::BOS_All); 10334 // For backward compatibility: 10335 CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators, 10336 FormatStyle::BOS_None); 10337 CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators, 10338 FormatStyle::BOS_All); 10339 10340 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 10341 CHECK_PARSE("BreakConstructorInitializers: BeforeComma", 10342 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 10343 CHECK_PARSE("BreakConstructorInitializers: AfterColon", 10344 BreakConstructorInitializers, FormatStyle::BCIS_AfterColon); 10345 CHECK_PARSE("BreakConstructorInitializers: BeforeColon", 10346 BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon); 10347 // For backward compatibility: 10348 CHECK_PARSE("BreakConstructorInitializersBeforeComma: true", 10349 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 10350 10351 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 10352 CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket, 10353 FormatStyle::BAS_Align); 10354 CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket, 10355 FormatStyle::BAS_DontAlign); 10356 CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket, 10357 FormatStyle::BAS_AlwaysBreak); 10358 // For backward compatibility: 10359 CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket, 10360 FormatStyle::BAS_DontAlign); 10361 CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket, 10362 FormatStyle::BAS_Align); 10363 10364 Style.AlignEscapedNewlines = FormatStyle::ENAS_Left; 10365 CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines, 10366 FormatStyle::ENAS_DontAlign); 10367 CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines, 10368 FormatStyle::ENAS_Left); 10369 CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines, 10370 FormatStyle::ENAS_Right); 10371 // For backward compatibility: 10372 CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines, 10373 FormatStyle::ENAS_Left); 10374 CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines, 10375 FormatStyle::ENAS_Right); 10376 10377 Style.UseTab = FormatStyle::UT_ForIndentation; 10378 CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never); 10379 CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation); 10380 CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always); 10381 CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab, 10382 FormatStyle::UT_ForContinuationAndIndentation); 10383 // For backward compatibility: 10384 CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never); 10385 CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always); 10386 10387 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 10388 CHECK_PARSE("AllowShortFunctionsOnASingleLine: None", 10389 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 10390 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline", 10391 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline); 10392 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty", 10393 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty); 10394 CHECK_PARSE("AllowShortFunctionsOnASingleLine: All", 10395 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 10396 // For backward compatibility: 10397 CHECK_PARSE("AllowShortFunctionsOnASingleLine: false", 10398 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 10399 CHECK_PARSE("AllowShortFunctionsOnASingleLine: true", 10400 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 10401 10402 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 10403 CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens, 10404 FormatStyle::SBPO_Never); 10405 CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens, 10406 FormatStyle::SBPO_Always); 10407 CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens, 10408 FormatStyle::SBPO_ControlStatements); 10409 // For backward compatibility: 10410 CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens, 10411 FormatStyle::SBPO_Never); 10412 CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens, 10413 FormatStyle::SBPO_ControlStatements); 10414 10415 Style.ColumnLimit = 123; 10416 FormatStyle BaseStyle = getLLVMStyle(); 10417 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit); 10418 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u); 10419 10420 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 10421 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces, 10422 FormatStyle::BS_Attach); 10423 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces, 10424 FormatStyle::BS_Linux); 10425 CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces, 10426 FormatStyle::BS_Mozilla); 10427 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces, 10428 FormatStyle::BS_Stroustrup); 10429 CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces, 10430 FormatStyle::BS_Allman); 10431 CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU); 10432 CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces, 10433 FormatStyle::BS_WebKit); 10434 CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces, 10435 FormatStyle::BS_Custom); 10436 10437 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 10438 CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType, 10439 FormatStyle::RTBS_None); 10440 CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType, 10441 FormatStyle::RTBS_All); 10442 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel", 10443 AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel); 10444 CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions", 10445 AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions); 10446 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions", 10447 AlwaysBreakAfterReturnType, 10448 FormatStyle::RTBS_TopLevelDefinitions); 10449 10450 Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All; 10451 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None", 10452 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None); 10453 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All", 10454 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All); 10455 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel", 10456 AlwaysBreakAfterDefinitionReturnType, 10457 FormatStyle::DRTBS_TopLevel); 10458 10459 Style.NamespaceIndentation = FormatStyle::NI_All; 10460 CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation, 10461 FormatStyle::NI_None); 10462 CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation, 10463 FormatStyle::NI_Inner); 10464 CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation, 10465 FormatStyle::NI_All); 10466 10467 // FIXME: This is required because parsing a configuration simply overwrites 10468 // the first N elements of the list instead of resetting it. 10469 Style.ForEachMacros.clear(); 10470 std::vector<std::string> BoostForeach; 10471 BoostForeach.push_back("BOOST_FOREACH"); 10472 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach); 10473 std::vector<std::string> BoostAndQForeach; 10474 BoostAndQForeach.push_back("BOOST_FOREACH"); 10475 BoostAndQForeach.push_back("Q_FOREACH"); 10476 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros, 10477 BoostAndQForeach); 10478 10479 Style.IncludeCategories.clear(); 10480 std::vector<FormatStyle::IncludeCategory> ExpectedCategories = {{"abc/.*", 2}, 10481 {".*", 1}}; 10482 CHECK_PARSE("IncludeCategories:\n" 10483 " - Regex: abc/.*\n" 10484 " Priority: 2\n" 10485 " - Regex: .*\n" 10486 " Priority: 1", 10487 IncludeCategories, ExpectedCategories); 10488 CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeIsMainRegex, "abc$"); 10489 10490 Style.RawStringFormats.clear(); 10491 std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = { 10492 { 10493 FormatStyle::LK_TextProto, 10494 {"pb", "proto"}, 10495 {"PARSE_TEXT_PROTO"}, 10496 /*CanonicalDelimiter=*/"", 10497 "llvm", 10498 }, 10499 { 10500 FormatStyle::LK_Cpp, 10501 {"cc", "cpp"}, 10502 {"C_CODEBLOCK", "CPPEVAL"}, 10503 /*CanonicalDelimiter=*/"cc", 10504 /*BasedOnStyle=*/"", 10505 }, 10506 }; 10507 10508 CHECK_PARSE("RawStringFormats:\n" 10509 " - Language: TextProto\n" 10510 " Delimiters:\n" 10511 " - 'pb'\n" 10512 " - 'proto'\n" 10513 " EnclosingFunctions:\n" 10514 " - 'PARSE_TEXT_PROTO'\n" 10515 " BasedOnStyle: llvm\n" 10516 " - Language: Cpp\n" 10517 " Delimiters:\n" 10518 " - 'cc'\n" 10519 " - 'cpp'\n" 10520 " EnclosingFunctions:\n" 10521 " - 'C_CODEBLOCK'\n" 10522 " - 'CPPEVAL'\n" 10523 " CanonicalDelimiter: 'cc'", 10524 RawStringFormats, ExpectedRawStringFormats); 10525 } 10526 10527 TEST_F(FormatTest, ParsesConfigurationWithLanguages) { 10528 FormatStyle Style = {}; 10529 Style.Language = FormatStyle::LK_Cpp; 10530 CHECK_PARSE("Language: Cpp\n" 10531 "IndentWidth: 12", 10532 IndentWidth, 12u); 10533 EXPECT_EQ(parseConfiguration("Language: JavaScript\n" 10534 "IndentWidth: 34", 10535 &Style), 10536 ParseError::Unsuitable); 10537 EXPECT_EQ(12u, Style.IndentWidth); 10538 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 10539 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 10540 10541 Style.Language = FormatStyle::LK_JavaScript; 10542 CHECK_PARSE("Language: JavaScript\n" 10543 "IndentWidth: 12", 10544 IndentWidth, 12u); 10545 CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u); 10546 EXPECT_EQ(parseConfiguration("Language: Cpp\n" 10547 "IndentWidth: 34", 10548 &Style), 10549 ParseError::Unsuitable); 10550 EXPECT_EQ(23u, Style.IndentWidth); 10551 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 10552 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 10553 10554 CHECK_PARSE("BasedOnStyle: LLVM\n" 10555 "IndentWidth: 67", 10556 IndentWidth, 67u); 10557 10558 CHECK_PARSE("---\n" 10559 "Language: JavaScript\n" 10560 "IndentWidth: 12\n" 10561 "---\n" 10562 "Language: Cpp\n" 10563 "IndentWidth: 34\n" 10564 "...\n", 10565 IndentWidth, 12u); 10566 10567 Style.Language = FormatStyle::LK_Cpp; 10568 CHECK_PARSE("---\n" 10569 "Language: JavaScript\n" 10570 "IndentWidth: 12\n" 10571 "---\n" 10572 "Language: Cpp\n" 10573 "IndentWidth: 34\n" 10574 "...\n", 10575 IndentWidth, 34u); 10576 CHECK_PARSE("---\n" 10577 "IndentWidth: 78\n" 10578 "---\n" 10579 "Language: JavaScript\n" 10580 "IndentWidth: 56\n" 10581 "...\n", 10582 IndentWidth, 78u); 10583 10584 Style.ColumnLimit = 123; 10585 Style.IndentWidth = 234; 10586 Style.BreakBeforeBraces = FormatStyle::BS_Linux; 10587 Style.TabWidth = 345; 10588 EXPECT_FALSE(parseConfiguration("---\n" 10589 "IndentWidth: 456\n" 10590 "BreakBeforeBraces: Allman\n" 10591 "---\n" 10592 "Language: JavaScript\n" 10593 "IndentWidth: 111\n" 10594 "TabWidth: 111\n" 10595 "---\n" 10596 "Language: Cpp\n" 10597 "BreakBeforeBraces: Stroustrup\n" 10598 "TabWidth: 789\n" 10599 "...\n", 10600 &Style)); 10601 EXPECT_EQ(123u, Style.ColumnLimit); 10602 EXPECT_EQ(456u, Style.IndentWidth); 10603 EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces); 10604 EXPECT_EQ(789u, Style.TabWidth); 10605 10606 EXPECT_EQ(parseConfiguration("---\n" 10607 "Language: JavaScript\n" 10608 "IndentWidth: 56\n" 10609 "---\n" 10610 "IndentWidth: 78\n" 10611 "...\n", 10612 &Style), 10613 ParseError::Error); 10614 EXPECT_EQ(parseConfiguration("---\n" 10615 "Language: JavaScript\n" 10616 "IndentWidth: 56\n" 10617 "---\n" 10618 "Language: JavaScript\n" 10619 "IndentWidth: 78\n" 10620 "...\n", 10621 &Style), 10622 ParseError::Error); 10623 10624 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 10625 } 10626 10627 #undef CHECK_PARSE 10628 10629 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) { 10630 FormatStyle Style = {}; 10631 Style.Language = FormatStyle::LK_JavaScript; 10632 Style.BreakBeforeTernaryOperators = true; 10633 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value()); 10634 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 10635 10636 Style.BreakBeforeTernaryOperators = true; 10637 EXPECT_EQ(0, parseConfiguration("---\n" 10638 "BasedOnStyle: Google\n" 10639 "---\n" 10640 "Language: JavaScript\n" 10641 "IndentWidth: 76\n" 10642 "...\n", 10643 &Style) 10644 .value()); 10645 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 10646 EXPECT_EQ(76u, Style.IndentWidth); 10647 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 10648 } 10649 10650 TEST_F(FormatTest, ConfigurationRoundTripTest) { 10651 FormatStyle Style = getLLVMStyle(); 10652 std::string YAML = configurationAsText(Style); 10653 FormatStyle ParsedStyle = {}; 10654 ParsedStyle.Language = FormatStyle::LK_Cpp; 10655 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value()); 10656 EXPECT_EQ(Style, ParsedStyle); 10657 } 10658 10659 TEST_F(FormatTest, WorksFor8bitEncodings) { 10660 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" 10661 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" 10662 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" 10663 "\"\xef\xee\xf0\xf3...\"", 10664 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " 10665 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " 10666 "\xef\xee\xf0\xf3...\"", 10667 getLLVMStyleWithColumns(12))); 10668 } 10669 10670 TEST_F(FormatTest, HandlesUTF8BOM) { 10671 EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf")); 10672 EXPECT_EQ("\xef\xbb\xbf#include <iostream>", 10673 format("\xef\xbb\xbf#include <iostream>")); 10674 EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>", 10675 format("\xef\xbb\xbf\n#include <iostream>")); 10676 } 10677 10678 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. 10679 #if !defined(_MSC_VER) 10680 10681 TEST_F(FormatTest, CountsUTF8CharactersProperly) { 10682 verifyFormat("\"Однажды в студёную зимнюю пору...\"", 10683 getLLVMStyleWithColumns(35)); 10684 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", 10685 getLLVMStyleWithColumns(31)); 10686 verifyFormat("// Однажды в студёную зимнюю пору...", 10687 getLLVMStyleWithColumns(36)); 10688 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32)); 10689 verifyFormat("/* Однажды в студёную зимнюю пору... */", 10690 getLLVMStyleWithColumns(39)); 10691 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", 10692 getLLVMStyleWithColumns(35)); 10693 } 10694 10695 TEST_F(FormatTest, SplitsUTF8Strings) { 10696 // Non-printable characters' width is currently considered to be the length in 10697 // bytes in UTF8. The characters can be displayed in very different manner 10698 // (zero-width, single width with a substitution glyph, expanded to their code 10699 // (e.g. "<8d>"), so there's no single correct way to handle them. 10700 EXPECT_EQ("\"aaaaÄ\"\n" 10701 "\"\xc2\x8d\";", 10702 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 10703 EXPECT_EQ("\"aaaaaaaÄ\"\n" 10704 "\"\xc2\x8d\";", 10705 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 10706 EXPECT_EQ("\"Однажды, в \"\n" 10707 "\"студёную \"\n" 10708 "\"зимнюю \"\n" 10709 "\"пору,\"", 10710 format("\"Однажды, в студёную зимнюю пору,\"", 10711 getLLVMStyleWithColumns(13))); 10712 EXPECT_EQ( 10713 "\"一 二 三 \"\n" 10714 "\"四 五六 \"\n" 10715 "\"七 八 九 \"\n" 10716 "\"十\"", 10717 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); 10718 EXPECT_EQ("\"一\t\"\n" 10719 "\"二 \t\"\n" 10720 "\"三 四 \"\n" 10721 "\"五\t\"\n" 10722 "\"六 \t\"\n" 10723 "\"七 \"\n" 10724 "\"八九十\tqq\"", 10725 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", 10726 getLLVMStyleWithColumns(11))); 10727 10728 // UTF8 character in an escape sequence. 10729 EXPECT_EQ("\"aaaaaa\"\n" 10730 "\"\\\xC2\x8D\"", 10731 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10))); 10732 } 10733 10734 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { 10735 EXPECT_EQ("const char *sssss =\n" 10736 " \"一二三四五六七八\\\n" 10737 " 九 十\";", 10738 format("const char *sssss = \"一二三四五六七八\\\n" 10739 " 九 十\";", 10740 getLLVMStyleWithColumns(30))); 10741 } 10742 10743 TEST_F(FormatTest, SplitsUTF8LineComments) { 10744 EXPECT_EQ("// aaaaÄ\xc2\x8d", 10745 format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10))); 10746 EXPECT_EQ("// Я из лесу\n" 10747 "// вышел; был\n" 10748 "// сильный\n" 10749 "// мороз.", 10750 format("// Я из лесу вышел; был сильный мороз.", 10751 getLLVMStyleWithColumns(13))); 10752 EXPECT_EQ("// 一二三\n" 10753 "// 四五六七\n" 10754 "// 八 九\n" 10755 "// 十", 10756 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9))); 10757 } 10758 10759 TEST_F(FormatTest, SplitsUTF8BlockComments) { 10760 EXPECT_EQ("/* Гляжу,\n" 10761 " * поднимается\n" 10762 " * медленно в\n" 10763 " * гору\n" 10764 " * Лошадка,\n" 10765 " * везущая\n" 10766 " * хворосту\n" 10767 " * воз. */", 10768 format("/* Гляжу, поднимается медленно в гору\n" 10769 " * Лошадка, везущая хворосту воз. */", 10770 getLLVMStyleWithColumns(13))); 10771 EXPECT_EQ( 10772 "/* 一二三\n" 10773 " * 四五六七\n" 10774 " * 八 九\n" 10775 " * 十 */", 10776 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9))); 10777 EXPECT_EQ("/* \n" 10778 " * \n" 10779 " * - */", 10780 format("/* - */", getLLVMStyleWithColumns(12))); 10781 } 10782 10783 #endif // _MSC_VER 10784 10785 TEST_F(FormatTest, ConstructorInitializerIndentWidth) { 10786 FormatStyle Style = getLLVMStyle(); 10787 10788 Style.ConstructorInitializerIndentWidth = 4; 10789 verifyFormat( 10790 "SomeClass::Constructor()\n" 10791 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10792 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10793 Style); 10794 10795 Style.ConstructorInitializerIndentWidth = 2; 10796 verifyFormat( 10797 "SomeClass::Constructor()\n" 10798 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10799 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10800 Style); 10801 10802 Style.ConstructorInitializerIndentWidth = 0; 10803 verifyFormat( 10804 "SomeClass::Constructor()\n" 10805 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10806 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10807 Style); 10808 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 10809 verifyFormat( 10810 "SomeLongTemplateVariableName<\n" 10811 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>", 10812 Style); 10813 verifyFormat( 10814 "bool smaller = 1 < bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 10815 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 10816 Style); 10817 } 10818 10819 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) { 10820 FormatStyle Style = getLLVMStyle(); 10821 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 10822 Style.ConstructorInitializerIndentWidth = 4; 10823 verifyFormat("SomeClass::Constructor()\n" 10824 " : a(a)\n" 10825 " , b(b)\n" 10826 " , c(c) {}", 10827 Style); 10828 verifyFormat("SomeClass::Constructor()\n" 10829 " : a(a) {}", 10830 Style); 10831 10832 Style.ColumnLimit = 0; 10833 verifyFormat("SomeClass::Constructor()\n" 10834 " : a(a) {}", 10835 Style); 10836 verifyFormat("SomeClass::Constructor() noexcept\n" 10837 " : a(a) {}", 10838 Style); 10839 verifyFormat("SomeClass::Constructor()\n" 10840 " : a(a)\n" 10841 " , b(b)\n" 10842 " , c(c) {}", 10843 Style); 10844 verifyFormat("SomeClass::Constructor()\n" 10845 " : a(a) {\n" 10846 " foo();\n" 10847 " bar();\n" 10848 "}", 10849 Style); 10850 10851 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 10852 verifyFormat("SomeClass::Constructor()\n" 10853 " : a(a)\n" 10854 " , b(b)\n" 10855 " , c(c) {\n}", 10856 Style); 10857 verifyFormat("SomeClass::Constructor()\n" 10858 " : a(a) {\n}", 10859 Style); 10860 10861 Style.ColumnLimit = 80; 10862 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 10863 Style.ConstructorInitializerIndentWidth = 2; 10864 verifyFormat("SomeClass::Constructor()\n" 10865 " : a(a)\n" 10866 " , b(b)\n" 10867 " , c(c) {}", 10868 Style); 10869 10870 Style.ConstructorInitializerIndentWidth = 0; 10871 verifyFormat("SomeClass::Constructor()\n" 10872 ": a(a)\n" 10873 ", b(b)\n" 10874 ", c(c) {}", 10875 Style); 10876 10877 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 10878 Style.ConstructorInitializerIndentWidth = 4; 10879 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style); 10880 verifyFormat( 10881 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n", 10882 Style); 10883 verifyFormat( 10884 "SomeClass::Constructor()\n" 10885 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", 10886 Style); 10887 Style.ConstructorInitializerIndentWidth = 4; 10888 Style.ColumnLimit = 60; 10889 verifyFormat("SomeClass::Constructor()\n" 10890 " : aaaaaaaa(aaaaaaaa)\n" 10891 " , aaaaaaaa(aaaaaaaa)\n" 10892 " , aaaaaaaa(aaaaaaaa) {}", 10893 Style); 10894 } 10895 10896 TEST_F(FormatTest, Destructors) { 10897 verifyFormat("void F(int &i) { i.~int(); }"); 10898 verifyFormat("void F(int &i) { i->~int(); }"); 10899 } 10900 10901 TEST_F(FormatTest, FormatsWithWebKitStyle) { 10902 FormatStyle Style = getWebKitStyle(); 10903 10904 // Don't indent in outer namespaces. 10905 verifyFormat("namespace outer {\n" 10906 "int i;\n" 10907 "namespace inner {\n" 10908 " int i;\n" 10909 "} // namespace inner\n" 10910 "} // namespace outer\n" 10911 "namespace other_outer {\n" 10912 "int i;\n" 10913 "}", 10914 Style); 10915 10916 // Don't indent case labels. 10917 verifyFormat("switch (variable) {\n" 10918 "case 1:\n" 10919 "case 2:\n" 10920 " doSomething();\n" 10921 " break;\n" 10922 "default:\n" 10923 " ++variable;\n" 10924 "}", 10925 Style); 10926 10927 // Wrap before binary operators. 10928 EXPECT_EQ("void f()\n" 10929 "{\n" 10930 " if (aaaaaaaaaaaaaaaa\n" 10931 " && bbbbbbbbbbbbbbbbbbbbbbbb\n" 10932 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 10933 " return;\n" 10934 "}", 10935 format("void f() {\n" 10936 "if (aaaaaaaaaaaaaaaa\n" 10937 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" 10938 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 10939 "return;\n" 10940 "}", 10941 Style)); 10942 10943 // Allow functions on a single line. 10944 verifyFormat("void f() { return; }", Style); 10945 10946 // Constructor initializers are formatted one per line with the "," on the 10947 // new line. 10948 verifyFormat("Constructor()\n" 10949 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 10950 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" 10951 " aaaaaaaaaaaaaa)\n" 10952 " , aaaaaaaaaaaaaaaaaaaaaaa()\n" 10953 "{\n" 10954 "}", 10955 Style); 10956 verifyFormat("SomeClass::Constructor()\n" 10957 " : a(a)\n" 10958 "{\n" 10959 "}", 10960 Style); 10961 EXPECT_EQ("SomeClass::Constructor()\n" 10962 " : a(a)\n" 10963 "{\n" 10964 "}", 10965 format("SomeClass::Constructor():a(a){}", Style)); 10966 verifyFormat("SomeClass::Constructor()\n" 10967 " : a(a)\n" 10968 " , b(b)\n" 10969 " , c(c)\n" 10970 "{\n" 10971 "}", 10972 Style); 10973 verifyFormat("SomeClass::Constructor()\n" 10974 " : a(a)\n" 10975 "{\n" 10976 " foo();\n" 10977 " bar();\n" 10978 "}", 10979 Style); 10980 10981 // Access specifiers should be aligned left. 10982 verifyFormat("class C {\n" 10983 "public:\n" 10984 " int i;\n" 10985 "};", 10986 Style); 10987 10988 // Do not align comments. 10989 verifyFormat("int a; // Do not\n" 10990 "double b; // align comments.", 10991 Style); 10992 10993 // Do not align operands. 10994 EXPECT_EQ("ASSERT(aaaa\n" 10995 " || bbbb);", 10996 format("ASSERT ( aaaa\n||bbbb);", Style)); 10997 10998 // Accept input's line breaks. 10999 EXPECT_EQ("if (aaaaaaaaaaaaaaa\n" 11000 " || bbbbbbbbbbbbbbb) {\n" 11001 " i++;\n" 11002 "}", 11003 format("if (aaaaaaaaaaaaaaa\n" 11004 "|| bbbbbbbbbbbbbbb) { i++; }", 11005 Style)); 11006 EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n" 11007 " i++;\n" 11008 "}", 11009 format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style)); 11010 11011 // Don't automatically break all macro definitions (llvm.org/PR17842). 11012 verifyFormat("#define aNumber 10", Style); 11013 // However, generally keep the line breaks that the user authored. 11014 EXPECT_EQ("#define aNumber \\\n" 11015 " 10", 11016 format("#define aNumber \\\n" 11017 " 10", 11018 Style)); 11019 11020 // Keep empty and one-element array literals on a single line. 11021 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n" 11022 " copyItems:YES];", 11023 format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n" 11024 "copyItems:YES];", 11025 Style)); 11026 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n" 11027 " copyItems:YES];", 11028 format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n" 11029 " copyItems:YES];", 11030 Style)); 11031 // FIXME: This does not seem right, there should be more indentation before 11032 // the array literal's entries. Nested blocks have the same problem. 11033 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 11034 " @\"a\",\n" 11035 " @\"a\"\n" 11036 "]\n" 11037 " copyItems:YES];", 11038 format("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 11039 " @\"a\",\n" 11040 " @\"a\"\n" 11041 " ]\n" 11042 " copyItems:YES];", 11043 Style)); 11044 EXPECT_EQ( 11045 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 11046 " copyItems:YES];", 11047 format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 11048 " copyItems:YES];", 11049 Style)); 11050 11051 verifyFormat("[self.a b:c c:d];", Style); 11052 EXPECT_EQ("[self.a b:c\n" 11053 " c:d];", 11054 format("[self.a b:c\n" 11055 "c:d];", 11056 Style)); 11057 } 11058 11059 TEST_F(FormatTest, FormatsLambdas) { 11060 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n"); 11061 verifyFormat("int c = [&] { [=] { return b++; }(); }();\n"); 11062 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n"); 11063 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n"); 11064 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n"); 11065 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n"); 11066 verifyFormat("auto c = [a = [b = 42] {}] {};\n"); 11067 verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n"); 11068 verifyFormat("int x = f(*+[] {});"); 11069 verifyFormat("void f() {\n" 11070 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n" 11071 "}\n"); 11072 verifyFormat("void f() {\n" 11073 " other(x.begin(), //\n" 11074 " x.end(), //\n" 11075 " [&](int, int) { return 1; });\n" 11076 "}\n"); 11077 verifyFormat("SomeFunction([]() { // A cool function...\n" 11078 " return 43;\n" 11079 "});"); 11080 EXPECT_EQ("SomeFunction([]() {\n" 11081 "#define A a\n" 11082 " return 43;\n" 11083 "});", 11084 format("SomeFunction([](){\n" 11085 "#define A a\n" 11086 "return 43;\n" 11087 "});")); 11088 verifyFormat("void f() {\n" 11089 " SomeFunction([](decltype(x), A *a) {});\n" 11090 "}"); 11091 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 11092 " [](const aaaaaaaaaa &a) { return a; });"); 11093 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n" 11094 " SomeOtherFunctioooooooooooooooooooooooooon();\n" 11095 "});"); 11096 verifyFormat("Constructor()\n" 11097 " : Field([] { // comment\n" 11098 " int i;\n" 11099 " }) {}"); 11100 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n" 11101 " return some_parameter.size();\n" 11102 "};"); 11103 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n" 11104 " [](const string &s) { return s; };"); 11105 verifyFormat("int i = aaaaaa ? 1 //\n" 11106 " : [] {\n" 11107 " return 2; //\n" 11108 " }();"); 11109 verifyFormat("llvm::errs() << \"number of twos is \"\n" 11110 " << std::count_if(v.begin(), v.end(), [](int x) {\n" 11111 " return x == 2; // force break\n" 11112 " });"); 11113 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 11114 " [=](int iiiiiiiiiiii) {\n" 11115 " return aaaaaaaaaaaaaaaaaaaaaaa !=\n" 11116 " aaaaaaaaaaaaaaaaaaaaaaa;\n" 11117 " });", 11118 getLLVMStyleWithColumns(60)); 11119 verifyFormat("SomeFunction({[&] {\n" 11120 " // comment\n" 11121 " },\n" 11122 " [&] {\n" 11123 " // comment\n" 11124 " }});"); 11125 verifyFormat("SomeFunction({[&] {\n" 11126 " // comment\n" 11127 "}});"); 11128 verifyFormat("virtual aaaaaaaaaaaaaaaa(std::function<bool()> bbbbbbbbbbbb =\n" 11129 " [&]() { return true; },\n" 11130 " aaaaa aaaaaaaaa);"); 11131 11132 // Lambdas with return types. 11133 verifyFormat("int c = []() -> int { return 2; }();\n"); 11134 verifyFormat("int c = []() -> int * { return 2; }();\n"); 11135 verifyFormat("int c = []() -> vector<int> { return {2}; }();\n"); 11136 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());"); 11137 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};"); 11138 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};"); 11139 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};"); 11140 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};"); 11141 verifyFormat("[a, a]() -> a<1> {};"); 11142 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n" 11143 " int j) -> int {\n" 11144 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n" 11145 "};"); 11146 verifyFormat( 11147 "aaaaaaaaaaaaaaaaaaaaaa(\n" 11148 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n" 11149 " return aaaaaaaaaaaaaaaaa;\n" 11150 " });", 11151 getLLVMStyleWithColumns(70)); 11152 verifyFormat("[]() //\n" 11153 " -> int {\n" 11154 " return 1; //\n" 11155 "};"); 11156 11157 // Multiple lambdas in the same parentheses change indentation rules. 11158 verifyFormat("SomeFunction(\n" 11159 " []() {\n" 11160 " int i = 42;\n" 11161 " return i;\n" 11162 " },\n" 11163 " []() {\n" 11164 " int j = 43;\n" 11165 " return j;\n" 11166 " });"); 11167 11168 // More complex introducers. 11169 verifyFormat("return [i, args...] {};"); 11170 11171 // Not lambdas. 11172 verifyFormat("constexpr char hello[]{\"hello\"};"); 11173 verifyFormat("double &operator[](int i) { return 0; }\n" 11174 "int i;"); 11175 verifyFormat("std::unique_ptr<int[]> foo() {}"); 11176 verifyFormat("int i = a[a][a]->f();"); 11177 verifyFormat("int i = (*b)[a]->f();"); 11178 11179 // Other corner cases. 11180 verifyFormat("void f() {\n" 11181 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n" 11182 " );\n" 11183 "}"); 11184 11185 // Lambdas created through weird macros. 11186 verifyFormat("void f() {\n" 11187 " MACRO((const AA &a) { return 1; });\n" 11188 " MACRO((AA &a) { return 1; });\n" 11189 "}"); 11190 11191 verifyFormat("if (blah_blah(whatever, whatever, [] {\n" 11192 " doo_dah();\n" 11193 " doo_dah();\n" 11194 " })) {\n" 11195 "}"); 11196 verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n" 11197 " doo_dah();\n" 11198 " doo_dah();\n" 11199 " })) {\n" 11200 "}"); 11201 verifyFormat("auto lambda = []() {\n" 11202 " int a = 2\n" 11203 "#if A\n" 11204 " + 2\n" 11205 "#endif\n" 11206 " ;\n" 11207 "};"); 11208 11209 // Lambdas with complex multiline introducers. 11210 verifyFormat( 11211 "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 11212 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n" 11213 " -> ::std::unordered_set<\n" 11214 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n" 11215 " //\n" 11216 " });"); 11217 } 11218 11219 TEST_F(FormatTest, EmptyLinesInLambdas) { 11220 verifyFormat("auto lambda = []() {\n" 11221 " x(); //\n" 11222 "};", 11223 "auto lambda = []() {\n" 11224 "\n" 11225 " x(); //\n" 11226 "\n" 11227 "};"); 11228 } 11229 11230 TEST_F(FormatTest, FormatsBlocks) { 11231 FormatStyle ShortBlocks = getLLVMStyle(); 11232 ShortBlocks.AllowShortBlocksOnASingleLine = true; 11233 verifyFormat("int (^Block)(int, int);", ShortBlocks); 11234 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks); 11235 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks); 11236 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks); 11237 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks); 11238 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks); 11239 11240 verifyFormat("foo(^{ bar(); });", ShortBlocks); 11241 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks); 11242 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks); 11243 11244 verifyFormat("[operation setCompletionBlock:^{\n" 11245 " [self onOperationDone];\n" 11246 "}];"); 11247 verifyFormat("int i = {[operation setCompletionBlock:^{\n" 11248 " [self onOperationDone];\n" 11249 "}]};"); 11250 verifyFormat("[operation setCompletionBlock:^(int *i) {\n" 11251 " f();\n" 11252 "}];"); 11253 verifyFormat("int a = [operation block:^int(int *i) {\n" 11254 " return 1;\n" 11255 "}];"); 11256 verifyFormat("[myObject doSomethingWith:arg1\n" 11257 " aaa:^int(int *a) {\n" 11258 " return 1;\n" 11259 " }\n" 11260 " bbb:f(a * bbbbbbbb)];"); 11261 11262 verifyFormat("[operation setCompletionBlock:^{\n" 11263 " [self.delegate newDataAvailable];\n" 11264 "}];", 11265 getLLVMStyleWithColumns(60)); 11266 verifyFormat("dispatch_async(_fileIOQueue, ^{\n" 11267 " NSString *path = [self sessionFilePath];\n" 11268 " if (path) {\n" 11269 " // ...\n" 11270 " }\n" 11271 "});"); 11272 verifyFormat("[[SessionService sharedService]\n" 11273 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 11274 " if (window) {\n" 11275 " [self windowDidLoad:window];\n" 11276 " } else {\n" 11277 " [self errorLoadingWindow];\n" 11278 " }\n" 11279 " }];"); 11280 verifyFormat("void (^largeBlock)(void) = ^{\n" 11281 " // ...\n" 11282 "};\n", 11283 getLLVMStyleWithColumns(40)); 11284 verifyFormat("[[SessionService sharedService]\n" 11285 " loadWindowWithCompletionBlock: //\n" 11286 " ^(SessionWindow *window) {\n" 11287 " if (window) {\n" 11288 " [self windowDidLoad:window];\n" 11289 " } else {\n" 11290 " [self errorLoadingWindow];\n" 11291 " }\n" 11292 " }];", 11293 getLLVMStyleWithColumns(60)); 11294 verifyFormat("[myObject doSomethingWith:arg1\n" 11295 " firstBlock:^(Foo *a) {\n" 11296 " // ...\n" 11297 " int i;\n" 11298 " }\n" 11299 " secondBlock:^(Bar *b) {\n" 11300 " // ...\n" 11301 " int i;\n" 11302 " }\n" 11303 " thirdBlock:^Foo(Bar *b) {\n" 11304 " // ...\n" 11305 " int i;\n" 11306 " }];"); 11307 verifyFormat("[myObject doSomethingWith:arg1\n" 11308 " firstBlock:-1\n" 11309 " secondBlock:^(Bar *b) {\n" 11310 " // ...\n" 11311 " int i;\n" 11312 " }];"); 11313 11314 verifyFormat("f(^{\n" 11315 " @autoreleasepool {\n" 11316 " if (a) {\n" 11317 " g();\n" 11318 " }\n" 11319 " }\n" 11320 "});"); 11321 verifyFormat("Block b = ^int *(A *a, B *b) {}"); 11322 verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n" 11323 "};"); 11324 11325 FormatStyle FourIndent = getLLVMStyle(); 11326 FourIndent.ObjCBlockIndentWidth = 4; 11327 verifyFormat("[operation setCompletionBlock:^{\n" 11328 " [self onOperationDone];\n" 11329 "}];", 11330 FourIndent); 11331 } 11332 11333 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) { 11334 FormatStyle ZeroColumn = getLLVMStyle(); 11335 ZeroColumn.ColumnLimit = 0; 11336 11337 verifyFormat("[[SessionService sharedService] " 11338 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 11339 " if (window) {\n" 11340 " [self windowDidLoad:window];\n" 11341 " } else {\n" 11342 " [self errorLoadingWindow];\n" 11343 " }\n" 11344 "}];", 11345 ZeroColumn); 11346 EXPECT_EQ("[[SessionService sharedService]\n" 11347 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 11348 " if (window) {\n" 11349 " [self windowDidLoad:window];\n" 11350 " } else {\n" 11351 " [self errorLoadingWindow];\n" 11352 " }\n" 11353 " }];", 11354 format("[[SessionService sharedService]\n" 11355 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 11356 " if (window) {\n" 11357 " [self windowDidLoad:window];\n" 11358 " } else {\n" 11359 " [self errorLoadingWindow];\n" 11360 " }\n" 11361 "}];", 11362 ZeroColumn)); 11363 verifyFormat("[myObject doSomethingWith:arg1\n" 11364 " firstBlock:^(Foo *a) {\n" 11365 " // ...\n" 11366 " int i;\n" 11367 " }\n" 11368 " secondBlock:^(Bar *b) {\n" 11369 " // ...\n" 11370 " int i;\n" 11371 " }\n" 11372 " thirdBlock:^Foo(Bar *b) {\n" 11373 " // ...\n" 11374 " int i;\n" 11375 " }];", 11376 ZeroColumn); 11377 verifyFormat("f(^{\n" 11378 " @autoreleasepool {\n" 11379 " if (a) {\n" 11380 " g();\n" 11381 " }\n" 11382 " }\n" 11383 "});", 11384 ZeroColumn); 11385 verifyFormat("void (^largeBlock)(void) = ^{\n" 11386 " // ...\n" 11387 "};", 11388 ZeroColumn); 11389 11390 ZeroColumn.AllowShortBlocksOnASingleLine = true; 11391 EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };", 11392 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 11393 ZeroColumn.AllowShortBlocksOnASingleLine = false; 11394 EXPECT_EQ("void (^largeBlock)(void) = ^{\n" 11395 " int i;\n" 11396 "};", 11397 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 11398 } 11399 11400 TEST_F(FormatTest, SupportsCRLF) { 11401 EXPECT_EQ("int a;\r\n" 11402 "int b;\r\n" 11403 "int c;\r\n", 11404 format("int a;\r\n" 11405 " int b;\r\n" 11406 " int c;\r\n", 11407 getLLVMStyle())); 11408 EXPECT_EQ("int a;\r\n" 11409 "int b;\r\n" 11410 "int c;\r\n", 11411 format("int a;\r\n" 11412 " int b;\n" 11413 " int c;\r\n", 11414 getLLVMStyle())); 11415 EXPECT_EQ("int a;\n" 11416 "int b;\n" 11417 "int c;\n", 11418 format("int a;\r\n" 11419 " int b;\n" 11420 " int c;\n", 11421 getLLVMStyle())); 11422 EXPECT_EQ("\"aaaaaaa \"\r\n" 11423 "\"bbbbbbb\";\r\n", 11424 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10))); 11425 EXPECT_EQ("#define A \\\r\n" 11426 " b; \\\r\n" 11427 " c; \\\r\n" 11428 " d;\r\n", 11429 format("#define A \\\r\n" 11430 " b; \\\r\n" 11431 " c; d; \r\n", 11432 getGoogleStyle())); 11433 11434 EXPECT_EQ("/*\r\n" 11435 "multi line block comments\r\n" 11436 "should not introduce\r\n" 11437 "an extra carriage return\r\n" 11438 "*/\r\n", 11439 format("/*\r\n" 11440 "multi line block comments\r\n" 11441 "should not introduce\r\n" 11442 "an extra carriage return\r\n" 11443 "*/\r\n")); 11444 } 11445 11446 TEST_F(FormatTest, MunchSemicolonAfterBlocks) { 11447 verifyFormat("MY_CLASS(C) {\n" 11448 " int i;\n" 11449 " int j;\n" 11450 "};"); 11451 } 11452 11453 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) { 11454 FormatStyle TwoIndent = getLLVMStyleWithColumns(15); 11455 TwoIndent.ContinuationIndentWidth = 2; 11456 11457 EXPECT_EQ("int i =\n" 11458 " longFunction(\n" 11459 " arg);", 11460 format("int i = longFunction(arg);", TwoIndent)); 11461 11462 FormatStyle SixIndent = getLLVMStyleWithColumns(20); 11463 SixIndent.ContinuationIndentWidth = 6; 11464 11465 EXPECT_EQ("int i =\n" 11466 " longFunction(\n" 11467 " arg);", 11468 format("int i = longFunction(arg);", SixIndent)); 11469 } 11470 11471 TEST_F(FormatTest, SpacesInAngles) { 11472 FormatStyle Spaces = getLLVMStyle(); 11473 Spaces.SpacesInAngles = true; 11474 11475 verifyFormat("static_cast< int >(arg);", Spaces); 11476 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces); 11477 verifyFormat("f< int, float >();", Spaces); 11478 verifyFormat("template <> g() {}", Spaces); 11479 verifyFormat("template < std::vector< int > > f() {}", Spaces); 11480 verifyFormat("std::function< void(int, int) > fct;", Spaces); 11481 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }", 11482 Spaces); 11483 11484 Spaces.Standard = FormatStyle::LS_Cpp03; 11485 Spaces.SpacesInAngles = true; 11486 verifyFormat("A< A< int > >();", Spaces); 11487 11488 Spaces.SpacesInAngles = false; 11489 verifyFormat("A<A<int> >();", Spaces); 11490 11491 Spaces.Standard = FormatStyle::LS_Cpp11; 11492 Spaces.SpacesInAngles = true; 11493 verifyFormat("A< A< int > >();", Spaces); 11494 11495 Spaces.SpacesInAngles = false; 11496 verifyFormat("A<A<int>>();", Spaces); 11497 } 11498 11499 TEST_F(FormatTest, SpaceAfterTemplateKeyword) { 11500 FormatStyle Style = getLLVMStyle(); 11501 Style.SpaceAfterTemplateKeyword = false; 11502 verifyFormat("template<int> void foo();", Style); 11503 } 11504 11505 TEST_F(FormatTest, TripleAngleBrackets) { 11506 verifyFormat("f<<<1, 1>>>();"); 11507 verifyFormat("f<<<1, 1, 1, s>>>();"); 11508 verifyFormat("f<<<a, b, c, d>>>();"); 11509 EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();")); 11510 verifyFormat("f<param><<<1, 1>>>();"); 11511 verifyFormat("f<1><<<1, 1>>>();"); 11512 EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();")); 11513 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 11514 "aaaaaaaaaaa<<<\n 1, 1>>>();"); 11515 verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n" 11516 " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();"); 11517 } 11518 11519 TEST_F(FormatTest, MergeLessLessAtEnd) { 11520 verifyFormat("<<"); 11521 EXPECT_EQ("< < <", format("\\\n<<<")); 11522 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 11523 "aaallvm::outs() <<"); 11524 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 11525 "aaaallvm::outs()\n <<"); 11526 } 11527 11528 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) { 11529 std::string code = "#if A\n" 11530 "#if B\n" 11531 "a.\n" 11532 "#endif\n" 11533 " a = 1;\n" 11534 "#else\n" 11535 "#endif\n" 11536 "#if C\n" 11537 "#else\n" 11538 "#endif\n"; 11539 EXPECT_EQ(code, format(code)); 11540 } 11541 11542 TEST_F(FormatTest, HandleConflictMarkers) { 11543 // Git/SVN conflict markers. 11544 EXPECT_EQ("int a;\n" 11545 "void f() {\n" 11546 " callme(some(parameter1,\n" 11547 "<<<<<<< text by the vcs\n" 11548 " parameter2),\n" 11549 "||||||| text by the vcs\n" 11550 " parameter2),\n" 11551 " parameter3,\n" 11552 "======= text by the vcs\n" 11553 " parameter2, parameter3),\n" 11554 ">>>>>>> text by the vcs\n" 11555 " otherparameter);\n", 11556 format("int a;\n" 11557 "void f() {\n" 11558 " callme(some(parameter1,\n" 11559 "<<<<<<< text by the vcs\n" 11560 " parameter2),\n" 11561 "||||||| text by the vcs\n" 11562 " parameter2),\n" 11563 " parameter3,\n" 11564 "======= text by the vcs\n" 11565 " parameter2,\n" 11566 " parameter3),\n" 11567 ">>>>>>> text by the vcs\n" 11568 " otherparameter);\n")); 11569 11570 // Perforce markers. 11571 EXPECT_EQ("void f() {\n" 11572 " function(\n" 11573 ">>>> text by the vcs\n" 11574 " parameter,\n" 11575 "==== text by the vcs\n" 11576 " parameter,\n" 11577 "==== text by the vcs\n" 11578 " parameter,\n" 11579 "<<<< text by the vcs\n" 11580 " parameter);\n", 11581 format("void f() {\n" 11582 " function(\n" 11583 ">>>> text by the vcs\n" 11584 " parameter,\n" 11585 "==== text by the vcs\n" 11586 " parameter,\n" 11587 "==== text by the vcs\n" 11588 " parameter,\n" 11589 "<<<< text by the vcs\n" 11590 " parameter);\n")); 11591 11592 EXPECT_EQ("<<<<<<<\n" 11593 "|||||||\n" 11594 "=======\n" 11595 ">>>>>>>", 11596 format("<<<<<<<\n" 11597 "|||||||\n" 11598 "=======\n" 11599 ">>>>>>>")); 11600 11601 EXPECT_EQ("<<<<<<<\n" 11602 "|||||||\n" 11603 "int i;\n" 11604 "=======\n" 11605 ">>>>>>>", 11606 format("<<<<<<<\n" 11607 "|||||||\n" 11608 "int i;\n" 11609 "=======\n" 11610 ">>>>>>>")); 11611 11612 // FIXME: Handle parsing of macros around conflict markers correctly: 11613 EXPECT_EQ("#define Macro \\\n" 11614 "<<<<<<<\n" 11615 "Something \\\n" 11616 "|||||||\n" 11617 "Else \\\n" 11618 "=======\n" 11619 "Other \\\n" 11620 ">>>>>>>\n" 11621 " End int i;\n", 11622 format("#define Macro \\\n" 11623 "<<<<<<<\n" 11624 " Something \\\n" 11625 "|||||||\n" 11626 " Else \\\n" 11627 "=======\n" 11628 " Other \\\n" 11629 ">>>>>>>\n" 11630 " End\n" 11631 "int i;\n")); 11632 } 11633 11634 TEST_F(FormatTest, DisableRegions) { 11635 EXPECT_EQ("int i;\n" 11636 "// clang-format off\n" 11637 " int j;\n" 11638 "// clang-format on\n" 11639 "int k;", 11640 format(" int i;\n" 11641 " // clang-format off\n" 11642 " int j;\n" 11643 " // clang-format on\n" 11644 " int k;")); 11645 EXPECT_EQ("int i;\n" 11646 "/* clang-format off */\n" 11647 " int j;\n" 11648 "/* clang-format on */\n" 11649 "int k;", 11650 format(" int i;\n" 11651 " /* clang-format off */\n" 11652 " int j;\n" 11653 " /* clang-format on */\n" 11654 " int k;")); 11655 11656 // Don't reflow comments within disabled regions. 11657 EXPECT_EQ( 11658 "// clang-format off\n" 11659 "// long long long long long long line\n" 11660 "/* clang-format on */\n" 11661 "/* long long long\n" 11662 " * long long long\n" 11663 " * line */\n" 11664 "int i;\n" 11665 "/* clang-format off */\n" 11666 "/* long long long long long long line */\n", 11667 format("// clang-format off\n" 11668 "// long long long long long long line\n" 11669 "/* clang-format on */\n" 11670 "/* long long long long long long line */\n" 11671 "int i;\n" 11672 "/* clang-format off */\n" 11673 "/* long long long long long long line */\n", 11674 getLLVMStyleWithColumns(20))); 11675 } 11676 11677 TEST_F(FormatTest, DoNotCrashOnInvalidInput) { 11678 format("? ) ="); 11679 verifyNoCrash("#define a\\\n /**/}"); 11680 } 11681 11682 TEST_F(FormatTest, FormatsTableGenCode) { 11683 FormatStyle Style = getLLVMStyle(); 11684 Style.Language = FormatStyle::LK_TableGen; 11685 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style); 11686 } 11687 11688 TEST_F(FormatTest, ArrayOfTemplates) { 11689 EXPECT_EQ("auto a = new unique_ptr<int>[10];", 11690 format("auto a = new unique_ptr<int > [ 10];")); 11691 11692 FormatStyle Spaces = getLLVMStyle(); 11693 Spaces.SpacesInSquareBrackets = true; 11694 EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];", 11695 format("auto a = new unique_ptr<int > [10];", Spaces)); 11696 } 11697 11698 TEST_F(FormatTest, ArrayAsTemplateType) { 11699 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;", 11700 format("auto a = unique_ptr < Foo < Bar>[ 10]> ;")); 11701 11702 FormatStyle Spaces = getLLVMStyle(); 11703 Spaces.SpacesInSquareBrackets = true; 11704 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;", 11705 format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces)); 11706 } 11707 11708 TEST_F(FormatTest, NoSpaceAfterSuper) { 11709 verifyFormat("__super::FooBar();"); 11710 } 11711 11712 TEST(FormatStyle, GetStyleOfFile) { 11713 vfs::InMemoryFileSystem FS; 11714 // Test 1: format file in the same directory. 11715 ASSERT_TRUE( 11716 FS.addFile("/a/.clang-format", 0, 11717 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM"))); 11718 ASSERT_TRUE( 11719 FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 11720 auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS); 11721 ASSERT_TRUE((bool)Style1); 11722 ASSERT_EQ(*Style1, getLLVMStyle()); 11723 11724 // Test 2.1: fallback to default. 11725 ASSERT_TRUE( 11726 FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 11727 auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS); 11728 ASSERT_TRUE((bool)Style2); 11729 ASSERT_EQ(*Style2, getMozillaStyle()); 11730 11731 // Test 2.2: no format on 'none' fallback style. 11732 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 11733 ASSERT_TRUE((bool)Style2); 11734 ASSERT_EQ(*Style2, getNoStyle()); 11735 11736 // Test 2.3: format if config is found with no based style while fallback is 11737 // 'none'. 11738 ASSERT_TRUE(FS.addFile("/b/.clang-format", 0, 11739 llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2"))); 11740 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 11741 ASSERT_TRUE((bool)Style2); 11742 ASSERT_EQ(*Style2, getLLVMStyle()); 11743 11744 // Test 2.4: format if yaml with no based style, while fallback is 'none'. 11745 Style2 = getStyle("{}", "a.h", "none", "", &FS); 11746 ASSERT_TRUE((bool)Style2); 11747 ASSERT_EQ(*Style2, getLLVMStyle()); 11748 11749 // Test 3: format file in parent directory. 11750 ASSERT_TRUE( 11751 FS.addFile("/c/.clang-format", 0, 11752 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 11753 ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0, 11754 llvm::MemoryBuffer::getMemBuffer("int i;"))); 11755 auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS); 11756 ASSERT_TRUE((bool)Style3); 11757 ASSERT_EQ(*Style3, getGoogleStyle()); 11758 11759 // Test 4: error on invalid fallback style 11760 auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS); 11761 ASSERT_FALSE((bool)Style4); 11762 llvm::consumeError(Style4.takeError()); 11763 11764 // Test 5: error on invalid yaml on command line 11765 auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS); 11766 ASSERT_FALSE((bool)Style5); 11767 llvm::consumeError(Style5.takeError()); 11768 11769 // Test 6: error on invalid style 11770 auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS); 11771 ASSERT_FALSE((bool)Style6); 11772 llvm::consumeError(Style6.takeError()); 11773 11774 // Test 7: found config file, error on parsing it 11775 ASSERT_TRUE( 11776 FS.addFile("/d/.clang-format", 0, 11777 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n" 11778 "InvalidKey: InvalidValue"))); 11779 ASSERT_TRUE( 11780 FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 11781 auto Style7 = getStyle("file", "/d/.clang-format", "LLVM", "", &FS); 11782 ASSERT_FALSE((bool)Style7); 11783 llvm::consumeError(Style7.takeError()); 11784 } 11785 11786 TEST_F(ReplacementTest, FormatCodeAfterReplacements) { 11787 // Column limit is 20. 11788 std::string Code = "Type *a =\n" 11789 " new Type();\n" 11790 "g(iiiii, 0, jjjjj,\n" 11791 " 0, kkkkk, 0, mm);\n" 11792 "int bad = format ;"; 11793 std::string Expected = "auto a = new Type();\n" 11794 "g(iiiii, nullptr,\n" 11795 " jjjjj, nullptr,\n" 11796 " kkkkk, nullptr,\n" 11797 " mm);\n" 11798 "int bad = format ;"; 11799 FileID ID = Context.createInMemoryFile("format.cpp", Code); 11800 tooling::Replacements Replaces = toReplacements( 11801 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6, 11802 "auto "), 11803 tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1, 11804 "nullptr"), 11805 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1, 11806 "nullptr"), 11807 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1, 11808 "nullptr")}); 11809 11810 format::FormatStyle Style = format::getLLVMStyle(); 11811 Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility. 11812 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 11813 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 11814 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 11815 auto Result = applyAllReplacements(Code, *FormattedReplaces); 11816 EXPECT_TRUE(static_cast<bool>(Result)); 11817 EXPECT_EQ(Expected, *Result); 11818 } 11819 11820 TEST_F(ReplacementTest, SortIncludesAfterReplacement) { 11821 std::string Code = "#include \"a.h\"\n" 11822 "#include \"c.h\"\n" 11823 "\n" 11824 "int main() {\n" 11825 " return 0;\n" 11826 "}"; 11827 std::string Expected = "#include \"a.h\"\n" 11828 "#include \"b.h\"\n" 11829 "#include \"c.h\"\n" 11830 "\n" 11831 "int main() {\n" 11832 " return 0;\n" 11833 "}"; 11834 FileID ID = Context.createInMemoryFile("fix.cpp", Code); 11835 tooling::Replacements Replaces = toReplacements( 11836 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0, 11837 "#include \"b.h\"\n")}); 11838 11839 format::FormatStyle Style = format::getLLVMStyle(); 11840 Style.SortIncludes = true; 11841 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 11842 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 11843 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 11844 auto Result = applyAllReplacements(Code, *FormattedReplaces); 11845 EXPECT_TRUE(static_cast<bool>(Result)); 11846 EXPECT_EQ(Expected, *Result); 11847 } 11848 11849 TEST_F(FormatTest, FormatSortsUsingDeclarations) { 11850 EXPECT_EQ("using std::cin;\n" 11851 "using std::cout;", 11852 format("using std::cout;\n" 11853 "using std::cin;", getGoogleStyle())); 11854 } 11855 11856 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) { 11857 format::FormatStyle Style = format::getLLVMStyle(); 11858 Style.Standard = FormatStyle::LS_Cpp03; 11859 // cpp03 recognize this string as identifier u8 and literal character 'a' 11860 EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style)); 11861 } 11862 11863 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) { 11864 // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers 11865 // all modes, including C++11, C++14 and C++17 11866 EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';")); 11867 } 11868 11869 TEST_F(FormatTest, DoNotFormatLikelyXml) { 11870 EXPECT_EQ("<!-- ;> -->", 11871 format("<!-- ;> -->", getGoogleStyle())); 11872 EXPECT_EQ(" <!-- >; -->", 11873 format(" <!-- >; -->", getGoogleStyle())); 11874 } 11875 11876 TEST_F(FormatTest, StructuredBindings) { 11877 // Structured bindings is a C++17 feature. 11878 // all modes, including C++11, C++14 and C++17 11879 verifyFormat("auto [a, b] = f();"); 11880 EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();")); 11881 EXPECT_EQ("const auto [a, b] = f();", format("const auto[a, b] = f();")); 11882 EXPECT_EQ("auto const [a, b] = f();", format("auto const[a, b] = f();")); 11883 EXPECT_EQ("auto const volatile [a, b] = f();", 11884 format("auto const volatile[a, b] = f();")); 11885 EXPECT_EQ("auto [a, b, c] = f();", format("auto [ a , b,c ] = f();")); 11886 EXPECT_EQ("auto &[a, b, c] = f();", 11887 format("auto &[ a , b,c ] = f();")); 11888 EXPECT_EQ("auto &&[a, b, c] = f();", 11889 format("auto &&[ a , b,c ] = f();")); 11890 EXPECT_EQ("auto const &[a, b] = f();", format("auto const&[a, b] = f();")); 11891 EXPECT_EQ("auto const volatile &&[a, b] = f();", 11892 format("auto const volatile &&[a, b] = f();")); 11893 EXPECT_EQ("auto const &&[a, b] = f();", format("auto const && [a, b] = f();")); 11894 EXPECT_EQ("const auto &[a, b] = f();", format("const auto & [a, b] = f();")); 11895 EXPECT_EQ("const auto volatile &&[a, b] = f();", 11896 format("const auto volatile &&[a, b] = f();")); 11897 EXPECT_EQ("volatile const auto &&[a, b] = f();", 11898 format("volatile const auto &&[a, b] = f();")); 11899 EXPECT_EQ("const auto &&[a, b] = f();", format("const auto && [a, b] = f();")); 11900 11901 // Make sure we don't mistake structured bindings for lambdas. 11902 FormatStyle PointerMiddle = getLLVMStyle(); 11903 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 11904 verifyFormat("auto [a1, b]{A * i};", getGoogleStyle()); 11905 verifyFormat("auto [a2, b]{A * i};", getLLVMStyle()); 11906 verifyFormat("auto [a3, b]{A * i};", PointerMiddle); 11907 verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle()); 11908 verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle()); 11909 verifyFormat("auto const [a3, b]{A * i};", PointerMiddle); 11910 verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle()); 11911 verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle()); 11912 verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle); 11913 verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle()); 11914 verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle()); 11915 verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle); 11916 11917 EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}", 11918 format("for (const auto && [a, b] : some_range) {\n}")); 11919 EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}", 11920 format("for (const auto & [a, b] : some_range) {\n}")); 11921 EXPECT_EQ("for (const auto [a, b] : some_range) {\n}", 11922 format("for (const auto[a, b] : some_range) {\n}")); 11923 EXPECT_EQ("auto [x, y](expr);", format("auto[x,y] (expr);")); 11924 EXPECT_EQ("auto &[x, y](expr);", format("auto & [x,y] (expr);")); 11925 EXPECT_EQ("auto &&[x, y](expr);", format("auto && [x,y] (expr);")); 11926 EXPECT_EQ("auto const &[x, y](expr);", format("auto const & [x,y] (expr);")); 11927 EXPECT_EQ("auto const &&[x, y](expr);", format("auto const && [x,y] (expr);")); 11928 EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y] {expr};")); 11929 EXPECT_EQ("auto const &[x, y]{expr};", format("auto const & [x,y] {expr};")); 11930 EXPECT_EQ("auto const &&[x, y]{expr};", format("auto const && [x,y] {expr};")); 11931 11932 format::FormatStyle Spaces = format::getLLVMStyle(); 11933 Spaces.SpacesInSquareBrackets = true; 11934 verifyFormat("auto [ a, b ] = f();", Spaces); 11935 verifyFormat("auto &&[ a, b ] = f();", Spaces); 11936 verifyFormat("auto &[ a, b ] = f();", Spaces); 11937 verifyFormat("auto const &&[ a, b ] = f();", Spaces); 11938 verifyFormat("auto const &[ a, b ] = f();", Spaces); 11939 } 11940 11941 } // end namespace 11942 } // end namespace format 11943 } // end namespace clang 11944