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"
366 FormatStyle BreakAlways
= getLLVMStyle();
367 BreakAlways
.BinPackParameters
= FormatStyle::BPPS_AlwaysOnePerLine
;
368 verifyFormat("int SomeFunction(a,\n"
373 verifyFormat("int SomeFunction(a,\n"
380 TEST_F(FormatTestComments
, RemovesTrailingWhitespaceOfComments
) {
381 EXPECT_EQ("// comment", format("// comment "));
382 EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment",
383 format("int aaaaaaa, bbbbbbb; // comment ",
384 getLLVMStyleWithColumns(33)));
385 EXPECT_EQ("// comment\\\n", format("// comment\\\n \t \v \f "));
386 EXPECT_EQ("// comment \\\n", format("// comment \\\n \t \v \f "));
389 TEST_F(FormatTestComments
, UnderstandsBlockComments
) {
390 verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);");
391 verifyFormat("void f() { g(/*aaa=*/x, /*bbb=*/!y, /*c=*/::c); }");
392 verifyFormat("fooooooooooooooooooooooooooooo(\n"
393 " /*qq_=*/move(q), [this, b](bar<void(uint32_t)> b) {},\n"
395 getLLVMStyleWithColumns(60));
396 EXPECT_EQ("f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n"
397 " bbbbbbbbbbbbbbbbbbbbbbbbb);",
398 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \\\n"
399 "/* Trailing comment for aa... */\n"
400 " bbbbbbbbbbbbbbbbbbbbbbbbb);"));
402 "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
403 " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);",
404 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n"
405 "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);"));
408 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
409 " aaaaaaaaaaaaaaaaaa,\n"
410 " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/ }",
411 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
412 " aaaaaaaaaaaaaaaaaa ,\n"
413 " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
416 verifyFormat("f(/* aaaaaaaaaaaaaaaaaa = */\n"
417 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
420 "int aaaaaaaaaaaaa(/* 1st */ int bbbbbbbbbb, /* 2nd */ int ccccccccccc,\n"
421 " /* 3rd */ int dddddddddddd);");
423 auto Style
= getLLVMStyle();
424 Style
.BinPackParameters
= FormatStyle::BPPS_OnePerLine
;
425 verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n"
426 " /* parameter 2 */ aaaaaa,\n"
427 " /* parameter 3 */ aaaaaa,\n"
428 " /* parameter 4 */ aaaaaa);",
430 verifyFormat("int a(/* 1st */ int b, /* 2nd */ int c);", Style
);
431 verifyFormat("int aaaaaaaaaaaaa(/* 1st */ int bbbbbbbbbb,\n"
432 " /* 2nd */ int ccccccccccc,\n"
433 " /* 3rd */ int dddddddddddd);",
436 Style
.BinPackParameters
= FormatStyle::BPPS_AlwaysOnePerLine
;
437 verifyFormat("int a(/* 1st */ int b,\n"
438 " /* 2nd */ int c);",
441 // Aligning block comments in macros.
442 verifyGoogleFormat("#define A \\\n"
447 TEST_F(FormatTestComments
, AlignsBlockComments
) {
449 " * Really multi-line\n"
454 " * Really multi-line\n"
458 EXPECT_EQ("class C {\n"
460 " * Another multi-line\n"
467 " * Another multi-line\n"
473 " 1. This is a comment with non-trivial formatting.\n"
474 " 1.1. We have to indent/outdent all lines equally\n"
475 " 1.1.1. to keep the formatting.\n"
478 " 1. This is a comment with non-trivial formatting.\n"
479 " 1.1. We have to indent/outdent all lines equally\n"
480 " 1.1.1. to keep the formatting.\n"
483 "Don't try to outdent if there's not enough indentation.\n"
486 " Don't try to outdent if there's not enough indentation.\n"
489 EXPECT_EQ("int i; /* Comment with empty...\n"
492 format("int i; /* Comment with empty...\n"
495 EXPECT_EQ("int foobar = 0; /* comment */\n"
496 "int bar = 0; /* multiline\n"
498 "int baz = 0; /* multiline\n"
500 "int bzz = 0; /* multiline\n"
502 format("int foobar = 0; /* comment */\n"
503 "int bar = 0; /* multiline\n"
505 "int baz = 0; /* multiline\n"
507 "int bzz = 0; /* multiline\n"
509 EXPECT_EQ("int foobar = 0; /* comment */\n"
510 "int bar = 0; /* multiline\n"
512 "int baz = 0; /* multiline\n"
514 format("int foobar = 0; /* comment */\n"
515 "int bar = 0; /* multiline\n"
517 "int baz = 0; /* multiline\n"
521 TEST_F(FormatTestComments
, CommentReflowingCanBeTurnedOff
) {
522 FormatStyle Style
= getLLVMStyleWithColumns(20);
523 Style
.ReflowComments
= FormatStyle::RCS_Never
;
524 verifyFormat("// aaaaaaaaa aaaaaaaaaa aaaaaaaaaa", Style
);
525 verifyFormat("/* aaaaaaaaa aaaaaaaaaa aaaaaaaaaa */", Style
);
526 verifyNoChange("/* aaaaaaaaa aaaaaaaaaa aaaaaaaaaa\n"
529 verifyNoChange("/* aaaaaaaaa aaaaaaaaaa aaaaaaaaaa\n"
532 verifyNoChange("/* aaaaaaaaa aaaaaaaaaa aaaaaaaaaa\n"
537 TEST_F(FormatTestComments
, CommentReflowingCanApplyOnlyToIndents
) {
538 FormatStyle Style
= getLLVMStyleWithColumns(20);
539 Style
.ReflowComments
= FormatStyle::RCS_IndentOnly
;
540 verifyFormat("// aaaaaaaaa aaaaaaaaaa aaaaaaaaaa", Style
);
541 verifyFormat("/* aaaaaaaaa aaaaaaaaaa aaaaaaaaaa */", Style
);
542 verifyNoChange("/* aaaaaaaaa aaaaaaaaaa aaaaaaaaaa\n"
545 verifyNoChange("/* aaaaaaaaa aaaaaaaaaa aaaaaaaaaa\n"
548 verifyFormat("/* aaaaaaaaa aaaaaaaaaa aaaaaaaaaa\n"
550 "/* aaaaaaaaa aaaaaaaaaa aaaaaaaaaa\n"
555 TEST_F(FormatTestComments
, CorrectlyHandlesLengthOfBlockComments
) {
556 EXPECT_EQ("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
557 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */",
558 format("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
559 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */"));
561 "void ffffffffffff(\n"
562 " int aaaaaaaa, int bbbbbbbb,\n"
563 " int cccccccccccc) { /*\n"
570 format("void ffffffffffff(int aaaaaaaa, int bbbbbbbb, int cccccccccccc)\n"
572 " aaaaaaaaaa aaaaaaaaaaaaa\n"
573 " bbbbbbbbbbbbbb bbbbbbbbbb\n"
576 getLLVMStyleWithColumns(40)));
579 TEST_F(FormatTestComments
, DontBreakNonTrailingBlockComments
) {
580 EXPECT_EQ("void ffffffffff(\n"
581 " int aaaaa /* test */);",
582 format("void ffffffffff(int aaaaa /* test */);",
583 getLLVMStyleWithColumns(35)));
586 TEST_F(FormatTestComments
, SplitsLongCxxComments
) {
587 EXPECT_EQ("// A comment that\n"
588 "// doesn't fit on\n"
590 format("// A comment that doesn't fit on one line",
591 getLLVMStyleWithColumns(20)));
592 EXPECT_EQ("/// A comment that\n"
593 "/// doesn't fit on\n"
595 format("/// A comment that doesn't fit on one line",
596 getLLVMStyleWithColumns(20)));
597 EXPECT_EQ("//! A comment that\n"
598 "//! doesn't fit on\n"
600 format("//! A comment that doesn't fit on one line",
601 getLLVMStyleWithColumns(20)));
602 EXPECT_EQ("// a b c d\n"
605 format("// a b c d e f g h i j k", getLLVMStyleWithColumns(10)));
610 format("\\\n// a b c d e f g h i j k", getLLVMStyleWithColumns(10)));
611 EXPECT_EQ("if (true) // A comment that\n"
612 " // doesn't fit on\n"
614 format("if (true) // A comment that doesn't fit on one line ",
615 getLLVMStyleWithColumns(30)));
616 verifyNoChange("// Don't_touch_leading_whitespace",
617 getLLVMStyleWithColumns(20));
618 EXPECT_EQ("// Add leading\n"
620 format("//Add leading whitespace", getLLVMStyleWithColumns(20)));
621 EXPECT_EQ("/// Add leading\n"
623 format("///Add leading whitespace", getLLVMStyleWithColumns(20)));
624 EXPECT_EQ("//! Add leading\n"
626 format("//!Add leading whitespace", getLLVMStyleWithColumns(20)));
627 EXPECT_EQ("// whitespace", format("//whitespace"));
628 EXPECT_EQ("// Even if it makes the line exceed the column\n"
630 format("//Even if it makes the line exceed the column limit",
631 getLLVMStyleWithColumns(51)));
632 verifyFormat("//--But not here");
633 EXPECT_EQ("/// line 1\n"
634 "// add leading whitespace",
635 format("/// line 1\n"
636 "//add leading whitespace",
637 getLLVMStyleWithColumns(30)));
638 EXPECT_EQ("/// line 1\n"
652 getLLVMStyleWithColumns(20)));
654 EXPECT_EQ("// aa bb cc dd",
655 format("// aa bb cc dd ",
656 getLLVMStyleWithColumns(15)));
658 EXPECT_EQ("// A comment before\n"
662 format("// A comment before a macro definition\n"
664 getLLVMStyleWithColumns(20)));
665 EXPECT_EQ("void ffffff(\n"
666 " int aaaaaaaaa, // wwww\n"
667 " int bbbbbbbbbb, // xxxxxxx\n"
669 " int c, int d, int e) {}",
670 format("void ffffff(\n"
671 " int aaaaaaaaa, // wwww\n"
672 " int bbbbbbbbbb, // xxxxxxx yyyyyyyyyy\n"
673 " int c, int d, int e) {}",
674 getLLVMStyleWithColumns(40)));
675 verifyFormat("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
676 getLLVMStyleWithColumns(20));
678 "#define XXX // a b c d\n"
680 format("#define XXX // a b c d e f g h", getLLVMStyleWithColumns(22)));
682 "#define XXX // q w e r\n"
684 format("#define XXX //q w e r t y u i", getLLVMStyleWithColumns(22)));
688 " // long 1 2 3 4 5\n"
693 " // long 1 2 3 4 5\n"
695 getLLVMStyleWithColumns(20)));
699 " // long 1 2 3 4 5\n"
705 " // long 1 2 3 4 5 6\n"
707 getLLVMStyleWithColumns(20)));
709 EXPECT_EQ("//: A comment that\n"
710 "//: doesn't fit on\n"
712 format("//: A comment that doesn't fit on one line",
713 getLLVMStyleWithColumns(20)));
716 "//\t\t\t\tofMap(message.velocity, 0, 127, 0, ofGetWidth()\n"
718 "//\t\t\t\tofMap(message.velocity, 0, 127, 0, ofGetWidth() * 0.2)");
721 TEST_F(FormatTestComments
, PreservesHangingIndentInCxxComments
) {
722 EXPECT_EQ("// A comment\n"
726 format("// A comment that doesn't fit on one line",
727 getLLVMStyleWithColumns(20)));
728 EXPECT_EQ("/// A comment\n"
732 format("/// A comment that doesn't fit on one line",
733 getLLVMStyleWithColumns(20)));
736 TEST_F(FormatTestComments
, DontSplitLineCommentsWithEscapedNewlines
) {
737 EXPECT_EQ("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
738 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
739 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
740 format("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
741 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
742 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
743 EXPECT_EQ("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
744 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
745 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
746 format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
747 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
748 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
749 getLLVMStyleWithColumns(50)));
750 // FIXME: One day we might want to implement adjustment of leading whitespace
751 // of the consecutive lines in this kind of comment:
753 " a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
754 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
755 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
756 format("double a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
757 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
758 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
759 getLLVMStyleWithColumns(49)));
762 TEST_F(FormatTestComments
, DontIntroduceMultilineComments
) {
763 // Avoid introducing a multiline comment by breaking after `\`.
764 for (int ColumnLimit
= 15; ColumnLimit
<= 17; ++ColumnLimit
) {
768 format("// aaaaaaaaaa \\ bb", getLLVMStyleWithColumns(ColumnLimit
)));
772 format("// aaaaaaaaa \\ bb", getLLVMStyleWithColumns(ColumnLimit
)));
776 format("// aaaaaaaaa \\ \\ bb", getLLVMStyleWithColumns(ColumnLimit
)));
780 TEST_F(FormatTestComments
, DontSplitLineCommentsWithPragmas
) {
781 FormatStyle Pragmas
= getLLVMStyleWithColumns(30);
782 Pragmas
.CommentPragmas
= "^ IWYU pragma:";
784 "// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb",
785 format("// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", Pragmas
));
787 "/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */",
788 format("/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", Pragmas
));
791 TEST_F(FormatTestComments
, PriorityOfCommentBreaking
) {
792 EXPECT_EQ("if (xxx ==\n"
793 " yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
796 format("if (xxx == yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
798 getLLVMStyleWithColumns(40)));
799 EXPECT_EQ("if (xxxxxxxxxx ==\n"
800 " yyy && // aaaaaa bbbbbbbb cccc\n"
803 format("if (xxxxxxxxxx == yyy && // aaaaaa bbbbbbbb cccc\n"
805 getLLVMStyleWithColumns(40)));
806 EXPECT_EQ("if (xxxxxxxxxx &&\n"
807 " yyy || // aaaaaa bbbbbbbb cccc\n"
810 format("if (xxxxxxxxxx && yyy || // aaaaaa bbbbbbbb cccc\n"
812 getLLVMStyleWithColumns(40)));
813 EXPECT_EQ("fffffffff(\n"
814 " &xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
816 format("fffffffff(&xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
818 getLLVMStyleWithColumns(40)));
821 TEST_F(FormatTestComments
, MultiLineCommentsInDefines
) {
822 EXPECT_EQ("#define A(x) /* \\\n"
826 format("#define A(x) /* \\\n"
830 getLLVMStyleWithColumns(17)));
831 EXPECT_EQ("#define A( \\\n"
836 format("#define A( \\\n"
841 getLLVMStyleWithColumns(17)));
844 TEST_F(FormatTestComments
, ParsesCommentsAdjacentToPPDirectives
) {
845 EXPECT_EQ("namespace {}\n// Test\n#define A",
846 format("namespace {}\n // Test\n#define A"));
847 EXPECT_EQ("namespace {}\n/* Test */\n#define A",
848 format("namespace {}\n /* Test */\n#define A"));
849 EXPECT_EQ("namespace {}\n/* Test */ #define A",
850 format("namespace {}\n /* Test */ #define A"));
853 TEST_F(FormatTestComments
, KeepsLevelOfCommentBeforePPDirective
) {
854 // Keep the current level if the comment was originally not aligned with
855 // the preprocessor directive.
856 EXPECT_EQ("void f() {\n"
862 format("void f() {\n"
869 EXPECT_EQ("void f() {\n"
876 format("void f() {\n"
884 EXPECT_EQ("int f(int i) {\n"
893 format("int f(int i) {\n"
903 EXPECT_EQ("int f(int i) {\n"
907 " // comment in else\n"
913 format("int f(int i) {\n"
917 " // comment in else\n"
924 EXPECT_EQ("int f(int i) {\n"
928 " /* comment in else */\n"
934 format("int f(int i) {\n"
938 " /* comment in else */\n"
945 // Keep the current level if there is an empty line between the comment and
946 // the preprocessor directive.
947 EXPECT_EQ("void f() {\n"
954 format("void f() {\n"
962 EXPECT_EQ("void f() {\n"
971 format("void f() {\n"
981 EXPECT_EQ("int f(int i) {\n"
991 format("int f(int i) {\n"
1002 EXPECT_EQ("int f(int i) {\n"
1006 " // comment in else\n"
1013 format("int f(int i) {\n"
1017 "// comment in else\n"
1025 EXPECT_EQ("int f(int i) {\n"
1029 " /* comment in else */\n"
1036 format("int f(int i) {\n"
1040 "/* comment in else */\n"
1048 // Align with the preprocessor directive if the comment was originally aligned
1049 // with the preprocessor directive and there is no newline between the comment
1050 // and the preprocessor directive.
1051 EXPECT_EQ("void f() {\n"
1057 format("void f() {\n"
1064 EXPECT_EQ("int f(int i) {\n"
1073 format("int f(int i) {\n"
1083 EXPECT_EQ("int f(int i) {\n"
1087 "// comment in else\n"
1093 format("int f(int i) {\n"
1097 " // comment in else\n"
1104 EXPECT_EQ("int f(int i) {\n"
1108 "/* comment in else */\n"
1114 format("int f(int i) {\n"
1118 " /* comment in else */\n"
1125 const StringRef
Code("void func() {\n"
1126 " // clang-format off\n"
1127 " #define KV(value) #value, value\n"
1128 " // clang-format on\n"
1130 verifyNoChange(Code
);
1132 auto Style
= getLLVMStyle();
1133 Style
.IndentPPDirectives
= FormatStyle::PPDIS_BeforeHash
;
1134 verifyFormat("#ifdef FOO\n"
1136 " #define Foo foo\n"
1139 " #define Bar bar\n"
1144 TEST_F(FormatTestComments
, CommentsBetweenUnbracedBodyAndPPDirective
) {
1147 " f(); // comment\n"
1158 verifyNoChange("{\n"
1165 verifyNoChange("{\n"
1174 TEST_F(FormatTestComments
, SplitsLongLinesInComments
) {
1175 // FIXME: Do we need to fix up the " */" at the end?
1176 // It doesn't look like any of our current logic triggers this.
1177 EXPECT_EQ("/* This is a long\n"
1179 " * doesn't fit on\n"
1185 "fit on one line. */",
1186 getLLVMStyleWithColumns(20)));
1192 format("/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10)));
1198 format("\\\n/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10)));
1201 "comment that doesn't\n"
1202 "fit on one line.\n"
1206 "comment that doesn't "
1207 "fit on one line. \n"
1209 getLLVMStyleWithColumns(20)));
1211 " * This is a long\n"
1213 " * doesn't fit on\n"
1217 " * This is a long "
1222 getLLVMStyleWithColumns(20)));
1224 " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n"
1225 " * so_it_should_be_broken\n"
1226 " * wherever_a_space_occurs\n"
1229 " * This_is_a_comment_with_words_that_dont_fit_on_one_line "
1230 " so_it_should_be_broken "
1231 " wherever_a_space_occurs \n"
1233 getLLVMStyleWithColumns(20)));
1235 " * This_comment_can_not_be_broken_into_lines\n"
1238 " * This_comment_can_not_be_broken_into_lines\n"
1240 getLLVMStyleWithColumns(20)));
1243 " This is another\n"
1244 " long comment that\n"
1245 " doesn't fit on one\n"
1246 " line 1234567890\n"
1252 " long comment that "
1253 " doesn't fit on one"
1254 " line 1234567890\n"
1257 getLLVMStyleWithColumns(20)));
1261 " * another comment\n"
1262 " * t hat doesn' t\n"
1263 " * fit on one l i\n"
1276 getLLVMStyleWithColumns(20)));
1278 " * This is a long\n"
1280 " * doesn't fit on\n"
1284 " * This is a long comment that doesn't fit on one line\n"
1286 getLLVMStyleWithColumns(20)));
1288 " if (something) /* This is a\n"
1294 " if (something) /* This is a long comment */\n"
1297 getLLVMStyleWithColumns(30)));
1299 EXPECT_EQ("/* A comment before\n"
1301 " * definition */\n"
1303 format("/* A comment before a macro definition */\n"
1305 getLLVMStyleWithColumns(20)));
1307 EXPECT_EQ("/* some comment\n"
1308 " * a comment that\n"
1309 " * we break another\n"
1310 " * comment we have\n"
1311 " * to break a left\n"
1314 format(" /* some comment\n"
1315 " * a comment that we break\n"
1316 " * another comment we have to break\n"
1317 "* a left comment\n"
1319 getLLVMStyleWithColumns(20)));
1322 " * multiline block\n"
1327 " * multiline block comment\n"
1330 getLLVMStyleWithColumns(20)));
1332 // This reproduces a crashing bug where both adaptStartOfLine and
1333 // getCommentSplit were trying to wrap after the "/**".
1334 verifyFormat("/** multilineblockcommentwithnowrapopportunity */",
1335 getLLVMStyleWithColumns(20));
1346 EXPECT_EQ("/* a a */",
1347 format("/* a a */", getLLVMStyleWithColumns(15)));
1348 EXPECT_EQ("/* a a bc */",
1349 format("/* a a bc */", getLLVMStyleWithColumns(15)));
1350 EXPECT_EQ("/* aaa aaa\n"
1352 format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15)));
1353 EXPECT_EQ("/* aaa aaa\n"
1355 format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15)));
1358 TEST_F(FormatTestComments
, SplitsLongLinesInCommentsInPreprocessor
) {
1359 EXPECT_EQ("#define X \\\n"
1362 " Macro comment \\\n"
1367 format("#define X \\\n"
1370 " Macro comment with a long line\n"
1373 getLLVMStyleWithColumns(20)));
1374 EXPECT_EQ("#define X \\\n"
1375 " /* Macro comment \\\n"
1379 format("#define X \\\n"
1380 " /* Macro comment with a long\n"
1383 getLLVMStyleWithColumns(20)));
1384 EXPECT_EQ("#define X \\\n"
1385 " /* Macro comment \\\n"
1386 " * with a long \\\n"
1389 format("#define X \\\n"
1390 " /* Macro comment with a long line */ \\\n"
1392 getLLVMStyleWithColumns(20)));
1395 TEST_F(FormatTestComments
, KeepsTrailingPPCommentsAndSectionCommentsSeparate
) {
1396 verifyFormat("#ifdef A // line about A\n"
1397 "// section comment\n"
1399 getLLVMStyleWithColumns(80));
1400 verifyFormat("#ifdef A // line 1 about A\n"
1401 " // line 2 about A\n"
1402 "// section comment\n"
1404 getLLVMStyleWithColumns(80));
1405 EXPECT_EQ("#ifdef A // line 1 about A\n"
1406 " // line 2 about A\n"
1407 "// section comment\n"
1409 format("#ifdef A // line 1 about A\n"
1410 " // line 2 about A\n"
1411 "// section comment\n"
1413 getLLVMStyleWithColumns(80)));
1414 verifyFormat("int f() {\n"
1416 "#ifdef A // comment about A\n"
1417 " // section comment 1\n"
1418 " // section comment 2\n"
1420 "#else // comment about #else\n"
1421 " // section comment 3\n"
1425 getLLVMStyleWithColumns(80));
1428 TEST_F(FormatTestComments
, AlignsPPElseEndifComments
) {
1429 verifyFormat("#if A\n"
1433 getLLVMStyleWithColumns(20));
1434 verifyFormat("#if A\n"
1438 getLLVMStyleWithColumns(20));
1449 getLLVMStyleWithColumns(20)));
1452 TEST_F(FormatTestComments
, CommentsInStaticInitializers
) {
1454 "static SomeType type = {aaaaaaaaaaaaaaaaaaaa, /* comment */\n"
1455 " aaaaaaaaaaaaaaaaaaaa /* comment */,\n"
1456 " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n"
1457 " aaaaaaaaaaaaaaaaaaaa, // comment\n"
1458 " aaaaaaaaaaaaaaaaaaaa};",
1459 format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n"
1460 " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n"
1461 " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n"
1462 " aaaaaaaaaaaaaaaaaaaa , // comment\n"
1463 " aaaaaaaaaaaaaaaaaaaa };"));
1464 verifyFormat("static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
1465 " bbbbbbbbbbb, ccccccccccc};");
1466 verifyFormat("static SomeType type = {aaaaaaaaaaa,\n"
1467 " // comment for bb....\n"
1468 " bbbbbbbbbbb, ccccccccccc};");
1470 "static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
1471 " bbbbbbbbbbb, ccccccccccc};");
1472 verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n"
1473 " // comment for bb....\n"
1474 " bbbbbbbbbbb, ccccccccccc};");
1476 verifyFormat("S s = {{a, b, c}, // Group #1\n"
1477 " {d, e, f}, // Group #2\n"
1478 " {g, h, i}}; // Group #3");
1479 verifyFormat("S s = {{// Group #1\n"
1486 EXPECT_EQ("S s = {\n"
1487 " // Some comment\n"
1490 " // Comment after empty line\n"
1493 " // Some comment\n"
1496 " // Comment after empty line\n"
1499 EXPECT_EQ("S s = {\n"
1500 " /* Some comment */\n"
1503 " /* Comment after empty line */\n"
1506 " /* Some comment */\n"
1509 " /* Comment after empty line */\n"
1512 verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n"
1513 " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
1514 " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
1515 " 0x00, 0x00, 0x00, 0x00}; // comment");
1518 TEST_F(FormatTestComments
, LineCommentsAfterRightBrace
) {
1519 EXPECT_EQ("if (true) { // comment about branch\n"
1520 " // comment about f\n"
1523 format("if (true) { // comment about branch\n"
1524 " // comment about f\n"
1527 getLLVMStyleWithColumns(80)));
1528 EXPECT_EQ("if (1) { // if line 1\n"
1534 "} else { // else line 1\n"
1540 format("if (1) { // if line 1\n"
1546 "} else { // else line 1\n"
1552 EXPECT_EQ("do { // line 1\n"
1557 format("do { // line 1\n"
1562 getLLVMStyleWithColumns(80)));
1563 EXPECT_EQ("while (a < b) { // line 1\n"
1568 format("while (a < b) {// line 1\n"
1573 getLLVMStyleWithColumns(80)));
1576 TEST_F(FormatTestComments
, ReflowsComments
) {
1577 // Break a long line and reflow with the full next line.
1578 EXPECT_EQ("// long long long\n"
1580 format("// long long long long\n"
1582 getLLVMStyleWithColumns(20)));
1584 // Keep the trailing newline while reflowing.
1585 EXPECT_EQ("// long long long\n"
1587 format("// long long long long\n"
1589 getLLVMStyleWithColumns(20)));
1591 // Break a long line and reflow with a part of the next line.
1592 EXPECT_EQ("// long long long\n"
1595 format("// long long long long\n"
1596 "// long long_long",
1597 getLLVMStyleWithColumns(20)));
1599 // Break but do not reflow if the first word from the next line is too long.
1600 EXPECT_EQ("// long long long\n"
1602 "// long_long_long",
1603 format("// long long long long\n"
1604 "// long_long_long",
1605 getLLVMStyleWithColumns(20)));
1607 // Don't break or reflow short lines.
1608 verifyFormat("// long\n"
1609 "// long long long lo\n"
1610 "// long long long lo\n"
1612 getLLVMStyleWithColumns(20));
1614 // Keep prefixes and decorations while reflowing.
1615 EXPECT_EQ("/// long long long\n"
1617 format("/// long long long long\n"
1619 getLLVMStyleWithColumns(20)));
1620 EXPECT_EQ("//! long long long\n"
1622 format("//! long long long long\n"
1624 getLLVMStyleWithColumns(20)));
1625 EXPECT_EQ("/* long long long\n"
1627 format("/* long long long long\n"
1629 getLLVMStyleWithColumns(20)));
1630 EXPECT_EQ("///< long long long\n"
1632 format("///< long long long long\n"
1634 getLLVMStyleWithColumns(20)));
1635 EXPECT_EQ("//!< long long long\n"
1637 format("//!< long long long long\n"
1639 getLLVMStyleWithColumns(20)));
1641 // Don't bring leading whitespace up while reflowing.
1642 EXPECT_EQ("/* long long long\n"
1643 " * long long long\n"
1645 format("/* long long long long\n"
1648 getLLVMStyleWithColumns(20)));
1650 // Reflow the last line of a block comment with its trailing '*/'.
1651 EXPECT_EQ("/* long long long\n"
1653 format("/* long long long long\n"
1655 getLLVMStyleWithColumns(20)));
1657 // Reflow two short lines; keep the postfix of the last one.
1658 EXPECT_EQ("/* long long long\n"
1659 " * long long long */",
1660 format("/* long long long long\n"
1663 getLLVMStyleWithColumns(20)));
1665 // Put the postfix of the last short reflow line on a newline if it doesn't
1667 EXPECT_EQ("/* long long long\n"
1668 " * long long longg\n"
1670 format("/* long long long long\n"
1673 getLLVMStyleWithColumns(20)));
1675 // Reflow lines with leading whitespace.
1678 " * long long long\n"
1679 " * long long long\n"
1680 " * long long long\n"
1685 " * long long long long\n"
1687 " * long long long long\n"
1690 getLLVMStyleWithColumns(20)));
1692 // Break single line block comments that are first in the line with ' *'
1694 EXPECT_EQ("/* long long long\n"
1696 format("/* long long long long */", getLLVMStyleWithColumns(20)));
1698 // Break single line block comment that are not first in the line with ' '
1700 EXPECT_EQ("int i; /* long long\n"
1702 format("int i; /* long long long */", getLLVMStyleWithColumns(20)));
1704 // Reflow a line that goes just over the column limit.
1705 EXPECT_EQ("// long long long\n"
1707 format("// long long long lon\n"
1709 getLLVMStyleWithColumns(20)));
1711 // Stop reflowing if the next line has a different indentation than the
1713 EXPECT_EQ("// long long long\n"
1717 format("// long long long long\n"
1720 getLLVMStyleWithColumns(20)));
1722 // Reflow into the last part of a really long line that has been broken into
1724 EXPECT_EQ("// long long long\n"
1725 "// long long long\n"
1726 "// long long long",
1727 format("// long long long long long long long long\n"
1729 getLLVMStyleWithColumns(20)));
1731 // Break the first line, then reflow the beginning of the second and third
1733 EXPECT_EQ("// long long long\n"
1734 "// lon1 lon2 lon2\n"
1735 "// lon2 lon3 lon3",
1736 format("// long long long lon1\n"
1737 "// lon2 lon2 lon2\n"
1739 getLLVMStyleWithColumns(20)));
1741 // Reflow the beginning of the second line, then break the rest.
1742 EXPECT_EQ("// long long long\n"
1743 "// lon1 lon2 lon2\n"
1744 "// lon2 lon2 lon2\n"
1746 format("// long long long lon1\n"
1747 "// lon2 lon2 lon2 lon2 lon2 lon3",
1748 getLLVMStyleWithColumns(20)));
1750 // Shrink the first line, then reflow the second line up.
1751 EXPECT_EQ("// long long long", format("// long long\n"
1753 getLLVMStyleWithColumns(20)));
1755 // Don't shrink leading whitespace.
1756 verifyNoChange("int i; /// a", getLLVMStyleWithColumns(20));
1758 // Shrink trailing whitespace if there is no postfix and reflow.
1759 EXPECT_EQ("// long long long\n"
1761 format("// long long long long \n"
1763 getLLVMStyleWithColumns(20)));
1765 // Shrink trailing whitespace to a single one if there is postfix.
1766 EXPECT_EQ("/* long long long */",
1767 format("/* long long long */", getLLVMStyleWithColumns(20)));
1769 // Break a block comment postfix if exceeding the line limit.
1770 EXPECT_EQ("/* long\n"
1772 format("/* long */", getLLVMStyleWithColumns(20)));
1774 // Reflow indented comments.
1776 " // long long long\n"
1778 " int i; /* long lon\n"
1783 " // long long long long\n"
1785 " int i; /* long lon g\n"
1788 getLLVMStyleWithColumns(20)));
1790 // Don't realign trailing comments after reflow has happened.
1791 EXPECT_EQ("// long long long\n"
1794 format("// long long long long\n"
1797 getLLVMStyleWithColumns(20)));
1798 EXPECT_EQ("// long long long\n"
1799 "// longng long long\n"
1801 format("// long long long longng\n"
1802 "// long long long\n"
1804 getLLVMStyleWithColumns(20)));
1806 // Reflow lines after a broken line.
1807 EXPECT_EQ("int a; // Trailing\n"
1811 format("int a; // Trailing comment\n"
1815 getLLVMStyleWithColumns(20)));
1816 EXPECT_EQ("/// This long line\n"
1817 "/// gets reflown.",
1818 format("/// This long line gets\n"
1820 getLLVMStyleWithColumns(20)));
1821 EXPECT_EQ("//! This long line\n"
1822 "//! gets reflown.",
1823 format(" //! This long line gets\n"
1825 getLLVMStyleWithColumns(20)));
1826 EXPECT_EQ("/* This long line\n"
1827 " * gets reflown.\n"
1829 format("/* This long line gets\n"
1832 getLLVMStyleWithColumns(20)));
1834 // Reflow after indentation makes a line too long.
1836 " // long long long\n"
1840 "// long long long lo\n"
1843 getLLVMStyleWithColumns(20)));
1845 // Break and reflow multiple lines.
1847 " * Reflow the end of\n"
1848 " * line by 11 22 33\n"
1852 " * Reflow the end of line\n"
1859 getLLVMStyleWithColumns(20)));
1860 EXPECT_EQ("/// First line gets\n"
1861 "/// broken. Second\n"
1864 "/// broken. Third\n"
1865 "/// gets reflown.",
1866 format("/// First line gets broken.\n"
1867 "/// Second line gets reflown and broken.\n"
1868 "/// Third gets reflown.",
1869 getLLVMStyleWithColumns(20)));
1870 EXPECT_EQ("int i; // first long\n"
1873 format("int i; // first long long\n"
1875 getLLVMStyleWithColumns(20)));
1877 " // first long line\n"
1879 " // long line line\n"
1880 " // third long line\n"
1884 " // first long line line\n"
1885 " // second long line line\n"
1886 " // third long line line\n"
1888 getLLVMStyleWithColumns(20)));
1889 EXPECT_EQ("int i; /* first line\n"
1894 format("int i; /* first line\n"
1898 getLLVMStyleWithColumns(20)));
1900 // Reflow the last two lines of a section that starts with a line having
1901 // different indentation.
1902 EXPECT_EQ("// long\n"
1903 "// long long long\n"
1906 "// long long long long\n"
1908 getLLVMStyleWithColumns(20)));
1910 // Keep the block comment endling '*/' while reflowing.
1911 EXPECT_EQ("/* Long long long\n"
1913 format("/* Long long long line\n"
1915 getLLVMStyleWithColumns(20)));
1917 // Don't reflow between separate blocks of comments.
1918 EXPECT_EQ("/* First comment\n"
1919 " * block will */\n"
1922 format("/* First comment block\n"
1926 getLLVMStyleWithColumns(20)));
1928 // Don't reflow across blank comment lines.
1929 EXPECT_EQ("int i; // This long\n"
1934 format("int i; // This long line gets broken.\n"
1937 getLLVMStyleWithColumns(20)));
1939 " /// long long long\n"
1945 " /// long long long long\n"
1950 getLLVMStyleWithColumns(20)));
1951 EXPECT_EQ("//! long long long\n"
1955 format("//! long long long long\n"
1958 getLLVMStyleWithColumns(20)));
1959 EXPECT_EQ("/* long long long\n"
1963 format("/* long long long long\n"
1966 getLLVMStyleWithColumns(20)));
1967 EXPECT_EQ("/* long long long\n"
1971 format("/* long long long long\n"
1974 getLLVMStyleWithColumns(20)));
1976 // Don't reflow lines having content that is a single character.
1977 EXPECT_EQ("// long long long\n"
1980 format("// long long long long\n"
1982 getLLVMStyleWithColumns(20)));
1984 // Don't reflow lines starting with two punctuation characters.
1985 EXPECT_EQ("// long long long\n"
1988 format("// long long long long\n"
1990 getLLVMStyleWithColumns(20)));
1992 // Don't reflow lines starting with '@'.
1993 EXPECT_EQ("// long long long\n"
1996 format("// long long long long\n"
1998 getLLVMStyleWithColumns(20)));
2000 // Don't reflow lines starting with '\'.
2001 verifyFormat("// long long long\n"
2004 "// long long long long\n"
2006 getLLVMStyleWithColumns(20));
2008 // Don't reflow lines starting with 'TODO'.
2009 EXPECT_EQ("// long long long\n"
2012 format("// long long long long\n"
2014 getLLVMStyleWithColumns(20)));
2016 // Don't reflow lines starting with 'FIXME'.
2017 EXPECT_EQ("// long long long\n"
2020 format("// long long long long\n"
2022 getLLVMStyleWithColumns(20)));
2024 // Don't reflow lines starting with 'XXX'.
2025 EXPECT_EQ("// long long long\n"
2028 format("// long long long long\n"
2030 getLLVMStyleWithColumns(20)));
2032 // Don't reflow comment pragmas.
2033 EXPECT_EQ("// long long long\n"
2036 format("// long long long long\n"
2038 getLLVMStyleWithColumns(20)));
2039 EXPECT_EQ("/* long long long\n"
2043 format("/* long long long long\n"
2046 getLLVMStyleWithColumns(20)));
2048 // Reflow lines that have a non-punctuation character among their first 2
2050 EXPECT_EQ("// long long long\n"
2052 format("// long long long long\n"
2054 getLLVMStyleWithColumns(20)));
2056 // Don't reflow between separate blocks of comments.
2057 EXPECT_EQ("/* First comment\n"
2058 " * block will */\n"
2061 format("/* First comment block\n"
2065 getLLVMStyleWithColumns(20)));
2067 // Don't reflow lines having different indentation.
2068 EXPECT_EQ("// long long long\n"
2071 format("// long long long long\n"
2073 getLLVMStyleWithColumns(20)));
2075 // Don't reflow separate bullets in list
2076 EXPECT_EQ("// - long long long\n"
2079 format("// - long long long long\n"
2081 getLLVMStyleWithColumns(20)));
2082 EXPECT_EQ("// * long long long\n"
2085 format("// * long long long long\n"
2087 getLLVMStyleWithColumns(20)));
2088 EXPECT_EQ("// + long long long\n"
2091 format("// + long long long long\n"
2093 getLLVMStyleWithColumns(20)));
2094 EXPECT_EQ("// 1. long long long\n"
2097 format("// 1. long long long long\n"
2099 getLLVMStyleWithColumns(20)));
2100 EXPECT_EQ("// -# long long long\n"
2103 format("// -# long long long long\n"
2105 getLLVMStyleWithColumns(20)));
2107 EXPECT_EQ("// - long long long\n"
2108 "// long long long\n"
2110 format("// - long long long long\n"
2113 getLLVMStyleWithColumns(20)));
2114 EXPECT_EQ("// - long long long\n"
2115 "// long long long\n"
2118 format("// - long long long long\n"
2119 "// long long long\n"
2121 getLLVMStyleWithColumns(20)));
2123 // Large number (>2 digits) are not list items
2124 EXPECT_EQ("// long long long\n"
2125 "// long 1024. long.",
2126 format("// long long long long\n"
2128 getLLVMStyleWithColumns(20)));
2130 // Do not break before number, to avoid introducing a non-reflowable doxygen
2132 EXPECT_EQ("// long long\n"
2133 "// long 10. long.",
2134 format("// long long long 10.\n"
2136 getLLVMStyleWithColumns(20)));
2138 // Don't break or reflow after implicit string literals.
2139 verifyFormat("#include <t> // l l l\n"
2141 getLLVMStyleWithColumns(20));
2143 // Don't break or reflow comments on import lines.
2144 EXPECT_EQ("#include \"t\" /* l l l\n"
2146 format("#include \"t\" /* l l l\n"
2148 getLLVMStyleWithColumns(20)));
2150 // Don't reflow between different trailing comment sections.
2151 EXPECT_EQ("int i; // long long\n"
2153 "int j; // long long\n"
2155 format("int i; // long long long\n"
2156 "int j; // long long long",
2157 getLLVMStyleWithColumns(20)));
2159 // Don't reflow if the first word on the next line is longer than the
2160 // available space at current line.
2161 EXPECT_EQ("int i; // trigger\n"
2164 format("int i; // trigger reflow\n"
2166 getLLVMStyleWithColumns(20)));
2168 // Simple case that correctly handles reflow in parameter lists.
2169 EXPECT_EQ("a = f(/* looooooooong\n"
2173 format("a = f(/* looooooooong long\n* long\n*/ a);",
2174 getLLVMStyleWithColumns(22)));
2175 // Tricky case that has fewer lines if we reflow the comment, ending up with
2177 EXPECT_EQ("a = f(/* loooooong\n"
2181 format("a = f(/* loooooong long\n* long\n*/ a);",
2182 getLLVMStyleWithColumns(22)));
2184 // Keep empty comment lines.
2185 EXPECT_EQ("/**/", format(" /**/", getLLVMStyleWithColumns(20)));
2186 EXPECT_EQ("/* */", format(" /* */", getLLVMStyleWithColumns(20)));
2187 EXPECT_EQ("/* */", format(" /* */", getLLVMStyleWithColumns(20)));
2188 EXPECT_EQ("//", format(" // ", getLLVMStyleWithColumns(20)));
2189 EXPECT_EQ("///", format(" /// ", getLLVMStyleWithColumns(20)));
2192 TEST_F(FormatTestComments
, ReflowsCommentsPrecise
) {
2193 // FIXME: This assumes we do not continue compressing whitespace once we are
2194 // in reflow mode. Consider compressing whitespace.
2196 // Test that we stop reflowing precisely at the column limit.
2197 // After reflowing, "// reflows into foo" does not fit the column limit,
2198 // so we compress the whitespace.
2199 EXPECT_EQ("// some text that\n"
2200 "// reflows into foo",
2201 format("// some text that reflows\n"
2203 getLLVMStyleWithColumns(20)));
2204 // Given one more column, "// reflows into foo" does fit the limit, so we
2205 // do not compress the whitespace.
2206 EXPECT_EQ("// some text that\n"
2207 "// reflows into foo",
2208 format("// some text that reflows\n"
2210 getLLVMStyleWithColumns(21)));
2212 // Make sure that we correctly account for the space added in the reflow case
2213 // when making the reflowing decision.
2214 // First, when the next line ends precisely one column over the limit, do not
2216 EXPECT_EQ("// some text that\n"
2219 format("// some text that reflows\n"
2221 getLLVMStyleWithColumns(21)));
2222 // Secondly, when the next line ends later, but the first word in that line
2223 // is precisely one column over the limit, do not reflow.
2224 EXPECT_EQ("// some text that\n"
2227 format("// some text that reflows\n"
2229 getLLVMStyleWithColumns(21)));
2232 TEST_F(FormatTestComments
, ReflowsCommentsWithExtraWhitespace
) {
2234 EXPECT_EQ("// some text\n"
2236 format("// some text that\n"
2238 getLLVMStyleWithColumns(16)));
2239 EXPECT_EQ("// some text\n"
2241 format("// some text that\n"
2243 getLLVMStyleWithColumns(16)));
2244 EXPECT_EQ("/* some text\n"
2245 " * that re flows\n"
2247 format("/* some text that\n"
2250 getLLVMStyleWithColumns(16)));
2251 // FIXME: We do not reflow if the indent of two subsequent lines differs;
2252 // given that this is different behavior from block comments, do we want
2254 EXPECT_EQ("// some text\n"
2257 format("// some text that\n"
2259 getLLVMStyleWithColumns(16)));
2260 // Space within parts of a line that fit.
2261 // FIXME: Use the earliest possible split while reflowing to compress the
2262 // whitespace within the line.
2263 EXPECT_EQ("// some text that\n"
2266 format("// some text that does\n"
2267 "// re flow more here",
2268 getLLVMStyleWithColumns(21)));
2271 TEST_F(FormatTestComments
, IgnoresIf0Contents
) {
2273 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
2277 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
2280 EXPECT_EQ("#if false\n"
2284 format("#if false\n"
2288 EXPECT_EQ("enum E {\n"
2304 EXPECT_EQ("enum F {\n"
2326 EXPECT_EQ("enum G {\n"
2344 EXPECT_EQ("enum H {\n"
2366 EXPECT_EQ("enum I {\n"
2368 "#if /* test */ 0 || 1\n"
2376 "#if /* test */ 0 || 1\n"
2382 EXPECT_EQ("enum J {\n"
2407 // Ignore stuff in SWIG-blocks.
2408 EXPECT_EQ("#ifdef SWIG\n"
2409 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
2412 format("#ifdef SWIG\n"
2413 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
2416 EXPECT_EQ("#ifndef SWIG\n"
2419 format("#ifndef SWIG\n"
2424 TEST_F(FormatTestComments
, DontCrashOnBlockComments
) {
2426 "int xxxxxxxxx; /* "
2427 "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\n"
2430 format("int xxxxxxxxx; /* "
2431 "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy zzzzzz\n"
2435 TEST_F(FormatTestComments
, BlockCommentsInControlLoops
) {
2436 verifyFormat("if (0) /* a comment in a strange place */ {\n"
2439 verifyFormat("if (0) /* a comment in a strange place */ {\n"
2441 "} /* another comment */ else /* comment #3 */ {\n"
2444 verifyFormat("while (0) /* a comment in a strange place */ {\n"
2447 verifyFormat("for (;;) /* a comment in a strange place */ {\n"
2450 verifyFormat("do /* a comment in a strange place */ {\n"
2452 "} /* another comment */ while (0);");
2455 TEST_F(FormatTestComments
, BlockComments
) {
2456 EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */",
2457 format("/* *//* */ /* */\n/* *//* */ /* */"));
2458 EXPECT_EQ("/* */ a /* */ b;", format(" /* */ a/* */ b;"));
2459 EXPECT_EQ("#define A /*123*/ \\\n"
2464 format("#define A /*123*/ b\n"
2466 "someCall(parameter);",
2467 getLLVMStyleWithColumns(15)));
2469 EXPECT_EQ("#define A\n"
2472 format("#define A\n"
2473 "/* */someCall(parameter);",
2474 getLLVMStyleWithColumns(15)));
2475 verifyNoChange("/*\n**\n*/");
2483 " * aaaaaa aaaaaa\n"
2485 getLLVMStyleWithColumns(10)));
2495 getLLVMStyleWithColumns(10)));
2496 EXPECT_EQ("int aaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
2498 " bbbbbbbbbbbb */\n"
2499 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
2500 format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
2502 " bbbbbbbbbbbb */ bbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
2503 getLLVMStyleWithColumns(50)));
2505 FormatStyle NoBinPacking
= getLLVMStyle();
2506 NoBinPacking
.BinPackParameters
= FormatStyle::BPPS_OnePerLine
;
2507 EXPECT_EQ("someFunction(1, /* comment 1 */\n"
2508 " 2, /* comment 2 */\n"
2509 " 3, /* comment 3 */\n"
2512 format("someFunction (1, /* comment 1 */\n"
2513 " 2, /* comment 2 */ \n"
2514 " 3, /* comment 3 */\n"
2518 "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2519 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
2521 "bool aaaaaaaaaaaaa = /* trailing comment */\n"
2522 " aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2523 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;",
2525 "bool aaaaaaaaaaaaa = /* trailing comment */\n"
2526 " aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2527 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;"));
2529 "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
2530 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
2531 "int cccccccccccccccccccccccccccccc; /* comment */",
2532 format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
2533 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
2534 "int cccccccccccccccccccccccccccccc; /* comment */"));
2536 verifyFormat("void f(int * /* unused */) {}");
2566 TEST_F(FormatTestComments
, BlockCommentsInMacros
) {
2567 EXPECT_EQ("#define A \\\n"
2569 " /* one line */ \\\n"
2571 format("#define A { \\\n"
2572 " /* one line */ \\\n"
2574 getLLVMStyleWithColumns(20)));
2575 EXPECT_EQ("#define A \\\n"
2577 " /* previous */ \\\n"
2578 " /* one line */ \\\n"
2580 format("#define A { \\\n"
2581 " /* previous */ \\\n"
2582 " /* one line */ \\\n"
2584 getLLVMStyleWithColumns(20)));
2587 TEST_F(FormatTestComments
, BlockCommentsAtEndOfLine
) {
2591 format("a = {1111 /* */\n"
2593 getLLVMStyleWithColumns(15)));
2597 format("a = {1111 /* */\n"
2599 getLLVMStyleWithColumns(15)));
2604 format("a = {1111 /* a */\n"
2606 getLLVMStyleWithColumns(15)));
2609 TEST_F(FormatTestComments
, BreaksAfterMultilineBlockCommentsInParamLists
) {
2610 EXPECT_EQ("a = f(/* long\n"
2613 format("a = f(/* long long */ a);", getLLVMStyleWithColumns(16)));
2614 EXPECT_EQ("a = f(\n"
2618 format("a = f(/* long long */ a);", getLLVMStyleWithColumns(15)));
2620 EXPECT_EQ("a = f(/* long\n"
2624 format("a = f(/* long\n"
2627 getLLVMStyleWithColumns(16)));
2629 EXPECT_EQ("a = f(/* long\n"
2633 format("a = f(/* long\n"
2636 getLLVMStyleWithColumns(16)));
2638 EXPECT_EQ("a = f(/* long\n"
2642 format("a = f(/* long\n"
2645 getLLVMStyleWithColumns(16)));
2652 format("a = f(a, /* long long */ b);", getLLVMStyleWithColumns(16)));
2660 format("a = f(a, /* long long */ b);", getLLVMStyleWithColumns(15)));
2662 EXPECT_EQ("a = f(a,\n"
2666 format("a = f(a, /* long long */ (1 + 1));",
2667 getLLVMStyleWithColumns(16)));
2668 EXPECT_EQ("a = f(\n"
2673 format("a = f(a, /* long long */ (1 + 1));",
2674 getLLVMStyleWithColumns(15)));
2677 TEST_F(FormatTestComments
, IndentLineCommentsInStartOfBlockAtEndOfFile
) {
2683 TEST_F(FormatTestComments
, AlignTrailingComments
) {
2684 EXPECT_EQ("#define MACRO(V) \\\n"
2685 " V(Rt2) /* one more char */ \\\n"
2686 " V(Rs) /* than here */ \\\n"
2687 "/* comment 3 */\n",
2688 format("#define MACRO(V)\\\n"
2689 "V(Rt2) /* one more char */ \\\n"
2690 "V(Rs) /* than here */ \\\n"
2691 "/* comment 3 */\n",
2692 getLLVMStyleWithColumns(40)));
2693 EXPECT_EQ("int i = f(abc, // line 1\n"
2697 format("int i = f(abc, // line 1\n"
2701 getLLVMStyleWithColumns(40)));
2703 // Align newly broken trailing comments.
2704 EXPECT_EQ("int ab; // line\n"
2707 format("int ab; // line\n"
2708 "int a; // long long",
2709 getLLVMStyleWithColumns(15)));
2710 EXPECT_EQ("int ab; // line\n"
2714 format("int ab; // line\n"
2715 "int a; // long long\n"
2717 getLLVMStyleWithColumns(15)));
2718 EXPECT_EQ("int ab; // line\n"
2722 format("int ab; // line\n"
2723 "int a; // long long\n"
2725 getLLVMStyleWithColumns(15)));
2726 EXPECT_EQ("int ab; // line\n"
2731 format("int ab; // line\n"
2732 "int a; // long long\n"
2735 getLLVMStyleWithColumns(15)));
2737 // Don't align newly broken trailing comments if that would put them over the
2739 EXPECT_EQ("int i, j; // line 1\n"
2740 "int k; // line longg\n"
2742 format("int i, j; // line 1\n"
2743 "int k; // line longg long",
2744 getLLVMStyleWithColumns(20)));
2746 // Always align if ColumnLimit = 0
2747 EXPECT_EQ("int i, j; // line 1\n"
2748 "int k; // line longg long",
2749 format("int i, j; // line 1\n"
2750 "int k; // line longg long",
2751 getLLVMStyleWithColumns(0)));
2753 // Align comment line sections aligned with the next token with the next
2755 EXPECT_EQ("class A {\n"
2756 "public: // public comment\n"
2757 " // comment about a\n"
2760 format("class A {\n"
2761 "public: // public comment\n"
2762 " // comment about a\n"
2765 getLLVMStyleWithColumns(40)));
2766 EXPECT_EQ("class A {\n"
2767 "public: // public comment 1\n"
2768 " // public comment 2\n"
2769 " // comment 1 about a\n"
2770 " // comment 2 about a\n"
2773 format("class A {\n"
2774 "public: // public comment 1\n"
2775 " // public comment 2\n"
2776 " // comment 1 about a\n"
2777 " // comment 2 about a\n"
2780 getLLVMStyleWithColumns(40)));
2781 EXPECT_EQ("int f(int n) { // comment line 1 on f\n"
2782 " // comment line 2 on f\n"
2783 " // comment line 1 before return\n"
2784 " // comment line 2 before return\n"
2785 " return n; // comment line 1 on return\n"
2786 " // comment line 2 on return\n"
2787 " // comment line 1 after return\n"
2789 format("int f(int n) { // comment line 1 on f\n"
2790 " // comment line 2 on f\n"
2791 " // comment line 1 before return\n"
2792 " // comment line 2 before return\n"
2793 " return n; // comment line 1 on return\n"
2794 " // comment line 2 on return\n"
2795 " // comment line 1 after return\n"
2797 getLLVMStyleWithColumns(40)));
2798 EXPECT_EQ("int f(int n) {\n"
2799 " switch (n) { // comment line 1 on switch\n"
2800 " // comment line 2 on switch\n"
2801 " // comment line 1 before case 1\n"
2802 " // comment line 2 before case 1\n"
2803 " case 1: // comment line 1 on case 1\n"
2804 " // comment line 2 on case 1\n"
2805 " // comment line 1 before return 1\n"
2806 " // comment line 2 before return 1\n"
2807 " return 1; // comment line 1 on return 1\n"
2808 " // comment line 2 on return 1\n"
2809 " // comment line 1 before default\n"
2810 " // comment line 2 before default\n"
2811 " default: // comment line 1 on default\n"
2812 " // comment line 2 on default\n"
2813 " // comment line 1 before return 2\n"
2814 " return 2 * f(n - 1); // comment line 1 on return 2\n"
2815 " // comment line 2 on return 2\n"
2816 " // comment line 1 after return\n"
2817 " // comment line 2 after return\n"
2820 format("int f(int n) {\n"
2821 " switch (n) { // comment line 1 on switch\n"
2822 " // comment line 2 on switch\n"
2823 " // comment line 1 before case 1\n"
2824 " // comment line 2 before case 1\n"
2825 " case 1: // comment line 1 on case 1\n"
2826 " // comment line 2 on case 1\n"
2827 " // comment line 1 before return 1\n"
2828 " // comment line 2 before return 1\n"
2829 " return 1; // comment line 1 on return 1\n"
2830 " // comment line 2 on return 1\n"
2831 " // comment line 1 before default\n"
2832 " // comment line 2 before default\n"
2833 " default: // comment line 1 on default\n"
2834 " // comment line 2 on default\n"
2835 " // comment line 1 before return 2\n"
2836 " return 2 * f(n - 1); // comment line 1 on return 2\n"
2837 " // comment line 2 on return 2\n"
2838 " // comment line 1 after return\n"
2839 " // comment line 2 after return\n"
2842 getLLVMStyleWithColumns(80)));
2844 // If all the lines in a sequence of line comments are aligned with the next
2845 // token, the first line belongs to the previous token and the other lines
2846 // belong to the next token.
2847 EXPECT_EQ("int a; // line about a\n"
2849 format("int a; // line about a\n"
2851 getLLVMStyleWithColumns(80)));
2852 EXPECT_EQ("int a; // line about a\n"
2855 format("int a; // line about a\n"
2856 " // line about b\n"
2858 getLLVMStyleWithColumns(80)));
2859 EXPECT_EQ("int a; // line about a\n"
2860 "// line 1 about b\n"
2861 "// line 2 about b\n"
2863 format("int a; // line about a\n"
2864 " // line 1 about b\n"
2865 " // line 2 about b\n"
2867 getLLVMStyleWithColumns(80)));
2869 // Checks an edge case in preprocessor handling.
2870 // These comments should *not* be aligned
2874 "long a; // Line about a\n"
2878 "long b_long_name; // Line about b\n"
2882 "long a; // Line about a\n" // Previous (bad) behavior
2886 "long b_long_name; // Line about b\n"
2888 getLLVMStyleWithColumns(80)));
2893 "#define FOO_GLOBAL 0 // Global scope.\n"
2894 "#define FOO_LINKLOCAL 1 // Link-local scope.\n"
2895 "#define FOO_SITELOCAL 2 // Site-local scope (deprecated).\n"
2896 "#define FOO_UNIQUELOCAL 3 // Unique local\n"
2897 "#define FOO_NODELOCAL 4 // Loopback\n\n"
2899 format("namespace m {\n\n"
2900 "#define FOO_GLOBAL 0 // Global scope.\n"
2901 "#define FOO_LINKLOCAL 1 // Link-local scope.\n"
2902 "#define FOO_SITELOCAL 2 // Site-local scope (deprecated).\n"
2903 "#define FOO_UNIQUELOCAL 3 // Unique local\n"
2904 "#define FOO_NODELOCAL 4 // Loopback\n\n"
2906 getLLVMStyleWithColumns(80)));
2908 // https://llvm.org/PR53441
2909 verifyFormat("/* */ //\n"
2911 verifyFormat("/**/ //\n"
2915 TEST_F(FormatTestComments
, AlignTrailingCommentsAcrossEmptyLines
) {
2916 FormatStyle Style
= getLLVMStyle();
2917 Style
.AlignTrailingComments
.Kind
= FormatStyle::TCAS_Always
;
2918 Style
.AlignTrailingComments
.OverEmptyLines
= 1;
2919 verifyFormat("#include \"a.h\" // simple\n"
2921 "#include \"aa.h\" // example case",
2924 verifyFormat("#include \"a.h\" // align across\n"
2926 "#include \"aa.h\" // two empty lines\n"
2928 "#include \"aaa.h\" // in a row",
2931 verifyFormat("#include \"a.h\" // align\n"
2932 "#include \"aa.h\" // comment\n"
2933 "#include \"aaa.h\" // blocks\n"
2935 "#include \"aaaa.h\" // across\n"
2936 "#include \"aaaaa.h\" // one\n"
2937 "#include \"aaaaaa.h\" // empty line",
2940 verifyFormat("#include \"a.h\" // align trailing comments\n"
2941 "#include \"a.h\"\n"
2942 "#include \"aa.h\" // across a line without comment",
2945 verifyFormat("#include \"a.h\" // align across\n"
2946 "#include \"a.h\"\n"
2947 "#include \"aa.h\" // two lines without comment\n"
2948 "#include \"a.h\"\n"
2949 "#include \"aaa.h\" // in a row",
2952 verifyFormat("#include \"a.h\" // align\n"
2953 "#include \"aa.h\" // comment\n"
2954 "#include \"aaa.h\" // blocks\n"
2955 "#include \"a.h\"\n"
2956 "#include \"aaaa.h\" // across\n"
2957 "#include \"aaaaa.h\" // a line without\n"
2958 "#include \"aaaaaa.h\" // comment",
2961 // Start of testing OverEmptyLines
2962 Style
.MaxEmptyLinesToKeep
= 3;
2963 Style
.AlignTrailingComments
.OverEmptyLines
= 2;
2964 // Cannot use verifyFormat here
2965 // test::messUp removes all new lines which changes the logic
2966 EXPECT_EQ("#include \"a.h\" // comment\n"
2970 "#include \"ab.h\" // comment\n"
2973 "#include \"abcdefg.h\" // comment",
2974 format("#include \"a.h\" // comment\n"
2978 "#include \"ab.h\" // comment\n"
2981 "#include \"abcdefg.h\" // comment",
2984 Style
.MaxEmptyLinesToKeep
= 1;
2985 Style
.AlignTrailingComments
.OverEmptyLines
= 1;
2986 // End of testing OverEmptyLines
2988 Style
.ColumnLimit
= 15;
2989 EXPECT_EQ("int ab; // line\n"
2994 format("int ab; // line\n"
2995 "int a; // long long\n"
3000 Style
.ColumnLimit
= 15;
3001 EXPECT_EQ("int ab; // line\n"
3005 format("int ab; // line\n"
3007 "int a; // long long",
3010 Style
.ColumnLimit
= 30;
3011 EXPECT_EQ("int foo = 12345; // comment\n"
3013 " 1234; // This is a very\n"
3014 " // long comment\n"
3015 " // which is wrapped\n"
3018 "int x = 2; // Is this still\n"
3020 format("int foo = 12345; // comment\n"
3021 "int bar = 1234; // This is a very long comment\n"
3022 " // which is wrapped arround.\n"
3024 "int x = 2; // Is this still aligned?",
3027 Style
.ColumnLimit
= 35;
3028 EXPECT_EQ("int foo = 12345; // comment\n"
3030 " 1234; // This is a very long\n"
3031 " // comment which is\n"
3032 " // wrapped arround.\n"
3035 " 2; // Is this still aligned?",
3036 format("int foo = 12345; // comment\n"
3037 "int bar = 1234; // This is a very long comment\n"
3038 " // which is wrapped arround.\n"
3040 "int x = 2; // Is this still aligned?",
3043 Style
.ColumnLimit
= 40;
3044 EXPECT_EQ("int foo = 12345; // comment\n"
3046 " 1234; // This is a very long comment\n"
3047 " // which is wrapped arround.\n"
3049 "int x = 2; // Is this still aligned?",
3050 format("int foo = 12345; // comment\n"
3051 "int bar = 1234; // This is a very long comment\n"
3052 " // which is wrapped arround.\n"
3054 "int x = 2; // Is this still aligned?",
3057 Style
.ColumnLimit
= 45;
3058 EXPECT_EQ("int foo = 12345; // comment\n"
3060 " 1234; // This is a very long comment\n"
3061 " // which is wrapped arround.\n"
3063 "int x = 2; // Is this still aligned?",
3064 format("int foo = 12345; // comment\n"
3065 "int bar = 1234; // This is a very long comment\n"
3066 " // which is wrapped arround.\n"
3068 "int x = 2; // Is this still aligned?",
3071 Style
.ColumnLimit
= 80;
3072 EXPECT_EQ("int a; // line about a\n"
3076 format("int a; // line about a\n"
3078 " // line about b\n"
3082 Style
.ColumnLimit
= 80;
3083 EXPECT_EQ("int a; // line about a\n"
3085 "// line 1 about b\n"
3086 "// line 2 about b\n"
3088 format("int a; // line about a\n"
3090 " // line 1 about b\n"
3091 " // line 2 about b\n"
3096 TEST_F(FormatTestComments
, AlignTrailingCommentsLeave
) {
3097 FormatStyle Style
= getLLVMStyle();
3098 Style
.AlignTrailingComments
.Kind
= FormatStyle::TCAS_Leave
;
3100 EXPECT_EQ("int a;// do not touch\n"
3101 "int b; // any comments\n"
3102 "int c; // comment\n"
3103 "int d; // comment",
3104 format("int a;// do not touch\n"
3105 "int b; // any comments\n"
3106 "int c; // comment\n"
3107 "int d; // comment",
3110 EXPECT_EQ("int a; // do not touch\n"
3111 "int b; // any comments\n"
3112 "int c; // comment\n"
3114 format("int a; // do not touch\n"
3115 "int b; // any comments\n"
3116 "int c; // comment\n"
3120 EXPECT_EQ("// do not touch\n"
3121 "int a; // any comments\n"
3127 format("// do not touch\n"
3128 "int a; // any comments\n"
3136 EXPECT_EQ("// do not touch\n"
3137 "int a; // any comments\n"
3143 format("// do not touch\n"
3144 "int a; // any comments\n"
3154 verifyFormat("namespace ns {\n"
3157 "} // namespace ns",
3164 Style
.AlignEscapedNewlines
= FormatStyle::ENAS_Left
;
3165 verifyNoChange("#define FOO \\\n"
3166 " /* foo(); */ \\\n"
3170 // Allow to keep 2 empty lines
3171 Style
.MaxEmptyLinesToKeep
= 2;
3172 EXPECT_EQ("// do not touch\n"
3173 "int a; // any comments\n"
3180 format("// do not touch\n"
3181 "int a; // any comments\n"
3189 Style
.MaxEmptyLinesToKeep
= 1;
3191 // Just format comments normally when leaving exceeds the column limit
3192 Style
.ColumnLimit
= 35;
3193 EXPECT_EQ("int foo = 12345; // comment\n"
3195 " 1234; // This is a very long\n"
3196 " // comment which is\n"
3197 " // wrapped arround.",
3198 format("int foo = 12345; // comment\n"
3199 "int bar = 1234; // This is a very long comment\n"
3200 " // which is wrapped arround.",
3203 Style
= getLLVMStyle();
3204 Style
.AlignTrailingComments
.Kind
= FormatStyle::TCAS_Leave
;
3206 Style
.UseTab
= FormatStyle::UT_ForIndentation
;
3207 verifyNoChange("{\n"
3214 "\t\t// h(); // h\n"
3215 "\t\tfoo(); // foo\n"
3221 TEST_F(FormatTestComments
, DontAlignNamespaceComments
) {
3222 FormatStyle Style
= getLLVMStyle();
3223 Style
.NamespaceIndentation
= FormatStyle::NI_All
;
3224 Style
.NamespaceMacros
.push_back("TESTSUITE");
3225 Style
.ShortNamespaceLines
= 0;
3227 StringRef Input
= "namespace A {\n"
3230 " namespace D { //\n"
3231 " } // namespace D\n"
3232 " std::string Foo = Bar; // Comment\n"
3233 " std::string BazString = Baz; // C2\n"
3234 " } // namespace C\n"
3238 EXPECT_TRUE(Style
.FixNamespaceComments
);
3239 EXPECT_EQ(Style
.AlignTrailingComments
.Kind
, FormatStyle::TCAS_Always
);
3240 verifyFormat("namespace A {\n"
3243 " namespace D { //\n"
3244 " } // namespace D\n"
3245 " std::string Foo = Bar; // Comment\n"
3246 " std::string BazString = Baz; // C2\n"
3247 " } // namespace C\n"
3248 " } // TESTSUITE(B)\n"
3252 Style
.AlignTrailingComments
.Kind
= FormatStyle::TCAS_Never
;
3253 verifyFormat("namespace A {\n"
3256 " namespace D { //\n"
3257 " } // namespace D\n"
3258 " std::string Foo = Bar; // Comment\n"
3259 " std::string BazString = Baz; // C2\n"
3260 " } // namespace C\n"
3261 " } // TESTSUITE(B)\n"
3265 Style
.AlignTrailingComments
.Kind
= FormatStyle::TCAS_Leave
;
3266 verifyFormat("namespace A {\n"
3269 " namespace D { //\n"
3270 " } // namespace D\n"
3271 " std::string Foo = Bar; // Comment\n"
3272 " std::string BazString = Baz; // C2\n"
3273 " } // namespace C\n"
3274 " } // TESTSUITE(B)\n"
3278 Style
.FixNamespaceComments
= false;
3279 Style
.AlignTrailingComments
.Kind
= FormatStyle::TCAS_Always
;
3280 verifyFormat("namespace A {\n"
3283 " namespace D { //\n"
3284 " } // namespace D\n"
3285 " std::string Foo = Bar; // Comment\n"
3286 " std::string BazString = Baz; // C2\n"
3287 " } // namespace C\n"
3292 Style
.AlignTrailingComments
.Kind
= FormatStyle::TCAS_Never
;
3293 verifyFormat("namespace A {\n"
3296 " namespace D { //\n"
3297 " } // namespace D\n"
3298 " std::string Foo = Bar; // Comment\n"
3299 " std::string BazString = Baz; // C2\n"
3300 " } // namespace C\n"
3305 Style
.AlignTrailingComments
.Kind
= FormatStyle::TCAS_Leave
;
3306 verifyFormat("namespace A {\n"
3309 " namespace D { //\n"
3310 " } // namespace D\n"
3311 " std::string Foo = Bar; // Comment\n"
3312 " std::string BazString = Baz; // C2\n"
3313 " } // namespace C\n"
3318 Style
.AlignTrailingComments
.Kind
= FormatStyle::TCAS_Always
;
3319 Style
.FixNamespaceComments
= true;
3320 Input
= "namespace A {\n"
3326 verifyFormat("namespace A {\n"
3329 "} // namespace A\n"
3333 Style
.FixNamespaceComments
= false;
3334 verifyFormat(Input
, Style
);
3337 TEST_F(FormatTestComments
, DontAlignOverScope
) {
3338 verifyFormat("if (foo) {\n"
3339 " int aLongVariable; // with comment\n"
3340 " int f; // aligned\n"
3341 "} // not aligned\n"
3342 "int bar; // new align\n"
3343 "int foobar; // group");
3345 verifyFormat("if (foo) {\n"
3348 " int aLongVariable; // with comment\n"
3349 " int f; // aligned\n"
3350 "} // not aligned\n"
3351 "int bar; // new align\n"
3352 "int foobar; // group");
3354 verifyFormat("if (foo) {\n"
3356 "} else if (foo) {\n"
3357 " int aLongVariable; // with comment\n"
3358 " int f; // aligned\n"
3359 "} // not aligned\n"
3360 "int bar; // new align\n"
3361 "int foobar; // group");
3363 verifyFormat("while (foo) {\n"
3364 " int aLongVariable; // with comment\n"
3365 " int f; // aligned\n"
3366 "} // not aligned\n"
3367 "int bar; // new align\n"
3368 "int foobar; // group");
3370 verifyFormat("for (;;) {\n"
3371 " int aLongVariable; // with comment\n"
3372 " int f; // aligned\n"
3373 "} // not aligned\n"
3374 "int bar; // new align\n"
3375 "int foobar; // group");
3377 verifyFormat("do {\n"
3378 " int aLongVariable; // with comment\n"
3379 " int f; // aligned\n"
3380 "} while (foo); // not aligned\n"
3381 "int bar; // new align\n"
3382 "int foobar; // group");
3385 " int aLongVariable; // with comment\n"
3386 "while (foo); // not aigned\n"
3387 "int bar; // new align\n"
3388 "int foobar; // group");
3391 " int aLongVariable; // with comment\n"
3392 "/**/ while (foo); // not aigned\n"
3393 "int bar; // new align\n"
3394 "int foobar; // group");
3396 verifyFormat("switch (foo) {\n"
3398 " int aLongVariable; // with comment\n"
3399 " int f; // aligned\n"
3400 "} // case not aligned\n"
3401 "} // switch also not aligned\n"
3402 "int bar; // new align\n"
3403 "int foobar; // group");
3405 verifyFormat("switch (foo) {\n"
3407 " int aLongVariable; // with comment\n"
3408 " int f; // aligned\n"
3409 "} // case not aligned\n"
3410 "} // switch also not aligned\n"
3411 "int bar; // new align\n"
3412 "int foobar; // group");
3414 verifyFormat("class C {\n"
3415 " int aLongVariable; // with comment\n"
3416 " int f; // aligned\n"
3417 "}; // not aligned\n"
3418 "int bar; // new align\n"
3419 "int foobar; // group");
3421 verifyFormat("struct S {\n"
3422 " int aLongVariable; // with comment\n"
3423 " int f; // aligned\n"
3424 "}; // not aligned\n"
3425 "int bar; // new align\n"
3426 "int foobar; // group");
3428 verifyFormat("union U {\n"
3429 " int aLongVariable; // with comment\n"
3430 " int f; // aligned\n"
3431 "}; // not aligned\n"
3432 "int bar; // new align\n"
3433 "int foobar; // group");
3435 verifyFormat("enum E {\n"
3436 " aLongVariable, // with comment\n"
3438 "}; // not aligned\n"
3439 "int bar; // new align\n"
3440 "int foobar; // group");
3442 verifyFormat("void foo() {\n"
3444 " int aLongVariable; // with comment\n"
3445 " int f; // aligned\n"
3446 " } // not aligned\n"
3447 " int bar; // new align\n"
3448 " int foobar; // group\n"
3451 verifyFormat("auto longLambda = [] { // comment\n"
3452 " int aLongVariable; // with comment\n"
3453 " int f; // aligned\n"
3454 "}; // not aligned\n"
3455 "int bar; // new align\n"
3456 "int foobar; // group\n"
3457 "auto shortLambda = [] { return 5; }; // aligned");
3459 verifyFormat("auto longLambdaResult = [] { // comment\n"
3460 " int aLongVariable; // with comment\n"
3461 " int f; // aligned\n"
3462 "}(); // not aligned\n"
3463 "int bar; // new align\n"
3464 "int foobar; // group\n"
3465 "auto shortLambda = [] { return 5; }(); // aligned");
3468 "auto longLambdaResult = [](auto I, auto J) { // comment\n"
3469 " int aLongVariable; // with comment\n"
3470 " int f; // aligned\n"
3471 "}(\"Input\", 5); // not aligned\n"
3472 "int bar; // new align\n"
3473 "int foobar; // group\n"
3474 "auto shortL = [](auto I, auto J) { return 5; }(\"In\", 5); // aligned");
3476 verifyFormat("enum E1 { V1, V2 }; // Aligned\n"
3477 "enum E2 { LongerNames, InThis, Enum }; // Comments");
3479 verifyFormat("class C {\n"
3480 " int aLongVariable; // with comment\n"
3481 " int f; // aligned\n"
3482 "} /* middle comment */; // not aligned\n"
3483 "int bar; // new align\n"
3484 "int foobar; // group");
3487 TEST_F(FormatTestComments
, AlignsBlockCommentDecorations
) {
3501 // Align a single line.
3539 // Align the end '*/' after a line.
3560 EXPECT_EQ("/* line 1\n"
3562 format("/* line 1\n"
3564 EXPECT_EQ("/* line 1\n"
3566 format("/* line 1\n"
3568 EXPECT_EQ("/* line 1\n"
3570 format("/* line 1\n"
3572 EXPECT_EQ("/* line 1\n"
3574 format("/* line 1\n"
3576 EXPECT_EQ("/* line 1\n"
3578 format("/* line 1\n"
3580 EXPECT_EQ("int i; /* line 1\n"
3582 format("int i; /* line 1\n"
3584 EXPECT_EQ("int i; /* line 1\n"
3586 format("int i; /* line 1\n"
3588 EXPECT_EQ("int i; /* line 1\n"
3590 format("int i; /* line 1\n"
3593 // Align several lines.
3594 EXPECT_EQ("/* line 1\n"
3597 format("/* line 1\n"
3600 EXPECT_EQ("/* line 1\n"
3603 format("/* line 1\n"
3615 // Align with different indent after the decorations.
3629 // Align empty or blank lines.
3641 // Align while breaking and reflowing.
3643 " * long long long\n"
3648 " * long long long long\n"
3652 getLLVMStyleWithColumns(20)));
3655 TEST_F(FormatTestComments
, NoCrash_Bug34236
) {
3656 // This is a test case from a crasher reported in:
3657 // https://bugs.llvm.org/show_bug.cgi?id=34236
3658 // Temporarily disable formatting for readability.
3668 getLLVMStyleWithColumns(80)));
3672 TEST_F(FormatTestComments
, NonTrailingBlockComments
) {
3673 verifyFormat("const /** comment comment */ A = B;",
3674 getLLVMStyleWithColumns(40));
3676 verifyFormat("const /** comment comment comment */ A =\n"
3678 getLLVMStyleWithColumns(40));
3680 EXPECT_EQ("const /** comment comment comment\n"
3683 format("const /** comment comment comment comment */\n"
3685 getLLVMStyleWithColumns(40)));
3688 TEST_F(FormatTestComments
, PythonStyleComments
) {
3689 // Keeps a space after '#'.
3690 EXPECT_EQ("# comment\n"
3694 getTextProtoStyleWithColumns(20)));
3695 EXPECT_EQ("# comment\n"
3697 format("# comment\n"
3699 getTextProtoStyleWithColumns(20)));
3700 // Breaks long comment.
3701 EXPECT_EQ("# comment comment\n"
3704 format("# comment comment comment\n"
3706 getTextProtoStyleWithColumns(20)));
3707 // Indents comments.
3708 EXPECT_EQ("data {\n"
3709 " # comment comment\n"
3714 "# comment comment comment\n"
3716 getTextProtoStyleWithColumns(20)));
3717 EXPECT_EQ("data {\n"
3718 " # comment comment\n"
3722 format("data {# comment comment comment\n"
3724 getTextProtoStyleWithColumns(20)));
3725 // Reflows long comments.
3726 EXPECT_EQ("# comment comment\n"
3727 "# comment comment\n"
3729 format("# comment comment comment\n"
3732 getTextProtoStyleWithColumns(20)));
3733 // Breaks trailing comments.
3734 EXPECT_EQ("k: val # comment\n"
3737 format("k:val#comment comment\n"
3739 getTextProtoStyleWithColumns(20)));
3741 " k: val # comment\n"
3746 format("id {k:val#comment comment\n"
3749 getTextProtoStyleWithColumns(20)));
3750 // Aligns trailing comments.
3751 EXPECT_EQ("k: val # commen1\n"
3758 format("k:val#commen1 commen2\n"
3761 "a:1#commen5 commen6\n"
3763 getTextProtoStyleWithColumns(20)));
3766 TEST_F(FormatTestComments
, BreaksBeforeTrailingUnbreakableSequence
) {
3767 // The end of /* trail */ is exactly at 80 columns, but the unbreakable
3768 // trailing sequence ); after it exceeds the column limit. Make sure we
3769 // correctly break the line in that case.
3770 verifyFormat("int a =\n"
3771 " foo(/* trail */);",
3772 getLLVMStyleWithColumns(23));
3775 TEST_F(FormatTestComments
, ReflowBackslashCrash
) {
3780 "// rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr\n"
3781 "// \\ <log_file> -- --output_directory=\"<output_directory>\"",
3785 "// rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr \\\n"
3786 "// <log_file> -- --output_directory=\"<output_directory>\""));
3790 TEST_F(FormatTestComments
, IndentsLongJavadocAnnotatedLines
) {
3791 FormatStyle Style
= getGoogleStyle(FormatStyle::LK_Java
);
3792 Style
.ColumnLimit
= 60;
3793 FormatStyle Style20
= getGoogleStyle(FormatStyle::LK_Java
);
3794 Style20
.ColumnLimit
= 20;
3797 " * @param x long long long long long long long long long\n"
3801 " * @param x long long long long long long long long long long\n"
3805 " * @param x long long long long long long long long long\n"
3806 " * long long long long long long long long long long\n"
3809 " * @param x long long long long long long long long long "
3810 "long long long long long long long long long long\n"
3814 " * @param x long long long long long long long long long\n"
3815 " * long long long long long long long long long long\n"
3819 " * @param x long long long long long long long long long "
3820 "long long long long long long long long long long long\n"
3824 " * Sentence that\n"
3825 " * should be broken.\n"
3827 " * keep indentation\n"
3830 " * Sentence that should be broken.\n"
3832 " * keep indentation\n"
3837 " * @param l1 long1\n"
3839 " * @param l2 long2\n"
3843 " * @param l1 long1 to break\n"
3844 " * @param l2 long2 to break\n"
3854 " * @param xx to break\n"
3865 " * @param xx to break\n"
3870 FormatStyle JSStyle20
= getGoogleStyle(FormatStyle::LK_JavaScript
);
3871 JSStyle20
.ColumnLimit
= 20;
3873 " * @param l1 long1\n"
3877 " * @param l1 long1 to break\n"
3881 " * @param {l1 long1\n"
3885 " * @param {l1 long1 to break}\n"
3890 TEST_F(FormatTestComments
, SpaceAtLineCommentBegin
) {
3891 FormatStyle Style
= getLLVMStyle();
3892 StringRef NoTextInComment
= " // \n"
3903 format(NoTextInComment
, Style
));
3905 Style
.SpacesInLineCommentPrefix
.Minimum
= 0;
3906 verifyFormat("//#comment", Style
);
3912 format(NoTextInComment
, Style
));
3914 Style
.SpacesInLineCommentPrefix
.Minimum
= 5;
3915 EXPECT_EQ("// #comment", format("//#comment", Style
));
3921 format(NoTextInComment
, Style
));
3923 Style
= getLLVMStyle();
3925 "//Free comment without space\n"
3927 "// Free comment with 3 spaces\n"
3929 "///Free Doxygen without space\n"
3931 "/// Free Doxygen with 3 spaces\n"
3933 "//🐉 A nice dragon\n"
3939 "// 🐉 Another nice dragon\n"
3941 "// \t Three leading spaces following tab\n"
3943 "// \\t Three leading spaces following backslash\n"
3945 "/// A Doxygen Comment with a nested list:\n"
3950 "/// of the inner list\n"
3955 "bool bar(bool b) {\n"
3956 " bool ret1 = true; ///<Doxygenstyle without space\n"
3957 " bool ret2 = true; ///< Doxygenstyle with 3 spaces\n"
3961 " // In function comment\n"
3965 "// if (ret1) {\n" // Commented out at the beginning of the line
3969 " //if (ret1) {\n" // Commtented out at the beginning of the content
3970 " // return ret2;\n"
3973 " return ret1 && ret2;\n"
3979 "} // namespace Bar\n"
3980 "//@Nothing added because of the non ascii char\n"
3982 "//@ Nothing removed because of the non ascii char\n"
3984 "// Comment to move to the left\n"
3988 "//Comment to move to the right\n"
3991 "//} will not move\n"
3993 "//vv will only move\n"
3994 "//} if the line above does";
3996 EXPECT_EQ("// Free comment without space\n"
3998 "// Free comment with 3 spaces\n"
4000 "/// Free Doxygen without space\n"
4002 "/// Free Doxygen with 3 spaces\n"
4004 "// 🐉 A nice dragon\n"
4010 "// 🐉 Another nice dragon\n"
4012 "// \t Three leading spaces following tab\n"
4014 "// \\t Three leading spaces following backslash\n"
4016 "/// A Doxygen Comment with a nested list:\n"
4021 "/// of the inner list\n"
4026 "bool bar(bool b) {\n"
4027 " bool ret1 = true; ///< Doxygenstyle without space\n"
4028 " bool ret2 = true; ///< Doxygenstyle with 3 spaces\n"
4032 " // In function comment\n"
4037 " // return ret2;\n"
4041 " // return ret2;\n"
4044 " return ret1 && ret2;\n"
4046 "} // namespace Foo\n"
4050 "} // namespace Bar\n"
4051 "//@Nothing added because of the non ascii char\n"
4053 "//@ Nothing removed because of the non ascii char\n"
4055 "// Comment to move to the left\n"
4056 "// But not this?\n"
4059 "// Comment to move to the right\n"
4062 "//} will not move\n"
4064 "// vv will only move\n"
4065 "// } if the line above does",
4066 format(Code
, Style
));
4068 Style
.SpacesInLineCommentPrefix
= {0, 0};
4069 EXPECT_EQ("//#comment", format("// #comment", Style
));
4070 EXPECT_EQ("//Free comment without space\n"
4072 "//Free comment with 3 spaces\n"
4074 "///Free Doxygen without space\n"
4076 "///Free Doxygen with 3 spaces\n"
4078 "//🐉 A nice dragon\n"
4084 "//🐉 Another nice dragon\n"
4086 "//\t Three leading spaces following tab\n"
4088 "//\\t Three leading spaces following backslash\n"
4090 "///A Doxygen Comment with a nested list:\n"
4093 "/// - Baz\n" // Here we keep the relative indentation
4095 "/// of the inner list\n"
4100 "bool bar(bool b) {\n"
4101 " bool ret1 = true; ///<Doxygenstyle without space\n"
4102 " bool ret2 = true; ///<Doxygenstyle with 3 spaces\n"
4106 " //In function comment\n"
4111 " // return ret2;\n"
4115 " // return ret2;\n"
4118 " return ret1 && ret2;\n"
4120 "} //namespace Foo\n"
4124 "} //namespace Bar\n"
4125 "//@Nothing added because of the non ascii char\n"
4127 "//@ Nothing removed because of the non ascii char\n"
4129 "//Comment to move to the left\n"
4133 "//Comment to move to the right\n"
4136 "//} will not move\n"
4138 "//vv will only move\n"
4139 "//} if the line above does",
4140 format(Code
, Style
));
4142 Style
.SpacesInLineCommentPrefix
= {2, -1u};
4143 EXPECT_EQ("// Free comment without space\n"
4145 "// Free comment with 3 spaces\n"
4147 "/// Free Doxygen without space\n"
4149 "/// Free Doxygen with 3 spaces\n"
4151 "// 🐉 A nice dragon\n"
4157 "// 🐉 Another nice dragon\n"
4159 "// \t Three leading spaces following tab\n"
4161 "// \\t Three leading spaces following backslash\n"
4163 "/// A Doxygen Comment with a nested list:\n"
4168 "/// of the inner list\n"
4173 "bool bar(bool b) {\n"
4174 " bool ret1 = true; ///< Doxygenstyle without space\n"
4175 " bool ret2 = true; ///< Doxygenstyle with 3 spaces\n"
4179 " // In function comment\n"
4184 " // return ret2;\n"
4188 " // return ret2;\n"
4191 " return ret1 && ret2;\n"
4193 "} // namespace Foo\n"
4197 "} // namespace Bar\n"
4198 "//@Nothing added because of the non ascii char\n"
4200 "//@ Nothing removed because of the non ascii char\n"
4202 "// Comment to move to the left\n"
4203 "// But not this?\n"
4206 "// Comment to move to the right\n"
4209 "//} will not move\n"
4211 "// vv will only move\n"
4212 "// } if the line above does",
4213 format(Code
, Style
));
4215 Style
= getLLVMStyleWithColumns(20);
4216 StringRef WrapCode
= "//Lorem ipsum dolor sit amet\n"
4218 "// Lorem ipsum dolor sit amet\n"
4220 "void f() {//Hello World\n"
4223 EXPECT_EQ("// Lorem ipsum dolor\n"
4227 "// dolor sit amet\n"
4229 "void f() { // Hello\n"
4232 format(WrapCode
, Style
));
4234 Style
.SpacesInLineCommentPrefix
= {0, 0};
4235 EXPECT_EQ("//Lorem ipsum dolor\n"
4239 "//dolor sit amet\n"
4241 "void f() { //Hello\n"
4244 format(WrapCode
, Style
));
4246 Style
.SpacesInLineCommentPrefix
= {1, 1};
4247 EXPECT_EQ("// Lorem ipsum dolor\n"
4251 "// dolor sit amet\n"
4253 "void f() { // Hello\n"
4256 format(WrapCode
, Style
));
4263 "// loooooooooooooooooooooooooooooong\n"
4264 "// commentcomments\n"
4265 "// normal comments",
4266 format("// loooooooooooooooooooooooooooooong commentcomments\n"
4267 "// normal comments",
4270 Style
.SpacesInLineCommentPrefix
= {3, 3};
4271 EXPECT_EQ("// Lorem ipsum\n"
4272 "// dolor sit amet\n"
4278 "void f() { // Hello\n"
4281 format(WrapCode
, Style
));
4283 Style
= getLLVMStyleWithColumns(20);
4284 StringRef LotsOfSpaces
= "// This are more spaces "
4285 "than the ColumnLimit, what now?\n"
4289 "// This is a text to split in multiple "
4290 "lines, please. Thank you very much!\n"
4292 "// A comment with\n"
4293 "// some indentation that has to be split.\n"
4294 "// And now without";
4295 EXPECT_EQ("// This are more spaces "
4296 "than the ColumnLimit, what now?\n"
4300 "// This is a text to\n"
4301 "// split in multiple\n"
4302 "// lines, please.\n"
4303 "// Thank you very\n"
4306 "// A comment with\n"
4309 "// that has to be\n"
4311 "// And now without",
4312 format(LotsOfSpaces
, Style
));
4314 Style
.SpacesInLineCommentPrefix
= {0, 0};
4315 EXPECT_EQ("//This are more\n"
4316 "//spaces than the\n"
4317 "//ColumnLimit, what\n"
4322 "//This is a text to\n"
4323 "//split in multiple\n"
4324 "//lines, please.\n"
4325 "//Thank you very\n"
4328 "//A comment with\n"
4329 "// some indentation\n"
4330 "// that has to be\n"
4332 "//And now without",
4333 format(LotsOfSpaces
, Style
));
4335 Style
.SpacesInLineCommentPrefix
= {3, 3};
4336 EXPECT_EQ("// This are more\n"
4337 "// spaces than the\n"
4343 "// This is a text\n"
4345 "// multiple lines,\n"
4346 "// please. Thank\n"
4347 "// you very much!\n"
4349 "// A comment with\n"
4354 "// And now without",
4355 format(LotsOfSpaces
, Style
));
4357 Style
.SpacesInLineCommentPrefix
= {30, -1u};
4358 EXPECT_EQ("// This are more spaces than the "
4359 "ColumnLimit, what now?\n"
4363 "// This is a text to split in "
4364 "multiple lines, please. Thank you very much!\n"
4366 "// A comment with\n"
4367 "// some indentation that has to be "
4369 "// And now without",
4370 format(LotsOfSpaces
, Style
));
4372 Style
.SpacesInLineCommentPrefix
= {2, 4};
4373 EXPECT_EQ("// A Comment to be\n"
4377 "// A Comment to be\n"
4381 "// A Comment to be\n"
4385 "// A Comment to be\n"
4400 format("//A Comment to be moved\n"
4403 "// A Comment to be moved\n"
4406 "// A Comment to be moved\n"
4409 "// A Comment to be moved\n"
4412 "// A Comment to be moved\n"
4415 "// A Comment to be moved\n"
4418 "// A Comment to be moved\n"
4422 Style
.ColumnLimit
= 30;
4423 EXPECT_EQ("int i; // A Comment to be\n"
4427 "int i; // A Comment to be\n"
4431 "int i; // A Comment to be\n"
4435 "int i; // A Comment to be\n"
4439 "int i; // A Comment to be\n"
4443 "int i; // A Comment to be\n"
4447 "int i; // A Comment to be\n"
4450 format("int i;//A Comment to be moved\n"
4453 "int i;// A Comment to be moved\n"
4456 "int i;// A Comment to be moved\n"
4459 "int i;// A Comment to be moved\n"
4462 "int i;// A Comment to be moved\n"
4465 "int i;// A Comment to be moved\n"
4468 "int i;// A Comment to be moved\n"
4472 Style
= getLLVMStyleWithColumns(0);
4473 EXPECT_EQ("// Free comment without space\n"
4475 "// Free comment with 3 spaces\n"
4477 "/// Free Doxygen without space\n"
4479 "/// Free Doxygen with 3 spaces\n"
4481 "// 🐉 A nice dragon\n"
4487 "// 🐉 Another nice dragon\n"
4489 "// \t Three leading spaces following tab\n"
4491 "// \\t Three leading spaces following backslash\n"
4493 "/// A Doxygen Comment with a nested list:\n"
4498 "/// of the inner list\n"
4503 "bool bar(bool b) {\n"
4504 " bool ret1 = true; ///< Doxygenstyle without space\n"
4505 " bool ret2 = true; ///< Doxygenstyle with 3 spaces\n"
4509 " // In function comment\n"
4514 " // return ret2;\n"
4518 " // return ret2;\n"
4521 " return ret1 && ret2;\n"
4523 "} // namespace Foo\n"
4527 "} // namespace Bar\n"
4528 "//@Nothing added because of the non ascii char\n"
4530 "//@ Nothing removed because of the non ascii char\n"
4532 "// Comment to move to the left\n"
4533 "// But not this?\n"
4536 "// Comment to move to the right\n"
4539 "//} will not move\n"
4541 "// vv will only move\n"
4542 "// } if the line above does",
4543 format(Code
, Style
));
4545 Style
.SpacesInLineCommentPrefix
= {0, 0};
4546 EXPECT_EQ("//Free comment without space\n"
4548 "//Free comment with 3 spaces\n"
4550 "///Free Doxygen without space\n"
4552 "///Free Doxygen with 3 spaces\n"
4554 "//🐉 A nice dragon\n"
4560 "//🐉 Another nice dragon\n"
4562 "//\t Three leading spaces following tab\n"
4564 "//\\t Three leading spaces following backslash\n"
4566 "///A Doxygen Comment with a nested list:\n"
4569 "/// - Baz\n" // Here we keep the relative indentation
4571 "/// of the inner list\n"
4576 "bool bar(bool b) {\n"
4577 " bool ret1 = true; ///<Doxygenstyle without space\n"
4578 " bool ret2 = true; ///<Doxygenstyle with 3 spaces\n"
4582 " //In function comment\n"
4587 " // return ret2;\n"
4591 " // return ret2;\n"
4594 " return ret1 && ret2;\n"
4596 "} //namespace Foo\n"
4600 "} //namespace Bar\n"
4601 "//@Nothing added because of the non ascii char\n"
4603 "//@ Nothing removed because of the non ascii char\n"
4605 "//Comment to move to the left\n"
4609 "//Comment to move to the right\n"
4612 "//} will not move\n"
4614 "//vv will only move\n"
4615 "//} if the line above does",
4616 format(Code
, Style
));
4618 Style
.SpacesInLineCommentPrefix
= {2, -1u};
4619 EXPECT_EQ("// Free comment without space\n"
4621 "// Free comment with 3 spaces\n"
4623 "/// Free Doxygen without space\n"
4625 "/// Free Doxygen with 3 spaces\n"
4627 "// 🐉 A nice dragon\n"
4633 "// 🐉 Another nice dragon\n"
4635 "// \t Three leading spaces following tab\n"
4637 "// \\t Three leading spaces following backslash\n"
4639 "/// A Doxygen Comment with a nested list:\n"
4644 "/// of the inner list\n"
4649 "bool bar(bool b) {\n"
4650 " bool ret1 = true; ///< Doxygenstyle without space\n"
4651 " bool ret2 = true; ///< Doxygenstyle with 3 spaces\n"
4655 " // In function comment\n"
4660 " // return ret2;\n"
4664 " // return ret2;\n"
4667 " return ret1 && ret2;\n"
4669 "} // namespace Foo\n"
4673 "} // namespace Bar\n"
4674 "//@Nothing added because of the non ascii char\n"
4676 "//@ Nothing removed because of the non ascii char\n"
4678 "// Comment to move to the left\n"
4679 "// But not this?\n"
4682 "// Comment to move to the right\n"
4685 "//} will not move\n"
4687 "// vv will only move\n"
4688 "// } if the line above does",
4689 format(Code
, Style
));
4692 TEST_F(FormatTestComments
, SplitCommentIntroducers
) {
4701 getLLVMStyleWithColumns(10)));
4706 } // end namespace format
4707 } // end namespace clang