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
);
1073 auto Style
= getLLVMStyle();
1074 Style
.IndentPPDirectives
= FormatStyle::PPDIS_BeforeHash
;
1075 verifyFormat("#ifdef FOO\n"
1077 " #define Foo foo\n"
1080 " #define Bar bar\n"
1085 TEST_F(FormatTestComments
, SplitsLongLinesInComments
) {
1086 // FIXME: Do we need to fix up the " */" at the end?
1087 // It doesn't look like any of our current logic triggers this.
1088 EXPECT_EQ("/* This is a long\n"
1090 " * doesn't fit on\n"
1096 "fit on one line. */",
1097 getLLVMStyleWithColumns(20)));
1103 format("/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10)));
1109 format("\\\n/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10)));
1112 "comment that doesn't\n"
1113 "fit on one line.\n"
1117 "comment that doesn't "
1118 "fit on one line. \n"
1120 getLLVMStyleWithColumns(20)));
1122 " * This is a long\n"
1124 " * doesn't fit on\n"
1128 " * This is a long "
1133 getLLVMStyleWithColumns(20)));
1135 " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n"
1136 " * so_it_should_be_broken\n"
1137 " * wherever_a_space_occurs\n"
1140 " * This_is_a_comment_with_words_that_dont_fit_on_one_line "
1141 " so_it_should_be_broken "
1142 " wherever_a_space_occurs \n"
1144 getLLVMStyleWithColumns(20)));
1146 " * This_comment_can_not_be_broken_into_lines\n"
1149 " * This_comment_can_not_be_broken_into_lines\n"
1151 getLLVMStyleWithColumns(20)));
1154 " This is another\n"
1155 " long comment that\n"
1156 " doesn't fit on one\n"
1157 " line 1234567890\n"
1163 " long comment that "
1164 " doesn't fit on one"
1165 " line 1234567890\n"
1168 getLLVMStyleWithColumns(20)));
1172 " * another comment\n"
1173 " * t hat doesn' t\n"
1174 " * fit on one l i\n"
1187 getLLVMStyleWithColumns(20)));
1189 " * This is a long\n"
1191 " * doesn't fit on\n"
1195 " * This is a long comment that doesn't fit on one line\n"
1197 getLLVMStyleWithColumns(20)));
1199 " if (something) /* This is a\n"
1205 " if (something) /* This is a long comment */\n"
1208 getLLVMStyleWithColumns(30)));
1210 EXPECT_EQ("/* A comment before\n"
1212 " * definition */\n"
1214 format("/* A comment before a macro definition */\n"
1216 getLLVMStyleWithColumns(20)));
1218 EXPECT_EQ("/* some comment\n"
1219 " * a comment that\n"
1220 " * we break another\n"
1221 " * comment we have\n"
1222 " * to break a left\n"
1225 format(" /* some comment\n"
1226 " * a comment that we break\n"
1227 " * another comment we have to break\n"
1228 "* a left comment\n"
1230 getLLVMStyleWithColumns(20)));
1233 " * multiline block\n"
1238 " * multiline block comment\n"
1241 getLLVMStyleWithColumns(20)));
1243 // This reproduces a crashing bug where both adaptStartOfLine and
1244 // getCommentSplit were trying to wrap after the "/**".
1245 verifyFormat("/** multilineblockcommentwithnowrapopportunity */",
1246 getLLVMStyleWithColumns(20));
1257 EXPECT_EQ("/* a a */",
1258 format("/* a a */", getLLVMStyleWithColumns(15)));
1259 EXPECT_EQ("/* a a bc */",
1260 format("/* a a bc */", getLLVMStyleWithColumns(15)));
1261 EXPECT_EQ("/* aaa aaa\n"
1263 format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15)));
1264 EXPECT_EQ("/* aaa aaa\n"
1266 format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15)));
1269 TEST_F(FormatTestComments
, SplitsLongLinesInCommentsInPreprocessor
) {
1270 EXPECT_EQ("#define X \\\n"
1273 " Macro comment \\\n"
1278 format("#define X \\\n"
1281 " Macro comment with a long line\n"
1284 getLLVMStyleWithColumns(20)));
1285 EXPECT_EQ("#define X \\\n"
1286 " /* Macro comment \\\n"
1290 format("#define X \\\n"
1291 " /* Macro comment with a long\n"
1294 getLLVMStyleWithColumns(20)));
1295 EXPECT_EQ("#define X \\\n"
1296 " /* Macro comment \\\n"
1297 " * with a long \\\n"
1300 format("#define X \\\n"
1301 " /* Macro comment with a long line */ \\\n"
1303 getLLVMStyleWithColumns(20)));
1306 TEST_F(FormatTestComments
, KeepsTrailingPPCommentsAndSectionCommentsSeparate
) {
1307 verifyFormat("#ifdef A // line about A\n"
1308 "// section comment\n"
1310 getLLVMStyleWithColumns(80));
1311 verifyFormat("#ifdef A // line 1 about A\n"
1312 " // line 2 about A\n"
1313 "// section comment\n"
1315 getLLVMStyleWithColumns(80));
1316 EXPECT_EQ("#ifdef A // line 1 about A\n"
1317 " // line 2 about A\n"
1318 "// section comment\n"
1320 format("#ifdef A // line 1 about A\n"
1321 " // line 2 about A\n"
1322 "// section comment\n"
1324 getLLVMStyleWithColumns(80)));
1325 verifyFormat("int f() {\n"
1327 "#ifdef A // comment about A\n"
1328 " // section comment 1\n"
1329 " // section comment 2\n"
1331 "#else // comment about #else\n"
1332 " // section comment 3\n"
1336 getLLVMStyleWithColumns(80));
1339 TEST_F(FormatTestComments
, AlignsPPElseEndifComments
) {
1340 verifyFormat("#if A\n"
1344 getLLVMStyleWithColumns(20));
1345 verifyFormat("#if A\n"
1349 getLLVMStyleWithColumns(20));
1360 getLLVMStyleWithColumns(20)));
1363 TEST_F(FormatTestComments
, CommentsInStaticInitializers
) {
1365 "static SomeType type = {aaaaaaaaaaaaaaaaaaaa, /* comment */\n"
1366 " aaaaaaaaaaaaaaaaaaaa /* comment */,\n"
1367 " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n"
1368 " aaaaaaaaaaaaaaaaaaaa, // comment\n"
1369 " aaaaaaaaaaaaaaaaaaaa};",
1370 format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n"
1371 " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n"
1372 " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n"
1373 " aaaaaaaaaaaaaaaaaaaa , // comment\n"
1374 " aaaaaaaaaaaaaaaaaaaa };"));
1375 verifyFormat("static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
1376 " bbbbbbbbbbb, ccccccccccc};");
1377 verifyFormat("static SomeType type = {aaaaaaaaaaa,\n"
1378 " // comment for bb....\n"
1379 " bbbbbbbbbbb, ccccccccccc};");
1381 "static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
1382 " bbbbbbbbbbb, ccccccccccc};");
1383 verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n"
1384 " // comment for bb....\n"
1385 " bbbbbbbbbbb, ccccccccccc};");
1387 verifyFormat("S s = {{a, b, c}, // Group #1\n"
1388 " {d, e, f}, // Group #2\n"
1389 " {g, h, i}}; // Group #3");
1390 verifyFormat("S s = {{// Group #1\n"
1397 EXPECT_EQ("S s = {\n"
1398 " // Some comment\n"
1401 " // Comment after empty line\n"
1404 " // Some comment\n"
1407 " // Comment after empty line\n"
1410 EXPECT_EQ("S s = {\n"
1411 " /* Some comment */\n"
1414 " /* Comment after empty line */\n"
1417 " /* Some comment */\n"
1420 " /* Comment after empty line */\n"
1423 verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n"
1424 " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
1425 " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
1426 " 0x00, 0x00, 0x00, 0x00}; // comment");
1429 TEST_F(FormatTestComments
, LineCommentsAfterRightBrace
) {
1430 EXPECT_EQ("if (true) { // comment about branch\n"
1431 " // comment about f\n"
1434 format("if (true) { // comment about branch\n"
1435 " // comment about f\n"
1438 getLLVMStyleWithColumns(80)));
1439 EXPECT_EQ("if (1) { // if line 1\n"
1445 "} else { // else line 1\n"
1451 format("if (1) { // if line 1\n"
1457 "} else { // else line 1\n"
1463 EXPECT_EQ("do { // line 1\n"
1468 format("do { // line 1\n"
1473 getLLVMStyleWithColumns(80)));
1474 EXPECT_EQ("while (a < b) { // line 1\n"
1479 format("while (a < b) {// line 1\n"
1484 getLLVMStyleWithColumns(80)));
1487 TEST_F(FormatTestComments
, ReflowsComments
) {
1488 // Break a long line and reflow with the full next line.
1489 EXPECT_EQ("// long long long\n"
1491 format("// long long long long\n"
1493 getLLVMStyleWithColumns(20)));
1495 // Keep the trailing newline while reflowing.
1496 EXPECT_EQ("// long long long\n"
1498 format("// long long long long\n"
1500 getLLVMStyleWithColumns(20)));
1502 // Break a long line and reflow with a part of the next line.
1503 EXPECT_EQ("// long long long\n"
1506 format("// long long long long\n"
1507 "// long long_long",
1508 getLLVMStyleWithColumns(20)));
1510 // Break but do not reflow if the first word from the next line is too long.
1511 EXPECT_EQ("// long long long\n"
1513 "// long_long_long",
1514 format("// long long long long\n"
1515 "// long_long_long",
1516 getLLVMStyleWithColumns(20)));
1518 // Don't break or reflow short lines.
1519 verifyFormat("// long\n"
1520 "// long long long lo\n"
1521 "// long long long lo\n"
1523 getLLVMStyleWithColumns(20));
1525 // Keep prefixes and decorations while reflowing.
1526 EXPECT_EQ("/// long long long\n"
1528 format("/// long long long long\n"
1530 getLLVMStyleWithColumns(20)));
1531 EXPECT_EQ("//! long long long\n"
1533 format("//! long long long long\n"
1535 getLLVMStyleWithColumns(20)));
1536 EXPECT_EQ("/* long long long\n"
1538 format("/* long long long long\n"
1540 getLLVMStyleWithColumns(20)));
1541 EXPECT_EQ("///< long long long\n"
1543 format("///< long long long long\n"
1545 getLLVMStyleWithColumns(20)));
1546 EXPECT_EQ("//!< long long long\n"
1548 format("//!< long long long long\n"
1550 getLLVMStyleWithColumns(20)));
1552 // Don't bring leading whitespace up while reflowing.
1553 EXPECT_EQ("/* long long long\n"
1554 " * long long long\n"
1556 format("/* long long long long\n"
1559 getLLVMStyleWithColumns(20)));
1561 // Reflow the last line of a block comment with its trailing '*/'.
1562 EXPECT_EQ("/* long long long\n"
1564 format("/* long long long long\n"
1566 getLLVMStyleWithColumns(20)));
1568 // Reflow two short lines; keep the postfix of the last one.
1569 EXPECT_EQ("/* long long long\n"
1570 " * long long long */",
1571 format("/* long long long long\n"
1574 getLLVMStyleWithColumns(20)));
1576 // Put the postfix of the last short reflow line on a newline if it doesn't
1578 EXPECT_EQ("/* long long long\n"
1579 " * long long longg\n"
1581 format("/* long long long long\n"
1584 getLLVMStyleWithColumns(20)));
1586 // Reflow lines with leading whitespace.
1589 " * long long long\n"
1590 " * long long long\n"
1591 " * long long long\n"
1596 " * long long long long\n"
1598 " * long long long long\n"
1601 getLLVMStyleWithColumns(20)));
1603 // Break single line block comments that are first in the line with ' *'
1605 EXPECT_EQ("/* long long long\n"
1607 format("/* long long long long */", getLLVMStyleWithColumns(20)));
1609 // Break single line block comment that are not first in the line with ' '
1611 EXPECT_EQ("int i; /* long long\n"
1613 format("int i; /* long long long */", getLLVMStyleWithColumns(20)));
1615 // Reflow a line that goes just over the column limit.
1616 EXPECT_EQ("// long long long\n"
1618 format("// long long long lon\n"
1620 getLLVMStyleWithColumns(20)));
1622 // Stop reflowing if the next line has a different indentation than the
1624 EXPECT_EQ("// long long long\n"
1628 format("// long long long long\n"
1631 getLLVMStyleWithColumns(20)));
1633 // Reflow into the last part of a really long line that has been broken into
1635 EXPECT_EQ("// long long long\n"
1636 "// long long long\n"
1637 "// long long long",
1638 format("// long long long long long long long long\n"
1640 getLLVMStyleWithColumns(20)));
1642 // Break the first line, then reflow the beginning of the second and third
1644 EXPECT_EQ("// long long long\n"
1645 "// lon1 lon2 lon2\n"
1646 "// lon2 lon3 lon3",
1647 format("// long long long lon1\n"
1648 "// lon2 lon2 lon2\n"
1650 getLLVMStyleWithColumns(20)));
1652 // Reflow the beginning of the second line, then break the rest.
1653 EXPECT_EQ("// long long long\n"
1654 "// lon1 lon2 lon2\n"
1655 "// lon2 lon2 lon2\n"
1657 format("// long long long lon1\n"
1658 "// lon2 lon2 lon2 lon2 lon2 lon3",
1659 getLLVMStyleWithColumns(20)));
1661 // Shrink the first line, then reflow the second line up.
1662 EXPECT_EQ("// long long long", format("// long long\n"
1664 getLLVMStyleWithColumns(20)));
1666 // Don't shrink leading whitespace.
1667 verifyNoChange("int i; /// a", getLLVMStyleWithColumns(20));
1669 // Shrink trailing whitespace if there is no postfix and reflow.
1670 EXPECT_EQ("// long long long\n"
1672 format("// long long long long \n"
1674 getLLVMStyleWithColumns(20)));
1676 // Shrink trailing whitespace to a single one if there is postfix.
1677 EXPECT_EQ("/* long long long */",
1678 format("/* long long long */", getLLVMStyleWithColumns(20)));
1680 // Break a block comment postfix if exceeding the line limit.
1681 EXPECT_EQ("/* long\n"
1683 format("/* long */", getLLVMStyleWithColumns(20)));
1685 // Reflow indented comments.
1687 " // long long long\n"
1689 " int i; /* long lon\n"
1694 " // long long long long\n"
1696 " int i; /* long lon g\n"
1699 getLLVMStyleWithColumns(20)));
1701 // Don't realign trailing comments after reflow has happened.
1702 EXPECT_EQ("// long long long\n"
1705 format("// long long long long\n"
1708 getLLVMStyleWithColumns(20)));
1709 EXPECT_EQ("// long long long\n"
1710 "// longng long long\n"
1712 format("// long long long longng\n"
1713 "// long long long\n"
1715 getLLVMStyleWithColumns(20)));
1717 // Reflow lines after a broken line.
1718 EXPECT_EQ("int a; // Trailing\n"
1722 format("int a; // Trailing comment\n"
1726 getLLVMStyleWithColumns(20)));
1727 EXPECT_EQ("/// This long line\n"
1728 "/// gets reflown.",
1729 format("/// This long line gets\n"
1731 getLLVMStyleWithColumns(20)));
1732 EXPECT_EQ("//! This long line\n"
1733 "//! gets reflown.",
1734 format(" //! This long line gets\n"
1736 getLLVMStyleWithColumns(20)));
1737 EXPECT_EQ("/* This long line\n"
1738 " * gets reflown.\n"
1740 format("/* This long line gets\n"
1743 getLLVMStyleWithColumns(20)));
1745 // Reflow after indentation makes a line too long.
1747 " // long long long\n"
1751 "// long long long lo\n"
1754 getLLVMStyleWithColumns(20)));
1756 // Break and reflow multiple lines.
1758 " * Reflow the end of\n"
1759 " * line by 11 22 33\n"
1763 " * Reflow the end of line\n"
1770 getLLVMStyleWithColumns(20)));
1771 EXPECT_EQ("/// First line gets\n"
1772 "/// broken. Second\n"
1775 "/// broken. Third\n"
1776 "/// gets reflown.",
1777 format("/// First line gets broken.\n"
1778 "/// Second line gets reflown and broken.\n"
1779 "/// Third gets reflown.",
1780 getLLVMStyleWithColumns(20)));
1781 EXPECT_EQ("int i; // first long\n"
1784 format("int i; // first long long\n"
1786 getLLVMStyleWithColumns(20)));
1788 " // first long line\n"
1790 " // long line line\n"
1791 " // third long line\n"
1795 " // first long line line\n"
1796 " // second long line line\n"
1797 " // third long line line\n"
1799 getLLVMStyleWithColumns(20)));
1800 EXPECT_EQ("int i; /* first line\n"
1805 format("int i; /* first line\n"
1809 getLLVMStyleWithColumns(20)));
1811 // Reflow the last two lines of a section that starts with a line having
1812 // different indentation.
1813 EXPECT_EQ("// long\n"
1814 "// long long long\n"
1817 "// long long long long\n"
1819 getLLVMStyleWithColumns(20)));
1821 // Keep the block comment endling '*/' while reflowing.
1822 EXPECT_EQ("/* Long long long\n"
1824 format("/* Long long long line\n"
1826 getLLVMStyleWithColumns(20)));
1828 // Don't reflow between separate blocks of comments.
1829 EXPECT_EQ("/* First comment\n"
1830 " * block will */\n"
1833 format("/* First comment block\n"
1837 getLLVMStyleWithColumns(20)));
1839 // Don't reflow across blank comment lines.
1840 EXPECT_EQ("int i; // This long\n"
1845 format("int i; // This long line gets broken.\n"
1848 getLLVMStyleWithColumns(20)));
1850 " /// long long long\n"
1856 " /// long long long long\n"
1861 getLLVMStyleWithColumns(20)));
1862 EXPECT_EQ("//! long long long\n"
1866 format("//! long long long long\n"
1869 getLLVMStyleWithColumns(20)));
1870 EXPECT_EQ("/* long long long\n"
1874 format("/* long long long long\n"
1877 getLLVMStyleWithColumns(20)));
1878 EXPECT_EQ("/* long long long\n"
1882 format("/* long long long long\n"
1885 getLLVMStyleWithColumns(20)));
1887 // Don't reflow lines having content that is a single character.
1888 EXPECT_EQ("// long long long\n"
1891 format("// long long long long\n"
1893 getLLVMStyleWithColumns(20)));
1895 // Don't reflow lines starting with two punctuation characters.
1896 EXPECT_EQ("// long long long\n"
1899 format("// long long long long\n"
1901 getLLVMStyleWithColumns(20)));
1903 // Don't reflow lines starting with '@'.
1904 EXPECT_EQ("// long long long\n"
1907 format("// long long long long\n"
1909 getLLVMStyleWithColumns(20)));
1911 // Don't reflow lines starting with 'TODO'.
1912 EXPECT_EQ("// long long long\n"
1915 format("// long long long long\n"
1917 getLLVMStyleWithColumns(20)));
1919 // Don't reflow lines starting with 'FIXME'.
1920 EXPECT_EQ("// long long long\n"
1923 format("// long long long long\n"
1925 getLLVMStyleWithColumns(20)));
1927 // Don't reflow lines starting with 'XXX'.
1928 EXPECT_EQ("// long long long\n"
1931 format("// long long long long\n"
1933 getLLVMStyleWithColumns(20)));
1935 // Don't reflow comment pragmas.
1936 EXPECT_EQ("// long long long\n"
1939 format("// long long long long\n"
1941 getLLVMStyleWithColumns(20)));
1942 EXPECT_EQ("/* long long long\n"
1946 format("/* long long long long\n"
1949 getLLVMStyleWithColumns(20)));
1951 // Reflow lines that have a non-punctuation character among their first 2
1953 EXPECT_EQ("// long long long\n"
1955 format("// long long long long\n"
1957 getLLVMStyleWithColumns(20)));
1959 // Don't reflow between separate blocks of comments.
1960 EXPECT_EQ("/* First comment\n"
1961 " * block will */\n"
1964 format("/* First comment block\n"
1968 getLLVMStyleWithColumns(20)));
1970 // Don't reflow lines having different indentation.
1971 EXPECT_EQ("// long long long\n"
1974 format("// long long long long\n"
1976 getLLVMStyleWithColumns(20)));
1978 // Don't reflow separate bullets in list
1979 EXPECT_EQ("// - long long long\n"
1982 format("// - long long long long\n"
1984 getLLVMStyleWithColumns(20)));
1985 EXPECT_EQ("// * long long long\n"
1988 format("// * long long long long\n"
1990 getLLVMStyleWithColumns(20)));
1991 EXPECT_EQ("// + long long long\n"
1994 format("// + long long long long\n"
1996 getLLVMStyleWithColumns(20)));
1997 EXPECT_EQ("// 1. long long long\n"
2000 format("// 1. long long long long\n"
2002 getLLVMStyleWithColumns(20)));
2003 EXPECT_EQ("// -# long long long\n"
2006 format("// -# long long long long\n"
2008 getLLVMStyleWithColumns(20)));
2010 EXPECT_EQ("// - long long long\n"
2011 "// long long long\n"
2013 format("// - long long long long\n"
2016 getLLVMStyleWithColumns(20)));
2017 EXPECT_EQ("// - long long long\n"
2018 "// long long long\n"
2021 format("// - long long long long\n"
2022 "// long long long\n"
2024 getLLVMStyleWithColumns(20)));
2026 // Large number (>2 digits) are not list items
2027 EXPECT_EQ("// long long long\n"
2028 "// long 1024. long.",
2029 format("// long long long long\n"
2031 getLLVMStyleWithColumns(20)));
2033 // Do not break before number, to avoid introducing a non-reflowable doxygen
2035 EXPECT_EQ("// long long\n"
2036 "// long 10. long.",
2037 format("// long long long 10.\n"
2039 getLLVMStyleWithColumns(20)));
2041 // Don't break or reflow after implicit string literals.
2042 verifyFormat("#include <t> // l l l\n"
2044 getLLVMStyleWithColumns(20));
2046 // Don't break or reflow comments on import lines.
2047 EXPECT_EQ("#include \"t\" /* l l l\n"
2049 format("#include \"t\" /* l l l\n"
2051 getLLVMStyleWithColumns(20)));
2053 // Don't reflow between different trailing comment sections.
2054 EXPECT_EQ("int i; // long long\n"
2056 "int j; // long long\n"
2058 format("int i; // long long long\n"
2059 "int j; // long long long",
2060 getLLVMStyleWithColumns(20)));
2062 // Don't reflow if the first word on the next line is longer than the
2063 // available space at current line.
2064 EXPECT_EQ("int i; // trigger\n"
2067 format("int i; // trigger reflow\n"
2069 getLLVMStyleWithColumns(20)));
2071 // Simple case that correctly handles reflow in parameter lists.
2072 EXPECT_EQ("a = f(/* looooooooong\n"
2076 format("a = f(/* looooooooong long\n* long\n*/ a);",
2077 getLLVMStyleWithColumns(22)));
2078 // Tricky case that has fewer lines if we reflow the comment, ending up with
2080 EXPECT_EQ("a = f(/* loooooong\n"
2084 format("a = f(/* loooooong long\n* long\n*/ a);",
2085 getLLVMStyleWithColumns(22)));
2087 // Keep empty comment lines.
2088 EXPECT_EQ("/**/", format(" /**/", getLLVMStyleWithColumns(20)));
2089 EXPECT_EQ("/* */", format(" /* */", getLLVMStyleWithColumns(20)));
2090 EXPECT_EQ("/* */", format(" /* */", getLLVMStyleWithColumns(20)));
2091 EXPECT_EQ("//", format(" // ", getLLVMStyleWithColumns(20)));
2092 EXPECT_EQ("///", format(" /// ", getLLVMStyleWithColumns(20)));
2095 TEST_F(FormatTestComments
, ReflowsCommentsPrecise
) {
2096 // FIXME: This assumes we do not continue compressing whitespace once we are
2097 // in reflow mode. Consider compressing whitespace.
2099 // Test that we stop reflowing precisely at the column limit.
2100 // After reflowing, "// reflows into foo" does not fit the column limit,
2101 // so we compress the whitespace.
2102 EXPECT_EQ("// some text that\n"
2103 "// reflows into foo",
2104 format("// some text that reflows\n"
2106 getLLVMStyleWithColumns(20)));
2107 // Given one more column, "// reflows into foo" does fit the limit, so we
2108 // do not compress the whitespace.
2109 EXPECT_EQ("// some text that\n"
2110 "// reflows into foo",
2111 format("// some text that reflows\n"
2113 getLLVMStyleWithColumns(21)));
2115 // Make sure that we correctly account for the space added in the reflow case
2116 // when making the reflowing decision.
2117 // First, when the next line ends precisely one column over the limit, do not
2119 EXPECT_EQ("// some text that\n"
2122 format("// some text that reflows\n"
2124 getLLVMStyleWithColumns(21)));
2125 // Secondly, when the next line ends later, but the first word in that line
2126 // is precisely one column over the limit, do not reflow.
2127 EXPECT_EQ("// some text that\n"
2130 format("// some text that reflows\n"
2132 getLLVMStyleWithColumns(21)));
2135 TEST_F(FormatTestComments
, ReflowsCommentsWithExtraWhitespace
) {
2137 EXPECT_EQ("// some text\n"
2139 format("// some text that\n"
2141 getLLVMStyleWithColumns(16)));
2142 EXPECT_EQ("// some text\n"
2144 format("// some text that\n"
2146 getLLVMStyleWithColumns(16)));
2147 EXPECT_EQ("/* some text\n"
2148 " * that re flows\n"
2150 format("/* some text that\n"
2153 getLLVMStyleWithColumns(16)));
2154 // FIXME: We do not reflow if the indent of two subsequent lines differs;
2155 // given that this is different behavior from block comments, do we want
2157 EXPECT_EQ("// some text\n"
2160 format("// some text that\n"
2162 getLLVMStyleWithColumns(16)));
2163 // Space within parts of a line that fit.
2164 // FIXME: Use the earliest possible split while reflowing to compress the
2165 // whitespace within the line.
2166 EXPECT_EQ("// some text that\n"
2169 format("// some text that does\n"
2170 "// re flow more here",
2171 getLLVMStyleWithColumns(21)));
2174 TEST_F(FormatTestComments
, IgnoresIf0Contents
) {
2176 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
2180 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
2183 EXPECT_EQ("#if false\n"
2187 format("#if false\n"
2191 EXPECT_EQ("enum E {\n"
2207 EXPECT_EQ("enum F {\n"
2229 EXPECT_EQ("enum G {\n"
2247 EXPECT_EQ("enum H {\n"
2269 EXPECT_EQ("enum I {\n"
2271 "#if /* test */ 0 || 1\n"
2279 "#if /* test */ 0 || 1\n"
2285 EXPECT_EQ("enum J {\n"
2310 // Ignore stuff in SWIG-blocks.
2311 EXPECT_EQ("#ifdef SWIG\n"
2312 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
2315 format("#ifdef SWIG\n"
2316 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
2319 EXPECT_EQ("#ifndef SWIG\n"
2322 format("#ifndef SWIG\n"
2327 TEST_F(FormatTestComments
, DontCrashOnBlockComments
) {
2329 "int xxxxxxxxx; /* "
2330 "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\n"
2333 format("int xxxxxxxxx; /* "
2334 "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy zzzzzz\n"
2338 TEST_F(FormatTestComments
, BlockCommentsInControlLoops
) {
2339 verifyFormat("if (0) /* a comment in a strange place */ {\n"
2342 verifyFormat("if (0) /* a comment in a strange place */ {\n"
2344 "} /* another comment */ else /* comment #3 */ {\n"
2347 verifyFormat("while (0) /* a comment in a strange place */ {\n"
2350 verifyFormat("for (;;) /* a comment in a strange place */ {\n"
2353 verifyFormat("do /* a comment in a strange place */ {\n"
2355 "} /* another comment */ while (0);");
2358 TEST_F(FormatTestComments
, BlockComments
) {
2359 EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */",
2360 format("/* *//* */ /* */\n/* *//* */ /* */"));
2361 EXPECT_EQ("/* */ a /* */ b;", format(" /* */ a/* */ b;"));
2362 EXPECT_EQ("#define A /*123*/ \\\n"
2367 format("#define A /*123*/ b\n"
2369 "someCall(parameter);",
2370 getLLVMStyleWithColumns(15)));
2372 EXPECT_EQ("#define A\n"
2375 format("#define A\n"
2376 "/* */someCall(parameter);",
2377 getLLVMStyleWithColumns(15)));
2378 verifyNoChange("/*\n**\n*/");
2386 " * aaaaaa aaaaaa\n"
2388 getLLVMStyleWithColumns(10)));
2398 getLLVMStyleWithColumns(10)));
2399 EXPECT_EQ("int aaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
2401 " bbbbbbbbbbbb */\n"
2402 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
2403 format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
2405 " bbbbbbbbbbbb */ bbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
2406 getLLVMStyleWithColumns(50)));
2408 FormatStyle NoBinPacking
= getLLVMStyle();
2409 NoBinPacking
.BinPackParameters
= false;
2410 EXPECT_EQ("someFunction(1, /* comment 1 */\n"
2411 " 2, /* comment 2 */\n"
2412 " 3, /* comment 3 */\n"
2415 format("someFunction (1, /* comment 1 */\n"
2416 " 2, /* comment 2 */ \n"
2417 " 3, /* comment 3 */\n"
2421 "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2422 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
2424 "bool aaaaaaaaaaaaa = /* trailing comment */\n"
2425 " aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2426 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;",
2428 "bool aaaaaaaaaaaaa = /* trailing comment */\n"
2429 " aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2430 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;"));
2432 "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
2433 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
2434 "int cccccccccccccccccccccccccccccc; /* comment */",
2435 format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
2436 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
2437 "int cccccccccccccccccccccccccccccc; /* comment */"));
2439 verifyFormat("void f(int * /* unused */) {}");
2469 TEST_F(FormatTestComments
, BlockCommentsInMacros
) {
2470 EXPECT_EQ("#define A \\\n"
2472 " /* one line */ \\\n"
2474 format("#define A { \\\n"
2475 " /* one line */ \\\n"
2477 getLLVMStyleWithColumns(20)));
2478 EXPECT_EQ("#define A \\\n"
2480 " /* previous */ \\\n"
2481 " /* one line */ \\\n"
2483 format("#define A { \\\n"
2484 " /* previous */ \\\n"
2485 " /* one line */ \\\n"
2487 getLLVMStyleWithColumns(20)));
2490 TEST_F(FormatTestComments
, BlockCommentsAtEndOfLine
) {
2494 format("a = {1111 /* */\n"
2496 getLLVMStyleWithColumns(15)));
2500 format("a = {1111 /* */\n"
2502 getLLVMStyleWithColumns(15)));
2507 format("a = {1111 /* a */\n"
2509 getLLVMStyleWithColumns(15)));
2512 TEST_F(FormatTestComments
, BreaksAfterMultilineBlockCommentsInParamLists
) {
2513 EXPECT_EQ("a = f(/* long\n"
2516 format("a = f(/* long long */ a);", getLLVMStyleWithColumns(16)));
2517 EXPECT_EQ("a = f(\n"
2521 format("a = f(/* long long */ a);", getLLVMStyleWithColumns(15)));
2523 EXPECT_EQ("a = f(/* long\n"
2527 format("a = f(/* long\n"
2530 getLLVMStyleWithColumns(16)));
2532 EXPECT_EQ("a = f(/* long\n"
2536 format("a = f(/* long\n"
2539 getLLVMStyleWithColumns(16)));
2541 EXPECT_EQ("a = f(/* long\n"
2545 format("a = f(/* long\n"
2548 getLLVMStyleWithColumns(16)));
2555 format("a = f(a, /* long long */ b);", getLLVMStyleWithColumns(16)));
2563 format("a = f(a, /* long long */ b);", getLLVMStyleWithColumns(15)));
2565 EXPECT_EQ("a = f(a,\n"
2569 format("a = f(a, /* long long */ (1 + 1));",
2570 getLLVMStyleWithColumns(16)));
2571 EXPECT_EQ("a = f(\n"
2576 format("a = f(a, /* long long */ (1 + 1));",
2577 getLLVMStyleWithColumns(15)));
2580 TEST_F(FormatTestComments
, IndentLineCommentsInStartOfBlockAtEndOfFile
) {
2586 TEST_F(FormatTestComments
, AlignTrailingComments
) {
2587 EXPECT_EQ("#define MACRO(V) \\\n"
2588 " V(Rt2) /* one more char */ \\\n"
2589 " V(Rs) /* than here */ \\\n"
2590 "/* comment 3 */\n",
2591 format("#define MACRO(V)\\\n"
2592 "V(Rt2) /* one more char */ \\\n"
2593 "V(Rs) /* than here */ \\\n"
2594 "/* comment 3 */\n",
2595 getLLVMStyleWithColumns(40)));
2596 EXPECT_EQ("int i = f(abc, // line 1\n"
2600 format("int i = f(abc, // line 1\n"
2604 getLLVMStyleWithColumns(40)));
2606 // Align newly broken trailing comments.
2607 EXPECT_EQ("int ab; // line\n"
2610 format("int ab; // line\n"
2611 "int a; // long long",
2612 getLLVMStyleWithColumns(15)));
2613 EXPECT_EQ("int ab; // line\n"
2617 format("int ab; // line\n"
2618 "int a; // long long\n"
2620 getLLVMStyleWithColumns(15)));
2621 EXPECT_EQ("int ab; // line\n"
2625 format("int ab; // line\n"
2626 "int a; // long long\n"
2628 getLLVMStyleWithColumns(15)));
2629 EXPECT_EQ("int ab; // line\n"
2634 format("int ab; // line\n"
2635 "int a; // long long\n"
2638 getLLVMStyleWithColumns(15)));
2640 // Don't align newly broken trailing comments if that would put them over the
2642 EXPECT_EQ("int i, j; // line 1\n"
2643 "int k; // line longg\n"
2645 format("int i, j; // line 1\n"
2646 "int k; // line longg long",
2647 getLLVMStyleWithColumns(20)));
2649 // Always align if ColumnLimit = 0
2650 EXPECT_EQ("int i, j; // line 1\n"
2651 "int k; // line longg long",
2652 format("int i, j; // line 1\n"
2653 "int k; // line longg long",
2654 getLLVMStyleWithColumns(0)));
2656 // Align comment line sections aligned with the next token with the next
2658 EXPECT_EQ("class A {\n"
2659 "public: // public comment\n"
2660 " // comment about a\n"
2663 format("class A {\n"
2664 "public: // public comment\n"
2665 " // comment about a\n"
2668 getLLVMStyleWithColumns(40)));
2669 EXPECT_EQ("class A {\n"
2670 "public: // public comment 1\n"
2671 " // public comment 2\n"
2672 " // comment 1 about a\n"
2673 " // comment 2 about a\n"
2676 format("class A {\n"
2677 "public: // public comment 1\n"
2678 " // public comment 2\n"
2679 " // comment 1 about a\n"
2680 " // comment 2 about a\n"
2683 getLLVMStyleWithColumns(40)));
2684 EXPECT_EQ("int f(int n) { // comment line 1 on f\n"
2685 " // comment line 2 on f\n"
2686 " // comment line 1 before return\n"
2687 " // comment line 2 before return\n"
2688 " return n; // comment line 1 on return\n"
2689 " // comment line 2 on return\n"
2690 " // comment line 1 after return\n"
2692 format("int f(int n) { // comment line 1 on f\n"
2693 " // comment line 2 on f\n"
2694 " // comment line 1 before return\n"
2695 " // comment line 2 before return\n"
2696 " return n; // comment line 1 on return\n"
2697 " // comment line 2 on return\n"
2698 " // comment line 1 after return\n"
2700 getLLVMStyleWithColumns(40)));
2701 EXPECT_EQ("int f(int n) {\n"
2702 " switch (n) { // comment line 1 on switch\n"
2703 " // comment line 2 on switch\n"
2704 " // comment line 1 before case 1\n"
2705 " // comment line 2 before case 1\n"
2706 " case 1: // comment line 1 on case 1\n"
2707 " // comment line 2 on case 1\n"
2708 " // comment line 1 before return 1\n"
2709 " // comment line 2 before return 1\n"
2710 " return 1; // comment line 1 on return 1\n"
2711 " // comment line 2 on return 1\n"
2712 " // comment line 1 before default\n"
2713 " // comment line 2 before default\n"
2714 " default: // comment line 1 on default\n"
2715 " // comment line 2 on default\n"
2716 " // comment line 1 before return 2\n"
2717 " return 2 * f(n - 1); // comment line 1 on return 2\n"
2718 " // comment line 2 on return 2\n"
2719 " // comment line 1 after return\n"
2720 " // comment line 2 after return\n"
2723 format("int f(int n) {\n"
2724 " switch (n) { // comment line 1 on switch\n"
2725 " // comment line 2 on switch\n"
2726 " // comment line 1 before case 1\n"
2727 " // comment line 2 before case 1\n"
2728 " case 1: // comment line 1 on case 1\n"
2729 " // comment line 2 on case 1\n"
2730 " // comment line 1 before return 1\n"
2731 " // comment line 2 before return 1\n"
2732 " return 1; // comment line 1 on return 1\n"
2733 " // comment line 2 on return 1\n"
2734 " // comment line 1 before default\n"
2735 " // comment line 2 before default\n"
2736 " default: // comment line 1 on default\n"
2737 " // comment line 2 on default\n"
2738 " // comment line 1 before return 2\n"
2739 " return 2 * f(n - 1); // comment line 1 on return 2\n"
2740 " // comment line 2 on return 2\n"
2741 " // comment line 1 after return\n"
2742 " // comment line 2 after return\n"
2745 getLLVMStyleWithColumns(80)));
2747 // If all the lines in a sequence of line comments are aligned with the next
2748 // token, the first line belongs to the previous token and the other lines
2749 // belong to the next token.
2750 EXPECT_EQ("int a; // line about a\n"
2752 format("int a; // line about a\n"
2754 getLLVMStyleWithColumns(80)));
2755 EXPECT_EQ("int a; // line about a\n"
2758 format("int a; // line about a\n"
2759 " // line about b\n"
2761 getLLVMStyleWithColumns(80)));
2762 EXPECT_EQ("int a; // line about a\n"
2763 "// line 1 about b\n"
2764 "// line 2 about b\n"
2766 format("int a; // line about a\n"
2767 " // line 1 about b\n"
2768 " // line 2 about b\n"
2770 getLLVMStyleWithColumns(80)));
2772 // Checks an edge case in preprocessor handling.
2773 // These comments should *not* be aligned
2777 "long a; // Line about a\n"
2781 "long b_long_name; // Line about b\n"
2785 "long a; // Line about a\n" // Previous (bad) behavior
2789 "long b_long_name; // Line about b\n"
2791 getLLVMStyleWithColumns(80)));
2796 "#define FOO_GLOBAL 0 // Global scope.\n"
2797 "#define FOO_LINKLOCAL 1 // Link-local scope.\n"
2798 "#define FOO_SITELOCAL 2 // Site-local scope (deprecated).\n"
2799 "#define FOO_UNIQUELOCAL 3 // Unique local\n"
2800 "#define FOO_NODELOCAL 4 // Loopback\n\n"
2802 format("namespace m {\n\n"
2803 "#define FOO_GLOBAL 0 // Global scope.\n"
2804 "#define FOO_LINKLOCAL 1 // Link-local scope.\n"
2805 "#define FOO_SITELOCAL 2 // Site-local scope (deprecated).\n"
2806 "#define FOO_UNIQUELOCAL 3 // Unique local\n"
2807 "#define FOO_NODELOCAL 4 // Loopback\n\n"
2809 getLLVMStyleWithColumns(80)));
2811 // https://llvm.org/PR53441
2812 verifyFormat("/* */ //\n"
2814 verifyFormat("/**/ //\n"
2818 TEST_F(FormatTestComments
, AlignTrailingCommentsAcrossEmptyLines
) {
2819 FormatStyle Style
= getLLVMStyle();
2820 Style
.AlignTrailingComments
.Kind
= FormatStyle::TCAS_Always
;
2821 Style
.AlignTrailingComments
.OverEmptyLines
= 1;
2822 verifyFormat("#include \"a.h\" // simple\n"
2824 "#include \"aa.h\" // example case",
2827 verifyFormat("#include \"a.h\" // align across\n"
2829 "#include \"aa.h\" // two empty lines\n"
2831 "#include \"aaa.h\" // in a row",
2834 verifyFormat("#include \"a.h\" // align\n"
2835 "#include \"aa.h\" // comment\n"
2836 "#include \"aaa.h\" // blocks\n"
2838 "#include \"aaaa.h\" // across\n"
2839 "#include \"aaaaa.h\" // one\n"
2840 "#include \"aaaaaa.h\" // empty line",
2843 verifyFormat("#include \"a.h\" // align trailing comments\n"
2844 "#include \"a.h\"\n"
2845 "#include \"aa.h\" // across a line without comment",
2848 verifyFormat("#include \"a.h\" // align across\n"
2849 "#include \"a.h\"\n"
2850 "#include \"aa.h\" // two lines without comment\n"
2851 "#include \"a.h\"\n"
2852 "#include \"aaa.h\" // in a row",
2855 verifyFormat("#include \"a.h\" // align\n"
2856 "#include \"aa.h\" // comment\n"
2857 "#include \"aaa.h\" // blocks\n"
2858 "#include \"a.h\"\n"
2859 "#include \"aaaa.h\" // across\n"
2860 "#include \"aaaaa.h\" // a line without\n"
2861 "#include \"aaaaaa.h\" // comment",
2864 // Start of testing OverEmptyLines
2865 Style
.MaxEmptyLinesToKeep
= 3;
2866 Style
.AlignTrailingComments
.OverEmptyLines
= 2;
2867 // Cannot use verifyFormat here
2868 // test::messUp removes all new lines which changes the logic
2869 EXPECT_EQ("#include \"a.h\" // comment\n"
2873 "#include \"ab.h\" // comment\n"
2876 "#include \"abcdefg.h\" // comment",
2877 format("#include \"a.h\" // comment\n"
2881 "#include \"ab.h\" // comment\n"
2884 "#include \"abcdefg.h\" // comment",
2887 Style
.MaxEmptyLinesToKeep
= 1;
2888 Style
.AlignTrailingComments
.OverEmptyLines
= 1;
2889 // End of testing OverEmptyLines
2891 Style
.ColumnLimit
= 15;
2892 EXPECT_EQ("int ab; // line\n"
2897 format("int ab; // line\n"
2898 "int a; // long long\n"
2903 Style
.ColumnLimit
= 15;
2904 EXPECT_EQ("int ab; // line\n"
2908 format("int ab; // line\n"
2910 "int a; // long long",
2913 Style
.ColumnLimit
= 30;
2914 EXPECT_EQ("int foo = 12345; // comment\n"
2916 " 1234; // This is a very\n"
2917 " // long comment\n"
2918 " // which is wrapped\n"
2921 "int x = 2; // Is this still\n"
2923 format("int foo = 12345; // comment\n"
2924 "int bar = 1234; // This is a very long comment\n"
2925 " // which is wrapped arround.\n"
2927 "int x = 2; // Is this still aligned?",
2930 Style
.ColumnLimit
= 35;
2931 EXPECT_EQ("int foo = 12345; // comment\n"
2933 " 1234; // This is a very long\n"
2934 " // comment which is\n"
2935 " // wrapped arround.\n"
2938 " 2; // Is this still aligned?",
2939 format("int foo = 12345; // comment\n"
2940 "int bar = 1234; // This is a very long comment\n"
2941 " // which is wrapped arround.\n"
2943 "int x = 2; // Is this still aligned?",
2946 Style
.ColumnLimit
= 40;
2947 EXPECT_EQ("int foo = 12345; // comment\n"
2949 " 1234; // This is a very long comment\n"
2950 " // which is wrapped arround.\n"
2952 "int x = 2; // Is this still aligned?",
2953 format("int foo = 12345; // comment\n"
2954 "int bar = 1234; // This is a very long comment\n"
2955 " // which is wrapped arround.\n"
2957 "int x = 2; // Is this still aligned?",
2960 Style
.ColumnLimit
= 45;
2961 EXPECT_EQ("int foo = 12345; // comment\n"
2963 " 1234; // This is a very long comment\n"
2964 " // which is wrapped arround.\n"
2966 "int x = 2; // Is this still aligned?",
2967 format("int foo = 12345; // comment\n"
2968 "int bar = 1234; // This is a very long comment\n"
2969 " // which is wrapped arround.\n"
2971 "int x = 2; // Is this still aligned?",
2974 Style
.ColumnLimit
= 80;
2975 EXPECT_EQ("int a; // line about a\n"
2979 format("int a; // line about a\n"
2981 " // line about b\n"
2985 Style
.ColumnLimit
= 80;
2986 EXPECT_EQ("int a; // line about a\n"
2988 "// line 1 about b\n"
2989 "// line 2 about b\n"
2991 format("int a; // line about a\n"
2993 " // line 1 about b\n"
2994 " // line 2 about b\n"
2999 TEST_F(FormatTestComments
, AlignTrailingCommentsLeave
) {
3000 FormatStyle Style
= getLLVMStyle();
3001 Style
.AlignTrailingComments
.Kind
= FormatStyle::TCAS_Leave
;
3003 EXPECT_EQ("int a;// do not touch\n"
3004 "int b; // any comments\n"
3005 "int c; // comment\n"
3006 "int d; // comment",
3007 format("int a;// do not touch\n"
3008 "int b; // any comments\n"
3009 "int c; // comment\n"
3010 "int d; // comment",
3013 EXPECT_EQ("int a; // do not touch\n"
3014 "int b; // any comments\n"
3015 "int c; // comment\n"
3017 format("int a; // do not touch\n"
3018 "int b; // any comments\n"
3019 "int c; // comment\n"
3023 EXPECT_EQ("// do not touch\n"
3024 "int a; // any comments\n"
3030 format("// do not touch\n"
3031 "int a; // any comments\n"
3039 EXPECT_EQ("// do not touch\n"
3040 "int a; // any comments\n"
3046 format("// do not touch\n"
3047 "int a; // any comments\n"
3057 verifyFormat("namespace ns {\n"
3060 "} // namespace ns",
3067 Style
.AlignEscapedNewlines
= FormatStyle::ENAS_Left
;
3068 verifyNoChange("#define FOO \\\n"
3069 " /* foo(); */ \\\n"
3073 // Allow to keep 2 empty lines
3074 Style
.MaxEmptyLinesToKeep
= 2;
3075 EXPECT_EQ("// do not touch\n"
3076 "int a; // any comments\n"
3083 format("// do not touch\n"
3084 "int a; // any comments\n"
3092 Style
.MaxEmptyLinesToKeep
= 1;
3094 // Just format comments normally when leaving exceeds the column limit
3095 Style
.ColumnLimit
= 35;
3096 EXPECT_EQ("int foo = 12345; // comment\n"
3098 " 1234; // This is a very long\n"
3099 " // comment which is\n"
3100 " // wrapped arround.",
3101 format("int foo = 12345; // comment\n"
3102 "int bar = 1234; // This is a very long comment\n"
3103 " // which is wrapped arround.",
3107 TEST_F(FormatTestComments
, DontAlignNamespaceComments
) {
3108 FormatStyle Style
= getLLVMStyle();
3109 Style
.NamespaceIndentation
= FormatStyle::NI_All
;
3110 Style
.NamespaceMacros
.push_back("TESTSUITE");
3111 Style
.ShortNamespaceLines
= 0;
3113 StringRef Input
= "namespace A {\n"
3116 " namespace D { //\n"
3117 " } // namespace D\n"
3118 " std::string Foo = Bar; // Comment\n"
3119 " std::string BazString = Baz; // C2\n"
3120 " } // namespace C\n"
3124 EXPECT_TRUE(Style
.FixNamespaceComments
);
3125 EXPECT_EQ(Style
.AlignTrailingComments
.Kind
, FormatStyle::TCAS_Always
);
3126 verifyFormat("namespace A {\n"
3129 " namespace D { //\n"
3130 " } // namespace D\n"
3131 " std::string Foo = Bar; // Comment\n"
3132 " std::string BazString = Baz; // C2\n"
3133 " } // namespace C\n"
3134 " } // TESTSUITE(B)\n"
3138 Style
.AlignTrailingComments
.Kind
= FormatStyle::TCAS_Never
;
3139 verifyFormat("namespace A {\n"
3142 " namespace D { //\n"
3143 " } // namespace D\n"
3144 " std::string Foo = Bar; // Comment\n"
3145 " std::string BazString = Baz; // C2\n"
3146 " } // namespace C\n"
3147 " } // TESTSUITE(B)\n"
3151 Style
.AlignTrailingComments
.Kind
= FormatStyle::TCAS_Leave
;
3152 verifyFormat("namespace A {\n"
3155 " namespace D { //\n"
3156 " } // namespace D\n"
3157 " std::string Foo = Bar; // Comment\n"
3158 " std::string BazString = Baz; // C2\n"
3159 " } // namespace C\n"
3160 " } // TESTSUITE(B)\n"
3164 Style
.FixNamespaceComments
= false;
3165 Style
.AlignTrailingComments
.Kind
= FormatStyle::TCAS_Always
;
3166 verifyFormat("namespace A {\n"
3169 " namespace D { //\n"
3170 " } // namespace D\n"
3171 " std::string Foo = Bar; // Comment\n"
3172 " std::string BazString = Baz; // C2\n"
3173 " } // namespace C\n"
3178 Style
.AlignTrailingComments
.Kind
= FormatStyle::TCAS_Never
;
3179 verifyFormat("namespace A {\n"
3182 " namespace D { //\n"
3183 " } // namespace D\n"
3184 " std::string Foo = Bar; // Comment\n"
3185 " std::string BazString = Baz; // C2\n"
3186 " } // namespace C\n"
3191 Style
.AlignTrailingComments
.Kind
= FormatStyle::TCAS_Leave
;
3192 verifyFormat("namespace A {\n"
3195 " namespace D { //\n"
3196 " } // namespace D\n"
3197 " std::string Foo = Bar; // Comment\n"
3198 " std::string BazString = Baz; // C2\n"
3199 " } // namespace C\n"
3204 Style
.AlignTrailingComments
.Kind
= FormatStyle::TCAS_Always
;
3205 Style
.FixNamespaceComments
= true;
3206 Input
= "namespace A {\n"
3212 verifyFormat("namespace A {\n"
3215 "} // namespace A\n"
3219 Style
.FixNamespaceComments
= false;
3220 verifyFormat(Input
, Style
);
3223 TEST_F(FormatTestComments
, DontAlignOverScope
) {
3224 verifyFormat("if (foo) {\n"
3225 " int aLongVariable; // with comment\n"
3226 " int f; // aligned\n"
3227 "} // not aligned\n"
3228 "int bar; // new align\n"
3229 "int foobar; // group");
3231 verifyFormat("if (foo) {\n"
3234 " int aLongVariable; // with comment\n"
3235 " int f; // aligned\n"
3236 "} // not aligned\n"
3237 "int bar; // new align\n"
3238 "int foobar; // group");
3240 verifyFormat("if (foo) {\n"
3242 "} else if (foo) {\n"
3243 " int aLongVariable; // with comment\n"
3244 " int f; // aligned\n"
3245 "} // not aligned\n"
3246 "int bar; // new align\n"
3247 "int foobar; // group");
3249 verifyFormat("while (foo) {\n"
3250 " int aLongVariable; // with comment\n"
3251 " int f; // aligned\n"
3252 "} // not aligned\n"
3253 "int bar; // new align\n"
3254 "int foobar; // group");
3256 verifyFormat("for (;;) {\n"
3257 " int aLongVariable; // with comment\n"
3258 " int f; // aligned\n"
3259 "} // not aligned\n"
3260 "int bar; // new align\n"
3261 "int foobar; // group");
3263 verifyFormat("do {\n"
3264 " int aLongVariable; // with comment\n"
3265 " int f; // aligned\n"
3266 "} while (foo); // not aligned\n"
3267 "int bar; // new align\n"
3268 "int foobar; // group");
3271 " int aLongVariable; // with comment\n"
3272 "while (foo); // not aigned\n"
3273 "int bar; // new align\n"
3274 "int foobar; // group");
3277 " int aLongVariable; // with comment\n"
3278 "/**/ while (foo); // not aigned\n"
3279 "int bar; // new align\n"
3280 "int foobar; // group");
3282 verifyFormat("switch (foo) {\n"
3284 " int aLongVariable; // with comment\n"
3285 " int f; // aligned\n"
3286 "} // case not aligned\n"
3287 "} // switch also not aligned\n"
3288 "int bar; // new align\n"
3289 "int foobar; // group");
3291 verifyFormat("switch (foo) {\n"
3293 " int aLongVariable; // with comment\n"
3294 " int f; // aligned\n"
3295 "} // case not aligned\n"
3296 "} // switch also not aligned\n"
3297 "int bar; // new align\n"
3298 "int foobar; // group");
3300 verifyFormat("class C {\n"
3301 " int aLongVariable; // with comment\n"
3302 " int f; // aligned\n"
3303 "}; // not aligned\n"
3304 "int bar; // new align\n"
3305 "int foobar; // group");
3307 verifyFormat("struct S {\n"
3308 " int aLongVariable; // with comment\n"
3309 " int f; // aligned\n"
3310 "}; // not aligned\n"
3311 "int bar; // new align\n"
3312 "int foobar; // group");
3314 verifyFormat("union U {\n"
3315 " int aLongVariable; // with comment\n"
3316 " int f; // aligned\n"
3317 "}; // not aligned\n"
3318 "int bar; // new align\n"
3319 "int foobar; // group");
3321 verifyFormat("enum E {\n"
3322 " aLongVariable, // with comment\n"
3324 "}; // not aligned\n"
3325 "int bar; // new align\n"
3326 "int foobar; // group");
3328 verifyFormat("void foo() {\n"
3330 " int aLongVariable; // with comment\n"
3331 " int f; // aligned\n"
3332 " } // not aligned\n"
3333 " int bar; // new align\n"
3334 " int foobar; // group\n"
3337 verifyFormat("auto longLambda = [] { // comment\n"
3338 " int aLongVariable; // with comment\n"
3339 " int f; // aligned\n"
3340 "}; // not aligned\n"
3341 "int bar; // new align\n"
3342 "int foobar; // group\n"
3343 "auto shortLambda = [] { return 5; }; // aligned");
3345 verifyFormat("auto longLambdaResult = [] { // comment\n"
3346 " int aLongVariable; // with comment\n"
3347 " int f; // aligned\n"
3348 "}(); // not aligned\n"
3349 "int bar; // new align\n"
3350 "int foobar; // group\n"
3351 "auto shortLambda = [] { return 5; }(); // aligned");
3354 "auto longLambdaResult = [](auto I, auto J) { // comment\n"
3355 " int aLongVariable; // with comment\n"
3356 " int f; // aligned\n"
3357 "}(\"Input\", 5); // not aligned\n"
3358 "int bar; // new align\n"
3359 "int foobar; // group\n"
3360 "auto shortL = [](auto I, auto J) { return 5; }(\"In\", 5); // aligned");
3362 verifyFormat("enum E1 { V1, V2 }; // Aligned\n"
3363 "enum E2 { LongerNames, InThis, Enum }; // Comments");
3365 verifyFormat("class C {\n"
3366 " int aLongVariable; // with comment\n"
3367 " int f; // aligned\n"
3368 "} /* middle comment */; // not aligned\n"
3369 "int bar; // new align\n"
3370 "int foobar; // group");
3373 TEST_F(FormatTestComments
, AlignsBlockCommentDecorations
) {
3387 // Align a single line.
3425 // Align the end '*/' after a line.
3446 EXPECT_EQ("/* line 1\n"
3448 format("/* line 1\n"
3450 EXPECT_EQ("/* line 1\n"
3452 format("/* line 1\n"
3454 EXPECT_EQ("/* line 1\n"
3456 format("/* line 1\n"
3458 EXPECT_EQ("/* line 1\n"
3460 format("/* line 1\n"
3462 EXPECT_EQ("/* line 1\n"
3464 format("/* line 1\n"
3466 EXPECT_EQ("int i; /* line 1\n"
3468 format("int i; /* line 1\n"
3470 EXPECT_EQ("int i; /* line 1\n"
3472 format("int i; /* line 1\n"
3474 EXPECT_EQ("int i; /* line 1\n"
3476 format("int i; /* line 1\n"
3479 // Align several lines.
3480 EXPECT_EQ("/* line 1\n"
3483 format("/* line 1\n"
3486 EXPECT_EQ("/* line 1\n"
3489 format("/* line 1\n"
3501 // Align with different indent after the decorations.
3515 // Align empty or blank lines.
3527 // Align while breaking and reflowing.
3529 " * long long long\n"
3534 " * long long long long\n"
3538 getLLVMStyleWithColumns(20)));
3541 TEST_F(FormatTestComments
, NoCrash_Bug34236
) {
3542 // This is a test case from a crasher reported in:
3543 // https://bugs.llvm.org/show_bug.cgi?id=34236
3544 // Temporarily disable formatting for readability.
3554 getLLVMStyleWithColumns(80)));
3558 TEST_F(FormatTestComments
, NonTrailingBlockComments
) {
3559 verifyFormat("const /** comment comment */ A = B;",
3560 getLLVMStyleWithColumns(40));
3562 verifyFormat("const /** comment comment comment */ A =\n"
3564 getLLVMStyleWithColumns(40));
3566 EXPECT_EQ("const /** comment comment comment\n"
3569 format("const /** comment comment comment comment */\n"
3571 getLLVMStyleWithColumns(40)));
3574 TEST_F(FormatTestComments
, PythonStyleComments
) {
3575 // Keeps a space after '#'.
3576 EXPECT_EQ("# comment\n"
3580 getTextProtoStyleWithColumns(20)));
3581 EXPECT_EQ("# comment\n"
3583 format("# comment\n"
3585 getTextProtoStyleWithColumns(20)));
3586 // Breaks long comment.
3587 EXPECT_EQ("# comment comment\n"
3590 format("# comment comment comment\n"
3592 getTextProtoStyleWithColumns(20)));
3593 // Indents comments.
3594 EXPECT_EQ("data {\n"
3595 " # comment comment\n"
3600 "# comment comment comment\n"
3602 getTextProtoStyleWithColumns(20)));
3603 EXPECT_EQ("data {\n"
3604 " # comment comment\n"
3608 format("data {# comment comment comment\n"
3610 getTextProtoStyleWithColumns(20)));
3611 // Reflows long comments.
3612 EXPECT_EQ("# comment comment\n"
3613 "# comment comment\n"
3615 format("# comment comment comment\n"
3618 getTextProtoStyleWithColumns(20)));
3619 // Breaks trailing comments.
3620 EXPECT_EQ("k: val # comment\n"
3623 format("k:val#comment comment\n"
3625 getTextProtoStyleWithColumns(20)));
3627 " k: val # comment\n"
3632 format("id {k:val#comment comment\n"
3635 getTextProtoStyleWithColumns(20)));
3636 // Aligns trailing comments.
3637 EXPECT_EQ("k: val # commen1\n"
3644 format("k:val#commen1 commen2\n"
3647 "a:1#commen5 commen6\n"
3649 getTextProtoStyleWithColumns(20)));
3652 TEST_F(FormatTestComments
, BreaksBeforeTrailingUnbreakableSequence
) {
3653 // The end of /* trail */ is exactly at 80 columns, but the unbreakable
3654 // trailing sequence ); after it exceeds the column limit. Make sure we
3655 // correctly break the line in that case.
3656 verifyFormat("int a =\n"
3657 " foo(/* trail */);",
3658 getLLVMStyleWithColumns(23));
3661 TEST_F(FormatTestComments
, ReflowBackslashCrash
) {
3666 "// rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr\n"
3667 "// \\ <log_file> -- --output_directory=\"<output_directory>\"",
3671 "// rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr \\\n"
3672 "// <log_file> -- --output_directory=\"<output_directory>\""));
3676 TEST_F(FormatTestComments
, IndentsLongJavadocAnnotatedLines
) {
3677 FormatStyle Style
= getGoogleStyle(FormatStyle::LK_Java
);
3678 Style
.ColumnLimit
= 60;
3679 FormatStyle Style20
= getGoogleStyle(FormatStyle::LK_Java
);
3680 Style20
.ColumnLimit
= 20;
3683 " * @param x long long long long long long long long long\n"
3687 " * @param x long long long long long long long long long long\n"
3691 " * @param x long long long long long long long long long\n"
3692 " * long long long long long long long long long long\n"
3695 " * @param x long long long long long long long long long "
3696 "long long long long long long long long long long\n"
3700 " * @param x long long long long long long long long long\n"
3701 " * long long long long long long long long long long\n"
3705 " * @param x long long long long long long long long long "
3706 "long long long long long long long long long long long\n"
3710 " * Sentence that\n"
3711 " * should be broken.\n"
3713 " * keep indentation\n"
3716 " * Sentence that should be broken.\n"
3718 " * keep indentation\n"
3723 " * @param l1 long1\n"
3725 " * @param l2 long2\n"
3729 " * @param l1 long1 to break\n"
3730 " * @param l2 long2 to break\n"
3740 " * @param xx to break\n"
3751 " * @param xx to break\n"
3756 FormatStyle JSStyle20
= getGoogleStyle(FormatStyle::LK_JavaScript
);
3757 JSStyle20
.ColumnLimit
= 20;
3759 " * @param l1 long1\n"
3763 " * @param l1 long1 to break\n"
3767 " * @param {l1 long1\n"
3771 " * @param {l1 long1 to break}\n"
3776 TEST_F(FormatTestComments
, SpaceAtLineCommentBegin
) {
3777 FormatStyle Style
= getLLVMStyle();
3778 StringRef NoTextInComment
= " // \n"
3789 format(NoTextInComment
, Style
));
3791 Style
.SpacesInLineCommentPrefix
.Minimum
= 0;
3792 verifyFormat("//#comment", Style
);
3798 format(NoTextInComment
, Style
));
3800 Style
.SpacesInLineCommentPrefix
.Minimum
= 5;
3801 EXPECT_EQ("// #comment", format("//#comment", Style
));
3807 format(NoTextInComment
, Style
));
3809 Style
= getLLVMStyle();
3811 "//Free comment without space\n"
3813 "// Free comment with 3 spaces\n"
3815 "///Free Doxygen without space\n"
3817 "/// Free Doxygen with 3 spaces\n"
3819 "//🐉 A nice dragon\n"
3825 "// 🐉 Another nice dragon\n"
3827 "// \t Three leading spaces following tab\n"
3829 "// \\t Three leading spaces following backslash\n"
3831 "/// A Doxygen Comment with a nested list:\n"
3836 "/// of the inner list\n"
3841 "bool bar(bool b) {\n"
3842 " bool ret1 = true; ///<Doxygenstyle without space\n"
3843 " bool ret2 = true; ///< Doxygenstyle with 3 spaces\n"
3847 " // In function comment\n"
3851 "// if (ret1) {\n" // Commented out at the beginning of the line
3855 " //if (ret1) {\n" // Commtented out at the beginning of the content
3856 " // return ret2;\n"
3859 " return ret1 && ret2;\n"
3865 "} // namespace Bar\n"
3866 "//@Nothing added because of the non ascii char\n"
3868 "//@ Nothing removed because of the non ascii char\n"
3870 "// Comment to move to the left\n"
3874 "//Comment to move to the right\n"
3877 "//} will not move\n"
3879 "//vv will only move\n"
3880 "//} if the line above does";
3882 EXPECT_EQ("// Free comment without space\n"
3884 "// Free comment with 3 spaces\n"
3886 "/// Free Doxygen without space\n"
3888 "/// Free Doxygen with 3 spaces\n"
3890 "// 🐉 A nice dragon\n"
3896 "// 🐉 Another nice dragon\n"
3898 "// \t Three leading spaces following tab\n"
3900 "// \\t Three leading spaces following backslash\n"
3902 "/// A Doxygen Comment with a nested list:\n"
3907 "/// of the inner list\n"
3912 "bool bar(bool b) {\n"
3913 " bool ret1 = true; ///< Doxygenstyle without space\n"
3914 " bool ret2 = true; ///< Doxygenstyle with 3 spaces\n"
3918 " // In function comment\n"
3923 " // return ret2;\n"
3927 " // return ret2;\n"
3930 " return ret1 && ret2;\n"
3932 "} // namespace Foo\n"
3936 "} // namespace Bar\n"
3937 "//@Nothing added because of the non ascii char\n"
3939 "//@ Nothing removed because of the non ascii char\n"
3941 "// Comment to move to the left\n"
3942 "// But not this?\n"
3945 "// Comment to move to the right\n"
3948 "//} will not move\n"
3950 "// vv will only move\n"
3951 "// } if the line above does",
3952 format(Code
, Style
));
3954 Style
.SpacesInLineCommentPrefix
= {0, 0};
3955 EXPECT_EQ("//#comment", format("// #comment", Style
));
3956 EXPECT_EQ("//Free comment without space\n"
3958 "//Free comment with 3 spaces\n"
3960 "///Free Doxygen without space\n"
3962 "///Free Doxygen with 3 spaces\n"
3964 "//🐉 A nice dragon\n"
3970 "//🐉 Another nice dragon\n"
3972 "//\t Three leading spaces following tab\n"
3974 "//\\t Three leading spaces following backslash\n"
3976 "///A Doxygen Comment with a nested list:\n"
3979 "/// - Baz\n" // Here we keep the relative indentation
3981 "/// of the inner list\n"
3986 "bool bar(bool b) {\n"
3987 " bool ret1 = true; ///<Doxygenstyle without space\n"
3988 " bool ret2 = true; ///<Doxygenstyle with 3 spaces\n"
3992 " //In function comment\n"
3997 " // return ret2;\n"
4001 " // return ret2;\n"
4004 " return ret1 && ret2;\n"
4006 "} //namespace Foo\n"
4010 "} //namespace Bar\n"
4011 "//@Nothing added because of the non ascii char\n"
4013 "//@ Nothing removed because of the non ascii char\n"
4015 "//Comment to move to the left\n"
4019 "//Comment to move to the right\n"
4022 "//} will not move\n"
4024 "//vv will only move\n"
4025 "//} if the line above does",
4026 format(Code
, Style
));
4028 Style
.SpacesInLineCommentPrefix
= {2, -1u};
4029 EXPECT_EQ("// Free comment without space\n"
4031 "// Free comment with 3 spaces\n"
4033 "/// Free Doxygen without space\n"
4035 "/// Free Doxygen with 3 spaces\n"
4037 "// 🐉 A nice dragon\n"
4043 "// 🐉 Another nice dragon\n"
4045 "// \t Three leading spaces following tab\n"
4047 "// \\t Three leading spaces following backslash\n"
4049 "/// A Doxygen Comment with a nested list:\n"
4054 "/// of the inner list\n"
4059 "bool bar(bool b) {\n"
4060 " bool ret1 = true; ///< Doxygenstyle without space\n"
4061 " bool ret2 = true; ///< Doxygenstyle with 3 spaces\n"
4065 " // In function comment\n"
4070 " // return ret2;\n"
4074 " // return ret2;\n"
4077 " return ret1 && ret2;\n"
4079 "} // namespace Foo\n"
4083 "} // namespace Bar\n"
4084 "//@Nothing added because of the non ascii char\n"
4086 "//@ Nothing removed because of the non ascii char\n"
4088 "// Comment to move to the left\n"
4089 "// But not this?\n"
4092 "// Comment to move to the right\n"
4095 "//} will not move\n"
4097 "// vv will only move\n"
4098 "// } if the line above does",
4099 format(Code
, Style
));
4101 Style
= getLLVMStyleWithColumns(20);
4102 StringRef WrapCode
= "//Lorem ipsum dolor sit amet\n"
4104 "// Lorem ipsum dolor sit amet\n"
4106 "void f() {//Hello World\n"
4109 EXPECT_EQ("// Lorem ipsum dolor\n"
4113 "// dolor sit amet\n"
4115 "void f() { // Hello\n"
4118 format(WrapCode
, Style
));
4120 Style
.SpacesInLineCommentPrefix
= {0, 0};
4121 EXPECT_EQ("//Lorem ipsum dolor\n"
4125 "//dolor sit amet\n"
4127 "void f() { //Hello\n"
4130 format(WrapCode
, Style
));
4132 Style
.SpacesInLineCommentPrefix
= {1, 1};
4133 EXPECT_EQ("// Lorem ipsum dolor\n"
4137 "// dolor sit amet\n"
4139 "void f() { // Hello\n"
4142 format(WrapCode
, Style
));
4149 "// loooooooooooooooooooooooooooooong\n"
4150 "// commentcomments\n"
4151 "// normal comments",
4152 format("// loooooooooooooooooooooooooooooong commentcomments\n"
4153 "// normal comments",
4156 Style
.SpacesInLineCommentPrefix
= {3, 3};
4157 EXPECT_EQ("// Lorem ipsum\n"
4158 "// dolor sit amet\n"
4164 "void f() { // Hello\n"
4167 format(WrapCode
, Style
));
4169 Style
= getLLVMStyleWithColumns(20);
4170 StringRef LotsOfSpaces
= "// This are more spaces "
4171 "than the ColumnLimit, what now?\n"
4175 "// This is a text to split in multiple "
4176 "lines, please. Thank you very much!\n"
4178 "// A comment with\n"
4179 "// some indentation that has to be split.\n"
4180 "// And now without";
4181 EXPECT_EQ("// This are more spaces "
4182 "than the ColumnLimit, what now?\n"
4186 "// This is a text to\n"
4187 "// split in multiple\n"
4188 "// lines, please.\n"
4189 "// Thank you very\n"
4192 "// A comment with\n"
4195 "// that has to be\n"
4197 "// And now without",
4198 format(LotsOfSpaces
, Style
));
4200 Style
.SpacesInLineCommentPrefix
= {0, 0};
4201 EXPECT_EQ("//This are more\n"
4202 "//spaces than the\n"
4203 "//ColumnLimit, what\n"
4208 "//This is a text to\n"
4209 "//split in multiple\n"
4210 "//lines, please.\n"
4211 "//Thank you very\n"
4214 "//A comment with\n"
4215 "// some indentation\n"
4216 "// that has to be\n"
4218 "//And now without",
4219 format(LotsOfSpaces
, Style
));
4221 Style
.SpacesInLineCommentPrefix
= {3, 3};
4222 EXPECT_EQ("// This are more\n"
4223 "// spaces than the\n"
4229 "// This is a text\n"
4231 "// multiple lines,\n"
4232 "// please. Thank\n"
4233 "// you very much!\n"
4235 "// A comment with\n"
4240 "// And now without",
4241 format(LotsOfSpaces
, Style
));
4243 Style
.SpacesInLineCommentPrefix
= {30, -1u};
4244 EXPECT_EQ("// This are more spaces than the "
4245 "ColumnLimit, what now?\n"
4249 "// This is a text to split in "
4250 "multiple lines, please. Thank you very much!\n"
4252 "// A comment with\n"
4253 "// some indentation that has to be "
4255 "// And now without",
4256 format(LotsOfSpaces
, Style
));
4258 Style
.SpacesInLineCommentPrefix
= {2, 4};
4259 EXPECT_EQ("// A Comment to be\n"
4263 "// A Comment to be\n"
4267 "// A Comment to be\n"
4271 "// A Comment to be\n"
4286 format("//A Comment to be moved\n"
4289 "// A Comment to be moved\n"
4292 "// A Comment to be moved\n"
4295 "// A Comment to be moved\n"
4298 "// A Comment to be moved\n"
4301 "// A Comment to be moved\n"
4304 "// A Comment to be moved\n"
4308 Style
.ColumnLimit
= 30;
4309 EXPECT_EQ("int i; // A Comment to be\n"
4313 "int i; // A Comment to be\n"
4317 "int i; // A Comment to be\n"
4321 "int i; // A Comment to be\n"
4325 "int i; // A Comment to be\n"
4329 "int i; // A Comment to be\n"
4333 "int i; // A Comment to be\n"
4336 format("int i;//A Comment to be moved\n"
4339 "int i;// A Comment to be moved\n"
4342 "int i;// A Comment to be moved\n"
4345 "int i;// A Comment to be moved\n"
4348 "int i;// A Comment to be moved\n"
4351 "int i;// A Comment to be moved\n"
4354 "int i;// A Comment to be moved\n"
4358 Style
= getLLVMStyleWithColumns(0);
4359 EXPECT_EQ("// Free comment without space\n"
4361 "// Free comment with 3 spaces\n"
4363 "/// Free Doxygen without space\n"
4365 "/// Free Doxygen with 3 spaces\n"
4367 "// 🐉 A nice dragon\n"
4373 "// 🐉 Another nice dragon\n"
4375 "// \t Three leading spaces following tab\n"
4377 "// \\t Three leading spaces following backslash\n"
4379 "/// A Doxygen Comment with a nested list:\n"
4384 "/// of the inner list\n"
4389 "bool bar(bool b) {\n"
4390 " bool ret1 = true; ///< Doxygenstyle without space\n"
4391 " bool ret2 = true; ///< Doxygenstyle with 3 spaces\n"
4395 " // In function comment\n"
4400 " // return ret2;\n"
4404 " // return ret2;\n"
4407 " return ret1 && ret2;\n"
4409 "} // namespace Foo\n"
4413 "} // namespace Bar\n"
4414 "//@Nothing added because of the non ascii char\n"
4416 "//@ Nothing removed because of the non ascii char\n"
4418 "// Comment to move to the left\n"
4419 "// But not this?\n"
4422 "// Comment to move to the right\n"
4425 "//} will not move\n"
4427 "// vv will only move\n"
4428 "// } if the line above does",
4429 format(Code
, Style
));
4431 Style
.SpacesInLineCommentPrefix
= {0, 0};
4432 EXPECT_EQ("//Free comment without space\n"
4434 "//Free comment with 3 spaces\n"
4436 "///Free Doxygen without space\n"
4438 "///Free Doxygen with 3 spaces\n"
4440 "//🐉 A nice dragon\n"
4446 "//🐉 Another nice dragon\n"
4448 "//\t Three leading spaces following tab\n"
4450 "//\\t Three leading spaces following backslash\n"
4452 "///A Doxygen Comment with a nested list:\n"
4455 "/// - Baz\n" // Here we keep the relative indentation
4457 "/// of the inner list\n"
4462 "bool bar(bool b) {\n"
4463 " bool ret1 = true; ///<Doxygenstyle without space\n"
4464 " bool ret2 = true; ///<Doxygenstyle with 3 spaces\n"
4468 " //In function comment\n"
4473 " // return ret2;\n"
4477 " // return ret2;\n"
4480 " return ret1 && ret2;\n"
4482 "} //namespace Foo\n"
4486 "} //namespace Bar\n"
4487 "//@Nothing added because of the non ascii char\n"
4489 "//@ Nothing removed because of the non ascii char\n"
4491 "//Comment to move to the left\n"
4495 "//Comment to move to the right\n"
4498 "//} will not move\n"
4500 "//vv will only move\n"
4501 "//} if the line above does",
4502 format(Code
, Style
));
4504 Style
.SpacesInLineCommentPrefix
= {2, -1u};
4505 EXPECT_EQ("// Free comment without space\n"
4507 "// Free comment with 3 spaces\n"
4509 "/// Free Doxygen without space\n"
4511 "/// Free Doxygen with 3 spaces\n"
4513 "// 🐉 A nice dragon\n"
4519 "// 🐉 Another nice dragon\n"
4521 "// \t Three leading spaces following tab\n"
4523 "// \\t Three leading spaces following backslash\n"
4525 "/// A Doxygen Comment with a nested list:\n"
4530 "/// of the inner list\n"
4535 "bool bar(bool b) {\n"
4536 " bool ret1 = true; ///< Doxygenstyle without space\n"
4537 " bool ret2 = true; ///< Doxygenstyle with 3 spaces\n"
4541 " // In function comment\n"
4546 " // return ret2;\n"
4550 " // return ret2;\n"
4553 " return ret1 && ret2;\n"
4555 "} // namespace Foo\n"
4559 "} // namespace Bar\n"
4560 "//@Nothing added because of the non ascii char\n"
4562 "//@ Nothing removed because of the non ascii char\n"
4564 "// Comment to move to the left\n"
4565 "// But not this?\n"
4568 "// Comment to move to the right\n"
4571 "//} will not move\n"
4573 "// vv will only move\n"
4574 "// } if the line above does",
4575 format(Code
, Style
));
4578 TEST_F(FormatTestComments
, SplitCommentIntroducers
) {
4587 getLLVMStyleWithColumns(10)));
4592 } // end namespace format
4593 } // end namespace clang