1 //===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "clang/Format/Format.h" 11 12 #include "../Tooling/ReplacementTest.h" 13 #include "FormatTestUtils.h" 14 15 #include "clang/Frontend/TextDiagnosticPrinter.h" 16 #include "llvm/Support/Debug.h" 17 #include "llvm/Support/MemoryBuffer.h" 18 #include "gtest/gtest.h" 19 20 #define DEBUG_TYPE "format-test" 21 22 using clang::tooling::ReplacementTest; 23 using clang::tooling::toReplacements; 24 25 namespace clang { 26 namespace format { 27 namespace { 28 29 FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); } 30 31 class FormatTest : public ::testing::Test { 32 protected: 33 enum StatusCheck { 34 SC_ExpectComplete, 35 SC_ExpectIncomplete, 36 SC_DoNotCheck 37 }; 38 39 std::string format(llvm::StringRef Code, 40 const FormatStyle &Style = getLLVMStyle(), 41 StatusCheck CheckComplete = SC_ExpectComplete) { 42 DEBUG(llvm::errs() << "---\n"); 43 DEBUG(llvm::errs() << Code << "\n\n"); 44 std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size())); 45 FormattingAttemptStatus Status; 46 tooling::Replacements Replaces = 47 reformat(Style, Code, Ranges, "<stdin>", &Status); 48 if (CheckComplete != SC_DoNotCheck) { 49 bool ExpectedCompleteFormat = CheckComplete == SC_ExpectComplete; 50 EXPECT_EQ(ExpectedCompleteFormat, Status.FormatComplete) 51 << Code << "\n\n"; 52 } 53 ReplacementCount = Replaces.size(); 54 auto Result = applyAllReplacements(Code, Replaces); 55 EXPECT_TRUE(static_cast<bool>(Result)); 56 DEBUG(llvm::errs() << "\n" << *Result << "\n\n"); 57 return *Result; 58 } 59 60 FormatStyle getStyleWithColumns(FormatStyle Style, unsigned ColumnLimit) { 61 Style.ColumnLimit = ColumnLimit; 62 return Style; 63 } 64 65 FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) { 66 return getStyleWithColumns(getLLVMStyle(), ColumnLimit); 67 } 68 69 FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) { 70 return getStyleWithColumns(getGoogleStyle(), ColumnLimit); 71 } 72 73 void verifyFormat(llvm::StringRef Code, 74 const FormatStyle &Style = getLLVMStyle()) { 75 EXPECT_EQ(Code.str(), format(test::messUp(Code), Style)); 76 if (Style.Language == FormatStyle::LK_Cpp) { 77 // Objective-C++ is a superset of C++, so everything checked for C++ 78 // needs to be checked for Objective-C++ as well. 79 FormatStyle ObjCStyle = Style; 80 ObjCStyle.Language = FormatStyle::LK_ObjC; 81 EXPECT_EQ(Code.str(), format(test::messUp(Code), ObjCStyle)); 82 } 83 } 84 85 void verifyIncompleteFormat(llvm::StringRef Code, 86 const FormatStyle &Style = getLLVMStyle()) { 87 EXPECT_EQ(Code.str(), 88 format(test::messUp(Code), Style, SC_ExpectIncomplete)); 89 } 90 91 void verifyGoogleFormat(llvm::StringRef Code) { 92 verifyFormat(Code, getGoogleStyle()); 93 } 94 95 void verifyIndependentOfContext(llvm::StringRef text) { 96 verifyFormat(text); 97 verifyFormat(llvm::Twine("void f() { " + text + " }").str()); 98 } 99 100 /// \brief Verify that clang-format does not crash on the given input. 101 void verifyNoCrash(llvm::StringRef Code, 102 const FormatStyle &Style = getLLVMStyle()) { 103 format(Code, Style, SC_DoNotCheck); 104 } 105 106 int ReplacementCount; 107 }; 108 109 TEST_F(FormatTest, MessUp) { 110 EXPECT_EQ("1 2 3", test::messUp("1 2 3")); 111 EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n")); 112 EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc")); 113 EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc")); 114 EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne")); 115 } 116 117 //===----------------------------------------------------------------------===// 118 // Basic function tests. 119 //===----------------------------------------------------------------------===// 120 121 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) { 122 EXPECT_EQ(";", format(";")); 123 } 124 125 TEST_F(FormatTest, FormatsGlobalStatementsAt0) { 126 EXPECT_EQ("int i;", format(" int i;")); 127 EXPECT_EQ("\nint i;", format(" \n\t \v \f int i;")); 128 EXPECT_EQ("int i;\nint j;", format(" int i; int j;")); 129 EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;")); 130 } 131 132 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) { 133 EXPECT_EQ("int i;", format("int\ni;")); 134 } 135 136 TEST_F(FormatTest, FormatsNestedBlockStatements) { 137 EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}")); 138 } 139 140 TEST_F(FormatTest, FormatsNestedCall) { 141 verifyFormat("Method(f1, f2(f3));"); 142 verifyFormat("Method(f1(f2, f3()));"); 143 verifyFormat("Method(f1(f2, (f3())));"); 144 } 145 146 TEST_F(FormatTest, NestedNameSpecifiers) { 147 verifyFormat("vector<::Type> v;"); 148 verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())"); 149 verifyFormat("static constexpr bool Bar = decltype(bar())::value;"); 150 verifyFormat("bool a = 2 < ::SomeFunction();"); 151 verifyFormat("ALWAYS_INLINE ::std::string getName();"); 152 verifyFormat("some::string getName();"); 153 } 154 155 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) { 156 EXPECT_EQ("if (a) {\n" 157 " f();\n" 158 "}", 159 format("if(a){f();}")); 160 EXPECT_EQ(4, ReplacementCount); 161 EXPECT_EQ("if (a) {\n" 162 " f();\n" 163 "}", 164 format("if (a) {\n" 165 " f();\n" 166 "}")); 167 EXPECT_EQ(0, ReplacementCount); 168 EXPECT_EQ("/*\r\n" 169 "\r\n" 170 "*/\r\n", 171 format("/*\r\n" 172 "\r\n" 173 "*/\r\n")); 174 EXPECT_EQ(0, ReplacementCount); 175 } 176 177 TEST_F(FormatTest, RemovesEmptyLines) { 178 EXPECT_EQ("class C {\n" 179 " int i;\n" 180 "};", 181 format("class C {\n" 182 " int i;\n" 183 "\n" 184 "};")); 185 186 // Don't remove empty lines at the start of namespaces or extern "C" blocks. 187 EXPECT_EQ("namespace N {\n" 188 "\n" 189 "int i;\n" 190 "}", 191 format("namespace N {\n" 192 "\n" 193 "int i;\n" 194 "}", 195 getGoogleStyle())); 196 EXPECT_EQ("extern /**/ \"C\" /**/ {\n" 197 "\n" 198 "int i;\n" 199 "}", 200 format("extern /**/ \"C\" /**/ {\n" 201 "\n" 202 "int i;\n" 203 "}", 204 getGoogleStyle())); 205 206 // ...but do keep inlining and removing empty lines for non-block extern "C" 207 // functions. 208 verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle()); 209 EXPECT_EQ("extern \"C\" int f() {\n" 210 " int i = 42;\n" 211 " return i;\n" 212 "}", 213 format("extern \"C\" int f() {\n" 214 "\n" 215 " int i = 42;\n" 216 " return i;\n" 217 "}", 218 getGoogleStyle())); 219 220 // Remove empty lines at the beginning and end of blocks. 221 EXPECT_EQ("void f() {\n" 222 "\n" 223 " if (a) {\n" 224 "\n" 225 " f();\n" 226 " }\n" 227 "}", 228 format("void f() {\n" 229 "\n" 230 " if (a) {\n" 231 "\n" 232 " f();\n" 233 "\n" 234 " }\n" 235 "\n" 236 "}", 237 getLLVMStyle())); 238 EXPECT_EQ("void f() {\n" 239 " if (a) {\n" 240 " f();\n" 241 " }\n" 242 "}", 243 format("void f() {\n" 244 "\n" 245 " if (a) {\n" 246 "\n" 247 " f();\n" 248 "\n" 249 " }\n" 250 "\n" 251 "}", 252 getGoogleStyle())); 253 254 // Don't remove empty lines in more complex control statements. 255 EXPECT_EQ("void f() {\n" 256 " if (a) {\n" 257 " f();\n" 258 "\n" 259 " } else if (b) {\n" 260 " f();\n" 261 " }\n" 262 "}", 263 format("void f() {\n" 264 " if (a) {\n" 265 " f();\n" 266 "\n" 267 " } else if (b) {\n" 268 " f();\n" 269 "\n" 270 " }\n" 271 "\n" 272 "}")); 273 274 // FIXME: This is slightly inconsistent. 275 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 276 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 277 EXPECT_EQ("namespace {\n" 278 "int i;\n" 279 "}", 280 format("namespace {\n" 281 "int i;\n" 282 "\n" 283 "}", LLVMWithNoNamespaceFix)); 284 EXPECT_EQ("namespace {\n" 285 "int i;\n" 286 "}", 287 format("namespace {\n" 288 "int i;\n" 289 "\n" 290 "}")); 291 EXPECT_EQ("namespace {\n" 292 "int i;\n" 293 "\n" 294 "} // namespace", 295 format("namespace {\n" 296 "int i;\n" 297 "\n" 298 "} // namespace")); 299 300 FormatStyle Style = getLLVMStyle(); 301 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 302 Style.MaxEmptyLinesToKeep = 2; 303 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 304 Style.BraceWrapping.AfterClass = true; 305 Style.BraceWrapping.AfterFunction = true; 306 Style.KeepEmptyLinesAtTheStartOfBlocks = false; 307 308 EXPECT_EQ("class Foo\n" 309 "{\n" 310 " Foo() {}\n" 311 "\n" 312 " void funk() {}\n" 313 "};", 314 format("class Foo\n" 315 "{\n" 316 " Foo()\n" 317 " {\n" 318 " }\n" 319 "\n" 320 " void funk() {}\n" 321 "};", 322 Style)); 323 } 324 325 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) { 326 verifyFormat("x = (a) and (b);"); 327 verifyFormat("x = (a) or (b);"); 328 verifyFormat("x = (a) bitand (b);"); 329 verifyFormat("x = (a) bitor (b);"); 330 verifyFormat("x = (a) not_eq (b);"); 331 verifyFormat("x = (a) and_eq (b);"); 332 verifyFormat("x = (a) or_eq (b);"); 333 verifyFormat("x = (a) xor (b);"); 334 } 335 336 //===----------------------------------------------------------------------===// 337 // Tests for control statements. 338 //===----------------------------------------------------------------------===// 339 340 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) { 341 verifyFormat("if (true)\n f();\ng();"); 342 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); 343 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); 344 verifyFormat("if constexpr (true)\n" 345 " f();\ng();"); 346 verifyFormat("if constexpr (a)\n" 347 " if constexpr (b)\n" 348 " if constexpr (c)\n" 349 " g();\n" 350 "h();"); 351 verifyFormat("if constexpr (a)\n" 352 " if constexpr (b) {\n" 353 " f();\n" 354 " }\n" 355 "g();"); 356 357 FormatStyle AllowsMergedIf = getLLVMStyle(); 358 AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left; 359 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 360 verifyFormat("if (a)\n" 361 " // comment\n" 362 " f();", 363 AllowsMergedIf); 364 verifyFormat("{\n" 365 " if (a)\n" 366 " label:\n" 367 " f();\n" 368 "}", 369 AllowsMergedIf); 370 verifyFormat("#define A \\\n" 371 " if (a) \\\n" 372 " label: \\\n" 373 " f()", 374 AllowsMergedIf); 375 verifyFormat("if (a)\n" 376 " ;", 377 AllowsMergedIf); 378 verifyFormat("if (a)\n" 379 " if (b) return;", 380 AllowsMergedIf); 381 382 verifyFormat("if (a) // Can't merge this\n" 383 " f();\n", 384 AllowsMergedIf); 385 verifyFormat("if (a) /* still don't merge */\n" 386 " f();", 387 AllowsMergedIf); 388 verifyFormat("if (a) { // Never merge this\n" 389 " f();\n" 390 "}", 391 AllowsMergedIf); 392 verifyFormat("if (a) { /* Never merge this */\n" 393 " f();\n" 394 "}", 395 AllowsMergedIf); 396 397 AllowsMergedIf.ColumnLimit = 14; 398 verifyFormat("if (a) return;", AllowsMergedIf); 399 verifyFormat("if (aaaaaaaaa)\n" 400 " return;", 401 AllowsMergedIf); 402 403 AllowsMergedIf.ColumnLimit = 13; 404 verifyFormat("if (a)\n return;", AllowsMergedIf); 405 } 406 407 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) { 408 FormatStyle AllowsMergedLoops = getLLVMStyle(); 409 AllowsMergedLoops.AllowShortLoopsOnASingleLine = true; 410 verifyFormat("while (true) continue;", AllowsMergedLoops); 411 verifyFormat("for (;;) continue;", AllowsMergedLoops); 412 verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops); 413 verifyFormat("while (true)\n" 414 " ;", 415 AllowsMergedLoops); 416 verifyFormat("for (;;)\n" 417 " ;", 418 AllowsMergedLoops); 419 verifyFormat("for (;;)\n" 420 " for (;;) continue;", 421 AllowsMergedLoops); 422 verifyFormat("for (;;) // Can't merge this\n" 423 " continue;", 424 AllowsMergedLoops); 425 verifyFormat("for (;;) /* still don't merge */\n" 426 " continue;", 427 AllowsMergedLoops); 428 } 429 430 TEST_F(FormatTest, FormatShortBracedStatements) { 431 FormatStyle AllowSimpleBracedStatements = getLLVMStyle(); 432 AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true; 433 434 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true; 435 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 436 437 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 438 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements); 439 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 440 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 441 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 442 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements); 443 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 444 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 445 verifyFormat("if (true) { //\n" 446 " f();\n" 447 "}", 448 AllowSimpleBracedStatements); 449 verifyFormat("if (true) {\n" 450 " f();\n" 451 " f();\n" 452 "}", 453 AllowSimpleBracedStatements); 454 verifyFormat("if (true) {\n" 455 " f();\n" 456 "} else {\n" 457 " f();\n" 458 "}", 459 AllowSimpleBracedStatements); 460 461 verifyFormat("struct A2 {\n" 462 " int X;\n" 463 "};", 464 AllowSimpleBracedStatements); 465 verifyFormat("typedef struct A2 {\n" 466 " int X;\n" 467 "} A2_t;", 468 AllowSimpleBracedStatements); 469 verifyFormat("template <int> struct A2 {\n" 470 " struct B {};\n" 471 "};", 472 AllowSimpleBracedStatements); 473 474 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false; 475 verifyFormat("if (true) {\n" 476 " f();\n" 477 "}", 478 AllowSimpleBracedStatements); 479 verifyFormat("if (true) {\n" 480 " f();\n" 481 "} else {\n" 482 " f();\n" 483 "}", 484 AllowSimpleBracedStatements); 485 486 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 487 verifyFormat("while (true) {\n" 488 " f();\n" 489 "}", 490 AllowSimpleBracedStatements); 491 verifyFormat("for (;;) {\n" 492 " f();\n" 493 "}", 494 AllowSimpleBracedStatements); 495 } 496 497 TEST_F(FormatTest, ParseIfElse) { 498 verifyFormat("if (true)\n" 499 " if (true)\n" 500 " if (true)\n" 501 " f();\n" 502 " else\n" 503 " g();\n" 504 " else\n" 505 " h();\n" 506 "else\n" 507 " i();"); 508 verifyFormat("if (true)\n" 509 " if (true)\n" 510 " if (true) {\n" 511 " if (true)\n" 512 " f();\n" 513 " } else {\n" 514 " g();\n" 515 " }\n" 516 " else\n" 517 " h();\n" 518 "else {\n" 519 " i();\n" 520 "}"); 521 verifyFormat("if (true)\n" 522 " if constexpr (true)\n" 523 " if (true) {\n" 524 " if constexpr (true)\n" 525 " f();\n" 526 " } else {\n" 527 " g();\n" 528 " }\n" 529 " else\n" 530 " h();\n" 531 "else {\n" 532 " i();\n" 533 "}"); 534 verifyFormat("void f() {\n" 535 " if (a) {\n" 536 " } else {\n" 537 " }\n" 538 "}"); 539 } 540 541 TEST_F(FormatTest, ElseIf) { 542 verifyFormat("if (a) {\n} else if (b) {\n}"); 543 verifyFormat("if (a)\n" 544 " f();\n" 545 "else if (b)\n" 546 " g();\n" 547 "else\n" 548 " h();"); 549 verifyFormat("if constexpr (a)\n" 550 " f();\n" 551 "else if constexpr (b)\n" 552 " g();\n" 553 "else\n" 554 " h();"); 555 verifyFormat("if (a) {\n" 556 " f();\n" 557 "}\n" 558 "// or else ..\n" 559 "else {\n" 560 " g()\n" 561 "}"); 562 563 verifyFormat("if (a) {\n" 564 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 565 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 566 "}"); 567 verifyFormat("if (a) {\n" 568 "} else if (\n" 569 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 570 "}", 571 getLLVMStyleWithColumns(62)); 572 verifyFormat("if (a) {\n" 573 "} else if constexpr (\n" 574 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 575 "}", 576 getLLVMStyleWithColumns(62)); 577 } 578 579 TEST_F(FormatTest, FormatsForLoop) { 580 verifyFormat( 581 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" 582 " ++VeryVeryLongLoopVariable)\n" 583 " ;"); 584 verifyFormat("for (;;)\n" 585 " f();"); 586 verifyFormat("for (;;) {\n}"); 587 verifyFormat("for (;;) {\n" 588 " f();\n" 589 "}"); 590 verifyFormat("for (int i = 0; (i < 10); ++i) {\n}"); 591 592 verifyFormat( 593 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 594 " E = UnwrappedLines.end();\n" 595 " I != E; ++I) {\n}"); 596 597 verifyFormat( 598 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" 599 " ++IIIII) {\n}"); 600 verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n" 601 " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n" 602 " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}"); 603 verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n" 604 " I = FD->getDeclsInPrototypeScope().begin(),\n" 605 " E = FD->getDeclsInPrototypeScope().end();\n" 606 " I != E; ++I) {\n}"); 607 verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n" 608 " I = Container.begin(),\n" 609 " E = Container.end();\n" 610 " I != E; ++I) {\n}", 611 getLLVMStyleWithColumns(76)); 612 613 verifyFormat( 614 "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 615 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n" 616 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 617 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 618 " ++aaaaaaaaaaa) {\n}"); 619 verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 620 " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n" 621 " ++i) {\n}"); 622 verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n" 623 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 624 "}"); 625 verifyFormat("for (some_namespace::SomeIterator iter( // force break\n" 626 " aaaaaaaaaa);\n" 627 " iter; ++iter) {\n" 628 "}"); 629 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 630 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 631 " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n" 632 " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {"); 633 634 FormatStyle NoBinPacking = getLLVMStyle(); 635 NoBinPacking.BinPackParameters = false; 636 verifyFormat("for (int aaaaaaaaaaa = 1;\n" 637 " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n" 638 " aaaaaaaaaaaaaaaa,\n" 639 " aaaaaaaaaaaaaaaa,\n" 640 " aaaaaaaaaaaaaaaa);\n" 641 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 642 "}", 643 NoBinPacking); 644 verifyFormat( 645 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 646 " E = UnwrappedLines.end();\n" 647 " I != E;\n" 648 " ++I) {\n}", 649 NoBinPacking); 650 } 651 652 TEST_F(FormatTest, RangeBasedForLoops) { 653 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 654 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 655 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n" 656 " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}"); 657 verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n" 658 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 659 verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n" 660 " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}"); 661 } 662 663 TEST_F(FormatTest, ForEachLoops) { 664 verifyFormat("void f() {\n" 665 " foreach (Item *item, itemlist) {}\n" 666 " Q_FOREACH (Item *item, itemlist) {}\n" 667 " BOOST_FOREACH (Item *item, itemlist) {}\n" 668 " UNKNOWN_FORACH(Item * item, itemlist) {}\n" 669 "}"); 670 671 // As function-like macros. 672 verifyFormat("#define foreach(x, y)\n" 673 "#define Q_FOREACH(x, y)\n" 674 "#define BOOST_FOREACH(x, y)\n" 675 "#define UNKNOWN_FOREACH(x, y)\n"); 676 677 // Not as function-like macros. 678 verifyFormat("#define foreach (x, y)\n" 679 "#define Q_FOREACH (x, y)\n" 680 "#define BOOST_FOREACH (x, y)\n" 681 "#define UNKNOWN_FOREACH (x, y)\n"); 682 } 683 684 TEST_F(FormatTest, FormatsWhileLoop) { 685 verifyFormat("while (true) {\n}"); 686 verifyFormat("while (true)\n" 687 " f();"); 688 verifyFormat("while () {\n}"); 689 verifyFormat("while () {\n" 690 " f();\n" 691 "}"); 692 } 693 694 TEST_F(FormatTest, FormatsDoWhile) { 695 verifyFormat("do {\n" 696 " do_something();\n" 697 "} while (something());"); 698 verifyFormat("do\n" 699 " do_something();\n" 700 "while (something());"); 701 } 702 703 TEST_F(FormatTest, FormatsSwitchStatement) { 704 verifyFormat("switch (x) {\n" 705 "case 1:\n" 706 " f();\n" 707 " break;\n" 708 "case kFoo:\n" 709 "case ns::kBar:\n" 710 "case kBaz:\n" 711 " break;\n" 712 "default:\n" 713 " g();\n" 714 " break;\n" 715 "}"); 716 verifyFormat("switch (x) {\n" 717 "case 1: {\n" 718 " f();\n" 719 " break;\n" 720 "}\n" 721 "case 2: {\n" 722 " break;\n" 723 "}\n" 724 "}"); 725 verifyFormat("switch (x) {\n" 726 "case 1: {\n" 727 " f();\n" 728 " {\n" 729 " g();\n" 730 " h();\n" 731 " }\n" 732 " break;\n" 733 "}\n" 734 "}"); 735 verifyFormat("switch (x) {\n" 736 "case 1: {\n" 737 " f();\n" 738 " if (foo) {\n" 739 " g();\n" 740 " h();\n" 741 " }\n" 742 " break;\n" 743 "}\n" 744 "}"); 745 verifyFormat("switch (x) {\n" 746 "case 1: {\n" 747 " f();\n" 748 " g();\n" 749 "} break;\n" 750 "}"); 751 verifyFormat("switch (test)\n" 752 " ;"); 753 verifyFormat("switch (x) {\n" 754 "default: {\n" 755 " // Do nothing.\n" 756 "}\n" 757 "}"); 758 verifyFormat("switch (x) {\n" 759 "// comment\n" 760 "// if 1, do f()\n" 761 "case 1:\n" 762 " f();\n" 763 "}"); 764 verifyFormat("switch (x) {\n" 765 "case 1:\n" 766 " // Do amazing stuff\n" 767 " {\n" 768 " f();\n" 769 " g();\n" 770 " }\n" 771 " break;\n" 772 "}"); 773 verifyFormat("#define A \\\n" 774 " switch (x) { \\\n" 775 " case a: \\\n" 776 " foo = b; \\\n" 777 " }", 778 getLLVMStyleWithColumns(20)); 779 verifyFormat("#define OPERATION_CASE(name) \\\n" 780 " case OP_name: \\\n" 781 " return operations::Operation##name\n", 782 getLLVMStyleWithColumns(40)); 783 verifyFormat("switch (x) {\n" 784 "case 1:;\n" 785 "default:;\n" 786 " int i;\n" 787 "}"); 788 789 verifyGoogleFormat("switch (x) {\n" 790 " case 1:\n" 791 " f();\n" 792 " break;\n" 793 " case kFoo:\n" 794 " case ns::kBar:\n" 795 " case kBaz:\n" 796 " break;\n" 797 " default:\n" 798 " g();\n" 799 " break;\n" 800 "}"); 801 verifyGoogleFormat("switch (x) {\n" 802 " case 1: {\n" 803 " f();\n" 804 " break;\n" 805 " }\n" 806 "}"); 807 verifyGoogleFormat("switch (test)\n" 808 " ;"); 809 810 verifyGoogleFormat("#define OPERATION_CASE(name) \\\n" 811 " case OP_name: \\\n" 812 " return operations::Operation##name\n"); 813 verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n" 814 " // Get the correction operation class.\n" 815 " switch (OpCode) {\n" 816 " CASE(Add);\n" 817 " CASE(Subtract);\n" 818 " default:\n" 819 " return operations::Unknown;\n" 820 " }\n" 821 "#undef OPERATION_CASE\n" 822 "}"); 823 verifyFormat("DEBUG({\n" 824 " switch (x) {\n" 825 " case A:\n" 826 " f();\n" 827 " break;\n" 828 " // On B:\n" 829 " case B:\n" 830 " g();\n" 831 " break;\n" 832 " }\n" 833 "});"); 834 verifyFormat("switch (a) {\n" 835 "case (b):\n" 836 " return;\n" 837 "}"); 838 839 verifyFormat("switch (a) {\n" 840 "case some_namespace::\n" 841 " some_constant:\n" 842 " return;\n" 843 "}", 844 getLLVMStyleWithColumns(34)); 845 } 846 847 TEST_F(FormatTest, CaseRanges) { 848 verifyFormat("switch (x) {\n" 849 "case 'A' ... 'Z':\n" 850 "case 1 ... 5:\n" 851 "case a ... b:\n" 852 " break;\n" 853 "}"); 854 } 855 856 TEST_F(FormatTest, ShortCaseLabels) { 857 FormatStyle Style = getLLVMStyle(); 858 Style.AllowShortCaseLabelsOnASingleLine = true; 859 verifyFormat("switch (a) {\n" 860 "case 1: x = 1; break;\n" 861 "case 2: return;\n" 862 "case 3:\n" 863 "case 4:\n" 864 "case 5: return;\n" 865 "case 6: // comment\n" 866 " return;\n" 867 "case 7:\n" 868 " // comment\n" 869 " return;\n" 870 "case 8:\n" 871 " x = 8; // comment\n" 872 " break;\n" 873 "default: y = 1; break;\n" 874 "}", 875 Style); 876 verifyFormat("switch (a) {\n" 877 "#if FOO\n" 878 "case 0: return 0;\n" 879 "#endif\n" 880 "}", 881 Style); 882 verifyFormat("switch (a) {\n" 883 "case 1: {\n" 884 "}\n" 885 "case 2: {\n" 886 " return;\n" 887 "}\n" 888 "case 3: {\n" 889 " x = 1;\n" 890 " return;\n" 891 "}\n" 892 "case 4:\n" 893 " if (x)\n" 894 " return;\n" 895 "}", 896 Style); 897 Style.ColumnLimit = 21; 898 verifyFormat("switch (a) {\n" 899 "case 1: x = 1; break;\n" 900 "case 2: return;\n" 901 "case 3:\n" 902 "case 4:\n" 903 "case 5: return;\n" 904 "default:\n" 905 " y = 1;\n" 906 " break;\n" 907 "}", 908 Style); 909 } 910 911 TEST_F(FormatTest, FormatsLabels) { 912 verifyFormat("void f() {\n" 913 " some_code();\n" 914 "test_label:\n" 915 " some_other_code();\n" 916 " {\n" 917 " some_more_code();\n" 918 " another_label:\n" 919 " some_more_code();\n" 920 " }\n" 921 "}"); 922 verifyFormat("{\n" 923 " some_code();\n" 924 "test_label:\n" 925 " some_other_code();\n" 926 "}"); 927 verifyFormat("{\n" 928 " some_code();\n" 929 "test_label:;\n" 930 " int i = 0;\n" 931 "}"); 932 } 933 934 //===----------------------------------------------------------------------===// 935 // Tests for classes, namespaces, etc. 936 //===----------------------------------------------------------------------===// 937 938 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { 939 verifyFormat("class A {};"); 940 } 941 942 TEST_F(FormatTest, UnderstandsAccessSpecifiers) { 943 verifyFormat("class A {\n" 944 "public:\n" 945 "public: // comment\n" 946 "protected:\n" 947 "private:\n" 948 " void f() {}\n" 949 "};"); 950 verifyGoogleFormat("class A {\n" 951 " public:\n" 952 " protected:\n" 953 " private:\n" 954 " void f() {}\n" 955 "};"); 956 verifyFormat("class A {\n" 957 "public slots:\n" 958 " void f1() {}\n" 959 "public Q_SLOTS:\n" 960 " void f2() {}\n" 961 "protected slots:\n" 962 " void f3() {}\n" 963 "protected Q_SLOTS:\n" 964 " void f4() {}\n" 965 "private slots:\n" 966 " void f5() {}\n" 967 "private Q_SLOTS:\n" 968 " void f6() {}\n" 969 "signals:\n" 970 " void g1();\n" 971 "Q_SIGNALS:\n" 972 " void g2();\n" 973 "};"); 974 975 // Don't interpret 'signals' the wrong way. 976 verifyFormat("signals.set();"); 977 verifyFormat("for (Signals signals : f()) {\n}"); 978 verifyFormat("{\n" 979 " signals.set(); // This needs indentation.\n" 980 "}"); 981 verifyFormat("void f() {\n" 982 "label:\n" 983 " signals.baz();\n" 984 "}"); 985 } 986 987 TEST_F(FormatTest, SeparatesLogicalBlocks) { 988 EXPECT_EQ("class A {\n" 989 "public:\n" 990 " void f();\n" 991 "\n" 992 "private:\n" 993 " void g() {}\n" 994 " // test\n" 995 "protected:\n" 996 " int h;\n" 997 "};", 998 format("class A {\n" 999 "public:\n" 1000 "void f();\n" 1001 "private:\n" 1002 "void g() {}\n" 1003 "// test\n" 1004 "protected:\n" 1005 "int h;\n" 1006 "};")); 1007 EXPECT_EQ("class A {\n" 1008 "protected:\n" 1009 "public:\n" 1010 " void f();\n" 1011 "};", 1012 format("class A {\n" 1013 "protected:\n" 1014 "\n" 1015 "public:\n" 1016 "\n" 1017 " void f();\n" 1018 "};")); 1019 1020 // Even ensure proper spacing inside macros. 1021 EXPECT_EQ("#define B \\\n" 1022 " class A { \\\n" 1023 " protected: \\\n" 1024 " public: \\\n" 1025 " void f(); \\\n" 1026 " };", 1027 format("#define B \\\n" 1028 " class A { \\\n" 1029 " protected: \\\n" 1030 " \\\n" 1031 " public: \\\n" 1032 " \\\n" 1033 " void f(); \\\n" 1034 " };", 1035 getGoogleStyle())); 1036 // But don't remove empty lines after macros ending in access specifiers. 1037 EXPECT_EQ("#define A private:\n" 1038 "\n" 1039 "int i;", 1040 format("#define A private:\n" 1041 "\n" 1042 "int i;")); 1043 } 1044 1045 TEST_F(FormatTest, FormatsClasses) { 1046 verifyFormat("class A : public B {};"); 1047 verifyFormat("class A : public ::B {};"); 1048 1049 verifyFormat( 1050 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 1051 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 1052 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 1053 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 1054 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 1055 verifyFormat( 1056 "class A : public B, public C, public D, public E, public F {};"); 1057 verifyFormat("class AAAAAAAAAAAA : public B,\n" 1058 " public C,\n" 1059 " public D,\n" 1060 " public E,\n" 1061 " public F,\n" 1062 " public G {};"); 1063 1064 verifyFormat("class\n" 1065 " ReallyReallyLongClassName {\n" 1066 " int i;\n" 1067 "};", 1068 getLLVMStyleWithColumns(32)); 1069 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 1070 " aaaaaaaaaaaaaaaa> {};"); 1071 verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n" 1072 " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n" 1073 " aaaaaaaaaaaaaaaaaaaaaa> {};"); 1074 verifyFormat("template <class R, class C>\n" 1075 "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n" 1076 " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};"); 1077 verifyFormat("class ::A::B {};"); 1078 } 1079 1080 TEST_F(FormatTest, BreakBeforeInheritanceComma) { 1081 FormatStyle StyleWithInheritanceBreak = getLLVMStyle(); 1082 StyleWithInheritanceBreak.BreakBeforeInheritanceComma = true; 1083 1084 verifyFormat("class MyClass : public X {};", StyleWithInheritanceBreak); 1085 verifyFormat("class MyClass\n" 1086 " : public X\n" 1087 " , public Y {};", 1088 StyleWithInheritanceBreak); 1089 } 1090 1091 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) { 1092 verifyFormat("class A {\n} a, b;"); 1093 verifyFormat("struct A {\n} a, b;"); 1094 verifyFormat("union A {\n} a;"); 1095 } 1096 1097 TEST_F(FormatTest, FormatsEnum) { 1098 verifyFormat("enum {\n" 1099 " Zero,\n" 1100 " One = 1,\n" 1101 " Two = One + 1,\n" 1102 " Three = (One + Two),\n" 1103 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1104 " Five = (One, Two, Three, Four, 5)\n" 1105 "};"); 1106 verifyGoogleFormat("enum {\n" 1107 " Zero,\n" 1108 " One = 1,\n" 1109 " Two = One + 1,\n" 1110 " Three = (One + Two),\n" 1111 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1112 " Five = (One, Two, Three, Four, 5)\n" 1113 "};"); 1114 verifyFormat("enum Enum {};"); 1115 verifyFormat("enum {};"); 1116 verifyFormat("enum X E {} d;"); 1117 verifyFormat("enum __attribute__((...)) E {} d;"); 1118 verifyFormat("enum __declspec__((...)) E {} d;"); 1119 verifyFormat("enum {\n" 1120 " Bar = Foo<int, int>::value\n" 1121 "};", 1122 getLLVMStyleWithColumns(30)); 1123 1124 verifyFormat("enum ShortEnum { A, B, C };"); 1125 verifyGoogleFormat("enum ShortEnum { A, B, C };"); 1126 1127 EXPECT_EQ("enum KeepEmptyLines {\n" 1128 " ONE,\n" 1129 "\n" 1130 " TWO,\n" 1131 "\n" 1132 " THREE\n" 1133 "}", 1134 format("enum KeepEmptyLines {\n" 1135 " ONE,\n" 1136 "\n" 1137 " TWO,\n" 1138 "\n" 1139 "\n" 1140 " THREE\n" 1141 "}")); 1142 verifyFormat("enum E { // comment\n" 1143 " ONE,\n" 1144 " TWO\n" 1145 "};\n" 1146 "int i;"); 1147 // Not enums. 1148 verifyFormat("enum X f() {\n" 1149 " a();\n" 1150 " return 42;\n" 1151 "}"); 1152 verifyFormat("enum X Type::f() {\n" 1153 " a();\n" 1154 " return 42;\n" 1155 "}"); 1156 verifyFormat("enum ::X f() {\n" 1157 " a();\n" 1158 " return 42;\n" 1159 "}"); 1160 verifyFormat("enum ns::X f() {\n" 1161 " a();\n" 1162 " return 42;\n" 1163 "}"); 1164 } 1165 1166 TEST_F(FormatTest, FormatsEnumsWithErrors) { 1167 verifyFormat("enum Type {\n" 1168 " One = 0; // These semicolons should be commas.\n" 1169 " Two = 1;\n" 1170 "};"); 1171 verifyFormat("namespace n {\n" 1172 "enum Type {\n" 1173 " One,\n" 1174 " Two, // missing };\n" 1175 " int i;\n" 1176 "}\n" 1177 "void g() {}"); 1178 } 1179 1180 TEST_F(FormatTest, FormatsEnumStruct) { 1181 verifyFormat("enum struct {\n" 1182 " Zero,\n" 1183 " One = 1,\n" 1184 " Two = One + 1,\n" 1185 " Three = (One + Two),\n" 1186 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1187 " Five = (One, Two, Three, Four, 5)\n" 1188 "};"); 1189 verifyFormat("enum struct Enum {};"); 1190 verifyFormat("enum struct {};"); 1191 verifyFormat("enum struct X E {} d;"); 1192 verifyFormat("enum struct __attribute__((...)) E {} d;"); 1193 verifyFormat("enum struct __declspec__((...)) E {} d;"); 1194 verifyFormat("enum struct X f() {\n a();\n return 42;\n}"); 1195 } 1196 1197 TEST_F(FormatTest, FormatsEnumClass) { 1198 verifyFormat("enum class {\n" 1199 " Zero,\n" 1200 " One = 1,\n" 1201 " Two = One + 1,\n" 1202 " Three = (One + Two),\n" 1203 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1204 " Five = (One, Two, Three, Four, 5)\n" 1205 "};"); 1206 verifyFormat("enum class Enum {};"); 1207 verifyFormat("enum class {};"); 1208 verifyFormat("enum class X E {} d;"); 1209 verifyFormat("enum class __attribute__((...)) E {} d;"); 1210 verifyFormat("enum class __declspec__((...)) E {} d;"); 1211 verifyFormat("enum class X f() {\n a();\n return 42;\n}"); 1212 } 1213 1214 TEST_F(FormatTest, FormatsEnumTypes) { 1215 verifyFormat("enum X : int {\n" 1216 " A, // Force multiple lines.\n" 1217 " B\n" 1218 "};"); 1219 verifyFormat("enum X : int { A, B };"); 1220 verifyFormat("enum X : std::uint32_t { A, B };"); 1221 } 1222 1223 TEST_F(FormatTest, FormatsNSEnums) { 1224 verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }"); 1225 verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n" 1226 " // Information about someDecentlyLongValue.\n" 1227 " someDecentlyLongValue,\n" 1228 " // Information about anotherDecentlyLongValue.\n" 1229 " anotherDecentlyLongValue,\n" 1230 " // Information about aThirdDecentlyLongValue.\n" 1231 " aThirdDecentlyLongValue\n" 1232 "};"); 1233 verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n" 1234 " a = 1,\n" 1235 " b = 2,\n" 1236 " c = 3,\n" 1237 "};"); 1238 verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n" 1239 " a = 1,\n" 1240 " b = 2,\n" 1241 " c = 3,\n" 1242 "};"); 1243 verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n" 1244 " a = 1,\n" 1245 " b = 2,\n" 1246 " c = 3,\n" 1247 "};"); 1248 } 1249 1250 TEST_F(FormatTest, FormatsBitfields) { 1251 verifyFormat("struct Bitfields {\n" 1252 " unsigned sClass : 8;\n" 1253 " unsigned ValueKind : 2;\n" 1254 "};"); 1255 verifyFormat("struct A {\n" 1256 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n" 1257 " bbbbbbbbbbbbbbbbbbbbbbbbb;\n" 1258 "};"); 1259 verifyFormat("struct MyStruct {\n" 1260 " uchar data;\n" 1261 " uchar : 8;\n" 1262 " uchar : 8;\n" 1263 " uchar other;\n" 1264 "};"); 1265 } 1266 1267 TEST_F(FormatTest, FormatsNamespaces) { 1268 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 1269 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 1270 1271 verifyFormat("namespace some_namespace {\n" 1272 "class A {};\n" 1273 "void f() { f(); }\n" 1274 "}", 1275 LLVMWithNoNamespaceFix); 1276 verifyFormat("namespace {\n" 1277 "class A {};\n" 1278 "void f() { f(); }\n" 1279 "}", 1280 LLVMWithNoNamespaceFix); 1281 verifyFormat("inline namespace X {\n" 1282 "class A {};\n" 1283 "void f() { f(); }\n" 1284 "}", 1285 LLVMWithNoNamespaceFix); 1286 verifyFormat("using namespace some_namespace;\n" 1287 "class A {};\n" 1288 "void f() { f(); }", 1289 LLVMWithNoNamespaceFix); 1290 1291 // This code is more common than we thought; if we 1292 // layout this correctly the semicolon will go into 1293 // its own line, which is undesirable. 1294 verifyFormat("namespace {};", 1295 LLVMWithNoNamespaceFix); 1296 verifyFormat("namespace {\n" 1297 "class A {};\n" 1298 "};", 1299 LLVMWithNoNamespaceFix); 1300 1301 verifyFormat("namespace {\n" 1302 "int SomeVariable = 0; // comment\n" 1303 "} // namespace", 1304 LLVMWithNoNamespaceFix); 1305 EXPECT_EQ("#ifndef HEADER_GUARD\n" 1306 "#define HEADER_GUARD\n" 1307 "namespace my_namespace {\n" 1308 "int i;\n" 1309 "} // my_namespace\n" 1310 "#endif // HEADER_GUARD", 1311 format("#ifndef HEADER_GUARD\n" 1312 " #define HEADER_GUARD\n" 1313 " namespace my_namespace {\n" 1314 "int i;\n" 1315 "} // my_namespace\n" 1316 "#endif // HEADER_GUARD", 1317 LLVMWithNoNamespaceFix)); 1318 1319 EXPECT_EQ("namespace A::B {\n" 1320 "class C {};\n" 1321 "}", 1322 format("namespace A::B {\n" 1323 "class C {};\n" 1324 "}", 1325 LLVMWithNoNamespaceFix)); 1326 1327 FormatStyle Style = getLLVMStyle(); 1328 Style.NamespaceIndentation = FormatStyle::NI_All; 1329 EXPECT_EQ("namespace out {\n" 1330 " int i;\n" 1331 " namespace in {\n" 1332 " int i;\n" 1333 " } // namespace in\n" 1334 "} // namespace out", 1335 format("namespace out {\n" 1336 "int i;\n" 1337 "namespace in {\n" 1338 "int i;\n" 1339 "} // namespace in\n" 1340 "} // namespace out", 1341 Style)); 1342 1343 Style.NamespaceIndentation = FormatStyle::NI_Inner; 1344 EXPECT_EQ("namespace out {\n" 1345 "int i;\n" 1346 "namespace in {\n" 1347 " int i;\n" 1348 "} // namespace in\n" 1349 "} // namespace out", 1350 format("namespace out {\n" 1351 "int i;\n" 1352 "namespace in {\n" 1353 "int i;\n" 1354 "} // namespace in\n" 1355 "} // namespace out", 1356 Style)); 1357 } 1358 1359 TEST_F(FormatTest, FormatsCompactNamespaces) { 1360 FormatStyle Style = getLLVMStyle(); 1361 Style.CompactNamespaces = true; 1362 1363 verifyFormat("namespace A { namespace B {\n" 1364 "}} // namespace A::B", 1365 Style); 1366 1367 EXPECT_EQ("namespace out { namespace in {\n" 1368 "}} // namespace out::in", 1369 format("namespace out {\n" 1370 "namespace in {\n" 1371 "} // namespace in\n" 1372 "} // namespace out", 1373 Style)); 1374 1375 // Only namespaces which have both consecutive opening and end get compacted 1376 EXPECT_EQ("namespace out {\n" 1377 "namespace in1 {\n" 1378 "} // namespace in1\n" 1379 "namespace in2 {\n" 1380 "} // namespace in2\n" 1381 "} // namespace out", 1382 format("namespace out {\n" 1383 "namespace in1 {\n" 1384 "} // namespace in1\n" 1385 "namespace in2 {\n" 1386 "} // namespace in2\n" 1387 "} // namespace out", 1388 Style)); 1389 1390 EXPECT_EQ("namespace out {\n" 1391 "int i;\n" 1392 "namespace in {\n" 1393 "int j;\n" 1394 "} // namespace in\n" 1395 "int k;\n" 1396 "} // namespace out", 1397 format("namespace out { int i;\n" 1398 "namespace in { int j; } // namespace in\n" 1399 "int k; } // namespace out", 1400 Style)); 1401 1402 EXPECT_EQ("namespace A { namespace B { namespace C {\n" 1403 "}}} // namespace A::B::C\n", 1404 format("namespace A { namespace B {\n" 1405 "namespace C {\n" 1406 "}} // namespace B::C\n" 1407 "} // namespace A\n", 1408 Style)); 1409 1410 Style.ColumnLimit = 40; 1411 EXPECT_EQ("namespace aaaaaaaaaa {\n" 1412 "namespace bbbbbbbbbb {\n" 1413 "}} // namespace aaaaaaaaaa::bbbbbbbbbb", 1414 format("namespace aaaaaaaaaa {\n" 1415 "namespace bbbbbbbbbb {\n" 1416 "} // namespace bbbbbbbbbb\n" 1417 "} // namespace aaaaaaaaaa", 1418 Style)); 1419 1420 EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n" 1421 "namespace cccccc {\n" 1422 "}}} // namespace aaaaaa::bbbbbb::cccccc", 1423 format("namespace aaaaaa {\n" 1424 "namespace bbbbbb {\n" 1425 "namespace cccccc {\n" 1426 "} // namespace cccccc\n" 1427 "} // namespace bbbbbb\n" 1428 "} // namespace aaaaaa", 1429 Style)); 1430 Style.ColumnLimit = 80; 1431 1432 // Extra semicolon after 'inner' closing brace prevents merging 1433 EXPECT_EQ("namespace out { namespace in {\n" 1434 "}; } // namespace out::in", 1435 format("namespace out {\n" 1436 "namespace in {\n" 1437 "}; // namespace in\n" 1438 "} // namespace out", 1439 Style)); 1440 1441 // Extra semicolon after 'outer' closing brace is conserved 1442 EXPECT_EQ("namespace out { namespace in {\n" 1443 "}}; // namespace out::in", 1444 format("namespace out {\n" 1445 "namespace in {\n" 1446 "} // namespace in\n" 1447 "}; // namespace out", 1448 Style)); 1449 1450 Style.NamespaceIndentation = FormatStyle::NI_All; 1451 EXPECT_EQ("namespace out { namespace in {\n" 1452 " int i;\n" 1453 "}} // namespace out::in", 1454 format("namespace out {\n" 1455 "namespace in {\n" 1456 "int i;\n" 1457 "} // namespace in\n" 1458 "} // namespace out", 1459 Style)); 1460 EXPECT_EQ("namespace out { namespace mid {\n" 1461 " namespace in {\n" 1462 " int j;\n" 1463 " } // namespace in\n" 1464 " int k;\n" 1465 "}} // namespace out::mid", 1466 format("namespace out { namespace mid {\n" 1467 "namespace in { int j; } // namespace in\n" 1468 "int k; }} // namespace out::mid", 1469 Style)); 1470 1471 Style.NamespaceIndentation = FormatStyle::NI_Inner; 1472 EXPECT_EQ("namespace out { namespace in {\n" 1473 " int i;\n" 1474 "}} // namespace out::in", 1475 format("namespace out {\n" 1476 "namespace in {\n" 1477 "int i;\n" 1478 "} // namespace in\n" 1479 "} // namespace out", 1480 Style)); 1481 EXPECT_EQ("namespace out { namespace mid { namespace in {\n" 1482 " int i;\n" 1483 "}}} // namespace out::mid::in", 1484 format("namespace out {\n" 1485 "namespace mid {\n" 1486 "namespace in {\n" 1487 "int i;\n" 1488 "} // namespace in\n" 1489 "} // namespace mid\n" 1490 "} // namespace out", 1491 Style)); 1492 } 1493 1494 TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); } 1495 1496 TEST_F(FormatTest, FormatsInlineASM) { 1497 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"); 1498 verifyFormat("asm(\"nop\" ::: \"memory\");"); 1499 verifyFormat( 1500 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" 1501 " \"cpuid\\n\\t\"\n" 1502 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n" 1503 " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n" 1504 " : \"a\"(value));"); 1505 EXPECT_EQ( 1506 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 1507 " __asm {\n" 1508 " mov edx,[that] // vtable in edx\n" 1509 " mov eax,methodIndex\n" 1510 " call [edx][eax*4] // stdcall\n" 1511 " }\n" 1512 "}", 1513 format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 1514 " __asm {\n" 1515 " mov edx,[that] // vtable in edx\n" 1516 " mov eax,methodIndex\n" 1517 " call [edx][eax*4] // stdcall\n" 1518 " }\n" 1519 "}")); 1520 EXPECT_EQ("_asm {\n" 1521 " xor eax, eax;\n" 1522 " cpuid;\n" 1523 "}", 1524 format("_asm {\n" 1525 " xor eax, eax;\n" 1526 " cpuid;\n" 1527 "}")); 1528 verifyFormat("void function() {\n" 1529 " // comment\n" 1530 " asm(\"\");\n" 1531 "}"); 1532 EXPECT_EQ("__asm {\n" 1533 "}\n" 1534 "int i;", 1535 format("__asm {\n" 1536 "}\n" 1537 "int i;")); 1538 } 1539 1540 TEST_F(FormatTest, FormatTryCatch) { 1541 verifyFormat("try {\n" 1542 " throw a * b;\n" 1543 "} catch (int a) {\n" 1544 " // Do nothing.\n" 1545 "} catch (...) {\n" 1546 " exit(42);\n" 1547 "}"); 1548 1549 // Function-level try statements. 1550 verifyFormat("int f() try { return 4; } catch (...) {\n" 1551 " return 5;\n" 1552 "}"); 1553 verifyFormat("class A {\n" 1554 " int a;\n" 1555 " A() try : a(0) {\n" 1556 " } catch (...) {\n" 1557 " throw;\n" 1558 " }\n" 1559 "};\n"); 1560 1561 // Incomplete try-catch blocks. 1562 verifyIncompleteFormat("try {} catch ("); 1563 } 1564 1565 TEST_F(FormatTest, FormatSEHTryCatch) { 1566 verifyFormat("__try {\n" 1567 " int a = b * c;\n" 1568 "} __except (EXCEPTION_EXECUTE_HANDLER) {\n" 1569 " // Do nothing.\n" 1570 "}"); 1571 1572 verifyFormat("__try {\n" 1573 " int a = b * c;\n" 1574 "} __finally {\n" 1575 " // Do nothing.\n" 1576 "}"); 1577 1578 verifyFormat("DEBUG({\n" 1579 " __try {\n" 1580 " } __finally {\n" 1581 " }\n" 1582 "});\n"); 1583 } 1584 1585 TEST_F(FormatTest, IncompleteTryCatchBlocks) { 1586 verifyFormat("try {\n" 1587 " f();\n" 1588 "} catch {\n" 1589 " g();\n" 1590 "}"); 1591 verifyFormat("try {\n" 1592 " f();\n" 1593 "} catch (A a) MACRO(x) {\n" 1594 " g();\n" 1595 "} catch (B b) MACRO(x) {\n" 1596 " g();\n" 1597 "}"); 1598 } 1599 1600 TEST_F(FormatTest, FormatTryCatchBraceStyles) { 1601 FormatStyle Style = getLLVMStyle(); 1602 for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla, 1603 FormatStyle::BS_WebKit}) { 1604 Style.BreakBeforeBraces = BraceStyle; 1605 verifyFormat("try {\n" 1606 " // something\n" 1607 "} catch (...) {\n" 1608 " // something\n" 1609 "}", 1610 Style); 1611 } 1612 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 1613 verifyFormat("try {\n" 1614 " // something\n" 1615 "}\n" 1616 "catch (...) {\n" 1617 " // something\n" 1618 "}", 1619 Style); 1620 verifyFormat("__try {\n" 1621 " // something\n" 1622 "}\n" 1623 "__finally {\n" 1624 " // something\n" 1625 "}", 1626 Style); 1627 verifyFormat("@try {\n" 1628 " // something\n" 1629 "}\n" 1630 "@finally {\n" 1631 " // something\n" 1632 "}", 1633 Style); 1634 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 1635 verifyFormat("try\n" 1636 "{\n" 1637 " // something\n" 1638 "}\n" 1639 "catch (...)\n" 1640 "{\n" 1641 " // something\n" 1642 "}", 1643 Style); 1644 Style.BreakBeforeBraces = FormatStyle::BS_GNU; 1645 verifyFormat("try\n" 1646 " {\n" 1647 " // something\n" 1648 " }\n" 1649 "catch (...)\n" 1650 " {\n" 1651 " // something\n" 1652 " }", 1653 Style); 1654 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 1655 Style.BraceWrapping.BeforeCatch = true; 1656 verifyFormat("try {\n" 1657 " // something\n" 1658 "}\n" 1659 "catch (...) {\n" 1660 " // something\n" 1661 "}", 1662 Style); 1663 } 1664 1665 TEST_F(FormatTest, StaticInitializers) { 1666 verifyFormat("static SomeClass SC = {1, 'a'};"); 1667 1668 verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n" 1669 " 100000000, " 1670 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};"); 1671 1672 // Here, everything other than the "}" would fit on a line. 1673 verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n" 1674 " 10000000000000000000000000};"); 1675 EXPECT_EQ("S s = {a,\n" 1676 "\n" 1677 " b};", 1678 format("S s = {\n" 1679 " a,\n" 1680 "\n" 1681 " b\n" 1682 "};")); 1683 1684 // FIXME: This would fit into the column limit if we'd fit "{ {" on the first 1685 // line. However, the formatting looks a bit off and this probably doesn't 1686 // happen often in practice. 1687 verifyFormat("static int Variable[1] = {\n" 1688 " {1000000000000000000000000000000000000}};", 1689 getLLVMStyleWithColumns(40)); 1690 } 1691 1692 TEST_F(FormatTest, DesignatedInitializers) { 1693 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 1694 verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n" 1695 " .bbbbbbbbbb = 2,\n" 1696 " .cccccccccc = 3,\n" 1697 " .dddddddddd = 4,\n" 1698 " .eeeeeeeeee = 5};"); 1699 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 1700 " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n" 1701 " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n" 1702 " .ccccccccccccccccccccccccccc = 3,\n" 1703 " .ddddddddddddddddddddddddddd = 4,\n" 1704 " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};"); 1705 1706 verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};"); 1707 1708 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 1709 verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n" 1710 " [2] = bbbbbbbbbb,\n" 1711 " [3] = cccccccccc,\n" 1712 " [4] = dddddddddd,\n" 1713 " [5] = eeeeeeeeee};"); 1714 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 1715 " [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1716 " [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 1717 " [3] = cccccccccccccccccccccccccccccccccccccc,\n" 1718 " [4] = dddddddddddddddddddddddddddddddddddddd,\n" 1719 " [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};"); 1720 } 1721 1722 TEST_F(FormatTest, NestedStaticInitializers) { 1723 verifyFormat("static A x = {{{}}};\n"); 1724 verifyFormat("static A x = {{{init1, init2, init3, init4},\n" 1725 " {init1, init2, init3, init4}}};", 1726 getLLVMStyleWithColumns(50)); 1727 1728 verifyFormat("somes Status::global_reps[3] = {\n" 1729 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 1730 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 1731 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};", 1732 getLLVMStyleWithColumns(60)); 1733 verifyGoogleFormat("SomeType Status::global_reps[3] = {\n" 1734 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 1735 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 1736 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};"); 1737 verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n" 1738 " {rect.fRight - rect.fLeft, rect.fBottom - " 1739 "rect.fTop}};"); 1740 1741 verifyFormat( 1742 "SomeArrayOfSomeType a = {\n" 1743 " {{1, 2, 3},\n" 1744 " {1, 2, 3},\n" 1745 " {111111111111111111111111111111, 222222222222222222222222222222,\n" 1746 " 333333333333333333333333333333},\n" 1747 " {1, 2, 3},\n" 1748 " {1, 2, 3}}};"); 1749 verifyFormat( 1750 "SomeArrayOfSomeType a = {\n" 1751 " {{1, 2, 3}},\n" 1752 " {{1, 2, 3}},\n" 1753 " {{111111111111111111111111111111, 222222222222222222222222222222,\n" 1754 " 333333333333333333333333333333}},\n" 1755 " {{1, 2, 3}},\n" 1756 " {{1, 2, 3}}};"); 1757 1758 verifyFormat("struct {\n" 1759 " unsigned bit;\n" 1760 " const char *const name;\n" 1761 "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n" 1762 " {kOsWin, \"Windows\"},\n" 1763 " {kOsLinux, \"Linux\"},\n" 1764 " {kOsCrOS, \"Chrome OS\"}};"); 1765 verifyFormat("struct {\n" 1766 " unsigned bit;\n" 1767 " const char *const name;\n" 1768 "} kBitsToOs[] = {\n" 1769 " {kOsMac, \"Mac\"},\n" 1770 " {kOsWin, \"Windows\"},\n" 1771 " {kOsLinux, \"Linux\"},\n" 1772 " {kOsCrOS, \"Chrome OS\"},\n" 1773 "};"); 1774 } 1775 1776 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { 1777 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 1778 " \\\n" 1779 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); 1780 } 1781 1782 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { 1783 verifyFormat("virtual void write(ELFWriter *writerrr,\n" 1784 " OwningPtr<FileOutputBuffer> &buffer) = 0;"); 1785 1786 // Do break defaulted and deleted functions. 1787 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 1788 " default;", 1789 getLLVMStyleWithColumns(40)); 1790 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 1791 " delete;", 1792 getLLVMStyleWithColumns(40)); 1793 } 1794 1795 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) { 1796 verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3", 1797 getLLVMStyleWithColumns(40)); 1798 verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 1799 getLLVMStyleWithColumns(40)); 1800 EXPECT_EQ("#define Q \\\n" 1801 " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n" 1802 " \"aaaaaaaa.cpp\"", 1803 format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 1804 getLLVMStyleWithColumns(40))); 1805 } 1806 1807 TEST_F(FormatTest, UnderstandsLinePPDirective) { 1808 EXPECT_EQ("# 123 \"A string literal\"", 1809 format(" # 123 \"A string literal\"")); 1810 } 1811 1812 TEST_F(FormatTest, LayoutUnknownPPDirective) { 1813 EXPECT_EQ("#;", format("#;")); 1814 verifyFormat("#\n;\n;\n;"); 1815 } 1816 1817 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { 1818 EXPECT_EQ("#line 42 \"test\"\n", 1819 format("# \\\n line \\\n 42 \\\n \"test\"\n")); 1820 EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n", 1821 getLLVMStyleWithColumns(12))); 1822 } 1823 1824 TEST_F(FormatTest, EndOfFileEndsPPDirective) { 1825 EXPECT_EQ("#line 42 \"test\"", 1826 format("# \\\n line \\\n 42 \\\n \"test\"")); 1827 EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B")); 1828 } 1829 1830 TEST_F(FormatTest, DoesntRemoveUnknownTokens) { 1831 verifyFormat("#define A \\x20"); 1832 verifyFormat("#define A \\ x20"); 1833 EXPECT_EQ("#define A \\ x20", format("#define A \\ x20")); 1834 verifyFormat("#define A ''"); 1835 verifyFormat("#define A ''qqq"); 1836 verifyFormat("#define A `qqq"); 1837 verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");"); 1838 EXPECT_EQ("const char *c = STRINGIFY(\n" 1839 "\\na : b);", 1840 format("const char * c = STRINGIFY(\n" 1841 "\\na : b);")); 1842 1843 verifyFormat("a\r\\"); 1844 verifyFormat("a\v\\"); 1845 verifyFormat("a\f\\"); 1846 } 1847 1848 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { 1849 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13)); 1850 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12)); 1851 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); 1852 // FIXME: We never break before the macro name. 1853 verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12)); 1854 1855 verifyFormat("#define A A\n#define A A"); 1856 verifyFormat("#define A(X) A\n#define A A"); 1857 1858 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23)); 1859 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22)); 1860 } 1861 1862 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { 1863 EXPECT_EQ("// somecomment\n" 1864 "#include \"a.h\"\n" 1865 "#define A( \\\n" 1866 " A, B)\n" 1867 "#include \"b.h\"\n" 1868 "// somecomment\n", 1869 format(" // somecomment\n" 1870 " #include \"a.h\"\n" 1871 "#define A(A,\\\n" 1872 " B)\n" 1873 " #include \"b.h\"\n" 1874 " // somecomment\n", 1875 getLLVMStyleWithColumns(13))); 1876 } 1877 1878 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); } 1879 1880 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { 1881 EXPECT_EQ("#define A \\\n" 1882 " c; \\\n" 1883 " e;\n" 1884 "f;", 1885 format("#define A c; e;\n" 1886 "f;", 1887 getLLVMStyleWithColumns(14))); 1888 } 1889 1890 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); } 1891 1892 TEST_F(FormatTest, MacroDefinitionInsideStatement) { 1893 EXPECT_EQ("int x,\n" 1894 "#define A\n" 1895 " y;", 1896 format("int x,\n#define A\ny;")); 1897 } 1898 1899 TEST_F(FormatTest, HashInMacroDefinition) { 1900 EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle())); 1901 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); 1902 verifyFormat("#define A \\\n" 1903 " { \\\n" 1904 " f(#c); \\\n" 1905 " }", 1906 getLLVMStyleWithColumns(11)); 1907 1908 verifyFormat("#define A(X) \\\n" 1909 " void function##X()", 1910 getLLVMStyleWithColumns(22)); 1911 1912 verifyFormat("#define A(a, b, c) \\\n" 1913 " void a##b##c()", 1914 getLLVMStyleWithColumns(22)); 1915 1916 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22)); 1917 } 1918 1919 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { 1920 EXPECT_EQ("#define A (x)", format("#define A (x)")); 1921 EXPECT_EQ("#define A(x)", format("#define A(x)")); 1922 } 1923 1924 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) { 1925 EXPECT_EQ("#define A b;", format("#define A \\\n" 1926 " \\\n" 1927 " b;", 1928 getLLVMStyleWithColumns(25))); 1929 EXPECT_EQ("#define A \\\n" 1930 " \\\n" 1931 " a; \\\n" 1932 " b;", 1933 format("#define A \\\n" 1934 " \\\n" 1935 " a; \\\n" 1936 " b;", 1937 getLLVMStyleWithColumns(11))); 1938 EXPECT_EQ("#define A \\\n" 1939 " a; \\\n" 1940 " \\\n" 1941 " b;", 1942 format("#define A \\\n" 1943 " a; \\\n" 1944 " \\\n" 1945 " b;", 1946 getLLVMStyleWithColumns(11))); 1947 } 1948 1949 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { 1950 verifyIncompleteFormat("#define A :"); 1951 verifyFormat("#define SOMECASES \\\n" 1952 " case 1: \\\n" 1953 " case 2\n", 1954 getLLVMStyleWithColumns(20)); 1955 verifyFormat("#define MACRO(a) \\\n" 1956 " if (a) \\\n" 1957 " f(); \\\n" 1958 " else \\\n" 1959 " g()", 1960 getLLVMStyleWithColumns(18)); 1961 verifyFormat("#define A template <typename T>"); 1962 verifyIncompleteFormat("#define STR(x) #x\n" 1963 "f(STR(this_is_a_string_literal{));"); 1964 verifyFormat("#pragma omp threadprivate( \\\n" 1965 " y)), // expected-warning", 1966 getLLVMStyleWithColumns(28)); 1967 verifyFormat("#d, = };"); 1968 verifyFormat("#if \"a"); 1969 verifyIncompleteFormat("({\n" 1970 "#define b \\\n" 1971 " } \\\n" 1972 " a\n" 1973 "a", 1974 getLLVMStyleWithColumns(15)); 1975 verifyFormat("#define A \\\n" 1976 " { \\\n" 1977 " {\n" 1978 "#define B \\\n" 1979 " } \\\n" 1980 " }", 1981 getLLVMStyleWithColumns(15)); 1982 verifyNoCrash("#if a\na(\n#else\n#endif\n{a"); 1983 verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}"); 1984 verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};"); 1985 verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}"); 1986 } 1987 1988 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) { 1989 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline. 1990 EXPECT_EQ("class A : public QObject {\n" 1991 " Q_OBJECT\n" 1992 "\n" 1993 " A() {}\n" 1994 "};", 1995 format("class A : public QObject {\n" 1996 " Q_OBJECT\n" 1997 "\n" 1998 " A() {\n}\n" 1999 "} ;")); 2000 EXPECT_EQ("MACRO\n" 2001 "/*static*/ int i;", 2002 format("MACRO\n" 2003 " /*static*/ int i;")); 2004 EXPECT_EQ("SOME_MACRO\n" 2005 "namespace {\n" 2006 "void f();\n" 2007 "} // namespace", 2008 format("SOME_MACRO\n" 2009 " namespace {\n" 2010 "void f( );\n" 2011 "} // namespace")); 2012 // Only if the identifier contains at least 5 characters. 2013 EXPECT_EQ("HTTP f();", format("HTTP\nf();")); 2014 EXPECT_EQ("MACRO\nf();", format("MACRO\nf();")); 2015 // Only if everything is upper case. 2016 EXPECT_EQ("class A : public QObject {\n" 2017 " Q_Object A() {}\n" 2018 "};", 2019 format("class A : public QObject {\n" 2020 " Q_Object\n" 2021 " A() {\n}\n" 2022 "} ;")); 2023 2024 // Only if the next line can actually start an unwrapped line. 2025 EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;", 2026 format("SOME_WEIRD_LOG_MACRO\n" 2027 "<< SomeThing;")); 2028 2029 verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), " 2030 "(n, buffers))\n", 2031 getChromiumStyle(FormatStyle::LK_Cpp)); 2032 } 2033 2034 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) { 2035 EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 2036 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 2037 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 2038 "class X {};\n" 2039 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 2040 "int *createScopDetectionPass() { return 0; }", 2041 format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 2042 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 2043 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 2044 " class X {};\n" 2045 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 2046 " int *createScopDetectionPass() { return 0; }")); 2047 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as 2048 // braces, so that inner block is indented one level more. 2049 EXPECT_EQ("int q() {\n" 2050 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 2051 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 2052 " IPC_END_MESSAGE_MAP()\n" 2053 "}", 2054 format("int q() {\n" 2055 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 2056 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 2057 " IPC_END_MESSAGE_MAP()\n" 2058 "}")); 2059 2060 // Same inside macros. 2061 EXPECT_EQ("#define LIST(L) \\\n" 2062 " L(A) \\\n" 2063 " L(B) \\\n" 2064 " L(C)", 2065 format("#define LIST(L) \\\n" 2066 " L(A) \\\n" 2067 " L(B) \\\n" 2068 " L(C)", 2069 getGoogleStyle())); 2070 2071 // These must not be recognized as macros. 2072 EXPECT_EQ("int q() {\n" 2073 " f(x);\n" 2074 " f(x) {}\n" 2075 " f(x)->g();\n" 2076 " f(x)->*g();\n" 2077 " f(x).g();\n" 2078 " f(x) = x;\n" 2079 " f(x) += x;\n" 2080 " f(x) -= x;\n" 2081 " f(x) *= x;\n" 2082 " f(x) /= x;\n" 2083 " f(x) %= x;\n" 2084 " f(x) &= x;\n" 2085 " f(x) |= x;\n" 2086 " f(x) ^= x;\n" 2087 " f(x) >>= x;\n" 2088 " f(x) <<= x;\n" 2089 " f(x)[y].z();\n" 2090 " LOG(INFO) << x;\n" 2091 " ifstream(x) >> x;\n" 2092 "}\n", 2093 format("int q() {\n" 2094 " f(x)\n;\n" 2095 " f(x)\n {}\n" 2096 " f(x)\n->g();\n" 2097 " f(x)\n->*g();\n" 2098 " f(x)\n.g();\n" 2099 " f(x)\n = x;\n" 2100 " f(x)\n += x;\n" 2101 " f(x)\n -= x;\n" 2102 " f(x)\n *= x;\n" 2103 " f(x)\n /= x;\n" 2104 " f(x)\n %= x;\n" 2105 " f(x)\n &= x;\n" 2106 " f(x)\n |= x;\n" 2107 " f(x)\n ^= x;\n" 2108 " f(x)\n >>= x;\n" 2109 " f(x)\n <<= x;\n" 2110 " f(x)\n[y].z();\n" 2111 " LOG(INFO)\n << x;\n" 2112 " ifstream(x)\n >> x;\n" 2113 "}\n")); 2114 EXPECT_EQ("int q() {\n" 2115 " F(x)\n" 2116 " if (1) {\n" 2117 " }\n" 2118 " F(x)\n" 2119 " while (1) {\n" 2120 " }\n" 2121 " F(x)\n" 2122 " G(x);\n" 2123 " F(x)\n" 2124 " try {\n" 2125 " Q();\n" 2126 " } catch (...) {\n" 2127 " }\n" 2128 "}\n", 2129 format("int q() {\n" 2130 "F(x)\n" 2131 "if (1) {}\n" 2132 "F(x)\n" 2133 "while (1) {}\n" 2134 "F(x)\n" 2135 "G(x);\n" 2136 "F(x)\n" 2137 "try { Q(); } catch (...) {}\n" 2138 "}\n")); 2139 EXPECT_EQ("class A {\n" 2140 " A() : t(0) {}\n" 2141 " A(int i) noexcept() : {}\n" 2142 " A(X x)\n" // FIXME: function-level try blocks are broken. 2143 " try : t(0) {\n" 2144 " } catch (...) {\n" 2145 " }\n" 2146 "};", 2147 format("class A {\n" 2148 " A()\n : t(0) {}\n" 2149 " A(int i)\n noexcept() : {}\n" 2150 " A(X x)\n" 2151 " try : t(0) {} catch (...) {}\n" 2152 "};")); 2153 EXPECT_EQ("class SomeClass {\n" 2154 "public:\n" 2155 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2156 "};", 2157 format("class SomeClass {\n" 2158 "public:\n" 2159 " SomeClass()\n" 2160 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2161 "};")); 2162 EXPECT_EQ("class SomeClass {\n" 2163 "public:\n" 2164 " SomeClass()\n" 2165 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2166 "};", 2167 format("class SomeClass {\n" 2168 "public:\n" 2169 " SomeClass()\n" 2170 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2171 "};", 2172 getLLVMStyleWithColumns(40))); 2173 2174 verifyFormat("MACRO(>)"); 2175 } 2176 2177 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) { 2178 verifyFormat("#define A \\\n" 2179 " f({ \\\n" 2180 " g(); \\\n" 2181 " });", 2182 getLLVMStyleWithColumns(11)); 2183 } 2184 2185 TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) { 2186 EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}")); 2187 } 2188 2189 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { 2190 verifyFormat("{\n { a #c; }\n}"); 2191 } 2192 2193 TEST_F(FormatTest, FormatUnbalancedStructuralElements) { 2194 EXPECT_EQ("#define A \\\n { \\\n {\nint i;", 2195 format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); 2196 EXPECT_EQ("#define A \\\n } \\\n }\nint i;", 2197 format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); 2198 } 2199 2200 TEST_F(FormatTest, EscapedNewlines) { 2201 EXPECT_EQ( 2202 "#define A \\\n int i; \\\n int j;", 2203 format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11))); 2204 EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;")); 2205 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 2206 EXPECT_EQ("/* \\ \\ \\\n */", format("\\\n/* \\ \\ \\\n */")); 2207 EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>")); 2208 } 2209 2210 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { 2211 verifyFormat("#define A \\\n" 2212 " int v( \\\n" 2213 " a); \\\n" 2214 " int i;", 2215 getLLVMStyleWithColumns(11)); 2216 } 2217 2218 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { 2219 EXPECT_EQ( 2220 "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 2221 " \\\n" 2222 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 2223 "\n" 2224 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 2225 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", 2226 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" 2227 "\\\n" 2228 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 2229 " \n" 2230 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 2231 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); 2232 } 2233 2234 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { 2235 EXPECT_EQ("int\n" 2236 "#define A\n" 2237 " a;", 2238 format("int\n#define A\na;")); 2239 verifyFormat("functionCallTo(\n" 2240 " someOtherFunction(\n" 2241 " withSomeParameters, whichInSequence,\n" 2242 " areLongerThanALine(andAnotherCall,\n" 2243 "#define A B\n" 2244 " withMoreParamters,\n" 2245 " whichStronglyInfluenceTheLayout),\n" 2246 " andMoreParameters),\n" 2247 " trailing);", 2248 getLLVMStyleWithColumns(69)); 2249 verifyFormat("Foo::Foo()\n" 2250 "#ifdef BAR\n" 2251 " : baz(0)\n" 2252 "#endif\n" 2253 "{\n" 2254 "}"); 2255 verifyFormat("void f() {\n" 2256 " if (true)\n" 2257 "#ifdef A\n" 2258 " f(42);\n" 2259 " x();\n" 2260 "#else\n" 2261 " g();\n" 2262 " x();\n" 2263 "#endif\n" 2264 "}"); 2265 verifyFormat("void f(param1, param2,\n" 2266 " param3,\n" 2267 "#ifdef A\n" 2268 " param4(param5,\n" 2269 "#ifdef A1\n" 2270 " param6,\n" 2271 "#ifdef A2\n" 2272 " param7),\n" 2273 "#else\n" 2274 " param8),\n" 2275 " param9,\n" 2276 "#endif\n" 2277 " param10,\n" 2278 "#endif\n" 2279 " param11)\n" 2280 "#else\n" 2281 " param12)\n" 2282 "#endif\n" 2283 "{\n" 2284 " x();\n" 2285 "}", 2286 getLLVMStyleWithColumns(28)); 2287 verifyFormat("#if 1\n" 2288 "int i;"); 2289 verifyFormat("#if 1\n" 2290 "#endif\n" 2291 "#if 1\n" 2292 "#else\n" 2293 "#endif\n"); 2294 verifyFormat("DEBUG({\n" 2295 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2296 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 2297 "});\n" 2298 "#if a\n" 2299 "#else\n" 2300 "#endif"); 2301 2302 verifyIncompleteFormat("void f(\n" 2303 "#if A\n" 2304 ");\n" 2305 "#else\n" 2306 "#endif"); 2307 } 2308 2309 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) { 2310 verifyFormat("#endif\n" 2311 "#if B"); 2312 } 2313 2314 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) { 2315 FormatStyle SingleLine = getLLVMStyle(); 2316 SingleLine.AllowShortIfStatementsOnASingleLine = true; 2317 verifyFormat("#if 0\n" 2318 "#elif 1\n" 2319 "#endif\n" 2320 "void foo() {\n" 2321 " if (test) foo2();\n" 2322 "}", 2323 SingleLine); 2324 } 2325 2326 TEST_F(FormatTest, LayoutBlockInsideParens) { 2327 verifyFormat("functionCall({ int i; });"); 2328 verifyFormat("functionCall({\n" 2329 " int i;\n" 2330 " int j;\n" 2331 "});"); 2332 verifyFormat("functionCall(\n" 2333 " {\n" 2334 " int i;\n" 2335 " int j;\n" 2336 " },\n" 2337 " aaaa, bbbb, cccc);"); 2338 verifyFormat("functionA(functionB({\n" 2339 " int i;\n" 2340 " int j;\n" 2341 " }),\n" 2342 " aaaa, bbbb, cccc);"); 2343 verifyFormat("functionCall(\n" 2344 " {\n" 2345 " int i;\n" 2346 " int j;\n" 2347 " },\n" 2348 " aaaa, bbbb, // comment\n" 2349 " cccc);"); 2350 verifyFormat("functionA(functionB({\n" 2351 " int i;\n" 2352 " int j;\n" 2353 " }),\n" 2354 " aaaa, bbbb, // comment\n" 2355 " cccc);"); 2356 verifyFormat("functionCall(aaaa, bbbb, { int i; });"); 2357 verifyFormat("functionCall(aaaa, bbbb, {\n" 2358 " int i;\n" 2359 " int j;\n" 2360 "});"); 2361 verifyFormat( 2362 "Aaa(\n" // FIXME: There shouldn't be a linebreak here. 2363 " {\n" 2364 " int i; // break\n" 2365 " },\n" 2366 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 2367 " ccccccccccccccccc));"); 2368 verifyFormat("DEBUG({\n" 2369 " if (a)\n" 2370 " f();\n" 2371 "});"); 2372 } 2373 2374 TEST_F(FormatTest, LayoutBlockInsideStatement) { 2375 EXPECT_EQ("SOME_MACRO { int i; }\n" 2376 "int i;", 2377 format(" SOME_MACRO {int i;} int i;")); 2378 } 2379 2380 TEST_F(FormatTest, LayoutNestedBlocks) { 2381 verifyFormat("void AddOsStrings(unsigned bitmask) {\n" 2382 " struct s {\n" 2383 " int i;\n" 2384 " };\n" 2385 " s kBitsToOs[] = {{10}};\n" 2386 " for (int i = 0; i < 10; ++i)\n" 2387 " return;\n" 2388 "}"); 2389 verifyFormat("call(parameter, {\n" 2390 " something();\n" 2391 " // Comment using all columns.\n" 2392 " somethingelse();\n" 2393 "});", 2394 getLLVMStyleWithColumns(40)); 2395 verifyFormat("DEBUG( //\n" 2396 " { f(); }, a);"); 2397 verifyFormat("DEBUG( //\n" 2398 " {\n" 2399 " f(); //\n" 2400 " },\n" 2401 " a);"); 2402 2403 EXPECT_EQ("call(parameter, {\n" 2404 " something();\n" 2405 " // Comment too\n" 2406 " // looooooooooong.\n" 2407 " somethingElse();\n" 2408 "});", 2409 format("call(parameter, {\n" 2410 " something();\n" 2411 " // Comment too looooooooooong.\n" 2412 " somethingElse();\n" 2413 "});", 2414 getLLVMStyleWithColumns(29))); 2415 EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });")); 2416 EXPECT_EQ("DEBUG({ // comment\n" 2417 " int i;\n" 2418 "});", 2419 format("DEBUG({ // comment\n" 2420 "int i;\n" 2421 "});")); 2422 EXPECT_EQ("DEBUG({\n" 2423 " int i;\n" 2424 "\n" 2425 " // comment\n" 2426 " int j;\n" 2427 "});", 2428 format("DEBUG({\n" 2429 " int i;\n" 2430 "\n" 2431 " // comment\n" 2432 " int j;\n" 2433 "});")); 2434 2435 verifyFormat("DEBUG({\n" 2436 " if (a)\n" 2437 " return;\n" 2438 "});"); 2439 verifyGoogleFormat("DEBUG({\n" 2440 " if (a) return;\n" 2441 "});"); 2442 FormatStyle Style = getGoogleStyle(); 2443 Style.ColumnLimit = 45; 2444 verifyFormat("Debug(aaaaa,\n" 2445 " {\n" 2446 " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n" 2447 " },\n" 2448 " a);", 2449 Style); 2450 2451 verifyFormat("SomeFunction({MACRO({ return output; }), b});"); 2452 2453 verifyNoCrash("^{v^{a}}"); 2454 } 2455 2456 TEST_F(FormatTest, FormatNestedBlocksInMacros) { 2457 EXPECT_EQ("#define MACRO() \\\n" 2458 " Debug(aaa, /* force line break */ \\\n" 2459 " { \\\n" 2460 " int i; \\\n" 2461 " int j; \\\n" 2462 " })", 2463 format("#define MACRO() Debug(aaa, /* force line break */ \\\n" 2464 " { int i; int j; })", 2465 getGoogleStyle())); 2466 2467 EXPECT_EQ("#define A \\\n" 2468 " [] { \\\n" 2469 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 2470 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 2471 " }", 2472 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 2473 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 2474 getGoogleStyle())); 2475 } 2476 2477 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { 2478 EXPECT_EQ("{}", format("{}")); 2479 verifyFormat("enum E {};"); 2480 verifyFormat("enum E {}"); 2481 } 2482 2483 TEST_F(FormatTest, FormatBeginBlockEndMacros) { 2484 FormatStyle Style = getLLVMStyle(); 2485 Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$"; 2486 Style.MacroBlockEnd = "^[A-Z_]+_END$"; 2487 verifyFormat("FOO_BEGIN\n" 2488 " FOO_ENTRY\n" 2489 "FOO_END", Style); 2490 verifyFormat("FOO_BEGIN\n" 2491 " NESTED_FOO_BEGIN\n" 2492 " NESTED_FOO_ENTRY\n" 2493 " NESTED_FOO_END\n" 2494 "FOO_END", Style); 2495 verifyFormat("FOO_BEGIN(Foo, Bar)\n" 2496 " int x;\n" 2497 " x = 1;\n" 2498 "FOO_END(Baz)", Style); 2499 } 2500 2501 //===----------------------------------------------------------------------===// 2502 // Line break tests. 2503 //===----------------------------------------------------------------------===// 2504 2505 TEST_F(FormatTest, PreventConfusingIndents) { 2506 verifyFormat( 2507 "void f() {\n" 2508 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" 2509 " parameter, parameter, parameter)),\n" 2510 " SecondLongCall(parameter));\n" 2511 "}"); 2512 verifyFormat( 2513 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2514 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 2515 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 2516 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 2517 verifyFormat( 2518 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2519 " [aaaaaaaaaaaaaaaaaaaaaaaa\n" 2520 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 2521 " [aaaaaaaaaaaaaaaaaaaaaaaa]];"); 2522 verifyFormat( 2523 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 2524 " aaaaaaaaaaaaaaaaaaaaaaaa<\n" 2525 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n" 2526 " aaaaaaaaaaaaaaaaaaaaaaaa>;"); 2527 verifyFormat("int a = bbbb && ccc &&\n" 2528 " fffff(\n" 2529 "#define A Just forcing a new line\n" 2530 " ddd);"); 2531 } 2532 2533 TEST_F(FormatTest, LineBreakingInBinaryExpressions) { 2534 verifyFormat( 2535 "bool aaaaaaa =\n" 2536 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n" 2537 " bbbbbbbb();"); 2538 verifyFormat( 2539 "bool aaaaaaa =\n" 2540 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n" 2541 " bbbbbbbb();"); 2542 2543 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 2544 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n" 2545 " ccccccccc == ddddddddddd;"); 2546 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 2547 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n" 2548 " ccccccccc == ddddddddddd;"); 2549 verifyFormat( 2550 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 2551 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n" 2552 " ccccccccc == ddddddddddd;"); 2553 2554 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 2555 " aaaaaa) &&\n" 2556 " bbbbbb && cccccc;"); 2557 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 2558 " aaaaaa) >>\n" 2559 " bbbbbb;"); 2560 verifyFormat("aa = Whitespaces.addUntouchableComment(\n" 2561 " SourceMgr.getSpellingColumnNumber(\n" 2562 " TheLine.Last->FormatTok.Tok.getLocation()) -\n" 2563 " 1);"); 2564 2565 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2566 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n" 2567 " cccccc) {\n}"); 2568 verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2569 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n" 2570 " cccccc) {\n}"); 2571 verifyFormat("b = a &&\n" 2572 " // Comment\n" 2573 " b.c && d;"); 2574 2575 // If the LHS of a comparison is not a binary expression itself, the 2576 // additional linebreak confuses many people. 2577 verifyFormat( 2578 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2579 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n" 2580 "}"); 2581 verifyFormat( 2582 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2583 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 2584 "}"); 2585 verifyFormat( 2586 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n" 2587 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 2588 "}"); 2589 // Even explicit parentheses stress the precedence enough to make the 2590 // additional break unnecessary. 2591 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2592 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 2593 "}"); 2594 // This cases is borderline, but with the indentation it is still readable. 2595 verifyFormat( 2596 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2597 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2598 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 2599 "}", 2600 getLLVMStyleWithColumns(75)); 2601 2602 // If the LHS is a binary expression, we should still use the additional break 2603 // as otherwise the formatting hides the operator precedence. 2604 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2605 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 2606 " 5) {\n" 2607 "}"); 2608 2609 FormatStyle OnePerLine = getLLVMStyle(); 2610 OnePerLine.BinPackParameters = false; 2611 verifyFormat( 2612 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2613 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2614 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}", 2615 OnePerLine); 2616 2617 verifyFormat("int i = someFunction(aaaaaaa, 0)\n" 2618 " .aaa(aaaaaaaaaaaaa) *\n" 2619 " aaaaaaa +\n" 2620 " aaaaaaa;", 2621 getLLVMStyleWithColumns(40)); 2622 } 2623 2624 TEST_F(FormatTest, ExpressionIndentation) { 2625 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2626 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2627 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 2628 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 2629 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 2630 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n" 2631 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 2632 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n" 2633 " ccccccccccccccccccccccccccccccccccccccccc;"); 2634 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 2635 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2636 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 2637 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 2638 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2639 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 2640 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 2641 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 2642 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 2643 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 2644 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2645 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 2646 verifyFormat("if () {\n" 2647 "} else if (aaaaa && bbbbb > // break\n" 2648 " ccccc) {\n" 2649 "}"); 2650 verifyFormat("if () {\n" 2651 "} else if (aaaaa &&\n" 2652 " bbbbb > // break\n" 2653 " ccccc &&\n" 2654 " ddddd) {\n" 2655 "}"); 2656 2657 // Presence of a trailing comment used to change indentation of b. 2658 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n" 2659 " b;\n" 2660 "return aaaaaaaaaaaaaaaaaaa +\n" 2661 " b; //", 2662 getLLVMStyleWithColumns(30)); 2663 } 2664 2665 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) { 2666 // Not sure what the best system is here. Like this, the LHS can be found 2667 // immediately above an operator (everything with the same or a higher 2668 // indent). The RHS is aligned right of the operator and so compasses 2669 // everything until something with the same indent as the operator is found. 2670 // FIXME: Is this a good system? 2671 FormatStyle Style = getLLVMStyle(); 2672 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 2673 verifyFormat( 2674 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2675 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2676 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2677 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2678 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 2679 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 2680 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2681 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2682 " > ccccccccccccccccccccccccccccccccccccccccc;", 2683 Style); 2684 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2685 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2686 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2687 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 2688 Style); 2689 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2690 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2691 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2692 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 2693 Style); 2694 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2695 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2696 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2697 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 2698 Style); 2699 verifyFormat("if () {\n" 2700 "} else if (aaaaa\n" 2701 " && bbbbb // break\n" 2702 " > ccccc) {\n" 2703 "}", 2704 Style); 2705 verifyFormat("return (a)\n" 2706 " // comment\n" 2707 " + b;", 2708 Style); 2709 verifyFormat( 2710 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2711 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 2712 " + cc;", 2713 Style); 2714 2715 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2716 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 2717 Style); 2718 2719 // Forced by comments. 2720 verifyFormat( 2721 "unsigned ContentSize =\n" 2722 " sizeof(int16_t) // DWARF ARange version number\n" 2723 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 2724 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 2725 " + sizeof(int8_t); // Segment Size (in bytes)"); 2726 2727 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 2728 " == boost::fusion::at_c<1>(iiii).second;", 2729 Style); 2730 2731 Style.ColumnLimit = 60; 2732 verifyFormat("zzzzzzzzzz\n" 2733 " = bbbbbbbbbbbbbbbbb\n" 2734 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 2735 Style); 2736 } 2737 2738 TEST_F(FormatTest, EnforcedOperatorWraps) { 2739 // Here we'd like to wrap after the || operators, but a comment is forcing an 2740 // earlier wrap. 2741 verifyFormat("bool x = aaaaa //\n" 2742 " || bbbbb\n" 2743 " //\n" 2744 " || cccc;"); 2745 } 2746 2747 TEST_F(FormatTest, NoOperandAlignment) { 2748 FormatStyle Style = getLLVMStyle(); 2749 Style.AlignOperands = false; 2750 verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n" 2751 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2752 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 2753 Style); 2754 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 2755 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2756 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2757 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2758 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2759 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 2760 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 2761 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2762 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2763 " > ccccccccccccccccccccccccccccccccccccccccc;", 2764 Style); 2765 2766 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2767 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 2768 " + cc;", 2769 Style); 2770 verifyFormat("int a = aa\n" 2771 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 2772 " * cccccccccccccccccccccccccccccccccccc;\n", 2773 Style); 2774 2775 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 2776 verifyFormat("return (a > b\n" 2777 " // comment1\n" 2778 " // comment2\n" 2779 " || c);", 2780 Style); 2781 } 2782 2783 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) { 2784 FormatStyle Style = getLLVMStyle(); 2785 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 2786 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 2787 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2788 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 2789 Style); 2790 } 2791 2792 TEST_F(FormatTest, AllowBinPackingInsideArguments) { 2793 FormatStyle Style = getLLVMStyle(); 2794 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 2795 Style.BinPackArguments = false; 2796 Style.ColumnLimit = 40; 2797 verifyFormat("void test() {\n" 2798 " someFunction(\n" 2799 " this + argument + is + quite\n" 2800 " + long + so + it + gets + wrapped\n" 2801 " + but + remains + bin - packed);\n" 2802 "}", 2803 Style); 2804 verifyFormat("void test() {\n" 2805 " someFunction(arg1,\n" 2806 " this + argument + is\n" 2807 " + quite + long + so\n" 2808 " + it + gets + wrapped\n" 2809 " + but + remains + bin\n" 2810 " - packed,\n" 2811 " arg3);\n" 2812 "}", 2813 Style); 2814 verifyFormat("void test() {\n" 2815 " someFunction(\n" 2816 " arg1,\n" 2817 " this + argument + has\n" 2818 " + anotherFunc(nested,\n" 2819 " calls + whose\n" 2820 " + arguments\n" 2821 " + are + also\n" 2822 " + wrapped,\n" 2823 " in + addition)\n" 2824 " + to + being + bin - packed,\n" 2825 " arg3);\n" 2826 "}", 2827 Style); 2828 2829 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 2830 verifyFormat("void test() {\n" 2831 " someFunction(\n" 2832 " arg1,\n" 2833 " this + argument + has +\n" 2834 " anotherFunc(nested,\n" 2835 " calls + whose +\n" 2836 " arguments +\n" 2837 " are + also +\n" 2838 " wrapped,\n" 2839 " in + addition) +\n" 2840 " to + being + bin - packed,\n" 2841 " arg3);\n" 2842 "}", 2843 Style); 2844 } 2845 2846 TEST_F(FormatTest, ConstructorInitializers) { 2847 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 2848 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", 2849 getLLVMStyleWithColumns(45)); 2850 verifyFormat("Constructor()\n" 2851 " : Inttializer(FitsOnTheLine) {}", 2852 getLLVMStyleWithColumns(44)); 2853 verifyFormat("Constructor()\n" 2854 " : Inttializer(FitsOnTheLine) {}", 2855 getLLVMStyleWithColumns(43)); 2856 2857 verifyFormat("template <typename T>\n" 2858 "Constructor() : Initializer(FitsOnTheLine) {}", 2859 getLLVMStyleWithColumns(45)); 2860 2861 verifyFormat( 2862 "SomeClass::Constructor()\n" 2863 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 2864 2865 verifyFormat( 2866 "SomeClass::Constructor()\n" 2867 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 2868 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}"); 2869 verifyFormat( 2870 "SomeClass::Constructor()\n" 2871 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 2872 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 2873 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 2874 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 2875 " : aaaaaaaaaa(aaaaaa) {}"); 2876 2877 verifyFormat("Constructor()\n" 2878 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 2879 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 2880 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 2881 " aaaaaaaaaaaaaaaaaaaaaaa() {}"); 2882 2883 verifyFormat("Constructor()\n" 2884 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2885 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 2886 2887 verifyFormat("Constructor(int Parameter = 0)\n" 2888 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 2889 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}"); 2890 verifyFormat("Constructor()\n" 2891 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 2892 "}", 2893 getLLVMStyleWithColumns(60)); 2894 verifyFormat("Constructor()\n" 2895 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2896 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}"); 2897 2898 // Here a line could be saved by splitting the second initializer onto two 2899 // lines, but that is not desirable. 2900 verifyFormat("Constructor()\n" 2901 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 2902 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 2903 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 2904 2905 FormatStyle OnePerLine = getLLVMStyle(); 2906 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 2907 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false; 2908 verifyFormat("SomeClass::Constructor()\n" 2909 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 2910 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 2911 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 2912 OnePerLine); 2913 verifyFormat("SomeClass::Constructor()\n" 2914 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 2915 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 2916 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 2917 OnePerLine); 2918 verifyFormat("MyClass::MyClass(int var)\n" 2919 " : some_var_(var), // 4 space indent\n" 2920 " some_other_var_(var + 1) { // lined up\n" 2921 "}", 2922 OnePerLine); 2923 verifyFormat("Constructor()\n" 2924 " : aaaaa(aaaaaa),\n" 2925 " aaaaa(aaaaaa),\n" 2926 " aaaaa(aaaaaa),\n" 2927 " aaaaa(aaaaaa),\n" 2928 " aaaaa(aaaaaa) {}", 2929 OnePerLine); 2930 verifyFormat("Constructor()\n" 2931 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 2932 " aaaaaaaaaaaaaaaaaaaaaa) {}", 2933 OnePerLine); 2934 OnePerLine.BinPackParameters = false; 2935 verifyFormat( 2936 "Constructor()\n" 2937 " : aaaaaaaaaaaaaaaaaaaaaaaa(\n" 2938 " aaaaaaaaaaa().aaa(),\n" 2939 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 2940 OnePerLine); 2941 OnePerLine.ColumnLimit = 60; 2942 verifyFormat("Constructor()\n" 2943 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 2944 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 2945 OnePerLine); 2946 2947 EXPECT_EQ("Constructor()\n" 2948 " : // Comment forcing unwanted break.\n" 2949 " aaaa(aaaa) {}", 2950 format("Constructor() :\n" 2951 " // Comment forcing unwanted break.\n" 2952 " aaaa(aaaa) {}")); 2953 } 2954 2955 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) { 2956 FormatStyle Style = getLLVMStyle(); 2957 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 2958 2959 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 2960 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}", 2961 getStyleWithColumns(Style, 45)); 2962 verifyFormat("Constructor() :\n" 2963 " Initializer(FitsOnTheLine) {}", 2964 getStyleWithColumns(Style, 44)); 2965 verifyFormat("Constructor() :\n" 2966 " Initializer(FitsOnTheLine) {}", 2967 getStyleWithColumns(Style, 43)); 2968 2969 verifyFormat("template <typename T>\n" 2970 "Constructor() : Initializer(FitsOnTheLine) {}", 2971 getStyleWithColumns(Style, 50)); 2972 2973 verifyFormat( 2974 "SomeClass::Constructor() :\n" 2975 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 2976 Style); 2977 2978 verifyFormat( 2979 "SomeClass::Constructor() :\n" 2980 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 2981 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 2982 Style); 2983 verifyFormat( 2984 "SomeClass::Constructor() :\n" 2985 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 2986 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 2987 Style); 2988 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 2989 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 2990 " aaaaaaaaaa(aaaaaa) {}", 2991 Style); 2992 2993 verifyFormat("Constructor() :\n" 2994 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 2995 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 2996 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 2997 " aaaaaaaaaaaaaaaaaaaaaaa() {}", 2998 Style); 2999 3000 verifyFormat("Constructor() :\n" 3001 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3002 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3003 Style); 3004 3005 verifyFormat("Constructor(int Parameter = 0) :\n" 3006 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 3007 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}", 3008 Style); 3009 verifyFormat("Constructor() :\n" 3010 " aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 3011 "}", 3012 getStyleWithColumns(Style, 60)); 3013 verifyFormat("Constructor() :\n" 3014 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3015 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}", 3016 Style); 3017 3018 // Here a line could be saved by splitting the second initializer onto two 3019 // lines, but that is not desirable. 3020 verifyFormat("Constructor() :\n" 3021 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 3022 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 3023 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3024 Style); 3025 3026 FormatStyle OnePerLine = Style; 3027 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 3028 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false; 3029 verifyFormat("SomeClass::Constructor() :\n" 3030 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3031 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3032 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3033 OnePerLine); 3034 verifyFormat("SomeClass::Constructor() :\n" 3035 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 3036 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3037 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3038 OnePerLine); 3039 verifyFormat("MyClass::MyClass(int var) :\n" 3040 " some_var_(var), // 4 space indent\n" 3041 " some_other_var_(var + 1) { // lined up\n" 3042 "}", 3043 OnePerLine); 3044 verifyFormat("Constructor() :\n" 3045 " aaaaa(aaaaaa),\n" 3046 " aaaaa(aaaaaa),\n" 3047 " aaaaa(aaaaaa),\n" 3048 " aaaaa(aaaaaa),\n" 3049 " aaaaa(aaaaaa) {}", 3050 OnePerLine); 3051 verifyFormat("Constructor() :\n" 3052 " aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 3053 " aaaaaaaaaaaaaaaaaaaaaa) {}", 3054 OnePerLine); 3055 OnePerLine.BinPackParameters = false; 3056 verifyFormat( 3057 "Constructor() :\n" 3058 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 3059 " aaaaaaaaaaa().aaa(),\n" 3060 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3061 OnePerLine); 3062 OnePerLine.ColumnLimit = 60; 3063 verifyFormat("Constructor() :\n" 3064 " aaaaaaaaaaaaaaaaaaaa(a),\n" 3065 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 3066 OnePerLine); 3067 3068 EXPECT_EQ("Constructor() :\n" 3069 " // Comment forcing unwanted break.\n" 3070 " aaaa(aaaa) {}", 3071 format("Constructor() :\n" 3072 " // Comment forcing unwanted break.\n" 3073 " aaaa(aaaa) {}", 3074 Style)); 3075 3076 Style.ColumnLimit = 0; 3077 verifyFormat("SomeClass::Constructor() :\n" 3078 " a(a) {}", 3079 Style); 3080 verifyFormat("SomeClass::Constructor() noexcept :\n" 3081 " a(a) {}", 3082 Style); 3083 verifyFormat("SomeClass::Constructor() :\n" 3084 " a(a), b(b), c(c) {}", 3085 Style); 3086 verifyFormat("SomeClass::Constructor() :\n" 3087 " a(a) {\n" 3088 " foo();\n" 3089 " bar();\n" 3090 "}", 3091 Style); 3092 3093 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 3094 verifyFormat("SomeClass::Constructor() :\n" 3095 " a(a), b(b), c(c) {\n" 3096 "}", 3097 Style); 3098 verifyFormat("SomeClass::Constructor() :\n" 3099 " a(a) {\n" 3100 "}", 3101 Style); 3102 3103 Style.ColumnLimit = 80; 3104 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 3105 Style.ConstructorInitializerIndentWidth = 2; 3106 verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", 3107 Style); 3108 verifyFormat("SomeClass::Constructor() :\n" 3109 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3110 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}", 3111 Style); 3112 } 3113 3114 TEST_F(FormatTest, MemoizationTests) { 3115 // This breaks if the memoization lookup does not take \c Indent and 3116 // \c LastSpace into account. 3117 verifyFormat( 3118 "extern CFRunLoopTimerRef\n" 3119 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n" 3120 " CFTimeInterval interval, CFOptionFlags flags,\n" 3121 " CFIndex order, CFRunLoopTimerCallBack callout,\n" 3122 " CFRunLoopTimerContext *context) {}"); 3123 3124 // Deep nesting somewhat works around our memoization. 3125 verifyFormat( 3126 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3127 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3128 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3129 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3130 " aaaaa())))))))))))))))))))))))))))))))))))))));", 3131 getLLVMStyleWithColumns(65)); 3132 verifyFormat( 3133 "aaaaa(\n" 3134 " aaaaa,\n" 3135 " aaaaa(\n" 3136 " aaaaa,\n" 3137 " aaaaa(\n" 3138 " aaaaa,\n" 3139 " aaaaa(\n" 3140 " aaaaa,\n" 3141 " aaaaa(\n" 3142 " aaaaa,\n" 3143 " aaaaa(\n" 3144 " aaaaa,\n" 3145 " aaaaa(\n" 3146 " aaaaa,\n" 3147 " aaaaa(\n" 3148 " aaaaa,\n" 3149 " aaaaa(\n" 3150 " aaaaa,\n" 3151 " aaaaa(\n" 3152 " aaaaa,\n" 3153 " aaaaa(\n" 3154 " aaaaa,\n" 3155 " aaaaa(\n" 3156 " aaaaa,\n" 3157 " aaaaa))))))))))));", 3158 getLLVMStyleWithColumns(65)); 3159 verifyFormat( 3160 "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" 3161 " a),\n" 3162 " a),\n" 3163 " a),\n" 3164 " a),\n" 3165 " a),\n" 3166 " a),\n" 3167 " a),\n" 3168 " a),\n" 3169 " a),\n" 3170 " a),\n" 3171 " a),\n" 3172 " a),\n" 3173 " a),\n" 3174 " a),\n" 3175 " a),\n" 3176 " a),\n" 3177 " a)", 3178 getLLVMStyleWithColumns(65)); 3179 3180 // This test takes VERY long when memoization is broken. 3181 FormatStyle OnePerLine = getLLVMStyle(); 3182 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 3183 OnePerLine.BinPackParameters = false; 3184 std::string input = "Constructor()\n" 3185 " : aaaa(a,\n"; 3186 for (unsigned i = 0, e = 80; i != e; ++i) { 3187 input += " a,\n"; 3188 } 3189 input += " a) {}"; 3190 verifyFormat(input, OnePerLine); 3191 } 3192 3193 TEST_F(FormatTest, BreaksAsHighAsPossible) { 3194 verifyFormat( 3195 "void f() {\n" 3196 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" 3197 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" 3198 " f();\n" 3199 "}"); 3200 verifyFormat("if (Intervals[i].getRange().getFirst() <\n" 3201 " Intervals[i - 1].getRange().getLast()) {\n}"); 3202 } 3203 3204 TEST_F(FormatTest, BreaksFunctionDeclarations) { 3205 // Principially, we break function declarations in a certain order: 3206 // 1) break amongst arguments. 3207 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n" 3208 " Cccccccccccccc cccccccccccccc);"); 3209 verifyFormat("template <class TemplateIt>\n" 3210 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n" 3211 " TemplateIt *stop) {}"); 3212 3213 // 2) break after return type. 3214 verifyFormat( 3215 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3216 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);", 3217 getGoogleStyle()); 3218 3219 // 3) break after (. 3220 verifyFormat( 3221 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n" 3222 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);", 3223 getGoogleStyle()); 3224 3225 // 4) break before after nested name specifiers. 3226 verifyFormat( 3227 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3228 "SomeClasssssssssssssssssssssssssssssssssssssss::\n" 3229 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);", 3230 getGoogleStyle()); 3231 3232 // However, there are exceptions, if a sufficient amount of lines can be 3233 // saved. 3234 // FIXME: The precise cut-offs wrt. the number of saved lines might need some 3235 // more adjusting. 3236 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 3237 " Cccccccccccccc cccccccccc,\n" 3238 " Cccccccccccccc cccccccccc,\n" 3239 " Cccccccccccccc cccccccccc,\n" 3240 " Cccccccccccccc cccccccccc);"); 3241 verifyFormat( 3242 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3243 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3244 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3245 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);", 3246 getGoogleStyle()); 3247 verifyFormat( 3248 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 3249 " Cccccccccccccc cccccccccc,\n" 3250 " Cccccccccccccc cccccccccc,\n" 3251 " Cccccccccccccc cccccccccc,\n" 3252 " Cccccccccccccc cccccccccc,\n" 3253 " Cccccccccccccc cccccccccc,\n" 3254 " Cccccccccccccc cccccccccc);"); 3255 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 3256 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3257 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3258 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3259 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); 3260 3261 // Break after multi-line parameters. 3262 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3263 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3264 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3265 " bbbb bbbb);"); 3266 verifyFormat("void SomeLoooooooooooongFunction(\n" 3267 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 3268 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3269 " int bbbbbbbbbbbbb);"); 3270 3271 // Treat overloaded operators like other functions. 3272 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3273 "operator>(const SomeLoooooooooooooooooooooooooogType &other);"); 3274 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3275 "operator>>(const SomeLooooooooooooooooooooooooogType &other);"); 3276 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3277 "operator<<(const SomeLooooooooooooooooooooooooogType &other);"); 3278 verifyGoogleFormat( 3279 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n" 3280 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 3281 verifyGoogleFormat( 3282 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n" 3283 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 3284 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3285 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 3286 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n" 3287 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 3288 verifyGoogleFormat( 3289 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n" 3290 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3291 " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}"); 3292 verifyGoogleFormat( 3293 "template <typename T>\n" 3294 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3295 "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n" 3296 " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);"); 3297 3298 FormatStyle Style = getLLVMStyle(); 3299 Style.PointerAlignment = FormatStyle::PAS_Left; 3300 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3301 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}", 3302 Style); 3303 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n" 3304 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3305 Style); 3306 } 3307 3308 TEST_F(FormatTest, TrailingReturnType) { 3309 verifyFormat("auto foo() -> int;\n"); 3310 verifyFormat("struct S {\n" 3311 " auto bar() const -> int;\n" 3312 "};"); 3313 verifyFormat("template <size_t Order, typename T>\n" 3314 "auto load_img(const std::string &filename)\n" 3315 " -> alias::tensor<Order, T, mem::tag::cpu> {}"); 3316 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n" 3317 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}"); 3318 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}"); 3319 verifyFormat("template <typename T>\n" 3320 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n" 3321 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());"); 3322 3323 // Not trailing return types. 3324 verifyFormat("void f() { auto a = b->c(); }"); 3325 } 3326 3327 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { 3328 // Avoid breaking before trailing 'const' or other trailing annotations, if 3329 // they are not function-like. 3330 FormatStyle Style = getGoogleStyle(); 3331 Style.ColumnLimit = 47; 3332 verifyFormat("void someLongFunction(\n" 3333 " int someLoooooooooooooongParameter) const {\n}", 3334 getLLVMStyleWithColumns(47)); 3335 verifyFormat("LoooooongReturnType\n" 3336 "someLoooooooongFunction() const {}", 3337 getLLVMStyleWithColumns(47)); 3338 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n" 3339 " const {}", 3340 Style); 3341 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3342 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;"); 3343 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3344 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;"); 3345 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3346 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;"); 3347 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n" 3348 " aaaaaaaaaaa aaaaa) const override;"); 3349 verifyGoogleFormat( 3350 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 3351 " const override;"); 3352 3353 // Even if the first parameter has to be wrapped. 3354 verifyFormat("void someLongFunction(\n" 3355 " int someLongParameter) const {}", 3356 getLLVMStyleWithColumns(46)); 3357 verifyFormat("void someLongFunction(\n" 3358 " int someLongParameter) const {}", 3359 Style); 3360 verifyFormat("void someLongFunction(\n" 3361 " int someLongParameter) override {}", 3362 Style); 3363 verifyFormat("void someLongFunction(\n" 3364 " int someLongParameter) OVERRIDE {}", 3365 Style); 3366 verifyFormat("void someLongFunction(\n" 3367 " int someLongParameter) final {}", 3368 Style); 3369 verifyFormat("void someLongFunction(\n" 3370 " int someLongParameter) FINAL {}", 3371 Style); 3372 verifyFormat("void someLongFunction(\n" 3373 " int parameter) const override {}", 3374 Style); 3375 3376 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 3377 verifyFormat("void someLongFunction(\n" 3378 " int someLongParameter) const\n" 3379 "{\n" 3380 "}", 3381 Style); 3382 3383 // Unless these are unknown annotations. 3384 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n" 3385 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3386 " LONG_AND_UGLY_ANNOTATION;"); 3387 3388 // Breaking before function-like trailing annotations is fine to keep them 3389 // close to their arguments. 3390 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3391 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 3392 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 3393 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 3394 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 3395 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}"); 3396 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n" 3397 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);"); 3398 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});"); 3399 3400 verifyFormat( 3401 "void aaaaaaaaaaaaaaaaaa()\n" 3402 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n" 3403 " aaaaaaaaaaaaaaaaaaaaaaaaa));"); 3404 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3405 " __attribute__((unused));"); 3406 verifyGoogleFormat( 3407 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3408 " GUARDED_BY(aaaaaaaaaaaa);"); 3409 verifyGoogleFormat( 3410 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3411 " GUARDED_BY(aaaaaaaaaaaa);"); 3412 verifyGoogleFormat( 3413 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 3414 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 3415 verifyGoogleFormat( 3416 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 3417 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 3418 } 3419 3420 TEST_F(FormatTest, FunctionAnnotations) { 3421 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 3422 "int OldFunction(const string ¶meter) {}"); 3423 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 3424 "string OldFunction(const string ¶meter) {}"); 3425 verifyFormat("template <typename T>\n" 3426 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 3427 "string OldFunction(const string ¶meter) {}"); 3428 3429 // Not function annotations. 3430 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3431 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); 3432 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n" 3433 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}"); 3434 verifyFormat("MACRO(abc).function() // wrap\n" 3435 " << abc;"); 3436 verifyFormat("MACRO(abc)->function() // wrap\n" 3437 " << abc;"); 3438 verifyFormat("MACRO(abc)::function() // wrap\n" 3439 " << abc;"); 3440 } 3441 3442 TEST_F(FormatTest, BreaksDesireably) { 3443 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 3444 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 3445 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); 3446 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3447 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 3448 "}"); 3449 3450 verifyFormat( 3451 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3452 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 3453 3454 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3455 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3456 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 3457 3458 verifyFormat( 3459 "aaaaaaaa(aaaaaaaaaaaaa,\n" 3460 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3461 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 3462 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3463 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 3464 3465 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3466 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3467 3468 verifyFormat( 3469 "void f() {\n" 3470 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 3471 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 3472 "}"); 3473 verifyFormat( 3474 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3475 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 3476 verifyFormat( 3477 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3478 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 3479 verifyFormat( 3480 "aaaaaa(aaa,\n" 3481 " new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3482 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3483 " aaaa);"); 3484 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3485 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3486 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3487 3488 // Indent consistently independent of call expression and unary operator. 3489 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 3490 " dddddddddddddddddddddddddddddd));"); 3491 verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 3492 " dddddddddddddddddddddddddddddd));"); 3493 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n" 3494 " dddddddddddddddddddddddddddddd));"); 3495 3496 // This test case breaks on an incorrect memoization, i.e. an optimization not 3497 // taking into account the StopAt value. 3498 verifyFormat( 3499 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 3500 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 3501 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 3502 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3503 3504 verifyFormat("{\n {\n {\n" 3505 " Annotation.SpaceRequiredBefore =\n" 3506 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" 3507 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" 3508 " }\n }\n}"); 3509 3510 // Break on an outer level if there was a break on an inner level. 3511 EXPECT_EQ("f(g(h(a, // comment\n" 3512 " b, c),\n" 3513 " d, e),\n" 3514 " x, y);", 3515 format("f(g(h(a, // comment\n" 3516 " b, c), d, e), x, y);")); 3517 3518 // Prefer breaking similar line breaks. 3519 verifyFormat( 3520 "const int kTrackingOptions = NSTrackingMouseMoved |\n" 3521 " NSTrackingMouseEnteredAndExited |\n" 3522 " NSTrackingActiveAlways;"); 3523 } 3524 3525 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) { 3526 FormatStyle NoBinPacking = getGoogleStyle(); 3527 NoBinPacking.BinPackParameters = false; 3528 NoBinPacking.BinPackArguments = true; 3529 verifyFormat("void f() {\n" 3530 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n" 3531 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 3532 "}", 3533 NoBinPacking); 3534 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n" 3535 " int aaaaaaaaaaaaaaaaaaaa,\n" 3536 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3537 NoBinPacking); 3538 3539 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 3540 verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3541 " vector<int> bbbbbbbbbbbbbbb);", 3542 NoBinPacking); 3543 // FIXME: This behavior difference is probably not wanted. However, currently 3544 // we cannot distinguish BreakBeforeParameter being set because of the wrapped 3545 // template arguments from BreakBeforeParameter being set because of the 3546 // one-per-line formatting. 3547 verifyFormat( 3548 "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n" 3549 " aaaaaaaaaa> aaaaaaaaaa);", 3550 NoBinPacking); 3551 verifyFormat( 3552 "void fffffffffff(\n" 3553 " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n" 3554 " aaaaaaaaaa);"); 3555 } 3556 3557 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { 3558 FormatStyle NoBinPacking = getGoogleStyle(); 3559 NoBinPacking.BinPackParameters = false; 3560 NoBinPacking.BinPackArguments = false; 3561 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" 3562 " aaaaaaaaaaaaaaaaaaaa,\n" 3563 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);", 3564 NoBinPacking); 3565 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n" 3566 " aaaaaaaaaaaaa,\n" 3567 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));", 3568 NoBinPacking); 3569 verifyFormat( 3570 "aaaaaaaa(aaaaaaaaaaaaa,\n" 3571 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3572 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 3573 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3574 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));", 3575 NoBinPacking); 3576 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 3577 " .aaaaaaaaaaaaaaaaaa();", 3578 NoBinPacking); 3579 verifyFormat("void f() {\n" 3580 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3581 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n" 3582 "}", 3583 NoBinPacking); 3584 3585 verifyFormat( 3586 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3587 " aaaaaaaaaaaa,\n" 3588 " aaaaaaaaaaaa);", 3589 NoBinPacking); 3590 verifyFormat( 3591 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" 3592 " ddddddddddddddddddddddddddddd),\n" 3593 " test);", 3594 NoBinPacking); 3595 3596 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" 3597 " aaaaaaaaaaaaaaaaaaaaaaa,\n" 3598 " aaaaaaaaaaaaaaaaaaaaaaa>\n" 3599 " aaaaaaaaaaaaaaaaaa;", 3600 NoBinPacking); 3601 verifyFormat("a(\"a\"\n" 3602 " \"a\",\n" 3603 " a);"); 3604 3605 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 3606 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" 3607 " aaaaaaaaa,\n" 3608 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 3609 NoBinPacking); 3610 verifyFormat( 3611 "void f() {\n" 3612 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 3613 " .aaaaaaa();\n" 3614 "}", 3615 NoBinPacking); 3616 verifyFormat( 3617 "template <class SomeType, class SomeOtherType>\n" 3618 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}", 3619 NoBinPacking); 3620 } 3621 3622 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) { 3623 FormatStyle Style = getLLVMStyleWithColumns(15); 3624 Style.ExperimentalAutoDetectBinPacking = true; 3625 EXPECT_EQ("aaa(aaaa,\n" 3626 " aaaa,\n" 3627 " aaaa);\n" 3628 "aaa(aaaa,\n" 3629 " aaaa,\n" 3630 " aaaa);", 3631 format("aaa(aaaa,\n" // one-per-line 3632 " aaaa,\n" 3633 " aaaa );\n" 3634 "aaa(aaaa, aaaa, aaaa);", // inconclusive 3635 Style)); 3636 EXPECT_EQ("aaa(aaaa, aaaa,\n" 3637 " aaaa);\n" 3638 "aaa(aaaa, aaaa,\n" 3639 " aaaa);", 3640 format("aaa(aaaa, aaaa,\n" // bin-packed 3641 " aaaa );\n" 3642 "aaa(aaaa, aaaa, aaaa);", // inconclusive 3643 Style)); 3644 } 3645 3646 TEST_F(FormatTest, FormatsBuilderPattern) { 3647 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n" 3648 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" 3649 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n" 3650 " .StartsWith(\".init\", ORDER_INIT)\n" 3651 " .StartsWith(\".fini\", ORDER_FINI)\n" 3652 " .StartsWith(\".hash\", ORDER_HASH)\n" 3653 " .Default(ORDER_TEXT);\n"); 3654 3655 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" 3656 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); 3657 verifyFormat( 3658 "aaaaaaa->aaaaaaa\n" 3659 " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3660 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3661 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 3662 verifyFormat( 3663 "aaaaaaa->aaaaaaa\n" 3664 " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3665 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 3666 verifyFormat( 3667 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" 3668 " aaaaaaaaaaaaaa);"); 3669 verifyFormat( 3670 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n" 3671 " aaaaaa->aaaaaaaaaaaa()\n" 3672 " ->aaaaaaaaaaaaaaaa(\n" 3673 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3674 " ->aaaaaaaaaaaaaaaaa();"); 3675 verifyGoogleFormat( 3676 "void f() {\n" 3677 " someo->Add((new util::filetools::Handler(dir))\n" 3678 " ->OnEvent1(NewPermanentCallback(\n" 3679 " this, &HandlerHolderClass::EventHandlerCBA))\n" 3680 " ->OnEvent2(NewPermanentCallback(\n" 3681 " this, &HandlerHolderClass::EventHandlerCBB))\n" 3682 " ->OnEvent3(NewPermanentCallback(\n" 3683 " this, &HandlerHolderClass::EventHandlerCBC))\n" 3684 " ->OnEvent5(NewPermanentCallback(\n" 3685 " this, &HandlerHolderClass::EventHandlerCBD))\n" 3686 " ->OnEvent6(NewPermanentCallback(\n" 3687 " this, &HandlerHolderClass::EventHandlerCBE)));\n" 3688 "}"); 3689 3690 verifyFormat( 3691 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();"); 3692 verifyFormat("aaaaaaaaaaaaaaa()\n" 3693 " .aaaaaaaaaaaaaaa()\n" 3694 " .aaaaaaaaaaaaaaa()\n" 3695 " .aaaaaaaaaaaaaaa()\n" 3696 " .aaaaaaaaaaaaaaa();"); 3697 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 3698 " .aaaaaaaaaaaaaaa()\n" 3699 " .aaaaaaaaaaaaaaa()\n" 3700 " .aaaaaaaaaaaaaaa();"); 3701 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 3702 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 3703 " .aaaaaaaaaaaaaaa();"); 3704 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n" 3705 " ->aaaaaaaaaaaaaae(0)\n" 3706 " ->aaaaaaaaaaaaaaa();"); 3707 3708 // Don't linewrap after very short segments. 3709 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 3710 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 3711 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 3712 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 3713 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 3714 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 3715 verifyFormat("aaa()\n" 3716 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 3717 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 3718 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 3719 3720 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 3721 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 3722 " .has<bbbbbbbbbbbbbbbbbbbbb>();"); 3723 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 3724 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 3725 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();"); 3726 3727 // Prefer not to break after empty parentheses. 3728 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n" 3729 " First->LastNewlineOffset);"); 3730 3731 // Prefer not to create "hanging" indents. 3732 verifyFormat( 3733 "return !soooooooooooooome_map\n" 3734 " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3735 " .second;"); 3736 verifyFormat( 3737 "return aaaaaaaaaaaaaaaa\n" 3738 " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n" 3739 " .aaaa(aaaaaaaaaaaaaa);"); 3740 // No hanging indent here. 3741 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n" 3742 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3743 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n" 3744 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3745 verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 3746 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 3747 getLLVMStyleWithColumns(60)); 3748 verifyFormat("aaaaaaaaaaaaaaaaaa\n" 3749 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 3750 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 3751 getLLVMStyleWithColumns(59)); 3752 verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3753 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3754 " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3755 } 3756 3757 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { 3758 verifyFormat( 3759 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3760 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); 3761 verifyFormat( 3762 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n" 3763 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}"); 3764 3765 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 3766 " ccccccccccccccccccccccccc) {\n}"); 3767 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n" 3768 " ccccccccccccccccccccccccc) {\n}"); 3769 3770 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 3771 " ccccccccccccccccccccccccc) {\n}"); 3772 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n" 3773 " ccccccccccccccccccccccccc) {\n}"); 3774 3775 verifyFormat( 3776 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" 3777 " ccccccccccccccccccccccccc) {\n}"); 3778 verifyFormat( 3779 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n" 3780 " ccccccccccccccccccccccccc) {\n}"); 3781 3782 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" 3783 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" 3784 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" 3785 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 3786 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n" 3787 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n" 3788 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n" 3789 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 3790 3791 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" 3792 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" 3793 " aaaaaaaaaaaaaaa != aa) {\n}"); 3794 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n" 3795 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n" 3796 " aaaaaaaaaaaaaaa != aa) {\n}"); 3797 } 3798 3799 TEST_F(FormatTest, BreaksAfterAssignments) { 3800 verifyFormat( 3801 "unsigned Cost =\n" 3802 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" 3803 " SI->getPointerAddressSpaceee());\n"); 3804 verifyFormat( 3805 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" 3806 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); 3807 3808 verifyFormat( 3809 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n" 3810 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); 3811 verifyFormat("unsigned OriginalStartColumn =\n" 3812 " SourceMgr.getSpellingColumnNumber(\n" 3813 " Current.FormatTok.getStartOfNonWhitespace()) -\n" 3814 " 1;"); 3815 } 3816 3817 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) { 3818 FormatStyle Style = getLLVMStyle(); 3819 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 3820 " bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;", 3821 Style); 3822 3823 Style.PenaltyBreakAssignment = 20; 3824 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 3825 " cccccccccccccccccccccccccc;", 3826 Style); 3827 } 3828 3829 TEST_F(FormatTest, AlignsAfterAssignments) { 3830 verifyFormat( 3831 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3832 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 3833 verifyFormat( 3834 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3835 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 3836 verifyFormat( 3837 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3838 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 3839 verifyFormat( 3840 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3841 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 3842 verifyFormat( 3843 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" 3844 " aaaaaaaaaaaaaaaaaaaaaaaa +\n" 3845 " aaaaaaaaaaaaaaaaaaaaaaaa;"); 3846 } 3847 3848 TEST_F(FormatTest, AlignsAfterReturn) { 3849 verifyFormat( 3850 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3851 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 3852 verifyFormat( 3853 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3854 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 3855 verifyFormat( 3856 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 3857 " aaaaaaaaaaaaaaaaaaaaaa();"); 3858 verifyFormat( 3859 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 3860 " aaaaaaaaaaaaaaaaaaaaaa());"); 3861 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3862 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3863 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3864 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n" 3865 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 3866 verifyFormat("return\n" 3867 " // true if code is one of a or b.\n" 3868 " code == a || code == b;"); 3869 } 3870 3871 TEST_F(FormatTest, AlignsAfterOpenBracket) { 3872 verifyFormat( 3873 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 3874 " aaaaaaaaa aaaaaaa) {}"); 3875 verifyFormat( 3876 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 3877 " aaaaaaaaaaa aaaaaaaaa);"); 3878 verifyFormat( 3879 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 3880 " aaaaaaaaaaaaaaaaaaaaa));"); 3881 FormatStyle Style = getLLVMStyle(); 3882 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 3883 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3884 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}", 3885 Style); 3886 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 3887 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);", 3888 Style); 3889 verifyFormat("SomeLongVariableName->someFunction(\n" 3890 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));", 3891 Style); 3892 verifyFormat( 3893 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 3894 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3895 Style); 3896 verifyFormat( 3897 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 3898 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 3899 Style); 3900 verifyFormat( 3901 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 3902 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 3903 Style); 3904 3905 verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n" 3906 " ccccccc(aaaaaaaaaaaaaaaaa, //\n" 3907 " b));", 3908 Style); 3909 3910 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 3911 Style.BinPackArguments = false; 3912 Style.BinPackParameters = false; 3913 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3914 " aaaaaaaaaaa aaaaaaaa,\n" 3915 " aaaaaaaaa aaaaaaa,\n" 3916 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3917 Style); 3918 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 3919 " aaaaaaaaaaa aaaaaaaaa,\n" 3920 " aaaaaaaaaaa aaaaaaaaa,\n" 3921 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 3922 Style); 3923 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n" 3924 " aaaaaaaaaaaaaaa,\n" 3925 " aaaaaaaaaaaaaaaaaaaaa,\n" 3926 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 3927 Style); 3928 verifyFormat( 3929 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n" 3930 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 3931 Style); 3932 verifyFormat( 3933 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n" 3934 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 3935 Style); 3936 verifyFormat( 3937 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 3938 " aaaaaaaaaaaaaaaaaaaaa(\n" 3939 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n" 3940 " aaaaaaaaaaaaaaaa);", 3941 Style); 3942 verifyFormat( 3943 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 3944 " aaaaaaaaaaaaaaaaaaaaa(\n" 3945 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n" 3946 " aaaaaaaaaaaaaaaa);", 3947 Style); 3948 } 3949 3950 TEST_F(FormatTest, ParenthesesAndOperandAlignment) { 3951 FormatStyle Style = getLLVMStyleWithColumns(40); 3952 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 3953 " bbbbbbbbbbbbbbbbbbbbbb);", 3954 Style); 3955 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 3956 Style.AlignOperands = false; 3957 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 3958 " bbbbbbbbbbbbbbbbbbbbbb);", 3959 Style); 3960 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 3961 Style.AlignOperands = true; 3962 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 3963 " bbbbbbbbbbbbbbbbbbbbbb);", 3964 Style); 3965 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 3966 Style.AlignOperands = false; 3967 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 3968 " bbbbbbbbbbbbbbbbbbbbbb);", 3969 Style); 3970 } 3971 3972 TEST_F(FormatTest, BreaksConditionalExpressions) { 3973 verifyFormat( 3974 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3975 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3976 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3977 verifyFormat( 3978 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 3979 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3980 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3981 verifyFormat( 3982 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3983 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3984 verifyFormat( 3985 "aaaa(aaaaaaaaa, aaaaaaaaa,\n" 3986 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3987 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3988 verifyFormat( 3989 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" 3990 " : aaaaaaaaaaaaa);"); 3991 verifyFormat( 3992 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3993 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3994 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3995 " aaaaaaaaaaaaa);"); 3996 verifyFormat( 3997 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3998 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3999 " aaaaaaaaaaaaa);"); 4000 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4001 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4002 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4003 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4004 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4005 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4006 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4007 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4008 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4009 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4010 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4011 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4012 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4013 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4014 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4015 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4016 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4017 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4018 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4019 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4020 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 4021 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4022 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4023 " : aaaaaaaaaaaaaaaa;"); 4024 verifyFormat( 4025 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4026 " ? aaaaaaaaaaaaaaa\n" 4027 " : aaaaaaaaaaaaaaa;"); 4028 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 4029 " aaaaaaaaa\n" 4030 " ? b\n" 4031 " : c);"); 4032 verifyFormat("return aaaa == bbbb\n" 4033 " // comment\n" 4034 " ? aaaa\n" 4035 " : bbbb;"); 4036 verifyFormat("unsigned Indent =\n" 4037 " format(TheLine.First,\n" 4038 " IndentForLevel[TheLine.Level] >= 0\n" 4039 " ? IndentForLevel[TheLine.Level]\n" 4040 " : TheLine * 2,\n" 4041 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 4042 getLLVMStyleWithColumns(60)); 4043 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 4044 " ? aaaaaaaaaaaaaaa\n" 4045 " : bbbbbbbbbbbbbbb //\n" 4046 " ? ccccccccccccccc\n" 4047 " : ddddddddddddddd;"); 4048 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 4049 " ? aaaaaaaaaaaaaaa\n" 4050 " : (bbbbbbbbbbbbbbb //\n" 4051 " ? ccccccccccccccc\n" 4052 " : ddddddddddddddd);"); 4053 verifyFormat( 4054 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4055 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4056 " aaaaaaaaaaaaaaaaaaaaa +\n" 4057 " aaaaaaaaaaaaaaaaaaaaa\n" 4058 " : aaaaaaaaaa;"); 4059 verifyFormat( 4060 "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4061 " : aaaaaaaaaaaaaaaaaaaaaa\n" 4062 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4063 4064 FormatStyle NoBinPacking = getLLVMStyle(); 4065 NoBinPacking.BinPackArguments = false; 4066 verifyFormat( 4067 "void f() {\n" 4068 " g(aaa,\n" 4069 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 4070 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4071 " ? aaaaaaaaaaaaaaa\n" 4072 " : aaaaaaaaaaaaaaa);\n" 4073 "}", 4074 NoBinPacking); 4075 verifyFormat( 4076 "void f() {\n" 4077 " g(aaa,\n" 4078 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 4079 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4080 " ?: aaaaaaaaaaaaaaa);\n" 4081 "}", 4082 NoBinPacking); 4083 4084 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n" 4085 " // comment.\n" 4086 " ccccccccccccccccccccccccccccccccccccccc\n" 4087 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4088 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);"); 4089 4090 // Assignments in conditional expressions. Apparently not uncommon :-(. 4091 verifyFormat("return a != b\n" 4092 " // comment\n" 4093 " ? a = b\n" 4094 " : a = b;"); 4095 verifyFormat("return a != b\n" 4096 " // comment\n" 4097 " ? a = a != b\n" 4098 " // comment\n" 4099 " ? a = b\n" 4100 " : a\n" 4101 " : a;\n"); 4102 verifyFormat("return a != b\n" 4103 " // comment\n" 4104 " ? a\n" 4105 " : a = a != b\n" 4106 " // comment\n" 4107 " ? a = b\n" 4108 " : a;"); 4109 } 4110 4111 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) { 4112 FormatStyle Style = getLLVMStyle(); 4113 Style.BreakBeforeTernaryOperators = false; 4114 Style.ColumnLimit = 70; 4115 verifyFormat( 4116 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4117 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4118 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4119 Style); 4120 verifyFormat( 4121 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 4122 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4123 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4124 Style); 4125 verifyFormat( 4126 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4127 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4128 Style); 4129 verifyFormat( 4130 "aaaa(aaaaaaaa, aaaaaaaaaa,\n" 4131 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4132 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4133 Style); 4134 verifyFormat( 4135 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n" 4136 " aaaaaaaaaaaaa);", 4137 Style); 4138 verifyFormat( 4139 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4140 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4141 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4142 " aaaaaaaaaaaaa);", 4143 Style); 4144 verifyFormat( 4145 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4146 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4147 " aaaaaaaaaaaaa);", 4148 Style); 4149 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4150 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4151 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 4152 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4153 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4154 Style); 4155 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4156 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4157 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4158 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 4159 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4160 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4161 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4162 Style); 4163 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4164 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n" 4165 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4166 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4167 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4168 Style); 4169 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4170 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4171 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4172 Style); 4173 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 4174 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4175 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4176 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4177 Style); 4178 verifyFormat( 4179 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4180 " aaaaaaaaaaaaaaa :\n" 4181 " aaaaaaaaaaaaaaa;", 4182 Style); 4183 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 4184 " aaaaaaaaa ?\n" 4185 " b :\n" 4186 " c);", 4187 Style); 4188 verifyFormat("unsigned Indent =\n" 4189 " format(TheLine.First,\n" 4190 " IndentForLevel[TheLine.Level] >= 0 ?\n" 4191 " IndentForLevel[TheLine.Level] :\n" 4192 " TheLine * 2,\n" 4193 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 4194 Style); 4195 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 4196 " aaaaaaaaaaaaaaa :\n" 4197 " bbbbbbbbbbbbbbb ? //\n" 4198 " ccccccccccccccc :\n" 4199 " ddddddddddddddd;", 4200 Style); 4201 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 4202 " aaaaaaaaaaaaaaa :\n" 4203 " (bbbbbbbbbbbbbbb ? //\n" 4204 " ccccccccccccccc :\n" 4205 " ddddddddddddddd);", 4206 Style); 4207 verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4208 " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n" 4209 " ccccccccccccccccccccccccccc;", 4210 Style); 4211 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4212 " aaaaa :\n" 4213 " bbbbbbbbbbbbbbb + cccccccccccccccc;", 4214 Style); 4215 } 4216 4217 TEST_F(FormatTest, DeclarationsOfMultipleVariables) { 4218 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" 4219 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); 4220 verifyFormat("bool a = true, b = false;"); 4221 4222 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" 4223 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" 4224 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" 4225 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); 4226 verifyFormat( 4227 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 4228 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n" 4229 " d = e && f;"); 4230 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n" 4231 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;"); 4232 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 4233 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;"); 4234 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n" 4235 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;"); 4236 4237 FormatStyle Style = getGoogleStyle(); 4238 Style.PointerAlignment = FormatStyle::PAS_Left; 4239 Style.DerivePointerAlignment = false; 4240 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4241 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" 4242 " *b = bbbbbbbbbbbbbbbbbbb;", 4243 Style); 4244 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 4245 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;", 4246 Style); 4247 verifyFormat("vector<int*> a, b;", Style); 4248 verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style); 4249 } 4250 4251 TEST_F(FormatTest, ConditionalExpressionsInBrackets) { 4252 verifyFormat("arr[foo ? bar : baz];"); 4253 verifyFormat("f()[foo ? bar : baz];"); 4254 verifyFormat("(a + b)[foo ? bar : baz];"); 4255 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); 4256 } 4257 4258 TEST_F(FormatTest, AlignsStringLiterals) { 4259 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" 4260 " \"short literal\");"); 4261 verifyFormat( 4262 "looooooooooooooooooooooooongFunction(\n" 4263 " \"short literal\"\n" 4264 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); 4265 verifyFormat("someFunction(\"Always break between multi-line\"\n" 4266 " \" string literals\",\n" 4267 " and, other, parameters);"); 4268 EXPECT_EQ("fun + \"1243\" /* comment */\n" 4269 " \"5678\";", 4270 format("fun + \"1243\" /* comment */\n" 4271 " \"5678\";", 4272 getLLVMStyleWithColumns(28))); 4273 EXPECT_EQ( 4274 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 4275 " \"aaaaaaaaaaaaaaaaaaaaa\"\n" 4276 " \"aaaaaaaaaaaaaaaa\";", 4277 format("aaaaaa =" 4278 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " 4279 "aaaaaaaaaaaaaaaaaaaaa\" " 4280 "\"aaaaaaaaaaaaaaaa\";")); 4281 verifyFormat("a = a + \"a\"\n" 4282 " \"a\"\n" 4283 " \"a\";"); 4284 verifyFormat("f(\"a\", \"b\"\n" 4285 " \"c\");"); 4286 4287 verifyFormat( 4288 "#define LL_FORMAT \"ll\"\n" 4289 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n" 4290 " \"d, ddddddddd: %\" LL_FORMAT \"d\");"); 4291 4292 verifyFormat("#define A(X) \\\n" 4293 " \"aaaaa\" #X \"bbbbbb\" \\\n" 4294 " \"ccccc\"", 4295 getLLVMStyleWithColumns(23)); 4296 verifyFormat("#define A \"def\"\n" 4297 "f(\"abc\" A \"ghi\"\n" 4298 " \"jkl\");"); 4299 4300 verifyFormat("f(L\"a\"\n" 4301 " L\"b\");"); 4302 verifyFormat("#define A(X) \\\n" 4303 " L\"aaaaa\" #X L\"bbbbbb\" \\\n" 4304 " L\"ccccc\"", 4305 getLLVMStyleWithColumns(25)); 4306 4307 verifyFormat("f(@\"a\"\n" 4308 " @\"b\");"); 4309 verifyFormat("NSString s = @\"a\"\n" 4310 " @\"b\"\n" 4311 " @\"c\";"); 4312 verifyFormat("NSString s = @\"a\"\n" 4313 " \"b\"\n" 4314 " \"c\";"); 4315 } 4316 4317 TEST_F(FormatTest, ReturnTypeBreakingStyle) { 4318 FormatStyle Style = getLLVMStyle(); 4319 // No declarations or definitions should be moved to own line. 4320 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None; 4321 verifyFormat("class A {\n" 4322 " int f() { return 1; }\n" 4323 " int g();\n" 4324 "};\n" 4325 "int f() { return 1; }\n" 4326 "int g();\n", 4327 Style); 4328 4329 // All declarations and definitions should have the return type moved to its 4330 // own 4331 // line. 4332 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 4333 verifyFormat("class E {\n" 4334 " int\n" 4335 " f() {\n" 4336 " return 1;\n" 4337 " }\n" 4338 " int\n" 4339 " g();\n" 4340 "};\n" 4341 "int\n" 4342 "f() {\n" 4343 " return 1;\n" 4344 "}\n" 4345 "int\n" 4346 "g();\n", 4347 Style); 4348 4349 // Top-level definitions, and no kinds of declarations should have the 4350 // return type moved to its own line. 4351 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions; 4352 verifyFormat("class B {\n" 4353 " int f() { return 1; }\n" 4354 " int g();\n" 4355 "};\n" 4356 "int\n" 4357 "f() {\n" 4358 " return 1;\n" 4359 "}\n" 4360 "int g();\n", 4361 Style); 4362 4363 // Top-level definitions and declarations should have the return type moved 4364 // to its own line. 4365 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel; 4366 verifyFormat("class C {\n" 4367 " int f() { return 1; }\n" 4368 " int g();\n" 4369 "};\n" 4370 "int\n" 4371 "f() {\n" 4372 " return 1;\n" 4373 "}\n" 4374 "int\n" 4375 "g();\n", 4376 Style); 4377 4378 // All definitions should have the return type moved to its own line, but no 4379 // kinds of declarations. 4380 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 4381 verifyFormat("class D {\n" 4382 " int\n" 4383 " f() {\n" 4384 " return 1;\n" 4385 " }\n" 4386 " int g();\n" 4387 "};\n" 4388 "int\n" 4389 "f() {\n" 4390 " return 1;\n" 4391 "}\n" 4392 "int g();\n", 4393 Style); 4394 verifyFormat("const char *\n" 4395 "f(void) {\n" // Break here. 4396 " return \"\";\n" 4397 "}\n" 4398 "const char *bar(void);\n", // No break here. 4399 Style); 4400 verifyFormat("template <class T>\n" 4401 "T *\n" 4402 "f(T &c) {\n" // Break here. 4403 " return NULL;\n" 4404 "}\n" 4405 "template <class T> T *f(T &c);\n", // No break here. 4406 Style); 4407 verifyFormat("class C {\n" 4408 " int\n" 4409 " operator+() {\n" 4410 " return 1;\n" 4411 " }\n" 4412 " int\n" 4413 " operator()() {\n" 4414 " return 1;\n" 4415 " }\n" 4416 "};\n", 4417 Style); 4418 verifyFormat("void\n" 4419 "A::operator()() {}\n" 4420 "void\n" 4421 "A::operator>>() {}\n" 4422 "void\n" 4423 "A::operator+() {}\n", 4424 Style); 4425 verifyFormat("void *operator new(std::size_t s);", // No break here. 4426 Style); 4427 verifyFormat("void *\n" 4428 "operator new(std::size_t s) {}", 4429 Style); 4430 verifyFormat("void *\n" 4431 "operator delete[](void *ptr) {}", 4432 Style); 4433 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 4434 verifyFormat("const char *\n" 4435 "f(void)\n" // Break here. 4436 "{\n" 4437 " return \"\";\n" 4438 "}\n" 4439 "const char *bar(void);\n", // No break here. 4440 Style); 4441 verifyFormat("template <class T>\n" 4442 "T *\n" // Problem here: no line break 4443 "f(T &c)\n" // Break here. 4444 "{\n" 4445 " return NULL;\n" 4446 "}\n" 4447 "template <class T> T *f(T &c);\n", // No break here. 4448 Style); 4449 } 4450 4451 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) { 4452 FormatStyle NoBreak = getLLVMStyle(); 4453 NoBreak.AlwaysBreakBeforeMultilineStrings = false; 4454 FormatStyle Break = getLLVMStyle(); 4455 Break.AlwaysBreakBeforeMultilineStrings = true; 4456 verifyFormat("aaaa = \"bbbb\"\n" 4457 " \"cccc\";", 4458 NoBreak); 4459 verifyFormat("aaaa =\n" 4460 " \"bbbb\"\n" 4461 " \"cccc\";", 4462 Break); 4463 verifyFormat("aaaa(\"bbbb\"\n" 4464 " \"cccc\");", 4465 NoBreak); 4466 verifyFormat("aaaa(\n" 4467 " \"bbbb\"\n" 4468 " \"cccc\");", 4469 Break); 4470 verifyFormat("aaaa(qqq, \"bbbb\"\n" 4471 " \"cccc\");", 4472 NoBreak); 4473 verifyFormat("aaaa(qqq,\n" 4474 " \"bbbb\"\n" 4475 " \"cccc\");", 4476 Break); 4477 verifyFormat("aaaa(qqq,\n" 4478 " L\"bbbb\"\n" 4479 " L\"cccc\");", 4480 Break); 4481 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n" 4482 " \"bbbb\"));", 4483 Break); 4484 verifyFormat("string s = someFunction(\n" 4485 " \"abc\"\n" 4486 " \"abc\");", 4487 Break); 4488 4489 // As we break before unary operators, breaking right after them is bad. 4490 verifyFormat("string foo = abc ? \"x\"\n" 4491 " \"blah blah blah blah blah blah\"\n" 4492 " : \"y\";", 4493 Break); 4494 4495 // Don't break if there is no column gain. 4496 verifyFormat("f(\"aaaa\"\n" 4497 " \"bbbb\");", 4498 Break); 4499 4500 // Treat literals with escaped newlines like multi-line string literals. 4501 EXPECT_EQ("x = \"a\\\n" 4502 "b\\\n" 4503 "c\";", 4504 format("x = \"a\\\n" 4505 "b\\\n" 4506 "c\";", 4507 NoBreak)); 4508 EXPECT_EQ("xxxx =\n" 4509 " \"a\\\n" 4510 "b\\\n" 4511 "c\";", 4512 format("xxxx = \"a\\\n" 4513 "b\\\n" 4514 "c\";", 4515 Break)); 4516 4517 EXPECT_EQ("NSString *const kString =\n" 4518 " @\"aaaa\"\n" 4519 " @\"bbbb\";", 4520 format("NSString *const kString = @\"aaaa\"\n" 4521 "@\"bbbb\";", 4522 Break)); 4523 4524 Break.ColumnLimit = 0; 4525 verifyFormat("const char *hello = \"hello llvm\";", Break); 4526 } 4527 4528 TEST_F(FormatTest, AlignsPipes) { 4529 verifyFormat( 4530 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4531 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4532 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4533 verifyFormat( 4534 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 4535 " << aaaaaaaaaaaaaaaaaaaa;"); 4536 verifyFormat( 4537 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4538 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4539 verifyFormat( 4540 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4541 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4542 verifyFormat( 4543 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 4544 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 4545 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 4546 verifyFormat( 4547 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4548 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4549 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4550 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4551 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4552 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4553 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 4554 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n" 4555 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);"); 4556 verifyFormat( 4557 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4558 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4559 verifyFormat( 4560 "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n" 4561 " aaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4562 4563 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n" 4564 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();"); 4565 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4566 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4567 " aaaaaaaaaaaaaaaaaaaaa)\n" 4568 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4569 verifyFormat("LOG_IF(aaa == //\n" 4570 " bbb)\n" 4571 " << a << b;"); 4572 4573 // But sometimes, breaking before the first "<<" is desirable. 4574 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 4575 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);"); 4576 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n" 4577 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4578 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4579 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n" 4580 " << BEF << IsTemplate << Description << E->getType();"); 4581 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 4582 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4583 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4584 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 4585 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4586 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4587 " << aaa;"); 4588 4589 verifyFormat( 4590 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4591 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4592 4593 // Incomplete string literal. 4594 EXPECT_EQ("llvm::errs() << \"\n" 4595 " << a;", 4596 format("llvm::errs() << \"\n<<a;")); 4597 4598 verifyFormat("void f() {\n" 4599 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n" 4600 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n" 4601 "}"); 4602 4603 // Handle 'endl'. 4604 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n" 4605 " << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 4606 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 4607 4608 // Handle '\n'. 4609 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n" 4610 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 4611 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n" 4612 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';"); 4613 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n" 4614 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";"); 4615 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 4616 } 4617 4618 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) { 4619 verifyFormat("return out << \"somepacket = {\\n\"\n" 4620 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" 4621 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" 4622 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" 4623 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" 4624 " << \"}\";"); 4625 4626 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 4627 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 4628 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;"); 4629 verifyFormat( 4630 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" 4631 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" 4632 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" 4633 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" 4634 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); 4635 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" 4636 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 4637 verifyFormat( 4638 "void f() {\n" 4639 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n" 4640 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 4641 "}"); 4642 4643 // Breaking before the first "<<" is generally not desirable. 4644 verifyFormat( 4645 "llvm::errs()\n" 4646 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4647 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4648 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4649 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4650 getLLVMStyleWithColumns(70)); 4651 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n" 4652 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4653 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 4654 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4655 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 4656 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4657 getLLVMStyleWithColumns(70)); 4658 4659 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 4660 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 4661 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;"); 4662 verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 4663 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 4664 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);"); 4665 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n" 4666 " (aaaa + aaaa);", 4667 getLLVMStyleWithColumns(40)); 4668 verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n" 4669 " (aaaaaaa + aaaaa));", 4670 getLLVMStyleWithColumns(40)); 4671 verifyFormat( 4672 "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n" 4673 " SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n" 4674 " bbbbbbbbbbbbbbbbbbbbbbb);"); 4675 } 4676 4677 TEST_F(FormatTest, UnderstandsEquals) { 4678 verifyFormat( 4679 "aaaaaaaaaaaaaaaaa =\n" 4680 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4681 verifyFormat( 4682 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 4683 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 4684 verifyFormat( 4685 "if (a) {\n" 4686 " f();\n" 4687 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 4688 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 4689 "}"); 4690 4691 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 4692 " 100000000 + 10000000) {\n}"); 4693 } 4694 4695 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 4696 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 4697 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 4698 4699 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 4700 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 4701 4702 verifyFormat( 4703 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 4704 " Parameter2);"); 4705 4706 verifyFormat( 4707 "ShortObject->shortFunction(\n" 4708 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 4709 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 4710 4711 verifyFormat("loooooooooooooongFunction(\n" 4712 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 4713 4714 verifyFormat( 4715 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 4716 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 4717 4718 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 4719 " .WillRepeatedly(Return(SomeValue));"); 4720 verifyFormat("void f() {\n" 4721 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 4722 " .Times(2)\n" 4723 " .WillRepeatedly(Return(SomeValue));\n" 4724 "}"); 4725 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n" 4726 " ccccccccccccccccccccccc);"); 4727 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4728 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4729 " .aaaaa(aaaaa),\n" 4730 " aaaaaaaaaaaaaaaaaaaaa);"); 4731 verifyFormat("void f() {\n" 4732 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4733 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n" 4734 "}"); 4735 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4736 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4737 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4738 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4739 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4740 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4741 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4742 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4743 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n" 4744 "}"); 4745 4746 // Here, it is not necessary to wrap at "." or "->". 4747 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 4748 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 4749 verifyFormat( 4750 "aaaaaaaaaaa->aaaaaaaaa(\n" 4751 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4752 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 4753 4754 verifyFormat( 4755 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4756 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); 4757 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n" 4758 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 4759 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" 4760 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 4761 4762 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4763 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4764 " .a();"); 4765 4766 FormatStyle NoBinPacking = getLLVMStyle(); 4767 NoBinPacking.BinPackParameters = false; 4768 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 4769 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 4770 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" 4771 " aaaaaaaaaaaaaaaaaaa,\n" 4772 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4773 NoBinPacking); 4774 4775 // If there is a subsequent call, change to hanging indentation. 4776 verifyFormat( 4777 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4778 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n" 4779 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4780 verifyFormat( 4781 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4782 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));"); 4783 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4784 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4785 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4786 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4787 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4788 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 4789 } 4790 4791 TEST_F(FormatTest, WrapsTemplateDeclarations) { 4792 verifyFormat("template <typename T>\n" 4793 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 4794 verifyFormat("template <typename T>\n" 4795 "// T should be one of {A, B}.\n" 4796 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 4797 verifyFormat( 4798 "template <typename T>\n" 4799 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); 4800 verifyFormat("template <typename T>\n" 4801 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 4802 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 4803 verifyFormat( 4804 "template <typename T>\n" 4805 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 4806 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 4807 verifyFormat( 4808 "template <typename T>\n" 4809 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 4810 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 4811 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4812 verifyFormat("template <typename T>\n" 4813 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4814 " int aaaaaaaaaaaaaaaaaaaaaa);"); 4815 verifyFormat( 4816 "template <typename T1, typename T2 = char, typename T3 = char,\n" 4817 " typename T4 = char>\n" 4818 "void f();"); 4819 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n" 4820 " template <typename> class cccccccccccccccccccccc,\n" 4821 " typename ddddddddddddd>\n" 4822 "class C {};"); 4823 verifyFormat( 4824 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 4825 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4826 4827 verifyFormat("void f() {\n" 4828 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 4829 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n" 4830 "}"); 4831 4832 verifyFormat("template <typename T> class C {};"); 4833 verifyFormat("template <typename T> void f();"); 4834 verifyFormat("template <typename T> void f() {}"); 4835 verifyFormat( 4836 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 4837 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4838 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n" 4839 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 4840 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4841 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n" 4842 " bbbbbbbbbbbbbbbbbbbbbbbb);", 4843 getLLVMStyleWithColumns(72)); 4844 EXPECT_EQ("static_cast<A< //\n" 4845 " B> *>(\n" 4846 "\n" 4847 ");", 4848 format("static_cast<A<//\n" 4849 " B>*>(\n" 4850 "\n" 4851 " );")); 4852 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4853 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);"); 4854 4855 FormatStyle AlwaysBreak = getLLVMStyle(); 4856 AlwaysBreak.AlwaysBreakTemplateDeclarations = true; 4857 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak); 4858 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak); 4859 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak); 4860 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4861 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 4862 " ccccccccccccccccccccccccccccccccccccccccccccccc);"); 4863 verifyFormat("template <template <typename> class Fooooooo,\n" 4864 " template <typename> class Baaaaaaar>\n" 4865 "struct C {};", 4866 AlwaysBreak); 4867 verifyFormat("template <typename T> // T can be A, B or C.\n" 4868 "struct C {};", 4869 AlwaysBreak); 4870 verifyFormat("template <enum E> class A {\n" 4871 "public:\n" 4872 " E *f();\n" 4873 "};"); 4874 } 4875 4876 TEST_F(FormatTest, WrapsTemplateParameters) { 4877 FormatStyle Style = getLLVMStyle(); 4878 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4879 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 4880 verifyFormat( 4881 "template <typename... a> struct q {};\n" 4882 "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 4883 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 4884 " y;", 4885 Style); 4886 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4887 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 4888 verifyFormat( 4889 "template <typename... a> struct r {};\n" 4890 "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 4891 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 4892 " y;", 4893 Style); 4894 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 4895 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 4896 verifyFormat( 4897 "template <typename... a> struct s {};\n" 4898 "extern s<\n" 4899 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 4900 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n" 4901 " y;", 4902 Style); 4903 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 4904 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 4905 verifyFormat( 4906 "template <typename... a> struct t {};\n" 4907 "extern t<\n" 4908 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 4909 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n" 4910 " y;", 4911 Style); 4912 } 4913 4914 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 4915 verifyFormat( 4916 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 4917 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4918 verifyFormat( 4919 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 4920 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4921 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 4922 4923 // FIXME: Should we have the extra indent after the second break? 4924 verifyFormat( 4925 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 4926 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 4927 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4928 4929 verifyFormat( 4930 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 4931 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 4932 4933 // Breaking at nested name specifiers is generally not desirable. 4934 verifyFormat( 4935 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4936 " aaaaaaaaaaaaaaaaaaaaaaa);"); 4937 4938 verifyFormat( 4939 "aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n" 4940 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 4941 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4942 " aaaaaaaaaaaaaaaaaaaaa);", 4943 getLLVMStyleWithColumns(74)); 4944 4945 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 4946 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4947 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4948 } 4949 4950 TEST_F(FormatTest, UnderstandsTemplateParameters) { 4951 verifyFormat("A<int> a;"); 4952 verifyFormat("A<A<A<int>>> a;"); 4953 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 4954 verifyFormat("bool x = a < 1 || 2 > a;"); 4955 verifyFormat("bool x = 5 < f<int>();"); 4956 verifyFormat("bool x = f<int>() > 5;"); 4957 verifyFormat("bool x = 5 < a<int>::x;"); 4958 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 4959 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 4960 4961 verifyGoogleFormat("A<A<int>> a;"); 4962 verifyGoogleFormat("A<A<A<int>>> a;"); 4963 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 4964 verifyGoogleFormat("A<A<int> > a;"); 4965 verifyGoogleFormat("A<A<A<int> > > a;"); 4966 verifyGoogleFormat("A<A<A<A<int> > > > a;"); 4967 verifyGoogleFormat("A<::A<int>> a;"); 4968 verifyGoogleFormat("A<::A> a;"); 4969 verifyGoogleFormat("A< ::A> a;"); 4970 verifyGoogleFormat("A< ::A<int> > a;"); 4971 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); 4972 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); 4973 EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle())); 4974 EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle())); 4975 EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };", 4976 format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle())); 4977 4978 verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp)); 4979 4980 verifyFormat("test >> a >> b;"); 4981 verifyFormat("test << a >> b;"); 4982 4983 verifyFormat("f<int>();"); 4984 verifyFormat("template <typename T> void f() {}"); 4985 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;"); 4986 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : " 4987 "sizeof(char)>::type>;"); 4988 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};"); 4989 verifyFormat("f(a.operator()<A>());"); 4990 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4991 " .template operator()<A>());", 4992 getLLVMStyleWithColumns(35)); 4993 4994 // Not template parameters. 4995 verifyFormat("return a < b && c > d;"); 4996 verifyFormat("void f() {\n" 4997 " while (a < b && c > d) {\n" 4998 " }\n" 4999 "}"); 5000 verifyFormat("template <typename... Types>\n" 5001 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}"); 5002 5003 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5004 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);", 5005 getLLVMStyleWithColumns(60)); 5006 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");"); 5007 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}"); 5008 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <"); 5009 } 5010 5011 TEST_F(FormatTest, BitshiftOperatorWidth) { 5012 EXPECT_EQ("int a = 1 << 2; /* foo\n" 5013 " bar */", 5014 format("int a=1<<2; /* foo\n" 5015 " bar */")); 5016 5017 EXPECT_EQ("int b = 256 >> 1; /* foo\n" 5018 " bar */", 5019 format("int b =256>>1 ; /* foo\n" 5020 " bar */")); 5021 } 5022 5023 TEST_F(FormatTest, UnderstandsBinaryOperators) { 5024 verifyFormat("COMPARE(a, ==, b);"); 5025 verifyFormat("auto s = sizeof...(Ts) - 1;"); 5026 } 5027 5028 TEST_F(FormatTest, UnderstandsPointersToMembers) { 5029 verifyFormat("int A::*x;"); 5030 verifyFormat("int (S::*func)(void *);"); 5031 verifyFormat("void f() { int (S::*func)(void *); }"); 5032 verifyFormat("typedef bool *(Class::*Member)() const;"); 5033 verifyFormat("void f() {\n" 5034 " (a->*f)();\n" 5035 " a->*x;\n" 5036 " (a.*f)();\n" 5037 " ((*a).*f)();\n" 5038 " a.*x;\n" 5039 "}"); 5040 verifyFormat("void f() {\n" 5041 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 5042 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n" 5043 "}"); 5044 verifyFormat( 5045 "(aaaaaaaaaa->*bbbbbbb)(\n" 5046 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 5047 FormatStyle Style = getLLVMStyle(); 5048 Style.PointerAlignment = FormatStyle::PAS_Left; 5049 verifyFormat("typedef bool* (Class::*Member)() const;", Style); 5050 } 5051 5052 TEST_F(FormatTest, UnderstandsUnaryOperators) { 5053 verifyFormat("int a = -2;"); 5054 verifyFormat("f(-1, -2, -3);"); 5055 verifyFormat("a[-1] = 5;"); 5056 verifyFormat("int a = 5 + -2;"); 5057 verifyFormat("if (i == -1) {\n}"); 5058 verifyFormat("if (i != -1) {\n}"); 5059 verifyFormat("if (i > -1) {\n}"); 5060 verifyFormat("if (i < -1) {\n}"); 5061 verifyFormat("++(a->f());"); 5062 verifyFormat("--(a->f());"); 5063 verifyFormat("(a->f())++;"); 5064 verifyFormat("a[42]++;"); 5065 verifyFormat("if (!(a->f())) {\n}"); 5066 5067 verifyFormat("a-- > b;"); 5068 verifyFormat("b ? -a : c;"); 5069 verifyFormat("n * sizeof char16;"); 5070 verifyFormat("n * alignof char16;", getGoogleStyle()); 5071 verifyFormat("sizeof(char);"); 5072 verifyFormat("alignof(char);", getGoogleStyle()); 5073 5074 verifyFormat("return -1;"); 5075 verifyFormat("switch (a) {\n" 5076 "case -1:\n" 5077 " break;\n" 5078 "}"); 5079 verifyFormat("#define X -1"); 5080 verifyFormat("#define X -kConstant"); 5081 5082 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};"); 5083 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};"); 5084 5085 verifyFormat("int a = /* confusing comment */ -1;"); 5086 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 5087 verifyFormat("int a = i /* confusing comment */++;"); 5088 } 5089 5090 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) { 5091 verifyFormat("if (!aaaaaaaaaa( // break\n" 5092 " aaaaa)) {\n" 5093 "}"); 5094 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n" 5095 " aaaaa));"); 5096 verifyFormat("*aaa = aaaaaaa( // break\n" 5097 " bbbbbb);"); 5098 } 5099 5100 TEST_F(FormatTest, UnderstandsOverloadedOperators) { 5101 verifyFormat("bool operator<();"); 5102 verifyFormat("bool operator>();"); 5103 verifyFormat("bool operator=();"); 5104 verifyFormat("bool operator==();"); 5105 verifyFormat("bool operator!=();"); 5106 verifyFormat("int operator+();"); 5107 verifyFormat("int operator++();"); 5108 verifyFormat("bool operator,();"); 5109 verifyFormat("bool operator();"); 5110 verifyFormat("bool operator()();"); 5111 verifyFormat("bool operator[]();"); 5112 verifyFormat("operator bool();"); 5113 verifyFormat("operator int();"); 5114 verifyFormat("operator void *();"); 5115 verifyFormat("operator SomeType<int>();"); 5116 verifyFormat("operator SomeType<int, int>();"); 5117 verifyFormat("operator SomeType<SomeType<int>>();"); 5118 verifyFormat("void *operator new(std::size_t size);"); 5119 verifyFormat("void *operator new[](std::size_t size);"); 5120 verifyFormat("void operator delete(void *ptr);"); 5121 verifyFormat("void operator delete[](void *ptr);"); 5122 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n" 5123 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);"); 5124 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n" 5125 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;"); 5126 5127 verifyFormat( 5128 "ostream &operator<<(ostream &OutputStream,\n" 5129 " SomeReallyLongType WithSomeReallyLongValue);"); 5130 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n" 5131 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n" 5132 " return left.group < right.group;\n" 5133 "}"); 5134 verifyFormat("SomeType &operator=(const SomeType &S);"); 5135 verifyFormat("f.template operator()<int>();"); 5136 5137 verifyGoogleFormat("operator void*();"); 5138 verifyGoogleFormat("operator SomeType<SomeType<int>>();"); 5139 verifyGoogleFormat("operator ::A();"); 5140 5141 verifyFormat("using A::operator+;"); 5142 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n" 5143 "int i;"); 5144 } 5145 5146 TEST_F(FormatTest, UnderstandsFunctionRefQualification) { 5147 verifyFormat("Deleted &operator=(const Deleted &) & = default;"); 5148 verifyFormat("Deleted &operator=(const Deleted &) && = delete;"); 5149 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;"); 5150 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;"); 5151 verifyFormat("Deleted &operator=(const Deleted &) &;"); 5152 verifyFormat("Deleted &operator=(const Deleted &) &&;"); 5153 verifyFormat("SomeType MemberFunction(const Deleted &) &;"); 5154 verifyFormat("SomeType MemberFunction(const Deleted &) &&;"); 5155 verifyFormat("SomeType MemberFunction(const Deleted &) && {}"); 5156 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}"); 5157 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}"); 5158 verifyFormat("SomeType MemberFunction(const Deleted &) const &;"); 5159 verifyFormat("template <typename T>\n" 5160 "void F(T) && = delete;", 5161 getGoogleStyle()); 5162 5163 FormatStyle AlignLeft = getLLVMStyle(); 5164 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 5165 verifyFormat("void A::b() && {}", AlignLeft); 5166 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft); 5167 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;", 5168 AlignLeft); 5169 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft); 5170 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft); 5171 verifyFormat("auto Function(T t) & -> void {}", AlignLeft); 5172 verifyFormat("auto Function(T... t) & -> void {}", AlignLeft); 5173 verifyFormat("auto Function(T) & -> void {}", AlignLeft); 5174 verifyFormat("auto Function(T) & -> void;", AlignLeft); 5175 verifyFormat("SomeType MemberFunction(const Deleted&) const &;", AlignLeft); 5176 5177 FormatStyle Spaces = getLLVMStyle(); 5178 Spaces.SpacesInCStyleCastParentheses = true; 5179 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces); 5180 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces); 5181 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces); 5182 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces); 5183 5184 Spaces.SpacesInCStyleCastParentheses = false; 5185 Spaces.SpacesInParentheses = true; 5186 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces); 5187 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces); 5188 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces); 5189 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces); 5190 } 5191 5192 TEST_F(FormatTest, UnderstandsNewAndDelete) { 5193 verifyFormat("void f() {\n" 5194 " A *a = new A;\n" 5195 " A *a = new (placement) A;\n" 5196 " delete a;\n" 5197 " delete (A *)a;\n" 5198 "}"); 5199 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 5200 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 5201 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5202 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 5203 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 5204 verifyFormat("delete[] h->p;"); 5205 } 5206 5207 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 5208 verifyFormat("int *f(int *a) {}"); 5209 verifyFormat("int main(int argc, char **argv) {}"); 5210 verifyFormat("Test::Test(int b) : a(b * b) {}"); 5211 verifyIndependentOfContext("f(a, *a);"); 5212 verifyFormat("void g() { f(*a); }"); 5213 verifyIndependentOfContext("int a = b * 10;"); 5214 verifyIndependentOfContext("int a = 10 * b;"); 5215 verifyIndependentOfContext("int a = b * c;"); 5216 verifyIndependentOfContext("int a += b * c;"); 5217 verifyIndependentOfContext("int a -= b * c;"); 5218 verifyIndependentOfContext("int a *= b * c;"); 5219 verifyIndependentOfContext("int a /= b * c;"); 5220 verifyIndependentOfContext("int a = *b;"); 5221 verifyIndependentOfContext("int a = *b * c;"); 5222 verifyIndependentOfContext("int a = b * *c;"); 5223 verifyIndependentOfContext("int a = b * (10);"); 5224 verifyIndependentOfContext("S << b * (10);"); 5225 verifyIndependentOfContext("return 10 * b;"); 5226 verifyIndependentOfContext("return *b * *c;"); 5227 verifyIndependentOfContext("return a & ~b;"); 5228 verifyIndependentOfContext("f(b ? *c : *d);"); 5229 verifyIndependentOfContext("int a = b ? *c : *d;"); 5230 verifyIndependentOfContext("*b = a;"); 5231 verifyIndependentOfContext("a * ~b;"); 5232 verifyIndependentOfContext("a * !b;"); 5233 verifyIndependentOfContext("a * +b;"); 5234 verifyIndependentOfContext("a * -b;"); 5235 verifyIndependentOfContext("a * ++b;"); 5236 verifyIndependentOfContext("a * --b;"); 5237 verifyIndependentOfContext("a[4] * b;"); 5238 verifyIndependentOfContext("a[a * a] = 1;"); 5239 verifyIndependentOfContext("f() * b;"); 5240 verifyIndependentOfContext("a * [self dostuff];"); 5241 verifyIndependentOfContext("int x = a * (a + b);"); 5242 verifyIndependentOfContext("(a *)(a + b);"); 5243 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;"); 5244 verifyIndependentOfContext("int *pa = (int *)&a;"); 5245 verifyIndependentOfContext("return sizeof(int **);"); 5246 verifyIndependentOfContext("return sizeof(int ******);"); 5247 verifyIndependentOfContext("return (int **&)a;"); 5248 verifyIndependentOfContext("f((*PointerToArray)[10]);"); 5249 verifyFormat("void f(Type (*parameter)[10]) {}"); 5250 verifyFormat("void f(Type (¶meter)[10]) {}"); 5251 verifyGoogleFormat("return sizeof(int**);"); 5252 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); 5253 verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); 5254 verifyFormat("auto a = [](int **&, int ***) {};"); 5255 verifyFormat("auto PointerBinding = [](const char *S) {};"); 5256 verifyFormat("typedef typeof(int(int, int)) *MyFunc;"); 5257 verifyFormat("[](const decltype(*a) &value) {}"); 5258 verifyFormat("decltype(a * b) F();"); 5259 verifyFormat("#define MACRO() [](A *a) { return 1; }"); 5260 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}"); 5261 verifyIndependentOfContext("typedef void (*f)(int *a);"); 5262 verifyIndependentOfContext("int i{a * b};"); 5263 verifyIndependentOfContext("aaa && aaa->f();"); 5264 verifyIndependentOfContext("int x = ~*p;"); 5265 verifyFormat("Constructor() : a(a), area(width * height) {}"); 5266 verifyFormat("Constructor() : a(a), area(a, width * height) {}"); 5267 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}"); 5268 verifyFormat("void f() { f(a, c * d); }"); 5269 verifyFormat("void f() { f(new a(), c * d); }"); 5270 verifyFormat("void f(const MyOverride &override);"); 5271 verifyFormat("void f(const MyFinal &final);"); 5272 verifyIndependentOfContext("bool a = f() && override.f();"); 5273 verifyIndependentOfContext("bool a = f() && final.f();"); 5274 5275 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 5276 5277 verifyIndependentOfContext("A<int *> a;"); 5278 verifyIndependentOfContext("A<int **> a;"); 5279 verifyIndependentOfContext("A<int *, int *> a;"); 5280 verifyIndependentOfContext("A<int *[]> a;"); 5281 verifyIndependentOfContext( 5282 "const char *const p = reinterpret_cast<const char *const>(q);"); 5283 verifyIndependentOfContext("A<int **, int **> a;"); 5284 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); 5285 verifyFormat("for (char **a = b; *a; ++a) {\n}"); 5286 verifyFormat("for (; a && b;) {\n}"); 5287 verifyFormat("bool foo = true && [] { return false; }();"); 5288 5289 verifyFormat( 5290 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5291 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5292 5293 verifyGoogleFormat("int const* a = &b;"); 5294 verifyGoogleFormat("**outparam = 1;"); 5295 verifyGoogleFormat("*outparam = a * b;"); 5296 verifyGoogleFormat("int main(int argc, char** argv) {}"); 5297 verifyGoogleFormat("A<int*> a;"); 5298 verifyGoogleFormat("A<int**> a;"); 5299 verifyGoogleFormat("A<int*, int*> a;"); 5300 verifyGoogleFormat("A<int**, int**> a;"); 5301 verifyGoogleFormat("f(b ? *c : *d);"); 5302 verifyGoogleFormat("int a = b ? *c : *d;"); 5303 verifyGoogleFormat("Type* t = **x;"); 5304 verifyGoogleFormat("Type* t = *++*x;"); 5305 verifyGoogleFormat("*++*x;"); 5306 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 5307 verifyGoogleFormat("Type* t = x++ * y;"); 5308 verifyGoogleFormat( 5309 "const char* const p = reinterpret_cast<const char* const>(q);"); 5310 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);"); 5311 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);"); 5312 verifyGoogleFormat("template <typename T>\n" 5313 "void f(int i = 0, SomeType** temps = NULL);"); 5314 5315 FormatStyle Left = getLLVMStyle(); 5316 Left.PointerAlignment = FormatStyle::PAS_Left; 5317 verifyFormat("x = *a(x) = *a(y);", Left); 5318 verifyFormat("for (;; *a = b) {\n}", Left); 5319 verifyFormat("return *this += 1;", Left); 5320 5321 verifyIndependentOfContext("a = *(x + y);"); 5322 verifyIndependentOfContext("a = &(x + y);"); 5323 verifyIndependentOfContext("*(x + y).call();"); 5324 verifyIndependentOfContext("&(x + y)->call();"); 5325 verifyFormat("void f() { &(*I).first; }"); 5326 5327 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 5328 verifyFormat( 5329 "int *MyValues = {\n" 5330 " *A, // Operator detection might be confused by the '{'\n" 5331 " *BB // Operator detection might be confused by previous comment\n" 5332 "};"); 5333 5334 verifyIndependentOfContext("if (int *a = &b)"); 5335 verifyIndependentOfContext("if (int &a = *b)"); 5336 verifyIndependentOfContext("if (a & b[i])"); 5337 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 5338 verifyIndependentOfContext("if (*b[i])"); 5339 verifyIndependentOfContext("if (int *a = (&b))"); 5340 verifyIndependentOfContext("while (int *a = &b)"); 5341 verifyIndependentOfContext("size = sizeof *a;"); 5342 verifyIndependentOfContext("if (a && (b = c))"); 5343 verifyFormat("void f() {\n" 5344 " for (const int &v : Values) {\n" 5345 " }\n" 5346 "}"); 5347 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); 5348 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); 5349 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}"); 5350 5351 verifyFormat("#define A (!a * b)"); 5352 verifyFormat("#define MACRO \\\n" 5353 " int *i = a * b; \\\n" 5354 " void f(a *b);", 5355 getLLVMStyleWithColumns(19)); 5356 5357 verifyIndependentOfContext("A = new SomeType *[Length];"); 5358 verifyIndependentOfContext("A = new SomeType *[Length]();"); 5359 verifyIndependentOfContext("T **t = new T *;"); 5360 verifyIndependentOfContext("T **t = new T *();"); 5361 verifyGoogleFormat("A = new SomeType*[Length]();"); 5362 verifyGoogleFormat("A = new SomeType*[Length];"); 5363 verifyGoogleFormat("T** t = new T*;"); 5364 verifyGoogleFormat("T** t = new T*();"); 5365 5366 FormatStyle PointerLeft = getLLVMStyle(); 5367 PointerLeft.PointerAlignment = FormatStyle::PAS_Left; 5368 verifyFormat("delete *x;", PointerLeft); 5369 verifyFormat("STATIC_ASSERT((a & b) == 0);"); 5370 verifyFormat("STATIC_ASSERT(0 == (a & b));"); 5371 verifyFormat("template <bool a, bool b> " 5372 "typename t::if<x && y>::type f() {}"); 5373 verifyFormat("template <int *y> f() {}"); 5374 verifyFormat("vector<int *> v;"); 5375 verifyFormat("vector<int *const> v;"); 5376 verifyFormat("vector<int *const **const *> v;"); 5377 verifyFormat("vector<int *volatile> v;"); 5378 verifyFormat("vector<a * b> v;"); 5379 verifyFormat("foo<b && false>();"); 5380 verifyFormat("foo<b & 1>();"); 5381 verifyFormat("decltype(*::std::declval<const T &>()) void F();"); 5382 verifyFormat( 5383 "template <class T, class = typename std::enable_if<\n" 5384 " std::is_integral<T>::value &&\n" 5385 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n" 5386 "void F();", 5387 getLLVMStyleWithColumns(70)); 5388 verifyFormat( 5389 "template <class T,\n" 5390 " class = typename std::enable_if<\n" 5391 " std::is_integral<T>::value &&\n" 5392 " (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n" 5393 " class U>\n" 5394 "void F();", 5395 getLLVMStyleWithColumns(70)); 5396 verifyFormat( 5397 "template <class T,\n" 5398 " class = typename ::std::enable_if<\n" 5399 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n" 5400 "void F();", 5401 getGoogleStyleWithColumns(68)); 5402 5403 verifyIndependentOfContext("MACRO(int *i);"); 5404 verifyIndependentOfContext("MACRO(auto *a);"); 5405 verifyIndependentOfContext("MACRO(const A *a);"); 5406 verifyIndependentOfContext("MACRO(A *const a);"); 5407 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');"); 5408 verifyFormat("void f() { f(float{1}, a * a); }"); 5409 // FIXME: Is there a way to make this work? 5410 // verifyIndependentOfContext("MACRO(A *a);"); 5411 5412 verifyFormat("DatumHandle const *operator->() const { return input_; }"); 5413 verifyFormat("return options != nullptr && operator==(*options);"); 5414 5415 EXPECT_EQ("#define OP(x) \\\n" 5416 " ostream &operator<<(ostream &s, const A &a) { \\\n" 5417 " return s << a.DebugString(); \\\n" 5418 " }", 5419 format("#define OP(x) \\\n" 5420 " ostream &operator<<(ostream &s, const A &a) { \\\n" 5421 " return s << a.DebugString(); \\\n" 5422 " }", 5423 getLLVMStyleWithColumns(50))); 5424 5425 // FIXME: We cannot handle this case yet; we might be able to figure out that 5426 // foo<x> d > v; doesn't make sense. 5427 verifyFormat("foo<a<b && c> d> v;"); 5428 5429 FormatStyle PointerMiddle = getLLVMStyle(); 5430 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 5431 verifyFormat("delete *x;", PointerMiddle); 5432 verifyFormat("int * x;", PointerMiddle); 5433 verifyFormat("template <int * y> f() {}", PointerMiddle); 5434 verifyFormat("int * f(int * a) {}", PointerMiddle); 5435 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle); 5436 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle); 5437 verifyFormat("A<int *> a;", PointerMiddle); 5438 verifyFormat("A<int **> a;", PointerMiddle); 5439 verifyFormat("A<int *, int *> a;", PointerMiddle); 5440 verifyFormat("A<int * []> a;", PointerMiddle); 5441 verifyFormat("A = new SomeType *[Length]();", PointerMiddle); 5442 verifyFormat("A = new SomeType *[Length];", PointerMiddle); 5443 verifyFormat("T ** t = new T *;", PointerMiddle); 5444 5445 // Member function reference qualifiers aren't binary operators. 5446 verifyFormat("string // break\n" 5447 "operator()() & {}"); 5448 verifyFormat("string // break\n" 5449 "operator()() && {}"); 5450 verifyGoogleFormat("template <typename T>\n" 5451 "auto x() & -> int {}"); 5452 } 5453 5454 TEST_F(FormatTest, UnderstandsAttributes) { 5455 verifyFormat("SomeType s __attribute__((unused)) (InitValue);"); 5456 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n" 5457 "aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 5458 FormatStyle AfterType = getLLVMStyle(); 5459 AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 5460 verifyFormat("__attribute__((nodebug)) void\n" 5461 "foo() {}\n", 5462 AfterType); 5463 } 5464 5465 TEST_F(FormatTest, UnderstandsEllipsis) { 5466 verifyFormat("int printf(const char *fmt, ...);"); 5467 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); 5468 verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}"); 5469 5470 FormatStyle PointersLeft = getLLVMStyle(); 5471 PointersLeft.PointerAlignment = FormatStyle::PAS_Left; 5472 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft); 5473 } 5474 5475 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { 5476 EXPECT_EQ("int *a;\n" 5477 "int *a;\n" 5478 "int *a;", 5479 format("int *a;\n" 5480 "int* a;\n" 5481 "int *a;", 5482 getGoogleStyle())); 5483 EXPECT_EQ("int* a;\n" 5484 "int* a;\n" 5485 "int* a;", 5486 format("int* a;\n" 5487 "int* a;\n" 5488 "int *a;", 5489 getGoogleStyle())); 5490 EXPECT_EQ("int *a;\n" 5491 "int *a;\n" 5492 "int *a;", 5493 format("int *a;\n" 5494 "int * a;\n" 5495 "int * a;", 5496 getGoogleStyle())); 5497 EXPECT_EQ("auto x = [] {\n" 5498 " int *a;\n" 5499 " int *a;\n" 5500 " int *a;\n" 5501 "};", 5502 format("auto x=[]{int *a;\n" 5503 "int * a;\n" 5504 "int * a;};", 5505 getGoogleStyle())); 5506 } 5507 5508 TEST_F(FormatTest, UnderstandsRvalueReferences) { 5509 verifyFormat("int f(int &&a) {}"); 5510 verifyFormat("int f(int a, char &&b) {}"); 5511 verifyFormat("void f() { int &&a = b; }"); 5512 verifyGoogleFormat("int f(int a, char&& b) {}"); 5513 verifyGoogleFormat("void f() { int&& a = b; }"); 5514 5515 verifyIndependentOfContext("A<int &&> a;"); 5516 verifyIndependentOfContext("A<int &&, int &&> a;"); 5517 verifyGoogleFormat("A<int&&> a;"); 5518 verifyGoogleFormat("A<int&&, int&&> a;"); 5519 5520 // Not rvalue references: 5521 verifyFormat("template <bool B, bool C> class A {\n" 5522 " static_assert(B && C, \"Something is wrong\");\n" 5523 "};"); 5524 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))"); 5525 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))"); 5526 verifyFormat("#define A(a, b) (a && b)"); 5527 } 5528 5529 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 5530 verifyFormat("void f() {\n" 5531 " x[aaaaaaaaa -\n" 5532 " b] = 23;\n" 5533 "}", 5534 getLLVMStyleWithColumns(15)); 5535 } 5536 5537 TEST_F(FormatTest, FormatsCasts) { 5538 verifyFormat("Type *A = static_cast<Type *>(P);"); 5539 verifyFormat("Type *A = (Type *)P;"); 5540 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 5541 verifyFormat("int a = (int)(2.0f);"); 5542 verifyFormat("int a = (int)2.0f;"); 5543 verifyFormat("x[(int32)y];"); 5544 verifyFormat("x = (int32)y;"); 5545 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)"); 5546 verifyFormat("int a = (int)*b;"); 5547 verifyFormat("int a = (int)2.0f;"); 5548 verifyFormat("int a = (int)~0;"); 5549 verifyFormat("int a = (int)++a;"); 5550 verifyFormat("int a = (int)sizeof(int);"); 5551 verifyFormat("int a = (int)+2;"); 5552 verifyFormat("my_int a = (my_int)2.0f;"); 5553 verifyFormat("my_int a = (my_int)sizeof(int);"); 5554 verifyFormat("return (my_int)aaa;"); 5555 verifyFormat("#define x ((int)-1)"); 5556 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1"); 5557 verifyFormat("#define p(q) ((int *)&q)"); 5558 verifyFormat("fn(a)(b) + 1;"); 5559 5560 verifyFormat("void f() { my_int a = (my_int)*b; }"); 5561 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }"); 5562 verifyFormat("my_int a = (my_int)~0;"); 5563 verifyFormat("my_int a = (my_int)++a;"); 5564 verifyFormat("my_int a = (my_int)-2;"); 5565 verifyFormat("my_int a = (my_int)1;"); 5566 verifyFormat("my_int a = (my_int *)1;"); 5567 verifyFormat("my_int a = (const my_int)-1;"); 5568 verifyFormat("my_int a = (const my_int *)-1;"); 5569 verifyFormat("my_int a = (my_int)(my_int)-1;"); 5570 verifyFormat("my_int a = (ns::my_int)-2;"); 5571 verifyFormat("case (my_int)ONE:"); 5572 verifyFormat("auto x = (X)this;"); 5573 5574 // FIXME: single value wrapped with paren will be treated as cast. 5575 verifyFormat("void f(int i = (kValue)*kMask) {}"); 5576 5577 verifyFormat("{ (void)F; }"); 5578 5579 // Don't break after a cast's 5580 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5581 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n" 5582 " bbbbbbbbbbbbbbbbbbbbbb);"); 5583 5584 // These are not casts. 5585 verifyFormat("void f(int *) {}"); 5586 verifyFormat("f(foo)->b;"); 5587 verifyFormat("f(foo).b;"); 5588 verifyFormat("f(foo)(b);"); 5589 verifyFormat("f(foo)[b];"); 5590 verifyFormat("[](foo) { return 4; }(bar);"); 5591 verifyFormat("(*funptr)(foo)[4];"); 5592 verifyFormat("funptrs[4](foo)[4];"); 5593 verifyFormat("void f(int *);"); 5594 verifyFormat("void f(int *) = 0;"); 5595 verifyFormat("void f(SmallVector<int>) {}"); 5596 verifyFormat("void f(SmallVector<int>);"); 5597 verifyFormat("void f(SmallVector<int>) = 0;"); 5598 verifyFormat("void f(int i = (kA * kB) & kMask) {}"); 5599 verifyFormat("int a = sizeof(int) * b;"); 5600 verifyFormat("int a = alignof(int) * b;", getGoogleStyle()); 5601 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;"); 5602 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");"); 5603 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;"); 5604 5605 // These are not casts, but at some point were confused with casts. 5606 verifyFormat("virtual void foo(int *) override;"); 5607 verifyFormat("virtual void foo(char &) const;"); 5608 verifyFormat("virtual void foo(int *a, char *) const;"); 5609 verifyFormat("int a = sizeof(int *) + b;"); 5610 verifyFormat("int a = alignof(int *) + b;", getGoogleStyle()); 5611 verifyFormat("bool b = f(g<int>) && c;"); 5612 verifyFormat("typedef void (*f)(int i) func;"); 5613 5614 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n" 5615 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 5616 // FIXME: The indentation here is not ideal. 5617 verifyFormat( 5618 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5619 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n" 5620 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];"); 5621 } 5622 5623 TEST_F(FormatTest, FormatsFunctionTypes) { 5624 verifyFormat("A<bool()> a;"); 5625 verifyFormat("A<SomeType()> a;"); 5626 verifyFormat("A<void (*)(int, std::string)> a;"); 5627 verifyFormat("A<void *(int)>;"); 5628 verifyFormat("void *(*a)(int *, SomeType *);"); 5629 verifyFormat("int (*func)(void *);"); 5630 verifyFormat("void f() { int (*func)(void *); }"); 5631 verifyFormat("template <class CallbackClass>\n" 5632 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);"); 5633 5634 verifyGoogleFormat("A<void*(int*, SomeType*)>;"); 5635 verifyGoogleFormat("void* (*a)(int);"); 5636 verifyGoogleFormat( 5637 "template <class CallbackClass>\n" 5638 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);"); 5639 5640 // Other constructs can look somewhat like function types: 5641 verifyFormat("A<sizeof(*x)> a;"); 5642 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)"); 5643 verifyFormat("some_var = function(*some_pointer_var)[0];"); 5644 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }"); 5645 verifyFormat("int x = f(&h)();"); 5646 verifyFormat("returnsFunction(¶m1, ¶m2)(param);"); 5647 verifyFormat("std::function<\n" 5648 " LooooooooooongTemplatedType<\n" 5649 " SomeType>*(\n" 5650 " LooooooooooooooooongType type)>\n" 5651 " function;", 5652 getGoogleStyleWithColumns(40)); 5653 } 5654 5655 TEST_F(FormatTest, FormatsPointersToArrayTypes) { 5656 verifyFormat("A (*foo_)[6];"); 5657 verifyFormat("vector<int> (*foo_)[6];"); 5658 } 5659 5660 TEST_F(FormatTest, BreaksLongVariableDeclarations) { 5661 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 5662 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 5663 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n" 5664 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 5665 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 5666 " *LoooooooooooooooooooooooooooooooooooooooongVariable;"); 5667 5668 // Different ways of ()-initializiation. 5669 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 5670 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);"); 5671 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 5672 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);"); 5673 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 5674 " LoooooooooooooooooooooooooooooooooooooooongVariable({});"); 5675 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 5676 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);"); 5677 5678 // Lambdas should not confuse the variable declaration heuristic. 5679 verifyFormat("LooooooooooooooooongType\n" 5680 " variable(nullptr, [](A *a) {});", 5681 getLLVMStyleWithColumns(40)); 5682 } 5683 5684 TEST_F(FormatTest, BreaksLongDeclarations) { 5685 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n" 5686 " AnotherNameForTheLongType;"); 5687 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n" 5688 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5689 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 5690 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 5691 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n" 5692 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 5693 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 5694 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 5695 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n" 5696 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 5697 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 5698 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 5699 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 5700 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 5701 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 5702 "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);"); 5703 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 5704 "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}"); 5705 FormatStyle Indented = getLLVMStyle(); 5706 Indented.IndentWrappedFunctionNames = true; 5707 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 5708 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();", 5709 Indented); 5710 verifyFormat( 5711 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 5712 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 5713 Indented); 5714 verifyFormat( 5715 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 5716 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 5717 Indented); 5718 verifyFormat( 5719 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 5720 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 5721 Indented); 5722 5723 // FIXME: Without the comment, this breaks after "(". 5724 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n" 5725 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();", 5726 getGoogleStyle()); 5727 5728 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 5729 " int LoooooooooooooooooooongParam2) {}"); 5730 verifyFormat( 5731 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" 5732 " SourceLocation L, IdentifierIn *II,\n" 5733 " Type *T) {}"); 5734 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" 5735 "ReallyReaaallyLongFunctionName(\n" 5736 " const std::string &SomeParameter,\n" 5737 " const SomeType<string, SomeOtherTemplateParameter>\n" 5738 " &ReallyReallyLongParameterName,\n" 5739 " const SomeType<string, SomeOtherTemplateParameter>\n" 5740 " &AnotherLongParameterName) {}"); 5741 verifyFormat("template <typename A>\n" 5742 "SomeLoooooooooooooooooooooongType<\n" 5743 " typename some_namespace::SomeOtherType<A>::Type>\n" 5744 "Function() {}"); 5745 5746 verifyGoogleFormat( 5747 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" 5748 " aaaaaaaaaaaaaaaaaaaaaaa;"); 5749 verifyGoogleFormat( 5750 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" 5751 " SourceLocation L) {}"); 5752 verifyGoogleFormat( 5753 "some_namespace::LongReturnType\n" 5754 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" 5755 " int first_long_parameter, int second_parameter) {}"); 5756 5757 verifyGoogleFormat("template <typename T>\n" 5758 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" 5759 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); 5760 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5761 " int aaaaaaaaaaaaaaaaaaaaaaa);"); 5762 5763 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 5764 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5765 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5766 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5767 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 5768 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 5769 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5770 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 5771 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n" 5772 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5773 5774 verifyFormat("template <typename T> // Templates on own line.\n" 5775 "static int // Some comment.\n" 5776 "MyFunction(int a);", 5777 getLLVMStyle()); 5778 } 5779 5780 TEST_F(FormatTest, FormatsArrays) { 5781 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 5782 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;"); 5783 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n" 5784 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;"); 5785 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n" 5786 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}"); 5787 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5788 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 5789 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5790 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;"); 5791 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5792 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 5793 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 5794 verifyFormat( 5795 "llvm::outs() << \"aaaaaaaaaaaa: \"\n" 5796 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 5797 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 5798 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n" 5799 " .aaaaaaaaaaaaaaaaaaaaaa();"); 5800 5801 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n" 5802 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];"); 5803 verifyFormat( 5804 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n" 5805 " .aaaaaaa[0]\n" 5806 " .aaaaaaaaaaaaaaaaaaaaaa();"); 5807 verifyFormat("a[::b::c];"); 5808 5809 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10)); 5810 5811 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0); 5812 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit); 5813 } 5814 5815 TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 5816 verifyFormat("(a)->b();"); 5817 verifyFormat("--a;"); 5818 } 5819 5820 TEST_F(FormatTest, HandlesIncludeDirectives) { 5821 verifyFormat("#include <string>\n" 5822 "#include <a/b/c.h>\n" 5823 "#include \"a/b/string\"\n" 5824 "#include \"string.h\"\n" 5825 "#include \"string.h\"\n" 5826 "#include <a-a>\n" 5827 "#include < path with space >\n" 5828 "#include_next <test.h>" 5829 "#include \"abc.h\" // this is included for ABC\n" 5830 "#include \"some long include\" // with a comment\n" 5831 "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"", 5832 getLLVMStyleWithColumns(35)); 5833 EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\"")); 5834 EXPECT_EQ("#include <a>", format("#include<a>")); 5835 5836 verifyFormat("#import <string>"); 5837 verifyFormat("#import <a/b/c.h>"); 5838 verifyFormat("#import \"a/b/string\""); 5839 verifyFormat("#import \"string.h\""); 5840 verifyFormat("#import \"string.h\""); 5841 verifyFormat("#if __has_include(<strstream>)\n" 5842 "#include <strstream>\n" 5843 "#endif"); 5844 5845 verifyFormat("#define MY_IMPORT <a/b>"); 5846 5847 verifyFormat("#if __has_include(<a/b>)"); 5848 verifyFormat("#if __has_include_next(<a/b>)"); 5849 verifyFormat("#define F __has_include(<a/b>)"); 5850 verifyFormat("#define F __has_include_next(<a/b>)"); 5851 5852 // Protocol buffer definition or missing "#". 5853 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";", 5854 getLLVMStyleWithColumns(30)); 5855 5856 FormatStyle Style = getLLVMStyle(); 5857 Style.AlwaysBreakBeforeMultilineStrings = true; 5858 Style.ColumnLimit = 0; 5859 verifyFormat("#import \"abc.h\"", Style); 5860 5861 // But 'import' might also be a regular C++ namespace. 5862 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5863 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5864 } 5865 5866 //===----------------------------------------------------------------------===// 5867 // Error recovery tests. 5868 //===----------------------------------------------------------------------===// 5869 5870 TEST_F(FormatTest, IncompleteParameterLists) { 5871 FormatStyle NoBinPacking = getLLVMStyle(); 5872 NoBinPacking.BinPackParameters = false; 5873 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" 5874 " double *min_x,\n" 5875 " double *max_x,\n" 5876 " double *min_y,\n" 5877 " double *max_y,\n" 5878 " double *min_z,\n" 5879 " double *max_z, ) {}", 5880 NoBinPacking); 5881 } 5882 5883 TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 5884 verifyFormat("void f() { return; }\n42"); 5885 verifyFormat("void f() {\n" 5886 " if (0)\n" 5887 " return;\n" 5888 "}\n" 5889 "42"); 5890 verifyFormat("void f() { return }\n42"); 5891 verifyFormat("void f() {\n" 5892 " if (0)\n" 5893 " return\n" 5894 "}\n" 5895 "42"); 5896 } 5897 5898 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 5899 EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); 5900 EXPECT_EQ("void f() {\n" 5901 " if (a)\n" 5902 " return\n" 5903 "}", 5904 format("void f ( ) { if ( a ) return }")); 5905 EXPECT_EQ("namespace N {\n" 5906 "void f()\n" 5907 "}", 5908 format("namespace N { void f() }")); 5909 EXPECT_EQ("namespace N {\n" 5910 "void f() {}\n" 5911 "void g()\n" 5912 "} // namespace N", 5913 format("namespace N { void f( ) { } void g( ) }")); 5914 } 5915 5916 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 5917 verifyFormat("int aaaaaaaa =\n" 5918 " // Overlylongcomment\n" 5919 " b;", 5920 getLLVMStyleWithColumns(20)); 5921 verifyFormat("function(\n" 5922 " ShortArgument,\n" 5923 " LoooooooooooongArgument);\n", 5924 getLLVMStyleWithColumns(20)); 5925 } 5926 5927 TEST_F(FormatTest, IncorrectAccessSpecifier) { 5928 verifyFormat("public:"); 5929 verifyFormat("class A {\n" 5930 "public\n" 5931 " void f() {}\n" 5932 "};"); 5933 verifyFormat("public\n" 5934 "int qwerty;"); 5935 verifyFormat("public\n" 5936 "B {}"); 5937 verifyFormat("public\n" 5938 "{}"); 5939 verifyFormat("public\n" 5940 "B { int x; }"); 5941 } 5942 5943 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 5944 verifyFormat("{"); 5945 verifyFormat("#})"); 5946 verifyNoCrash("(/**/[:!] ?[)."); 5947 } 5948 5949 TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 5950 verifyFormat("do {\n}"); 5951 verifyFormat("do {\n}\n" 5952 "f();"); 5953 verifyFormat("do {\n}\n" 5954 "wheeee(fun);"); 5955 verifyFormat("do {\n" 5956 " f();\n" 5957 "}"); 5958 } 5959 5960 TEST_F(FormatTest, IncorrectCodeMissingParens) { 5961 verifyFormat("if {\n foo;\n foo();\n}"); 5962 verifyFormat("switch {\n foo;\n foo();\n}"); 5963 verifyIncompleteFormat("for {\n foo;\n foo();\n}"); 5964 verifyFormat("while {\n foo;\n foo();\n}"); 5965 verifyFormat("do {\n foo;\n foo();\n} while;"); 5966 } 5967 5968 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 5969 verifyIncompleteFormat("namespace {\n" 5970 "class Foo { Foo (\n" 5971 "};\n" 5972 "} // namespace"); 5973 } 5974 5975 TEST_F(FormatTest, IncorrectCodeErrorDetection) { 5976 EXPECT_EQ("{\n {}\n", format("{\n{\n}\n")); 5977 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 5978 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 5979 EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n")); 5980 5981 EXPECT_EQ("{\n" 5982 " {\n" 5983 " breakme(\n" 5984 " qwe);\n" 5985 " }\n", 5986 format("{\n" 5987 " {\n" 5988 " breakme(qwe);\n" 5989 "}\n", 5990 getLLVMStyleWithColumns(10))); 5991 } 5992 5993 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 5994 verifyFormat("int x = {\n" 5995 " avariable,\n" 5996 " b(alongervariable)};", 5997 getLLVMStyleWithColumns(25)); 5998 } 5999 6000 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 6001 verifyFormat("return (a)(b){1, 2, 3};"); 6002 } 6003 6004 TEST_F(FormatTest, LayoutCxx11BraceInitializers) { 6005 verifyFormat("vector<int> x{1, 2, 3, 4};"); 6006 verifyFormat("vector<int> x{\n" 6007 " 1,\n" 6008 " 2,\n" 6009 " 3,\n" 6010 " 4,\n" 6011 "};"); 6012 verifyFormat("vector<T> x{{}, {}, {}, {}};"); 6013 verifyFormat("f({1, 2});"); 6014 verifyFormat("auto v = Foo{-1};"); 6015 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});"); 6016 verifyFormat("Class::Class : member{1, 2, 3} {}"); 6017 verifyFormat("new vector<int>{1, 2, 3};"); 6018 verifyFormat("new int[3]{1, 2, 3};"); 6019 verifyFormat("new int{1};"); 6020 verifyFormat("return {arg1, arg2};"); 6021 verifyFormat("return {arg1, SomeType{parameter}};"); 6022 verifyFormat("int count = set<int>{f(), g(), h()}.size();"); 6023 verifyFormat("new T{arg1, arg2};"); 6024 verifyFormat("f(MyMap[{composite, key}]);"); 6025 verifyFormat("class Class {\n" 6026 " T member = {arg1, arg2};\n" 6027 "};"); 6028 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};"); 6029 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 6030 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 6031 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");"); 6032 verifyFormat("int a = std::is_integral<int>{} + 0;"); 6033 6034 verifyFormat("int foo(int i) { return fo1{}(i); }"); 6035 verifyFormat("int foo(int i) { return fo1{}(i); }"); 6036 verifyFormat("auto i = decltype(x){};"); 6037 verifyFormat("std::vector<int> v = {1, 0 /* comment */};"); 6038 verifyFormat("Node n{1, Node{1000}, //\n" 6039 " 2};"); 6040 verifyFormat("Aaaa aaaaaaa{\n" 6041 " {\n" 6042 " aaaa,\n" 6043 " },\n" 6044 "};"); 6045 verifyFormat("class C : public D {\n" 6046 " SomeClass SC{2};\n" 6047 "};"); 6048 verifyFormat("class C : public A {\n" 6049 " class D : public B {\n" 6050 " void f() { int i{2}; }\n" 6051 " };\n" 6052 "};"); 6053 verifyFormat("#define A {a, a},"); 6054 6055 // Binpacking only if there is no trailing comma 6056 verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n" 6057 " cccccccccc, dddddddddd};", 6058 getLLVMStyleWithColumns(50)); 6059 verifyFormat("const Aaaaaa aaaaa = {\n" 6060 " aaaaaaaaaaa,\n" 6061 " bbbbbbbbbbb,\n" 6062 " ccccccccccc,\n" 6063 " ddddddddddd,\n" 6064 "};", getLLVMStyleWithColumns(50)); 6065 6066 // Cases where distinguising braced lists and blocks is hard. 6067 verifyFormat("vector<int> v{12} GUARDED_BY(mutex);"); 6068 verifyFormat("void f() {\n" 6069 " return; // comment\n" 6070 "}\n" 6071 "SomeType t;"); 6072 verifyFormat("void f() {\n" 6073 " if (a) {\n" 6074 " f();\n" 6075 " }\n" 6076 "}\n" 6077 "SomeType t;"); 6078 6079 // In combination with BinPackArguments = false. 6080 FormatStyle NoBinPacking = getLLVMStyle(); 6081 NoBinPacking.BinPackArguments = false; 6082 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n" 6083 " bbbbb,\n" 6084 " ccccc,\n" 6085 " ddddd,\n" 6086 " eeeee,\n" 6087 " ffffff,\n" 6088 " ggggg,\n" 6089 " hhhhhh,\n" 6090 " iiiiii,\n" 6091 " jjjjjj,\n" 6092 " kkkkkk};", 6093 NoBinPacking); 6094 verifyFormat("const Aaaaaa aaaaa = {\n" 6095 " aaaaa,\n" 6096 " bbbbb,\n" 6097 " ccccc,\n" 6098 " ddddd,\n" 6099 " eeeee,\n" 6100 " ffffff,\n" 6101 " ggggg,\n" 6102 " hhhhhh,\n" 6103 " iiiiii,\n" 6104 " jjjjjj,\n" 6105 " kkkkkk,\n" 6106 "};", 6107 NoBinPacking); 6108 verifyFormat( 6109 "const Aaaaaa aaaaa = {\n" 6110 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n" 6111 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n" 6112 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n" 6113 "};", 6114 NoBinPacking); 6115 6116 // FIXME: The alignment of these trailing comments might be bad. Then again, 6117 // this might be utterly useless in real code. 6118 verifyFormat("Constructor::Constructor()\n" 6119 " : some_value{ //\n" 6120 " aaaaaaa, //\n" 6121 " bbbbbbb} {}"); 6122 6123 // In braced lists, the first comment is always assumed to belong to the 6124 // first element. Thus, it can be moved to the next or previous line as 6125 // appropriate. 6126 EXPECT_EQ("function({// First element:\n" 6127 " 1,\n" 6128 " // Second element:\n" 6129 " 2});", 6130 format("function({\n" 6131 " // First element:\n" 6132 " 1,\n" 6133 " // Second element:\n" 6134 " 2});")); 6135 EXPECT_EQ("std::vector<int> MyNumbers{\n" 6136 " // First element:\n" 6137 " 1,\n" 6138 " // Second element:\n" 6139 " 2};", 6140 format("std::vector<int> MyNumbers{// First element:\n" 6141 " 1,\n" 6142 " // Second element:\n" 6143 " 2};", 6144 getLLVMStyleWithColumns(30))); 6145 // A trailing comma should still lead to an enforced line break and no 6146 // binpacking. 6147 EXPECT_EQ("vector<int> SomeVector = {\n" 6148 " // aaa\n" 6149 " 1,\n" 6150 " 2,\n" 6151 "};", 6152 format("vector<int> SomeVector = { // aaa\n" 6153 " 1, 2, };")); 6154 6155 FormatStyle ExtraSpaces = getLLVMStyle(); 6156 ExtraSpaces.Cpp11BracedListStyle = false; 6157 ExtraSpaces.ColumnLimit = 75; 6158 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces); 6159 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces); 6160 verifyFormat("f({ 1, 2 });", ExtraSpaces); 6161 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces); 6162 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces); 6163 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces); 6164 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces); 6165 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces); 6166 verifyFormat("return { arg1, arg2 };", ExtraSpaces); 6167 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces); 6168 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces); 6169 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces); 6170 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces); 6171 verifyFormat("class Class {\n" 6172 " T member = { arg1, arg2 };\n" 6173 "};", 6174 ExtraSpaces); 6175 verifyFormat( 6176 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6177 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n" 6178 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 6179 " bbbbbbbbbbbbbbbbbbbb, bbbbb };", 6180 ExtraSpaces); 6181 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces); 6182 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });", 6183 ExtraSpaces); 6184 verifyFormat( 6185 "someFunction(OtherParam,\n" 6186 " BracedList{ // comment 1 (Forcing interesting break)\n" 6187 " param1, param2,\n" 6188 " // comment 2\n" 6189 " param3, param4 });", 6190 ExtraSpaces); 6191 verifyFormat( 6192 "std::this_thread::sleep_for(\n" 6193 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);", 6194 ExtraSpaces); 6195 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n" 6196 " aaaaaaa,\n" 6197 " aaaaaaaaaa,\n" 6198 " aaaaa,\n" 6199 " aaaaaaaaaaaaaaa,\n" 6200 " aaa,\n" 6201 " aaaaaaaaaa,\n" 6202 " a,\n" 6203 " aaaaaaaaaaaaaaaaaaaaa,\n" 6204 " aaaaaaaaaaaa,\n" 6205 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n" 6206 " aaaaaaa,\n" 6207 " a};"); 6208 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces); 6209 verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces); 6210 verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces); 6211 } 6212 6213 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { 6214 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6215 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6216 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6217 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6218 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6219 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 6220 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n" 6221 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6222 " 1, 22, 333, 4444, 55555, //\n" 6223 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6224 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 6225 verifyFormat( 6226 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6227 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6228 " 1, 22, 333, 4444, 55555, 666666, // comment\n" 6229 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6230 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6231 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6232 " 7777777};"); 6233 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6234 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6235 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6236 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6237 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6238 " // Separating comment.\n" 6239 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6240 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6241 " // Leading comment\n" 6242 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6243 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6244 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6245 " 1, 1, 1, 1};", 6246 getLLVMStyleWithColumns(39)); 6247 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6248 " 1, 1, 1, 1};", 6249 getLLVMStyleWithColumns(38)); 6250 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n" 6251 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};", 6252 getLLVMStyleWithColumns(43)); 6253 verifyFormat( 6254 "static unsigned SomeValues[10][3] = {\n" 6255 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n" 6256 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};"); 6257 verifyFormat("static auto fields = new vector<string>{\n" 6258 " \"aaaaaaaaaaaaa\",\n" 6259 " \"aaaaaaaaaaaaa\",\n" 6260 " \"aaaaaaaaaaaa\",\n" 6261 " \"aaaaaaaaaaaaaa\",\n" 6262 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 6263 " \"aaaaaaaaaaaa\",\n" 6264 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 6265 "};"); 6266 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};"); 6267 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n" 6268 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n" 6269 " 3, cccccccccccccccccccccc};", 6270 getLLVMStyleWithColumns(60)); 6271 6272 // Trailing commas. 6273 verifyFormat("vector<int> x = {\n" 6274 " 1, 1, 1, 1, 1, 1, 1, 1,\n" 6275 "};", 6276 getLLVMStyleWithColumns(39)); 6277 verifyFormat("vector<int> x = {\n" 6278 " 1, 1, 1, 1, 1, 1, 1, 1, //\n" 6279 "};", 6280 getLLVMStyleWithColumns(39)); 6281 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6282 " 1, 1, 1, 1,\n" 6283 " /**/ /**/};", 6284 getLLVMStyleWithColumns(39)); 6285 6286 // Trailing comment in the first line. 6287 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n" 6288 " 1111111111, 2222222222, 33333333333, 4444444444, //\n" 6289 " 111111111, 222222222, 3333333333, 444444444, //\n" 6290 " 11111111, 22222222, 333333333, 44444444};"); 6291 // Trailing comment in the last line. 6292 verifyFormat("int aaaaa[] = {\n" 6293 " 1, 2, 3, // comment\n" 6294 " 4, 5, 6 // comment\n" 6295 "};"); 6296 6297 // With nested lists, we should either format one item per line or all nested 6298 // lists one on line. 6299 // FIXME: For some nested lists, we can do better. 6300 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n" 6301 " {aaaaaaaaaaaaaaaaaaa},\n" 6302 " {aaaaaaaaaaaaaaaaaaaaa},\n" 6303 " {aaaaaaaaaaaaaaaaa}};", 6304 getLLVMStyleWithColumns(60)); 6305 verifyFormat( 6306 "SomeStruct my_struct_array = {\n" 6307 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n" 6308 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n" 6309 " {aaa, aaa},\n" 6310 " {aaa, aaa},\n" 6311 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n" 6312 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6313 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};"); 6314 6315 // No column layout should be used here. 6316 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n" 6317 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};"); 6318 6319 verifyNoCrash("a<,"); 6320 6321 // No braced initializer here. 6322 verifyFormat("void f() {\n" 6323 " struct Dummy {};\n" 6324 " f(v);\n" 6325 "}"); 6326 6327 // Long lists should be formatted in columns even if they are nested. 6328 verifyFormat( 6329 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6330 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6331 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6332 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6333 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6334 " 1, 22, 333, 4444, 55555, 666666, 7777777});"); 6335 6336 // Allow "single-column" layout even if that violates the column limit. There 6337 // isn't going to be a better way. 6338 verifyFormat("std::vector<int> a = {\n" 6339 " aaaaaaaa,\n" 6340 " aaaaaaaa,\n" 6341 " aaaaaaaa,\n" 6342 " aaaaaaaa,\n" 6343 " aaaaaaaaaa,\n" 6344 " aaaaaaaa,\n" 6345 " aaaaaaaaaaaaaaaaaaaaaaaaaaa};", 6346 getLLVMStyleWithColumns(30)); 6347 verifyFormat("vector<int> aaaa = {\n" 6348 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6349 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6350 " aaaaaa.aaaaaaa,\n" 6351 " aaaaaa.aaaaaaa,\n" 6352 " aaaaaa.aaaaaaa,\n" 6353 " aaaaaa.aaaaaaa,\n" 6354 "};"); 6355 6356 // Don't create hanging lists. 6357 verifyFormat("someFunction(Param, {List1, List2,\n" 6358 " List3});", 6359 getLLVMStyleWithColumns(35)); 6360 verifyFormat("someFunction(Param, Param,\n" 6361 " {List1, List2,\n" 6362 " List3});", 6363 getLLVMStyleWithColumns(35)); 6364 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n" 6365 " aaaaaaaaaaaaaaaaaaaaaaa);"); 6366 } 6367 6368 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 6369 FormatStyle DoNotMerge = getLLVMStyle(); 6370 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 6371 6372 verifyFormat("void f() { return 42; }"); 6373 verifyFormat("void f() {\n" 6374 " return 42;\n" 6375 "}", 6376 DoNotMerge); 6377 verifyFormat("void f() {\n" 6378 " // Comment\n" 6379 "}"); 6380 verifyFormat("{\n" 6381 "#error {\n" 6382 " int a;\n" 6383 "}"); 6384 verifyFormat("{\n" 6385 " int a;\n" 6386 "#error {\n" 6387 "}"); 6388 verifyFormat("void f() {} // comment"); 6389 verifyFormat("void f() { int a; } // comment"); 6390 verifyFormat("void f() {\n" 6391 "} // comment", 6392 DoNotMerge); 6393 verifyFormat("void f() {\n" 6394 " int a;\n" 6395 "} // comment", 6396 DoNotMerge); 6397 verifyFormat("void f() {\n" 6398 "} // comment", 6399 getLLVMStyleWithColumns(15)); 6400 6401 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 6402 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 6403 6404 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 6405 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 6406 verifyFormat("class C {\n" 6407 " C()\n" 6408 " : iiiiiiii(nullptr),\n" 6409 " kkkkkkk(nullptr),\n" 6410 " mmmmmmm(nullptr),\n" 6411 " nnnnnnn(nullptr) {}\n" 6412 "};", 6413 getGoogleStyle()); 6414 6415 FormatStyle NoColumnLimit = getLLVMStyle(); 6416 NoColumnLimit.ColumnLimit = 0; 6417 EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit)); 6418 EXPECT_EQ("class C {\n" 6419 " A() : b(0) {}\n" 6420 "};", 6421 format("class C{A():b(0){}};", NoColumnLimit)); 6422 EXPECT_EQ("A()\n" 6423 " : b(0) {\n" 6424 "}", 6425 format("A()\n:b(0)\n{\n}", NoColumnLimit)); 6426 6427 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit; 6428 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine = 6429 FormatStyle::SFS_None; 6430 EXPECT_EQ("A()\n" 6431 " : b(0) {\n" 6432 "}", 6433 format("A():b(0){}", DoNotMergeNoColumnLimit)); 6434 EXPECT_EQ("A()\n" 6435 " : b(0) {\n" 6436 "}", 6437 format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit)); 6438 6439 verifyFormat("#define A \\\n" 6440 " void f() { \\\n" 6441 " int i; \\\n" 6442 " }", 6443 getLLVMStyleWithColumns(20)); 6444 verifyFormat("#define A \\\n" 6445 " void f() { int i; }", 6446 getLLVMStyleWithColumns(21)); 6447 verifyFormat("#define A \\\n" 6448 " void f() { \\\n" 6449 " int i; \\\n" 6450 " } \\\n" 6451 " int j;", 6452 getLLVMStyleWithColumns(22)); 6453 verifyFormat("#define A \\\n" 6454 " void f() { int i; } \\\n" 6455 " int j;", 6456 getLLVMStyleWithColumns(23)); 6457 } 6458 6459 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) { 6460 FormatStyle MergeEmptyOnly = getLLVMStyle(); 6461 MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 6462 verifyFormat("class C {\n" 6463 " int f() {}\n" 6464 "};", 6465 MergeEmptyOnly); 6466 verifyFormat("class C {\n" 6467 " int f() {\n" 6468 " return 42;\n" 6469 " }\n" 6470 "};", 6471 MergeEmptyOnly); 6472 verifyFormat("int f() {}", MergeEmptyOnly); 6473 verifyFormat("int f() {\n" 6474 " return 42;\n" 6475 "}", 6476 MergeEmptyOnly); 6477 6478 // Also verify behavior when BraceWrapping.AfterFunction = true 6479 MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 6480 MergeEmptyOnly.BraceWrapping.AfterFunction = true; 6481 verifyFormat("int f() {}", MergeEmptyOnly); 6482 verifyFormat("class C {\n" 6483 " int f() {}\n" 6484 "};", 6485 MergeEmptyOnly); 6486 } 6487 6488 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) { 6489 FormatStyle MergeInlineOnly = getLLVMStyle(); 6490 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 6491 verifyFormat("class C {\n" 6492 " int f() { return 42; }\n" 6493 "};", 6494 MergeInlineOnly); 6495 verifyFormat("int f() {\n" 6496 " return 42;\n" 6497 "}", 6498 MergeInlineOnly); 6499 6500 // SFS_Inline implies SFS_Empty 6501 verifyFormat("class C {\n" 6502 " int f() {}\n" 6503 "};", 6504 MergeInlineOnly); 6505 verifyFormat("int f() {}", MergeInlineOnly); 6506 6507 // Also verify behavior when BraceWrapping.AfterFunction = true 6508 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 6509 MergeInlineOnly.BraceWrapping.AfterFunction = true; 6510 verifyFormat("class C {\n" 6511 " int f() { return 42; }\n" 6512 "};", 6513 MergeInlineOnly); 6514 verifyFormat("int f()\n" 6515 "{\n" 6516 " return 42;\n" 6517 "}", 6518 MergeInlineOnly); 6519 6520 // SFS_Inline implies SFS_Empty 6521 verifyFormat("int f() {}", MergeInlineOnly); 6522 verifyFormat("class C {\n" 6523 " int f() {}\n" 6524 "};", 6525 MergeInlineOnly); 6526 } 6527 6528 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) { 6529 FormatStyle MergeInlineOnly = getLLVMStyle(); 6530 MergeInlineOnly.AllowShortFunctionsOnASingleLine = 6531 FormatStyle::SFS_InlineOnly; 6532 verifyFormat("class C {\n" 6533 " int f() { return 42; }\n" 6534 "};", 6535 MergeInlineOnly); 6536 verifyFormat("int f() {\n" 6537 " return 42;\n" 6538 "}", 6539 MergeInlineOnly); 6540 6541 // SFS_InlineOnly does not imply SFS_Empty 6542 verifyFormat("class C {\n" 6543 " int f() {}\n" 6544 "};", 6545 MergeInlineOnly); 6546 verifyFormat("int f() {\n" 6547 "}", 6548 MergeInlineOnly); 6549 6550 // Also verify behavior when BraceWrapping.AfterFunction = true 6551 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 6552 MergeInlineOnly.BraceWrapping.AfterFunction = true; 6553 verifyFormat("class C {\n" 6554 " int f() { return 42; }\n" 6555 "};", 6556 MergeInlineOnly); 6557 verifyFormat("int f()\n" 6558 "{\n" 6559 " return 42;\n" 6560 "}", 6561 MergeInlineOnly); 6562 6563 // SFS_InlineOnly does not imply SFS_Empty 6564 verifyFormat("int f()\n" 6565 "{\n" 6566 "}", 6567 MergeInlineOnly); 6568 verifyFormat("class C {\n" 6569 " int f() {}\n" 6570 "};", 6571 MergeInlineOnly); 6572 } 6573 6574 TEST_F(FormatTest, SplitEmptyFunction) { 6575 FormatStyle Style = getLLVMStyle(); 6576 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 6577 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 6578 Style.BraceWrapping.AfterFunction = true; 6579 Style.BraceWrapping.SplitEmptyFunction = false; 6580 Style.ColumnLimit = 40; 6581 6582 verifyFormat("int f()\n" 6583 "{}", 6584 Style); 6585 verifyFormat("int f()\n" 6586 "{\n" 6587 " return 42;\n" 6588 "}", 6589 Style); 6590 verifyFormat("int f()\n" 6591 "{\n" 6592 " // some comment\n" 6593 "}", 6594 Style); 6595 6596 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 6597 verifyFormat("int f() {}", Style); 6598 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 6599 "{}", 6600 Style); 6601 verifyFormat("int f()\n" 6602 "{\n" 6603 " return 0;\n" 6604 "}", 6605 Style); 6606 6607 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 6608 verifyFormat("class Foo {\n" 6609 " int f() {}\n" 6610 "};\n", 6611 Style); 6612 verifyFormat("class Foo {\n" 6613 " int f() { return 0; }\n" 6614 "};\n", 6615 Style); 6616 verifyFormat("class Foo {\n" 6617 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 6618 " {}\n" 6619 "};\n", 6620 Style); 6621 verifyFormat("class Foo {\n" 6622 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 6623 " {\n" 6624 " return 0;\n" 6625 " }\n" 6626 "};\n", 6627 Style); 6628 6629 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 6630 verifyFormat("int f() {}", Style); 6631 verifyFormat("int f() { return 0; }", Style); 6632 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 6633 "{}", 6634 Style); 6635 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 6636 "{\n" 6637 " return 0;\n" 6638 "}", 6639 Style); 6640 } 6641 6642 TEST_F(FormatTest, SplitEmptyClass) { 6643 FormatStyle Style = getLLVMStyle(); 6644 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 6645 Style.BraceWrapping.AfterClass = true; 6646 Style.BraceWrapping.SplitEmptyRecord = false; 6647 6648 verifyFormat("class Foo\n" 6649 "{};", 6650 Style); 6651 verifyFormat("/* something */ class Foo\n" 6652 "{};", 6653 Style); 6654 verifyFormat("template <typename X> class Foo\n" 6655 "{};", 6656 Style); 6657 verifyFormat("class Foo\n" 6658 "{\n" 6659 " Foo();\n" 6660 "};", 6661 Style); 6662 verifyFormat("typedef class Foo\n" 6663 "{\n" 6664 "} Foo_t;", 6665 Style); 6666 } 6667 6668 TEST_F(FormatTest, SplitEmptyStruct) { 6669 FormatStyle Style = getLLVMStyle(); 6670 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 6671 Style.BraceWrapping.AfterStruct = true; 6672 Style.BraceWrapping.SplitEmptyRecord = false; 6673 6674 verifyFormat("struct Foo\n" 6675 "{};", 6676 Style); 6677 verifyFormat("/* something */ struct Foo\n" 6678 "{};", 6679 Style); 6680 verifyFormat("template <typename X> struct Foo\n" 6681 "{};", 6682 Style); 6683 verifyFormat("struct Foo\n" 6684 "{\n" 6685 " Foo();\n" 6686 "};", 6687 Style); 6688 verifyFormat("typedef struct Foo\n" 6689 "{\n" 6690 "} Foo_t;", 6691 Style); 6692 //typedef struct Bar {} Bar_t; 6693 } 6694 6695 TEST_F(FormatTest, SplitEmptyUnion) { 6696 FormatStyle Style = getLLVMStyle(); 6697 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 6698 Style.BraceWrapping.AfterUnion = true; 6699 Style.BraceWrapping.SplitEmptyRecord = false; 6700 6701 verifyFormat("union Foo\n" 6702 "{};", 6703 Style); 6704 verifyFormat("/* something */ union Foo\n" 6705 "{};", 6706 Style); 6707 verifyFormat("union Foo\n" 6708 "{\n" 6709 " A,\n" 6710 "};", 6711 Style); 6712 verifyFormat("typedef union Foo\n" 6713 "{\n" 6714 "} Foo_t;", 6715 Style); 6716 } 6717 6718 TEST_F(FormatTest, SplitEmptyNamespace) { 6719 FormatStyle Style = getLLVMStyle(); 6720 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 6721 Style.BraceWrapping.AfterNamespace = true; 6722 Style.BraceWrapping.SplitEmptyNamespace = false; 6723 6724 verifyFormat("namespace Foo\n" 6725 "{};", 6726 Style); 6727 verifyFormat("/* something */ namespace Foo\n" 6728 "{};", 6729 Style); 6730 verifyFormat("inline namespace Foo\n" 6731 "{};", 6732 Style); 6733 verifyFormat("namespace Foo\n" 6734 "{\n" 6735 "void Bar();\n" 6736 "};", 6737 Style); 6738 } 6739 6740 TEST_F(FormatTest, NeverMergeShortRecords) { 6741 FormatStyle Style = getLLVMStyle(); 6742 6743 verifyFormat("class Foo {\n" 6744 " Foo();\n" 6745 "};", 6746 Style); 6747 verifyFormat("typedef class Foo {\n" 6748 " Foo();\n" 6749 "} Foo_t;", 6750 Style); 6751 verifyFormat("struct Foo {\n" 6752 " Foo();\n" 6753 "};", 6754 Style); 6755 verifyFormat("typedef struct Foo {\n" 6756 " Foo();\n" 6757 "} Foo_t;", 6758 Style); 6759 verifyFormat("union Foo {\n" 6760 " A,\n" 6761 "};", 6762 Style); 6763 verifyFormat("typedef union Foo {\n" 6764 " A,\n" 6765 "} Foo_t;", 6766 Style); 6767 verifyFormat("namespace Foo {\n" 6768 "void Bar();\n" 6769 "};", 6770 Style); 6771 6772 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 6773 Style.BraceWrapping.AfterClass = true; 6774 Style.BraceWrapping.AfterStruct = true; 6775 Style.BraceWrapping.AfterUnion = true; 6776 Style.BraceWrapping.AfterNamespace = true; 6777 verifyFormat("class Foo\n" 6778 "{\n" 6779 " Foo();\n" 6780 "};", 6781 Style); 6782 verifyFormat("typedef class Foo\n" 6783 "{\n" 6784 " Foo();\n" 6785 "} Foo_t;", 6786 Style); 6787 verifyFormat("struct Foo\n" 6788 "{\n" 6789 " Foo();\n" 6790 "};", 6791 Style); 6792 verifyFormat("typedef struct Foo\n" 6793 "{\n" 6794 " Foo();\n" 6795 "} Foo_t;", 6796 Style); 6797 verifyFormat("union Foo\n" 6798 "{\n" 6799 " A,\n" 6800 "};", 6801 Style); 6802 verifyFormat("typedef union Foo\n" 6803 "{\n" 6804 " A,\n" 6805 "} Foo_t;", 6806 Style); 6807 verifyFormat("namespace Foo\n" 6808 "{\n" 6809 "void Bar();\n" 6810 "};", 6811 Style); 6812 } 6813 6814 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 6815 // Elaborate type variable declarations. 6816 verifyFormat("struct foo a = {bar};\nint n;"); 6817 verifyFormat("class foo a = {bar};\nint n;"); 6818 verifyFormat("union foo a = {bar};\nint n;"); 6819 6820 // Elaborate types inside function definitions. 6821 verifyFormat("struct foo f() {}\nint n;"); 6822 verifyFormat("class foo f() {}\nint n;"); 6823 verifyFormat("union foo f() {}\nint n;"); 6824 6825 // Templates. 6826 verifyFormat("template <class X> void f() {}\nint n;"); 6827 verifyFormat("template <struct X> void f() {}\nint n;"); 6828 verifyFormat("template <union X> void f() {}\nint n;"); 6829 6830 // Actual definitions... 6831 verifyFormat("struct {\n} n;"); 6832 verifyFormat( 6833 "template <template <class T, class Y>, class Z> class X {\n} n;"); 6834 verifyFormat("union Z {\n int n;\n} x;"); 6835 verifyFormat("class MACRO Z {\n} n;"); 6836 verifyFormat("class MACRO(X) Z {\n} n;"); 6837 verifyFormat("class __attribute__(X) Z {\n} n;"); 6838 verifyFormat("class __declspec(X) Z {\n} n;"); 6839 verifyFormat("class A##B##C {\n} n;"); 6840 verifyFormat("class alignas(16) Z {\n} n;"); 6841 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;"); 6842 verifyFormat("class MACROA MACRO(X) Z {\n} n;"); 6843 6844 // Redefinition from nested context: 6845 verifyFormat("class A::B::C {\n} n;"); 6846 6847 // Template definitions. 6848 verifyFormat( 6849 "template <typename F>\n" 6850 "Matcher(const Matcher<F> &Other,\n" 6851 " typename enable_if_c<is_base_of<F, T>::value &&\n" 6852 " !is_same<F, T>::value>::type * = 0)\n" 6853 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}"); 6854 6855 // FIXME: This is still incorrectly handled at the formatter side. 6856 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};"); 6857 verifyFormat("int i = SomeFunction(a<b, a> b);"); 6858 6859 // FIXME: 6860 // This now gets parsed incorrectly as class definition. 6861 // verifyFormat("class A<int> f() {\n}\nint n;"); 6862 6863 // Elaborate types where incorrectly parsing the structural element would 6864 // break the indent. 6865 verifyFormat("if (true)\n" 6866 " class X x;\n" 6867 "else\n" 6868 " f();\n"); 6869 6870 // This is simply incomplete. Formatting is not important, but must not crash. 6871 verifyFormat("class A:"); 6872 } 6873 6874 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 6875 EXPECT_EQ("#error Leave all white!!!!! space* alone!\n", 6876 format("#error Leave all white!!!!! space* alone!\n")); 6877 EXPECT_EQ( 6878 "#warning Leave all white!!!!! space* alone!\n", 6879 format("#warning Leave all white!!!!! space* alone!\n")); 6880 EXPECT_EQ("#error 1", format(" # error 1")); 6881 EXPECT_EQ("#warning 1", format(" # warning 1")); 6882 } 6883 6884 TEST_F(FormatTest, FormatHashIfExpressions) { 6885 verifyFormat("#if AAAA && BBBB"); 6886 verifyFormat("#if (AAAA && BBBB)"); 6887 verifyFormat("#elif (AAAA && BBBB)"); 6888 // FIXME: Come up with a better indentation for #elif. 6889 verifyFormat( 6890 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" 6891 " defined(BBBBBBBB)\n" 6892 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" 6893 " defined(BBBBBBBB)\n" 6894 "#endif", 6895 getLLVMStyleWithColumns(65)); 6896 } 6897 6898 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 6899 FormatStyle AllowsMergedIf = getGoogleStyle(); 6900 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 6901 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 6902 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 6903 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 6904 EXPECT_EQ("if (true) return 42;", 6905 format("if (true)\nreturn 42;", AllowsMergedIf)); 6906 FormatStyle ShortMergedIf = AllowsMergedIf; 6907 ShortMergedIf.ColumnLimit = 25; 6908 verifyFormat("#define A \\\n" 6909 " if (true) return 42;", 6910 ShortMergedIf); 6911 verifyFormat("#define A \\\n" 6912 " f(); \\\n" 6913 " if (true)\n" 6914 "#define B", 6915 ShortMergedIf); 6916 verifyFormat("#define A \\\n" 6917 " f(); \\\n" 6918 " if (true)\n" 6919 "g();", 6920 ShortMergedIf); 6921 verifyFormat("{\n" 6922 "#ifdef A\n" 6923 " // Comment\n" 6924 " if (true) continue;\n" 6925 "#endif\n" 6926 " // Comment\n" 6927 " if (true) continue;\n" 6928 "}", 6929 ShortMergedIf); 6930 ShortMergedIf.ColumnLimit = 33; 6931 verifyFormat("#define A \\\n" 6932 " if constexpr (true) return 42;", 6933 ShortMergedIf); 6934 ShortMergedIf.ColumnLimit = 29; 6935 verifyFormat("#define A \\\n" 6936 " if (aaaaaaaaaa) return 1; \\\n" 6937 " return 2;", 6938 ShortMergedIf); 6939 ShortMergedIf.ColumnLimit = 28; 6940 verifyFormat("#define A \\\n" 6941 " if (aaaaaaaaaa) \\\n" 6942 " return 1; \\\n" 6943 " return 2;", 6944 ShortMergedIf); 6945 verifyFormat("#define A \\\n" 6946 " if constexpr (aaaaaaa) \\\n" 6947 " return 1; \\\n" 6948 " return 2;", 6949 ShortMergedIf); 6950 } 6951 6952 TEST_F(FormatTest, FormatStarDependingOnContext) { 6953 verifyFormat("void f(int *a);"); 6954 verifyFormat("void f() { f(fint * b); }"); 6955 verifyFormat("class A {\n void f(int *a);\n};"); 6956 verifyFormat("class A {\n int *a;\n};"); 6957 verifyFormat("namespace a {\n" 6958 "namespace b {\n" 6959 "class A {\n" 6960 " void f() {}\n" 6961 " int *a;\n" 6962 "};\n" 6963 "} // namespace b\n" 6964 "} // namespace a"); 6965 } 6966 6967 TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 6968 verifyFormat("while"); 6969 verifyFormat("operator"); 6970 } 6971 6972 TEST_F(FormatTest, SkipsDeeplyNestedLines) { 6973 // This code would be painfully slow to format if we didn't skip it. 6974 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 6975 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 6976 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 6977 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 6978 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 6979 "A(1, 1)\n" 6980 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x 6981 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 6982 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 6983 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 6984 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 6985 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 6986 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 6987 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 6988 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 6989 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n"); 6990 // Deeply nested part is untouched, rest is formatted. 6991 EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n", 6992 format(std::string("int i;\n") + Code + "int j;\n", 6993 getLLVMStyle(), SC_ExpectIncomplete)); 6994 } 6995 6996 //===----------------------------------------------------------------------===// 6997 // Objective-C tests. 6998 //===----------------------------------------------------------------------===// 6999 7000 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 7001 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 7002 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 7003 format("-(NSUInteger)indexOfObject:(id)anObject;")); 7004 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 7005 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 7006 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 7007 format("-(NSInteger)Method3:(id)anObject;")); 7008 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 7009 format("-(NSInteger)Method4:(id)anObject;")); 7010 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 7011 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 7012 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 7013 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 7014 EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject " 7015 "forAllCells:(BOOL)flag;", 7016 format("- (void)sendAction:(SEL)aSelector to:(id)anObject " 7017 "forAllCells:(BOOL)flag;")); 7018 7019 // Very long objectiveC method declaration. 7020 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 7021 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;"); 7022 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 7023 " inRange:(NSRange)range\n" 7024 " outRange:(NSRange)out_range\n" 7025 " outRange1:(NSRange)out_range1\n" 7026 " outRange2:(NSRange)out_range2\n" 7027 " outRange3:(NSRange)out_range3\n" 7028 " outRange4:(NSRange)out_range4\n" 7029 " outRange5:(NSRange)out_range5\n" 7030 " outRange6:(NSRange)out_range6\n" 7031 " outRange7:(NSRange)out_range7\n" 7032 " outRange8:(NSRange)out_range8\n" 7033 " outRange9:(NSRange)out_range9;"); 7034 7035 // When the function name has to be wrapped. 7036 FormatStyle Style = getLLVMStyle(); 7037 Style.IndentWrappedFunctionNames = false; 7038 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 7039 "veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 7040 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 7041 "}", 7042 Style); 7043 Style.IndentWrappedFunctionNames = true; 7044 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 7045 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 7046 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 7047 "}", 7048 Style); 7049 7050 verifyFormat("- (int)sum:(vector<int>)numbers;"); 7051 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 7052 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 7053 // protocol lists (but not for template classes): 7054 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 7055 7056 verifyFormat("- (int (*)())foo:(int (*)())f;"); 7057 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); 7058 7059 // If there's no return type (very rare in practice!), LLVM and Google style 7060 // agree. 7061 verifyFormat("- foo;"); 7062 verifyFormat("- foo:(int)f;"); 7063 verifyGoogleFormat("- foo:(int)foo;"); 7064 } 7065 7066 7067 TEST_F(FormatTest, BreaksStringLiterals) { 7068 EXPECT_EQ("\"some text \"\n" 7069 "\"other\";", 7070 format("\"some text other\";", getLLVMStyleWithColumns(12))); 7071 EXPECT_EQ("\"some text \"\n" 7072 "\"other\";", 7073 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); 7074 EXPECT_EQ( 7075 "#define A \\\n" 7076 " \"some \" \\\n" 7077 " \"text \" \\\n" 7078 " \"other\";", 7079 format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); 7080 EXPECT_EQ( 7081 "#define A \\\n" 7082 " \"so \" \\\n" 7083 " \"text \" \\\n" 7084 " \"other\";", 7085 format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); 7086 7087 EXPECT_EQ("\"some text\"", 7088 format("\"some text\"", getLLVMStyleWithColumns(1))); 7089 EXPECT_EQ("\"some text\"", 7090 format("\"some text\"", getLLVMStyleWithColumns(11))); 7091 EXPECT_EQ("\"some \"\n" 7092 "\"text\"", 7093 format("\"some text\"", getLLVMStyleWithColumns(10))); 7094 EXPECT_EQ("\"some \"\n" 7095 "\"text\"", 7096 format("\"some text\"", getLLVMStyleWithColumns(7))); 7097 EXPECT_EQ("\"some\"\n" 7098 "\" tex\"\n" 7099 "\"t\"", 7100 format("\"some text\"", getLLVMStyleWithColumns(6))); 7101 EXPECT_EQ("\"some\"\n" 7102 "\" tex\"\n" 7103 "\" and\"", 7104 format("\"some tex and\"", getLLVMStyleWithColumns(6))); 7105 EXPECT_EQ("\"some\"\n" 7106 "\"/tex\"\n" 7107 "\"/and\"", 7108 format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); 7109 7110 EXPECT_EQ("variable =\n" 7111 " \"long string \"\n" 7112 " \"literal\";", 7113 format("variable = \"long string literal\";", 7114 getLLVMStyleWithColumns(20))); 7115 7116 EXPECT_EQ("variable = f(\n" 7117 " \"long string \"\n" 7118 " \"literal\",\n" 7119 " short,\n" 7120 " loooooooooooooooooooong);", 7121 format("variable = f(\"long string literal\", short, " 7122 "loooooooooooooooooooong);", 7123 getLLVMStyleWithColumns(20))); 7124 7125 EXPECT_EQ( 7126 "f(g(\"long string \"\n" 7127 " \"literal\"),\n" 7128 " b);", 7129 format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20))); 7130 EXPECT_EQ("f(g(\"long string \"\n" 7131 " \"literal\",\n" 7132 " a),\n" 7133 " b);", 7134 format("f(g(\"long string literal\", a), b);", 7135 getLLVMStyleWithColumns(20))); 7136 EXPECT_EQ( 7137 "f(\"one two\".split(\n" 7138 " variable));", 7139 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); 7140 EXPECT_EQ("f(\"one two three four five six \"\n" 7141 " \"seven\".split(\n" 7142 " really_looooong_variable));", 7143 format("f(\"one two three four five six seven\"." 7144 "split(really_looooong_variable));", 7145 getLLVMStyleWithColumns(33))); 7146 7147 EXPECT_EQ("f(\"some \"\n" 7148 " \"text\",\n" 7149 " other);", 7150 format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); 7151 7152 // Only break as a last resort. 7153 verifyFormat( 7154 "aaaaaaaaaaaaaaaaaaaa(\n" 7155 " aaaaaaaaaaaaaaaaaaaa,\n" 7156 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); 7157 7158 EXPECT_EQ("\"splitmea\"\n" 7159 "\"trandomp\"\n" 7160 "\"oint\"", 7161 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); 7162 7163 EXPECT_EQ("\"split/\"\n" 7164 "\"pathat/\"\n" 7165 "\"slashes\"", 7166 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 7167 7168 EXPECT_EQ("\"split/\"\n" 7169 "\"pathat/\"\n" 7170 "\"slashes\"", 7171 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 7172 EXPECT_EQ("\"split at \"\n" 7173 "\"spaces/at/\"\n" 7174 "\"slashes.at.any$\"\n" 7175 "\"non-alphanumeric%\"\n" 7176 "\"1111111111characte\"\n" 7177 "\"rs\"", 7178 format("\"split at " 7179 "spaces/at/" 7180 "slashes.at." 7181 "any$non-" 7182 "alphanumeric%" 7183 "1111111111characte" 7184 "rs\"", 7185 getLLVMStyleWithColumns(20))); 7186 7187 // Verify that splitting the strings understands 7188 // Style::AlwaysBreakBeforeMultilineStrings. 7189 EXPECT_EQ( 7190 "aaaaaaaaaaaa(\n" 7191 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" 7192 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", 7193 format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa " 7194 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 7195 "aaaaaaaaaaaaaaaaaaaaaa\");", 7196 getGoogleStyle())); 7197 EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7198 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", 7199 format("return \"aaaaaaaaaaaaaaaaaaaaaa " 7200 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 7201 "aaaaaaaaaaaaaaaaaaaaaa\";", 7202 getGoogleStyle())); 7203 EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7204 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 7205 format("llvm::outs() << " 7206 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa" 7207 "aaaaaaaaaaaaaaaaaaa\";")); 7208 EXPECT_EQ("ffff(\n" 7209 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7210 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 7211 format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " 7212 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 7213 getGoogleStyle())); 7214 7215 FormatStyle Style = getLLVMStyleWithColumns(12); 7216 Style.BreakStringLiterals = false; 7217 EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style)); 7218 7219 FormatStyle AlignLeft = getLLVMStyleWithColumns(12); 7220 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 7221 EXPECT_EQ("#define A \\\n" 7222 " \"some \" \\\n" 7223 " \"text \" \\\n" 7224 " \"other\";", 7225 format("#define A \"some text other\";", AlignLeft)); 7226 } 7227 7228 TEST_F(FormatTest, FullyRemoveEmptyLines) { 7229 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80); 7230 NoEmptyLines.MaxEmptyLinesToKeep = 0; 7231 EXPECT_EQ("int i = a(b());", 7232 format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines)); 7233 } 7234 7235 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) { 7236 EXPECT_EQ( 7237 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 7238 "(\n" 7239 " \"x\t\");", 7240 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 7241 "aaaaaaa(" 7242 "\"x\t\");")); 7243 } 7244 7245 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) { 7246 EXPECT_EQ( 7247 "u8\"utf8 string \"\n" 7248 "u8\"literal\";", 7249 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16))); 7250 EXPECT_EQ( 7251 "u\"utf16 string \"\n" 7252 "u\"literal\";", 7253 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16))); 7254 EXPECT_EQ( 7255 "U\"utf32 string \"\n" 7256 "U\"literal\";", 7257 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16))); 7258 EXPECT_EQ("L\"wide string \"\n" 7259 "L\"literal\";", 7260 format("L\"wide string literal\";", getGoogleStyleWithColumns(16))); 7261 EXPECT_EQ("@\"NSString \"\n" 7262 "@\"literal\";", 7263 format("@\"NSString literal\";", getGoogleStyleWithColumns(19))); 7264 verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26)); 7265 7266 // This input makes clang-format try to split the incomplete unicode escape 7267 // sequence, which used to lead to a crasher. 7268 verifyNoCrash( 7269 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 7270 getLLVMStyleWithColumns(60)); 7271 } 7272 7273 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) { 7274 FormatStyle Style = getGoogleStyleWithColumns(15); 7275 EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style)); 7276 EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style)); 7277 EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style)); 7278 EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style)); 7279 EXPECT_EQ("u8R\"x(raw literal)x\";", 7280 format("u8R\"x(raw literal)x\";", Style)); 7281 } 7282 7283 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { 7284 FormatStyle Style = getLLVMStyleWithColumns(20); 7285 EXPECT_EQ( 7286 "_T(\"aaaaaaaaaaaaaa\")\n" 7287 "_T(\"aaaaaaaaaaaaaa\")\n" 7288 "_T(\"aaaaaaaaaaaa\")", 7289 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style)); 7290 EXPECT_EQ("f(x,\n" 7291 " _T(\"aaaaaaaaaaaa\")\n" 7292 " _T(\"aaa\"),\n" 7293 " z);", 7294 format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style)); 7295 7296 // FIXME: Handle embedded spaces in one iteration. 7297 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n" 7298 // "_T(\"aaaaaaaaaaaaa\")\n" 7299 // "_T(\"aaaaaaaaaaaaa\")\n" 7300 // "_T(\"a\")", 7301 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 7302 // getLLVMStyleWithColumns(20))); 7303 EXPECT_EQ( 7304 "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 7305 format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style)); 7306 EXPECT_EQ("f(\n" 7307 "#if !TEST\n" 7308 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 7309 "#endif\n" 7310 ");", 7311 format("f(\n" 7312 "#if !TEST\n" 7313 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 7314 "#endif\n" 7315 ");")); 7316 EXPECT_EQ("f(\n" 7317 "\n" 7318 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));", 7319 format("f(\n" 7320 "\n" 7321 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));")); 7322 } 7323 7324 TEST_F(FormatTest, BreaksStringLiteralOperands) { 7325 // In a function call with two operands, the second can be broken with no line 7326 // break before it. 7327 EXPECT_EQ("func(a, \"long long \"\n" 7328 " \"long long\");", 7329 format("func(a, \"long long long long\");", 7330 getLLVMStyleWithColumns(24))); 7331 // In a function call with three operands, the second must be broken with a 7332 // line break before it. 7333 EXPECT_EQ("func(a,\n" 7334 " \"long long long \"\n" 7335 " \"long\",\n" 7336 " c);", 7337 format("func(a, \"long long long long\", c);", 7338 getLLVMStyleWithColumns(24))); 7339 // In a function call with three operands, the third must be broken with a 7340 // line break before it. 7341 EXPECT_EQ("func(a, b,\n" 7342 " \"long long long \"\n" 7343 " \"long\");", 7344 format("func(a, b, \"long long long long\");", 7345 getLLVMStyleWithColumns(24))); 7346 // In a function call with three operands, both the second and the third must 7347 // be broken with a line break before them. 7348 EXPECT_EQ("func(a,\n" 7349 " \"long long long \"\n" 7350 " \"long\",\n" 7351 " \"long long long \"\n" 7352 " \"long\");", 7353 format("func(a, \"long long long long\", \"long long long long\");", 7354 getLLVMStyleWithColumns(24))); 7355 // In a chain of << with two operands, the second can be broken with no line 7356 // break before it. 7357 EXPECT_EQ("a << \"line line \"\n" 7358 " \"line\";", 7359 format("a << \"line line line\";", 7360 getLLVMStyleWithColumns(20))); 7361 // In a chain of << with three operands, the second can be broken with no line 7362 // break before it. 7363 EXPECT_EQ("abcde << \"line \"\n" 7364 " \"line line\"\n" 7365 " << c;", 7366 format("abcde << \"line line line\" << c;", 7367 getLLVMStyleWithColumns(20))); 7368 // In a chain of << with three operands, the third must be broken with a line 7369 // break before it. 7370 EXPECT_EQ("a << b\n" 7371 " << \"line line \"\n" 7372 " \"line\";", 7373 format("a << b << \"line line line\";", 7374 getLLVMStyleWithColumns(20))); 7375 // In a chain of << with three operands, the second can be broken with no line 7376 // break before it and the third must be broken with a line break before it. 7377 EXPECT_EQ("abcd << \"line line \"\n" 7378 " \"line\"\n" 7379 " << \"line line \"\n" 7380 " \"line\";", 7381 format("abcd << \"line line line\" << \"line line line\";", 7382 getLLVMStyleWithColumns(20))); 7383 // In a chain of binary operators with two operands, the second can be broken 7384 // with no line break before it. 7385 EXPECT_EQ("abcd + \"line line \"\n" 7386 " \"line line\";", 7387 format("abcd + \"line line line line\";", 7388 getLLVMStyleWithColumns(20))); 7389 // In a chain of binary operators with three operands, the second must be 7390 // broken with a line break before it. 7391 EXPECT_EQ("abcd +\n" 7392 " \"line line \"\n" 7393 " \"line line\" +\n" 7394 " e;", 7395 format("abcd + \"line line line line\" + e;", 7396 getLLVMStyleWithColumns(20))); 7397 // In a function call with two operands, with AlignAfterOpenBracket enabled, 7398 // the first must be broken with a line break before it. 7399 FormatStyle Style = getLLVMStyleWithColumns(25); 7400 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 7401 EXPECT_EQ("someFunction(\n" 7402 " \"long long long \"\n" 7403 " \"long\",\n" 7404 " a);", 7405 format("someFunction(\"long long long long\", a);", Style)); 7406 } 7407 7408 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { 7409 EXPECT_EQ( 7410 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 7411 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 7412 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 7413 format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 7414 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 7415 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";")); 7416 } 7417 7418 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { 7419 EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);", 7420 format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle())); 7421 EXPECT_EQ("fffffffffff(g(R\"x(\n" 7422 "multiline raw string literal xxxxxxxxxxxxxx\n" 7423 ")x\",\n" 7424 " a),\n" 7425 " b);", 7426 format("fffffffffff(g(R\"x(\n" 7427 "multiline raw string literal xxxxxxxxxxxxxx\n" 7428 ")x\", a), b);", 7429 getGoogleStyleWithColumns(20))); 7430 EXPECT_EQ("fffffffffff(\n" 7431 " g(R\"x(qqq\n" 7432 "multiline raw string literal xxxxxxxxxxxxxx\n" 7433 ")x\",\n" 7434 " a),\n" 7435 " b);", 7436 format("fffffffffff(g(R\"x(qqq\n" 7437 "multiline raw string literal xxxxxxxxxxxxxx\n" 7438 ")x\", a), b);", 7439 getGoogleStyleWithColumns(20))); 7440 7441 EXPECT_EQ("fffffffffff(R\"x(\n" 7442 "multiline raw string literal xxxxxxxxxxxxxx\n" 7443 ")x\");", 7444 format("fffffffffff(R\"x(\n" 7445 "multiline raw string literal xxxxxxxxxxxxxx\n" 7446 ")x\");", 7447 getGoogleStyleWithColumns(20))); 7448 EXPECT_EQ("fffffffffff(R\"x(\n" 7449 "multiline raw string literal xxxxxxxxxxxxxx\n" 7450 ")x\" + bbbbbb);", 7451 format("fffffffffff(R\"x(\n" 7452 "multiline raw string literal xxxxxxxxxxxxxx\n" 7453 ")x\" + bbbbbb);", 7454 getGoogleStyleWithColumns(20))); 7455 EXPECT_EQ("fffffffffff(\n" 7456 " R\"x(\n" 7457 "multiline raw string literal xxxxxxxxxxxxxx\n" 7458 ")x\" +\n" 7459 " bbbbbb);", 7460 format("fffffffffff(\n" 7461 " R\"x(\n" 7462 "multiline raw string literal xxxxxxxxxxxxxx\n" 7463 ")x\" + bbbbbb);", 7464 getGoogleStyleWithColumns(20))); 7465 } 7466 7467 TEST_F(FormatTest, SkipsUnknownStringLiterals) { 7468 verifyFormat("string a = \"unterminated;"); 7469 EXPECT_EQ("function(\"unterminated,\n" 7470 " OtherParameter);", 7471 format("function( \"unterminated,\n" 7472 " OtherParameter);")); 7473 } 7474 7475 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { 7476 FormatStyle Style = getLLVMStyle(); 7477 Style.Standard = FormatStyle::LS_Cpp03; 7478 EXPECT_EQ("#define x(_a) printf(\"foo\" _a);", 7479 format("#define x(_a) printf(\"foo\"_a);", Style)); 7480 } 7481 7482 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); } 7483 7484 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { 7485 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n" 7486 " \"ddeeefff\");", 7487 format("someFunction(\"aaabbbcccdddeeefff\");", 7488 getLLVMStyleWithColumns(25))); 7489 EXPECT_EQ("someFunction1234567890(\n" 7490 " \"aaabbbcccdddeeefff\");", 7491 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 7492 getLLVMStyleWithColumns(26))); 7493 EXPECT_EQ("someFunction1234567890(\n" 7494 " \"aaabbbcccdddeeeff\"\n" 7495 " \"f\");", 7496 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 7497 getLLVMStyleWithColumns(25))); 7498 EXPECT_EQ("someFunction1234567890(\n" 7499 " \"aaabbbcccdddeeeff\"\n" 7500 " \"f\");", 7501 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 7502 getLLVMStyleWithColumns(24))); 7503 EXPECT_EQ("someFunction(\"aaabbbcc \"\n" 7504 " \"ddde \"\n" 7505 " \"efff\");", 7506 format("someFunction(\"aaabbbcc ddde efff\");", 7507 getLLVMStyleWithColumns(25))); 7508 EXPECT_EQ("someFunction(\"aaabbbccc \"\n" 7509 " \"ddeeefff\");", 7510 format("someFunction(\"aaabbbccc ddeeefff\");", 7511 getLLVMStyleWithColumns(25))); 7512 EXPECT_EQ("someFunction1234567890(\n" 7513 " \"aaabb \"\n" 7514 " \"cccdddeeefff\");", 7515 format("someFunction1234567890(\"aaabb cccdddeeefff\");", 7516 getLLVMStyleWithColumns(25))); 7517 EXPECT_EQ("#define A \\\n" 7518 " string s = \\\n" 7519 " \"123456789\" \\\n" 7520 " \"0\"; \\\n" 7521 " int i;", 7522 format("#define A string s = \"1234567890\"; int i;", 7523 getLLVMStyleWithColumns(20))); 7524 // FIXME: Put additional penalties on breaking at non-whitespace locations. 7525 EXPECT_EQ("someFunction(\"aaabbbcc \"\n" 7526 " \"dddeeeff\"\n" 7527 " \"f\");", 7528 format("someFunction(\"aaabbbcc dddeeefff\");", 7529 getLLVMStyleWithColumns(25))); 7530 } 7531 7532 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { 7533 EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3))); 7534 EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2))); 7535 EXPECT_EQ("\"test\"\n" 7536 "\"\\n\"", 7537 format("\"test\\n\"", getLLVMStyleWithColumns(7))); 7538 EXPECT_EQ("\"tes\\\\\"\n" 7539 "\"n\"", 7540 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); 7541 EXPECT_EQ("\"\\\\\\\\\"\n" 7542 "\"\\n\"", 7543 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); 7544 EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7))); 7545 EXPECT_EQ("\"\\uff01\"\n" 7546 "\"test\"", 7547 format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); 7548 EXPECT_EQ("\"\\Uff01ff02\"", 7549 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); 7550 EXPECT_EQ("\"\\x000000000001\"\n" 7551 "\"next\"", 7552 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); 7553 EXPECT_EQ("\"\\x000000000001next\"", 7554 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); 7555 EXPECT_EQ("\"\\x000000000001\"", 7556 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); 7557 EXPECT_EQ("\"test\"\n" 7558 "\"\\000000\"\n" 7559 "\"000001\"", 7560 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); 7561 EXPECT_EQ("\"test\\000\"\n" 7562 "\"00000000\"\n" 7563 "\"1\"", 7564 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); 7565 } 7566 7567 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { 7568 verifyFormat("void f() {\n" 7569 " return g() {}\n" 7570 " void h() {}"); 7571 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n" 7572 "g();\n" 7573 "}"); 7574 } 7575 7576 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) { 7577 verifyFormat( 7578 "void f() { return C{param1, param2}.SomeCall(param1, param2); }"); 7579 } 7580 7581 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { 7582 verifyFormat("class X {\n" 7583 " void f() {\n" 7584 " }\n" 7585 "};", 7586 getLLVMStyleWithColumns(12)); 7587 } 7588 7589 TEST_F(FormatTest, ConfigurableIndentWidth) { 7590 FormatStyle EightIndent = getLLVMStyleWithColumns(18); 7591 EightIndent.IndentWidth = 8; 7592 EightIndent.ContinuationIndentWidth = 8; 7593 verifyFormat("void f() {\n" 7594 " someFunction();\n" 7595 " if (true) {\n" 7596 " f();\n" 7597 " }\n" 7598 "}", 7599 EightIndent); 7600 verifyFormat("class X {\n" 7601 " void f() {\n" 7602 " }\n" 7603 "};", 7604 EightIndent); 7605 verifyFormat("int x[] = {\n" 7606 " call(),\n" 7607 " call()};", 7608 EightIndent); 7609 } 7610 7611 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) { 7612 verifyFormat("double\n" 7613 "f();", 7614 getLLVMStyleWithColumns(8)); 7615 } 7616 7617 TEST_F(FormatTest, ConfigurableUseOfTab) { 7618 FormatStyle Tab = getLLVMStyleWithColumns(42); 7619 Tab.IndentWidth = 8; 7620 Tab.UseTab = FormatStyle::UT_Always; 7621 Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left; 7622 7623 EXPECT_EQ("if (aaaaaaaa && // q\n" 7624 " bb)\t\t// w\n" 7625 "\t;", 7626 format("if (aaaaaaaa &&// q\n" 7627 "bb)// w\n" 7628 ";", 7629 Tab)); 7630 EXPECT_EQ("if (aaa && bbb) // w\n" 7631 "\t;", 7632 format("if(aaa&&bbb)// w\n" 7633 ";", 7634 Tab)); 7635 7636 verifyFormat("class X {\n" 7637 "\tvoid f() {\n" 7638 "\t\tsomeFunction(parameter1,\n" 7639 "\t\t\t parameter2);\n" 7640 "\t}\n" 7641 "};", 7642 Tab); 7643 verifyFormat("#define A \\\n" 7644 "\tvoid f() { \\\n" 7645 "\t\tsomeFunction( \\\n" 7646 "\t\t parameter1, \\\n" 7647 "\t\t parameter2); \\\n" 7648 "\t}", 7649 Tab); 7650 7651 Tab.TabWidth = 4; 7652 Tab.IndentWidth = 8; 7653 verifyFormat("class TabWidth4Indent8 {\n" 7654 "\t\tvoid f() {\n" 7655 "\t\t\t\tsomeFunction(parameter1,\n" 7656 "\t\t\t\t\t\t\t parameter2);\n" 7657 "\t\t}\n" 7658 "};", 7659 Tab); 7660 7661 Tab.TabWidth = 4; 7662 Tab.IndentWidth = 4; 7663 verifyFormat("class TabWidth4Indent4 {\n" 7664 "\tvoid f() {\n" 7665 "\t\tsomeFunction(parameter1,\n" 7666 "\t\t\t\t\t parameter2);\n" 7667 "\t}\n" 7668 "};", 7669 Tab); 7670 7671 Tab.TabWidth = 8; 7672 Tab.IndentWidth = 4; 7673 verifyFormat("class TabWidth8Indent4 {\n" 7674 " void f() {\n" 7675 "\tsomeFunction(parameter1,\n" 7676 "\t\t parameter2);\n" 7677 " }\n" 7678 "};", 7679 Tab); 7680 7681 Tab.TabWidth = 8; 7682 Tab.IndentWidth = 8; 7683 EXPECT_EQ("/*\n" 7684 "\t a\t\tcomment\n" 7685 "\t in multiple lines\n" 7686 " */", 7687 format(" /*\t \t \n" 7688 " \t \t a\t\tcomment\t \t\n" 7689 " \t \t in multiple lines\t\n" 7690 " \t */", 7691 Tab)); 7692 7693 Tab.UseTab = FormatStyle::UT_ForIndentation; 7694 verifyFormat("{\n" 7695 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7696 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7697 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7698 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7699 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7700 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7701 "};", 7702 Tab); 7703 verifyFormat("enum AA {\n" 7704 "\ta1, // Force multiple lines\n" 7705 "\ta2,\n" 7706 "\ta3\n" 7707 "};", 7708 Tab); 7709 EXPECT_EQ("if (aaaaaaaa && // q\n" 7710 " bb) // w\n" 7711 "\t;", 7712 format("if (aaaaaaaa &&// q\n" 7713 "bb)// w\n" 7714 ";", 7715 Tab)); 7716 verifyFormat("class X {\n" 7717 "\tvoid f() {\n" 7718 "\t\tsomeFunction(parameter1,\n" 7719 "\t\t parameter2);\n" 7720 "\t}\n" 7721 "};", 7722 Tab); 7723 verifyFormat("{\n" 7724 "\tQ(\n" 7725 "\t {\n" 7726 "\t\t int a;\n" 7727 "\t\t someFunction(aaaaaaaa,\n" 7728 "\t\t bbbbbbb);\n" 7729 "\t },\n" 7730 "\t p);\n" 7731 "}", 7732 Tab); 7733 EXPECT_EQ("{\n" 7734 "\t/* aaaa\n" 7735 "\t bbbb */\n" 7736 "}", 7737 format("{\n" 7738 "/* aaaa\n" 7739 " bbbb */\n" 7740 "}", 7741 Tab)); 7742 EXPECT_EQ("{\n" 7743 "\t/*\n" 7744 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7745 "\t bbbbbbbbbbbbb\n" 7746 "\t*/\n" 7747 "}", 7748 format("{\n" 7749 "/*\n" 7750 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 7751 "*/\n" 7752 "}", 7753 Tab)); 7754 EXPECT_EQ("{\n" 7755 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7756 "\t// bbbbbbbbbbbbb\n" 7757 "}", 7758 format("{\n" 7759 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 7760 "}", 7761 Tab)); 7762 EXPECT_EQ("{\n" 7763 "\t/*\n" 7764 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7765 "\t bbbbbbbbbbbbb\n" 7766 "\t*/\n" 7767 "}", 7768 format("{\n" 7769 "\t/*\n" 7770 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 7771 "\t*/\n" 7772 "}", 7773 Tab)); 7774 EXPECT_EQ("{\n" 7775 "\t/*\n" 7776 "\n" 7777 "\t*/\n" 7778 "}", 7779 format("{\n" 7780 "\t/*\n" 7781 "\n" 7782 "\t*/\n" 7783 "}", 7784 Tab)); 7785 EXPECT_EQ("{\n" 7786 "\t/*\n" 7787 " asdf\n" 7788 "\t*/\n" 7789 "}", 7790 format("{\n" 7791 "\t/*\n" 7792 " asdf\n" 7793 "\t*/\n" 7794 "}", 7795 Tab)); 7796 7797 Tab.UseTab = FormatStyle::UT_Never; 7798 EXPECT_EQ("/*\n" 7799 " a\t\tcomment\n" 7800 " in multiple lines\n" 7801 " */", 7802 format(" /*\t \t \n" 7803 " \t \t a\t\tcomment\t \t\n" 7804 " \t \t in multiple lines\t\n" 7805 " \t */", 7806 Tab)); 7807 EXPECT_EQ("/* some\n" 7808 " comment */", 7809 format(" \t \t /* some\n" 7810 " \t \t comment */", 7811 Tab)); 7812 EXPECT_EQ("int a; /* some\n" 7813 " comment */", 7814 format(" \t \t int a; /* some\n" 7815 " \t \t comment */", 7816 Tab)); 7817 7818 EXPECT_EQ("int a; /* some\n" 7819 "comment */", 7820 format(" \t \t int\ta; /* some\n" 7821 " \t \t comment */", 7822 Tab)); 7823 EXPECT_EQ("f(\"\t\t\"); /* some\n" 7824 " comment */", 7825 format(" \t \t f(\"\t\t\"); /* some\n" 7826 " \t \t comment */", 7827 Tab)); 7828 EXPECT_EQ("{\n" 7829 " /*\n" 7830 " * Comment\n" 7831 " */\n" 7832 " int i;\n" 7833 "}", 7834 format("{\n" 7835 "\t/*\n" 7836 "\t * Comment\n" 7837 "\t */\n" 7838 "\t int i;\n" 7839 "}")); 7840 7841 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 7842 Tab.TabWidth = 8; 7843 Tab.IndentWidth = 8; 7844 EXPECT_EQ("if (aaaaaaaa && // q\n" 7845 " bb) // w\n" 7846 "\t;", 7847 format("if (aaaaaaaa &&// q\n" 7848 "bb)// w\n" 7849 ";", 7850 Tab)); 7851 EXPECT_EQ("if (aaa && bbb) // w\n" 7852 "\t;", 7853 format("if(aaa&&bbb)// w\n" 7854 ";", 7855 Tab)); 7856 verifyFormat("class X {\n" 7857 "\tvoid f() {\n" 7858 "\t\tsomeFunction(parameter1,\n" 7859 "\t\t\t parameter2);\n" 7860 "\t}\n" 7861 "};", 7862 Tab); 7863 verifyFormat("#define A \\\n" 7864 "\tvoid f() { \\\n" 7865 "\t\tsomeFunction( \\\n" 7866 "\t\t parameter1, \\\n" 7867 "\t\t parameter2); \\\n" 7868 "\t}", 7869 Tab); 7870 Tab.TabWidth = 4; 7871 Tab.IndentWidth = 8; 7872 verifyFormat("class TabWidth4Indent8 {\n" 7873 "\t\tvoid f() {\n" 7874 "\t\t\t\tsomeFunction(parameter1,\n" 7875 "\t\t\t\t\t\t\t parameter2);\n" 7876 "\t\t}\n" 7877 "};", 7878 Tab); 7879 Tab.TabWidth = 4; 7880 Tab.IndentWidth = 4; 7881 verifyFormat("class TabWidth4Indent4 {\n" 7882 "\tvoid f() {\n" 7883 "\t\tsomeFunction(parameter1,\n" 7884 "\t\t\t\t\t parameter2);\n" 7885 "\t}\n" 7886 "};", 7887 Tab); 7888 Tab.TabWidth = 8; 7889 Tab.IndentWidth = 4; 7890 verifyFormat("class TabWidth8Indent4 {\n" 7891 " void f() {\n" 7892 "\tsomeFunction(parameter1,\n" 7893 "\t\t parameter2);\n" 7894 " }\n" 7895 "};", 7896 Tab); 7897 Tab.TabWidth = 8; 7898 Tab.IndentWidth = 8; 7899 EXPECT_EQ("/*\n" 7900 "\t a\t\tcomment\n" 7901 "\t in multiple lines\n" 7902 " */", 7903 format(" /*\t \t \n" 7904 " \t \t a\t\tcomment\t \t\n" 7905 " \t \t in multiple lines\t\n" 7906 " \t */", 7907 Tab)); 7908 verifyFormat("{\n" 7909 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7910 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7911 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7912 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7913 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7914 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7915 "};", 7916 Tab); 7917 verifyFormat("enum AA {\n" 7918 "\ta1, // Force multiple lines\n" 7919 "\ta2,\n" 7920 "\ta3\n" 7921 "};", 7922 Tab); 7923 EXPECT_EQ("if (aaaaaaaa && // q\n" 7924 " bb) // w\n" 7925 "\t;", 7926 format("if (aaaaaaaa &&// q\n" 7927 "bb)// w\n" 7928 ";", 7929 Tab)); 7930 verifyFormat("class X {\n" 7931 "\tvoid f() {\n" 7932 "\t\tsomeFunction(parameter1,\n" 7933 "\t\t\t parameter2);\n" 7934 "\t}\n" 7935 "};", 7936 Tab); 7937 verifyFormat("{\n" 7938 "\tQ(\n" 7939 "\t {\n" 7940 "\t\t int a;\n" 7941 "\t\t someFunction(aaaaaaaa,\n" 7942 "\t\t\t\t bbbbbbb);\n" 7943 "\t },\n" 7944 "\t p);\n" 7945 "}", 7946 Tab); 7947 EXPECT_EQ("{\n" 7948 "\t/* aaaa\n" 7949 "\t bbbb */\n" 7950 "}", 7951 format("{\n" 7952 "/* aaaa\n" 7953 " bbbb */\n" 7954 "}", 7955 Tab)); 7956 EXPECT_EQ("{\n" 7957 "\t/*\n" 7958 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7959 "\t bbbbbbbbbbbbb\n" 7960 "\t*/\n" 7961 "}", 7962 format("{\n" 7963 "/*\n" 7964 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 7965 "*/\n" 7966 "}", 7967 Tab)); 7968 EXPECT_EQ("{\n" 7969 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7970 "\t// bbbbbbbbbbbbb\n" 7971 "}", 7972 format("{\n" 7973 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 7974 "}", 7975 Tab)); 7976 EXPECT_EQ("{\n" 7977 "\t/*\n" 7978 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7979 "\t bbbbbbbbbbbbb\n" 7980 "\t*/\n" 7981 "}", 7982 format("{\n" 7983 "\t/*\n" 7984 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 7985 "\t*/\n" 7986 "}", 7987 Tab)); 7988 EXPECT_EQ("{\n" 7989 "\t/*\n" 7990 "\n" 7991 "\t*/\n" 7992 "}", 7993 format("{\n" 7994 "\t/*\n" 7995 "\n" 7996 "\t*/\n" 7997 "}", 7998 Tab)); 7999 EXPECT_EQ("{\n" 8000 "\t/*\n" 8001 " asdf\n" 8002 "\t*/\n" 8003 "}", 8004 format("{\n" 8005 "\t/*\n" 8006 " asdf\n" 8007 "\t*/\n" 8008 "}", 8009 Tab)); 8010 EXPECT_EQ("/*\n" 8011 "\t a\t\tcomment\n" 8012 "\t in multiple lines\n" 8013 " */", 8014 format(" /*\t \t \n" 8015 " \t \t a\t\tcomment\t \t\n" 8016 " \t \t in multiple lines\t\n" 8017 " \t */", 8018 Tab)); 8019 EXPECT_EQ("/* some\n" 8020 " comment */", 8021 format(" \t \t /* some\n" 8022 " \t \t comment */", 8023 Tab)); 8024 EXPECT_EQ("int a; /* some\n" 8025 " comment */", 8026 format(" \t \t int a; /* some\n" 8027 " \t \t comment */", 8028 Tab)); 8029 EXPECT_EQ("int a; /* some\n" 8030 "comment */", 8031 format(" \t \t int\ta; /* some\n" 8032 " \t \t comment */", 8033 Tab)); 8034 EXPECT_EQ("f(\"\t\t\"); /* some\n" 8035 " comment */", 8036 format(" \t \t f(\"\t\t\"); /* some\n" 8037 " \t \t comment */", 8038 Tab)); 8039 EXPECT_EQ("{\n" 8040 " /*\n" 8041 " * Comment\n" 8042 " */\n" 8043 " int i;\n" 8044 "}", 8045 format("{\n" 8046 "\t/*\n" 8047 "\t * Comment\n" 8048 "\t */\n" 8049 "\t int i;\n" 8050 "}")); 8051 Tab.AlignConsecutiveAssignments = true; 8052 Tab.AlignConsecutiveDeclarations = true; 8053 Tab.TabWidth = 4; 8054 Tab.IndentWidth = 4; 8055 verifyFormat("class Assign {\n" 8056 "\tvoid f() {\n" 8057 "\t\tint x = 123;\n" 8058 "\t\tint random = 4;\n" 8059 "\t\tstd::string alphabet =\n" 8060 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 8061 "\t}\n" 8062 "};", 8063 Tab); 8064 } 8065 8066 TEST_F(FormatTest, CalculatesOriginalColumn) { 8067 EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8068 "q\"; /* some\n" 8069 " comment */", 8070 format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8071 "q\"; /* some\n" 8072 " comment */", 8073 getLLVMStyle())); 8074 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 8075 "/* some\n" 8076 " comment */", 8077 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 8078 " /* some\n" 8079 " comment */", 8080 getLLVMStyle())); 8081 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8082 "qqq\n" 8083 "/* some\n" 8084 " comment */", 8085 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8086 "qqq\n" 8087 " /* some\n" 8088 " comment */", 8089 getLLVMStyle())); 8090 EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8091 "wwww; /* some\n" 8092 " comment */", 8093 format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8094 "wwww; /* some\n" 8095 " comment */", 8096 getLLVMStyle())); 8097 } 8098 8099 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { 8100 FormatStyle NoSpace = getLLVMStyle(); 8101 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never; 8102 8103 verifyFormat("while(true)\n" 8104 " continue;", 8105 NoSpace); 8106 verifyFormat("for(;;)\n" 8107 " continue;", 8108 NoSpace); 8109 verifyFormat("if(true)\n" 8110 " f();\n" 8111 "else if(true)\n" 8112 " f();", 8113 NoSpace); 8114 verifyFormat("do {\n" 8115 " do_something();\n" 8116 "} while(something());", 8117 NoSpace); 8118 verifyFormat("switch(x) {\n" 8119 "default:\n" 8120 " break;\n" 8121 "}", 8122 NoSpace); 8123 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace); 8124 verifyFormat("size_t x = sizeof(x);", NoSpace); 8125 verifyFormat("auto f(int x) -> decltype(x);", NoSpace); 8126 verifyFormat("int f(T x) noexcept(x.create());", NoSpace); 8127 verifyFormat("alignas(128) char a[128];", NoSpace); 8128 verifyFormat("size_t x = alignof(MyType);", NoSpace); 8129 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace); 8130 verifyFormat("int f() throw(Deprecated);", NoSpace); 8131 verifyFormat("typedef void (*cb)(int);", NoSpace); 8132 verifyFormat("T A::operator()();", NoSpace); 8133 verifyFormat("X A::operator++(T);", NoSpace); 8134 8135 FormatStyle Space = getLLVMStyle(); 8136 Space.SpaceBeforeParens = FormatStyle::SBPO_Always; 8137 8138 verifyFormat("int f ();", Space); 8139 verifyFormat("void f (int a, T b) {\n" 8140 " while (true)\n" 8141 " continue;\n" 8142 "}", 8143 Space); 8144 verifyFormat("if (true)\n" 8145 " f ();\n" 8146 "else if (true)\n" 8147 " f ();", 8148 Space); 8149 verifyFormat("do {\n" 8150 " do_something ();\n" 8151 "} while (something ());", 8152 Space); 8153 verifyFormat("switch (x) {\n" 8154 "default:\n" 8155 " break;\n" 8156 "}", 8157 Space); 8158 verifyFormat("A::A () : a (1) {}", Space); 8159 verifyFormat("void f () __attribute__ ((asdf));", Space); 8160 verifyFormat("*(&a + 1);\n" 8161 "&((&a)[1]);\n" 8162 "a[(b + c) * d];\n" 8163 "(((a + 1) * 2) + 3) * 4;", 8164 Space); 8165 verifyFormat("#define A(x) x", Space); 8166 verifyFormat("#define A (x) x", Space); 8167 verifyFormat("#if defined(x)\n" 8168 "#endif", 8169 Space); 8170 verifyFormat("auto i = std::make_unique<int> (5);", Space); 8171 verifyFormat("size_t x = sizeof (x);", Space); 8172 verifyFormat("auto f (int x) -> decltype (x);", Space); 8173 verifyFormat("int f (T x) noexcept (x.create ());", Space); 8174 verifyFormat("alignas (128) char a[128];", Space); 8175 verifyFormat("size_t x = alignof (MyType);", Space); 8176 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space); 8177 verifyFormat("int f () throw (Deprecated);", Space); 8178 verifyFormat("typedef void (*cb) (int);", Space); 8179 verifyFormat("T A::operator() ();", Space); 8180 verifyFormat("X A::operator++ (T);", Space); 8181 } 8182 8183 TEST_F(FormatTest, ConfigurableSpacesInParentheses) { 8184 FormatStyle Spaces = getLLVMStyle(); 8185 8186 Spaces.SpacesInParentheses = true; 8187 verifyFormat("call( x, y, z );", Spaces); 8188 verifyFormat("call();", Spaces); 8189 verifyFormat("std::function<void( int, int )> callback;", Spaces); 8190 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }", 8191 Spaces); 8192 verifyFormat("while ( (bool)1 )\n" 8193 " continue;", 8194 Spaces); 8195 verifyFormat("for ( ;; )\n" 8196 " continue;", 8197 Spaces); 8198 verifyFormat("if ( true )\n" 8199 " f();\n" 8200 "else if ( true )\n" 8201 " f();", 8202 Spaces); 8203 verifyFormat("do {\n" 8204 " do_something( (int)i );\n" 8205 "} while ( something() );", 8206 Spaces); 8207 verifyFormat("switch ( x ) {\n" 8208 "default:\n" 8209 " break;\n" 8210 "}", 8211 Spaces); 8212 8213 Spaces.SpacesInParentheses = false; 8214 Spaces.SpacesInCStyleCastParentheses = true; 8215 verifyFormat("Type *A = ( Type * )P;", Spaces); 8216 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces); 8217 verifyFormat("x = ( int32 )y;", Spaces); 8218 verifyFormat("int a = ( int )(2.0f);", Spaces); 8219 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces); 8220 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces); 8221 verifyFormat("#define x (( int )-1)", Spaces); 8222 8223 // Run the first set of tests again with: 8224 Spaces.SpacesInParentheses = false; 8225 Spaces.SpaceInEmptyParentheses = true; 8226 Spaces.SpacesInCStyleCastParentheses = true; 8227 verifyFormat("call(x, y, z);", Spaces); 8228 verifyFormat("call( );", Spaces); 8229 verifyFormat("std::function<void(int, int)> callback;", Spaces); 8230 verifyFormat("while (( bool )1)\n" 8231 " continue;", 8232 Spaces); 8233 verifyFormat("for (;;)\n" 8234 " continue;", 8235 Spaces); 8236 verifyFormat("if (true)\n" 8237 " f( );\n" 8238 "else if (true)\n" 8239 " f( );", 8240 Spaces); 8241 verifyFormat("do {\n" 8242 " do_something(( int )i);\n" 8243 "} while (something( ));", 8244 Spaces); 8245 verifyFormat("switch (x) {\n" 8246 "default:\n" 8247 " break;\n" 8248 "}", 8249 Spaces); 8250 8251 // Run the first set of tests again with: 8252 Spaces.SpaceAfterCStyleCast = true; 8253 verifyFormat("call(x, y, z);", Spaces); 8254 verifyFormat("call( );", Spaces); 8255 verifyFormat("std::function<void(int, int)> callback;", Spaces); 8256 verifyFormat("while (( bool ) 1)\n" 8257 " continue;", 8258 Spaces); 8259 verifyFormat("for (;;)\n" 8260 " continue;", 8261 Spaces); 8262 verifyFormat("if (true)\n" 8263 " f( );\n" 8264 "else if (true)\n" 8265 " f( );", 8266 Spaces); 8267 verifyFormat("do {\n" 8268 " do_something(( int ) i);\n" 8269 "} while (something( ));", 8270 Spaces); 8271 verifyFormat("switch (x) {\n" 8272 "default:\n" 8273 " break;\n" 8274 "}", 8275 Spaces); 8276 8277 // Run subset of tests again with: 8278 Spaces.SpacesInCStyleCastParentheses = false; 8279 Spaces.SpaceAfterCStyleCast = true; 8280 verifyFormat("while ((bool) 1)\n" 8281 " continue;", 8282 Spaces); 8283 verifyFormat("do {\n" 8284 " do_something((int) i);\n" 8285 "} while (something( ));", 8286 Spaces); 8287 } 8288 8289 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) { 8290 verifyFormat("int a[5];"); 8291 verifyFormat("a[3] += 42;"); 8292 8293 FormatStyle Spaces = getLLVMStyle(); 8294 Spaces.SpacesInSquareBrackets = true; 8295 // Lambdas unchanged. 8296 verifyFormat("int c = []() -> int { return 2; }();\n", Spaces); 8297 verifyFormat("return [i, args...] {};", Spaces); 8298 8299 // Not lambdas. 8300 verifyFormat("int a[ 5 ];", Spaces); 8301 verifyFormat("a[ 3 ] += 42;", Spaces); 8302 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces); 8303 verifyFormat("double &operator[](int i) { return 0; }\n" 8304 "int i;", 8305 Spaces); 8306 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces); 8307 verifyFormat("int i = a[ a ][ a ]->f();", Spaces); 8308 verifyFormat("int i = (*b)[ a ]->f();", Spaces); 8309 } 8310 8311 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) { 8312 verifyFormat("int a = 5;"); 8313 verifyFormat("a += 42;"); 8314 verifyFormat("a or_eq 8;"); 8315 8316 FormatStyle Spaces = getLLVMStyle(); 8317 Spaces.SpaceBeforeAssignmentOperators = false; 8318 verifyFormat("int a= 5;", Spaces); 8319 verifyFormat("a+= 42;", Spaces); 8320 verifyFormat("a or_eq 8;", Spaces); 8321 } 8322 8323 TEST_F(FormatTest, AlignConsecutiveAssignments) { 8324 FormatStyle Alignment = getLLVMStyle(); 8325 Alignment.AlignConsecutiveAssignments = false; 8326 verifyFormat("int a = 5;\n" 8327 "int oneTwoThree = 123;", 8328 Alignment); 8329 verifyFormat("int a = 5;\n" 8330 "int oneTwoThree = 123;", 8331 Alignment); 8332 8333 Alignment.AlignConsecutiveAssignments = true; 8334 verifyFormat("int a = 5;\n" 8335 "int oneTwoThree = 123;", 8336 Alignment); 8337 verifyFormat("int a = method();\n" 8338 "int oneTwoThree = 133;", 8339 Alignment); 8340 verifyFormat("a &= 5;\n" 8341 "bcd *= 5;\n" 8342 "ghtyf += 5;\n" 8343 "dvfvdb -= 5;\n" 8344 "a /= 5;\n" 8345 "vdsvsv %= 5;\n" 8346 "sfdbddfbdfbb ^= 5;\n" 8347 "dvsdsv |= 5;\n" 8348 "int dsvvdvsdvvv = 123;", 8349 Alignment); 8350 verifyFormat("int i = 1, j = 10;\n" 8351 "something = 2000;", 8352 Alignment); 8353 verifyFormat("something = 2000;\n" 8354 "int i = 1, j = 10;\n", 8355 Alignment); 8356 verifyFormat("something = 2000;\n" 8357 "another = 911;\n" 8358 "int i = 1, j = 10;\n" 8359 "oneMore = 1;\n" 8360 "i = 2;", 8361 Alignment); 8362 verifyFormat("int a = 5;\n" 8363 "int one = 1;\n" 8364 "method();\n" 8365 "int oneTwoThree = 123;\n" 8366 "int oneTwo = 12;", 8367 Alignment); 8368 verifyFormat("int oneTwoThree = 123;\n" 8369 "int oneTwo = 12;\n" 8370 "method();\n", 8371 Alignment); 8372 verifyFormat("int oneTwoThree = 123; // comment\n" 8373 "int oneTwo = 12; // comment", 8374 Alignment); 8375 EXPECT_EQ("int a = 5;\n" 8376 "\n" 8377 "int oneTwoThree = 123;", 8378 format("int a = 5;\n" 8379 "\n" 8380 "int oneTwoThree= 123;", 8381 Alignment)); 8382 EXPECT_EQ("int a = 5;\n" 8383 "int one = 1;\n" 8384 "\n" 8385 "int oneTwoThree = 123;", 8386 format("int a = 5;\n" 8387 "int one = 1;\n" 8388 "\n" 8389 "int oneTwoThree = 123;", 8390 Alignment)); 8391 EXPECT_EQ("int a = 5;\n" 8392 "int one = 1;\n" 8393 "\n" 8394 "int oneTwoThree = 123;\n" 8395 "int oneTwo = 12;", 8396 format("int a = 5;\n" 8397 "int one = 1;\n" 8398 "\n" 8399 "int oneTwoThree = 123;\n" 8400 "int oneTwo = 12;", 8401 Alignment)); 8402 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 8403 verifyFormat("#define A \\\n" 8404 " int aaaa = 12; \\\n" 8405 " int b = 23; \\\n" 8406 " int ccc = 234; \\\n" 8407 " int dddddddddd = 2345;", 8408 Alignment); 8409 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 8410 verifyFormat("#define A \\\n" 8411 " int aaaa = 12; \\\n" 8412 " int b = 23; \\\n" 8413 " int ccc = 234; \\\n" 8414 " int dddddddddd = 2345;", 8415 Alignment); 8416 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 8417 verifyFormat("#define A " 8418 " \\\n" 8419 " int aaaa = 12; " 8420 " \\\n" 8421 " int b = 23; " 8422 " \\\n" 8423 " int ccc = 234; " 8424 " \\\n" 8425 " int dddddddddd = 2345;", 8426 Alignment); 8427 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 8428 "k = 4, int l = 5,\n" 8429 " int m = 6) {\n" 8430 " int j = 10;\n" 8431 " otherThing = 1;\n" 8432 "}", 8433 Alignment); 8434 verifyFormat("void SomeFunction(int parameter = 0) {\n" 8435 " int i = 1;\n" 8436 " int j = 2;\n" 8437 " int big = 10000;\n" 8438 "}", 8439 Alignment); 8440 verifyFormat("class C {\n" 8441 "public:\n" 8442 " int i = 1;\n" 8443 " virtual void f() = 0;\n" 8444 "};", 8445 Alignment); 8446 verifyFormat("int i = 1;\n" 8447 "if (SomeType t = getSomething()) {\n" 8448 "}\n" 8449 "int j = 2;\n" 8450 "int big = 10000;", 8451 Alignment); 8452 verifyFormat("int j = 7;\n" 8453 "for (int k = 0; k < N; ++k) {\n" 8454 "}\n" 8455 "int j = 2;\n" 8456 "int big = 10000;\n" 8457 "}", 8458 Alignment); 8459 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 8460 verifyFormat("int i = 1;\n" 8461 "LooooooooooongType loooooooooooooooooooooongVariable\n" 8462 " = someLooooooooooooooooongFunction();\n" 8463 "int j = 2;", 8464 Alignment); 8465 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 8466 verifyFormat("int i = 1;\n" 8467 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 8468 " someLooooooooooooooooongFunction();\n" 8469 "int j = 2;", 8470 Alignment); 8471 8472 verifyFormat("auto lambda = []() {\n" 8473 " auto i = 0;\n" 8474 " return 0;\n" 8475 "};\n" 8476 "int i = 0;\n" 8477 "auto v = type{\n" 8478 " i = 1, //\n" 8479 " (i = 2), //\n" 8480 " i = 3 //\n" 8481 "};", 8482 Alignment); 8483 8484 verifyFormat( 8485 "int i = 1;\n" 8486 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 8487 " loooooooooooooooooooooongParameterB);\n" 8488 "int j = 2;", 8489 Alignment); 8490 8491 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 8492 " typename B = very_long_type_name_1,\n" 8493 " typename T_2 = very_long_type_name_2>\n" 8494 "auto foo() {}\n", 8495 Alignment); 8496 verifyFormat("int a, b = 1;\n" 8497 "int c = 2;\n" 8498 "int dd = 3;\n", 8499 Alignment); 8500 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 8501 "float b[1][] = {{3.f}};\n", 8502 Alignment); 8503 verifyFormat("for (int i = 0; i < 1; i++)\n" 8504 " int x = 1;\n", 8505 Alignment); 8506 verifyFormat("for (i = 0; i < 1; i++)\n" 8507 " x = 1;\n" 8508 "y = 1;\n", 8509 Alignment); 8510 } 8511 8512 TEST_F(FormatTest, AlignConsecutiveDeclarations) { 8513 FormatStyle Alignment = getLLVMStyle(); 8514 Alignment.AlignConsecutiveDeclarations = false; 8515 verifyFormat("float const a = 5;\n" 8516 "int oneTwoThree = 123;", 8517 Alignment); 8518 verifyFormat("int a = 5;\n" 8519 "float const oneTwoThree = 123;", 8520 Alignment); 8521 8522 Alignment.AlignConsecutiveDeclarations = true; 8523 verifyFormat("float const a = 5;\n" 8524 "int oneTwoThree = 123;", 8525 Alignment); 8526 verifyFormat("int a = method();\n" 8527 "float const oneTwoThree = 133;", 8528 Alignment); 8529 verifyFormat("int i = 1, j = 10;\n" 8530 "something = 2000;", 8531 Alignment); 8532 verifyFormat("something = 2000;\n" 8533 "int i = 1, j = 10;\n", 8534 Alignment); 8535 verifyFormat("float something = 2000;\n" 8536 "double another = 911;\n" 8537 "int i = 1, j = 10;\n" 8538 "const int *oneMore = 1;\n" 8539 "unsigned i = 2;", 8540 Alignment); 8541 verifyFormat("float a = 5;\n" 8542 "int one = 1;\n" 8543 "method();\n" 8544 "const double oneTwoThree = 123;\n" 8545 "const unsigned int oneTwo = 12;", 8546 Alignment); 8547 verifyFormat("int oneTwoThree{0}; // comment\n" 8548 "unsigned oneTwo; // comment", 8549 Alignment); 8550 EXPECT_EQ("float const a = 5;\n" 8551 "\n" 8552 "int oneTwoThree = 123;", 8553 format("float const a = 5;\n" 8554 "\n" 8555 "int oneTwoThree= 123;", 8556 Alignment)); 8557 EXPECT_EQ("float a = 5;\n" 8558 "int one = 1;\n" 8559 "\n" 8560 "unsigned oneTwoThree = 123;", 8561 format("float a = 5;\n" 8562 "int one = 1;\n" 8563 "\n" 8564 "unsigned oneTwoThree = 123;", 8565 Alignment)); 8566 EXPECT_EQ("float a = 5;\n" 8567 "int one = 1;\n" 8568 "\n" 8569 "unsigned oneTwoThree = 123;\n" 8570 "int oneTwo = 12;", 8571 format("float a = 5;\n" 8572 "int one = 1;\n" 8573 "\n" 8574 "unsigned oneTwoThree = 123;\n" 8575 "int oneTwo = 12;", 8576 Alignment)); 8577 // Function prototype alignment 8578 verifyFormat("int a();\n" 8579 "double b();", 8580 Alignment); 8581 verifyFormat("int a(int x);\n" 8582 "double b();", 8583 Alignment); 8584 unsigned OldColumnLimit = Alignment.ColumnLimit; 8585 // We need to set ColumnLimit to zero, in order to stress nested alignments, 8586 // otherwise the function parameters will be re-flowed onto a single line. 8587 Alignment.ColumnLimit = 0; 8588 EXPECT_EQ("int a(int x,\n" 8589 " float y);\n" 8590 "double b(int x,\n" 8591 " double y);", 8592 format("int a(int x,\n" 8593 " float y);\n" 8594 "double b(int x,\n" 8595 " double y);", 8596 Alignment)); 8597 // This ensures that function parameters of function declarations are 8598 // correctly indented when their owning functions are indented. 8599 // The failure case here is for 'double y' to not be indented enough. 8600 EXPECT_EQ("double a(int x);\n" 8601 "int b(int y,\n" 8602 " double z);", 8603 format("double a(int x);\n" 8604 "int b(int y,\n" 8605 " double z);", 8606 Alignment)); 8607 // Set ColumnLimit low so that we induce wrapping immediately after 8608 // the function name and opening paren. 8609 Alignment.ColumnLimit = 13; 8610 verifyFormat("int function(\n" 8611 " int x,\n" 8612 " bool y);", 8613 Alignment); 8614 Alignment.ColumnLimit = OldColumnLimit; 8615 // Ensure function pointers don't screw up recursive alignment 8616 verifyFormat("int a(int x, void (*fp)(int y));\n" 8617 "double b();", 8618 Alignment); 8619 Alignment.AlignConsecutiveAssignments = true; 8620 // Ensure recursive alignment is broken by function braces, so that the 8621 // "a = 1" does not align with subsequent assignments inside the function 8622 // body. 8623 verifyFormat("int func(int a = 1) {\n" 8624 " int b = 2;\n" 8625 " int cc = 3;\n" 8626 "}", 8627 Alignment); 8628 verifyFormat("float something = 2000;\n" 8629 "double another = 911;\n" 8630 "int i = 1, j = 10;\n" 8631 "const int *oneMore = 1;\n" 8632 "unsigned i = 2;", 8633 Alignment); 8634 verifyFormat("int oneTwoThree = {0}; // comment\n" 8635 "unsigned oneTwo = 0; // comment", 8636 Alignment); 8637 // Make sure that scope is correctly tracked, in the absence of braces 8638 verifyFormat("for (int i = 0; i < n; i++)\n" 8639 " j = i;\n" 8640 "double x = 1;\n", 8641 Alignment); 8642 verifyFormat("if (int i = 0)\n" 8643 " j = i;\n" 8644 "double x = 1;\n", 8645 Alignment); 8646 // Ensure operator[] and operator() are comprehended 8647 verifyFormat("struct test {\n" 8648 " long long int foo();\n" 8649 " int operator[](int a);\n" 8650 " double bar();\n" 8651 "};\n", 8652 Alignment); 8653 verifyFormat("struct test {\n" 8654 " long long int foo();\n" 8655 " int operator()(int a);\n" 8656 " double bar();\n" 8657 "};\n", 8658 Alignment); 8659 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 8660 " int const i = 1;\n" 8661 " int * j = 2;\n" 8662 " int big = 10000;\n" 8663 "\n" 8664 " unsigned oneTwoThree = 123;\n" 8665 " int oneTwo = 12;\n" 8666 " method();\n" 8667 " float k = 2;\n" 8668 " int ll = 10000;\n" 8669 "}", 8670 format("void SomeFunction(int parameter= 0) {\n" 8671 " int const i= 1;\n" 8672 " int *j=2;\n" 8673 " int big = 10000;\n" 8674 "\n" 8675 "unsigned oneTwoThree =123;\n" 8676 "int oneTwo = 12;\n" 8677 " method();\n" 8678 "float k= 2;\n" 8679 "int ll=10000;\n" 8680 "}", 8681 Alignment)); 8682 Alignment.AlignConsecutiveAssignments = false; 8683 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 8684 verifyFormat("#define A \\\n" 8685 " int aaaa = 12; \\\n" 8686 " float b = 23; \\\n" 8687 " const int ccc = 234; \\\n" 8688 " unsigned dddddddddd = 2345;", 8689 Alignment); 8690 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 8691 verifyFormat("#define A \\\n" 8692 " int aaaa = 12; \\\n" 8693 " float b = 23; \\\n" 8694 " const int ccc = 234; \\\n" 8695 " unsigned dddddddddd = 2345;", 8696 Alignment); 8697 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 8698 Alignment.ColumnLimit = 30; 8699 verifyFormat("#define A \\\n" 8700 " int aaaa = 12; \\\n" 8701 " float b = 23; \\\n" 8702 " const int ccc = 234; \\\n" 8703 " int dddddddddd = 2345;", 8704 Alignment); 8705 Alignment.ColumnLimit = 80; 8706 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 8707 "k = 4, int l = 5,\n" 8708 " int m = 6) {\n" 8709 " const int j = 10;\n" 8710 " otherThing = 1;\n" 8711 "}", 8712 Alignment); 8713 verifyFormat("void SomeFunction(int parameter = 0) {\n" 8714 " int const i = 1;\n" 8715 " int * j = 2;\n" 8716 " int big = 10000;\n" 8717 "}", 8718 Alignment); 8719 verifyFormat("class C {\n" 8720 "public:\n" 8721 " int i = 1;\n" 8722 " virtual void f() = 0;\n" 8723 "};", 8724 Alignment); 8725 verifyFormat("float i = 1;\n" 8726 "if (SomeType t = getSomething()) {\n" 8727 "}\n" 8728 "const unsigned j = 2;\n" 8729 "int big = 10000;", 8730 Alignment); 8731 verifyFormat("float j = 7;\n" 8732 "for (int k = 0; k < N; ++k) {\n" 8733 "}\n" 8734 "unsigned j = 2;\n" 8735 "int big = 10000;\n" 8736 "}", 8737 Alignment); 8738 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 8739 verifyFormat("float i = 1;\n" 8740 "LooooooooooongType loooooooooooooooooooooongVariable\n" 8741 " = someLooooooooooooooooongFunction();\n" 8742 "int j = 2;", 8743 Alignment); 8744 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 8745 verifyFormat("int i = 1;\n" 8746 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 8747 " someLooooooooooooooooongFunction();\n" 8748 "int j = 2;", 8749 Alignment); 8750 8751 Alignment.AlignConsecutiveAssignments = true; 8752 verifyFormat("auto lambda = []() {\n" 8753 " auto ii = 0;\n" 8754 " float j = 0;\n" 8755 " return 0;\n" 8756 "};\n" 8757 "int i = 0;\n" 8758 "float i2 = 0;\n" 8759 "auto v = type{\n" 8760 " i = 1, //\n" 8761 " (i = 2), //\n" 8762 " i = 3 //\n" 8763 "};", 8764 Alignment); 8765 Alignment.AlignConsecutiveAssignments = false; 8766 8767 verifyFormat( 8768 "int i = 1;\n" 8769 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 8770 " loooooooooooooooooooooongParameterB);\n" 8771 "int j = 2;", 8772 Alignment); 8773 8774 // Test interactions with ColumnLimit and AlignConsecutiveAssignments: 8775 // We expect declarations and assignments to align, as long as it doesn't 8776 // exceed the column limit, starting a new alignment sequence whenever it 8777 // happens. 8778 Alignment.AlignConsecutiveAssignments = true; 8779 Alignment.ColumnLimit = 30; 8780 verifyFormat("float ii = 1;\n" 8781 "unsigned j = 2;\n" 8782 "int someVerylongVariable = 1;\n" 8783 "AnotherLongType ll = 123456;\n" 8784 "VeryVeryLongType k = 2;\n" 8785 "int myvar = 1;", 8786 Alignment); 8787 Alignment.ColumnLimit = 80; 8788 Alignment.AlignConsecutiveAssignments = false; 8789 8790 verifyFormat( 8791 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n" 8792 " typename LongType, typename B>\n" 8793 "auto foo() {}\n", 8794 Alignment); 8795 verifyFormat("float a, b = 1;\n" 8796 "int c = 2;\n" 8797 "int dd = 3;\n", 8798 Alignment); 8799 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 8800 "float b[1][] = {{3.f}};\n", 8801 Alignment); 8802 Alignment.AlignConsecutiveAssignments = true; 8803 verifyFormat("float a, b = 1;\n" 8804 "int c = 2;\n" 8805 "int dd = 3;\n", 8806 Alignment); 8807 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 8808 "float b[1][] = {{3.f}};\n", 8809 Alignment); 8810 Alignment.AlignConsecutiveAssignments = false; 8811 8812 Alignment.ColumnLimit = 30; 8813 Alignment.BinPackParameters = false; 8814 verifyFormat("void foo(float a,\n" 8815 " float b,\n" 8816 " int c,\n" 8817 " uint32_t *d) {\n" 8818 " int * e = 0;\n" 8819 " float f = 0;\n" 8820 " double g = 0;\n" 8821 "}\n" 8822 "void bar(ino_t a,\n" 8823 " int b,\n" 8824 " uint32_t *c,\n" 8825 " bool d) {}\n", 8826 Alignment); 8827 Alignment.BinPackParameters = true; 8828 Alignment.ColumnLimit = 80; 8829 } 8830 8831 TEST_F(FormatTest, LinuxBraceBreaking) { 8832 FormatStyle LinuxBraceStyle = getLLVMStyle(); 8833 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux; 8834 verifyFormat("namespace a\n" 8835 "{\n" 8836 "class A\n" 8837 "{\n" 8838 " void f()\n" 8839 " {\n" 8840 " if (true) {\n" 8841 " a();\n" 8842 " b();\n" 8843 " } else {\n" 8844 " a();\n" 8845 " }\n" 8846 " }\n" 8847 " void g() { return; }\n" 8848 "};\n" 8849 "struct B {\n" 8850 " int x;\n" 8851 "};\n" 8852 "}\n", 8853 LinuxBraceStyle); 8854 verifyFormat("enum X {\n" 8855 " Y = 0,\n" 8856 "}\n", 8857 LinuxBraceStyle); 8858 verifyFormat("struct S {\n" 8859 " int Type;\n" 8860 " union {\n" 8861 " int x;\n" 8862 " double y;\n" 8863 " } Value;\n" 8864 " class C\n" 8865 " {\n" 8866 " MyFavoriteType Value;\n" 8867 " } Class;\n" 8868 "}\n", 8869 LinuxBraceStyle); 8870 } 8871 8872 TEST_F(FormatTest, MozillaBraceBreaking) { 8873 FormatStyle MozillaBraceStyle = getLLVMStyle(); 8874 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; 8875 MozillaBraceStyle.FixNamespaceComments = false; 8876 verifyFormat("namespace a {\n" 8877 "class A\n" 8878 "{\n" 8879 " void f()\n" 8880 " {\n" 8881 " if (true) {\n" 8882 " a();\n" 8883 " b();\n" 8884 " }\n" 8885 " }\n" 8886 " void g() { return; }\n" 8887 "};\n" 8888 "enum E\n" 8889 "{\n" 8890 " A,\n" 8891 " // foo\n" 8892 " B,\n" 8893 " C\n" 8894 "};\n" 8895 "struct B\n" 8896 "{\n" 8897 " int x;\n" 8898 "};\n" 8899 "}\n", 8900 MozillaBraceStyle); 8901 verifyFormat("struct S\n" 8902 "{\n" 8903 " int Type;\n" 8904 " union\n" 8905 " {\n" 8906 " int x;\n" 8907 " double y;\n" 8908 " } Value;\n" 8909 " class C\n" 8910 " {\n" 8911 " MyFavoriteType Value;\n" 8912 " } Class;\n" 8913 "}\n", 8914 MozillaBraceStyle); 8915 } 8916 8917 TEST_F(FormatTest, StroustrupBraceBreaking) { 8918 FormatStyle StroustrupBraceStyle = getLLVMStyle(); 8919 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 8920 verifyFormat("namespace a {\n" 8921 "class A {\n" 8922 " void f()\n" 8923 " {\n" 8924 " if (true) {\n" 8925 " a();\n" 8926 " b();\n" 8927 " }\n" 8928 " }\n" 8929 " void g() { return; }\n" 8930 "};\n" 8931 "struct B {\n" 8932 " int x;\n" 8933 "};\n" 8934 "} // namespace a\n", 8935 StroustrupBraceStyle); 8936 8937 verifyFormat("void foo()\n" 8938 "{\n" 8939 " if (a) {\n" 8940 " a();\n" 8941 " }\n" 8942 " else {\n" 8943 " b();\n" 8944 " }\n" 8945 "}\n", 8946 StroustrupBraceStyle); 8947 8948 verifyFormat("#ifdef _DEBUG\n" 8949 "int foo(int i = 0)\n" 8950 "#else\n" 8951 "int foo(int i = 5)\n" 8952 "#endif\n" 8953 "{\n" 8954 " return i;\n" 8955 "}", 8956 StroustrupBraceStyle); 8957 8958 verifyFormat("void foo() {}\n" 8959 "void bar()\n" 8960 "#ifdef _DEBUG\n" 8961 "{\n" 8962 " foo();\n" 8963 "}\n" 8964 "#else\n" 8965 "{\n" 8966 "}\n" 8967 "#endif", 8968 StroustrupBraceStyle); 8969 8970 verifyFormat("void foobar() { int i = 5; }\n" 8971 "#ifdef _DEBUG\n" 8972 "void bar() {}\n" 8973 "#else\n" 8974 "void bar() { foobar(); }\n" 8975 "#endif", 8976 StroustrupBraceStyle); 8977 } 8978 8979 TEST_F(FormatTest, AllmanBraceBreaking) { 8980 FormatStyle AllmanBraceStyle = getLLVMStyle(); 8981 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman; 8982 verifyFormat("namespace a\n" 8983 "{\n" 8984 "class A\n" 8985 "{\n" 8986 " void f()\n" 8987 " {\n" 8988 " if (true)\n" 8989 " {\n" 8990 " a();\n" 8991 " b();\n" 8992 " }\n" 8993 " }\n" 8994 " void g() { return; }\n" 8995 "};\n" 8996 "struct B\n" 8997 "{\n" 8998 " int x;\n" 8999 "};\n" 9000 "}", 9001 AllmanBraceStyle); 9002 9003 verifyFormat("void f()\n" 9004 "{\n" 9005 " if (true)\n" 9006 " {\n" 9007 " a();\n" 9008 " }\n" 9009 " else if (false)\n" 9010 " {\n" 9011 " b();\n" 9012 " }\n" 9013 " else\n" 9014 " {\n" 9015 " c();\n" 9016 " }\n" 9017 "}\n", 9018 AllmanBraceStyle); 9019 9020 verifyFormat("void f()\n" 9021 "{\n" 9022 " for (int i = 0; i < 10; ++i)\n" 9023 " {\n" 9024 " a();\n" 9025 " }\n" 9026 " while (false)\n" 9027 " {\n" 9028 " b();\n" 9029 " }\n" 9030 " do\n" 9031 " {\n" 9032 " c();\n" 9033 " } while (false)\n" 9034 "}\n", 9035 AllmanBraceStyle); 9036 9037 verifyFormat("void f(int a)\n" 9038 "{\n" 9039 " switch (a)\n" 9040 " {\n" 9041 " case 0:\n" 9042 " break;\n" 9043 " case 1:\n" 9044 " {\n" 9045 " break;\n" 9046 " }\n" 9047 " case 2:\n" 9048 " {\n" 9049 " }\n" 9050 " break;\n" 9051 " default:\n" 9052 " break;\n" 9053 " }\n" 9054 "}\n", 9055 AllmanBraceStyle); 9056 9057 verifyFormat("enum X\n" 9058 "{\n" 9059 " Y = 0,\n" 9060 "}\n", 9061 AllmanBraceStyle); 9062 verifyFormat("enum X\n" 9063 "{\n" 9064 " Y = 0\n" 9065 "}\n", 9066 AllmanBraceStyle); 9067 9068 verifyFormat("@interface BSApplicationController ()\n" 9069 "{\n" 9070 "@private\n" 9071 " id _extraIvar;\n" 9072 "}\n" 9073 "@end\n", 9074 AllmanBraceStyle); 9075 9076 verifyFormat("#ifdef _DEBUG\n" 9077 "int foo(int i = 0)\n" 9078 "#else\n" 9079 "int foo(int i = 5)\n" 9080 "#endif\n" 9081 "{\n" 9082 " return i;\n" 9083 "}", 9084 AllmanBraceStyle); 9085 9086 verifyFormat("void foo() {}\n" 9087 "void bar()\n" 9088 "#ifdef _DEBUG\n" 9089 "{\n" 9090 " foo();\n" 9091 "}\n" 9092 "#else\n" 9093 "{\n" 9094 "}\n" 9095 "#endif", 9096 AllmanBraceStyle); 9097 9098 verifyFormat("void foobar() { int i = 5; }\n" 9099 "#ifdef _DEBUG\n" 9100 "void bar() {}\n" 9101 "#else\n" 9102 "void bar() { foobar(); }\n" 9103 "#endif", 9104 AllmanBraceStyle); 9105 9106 // This shouldn't affect ObjC blocks.. 9107 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 9108 " // ...\n" 9109 " int i;\n" 9110 "}];", 9111 AllmanBraceStyle); 9112 verifyFormat("void (^block)(void) = ^{\n" 9113 " // ...\n" 9114 " int i;\n" 9115 "};", 9116 AllmanBraceStyle); 9117 // .. or dict literals. 9118 verifyFormat("void f()\n" 9119 "{\n" 9120 " // ...\n" 9121 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 9122 "}", 9123 AllmanBraceStyle); 9124 verifyFormat("void f()\n" 9125 "{\n" 9126 " // ...\n" 9127 " [object someMethod:@{a : @\"b\"}];\n" 9128 "}", 9129 AllmanBraceStyle); 9130 verifyFormat("int f()\n" 9131 "{ // comment\n" 9132 " return 42;\n" 9133 "}", 9134 AllmanBraceStyle); 9135 9136 AllmanBraceStyle.ColumnLimit = 19; 9137 verifyFormat("void f() { int i; }", AllmanBraceStyle); 9138 AllmanBraceStyle.ColumnLimit = 18; 9139 verifyFormat("void f()\n" 9140 "{\n" 9141 " int i;\n" 9142 "}", 9143 AllmanBraceStyle); 9144 AllmanBraceStyle.ColumnLimit = 80; 9145 9146 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle; 9147 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true; 9148 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 9149 verifyFormat("void f(bool b)\n" 9150 "{\n" 9151 " if (b)\n" 9152 " {\n" 9153 " return;\n" 9154 " }\n" 9155 "}\n", 9156 BreakBeforeBraceShortIfs); 9157 verifyFormat("void f(bool b)\n" 9158 "{\n" 9159 " if constexpr (b)\n" 9160 " {\n" 9161 " return;\n" 9162 " }\n" 9163 "}\n", 9164 BreakBeforeBraceShortIfs); 9165 verifyFormat("void f(bool b)\n" 9166 "{\n" 9167 " if (b) return;\n" 9168 "}\n", 9169 BreakBeforeBraceShortIfs); 9170 verifyFormat("void f(bool b)\n" 9171 "{\n" 9172 " if constexpr (b) return;\n" 9173 "}\n", 9174 BreakBeforeBraceShortIfs); 9175 verifyFormat("void f(bool b)\n" 9176 "{\n" 9177 " while (b)\n" 9178 " {\n" 9179 " return;\n" 9180 " }\n" 9181 "}\n", 9182 BreakBeforeBraceShortIfs); 9183 } 9184 9185 TEST_F(FormatTest, GNUBraceBreaking) { 9186 FormatStyle GNUBraceStyle = getLLVMStyle(); 9187 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU; 9188 verifyFormat("namespace a\n" 9189 "{\n" 9190 "class A\n" 9191 "{\n" 9192 " void f()\n" 9193 " {\n" 9194 " int a;\n" 9195 " {\n" 9196 " int b;\n" 9197 " }\n" 9198 " if (true)\n" 9199 " {\n" 9200 " a();\n" 9201 " b();\n" 9202 " }\n" 9203 " }\n" 9204 " void g() { return; }\n" 9205 "}\n" 9206 "}", 9207 GNUBraceStyle); 9208 9209 verifyFormat("void f()\n" 9210 "{\n" 9211 " if (true)\n" 9212 " {\n" 9213 " a();\n" 9214 " }\n" 9215 " else if (false)\n" 9216 " {\n" 9217 " b();\n" 9218 " }\n" 9219 " else\n" 9220 " {\n" 9221 " c();\n" 9222 " }\n" 9223 "}\n", 9224 GNUBraceStyle); 9225 9226 verifyFormat("void f()\n" 9227 "{\n" 9228 " for (int i = 0; i < 10; ++i)\n" 9229 " {\n" 9230 " a();\n" 9231 " }\n" 9232 " while (false)\n" 9233 " {\n" 9234 " b();\n" 9235 " }\n" 9236 " do\n" 9237 " {\n" 9238 " c();\n" 9239 " }\n" 9240 " while (false);\n" 9241 "}\n", 9242 GNUBraceStyle); 9243 9244 verifyFormat("void f(int a)\n" 9245 "{\n" 9246 " switch (a)\n" 9247 " {\n" 9248 " case 0:\n" 9249 " break;\n" 9250 " case 1:\n" 9251 " {\n" 9252 " break;\n" 9253 " }\n" 9254 " case 2:\n" 9255 " {\n" 9256 " }\n" 9257 " break;\n" 9258 " default:\n" 9259 " break;\n" 9260 " }\n" 9261 "}\n", 9262 GNUBraceStyle); 9263 9264 verifyFormat("enum X\n" 9265 "{\n" 9266 " Y = 0,\n" 9267 "}\n", 9268 GNUBraceStyle); 9269 9270 verifyFormat("@interface BSApplicationController ()\n" 9271 "{\n" 9272 "@private\n" 9273 " id _extraIvar;\n" 9274 "}\n" 9275 "@end\n", 9276 GNUBraceStyle); 9277 9278 verifyFormat("#ifdef _DEBUG\n" 9279 "int foo(int i = 0)\n" 9280 "#else\n" 9281 "int foo(int i = 5)\n" 9282 "#endif\n" 9283 "{\n" 9284 " return i;\n" 9285 "}", 9286 GNUBraceStyle); 9287 9288 verifyFormat("void foo() {}\n" 9289 "void bar()\n" 9290 "#ifdef _DEBUG\n" 9291 "{\n" 9292 " foo();\n" 9293 "}\n" 9294 "#else\n" 9295 "{\n" 9296 "}\n" 9297 "#endif", 9298 GNUBraceStyle); 9299 9300 verifyFormat("void foobar() { int i = 5; }\n" 9301 "#ifdef _DEBUG\n" 9302 "void bar() {}\n" 9303 "#else\n" 9304 "void bar() { foobar(); }\n" 9305 "#endif", 9306 GNUBraceStyle); 9307 } 9308 9309 TEST_F(FormatTest, WebKitBraceBreaking) { 9310 FormatStyle WebKitBraceStyle = getLLVMStyle(); 9311 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit; 9312 WebKitBraceStyle.FixNamespaceComments = false; 9313 verifyFormat("namespace a {\n" 9314 "class A {\n" 9315 " void f()\n" 9316 " {\n" 9317 " if (true) {\n" 9318 " a();\n" 9319 " b();\n" 9320 " }\n" 9321 " }\n" 9322 " void g() { return; }\n" 9323 "};\n" 9324 "enum E {\n" 9325 " A,\n" 9326 " // foo\n" 9327 " B,\n" 9328 " C\n" 9329 "};\n" 9330 "struct B {\n" 9331 " int x;\n" 9332 "};\n" 9333 "}\n", 9334 WebKitBraceStyle); 9335 verifyFormat("struct S {\n" 9336 " int Type;\n" 9337 " union {\n" 9338 " int x;\n" 9339 " double y;\n" 9340 " } Value;\n" 9341 " class C {\n" 9342 " MyFavoriteType Value;\n" 9343 " } Class;\n" 9344 "};\n", 9345 WebKitBraceStyle); 9346 } 9347 9348 TEST_F(FormatTest, CatchExceptionReferenceBinding) { 9349 verifyFormat("void f() {\n" 9350 " try {\n" 9351 " } catch (const Exception &e) {\n" 9352 " }\n" 9353 "}\n", 9354 getLLVMStyle()); 9355 } 9356 9357 TEST_F(FormatTest, UnderstandsPragmas) { 9358 verifyFormat("#pragma omp reduction(| : var)"); 9359 verifyFormat("#pragma omp reduction(+ : var)"); 9360 9361 EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string " 9362 "(including parentheses).", 9363 format("#pragma mark Any non-hyphenated or hyphenated string " 9364 "(including parentheses).")); 9365 } 9366 9367 TEST_F(FormatTest, UnderstandPragmaOption) { 9368 verifyFormat("#pragma option -C -A"); 9369 9370 EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A")); 9371 } 9372 9373 #define EXPECT_ALL_STYLES_EQUAL(Styles) \ 9374 for (size_t i = 1; i < Styles.size(); ++i) \ 9375 EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \ 9376 << " differs from Style #0" 9377 9378 TEST_F(FormatTest, GetsPredefinedStyleByName) { 9379 SmallVector<FormatStyle, 3> Styles; 9380 Styles.resize(3); 9381 9382 Styles[0] = getLLVMStyle(); 9383 EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1])); 9384 EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2])); 9385 EXPECT_ALL_STYLES_EQUAL(Styles); 9386 9387 Styles[0] = getGoogleStyle(); 9388 EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1])); 9389 EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2])); 9390 EXPECT_ALL_STYLES_EQUAL(Styles); 9391 9392 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 9393 EXPECT_TRUE( 9394 getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1])); 9395 EXPECT_TRUE( 9396 getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2])); 9397 EXPECT_ALL_STYLES_EQUAL(Styles); 9398 9399 Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp); 9400 EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1])); 9401 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2])); 9402 EXPECT_ALL_STYLES_EQUAL(Styles); 9403 9404 Styles[0] = getMozillaStyle(); 9405 EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1])); 9406 EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2])); 9407 EXPECT_ALL_STYLES_EQUAL(Styles); 9408 9409 Styles[0] = getWebKitStyle(); 9410 EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1])); 9411 EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2])); 9412 EXPECT_ALL_STYLES_EQUAL(Styles); 9413 9414 Styles[0] = getGNUStyle(); 9415 EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1])); 9416 EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2])); 9417 EXPECT_ALL_STYLES_EQUAL(Styles); 9418 9419 EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0])); 9420 } 9421 9422 TEST_F(FormatTest, GetsCorrectBasedOnStyle) { 9423 SmallVector<FormatStyle, 8> Styles; 9424 Styles.resize(2); 9425 9426 Styles[0] = getGoogleStyle(); 9427 Styles[1] = getLLVMStyle(); 9428 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 9429 EXPECT_ALL_STYLES_EQUAL(Styles); 9430 9431 Styles.resize(5); 9432 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 9433 Styles[1] = getLLVMStyle(); 9434 Styles[1].Language = FormatStyle::LK_JavaScript; 9435 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 9436 9437 Styles[2] = getLLVMStyle(); 9438 Styles[2].Language = FormatStyle::LK_JavaScript; 9439 EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n" 9440 "BasedOnStyle: Google", 9441 &Styles[2]) 9442 .value()); 9443 9444 Styles[3] = getLLVMStyle(); 9445 Styles[3].Language = FormatStyle::LK_JavaScript; 9446 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n" 9447 "Language: JavaScript", 9448 &Styles[3]) 9449 .value()); 9450 9451 Styles[4] = getLLVMStyle(); 9452 Styles[4].Language = FormatStyle::LK_JavaScript; 9453 EXPECT_EQ(0, parseConfiguration("---\n" 9454 "BasedOnStyle: LLVM\n" 9455 "IndentWidth: 123\n" 9456 "---\n" 9457 "BasedOnStyle: Google\n" 9458 "Language: JavaScript", 9459 &Styles[4]) 9460 .value()); 9461 EXPECT_ALL_STYLES_EQUAL(Styles); 9462 } 9463 9464 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \ 9465 Style.FIELD = false; \ 9466 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \ 9467 EXPECT_TRUE(Style.FIELD); \ 9468 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \ 9469 EXPECT_FALSE(Style.FIELD); 9470 9471 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD) 9472 9473 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \ 9474 Style.STRUCT.FIELD = false; \ 9475 EXPECT_EQ(0, \ 9476 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \ 9477 .value()); \ 9478 EXPECT_TRUE(Style.STRUCT.FIELD); \ 9479 EXPECT_EQ(0, \ 9480 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \ 9481 .value()); \ 9482 EXPECT_FALSE(Style.STRUCT.FIELD); 9483 9484 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \ 9485 CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD) 9486 9487 #define CHECK_PARSE(TEXT, FIELD, VALUE) \ 9488 EXPECT_NE(VALUE, Style.FIELD); \ 9489 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \ 9490 EXPECT_EQ(VALUE, Style.FIELD) 9491 9492 TEST_F(FormatTest, ParsesConfigurationBools) { 9493 FormatStyle Style = {}; 9494 Style.Language = FormatStyle::LK_Cpp; 9495 CHECK_PARSE_BOOL(AlignOperands); 9496 CHECK_PARSE_BOOL(AlignTrailingComments); 9497 CHECK_PARSE_BOOL(AlignConsecutiveAssignments); 9498 CHECK_PARSE_BOOL(AlignConsecutiveDeclarations); 9499 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); 9500 CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine); 9501 CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine); 9502 CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine); 9503 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); 9504 CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations); 9505 CHECK_PARSE_BOOL(BinPackArguments); 9506 CHECK_PARSE_BOOL(BinPackParameters); 9507 CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations); 9508 CHECK_PARSE_BOOL(BreakBeforeTernaryOperators); 9509 CHECK_PARSE_BOOL(BreakStringLiterals); 9510 CHECK_PARSE_BOOL(BreakBeforeInheritanceComma) 9511 CHECK_PARSE_BOOL(CompactNamespaces); 9512 CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine); 9513 CHECK_PARSE_BOOL(DerivePointerAlignment); 9514 CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding"); 9515 CHECK_PARSE_BOOL(DisableFormat); 9516 CHECK_PARSE_BOOL(IndentCaseLabels); 9517 CHECK_PARSE_BOOL(IndentWrappedFunctionNames); 9518 CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks); 9519 CHECK_PARSE_BOOL(ObjCSpaceAfterProperty); 9520 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList); 9521 CHECK_PARSE_BOOL(Cpp11BracedListStyle); 9522 CHECK_PARSE_BOOL(ReflowComments); 9523 CHECK_PARSE_BOOL(SortIncludes); 9524 CHECK_PARSE_BOOL(SortUsingDeclarations); 9525 CHECK_PARSE_BOOL(SpacesInParentheses); 9526 CHECK_PARSE_BOOL(SpacesInSquareBrackets); 9527 CHECK_PARSE_BOOL(SpacesInAngles); 9528 CHECK_PARSE_BOOL(SpaceInEmptyParentheses); 9529 CHECK_PARSE_BOOL(SpacesInContainerLiterals); 9530 CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); 9531 CHECK_PARSE_BOOL(SpaceAfterCStyleCast); 9532 CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword); 9533 CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); 9534 9535 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass); 9536 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement); 9537 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum); 9538 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction); 9539 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace); 9540 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration); 9541 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct); 9542 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion); 9543 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch); 9544 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse); 9545 CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces); 9546 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction); 9547 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord); 9548 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace); 9549 } 9550 9551 #undef CHECK_PARSE_BOOL 9552 9553 TEST_F(FormatTest, ParsesConfiguration) { 9554 FormatStyle Style = {}; 9555 Style.Language = FormatStyle::LK_Cpp; 9556 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234); 9557 CHECK_PARSE("ConstructorInitializerIndentWidth: 1234", 9558 ConstructorInitializerIndentWidth, 1234u); 9559 CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u); 9560 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); 9561 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u); 9562 CHECK_PARSE("PenaltyBreakAssignment: 1234", 9563 PenaltyBreakAssignment, 1234u); 9564 CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234", 9565 PenaltyBreakBeforeFirstCallParameter, 1234u); 9566 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); 9567 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234", 9568 PenaltyReturnTypeOnItsOwnLine, 1234u); 9569 CHECK_PARSE("SpacesBeforeTrailingComments: 1234", 9570 SpacesBeforeTrailingComments, 1234u); 9571 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u); 9572 CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u); 9573 CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$"); 9574 9575 Style.PointerAlignment = FormatStyle::PAS_Middle; 9576 CHECK_PARSE("PointerAlignment: Left", PointerAlignment, 9577 FormatStyle::PAS_Left); 9578 CHECK_PARSE("PointerAlignment: Right", PointerAlignment, 9579 FormatStyle::PAS_Right); 9580 CHECK_PARSE("PointerAlignment: Middle", PointerAlignment, 9581 FormatStyle::PAS_Middle); 9582 // For backward compatibility: 9583 CHECK_PARSE("PointerBindsToType: Left", PointerAlignment, 9584 FormatStyle::PAS_Left); 9585 CHECK_PARSE("PointerBindsToType: Right", PointerAlignment, 9586 FormatStyle::PAS_Right); 9587 CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment, 9588 FormatStyle::PAS_Middle); 9589 9590 Style.Standard = FormatStyle::LS_Auto; 9591 CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03); 9592 CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11); 9593 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03); 9594 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11); 9595 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto); 9596 9597 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 9598 CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment", 9599 BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment); 9600 CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators, 9601 FormatStyle::BOS_None); 9602 CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators, 9603 FormatStyle::BOS_All); 9604 // For backward compatibility: 9605 CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators, 9606 FormatStyle::BOS_None); 9607 CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators, 9608 FormatStyle::BOS_All); 9609 9610 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 9611 CHECK_PARSE("BreakConstructorInitializers: BeforeComma", 9612 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 9613 CHECK_PARSE("BreakConstructorInitializers: AfterColon", 9614 BreakConstructorInitializers, FormatStyle::BCIS_AfterColon); 9615 CHECK_PARSE("BreakConstructorInitializers: BeforeColon", 9616 BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon); 9617 // For backward compatibility: 9618 CHECK_PARSE("BreakConstructorInitializersBeforeComma: true", 9619 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 9620 9621 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 9622 CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket, 9623 FormatStyle::BAS_Align); 9624 CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket, 9625 FormatStyle::BAS_DontAlign); 9626 CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket, 9627 FormatStyle::BAS_AlwaysBreak); 9628 // For backward compatibility: 9629 CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket, 9630 FormatStyle::BAS_DontAlign); 9631 CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket, 9632 FormatStyle::BAS_Align); 9633 9634 Style.AlignEscapedNewlines = FormatStyle::ENAS_Left; 9635 CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines, 9636 FormatStyle::ENAS_DontAlign); 9637 CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines, 9638 FormatStyle::ENAS_Left); 9639 CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines, 9640 FormatStyle::ENAS_Right); 9641 // For backward compatibility: 9642 CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines, 9643 FormatStyle::ENAS_Left); 9644 CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines, 9645 FormatStyle::ENAS_Right); 9646 9647 Style.UseTab = FormatStyle::UT_ForIndentation; 9648 CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never); 9649 CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation); 9650 CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always); 9651 CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab, 9652 FormatStyle::UT_ForContinuationAndIndentation); 9653 // For backward compatibility: 9654 CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never); 9655 CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always); 9656 9657 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 9658 CHECK_PARSE("AllowShortFunctionsOnASingleLine: None", 9659 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 9660 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline", 9661 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline); 9662 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty", 9663 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty); 9664 CHECK_PARSE("AllowShortFunctionsOnASingleLine: All", 9665 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 9666 // For backward compatibility: 9667 CHECK_PARSE("AllowShortFunctionsOnASingleLine: false", 9668 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 9669 CHECK_PARSE("AllowShortFunctionsOnASingleLine: true", 9670 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 9671 9672 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 9673 CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens, 9674 FormatStyle::SBPO_Never); 9675 CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens, 9676 FormatStyle::SBPO_Always); 9677 CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens, 9678 FormatStyle::SBPO_ControlStatements); 9679 // For backward compatibility: 9680 CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens, 9681 FormatStyle::SBPO_Never); 9682 CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens, 9683 FormatStyle::SBPO_ControlStatements); 9684 9685 Style.ColumnLimit = 123; 9686 FormatStyle BaseStyle = getLLVMStyle(); 9687 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit); 9688 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u); 9689 9690 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 9691 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces, 9692 FormatStyle::BS_Attach); 9693 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces, 9694 FormatStyle::BS_Linux); 9695 CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces, 9696 FormatStyle::BS_Mozilla); 9697 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces, 9698 FormatStyle::BS_Stroustrup); 9699 CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces, 9700 FormatStyle::BS_Allman); 9701 CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU); 9702 CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces, 9703 FormatStyle::BS_WebKit); 9704 CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces, 9705 FormatStyle::BS_Custom); 9706 9707 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 9708 CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType, 9709 FormatStyle::RTBS_None); 9710 CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType, 9711 FormatStyle::RTBS_All); 9712 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel", 9713 AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel); 9714 CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions", 9715 AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions); 9716 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions", 9717 AlwaysBreakAfterReturnType, 9718 FormatStyle::RTBS_TopLevelDefinitions); 9719 9720 Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All; 9721 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None", 9722 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None); 9723 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All", 9724 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All); 9725 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel", 9726 AlwaysBreakAfterDefinitionReturnType, 9727 FormatStyle::DRTBS_TopLevel); 9728 9729 Style.NamespaceIndentation = FormatStyle::NI_All; 9730 CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation, 9731 FormatStyle::NI_None); 9732 CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation, 9733 FormatStyle::NI_Inner); 9734 CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation, 9735 FormatStyle::NI_All); 9736 9737 // FIXME: This is required because parsing a configuration simply overwrites 9738 // the first N elements of the list instead of resetting it. 9739 Style.ForEachMacros.clear(); 9740 std::vector<std::string> BoostForeach; 9741 BoostForeach.push_back("BOOST_FOREACH"); 9742 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach); 9743 std::vector<std::string> BoostAndQForeach; 9744 BoostAndQForeach.push_back("BOOST_FOREACH"); 9745 BoostAndQForeach.push_back("Q_FOREACH"); 9746 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros, 9747 BoostAndQForeach); 9748 9749 Style.IncludeCategories.clear(); 9750 std::vector<FormatStyle::IncludeCategory> ExpectedCategories = {{"abc/.*", 2}, 9751 {".*", 1}}; 9752 CHECK_PARSE("IncludeCategories:\n" 9753 " - Regex: abc/.*\n" 9754 " Priority: 2\n" 9755 " - Regex: .*\n" 9756 " Priority: 1", 9757 IncludeCategories, ExpectedCategories); 9758 CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeIsMainRegex, "abc$"); 9759 } 9760 9761 TEST_F(FormatTest, ParsesConfigurationWithLanguages) { 9762 FormatStyle Style = {}; 9763 Style.Language = FormatStyle::LK_Cpp; 9764 CHECK_PARSE("Language: Cpp\n" 9765 "IndentWidth: 12", 9766 IndentWidth, 12u); 9767 EXPECT_EQ(parseConfiguration("Language: JavaScript\n" 9768 "IndentWidth: 34", 9769 &Style), 9770 ParseError::Unsuitable); 9771 EXPECT_EQ(12u, Style.IndentWidth); 9772 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 9773 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 9774 9775 Style.Language = FormatStyle::LK_JavaScript; 9776 CHECK_PARSE("Language: JavaScript\n" 9777 "IndentWidth: 12", 9778 IndentWidth, 12u); 9779 CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u); 9780 EXPECT_EQ(parseConfiguration("Language: Cpp\n" 9781 "IndentWidth: 34", 9782 &Style), 9783 ParseError::Unsuitable); 9784 EXPECT_EQ(23u, Style.IndentWidth); 9785 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 9786 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 9787 9788 CHECK_PARSE("BasedOnStyle: LLVM\n" 9789 "IndentWidth: 67", 9790 IndentWidth, 67u); 9791 9792 CHECK_PARSE("---\n" 9793 "Language: JavaScript\n" 9794 "IndentWidth: 12\n" 9795 "---\n" 9796 "Language: Cpp\n" 9797 "IndentWidth: 34\n" 9798 "...\n", 9799 IndentWidth, 12u); 9800 9801 Style.Language = FormatStyle::LK_Cpp; 9802 CHECK_PARSE("---\n" 9803 "Language: JavaScript\n" 9804 "IndentWidth: 12\n" 9805 "---\n" 9806 "Language: Cpp\n" 9807 "IndentWidth: 34\n" 9808 "...\n", 9809 IndentWidth, 34u); 9810 CHECK_PARSE("---\n" 9811 "IndentWidth: 78\n" 9812 "---\n" 9813 "Language: JavaScript\n" 9814 "IndentWidth: 56\n" 9815 "...\n", 9816 IndentWidth, 78u); 9817 9818 Style.ColumnLimit = 123; 9819 Style.IndentWidth = 234; 9820 Style.BreakBeforeBraces = FormatStyle::BS_Linux; 9821 Style.TabWidth = 345; 9822 EXPECT_FALSE(parseConfiguration("---\n" 9823 "IndentWidth: 456\n" 9824 "BreakBeforeBraces: Allman\n" 9825 "---\n" 9826 "Language: JavaScript\n" 9827 "IndentWidth: 111\n" 9828 "TabWidth: 111\n" 9829 "---\n" 9830 "Language: Cpp\n" 9831 "BreakBeforeBraces: Stroustrup\n" 9832 "TabWidth: 789\n" 9833 "...\n", 9834 &Style)); 9835 EXPECT_EQ(123u, Style.ColumnLimit); 9836 EXPECT_EQ(456u, Style.IndentWidth); 9837 EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces); 9838 EXPECT_EQ(789u, Style.TabWidth); 9839 9840 EXPECT_EQ(parseConfiguration("---\n" 9841 "Language: JavaScript\n" 9842 "IndentWidth: 56\n" 9843 "---\n" 9844 "IndentWidth: 78\n" 9845 "...\n", 9846 &Style), 9847 ParseError::Error); 9848 EXPECT_EQ(parseConfiguration("---\n" 9849 "Language: JavaScript\n" 9850 "IndentWidth: 56\n" 9851 "---\n" 9852 "Language: JavaScript\n" 9853 "IndentWidth: 78\n" 9854 "...\n", 9855 &Style), 9856 ParseError::Error); 9857 9858 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 9859 } 9860 9861 #undef CHECK_PARSE 9862 9863 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) { 9864 FormatStyle Style = {}; 9865 Style.Language = FormatStyle::LK_JavaScript; 9866 Style.BreakBeforeTernaryOperators = true; 9867 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value()); 9868 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 9869 9870 Style.BreakBeforeTernaryOperators = true; 9871 EXPECT_EQ(0, parseConfiguration("---\n" 9872 "BasedOnStyle: Google\n" 9873 "---\n" 9874 "Language: JavaScript\n" 9875 "IndentWidth: 76\n" 9876 "...\n", 9877 &Style) 9878 .value()); 9879 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 9880 EXPECT_EQ(76u, Style.IndentWidth); 9881 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 9882 } 9883 9884 TEST_F(FormatTest, ConfigurationRoundTripTest) { 9885 FormatStyle Style = getLLVMStyle(); 9886 std::string YAML = configurationAsText(Style); 9887 FormatStyle ParsedStyle = {}; 9888 ParsedStyle.Language = FormatStyle::LK_Cpp; 9889 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value()); 9890 EXPECT_EQ(Style, ParsedStyle); 9891 } 9892 9893 TEST_F(FormatTest, WorksFor8bitEncodings) { 9894 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" 9895 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" 9896 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" 9897 "\"\xef\xee\xf0\xf3...\"", 9898 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " 9899 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " 9900 "\xef\xee\xf0\xf3...\"", 9901 getLLVMStyleWithColumns(12))); 9902 } 9903 9904 TEST_F(FormatTest, HandlesUTF8BOM) { 9905 EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf")); 9906 EXPECT_EQ("\xef\xbb\xbf#include <iostream>", 9907 format("\xef\xbb\xbf#include <iostream>")); 9908 EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>", 9909 format("\xef\xbb\xbf\n#include <iostream>")); 9910 } 9911 9912 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. 9913 #if !defined(_MSC_VER) 9914 9915 TEST_F(FormatTest, CountsUTF8CharactersProperly) { 9916 verifyFormat("\"Однажды в студёную зимнюю пору...\"", 9917 getLLVMStyleWithColumns(35)); 9918 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", 9919 getLLVMStyleWithColumns(31)); 9920 verifyFormat("// Однажды в студёную зимнюю пору...", 9921 getLLVMStyleWithColumns(36)); 9922 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32)); 9923 verifyFormat("/* Однажды в студёную зимнюю пору... */", 9924 getLLVMStyleWithColumns(39)); 9925 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", 9926 getLLVMStyleWithColumns(35)); 9927 } 9928 9929 TEST_F(FormatTest, SplitsUTF8Strings) { 9930 // Non-printable characters' width is currently considered to be the length in 9931 // bytes in UTF8. The characters can be displayed in very different manner 9932 // (zero-width, single width with a substitution glyph, expanded to their code 9933 // (e.g. "<8d>"), so there's no single correct way to handle them. 9934 EXPECT_EQ("\"aaaaÄ\"\n" 9935 "\"\xc2\x8d\";", 9936 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 9937 EXPECT_EQ("\"aaaaaaaÄ\"\n" 9938 "\"\xc2\x8d\";", 9939 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 9940 EXPECT_EQ("\"Однажды, в \"\n" 9941 "\"студёную \"\n" 9942 "\"зимнюю \"\n" 9943 "\"пору,\"", 9944 format("\"Однажды, в студёную зимнюю пору,\"", 9945 getLLVMStyleWithColumns(13))); 9946 EXPECT_EQ( 9947 "\"一 二 三 \"\n" 9948 "\"四 五六 \"\n" 9949 "\"七 八 九 \"\n" 9950 "\"十\"", 9951 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); 9952 EXPECT_EQ("\"一\t二 \"\n" 9953 "\"\t三 \"\n" 9954 "\"四 五\t六 \"\n" 9955 "\"\t七 \"\n" 9956 "\"八九十\tqq\"", 9957 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", 9958 getLLVMStyleWithColumns(11))); 9959 9960 // UTF8 character in an escape sequence. 9961 EXPECT_EQ("\"aaaaaa\"\n" 9962 "\"\\\xC2\x8D\"", 9963 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10))); 9964 } 9965 9966 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { 9967 EXPECT_EQ("const char *sssss =\n" 9968 " \"一二三四五六七八\\\n" 9969 " 九 十\";", 9970 format("const char *sssss = \"一二三四五六七八\\\n" 9971 " 九 十\";", 9972 getLLVMStyleWithColumns(30))); 9973 } 9974 9975 TEST_F(FormatTest, SplitsUTF8LineComments) { 9976 EXPECT_EQ("// aaaaÄ\xc2\x8d", 9977 format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10))); 9978 EXPECT_EQ("// Я из лесу\n" 9979 "// вышел; был\n" 9980 "// сильный\n" 9981 "// мороз.", 9982 format("// Я из лесу вышел; был сильный мороз.", 9983 getLLVMStyleWithColumns(13))); 9984 EXPECT_EQ("// 一二三\n" 9985 "// 四五六七\n" 9986 "// 八 九\n" 9987 "// 十", 9988 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9))); 9989 } 9990 9991 TEST_F(FormatTest, SplitsUTF8BlockComments) { 9992 EXPECT_EQ("/* Гляжу,\n" 9993 " * поднимается\n" 9994 " * медленно в\n" 9995 " * гору\n" 9996 " * Лошадка,\n" 9997 " * везущая\n" 9998 " * хворосту\n" 9999 " * воз. */", 10000 format("/* Гляжу, поднимается медленно в гору\n" 10001 " * Лошадка, везущая хворосту воз. */", 10002 getLLVMStyleWithColumns(13))); 10003 EXPECT_EQ( 10004 "/* 一二三\n" 10005 " * 四五六七\n" 10006 " * 八 九\n" 10007 " * 十 */", 10008 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9))); 10009 EXPECT_EQ("/* \n" 10010 " * \n" 10011 " * - */", 10012 format("/* - */", getLLVMStyleWithColumns(12))); 10013 } 10014 10015 #endif // _MSC_VER 10016 10017 TEST_F(FormatTest, ConstructorInitializerIndentWidth) { 10018 FormatStyle Style = getLLVMStyle(); 10019 10020 Style.ConstructorInitializerIndentWidth = 4; 10021 verifyFormat( 10022 "SomeClass::Constructor()\n" 10023 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10024 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10025 Style); 10026 10027 Style.ConstructorInitializerIndentWidth = 2; 10028 verifyFormat( 10029 "SomeClass::Constructor()\n" 10030 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10031 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10032 Style); 10033 10034 Style.ConstructorInitializerIndentWidth = 0; 10035 verifyFormat( 10036 "SomeClass::Constructor()\n" 10037 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10038 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10039 Style); 10040 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 10041 verifyFormat( 10042 "SomeLongTemplateVariableName<\n" 10043 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>", 10044 Style); 10045 verifyFormat( 10046 "bool smaller = 1 < bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 10047 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 10048 Style); 10049 } 10050 10051 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) { 10052 FormatStyle Style = getLLVMStyle(); 10053 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 10054 Style.ConstructorInitializerIndentWidth = 4; 10055 verifyFormat("SomeClass::Constructor()\n" 10056 " : a(a)\n" 10057 " , b(b)\n" 10058 " , c(c) {}", 10059 Style); 10060 verifyFormat("SomeClass::Constructor()\n" 10061 " : a(a) {}", 10062 Style); 10063 10064 Style.ColumnLimit = 0; 10065 verifyFormat("SomeClass::Constructor()\n" 10066 " : a(a) {}", 10067 Style); 10068 verifyFormat("SomeClass::Constructor() noexcept\n" 10069 " : a(a) {}", 10070 Style); 10071 verifyFormat("SomeClass::Constructor()\n" 10072 " : a(a)\n" 10073 " , b(b)\n" 10074 " , c(c) {}", 10075 Style); 10076 verifyFormat("SomeClass::Constructor()\n" 10077 " : a(a) {\n" 10078 " foo();\n" 10079 " bar();\n" 10080 "}", 10081 Style); 10082 10083 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 10084 verifyFormat("SomeClass::Constructor()\n" 10085 " : a(a)\n" 10086 " , b(b)\n" 10087 " , c(c) {\n}", 10088 Style); 10089 verifyFormat("SomeClass::Constructor()\n" 10090 " : a(a) {\n}", 10091 Style); 10092 10093 Style.ColumnLimit = 80; 10094 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 10095 Style.ConstructorInitializerIndentWidth = 2; 10096 verifyFormat("SomeClass::Constructor()\n" 10097 " : a(a)\n" 10098 " , b(b)\n" 10099 " , c(c) {}", 10100 Style); 10101 10102 Style.ConstructorInitializerIndentWidth = 0; 10103 verifyFormat("SomeClass::Constructor()\n" 10104 ": a(a)\n" 10105 ", b(b)\n" 10106 ", c(c) {}", 10107 Style); 10108 10109 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 10110 Style.ConstructorInitializerIndentWidth = 4; 10111 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style); 10112 verifyFormat( 10113 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n", 10114 Style); 10115 verifyFormat( 10116 "SomeClass::Constructor()\n" 10117 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", 10118 Style); 10119 Style.ConstructorInitializerIndentWidth = 4; 10120 Style.ColumnLimit = 60; 10121 verifyFormat("SomeClass::Constructor()\n" 10122 " : aaaaaaaa(aaaaaaaa)\n" 10123 " , aaaaaaaa(aaaaaaaa)\n" 10124 " , aaaaaaaa(aaaaaaaa) {}", 10125 Style); 10126 } 10127 10128 TEST_F(FormatTest, Destructors) { 10129 verifyFormat("void F(int &i) { i.~int(); }"); 10130 verifyFormat("void F(int &i) { i->~int(); }"); 10131 } 10132 10133 TEST_F(FormatTest, FormatsWithWebKitStyle) { 10134 FormatStyle Style = getWebKitStyle(); 10135 10136 // Don't indent in outer namespaces. 10137 verifyFormat("namespace outer {\n" 10138 "int i;\n" 10139 "namespace inner {\n" 10140 " int i;\n" 10141 "} // namespace inner\n" 10142 "} // namespace outer\n" 10143 "namespace other_outer {\n" 10144 "int i;\n" 10145 "}", 10146 Style); 10147 10148 // Don't indent case labels. 10149 verifyFormat("switch (variable) {\n" 10150 "case 1:\n" 10151 "case 2:\n" 10152 " doSomething();\n" 10153 " break;\n" 10154 "default:\n" 10155 " ++variable;\n" 10156 "}", 10157 Style); 10158 10159 // Wrap before binary operators. 10160 EXPECT_EQ("void f()\n" 10161 "{\n" 10162 " if (aaaaaaaaaaaaaaaa\n" 10163 " && bbbbbbbbbbbbbbbbbbbbbbbb\n" 10164 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 10165 " return;\n" 10166 "}", 10167 format("void f() {\n" 10168 "if (aaaaaaaaaaaaaaaa\n" 10169 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" 10170 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 10171 "return;\n" 10172 "}", 10173 Style)); 10174 10175 // Allow functions on a single line. 10176 verifyFormat("void f() { return; }", Style); 10177 10178 // Constructor initializers are formatted one per line with the "," on the 10179 // new line. 10180 verifyFormat("Constructor()\n" 10181 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 10182 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" 10183 " aaaaaaaaaaaaaa)\n" 10184 " , aaaaaaaaaaaaaaaaaaaaaaa()\n" 10185 "{\n" 10186 "}", 10187 Style); 10188 verifyFormat("SomeClass::Constructor()\n" 10189 " : a(a)\n" 10190 "{\n" 10191 "}", 10192 Style); 10193 EXPECT_EQ("SomeClass::Constructor()\n" 10194 " : a(a)\n" 10195 "{\n" 10196 "}", 10197 format("SomeClass::Constructor():a(a){}", Style)); 10198 verifyFormat("SomeClass::Constructor()\n" 10199 " : a(a)\n" 10200 " , b(b)\n" 10201 " , c(c)\n" 10202 "{\n" 10203 "}", 10204 Style); 10205 verifyFormat("SomeClass::Constructor()\n" 10206 " : a(a)\n" 10207 "{\n" 10208 " foo();\n" 10209 " bar();\n" 10210 "}", 10211 Style); 10212 10213 // Access specifiers should be aligned left. 10214 verifyFormat("class C {\n" 10215 "public:\n" 10216 " int i;\n" 10217 "};", 10218 Style); 10219 10220 // Do not align comments. 10221 verifyFormat("int a; // Do not\n" 10222 "double b; // align comments.", 10223 Style); 10224 10225 // Do not align operands. 10226 EXPECT_EQ("ASSERT(aaaa\n" 10227 " || bbbb);", 10228 format("ASSERT ( aaaa\n||bbbb);", Style)); 10229 10230 // Accept input's line breaks. 10231 EXPECT_EQ("if (aaaaaaaaaaaaaaa\n" 10232 " || bbbbbbbbbbbbbbb) {\n" 10233 " i++;\n" 10234 "}", 10235 format("if (aaaaaaaaaaaaaaa\n" 10236 "|| bbbbbbbbbbbbbbb) { i++; }", 10237 Style)); 10238 EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n" 10239 " i++;\n" 10240 "}", 10241 format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style)); 10242 10243 // Don't automatically break all macro definitions (llvm.org/PR17842). 10244 verifyFormat("#define aNumber 10", Style); 10245 // However, generally keep the line breaks that the user authored. 10246 EXPECT_EQ("#define aNumber \\\n" 10247 " 10", 10248 format("#define aNumber \\\n" 10249 " 10", 10250 Style)); 10251 10252 // Keep empty and one-element array literals on a single line. 10253 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n" 10254 " copyItems:YES];", 10255 format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n" 10256 "copyItems:YES];", 10257 Style)); 10258 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n" 10259 " copyItems:YES];", 10260 format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n" 10261 " copyItems:YES];", 10262 Style)); 10263 // FIXME: This does not seem right, there should be more indentation before 10264 // the array literal's entries. Nested blocks have the same problem. 10265 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 10266 " @\"a\",\n" 10267 " @\"a\"\n" 10268 "]\n" 10269 " copyItems:YES];", 10270 format("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 10271 " @\"a\",\n" 10272 " @\"a\"\n" 10273 " ]\n" 10274 " copyItems:YES];", 10275 Style)); 10276 EXPECT_EQ( 10277 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 10278 " copyItems:YES];", 10279 format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 10280 " copyItems:YES];", 10281 Style)); 10282 10283 verifyFormat("[self.a b:c c:d];", Style); 10284 EXPECT_EQ("[self.a b:c\n" 10285 " c:d];", 10286 format("[self.a b:c\n" 10287 "c:d];", 10288 Style)); 10289 } 10290 10291 TEST_F(FormatTest, FormatsLambdas) { 10292 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n"); 10293 verifyFormat("int c = [&] { [=] { return b++; }(); }();\n"); 10294 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n"); 10295 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n"); 10296 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n"); 10297 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n"); 10298 verifyFormat("int x = f(*+[] {});"); 10299 verifyFormat("void f() {\n" 10300 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n" 10301 "}\n"); 10302 verifyFormat("void f() {\n" 10303 " other(x.begin(), //\n" 10304 " x.end(), //\n" 10305 " [&](int, int) { return 1; });\n" 10306 "}\n"); 10307 verifyFormat("SomeFunction([]() { // A cool function...\n" 10308 " return 43;\n" 10309 "});"); 10310 EXPECT_EQ("SomeFunction([]() {\n" 10311 "#define A a\n" 10312 " return 43;\n" 10313 "});", 10314 format("SomeFunction([](){\n" 10315 "#define A a\n" 10316 "return 43;\n" 10317 "});")); 10318 verifyFormat("void f() {\n" 10319 " SomeFunction([](decltype(x), A *a) {});\n" 10320 "}"); 10321 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10322 " [](const aaaaaaaaaa &a) { return a; });"); 10323 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n" 10324 " SomeOtherFunctioooooooooooooooooooooooooon();\n" 10325 "});"); 10326 verifyFormat("Constructor()\n" 10327 " : Field([] { // comment\n" 10328 " int i;\n" 10329 " }) {}"); 10330 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n" 10331 " return some_parameter.size();\n" 10332 "};"); 10333 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n" 10334 " [](const string &s) { return s; };"); 10335 verifyFormat("int i = aaaaaa ? 1 //\n" 10336 " : [] {\n" 10337 " return 2; //\n" 10338 " }();"); 10339 verifyFormat("llvm::errs() << \"number of twos is \"\n" 10340 " << std::count_if(v.begin(), v.end(), [](int x) {\n" 10341 " return x == 2; // force break\n" 10342 " });"); 10343 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10344 " [=](int iiiiiiiiiiii) {\n" 10345 " return aaaaaaaaaaaaaaaaaaaaaaa !=\n" 10346 " aaaaaaaaaaaaaaaaaaaaaaa;\n" 10347 " });", 10348 getLLVMStyleWithColumns(60)); 10349 verifyFormat("SomeFunction({[&] {\n" 10350 " // comment\n" 10351 " },\n" 10352 " [&] {\n" 10353 " // comment\n" 10354 " }});"); 10355 verifyFormat("SomeFunction({[&] {\n" 10356 " // comment\n" 10357 "}});"); 10358 verifyFormat("virtual aaaaaaaaaaaaaaaa(std::function<bool()> bbbbbbbbbbbb =\n" 10359 " [&]() { return true; },\n" 10360 " aaaaa aaaaaaaaa);"); 10361 10362 // Lambdas with return types. 10363 verifyFormat("int c = []() -> int { return 2; }();\n"); 10364 verifyFormat("int c = []() -> int * { return 2; }();\n"); 10365 verifyFormat("int c = []() -> vector<int> { return {2}; }();\n"); 10366 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());"); 10367 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};"); 10368 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};"); 10369 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};"); 10370 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};"); 10371 verifyFormat("[a, a]() -> a<1> {};"); 10372 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n" 10373 " int j) -> int {\n" 10374 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n" 10375 "};"); 10376 verifyFormat( 10377 "aaaaaaaaaaaaaaaaaaaaaa(\n" 10378 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n" 10379 " return aaaaaaaaaaaaaaaaa;\n" 10380 " });", 10381 getLLVMStyleWithColumns(70)); 10382 verifyFormat("[]() //\n" 10383 " -> int {\n" 10384 " return 1; //\n" 10385 "};"); 10386 10387 // Multiple lambdas in the same parentheses change indentation rules. 10388 verifyFormat("SomeFunction(\n" 10389 " []() {\n" 10390 " int i = 42;\n" 10391 " return i;\n" 10392 " },\n" 10393 " []() {\n" 10394 " int j = 43;\n" 10395 " return j;\n" 10396 " });"); 10397 10398 // More complex introducers. 10399 verifyFormat("return [i, args...] {};"); 10400 10401 // Not lambdas. 10402 verifyFormat("constexpr char hello[]{\"hello\"};"); 10403 verifyFormat("double &operator[](int i) { return 0; }\n" 10404 "int i;"); 10405 verifyFormat("std::unique_ptr<int[]> foo() {}"); 10406 verifyFormat("int i = a[a][a]->f();"); 10407 verifyFormat("int i = (*b)[a]->f();"); 10408 10409 // Other corner cases. 10410 verifyFormat("void f() {\n" 10411 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n" 10412 " );\n" 10413 "}"); 10414 10415 // Lambdas created through weird macros. 10416 verifyFormat("void f() {\n" 10417 " MACRO((const AA &a) { return 1; });\n" 10418 " MACRO((AA &a) { return 1; });\n" 10419 "}"); 10420 10421 verifyFormat("if (blah_blah(whatever, whatever, [] {\n" 10422 " doo_dah();\n" 10423 " doo_dah();\n" 10424 " })) {\n" 10425 "}"); 10426 verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n" 10427 " doo_dah();\n" 10428 " doo_dah();\n" 10429 " })) {\n" 10430 "}"); 10431 verifyFormat("auto lambda = []() {\n" 10432 " int a = 2\n" 10433 "#if A\n" 10434 " + 2\n" 10435 "#endif\n" 10436 " ;\n" 10437 "};"); 10438 10439 // Lambdas with complex multiline introducers. 10440 verifyFormat( 10441 "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10442 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n" 10443 " -> ::std::unordered_set<\n" 10444 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n" 10445 " //\n" 10446 " });"); 10447 } 10448 10449 TEST_F(FormatTest, FormatsBlocks) { 10450 FormatStyle ShortBlocks = getLLVMStyle(); 10451 ShortBlocks.AllowShortBlocksOnASingleLine = true; 10452 verifyFormat("int (^Block)(int, int);", ShortBlocks); 10453 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks); 10454 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks); 10455 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks); 10456 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks); 10457 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks); 10458 10459 verifyFormat("foo(^{ bar(); });", ShortBlocks); 10460 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks); 10461 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks); 10462 10463 verifyFormat("[operation setCompletionBlock:^{\n" 10464 " [self onOperationDone];\n" 10465 "}];"); 10466 verifyFormat("int i = {[operation setCompletionBlock:^{\n" 10467 " [self onOperationDone];\n" 10468 "}]};"); 10469 verifyFormat("[operation setCompletionBlock:^(int *i) {\n" 10470 " f();\n" 10471 "}];"); 10472 verifyFormat("int a = [operation block:^int(int *i) {\n" 10473 " return 1;\n" 10474 "}];"); 10475 verifyFormat("[myObject doSomethingWith:arg1\n" 10476 " aaa:^int(int *a) {\n" 10477 " return 1;\n" 10478 " }\n" 10479 " bbb:f(a * bbbbbbbb)];"); 10480 10481 verifyFormat("[operation setCompletionBlock:^{\n" 10482 " [self.delegate newDataAvailable];\n" 10483 "}];", 10484 getLLVMStyleWithColumns(60)); 10485 verifyFormat("dispatch_async(_fileIOQueue, ^{\n" 10486 " NSString *path = [self sessionFilePath];\n" 10487 " if (path) {\n" 10488 " // ...\n" 10489 " }\n" 10490 "});"); 10491 verifyFormat("[[SessionService sharedService]\n" 10492 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 10493 " if (window) {\n" 10494 " [self windowDidLoad:window];\n" 10495 " } else {\n" 10496 " [self errorLoadingWindow];\n" 10497 " }\n" 10498 " }];"); 10499 verifyFormat("void (^largeBlock)(void) = ^{\n" 10500 " // ...\n" 10501 "};\n", 10502 getLLVMStyleWithColumns(40)); 10503 verifyFormat("[[SessionService sharedService]\n" 10504 " loadWindowWithCompletionBlock: //\n" 10505 " ^(SessionWindow *window) {\n" 10506 " if (window) {\n" 10507 " [self windowDidLoad:window];\n" 10508 " } else {\n" 10509 " [self errorLoadingWindow];\n" 10510 " }\n" 10511 " }];", 10512 getLLVMStyleWithColumns(60)); 10513 verifyFormat("[myObject doSomethingWith:arg1\n" 10514 " firstBlock:^(Foo *a) {\n" 10515 " // ...\n" 10516 " int i;\n" 10517 " }\n" 10518 " secondBlock:^(Bar *b) {\n" 10519 " // ...\n" 10520 " int i;\n" 10521 " }\n" 10522 " thirdBlock:^Foo(Bar *b) {\n" 10523 " // ...\n" 10524 " int i;\n" 10525 " }];"); 10526 verifyFormat("[myObject doSomethingWith:arg1\n" 10527 " firstBlock:-1\n" 10528 " secondBlock:^(Bar *b) {\n" 10529 " // ...\n" 10530 " int i;\n" 10531 " }];"); 10532 10533 verifyFormat("f(^{\n" 10534 " @autoreleasepool {\n" 10535 " if (a) {\n" 10536 " g();\n" 10537 " }\n" 10538 " }\n" 10539 "});"); 10540 verifyFormat("Block b = ^int *(A *a, B *b) {}"); 10541 verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n" 10542 "};"); 10543 10544 FormatStyle FourIndent = getLLVMStyle(); 10545 FourIndent.ObjCBlockIndentWidth = 4; 10546 verifyFormat("[operation setCompletionBlock:^{\n" 10547 " [self onOperationDone];\n" 10548 "}];", 10549 FourIndent); 10550 } 10551 10552 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) { 10553 FormatStyle ZeroColumn = getLLVMStyle(); 10554 ZeroColumn.ColumnLimit = 0; 10555 10556 verifyFormat("[[SessionService sharedService] " 10557 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 10558 " if (window) {\n" 10559 " [self windowDidLoad:window];\n" 10560 " } else {\n" 10561 " [self errorLoadingWindow];\n" 10562 " }\n" 10563 "}];", 10564 ZeroColumn); 10565 EXPECT_EQ("[[SessionService sharedService]\n" 10566 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 10567 " if (window) {\n" 10568 " [self windowDidLoad:window];\n" 10569 " } else {\n" 10570 " [self errorLoadingWindow];\n" 10571 " }\n" 10572 " }];", 10573 format("[[SessionService sharedService]\n" 10574 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 10575 " if (window) {\n" 10576 " [self windowDidLoad:window];\n" 10577 " } else {\n" 10578 " [self errorLoadingWindow];\n" 10579 " }\n" 10580 "}];", 10581 ZeroColumn)); 10582 verifyFormat("[myObject doSomethingWith:arg1\n" 10583 " firstBlock:^(Foo *a) {\n" 10584 " // ...\n" 10585 " int i;\n" 10586 " }\n" 10587 " secondBlock:^(Bar *b) {\n" 10588 " // ...\n" 10589 " int i;\n" 10590 " }\n" 10591 " thirdBlock:^Foo(Bar *b) {\n" 10592 " // ...\n" 10593 " int i;\n" 10594 " }];", 10595 ZeroColumn); 10596 verifyFormat("f(^{\n" 10597 " @autoreleasepool {\n" 10598 " if (a) {\n" 10599 " g();\n" 10600 " }\n" 10601 " }\n" 10602 "});", 10603 ZeroColumn); 10604 verifyFormat("void (^largeBlock)(void) = ^{\n" 10605 " // ...\n" 10606 "};", 10607 ZeroColumn); 10608 10609 ZeroColumn.AllowShortBlocksOnASingleLine = true; 10610 EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };", 10611 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 10612 ZeroColumn.AllowShortBlocksOnASingleLine = false; 10613 EXPECT_EQ("void (^largeBlock)(void) = ^{\n" 10614 " int i;\n" 10615 "};", 10616 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 10617 } 10618 10619 TEST_F(FormatTest, SupportsCRLF) { 10620 EXPECT_EQ("int a;\r\n" 10621 "int b;\r\n" 10622 "int c;\r\n", 10623 format("int a;\r\n" 10624 " int b;\r\n" 10625 " int c;\r\n", 10626 getLLVMStyle())); 10627 EXPECT_EQ("int a;\r\n" 10628 "int b;\r\n" 10629 "int c;\r\n", 10630 format("int a;\r\n" 10631 " int b;\n" 10632 " int c;\r\n", 10633 getLLVMStyle())); 10634 EXPECT_EQ("int a;\n" 10635 "int b;\n" 10636 "int c;\n", 10637 format("int a;\r\n" 10638 " int b;\n" 10639 " int c;\n", 10640 getLLVMStyle())); 10641 EXPECT_EQ("\"aaaaaaa \"\r\n" 10642 "\"bbbbbbb\";\r\n", 10643 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10))); 10644 EXPECT_EQ("#define A \\\r\n" 10645 " b; \\\r\n" 10646 " c; \\\r\n" 10647 " d;\r\n", 10648 format("#define A \\\r\n" 10649 " b; \\\r\n" 10650 " c; d; \r\n", 10651 getGoogleStyle())); 10652 10653 EXPECT_EQ("/*\r\n" 10654 "multi line block comments\r\n" 10655 "should not introduce\r\n" 10656 "an extra carriage return\r\n" 10657 "*/\r\n", 10658 format("/*\r\n" 10659 "multi line block comments\r\n" 10660 "should not introduce\r\n" 10661 "an extra carriage return\r\n" 10662 "*/\r\n")); 10663 } 10664 10665 TEST_F(FormatTest, MunchSemicolonAfterBlocks) { 10666 verifyFormat("MY_CLASS(C) {\n" 10667 " int i;\n" 10668 " int j;\n" 10669 "};"); 10670 } 10671 10672 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) { 10673 FormatStyle TwoIndent = getLLVMStyleWithColumns(15); 10674 TwoIndent.ContinuationIndentWidth = 2; 10675 10676 EXPECT_EQ("int i =\n" 10677 " longFunction(\n" 10678 " arg);", 10679 format("int i = longFunction(arg);", TwoIndent)); 10680 10681 FormatStyle SixIndent = getLLVMStyleWithColumns(20); 10682 SixIndent.ContinuationIndentWidth = 6; 10683 10684 EXPECT_EQ("int i =\n" 10685 " longFunction(\n" 10686 " arg);", 10687 format("int i = longFunction(arg);", SixIndent)); 10688 } 10689 10690 TEST_F(FormatTest, SpacesInAngles) { 10691 FormatStyle Spaces = getLLVMStyle(); 10692 Spaces.SpacesInAngles = true; 10693 10694 verifyFormat("static_cast< int >(arg);", Spaces); 10695 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces); 10696 verifyFormat("f< int, float >();", Spaces); 10697 verifyFormat("template <> g() {}", Spaces); 10698 verifyFormat("template < std::vector< int > > f() {}", Spaces); 10699 verifyFormat("std::function< void(int, int) > fct;", Spaces); 10700 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }", 10701 Spaces); 10702 10703 Spaces.Standard = FormatStyle::LS_Cpp03; 10704 Spaces.SpacesInAngles = true; 10705 verifyFormat("A< A< int > >();", Spaces); 10706 10707 Spaces.SpacesInAngles = false; 10708 verifyFormat("A<A<int> >();", Spaces); 10709 10710 Spaces.Standard = FormatStyle::LS_Cpp11; 10711 Spaces.SpacesInAngles = true; 10712 verifyFormat("A< A< int > >();", Spaces); 10713 10714 Spaces.SpacesInAngles = false; 10715 verifyFormat("A<A<int>>();", Spaces); 10716 } 10717 10718 TEST_F(FormatTest, SpaceAfterTemplateKeyword) { 10719 FormatStyle Style = getLLVMStyle(); 10720 Style.SpaceAfterTemplateKeyword = false; 10721 verifyFormat("template<int> void foo();", Style); 10722 } 10723 10724 TEST_F(FormatTest, TripleAngleBrackets) { 10725 verifyFormat("f<<<1, 1>>>();"); 10726 verifyFormat("f<<<1, 1, 1, s>>>();"); 10727 verifyFormat("f<<<a, b, c, d>>>();"); 10728 EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();")); 10729 verifyFormat("f<param><<<1, 1>>>();"); 10730 verifyFormat("f<1><<<1, 1>>>();"); 10731 EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();")); 10732 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 10733 "aaaaaaaaaaa<<<\n 1, 1>>>();"); 10734 verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n" 10735 " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();"); 10736 } 10737 10738 TEST_F(FormatTest, MergeLessLessAtEnd) { 10739 verifyFormat("<<"); 10740 EXPECT_EQ("< < <", format("\\\n<<<")); 10741 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 10742 "aaallvm::outs() <<"); 10743 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 10744 "aaaallvm::outs()\n <<"); 10745 } 10746 10747 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) { 10748 std::string code = "#if A\n" 10749 "#if B\n" 10750 "a.\n" 10751 "#endif\n" 10752 " a = 1;\n" 10753 "#else\n" 10754 "#endif\n" 10755 "#if C\n" 10756 "#else\n" 10757 "#endif\n"; 10758 EXPECT_EQ(code, format(code)); 10759 } 10760 10761 TEST_F(FormatTest, HandleConflictMarkers) { 10762 // Git/SVN conflict markers. 10763 EXPECT_EQ("int a;\n" 10764 "void f() {\n" 10765 " callme(some(parameter1,\n" 10766 "<<<<<<< text by the vcs\n" 10767 " parameter2),\n" 10768 "||||||| text by the vcs\n" 10769 " parameter2),\n" 10770 " parameter3,\n" 10771 "======= text by the vcs\n" 10772 " parameter2, parameter3),\n" 10773 ">>>>>>> text by the vcs\n" 10774 " otherparameter);\n", 10775 format("int a;\n" 10776 "void f() {\n" 10777 " callme(some(parameter1,\n" 10778 "<<<<<<< text by the vcs\n" 10779 " parameter2),\n" 10780 "||||||| text by the vcs\n" 10781 " parameter2),\n" 10782 " parameter3,\n" 10783 "======= text by the vcs\n" 10784 " parameter2,\n" 10785 " parameter3),\n" 10786 ">>>>>>> text by the vcs\n" 10787 " otherparameter);\n")); 10788 10789 // Perforce markers. 10790 EXPECT_EQ("void f() {\n" 10791 " function(\n" 10792 ">>>> text by the vcs\n" 10793 " parameter,\n" 10794 "==== text by the vcs\n" 10795 " parameter,\n" 10796 "==== text by the vcs\n" 10797 " parameter,\n" 10798 "<<<< text by the vcs\n" 10799 " parameter);\n", 10800 format("void f() {\n" 10801 " function(\n" 10802 ">>>> text by the vcs\n" 10803 " parameter,\n" 10804 "==== text by the vcs\n" 10805 " parameter,\n" 10806 "==== text by the vcs\n" 10807 " parameter,\n" 10808 "<<<< text by the vcs\n" 10809 " parameter);\n")); 10810 10811 EXPECT_EQ("<<<<<<<\n" 10812 "|||||||\n" 10813 "=======\n" 10814 ">>>>>>>", 10815 format("<<<<<<<\n" 10816 "|||||||\n" 10817 "=======\n" 10818 ">>>>>>>")); 10819 10820 EXPECT_EQ("<<<<<<<\n" 10821 "|||||||\n" 10822 "int i;\n" 10823 "=======\n" 10824 ">>>>>>>", 10825 format("<<<<<<<\n" 10826 "|||||||\n" 10827 "int i;\n" 10828 "=======\n" 10829 ">>>>>>>")); 10830 10831 // FIXME: Handle parsing of macros around conflict markers correctly: 10832 EXPECT_EQ("#define Macro \\\n" 10833 "<<<<<<<\n" 10834 "Something \\\n" 10835 "|||||||\n" 10836 "Else \\\n" 10837 "=======\n" 10838 "Other \\\n" 10839 ">>>>>>>\n" 10840 " End int i;\n", 10841 format("#define Macro \\\n" 10842 "<<<<<<<\n" 10843 " Something \\\n" 10844 "|||||||\n" 10845 " Else \\\n" 10846 "=======\n" 10847 " Other \\\n" 10848 ">>>>>>>\n" 10849 " End\n" 10850 "int i;\n")); 10851 } 10852 10853 TEST_F(FormatTest, DisableRegions) { 10854 EXPECT_EQ("int i;\n" 10855 "// clang-format off\n" 10856 " int j;\n" 10857 "// clang-format on\n" 10858 "int k;", 10859 format(" int i;\n" 10860 " // clang-format off\n" 10861 " int j;\n" 10862 " // clang-format on\n" 10863 " int k;")); 10864 EXPECT_EQ("int i;\n" 10865 "/* clang-format off */\n" 10866 " int j;\n" 10867 "/* clang-format on */\n" 10868 "int k;", 10869 format(" int i;\n" 10870 " /* clang-format off */\n" 10871 " int j;\n" 10872 " /* clang-format on */\n" 10873 " int k;")); 10874 10875 // Don't reflow comments within disabled regions. 10876 EXPECT_EQ( 10877 "// clang-format off\n" 10878 "// long long long long long long line\n" 10879 "/* clang-format on */\n" 10880 "/* long long long\n" 10881 " * long long long\n" 10882 " * line */\n" 10883 "int i;\n" 10884 "/* clang-format off */\n" 10885 "/* long long long long long long line */\n", 10886 format("// clang-format off\n" 10887 "// long long long long long long line\n" 10888 "/* clang-format on */\n" 10889 "/* long long long long long long line */\n" 10890 "int i;\n" 10891 "/* clang-format off */\n" 10892 "/* long long long long long long line */\n", 10893 getLLVMStyleWithColumns(20))); 10894 } 10895 10896 TEST_F(FormatTest, DoNotCrashOnInvalidInput) { 10897 format("? ) ="); 10898 verifyNoCrash("#define a\\\n /**/}"); 10899 } 10900 10901 TEST_F(FormatTest, FormatsTableGenCode) { 10902 FormatStyle Style = getLLVMStyle(); 10903 Style.Language = FormatStyle::LK_TableGen; 10904 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style); 10905 } 10906 10907 TEST_F(FormatTest, ArrayOfTemplates) { 10908 EXPECT_EQ("auto a = new unique_ptr<int>[10];", 10909 format("auto a = new unique_ptr<int > [ 10];")); 10910 10911 FormatStyle Spaces = getLLVMStyle(); 10912 Spaces.SpacesInSquareBrackets = true; 10913 EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];", 10914 format("auto a = new unique_ptr<int > [10];", Spaces)); 10915 } 10916 10917 TEST_F(FormatTest, ArrayAsTemplateType) { 10918 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;", 10919 format("auto a = unique_ptr < Foo < Bar>[ 10]> ;")); 10920 10921 FormatStyle Spaces = getLLVMStyle(); 10922 Spaces.SpacesInSquareBrackets = true; 10923 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;", 10924 format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces)); 10925 } 10926 10927 TEST_F(FormatTest, NoSpaceAfterSuper) { 10928 verifyFormat("__super::FooBar();"); 10929 } 10930 10931 TEST(FormatStyle, GetStyleOfFile) { 10932 vfs::InMemoryFileSystem FS; 10933 // Test 1: format file in the same directory. 10934 ASSERT_TRUE( 10935 FS.addFile("/a/.clang-format", 0, 10936 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM"))); 10937 ASSERT_TRUE( 10938 FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 10939 auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS); 10940 ASSERT_TRUE((bool)Style1); 10941 ASSERT_EQ(*Style1, getLLVMStyle()); 10942 10943 // Test 2.1: fallback to default. 10944 ASSERT_TRUE( 10945 FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 10946 auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS); 10947 ASSERT_TRUE((bool)Style2); 10948 ASSERT_EQ(*Style2, getMozillaStyle()); 10949 10950 // Test 2.2: no format on 'none' fallback style. 10951 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 10952 ASSERT_TRUE((bool)Style2); 10953 ASSERT_EQ(*Style2, getNoStyle()); 10954 10955 // Test 2.3: format if config is found with no based style while fallback is 10956 // 'none'. 10957 ASSERT_TRUE(FS.addFile("/b/.clang-format", 0, 10958 llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2"))); 10959 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 10960 ASSERT_TRUE((bool)Style2); 10961 ASSERT_EQ(*Style2, getLLVMStyle()); 10962 10963 // Test 2.4: format if yaml with no based style, while fallback is 'none'. 10964 Style2 = getStyle("{}", "a.h", "none", "", &FS); 10965 ASSERT_TRUE((bool)Style2); 10966 ASSERT_EQ(*Style2, getLLVMStyle()); 10967 10968 // Test 3: format file in parent directory. 10969 ASSERT_TRUE( 10970 FS.addFile("/c/.clang-format", 0, 10971 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 10972 ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0, 10973 llvm::MemoryBuffer::getMemBuffer("int i;"))); 10974 auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS); 10975 ASSERT_TRUE((bool)Style3); 10976 ASSERT_EQ(*Style3, getGoogleStyle()); 10977 10978 // Test 4: error on invalid fallback style 10979 auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS); 10980 ASSERT_FALSE((bool)Style4); 10981 llvm::consumeError(Style4.takeError()); 10982 10983 // Test 5: error on invalid yaml on command line 10984 auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS); 10985 ASSERT_FALSE((bool)Style5); 10986 llvm::consumeError(Style5.takeError()); 10987 10988 // Test 6: error on invalid style 10989 auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS); 10990 ASSERT_FALSE((bool)Style6); 10991 llvm::consumeError(Style6.takeError()); 10992 10993 // Test 7: found config file, error on parsing it 10994 ASSERT_TRUE( 10995 FS.addFile("/d/.clang-format", 0, 10996 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n" 10997 "InvalidKey: InvalidValue"))); 10998 ASSERT_TRUE( 10999 FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 11000 auto Style7 = getStyle("file", "/d/.clang-format", "LLVM", "", &FS); 11001 ASSERT_FALSE((bool)Style7); 11002 llvm::consumeError(Style7.takeError()); 11003 } 11004 11005 TEST_F(ReplacementTest, FormatCodeAfterReplacements) { 11006 // Column limit is 20. 11007 std::string Code = "Type *a =\n" 11008 " new Type();\n" 11009 "g(iiiii, 0, jjjjj,\n" 11010 " 0, kkkkk, 0, mm);\n" 11011 "int bad = format ;"; 11012 std::string Expected = "auto a = new Type();\n" 11013 "g(iiiii, nullptr,\n" 11014 " jjjjj, nullptr,\n" 11015 " kkkkk, nullptr,\n" 11016 " mm);\n" 11017 "int bad = format ;"; 11018 FileID ID = Context.createInMemoryFile("format.cpp", Code); 11019 tooling::Replacements Replaces = toReplacements( 11020 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6, 11021 "auto "), 11022 tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1, 11023 "nullptr"), 11024 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1, 11025 "nullptr"), 11026 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1, 11027 "nullptr")}); 11028 11029 format::FormatStyle Style = format::getLLVMStyle(); 11030 Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility. 11031 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 11032 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 11033 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 11034 auto Result = applyAllReplacements(Code, *FormattedReplaces); 11035 EXPECT_TRUE(static_cast<bool>(Result)); 11036 EXPECT_EQ(Expected, *Result); 11037 } 11038 11039 TEST_F(ReplacementTest, SortIncludesAfterReplacement) { 11040 std::string Code = "#include \"a.h\"\n" 11041 "#include \"c.h\"\n" 11042 "\n" 11043 "int main() {\n" 11044 " return 0;\n" 11045 "}"; 11046 std::string Expected = "#include \"a.h\"\n" 11047 "#include \"b.h\"\n" 11048 "#include \"c.h\"\n" 11049 "\n" 11050 "int main() {\n" 11051 " return 0;\n" 11052 "}"; 11053 FileID ID = Context.createInMemoryFile("fix.cpp", Code); 11054 tooling::Replacements Replaces = toReplacements( 11055 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0, 11056 "#include \"b.h\"\n")}); 11057 11058 format::FormatStyle Style = format::getLLVMStyle(); 11059 Style.SortIncludes = true; 11060 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 11061 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 11062 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 11063 auto Result = applyAllReplacements(Code, *FormattedReplaces); 11064 EXPECT_TRUE(static_cast<bool>(Result)); 11065 EXPECT_EQ(Expected, *Result); 11066 } 11067 11068 TEST_F(FormatTest, FormatSortsUsingDeclarations) { 11069 EXPECT_EQ("using std::cin;\n" 11070 "using std::cout;", 11071 format("using std::cout;\n" 11072 "using std::cin;", getGoogleStyle())); 11073 } 11074 11075 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) { 11076 format::FormatStyle Style = format::getLLVMStyle(); 11077 Style.Standard = FormatStyle::LS_Cpp03; 11078 // cpp03 recognize this string as identifier u8 and literal character 'a' 11079 EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style)); 11080 } 11081 11082 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) { 11083 // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers 11084 // all modes, including C++11, C++14 and C++17 11085 EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';")); 11086 } 11087 11088 } // end namespace 11089 } // end namespace format 11090 } // end namespace clang 11091