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, DontSplitLineCommentsWithPragmas) { 749 FormatStyle Pragmas = getLLVMStyleWithColumns(30); 750 Pragmas.CommentPragmas = "^ IWYU pragma:"; 751 EXPECT_EQ( 752 "// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", 753 format("// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", Pragmas)); 754 EXPECT_EQ( 755 "/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", 756 format("/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", Pragmas)); 757 } 758 759 TEST_F(FormatTestComments, PriorityOfCommentBreaking) { 760 EXPECT_EQ("if (xxx ==\n" 761 " yyy && // aaaaaaaaaaaa bbbbbbbbb\n" 762 " zzz)\n" 763 " q();", 764 format("if (xxx == yyy && // aaaaaaaaaaaa bbbbbbbbb\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("if (xxxxxxxxxx &&\n" 775 " yyy || // aaaaaa bbbbbbbb cccc\n" 776 " zzz)\n" 777 " q();", 778 format("if (xxxxxxxxxx && yyy || // aaaaaa bbbbbbbb cccc\n" 779 " zzz) q();", 780 getLLVMStyleWithColumns(40))); 781 EXPECT_EQ("fffffffff(\n" 782 " &xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n" 783 " zzz);", 784 format("fffffffff(&xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n" 785 " zzz);", 786 getLLVMStyleWithColumns(40))); 787 } 788 789 TEST_F(FormatTestComments, MultiLineCommentsInDefines) { 790 EXPECT_EQ("#define A(x) /* \\\n" 791 " a comment \\\n" 792 " inside */ \\\n" 793 " f();", 794 format("#define A(x) /* \\\n" 795 " a comment \\\n" 796 " inside */ \\\n" 797 " f();", 798 getLLVMStyleWithColumns(17))); 799 EXPECT_EQ("#define A( \\\n" 800 " x) /* \\\n" 801 " a comment \\\n" 802 " inside */ \\\n" 803 " f();", 804 format("#define A( \\\n" 805 " x) /* \\\n" 806 " a comment \\\n" 807 " inside */ \\\n" 808 " f();", 809 getLLVMStyleWithColumns(17))); 810 } 811 812 TEST_F(FormatTestComments, ParsesCommentsAdjacentToPPDirectives) { 813 EXPECT_EQ("namespace {}\n// Test\n#define A", 814 format("namespace {}\n // Test\n#define A")); 815 EXPECT_EQ("namespace {}\n/* Test */\n#define A", 816 format("namespace {}\n /* Test */\n#define A")); 817 EXPECT_EQ("namespace {}\n/* Test */ #define A", 818 format("namespace {}\n /* Test */ #define A")); 819 } 820 821 TEST_F(FormatTestComments, KeepsLevelOfCommentBeforePPDirective) { 822 // Keep the current level if the comment was originally not aligned with 823 // the preprocessor directive. 824 EXPECT_EQ("void f() {\n" 825 " int i;\n" 826 " /* comment */\n" 827 "#ifdef A\n" 828 " int j;\n" 829 "}", 830 format("void f() {\n" 831 " int i;\n" 832 " /* comment */\n" 833 "#ifdef A\n" 834 " int j;\n" 835 "}")); 836 837 EXPECT_EQ("void f() {\n" 838 " int i;\n" 839 " /* comment */\n" 840 "\n" 841 "#ifdef A\n" 842 " int j;\n" 843 "}", 844 format("void f() {\n" 845 " int i;\n" 846 " /* comment */\n" 847 "\n" 848 "#ifdef A\n" 849 " int j;\n" 850 "}")); 851 852 EXPECT_EQ("int f(int i) {\n" 853 " if (true) {\n" 854 " ++i;\n" 855 " }\n" 856 " // comment\n" 857 "#ifdef A\n" 858 " int j;\n" 859 "#endif\n" 860 "}", 861 format("int f(int i) {\n" 862 " if (true) {\n" 863 " ++i;\n" 864 " }\n" 865 " // comment\n" 866 "#ifdef A\n" 867 "int j;\n" 868 "#endif\n" 869 "}")); 870 871 EXPECT_EQ("int f(int i) {\n" 872 " if (true) {\n" 873 " i++;\n" 874 " } else {\n" 875 " // comment in else\n" 876 "#ifdef A\n" 877 " j++;\n" 878 "#endif\n" 879 " }\n" 880 "}", 881 format("int f(int i) {\n" 882 " if (true) {\n" 883 " i++;\n" 884 " } else {\n" 885 " // comment in else\n" 886 "#ifdef A\n" 887 " j++;\n" 888 "#endif\n" 889 " }\n" 890 "}")); 891 892 EXPECT_EQ("int f(int i) {\n" 893 " if (true) {\n" 894 " i++;\n" 895 " } else {\n" 896 " /* comment in else */\n" 897 "#ifdef A\n" 898 " j++;\n" 899 "#endif\n" 900 " }\n" 901 "}", 902 format("int f(int i) {\n" 903 " if (true) {\n" 904 " i++;\n" 905 " } else {\n" 906 " /* comment in else */\n" 907 "#ifdef A\n" 908 " j++;\n" 909 "#endif\n" 910 " }\n" 911 "}")); 912 913 // Keep the current level if there is an empty line between the comment and 914 // the preprocessor directive. 915 EXPECT_EQ("void f() {\n" 916 " int i;\n" 917 " /* comment */\n" 918 "\n" 919 "#ifdef A\n" 920 " int j;\n" 921 "}", 922 format("void f() {\n" 923 " int i;\n" 924 "/* comment */\n" 925 "\n" 926 "#ifdef A\n" 927 " int j;\n" 928 "}")); 929 930 EXPECT_EQ("void f() {\n" 931 " int i;\n" 932 " return i;\n" 933 "}\n" 934 "// comment\n" 935 "\n" 936 "#ifdef A\n" 937 "int i;\n" 938 "#endif // A", 939 format("void f() {\n" 940 " int i;\n" 941 " return i;\n" 942 "}\n" 943 "// comment\n" 944 "\n" 945 "#ifdef A\n" 946 "int i;\n" 947 "#endif // A")); 948 949 EXPECT_EQ("int f(int i) {\n" 950 " if (true) {\n" 951 " ++i;\n" 952 " }\n" 953 " // comment\n" 954 "\n" 955 "#ifdef A\n" 956 " int j;\n" 957 "#endif\n" 958 "}", 959 format("int f(int i) {\n" 960 " if (true) {\n" 961 " ++i;\n" 962 " }\n" 963 " // comment\n" 964 "\n" 965 "#ifdef A\n" 966 " int j;\n" 967 "#endif\n" 968 "}")); 969 970 EXPECT_EQ("int f(int i) {\n" 971 " if (true) {\n" 972 " i++;\n" 973 " } else {\n" 974 " // comment in else\n" 975 "\n" 976 "#ifdef A\n" 977 " j++;\n" 978 "#endif\n" 979 " }\n" 980 "}", 981 format("int f(int i) {\n" 982 " if (true) {\n" 983 " i++;\n" 984 " } else {\n" 985 "// comment in else\n" 986 "\n" 987 "#ifdef A\n" 988 " j++;\n" 989 "#endif\n" 990 " }\n" 991 "}")); 992 993 EXPECT_EQ("int f(int i) {\n" 994 " if (true) {\n" 995 " i++;\n" 996 " } else {\n" 997 " /* comment in else */\n" 998 "\n" 999 "#ifdef A\n" 1000 " j++;\n" 1001 "#endif\n" 1002 " }\n" 1003 "}", 1004 format("int f(int i) {\n" 1005 " if (true) {\n" 1006 " i++;\n" 1007 " } else {\n" 1008 "/* comment in else */\n" 1009 "\n" 1010 "#ifdef A\n" 1011 " j++;\n" 1012 "#endif\n" 1013 " }\n" 1014 "}")); 1015 1016 // Align with the preprocessor directive if the comment was originally aligned 1017 // with the preprocessor directive and there is no newline between the comment 1018 // and the preprocessor directive. 1019 EXPECT_EQ("void f() {\n" 1020 " int i;\n" 1021 "/* comment */\n" 1022 "#ifdef A\n" 1023 " int j;\n" 1024 "}", 1025 format("void f() {\n" 1026 " int i;\n" 1027 "/* comment */\n" 1028 "#ifdef A\n" 1029 " int j;\n" 1030 "}")); 1031 1032 EXPECT_EQ("int f(int i) {\n" 1033 " if (true) {\n" 1034 " ++i;\n" 1035 " }\n" 1036 "// comment\n" 1037 "#ifdef A\n" 1038 " int j;\n" 1039 "#endif\n" 1040 "}", 1041 format("int f(int i) {\n" 1042 " if (true) {\n" 1043 " ++i;\n" 1044 " }\n" 1045 "// comment\n" 1046 "#ifdef A\n" 1047 " int j;\n" 1048 "#endif\n" 1049 "}")); 1050 1051 EXPECT_EQ("int f(int i) {\n" 1052 " if (true) {\n" 1053 " i++;\n" 1054 " } else {\n" 1055 "// comment in else\n" 1056 "#ifdef A\n" 1057 " j++;\n" 1058 "#endif\n" 1059 " }\n" 1060 "}", 1061 format("int f(int i) {\n" 1062 " if (true) {\n" 1063 " i++;\n" 1064 " } else {\n" 1065 " // comment in else\n" 1066 " #ifdef A\n" 1067 " j++;\n" 1068 "#endif\n" 1069 " }\n" 1070 "}")); 1071 1072 EXPECT_EQ("int f(int i) {\n" 1073 " if (true) {\n" 1074 " i++;\n" 1075 " } else {\n" 1076 "/* comment in else */\n" 1077 "#ifdef A\n" 1078 " j++;\n" 1079 "#endif\n" 1080 " }\n" 1081 "}", 1082 format("int f(int i) {\n" 1083 " if (true) {\n" 1084 " i++;\n" 1085 " } else {\n" 1086 " /* comment in else */\n" 1087 " #ifdef A\n" 1088 " j++;\n" 1089 "#endif\n" 1090 " }\n" 1091 "}")); 1092 } 1093 1094 TEST_F(FormatTestComments, SplitsLongLinesInComments) { 1095 // FIXME: Do we need to fix up the " */" at the end? 1096 // It doesn't look like any of our current logic triggers this. 1097 EXPECT_EQ("/* This is a long\n" 1098 " * comment that\n" 1099 " * doesn't fit on\n" 1100 " * one line. */", 1101 format("/* " 1102 "This is a long " 1103 "comment that " 1104 "doesn't " 1105 "fit on one line. */", 1106 getLLVMStyleWithColumns(20))); 1107 EXPECT_EQ( 1108 "/* a b c d\n" 1109 " * e f g\n" 1110 " * h i j k\n" 1111 " */", 1112 format("/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10))); 1113 EXPECT_EQ( 1114 "/* a b c d\n" 1115 " * e f g\n" 1116 " * h i j k\n" 1117 " */", 1118 format("\\\n/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10))); 1119 EXPECT_EQ("/*\n" 1120 "This is a long\n" 1121 "comment that doesn't\n" 1122 "fit on one line.\n" 1123 "*/", 1124 format("/*\n" 1125 "This is a long " 1126 "comment that doesn't " 1127 "fit on one line. \n" 1128 "*/", 1129 getLLVMStyleWithColumns(20))); 1130 EXPECT_EQ("/*\n" 1131 " * This is a long\n" 1132 " * comment that\n" 1133 " * doesn't fit on\n" 1134 " * one line.\n" 1135 " */", 1136 format("/* \n" 1137 " * This is a long " 1138 " comment that " 1139 " doesn't fit on " 1140 " one line. \n" 1141 " */", 1142 getLLVMStyleWithColumns(20))); 1143 EXPECT_EQ("/*\n" 1144 " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n" 1145 " * so_it_should_be_broken\n" 1146 " * wherever_a_space_occurs\n" 1147 " */", 1148 format("/*\n" 1149 " * This_is_a_comment_with_words_that_dont_fit_on_one_line " 1150 " so_it_should_be_broken " 1151 " wherever_a_space_occurs \n" 1152 " */", 1153 getLLVMStyleWithColumns(20))); 1154 EXPECT_EQ("/*\n" 1155 " * This_comment_can_not_be_broken_into_lines\n" 1156 " */", 1157 format("/*\n" 1158 " * This_comment_can_not_be_broken_into_lines\n" 1159 " */", 1160 getLLVMStyleWithColumns(20))); 1161 EXPECT_EQ("{\n" 1162 " /*\n" 1163 " This is another\n" 1164 " long comment that\n" 1165 " doesn't fit on one\n" 1166 " line 1234567890\n" 1167 " */\n" 1168 "}", 1169 format("{\n" 1170 "/*\n" 1171 "This is another " 1172 " long comment that " 1173 " doesn't fit on one" 1174 " line 1234567890\n" 1175 "*/\n" 1176 "}", 1177 getLLVMStyleWithColumns(20))); 1178 EXPECT_EQ("{\n" 1179 " /*\n" 1180 " * This i s\n" 1181 " * another comment\n" 1182 " * t hat doesn' t\n" 1183 " * fit on one l i\n" 1184 " * n e\n" 1185 " */\n" 1186 "}", 1187 format("{\n" 1188 "/*\n" 1189 " * This i s" 1190 " another comment" 1191 " t hat doesn' t" 1192 " fit on one l i" 1193 " n e\n" 1194 " */\n" 1195 "}", 1196 getLLVMStyleWithColumns(20))); 1197 EXPECT_EQ("/*\n" 1198 " * This is a long\n" 1199 " * comment that\n" 1200 " * doesn't fit on\n" 1201 " * one line\n" 1202 " */", 1203 format(" /*\n" 1204 " * This is a long comment that doesn't fit on one line\n" 1205 " */", 1206 getLLVMStyleWithColumns(20))); 1207 EXPECT_EQ("{\n" 1208 " if (something) /* This is a\n" 1209 " long\n" 1210 " comment */\n" 1211 " ;\n" 1212 "}", 1213 format("{\n" 1214 " if (something) /* This is a long comment */\n" 1215 " ;\n" 1216 "}", 1217 getLLVMStyleWithColumns(30))); 1218 1219 EXPECT_EQ("/* A comment before\n" 1220 " * a macro\n" 1221 " * definition */\n" 1222 "#define a b", 1223 format("/* A comment before a macro definition */\n" 1224 "#define a b", 1225 getLLVMStyleWithColumns(20))); 1226 1227 EXPECT_EQ("/* some comment\n" 1228 " * a comment that\n" 1229 " * we break another\n" 1230 " * comment we have\n" 1231 " * to break a left\n" 1232 " * comment\n" 1233 " */", 1234 format(" /* some comment\n" 1235 " * a comment that we break\n" 1236 " * another comment we have to break\n" 1237 "* a left comment\n" 1238 " */", 1239 getLLVMStyleWithColumns(20))); 1240 1241 EXPECT_EQ("/**\n" 1242 " * multiline block\n" 1243 " * comment\n" 1244 " *\n" 1245 " */", 1246 format("/**\n" 1247 " * multiline block comment\n" 1248 " *\n" 1249 " */", 1250 getLLVMStyleWithColumns(20))); 1251 1252 // This reproduces a crashing bug where both adaptStartOfLine and 1253 // getCommentSplit were trying to wrap after the "/**". 1254 EXPECT_EQ("/** multilineblockcommentwithnowrapopportunity */", 1255 format("/** multilineblockcommentwithnowrapopportunity */", 1256 getLLVMStyleWithColumns(20))); 1257 1258 EXPECT_EQ("/*\n" 1259 "\n" 1260 "\n" 1261 " */\n", 1262 format(" /* \n" 1263 " \n" 1264 " \n" 1265 " */\n")); 1266 1267 EXPECT_EQ("/* a a */", 1268 format("/* a a */", getLLVMStyleWithColumns(15))); 1269 EXPECT_EQ("/* a a bc */", 1270 format("/* a a bc */", getLLVMStyleWithColumns(15))); 1271 EXPECT_EQ("/* aaa aaa\n" 1272 " * aaaaa */", 1273 format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15))); 1274 EXPECT_EQ("/* aaa aaa\n" 1275 " * aaaaa */", 1276 format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15))); 1277 } 1278 1279 TEST_F(FormatTestComments, SplitsLongLinesInCommentsInPreprocessor) { 1280 EXPECT_EQ("#define X \\\n" 1281 " /* \\\n" 1282 " Test \\\n" 1283 " Macro comment \\\n" 1284 " with a long \\\n" 1285 " line \\\n" 1286 " */ \\\n" 1287 " A + B", 1288 format("#define X \\\n" 1289 " /*\n" 1290 " Test\n" 1291 " Macro comment with a long line\n" 1292 " */ \\\n" 1293 " A + B", 1294 getLLVMStyleWithColumns(20))); 1295 EXPECT_EQ("#define X \\\n" 1296 " /* Macro comment \\\n" 1297 " with a long \\\n" 1298 " line */ \\\n" 1299 " A + B", 1300 format("#define X \\\n" 1301 " /* Macro comment with a long\n" 1302 " line */ \\\n" 1303 " A + B", 1304 getLLVMStyleWithColumns(20))); 1305 EXPECT_EQ("#define X \\\n" 1306 " /* Macro comment \\\n" 1307 " * with a long \\\n" 1308 " * line */ \\\n" 1309 " A + B", 1310 format("#define X \\\n" 1311 " /* Macro comment with a long line */ \\\n" 1312 " A + B", 1313 getLLVMStyleWithColumns(20))); 1314 } 1315 1316 TEST_F(FormatTestComments, KeepsTrailingPPCommentsAndSectionCommentsSeparate) { 1317 verifyFormat("#ifdef A // line about A\n" 1318 "// section comment\n" 1319 "#endif", 1320 getLLVMStyleWithColumns(80)); 1321 verifyFormat("#ifdef A // line 1 about A\n" 1322 " // line 2 about A\n" 1323 "// section comment\n" 1324 "#endif", 1325 getLLVMStyleWithColumns(80)); 1326 EXPECT_EQ("#ifdef A // line 1 about A\n" 1327 " // line 2 about A\n" 1328 "// section comment\n" 1329 "#endif", 1330 format("#ifdef A // line 1 about A\n" 1331 " // line 2 about A\n" 1332 "// section comment\n" 1333 "#endif", 1334 getLLVMStyleWithColumns(80))); 1335 verifyFormat("int f() {\n" 1336 " int i;\n" 1337 "#ifdef A // comment about A\n" 1338 " // section comment 1\n" 1339 " // section comment 2\n" 1340 " i = 2;\n" 1341 "#else // comment about #else\n" 1342 " // section comment 3\n" 1343 " i = 4;\n" 1344 "#endif\n" 1345 "}", 1346 getLLVMStyleWithColumns(80)); 1347 } 1348 1349 TEST_F(FormatTestComments, AlignsPPElseEndifComments) { 1350 verifyFormat("#if A\n" 1351 "#else // A\n" 1352 "int iiii;\n" 1353 "#endif // B", 1354 getLLVMStyleWithColumns(20)); 1355 verifyFormat("#if A\n" 1356 "#else // A\n" 1357 "int iiii; // CC\n" 1358 "#endif // B", 1359 getLLVMStyleWithColumns(20)); 1360 EXPECT_EQ("#if A\n" 1361 "#else // A1\n" 1362 " // A2\n" 1363 "int ii;\n" 1364 "#endif // B", 1365 format("#if A\n" 1366 "#else // A1\n" 1367 " // A2\n" 1368 "int ii;\n" 1369 "#endif // B", 1370 getLLVMStyleWithColumns(20))); 1371 } 1372 1373 TEST_F(FormatTestComments, CommentsInStaticInitializers) { 1374 EXPECT_EQ( 1375 "static SomeType type = {aaaaaaaaaaaaaaaaaaaa, /* comment */\n" 1376 " aaaaaaaaaaaaaaaaaaaa /* comment */,\n" 1377 " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n" 1378 " aaaaaaaaaaaaaaaaaaaa, // comment\n" 1379 " aaaaaaaaaaaaaaaaaaaa};", 1380 format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n" 1381 " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n" 1382 " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n" 1383 " aaaaaaaaaaaaaaaaaaaa , // comment\n" 1384 " aaaaaaaaaaaaaaaaaaaa };")); 1385 verifyFormat("static SomeType type = {aaaaaaaaaaa, // comment for aa...\n" 1386 " bbbbbbbbbbb, ccccccccccc};"); 1387 verifyFormat("static SomeType type = {aaaaaaaaaaa,\n" 1388 " // comment for bb....\n" 1389 " bbbbbbbbbbb, ccccccccccc};"); 1390 verifyGoogleFormat( 1391 "static SomeType type = {aaaaaaaaaaa, // comment for aa...\n" 1392 " bbbbbbbbbbb, ccccccccccc};"); 1393 verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n" 1394 " // comment for bb....\n" 1395 " bbbbbbbbbbb, ccccccccccc};"); 1396 1397 verifyFormat("S s = {{a, b, c}, // Group #1\n" 1398 " {d, e, f}, // Group #2\n" 1399 " {g, h, i}}; // Group #3"); 1400 verifyFormat("S s = {{// Group #1\n" 1401 " a, b, c},\n" 1402 " {// Group #2\n" 1403 " d, e, f},\n" 1404 " {// Group #3\n" 1405 " g, h, i}};"); 1406 1407 EXPECT_EQ("S s = {\n" 1408 " // Some comment\n" 1409 " a,\n" 1410 "\n" 1411 " // Comment after empty line\n" 1412 " b}", 1413 format("S s = {\n" 1414 " // Some comment\n" 1415 " a,\n" 1416 " \n" 1417 " // Comment after empty line\n" 1418 " b\n" 1419 "}")); 1420 EXPECT_EQ("S s = {\n" 1421 " /* Some comment */\n" 1422 " a,\n" 1423 "\n" 1424 " /* Comment after empty line */\n" 1425 " b}", 1426 format("S s = {\n" 1427 " /* Some comment */\n" 1428 " a,\n" 1429 " \n" 1430 " /* Comment after empty line */\n" 1431 " b\n" 1432 "}")); 1433 verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n" 1434 " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n" 1435 " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n" 1436 " 0x00, 0x00, 0x00, 0x00}; // comment\n"); 1437 } 1438 1439 TEST_F(FormatTestComments, LineCommentsAfterRightBrace) { 1440 EXPECT_EQ("if (true) { // comment about branch\n" 1441 " // comment about f\n" 1442 " f();\n" 1443 "}", 1444 format("if (true) { // comment about branch\n" 1445 " // comment about f\n" 1446 " f();\n" 1447 "}", 1448 getLLVMStyleWithColumns(80))); 1449 EXPECT_EQ("if (1) { // if line 1\n" 1450 " // if line 2\n" 1451 " // if line 3\n" 1452 " // f line 1\n" 1453 " // f line 2\n" 1454 " f();\n" 1455 "} else { // else line 1\n" 1456 " // else line 2\n" 1457 " // else line 3\n" 1458 " // g line 1\n" 1459 " g();\n" 1460 "}", 1461 format("if (1) { // if line 1\n" 1462 " // if line 2\n" 1463 " // if line 3\n" 1464 " // f line 1\n" 1465 " // f line 2\n" 1466 " f();\n" 1467 "} else { // else line 1\n" 1468 " // else line 2\n" 1469 " // else line 3\n" 1470 " // g line 1\n" 1471 " g();\n" 1472 "}")); 1473 EXPECT_EQ("do { // line 1\n" 1474 " // line 2\n" 1475 " // line 3\n" 1476 " f();\n" 1477 "} while (true);", 1478 format("do { // line 1\n" 1479 " // line 2\n" 1480 " // line 3\n" 1481 " f();\n" 1482 "} while (true);", 1483 getLLVMStyleWithColumns(80))); 1484 EXPECT_EQ("while (a < b) { // line 1\n" 1485 " // line 2\n" 1486 " // line 3\n" 1487 " f();\n" 1488 "}", 1489 format("while (a < b) {// line 1\n" 1490 " // line 2\n" 1491 " // line 3\n" 1492 " f();\n" 1493 "}", 1494 getLLVMStyleWithColumns(80))); 1495 } 1496 1497 TEST_F(FormatTestComments, ReflowsComments) { 1498 // Break a long line and reflow with the full next line. 1499 EXPECT_EQ("// long long long\n" 1500 "// long long", 1501 format("// long long long long\n" 1502 "// long", 1503 getLLVMStyleWithColumns(20))); 1504 1505 // Keep the trailing newline while reflowing. 1506 EXPECT_EQ("// long long long\n" 1507 "// long long\n", 1508 format("// long long long long\n" 1509 "// long\n", 1510 getLLVMStyleWithColumns(20))); 1511 1512 // Break a long line and reflow with a part of the next line. 1513 EXPECT_EQ("// long long long\n" 1514 "// long long\n" 1515 "// long_long", 1516 format("// long long long long\n" 1517 "// long long_long", 1518 getLLVMStyleWithColumns(20))); 1519 1520 // Break but do not reflow if the first word from the next line is too long. 1521 EXPECT_EQ("// long long long\n" 1522 "// long\n" 1523 "// long_long_long\n", 1524 format("// long long long long\n" 1525 "// long_long_long\n", 1526 getLLVMStyleWithColumns(20))); 1527 1528 // Don't break or reflow short lines. 1529 verifyFormat("// long\n" 1530 "// long long long lo\n" 1531 "// long long long lo\n" 1532 "// long", 1533 getLLVMStyleWithColumns(20)); 1534 1535 // Keep prefixes and decorations while reflowing. 1536 EXPECT_EQ("/// long long long\n" 1537 "/// long long\n", 1538 format("/// long long long long\n" 1539 "/// long\n", 1540 getLLVMStyleWithColumns(20))); 1541 EXPECT_EQ("//! long long long\n" 1542 "//! long long\n", 1543 format("//! long long long long\n" 1544 "//! long\n", 1545 getLLVMStyleWithColumns(20))); 1546 EXPECT_EQ("/* long long long\n" 1547 " * long long */", 1548 format("/* long long long long\n" 1549 " * long */", 1550 getLLVMStyleWithColumns(20))); 1551 EXPECT_EQ("///< long long long\n" 1552 "///< long long\n", 1553 format("///< long long long long\n" 1554 "///< long\n", 1555 getLLVMStyleWithColumns(20))); 1556 EXPECT_EQ("//!< long long long\n" 1557 "//!< long long\n", 1558 format("//!< long long long long\n" 1559 "//!< long\n", 1560 getLLVMStyleWithColumns(20))); 1561 1562 // Don't bring leading whitespace up while reflowing. 1563 EXPECT_EQ("/* long long long\n" 1564 " * long long long\n" 1565 " */", 1566 format("/* long long long long\n" 1567 " * long long\n" 1568 " */", 1569 getLLVMStyleWithColumns(20))); 1570 1571 // Reflow the last line of a block comment with its trailing '*/'. 1572 EXPECT_EQ("/* long long long\n" 1573 " long long */", 1574 format("/* long long long long\n" 1575 " long */", 1576 getLLVMStyleWithColumns(20))); 1577 1578 // Reflow two short lines; keep the postfix of the last one. 1579 EXPECT_EQ("/* long long long\n" 1580 " * long long long */", 1581 format("/* long long long long\n" 1582 " * long\n" 1583 " * long */", 1584 getLLVMStyleWithColumns(20))); 1585 1586 // Put the postfix of the last short reflow line on a newline if it doesn't 1587 // fit. 1588 EXPECT_EQ("/* long long long\n" 1589 " * long long longg\n" 1590 " */", 1591 format("/* long long long long\n" 1592 " * long\n" 1593 " * longg */", 1594 getLLVMStyleWithColumns(20))); 1595 1596 // Reflow lines with leading whitespace. 1597 EXPECT_EQ("{\n" 1598 " /*\n" 1599 " * long long long\n" 1600 " * long long long\n" 1601 " * long long long\n" 1602 " */\n" 1603 "}", 1604 format("{\n" 1605 "/*\n" 1606 " * long long long long\n" 1607 " * long\n" 1608 " * long long long long\n" 1609 " */\n" 1610 "}", 1611 getLLVMStyleWithColumns(20))); 1612 1613 // Break single line block comments that are first in the line with ' *' 1614 // decoration. 1615 EXPECT_EQ("/* long long long\n" 1616 " * long */", 1617 format("/* long long long long */", getLLVMStyleWithColumns(20))); 1618 1619 // Break single line block comment that are not first in the line with ' ' 1620 // decoration. 1621 EXPECT_EQ("int i; /* long long\n" 1622 " long */", 1623 format("int i; /* long long long */", getLLVMStyleWithColumns(20))); 1624 1625 // Reflow a line that goes just over the column limit. 1626 EXPECT_EQ("// long long long\n" 1627 "// lon long", 1628 format("// long long long lon\n" 1629 "// long", 1630 getLLVMStyleWithColumns(20))); 1631 1632 // Stop reflowing if the next line has a different indentation than the 1633 // previous line. 1634 EXPECT_EQ("// long long long\n" 1635 "// long\n" 1636 "// long long\n" 1637 "// long", 1638 format("// long long long long\n" 1639 "// long long\n" 1640 "// long", 1641 getLLVMStyleWithColumns(20))); 1642 1643 // Reflow into the last part of a really long line that has been broken into 1644 // multiple lines. 1645 EXPECT_EQ("// long long long\n" 1646 "// long long long\n" 1647 "// long long long\n", 1648 format("// long long long long long long long long\n" 1649 "// long\n", 1650 getLLVMStyleWithColumns(20))); 1651 1652 // Break the first line, then reflow the beginning of the second and third 1653 // line up. 1654 EXPECT_EQ("// long long long\n" 1655 "// lon1 lon2 lon2\n" 1656 "// lon2 lon3 lon3", 1657 format("// long long long lon1\n" 1658 "// lon2 lon2 lon2\n" 1659 "// lon3 lon3", 1660 getLLVMStyleWithColumns(20))); 1661 1662 // Reflow the beginning of the second line, then break the rest. 1663 EXPECT_EQ("// long long long\n" 1664 "// lon1 lon2 lon2\n" 1665 "// lon2 lon2 lon2\n" 1666 "// lon3", 1667 format("// long long long lon1\n" 1668 "// lon2 lon2 lon2 lon2 lon2 lon3", 1669 getLLVMStyleWithColumns(20))); 1670 1671 // Shrink the first line, then reflow the second line up. 1672 EXPECT_EQ("// long long long", format("// long long\n" 1673 "// long", 1674 getLLVMStyleWithColumns(20))); 1675 1676 // Don't shrink leading whitespace. 1677 EXPECT_EQ("int i; /// a", 1678 format("int i; /// a", getLLVMStyleWithColumns(20))); 1679 1680 // Shrink trailing whitespace if there is no postfix and reflow. 1681 EXPECT_EQ("// long long long\n" 1682 "// long long", 1683 format("// long long long long \n" 1684 "// long", 1685 getLLVMStyleWithColumns(20))); 1686 1687 // Shrink trailing whitespace to a single one if there is postfix. 1688 EXPECT_EQ("/* long long long */", 1689 format("/* long long long */", getLLVMStyleWithColumns(20))); 1690 1691 // Break a block comment postfix if exceeding the line limit. 1692 EXPECT_EQ("/* long\n" 1693 " */", 1694 format("/* long */", getLLVMStyleWithColumns(20))); 1695 1696 // Reflow indented comments. 1697 EXPECT_EQ("{\n" 1698 " // long long long\n" 1699 " // long long\n" 1700 " int i; /* long lon\n" 1701 " g long\n" 1702 " */\n" 1703 "}", 1704 format("{\n" 1705 " // long long long long\n" 1706 " // long\n" 1707 " int i; /* long lon g\n" 1708 " long */\n" 1709 "}", 1710 getLLVMStyleWithColumns(20))); 1711 1712 // Don't realign trailing comments after reflow has happened. 1713 EXPECT_EQ("// long long long\n" 1714 "// long long\n" 1715 "long i; // long", 1716 format("// long long long long\n" 1717 "// long\n" 1718 "long i; // long", 1719 getLLVMStyleWithColumns(20))); 1720 EXPECT_EQ("// long long long\n" 1721 "// longng long long\n" 1722 "// long lo", 1723 format("// long long long longng\n" 1724 "// long long long\n" 1725 "// lo", 1726 getLLVMStyleWithColumns(20))); 1727 1728 // Reflow lines after a broken line. 1729 EXPECT_EQ("int a; // Trailing\n" 1730 " // comment on\n" 1731 " // 2 or 3\n" 1732 " // lines.\n", 1733 format("int a; // Trailing comment\n" 1734 " // on 2\n" 1735 " // or 3\n" 1736 " // lines.\n", 1737 getLLVMStyleWithColumns(20))); 1738 EXPECT_EQ("/// This long line\n" 1739 "/// gets reflown.\n", 1740 format("/// This long line gets\n" 1741 "/// reflown.\n", 1742 getLLVMStyleWithColumns(20))); 1743 EXPECT_EQ("//! This long line\n" 1744 "//! gets reflown.\n", 1745 format(" //! This long line gets\n" 1746 " //! reflown.\n", 1747 getLLVMStyleWithColumns(20))); 1748 EXPECT_EQ("/* This long line\n" 1749 " * gets reflown.\n" 1750 " */\n", 1751 format("/* This long line gets\n" 1752 " * reflown.\n" 1753 " */\n", 1754 getLLVMStyleWithColumns(20))); 1755 1756 // Reflow after indentation makes a line too long. 1757 EXPECT_EQ("{\n" 1758 " // long long long\n" 1759 " // lo long\n" 1760 "}\n", 1761 format("{\n" 1762 "// long long long lo\n" 1763 "// long\n" 1764 "}\n", 1765 getLLVMStyleWithColumns(20))); 1766 1767 // Break and reflow multiple lines. 1768 EXPECT_EQ("/*\n" 1769 " * Reflow the end of\n" 1770 " * line by 11 22 33\n" 1771 " * 4.\n" 1772 " */\n", 1773 format("/*\n" 1774 " * Reflow the end of line\n" 1775 " * by\n" 1776 " * 11\n" 1777 " * 22\n" 1778 " * 33\n" 1779 " * 4.\n" 1780 " */\n", 1781 getLLVMStyleWithColumns(20))); 1782 EXPECT_EQ("/// First line gets\n" 1783 "/// broken. Second\n" 1784 "/// line gets\n" 1785 "/// reflown and\n" 1786 "/// broken. Third\n" 1787 "/// gets reflown.\n", 1788 format("/// First line gets broken.\n" 1789 "/// Second line gets reflown and broken.\n" 1790 "/// Third gets reflown.\n", 1791 getLLVMStyleWithColumns(20))); 1792 EXPECT_EQ("int i; // first long\n" 1793 " // long snd\n" 1794 " // long.\n", 1795 format("int i; // first long long\n" 1796 " // snd long.\n", 1797 getLLVMStyleWithColumns(20))); 1798 EXPECT_EQ("{\n" 1799 " // first long line\n" 1800 " // line second\n" 1801 " // long line line\n" 1802 " // third long line\n" 1803 " // line\n" 1804 "}\n", 1805 format("{\n" 1806 " // first long line line\n" 1807 " // second long line line\n" 1808 " // third long line line\n" 1809 "}\n", 1810 getLLVMStyleWithColumns(20))); 1811 EXPECT_EQ("int i; /* first line\n" 1812 " * second\n" 1813 " * line third\n" 1814 " * line\n" 1815 " */", 1816 format("int i; /* first line\n" 1817 " * second line\n" 1818 " * third line\n" 1819 " */", 1820 getLLVMStyleWithColumns(20))); 1821 1822 // Reflow the last two lines of a section that starts with a line having 1823 // different indentation. 1824 EXPECT_EQ("// long\n" 1825 "// long long long\n" 1826 "// long long", 1827 format("// long\n" 1828 "// long long long long\n" 1829 "// long", 1830 getLLVMStyleWithColumns(20))); 1831 1832 // Keep the block comment endling '*/' while reflowing. 1833 EXPECT_EQ("/* Long long long\n" 1834 " * line short */\n", 1835 format("/* Long long long line\n" 1836 " * short */\n", 1837 getLLVMStyleWithColumns(20))); 1838 1839 // Don't reflow between separate blocks of comments. 1840 EXPECT_EQ("/* First comment\n" 1841 " * block will */\n" 1842 "/* Snd\n" 1843 " */\n", 1844 format("/* First comment block\n" 1845 " * will */\n" 1846 "/* Snd\n" 1847 " */\n", 1848 getLLVMStyleWithColumns(20))); 1849 1850 // Don't reflow across blank comment lines. 1851 EXPECT_EQ("int i; // This long\n" 1852 " // line gets\n" 1853 " // broken.\n" 1854 " //\n" 1855 " // keep.\n", 1856 format("int i; // This long line gets broken.\n" 1857 " // \n" 1858 " // keep.\n", 1859 getLLVMStyleWithColumns(20))); 1860 EXPECT_EQ("{\n" 1861 " /// long long long\n" 1862 " /// long long\n" 1863 " ///\n" 1864 " /// long\n" 1865 "}", 1866 format("{\n" 1867 " /// long long long long\n" 1868 " /// long\n" 1869 " ///\n" 1870 " /// long\n" 1871 "}", 1872 getLLVMStyleWithColumns(20))); 1873 EXPECT_EQ("//! long long long\n" 1874 "//! long\n" 1875 "\n" 1876 "//! long", 1877 format("//! long long long long\n" 1878 "\n" 1879 "//! long", 1880 getLLVMStyleWithColumns(20))); 1881 EXPECT_EQ("/* long long long\n" 1882 " long\n" 1883 "\n" 1884 " long */", 1885 format("/* long long long long\n" 1886 "\n" 1887 " long */", 1888 getLLVMStyleWithColumns(20))); 1889 EXPECT_EQ("/* long long long\n" 1890 " * long\n" 1891 " *\n" 1892 " * long */", 1893 format("/* long long long long\n" 1894 " *\n" 1895 " * long */", 1896 getLLVMStyleWithColumns(20))); 1897 1898 // Don't reflow lines having content that is a single character. 1899 EXPECT_EQ("// long long long\n" 1900 "// long\n" 1901 "// l", 1902 format("// long long long long\n" 1903 "// l", 1904 getLLVMStyleWithColumns(20))); 1905 1906 // Don't reflow lines starting with two punctuation characters. 1907 EXPECT_EQ("// long long long\n" 1908 "// long\n" 1909 "// ... --- ...", 1910 format("// long long long long\n" 1911 "// ... --- ...", 1912 getLLVMStyleWithColumns(20))); 1913 1914 // Don't reflow lines starting with '@'. 1915 EXPECT_EQ("// long long long\n" 1916 "// long\n" 1917 "// @param arg", 1918 format("// long long long long\n" 1919 "// @param arg", 1920 getLLVMStyleWithColumns(20))); 1921 1922 // Don't reflow lines starting with 'TODO'. 1923 EXPECT_EQ("// long long long\n" 1924 "// long\n" 1925 "// TODO: long", 1926 format("// long long long long\n" 1927 "// TODO: long", 1928 getLLVMStyleWithColumns(20))); 1929 1930 // Don't reflow lines starting with 'FIXME'. 1931 EXPECT_EQ("// long long long\n" 1932 "// long\n" 1933 "// FIXME: long", 1934 format("// long long long long\n" 1935 "// FIXME: long", 1936 getLLVMStyleWithColumns(20))); 1937 1938 // Don't reflow lines starting with 'XXX'. 1939 EXPECT_EQ("// long long long\n" 1940 "// long\n" 1941 "// XXX: long", 1942 format("// long long long long\n" 1943 "// XXX: long", 1944 getLLVMStyleWithColumns(20))); 1945 1946 // Don't reflow comment pragmas. 1947 EXPECT_EQ("// long long long\n" 1948 "// long\n" 1949 "// IWYU pragma:", 1950 format("// long long long long\n" 1951 "// IWYU pragma:", 1952 getLLVMStyleWithColumns(20))); 1953 EXPECT_EQ("/* long long long\n" 1954 " * long\n" 1955 " * IWYU pragma:\n" 1956 " */", 1957 format("/* long long long long\n" 1958 " * IWYU pragma:\n" 1959 " */", 1960 getLLVMStyleWithColumns(20))); 1961 1962 // Reflow lines that have a non-punctuation character among their first 2 1963 // characters. 1964 EXPECT_EQ("// long long long\n" 1965 "// long 'long'", 1966 format("// long long long long\n" 1967 "// 'long'", 1968 getLLVMStyleWithColumns(20))); 1969 1970 // Don't reflow between separate blocks of comments. 1971 EXPECT_EQ("/* First comment\n" 1972 " * block will */\n" 1973 "/* Snd\n" 1974 " */\n", 1975 format("/* First comment block\n" 1976 " * will */\n" 1977 "/* Snd\n" 1978 " */\n", 1979 getLLVMStyleWithColumns(20))); 1980 1981 // Don't reflow lines having different indentation. 1982 EXPECT_EQ("// long long long\n" 1983 "// long\n" 1984 "// long", 1985 format("// long long long long\n" 1986 "// long", 1987 getLLVMStyleWithColumns(20))); 1988 1989 // Don't reflow separate bullets in list 1990 EXPECT_EQ("// - long long long\n" 1991 "// long\n" 1992 "// - long", 1993 format("// - long long long long\n" 1994 "// - long", 1995 getLLVMStyleWithColumns(20))); 1996 EXPECT_EQ("// * long long long\n" 1997 "// long\n" 1998 "// * long", 1999 format("// * long long long long\n" 2000 "// * long", 2001 getLLVMStyleWithColumns(20))); 2002 EXPECT_EQ("// + long long long\n" 2003 "// long\n" 2004 "// + long", 2005 format("// + long long long long\n" 2006 "// + long", 2007 getLLVMStyleWithColumns(20))); 2008 EXPECT_EQ("// 1. long long long\n" 2009 "// long\n" 2010 "// 2. long", 2011 format("// 1. long long long long\n" 2012 "// 2. 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 2021 EXPECT_EQ("// - long long long\n" 2022 "// long long long\n" 2023 "// - long", 2024 format("// - long long long long\n" 2025 "// long long\n" 2026 "// - long", 2027 getLLVMStyleWithColumns(20))); 2028 EXPECT_EQ("// - long long long\n" 2029 "// long long long\n" 2030 "// long\n" 2031 "// - long", 2032 format("// - long long long long\n" 2033 "// long long long\n" 2034 "// - long", 2035 getLLVMStyleWithColumns(20))); 2036 2037 // Large number (>2 digits) are not list items 2038 EXPECT_EQ("// long long long\n" 2039 "// long 1024. long.", 2040 format("// long long long long\n" 2041 "// 1024. long.", 2042 getLLVMStyleWithColumns(20))); 2043 2044 // Do not break before number, to avoid introducing a non-reflowable doxygen 2045 // list item. 2046 EXPECT_EQ("// long long\n" 2047 "// long 10. long.", 2048 format("// long long long 10.\n" 2049 "// long.", 2050 getLLVMStyleWithColumns(20))); 2051 2052 // Don't break or reflow after implicit string literals. 2053 verifyFormat("#include <t> // l l l\n" 2054 " // l", 2055 getLLVMStyleWithColumns(20)); 2056 2057 // Don't break or reflow comments on import lines. 2058 EXPECT_EQ("#include \"t\" /* l l l\n" 2059 " * l */", 2060 format("#include \"t\" /* l l l\n" 2061 " * l */", 2062 getLLVMStyleWithColumns(20))); 2063 2064 // Don't reflow between different trailing comment sections. 2065 EXPECT_EQ("int i; // long long\n" 2066 " // long\n" 2067 "int j; // long long\n" 2068 " // long\n", 2069 format("int i; // long long long\n" 2070 "int j; // long long long\n", 2071 getLLVMStyleWithColumns(20))); 2072 2073 // Don't reflow if the first word on the next line is longer than the 2074 // available space at current line. 2075 EXPECT_EQ("int i; // trigger\n" 2076 " // reflow\n" 2077 " // longsec\n", 2078 format("int i; // trigger reflow\n" 2079 " // longsec\n", 2080 getLLVMStyleWithColumns(20))); 2081 2082 // Simple case that correctly handles reflow in parameter lists. 2083 EXPECT_EQ("a = f(/* looooooooong\n" 2084 " * long long\n" 2085 " */\n" 2086 " a);", 2087 format("a = f(/* looooooooong long\n* long\n*/ a);", 2088 getLLVMStyleWithColumns(22))); 2089 // Tricky case that has fewer lines if we reflow the comment, ending up with 2090 // fewer lines. 2091 EXPECT_EQ("a = f(/* loooooong\n" 2092 " * long long\n" 2093 " */\n" 2094 " a);", 2095 format("a = f(/* loooooong long\n* long\n*/ a);", 2096 getLLVMStyleWithColumns(22))); 2097 2098 // Keep empty comment lines. 2099 EXPECT_EQ("/**/", format(" /**/", getLLVMStyleWithColumns(20))); 2100 EXPECT_EQ("/* */", format(" /* */", getLLVMStyleWithColumns(20))); 2101 EXPECT_EQ("/* */", format(" /* */", getLLVMStyleWithColumns(20))); 2102 EXPECT_EQ("//", format(" // ", getLLVMStyleWithColumns(20))); 2103 EXPECT_EQ("///", format(" /// ", getLLVMStyleWithColumns(20))); 2104 } 2105 2106 TEST_F(FormatTestComments, ReflowsCommentsPrecise) { 2107 // FIXME: This assumes we do not continue compressing whitespace once we are 2108 // in reflow mode. Consider compressing whitespace. 2109 2110 // Test that we stop reflowing precisely at the column limit. 2111 // After reflowing, "// reflows into foo" does not fit the column limit, 2112 // so we compress the whitespace. 2113 EXPECT_EQ("// some text that\n" 2114 "// reflows into foo\n", 2115 format("// some text that reflows\n" 2116 "// into foo\n", 2117 getLLVMStyleWithColumns(20))); 2118 // Given one more column, "// reflows into foo" does fit the limit, so we 2119 // do not compress the whitespace. 2120 EXPECT_EQ("// some text that\n" 2121 "// reflows into foo\n", 2122 format("// some text that reflows\n" 2123 "// into foo\n", 2124 getLLVMStyleWithColumns(21))); 2125 2126 // Make sure that we correctly account for the space added in the reflow case 2127 // when making the reflowing decision. 2128 // First, when the next line ends precisely one column over the limit, do not 2129 // reflow. 2130 EXPECT_EQ("// some text that\n" 2131 "// reflows\n" 2132 "// into1234567\n", 2133 format("// some text that reflows\n" 2134 "// into1234567\n", 2135 getLLVMStyleWithColumns(21))); 2136 // Secondly, when the next line ends later, but the first word in that line 2137 // is precisely one column over the limit, do not reflow. 2138 EXPECT_EQ("// some text that\n" 2139 "// reflows\n" 2140 "// into1234567 f\n", 2141 format("// some text that reflows\n" 2142 "// into1234567 f\n", 2143 getLLVMStyleWithColumns(21))); 2144 } 2145 2146 TEST_F(FormatTestComments, ReflowsCommentsWithExtraWhitespace) { 2147 // Baseline. 2148 EXPECT_EQ("// some text\n" 2149 "// that re flows\n", 2150 format("// some text that\n" 2151 "// re flows\n", 2152 getLLVMStyleWithColumns(16))); 2153 EXPECT_EQ("// some text\n" 2154 "// that re flows\n", 2155 format("// some text that\n" 2156 "// re flows\n", 2157 getLLVMStyleWithColumns(16))); 2158 EXPECT_EQ("/* some text\n" 2159 " * that re flows\n" 2160 " */\n", 2161 format("/* some text that\n" 2162 "* re flows\n" 2163 "*/\n", 2164 getLLVMStyleWithColumns(16))); 2165 // FIXME: We do not reflow if the indent of two subsequent lines differs; 2166 // given that this is different behavior from block comments, do we want 2167 // to keep this? 2168 EXPECT_EQ("// some text\n" 2169 "// that\n" 2170 "// re flows\n", 2171 format("// some text that\n" 2172 "// re flows\n", 2173 getLLVMStyleWithColumns(16))); 2174 // Space within parts of a line that fit. 2175 // FIXME: Use the earliest possible split while reflowing to compress the 2176 // whitespace within the line. 2177 EXPECT_EQ("// some text that\n" 2178 "// does re flow\n" 2179 "// more here\n", 2180 format("// some text that does\n" 2181 "// re flow more here\n", 2182 getLLVMStyleWithColumns(21))); 2183 } 2184 2185 TEST_F(FormatTestComments, IgnoresIf0Contents) { 2186 EXPECT_EQ("#if 0\n" 2187 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n" 2188 "#endif\n" 2189 "void f() {}", 2190 format("#if 0\n" 2191 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n" 2192 "#endif\n" 2193 "void f( ) { }")); 2194 EXPECT_EQ("#if false\n" 2195 "void f( ) { }\n" 2196 "#endif\n" 2197 "void g() {}\n", 2198 format("#if false\n" 2199 "void f( ) { }\n" 2200 "#endif\n" 2201 "void g( ) { }\n")); 2202 EXPECT_EQ("enum E {\n" 2203 " One,\n" 2204 " Two,\n" 2205 "#if 0\n" 2206 "Three,\n" 2207 " Four,\n" 2208 "#endif\n" 2209 " Five\n" 2210 "};", 2211 format("enum E {\n" 2212 " One,Two,\n" 2213 "#if 0\n" 2214 "Three,\n" 2215 " Four,\n" 2216 "#endif\n" 2217 " Five};")); 2218 EXPECT_EQ("enum F {\n" 2219 " One,\n" 2220 "#if 1\n" 2221 " Two,\n" 2222 "#if 0\n" 2223 "Three,\n" 2224 " Four,\n" 2225 "#endif\n" 2226 " Five\n" 2227 "#endif\n" 2228 "};", 2229 format("enum F {\n" 2230 "One,\n" 2231 "#if 1\n" 2232 "Two,\n" 2233 "#if 0\n" 2234 "Three,\n" 2235 " Four,\n" 2236 "#endif\n" 2237 "Five\n" 2238 "#endif\n" 2239 "};")); 2240 EXPECT_EQ("enum G {\n" 2241 " One,\n" 2242 "#if 0\n" 2243 "Two,\n" 2244 "#else\n" 2245 " Three,\n" 2246 "#endif\n" 2247 " Four\n" 2248 "};", 2249 format("enum G {\n" 2250 "One,\n" 2251 "#if 0\n" 2252 "Two,\n" 2253 "#else\n" 2254 "Three,\n" 2255 "#endif\n" 2256 "Four\n" 2257 "};")); 2258 EXPECT_EQ("enum H {\n" 2259 " One,\n" 2260 "#if 0\n" 2261 "#ifdef Q\n" 2262 "Two,\n" 2263 "#else\n" 2264 "Three,\n" 2265 "#endif\n" 2266 "#endif\n" 2267 " Four\n" 2268 "};", 2269 format("enum H {\n" 2270 "One,\n" 2271 "#if 0\n" 2272 "#ifdef Q\n" 2273 "Two,\n" 2274 "#else\n" 2275 "Three,\n" 2276 "#endif\n" 2277 "#endif\n" 2278 "Four\n" 2279 "};")); 2280 EXPECT_EQ("enum I {\n" 2281 " One,\n" 2282 "#if /* test */ 0 || 1\n" 2283 "Two,\n" 2284 "Three,\n" 2285 "#endif\n" 2286 " Four\n" 2287 "};", 2288 format("enum I {\n" 2289 "One,\n" 2290 "#if /* test */ 0 || 1\n" 2291 "Two,\n" 2292 "Three,\n" 2293 "#endif\n" 2294 "Four\n" 2295 "};")); 2296 EXPECT_EQ("enum J {\n" 2297 " One,\n" 2298 "#if 0\n" 2299 "#if 0\n" 2300 "Two,\n" 2301 "#else\n" 2302 "Three,\n" 2303 "#endif\n" 2304 "Four,\n" 2305 "#endif\n" 2306 " Five\n" 2307 "};", 2308 format("enum J {\n" 2309 "One,\n" 2310 "#if 0\n" 2311 "#if 0\n" 2312 "Two,\n" 2313 "#else\n" 2314 "Three,\n" 2315 "#endif\n" 2316 "Four,\n" 2317 "#endif\n" 2318 "Five\n" 2319 "};")); 2320 2321 // Ignore stuff in SWIG-blocks. 2322 EXPECT_EQ("#ifdef SWIG\n" 2323 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n" 2324 "#endif\n" 2325 "void f() {}", 2326 format("#ifdef SWIG\n" 2327 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n" 2328 "#endif\n" 2329 "void f( ) { }")); 2330 EXPECT_EQ("#ifndef SWIG\n" 2331 "void f() {}\n" 2332 "#endif", 2333 format("#ifndef SWIG\n" 2334 "void f( ) { }\n" 2335 "#endif")); 2336 } 2337 2338 TEST_F(FormatTestComments, DontCrashOnBlockComments) { 2339 EXPECT_EQ( 2340 "int xxxxxxxxx; /* " 2341 "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\n" 2342 "zzzzzz\n" 2343 "0*/", 2344 format("int xxxxxxxxx; /* " 2345 "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy zzzzzz\n" 2346 "0*/")); 2347 } 2348 2349 TEST_F(FormatTestComments, BlockCommentsInControlLoops) { 2350 verifyFormat("if (0) /* a comment in a strange place */ {\n" 2351 " f();\n" 2352 "}"); 2353 verifyFormat("if (0) /* a comment in a strange place */ {\n" 2354 " f();\n" 2355 "} /* another comment */ else /* comment #3 */ {\n" 2356 " g();\n" 2357 "}"); 2358 verifyFormat("while (0) /* a comment in a strange place */ {\n" 2359 " f();\n" 2360 "}"); 2361 verifyFormat("for (;;) /* a comment in a strange place */ {\n" 2362 " f();\n" 2363 "}"); 2364 verifyFormat("do /* a comment in a strange place */ {\n" 2365 " f();\n" 2366 "} /* another comment */ while (0);"); 2367 } 2368 2369 TEST_F(FormatTestComments, BlockComments) { 2370 EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */", 2371 format("/* *//* */ /* */\n/* *//* */ /* */")); 2372 EXPECT_EQ("/* */ a /* */ b;", format(" /* */ a/* */ b;")); 2373 EXPECT_EQ("#define A /*123*/ \\\n" 2374 " b\n" 2375 "/* */\n" 2376 "someCall(\n" 2377 " parameter);", 2378 format("#define A /*123*/ b\n" 2379 "/* */\n" 2380 "someCall(parameter);", 2381 getLLVMStyleWithColumns(15))); 2382 2383 EXPECT_EQ("#define A\n" 2384 "/* */ someCall(\n" 2385 " parameter);", 2386 format("#define A\n" 2387 "/* */someCall(parameter);", 2388 getLLVMStyleWithColumns(15))); 2389 EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/")); 2390 EXPECT_EQ("/*\n" 2391 " *\n" 2392 " * aaaaaa\n" 2393 " * aaaaaa\n" 2394 " */", 2395 format("/*\n" 2396 "*\n" 2397 " * aaaaaa aaaaaa\n" 2398 "*/", 2399 getLLVMStyleWithColumns(10))); 2400 EXPECT_EQ("/*\n" 2401 "**\n" 2402 "* aaaaaa\n" 2403 "*aaaaaa\n" 2404 "*/", 2405 format("/*\n" 2406 "**\n" 2407 "* aaaaaa aaaaaa\n" 2408 "*/", 2409 getLLVMStyleWithColumns(10))); 2410 EXPECT_EQ("int aaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 2411 " /* line 1\n" 2412 " bbbbbbbbbbbb */\n" 2413 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 2414 format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 2415 " /* line 1\n" 2416 " bbbbbbbbbbbb */ bbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 2417 getLLVMStyleWithColumns(50))); 2418 2419 FormatStyle NoBinPacking = getLLVMStyle(); 2420 NoBinPacking.BinPackParameters = false; 2421 EXPECT_EQ("someFunction(1, /* comment 1 */\n" 2422 " 2, /* comment 2 */\n" 2423 " 3, /* comment 3 */\n" 2424 " aaaa,\n" 2425 " bbbb);", 2426 format("someFunction (1, /* comment 1 */\n" 2427 " 2, /* comment 2 */ \n" 2428 " 3, /* comment 3 */\n" 2429 "aaaa, bbbb );", 2430 NoBinPacking)); 2431 verifyFormat( 2432 "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2433 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 2434 EXPECT_EQ( 2435 "bool aaaaaaaaaaaaa = /* trailing comment */\n" 2436 " aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2437 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;", 2438 format( 2439 "bool aaaaaaaaaaaaa = /* trailing comment */\n" 2440 " aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2441 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;")); 2442 EXPECT_EQ( 2443 "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n" 2444 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n" 2445 "int cccccccccccccccccccccccccccccc; /* comment */\n", 2446 format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n" 2447 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n" 2448 "int cccccccccccccccccccccccccccccc; /* comment */\n")); 2449 2450 verifyFormat("void f(int * /* unused */) {}"); 2451 2452 EXPECT_EQ("/*\n" 2453 " **\n" 2454 " */", 2455 format("/*\n" 2456 " **\n" 2457 " */")); 2458 EXPECT_EQ("/*\n" 2459 " *q\n" 2460 " */", 2461 format("/*\n" 2462 " *q\n" 2463 " */")); 2464 EXPECT_EQ("/*\n" 2465 " * q\n" 2466 " */", 2467 format("/*\n" 2468 " * q\n" 2469 " */")); 2470 EXPECT_EQ("/*\n" 2471 " **/", 2472 format("/*\n" 2473 " **/")); 2474 EXPECT_EQ("/*\n" 2475 " ***/", 2476 format("/*\n" 2477 " ***/")); 2478 } 2479 2480 TEST_F(FormatTestComments, BlockCommentsInMacros) { 2481 EXPECT_EQ("#define A \\\n" 2482 " { \\\n" 2483 " /* one line */ \\\n" 2484 " someCall();", 2485 format("#define A { \\\n" 2486 " /* one line */ \\\n" 2487 " someCall();", 2488 getLLVMStyleWithColumns(20))); 2489 EXPECT_EQ("#define A \\\n" 2490 " { \\\n" 2491 " /* previous */ \\\n" 2492 " /* one line */ \\\n" 2493 " someCall();", 2494 format("#define A { \\\n" 2495 " /* previous */ \\\n" 2496 " /* one line */ \\\n" 2497 " someCall();", 2498 getLLVMStyleWithColumns(20))); 2499 } 2500 2501 TEST_F(FormatTestComments, BlockCommentsAtEndOfLine) { 2502 EXPECT_EQ("a = {\n" 2503 " 1111 /* */\n" 2504 "};", 2505 format("a = {1111 /* */\n" 2506 "};", 2507 getLLVMStyleWithColumns(15))); 2508 EXPECT_EQ("a = {\n" 2509 " 1111 /* */\n" 2510 "};", 2511 format("a = {1111 /* */\n" 2512 "};", 2513 getLLVMStyleWithColumns(15))); 2514 EXPECT_EQ("a = {\n" 2515 " 1111 /* a\n" 2516 " */\n" 2517 "};", 2518 format("a = {1111 /* a */\n" 2519 "};", 2520 getLLVMStyleWithColumns(15))); 2521 } 2522 2523 TEST_F(FormatTestComments, BreaksAfterMultilineBlockCommentsInParamLists) { 2524 EXPECT_EQ("a = f(/* long\n" 2525 " long */\n" 2526 " a);", 2527 format("a = f(/* long long */ a);", getLLVMStyleWithColumns(16))); 2528 EXPECT_EQ("a = f(\n" 2529 " /* long\n" 2530 " long */\n" 2531 " a);", 2532 format("a = f(/* long long */ a);", getLLVMStyleWithColumns(15))); 2533 2534 EXPECT_EQ("a = f(/* long\n" 2535 " long\n" 2536 " */\n" 2537 " a);", 2538 format("a = f(/* long\n" 2539 " long\n" 2540 " */a);", 2541 getLLVMStyleWithColumns(16))); 2542 2543 EXPECT_EQ("a = f(/* long\n" 2544 " long\n" 2545 " */\n" 2546 " a);", 2547 format("a = f(/* long\n" 2548 " long\n" 2549 " */ a);", 2550 getLLVMStyleWithColumns(16))); 2551 2552 EXPECT_EQ("a = f(/* long\n" 2553 " long\n" 2554 " */\n" 2555 " (1 + 1));", 2556 format("a = f(/* long\n" 2557 " long\n" 2558 " */ (1 + 1));", 2559 getLLVMStyleWithColumns(16))); 2560 2561 EXPECT_EQ( 2562 "a = f(a,\n" 2563 " /* long\n" 2564 " long */\n" 2565 " b);", 2566 format("a = f(a, /* long long */ b);", getLLVMStyleWithColumns(16))); 2567 2568 EXPECT_EQ( 2569 "a = f(\n" 2570 " a,\n" 2571 " /* long\n" 2572 " long */\n" 2573 " b);", 2574 format("a = f(a, /* long long */ b);", getLLVMStyleWithColumns(15))); 2575 2576 EXPECT_EQ("a = f(a,\n" 2577 " /* long\n" 2578 " long */\n" 2579 " (1 + 1));", 2580 format("a = f(a, /* long long */ (1 + 1));", 2581 getLLVMStyleWithColumns(16))); 2582 EXPECT_EQ("a = f(\n" 2583 " a,\n" 2584 " /* long\n" 2585 " long */\n" 2586 " (1 + 1));", 2587 format("a = f(a, /* long long */ (1 + 1));", 2588 getLLVMStyleWithColumns(15))); 2589 } 2590 2591 TEST_F(FormatTestComments, IndentLineCommentsInStartOfBlockAtEndOfFile) { 2592 verifyFormat("{\n" 2593 " // a\n" 2594 " // b"); 2595 } 2596 2597 TEST_F(FormatTestComments, AlignTrailingComments) { 2598 EXPECT_EQ("#define MACRO(V) \\\n" 2599 " V(Rt2) /* one more char */ \\\n" 2600 " V(Rs) /* than here */ \\\n" 2601 "/* comment 3 */\n", 2602 format("#define MACRO(V)\\\n" 2603 "V(Rt2) /* one more char */ \\\n" 2604 "V(Rs) /* than here */ \\\n" 2605 "/* comment 3 */\n", 2606 getLLVMStyleWithColumns(40))); 2607 EXPECT_EQ("int i = f(abc, // line 1\n" 2608 " d, // line 2\n" 2609 " // line 3\n" 2610 " b);", 2611 format("int i = f(abc, // line 1\n" 2612 " d, // line 2\n" 2613 " // line 3\n" 2614 " b);", 2615 getLLVMStyleWithColumns(40))); 2616 2617 // Align newly broken trailing comments. 2618 EXPECT_EQ("int ab; // line\n" 2619 "int a; // long\n" 2620 " // long\n", 2621 format("int ab; // line\n" 2622 "int a; // long long\n", 2623 getLLVMStyleWithColumns(15))); 2624 EXPECT_EQ("int ab; // line\n" 2625 "int a; // long\n" 2626 " // long\n" 2627 " // long", 2628 format("int ab; // line\n" 2629 "int a; // long long\n" 2630 " // long", 2631 getLLVMStyleWithColumns(15))); 2632 EXPECT_EQ("int ab; // line\n" 2633 "int a; // long\n" 2634 " // long\n" 2635 "pt c; // long", 2636 format("int ab; // line\n" 2637 "int a; // long long\n" 2638 "pt c; // long", 2639 getLLVMStyleWithColumns(15))); 2640 EXPECT_EQ("int ab; // line\n" 2641 "int a; // long\n" 2642 " // long\n" 2643 "\n" 2644 "// long", 2645 format("int ab; // line\n" 2646 "int a; // long long\n" 2647 "\n" 2648 "// long", 2649 getLLVMStyleWithColumns(15))); 2650 2651 // Don't align newly broken trailing comments if that would put them over the 2652 // column limit. 2653 EXPECT_EQ("int i, j; // line 1\n" 2654 "int k; // line longg\n" 2655 " // long", 2656 format("int i, j; // line 1\n" 2657 "int k; // line longg long", 2658 getLLVMStyleWithColumns(20))); 2659 2660 // Always align if ColumnLimit = 0 2661 EXPECT_EQ("int i, j; // line 1\n" 2662 "int k; // line longg long", 2663 format("int i, j; // line 1\n" 2664 "int k; // line longg long", 2665 getLLVMStyleWithColumns(0))); 2666 2667 // Align comment line sections aligned with the next token with the next 2668 // token. 2669 EXPECT_EQ("class A {\n" 2670 "public: // public comment\n" 2671 " // comment about a\n" 2672 " int a;\n" 2673 "};", 2674 format("class A {\n" 2675 "public: // public comment\n" 2676 " // comment about a\n" 2677 " int a;\n" 2678 "};", 2679 getLLVMStyleWithColumns(40))); 2680 EXPECT_EQ("class A {\n" 2681 "public: // public comment 1\n" 2682 " // public comment 2\n" 2683 " // comment 1 about a\n" 2684 " // comment 2 about a\n" 2685 " int a;\n" 2686 "};", 2687 format("class A {\n" 2688 "public: // public comment 1\n" 2689 " // public comment 2\n" 2690 " // comment 1 about a\n" 2691 " // comment 2 about a\n" 2692 " int a;\n" 2693 "};", 2694 getLLVMStyleWithColumns(40))); 2695 EXPECT_EQ("int f(int n) { // comment line 1 on f\n" 2696 " // comment line 2 on f\n" 2697 " // comment line 1 before return\n" 2698 " // comment line 2 before return\n" 2699 " return n; // comment line 1 on return\n" 2700 " // comment line 2 on return\n" 2701 " // comment line 1 after return\n" 2702 "}", 2703 format("int f(int n) { // comment line 1 on f\n" 2704 " // comment line 2 on f\n" 2705 " // comment line 1 before return\n" 2706 " // comment line 2 before return\n" 2707 " return n; // comment line 1 on return\n" 2708 " // comment line 2 on return\n" 2709 " // comment line 1 after return\n" 2710 "}", 2711 getLLVMStyleWithColumns(40))); 2712 EXPECT_EQ("int f(int n) {\n" 2713 " switch (n) { // comment line 1 on switch\n" 2714 " // comment line 2 on switch\n" 2715 " // comment line 1 before case 1\n" 2716 " // comment line 2 before case 1\n" 2717 " case 1: // comment line 1 on case 1\n" 2718 " // comment line 2 on case 1\n" 2719 " // comment line 1 before return 1\n" 2720 " // comment line 2 before return 1\n" 2721 " return 1; // comment line 1 on return 1\n" 2722 " // comment line 2 on return 1\n" 2723 " // comment line 1 before default\n" 2724 " // comment line 2 before default\n" 2725 " default: // comment line 1 on default\n" 2726 " // comment line 2 on default\n" 2727 " // comment line 1 before return 2\n" 2728 " return 2 * f(n - 1); // comment line 1 on return 2\n" 2729 " // comment line 2 on return 2\n" 2730 " // comment line 1 after return\n" 2731 " // comment line 2 after return\n" 2732 " }\n" 2733 "}", 2734 format("int f(int n) {\n" 2735 " switch (n) { // comment line 1 on switch\n" 2736 " // comment line 2 on switch\n" 2737 " // comment line 1 before case 1\n" 2738 " // comment line 2 before case 1\n" 2739 " case 1: // comment line 1 on case 1\n" 2740 " // comment line 2 on case 1\n" 2741 " // comment line 1 before return 1\n" 2742 " // comment line 2 before return 1\n" 2743 " return 1; // comment line 1 on return 1\n" 2744 " // comment line 2 on return 1\n" 2745 " // comment line 1 before default\n" 2746 " // comment line 2 before default\n" 2747 " default: // comment line 1 on default\n" 2748 " // comment line 2 on default\n" 2749 " // comment line 1 before return 2\n" 2750 " return 2 * f(n - 1); // comment line 1 on return 2\n" 2751 " // comment line 2 on return 2\n" 2752 " // comment line 1 after return\n" 2753 " // comment line 2 after return\n" 2754 " }\n" 2755 "}", 2756 getLLVMStyleWithColumns(80))); 2757 2758 // If all the lines in a sequence of line comments are aligned with the next 2759 // token, the first line belongs to the previous token and the other lines 2760 // belong to the next token. 2761 EXPECT_EQ("int a; // line about a\n" 2762 "long b;", 2763 format("int a; // line about a\n" 2764 " long b;", 2765 getLLVMStyleWithColumns(80))); 2766 EXPECT_EQ("int a; // line about a\n" 2767 "// line about b\n" 2768 "long b;", 2769 format("int a; // line about a\n" 2770 " // line about b\n" 2771 " long b;", 2772 getLLVMStyleWithColumns(80))); 2773 EXPECT_EQ("int a; // line about a\n" 2774 "// line 1 about b\n" 2775 "// line 2 about b\n" 2776 "long b;", 2777 format("int a; // line about a\n" 2778 " // line 1 about b\n" 2779 " // line 2 about b\n" 2780 " long b;", 2781 getLLVMStyleWithColumns(80))); 2782 2783 // Checks an edge case in preprocessor handling. 2784 // These comments should *not* be aligned 2785 EXPECT_NE( // change for EQ when fixed 2786 "#if FOO\n" 2787 "#else\n" 2788 "long a; // Line about a\n" 2789 "#endif\n" 2790 "#if BAR\n" 2791 "#else\n" 2792 "long b_long_name; // Line about b\n" 2793 "#endif\n", 2794 format("#if FOO\n" 2795 "#else\n" 2796 "long a; // Line about a\n" // Previous (bad) behavior 2797 "#endif\n" 2798 "#if BAR\n" 2799 "#else\n" 2800 "long b_long_name; // Line about b\n" 2801 "#endif\n", 2802 getLLVMStyleWithColumns(80))); 2803 2804 // bug 47589 2805 EXPECT_EQ( 2806 "namespace m {\n\n" 2807 "#define FOO_GLOBAL 0 // Global scope.\n" 2808 "#define FOO_LINKLOCAL 1 // Link-local scope.\n" 2809 "#define FOO_SITELOCAL 2 // Site-local scope (deprecated).\n" 2810 "#define FOO_UNIQUELOCAL 3 // Unique local\n" 2811 "#define FOO_NODELOCAL 4 // Loopback\n\n" 2812 "} // namespace m\n", 2813 format("namespace m {\n\n" 2814 "#define FOO_GLOBAL 0 // Global scope.\n" 2815 "#define FOO_LINKLOCAL 1 // Link-local scope.\n" 2816 "#define FOO_SITELOCAL 2 // Site-local scope (deprecated).\n" 2817 "#define FOO_UNIQUELOCAL 3 // Unique local\n" 2818 "#define FOO_NODELOCAL 4 // Loopback\n\n" 2819 "} // namespace m\n", 2820 getLLVMStyleWithColumns(80))); 2821 } 2822 2823 TEST_F(FormatTestComments, AlignsBlockCommentDecorations) { 2824 EXPECT_EQ("/*\n" 2825 " */", 2826 format("/*\n" 2827 "*/", 2828 getLLVMStyle())); 2829 EXPECT_EQ("/*\n" 2830 " */", 2831 format("/*\n" 2832 " */", 2833 getLLVMStyle())); 2834 EXPECT_EQ("/*\n" 2835 " */", 2836 format("/*\n" 2837 " */", 2838 getLLVMStyle())); 2839 2840 // Align a single line. 2841 EXPECT_EQ("/*\n" 2842 " * line */", 2843 format("/*\n" 2844 "* line */", 2845 getLLVMStyle())); 2846 EXPECT_EQ("/*\n" 2847 " * line */", 2848 format("/*\n" 2849 " * line */", 2850 getLLVMStyle())); 2851 EXPECT_EQ("/*\n" 2852 " * line */", 2853 format("/*\n" 2854 " * line */", 2855 getLLVMStyle())); 2856 EXPECT_EQ("/*\n" 2857 " * line */", 2858 format("/*\n" 2859 " * line */", 2860 getLLVMStyle())); 2861 EXPECT_EQ("/**\n" 2862 " * line */", 2863 format("/**\n" 2864 "* line */", 2865 getLLVMStyle())); 2866 EXPECT_EQ("/**\n" 2867 " * line */", 2868 format("/**\n" 2869 " * line */", 2870 getLLVMStyle())); 2871 EXPECT_EQ("/**\n" 2872 " * line */", 2873 format("/**\n" 2874 " * line */", 2875 getLLVMStyle())); 2876 EXPECT_EQ("/**\n" 2877 " * line */", 2878 format("/**\n" 2879 " * line */", 2880 getLLVMStyle())); 2881 EXPECT_EQ("/**\n" 2882 " * line */", 2883 format("/**\n" 2884 " * line */", 2885 getLLVMStyle())); 2886 2887 // Align the end '*/' after a line. 2888 EXPECT_EQ("/*\n" 2889 " * line\n" 2890 " */", 2891 format("/*\n" 2892 "* line\n" 2893 "*/", 2894 getLLVMStyle())); 2895 EXPECT_EQ("/*\n" 2896 " * line\n" 2897 " */", 2898 format("/*\n" 2899 " * line\n" 2900 " */", 2901 getLLVMStyle())); 2902 EXPECT_EQ("/*\n" 2903 " * line\n" 2904 " */", 2905 format("/*\n" 2906 " * line\n" 2907 " */", 2908 getLLVMStyle())); 2909 2910 // Align two lines. 2911 EXPECT_EQ("/* line 1\n" 2912 " * line 2 */", 2913 format("/* line 1\n" 2914 " * line 2 */", 2915 getLLVMStyle())); 2916 EXPECT_EQ("/* line 1\n" 2917 " * line 2 */", 2918 format("/* line 1\n" 2919 "* line 2 */", 2920 getLLVMStyle())); 2921 EXPECT_EQ("/* line 1\n" 2922 " * line 2 */", 2923 format("/* line 1\n" 2924 " * line 2 */", 2925 getLLVMStyle())); 2926 EXPECT_EQ("/* line 1\n" 2927 " * line 2 */", 2928 format("/* line 1\n" 2929 " * line 2 */", 2930 getLLVMStyle())); 2931 EXPECT_EQ("/* line 1\n" 2932 " * line 2 */", 2933 format("/* line 1\n" 2934 " * line 2 */", 2935 getLLVMStyle())); 2936 EXPECT_EQ("int i; /* line 1\n" 2937 " * line 2 */", 2938 format("int i; /* line 1\n" 2939 "* line 2 */", 2940 getLLVMStyle())); 2941 EXPECT_EQ("int i; /* line 1\n" 2942 " * line 2 */", 2943 format("int i; /* line 1\n" 2944 " * line 2 */", 2945 getLLVMStyle())); 2946 EXPECT_EQ("int i; /* line 1\n" 2947 " * line 2 */", 2948 format("int i; /* line 1\n" 2949 " * line 2 */", 2950 getLLVMStyle())); 2951 2952 // Align several lines. 2953 EXPECT_EQ("/* line 1\n" 2954 " * line 2\n" 2955 " * line 3 */", 2956 format("/* line 1\n" 2957 " * line 2\n" 2958 "* line 3 */", 2959 getLLVMStyle())); 2960 EXPECT_EQ("/* line 1\n" 2961 " * line 2\n" 2962 " * line 3 */", 2963 format("/* line 1\n" 2964 " * line 2\n" 2965 "* line 3 */", 2966 getLLVMStyle())); 2967 EXPECT_EQ("/*\n" 2968 "** line 1\n" 2969 "** line 2\n" 2970 "*/", 2971 format("/*\n" 2972 "** line 1\n" 2973 " ** line 2\n" 2974 "*/", 2975 getLLVMStyle())); 2976 2977 // Align with different indent after the decorations. 2978 EXPECT_EQ("/*\n" 2979 " * line 1\n" 2980 " * line 2\n" 2981 " * line 3\n" 2982 " * line 4\n" 2983 " */", 2984 format("/*\n" 2985 "* line 1\n" 2986 " * line 2\n" 2987 " * line 3\n" 2988 "* line 4\n" 2989 "*/", 2990 getLLVMStyle())); 2991 2992 // Align empty or blank lines. 2993 EXPECT_EQ("/**\n" 2994 " *\n" 2995 " *\n" 2996 " *\n" 2997 " */", 2998 format("/**\n" 2999 "* \n" 3000 " * \n" 3001 " *\n" 3002 "*/", 3003 getLLVMStyle())); 3004 3005 // Align while breaking and reflowing. 3006 EXPECT_EQ("/*\n" 3007 " * long long long\n" 3008 " * long long\n" 3009 " *\n" 3010 " * long */", 3011 format("/*\n" 3012 " * long long long long\n" 3013 " * long\n" 3014 " *\n" 3015 "* long */", 3016 getLLVMStyleWithColumns(20))); 3017 } 3018 3019 TEST_F(FormatTestComments, NoCrash_Bug34236) { 3020 // This is a test case from a crasher reported in: 3021 // https://bugs.llvm.org/show_bug.cgi?id=34236 3022 // Temporarily disable formatting for readability. 3023 // clang-format off 3024 EXPECT_EQ( 3025 "/* */ /*\n" 3026 " * a\n" 3027 " * b c d*/", 3028 format( 3029 "/* */ /*\n" 3030 " * a b\n" 3031 " * c d*/", 3032 getLLVMStyleWithColumns(80))); 3033 // clang-format on 3034 } 3035 3036 TEST_F(FormatTestComments, NonTrailingBlockComments) { 3037 verifyFormat("const /** comment comment */ A = B;", 3038 getLLVMStyleWithColumns(40)); 3039 3040 verifyFormat("const /** comment comment comment */ A =\n" 3041 " B;", 3042 getLLVMStyleWithColumns(40)); 3043 3044 EXPECT_EQ("const /** comment comment comment\n" 3045 " comment */\n" 3046 " A = B;", 3047 format("const /** comment comment comment comment */\n" 3048 " A = B;", 3049 getLLVMStyleWithColumns(40))); 3050 } 3051 3052 TEST_F(FormatTestComments, PythonStyleComments) { 3053 // Keeps a space after '#'. 3054 EXPECT_EQ("# comment\n" 3055 "key: value", 3056 format("#comment\n" 3057 "key:value", 3058 getTextProtoStyleWithColumns(20))); 3059 EXPECT_EQ("# comment\n" 3060 "key: value", 3061 format("# comment\n" 3062 "key:value", 3063 getTextProtoStyleWithColumns(20))); 3064 // Breaks long comment. 3065 EXPECT_EQ("# comment comment\n" 3066 "# comment\n" 3067 "key: value", 3068 format("# comment comment comment\n" 3069 "key:value", 3070 getTextProtoStyleWithColumns(20))); 3071 // Indents comments. 3072 EXPECT_EQ("data {\n" 3073 " # comment comment\n" 3074 " # comment\n" 3075 " key: value\n" 3076 "}", 3077 format("data {\n" 3078 "# comment comment comment\n" 3079 "key: value}", 3080 getTextProtoStyleWithColumns(20))); 3081 EXPECT_EQ("data {\n" 3082 " # comment comment\n" 3083 " # comment\n" 3084 " key: value\n" 3085 "}", 3086 format("data {# comment comment comment\n" 3087 "key: value}", 3088 getTextProtoStyleWithColumns(20))); 3089 // Reflows long comments. 3090 EXPECT_EQ("# comment comment\n" 3091 "# comment comment\n" 3092 "key: value", 3093 format("# comment comment comment\n" 3094 "# comment\n" 3095 "key:value", 3096 getTextProtoStyleWithColumns(20))); 3097 // Breaks trailing comments. 3098 EXPECT_EQ("k: val # comment\n" 3099 " # comment\n" 3100 "a: 1", 3101 format("k:val#comment comment\n" 3102 "a:1", 3103 getTextProtoStyleWithColumns(20))); 3104 EXPECT_EQ("id {\n" 3105 " k: val # comment\n" 3106 " # comment\n" 3107 " # line line\n" 3108 " a: 1\n" 3109 "}", 3110 format("id {k:val#comment comment\n" 3111 "# line line\n" 3112 "a:1}", 3113 getTextProtoStyleWithColumns(20))); 3114 // Aligns trailing comments. 3115 EXPECT_EQ("k: val # commen1\n" 3116 " # commen2\n" 3117 " # commen3\n" 3118 "# commen4\n" 3119 "a: 1 # commen5\n" 3120 " # commen6\n" 3121 " # commen7", 3122 format("k:val#commen1 commen2\n" 3123 " # commen3\n" 3124 "# commen4\n" 3125 "a:1#commen5 commen6\n" 3126 " #commen7", 3127 getTextProtoStyleWithColumns(20))); 3128 } 3129 3130 TEST_F(FormatTestComments, BreaksBeforeTrailingUnbreakableSequence) { 3131 // The end of /* trail */ is exactly at 80 columns, but the unbreakable 3132 // trailing sequence ); after it exceeds the column limit. Make sure we 3133 // correctly break the line in that case. 3134 verifyFormat("int a =\n" 3135 " foo(/* trail */);", 3136 getLLVMStyleWithColumns(23)); 3137 } 3138 3139 TEST_F(FormatTestComments, ReflowBackslashCrash) { 3140 // clang-format off 3141 EXPECT_EQ( 3142 "// How to run:\n" 3143 "// bbbbb run \\\n" 3144 "// rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr\n" 3145 "// \\ <log_file> -- --output_directory=\"<output_directory>\"", 3146 format( 3147 "// How to run:\n" 3148 "// bbbbb run \\\n" 3149 "// rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr \\\n" 3150 "// <log_file> -- --output_directory=\"<output_directory>\"")); 3151 // clang-format on 3152 } 3153 3154 TEST_F(FormatTestComments, IndentsLongJavadocAnnotatedLines) { 3155 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Java); 3156 Style.ColumnLimit = 60; 3157 FormatStyle Style20 = getGoogleStyle(FormatStyle::LK_Java); 3158 Style20.ColumnLimit = 20; 3159 EXPECT_EQ( 3160 "/**\n" 3161 " * @param x long long long long long long long long long\n" 3162 " * long\n" 3163 " */\n", 3164 format("/**\n" 3165 " * @param x long long long long long long long long long long\n" 3166 " */\n", 3167 Style)); 3168 EXPECT_EQ("/**\n" 3169 " * @param x long long long long long long long long long\n" 3170 " * long long long long long long long long long long\n" 3171 " */\n", 3172 format("/**\n" 3173 " * @param x long long long long long long long long long " 3174 "long long long long long long long long long long\n" 3175 " */\n", 3176 Style)); 3177 EXPECT_EQ("/**\n" 3178 " * @param x long long long long long long long long long\n" 3179 " * long 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 " 3184 "long long long long long long long long long long long\n" 3185 " */\n", 3186 Style)); 3187 EXPECT_EQ("/**\n" 3188 " * Sentence that\n" 3189 " * should be broken.\n" 3190 " * @param short\n" 3191 " * keep indentation\n" 3192 " */\n", 3193 format("/**\n" 3194 " * Sentence that should be broken.\n" 3195 " * @param short\n" 3196 " * keep indentation\n" 3197 " */\n", 3198 Style20)); 3199 3200 EXPECT_EQ("/**\n" 3201 " * @param l1 long1\n" 3202 " * to break\n" 3203 " * @param l2 long2\n" 3204 " * to break\n" 3205 " */\n", 3206 format("/**\n" 3207 " * @param l1 long1 to break\n" 3208 " * @param l2 long2 to break\n" 3209 " */\n", 3210 Style20)); 3211 3212 EXPECT_EQ("/**\n" 3213 " * @param xx to\n" 3214 " * break\n" 3215 " * no reflow\n" 3216 " */\n", 3217 format("/**\n" 3218 " * @param xx to break\n" 3219 " * no reflow\n" 3220 " */\n", 3221 Style20)); 3222 3223 EXPECT_EQ("/**\n" 3224 " * @param xx to\n" 3225 " * break yes\n" 3226 " * reflow\n" 3227 " */\n", 3228 format("/**\n" 3229 " * @param xx to break\n" 3230 " * yes reflow\n" 3231 " */\n", 3232 Style20)); 3233 3234 FormatStyle JSStyle20 = getGoogleStyle(FormatStyle::LK_JavaScript); 3235 JSStyle20.ColumnLimit = 20; 3236 EXPECT_EQ("/**\n" 3237 " * @param l1 long1\n" 3238 " * to break\n" 3239 " */\n", 3240 format("/**\n" 3241 " * @param l1 long1 to break\n" 3242 " */\n", 3243 JSStyle20)); 3244 EXPECT_EQ("/**\n" 3245 " * @param {l1 long1\n" 3246 " * to break}\n" 3247 " */\n", 3248 format("/**\n" 3249 " * @param {l1 long1 to break}\n" 3250 " */\n", 3251 JSStyle20)); 3252 } 3253 3254 } // end namespace 3255 } // end namespace format 3256 } // end namespace clang 3257