1 //===- unittest/Format/FormatTestComments.cpp - Formatting unit tests -----===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "clang/Format/Format.h" 10 11 #include "../Tooling/ReplacementTest.h" 12 #include "FormatTestUtils.h" 13 14 #include "llvm/Support/Debug.h" 15 #include "llvm/Support/MemoryBuffer.h" 16 #include "gtest/gtest.h" 17 18 #define DEBUG_TYPE "format-test" 19 20 using clang::tooling::ReplacementTest; 21 22 namespace clang { 23 namespace format { 24 namespace { 25 26 FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); } 27 28 class FormatTestComments : public ::testing::Test { 29 protected: 30 enum StatusCheck { SC_ExpectComplete, SC_ExpectIncomplete, SC_DoNotCheck }; 31 32 std::string format(llvm::StringRef Code, 33 const FormatStyle &Style = getLLVMStyle(), 34 StatusCheck CheckComplete = SC_ExpectComplete) { 35 LLVM_DEBUG(llvm::errs() << "---\n"); 36 LLVM_DEBUG(llvm::errs() << Code << "\n\n"); 37 std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size())); 38 FormattingAttemptStatus Status; 39 tooling::Replacements Replaces = 40 reformat(Style, Code, Ranges, "<stdin>", &Status); 41 if (CheckComplete != SC_DoNotCheck) { 42 bool ExpectedCompleteFormat = CheckComplete == SC_ExpectComplete; 43 EXPECT_EQ(ExpectedCompleteFormat, Status.FormatComplete) 44 << Code << "\n\n"; 45 } 46 ReplacementCount = Replaces.size(); 47 auto Result = applyAllReplacements(Code, Replaces); 48 EXPECT_TRUE(static_cast<bool>(Result)); 49 LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n"); 50 return *Result; 51 } 52 53 FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) { 54 FormatStyle Style = getLLVMStyle(); 55 Style.ColumnLimit = ColumnLimit; 56 return Style; 57 } 58 59 FormatStyle getTextProtoStyleWithColumns(unsigned ColumnLimit) { 60 FormatStyle Style = getGoogleStyle(FormatStyle::FormatStyle::LK_TextProto); 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(Code, Style)) << "Expected code is not stable"; 68 EXPECT_EQ(Code.str(), format(test::messUp(Code), Style)); 69 } 70 71 void verifyGoogleFormat(llvm::StringRef Code) { 72 verifyFormat(Code, getGoogleStyle()); 73 } 74 75 /// \brief Verify that clang-format does not crash on the given input. 76 void verifyNoCrash(llvm::StringRef Code, 77 const FormatStyle &Style = getLLVMStyle()) { 78 format(Code, Style, SC_DoNotCheck); 79 } 80 81 int ReplacementCount; 82 }; 83 84 //===----------------------------------------------------------------------===// 85 // Tests for comments. 86 //===----------------------------------------------------------------------===// 87 88 TEST_F(FormatTestComments, UnderstandsSingleLineComments) { 89 verifyFormat("//* */"); 90 verifyFormat("// line 1\n" 91 "// line 2\n" 92 "void f() {}\n"); 93 94 verifyFormat("void f() {\n" 95 " // Doesn't do anything\n" 96 "}"); 97 verifyFormat("SomeObject\n" 98 " // Calling someFunction on SomeObject\n" 99 " .someFunction();"); 100 verifyFormat("auto result = SomeObject\n" 101 " // Calling someFunction on SomeObject\n" 102 " .someFunction();"); 103 verifyFormat("void f(int i, // some comment (probably for i)\n" 104 " int j, // some comment (probably for j)\n" 105 " int k); // some comment (probably for k)"); 106 verifyFormat("void f(int i,\n" 107 " // some comment (probably for j)\n" 108 " int j,\n" 109 " // some comment (probably for k)\n" 110 " int k);"); 111 112 verifyFormat("int i // This is a fancy variable\n" 113 " = 5; // with nicely aligned comment."); 114 115 verifyFormat("// Leading comment.\n" 116 "int a; // Trailing comment."); 117 verifyFormat("int a; // Trailing comment\n" 118 " // on 2\n" 119 " // or 3 lines.\n" 120 "int b;"); 121 verifyFormat("int a; // Trailing comment\n" 122 "\n" 123 "// Leading comment.\n" 124 "int b;"); 125 verifyFormat("int a; // Comment.\n" 126 " // More details.\n" 127 "int bbbb; // Another comment."); 128 verifyFormat( 129 "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n" 130 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // comment\n" 131 "int cccccccccccccccccccccccccccccc; // comment\n" 132 "int ddd; // looooooooooooooooooooooooong comment\n" 133 "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n" 134 "int bbbbbbbbbbbbbbbbbbbbb; // comment\n" 135 "int ccccccccccccccccccc; // comment"); 136 137 verifyFormat("#include \"a\" // comment\n" 138 "#include \"a/b/c\" // comment"); 139 verifyFormat("#include <a> // comment\n" 140 "#include <a/b/c> // comment"); 141 EXPECT_EQ("#include \"a\" // comment\n" 142 "#include \"a/b/c\" // comment", 143 format("#include \\\n" 144 " \"a\" // comment\n" 145 "#include \"a/b/c\" // comment")); 146 147 verifyFormat("enum E {\n" 148 " // comment\n" 149 " VAL_A, // comment\n" 150 " VAL_B\n" 151 "};"); 152 153 EXPECT_EQ("enum A {\n" 154 " // line a\n" 155 " a,\n" 156 " b, // line b\n" 157 "\n" 158 " // line c\n" 159 " c\n" 160 "};", 161 format("enum A {\n" 162 " // line a\n" 163 " a,\n" 164 " b, // line b\n" 165 "\n" 166 " // line c\n" 167 " c\n" 168 "};", 169 getLLVMStyleWithColumns(20))); 170 EXPECT_EQ("enum A {\n" 171 " a, // line 1\n" 172 " // line 2\n" 173 "};", 174 format("enum A {\n" 175 " a, // line 1\n" 176 " // line 2\n" 177 "};", 178 getLLVMStyleWithColumns(20))); 179 EXPECT_EQ("enum A {\n" 180 " a, // line 1\n" 181 " // line 2\n" 182 "};", 183 format("enum A {\n" 184 " a, // line 1\n" 185 " // line 2\n" 186 "};", 187 getLLVMStyleWithColumns(20))); 188 EXPECT_EQ("enum A {\n" 189 " a, // line 1\n" 190 " // line 2\n" 191 " b\n" 192 "};", 193 format("enum A {\n" 194 " a, // line 1\n" 195 " // line 2\n" 196 " b\n" 197 "};", 198 getLLVMStyleWithColumns(20))); 199 EXPECT_EQ("enum A {\n" 200 " a, // line 1\n" 201 " // line 2\n" 202 " b\n" 203 "};", 204 format("enum A {\n" 205 " a, // line 1\n" 206 " // line 2\n" 207 " b\n" 208 "};", 209 getLLVMStyleWithColumns(20))); 210 verifyFormat( 211 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 212 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment"); 213 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 214 " // Comment inside a statement.\n" 215 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 216 verifyFormat("SomeFunction(a,\n" 217 " // comment\n" 218 " b + x);"); 219 verifyFormat("SomeFunction(a, a,\n" 220 " // comment\n" 221 " b + x);"); 222 verifyFormat( 223 "bool aaaaaaaaaaaaa = // comment\n" 224 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 225 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 226 227 verifyFormat("int aaaa; // aaaaa\n" 228 "int aa; // aaaaaaa", 229 getLLVMStyleWithColumns(20)); 230 231 EXPECT_EQ("void f() { // This does something ..\n" 232 "}\n" 233 "int a; // This is unrelated", 234 format("void f() { // This does something ..\n" 235 " }\n" 236 "int a; // This is unrelated")); 237 EXPECT_EQ("class C {\n" 238 " void f() { // This does something ..\n" 239 " } // awesome..\n" 240 "\n" 241 " int a; // This is unrelated\n" 242 "};", 243 format("class C{void f() { // This does something ..\n" 244 " } // awesome..\n" 245 " \n" 246 "int a; // This is unrelated\n" 247 "};")); 248 249 EXPECT_EQ("int i; // single line trailing comment", 250 format("int i;\\\n// single line trailing comment")); 251 252 verifyGoogleFormat("int a; // Trailing comment."); 253 254 verifyFormat("someFunction(anotherFunction( // Force break.\n" 255 " parameter));"); 256 257 verifyGoogleFormat("#endif // HEADER_GUARD"); 258 259 verifyFormat("const char *test[] = {\n" 260 " // A\n" 261 " \"aaaa\",\n" 262 " // B\n" 263 " \"aaaaa\"};"); 264 verifyGoogleFormat( 265 "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 266 " aaaaaaaaaaaaaaaaaaaaaa); // 81_cols_with_this_comment"); 267 EXPECT_EQ("D(a, {\n" 268 " // test\n" 269 " int a;\n" 270 "});", 271 format("D(a, {\n" 272 "// test\n" 273 "int a;\n" 274 "});")); 275 276 EXPECT_EQ("lineWith(); // comment\n" 277 "// at start\n" 278 "otherLine();", 279 format("lineWith(); // comment\n" 280 "// at start\n" 281 "otherLine();")); 282 EXPECT_EQ("lineWith(); // comment\n" 283 "/*\n" 284 " * at start */\n" 285 "otherLine();", 286 format("lineWith(); // comment\n" 287 "/*\n" 288 " * at start */\n" 289 "otherLine();")); 290 EXPECT_EQ("lineWith(); // comment\n" 291 " // at start\n" 292 "otherLine();", 293 format("lineWith(); // comment\n" 294 " // at start\n" 295 "otherLine();")); 296 297 EXPECT_EQ("lineWith(); // comment\n" 298 "// at start\n" 299 "otherLine(); // comment", 300 format("lineWith(); // comment\n" 301 "// at start\n" 302 "otherLine(); // comment")); 303 EXPECT_EQ("lineWith();\n" 304 "// at start\n" 305 "otherLine(); // comment", 306 format("lineWith();\n" 307 " // at start\n" 308 "otherLine(); // comment")); 309 EXPECT_EQ("// first\n" 310 "// at start\n" 311 "otherLine(); // comment", 312 format("// first\n" 313 " // at start\n" 314 "otherLine(); // comment")); 315 EXPECT_EQ("f();\n" 316 "// first\n" 317 "// at start\n" 318 "otherLine(); // comment", 319 format("f();\n" 320 "// first\n" 321 " // at start\n" 322 "otherLine(); // comment")); 323 verifyFormat("f(); // comment\n" 324 "// first\n" 325 "// at start\n" 326 "otherLine();"); 327 EXPECT_EQ("f(); // comment\n" 328 "// first\n" 329 "// at start\n" 330 "otherLine();", 331 format("f(); // comment\n" 332 "// first\n" 333 " // at start\n" 334 "otherLine();")); 335 EXPECT_EQ("f(); // comment\n" 336 " // first\n" 337 "// at start\n" 338 "otherLine();", 339 format("f(); // comment\n" 340 " // first\n" 341 "// at start\n" 342 "otherLine();")); 343 EXPECT_EQ("void f() {\n" 344 " lineWith(); // comment\n" 345 " // at start\n" 346 "}", 347 format("void f() {\n" 348 " lineWith(); // comment\n" 349 " // at start\n" 350 "}")); 351 EXPECT_EQ("int xy; // a\n" 352 "int z; // b", 353 format("int xy; // a\n" 354 "int z; //b")); 355 EXPECT_EQ("int xy; // a\n" 356 "int z; // bb", 357 format("int xy; // a\n" 358 "int z; //bb", 359 getLLVMStyleWithColumns(12))); 360 361 verifyFormat("#define A \\\n" 362 " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n" 363 " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */", 364 getLLVMStyleWithColumns(60)); 365 verifyFormat( 366 "#define A \\\n" 367 " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n" 368 " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */", 369 getLLVMStyleWithColumns(61)); 370 371 verifyFormat("if ( // This is some comment\n" 372 " x + 3) {\n" 373 "}"); 374 EXPECT_EQ("if ( // This is some comment\n" 375 " // spanning two lines\n" 376 " x + 3) {\n" 377 "}", 378 format("if( // This is some comment\n" 379 " // spanning two lines\n" 380 " x + 3) {\n" 381 "}")); 382 383 verifyNoCrash("/\\\n/"); 384 verifyNoCrash("/\\\n* */"); 385 // The 0-character somehow makes the lexer return a proper comment. 386 verifyNoCrash(StringRef("/*\\\0\n/", 6)); 387 } 388 389 TEST_F(FormatTestComments, KeepsParameterWithTrailingCommentsOnTheirOwnLine) { 390 EXPECT_EQ("SomeFunction(a,\n" 391 " b, // comment\n" 392 " c);", 393 format("SomeFunction(a,\n" 394 " b, // comment\n" 395 " c);")); 396 EXPECT_EQ("SomeFunction(a, b,\n" 397 " // comment\n" 398 " c);", 399 format("SomeFunction(a,\n" 400 " b,\n" 401 " // comment\n" 402 " c);")); 403 EXPECT_EQ("SomeFunction(a, b, // comment (unclear relation)\n" 404 " c);", 405 format("SomeFunction(a, b, // comment (unclear relation)\n" 406 " c);")); 407 EXPECT_EQ("SomeFunction(a, // comment\n" 408 " b,\n" 409 " c); // comment", 410 format("SomeFunction(a, // comment\n" 411 " b,\n" 412 " c); // comment")); 413 EXPECT_EQ("aaaaaaaaaa(aaaa(aaaa,\n" 414 " aaaa), //\n" 415 " aaaa, bbbbb);", 416 format("aaaaaaaaaa(aaaa(aaaa,\n" 417 "aaaa), //\n" 418 "aaaa, bbbbb);")); 419 } 420 421 TEST_F(FormatTestComments, RemovesTrailingWhitespaceOfComments) { 422 EXPECT_EQ("// comment", format("// comment ")); 423 EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment", 424 format("int aaaaaaa, bbbbbbb; // comment ", 425 getLLVMStyleWithColumns(33))); 426 EXPECT_EQ("// comment\\\n", format("// comment\\\n \t \v \f ")); 427 EXPECT_EQ("// comment \\\n", format("// comment \\\n \t \v \f ")); 428 } 429 430 TEST_F(FormatTestComments, UnderstandsBlockComments) { 431 verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);"); 432 verifyFormat("void f() { g(/*aaa=*/x, /*bbb=*/!y, /*c=*/::c); }"); 433 EXPECT_EQ("f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n" 434 " bbbbbbbbbbbbbbbbbbbbbbbbb);", 435 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \\\n" 436 "/* Trailing comment for aa... */\n" 437 " bbbbbbbbbbbbbbbbbbbbbbbbb);")); 438 EXPECT_EQ( 439 "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 440 " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);", 441 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n" 442 "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);")); 443 EXPECT_EQ( 444 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 445 " aaaaaaaaaaaaaaaaaa,\n" 446 " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n" 447 "}", 448 format("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 449 " aaaaaaaaaaaaaaaaaa ,\n" 450 " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n" 451 "}")); 452 verifyFormat("f(/* aaaaaaaaaaaaaaaaaa = */\n" 453 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 454 455 FormatStyle NoBinPacking = getLLVMStyle(); 456 NoBinPacking.BinPackParameters = false; 457 verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n" 458 " /* parameter 2 */ aaaaaa,\n" 459 " /* parameter 3 */ aaaaaa,\n" 460 " /* parameter 4 */ aaaaaa);", 461 NoBinPacking); 462 463 // Aligning block comments in macros. 464 verifyGoogleFormat("#define A \\\n" 465 " int i; /*a*/ \\\n" 466 " int jjj; /*b*/"); 467 } 468 469 TEST_F(FormatTestComments, AlignsBlockComments) { 470 EXPECT_EQ("/*\n" 471 " * Really multi-line\n" 472 " * comment.\n" 473 " */\n" 474 "void f() {}", 475 format(" /*\n" 476 " * Really multi-line\n" 477 " * comment.\n" 478 " */\n" 479 " void f() {}")); 480 EXPECT_EQ("class C {\n" 481 " /*\n" 482 " * Another multi-line\n" 483 " * comment.\n" 484 " */\n" 485 " void f() {}\n" 486 "};", 487 format("class C {\n" 488 "/*\n" 489 " * Another multi-line\n" 490 " * comment.\n" 491 " */\n" 492 "void f() {}\n" 493 "};")); 494 EXPECT_EQ("/*\n" 495 " 1. This is a comment with non-trivial formatting.\n" 496 " 1.1. We have to indent/outdent all lines equally\n" 497 " 1.1.1. to keep the formatting.\n" 498 " */", 499 format(" /*\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 EXPECT_EQ("/*\n" 505 "Don't try to outdent if there's not enough indentation.\n" 506 "*/", 507 format(" /*\n" 508 " Don't try to outdent if there's not enough indentation.\n" 509 " */")); 510 511 EXPECT_EQ("int i; /* Comment with empty...\n" 512 " *\n" 513 " * line. */", 514 format("int i; /* Comment with empty...\n" 515 " *\n" 516 " * line. */")); 517 EXPECT_EQ("int foobar = 0; /* comment */\n" 518 "int bar = 0; /* multiline\n" 519 " comment 1 */\n" 520 "int baz = 0; /* multiline\n" 521 " comment 2 */\n" 522 "int bzz = 0; /* multiline\n" 523 " comment 3 */", 524 format("int foobar = 0; /* comment */\n" 525 "int bar = 0; /* multiline\n" 526 " comment 1 */\n" 527 "int baz = 0; /* multiline\n" 528 " comment 2 */\n" 529 "int bzz = 0; /* multiline\n" 530 " comment 3 */")); 531 EXPECT_EQ("int foobar = 0; /* comment */\n" 532 "int bar = 0; /* multiline\n" 533 " comment */\n" 534 "int baz = 0; /* multiline\n" 535 "comment */", 536 format("int foobar = 0; /* comment */\n" 537 "int bar = 0; /* multiline\n" 538 "comment */\n" 539 "int baz = 0; /* multiline\n" 540 "comment */")); 541 } 542 543 TEST_F(FormatTestComments, CommentReflowingCanBeTurnedOff) { 544 FormatStyle Style = getLLVMStyleWithColumns(20); 545 Style.ReflowComments = false; 546 verifyFormat("// aaaaaaaaa aaaaaaaaaa aaaaaaaaaa", Style); 547 verifyFormat("/* aaaaaaaaa aaaaaaaaaa aaaaaaaaaa */", Style); 548 } 549 550 TEST_F(FormatTestComments, CorrectlyHandlesLengthOfBlockComments) { 551 EXPECT_EQ("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 552 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */", 553 format("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 554 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */")); 555 EXPECT_EQ( 556 "void ffffffffffff(\n" 557 " int aaaaaaaa, int bbbbbbbb,\n" 558 " int cccccccccccc) { /*\n" 559 " aaaaaaaaaa\n" 560 " aaaaaaaaaaaaa\n" 561 " bbbbbbbbbbbbbb\n" 562 " bbbbbbbbbb\n" 563 " */\n" 564 "}", 565 format("void ffffffffffff(int aaaaaaaa, int bbbbbbbb, int cccccccccccc)\n" 566 "{ /*\n" 567 " aaaaaaaaaa aaaaaaaaaaaaa\n" 568 " bbbbbbbbbbbbbb bbbbbbbbbb\n" 569 " */\n" 570 "}", 571 getLLVMStyleWithColumns(40))); 572 } 573 574 TEST_F(FormatTestComments, DontBreakNonTrailingBlockComments) { 575 EXPECT_EQ("void ffffffffff(\n" 576 " int aaaaa /* test */);", 577 format("void ffffffffff(int aaaaa /* test */);", 578 getLLVMStyleWithColumns(35))); 579 } 580 581 TEST_F(FormatTestComments, SplitsLongCxxComments) { 582 EXPECT_EQ("// A comment that\n" 583 "// doesn't fit on\n" 584 "// one line", 585 format("// A comment that doesn't fit on one line", 586 getLLVMStyleWithColumns(20))); 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 b c d\n" 598 "// e f g\n" 599 "// h i j k", 600 format("// a b c d e f g h i j k", getLLVMStyleWithColumns(10))); 601 EXPECT_EQ( 602 "// a b c d\n" 603 "// e f g\n" 604 "// h i j k", 605 format("\\\n// a b c d e f g h i j k", getLLVMStyleWithColumns(10))); 606 EXPECT_EQ("if (true) // A comment that\n" 607 " // doesn't fit on\n" 608 " // one line", 609 format("if (true) // A comment that doesn't fit on one line ", 610 getLLVMStyleWithColumns(30))); 611 EXPECT_EQ("// Don't_touch_leading_whitespace", 612 format("// Don't_touch_leading_whitespace", 613 getLLVMStyleWithColumns(20))); 614 EXPECT_EQ("// Add leading\n" 615 "// whitespace", 616 format("//Add leading whitespace", getLLVMStyleWithColumns(20))); 617 EXPECT_EQ("/// Add leading\n" 618 "/// whitespace", 619 format("///Add leading whitespace", getLLVMStyleWithColumns(20))); 620 EXPECT_EQ("//! Add leading\n" 621 "//! whitespace", 622 format("//!Add leading whitespace", getLLVMStyleWithColumns(20))); 623 EXPECT_EQ("// whitespace", format("//whitespace", getLLVMStyle())); 624 EXPECT_EQ("// Even if it makes the line exceed the column\n" 625 "// limit", 626 format("//Even if it makes the line exceed the column limit", 627 getLLVMStyleWithColumns(51))); 628 EXPECT_EQ("//--But not here", format("//--But not here", getLLVMStyle())); 629 EXPECT_EQ("/// line 1\n" 630 "// add leading whitespace", 631 format("/// line 1\n" 632 "//add leading whitespace", 633 getLLVMStyleWithColumns(30))); 634 EXPECT_EQ("/// line 1\n" 635 "/// line 2\n" 636 "//! line 3\n" 637 "//! line 4\n" 638 "//! line 5\n" 639 "// line 6\n" 640 "// line 7", 641 format("///line 1\n" 642 "///line 2\n" 643 "//! line 3\n" 644 "//!line 4\n" 645 "//!line 5\n" 646 "// line 6\n" 647 "//line 7", 648 getLLVMStyleWithColumns(20))); 649 650 EXPECT_EQ("// aa bb cc dd", 651 format("// aa bb cc dd ", 652 getLLVMStyleWithColumns(15))); 653 654 EXPECT_EQ("// A comment before\n" 655 "// a macro\n" 656 "// definition\n" 657 "#define a b", 658 format("// A comment before a macro definition\n" 659 "#define a b", 660 getLLVMStyleWithColumns(20))); 661 EXPECT_EQ("void ffffff(\n" 662 " int aaaaaaaaa, // wwww\n" 663 " int bbbbbbbbbb, // xxxxxxx\n" 664 " // yyyyyyyyyy\n" 665 " int c, int d, int e) {}", 666 format("void ffffff(\n" 667 " int aaaaaaaaa, // wwww\n" 668 " int bbbbbbbbbb, // xxxxxxx yyyyyyyyyy\n" 669 " int c, int d, int e) {}", 670 getLLVMStyleWithColumns(40))); 671 EXPECT_EQ("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 672 format("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 673 getLLVMStyleWithColumns(20))); 674 EXPECT_EQ( 675 "#define XXX // a b c d\n" 676 " // e f g h", 677 format("#define XXX // a b c d e f g h", getLLVMStyleWithColumns(22))); 678 EXPECT_EQ( 679 "#define XXX // q w e r\n" 680 " // t y u i", 681 format("#define XXX //q w e r t y u i", getLLVMStyleWithColumns(22))); 682 EXPECT_EQ("{\n" 683 " //\n" 684 " //\\\n" 685 " // long 1 2 3 4 5\n" 686 "}", 687 format("{\n" 688 " //\n" 689 " //\\\n" 690 " // long 1 2 3 4 5\n" 691 "}", 692 getLLVMStyleWithColumns(20))); 693 EXPECT_EQ("{\n" 694 " //\n" 695 " //\\\n" 696 " // long 1 2 3 4 5\n" 697 " // 6\n" 698 "}", 699 format("{\n" 700 " //\n" 701 " //\\\n" 702 " // long 1 2 3 4 5 6\n" 703 "}", 704 getLLVMStyleWithColumns(20))); 705 } 706 707 TEST_F(FormatTestComments, PreservesHangingIndentInCxxComments) { 708 EXPECT_EQ("// A comment\n" 709 "// that doesn't\n" 710 "// fit on one\n" 711 "// line", 712 format("// A comment that doesn't fit on one line", 713 getLLVMStyleWithColumns(20))); 714 EXPECT_EQ("/// A comment\n" 715 "/// that doesn't\n" 716 "/// fit on one\n" 717 "/// line", 718 format("/// A comment that doesn't fit on one line", 719 getLLVMStyleWithColumns(20))); 720 } 721 722 TEST_F(FormatTestComments, DontSplitLineCommentsWithEscapedNewlines) { 723 EXPECT_EQ("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 724 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 725 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 726 format("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 727 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 728 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")); 729 EXPECT_EQ("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 730 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 731 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 732 format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 733 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 734 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 735 getLLVMStyleWithColumns(50))); 736 // FIXME: One day we might want to implement adjustment of leading whitespace 737 // of the consecutive lines in this kind of comment: 738 EXPECT_EQ("double\n" 739 " a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 740 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 741 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 742 format("double a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 743 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 744 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 745 getLLVMStyleWithColumns(49))); 746 } 747 748 TEST_F(FormatTestComments, DontIntroduceMultilineComments) { 749 // Avoid introducing a multiline comment by breaking after `\`. 750 for (int ColumnLimit = 15; ColumnLimit <= 17; ++ColumnLimit) { 751 EXPECT_EQ( 752 "// aaaaaaaaaa\n" 753 "// \\ bb", 754 format("// aaaaaaaaaa \\ bb", getLLVMStyleWithColumns(ColumnLimit))); 755 EXPECT_EQ( 756 "// aaaaaaaaa\n" 757 "// \\ bb", 758 format("// aaaaaaaaa \\ bb", getLLVMStyleWithColumns(ColumnLimit))); 759 EXPECT_EQ( 760 "// aaaaaaaaa\n" 761 "// \\ \\ bb", 762 format("// aaaaaaaaa \\ \\ bb", getLLVMStyleWithColumns(ColumnLimit))); 763 } 764 } 765 766 TEST_F(FormatTestComments, DontSplitLineCommentsWithPragmas) { 767 FormatStyle Pragmas = getLLVMStyleWithColumns(30); 768 Pragmas.CommentPragmas = "^ IWYU pragma:"; 769 EXPECT_EQ( 770 "// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", 771 format("// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", Pragmas)); 772 EXPECT_EQ( 773 "/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", 774 format("/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", Pragmas)); 775 } 776 777 TEST_F(FormatTestComments, PriorityOfCommentBreaking) { 778 EXPECT_EQ("if (xxx ==\n" 779 " yyy && // aaaaaaaaaaaa bbbbbbbbb\n" 780 " zzz)\n" 781 " q();", 782 format("if (xxx == yyy && // aaaaaaaaaaaa bbbbbbbbb\n" 783 " zzz) q();", 784 getLLVMStyleWithColumns(40))); 785 EXPECT_EQ("if (xxxxxxxxxx ==\n" 786 " yyy && // aaaaaa bbbbbbbb cccc\n" 787 " zzz)\n" 788 " q();", 789 format("if (xxxxxxxxxx == yyy && // aaaaaa bbbbbbbb cccc\n" 790 " zzz) q();", 791 getLLVMStyleWithColumns(40))); 792 EXPECT_EQ("if (xxxxxxxxxx &&\n" 793 " yyy || // aaaaaa bbbbbbbb cccc\n" 794 " zzz)\n" 795 " q();", 796 format("if (xxxxxxxxxx && yyy || // aaaaaa bbbbbbbb cccc\n" 797 " zzz) q();", 798 getLLVMStyleWithColumns(40))); 799 EXPECT_EQ("fffffffff(\n" 800 " &xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n" 801 " zzz);", 802 format("fffffffff(&xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n" 803 " zzz);", 804 getLLVMStyleWithColumns(40))); 805 } 806 807 TEST_F(FormatTestComments, MultiLineCommentsInDefines) { 808 EXPECT_EQ("#define A(x) /* \\\n" 809 " a comment \\\n" 810 " inside */ \\\n" 811 " f();", 812 format("#define A(x) /* \\\n" 813 " a comment \\\n" 814 " inside */ \\\n" 815 " f();", 816 getLLVMStyleWithColumns(17))); 817 EXPECT_EQ("#define A( \\\n" 818 " x) /* \\\n" 819 " a comment \\\n" 820 " inside */ \\\n" 821 " f();", 822 format("#define A( \\\n" 823 " x) /* \\\n" 824 " a comment \\\n" 825 " inside */ \\\n" 826 " f();", 827 getLLVMStyleWithColumns(17))); 828 } 829 830 TEST_F(FormatTestComments, ParsesCommentsAdjacentToPPDirectives) { 831 EXPECT_EQ("namespace {}\n// Test\n#define A", 832 format("namespace {}\n // Test\n#define A")); 833 EXPECT_EQ("namespace {}\n/* Test */\n#define A", 834 format("namespace {}\n /* Test */\n#define A")); 835 EXPECT_EQ("namespace {}\n/* Test */ #define A", 836 format("namespace {}\n /* Test */ #define A")); 837 } 838 839 TEST_F(FormatTestComments, KeepsLevelOfCommentBeforePPDirective) { 840 // Keep the current level if the comment was originally not aligned with 841 // the preprocessor directive. 842 EXPECT_EQ("void f() {\n" 843 " int i;\n" 844 " /* comment */\n" 845 "#ifdef A\n" 846 " int j;\n" 847 "}", 848 format("void f() {\n" 849 " int i;\n" 850 " /* comment */\n" 851 "#ifdef A\n" 852 " int j;\n" 853 "}")); 854 855 EXPECT_EQ("void f() {\n" 856 " int i;\n" 857 " /* comment */\n" 858 "\n" 859 "#ifdef A\n" 860 " int j;\n" 861 "}", 862 format("void f() {\n" 863 " int i;\n" 864 " /* comment */\n" 865 "\n" 866 "#ifdef A\n" 867 " int j;\n" 868 "}")); 869 870 EXPECT_EQ("int f(int i) {\n" 871 " if (true) {\n" 872 " ++i;\n" 873 " }\n" 874 " // comment\n" 875 "#ifdef A\n" 876 " int j;\n" 877 "#endif\n" 878 "}", 879 format("int f(int i) {\n" 880 " if (true) {\n" 881 " ++i;\n" 882 " }\n" 883 " // comment\n" 884 "#ifdef A\n" 885 "int j;\n" 886 "#endif\n" 887 "}")); 888 889 EXPECT_EQ("int f(int i) {\n" 890 " if (true) {\n" 891 " i++;\n" 892 " } else {\n" 893 " // comment in else\n" 894 "#ifdef A\n" 895 " j++;\n" 896 "#endif\n" 897 " }\n" 898 "}", 899 format("int f(int i) {\n" 900 " if (true) {\n" 901 " i++;\n" 902 " } else {\n" 903 " // comment in else\n" 904 "#ifdef A\n" 905 " j++;\n" 906 "#endif\n" 907 " }\n" 908 "}")); 909 910 EXPECT_EQ("int f(int i) {\n" 911 " if (true) {\n" 912 " i++;\n" 913 " } else {\n" 914 " /* comment in else */\n" 915 "#ifdef A\n" 916 " j++;\n" 917 "#endif\n" 918 " }\n" 919 "}", 920 format("int f(int i) {\n" 921 " if (true) {\n" 922 " i++;\n" 923 " } else {\n" 924 " /* comment in else */\n" 925 "#ifdef A\n" 926 " j++;\n" 927 "#endif\n" 928 " }\n" 929 "}")); 930 931 // Keep the current level if there is an empty line between the comment and 932 // the preprocessor directive. 933 EXPECT_EQ("void f() {\n" 934 " int i;\n" 935 " /* comment */\n" 936 "\n" 937 "#ifdef A\n" 938 " int j;\n" 939 "}", 940 format("void f() {\n" 941 " int i;\n" 942 "/* comment */\n" 943 "\n" 944 "#ifdef A\n" 945 " int j;\n" 946 "}")); 947 948 EXPECT_EQ("void f() {\n" 949 " int i;\n" 950 " return i;\n" 951 "}\n" 952 "// comment\n" 953 "\n" 954 "#ifdef A\n" 955 "int i;\n" 956 "#endif // A", 957 format("void f() {\n" 958 " int i;\n" 959 " return i;\n" 960 "}\n" 961 "// comment\n" 962 "\n" 963 "#ifdef A\n" 964 "int i;\n" 965 "#endif // A")); 966 967 EXPECT_EQ("int f(int i) {\n" 968 " if (true) {\n" 969 " ++i;\n" 970 " }\n" 971 " // comment\n" 972 "\n" 973 "#ifdef A\n" 974 " int j;\n" 975 "#endif\n" 976 "}", 977 format("int f(int i) {\n" 978 " if (true) {\n" 979 " ++i;\n" 980 " }\n" 981 " // comment\n" 982 "\n" 983 "#ifdef A\n" 984 " int j;\n" 985 "#endif\n" 986 "}")); 987 988 EXPECT_EQ("int f(int i) {\n" 989 " if (true) {\n" 990 " i++;\n" 991 " } else {\n" 992 " // comment in else\n" 993 "\n" 994 "#ifdef A\n" 995 " j++;\n" 996 "#endif\n" 997 " }\n" 998 "}", 999 format("int f(int i) {\n" 1000 " if (true) {\n" 1001 " i++;\n" 1002 " } else {\n" 1003 "// comment in else\n" 1004 "\n" 1005 "#ifdef A\n" 1006 " j++;\n" 1007 "#endif\n" 1008 " }\n" 1009 "}")); 1010 1011 EXPECT_EQ("int f(int i) {\n" 1012 " if (true) {\n" 1013 " i++;\n" 1014 " } else {\n" 1015 " /* comment in else */\n" 1016 "\n" 1017 "#ifdef A\n" 1018 " j++;\n" 1019 "#endif\n" 1020 " }\n" 1021 "}", 1022 format("int f(int i) {\n" 1023 " if (true) {\n" 1024 " i++;\n" 1025 " } else {\n" 1026 "/* comment in else */\n" 1027 "\n" 1028 "#ifdef A\n" 1029 " j++;\n" 1030 "#endif\n" 1031 " }\n" 1032 "}")); 1033 1034 // Align with the preprocessor directive if the comment was originally aligned 1035 // with the preprocessor directive and there is no newline between the comment 1036 // and the preprocessor directive. 1037 EXPECT_EQ("void f() {\n" 1038 " int i;\n" 1039 "/* comment */\n" 1040 "#ifdef A\n" 1041 " int j;\n" 1042 "}", 1043 format("void f() {\n" 1044 " int i;\n" 1045 "/* comment */\n" 1046 "#ifdef A\n" 1047 " int j;\n" 1048 "}")); 1049 1050 EXPECT_EQ("int f(int i) {\n" 1051 " if (true) {\n" 1052 " ++i;\n" 1053 " }\n" 1054 "// comment\n" 1055 "#ifdef A\n" 1056 " int j;\n" 1057 "#endif\n" 1058 "}", 1059 format("int f(int i) {\n" 1060 " if (true) {\n" 1061 " ++i;\n" 1062 " }\n" 1063 "// comment\n" 1064 "#ifdef A\n" 1065 " int j;\n" 1066 "#endif\n" 1067 "}")); 1068 1069 EXPECT_EQ("int f(int i) {\n" 1070 " if (true) {\n" 1071 " i++;\n" 1072 " } else {\n" 1073 "// comment in else\n" 1074 "#ifdef A\n" 1075 " j++;\n" 1076 "#endif\n" 1077 " }\n" 1078 "}", 1079 format("int f(int i) {\n" 1080 " if (true) {\n" 1081 " i++;\n" 1082 " } else {\n" 1083 " // comment in else\n" 1084 " #ifdef A\n" 1085 " j++;\n" 1086 "#endif\n" 1087 " }\n" 1088 "}")); 1089 1090 EXPECT_EQ("int f(int i) {\n" 1091 " if (true) {\n" 1092 " i++;\n" 1093 " } else {\n" 1094 "/* comment in else */\n" 1095 "#ifdef A\n" 1096 " j++;\n" 1097 "#endif\n" 1098 " }\n" 1099 "}", 1100 format("int f(int i) {\n" 1101 " if (true) {\n" 1102 " i++;\n" 1103 " } else {\n" 1104 " /* comment in else */\n" 1105 " #ifdef A\n" 1106 " j++;\n" 1107 "#endif\n" 1108 " }\n" 1109 "}")); 1110 } 1111 1112 TEST_F(FormatTestComments, SplitsLongLinesInComments) { 1113 // FIXME: Do we need to fix up the " */" at the end? 1114 // It doesn't look like any of our current logic triggers this. 1115 EXPECT_EQ("/* This is a long\n" 1116 " * comment that\n" 1117 " * doesn't fit on\n" 1118 " * one line. */", 1119 format("/* " 1120 "This is a long " 1121 "comment that " 1122 "doesn't " 1123 "fit on one line. */", 1124 getLLVMStyleWithColumns(20))); 1125 EXPECT_EQ( 1126 "/* a b c d\n" 1127 " * e f g\n" 1128 " * h i j k\n" 1129 " */", 1130 format("/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10))); 1131 EXPECT_EQ( 1132 "/* a b c d\n" 1133 " * e f g\n" 1134 " * h i j k\n" 1135 " */", 1136 format("\\\n/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10))); 1137 EXPECT_EQ("/*\n" 1138 "This is a long\n" 1139 "comment that doesn't\n" 1140 "fit on one line.\n" 1141 "*/", 1142 format("/*\n" 1143 "This is a long " 1144 "comment that doesn't " 1145 "fit on one line. \n" 1146 "*/", 1147 getLLVMStyleWithColumns(20))); 1148 EXPECT_EQ("/*\n" 1149 " * This is a long\n" 1150 " * comment that\n" 1151 " * doesn't fit on\n" 1152 " * one line.\n" 1153 " */", 1154 format("/* \n" 1155 " * This is a long " 1156 " comment that " 1157 " doesn't fit on " 1158 " one line. \n" 1159 " */", 1160 getLLVMStyleWithColumns(20))); 1161 EXPECT_EQ("/*\n" 1162 " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n" 1163 " * so_it_should_be_broken\n" 1164 " * wherever_a_space_occurs\n" 1165 " */", 1166 format("/*\n" 1167 " * This_is_a_comment_with_words_that_dont_fit_on_one_line " 1168 " so_it_should_be_broken " 1169 " wherever_a_space_occurs \n" 1170 " */", 1171 getLLVMStyleWithColumns(20))); 1172 EXPECT_EQ("/*\n" 1173 " * This_comment_can_not_be_broken_into_lines\n" 1174 " */", 1175 format("/*\n" 1176 " * This_comment_can_not_be_broken_into_lines\n" 1177 " */", 1178 getLLVMStyleWithColumns(20))); 1179 EXPECT_EQ("{\n" 1180 " /*\n" 1181 " This is another\n" 1182 " long comment that\n" 1183 " doesn't fit on one\n" 1184 " line 1234567890\n" 1185 " */\n" 1186 "}", 1187 format("{\n" 1188 "/*\n" 1189 "This is another " 1190 " long comment that " 1191 " doesn't fit on one" 1192 " line 1234567890\n" 1193 "*/\n" 1194 "}", 1195 getLLVMStyleWithColumns(20))); 1196 EXPECT_EQ("{\n" 1197 " /*\n" 1198 " * This i s\n" 1199 " * another comment\n" 1200 " * t hat doesn' t\n" 1201 " * fit on one l i\n" 1202 " * n e\n" 1203 " */\n" 1204 "}", 1205 format("{\n" 1206 "/*\n" 1207 " * This i s" 1208 " another comment" 1209 " t hat doesn' t" 1210 " fit on one l i" 1211 " n e\n" 1212 " */\n" 1213 "}", 1214 getLLVMStyleWithColumns(20))); 1215 EXPECT_EQ("/*\n" 1216 " * This is a long\n" 1217 " * comment that\n" 1218 " * doesn't fit on\n" 1219 " * one line\n" 1220 " */", 1221 format(" /*\n" 1222 " * This is a long comment that doesn't fit on one line\n" 1223 " */", 1224 getLLVMStyleWithColumns(20))); 1225 EXPECT_EQ("{\n" 1226 " if (something) /* This is a\n" 1227 " long\n" 1228 " comment */\n" 1229 " ;\n" 1230 "}", 1231 format("{\n" 1232 " if (something) /* This is a long comment */\n" 1233 " ;\n" 1234 "}", 1235 getLLVMStyleWithColumns(30))); 1236 1237 EXPECT_EQ("/* A comment before\n" 1238 " * a macro\n" 1239 " * definition */\n" 1240 "#define a b", 1241 format("/* A comment before a macro definition */\n" 1242 "#define a b", 1243 getLLVMStyleWithColumns(20))); 1244 1245 EXPECT_EQ("/* some comment\n" 1246 " * a comment that\n" 1247 " * we break another\n" 1248 " * comment we have\n" 1249 " * to break a left\n" 1250 " * comment\n" 1251 " */", 1252 format(" /* some comment\n" 1253 " * a comment that we break\n" 1254 " * another comment we have to break\n" 1255 "* a left comment\n" 1256 " */", 1257 getLLVMStyleWithColumns(20))); 1258 1259 EXPECT_EQ("/**\n" 1260 " * multiline block\n" 1261 " * comment\n" 1262 " *\n" 1263 " */", 1264 format("/**\n" 1265 " * multiline block comment\n" 1266 " *\n" 1267 " */", 1268 getLLVMStyleWithColumns(20))); 1269 1270 // This reproduces a crashing bug where both adaptStartOfLine and 1271 // getCommentSplit were trying to wrap after the "/**". 1272 EXPECT_EQ("/** multilineblockcommentwithnowrapopportunity */", 1273 format("/** multilineblockcommentwithnowrapopportunity */", 1274 getLLVMStyleWithColumns(20))); 1275 1276 EXPECT_EQ("/*\n" 1277 "\n" 1278 "\n" 1279 " */\n", 1280 format(" /* \n" 1281 " \n" 1282 " \n" 1283 " */\n")); 1284 1285 EXPECT_EQ("/* a a */", 1286 format("/* a a */", getLLVMStyleWithColumns(15))); 1287 EXPECT_EQ("/* a a bc */", 1288 format("/* a a bc */", getLLVMStyleWithColumns(15))); 1289 EXPECT_EQ("/* aaa aaa\n" 1290 " * aaaaa */", 1291 format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15))); 1292 EXPECT_EQ("/* aaa aaa\n" 1293 " * aaaaa */", 1294 format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15))); 1295 } 1296 1297 TEST_F(FormatTestComments, SplitsLongLinesInCommentsInPreprocessor) { 1298 EXPECT_EQ("#define X \\\n" 1299 " /* \\\n" 1300 " Test \\\n" 1301 " Macro comment \\\n" 1302 " with a long \\\n" 1303 " line \\\n" 1304 " */ \\\n" 1305 " A + B", 1306 format("#define X \\\n" 1307 " /*\n" 1308 " Test\n" 1309 " Macro comment with a long line\n" 1310 " */ \\\n" 1311 " A + B", 1312 getLLVMStyleWithColumns(20))); 1313 EXPECT_EQ("#define X \\\n" 1314 " /* Macro comment \\\n" 1315 " with a long \\\n" 1316 " line */ \\\n" 1317 " A + B", 1318 format("#define X \\\n" 1319 " /* Macro comment with a long\n" 1320 " line */ \\\n" 1321 " A + B", 1322 getLLVMStyleWithColumns(20))); 1323 EXPECT_EQ("#define X \\\n" 1324 " /* Macro comment \\\n" 1325 " * with a long \\\n" 1326 " * line */ \\\n" 1327 " A + B", 1328 format("#define X \\\n" 1329 " /* Macro comment with a long line */ \\\n" 1330 " A + B", 1331 getLLVMStyleWithColumns(20))); 1332 } 1333 1334 TEST_F(FormatTestComments, KeepsTrailingPPCommentsAndSectionCommentsSeparate) { 1335 verifyFormat("#ifdef A // line about A\n" 1336 "// section comment\n" 1337 "#endif", 1338 getLLVMStyleWithColumns(80)); 1339 verifyFormat("#ifdef A // line 1 about A\n" 1340 " // line 2 about A\n" 1341 "// section comment\n" 1342 "#endif", 1343 getLLVMStyleWithColumns(80)); 1344 EXPECT_EQ("#ifdef A // line 1 about A\n" 1345 " // line 2 about A\n" 1346 "// section comment\n" 1347 "#endif", 1348 format("#ifdef A // line 1 about A\n" 1349 " // line 2 about A\n" 1350 "// section comment\n" 1351 "#endif", 1352 getLLVMStyleWithColumns(80))); 1353 verifyFormat("int f() {\n" 1354 " int i;\n" 1355 "#ifdef A // comment about A\n" 1356 " // section comment 1\n" 1357 " // section comment 2\n" 1358 " i = 2;\n" 1359 "#else // comment about #else\n" 1360 " // section comment 3\n" 1361 " i = 4;\n" 1362 "#endif\n" 1363 "}", 1364 getLLVMStyleWithColumns(80)); 1365 } 1366 1367 TEST_F(FormatTestComments, AlignsPPElseEndifComments) { 1368 verifyFormat("#if A\n" 1369 "#else // A\n" 1370 "int iiii;\n" 1371 "#endif // B", 1372 getLLVMStyleWithColumns(20)); 1373 verifyFormat("#if A\n" 1374 "#else // A\n" 1375 "int iiii; // CC\n" 1376 "#endif // B", 1377 getLLVMStyleWithColumns(20)); 1378 EXPECT_EQ("#if A\n" 1379 "#else // A1\n" 1380 " // A2\n" 1381 "int ii;\n" 1382 "#endif // B", 1383 format("#if A\n" 1384 "#else // A1\n" 1385 " // A2\n" 1386 "int ii;\n" 1387 "#endif // B", 1388 getLLVMStyleWithColumns(20))); 1389 } 1390 1391 TEST_F(FormatTestComments, CommentsInStaticInitializers) { 1392 EXPECT_EQ( 1393 "static SomeType type = {aaaaaaaaaaaaaaaaaaaa, /* comment */\n" 1394 " aaaaaaaaaaaaaaaaaaaa /* comment */,\n" 1395 " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n" 1396 " aaaaaaaaaaaaaaaaaaaa, // comment\n" 1397 " aaaaaaaaaaaaaaaaaaaa};", 1398 format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n" 1399 " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n" 1400 " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n" 1401 " aaaaaaaaaaaaaaaaaaaa , // comment\n" 1402 " aaaaaaaaaaaaaaaaaaaa };")); 1403 verifyFormat("static SomeType type = {aaaaaaaaaaa, // comment for aa...\n" 1404 " bbbbbbbbbbb, ccccccccccc};"); 1405 verifyFormat("static SomeType type = {aaaaaaaaaaa,\n" 1406 " // comment for bb....\n" 1407 " bbbbbbbbbbb, ccccccccccc};"); 1408 verifyGoogleFormat( 1409 "static SomeType type = {aaaaaaaaaaa, // comment for aa...\n" 1410 " bbbbbbbbbbb, ccccccccccc};"); 1411 verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n" 1412 " // comment for bb....\n" 1413 " bbbbbbbbbbb, ccccccccccc};"); 1414 1415 verifyFormat("S s = {{a, b, c}, // Group #1\n" 1416 " {d, e, f}, // Group #2\n" 1417 " {g, h, i}}; // Group #3"); 1418 verifyFormat("S s = {{// Group #1\n" 1419 " a, b, c},\n" 1420 " {// Group #2\n" 1421 " d, e, f},\n" 1422 " {// Group #3\n" 1423 " g, h, i}};"); 1424 1425 EXPECT_EQ("S s = {\n" 1426 " // Some comment\n" 1427 " a,\n" 1428 "\n" 1429 " // Comment after empty line\n" 1430 " b}", 1431 format("S s = {\n" 1432 " // Some comment\n" 1433 " a,\n" 1434 " \n" 1435 " // Comment after empty line\n" 1436 " b\n" 1437 "}")); 1438 EXPECT_EQ("S s = {\n" 1439 " /* Some comment */\n" 1440 " a,\n" 1441 "\n" 1442 " /* Comment after empty line */\n" 1443 " b}", 1444 format("S s = {\n" 1445 " /* Some comment */\n" 1446 " a,\n" 1447 " \n" 1448 " /* Comment after empty line */\n" 1449 " b\n" 1450 "}")); 1451 verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n" 1452 " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n" 1453 " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n" 1454 " 0x00, 0x00, 0x00, 0x00}; // comment\n"); 1455 } 1456 1457 TEST_F(FormatTestComments, LineCommentsAfterRightBrace) { 1458 EXPECT_EQ("if (true) { // comment about branch\n" 1459 " // comment about f\n" 1460 " f();\n" 1461 "}", 1462 format("if (true) { // comment about branch\n" 1463 " // comment about f\n" 1464 " f();\n" 1465 "}", 1466 getLLVMStyleWithColumns(80))); 1467 EXPECT_EQ("if (1) { // if line 1\n" 1468 " // if line 2\n" 1469 " // if line 3\n" 1470 " // f line 1\n" 1471 " // f line 2\n" 1472 " f();\n" 1473 "} else { // else line 1\n" 1474 " // else line 2\n" 1475 " // else line 3\n" 1476 " // g line 1\n" 1477 " g();\n" 1478 "}", 1479 format("if (1) { // if line 1\n" 1480 " // if line 2\n" 1481 " // if line 3\n" 1482 " // f line 1\n" 1483 " // f line 2\n" 1484 " f();\n" 1485 "} else { // else line 1\n" 1486 " // else line 2\n" 1487 " // else line 3\n" 1488 " // g line 1\n" 1489 " g();\n" 1490 "}")); 1491 EXPECT_EQ("do { // line 1\n" 1492 " // line 2\n" 1493 " // line 3\n" 1494 " f();\n" 1495 "} while (true);", 1496 format("do { // line 1\n" 1497 " // line 2\n" 1498 " // line 3\n" 1499 " f();\n" 1500 "} while (true);", 1501 getLLVMStyleWithColumns(80))); 1502 EXPECT_EQ("while (a < b) { // line 1\n" 1503 " // line 2\n" 1504 " // line 3\n" 1505 " f();\n" 1506 "}", 1507 format("while (a < b) {// line 1\n" 1508 " // line 2\n" 1509 " // line 3\n" 1510 " f();\n" 1511 "}", 1512 getLLVMStyleWithColumns(80))); 1513 } 1514 1515 TEST_F(FormatTestComments, ReflowsComments) { 1516 // Break a long line and reflow with the full next line. 1517 EXPECT_EQ("// long long long\n" 1518 "// long long", 1519 format("// long long long long\n" 1520 "// long", 1521 getLLVMStyleWithColumns(20))); 1522 1523 // Keep the trailing newline while reflowing. 1524 EXPECT_EQ("// long long long\n" 1525 "// long long\n", 1526 format("// long long long long\n" 1527 "// long\n", 1528 getLLVMStyleWithColumns(20))); 1529 1530 // Break a long line and reflow with a part of the next line. 1531 EXPECT_EQ("// long long long\n" 1532 "// long long\n" 1533 "// long_long", 1534 format("// long long long long\n" 1535 "// long long_long", 1536 getLLVMStyleWithColumns(20))); 1537 1538 // Break but do not reflow if the first word from the next line is too long. 1539 EXPECT_EQ("// long long long\n" 1540 "// long\n" 1541 "// long_long_long\n", 1542 format("// long long long long\n" 1543 "// long_long_long\n", 1544 getLLVMStyleWithColumns(20))); 1545 1546 // Don't break or reflow short lines. 1547 verifyFormat("// long\n" 1548 "// long long long lo\n" 1549 "// long long long lo\n" 1550 "// long", 1551 getLLVMStyleWithColumns(20)); 1552 1553 // Keep prefixes and decorations while reflowing. 1554 EXPECT_EQ("/// long long long\n" 1555 "/// long long\n", 1556 format("/// long long long long\n" 1557 "/// long\n", 1558 getLLVMStyleWithColumns(20))); 1559 EXPECT_EQ("//! long long long\n" 1560 "//! long long\n", 1561 format("//! long long long long\n" 1562 "//! long\n", 1563 getLLVMStyleWithColumns(20))); 1564 EXPECT_EQ("/* long long long\n" 1565 " * long long */", 1566 format("/* long long long long\n" 1567 " * long */", 1568 getLLVMStyleWithColumns(20))); 1569 EXPECT_EQ("///< long long long\n" 1570 "///< long long\n", 1571 format("///< long long long long\n" 1572 "///< long\n", 1573 getLLVMStyleWithColumns(20))); 1574 EXPECT_EQ("//!< long long long\n" 1575 "//!< long long\n", 1576 format("//!< long long long long\n" 1577 "//!< long\n", 1578 getLLVMStyleWithColumns(20))); 1579 1580 // Don't bring leading whitespace up while reflowing. 1581 EXPECT_EQ("/* long long long\n" 1582 " * long long long\n" 1583 " */", 1584 format("/* long long long long\n" 1585 " * long long\n" 1586 " */", 1587 getLLVMStyleWithColumns(20))); 1588 1589 // Reflow the last line of a block comment with its trailing '*/'. 1590 EXPECT_EQ("/* long long long\n" 1591 " long long */", 1592 format("/* long long long long\n" 1593 " long */", 1594 getLLVMStyleWithColumns(20))); 1595 1596 // Reflow two short lines; keep the postfix of the last one. 1597 EXPECT_EQ("/* long long long\n" 1598 " * long long long */", 1599 format("/* long long long long\n" 1600 " * long\n" 1601 " * long */", 1602 getLLVMStyleWithColumns(20))); 1603 1604 // Put the postfix of the last short reflow line on a newline if it doesn't 1605 // fit. 1606 EXPECT_EQ("/* long long long\n" 1607 " * long long longg\n" 1608 " */", 1609 format("/* long long long long\n" 1610 " * long\n" 1611 " * longg */", 1612 getLLVMStyleWithColumns(20))); 1613 1614 // Reflow lines with leading whitespace. 1615 EXPECT_EQ("{\n" 1616 " /*\n" 1617 " * long long long\n" 1618 " * long long long\n" 1619 " * long long long\n" 1620 " */\n" 1621 "}", 1622 format("{\n" 1623 "/*\n" 1624 " * long long long long\n" 1625 " * long\n" 1626 " * long long long long\n" 1627 " */\n" 1628 "}", 1629 getLLVMStyleWithColumns(20))); 1630 1631 // Break single line block comments that are first in the line with ' *' 1632 // decoration. 1633 EXPECT_EQ("/* long long long\n" 1634 " * long */", 1635 format("/* long long long long */", getLLVMStyleWithColumns(20))); 1636 1637 // Break single line block comment that are not first in the line with ' ' 1638 // decoration. 1639 EXPECT_EQ("int i; /* long long\n" 1640 " long */", 1641 format("int i; /* long long long */", getLLVMStyleWithColumns(20))); 1642 1643 // Reflow a line that goes just over the column limit. 1644 EXPECT_EQ("// long long long\n" 1645 "// lon long", 1646 format("// long long long lon\n" 1647 "// long", 1648 getLLVMStyleWithColumns(20))); 1649 1650 // Stop reflowing if the next line has a different indentation than the 1651 // previous line. 1652 EXPECT_EQ("// long long long\n" 1653 "// long\n" 1654 "// long long\n" 1655 "// long", 1656 format("// long long long long\n" 1657 "// long long\n" 1658 "// long", 1659 getLLVMStyleWithColumns(20))); 1660 1661 // Reflow into the last part of a really long line that has been broken into 1662 // multiple lines. 1663 EXPECT_EQ("// long long long\n" 1664 "// long long long\n" 1665 "// long long long\n", 1666 format("// long long long long long long long long\n" 1667 "// long\n", 1668 getLLVMStyleWithColumns(20))); 1669 1670 // Break the first line, then reflow the beginning of the second and third 1671 // line up. 1672 EXPECT_EQ("// long long long\n" 1673 "// lon1 lon2 lon2\n" 1674 "// lon2 lon3 lon3", 1675 format("// long long long lon1\n" 1676 "// lon2 lon2 lon2\n" 1677 "// lon3 lon3", 1678 getLLVMStyleWithColumns(20))); 1679 1680 // Reflow the beginning of the second line, then break the rest. 1681 EXPECT_EQ("// long long long\n" 1682 "// lon1 lon2 lon2\n" 1683 "// lon2 lon2 lon2\n" 1684 "// lon3", 1685 format("// long long long lon1\n" 1686 "// lon2 lon2 lon2 lon2 lon2 lon3", 1687 getLLVMStyleWithColumns(20))); 1688 1689 // Shrink the first line, then reflow the second line up. 1690 EXPECT_EQ("// long long long", format("// long long\n" 1691 "// long", 1692 getLLVMStyleWithColumns(20))); 1693 1694 // Don't shrink leading whitespace. 1695 EXPECT_EQ("int i; /// a", 1696 format("int i; /// a", getLLVMStyleWithColumns(20))); 1697 1698 // Shrink trailing whitespace if there is no postfix and reflow. 1699 EXPECT_EQ("// long long long\n" 1700 "// long long", 1701 format("// long long long long \n" 1702 "// long", 1703 getLLVMStyleWithColumns(20))); 1704 1705 // Shrink trailing whitespace to a single one if there is postfix. 1706 EXPECT_EQ("/* long long long */", 1707 format("/* long long long */", getLLVMStyleWithColumns(20))); 1708 1709 // Break a block comment postfix if exceeding the line limit. 1710 EXPECT_EQ("/* long\n" 1711 " */", 1712 format("/* long */", getLLVMStyleWithColumns(20))); 1713 1714 // Reflow indented comments. 1715 EXPECT_EQ("{\n" 1716 " // long long long\n" 1717 " // long long\n" 1718 " int i; /* long lon\n" 1719 " g long\n" 1720 " */\n" 1721 "}", 1722 format("{\n" 1723 " // long long long long\n" 1724 " // long\n" 1725 " int i; /* long lon g\n" 1726 " long */\n" 1727 "}", 1728 getLLVMStyleWithColumns(20))); 1729 1730 // Don't realign trailing comments after reflow has happened. 1731 EXPECT_EQ("// long long long\n" 1732 "// long long\n" 1733 "long i; // long", 1734 format("// long long long long\n" 1735 "// long\n" 1736 "long i; // long", 1737 getLLVMStyleWithColumns(20))); 1738 EXPECT_EQ("// long long long\n" 1739 "// longng long long\n" 1740 "// long lo", 1741 format("// long long long longng\n" 1742 "// long long long\n" 1743 "// lo", 1744 getLLVMStyleWithColumns(20))); 1745 1746 // Reflow lines after a broken line. 1747 EXPECT_EQ("int a; // Trailing\n" 1748 " // comment on\n" 1749 " // 2 or 3\n" 1750 " // lines.\n", 1751 format("int a; // Trailing comment\n" 1752 " // on 2\n" 1753 " // or 3\n" 1754 " // lines.\n", 1755 getLLVMStyleWithColumns(20))); 1756 EXPECT_EQ("/// This long line\n" 1757 "/// gets reflown.\n", 1758 format("/// This long line gets\n" 1759 "/// reflown.\n", 1760 getLLVMStyleWithColumns(20))); 1761 EXPECT_EQ("//! This long line\n" 1762 "//! gets reflown.\n", 1763 format(" //! This long line gets\n" 1764 " //! reflown.\n", 1765 getLLVMStyleWithColumns(20))); 1766 EXPECT_EQ("/* This long line\n" 1767 " * gets reflown.\n" 1768 " */\n", 1769 format("/* This long line gets\n" 1770 " * reflown.\n" 1771 " */\n", 1772 getLLVMStyleWithColumns(20))); 1773 1774 // Reflow after indentation makes a line too long. 1775 EXPECT_EQ("{\n" 1776 " // long long long\n" 1777 " // lo long\n" 1778 "}\n", 1779 format("{\n" 1780 "// long long long lo\n" 1781 "// long\n" 1782 "}\n", 1783 getLLVMStyleWithColumns(20))); 1784 1785 // Break and reflow multiple lines. 1786 EXPECT_EQ("/*\n" 1787 " * Reflow the end of\n" 1788 " * line by 11 22 33\n" 1789 " * 4.\n" 1790 " */\n", 1791 format("/*\n" 1792 " * Reflow the end of line\n" 1793 " * by\n" 1794 " * 11\n" 1795 " * 22\n" 1796 " * 33\n" 1797 " * 4.\n" 1798 " */\n", 1799 getLLVMStyleWithColumns(20))); 1800 EXPECT_EQ("/// First line gets\n" 1801 "/// broken. Second\n" 1802 "/// line gets\n" 1803 "/// reflown and\n" 1804 "/// broken. Third\n" 1805 "/// gets reflown.\n", 1806 format("/// First line gets broken.\n" 1807 "/// Second line gets reflown and broken.\n" 1808 "/// Third gets reflown.\n", 1809 getLLVMStyleWithColumns(20))); 1810 EXPECT_EQ("int i; // first long\n" 1811 " // long snd\n" 1812 " // long.\n", 1813 format("int i; // first long long\n" 1814 " // snd long.\n", 1815 getLLVMStyleWithColumns(20))); 1816 EXPECT_EQ("{\n" 1817 " // first long line\n" 1818 " // line second\n" 1819 " // long line line\n" 1820 " // third long line\n" 1821 " // line\n" 1822 "}\n", 1823 format("{\n" 1824 " // first long line line\n" 1825 " // second long line line\n" 1826 " // third long line line\n" 1827 "}\n", 1828 getLLVMStyleWithColumns(20))); 1829 EXPECT_EQ("int i; /* first line\n" 1830 " * second\n" 1831 " * line third\n" 1832 " * line\n" 1833 " */", 1834 format("int i; /* first line\n" 1835 " * second line\n" 1836 " * third line\n" 1837 " */", 1838 getLLVMStyleWithColumns(20))); 1839 1840 // Reflow the last two lines of a section that starts with a line having 1841 // different indentation. 1842 EXPECT_EQ("// long\n" 1843 "// long long long\n" 1844 "// long long", 1845 format("// long\n" 1846 "// long long long long\n" 1847 "// long", 1848 getLLVMStyleWithColumns(20))); 1849 1850 // Keep the block comment endling '*/' while reflowing. 1851 EXPECT_EQ("/* Long long long\n" 1852 " * line short */\n", 1853 format("/* Long long long line\n" 1854 " * short */\n", 1855 getLLVMStyleWithColumns(20))); 1856 1857 // Don't reflow between separate blocks of comments. 1858 EXPECT_EQ("/* First comment\n" 1859 " * block will */\n" 1860 "/* Snd\n" 1861 " */\n", 1862 format("/* First comment block\n" 1863 " * will */\n" 1864 "/* Snd\n" 1865 " */\n", 1866 getLLVMStyleWithColumns(20))); 1867 1868 // Don't reflow across blank comment lines. 1869 EXPECT_EQ("int i; // This long\n" 1870 " // line gets\n" 1871 " // broken.\n" 1872 " //\n" 1873 " // keep.\n", 1874 format("int i; // This long line gets broken.\n" 1875 " // \n" 1876 " // keep.\n", 1877 getLLVMStyleWithColumns(20))); 1878 EXPECT_EQ("{\n" 1879 " /// long long long\n" 1880 " /// long long\n" 1881 " ///\n" 1882 " /// long\n" 1883 "}", 1884 format("{\n" 1885 " /// long long long long\n" 1886 " /// long\n" 1887 " ///\n" 1888 " /// long\n" 1889 "}", 1890 getLLVMStyleWithColumns(20))); 1891 EXPECT_EQ("//! long long long\n" 1892 "//! long\n" 1893 "\n" 1894 "//! long", 1895 format("//! long long long long\n" 1896 "\n" 1897 "//! long", 1898 getLLVMStyleWithColumns(20))); 1899 EXPECT_EQ("/* long long long\n" 1900 " long\n" 1901 "\n" 1902 " long */", 1903 format("/* long long long long\n" 1904 "\n" 1905 " long */", 1906 getLLVMStyleWithColumns(20))); 1907 EXPECT_EQ("/* long long long\n" 1908 " * long\n" 1909 " *\n" 1910 " * long */", 1911 format("/* long long long long\n" 1912 " *\n" 1913 " * long */", 1914 getLLVMStyleWithColumns(20))); 1915 1916 // Don't reflow lines having content that is a single character. 1917 EXPECT_EQ("// long long long\n" 1918 "// long\n" 1919 "// l", 1920 format("// long long long long\n" 1921 "// l", 1922 getLLVMStyleWithColumns(20))); 1923 1924 // Don't reflow lines starting with two punctuation characters. 1925 EXPECT_EQ("// long long long\n" 1926 "// long\n" 1927 "// ... --- ...", 1928 format("// long long long long\n" 1929 "// ... --- ...", 1930 getLLVMStyleWithColumns(20))); 1931 1932 // Don't reflow lines starting with '@'. 1933 EXPECT_EQ("// long long long\n" 1934 "// long\n" 1935 "// @param arg", 1936 format("// long long long long\n" 1937 "// @param arg", 1938 getLLVMStyleWithColumns(20))); 1939 1940 // Don't reflow lines starting with 'TODO'. 1941 EXPECT_EQ("// long long long\n" 1942 "// long\n" 1943 "// TODO: long", 1944 format("// long long long long\n" 1945 "// TODO: long", 1946 getLLVMStyleWithColumns(20))); 1947 1948 // Don't reflow lines starting with 'FIXME'. 1949 EXPECT_EQ("// long long long\n" 1950 "// long\n" 1951 "// FIXME: long", 1952 format("// long long long long\n" 1953 "// FIXME: long", 1954 getLLVMStyleWithColumns(20))); 1955 1956 // Don't reflow lines starting with 'XXX'. 1957 EXPECT_EQ("// long long long\n" 1958 "// long\n" 1959 "// XXX: long", 1960 format("// long long long long\n" 1961 "// XXX: long", 1962 getLLVMStyleWithColumns(20))); 1963 1964 // Don't reflow comment pragmas. 1965 EXPECT_EQ("// long long long\n" 1966 "// long\n" 1967 "// IWYU pragma:", 1968 format("// long long long long\n" 1969 "// IWYU pragma:", 1970 getLLVMStyleWithColumns(20))); 1971 EXPECT_EQ("/* long long long\n" 1972 " * long\n" 1973 " * IWYU pragma:\n" 1974 " */", 1975 format("/* long long long long\n" 1976 " * IWYU pragma:\n" 1977 " */", 1978 getLLVMStyleWithColumns(20))); 1979 1980 // Reflow lines that have a non-punctuation character among their first 2 1981 // characters. 1982 EXPECT_EQ("// long long long\n" 1983 "// long 'long'", 1984 format("// long long long long\n" 1985 "// 'long'", 1986 getLLVMStyleWithColumns(20))); 1987 1988 // Don't reflow between separate blocks of comments. 1989 EXPECT_EQ("/* First comment\n" 1990 " * block will */\n" 1991 "/* Snd\n" 1992 " */\n", 1993 format("/* First comment block\n" 1994 " * will */\n" 1995 "/* Snd\n" 1996 " */\n", 1997 getLLVMStyleWithColumns(20))); 1998 1999 // Don't reflow lines having different indentation. 2000 EXPECT_EQ("// long long long\n" 2001 "// long\n" 2002 "// long", 2003 format("// long long long long\n" 2004 "// long", 2005 getLLVMStyleWithColumns(20))); 2006 2007 // Don't reflow separate bullets in list 2008 EXPECT_EQ("// - long long long\n" 2009 "// long\n" 2010 "// - long", 2011 format("// - long long long long\n" 2012 "// - long", 2013 getLLVMStyleWithColumns(20))); 2014 EXPECT_EQ("// * long long long\n" 2015 "// long\n" 2016 "// * long", 2017 format("// * long long long long\n" 2018 "// * long", 2019 getLLVMStyleWithColumns(20))); 2020 EXPECT_EQ("// + long long long\n" 2021 "// long\n" 2022 "// + long", 2023 format("// + long long long long\n" 2024 "// + long", 2025 getLLVMStyleWithColumns(20))); 2026 EXPECT_EQ("// 1. long long long\n" 2027 "// long\n" 2028 "// 2. long", 2029 format("// 1. long long long long\n" 2030 "// 2. long", 2031 getLLVMStyleWithColumns(20))); 2032 EXPECT_EQ("// -# long long long\n" 2033 "// long\n" 2034 "// -# long", 2035 format("// -# long long long long\n" 2036 "// -# long", 2037 getLLVMStyleWithColumns(20))); 2038 2039 EXPECT_EQ("// - long long long\n" 2040 "// long long long\n" 2041 "// - long", 2042 format("// - long long long long\n" 2043 "// long long\n" 2044 "// - long", 2045 getLLVMStyleWithColumns(20))); 2046 EXPECT_EQ("// - long long long\n" 2047 "// long long long\n" 2048 "// long\n" 2049 "// - long", 2050 format("// - long long long long\n" 2051 "// long long long\n" 2052 "// - long", 2053 getLLVMStyleWithColumns(20))); 2054 2055 // Large number (>2 digits) are not list items 2056 EXPECT_EQ("// long long long\n" 2057 "// long 1024. long.", 2058 format("// long long long long\n" 2059 "// 1024. long.", 2060 getLLVMStyleWithColumns(20))); 2061 2062 // Do not break before number, to avoid introducing a non-reflowable doxygen 2063 // list item. 2064 EXPECT_EQ("// long long\n" 2065 "// long 10. long.", 2066 format("// long long long 10.\n" 2067 "// long.", 2068 getLLVMStyleWithColumns(20))); 2069 2070 // Don't break or reflow after implicit string literals. 2071 verifyFormat("#include <t> // l l l\n" 2072 " // l", 2073 getLLVMStyleWithColumns(20)); 2074 2075 // Don't break or reflow comments on import lines. 2076 EXPECT_EQ("#include \"t\" /* l l l\n" 2077 " * l */", 2078 format("#include \"t\" /* l l l\n" 2079 " * l */", 2080 getLLVMStyleWithColumns(20))); 2081 2082 // Don't reflow between different trailing comment sections. 2083 EXPECT_EQ("int i; // long long\n" 2084 " // long\n" 2085 "int j; // long long\n" 2086 " // long\n", 2087 format("int i; // long long long\n" 2088 "int j; // long long long\n", 2089 getLLVMStyleWithColumns(20))); 2090 2091 // Don't reflow if the first word on the next line is longer than the 2092 // available space at current line. 2093 EXPECT_EQ("int i; // trigger\n" 2094 " // reflow\n" 2095 " // longsec\n", 2096 format("int i; // trigger reflow\n" 2097 " // longsec\n", 2098 getLLVMStyleWithColumns(20))); 2099 2100 // Simple case that correctly handles reflow in parameter lists. 2101 EXPECT_EQ("a = f(/* looooooooong\n" 2102 " * long long\n" 2103 " */\n" 2104 " a);", 2105 format("a = f(/* looooooooong long\n* long\n*/ a);", 2106 getLLVMStyleWithColumns(22))); 2107 // Tricky case that has fewer lines if we reflow the comment, ending up with 2108 // fewer lines. 2109 EXPECT_EQ("a = f(/* loooooong\n" 2110 " * long long\n" 2111 " */\n" 2112 " a);", 2113 format("a = f(/* loooooong long\n* long\n*/ a);", 2114 getLLVMStyleWithColumns(22))); 2115 2116 // Keep empty comment lines. 2117 EXPECT_EQ("/**/", format(" /**/", getLLVMStyleWithColumns(20))); 2118 EXPECT_EQ("/* */", format(" /* */", getLLVMStyleWithColumns(20))); 2119 EXPECT_EQ("/* */", format(" /* */", getLLVMStyleWithColumns(20))); 2120 EXPECT_EQ("//", format(" // ", getLLVMStyleWithColumns(20))); 2121 EXPECT_EQ("///", format(" /// ", getLLVMStyleWithColumns(20))); 2122 } 2123 2124 TEST_F(FormatTestComments, ReflowsCommentsPrecise) { 2125 // FIXME: This assumes we do not continue compressing whitespace once we are 2126 // in reflow mode. Consider compressing whitespace. 2127 2128 // Test that we stop reflowing precisely at the column limit. 2129 // After reflowing, "// reflows into foo" does not fit the column limit, 2130 // so we compress the whitespace. 2131 EXPECT_EQ("// some text that\n" 2132 "// reflows into foo\n", 2133 format("// some text that reflows\n" 2134 "// into foo\n", 2135 getLLVMStyleWithColumns(20))); 2136 // Given one more column, "// reflows into foo" does fit the limit, so we 2137 // do not compress the whitespace. 2138 EXPECT_EQ("// some text that\n" 2139 "// reflows into foo\n", 2140 format("// some text that reflows\n" 2141 "// into foo\n", 2142 getLLVMStyleWithColumns(21))); 2143 2144 // Make sure that we correctly account for the space added in the reflow case 2145 // when making the reflowing decision. 2146 // First, when the next line ends precisely one column over the limit, do not 2147 // reflow. 2148 EXPECT_EQ("// some text that\n" 2149 "// reflows\n" 2150 "// into1234567\n", 2151 format("// some text that reflows\n" 2152 "// into1234567\n", 2153 getLLVMStyleWithColumns(21))); 2154 // Secondly, when the next line ends later, but the first word in that line 2155 // is precisely one column over the limit, do not reflow. 2156 EXPECT_EQ("// some text that\n" 2157 "// reflows\n" 2158 "// into1234567 f\n", 2159 format("// some text that reflows\n" 2160 "// into1234567 f\n", 2161 getLLVMStyleWithColumns(21))); 2162 } 2163 2164 TEST_F(FormatTestComments, ReflowsCommentsWithExtraWhitespace) { 2165 // Baseline. 2166 EXPECT_EQ("// some text\n" 2167 "// that re flows\n", 2168 format("// some text that\n" 2169 "// re flows\n", 2170 getLLVMStyleWithColumns(16))); 2171 EXPECT_EQ("// some text\n" 2172 "// that re flows\n", 2173 format("// some text that\n" 2174 "// re flows\n", 2175 getLLVMStyleWithColumns(16))); 2176 EXPECT_EQ("/* some text\n" 2177 " * that re flows\n" 2178 " */\n", 2179 format("/* some text that\n" 2180 "* re flows\n" 2181 "*/\n", 2182 getLLVMStyleWithColumns(16))); 2183 // FIXME: We do not reflow if the indent of two subsequent lines differs; 2184 // given that this is different behavior from block comments, do we want 2185 // to keep this? 2186 EXPECT_EQ("// some text\n" 2187 "// that\n" 2188 "// re flows\n", 2189 format("// some text that\n" 2190 "// re flows\n", 2191 getLLVMStyleWithColumns(16))); 2192 // Space within parts of a line that fit. 2193 // FIXME: Use the earliest possible split while reflowing to compress the 2194 // whitespace within the line. 2195 EXPECT_EQ("// some text that\n" 2196 "// does re flow\n" 2197 "// more here\n", 2198 format("// some text that does\n" 2199 "// re flow more here\n", 2200 getLLVMStyleWithColumns(21))); 2201 } 2202 2203 TEST_F(FormatTestComments, IgnoresIf0Contents) { 2204 EXPECT_EQ("#if 0\n" 2205 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n" 2206 "#endif\n" 2207 "void f() {}", 2208 format("#if 0\n" 2209 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n" 2210 "#endif\n" 2211 "void f( ) { }")); 2212 EXPECT_EQ("#if false\n" 2213 "void f( ) { }\n" 2214 "#endif\n" 2215 "void g() {}\n", 2216 format("#if false\n" 2217 "void f( ) { }\n" 2218 "#endif\n" 2219 "void g( ) { }\n")); 2220 EXPECT_EQ("enum E {\n" 2221 " One,\n" 2222 " Two,\n" 2223 "#if 0\n" 2224 "Three,\n" 2225 " Four,\n" 2226 "#endif\n" 2227 " Five\n" 2228 "};", 2229 format("enum E {\n" 2230 " One,Two,\n" 2231 "#if 0\n" 2232 "Three,\n" 2233 " Four,\n" 2234 "#endif\n" 2235 " Five};")); 2236 EXPECT_EQ("enum F {\n" 2237 " One,\n" 2238 "#if 1\n" 2239 " Two,\n" 2240 "#if 0\n" 2241 "Three,\n" 2242 " Four,\n" 2243 "#endif\n" 2244 " Five\n" 2245 "#endif\n" 2246 "};", 2247 format("enum F {\n" 2248 "One,\n" 2249 "#if 1\n" 2250 "Two,\n" 2251 "#if 0\n" 2252 "Three,\n" 2253 " Four,\n" 2254 "#endif\n" 2255 "Five\n" 2256 "#endif\n" 2257 "};")); 2258 EXPECT_EQ("enum G {\n" 2259 " One,\n" 2260 "#if 0\n" 2261 "Two,\n" 2262 "#else\n" 2263 " Three,\n" 2264 "#endif\n" 2265 " Four\n" 2266 "};", 2267 format("enum G {\n" 2268 "One,\n" 2269 "#if 0\n" 2270 "Two,\n" 2271 "#else\n" 2272 "Three,\n" 2273 "#endif\n" 2274 "Four\n" 2275 "};")); 2276 EXPECT_EQ("enum H {\n" 2277 " One,\n" 2278 "#if 0\n" 2279 "#ifdef Q\n" 2280 "Two,\n" 2281 "#else\n" 2282 "Three,\n" 2283 "#endif\n" 2284 "#endif\n" 2285 " Four\n" 2286 "};", 2287 format("enum H {\n" 2288 "One,\n" 2289 "#if 0\n" 2290 "#ifdef Q\n" 2291 "Two,\n" 2292 "#else\n" 2293 "Three,\n" 2294 "#endif\n" 2295 "#endif\n" 2296 "Four\n" 2297 "};")); 2298 EXPECT_EQ("enum I {\n" 2299 " One,\n" 2300 "#if /* test */ 0 || 1\n" 2301 "Two,\n" 2302 "Three,\n" 2303 "#endif\n" 2304 " Four\n" 2305 "};", 2306 format("enum I {\n" 2307 "One,\n" 2308 "#if /* test */ 0 || 1\n" 2309 "Two,\n" 2310 "Three,\n" 2311 "#endif\n" 2312 "Four\n" 2313 "};")); 2314 EXPECT_EQ("enum J {\n" 2315 " One,\n" 2316 "#if 0\n" 2317 "#if 0\n" 2318 "Two,\n" 2319 "#else\n" 2320 "Three,\n" 2321 "#endif\n" 2322 "Four,\n" 2323 "#endif\n" 2324 " Five\n" 2325 "};", 2326 format("enum J {\n" 2327 "One,\n" 2328 "#if 0\n" 2329 "#if 0\n" 2330 "Two,\n" 2331 "#else\n" 2332 "Three,\n" 2333 "#endif\n" 2334 "Four,\n" 2335 "#endif\n" 2336 "Five\n" 2337 "};")); 2338 2339 // Ignore stuff in SWIG-blocks. 2340 EXPECT_EQ("#ifdef SWIG\n" 2341 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n" 2342 "#endif\n" 2343 "void f() {}", 2344 format("#ifdef SWIG\n" 2345 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n" 2346 "#endif\n" 2347 "void f( ) { }")); 2348 EXPECT_EQ("#ifndef SWIG\n" 2349 "void f() {}\n" 2350 "#endif", 2351 format("#ifndef SWIG\n" 2352 "void f( ) { }\n" 2353 "#endif")); 2354 } 2355 2356 TEST_F(FormatTestComments, DontCrashOnBlockComments) { 2357 EXPECT_EQ( 2358 "int xxxxxxxxx; /* " 2359 "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\n" 2360 "zzzzzz\n" 2361 "0*/", 2362 format("int xxxxxxxxx; /* " 2363 "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy zzzzzz\n" 2364 "0*/")); 2365 } 2366 2367 TEST_F(FormatTestComments, BlockCommentsInControlLoops) { 2368 verifyFormat("if (0) /* a comment in a strange place */ {\n" 2369 " f();\n" 2370 "}"); 2371 verifyFormat("if (0) /* a comment in a strange place */ {\n" 2372 " f();\n" 2373 "} /* another comment */ else /* comment #3 */ {\n" 2374 " g();\n" 2375 "}"); 2376 verifyFormat("while (0) /* a comment in a strange place */ {\n" 2377 " f();\n" 2378 "}"); 2379 verifyFormat("for (;;) /* a comment in a strange place */ {\n" 2380 " f();\n" 2381 "}"); 2382 verifyFormat("do /* a comment in a strange place */ {\n" 2383 " f();\n" 2384 "} /* another comment */ while (0);"); 2385 } 2386 2387 TEST_F(FormatTestComments, BlockComments) { 2388 EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */", 2389 format("/* *//* */ /* */\n/* *//* */ /* */")); 2390 EXPECT_EQ("/* */ a /* */ b;", format(" /* */ a/* */ b;")); 2391 EXPECT_EQ("#define A /*123*/ \\\n" 2392 " b\n" 2393 "/* */\n" 2394 "someCall(\n" 2395 " parameter);", 2396 format("#define A /*123*/ b\n" 2397 "/* */\n" 2398 "someCall(parameter);", 2399 getLLVMStyleWithColumns(15))); 2400 2401 EXPECT_EQ("#define A\n" 2402 "/* */ someCall(\n" 2403 " parameter);", 2404 format("#define A\n" 2405 "/* */someCall(parameter);", 2406 getLLVMStyleWithColumns(15))); 2407 EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/")); 2408 EXPECT_EQ("/*\n" 2409 " *\n" 2410 " * aaaaaa\n" 2411 " * aaaaaa\n" 2412 " */", 2413 format("/*\n" 2414 "*\n" 2415 " * aaaaaa aaaaaa\n" 2416 "*/", 2417 getLLVMStyleWithColumns(10))); 2418 EXPECT_EQ("/*\n" 2419 "**\n" 2420 "* aaaaaa\n" 2421 "*aaaaaa\n" 2422 "*/", 2423 format("/*\n" 2424 "**\n" 2425 "* aaaaaa aaaaaa\n" 2426 "*/", 2427 getLLVMStyleWithColumns(10))); 2428 EXPECT_EQ("int aaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 2429 " /* line 1\n" 2430 " bbbbbbbbbbbb */\n" 2431 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 2432 format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 2433 " /* line 1\n" 2434 " bbbbbbbbbbbb */ bbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 2435 getLLVMStyleWithColumns(50))); 2436 2437 FormatStyle NoBinPacking = getLLVMStyle(); 2438 NoBinPacking.BinPackParameters = false; 2439 EXPECT_EQ("someFunction(1, /* comment 1 */\n" 2440 " 2, /* comment 2 */\n" 2441 " 3, /* comment 3 */\n" 2442 " aaaa,\n" 2443 " bbbb);", 2444 format("someFunction (1, /* comment 1 */\n" 2445 " 2, /* comment 2 */ \n" 2446 " 3, /* comment 3 */\n" 2447 "aaaa, bbbb );", 2448 NoBinPacking)); 2449 verifyFormat( 2450 "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2451 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 2452 EXPECT_EQ( 2453 "bool aaaaaaaaaaaaa = /* trailing comment */\n" 2454 " aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2455 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;", 2456 format( 2457 "bool aaaaaaaaaaaaa = /* trailing comment */\n" 2458 " aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2459 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;")); 2460 EXPECT_EQ( 2461 "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n" 2462 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n" 2463 "int cccccccccccccccccccccccccccccc; /* comment */\n", 2464 format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n" 2465 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n" 2466 "int cccccccccccccccccccccccccccccc; /* comment */\n")); 2467 2468 verifyFormat("void f(int * /* unused */) {}"); 2469 2470 EXPECT_EQ("/*\n" 2471 " **\n" 2472 " */", 2473 format("/*\n" 2474 " **\n" 2475 " */")); 2476 EXPECT_EQ("/*\n" 2477 " *q\n" 2478 " */", 2479 format("/*\n" 2480 " *q\n" 2481 " */")); 2482 EXPECT_EQ("/*\n" 2483 " * q\n" 2484 " */", 2485 format("/*\n" 2486 " * q\n" 2487 " */")); 2488 EXPECT_EQ("/*\n" 2489 " **/", 2490 format("/*\n" 2491 " **/")); 2492 EXPECT_EQ("/*\n" 2493 " ***/", 2494 format("/*\n" 2495 " ***/")); 2496 } 2497 2498 TEST_F(FormatTestComments, BlockCommentsInMacros) { 2499 EXPECT_EQ("#define A \\\n" 2500 " { \\\n" 2501 " /* one line */ \\\n" 2502 " someCall();", 2503 format("#define A { \\\n" 2504 " /* one line */ \\\n" 2505 " someCall();", 2506 getLLVMStyleWithColumns(20))); 2507 EXPECT_EQ("#define A \\\n" 2508 " { \\\n" 2509 " /* previous */ \\\n" 2510 " /* one line */ \\\n" 2511 " someCall();", 2512 format("#define A { \\\n" 2513 " /* previous */ \\\n" 2514 " /* one line */ \\\n" 2515 " someCall();", 2516 getLLVMStyleWithColumns(20))); 2517 } 2518 2519 TEST_F(FormatTestComments, BlockCommentsAtEndOfLine) { 2520 EXPECT_EQ("a = {\n" 2521 " 1111 /* */\n" 2522 "};", 2523 format("a = {1111 /* */\n" 2524 "};", 2525 getLLVMStyleWithColumns(15))); 2526 EXPECT_EQ("a = {\n" 2527 " 1111 /* */\n" 2528 "};", 2529 format("a = {1111 /* */\n" 2530 "};", 2531 getLLVMStyleWithColumns(15))); 2532 EXPECT_EQ("a = {\n" 2533 " 1111 /* a\n" 2534 " */\n" 2535 "};", 2536 format("a = {1111 /* a */\n" 2537 "};", 2538 getLLVMStyleWithColumns(15))); 2539 } 2540 2541 TEST_F(FormatTestComments, BreaksAfterMultilineBlockCommentsInParamLists) { 2542 EXPECT_EQ("a = f(/* long\n" 2543 " long */\n" 2544 " a);", 2545 format("a = f(/* long long */ a);", getLLVMStyleWithColumns(16))); 2546 EXPECT_EQ("a = f(\n" 2547 " /* long\n" 2548 " long */\n" 2549 " a);", 2550 format("a = f(/* long long */ a);", getLLVMStyleWithColumns(15))); 2551 2552 EXPECT_EQ("a = f(/* long\n" 2553 " long\n" 2554 " */\n" 2555 " a);", 2556 format("a = f(/* long\n" 2557 " long\n" 2558 " */a);", 2559 getLLVMStyleWithColumns(16))); 2560 2561 EXPECT_EQ("a = f(/* long\n" 2562 " long\n" 2563 " */\n" 2564 " a);", 2565 format("a = f(/* long\n" 2566 " long\n" 2567 " */ a);", 2568 getLLVMStyleWithColumns(16))); 2569 2570 EXPECT_EQ("a = f(/* long\n" 2571 " long\n" 2572 " */\n" 2573 " (1 + 1));", 2574 format("a = f(/* long\n" 2575 " long\n" 2576 " */ (1 + 1));", 2577 getLLVMStyleWithColumns(16))); 2578 2579 EXPECT_EQ( 2580 "a = f(a,\n" 2581 " /* long\n" 2582 " long */\n" 2583 " b);", 2584 format("a = f(a, /* long long */ b);", getLLVMStyleWithColumns(16))); 2585 2586 EXPECT_EQ( 2587 "a = f(\n" 2588 " a,\n" 2589 " /* long\n" 2590 " long */\n" 2591 " b);", 2592 format("a = f(a, /* long long */ b);", getLLVMStyleWithColumns(15))); 2593 2594 EXPECT_EQ("a = f(a,\n" 2595 " /* long\n" 2596 " long */\n" 2597 " (1 + 1));", 2598 format("a = f(a, /* long long */ (1 + 1));", 2599 getLLVMStyleWithColumns(16))); 2600 EXPECT_EQ("a = f(\n" 2601 " a,\n" 2602 " /* long\n" 2603 " long */\n" 2604 " (1 + 1));", 2605 format("a = f(a, /* long long */ (1 + 1));", 2606 getLLVMStyleWithColumns(15))); 2607 } 2608 2609 TEST_F(FormatTestComments, IndentLineCommentsInStartOfBlockAtEndOfFile) { 2610 verifyFormat("{\n" 2611 " // a\n" 2612 " // b"); 2613 } 2614 2615 TEST_F(FormatTestComments, AlignTrailingComments) { 2616 EXPECT_EQ("#define MACRO(V) \\\n" 2617 " V(Rt2) /* one more char */ \\\n" 2618 " V(Rs) /* than here */ \\\n" 2619 "/* comment 3 */\n", 2620 format("#define MACRO(V)\\\n" 2621 "V(Rt2) /* one more char */ \\\n" 2622 "V(Rs) /* than here */ \\\n" 2623 "/* comment 3 */\n", 2624 getLLVMStyleWithColumns(40))); 2625 EXPECT_EQ("int i = f(abc, // line 1\n" 2626 " d, // line 2\n" 2627 " // line 3\n" 2628 " b);", 2629 format("int i = f(abc, // line 1\n" 2630 " d, // line 2\n" 2631 " // line 3\n" 2632 " b);", 2633 getLLVMStyleWithColumns(40))); 2634 2635 // Align newly broken trailing comments. 2636 EXPECT_EQ("int ab; // line\n" 2637 "int a; // long\n" 2638 " // long\n", 2639 format("int ab; // line\n" 2640 "int a; // long long\n", 2641 getLLVMStyleWithColumns(15))); 2642 EXPECT_EQ("int ab; // line\n" 2643 "int a; // long\n" 2644 " // long\n" 2645 " // long", 2646 format("int ab; // line\n" 2647 "int a; // long long\n" 2648 " // long", 2649 getLLVMStyleWithColumns(15))); 2650 EXPECT_EQ("int ab; // line\n" 2651 "int a; // long\n" 2652 " // long\n" 2653 "pt c; // long", 2654 format("int ab; // line\n" 2655 "int a; // long long\n" 2656 "pt c; // long", 2657 getLLVMStyleWithColumns(15))); 2658 EXPECT_EQ("int ab; // line\n" 2659 "int a; // long\n" 2660 " // long\n" 2661 "\n" 2662 "// long", 2663 format("int ab; // line\n" 2664 "int a; // long long\n" 2665 "\n" 2666 "// long", 2667 getLLVMStyleWithColumns(15))); 2668 2669 // Don't align newly broken trailing comments if that would put them over the 2670 // column limit. 2671 EXPECT_EQ("int i, j; // line 1\n" 2672 "int k; // line longg\n" 2673 " // long", 2674 format("int i, j; // line 1\n" 2675 "int k; // line longg long", 2676 getLLVMStyleWithColumns(20))); 2677 2678 // Always align if ColumnLimit = 0 2679 EXPECT_EQ("int i, j; // line 1\n" 2680 "int k; // line longg long", 2681 format("int i, j; // line 1\n" 2682 "int k; // line longg long", 2683 getLLVMStyleWithColumns(0))); 2684 2685 // Align comment line sections aligned with the next token with the next 2686 // token. 2687 EXPECT_EQ("class A {\n" 2688 "public: // public comment\n" 2689 " // comment about a\n" 2690 " int a;\n" 2691 "};", 2692 format("class A {\n" 2693 "public: // public comment\n" 2694 " // comment about a\n" 2695 " int a;\n" 2696 "};", 2697 getLLVMStyleWithColumns(40))); 2698 EXPECT_EQ("class A {\n" 2699 "public: // public comment 1\n" 2700 " // public comment 2\n" 2701 " // comment 1 about a\n" 2702 " // comment 2 about a\n" 2703 " int a;\n" 2704 "};", 2705 format("class A {\n" 2706 "public: // public comment 1\n" 2707 " // public comment 2\n" 2708 " // comment 1 about a\n" 2709 " // comment 2 about a\n" 2710 " int a;\n" 2711 "};", 2712 getLLVMStyleWithColumns(40))); 2713 EXPECT_EQ("int f(int n) { // comment line 1 on f\n" 2714 " // comment line 2 on f\n" 2715 " // comment line 1 before return\n" 2716 " // comment line 2 before return\n" 2717 " return n; // comment line 1 on return\n" 2718 " // comment line 2 on return\n" 2719 " // comment line 1 after return\n" 2720 "}", 2721 format("int f(int n) { // comment line 1 on f\n" 2722 " // comment line 2 on f\n" 2723 " // comment line 1 before return\n" 2724 " // comment line 2 before return\n" 2725 " return n; // comment line 1 on return\n" 2726 " // comment line 2 on return\n" 2727 " // comment line 1 after return\n" 2728 "}", 2729 getLLVMStyleWithColumns(40))); 2730 EXPECT_EQ("int f(int n) {\n" 2731 " switch (n) { // comment line 1 on switch\n" 2732 " // comment line 2 on switch\n" 2733 " // comment line 1 before case 1\n" 2734 " // comment line 2 before case 1\n" 2735 " case 1: // comment line 1 on case 1\n" 2736 " // comment line 2 on case 1\n" 2737 " // comment line 1 before return 1\n" 2738 " // comment line 2 before return 1\n" 2739 " return 1; // comment line 1 on return 1\n" 2740 " // comment line 2 on return 1\n" 2741 " // comment line 1 before default\n" 2742 " // comment line 2 before default\n" 2743 " default: // comment line 1 on default\n" 2744 " // comment line 2 on default\n" 2745 " // comment line 1 before return 2\n" 2746 " return 2 * f(n - 1); // comment line 1 on return 2\n" 2747 " // comment line 2 on return 2\n" 2748 " // comment line 1 after return\n" 2749 " // comment line 2 after return\n" 2750 " }\n" 2751 "}", 2752 format("int f(int n) {\n" 2753 " switch (n) { // comment line 1 on switch\n" 2754 " // comment line 2 on switch\n" 2755 " // comment line 1 before case 1\n" 2756 " // comment line 2 before case 1\n" 2757 " case 1: // comment line 1 on case 1\n" 2758 " // comment line 2 on case 1\n" 2759 " // comment line 1 before return 1\n" 2760 " // comment line 2 before return 1\n" 2761 " return 1; // comment line 1 on return 1\n" 2762 " // comment line 2 on return 1\n" 2763 " // comment line 1 before default\n" 2764 " // comment line 2 before default\n" 2765 " default: // comment line 1 on default\n" 2766 " // comment line 2 on default\n" 2767 " // comment line 1 before return 2\n" 2768 " return 2 * f(n - 1); // comment line 1 on return 2\n" 2769 " // comment line 2 on return 2\n" 2770 " // comment line 1 after return\n" 2771 " // comment line 2 after return\n" 2772 " }\n" 2773 "}", 2774 getLLVMStyleWithColumns(80))); 2775 2776 // If all the lines in a sequence of line comments are aligned with the next 2777 // token, the first line belongs to the previous token and the other lines 2778 // belong to the next token. 2779 EXPECT_EQ("int a; // line about a\n" 2780 "long b;", 2781 format("int a; // line about a\n" 2782 " long b;", 2783 getLLVMStyleWithColumns(80))); 2784 EXPECT_EQ("int a; // line about a\n" 2785 "// line about b\n" 2786 "long b;", 2787 format("int a; // line about a\n" 2788 " // line about b\n" 2789 " long b;", 2790 getLLVMStyleWithColumns(80))); 2791 EXPECT_EQ("int a; // line about a\n" 2792 "// line 1 about b\n" 2793 "// line 2 about b\n" 2794 "long b;", 2795 format("int a; // line about a\n" 2796 " // line 1 about b\n" 2797 " // line 2 about b\n" 2798 " long b;", 2799 getLLVMStyleWithColumns(80))); 2800 2801 // Checks an edge case in preprocessor handling. 2802 // These comments should *not* be aligned 2803 EXPECT_NE( // change for EQ when fixed 2804 "#if FOO\n" 2805 "#else\n" 2806 "long a; // Line about a\n" 2807 "#endif\n" 2808 "#if BAR\n" 2809 "#else\n" 2810 "long b_long_name; // Line about b\n" 2811 "#endif\n", 2812 format("#if FOO\n" 2813 "#else\n" 2814 "long a; // Line about a\n" // Previous (bad) behavior 2815 "#endif\n" 2816 "#if BAR\n" 2817 "#else\n" 2818 "long b_long_name; // Line about b\n" 2819 "#endif\n", 2820 getLLVMStyleWithColumns(80))); 2821 2822 // bug 47589 2823 EXPECT_EQ( 2824 "namespace m {\n\n" 2825 "#define FOO_GLOBAL 0 // Global scope.\n" 2826 "#define FOO_LINKLOCAL 1 // Link-local scope.\n" 2827 "#define FOO_SITELOCAL 2 // Site-local scope (deprecated).\n" 2828 "#define FOO_UNIQUELOCAL 3 // Unique local\n" 2829 "#define FOO_NODELOCAL 4 // Loopback\n\n" 2830 "} // namespace m\n", 2831 format("namespace m {\n\n" 2832 "#define FOO_GLOBAL 0 // Global scope.\n" 2833 "#define FOO_LINKLOCAL 1 // Link-local scope.\n" 2834 "#define FOO_SITELOCAL 2 // Site-local scope (deprecated).\n" 2835 "#define FOO_UNIQUELOCAL 3 // Unique local\n" 2836 "#define FOO_NODELOCAL 4 // Loopback\n\n" 2837 "} // namespace m\n", 2838 getLLVMStyleWithColumns(80))); 2839 } 2840 2841 TEST_F(FormatTestComments, AlignsBlockCommentDecorations) { 2842 EXPECT_EQ("/*\n" 2843 " */", 2844 format("/*\n" 2845 "*/", 2846 getLLVMStyle())); 2847 EXPECT_EQ("/*\n" 2848 " */", 2849 format("/*\n" 2850 " */", 2851 getLLVMStyle())); 2852 EXPECT_EQ("/*\n" 2853 " */", 2854 format("/*\n" 2855 " */", 2856 getLLVMStyle())); 2857 2858 // Align a single line. 2859 EXPECT_EQ("/*\n" 2860 " * line */", 2861 format("/*\n" 2862 "* line */", 2863 getLLVMStyle())); 2864 EXPECT_EQ("/*\n" 2865 " * line */", 2866 format("/*\n" 2867 " * line */", 2868 getLLVMStyle())); 2869 EXPECT_EQ("/*\n" 2870 " * line */", 2871 format("/*\n" 2872 " * line */", 2873 getLLVMStyle())); 2874 EXPECT_EQ("/*\n" 2875 " * line */", 2876 format("/*\n" 2877 " * line */", 2878 getLLVMStyle())); 2879 EXPECT_EQ("/**\n" 2880 " * line */", 2881 format("/**\n" 2882 "* line */", 2883 getLLVMStyle())); 2884 EXPECT_EQ("/**\n" 2885 " * line */", 2886 format("/**\n" 2887 " * line */", 2888 getLLVMStyle())); 2889 EXPECT_EQ("/**\n" 2890 " * line */", 2891 format("/**\n" 2892 " * line */", 2893 getLLVMStyle())); 2894 EXPECT_EQ("/**\n" 2895 " * line */", 2896 format("/**\n" 2897 " * line */", 2898 getLLVMStyle())); 2899 EXPECT_EQ("/**\n" 2900 " * line */", 2901 format("/**\n" 2902 " * line */", 2903 getLLVMStyle())); 2904 2905 // Align the end '*/' after a line. 2906 EXPECT_EQ("/*\n" 2907 " * line\n" 2908 " */", 2909 format("/*\n" 2910 "* line\n" 2911 "*/", 2912 getLLVMStyle())); 2913 EXPECT_EQ("/*\n" 2914 " * line\n" 2915 " */", 2916 format("/*\n" 2917 " * line\n" 2918 " */", 2919 getLLVMStyle())); 2920 EXPECT_EQ("/*\n" 2921 " * line\n" 2922 " */", 2923 format("/*\n" 2924 " * line\n" 2925 " */", 2926 getLLVMStyle())); 2927 2928 // Align two lines. 2929 EXPECT_EQ("/* line 1\n" 2930 " * line 2 */", 2931 format("/* line 1\n" 2932 " * line 2 */", 2933 getLLVMStyle())); 2934 EXPECT_EQ("/* line 1\n" 2935 " * line 2 */", 2936 format("/* line 1\n" 2937 "* line 2 */", 2938 getLLVMStyle())); 2939 EXPECT_EQ("/* line 1\n" 2940 " * line 2 */", 2941 format("/* line 1\n" 2942 " * line 2 */", 2943 getLLVMStyle())); 2944 EXPECT_EQ("/* line 1\n" 2945 " * line 2 */", 2946 format("/* line 1\n" 2947 " * line 2 */", 2948 getLLVMStyle())); 2949 EXPECT_EQ("/* line 1\n" 2950 " * line 2 */", 2951 format("/* line 1\n" 2952 " * line 2 */", 2953 getLLVMStyle())); 2954 EXPECT_EQ("int i; /* line 1\n" 2955 " * line 2 */", 2956 format("int i; /* line 1\n" 2957 "* line 2 */", 2958 getLLVMStyle())); 2959 EXPECT_EQ("int i; /* line 1\n" 2960 " * line 2 */", 2961 format("int i; /* line 1\n" 2962 " * line 2 */", 2963 getLLVMStyle())); 2964 EXPECT_EQ("int i; /* line 1\n" 2965 " * line 2 */", 2966 format("int i; /* line 1\n" 2967 " * line 2 */", 2968 getLLVMStyle())); 2969 2970 // Align several lines. 2971 EXPECT_EQ("/* line 1\n" 2972 " * line 2\n" 2973 " * line 3 */", 2974 format("/* line 1\n" 2975 " * line 2\n" 2976 "* line 3 */", 2977 getLLVMStyle())); 2978 EXPECT_EQ("/* line 1\n" 2979 " * line 2\n" 2980 " * line 3 */", 2981 format("/* line 1\n" 2982 " * line 2\n" 2983 "* line 3 */", 2984 getLLVMStyle())); 2985 EXPECT_EQ("/*\n" 2986 "** line 1\n" 2987 "** line 2\n" 2988 "*/", 2989 format("/*\n" 2990 "** line 1\n" 2991 " ** line 2\n" 2992 "*/", 2993 getLLVMStyle())); 2994 2995 // Align with different indent after the decorations. 2996 EXPECT_EQ("/*\n" 2997 " * line 1\n" 2998 " * line 2\n" 2999 " * line 3\n" 3000 " * line 4\n" 3001 " */", 3002 format("/*\n" 3003 "* line 1\n" 3004 " * line 2\n" 3005 " * line 3\n" 3006 "* line 4\n" 3007 "*/", 3008 getLLVMStyle())); 3009 3010 // Align empty or blank lines. 3011 EXPECT_EQ("/**\n" 3012 " *\n" 3013 " *\n" 3014 " *\n" 3015 " */", 3016 format("/**\n" 3017 "* \n" 3018 " * \n" 3019 " *\n" 3020 "*/", 3021 getLLVMStyle())); 3022 3023 // Align while breaking and reflowing. 3024 EXPECT_EQ("/*\n" 3025 " * long long long\n" 3026 " * long long\n" 3027 " *\n" 3028 " * long */", 3029 format("/*\n" 3030 " * long long long long\n" 3031 " * long\n" 3032 " *\n" 3033 "* long */", 3034 getLLVMStyleWithColumns(20))); 3035 } 3036 3037 TEST_F(FormatTestComments, NoCrash_Bug34236) { 3038 // This is a test case from a crasher reported in: 3039 // https://bugs.llvm.org/show_bug.cgi?id=34236 3040 // Temporarily disable formatting for readability. 3041 // clang-format off 3042 EXPECT_EQ( 3043 "/* */ /*\n" 3044 " * a\n" 3045 " * b c d*/", 3046 format( 3047 "/* */ /*\n" 3048 " * a b\n" 3049 " * c d*/", 3050 getLLVMStyleWithColumns(80))); 3051 // clang-format on 3052 } 3053 3054 TEST_F(FormatTestComments, NonTrailingBlockComments) { 3055 verifyFormat("const /** comment comment */ A = B;", 3056 getLLVMStyleWithColumns(40)); 3057 3058 verifyFormat("const /** comment comment comment */ A =\n" 3059 " B;", 3060 getLLVMStyleWithColumns(40)); 3061 3062 EXPECT_EQ("const /** comment comment comment\n" 3063 " comment */\n" 3064 " A = B;", 3065 format("const /** comment comment comment comment */\n" 3066 " A = B;", 3067 getLLVMStyleWithColumns(40))); 3068 } 3069 3070 TEST_F(FormatTestComments, PythonStyleComments) { 3071 // Keeps a space after '#'. 3072 EXPECT_EQ("# comment\n" 3073 "key: value", 3074 format("#comment\n" 3075 "key:value", 3076 getTextProtoStyleWithColumns(20))); 3077 EXPECT_EQ("# comment\n" 3078 "key: value", 3079 format("# comment\n" 3080 "key:value", 3081 getTextProtoStyleWithColumns(20))); 3082 // Breaks long comment. 3083 EXPECT_EQ("# comment comment\n" 3084 "# comment\n" 3085 "key: value", 3086 format("# comment comment comment\n" 3087 "key:value", 3088 getTextProtoStyleWithColumns(20))); 3089 // Indents comments. 3090 EXPECT_EQ("data {\n" 3091 " # comment comment\n" 3092 " # comment\n" 3093 " key: value\n" 3094 "}", 3095 format("data {\n" 3096 "# comment comment comment\n" 3097 "key: value}", 3098 getTextProtoStyleWithColumns(20))); 3099 EXPECT_EQ("data {\n" 3100 " # comment comment\n" 3101 " # comment\n" 3102 " key: value\n" 3103 "}", 3104 format("data {# comment comment comment\n" 3105 "key: value}", 3106 getTextProtoStyleWithColumns(20))); 3107 // Reflows long comments. 3108 EXPECT_EQ("# comment comment\n" 3109 "# comment comment\n" 3110 "key: value", 3111 format("# comment comment comment\n" 3112 "# comment\n" 3113 "key:value", 3114 getTextProtoStyleWithColumns(20))); 3115 // Breaks trailing comments. 3116 EXPECT_EQ("k: val # comment\n" 3117 " # comment\n" 3118 "a: 1", 3119 format("k:val#comment comment\n" 3120 "a:1", 3121 getTextProtoStyleWithColumns(20))); 3122 EXPECT_EQ("id {\n" 3123 " k: val # comment\n" 3124 " # comment\n" 3125 " # line line\n" 3126 " a: 1\n" 3127 "}", 3128 format("id {k:val#comment comment\n" 3129 "# line line\n" 3130 "a:1}", 3131 getTextProtoStyleWithColumns(20))); 3132 // Aligns trailing comments. 3133 EXPECT_EQ("k: val # commen1\n" 3134 " # commen2\n" 3135 " # commen3\n" 3136 "# commen4\n" 3137 "a: 1 # commen5\n" 3138 " # commen6\n" 3139 " # commen7", 3140 format("k:val#commen1 commen2\n" 3141 " # commen3\n" 3142 "# commen4\n" 3143 "a:1#commen5 commen6\n" 3144 " #commen7", 3145 getTextProtoStyleWithColumns(20))); 3146 } 3147 3148 TEST_F(FormatTestComments, BreaksBeforeTrailingUnbreakableSequence) { 3149 // The end of /* trail */ is exactly at 80 columns, but the unbreakable 3150 // trailing sequence ); after it exceeds the column limit. Make sure we 3151 // correctly break the line in that case. 3152 verifyFormat("int a =\n" 3153 " foo(/* trail */);", 3154 getLLVMStyleWithColumns(23)); 3155 } 3156 3157 TEST_F(FormatTestComments, ReflowBackslashCrash) { 3158 // clang-format off 3159 EXPECT_EQ( 3160 "// How to run:\n" 3161 "// bbbbb run \\\n" 3162 "// rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr\n" 3163 "// \\ <log_file> -- --output_directory=\"<output_directory>\"", 3164 format( 3165 "// How to run:\n" 3166 "// bbbbb run \\\n" 3167 "// rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr \\\n" 3168 "// <log_file> -- --output_directory=\"<output_directory>\"")); 3169 // clang-format on 3170 } 3171 3172 TEST_F(FormatTestComments, IndentsLongJavadocAnnotatedLines) { 3173 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Java); 3174 Style.ColumnLimit = 60; 3175 FormatStyle Style20 = getGoogleStyle(FormatStyle::LK_Java); 3176 Style20.ColumnLimit = 20; 3177 EXPECT_EQ( 3178 "/**\n" 3179 " * @param x long long long long long long long long long\n" 3180 " * long\n" 3181 " */\n", 3182 format("/**\n" 3183 " * @param x long long long long long long long long long long\n" 3184 " */\n", 3185 Style)); 3186 EXPECT_EQ("/**\n" 3187 " * @param x long long long long long long long long long\n" 3188 " * long long long long long long long long long long\n" 3189 " */\n", 3190 format("/**\n" 3191 " * @param x long long long long long long long long long " 3192 "long long long long long long long long long long\n" 3193 " */\n", 3194 Style)); 3195 EXPECT_EQ("/**\n" 3196 " * @param x long long long long long long long long long\n" 3197 " * long long long long long long long long long long\n" 3198 " * long\n" 3199 " */\n", 3200 format("/**\n" 3201 " * @param x long long long long long long long long long " 3202 "long long long long long long long long long long long\n" 3203 " */\n", 3204 Style)); 3205 EXPECT_EQ("/**\n" 3206 " * Sentence that\n" 3207 " * should be broken.\n" 3208 " * @param short\n" 3209 " * keep indentation\n" 3210 " */\n", 3211 format("/**\n" 3212 " * Sentence that should be broken.\n" 3213 " * @param short\n" 3214 " * keep indentation\n" 3215 " */\n", 3216 Style20)); 3217 3218 EXPECT_EQ("/**\n" 3219 " * @param l1 long1\n" 3220 " * to break\n" 3221 " * @param l2 long2\n" 3222 " * to break\n" 3223 " */\n", 3224 format("/**\n" 3225 " * @param l1 long1 to break\n" 3226 " * @param l2 long2 to break\n" 3227 " */\n", 3228 Style20)); 3229 3230 EXPECT_EQ("/**\n" 3231 " * @param xx to\n" 3232 " * break\n" 3233 " * no reflow\n" 3234 " */\n", 3235 format("/**\n" 3236 " * @param xx to break\n" 3237 " * no reflow\n" 3238 " */\n", 3239 Style20)); 3240 3241 EXPECT_EQ("/**\n" 3242 " * @param xx to\n" 3243 " * break yes\n" 3244 " * reflow\n" 3245 " */\n", 3246 format("/**\n" 3247 " * @param xx to break\n" 3248 " * yes reflow\n" 3249 " */\n", 3250 Style20)); 3251 3252 FormatStyle JSStyle20 = getGoogleStyle(FormatStyle::LK_JavaScript); 3253 JSStyle20.ColumnLimit = 20; 3254 EXPECT_EQ("/**\n" 3255 " * @param l1 long1\n" 3256 " * to break\n" 3257 " */\n", 3258 format("/**\n" 3259 " * @param l1 long1 to break\n" 3260 " */\n", 3261 JSStyle20)); 3262 EXPECT_EQ("/**\n" 3263 " * @param {l1 long1\n" 3264 " * to break}\n" 3265 " */\n", 3266 format("/**\n" 3267 " * @param {l1 long1 to break}\n" 3268 " */\n", 3269 JSStyle20)); 3270 } 3271 3272 } // end namespace 3273 } // end namespace format 3274 } // end namespace clang 3275