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