1 //===- unittest/Format/FormatTestComments.cpp - Formatting unit tests -----===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "clang/Format/Format.h"
10 
11 #include "../Tooling/ReplacementTest.h"
12 #include "FormatTestUtils.h"
13 
14 #include "llvm/Support/Debug.h"
15 #include "llvm/Support/MemoryBuffer.h"
16 #include "gtest/gtest.h"
17 
18 #define DEBUG_TYPE "format-test"
19 
20 using clang::tooling::ReplacementTest;
21 
22 namespace clang {
23 namespace format {
24 namespace {
25 
getGoogleStyle()26 FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); }
27 
28 class FormatTestComments : public ::testing::Test {
29 protected:
30   enum StatusCheck { SC_ExpectComplete, SC_ExpectIncomplete, SC_DoNotCheck };
31 
format(llvm::StringRef Code,const FormatStyle & Style=getLLVMStyle (),StatusCheck CheckComplete=SC_ExpectComplete)32   std::string format(llvm::StringRef Code,
33                      const FormatStyle &Style = getLLVMStyle(),
34                      StatusCheck CheckComplete = SC_ExpectComplete) {
35     LLVM_DEBUG(llvm::errs() << "---\n");
36     LLVM_DEBUG(llvm::errs() << Code << "\n\n");
37     std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
38     FormattingAttemptStatus Status;
39     tooling::Replacements Replaces =
40         reformat(Style, Code, Ranges, "<stdin>", &Status);
41     if (CheckComplete != SC_DoNotCheck) {
42       bool ExpectedCompleteFormat = CheckComplete == SC_ExpectComplete;
43       EXPECT_EQ(ExpectedCompleteFormat, Status.FormatComplete)
44           << Code << "\n\n";
45     }
46     ReplacementCount = Replaces.size();
47     auto Result = applyAllReplacements(Code, Replaces);
48     EXPECT_TRUE(static_cast<bool>(Result));
49     LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
50     return *Result;
51   }
52 
getLLVMStyleWithColumns(unsigned ColumnLimit)53   FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
54     FormatStyle Style = getLLVMStyle();
55     Style.ColumnLimit = ColumnLimit;
56     return Style;
57   }
58 
getTextProtoStyleWithColumns(unsigned ColumnLimit)59   FormatStyle getTextProtoStyleWithColumns(unsigned ColumnLimit) {
60     FormatStyle Style = getGoogleStyle(FormatStyle::FormatStyle::LK_TextProto);
61     Style.ColumnLimit = ColumnLimit;
62     return Style;
63   }
64 
verifyFormat(llvm::StringRef Code,const FormatStyle & Style=getLLVMStyle ())65   void verifyFormat(llvm::StringRef Code,
66                     const FormatStyle &Style = getLLVMStyle()) {
67     EXPECT_EQ(Code.str(), format(Code, Style)) << "Expected code is not stable";
68     EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
69   }
70 
verifyGoogleFormat(llvm::StringRef Code)71   void verifyGoogleFormat(llvm::StringRef Code) {
72     verifyFormat(Code, getGoogleStyle());
73   }
74 
75   /// \brief Verify that clang-format does not crash on the given input.
verifyNoCrash(llvm::StringRef Code,const FormatStyle & Style=getLLVMStyle ())76   void verifyNoCrash(llvm::StringRef Code,
77                      const FormatStyle &Style = getLLVMStyle()) {
78     format(Code, Style, SC_DoNotCheck);
79   }
80 
81   int ReplacementCount;
82 };
83 
84 //===----------------------------------------------------------------------===//
85 // Tests for comments.
86 //===----------------------------------------------------------------------===//
87 
TEST_F(FormatTestComments,UnderstandsSingleLineComments)88 TEST_F(FormatTestComments, UnderstandsSingleLineComments) {
89   verifyFormat("//* */");
90   verifyFormat("// line 1\n"
91                "// line 2\n"
92                "void f() {}\n");
93 
94   EXPECT_EQ("// comment\n", format("//comment\n"));
95   EXPECT_EQ("// #comment\n", format("//#comment\n"));
96 
97   EXPECT_EQ("// comment\n"
98             "// clang-format on\n",
99             format("//comment\n"
100                    "// clang-format on\n"));
101 
102   verifyFormat("void f() {\n"
103                "  // Doesn't do anything\n"
104                "}");
105   verifyFormat("SomeObject\n"
106                "    // Calling someFunction on SomeObject\n"
107                "    .someFunction();");
108   verifyFormat("auto result = SomeObject\n"
109                "                  // Calling someFunction on SomeObject\n"
110                "                  .someFunction();");
111   verifyFormat("void f(int i,  // some comment (probably for i)\n"
112                "       int j,  // some comment (probably for j)\n"
113                "       int k); // some comment (probably for k)");
114   verifyFormat("void f(int i,\n"
115                "       // some comment (probably for j)\n"
116                "       int j,\n"
117                "       // some comment (probably for k)\n"
118                "       int k);");
119 
120   verifyFormat("int i    // This is a fancy variable\n"
121                "    = 5; // with nicely aligned comment.");
122 
123   verifyFormat("// Leading comment.\n"
124                "int a; // Trailing comment.");
125   verifyFormat("int a; // Trailing comment\n"
126                "       // on 2\n"
127                "       // or 3 lines.\n"
128                "int b;");
129   verifyFormat("int a; // Trailing comment\n"
130                "\n"
131                "// Leading comment.\n"
132                "int b;");
133   verifyFormat("int a;    // Comment.\n"
134                "          // More details.\n"
135                "int bbbb; // Another comment.");
136   verifyFormat(
137       "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
138       "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;   // comment\n"
139       "int cccccccccccccccccccccccccccccc;       // comment\n"
140       "int ddd;                     // looooooooooooooooooooooooong comment\n"
141       "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
142       "int bbbbbbbbbbbbbbbbbbbbb;   // comment\n"
143       "int ccccccccccccccccccc;     // comment");
144 
145   verifyFormat("#include \"a\"     // comment\n"
146                "#include \"a/b/c\" // comment");
147   verifyFormat("#include <a>     // comment\n"
148                "#include <a/b/c> // comment");
149   EXPECT_EQ("#include \"a\"     // comment\n"
150             "#include \"a/b/c\" // comment",
151             format("#include \\\n"
152                    "  \"a\" // comment\n"
153                    "#include \"a/b/c\" // comment"));
154 
155   verifyFormat("enum E {\n"
156                "  // comment\n"
157                "  VAL_A, // comment\n"
158                "  VAL_B\n"
159                "};");
160 
161   EXPECT_EQ("enum A {\n"
162             "  // line a\n"
163             "  a,\n"
164             "  b, // line b\n"
165             "\n"
166             "  // line c\n"
167             "  c\n"
168             "};",
169             format("enum A {\n"
170                    "  // line a\n"
171                    "  a,\n"
172                    "  b, // line b\n"
173                    "\n"
174                    "  // line c\n"
175                    "  c\n"
176                    "};",
177                    getLLVMStyleWithColumns(20)));
178   EXPECT_EQ("enum A {\n"
179             "  a, // line 1\n"
180             "  // line 2\n"
181             "};",
182             format("enum A {\n"
183                    "  a, // line 1\n"
184                    "  // line 2\n"
185                    "};",
186                    getLLVMStyleWithColumns(20)));
187   EXPECT_EQ("enum A {\n"
188             "  a, // line 1\n"
189             "     // line 2\n"
190             "};",
191             format("enum A {\n"
192                    "  a, // line 1\n"
193                    "   // line 2\n"
194                    "};",
195                    getLLVMStyleWithColumns(20)));
196   EXPECT_EQ("enum A {\n"
197             "  a, // line 1\n"
198             "  // line 2\n"
199             "  b\n"
200             "};",
201             format("enum A {\n"
202                    "  a, // line 1\n"
203                    "  // line 2\n"
204                    "  b\n"
205                    "};",
206                    getLLVMStyleWithColumns(20)));
207   EXPECT_EQ("enum A {\n"
208             "  a, // line 1\n"
209             "     // line 2\n"
210             "  b\n"
211             "};",
212             format("enum A {\n"
213                    "  a, // line 1\n"
214                    "   // line 2\n"
215                    "  b\n"
216                    "};",
217                    getLLVMStyleWithColumns(20)));
218   verifyFormat(
219       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
220       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
221   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
222                "    // Comment inside a statement.\n"
223                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
224   verifyFormat("SomeFunction(a,\n"
225                "             // comment\n"
226                "             b + x);");
227   verifyFormat("SomeFunction(a, a,\n"
228                "             // comment\n"
229                "             b + x);");
230   verifyFormat(
231       "bool aaaaaaaaaaaaa = // comment\n"
232       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
233       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
234 
235   verifyFormat("int aaaa; // aaaaa\n"
236                "int aa;   // aaaaaaa",
237                getLLVMStyleWithColumns(20));
238 
239   EXPECT_EQ("void f() { // This does something ..\n"
240             "}\n"
241             "int a; // This is unrelated",
242             format("void f()    {     // This does something ..\n"
243                    "  }\n"
244                    "int   a;     // This is unrelated"));
245   EXPECT_EQ("class C {\n"
246             "  void f() { // This does something ..\n"
247             "  }          // awesome..\n"
248             "\n"
249             "  int a; // This is unrelated\n"
250             "};",
251             format("class C{void f()    { // This does something ..\n"
252                    "      } // awesome..\n"
253                    " \n"
254                    "int a;    // This is unrelated\n"
255                    "};"));
256 
257   EXPECT_EQ("int i; // single line trailing comment",
258             format("int i;\\\n// single line trailing comment"));
259 
260   verifyGoogleFormat("int a;  // Trailing comment.");
261 
262   verifyFormat("someFunction(anotherFunction( // Force break.\n"
263                "    parameter));");
264 
265   verifyGoogleFormat("#endif  // HEADER_GUARD");
266 
267   verifyFormat("const char *test[] = {\n"
268                "    // A\n"
269                "    \"aaaa\",\n"
270                "    // B\n"
271                "    \"aaaaa\"};");
272   verifyGoogleFormat(
273       "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
274       "    aaaaaaaaaaaaaaaaaaaaaa);  // 81_cols_with_this_comment");
275   EXPECT_EQ("D(a, {\n"
276             "  // test\n"
277             "  int a;\n"
278             "});",
279             format("D(a, {\n"
280                    "// test\n"
281                    "int a;\n"
282                    "});"));
283 
284   EXPECT_EQ("lineWith(); // comment\n"
285             "// at start\n"
286             "otherLine();",
287             format("lineWith();   // comment\n"
288                    "// at start\n"
289                    "otherLine();"));
290   EXPECT_EQ("lineWith(); // comment\n"
291             "/*\n"
292             " * at start */\n"
293             "otherLine();",
294             format("lineWith();   // comment\n"
295                    "/*\n"
296                    " * at start */\n"
297                    "otherLine();"));
298   EXPECT_EQ("lineWith(); // comment\n"
299             "            // at start\n"
300             "otherLine();",
301             format("lineWith();   // comment\n"
302                    " // at start\n"
303                    "otherLine();"));
304 
305   EXPECT_EQ("lineWith(); // comment\n"
306             "// at start\n"
307             "otherLine(); // comment",
308             format("lineWith();   // comment\n"
309                    "// at start\n"
310                    "otherLine();   // comment"));
311   EXPECT_EQ("lineWith();\n"
312             "// at start\n"
313             "otherLine(); // comment",
314             format("lineWith();\n"
315                    " // at start\n"
316                    "otherLine();   // comment"));
317   EXPECT_EQ("// first\n"
318             "// at start\n"
319             "otherLine(); // comment",
320             format("// first\n"
321                    " // at start\n"
322                    "otherLine();   // comment"));
323   EXPECT_EQ("f();\n"
324             "// first\n"
325             "// at start\n"
326             "otherLine(); // comment",
327             format("f();\n"
328                    "// first\n"
329                    " // at start\n"
330                    "otherLine();   // comment"));
331   verifyFormat("f(); // comment\n"
332                "// first\n"
333                "// at start\n"
334                "otherLine();");
335   EXPECT_EQ("f(); // comment\n"
336             "// first\n"
337             "// at start\n"
338             "otherLine();",
339             format("f();   // comment\n"
340                    "// first\n"
341                    " // at start\n"
342                    "otherLine();"));
343   EXPECT_EQ("f(); // comment\n"
344             "     // first\n"
345             "// at start\n"
346             "otherLine();",
347             format("f();   // comment\n"
348                    " // first\n"
349                    "// at start\n"
350                    "otherLine();"));
351   EXPECT_EQ("void f() {\n"
352             "  lineWith(); // comment\n"
353             "  // at start\n"
354             "}",
355             format("void              f() {\n"
356                    "  lineWith(); // comment\n"
357                    "  // at start\n"
358                    "}"));
359   EXPECT_EQ("int xy; // a\n"
360             "int z;  // b",
361             format("int xy;    // a\n"
362                    "int z;    //b"));
363   EXPECT_EQ("int xy; // a\n"
364             "int z; // bb",
365             format("int xy;    // a\n"
366                    "int z;    //bb",
367                    getLLVMStyleWithColumns(12)));
368 
369   verifyFormat("#define A                                                  \\\n"
370                "  int i; /* iiiiiiiiiiiiiiiiiiiii */                       \\\n"
371                "  int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
372                getLLVMStyleWithColumns(60));
373   verifyFormat(
374       "#define A                                                   \\\n"
375       "  int i;                        /* iiiiiiiiiiiiiiiiiiiii */ \\\n"
376       "  int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
377       getLLVMStyleWithColumns(61));
378 
379   verifyFormat("if ( // This is some comment\n"
380                "    x + 3) {\n"
381                "}");
382   EXPECT_EQ("if ( // This is some comment\n"
383             "     // spanning two lines\n"
384             "    x + 3) {\n"
385             "}",
386             format("if( // This is some comment\n"
387                    "     // spanning two lines\n"
388                    " x + 3) {\n"
389                    "}"));
390 
391   verifyNoCrash("/\\\n/");
392   verifyNoCrash("/\\\n* */");
393   // The 0-character somehow makes the lexer return a proper comment.
394   verifyNoCrash(StringRef("/*\\\0\n/", 6));
395 }
396 
TEST_F(FormatTestComments,KeepsParameterWithTrailingCommentsOnTheirOwnLine)397 TEST_F(FormatTestComments, KeepsParameterWithTrailingCommentsOnTheirOwnLine) {
398   EXPECT_EQ("SomeFunction(a,\n"
399             "             b, // comment\n"
400             "             c);",
401             format("SomeFunction(a,\n"
402                    "          b, // comment\n"
403                    "      c);"));
404   EXPECT_EQ("SomeFunction(a, b,\n"
405             "             // comment\n"
406             "             c);",
407             format("SomeFunction(a,\n"
408                    "          b,\n"
409                    "  // comment\n"
410                    "      c);"));
411   EXPECT_EQ("SomeFunction(a, b, // comment (unclear relation)\n"
412             "             c);",
413             format("SomeFunction(a, b, // comment (unclear relation)\n"
414                    "      c);"));
415   EXPECT_EQ("SomeFunction(a, // comment\n"
416             "             b,\n"
417             "             c); // comment",
418             format("SomeFunction(a,     // comment\n"
419                    "          b,\n"
420                    "      c); // comment"));
421   EXPECT_EQ("aaaaaaaaaa(aaaa(aaaa,\n"
422             "                aaaa), //\n"
423             "           aaaa, bbbbb);",
424             format("aaaaaaaaaa(aaaa(aaaa,\n"
425                    "aaaa), //\n"
426                    "aaaa, bbbbb);"));
427 }
428 
TEST_F(FormatTestComments,RemovesTrailingWhitespaceOfComments)429 TEST_F(FormatTestComments, RemovesTrailingWhitespaceOfComments) {
430   EXPECT_EQ("// comment", format("// comment  "));
431   EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment",
432             format("int aaaaaaa, bbbbbbb; // comment                   ",
433                    getLLVMStyleWithColumns(33)));
434   EXPECT_EQ("// comment\\\n", format("// comment\\\n  \t \v   \f   "));
435   EXPECT_EQ("// comment    \\\n", format("// comment    \\\n  \t \v   \f   "));
436 }
437 
TEST_F(FormatTestComments,UnderstandsBlockComments)438 TEST_F(FormatTestComments, UnderstandsBlockComments) {
439   verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);");
440   verifyFormat("void f() { g(/*aaa=*/x, /*bbb=*/!y, /*c=*/::c); }");
441   EXPECT_EQ("f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n"
442             "  bbbbbbbbbbbbbbbbbbbbbbbbb);",
443             format("f(aaaaaaaaaaaaaaaaaaaaaaaaa ,   \\\n"
444                    "/* Trailing comment for aa... */\n"
445                    "  bbbbbbbbbbbbbbbbbbbbbbbbb);"));
446   EXPECT_EQ(
447       "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
448       "  /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);",
449       format("f(aaaaaaaaaaaaaaaaaaaaaaaaa    ,   \n"
450              "/* Leading comment for bb... */   bbbbbbbbbbbbbbbbbbbbbbbbb);"));
451   EXPECT_EQ(
452       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
453       "    aaaaaaaaaaaaaaaaaa,\n"
454       "    aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
455       "}",
456       format("void      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
457              "                      aaaaaaaaaaaaaaaaaa  ,\n"
458              "    aaaaaaaaaaaaaaaaaa) {   /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
459              "}"));
460   verifyFormat("f(/* aaaaaaaaaaaaaaaaaa = */\n"
461                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
462 
463   FormatStyle NoBinPacking = getLLVMStyle();
464   NoBinPacking.BinPackParameters = false;
465   verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n"
466                "         /* parameter 2 */ aaaaaa,\n"
467                "         /* parameter 3 */ aaaaaa,\n"
468                "         /* parameter 4 */ aaaaaa);",
469                NoBinPacking);
470 
471   // Aligning block comments in macros.
472   verifyGoogleFormat("#define A        \\\n"
473                      "  int i;   /*a*/ \\\n"
474                      "  int jjj; /*b*/");
475 }
476 
TEST_F(FormatTestComments,AlignsBlockComments)477 TEST_F(FormatTestComments, AlignsBlockComments) {
478   EXPECT_EQ("/*\n"
479             " * Really multi-line\n"
480             " * comment.\n"
481             " */\n"
482             "void f() {}",
483             format("  /*\n"
484                    "   * Really multi-line\n"
485                    "   * comment.\n"
486                    "   */\n"
487                    "  void f() {}"));
488   EXPECT_EQ("class C {\n"
489             "  /*\n"
490             "   * Another multi-line\n"
491             "   * comment.\n"
492             "   */\n"
493             "  void f() {}\n"
494             "};",
495             format("class C {\n"
496                    "/*\n"
497                    " * Another multi-line\n"
498                    " * comment.\n"
499                    " */\n"
500                    "void f() {}\n"
501                    "};"));
502   EXPECT_EQ("/*\n"
503             "  1. This is a comment with non-trivial formatting.\n"
504             "     1.1. We have to indent/outdent all lines equally\n"
505             "         1.1.1. to keep the formatting.\n"
506             " */",
507             format("  /*\n"
508                    "    1. This is a comment with non-trivial formatting.\n"
509                    "       1.1. We have to indent/outdent all lines equally\n"
510                    "           1.1.1. to keep the formatting.\n"
511                    "   */"));
512   EXPECT_EQ("/*\n"
513             "Don't try to outdent if there's not enough indentation.\n"
514             "*/",
515             format("  /*\n"
516                    " Don't try to outdent if there's not enough indentation.\n"
517                    " */"));
518 
519   EXPECT_EQ("int i; /* Comment with empty...\n"
520             "        *\n"
521             "        * line. */",
522             format("int i; /* Comment with empty...\n"
523                    "        *\n"
524                    "        * line. */"));
525   EXPECT_EQ("int foobar = 0; /* comment */\n"
526             "int bar = 0;    /* multiline\n"
527             "                   comment 1 */\n"
528             "int baz = 0;    /* multiline\n"
529             "                   comment 2 */\n"
530             "int bzz = 0;    /* multiline\n"
531             "                   comment 3 */",
532             format("int foobar = 0; /* comment */\n"
533                    "int bar = 0;    /* multiline\n"
534                    "                   comment 1 */\n"
535                    "int baz = 0; /* multiline\n"
536                    "                comment 2 */\n"
537                    "int bzz = 0;         /* multiline\n"
538                    "                        comment 3 */"));
539   EXPECT_EQ("int foobar = 0; /* comment */\n"
540             "int bar = 0;    /* multiline\n"
541             "   comment */\n"
542             "int baz = 0;    /* multiline\n"
543             "comment */",
544             format("int foobar = 0; /* comment */\n"
545                    "int bar = 0; /* multiline\n"
546                    "comment */\n"
547                    "int baz = 0;        /* multiline\n"
548                    "comment */"));
549 }
550 
TEST_F(FormatTestComments,CommentReflowingCanBeTurnedOff)551 TEST_F(FormatTestComments, CommentReflowingCanBeTurnedOff) {
552   FormatStyle Style = getLLVMStyleWithColumns(20);
553   Style.ReflowComments = false;
554   verifyFormat("// aaaaaaaaa aaaaaaaaaa aaaaaaaaaa", Style);
555   verifyFormat("/* aaaaaaaaa aaaaaaaaaa aaaaaaaaaa */", Style);
556 }
557 
TEST_F(FormatTestComments,CorrectlyHandlesLengthOfBlockComments)558 TEST_F(FormatTestComments, CorrectlyHandlesLengthOfBlockComments) {
559   EXPECT_EQ("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
560             "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */",
561             format("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
562                    "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */"));
563   EXPECT_EQ(
564       "void ffffffffffff(\n"
565       "    int aaaaaaaa, int bbbbbbbb,\n"
566       "    int cccccccccccc) { /*\n"
567       "                           aaaaaaaaaa\n"
568       "                           aaaaaaaaaaaaa\n"
569       "                           bbbbbbbbbbbbbb\n"
570       "                           bbbbbbbbbb\n"
571       "                         */\n"
572       "}",
573       format("void ffffffffffff(int aaaaaaaa, int bbbbbbbb, int cccccccccccc)\n"
574              "{ /*\n"
575              "     aaaaaaaaaa aaaaaaaaaaaaa\n"
576              "     bbbbbbbbbbbbbb bbbbbbbbbb\n"
577              "   */\n"
578              "}",
579              getLLVMStyleWithColumns(40)));
580 }
581 
TEST_F(FormatTestComments,DontBreakNonTrailingBlockComments)582 TEST_F(FormatTestComments, DontBreakNonTrailingBlockComments) {
583   EXPECT_EQ("void ffffffffff(\n"
584             "    int aaaaa /* test */);",
585             format("void ffffffffff(int aaaaa /* test */);",
586                    getLLVMStyleWithColumns(35)));
587 }
588 
TEST_F(FormatTestComments,SplitsLongCxxComments)589 TEST_F(FormatTestComments, SplitsLongCxxComments) {
590   EXPECT_EQ("// A comment that\n"
591             "// doesn't fit on\n"
592             "// one line",
593             format("// A comment that doesn't fit on one line",
594                    getLLVMStyleWithColumns(20)));
595   EXPECT_EQ("/// A comment that\n"
596             "/// doesn't fit on\n"
597             "/// one line",
598             format("/// A comment that doesn't fit on one line",
599                    getLLVMStyleWithColumns(20)));
600   EXPECT_EQ("//! A comment that\n"
601             "//! doesn't fit on\n"
602             "//! one line",
603             format("//! A comment that doesn't fit on one line",
604                    getLLVMStyleWithColumns(20)));
605   EXPECT_EQ("// a b c d\n"
606             "// e f  g\n"
607             "// h i j k",
608             format("// a b c d e f  g h i j k", getLLVMStyleWithColumns(10)));
609   EXPECT_EQ(
610       "// a b c d\n"
611       "// e f  g\n"
612       "// h i j k",
613       format("\\\n// a b c d e f  g h i j k", getLLVMStyleWithColumns(10)));
614   EXPECT_EQ("if (true) // A comment that\n"
615             "          // doesn't fit on\n"
616             "          // one line",
617             format("if (true) // A comment that doesn't fit on one line   ",
618                    getLLVMStyleWithColumns(30)));
619   EXPECT_EQ("//    Don't_touch_leading_whitespace",
620             format("//    Don't_touch_leading_whitespace",
621                    getLLVMStyleWithColumns(20)));
622   EXPECT_EQ("// Add leading\n"
623             "// whitespace",
624             format("//Add leading whitespace", getLLVMStyleWithColumns(20)));
625   EXPECT_EQ("/// Add leading\n"
626             "/// whitespace",
627             format("///Add leading whitespace", getLLVMStyleWithColumns(20)));
628   EXPECT_EQ("//! Add leading\n"
629             "//! whitespace",
630             format("//!Add leading whitespace", getLLVMStyleWithColumns(20)));
631   EXPECT_EQ("// whitespace", format("//whitespace", getLLVMStyle()));
632   EXPECT_EQ("// Even if it makes the line exceed the column\n"
633             "// limit",
634             format("//Even if it makes the line exceed the column limit",
635                    getLLVMStyleWithColumns(51)));
636   EXPECT_EQ("//--But not here", format("//--But not here", getLLVMStyle()));
637   EXPECT_EQ("/// line 1\n"
638             "// add leading whitespace",
639             format("/// line 1\n"
640                    "//add leading whitespace",
641                    getLLVMStyleWithColumns(30)));
642   EXPECT_EQ("/// line 1\n"
643             "/// line 2\n"
644             "//! line 3\n"
645             "//! line 4\n"
646             "//! line 5\n"
647             "// line 6\n"
648             "// line 7",
649             format("///line 1\n"
650                    "///line 2\n"
651                    "//! line 3\n"
652                    "//!line 4\n"
653                    "//!line 5\n"
654                    "// line 6\n"
655                    "//line 7",
656                    getLLVMStyleWithColumns(20)));
657 
658   EXPECT_EQ("// aa bb cc dd",
659             format("// aa bb             cc dd                   ",
660                    getLLVMStyleWithColumns(15)));
661 
662   EXPECT_EQ("// A comment before\n"
663             "// a macro\n"
664             "// definition\n"
665             "#define a b",
666             format("// A comment before a macro definition\n"
667                    "#define a b",
668                    getLLVMStyleWithColumns(20)));
669   EXPECT_EQ("void ffffff(\n"
670             "    int aaaaaaaaa,  // wwww\n"
671             "    int bbbbbbbbbb, // xxxxxxx\n"
672             "                    // yyyyyyyyyy\n"
673             "    int c, int d, int e) {}",
674             format("void ffffff(\n"
675                    "    int aaaaaaaaa, // wwww\n"
676                    "    int bbbbbbbbbb, // xxxxxxx yyyyyyyyyy\n"
677                    "    int c, int d, int e) {}",
678                    getLLVMStyleWithColumns(40)));
679   EXPECT_EQ("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
680             format("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
681                    getLLVMStyleWithColumns(20)));
682   EXPECT_EQ(
683       "#define XXX // a b c d\n"
684       "            // e f g h",
685       format("#define XXX // a b c d e f g h", getLLVMStyleWithColumns(22)));
686   EXPECT_EQ(
687       "#define XXX // q w e r\n"
688       "            // t y u i",
689       format("#define XXX //q w e r t y u i", getLLVMStyleWithColumns(22)));
690   EXPECT_EQ("{\n"
691             "  //\n"
692             "  //\\\n"
693             "  // long 1 2 3 4 5\n"
694             "}",
695             format("{\n"
696                    "  //\n"
697                    "  //\\\n"
698                    "  // long 1 2 3 4 5\n"
699                    "}",
700                    getLLVMStyleWithColumns(20)));
701   EXPECT_EQ("{\n"
702             "  //\n"
703             "  //\\\n"
704             "  // long 1 2 3 4 5\n"
705             "  // 6\n"
706             "}",
707             format("{\n"
708                    "  //\n"
709                    "  //\\\n"
710                    "  // long 1 2 3 4 5 6\n"
711                    "}",
712                    getLLVMStyleWithColumns(20)));
713 
714   EXPECT_EQ("//: A comment that\n"
715             "//: doesn't fit on\n"
716             "//: one line",
717             format("//: A comment that doesn't fit on one line",
718                    getLLVMStyleWithColumns(20)));
719 }
720 
TEST_F(FormatTestComments,PreservesHangingIndentInCxxComments)721 TEST_F(FormatTestComments, PreservesHangingIndentInCxxComments) {
722   EXPECT_EQ("//     A comment\n"
723             "//     that doesn't\n"
724             "//     fit on one\n"
725             "//     line",
726             format("//     A comment that doesn't fit on one line",
727                    getLLVMStyleWithColumns(20)));
728   EXPECT_EQ("///     A comment\n"
729             "///     that doesn't\n"
730             "///     fit on one\n"
731             "///     line",
732             format("///     A comment that doesn't fit on one line",
733                    getLLVMStyleWithColumns(20)));
734 }
735 
TEST_F(FormatTestComments,DontSplitLineCommentsWithEscapedNewlines)736 TEST_F(FormatTestComments, DontSplitLineCommentsWithEscapedNewlines) {
737   EXPECT_EQ("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
738             "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
739             "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
740             format("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
741                    "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
742                    "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
743   EXPECT_EQ("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
744             "       // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
745             "       // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
746             format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
747                    "       // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
748                    "       // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
749                    getLLVMStyleWithColumns(50)));
750   // FIXME: One day we might want to implement adjustment of leading whitespace
751   // of the consecutive lines in this kind of comment:
752   EXPECT_EQ("double\n"
753             "    a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
754             "          // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
755             "          // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
756             format("double a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
757                    "          // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
758                    "          // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
759                    getLLVMStyleWithColumns(49)));
760 }
761 
TEST_F(FormatTestComments,DontIntroduceMultilineComments)762 TEST_F(FormatTestComments, DontIntroduceMultilineComments) {
763   // Avoid introducing a multiline comment by breaking after `\`.
764   for (int ColumnLimit = 15; ColumnLimit <= 17; ++ColumnLimit) {
765     EXPECT_EQ(
766         "// aaaaaaaaaa\n"
767         "// \\ bb",
768         format("// aaaaaaaaaa \\ bb", getLLVMStyleWithColumns(ColumnLimit)));
769     EXPECT_EQ(
770         "// aaaaaaaaa\n"
771         "// \\  bb",
772         format("// aaaaaaaaa \\  bb", getLLVMStyleWithColumns(ColumnLimit)));
773     EXPECT_EQ(
774         "// aaaaaaaaa\n"
775         "// \\  \\ bb",
776         format("// aaaaaaaaa \\  \\ bb", getLLVMStyleWithColumns(ColumnLimit)));
777   }
778 }
779 
TEST_F(FormatTestComments,DontSplitLineCommentsWithPragmas)780 TEST_F(FormatTestComments, DontSplitLineCommentsWithPragmas) {
781   FormatStyle Pragmas = getLLVMStyleWithColumns(30);
782   Pragmas.CommentPragmas = "^ IWYU pragma:";
783   EXPECT_EQ(
784       "// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb",
785       format("// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", Pragmas));
786   EXPECT_EQ(
787       "/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */",
788       format("/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", Pragmas));
789 }
790 
TEST_F(FormatTestComments,PriorityOfCommentBreaking)791 TEST_F(FormatTestComments, PriorityOfCommentBreaking) {
792   EXPECT_EQ("if (xxx ==\n"
793             "        yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
794             "    zzz)\n"
795             "  q();",
796             format("if (xxx == yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
797                    "    zzz) q();",
798                    getLLVMStyleWithColumns(40)));
799   EXPECT_EQ("if (xxxxxxxxxx ==\n"
800             "        yyy && // aaaaaa bbbbbbbb cccc\n"
801             "    zzz)\n"
802             "  q();",
803             format("if (xxxxxxxxxx == yyy && // aaaaaa bbbbbbbb cccc\n"
804                    "    zzz) q();",
805                    getLLVMStyleWithColumns(40)));
806   EXPECT_EQ("if (xxxxxxxxxx &&\n"
807             "        yyy || // aaaaaa bbbbbbbb cccc\n"
808             "    zzz)\n"
809             "  q();",
810             format("if (xxxxxxxxxx && yyy || // aaaaaa bbbbbbbb cccc\n"
811                    "    zzz) q();",
812                    getLLVMStyleWithColumns(40)));
813   EXPECT_EQ("fffffffff(\n"
814             "    &xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
815             "    zzz);",
816             format("fffffffff(&xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
817                    " zzz);",
818                    getLLVMStyleWithColumns(40)));
819 }
820 
TEST_F(FormatTestComments,MultiLineCommentsInDefines)821 TEST_F(FormatTestComments, MultiLineCommentsInDefines) {
822   EXPECT_EQ("#define A(x) /* \\\n"
823             "  a comment     \\\n"
824             "  inside */     \\\n"
825             "  f();",
826             format("#define A(x) /* \\\n"
827                    "  a comment     \\\n"
828                    "  inside */     \\\n"
829                    "  f();",
830                    getLLVMStyleWithColumns(17)));
831   EXPECT_EQ("#define A(      \\\n"
832             "    x) /*       \\\n"
833             "  a comment     \\\n"
834             "  inside */     \\\n"
835             "  f();",
836             format("#define A(      \\\n"
837                    "    x) /*       \\\n"
838                    "  a comment     \\\n"
839                    "  inside */     \\\n"
840                    "  f();",
841                    getLLVMStyleWithColumns(17)));
842 }
843 
TEST_F(FormatTestComments,ParsesCommentsAdjacentToPPDirectives)844 TEST_F(FormatTestComments, ParsesCommentsAdjacentToPPDirectives) {
845   EXPECT_EQ("namespace {}\n// Test\n#define A",
846             format("namespace {}\n   // Test\n#define A"));
847   EXPECT_EQ("namespace {}\n/* Test */\n#define A",
848             format("namespace {}\n   /* Test */\n#define A"));
849   EXPECT_EQ("namespace {}\n/* Test */ #define A",
850             format("namespace {}\n   /* Test */    #define A"));
851 }
852 
TEST_F(FormatTestComments,KeepsLevelOfCommentBeforePPDirective)853 TEST_F(FormatTestComments, KeepsLevelOfCommentBeforePPDirective) {
854   // Keep the current level if the comment was originally not aligned with
855   // the preprocessor directive.
856   EXPECT_EQ("void f() {\n"
857             "  int i;\n"
858             "  /* comment */\n"
859             "#ifdef A\n"
860             "  int j;\n"
861             "}",
862             format("void f() {\n"
863                    "  int i;\n"
864                    "  /* comment */\n"
865                    "#ifdef A\n"
866                    "  int j;\n"
867                    "}"));
868 
869   EXPECT_EQ("void f() {\n"
870             "  int i;\n"
871             "  /* comment */\n"
872             "\n"
873             "#ifdef A\n"
874             "  int j;\n"
875             "}",
876             format("void f() {\n"
877                    "  int i;\n"
878                    "  /* comment */\n"
879                    "\n"
880                    "#ifdef A\n"
881                    "  int j;\n"
882                    "}"));
883 
884   EXPECT_EQ("int f(int i) {\n"
885             "  if (true) {\n"
886             "    ++i;\n"
887             "  }\n"
888             "  // comment\n"
889             "#ifdef A\n"
890             "  int j;\n"
891             "#endif\n"
892             "}",
893             format("int f(int i) {\n"
894                    "  if (true) {\n"
895                    "    ++i;\n"
896                    "  }\n"
897                    "  // comment\n"
898                    "#ifdef A\n"
899                    "int j;\n"
900                    "#endif\n"
901                    "}"));
902 
903   EXPECT_EQ("int f(int i) {\n"
904             "  if (true) {\n"
905             "    i++;\n"
906             "  } else {\n"
907             "    // comment in else\n"
908             "#ifdef A\n"
909             "    j++;\n"
910             "#endif\n"
911             "  }\n"
912             "}",
913             format("int f(int i) {\n"
914                    "  if (true) {\n"
915                    "    i++;\n"
916                    "  } else {\n"
917                    "  // comment in else\n"
918                    "#ifdef A\n"
919                    "    j++;\n"
920                    "#endif\n"
921                    "  }\n"
922                    "}"));
923 
924   EXPECT_EQ("int f(int i) {\n"
925             "  if (true) {\n"
926             "    i++;\n"
927             "  } else {\n"
928             "    /* comment in else */\n"
929             "#ifdef A\n"
930             "    j++;\n"
931             "#endif\n"
932             "  }\n"
933             "}",
934             format("int f(int i) {\n"
935                    "  if (true) {\n"
936                    "    i++;\n"
937                    "  } else {\n"
938                    "  /* comment in else */\n"
939                    "#ifdef A\n"
940                    "    j++;\n"
941                    "#endif\n"
942                    "  }\n"
943                    "}"));
944 
945   // Keep the current level if there is an empty line between the comment and
946   // the preprocessor directive.
947   EXPECT_EQ("void f() {\n"
948             "  int i;\n"
949             "  /* comment */\n"
950             "\n"
951             "#ifdef A\n"
952             "  int j;\n"
953             "}",
954             format("void f() {\n"
955                    "  int i;\n"
956                    "/* comment */\n"
957                    "\n"
958                    "#ifdef A\n"
959                    "  int j;\n"
960                    "}"));
961 
962   EXPECT_EQ("void f() {\n"
963             "  int i;\n"
964             "  return i;\n"
965             "}\n"
966             "// comment\n"
967             "\n"
968             "#ifdef A\n"
969             "int i;\n"
970             "#endif // A",
971             format("void f() {\n"
972                    "   int i;\n"
973                    "  return i;\n"
974                    "}\n"
975                    "// comment\n"
976                    "\n"
977                    "#ifdef A\n"
978                    "int i;\n"
979                    "#endif // A"));
980 
981   EXPECT_EQ("int f(int i) {\n"
982             "  if (true) {\n"
983             "    ++i;\n"
984             "  }\n"
985             "  // comment\n"
986             "\n"
987             "#ifdef A\n"
988             "  int j;\n"
989             "#endif\n"
990             "}",
991             format("int f(int i) {\n"
992                    "   if (true) {\n"
993                    "    ++i;\n"
994                    "  }\n"
995                    "  // comment\n"
996                    "\n"
997                    "#ifdef A\n"
998                    "  int j;\n"
999                    "#endif\n"
1000                    "}"));
1001 
1002   EXPECT_EQ("int f(int i) {\n"
1003             "  if (true) {\n"
1004             "    i++;\n"
1005             "  } else {\n"
1006             "    // comment in else\n"
1007             "\n"
1008             "#ifdef A\n"
1009             "    j++;\n"
1010             "#endif\n"
1011             "  }\n"
1012             "}",
1013             format("int f(int i) {\n"
1014                    "  if (true) {\n"
1015                    "    i++;\n"
1016                    "  } else {\n"
1017                    "// comment in else\n"
1018                    "\n"
1019                    "#ifdef A\n"
1020                    "    j++;\n"
1021                    "#endif\n"
1022                    "  }\n"
1023                    "}"));
1024 
1025   EXPECT_EQ("int f(int i) {\n"
1026             "  if (true) {\n"
1027             "    i++;\n"
1028             "  } else {\n"
1029             "    /* comment in else */\n"
1030             "\n"
1031             "#ifdef A\n"
1032             "    j++;\n"
1033             "#endif\n"
1034             "  }\n"
1035             "}",
1036             format("int f(int i) {\n"
1037                    "  if (true) {\n"
1038                    "    i++;\n"
1039                    "  } else {\n"
1040                    "/* comment in else */\n"
1041                    "\n"
1042                    "#ifdef A\n"
1043                    "    j++;\n"
1044                    "#endif\n"
1045                    "  }\n"
1046                    "}"));
1047 
1048   // Align with the preprocessor directive if the comment was originally aligned
1049   // with the preprocessor directive and there is no newline between the comment
1050   // and the preprocessor directive.
1051   EXPECT_EQ("void f() {\n"
1052             "  int i;\n"
1053             "/* comment */\n"
1054             "#ifdef A\n"
1055             "  int j;\n"
1056             "}",
1057             format("void f() {\n"
1058                    "  int i;\n"
1059                    "/* comment */\n"
1060                    "#ifdef A\n"
1061                    "  int j;\n"
1062                    "}"));
1063 
1064   EXPECT_EQ("int f(int i) {\n"
1065             "  if (true) {\n"
1066             "    ++i;\n"
1067             "  }\n"
1068             "// comment\n"
1069             "#ifdef A\n"
1070             "  int j;\n"
1071             "#endif\n"
1072             "}",
1073             format("int f(int i) {\n"
1074                    "   if (true) {\n"
1075                    "    ++i;\n"
1076                    "  }\n"
1077                    "// comment\n"
1078                    "#ifdef A\n"
1079                    "  int j;\n"
1080                    "#endif\n"
1081                    "}"));
1082 
1083   EXPECT_EQ("int f(int i) {\n"
1084             "  if (true) {\n"
1085             "    i++;\n"
1086             "  } else {\n"
1087             "// comment in else\n"
1088             "#ifdef A\n"
1089             "    j++;\n"
1090             "#endif\n"
1091             "  }\n"
1092             "}",
1093             format("int f(int i) {\n"
1094                    "  if (true) {\n"
1095                    "    i++;\n"
1096                    "  } else {\n"
1097                    " // comment in else\n"
1098                    " #ifdef A\n"
1099                    "    j++;\n"
1100                    "#endif\n"
1101                    "  }\n"
1102                    "}"));
1103 
1104   EXPECT_EQ("int f(int i) {\n"
1105             "  if (true) {\n"
1106             "    i++;\n"
1107             "  } else {\n"
1108             "/* comment in else */\n"
1109             "#ifdef A\n"
1110             "    j++;\n"
1111             "#endif\n"
1112             "  }\n"
1113             "}",
1114             format("int f(int i) {\n"
1115                    "  if (true) {\n"
1116                    "    i++;\n"
1117                    "  } else {\n"
1118                    " /* comment in else */\n"
1119                    " #ifdef A\n"
1120                    "    j++;\n"
1121                    "#endif\n"
1122                    "  }\n"
1123                    "}"));
1124 }
1125 
TEST_F(FormatTestComments,SplitsLongLinesInComments)1126 TEST_F(FormatTestComments, SplitsLongLinesInComments) {
1127   // FIXME: Do we need to fix up the "  */" at the end?
1128   // It doesn't look like any of our current logic triggers this.
1129   EXPECT_EQ("/* This is a long\n"
1130             " * comment that\n"
1131             " * doesn't fit on\n"
1132             " * one line.  */",
1133             format("/* "
1134                    "This is a long                                         "
1135                    "comment that "
1136                    "doesn't                                    "
1137                    "fit on one line.  */",
1138                    getLLVMStyleWithColumns(20)));
1139   EXPECT_EQ(
1140       "/* a b c d\n"
1141       " * e f  g\n"
1142       " * h i j k\n"
1143       " */",
1144       format("/* a b c d e f  g h i j k */", getLLVMStyleWithColumns(10)));
1145   EXPECT_EQ(
1146       "/* a b c d\n"
1147       " * e f  g\n"
1148       " * h i j k\n"
1149       " */",
1150       format("\\\n/* a b c d e f  g h i j k */", getLLVMStyleWithColumns(10)));
1151   EXPECT_EQ("/*\n"
1152             "This is a long\n"
1153             "comment that doesn't\n"
1154             "fit on one line.\n"
1155             "*/",
1156             format("/*\n"
1157                    "This is a long                                         "
1158                    "comment that doesn't                                    "
1159                    "fit on one line.                                      \n"
1160                    "*/",
1161                    getLLVMStyleWithColumns(20)));
1162   EXPECT_EQ("/*\n"
1163             " * This is a long\n"
1164             " * comment that\n"
1165             " * doesn't fit on\n"
1166             " * one line.\n"
1167             " */",
1168             format("/*      \n"
1169                    " * This is a long "
1170                    "   comment that     "
1171                    "   doesn't fit on   "
1172                    "   one line.                                            \n"
1173                    " */",
1174                    getLLVMStyleWithColumns(20)));
1175   EXPECT_EQ("/*\n"
1176             " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n"
1177             " * so_it_should_be_broken\n"
1178             " * wherever_a_space_occurs\n"
1179             " */",
1180             format("/*\n"
1181                    " * This_is_a_comment_with_words_that_dont_fit_on_one_line "
1182                    "   so_it_should_be_broken "
1183                    "   wherever_a_space_occurs                             \n"
1184                    " */",
1185                    getLLVMStyleWithColumns(20)));
1186   EXPECT_EQ("/*\n"
1187             " *    This_comment_can_not_be_broken_into_lines\n"
1188             " */",
1189             format("/*\n"
1190                    " *    This_comment_can_not_be_broken_into_lines\n"
1191                    " */",
1192                    getLLVMStyleWithColumns(20)));
1193   EXPECT_EQ("{\n"
1194             "  /*\n"
1195             "  This is another\n"
1196             "  long comment that\n"
1197             "  doesn't fit on one\n"
1198             "  line    1234567890\n"
1199             "  */\n"
1200             "}",
1201             format("{\n"
1202                    "/*\n"
1203                    "This is another     "
1204                    "  long comment that "
1205                    "  doesn't fit on one"
1206                    "  line    1234567890\n"
1207                    "*/\n"
1208                    "}",
1209                    getLLVMStyleWithColumns(20)));
1210   EXPECT_EQ("{\n"
1211             "  /*\n"
1212             "   * This        i s\n"
1213             "   * another comment\n"
1214             "   * t hat  doesn' t\n"
1215             "   * fit on one l i\n"
1216             "   * n e\n"
1217             "   */\n"
1218             "}",
1219             format("{\n"
1220                    "/*\n"
1221                    " * This        i s"
1222                    "   another comment"
1223                    "   t hat  doesn' t"
1224                    "   fit on one l i"
1225                    "   n e\n"
1226                    " */\n"
1227                    "}",
1228                    getLLVMStyleWithColumns(20)));
1229   EXPECT_EQ("/*\n"
1230             " * This is a long\n"
1231             " * comment that\n"
1232             " * doesn't fit on\n"
1233             " * one line\n"
1234             " */",
1235             format("   /*\n"
1236                    "    * This is a long comment that doesn't fit on one line\n"
1237                    "    */",
1238                    getLLVMStyleWithColumns(20)));
1239   EXPECT_EQ("{\n"
1240             "  if (something) /* This is a\n"
1241             "                    long\n"
1242             "                    comment */\n"
1243             "    ;\n"
1244             "}",
1245             format("{\n"
1246                    "  if (something) /* This is a long comment */\n"
1247                    "    ;\n"
1248                    "}",
1249                    getLLVMStyleWithColumns(30)));
1250 
1251   EXPECT_EQ("/* A comment before\n"
1252             " * a macro\n"
1253             " * definition */\n"
1254             "#define a b",
1255             format("/* A comment before a macro definition */\n"
1256                    "#define a b",
1257                    getLLVMStyleWithColumns(20)));
1258 
1259   EXPECT_EQ("/* some comment\n"
1260             " *   a comment that\n"
1261             " * we break another\n"
1262             " * comment we have\n"
1263             " * to break a left\n"
1264             " * comment\n"
1265             " */",
1266             format("  /* some comment\n"
1267                    "       *   a comment that we break\n"
1268                    "   * another comment we have to break\n"
1269                    "* a left comment\n"
1270                    "   */",
1271                    getLLVMStyleWithColumns(20)));
1272 
1273   EXPECT_EQ("/**\n"
1274             " * multiline block\n"
1275             " * comment\n"
1276             " *\n"
1277             " */",
1278             format("/**\n"
1279                    " * multiline block comment\n"
1280                    " *\n"
1281                    " */",
1282                    getLLVMStyleWithColumns(20)));
1283 
1284   // This reproduces a crashing bug where both adaptStartOfLine and
1285   // getCommentSplit were trying to wrap after the "/**".
1286   EXPECT_EQ("/** multilineblockcommentwithnowrapopportunity */",
1287             format("/** multilineblockcommentwithnowrapopportunity */",
1288                    getLLVMStyleWithColumns(20)));
1289 
1290   EXPECT_EQ("/*\n"
1291             "\n"
1292             "\n"
1293             "    */\n",
1294             format("  /*       \n"
1295                    "      \n"
1296                    "               \n"
1297                    "      */\n"));
1298 
1299   EXPECT_EQ("/* a a */",
1300             format("/* a a            */", getLLVMStyleWithColumns(15)));
1301   EXPECT_EQ("/* a a bc  */",
1302             format("/* a a            bc  */", getLLVMStyleWithColumns(15)));
1303   EXPECT_EQ("/* aaa aaa\n"
1304             " * aaaaa */",
1305             format("/* aaa aaa aaaaa       */", getLLVMStyleWithColumns(15)));
1306   EXPECT_EQ("/* aaa aaa\n"
1307             " * aaaaa     */",
1308             format("/* aaa aaa aaaaa     */", getLLVMStyleWithColumns(15)));
1309 }
1310 
TEST_F(FormatTestComments,SplitsLongLinesInCommentsInPreprocessor)1311 TEST_F(FormatTestComments, SplitsLongLinesInCommentsInPreprocessor) {
1312   EXPECT_EQ("#define X          \\\n"
1313             "  /*               \\\n"
1314             "   Test            \\\n"
1315             "   Macro comment   \\\n"
1316             "   with a long     \\\n"
1317             "   line            \\\n"
1318             "   */              \\\n"
1319             "  A + B",
1320             format("#define X \\\n"
1321                    "  /*\n"
1322                    "   Test\n"
1323                    "   Macro comment with a long  line\n"
1324                    "   */ \\\n"
1325                    "  A + B",
1326                    getLLVMStyleWithColumns(20)));
1327   EXPECT_EQ("#define X          \\\n"
1328             "  /* Macro comment \\\n"
1329             "     with a long   \\\n"
1330             "     line */       \\\n"
1331             "  A + B",
1332             format("#define X \\\n"
1333                    "  /* Macro comment with a long\n"
1334                    "     line */ \\\n"
1335                    "  A + B",
1336                    getLLVMStyleWithColumns(20)));
1337   EXPECT_EQ("#define X          \\\n"
1338             "  /* Macro comment \\\n"
1339             "   * with a long   \\\n"
1340             "   * line */       \\\n"
1341             "  A + B",
1342             format("#define X \\\n"
1343                    "  /* Macro comment with a long  line */ \\\n"
1344                    "  A + B",
1345                    getLLVMStyleWithColumns(20)));
1346 }
1347 
TEST_F(FormatTestComments,KeepsTrailingPPCommentsAndSectionCommentsSeparate)1348 TEST_F(FormatTestComments, KeepsTrailingPPCommentsAndSectionCommentsSeparate) {
1349   verifyFormat("#ifdef A // line about A\n"
1350                "// section comment\n"
1351                "#endif",
1352                getLLVMStyleWithColumns(80));
1353   verifyFormat("#ifdef A // line 1 about A\n"
1354                "         // line 2 about A\n"
1355                "// section comment\n"
1356                "#endif",
1357                getLLVMStyleWithColumns(80));
1358   EXPECT_EQ("#ifdef A // line 1 about A\n"
1359             "         // line 2 about A\n"
1360             "// section comment\n"
1361             "#endif",
1362             format("#ifdef A // line 1 about A\n"
1363                    "          // line 2 about A\n"
1364                    "// section comment\n"
1365                    "#endif",
1366                    getLLVMStyleWithColumns(80)));
1367   verifyFormat("int f() {\n"
1368                "  int i;\n"
1369                "#ifdef A // comment about A\n"
1370                "  // section comment 1\n"
1371                "  // section comment 2\n"
1372                "  i = 2;\n"
1373                "#else // comment about #else\n"
1374                "  // section comment 3\n"
1375                "  i = 4;\n"
1376                "#endif\n"
1377                "}",
1378                getLLVMStyleWithColumns(80));
1379 }
1380 
TEST_F(FormatTestComments,AlignsPPElseEndifComments)1381 TEST_F(FormatTestComments, AlignsPPElseEndifComments) {
1382   verifyFormat("#if A\n"
1383                "#else  // A\n"
1384                "int iiii;\n"
1385                "#endif // B",
1386                getLLVMStyleWithColumns(20));
1387   verifyFormat("#if A\n"
1388                "#else  // A\n"
1389                "int iiii; // CC\n"
1390                "#endif // B",
1391                getLLVMStyleWithColumns(20));
1392   EXPECT_EQ("#if A\n"
1393             "#else  // A1\n"
1394             "       // A2\n"
1395             "int ii;\n"
1396             "#endif // B",
1397             format("#if A\n"
1398                    "#else  // A1\n"
1399                    "       // A2\n"
1400                    "int ii;\n"
1401                    "#endif // B",
1402                    getLLVMStyleWithColumns(20)));
1403 }
1404 
TEST_F(FormatTestComments,CommentsInStaticInitializers)1405 TEST_F(FormatTestComments, CommentsInStaticInitializers) {
1406   EXPECT_EQ(
1407       "static SomeType type = {aaaaaaaaaaaaaaaaaaaa, /* comment */\n"
1408       "                        aaaaaaaaaaaaaaaaaaaa /* comment */,\n"
1409       "                        /* comment */ aaaaaaaaaaaaaaaaaaaa,\n"
1410       "                        aaaaaaaaaaaaaaaaaaaa, // comment\n"
1411       "                        aaaaaaaaaaaaaaaaaaaa};",
1412       format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa  ,  /* comment */\n"
1413              "                   aaaaaaaaaaaaaaaaaaaa   /* comment */ ,\n"
1414              "                     /* comment */   aaaaaaaaaaaaaaaaaaaa ,\n"
1415              "              aaaaaaaaaaaaaaaaaaaa ,   // comment\n"
1416              "                  aaaaaaaaaaaaaaaaaaaa };"));
1417   verifyFormat("static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
1418                "                        bbbbbbbbbbb, ccccccccccc};");
1419   verifyFormat("static SomeType type = {aaaaaaaaaaa,\n"
1420                "                        // comment for bb....\n"
1421                "                        bbbbbbbbbbb, ccccccccccc};");
1422   verifyGoogleFormat(
1423       "static SomeType type = {aaaaaaaaaaa,  // comment for aa...\n"
1424       "                        bbbbbbbbbbb, ccccccccccc};");
1425   verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n"
1426                      "                        // comment for bb....\n"
1427                      "                        bbbbbbbbbbb, ccccccccccc};");
1428 
1429   verifyFormat("S s = {{a, b, c},  // Group #1\n"
1430                "       {d, e, f},  // Group #2\n"
1431                "       {g, h, i}}; // Group #3");
1432   verifyFormat("S s = {{// Group #1\n"
1433                "        a, b, c},\n"
1434                "       {// Group #2\n"
1435                "        d, e, f},\n"
1436                "       {// Group #3\n"
1437                "        g, h, i}};");
1438 
1439   EXPECT_EQ("S s = {\n"
1440             "    // Some comment\n"
1441             "    a,\n"
1442             "\n"
1443             "    // Comment after empty line\n"
1444             "    b}",
1445             format("S s =    {\n"
1446                    "      // Some comment\n"
1447                    "  a,\n"
1448                    "  \n"
1449                    "     // Comment after empty line\n"
1450                    "      b\n"
1451                    "}"));
1452   EXPECT_EQ("S s = {\n"
1453             "    /* Some comment */\n"
1454             "    a,\n"
1455             "\n"
1456             "    /* Comment after empty line */\n"
1457             "    b}",
1458             format("S s =    {\n"
1459                    "      /* Some comment */\n"
1460                    "  a,\n"
1461                    "  \n"
1462                    "     /* Comment after empty line */\n"
1463                    "      b\n"
1464                    "}"));
1465   verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n"
1466                "    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
1467                "    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
1468                "    0x00, 0x00, 0x00, 0x00};            // comment\n");
1469 }
1470 
TEST_F(FormatTestComments,LineCommentsAfterRightBrace)1471 TEST_F(FormatTestComments, LineCommentsAfterRightBrace) {
1472   EXPECT_EQ("if (true) { // comment about branch\n"
1473             "  // comment about f\n"
1474             "  f();\n"
1475             "}",
1476             format("if (true) { // comment about branch\n"
1477                    "  // comment about f\n"
1478                    "  f();\n"
1479                    "}",
1480                    getLLVMStyleWithColumns(80)));
1481   EXPECT_EQ("if (1) { // if line 1\n"
1482             "         // if line 2\n"
1483             "         // if line 3\n"
1484             "  // f line 1\n"
1485             "  // f line 2\n"
1486             "  f();\n"
1487             "} else { // else line 1\n"
1488             "         // else line 2\n"
1489             "         // else line 3\n"
1490             "  // g line 1\n"
1491             "  g();\n"
1492             "}",
1493             format("if (1) { // if line 1\n"
1494                    "          // if line 2\n"
1495                    "        // if line 3\n"
1496                    "  // f line 1\n"
1497                    "    // f line 2\n"
1498                    "  f();\n"
1499                    "} else { // else line 1\n"
1500                    "        // else line 2\n"
1501                    "         // else line 3\n"
1502                    "  // g line 1\n"
1503                    "  g();\n"
1504                    "}"));
1505   EXPECT_EQ("do { // line 1\n"
1506             "     // line 2\n"
1507             "     // line 3\n"
1508             "  f();\n"
1509             "} while (true);",
1510             format("do { // line 1\n"
1511                    "     // line 2\n"
1512                    "   // line 3\n"
1513                    "  f();\n"
1514                    "} while (true);",
1515                    getLLVMStyleWithColumns(80)));
1516   EXPECT_EQ("while (a < b) { // line 1\n"
1517             "  // line 2\n"
1518             "  // line 3\n"
1519             "  f();\n"
1520             "}",
1521             format("while (a < b) {// line 1\n"
1522                    "  // line 2\n"
1523                    "  // line 3\n"
1524                    "  f();\n"
1525                    "}",
1526                    getLLVMStyleWithColumns(80)));
1527 }
1528 
TEST_F(FormatTestComments,ReflowsComments)1529 TEST_F(FormatTestComments, ReflowsComments) {
1530   // Break a long line and reflow with the full next line.
1531   EXPECT_EQ("// long long long\n"
1532             "// long long",
1533             format("// long long long long\n"
1534                    "// long",
1535                    getLLVMStyleWithColumns(20)));
1536 
1537   // Keep the trailing newline while reflowing.
1538   EXPECT_EQ("// long long long\n"
1539             "// long long\n",
1540             format("// long long long long\n"
1541                    "// long\n",
1542                    getLLVMStyleWithColumns(20)));
1543 
1544   // Break a long line and reflow with a part of the next line.
1545   EXPECT_EQ("// long long long\n"
1546             "// long long\n"
1547             "// long_long",
1548             format("// long long long long\n"
1549                    "// long long_long",
1550                    getLLVMStyleWithColumns(20)));
1551 
1552   // Break but do not reflow if the first word from the next line is too long.
1553   EXPECT_EQ("// long long long\n"
1554             "// long\n"
1555             "// long_long_long\n",
1556             format("// long long long long\n"
1557                    "// long_long_long\n",
1558                    getLLVMStyleWithColumns(20)));
1559 
1560   // Don't break or reflow short lines.
1561   verifyFormat("// long\n"
1562                "// long long long lo\n"
1563                "// long long long lo\n"
1564                "// long",
1565                getLLVMStyleWithColumns(20));
1566 
1567   // Keep prefixes and decorations while reflowing.
1568   EXPECT_EQ("/// long long long\n"
1569             "/// long long\n",
1570             format("/// long long long long\n"
1571                    "/// long\n",
1572                    getLLVMStyleWithColumns(20)));
1573   EXPECT_EQ("//! long long long\n"
1574             "//! long long\n",
1575             format("//! long long long long\n"
1576                    "//! long\n",
1577                    getLLVMStyleWithColumns(20)));
1578   EXPECT_EQ("/* long long long\n"
1579             " * long long */",
1580             format("/* long long long long\n"
1581                    " * long */",
1582                    getLLVMStyleWithColumns(20)));
1583   EXPECT_EQ("///< long long long\n"
1584             "///< long long\n",
1585             format("///< long long long long\n"
1586                    "///< long\n",
1587                    getLLVMStyleWithColumns(20)));
1588   EXPECT_EQ("//!< long long long\n"
1589             "//!< long long\n",
1590             format("//!< long long long long\n"
1591                    "//!< long\n",
1592                    getLLVMStyleWithColumns(20)));
1593 
1594   // Don't bring leading whitespace up while reflowing.
1595   EXPECT_EQ("/*  long long long\n"
1596             " * long long long\n"
1597             " */",
1598             format("/*  long long long long\n"
1599                    " *  long long\n"
1600                    " */",
1601                    getLLVMStyleWithColumns(20)));
1602 
1603   // Reflow the last line of a block comment with its trailing '*/'.
1604   EXPECT_EQ("/* long long long\n"
1605             "   long long */",
1606             format("/* long long long long\n"
1607                    "   long */",
1608                    getLLVMStyleWithColumns(20)));
1609 
1610   // Reflow two short lines; keep the postfix of the last one.
1611   EXPECT_EQ("/* long long long\n"
1612             " * long long long */",
1613             format("/* long long long long\n"
1614                    " * long\n"
1615                    " * long */",
1616                    getLLVMStyleWithColumns(20)));
1617 
1618   // Put the postfix of the last short reflow line on a newline if it doesn't
1619   // fit.
1620   EXPECT_EQ("/* long long long\n"
1621             " * long long longg\n"
1622             " */",
1623             format("/* long long long long\n"
1624                    " * long\n"
1625                    " * longg */",
1626                    getLLVMStyleWithColumns(20)));
1627 
1628   // Reflow lines with leading whitespace.
1629   EXPECT_EQ("{\n"
1630             "  /*\n"
1631             "   * long long long\n"
1632             "   * long long long\n"
1633             "   * long long long\n"
1634             "   */\n"
1635             "}",
1636             format("{\n"
1637                    "/*\n"
1638                    " * long long long long\n"
1639                    " *   long\n"
1640                    " * long long long long\n"
1641                    " */\n"
1642                    "}",
1643                    getLLVMStyleWithColumns(20)));
1644 
1645   // Break single line block comments that are first in the line with ' *'
1646   // decoration.
1647   EXPECT_EQ("/* long long long\n"
1648             " * long */",
1649             format("/* long long long long */", getLLVMStyleWithColumns(20)));
1650 
1651   // Break single line block comment that are not first in the line with '  '
1652   // decoration.
1653   EXPECT_EQ("int i; /* long long\n"
1654             "          long */",
1655             format("int i; /* long long long */", getLLVMStyleWithColumns(20)));
1656 
1657   // Reflow a line that goes just over the column limit.
1658   EXPECT_EQ("// long long long\n"
1659             "// lon long",
1660             format("// long long long lon\n"
1661                    "// long",
1662                    getLLVMStyleWithColumns(20)));
1663 
1664   // Stop reflowing if the next line has a different indentation than the
1665   // previous line.
1666   EXPECT_EQ("// long long long\n"
1667             "// long\n"
1668             "//  long long\n"
1669             "//  long",
1670             format("// long long long long\n"
1671                    "//  long long\n"
1672                    "//  long",
1673                    getLLVMStyleWithColumns(20)));
1674 
1675   // Reflow into the last part of a really long line that has been broken into
1676   // multiple lines.
1677   EXPECT_EQ("// long long long\n"
1678             "// long long long\n"
1679             "// long long long\n",
1680             format("// long long long long long long long long\n"
1681                    "// long\n",
1682                    getLLVMStyleWithColumns(20)));
1683 
1684   // Break the first line, then reflow the beginning of the second and third
1685   // line up.
1686   EXPECT_EQ("// long long long\n"
1687             "// lon1 lon2 lon2\n"
1688             "// lon2 lon3 lon3",
1689             format("// long long long lon1\n"
1690                    "// lon2 lon2 lon2\n"
1691                    "// lon3 lon3",
1692                    getLLVMStyleWithColumns(20)));
1693 
1694   // Reflow the beginning of the second line, then break the rest.
1695   EXPECT_EQ("// long long long\n"
1696             "// lon1 lon2 lon2\n"
1697             "// lon2 lon2 lon2\n"
1698             "// lon3",
1699             format("// long long long lon1\n"
1700                    "// lon2 lon2 lon2 lon2 lon2 lon3",
1701                    getLLVMStyleWithColumns(20)));
1702 
1703   // Shrink the first line, then reflow the second line up.
1704   EXPECT_EQ("// long long long", format("// long              long\n"
1705                                         "// long",
1706                                         getLLVMStyleWithColumns(20)));
1707 
1708   // Don't shrink leading whitespace.
1709   EXPECT_EQ("int i; ///           a",
1710             format("int i; ///           a", getLLVMStyleWithColumns(20)));
1711 
1712   // Shrink trailing whitespace if there is no postfix and reflow.
1713   EXPECT_EQ("// long long long\n"
1714             "// long long",
1715             format("// long long long long    \n"
1716                    "// long",
1717                    getLLVMStyleWithColumns(20)));
1718 
1719   // Shrink trailing whitespace to a single one if there is postfix.
1720   EXPECT_EQ("/* long long long */",
1721             format("/* long long long     */", getLLVMStyleWithColumns(20)));
1722 
1723   // Break a block comment postfix if exceeding the line limit.
1724   EXPECT_EQ("/*               long\n"
1725             " */",
1726             format("/*               long */", getLLVMStyleWithColumns(20)));
1727 
1728   // Reflow indented comments.
1729   EXPECT_EQ("{\n"
1730             "  // long long long\n"
1731             "  // long long\n"
1732             "  int i; /* long lon\n"
1733             "            g long\n"
1734             "          */\n"
1735             "}",
1736             format("{\n"
1737                    "  // long long long long\n"
1738                    "  // long\n"
1739                    "  int i; /* long lon g\n"
1740                    "            long */\n"
1741                    "}",
1742                    getLLVMStyleWithColumns(20)));
1743 
1744   // Don't realign trailing comments after reflow has happened.
1745   EXPECT_EQ("// long long long\n"
1746             "// long long\n"
1747             "long i; // long",
1748             format("// long long long long\n"
1749                    "// long\n"
1750                    "long i; // long",
1751                    getLLVMStyleWithColumns(20)));
1752   EXPECT_EQ("// long long long\n"
1753             "// longng long long\n"
1754             "// long lo",
1755             format("// long long long longng\n"
1756                    "// long long long\n"
1757                    "// lo",
1758                    getLLVMStyleWithColumns(20)));
1759 
1760   // Reflow lines after a broken line.
1761   EXPECT_EQ("int a; // Trailing\n"
1762             "       // comment on\n"
1763             "       // 2 or 3\n"
1764             "       // lines.\n",
1765             format("int a; // Trailing comment\n"
1766                    "       // on 2\n"
1767                    "       // or 3\n"
1768                    "       // lines.\n",
1769                    getLLVMStyleWithColumns(20)));
1770   EXPECT_EQ("/// This long line\n"
1771             "/// gets reflown.\n",
1772             format("/// This long line gets\n"
1773                    "/// reflown.\n",
1774                    getLLVMStyleWithColumns(20)));
1775   EXPECT_EQ("//! This long line\n"
1776             "//! gets reflown.\n",
1777             format(" //! This long line gets\n"
1778                    " //! reflown.\n",
1779                    getLLVMStyleWithColumns(20)));
1780   EXPECT_EQ("/* This long line\n"
1781             " * gets reflown.\n"
1782             " */\n",
1783             format("/* This long line gets\n"
1784                    " * reflown.\n"
1785                    " */\n",
1786                    getLLVMStyleWithColumns(20)));
1787 
1788   // Reflow after indentation makes a line too long.
1789   EXPECT_EQ("{\n"
1790             "  // long long long\n"
1791             "  // lo long\n"
1792             "}\n",
1793             format("{\n"
1794                    "// long long long lo\n"
1795                    "// long\n"
1796                    "}\n",
1797                    getLLVMStyleWithColumns(20)));
1798 
1799   // Break and reflow multiple lines.
1800   EXPECT_EQ("/*\n"
1801             " * Reflow the end of\n"
1802             " * line by 11 22 33\n"
1803             " * 4.\n"
1804             " */\n",
1805             format("/*\n"
1806                    " * Reflow the end of line\n"
1807                    " * by\n"
1808                    " * 11\n"
1809                    " * 22\n"
1810                    " * 33\n"
1811                    " * 4.\n"
1812                    " */\n",
1813                    getLLVMStyleWithColumns(20)));
1814   EXPECT_EQ("/// First line gets\n"
1815             "/// broken. Second\n"
1816             "/// line gets\n"
1817             "/// reflown and\n"
1818             "/// broken. Third\n"
1819             "/// gets reflown.\n",
1820             format("/// First line gets broken.\n"
1821                    "/// Second line gets reflown and broken.\n"
1822                    "/// Third gets reflown.\n",
1823                    getLLVMStyleWithColumns(20)));
1824   EXPECT_EQ("int i; // first long\n"
1825             "       // long snd\n"
1826             "       // long.\n",
1827             format("int i; // first long long\n"
1828                    "       // snd long.\n",
1829                    getLLVMStyleWithColumns(20)));
1830   EXPECT_EQ("{\n"
1831             "  // first long line\n"
1832             "  // line second\n"
1833             "  // long line line\n"
1834             "  // third long line\n"
1835             "  // line\n"
1836             "}\n",
1837             format("{\n"
1838                    "  // first long line line\n"
1839                    "  // second long line line\n"
1840                    "  // third long line line\n"
1841                    "}\n",
1842                    getLLVMStyleWithColumns(20)));
1843   EXPECT_EQ("int i; /* first line\n"
1844             "        * second\n"
1845             "        * line third\n"
1846             "        * line\n"
1847             "        */",
1848             format("int i; /* first line\n"
1849                    "        * second line\n"
1850                    "        * third line\n"
1851                    "        */",
1852                    getLLVMStyleWithColumns(20)));
1853 
1854   // Reflow the last two lines of a section that starts with a line having
1855   // different indentation.
1856   EXPECT_EQ("//     long\n"
1857             "// long long long\n"
1858             "// long long",
1859             format("//     long\n"
1860                    "// long long long long\n"
1861                    "// long",
1862                    getLLVMStyleWithColumns(20)));
1863 
1864   // Keep the block comment endling '*/' while reflowing.
1865   EXPECT_EQ("/* Long long long\n"
1866             " * line short */\n",
1867             format("/* Long long long line\n"
1868                    " * short */\n",
1869                    getLLVMStyleWithColumns(20)));
1870 
1871   // Don't reflow between separate blocks of comments.
1872   EXPECT_EQ("/* First comment\n"
1873             " * block will */\n"
1874             "/* Snd\n"
1875             " */\n",
1876             format("/* First comment block\n"
1877                    " * will */\n"
1878                    "/* Snd\n"
1879                    " */\n",
1880                    getLLVMStyleWithColumns(20)));
1881 
1882   // Don't reflow across blank comment lines.
1883   EXPECT_EQ("int i; // This long\n"
1884             "       // line gets\n"
1885             "       // broken.\n"
1886             "       //\n"
1887             "       // keep.\n",
1888             format("int i; // This long line gets broken.\n"
1889                    "       //  \n"
1890                    "       // keep.\n",
1891                    getLLVMStyleWithColumns(20)));
1892   EXPECT_EQ("{\n"
1893             "  /// long long long\n"
1894             "  /// long long\n"
1895             "  ///\n"
1896             "  /// long\n"
1897             "}",
1898             format("{\n"
1899                    "  /// long long long long\n"
1900                    "  /// long\n"
1901                    "  ///\n"
1902                    "  /// long\n"
1903                    "}",
1904                    getLLVMStyleWithColumns(20)));
1905   EXPECT_EQ("//! long long long\n"
1906             "//! long\n"
1907             "\n"
1908             "//! long",
1909             format("//! long long long long\n"
1910                    "\n"
1911                    "//! long",
1912                    getLLVMStyleWithColumns(20)));
1913   EXPECT_EQ("/* long long long\n"
1914             "   long\n"
1915             "\n"
1916             "   long */",
1917             format("/* long long long long\n"
1918                    "\n"
1919                    "   long */",
1920                    getLLVMStyleWithColumns(20)));
1921   EXPECT_EQ("/* long long long\n"
1922             " * long\n"
1923             " *\n"
1924             " * long */",
1925             format("/* long long long long\n"
1926                    " *\n"
1927                    " * long */",
1928                    getLLVMStyleWithColumns(20)));
1929 
1930   // Don't reflow lines having content that is a single character.
1931   EXPECT_EQ("// long long long\n"
1932             "// long\n"
1933             "// l",
1934             format("// long long long long\n"
1935                    "// l",
1936                    getLLVMStyleWithColumns(20)));
1937 
1938   // Don't reflow lines starting with two punctuation characters.
1939   EXPECT_EQ("// long long long\n"
1940             "// long\n"
1941             "// ... --- ...",
1942             format("// long long long long\n"
1943                    "// ... --- ...",
1944                    getLLVMStyleWithColumns(20)));
1945 
1946   // Don't reflow lines starting with '@'.
1947   EXPECT_EQ("// long long long\n"
1948             "// long\n"
1949             "// @param arg",
1950             format("// long long long long\n"
1951                    "// @param arg",
1952                    getLLVMStyleWithColumns(20)));
1953 
1954   // Don't reflow lines starting with 'TODO'.
1955   EXPECT_EQ("// long long long\n"
1956             "// long\n"
1957             "// TODO: long",
1958             format("// long long long long\n"
1959                    "// TODO: long",
1960                    getLLVMStyleWithColumns(20)));
1961 
1962   // Don't reflow lines starting with 'FIXME'.
1963   EXPECT_EQ("// long long long\n"
1964             "// long\n"
1965             "// FIXME: long",
1966             format("// long long long long\n"
1967                    "// FIXME: long",
1968                    getLLVMStyleWithColumns(20)));
1969 
1970   // Don't reflow lines starting with 'XXX'.
1971   EXPECT_EQ("// long long long\n"
1972             "// long\n"
1973             "// XXX: long",
1974             format("// long long long long\n"
1975                    "// XXX: long",
1976                    getLLVMStyleWithColumns(20)));
1977 
1978   // Don't reflow comment pragmas.
1979   EXPECT_EQ("// long long long\n"
1980             "// long\n"
1981             "// IWYU pragma:",
1982             format("// long long long long\n"
1983                    "// IWYU pragma:",
1984                    getLLVMStyleWithColumns(20)));
1985   EXPECT_EQ("/* long long long\n"
1986             " * long\n"
1987             " * IWYU pragma:\n"
1988             " */",
1989             format("/* long long long long\n"
1990                    " * IWYU pragma:\n"
1991                    " */",
1992                    getLLVMStyleWithColumns(20)));
1993 
1994   // Reflow lines that have a non-punctuation character among their first 2
1995   // characters.
1996   EXPECT_EQ("// long long long\n"
1997             "// long 'long'",
1998             format("// long long long long\n"
1999                    "// 'long'",
2000                    getLLVMStyleWithColumns(20)));
2001 
2002   // Don't reflow between separate blocks of comments.
2003   EXPECT_EQ("/* First comment\n"
2004             " * block will */\n"
2005             "/* Snd\n"
2006             " */\n",
2007             format("/* First comment block\n"
2008                    " * will */\n"
2009                    "/* Snd\n"
2010                    " */\n",
2011                    getLLVMStyleWithColumns(20)));
2012 
2013   // Don't reflow lines having different indentation.
2014   EXPECT_EQ("// long long long\n"
2015             "// long\n"
2016             "//  long",
2017             format("// long long long long\n"
2018                    "//  long",
2019                    getLLVMStyleWithColumns(20)));
2020 
2021   // Don't reflow separate bullets in list
2022   EXPECT_EQ("// - long long long\n"
2023             "// long\n"
2024             "// - long",
2025             format("// - long long long long\n"
2026                    "// - long",
2027                    getLLVMStyleWithColumns(20)));
2028   EXPECT_EQ("// * long long long\n"
2029             "// long\n"
2030             "// * long",
2031             format("// * long long long long\n"
2032                    "// * long",
2033                    getLLVMStyleWithColumns(20)));
2034   EXPECT_EQ("// + long long long\n"
2035             "// long\n"
2036             "// + long",
2037             format("// + long long long long\n"
2038                    "// + long",
2039                    getLLVMStyleWithColumns(20)));
2040   EXPECT_EQ("// 1. long long long\n"
2041             "// long\n"
2042             "// 2. long",
2043             format("// 1. long long long long\n"
2044                    "// 2. long",
2045                    getLLVMStyleWithColumns(20)));
2046   EXPECT_EQ("// -# long long long\n"
2047             "// long\n"
2048             "// -# long",
2049             format("// -# long long long long\n"
2050                    "// -# long",
2051                    getLLVMStyleWithColumns(20)));
2052 
2053   EXPECT_EQ("// - long long long\n"
2054             "// long long long\n"
2055             "// - long",
2056             format("// - long long long long\n"
2057                    "// long long\n"
2058                    "// - long",
2059                    getLLVMStyleWithColumns(20)));
2060   EXPECT_EQ("// - long long long\n"
2061             "// long long long\n"
2062             "// long\n"
2063             "// - long",
2064             format("// - long long long long\n"
2065                    "// long long long\n"
2066                    "// - long",
2067                    getLLVMStyleWithColumns(20)));
2068 
2069   // Large number (>2 digits) are not list items
2070   EXPECT_EQ("// long long long\n"
2071             "// long 1024. long.",
2072             format("// long long long long\n"
2073                    "// 1024. long.",
2074                    getLLVMStyleWithColumns(20)));
2075 
2076   // Do not break before number, to avoid introducing a non-reflowable doxygen
2077   // list item.
2078   EXPECT_EQ("// long long\n"
2079             "// long 10. long.",
2080             format("// long long long 10.\n"
2081                    "// long.",
2082                    getLLVMStyleWithColumns(20)));
2083 
2084   // Don't break or reflow after implicit string literals.
2085   verifyFormat("#include <t> // l l l\n"
2086                "             // l",
2087                getLLVMStyleWithColumns(20));
2088 
2089   // Don't break or reflow comments on import lines.
2090   EXPECT_EQ("#include \"t\" /* l l l\n"
2091             "                * l */",
2092             format("#include \"t\" /* l l l\n"
2093                    "                * l */",
2094                    getLLVMStyleWithColumns(20)));
2095 
2096   // Don't reflow between different trailing comment sections.
2097   EXPECT_EQ("int i; // long long\n"
2098             "       // long\n"
2099             "int j; // long long\n"
2100             "       // long\n",
2101             format("int i; // long long long\n"
2102                    "int j; // long long long\n",
2103                    getLLVMStyleWithColumns(20)));
2104 
2105   // Don't reflow if the first word on the next line is longer than the
2106   // available space at current line.
2107   EXPECT_EQ("int i; // trigger\n"
2108             "       // reflow\n"
2109             "       // longsec\n",
2110             format("int i; // trigger reflow\n"
2111                    "       // longsec\n",
2112                    getLLVMStyleWithColumns(20)));
2113 
2114   // Simple case that correctly handles reflow in parameter lists.
2115   EXPECT_EQ("a = f(/* looooooooong\n"
2116             "       * long long\n"
2117             "       */\n"
2118             "      a);",
2119             format("a = f(/* looooooooong long\n* long\n*/ a);",
2120                    getLLVMStyleWithColumns(22)));
2121   // Tricky case that has fewer lines if we reflow the comment, ending up with
2122   // fewer lines.
2123   EXPECT_EQ("a = f(/* loooooong\n"
2124             "       * long long\n"
2125             "       */\n"
2126             "      a);",
2127             format("a = f(/* loooooong long\n* long\n*/ a);",
2128                    getLLVMStyleWithColumns(22)));
2129 
2130   // Keep empty comment lines.
2131   EXPECT_EQ("/**/", format(" /**/", getLLVMStyleWithColumns(20)));
2132   EXPECT_EQ("/* */", format(" /* */", getLLVMStyleWithColumns(20)));
2133   EXPECT_EQ("/*  */", format(" /*  */", getLLVMStyleWithColumns(20)));
2134   EXPECT_EQ("//", format(" //  ", getLLVMStyleWithColumns(20)));
2135   EXPECT_EQ("///", format(" ///  ", getLLVMStyleWithColumns(20)));
2136 }
2137 
TEST_F(FormatTestComments,ReflowsCommentsPrecise)2138 TEST_F(FormatTestComments, ReflowsCommentsPrecise) {
2139   // FIXME: This assumes we do not continue compressing whitespace once we are
2140   // in reflow mode. Consider compressing whitespace.
2141 
2142   // Test that we stop reflowing precisely at the column limit.
2143   // After reflowing, "// reflows into   foo" does not fit the column limit,
2144   // so we compress the whitespace.
2145   EXPECT_EQ("// some text that\n"
2146             "// reflows into foo\n",
2147             format("// some text that reflows\n"
2148                    "// into   foo\n",
2149                    getLLVMStyleWithColumns(20)));
2150   // Given one more column, "// reflows into   foo" does fit the limit, so we
2151   // do not compress the whitespace.
2152   EXPECT_EQ("// some text that\n"
2153             "// reflows into   foo\n",
2154             format("// some text that reflows\n"
2155                    "// into   foo\n",
2156                    getLLVMStyleWithColumns(21)));
2157 
2158   // Make sure that we correctly account for the space added in the reflow case
2159   // when making the reflowing decision.
2160   // First, when the next line ends precisely one column over the limit, do not
2161   // reflow.
2162   EXPECT_EQ("// some text that\n"
2163             "// reflows\n"
2164             "// into1234567\n",
2165             format("// some text that reflows\n"
2166                    "// into1234567\n",
2167                    getLLVMStyleWithColumns(21)));
2168   // Secondly, when the next line ends later, but the first word in that line
2169   // is precisely one column over the limit, do not reflow.
2170   EXPECT_EQ("// some text that\n"
2171             "// reflows\n"
2172             "// into1234567 f\n",
2173             format("// some text that reflows\n"
2174                    "// into1234567 f\n",
2175                    getLLVMStyleWithColumns(21)));
2176 }
2177 
TEST_F(FormatTestComments,ReflowsCommentsWithExtraWhitespace)2178 TEST_F(FormatTestComments, ReflowsCommentsWithExtraWhitespace) {
2179   // Baseline.
2180   EXPECT_EQ("// some text\n"
2181             "// that re flows\n",
2182             format("// some text that\n"
2183                    "// re flows\n",
2184                    getLLVMStyleWithColumns(16)));
2185   EXPECT_EQ("// some text\n"
2186             "// that re flows\n",
2187             format("// some text that\n"
2188                    "// re    flows\n",
2189                    getLLVMStyleWithColumns(16)));
2190   EXPECT_EQ("/* some text\n"
2191             " * that re flows\n"
2192             " */\n",
2193             format("/* some text that\n"
2194                    "*      re       flows\n"
2195                    "*/\n",
2196                    getLLVMStyleWithColumns(16)));
2197   // FIXME: We do not reflow if the indent of two subsequent lines differs;
2198   // given that this is different behavior from block comments, do we want
2199   // to keep this?
2200   EXPECT_EQ("// some text\n"
2201             "// that\n"
2202             "//     re flows\n",
2203             format("// some text that\n"
2204                    "//     re       flows\n",
2205                    getLLVMStyleWithColumns(16)));
2206   // Space within parts of a line that fit.
2207   // FIXME: Use the earliest possible split while reflowing to compress the
2208   // whitespace within the line.
2209   EXPECT_EQ("// some text that\n"
2210             "// does re   flow\n"
2211             "// more  here\n",
2212             format("// some text that does\n"
2213                    "// re   flow  more  here\n",
2214                    getLLVMStyleWithColumns(21)));
2215 }
2216 
TEST_F(FormatTestComments,IgnoresIf0Contents)2217 TEST_F(FormatTestComments, IgnoresIf0Contents) {
2218   EXPECT_EQ("#if 0\n"
2219             "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
2220             "#endif\n"
2221             "void f() {}",
2222             format("#if 0\n"
2223                    "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
2224                    "#endif\n"
2225                    "void f(  ) {  }"));
2226   EXPECT_EQ("#if false\n"
2227             "void f(  ) {  }\n"
2228             "#endif\n"
2229             "void g() {}\n",
2230             format("#if false\n"
2231                    "void f(  ) {  }\n"
2232                    "#endif\n"
2233                    "void g(  ) {  }\n"));
2234   EXPECT_EQ("enum E {\n"
2235             "  One,\n"
2236             "  Two,\n"
2237             "#if 0\n"
2238             "Three,\n"
2239             "      Four,\n"
2240             "#endif\n"
2241             "  Five\n"
2242             "};",
2243             format("enum E {\n"
2244                    "  One,Two,\n"
2245                    "#if 0\n"
2246                    "Three,\n"
2247                    "      Four,\n"
2248                    "#endif\n"
2249                    "  Five};"));
2250   EXPECT_EQ("enum F {\n"
2251             "  One,\n"
2252             "#if 1\n"
2253             "  Two,\n"
2254             "#if 0\n"
2255             "Three,\n"
2256             "      Four,\n"
2257             "#endif\n"
2258             "  Five\n"
2259             "#endif\n"
2260             "};",
2261             format("enum F {\n"
2262                    "One,\n"
2263                    "#if 1\n"
2264                    "Two,\n"
2265                    "#if 0\n"
2266                    "Three,\n"
2267                    "      Four,\n"
2268                    "#endif\n"
2269                    "Five\n"
2270                    "#endif\n"
2271                    "};"));
2272   EXPECT_EQ("enum G {\n"
2273             "  One,\n"
2274             "#if 0\n"
2275             "Two,\n"
2276             "#else\n"
2277             "  Three,\n"
2278             "#endif\n"
2279             "  Four\n"
2280             "};",
2281             format("enum G {\n"
2282                    "One,\n"
2283                    "#if 0\n"
2284                    "Two,\n"
2285                    "#else\n"
2286                    "Three,\n"
2287                    "#endif\n"
2288                    "Four\n"
2289                    "};"));
2290   EXPECT_EQ("enum H {\n"
2291             "  One,\n"
2292             "#if 0\n"
2293             "#ifdef Q\n"
2294             "Two,\n"
2295             "#else\n"
2296             "Three,\n"
2297             "#endif\n"
2298             "#endif\n"
2299             "  Four\n"
2300             "};",
2301             format("enum H {\n"
2302                    "One,\n"
2303                    "#if 0\n"
2304                    "#ifdef Q\n"
2305                    "Two,\n"
2306                    "#else\n"
2307                    "Three,\n"
2308                    "#endif\n"
2309                    "#endif\n"
2310                    "Four\n"
2311                    "};"));
2312   EXPECT_EQ("enum I {\n"
2313             "  One,\n"
2314             "#if /* test */ 0 || 1\n"
2315             "Two,\n"
2316             "Three,\n"
2317             "#endif\n"
2318             "  Four\n"
2319             "};",
2320             format("enum I {\n"
2321                    "One,\n"
2322                    "#if /* test */ 0 || 1\n"
2323                    "Two,\n"
2324                    "Three,\n"
2325                    "#endif\n"
2326                    "Four\n"
2327                    "};"));
2328   EXPECT_EQ("enum J {\n"
2329             "  One,\n"
2330             "#if 0\n"
2331             "#if 0\n"
2332             "Two,\n"
2333             "#else\n"
2334             "Three,\n"
2335             "#endif\n"
2336             "Four,\n"
2337             "#endif\n"
2338             "  Five\n"
2339             "};",
2340             format("enum J {\n"
2341                    "One,\n"
2342                    "#if 0\n"
2343                    "#if 0\n"
2344                    "Two,\n"
2345                    "#else\n"
2346                    "Three,\n"
2347                    "#endif\n"
2348                    "Four,\n"
2349                    "#endif\n"
2350                    "Five\n"
2351                    "};"));
2352 
2353   // Ignore stuff in SWIG-blocks.
2354   EXPECT_EQ("#ifdef SWIG\n"
2355             "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
2356             "#endif\n"
2357             "void f() {}",
2358             format("#ifdef SWIG\n"
2359                    "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
2360                    "#endif\n"
2361                    "void f(  ) {  }"));
2362   EXPECT_EQ("#ifndef SWIG\n"
2363             "void f() {}\n"
2364             "#endif",
2365             format("#ifndef SWIG\n"
2366                    "void f(      ) {       }\n"
2367                    "#endif"));
2368 }
2369 
TEST_F(FormatTestComments,DontCrashOnBlockComments)2370 TEST_F(FormatTestComments, DontCrashOnBlockComments) {
2371   EXPECT_EQ(
2372       "int xxxxxxxxx; /* "
2373       "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\n"
2374       "zzzzzz\n"
2375       "0*/",
2376       format("int xxxxxxxxx;                          /* "
2377              "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy zzzzzz\n"
2378              "0*/"));
2379 }
2380 
TEST_F(FormatTestComments,BlockCommentsInControlLoops)2381 TEST_F(FormatTestComments, BlockCommentsInControlLoops) {
2382   verifyFormat("if (0) /* a comment in a strange place */ {\n"
2383                "  f();\n"
2384                "}");
2385   verifyFormat("if (0) /* a comment in a strange place */ {\n"
2386                "  f();\n"
2387                "} /* another comment */ else /* comment #3 */ {\n"
2388                "  g();\n"
2389                "}");
2390   verifyFormat("while (0) /* a comment in a strange place */ {\n"
2391                "  f();\n"
2392                "}");
2393   verifyFormat("for (;;) /* a comment in a strange place */ {\n"
2394                "  f();\n"
2395                "}");
2396   verifyFormat("do /* a comment in a strange place */ {\n"
2397                "  f();\n"
2398                "} /* another comment */ while (0);");
2399 }
2400 
TEST_F(FormatTestComments,BlockComments)2401 TEST_F(FormatTestComments, BlockComments) {
2402   EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */",
2403             format("/* *//* */  /* */\n/* *//* */  /* */"));
2404   EXPECT_EQ("/* */ a /* */ b;", format("  /* */  a/* */  b;"));
2405   EXPECT_EQ("#define A /*123*/ \\\n"
2406             "  b\n"
2407             "/* */\n"
2408             "someCall(\n"
2409             "    parameter);",
2410             format("#define A /*123*/ b\n"
2411                    "/* */\n"
2412                    "someCall(parameter);",
2413                    getLLVMStyleWithColumns(15)));
2414 
2415   EXPECT_EQ("#define A\n"
2416             "/* */ someCall(\n"
2417             "    parameter);",
2418             format("#define A\n"
2419                    "/* */someCall(parameter);",
2420                    getLLVMStyleWithColumns(15)));
2421   EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/"));
2422   EXPECT_EQ("/*\n"
2423             " *\n"
2424             " * aaaaaa\n"
2425             " * aaaaaa\n"
2426             " */",
2427             format("/*\n"
2428                    "*\n"
2429                    " * aaaaaa aaaaaa\n"
2430                    "*/",
2431                    getLLVMStyleWithColumns(10)));
2432   EXPECT_EQ("/*\n"
2433             "**\n"
2434             "* aaaaaa\n"
2435             "*aaaaaa\n"
2436             "*/",
2437             format("/*\n"
2438                    "**\n"
2439                    "* aaaaaa aaaaaa\n"
2440                    "*/",
2441                    getLLVMStyleWithColumns(10)));
2442   EXPECT_EQ("int aaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
2443             "    /* line 1\n"
2444             "       bbbbbbbbbbbb */\n"
2445             "    bbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
2446             format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
2447                    "    /* line 1\n"
2448                    "       bbbbbbbbbbbb */ bbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
2449                    getLLVMStyleWithColumns(50)));
2450 
2451   FormatStyle NoBinPacking = getLLVMStyle();
2452   NoBinPacking.BinPackParameters = false;
2453   EXPECT_EQ("someFunction(1, /* comment 1 */\n"
2454             "             2, /* comment 2 */\n"
2455             "             3, /* comment 3 */\n"
2456             "             aaaa,\n"
2457             "             bbbb);",
2458             format("someFunction (1,   /* comment 1 */\n"
2459                    "                2, /* comment 2 */  \n"
2460                    "               3,   /* comment 3 */\n"
2461                    "aaaa, bbbb );",
2462                    NoBinPacking));
2463   verifyFormat(
2464       "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2465       "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
2466   EXPECT_EQ(
2467       "bool aaaaaaaaaaaaa = /* trailing comment */\n"
2468       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2469       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;",
2470       format(
2471           "bool       aaaaaaaaaaaaa =       /* trailing comment */\n"
2472           "    aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa    ||\n"
2473           "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa   || aaaaaaaaaaaaaaaaaaaaaaaaaa;"));
2474   EXPECT_EQ(
2475       "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
2476       "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;   /* comment */\n"
2477       "int cccccccccccccccccccccccccccccc;       /* comment */\n",
2478       format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
2479              "int      bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
2480              "int    cccccccccccccccccccccccccccccc;  /* comment */\n"));
2481 
2482   verifyFormat("void f(int * /* unused */) {}");
2483 
2484   EXPECT_EQ("/*\n"
2485             " **\n"
2486             " */",
2487             format("/*\n"
2488                    " **\n"
2489                    " */"));
2490   EXPECT_EQ("/*\n"
2491             " *q\n"
2492             " */",
2493             format("/*\n"
2494                    " *q\n"
2495                    " */"));
2496   EXPECT_EQ("/*\n"
2497             " * q\n"
2498             " */",
2499             format("/*\n"
2500                    " * q\n"
2501                    " */"));
2502   EXPECT_EQ("/*\n"
2503             " **/",
2504             format("/*\n"
2505                    " **/"));
2506   EXPECT_EQ("/*\n"
2507             " ***/",
2508             format("/*\n"
2509                    " ***/"));
2510 }
2511 
TEST_F(FormatTestComments,BlockCommentsInMacros)2512 TEST_F(FormatTestComments, BlockCommentsInMacros) {
2513   EXPECT_EQ("#define A          \\\n"
2514             "  {                \\\n"
2515             "    /* one line */ \\\n"
2516             "    someCall();",
2517             format("#define A {        \\\n"
2518                    "  /* one line */   \\\n"
2519                    "  someCall();",
2520                    getLLVMStyleWithColumns(20)));
2521   EXPECT_EQ("#define A          \\\n"
2522             "  {                \\\n"
2523             "    /* previous */ \\\n"
2524             "    /* one line */ \\\n"
2525             "    someCall();",
2526             format("#define A {        \\\n"
2527                    "  /* previous */   \\\n"
2528                    "  /* one line */   \\\n"
2529                    "  someCall();",
2530                    getLLVMStyleWithColumns(20)));
2531 }
2532 
TEST_F(FormatTestComments,BlockCommentsAtEndOfLine)2533 TEST_F(FormatTestComments, BlockCommentsAtEndOfLine) {
2534   EXPECT_EQ("a = {\n"
2535             "    1111 /*    */\n"
2536             "};",
2537             format("a = {1111 /*    */\n"
2538                    "};",
2539                    getLLVMStyleWithColumns(15)));
2540   EXPECT_EQ("a = {\n"
2541             "    1111 /*      */\n"
2542             "};",
2543             format("a = {1111 /*      */\n"
2544                    "};",
2545                    getLLVMStyleWithColumns(15)));
2546   EXPECT_EQ("a = {\n"
2547             "    1111 /*      a\n"
2548             "          */\n"
2549             "};",
2550             format("a = {1111 /*      a */\n"
2551                    "};",
2552                    getLLVMStyleWithColumns(15)));
2553 }
2554 
TEST_F(FormatTestComments,BreaksAfterMultilineBlockCommentsInParamLists)2555 TEST_F(FormatTestComments, BreaksAfterMultilineBlockCommentsInParamLists) {
2556   EXPECT_EQ("a = f(/* long\n"
2557             "         long */\n"
2558             "      a);",
2559             format("a = f(/* long long */ a);", getLLVMStyleWithColumns(16)));
2560   EXPECT_EQ("a = f(\n"
2561             "    /* long\n"
2562             "       long */\n"
2563             "    a);",
2564             format("a = f(/* long long */ a);", getLLVMStyleWithColumns(15)));
2565 
2566   EXPECT_EQ("a = f(/* long\n"
2567             "         long\n"
2568             "       */\n"
2569             "      a);",
2570             format("a = f(/* long\n"
2571                    "         long\n"
2572                    "       */a);",
2573                    getLLVMStyleWithColumns(16)));
2574 
2575   EXPECT_EQ("a = f(/* long\n"
2576             "         long\n"
2577             "       */\n"
2578             "      a);",
2579             format("a = f(/* long\n"
2580                    "         long\n"
2581                    "       */ a);",
2582                    getLLVMStyleWithColumns(16)));
2583 
2584   EXPECT_EQ("a = f(/* long\n"
2585             "         long\n"
2586             "       */\n"
2587             "      (1 + 1));",
2588             format("a = f(/* long\n"
2589                    "         long\n"
2590                    "       */ (1 + 1));",
2591                    getLLVMStyleWithColumns(16)));
2592 
2593   EXPECT_EQ(
2594       "a = f(a,\n"
2595       "      /* long\n"
2596       "         long */\n"
2597       "      b);",
2598       format("a = f(a, /* long long */ b);", getLLVMStyleWithColumns(16)));
2599 
2600   EXPECT_EQ(
2601       "a = f(\n"
2602       "    a,\n"
2603       "    /* long\n"
2604       "       long */\n"
2605       "    b);",
2606       format("a = f(a, /* long long */ b);", getLLVMStyleWithColumns(15)));
2607 
2608   EXPECT_EQ("a = f(a,\n"
2609             "      /* long\n"
2610             "         long */\n"
2611             "      (1 + 1));",
2612             format("a = f(a, /* long long */ (1 + 1));",
2613                    getLLVMStyleWithColumns(16)));
2614   EXPECT_EQ("a = f(\n"
2615             "    a,\n"
2616             "    /* long\n"
2617             "       long */\n"
2618             "    (1 + 1));",
2619             format("a = f(a, /* long long */ (1 + 1));",
2620                    getLLVMStyleWithColumns(15)));
2621 }
2622 
TEST_F(FormatTestComments,IndentLineCommentsInStartOfBlockAtEndOfFile)2623 TEST_F(FormatTestComments, IndentLineCommentsInStartOfBlockAtEndOfFile) {
2624   verifyFormat("{\n"
2625                "  // a\n"
2626                "  // b");
2627 }
2628 
TEST_F(FormatTestComments,AlignTrailingComments)2629 TEST_F(FormatTestComments, AlignTrailingComments) {
2630   EXPECT_EQ("#define MACRO(V)                       \\\n"
2631             "  V(Rt2) /* one more char */           \\\n"
2632             "  V(Rs)  /* than here  */              \\\n"
2633             "/* comment 3 */\n",
2634             format("#define MACRO(V)\\\n"
2635                    "V(Rt2)  /* one more char */ \\\n"
2636                    "V(Rs) /* than here  */    \\\n"
2637                    "/* comment 3 */\n",
2638                    getLLVMStyleWithColumns(40)));
2639   EXPECT_EQ("int i = f(abc, // line 1\n"
2640             "          d,   // line 2\n"
2641             "               // line 3\n"
2642             "          b);",
2643             format("int i = f(abc, // line 1\n"
2644                    "          d, // line 2\n"
2645                    "             // line 3\n"
2646                    "          b);",
2647                    getLLVMStyleWithColumns(40)));
2648 
2649   // Align newly broken trailing comments.
2650   EXPECT_EQ("int ab; // line\n"
2651             "int a;  // long\n"
2652             "        // long\n",
2653             format("int ab; // line\n"
2654                    "int a; // long long\n",
2655                    getLLVMStyleWithColumns(15)));
2656   EXPECT_EQ("int ab; // line\n"
2657             "int a;  // long\n"
2658             "        // long\n"
2659             "        // long",
2660             format("int ab; // line\n"
2661                    "int a; // long long\n"
2662                    "       // long",
2663                    getLLVMStyleWithColumns(15)));
2664   EXPECT_EQ("int ab; // line\n"
2665             "int a;  // long\n"
2666             "        // long\n"
2667             "pt c;   // long",
2668             format("int ab; // line\n"
2669                    "int a; // long long\n"
2670                    "pt c; // long",
2671                    getLLVMStyleWithColumns(15)));
2672   EXPECT_EQ("int ab; // line\n"
2673             "int a;  // long\n"
2674             "        // long\n"
2675             "\n"
2676             "// long",
2677             format("int ab; // line\n"
2678                    "int a; // long long\n"
2679                    "\n"
2680                    "// long",
2681                    getLLVMStyleWithColumns(15)));
2682 
2683   // Don't align newly broken trailing comments if that would put them over the
2684   // column limit.
2685   EXPECT_EQ("int i, j; // line 1\n"
2686             "int k; // line longg\n"
2687             "       // long",
2688             format("int i, j; // line 1\n"
2689                    "int k; // line longg long",
2690                    getLLVMStyleWithColumns(20)));
2691 
2692   // Always align if ColumnLimit = 0
2693   EXPECT_EQ("int i, j; // line 1\n"
2694             "int k;    // line longg long",
2695             format("int i, j; // line 1\n"
2696                    "int k; // line longg long",
2697                    getLLVMStyleWithColumns(0)));
2698 
2699   // Align comment line sections aligned with the next token with the next
2700   // token.
2701   EXPECT_EQ("class A {\n"
2702             "public: // public comment\n"
2703             "  // comment about a\n"
2704             "  int a;\n"
2705             "};",
2706             format("class A {\n"
2707                    "public: // public comment\n"
2708                    "  // comment about a\n"
2709                    "  int a;\n"
2710                    "};",
2711                    getLLVMStyleWithColumns(40)));
2712   EXPECT_EQ("class A {\n"
2713             "public: // public comment 1\n"
2714             "        // public comment 2\n"
2715             "  // comment 1 about a\n"
2716             "  // comment 2 about a\n"
2717             "  int a;\n"
2718             "};",
2719             format("class A {\n"
2720                    "public: // public comment 1\n"
2721                    "   // public comment 2\n"
2722                    "  // comment 1 about a\n"
2723                    "  // comment 2 about a\n"
2724                    "  int a;\n"
2725                    "};",
2726                    getLLVMStyleWithColumns(40)));
2727   EXPECT_EQ("int f(int n) { // comment line 1 on f\n"
2728             "               // comment line 2 on f\n"
2729             "  // comment line 1 before return\n"
2730             "  // comment line 2 before return\n"
2731             "  return n; // comment line 1 on return\n"
2732             "            // comment line 2 on return\n"
2733             "  // comment line 1 after return\n"
2734             "}",
2735             format("int f(int n) { // comment line 1 on f\n"
2736                    "   // comment line 2 on f\n"
2737                    "  // comment line 1 before return\n"
2738                    "  // comment line 2 before return\n"
2739                    "  return n; // comment line 1 on return\n"
2740                    "   // comment line 2 on return\n"
2741                    "  // comment line 1 after return\n"
2742                    "}",
2743                    getLLVMStyleWithColumns(40)));
2744   EXPECT_EQ("int f(int n) {\n"
2745             "  switch (n) { // comment line 1 on switch\n"
2746             "               // comment line 2 on switch\n"
2747             "  // comment line 1 before case 1\n"
2748             "  // comment line 2 before case 1\n"
2749             "  case 1: // comment line 1 on case 1\n"
2750             "          // comment line 2 on case 1\n"
2751             "    // comment line 1 before return 1\n"
2752             "    // comment line 2 before return 1\n"
2753             "    return 1; // comment line 1 on return 1\n"
2754             "              // comment line 2 on return 1\n"
2755             "  // comment line 1 before default\n"
2756             "  // comment line 2 before default\n"
2757             "  default: // comment line 1 on default\n"
2758             "           // comment line 2 on default\n"
2759             "    // comment line 1 before return 2\n"
2760             "    return 2 * f(n - 1); // comment line 1 on return 2\n"
2761             "                         // comment line 2 on return 2\n"
2762             "    // comment line 1 after return\n"
2763             "    // comment line 2 after return\n"
2764             "  }\n"
2765             "}",
2766             format("int f(int n) {\n"
2767                    "  switch (n) { // comment line 1 on switch\n"
2768                    "              // comment line 2 on switch\n"
2769                    "    // comment line 1 before case 1\n"
2770                    "    // comment line 2 before case 1\n"
2771                    "    case 1: // comment line 1 on case 1\n"
2772                    "              // comment line 2 on case 1\n"
2773                    "    // comment line 1 before return 1\n"
2774                    "    // comment line 2 before return 1\n"
2775                    "    return 1;  // comment line 1 on return 1\n"
2776                    "             // comment line 2 on return 1\n"
2777                    "    // comment line 1 before default\n"
2778                    "    // comment line 2 before default\n"
2779                    "    default:   // comment line 1 on default\n"
2780                    "                // comment line 2 on default\n"
2781                    "    // comment line 1 before return 2\n"
2782                    "    return 2 * f(n - 1); // comment line 1 on return 2\n"
2783                    "                        // comment line 2 on return 2\n"
2784                    "    // comment line 1 after return\n"
2785                    "     // comment line 2 after return\n"
2786                    "  }\n"
2787                    "}",
2788                    getLLVMStyleWithColumns(80)));
2789 
2790   // If all the lines in a sequence of line comments are aligned with the next
2791   // token, the first line belongs to the previous token and the other lines
2792   // belong to the next token.
2793   EXPECT_EQ("int a; // line about a\n"
2794             "long b;",
2795             format("int a; // line about a\n"
2796                    "       long b;",
2797                    getLLVMStyleWithColumns(80)));
2798   EXPECT_EQ("int a; // line about a\n"
2799             "// line about b\n"
2800             "long b;",
2801             format("int a; // line about a\n"
2802                    "       // line about b\n"
2803                    "       long b;",
2804                    getLLVMStyleWithColumns(80)));
2805   EXPECT_EQ("int a; // line about a\n"
2806             "// line 1 about b\n"
2807             "// line 2 about b\n"
2808             "long b;",
2809             format("int a; // line about a\n"
2810                    "       // line 1 about b\n"
2811                    "       // line 2 about b\n"
2812                    "       long b;",
2813                    getLLVMStyleWithColumns(80)));
2814 
2815   // Checks an edge case in preprocessor handling.
2816   // These comments should *not* be aligned
2817   EXPECT_NE( // change for EQ when fixed
2818       "#if FOO\n"
2819       "#else\n"
2820       "long a; // Line about a\n"
2821       "#endif\n"
2822       "#if BAR\n"
2823       "#else\n"
2824       "long b_long_name; // Line about b\n"
2825       "#endif\n",
2826       format("#if FOO\n"
2827              "#else\n"
2828              "long a;           // Line about a\n" // Previous (bad) behavior
2829              "#endif\n"
2830              "#if BAR\n"
2831              "#else\n"
2832              "long b_long_name; // Line about b\n"
2833              "#endif\n",
2834              getLLVMStyleWithColumns(80)));
2835 
2836   // bug 47589
2837   EXPECT_EQ(
2838       "namespace m {\n\n"
2839       "#define FOO_GLOBAL 0      // Global scope.\n"
2840       "#define FOO_LINKLOCAL 1   // Link-local scope.\n"
2841       "#define FOO_SITELOCAL 2   // Site-local scope (deprecated).\n"
2842       "#define FOO_UNIQUELOCAL 3 // Unique local\n"
2843       "#define FOO_NODELOCAL 4   // Loopback\n\n"
2844       "} // namespace m\n",
2845       format("namespace m {\n\n"
2846              "#define FOO_GLOBAL 0   // Global scope.\n"
2847              "#define FOO_LINKLOCAL 1  // Link-local scope.\n"
2848              "#define FOO_SITELOCAL 2  // Site-local scope (deprecated).\n"
2849              "#define FOO_UNIQUELOCAL 3 // Unique local\n"
2850              "#define FOO_NODELOCAL 4  // Loopback\n\n"
2851              "} // namespace m\n",
2852              getLLVMStyleWithColumns(80)));
2853 
2854   // https://llvm.org/PR53441
2855   verifyFormat("/* */  //\n"
2856                "int a; //\n");
2857   verifyFormat("/**/   //\n"
2858                "int a; //\n");
2859 }
2860 
TEST_F(FormatTestComments,AlignsBlockCommentDecorations)2861 TEST_F(FormatTestComments, AlignsBlockCommentDecorations) {
2862   EXPECT_EQ("/*\n"
2863             " */",
2864             format("/*\n"
2865                    "*/",
2866                    getLLVMStyle()));
2867   EXPECT_EQ("/*\n"
2868             " */",
2869             format("/*\n"
2870                    " */",
2871                    getLLVMStyle()));
2872   EXPECT_EQ("/*\n"
2873             " */",
2874             format("/*\n"
2875                    "  */",
2876                    getLLVMStyle()));
2877 
2878   // Align a single line.
2879   EXPECT_EQ("/*\n"
2880             " * line */",
2881             format("/*\n"
2882                    "* line */",
2883                    getLLVMStyle()));
2884   EXPECT_EQ("/*\n"
2885             " * line */",
2886             format("/*\n"
2887                    " * line */",
2888                    getLLVMStyle()));
2889   EXPECT_EQ("/*\n"
2890             " * line */",
2891             format("/*\n"
2892                    "  * line */",
2893                    getLLVMStyle()));
2894   EXPECT_EQ("/*\n"
2895             " * line */",
2896             format("/*\n"
2897                    "   * line */",
2898                    getLLVMStyle()));
2899   EXPECT_EQ("/**\n"
2900             " * line */",
2901             format("/**\n"
2902                    "* line */",
2903                    getLLVMStyle()));
2904   EXPECT_EQ("/**\n"
2905             " * line */",
2906             format("/**\n"
2907                    " * line */",
2908                    getLLVMStyle()));
2909   EXPECT_EQ("/**\n"
2910             " * line */",
2911             format("/**\n"
2912                    "  * line */",
2913                    getLLVMStyle()));
2914   EXPECT_EQ("/**\n"
2915             " * line */",
2916             format("/**\n"
2917                    "   * line */",
2918                    getLLVMStyle()));
2919   EXPECT_EQ("/**\n"
2920             " * line */",
2921             format("/**\n"
2922                    "    * line */",
2923                    getLLVMStyle()));
2924 
2925   // Align the end '*/' after a line.
2926   EXPECT_EQ("/*\n"
2927             " * line\n"
2928             " */",
2929             format("/*\n"
2930                    "* line\n"
2931                    "*/",
2932                    getLLVMStyle()));
2933   EXPECT_EQ("/*\n"
2934             " * line\n"
2935             " */",
2936             format("/*\n"
2937                    "   * line\n"
2938                    "  */",
2939                    getLLVMStyle()));
2940   EXPECT_EQ("/*\n"
2941             " * line\n"
2942             " */",
2943             format("/*\n"
2944                    "  * line\n"
2945                    "  */",
2946                    getLLVMStyle()));
2947 
2948   // Align two lines.
2949   EXPECT_EQ("/* line 1\n"
2950             " * line 2 */",
2951             format("/* line 1\n"
2952                    " * line 2 */",
2953                    getLLVMStyle()));
2954   EXPECT_EQ("/* line 1\n"
2955             " * line 2 */",
2956             format("/* line 1\n"
2957                    "* line 2 */",
2958                    getLLVMStyle()));
2959   EXPECT_EQ("/* line 1\n"
2960             " * line 2 */",
2961             format("/* line 1\n"
2962                    "  * line 2 */",
2963                    getLLVMStyle()));
2964   EXPECT_EQ("/* line 1\n"
2965             " * line 2 */",
2966             format("/* line 1\n"
2967                    "   * line 2 */",
2968                    getLLVMStyle()));
2969   EXPECT_EQ("/* line 1\n"
2970             " * line 2 */",
2971             format("/* line 1\n"
2972                    "    * line 2 */",
2973                    getLLVMStyle()));
2974   EXPECT_EQ("int i; /* line 1\n"
2975             "        * line 2 */",
2976             format("int i; /* line 1\n"
2977                    "* line 2 */",
2978                    getLLVMStyle()));
2979   EXPECT_EQ("int i; /* line 1\n"
2980             "        * line 2 */",
2981             format("int i; /* line 1\n"
2982                    "        * line 2 */",
2983                    getLLVMStyle()));
2984   EXPECT_EQ("int i; /* line 1\n"
2985             "        * line 2 */",
2986             format("int i; /* line 1\n"
2987                    "             * line 2 */",
2988                    getLLVMStyle()));
2989 
2990   // Align several lines.
2991   EXPECT_EQ("/* line 1\n"
2992             " * line 2\n"
2993             " * line 3 */",
2994             format("/* line 1\n"
2995                    " * line 2\n"
2996                    "* line 3 */",
2997                    getLLVMStyle()));
2998   EXPECT_EQ("/* line 1\n"
2999             " * line 2\n"
3000             " * line 3 */",
3001             format("/* line 1\n"
3002                    "  * line 2\n"
3003                    "* line 3 */",
3004                    getLLVMStyle()));
3005   EXPECT_EQ("/*\n"
3006             "** line 1\n"
3007             "** line 2\n"
3008             "*/",
3009             format("/*\n"
3010                    "** line 1\n"
3011                    " ** line 2\n"
3012                    "*/",
3013                    getLLVMStyle()));
3014 
3015   // Align with different indent after the decorations.
3016   EXPECT_EQ("/*\n"
3017             " * line 1\n"
3018             " *  line 2\n"
3019             " * line 3\n"
3020             " *   line 4\n"
3021             " */",
3022             format("/*\n"
3023                    "* line 1\n"
3024                    "  *  line 2\n"
3025                    "   * line 3\n"
3026                    "*   line 4\n"
3027                    "*/",
3028                    getLLVMStyle()));
3029 
3030   // Align empty or blank lines.
3031   EXPECT_EQ("/**\n"
3032             " *\n"
3033             " *\n"
3034             " *\n"
3035             " */",
3036             format("/**\n"
3037                    "*  \n"
3038                    " * \n"
3039                    "  *\n"
3040                    "*/",
3041                    getLLVMStyle()));
3042 
3043   // Align while breaking and reflowing.
3044   EXPECT_EQ("/*\n"
3045             " * long long long\n"
3046             " * long long\n"
3047             " *\n"
3048             " * long */",
3049             format("/*\n"
3050                    " * long long long long\n"
3051                    " * long\n"
3052                    "  *\n"
3053                    "* long */",
3054                    getLLVMStyleWithColumns(20)));
3055 }
3056 
TEST_F(FormatTestComments,NoCrash_Bug34236)3057 TEST_F(FormatTestComments, NoCrash_Bug34236) {
3058   // This is a test case from a crasher reported in:
3059   // https://bugs.llvm.org/show_bug.cgi?id=34236
3060   // Temporarily disable formatting for readability.
3061   // clang-format off
3062   EXPECT_EQ(
3063 "/*                                                                */ /*\n"
3064 "                                                                      *       a\n"
3065 "                                                                      * b c d*/",
3066       format(
3067 "/*                                                                */ /*\n"
3068 " *       a b\n"
3069 " *       c     d*/",
3070           getLLVMStyleWithColumns(80)));
3071   // clang-format on
3072 }
3073 
TEST_F(FormatTestComments,NonTrailingBlockComments)3074 TEST_F(FormatTestComments, NonTrailingBlockComments) {
3075   verifyFormat("const /** comment comment */ A = B;",
3076                getLLVMStyleWithColumns(40));
3077 
3078   verifyFormat("const /** comment comment comment */ A =\n"
3079                "    B;",
3080                getLLVMStyleWithColumns(40));
3081 
3082   EXPECT_EQ("const /** comment comment comment\n"
3083             "         comment */\n"
3084             "    A = B;",
3085             format("const /** comment comment comment comment */\n"
3086                    "    A = B;",
3087                    getLLVMStyleWithColumns(40)));
3088 }
3089 
TEST_F(FormatTestComments,PythonStyleComments)3090 TEST_F(FormatTestComments, PythonStyleComments) {
3091   // Keeps a space after '#'.
3092   EXPECT_EQ("# comment\n"
3093             "key: value",
3094             format("#comment\n"
3095                    "key:value",
3096                    getTextProtoStyleWithColumns(20)));
3097   EXPECT_EQ("# comment\n"
3098             "key: value",
3099             format("# comment\n"
3100                    "key:value",
3101                    getTextProtoStyleWithColumns(20)));
3102   // Breaks long comment.
3103   EXPECT_EQ("# comment comment\n"
3104             "# comment\n"
3105             "key: value",
3106             format("# comment comment comment\n"
3107                    "key:value",
3108                    getTextProtoStyleWithColumns(20)));
3109   // Indents comments.
3110   EXPECT_EQ("data {\n"
3111             "  # comment comment\n"
3112             "  # comment\n"
3113             "  key: value\n"
3114             "}",
3115             format("data {\n"
3116                    "# comment comment comment\n"
3117                    "key: value}",
3118                    getTextProtoStyleWithColumns(20)));
3119   EXPECT_EQ("data {\n"
3120             "  # comment comment\n"
3121             "  # comment\n"
3122             "  key: value\n"
3123             "}",
3124             format("data {# comment comment comment\n"
3125                    "key: value}",
3126                    getTextProtoStyleWithColumns(20)));
3127   // Reflows long comments.
3128   EXPECT_EQ("# comment comment\n"
3129             "# comment comment\n"
3130             "key: value",
3131             format("# comment comment comment\n"
3132                    "# comment\n"
3133                    "key:value",
3134                    getTextProtoStyleWithColumns(20)));
3135   // Breaks trailing comments.
3136   EXPECT_EQ("k: val  # comment\n"
3137             "        # comment\n"
3138             "a: 1",
3139             format("k:val#comment comment\n"
3140                    "a:1",
3141                    getTextProtoStyleWithColumns(20)));
3142   EXPECT_EQ("id {\n"
3143             "  k: val  # comment\n"
3144             "          # comment\n"
3145             "  # line line\n"
3146             "  a: 1\n"
3147             "}",
3148             format("id {k:val#comment comment\n"
3149                    "# line line\n"
3150                    "a:1}",
3151                    getTextProtoStyleWithColumns(20)));
3152   // Aligns trailing comments.
3153   EXPECT_EQ("k: val  # commen1\n"
3154             "        # commen2\n"
3155             "        # commen3\n"
3156             "# commen4\n"
3157             "a: 1  # commen5\n"
3158             "      # commen6\n"
3159             "      # commen7",
3160             format("k:val#commen1 commen2\n"
3161                    " #commen3\n"
3162                    "# commen4\n"
3163                    "a:1#commen5 commen6\n"
3164                    " #commen7",
3165                    getTextProtoStyleWithColumns(20)));
3166 }
3167 
TEST_F(FormatTestComments,BreaksBeforeTrailingUnbreakableSequence)3168 TEST_F(FormatTestComments, BreaksBeforeTrailingUnbreakableSequence) {
3169   // The end of /* trail */ is exactly at 80 columns, but the unbreakable
3170   // trailing sequence ); after it exceeds the column limit. Make sure we
3171   // correctly break the line in that case.
3172   verifyFormat("int a =\n"
3173                "    foo(/* trail */);",
3174                getLLVMStyleWithColumns(23));
3175 }
3176 
TEST_F(FormatTestComments,ReflowBackslashCrash)3177 TEST_F(FormatTestComments, ReflowBackslashCrash) {
3178   // clang-format off
3179   EXPECT_EQ(
3180 "// How to run:\n"
3181 "// bbbbb run \\\n"
3182 "// rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr\n"
3183 "// \\ <log_file> -- --output_directory=\"<output_directory>\"",
3184   format(
3185 "// How to run:\n"
3186 "// bbbbb run \\\n"
3187 "// rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr \\\n"
3188 "// <log_file> -- --output_directory=\"<output_directory>\""));
3189   // clang-format on
3190 }
3191 
TEST_F(FormatTestComments,IndentsLongJavadocAnnotatedLines)3192 TEST_F(FormatTestComments, IndentsLongJavadocAnnotatedLines) {
3193   FormatStyle Style = getGoogleStyle(FormatStyle::LK_Java);
3194   Style.ColumnLimit = 60;
3195   FormatStyle Style20 = getGoogleStyle(FormatStyle::LK_Java);
3196   Style20.ColumnLimit = 20;
3197   EXPECT_EQ(
3198       "/**\n"
3199       " * @param x long long long long long long long long long\n"
3200       " *     long\n"
3201       " */\n",
3202       format("/**\n"
3203              " * @param x long long long long long long long long long long\n"
3204              " */\n",
3205              Style));
3206   EXPECT_EQ("/**\n"
3207             " * @param x long long long long long long long long long\n"
3208             " *     long long long long long long long long long long\n"
3209             " */\n",
3210             format("/**\n"
3211                    " * @param x long long long long long long long long long "
3212                    "long long long long long long long long long long\n"
3213                    " */\n",
3214                    Style));
3215   EXPECT_EQ("/**\n"
3216             " * @param x long long long long long long long long long\n"
3217             " *     long long long long long long long long long long\n"
3218             " *     long\n"
3219             " */\n",
3220             format("/**\n"
3221                    " * @param x long long long long long long long long long "
3222                    "long long long long long long long long long long long\n"
3223                    " */\n",
3224                    Style));
3225   EXPECT_EQ("/**\n"
3226             " * Sentence that\n"
3227             " * should be broken.\n"
3228             " * @param short\n"
3229             " * keep indentation\n"
3230             " */\n",
3231             format("/**\n"
3232                    " * Sentence that should be broken.\n"
3233                    " * @param short\n"
3234                    " * keep indentation\n"
3235                    " */\n",
3236                    Style20));
3237 
3238   EXPECT_EQ("/**\n"
3239             " * @param l1 long1\n"
3240             " *     to break\n"
3241             " * @param l2 long2\n"
3242             " *     to break\n"
3243             " */\n",
3244             format("/**\n"
3245                    " * @param l1 long1 to break\n"
3246                    " * @param l2 long2 to break\n"
3247                    " */\n",
3248                    Style20));
3249 
3250   EXPECT_EQ("/**\n"
3251             " * @param xx to\n"
3252             " *     break\n"
3253             " * no reflow\n"
3254             " */\n",
3255             format("/**\n"
3256                    " * @param xx to break\n"
3257                    " * no reflow\n"
3258                    " */\n",
3259                    Style20));
3260 
3261   EXPECT_EQ("/**\n"
3262             " * @param xx to\n"
3263             " *     break yes\n"
3264             " *     reflow\n"
3265             " */\n",
3266             format("/**\n"
3267                    " * @param xx to break\n"
3268                    " *     yes reflow\n"
3269                    " */\n",
3270                    Style20));
3271 
3272   FormatStyle JSStyle20 = getGoogleStyle(FormatStyle::LK_JavaScript);
3273   JSStyle20.ColumnLimit = 20;
3274   EXPECT_EQ("/**\n"
3275             " * @param l1 long1\n"
3276             " *     to break\n"
3277             " */\n",
3278             format("/**\n"
3279                    " * @param l1 long1 to break\n"
3280                    " */\n",
3281                    JSStyle20));
3282   EXPECT_EQ("/**\n"
3283             " * @param {l1 long1\n"
3284             " *     to break}\n"
3285             " */\n",
3286             format("/**\n"
3287                    " * @param {l1 long1 to break}\n"
3288                    " */\n",
3289                    JSStyle20));
3290 }
3291 
TEST_F(FormatTestComments,SpaceAtLineCommentBegin)3292 TEST_F(FormatTestComments, SpaceAtLineCommentBegin) {
3293   FormatStyle Style = getLLVMStyle();
3294   StringRef NoTextInComment = " //       \n"
3295                               "\n"
3296                               "void foo() {// \n"
3297                               "// \n"
3298                               "}";
3299 
3300   EXPECT_EQ("//\n"
3301             "\n"
3302             "void foo() { //\n"
3303             "  //\n"
3304             "}",
3305             format(NoTextInComment, Style));
3306 
3307   Style.SpacesInLineCommentPrefix.Minimum = 0;
3308   EXPECT_EQ("//#comment", format("//#comment", Style));
3309   EXPECT_EQ("//\n"
3310             "\n"
3311             "void foo() { //\n"
3312             "  //\n"
3313             "}",
3314             format(NoTextInComment, Style));
3315 
3316   Style.SpacesInLineCommentPrefix.Minimum = 5;
3317   EXPECT_EQ("//     #comment", format("//#comment", Style));
3318   EXPECT_EQ("//\n"
3319             "\n"
3320             "void foo() { //\n"
3321             "  //\n"
3322             "}",
3323             format(NoTextInComment, Style));
3324 
3325   Style = getLLVMStyle();
3326   StringRef Code =
3327       "//Free comment without space\n"
3328       "\n"
3329       "//   Free comment with 3 spaces\n"
3330       "\n"
3331       "///Free Doxygen without space\n"
3332       "\n"
3333       "///   Free Doxygen with 3 spaces\n"
3334       "\n"
3335       "//�� A nice dragon\n"
3336       "\n"
3337       "//\t abccba\n"
3338       "\n"
3339       "//\\t deffed\n"
3340       "\n"
3341       "//   �� Another nice dragon\n"
3342       "\n"
3343       "//   \t Three leading spaces following tab\n"
3344       "\n"
3345       "//   \\t Three leading spaces following backslash\n"
3346       "\n"
3347       "/// A Doxygen Comment with a nested list:\n"
3348       "/// - Foo\n"
3349       "/// - Bar\n"
3350       "///   - Baz\n"
3351       "///   - End\n"
3352       "///     of the inner list\n"
3353       "///   .\n"
3354       "/// .\n"
3355       "\n"
3356       "namespace Foo {\n"
3357       "bool bar(bool b) {\n"
3358       "  bool ret1 = true; ///<Doxygenstyle without space\n"
3359       "  bool ret2 = true; ///<   Doxygenstyle with 3 spaces\n"
3360       "  if (b) {\n"
3361       "    //Foo\n"
3362       "\n"
3363       "    //   In function comment\n"
3364       "    ret2 = false;\n"
3365       "  } // End of if\n"
3366       "\n"
3367       "//  if (ret1) {\n" // Commented out at the beginning of the line
3368       "//    return ret2;\n"
3369       "//  }\n"
3370       "\n"
3371       "  //if (ret1) {\n" // Commtented out at the beginning of the content
3372       "  //  return ret2;\n"
3373       "  //}\n"
3374       "\n"
3375       "  return ret1 && ret2;\n"
3376       "}\n"
3377       "}\n"
3378       "\n"
3379       "namespace Bar {\n"
3380       "int foo();\n"
3381       "} //  namespace Bar\n"
3382       "//@Nothing added because of the non ascii char\n"
3383       "\n"
3384       "//@      Nothing removed because of the non ascii char\n"
3385       "\n"
3386       "//  Comment to move to the left\n"
3387       "//But not this?\n"
3388       "//  @but this\n"
3389       "\n"
3390       "//Comment to move to the right\n"
3391       "//@ this stays\n"
3392       "\n"
3393       "//} will not move\n"
3394       "\n"
3395       "//vv will only move\n"
3396       "//} if the line above does\n";
3397 
3398   EXPECT_EQ("// Free comment without space\n"
3399             "\n"
3400             "//   Free comment with 3 spaces\n"
3401             "\n"
3402             "/// Free Doxygen without space\n"
3403             "\n"
3404             "///   Free Doxygen with 3 spaces\n"
3405             "\n"
3406             "// �� A nice dragon\n"
3407             "\n"
3408             "//\t abccba\n"
3409             "\n"
3410             "//\\t deffed\n"
3411             "\n"
3412             "//   �� Another nice dragon\n"
3413             "\n"
3414             "//   \t Three leading spaces following tab\n"
3415             "\n"
3416             "//   \\t Three leading spaces following backslash\n"
3417             "\n"
3418             "/// A Doxygen Comment with a nested list:\n"
3419             "/// - Foo\n"
3420             "/// - Bar\n"
3421             "///   - Baz\n"
3422             "///   - End\n"
3423             "///     of the inner list\n"
3424             "///   .\n"
3425             "/// .\n"
3426             "\n"
3427             "namespace Foo {\n"
3428             "bool bar(bool b) {\n"
3429             "  bool ret1 = true; ///< Doxygenstyle without space\n"
3430             "  bool ret2 = true; ///<   Doxygenstyle with 3 spaces\n"
3431             "  if (b) {\n"
3432             "    // Foo\n"
3433             "\n"
3434             "    //   In function comment\n"
3435             "    ret2 = false;\n"
3436             "  } // End of if\n"
3437             "\n"
3438             "  //  if (ret1) {\n"
3439             "  //    return ret2;\n"
3440             "  //  }\n"
3441             "\n"
3442             "  // if (ret1) {\n"
3443             "  //   return ret2;\n"
3444             "  // }\n"
3445             "\n"
3446             "  return ret1 && ret2;\n"
3447             "}\n"
3448             "} // namespace Foo\n"
3449             "\n"
3450             "namespace Bar {\n"
3451             "int foo();\n"
3452             "} //  namespace Bar\n"
3453             "//@Nothing added because of the non ascii char\n"
3454             "\n"
3455             "//@      Nothing removed because of the non ascii char\n"
3456             "\n"
3457             "//  Comment to move to the left\n"
3458             "// But not this?\n"
3459             "//  @but this\n"
3460             "\n"
3461             "// Comment to move to the right\n"
3462             "//@ this stays\n"
3463             "\n"
3464             "//} will not move\n"
3465             "\n"
3466             "// vv will only move\n"
3467             "// } if the line above does\n",
3468             format(Code, Style));
3469 
3470   Style.SpacesInLineCommentPrefix = {0, 0};
3471   EXPECT_EQ("//#comment", format("//   #comment", Style));
3472   EXPECT_EQ("//Free comment without space\n"
3473             "\n"
3474             "//Free comment with 3 spaces\n"
3475             "\n"
3476             "///Free Doxygen without space\n"
3477             "\n"
3478             "///Free Doxygen with 3 spaces\n"
3479             "\n"
3480             "//�� A nice dragon\n"
3481             "\n"
3482             "//\t abccba\n"
3483             "\n"
3484             "//\\t deffed\n"
3485             "\n"
3486             "//�� Another nice dragon\n"
3487             "\n"
3488             "//\t Three leading spaces following tab\n"
3489             "\n"
3490             "//\\t Three leading spaces following backslash\n"
3491             "\n"
3492             "///A Doxygen Comment with a nested list:\n"
3493             "///- Foo\n"
3494             "///- Bar\n"
3495             "///  - Baz\n" // Here we keep the relative indentation
3496             "///  - End\n"
3497             "///    of the inner list\n"
3498             "///  .\n"
3499             "///.\n"
3500             "\n"
3501             "namespace Foo {\n"
3502             "bool bar(bool b) {\n"
3503             "  bool ret1 = true; ///<Doxygenstyle without space\n"
3504             "  bool ret2 = true; ///<Doxygenstyle with 3 spaces\n"
3505             "  if (b) {\n"
3506             "    //Foo\n"
3507             "\n"
3508             "    //In function comment\n"
3509             "    ret2 = false;\n"
3510             "  } //End of if\n"
3511             "\n"
3512             "  //if (ret1) {\n"
3513             "  //  return ret2;\n"
3514             "  //}\n"
3515             "\n"
3516             "  //if (ret1) {\n"
3517             "  //  return ret2;\n"
3518             "  //}\n"
3519             "\n"
3520             "  return ret1 && ret2;\n"
3521             "}\n"
3522             "} //namespace Foo\n"
3523             "\n"
3524             "namespace Bar {\n"
3525             "int foo();\n"
3526             "} //namespace Bar\n"
3527             "//@Nothing added because of the non ascii char\n"
3528             "\n"
3529             "//@      Nothing removed because of the non ascii char\n"
3530             "\n"
3531             "//Comment to move to the left\n"
3532             "//But not this?\n"
3533             "//@but this\n"
3534             "\n"
3535             "//Comment to move to the right\n"
3536             "//@ this stays\n"
3537             "\n"
3538             "//} will not move\n"
3539             "\n"
3540             "//vv will only move\n"
3541             "//} if the line above does\n",
3542             format(Code, Style));
3543 
3544   Style.SpacesInLineCommentPrefix = {2, -1u};
3545   EXPECT_EQ("//  Free comment without space\n"
3546             "\n"
3547             "//   Free comment with 3 spaces\n"
3548             "\n"
3549             "///  Free Doxygen without space\n"
3550             "\n"
3551             "///   Free Doxygen with 3 spaces\n"
3552             "\n"
3553             "//  �� A nice dragon\n"
3554             "\n"
3555             "//\t abccba\n"
3556             "\n"
3557             "//\\t deffed\n"
3558             "\n"
3559             "//   �� Another nice dragon\n"
3560             "\n"
3561             "//   \t Three leading spaces following tab\n"
3562             "\n"
3563             "//   \\t Three leading spaces following backslash\n"
3564             "\n"
3565             "///  A Doxygen Comment with a nested list:\n"
3566             "///  - Foo\n"
3567             "///  - Bar\n"
3568             "///    - Baz\n"
3569             "///    - End\n"
3570             "///      of the inner list\n"
3571             "///    .\n"
3572             "///  .\n"
3573             "\n"
3574             "namespace Foo {\n"
3575             "bool bar(bool b) {\n"
3576             "  bool ret1 = true; ///<  Doxygenstyle without space\n"
3577             "  bool ret2 = true; ///<   Doxygenstyle with 3 spaces\n"
3578             "  if (b) {\n"
3579             "    //  Foo\n"
3580             "\n"
3581             "    //   In function comment\n"
3582             "    ret2 = false;\n"
3583             "  } //  End of if\n"
3584             "\n"
3585             "  //  if (ret1) {\n"
3586             "  //    return ret2;\n"
3587             "  //  }\n"
3588             "\n"
3589             "  //  if (ret1) {\n"
3590             "  //    return ret2;\n"
3591             "  //  }\n"
3592             "\n"
3593             "  return ret1 && ret2;\n"
3594             "}\n"
3595             "} //  namespace Foo\n"
3596             "\n"
3597             "namespace Bar {\n"
3598             "int foo();\n"
3599             "} //  namespace Bar\n"
3600             "//@Nothing added because of the non ascii char\n"
3601             "\n"
3602             "//@      Nothing removed because of the non ascii char\n"
3603             "\n"
3604             "//  Comment to move to the left\n"
3605             "//  But not this?\n"
3606             "//  @but this\n"
3607             "\n"
3608             "//  Comment to move to the right\n"
3609             "//@ this stays\n"
3610             "\n"
3611             "//} will not move\n"
3612             "\n"
3613             "//  vv will only move\n"
3614             "//  } if the line above does\n",
3615             format(Code, Style));
3616 
3617   Style = getLLVMStyleWithColumns(20);
3618   StringRef WrapCode = "//Lorem ipsum dolor sit amet\n"
3619                        "\n"
3620                        "//  Lorem   ipsum   dolor   sit   amet\n"
3621                        "\n"
3622                        "void f() {//Hello World\n"
3623                        "}";
3624 
3625   EXPECT_EQ("// Lorem ipsum dolor\n"
3626             "// sit amet\n"
3627             "\n"
3628             "//  Lorem   ipsum\n"
3629             "//  dolor   sit amet\n"
3630             "\n"
3631             "void f() { // Hello\n"
3632             "           // World\n"
3633             "}",
3634             format(WrapCode, Style));
3635 
3636   Style.SpacesInLineCommentPrefix = {0, 0};
3637   EXPECT_EQ("//Lorem ipsum dolor\n"
3638             "//sit amet\n"
3639             "\n"
3640             "//Lorem   ipsum\n"
3641             "//dolor   sit   amet\n"
3642             "\n"
3643             "void f() { //Hello\n"
3644             "           //World\n"
3645             "}",
3646             format(WrapCode, Style));
3647 
3648   Style.SpacesInLineCommentPrefix = {1, 1};
3649   EXPECT_EQ("// Lorem ipsum dolor\n"
3650             "// sit amet\n"
3651             "\n"
3652             "// Lorem   ipsum\n"
3653             "// dolor   sit amet\n"
3654             "\n"
3655             "void f() { // Hello\n"
3656             "           // World\n"
3657             "}",
3658             format(WrapCode, Style));
3659   EXPECT_EQ("// x\n"
3660             "// y",
3661             format("//   x\n"
3662                    "// y",
3663                    Style));
3664   EXPECT_EQ(
3665       "// loooooooooooooooooooooooooooooong\n"
3666       "// commentcomments\n"
3667       "// normal comments",
3668       format("//            loooooooooooooooooooooooooooooong commentcomments\n"
3669              "// normal comments",
3670              Style));
3671 
3672   Style.SpacesInLineCommentPrefix = {3, 3};
3673   EXPECT_EQ("//   Lorem ipsum\n"
3674             "//   dolor sit amet\n"
3675             "\n"
3676             "//   Lorem   ipsum\n"
3677             "//   dolor   sit\n"
3678             "//   amet\n"
3679             "\n"
3680             "void f() { //   Hello\n"
3681             "           //   World\n"
3682             "}",
3683             format(WrapCode, Style));
3684 
3685   Style = getLLVMStyleWithColumns(20);
3686   StringRef LotsOfSpaces = "//                      This are more spaces "
3687                            "than the ColumnLimit, what now?\n"
3688                            "\n"
3689                            "//   Comment\n"
3690                            "\n"
3691                            "// This is a text to split in multiple "
3692                            "lines, please. Thank you very much!\n"
3693                            "\n"
3694                            "// A comment with\n"
3695                            "//   some indentation that has to be split.\n"
3696                            "// And now without";
3697   EXPECT_EQ("//                      This are more spaces "
3698             "than the ColumnLimit, what now?\n"
3699             "\n"
3700             "//   Comment\n"
3701             "\n"
3702             "// This is a text to\n"
3703             "// split in multiple\n"
3704             "// lines, please.\n"
3705             "// Thank you very\n"
3706             "// much!\n"
3707             "\n"
3708             "// A comment with\n"
3709             "//   some\n"
3710             "//   indentation\n"
3711             "//   that has to be\n"
3712             "//   split.\n"
3713             "// And now without",
3714             format(LotsOfSpaces, Style));
3715 
3716   Style.SpacesInLineCommentPrefix = {0, 0};
3717   EXPECT_EQ("//This are more\n"
3718             "//spaces than the\n"
3719             "//ColumnLimit, what\n"
3720             "//now?\n"
3721             "\n"
3722             "//Comment\n"
3723             "\n"
3724             "//This is a text to\n"
3725             "//split in multiple\n"
3726             "//lines, please.\n"
3727             "//Thank you very\n"
3728             "//much!\n"
3729             "\n"
3730             "//A comment with\n"
3731             "//  some indentation\n"
3732             "//  that has to be\n"
3733             "//  split.\n"
3734             "//And now without",
3735             format(LotsOfSpaces, Style));
3736 
3737   Style.SpacesInLineCommentPrefix = {3, 3};
3738   EXPECT_EQ("//   This are more\n"
3739             "//   spaces than the\n"
3740             "//   ColumnLimit,\n"
3741             "//   what now?\n"
3742             "\n"
3743             "//   Comment\n"
3744             "\n"
3745             "//   This is a text\n"
3746             "//   to split in\n"
3747             "//   multiple lines,\n"
3748             "//   please. Thank\n"
3749             "//   you very much!\n"
3750             "\n"
3751             "//   A comment with\n"
3752             "//     some\n"
3753             "//     indentation\n"
3754             "//     that has to\n"
3755             "//     be split.\n"
3756             "//   And now without",
3757             format(LotsOfSpaces, Style));
3758 
3759   Style.SpacesInLineCommentPrefix = {30, -1u};
3760   EXPECT_EQ("//                              This are more spaces than the "
3761             "ColumnLimit, what now?\n"
3762             "\n"
3763             "//                              Comment\n"
3764             "\n"
3765             "//                              This is a text to split in "
3766             "multiple lines, please. Thank you very much!\n"
3767             "\n"
3768             "//                              A comment with\n"
3769             "//                                some indentation that has to be "
3770             "split.\n"
3771             "//                              And now without",
3772             format(LotsOfSpaces, Style));
3773 
3774   Style.SpacesInLineCommentPrefix = {2, 4};
3775   EXPECT_EQ("//  A Comment to be\n"
3776             "//  moved\n"
3777             "//   with indent\n"
3778             "\n"
3779             "//  A Comment to be\n"
3780             "//  moved\n"
3781             "//   with indent\n"
3782             "\n"
3783             "//  A Comment to be\n"
3784             "//  moved\n"
3785             "//   with indent\n"
3786             "\n"
3787             "//   A Comment to be\n"
3788             "//   moved\n"
3789             "//    with indent\n"
3790             "\n"
3791             "//    A Comment to\n"
3792             "//    be moved\n"
3793             "//     with indent\n"
3794             "\n"
3795             "//    A Comment to\n"
3796             "//    be moved\n"
3797             "//     with indent\n"
3798             "\n"
3799             "//    A Comment to\n"
3800             "//    be moved\n"
3801             "//     with indent\n",
3802             format("//A Comment to be moved\n"
3803                    "// with indent\n"
3804                    "\n"
3805                    "// A Comment to be moved\n"
3806                    "//  with indent\n"
3807                    "\n"
3808                    "//  A Comment to be moved\n"
3809                    "//   with indent\n"
3810                    "\n"
3811                    "//   A Comment to be moved\n"
3812                    "//    with indent\n"
3813                    "\n"
3814                    "//    A Comment to be moved\n"
3815                    "//     with indent\n"
3816                    "\n"
3817                    "//     A Comment to be moved\n"
3818                    "//      with indent\n"
3819                    "\n"
3820                    "//      A Comment to be moved\n"
3821                    "//       with indent\n",
3822                    Style));
3823 
3824   Style.ColumnLimit = 30;
3825   EXPECT_EQ("int i; //  A Comment to be\n"
3826             "       //  moved\n"
3827             "       //   with indent\n"
3828             "\n"
3829             "int i; //  A Comment to be\n"
3830             "       //  moved\n"
3831             "       //   with indent\n"
3832             "\n"
3833             "int i; //  A Comment to be\n"
3834             "       //  moved\n"
3835             "       //   with indent\n"
3836             "\n"
3837             "int i; //   A Comment to be\n"
3838             "       //   moved\n"
3839             "       //    with indent\n"
3840             "\n"
3841             "int i; //    A Comment to be\n"
3842             "       //    moved\n"
3843             "       //     with indent\n"
3844             "\n"
3845             "int i; //    A Comment to be\n"
3846             "       //    moved\n"
3847             "       //     with indent\n"
3848             "\n"
3849             "int i; //    A Comment to be\n"
3850             "       //    moved\n"
3851             "       //     with indent\n",
3852             format("int i;//A Comment to be moved\n"
3853                    "      // with indent\n"
3854                    "\n"
3855                    "int i;// A Comment to be moved\n"
3856                    "      //  with indent\n"
3857                    "\n"
3858                    "int i;//  A Comment to be moved\n"
3859                    "      //   with indent\n"
3860                    "\n"
3861                    "int i;//   A Comment to be moved\n"
3862                    "      //    with indent\n"
3863                    "\n"
3864                    "int i;//    A Comment to be moved\n"
3865                    "      //     with indent\n"
3866                    "\n"
3867                    "int i;//     A Comment to be moved\n"
3868                    "      //      with indent\n"
3869                    "\n"
3870                    "int i;//      A Comment to be moved\n"
3871                    "      //       with indent\n",
3872                    Style));
3873 
3874   Style = getLLVMStyleWithColumns(0);
3875   EXPECT_EQ("// Free comment without space\n"
3876             "\n"
3877             "//   Free comment with 3 spaces\n"
3878             "\n"
3879             "/// Free Doxygen without space\n"
3880             "\n"
3881             "///   Free Doxygen with 3 spaces\n"
3882             "\n"
3883             "// �� A nice dragon\n"
3884             "\n"
3885             "//\t abccba\n"
3886             "\n"
3887             "//\\t deffed\n"
3888             "\n"
3889             "//   �� Another nice dragon\n"
3890             "\n"
3891             "//   \t Three leading spaces following tab\n"
3892             "\n"
3893             "//   \\t Three leading spaces following backslash\n"
3894             "\n"
3895             "/// A Doxygen Comment with a nested list:\n"
3896             "/// - Foo\n"
3897             "/// - Bar\n"
3898             "///   - Baz\n"
3899             "///   - End\n"
3900             "///     of the inner list\n"
3901             "///   .\n"
3902             "/// .\n"
3903             "\n"
3904             "namespace Foo {\n"
3905             "bool bar(bool b) {\n"
3906             "  bool ret1 = true; ///< Doxygenstyle without space\n"
3907             "  bool ret2 = true; ///<   Doxygenstyle with 3 spaces\n"
3908             "  if (b) {\n"
3909             "    // Foo\n"
3910             "\n"
3911             "    //   In function comment\n"
3912             "    ret2 = false;\n"
3913             "  } // End of if\n"
3914             "\n"
3915             "  //  if (ret1) {\n"
3916             "  //    return ret2;\n"
3917             "  //  }\n"
3918             "\n"
3919             "  // if (ret1) {\n"
3920             "  //   return ret2;\n"
3921             "  // }\n"
3922             "\n"
3923             "  return ret1 && ret2;\n"
3924             "}\n"
3925             "} // namespace Foo\n"
3926             "\n"
3927             "namespace Bar {\n"
3928             "int foo();\n"
3929             "} //  namespace Bar\n"
3930             "//@Nothing added because of the non ascii char\n"
3931             "\n"
3932             "//@      Nothing removed because of the non ascii char\n"
3933             "\n"
3934             "//  Comment to move to the left\n"
3935             "// But not this?\n"
3936             "//  @but this\n"
3937             "\n"
3938             "// Comment to move to the right\n"
3939             "//@ this stays\n"
3940             "\n"
3941             "//} will not move\n"
3942             "\n"
3943             "// vv will only move\n"
3944             "// } if the line above does\n",
3945             format(Code, Style));
3946 
3947   Style.SpacesInLineCommentPrefix = {0, 0};
3948   EXPECT_EQ("//Free comment without space\n"
3949             "\n"
3950             "//Free comment with 3 spaces\n"
3951             "\n"
3952             "///Free Doxygen without space\n"
3953             "\n"
3954             "///Free Doxygen with 3 spaces\n"
3955             "\n"
3956             "//�� A nice dragon\n"
3957             "\n"
3958             "//\t abccba\n"
3959             "\n"
3960             "//\\t deffed\n"
3961             "\n"
3962             "//�� Another nice dragon\n"
3963             "\n"
3964             "//\t Three leading spaces following tab\n"
3965             "\n"
3966             "//\\t Three leading spaces following backslash\n"
3967             "\n"
3968             "///A Doxygen Comment with a nested list:\n"
3969             "///- Foo\n"
3970             "///- Bar\n"
3971             "///  - Baz\n" // Here we keep the relative indentation
3972             "///  - End\n"
3973             "///    of the inner list\n"
3974             "///  .\n"
3975             "///.\n"
3976             "\n"
3977             "namespace Foo {\n"
3978             "bool bar(bool b) {\n"
3979             "  bool ret1 = true; ///<Doxygenstyle without space\n"
3980             "  bool ret2 = true; ///<Doxygenstyle with 3 spaces\n"
3981             "  if (b) {\n"
3982             "    //Foo\n"
3983             "\n"
3984             "    //In function comment\n"
3985             "    ret2 = false;\n"
3986             "  } //End of if\n"
3987             "\n"
3988             "  //if (ret1) {\n"
3989             "  //  return ret2;\n"
3990             "  //}\n"
3991             "\n"
3992             "  //if (ret1) {\n"
3993             "  //  return ret2;\n"
3994             "  //}\n"
3995             "\n"
3996             "  return ret1 && ret2;\n"
3997             "}\n"
3998             "} //namespace Foo\n"
3999             "\n"
4000             "namespace Bar {\n"
4001             "int foo();\n"
4002             "} //namespace Bar\n"
4003             "//@Nothing added because of the non ascii char\n"
4004             "\n"
4005             "//@      Nothing removed because of the non ascii char\n"
4006             "\n"
4007             "//Comment to move to the left\n"
4008             "//But not this?\n"
4009             "//@but this\n"
4010             "\n"
4011             "//Comment to move to the right\n"
4012             "//@ this stays\n"
4013             "\n"
4014             "//} will not move\n"
4015             "\n"
4016             "//vv will only move\n"
4017             "//} if the line above does\n",
4018             format(Code, Style));
4019 
4020   Style.SpacesInLineCommentPrefix = {2, -1u};
4021   EXPECT_EQ("//  Free comment without space\n"
4022             "\n"
4023             "//   Free comment with 3 spaces\n"
4024             "\n"
4025             "///  Free Doxygen without space\n"
4026             "\n"
4027             "///   Free Doxygen with 3 spaces\n"
4028             "\n"
4029             "//  �� A nice dragon\n"
4030             "\n"
4031             "//\t abccba\n"
4032             "\n"
4033             "//\\t deffed\n"
4034             "\n"
4035             "//   �� Another nice dragon\n"
4036             "\n"
4037             "//   \t Three leading spaces following tab\n"
4038             "\n"
4039             "//   \\t Three leading spaces following backslash\n"
4040             "\n"
4041             "///  A Doxygen Comment with a nested list:\n"
4042             "///  - Foo\n"
4043             "///  - Bar\n"
4044             "///    - Baz\n"
4045             "///    - End\n"
4046             "///      of the inner list\n"
4047             "///    .\n"
4048             "///  .\n"
4049             "\n"
4050             "namespace Foo {\n"
4051             "bool bar(bool b) {\n"
4052             "  bool ret1 = true; ///<  Doxygenstyle without space\n"
4053             "  bool ret2 = true; ///<   Doxygenstyle with 3 spaces\n"
4054             "  if (b) {\n"
4055             "    //  Foo\n"
4056             "\n"
4057             "    //   In function comment\n"
4058             "    ret2 = false;\n"
4059             "  } //  End of if\n"
4060             "\n"
4061             "  //  if (ret1) {\n"
4062             "  //    return ret2;\n"
4063             "  //  }\n"
4064             "\n"
4065             "  //  if (ret1) {\n"
4066             "  //    return ret2;\n"
4067             "  //  }\n"
4068             "\n"
4069             "  return ret1 && ret2;\n"
4070             "}\n"
4071             "} //  namespace Foo\n"
4072             "\n"
4073             "namespace Bar {\n"
4074             "int foo();\n"
4075             "} //  namespace Bar\n"
4076             "//@Nothing added because of the non ascii char\n"
4077             "\n"
4078             "//@      Nothing removed because of the non ascii char\n"
4079             "\n"
4080             "//  Comment to move to the left\n"
4081             "//  But not this?\n"
4082             "//  @but this\n"
4083             "\n"
4084             "//  Comment to move to the right\n"
4085             "//@ this stays\n"
4086             "\n"
4087             "//} will not move\n"
4088             "\n"
4089             "//  vv will only move\n"
4090             "//  } if the line above does\n",
4091             format(Code, Style));
4092 }
4093 
TEST_F(FormatTestComments,SplitCommentIntroducers)4094 TEST_F(FormatTestComments, SplitCommentIntroducers) {
4095   EXPECT_EQ(R"(//
4096 /\
4097 /
4098 )",
4099             format(R"(//
4100 /\
4101 /
4102   )",
4103                    getLLVMStyleWithColumns(10)));
4104 }
4105 
4106 } // end namespace
4107 } // end namespace format
4108 } // end namespace clang
4109