1 //===- unittest/Format/FormatTestComments.cpp - Formatting unit tests -----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "clang/Format/Format.h"
12 #include "../Tooling/ReplacementTest.h"
13 #include "FormatTestUtils.h"
15 #include "clang/Frontend/TextDiagnosticPrinter.h"
16 #include "llvm/Support/Debug.h"
17 #include "llvm/Support/MemoryBuffer.h"
18 #include "gtest/gtest.h"
20 #define DEBUG_TYPE "format-test"
22 using clang::tooling::ReplacementTest;
28 FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); }
30 class FormatTestComments : public ::testing::Test {
38 std::string format(llvm::StringRef Code,
39 const FormatStyle &Style = getLLVMStyle(),
40 StatusCheck CheckComplete = SC_ExpectComplete) {
41 LLVM_DEBUG(llvm::errs() << "---\n");
42 LLVM_DEBUG(llvm::errs() << Code << "\n\n");
43 std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
44 FormattingAttemptStatus Status;
45 tooling::Replacements Replaces =
46 reformat(Style, Code, Ranges, "<stdin>", &Status);
47 if (CheckComplete != SC_DoNotCheck) {
48 bool ExpectedCompleteFormat = CheckComplete == SC_ExpectComplete;
49 EXPECT_EQ(ExpectedCompleteFormat, Status.FormatComplete)
52 ReplacementCount = Replaces.size();
53 auto Result = applyAllReplacements(Code, Replaces);
54 EXPECT_TRUE(static_cast<bool>(Result));
55 LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
59 FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
60 FormatStyle Style = getLLVMStyle();
61 Style.ColumnLimit = ColumnLimit;
65 FormatStyle getTextProtoStyleWithColumns(unsigned ColumnLimit) {
66 FormatStyle Style = getGoogleStyle(FormatStyle::FormatStyle::LK_TextProto);
67 Style.ColumnLimit = ColumnLimit;
71 void verifyFormat(llvm::StringRef Code,
72 const FormatStyle &Style = getLLVMStyle()) {
73 EXPECT_EQ(Code.str(), format(Code, Style)) << "Expected code is not stable";
74 EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
77 void verifyGoogleFormat(llvm::StringRef Code) {
78 verifyFormat(Code, getGoogleStyle());
81 /// \brief Verify that clang-format does not crash on the given input.
82 void verifyNoCrash(llvm::StringRef Code,
83 const FormatStyle &Style = getLLVMStyle()) {
84 format(Code, Style, SC_DoNotCheck);
90 //===----------------------------------------------------------------------===//
91 // Tests for comments.
92 //===----------------------------------------------------------------------===//
94 TEST_F(FormatTestComments, UnderstandsSingleLineComments) {
95 verifyFormat("//* */");
96 verifyFormat("// line 1\n"
100 verifyFormat("void f() {\n"
101 " // Doesn't do anything\n"
103 verifyFormat("SomeObject\n"
104 " // Calling someFunction on SomeObject\n"
105 " .someFunction();");
106 verifyFormat("auto result = SomeObject\n"
107 " // Calling someFunction on SomeObject\n"
108 " .someFunction();");
109 verifyFormat("void f(int i, // some comment (probably for i)\n"
110 " int j, // some comment (probably for j)\n"
111 " int k); // some comment (probably for k)");
112 verifyFormat("void f(int i,\n"
113 " // some comment (probably for j)\n"
115 " // some comment (probably for k)\n"
118 verifyFormat("int i // This is a fancy variable\n"
119 " = 5; // with nicely aligned comment.");
121 verifyFormat("// Leading comment.\n"
122 "int a; // Trailing comment.");
123 verifyFormat("int a; // Trailing comment\n"
127 verifyFormat("int a; // Trailing comment\n"
129 "// Leading comment.\n"
131 verifyFormat("int a; // Comment.\n"
132 " // More details.\n"
133 "int bbbb; // Another comment.");
135 "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
136 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // comment\n"
137 "int cccccccccccccccccccccccccccccc; // comment\n"
138 "int ddd; // looooooooooooooooooooooooong comment\n"
139 "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
140 "int bbbbbbbbbbbbbbbbbbbbb; // comment\n"
141 "int ccccccccccccccccccc; // comment");
143 verifyFormat("#include \"a\" // comment\n"
144 "#include \"a/b/c\" // comment");
145 verifyFormat("#include <a> // comment\n"
146 "#include <a/b/c> // comment");
147 EXPECT_EQ("#include \"a\" // comment\n"
148 "#include \"a/b/c\" // comment",
149 format("#include \\\n"
150 " \"a\" // comment\n"
151 "#include \"a/b/c\" // comment"));
153 verifyFormat("enum E {\n"
155 " VAL_A, // comment\n"
159 EXPECT_EQ("enum A {\n"
175 getLLVMStyleWithColumns(20)));
176 EXPECT_EQ("enum A {\n"
184 getLLVMStyleWithColumns(20)));
185 EXPECT_EQ("enum A {\n"
193 getLLVMStyleWithColumns(20)));
194 EXPECT_EQ("enum A {\n"
204 getLLVMStyleWithColumns(20)));
205 EXPECT_EQ("enum A {\n"
215 getLLVMStyleWithColumns(20)));
217 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
218 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
219 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
220 " // Comment inside a statement.\n"
221 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
222 verifyFormat("SomeFunction(a,\n"
225 verifyFormat("SomeFunction(a, a,\n"
229 "bool aaaaaaaaaaaaa = // comment\n"
230 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
231 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
233 verifyFormat("int aaaa; // aaaaa\n"
234 "int aa; // aaaaaaa",
235 getLLVMStyleWithColumns(20));
237 EXPECT_EQ("void f() { // This does something ..\n"
239 "int a; // This is unrelated",
240 format("void f() { // This does something ..\n"
242 "int a; // This is unrelated"));
243 EXPECT_EQ("class C {\n"
244 " void f() { // This does something ..\n"
247 " int a; // This is unrelated\n"
249 format("class C{void f() { // This does something ..\n"
252 "int a; // This is unrelated\n"
255 EXPECT_EQ("int i; // single line trailing comment",
256 format("int i;\\\n// single line trailing comment"));
258 verifyGoogleFormat("int a; // Trailing comment.");
260 verifyFormat("someFunction(anotherFunction( // Force break.\n"
263 verifyGoogleFormat("#endif // HEADER_GUARD");
265 verifyFormat("const char *test[] = {\n"
271 "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
272 " aaaaaaaaaaaaaaaaaaaaaa); // 81_cols_with_this_comment");
282 EXPECT_EQ("lineWith(); // comment\n"
285 format("lineWith(); // comment\n"
288 EXPECT_EQ("lineWith(); // comment\n"
292 format("lineWith(); // comment\n"
296 EXPECT_EQ("lineWith(); // comment\n"
299 format("lineWith(); // comment\n"
303 EXPECT_EQ("lineWith(); // comment\n"
305 "otherLine(); // comment",
306 format("lineWith(); // comment\n"
308 "otherLine(); // comment"));
309 EXPECT_EQ("lineWith();\n"
311 "otherLine(); // comment",
312 format("lineWith();\n"
314 "otherLine(); // comment"));
315 EXPECT_EQ("// first\n"
317 "otherLine(); // comment",
320 "otherLine(); // comment"));
324 "otherLine(); // comment",
328 "otherLine(); // comment"));
329 verifyFormat("f(); // comment\n"
333 EXPECT_EQ("f(); // comment\n"
337 format("f(); // comment\n"
341 EXPECT_EQ("f(); // comment\n"
345 format("f(); // comment\n"
349 EXPECT_EQ("void f() {\n"
350 " lineWith(); // comment\n"
353 format("void f() {\n"
354 " lineWith(); // comment\n"
357 EXPECT_EQ("int xy; // a\n"
359 format("int xy; // a\n"
361 EXPECT_EQ("int xy; // a\n"
363 format("int xy; // a\n"
365 getLLVMStyleWithColumns(12)));
367 verifyFormat("#define A \\\n"
368 " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n"
369 " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
370 getLLVMStyleWithColumns(60));
373 " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n"
374 " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
375 getLLVMStyleWithColumns(61));
377 verifyFormat("if ( // This is some comment\n"
380 EXPECT_EQ("if ( // This is some comment\n"
381 " // spanning two lines\n"
384 format("if( // This is some comment\n"
385 " // spanning two lines\n"
389 verifyNoCrash("/\\\n/");
390 verifyNoCrash("/\\\n* */");
391 // The 0-character somehow makes the lexer return a proper comment.
392 verifyNoCrash(StringRef("/*\\\0\n/", 6));
395 TEST_F(FormatTestComments, KeepsParameterWithTrailingCommentsOnTheirOwnLine) {
396 EXPECT_EQ("SomeFunction(a,\n"
399 format("SomeFunction(a,\n"
402 EXPECT_EQ("SomeFunction(a, b,\n"
405 format("SomeFunction(a,\n"
409 EXPECT_EQ("SomeFunction(a, b, // comment (unclear relation)\n"
411 format("SomeFunction(a, b, // comment (unclear relation)\n"
413 EXPECT_EQ("SomeFunction(a, // comment\n"
416 format("SomeFunction(a, // comment\n"
419 EXPECT_EQ("aaaaaaaaaa(aaaa(aaaa,\n"
422 format("aaaaaaaaaa(aaaa(aaaa,\n"
427 TEST_F(FormatTestComments, RemovesTrailingWhitespaceOfComments) {
428 EXPECT_EQ("// comment", format("// comment "));
429 EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment",
430 format("int aaaaaaa, bbbbbbb; // comment ",
431 getLLVMStyleWithColumns(33)));
432 EXPECT_EQ("// comment\\\n", format("// comment\\\n \t \v \f "));
433 EXPECT_EQ("// comment \\\n", format("// comment \\\n \t \v \f "));
436 TEST_F(FormatTestComments, UnderstandsBlockComments) {
437 verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);");
438 verifyFormat("void f() { g(/*aaa=*/x, /*bbb=*/!y, /*c=*/::c); }");
439 EXPECT_EQ("f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n"
440 " bbbbbbbbbbbbbbbbbbbbbbbbb);",
441 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \\\n"
442 "/* Trailing comment for aa... */\n"
443 " bbbbbbbbbbbbbbbbbbbbbbbbb);"));
445 "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
446 " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);",
447 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n"
448 "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);"));
450 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
451 " aaaaaaaaaaaaaaaaaa,\n"
452 " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
454 format("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
455 " aaaaaaaaaaaaaaaaaa ,\n"
456 " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
458 verifyFormat("f(/* aaaaaaaaaaaaaaaaaa = */\n"
459 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
461 FormatStyle NoBinPacking = getLLVMStyle();
462 NoBinPacking.BinPackParameters = false;
463 verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n"
464 " /* parameter 2 */ aaaaaa,\n"
465 " /* parameter 3 */ aaaaaa,\n"
466 " /* parameter 4 */ aaaaaa);",
469 // Aligning block comments in macros.
470 verifyGoogleFormat("#define A \\\n"
475 TEST_F(FormatTestComments, AlignsBlockComments) {
477 " * Really multi-line\n"
482 " * Really multi-line\n"
486 EXPECT_EQ("class C {\n"
488 " * Another multi-line\n"
495 " * Another multi-line\n"
501 " 1. This is a comment with non-trivial formatting.\n"
502 " 1.1. We have to indent/outdent all lines equally\n"
503 " 1.1.1. to keep the formatting.\n"
506 " 1. This is a comment with non-trivial formatting.\n"
507 " 1.1. We have to indent/outdent all lines equally\n"
508 " 1.1.1. to keep the formatting.\n"
511 "Don't try to outdent if there's not enough indentation.\n"
514 " Don't try to outdent if there's not enough indentation.\n"
517 EXPECT_EQ("int i; /* Comment with empty...\n"
520 format("int i; /* Comment with empty...\n"
523 EXPECT_EQ("int foobar = 0; /* comment */\n"
524 "int bar = 0; /* multiline\n"
526 "int baz = 0; /* multiline\n"
528 "int bzz = 0; /* multiline\n"
530 format("int foobar = 0; /* comment */\n"
531 "int bar = 0; /* multiline\n"
533 "int baz = 0; /* multiline\n"
535 "int bzz = 0; /* multiline\n"
537 EXPECT_EQ("int foobar = 0; /* comment */\n"
538 "int bar = 0; /* multiline\n"
540 "int baz = 0; /* multiline\n"
542 format("int foobar = 0; /* comment */\n"
543 "int bar = 0; /* multiline\n"
545 "int baz = 0; /* multiline\n"
549 TEST_F(FormatTestComments, CommentReflowingCanBeTurnedOff) {
550 FormatStyle Style = getLLVMStyleWithColumns(20);
551 Style.ReflowComments = false;
552 verifyFormat("// aaaaaaaaa aaaaaaaaaa aaaaaaaaaa", Style);
553 verifyFormat("/* aaaaaaaaa aaaaaaaaaa aaaaaaaaaa */", Style);
556 TEST_F(FormatTestComments, CorrectlyHandlesLengthOfBlockComments) {
557 EXPECT_EQ("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
558 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */",
559 format("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
560 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */"));
562 "void ffffffffffff(\n"
563 " int aaaaaaaa, int bbbbbbbb,\n"
564 " int cccccccccccc) { /*\n"
571 format("void ffffffffffff(int aaaaaaaa, int bbbbbbbb, int cccccccccccc)\n"
573 " aaaaaaaaaa aaaaaaaaaaaaa\n"
574 " bbbbbbbbbbbbbb bbbbbbbbbb\n"
577 getLLVMStyleWithColumns(40)));
580 TEST_F(FormatTestComments, DontBreakNonTrailingBlockComments) {
581 EXPECT_EQ("void ffffffffff(\n"
582 " int aaaaa /* test */);",
583 format("void ffffffffff(int aaaaa /* test */);",
584 getLLVMStyleWithColumns(35)));
587 TEST_F(FormatTestComments, SplitsLongCxxComments) {
588 EXPECT_EQ("// A comment that\n"
589 "// doesn't fit on\n"
591 format("// A comment that doesn't fit on one line",
592 getLLVMStyleWithColumns(20)));
593 EXPECT_EQ("/// A comment that\n"
594 "/// doesn't fit on\n"
596 format("/// A comment that doesn't fit on one line",
597 getLLVMStyleWithColumns(20)));
598 EXPECT_EQ("//! A comment that\n"
599 "//! doesn't fit on\n"
601 format("//! A comment that doesn't fit on one line",
602 getLLVMStyleWithColumns(20)));
603 EXPECT_EQ("// a b c d\n"
606 format("// a b c d e f g h i j k", getLLVMStyleWithColumns(10)));
611 format("\\\n// a b c d e f g h i j k", getLLVMStyleWithColumns(10)));
612 EXPECT_EQ("if (true) // A comment that\n"
613 " // doesn't fit on\n"
615 format("if (true) // A comment that doesn't fit on one line ",
616 getLLVMStyleWithColumns(30)));
617 EXPECT_EQ("// Don't_touch_leading_whitespace",
618 format("// Don't_touch_leading_whitespace",
619 getLLVMStyleWithColumns(20)));
620 EXPECT_EQ("// Add leading\n"
622 format("//Add leading whitespace", getLLVMStyleWithColumns(20)));
623 EXPECT_EQ("/// Add leading\n"
625 format("///Add leading whitespace", getLLVMStyleWithColumns(20)));
626 EXPECT_EQ("//! Add leading\n"
628 format("//!Add leading whitespace", getLLVMStyleWithColumns(20)));
629 EXPECT_EQ("// whitespace", format("//whitespace", getLLVMStyle()));
630 EXPECT_EQ("// Even if it makes the line exceed the column\n"
632 format("//Even if it makes the line exceed the column limit",
633 getLLVMStyleWithColumns(51)));
634 EXPECT_EQ("//--But not here", format("//--But not here", getLLVMStyle()));
635 EXPECT_EQ("/// line 1\n"
636 "// add leading whitespace",
637 format("/// line 1\n"
638 "//add leading whitespace",
639 getLLVMStyleWithColumns(30)));
640 EXPECT_EQ("/// line 1\n"
653 "//line 7", getLLVMStyleWithColumns(20)));
655 EXPECT_EQ("// aa bb cc dd",
656 format("// aa bb cc dd ",
657 getLLVMStyleWithColumns(15)));
659 EXPECT_EQ("// A comment before\n"
663 format("// A comment before a macro definition\n"
665 getLLVMStyleWithColumns(20)));
666 EXPECT_EQ("void ffffff(\n"
667 " int aaaaaaaaa, // wwww\n"
668 " int bbbbbbbbbb, // xxxxxxx\n"
670 " int c, int d, int e) {}",
671 format("void ffffff(\n"
672 " int aaaaaaaaa, // wwww\n"
673 " int bbbbbbbbbb, // xxxxxxx yyyyyyyyyy\n"
674 " int c, int d, int e) {}",
675 getLLVMStyleWithColumns(40)));
676 EXPECT_EQ("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
677 format("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
678 getLLVMStyleWithColumns(20)));
680 "#define XXX // a b c d\n"
682 format("#define XXX // a b c d e f g h", getLLVMStyleWithColumns(22)));
684 "#define XXX // q w e r\n"
686 format("#define XXX //q w e r t y u i", getLLVMStyleWithColumns(22)));
690 " // long 1 2 3 4 5\n"
695 " // long 1 2 3 4 5\n"
697 getLLVMStyleWithColumns(20)));
701 " // long 1 2 3 4 5\n"
707 " // long 1 2 3 4 5 6\n"
709 getLLVMStyleWithColumns(20)));
712 TEST_F(FormatTestComments, PreservesHangingIndentInCxxComments) {
713 EXPECT_EQ("// A comment\n"
717 format("// A comment that doesn't fit on one line",
718 getLLVMStyleWithColumns(20)));
719 EXPECT_EQ("/// A comment\n"
723 format("/// A comment that doesn't fit on one line",
724 getLLVMStyleWithColumns(20)));
727 TEST_F(FormatTestComments, DontSplitLineCommentsWithEscapedNewlines) {
728 EXPECT_EQ("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
729 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
730 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
731 format("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
732 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
733 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
734 EXPECT_EQ("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
735 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
736 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
737 format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
738 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
739 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
740 getLLVMStyleWithColumns(50)));
741 // FIXME: One day we might want to implement adjustment of leading whitespace
742 // of the consecutive lines in this kind of comment:
744 " a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
745 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
746 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
747 format("double a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
748 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
749 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
750 getLLVMStyleWithColumns(49)));
753 TEST_F(FormatTestComments, DontSplitLineCommentsWithPragmas) {
754 FormatStyle Pragmas = getLLVMStyleWithColumns(30);
755 Pragmas.CommentPragmas = "^ IWYU pragma:";
757 "// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb",
758 format("// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", Pragmas));
760 "/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */",
761 format("/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", Pragmas));
764 TEST_F(FormatTestComments, PriorityOfCommentBreaking) {
765 EXPECT_EQ("if (xxx ==\n"
766 " yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
769 format("if (xxx == yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
771 getLLVMStyleWithColumns(40)));
772 EXPECT_EQ("if (xxxxxxxxxx ==\n"
773 " yyy && // aaaaaa bbbbbbbb cccc\n"
776 format("if (xxxxxxxxxx == yyy && // aaaaaa bbbbbbbb cccc\n"
778 getLLVMStyleWithColumns(40)));
779 EXPECT_EQ("if (xxxxxxxxxx &&\n"
780 " yyy || // aaaaaa bbbbbbbb cccc\n"
783 format("if (xxxxxxxxxx && yyy || // aaaaaa bbbbbbbb cccc\n"
785 getLLVMStyleWithColumns(40)));
786 EXPECT_EQ("fffffffff(\n"
787 " &xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
789 format("fffffffff(&xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
791 getLLVMStyleWithColumns(40)));
794 TEST_F(FormatTestComments, MultiLineCommentsInDefines) {
795 EXPECT_EQ("#define A(x) /* \\\n"
799 format("#define A(x) /* \\\n"
803 getLLVMStyleWithColumns(17)));
804 EXPECT_EQ("#define A( \\\n"
809 format("#define A( \\\n"
814 getLLVMStyleWithColumns(17)));
817 TEST_F(FormatTestComments, ParsesCommentsAdjacentToPPDirectives) {
818 EXPECT_EQ("namespace {}\n// Test\n#define A",
819 format("namespace {}\n // Test\n#define A"));
820 EXPECT_EQ("namespace {}\n/* Test */\n#define A",
821 format("namespace {}\n /* Test */\n#define A"));
822 EXPECT_EQ("namespace {}\n/* Test */ #define A",
823 format("namespace {}\n /* Test */ #define A"));
826 TEST_F(FormatTestComments, KeepsLevelOfCommentBeforePPDirective) {
827 // Keep the current level if the comment was originally not aligned with
828 // the preprocessor directive.
829 EXPECT_EQ("void f() {\n"
835 format("void f() {\n"
842 EXPECT_EQ("void f() {\n"
849 format("void f() {\n"
857 EXPECT_EQ("int f(int i) {\n"
866 format("int f(int i) {\n"
876 EXPECT_EQ("int f(int i) {\n"
880 " // comment in else\n"
886 format("int f(int i) {\n"
890 " // comment in else\n"
897 EXPECT_EQ("int f(int i) {\n"
901 " /* comment in else */\n"
907 format("int f(int i) {\n"
911 " /* comment in else */\n"
918 // Keep the current level if there is an empty line between the comment and
919 // the preprocessor directive.
920 EXPECT_EQ("void f() {\n"
927 format("void f() {\n"
935 EXPECT_EQ("void f() {\n"
944 format("void f() {\n"
954 EXPECT_EQ("int f(int i) {\n"
964 format("int f(int i) {\n"
975 EXPECT_EQ("int f(int i) {\n"
979 " // comment in else\n"
986 format("int f(int i) {\n"
990 "// comment in else\n"
998 EXPECT_EQ("int f(int i) {\n"
1002 " /* comment in else */\n"
1009 format("int f(int i) {\n"
1013 "/* comment in else */\n"
1021 // Align with the preprocessor directive if the comment was originally aligned
1022 // with the preprocessor directive and there is no newline between the comment
1023 // and the preprocessor directive.
1024 EXPECT_EQ("void f() {\n"
1030 format("void f() {\n"
1037 EXPECT_EQ("int f(int i) {\n"
1046 format("int f(int i) {\n"
1056 EXPECT_EQ("int f(int i) {\n"
1060 "// comment in else\n"
1066 format("int f(int i) {\n"
1070 " // comment in else\n"
1077 EXPECT_EQ("int f(int i) {\n"
1081 "/* comment in else */\n"
1087 format("int f(int i) {\n"
1091 " /* comment in else */\n"
1099 TEST_F(FormatTestComments, SplitsLongLinesInComments) {
1100 // FIXME: Do we need to fix up the " */" at the end?
1101 // It doesn't look like any of our current logic triggers this.
1102 EXPECT_EQ("/* This is a long\n"
1104 " * doesn't fit on\n"
1110 "fit on one line. */",
1111 getLLVMStyleWithColumns(20)));
1117 format("/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10)));
1123 format("\\\n/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10)));
1126 "comment that doesn't\n"
1127 "fit on one line.\n"
1131 "comment that doesn't "
1132 "fit on one line. \n"
1134 getLLVMStyleWithColumns(20)));
1136 " * This is a long\n"
1138 " * doesn't fit on\n"
1142 " * This is a long "
1147 getLLVMStyleWithColumns(20)));
1149 " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n"
1150 " * so_it_should_be_broken\n"
1151 " * wherever_a_space_occurs\n"
1154 " * This_is_a_comment_with_words_that_dont_fit_on_one_line "
1155 " so_it_should_be_broken "
1156 " wherever_a_space_occurs \n"
1158 getLLVMStyleWithColumns(20)));
1160 " * This_comment_can_not_be_broken_into_lines\n"
1163 " * This_comment_can_not_be_broken_into_lines\n"
1165 getLLVMStyleWithColumns(20)));
1168 " This is another\n"
1169 " long comment that\n"
1170 " doesn't fit on one\n"
1171 " line 1234567890\n"
1177 " long comment that "
1178 " doesn't fit on one"
1179 " line 1234567890\n"
1182 getLLVMStyleWithColumns(20)));
1186 " * another comment\n"
1187 " * t hat doesn' t\n"
1188 " * fit on one l i\n"
1201 getLLVMStyleWithColumns(20)));
1203 " * This is a long\n"
1205 " * doesn't fit on\n"
1209 " * This is a long comment that doesn't fit on one line\n"
1211 getLLVMStyleWithColumns(20)));
1213 " if (something) /* This is a\n"
1219 " if (something) /* This is a long comment */\n"
1222 getLLVMStyleWithColumns(30)));
1224 EXPECT_EQ("/* A comment before\n"
1226 " * definition */\n"
1228 format("/* A comment before a macro definition */\n"
1230 getLLVMStyleWithColumns(20)));
1232 EXPECT_EQ("/* some comment\n"
1233 " * a comment that\n"
1234 " * we break another\n"
1235 " * comment we have\n"
1236 " * to break a left\n"
1239 format(" /* some comment\n"
1240 " * a comment that we break\n"
1241 " * another comment we have to break\n"
1242 "* a left comment\n"
1244 getLLVMStyleWithColumns(20)));
1247 " * multiline block\n"
1252 " * multiline block comment\n"
1255 getLLVMStyleWithColumns(20)));
1257 // This reproduces a crashing bug where both adaptStartOfLine and
1258 // getCommentSplit were trying to wrap after the "/**".
1259 EXPECT_EQ("/** multilineblockcommentwithnowrapopportunity */",
1260 format("/** multilineblockcommentwithnowrapopportunity */",
1261 getLLVMStyleWithColumns(20)));
1272 EXPECT_EQ("/* a a */",
1273 format("/* a a */", getLLVMStyleWithColumns(15)));
1274 EXPECT_EQ("/* a a bc */",
1275 format("/* a a bc */", getLLVMStyleWithColumns(15)));
1276 EXPECT_EQ("/* aaa aaa\n"
1278 format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15)));
1279 EXPECT_EQ("/* aaa aaa\n"
1281 format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15)));
1284 TEST_F(FormatTestComments, SplitsLongLinesInCommentsInPreprocessor) {
1285 EXPECT_EQ("#define X \\\n"
1288 " Macro comment \\\n"
1293 format("#define X \\\n"
1296 " Macro comment with a long line\n"
1299 getLLVMStyleWithColumns(20)));
1300 EXPECT_EQ("#define X \\\n"
1301 " /* Macro comment \\\n"
1305 format("#define X \\\n"
1306 " /* Macro comment with a long\n"
1309 getLLVMStyleWithColumns(20)));
1310 EXPECT_EQ("#define X \\\n"
1311 " /* Macro comment \\\n"
1312 " * with a long \\\n"
1315 format("#define X \\\n"
1316 " /* Macro comment with a long line */ \\\n"
1318 getLLVMStyleWithColumns(20)));
1321 TEST_F(FormatTestComments, KeepsTrailingPPCommentsAndSectionCommentsSeparate) {
1322 verifyFormat("#ifdef A // line about A\n"
1323 "// section comment\n"
1325 getLLVMStyleWithColumns(80));
1326 verifyFormat("#ifdef A // line 1 about A\n"
1327 " // line 2 about A\n"
1328 "// section comment\n"
1330 getLLVMStyleWithColumns(80));
1331 EXPECT_EQ("#ifdef A // line 1 about A\n"
1332 " // line 2 about A\n"
1333 "// section comment\n"
1335 format("#ifdef A // line 1 about A\n"
1336 " // line 2 about A\n"
1337 "// section comment\n"
1339 getLLVMStyleWithColumns(80)));
1340 verifyFormat("int f() {\n"
1342 "#ifdef A // comment about A\n"
1343 " // section comment 1\n"
1344 " // section comment 2\n"
1346 "#else // comment about #else\n"
1347 " // section comment 3\n"
1350 "}", getLLVMStyleWithColumns(80));
1353 TEST_F(FormatTestComments, AlignsPPElseEndifComments) {
1354 verifyFormat("#if A\n"
1358 getLLVMStyleWithColumns(20));
1359 verifyFormat("#if A\n"
1363 getLLVMStyleWithColumns(20));
1374 getLLVMStyleWithColumns(20)));
1377 TEST_F(FormatTestComments, CommentsInStaticInitializers) {
1379 "static SomeType type = {aaaaaaaaaaaaaaaaaaaa, /* comment */\n"
1380 " aaaaaaaaaaaaaaaaaaaa /* comment */,\n"
1381 " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n"
1382 " aaaaaaaaaaaaaaaaaaaa, // comment\n"
1383 " aaaaaaaaaaaaaaaaaaaa};",
1384 format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n"
1385 " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n"
1386 " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n"
1387 " aaaaaaaaaaaaaaaaaaaa , // comment\n"
1388 " aaaaaaaaaaaaaaaaaaaa };"));
1389 verifyFormat("static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
1390 " bbbbbbbbbbb, ccccccccccc};");
1391 verifyFormat("static SomeType type = {aaaaaaaaaaa,\n"
1392 " // comment for bb....\n"
1393 " bbbbbbbbbbb, ccccccccccc};");
1395 "static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
1396 " bbbbbbbbbbb, ccccccccccc};");
1397 verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n"
1398 " // comment for bb....\n"
1399 " bbbbbbbbbbb, ccccccccccc};");
1401 verifyFormat("S s = {{a, b, c}, // Group #1\n"
1402 " {d, e, f}, // Group #2\n"
1403 " {g, h, i}}; // Group #3");
1404 verifyFormat("S s = {{// Group #1\n"
1411 EXPECT_EQ("S s = {\n"
1412 " // Some comment\n"
1415 " // Comment after empty line\n"
1418 " // Some comment\n"
1421 " // Comment after empty line\n"
1424 EXPECT_EQ("S s = {\n"
1425 " /* Some comment */\n"
1428 " /* Comment after empty line */\n"
1431 " /* Some comment */\n"
1434 " /* Comment after empty line */\n"
1437 verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n"
1438 " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
1439 " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
1440 " 0x00, 0x00, 0x00, 0x00}; // comment\n");
1443 TEST_F(FormatTestComments, LineCommentsAfterRightBrace) {
1444 EXPECT_EQ("if (true) { // comment about branch\n"
1445 " // comment about f\n"
1448 format("if (true) { // comment about branch\n"
1449 " // comment about f\n"
1452 getLLVMStyleWithColumns(80)));
1453 EXPECT_EQ("if (1) { // if line 1\n"
1459 "} else { // else line 1\n"
1465 format("if (1) { // if line 1\n"
1471 "} else { // else line 1\n"
1477 EXPECT_EQ("do { // line 1\n"
1482 format("do { // line 1\n"
1487 getLLVMStyleWithColumns(80)));
1488 EXPECT_EQ("while (a < b) { // line 1\n"
1493 format("while (a < b) {// line 1\n"
1498 getLLVMStyleWithColumns(80)));
1501 TEST_F(FormatTestComments, ReflowsComments) {
1502 // Break a long line and reflow with the full next line.
1503 EXPECT_EQ("// long long long\n"
1505 format("// long long long long\n"
1507 getLLVMStyleWithColumns(20)));
1509 // Keep the trailing newline while reflowing.
1510 EXPECT_EQ("// long long long\n"
1512 format("// long long long long\n"
1514 getLLVMStyleWithColumns(20)));
1516 // Break a long line and reflow with a part of the next line.
1517 EXPECT_EQ("// long long long\n"
1520 format("// long long long long\n"
1521 "// long long_long",
1522 getLLVMStyleWithColumns(20)));
1524 // Break but do not reflow if the first word from the next line is too long.
1525 EXPECT_EQ("// long long long\n"
1527 "// long_long_long\n",
1528 format("// long long long long\n"
1529 "// long_long_long\n",
1530 getLLVMStyleWithColumns(20)));
1532 // Don't break or reflow short lines.
1533 verifyFormat("// long\n"
1534 "// long long long lo\n"
1535 "// long long long lo\n"
1537 getLLVMStyleWithColumns(20));
1539 // Keep prefixes and decorations while reflowing.
1540 EXPECT_EQ("/// long long long\n"
1542 format("/// long long long long\n"
1544 getLLVMStyleWithColumns(20)));
1545 EXPECT_EQ("//! long long long\n"
1547 format("//! long long long long\n"
1549 getLLVMStyleWithColumns(20)));
1550 EXPECT_EQ("/* long long long\n"
1552 format("/* long long long long\n"
1554 getLLVMStyleWithColumns(20)));
1555 EXPECT_EQ("///< long long long\n"
1557 format("///< long long long long\n"
1559 getLLVMStyleWithColumns(20)));
1560 EXPECT_EQ("//!< long long long\n"
1562 format("//!< long long long long\n"
1564 getLLVMStyleWithColumns(20)));
1566 // Don't bring leading whitespace up while reflowing.
1567 EXPECT_EQ("/* long long long\n"
1568 " * long long long\n"
1570 format("/* long long long long\n"
1573 getLLVMStyleWithColumns(20)));
1575 // Reflow the last line of a block comment with its trailing '*/'.
1576 EXPECT_EQ("/* long long long\n"
1578 format("/* long long long long\n"
1580 getLLVMStyleWithColumns(20)));
1582 // Reflow two short lines; keep the postfix of the last one.
1583 EXPECT_EQ("/* long long long\n"
1584 " * long long long */",
1585 format("/* long long long long\n"
1588 getLLVMStyleWithColumns(20)));
1590 // Put the postfix of the last short reflow line on a newline if it doesn't
1592 EXPECT_EQ("/* long long long\n"
1593 " * long long longg\n"
1595 format("/* long long long long\n"
1598 getLLVMStyleWithColumns(20)));
1600 // Reflow lines with leading whitespace.
1603 " * long long long\n"
1604 " * long long long\n"
1605 " * long long long\n"
1610 " * long long long long\n"
1612 " * long long long long\n"
1615 getLLVMStyleWithColumns(20)));
1617 // Break single line block comments that are first in the line with ' *'
1619 EXPECT_EQ("/* long long long\n"
1621 format("/* long long long long */", getLLVMStyleWithColumns(20)));
1623 // Break single line block comment that are not first in the line with ' '
1625 EXPECT_EQ("int i; /* long long\n"
1627 format("int i; /* long long long */", getLLVMStyleWithColumns(20)));
1629 // Reflow a line that goes just over the column limit.
1630 EXPECT_EQ("// long long long\n"
1632 format("// long long long lon\n"
1634 getLLVMStyleWithColumns(20)));
1636 // Stop reflowing if the next line has a different indentation than the
1638 EXPECT_EQ("// long long long\n"
1642 format("// long long long long\n"
1645 getLLVMStyleWithColumns(20)));
1647 // Reflow into the last part of a really long line that has been broken into
1649 EXPECT_EQ("// long long long\n"
1650 "// long long long\n"
1651 "// long long long\n",
1652 format("// long long long long long long long long\n"
1654 getLLVMStyleWithColumns(20)));
1656 // Break the first line, then reflow the beginning of the second and third
1658 EXPECT_EQ("// long long long\n"
1659 "// lon1 lon2 lon2\n"
1660 "// lon2 lon3 lon3",
1661 format("// long long long lon1\n"
1662 "// lon2 lon2 lon2\n"
1664 getLLVMStyleWithColumns(20)));
1666 // Reflow the beginning of the second line, then break the rest.
1667 EXPECT_EQ("// long long long\n"
1668 "// lon1 lon2 lon2\n"
1669 "// lon2 lon2 lon2\n"
1671 format("// long long long lon1\n"
1672 "// lon2 lon2 lon2 lon2 lon2 lon3",
1673 getLLVMStyleWithColumns(20)));
1675 // Shrink the first line, then reflow the second line up.
1676 EXPECT_EQ("// long long long", format("// long long\n"
1678 getLLVMStyleWithColumns(20)));
1680 // Don't shrink leading whitespace.
1681 EXPECT_EQ("int i; /// a",
1682 format("int i; /// a", getLLVMStyleWithColumns(20)));
1684 // Shrink trailing whitespace if there is no postfix and reflow.
1685 EXPECT_EQ("// long long long\n"
1687 format("// long long long long \n"
1689 getLLVMStyleWithColumns(20)));
1691 // Shrink trailing whitespace to a single one if there is postfix.
1692 EXPECT_EQ("/* long long long */",
1693 format("/* long long long */", getLLVMStyleWithColumns(20)));
1695 // Break a block comment postfix if exceeding the line limit.
1696 EXPECT_EQ("/* long\n"
1698 format("/* long */", getLLVMStyleWithColumns(20)));
1700 // Reflow indented comments.
1702 " // long long long\n"
1704 " int i; /* long lon\n"
1709 " // long long long long\n"
1711 " int i; /* long lon g\n"
1714 getLLVMStyleWithColumns(20)));
1716 // Don't realign trailing comments after reflow has happened.
1717 EXPECT_EQ("// long long long\n"
1720 format("// long long long long\n"
1723 getLLVMStyleWithColumns(20)));
1724 EXPECT_EQ("// long long long\n"
1725 "// longng long long\n"
1727 format("// long long long longng\n"
1728 "// long long long\n"
1730 getLLVMStyleWithColumns(20)));
1732 // Reflow lines after a broken line.
1733 EXPECT_EQ("int a; // Trailing\n"
1737 format("int a; // Trailing comment\n"
1741 getLLVMStyleWithColumns(20)));
1742 EXPECT_EQ("/// This long line\n"
1743 "/// gets reflown.\n",
1744 format("/// This long line gets\n"
1746 getLLVMStyleWithColumns(20)));
1747 EXPECT_EQ("//! This long line\n"
1748 "//! gets reflown.\n",
1749 format(" //! This long line gets\n"
1751 getLLVMStyleWithColumns(20)));
1752 EXPECT_EQ("/* This long line\n"
1753 " * gets reflown.\n"
1755 format("/* This long line gets\n"
1758 getLLVMStyleWithColumns(20)));
1760 // Reflow after indentation makes a line too long.
1762 " // long long long\n"
1766 "// long long long lo\n"
1769 getLLVMStyleWithColumns(20)));
1771 // Break and reflow multiple lines.
1773 " * Reflow the end of\n"
1774 " * line by 11 22 33\n"
1778 " * Reflow the end of line\n"
1785 getLLVMStyleWithColumns(20)));
1786 EXPECT_EQ("/// First line gets\n"
1787 "/// broken. Second\n"
1790 "/// broken. Third\n"
1791 "/// gets reflown.\n",
1792 format("/// First line gets broken.\n"
1793 "/// Second line gets reflown and broken.\n"
1794 "/// Third gets reflown.\n",
1795 getLLVMStyleWithColumns(20)));
1796 EXPECT_EQ("int i; // first long\n"
1799 format("int i; // first long long\n"
1801 getLLVMStyleWithColumns(20)));
1803 " // first long line\n"
1805 " // long line line\n"
1806 " // third long line\n"
1810 " // first long line line\n"
1811 " // second long line line\n"
1812 " // third long line line\n"
1814 getLLVMStyleWithColumns(20)));
1815 EXPECT_EQ("int i; /* first line\n"
1820 format("int i; /* first line\n"
1824 getLLVMStyleWithColumns(20)));
1826 // Reflow the last two lines of a section that starts with a line having
1827 // different indentation.
1830 "// long long long\n"
1833 "// long long long long\n"
1835 getLLVMStyleWithColumns(20)));
1837 // Keep the block comment endling '*/' while reflowing.
1838 EXPECT_EQ("/* Long long long\n"
1839 " * line short */\n",
1840 format("/* Long long long line\n"
1842 getLLVMStyleWithColumns(20)));
1844 // Don't reflow between separate blocks of comments.
1845 EXPECT_EQ("/* First comment\n"
1846 " * block will */\n"
1849 format("/* First comment block\n"
1853 getLLVMStyleWithColumns(20)));
1855 // Don't reflow across blank comment lines.
1856 EXPECT_EQ("int i; // This long\n"
1861 format("int i; // This long line gets broken.\n"
1864 getLLVMStyleWithColumns(20)));
1866 " /// long long long\n"
1872 " /// 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)));
1886 EXPECT_EQ("/* long long long\n"
1890 format("/* long long long long\n"
1893 getLLVMStyleWithColumns(20)));
1894 EXPECT_EQ("/* long long long\n"
1898 format("/* long long long long\n"
1901 getLLVMStyleWithColumns(20)));
1903 // Don't reflow lines having content that is a single character.
1904 EXPECT_EQ("// long long long\n"
1907 format("// long long long long\n"
1909 getLLVMStyleWithColumns(20)));
1911 // Don't reflow lines starting with two punctuation characters.
1912 EXPECT_EQ("// long long long\n"
1916 "// long long long long\n"
1918 getLLVMStyleWithColumns(20)));
1920 // Don't reflow lines starting with '@'.
1921 EXPECT_EQ("// long long long\n"
1924 format("// long long long long\n"
1926 getLLVMStyleWithColumns(20)));
1928 // Don't reflow lines starting with 'TODO'.
1929 EXPECT_EQ("// long long long\n"
1932 format("// long long long long\n"
1934 getLLVMStyleWithColumns(20)));
1936 // Don't reflow lines starting with 'FIXME'.
1937 EXPECT_EQ("// long long long\n"
1940 format("// long long long long\n"
1942 getLLVMStyleWithColumns(20)));
1944 // Don't reflow lines starting with 'XXX'.
1945 EXPECT_EQ("// long long long\n"
1948 format("// long long long long\n"
1950 getLLVMStyleWithColumns(20)));
1952 // Don't reflow comment pragmas.
1953 EXPECT_EQ("// long long long\n"
1956 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)));
1968 // Reflow lines that have a non-punctuation character among their first 2
1970 EXPECT_EQ("// long long long\n"
1973 "// long long long long\n"
1975 getLLVMStyleWithColumns(20)));
1977 // Don't reflow between separate blocks of comments.
1978 EXPECT_EQ("/* First comment\n"
1979 " * block will */\n"
1982 format("/* First comment block\n"
1986 getLLVMStyleWithColumns(20)));
1988 // Don't reflow lines having different indentation.
1989 EXPECT_EQ("// long long long\n"
1992 format("// long long long long\n"
1994 getLLVMStyleWithColumns(20)));
1996 // Don't reflow separate bullets in list
1997 EXPECT_EQ("// - long long long\n"
2000 format("// - 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)));
2009 EXPECT_EQ("// + long long long\n"
2012 format("// + long long long long\n"
2014 getLLVMStyleWithColumns(20)));
2015 EXPECT_EQ("// 1. long long long\n"
2018 format("// 1. long long long long\n"
2020 getLLVMStyleWithColumns(20)));
2021 EXPECT_EQ("// -# long long long\n"
2024 format("// -# long long long long\n"
2026 getLLVMStyleWithColumns(20)));
2028 EXPECT_EQ("// - long long long\n"
2029 "// long long long\n"
2031 format("// - long long long long\n"
2034 getLLVMStyleWithColumns(20)));
2035 EXPECT_EQ("// - long long long\n"
2036 "// long long long\n"
2039 format("// - long long long long\n"
2040 "// long long long\n"
2042 getLLVMStyleWithColumns(20)));
2044 // Large number (>2 digits) are not list items
2045 EXPECT_EQ("// long long long\n"
2046 "// long 1024. long.",
2047 format("// long long long long\n"
2049 getLLVMStyleWithColumns(20)));
2051 // Do not break before number, to avoid introducing a non-reflowable doxygen
2053 EXPECT_EQ("// long long\n"
2054 "// long 10. long.",
2055 format("// long long long 10.\n"
2057 getLLVMStyleWithColumns(20)));
2059 // Don't break or reflow after implicit string literals.
2060 verifyFormat("#include <t> // l l l\n"
2062 getLLVMStyleWithColumns(20));
2064 // Don't break or reflow comments on import lines.
2065 EXPECT_EQ("#include \"t\" /* l l l\n"
2067 format("#include \"t\" /* l l l\n"
2069 getLLVMStyleWithColumns(20)));
2071 // Don't reflow between different trailing comment sections.
2072 EXPECT_EQ("int i; // long long\n"
2074 "int j; // long long\n"
2076 format("int i; // long long long\n"
2077 "int j; // long long long\n",
2078 getLLVMStyleWithColumns(20)));
2080 // Don't reflow if the first word on the next line is longer than the
2081 // available space at current line.
2082 EXPECT_EQ("int i; // trigger\n"
2085 format("int i; // trigger reflow\n"
2087 getLLVMStyleWithColumns(20)));
2089 // Simple case that correctly handles reflow in parameter lists.
2090 EXPECT_EQ("a = f(/* looooooooong\n"
2094 format("a = f(/* looooooooong long\n* long\n*/ a);",
2095 getLLVMStyleWithColumns(22)));
2096 // Tricky case that has fewer lines if we reflow the comment, ending up with
2098 EXPECT_EQ("a = f(/* loooooong\n"
2102 format("a = f(/* loooooong long\n* long\n*/ a);",
2103 getLLVMStyleWithColumns(22)));
2105 // Keep empty comment lines.
2106 EXPECT_EQ("/**/", format(" /**/", getLLVMStyleWithColumns(20)));
2107 EXPECT_EQ("/* */", format(" /* */", getLLVMStyleWithColumns(20)));
2108 EXPECT_EQ("/* */", format(" /* */", getLLVMStyleWithColumns(20)));
2109 EXPECT_EQ("//", format(" // ", getLLVMStyleWithColumns(20)));
2110 EXPECT_EQ("///", format(" /// ", getLLVMStyleWithColumns(20)));
2113 TEST_F(FormatTestComments, ReflowsCommentsPrecise) {
2114 // FIXME: This assumes we do not continue compressing whitespace once we are
2115 // in reflow mode. Consider compressing whitespace.
2117 // Test that we stop reflowing precisely at the column limit.
2118 // After reflowing, "// reflows into foo" does not fit the column limit,
2119 // so we compress the whitespace.
2120 EXPECT_EQ("// some text that\n"
2121 "// reflows into foo\n",
2122 format("// some text that reflows\n"
2124 getLLVMStyleWithColumns(20)));
2125 // Given one more column, "// reflows into foo" does fit the limit, so we
2126 // do not compress the whitespace.
2127 EXPECT_EQ("// some text that\n"
2128 "// reflows into foo\n",
2129 format("// some text that reflows\n"
2131 getLLVMStyleWithColumns(21)));
2133 // Make sure that we correctly account for the space added in the reflow case
2134 // when making the reflowing decision.
2135 // First, when the next line ends precisely one column over the limit, do not
2137 EXPECT_EQ("// some text that\n"
2140 format("// some text that reflows\n"
2142 getLLVMStyleWithColumns(21)));
2143 // Secondly, when the next line ends later, but the first word in that line
2144 // is precisely one column over the limit, do not reflow.
2145 EXPECT_EQ("// some text that\n"
2147 "// into1234567 f\n",
2148 format("// some text that reflows\n"
2149 "// into1234567 f\n",
2150 getLLVMStyleWithColumns(21)));
2153 TEST_F(FormatTestComments, ReflowsCommentsWithExtraWhitespace) {
2155 EXPECT_EQ("// some text\n"
2156 "// that re flows\n",
2157 format("// some text that\n"
2159 getLLVMStyleWithColumns(16)));
2160 EXPECT_EQ("// some text\n"
2161 "// that re flows\n",
2162 format("// some text that\n"
2164 getLLVMStyleWithColumns(16)));
2165 EXPECT_EQ("/* some text\n"
2166 " * that re flows\n"
2168 format("/* some text that\n"
2171 getLLVMStyleWithColumns(16)));
2172 // FIXME: We do not reflow if the indent of two subsequent lines differs;
2173 // given that this is different behavior from block comments, do we want
2175 EXPECT_EQ("// some text\n"
2178 format("// some text that\n"
2180 getLLVMStyleWithColumns(16)));
2181 // Space within parts of a line that fit.
2182 // FIXME: Use the earliest possible split while reflowing to compress the
2183 // whitespace within the line.
2184 EXPECT_EQ("// some text that\n"
2187 format("// some text that does\n"
2188 "// re flow more here\n",
2189 getLLVMStyleWithColumns(21)));
2192 TEST_F(FormatTestComments, IgnoresIf0Contents) {
2194 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
2198 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
2201 EXPECT_EQ("#if false\n"
2205 format("#if false\n"
2208 "void g( ) { }\n"));
2209 EXPECT_EQ("enum E {\n"
2225 EXPECT_EQ("enum F {\n"
2247 EXPECT_EQ("enum G {\n"
2265 EXPECT_EQ("enum H {\n"
2287 EXPECT_EQ("enum I {\n"
2289 "#if /* test */ 0 || 1\n"
2297 "#if /* test */ 0 || 1\n"
2303 EXPECT_EQ("enum J {\n"
2328 // Ignore stuff in SWIG-blocks.
2329 EXPECT_EQ("#ifdef SWIG\n"
2330 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
2333 format("#ifdef SWIG\n"
2334 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
2337 EXPECT_EQ("#ifndef SWIG\n"
2340 format("#ifndef SWIG\n"
2345 TEST_F(FormatTestComments, DontCrashOnBlockComments) {
2347 "int xxxxxxxxx; /* "
2348 "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\n"
2351 format("int xxxxxxxxx; /* "
2352 "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy zzzzzz\n"
2356 TEST_F(FormatTestComments, BlockCommentsInControlLoops) {
2357 verifyFormat("if (0) /* a comment in a strange place */ {\n"
2360 verifyFormat("if (0) /* a comment in a strange place */ {\n"
2362 "} /* another comment */ else /* comment #3 */ {\n"
2365 verifyFormat("while (0) /* a comment in a strange place */ {\n"
2368 verifyFormat("for (;;) /* a comment in a strange place */ {\n"
2371 verifyFormat("do /* a comment in a strange place */ {\n"
2373 "} /* another comment */ while (0);");
2376 TEST_F(FormatTestComments, BlockComments) {
2377 EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */",
2378 format("/* *//* */ /* */\n/* *//* */ /* */"));
2379 EXPECT_EQ("/* */ a /* */ b;", format(" /* */ a/* */ b;"));
2380 EXPECT_EQ("#define A /*123*/ \\\n"
2385 format("#define A /*123*/ b\n"
2387 "someCall(parameter);",
2388 getLLVMStyleWithColumns(15)));
2390 EXPECT_EQ("#define A\n"
2393 format("#define A\n"
2394 "/* */someCall(parameter);",
2395 getLLVMStyleWithColumns(15)));
2396 EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/"));
2404 " * aaaaaa aaaaaa\n"
2406 getLLVMStyleWithColumns(10)));
2416 getLLVMStyleWithColumns(10)));
2417 EXPECT_EQ("int aaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
2419 " bbbbbbbbbbbb */\n"
2420 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
2421 format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
2423 " bbbbbbbbbbbb */ bbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
2424 getLLVMStyleWithColumns(50)));
2426 FormatStyle NoBinPacking = getLLVMStyle();
2427 NoBinPacking.BinPackParameters = false;
2428 EXPECT_EQ("someFunction(1, /* comment 1 */\n"
2429 " 2, /* comment 2 */\n"
2430 " 3, /* comment 3 */\n"
2433 format("someFunction (1, /* comment 1 */\n"
2434 " 2, /* comment 2 */ \n"
2435 " 3, /* comment 3 */\n"
2439 "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2440 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
2442 "bool aaaaaaaaaaaaa = /* trailing comment */\n"
2443 " aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2444 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;",
2446 "bool aaaaaaaaaaaaa = /* trailing comment */\n"
2447 " aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2448 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;"));
2450 "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
2451 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
2452 "int cccccccccccccccccccccccccccccc; /* comment */\n",
2453 format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
2454 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
2455 "int cccccccccccccccccccccccccccccc; /* comment */\n"));
2457 verifyFormat("void f(int * /* unused */) {}");
2487 TEST_F(FormatTestComments, BlockCommentsInMacros) {
2488 EXPECT_EQ("#define A \\\n"
2490 " /* one line */ \\\n"
2492 format("#define A { \\\n"
2493 " /* one line */ \\\n"
2495 getLLVMStyleWithColumns(20)));
2496 EXPECT_EQ("#define A \\\n"
2498 " /* previous */ \\\n"
2499 " /* one line */ \\\n"
2501 format("#define A { \\\n"
2502 " /* previous */ \\\n"
2503 " /* one line */ \\\n"
2505 getLLVMStyleWithColumns(20)));
2508 TEST_F(FormatTestComments, BlockCommentsAtEndOfLine) {
2512 format("a = {1111 /* */\n"
2514 getLLVMStyleWithColumns(15)));
2518 format("a = {1111 /* */\n"
2520 getLLVMStyleWithColumns(15)));
2525 format("a = {1111 /* a */\n"
2527 getLLVMStyleWithColumns(15)));
2530 TEST_F(FormatTestComments, BreaksAfterMultilineBlockCommentsInParamLists) {
2531 EXPECT_EQ("a = f(/* long\n"
2534 format("a = f(/* long long */ a);", getLLVMStyleWithColumns(16)));
2535 EXPECT_EQ("a = f(\n"
2539 format("a = f(/* long long */ a);", getLLVMStyleWithColumns(15)));
2541 EXPECT_EQ("a = f(/* long\n"
2545 format("a = f(/* long\n"
2548 getLLVMStyleWithColumns(16)));
2550 EXPECT_EQ("a = f(/* long\n"
2554 format("a = f(/* long\n"
2557 getLLVMStyleWithColumns(16)));
2559 EXPECT_EQ("a = f(/* long\n"
2563 format("a = f(/* long\n"
2566 getLLVMStyleWithColumns(16)));
2573 format("a = f(a, /* long long */ b);", getLLVMStyleWithColumns(16)));
2581 format("a = f(a, /* long long */ b);", getLLVMStyleWithColumns(15)));
2583 EXPECT_EQ("a = f(a,\n"
2587 format("a = f(a, /* long long */ (1 + 1));",
2588 getLLVMStyleWithColumns(16)));
2589 EXPECT_EQ("a = f(\n"
2594 format("a = f(a, /* long long */ (1 + 1));",
2595 getLLVMStyleWithColumns(15)));
2598 TEST_F(FormatTestComments, IndentLineCommentsInStartOfBlockAtEndOfFile) {
2604 TEST_F(FormatTestComments, AlignTrailingComments) {
2605 EXPECT_EQ("#define MACRO(V) \\\n"
2606 " V(Rt2) /* one more char */ \\\n"
2607 " V(Rs) /* than here */ \\\n"
2608 "/* comment 3 */\n",
2609 format("#define MACRO(V)\\\n"
2610 "V(Rt2) /* one more char */ \\\n"
2611 "V(Rs) /* than here */ \\\n"
2612 "/* comment 3 */\n",
2613 getLLVMStyleWithColumns(40)));
2614 EXPECT_EQ("int i = f(abc, // line 1\n"
2618 format("int i = f(abc, // line 1\n"
2622 getLLVMStyleWithColumns(40)));
2624 // Align newly broken trailing comments.
2625 EXPECT_EQ("int ab; // line\n"
2628 format("int ab; // line\n"
2629 "int a; // long long\n",
2630 getLLVMStyleWithColumns(15)));
2631 EXPECT_EQ("int ab; // line\n"
2635 format("int ab; // line\n"
2636 "int a; // long long\n"
2638 getLLVMStyleWithColumns(15)));
2639 EXPECT_EQ("int ab; // line\n"
2643 format("int ab; // line\n"
2644 "int a; // long long\n"
2646 getLLVMStyleWithColumns(15)));
2647 EXPECT_EQ("int ab; // line\n"
2652 format("int ab; // line\n"
2653 "int a; // long long\n"
2656 getLLVMStyleWithColumns(15)));
2658 // Don't align newly broken trailing comments if that would put them over the
2660 EXPECT_EQ("int i, j; // line 1\n"
2661 "int k; // line longg\n"
2663 format("int i, j; // line 1\n"
2664 "int k; // line longg long",
2665 getLLVMStyleWithColumns(20)));
2667 // Always align if ColumnLimit = 0
2668 EXPECT_EQ("int i, j; // line 1\n"
2669 "int k; // line longg long",
2670 format("int i, j; // line 1\n"
2671 "int k; // line longg long",
2672 getLLVMStyleWithColumns(0)));
2674 // Align comment line sections aligned with the next token with the next
2676 EXPECT_EQ("class A {\n"
2677 "public: // public comment\n"
2678 " // comment about a\n"
2681 format("class A {\n"
2682 "public: // public comment\n"
2683 " // comment about a\n"
2686 getLLVMStyleWithColumns(40)));
2687 EXPECT_EQ("class A {\n"
2688 "public: // public comment 1\n"
2689 " // public comment 2\n"
2690 " // comment 1 about a\n"
2691 " // comment 2 about a\n"
2694 format("class A {\n"
2695 "public: // public comment 1\n"
2696 " // public comment 2\n"
2697 " // comment 1 about a\n"
2698 " // comment 2 about a\n"
2701 getLLVMStyleWithColumns(40)));
2702 EXPECT_EQ("int f(int n) { // comment line 1 on f\n"
2703 " // comment line 2 on f\n"
2704 " // comment line 1 before return\n"
2705 " // comment line 2 before return\n"
2706 " return n; // comment line 1 on return\n"
2707 " // comment line 2 on return\n"
2708 " // comment line 1 after return\n"
2710 format("int f(int n) { // comment line 1 on f\n"
2711 " // comment line 2 on f\n"
2712 " // comment line 1 before return\n"
2713 " // comment line 2 before return\n"
2714 " return n; // comment line 1 on return\n"
2715 " // comment line 2 on return\n"
2716 " // comment line 1 after return\n"
2718 getLLVMStyleWithColumns(40)));
2719 EXPECT_EQ("int f(int n) {\n"
2720 " switch (n) { // comment line 1 on switch\n"
2721 " // comment line 2 on switch\n"
2722 " // comment line 1 before case 1\n"
2723 " // comment line 2 before case 1\n"
2724 " case 1: // comment line 1 on case 1\n"
2725 " // comment line 2 on case 1\n"
2726 " // comment line 1 before return 1\n"
2727 " // comment line 2 before return 1\n"
2728 " return 1; // comment line 1 on return 1\n"
2729 " // comment line 2 on return 1\n"
2730 " // comment line 1 before default\n"
2731 " // comment line 2 before default\n"
2732 " default: // comment line 1 on default\n"
2733 " // comment line 2 on default\n"
2734 " // comment line 1 before return 2\n"
2735 " return 2 * f(n - 1); // comment line 1 on return 2\n"
2736 " // comment line 2 on return 2\n"
2737 " // comment line 1 after return\n"
2738 " // comment line 2 after return\n"
2741 format("int f(int n) {\n"
2742 " switch (n) { // comment line 1 on switch\n"
2743 " // comment line 2 on switch\n"
2744 " // comment line 1 before case 1\n"
2745 " // comment line 2 before case 1\n"
2746 " case 1: // comment line 1 on case 1\n"
2747 " // comment line 2 on case 1\n"
2748 " // comment line 1 before return 1\n"
2749 " // comment line 2 before return 1\n"
2750 " return 1; // comment line 1 on return 1\n"
2751 " // comment line 2 on return 1\n"
2752 " // comment line 1 before default\n"
2753 " // comment line 2 before default\n"
2754 " default: // comment line 1 on default\n"
2755 " // comment line 2 on default\n"
2756 " // comment line 1 before return 2\n"
2757 " return 2 * f(n - 1); // comment line 1 on return 2\n"
2758 " // comment line 2 on return 2\n"
2759 " // comment line 1 after return\n"
2760 " // comment line 2 after return\n"
2763 getLLVMStyleWithColumns(80)));
2765 // If all the lines in a sequence of line comments are aligned with the next
2766 // token, the first line belongs to the previous token and the other lines
2767 // belong to the next token.
2768 EXPECT_EQ("int a; // line about a\n"
2770 format("int a; // line about a\n"
2772 getLLVMStyleWithColumns(80)));
2773 EXPECT_EQ("int a; // line about a\n"
2776 format("int a; // line about a\n"
2777 " // line about b\n"
2779 getLLVMStyleWithColumns(80)));
2780 EXPECT_EQ("int a; // line about a\n"
2781 "// line 1 about b\n"
2782 "// line 2 about b\n"
2784 format("int a; // line about a\n"
2785 " // line 1 about b\n"
2786 " // line 2 about b\n"
2788 getLLVMStyleWithColumns(80)));
2791 TEST_F(FormatTestComments, AlignsBlockCommentDecorations) {
2795 "*/", getLLVMStyle()));
2799 " */", getLLVMStyle()));
2803 " */", getLLVMStyle()));
2805 // Align a single line.
2852 // Align the end '*/' after a line.
2858 "*/", getLLVMStyle()));
2864 " */", getLLVMStyle()));
2870 " */", getLLVMStyle()));
2873 EXPECT_EQ("/* line 1\n"
2875 format("/* line 1\n"
2878 EXPECT_EQ("/* line 1\n"
2880 format("/* line 1\n"
2883 EXPECT_EQ("/* line 1\n"
2885 format("/* line 1\n"
2888 EXPECT_EQ("/* line 1\n"
2890 format("/* line 1\n"
2893 EXPECT_EQ("/* line 1\n"
2895 format("/* line 1\n"
2898 EXPECT_EQ("int i; /* line 1\n"
2900 format("int i; /* line 1\n"
2903 EXPECT_EQ("int i; /* line 1\n"
2905 format("int i; /* line 1\n"
2908 EXPECT_EQ("int i; /* line 1\n"
2910 format("int i; /* line 1\n"
2914 // Align several lines.
2915 EXPECT_EQ("/* line 1\n"
2918 format("/* line 1\n"
2922 EXPECT_EQ("/* line 1\n"
2925 format("/* line 1\n"
2939 // Align with different indent after the decorations.
2951 "*/", getLLVMStyle()));
2953 // Align empty or blank lines.
2963 "*/", getLLVMStyle()));
2965 // Align while breaking and reflowing.
2967 " * long long long\n"
2972 " * long long long long\n"
2976 getLLVMStyleWithColumns(20)));
2979 TEST_F(FormatTestComments, NoCrash_Bug34236) {
2980 // This is a test case from a crasher reported in:
2981 // https://bugs.llvm.org/show_bug.cgi?id=34236
2982 // Temporarily disable formatting for readability.
2992 getLLVMStyleWithColumns(80)));
2996 TEST_F(FormatTestComments, NonTrailingBlockComments) {
2997 verifyFormat("const /** comment comment */ A = B;",
2998 getLLVMStyleWithColumns(40));
3000 verifyFormat("const /** comment comment comment */ A =\n"
3002 getLLVMStyleWithColumns(40));
3004 EXPECT_EQ("const /** comment comment comment\n"
3007 format("const /** comment comment comment comment */\n"
3009 getLLVMStyleWithColumns(40)));
3012 TEST_F(FormatTestComments, PythonStyleComments) {
3013 // Keeps a space after '#'.
3014 EXPECT_EQ("# comment\n"
3018 getTextProtoStyleWithColumns(20)));
3019 EXPECT_EQ("# comment\n"
3021 format("# comment\n"
3023 getTextProtoStyleWithColumns(20)));
3024 // Breaks long comment.
3025 EXPECT_EQ("# comment comment\n"
3028 format("# comment comment comment\n"
3030 getTextProtoStyleWithColumns(20)));
3031 // Indents comments.
3032 EXPECT_EQ("data {\n"
3033 " # comment comment\n"
3038 "# comment comment comment\n"
3040 getTextProtoStyleWithColumns(20)));
3041 EXPECT_EQ("data {\n"
3042 " # comment comment\n"
3046 format("data {# comment comment comment\n"
3048 getTextProtoStyleWithColumns(20)));
3049 // Reflows long comments.
3050 EXPECT_EQ("# comment comment\n"
3051 "# comment comment\n"
3053 format("# comment comment comment\n"
3056 getTextProtoStyleWithColumns(20)));
3057 // Breaks trailing comments.
3058 EXPECT_EQ("k: val # comment\n"
3061 format("k:val#comment comment\n"
3063 getTextProtoStyleWithColumns(20)));
3065 " k: val # comment\n"
3070 format("id {k:val#comment comment\n"
3073 getTextProtoStyleWithColumns(20)));
3074 // Aligns trailing comments.
3075 EXPECT_EQ("k: val # commen1\n"
3082 format("k:val#commen1 commen2\n"
3085 "a:1#commen5 commen6\n"
3087 getTextProtoStyleWithColumns(20)));
3090 TEST_F(FormatTestComments, BreaksBeforeTrailingUnbreakableSequence) {
3091 // The end of /* trail */ is exactly at 80 columns, but the unbreakable
3092 // trailing sequence ); after it exceeds the column limit. Make sure we
3093 // correctly break the line in that case.
3094 verifyFormat("int a =\n"
3095 " foo(/* trail */);",
3096 getLLVMStyleWithColumns(23));
3099 TEST_F(FormatTestComments, ReflowBackslashCrash) {
3104 "// rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr\n"
3105 "// \\ <log_file> -- --output_directory=\"<output_directory>\"",
3109 "// rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr \\\n"
3110 "// <log_file> -- --output_directory=\"<output_directory>\""));
3114 TEST_F(FormatTestComments, IndentsLongJavadocAnnotatedLines) {
3115 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Java);
3116 Style.ColumnLimit = 60;
3117 FormatStyle Style20 = getGoogleStyle(FormatStyle::LK_Java);
3118 Style20.ColumnLimit = 20;
3121 " * @param x long long long long long long long long long\n"
3125 " * @param x long long long long long long long long long long\n"
3129 " * @param x long long long long long long long long long\n"
3130 " * long long long long long long long long long long\n"
3133 " * @param x long long long long long long long long long "
3134 "long long long long long long long long long long\n"
3138 " * @param x long long long long long long long long long\n"
3139 " * long long long long long long long long long long\n"
3143 " * @param x long long long long long long long long long "
3144 "long long long long long long long long long long long\n"
3149 " * Sentence that\n"
3150 " * should be broken.\n"
3152 " * keep indentation\n"
3155 " * Sentence that should be broken.\n"
3157 " * keep indentation\n"
3161 " * @param l1 long1\n"
3163 " * @param l2 long2\n"
3167 " * @param l1 long1 to break\n"
3168 " * @param l2 long2 to break\n"
3178 " * @param xx to break\n"
3189 " * @param xx to break\n"
3194 FormatStyle JSStyle20 = getGoogleStyle(FormatStyle::LK_JavaScript);
3195 JSStyle20.ColumnLimit = 20;
3197 " * @param l1 long1\n"
3201 " * @param l1 long1 to break\n"
3205 " * @param {l1 long1\n"
3209 " * @param {l1 long1 to break}\n"
3215 } // end namespace format
3216 } // end namespace clang