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 "FormatTestUtils.h" 11 #include "clang/Format/Format.h" 12 #include "llvm/Support/Debug.h" 13 #include "gtest/gtest.h" 14 15 #define DEBUG_TYPE "format-test" 16 17 namespace clang { 18 namespace format { 19 20 FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); } 21 22 class FormatTest : public ::testing::Test { 23 protected: 24 enum IncompleteCheck { 25 IC_ExpectComplete, 26 IC_ExpectIncomplete, 27 IC_DoNotCheck 28 }; 29 30 std::string format(llvm::StringRef Code, unsigned Offset, unsigned Length, 31 const FormatStyle &Style, 32 IncompleteCheck CheckIncomplete = IC_ExpectComplete) { 33 DEBUG(llvm::errs() << "---\n"); 34 DEBUG(llvm::errs() << Code << "\n\n"); 35 std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length)); 36 bool IncompleteFormat = false; 37 tooling::Replacements Replaces = 38 reformat(Style, Code, Ranges, "<stdin>", &IncompleteFormat); 39 if (CheckIncomplete != IC_DoNotCheck) { 40 bool ExpectedIncompleteFormat = CheckIncomplete == IC_ExpectIncomplete; 41 EXPECT_EQ(ExpectedIncompleteFormat, IncompleteFormat) << Code << "\n\n"; 42 } 43 ReplacementCount = Replaces.size(); 44 std::string Result = applyAllReplacements(Code, Replaces); 45 EXPECT_NE("", Result); 46 DEBUG(llvm::errs() << "\n" << Result << "\n\n"); 47 return Result; 48 } 49 50 std::string format(llvm::StringRef Code, 51 const FormatStyle &Style = getLLVMStyle(), 52 IncompleteCheck CheckIncomplete = IC_ExpectComplete) { 53 return format(Code, 0, Code.size(), Style, CheckIncomplete); 54 } 55 56 FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) { 57 FormatStyle Style = getLLVMStyle(); 58 Style.ColumnLimit = ColumnLimit; 59 return Style; 60 } 61 62 FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) { 63 FormatStyle Style = getGoogleStyle(); 64 Style.ColumnLimit = ColumnLimit; 65 return Style; 66 } 67 68 void verifyFormat(llvm::StringRef Code, 69 const FormatStyle &Style = getLLVMStyle()) { 70 EXPECT_EQ(Code.str(), format(test::messUp(Code), Style)); 71 } 72 73 void verifyIncompleteFormat(llvm::StringRef Code, 74 const FormatStyle &Style = getLLVMStyle()) { 75 EXPECT_EQ(Code.str(), 76 format(test::messUp(Code), Style, IC_ExpectIncomplete)); 77 } 78 79 void verifyGoogleFormat(llvm::StringRef Code) { 80 verifyFormat(Code, getGoogleStyle()); 81 } 82 83 void verifyIndependentOfContext(llvm::StringRef text) { 84 verifyFormat(text); 85 verifyFormat(llvm::Twine("void f() { " + text + " }").str()); 86 } 87 88 /// \brief Verify that clang-format does not crash on the given input. 89 void verifyNoCrash(llvm::StringRef Code, 90 const FormatStyle &Style = getLLVMStyle()) { 91 format(Code, Style, IC_DoNotCheck); 92 } 93 94 int ReplacementCount; 95 }; 96 97 TEST_F(FormatTest, MessUp) { 98 EXPECT_EQ("1 2 3", test::messUp("1 2 3")); 99 EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n")); 100 EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc")); 101 EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc")); 102 EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne")); 103 } 104 105 //===----------------------------------------------------------------------===// 106 // Basic function tests. 107 //===----------------------------------------------------------------------===// 108 109 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) { 110 EXPECT_EQ(";", format(";")); 111 } 112 113 TEST_F(FormatTest, FormatsGlobalStatementsAt0) { 114 EXPECT_EQ("int i;", format(" int i;")); 115 EXPECT_EQ("\nint i;", format(" \n\t \v \f int i;")); 116 EXPECT_EQ("int i;\nint j;", format(" int i; int j;")); 117 EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;")); 118 } 119 120 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) { 121 EXPECT_EQ("int i;", format("int\ni;")); 122 } 123 124 TEST_F(FormatTest, FormatsNestedBlockStatements) { 125 EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}")); 126 } 127 128 TEST_F(FormatTest, FormatsNestedCall) { 129 verifyFormat("Method(f1, f2(f3));"); 130 verifyFormat("Method(f1(f2, f3()));"); 131 verifyFormat("Method(f1(f2, (f3())));"); 132 } 133 134 TEST_F(FormatTest, NestedNameSpecifiers) { 135 verifyFormat("vector<::Type> v;"); 136 verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())"); 137 verifyFormat("static constexpr bool Bar = decltype(bar())::value;"); 138 verifyFormat("bool a = 2 < ::SomeFunction();"); 139 } 140 141 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) { 142 EXPECT_EQ("if (a) {\n" 143 " f();\n" 144 "}", 145 format("if(a){f();}")); 146 EXPECT_EQ(4, ReplacementCount); 147 EXPECT_EQ("if (a) {\n" 148 " f();\n" 149 "}", 150 format("if (a) {\n" 151 " f();\n" 152 "}")); 153 EXPECT_EQ(0, ReplacementCount); 154 } 155 156 TEST_F(FormatTest, RemovesTrailingWhitespaceOfFormattedLine) { 157 EXPECT_EQ("int a;\nint b;", format("int a; \nint b;", 0, 0, getLLVMStyle())); 158 EXPECT_EQ("int a;", format("int a; ")); 159 EXPECT_EQ("int a;\n", format("int a; \n \n \n ")); 160 EXPECT_EQ("int a;\nint b; ", 161 format("int a; \nint b; ", 0, 0, getLLVMStyle())); 162 } 163 164 TEST_F(FormatTest, FormatsCorrectRegionForLeadingWhitespace) { 165 EXPECT_EQ("int b;\nint a;", 166 format("int b;\n int a;", 7, 0, getLLVMStyle())); 167 EXPECT_EQ("int b;\n int a;", 168 format("int b;\n int a;", 6, 0, getLLVMStyle())); 169 170 EXPECT_EQ("#define A \\\n" 171 " int a; \\\n" 172 " int b;", 173 format("#define A \\\n" 174 " int a; \\\n" 175 " int b;", 176 26, 0, getLLVMStyleWithColumns(12))); 177 EXPECT_EQ("#define A \\\n" 178 " int a; \\\n" 179 " int b;", 180 format("#define A \\\n" 181 " int a; \\\n" 182 " int b;", 183 25, 0, getLLVMStyleWithColumns(12))); 184 } 185 186 TEST_F(FormatTest, FormatLineWhenInvokedOnTrailingNewline) { 187 EXPECT_EQ("int b;\n\nint a;", 188 format("int b;\n\nint a;", 8, 0, getLLVMStyle())); 189 EXPECT_EQ("int b;\n\nint a;", 190 format("int b;\n\nint a;", 7, 0, getLLVMStyle())); 191 192 // This might not strictly be correct, but is likely good in all practical 193 // cases. 194 EXPECT_EQ("int b;\nint a;", format("int b;int a;", 7, 0, getLLVMStyle())); 195 } 196 197 TEST_F(FormatTest, RemovesWhitespaceWhenTriggeredOnEmptyLine) { 198 EXPECT_EQ("int a;\n\n int b;", 199 format("int a;\n \n\n int b;", 8, 0, getLLVMStyle())); 200 EXPECT_EQ("int a;\n\n int b;", 201 format("int a;\n \n\n int b;", 9, 0, getLLVMStyle())); 202 } 203 204 TEST_F(FormatTest, RemovesEmptyLines) { 205 EXPECT_EQ("class C {\n" 206 " int i;\n" 207 "};", 208 format("class C {\n" 209 " int i;\n" 210 "\n" 211 "};")); 212 213 // Don't remove empty lines at the start of namespaces or extern "C" blocks. 214 EXPECT_EQ("namespace N {\n" 215 "\n" 216 "int i;\n" 217 "}", 218 format("namespace N {\n" 219 "\n" 220 "int i;\n" 221 "}", 222 getGoogleStyle())); 223 EXPECT_EQ("extern /**/ \"C\" /**/ {\n" 224 "\n" 225 "int i;\n" 226 "}", 227 format("extern /**/ \"C\" /**/ {\n" 228 "\n" 229 "int i;\n" 230 "}", 231 getGoogleStyle())); 232 233 // ...but do keep inlining and removing empty lines for non-block extern "C" 234 // functions. 235 verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle()); 236 EXPECT_EQ("extern \"C\" int f() {\n" 237 " int i = 42;\n" 238 " return i;\n" 239 "}", 240 format("extern \"C\" int f() {\n" 241 "\n" 242 " int i = 42;\n" 243 " return i;\n" 244 "}", 245 getGoogleStyle())); 246 247 // Remove empty lines at the beginning and end of blocks. 248 EXPECT_EQ("void f() {\n" 249 "\n" 250 " if (a) {\n" 251 "\n" 252 " f();\n" 253 " }\n" 254 "}", 255 format("void f() {\n" 256 "\n" 257 " if (a) {\n" 258 "\n" 259 " f();\n" 260 "\n" 261 " }\n" 262 "\n" 263 "}", 264 getLLVMStyle())); 265 EXPECT_EQ("void f() {\n" 266 " if (a) {\n" 267 " f();\n" 268 " }\n" 269 "}", 270 format("void f() {\n" 271 "\n" 272 " if (a) {\n" 273 "\n" 274 " f();\n" 275 "\n" 276 " }\n" 277 "\n" 278 "}", 279 getGoogleStyle())); 280 281 // Don't remove empty lines in more complex control statements. 282 EXPECT_EQ("void f() {\n" 283 " if (a) {\n" 284 " f();\n" 285 "\n" 286 " } else if (b) {\n" 287 " f();\n" 288 " }\n" 289 "}", 290 format("void f() {\n" 291 " if (a) {\n" 292 " f();\n" 293 "\n" 294 " } else if (b) {\n" 295 " f();\n" 296 "\n" 297 " }\n" 298 "\n" 299 "}")); 300 301 // FIXME: This is slightly inconsistent. 302 EXPECT_EQ("namespace {\n" 303 "int i;\n" 304 "}", 305 format("namespace {\n" 306 "int i;\n" 307 "\n" 308 "}")); 309 EXPECT_EQ("namespace {\n" 310 "int i;\n" 311 "\n" 312 "} // namespace", 313 format("namespace {\n" 314 "int i;\n" 315 "\n" 316 "} // namespace")); 317 } 318 319 TEST_F(FormatTest, ReformatsMovedLines) { 320 EXPECT_EQ( 321 "template <typename T> T *getFETokenInfo() const {\n" 322 " return static_cast<T *>(FETokenInfo);\n" 323 "}\n" 324 " int a; // <- Should not be formatted", 325 format( 326 "template<typename T>\n" 327 "T *getFETokenInfo() const { return static_cast<T*>(FETokenInfo); }\n" 328 " int a; // <- Should not be formatted", 329 9, 5, getLLVMStyle())); 330 } 331 332 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) { 333 verifyFormat("x = (a) and (b);"); 334 verifyFormat("x = (a) or (b);"); 335 verifyFormat("x = (a) bitand (b);"); 336 verifyFormat("x = (a) bitor (b);"); 337 verifyFormat("x = (a) not_eq (b);"); 338 verifyFormat("x = (a) and_eq (b);"); 339 verifyFormat("x = (a) or_eq (b);"); 340 verifyFormat("x = (a) xor (b);"); 341 } 342 343 //===----------------------------------------------------------------------===// 344 // Tests for control statements. 345 //===----------------------------------------------------------------------===// 346 347 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) { 348 verifyFormat("if (true)\n f();\ng();"); 349 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); 350 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); 351 352 FormatStyle AllowsMergedIf = getLLVMStyle(); 353 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 354 verifyFormat("if (a)\n" 355 " // comment\n" 356 " f();", 357 AllowsMergedIf); 358 verifyFormat("if (a)\n" 359 " ;", 360 AllowsMergedIf); 361 verifyFormat("if (a)\n" 362 " if (b) return;", 363 AllowsMergedIf); 364 365 verifyFormat("if (a) // Can't merge this\n" 366 " f();\n", 367 AllowsMergedIf); 368 verifyFormat("if (a) /* still don't merge */\n" 369 " f();", 370 AllowsMergedIf); 371 verifyFormat("if (a) { // Never merge this\n" 372 " f();\n" 373 "}", 374 AllowsMergedIf); 375 verifyFormat("if (a) {/* Never merge this */\n" 376 " f();\n" 377 "}", 378 AllowsMergedIf); 379 380 EXPECT_EQ("if (a) return;", format("if(a)\nreturn;", 7, 1, AllowsMergedIf)); 381 EXPECT_EQ("if (a) return; // comment", 382 format("if(a)\nreturn; // comment", 20, 1, AllowsMergedIf)); 383 384 AllowsMergedIf.ColumnLimit = 14; 385 verifyFormat("if (a) return;", AllowsMergedIf); 386 verifyFormat("if (aaaaaaaaa)\n" 387 " return;", 388 AllowsMergedIf); 389 390 AllowsMergedIf.ColumnLimit = 13; 391 verifyFormat("if (a)\n return;", AllowsMergedIf); 392 } 393 394 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) { 395 FormatStyle AllowsMergedLoops = getLLVMStyle(); 396 AllowsMergedLoops.AllowShortLoopsOnASingleLine = true; 397 verifyFormat("while (true) continue;", AllowsMergedLoops); 398 verifyFormat("for (;;) continue;", AllowsMergedLoops); 399 verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops); 400 verifyFormat("while (true)\n" 401 " ;", 402 AllowsMergedLoops); 403 verifyFormat("for (;;)\n" 404 " ;", 405 AllowsMergedLoops); 406 verifyFormat("for (;;)\n" 407 " for (;;) continue;", 408 AllowsMergedLoops); 409 verifyFormat("for (;;) // Can't merge this\n" 410 " continue;", 411 AllowsMergedLoops); 412 verifyFormat("for (;;) /* still don't merge */\n" 413 " continue;", 414 AllowsMergedLoops); 415 } 416 417 TEST_F(FormatTest, FormatShortBracedStatements) { 418 FormatStyle AllowSimpleBracedStatements = getLLVMStyle(); 419 AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true; 420 421 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true; 422 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 423 424 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 425 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 426 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 427 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 428 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 429 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 430 verifyFormat("if (true) { //\n" 431 " f();\n" 432 "}", 433 AllowSimpleBracedStatements); 434 verifyFormat("if (true) {\n" 435 " f();\n" 436 " f();\n" 437 "}", 438 AllowSimpleBracedStatements); 439 verifyFormat("if (true) {\n" 440 " f();\n" 441 "} else {\n" 442 " f();\n" 443 "}", 444 AllowSimpleBracedStatements); 445 446 verifyFormat("template <int> struct A2 {\n" 447 " struct B {};\n" 448 "};", 449 AllowSimpleBracedStatements); 450 451 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false; 452 verifyFormat("if (true) {\n" 453 " f();\n" 454 "}", 455 AllowSimpleBracedStatements); 456 verifyFormat("if (true) {\n" 457 " f();\n" 458 "} else {\n" 459 " f();\n" 460 "}", 461 AllowSimpleBracedStatements); 462 463 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 464 verifyFormat("while (true) {\n" 465 " f();\n" 466 "}", 467 AllowSimpleBracedStatements); 468 verifyFormat("for (;;) {\n" 469 " f();\n" 470 "}", 471 AllowSimpleBracedStatements); 472 } 473 474 TEST_F(FormatTest, ParseIfElse) { 475 verifyFormat("if (true)\n" 476 " if (true)\n" 477 " if (true)\n" 478 " f();\n" 479 " else\n" 480 " g();\n" 481 " else\n" 482 " h();\n" 483 "else\n" 484 " i();"); 485 verifyFormat("if (true)\n" 486 " if (true)\n" 487 " if (true) {\n" 488 " if (true)\n" 489 " f();\n" 490 " } else {\n" 491 " g();\n" 492 " }\n" 493 " else\n" 494 " h();\n" 495 "else {\n" 496 " i();\n" 497 "}"); 498 verifyFormat("void f() {\n" 499 " if (a) {\n" 500 " } else {\n" 501 " }\n" 502 "}"); 503 } 504 505 TEST_F(FormatTest, ElseIf) { 506 verifyFormat("if (a) {\n} else if (b) {\n}"); 507 verifyFormat("if (a)\n" 508 " f();\n" 509 "else if (b)\n" 510 " g();\n" 511 "else\n" 512 " h();"); 513 verifyFormat("if (a) {\n" 514 " f();\n" 515 "}\n" 516 "// or else ..\n" 517 "else {\n" 518 " g()\n" 519 "}"); 520 521 verifyFormat("if (a) {\n" 522 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 523 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 524 "}"); 525 verifyFormat("if (a) {\n" 526 "} else if (\n" 527 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 528 "}", 529 getLLVMStyleWithColumns(62)); 530 } 531 532 TEST_F(FormatTest, FormatsForLoop) { 533 verifyFormat( 534 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" 535 " ++VeryVeryLongLoopVariable)\n" 536 " ;"); 537 verifyFormat("for (;;)\n" 538 " f();"); 539 verifyFormat("for (;;) {\n}"); 540 verifyFormat("for (;;) {\n" 541 " f();\n" 542 "}"); 543 verifyFormat("for (int i = 0; (i < 10); ++i) {\n}"); 544 545 verifyFormat( 546 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 547 " E = UnwrappedLines.end();\n" 548 " I != E; ++I) {\n}"); 549 550 verifyFormat( 551 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" 552 " ++IIIII) {\n}"); 553 verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n" 554 " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n" 555 " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}"); 556 verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n" 557 " I = FD->getDeclsInPrototypeScope().begin(),\n" 558 " E = FD->getDeclsInPrototypeScope().end();\n" 559 " I != E; ++I) {\n}"); 560 verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n" 561 " I = Container.begin(),\n" 562 " E = Container.end();\n" 563 " I != E; ++I) {\n}", 564 getLLVMStyleWithColumns(76)); 565 566 verifyFormat( 567 "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 568 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n" 569 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 570 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 571 " ++aaaaaaaaaaa) {\n}"); 572 verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 573 " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n" 574 " ++i) {\n}"); 575 verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n" 576 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 577 "}"); 578 verifyFormat("for (some_namespace::SomeIterator iter( // force break\n" 579 " aaaaaaaaaa);\n" 580 " iter; ++iter) {\n" 581 "}"); 582 583 FormatStyle NoBinPacking = getLLVMStyle(); 584 NoBinPacking.BinPackParameters = false; 585 verifyFormat("for (int aaaaaaaaaaa = 1;\n" 586 " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n" 587 " aaaaaaaaaaaaaaaa,\n" 588 " aaaaaaaaaaaaaaaa,\n" 589 " aaaaaaaaaaaaaaaa);\n" 590 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 591 "}", 592 NoBinPacking); 593 verifyFormat( 594 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 595 " E = UnwrappedLines.end();\n" 596 " I != E;\n" 597 " ++I) {\n}", 598 NoBinPacking); 599 } 600 601 TEST_F(FormatTest, RangeBasedForLoops) { 602 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 603 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 604 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n" 605 " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}"); 606 verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n" 607 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 608 verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n" 609 " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}"); 610 } 611 612 TEST_F(FormatTest, ForEachLoops) { 613 verifyFormat("void f() {\n" 614 " foreach (Item *item, itemlist) {}\n" 615 " Q_FOREACH (Item *item, itemlist) {}\n" 616 " BOOST_FOREACH (Item *item, itemlist) {}\n" 617 " UNKNOWN_FORACH(Item * item, itemlist) {}\n" 618 "}"); 619 } 620 621 TEST_F(FormatTest, FormatsWhileLoop) { 622 verifyFormat("while (true) {\n}"); 623 verifyFormat("while (true)\n" 624 " f();"); 625 verifyFormat("while () {\n}"); 626 verifyFormat("while () {\n" 627 " f();\n" 628 "}"); 629 } 630 631 TEST_F(FormatTest, FormatsDoWhile) { 632 verifyFormat("do {\n" 633 " do_something();\n" 634 "} while (something());"); 635 verifyFormat("do\n" 636 " do_something();\n" 637 "while (something());"); 638 } 639 640 TEST_F(FormatTest, FormatsSwitchStatement) { 641 verifyFormat("switch (x) {\n" 642 "case 1:\n" 643 " f();\n" 644 " break;\n" 645 "case kFoo:\n" 646 "case ns::kBar:\n" 647 "case kBaz:\n" 648 " break;\n" 649 "default:\n" 650 " g();\n" 651 " break;\n" 652 "}"); 653 verifyFormat("switch (x) {\n" 654 "case 1: {\n" 655 " f();\n" 656 " break;\n" 657 "}\n" 658 "case 2: {\n" 659 " break;\n" 660 "}\n" 661 "}"); 662 verifyFormat("switch (x) {\n" 663 "case 1: {\n" 664 " f();\n" 665 " {\n" 666 " g();\n" 667 " h();\n" 668 " }\n" 669 " break;\n" 670 "}\n" 671 "}"); 672 verifyFormat("switch (x) {\n" 673 "case 1: {\n" 674 " f();\n" 675 " if (foo) {\n" 676 " g();\n" 677 " h();\n" 678 " }\n" 679 " break;\n" 680 "}\n" 681 "}"); 682 verifyFormat("switch (x) {\n" 683 "case 1: {\n" 684 " f();\n" 685 " g();\n" 686 "} break;\n" 687 "}"); 688 verifyFormat("switch (test)\n" 689 " ;"); 690 verifyFormat("switch (x) {\n" 691 "default: {\n" 692 " // Do nothing.\n" 693 "}\n" 694 "}"); 695 verifyFormat("switch (x) {\n" 696 "// comment\n" 697 "// if 1, do f()\n" 698 "case 1:\n" 699 " f();\n" 700 "}"); 701 verifyFormat("switch (x) {\n" 702 "case 1:\n" 703 " // Do amazing stuff\n" 704 " {\n" 705 " f();\n" 706 " g();\n" 707 " }\n" 708 " break;\n" 709 "}"); 710 verifyFormat("#define A \\\n" 711 " switch (x) { \\\n" 712 " case a: \\\n" 713 " foo = b; \\\n" 714 " }", 715 getLLVMStyleWithColumns(20)); 716 verifyFormat("#define OPERATION_CASE(name) \\\n" 717 " case OP_name: \\\n" 718 " return operations::Operation##name\n", 719 getLLVMStyleWithColumns(40)); 720 verifyFormat("switch (x) {\n" 721 "case 1:;\n" 722 "default:;\n" 723 " int i;\n" 724 "}"); 725 726 verifyGoogleFormat("switch (x) {\n" 727 " case 1:\n" 728 " f();\n" 729 " break;\n" 730 " case kFoo:\n" 731 " case ns::kBar:\n" 732 " case kBaz:\n" 733 " break;\n" 734 " default:\n" 735 " g();\n" 736 " break;\n" 737 "}"); 738 verifyGoogleFormat("switch (x) {\n" 739 " case 1: {\n" 740 " f();\n" 741 " break;\n" 742 " }\n" 743 "}"); 744 verifyGoogleFormat("switch (test)\n" 745 " ;"); 746 747 verifyGoogleFormat("#define OPERATION_CASE(name) \\\n" 748 " case OP_name: \\\n" 749 " return operations::Operation##name\n"); 750 verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n" 751 " // Get the correction operation class.\n" 752 " switch (OpCode) {\n" 753 " CASE(Add);\n" 754 " CASE(Subtract);\n" 755 " default:\n" 756 " return operations::Unknown;\n" 757 " }\n" 758 "#undef OPERATION_CASE\n" 759 "}"); 760 verifyFormat("DEBUG({\n" 761 " switch (x) {\n" 762 " case A:\n" 763 " f();\n" 764 " break;\n" 765 " // On B:\n" 766 " case B:\n" 767 " g();\n" 768 " break;\n" 769 " }\n" 770 "});"); 771 verifyFormat("switch (a) {\n" 772 "case (b):\n" 773 " return;\n" 774 "}"); 775 776 verifyFormat("switch (a) {\n" 777 "case some_namespace::\n" 778 " some_constant:\n" 779 " return;\n" 780 "}", 781 getLLVMStyleWithColumns(34)); 782 } 783 784 TEST_F(FormatTest, CaseRanges) { 785 verifyFormat("switch (x) {\n" 786 "case 'A' ... 'Z':\n" 787 "case 1 ... 5:\n" 788 " break;\n" 789 "}"); 790 } 791 792 TEST_F(FormatTest, ShortCaseLabels) { 793 FormatStyle Style = getLLVMStyle(); 794 Style.AllowShortCaseLabelsOnASingleLine = true; 795 verifyFormat("switch (a) {\n" 796 "case 1: x = 1; break;\n" 797 "case 2: return;\n" 798 "case 3:\n" 799 "case 4:\n" 800 "case 5: return;\n" 801 "case 6: // comment\n" 802 " return;\n" 803 "case 7:\n" 804 " // comment\n" 805 " return;\n" 806 "default: y = 1; break;\n" 807 "}", 808 Style); 809 verifyFormat("switch (a) {\n" 810 "#if FOO\n" 811 "case 0: return 0;\n" 812 "#endif\n" 813 "}", 814 Style); 815 verifyFormat("switch (a) {\n" 816 "case 1: {\n" 817 "}\n" 818 "case 2: {\n" 819 " return;\n" 820 "}\n" 821 "case 3: {\n" 822 " x = 1;\n" 823 " return;\n" 824 "}\n" 825 "case 4:\n" 826 " if (x)\n" 827 " return;\n" 828 "}", 829 Style); 830 Style.ColumnLimit = 21; 831 verifyFormat("switch (a) {\n" 832 "case 1: x = 1; break;\n" 833 "case 2: return;\n" 834 "case 3:\n" 835 "case 4:\n" 836 "case 5: return;\n" 837 "default:\n" 838 " y = 1;\n" 839 " break;\n" 840 "}", 841 Style); 842 } 843 844 TEST_F(FormatTest, FormatsLabels) { 845 verifyFormat("void f() {\n" 846 " some_code();\n" 847 "test_label:\n" 848 " some_other_code();\n" 849 " {\n" 850 " some_more_code();\n" 851 " another_label:\n" 852 " some_more_code();\n" 853 " }\n" 854 "}"); 855 verifyFormat("{\n" 856 " some_code();\n" 857 "test_label:\n" 858 " some_other_code();\n" 859 "}"); 860 verifyFormat("{\n" 861 " some_code();\n" 862 "test_label:;\n" 863 " int i = 0;\n" 864 "}"); 865 } 866 867 //===----------------------------------------------------------------------===// 868 // Tests for comments. 869 //===----------------------------------------------------------------------===// 870 871 TEST_F(FormatTest, UnderstandsSingleLineComments) { 872 verifyFormat("//* */"); 873 verifyFormat("// line 1\n" 874 "// line 2\n" 875 "void f() {}\n"); 876 877 verifyFormat("void f() {\n" 878 " // Doesn't do anything\n" 879 "}"); 880 verifyFormat("SomeObject\n" 881 " // Calling someFunction on SomeObject\n" 882 " .someFunction();"); 883 verifyFormat("auto result = SomeObject\n" 884 " // Calling someFunction on SomeObject\n" 885 " .someFunction();"); 886 verifyFormat("void f(int i, // some comment (probably for i)\n" 887 " int j, // some comment (probably for j)\n" 888 " int k); // some comment (probably for k)"); 889 verifyFormat("void f(int i,\n" 890 " // some comment (probably for j)\n" 891 " int j,\n" 892 " // some comment (probably for k)\n" 893 " int k);"); 894 895 verifyFormat("int i // This is a fancy variable\n" 896 " = 5; // with nicely aligned comment."); 897 898 verifyFormat("// Leading comment.\n" 899 "int a; // Trailing comment."); 900 verifyFormat("int a; // Trailing comment\n" 901 " // on 2\n" 902 " // or 3 lines.\n" 903 "int b;"); 904 verifyFormat("int a; // Trailing comment\n" 905 "\n" 906 "// Leading comment.\n" 907 "int b;"); 908 verifyFormat("int a; // Comment.\n" 909 " // More details.\n" 910 "int bbbb; // Another comment."); 911 verifyFormat( 912 "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n" 913 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // comment\n" 914 "int cccccccccccccccccccccccccccccc; // comment\n" 915 "int ddd; // looooooooooooooooooooooooong comment\n" 916 "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n" 917 "int bbbbbbbbbbbbbbbbbbbbb; // comment\n" 918 "int ccccccccccccccccccc; // comment"); 919 920 verifyFormat("#include \"a\" // comment\n" 921 "#include \"a/b/c\" // comment"); 922 verifyFormat("#include <a> // comment\n" 923 "#include <a/b/c> // comment"); 924 EXPECT_EQ("#include \"a\" // comment\n" 925 "#include \"a/b/c\" // comment", 926 format("#include \\\n" 927 " \"a\" // comment\n" 928 "#include \"a/b/c\" // comment")); 929 930 verifyFormat("enum E {\n" 931 " // comment\n" 932 " VAL_A, // comment\n" 933 " VAL_B\n" 934 "};"); 935 936 verifyFormat( 937 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 938 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment"); 939 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 940 " // Comment inside a statement.\n" 941 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 942 verifyFormat("SomeFunction(a,\n" 943 " // comment\n" 944 " b + x);"); 945 verifyFormat("SomeFunction(a, a,\n" 946 " // comment\n" 947 " b + x);"); 948 verifyFormat( 949 "bool aaaaaaaaaaaaa = // comment\n" 950 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 951 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 952 953 verifyFormat("int aaaa; // aaaaa\n" 954 "int aa; // aaaaaaa", 955 getLLVMStyleWithColumns(20)); 956 957 EXPECT_EQ("void f() { // This does something ..\n" 958 "}\n" 959 "int a; // This is unrelated", 960 format("void f() { // This does something ..\n" 961 " }\n" 962 "int a; // This is unrelated")); 963 EXPECT_EQ("class C {\n" 964 " void f() { // This does something ..\n" 965 " } // awesome..\n" 966 "\n" 967 " int a; // This is unrelated\n" 968 "};", 969 format("class C{void f() { // This does something ..\n" 970 " } // awesome..\n" 971 " \n" 972 "int a; // This is unrelated\n" 973 "};")); 974 975 EXPECT_EQ("int i; // single line trailing comment", 976 format("int i;\\\n// single line trailing comment")); 977 978 verifyGoogleFormat("int a; // Trailing comment."); 979 980 verifyFormat("someFunction(anotherFunction( // Force break.\n" 981 " parameter));"); 982 983 verifyGoogleFormat("#endif // HEADER_GUARD"); 984 985 verifyFormat("const char *test[] = {\n" 986 " // A\n" 987 " \"aaaa\",\n" 988 " // B\n" 989 " \"aaaaa\"};"); 990 verifyGoogleFormat( 991 "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 992 " aaaaaaaaaaaaaaaaaaaaaa); // 81_cols_with_this_comment"); 993 EXPECT_EQ("D(a, {\n" 994 " // test\n" 995 " int a;\n" 996 "});", 997 format("D(a, {\n" 998 "// test\n" 999 "int a;\n" 1000 "});")); 1001 1002 EXPECT_EQ("lineWith(); // comment\n" 1003 "// at start\n" 1004 "otherLine();", 1005 format("lineWith(); // comment\n" 1006 "// at start\n" 1007 "otherLine();")); 1008 EXPECT_EQ("lineWith(); // comment\n" 1009 " // at start\n" 1010 "otherLine();", 1011 format("lineWith(); // comment\n" 1012 " // at start\n" 1013 "otherLine();")); 1014 1015 EXPECT_EQ("lineWith(); // comment\n" 1016 "// at start\n" 1017 "otherLine(); // comment", 1018 format("lineWith(); // comment\n" 1019 "// at start\n" 1020 "otherLine(); // comment")); 1021 EXPECT_EQ("lineWith();\n" 1022 "// at start\n" 1023 "otherLine(); // comment", 1024 format("lineWith();\n" 1025 " // at start\n" 1026 "otherLine(); // comment")); 1027 EXPECT_EQ("// first\n" 1028 "// at start\n" 1029 "otherLine(); // comment", 1030 format("// first\n" 1031 " // at start\n" 1032 "otherLine(); // comment")); 1033 EXPECT_EQ("f();\n" 1034 "// first\n" 1035 "// at start\n" 1036 "otherLine(); // comment", 1037 format("f();\n" 1038 "// first\n" 1039 " // at start\n" 1040 "otherLine(); // comment")); 1041 verifyFormat("f(); // comment\n" 1042 "// first\n" 1043 "// at start\n" 1044 "otherLine();"); 1045 EXPECT_EQ("f(); // comment\n" 1046 "// first\n" 1047 "// at start\n" 1048 "otherLine();", 1049 format("f(); // comment\n" 1050 "// first\n" 1051 " // at start\n" 1052 "otherLine();")); 1053 EXPECT_EQ("f(); // comment\n" 1054 " // first\n" 1055 "// at start\n" 1056 "otherLine();", 1057 format("f(); // comment\n" 1058 " // first\n" 1059 "// at start\n" 1060 "otherLine();")); 1061 EXPECT_EQ("void f() {\n" 1062 " lineWith(); // comment\n" 1063 " // at start\n" 1064 "}", 1065 format("void f() {\n" 1066 " lineWith(); // comment\n" 1067 " // at start\n" 1068 "}")); 1069 1070 verifyFormat("#define A \\\n" 1071 " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n" 1072 " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */", 1073 getLLVMStyleWithColumns(60)); 1074 verifyFormat( 1075 "#define A \\\n" 1076 " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n" 1077 " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */", 1078 getLLVMStyleWithColumns(61)); 1079 1080 verifyFormat("if ( // This is some comment\n" 1081 " x + 3) {\n" 1082 "}"); 1083 EXPECT_EQ("if ( // This is some comment\n" 1084 " // spanning two lines\n" 1085 " x + 3) {\n" 1086 "}", 1087 format("if( // This is some comment\n" 1088 " // spanning two lines\n" 1089 " x + 3) {\n" 1090 "}")); 1091 1092 verifyNoCrash("/\\\n/"); 1093 verifyNoCrash("/\\\n* */"); 1094 // The 0-character somehow makes the lexer return a proper comment. 1095 verifyNoCrash(StringRef("/*\\\0\n/", 6)); 1096 } 1097 1098 TEST_F(FormatTest, KeepsParameterWithTrailingCommentsOnTheirOwnLine) { 1099 EXPECT_EQ("SomeFunction(a,\n" 1100 " b, // comment\n" 1101 " c);", 1102 format("SomeFunction(a,\n" 1103 " b, // comment\n" 1104 " c);")); 1105 EXPECT_EQ("SomeFunction(a, b,\n" 1106 " // comment\n" 1107 " c);", 1108 format("SomeFunction(a,\n" 1109 " b,\n" 1110 " // comment\n" 1111 " c);")); 1112 EXPECT_EQ("SomeFunction(a, b, // comment (unclear relation)\n" 1113 " c);", 1114 format("SomeFunction(a, b, // comment (unclear relation)\n" 1115 " c);")); 1116 EXPECT_EQ("SomeFunction(a, // comment\n" 1117 " b,\n" 1118 " c); // comment", 1119 format("SomeFunction(a, // comment\n" 1120 " b,\n" 1121 " c); // comment")); 1122 } 1123 1124 TEST_F(FormatTest, CanFormatCommentsLocally) { 1125 EXPECT_EQ("int a; // comment\n" 1126 "int b; // comment", 1127 format("int a; // comment\n" 1128 "int b; // comment", 1129 0, 0, getLLVMStyle())); 1130 EXPECT_EQ("int a; // comment\n" 1131 " // line 2\n" 1132 "int b;", 1133 format("int a; // comment\n" 1134 " // line 2\n" 1135 "int b;", 1136 28, 0, getLLVMStyle())); 1137 EXPECT_EQ("int aaaaaa; // comment\n" 1138 "int b;\n" 1139 "int c; // unrelated comment", 1140 format("int aaaaaa; // comment\n" 1141 "int b;\n" 1142 "int c; // unrelated comment", 1143 31, 0, getLLVMStyle())); 1144 1145 EXPECT_EQ("int a; // This\n" 1146 " // is\n" 1147 " // a", 1148 format("int a; // This\n" 1149 " // is\n" 1150 " // a", 1151 0, 0, getLLVMStyle())); 1152 EXPECT_EQ("int a; // This\n" 1153 " // is\n" 1154 " // a\n" 1155 "// This is b\n" 1156 "int b;", 1157 format("int a; // This\n" 1158 " // is\n" 1159 " // a\n" 1160 "// This is b\n" 1161 "int b;", 1162 0, 0, getLLVMStyle())); 1163 EXPECT_EQ("int a; // This\n" 1164 " // is\n" 1165 " // a\n" 1166 "\n" 1167 " // This is unrelated", 1168 format("int a; // This\n" 1169 " // is\n" 1170 " // a\n" 1171 "\n" 1172 " // This is unrelated", 1173 0, 0, getLLVMStyle())); 1174 EXPECT_EQ("int a;\n" 1175 "// This is\n" 1176 "// not formatted. ", 1177 format("int a;\n" 1178 "// This is\n" 1179 "// not formatted. ", 1180 0, 0, getLLVMStyle())); 1181 } 1182 1183 TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) { 1184 EXPECT_EQ("// comment", format("// comment ")); 1185 EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment", 1186 format("int aaaaaaa, bbbbbbb; // comment ", 1187 getLLVMStyleWithColumns(33))); 1188 EXPECT_EQ("// comment\\\n", format("// comment\\\n \t \v \f ")); 1189 EXPECT_EQ("// comment \\\n", format("// comment \\\n \t \v \f ")); 1190 } 1191 1192 TEST_F(FormatTest, UnderstandsBlockComments) { 1193 verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);"); 1194 verifyFormat("void f() { g(/*aaa=*/x, /*bbb=*/!y); }"); 1195 EXPECT_EQ("f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n" 1196 " bbbbbbbbbbbbbbbbbbbbbbbbb);", 1197 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \\\n" 1198 "/* Trailing comment for aa... */\n" 1199 " bbbbbbbbbbbbbbbbbbbbbbbbb);")); 1200 EXPECT_EQ( 1201 "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1202 " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);", 1203 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n" 1204 "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);")); 1205 EXPECT_EQ( 1206 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1207 " aaaaaaaaaaaaaaaaaa,\n" 1208 " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n" 1209 "}", 1210 format("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1211 " aaaaaaaaaaaaaaaaaa ,\n" 1212 " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n" 1213 "}")); 1214 1215 FormatStyle NoBinPacking = getLLVMStyle(); 1216 NoBinPacking.BinPackParameters = false; 1217 verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n" 1218 " /* parameter 2 */ aaaaaa,\n" 1219 " /* parameter 3 */ aaaaaa,\n" 1220 " /* parameter 4 */ aaaaaa);", 1221 NoBinPacking); 1222 1223 // Aligning block comments in macros. 1224 verifyGoogleFormat("#define A \\\n" 1225 " int i; /*a*/ \\\n" 1226 " int jjj; /*b*/"); 1227 } 1228 1229 TEST_F(FormatTest, AlignsBlockComments) { 1230 EXPECT_EQ("/*\n" 1231 " * Really multi-line\n" 1232 " * comment.\n" 1233 " */\n" 1234 "void f() {}", 1235 format(" /*\n" 1236 " * Really multi-line\n" 1237 " * comment.\n" 1238 " */\n" 1239 " void f() {}")); 1240 EXPECT_EQ("class C {\n" 1241 " /*\n" 1242 " * Another multi-line\n" 1243 " * comment.\n" 1244 " */\n" 1245 " void f() {}\n" 1246 "};", 1247 format("class C {\n" 1248 "/*\n" 1249 " * Another multi-line\n" 1250 " * comment.\n" 1251 " */\n" 1252 "void f() {}\n" 1253 "};")); 1254 EXPECT_EQ("/*\n" 1255 " 1. This is a comment with non-trivial formatting.\n" 1256 " 1.1. We have to indent/outdent all lines equally\n" 1257 " 1.1.1. to keep the formatting.\n" 1258 " */", 1259 format(" /*\n" 1260 " 1. This is a comment with non-trivial formatting.\n" 1261 " 1.1. We have to indent/outdent all lines equally\n" 1262 " 1.1.1. to keep the formatting.\n" 1263 " */")); 1264 EXPECT_EQ("/*\n" 1265 "Don't try to outdent if there's not enough indentation.\n" 1266 "*/", 1267 format(" /*\n" 1268 " Don't try to outdent if there's not enough indentation.\n" 1269 " */")); 1270 1271 EXPECT_EQ("int i; /* Comment with empty...\n" 1272 " *\n" 1273 " * line. */", 1274 format("int i; /* Comment with empty...\n" 1275 " *\n" 1276 " * line. */")); 1277 EXPECT_EQ("int foobar = 0; /* comment */\n" 1278 "int bar = 0; /* multiline\n" 1279 " comment 1 */\n" 1280 "int baz = 0; /* multiline\n" 1281 " comment 2 */\n" 1282 "int bzz = 0; /* multiline\n" 1283 " comment 3 */", 1284 format("int foobar = 0; /* comment */\n" 1285 "int bar = 0; /* multiline\n" 1286 " comment 1 */\n" 1287 "int baz = 0; /* multiline\n" 1288 " comment 2 */\n" 1289 "int bzz = 0; /* multiline\n" 1290 " comment 3 */")); 1291 EXPECT_EQ("int foobar = 0; /* comment */\n" 1292 "int bar = 0; /* multiline\n" 1293 " comment */\n" 1294 "int baz = 0; /* multiline\n" 1295 "comment */", 1296 format("int foobar = 0; /* comment */\n" 1297 "int bar = 0; /* multiline\n" 1298 "comment */\n" 1299 "int baz = 0; /* multiline\n" 1300 "comment */")); 1301 } 1302 1303 TEST_F(FormatTest, CorrectlyHandlesLengthOfBlockComments) { 1304 EXPECT_EQ("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1305 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */", 1306 format("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1307 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */")); 1308 EXPECT_EQ( 1309 "void ffffffffffff(\n" 1310 " int aaaaaaaa, int bbbbbbbb,\n" 1311 " int cccccccccccc) { /*\n" 1312 " aaaaaaaaaa\n" 1313 " aaaaaaaaaaaaa\n" 1314 " bbbbbbbbbbbbbb\n" 1315 " bbbbbbbbbb\n" 1316 " */\n" 1317 "}", 1318 format("void ffffffffffff(int aaaaaaaa, int bbbbbbbb, int cccccccccccc)\n" 1319 "{ /*\n" 1320 " aaaaaaaaaa aaaaaaaaaaaaa\n" 1321 " bbbbbbbbbbbbbb bbbbbbbbbb\n" 1322 " */\n" 1323 "}", 1324 getLLVMStyleWithColumns(40))); 1325 } 1326 1327 TEST_F(FormatTest, DontBreakNonTrailingBlockComments) { 1328 EXPECT_EQ("void ffffffffff(\n" 1329 " int aaaaa /* test */);", 1330 format("void ffffffffff(int aaaaa /* test */);", 1331 getLLVMStyleWithColumns(35))); 1332 } 1333 1334 TEST_F(FormatTest, SplitsLongCxxComments) { 1335 EXPECT_EQ("// A comment that\n" 1336 "// doesn't fit on\n" 1337 "// one line", 1338 format("// A comment that doesn't fit on one line", 1339 getLLVMStyleWithColumns(20))); 1340 EXPECT_EQ("// a b c d\n" 1341 "// e f g\n" 1342 "// h i j k", 1343 format("// a b c d e f g h i j k", getLLVMStyleWithColumns(10))); 1344 EXPECT_EQ( 1345 "// a b c d\n" 1346 "// e f g\n" 1347 "// h i j k", 1348 format("\\\n// a b c d e f g h i j k", getLLVMStyleWithColumns(10))); 1349 EXPECT_EQ("if (true) // A comment that\n" 1350 " // doesn't fit on\n" 1351 " // one line", 1352 format("if (true) // A comment that doesn't fit on one line ", 1353 getLLVMStyleWithColumns(30))); 1354 EXPECT_EQ("// Don't_touch_leading_whitespace", 1355 format("// Don't_touch_leading_whitespace", 1356 getLLVMStyleWithColumns(20))); 1357 EXPECT_EQ("// Add leading\n" 1358 "// whitespace", 1359 format("//Add leading whitespace", getLLVMStyleWithColumns(20))); 1360 EXPECT_EQ("// whitespace", format("//whitespace", getLLVMStyle())); 1361 EXPECT_EQ("// Even if it makes the line exceed the column\n" 1362 "// limit", 1363 format("//Even if it makes the line exceed the column limit", 1364 getLLVMStyleWithColumns(51))); 1365 EXPECT_EQ("//--But not here", format("//--But not here", getLLVMStyle())); 1366 1367 EXPECT_EQ("// aa bb cc dd", 1368 format("// aa bb cc dd ", 1369 getLLVMStyleWithColumns(15))); 1370 1371 EXPECT_EQ("// A comment before\n" 1372 "// a macro\n" 1373 "// definition\n" 1374 "#define a b", 1375 format("// A comment before a macro definition\n" 1376 "#define a b", 1377 getLLVMStyleWithColumns(20))); 1378 EXPECT_EQ("void ffffff(\n" 1379 " int aaaaaaaaa, // wwww\n" 1380 " int bbbbbbbbbb, // xxxxxxx\n" 1381 " // yyyyyyyyyy\n" 1382 " int c, int d, int e) {}", 1383 format("void ffffff(\n" 1384 " int aaaaaaaaa, // wwww\n" 1385 " int bbbbbbbbbb, // xxxxxxx yyyyyyyyyy\n" 1386 " int c, int d, int e) {}", 1387 getLLVMStyleWithColumns(40))); 1388 EXPECT_EQ("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 1389 format("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 1390 getLLVMStyleWithColumns(20))); 1391 EXPECT_EQ( 1392 "#define XXX // a b c d\n" 1393 " // e f g h", 1394 format("#define XXX // a b c d e f g h", getLLVMStyleWithColumns(22))); 1395 EXPECT_EQ( 1396 "#define XXX // q w e r\n" 1397 " // t y u i", 1398 format("#define XXX //q w e r t y u i", getLLVMStyleWithColumns(22))); 1399 } 1400 1401 TEST_F(FormatTest, PreservesHangingIndentInCxxComments) { 1402 EXPECT_EQ("// A comment\n" 1403 "// that doesn't\n" 1404 "// fit on one\n" 1405 "// line", 1406 format("// A comment that doesn't fit on one line", 1407 getLLVMStyleWithColumns(20))); 1408 EXPECT_EQ("/// A comment\n" 1409 "/// that doesn't\n" 1410 "/// fit on one\n" 1411 "/// line", 1412 format("/// A comment that doesn't fit on one line", 1413 getLLVMStyleWithColumns(20))); 1414 } 1415 1416 TEST_F(FormatTest, DontSplitLineCommentsWithEscapedNewlines) { 1417 EXPECT_EQ("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 1418 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 1419 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 1420 format("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 1421 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 1422 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")); 1423 EXPECT_EQ("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1424 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1425 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 1426 format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1427 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1428 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 1429 getLLVMStyleWithColumns(50))); 1430 // FIXME: One day we might want to implement adjustment of leading whitespace 1431 // of the consecutive lines in this kind of comment: 1432 EXPECT_EQ("double\n" 1433 " a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1434 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1435 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 1436 format("double a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1437 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1438 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 1439 getLLVMStyleWithColumns(49))); 1440 } 1441 1442 TEST_F(FormatTest, DontSplitLineCommentsWithPragmas) { 1443 FormatStyle Pragmas = getLLVMStyleWithColumns(30); 1444 Pragmas.CommentPragmas = "^ IWYU pragma:"; 1445 EXPECT_EQ( 1446 "// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", 1447 format("// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", Pragmas)); 1448 EXPECT_EQ( 1449 "/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", 1450 format("/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", Pragmas)); 1451 } 1452 1453 TEST_F(FormatTest, PriorityOfCommentBreaking) { 1454 EXPECT_EQ("if (xxx ==\n" 1455 " yyy && // aaaaaaaaaaaa bbbbbbbbb\n" 1456 " zzz)\n" 1457 " q();", 1458 format("if (xxx == yyy && // aaaaaaaaaaaa bbbbbbbbb\n" 1459 " zzz) q();", 1460 getLLVMStyleWithColumns(40))); 1461 EXPECT_EQ("if (xxxxxxxxxx ==\n" 1462 " yyy && // aaaaaa bbbbbbbb cccc\n" 1463 " zzz)\n" 1464 " q();", 1465 format("if (xxxxxxxxxx == yyy && // aaaaaa bbbbbbbb cccc\n" 1466 " zzz) q();", 1467 getLLVMStyleWithColumns(40))); 1468 EXPECT_EQ("if (xxxxxxxxxx &&\n" 1469 " yyy || // aaaaaa bbbbbbbb cccc\n" 1470 " zzz)\n" 1471 " q();", 1472 format("if (xxxxxxxxxx && yyy || // aaaaaa bbbbbbbb cccc\n" 1473 " zzz) q();", 1474 getLLVMStyleWithColumns(40))); 1475 EXPECT_EQ("fffffffff(\n" 1476 " &xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n" 1477 " zzz);", 1478 format("fffffffff(&xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n" 1479 " zzz);", 1480 getLLVMStyleWithColumns(40))); 1481 } 1482 1483 TEST_F(FormatTest, MultiLineCommentsInDefines) { 1484 EXPECT_EQ("#define A(x) /* \\\n" 1485 " a comment \\\n" 1486 " inside */ \\\n" 1487 " f();", 1488 format("#define A(x) /* \\\n" 1489 " a comment \\\n" 1490 " inside */ \\\n" 1491 " f();", 1492 getLLVMStyleWithColumns(17))); 1493 EXPECT_EQ("#define A( \\\n" 1494 " x) /* \\\n" 1495 " a comment \\\n" 1496 " inside */ \\\n" 1497 " f();", 1498 format("#define A( \\\n" 1499 " x) /* \\\n" 1500 " a comment \\\n" 1501 " inside */ \\\n" 1502 " f();", 1503 getLLVMStyleWithColumns(17))); 1504 } 1505 1506 TEST_F(FormatTest, ParsesCommentsAdjacentToPPDirectives) { 1507 EXPECT_EQ("namespace {}\n// Test\n#define A", 1508 format("namespace {}\n // Test\n#define A")); 1509 EXPECT_EQ("namespace {}\n/* Test */\n#define A", 1510 format("namespace {}\n /* Test */\n#define A")); 1511 EXPECT_EQ("namespace {}\n/* Test */ #define A", 1512 format("namespace {}\n /* Test */ #define A")); 1513 } 1514 1515 TEST_F(FormatTest, SplitsLongLinesInComments) { 1516 EXPECT_EQ("/* This is a long\n" 1517 " * comment that\n" 1518 " * doesn't\n" 1519 " * fit on one line.\n" 1520 " */", 1521 format("/* " 1522 "This is a long " 1523 "comment that " 1524 "doesn't " 1525 "fit on one line. */", 1526 getLLVMStyleWithColumns(20))); 1527 EXPECT_EQ( 1528 "/* a b c d\n" 1529 " * e f g\n" 1530 " * h i j k\n" 1531 " */", 1532 format("/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10))); 1533 EXPECT_EQ( 1534 "/* a b c d\n" 1535 " * e f g\n" 1536 " * h i j k\n" 1537 " */", 1538 format("\\\n/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10))); 1539 EXPECT_EQ("/*\n" 1540 "This is a long\n" 1541 "comment that doesn't\n" 1542 "fit on one line.\n" 1543 "*/", 1544 format("/*\n" 1545 "This is a long " 1546 "comment that doesn't " 1547 "fit on one line. \n" 1548 "*/", 1549 getLLVMStyleWithColumns(20))); 1550 EXPECT_EQ("/*\n" 1551 " * This is a long\n" 1552 " * comment that\n" 1553 " * doesn't fit on\n" 1554 " * one line.\n" 1555 " */", 1556 format("/* \n" 1557 " * This is a long " 1558 " comment that " 1559 " doesn't fit on " 1560 " one line. \n" 1561 " */", 1562 getLLVMStyleWithColumns(20))); 1563 EXPECT_EQ("/*\n" 1564 " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n" 1565 " * so_it_should_be_broken\n" 1566 " * wherever_a_space_occurs\n" 1567 " */", 1568 format("/*\n" 1569 " * This_is_a_comment_with_words_that_dont_fit_on_one_line " 1570 " so_it_should_be_broken " 1571 " wherever_a_space_occurs \n" 1572 " */", 1573 getLLVMStyleWithColumns(20))); 1574 EXPECT_EQ("/*\n" 1575 " * This_comment_can_not_be_broken_into_lines\n" 1576 " */", 1577 format("/*\n" 1578 " * This_comment_can_not_be_broken_into_lines\n" 1579 " */", 1580 getLLVMStyleWithColumns(20))); 1581 EXPECT_EQ("{\n" 1582 " /*\n" 1583 " This is another\n" 1584 " long comment that\n" 1585 " doesn't fit on one\n" 1586 " line 1234567890\n" 1587 " */\n" 1588 "}", 1589 format("{\n" 1590 "/*\n" 1591 "This is another " 1592 " long comment that " 1593 " doesn't fit on one" 1594 " line 1234567890\n" 1595 "*/\n" 1596 "}", 1597 getLLVMStyleWithColumns(20))); 1598 EXPECT_EQ("{\n" 1599 " /*\n" 1600 " * This i s\n" 1601 " * another comment\n" 1602 " * t hat doesn' t\n" 1603 " * fit on one l i\n" 1604 " * n e\n" 1605 " */\n" 1606 "}", 1607 format("{\n" 1608 "/*\n" 1609 " * This i s" 1610 " another comment" 1611 " t hat doesn' t" 1612 " fit on one l i" 1613 " n e\n" 1614 " */\n" 1615 "}", 1616 getLLVMStyleWithColumns(20))); 1617 EXPECT_EQ("/*\n" 1618 " * This is a long\n" 1619 " * comment that\n" 1620 " * doesn't fit on\n" 1621 " * one line\n" 1622 " */", 1623 format(" /*\n" 1624 " * This is a long comment that doesn't fit on one line\n" 1625 " */", 1626 getLLVMStyleWithColumns(20))); 1627 EXPECT_EQ("{\n" 1628 " if (something) /* This is a\n" 1629 " long\n" 1630 " comment */\n" 1631 " ;\n" 1632 "}", 1633 format("{\n" 1634 " if (something) /* This is a long comment */\n" 1635 " ;\n" 1636 "}", 1637 getLLVMStyleWithColumns(30))); 1638 1639 EXPECT_EQ("/* A comment before\n" 1640 " * a macro\n" 1641 " * definition */\n" 1642 "#define a b", 1643 format("/* A comment before a macro definition */\n" 1644 "#define a b", 1645 getLLVMStyleWithColumns(20))); 1646 1647 EXPECT_EQ("/* some comment\n" 1648 " * a comment\n" 1649 "* that we break\n" 1650 " * another comment\n" 1651 "* we have to break\n" 1652 "* a left comment\n" 1653 " */", 1654 format(" /* some comment\n" 1655 " * a comment that we break\n" 1656 " * another comment we have to break\n" 1657 "* a left comment\n" 1658 " */", 1659 getLLVMStyleWithColumns(20))); 1660 1661 EXPECT_EQ("/**\n" 1662 " * multiline block\n" 1663 " * comment\n" 1664 " *\n" 1665 " */", 1666 format("/**\n" 1667 " * multiline block comment\n" 1668 " *\n" 1669 " */", 1670 getLLVMStyleWithColumns(20))); 1671 1672 EXPECT_EQ("/*\n" 1673 "\n" 1674 "\n" 1675 " */\n", 1676 format(" /* \n" 1677 " \n" 1678 " \n" 1679 " */\n")); 1680 1681 EXPECT_EQ("/* a a */", 1682 format("/* a a */", getLLVMStyleWithColumns(15))); 1683 EXPECT_EQ("/* a a bc */", 1684 format("/* a a bc */", getLLVMStyleWithColumns(15))); 1685 EXPECT_EQ("/* aaa aaa\n" 1686 " * aaaaa */", 1687 format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15))); 1688 EXPECT_EQ("/* aaa aaa\n" 1689 " * aaaaa */", 1690 format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15))); 1691 } 1692 1693 TEST_F(FormatTest, SplitsLongLinesInCommentsInPreprocessor) { 1694 EXPECT_EQ("#define X \\\n" 1695 " /* \\\n" 1696 " Test \\\n" 1697 " Macro comment \\\n" 1698 " with a long \\\n" 1699 " line \\\n" 1700 " */ \\\n" 1701 " A + B", 1702 format("#define X \\\n" 1703 " /*\n" 1704 " Test\n" 1705 " Macro comment with a long line\n" 1706 " */ \\\n" 1707 " A + B", 1708 getLLVMStyleWithColumns(20))); 1709 EXPECT_EQ("#define X \\\n" 1710 " /* Macro comment \\\n" 1711 " with a long \\\n" 1712 " line */ \\\n" 1713 " A + B", 1714 format("#define X \\\n" 1715 " /* Macro comment with a long\n" 1716 " line */ \\\n" 1717 " A + B", 1718 getLLVMStyleWithColumns(20))); 1719 EXPECT_EQ("#define X \\\n" 1720 " /* Macro comment \\\n" 1721 " * with a long \\\n" 1722 " * line */ \\\n" 1723 " A + B", 1724 format("#define X \\\n" 1725 " /* Macro comment with a long line */ \\\n" 1726 " A + B", 1727 getLLVMStyleWithColumns(20))); 1728 } 1729 1730 TEST_F(FormatTest, CommentsInStaticInitializers) { 1731 EXPECT_EQ( 1732 "static SomeType type = {aaaaaaaaaaaaaaaaaaaa, /* comment */\n" 1733 " aaaaaaaaaaaaaaaaaaaa /* comment */,\n" 1734 " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n" 1735 " aaaaaaaaaaaaaaaaaaaa, // comment\n" 1736 " aaaaaaaaaaaaaaaaaaaa};", 1737 format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n" 1738 " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n" 1739 " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n" 1740 " aaaaaaaaaaaaaaaaaaaa , // comment\n" 1741 " aaaaaaaaaaaaaaaaaaaa };")); 1742 verifyFormat("static SomeType type = {aaaaaaaaaaa, // comment for aa...\n" 1743 " bbbbbbbbbbb, ccccccccccc};"); 1744 verifyFormat("static SomeType type = {aaaaaaaaaaa,\n" 1745 " // comment for bb....\n" 1746 " bbbbbbbbbbb, ccccccccccc};"); 1747 verifyGoogleFormat( 1748 "static SomeType type = {aaaaaaaaaaa, // comment for aa...\n" 1749 " bbbbbbbbbbb, ccccccccccc};"); 1750 verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n" 1751 " // comment for bb....\n" 1752 " bbbbbbbbbbb, ccccccccccc};"); 1753 1754 verifyFormat("S s = {{a, b, c}, // Group #1\n" 1755 " {d, e, f}, // Group #2\n" 1756 " {g, h, i}}; // Group #3"); 1757 verifyFormat("S s = {{// Group #1\n" 1758 " a, b, c},\n" 1759 " {// Group #2\n" 1760 " d, e, f},\n" 1761 " {// Group #3\n" 1762 " g, h, i}};"); 1763 1764 EXPECT_EQ("S s = {\n" 1765 " // Some comment\n" 1766 " a,\n" 1767 "\n" 1768 " // Comment after empty line\n" 1769 " b}", 1770 format("S s = {\n" 1771 " // Some comment\n" 1772 " a,\n" 1773 " \n" 1774 " // Comment after empty line\n" 1775 " b\n" 1776 "}")); 1777 EXPECT_EQ("S s = {\n" 1778 " /* Some comment */\n" 1779 " a,\n" 1780 "\n" 1781 " /* Comment after empty line */\n" 1782 " b}", 1783 format("S s = {\n" 1784 " /* Some comment */\n" 1785 " a,\n" 1786 " \n" 1787 " /* Comment after empty line */\n" 1788 " b\n" 1789 "}")); 1790 verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n" 1791 " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n" 1792 " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n" 1793 " 0x00, 0x00, 0x00, 0x00}; // comment\n"); 1794 } 1795 1796 TEST_F(FormatTest, IgnoresIf0Contents) { 1797 EXPECT_EQ("#if 0\n" 1798 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n" 1799 "#endif\n" 1800 "void f() {}", 1801 format("#if 0\n" 1802 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n" 1803 "#endif\n" 1804 "void f( ) { }")); 1805 EXPECT_EQ("#if false\n" 1806 "void f( ) { }\n" 1807 "#endif\n" 1808 "void g() {}\n", 1809 format("#if false\n" 1810 "void f( ) { }\n" 1811 "#endif\n" 1812 "void g( ) { }\n")); 1813 EXPECT_EQ("enum E {\n" 1814 " One,\n" 1815 " Two,\n" 1816 "#if 0\n" 1817 "Three,\n" 1818 " Four,\n" 1819 "#endif\n" 1820 " Five\n" 1821 "};", 1822 format("enum E {\n" 1823 " One,Two,\n" 1824 "#if 0\n" 1825 "Three,\n" 1826 " Four,\n" 1827 "#endif\n" 1828 " Five};")); 1829 EXPECT_EQ("enum F {\n" 1830 " One,\n" 1831 "#if 1\n" 1832 " Two,\n" 1833 "#if 0\n" 1834 "Three,\n" 1835 " Four,\n" 1836 "#endif\n" 1837 " Five\n" 1838 "#endif\n" 1839 "};", 1840 format("enum F {\n" 1841 "One,\n" 1842 "#if 1\n" 1843 "Two,\n" 1844 "#if 0\n" 1845 "Three,\n" 1846 " Four,\n" 1847 "#endif\n" 1848 "Five\n" 1849 "#endif\n" 1850 "};")); 1851 EXPECT_EQ("enum G {\n" 1852 " One,\n" 1853 "#if 0\n" 1854 "Two,\n" 1855 "#else\n" 1856 " Three,\n" 1857 "#endif\n" 1858 " Four\n" 1859 "};", 1860 format("enum G {\n" 1861 "One,\n" 1862 "#if 0\n" 1863 "Two,\n" 1864 "#else\n" 1865 "Three,\n" 1866 "#endif\n" 1867 "Four\n" 1868 "};")); 1869 EXPECT_EQ("enum H {\n" 1870 " One,\n" 1871 "#if 0\n" 1872 "#ifdef Q\n" 1873 "Two,\n" 1874 "#else\n" 1875 "Three,\n" 1876 "#endif\n" 1877 "#endif\n" 1878 " Four\n" 1879 "};", 1880 format("enum H {\n" 1881 "One,\n" 1882 "#if 0\n" 1883 "#ifdef Q\n" 1884 "Two,\n" 1885 "#else\n" 1886 "Three,\n" 1887 "#endif\n" 1888 "#endif\n" 1889 "Four\n" 1890 "};")); 1891 EXPECT_EQ("enum I {\n" 1892 " One,\n" 1893 "#if /* test */ 0 || 1\n" 1894 "Two,\n" 1895 "Three,\n" 1896 "#endif\n" 1897 " Four\n" 1898 "};", 1899 format("enum I {\n" 1900 "One,\n" 1901 "#if /* test */ 0 || 1\n" 1902 "Two,\n" 1903 "Three,\n" 1904 "#endif\n" 1905 "Four\n" 1906 "};")); 1907 EXPECT_EQ("enum J {\n" 1908 " One,\n" 1909 "#if 0\n" 1910 "#if 0\n" 1911 "Two,\n" 1912 "#else\n" 1913 "Three,\n" 1914 "#endif\n" 1915 "Four,\n" 1916 "#endif\n" 1917 " Five\n" 1918 "};", 1919 format("enum J {\n" 1920 "One,\n" 1921 "#if 0\n" 1922 "#if 0\n" 1923 "Two,\n" 1924 "#else\n" 1925 "Three,\n" 1926 "#endif\n" 1927 "Four,\n" 1928 "#endif\n" 1929 "Five\n" 1930 "};")); 1931 } 1932 1933 //===----------------------------------------------------------------------===// 1934 // Tests for classes, namespaces, etc. 1935 //===----------------------------------------------------------------------===// 1936 1937 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { 1938 verifyFormat("class A {};"); 1939 } 1940 1941 TEST_F(FormatTest, UnderstandsAccessSpecifiers) { 1942 verifyFormat("class A {\n" 1943 "public:\n" 1944 "public: // comment\n" 1945 "protected:\n" 1946 "private:\n" 1947 " void f() {}\n" 1948 "};"); 1949 verifyGoogleFormat("class A {\n" 1950 " public:\n" 1951 " protected:\n" 1952 " private:\n" 1953 " void f() {}\n" 1954 "};"); 1955 verifyFormat("class A {\n" 1956 "public slots:\n" 1957 " void f() {}\n" 1958 "public Q_SLOTS:\n" 1959 " void f() {}\n" 1960 "signals:\n" 1961 " void g();\n" 1962 "};"); 1963 1964 // Don't interpret 'signals' the wrong way. 1965 verifyFormat("signals.set();"); 1966 verifyFormat("for (Signals signals : f()) {\n}"); 1967 verifyFormat("{\n" 1968 " signals.set(); // This needs indentation.\n" 1969 "}"); 1970 } 1971 1972 TEST_F(FormatTest, SeparatesLogicalBlocks) { 1973 EXPECT_EQ("class A {\n" 1974 "public:\n" 1975 " void f();\n" 1976 "\n" 1977 "private:\n" 1978 " void g() {}\n" 1979 " // test\n" 1980 "protected:\n" 1981 " int h;\n" 1982 "};", 1983 format("class A {\n" 1984 "public:\n" 1985 "void f();\n" 1986 "private:\n" 1987 "void g() {}\n" 1988 "// test\n" 1989 "protected:\n" 1990 "int h;\n" 1991 "};")); 1992 EXPECT_EQ("class A {\n" 1993 "protected:\n" 1994 "public:\n" 1995 " void f();\n" 1996 "};", 1997 format("class A {\n" 1998 "protected:\n" 1999 "\n" 2000 "public:\n" 2001 "\n" 2002 " void f();\n" 2003 "};")); 2004 2005 // Even ensure proper spacing inside macros. 2006 EXPECT_EQ("#define B \\\n" 2007 " class A { \\\n" 2008 " protected: \\\n" 2009 " public: \\\n" 2010 " void f(); \\\n" 2011 " };", 2012 format("#define B \\\n" 2013 " class A { \\\n" 2014 " protected: \\\n" 2015 " \\\n" 2016 " public: \\\n" 2017 " \\\n" 2018 " void f(); \\\n" 2019 " };", 2020 getGoogleStyle())); 2021 // But don't remove empty lines after macros ending in access specifiers. 2022 EXPECT_EQ("#define A private:\n" 2023 "\n" 2024 "int i;", 2025 format("#define A private:\n" 2026 "\n" 2027 "int i;")); 2028 } 2029 2030 TEST_F(FormatTest, FormatsClasses) { 2031 verifyFormat("class A : public B {};"); 2032 verifyFormat("class A : public ::B {};"); 2033 2034 verifyFormat( 2035 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 2036 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 2037 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 2038 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 2039 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 2040 verifyFormat( 2041 "class A : public B, public C, public D, public E, public F {};"); 2042 verifyFormat("class AAAAAAAAAAAA : public B,\n" 2043 " public C,\n" 2044 " public D,\n" 2045 " public E,\n" 2046 " public F,\n" 2047 " public G {};"); 2048 2049 verifyFormat("class\n" 2050 " ReallyReallyLongClassName {\n" 2051 " int i;\n" 2052 "};", 2053 getLLVMStyleWithColumns(32)); 2054 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 2055 " aaaaaaaaaaaaaaaa> {};"); 2056 verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n" 2057 " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n" 2058 " aaaaaaaaaaaaaaaaaaaaaa> {};"); 2059 verifyFormat("template <class R, class C>\n" 2060 "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n" 2061 " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};"); 2062 verifyFormat("class ::A::B {};"); 2063 } 2064 2065 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) { 2066 verifyFormat("class A {\n} a, b;"); 2067 verifyFormat("struct A {\n} a, b;"); 2068 verifyFormat("union A {\n} a;"); 2069 } 2070 2071 TEST_F(FormatTest, FormatsEnum) { 2072 verifyFormat("enum {\n" 2073 " Zero,\n" 2074 " One = 1,\n" 2075 " Two = One + 1,\n" 2076 " Three = (One + Two),\n" 2077 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 2078 " Five = (One, Two, Three, Four, 5)\n" 2079 "};"); 2080 verifyGoogleFormat("enum {\n" 2081 " Zero,\n" 2082 " One = 1,\n" 2083 " Two = One + 1,\n" 2084 " Three = (One + Two),\n" 2085 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 2086 " Five = (One, Two, Three, Four, 5)\n" 2087 "};"); 2088 verifyFormat("enum Enum {};"); 2089 verifyFormat("enum {};"); 2090 verifyFormat("enum X E {} d;"); 2091 verifyFormat("enum __attribute__((...)) E {} d;"); 2092 verifyFormat("enum __declspec__((...)) E {} d;"); 2093 verifyFormat("enum X f() {\n a();\n return 42;\n}"); 2094 verifyFormat("enum {\n" 2095 " Bar = Foo<int, int>::value\n" 2096 "};", 2097 getLLVMStyleWithColumns(30)); 2098 2099 verifyFormat("enum ShortEnum { A, B, C };"); 2100 verifyGoogleFormat("enum ShortEnum { A, B, C };"); 2101 2102 EXPECT_EQ("enum KeepEmptyLines {\n" 2103 " ONE,\n" 2104 "\n" 2105 " TWO,\n" 2106 "\n" 2107 " THREE\n" 2108 "}", 2109 format("enum KeepEmptyLines {\n" 2110 " ONE,\n" 2111 "\n" 2112 " TWO,\n" 2113 "\n" 2114 "\n" 2115 " THREE\n" 2116 "}")); 2117 verifyFormat("enum E { // comment\n" 2118 " ONE,\n" 2119 " TWO\n" 2120 "};\n" 2121 "int i;"); 2122 } 2123 2124 TEST_F(FormatTest, FormatsEnumsWithErrors) { 2125 verifyFormat("enum Type {\n" 2126 " One = 0; // These semicolons should be commas.\n" 2127 " Two = 1;\n" 2128 "};"); 2129 verifyFormat("namespace n {\n" 2130 "enum Type {\n" 2131 " One,\n" 2132 " Two, // missing };\n" 2133 " int i;\n" 2134 "}\n" 2135 "void g() {}"); 2136 } 2137 2138 TEST_F(FormatTest, FormatsEnumStruct) { 2139 verifyFormat("enum struct {\n" 2140 " Zero,\n" 2141 " One = 1,\n" 2142 " Two = One + 1,\n" 2143 " Three = (One + Two),\n" 2144 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 2145 " Five = (One, Two, Three, Four, 5)\n" 2146 "};"); 2147 verifyFormat("enum struct Enum {};"); 2148 verifyFormat("enum struct {};"); 2149 verifyFormat("enum struct X E {} d;"); 2150 verifyFormat("enum struct __attribute__((...)) E {} d;"); 2151 verifyFormat("enum struct __declspec__((...)) E {} d;"); 2152 verifyFormat("enum struct X f() {\n a();\n return 42;\n}"); 2153 } 2154 2155 TEST_F(FormatTest, FormatsEnumClass) { 2156 verifyFormat("enum class {\n" 2157 " Zero,\n" 2158 " One = 1,\n" 2159 " Two = One + 1,\n" 2160 " Three = (One + Two),\n" 2161 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 2162 " Five = (One, Two, Three, Four, 5)\n" 2163 "};"); 2164 verifyFormat("enum class Enum {};"); 2165 verifyFormat("enum class {};"); 2166 verifyFormat("enum class X E {} d;"); 2167 verifyFormat("enum class __attribute__((...)) E {} d;"); 2168 verifyFormat("enum class __declspec__((...)) E {} d;"); 2169 verifyFormat("enum class X f() {\n a();\n return 42;\n}"); 2170 } 2171 2172 TEST_F(FormatTest, FormatsEnumTypes) { 2173 verifyFormat("enum X : int {\n" 2174 " A, // Force multiple lines.\n" 2175 " B\n" 2176 "};"); 2177 verifyFormat("enum X : int { A, B };"); 2178 verifyFormat("enum X : std::uint32_t { A, B };"); 2179 } 2180 2181 TEST_F(FormatTest, FormatsNSEnums) { 2182 verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }"); 2183 verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n" 2184 " // Information about someDecentlyLongValue.\n" 2185 " someDecentlyLongValue,\n" 2186 " // Information about anotherDecentlyLongValue.\n" 2187 " anotherDecentlyLongValue,\n" 2188 " // Information about aThirdDecentlyLongValue.\n" 2189 " aThirdDecentlyLongValue\n" 2190 "};"); 2191 verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n" 2192 " a = 1,\n" 2193 " b = 2,\n" 2194 " c = 3,\n" 2195 "};"); 2196 verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n" 2197 " a = 1,\n" 2198 " b = 2,\n" 2199 " c = 3,\n" 2200 "};"); 2201 verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n" 2202 " a = 1,\n" 2203 " b = 2,\n" 2204 " c = 3,\n" 2205 "};"); 2206 } 2207 2208 TEST_F(FormatTest, FormatsBitfields) { 2209 verifyFormat("struct Bitfields {\n" 2210 " unsigned sClass : 8;\n" 2211 " unsigned ValueKind : 2;\n" 2212 "};"); 2213 verifyFormat("struct A {\n" 2214 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n" 2215 " bbbbbbbbbbbbbbbbbbbbbbbbb;\n" 2216 "};"); 2217 verifyFormat("struct MyStruct {\n" 2218 " uchar data;\n" 2219 " uchar : 8;\n" 2220 " uchar : 8;\n" 2221 " uchar other;\n" 2222 "};"); 2223 } 2224 2225 TEST_F(FormatTest, FormatsNamespaces) { 2226 verifyFormat("namespace some_namespace {\n" 2227 "class A {};\n" 2228 "void f() { f(); }\n" 2229 "}"); 2230 verifyFormat("namespace {\n" 2231 "class A {};\n" 2232 "void f() { f(); }\n" 2233 "}"); 2234 verifyFormat("inline namespace X {\n" 2235 "class A {};\n" 2236 "void f() { f(); }\n" 2237 "}"); 2238 verifyFormat("using namespace some_namespace;\n" 2239 "class A {};\n" 2240 "void f() { f(); }"); 2241 2242 // This code is more common than we thought; if we 2243 // layout this correctly the semicolon will go into 2244 // its own line, which is undesirable. 2245 verifyFormat("namespace {};"); 2246 verifyFormat("namespace {\n" 2247 "class A {};\n" 2248 "};"); 2249 2250 verifyFormat("namespace {\n" 2251 "int SomeVariable = 0; // comment\n" 2252 "} // namespace"); 2253 EXPECT_EQ("#ifndef HEADER_GUARD\n" 2254 "#define HEADER_GUARD\n" 2255 "namespace my_namespace {\n" 2256 "int i;\n" 2257 "} // my_namespace\n" 2258 "#endif // HEADER_GUARD", 2259 format("#ifndef HEADER_GUARD\n" 2260 " #define HEADER_GUARD\n" 2261 " namespace my_namespace {\n" 2262 "int i;\n" 2263 "} // my_namespace\n" 2264 "#endif // HEADER_GUARD")); 2265 2266 FormatStyle Style = getLLVMStyle(); 2267 Style.NamespaceIndentation = FormatStyle::NI_All; 2268 EXPECT_EQ("namespace out {\n" 2269 " int i;\n" 2270 " namespace in {\n" 2271 " int i;\n" 2272 " } // namespace\n" 2273 "} // namespace", 2274 format("namespace out {\n" 2275 "int i;\n" 2276 "namespace in {\n" 2277 "int i;\n" 2278 "} // namespace\n" 2279 "} // namespace", 2280 Style)); 2281 2282 Style.NamespaceIndentation = FormatStyle::NI_Inner; 2283 EXPECT_EQ("namespace out {\n" 2284 "int i;\n" 2285 "namespace in {\n" 2286 " int i;\n" 2287 "} // namespace\n" 2288 "} // namespace", 2289 format("namespace out {\n" 2290 "int i;\n" 2291 "namespace in {\n" 2292 "int i;\n" 2293 "} // namespace\n" 2294 "} // namespace", 2295 Style)); 2296 } 2297 2298 TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); } 2299 2300 TEST_F(FormatTest, FormatsInlineASM) { 2301 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"); 2302 verifyFormat("asm(\"nop\" ::: \"memory\");"); 2303 verifyFormat( 2304 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" 2305 " \"cpuid\\n\\t\"\n" 2306 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n" 2307 " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n" 2308 " : \"a\"(value));"); 2309 EXPECT_EQ( 2310 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 2311 " __asm {\n" 2312 " mov edx,[that] // vtable in edx\n" 2313 " mov eax,methodIndex\n" 2314 " call [edx][eax*4] // stdcall\n" 2315 " }\n" 2316 "}", 2317 format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 2318 " __asm {\n" 2319 " mov edx,[that] // vtable in edx\n" 2320 " mov eax,methodIndex\n" 2321 " call [edx][eax*4] // stdcall\n" 2322 " }\n" 2323 "}")); 2324 EXPECT_EQ("_asm {\n" 2325 " xor eax, eax;\n" 2326 " cpuid;\n" 2327 "}", 2328 format("_asm {\n" 2329 " xor eax, eax;\n" 2330 " cpuid;\n" 2331 "}")); 2332 verifyFormat("void function() {\n" 2333 " // comment\n" 2334 " asm(\"\");\n" 2335 "}"); 2336 EXPECT_EQ("__asm {\n" 2337 "}\n" 2338 "int i;", 2339 format("__asm {\n" 2340 "}\n" 2341 "int i;")); 2342 } 2343 2344 TEST_F(FormatTest, FormatTryCatch) { 2345 verifyFormat("try {\n" 2346 " throw a * b;\n" 2347 "} catch (int a) {\n" 2348 " // Do nothing.\n" 2349 "} catch (...) {\n" 2350 " exit(42);\n" 2351 "}"); 2352 2353 // Function-level try statements. 2354 verifyFormat("int f() try { return 4; } catch (...) {\n" 2355 " return 5;\n" 2356 "}"); 2357 verifyFormat("class A {\n" 2358 " int a;\n" 2359 " A() try : a(0) {\n" 2360 " } catch (...) {\n" 2361 " throw;\n" 2362 " }\n" 2363 "};\n"); 2364 2365 // Incomplete try-catch blocks. 2366 verifyIncompleteFormat("try {} catch ("); 2367 } 2368 2369 TEST_F(FormatTest, FormatSEHTryCatch) { 2370 verifyFormat("__try {\n" 2371 " int a = b * c;\n" 2372 "} __except (EXCEPTION_EXECUTE_HANDLER) {\n" 2373 " // Do nothing.\n" 2374 "}"); 2375 2376 verifyFormat("__try {\n" 2377 " int a = b * c;\n" 2378 "} __finally {\n" 2379 " // Do nothing.\n" 2380 "}"); 2381 2382 verifyFormat("DEBUG({\n" 2383 " __try {\n" 2384 " } __finally {\n" 2385 " }\n" 2386 "});\n"); 2387 } 2388 2389 TEST_F(FormatTest, IncompleteTryCatchBlocks) { 2390 verifyFormat("try {\n" 2391 " f();\n" 2392 "} catch {\n" 2393 " g();\n" 2394 "}"); 2395 verifyFormat("try {\n" 2396 " f();\n" 2397 "} catch (A a) MACRO(x) {\n" 2398 " g();\n" 2399 "} catch (B b) MACRO(x) {\n" 2400 " g();\n" 2401 "}"); 2402 } 2403 2404 TEST_F(FormatTest, FormatTryCatchBraceStyles) { 2405 FormatStyle Style = getLLVMStyle(); 2406 Style.BreakBeforeBraces = FormatStyle::BS_Attach; 2407 verifyFormat("try {\n" 2408 " // something\n" 2409 "} catch (...) {\n" 2410 " // something\n" 2411 "}", 2412 Style); 2413 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 2414 verifyFormat("try {\n" 2415 " // something\n" 2416 "}\n" 2417 "catch (...) {\n" 2418 " // something\n" 2419 "}", 2420 Style); 2421 verifyFormat("__try {\n" 2422 " // something\n" 2423 "}\n" 2424 "__finally {\n" 2425 " // something\n" 2426 "}", 2427 Style); 2428 verifyFormat("@try {\n" 2429 " // something\n" 2430 "}\n" 2431 "@finally {\n" 2432 " // something\n" 2433 "}", 2434 Style); 2435 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 2436 verifyFormat("try\n" 2437 "{\n" 2438 " // something\n" 2439 "}\n" 2440 "catch (...)\n" 2441 "{\n" 2442 " // something\n" 2443 "}", 2444 Style); 2445 Style.BreakBeforeBraces = FormatStyle::BS_GNU; 2446 verifyFormat("try\n" 2447 " {\n" 2448 " // something\n" 2449 " }\n" 2450 "catch (...)\n" 2451 " {\n" 2452 " // something\n" 2453 " }", 2454 Style); 2455 } 2456 2457 TEST_F(FormatTest, FormatObjCTryCatch) { 2458 verifyFormat("@try {\n" 2459 " f();\n" 2460 "} @catch (NSException e) {\n" 2461 " @throw;\n" 2462 "} @finally {\n" 2463 " exit(42);\n" 2464 "}"); 2465 verifyFormat("DEBUG({\n" 2466 " @try {\n" 2467 " } @finally {\n" 2468 " }\n" 2469 "});\n"); 2470 } 2471 2472 TEST_F(FormatTest, StaticInitializers) { 2473 verifyFormat("static SomeClass SC = {1, 'a'};"); 2474 2475 verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n" 2476 " 100000000, " 2477 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};"); 2478 2479 // Here, everything other than the "}" would fit on a line. 2480 verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n" 2481 " 10000000000000000000000000};"); 2482 EXPECT_EQ("S s = {a,\n" 2483 "\n" 2484 " b};", 2485 format("S s = {\n" 2486 " a,\n" 2487 "\n" 2488 " b\n" 2489 "};")); 2490 2491 // FIXME: This would fit into the column limit if we'd fit "{ {" on the first 2492 // line. However, the formatting looks a bit off and this probably doesn't 2493 // happen often in practice. 2494 verifyFormat("static int Variable[1] = {\n" 2495 " {1000000000000000000000000000000000000}};", 2496 getLLVMStyleWithColumns(40)); 2497 } 2498 2499 TEST_F(FormatTest, DesignatedInitializers) { 2500 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 2501 verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n" 2502 " .bbbbbbbbbb = 2,\n" 2503 " .cccccccccc = 3,\n" 2504 " .dddddddddd = 4,\n" 2505 " .eeeeeeeeee = 5};"); 2506 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 2507 " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n" 2508 " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n" 2509 " .ccccccccccccccccccccccccccc = 3,\n" 2510 " .ddddddddddddddddddddddddddd = 4,\n" 2511 " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};"); 2512 2513 verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};"); 2514 } 2515 2516 TEST_F(FormatTest, NestedStaticInitializers) { 2517 verifyFormat("static A x = {{{}}};\n"); 2518 verifyFormat("static A x = {{{init1, init2, init3, init4},\n" 2519 " {init1, init2, init3, init4}}};", 2520 getLLVMStyleWithColumns(50)); 2521 2522 verifyFormat("somes Status::global_reps[3] = {\n" 2523 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 2524 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 2525 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};", 2526 getLLVMStyleWithColumns(60)); 2527 verifyGoogleFormat("SomeType Status::global_reps[3] = {\n" 2528 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 2529 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 2530 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};"); 2531 verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n" 2532 " {rect.fRight - rect.fLeft, rect.fBottom - " 2533 "rect.fTop}};"); 2534 2535 verifyFormat( 2536 "SomeArrayOfSomeType a = {\n" 2537 " {{1, 2, 3},\n" 2538 " {1, 2, 3},\n" 2539 " {111111111111111111111111111111, 222222222222222222222222222222,\n" 2540 " 333333333333333333333333333333},\n" 2541 " {1, 2, 3},\n" 2542 " {1, 2, 3}}};"); 2543 verifyFormat( 2544 "SomeArrayOfSomeType a = {\n" 2545 " {{1, 2, 3}},\n" 2546 " {{1, 2, 3}},\n" 2547 " {{111111111111111111111111111111, 222222222222222222222222222222,\n" 2548 " 333333333333333333333333333333}},\n" 2549 " {{1, 2, 3}},\n" 2550 " {{1, 2, 3}}};"); 2551 2552 verifyFormat("struct {\n" 2553 " unsigned bit;\n" 2554 " const char *const name;\n" 2555 "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n" 2556 " {kOsWin, \"Windows\"},\n" 2557 " {kOsLinux, \"Linux\"},\n" 2558 " {kOsCrOS, \"Chrome OS\"}};"); 2559 verifyFormat("struct {\n" 2560 " unsigned bit;\n" 2561 " const char *const name;\n" 2562 "} kBitsToOs[] = {\n" 2563 " {kOsMac, \"Mac\"},\n" 2564 " {kOsWin, \"Windows\"},\n" 2565 " {kOsLinux, \"Linux\"},\n" 2566 " {kOsCrOS, \"Chrome OS\"},\n" 2567 "};"); 2568 } 2569 2570 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { 2571 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 2572 " \\\n" 2573 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); 2574 } 2575 2576 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { 2577 verifyFormat("virtual void write(ELFWriter *writerrr,\n" 2578 " OwningPtr<FileOutputBuffer> &buffer) = 0;"); 2579 2580 // Do break defaulted and deleted functions. 2581 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 2582 " default;", 2583 getLLVMStyleWithColumns(40)); 2584 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 2585 " delete;", 2586 getLLVMStyleWithColumns(40)); 2587 } 2588 2589 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) { 2590 verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3", 2591 getLLVMStyleWithColumns(40)); 2592 verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 2593 getLLVMStyleWithColumns(40)); 2594 EXPECT_EQ("#define Q \\\n" 2595 " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n" 2596 " \"aaaaaaaa.cpp\"", 2597 format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 2598 getLLVMStyleWithColumns(40))); 2599 } 2600 2601 TEST_F(FormatTest, UnderstandsLinePPDirective) { 2602 EXPECT_EQ("# 123 \"A string literal\"", 2603 format(" # 123 \"A string literal\"")); 2604 } 2605 2606 TEST_F(FormatTest, LayoutUnknownPPDirective) { 2607 EXPECT_EQ("#;", format("#;")); 2608 verifyFormat("#\n;\n;\n;"); 2609 } 2610 2611 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { 2612 EXPECT_EQ("#line 42 \"test\"\n", 2613 format("# \\\n line \\\n 42 \\\n \"test\"\n")); 2614 EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n", 2615 getLLVMStyleWithColumns(12))); 2616 } 2617 2618 TEST_F(FormatTest, EndOfFileEndsPPDirective) { 2619 EXPECT_EQ("#line 42 \"test\"", 2620 format("# \\\n line \\\n 42 \\\n \"test\"")); 2621 EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B")); 2622 } 2623 2624 TEST_F(FormatTest, DoesntRemoveUnknownTokens) { 2625 verifyFormat("#define A \\x20"); 2626 verifyFormat("#define A \\ x20"); 2627 EXPECT_EQ("#define A \\ x20", format("#define A \\ x20")); 2628 verifyFormat("#define A ''"); 2629 verifyFormat("#define A ''qqq"); 2630 verifyFormat("#define A `qqq"); 2631 verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");"); 2632 EXPECT_EQ("const char *c = STRINGIFY(\n" 2633 "\\na : b);", 2634 format("const char * c = STRINGIFY(\n" 2635 "\\na : b);")); 2636 2637 verifyFormat("a\r\\"); 2638 verifyFormat("a\v\\"); 2639 verifyFormat("a\f\\"); 2640 } 2641 2642 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { 2643 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13)); 2644 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12)); 2645 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); 2646 // FIXME: We never break before the macro name. 2647 verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12)); 2648 2649 verifyFormat("#define A A\n#define A A"); 2650 verifyFormat("#define A(X) A\n#define A A"); 2651 2652 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23)); 2653 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22)); 2654 } 2655 2656 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { 2657 EXPECT_EQ("// somecomment\n" 2658 "#include \"a.h\"\n" 2659 "#define A( \\\n" 2660 " A, B)\n" 2661 "#include \"b.h\"\n" 2662 "// somecomment\n", 2663 format(" // somecomment\n" 2664 " #include \"a.h\"\n" 2665 "#define A(A,\\\n" 2666 " B)\n" 2667 " #include \"b.h\"\n" 2668 " // somecomment\n", 2669 getLLVMStyleWithColumns(13))); 2670 } 2671 2672 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); } 2673 2674 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { 2675 EXPECT_EQ("#define A \\\n" 2676 " c; \\\n" 2677 " e;\n" 2678 "f;", 2679 format("#define A c; e;\n" 2680 "f;", 2681 getLLVMStyleWithColumns(14))); 2682 } 2683 2684 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); } 2685 2686 TEST_F(FormatTest, AlwaysFormatsEntireMacroDefinitions) { 2687 EXPECT_EQ("int i;\n" 2688 "#define A \\\n" 2689 " int i; \\\n" 2690 " int j\n" 2691 "int k;", 2692 format("int i;\n" 2693 "#define A \\\n" 2694 " int i ; \\\n" 2695 " int j\n" 2696 "int k;", 2697 8, 0, getGoogleStyle())); // 8: position of "#define". 2698 EXPECT_EQ("int i;\n" 2699 "#define A \\\n" 2700 " int i; \\\n" 2701 " int j\n" 2702 "int k;", 2703 format("int i;\n" 2704 "#define A \\\n" 2705 " int i ; \\\n" 2706 " int j\n" 2707 "int k;", 2708 45, 0, getGoogleStyle())); // 45: position of "j". 2709 } 2710 2711 TEST_F(FormatTest, MacroDefinitionInsideStatement) { 2712 EXPECT_EQ("int x,\n" 2713 "#define A\n" 2714 " y;", 2715 format("int x,\n#define A\ny;")); 2716 } 2717 2718 TEST_F(FormatTest, HashInMacroDefinition) { 2719 EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle())); 2720 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); 2721 verifyFormat("#define A \\\n" 2722 " { \\\n" 2723 " f(#c); \\\n" 2724 " }", 2725 getLLVMStyleWithColumns(11)); 2726 2727 verifyFormat("#define A(X) \\\n" 2728 " void function##X()", 2729 getLLVMStyleWithColumns(22)); 2730 2731 verifyFormat("#define A(a, b, c) \\\n" 2732 " void a##b##c()", 2733 getLLVMStyleWithColumns(22)); 2734 2735 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22)); 2736 } 2737 2738 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { 2739 EXPECT_EQ("#define A (x)", format("#define A (x)")); 2740 EXPECT_EQ("#define A(x)", format("#define A(x)")); 2741 } 2742 2743 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) { 2744 EXPECT_EQ("#define A b;", format("#define A \\\n" 2745 " \\\n" 2746 " b;", 2747 getLLVMStyleWithColumns(25))); 2748 EXPECT_EQ("#define A \\\n" 2749 " \\\n" 2750 " a; \\\n" 2751 " b;", 2752 format("#define A \\\n" 2753 " \\\n" 2754 " a; \\\n" 2755 " b;", 2756 getLLVMStyleWithColumns(11))); 2757 EXPECT_EQ("#define A \\\n" 2758 " a; \\\n" 2759 " \\\n" 2760 " b;", 2761 format("#define A \\\n" 2762 " a; \\\n" 2763 " \\\n" 2764 " b;", 2765 getLLVMStyleWithColumns(11))); 2766 } 2767 2768 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { 2769 verifyIncompleteFormat("#define A :"); 2770 verifyFormat("#define SOMECASES \\\n" 2771 " case 1: \\\n" 2772 " case 2\n", 2773 getLLVMStyleWithColumns(20)); 2774 verifyFormat("#define A template <typename T>"); 2775 verifyIncompleteFormat("#define STR(x) #x\n" 2776 "f(STR(this_is_a_string_literal{));"); 2777 verifyFormat("#pragma omp threadprivate( \\\n" 2778 " y)), // expected-warning", 2779 getLLVMStyleWithColumns(28)); 2780 verifyFormat("#d, = };"); 2781 verifyFormat("#if \"a"); 2782 verifyIncompleteFormat("({\n" 2783 "#define b \\\n" 2784 " } \\\n" 2785 " a\n" 2786 "a", getLLVMStyleWithColumns(15)); 2787 verifyFormat("#define A \\\n" 2788 " { \\\n" 2789 " {\n" 2790 "#define B \\\n" 2791 " } \\\n" 2792 " }", 2793 getLLVMStyleWithColumns(15)); 2794 verifyNoCrash("#if a\na(\n#else\n#endif\n{a"); 2795 verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}"); 2796 verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};"); 2797 verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}"); 2798 } 2799 2800 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) { 2801 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline. 2802 EXPECT_EQ("class A : public QObject {\n" 2803 " Q_OBJECT\n" 2804 "\n" 2805 " A() {}\n" 2806 "};", 2807 format("class A : public QObject {\n" 2808 " Q_OBJECT\n" 2809 "\n" 2810 " A() {\n}\n" 2811 "} ;")); 2812 EXPECT_EQ("MACRO\n" 2813 "/*static*/ int i;", 2814 format("MACRO\n" 2815 " /*static*/ int i;")); 2816 EXPECT_EQ("SOME_MACRO\n" 2817 "namespace {\n" 2818 "void f();\n" 2819 "}", 2820 format("SOME_MACRO\n" 2821 " namespace {\n" 2822 "void f( );\n" 2823 "}")); 2824 // Only if the identifier contains at least 5 characters. 2825 EXPECT_EQ("HTTP f();", format("HTTP\nf();")); 2826 EXPECT_EQ("MACRO\nf();", format("MACRO\nf();")); 2827 // Only if everything is upper case. 2828 EXPECT_EQ("class A : public QObject {\n" 2829 " Q_Object A() {}\n" 2830 "};", 2831 format("class A : public QObject {\n" 2832 " Q_Object\n" 2833 " A() {\n}\n" 2834 "} ;")); 2835 2836 // Only if the next line can actually start an unwrapped line. 2837 EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;", 2838 format("SOME_WEIRD_LOG_MACRO\n" 2839 "<< SomeThing;")); 2840 2841 verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), " 2842 "(n, buffers))\n", 2843 getChromiumStyle(FormatStyle::LK_Cpp)); 2844 } 2845 2846 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) { 2847 EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 2848 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 2849 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 2850 "class X {};\n" 2851 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 2852 "int *createScopDetectionPass() { return 0; }", 2853 format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 2854 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 2855 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 2856 " class X {};\n" 2857 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 2858 " int *createScopDetectionPass() { return 0; }")); 2859 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as 2860 // braces, so that inner block is indented one level more. 2861 EXPECT_EQ("int q() {\n" 2862 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 2863 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 2864 " IPC_END_MESSAGE_MAP()\n" 2865 "}", 2866 format("int q() {\n" 2867 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 2868 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 2869 " IPC_END_MESSAGE_MAP()\n" 2870 "}")); 2871 2872 // Same inside macros. 2873 EXPECT_EQ("#define LIST(L) \\\n" 2874 " L(A) \\\n" 2875 " L(B) \\\n" 2876 " L(C)", 2877 format("#define LIST(L) \\\n" 2878 " L(A) \\\n" 2879 " L(B) \\\n" 2880 " L(C)", 2881 getGoogleStyle())); 2882 2883 // These must not be recognized as macros. 2884 EXPECT_EQ("int q() {\n" 2885 " f(x);\n" 2886 " f(x) {}\n" 2887 " f(x)->g();\n" 2888 " f(x)->*g();\n" 2889 " f(x).g();\n" 2890 " f(x) = x;\n" 2891 " f(x) += x;\n" 2892 " f(x) -= x;\n" 2893 " f(x) *= x;\n" 2894 " f(x) /= x;\n" 2895 " f(x) %= x;\n" 2896 " f(x) &= x;\n" 2897 " f(x) |= x;\n" 2898 " f(x) ^= x;\n" 2899 " f(x) >>= x;\n" 2900 " f(x) <<= x;\n" 2901 " f(x)[y].z();\n" 2902 " LOG(INFO) << x;\n" 2903 " ifstream(x) >> x;\n" 2904 "}\n", 2905 format("int q() {\n" 2906 " f(x)\n;\n" 2907 " f(x)\n {}\n" 2908 " f(x)\n->g();\n" 2909 " f(x)\n->*g();\n" 2910 " f(x)\n.g();\n" 2911 " f(x)\n = x;\n" 2912 " f(x)\n += x;\n" 2913 " f(x)\n -= x;\n" 2914 " f(x)\n *= x;\n" 2915 " f(x)\n /= x;\n" 2916 " f(x)\n %= x;\n" 2917 " f(x)\n &= x;\n" 2918 " f(x)\n |= x;\n" 2919 " f(x)\n ^= x;\n" 2920 " f(x)\n >>= x;\n" 2921 " f(x)\n <<= x;\n" 2922 " f(x)\n[y].z();\n" 2923 " LOG(INFO)\n << x;\n" 2924 " ifstream(x)\n >> x;\n" 2925 "}\n")); 2926 EXPECT_EQ("int q() {\n" 2927 " F(x)\n" 2928 " if (1) {\n" 2929 " }\n" 2930 " F(x)\n" 2931 " while (1) {\n" 2932 " }\n" 2933 " F(x)\n" 2934 " G(x);\n" 2935 " F(x)\n" 2936 " try {\n" 2937 " Q();\n" 2938 " } catch (...) {\n" 2939 " }\n" 2940 "}\n", 2941 format("int q() {\n" 2942 "F(x)\n" 2943 "if (1) {}\n" 2944 "F(x)\n" 2945 "while (1) {}\n" 2946 "F(x)\n" 2947 "G(x);\n" 2948 "F(x)\n" 2949 "try { Q(); } catch (...) {}\n" 2950 "}\n")); 2951 EXPECT_EQ("class A {\n" 2952 " A() : t(0) {}\n" 2953 " A(int i) noexcept() : {}\n" 2954 " A(X x)\n" // FIXME: function-level try blocks are broken. 2955 " try : t(0) {\n" 2956 " } catch (...) {\n" 2957 " }\n" 2958 "};", 2959 format("class A {\n" 2960 " A()\n : t(0) {}\n" 2961 " A(int i)\n noexcept() : {}\n" 2962 " A(X x)\n" 2963 " try : t(0) {} catch (...) {}\n" 2964 "};")); 2965 EXPECT_EQ("class SomeClass {\n" 2966 "public:\n" 2967 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2968 "};", 2969 format("class SomeClass {\n" 2970 "public:\n" 2971 " SomeClass()\n" 2972 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2973 "};")); 2974 EXPECT_EQ("class SomeClass {\n" 2975 "public:\n" 2976 " SomeClass()\n" 2977 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2978 "};", 2979 format("class SomeClass {\n" 2980 "public:\n" 2981 " SomeClass()\n" 2982 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2983 "};", 2984 getLLVMStyleWithColumns(40))); 2985 } 2986 2987 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) { 2988 verifyFormat("#define A \\\n" 2989 " f({ \\\n" 2990 " g(); \\\n" 2991 " });", 2992 getLLVMStyleWithColumns(11)); 2993 } 2994 2995 TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) { 2996 EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}")); 2997 } 2998 2999 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { 3000 verifyFormat("{\n { a #c; }\n}"); 3001 } 3002 3003 TEST_F(FormatTest, FormatUnbalancedStructuralElements) { 3004 EXPECT_EQ("#define A \\\n { \\\n {\nint i;", 3005 format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); 3006 EXPECT_EQ("#define A \\\n } \\\n }\nint i;", 3007 format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); 3008 } 3009 3010 TEST_F(FormatTest, EscapedNewlines) { 3011 EXPECT_EQ( 3012 "#define A \\\n int i; \\\n int j;", 3013 format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11))); 3014 EXPECT_EQ( 3015 "#define A\n\nint i;", format("#define A \\\n\n int i;")); 3016 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 3017 EXPECT_EQ("/* \\ \\ \\\n*/", format("\\\n/* \\ \\ \\\n*/")); 3018 EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>")); 3019 } 3020 3021 TEST_F(FormatTest, DontCrashOnBlockComments) { 3022 EXPECT_EQ( 3023 "int xxxxxxxxx; /* " 3024 "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\n" 3025 "zzzzzz\n" 3026 "0*/", 3027 format("int xxxxxxxxx; /* " 3028 "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy zzzzzz\n" 3029 "0*/")); 3030 } 3031 3032 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { 3033 verifyFormat("#define A \\\n" 3034 " int v( \\\n" 3035 " a); \\\n" 3036 " int i;", 3037 getLLVMStyleWithColumns(11)); 3038 } 3039 3040 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { 3041 EXPECT_EQ( 3042 "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 3043 " \\\n" 3044 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 3045 "\n" 3046 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 3047 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", 3048 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" 3049 "\\\n" 3050 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 3051 " \n" 3052 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 3053 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); 3054 } 3055 3056 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { 3057 EXPECT_EQ("int\n" 3058 "#define A\n" 3059 " a;", 3060 format("int\n#define A\na;")); 3061 verifyFormat("functionCallTo(\n" 3062 " someOtherFunction(\n" 3063 " withSomeParameters, whichInSequence,\n" 3064 " areLongerThanALine(andAnotherCall,\n" 3065 "#define A B\n" 3066 " withMoreParamters,\n" 3067 " whichStronglyInfluenceTheLayout),\n" 3068 " andMoreParameters),\n" 3069 " trailing);", 3070 getLLVMStyleWithColumns(69)); 3071 verifyFormat("Foo::Foo()\n" 3072 "#ifdef BAR\n" 3073 " : baz(0)\n" 3074 "#endif\n" 3075 "{\n" 3076 "}"); 3077 verifyFormat("void f() {\n" 3078 " if (true)\n" 3079 "#ifdef A\n" 3080 " f(42);\n" 3081 " x();\n" 3082 "#else\n" 3083 " g();\n" 3084 " x();\n" 3085 "#endif\n" 3086 "}"); 3087 verifyFormat("void f(param1, param2,\n" 3088 " param3,\n" 3089 "#ifdef A\n" 3090 " param4(param5,\n" 3091 "#ifdef A1\n" 3092 " param6,\n" 3093 "#ifdef A2\n" 3094 " param7),\n" 3095 "#else\n" 3096 " param8),\n" 3097 " param9,\n" 3098 "#endif\n" 3099 " param10,\n" 3100 "#endif\n" 3101 " param11)\n" 3102 "#else\n" 3103 " param12)\n" 3104 "#endif\n" 3105 "{\n" 3106 " x();\n" 3107 "}", 3108 getLLVMStyleWithColumns(28)); 3109 verifyFormat("#if 1\n" 3110 "int i;"); 3111 verifyFormat("#if 1\n" 3112 "#endif\n" 3113 "#if 1\n" 3114 "#else\n" 3115 "#endif\n"); 3116 verifyFormat("DEBUG({\n" 3117 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3118 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 3119 "});\n" 3120 "#if a\n" 3121 "#else\n" 3122 "#endif"); 3123 3124 verifyIncompleteFormat("void f(\n" 3125 "#if A\n" 3126 " );\n" 3127 "#else\n" 3128 "#endif"); 3129 } 3130 3131 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) { 3132 verifyFormat("#endif\n" 3133 "#if B"); 3134 } 3135 3136 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) { 3137 FormatStyle SingleLine = getLLVMStyle(); 3138 SingleLine.AllowShortIfStatementsOnASingleLine = true; 3139 verifyFormat("#if 0\n" 3140 "#elif 1\n" 3141 "#endif\n" 3142 "void foo() {\n" 3143 " if (test) foo2();\n" 3144 "}", 3145 SingleLine); 3146 } 3147 3148 TEST_F(FormatTest, LayoutBlockInsideParens) { 3149 verifyFormat("functionCall({ int i; });"); 3150 verifyFormat("functionCall({\n" 3151 " int i;\n" 3152 " int j;\n" 3153 "});"); 3154 verifyFormat("functionCall({\n" 3155 " int i;\n" 3156 " int j;\n" 3157 "}, aaaa, bbbb, cccc);"); 3158 verifyFormat("functionA(functionB({\n" 3159 " int i;\n" 3160 " int j;\n" 3161 " }),\n" 3162 " aaaa, bbbb, cccc);"); 3163 verifyFormat("functionCall(\n" 3164 " {\n" 3165 " int i;\n" 3166 " int j;\n" 3167 " },\n" 3168 " aaaa, bbbb, // comment\n" 3169 " cccc);"); 3170 verifyFormat("functionA(functionB({\n" 3171 " int i;\n" 3172 " int j;\n" 3173 " }),\n" 3174 " aaaa, bbbb, // comment\n" 3175 " cccc);"); 3176 verifyFormat("functionCall(aaaa, bbbb, { int i; });"); 3177 verifyFormat("functionCall(aaaa, bbbb, {\n" 3178 " int i;\n" 3179 " int j;\n" 3180 "});"); 3181 verifyFormat( 3182 "Aaa(\n" // FIXME: There shouldn't be a linebreak here. 3183 " {\n" 3184 " int i; // break\n" 3185 " },\n" 3186 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 3187 " ccccccccccccccccc));"); 3188 verifyFormat("DEBUG({\n" 3189 " if (a)\n" 3190 " f();\n" 3191 "});"); 3192 EXPECT_EQ("int longlongname; // comment\n" 3193 "int x = f({\n" 3194 " int x; // comment\n" 3195 " int y; // comment\n" 3196 "});", 3197 format("int longlongname; // comment\n" 3198 "int x = f({\n" 3199 " int x; // comment\n" 3200 " int y; // comment\n" 3201 "});", 3202 65, 0, getLLVMStyle())); 3203 EXPECT_EQ("int s = f({\n" 3204 " class X {\n" 3205 " public:\n" 3206 " void f();\n" 3207 " };\n" 3208 "});", 3209 format("int s = f({\n" 3210 " class X {\n" 3211 " public:\n" 3212 " void f();\n" 3213 " };\n" 3214 "});", 3215 0, 0, getLLVMStyle())); 3216 } 3217 3218 TEST_F(FormatTest, LayoutBlockInsideStatement) { 3219 EXPECT_EQ("SOME_MACRO { int i; }\n" 3220 "int i;", 3221 format(" SOME_MACRO {int i;} int i;")); 3222 } 3223 3224 TEST_F(FormatTest, LayoutNestedBlocks) { 3225 verifyFormat("void AddOsStrings(unsigned bitmask) {\n" 3226 " struct s {\n" 3227 " int i;\n" 3228 " };\n" 3229 " s kBitsToOs[] = {{10}};\n" 3230 " for (int i = 0; i < 10; ++i)\n" 3231 " return;\n" 3232 "}"); 3233 verifyFormat("call(parameter, {\n" 3234 " something();\n" 3235 " // Comment using all columns.\n" 3236 " somethingelse();\n" 3237 "});", 3238 getLLVMStyleWithColumns(40)); 3239 verifyFormat("DEBUG( //\n" 3240 " { f(); }, a);"); 3241 verifyFormat("DEBUG( //\n" 3242 " {\n" 3243 " f(); //\n" 3244 " },\n" 3245 " a);"); 3246 3247 EXPECT_EQ("call(parameter, {\n" 3248 " something();\n" 3249 " // Comment too\n" 3250 " // looooooooooong.\n" 3251 " somethingElse();\n" 3252 "});", 3253 format("call(parameter, {\n" 3254 " something();\n" 3255 " // Comment too looooooooooong.\n" 3256 " somethingElse();\n" 3257 "});", 3258 getLLVMStyleWithColumns(29))); 3259 EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });")); 3260 EXPECT_EQ("DEBUG({ // comment\n" 3261 " int i;\n" 3262 "});", 3263 format("DEBUG({ // comment\n" 3264 "int i;\n" 3265 "});")); 3266 EXPECT_EQ("DEBUG({\n" 3267 " int i;\n" 3268 "\n" 3269 " // comment\n" 3270 " int j;\n" 3271 "});", 3272 format("DEBUG({\n" 3273 " int i;\n" 3274 "\n" 3275 " // comment\n" 3276 " int j;\n" 3277 "});")); 3278 3279 verifyFormat("DEBUG({\n" 3280 " if (a)\n" 3281 " return;\n" 3282 "});"); 3283 verifyGoogleFormat("DEBUG({\n" 3284 " if (a) return;\n" 3285 "});"); 3286 FormatStyle Style = getGoogleStyle(); 3287 Style.ColumnLimit = 45; 3288 verifyFormat("Debug(aaaaa, {\n" 3289 " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n" 3290 "}, a);", 3291 Style); 3292 3293 verifyNoCrash("^{v^{a}}"); 3294 } 3295 3296 TEST_F(FormatTest, FormatNestedBlocksInMacros) { 3297 EXPECT_EQ("#define MACRO() \\\n" 3298 " Debug(aaa, /* force line break */ \\\n" 3299 " { \\\n" 3300 " int i; \\\n" 3301 " int j; \\\n" 3302 " })", 3303 format("#define MACRO() Debug(aaa, /* force line break */ \\\n" 3304 " { int i; int j; })", 3305 getGoogleStyle())); 3306 } 3307 3308 TEST_F(FormatTest, IndividualStatementsOfNestedBlocks) { 3309 EXPECT_EQ("DEBUG({\n" 3310 " int i;\n" 3311 " int j;\n" 3312 "});", 3313 format("DEBUG( {\n" 3314 " int i;\n" 3315 " int j;\n" 3316 "} ) ;", 3317 20, 1, getLLVMStyle())); 3318 EXPECT_EQ("DEBUG( {\n" 3319 " int i;\n" 3320 " int j;\n" 3321 "} ) ;", 3322 format("DEBUG( {\n" 3323 " int i;\n" 3324 " int j;\n" 3325 "} ) ;", 3326 41, 1, getLLVMStyle())); 3327 EXPECT_EQ("DEBUG( {\n" 3328 " int i;\n" 3329 " int j;\n" 3330 "} ) ;", 3331 format("DEBUG( {\n" 3332 " int i;\n" 3333 " int j;\n" 3334 "} ) ;", 3335 41, 1, getLLVMStyle())); 3336 EXPECT_EQ("DEBUG({\n" 3337 " int i;\n" 3338 " int j;\n" 3339 "});", 3340 format("DEBUG( {\n" 3341 " int i;\n" 3342 " int j;\n" 3343 "} ) ;", 3344 20, 1, getLLVMStyle())); 3345 3346 EXPECT_EQ("Debug({\n" 3347 " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n" 3348 " return;\n" 3349 " },\n" 3350 " a);", 3351 format("Debug({\n" 3352 " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n" 3353 " return;\n" 3354 " },\n" 3355 " a);", 3356 50, 1, getLLVMStyle())); 3357 EXPECT_EQ("DEBUG({\n" 3358 " DEBUG({\n" 3359 " int a;\n" 3360 " int b;\n" 3361 " }) ;\n" 3362 "});", 3363 format("DEBUG({\n" 3364 " DEBUG({\n" 3365 " int a;\n" 3366 " int b;\n" // Format this line only. 3367 " }) ;\n" // Don't touch this line. 3368 "});", 3369 35, 0, getLLVMStyle())); 3370 EXPECT_EQ("DEBUG({\n" 3371 " int a; //\n" 3372 "});", 3373 format("DEBUG({\n" 3374 " int a; //\n" 3375 "});", 3376 0, 0, getLLVMStyle())); 3377 EXPECT_EQ("someFunction(\n" 3378 " [] {\n" 3379 " // Only with this comment.\n" 3380 " int i; // invoke formatting here.\n" 3381 " }, // force line break\n" 3382 " aaa);", 3383 format("someFunction(\n" 3384 " [] {\n" 3385 " // Only with this comment.\n" 3386 " int i; // invoke formatting here.\n" 3387 " }, // force line break\n" 3388 " aaa);", 3389 63, 1, getLLVMStyle())); 3390 } 3391 3392 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { 3393 EXPECT_EQ("{}", format("{}")); 3394 verifyFormat("enum E {};"); 3395 verifyFormat("enum E {}"); 3396 } 3397 3398 //===----------------------------------------------------------------------===// 3399 // Line break tests. 3400 //===----------------------------------------------------------------------===// 3401 3402 TEST_F(FormatTest, PreventConfusingIndents) { 3403 verifyFormat( 3404 "void f() {\n" 3405 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" 3406 " parameter, parameter, parameter)),\n" 3407 " SecondLongCall(parameter));\n" 3408 "}"); 3409 verifyFormat( 3410 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3411 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 3412 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3413 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 3414 verifyFormat( 3415 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3416 " [aaaaaaaaaaaaaaaaaaaaaaaa\n" 3417 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 3418 " [aaaaaaaaaaaaaaaaaaaaaaaa]];"); 3419 verifyFormat( 3420 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 3421 " aaaaaaaaaaaaaaaaaaaaaaaa<\n" 3422 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n" 3423 " aaaaaaaaaaaaaaaaaaaaaaaa>;"); 3424 verifyFormat("int a = bbbb && ccc && fffff(\n" 3425 "#define A Just forcing a new line\n" 3426 " ddd);"); 3427 } 3428 3429 TEST_F(FormatTest, LineBreakingInBinaryExpressions) { 3430 verifyFormat( 3431 "bool aaaaaaa =\n" 3432 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n" 3433 " bbbbbbbb();"); 3434 verifyFormat( 3435 "bool aaaaaaa =\n" 3436 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n" 3437 " bbbbbbbb();"); 3438 3439 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 3440 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n" 3441 " ccccccccc == ddddddddddd;"); 3442 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 3443 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n" 3444 " ccccccccc == ddddddddddd;"); 3445 verifyFormat( 3446 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 3447 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n" 3448 " ccccccccc == ddddddddddd;"); 3449 3450 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 3451 " aaaaaa) &&\n" 3452 " bbbbbb && cccccc;"); 3453 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 3454 " aaaaaa) >>\n" 3455 " bbbbbb;"); 3456 verifyFormat("Whitespaces.addUntouchableComment(\n" 3457 " SourceMgr.getSpellingColumnNumber(\n" 3458 " TheLine.Last->FormatTok.Tok.getLocation()) -\n" 3459 " 1);"); 3460 3461 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3462 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n" 3463 " cccccc) {\n}"); 3464 verifyFormat("b = a &&\n" 3465 " // Comment\n" 3466 " b.c && d;"); 3467 3468 // If the LHS of a comparison is not a binary expression itself, the 3469 // additional linebreak confuses many people. 3470 verifyFormat( 3471 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3472 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n" 3473 "}"); 3474 verifyFormat( 3475 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3476 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3477 "}"); 3478 verifyFormat( 3479 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n" 3480 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3481 "}"); 3482 // Even explicit parentheses stress the precedence enough to make the 3483 // additional break unnecessary. 3484 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3485 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3486 "}"); 3487 // This cases is borderline, but with the indentation it is still readable. 3488 verifyFormat( 3489 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3490 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3491 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 3492 "}", 3493 getLLVMStyleWithColumns(75)); 3494 3495 // If the LHS is a binary expression, we should still use the additional break 3496 // as otherwise the formatting hides the operator precedence. 3497 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3498 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3499 " 5) {\n" 3500 "}"); 3501 3502 FormatStyle OnePerLine = getLLVMStyle(); 3503 OnePerLine.BinPackParameters = false; 3504 verifyFormat( 3505 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3506 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3507 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}", 3508 OnePerLine); 3509 } 3510 3511 TEST_F(FormatTest, ExpressionIndentation) { 3512 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3513 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3514 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3515 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3516 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 3517 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n" 3518 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3519 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n" 3520 " ccccccccccccccccccccccccccccccccccccccccc;"); 3521 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3522 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3523 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3524 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3525 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3526 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3527 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3528 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3529 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3530 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3531 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3532 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3533 verifyFormat("if () {\n" 3534 "} else if (aaaaa &&\n" 3535 " bbbbb > // break\n" 3536 " ccccc) {\n" 3537 "}"); 3538 3539 // Presence of a trailing comment used to change indentation of b. 3540 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n" 3541 " b;\n" 3542 "return aaaaaaaaaaaaaaaaaaa +\n" 3543 " b; //", 3544 getLLVMStyleWithColumns(30)); 3545 } 3546 3547 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) { 3548 // Not sure what the best system is here. Like this, the LHS can be found 3549 // immediately above an operator (everything with the same or a higher 3550 // indent). The RHS is aligned right of the operator and so compasses 3551 // everything until something with the same indent as the operator is found. 3552 // FIXME: Is this a good system? 3553 FormatStyle Style = getLLVMStyle(); 3554 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 3555 verifyFormat( 3556 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3557 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3558 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3559 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3560 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3561 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3562 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3563 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3564 " > ccccccccccccccccccccccccccccccccccccccccc;", 3565 Style); 3566 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3567 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3568 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3569 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3570 Style); 3571 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3572 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3573 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3574 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3575 Style); 3576 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3577 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3578 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3579 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3580 Style); 3581 verifyFormat("if () {\n" 3582 "} else if (aaaaa\n" 3583 " && bbbbb // break\n" 3584 " > ccccc) {\n" 3585 "}", 3586 Style); 3587 verifyFormat("return (a)\n" 3588 " // comment\n" 3589 " + b;", 3590 Style); 3591 verifyFormat( 3592 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3593 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3594 " + cc;", 3595 Style); 3596 3597 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3598 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 3599 Style); 3600 3601 // Forced by comments. 3602 verifyFormat( 3603 "unsigned ContentSize =\n" 3604 " sizeof(int16_t) // DWARF ARange version number\n" 3605 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 3606 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 3607 " + sizeof(int8_t); // Segment Size (in bytes)"); 3608 3609 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 3610 " == boost::fusion::at_c<1>(iiii).second;", 3611 Style); 3612 3613 Style.ColumnLimit = 60; 3614 verifyFormat("zzzzzzzzzz\n" 3615 " = bbbbbbbbbbbbbbbbb\n" 3616 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 3617 Style); 3618 } 3619 3620 TEST_F(FormatTest, NoOperandAlignment) { 3621 FormatStyle Style = getLLVMStyle(); 3622 Style.AlignOperands = false; 3623 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 3624 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3625 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3626 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3627 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3628 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3629 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3630 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3631 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3632 " > ccccccccccccccccccccccccccccccccccccccccc;", 3633 Style); 3634 3635 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3636 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3637 " + cc;", 3638 Style); 3639 verifyFormat("int a = aa\n" 3640 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3641 " * cccccccccccccccccccccccccccccccccccc;", 3642 Style); 3643 3644 Style.AlignAfterOpenBracket = false; 3645 verifyFormat("return (a > b\n" 3646 " // comment1\n" 3647 " // comment2\n" 3648 " || c);", 3649 Style); 3650 } 3651 3652 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) { 3653 FormatStyle Style = getLLVMStyle(); 3654 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 3655 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 3656 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3657 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 3658 Style); 3659 } 3660 3661 TEST_F(FormatTest, ConstructorInitializers) { 3662 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 3663 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", 3664 getLLVMStyleWithColumns(45)); 3665 verifyFormat("Constructor()\n" 3666 " : Inttializer(FitsOnTheLine) {}", 3667 getLLVMStyleWithColumns(44)); 3668 verifyFormat("Constructor()\n" 3669 " : Inttializer(FitsOnTheLine) {}", 3670 getLLVMStyleWithColumns(43)); 3671 3672 verifyFormat( 3673 "SomeClass::Constructor()\n" 3674 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 3675 3676 verifyFormat( 3677 "SomeClass::Constructor()\n" 3678 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3679 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}"); 3680 verifyFormat( 3681 "SomeClass::Constructor()\n" 3682 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3683 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 3684 3685 verifyFormat("Constructor()\n" 3686 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3687 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3688 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3689 " aaaaaaaaaaaaaaaaaaaaaaa() {}"); 3690 3691 verifyFormat("Constructor()\n" 3692 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3693 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 3694 3695 verifyFormat("Constructor(int Parameter = 0)\n" 3696 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 3697 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}"); 3698 verifyFormat("Constructor()\n" 3699 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 3700 "}", 3701 getLLVMStyleWithColumns(60)); 3702 verifyFormat("Constructor()\n" 3703 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3704 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}"); 3705 3706 // Here a line could be saved by splitting the second initializer onto two 3707 // lines, but that is not desirable. 3708 verifyFormat("Constructor()\n" 3709 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 3710 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 3711 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 3712 3713 FormatStyle OnePerLine = getLLVMStyle(); 3714 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 3715 verifyFormat("SomeClass::Constructor()\n" 3716 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3717 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3718 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3719 OnePerLine); 3720 verifyFormat("SomeClass::Constructor()\n" 3721 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 3722 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3723 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3724 OnePerLine); 3725 verifyFormat("MyClass::MyClass(int var)\n" 3726 " : some_var_(var), // 4 space indent\n" 3727 " some_other_var_(var + 1) { // lined up\n" 3728 "}", 3729 OnePerLine); 3730 verifyFormat("Constructor()\n" 3731 " : aaaaa(aaaaaa),\n" 3732 " aaaaa(aaaaaa),\n" 3733 " aaaaa(aaaaaa),\n" 3734 " aaaaa(aaaaaa),\n" 3735 " aaaaa(aaaaaa) {}", 3736 OnePerLine); 3737 verifyFormat("Constructor()\n" 3738 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 3739 " aaaaaaaaaaaaaaaaaaaaaa) {}", 3740 OnePerLine); 3741 3742 EXPECT_EQ("Constructor()\n" 3743 " : // Comment forcing unwanted break.\n" 3744 " aaaa(aaaa) {}", 3745 format("Constructor() :\n" 3746 " // Comment forcing unwanted break.\n" 3747 " aaaa(aaaa) {}")); 3748 } 3749 3750 TEST_F(FormatTest, MemoizationTests) { 3751 // This breaks if the memoization lookup does not take \c Indent and 3752 // \c LastSpace into account. 3753 verifyFormat( 3754 "extern CFRunLoopTimerRef\n" 3755 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n" 3756 " CFTimeInterval interval, CFOptionFlags flags,\n" 3757 " CFIndex order, CFRunLoopTimerCallBack callout,\n" 3758 " CFRunLoopTimerContext *context) {}"); 3759 3760 // Deep nesting somewhat works around our memoization. 3761 verifyFormat( 3762 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3763 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3764 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3765 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3766 " aaaaa())))))))))))))))))))))))))))))))))))))));", 3767 getLLVMStyleWithColumns(65)); 3768 verifyFormat( 3769 "aaaaa(\n" 3770 " aaaaa,\n" 3771 " aaaaa(\n" 3772 " aaaaa,\n" 3773 " aaaaa(\n" 3774 " aaaaa,\n" 3775 " aaaaa(\n" 3776 " aaaaa,\n" 3777 " aaaaa(\n" 3778 " aaaaa,\n" 3779 " aaaaa(\n" 3780 " aaaaa,\n" 3781 " aaaaa(\n" 3782 " aaaaa,\n" 3783 " aaaaa(\n" 3784 " aaaaa,\n" 3785 " aaaaa(\n" 3786 " aaaaa,\n" 3787 " aaaaa(\n" 3788 " aaaaa,\n" 3789 " aaaaa(\n" 3790 " aaaaa,\n" 3791 " aaaaa(\n" 3792 " aaaaa,\n" 3793 " aaaaa))))))))))));", 3794 getLLVMStyleWithColumns(65)); 3795 verifyFormat( 3796 "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" 3797 " a),\n" 3798 " a),\n" 3799 " a),\n" 3800 " a),\n" 3801 " a),\n" 3802 " a),\n" 3803 " a),\n" 3804 " a),\n" 3805 " a),\n" 3806 " a),\n" 3807 " a),\n" 3808 " a),\n" 3809 " a),\n" 3810 " a),\n" 3811 " a),\n" 3812 " a),\n" 3813 " a)", 3814 getLLVMStyleWithColumns(65)); 3815 3816 // This test takes VERY long when memoization is broken. 3817 FormatStyle OnePerLine = getLLVMStyle(); 3818 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 3819 OnePerLine.BinPackParameters = false; 3820 std::string input = "Constructor()\n" 3821 " : aaaa(a,\n"; 3822 for (unsigned i = 0, e = 80; i != e; ++i) { 3823 input += " a,\n"; 3824 } 3825 input += " a) {}"; 3826 verifyFormat(input, OnePerLine); 3827 } 3828 3829 TEST_F(FormatTest, BreaksAsHighAsPossible) { 3830 verifyFormat( 3831 "void f() {\n" 3832 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" 3833 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" 3834 " f();\n" 3835 "}"); 3836 verifyFormat("if (Intervals[i].getRange().getFirst() <\n" 3837 " Intervals[i - 1].getRange().getLast()) {\n}"); 3838 } 3839 3840 TEST_F(FormatTest, BreaksFunctionDeclarations) { 3841 // Principially, we break function declarations in a certain order: 3842 // 1) break amongst arguments. 3843 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n" 3844 " Cccccccccccccc cccccccccccccc);"); 3845 verifyFormat("template <class TemplateIt>\n" 3846 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n" 3847 " TemplateIt *stop) {}"); 3848 3849 // 2) break after return type. 3850 verifyFormat( 3851 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3852 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);", 3853 getGoogleStyle()); 3854 3855 // 3) break after (. 3856 verifyFormat( 3857 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n" 3858 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);", 3859 getGoogleStyle()); 3860 3861 // 4) break before after nested name specifiers. 3862 verifyFormat( 3863 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3864 "SomeClasssssssssssssssssssssssssssssssssssssss::\n" 3865 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);", 3866 getGoogleStyle()); 3867 3868 // However, there are exceptions, if a sufficient amount of lines can be 3869 // saved. 3870 // FIXME: The precise cut-offs wrt. the number of saved lines might need some 3871 // more adjusting. 3872 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 3873 " Cccccccccccccc cccccccccc,\n" 3874 " Cccccccccccccc cccccccccc,\n" 3875 " Cccccccccccccc cccccccccc,\n" 3876 " Cccccccccccccc cccccccccc);"); 3877 verifyFormat( 3878 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3879 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3880 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3881 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);", 3882 getGoogleStyle()); 3883 verifyFormat( 3884 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 3885 " Cccccccccccccc cccccccccc,\n" 3886 " Cccccccccccccc cccccccccc,\n" 3887 " Cccccccccccccc cccccccccc,\n" 3888 " Cccccccccccccc cccccccccc,\n" 3889 " Cccccccccccccc cccccccccc,\n" 3890 " Cccccccccccccc cccccccccc);"); 3891 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 3892 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3893 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3894 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3895 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); 3896 3897 // Break after multi-line parameters. 3898 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3899 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3900 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3901 " bbbb bbbb);"); 3902 verifyFormat("void SomeLoooooooooooongFunction(\n" 3903 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 3904 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3905 " int bbbbbbbbbbbbb);"); 3906 3907 // Treat overloaded operators like other functions. 3908 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3909 "operator>(const SomeLoooooooooooooooooooooooooogType &other);"); 3910 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3911 "operator>>(const SomeLooooooooooooooooooooooooogType &other);"); 3912 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3913 "operator<<(const SomeLooooooooooooooooooooooooogType &other);"); 3914 verifyGoogleFormat( 3915 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n" 3916 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 3917 verifyGoogleFormat( 3918 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n" 3919 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 3920 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3921 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 3922 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n" 3923 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 3924 verifyGoogleFormat( 3925 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n" 3926 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3927 " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}"); 3928 3929 FormatStyle Style = getLLVMStyle(); 3930 Style.PointerAlignment = FormatStyle::PAS_Left; 3931 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3932 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}", 3933 Style); 3934 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n" 3935 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3936 Style); 3937 } 3938 3939 TEST_F(FormatTest, TrailingReturnType) { 3940 verifyFormat("auto foo() -> int;\n"); 3941 verifyFormat("struct S {\n" 3942 " auto bar() const -> int;\n" 3943 "};"); 3944 verifyFormat("template <size_t Order, typename T>\n" 3945 "auto load_img(const std::string &filename)\n" 3946 " -> alias::tensor<Order, T, mem::tag::cpu> {}"); 3947 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n" 3948 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}"); 3949 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}"); 3950 3951 // Not trailing return types. 3952 verifyFormat("void f() { auto a = b->c(); }"); 3953 } 3954 3955 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { 3956 // Avoid breaking before trailing 'const' or other trailing annotations, if 3957 // they are not function-like. 3958 FormatStyle Style = getGoogleStyle(); 3959 Style.ColumnLimit = 47; 3960 verifyFormat("void someLongFunction(\n" 3961 " int someLoooooooooooooongParameter) const {\n}", 3962 getLLVMStyleWithColumns(47)); 3963 verifyFormat("LoooooongReturnType\n" 3964 "someLoooooooongFunction() const {}", 3965 getLLVMStyleWithColumns(47)); 3966 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n" 3967 " const {}", 3968 Style); 3969 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3970 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;"); 3971 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3972 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;"); 3973 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3974 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;"); 3975 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n" 3976 " aaaaaaaaaaa aaaaa) const override;"); 3977 verifyGoogleFormat( 3978 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 3979 " const override;"); 3980 3981 // Even if the first parameter has to be wrapped. 3982 verifyFormat("void someLongFunction(\n" 3983 " int someLongParameter) const {}", 3984 getLLVMStyleWithColumns(46)); 3985 verifyFormat("void someLongFunction(\n" 3986 " int someLongParameter) const {}", 3987 Style); 3988 verifyFormat("void someLongFunction(\n" 3989 " int someLongParameter) override {}", 3990 Style); 3991 verifyFormat("void someLongFunction(\n" 3992 " int someLongParameter) OVERRIDE {}", 3993 Style); 3994 verifyFormat("void someLongFunction(\n" 3995 " int someLongParameter) final {}", 3996 Style); 3997 verifyFormat("void someLongFunction(\n" 3998 " int someLongParameter) FINAL {}", 3999 Style); 4000 verifyFormat("void someLongFunction(\n" 4001 " int parameter) const override {}", 4002 Style); 4003 4004 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 4005 verifyFormat("void someLongFunction(\n" 4006 " int someLongParameter) const\n" 4007 "{\n" 4008 "}", 4009 Style); 4010 4011 // Unless these are unknown annotations. 4012 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n" 4013 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4014 " LONG_AND_UGLY_ANNOTATION;"); 4015 4016 // Breaking before function-like trailing annotations is fine to keep them 4017 // close to their arguments. 4018 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4019 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 4020 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 4021 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 4022 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 4023 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}"); 4024 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n" 4025 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);"); 4026 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});"); 4027 4028 verifyFormat( 4029 "void aaaaaaaaaaaaaaaaaa()\n" 4030 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n" 4031 " aaaaaaaaaaaaaaaaaaaaaaaaa));"); 4032 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4033 " __attribute__((unused));"); 4034 verifyGoogleFormat( 4035 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4036 " GUARDED_BY(aaaaaaaaaaaa);"); 4037 verifyGoogleFormat( 4038 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4039 " GUARDED_BY(aaaaaaaaaaaa);"); 4040 verifyGoogleFormat( 4041 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 4042 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4043 verifyGoogleFormat( 4044 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 4045 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4046 } 4047 4048 TEST_F(FormatTest, FunctionAnnotations) { 4049 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 4050 "string OldFunction(const string ¶meter) {}"); 4051 verifyFormat("template <typename T>\n" 4052 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 4053 "string OldFunction(const string ¶meter) {}"); 4054 4055 // Not function annotations. 4056 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4057 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); 4058 } 4059 4060 TEST_F(FormatTest, BreaksDesireably) { 4061 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 4062 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 4063 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); 4064 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4065 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 4066 "}"); 4067 4068 verifyFormat( 4069 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4070 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 4071 4072 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4073 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4074 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4075 4076 verifyFormat( 4077 "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4078 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 4079 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4080 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 4081 4082 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4083 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4084 4085 verifyFormat( 4086 "void f() {\n" 4087 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 4088 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 4089 "}"); 4090 verifyFormat( 4091 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4092 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4093 verifyFormat( 4094 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4095 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4096 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4097 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4098 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4099 4100 // Indent consistently independent of call expression. 4101 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n" 4102 " dddddddddddddddddddddddddddddd));\n" 4103 "aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 4104 " dddddddddddddddddddddddddddddd));"); 4105 4106 // This test case breaks on an incorrect memoization, i.e. an optimization not 4107 // taking into account the StopAt value. 4108 verifyFormat( 4109 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4110 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4111 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4112 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4113 4114 verifyFormat("{\n {\n {\n" 4115 " Annotation.SpaceRequiredBefore =\n" 4116 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" 4117 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" 4118 " }\n }\n}"); 4119 4120 // Break on an outer level if there was a break on an inner level. 4121 EXPECT_EQ("f(g(h(a, // comment\n" 4122 " b, c),\n" 4123 " d, e),\n" 4124 " x, y);", 4125 format("f(g(h(a, // comment\n" 4126 " b, c), d, e), x, y);")); 4127 4128 // Prefer breaking similar line breaks. 4129 verifyFormat( 4130 "const int kTrackingOptions = NSTrackingMouseMoved |\n" 4131 " NSTrackingMouseEnteredAndExited |\n" 4132 " NSTrackingActiveAlways;"); 4133 } 4134 4135 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) { 4136 FormatStyle NoBinPacking = getGoogleStyle(); 4137 NoBinPacking.BinPackParameters = false; 4138 NoBinPacking.BinPackArguments = true; 4139 verifyFormat("void f() {\n" 4140 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n" 4141 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 4142 "}", 4143 NoBinPacking); 4144 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n" 4145 " int aaaaaaaaaaaaaaaaaaaa,\n" 4146 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4147 NoBinPacking); 4148 } 4149 4150 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { 4151 FormatStyle NoBinPacking = getGoogleStyle(); 4152 NoBinPacking.BinPackParameters = false; 4153 NoBinPacking.BinPackArguments = false; 4154 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" 4155 " aaaaaaaaaaaaaaaaaaaa,\n" 4156 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);", 4157 NoBinPacking); 4158 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n" 4159 " aaaaaaaaaaaaa,\n" 4160 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));", 4161 NoBinPacking); 4162 verifyFormat( 4163 "aaaaaaaa(aaaaaaaaaaaaa,\n" 4164 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4165 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 4166 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4167 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));", 4168 NoBinPacking); 4169 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 4170 " .aaaaaaaaaaaaaaaaaa();", 4171 NoBinPacking); 4172 verifyFormat("void f() {\n" 4173 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4174 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n" 4175 "}", 4176 NoBinPacking); 4177 4178 verifyFormat( 4179 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4180 " aaaaaaaaaaaa,\n" 4181 " aaaaaaaaaaaa);", 4182 NoBinPacking); 4183 verifyFormat( 4184 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" 4185 " ddddddddddddddddddddddddddddd),\n" 4186 " test);", 4187 NoBinPacking); 4188 4189 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" 4190 " aaaaaaaaaaaaaaaaaaaaaaa,\n" 4191 " aaaaaaaaaaaaaaaaaaaaaaa> aaaaaaaaaaaaaaaaaa;", 4192 NoBinPacking); 4193 verifyFormat("a(\"a\"\n" 4194 " \"a\",\n" 4195 " a);"); 4196 4197 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 4198 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" 4199 " aaaaaaaaa,\n" 4200 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4201 NoBinPacking); 4202 verifyFormat( 4203 "void f() {\n" 4204 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 4205 " .aaaaaaa();\n" 4206 "}", 4207 NoBinPacking); 4208 verifyFormat( 4209 "template <class SomeType, class SomeOtherType>\n" 4210 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}", 4211 NoBinPacking); 4212 } 4213 4214 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) { 4215 FormatStyle Style = getLLVMStyleWithColumns(15); 4216 Style.ExperimentalAutoDetectBinPacking = true; 4217 EXPECT_EQ("aaa(aaaa,\n" 4218 " aaaa,\n" 4219 " aaaa);\n" 4220 "aaa(aaaa,\n" 4221 " aaaa,\n" 4222 " aaaa);", 4223 format("aaa(aaaa,\n" // one-per-line 4224 " aaaa,\n" 4225 " aaaa );\n" 4226 "aaa(aaaa, aaaa, aaaa);", // inconclusive 4227 Style)); 4228 EXPECT_EQ("aaa(aaaa, aaaa,\n" 4229 " aaaa);\n" 4230 "aaa(aaaa, aaaa,\n" 4231 " aaaa);", 4232 format("aaa(aaaa, aaaa,\n" // bin-packed 4233 " aaaa );\n" 4234 "aaa(aaaa, aaaa, aaaa);", // inconclusive 4235 Style)); 4236 } 4237 4238 TEST_F(FormatTest, FormatsBuilderPattern) { 4239 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n" 4240 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" 4241 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n" 4242 " .StartsWith(\".init\", ORDER_INIT)\n" 4243 " .StartsWith(\".fini\", ORDER_FINI)\n" 4244 " .StartsWith(\".hash\", ORDER_HASH)\n" 4245 " .Default(ORDER_TEXT);\n"); 4246 4247 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" 4248 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); 4249 verifyFormat( 4250 "aaaaaaa->aaaaaaa->aaaaaaaaaaaaaaaa(\n" 4251 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4252 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 4253 verifyFormat( 4254 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" 4255 " aaaaaaaaaaaaaa);"); 4256 verifyFormat( 4257 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n" 4258 " aaaaaa->aaaaaaaaaaaa()\n" 4259 " ->aaaaaaaaaaaaaaaa(\n" 4260 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4261 " ->aaaaaaaaaaaaaaaaa();"); 4262 verifyGoogleFormat( 4263 "void f() {\n" 4264 " someo->Add((new util::filetools::Handler(dir))\n" 4265 " ->OnEvent1(NewPermanentCallback(\n" 4266 " this, &HandlerHolderClass::EventHandlerCBA))\n" 4267 " ->OnEvent2(NewPermanentCallback(\n" 4268 " this, &HandlerHolderClass::EventHandlerCBB))\n" 4269 " ->OnEvent3(NewPermanentCallback(\n" 4270 " this, &HandlerHolderClass::EventHandlerCBC))\n" 4271 " ->OnEvent5(NewPermanentCallback(\n" 4272 " this, &HandlerHolderClass::EventHandlerCBD))\n" 4273 " ->OnEvent6(NewPermanentCallback(\n" 4274 " this, &HandlerHolderClass::EventHandlerCBE)));\n" 4275 "}"); 4276 4277 verifyFormat( 4278 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();"); 4279 verifyFormat("aaaaaaaaaaaaaaa()\n" 4280 " .aaaaaaaaaaaaaaa()\n" 4281 " .aaaaaaaaaaaaaaa()\n" 4282 " .aaaaaaaaaaaaaaa()\n" 4283 " .aaaaaaaaaaaaaaa();"); 4284 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4285 " .aaaaaaaaaaaaaaa()\n" 4286 " .aaaaaaaaaaaaaaa()\n" 4287 " .aaaaaaaaaaaaaaa();"); 4288 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4289 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4290 " .aaaaaaaaaaaaaaa();"); 4291 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n" 4292 " ->aaaaaaaaaaaaaae(0)\n" 4293 " ->aaaaaaaaaaaaaaa();"); 4294 4295 // Don't linewrap after very short segments. 4296 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4297 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4298 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4299 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4300 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4301 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4302 verifyFormat("aaa()\n" 4303 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4304 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4305 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4306 4307 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 4308 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4309 " .has<bbbbbbbbbbbbbbbbbbbbb>();"); 4310 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 4311 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 4312 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();"); 4313 4314 // Prefer not to break after empty parentheses. 4315 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n" 4316 " First->LastNewlineOffset);"); 4317 } 4318 4319 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { 4320 verifyFormat( 4321 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4322 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); 4323 verifyFormat( 4324 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n" 4325 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}"); 4326 4327 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 4328 " ccccccccccccccccccccccccc) {\n}"); 4329 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n" 4330 " ccccccccccccccccccccccccc) {\n}"); 4331 4332 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 4333 " ccccccccccccccccccccccccc) {\n}"); 4334 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n" 4335 " ccccccccccccccccccccccccc) {\n}"); 4336 4337 verifyFormat( 4338 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" 4339 " ccccccccccccccccccccccccc) {\n}"); 4340 verifyFormat( 4341 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n" 4342 " ccccccccccccccccccccccccc) {\n}"); 4343 4344 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" 4345 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" 4346 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" 4347 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 4348 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n" 4349 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n" 4350 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n" 4351 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 4352 4353 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" 4354 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" 4355 " aaaaaaaaaaaaaaa != aa) {\n}"); 4356 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n" 4357 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n" 4358 " aaaaaaaaaaaaaaa != aa) {\n}"); 4359 } 4360 4361 TEST_F(FormatTest, BreaksAfterAssignments) { 4362 verifyFormat( 4363 "unsigned Cost =\n" 4364 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" 4365 " SI->getPointerAddressSpaceee());\n"); 4366 verifyFormat( 4367 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" 4368 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); 4369 4370 verifyFormat( 4371 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n" 4372 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); 4373 verifyFormat("unsigned OriginalStartColumn =\n" 4374 " SourceMgr.getSpellingColumnNumber(\n" 4375 " Current.FormatTok.getStartOfNonWhitespace()) -\n" 4376 " 1;"); 4377 } 4378 4379 TEST_F(FormatTest, AlignsAfterAssignments) { 4380 verifyFormat( 4381 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4382 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4383 verifyFormat( 4384 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4385 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4386 verifyFormat( 4387 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4388 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4389 verifyFormat( 4390 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4391 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 4392 verifyFormat( 4393 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" 4394 " aaaaaaaaaaaaaaaaaaaaaaaa +\n" 4395 " aaaaaaaaaaaaaaaaaaaaaaaa;"); 4396 } 4397 4398 TEST_F(FormatTest, AlignsAfterReturn) { 4399 verifyFormat( 4400 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4401 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4402 verifyFormat( 4403 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4404 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 4405 verifyFormat( 4406 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 4407 " aaaaaaaaaaaaaaaaaaaaaa();"); 4408 verifyFormat( 4409 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 4410 " aaaaaaaaaaaaaaaaaaaaaa());"); 4411 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4412 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4413 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4414 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n" 4415 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4416 verifyFormat("return\n" 4417 " // true if code is one of a or b.\n" 4418 " code == a || code == b;"); 4419 } 4420 4421 TEST_F(FormatTest, AlignsAfterOpenBracket) { 4422 verifyFormat( 4423 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 4424 " aaaaaaaaa aaaaaaa) {}"); 4425 verifyFormat( 4426 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 4427 " aaaaaaaaaaa aaaaaaaaa);"); 4428 verifyFormat( 4429 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 4430 " aaaaaaaaaaaaaaaaaaaaa));"); 4431 FormatStyle Style = getLLVMStyle(); 4432 Style.AlignAfterOpenBracket = false; 4433 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4434 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}", 4435 Style); 4436 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 4437 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);", 4438 Style); 4439 verifyFormat("SomeLongVariableName->someFunction(\n" 4440 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));", 4441 Style); 4442 verifyFormat( 4443 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 4444 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4445 Style); 4446 verifyFormat( 4447 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 4448 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4449 Style); 4450 verifyFormat( 4451 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 4452 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 4453 Style); 4454 } 4455 4456 TEST_F(FormatTest, ParenthesesAndOperandAlignment) { 4457 FormatStyle Style = getLLVMStyleWithColumns(40); 4458 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4459 " bbbbbbbbbbbbbbbbbbbbbb);", 4460 Style); 4461 Style.AlignAfterOpenBracket = true; 4462 Style.AlignOperands = false; 4463 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4464 " bbbbbbbbbbbbbbbbbbbbbb);", 4465 Style); 4466 Style.AlignAfterOpenBracket = false; 4467 Style.AlignOperands = true; 4468 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4469 " bbbbbbbbbbbbbbbbbbbbbb);", 4470 Style); 4471 Style.AlignAfterOpenBracket = false; 4472 Style.AlignOperands = false; 4473 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4474 " bbbbbbbbbbbbbbbbbbbbbb);", 4475 Style); 4476 } 4477 4478 TEST_F(FormatTest, BreaksConditionalExpressions) { 4479 verifyFormat( 4480 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4481 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4482 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4483 verifyFormat( 4484 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4485 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4486 verifyFormat( 4487 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" 4488 " : aaaaaaaaaaaaa);"); 4489 verifyFormat( 4490 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4491 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4492 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4493 " aaaaaaaaaaaaa);"); 4494 verifyFormat( 4495 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4496 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4497 " aaaaaaaaaaaaa);"); 4498 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4499 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4500 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4501 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4502 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4503 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4504 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4505 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4506 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4507 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4508 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4509 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4510 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4511 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4512 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4513 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4514 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4515 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4516 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4517 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4518 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 4519 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4520 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4521 " : aaaaaaaaaaaaaaaa;"); 4522 verifyFormat( 4523 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4524 " ? aaaaaaaaaaaaaaa\n" 4525 " : aaaaaaaaaaaaaaa;"); 4526 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 4527 " aaaaaaaaa\n" 4528 " ? b\n" 4529 " : c);"); 4530 verifyFormat("return aaaa == bbbb\n" 4531 " // comment\n" 4532 " ? aaaa\n" 4533 " : bbbb;"); 4534 verifyFormat("unsigned Indent =\n" 4535 " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n" 4536 " ? IndentForLevel[TheLine.Level]\n" 4537 " : TheLine * 2,\n" 4538 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 4539 getLLVMStyleWithColumns(70)); 4540 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 4541 " ? aaaaaaaaaaaaaaa\n" 4542 " : bbbbbbbbbbbbbbb //\n" 4543 " ? ccccccccccccccc\n" 4544 " : ddddddddddddddd;"); 4545 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 4546 " ? aaaaaaaaaaaaaaa\n" 4547 " : (bbbbbbbbbbbbbbb //\n" 4548 " ? ccccccccccccccc\n" 4549 " : ddddddddddddddd);"); 4550 verifyFormat( 4551 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4552 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4553 " aaaaaaaaaaaaaaaaaaaaa +\n" 4554 " aaaaaaaaaaaaaaaaaaaaa\n" 4555 " : aaaaaaaaaa;"); 4556 verifyFormat( 4557 "aaaaaa = aaaaaaaaaaaa\n" 4558 " ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4559 " : aaaaaaaaaaaaaaaaaaaaaa\n" 4560 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4561 4562 FormatStyle NoBinPacking = getLLVMStyle(); 4563 NoBinPacking.BinPackArguments = false; 4564 verifyFormat( 4565 "void f() {\n" 4566 " g(aaa,\n" 4567 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 4568 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4569 " ? aaaaaaaaaaaaaaa\n" 4570 " : aaaaaaaaaaaaaaa);\n" 4571 "}", 4572 NoBinPacking); 4573 verifyFormat( 4574 "void f() {\n" 4575 " g(aaa,\n" 4576 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 4577 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4578 " ?: aaaaaaaaaaaaaaa);\n" 4579 "}", 4580 NoBinPacking); 4581 4582 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n" 4583 " // comment.\n" 4584 " ccccccccccccccccccccccccccccccccccccccc\n" 4585 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4586 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);"); 4587 4588 // Assignments in conditional expressions. Apparently not uncommon :-(. 4589 verifyFormat("return a != b\n" 4590 " // comment\n" 4591 " ? a = b\n" 4592 " : a = b;"); 4593 verifyFormat("return a != b\n" 4594 " // comment\n" 4595 " ? a = a != b\n" 4596 " // comment\n" 4597 " ? a = b\n" 4598 " : a\n" 4599 " : a;\n"); 4600 verifyFormat("return a != b\n" 4601 " // comment\n" 4602 " ? a\n" 4603 " : a = a != b\n" 4604 " // comment\n" 4605 " ? a = b\n" 4606 " : a;"); 4607 } 4608 4609 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) { 4610 FormatStyle Style = getLLVMStyle(); 4611 Style.BreakBeforeTernaryOperators = false; 4612 Style.ColumnLimit = 70; 4613 verifyFormat( 4614 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4615 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4616 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4617 Style); 4618 verifyFormat( 4619 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4620 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4621 Style); 4622 verifyFormat( 4623 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n" 4624 " aaaaaaaaaaaaa);", 4625 Style); 4626 verifyFormat( 4627 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4628 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4629 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4630 " aaaaaaaaaaaaa);", 4631 Style); 4632 verifyFormat( 4633 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4634 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4635 " aaaaaaaaaaaaa);", 4636 Style); 4637 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4638 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4639 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 4640 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4641 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4642 Style); 4643 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4644 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4645 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4646 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 4647 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4648 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4649 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4650 Style); 4651 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4652 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n" 4653 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4654 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4655 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4656 Style); 4657 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4658 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4659 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4660 Style); 4661 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 4662 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4663 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4664 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4665 Style); 4666 verifyFormat( 4667 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4668 " aaaaaaaaaaaaaaa :\n" 4669 " aaaaaaaaaaaaaaa;", 4670 Style); 4671 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 4672 " aaaaaaaaa ?\n" 4673 " b :\n" 4674 " c);", 4675 Style); 4676 verifyFormat( 4677 "unsigned Indent =\n" 4678 " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0 ?\n" 4679 " IndentForLevel[TheLine.Level] :\n" 4680 " TheLine * 2,\n" 4681 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 4682 Style); 4683 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 4684 " aaaaaaaaaaaaaaa :\n" 4685 " bbbbbbbbbbbbbbb ? //\n" 4686 " ccccccccccccccc :\n" 4687 " ddddddddddddddd;", 4688 Style); 4689 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 4690 " aaaaaaaaaaaaaaa :\n" 4691 " (bbbbbbbbbbbbbbb ? //\n" 4692 " ccccccccccccccc :\n" 4693 " ddddddddddddddd);", 4694 Style); 4695 } 4696 4697 TEST_F(FormatTest, DeclarationsOfMultipleVariables) { 4698 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" 4699 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); 4700 verifyFormat("bool a = true, b = false;"); 4701 4702 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" 4703 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" 4704 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" 4705 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); 4706 verifyFormat( 4707 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 4708 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n" 4709 " d = e && f;"); 4710 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n" 4711 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;"); 4712 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 4713 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;"); 4714 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n" 4715 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;"); 4716 4717 FormatStyle Style = getGoogleStyle(); 4718 Style.PointerAlignment = FormatStyle::PAS_Left; 4719 Style.DerivePointerAlignment = false; 4720 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4721 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" 4722 " *b = bbbbbbbbbbbbbbbbbbb;", 4723 Style); 4724 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 4725 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;", 4726 Style); 4727 } 4728 4729 TEST_F(FormatTest, ConditionalExpressionsInBrackets) { 4730 verifyFormat("arr[foo ? bar : baz];"); 4731 verifyFormat("f()[foo ? bar : baz];"); 4732 verifyFormat("(a + b)[foo ? bar : baz];"); 4733 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); 4734 } 4735 4736 TEST_F(FormatTest, AlignsStringLiterals) { 4737 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" 4738 " \"short literal\");"); 4739 verifyFormat( 4740 "looooooooooooooooooooooooongFunction(\n" 4741 " \"short literal\"\n" 4742 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); 4743 verifyFormat("someFunction(\"Always break between multi-line\"\n" 4744 " \" string literals\",\n" 4745 " and, other, parameters);"); 4746 EXPECT_EQ("fun + \"1243\" /* comment */\n" 4747 " \"5678\";", 4748 format("fun + \"1243\" /* comment */\n" 4749 " \"5678\";", 4750 getLLVMStyleWithColumns(28))); 4751 EXPECT_EQ( 4752 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 4753 " \"aaaaaaaaaaaaaaaaaaaaa\"\n" 4754 " \"aaaaaaaaaaaaaaaa\";", 4755 format("aaaaaa =" 4756 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " 4757 "aaaaaaaaaaaaaaaaaaaaa\" " 4758 "\"aaaaaaaaaaaaaaaa\";")); 4759 verifyFormat("a = a + \"a\"\n" 4760 " \"a\"\n" 4761 " \"a\";"); 4762 verifyFormat("f(\"a\", \"b\"\n" 4763 " \"c\");"); 4764 4765 verifyFormat( 4766 "#define LL_FORMAT \"ll\"\n" 4767 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n" 4768 " \"d, ddddddddd: %\" LL_FORMAT \"d\");"); 4769 4770 verifyFormat("#define A(X) \\\n" 4771 " \"aaaaa\" #X \"bbbbbb\" \\\n" 4772 " \"ccccc\"", 4773 getLLVMStyleWithColumns(23)); 4774 verifyFormat("#define A \"def\"\n" 4775 "f(\"abc\" A \"ghi\"\n" 4776 " \"jkl\");"); 4777 4778 verifyFormat("f(L\"a\"\n" 4779 " L\"b\");"); 4780 verifyFormat("#define A(X) \\\n" 4781 " L\"aaaaa\" #X L\"bbbbbb\" \\\n" 4782 " L\"ccccc\"", 4783 getLLVMStyleWithColumns(25)); 4784 4785 verifyFormat("f(@\"a\"\n" 4786 " @\"b\");"); 4787 verifyFormat("NSString s = @\"a\"\n" 4788 " @\"b\"\n" 4789 " @\"c\";"); 4790 verifyFormat("NSString s = @\"a\"\n" 4791 " \"b\"\n" 4792 " \"c\";"); 4793 } 4794 4795 TEST_F(FormatTest, AlwaysBreakAfterDefinitionReturnType) { 4796 FormatStyle AfterType = getLLVMStyle(); 4797 AfterType.AlwaysBreakAfterDefinitionReturnType = true; 4798 verifyFormat("const char *\n" 4799 "f(void) {\n" // Break here. 4800 " return \"\";\n" 4801 "}\n" 4802 "const char *bar(void);\n", // No break here. 4803 AfterType); 4804 verifyFormat("template <class T>\n" 4805 "T *\n" 4806 "f(T &c) {\n" // Break here. 4807 " return NULL;\n" 4808 "}\n" 4809 "template <class T> T *f(T &c);\n", // No break here. 4810 AfterType); 4811 AfterType.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 4812 verifyFormat("const char *\n" 4813 "f(void)\n" // Break here. 4814 "{\n" 4815 " return \"\";\n" 4816 "}\n" 4817 "const char *bar(void);\n", // No break here. 4818 AfterType); 4819 verifyFormat("template <class T>\n" 4820 "T *\n" // Problem here: no line break 4821 "f(T &c)\n" // Break here. 4822 "{\n" 4823 " return NULL;\n" 4824 "}\n" 4825 "template <class T> T *f(T &c);\n", // No break here. 4826 AfterType); 4827 } 4828 4829 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) { 4830 FormatStyle NoBreak = getLLVMStyle(); 4831 NoBreak.AlwaysBreakBeforeMultilineStrings = false; 4832 FormatStyle Break = getLLVMStyle(); 4833 Break.AlwaysBreakBeforeMultilineStrings = true; 4834 verifyFormat("aaaa = \"bbbb\"\n" 4835 " \"cccc\";", 4836 NoBreak); 4837 verifyFormat("aaaa =\n" 4838 " \"bbbb\"\n" 4839 " \"cccc\";", 4840 Break); 4841 verifyFormat("aaaa(\"bbbb\"\n" 4842 " \"cccc\");", 4843 NoBreak); 4844 verifyFormat("aaaa(\n" 4845 " \"bbbb\"\n" 4846 " \"cccc\");", 4847 Break); 4848 verifyFormat("aaaa(qqq, \"bbbb\"\n" 4849 " \"cccc\");", 4850 NoBreak); 4851 verifyFormat("aaaa(qqq,\n" 4852 " \"bbbb\"\n" 4853 " \"cccc\");", 4854 Break); 4855 verifyFormat("aaaa(qqq,\n" 4856 " L\"bbbb\"\n" 4857 " L\"cccc\");", 4858 Break); 4859 4860 // As we break before unary operators, breaking right after them is bad. 4861 verifyFormat("string foo = abc ? \"x\"\n" 4862 " \"blah blah blah blah blah blah\"\n" 4863 " : \"y\";", 4864 Break); 4865 4866 // Don't break if there is no column gain. 4867 verifyFormat("f(\"aaaa\"\n" 4868 " \"bbbb\");", 4869 Break); 4870 4871 // Treat literals with escaped newlines like multi-line string literals. 4872 EXPECT_EQ("x = \"a\\\n" 4873 "b\\\n" 4874 "c\";", 4875 format("x = \"a\\\n" 4876 "b\\\n" 4877 "c\";", 4878 NoBreak)); 4879 EXPECT_EQ("x =\n" 4880 " \"a\\\n" 4881 "b\\\n" 4882 "c\";", 4883 format("x = \"a\\\n" 4884 "b\\\n" 4885 "c\";", 4886 Break)); 4887 4888 // Exempt ObjC strings for now. 4889 EXPECT_EQ("NSString *const kString = @\"aaaa\"\n" 4890 " @\"bbbb\";", 4891 format("NSString *const kString = @\"aaaa\"\n" 4892 "@\"bbbb\";", 4893 Break)); 4894 4895 Break.ColumnLimit = 0; 4896 verifyFormat("const char *hello = \"hello llvm\";", Break); 4897 } 4898 4899 TEST_F(FormatTest, AlignsPipes) { 4900 verifyFormat( 4901 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4902 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4903 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4904 verifyFormat( 4905 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 4906 " << aaaaaaaaaaaaaaaaaaaa;"); 4907 verifyFormat( 4908 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4909 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4910 verifyFormat( 4911 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 4912 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 4913 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 4914 verifyFormat( 4915 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4916 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4917 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4918 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4919 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4920 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4921 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 4922 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n" 4923 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);"); 4924 verifyFormat( 4925 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4926 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4927 4928 verifyFormat("return out << \"somepacket = {\\n\"\n" 4929 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" 4930 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" 4931 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" 4932 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" 4933 " << \"}\";"); 4934 4935 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 4936 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 4937 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;"); 4938 verifyFormat( 4939 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" 4940 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" 4941 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" 4942 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" 4943 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); 4944 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" 4945 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 4946 verifyFormat( 4947 "void f() {\n" 4948 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n" 4949 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 4950 "}"); 4951 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n" 4952 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();"); 4953 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4954 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4955 " aaaaaaaaaaaaaaaaaaaaa)\n" 4956 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4957 4958 // Breaking before the first "<<" is generally not desirable. 4959 verifyFormat( 4960 "llvm::errs()\n" 4961 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4962 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4963 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4964 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4965 getLLVMStyleWithColumns(70)); 4966 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n" 4967 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4968 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 4969 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4970 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 4971 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4972 getLLVMStyleWithColumns(70)); 4973 4974 // But sometimes, breaking before the first "<<" is desirable. 4975 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 4976 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);"); 4977 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n" 4978 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4979 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4980 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n" 4981 " << BEF << IsTemplate << Description << E->getType();"); 4982 4983 verifyFormat( 4984 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4985 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4986 4987 // Incomplete string literal. 4988 EXPECT_EQ("llvm::errs() << \"\n" 4989 " << a;", 4990 format("llvm::errs() << \"\n<<a;")); 4991 4992 verifyFormat("void f() {\n" 4993 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n" 4994 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n" 4995 "}"); 4996 4997 // Handle 'endl'. 4998 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n" 4999 " << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 5000 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 5001 } 5002 5003 TEST_F(FormatTest, UnderstandsEquals) { 5004 verifyFormat( 5005 "aaaaaaaaaaaaaaaaa =\n" 5006 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5007 verifyFormat( 5008 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5009 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 5010 verifyFormat( 5011 "if (a) {\n" 5012 " f();\n" 5013 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5014 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 5015 "}"); 5016 5017 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5018 " 100000000 + 10000000) {\n}"); 5019 } 5020 5021 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 5022 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 5023 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 5024 5025 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 5026 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 5027 5028 verifyFormat( 5029 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 5030 " Parameter2);"); 5031 5032 verifyFormat( 5033 "ShortObject->shortFunction(\n" 5034 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 5035 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 5036 5037 verifyFormat("loooooooooooooongFunction(\n" 5038 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 5039 5040 verifyFormat( 5041 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 5042 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 5043 5044 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 5045 " .WillRepeatedly(Return(SomeValue));"); 5046 verifyFormat("void f() {\n" 5047 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 5048 " .Times(2)\n" 5049 " .WillRepeatedly(Return(SomeValue));\n" 5050 "}"); 5051 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n" 5052 " ccccccccccccccccccccccc);"); 5053 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5054 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5055 " .aaaaa(aaaaa),\n" 5056 " aaaaaaaaaaaaaaaaaaaaa);"); 5057 verifyFormat("void f() {\n" 5058 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5059 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n" 5060 "}"); 5061 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5062 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5063 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5064 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5065 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 5066 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5067 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5068 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5069 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n" 5070 "}"); 5071 5072 // Here, it is not necessary to wrap at "." or "->". 5073 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 5074 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 5075 verifyFormat( 5076 "aaaaaaaaaaa->aaaaaaaaa(\n" 5077 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5078 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 5079 5080 verifyFormat( 5081 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5082 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); 5083 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n" 5084 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 5085 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" 5086 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 5087 5088 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5089 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5090 " .a();"); 5091 5092 FormatStyle NoBinPacking = getLLVMStyle(); 5093 NoBinPacking.BinPackParameters = false; 5094 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 5095 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 5096 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" 5097 " aaaaaaaaaaaaaaaaaaa,\n" 5098 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5099 NoBinPacking); 5100 5101 // If there is a subsequent call, change to hanging indentation. 5102 verifyFormat( 5103 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5104 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n" 5105 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5106 verifyFormat( 5107 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5108 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));"); 5109 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5110 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5111 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5112 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5113 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5114 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 5115 } 5116 5117 TEST_F(FormatTest, WrapsTemplateDeclarations) { 5118 verifyFormat("template <typename T>\n" 5119 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 5120 verifyFormat("template <typename T>\n" 5121 "// T should be one of {A, B}.\n" 5122 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 5123 verifyFormat( 5124 "template <typename T>\n" 5125 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); 5126 verifyFormat("template <typename T>\n" 5127 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 5128 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 5129 verifyFormat( 5130 "template <typename T>\n" 5131 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 5132 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 5133 verifyFormat( 5134 "template <typename T>\n" 5135 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 5136 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 5137 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5138 verifyFormat("template <typename T>\n" 5139 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5140 " int aaaaaaaaaaaaaaaaaaaaaa);"); 5141 verifyFormat( 5142 "template <typename T1, typename T2 = char, typename T3 = char,\n" 5143 " typename T4 = char>\n" 5144 "void f();"); 5145 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n" 5146 " template <typename> class cccccccccccccccccccccc,\n" 5147 " typename ddddddddddddd>\n" 5148 "class C {};"); 5149 verifyFormat( 5150 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 5151 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5152 5153 verifyFormat("void f() {\n" 5154 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 5155 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n" 5156 "}"); 5157 5158 verifyFormat("template <typename T> class C {};"); 5159 verifyFormat("template <typename T> void f();"); 5160 verifyFormat("template <typename T> void f() {}"); 5161 verifyFormat( 5162 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 5163 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5164 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n" 5165 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 5166 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5167 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n" 5168 " bbbbbbbbbbbbbbbbbbbbbbbb);", 5169 getLLVMStyleWithColumns(72)); 5170 EXPECT_EQ("static_cast<A< //\n" 5171 " B> *>(\n" 5172 "\n" 5173 " );", 5174 format("static_cast<A<//\n" 5175 " B>*>(\n" 5176 "\n" 5177 " );")); 5178 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5179 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);"); 5180 5181 FormatStyle AlwaysBreak = getLLVMStyle(); 5182 AlwaysBreak.AlwaysBreakTemplateDeclarations = true; 5183 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak); 5184 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak); 5185 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak); 5186 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5187 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 5188 " ccccccccccccccccccccccccccccccccccccccccccccccc);"); 5189 verifyFormat("template <template <typename> class Fooooooo,\n" 5190 " template <typename> class Baaaaaaar>\n" 5191 "struct C {};", 5192 AlwaysBreak); 5193 verifyFormat("template <typename T> // T can be A, B or C.\n" 5194 "struct C {};", 5195 AlwaysBreak); 5196 } 5197 5198 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 5199 verifyFormat( 5200 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5201 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5202 verifyFormat( 5203 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5204 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5205 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 5206 5207 // FIXME: Should we have the extra indent after the second break? 5208 verifyFormat( 5209 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5210 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5211 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5212 5213 verifyFormat( 5214 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 5215 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 5216 5217 // Breaking at nested name specifiers is generally not desirable. 5218 verifyFormat( 5219 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5220 " aaaaaaaaaaaaaaaaaaaaaaa);"); 5221 5222 verifyFormat( 5223 "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5224 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5225 " aaaaaaaaaaaaaaaaaaaaa);", 5226 getLLVMStyleWithColumns(74)); 5227 5228 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5229 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5230 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5231 } 5232 5233 TEST_F(FormatTest, UnderstandsTemplateParameters) { 5234 verifyFormat("A<int> a;"); 5235 verifyFormat("A<A<A<int>>> a;"); 5236 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 5237 verifyFormat("bool x = a < 1 || 2 > a;"); 5238 verifyFormat("bool x = 5 < f<int>();"); 5239 verifyFormat("bool x = f<int>() > 5;"); 5240 verifyFormat("bool x = 5 < a<int>::x;"); 5241 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 5242 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 5243 5244 verifyGoogleFormat("A<A<int>> a;"); 5245 verifyGoogleFormat("A<A<A<int>>> a;"); 5246 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 5247 verifyGoogleFormat("A<A<int> > a;"); 5248 verifyGoogleFormat("A<A<A<int> > > a;"); 5249 verifyGoogleFormat("A<A<A<A<int> > > > a;"); 5250 verifyGoogleFormat("A<::A<int>> a;"); 5251 verifyGoogleFormat("A<::A> a;"); 5252 verifyGoogleFormat("A< ::A> a;"); 5253 verifyGoogleFormat("A< ::A<int> > a;"); 5254 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); 5255 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); 5256 EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle())); 5257 EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle())); 5258 5259 verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp)); 5260 5261 verifyFormat("test >> a >> b;"); 5262 verifyFormat("test << a >> b;"); 5263 5264 verifyFormat("f<int>();"); 5265 verifyFormat("template <typename T> void f() {}"); 5266 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;"); 5267 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : " 5268 "sizeof(char)>::type>;"); 5269 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};"); 5270 5271 // Not template parameters. 5272 verifyFormat("return a < b && c > d;"); 5273 verifyFormat("void f() {\n" 5274 " while (a < b && c > d) {\n" 5275 " }\n" 5276 "}"); 5277 verifyFormat("template <typename... Types>\n" 5278 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}"); 5279 5280 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5281 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);", 5282 getLLVMStyleWithColumns(60)); 5283 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");"); 5284 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}"); 5285 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <"); 5286 } 5287 5288 TEST_F(FormatTest, UnderstandsBinaryOperators) { 5289 verifyFormat("COMPARE(a, ==, b);"); 5290 } 5291 5292 TEST_F(FormatTest, UnderstandsPointersToMembers) { 5293 verifyFormat("int A::*x;"); 5294 verifyFormat("int (S::*func)(void *);"); 5295 verifyFormat("void f() { int (S::*func)(void *); }"); 5296 verifyFormat("typedef bool *(Class::*Member)() const;"); 5297 verifyFormat("void f() {\n" 5298 " (a->*f)();\n" 5299 " a->*x;\n" 5300 " (a.*f)();\n" 5301 " ((*a).*f)();\n" 5302 " a.*x;\n" 5303 "}"); 5304 verifyFormat("void f() {\n" 5305 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 5306 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n" 5307 "}"); 5308 verifyFormat( 5309 "(aaaaaaaaaa->*bbbbbbb)(\n" 5310 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 5311 FormatStyle Style = getLLVMStyle(); 5312 Style.PointerAlignment = FormatStyle::PAS_Left; 5313 verifyFormat("typedef bool* (Class::*Member)() const;", Style); 5314 } 5315 5316 TEST_F(FormatTest, UnderstandsUnaryOperators) { 5317 verifyFormat("int a = -2;"); 5318 verifyFormat("f(-1, -2, -3);"); 5319 verifyFormat("a[-1] = 5;"); 5320 verifyFormat("int a = 5 + -2;"); 5321 verifyFormat("if (i == -1) {\n}"); 5322 verifyFormat("if (i != -1) {\n}"); 5323 verifyFormat("if (i > -1) {\n}"); 5324 verifyFormat("if (i < -1) {\n}"); 5325 verifyFormat("++(a->f());"); 5326 verifyFormat("--(a->f());"); 5327 verifyFormat("(a->f())++;"); 5328 verifyFormat("a[42]++;"); 5329 verifyFormat("if (!(a->f())) {\n}"); 5330 5331 verifyFormat("a-- > b;"); 5332 verifyFormat("b ? -a : c;"); 5333 verifyFormat("n * sizeof char16;"); 5334 verifyFormat("n * alignof char16;", getGoogleStyle()); 5335 verifyFormat("sizeof(char);"); 5336 verifyFormat("alignof(char);", getGoogleStyle()); 5337 5338 verifyFormat("return -1;"); 5339 verifyFormat("switch (a) {\n" 5340 "case -1:\n" 5341 " break;\n" 5342 "}"); 5343 verifyFormat("#define X -1"); 5344 verifyFormat("#define X -kConstant"); 5345 5346 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};"); 5347 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};"); 5348 5349 verifyFormat("int a = /* confusing comment */ -1;"); 5350 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 5351 verifyFormat("int a = i /* confusing comment */++;"); 5352 } 5353 5354 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) { 5355 verifyFormat("if (!aaaaaaaaaa( // break\n" 5356 " aaaaa)) {\n" 5357 "}"); 5358 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n" 5359 " aaaaa));"); 5360 verifyFormat("*aaa = aaaaaaa( // break\n" 5361 " bbbbbb);"); 5362 } 5363 5364 TEST_F(FormatTest, UnderstandsOverloadedOperators) { 5365 verifyFormat("bool operator<();"); 5366 verifyFormat("bool operator>();"); 5367 verifyFormat("bool operator=();"); 5368 verifyFormat("bool operator==();"); 5369 verifyFormat("bool operator!=();"); 5370 verifyFormat("int operator+();"); 5371 verifyFormat("int operator++();"); 5372 verifyFormat("bool operator();"); 5373 verifyFormat("bool operator()();"); 5374 verifyFormat("bool operator[]();"); 5375 verifyFormat("operator bool();"); 5376 verifyFormat("operator int();"); 5377 verifyFormat("operator void *();"); 5378 verifyFormat("operator SomeType<int>();"); 5379 verifyFormat("operator SomeType<int, int>();"); 5380 verifyFormat("operator SomeType<SomeType<int>>();"); 5381 verifyFormat("void *operator new(std::size_t size);"); 5382 verifyFormat("void *operator new[](std::size_t size);"); 5383 verifyFormat("void operator delete(void *ptr);"); 5384 verifyFormat("void operator delete[](void *ptr);"); 5385 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n" 5386 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);"); 5387 5388 verifyFormat( 5389 "ostream &operator<<(ostream &OutputStream,\n" 5390 " SomeReallyLongType WithSomeReallyLongValue);"); 5391 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n" 5392 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n" 5393 " return left.group < right.group;\n" 5394 "}"); 5395 verifyFormat("SomeType &operator=(const SomeType &S);"); 5396 verifyFormat("f.template operator()<int>();"); 5397 5398 verifyGoogleFormat("operator void*();"); 5399 verifyGoogleFormat("operator SomeType<SomeType<int>>();"); 5400 verifyGoogleFormat("operator ::A();"); 5401 5402 verifyFormat("using A::operator+;"); 5403 5404 verifyFormat("string // break\n" 5405 "operator()() & {}"); 5406 verifyFormat("string // break\n" 5407 "operator()() && {}"); 5408 } 5409 5410 TEST_F(FormatTest, UnderstandsFunctionRefQualification) { 5411 verifyFormat("Deleted &operator=(const Deleted &)& = default;"); 5412 verifyFormat("Deleted &operator=(const Deleted &)&& = delete;"); 5413 verifyFormat("SomeType MemberFunction(const Deleted &)& = delete;"); 5414 verifyFormat("SomeType MemberFunction(const Deleted &)&& = delete;"); 5415 verifyFormat("Deleted &operator=(const Deleted &)&;"); 5416 verifyFormat("Deleted &operator=(const Deleted &)&&;"); 5417 verifyFormat("SomeType MemberFunction(const Deleted &)&;"); 5418 verifyFormat("SomeType MemberFunction(const Deleted &)&&;"); 5419 5420 verifyGoogleFormat("Deleted& operator=(const Deleted&)& = default;"); 5421 verifyGoogleFormat("SomeType MemberFunction(const Deleted&)& = delete;"); 5422 verifyGoogleFormat("Deleted& operator=(const Deleted&)&;"); 5423 verifyGoogleFormat("SomeType MemberFunction(const Deleted&)&;"); 5424 5425 FormatStyle Spaces = getLLVMStyle(); 5426 Spaces.SpacesInCStyleCastParentheses = true; 5427 verifyFormat("Deleted &operator=(const Deleted &)& = default;", Spaces); 5428 verifyFormat("SomeType MemberFunction(const Deleted &)& = delete;", Spaces); 5429 verifyFormat("Deleted &operator=(const Deleted &)&;", Spaces); 5430 verifyFormat("SomeType MemberFunction(const Deleted &)&;", Spaces); 5431 5432 Spaces.SpacesInCStyleCastParentheses = false; 5433 Spaces.SpacesInParentheses = true; 5434 verifyFormat("Deleted &operator=( const Deleted & )& = default;", Spaces); 5435 verifyFormat("SomeType MemberFunction( const Deleted & )& = delete;", Spaces); 5436 verifyFormat("Deleted &operator=( const Deleted & )&;", Spaces); 5437 verifyFormat("SomeType MemberFunction( const Deleted & )&;", Spaces); 5438 } 5439 5440 TEST_F(FormatTest, UnderstandsNewAndDelete) { 5441 verifyFormat("void f() {\n" 5442 " A *a = new A;\n" 5443 " A *a = new (placement) A;\n" 5444 " delete a;\n" 5445 " delete (A *)a;\n" 5446 "}"); 5447 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 5448 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 5449 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5450 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 5451 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 5452 verifyFormat("delete[] h->p;"); 5453 } 5454 5455 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 5456 verifyFormat("int *f(int *a) {}"); 5457 verifyFormat("int main(int argc, char **argv) {}"); 5458 verifyFormat("Test::Test(int b) : a(b * b) {}"); 5459 verifyIndependentOfContext("f(a, *a);"); 5460 verifyFormat("void g() { f(*a); }"); 5461 verifyIndependentOfContext("int a = b * 10;"); 5462 verifyIndependentOfContext("int a = 10 * b;"); 5463 verifyIndependentOfContext("int a = b * c;"); 5464 verifyIndependentOfContext("int a += b * c;"); 5465 verifyIndependentOfContext("int a -= b * c;"); 5466 verifyIndependentOfContext("int a *= b * c;"); 5467 verifyIndependentOfContext("int a /= b * c;"); 5468 verifyIndependentOfContext("int a = *b;"); 5469 verifyIndependentOfContext("int a = *b * c;"); 5470 verifyIndependentOfContext("int a = b * *c;"); 5471 verifyIndependentOfContext("int a = b * (10);"); 5472 verifyIndependentOfContext("S << b * (10);"); 5473 verifyIndependentOfContext("return 10 * b;"); 5474 verifyIndependentOfContext("return *b * *c;"); 5475 verifyIndependentOfContext("return a & ~b;"); 5476 verifyIndependentOfContext("f(b ? *c : *d);"); 5477 verifyIndependentOfContext("int a = b ? *c : *d;"); 5478 verifyIndependentOfContext("*b = a;"); 5479 verifyIndependentOfContext("a * ~b;"); 5480 verifyIndependentOfContext("a * !b;"); 5481 verifyIndependentOfContext("a * +b;"); 5482 verifyIndependentOfContext("a * -b;"); 5483 verifyIndependentOfContext("a * ++b;"); 5484 verifyIndependentOfContext("a * --b;"); 5485 verifyIndependentOfContext("a[4] * b;"); 5486 verifyIndependentOfContext("a[a * a] = 1;"); 5487 verifyIndependentOfContext("f() * b;"); 5488 verifyIndependentOfContext("a * [self dostuff];"); 5489 verifyIndependentOfContext("int x = a * (a + b);"); 5490 verifyIndependentOfContext("(a *)(a + b);"); 5491 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;"); 5492 verifyIndependentOfContext("int *pa = (int *)&a;"); 5493 verifyIndependentOfContext("return sizeof(int **);"); 5494 verifyIndependentOfContext("return sizeof(int ******);"); 5495 verifyIndependentOfContext("return (int **&)a;"); 5496 verifyIndependentOfContext("f((*PointerToArray)[10]);"); 5497 verifyFormat("void f(Type (*parameter)[10]) {}"); 5498 verifyGoogleFormat("return sizeof(int**);"); 5499 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); 5500 verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); 5501 verifyFormat("auto a = [](int **&, int ***) {};"); 5502 verifyFormat("auto PointerBinding = [](const char *S) {};"); 5503 verifyFormat("typedef typeof(int(int, int)) *MyFunc;"); 5504 verifyFormat("[](const decltype(*a) &value) {}"); 5505 verifyFormat("#define MACRO() [](A *a) { return 1; }"); 5506 verifyIndependentOfContext("typedef void (*f)(int *a);"); 5507 verifyIndependentOfContext("int i{a * b};"); 5508 verifyIndependentOfContext("aaa && aaa->f();"); 5509 verifyIndependentOfContext("int x = ~*p;"); 5510 verifyFormat("Constructor() : a(a), area(width * height) {}"); 5511 verifyFormat("Constructor() : a(a), area(a, width * height) {}"); 5512 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}"); 5513 verifyFormat("void f() { f(a, c * d); }"); 5514 verifyFormat("void f() { f(new a(), c * d); }"); 5515 5516 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 5517 5518 verifyIndependentOfContext("A<int *> a;"); 5519 verifyIndependentOfContext("A<int **> a;"); 5520 verifyIndependentOfContext("A<int *, int *> a;"); 5521 verifyIndependentOfContext("A<int *[]> a;"); 5522 verifyIndependentOfContext( 5523 "const char *const p = reinterpret_cast<const char *const>(q);"); 5524 verifyIndependentOfContext("A<int **, int **> a;"); 5525 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); 5526 verifyFormat("for (char **a = b; *a; ++a) {\n}"); 5527 verifyFormat("for (; a && b;) {\n}"); 5528 verifyFormat("bool foo = true && [] { return false; }();"); 5529 5530 verifyFormat( 5531 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5532 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5533 5534 verifyGoogleFormat("**outparam = 1;"); 5535 verifyGoogleFormat("*outparam = a * b;"); 5536 verifyGoogleFormat("int main(int argc, char** argv) {}"); 5537 verifyGoogleFormat("A<int*> a;"); 5538 verifyGoogleFormat("A<int**> a;"); 5539 verifyGoogleFormat("A<int*, int*> a;"); 5540 verifyGoogleFormat("A<int**, int**> a;"); 5541 verifyGoogleFormat("f(b ? *c : *d);"); 5542 verifyGoogleFormat("int a = b ? *c : *d;"); 5543 verifyGoogleFormat("Type* t = **x;"); 5544 verifyGoogleFormat("Type* t = *++*x;"); 5545 verifyGoogleFormat("*++*x;"); 5546 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 5547 verifyGoogleFormat("Type* t = x++ * y;"); 5548 verifyGoogleFormat( 5549 "const char* const p = reinterpret_cast<const char* const>(q);"); 5550 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);"); 5551 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);"); 5552 verifyGoogleFormat("template <typename T>\n" 5553 "void f(int i = 0, SomeType** temps = NULL);"); 5554 5555 FormatStyle Left = getLLVMStyle(); 5556 Left.PointerAlignment = FormatStyle::PAS_Left; 5557 verifyFormat("x = *a(x) = *a(y);", Left); 5558 5559 verifyIndependentOfContext("a = *(x + y);"); 5560 verifyIndependentOfContext("a = &(x + y);"); 5561 verifyIndependentOfContext("*(x + y).call();"); 5562 verifyIndependentOfContext("&(x + y)->call();"); 5563 verifyFormat("void f() { &(*I).first; }"); 5564 5565 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 5566 verifyFormat( 5567 "int *MyValues = {\n" 5568 " *A, // Operator detection might be confused by the '{'\n" 5569 " *BB // Operator detection might be confused by previous comment\n" 5570 "};"); 5571 5572 verifyIndependentOfContext("if (int *a = &b)"); 5573 verifyIndependentOfContext("if (int &a = *b)"); 5574 verifyIndependentOfContext("if (a & b[i])"); 5575 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 5576 verifyIndependentOfContext("if (*b[i])"); 5577 verifyIndependentOfContext("if (int *a = (&b))"); 5578 verifyIndependentOfContext("while (int *a = &b)"); 5579 verifyIndependentOfContext("size = sizeof *a;"); 5580 verifyIndependentOfContext("if (a && (b = c))"); 5581 verifyFormat("void f() {\n" 5582 " for (const int &v : Values) {\n" 5583 " }\n" 5584 "}"); 5585 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); 5586 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); 5587 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}"); 5588 5589 verifyFormat("#define A (!a * b)"); 5590 verifyFormat("#define MACRO \\\n" 5591 " int *i = a * b; \\\n" 5592 " void f(a *b);", 5593 getLLVMStyleWithColumns(19)); 5594 5595 verifyIndependentOfContext("A = new SomeType *[Length];"); 5596 verifyIndependentOfContext("A = new SomeType *[Length]();"); 5597 verifyIndependentOfContext("T **t = new T *;"); 5598 verifyIndependentOfContext("T **t = new T *();"); 5599 verifyGoogleFormat("A = new SomeType*[Length]();"); 5600 verifyGoogleFormat("A = new SomeType*[Length];"); 5601 verifyGoogleFormat("T** t = new T*;"); 5602 verifyGoogleFormat("T** t = new T*();"); 5603 5604 FormatStyle PointerLeft = getLLVMStyle(); 5605 PointerLeft.PointerAlignment = FormatStyle::PAS_Left; 5606 verifyFormat("delete *x;", PointerLeft); 5607 verifyFormat("STATIC_ASSERT((a & b) == 0);"); 5608 verifyFormat("STATIC_ASSERT(0 == (a & b));"); 5609 verifyFormat("template <bool a, bool b> " 5610 "typename t::if<x && y>::type f() {}"); 5611 verifyFormat("template <int *y> f() {}"); 5612 verifyFormat("vector<int *> v;"); 5613 verifyFormat("vector<int *const> v;"); 5614 verifyFormat("vector<int *const **const *> v;"); 5615 verifyFormat("vector<int *volatile> v;"); 5616 verifyFormat("vector<a * b> v;"); 5617 verifyFormat("foo<b && false>();"); 5618 verifyFormat("foo<b & 1>();"); 5619 verifyFormat("decltype(*::std::declval<const T &>()) void F();"); 5620 verifyFormat( 5621 "template <class T, class = typename std::enable_if<\n" 5622 " std::is_integral<T>::value &&\n" 5623 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n" 5624 "void F();", 5625 getLLVMStyleWithColumns(76)); 5626 verifyFormat( 5627 "template <class T,\n" 5628 " class = typename ::std::enable_if<\n" 5629 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n" 5630 "void F();", 5631 getGoogleStyleWithColumns(68)); 5632 5633 verifyIndependentOfContext("MACRO(int *i);"); 5634 verifyIndependentOfContext("MACRO(auto *a);"); 5635 verifyIndependentOfContext("MACRO(const A *a);"); 5636 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');"); 5637 // FIXME: Is there a way to make this work? 5638 // verifyIndependentOfContext("MACRO(A *a);"); 5639 5640 verifyFormat("DatumHandle const *operator->() const { return input_; }"); 5641 5642 EXPECT_EQ("#define OP(x) \\\n" 5643 " ostream &operator<<(ostream &s, const A &a) { \\\n" 5644 " return s << a.DebugString(); \\\n" 5645 " }", 5646 format("#define OP(x) \\\n" 5647 " ostream &operator<<(ostream &s, const A &a) { \\\n" 5648 " return s << a.DebugString(); \\\n" 5649 " }", 5650 getLLVMStyleWithColumns(50))); 5651 5652 // FIXME: We cannot handle this case yet; we might be able to figure out that 5653 // foo<x> d > v; doesn't make sense. 5654 verifyFormat("foo<a<b && c> d> v;"); 5655 5656 FormatStyle PointerMiddle = getLLVMStyle(); 5657 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 5658 verifyFormat("delete *x;", PointerMiddle); 5659 verifyFormat("int * x;", PointerMiddle); 5660 verifyFormat("template <int * y> f() {}", PointerMiddle); 5661 verifyFormat("int * f(int * a) {}", PointerMiddle); 5662 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle); 5663 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle); 5664 verifyFormat("A<int *> a;", PointerMiddle); 5665 verifyFormat("A<int **> a;", PointerMiddle); 5666 verifyFormat("A<int *, int *> a;", PointerMiddle); 5667 verifyFormat("A<int * []> a;", PointerMiddle); 5668 verifyFormat("A = new SomeType *[Length]();", PointerMiddle); 5669 verifyFormat("A = new SomeType *[Length];", PointerMiddle); 5670 verifyFormat("T ** t = new T *;", PointerMiddle); 5671 } 5672 5673 TEST_F(FormatTest, UnderstandsAttributes) { 5674 verifyFormat("SomeType s __attribute__((unused)) (InitValue);"); 5675 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n" 5676 "aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 5677 } 5678 5679 TEST_F(FormatTest, UnderstandsEllipsis) { 5680 verifyFormat("int printf(const char *fmt, ...);"); 5681 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); 5682 verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}"); 5683 5684 FormatStyle PointersLeft = getLLVMStyle(); 5685 PointersLeft.PointerAlignment = FormatStyle::PAS_Left; 5686 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft); 5687 } 5688 5689 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { 5690 EXPECT_EQ("int *a;\n" 5691 "int *a;\n" 5692 "int *a;", 5693 format("int *a;\n" 5694 "int* a;\n" 5695 "int *a;", 5696 getGoogleStyle())); 5697 EXPECT_EQ("int* a;\n" 5698 "int* a;\n" 5699 "int* a;", 5700 format("int* a;\n" 5701 "int* a;\n" 5702 "int *a;", 5703 getGoogleStyle())); 5704 EXPECT_EQ("int *a;\n" 5705 "int *a;\n" 5706 "int *a;", 5707 format("int *a;\n" 5708 "int * a;\n" 5709 "int * a;", 5710 getGoogleStyle())); 5711 } 5712 5713 TEST_F(FormatTest, UnderstandsRvalueReferences) { 5714 verifyFormat("int f(int &&a) {}"); 5715 verifyFormat("int f(int a, char &&b) {}"); 5716 verifyFormat("void f() { int &&a = b; }"); 5717 verifyGoogleFormat("int f(int a, char&& b) {}"); 5718 verifyGoogleFormat("void f() { int&& a = b; }"); 5719 5720 verifyIndependentOfContext("A<int &&> a;"); 5721 verifyIndependentOfContext("A<int &&, int &&> a;"); 5722 verifyGoogleFormat("A<int&&> a;"); 5723 verifyGoogleFormat("A<int&&, int&&> a;"); 5724 5725 // Not rvalue references: 5726 verifyFormat("template <bool B, bool C> class A {\n" 5727 " static_assert(B && C, \"Something is wrong\");\n" 5728 "};"); 5729 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))"); 5730 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))"); 5731 verifyFormat("#define A(a, b) (a && b)"); 5732 } 5733 5734 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 5735 verifyFormat("void f() {\n" 5736 " x[aaaaaaaaa -\n" 5737 " b] = 23;\n" 5738 "}", 5739 getLLVMStyleWithColumns(15)); 5740 } 5741 5742 TEST_F(FormatTest, FormatsCasts) { 5743 verifyFormat("Type *A = static_cast<Type *>(P);"); 5744 verifyFormat("Type *A = (Type *)P;"); 5745 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 5746 verifyFormat("int a = (int)(2.0f);"); 5747 verifyFormat("int a = (int)2.0f;"); 5748 verifyFormat("x[(int32)y];"); 5749 verifyFormat("x = (int32)y;"); 5750 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)"); 5751 verifyFormat("int a = (int)*b;"); 5752 verifyFormat("int a = (int)2.0f;"); 5753 verifyFormat("int a = (int)~0;"); 5754 verifyFormat("int a = (int)++a;"); 5755 verifyFormat("int a = (int)sizeof(int);"); 5756 verifyFormat("int a = (int)+2;"); 5757 verifyFormat("my_int a = (my_int)2.0f;"); 5758 verifyFormat("my_int a = (my_int)sizeof(int);"); 5759 verifyFormat("return (my_int)aaa;"); 5760 verifyFormat("#define x ((int)-1)"); 5761 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1"); 5762 verifyFormat("#define p(q) ((int *)&q)"); 5763 verifyFormat("fn(a)(b) + 1;"); 5764 5765 verifyFormat("void f() { my_int a = (my_int)*b; }"); 5766 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }"); 5767 verifyFormat("my_int a = (my_int)~0;"); 5768 verifyFormat("my_int a = (my_int)++a;"); 5769 verifyFormat("my_int a = (my_int)-2;"); 5770 verifyFormat("my_int a = (my_int)1;"); 5771 verifyFormat("my_int a = (my_int *)1;"); 5772 verifyFormat("my_int a = (const my_int)-1;"); 5773 verifyFormat("my_int a = (const my_int *)-1;"); 5774 verifyFormat("my_int a = (my_int)(my_int)-1;"); 5775 verifyFormat("my_int a = (ns::my_int)-2;"); 5776 5777 // FIXME: single value wrapped with paren will be treated as cast. 5778 verifyFormat("void f(int i = (kValue)*kMask) {}"); 5779 5780 verifyFormat("{ (void)F; }"); 5781 5782 // Don't break after a cast's 5783 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5784 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n" 5785 " bbbbbbbbbbbbbbbbbbbbbb);"); 5786 5787 // These are not casts. 5788 verifyFormat("void f(int *) {}"); 5789 verifyFormat("f(foo)->b;"); 5790 verifyFormat("f(foo).b;"); 5791 verifyFormat("f(foo)(b);"); 5792 verifyFormat("f(foo)[b];"); 5793 verifyFormat("[](foo) { return 4; }(bar);"); 5794 verifyFormat("(*funptr)(foo)[4];"); 5795 verifyFormat("funptrs[4](foo)[4];"); 5796 verifyFormat("void f(int *);"); 5797 verifyFormat("void f(int *) = 0;"); 5798 verifyFormat("void f(SmallVector<int>) {}"); 5799 verifyFormat("void f(SmallVector<int>);"); 5800 verifyFormat("void f(SmallVector<int>) = 0;"); 5801 verifyFormat("void f(int i = (kA * kB) & kMask) {}"); 5802 verifyFormat("int a = sizeof(int) * b;"); 5803 verifyFormat("int a = alignof(int) * b;", getGoogleStyle()); 5804 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;"); 5805 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");"); 5806 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;"); 5807 5808 // These are not casts, but at some point were confused with casts. 5809 verifyFormat("virtual void foo(int *) override;"); 5810 verifyFormat("virtual void foo(char &) const;"); 5811 verifyFormat("virtual void foo(int *a, char *) const;"); 5812 verifyFormat("int a = sizeof(int *) + b;"); 5813 verifyFormat("int a = alignof(int *) + b;", getGoogleStyle()); 5814 5815 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n" 5816 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 5817 // FIXME: The indentation here is not ideal. 5818 verifyFormat( 5819 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5820 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n" 5821 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];"); 5822 } 5823 5824 TEST_F(FormatTest, FormatsFunctionTypes) { 5825 verifyFormat("A<bool()> a;"); 5826 verifyFormat("A<SomeType()> a;"); 5827 verifyFormat("A<void (*)(int, std::string)> a;"); 5828 verifyFormat("A<void *(int)>;"); 5829 verifyFormat("void *(*a)(int *, SomeType *);"); 5830 verifyFormat("int (*func)(void *);"); 5831 verifyFormat("void f() { int (*func)(void *); }"); 5832 verifyFormat("template <class CallbackClass>\n" 5833 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);"); 5834 5835 verifyGoogleFormat("A<void*(int*, SomeType*)>;"); 5836 verifyGoogleFormat("void* (*a)(int);"); 5837 verifyGoogleFormat( 5838 "template <class CallbackClass>\n" 5839 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);"); 5840 5841 // Other constructs can look somewhat like function types: 5842 verifyFormat("A<sizeof(*x)> a;"); 5843 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)"); 5844 verifyFormat("some_var = function(*some_pointer_var)[0];"); 5845 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }"); 5846 } 5847 5848 TEST_F(FormatTest, FormatsPointersToArrayTypes) { 5849 verifyFormat("A (*foo_)[6];"); 5850 verifyFormat("vector<int> (*foo_)[6];"); 5851 } 5852 5853 TEST_F(FormatTest, BreaksLongVariableDeclarations) { 5854 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 5855 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 5856 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n" 5857 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 5858 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 5859 " *LoooooooooooooooooooooooooooooooooooooooongVariable;"); 5860 5861 // Different ways of ()-initializiation. 5862 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 5863 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);"); 5864 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 5865 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);"); 5866 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 5867 " LoooooooooooooooooooooooooooooooooooooooongVariable({});"); 5868 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 5869 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);"); 5870 } 5871 5872 TEST_F(FormatTest, BreaksLongDeclarations) { 5873 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n" 5874 " AnotherNameForTheLongType;"); 5875 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n" 5876 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5877 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 5878 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 5879 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n" 5880 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 5881 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 5882 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 5883 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n" 5884 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 5885 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 5886 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 5887 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 5888 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 5889 FormatStyle Indented = getLLVMStyle(); 5890 Indented.IndentWrappedFunctionNames = true; 5891 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 5892 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();", 5893 Indented); 5894 verifyFormat( 5895 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 5896 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 5897 Indented); 5898 verifyFormat( 5899 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 5900 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 5901 Indented); 5902 verifyFormat( 5903 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 5904 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 5905 Indented); 5906 5907 // FIXME: Without the comment, this breaks after "(". 5908 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n" 5909 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();", 5910 getGoogleStyle()); 5911 5912 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 5913 " int LoooooooooooooooooooongParam2) {}"); 5914 verifyFormat( 5915 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" 5916 " SourceLocation L, IdentifierIn *II,\n" 5917 " Type *T) {}"); 5918 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" 5919 "ReallyReaaallyLongFunctionName(\n" 5920 " const std::string &SomeParameter,\n" 5921 " const SomeType<string, SomeOtherTemplateParameter>\n" 5922 " &ReallyReallyLongParameterName,\n" 5923 " const SomeType<string, SomeOtherTemplateParameter>\n" 5924 " &AnotherLongParameterName) {}"); 5925 verifyFormat("template <typename A>\n" 5926 "SomeLoooooooooooooooooooooongType<\n" 5927 " typename some_namespace::SomeOtherType<A>::Type>\n" 5928 "Function() {}"); 5929 5930 verifyGoogleFormat( 5931 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" 5932 " aaaaaaaaaaaaaaaaaaaaaaa;"); 5933 verifyGoogleFormat( 5934 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" 5935 " SourceLocation L) {}"); 5936 verifyGoogleFormat( 5937 "some_namespace::LongReturnType\n" 5938 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" 5939 " int first_long_parameter, int second_parameter) {}"); 5940 5941 verifyGoogleFormat("template <typename T>\n" 5942 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" 5943 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); 5944 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5945 " int aaaaaaaaaaaaaaaaaaaaaaa);"); 5946 5947 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 5948 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5949 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5950 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5951 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 5952 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 5953 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5954 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 5955 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n" 5956 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5957 } 5958 5959 TEST_F(FormatTest, FormatsArrays) { 5960 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 5961 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;"); 5962 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5963 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 5964 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5965 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;"); 5966 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5967 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 5968 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 5969 verifyFormat( 5970 "llvm::outs() << \"aaaaaaaaaaaa: \"\n" 5971 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 5972 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 5973 5974 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n" 5975 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];"); 5976 verifyFormat( 5977 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n" 5978 " .aaaaaaa[0]\n" 5979 " .aaaaaaaaaaaaaaaaaaaaaa();"); 5980 5981 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10)); 5982 } 5983 5984 TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 5985 verifyFormat("(a)->b();"); 5986 verifyFormat("--a;"); 5987 } 5988 5989 TEST_F(FormatTest, HandlesIncludeDirectives) { 5990 verifyFormat("#include <string>\n" 5991 "#include <a/b/c.h>\n" 5992 "#include \"a/b/string\"\n" 5993 "#include \"string.h\"\n" 5994 "#include \"string.h\"\n" 5995 "#include <a-a>\n" 5996 "#include < path with space >\n" 5997 "#include_next <test.h>" 5998 "#include \"abc.h\" // this is included for ABC\n" 5999 "#include \"some long include\" // with a comment\n" 6000 "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"", 6001 getLLVMStyleWithColumns(35)); 6002 EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\"")); 6003 EXPECT_EQ("#include <a>", format("#include<a>")); 6004 6005 verifyFormat("#import <string>"); 6006 verifyFormat("#import <a/b/c.h>"); 6007 verifyFormat("#import \"a/b/string\""); 6008 verifyFormat("#import \"string.h\""); 6009 verifyFormat("#import \"string.h\""); 6010 verifyFormat("#if __has_include(<strstream>)\n" 6011 "#include <strstream>\n" 6012 "#endif"); 6013 6014 verifyFormat("#define MY_IMPORT <a/b>"); 6015 6016 // Protocol buffer definition or missing "#". 6017 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";", 6018 getLLVMStyleWithColumns(30)); 6019 6020 FormatStyle Style = getLLVMStyle(); 6021 Style.AlwaysBreakBeforeMultilineStrings = true; 6022 Style.ColumnLimit = 0; 6023 verifyFormat("#import \"abc.h\"", Style); 6024 6025 // But 'import' might also be a regular C++ namespace. 6026 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6027 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6028 } 6029 6030 //===----------------------------------------------------------------------===// 6031 // Error recovery tests. 6032 //===----------------------------------------------------------------------===// 6033 6034 TEST_F(FormatTest, IncompleteParameterLists) { 6035 FormatStyle NoBinPacking = getLLVMStyle(); 6036 NoBinPacking.BinPackParameters = false; 6037 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" 6038 " double *min_x,\n" 6039 " double *max_x,\n" 6040 " double *min_y,\n" 6041 " double *max_y,\n" 6042 " double *min_z,\n" 6043 " double *max_z, ) {}", 6044 NoBinPacking); 6045 } 6046 6047 TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 6048 verifyFormat("void f() { return; }\n42"); 6049 verifyFormat("void f() {\n" 6050 " if (0)\n" 6051 " return;\n" 6052 "}\n" 6053 "42"); 6054 verifyFormat("void f() { return }\n42"); 6055 verifyFormat("void f() {\n" 6056 " if (0)\n" 6057 " return\n" 6058 "}\n" 6059 "42"); 6060 } 6061 6062 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 6063 EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); 6064 EXPECT_EQ("void f() {\n" 6065 " if (a)\n" 6066 " return\n" 6067 "}", 6068 format("void f ( ) { if ( a ) return }")); 6069 EXPECT_EQ("namespace N {\n" 6070 "void f()\n" 6071 "}", 6072 format("namespace N { void f() }")); 6073 EXPECT_EQ("namespace N {\n" 6074 "void f() {}\n" 6075 "void g()\n" 6076 "}", 6077 format("namespace N { void f( ) { } void g( ) }")); 6078 } 6079 6080 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 6081 verifyFormat("int aaaaaaaa =\n" 6082 " // Overlylongcomment\n" 6083 " b;", 6084 getLLVMStyleWithColumns(20)); 6085 verifyFormat("function(\n" 6086 " ShortArgument,\n" 6087 " LoooooooooooongArgument);\n", 6088 getLLVMStyleWithColumns(20)); 6089 } 6090 6091 TEST_F(FormatTest, IncorrectAccessSpecifier) { 6092 verifyFormat("public:"); 6093 verifyFormat("class A {\n" 6094 "public\n" 6095 " void f() {}\n" 6096 "};"); 6097 verifyFormat("public\n" 6098 "int qwerty;"); 6099 verifyFormat("public\n" 6100 "B {}"); 6101 verifyFormat("public\n" 6102 "{}"); 6103 verifyFormat("public\n" 6104 "B { int x; }"); 6105 } 6106 6107 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 6108 verifyFormat("{"); 6109 verifyFormat("#})"); 6110 verifyNoCrash("(/**/[:!] ?[)."); 6111 } 6112 6113 TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 6114 verifyFormat("do {\n}"); 6115 verifyFormat("do {\n}\n" 6116 "f();"); 6117 verifyFormat("do {\n}\n" 6118 "wheeee(fun);"); 6119 verifyFormat("do {\n" 6120 " f();\n" 6121 "}"); 6122 } 6123 6124 TEST_F(FormatTest, IncorrectCodeMissingParens) { 6125 verifyFormat("if {\n foo;\n foo();\n}"); 6126 verifyFormat("switch {\n foo;\n foo();\n}"); 6127 verifyIncompleteFormat("for {\n foo;\n foo();\n}"); 6128 verifyFormat("while {\n foo;\n foo();\n}"); 6129 verifyFormat("do {\n foo;\n foo();\n} while;"); 6130 } 6131 6132 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 6133 verifyIncompleteFormat("namespace {\n" 6134 "class Foo { Foo (\n" 6135 "};\n" 6136 "} // comment"); 6137 } 6138 6139 TEST_F(FormatTest, IncorrectCodeErrorDetection) { 6140 EXPECT_EQ("{\n {}\n", format("{\n{\n}\n")); 6141 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 6142 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 6143 EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n")); 6144 6145 EXPECT_EQ("{\n" 6146 " {\n" 6147 " breakme(\n" 6148 " qwe);\n" 6149 " }\n", 6150 format("{\n" 6151 " {\n" 6152 " breakme(qwe);\n" 6153 "}\n", 6154 getLLVMStyleWithColumns(10))); 6155 } 6156 6157 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 6158 verifyFormat("int x = {\n" 6159 " avariable,\n" 6160 " b(alongervariable)};", 6161 getLLVMStyleWithColumns(25)); 6162 } 6163 6164 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 6165 verifyFormat("return (a)(b){1, 2, 3};"); 6166 } 6167 6168 TEST_F(FormatTest, LayoutCxx11BraceInitializers) { 6169 verifyFormat("vector<int> x{1, 2, 3, 4};"); 6170 verifyFormat("vector<int> x{\n" 6171 " 1, 2, 3, 4,\n" 6172 "};"); 6173 verifyFormat("vector<T> x{{}, {}, {}, {}};"); 6174 verifyFormat("f({1, 2});"); 6175 verifyFormat("auto v = Foo{-1};"); 6176 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});"); 6177 verifyFormat("Class::Class : member{1, 2, 3} {}"); 6178 verifyFormat("new vector<int>{1, 2, 3};"); 6179 verifyFormat("new int[3]{1, 2, 3};"); 6180 verifyFormat("new int{1};"); 6181 verifyFormat("return {arg1, arg2};"); 6182 verifyFormat("return {arg1, SomeType{parameter}};"); 6183 verifyFormat("int count = set<int>{f(), g(), h()}.size();"); 6184 verifyFormat("new T{arg1, arg2};"); 6185 verifyFormat("f(MyMap[{composite, key}]);"); 6186 verifyFormat("class Class {\n" 6187 " T member = {arg1, arg2};\n" 6188 "};"); 6189 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};"); 6190 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");"); 6191 verifyFormat("int a = std::is_integral<int>{} + 0;"); 6192 6193 verifyFormat("int foo(int i) { return fo1{}(i); }"); 6194 verifyFormat("int foo(int i) { return fo1{}(i); }"); 6195 verifyFormat("auto i = decltype(x){};"); 6196 verifyFormat("std::vector<int> v = {1, 0 /* comment */};"); 6197 verifyFormat("Node n{1, Node{1000}, //\n" 6198 " 2};"); 6199 verifyFormat("Aaaa aaaaaaa{\n" 6200 " {\n" 6201 " aaaa,\n" 6202 " },\n" 6203 "};"); 6204 verifyFormat("class C : public D {\n" 6205 " SomeClass SC{2};\n" 6206 "};"); 6207 verifyFormat("class C : public A {\n" 6208 " class D : public B {\n" 6209 " void f() { int i{2}; }\n" 6210 " };\n" 6211 "};"); 6212 6213 // In combination with BinPackParameters = false. 6214 FormatStyle NoBinPacking = getLLVMStyle(); 6215 NoBinPacking.BinPackParameters = false; 6216 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n" 6217 " bbbbb,\n" 6218 " ccccc,\n" 6219 " ddddd,\n" 6220 " eeeee,\n" 6221 " ffffff,\n" 6222 " ggggg,\n" 6223 " hhhhhh,\n" 6224 " iiiiii,\n" 6225 " jjjjjj,\n" 6226 " kkkkkk};", 6227 NoBinPacking); 6228 verifyFormat("const Aaaaaa aaaaa = {\n" 6229 " aaaaa,\n" 6230 " bbbbb,\n" 6231 " ccccc,\n" 6232 " ddddd,\n" 6233 " eeeee,\n" 6234 " ffffff,\n" 6235 " ggggg,\n" 6236 " hhhhhh,\n" 6237 " iiiiii,\n" 6238 " jjjjjj,\n" 6239 " kkkkkk,\n" 6240 "};", 6241 NoBinPacking); 6242 verifyFormat( 6243 "const Aaaaaa aaaaa = {\n" 6244 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n" 6245 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n" 6246 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n" 6247 "};", 6248 NoBinPacking); 6249 6250 // FIXME: The alignment of these trailing comments might be bad. Then again, 6251 // this might be utterly useless in real code. 6252 verifyFormat("Constructor::Constructor()\n" 6253 " : some_value{ //\n" 6254 " aaaaaaa, //\n" 6255 " bbbbbbb} {}"); 6256 6257 // In braced lists, the first comment is always assumed to belong to the 6258 // first element. Thus, it can be moved to the next or previous line as 6259 // appropriate. 6260 EXPECT_EQ("function({// First element:\n" 6261 " 1,\n" 6262 " // Second element:\n" 6263 " 2});", 6264 format("function({\n" 6265 " // First element:\n" 6266 " 1,\n" 6267 " // Second element:\n" 6268 " 2});")); 6269 EXPECT_EQ("std::vector<int> MyNumbers{\n" 6270 " // First element:\n" 6271 " 1,\n" 6272 " // Second element:\n" 6273 " 2};", 6274 format("std::vector<int> MyNumbers{// First element:\n" 6275 " 1,\n" 6276 " // Second element:\n" 6277 " 2};", 6278 getLLVMStyleWithColumns(30))); 6279 // A trailing comma should still lead to an enforced line break. 6280 EXPECT_EQ("vector<int> SomeVector = {\n" 6281 " // aaa\n" 6282 " 1, 2,\n" 6283 "};", 6284 format("vector<int> SomeVector = { // aaa\n" 6285 " 1, 2, };")); 6286 6287 FormatStyle ExtraSpaces = getLLVMStyle(); 6288 ExtraSpaces.Cpp11BracedListStyle = false; 6289 ExtraSpaces.ColumnLimit = 75; 6290 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces); 6291 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces); 6292 verifyFormat("f({ 1, 2 });", ExtraSpaces); 6293 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces); 6294 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces); 6295 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces); 6296 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces); 6297 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces); 6298 verifyFormat("return { arg1, arg2 };", ExtraSpaces); 6299 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces); 6300 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces); 6301 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces); 6302 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces); 6303 verifyFormat("class Class {\n" 6304 " T member = { arg1, arg2 };\n" 6305 "};", 6306 ExtraSpaces); 6307 verifyFormat( 6308 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6309 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n" 6310 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 6311 " bbbbbbbbbbbbbbbbbbbb, bbbbb };", 6312 ExtraSpaces); 6313 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces); 6314 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });", 6315 ExtraSpaces); 6316 verifyFormat( 6317 "someFunction(OtherParam,\n" 6318 " BracedList{ // comment 1 (Forcing interesting break)\n" 6319 " param1, param2,\n" 6320 " // comment 2\n" 6321 " param3, param4 });", 6322 ExtraSpaces); 6323 verifyFormat( 6324 "std::this_thread::sleep_for(\n" 6325 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);", 6326 ExtraSpaces); 6327 verifyFormat( 6328 "std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{\n" 6329 " aaaaaaa,\n" 6330 " aaaaaaaaaa,\n" 6331 " aaaaa,\n" 6332 " aaaaaaaaaaaaaaa,\n" 6333 " aaa,\n" 6334 " aaaaaaaaaa,\n" 6335 " a,\n" 6336 " aaaaaaaaaaaaaaaaaaaaa,\n" 6337 " aaaaaaaaaaaa,\n" 6338 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n" 6339 " aaaaaaa,\n" 6340 " a};"); 6341 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces); 6342 } 6343 6344 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { 6345 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6346 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6347 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6348 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6349 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6350 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 6351 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n" 6352 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6353 " 1, 22, 333, 4444, 55555, //\n" 6354 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6355 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 6356 verifyFormat( 6357 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6358 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6359 " 1, 22, 333, 4444, 55555, 666666, // comment\n" 6360 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6361 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6362 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6363 " 7777777};"); 6364 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6365 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6366 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6367 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6368 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6369 " // Separating comment.\n" 6370 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6371 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6372 " // Leading comment\n" 6373 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6374 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6375 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6376 " 1, 1, 1, 1};", 6377 getLLVMStyleWithColumns(39)); 6378 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6379 " 1, 1, 1, 1};", 6380 getLLVMStyleWithColumns(38)); 6381 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n" 6382 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};", 6383 getLLVMStyleWithColumns(43)); 6384 verifyFormat( 6385 "static unsigned SomeValues[10][3] = {\n" 6386 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n" 6387 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};"); 6388 verifyFormat("static auto fields = new vector<string>{\n" 6389 " \"aaaaaaaaaaaaa\",\n" 6390 " \"aaaaaaaaaaaaa\",\n" 6391 " \"aaaaaaaaaaaa\",\n" 6392 " \"aaaaaaaaaaaaaa\",\n" 6393 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 6394 " \"aaaaaaaaaaaa\",\n" 6395 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 6396 "};"); 6397 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};"); 6398 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n" 6399 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n" 6400 " 3, cccccccccccccccccccccc};", 6401 getLLVMStyleWithColumns(60)); 6402 6403 // Trailing commas. 6404 verifyFormat("vector<int> x = {\n" 6405 " 1, 1, 1, 1, 1, 1, 1, 1,\n" 6406 "};", 6407 getLLVMStyleWithColumns(39)); 6408 verifyFormat("vector<int> x = {\n" 6409 " 1, 1, 1, 1, 1, 1, 1, 1, //\n" 6410 "};", 6411 getLLVMStyleWithColumns(39)); 6412 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6413 " 1, 1, 1, 1,\n" 6414 " /**/ /**/};", 6415 getLLVMStyleWithColumns(39)); 6416 6417 // Trailing comment in the first line. 6418 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n" 6419 " 1111111111, 2222222222, 33333333333, 4444444444, //\n" 6420 " 111111111, 222222222, 3333333333, 444444444, //\n" 6421 " 11111111, 22222222, 333333333, 44444444};"); 6422 6423 // With nested lists, we should either format one item per line or all nested 6424 // lists one on line. 6425 // FIXME: For some nested lists, we can do better. 6426 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n" 6427 " {aaaaaaaaaaaaaaaaaaa},\n" 6428 " {aaaaaaaaaaaaaaaaaaaaa},\n" 6429 " {aaaaaaaaaaaaaaaaa}};", 6430 getLLVMStyleWithColumns(60)); 6431 verifyFormat( 6432 "SomeStruct my_struct_array = {\n" 6433 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n" 6434 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n" 6435 " {aaa, aaa},\n" 6436 " {aaa, aaa},\n" 6437 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n" 6438 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6439 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};"); 6440 6441 // No column layout should be used here. 6442 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n" 6443 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};"); 6444 6445 verifyNoCrash("a<,"); 6446 } 6447 6448 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 6449 FormatStyle DoNotMerge = getLLVMStyle(); 6450 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 6451 6452 verifyFormat("void f() { return 42; }"); 6453 verifyFormat("void f() {\n" 6454 " return 42;\n" 6455 "}", 6456 DoNotMerge); 6457 verifyFormat("void f() {\n" 6458 " // Comment\n" 6459 "}"); 6460 verifyFormat("{\n" 6461 "#error {\n" 6462 " int a;\n" 6463 "}"); 6464 verifyFormat("{\n" 6465 " int a;\n" 6466 "#error {\n" 6467 "}"); 6468 verifyFormat("void f() {} // comment"); 6469 verifyFormat("void f() { int a; } // comment"); 6470 verifyFormat("void f() {\n" 6471 "} // comment", 6472 DoNotMerge); 6473 verifyFormat("void f() {\n" 6474 " int a;\n" 6475 "} // comment", 6476 DoNotMerge); 6477 verifyFormat("void f() {\n" 6478 "} // comment", 6479 getLLVMStyleWithColumns(15)); 6480 6481 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 6482 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 6483 6484 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 6485 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 6486 verifyFormat("class C {\n" 6487 " C()\n" 6488 " : iiiiiiii(nullptr),\n" 6489 " kkkkkkk(nullptr),\n" 6490 " mmmmmmm(nullptr),\n" 6491 " nnnnnnn(nullptr) {}\n" 6492 "};", 6493 getGoogleStyle()); 6494 6495 FormatStyle NoColumnLimit = getLLVMStyle(); 6496 NoColumnLimit.ColumnLimit = 0; 6497 EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit)); 6498 EXPECT_EQ("class C {\n" 6499 " A() : b(0) {}\n" 6500 "};", 6501 format("class C{A():b(0){}};", NoColumnLimit)); 6502 EXPECT_EQ("A()\n" 6503 " : b(0) {\n" 6504 "}", 6505 format("A()\n:b(0)\n{\n}", NoColumnLimit)); 6506 6507 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit; 6508 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine = 6509 FormatStyle::SFS_None; 6510 EXPECT_EQ("A()\n" 6511 " : b(0) {\n" 6512 "}", 6513 format("A():b(0){}", DoNotMergeNoColumnLimit)); 6514 EXPECT_EQ("A()\n" 6515 " : b(0) {\n" 6516 "}", 6517 format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit)); 6518 6519 verifyFormat("#define A \\\n" 6520 " void f() { \\\n" 6521 " int i; \\\n" 6522 " }", 6523 getLLVMStyleWithColumns(20)); 6524 verifyFormat("#define A \\\n" 6525 " void f() { int i; }", 6526 getLLVMStyleWithColumns(21)); 6527 verifyFormat("#define A \\\n" 6528 " void f() { \\\n" 6529 " int i; \\\n" 6530 " } \\\n" 6531 " int j;", 6532 getLLVMStyleWithColumns(22)); 6533 verifyFormat("#define A \\\n" 6534 " void f() { int i; } \\\n" 6535 " int j;", 6536 getLLVMStyleWithColumns(23)); 6537 } 6538 6539 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) { 6540 FormatStyle MergeInlineOnly = getLLVMStyle(); 6541 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 6542 verifyFormat("class C {\n" 6543 " int f() { return 42; }\n" 6544 "};", 6545 MergeInlineOnly); 6546 verifyFormat("int f() {\n" 6547 " return 42;\n" 6548 "}", 6549 MergeInlineOnly); 6550 } 6551 6552 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 6553 // Elaborate type variable declarations. 6554 verifyFormat("struct foo a = {bar};\nint n;"); 6555 verifyFormat("class foo a = {bar};\nint n;"); 6556 verifyFormat("union foo a = {bar};\nint n;"); 6557 6558 // Elaborate types inside function definitions. 6559 verifyFormat("struct foo f() {}\nint n;"); 6560 verifyFormat("class foo f() {}\nint n;"); 6561 verifyFormat("union foo f() {}\nint n;"); 6562 6563 // Templates. 6564 verifyFormat("template <class X> void f() {}\nint n;"); 6565 verifyFormat("template <struct X> void f() {}\nint n;"); 6566 verifyFormat("template <union X> void f() {}\nint n;"); 6567 6568 // Actual definitions... 6569 verifyFormat("struct {\n} n;"); 6570 verifyFormat( 6571 "template <template <class T, class Y>, class Z> class X {\n} n;"); 6572 verifyFormat("union Z {\n int n;\n} x;"); 6573 verifyFormat("class MACRO Z {\n} n;"); 6574 verifyFormat("class MACRO(X) Z {\n} n;"); 6575 verifyFormat("class __attribute__(X) Z {\n} n;"); 6576 verifyFormat("class __declspec(X) Z {\n} n;"); 6577 verifyFormat("class A##B##C {\n} n;"); 6578 verifyFormat("class alignas(16) Z {\n} n;"); 6579 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;"); 6580 verifyFormat("class MACROA MACRO(X) Z {\n} n;"); 6581 6582 // Redefinition from nested context: 6583 verifyFormat("class A::B::C {\n} n;"); 6584 6585 // Template definitions. 6586 verifyFormat( 6587 "template <typename F>\n" 6588 "Matcher(const Matcher<F> &Other,\n" 6589 " typename enable_if_c<is_base_of<F, T>::value &&\n" 6590 " !is_same<F, T>::value>::type * = 0)\n" 6591 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}"); 6592 6593 // FIXME: This is still incorrectly handled at the formatter side. 6594 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};"); 6595 6596 // FIXME: 6597 // This now gets parsed incorrectly as class definition. 6598 // verifyFormat("class A<int> f() {\n}\nint n;"); 6599 6600 // Elaborate types where incorrectly parsing the structural element would 6601 // break the indent. 6602 verifyFormat("if (true)\n" 6603 " class X x;\n" 6604 "else\n" 6605 " f();\n"); 6606 6607 // This is simply incomplete. Formatting is not important, but must not crash. 6608 verifyFormat("class A:"); 6609 } 6610 6611 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 6612 EXPECT_EQ("#error Leave all white!!!!! space* alone!\n", 6613 format("#error Leave all white!!!!! space* alone!\n")); 6614 EXPECT_EQ( 6615 "#warning Leave all white!!!!! space* alone!\n", 6616 format("#warning Leave all white!!!!! space* alone!\n")); 6617 EXPECT_EQ("#error 1", format(" # error 1")); 6618 EXPECT_EQ("#warning 1", format(" # warning 1")); 6619 } 6620 6621 TEST_F(FormatTest, FormatHashIfExpressions) { 6622 verifyFormat("#if AAAA && BBBB"); 6623 // FIXME: Come up with a better indentation for #elif. 6624 verifyFormat( 6625 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" 6626 " defined(BBBBBBBB)\n" 6627 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" 6628 " defined(BBBBBBBB)\n" 6629 "#endif", 6630 getLLVMStyleWithColumns(65)); 6631 } 6632 6633 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 6634 FormatStyle AllowsMergedIf = getGoogleStyle(); 6635 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 6636 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 6637 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 6638 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 6639 EXPECT_EQ("if (true) return 42;", 6640 format("if (true)\nreturn 42;", AllowsMergedIf)); 6641 FormatStyle ShortMergedIf = AllowsMergedIf; 6642 ShortMergedIf.ColumnLimit = 25; 6643 verifyFormat("#define A \\\n" 6644 " if (true) return 42;", 6645 ShortMergedIf); 6646 verifyFormat("#define A \\\n" 6647 " f(); \\\n" 6648 " if (true)\n" 6649 "#define B", 6650 ShortMergedIf); 6651 verifyFormat("#define A \\\n" 6652 " f(); \\\n" 6653 " if (true)\n" 6654 "g();", 6655 ShortMergedIf); 6656 verifyFormat("{\n" 6657 "#ifdef A\n" 6658 " // Comment\n" 6659 " if (true) continue;\n" 6660 "#endif\n" 6661 " // Comment\n" 6662 " if (true) continue;\n" 6663 "}", 6664 ShortMergedIf); 6665 ShortMergedIf.ColumnLimit = 29; 6666 verifyFormat("#define A \\\n" 6667 " if (aaaaaaaaaa) return 1; \\\n" 6668 " return 2;", 6669 ShortMergedIf); 6670 ShortMergedIf.ColumnLimit = 28; 6671 verifyFormat("#define A \\\n" 6672 " if (aaaaaaaaaa) \\\n" 6673 " return 1; \\\n" 6674 " return 2;", 6675 ShortMergedIf); 6676 } 6677 6678 TEST_F(FormatTest, BlockCommentsInControlLoops) { 6679 verifyFormat("if (0) /* a comment in a strange place */ {\n" 6680 " f();\n" 6681 "}"); 6682 verifyFormat("if (0) /* a comment in a strange place */ {\n" 6683 " f();\n" 6684 "} /* another comment */ else /* comment #3 */ {\n" 6685 " g();\n" 6686 "}"); 6687 verifyFormat("while (0) /* a comment in a strange place */ {\n" 6688 " f();\n" 6689 "}"); 6690 verifyFormat("for (;;) /* a comment in a strange place */ {\n" 6691 " f();\n" 6692 "}"); 6693 verifyFormat("do /* a comment in a strange place */ {\n" 6694 " f();\n" 6695 "} /* another comment */ while (0);"); 6696 } 6697 6698 TEST_F(FormatTest, BlockComments) { 6699 EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */", 6700 format("/* *//* */ /* */\n/* *//* */ /* */")); 6701 EXPECT_EQ("/* */ a /* */ b;", format(" /* */ a/* */ b;")); 6702 EXPECT_EQ("#define A /*123*/ \\\n" 6703 " b\n" 6704 "/* */\n" 6705 "someCall(\n" 6706 " parameter);", 6707 format("#define A /*123*/ b\n" 6708 "/* */\n" 6709 "someCall(parameter);", 6710 getLLVMStyleWithColumns(15))); 6711 6712 EXPECT_EQ("#define A\n" 6713 "/* */ someCall(\n" 6714 " parameter);", 6715 format("#define A\n" 6716 "/* */someCall(parameter);", 6717 getLLVMStyleWithColumns(15))); 6718 EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/")); 6719 EXPECT_EQ("/*\n" 6720 "*\n" 6721 " * aaaaaa\n" 6722 " * aaaaaa\n" 6723 "*/", 6724 format("/*\n" 6725 "*\n" 6726 " * aaaaaa aaaaaa\n" 6727 "*/", 6728 getLLVMStyleWithColumns(10))); 6729 EXPECT_EQ("/*\n" 6730 "**\n" 6731 "* aaaaaa\n" 6732 "*aaaaaa\n" 6733 "*/", 6734 format("/*\n" 6735 "**\n" 6736 "* aaaaaa aaaaaa\n" 6737 "*/", 6738 getLLVMStyleWithColumns(10))); 6739 6740 FormatStyle NoBinPacking = getLLVMStyle(); 6741 NoBinPacking.BinPackParameters = false; 6742 EXPECT_EQ("someFunction(1, /* comment 1 */\n" 6743 " 2, /* comment 2 */\n" 6744 " 3, /* comment 3 */\n" 6745 " aaaa,\n" 6746 " bbbb);", 6747 format("someFunction (1, /* comment 1 */\n" 6748 " 2, /* comment 2 */ \n" 6749 " 3, /* comment 3 */\n" 6750 "aaaa, bbbb );", 6751 NoBinPacking)); 6752 verifyFormat( 6753 "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 6754 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 6755 EXPECT_EQ( 6756 "bool aaaaaaaaaaaaa = /* trailing comment */\n" 6757 " aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 6758 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;", 6759 format( 6760 "bool aaaaaaaaaaaaa = /* trailing comment */\n" 6761 " aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 6762 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;")); 6763 EXPECT_EQ( 6764 "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n" 6765 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n" 6766 "int cccccccccccccccccccccccccccccc; /* comment */\n", 6767 format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n" 6768 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n" 6769 "int cccccccccccccccccccccccccccccc; /* comment */\n")); 6770 6771 verifyFormat("void f(int * /* unused */) {}"); 6772 6773 EXPECT_EQ("/*\n" 6774 " **\n" 6775 " */", 6776 format("/*\n" 6777 " **\n" 6778 " */")); 6779 EXPECT_EQ("/*\n" 6780 " *q\n" 6781 " */", 6782 format("/*\n" 6783 " *q\n" 6784 " */")); 6785 EXPECT_EQ("/*\n" 6786 " * q\n" 6787 " */", 6788 format("/*\n" 6789 " * q\n" 6790 " */")); 6791 EXPECT_EQ("/*\n" 6792 " **/", 6793 format("/*\n" 6794 " **/")); 6795 EXPECT_EQ("/*\n" 6796 " ***/", 6797 format("/*\n" 6798 " ***/")); 6799 } 6800 6801 TEST_F(FormatTest, BlockCommentsInMacros) { 6802 EXPECT_EQ("#define A \\\n" 6803 " { \\\n" 6804 " /* one line */ \\\n" 6805 " someCall();", 6806 format("#define A { \\\n" 6807 " /* one line */ \\\n" 6808 " someCall();", 6809 getLLVMStyleWithColumns(20))); 6810 EXPECT_EQ("#define A \\\n" 6811 " { \\\n" 6812 " /* previous */ \\\n" 6813 " /* one line */ \\\n" 6814 " someCall();", 6815 format("#define A { \\\n" 6816 " /* previous */ \\\n" 6817 " /* one line */ \\\n" 6818 " someCall();", 6819 getLLVMStyleWithColumns(20))); 6820 } 6821 6822 TEST_F(FormatTest, BlockCommentsAtEndOfLine) { 6823 EXPECT_EQ("a = {\n" 6824 " 1111 /* */\n" 6825 "};", 6826 format("a = {1111 /* */\n" 6827 "};", 6828 getLLVMStyleWithColumns(15))); 6829 EXPECT_EQ("a = {\n" 6830 " 1111 /* */\n" 6831 "};", 6832 format("a = {1111 /* */\n" 6833 "};", 6834 getLLVMStyleWithColumns(15))); 6835 6836 // FIXME: The formatting is still wrong here. 6837 EXPECT_EQ("a = {\n" 6838 " 1111 /* a\n" 6839 " */\n" 6840 "};", 6841 format("a = {1111 /* a */\n" 6842 "};", 6843 getLLVMStyleWithColumns(15))); 6844 } 6845 6846 TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) { 6847 // FIXME: This is not what we want... 6848 verifyFormat("{\n" 6849 "// a" 6850 "// b"); 6851 } 6852 6853 TEST_F(FormatTest, FormatStarDependingOnContext) { 6854 verifyFormat("void f(int *a);"); 6855 verifyFormat("void f() { f(fint * b); }"); 6856 verifyFormat("class A {\n void f(int *a);\n};"); 6857 verifyFormat("class A {\n int *a;\n};"); 6858 verifyFormat("namespace a {\n" 6859 "namespace b {\n" 6860 "class A {\n" 6861 " void f() {}\n" 6862 " int *a;\n" 6863 "};\n" 6864 "}\n" 6865 "}"); 6866 } 6867 6868 TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 6869 verifyFormat("while"); 6870 verifyFormat("operator"); 6871 } 6872 6873 //===----------------------------------------------------------------------===// 6874 // Objective-C tests. 6875 //===----------------------------------------------------------------------===// 6876 6877 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 6878 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 6879 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 6880 format("-(NSUInteger)indexOfObject:(id)anObject;")); 6881 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 6882 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 6883 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 6884 format("-(NSInteger)Method3:(id)anObject;")); 6885 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 6886 format("-(NSInteger)Method4:(id)anObject;")); 6887 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 6888 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 6889 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 6890 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 6891 EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject " 6892 "forAllCells:(BOOL)flag;", 6893 format("- (void)sendAction:(SEL)aSelector to:(id)anObject " 6894 "forAllCells:(BOOL)flag;")); 6895 6896 // Very long objectiveC method declaration. 6897 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 6898 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;"); 6899 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 6900 " inRange:(NSRange)range\n" 6901 " outRange:(NSRange)out_range\n" 6902 " outRange1:(NSRange)out_range1\n" 6903 " outRange2:(NSRange)out_range2\n" 6904 " outRange3:(NSRange)out_range3\n" 6905 " outRange4:(NSRange)out_range4\n" 6906 " outRange5:(NSRange)out_range5\n" 6907 " outRange6:(NSRange)out_range6\n" 6908 " outRange7:(NSRange)out_range7\n" 6909 " outRange8:(NSRange)out_range8\n" 6910 " outRange9:(NSRange)out_range9;"); 6911 6912 // When the function name has to be wrapped. 6913 FormatStyle Style = getLLVMStyle(); 6914 Style.IndentWrappedFunctionNames = false; 6915 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 6916 "veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 6917 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 6918 "}", 6919 Style); 6920 Style.IndentWrappedFunctionNames = true; 6921 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 6922 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 6923 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 6924 "}", 6925 Style); 6926 6927 verifyFormat("- (int)sum:(vector<int>)numbers;"); 6928 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 6929 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 6930 // protocol lists (but not for template classes): 6931 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 6932 6933 verifyFormat("- (int (*)())foo:(int (*)())f;"); 6934 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); 6935 6936 // If there's no return type (very rare in practice!), LLVM and Google style 6937 // agree. 6938 verifyFormat("- foo;"); 6939 verifyFormat("- foo:(int)f;"); 6940 verifyGoogleFormat("- foo:(int)foo;"); 6941 } 6942 6943 TEST_F(FormatTest, FormatObjCInterface) { 6944 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n" 6945 "@public\n" 6946 " int field1;\n" 6947 "@protected\n" 6948 " int field2;\n" 6949 "@private\n" 6950 " int field3;\n" 6951 "@package\n" 6952 " int field4;\n" 6953 "}\n" 6954 "+ (id)init;\n" 6955 "@end"); 6956 6957 verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n" 6958 " @public\n" 6959 " int field1;\n" 6960 " @protected\n" 6961 " int field2;\n" 6962 " @private\n" 6963 " int field3;\n" 6964 " @package\n" 6965 " int field4;\n" 6966 "}\n" 6967 "+ (id)init;\n" 6968 "@end"); 6969 6970 verifyFormat("@interface /* wait for it */ Foo\n" 6971 "+ (id)init;\n" 6972 "// Look, a comment!\n" 6973 "- (int)answerWith:(int)i;\n" 6974 "@end"); 6975 6976 verifyFormat("@interface Foo\n" 6977 "@end\n" 6978 "@interface Bar\n" 6979 "@end"); 6980 6981 verifyFormat("@interface Foo : Bar\n" 6982 "+ (id)init;\n" 6983 "@end"); 6984 6985 verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n" 6986 "+ (id)init;\n" 6987 "@end"); 6988 6989 verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n" 6990 "+ (id)init;\n" 6991 "@end"); 6992 6993 verifyFormat("@interface Foo (HackStuff)\n" 6994 "+ (id)init;\n" 6995 "@end"); 6996 6997 verifyFormat("@interface Foo ()\n" 6998 "+ (id)init;\n" 6999 "@end"); 7000 7001 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n" 7002 "+ (id)init;\n" 7003 "@end"); 7004 7005 verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n" 7006 "+ (id)init;\n" 7007 "@end"); 7008 7009 verifyFormat("@interface Foo {\n" 7010 " int _i;\n" 7011 "}\n" 7012 "+ (id)init;\n" 7013 "@end"); 7014 7015 verifyFormat("@interface Foo : Bar {\n" 7016 " int _i;\n" 7017 "}\n" 7018 "+ (id)init;\n" 7019 "@end"); 7020 7021 verifyFormat("@interface Foo : Bar <Baz, Quux> {\n" 7022 " int _i;\n" 7023 "}\n" 7024 "+ (id)init;\n" 7025 "@end"); 7026 7027 verifyFormat("@interface Foo (HackStuff) {\n" 7028 " int _i;\n" 7029 "}\n" 7030 "+ (id)init;\n" 7031 "@end"); 7032 7033 verifyFormat("@interface Foo () {\n" 7034 " int _i;\n" 7035 "}\n" 7036 "+ (id)init;\n" 7037 "@end"); 7038 7039 verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n" 7040 " int _i;\n" 7041 "}\n" 7042 "+ (id)init;\n" 7043 "@end"); 7044 7045 FormatStyle OnePerLine = getGoogleStyle(); 7046 OnePerLine.BinPackParameters = false; 7047 verifyFormat("@interface aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ()<\n" 7048 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7049 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7050 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7051 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n" 7052 "}", 7053 OnePerLine); 7054 } 7055 7056 TEST_F(FormatTest, FormatObjCImplementation) { 7057 verifyFormat("@implementation Foo : NSObject {\n" 7058 "@public\n" 7059 " int field1;\n" 7060 "@protected\n" 7061 " int field2;\n" 7062 "@private\n" 7063 " int field3;\n" 7064 "@package\n" 7065 " int field4;\n" 7066 "}\n" 7067 "+ (id)init {\n}\n" 7068 "@end"); 7069 7070 verifyGoogleFormat("@implementation Foo : NSObject {\n" 7071 " @public\n" 7072 " int field1;\n" 7073 " @protected\n" 7074 " int field2;\n" 7075 " @private\n" 7076 " int field3;\n" 7077 " @package\n" 7078 " int field4;\n" 7079 "}\n" 7080 "+ (id)init {\n}\n" 7081 "@end"); 7082 7083 verifyFormat("@implementation Foo\n" 7084 "+ (id)init {\n" 7085 " if (true)\n" 7086 " return nil;\n" 7087 "}\n" 7088 "// Look, a comment!\n" 7089 "- (int)answerWith:(int)i {\n" 7090 " return i;\n" 7091 "}\n" 7092 "+ (int)answerWith:(int)i {\n" 7093 " return i;\n" 7094 "}\n" 7095 "@end"); 7096 7097 verifyFormat("@implementation Foo\n" 7098 "@end\n" 7099 "@implementation Bar\n" 7100 "@end"); 7101 7102 EXPECT_EQ("@implementation Foo : Bar\n" 7103 "+ (id)init {\n}\n" 7104 "- (void)foo {\n}\n" 7105 "@end", 7106 format("@implementation Foo : Bar\n" 7107 "+(id)init{}\n" 7108 "-(void)foo{}\n" 7109 "@end")); 7110 7111 verifyFormat("@implementation Foo {\n" 7112 " int _i;\n" 7113 "}\n" 7114 "+ (id)init {\n}\n" 7115 "@end"); 7116 7117 verifyFormat("@implementation Foo : Bar {\n" 7118 " int _i;\n" 7119 "}\n" 7120 "+ (id)init {\n}\n" 7121 "@end"); 7122 7123 verifyFormat("@implementation Foo (HackStuff)\n" 7124 "+ (id)init {\n}\n" 7125 "@end"); 7126 verifyFormat("@implementation ObjcClass\n" 7127 "- (void)method;\n" 7128 "{}\n" 7129 "@end"); 7130 } 7131 7132 TEST_F(FormatTest, FormatObjCProtocol) { 7133 verifyFormat("@protocol Foo\n" 7134 "@property(weak) id delegate;\n" 7135 "- (NSUInteger)numberOfThings;\n" 7136 "@end"); 7137 7138 verifyFormat("@protocol MyProtocol <NSObject>\n" 7139 "- (NSUInteger)numberOfThings;\n" 7140 "@end"); 7141 7142 verifyGoogleFormat("@protocol MyProtocol<NSObject>\n" 7143 "- (NSUInteger)numberOfThings;\n" 7144 "@end"); 7145 7146 verifyFormat("@protocol Foo;\n" 7147 "@protocol Bar;\n"); 7148 7149 verifyFormat("@protocol Foo\n" 7150 "@end\n" 7151 "@protocol Bar\n" 7152 "@end"); 7153 7154 verifyFormat("@protocol myProtocol\n" 7155 "- (void)mandatoryWithInt:(int)i;\n" 7156 "@optional\n" 7157 "- (void)optional;\n" 7158 "@required\n" 7159 "- (void)required;\n" 7160 "@optional\n" 7161 "@property(assign) int madProp;\n" 7162 "@end\n"); 7163 7164 verifyFormat("@property(nonatomic, assign, readonly)\n" 7165 " int *looooooooooooooooooooooooooooongNumber;\n" 7166 "@property(nonatomic, assign, readonly)\n" 7167 " NSString *looooooooooooooooooooooooooooongName;"); 7168 7169 verifyFormat("@implementation PR18406\n" 7170 "}\n" 7171 "@end"); 7172 } 7173 7174 TEST_F(FormatTest, FormatObjCMethodDeclarations) { 7175 verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n" 7176 " rect:(NSRect)theRect\n" 7177 " interval:(float)theInterval {\n" 7178 "}"); 7179 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n" 7180 " longKeyword:(NSRect)theRect\n" 7181 " evenLongerKeyword:(float)theInterval\n" 7182 " error:(NSError **)theError {\n" 7183 "}"); 7184 verifyFormat("- (instancetype)initXxxxxx:(id<x>)x\n" 7185 " y:(id<yyyyyyyyyyyyyyyyyyyy>)y\n" 7186 " NS_DESIGNATED_INITIALIZER;", 7187 getLLVMStyleWithColumns(60)); 7188 } 7189 7190 TEST_F(FormatTest, FormatObjCMethodExpr) { 7191 verifyFormat("[foo bar:baz];"); 7192 verifyFormat("return [foo bar:baz];"); 7193 verifyFormat("return (a)[foo bar:baz];"); 7194 verifyFormat("f([foo bar:baz]);"); 7195 verifyFormat("f(2, [foo bar:baz]);"); 7196 verifyFormat("f(2, a ? b : c);"); 7197 verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];"); 7198 7199 // Unary operators. 7200 verifyFormat("int a = +[foo bar:baz];"); 7201 verifyFormat("int a = -[foo bar:baz];"); 7202 verifyFormat("int a = ![foo bar:baz];"); 7203 verifyFormat("int a = ~[foo bar:baz];"); 7204 verifyFormat("int a = ++[foo bar:baz];"); 7205 verifyFormat("int a = --[foo bar:baz];"); 7206 verifyFormat("int a = sizeof [foo bar:baz];"); 7207 verifyFormat("int a = alignof [foo bar:baz];", getGoogleStyle()); 7208 verifyFormat("int a = &[foo bar:baz];"); 7209 verifyFormat("int a = *[foo bar:baz];"); 7210 // FIXME: Make casts work, without breaking f()[4]. 7211 // verifyFormat("int a = (int)[foo bar:baz];"); 7212 // verifyFormat("return (int)[foo bar:baz];"); 7213 // verifyFormat("(void)[foo bar:baz];"); 7214 verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];"); 7215 7216 // Binary operators. 7217 verifyFormat("[foo bar:baz], [foo bar:baz];"); 7218 verifyFormat("[foo bar:baz] = [foo bar:baz];"); 7219 verifyFormat("[foo bar:baz] *= [foo bar:baz];"); 7220 verifyFormat("[foo bar:baz] /= [foo bar:baz];"); 7221 verifyFormat("[foo bar:baz] %= [foo bar:baz];"); 7222 verifyFormat("[foo bar:baz] += [foo bar:baz];"); 7223 verifyFormat("[foo bar:baz] -= [foo bar:baz];"); 7224 verifyFormat("[foo bar:baz] <<= [foo bar:baz];"); 7225 verifyFormat("[foo bar:baz] >>= [foo bar:baz];"); 7226 verifyFormat("[foo bar:baz] &= [foo bar:baz];"); 7227 verifyFormat("[foo bar:baz] ^= [foo bar:baz];"); 7228 verifyFormat("[foo bar:baz] |= [foo bar:baz];"); 7229 verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];"); 7230 verifyFormat("[foo bar:baz] || [foo bar:baz];"); 7231 verifyFormat("[foo bar:baz] && [foo bar:baz];"); 7232 verifyFormat("[foo bar:baz] | [foo bar:baz];"); 7233 verifyFormat("[foo bar:baz] ^ [foo bar:baz];"); 7234 verifyFormat("[foo bar:baz] & [foo bar:baz];"); 7235 verifyFormat("[foo bar:baz] == [foo bar:baz];"); 7236 verifyFormat("[foo bar:baz] != [foo bar:baz];"); 7237 verifyFormat("[foo bar:baz] >= [foo bar:baz];"); 7238 verifyFormat("[foo bar:baz] <= [foo bar:baz];"); 7239 verifyFormat("[foo bar:baz] > [foo bar:baz];"); 7240 verifyFormat("[foo bar:baz] < [foo bar:baz];"); 7241 verifyFormat("[foo bar:baz] >> [foo bar:baz];"); 7242 verifyFormat("[foo bar:baz] << [foo bar:baz];"); 7243 verifyFormat("[foo bar:baz] - [foo bar:baz];"); 7244 verifyFormat("[foo bar:baz] + [foo bar:baz];"); 7245 verifyFormat("[foo bar:baz] * [foo bar:baz];"); 7246 verifyFormat("[foo bar:baz] / [foo bar:baz];"); 7247 verifyFormat("[foo bar:baz] % [foo bar:baz];"); 7248 // Whew! 7249 7250 verifyFormat("return in[42];"); 7251 verifyFormat("for (auto v : in[1]) {\n}"); 7252 verifyFormat("for (int i = 0; i < in[a]; ++i) {\n}"); 7253 verifyFormat("for (int i = 0; in[a] < i; ++i) {\n}"); 7254 verifyFormat("for (int i = 0; i < n; ++i, ++in[a]) {\n}"); 7255 verifyFormat("for (int i = 0; i < n; ++i, in[a]++) {\n}"); 7256 verifyFormat("for (int i = 0; i < f(in[a]); ++i, in[a]++) {\n}"); 7257 verifyFormat("for (id foo in [self getStuffFor:bla]) {\n" 7258 "}"); 7259 verifyFormat("[self aaaaa:MACRO(a, b:, c:)];"); 7260 verifyFormat("[self aaaaa:(1 + 2) bbbbb:3];"); 7261 verifyFormat("[self aaaaa:(Type)a bbbbb:3];"); 7262 7263 verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];"); 7264 verifyFormat("[self stuffWithInt:a ? b : c float:4.5];"); 7265 verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];"); 7266 verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];"); 7267 verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]"); 7268 verifyFormat("[button setAction:@selector(zoomOut:)];"); 7269 verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];"); 7270 7271 verifyFormat("arr[[self indexForFoo:a]];"); 7272 verifyFormat("throw [self errorFor:a];"); 7273 verifyFormat("@throw [self errorFor:a];"); 7274 7275 verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];"); 7276 verifyFormat("[(id)foo bar:(id) ? baz : quux];"); 7277 verifyFormat("4 > 4 ? (id)a : (id)baz;"); 7278 7279 // This tests that the formatter doesn't break after "backing" but before ":", 7280 // which would be at 80 columns. 7281 verifyFormat( 7282 "void f() {\n" 7283 " if ((self = [super initWithContentRect:contentRect\n" 7284 " styleMask:styleMask ?: otherMask\n" 7285 " backing:NSBackingStoreBuffered\n" 7286 " defer:YES]))"); 7287 7288 verifyFormat( 7289 "[foo checkThatBreakingAfterColonWorksOk:\n" 7290 " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];"); 7291 7292 verifyFormat("[myObj short:arg1 // Force line break\n" 7293 " longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n" 7294 " evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n" 7295 " error:arg4];"); 7296 verifyFormat( 7297 "void f() {\n" 7298 " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n" 7299 " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n" 7300 " pos.width(), pos.height())\n" 7301 " styleMask:NSBorderlessWindowMask\n" 7302 " backing:NSBackingStoreBuffered\n" 7303 " defer:NO]);\n" 7304 "}"); 7305 verifyFormat( 7306 "void f() {\n" 7307 " popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n" 7308 " iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n" 7309 " pos.width(), pos.height())\n" 7310 " syeMask:NSBorderlessWindowMask\n" 7311 " bking:NSBackingStoreBuffered\n" 7312 " der:NO]);\n" 7313 "}", 7314 getLLVMStyleWithColumns(70)); 7315 verifyFormat( 7316 "void f() {\n" 7317 " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n" 7318 " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n" 7319 " pos.width(), pos.height())\n" 7320 " styleMask:NSBorderlessWindowMask\n" 7321 " backing:NSBackingStoreBuffered\n" 7322 " defer:NO]);\n" 7323 "}", 7324 getChromiumStyle(FormatStyle::LK_Cpp)); 7325 verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n" 7326 " with:contentsNativeView];"); 7327 7328 verifyFormat( 7329 "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n" 7330 " owner:nillllll];"); 7331 7332 verifyFormat( 7333 "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n" 7334 " forType:kBookmarkButtonDragType];"); 7335 7336 verifyFormat("[defaultCenter addObserver:self\n" 7337 " selector:@selector(willEnterFullscreen)\n" 7338 " name:kWillEnterFullscreenNotification\n" 7339 " object:nil];"); 7340 verifyFormat("[image_rep drawInRect:drawRect\n" 7341 " fromRect:NSZeroRect\n" 7342 " operation:NSCompositeCopy\n" 7343 " fraction:1.0\n" 7344 " respectFlipped:NO\n" 7345 " hints:nil];"); 7346 verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7347 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 7348 verifyFormat("[aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 7349 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 7350 verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaa[aaaaaaaaaaaaaaaaaaaaa]\n" 7351 " aaaaaaaaaaaaaaaaaaaaaa];"); 7352 verifyFormat("[call aaaaaaaa.aaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa\n" 7353 " .aaaaaaaa];", // FIXME: Indentation seems off. 7354 getLLVMStyleWithColumns(60)); 7355 7356 verifyFormat( 7357 "scoped_nsobject<NSTextField> message(\n" 7358 " // The frame will be fixed up when |-setMessageText:| is called.\n" 7359 " [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);"); 7360 verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n" 7361 " aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n" 7362 " aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n" 7363 " aaaa:bbb];"); 7364 verifyFormat("[self param:function( //\n" 7365 " parameter)]"); 7366 verifyFormat( 7367 "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n" 7368 " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n" 7369 " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];"); 7370 7371 // FIXME: This violates the column limit. 7372 verifyFormat( 7373 "[aaaaaaaaaaaaaaaaaaaaaaaaa\n" 7374 " aaaaaaaaaaaaaaaaa:aaaaaaaa\n" 7375 " aaa:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];", 7376 getLLVMStyleWithColumns(60)); 7377 7378 // Variadic parameters. 7379 verifyFormat( 7380 "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];"); 7381 verifyFormat( 7382 "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n" 7383 " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n" 7384 " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];"); 7385 verifyFormat("[self // break\n" 7386 " a:a\n" 7387 " aaa:aaa];"); 7388 verifyFormat("bool a = ([aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaa ||\n" 7389 " [aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaaaaa);"); 7390 } 7391 7392 TEST_F(FormatTest, ObjCAt) { 7393 verifyFormat("@autoreleasepool"); 7394 verifyFormat("@catch"); 7395 verifyFormat("@class"); 7396 verifyFormat("@compatibility_alias"); 7397 verifyFormat("@defs"); 7398 verifyFormat("@dynamic"); 7399 verifyFormat("@encode"); 7400 verifyFormat("@end"); 7401 verifyFormat("@finally"); 7402 verifyFormat("@implementation"); 7403 verifyFormat("@import"); 7404 verifyFormat("@interface"); 7405 verifyFormat("@optional"); 7406 verifyFormat("@package"); 7407 verifyFormat("@private"); 7408 verifyFormat("@property"); 7409 verifyFormat("@protected"); 7410 verifyFormat("@protocol"); 7411 verifyFormat("@public"); 7412 verifyFormat("@required"); 7413 verifyFormat("@selector"); 7414 verifyFormat("@synchronized"); 7415 verifyFormat("@synthesize"); 7416 verifyFormat("@throw"); 7417 verifyFormat("@try"); 7418 7419 EXPECT_EQ("@interface", format("@ interface")); 7420 7421 // The precise formatting of this doesn't matter, nobody writes code like 7422 // this. 7423 verifyFormat("@ /*foo*/ interface"); 7424 } 7425 7426 TEST_F(FormatTest, ObjCSnippets) { 7427 verifyFormat("@autoreleasepool {\n" 7428 " foo();\n" 7429 "}"); 7430 verifyFormat("@class Foo, Bar;"); 7431 verifyFormat("@compatibility_alias AliasName ExistingClass;"); 7432 verifyFormat("@dynamic textColor;"); 7433 verifyFormat("char *buf1 = @encode(int *);"); 7434 verifyFormat("char *buf1 = @encode(typeof(4 * 5));"); 7435 verifyFormat("char *buf1 = @encode(int **);"); 7436 verifyFormat("Protocol *proto = @protocol(p1);"); 7437 verifyFormat("SEL s = @selector(foo:);"); 7438 verifyFormat("@synchronized(self) {\n" 7439 " f();\n" 7440 "}"); 7441 7442 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); 7443 verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); 7444 7445 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;"); 7446 verifyFormat("@property(assign, getter=isEditable) BOOL editable;"); 7447 verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;"); 7448 verifyFormat("@property (assign, getter=isEditable) BOOL editable;", 7449 getMozillaStyle()); 7450 verifyFormat("@property BOOL editable;", getMozillaStyle()); 7451 verifyFormat("@property (assign, getter=isEditable) BOOL editable;", 7452 getWebKitStyle()); 7453 verifyFormat("@property BOOL editable;", getWebKitStyle()); 7454 7455 verifyFormat("@import foo.bar;\n" 7456 "@import baz;"); 7457 } 7458 7459 TEST_F(FormatTest, ObjCLiterals) { 7460 verifyFormat("@\"String\""); 7461 verifyFormat("@1"); 7462 verifyFormat("@+4.8"); 7463 verifyFormat("@-4"); 7464 verifyFormat("@1LL"); 7465 verifyFormat("@.5"); 7466 verifyFormat("@'c'"); 7467 verifyFormat("@true"); 7468 7469 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);"); 7470 verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);"); 7471 verifyFormat("NSNumber *favoriteColor = @(Green);"); 7472 verifyFormat("NSString *path = @(getenv(\"PATH\"));"); 7473 7474 verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];"); 7475 } 7476 7477 TEST_F(FormatTest, ObjCDictLiterals) { 7478 verifyFormat("@{"); 7479 verifyFormat("@{}"); 7480 verifyFormat("@{@\"one\" : @1}"); 7481 verifyFormat("return @{@\"one\" : @1;"); 7482 verifyFormat("@{@\"one\" : @1}"); 7483 7484 verifyFormat("@{@\"one\" : @{@2 : @1}}"); 7485 verifyFormat("@{\n" 7486 " @\"one\" : @{@2 : @1},\n" 7487 "}"); 7488 7489 verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}"); 7490 verifyIncompleteFormat("[self setDict:@{}"); 7491 verifyIncompleteFormat("[self setDict:@{@1 : @2}"); 7492 verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);"); 7493 verifyFormat( 7494 "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};"); 7495 verifyFormat( 7496 "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};"); 7497 7498 verifyFormat("NSDictionary *d = @{\n" 7499 " @\"nam\" : NSUserNam(),\n" 7500 " @\"dte\" : [NSDate date],\n" 7501 " @\"processInfo\" : [NSProcessInfo processInfo]\n" 7502 "};"); 7503 verifyFormat( 7504 "@{\n" 7505 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : " 7506 "regularFont,\n" 7507 "};"); 7508 verifyGoogleFormat( 7509 "@{\n" 7510 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : " 7511 "regularFont,\n" 7512 "};"); 7513 verifyFormat( 7514 "@{\n" 7515 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n" 7516 " reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n" 7517 "};"); 7518 7519 // We should try to be robust in case someone forgets the "@". 7520 verifyFormat("NSDictionary *d = {\n" 7521 " @\"nam\" : NSUserNam(),\n" 7522 " @\"dte\" : [NSDate date],\n" 7523 " @\"processInfo\" : [NSProcessInfo processInfo]\n" 7524 "};"); 7525 verifyFormat("NSMutableDictionary *dictionary =\n" 7526 " [NSMutableDictionary dictionaryWithDictionary:@{\n" 7527 " aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n" 7528 " bbbbbbbbbbbbbbbbbb : bbbbb,\n" 7529 " cccccccccccccccc : ccccccccccccccc\n" 7530 " }];"); 7531 } 7532 7533 TEST_F(FormatTest, ObjCArrayLiterals) { 7534 verifyIncompleteFormat("@["); 7535 verifyFormat("@[]"); 7536 verifyFormat( 7537 "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];"); 7538 verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];"); 7539 verifyFormat("NSArray *array = @[ [foo description] ];"); 7540 7541 verifyFormat( 7542 "NSArray *some_variable = @[\n" 7543 " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n" 7544 " @\"aaaaaaaaaaaaaaaaa\",\n" 7545 " @\"aaaaaaaaaaaaaaaaa\",\n" 7546 " @\"aaaaaaaaaaaaaaaaa\"\n" 7547 "];"); 7548 verifyFormat("NSArray *some_variable = @[\n" 7549 " @\"aaaaaaaaaaaaaaaaa\",\n" 7550 " @\"aaaaaaaaaaaaaaaaa\",\n" 7551 " @\"aaaaaaaaaaaaaaaaa\",\n" 7552 " @\"aaaaaaaaaaaaaaaaa\",\n" 7553 "];"); 7554 verifyGoogleFormat("NSArray *some_variable = @[\n" 7555 " @\"aaaaaaaaaaaaaaaaa\",\n" 7556 " @\"aaaaaaaaaaaaaaaaa\",\n" 7557 " @\"aaaaaaaaaaaaaaaaa\",\n" 7558 " @\"aaaaaaaaaaaaaaaaa\"\n" 7559 "];"); 7560 verifyFormat("NSArray *array = @[\n" 7561 " @\"a\",\n" 7562 " @\"a\",\n" // Trailing comma -> one per line. 7563 "];"); 7564 7565 // We should try to be robust in case someone forgets the "@". 7566 verifyFormat("NSArray *some_variable = [\n" 7567 " @\"aaaaaaaaaaaaaaaaa\",\n" 7568 " @\"aaaaaaaaaaaaaaaaa\",\n" 7569 " @\"aaaaaaaaaaaaaaaaa\",\n" 7570 " @\"aaaaaaaaaaaaaaaaa\",\n" 7571 "];"); 7572 verifyFormat( 7573 "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n" 7574 " index:(NSUInteger)index\n" 7575 " nonDigitAttributes:\n" 7576 " (NSDictionary *)noDigitAttributes;"); 7577 verifyFormat( 7578 "[someFunction someLooooooooooooongParameter:\n" 7579 " @[ NSBundle.mainBundle.infoDictionary[@\"a\"] ]];"); 7580 } 7581 7582 TEST_F(FormatTest, ReformatRegionAdjustsIndent) { 7583 EXPECT_EQ("{\n" 7584 "{\n" 7585 "a;\n" 7586 "b;\n" 7587 "}\n" 7588 "}", 7589 format("{\n" 7590 "{\n" 7591 "a;\n" 7592 " b;\n" 7593 "}\n" 7594 "}", 7595 13, 2, getLLVMStyle())); 7596 EXPECT_EQ("{\n" 7597 "{\n" 7598 " a;\n" 7599 "b;\n" 7600 "}\n" 7601 "}", 7602 format("{\n" 7603 "{\n" 7604 " a;\n" 7605 "b;\n" 7606 "}\n" 7607 "}", 7608 9, 2, getLLVMStyle())); 7609 EXPECT_EQ("{\n" 7610 "{\n" 7611 "public:\n" 7612 " b;\n" 7613 "}\n" 7614 "}", 7615 format("{\n" 7616 "{\n" 7617 "public:\n" 7618 " b;\n" 7619 "}\n" 7620 "}", 7621 17, 2, getLLVMStyle())); 7622 EXPECT_EQ("{\n" 7623 "{\n" 7624 "a;\n" 7625 "}\n" 7626 "{\n" 7627 " b; //\n" 7628 "}\n" 7629 "}", 7630 format("{\n" 7631 "{\n" 7632 "a;\n" 7633 "}\n" 7634 "{\n" 7635 " b; //\n" 7636 "}\n" 7637 "}", 7638 22, 2, getLLVMStyle())); 7639 EXPECT_EQ(" {\n" 7640 " a; //\n" 7641 " }", 7642 format(" {\n" 7643 "a; //\n" 7644 " }", 7645 4, 2, getLLVMStyle())); 7646 EXPECT_EQ("void f() {}\n" 7647 "void g() {}", 7648 format("void f() {}\n" 7649 "void g() {}", 7650 13, 0, getLLVMStyle())); 7651 EXPECT_EQ("int a; // comment\n" 7652 " // line 2\n" 7653 "int b;", 7654 format("int a; // comment\n" 7655 " // line 2\n" 7656 " int b;", 7657 35, 0, getLLVMStyle())); 7658 EXPECT_EQ(" int a;\n" 7659 " void\n" 7660 " ffffff() {\n" 7661 " }", 7662 format(" int a;\n" 7663 "void ffffff() {}", 7664 11, 0, getLLVMStyleWithColumns(11))); 7665 7666 EXPECT_EQ(" void f() {\n" 7667 "#define A 1\n" 7668 " }", 7669 format(" void f() {\n" 7670 " #define A 1\n" // Format this line. 7671 " }", 7672 20, 0, getLLVMStyle())); 7673 EXPECT_EQ(" void f() {\n" 7674 " int i;\n" 7675 "#define A \\\n" 7676 " int i; \\\n" 7677 " int j;\n" 7678 " int k;\n" 7679 " }", 7680 format(" void f() {\n" 7681 " int i;\n" 7682 "#define A \\\n" 7683 " int i; \\\n" 7684 " int j;\n" 7685 " int k;\n" // Format this line. 7686 " }", 7687 67, 0, getLLVMStyle())); 7688 } 7689 7690 TEST_F(FormatTest, BreaksStringLiterals) { 7691 EXPECT_EQ("\"some text \"\n" 7692 "\"other\";", 7693 format("\"some text other\";", getLLVMStyleWithColumns(12))); 7694 EXPECT_EQ("\"some text \"\n" 7695 "\"other\";", 7696 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); 7697 EXPECT_EQ( 7698 "#define A \\\n" 7699 " \"some \" \\\n" 7700 " \"text \" \\\n" 7701 " \"other\";", 7702 format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); 7703 EXPECT_EQ( 7704 "#define A \\\n" 7705 " \"so \" \\\n" 7706 " \"text \" \\\n" 7707 " \"other\";", 7708 format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); 7709 7710 EXPECT_EQ("\"some text\"", 7711 format("\"some text\"", getLLVMStyleWithColumns(1))); 7712 EXPECT_EQ("\"some text\"", 7713 format("\"some text\"", getLLVMStyleWithColumns(11))); 7714 EXPECT_EQ("\"some \"\n" 7715 "\"text\"", 7716 format("\"some text\"", getLLVMStyleWithColumns(10))); 7717 EXPECT_EQ("\"some \"\n" 7718 "\"text\"", 7719 format("\"some text\"", getLLVMStyleWithColumns(7))); 7720 EXPECT_EQ("\"some\"\n" 7721 "\" tex\"\n" 7722 "\"t\"", 7723 format("\"some text\"", getLLVMStyleWithColumns(6))); 7724 EXPECT_EQ("\"some\"\n" 7725 "\" tex\"\n" 7726 "\" and\"", 7727 format("\"some tex and\"", getLLVMStyleWithColumns(6))); 7728 EXPECT_EQ("\"some\"\n" 7729 "\"/tex\"\n" 7730 "\"/and\"", 7731 format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); 7732 7733 EXPECT_EQ("variable =\n" 7734 " \"long string \"\n" 7735 " \"literal\";", 7736 format("variable = \"long string literal\";", 7737 getLLVMStyleWithColumns(20))); 7738 7739 EXPECT_EQ("variable = f(\n" 7740 " \"long string \"\n" 7741 " \"literal\",\n" 7742 " short,\n" 7743 " loooooooooooooooooooong);", 7744 format("variable = f(\"long string literal\", short, " 7745 "loooooooooooooooooooong);", 7746 getLLVMStyleWithColumns(20))); 7747 7748 EXPECT_EQ( 7749 "f(g(\"long string \"\n" 7750 " \"literal\"),\n" 7751 " b);", 7752 format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20))); 7753 EXPECT_EQ("f(g(\"long string \"\n" 7754 " \"literal\",\n" 7755 " a),\n" 7756 " b);", 7757 format("f(g(\"long string literal\", a), b);", 7758 getLLVMStyleWithColumns(20))); 7759 EXPECT_EQ( 7760 "f(\"one two\".split(\n" 7761 " variable));", 7762 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); 7763 EXPECT_EQ("f(\"one two three four five six \"\n" 7764 " \"seven\".split(\n" 7765 " really_looooong_variable));", 7766 format("f(\"one two three four five six seven\"." 7767 "split(really_looooong_variable));", 7768 getLLVMStyleWithColumns(33))); 7769 7770 EXPECT_EQ("f(\"some \"\n" 7771 " \"text\",\n" 7772 " other);", 7773 format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); 7774 7775 // Only break as a last resort. 7776 verifyFormat( 7777 "aaaaaaaaaaaaaaaaaaaa(\n" 7778 " aaaaaaaaaaaaaaaaaaaa,\n" 7779 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); 7780 7781 EXPECT_EQ("\"splitmea\"\n" 7782 "\"trandomp\"\n" 7783 "\"oint\"", 7784 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); 7785 7786 EXPECT_EQ("\"split/\"\n" 7787 "\"pathat/\"\n" 7788 "\"slashes\"", 7789 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 7790 7791 EXPECT_EQ("\"split/\"\n" 7792 "\"pathat/\"\n" 7793 "\"slashes\"", 7794 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 7795 EXPECT_EQ("\"split at \"\n" 7796 "\"spaces/at/\"\n" 7797 "\"slashes.at.any$\"\n" 7798 "\"non-alphanumeric%\"\n" 7799 "\"1111111111characte\"\n" 7800 "\"rs\"", 7801 format("\"split at " 7802 "spaces/at/" 7803 "slashes.at." 7804 "any$non-" 7805 "alphanumeric%" 7806 "1111111111characte" 7807 "rs\"", 7808 getLLVMStyleWithColumns(20))); 7809 7810 // Verify that splitting the strings understands 7811 // Style::AlwaysBreakBeforeMultilineStrings. 7812 EXPECT_EQ("aaaaaaaaaaaa(aaaaaaaaaaaaa,\n" 7813 " \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" 7814 " \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", 7815 format("aaaaaaaaaaaa(aaaaaaaaaaaaa, \"aaaaaaaaaaaaaaaaaaaaaa " 7816 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa " 7817 "aaaaaaaaaaaaaaaaaaaaaa\");", 7818 getGoogleStyle())); 7819 EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7820 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", 7821 format("return \"aaaaaaaaaaaaaaaaaaaaaa " 7822 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 7823 "aaaaaaaaaaaaaaaaaaaaaa\";", 7824 getGoogleStyle())); 7825 EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7826 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 7827 format("llvm::outs() << " 7828 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa" 7829 "aaaaaaaaaaaaaaaaaaa\";")); 7830 EXPECT_EQ("ffff(\n" 7831 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7832 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 7833 format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " 7834 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 7835 getGoogleStyle())); 7836 7837 FormatStyle AlignLeft = getLLVMStyleWithColumns(12); 7838 AlignLeft.AlignEscapedNewlinesLeft = true; 7839 EXPECT_EQ("#define A \\\n" 7840 " \"some \" \\\n" 7841 " \"text \" \\\n" 7842 " \"other\";", 7843 format("#define A \"some text other\";", AlignLeft)); 7844 } 7845 7846 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) { 7847 EXPECT_EQ( 7848 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 7849 "(\n" 7850 " \"x\t\");", 7851 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 7852 "aaaaaaa(" 7853 "\"x\t\");")); 7854 } 7855 7856 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) { 7857 EXPECT_EQ( 7858 "u8\"utf8 string \"\n" 7859 "u8\"literal\";", 7860 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16))); 7861 EXPECT_EQ( 7862 "u\"utf16 string \"\n" 7863 "u\"literal\";", 7864 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16))); 7865 EXPECT_EQ( 7866 "U\"utf32 string \"\n" 7867 "U\"literal\";", 7868 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16))); 7869 EXPECT_EQ("L\"wide string \"\n" 7870 "L\"literal\";", 7871 format("L\"wide string literal\";", getGoogleStyleWithColumns(16))); 7872 EXPECT_EQ("@\"NSString \"\n" 7873 "@\"literal\";", 7874 format("@\"NSString literal\";", getGoogleStyleWithColumns(19))); 7875 7876 // This input makes clang-format try to split the incomplete unicode escape 7877 // sequence, which used to lead to a crasher. 7878 verifyNoCrash( 7879 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 7880 getLLVMStyleWithColumns(60)); 7881 } 7882 7883 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) { 7884 FormatStyle Style = getGoogleStyleWithColumns(15); 7885 EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style)); 7886 EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style)); 7887 EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style)); 7888 EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style)); 7889 EXPECT_EQ("u8R\"x(raw literal)x\";", 7890 format("u8R\"x(raw literal)x\";", Style)); 7891 } 7892 7893 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { 7894 FormatStyle Style = getLLVMStyleWithColumns(20); 7895 EXPECT_EQ( 7896 "_T(\"aaaaaaaaaaaaaa\")\n" 7897 "_T(\"aaaaaaaaaaaaaa\")\n" 7898 "_T(\"aaaaaaaaaaaa\")", 7899 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style)); 7900 EXPECT_EQ("f(x, _T(\"aaaaaaaaa\")\n" 7901 " _T(\"aaaaaa\"),\n" 7902 " z);", 7903 format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style)); 7904 7905 // FIXME: Handle embedded spaces in one iteration. 7906 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n" 7907 // "_T(\"aaaaaaaaaaaaa\")\n" 7908 // "_T(\"aaaaaaaaaaaaa\")\n" 7909 // "_T(\"a\")", 7910 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 7911 // getLLVMStyleWithColumns(20))); 7912 EXPECT_EQ( 7913 "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 7914 format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style)); 7915 EXPECT_EQ("f(\n" 7916 "#if !TEST\n" 7917 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 7918 "#endif\n" 7919 " );", 7920 format("f(\n" 7921 "#if !TEST\n" 7922 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 7923 "#endif\n" 7924 ");")); 7925 EXPECT_EQ("f(\n" 7926 "\n" 7927 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));", 7928 format("f(\n" 7929 "\n" 7930 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));")); 7931 } 7932 7933 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { 7934 EXPECT_EQ( 7935 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 7936 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 7937 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 7938 format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 7939 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 7940 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";")); 7941 } 7942 7943 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { 7944 EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);", 7945 format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle())); 7946 EXPECT_EQ("fffffffffff(g(R\"x(\n" 7947 "multiline raw string literal xxxxxxxxxxxxxx\n" 7948 ")x\",\n" 7949 " a),\n" 7950 " b);", 7951 format("fffffffffff(g(R\"x(\n" 7952 "multiline raw string literal xxxxxxxxxxxxxx\n" 7953 ")x\", a), b);", 7954 getGoogleStyleWithColumns(20))); 7955 EXPECT_EQ("fffffffffff(\n" 7956 " g(R\"x(qqq\n" 7957 "multiline raw string literal xxxxxxxxxxxxxx\n" 7958 ")x\",\n" 7959 " a),\n" 7960 " b);", 7961 format("fffffffffff(g(R\"x(qqq\n" 7962 "multiline raw string literal xxxxxxxxxxxxxx\n" 7963 ")x\", a), b);", 7964 getGoogleStyleWithColumns(20))); 7965 7966 EXPECT_EQ("fffffffffff(R\"x(\n" 7967 "multiline raw string literal xxxxxxxxxxxxxx\n" 7968 ")x\");", 7969 format("fffffffffff(R\"x(\n" 7970 "multiline raw string literal xxxxxxxxxxxxxx\n" 7971 ")x\");", 7972 getGoogleStyleWithColumns(20))); 7973 EXPECT_EQ("fffffffffff(R\"x(\n" 7974 "multiline raw string literal xxxxxxxxxxxxxx\n" 7975 ")x\" + bbbbbb);", 7976 format("fffffffffff(R\"x(\n" 7977 "multiline raw string literal xxxxxxxxxxxxxx\n" 7978 ")x\" + bbbbbb);", 7979 getGoogleStyleWithColumns(20))); 7980 EXPECT_EQ("fffffffffff(\n" 7981 " R\"x(\n" 7982 "multiline raw string literal xxxxxxxxxxxxxx\n" 7983 ")x\" +\n" 7984 " bbbbbb);", 7985 format("fffffffffff(\n" 7986 " R\"x(\n" 7987 "multiline raw string literal xxxxxxxxxxxxxx\n" 7988 ")x\" + bbbbbb);", 7989 getGoogleStyleWithColumns(20))); 7990 } 7991 7992 TEST_F(FormatTest, SkipsUnknownStringLiterals) { 7993 verifyFormat("string a = \"unterminated;"); 7994 EXPECT_EQ("function(\"unterminated,\n" 7995 " OtherParameter);", 7996 format("function( \"unterminated,\n" 7997 " OtherParameter);")); 7998 } 7999 8000 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { 8001 FormatStyle Style = getLLVMStyle(); 8002 Style.Standard = FormatStyle::LS_Cpp03; 8003 EXPECT_EQ("#define x(_a) printf(\"foo\" _a);", 8004 format("#define x(_a) printf(\"foo\"_a);", Style)); 8005 } 8006 8007 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); } 8008 8009 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { 8010 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n" 8011 " \"ddeeefff\");", 8012 format("someFunction(\"aaabbbcccdddeeefff\");", 8013 getLLVMStyleWithColumns(25))); 8014 EXPECT_EQ("someFunction1234567890(\n" 8015 " \"aaabbbcccdddeeefff\");", 8016 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 8017 getLLVMStyleWithColumns(26))); 8018 EXPECT_EQ("someFunction1234567890(\n" 8019 " \"aaabbbcccdddeeeff\"\n" 8020 " \"f\");", 8021 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 8022 getLLVMStyleWithColumns(25))); 8023 EXPECT_EQ("someFunction1234567890(\n" 8024 " \"aaabbbcccdddeeeff\"\n" 8025 " \"f\");", 8026 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 8027 getLLVMStyleWithColumns(24))); 8028 EXPECT_EQ("someFunction(\"aaabbbcc \"\n" 8029 " \"ddde \"\n" 8030 " \"efff\");", 8031 format("someFunction(\"aaabbbcc ddde efff\");", 8032 getLLVMStyleWithColumns(25))); 8033 EXPECT_EQ("someFunction(\"aaabbbccc \"\n" 8034 " \"ddeeefff\");", 8035 format("someFunction(\"aaabbbccc ddeeefff\");", 8036 getLLVMStyleWithColumns(25))); 8037 EXPECT_EQ("someFunction1234567890(\n" 8038 " \"aaabb \"\n" 8039 " \"cccdddeeefff\");", 8040 format("someFunction1234567890(\"aaabb cccdddeeefff\");", 8041 getLLVMStyleWithColumns(25))); 8042 EXPECT_EQ("#define A \\\n" 8043 " string s = \\\n" 8044 " \"123456789\" \\\n" 8045 " \"0\"; \\\n" 8046 " int i;", 8047 format("#define A string s = \"1234567890\"; int i;", 8048 getLLVMStyleWithColumns(20))); 8049 // FIXME: Put additional penalties on breaking at non-whitespace locations. 8050 EXPECT_EQ("someFunction(\"aaabbbcc \"\n" 8051 " \"dddeeeff\"\n" 8052 " \"f\");", 8053 format("someFunction(\"aaabbbcc dddeeefff\");", 8054 getLLVMStyleWithColumns(25))); 8055 } 8056 8057 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { 8058 EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3))); 8059 EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2))); 8060 EXPECT_EQ("\"test\"\n" 8061 "\"\\n\"", 8062 format("\"test\\n\"", getLLVMStyleWithColumns(7))); 8063 EXPECT_EQ("\"tes\\\\\"\n" 8064 "\"n\"", 8065 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); 8066 EXPECT_EQ("\"\\\\\\\\\"\n" 8067 "\"\\n\"", 8068 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); 8069 EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7))); 8070 EXPECT_EQ("\"\\uff01\"\n" 8071 "\"test\"", 8072 format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); 8073 EXPECT_EQ("\"\\Uff01ff02\"", 8074 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); 8075 EXPECT_EQ("\"\\x000000000001\"\n" 8076 "\"next\"", 8077 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); 8078 EXPECT_EQ("\"\\x000000000001next\"", 8079 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); 8080 EXPECT_EQ("\"\\x000000000001\"", 8081 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); 8082 EXPECT_EQ("\"test\"\n" 8083 "\"\\000000\"\n" 8084 "\"000001\"", 8085 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); 8086 EXPECT_EQ("\"test\\000\"\n" 8087 "\"00000000\"\n" 8088 "\"1\"", 8089 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); 8090 } 8091 8092 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { 8093 verifyFormat("void f() {\n" 8094 " return g() {}\n" 8095 " void h() {}"); 8096 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n" 8097 "g();\n" 8098 "}"); 8099 } 8100 8101 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) { 8102 verifyFormat( 8103 "void f() { return C{param1, param2}.SomeCall(param1, param2); }"); 8104 } 8105 8106 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { 8107 verifyFormat("class X {\n" 8108 " void f() {\n" 8109 " }\n" 8110 "};", 8111 getLLVMStyleWithColumns(12)); 8112 } 8113 8114 TEST_F(FormatTest, ConfigurableIndentWidth) { 8115 FormatStyle EightIndent = getLLVMStyleWithColumns(18); 8116 EightIndent.IndentWidth = 8; 8117 EightIndent.ContinuationIndentWidth = 8; 8118 verifyFormat("void f() {\n" 8119 " someFunction();\n" 8120 " if (true) {\n" 8121 " f();\n" 8122 " }\n" 8123 "}", 8124 EightIndent); 8125 verifyFormat("class X {\n" 8126 " void f() {\n" 8127 " }\n" 8128 "};", 8129 EightIndent); 8130 verifyFormat("int x[] = {\n" 8131 " call(),\n" 8132 " call()};", 8133 EightIndent); 8134 } 8135 8136 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) { 8137 verifyFormat("double\n" 8138 "f();", 8139 getLLVMStyleWithColumns(8)); 8140 } 8141 8142 TEST_F(FormatTest, ConfigurableUseOfTab) { 8143 FormatStyle Tab = getLLVMStyleWithColumns(42); 8144 Tab.IndentWidth = 8; 8145 Tab.UseTab = FormatStyle::UT_Always; 8146 Tab.AlignEscapedNewlinesLeft = true; 8147 8148 EXPECT_EQ("if (aaaaaaaa && // q\n" 8149 " bb)\t\t// w\n" 8150 "\t;", 8151 format("if (aaaaaaaa &&// q\n" 8152 "bb)// w\n" 8153 ";", 8154 Tab)); 8155 EXPECT_EQ("if (aaa && bbb) // w\n" 8156 "\t;", 8157 format("if(aaa&&bbb)// w\n" 8158 ";", 8159 Tab)); 8160 8161 verifyFormat("class X {\n" 8162 "\tvoid f() {\n" 8163 "\t\tsomeFunction(parameter1,\n" 8164 "\t\t\t parameter2);\n" 8165 "\t}\n" 8166 "};", 8167 Tab); 8168 verifyFormat("#define A \\\n" 8169 "\tvoid f() { \\\n" 8170 "\t\tsomeFunction( \\\n" 8171 "\t\t parameter1, \\\n" 8172 "\t\t parameter2); \\\n" 8173 "\t}", 8174 Tab); 8175 EXPECT_EQ("void f() {\n" 8176 "\tf();\n" 8177 "\tg();\n" 8178 "}", 8179 format("void f() {\n" 8180 "\tf();\n" 8181 "\tg();\n" 8182 "}", 8183 0, 0, Tab)); 8184 EXPECT_EQ("void f() {\n" 8185 "\tf();\n" 8186 "\tg();\n" 8187 "}", 8188 format("void f() {\n" 8189 "\tf();\n" 8190 "\tg();\n" 8191 "}", 8192 16, 0, Tab)); 8193 EXPECT_EQ("void f() {\n" 8194 " \tf();\n" 8195 "\tg();\n" 8196 "}", 8197 format("void f() {\n" 8198 " \tf();\n" 8199 " \tg();\n" 8200 "}", 8201 21, 0, Tab)); 8202 8203 Tab.TabWidth = 4; 8204 Tab.IndentWidth = 8; 8205 verifyFormat("class TabWidth4Indent8 {\n" 8206 "\t\tvoid f() {\n" 8207 "\t\t\t\tsomeFunction(parameter1,\n" 8208 "\t\t\t\t\t\t\t parameter2);\n" 8209 "\t\t}\n" 8210 "};", 8211 Tab); 8212 8213 Tab.TabWidth = 4; 8214 Tab.IndentWidth = 4; 8215 verifyFormat("class TabWidth4Indent4 {\n" 8216 "\tvoid f() {\n" 8217 "\t\tsomeFunction(parameter1,\n" 8218 "\t\t\t\t\t parameter2);\n" 8219 "\t}\n" 8220 "};", 8221 Tab); 8222 8223 Tab.TabWidth = 8; 8224 Tab.IndentWidth = 4; 8225 verifyFormat("class TabWidth8Indent4 {\n" 8226 " void f() {\n" 8227 "\tsomeFunction(parameter1,\n" 8228 "\t\t parameter2);\n" 8229 " }\n" 8230 "};", 8231 Tab); 8232 8233 Tab.TabWidth = 8; 8234 Tab.IndentWidth = 8; 8235 EXPECT_EQ("/*\n" 8236 "\t a\t\tcomment\n" 8237 "\t in multiple lines\n" 8238 " */", 8239 format(" /*\t \t \n" 8240 " \t \t a\t\tcomment\t \t\n" 8241 " \t \t in multiple lines\t\n" 8242 " \t */", 8243 Tab)); 8244 8245 Tab.UseTab = FormatStyle::UT_ForIndentation; 8246 verifyFormat("{\n" 8247 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8248 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8249 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8250 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8251 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8252 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8253 "};", 8254 Tab); 8255 verifyFormat("enum A {\n" 8256 "\ta1, // Force multiple lines\n" 8257 "\ta2,\n" 8258 "\ta3\n" 8259 "};", 8260 Tab); 8261 EXPECT_EQ("if (aaaaaaaa && // q\n" 8262 " bb) // w\n" 8263 "\t;", 8264 format("if (aaaaaaaa &&// q\n" 8265 "bb)// w\n" 8266 ";", 8267 Tab)); 8268 verifyFormat("class X {\n" 8269 "\tvoid f() {\n" 8270 "\t\tsomeFunction(parameter1,\n" 8271 "\t\t parameter2);\n" 8272 "\t}\n" 8273 "};", 8274 Tab); 8275 verifyFormat("{\n" 8276 "\tQ({\n" 8277 "\t\tint a;\n" 8278 "\t\tsomeFunction(aaaaaaaa,\n" 8279 "\t\t bbbbbbb);\n" 8280 "\t}, p);\n" 8281 "}", 8282 Tab); 8283 EXPECT_EQ("{\n" 8284 "\t/* aaaa\n" 8285 "\t bbbb */\n" 8286 "}", 8287 format("{\n" 8288 "/* aaaa\n" 8289 " bbbb */\n" 8290 "}", 8291 Tab)); 8292 EXPECT_EQ("{\n" 8293 "\t/*\n" 8294 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8295 "\t bbbbbbbbbbbbb\n" 8296 "\t*/\n" 8297 "}", 8298 format("{\n" 8299 "/*\n" 8300 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8301 "*/\n" 8302 "}", 8303 Tab)); 8304 EXPECT_EQ("{\n" 8305 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8306 "\t// bbbbbbbbbbbbb\n" 8307 "}", 8308 format("{\n" 8309 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8310 "}", 8311 Tab)); 8312 EXPECT_EQ("{\n" 8313 "\t/*\n" 8314 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8315 "\t bbbbbbbbbbbbb\n" 8316 "\t*/\n" 8317 "}", 8318 format("{\n" 8319 "\t/*\n" 8320 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8321 "\t*/\n" 8322 "}", 8323 Tab)); 8324 EXPECT_EQ("{\n" 8325 "\t/*\n" 8326 "\n" 8327 "\t*/\n" 8328 "}", 8329 format("{\n" 8330 "\t/*\n" 8331 "\n" 8332 "\t*/\n" 8333 "}", 8334 Tab)); 8335 EXPECT_EQ("{\n" 8336 "\t/*\n" 8337 " asdf\n" 8338 "\t*/\n" 8339 "}", 8340 format("{\n" 8341 "\t/*\n" 8342 " asdf\n" 8343 "\t*/\n" 8344 "}", 8345 Tab)); 8346 8347 Tab.UseTab = FormatStyle::UT_Never; 8348 EXPECT_EQ("/*\n" 8349 " a\t\tcomment\n" 8350 " in multiple lines\n" 8351 " */", 8352 format(" /*\t \t \n" 8353 " \t \t a\t\tcomment\t \t\n" 8354 " \t \t in multiple lines\t\n" 8355 " \t */", 8356 Tab)); 8357 EXPECT_EQ("/* some\n" 8358 " comment */", 8359 format(" \t \t /* some\n" 8360 " \t \t comment */", 8361 Tab)); 8362 EXPECT_EQ("int a; /* some\n" 8363 " comment */", 8364 format(" \t \t int a; /* some\n" 8365 " \t \t comment */", 8366 Tab)); 8367 8368 EXPECT_EQ("int a; /* some\n" 8369 "comment */", 8370 format(" \t \t int\ta; /* some\n" 8371 " \t \t comment */", 8372 Tab)); 8373 EXPECT_EQ("f(\"\t\t\"); /* some\n" 8374 " comment */", 8375 format(" \t \t f(\"\t\t\"); /* some\n" 8376 " \t \t comment */", 8377 Tab)); 8378 EXPECT_EQ("{\n" 8379 " /*\n" 8380 " * Comment\n" 8381 " */\n" 8382 " int i;\n" 8383 "}", 8384 format("{\n" 8385 "\t/*\n" 8386 "\t * Comment\n" 8387 "\t */\n" 8388 "\t int i;\n" 8389 "}")); 8390 } 8391 8392 TEST_F(FormatTest, CalculatesOriginalColumn) { 8393 EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8394 "q\"; /* some\n" 8395 " comment */", 8396 format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8397 "q\"; /* some\n" 8398 " comment */", 8399 getLLVMStyle())); 8400 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 8401 "/* some\n" 8402 " comment */", 8403 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 8404 " /* some\n" 8405 " comment */", 8406 getLLVMStyle())); 8407 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8408 "qqq\n" 8409 "/* some\n" 8410 " comment */", 8411 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8412 "qqq\n" 8413 " /* some\n" 8414 " comment */", 8415 getLLVMStyle())); 8416 EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8417 "wwww; /* some\n" 8418 " comment */", 8419 format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8420 "wwww; /* some\n" 8421 " comment */", 8422 getLLVMStyle())); 8423 } 8424 8425 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { 8426 FormatStyle NoSpace = getLLVMStyle(); 8427 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never; 8428 8429 verifyFormat("while(true)\n" 8430 " continue;", 8431 NoSpace); 8432 verifyFormat("for(;;)\n" 8433 " continue;", 8434 NoSpace); 8435 verifyFormat("if(true)\n" 8436 " f();\n" 8437 "else if(true)\n" 8438 " f();", 8439 NoSpace); 8440 verifyFormat("do {\n" 8441 " do_something();\n" 8442 "} while(something());", 8443 NoSpace); 8444 verifyFormat("switch(x) {\n" 8445 "default:\n" 8446 " break;\n" 8447 "}", 8448 NoSpace); 8449 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace); 8450 verifyFormat("size_t x = sizeof(x);", NoSpace); 8451 verifyFormat("auto f(int x) -> decltype(x);", NoSpace); 8452 verifyFormat("int f(T x) noexcept(x.create());", NoSpace); 8453 verifyFormat("alignas(128) char a[128];", NoSpace); 8454 verifyFormat("size_t x = alignof(MyType);", NoSpace); 8455 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace); 8456 verifyFormat("int f() throw(Deprecated);", NoSpace); 8457 verifyFormat("typedef void (*cb)(int);", NoSpace); 8458 8459 FormatStyle Space = getLLVMStyle(); 8460 Space.SpaceBeforeParens = FormatStyle::SBPO_Always; 8461 8462 verifyFormat("int f ();", Space); 8463 verifyFormat("void f (int a, T b) {\n" 8464 " while (true)\n" 8465 " continue;\n" 8466 "}", 8467 Space); 8468 verifyFormat("if (true)\n" 8469 " f ();\n" 8470 "else if (true)\n" 8471 " f ();", 8472 Space); 8473 verifyFormat("do {\n" 8474 " do_something ();\n" 8475 "} while (something ());", 8476 Space); 8477 verifyFormat("switch (x) {\n" 8478 "default:\n" 8479 " break;\n" 8480 "}", 8481 Space); 8482 verifyFormat("A::A () : a (1) {}", Space); 8483 verifyFormat("void f () __attribute__ ((asdf));", Space); 8484 verifyFormat("*(&a + 1);\n" 8485 "&((&a)[1]);\n" 8486 "a[(b + c) * d];\n" 8487 "(((a + 1) * 2) + 3) * 4;", 8488 Space); 8489 verifyFormat("#define A(x) x", Space); 8490 verifyFormat("#define A (x) x", Space); 8491 verifyFormat("#if defined(x)\n" 8492 "#endif", 8493 Space); 8494 verifyFormat("auto i = std::make_unique<int> (5);", Space); 8495 verifyFormat("size_t x = sizeof (x);", Space); 8496 verifyFormat("auto f (int x) -> decltype (x);", Space); 8497 verifyFormat("int f (T x) noexcept (x.create ());", Space); 8498 verifyFormat("alignas (128) char a[128];", Space); 8499 verifyFormat("size_t x = alignof (MyType);", Space); 8500 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space); 8501 verifyFormat("int f () throw (Deprecated);", Space); 8502 verifyFormat("typedef void (*cb) (int);", Space); 8503 } 8504 8505 TEST_F(FormatTest, ConfigurableSpacesInParentheses) { 8506 FormatStyle Spaces = getLLVMStyle(); 8507 8508 Spaces.SpacesInParentheses = true; 8509 verifyFormat("call( x, y, z );", Spaces); 8510 verifyFormat("call();", Spaces); 8511 verifyFormat("std::function<void( int, int )> callback;", Spaces); 8512 verifyFormat("while ( (bool)1 )\n" 8513 " continue;", 8514 Spaces); 8515 verifyFormat("for ( ;; )\n" 8516 " continue;", 8517 Spaces); 8518 verifyFormat("if ( true )\n" 8519 " f();\n" 8520 "else if ( true )\n" 8521 " f();", 8522 Spaces); 8523 verifyFormat("do {\n" 8524 " do_something( (int)i );\n" 8525 "} while ( something() );", 8526 Spaces); 8527 verifyFormat("switch ( x ) {\n" 8528 "default:\n" 8529 " break;\n" 8530 "}", 8531 Spaces); 8532 8533 Spaces.SpacesInParentheses = false; 8534 Spaces.SpacesInCStyleCastParentheses = true; 8535 verifyFormat("Type *A = ( Type * )P;", Spaces); 8536 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces); 8537 verifyFormat("x = ( int32 )y;", Spaces); 8538 verifyFormat("int a = ( int )(2.0f);", Spaces); 8539 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces); 8540 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces); 8541 verifyFormat("#define x (( int )-1)", Spaces); 8542 8543 // Run the first set of tests again with: 8544 Spaces.SpacesInParentheses = false, Spaces.SpaceInEmptyParentheses = true; 8545 Spaces.SpacesInCStyleCastParentheses = true; 8546 verifyFormat("call(x, y, z);", Spaces); 8547 verifyFormat("call( );", Spaces); 8548 verifyFormat("std::function<void(int, int)> callback;", Spaces); 8549 verifyFormat("while (( bool )1)\n" 8550 " continue;", 8551 Spaces); 8552 verifyFormat("for (;;)\n" 8553 " continue;", 8554 Spaces); 8555 verifyFormat("if (true)\n" 8556 " f( );\n" 8557 "else if (true)\n" 8558 " f( );", 8559 Spaces); 8560 verifyFormat("do {\n" 8561 " do_something(( int )i);\n" 8562 "} while (something( ));", 8563 Spaces); 8564 verifyFormat("switch (x) {\n" 8565 "default:\n" 8566 " break;\n" 8567 "}", 8568 Spaces); 8569 8570 // Run the first set of tests again with: 8571 Spaces.SpaceAfterCStyleCast = true; 8572 verifyFormat("call(x, y, z);", Spaces); 8573 verifyFormat("call( );", Spaces); 8574 verifyFormat("std::function<void(int, int)> callback;", Spaces); 8575 verifyFormat("while (( bool ) 1)\n" 8576 " continue;", 8577 Spaces); 8578 verifyFormat("for (;;)\n" 8579 " continue;", 8580 Spaces); 8581 verifyFormat("if (true)\n" 8582 " f( );\n" 8583 "else if (true)\n" 8584 " f( );", 8585 Spaces); 8586 verifyFormat("do {\n" 8587 " do_something(( int ) i);\n" 8588 "} while (something( ));", 8589 Spaces); 8590 verifyFormat("switch (x) {\n" 8591 "default:\n" 8592 " break;\n" 8593 "}", 8594 Spaces); 8595 8596 // Run subset of tests again with: 8597 Spaces.SpacesInCStyleCastParentheses = false; 8598 Spaces.SpaceAfterCStyleCast = true; 8599 verifyFormat("while ((bool) 1)\n" 8600 " continue;", 8601 Spaces); 8602 verifyFormat("do {\n" 8603 " do_something((int) i);\n" 8604 "} while (something( ));", 8605 Spaces); 8606 } 8607 8608 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) { 8609 verifyFormat("int a[5];"); 8610 verifyFormat("a[3] += 42;"); 8611 8612 FormatStyle Spaces = getLLVMStyle(); 8613 Spaces.SpacesInSquareBrackets = true; 8614 // Lambdas unchanged. 8615 verifyFormat("int c = []() -> int { return 2; }();\n", Spaces); 8616 verifyFormat("return [i, args...] {};", Spaces); 8617 8618 // Not lambdas. 8619 verifyFormat("int a[ 5 ];", Spaces); 8620 verifyFormat("a[ 3 ] += 42;", Spaces); 8621 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces); 8622 verifyFormat("double &operator[](int i) { return 0; }\n" 8623 "int i;", 8624 Spaces); 8625 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces); 8626 verifyFormat("int i = a[ a ][ a ]->f();", Spaces); 8627 verifyFormat("int i = (*b)[ a ]->f();", Spaces); 8628 } 8629 8630 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) { 8631 verifyFormat("int a = 5;"); 8632 verifyFormat("a += 42;"); 8633 verifyFormat("a or_eq 8;"); 8634 8635 FormatStyle Spaces = getLLVMStyle(); 8636 Spaces.SpaceBeforeAssignmentOperators = false; 8637 verifyFormat("int a= 5;", Spaces); 8638 verifyFormat("a+= 42;", Spaces); 8639 verifyFormat("a or_eq 8;", Spaces); 8640 } 8641 8642 TEST_F(FormatTest, AlignConsecutiveAssignments) { 8643 FormatStyle Alignment = getLLVMStyle(); 8644 Alignment.AlignConsecutiveAssignments = false; 8645 verifyFormat("int a = 5;\n" 8646 "int oneTwoThree = 123;", 8647 Alignment); 8648 verifyFormat("int a = 5;\n" 8649 "int oneTwoThree = 123;", 8650 Alignment); 8651 8652 Alignment.AlignConsecutiveAssignments = true; 8653 verifyFormat("int a = 5;\n" 8654 "int oneTwoThree = 123;", 8655 Alignment); 8656 verifyFormat("int a = method();\n" 8657 "int oneTwoThree = 133;", 8658 Alignment); 8659 verifyFormat("a &= 5;\n" 8660 "bcd *= 5;\n" 8661 "ghtyf += 5;\n" 8662 "dvfvdb -= 5;\n" 8663 "a /= 5;\n" 8664 "vdsvsv %= 5;\n" 8665 "sfdbddfbdfbb ^= 5;\n" 8666 "dvsdsv |= 5;\n" 8667 "int dsvvdvsdvvv = 123;", 8668 Alignment); 8669 verifyFormat("int i = 1, j = 10;\n" 8670 "something = 2000;", 8671 Alignment); 8672 verifyFormat("something = 2000;\n" 8673 "int i = 1, j = 10;\n", 8674 Alignment); 8675 verifyFormat("something = 2000;\n" 8676 "another = 911;\n" 8677 "int i = 1, j = 10;\n" 8678 "oneMore = 1;\n" 8679 "i = 2;", 8680 Alignment); 8681 verifyFormat("int a = 5;\n" 8682 "int one = 1;\n" 8683 "method();\n" 8684 "int oneTwoThree = 123;\n" 8685 "int oneTwo = 12;", 8686 Alignment); 8687 verifyFormat("int oneTwoThree = 123; // comment\n" 8688 "int oneTwo = 12; // comment", 8689 Alignment); 8690 EXPECT_EQ("int a = 5;\n" 8691 "\n" 8692 "int oneTwoThree = 123;", 8693 format("int a = 5;\n" 8694 "\n" 8695 "int oneTwoThree= 123;", 8696 Alignment)); 8697 EXPECT_EQ("int a = 5;\n" 8698 "int one = 1;\n" 8699 "\n" 8700 "int oneTwoThree = 123;", 8701 format("int a = 5;\n" 8702 "int one = 1;\n" 8703 "\n" 8704 "int oneTwoThree = 123;", 8705 Alignment)); 8706 EXPECT_EQ("int a = 5;\n" 8707 "int one = 1;\n" 8708 "\n" 8709 "int oneTwoThree = 123;\n" 8710 "int oneTwo = 12;", 8711 format("int a = 5;\n" 8712 "int one = 1;\n" 8713 "\n" 8714 "int oneTwoThree = 123;\n" 8715 "int oneTwo = 12;", 8716 Alignment)); 8717 Alignment.AlignEscapedNewlinesLeft = true; 8718 verifyFormat("#define A \\\n" 8719 " int aaaa = 12; \\\n" 8720 " int b = 23; \\\n" 8721 " int ccc = 234; \\\n" 8722 " int dddddddddd = 2345;", 8723 Alignment); 8724 Alignment.AlignEscapedNewlinesLeft = false; 8725 verifyFormat("#define A " 8726 " \\\n" 8727 " int aaaa = 12; " 8728 " \\\n" 8729 " int b = 23; " 8730 " \\\n" 8731 " int ccc = 234; " 8732 " \\\n" 8733 " int dddddddddd = 2345;", 8734 Alignment); 8735 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 8736 "k = 4, int l = 5,\n" 8737 " int m = 6) {\n" 8738 " int j = 10;\n" 8739 " otherThing = 1;\n" 8740 "}", 8741 Alignment); 8742 verifyFormat("void SomeFunction(int parameter = 0) {\n" 8743 " int i = 1;\n" 8744 " int j = 2;\n" 8745 " int big = 10000;\n" 8746 "}", 8747 Alignment); 8748 verifyFormat("class C {\n" 8749 "public:\n" 8750 " int i = 1;\n" 8751 " virtual void f() = 0;\n" 8752 "};", 8753 Alignment); 8754 verifyFormat("int i = 1;\n" 8755 "if (SomeType t = getSomething()) {\n" 8756 "}\n" 8757 "int j = 2;\n" 8758 "int big = 10000;", 8759 Alignment); 8760 verifyFormat("int j = 7;\n" 8761 "for (int k = 0; k < N; ++k) {\n" 8762 "}\n" 8763 "int j = 2;\n" 8764 "int big = 10000;\n" 8765 "}", 8766 Alignment); 8767 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 8768 verifyFormat("int i = 1;\n" 8769 "LooooooooooongType loooooooooooooooooooooongVariable\n" 8770 " = someLooooooooooooooooongFunction();\n" 8771 "int j = 2;", 8772 Alignment); 8773 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 8774 verifyFormat("int i = 1;\n" 8775 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 8776 " someLooooooooooooooooongFunction();\n" 8777 "int j = 2;", 8778 Alignment); 8779 // FIXME: Should align all three assignments 8780 verifyFormat( 8781 "int i = 1;\n" 8782 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 8783 " loooooooooooooooooooooongParameterB);\n" 8784 "int j = 2;", 8785 Alignment); 8786 } 8787 8788 TEST_F(FormatTest, LinuxBraceBreaking) { 8789 FormatStyle LinuxBraceStyle = getLLVMStyle(); 8790 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux; 8791 verifyFormat("namespace a\n" 8792 "{\n" 8793 "class A\n" 8794 "{\n" 8795 " void f()\n" 8796 " {\n" 8797 " if (true) {\n" 8798 " a();\n" 8799 " b();\n" 8800 " }\n" 8801 " }\n" 8802 " void g() { return; }\n" 8803 "};\n" 8804 "struct B {\n" 8805 " int x;\n" 8806 "};\n" 8807 "}\n", 8808 LinuxBraceStyle); 8809 verifyFormat("enum X {\n" 8810 " Y = 0,\n" 8811 "}\n", 8812 LinuxBraceStyle); 8813 verifyFormat("struct S {\n" 8814 " int Type;\n" 8815 " union {\n" 8816 " int x;\n" 8817 " double y;\n" 8818 " } Value;\n" 8819 " class C\n" 8820 " {\n" 8821 " MyFavoriteType Value;\n" 8822 " } Class;\n" 8823 "}\n", 8824 LinuxBraceStyle); 8825 } 8826 8827 TEST_F(FormatTest, StroustrupBraceBreaking) { 8828 FormatStyle StroustrupBraceStyle = getLLVMStyle(); 8829 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 8830 verifyFormat("namespace a {\n" 8831 "class A {\n" 8832 " void f()\n" 8833 " {\n" 8834 " if (true) {\n" 8835 " a();\n" 8836 " b();\n" 8837 " }\n" 8838 " }\n" 8839 " void g() { return; }\n" 8840 "};\n" 8841 "struct B {\n" 8842 " int x;\n" 8843 "};\n" 8844 "}\n", 8845 StroustrupBraceStyle); 8846 8847 verifyFormat("void foo()\n" 8848 "{\n" 8849 " if (a) {\n" 8850 " a();\n" 8851 " }\n" 8852 " else {\n" 8853 " b();\n" 8854 " }\n" 8855 "}\n", 8856 StroustrupBraceStyle); 8857 8858 verifyFormat("#ifdef _DEBUG\n" 8859 "int foo(int i = 0)\n" 8860 "#else\n" 8861 "int foo(int i = 5)\n" 8862 "#endif\n" 8863 "{\n" 8864 " return i;\n" 8865 "}", 8866 StroustrupBraceStyle); 8867 8868 verifyFormat("void foo() {}\n" 8869 "void bar()\n" 8870 "#ifdef _DEBUG\n" 8871 "{\n" 8872 " foo();\n" 8873 "}\n" 8874 "#else\n" 8875 "{\n" 8876 "}\n" 8877 "#endif", 8878 StroustrupBraceStyle); 8879 8880 verifyFormat("void foobar() { int i = 5; }\n" 8881 "#ifdef _DEBUG\n" 8882 "void bar() {}\n" 8883 "#else\n" 8884 "void bar() { foobar(); }\n" 8885 "#endif", 8886 StroustrupBraceStyle); 8887 } 8888 8889 TEST_F(FormatTest, AllmanBraceBreaking) { 8890 FormatStyle AllmanBraceStyle = getLLVMStyle(); 8891 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman; 8892 verifyFormat("namespace a\n" 8893 "{\n" 8894 "class A\n" 8895 "{\n" 8896 " void f()\n" 8897 " {\n" 8898 " if (true)\n" 8899 " {\n" 8900 " a();\n" 8901 " b();\n" 8902 " }\n" 8903 " }\n" 8904 " void g() { return; }\n" 8905 "};\n" 8906 "struct B\n" 8907 "{\n" 8908 " int x;\n" 8909 "};\n" 8910 "}", 8911 AllmanBraceStyle); 8912 8913 verifyFormat("void f()\n" 8914 "{\n" 8915 " if (true)\n" 8916 " {\n" 8917 " a();\n" 8918 " }\n" 8919 " else if (false)\n" 8920 " {\n" 8921 " b();\n" 8922 " }\n" 8923 " else\n" 8924 " {\n" 8925 " c();\n" 8926 " }\n" 8927 "}\n", 8928 AllmanBraceStyle); 8929 8930 verifyFormat("void f()\n" 8931 "{\n" 8932 " for (int i = 0; i < 10; ++i)\n" 8933 " {\n" 8934 " a();\n" 8935 " }\n" 8936 " while (false)\n" 8937 " {\n" 8938 " b();\n" 8939 " }\n" 8940 " do\n" 8941 " {\n" 8942 " c();\n" 8943 " } while (false)\n" 8944 "}\n", 8945 AllmanBraceStyle); 8946 8947 verifyFormat("void f(int a)\n" 8948 "{\n" 8949 " switch (a)\n" 8950 " {\n" 8951 " case 0:\n" 8952 " break;\n" 8953 " case 1:\n" 8954 " {\n" 8955 " break;\n" 8956 " }\n" 8957 " case 2:\n" 8958 " {\n" 8959 " }\n" 8960 " break;\n" 8961 " default:\n" 8962 " break;\n" 8963 " }\n" 8964 "}\n", 8965 AllmanBraceStyle); 8966 8967 verifyFormat("enum X\n" 8968 "{\n" 8969 " Y = 0,\n" 8970 "}\n", 8971 AllmanBraceStyle); 8972 verifyFormat("enum X\n" 8973 "{\n" 8974 " Y = 0\n" 8975 "}\n", 8976 AllmanBraceStyle); 8977 8978 verifyFormat("@interface BSApplicationController ()\n" 8979 "{\n" 8980 "@private\n" 8981 " id _extraIvar;\n" 8982 "}\n" 8983 "@end\n", 8984 AllmanBraceStyle); 8985 8986 verifyFormat("#ifdef _DEBUG\n" 8987 "int foo(int i = 0)\n" 8988 "#else\n" 8989 "int foo(int i = 5)\n" 8990 "#endif\n" 8991 "{\n" 8992 " return i;\n" 8993 "}", 8994 AllmanBraceStyle); 8995 8996 verifyFormat("void foo() {}\n" 8997 "void bar()\n" 8998 "#ifdef _DEBUG\n" 8999 "{\n" 9000 " foo();\n" 9001 "}\n" 9002 "#else\n" 9003 "{\n" 9004 "}\n" 9005 "#endif", 9006 AllmanBraceStyle); 9007 9008 verifyFormat("void foobar() { int i = 5; }\n" 9009 "#ifdef _DEBUG\n" 9010 "void bar() {}\n" 9011 "#else\n" 9012 "void bar() { foobar(); }\n" 9013 "#endif", 9014 AllmanBraceStyle); 9015 9016 // This shouldn't affect ObjC blocks.. 9017 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 9018 " // ...\n" 9019 " int i;\n" 9020 "}];", 9021 AllmanBraceStyle); 9022 verifyFormat("void (^block)(void) = ^{\n" 9023 " // ...\n" 9024 " int i;\n" 9025 "};", 9026 AllmanBraceStyle); 9027 // .. or dict literals. 9028 verifyFormat("void f()\n" 9029 "{\n" 9030 " [object someMethod:@{ @\"a\" : @\"b\" }];\n" 9031 "}", 9032 AllmanBraceStyle); 9033 verifyFormat("int f()\n" 9034 "{ // comment\n" 9035 " return 42;\n" 9036 "}", 9037 AllmanBraceStyle); 9038 9039 AllmanBraceStyle.ColumnLimit = 19; 9040 verifyFormat("void f() { int i; }", AllmanBraceStyle); 9041 AllmanBraceStyle.ColumnLimit = 18; 9042 verifyFormat("void f()\n" 9043 "{\n" 9044 " int i;\n" 9045 "}", 9046 AllmanBraceStyle); 9047 AllmanBraceStyle.ColumnLimit = 80; 9048 9049 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle; 9050 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true; 9051 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 9052 verifyFormat("void f(bool b)\n" 9053 "{\n" 9054 " if (b)\n" 9055 " {\n" 9056 " return;\n" 9057 " }\n" 9058 "}\n", 9059 BreakBeforeBraceShortIfs); 9060 verifyFormat("void f(bool b)\n" 9061 "{\n" 9062 " if (b) return;\n" 9063 "}\n", 9064 BreakBeforeBraceShortIfs); 9065 verifyFormat("void f(bool b)\n" 9066 "{\n" 9067 " while (b)\n" 9068 " {\n" 9069 " return;\n" 9070 " }\n" 9071 "}\n", 9072 BreakBeforeBraceShortIfs); 9073 } 9074 9075 TEST_F(FormatTest, GNUBraceBreaking) { 9076 FormatStyle GNUBraceStyle = getLLVMStyle(); 9077 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU; 9078 verifyFormat("namespace a\n" 9079 "{\n" 9080 "class A\n" 9081 "{\n" 9082 " void f()\n" 9083 " {\n" 9084 " int a;\n" 9085 " {\n" 9086 " int b;\n" 9087 " }\n" 9088 " if (true)\n" 9089 " {\n" 9090 " a();\n" 9091 " b();\n" 9092 " }\n" 9093 " }\n" 9094 " void g() { return; }\n" 9095 "}\n" 9096 "}", 9097 GNUBraceStyle); 9098 9099 verifyFormat("void f()\n" 9100 "{\n" 9101 " if (true)\n" 9102 " {\n" 9103 " a();\n" 9104 " }\n" 9105 " else if (false)\n" 9106 " {\n" 9107 " b();\n" 9108 " }\n" 9109 " else\n" 9110 " {\n" 9111 " c();\n" 9112 " }\n" 9113 "}\n", 9114 GNUBraceStyle); 9115 9116 verifyFormat("void f()\n" 9117 "{\n" 9118 " for (int i = 0; i < 10; ++i)\n" 9119 " {\n" 9120 " a();\n" 9121 " }\n" 9122 " while (false)\n" 9123 " {\n" 9124 " b();\n" 9125 " }\n" 9126 " do\n" 9127 " {\n" 9128 " c();\n" 9129 " }\n" 9130 " while (false);\n" 9131 "}\n", 9132 GNUBraceStyle); 9133 9134 verifyFormat("void f(int a)\n" 9135 "{\n" 9136 " switch (a)\n" 9137 " {\n" 9138 " case 0:\n" 9139 " break;\n" 9140 " case 1:\n" 9141 " {\n" 9142 " break;\n" 9143 " }\n" 9144 " case 2:\n" 9145 " {\n" 9146 " }\n" 9147 " break;\n" 9148 " default:\n" 9149 " break;\n" 9150 " }\n" 9151 "}\n", 9152 GNUBraceStyle); 9153 9154 verifyFormat("enum X\n" 9155 "{\n" 9156 " Y = 0,\n" 9157 "}\n", 9158 GNUBraceStyle); 9159 9160 verifyFormat("@interface BSApplicationController ()\n" 9161 "{\n" 9162 "@private\n" 9163 " id _extraIvar;\n" 9164 "}\n" 9165 "@end\n", 9166 GNUBraceStyle); 9167 9168 verifyFormat("#ifdef _DEBUG\n" 9169 "int foo(int i = 0)\n" 9170 "#else\n" 9171 "int foo(int i = 5)\n" 9172 "#endif\n" 9173 "{\n" 9174 " return i;\n" 9175 "}", 9176 GNUBraceStyle); 9177 9178 verifyFormat("void foo() {}\n" 9179 "void bar()\n" 9180 "#ifdef _DEBUG\n" 9181 "{\n" 9182 " foo();\n" 9183 "}\n" 9184 "#else\n" 9185 "{\n" 9186 "}\n" 9187 "#endif", 9188 GNUBraceStyle); 9189 9190 verifyFormat("void foobar() { int i = 5; }\n" 9191 "#ifdef _DEBUG\n" 9192 "void bar() {}\n" 9193 "#else\n" 9194 "void bar() { foobar(); }\n" 9195 "#endif", 9196 GNUBraceStyle); 9197 } 9198 TEST_F(FormatTest, CatchExceptionReferenceBinding) { 9199 verifyFormat("void f() {\n" 9200 " try {\n" 9201 " } catch (const Exception &e) {\n" 9202 " }\n" 9203 "}\n", 9204 getLLVMStyle()); 9205 } 9206 9207 TEST_F(FormatTest, UnderstandsPragmas) { 9208 verifyFormat("#pragma omp reduction(| : var)"); 9209 verifyFormat("#pragma omp reduction(+ : var)"); 9210 9211 EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string " 9212 "(including parentheses).", 9213 format("#pragma mark Any non-hyphenated or hyphenated string " 9214 "(including parentheses).")); 9215 } 9216 9217 TEST_F(FormatTest, UnderstandPragmaOption) { 9218 verifyFormat("#pragma option -C -A"); 9219 9220 EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A")); 9221 } 9222 9223 #define EXPECT_ALL_STYLES_EQUAL(Styles) \ 9224 for (size_t i = 1; i < Styles.size(); ++i) \ 9225 EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \ 9226 << " differs from Style #0" 9227 9228 TEST_F(FormatTest, GetsPredefinedStyleByName) { 9229 SmallVector<FormatStyle, 3> Styles; 9230 Styles.resize(3); 9231 9232 Styles[0] = getLLVMStyle(); 9233 EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1])); 9234 EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2])); 9235 EXPECT_ALL_STYLES_EQUAL(Styles); 9236 9237 Styles[0] = getGoogleStyle(); 9238 EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1])); 9239 EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2])); 9240 EXPECT_ALL_STYLES_EQUAL(Styles); 9241 9242 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 9243 EXPECT_TRUE( 9244 getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1])); 9245 EXPECT_TRUE( 9246 getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2])); 9247 EXPECT_ALL_STYLES_EQUAL(Styles); 9248 9249 Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp); 9250 EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1])); 9251 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2])); 9252 EXPECT_ALL_STYLES_EQUAL(Styles); 9253 9254 Styles[0] = getMozillaStyle(); 9255 EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1])); 9256 EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2])); 9257 EXPECT_ALL_STYLES_EQUAL(Styles); 9258 9259 Styles[0] = getWebKitStyle(); 9260 EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1])); 9261 EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2])); 9262 EXPECT_ALL_STYLES_EQUAL(Styles); 9263 9264 Styles[0] = getGNUStyle(); 9265 EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1])); 9266 EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2])); 9267 EXPECT_ALL_STYLES_EQUAL(Styles); 9268 9269 EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0])); 9270 } 9271 9272 TEST_F(FormatTest, GetsCorrectBasedOnStyle) { 9273 SmallVector<FormatStyle, 8> Styles; 9274 Styles.resize(2); 9275 9276 Styles[0] = getGoogleStyle(); 9277 Styles[1] = getLLVMStyle(); 9278 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 9279 EXPECT_ALL_STYLES_EQUAL(Styles); 9280 9281 Styles.resize(5); 9282 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 9283 Styles[1] = getLLVMStyle(); 9284 Styles[1].Language = FormatStyle::LK_JavaScript; 9285 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 9286 9287 Styles[2] = getLLVMStyle(); 9288 Styles[2].Language = FormatStyle::LK_JavaScript; 9289 EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n" 9290 "BasedOnStyle: Google", 9291 &Styles[2]).value()); 9292 9293 Styles[3] = getLLVMStyle(); 9294 Styles[3].Language = FormatStyle::LK_JavaScript; 9295 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n" 9296 "Language: JavaScript", 9297 &Styles[3]).value()); 9298 9299 Styles[4] = getLLVMStyle(); 9300 Styles[4].Language = FormatStyle::LK_JavaScript; 9301 EXPECT_EQ(0, parseConfiguration("---\n" 9302 "BasedOnStyle: LLVM\n" 9303 "IndentWidth: 123\n" 9304 "---\n" 9305 "BasedOnStyle: Google\n" 9306 "Language: JavaScript", 9307 &Styles[4]).value()); 9308 EXPECT_ALL_STYLES_EQUAL(Styles); 9309 } 9310 9311 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \ 9312 Style.FIELD = false; \ 9313 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \ 9314 EXPECT_TRUE(Style.FIELD); \ 9315 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \ 9316 EXPECT_FALSE(Style.FIELD); 9317 9318 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD) 9319 9320 #define CHECK_PARSE(TEXT, FIELD, VALUE) \ 9321 EXPECT_NE(VALUE, Style.FIELD); \ 9322 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \ 9323 EXPECT_EQ(VALUE, Style.FIELD) 9324 9325 TEST_F(FormatTest, ParsesConfigurationBools) { 9326 FormatStyle Style = {}; 9327 Style.Language = FormatStyle::LK_Cpp; 9328 CHECK_PARSE_BOOL(AlignAfterOpenBracket); 9329 CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft); 9330 CHECK_PARSE_BOOL(AlignOperands); 9331 CHECK_PARSE_BOOL(AlignTrailingComments); 9332 CHECK_PARSE_BOOL(AlignConsecutiveAssignments); 9333 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); 9334 CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine); 9335 CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine); 9336 CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine); 9337 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); 9338 CHECK_PARSE_BOOL(AlwaysBreakAfterDefinitionReturnType); 9339 CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations); 9340 CHECK_PARSE_BOOL(BinPackParameters); 9341 CHECK_PARSE_BOOL(BinPackArguments); 9342 CHECK_PARSE_BOOL(BreakBeforeTernaryOperators); 9343 CHECK_PARSE_BOOL(BreakConstructorInitializersBeforeComma); 9344 CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine); 9345 CHECK_PARSE_BOOL(DerivePointerAlignment); 9346 CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding"); 9347 CHECK_PARSE_BOOL(IndentCaseLabels); 9348 CHECK_PARSE_BOOL(IndentWrappedFunctionNames); 9349 CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks); 9350 CHECK_PARSE_BOOL(ObjCSpaceAfterProperty); 9351 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList); 9352 CHECK_PARSE_BOOL(Cpp11BracedListStyle); 9353 CHECK_PARSE_BOOL(SpacesInParentheses); 9354 CHECK_PARSE_BOOL(SpacesInSquareBrackets); 9355 CHECK_PARSE_BOOL(SpacesInAngles); 9356 CHECK_PARSE_BOOL(SpaceInEmptyParentheses); 9357 CHECK_PARSE_BOOL(SpacesInContainerLiterals); 9358 CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); 9359 CHECK_PARSE_BOOL(SpaceAfterCStyleCast); 9360 CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); 9361 } 9362 9363 #undef CHECK_PARSE_BOOL 9364 9365 TEST_F(FormatTest, ParsesConfiguration) { 9366 FormatStyle Style = {}; 9367 Style.Language = FormatStyle::LK_Cpp; 9368 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234); 9369 CHECK_PARSE("ConstructorInitializerIndentWidth: 1234", 9370 ConstructorInitializerIndentWidth, 1234u); 9371 CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u); 9372 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); 9373 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u); 9374 CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234", 9375 PenaltyBreakBeforeFirstCallParameter, 1234u); 9376 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); 9377 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234", 9378 PenaltyReturnTypeOnItsOwnLine, 1234u); 9379 CHECK_PARSE("SpacesBeforeTrailingComments: 1234", 9380 SpacesBeforeTrailingComments, 1234u); 9381 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u); 9382 CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u); 9383 9384 Style.PointerAlignment = FormatStyle::PAS_Middle; 9385 CHECK_PARSE("PointerAlignment: Left", PointerAlignment, 9386 FormatStyle::PAS_Left); 9387 CHECK_PARSE("PointerAlignment: Right", PointerAlignment, 9388 FormatStyle::PAS_Right); 9389 CHECK_PARSE("PointerAlignment: Middle", PointerAlignment, 9390 FormatStyle::PAS_Middle); 9391 // For backward compatibility: 9392 CHECK_PARSE("PointerBindsToType: Left", PointerAlignment, 9393 FormatStyle::PAS_Left); 9394 CHECK_PARSE("PointerBindsToType: Right", PointerAlignment, 9395 FormatStyle::PAS_Right); 9396 CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment, 9397 FormatStyle::PAS_Middle); 9398 9399 Style.Standard = FormatStyle::LS_Auto; 9400 CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03); 9401 CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11); 9402 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03); 9403 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11); 9404 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto); 9405 9406 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 9407 CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment", 9408 BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment); 9409 CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators, 9410 FormatStyle::BOS_None); 9411 CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators, 9412 FormatStyle::BOS_All); 9413 // For backward compatibility: 9414 CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators, 9415 FormatStyle::BOS_None); 9416 CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators, 9417 FormatStyle::BOS_All); 9418 9419 Style.UseTab = FormatStyle::UT_ForIndentation; 9420 CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never); 9421 CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation); 9422 CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always); 9423 // For backward compatibility: 9424 CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never); 9425 CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always); 9426 9427 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 9428 CHECK_PARSE("AllowShortFunctionsOnASingleLine: None", 9429 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 9430 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline", 9431 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline); 9432 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty", 9433 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty); 9434 CHECK_PARSE("AllowShortFunctionsOnASingleLine: All", 9435 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 9436 // For backward compatibility: 9437 CHECK_PARSE("AllowShortFunctionsOnASingleLine: false", 9438 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 9439 CHECK_PARSE("AllowShortFunctionsOnASingleLine: true", 9440 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 9441 9442 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 9443 CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens, 9444 FormatStyle::SBPO_Never); 9445 CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens, 9446 FormatStyle::SBPO_Always); 9447 CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens, 9448 FormatStyle::SBPO_ControlStatements); 9449 // For backward compatibility: 9450 CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens, 9451 FormatStyle::SBPO_Never); 9452 CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens, 9453 FormatStyle::SBPO_ControlStatements); 9454 9455 Style.ColumnLimit = 123; 9456 FormatStyle BaseStyle = getLLVMStyle(); 9457 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit); 9458 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u); 9459 9460 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 9461 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces, 9462 FormatStyle::BS_Attach); 9463 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces, 9464 FormatStyle::BS_Linux); 9465 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces, 9466 FormatStyle::BS_Stroustrup); 9467 CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces, 9468 FormatStyle::BS_Allman); 9469 CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU); 9470 9471 Style.NamespaceIndentation = FormatStyle::NI_All; 9472 CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation, 9473 FormatStyle::NI_None); 9474 CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation, 9475 FormatStyle::NI_Inner); 9476 CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation, 9477 FormatStyle::NI_All); 9478 9479 Style.ForEachMacros.clear(); 9480 std::vector<std::string> BoostForeach; 9481 BoostForeach.push_back("BOOST_FOREACH"); 9482 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach); 9483 std::vector<std::string> BoostAndQForeach; 9484 BoostAndQForeach.push_back("BOOST_FOREACH"); 9485 BoostAndQForeach.push_back("Q_FOREACH"); 9486 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros, 9487 BoostAndQForeach); 9488 } 9489 9490 TEST_F(FormatTest, ParsesConfigurationWithLanguages) { 9491 FormatStyle Style = {}; 9492 Style.Language = FormatStyle::LK_Cpp; 9493 CHECK_PARSE("Language: Cpp\n" 9494 "IndentWidth: 12", 9495 IndentWidth, 12u); 9496 EXPECT_EQ(parseConfiguration("Language: JavaScript\n" 9497 "IndentWidth: 34", 9498 &Style), 9499 ParseError::Unsuitable); 9500 EXPECT_EQ(12u, Style.IndentWidth); 9501 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 9502 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 9503 9504 Style.Language = FormatStyle::LK_JavaScript; 9505 CHECK_PARSE("Language: JavaScript\n" 9506 "IndentWidth: 12", 9507 IndentWidth, 12u); 9508 CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u); 9509 EXPECT_EQ(parseConfiguration("Language: Cpp\n" 9510 "IndentWidth: 34", 9511 &Style), 9512 ParseError::Unsuitable); 9513 EXPECT_EQ(23u, Style.IndentWidth); 9514 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 9515 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 9516 9517 CHECK_PARSE("BasedOnStyle: LLVM\n" 9518 "IndentWidth: 67", 9519 IndentWidth, 67u); 9520 9521 CHECK_PARSE("---\n" 9522 "Language: JavaScript\n" 9523 "IndentWidth: 12\n" 9524 "---\n" 9525 "Language: Cpp\n" 9526 "IndentWidth: 34\n" 9527 "...\n", 9528 IndentWidth, 12u); 9529 9530 Style.Language = FormatStyle::LK_Cpp; 9531 CHECK_PARSE("---\n" 9532 "Language: JavaScript\n" 9533 "IndentWidth: 12\n" 9534 "---\n" 9535 "Language: Cpp\n" 9536 "IndentWidth: 34\n" 9537 "...\n", 9538 IndentWidth, 34u); 9539 CHECK_PARSE("---\n" 9540 "IndentWidth: 78\n" 9541 "---\n" 9542 "Language: JavaScript\n" 9543 "IndentWidth: 56\n" 9544 "...\n", 9545 IndentWidth, 78u); 9546 9547 Style.ColumnLimit = 123; 9548 Style.IndentWidth = 234; 9549 Style.BreakBeforeBraces = FormatStyle::BS_Linux; 9550 Style.TabWidth = 345; 9551 EXPECT_FALSE(parseConfiguration("---\n" 9552 "IndentWidth: 456\n" 9553 "BreakBeforeBraces: Allman\n" 9554 "---\n" 9555 "Language: JavaScript\n" 9556 "IndentWidth: 111\n" 9557 "TabWidth: 111\n" 9558 "---\n" 9559 "Language: Cpp\n" 9560 "BreakBeforeBraces: Stroustrup\n" 9561 "TabWidth: 789\n" 9562 "...\n", 9563 &Style)); 9564 EXPECT_EQ(123u, Style.ColumnLimit); 9565 EXPECT_EQ(456u, Style.IndentWidth); 9566 EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces); 9567 EXPECT_EQ(789u, Style.TabWidth); 9568 9569 EXPECT_EQ(parseConfiguration("---\n" 9570 "Language: JavaScript\n" 9571 "IndentWidth: 56\n" 9572 "---\n" 9573 "IndentWidth: 78\n" 9574 "...\n", 9575 &Style), 9576 ParseError::Error); 9577 EXPECT_EQ(parseConfiguration("---\n" 9578 "Language: JavaScript\n" 9579 "IndentWidth: 56\n" 9580 "---\n" 9581 "Language: JavaScript\n" 9582 "IndentWidth: 78\n" 9583 "...\n", 9584 &Style), 9585 ParseError::Error); 9586 9587 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 9588 } 9589 9590 #undef CHECK_PARSE 9591 9592 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) { 9593 FormatStyle Style = {}; 9594 Style.Language = FormatStyle::LK_JavaScript; 9595 Style.BreakBeforeTernaryOperators = true; 9596 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value()); 9597 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 9598 9599 Style.BreakBeforeTernaryOperators = true; 9600 EXPECT_EQ(0, parseConfiguration("---\n" 9601 "BasedOnStyle: Google\n" 9602 "---\n" 9603 "Language: JavaScript\n" 9604 "IndentWidth: 76\n" 9605 "...\n", 9606 &Style).value()); 9607 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 9608 EXPECT_EQ(76u, Style.IndentWidth); 9609 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 9610 } 9611 9612 TEST_F(FormatTest, ConfigurationRoundTripTest) { 9613 FormatStyle Style = getLLVMStyle(); 9614 std::string YAML = configurationAsText(Style); 9615 FormatStyle ParsedStyle = {}; 9616 ParsedStyle.Language = FormatStyle::LK_Cpp; 9617 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value()); 9618 EXPECT_EQ(Style, ParsedStyle); 9619 } 9620 9621 TEST_F(FormatTest, WorksFor8bitEncodings) { 9622 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" 9623 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" 9624 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" 9625 "\"\xef\xee\xf0\xf3...\"", 9626 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " 9627 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " 9628 "\xef\xee\xf0\xf3...\"", 9629 getLLVMStyleWithColumns(12))); 9630 } 9631 9632 TEST_F(FormatTest, HandlesUTF8BOM) { 9633 EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf")); 9634 EXPECT_EQ("\xef\xbb\xbf#include <iostream>", 9635 format("\xef\xbb\xbf#include <iostream>")); 9636 EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>", 9637 format("\xef\xbb\xbf\n#include <iostream>")); 9638 } 9639 9640 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. 9641 #if !defined(_MSC_VER) 9642 9643 TEST_F(FormatTest, CountsUTF8CharactersProperly) { 9644 verifyFormat("\"Однажды в студёную зимнюю пору...\"", 9645 getLLVMStyleWithColumns(35)); 9646 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", 9647 getLLVMStyleWithColumns(31)); 9648 verifyFormat("// Однажды в студёную зимнюю пору...", 9649 getLLVMStyleWithColumns(36)); 9650 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32)); 9651 verifyFormat("/* Однажды в студёную зимнюю пору... */", 9652 getLLVMStyleWithColumns(39)); 9653 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", 9654 getLLVMStyleWithColumns(35)); 9655 } 9656 9657 TEST_F(FormatTest, SplitsUTF8Strings) { 9658 // Non-printable characters' width is currently considered to be the length in 9659 // bytes in UTF8. The characters can be displayed in very different manner 9660 // (zero-width, single width with a substitution glyph, expanded to their code 9661 // (e.g. "<8d>"), so there's no single correct way to handle them. 9662 EXPECT_EQ("\"aaaaÄ\"\n" 9663 "\"\xc2\x8d\";", 9664 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 9665 EXPECT_EQ("\"aaaaaaaÄ\"\n" 9666 "\"\xc2\x8d\";", 9667 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 9668 EXPECT_EQ("\"Однажды, в \"\n" 9669 "\"студёную \"\n" 9670 "\"зимнюю \"\n" 9671 "\"пору,\"", 9672 format("\"Однажды, в студёную зимнюю пору,\"", 9673 getLLVMStyleWithColumns(13))); 9674 EXPECT_EQ( 9675 "\"一 二 三 \"\n" 9676 "\"四 五六 \"\n" 9677 "\"七 八 九 \"\n" 9678 "\"十\"", 9679 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); 9680 EXPECT_EQ("\"一\t二 \"\n" 9681 "\"\t三 \"\n" 9682 "\"四 五\t六 \"\n" 9683 "\"\t七 \"\n" 9684 "\"八九十\tqq\"", 9685 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", 9686 getLLVMStyleWithColumns(11))); 9687 } 9688 9689 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { 9690 EXPECT_EQ("const char *sssss =\n" 9691 " \"一二三四五六七八\\\n" 9692 " 九 十\";", 9693 format("const char *sssss = \"一二三四五六七八\\\n" 9694 " 九 十\";", 9695 getLLVMStyleWithColumns(30))); 9696 } 9697 9698 TEST_F(FormatTest, SplitsUTF8LineComments) { 9699 EXPECT_EQ("// aaaaÄ\xc2\x8d", 9700 format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10))); 9701 EXPECT_EQ("// Я из лесу\n" 9702 "// вышел; был\n" 9703 "// сильный\n" 9704 "// мороз.", 9705 format("// Я из лесу вышел; был сильный мороз.", 9706 getLLVMStyleWithColumns(13))); 9707 EXPECT_EQ("// 一二三\n" 9708 "// 四五六七\n" 9709 "// 八 九\n" 9710 "// 十", 9711 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9))); 9712 } 9713 9714 TEST_F(FormatTest, SplitsUTF8BlockComments) { 9715 EXPECT_EQ("/* Гляжу,\n" 9716 " * поднимается\n" 9717 " * медленно в\n" 9718 " * гору\n" 9719 " * Лошадка,\n" 9720 " * везущая\n" 9721 " * хворосту\n" 9722 " * воз. */", 9723 format("/* Гляжу, поднимается медленно в гору\n" 9724 " * Лошадка, везущая хворосту воз. */", 9725 getLLVMStyleWithColumns(13))); 9726 EXPECT_EQ( 9727 "/* 一二三\n" 9728 " * 四五六七\n" 9729 " * 八 九\n" 9730 " * 十 */", 9731 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9))); 9732 EXPECT_EQ("/* \n" 9733 " * \n" 9734 " * - */", 9735 format("/* - */", getLLVMStyleWithColumns(12))); 9736 } 9737 9738 #endif // _MSC_VER 9739 9740 TEST_F(FormatTest, ConstructorInitializerIndentWidth) { 9741 FormatStyle Style = getLLVMStyle(); 9742 9743 Style.ConstructorInitializerIndentWidth = 4; 9744 verifyFormat( 9745 "SomeClass::Constructor()\n" 9746 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 9747 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 9748 Style); 9749 9750 Style.ConstructorInitializerIndentWidth = 2; 9751 verifyFormat( 9752 "SomeClass::Constructor()\n" 9753 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 9754 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 9755 Style); 9756 9757 Style.ConstructorInitializerIndentWidth = 0; 9758 verifyFormat( 9759 "SomeClass::Constructor()\n" 9760 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 9761 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 9762 Style); 9763 } 9764 9765 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) { 9766 FormatStyle Style = getLLVMStyle(); 9767 Style.BreakConstructorInitializersBeforeComma = true; 9768 Style.ConstructorInitializerIndentWidth = 4; 9769 verifyFormat("SomeClass::Constructor()\n" 9770 " : a(a)\n" 9771 " , b(b)\n" 9772 " , c(c) {}", 9773 Style); 9774 verifyFormat("SomeClass::Constructor()\n" 9775 " : a(a) {}", 9776 Style); 9777 9778 Style.ColumnLimit = 0; 9779 verifyFormat("SomeClass::Constructor()\n" 9780 " : a(a) {}", 9781 Style); 9782 verifyFormat("SomeClass::Constructor()\n" 9783 " : a(a)\n" 9784 " , b(b)\n" 9785 " , c(c) {}", 9786 Style); 9787 verifyFormat("SomeClass::Constructor()\n" 9788 " : a(a) {\n" 9789 " foo();\n" 9790 " bar();\n" 9791 "}", 9792 Style); 9793 9794 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 9795 verifyFormat("SomeClass::Constructor()\n" 9796 " : a(a)\n" 9797 " , b(b)\n" 9798 " , c(c) {\n}", 9799 Style); 9800 verifyFormat("SomeClass::Constructor()\n" 9801 " : a(a) {\n}", 9802 Style); 9803 9804 Style.ColumnLimit = 80; 9805 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 9806 Style.ConstructorInitializerIndentWidth = 2; 9807 verifyFormat("SomeClass::Constructor()\n" 9808 " : a(a)\n" 9809 " , b(b)\n" 9810 " , c(c) {}", 9811 Style); 9812 9813 Style.ConstructorInitializerIndentWidth = 0; 9814 verifyFormat("SomeClass::Constructor()\n" 9815 ": a(a)\n" 9816 ", b(b)\n" 9817 ", c(c) {}", 9818 Style); 9819 9820 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 9821 Style.ConstructorInitializerIndentWidth = 4; 9822 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style); 9823 verifyFormat( 9824 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n", 9825 Style); 9826 verifyFormat( 9827 "SomeClass::Constructor()\n" 9828 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", 9829 Style); 9830 Style.ConstructorInitializerIndentWidth = 4; 9831 Style.ColumnLimit = 60; 9832 verifyFormat("SomeClass::Constructor()\n" 9833 " : aaaaaaaa(aaaaaaaa)\n" 9834 " , aaaaaaaa(aaaaaaaa)\n" 9835 " , aaaaaaaa(aaaaaaaa) {}", 9836 Style); 9837 } 9838 9839 TEST_F(FormatTest, Destructors) { 9840 verifyFormat("void F(int &i) { i.~int(); }"); 9841 verifyFormat("void F(int &i) { i->~int(); }"); 9842 } 9843 9844 TEST_F(FormatTest, FormatsWithWebKitStyle) { 9845 FormatStyle Style = getWebKitStyle(); 9846 9847 // Don't indent in outer namespaces. 9848 verifyFormat("namespace outer {\n" 9849 "int i;\n" 9850 "namespace inner {\n" 9851 " int i;\n" 9852 "} // namespace inner\n" 9853 "} // namespace outer\n" 9854 "namespace other_outer {\n" 9855 "int i;\n" 9856 "}", 9857 Style); 9858 9859 // Don't indent case labels. 9860 verifyFormat("switch (variable) {\n" 9861 "case 1:\n" 9862 "case 2:\n" 9863 " doSomething();\n" 9864 " break;\n" 9865 "default:\n" 9866 " ++variable;\n" 9867 "}", 9868 Style); 9869 9870 // Wrap before binary operators. 9871 EXPECT_EQ("void f()\n" 9872 "{\n" 9873 " if (aaaaaaaaaaaaaaaa\n" 9874 " && bbbbbbbbbbbbbbbbbbbbbbbb\n" 9875 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 9876 " return;\n" 9877 "}", 9878 format("void f() {\n" 9879 "if (aaaaaaaaaaaaaaaa\n" 9880 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" 9881 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 9882 "return;\n" 9883 "}", 9884 Style)); 9885 9886 // Allow functions on a single line. 9887 verifyFormat("void f() { return; }", Style); 9888 9889 // Constructor initializers are formatted one per line with the "," on the 9890 // new line. 9891 verifyFormat("Constructor()\n" 9892 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 9893 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" 9894 " aaaaaaaaaaaaaa)\n" 9895 " , aaaaaaaaaaaaaaaaaaaaaaa()\n" 9896 "{\n" 9897 "}", 9898 Style); 9899 verifyFormat("SomeClass::Constructor()\n" 9900 " : a(a)\n" 9901 "{\n" 9902 "}", 9903 Style); 9904 EXPECT_EQ("SomeClass::Constructor()\n" 9905 " : a(a)\n" 9906 "{\n" 9907 "}", 9908 format("SomeClass::Constructor():a(a){}", Style)); 9909 verifyFormat("SomeClass::Constructor()\n" 9910 " : a(a)\n" 9911 " , b(b)\n" 9912 " , c(c)\n" 9913 "{\n" 9914 "}", 9915 Style); 9916 verifyFormat("SomeClass::Constructor()\n" 9917 " : a(a)\n" 9918 "{\n" 9919 " foo();\n" 9920 " bar();\n" 9921 "}", 9922 Style); 9923 9924 // Access specifiers should be aligned left. 9925 verifyFormat("class C {\n" 9926 "public:\n" 9927 " int i;\n" 9928 "};", 9929 Style); 9930 9931 // Do not align comments. 9932 verifyFormat("int a; // Do not\n" 9933 "double b; // align comments.", 9934 Style); 9935 9936 // Do not align operands. 9937 EXPECT_EQ("ASSERT(aaaa\n" 9938 " || bbbb);", 9939 format("ASSERT ( aaaa\n||bbbb);", Style)); 9940 9941 // Accept input's line breaks. 9942 EXPECT_EQ("if (aaaaaaaaaaaaaaa\n" 9943 " || bbbbbbbbbbbbbbb) {\n" 9944 " i++;\n" 9945 "}", 9946 format("if (aaaaaaaaaaaaaaa\n" 9947 "|| bbbbbbbbbbbbbbb) { i++; }", 9948 Style)); 9949 EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n" 9950 " i++;\n" 9951 "}", 9952 format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style)); 9953 9954 // Don't automatically break all macro definitions (llvm.org/PR17842). 9955 verifyFormat("#define aNumber 10", Style); 9956 // However, generally keep the line breaks that the user authored. 9957 EXPECT_EQ("#define aNumber \\\n" 9958 " 10", 9959 format("#define aNumber \\\n" 9960 " 10", 9961 Style)); 9962 9963 // Keep empty and one-element array literals on a single line. 9964 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n" 9965 " copyItems:YES];", 9966 format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n" 9967 "copyItems:YES];", 9968 Style)); 9969 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n" 9970 " copyItems:YES];", 9971 format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n" 9972 " copyItems:YES];", 9973 Style)); 9974 // FIXME: This does not seem right, there should be more indentation before 9975 // the array literal's entries. Nested blocks have the same problem. 9976 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 9977 " @\"a\",\n" 9978 " @\"a\"\n" 9979 "]\n" 9980 " copyItems:YES];", 9981 format("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 9982 " @\"a\",\n" 9983 " @\"a\"\n" 9984 " ]\n" 9985 " copyItems:YES];", 9986 Style)); 9987 EXPECT_EQ( 9988 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 9989 " copyItems:YES];", 9990 format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 9991 " copyItems:YES];", 9992 Style)); 9993 9994 verifyFormat("[self.a b:c c:d];", Style); 9995 EXPECT_EQ("[self.a b:c\n" 9996 " c:d];", 9997 format("[self.a b:c\n" 9998 "c:d];", 9999 Style)); 10000 } 10001 10002 TEST_F(FormatTest, FormatsLambdas) { 10003 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n"); 10004 verifyFormat("int c = [&] { [=] { return b++; }(); }();\n"); 10005 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n"); 10006 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n"); 10007 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n"); 10008 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n"); 10009 verifyFormat("void f() {\n" 10010 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n" 10011 "}\n"); 10012 verifyFormat("void f() {\n" 10013 " other(x.begin(), //\n" 10014 " x.end(), //\n" 10015 " [&](int, int) { return 1; });\n" 10016 "}\n"); 10017 verifyFormat("SomeFunction([]() { // A cool function...\n" 10018 " return 43;\n" 10019 "});"); 10020 EXPECT_EQ("SomeFunction([]() {\n" 10021 "#define A a\n" 10022 " return 43;\n" 10023 "});", 10024 format("SomeFunction([](){\n" 10025 "#define A a\n" 10026 "return 43;\n" 10027 "});")); 10028 verifyFormat("void f() {\n" 10029 " SomeFunction([](decltype(x), A *a) {});\n" 10030 "}"); 10031 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10032 " [](const aaaaaaaaaa &a) { return a; });"); 10033 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n" 10034 " SomeOtherFunctioooooooooooooooooooooooooon();\n" 10035 "});"); 10036 verifyFormat("Constructor()\n" 10037 " : Field([] { // comment\n" 10038 " int i;\n" 10039 " }) {}"); 10040 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n" 10041 " return some_parameter.size();\n" 10042 "};"); 10043 verifyFormat("int i = aaaaaa ? 1 //\n" 10044 " : [] {\n" 10045 " return 2; //\n" 10046 " }();"); 10047 verifyFormat("llvm::errs() << \"number of twos is \"\n" 10048 " << std::count_if(v.begin(), v.end(), [](int x) {\n" 10049 " return x == 2; // force break\n" 10050 " });"); 10051 10052 // Lambdas with return types. 10053 verifyFormat("int c = []() -> int { return 2; }();\n"); 10054 verifyFormat("int c = []() -> vector<int> { return {2}; }();\n"); 10055 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());"); 10056 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};"); 10057 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};"); 10058 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};"); 10059 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};"); 10060 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n" 10061 " int j) -> int {\n" 10062 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n" 10063 "};"); 10064 verifyFormat( 10065 "aaaaaaaaaaaaaaaaaaaaaa(\n" 10066 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n" 10067 " return aaaaaaaaaaaaaaaaa;\n" 10068 " });", 10069 getLLVMStyleWithColumns(70)); 10070 10071 // Multiple lambdas in the same parentheses change indentation rules. 10072 verifyFormat("SomeFunction(\n" 10073 " []() {\n" 10074 " int i = 42;\n" 10075 " return i;\n" 10076 " },\n" 10077 " []() {\n" 10078 " int j = 43;\n" 10079 " return j;\n" 10080 " });"); 10081 10082 // More complex introducers. 10083 verifyFormat("return [i, args...] {};"); 10084 10085 // Not lambdas. 10086 verifyFormat("constexpr char hello[]{\"hello\"};"); 10087 verifyFormat("double &operator[](int i) { return 0; }\n" 10088 "int i;"); 10089 verifyFormat("std::unique_ptr<int[]> foo() {}"); 10090 verifyFormat("int i = a[a][a]->f();"); 10091 verifyFormat("int i = (*b)[a]->f();"); 10092 10093 // Other corner cases. 10094 verifyFormat("void f() {\n" 10095 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n" 10096 " );\n" 10097 "}"); 10098 10099 // Lambdas created through weird macros. 10100 verifyFormat("void f() {\n" 10101 " MACRO((const AA &a) { return 1; });\n" 10102 "}"); 10103 10104 verifyFormat("if (blah_blah(whatever, whatever, [] {\n" 10105 " doo_dah();\n" 10106 " doo_dah();\n" 10107 " })) {\n" 10108 "}"); 10109 verifyFormat("auto lambda = []() {\n" 10110 " int a = 2\n" 10111 "#if A\n" 10112 " + 2\n" 10113 "#endif\n" 10114 " ;\n" 10115 "};"); 10116 } 10117 10118 TEST_F(FormatTest, FormatsBlocks) { 10119 FormatStyle ShortBlocks = getLLVMStyle(); 10120 ShortBlocks.AllowShortBlocksOnASingleLine = true; 10121 verifyFormat("int (^Block)(int, int);", ShortBlocks); 10122 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks); 10123 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks); 10124 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks); 10125 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks); 10126 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks); 10127 10128 verifyFormat("foo(^{ bar(); });", ShortBlocks); 10129 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks); 10130 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks); 10131 10132 verifyFormat("[operation setCompletionBlock:^{\n" 10133 " [self onOperationDone];\n" 10134 "}];"); 10135 verifyFormat("int i = {[operation setCompletionBlock:^{\n" 10136 " [self onOperationDone];\n" 10137 "}]};"); 10138 verifyFormat("[operation setCompletionBlock:^(int *i) {\n" 10139 " f();\n" 10140 "}];"); 10141 verifyFormat("int a = [operation block:^int(int *i) {\n" 10142 " return 1;\n" 10143 "}];"); 10144 verifyFormat("[myObject doSomethingWith:arg1\n" 10145 " aaa:^int(int *a) {\n" 10146 " return 1;\n" 10147 " }\n" 10148 " bbb:f(a * bbbbbbbb)];"); 10149 10150 verifyFormat("[operation setCompletionBlock:^{\n" 10151 " [self.delegate newDataAvailable];\n" 10152 "}];", 10153 getLLVMStyleWithColumns(60)); 10154 verifyFormat("dispatch_async(_fileIOQueue, ^{\n" 10155 " NSString *path = [self sessionFilePath];\n" 10156 " if (path) {\n" 10157 " // ...\n" 10158 " }\n" 10159 "});"); 10160 verifyFormat("[[SessionService sharedService]\n" 10161 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 10162 " if (window) {\n" 10163 " [self windowDidLoad:window];\n" 10164 " } else {\n" 10165 " [self errorLoadingWindow];\n" 10166 " }\n" 10167 " }];"); 10168 verifyFormat("void (^largeBlock)(void) = ^{\n" 10169 " // ...\n" 10170 "};\n", 10171 getLLVMStyleWithColumns(40)); 10172 verifyFormat("[[SessionService sharedService]\n" 10173 " loadWindowWithCompletionBlock: //\n" 10174 " ^(SessionWindow *window) {\n" 10175 " if (window) {\n" 10176 " [self windowDidLoad:window];\n" 10177 " } else {\n" 10178 " [self errorLoadingWindow];\n" 10179 " }\n" 10180 " }];", 10181 getLLVMStyleWithColumns(60)); 10182 verifyFormat("[myObject doSomethingWith:arg1\n" 10183 " firstBlock:^(Foo *a) {\n" 10184 " // ...\n" 10185 " int i;\n" 10186 " }\n" 10187 " secondBlock:^(Bar *b) {\n" 10188 " // ...\n" 10189 " int i;\n" 10190 " }\n" 10191 " thirdBlock:^Foo(Bar *b) {\n" 10192 " // ...\n" 10193 " int i;\n" 10194 " }];"); 10195 verifyFormat("[myObject doSomethingWith:arg1\n" 10196 " firstBlock:-1\n" 10197 " secondBlock:^(Bar *b) {\n" 10198 " // ...\n" 10199 " int i;\n" 10200 " }];"); 10201 10202 verifyFormat("f(^{\n" 10203 " @autoreleasepool {\n" 10204 " if (a) {\n" 10205 " g();\n" 10206 " }\n" 10207 " }\n" 10208 "});"); 10209 verifyFormat("Block b = ^int *(A *a, B *b) {}"); 10210 10211 FormatStyle FourIndent = getLLVMStyle(); 10212 FourIndent.ObjCBlockIndentWidth = 4; 10213 verifyFormat("[operation setCompletionBlock:^{\n" 10214 " [self onOperationDone];\n" 10215 "}];", 10216 FourIndent); 10217 } 10218 10219 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) { 10220 FormatStyle ZeroColumn = getLLVMStyle(); 10221 ZeroColumn.ColumnLimit = 0; 10222 10223 verifyFormat("[[SessionService sharedService] " 10224 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 10225 " if (window) {\n" 10226 " [self windowDidLoad:window];\n" 10227 " } else {\n" 10228 " [self errorLoadingWindow];\n" 10229 " }\n" 10230 "}];", 10231 ZeroColumn); 10232 EXPECT_EQ("[[SessionService sharedService]\n" 10233 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 10234 " if (window) {\n" 10235 " [self windowDidLoad:window];\n" 10236 " } else {\n" 10237 " [self errorLoadingWindow];\n" 10238 " }\n" 10239 " }];", 10240 format("[[SessionService sharedService]\n" 10241 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 10242 " if (window) {\n" 10243 " [self windowDidLoad:window];\n" 10244 " } else {\n" 10245 " [self errorLoadingWindow];\n" 10246 " }\n" 10247 "}];", 10248 ZeroColumn)); 10249 verifyFormat("[myObject doSomethingWith:arg1\n" 10250 " firstBlock:^(Foo *a) {\n" 10251 " // ...\n" 10252 " int i;\n" 10253 " }\n" 10254 " secondBlock:^(Bar *b) {\n" 10255 " // ...\n" 10256 " int i;\n" 10257 " }\n" 10258 " thirdBlock:^Foo(Bar *b) {\n" 10259 " // ...\n" 10260 " int i;\n" 10261 " }];", 10262 ZeroColumn); 10263 verifyFormat("f(^{\n" 10264 " @autoreleasepool {\n" 10265 " if (a) {\n" 10266 " g();\n" 10267 " }\n" 10268 " }\n" 10269 "});", 10270 ZeroColumn); 10271 verifyFormat("void (^largeBlock)(void) = ^{\n" 10272 " // ...\n" 10273 "};", 10274 ZeroColumn); 10275 10276 ZeroColumn.AllowShortBlocksOnASingleLine = true; 10277 EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };", 10278 format("void (^largeBlock)(void) = ^{ int i; };", 10279 ZeroColumn)); 10280 ZeroColumn.AllowShortBlocksOnASingleLine = false; 10281 EXPECT_EQ("void (^largeBlock)(void) = ^{\n" 10282 " int i;\n" 10283 "};", 10284 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 10285 } 10286 10287 TEST_F(FormatTest, SupportsCRLF) { 10288 EXPECT_EQ("int a;\r\n" 10289 "int b;\r\n" 10290 "int c;\r\n", 10291 format("int a;\r\n" 10292 " int b;\r\n" 10293 " int c;\r\n", 10294 getLLVMStyle())); 10295 EXPECT_EQ("int a;\r\n" 10296 "int b;\r\n" 10297 "int c;\r\n", 10298 format("int a;\r\n" 10299 " int b;\n" 10300 " int c;\r\n", 10301 getLLVMStyle())); 10302 EXPECT_EQ("int a;\n" 10303 "int b;\n" 10304 "int c;\n", 10305 format("int a;\r\n" 10306 " int b;\n" 10307 " int c;\n", 10308 getLLVMStyle())); 10309 EXPECT_EQ("\"aaaaaaa \"\r\n" 10310 "\"bbbbbbb\";\r\n", 10311 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10))); 10312 EXPECT_EQ("#define A \\\r\n" 10313 " b; \\\r\n" 10314 " c; \\\r\n" 10315 " d;\r\n", 10316 format("#define A \\\r\n" 10317 " b; \\\r\n" 10318 " c; d; \r\n", 10319 getGoogleStyle())); 10320 10321 EXPECT_EQ("/*\r\n" 10322 "multi line block comments\r\n" 10323 "should not introduce\r\n" 10324 "an extra carriage return\r\n" 10325 "*/\r\n", 10326 format("/*\r\n" 10327 "multi line block comments\r\n" 10328 "should not introduce\r\n" 10329 "an extra carriage return\r\n" 10330 "*/\r\n")); 10331 } 10332 10333 TEST_F(FormatTest, MunchSemicolonAfterBlocks) { 10334 verifyFormat("MY_CLASS(C) {\n" 10335 " int i;\n" 10336 " int j;\n" 10337 "};"); 10338 } 10339 10340 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) { 10341 FormatStyle TwoIndent = getLLVMStyleWithColumns(15); 10342 TwoIndent.ContinuationIndentWidth = 2; 10343 10344 EXPECT_EQ("int i =\n" 10345 " longFunction(\n" 10346 " arg);", 10347 format("int i = longFunction(arg);", TwoIndent)); 10348 10349 FormatStyle SixIndent = getLLVMStyleWithColumns(20); 10350 SixIndent.ContinuationIndentWidth = 6; 10351 10352 EXPECT_EQ("int i =\n" 10353 " longFunction(\n" 10354 " arg);", 10355 format("int i = longFunction(arg);", SixIndent)); 10356 } 10357 10358 TEST_F(FormatTest, SpacesInAngles) { 10359 FormatStyle Spaces = getLLVMStyle(); 10360 Spaces.SpacesInAngles = true; 10361 10362 verifyFormat("static_cast< int >(arg);", Spaces); 10363 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces); 10364 verifyFormat("f< int, float >();", Spaces); 10365 verifyFormat("template <> g() {}", Spaces); 10366 verifyFormat("template < std::vector< int > > f() {}", Spaces); 10367 10368 Spaces.Standard = FormatStyle::LS_Cpp03; 10369 Spaces.SpacesInAngles = true; 10370 verifyFormat("A< A< int > >();", Spaces); 10371 10372 Spaces.SpacesInAngles = false; 10373 verifyFormat("A<A<int> >();", Spaces); 10374 10375 Spaces.Standard = FormatStyle::LS_Cpp11; 10376 Spaces.SpacesInAngles = true; 10377 verifyFormat("A< A< int > >();", Spaces); 10378 10379 Spaces.SpacesInAngles = false; 10380 verifyFormat("A<A<int>>();", Spaces); 10381 } 10382 10383 TEST_F(FormatTest, TripleAngleBrackets) { 10384 verifyFormat("f<<<1, 1>>>();"); 10385 verifyFormat("f<<<1, 1, 1, s>>>();"); 10386 verifyFormat("f<<<a, b, c, d>>>();"); 10387 EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();")); 10388 verifyFormat("f<param><<<1, 1>>>();"); 10389 verifyFormat("f<1><<<1, 1>>>();"); 10390 EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();")); 10391 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 10392 "aaaaaaaaaaa<<<\n 1, 1>>>();"); 10393 } 10394 10395 TEST_F(FormatTest, MergeLessLessAtEnd) { 10396 verifyFormat("<<"); 10397 EXPECT_EQ("< < <", format("\\\n<<<")); 10398 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 10399 "aaallvm::outs() <<"); 10400 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 10401 "aaaallvm::outs()\n <<"); 10402 } 10403 10404 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) { 10405 std::string code = "#if A\n" 10406 "#if B\n" 10407 "a.\n" 10408 "#endif\n" 10409 " a = 1;\n" 10410 "#else\n" 10411 "#endif\n" 10412 "#if C\n" 10413 "#else\n" 10414 "#endif\n"; 10415 EXPECT_EQ(code, format(code)); 10416 } 10417 10418 TEST_F(FormatTest, HandleConflictMarkers) { 10419 // Git/SVN conflict markers. 10420 EXPECT_EQ("int a;\n" 10421 "void f() {\n" 10422 " callme(some(parameter1,\n" 10423 "<<<<<<< text by the vcs\n" 10424 " parameter2),\n" 10425 "||||||| text by the vcs\n" 10426 " parameter2),\n" 10427 " parameter3,\n" 10428 "======= text by the vcs\n" 10429 " parameter2, parameter3),\n" 10430 ">>>>>>> text by the vcs\n" 10431 " otherparameter);\n", 10432 format("int a;\n" 10433 "void f() {\n" 10434 " callme(some(parameter1,\n" 10435 "<<<<<<< text by the vcs\n" 10436 " parameter2),\n" 10437 "||||||| text by the vcs\n" 10438 " parameter2),\n" 10439 " parameter3,\n" 10440 "======= text by the vcs\n" 10441 " parameter2,\n" 10442 " parameter3),\n" 10443 ">>>>>>> text by the vcs\n" 10444 " otherparameter);\n")); 10445 10446 // Perforce markers. 10447 EXPECT_EQ("void f() {\n" 10448 " function(\n" 10449 ">>>> text by the vcs\n" 10450 " parameter,\n" 10451 "==== text by the vcs\n" 10452 " parameter,\n" 10453 "==== text by the vcs\n" 10454 " parameter,\n" 10455 "<<<< text by the vcs\n" 10456 " parameter);\n", 10457 format("void f() {\n" 10458 " function(\n" 10459 ">>>> text by the vcs\n" 10460 " parameter,\n" 10461 "==== text by the vcs\n" 10462 " parameter,\n" 10463 "==== text by the vcs\n" 10464 " parameter,\n" 10465 "<<<< text by the vcs\n" 10466 " parameter);\n")); 10467 10468 EXPECT_EQ("<<<<<<<\n" 10469 "|||||||\n" 10470 "=======\n" 10471 ">>>>>>>", 10472 format("<<<<<<<\n" 10473 "|||||||\n" 10474 "=======\n" 10475 ">>>>>>>")); 10476 10477 EXPECT_EQ("<<<<<<<\n" 10478 "|||||||\n" 10479 "int i;\n" 10480 "=======\n" 10481 ">>>>>>>", 10482 format("<<<<<<<\n" 10483 "|||||||\n" 10484 "int i;\n" 10485 "=======\n" 10486 ">>>>>>>")); 10487 10488 // FIXME: Handle parsing of macros around conflict markers correctly: 10489 EXPECT_EQ("#define Macro \\\n" 10490 "<<<<<<<\n" 10491 "Something \\\n" 10492 "|||||||\n" 10493 "Else \\\n" 10494 "=======\n" 10495 "Other \\\n" 10496 ">>>>>>>\n" 10497 " End int i;\n", 10498 format("#define Macro \\\n" 10499 "<<<<<<<\n" 10500 " Something \\\n" 10501 "|||||||\n" 10502 " Else \\\n" 10503 "=======\n" 10504 " Other \\\n" 10505 ">>>>>>>\n" 10506 " End\n" 10507 "int i;\n")); 10508 } 10509 10510 TEST_F(FormatTest, DisableRegions) { 10511 EXPECT_EQ("int i;\n" 10512 "// clang-format off\n" 10513 " int j;\n" 10514 "// clang-format on\n" 10515 "int k;", 10516 format(" int i;\n" 10517 " // clang-format off\n" 10518 " int j;\n" 10519 " // clang-format on\n" 10520 " int k;")); 10521 EXPECT_EQ("int i;\n" 10522 "/* clang-format off */\n" 10523 " int j;\n" 10524 "/* clang-format on */\n" 10525 "int k;", 10526 format(" int i;\n" 10527 " /* clang-format off */\n" 10528 " int j;\n" 10529 " /* clang-format on */\n" 10530 " int k;")); 10531 } 10532 10533 TEST_F(FormatTest, DoNotCrashOnInvalidInput) { format("? ) ="); } 10534 10535 } // end namespace tooling 10536 } // end namespace clang 10537