1 //===- unittest/Format/FormatTestComments.cpp - Formatting unit tests -----===//
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
7 //===----------------------------------------------------------------------===//
9 #include "FormatTestBase.h"
11 #define DEBUG_TYPE "format-test-comments"
18 FormatStyle
getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp
); }
20 class FormatTestComments
: public FormatTestBase
{};
22 //===----------------------------------------------------------------------===//
23 // Tests for comments.
24 //===----------------------------------------------------------------------===//
26 TEST_F(FormatTestComments
, UnderstandsSingleLineComments
) {
27 verifyFormat("//* */");
28 verifyFormat("// line 1\n"
32 EXPECT_EQ("// comment", format("//comment"));
33 EXPECT_EQ("// #comment", format("//#comment"));
35 EXPECT_EQ("// comment\n"
38 "// clang-format on"));
40 verifyFormat("void f() {\n"
41 " // Doesn't do anything\n"
43 verifyFormat("SomeObject\n"
44 " // Calling someFunction on SomeObject\n"
46 verifyFormat("auto result = SomeObject\n"
47 " // Calling someFunction on SomeObject\n"
49 verifyFormat("void f(int i, // some comment (probably for i)\n"
50 " int j, // some comment (probably for j)\n"
51 " int k); // some comment (probably for k)");
52 verifyFormat("void f(int i,\n"
53 " // some comment (probably for j)\n"
55 " // some comment (probably for k)\n"
58 verifyFormat("int i // This is a fancy variable\n"
59 " = 5; // with nicely aligned comment.");
61 verifyFormat("// Leading comment.\n"
62 "int a; // Trailing comment.");
63 verifyFormat("int a; // Trailing comment\n"
67 verifyFormat("int a; // Trailing comment\n"
69 "// Leading comment.\n"
71 verifyFormat("int a; // Comment.\n"
73 "int bbbb; // Another comment.");
75 "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
76 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // comment\n"
77 "int cccccccccccccccccccccccccccccc; // comment\n"
78 "int ddd; // looooooooooooooooooooooooong comment\n"
79 "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
80 "int bbbbbbbbbbbbbbbbbbbbb; // comment\n"
81 "int ccccccccccccccccccc; // comment");
83 verifyFormat("#include \"a\" // comment\n"
84 "#include \"a/b/c\" // comment");
85 verifyFormat("#include <a> // comment\n"
86 "#include <a/b/c> // comment");
87 EXPECT_EQ("#include \"a\" // comment\n"
88 "#include \"a/b/c\" // comment",
89 format("#include \\\n"
91 "#include \"a/b/c\" // comment"));
93 verifyFormat("enum E {\n"
95 " VAL_A, // comment\n"
99 EXPECT_EQ("enum A {\n"
115 getLLVMStyleWithColumns(20)));
116 EXPECT_EQ("enum A {\n"
124 getLLVMStyleWithColumns(20)));
125 EXPECT_EQ("enum A {\n"
133 getLLVMStyleWithColumns(20)));
134 EXPECT_EQ("enum A {\n"
144 getLLVMStyleWithColumns(20)));
145 EXPECT_EQ("enum A {\n"
155 getLLVMStyleWithColumns(20)));
157 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
158 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
159 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
160 " // Comment inside a statement.\n"
161 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
162 verifyFormat("SomeFunction(a,\n"
165 verifyFormat("SomeFunction(a, a,\n"
169 "bool aaaaaaaaaaaaa = // comment\n"
170 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
171 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
173 verifyFormat("int aaaa; // aaaaa\n"
174 "int aa; // aaaaaaa",
175 getLLVMStyleWithColumns(20));
177 EXPECT_EQ("void f() { // This does something ..\n"
179 "int a; // This is unrelated",
180 format("void f() { // This does something ..\n"
182 "int a; // This is unrelated"));
183 EXPECT_EQ("class C {\n"
184 " void f() { // This does something ..\n"
187 " int a; // This is unrelated\n"
189 format("class C{void f() { // This does something ..\n"
192 "int a; // This is unrelated\n"
195 EXPECT_EQ("int i; // single line trailing comment",
196 format("int i;\\\n// single line trailing comment"));
198 verifyGoogleFormat("int a; // Trailing comment.");
200 verifyFormat("someFunction(anotherFunction( // Force break.\n"
203 verifyGoogleFormat("#endif // HEADER_GUARD");
205 verifyFormat("const char *test[] = {\n"
211 "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
212 " aaaaaaaaaaaaaaaaaaaaaa); // 81_cols_with_this_comment");
222 EXPECT_EQ("lineWith(); // comment\n"
225 format("lineWith(); // comment\n"
228 EXPECT_EQ("lineWith(); // comment\n"
232 format("lineWith(); // comment\n"
236 EXPECT_EQ("lineWith(); // comment\n"
239 format("lineWith(); // comment\n"
243 EXPECT_EQ("lineWith(); // comment\n"
245 "otherLine(); // comment",
246 format("lineWith(); // comment\n"
248 "otherLine(); // comment"));
249 EXPECT_EQ("lineWith();\n"
251 "otherLine(); // comment",
252 format("lineWith();\n"
254 "otherLine(); // comment"));
255 EXPECT_EQ("// first\n"
257 "otherLine(); // comment",
260 "otherLine(); // comment"));
264 "otherLine(); // comment",
268 "otherLine(); // comment"));
269 verifyFormat("f(); // comment\n"
273 EXPECT_EQ("f(); // comment\n"
277 format("f(); // comment\n"
281 EXPECT_EQ("f(); // comment\n"
285 format("f(); // comment\n"
289 EXPECT_EQ("void f() {\n"
290 " lineWith(); // comment\n"
293 format("void f() {\n"
294 " lineWith(); // comment\n"
297 EXPECT_EQ("int xy; // a\n"
299 format("int xy; // a\n"
301 EXPECT_EQ("int xy; // a\n"
303 format("int xy; // a\n"
305 getLLVMStyleWithColumns(12)));
307 verifyFormat("#define A \\\n"
308 " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n"
309 " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
310 getLLVMStyleWithColumns(60));
313 " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n"
314 " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
315 getLLVMStyleWithColumns(61));
317 verifyFormat("if ( // This is some comment\n"
320 EXPECT_EQ("if ( // This is some comment\n"
321 " // spanning two lines\n"
324 format("if( // This is some comment\n"
325 " // spanning two lines\n"
329 verifyNoCrash("/\\\n/");
330 verifyNoCrash("/\\\n* */");
331 // The 0-character somehow makes the lexer return a proper comment.
332 verifyNoCrash(StringRef("/*\\\0\n/", 6));
335 TEST_F(FormatTestComments
, KeepsParameterWithTrailingCommentsOnTheirOwnLine
) {
336 EXPECT_EQ("SomeFunction(a,\n"
339 format("SomeFunction(a,\n"
342 EXPECT_EQ("SomeFunction(a, b,\n"
345 format("SomeFunction(a,\n"
349 EXPECT_EQ("SomeFunction(a, b, // comment (unclear relation)\n"
351 format("SomeFunction(a, b, // comment (unclear relation)\n"
353 EXPECT_EQ("SomeFunction(a, // comment\n"
356 format("SomeFunction(a, // comment\n"
359 EXPECT_EQ("aaaaaaaaaa(aaaa(aaaa,\n"
362 format("aaaaaaaaaa(aaaa(aaaa,\n"
367 TEST_F(FormatTestComments
, RemovesTrailingWhitespaceOfComments
) {
368 EXPECT_EQ("// comment", format("// comment "));
369 EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment",
370 format("int aaaaaaa, bbbbbbb; // comment ",
371 getLLVMStyleWithColumns(33)));
372 EXPECT_EQ("// comment\\\n", format("// comment\\\n \t \v \f "));
373 EXPECT_EQ("// comment \\\n", format("// comment \\\n \t \v \f "));
376 TEST_F(FormatTestComments
, UnderstandsBlockComments
) {
377 verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);");
378 verifyFormat("void f() { g(/*aaa=*/x, /*bbb=*/!y, /*c=*/::c); }");
379 EXPECT_EQ("f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n"
380 " bbbbbbbbbbbbbbbbbbbbbbbbb);",
381 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \\\n"
382 "/* Trailing comment for aa... */\n"
383 " bbbbbbbbbbbbbbbbbbbbbbbbb);"));
385 "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
386 " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);",
387 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n"
388 "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);"));
390 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
391 " aaaaaaaaaaaaaaaaaa,\n"
392 " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
394 format("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
395 " aaaaaaaaaaaaaaaaaa ,\n"
396 " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
398 verifyFormat("f(/* aaaaaaaaaaaaaaaaaa = */\n"
399 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
401 FormatStyle NoBinPacking
= getLLVMStyle();
402 NoBinPacking
.BinPackParameters
= false;
403 verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n"
404 " /* parameter 2 */ aaaaaa,\n"
405 " /* parameter 3 */ aaaaaa,\n"
406 " /* parameter 4 */ aaaaaa);",
409 // Aligning block comments in macros.
410 verifyGoogleFormat("#define A \\\n"
415 TEST_F(FormatTestComments
, AlignsBlockComments
) {
417 " * Really multi-line\n"
422 " * Really multi-line\n"
426 EXPECT_EQ("class C {\n"
428 " * Another multi-line\n"
435 " * Another multi-line\n"
441 " 1. This is a comment with non-trivial formatting.\n"
442 " 1.1. We have to indent/outdent all lines equally\n"
443 " 1.1.1. to keep the formatting.\n"
446 " 1. This is a comment with non-trivial formatting.\n"
447 " 1.1. We have to indent/outdent all lines equally\n"
448 " 1.1.1. to keep the formatting.\n"
451 "Don't try to outdent if there's not enough indentation.\n"
454 " Don't try to outdent if there's not enough indentation.\n"
457 EXPECT_EQ("int i; /* Comment with empty...\n"
460 format("int i; /* Comment with empty...\n"
463 EXPECT_EQ("int foobar = 0; /* comment */\n"
464 "int bar = 0; /* multiline\n"
466 "int baz = 0; /* multiline\n"
468 "int bzz = 0; /* multiline\n"
470 format("int foobar = 0; /* comment */\n"
471 "int bar = 0; /* multiline\n"
473 "int baz = 0; /* multiline\n"
475 "int bzz = 0; /* multiline\n"
477 EXPECT_EQ("int foobar = 0; /* comment */\n"
478 "int bar = 0; /* multiline\n"
480 "int baz = 0; /* multiline\n"
482 format("int foobar = 0; /* comment */\n"
483 "int bar = 0; /* multiline\n"
485 "int baz = 0; /* multiline\n"
489 TEST_F(FormatTestComments
, CommentReflowingCanBeTurnedOff
) {
490 FormatStyle Style
= getLLVMStyleWithColumns(20);
491 Style
.ReflowComments
= false;
492 verifyFormat("// aaaaaaaaa aaaaaaaaaa aaaaaaaaaa", Style
);
493 verifyFormat("/* aaaaaaaaa aaaaaaaaaa aaaaaaaaaa */", Style
);
496 TEST_F(FormatTestComments
, CorrectlyHandlesLengthOfBlockComments
) {
497 EXPECT_EQ("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
498 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */",
499 format("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
500 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */"));
502 "void ffffffffffff(\n"
503 " int aaaaaaaa, int bbbbbbbb,\n"
504 " int cccccccccccc) { /*\n"
511 format("void ffffffffffff(int aaaaaaaa, int bbbbbbbb, int cccccccccccc)\n"
513 " aaaaaaaaaa aaaaaaaaaaaaa\n"
514 " bbbbbbbbbbbbbb bbbbbbbbbb\n"
517 getLLVMStyleWithColumns(40)));
520 TEST_F(FormatTestComments
, DontBreakNonTrailingBlockComments
) {
521 EXPECT_EQ("void ffffffffff(\n"
522 " int aaaaa /* test */);",
523 format("void ffffffffff(int aaaaa /* test */);",
524 getLLVMStyleWithColumns(35)));
527 TEST_F(FormatTestComments
, SplitsLongCxxComments
) {
528 EXPECT_EQ("// A comment that\n"
529 "// doesn't fit on\n"
531 format("// A comment that doesn't fit on one line",
532 getLLVMStyleWithColumns(20)));
533 EXPECT_EQ("/// A comment that\n"
534 "/// doesn't fit on\n"
536 format("/// A comment that doesn't fit on one line",
537 getLLVMStyleWithColumns(20)));
538 EXPECT_EQ("//! A comment that\n"
539 "//! doesn't fit on\n"
541 format("//! A comment that doesn't fit on one line",
542 getLLVMStyleWithColumns(20)));
543 EXPECT_EQ("// a b c d\n"
546 format("// a b c d e f g h i j k", getLLVMStyleWithColumns(10)));
551 format("\\\n// a b c d e f g h i j k", getLLVMStyleWithColumns(10)));
552 EXPECT_EQ("if (true) // A comment that\n"
553 " // doesn't fit on\n"
555 format("if (true) // A comment that doesn't fit on one line ",
556 getLLVMStyleWithColumns(30)));
557 verifyNoChange("// Don't_touch_leading_whitespace",
558 getLLVMStyleWithColumns(20));
559 EXPECT_EQ("// Add leading\n"
561 format("//Add leading whitespace", getLLVMStyleWithColumns(20)));
562 EXPECT_EQ("/// Add leading\n"
564 format("///Add leading whitespace", getLLVMStyleWithColumns(20)));
565 EXPECT_EQ("//! Add leading\n"
567 format("//!Add leading whitespace", getLLVMStyleWithColumns(20)));
568 EXPECT_EQ("// whitespace", format("//whitespace"));
569 EXPECT_EQ("// Even if it makes the line exceed the column\n"
571 format("//Even if it makes the line exceed the column limit",
572 getLLVMStyleWithColumns(51)));
573 verifyFormat("//--But not here");
574 EXPECT_EQ("/// line 1\n"
575 "// add leading whitespace",
576 format("/// line 1\n"
577 "//add leading whitespace",
578 getLLVMStyleWithColumns(30)));
579 EXPECT_EQ("/// line 1\n"
593 getLLVMStyleWithColumns(20)));
595 EXPECT_EQ("// aa bb cc dd",
596 format("// aa bb cc dd ",
597 getLLVMStyleWithColumns(15)));
599 EXPECT_EQ("// A comment before\n"
603 format("// A comment before a macro definition\n"
605 getLLVMStyleWithColumns(20)));
606 EXPECT_EQ("void ffffff(\n"
607 " int aaaaaaaaa, // wwww\n"
608 " int bbbbbbbbbb, // xxxxxxx\n"
610 " int c, int d, int e) {}",
611 format("void ffffff(\n"
612 " int aaaaaaaaa, // wwww\n"
613 " int bbbbbbbbbb, // xxxxxxx yyyyyyyyyy\n"
614 " int c, int d, int e) {}",
615 getLLVMStyleWithColumns(40)));
616 verifyFormat("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
617 getLLVMStyleWithColumns(20));
619 "#define XXX // a b c d\n"
621 format("#define XXX // a b c d e f g h", getLLVMStyleWithColumns(22)));
623 "#define XXX // q w e r\n"
625 format("#define XXX //q w e r t y u i", getLLVMStyleWithColumns(22)));
629 " // long 1 2 3 4 5\n"
634 " // long 1 2 3 4 5\n"
636 getLLVMStyleWithColumns(20)));
640 " // long 1 2 3 4 5\n"
646 " // long 1 2 3 4 5 6\n"
648 getLLVMStyleWithColumns(20)));
650 EXPECT_EQ("//: A comment that\n"
651 "//: doesn't fit on\n"
653 format("//: A comment that doesn't fit on one line",
654 getLLVMStyleWithColumns(20)));
657 "//\t\t\t\tofMap(message.velocity, 0, 127, 0, ofGetWidth()\n"
659 "//\t\t\t\tofMap(message.velocity, 0, 127, 0, ofGetWidth() * 0.2)");
662 TEST_F(FormatTestComments
, PreservesHangingIndentInCxxComments
) {
663 EXPECT_EQ("// A comment\n"
667 format("// A comment that doesn't fit on one line",
668 getLLVMStyleWithColumns(20)));
669 EXPECT_EQ("/// A comment\n"
673 format("/// A comment that doesn't fit on one line",
674 getLLVMStyleWithColumns(20)));
677 TEST_F(FormatTestComments
, DontSplitLineCommentsWithEscapedNewlines
) {
678 EXPECT_EQ("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
679 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
680 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
681 format("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
682 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
683 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
684 EXPECT_EQ("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
685 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
686 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
687 format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
688 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
689 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
690 getLLVMStyleWithColumns(50)));
691 // FIXME: One day we might want to implement adjustment of leading whitespace
692 // of the consecutive lines in this kind of comment:
694 " a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
695 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
696 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
697 format("double a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
698 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
699 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
700 getLLVMStyleWithColumns(49)));
703 TEST_F(FormatTestComments
, DontIntroduceMultilineComments
) {
704 // Avoid introducing a multiline comment by breaking after `\`.
705 for (int ColumnLimit
= 15; ColumnLimit
<= 17; ++ColumnLimit
) {
709 format("// aaaaaaaaaa \\ bb", getLLVMStyleWithColumns(ColumnLimit
)));
713 format("// aaaaaaaaa \\ bb", getLLVMStyleWithColumns(ColumnLimit
)));
717 format("// aaaaaaaaa \\ \\ bb", getLLVMStyleWithColumns(ColumnLimit
)));
721 TEST_F(FormatTestComments
, DontSplitLineCommentsWithPragmas
) {
722 FormatStyle Pragmas
= getLLVMStyleWithColumns(30);
723 Pragmas
.CommentPragmas
= "^ IWYU pragma:";
725 "// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb",
726 format("// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", Pragmas
));
728 "/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */",
729 format("/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", Pragmas
));
732 TEST_F(FormatTestComments
, PriorityOfCommentBreaking
) {
733 EXPECT_EQ("if (xxx ==\n"
734 " yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
737 format("if (xxx == yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
739 getLLVMStyleWithColumns(40)));
740 EXPECT_EQ("if (xxxxxxxxxx ==\n"
741 " yyy && // aaaaaa bbbbbbbb cccc\n"
744 format("if (xxxxxxxxxx == yyy && // aaaaaa bbbbbbbb cccc\n"
746 getLLVMStyleWithColumns(40)));
747 EXPECT_EQ("if (xxxxxxxxxx &&\n"
748 " yyy || // aaaaaa bbbbbbbb cccc\n"
751 format("if (xxxxxxxxxx && yyy || // aaaaaa bbbbbbbb cccc\n"
753 getLLVMStyleWithColumns(40)));
754 EXPECT_EQ("fffffffff(\n"
755 " &xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
757 format("fffffffff(&xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
759 getLLVMStyleWithColumns(40)));
762 TEST_F(FormatTestComments
, MultiLineCommentsInDefines
) {
763 EXPECT_EQ("#define A(x) /* \\\n"
767 format("#define A(x) /* \\\n"
771 getLLVMStyleWithColumns(17)));
772 EXPECT_EQ("#define A( \\\n"
777 format("#define A( \\\n"
782 getLLVMStyleWithColumns(17)));
785 TEST_F(FormatTestComments
, ParsesCommentsAdjacentToPPDirectives
) {
786 EXPECT_EQ("namespace {}\n// Test\n#define A",
787 format("namespace {}\n // Test\n#define A"));
788 EXPECT_EQ("namespace {}\n/* Test */\n#define A",
789 format("namespace {}\n /* Test */\n#define A"));
790 EXPECT_EQ("namespace {}\n/* Test */ #define A",
791 format("namespace {}\n /* Test */ #define A"));
794 TEST_F(FormatTestComments
, KeepsLevelOfCommentBeforePPDirective
) {
795 // Keep the current level if the comment was originally not aligned with
796 // the preprocessor directive.
797 EXPECT_EQ("void f() {\n"
803 format("void f() {\n"
810 EXPECT_EQ("void f() {\n"
817 format("void f() {\n"
825 EXPECT_EQ("int f(int i) {\n"
834 format("int f(int i) {\n"
844 EXPECT_EQ("int f(int i) {\n"
848 " // comment in else\n"
854 format("int f(int i) {\n"
858 " // comment in else\n"
865 EXPECT_EQ("int f(int i) {\n"
869 " /* comment in else */\n"
875 format("int f(int i) {\n"
879 " /* comment in else */\n"
886 // Keep the current level if there is an empty line between the comment and
887 // the preprocessor directive.
888 EXPECT_EQ("void f() {\n"
895 format("void f() {\n"
903 EXPECT_EQ("void f() {\n"
912 format("void f() {\n"
922 EXPECT_EQ("int f(int i) {\n"
932 format("int f(int i) {\n"
943 EXPECT_EQ("int f(int i) {\n"
947 " // comment in else\n"
954 format("int f(int i) {\n"
958 "// comment in else\n"
966 EXPECT_EQ("int f(int i) {\n"
970 " /* comment in else */\n"
977 format("int f(int i) {\n"
981 "/* comment in else */\n"
989 // Align with the preprocessor directive if the comment was originally aligned
990 // with the preprocessor directive and there is no newline between the comment
991 // and the preprocessor directive.
992 EXPECT_EQ("void f() {\n"
998 format("void f() {\n"
1005 EXPECT_EQ("int f(int i) {\n"
1014 format("int f(int i) {\n"
1024 EXPECT_EQ("int f(int i) {\n"
1028 "// comment in else\n"
1034 format("int f(int i) {\n"
1038 " // comment in else\n"
1045 EXPECT_EQ("int f(int i) {\n"
1049 "/* comment in else */\n"
1055 format("int f(int i) {\n"
1059 " /* comment in else */\n"
1066 const StringRef
Code("void func() {\n"
1067 " // clang-format off\n"
1068 " #define KV(value) #value, value\n"
1069 " // clang-format on\n"
1071 verifyNoChange(Code
);
1074 TEST_F(FormatTestComments
, SplitsLongLinesInComments
) {
1075 // FIXME: Do we need to fix up the " */" at the end?
1076 // It doesn't look like any of our current logic triggers this.
1077 EXPECT_EQ("/* This is a long\n"
1079 " * doesn't fit on\n"
1085 "fit on one line. */",
1086 getLLVMStyleWithColumns(20)));
1092 format("/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10)));
1098 format("\\\n/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10)));
1101 "comment that doesn't\n"
1102 "fit on one line.\n"
1106 "comment that doesn't "
1107 "fit on one line. \n"
1109 getLLVMStyleWithColumns(20)));
1111 " * This is a long\n"
1113 " * doesn't fit on\n"
1117 " * This is a long "
1122 getLLVMStyleWithColumns(20)));
1124 " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n"
1125 " * so_it_should_be_broken\n"
1126 " * wherever_a_space_occurs\n"
1129 " * This_is_a_comment_with_words_that_dont_fit_on_one_line "
1130 " so_it_should_be_broken "
1131 " wherever_a_space_occurs \n"
1133 getLLVMStyleWithColumns(20)));
1135 " * This_comment_can_not_be_broken_into_lines\n"
1138 " * This_comment_can_not_be_broken_into_lines\n"
1140 getLLVMStyleWithColumns(20)));
1143 " This is another\n"
1144 " long comment that\n"
1145 " doesn't fit on one\n"
1146 " line 1234567890\n"
1152 " long comment that "
1153 " doesn't fit on one"
1154 " line 1234567890\n"
1157 getLLVMStyleWithColumns(20)));
1161 " * another comment\n"
1162 " * t hat doesn' t\n"
1163 " * fit on one l i\n"
1176 getLLVMStyleWithColumns(20)));
1178 " * This is a long\n"
1180 " * doesn't fit on\n"
1184 " * This is a long comment that doesn't fit on one line\n"
1186 getLLVMStyleWithColumns(20)));
1188 " if (something) /* This is a\n"
1194 " if (something) /* This is a long comment */\n"
1197 getLLVMStyleWithColumns(30)));
1199 EXPECT_EQ("/* A comment before\n"
1201 " * definition */\n"
1203 format("/* A comment before a macro definition */\n"
1205 getLLVMStyleWithColumns(20)));
1207 EXPECT_EQ("/* some comment\n"
1208 " * a comment that\n"
1209 " * we break another\n"
1210 " * comment we have\n"
1211 " * to break a left\n"
1214 format(" /* some comment\n"
1215 " * a comment that we break\n"
1216 " * another comment we have to break\n"
1217 "* a left comment\n"
1219 getLLVMStyleWithColumns(20)));
1222 " * multiline block\n"
1227 " * multiline block comment\n"
1230 getLLVMStyleWithColumns(20)));
1232 // This reproduces a crashing bug where both adaptStartOfLine and
1233 // getCommentSplit were trying to wrap after the "/**".
1234 verifyFormat("/** multilineblockcommentwithnowrapopportunity */",
1235 getLLVMStyleWithColumns(20));
1246 EXPECT_EQ("/* a a */",
1247 format("/* a a */", getLLVMStyleWithColumns(15)));
1248 EXPECT_EQ("/* a a bc */",
1249 format("/* a a bc */", getLLVMStyleWithColumns(15)));
1250 EXPECT_EQ("/* aaa aaa\n"
1252 format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15)));
1253 EXPECT_EQ("/* aaa aaa\n"
1255 format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15)));
1258 TEST_F(FormatTestComments
, SplitsLongLinesInCommentsInPreprocessor
) {
1259 EXPECT_EQ("#define X \\\n"
1262 " Macro comment \\\n"
1267 format("#define X \\\n"
1270 " Macro comment with a long line\n"
1273 getLLVMStyleWithColumns(20)));
1274 EXPECT_EQ("#define X \\\n"
1275 " /* Macro comment \\\n"
1279 format("#define X \\\n"
1280 " /* Macro comment with a long\n"
1283 getLLVMStyleWithColumns(20)));
1284 EXPECT_EQ("#define X \\\n"
1285 " /* Macro comment \\\n"
1286 " * with a long \\\n"
1289 format("#define X \\\n"
1290 " /* Macro comment with a long line */ \\\n"
1292 getLLVMStyleWithColumns(20)));
1295 TEST_F(FormatTestComments
, KeepsTrailingPPCommentsAndSectionCommentsSeparate
) {
1296 verifyFormat("#ifdef A // line about A\n"
1297 "// section comment\n"
1299 getLLVMStyleWithColumns(80));
1300 verifyFormat("#ifdef A // line 1 about A\n"
1301 " // line 2 about A\n"
1302 "// section comment\n"
1304 getLLVMStyleWithColumns(80));
1305 EXPECT_EQ("#ifdef A // line 1 about A\n"
1306 " // line 2 about A\n"
1307 "// section comment\n"
1309 format("#ifdef A // line 1 about A\n"
1310 " // line 2 about A\n"
1311 "// section comment\n"
1313 getLLVMStyleWithColumns(80)));
1314 verifyFormat("int f() {\n"
1316 "#ifdef A // comment about A\n"
1317 " // section comment 1\n"
1318 " // section comment 2\n"
1320 "#else // comment about #else\n"
1321 " // section comment 3\n"
1325 getLLVMStyleWithColumns(80));
1328 TEST_F(FormatTestComments
, AlignsPPElseEndifComments
) {
1329 verifyFormat("#if A\n"
1333 getLLVMStyleWithColumns(20));
1334 verifyFormat("#if A\n"
1338 getLLVMStyleWithColumns(20));
1349 getLLVMStyleWithColumns(20)));
1352 TEST_F(FormatTestComments
, CommentsInStaticInitializers
) {
1354 "static SomeType type = {aaaaaaaaaaaaaaaaaaaa, /* comment */\n"
1355 " aaaaaaaaaaaaaaaaaaaa /* comment */,\n"
1356 " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n"
1357 " aaaaaaaaaaaaaaaaaaaa, // comment\n"
1358 " aaaaaaaaaaaaaaaaaaaa};",
1359 format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n"
1360 " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n"
1361 " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n"
1362 " aaaaaaaaaaaaaaaaaaaa , // comment\n"
1363 " aaaaaaaaaaaaaaaaaaaa };"));
1364 verifyFormat("static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
1365 " bbbbbbbbbbb, ccccccccccc};");
1366 verifyFormat("static SomeType type = {aaaaaaaaaaa,\n"
1367 " // comment for bb....\n"
1368 " bbbbbbbbbbb, ccccccccccc};");
1370 "static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
1371 " bbbbbbbbbbb, ccccccccccc};");
1372 verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n"
1373 " // comment for bb....\n"
1374 " bbbbbbbbbbb, ccccccccccc};");
1376 verifyFormat("S s = {{a, b, c}, // Group #1\n"
1377 " {d, e, f}, // Group #2\n"
1378 " {g, h, i}}; // Group #3");
1379 verifyFormat("S s = {{// Group #1\n"
1386 EXPECT_EQ("S s = {\n"
1387 " // Some comment\n"
1390 " // Comment after empty line\n"
1393 " // Some comment\n"
1396 " // Comment after empty line\n"
1399 EXPECT_EQ("S s = {\n"
1400 " /* Some comment */\n"
1403 " /* Comment after empty line */\n"
1406 " /* Some comment */\n"
1409 " /* Comment after empty line */\n"
1412 verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n"
1413 " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
1414 " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
1415 " 0x00, 0x00, 0x00, 0x00}; // comment");
1418 TEST_F(FormatTestComments
, LineCommentsAfterRightBrace
) {
1419 EXPECT_EQ("if (true) { // comment about branch\n"
1420 " // comment about f\n"
1423 format("if (true) { // comment about branch\n"
1424 " // comment about f\n"
1427 getLLVMStyleWithColumns(80)));
1428 EXPECT_EQ("if (1) { // if line 1\n"
1434 "} else { // else line 1\n"
1440 format("if (1) { // if line 1\n"
1446 "} else { // else line 1\n"
1452 EXPECT_EQ("do { // line 1\n"
1457 format("do { // line 1\n"
1462 getLLVMStyleWithColumns(80)));
1463 EXPECT_EQ("while (a < b) { // line 1\n"
1468 format("while (a < b) {// line 1\n"
1473 getLLVMStyleWithColumns(80)));
1476 TEST_F(FormatTestComments
, ReflowsComments
) {
1477 // Break a long line and reflow with the full next line.
1478 EXPECT_EQ("// long long long\n"
1480 format("// long long long long\n"
1482 getLLVMStyleWithColumns(20)));
1484 // Keep the trailing newline while reflowing.
1485 EXPECT_EQ("// long long long\n"
1487 format("// long long long long\n"
1489 getLLVMStyleWithColumns(20)));
1491 // Break a long line and reflow with a part of the next line.
1492 EXPECT_EQ("// long long long\n"
1495 format("// long long long long\n"
1496 "// long long_long",
1497 getLLVMStyleWithColumns(20)));
1499 // Break but do not reflow if the first word from the next line is too long.
1500 EXPECT_EQ("// long long long\n"
1502 "// long_long_long",
1503 format("// long long long long\n"
1504 "// long_long_long",
1505 getLLVMStyleWithColumns(20)));
1507 // Don't break or reflow short lines.
1508 verifyFormat("// long\n"
1509 "// long long long lo\n"
1510 "// long long long lo\n"
1512 getLLVMStyleWithColumns(20));
1514 // Keep prefixes and decorations while reflowing.
1515 EXPECT_EQ("/// long long long\n"
1517 format("/// long long long long\n"
1519 getLLVMStyleWithColumns(20)));
1520 EXPECT_EQ("//! long long long\n"
1522 format("//! long long long long\n"
1524 getLLVMStyleWithColumns(20)));
1525 EXPECT_EQ("/* long long long\n"
1527 format("/* long long long long\n"
1529 getLLVMStyleWithColumns(20)));
1530 EXPECT_EQ("///< long long long\n"
1532 format("///< long long long long\n"
1534 getLLVMStyleWithColumns(20)));
1535 EXPECT_EQ("//!< long long long\n"
1537 format("//!< long long long long\n"
1539 getLLVMStyleWithColumns(20)));
1541 // Don't bring leading whitespace up while reflowing.
1542 EXPECT_EQ("/* long long long\n"
1543 " * long long long\n"
1545 format("/* long long long long\n"
1548 getLLVMStyleWithColumns(20)));
1550 // Reflow the last line of a block comment with its trailing '*/'.
1551 EXPECT_EQ("/* long long long\n"
1553 format("/* long long long long\n"
1555 getLLVMStyleWithColumns(20)));
1557 // Reflow two short lines; keep the postfix of the last one.
1558 EXPECT_EQ("/* long long long\n"
1559 " * long long long */",
1560 format("/* long long long long\n"
1563 getLLVMStyleWithColumns(20)));
1565 // Put the postfix of the last short reflow line on a newline if it doesn't
1567 EXPECT_EQ("/* long long long\n"
1568 " * long long longg\n"
1570 format("/* long long long long\n"
1573 getLLVMStyleWithColumns(20)));
1575 // Reflow lines with leading whitespace.
1578 " * long long long\n"
1579 " * long long long\n"
1580 " * long long long\n"
1585 " * long long long long\n"
1587 " * long long long long\n"
1590 getLLVMStyleWithColumns(20)));
1592 // Break single line block comments that are first in the line with ' *'
1594 EXPECT_EQ("/* long long long\n"
1596 format("/* long long long long */", getLLVMStyleWithColumns(20)));
1598 // Break single line block comment that are not first in the line with ' '
1600 EXPECT_EQ("int i; /* long long\n"
1602 format("int i; /* long long long */", getLLVMStyleWithColumns(20)));
1604 // Reflow a line that goes just over the column limit.
1605 EXPECT_EQ("// long long long\n"
1607 format("// long long long lon\n"
1609 getLLVMStyleWithColumns(20)));
1611 // Stop reflowing if the next line has a different indentation than the
1613 EXPECT_EQ("// long long long\n"
1617 format("// long long long long\n"
1620 getLLVMStyleWithColumns(20)));
1622 // Reflow into the last part of a really long line that has been broken into
1624 EXPECT_EQ("// long long long\n"
1625 "// long long long\n"
1626 "// long long long",
1627 format("// long long long long long long long long\n"
1629 getLLVMStyleWithColumns(20)));
1631 // Break the first line, then reflow the beginning of the second and third
1633 EXPECT_EQ("// long long long\n"
1634 "// lon1 lon2 lon2\n"
1635 "// lon2 lon3 lon3",
1636 format("// long long long lon1\n"
1637 "// lon2 lon2 lon2\n"
1639 getLLVMStyleWithColumns(20)));
1641 // Reflow the beginning of the second line, then break the rest.
1642 EXPECT_EQ("// long long long\n"
1643 "// lon1 lon2 lon2\n"
1644 "// lon2 lon2 lon2\n"
1646 format("// long long long lon1\n"
1647 "// lon2 lon2 lon2 lon2 lon2 lon3",
1648 getLLVMStyleWithColumns(20)));
1650 // Shrink the first line, then reflow the second line up.
1651 EXPECT_EQ("// long long long", format("// long long\n"
1653 getLLVMStyleWithColumns(20)));
1655 // Don't shrink leading whitespace.
1656 verifyNoChange("int i; /// a", getLLVMStyleWithColumns(20));
1658 // Shrink trailing whitespace if there is no postfix and reflow.
1659 EXPECT_EQ("// long long long\n"
1661 format("// long long long long \n"
1663 getLLVMStyleWithColumns(20)));
1665 // Shrink trailing whitespace to a single one if there is postfix.
1666 EXPECT_EQ("/* long long long */",
1667 format("/* long long long */", getLLVMStyleWithColumns(20)));
1669 // Break a block comment postfix if exceeding the line limit.
1670 EXPECT_EQ("/* long\n"
1672 format("/* long */", getLLVMStyleWithColumns(20)));
1674 // Reflow indented comments.
1676 " // long long long\n"
1678 " int i; /* long lon\n"
1683 " // long long long long\n"
1685 " int i; /* long lon g\n"
1688 getLLVMStyleWithColumns(20)));
1690 // Don't realign trailing comments after reflow has happened.
1691 EXPECT_EQ("// long long long\n"
1694 format("// long long long long\n"
1697 getLLVMStyleWithColumns(20)));
1698 EXPECT_EQ("// long long long\n"
1699 "// longng long long\n"
1701 format("// long long long longng\n"
1702 "// long long long\n"
1704 getLLVMStyleWithColumns(20)));
1706 // Reflow lines after a broken line.
1707 EXPECT_EQ("int a; // Trailing\n"
1711 format("int a; // Trailing comment\n"
1715 getLLVMStyleWithColumns(20)));
1716 EXPECT_EQ("/// This long line\n"
1717 "/// gets reflown.",
1718 format("/// This long line gets\n"
1720 getLLVMStyleWithColumns(20)));
1721 EXPECT_EQ("//! This long line\n"
1722 "//! gets reflown.",
1723 format(" //! This long line gets\n"
1725 getLLVMStyleWithColumns(20)));
1726 EXPECT_EQ("/* This long line\n"
1727 " * gets reflown.\n"
1729 format("/* This long line gets\n"
1732 getLLVMStyleWithColumns(20)));
1734 // Reflow after indentation makes a line too long.
1736 " // long long long\n"
1740 "// long long long lo\n"
1743 getLLVMStyleWithColumns(20)));
1745 // Break and reflow multiple lines.
1747 " * Reflow the end of\n"
1748 " * line by 11 22 33\n"
1752 " * Reflow the end of line\n"
1759 getLLVMStyleWithColumns(20)));
1760 EXPECT_EQ("/// First line gets\n"
1761 "/// broken. Second\n"
1764 "/// broken. Third\n"
1765 "/// gets reflown.",
1766 format("/// First line gets broken.\n"
1767 "/// Second line gets reflown and broken.\n"
1768 "/// Third gets reflown.",
1769 getLLVMStyleWithColumns(20)));
1770 EXPECT_EQ("int i; // first long\n"
1773 format("int i; // first long long\n"
1775 getLLVMStyleWithColumns(20)));
1777 " // first long line\n"
1779 " // long line line\n"
1780 " // third long line\n"
1784 " // first long line line\n"
1785 " // second long line line\n"
1786 " // third long line line\n"
1788 getLLVMStyleWithColumns(20)));
1789 EXPECT_EQ("int i; /* first line\n"
1794 format("int i; /* first line\n"
1798 getLLVMStyleWithColumns(20)));
1800 // Reflow the last two lines of a section that starts with a line having
1801 // different indentation.
1802 EXPECT_EQ("// long\n"
1803 "// long long long\n"
1806 "// long long long long\n"
1808 getLLVMStyleWithColumns(20)));
1810 // Keep the block comment endling '*/' while reflowing.
1811 EXPECT_EQ("/* Long long long\n"
1813 format("/* Long long long line\n"
1815 getLLVMStyleWithColumns(20)));
1817 // Don't reflow between separate blocks of comments.
1818 EXPECT_EQ("/* First comment\n"
1819 " * block will */\n"
1822 format("/* First comment block\n"
1826 getLLVMStyleWithColumns(20)));
1828 // Don't reflow across blank comment lines.
1829 EXPECT_EQ("int i; // This long\n"
1834 format("int i; // This long line gets broken.\n"
1837 getLLVMStyleWithColumns(20)));
1839 " /// long long long\n"
1845 " /// long long long long\n"
1850 getLLVMStyleWithColumns(20)));
1851 EXPECT_EQ("//! long long long\n"
1855 format("//! long long long long\n"
1858 getLLVMStyleWithColumns(20)));
1859 EXPECT_EQ("/* long long long\n"
1863 format("/* long long long long\n"
1866 getLLVMStyleWithColumns(20)));
1867 EXPECT_EQ("/* long long long\n"
1871 format("/* long long long long\n"
1874 getLLVMStyleWithColumns(20)));
1876 // Don't reflow lines having content that is a single character.
1877 EXPECT_EQ("// long long long\n"
1880 format("// long long long long\n"
1882 getLLVMStyleWithColumns(20)));
1884 // Don't reflow lines starting with two punctuation characters.
1885 EXPECT_EQ("// long long long\n"
1888 format("// long long long long\n"
1890 getLLVMStyleWithColumns(20)));
1892 // Don't reflow lines starting with '@'.
1893 EXPECT_EQ("// long long long\n"
1896 format("// long long long long\n"
1898 getLLVMStyleWithColumns(20)));
1900 // Don't reflow lines starting with 'TODO'.
1901 EXPECT_EQ("// long long long\n"
1904 format("// long long long long\n"
1906 getLLVMStyleWithColumns(20)));
1908 // Don't reflow lines starting with 'FIXME'.
1909 EXPECT_EQ("// long long long\n"
1912 format("// long long long long\n"
1914 getLLVMStyleWithColumns(20)));
1916 // Don't reflow lines starting with 'XXX'.
1917 EXPECT_EQ("// long long long\n"
1920 format("// long long long long\n"
1922 getLLVMStyleWithColumns(20)));
1924 // Don't reflow comment pragmas.
1925 EXPECT_EQ("// long long long\n"
1928 format("// long long long long\n"
1930 getLLVMStyleWithColumns(20)));
1931 EXPECT_EQ("/* long long long\n"
1935 format("/* long long long long\n"
1938 getLLVMStyleWithColumns(20)));
1940 // Reflow lines that have a non-punctuation character among their first 2
1942 EXPECT_EQ("// long long long\n"
1944 format("// long long long long\n"
1946 getLLVMStyleWithColumns(20)));
1948 // Don't reflow between separate blocks of comments.
1949 EXPECT_EQ("/* First comment\n"
1950 " * block will */\n"
1953 format("/* First comment block\n"
1957 getLLVMStyleWithColumns(20)));
1959 // Don't reflow lines having different indentation.
1960 EXPECT_EQ("// long long long\n"
1963 format("// long long long long\n"
1965 getLLVMStyleWithColumns(20)));
1967 // Don't reflow separate bullets in list
1968 EXPECT_EQ("// - long long long\n"
1971 format("// - long long long long\n"
1973 getLLVMStyleWithColumns(20)));
1974 EXPECT_EQ("// * long long long\n"
1977 format("// * long long long long\n"
1979 getLLVMStyleWithColumns(20)));
1980 EXPECT_EQ("// + long long long\n"
1983 format("// + long long long long\n"
1985 getLLVMStyleWithColumns(20)));
1986 EXPECT_EQ("// 1. long long long\n"
1989 format("// 1. long long long long\n"
1991 getLLVMStyleWithColumns(20)));
1992 EXPECT_EQ("// -# long long long\n"
1995 format("// -# long long long long\n"
1997 getLLVMStyleWithColumns(20)));
1999 EXPECT_EQ("// - long long long\n"
2000 "// long long long\n"
2002 format("// - long long long long\n"
2005 getLLVMStyleWithColumns(20)));
2006 EXPECT_EQ("// - long long long\n"
2007 "// long long long\n"
2010 format("// - long long long long\n"
2011 "// long long long\n"
2013 getLLVMStyleWithColumns(20)));
2015 // Large number (>2 digits) are not list items
2016 EXPECT_EQ("// long long long\n"
2017 "// long 1024. long.",
2018 format("// long long long long\n"
2020 getLLVMStyleWithColumns(20)));
2022 // Do not break before number, to avoid introducing a non-reflowable doxygen
2024 EXPECT_EQ("// long long\n"
2025 "// long 10. long.",
2026 format("// long long long 10.\n"
2028 getLLVMStyleWithColumns(20)));
2030 // Don't break or reflow after implicit string literals.
2031 verifyFormat("#include <t> // l l l\n"
2033 getLLVMStyleWithColumns(20));
2035 // Don't break or reflow comments on import lines.
2036 EXPECT_EQ("#include \"t\" /* l l l\n"
2038 format("#include \"t\" /* l l l\n"
2040 getLLVMStyleWithColumns(20)));
2042 // Don't reflow between different trailing comment sections.
2043 EXPECT_EQ("int i; // long long\n"
2045 "int j; // long long\n"
2047 format("int i; // long long long\n"
2048 "int j; // long long long",
2049 getLLVMStyleWithColumns(20)));
2051 // Don't reflow if the first word on the next line is longer than the
2052 // available space at current line.
2053 EXPECT_EQ("int i; // trigger\n"
2056 format("int i; // trigger reflow\n"
2058 getLLVMStyleWithColumns(20)));
2060 // Simple case that correctly handles reflow in parameter lists.
2061 EXPECT_EQ("a = f(/* looooooooong\n"
2065 format("a = f(/* looooooooong long\n* long\n*/ a);",
2066 getLLVMStyleWithColumns(22)));
2067 // Tricky case that has fewer lines if we reflow the comment, ending up with
2069 EXPECT_EQ("a = f(/* loooooong\n"
2073 format("a = f(/* loooooong long\n* long\n*/ a);",
2074 getLLVMStyleWithColumns(22)));
2076 // Keep empty comment lines.
2077 EXPECT_EQ("/**/", format(" /**/", getLLVMStyleWithColumns(20)));
2078 EXPECT_EQ("/* */", format(" /* */", getLLVMStyleWithColumns(20)));
2079 EXPECT_EQ("/* */", format(" /* */", getLLVMStyleWithColumns(20)));
2080 EXPECT_EQ("//", format(" // ", getLLVMStyleWithColumns(20)));
2081 EXPECT_EQ("///", format(" /// ", getLLVMStyleWithColumns(20)));
2084 TEST_F(FormatTestComments
, ReflowsCommentsPrecise
) {
2085 // FIXME: This assumes we do not continue compressing whitespace once we are
2086 // in reflow mode. Consider compressing whitespace.
2088 // Test that we stop reflowing precisely at the column limit.
2089 // After reflowing, "// reflows into foo" does not fit the column limit,
2090 // so we compress the whitespace.
2091 EXPECT_EQ("// some text that\n"
2092 "// reflows into foo",
2093 format("// some text that reflows\n"
2095 getLLVMStyleWithColumns(20)));
2096 // Given one more column, "// reflows into foo" does fit the limit, so we
2097 // do not compress the whitespace.
2098 EXPECT_EQ("// some text that\n"
2099 "// reflows into foo",
2100 format("// some text that reflows\n"
2102 getLLVMStyleWithColumns(21)));
2104 // Make sure that we correctly account for the space added in the reflow case
2105 // when making the reflowing decision.
2106 // First, when the next line ends precisely one column over the limit, do not
2108 EXPECT_EQ("// some text that\n"
2111 format("// some text that reflows\n"
2113 getLLVMStyleWithColumns(21)));
2114 // Secondly, when the next line ends later, but the first word in that line
2115 // is precisely one column over the limit, do not reflow.
2116 EXPECT_EQ("// some text that\n"
2119 format("// some text that reflows\n"
2121 getLLVMStyleWithColumns(21)));
2124 TEST_F(FormatTestComments
, ReflowsCommentsWithExtraWhitespace
) {
2126 EXPECT_EQ("// some text\n"
2128 format("// some text that\n"
2130 getLLVMStyleWithColumns(16)));
2131 EXPECT_EQ("// some text\n"
2133 format("// some text that\n"
2135 getLLVMStyleWithColumns(16)));
2136 EXPECT_EQ("/* some text\n"
2137 " * that re flows\n"
2139 format("/* some text that\n"
2142 getLLVMStyleWithColumns(16)));
2143 // FIXME: We do not reflow if the indent of two subsequent lines differs;
2144 // given that this is different behavior from block comments, do we want
2146 EXPECT_EQ("// some text\n"
2149 format("// some text that\n"
2151 getLLVMStyleWithColumns(16)));
2152 // Space within parts of a line that fit.
2153 // FIXME: Use the earliest possible split while reflowing to compress the
2154 // whitespace within the line.
2155 EXPECT_EQ("// some text that\n"
2158 format("// some text that does\n"
2159 "// re flow more here",
2160 getLLVMStyleWithColumns(21)));
2163 TEST_F(FormatTestComments
, IgnoresIf0Contents
) {
2165 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
2169 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
2172 EXPECT_EQ("#if false\n"
2176 format("#if false\n"
2180 EXPECT_EQ("enum E {\n"
2196 EXPECT_EQ("enum F {\n"
2218 EXPECT_EQ("enum G {\n"
2236 EXPECT_EQ("enum H {\n"
2258 EXPECT_EQ("enum I {\n"
2260 "#if /* test */ 0 || 1\n"
2268 "#if /* test */ 0 || 1\n"
2274 EXPECT_EQ("enum J {\n"
2299 // Ignore stuff in SWIG-blocks.
2300 EXPECT_EQ("#ifdef SWIG\n"
2301 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
2304 format("#ifdef SWIG\n"
2305 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
2308 EXPECT_EQ("#ifndef SWIG\n"
2311 format("#ifndef SWIG\n"
2316 TEST_F(FormatTestComments
, DontCrashOnBlockComments
) {
2318 "int xxxxxxxxx; /* "
2319 "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\n"
2322 format("int xxxxxxxxx; /* "
2323 "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy zzzzzz\n"
2327 TEST_F(FormatTestComments
, BlockCommentsInControlLoops
) {
2328 verifyFormat("if (0) /* a comment in a strange place */ {\n"
2331 verifyFormat("if (0) /* a comment in a strange place */ {\n"
2333 "} /* another comment */ else /* comment #3 */ {\n"
2336 verifyFormat("while (0) /* a comment in a strange place */ {\n"
2339 verifyFormat("for (;;) /* a comment in a strange place */ {\n"
2342 verifyFormat("do /* a comment in a strange place */ {\n"
2344 "} /* another comment */ while (0);");
2347 TEST_F(FormatTestComments
, BlockComments
) {
2348 EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */",
2349 format("/* *//* */ /* */\n/* *//* */ /* */"));
2350 EXPECT_EQ("/* */ a /* */ b;", format(" /* */ a/* */ b;"));
2351 EXPECT_EQ("#define A /*123*/ \\\n"
2356 format("#define A /*123*/ b\n"
2358 "someCall(parameter);",
2359 getLLVMStyleWithColumns(15)));
2361 EXPECT_EQ("#define A\n"
2364 format("#define A\n"
2365 "/* */someCall(parameter);",
2366 getLLVMStyleWithColumns(15)));
2367 verifyNoChange("/*\n**\n*/");
2375 " * aaaaaa aaaaaa\n"
2377 getLLVMStyleWithColumns(10)));
2387 getLLVMStyleWithColumns(10)));
2388 EXPECT_EQ("int aaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
2390 " bbbbbbbbbbbb */\n"
2391 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
2392 format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
2394 " bbbbbbbbbbbb */ bbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
2395 getLLVMStyleWithColumns(50)));
2397 FormatStyle NoBinPacking
= getLLVMStyle();
2398 NoBinPacking
.BinPackParameters
= false;
2399 EXPECT_EQ("someFunction(1, /* comment 1 */\n"
2400 " 2, /* comment 2 */\n"
2401 " 3, /* comment 3 */\n"
2404 format("someFunction (1, /* comment 1 */\n"
2405 " 2, /* comment 2 */ \n"
2406 " 3, /* comment 3 */\n"
2410 "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2411 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
2413 "bool aaaaaaaaaaaaa = /* trailing comment */\n"
2414 " aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2415 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;",
2417 "bool aaaaaaaaaaaaa = /* trailing comment */\n"
2418 " aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2419 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;"));
2421 "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
2422 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
2423 "int cccccccccccccccccccccccccccccc; /* comment */",
2424 format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
2425 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
2426 "int cccccccccccccccccccccccccccccc; /* comment */"));
2428 verifyFormat("void f(int * /* unused */) {}");
2458 TEST_F(FormatTestComments
, BlockCommentsInMacros
) {
2459 EXPECT_EQ("#define A \\\n"
2461 " /* one line */ \\\n"
2463 format("#define A { \\\n"
2464 " /* one line */ \\\n"
2466 getLLVMStyleWithColumns(20)));
2467 EXPECT_EQ("#define A \\\n"
2469 " /* previous */ \\\n"
2470 " /* one line */ \\\n"
2472 format("#define A { \\\n"
2473 " /* previous */ \\\n"
2474 " /* one line */ \\\n"
2476 getLLVMStyleWithColumns(20)));
2479 TEST_F(FormatTestComments
, BlockCommentsAtEndOfLine
) {
2483 format("a = {1111 /* */\n"
2485 getLLVMStyleWithColumns(15)));
2489 format("a = {1111 /* */\n"
2491 getLLVMStyleWithColumns(15)));
2496 format("a = {1111 /* a */\n"
2498 getLLVMStyleWithColumns(15)));
2501 TEST_F(FormatTestComments
, BreaksAfterMultilineBlockCommentsInParamLists
) {
2502 EXPECT_EQ("a = f(/* long\n"
2505 format("a = f(/* long long */ a);", getLLVMStyleWithColumns(16)));
2506 EXPECT_EQ("a = f(\n"
2510 format("a = f(/* long long */ a);", getLLVMStyleWithColumns(15)));
2512 EXPECT_EQ("a = f(/* long\n"
2516 format("a = f(/* long\n"
2519 getLLVMStyleWithColumns(16)));
2521 EXPECT_EQ("a = f(/* long\n"
2525 format("a = f(/* long\n"
2528 getLLVMStyleWithColumns(16)));
2530 EXPECT_EQ("a = f(/* long\n"
2534 format("a = f(/* long\n"
2537 getLLVMStyleWithColumns(16)));
2544 format("a = f(a, /* long long */ b);", getLLVMStyleWithColumns(16)));
2552 format("a = f(a, /* long long */ b);", getLLVMStyleWithColumns(15)));
2554 EXPECT_EQ("a = f(a,\n"
2558 format("a = f(a, /* long long */ (1 + 1));",
2559 getLLVMStyleWithColumns(16)));
2560 EXPECT_EQ("a = f(\n"
2565 format("a = f(a, /* long long */ (1 + 1));",
2566 getLLVMStyleWithColumns(15)));
2569 TEST_F(FormatTestComments
, IndentLineCommentsInStartOfBlockAtEndOfFile
) {
2575 TEST_F(FormatTestComments
, AlignTrailingComments
) {
2576 EXPECT_EQ("#define MACRO(V) \\\n"
2577 " V(Rt2) /* one more char */ \\\n"
2578 " V(Rs) /* than here */ \\\n"
2579 "/* comment 3 */\n",
2580 format("#define MACRO(V)\\\n"
2581 "V(Rt2) /* one more char */ \\\n"
2582 "V(Rs) /* than here */ \\\n"
2583 "/* comment 3 */\n",
2584 getLLVMStyleWithColumns(40)));
2585 EXPECT_EQ("int i = f(abc, // line 1\n"
2589 format("int i = f(abc, // line 1\n"
2593 getLLVMStyleWithColumns(40)));
2595 // Align newly broken trailing comments.
2596 EXPECT_EQ("int ab; // line\n"
2599 format("int ab; // line\n"
2600 "int a; // long long",
2601 getLLVMStyleWithColumns(15)));
2602 EXPECT_EQ("int ab; // line\n"
2606 format("int ab; // line\n"
2607 "int a; // long long\n"
2609 getLLVMStyleWithColumns(15)));
2610 EXPECT_EQ("int ab; // line\n"
2614 format("int ab; // line\n"
2615 "int a; // long long\n"
2617 getLLVMStyleWithColumns(15)));
2618 EXPECT_EQ("int ab; // line\n"
2623 format("int ab; // line\n"
2624 "int a; // long long\n"
2627 getLLVMStyleWithColumns(15)));
2629 // Don't align newly broken trailing comments if that would put them over the
2631 EXPECT_EQ("int i, j; // line 1\n"
2632 "int k; // line longg\n"
2634 format("int i, j; // line 1\n"
2635 "int k; // line longg long",
2636 getLLVMStyleWithColumns(20)));
2638 // Always align if ColumnLimit = 0
2639 EXPECT_EQ("int i, j; // line 1\n"
2640 "int k; // line longg long",
2641 format("int i, j; // line 1\n"
2642 "int k; // line longg long",
2643 getLLVMStyleWithColumns(0)));
2645 // Align comment line sections aligned with the next token with the next
2647 EXPECT_EQ("class A {\n"
2648 "public: // public comment\n"
2649 " // comment about a\n"
2652 format("class A {\n"
2653 "public: // public comment\n"
2654 " // comment about a\n"
2657 getLLVMStyleWithColumns(40)));
2658 EXPECT_EQ("class A {\n"
2659 "public: // public comment 1\n"
2660 " // public comment 2\n"
2661 " // comment 1 about a\n"
2662 " // comment 2 about a\n"
2665 format("class A {\n"
2666 "public: // public comment 1\n"
2667 " // public comment 2\n"
2668 " // comment 1 about a\n"
2669 " // comment 2 about a\n"
2672 getLLVMStyleWithColumns(40)));
2673 EXPECT_EQ("int f(int n) { // comment line 1 on f\n"
2674 " // comment line 2 on f\n"
2675 " // comment line 1 before return\n"
2676 " // comment line 2 before return\n"
2677 " return n; // comment line 1 on return\n"
2678 " // comment line 2 on return\n"
2679 " // comment line 1 after return\n"
2681 format("int f(int n) { // comment line 1 on f\n"
2682 " // comment line 2 on f\n"
2683 " // comment line 1 before return\n"
2684 " // comment line 2 before return\n"
2685 " return n; // comment line 1 on return\n"
2686 " // comment line 2 on return\n"
2687 " // comment line 1 after return\n"
2689 getLLVMStyleWithColumns(40)));
2690 EXPECT_EQ("int f(int n) {\n"
2691 " switch (n) { // comment line 1 on switch\n"
2692 " // comment line 2 on switch\n"
2693 " // comment line 1 before case 1\n"
2694 " // comment line 2 before case 1\n"
2695 " case 1: // comment line 1 on case 1\n"
2696 " // comment line 2 on case 1\n"
2697 " // comment line 1 before return 1\n"
2698 " // comment line 2 before return 1\n"
2699 " return 1; // comment line 1 on return 1\n"
2700 " // comment line 2 on return 1\n"
2701 " // comment line 1 before default\n"
2702 " // comment line 2 before default\n"
2703 " default: // comment line 1 on default\n"
2704 " // comment line 2 on default\n"
2705 " // comment line 1 before return 2\n"
2706 " return 2 * f(n - 1); // comment line 1 on return 2\n"
2707 " // comment line 2 on return 2\n"
2708 " // comment line 1 after return\n"
2709 " // comment line 2 after return\n"
2712 format("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"
2734 getLLVMStyleWithColumns(80)));
2736 // If all the lines in a sequence of line comments are aligned with the next
2737 // token, the first line belongs to the previous token and the other lines
2738 // belong to the next token.
2739 EXPECT_EQ("int a; // line about a\n"
2741 format("int a; // line about a\n"
2743 getLLVMStyleWithColumns(80)));
2744 EXPECT_EQ("int a; // line about a\n"
2747 format("int a; // line about a\n"
2748 " // line about b\n"
2750 getLLVMStyleWithColumns(80)));
2751 EXPECT_EQ("int a; // line about a\n"
2752 "// line 1 about b\n"
2753 "// line 2 about b\n"
2755 format("int a; // line about a\n"
2756 " // line 1 about b\n"
2757 " // line 2 about b\n"
2759 getLLVMStyleWithColumns(80)));
2761 // Checks an edge case in preprocessor handling.
2762 // These comments should *not* be aligned
2766 "long a; // Line about a\n"
2770 "long b_long_name; // Line about b\n"
2774 "long a; // Line about a\n" // Previous (bad) behavior
2778 "long b_long_name; // Line about b\n"
2780 getLLVMStyleWithColumns(80)));
2785 "#define FOO_GLOBAL 0 // Global scope.\n"
2786 "#define FOO_LINKLOCAL 1 // Link-local scope.\n"
2787 "#define FOO_SITELOCAL 2 // Site-local scope (deprecated).\n"
2788 "#define FOO_UNIQUELOCAL 3 // Unique local\n"
2789 "#define FOO_NODELOCAL 4 // Loopback\n\n"
2791 format("namespace m {\n\n"
2792 "#define FOO_GLOBAL 0 // Global scope.\n"
2793 "#define FOO_LINKLOCAL 1 // Link-local scope.\n"
2794 "#define FOO_SITELOCAL 2 // Site-local scope (deprecated).\n"
2795 "#define FOO_UNIQUELOCAL 3 // Unique local\n"
2796 "#define FOO_NODELOCAL 4 // Loopback\n\n"
2798 getLLVMStyleWithColumns(80)));
2800 // https://llvm.org/PR53441
2801 verifyFormat("/* */ //\n"
2803 verifyFormat("/**/ //\n"
2807 TEST_F(FormatTestComments
, AlignTrailingCommentsAcrossEmptyLines
) {
2808 FormatStyle Style
= getLLVMStyle();
2809 Style
.AlignTrailingComments
.Kind
= FormatStyle::TCAS_Always
;
2810 Style
.AlignTrailingComments
.OverEmptyLines
= 1;
2811 verifyFormat("#include \"a.h\" // simple\n"
2813 "#include \"aa.h\" // example case",
2816 verifyFormat("#include \"a.h\" // align across\n"
2818 "#include \"aa.h\" // two empty lines\n"
2820 "#include \"aaa.h\" // in a row",
2823 verifyFormat("#include \"a.h\" // align\n"
2824 "#include \"aa.h\" // comment\n"
2825 "#include \"aaa.h\" // blocks\n"
2827 "#include \"aaaa.h\" // across\n"
2828 "#include \"aaaaa.h\" // one\n"
2829 "#include \"aaaaaa.h\" // empty line",
2832 verifyFormat("#include \"a.h\" // align trailing comments\n"
2833 "#include \"a.h\"\n"
2834 "#include \"aa.h\" // across a line without comment",
2837 verifyFormat("#include \"a.h\" // align across\n"
2838 "#include \"a.h\"\n"
2839 "#include \"aa.h\" // two lines without comment\n"
2840 "#include \"a.h\"\n"
2841 "#include \"aaa.h\" // in a row",
2844 verifyFormat("#include \"a.h\" // align\n"
2845 "#include \"aa.h\" // comment\n"
2846 "#include \"aaa.h\" // blocks\n"
2847 "#include \"a.h\"\n"
2848 "#include \"aaaa.h\" // across\n"
2849 "#include \"aaaaa.h\" // a line without\n"
2850 "#include \"aaaaaa.h\" // comment",
2853 // Start of testing OverEmptyLines
2854 Style
.MaxEmptyLinesToKeep
= 3;
2855 Style
.AlignTrailingComments
.OverEmptyLines
= 2;
2856 // Cannot use verifyFormat here
2857 // test::messUp removes all new lines which changes the logic
2858 EXPECT_EQ("#include \"a.h\" // comment\n"
2862 "#include \"ab.h\" // comment\n"
2865 "#include \"abcdefg.h\" // comment",
2866 format("#include \"a.h\" // comment\n"
2870 "#include \"ab.h\" // comment\n"
2873 "#include \"abcdefg.h\" // comment",
2876 Style
.MaxEmptyLinesToKeep
= 1;
2877 Style
.AlignTrailingComments
.OverEmptyLines
= 1;
2878 // End of testing OverEmptyLines
2880 Style
.ColumnLimit
= 15;
2881 EXPECT_EQ("int ab; // line\n"
2886 format("int ab; // line\n"
2887 "int a; // long long\n"
2892 Style
.ColumnLimit
= 15;
2893 EXPECT_EQ("int ab; // line\n"
2897 format("int ab; // line\n"
2899 "int a; // long long",
2902 Style
.ColumnLimit
= 30;
2903 EXPECT_EQ("int foo = 12345; // comment\n"
2905 " 1234; // This is a very\n"
2906 " // long comment\n"
2907 " // which is wrapped\n"
2910 "int x = 2; // Is this still\n"
2912 format("int foo = 12345; // comment\n"
2913 "int bar = 1234; // This is a very long comment\n"
2914 " // which is wrapped arround.\n"
2916 "int x = 2; // Is this still aligned?",
2919 Style
.ColumnLimit
= 35;
2920 EXPECT_EQ("int foo = 12345; // comment\n"
2922 " 1234; // This is a very long\n"
2923 " // comment which is\n"
2924 " // wrapped arround.\n"
2927 " 2; // Is this still aligned?",
2928 format("int foo = 12345; // comment\n"
2929 "int bar = 1234; // This is a very long comment\n"
2930 " // which is wrapped arround.\n"
2932 "int x = 2; // Is this still aligned?",
2935 Style
.ColumnLimit
= 40;
2936 EXPECT_EQ("int foo = 12345; // comment\n"
2938 " 1234; // This is a very long comment\n"
2939 " // which is wrapped arround.\n"
2941 "int x = 2; // Is this still aligned?",
2942 format("int foo = 12345; // comment\n"
2943 "int bar = 1234; // This is a very long comment\n"
2944 " // which is wrapped arround.\n"
2946 "int x = 2; // Is this still aligned?",
2949 Style
.ColumnLimit
= 45;
2950 EXPECT_EQ("int foo = 12345; // comment\n"
2952 " 1234; // This is a very long comment\n"
2953 " // which is wrapped arround.\n"
2955 "int x = 2; // Is this still aligned?",
2956 format("int foo = 12345; // comment\n"
2957 "int bar = 1234; // This is a very long comment\n"
2958 " // which is wrapped arround.\n"
2960 "int x = 2; // Is this still aligned?",
2963 Style
.ColumnLimit
= 80;
2964 EXPECT_EQ("int a; // line about a\n"
2968 format("int a; // line about a\n"
2970 " // line about b\n"
2974 Style
.ColumnLimit
= 80;
2975 EXPECT_EQ("int a; // line about a\n"
2977 "// line 1 about b\n"
2978 "// line 2 about b\n"
2980 format("int a; // line about a\n"
2982 " // line 1 about b\n"
2983 " // line 2 about b\n"
2988 TEST_F(FormatTestComments
, AlignTrailingCommentsLeave
) {
2989 FormatStyle Style
= getLLVMStyle();
2990 Style
.AlignTrailingComments
.Kind
= FormatStyle::TCAS_Leave
;
2992 EXPECT_EQ("int a;// do not touch\n"
2993 "int b; // any comments\n"
2994 "int c; // comment\n"
2995 "int d; // comment",
2996 format("int a;// do not touch\n"
2997 "int b; // any comments\n"
2998 "int c; // comment\n"
2999 "int d; // comment",
3002 EXPECT_EQ("int a; // do not touch\n"
3003 "int b; // any comments\n"
3004 "int c; // comment\n"
3006 format("int a; // do not touch\n"
3007 "int b; // any comments\n"
3008 "int c; // comment\n"
3012 EXPECT_EQ("// do not touch\n"
3013 "int a; // any comments\n"
3019 format("// do not touch\n"
3020 "int a; // any comments\n"
3028 EXPECT_EQ("// do not touch\n"
3029 "int a; // any comments\n"
3035 format("// do not touch\n"
3036 "int a; // any comments\n"
3046 verifyFormat("namespace ns {\n"
3049 "} // namespace ns",
3056 Style
.AlignEscapedNewlines
= FormatStyle::ENAS_Left
;
3057 verifyNoChange("#define FOO \\\n"
3058 " /* foo(); */ \\\n"
3062 // Allow to keep 2 empty lines
3063 Style
.MaxEmptyLinesToKeep
= 2;
3064 EXPECT_EQ("// do not touch\n"
3065 "int a; // any comments\n"
3072 format("// do not touch\n"
3073 "int a; // any comments\n"
3081 Style
.MaxEmptyLinesToKeep
= 1;
3083 // Just format comments normally when leaving exceeds the column limit
3084 Style
.ColumnLimit
= 35;
3085 EXPECT_EQ("int foo = 12345; // comment\n"
3087 " 1234; // This is a very long\n"
3088 " // comment which is\n"
3089 " // wrapped arround.",
3090 format("int foo = 12345; // comment\n"
3091 "int bar = 1234; // This is a very long comment\n"
3092 " // which is wrapped arround.",
3096 TEST_F(FormatTestComments
, DontAlignNamespaceComments
) {
3097 FormatStyle Style
= getLLVMStyle();
3098 Style
.NamespaceIndentation
= FormatStyle::NI_All
;
3099 Style
.NamespaceMacros
.push_back("TESTSUITE");
3100 Style
.ShortNamespaceLines
= 0;
3102 StringRef Input
= "namespace A {\n"
3105 " namespace D { //\n"
3106 " } // namespace D\n"
3107 " std::string Foo = Bar; // Comment\n"
3108 " std::string BazString = Baz; // C2\n"
3109 " } // namespace C\n"
3113 EXPECT_TRUE(Style
.FixNamespaceComments
);
3114 EXPECT_EQ(Style
.AlignTrailingComments
.Kind
, FormatStyle::TCAS_Always
);
3115 verifyFormat("namespace A {\n"
3118 " namespace D { //\n"
3119 " } // namespace D\n"
3120 " std::string Foo = Bar; // Comment\n"
3121 " std::string BazString = Baz; // C2\n"
3122 " } // namespace C\n"
3123 " } // TESTSUITE(B)\n"
3127 Style
.AlignTrailingComments
.Kind
= FormatStyle::TCAS_Never
;
3128 verifyFormat("namespace A {\n"
3131 " namespace D { //\n"
3132 " } // namespace D\n"
3133 " std::string Foo = Bar; // Comment\n"
3134 " std::string BazString = Baz; // C2\n"
3135 " } // namespace C\n"
3136 " } // TESTSUITE(B)\n"
3140 Style
.AlignTrailingComments
.Kind
= FormatStyle::TCAS_Leave
;
3141 verifyFormat("namespace A {\n"
3144 " namespace D { //\n"
3145 " } // namespace D\n"
3146 " std::string Foo = Bar; // Comment\n"
3147 " std::string BazString = Baz; // C2\n"
3148 " } // namespace C\n"
3149 " } // TESTSUITE(B)\n"
3153 Style
.FixNamespaceComments
= false;
3154 Style
.AlignTrailingComments
.Kind
= FormatStyle::TCAS_Always
;
3155 verifyFormat("namespace A {\n"
3158 " namespace D { //\n"
3159 " } // namespace D\n"
3160 " std::string Foo = Bar; // Comment\n"
3161 " std::string BazString = Baz; // C2\n"
3162 " } // namespace C\n"
3167 Style
.AlignTrailingComments
.Kind
= FormatStyle::TCAS_Never
;
3168 verifyFormat("namespace A {\n"
3171 " namespace D { //\n"
3172 " } // namespace D\n"
3173 " std::string Foo = Bar; // Comment\n"
3174 " std::string BazString = Baz; // C2\n"
3175 " } // namespace C\n"
3180 Style
.AlignTrailingComments
.Kind
= FormatStyle::TCAS_Leave
;
3181 verifyFormat("namespace A {\n"
3184 " namespace D { //\n"
3185 " } // namespace D\n"
3186 " std::string Foo = Bar; // Comment\n"
3187 " std::string BazString = Baz; // C2\n"
3188 " } // namespace C\n"
3193 Style
.AlignTrailingComments
.Kind
= FormatStyle::TCAS_Always
;
3194 Style
.FixNamespaceComments
= true;
3195 Input
= "namespace A {\n"
3201 verifyFormat("namespace A {\n"
3204 "} // namespace A\n"
3208 Style
.FixNamespaceComments
= false;
3209 verifyFormat(Input
, Style
);
3212 TEST_F(FormatTestComments
, DontAlignOverScope
) {
3213 verifyFormat("if (foo) {\n"
3214 " int aLongVariable; // with comment\n"
3215 " int f; // aligned\n"
3216 "} // not aligned\n"
3217 "int bar; // new align\n"
3218 "int foobar; // group");
3220 verifyFormat("if (foo) {\n"
3223 " int aLongVariable; // with comment\n"
3224 " int f; // aligned\n"
3225 "} // not aligned\n"
3226 "int bar; // new align\n"
3227 "int foobar; // group");
3229 verifyFormat("if (foo) {\n"
3231 "} else if (foo) {\n"
3232 " int aLongVariable; // with comment\n"
3233 " int f; // aligned\n"
3234 "} // not aligned\n"
3235 "int bar; // new align\n"
3236 "int foobar; // group");
3238 verifyFormat("while (foo) {\n"
3239 " int aLongVariable; // with comment\n"
3240 " int f; // aligned\n"
3241 "} // not aligned\n"
3242 "int bar; // new align\n"
3243 "int foobar; // group");
3245 verifyFormat("for (;;) {\n"
3246 " int aLongVariable; // with comment\n"
3247 " int f; // aligned\n"
3248 "} // not aligned\n"
3249 "int bar; // new align\n"
3250 "int foobar; // group");
3252 verifyFormat("do {\n"
3253 " int aLongVariable; // with comment\n"
3254 " int f; // aligned\n"
3255 "} while (foo); // not aligned\n"
3256 "int bar; // new align\n"
3257 "int foobar; // group");
3260 " int aLongVariable; // with comment\n"
3261 "while (foo); // not aigned\n"
3262 "int bar; // new align\n"
3263 "int foobar; // group");
3266 " int aLongVariable; // with comment\n"
3267 "/**/ while (foo); // not aigned\n"
3268 "int bar; // new align\n"
3269 "int foobar; // group");
3271 verifyFormat("switch (foo) {\n"
3273 " int aLongVariable; // with comment\n"
3274 " int f; // aligned\n"
3275 "} // case not aligned\n"
3276 "} // switch also not aligned\n"
3277 "int bar; // new align\n"
3278 "int foobar; // group");
3280 verifyFormat("switch (foo) {\n"
3282 " int aLongVariable; // with comment\n"
3283 " int f; // aligned\n"
3284 "} // case not aligned\n"
3285 "} // switch also not aligned\n"
3286 "int bar; // new align\n"
3287 "int foobar; // group");
3289 verifyFormat("class C {\n"
3290 " int aLongVariable; // with comment\n"
3291 " int f; // aligned\n"
3292 "}; // not aligned\n"
3293 "int bar; // new align\n"
3294 "int foobar; // group");
3296 verifyFormat("struct S {\n"
3297 " int aLongVariable; // with comment\n"
3298 " int f; // aligned\n"
3299 "}; // not aligned\n"
3300 "int bar; // new align\n"
3301 "int foobar; // group");
3303 verifyFormat("union U {\n"
3304 " int aLongVariable; // with comment\n"
3305 " int f; // aligned\n"
3306 "}; // not aligned\n"
3307 "int bar; // new align\n"
3308 "int foobar; // group");
3310 verifyFormat("enum E {\n"
3311 " aLongVariable, // with comment\n"
3313 "}; // not aligned\n"
3314 "int bar; // new align\n"
3315 "int foobar; // group");
3317 verifyFormat("void foo() {\n"
3319 " int aLongVariable; // with comment\n"
3320 " int f; // aligned\n"
3321 " } // not aligned\n"
3322 " int bar; // new align\n"
3323 " int foobar; // group\n"
3326 verifyFormat("auto longLambda = [] { // comment\n"
3327 " int aLongVariable; // with comment\n"
3328 " int f; // aligned\n"
3329 "}; // not aligned\n"
3330 "int bar; // new align\n"
3331 "int foobar; // group\n"
3332 "auto shortLambda = [] { return 5; }; // aligned");
3334 verifyFormat("auto longLambdaResult = [] { // comment\n"
3335 " int aLongVariable; // with comment\n"
3336 " int f; // aligned\n"
3337 "}(); // not aligned\n"
3338 "int bar; // new align\n"
3339 "int foobar; // group\n"
3340 "auto shortLambda = [] { return 5; }(); // aligned");
3343 "auto longLambdaResult = [](auto I, auto J) { // comment\n"
3344 " int aLongVariable; // with comment\n"
3345 " int f; // aligned\n"
3346 "}(\"Input\", 5); // not aligned\n"
3347 "int bar; // new align\n"
3348 "int foobar; // group\n"
3349 "auto shortL = [](auto I, auto J) { return 5; }(\"In\", 5); // aligned");
3351 verifyFormat("enum E1 { V1, V2 }; // Aligned\n"
3352 "enum E2 { LongerNames, InThis, Enum }; // Comments");
3354 verifyFormat("class C {\n"
3355 " int aLongVariable; // with comment\n"
3356 " int f; // aligned\n"
3357 "} /* middle comment */; // not aligned\n"
3358 "int bar; // new align\n"
3359 "int foobar; // group");
3362 TEST_F(FormatTestComments
, AlignsBlockCommentDecorations
) {
3376 // Align a single line.
3414 // Align the end '*/' after a line.
3435 EXPECT_EQ("/* line 1\n"
3437 format("/* line 1\n"
3439 EXPECT_EQ("/* line 1\n"
3441 format("/* line 1\n"
3443 EXPECT_EQ("/* line 1\n"
3445 format("/* line 1\n"
3447 EXPECT_EQ("/* line 1\n"
3449 format("/* line 1\n"
3451 EXPECT_EQ("/* line 1\n"
3453 format("/* line 1\n"
3455 EXPECT_EQ("int i; /* line 1\n"
3457 format("int i; /* line 1\n"
3459 EXPECT_EQ("int i; /* line 1\n"
3461 format("int i; /* line 1\n"
3463 EXPECT_EQ("int i; /* line 1\n"
3465 format("int i; /* line 1\n"
3468 // Align several lines.
3469 EXPECT_EQ("/* line 1\n"
3472 format("/* line 1\n"
3475 EXPECT_EQ("/* line 1\n"
3478 format("/* line 1\n"
3490 // Align with different indent after the decorations.
3504 // Align empty or blank lines.
3516 // Align while breaking and reflowing.
3518 " * long long long\n"
3523 " * long long long long\n"
3527 getLLVMStyleWithColumns(20)));
3530 TEST_F(FormatTestComments
, NoCrash_Bug34236
) {
3531 // This is a test case from a crasher reported in:
3532 // https://bugs.llvm.org/show_bug.cgi?id=34236
3533 // Temporarily disable formatting for readability.
3543 getLLVMStyleWithColumns(80)));
3547 TEST_F(FormatTestComments
, NonTrailingBlockComments
) {
3548 verifyFormat("const /** comment comment */ A = B;",
3549 getLLVMStyleWithColumns(40));
3551 verifyFormat("const /** comment comment comment */ A =\n"
3553 getLLVMStyleWithColumns(40));
3555 EXPECT_EQ("const /** comment comment comment\n"
3558 format("const /** comment comment comment comment */\n"
3560 getLLVMStyleWithColumns(40)));
3563 TEST_F(FormatTestComments
, PythonStyleComments
) {
3564 // Keeps a space after '#'.
3565 EXPECT_EQ("# comment\n"
3569 getTextProtoStyleWithColumns(20)));
3570 EXPECT_EQ("# comment\n"
3572 format("# comment\n"
3574 getTextProtoStyleWithColumns(20)));
3575 // Breaks long comment.
3576 EXPECT_EQ("# comment comment\n"
3579 format("# comment comment comment\n"
3581 getTextProtoStyleWithColumns(20)));
3582 // Indents comments.
3583 EXPECT_EQ("data {\n"
3584 " # comment comment\n"
3589 "# comment comment comment\n"
3591 getTextProtoStyleWithColumns(20)));
3592 EXPECT_EQ("data {\n"
3593 " # comment comment\n"
3597 format("data {# comment comment comment\n"
3599 getTextProtoStyleWithColumns(20)));
3600 // Reflows long comments.
3601 EXPECT_EQ("# comment comment\n"
3602 "# comment comment\n"
3604 format("# comment comment comment\n"
3607 getTextProtoStyleWithColumns(20)));
3608 // Breaks trailing comments.
3609 EXPECT_EQ("k: val # comment\n"
3612 format("k:val#comment comment\n"
3614 getTextProtoStyleWithColumns(20)));
3616 " k: val # comment\n"
3621 format("id {k:val#comment comment\n"
3624 getTextProtoStyleWithColumns(20)));
3625 // Aligns trailing comments.
3626 EXPECT_EQ("k: val # commen1\n"
3633 format("k:val#commen1 commen2\n"
3636 "a:1#commen5 commen6\n"
3638 getTextProtoStyleWithColumns(20)));
3641 TEST_F(FormatTestComments
, BreaksBeforeTrailingUnbreakableSequence
) {
3642 // The end of /* trail */ is exactly at 80 columns, but the unbreakable
3643 // trailing sequence ); after it exceeds the column limit. Make sure we
3644 // correctly break the line in that case.
3645 verifyFormat("int a =\n"
3646 " foo(/* trail */);",
3647 getLLVMStyleWithColumns(23));
3650 TEST_F(FormatTestComments
, ReflowBackslashCrash
) {
3655 "// rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr\n"
3656 "// \\ <log_file> -- --output_directory=\"<output_directory>\"",
3660 "// rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr \\\n"
3661 "// <log_file> -- --output_directory=\"<output_directory>\""));
3665 TEST_F(FormatTestComments
, IndentsLongJavadocAnnotatedLines
) {
3666 FormatStyle Style
= getGoogleStyle(FormatStyle::LK_Java
);
3667 Style
.ColumnLimit
= 60;
3668 FormatStyle Style20
= getGoogleStyle(FormatStyle::LK_Java
);
3669 Style20
.ColumnLimit
= 20;
3672 " * @param x long long long long long long long long long\n"
3676 " * @param x long long long long long long long long long long\n"
3680 " * @param x long long long long long long long long long\n"
3681 " * long long long long long long long long long long\n"
3684 " * @param x long long long long long long long long long "
3685 "long long long long long long long long long long\n"
3689 " * @param x long long long long long long long long long\n"
3690 " * long long long long long long long long long long\n"
3694 " * @param x long long long long long long long long long "
3695 "long long long long long long long long long long long\n"
3699 " * Sentence that\n"
3700 " * should be broken.\n"
3702 " * keep indentation\n"
3705 " * Sentence that should be broken.\n"
3707 " * keep indentation\n"
3712 " * @param l1 long1\n"
3714 " * @param l2 long2\n"
3718 " * @param l1 long1 to break\n"
3719 " * @param l2 long2 to break\n"
3729 " * @param xx to break\n"
3740 " * @param xx to break\n"
3745 FormatStyle JSStyle20
= getGoogleStyle(FormatStyle::LK_JavaScript
);
3746 JSStyle20
.ColumnLimit
= 20;
3748 " * @param l1 long1\n"
3752 " * @param l1 long1 to break\n"
3756 " * @param {l1 long1\n"
3760 " * @param {l1 long1 to break}\n"
3765 TEST_F(FormatTestComments
, SpaceAtLineCommentBegin
) {
3766 FormatStyle Style
= getLLVMStyle();
3767 StringRef NoTextInComment
= " // \n"
3778 format(NoTextInComment
, Style
));
3780 Style
.SpacesInLineCommentPrefix
.Minimum
= 0;
3781 verifyFormat("//#comment", Style
);
3787 format(NoTextInComment
, Style
));
3789 Style
.SpacesInLineCommentPrefix
.Minimum
= 5;
3790 EXPECT_EQ("// #comment", format("//#comment", Style
));
3796 format(NoTextInComment
, Style
));
3798 Style
= getLLVMStyle();
3800 "//Free comment without space\n"
3802 "// Free comment with 3 spaces\n"
3804 "///Free Doxygen without space\n"
3806 "/// Free Doxygen with 3 spaces\n"
3808 "//🐉 A nice dragon\n"
3814 "// 🐉 Another nice dragon\n"
3816 "// \t Three leading spaces following tab\n"
3818 "// \\t Three leading spaces following backslash\n"
3820 "/// A Doxygen Comment with a nested list:\n"
3825 "/// of the inner list\n"
3830 "bool bar(bool b) {\n"
3831 " bool ret1 = true; ///<Doxygenstyle without space\n"
3832 " bool ret2 = true; ///< Doxygenstyle with 3 spaces\n"
3836 " // In function comment\n"
3840 "// if (ret1) {\n" // Commented out at the beginning of the line
3844 " //if (ret1) {\n" // Commtented out at the beginning of the content
3845 " // return ret2;\n"
3848 " return ret1 && ret2;\n"
3854 "} // namespace Bar\n"
3855 "//@Nothing added because of the non ascii char\n"
3857 "//@ Nothing removed because of the non ascii char\n"
3859 "// Comment to move to the left\n"
3863 "//Comment to move to the right\n"
3866 "//} will not move\n"
3868 "//vv will only move\n"
3869 "//} if the line above does";
3871 EXPECT_EQ("// Free comment without space\n"
3873 "// Free comment with 3 spaces\n"
3875 "/// Free Doxygen without space\n"
3877 "/// Free Doxygen with 3 spaces\n"
3879 "// 🐉 A nice dragon\n"
3885 "// 🐉 Another nice dragon\n"
3887 "// \t Three leading spaces following tab\n"
3889 "// \\t Three leading spaces following backslash\n"
3891 "/// A Doxygen Comment with a nested list:\n"
3896 "/// of the inner list\n"
3901 "bool bar(bool b) {\n"
3902 " bool ret1 = true; ///< Doxygenstyle without space\n"
3903 " bool ret2 = true; ///< Doxygenstyle with 3 spaces\n"
3907 " // In function comment\n"
3912 " // return ret2;\n"
3916 " // return ret2;\n"
3919 " return ret1 && ret2;\n"
3921 "} // namespace Foo\n"
3925 "} // namespace Bar\n"
3926 "//@Nothing added because of the non ascii char\n"
3928 "//@ Nothing removed because of the non ascii char\n"
3930 "// Comment to move to the left\n"
3931 "// But not this?\n"
3934 "// Comment to move to the right\n"
3937 "//} will not move\n"
3939 "// vv will only move\n"
3940 "// } if the line above does",
3941 format(Code
, Style
));
3943 Style
.SpacesInLineCommentPrefix
= {0, 0};
3944 EXPECT_EQ("//#comment", format("// #comment", Style
));
3945 EXPECT_EQ("//Free comment without space\n"
3947 "//Free comment with 3 spaces\n"
3949 "///Free Doxygen without space\n"
3951 "///Free Doxygen with 3 spaces\n"
3953 "//🐉 A nice dragon\n"
3959 "//🐉 Another nice dragon\n"
3961 "//\t Three leading spaces following tab\n"
3963 "//\\t Three leading spaces following backslash\n"
3965 "///A Doxygen Comment with a nested list:\n"
3968 "/// - Baz\n" // Here we keep the relative indentation
3970 "/// of the inner list\n"
3975 "bool bar(bool b) {\n"
3976 " bool ret1 = true; ///<Doxygenstyle without space\n"
3977 " bool ret2 = true; ///<Doxygenstyle with 3 spaces\n"
3981 " //In function comment\n"
3986 " // return ret2;\n"
3990 " // return ret2;\n"
3993 " return ret1 && ret2;\n"
3995 "} //namespace Foo\n"
3999 "} //namespace Bar\n"
4000 "//@Nothing added because of the non ascii char\n"
4002 "//@ Nothing removed because of the non ascii char\n"
4004 "//Comment to move to the left\n"
4008 "//Comment to move to the right\n"
4011 "//} will not move\n"
4013 "//vv will only move\n"
4014 "//} if the line above does",
4015 format(Code
, Style
));
4017 Style
.SpacesInLineCommentPrefix
= {2, -1u};
4018 EXPECT_EQ("// Free comment without space\n"
4020 "// Free comment with 3 spaces\n"
4022 "/// Free Doxygen without space\n"
4024 "/// Free Doxygen with 3 spaces\n"
4026 "// 🐉 A nice dragon\n"
4032 "// 🐉 Another nice dragon\n"
4034 "// \t Three leading spaces following tab\n"
4036 "// \\t Three leading spaces following backslash\n"
4038 "/// A Doxygen Comment with a nested list:\n"
4043 "/// of the inner list\n"
4048 "bool bar(bool b) {\n"
4049 " bool ret1 = true; ///< Doxygenstyle without space\n"
4050 " bool ret2 = true; ///< Doxygenstyle with 3 spaces\n"
4054 " // In function comment\n"
4059 " // return ret2;\n"
4063 " // return ret2;\n"
4066 " return ret1 && ret2;\n"
4068 "} // namespace Foo\n"
4072 "} // namespace Bar\n"
4073 "//@Nothing added because of the non ascii char\n"
4075 "//@ Nothing removed because of the non ascii char\n"
4077 "// Comment to move to the left\n"
4078 "// But not this?\n"
4081 "// Comment to move to the right\n"
4084 "//} will not move\n"
4086 "// vv will only move\n"
4087 "// } if the line above does",
4088 format(Code
, Style
));
4090 Style
= getLLVMStyleWithColumns(20);
4091 StringRef WrapCode
= "//Lorem ipsum dolor sit amet\n"
4093 "// Lorem ipsum dolor sit amet\n"
4095 "void f() {//Hello World\n"
4098 EXPECT_EQ("// Lorem ipsum dolor\n"
4102 "// dolor sit amet\n"
4104 "void f() { // Hello\n"
4107 format(WrapCode
, Style
));
4109 Style
.SpacesInLineCommentPrefix
= {0, 0};
4110 EXPECT_EQ("//Lorem ipsum dolor\n"
4114 "//dolor sit amet\n"
4116 "void f() { //Hello\n"
4119 format(WrapCode
, Style
));
4121 Style
.SpacesInLineCommentPrefix
= {1, 1};
4122 EXPECT_EQ("// Lorem ipsum dolor\n"
4126 "// dolor sit amet\n"
4128 "void f() { // Hello\n"
4131 format(WrapCode
, Style
));
4138 "// loooooooooooooooooooooooooooooong\n"
4139 "// commentcomments\n"
4140 "// normal comments",
4141 format("// loooooooooooooooooooooooooooooong commentcomments\n"
4142 "// normal comments",
4145 Style
.SpacesInLineCommentPrefix
= {3, 3};
4146 EXPECT_EQ("// Lorem ipsum\n"
4147 "// dolor sit amet\n"
4153 "void f() { // Hello\n"
4156 format(WrapCode
, Style
));
4158 Style
= getLLVMStyleWithColumns(20);
4159 StringRef LotsOfSpaces
= "// This are more spaces "
4160 "than the ColumnLimit, what now?\n"
4164 "// This is a text to split in multiple "
4165 "lines, please. Thank you very much!\n"
4167 "// A comment with\n"
4168 "// some indentation that has to be split.\n"
4169 "// And now without";
4170 EXPECT_EQ("// This are more spaces "
4171 "than the ColumnLimit, what now?\n"
4175 "// This is a text to\n"
4176 "// split in multiple\n"
4177 "// lines, please.\n"
4178 "// Thank you very\n"
4181 "// A comment with\n"
4184 "// that has to be\n"
4186 "// And now without",
4187 format(LotsOfSpaces
, Style
));
4189 Style
.SpacesInLineCommentPrefix
= {0, 0};
4190 EXPECT_EQ("//This are more\n"
4191 "//spaces than the\n"
4192 "//ColumnLimit, what\n"
4197 "//This is a text to\n"
4198 "//split in multiple\n"
4199 "//lines, please.\n"
4200 "//Thank you very\n"
4203 "//A comment with\n"
4204 "// some indentation\n"
4205 "// that has to be\n"
4207 "//And now without",
4208 format(LotsOfSpaces
, Style
));
4210 Style
.SpacesInLineCommentPrefix
= {3, 3};
4211 EXPECT_EQ("// This are more\n"
4212 "// spaces than the\n"
4218 "// This is a text\n"
4220 "// multiple lines,\n"
4221 "// please. Thank\n"
4222 "// you very much!\n"
4224 "// A comment with\n"
4229 "// And now without",
4230 format(LotsOfSpaces
, Style
));
4232 Style
.SpacesInLineCommentPrefix
= {30, -1u};
4233 EXPECT_EQ("// This are more spaces than the "
4234 "ColumnLimit, what now?\n"
4238 "// This is a text to split in "
4239 "multiple lines, please. Thank you very much!\n"
4241 "// A comment with\n"
4242 "// some indentation that has to be "
4244 "// And now without",
4245 format(LotsOfSpaces
, Style
));
4247 Style
.SpacesInLineCommentPrefix
= {2, 4};
4248 EXPECT_EQ("// A Comment to be\n"
4252 "// A Comment to be\n"
4256 "// A Comment to be\n"
4260 "// A Comment to be\n"
4275 format("//A Comment to be moved\n"
4278 "// A Comment to be moved\n"
4281 "// A Comment to be moved\n"
4284 "// A Comment to be moved\n"
4287 "// A Comment to be moved\n"
4290 "// A Comment to be moved\n"
4293 "// A Comment to be moved\n"
4297 Style
.ColumnLimit
= 30;
4298 EXPECT_EQ("int i; // A Comment to be\n"
4302 "int i; // A Comment to be\n"
4306 "int i; // A Comment to be\n"
4310 "int i; // A Comment to be\n"
4314 "int i; // A Comment to be\n"
4318 "int i; // A Comment to be\n"
4322 "int i; // A Comment to be\n"
4325 format("int i;//A Comment to be moved\n"
4328 "int i;// A Comment to be moved\n"
4331 "int i;// A Comment to be moved\n"
4334 "int i;// A Comment to be moved\n"
4337 "int i;// A Comment to be moved\n"
4340 "int i;// A Comment to be moved\n"
4343 "int i;// A Comment to be moved\n"
4347 Style
= getLLVMStyleWithColumns(0);
4348 EXPECT_EQ("// Free comment without space\n"
4350 "// Free comment with 3 spaces\n"
4352 "/// Free Doxygen without space\n"
4354 "/// Free Doxygen with 3 spaces\n"
4356 "// 🐉 A nice dragon\n"
4362 "// 🐉 Another nice dragon\n"
4364 "// \t Three leading spaces following tab\n"
4366 "// \\t Three leading spaces following backslash\n"
4368 "/// A Doxygen Comment with a nested list:\n"
4373 "/// of the inner list\n"
4378 "bool bar(bool b) {\n"
4379 " bool ret1 = true; ///< Doxygenstyle without space\n"
4380 " bool ret2 = true; ///< Doxygenstyle with 3 spaces\n"
4384 " // In function comment\n"
4389 " // return ret2;\n"
4393 " // return ret2;\n"
4396 " return ret1 && ret2;\n"
4398 "} // namespace Foo\n"
4402 "} // namespace Bar\n"
4403 "//@Nothing added because of the non ascii char\n"
4405 "//@ Nothing removed because of the non ascii char\n"
4407 "// Comment to move to the left\n"
4408 "// But not this?\n"
4411 "// Comment to move to the right\n"
4414 "//} will not move\n"
4416 "// vv will only move\n"
4417 "// } if the line above does",
4418 format(Code
, Style
));
4420 Style
.SpacesInLineCommentPrefix
= {0, 0};
4421 EXPECT_EQ("//Free comment without space\n"
4423 "//Free comment with 3 spaces\n"
4425 "///Free Doxygen without space\n"
4427 "///Free Doxygen with 3 spaces\n"
4429 "//🐉 A nice dragon\n"
4435 "//🐉 Another nice dragon\n"
4437 "//\t Three leading spaces following tab\n"
4439 "//\\t Three leading spaces following backslash\n"
4441 "///A Doxygen Comment with a nested list:\n"
4444 "/// - Baz\n" // Here we keep the relative indentation
4446 "/// of the inner list\n"
4451 "bool bar(bool b) {\n"
4452 " bool ret1 = true; ///<Doxygenstyle without space\n"
4453 " bool ret2 = true; ///<Doxygenstyle with 3 spaces\n"
4457 " //In function comment\n"
4462 " // return ret2;\n"
4466 " // return ret2;\n"
4469 " return ret1 && ret2;\n"
4471 "} //namespace Foo\n"
4475 "} //namespace Bar\n"
4476 "//@Nothing added because of the non ascii char\n"
4478 "//@ Nothing removed because of the non ascii char\n"
4480 "//Comment to move to the left\n"
4484 "//Comment to move to the right\n"
4487 "//} will not move\n"
4489 "//vv will only move\n"
4490 "//} if the line above does",
4491 format(Code
, Style
));
4493 Style
.SpacesInLineCommentPrefix
= {2, -1u};
4494 EXPECT_EQ("// Free comment without space\n"
4496 "// Free comment with 3 spaces\n"
4498 "/// Free Doxygen without space\n"
4500 "/// Free Doxygen with 3 spaces\n"
4502 "// 🐉 A nice dragon\n"
4508 "// 🐉 Another nice dragon\n"
4510 "// \t Three leading spaces following tab\n"
4512 "// \\t Three leading spaces following backslash\n"
4514 "/// A Doxygen Comment with a nested list:\n"
4519 "/// of the inner list\n"
4524 "bool bar(bool b) {\n"
4525 " bool ret1 = true; ///< Doxygenstyle without space\n"
4526 " bool ret2 = true; ///< Doxygenstyle with 3 spaces\n"
4530 " // In function comment\n"
4535 " // return ret2;\n"
4539 " // return ret2;\n"
4542 " return ret1 && ret2;\n"
4544 "} // namespace Foo\n"
4548 "} // namespace Bar\n"
4549 "//@Nothing added because of the non ascii char\n"
4551 "//@ Nothing removed because of the non ascii char\n"
4553 "// Comment to move to the left\n"
4554 "// But not this?\n"
4557 "// Comment to move to the right\n"
4560 "//} will not move\n"
4562 "// vv will only move\n"
4563 "// } if the line above does",
4564 format(Code
, Style
));
4567 TEST_F(FormatTestComments
, SplitCommentIntroducers
) {
4576 getLLVMStyleWithColumns(10)));
4581 } // end namespace format
4582 } // end namespace clang