1 //===- unittest/Format/FormatTestComments.cpp - Formatting unit tests -----===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "clang/Format/Format.h" 11 12 #include "../Tooling/ReplacementTest.h" 13 #include "FormatTestUtils.h" 14 15 #include "clang/Frontend/TextDiagnosticPrinter.h" 16 #include "llvm/Support/Debug.h" 17 #include "llvm/Support/MemoryBuffer.h" 18 #include "gtest/gtest.h" 19 20 #define DEBUG_TYPE "format-test" 21 22 using clang::tooling::ReplacementTest; 23 using clang::tooling::toReplacements; 24 25 namespace clang { 26 namespace format { 27 namespace { 28 29 FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); } 30 31 class FormatTestComments : public ::testing::Test { 32 protected: 33 enum IncompleteCheck { 34 IC_ExpectComplete, 35 IC_ExpectIncomplete, 36 IC_DoNotCheck 37 }; 38 39 std::string format(llvm::StringRef Code, 40 const FormatStyle &Style = getLLVMStyle(), 41 IncompleteCheck CheckIncomplete = IC_ExpectComplete) { 42 DEBUG(llvm::errs() << "---\n"); 43 DEBUG(llvm::errs() << Code << "\n\n"); 44 std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size())); 45 bool IncompleteFormat = false; 46 tooling::Replacements Replaces = 47 reformat(Style, Code, Ranges, "<stdin>", &IncompleteFormat); 48 if (CheckIncomplete != IC_DoNotCheck) { 49 bool ExpectedIncompleteFormat = CheckIncomplete == IC_ExpectIncomplete; 50 EXPECT_EQ(ExpectedIncompleteFormat, IncompleteFormat) << Code << "\n\n"; 51 } 52 ReplacementCount = Replaces.size(); 53 auto Result = applyAllReplacements(Code, Replaces); 54 EXPECT_TRUE(static_cast<bool>(Result)); 55 DEBUG(llvm::errs() << "\n" << *Result << "\n\n"); 56 return *Result; 57 } 58 59 FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) { 60 FormatStyle Style = getLLVMStyle(); 61 Style.ColumnLimit = ColumnLimit; 62 return Style; 63 } 64 65 void verifyFormat(llvm::StringRef Code, 66 const FormatStyle &Style = getLLVMStyle()) { 67 EXPECT_EQ(Code.str(), format(test::messUp(Code), Style)); 68 } 69 70 void verifyIncompleteFormat(llvm::StringRef Code, 71 const FormatStyle &Style = getLLVMStyle()) { 72 EXPECT_EQ(Code.str(), 73 format(test::messUp(Code), Style, IC_ExpectIncomplete)); 74 } 75 76 void verifyGoogleFormat(llvm::StringRef Code) { 77 verifyFormat(Code, getGoogleStyle()); 78 } 79 80 /// \brief Verify that clang-format does not crash on the given input. 81 void verifyNoCrash(llvm::StringRef Code, 82 const FormatStyle &Style = getLLVMStyle()) { 83 format(Code, Style, IC_DoNotCheck); 84 } 85 86 int ReplacementCount; 87 }; 88 89 //===----------------------------------------------------------------------===// 90 // Tests for comments. 91 //===----------------------------------------------------------------------===// 92 93 TEST_F(FormatTestComments, UnderstandsSingleLineComments) { 94 verifyFormat("//* */"); 95 verifyFormat("// line 1\n" 96 "// line 2\n" 97 "void f() {}\n"); 98 99 verifyFormat("void f() {\n" 100 " // Doesn't do anything\n" 101 "}"); 102 verifyFormat("SomeObject\n" 103 " // Calling someFunction on SomeObject\n" 104 " .someFunction();"); 105 verifyFormat("auto result = SomeObject\n" 106 " // Calling someFunction on SomeObject\n" 107 " .someFunction();"); 108 verifyFormat("void f(int i, // some comment (probably for i)\n" 109 " int j, // some comment (probably for j)\n" 110 " int k); // some comment (probably for k)"); 111 verifyFormat("void f(int i,\n" 112 " // some comment (probably for j)\n" 113 " int j,\n" 114 " // some comment (probably for k)\n" 115 " int k);"); 116 117 verifyFormat("int i // This is a fancy variable\n" 118 " = 5; // with nicely aligned comment."); 119 120 verifyFormat("// Leading comment.\n" 121 "int a; // Trailing comment."); 122 verifyFormat("int a; // Trailing comment\n" 123 " // on 2\n" 124 " // or 3 lines.\n" 125 "int b;"); 126 verifyFormat("int a; // Trailing comment\n" 127 "\n" 128 "// Leading comment.\n" 129 "int b;"); 130 verifyFormat("int a; // Comment.\n" 131 " // More details.\n" 132 "int bbbb; // Another comment."); 133 verifyFormat( 134 "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n" 135 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // comment\n" 136 "int cccccccccccccccccccccccccccccc; // comment\n" 137 "int ddd; // looooooooooooooooooooooooong comment\n" 138 "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n" 139 "int bbbbbbbbbbbbbbbbbbbbb; // comment\n" 140 "int ccccccccccccccccccc; // comment"); 141 142 verifyFormat("#include \"a\" // comment\n" 143 "#include \"a/b/c\" // comment"); 144 verifyFormat("#include <a> // comment\n" 145 "#include <a/b/c> // comment"); 146 EXPECT_EQ("#include \"a\" // comment\n" 147 "#include \"a/b/c\" // comment", 148 format("#include \\\n" 149 " \"a\" // comment\n" 150 "#include \"a/b/c\" // comment")); 151 152 verifyFormat("enum E {\n" 153 " // comment\n" 154 " VAL_A, // comment\n" 155 " VAL_B\n" 156 "};"); 157 158 EXPECT_EQ("enum A {\n" 159 " // line a\n" 160 " a,\n" 161 " b, // line b\n" 162 "\n" 163 " // line c\n" 164 " c\n" 165 "};", 166 format("enum A {\n" 167 " // line a\n" 168 " a,\n" 169 " b, // line b\n" 170 "\n" 171 " // line c\n" 172 " c\n" 173 "};", 174 getLLVMStyleWithColumns(20))); 175 EXPECT_EQ("enum A {\n" 176 " a, // line 1\n" 177 " // line 2\n" 178 "};", 179 format("enum A {\n" 180 " a, // line 1\n" 181 " // line 2\n" 182 "};", 183 getLLVMStyleWithColumns(20))); 184 EXPECT_EQ("enum A {\n" 185 " a, // line 1\n" 186 " // line 2\n" 187 "};", 188 format("enum A {\n" 189 " a, // line 1\n" 190 " // line 2\n" 191 "};", 192 getLLVMStyleWithColumns(20))); 193 EXPECT_EQ("enum A {\n" 194 " a, // line 1\n" 195 " // line 2\n" 196 " b\n" 197 "};", 198 format("enum A {\n" 199 " a, // line 1\n" 200 " // line 2\n" 201 " b\n" 202 "};", 203 getLLVMStyleWithColumns(20))); 204 EXPECT_EQ("enum A {\n" 205 " a, // line 1\n" 206 " // line 2\n" 207 " b\n" 208 "};", 209 format("enum A {\n" 210 " a, // line 1\n" 211 " // line 2\n" 212 " b\n" 213 "};", 214 getLLVMStyleWithColumns(20))); 215 verifyFormat( 216 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 217 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment"); 218 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 219 " // Comment inside a statement.\n" 220 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 221 verifyFormat("SomeFunction(a,\n" 222 " // comment\n" 223 " b + x);"); 224 verifyFormat("SomeFunction(a, a,\n" 225 " // comment\n" 226 " b + x);"); 227 verifyFormat( 228 "bool aaaaaaaaaaaaa = // comment\n" 229 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 230 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 231 232 verifyFormat("int aaaa; // aaaaa\n" 233 "int aa; // aaaaaaa", 234 getLLVMStyleWithColumns(20)); 235 236 EXPECT_EQ("void f() { // This does something ..\n" 237 "}\n" 238 "int a; // This is unrelated", 239 format("void f() { // This does something ..\n" 240 " }\n" 241 "int a; // This is unrelated")); 242 EXPECT_EQ("class C {\n" 243 " void f() { // This does something ..\n" 244 " } // awesome..\n" 245 "\n" 246 " int a; // This is unrelated\n" 247 "};", 248 format("class C{void f() { // This does something ..\n" 249 " } // awesome..\n" 250 " \n" 251 "int a; // This is unrelated\n" 252 "};")); 253 254 EXPECT_EQ("int i; // single line trailing comment", 255 format("int i;\\\n// single line trailing comment")); 256 257 verifyGoogleFormat("int a; // Trailing comment."); 258 259 verifyFormat("someFunction(anotherFunction( // Force break.\n" 260 " parameter));"); 261 262 verifyGoogleFormat("#endif // HEADER_GUARD"); 263 264 verifyFormat("const char *test[] = {\n" 265 " // A\n" 266 " \"aaaa\",\n" 267 " // B\n" 268 " \"aaaaa\"};"); 269 verifyGoogleFormat( 270 "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 271 " aaaaaaaaaaaaaaaaaaaaaa); // 81_cols_with_this_comment"); 272 EXPECT_EQ("D(a, {\n" 273 " // test\n" 274 " int a;\n" 275 "});", 276 format("D(a, {\n" 277 "// test\n" 278 "int a;\n" 279 "});")); 280 281 EXPECT_EQ("lineWith(); // comment\n" 282 "// at start\n" 283 "otherLine();", 284 format("lineWith(); // comment\n" 285 "// at start\n" 286 "otherLine();")); 287 EXPECT_EQ("lineWith(); // comment\n" 288 "/*\n" 289 " * at start */\n" 290 "otherLine();", 291 format("lineWith(); // comment\n" 292 "/*\n" 293 " * at start */\n" 294 "otherLine();")); 295 EXPECT_EQ("lineWith(); // comment\n" 296 " // at start\n" 297 "otherLine();", 298 format("lineWith(); // comment\n" 299 " // at start\n" 300 "otherLine();")); 301 302 EXPECT_EQ("lineWith(); // comment\n" 303 "// at start\n" 304 "otherLine(); // comment", 305 format("lineWith(); // comment\n" 306 "// at start\n" 307 "otherLine(); // comment")); 308 EXPECT_EQ("lineWith();\n" 309 "// at start\n" 310 "otherLine(); // comment", 311 format("lineWith();\n" 312 " // at start\n" 313 "otherLine(); // comment")); 314 EXPECT_EQ("// first\n" 315 "// at start\n" 316 "otherLine(); // comment", 317 format("// first\n" 318 " // at start\n" 319 "otherLine(); // comment")); 320 EXPECT_EQ("f();\n" 321 "// first\n" 322 "// at start\n" 323 "otherLine(); // comment", 324 format("f();\n" 325 "// first\n" 326 " // at start\n" 327 "otherLine(); // comment")); 328 verifyFormat("f(); // comment\n" 329 "// first\n" 330 "// at start\n" 331 "otherLine();"); 332 EXPECT_EQ("f(); // comment\n" 333 "// first\n" 334 "// at start\n" 335 "otherLine();", 336 format("f(); // comment\n" 337 "// first\n" 338 " // at start\n" 339 "otherLine();")); 340 EXPECT_EQ("f(); // comment\n" 341 " // first\n" 342 "// at start\n" 343 "otherLine();", 344 format("f(); // comment\n" 345 " // first\n" 346 "// at start\n" 347 "otherLine();")); 348 EXPECT_EQ("void f() {\n" 349 " lineWith(); // comment\n" 350 " // at start\n" 351 "}", 352 format("void f() {\n" 353 " lineWith(); // comment\n" 354 " // at start\n" 355 "}")); 356 EXPECT_EQ("int xy; // a\n" 357 "int z; // b", 358 format("int xy; // a\n" 359 "int z; //b")); 360 EXPECT_EQ("int xy; // a\n" 361 "int z; // bb", 362 format("int xy; // a\n" 363 "int z; //bb", 364 getLLVMStyleWithColumns(12))); 365 366 verifyFormat("#define A \\\n" 367 " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n" 368 " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */", 369 getLLVMStyleWithColumns(60)); 370 verifyFormat( 371 "#define A \\\n" 372 " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n" 373 " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */", 374 getLLVMStyleWithColumns(61)); 375 376 verifyFormat("if ( // This is some comment\n" 377 " x + 3) {\n" 378 "}"); 379 EXPECT_EQ("if ( // This is some comment\n" 380 " // spanning two lines\n" 381 " x + 3) {\n" 382 "}", 383 format("if( // This is some comment\n" 384 " // spanning two lines\n" 385 " x + 3) {\n" 386 "}")); 387 388 verifyNoCrash("/\\\n/"); 389 verifyNoCrash("/\\\n* */"); 390 // The 0-character somehow makes the lexer return a proper comment. 391 verifyNoCrash(StringRef("/*\\\0\n/", 6)); 392 } 393 394 TEST_F(FormatTestComments, KeepsParameterWithTrailingCommentsOnTheirOwnLine) { 395 EXPECT_EQ("SomeFunction(a,\n" 396 " b, // comment\n" 397 " c);", 398 format("SomeFunction(a,\n" 399 " b, // comment\n" 400 " c);")); 401 EXPECT_EQ("SomeFunction(a, b,\n" 402 " // comment\n" 403 " c);", 404 format("SomeFunction(a,\n" 405 " b,\n" 406 " // comment\n" 407 " c);")); 408 EXPECT_EQ("SomeFunction(a, b, // comment (unclear relation)\n" 409 " c);", 410 format("SomeFunction(a, b, // comment (unclear relation)\n" 411 " c);")); 412 EXPECT_EQ("SomeFunction(a, // comment\n" 413 " b,\n" 414 " c); // comment", 415 format("SomeFunction(a, // comment\n" 416 " b,\n" 417 " c); // comment")); 418 EXPECT_EQ("aaaaaaaaaa(aaaa(aaaa,\n" 419 " aaaa), //\n" 420 " aaaa, bbbbb);", 421 format("aaaaaaaaaa(aaaa(aaaa,\n" 422 "aaaa), //\n" 423 "aaaa, bbbbb);")); 424 } 425 426 TEST_F(FormatTestComments, RemovesTrailingWhitespaceOfComments) { 427 EXPECT_EQ("// comment", format("// comment ")); 428 EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment", 429 format("int aaaaaaa, bbbbbbb; // comment ", 430 getLLVMStyleWithColumns(33))); 431 EXPECT_EQ("// comment\\\n", format("// comment\\\n \t \v \f ")); 432 EXPECT_EQ("// comment \\\n", format("// comment \\\n \t \v \f ")); 433 } 434 435 TEST_F(FormatTestComments, UnderstandsBlockComments) { 436 verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);"); 437 verifyFormat("void f() { g(/*aaa=*/x, /*bbb=*/!y, /*c=*/::c); }"); 438 EXPECT_EQ("f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n" 439 " bbbbbbbbbbbbbbbbbbbbbbbbb);", 440 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \\\n" 441 "/* Trailing comment for aa... */\n" 442 " bbbbbbbbbbbbbbbbbbbbbbbbb);")); 443 EXPECT_EQ( 444 "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 445 " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);", 446 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n" 447 "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);")); 448 EXPECT_EQ( 449 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 450 " aaaaaaaaaaaaaaaaaa,\n" 451 " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n" 452 "}", 453 format("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 454 " aaaaaaaaaaaaaaaaaa ,\n" 455 " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n" 456 "}")); 457 verifyFormat("f(/* aaaaaaaaaaaaaaaaaa = */\n" 458 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 459 460 FormatStyle NoBinPacking = getLLVMStyle(); 461 NoBinPacking.BinPackParameters = false; 462 verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n" 463 " /* parameter 2 */ aaaaaa,\n" 464 " /* parameter 3 */ aaaaaa,\n" 465 " /* parameter 4 */ aaaaaa);", 466 NoBinPacking); 467 468 // Aligning block comments in macros. 469 verifyGoogleFormat("#define A \\\n" 470 " int i; /*a*/ \\\n" 471 " int jjj; /*b*/"); 472 } 473 474 TEST_F(FormatTestComments, AlignsBlockComments) { 475 EXPECT_EQ("/*\n" 476 " * Really multi-line\n" 477 " * comment.\n" 478 " */\n" 479 "void f() {}", 480 format(" /*\n" 481 " * Really multi-line\n" 482 " * comment.\n" 483 " */\n" 484 " void f() {}")); 485 EXPECT_EQ("class C {\n" 486 " /*\n" 487 " * Another multi-line\n" 488 " * comment.\n" 489 " */\n" 490 " void f() {}\n" 491 "};", 492 format("class C {\n" 493 "/*\n" 494 " * Another multi-line\n" 495 " * comment.\n" 496 " */\n" 497 "void f() {}\n" 498 "};")); 499 EXPECT_EQ("/*\n" 500 " 1. This is a comment with non-trivial formatting.\n" 501 " 1.1. We have to indent/outdent all lines equally\n" 502 " 1.1.1. to keep the formatting.\n" 503 " */", 504 format(" /*\n" 505 " 1. This is a comment with non-trivial formatting.\n" 506 " 1.1. We have to indent/outdent all lines equally\n" 507 " 1.1.1. to keep the formatting.\n" 508 " */")); 509 EXPECT_EQ("/*\n" 510 "Don't try to outdent if there's not enough indentation.\n" 511 "*/", 512 format(" /*\n" 513 " Don't try to outdent if there's not enough indentation.\n" 514 " */")); 515 516 EXPECT_EQ("int i; /* Comment with empty...\n" 517 " *\n" 518 " * line. */", 519 format("int i; /* Comment with empty...\n" 520 " *\n" 521 " * line. */")); 522 EXPECT_EQ("int foobar = 0; /* comment */\n" 523 "int bar = 0; /* multiline\n" 524 " comment 1 */\n" 525 "int baz = 0; /* multiline\n" 526 " comment 2 */\n" 527 "int bzz = 0; /* multiline\n" 528 " comment 3 */", 529 format("int foobar = 0; /* comment */\n" 530 "int bar = 0; /* multiline\n" 531 " comment 1 */\n" 532 "int baz = 0; /* multiline\n" 533 " comment 2 */\n" 534 "int bzz = 0; /* multiline\n" 535 " comment 3 */")); 536 EXPECT_EQ("int foobar = 0; /* comment */\n" 537 "int bar = 0; /* multiline\n" 538 " comment */\n" 539 "int baz = 0; /* multiline\n" 540 "comment */", 541 format("int foobar = 0; /* comment */\n" 542 "int bar = 0; /* multiline\n" 543 "comment */\n" 544 "int baz = 0; /* multiline\n" 545 "comment */")); 546 } 547 548 TEST_F(FormatTestComments, CommentReflowingCanBeTurnedOff) { 549 FormatStyle Style = getLLVMStyleWithColumns(20); 550 Style.ReflowComments = false; 551 verifyFormat("// aaaaaaaaa aaaaaaaaaa aaaaaaaaaa", Style); 552 verifyFormat("/* aaaaaaaaa aaaaaaaaaa aaaaaaaaaa */", Style); 553 } 554 555 TEST_F(FormatTestComments, CorrectlyHandlesLengthOfBlockComments) { 556 EXPECT_EQ("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 557 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */", 558 format("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 559 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */")); 560 EXPECT_EQ( 561 "void ffffffffffff(\n" 562 " int aaaaaaaa, int bbbbbbbb,\n" 563 " int cccccccccccc) { /*\n" 564 " aaaaaaaaaa\n" 565 " aaaaaaaaaaaaa\n" 566 " bbbbbbbbbbbbbb\n" 567 " bbbbbbbbbb\n" 568 " */\n" 569 "}", 570 format("void ffffffffffff(int aaaaaaaa, int bbbbbbbb, int cccccccccccc)\n" 571 "{ /*\n" 572 " aaaaaaaaaa aaaaaaaaaaaaa\n" 573 " bbbbbbbbbbbbbb bbbbbbbbbb\n" 574 " */\n" 575 "}", 576 getLLVMStyleWithColumns(40))); 577 } 578 579 TEST_F(FormatTestComments, DontBreakNonTrailingBlockComments) { 580 EXPECT_EQ("void ffffffffff(\n" 581 " int aaaaa /* test */);", 582 format("void ffffffffff(int aaaaa /* test */);", 583 getLLVMStyleWithColumns(35))); 584 } 585 586 TEST_F(FormatTestComments, SplitsLongCxxComments) { 587 EXPECT_EQ("// A comment that\n" 588 "// doesn't fit on\n" 589 "// one line", 590 format("// A comment that doesn't fit on one line", 591 getLLVMStyleWithColumns(20))); 592 EXPECT_EQ("/// A comment that\n" 593 "/// doesn't fit on\n" 594 "/// one line", 595 format("/// A comment that doesn't fit on one line", 596 getLLVMStyleWithColumns(20))); 597 EXPECT_EQ("//! A comment that\n" 598 "//! doesn't fit on\n" 599 "//! one line", 600 format("//! A comment that doesn't fit on one line", 601 getLLVMStyleWithColumns(20))); 602 EXPECT_EQ("// a b c d\n" 603 "// e f g\n" 604 "// h i j k", 605 format("// a b c d e f g h i j k", getLLVMStyleWithColumns(10))); 606 EXPECT_EQ( 607 "// a b c d\n" 608 "// e f g\n" 609 "// h i j k", 610 format("\\\n// a b c d e f g h i j k", getLLVMStyleWithColumns(10))); 611 EXPECT_EQ("if (true) // A comment that\n" 612 " // doesn't fit on\n" 613 " // one line", 614 format("if (true) // A comment that doesn't fit on one line ", 615 getLLVMStyleWithColumns(30))); 616 EXPECT_EQ("// Don't_touch_leading_whitespace", 617 format("// Don't_touch_leading_whitespace", 618 getLLVMStyleWithColumns(20))); 619 EXPECT_EQ("// Add leading\n" 620 "// whitespace", 621 format("//Add leading whitespace", getLLVMStyleWithColumns(20))); 622 EXPECT_EQ("/// Add leading\n" 623 "/// whitespace", 624 format("///Add leading whitespace", getLLVMStyleWithColumns(20))); 625 EXPECT_EQ("//! Add leading\n" 626 "//! whitespace", 627 format("//!Add leading whitespace", getLLVMStyleWithColumns(20))); 628 EXPECT_EQ("// whitespace", format("//whitespace", getLLVMStyle())); 629 EXPECT_EQ("// Even if it makes the line exceed the column\n" 630 "// limit", 631 format("//Even if it makes the line exceed the column limit", 632 getLLVMStyleWithColumns(51))); 633 EXPECT_EQ("//--But not here", format("//--But not here", getLLVMStyle())); 634 EXPECT_EQ("/// line 1\n" 635 "// add leading whitespace", 636 format("/// line 1\n" 637 "//add leading whitespace", 638 getLLVMStyleWithColumns(30))); 639 EXPECT_EQ("/// line 1\n" 640 "/// line 2\n" 641 "//! line 3\n" 642 "//! line 4\n" 643 "//! line 5\n" 644 "// line 6\n" 645 "// line 7", 646 format("///line 1\n" 647 "///line 2\n" 648 "//! line 3\n" 649 "//!line 4\n" 650 "//!line 5\n" 651 "// line 6\n" 652 "//line 7", getLLVMStyleWithColumns(20))); 653 654 EXPECT_EQ("// aa bb cc dd", 655 format("// aa bb cc dd ", 656 getLLVMStyleWithColumns(15))); 657 658 EXPECT_EQ("// A comment before\n" 659 "// a macro\n" 660 "// definition\n" 661 "#define a b", 662 format("// A comment before a macro definition\n" 663 "#define a b", 664 getLLVMStyleWithColumns(20))); 665 EXPECT_EQ("void ffffff(\n" 666 " int aaaaaaaaa, // wwww\n" 667 " int bbbbbbbbbb, // xxxxxxx\n" 668 " // yyyyyyyyyy\n" 669 " int c, int d, int e) {}", 670 format("void ffffff(\n" 671 " int aaaaaaaaa, // wwww\n" 672 " int bbbbbbbbbb, // xxxxxxx yyyyyyyyyy\n" 673 " int c, int d, int e) {}", 674 getLLVMStyleWithColumns(40))); 675 EXPECT_EQ("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 676 format("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 677 getLLVMStyleWithColumns(20))); 678 EXPECT_EQ( 679 "#define XXX // a b c d\n" 680 " // e f g h", 681 format("#define XXX // a b c d e f g h", getLLVMStyleWithColumns(22))); 682 EXPECT_EQ( 683 "#define XXX // q w e r\n" 684 " // t y u i", 685 format("#define XXX //q w e r t y u i", getLLVMStyleWithColumns(22))); 686 EXPECT_EQ("{\n" 687 " //\n" 688 " //\\\n" 689 " // long 1 2 3 4\n" 690 " // 5\n" 691 "}", 692 format("{\n" 693 " //\n" 694 " //\\\n" 695 " // long 1 2 3 4 5\n" 696 "}", 697 getLLVMStyleWithColumns(20))); 698 } 699 700 TEST_F(FormatTestComments, PreservesHangingIndentInCxxComments) { 701 EXPECT_EQ("// A comment\n" 702 "// that doesn't\n" 703 "// fit on one\n" 704 "// line", 705 format("// A comment that doesn't fit on one line", 706 getLLVMStyleWithColumns(20))); 707 EXPECT_EQ("/// A comment\n" 708 "/// that doesn't\n" 709 "/// fit on one\n" 710 "/// line", 711 format("/// A comment that doesn't fit on one line", 712 getLLVMStyleWithColumns(20))); 713 } 714 715 TEST_F(FormatTestComments, DontSplitLineCommentsWithEscapedNewlines) { 716 EXPECT_EQ("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 717 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 718 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 719 format("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 720 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 721 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")); 722 EXPECT_EQ("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 723 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 724 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 725 format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 726 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 727 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 728 getLLVMStyleWithColumns(50))); 729 // FIXME: One day we might want to implement adjustment of leading whitespace 730 // of the consecutive lines in this kind of comment: 731 EXPECT_EQ("double\n" 732 " a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 733 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 734 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 735 format("double a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 736 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 737 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 738 getLLVMStyleWithColumns(49))); 739 } 740 741 TEST_F(FormatTestComments, DontSplitLineCommentsWithPragmas) { 742 FormatStyle Pragmas = getLLVMStyleWithColumns(30); 743 Pragmas.CommentPragmas = "^ IWYU pragma:"; 744 EXPECT_EQ( 745 "// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", 746 format("// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", Pragmas)); 747 EXPECT_EQ( 748 "/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", 749 format("/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", Pragmas)); 750 } 751 752 TEST_F(FormatTestComments, PriorityOfCommentBreaking) { 753 EXPECT_EQ("if (xxx ==\n" 754 " yyy && // aaaaaaaaaaaa bbbbbbbbb\n" 755 " zzz)\n" 756 " q();", 757 format("if (xxx == yyy && // aaaaaaaaaaaa bbbbbbbbb\n" 758 " zzz) q();", 759 getLLVMStyleWithColumns(40))); 760 EXPECT_EQ("if (xxxxxxxxxx ==\n" 761 " yyy && // aaaaaa bbbbbbbb cccc\n" 762 " zzz)\n" 763 " q();", 764 format("if (xxxxxxxxxx == yyy && // aaaaaa bbbbbbbb cccc\n" 765 " zzz) q();", 766 getLLVMStyleWithColumns(40))); 767 EXPECT_EQ("if (xxxxxxxxxx &&\n" 768 " yyy || // aaaaaa bbbbbbbb cccc\n" 769 " zzz)\n" 770 " q();", 771 format("if (xxxxxxxxxx && yyy || // aaaaaa bbbbbbbb cccc\n" 772 " zzz) q();", 773 getLLVMStyleWithColumns(40))); 774 EXPECT_EQ("fffffffff(\n" 775 " &xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n" 776 " zzz);", 777 format("fffffffff(&xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n" 778 " zzz);", 779 getLLVMStyleWithColumns(40))); 780 } 781 782 TEST_F(FormatTestComments, MultiLineCommentsInDefines) { 783 EXPECT_EQ("#define A(x) /* \\\n" 784 " a comment \\\n" 785 " inside */ \\\n" 786 " f();", 787 format("#define A(x) /* \\\n" 788 " a comment \\\n" 789 " inside */ \\\n" 790 " f();", 791 getLLVMStyleWithColumns(17))); 792 EXPECT_EQ("#define A( \\\n" 793 " x) /* \\\n" 794 " a comment \\\n" 795 " inside */ \\\n" 796 " f();", 797 format("#define A( \\\n" 798 " x) /* \\\n" 799 " a comment \\\n" 800 " inside */ \\\n" 801 " f();", 802 getLLVMStyleWithColumns(17))); 803 } 804 805 TEST_F(FormatTestComments, ParsesCommentsAdjacentToPPDirectives) { 806 EXPECT_EQ("namespace {}\n// Test\n#define A", 807 format("namespace {}\n // Test\n#define A")); 808 EXPECT_EQ("namespace {}\n/* Test */\n#define A", 809 format("namespace {}\n /* Test */\n#define A")); 810 EXPECT_EQ("namespace {}\n/* Test */ #define A", 811 format("namespace {}\n /* Test */ #define A")); 812 } 813 814 TEST_F(FormatTestComments, SplitsLongLinesInComments) { 815 EXPECT_EQ("/* This is a long\n" 816 " * comment that\n" 817 " * doesn't\n" 818 " * fit on one line.\n" 819 " */", 820 format("/* " 821 "This is a long " 822 "comment that " 823 "doesn't " 824 "fit on one line. */", 825 getLLVMStyleWithColumns(20))); 826 EXPECT_EQ( 827 "/* a b c d\n" 828 " * e f g\n" 829 " * h i j k\n" 830 " */", 831 format("/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10))); 832 EXPECT_EQ( 833 "/* a b c d\n" 834 " * e f g\n" 835 " * h i j k\n" 836 " */", 837 format("\\\n/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10))); 838 EXPECT_EQ("/*\n" 839 "This is a long\n" 840 "comment that doesn't\n" 841 "fit on one line.\n" 842 "*/", 843 format("/*\n" 844 "This is a long " 845 "comment that doesn't " 846 "fit on one line. \n" 847 "*/", 848 getLLVMStyleWithColumns(20))); 849 EXPECT_EQ("/*\n" 850 " * This is a long\n" 851 " * comment that\n" 852 " * doesn't fit on\n" 853 " * one line.\n" 854 " */", 855 format("/* \n" 856 " * This is a long " 857 " comment that " 858 " doesn't fit on " 859 " one line. \n" 860 " */", 861 getLLVMStyleWithColumns(20))); 862 EXPECT_EQ("/*\n" 863 " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n" 864 " * so_it_should_be_broken\n" 865 " * wherever_a_space_occurs\n" 866 " */", 867 format("/*\n" 868 " * This_is_a_comment_with_words_that_dont_fit_on_one_line " 869 " so_it_should_be_broken " 870 " wherever_a_space_occurs \n" 871 " */", 872 getLLVMStyleWithColumns(20))); 873 EXPECT_EQ("/*\n" 874 " * This_comment_can_not_be_broken_into_lines\n" 875 " */", 876 format("/*\n" 877 " * This_comment_can_not_be_broken_into_lines\n" 878 " */", 879 getLLVMStyleWithColumns(20))); 880 EXPECT_EQ("{\n" 881 " /*\n" 882 " This is another\n" 883 " long comment that\n" 884 " doesn't fit on one\n" 885 " line 1234567890\n" 886 " */\n" 887 "}", 888 format("{\n" 889 "/*\n" 890 "This is another " 891 " long comment that " 892 " doesn't fit on one" 893 " line 1234567890\n" 894 "*/\n" 895 "}", 896 getLLVMStyleWithColumns(20))); 897 EXPECT_EQ("{\n" 898 " /*\n" 899 " * This i s\n" 900 " * another comment\n" 901 " * t hat doesn' t\n" 902 " * fit on one l i\n" 903 " * n e\n" 904 " */\n" 905 "}", 906 format("{\n" 907 "/*\n" 908 " * This i s" 909 " another comment" 910 " t hat doesn' t" 911 " fit on one l i" 912 " n e\n" 913 " */\n" 914 "}", 915 getLLVMStyleWithColumns(20))); 916 EXPECT_EQ("/*\n" 917 " * This is a long\n" 918 " * comment that\n" 919 " * doesn't fit on\n" 920 " * one line\n" 921 " */", 922 format(" /*\n" 923 " * This is a long comment that doesn't fit on one line\n" 924 " */", 925 getLLVMStyleWithColumns(20))); 926 EXPECT_EQ("{\n" 927 " if (something) /* This is a\n" 928 " long\n" 929 " comment */\n" 930 " ;\n" 931 "}", 932 format("{\n" 933 " if (something) /* This is a long comment */\n" 934 " ;\n" 935 "}", 936 getLLVMStyleWithColumns(30))); 937 938 EXPECT_EQ("/* A comment before\n" 939 " * a macro\n" 940 " * definition */\n" 941 "#define a b", 942 format("/* A comment before a macro definition */\n" 943 "#define a b", 944 getLLVMStyleWithColumns(20))); 945 946 EXPECT_EQ("/* some comment\n" 947 " * a comment\n" 948 "* that we break\n" 949 " * another comment\n" 950 "* we have to break\n" 951 "* a left comment\n" 952 " */", 953 format(" /* some comment\n" 954 " * a comment that we break\n" 955 " * another comment we have to break\n" 956 "* a left comment\n" 957 " */", 958 getLLVMStyleWithColumns(20))); 959 960 EXPECT_EQ("/**\n" 961 " * multiline block\n" 962 " * comment\n" 963 " *\n" 964 " */", 965 format("/**\n" 966 " * multiline block comment\n" 967 " *\n" 968 " */", 969 getLLVMStyleWithColumns(20))); 970 971 EXPECT_EQ("/*\n" 972 "\n" 973 "\n" 974 " */\n", 975 format(" /* \n" 976 " \n" 977 " \n" 978 " */\n")); 979 980 EXPECT_EQ("/* a a */", 981 format("/* a a */", getLLVMStyleWithColumns(15))); 982 EXPECT_EQ("/* a a bc */", 983 format("/* a a bc */", getLLVMStyleWithColumns(15))); 984 EXPECT_EQ("/* aaa aaa\n" 985 " * aaaaa */", 986 format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15))); 987 EXPECT_EQ("/* aaa aaa\n" 988 " * aaaaa */", 989 format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15))); 990 } 991 992 TEST_F(FormatTestComments, SplitsLongLinesInCommentsInPreprocessor) { 993 EXPECT_EQ("#define X \\\n" 994 " /* \\\n" 995 " Test \\\n" 996 " Macro comment \\\n" 997 " with a long \\\n" 998 " line \\\n" 999 " */ \\\n" 1000 " A + B", 1001 format("#define X \\\n" 1002 " /*\n" 1003 " Test\n" 1004 " Macro comment with a long line\n" 1005 " */ \\\n" 1006 " A + B", 1007 getLLVMStyleWithColumns(20))); 1008 EXPECT_EQ("#define X \\\n" 1009 " /* Macro comment \\\n" 1010 " with a long \\\n" 1011 " line */ \\\n" 1012 " A + B", 1013 format("#define X \\\n" 1014 " /* Macro comment with a long\n" 1015 " line */ \\\n" 1016 " A + B", 1017 getLLVMStyleWithColumns(20))); 1018 EXPECT_EQ("#define X \\\n" 1019 " /* Macro comment \\\n" 1020 " * with a long \\\n" 1021 " * line */ \\\n" 1022 " A + B", 1023 format("#define X \\\n" 1024 " /* Macro comment with a long line */ \\\n" 1025 " A + B", 1026 getLLVMStyleWithColumns(20))); 1027 } 1028 1029 TEST_F(FormatTestComments, CommentsInStaticInitializers) { 1030 EXPECT_EQ( 1031 "static SomeType type = {aaaaaaaaaaaaaaaaaaaa, /* comment */\n" 1032 " aaaaaaaaaaaaaaaaaaaa /* comment */,\n" 1033 " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n" 1034 " aaaaaaaaaaaaaaaaaaaa, // comment\n" 1035 " aaaaaaaaaaaaaaaaaaaa};", 1036 format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n" 1037 " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n" 1038 " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n" 1039 " aaaaaaaaaaaaaaaaaaaa , // comment\n" 1040 " aaaaaaaaaaaaaaaaaaaa };")); 1041 verifyFormat("static SomeType type = {aaaaaaaaaaa, // comment for aa...\n" 1042 " bbbbbbbbbbb, ccccccccccc};"); 1043 verifyFormat("static SomeType type = {aaaaaaaaaaa,\n" 1044 " // comment for bb....\n" 1045 " bbbbbbbbbbb, ccccccccccc};"); 1046 verifyGoogleFormat( 1047 "static SomeType type = {aaaaaaaaaaa, // comment for aa...\n" 1048 " bbbbbbbbbbb, ccccccccccc};"); 1049 verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n" 1050 " // comment for bb....\n" 1051 " bbbbbbbbbbb, ccccccccccc};"); 1052 1053 verifyFormat("S s = {{a, b, c}, // Group #1\n" 1054 " {d, e, f}, // Group #2\n" 1055 " {g, h, i}}; // Group #3"); 1056 verifyFormat("S s = {{// Group #1\n" 1057 " a, b, c},\n" 1058 " {// Group #2\n" 1059 " d, e, f},\n" 1060 " {// Group #3\n" 1061 " g, h, i}};"); 1062 1063 EXPECT_EQ("S s = {\n" 1064 " // Some comment\n" 1065 " a,\n" 1066 "\n" 1067 " // Comment after empty line\n" 1068 " b}", 1069 format("S s = {\n" 1070 " // Some comment\n" 1071 " a,\n" 1072 " \n" 1073 " // Comment after empty line\n" 1074 " b\n" 1075 "}")); 1076 EXPECT_EQ("S s = {\n" 1077 " /* Some comment */\n" 1078 " a,\n" 1079 "\n" 1080 " /* Comment after empty line */\n" 1081 " b}", 1082 format("S s = {\n" 1083 " /* Some comment */\n" 1084 " a,\n" 1085 " \n" 1086 " /* Comment after empty line */\n" 1087 " b\n" 1088 "}")); 1089 verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n" 1090 " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n" 1091 " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n" 1092 " 0x00, 0x00, 0x00, 0x00}; // comment\n"); 1093 } 1094 1095 TEST_F(FormatTestComments, LineCommentsAfterRightBrace) { 1096 EXPECT_EQ("if (true) { // comment about branch\n" 1097 " // comment about f\n" 1098 " f();\n" 1099 "}", 1100 format("if (true) { // comment about branch\n" 1101 " // comment about f\n" 1102 " f();\n" 1103 "}", 1104 getLLVMStyleWithColumns(80))); 1105 EXPECT_EQ("if (1) { // if line 1\n" 1106 " // if line 2\n" 1107 " // if line 3\n" 1108 " // f line 1\n" 1109 " // f line 2\n" 1110 " f();\n" 1111 "} else { // else line 1\n" 1112 " // else line 2\n" 1113 " // else line 3\n" 1114 " // g line 1\n" 1115 " g();\n" 1116 "}", 1117 format("if (1) { // if line 1\n" 1118 " // if line 2\n" 1119 " // if line 3\n" 1120 " // f line 1\n" 1121 " // f line 2\n" 1122 " f();\n" 1123 "} else { // else line 1\n" 1124 " // else line 2\n" 1125 " // else line 3\n" 1126 " // g line 1\n" 1127 " g();\n" 1128 "}")); 1129 EXPECT_EQ("do { // line 1\n" 1130 " // line 2\n" 1131 " // line 3\n" 1132 " f();\n" 1133 "} while (true);", 1134 format("do { // line 1\n" 1135 " // line 2\n" 1136 " // line 3\n" 1137 " f();\n" 1138 "} while (true);", 1139 getLLVMStyleWithColumns(80))); 1140 EXPECT_EQ("while (a < b) { // line 1\n" 1141 " // line 2\n" 1142 " // line 3\n" 1143 " f();\n" 1144 "}", 1145 format("while (a < b) {// line 1\n" 1146 " // line 2\n" 1147 " // line 3\n" 1148 " f();\n" 1149 "}", 1150 getLLVMStyleWithColumns(80))); 1151 } 1152 1153 TEST_F(FormatTestComments, ReflowsComments) { 1154 // Break a long line and reflow with the full next line. 1155 EXPECT_EQ("// long long long\n" 1156 "// long long", 1157 format("// long long long long\n" 1158 "// long", 1159 getLLVMStyleWithColumns(20))); 1160 1161 // Keep the trailing newline while reflowing. 1162 EXPECT_EQ("// long long long\n" 1163 "// long long\n", 1164 format("// long long long long\n" 1165 "// long\n", 1166 getLLVMStyleWithColumns(20))); 1167 1168 // Break a long line and reflow with a part of the next line. 1169 EXPECT_EQ("// long long long\n" 1170 "// long long\n" 1171 "// long_long", 1172 format("// long long long long\n" 1173 "// long long_long", 1174 getLLVMStyleWithColumns(20))); 1175 1176 // Break but do not reflow if the first word from the next line is too long. 1177 EXPECT_EQ("// long long long\n" 1178 "// long\n" 1179 "// long_long_long\n", 1180 format("// long long long long\n" 1181 "// long_long_long\n", 1182 getLLVMStyleWithColumns(20))); 1183 1184 // Don't break or reflow short lines. 1185 verifyFormat("// long\n" 1186 "// long long long lo\n" 1187 "// long long long lo\n" 1188 "// long", 1189 getLLVMStyleWithColumns(20)); 1190 1191 // Keep prefixes and decorations while reflowing. 1192 EXPECT_EQ("/// long long long\n" 1193 "/// long long\n", 1194 format("/// long long long long\n" 1195 "/// long\n", 1196 getLLVMStyleWithColumns(20))); 1197 EXPECT_EQ("//! long long long\n" 1198 "//! long long\n", 1199 format("//! long long long long\n" 1200 "//! long\n", 1201 getLLVMStyleWithColumns(20))); 1202 EXPECT_EQ("/* long long long\n" 1203 " * long long */", 1204 format("/* long long long long\n" 1205 " * long */", 1206 getLLVMStyleWithColumns(20))); 1207 1208 // Don't bring leading whitespace up while reflowing. 1209 EXPECT_EQ("/* long long long\n" 1210 " * long long long\n" 1211 " */", 1212 format("/* long long long long\n" 1213 " * long long\n" 1214 " */", 1215 getLLVMStyleWithColumns(20))); 1216 1217 // Reflow the last line of a block comment with its trailing '*/'. 1218 EXPECT_EQ("/* long long long\n" 1219 " long long */", 1220 format("/* long long long long\n" 1221 " long */", 1222 getLLVMStyleWithColumns(20))); 1223 1224 // Reflow two short lines; keep the postfix of the last one. 1225 EXPECT_EQ("/* long long long\n" 1226 " * long long long */", 1227 format("/* long long long long\n" 1228 " * long\n" 1229 " * long */", 1230 getLLVMStyleWithColumns(20))); 1231 1232 // Put the postfix of the last short reflow line on a newline if it doesn't 1233 // fit. 1234 EXPECT_EQ("/* long long long\n" 1235 " * long long longg\n" 1236 " */", 1237 format("/* long long long long\n" 1238 " * long\n" 1239 " * longg */", 1240 getLLVMStyleWithColumns(20))); 1241 1242 // Reflow lines with leading whitespace. 1243 EXPECT_EQ("{\n" 1244 " /*\n" 1245 " * long long long\n" 1246 " * long long long\n" 1247 " * long long long\n" 1248 " */\n" 1249 "}", 1250 format("{\n" 1251 "/*\n" 1252 " * long long long long\n" 1253 " * long\n" 1254 " * long long long long\n" 1255 " */\n" 1256 "}", 1257 getLLVMStyleWithColumns(20))); 1258 1259 // Break single line block comments that are first in the line with ' *' 1260 // decoration. 1261 EXPECT_EQ("/* long long long\n" 1262 " * long */", 1263 format("/* long long long long */", getLLVMStyleWithColumns(20))); 1264 1265 // Break single line block comment that are not first in the line with ' ' 1266 // decoration. 1267 EXPECT_EQ("int i; /* long long\n" 1268 " long */", 1269 format("int i; /* long long long */", getLLVMStyleWithColumns(20))); 1270 1271 // Reflow a line that goes just over the column limit. 1272 EXPECT_EQ("// long long long\n" 1273 "// lon long", 1274 format("// long long long lon\n" 1275 "// long", 1276 getLLVMStyleWithColumns(20))); 1277 1278 // Stop reflowing if the next line has a different indentation than the 1279 // previous line. 1280 EXPECT_EQ("// long long long\n" 1281 "// long\n" 1282 "// long long\n" 1283 "// long", 1284 format("// long long long long\n" 1285 "// long long\n" 1286 "// long", 1287 getLLVMStyleWithColumns(20))); 1288 1289 // Reflow into the last part of a really long line that has been broken into 1290 // multiple lines. 1291 EXPECT_EQ("// long long long\n" 1292 "// long long long\n" 1293 "// long long long\n", 1294 format("// long long long long long long long long\n" 1295 "// long\n", 1296 getLLVMStyleWithColumns(20))); 1297 1298 // Break the first line, then reflow the beginning of the second and third 1299 // line up. 1300 EXPECT_EQ("// long long long\n" 1301 "// lon1 lon2 lon2\n" 1302 "// lon2 lon3 lon3", 1303 format("// long long long lon1\n" 1304 "// lon2 lon2 lon2\n" 1305 "// lon3 lon3", 1306 getLLVMStyleWithColumns(20))); 1307 1308 // Reflow the beginning of the second line, then break the rest. 1309 EXPECT_EQ("// long long long\n" 1310 "// lon1 lon2 lon2\n" 1311 "// lon2 lon2 lon2\n" 1312 "// lon3", 1313 format("// long long long lon1\n" 1314 "// lon2 lon2 lon2 lon2 lon2 lon3", 1315 getLLVMStyleWithColumns(20))); 1316 1317 // Shrink the first line, then reflow the second line up. 1318 EXPECT_EQ("// long long long", format("// long long\n" 1319 "// long", 1320 getLLVMStyleWithColumns(20))); 1321 1322 // Don't shrink leading whitespace. 1323 EXPECT_EQ("int i; /// a", 1324 format("int i; /// a", getLLVMStyleWithColumns(20))); 1325 1326 // Shrink trailing whitespace if there is no postfix and reflow. 1327 EXPECT_EQ("// long long long\n" 1328 "// long long", 1329 format("// long long long long \n" 1330 "// long", 1331 getLLVMStyleWithColumns(20))); 1332 1333 // Shrink trailing whitespace to a single one if there is postfix. 1334 EXPECT_EQ("/* long long long */", 1335 format("/* long long long */", getLLVMStyleWithColumns(20))); 1336 1337 // Break a block comment postfix if exceeding the line limit. 1338 EXPECT_EQ("/* long\n" 1339 " */", 1340 format("/* long */", getLLVMStyleWithColumns(20))); 1341 1342 // Reflow indented comments. 1343 EXPECT_EQ("{\n" 1344 " // long long long\n" 1345 " // long long\n" 1346 " int i; /* long lon\n" 1347 " g long\n" 1348 " */\n" 1349 "}", 1350 format("{\n" 1351 " // long long long long\n" 1352 " // long\n" 1353 " int i; /* long lon g\n" 1354 " long */\n" 1355 "}", 1356 getLLVMStyleWithColumns(20))); 1357 1358 // Don't realign trailing comments after reflow has happened. 1359 EXPECT_EQ("// long long long\n" 1360 "// long long\n" 1361 "long i; // long", 1362 format("// long long long long\n" 1363 "// long\n" 1364 "long i; // long", 1365 getLLVMStyleWithColumns(20))); 1366 EXPECT_EQ("// long long long\n" 1367 "// longng long long\n" 1368 "// long lo", 1369 format("// long long long longng\n" 1370 "// long long long\n" 1371 "// lo", 1372 getLLVMStyleWithColumns(20))); 1373 1374 // Reflow lines after a broken line. 1375 EXPECT_EQ("int a; // Trailing\n" 1376 " // comment on\n" 1377 " // 2 or 3\n" 1378 " // lines.\n", 1379 format("int a; // Trailing comment\n" 1380 " // on 2\n" 1381 " // or 3\n" 1382 " // lines.\n", 1383 getLLVMStyleWithColumns(20))); 1384 EXPECT_EQ("/// This long line\n" 1385 "/// gets reflown.\n", 1386 format("/// This long line gets\n" 1387 "/// reflown.\n", 1388 getLLVMStyleWithColumns(20))); 1389 EXPECT_EQ("//! This long line\n" 1390 "//! gets reflown.\n", 1391 format(" //! This long line gets\n" 1392 " //! reflown.\n", 1393 getLLVMStyleWithColumns(20))); 1394 EXPECT_EQ("/* This long line\n" 1395 " * gets reflown.\n" 1396 " */\n", 1397 format("/* This long line gets\n" 1398 " * reflown.\n" 1399 " */\n", 1400 getLLVMStyleWithColumns(20))); 1401 1402 // Reflow after indentation makes a line too long. 1403 EXPECT_EQ("{\n" 1404 " // long long long\n" 1405 " // lo long\n" 1406 "}\n", 1407 format("{\n" 1408 "// long long long lo\n" 1409 "// long\n" 1410 "}\n", 1411 getLLVMStyleWithColumns(20))); 1412 1413 // Break and reflow multiple lines. 1414 EXPECT_EQ("/*\n" 1415 " * Reflow the end of\n" 1416 " * line by 11 22 33\n" 1417 " * 4.\n" 1418 " */\n", 1419 format("/*\n" 1420 " * Reflow the end of line\n" 1421 " * by\n" 1422 " * 11\n" 1423 " * 22\n" 1424 " * 33\n" 1425 " * 4.\n" 1426 " */\n", 1427 getLLVMStyleWithColumns(20))); 1428 EXPECT_EQ("/// First line gets\n" 1429 "/// broken. Second\n" 1430 "/// line gets\n" 1431 "/// reflown and\n" 1432 "/// broken. Third\n" 1433 "/// gets reflown.\n", 1434 format("/// First line gets broken.\n" 1435 "/// Second line gets reflown and broken.\n" 1436 "/// Third gets reflown.\n", 1437 getLLVMStyleWithColumns(20))); 1438 EXPECT_EQ("int i; // first long\n" 1439 " // long snd\n" 1440 " // long.\n", 1441 format("int i; // first long long\n" 1442 " // snd long.\n", 1443 getLLVMStyleWithColumns(20))); 1444 EXPECT_EQ("{\n" 1445 " // first long line\n" 1446 " // line second\n" 1447 " // long line line\n" 1448 " // third long line\n" 1449 " // line\n" 1450 "}\n", 1451 format("{\n" 1452 " // first long line line\n" 1453 " // second long line line\n" 1454 " // third long line line\n" 1455 "}\n", 1456 getLLVMStyleWithColumns(20))); 1457 EXPECT_EQ("int i; /* first line\n" 1458 " * second\n" 1459 " * line third\n" 1460 " * line\n" 1461 " */", 1462 format("int i; /* first line\n" 1463 " * second line\n" 1464 " * third line\n" 1465 " */", 1466 getLLVMStyleWithColumns(20))); 1467 1468 // Reflow the last two lines of a section that starts with a line having 1469 // different indentation. 1470 EXPECT_EQ( 1471 "// long\n" 1472 "// long long long\n" 1473 "// long long", 1474 format("// long\n" 1475 "// long long long long\n" 1476 "// long", 1477 getLLVMStyleWithColumns(20))); 1478 1479 // Keep the block comment endling '*/' while reflowing. 1480 EXPECT_EQ("/* Long long long\n" 1481 " * line short */\n", 1482 format("/* Long long long line\n" 1483 " * short */\n", 1484 getLLVMStyleWithColumns(20))); 1485 1486 // Don't reflow between separate blocks of comments. 1487 EXPECT_EQ("/* First comment\n" 1488 " * block will */\n" 1489 "/* Snd\n" 1490 " */\n", 1491 format("/* First comment block\n" 1492 " * will */\n" 1493 "/* Snd\n" 1494 " */\n", 1495 getLLVMStyleWithColumns(20))); 1496 1497 // Don't reflow across blank comment lines. 1498 EXPECT_EQ("int i; // This long\n" 1499 " // line gets\n" 1500 " // broken.\n" 1501 " //\n" 1502 " // keep.\n", 1503 format("int i; // This long line gets broken.\n" 1504 " // \n" 1505 " // keep.\n", 1506 getLLVMStyleWithColumns(20))); 1507 EXPECT_EQ("{\n" 1508 " /// long long long\n" 1509 " /// long long\n" 1510 " ///\n" 1511 " /// long\n" 1512 "}", 1513 format("{\n" 1514 " /// long long long long\n" 1515 " /// long\n" 1516 " ///\n" 1517 " /// long\n" 1518 "}", 1519 getLLVMStyleWithColumns(20))); 1520 EXPECT_EQ("//! long long long\n" 1521 "//! long\n" 1522 "\n" 1523 "//! long", 1524 format("//! long long long long\n" 1525 "\n" 1526 "//! long", 1527 getLLVMStyleWithColumns(20))); 1528 EXPECT_EQ("/* long long long\n" 1529 " long\n" 1530 "\n" 1531 " long */", 1532 format("/* long long long long\n" 1533 "\n" 1534 " long */", 1535 getLLVMStyleWithColumns(20))); 1536 EXPECT_EQ("/* long long long\n" 1537 " * long\n" 1538 " *\n" 1539 " * long */", 1540 format("/* long long long long\n" 1541 " *\n" 1542 " * long */", 1543 getLLVMStyleWithColumns(20))); 1544 1545 // Don't reflow lines having content that is a single character. 1546 EXPECT_EQ("// long long long\n" 1547 "// long\n" 1548 "// l", 1549 format("// long long long long\n" 1550 "// l", 1551 getLLVMStyleWithColumns(20))); 1552 1553 // Don't reflow lines starting with two punctuation characters. 1554 EXPECT_EQ("// long long long\n" 1555 "// long\n" 1556 "// ... --- ...", 1557 format( 1558 "// long long long long\n" 1559 "// ... --- ...", 1560 getLLVMStyleWithColumns(20))); 1561 1562 // Don't reflow lines starting with '@'. 1563 EXPECT_EQ("// long long long\n" 1564 "// long\n" 1565 "// @param arg", 1566 format("// long long long long\n" 1567 "// @param arg", 1568 getLLVMStyleWithColumns(20))); 1569 1570 // Don't reflow lines starting with 'TODO'. 1571 EXPECT_EQ("// long long long\n" 1572 "// long\n" 1573 "// TODO: long", 1574 format("// long long long long\n" 1575 "// TODO: long", 1576 getLLVMStyleWithColumns(20))); 1577 1578 // Don't reflow lines starting with 'FIXME'. 1579 EXPECT_EQ("// long long long\n" 1580 "// long\n" 1581 "// FIXME: long", 1582 format("// long long long long\n" 1583 "// FIXME: long", 1584 getLLVMStyleWithColumns(20))); 1585 1586 // Don't reflow lines starting with 'XXX'. 1587 EXPECT_EQ("// long long long\n" 1588 "// long\n" 1589 "// XXX: long", 1590 format("// long long long long\n" 1591 "// XXX: long", 1592 getLLVMStyleWithColumns(20))); 1593 1594 // Don't reflow comment pragmas. 1595 EXPECT_EQ("// long long long\n" 1596 "// long\n" 1597 "// IWYU pragma:", 1598 format("// long long long long\n" 1599 "// IWYU pragma:", 1600 getLLVMStyleWithColumns(20))); 1601 EXPECT_EQ("/* long long long\n" 1602 " * long\n" 1603 " * IWYU pragma:\n" 1604 " */", 1605 format("/* long long long long\n" 1606 " * IWYU pragma:\n" 1607 " */", 1608 getLLVMStyleWithColumns(20))); 1609 1610 // Reflow lines that have a non-punctuation character among their first 2 1611 // characters. 1612 EXPECT_EQ("// long long long\n" 1613 "// long 'long'", 1614 format( 1615 "// long long long long\n" 1616 "// 'long'", 1617 getLLVMStyleWithColumns(20))); 1618 1619 // Don't reflow between separate blocks of comments. 1620 EXPECT_EQ("/* First comment\n" 1621 " * block will */\n" 1622 "/* Snd\n" 1623 " */\n", 1624 format("/* First comment block\n" 1625 " * will */\n" 1626 "/* Snd\n" 1627 " */\n", 1628 getLLVMStyleWithColumns(20))); 1629 1630 // Don't reflow lines having different indentation. 1631 EXPECT_EQ("// long long long\n" 1632 "// long\n" 1633 "// long", 1634 format("// long long long long\n" 1635 "// long", 1636 getLLVMStyleWithColumns(20))); 1637 1638 // Don't break or reflow after implicit string literals. 1639 verifyFormat("#include <t> // l l l\n" 1640 " // l", 1641 getLLVMStyleWithColumns(20)); 1642 1643 // Don't break or reflow comments on import lines. 1644 EXPECT_EQ("#include \"t\" /* l l l\n" 1645 " * l */", 1646 format("#include \"t\" /* l l l\n" 1647 " * l */", 1648 getLLVMStyleWithColumns(20))); 1649 1650 // Don't reflow between different trailing comment sections. 1651 EXPECT_EQ("int i; // long long\n" 1652 " // long\n" 1653 "int j; // long long\n" 1654 " // long\n", 1655 format("int i; // long long long\n" 1656 "int j; // long long long\n", 1657 getLLVMStyleWithColumns(20))); 1658 1659 // Don't reflow if the first word on the next line is longer than the 1660 // available space at current line. 1661 EXPECT_EQ("int i; // trigger\n" 1662 " // reflow\n" 1663 " // longsec\n", 1664 format("int i; // trigger reflow\n" 1665 " // longsec\n", 1666 getLLVMStyleWithColumns(20))); 1667 1668 // Keep empty comment lines. 1669 EXPECT_EQ("/**/", format(" /**/", getLLVMStyleWithColumns(20))); 1670 EXPECT_EQ("/* */", format(" /* */", getLLVMStyleWithColumns(20))); 1671 EXPECT_EQ("/* */", format(" /* */", getLLVMStyleWithColumns(20))); 1672 EXPECT_EQ("//", format(" // ", getLLVMStyleWithColumns(20))); 1673 EXPECT_EQ("///", format(" /// ", getLLVMStyleWithColumns(20))); 1674 } 1675 1676 TEST_F(FormatTestComments, IgnoresIf0Contents) { 1677 EXPECT_EQ("#if 0\n" 1678 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n" 1679 "#endif\n" 1680 "void f() {}", 1681 format("#if 0\n" 1682 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n" 1683 "#endif\n" 1684 "void f( ) { }")); 1685 EXPECT_EQ("#if false\n" 1686 "void f( ) { }\n" 1687 "#endif\n" 1688 "void g() {}\n", 1689 format("#if false\n" 1690 "void f( ) { }\n" 1691 "#endif\n" 1692 "void g( ) { }\n")); 1693 EXPECT_EQ("enum E {\n" 1694 " One,\n" 1695 " Two,\n" 1696 "#if 0\n" 1697 "Three,\n" 1698 " Four,\n" 1699 "#endif\n" 1700 " Five\n" 1701 "};", 1702 format("enum E {\n" 1703 " One,Two,\n" 1704 "#if 0\n" 1705 "Three,\n" 1706 " Four,\n" 1707 "#endif\n" 1708 " Five};")); 1709 EXPECT_EQ("enum F {\n" 1710 " One,\n" 1711 "#if 1\n" 1712 " Two,\n" 1713 "#if 0\n" 1714 "Three,\n" 1715 " Four,\n" 1716 "#endif\n" 1717 " Five\n" 1718 "#endif\n" 1719 "};", 1720 format("enum F {\n" 1721 "One,\n" 1722 "#if 1\n" 1723 "Two,\n" 1724 "#if 0\n" 1725 "Three,\n" 1726 " Four,\n" 1727 "#endif\n" 1728 "Five\n" 1729 "#endif\n" 1730 "};")); 1731 EXPECT_EQ("enum G {\n" 1732 " One,\n" 1733 "#if 0\n" 1734 "Two,\n" 1735 "#else\n" 1736 " Three,\n" 1737 "#endif\n" 1738 " Four\n" 1739 "};", 1740 format("enum G {\n" 1741 "One,\n" 1742 "#if 0\n" 1743 "Two,\n" 1744 "#else\n" 1745 "Three,\n" 1746 "#endif\n" 1747 "Four\n" 1748 "};")); 1749 EXPECT_EQ("enum H {\n" 1750 " One,\n" 1751 "#if 0\n" 1752 "#ifdef Q\n" 1753 "Two,\n" 1754 "#else\n" 1755 "Three,\n" 1756 "#endif\n" 1757 "#endif\n" 1758 " Four\n" 1759 "};", 1760 format("enum H {\n" 1761 "One,\n" 1762 "#if 0\n" 1763 "#ifdef Q\n" 1764 "Two,\n" 1765 "#else\n" 1766 "Three,\n" 1767 "#endif\n" 1768 "#endif\n" 1769 "Four\n" 1770 "};")); 1771 EXPECT_EQ("enum I {\n" 1772 " One,\n" 1773 "#if /* test */ 0 || 1\n" 1774 "Two,\n" 1775 "Three,\n" 1776 "#endif\n" 1777 " Four\n" 1778 "};", 1779 format("enum I {\n" 1780 "One,\n" 1781 "#if /* test */ 0 || 1\n" 1782 "Two,\n" 1783 "Three,\n" 1784 "#endif\n" 1785 "Four\n" 1786 "};")); 1787 EXPECT_EQ("enum J {\n" 1788 " One,\n" 1789 "#if 0\n" 1790 "#if 0\n" 1791 "Two,\n" 1792 "#else\n" 1793 "Three,\n" 1794 "#endif\n" 1795 "Four,\n" 1796 "#endif\n" 1797 " Five\n" 1798 "};", 1799 format("enum J {\n" 1800 "One,\n" 1801 "#if 0\n" 1802 "#if 0\n" 1803 "Two,\n" 1804 "#else\n" 1805 "Three,\n" 1806 "#endif\n" 1807 "Four,\n" 1808 "#endif\n" 1809 "Five\n" 1810 "};")); 1811 } 1812 1813 TEST_F(FormatTestComments, DontCrashOnBlockComments) { 1814 EXPECT_EQ( 1815 "int xxxxxxxxx; /* " 1816 "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\n" 1817 "zzzzzz\n" 1818 "0*/", 1819 format("int xxxxxxxxx; /* " 1820 "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy zzzzzz\n" 1821 "0*/")); 1822 } 1823 1824 TEST_F(FormatTestComments, BlockCommentsInControlLoops) { 1825 verifyFormat("if (0) /* a comment in a strange place */ {\n" 1826 " f();\n" 1827 "}"); 1828 verifyFormat("if (0) /* a comment in a strange place */ {\n" 1829 " f();\n" 1830 "} /* another comment */ else /* comment #3 */ {\n" 1831 " g();\n" 1832 "}"); 1833 verifyFormat("while (0) /* a comment in a strange place */ {\n" 1834 " f();\n" 1835 "}"); 1836 verifyFormat("for (;;) /* a comment in a strange place */ {\n" 1837 " f();\n" 1838 "}"); 1839 verifyFormat("do /* a comment in a strange place */ {\n" 1840 " f();\n" 1841 "} /* another comment */ while (0);"); 1842 } 1843 1844 TEST_F(FormatTestComments, BlockComments) { 1845 EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */", 1846 format("/* *//* */ /* */\n/* *//* */ /* */")); 1847 EXPECT_EQ("/* */ a /* */ b;", format(" /* */ a/* */ b;")); 1848 EXPECT_EQ("#define A /*123*/ \\\n" 1849 " b\n" 1850 "/* */\n" 1851 "someCall(\n" 1852 " parameter);", 1853 format("#define A /*123*/ b\n" 1854 "/* */\n" 1855 "someCall(parameter);", 1856 getLLVMStyleWithColumns(15))); 1857 1858 EXPECT_EQ("#define A\n" 1859 "/* */ someCall(\n" 1860 " parameter);", 1861 format("#define A\n" 1862 "/* */someCall(parameter);", 1863 getLLVMStyleWithColumns(15))); 1864 EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/")); 1865 EXPECT_EQ("/*\n" 1866 "*\n" 1867 " * aaaaaa\n" 1868 " * aaaaaa\n" 1869 "*/", 1870 format("/*\n" 1871 "*\n" 1872 " * aaaaaa aaaaaa\n" 1873 "*/", 1874 getLLVMStyleWithColumns(10))); 1875 EXPECT_EQ("/*\n" 1876 "**\n" 1877 "* aaaaaa\n" 1878 "*aaaaaa\n" 1879 "*/", 1880 format("/*\n" 1881 "**\n" 1882 "* aaaaaa aaaaaa\n" 1883 "*/", 1884 getLLVMStyleWithColumns(10))); 1885 EXPECT_EQ("int aaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 1886 " /* line 1\n" 1887 " bbbbbbbbbbbb */\n" 1888 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 1889 format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 1890 " /* line 1\n" 1891 " bbbbbbbbbbbb */ bbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 1892 getLLVMStyleWithColumns(50))); 1893 1894 FormatStyle NoBinPacking = getLLVMStyle(); 1895 NoBinPacking.BinPackParameters = false; 1896 EXPECT_EQ("someFunction(1, /* comment 1 */\n" 1897 " 2, /* comment 2 */\n" 1898 " 3, /* comment 3 */\n" 1899 " aaaa,\n" 1900 " bbbb);", 1901 format("someFunction (1, /* comment 1 */\n" 1902 " 2, /* comment 2 */ \n" 1903 " 3, /* comment 3 */\n" 1904 "aaaa, bbbb );", 1905 NoBinPacking)); 1906 verifyFormat( 1907 "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 1908 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 1909 EXPECT_EQ( 1910 "bool aaaaaaaaaaaaa = /* trailing comment */\n" 1911 " aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 1912 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;", 1913 format( 1914 "bool aaaaaaaaaaaaa = /* trailing comment */\n" 1915 " aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 1916 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;")); 1917 EXPECT_EQ( 1918 "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n" 1919 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n" 1920 "int cccccccccccccccccccccccccccccc; /* comment */\n", 1921 format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n" 1922 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n" 1923 "int cccccccccccccccccccccccccccccc; /* comment */\n")); 1924 1925 verifyFormat("void f(int * /* unused */) {}"); 1926 1927 EXPECT_EQ("/*\n" 1928 " **\n" 1929 " */", 1930 format("/*\n" 1931 " **\n" 1932 " */")); 1933 EXPECT_EQ("/*\n" 1934 " *q\n" 1935 " */", 1936 format("/*\n" 1937 " *q\n" 1938 " */")); 1939 EXPECT_EQ("/*\n" 1940 " * q\n" 1941 " */", 1942 format("/*\n" 1943 " * q\n" 1944 " */")); 1945 EXPECT_EQ("/*\n" 1946 " **/", 1947 format("/*\n" 1948 " **/")); 1949 EXPECT_EQ("/*\n" 1950 " ***/", 1951 format("/*\n" 1952 " ***/")); 1953 } 1954 1955 TEST_F(FormatTestComments, BlockCommentsInMacros) { 1956 EXPECT_EQ("#define A \\\n" 1957 " { \\\n" 1958 " /* one line */ \\\n" 1959 " someCall();", 1960 format("#define A { \\\n" 1961 " /* one line */ \\\n" 1962 " someCall();", 1963 getLLVMStyleWithColumns(20))); 1964 EXPECT_EQ("#define A \\\n" 1965 " { \\\n" 1966 " /* previous */ \\\n" 1967 " /* one line */ \\\n" 1968 " someCall();", 1969 format("#define A { \\\n" 1970 " /* previous */ \\\n" 1971 " /* one line */ \\\n" 1972 " someCall();", 1973 getLLVMStyleWithColumns(20))); 1974 } 1975 1976 TEST_F(FormatTestComments, BlockCommentsAtEndOfLine) { 1977 EXPECT_EQ("a = {\n" 1978 " 1111 /* */\n" 1979 "};", 1980 format("a = {1111 /* */\n" 1981 "};", 1982 getLLVMStyleWithColumns(15))); 1983 EXPECT_EQ("a = {\n" 1984 " 1111 /* */\n" 1985 "};", 1986 format("a = {1111 /* */\n" 1987 "};", 1988 getLLVMStyleWithColumns(15))); 1989 EXPECT_EQ("a = {\n" 1990 " 1111 /* a\n" 1991 " */\n" 1992 "};", 1993 format("a = {1111 /* a */\n" 1994 "};", 1995 getLLVMStyleWithColumns(15))); 1996 } 1997 1998 TEST_F(FormatTestComments, IndentLineCommentsInStartOfBlockAtEndOfFile) { 1999 verifyFormat("{\n" 2000 " // a\n" 2001 " // b"); 2002 } 2003 2004 TEST_F(FormatTestComments, AlignTrailingComments) { 2005 EXPECT_EQ("#define MACRO(V) \\\n" 2006 " V(Rt2) /* one more char */ \\\n" 2007 " V(Rs) /* than here */ \\\n" 2008 "/* comment 3 */\n", 2009 format("#define MACRO(V)\\\n" 2010 "V(Rt2) /* one more char */ \\\n" 2011 "V(Rs) /* than here */ \\\n" 2012 "/* comment 3 */ \\\n", 2013 getLLVMStyleWithColumns(40))); 2014 EXPECT_EQ("int i = f(abc, // line 1\n" 2015 " d, // line 2\n" 2016 " // line 3\n" 2017 " b);", 2018 format("int i = f(abc, // line 1\n" 2019 " d, // line 2\n" 2020 " // line 3\n" 2021 " b);", 2022 getLLVMStyleWithColumns(40))); 2023 2024 // Align newly broken trailing comments. 2025 EXPECT_EQ("int ab; // line\n" 2026 "int a; // long\n" 2027 " // long\n", 2028 format("int ab; // line\n" 2029 "int a; // long long\n", 2030 getLLVMStyleWithColumns(15))); 2031 EXPECT_EQ("int ab; // line\n" 2032 "int a; // long\n" 2033 " // long\n" 2034 " // long", 2035 format("int ab; // line\n" 2036 "int a; // long long\n" 2037 " // long", 2038 getLLVMStyleWithColumns(15))); 2039 EXPECT_EQ("int ab; // line\n" 2040 "int a; // long\n" 2041 " // long\n" 2042 "pt c; // long", 2043 format("int ab; // line\n" 2044 "int a; // long long\n" 2045 "pt c; // long", 2046 getLLVMStyleWithColumns(15))); 2047 EXPECT_EQ("int ab; // line\n" 2048 "int a; // long\n" 2049 " // long\n" 2050 "\n" 2051 "// long", 2052 format("int ab; // line\n" 2053 "int a; // long long\n" 2054 "\n" 2055 "// long", 2056 getLLVMStyleWithColumns(15))); 2057 2058 // Align comment line sections aligned with the next token with the next 2059 // token. 2060 EXPECT_EQ("class A {\n" 2061 "public: // public comment\n" 2062 " // comment about a\n" 2063 " int a;\n" 2064 "};", 2065 format("class A {\n" 2066 "public: // public comment\n" 2067 " // comment about a\n" 2068 " int a;\n" 2069 "};", 2070 getLLVMStyleWithColumns(40))); 2071 EXPECT_EQ("class A {\n" 2072 "public: // public comment 1\n" 2073 " // public comment 2\n" 2074 " // comment 1 about a\n" 2075 " // comment 2 about a\n" 2076 " int a;\n" 2077 "};", 2078 format("class A {\n" 2079 "public: // public comment 1\n" 2080 " // public comment 2\n" 2081 " // comment 1 about a\n" 2082 " // comment 2 about a\n" 2083 " int a;\n" 2084 "};", 2085 getLLVMStyleWithColumns(40))); 2086 EXPECT_EQ("int f(int n) { // comment line 1 on f\n" 2087 " // comment line 2 on f\n" 2088 " // comment line 1 before return\n" 2089 " // comment line 2 before return\n" 2090 " return n; // comment line 1 on return\n" 2091 " // comment line 2 on return\n" 2092 " // comment line 1 after return\n" 2093 "}", 2094 format("int f(int n) { // comment line 1 on f\n" 2095 " // comment line 2 on f\n" 2096 " // comment line 1 before return\n" 2097 " // comment line 2 before return\n" 2098 " return n; // comment line 1 on return\n" 2099 " // comment line 2 on return\n" 2100 " // comment line 1 after return\n" 2101 "}", 2102 getLLVMStyleWithColumns(40))); 2103 EXPECT_EQ("int f(int n) {\n" 2104 " switch (n) { // comment line 1 on switch\n" 2105 " // comment line 2 on switch\n" 2106 " // comment line 1 before case 1\n" 2107 " // comment line 2 before case 1\n" 2108 " case 1: // comment line 1 on case 1\n" 2109 " // comment line 2 on case 1\n" 2110 " // comment line 1 before return 1\n" 2111 " // comment line 2 before return 1\n" 2112 " return 1; // comment line 1 on return 1\n" 2113 " // comment line 2 on return 1\n" 2114 " // comment line 1 before default\n" 2115 " // comment line 2 before default\n" 2116 " default: // comment line 1 on default\n" 2117 " // comment line 2 on default\n" 2118 " // comment line 1 before return 2\n" 2119 " return 2 * f(n - 1); // comment line 1 on return 2\n" 2120 " // comment line 2 on return 2\n" 2121 " // comment line 1 after return\n" 2122 " // comment line 2 after return\n" 2123 " }\n" 2124 "}", 2125 format("int f(int n) {\n" 2126 " switch (n) { // comment line 1 on switch\n" 2127 " // comment line 2 on switch\n" 2128 " // comment line 1 before case 1\n" 2129 " // comment line 2 before case 1\n" 2130 " case 1: // comment line 1 on case 1\n" 2131 " // comment line 2 on case 1\n" 2132 " // comment line 1 before return 1\n" 2133 " // comment line 2 before return 1\n" 2134 " return 1; // comment line 1 on return 1\n" 2135 " // comment line 2 on return 1\n" 2136 " // comment line 1 before default\n" 2137 " // comment line 2 before default\n" 2138 " default: // comment line 1 on default\n" 2139 " // comment line 2 on default\n" 2140 " // comment line 1 before return 2\n" 2141 " return 2 * f(n - 1); // comment line 1 on return 2\n" 2142 " // comment line 2 on return 2\n" 2143 " // comment line 1 after return\n" 2144 " // comment line 2 after return\n" 2145 " }\n" 2146 "}", 2147 getLLVMStyleWithColumns(80))); 2148 2149 // If all the lines in a sequence of line comments are aligned with the next 2150 // token, the first line belongs to the previous token and the other lines 2151 // belong to the next token. 2152 EXPECT_EQ("int a; // line about a\n" 2153 "long b;", 2154 format("int a; // line about a\n" 2155 " long b;", 2156 getLLVMStyleWithColumns(80))); 2157 EXPECT_EQ("int a; // line about a\n" 2158 "// line about b\n" 2159 "long b;", 2160 format("int a; // line about a\n" 2161 " // line about b\n" 2162 " long b;", 2163 getLLVMStyleWithColumns(80))); 2164 EXPECT_EQ("int a; // line about a\n" 2165 "// line 1 about b\n" 2166 "// line 2 about b\n" 2167 "long b;", 2168 format("int a; // line about a\n" 2169 " // line 1 about b\n" 2170 " // line 2 about b\n" 2171 " long b;", 2172 getLLVMStyleWithColumns(80))); 2173 } 2174 } // end namespace 2175 } // end namespace format 2176 } // end namespace clang 2177