1 //===- unittest/Format/FormatTestComments.cpp - Formatting unit tests -----===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "clang/Format/Format.h"
11 
12 #include "../Tooling/ReplacementTest.h"
13 #include "FormatTestUtils.h"
14 
15 #include "clang/Frontend/TextDiagnosticPrinter.h"
16 #include "llvm/Support/Debug.h"
17 #include "llvm/Support/MemoryBuffer.h"
18 #include "gtest/gtest.h"
19 
20 #define DEBUG_TYPE "format-test"
21 
22 using clang::tooling::ReplacementTest;
23 
24 namespace clang {
25 namespace format {
26 namespace {
27 
getGoogleStyle()28 FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); }
29 
30 class FormatTestComments : public ::testing::Test {
31 protected:
32   enum StatusCheck {
33     SC_ExpectComplete,
34     SC_ExpectIncomplete,
35     SC_DoNotCheck
36   };
37 
format(llvm::StringRef Code,const FormatStyle & Style=getLLVMStyle (),StatusCheck CheckComplete=SC_ExpectComplete)38   std::string format(llvm::StringRef Code,
39                      const FormatStyle &Style = getLLVMStyle(),
40                      StatusCheck CheckComplete = SC_ExpectComplete) {
41     LLVM_DEBUG(llvm::errs() << "---\n");
42     LLVM_DEBUG(llvm::errs() << Code << "\n\n");
43     std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
44     FormattingAttemptStatus Status;
45     tooling::Replacements Replaces =
46         reformat(Style, Code, Ranges, "<stdin>", &Status);
47     if (CheckComplete != SC_DoNotCheck) {
48       bool ExpectedCompleteFormat = CheckComplete == SC_ExpectComplete;
49       EXPECT_EQ(ExpectedCompleteFormat, Status.FormatComplete)
50           << Code << "\n\n";
51     }
52     ReplacementCount = Replaces.size();
53     auto Result = applyAllReplacements(Code, Replaces);
54     EXPECT_TRUE(static_cast<bool>(Result));
55     LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
56     return *Result;
57   }
58 
getLLVMStyleWithColumns(unsigned ColumnLimit)59   FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
60     FormatStyle Style = getLLVMStyle();
61     Style.ColumnLimit = ColumnLimit;
62     return Style;
63   }
64 
getTextProtoStyleWithColumns(unsigned ColumnLimit)65   FormatStyle getTextProtoStyleWithColumns(unsigned ColumnLimit) {
66     FormatStyle Style = getGoogleStyle(FormatStyle::FormatStyle::LK_TextProto);
67     Style.ColumnLimit = ColumnLimit;
68     return Style;
69   }
70 
verifyFormat(llvm::StringRef Code,const FormatStyle & Style=getLLVMStyle ())71   void verifyFormat(llvm::StringRef Code,
72                     const FormatStyle &Style = getLLVMStyle()) {
73     EXPECT_EQ(Code.str(), format(Code, Style)) << "Expected code is not stable";
74     EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
75   }
76 
verifyGoogleFormat(llvm::StringRef Code)77   void verifyGoogleFormat(llvm::StringRef Code) {
78     verifyFormat(Code, getGoogleStyle());
79   }
80 
81   /// \brief Verify that clang-format does not crash on the given input.
verifyNoCrash(llvm::StringRef Code,const FormatStyle & Style=getLLVMStyle ())82   void verifyNoCrash(llvm::StringRef Code,
83                      const FormatStyle &Style = getLLVMStyle()) {
84     format(Code, Style, SC_DoNotCheck);
85   }
86 
87   int ReplacementCount;
88 };
89 
90 //===----------------------------------------------------------------------===//
91 // Tests for comments.
92 //===----------------------------------------------------------------------===//
93 
TEST_F(FormatTestComments,UnderstandsSingleLineComments)94 TEST_F(FormatTestComments, UnderstandsSingleLineComments) {
95   verifyFormat("//* */");
96   verifyFormat("// line 1\n"
97                "// line 2\n"
98                "void f() {}\n");
99 
100   verifyFormat("void f() {\n"
101                "  // Doesn't do anything\n"
102                "}");
103   verifyFormat("SomeObject\n"
104                "    // Calling someFunction on SomeObject\n"
105                "    .someFunction();");
106   verifyFormat("auto result = SomeObject\n"
107                "                  // Calling someFunction on SomeObject\n"
108                "                  .someFunction();");
109   verifyFormat("void f(int i,  // some comment (probably for i)\n"
110                "       int j,  // some comment (probably for j)\n"
111                "       int k); // some comment (probably for k)");
112   verifyFormat("void f(int i,\n"
113                "       // some comment (probably for j)\n"
114                "       int j,\n"
115                "       // some comment (probably for k)\n"
116                "       int k);");
117 
118   verifyFormat("int i    // This is a fancy variable\n"
119                "    = 5; // with nicely aligned comment.");
120 
121   verifyFormat("// Leading comment.\n"
122                "int a; // Trailing comment.");
123   verifyFormat("int a; // Trailing comment\n"
124                "       // on 2\n"
125                "       // or 3 lines.\n"
126                "int b;");
127   verifyFormat("int a; // Trailing comment\n"
128                "\n"
129                "// Leading comment.\n"
130                "int b;");
131   verifyFormat("int a;    // Comment.\n"
132                "          // More details.\n"
133                "int bbbb; // Another comment.");
134   verifyFormat(
135       "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
136       "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;   // comment\n"
137       "int cccccccccccccccccccccccccccccc;       // comment\n"
138       "int ddd;                     // looooooooooooooooooooooooong comment\n"
139       "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
140       "int bbbbbbbbbbbbbbbbbbbbb;   // comment\n"
141       "int ccccccccccccccccccc;     // comment");
142 
143   verifyFormat("#include \"a\"     // comment\n"
144                "#include \"a/b/c\" // comment");
145   verifyFormat("#include <a>     // comment\n"
146                "#include <a/b/c> // comment");
147   EXPECT_EQ("#include \"a\"     // comment\n"
148             "#include \"a/b/c\" // comment",
149             format("#include \\\n"
150                    "  \"a\" // comment\n"
151                    "#include \"a/b/c\" // comment"));
152 
153   verifyFormat("enum E {\n"
154                "  // comment\n"
155                "  VAL_A, // comment\n"
156                "  VAL_B\n"
157                "};");
158 
159   EXPECT_EQ("enum A {\n"
160             "  // line a\n"
161             "  a,\n"
162             "  b, // line b\n"
163             "\n"
164             "  // line c\n"
165             "  c\n"
166             "};",
167             format("enum A {\n"
168                    "  // line a\n"
169                    "  a,\n"
170                    "  b, // line b\n"
171                    "\n"
172                    "  // line c\n"
173                    "  c\n"
174                    "};",
175                    getLLVMStyleWithColumns(20)));
176   EXPECT_EQ("enum A {\n"
177             "  a, // line 1\n"
178             "  // line 2\n"
179             "};",
180             format("enum A {\n"
181                    "  a, // line 1\n"
182                    "  // line 2\n"
183                    "};",
184                    getLLVMStyleWithColumns(20)));
185   EXPECT_EQ("enum A {\n"
186             "  a, // line 1\n"
187             "     // line 2\n"
188             "};",
189             format("enum A {\n"
190                    "  a, // line 1\n"
191                    "   // line 2\n"
192                    "};",
193                    getLLVMStyleWithColumns(20)));
194   EXPECT_EQ("enum A {\n"
195             "  a, // line 1\n"
196             "  // line 2\n"
197             "  b\n"
198             "};",
199             format("enum A {\n"
200                    "  a, // line 1\n"
201                    "  // line 2\n"
202                    "  b\n"
203                    "};",
204                    getLLVMStyleWithColumns(20)));
205   EXPECT_EQ("enum A {\n"
206             "  a, // line 1\n"
207             "     // line 2\n"
208             "  b\n"
209             "};",
210             format("enum A {\n"
211                    "  a, // line 1\n"
212                    "   // line 2\n"
213                    "  b\n"
214                    "};",
215                    getLLVMStyleWithColumns(20)));
216   verifyFormat(
217       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
218       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
219   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
220                "    // Comment inside a statement.\n"
221                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
222   verifyFormat("SomeFunction(a,\n"
223                "             // comment\n"
224                "             b + x);");
225   verifyFormat("SomeFunction(a, a,\n"
226                "             // comment\n"
227                "             b + x);");
228   verifyFormat(
229       "bool aaaaaaaaaaaaa = // comment\n"
230       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
231       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
232 
233   verifyFormat("int aaaa; // aaaaa\n"
234                "int aa;   // aaaaaaa",
235                getLLVMStyleWithColumns(20));
236 
237   EXPECT_EQ("void f() { // This does something ..\n"
238             "}\n"
239             "int a; // This is unrelated",
240             format("void f()    {     // This does something ..\n"
241                    "  }\n"
242                    "int   a;     // This is unrelated"));
243   EXPECT_EQ("class C {\n"
244             "  void f() { // This does something ..\n"
245             "  }          // awesome..\n"
246             "\n"
247             "  int a; // This is unrelated\n"
248             "};",
249             format("class C{void f()    { // This does something ..\n"
250                    "      } // awesome..\n"
251                    " \n"
252                    "int a;    // This is unrelated\n"
253                    "};"));
254 
255   EXPECT_EQ("int i; // single line trailing comment",
256             format("int i;\\\n// single line trailing comment"));
257 
258   verifyGoogleFormat("int a;  // Trailing comment.");
259 
260   verifyFormat("someFunction(anotherFunction( // Force break.\n"
261                "    parameter));");
262 
263   verifyGoogleFormat("#endif  // HEADER_GUARD");
264 
265   verifyFormat("const char *test[] = {\n"
266                "    // A\n"
267                "    \"aaaa\",\n"
268                "    // B\n"
269                "    \"aaaaa\"};");
270   verifyGoogleFormat(
271       "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
272       "    aaaaaaaaaaaaaaaaaaaaaa);  // 81_cols_with_this_comment");
273   EXPECT_EQ("D(a, {\n"
274             "  // test\n"
275             "  int a;\n"
276             "});",
277             format("D(a, {\n"
278                    "// test\n"
279                    "int a;\n"
280                    "});"));
281 
282   EXPECT_EQ("lineWith(); // comment\n"
283             "// at start\n"
284             "otherLine();",
285             format("lineWith();   // comment\n"
286                    "// at start\n"
287                    "otherLine();"));
288   EXPECT_EQ("lineWith(); // comment\n"
289             "/*\n"
290             " * at start */\n"
291             "otherLine();",
292             format("lineWith();   // comment\n"
293                    "/*\n"
294                    " * at start */\n"
295                    "otherLine();"));
296   EXPECT_EQ("lineWith(); // comment\n"
297             "            // at start\n"
298             "otherLine();",
299             format("lineWith();   // comment\n"
300                    " // at start\n"
301                    "otherLine();"));
302 
303   EXPECT_EQ("lineWith(); // comment\n"
304             "// at start\n"
305             "otherLine(); // comment",
306             format("lineWith();   // comment\n"
307                    "// at start\n"
308                    "otherLine();   // comment"));
309   EXPECT_EQ("lineWith();\n"
310             "// at start\n"
311             "otherLine(); // comment",
312             format("lineWith();\n"
313                    " // at start\n"
314                    "otherLine();   // comment"));
315   EXPECT_EQ("// first\n"
316             "// at start\n"
317             "otherLine(); // comment",
318             format("// first\n"
319                    " // at start\n"
320                    "otherLine();   // comment"));
321   EXPECT_EQ("f();\n"
322             "// first\n"
323             "// at start\n"
324             "otherLine(); // comment",
325             format("f();\n"
326                    "// first\n"
327                    " // at start\n"
328                    "otherLine();   // comment"));
329   verifyFormat("f(); // comment\n"
330                "// first\n"
331                "// at start\n"
332                "otherLine();");
333   EXPECT_EQ("f(); // comment\n"
334             "// first\n"
335             "// at start\n"
336             "otherLine();",
337             format("f();   // comment\n"
338                    "// first\n"
339                    " // at start\n"
340                    "otherLine();"));
341   EXPECT_EQ("f(); // comment\n"
342             "     // first\n"
343             "// at start\n"
344             "otherLine();",
345             format("f();   // comment\n"
346                    " // first\n"
347                    "// at start\n"
348                    "otherLine();"));
349   EXPECT_EQ("void f() {\n"
350             "  lineWith(); // comment\n"
351             "  // at start\n"
352             "}",
353             format("void              f() {\n"
354                    "  lineWith(); // comment\n"
355                    "  // at start\n"
356                    "}"));
357   EXPECT_EQ("int xy; // a\n"
358             "int z;  // b",
359             format("int xy;    // a\n"
360                    "int z;    //b"));
361   EXPECT_EQ("int xy; // a\n"
362             "int z; // bb",
363             format("int xy;    // a\n"
364                    "int z;    //bb",
365                    getLLVMStyleWithColumns(12)));
366 
367   verifyFormat("#define A                                                  \\\n"
368                "  int i; /* iiiiiiiiiiiiiiiiiiiii */                       \\\n"
369                "  int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
370                getLLVMStyleWithColumns(60));
371   verifyFormat(
372       "#define A                                                   \\\n"
373       "  int i;                        /* iiiiiiiiiiiiiiiiiiiii */ \\\n"
374       "  int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
375       getLLVMStyleWithColumns(61));
376 
377   verifyFormat("if ( // This is some comment\n"
378                "    x + 3) {\n"
379                "}");
380   EXPECT_EQ("if ( // This is some comment\n"
381             "     // spanning two lines\n"
382             "    x + 3) {\n"
383             "}",
384             format("if( // This is some comment\n"
385                    "     // spanning two lines\n"
386                    " x + 3) {\n"
387                    "}"));
388 
389   verifyNoCrash("/\\\n/");
390   verifyNoCrash("/\\\n* */");
391   // The 0-character somehow makes the lexer return a proper comment.
392   verifyNoCrash(StringRef("/*\\\0\n/", 6));
393 }
394 
TEST_F(FormatTestComments,KeepsParameterWithTrailingCommentsOnTheirOwnLine)395 TEST_F(FormatTestComments, KeepsParameterWithTrailingCommentsOnTheirOwnLine) {
396   EXPECT_EQ("SomeFunction(a,\n"
397             "             b, // comment\n"
398             "             c);",
399             format("SomeFunction(a,\n"
400                    "          b, // comment\n"
401                    "      c);"));
402   EXPECT_EQ("SomeFunction(a, b,\n"
403             "             // comment\n"
404             "             c);",
405             format("SomeFunction(a,\n"
406                    "          b,\n"
407                    "  // comment\n"
408                    "      c);"));
409   EXPECT_EQ("SomeFunction(a, b, // comment (unclear relation)\n"
410             "             c);",
411             format("SomeFunction(a, b, // comment (unclear relation)\n"
412                    "      c);"));
413   EXPECT_EQ("SomeFunction(a, // comment\n"
414             "             b,\n"
415             "             c); // comment",
416             format("SomeFunction(a,     // comment\n"
417                    "          b,\n"
418                    "      c); // comment"));
419   EXPECT_EQ("aaaaaaaaaa(aaaa(aaaa,\n"
420             "                aaaa), //\n"
421             "           aaaa, bbbbb);",
422             format("aaaaaaaaaa(aaaa(aaaa,\n"
423                    "aaaa), //\n"
424                    "aaaa, bbbbb);"));
425 }
426 
TEST_F(FormatTestComments,RemovesTrailingWhitespaceOfComments)427 TEST_F(FormatTestComments, RemovesTrailingWhitespaceOfComments) {
428   EXPECT_EQ("// comment", format("// comment  "));
429   EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment",
430             format("int aaaaaaa, bbbbbbb; // comment                   ",
431                    getLLVMStyleWithColumns(33)));
432   EXPECT_EQ("// comment\\\n", format("// comment\\\n  \t \v   \f   "));
433   EXPECT_EQ("// comment    \\\n", format("// comment    \\\n  \t \v   \f   "));
434 }
435 
TEST_F(FormatTestComments,UnderstandsBlockComments)436 TEST_F(FormatTestComments, UnderstandsBlockComments) {
437   verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);");
438   verifyFormat("void f() { g(/*aaa=*/x, /*bbb=*/!y, /*c=*/::c); }");
439   EXPECT_EQ("f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n"
440             "  bbbbbbbbbbbbbbbbbbbbbbbbb);",
441             format("f(aaaaaaaaaaaaaaaaaaaaaaaaa ,   \\\n"
442                    "/* Trailing comment for aa... */\n"
443                    "  bbbbbbbbbbbbbbbbbbbbbbbbb);"));
444   EXPECT_EQ(
445       "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
446       "  /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);",
447       format("f(aaaaaaaaaaaaaaaaaaaaaaaaa    ,   \n"
448              "/* Leading comment for bb... */   bbbbbbbbbbbbbbbbbbbbbbbbb);"));
449   EXPECT_EQ(
450       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
451       "    aaaaaaaaaaaaaaaaaa,\n"
452       "    aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
453       "}",
454       format("void      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
455              "                      aaaaaaaaaaaaaaaaaa  ,\n"
456              "    aaaaaaaaaaaaaaaaaa) {   /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
457              "}"));
458   verifyFormat("f(/* aaaaaaaaaaaaaaaaaa = */\n"
459                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
460 
461   FormatStyle NoBinPacking = getLLVMStyle();
462   NoBinPacking.BinPackParameters = false;
463   verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n"
464                "         /* parameter 2 */ aaaaaa,\n"
465                "         /* parameter 3 */ aaaaaa,\n"
466                "         /* parameter 4 */ aaaaaa);",
467                NoBinPacking);
468 
469   // Aligning block comments in macros.
470   verifyGoogleFormat("#define A        \\\n"
471                      "  int i;   /*a*/ \\\n"
472                      "  int jjj; /*b*/");
473 }
474 
TEST_F(FormatTestComments,AlignsBlockComments)475 TEST_F(FormatTestComments, AlignsBlockComments) {
476   EXPECT_EQ("/*\n"
477             " * Really multi-line\n"
478             " * comment.\n"
479             " */\n"
480             "void f() {}",
481             format("  /*\n"
482                    "   * Really multi-line\n"
483                    "   * comment.\n"
484                    "   */\n"
485                    "  void f() {}"));
486   EXPECT_EQ("class C {\n"
487             "  /*\n"
488             "   * Another multi-line\n"
489             "   * comment.\n"
490             "   */\n"
491             "  void f() {}\n"
492             "};",
493             format("class C {\n"
494                    "/*\n"
495                    " * Another multi-line\n"
496                    " * comment.\n"
497                    " */\n"
498                    "void f() {}\n"
499                    "};"));
500   EXPECT_EQ("/*\n"
501             "  1. This is a comment with non-trivial formatting.\n"
502             "     1.1. We have to indent/outdent all lines equally\n"
503             "         1.1.1. to keep the formatting.\n"
504             " */",
505             format("  /*\n"
506                    "    1. This is a comment with non-trivial formatting.\n"
507                    "       1.1. We have to indent/outdent all lines equally\n"
508                    "           1.1.1. to keep the formatting.\n"
509                    "   */"));
510   EXPECT_EQ("/*\n"
511             "Don't try to outdent if there's not enough indentation.\n"
512             "*/",
513             format("  /*\n"
514                    " Don't try to outdent if there's not enough indentation.\n"
515                    " */"));
516 
517   EXPECT_EQ("int i; /* Comment with empty...\n"
518             "        *\n"
519             "        * line. */",
520             format("int i; /* Comment with empty...\n"
521                    "        *\n"
522                    "        * line. */"));
523   EXPECT_EQ("int foobar = 0; /* comment */\n"
524             "int bar = 0;    /* multiline\n"
525             "                   comment 1 */\n"
526             "int baz = 0;    /* multiline\n"
527             "                   comment 2 */\n"
528             "int bzz = 0;    /* multiline\n"
529             "                   comment 3 */",
530             format("int foobar = 0; /* comment */\n"
531                    "int bar = 0;    /* multiline\n"
532                    "                   comment 1 */\n"
533                    "int baz = 0; /* multiline\n"
534                    "                comment 2 */\n"
535                    "int bzz = 0;         /* multiline\n"
536                    "                        comment 3 */"));
537   EXPECT_EQ("int foobar = 0; /* comment */\n"
538             "int bar = 0;    /* multiline\n"
539             "   comment */\n"
540             "int baz = 0;    /* multiline\n"
541             "comment */",
542             format("int foobar = 0; /* comment */\n"
543                    "int bar = 0; /* multiline\n"
544                    "comment */\n"
545                    "int baz = 0;        /* multiline\n"
546                    "comment */"));
547 }
548 
TEST_F(FormatTestComments,CommentReflowingCanBeTurnedOff)549 TEST_F(FormatTestComments, CommentReflowingCanBeTurnedOff) {
550   FormatStyle Style = getLLVMStyleWithColumns(20);
551   Style.ReflowComments = false;
552   verifyFormat("// aaaaaaaaa aaaaaaaaaa aaaaaaaaaa", Style);
553   verifyFormat("/* aaaaaaaaa aaaaaaaaaa aaaaaaaaaa */", Style);
554 }
555 
TEST_F(FormatTestComments,CorrectlyHandlesLengthOfBlockComments)556 TEST_F(FormatTestComments, CorrectlyHandlesLengthOfBlockComments) {
557   EXPECT_EQ("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
558             "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */",
559             format("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
560                    "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */"));
561   EXPECT_EQ(
562       "void ffffffffffff(\n"
563       "    int aaaaaaaa, int bbbbbbbb,\n"
564       "    int cccccccccccc) { /*\n"
565       "                           aaaaaaaaaa\n"
566       "                           aaaaaaaaaaaaa\n"
567       "                           bbbbbbbbbbbbbb\n"
568       "                           bbbbbbbbbb\n"
569       "                         */\n"
570       "}",
571       format("void ffffffffffff(int aaaaaaaa, int bbbbbbbb, int cccccccccccc)\n"
572              "{ /*\n"
573              "     aaaaaaaaaa aaaaaaaaaaaaa\n"
574              "     bbbbbbbbbbbbbb bbbbbbbbbb\n"
575              "   */\n"
576              "}",
577              getLLVMStyleWithColumns(40)));
578 }
579 
TEST_F(FormatTestComments,DontBreakNonTrailingBlockComments)580 TEST_F(FormatTestComments, DontBreakNonTrailingBlockComments) {
581   EXPECT_EQ("void ffffffffff(\n"
582             "    int aaaaa /* test */);",
583             format("void ffffffffff(int aaaaa /* test */);",
584                    getLLVMStyleWithColumns(35)));
585 }
586 
TEST_F(FormatTestComments,SplitsLongCxxComments)587 TEST_F(FormatTestComments, SplitsLongCxxComments) {
588   EXPECT_EQ("// A comment that\n"
589             "// doesn't fit on\n"
590             "// one line",
591             format("// A comment that doesn't fit on one line",
592                    getLLVMStyleWithColumns(20)));
593   EXPECT_EQ("/// A comment that\n"
594             "/// doesn't fit on\n"
595             "/// one line",
596             format("/// A comment that doesn't fit on one line",
597                    getLLVMStyleWithColumns(20)));
598   EXPECT_EQ("//! A comment that\n"
599             "//! doesn't fit on\n"
600             "//! one line",
601             format("//! A comment that doesn't fit on one line",
602                    getLLVMStyleWithColumns(20)));
603   EXPECT_EQ("// a b c d\n"
604             "// e f  g\n"
605             "// h i j k",
606             format("// a b c d e f  g h i j k", getLLVMStyleWithColumns(10)));
607   EXPECT_EQ(
608       "// a b c d\n"
609       "// e f  g\n"
610       "// h i j k",
611       format("\\\n// a b c d e f  g h i j k", getLLVMStyleWithColumns(10)));
612   EXPECT_EQ("if (true) // A comment that\n"
613             "          // doesn't fit on\n"
614             "          // one line",
615             format("if (true) // A comment that doesn't fit on one line   ",
616                    getLLVMStyleWithColumns(30)));
617   EXPECT_EQ("//    Don't_touch_leading_whitespace",
618             format("//    Don't_touch_leading_whitespace",
619                    getLLVMStyleWithColumns(20)));
620   EXPECT_EQ("// Add leading\n"
621             "// whitespace",
622             format("//Add leading whitespace", getLLVMStyleWithColumns(20)));
623   EXPECT_EQ("/// Add leading\n"
624             "/// whitespace",
625             format("///Add leading whitespace", getLLVMStyleWithColumns(20)));
626   EXPECT_EQ("//! Add leading\n"
627             "//! whitespace",
628             format("//!Add leading whitespace", getLLVMStyleWithColumns(20)));
629   EXPECT_EQ("// whitespace", format("//whitespace", getLLVMStyle()));
630   EXPECT_EQ("// Even if it makes the line exceed the column\n"
631             "// limit",
632             format("//Even if it makes the line exceed the column limit",
633                    getLLVMStyleWithColumns(51)));
634   EXPECT_EQ("//--But not here", format("//--But not here", getLLVMStyle()));
635   EXPECT_EQ("/// line 1\n"
636             "// add leading whitespace",
637             format("/// line 1\n"
638                    "//add leading whitespace",
639                    getLLVMStyleWithColumns(30)));
640   EXPECT_EQ("/// line 1\n"
641             "/// line 2\n"
642             "//! line 3\n"
643             "//! line 4\n"
644             "//! line 5\n"
645             "// line 6\n"
646             "// line 7",
647             format("///line 1\n"
648                    "///line 2\n"
649                    "//! line 3\n"
650                    "//!line 4\n"
651                    "//!line 5\n"
652                    "// line 6\n"
653                    "//line 7", getLLVMStyleWithColumns(20)));
654 
655   EXPECT_EQ("// aa bb cc dd",
656             format("// aa bb             cc dd                   ",
657                    getLLVMStyleWithColumns(15)));
658 
659   EXPECT_EQ("// A comment before\n"
660             "// a macro\n"
661             "// definition\n"
662             "#define a b",
663             format("// A comment before a macro definition\n"
664                    "#define a b",
665                    getLLVMStyleWithColumns(20)));
666   EXPECT_EQ("void ffffff(\n"
667             "    int aaaaaaaaa,  // wwww\n"
668             "    int bbbbbbbbbb, // xxxxxxx\n"
669             "                    // yyyyyyyyyy\n"
670             "    int c, int d, int e) {}",
671             format("void ffffff(\n"
672                    "    int aaaaaaaaa, // wwww\n"
673                    "    int bbbbbbbbbb, // xxxxxxx yyyyyyyyyy\n"
674                    "    int c, int d, int e) {}",
675                    getLLVMStyleWithColumns(40)));
676   EXPECT_EQ("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
677             format("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
678                    getLLVMStyleWithColumns(20)));
679   EXPECT_EQ(
680       "#define XXX // a b c d\n"
681       "            // e f g h",
682       format("#define XXX // a b c d e f g h", getLLVMStyleWithColumns(22)));
683   EXPECT_EQ(
684       "#define XXX // q w e r\n"
685       "            // t y u i",
686       format("#define XXX //q w e r t y u i", getLLVMStyleWithColumns(22)));
687   EXPECT_EQ("{\n"
688             "  //\n"
689             "  //\\\n"
690             "  // long 1 2 3 4 5\n"
691             "}",
692             format("{\n"
693                    "  //\n"
694                    "  //\\\n"
695                    "  // long 1 2 3 4 5\n"
696                    "}",
697                    getLLVMStyleWithColumns(20)));
698   EXPECT_EQ("{\n"
699             "  //\n"
700             "  //\\\n"
701             "  // long 1 2 3 4 5\n"
702             "  // 6\n"
703             "}",
704             format("{\n"
705                    "  //\n"
706                    "  //\\\n"
707                    "  // long 1 2 3 4 5 6\n"
708                    "}",
709                    getLLVMStyleWithColumns(20)));
710 }
711 
TEST_F(FormatTestComments,PreservesHangingIndentInCxxComments)712 TEST_F(FormatTestComments, PreservesHangingIndentInCxxComments) {
713   EXPECT_EQ("//     A comment\n"
714             "//     that doesn't\n"
715             "//     fit on one\n"
716             "//     line",
717             format("//     A comment that doesn't fit on one line",
718                    getLLVMStyleWithColumns(20)));
719   EXPECT_EQ("///     A comment\n"
720             "///     that doesn't\n"
721             "///     fit on one\n"
722             "///     line",
723             format("///     A comment that doesn't fit on one line",
724                    getLLVMStyleWithColumns(20)));
725 }
726 
TEST_F(FormatTestComments,DontSplitLineCommentsWithEscapedNewlines)727 TEST_F(FormatTestComments, DontSplitLineCommentsWithEscapedNewlines) {
728   EXPECT_EQ("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
729             "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
730             "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
731             format("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
732                    "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
733                    "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
734   EXPECT_EQ("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
735             "       // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
736             "       // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
737             format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
738                    "       // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
739                    "       // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
740                    getLLVMStyleWithColumns(50)));
741   // FIXME: One day we might want to implement adjustment of leading whitespace
742   // of the consecutive lines in this kind of comment:
743   EXPECT_EQ("double\n"
744             "    a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
745             "          // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
746             "          // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
747             format("double a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
748                    "          // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
749                    "          // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
750                    getLLVMStyleWithColumns(49)));
751 }
752 
TEST_F(FormatTestComments,DontSplitLineCommentsWithPragmas)753 TEST_F(FormatTestComments, DontSplitLineCommentsWithPragmas) {
754   FormatStyle Pragmas = getLLVMStyleWithColumns(30);
755   Pragmas.CommentPragmas = "^ IWYU pragma:";
756   EXPECT_EQ(
757       "// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb",
758       format("// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", Pragmas));
759   EXPECT_EQ(
760       "/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */",
761       format("/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", Pragmas));
762 }
763 
TEST_F(FormatTestComments,PriorityOfCommentBreaking)764 TEST_F(FormatTestComments, PriorityOfCommentBreaking) {
765   EXPECT_EQ("if (xxx ==\n"
766             "        yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
767             "    zzz)\n"
768             "  q();",
769             format("if (xxx == yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
770                    "    zzz) q();",
771                    getLLVMStyleWithColumns(40)));
772   EXPECT_EQ("if (xxxxxxxxxx ==\n"
773             "        yyy && // aaaaaa bbbbbbbb cccc\n"
774             "    zzz)\n"
775             "  q();",
776             format("if (xxxxxxxxxx == yyy && // aaaaaa bbbbbbbb cccc\n"
777                    "    zzz) q();",
778                    getLLVMStyleWithColumns(40)));
779   EXPECT_EQ("if (xxxxxxxxxx &&\n"
780             "        yyy || // aaaaaa bbbbbbbb cccc\n"
781             "    zzz)\n"
782             "  q();",
783             format("if (xxxxxxxxxx && yyy || // aaaaaa bbbbbbbb cccc\n"
784                    "    zzz) q();",
785                    getLLVMStyleWithColumns(40)));
786   EXPECT_EQ("fffffffff(\n"
787             "    &xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
788             "    zzz);",
789             format("fffffffff(&xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
790                    " zzz);",
791                    getLLVMStyleWithColumns(40)));
792 }
793 
TEST_F(FormatTestComments,MultiLineCommentsInDefines)794 TEST_F(FormatTestComments, MultiLineCommentsInDefines) {
795   EXPECT_EQ("#define A(x) /* \\\n"
796             "  a comment     \\\n"
797             "  inside */     \\\n"
798             "  f();",
799             format("#define A(x) /* \\\n"
800                    "  a comment     \\\n"
801                    "  inside */     \\\n"
802                    "  f();",
803                    getLLVMStyleWithColumns(17)));
804   EXPECT_EQ("#define A(      \\\n"
805             "    x) /*       \\\n"
806             "  a comment     \\\n"
807             "  inside */     \\\n"
808             "  f();",
809             format("#define A(      \\\n"
810                    "    x) /*       \\\n"
811                    "  a comment     \\\n"
812                    "  inside */     \\\n"
813                    "  f();",
814                    getLLVMStyleWithColumns(17)));
815 }
816 
TEST_F(FormatTestComments,ParsesCommentsAdjacentToPPDirectives)817 TEST_F(FormatTestComments, ParsesCommentsAdjacentToPPDirectives) {
818   EXPECT_EQ("namespace {}\n// Test\n#define A",
819             format("namespace {}\n   // Test\n#define A"));
820   EXPECT_EQ("namespace {}\n/* Test */\n#define A",
821             format("namespace {}\n   /* Test */\n#define A"));
822   EXPECT_EQ("namespace {}\n/* Test */ #define A",
823             format("namespace {}\n   /* Test */    #define A"));
824 }
825 
TEST_F(FormatTestComments,KeepsLevelOfCommentBeforePPDirective)826 TEST_F(FormatTestComments, KeepsLevelOfCommentBeforePPDirective) {
827   // Keep the current level if the comment was originally not aligned with
828   // the preprocessor directive.
829   EXPECT_EQ("void f() {\n"
830             "  int i;\n"
831             "  /* comment */\n"
832             "#ifdef A\n"
833             "  int j;\n"
834             "}",
835             format("void f() {\n"
836                    "  int i;\n"
837                    "  /* comment */\n"
838                    "#ifdef A\n"
839                    "  int j;\n"
840                    "}"));
841 
842   EXPECT_EQ("void f() {\n"
843             "  int i;\n"
844             "  /* comment */\n"
845             "\n"
846             "#ifdef A\n"
847             "  int j;\n"
848             "}",
849             format("void f() {\n"
850                    "  int i;\n"
851                    "  /* comment */\n"
852                    "\n"
853                    "#ifdef A\n"
854                    "  int j;\n"
855                    "}"));
856 
857   EXPECT_EQ("int f(int i) {\n"
858             "  if (true) {\n"
859             "    ++i;\n"
860             "  }\n"
861             "  // comment\n"
862             "#ifdef A\n"
863             "  int j;\n"
864             "#endif\n"
865             "}",
866             format("int f(int i) {\n"
867                    "  if (true) {\n"
868                    "    ++i;\n"
869                    "  }\n"
870                    "  // comment\n"
871                    "#ifdef A\n"
872                    "int j;\n"
873                    "#endif\n"
874                    "}"));
875 
876   EXPECT_EQ("int f(int i) {\n"
877             "  if (true) {\n"
878             "    i++;\n"
879             "  } else {\n"
880             "    // comment in else\n"
881             "#ifdef A\n"
882             "    j++;\n"
883             "#endif\n"
884             "  }\n"
885             "}",
886             format("int f(int i) {\n"
887                    "  if (true) {\n"
888                    "    i++;\n"
889                    "  } else {\n"
890                    "  // comment in else\n"
891                    "#ifdef A\n"
892                    "    j++;\n"
893                    "#endif\n"
894                    "  }\n"
895                    "}"));
896 
897   EXPECT_EQ("int f(int i) {\n"
898             "  if (true) {\n"
899             "    i++;\n"
900             "  } else {\n"
901             "    /* comment in else */\n"
902             "#ifdef A\n"
903             "    j++;\n"
904             "#endif\n"
905             "  }\n"
906             "}",
907             format("int f(int i) {\n"
908                    "  if (true) {\n"
909                    "    i++;\n"
910                    "  } else {\n"
911                    "  /* comment in else */\n"
912                    "#ifdef A\n"
913                    "    j++;\n"
914                    "#endif\n"
915                    "  }\n"
916                    "}"));
917 
918   // Keep the current level if there is an empty line between the comment and
919   // the preprocessor directive.
920   EXPECT_EQ("void f() {\n"
921             "  int i;\n"
922             "  /* comment */\n"
923             "\n"
924             "#ifdef A\n"
925             "  int j;\n"
926             "}",
927             format("void f() {\n"
928                    "  int i;\n"
929                    "/* comment */\n"
930                    "\n"
931                    "#ifdef A\n"
932                    "  int j;\n"
933                    "}"));
934 
935   EXPECT_EQ("void f() {\n"
936             "  int i;\n"
937             "  return i;\n"
938             "}\n"
939             "// comment\n"
940             "\n"
941             "#ifdef A\n"
942             "int i;\n"
943             "#endif // A",
944             format("void f() {\n"
945                    "   int i;\n"
946                    "  return i;\n"
947                    "}\n"
948                    "// comment\n"
949                    "\n"
950                    "#ifdef A\n"
951                    "int i;\n"
952                    "#endif // A"));
953 
954   EXPECT_EQ("int f(int i) {\n"
955             "  if (true) {\n"
956             "    ++i;\n"
957             "  }\n"
958             "  // comment\n"
959             "\n"
960             "#ifdef A\n"
961             "  int j;\n"
962             "#endif\n"
963             "}",
964             format("int f(int i) {\n"
965                    "   if (true) {\n"
966                    "    ++i;\n"
967                    "  }\n"
968                    "  // comment\n"
969                    "\n"
970                    "#ifdef A\n"
971                    "  int j;\n"
972                    "#endif\n"
973                    "}"));
974 
975   EXPECT_EQ("int f(int i) {\n"
976             "  if (true) {\n"
977             "    i++;\n"
978             "  } else {\n"
979             "    // comment in else\n"
980             "\n"
981             "#ifdef A\n"
982             "    j++;\n"
983             "#endif\n"
984             "  }\n"
985             "}",
986             format("int f(int i) {\n"
987                    "  if (true) {\n"
988                    "    i++;\n"
989                    "  } else {\n"
990                    "// comment in else\n"
991                    "\n"
992                    "#ifdef A\n"
993                    "    j++;\n"
994                    "#endif\n"
995                    "  }\n"
996                    "}"));
997 
998   EXPECT_EQ("int f(int i) {\n"
999             "  if (true) {\n"
1000             "    i++;\n"
1001             "  } else {\n"
1002             "    /* comment in else */\n"
1003             "\n"
1004             "#ifdef A\n"
1005             "    j++;\n"
1006             "#endif\n"
1007             "  }\n"
1008             "}",
1009             format("int f(int i) {\n"
1010                    "  if (true) {\n"
1011                    "    i++;\n"
1012                    "  } else {\n"
1013                    "/* comment in else */\n"
1014                    "\n"
1015                    "#ifdef A\n"
1016                    "    j++;\n"
1017                    "#endif\n"
1018                    "  }\n"
1019                    "}"));
1020 
1021   // Align with the preprocessor directive if the comment was originally aligned
1022   // with the preprocessor directive and there is no newline between the comment
1023   // and the preprocessor directive.
1024   EXPECT_EQ("void f() {\n"
1025             "  int i;\n"
1026             "/* comment */\n"
1027             "#ifdef A\n"
1028             "  int j;\n"
1029             "}",
1030             format("void f() {\n"
1031                    "  int i;\n"
1032                    "/* comment */\n"
1033                    "#ifdef A\n"
1034                    "  int j;\n"
1035                    "}"));
1036 
1037   EXPECT_EQ("int f(int i) {\n"
1038             "  if (true) {\n"
1039             "    ++i;\n"
1040             "  }\n"
1041             "// comment\n"
1042             "#ifdef A\n"
1043             "  int j;\n"
1044             "#endif\n"
1045             "}",
1046             format("int f(int i) {\n"
1047                    "   if (true) {\n"
1048                    "    ++i;\n"
1049                    "  }\n"
1050                    "// comment\n"
1051                    "#ifdef A\n"
1052                    "  int j;\n"
1053                    "#endif\n"
1054                    "}"));
1055 
1056   EXPECT_EQ("int f(int i) {\n"
1057             "  if (true) {\n"
1058             "    i++;\n"
1059             "  } else {\n"
1060             "// comment in else\n"
1061             "#ifdef A\n"
1062             "    j++;\n"
1063             "#endif\n"
1064             "  }\n"
1065             "}",
1066             format("int f(int i) {\n"
1067                    "  if (true) {\n"
1068                    "    i++;\n"
1069                    "  } else {\n"
1070                    " // comment in else\n"
1071                    " #ifdef A\n"
1072                    "    j++;\n"
1073                    "#endif\n"
1074                    "  }\n"
1075                    "}"));
1076 
1077   EXPECT_EQ("int f(int i) {\n"
1078             "  if (true) {\n"
1079             "    i++;\n"
1080             "  } else {\n"
1081             "/* comment in else */\n"
1082             "#ifdef A\n"
1083             "    j++;\n"
1084             "#endif\n"
1085             "  }\n"
1086             "}",
1087             format("int f(int i) {\n"
1088                    "  if (true) {\n"
1089                    "    i++;\n"
1090                    "  } else {\n"
1091                    " /* comment in else */\n"
1092                    " #ifdef A\n"
1093                    "    j++;\n"
1094                    "#endif\n"
1095                    "  }\n"
1096                    "}"));
1097 }
1098 
TEST_F(FormatTestComments,SplitsLongLinesInComments)1099 TEST_F(FormatTestComments, SplitsLongLinesInComments) {
1100   // FIXME: Do we need to fix up the "  */" at the end?
1101   // It doesn't look like any of our current logic triggers this.
1102   EXPECT_EQ("/* This is a long\n"
1103             " * comment that\n"
1104             " * doesn't fit on\n"
1105             " * one line.  */",
1106             format("/* "
1107                    "This is a long                                         "
1108                    "comment that "
1109                    "doesn't                                    "
1110                    "fit on one line.  */",
1111                    getLLVMStyleWithColumns(20)));
1112   EXPECT_EQ(
1113       "/* a b c d\n"
1114       " * e f  g\n"
1115       " * h i j k\n"
1116       " */",
1117       format("/* a b c d e f  g h i j k */", getLLVMStyleWithColumns(10)));
1118   EXPECT_EQ(
1119       "/* a b c d\n"
1120       " * e f  g\n"
1121       " * h i j k\n"
1122       " */",
1123       format("\\\n/* a b c d e f  g h i j k */", getLLVMStyleWithColumns(10)));
1124   EXPECT_EQ("/*\n"
1125             "This is a long\n"
1126             "comment that doesn't\n"
1127             "fit on one line.\n"
1128             "*/",
1129             format("/*\n"
1130                    "This is a long                                         "
1131                    "comment that doesn't                                    "
1132                    "fit on one line.                                      \n"
1133                    "*/",
1134                    getLLVMStyleWithColumns(20)));
1135   EXPECT_EQ("/*\n"
1136             " * This is a long\n"
1137             " * comment that\n"
1138             " * doesn't fit on\n"
1139             " * one line.\n"
1140             " */",
1141             format("/*      \n"
1142                    " * This is a long "
1143                    "   comment that     "
1144                    "   doesn't fit on   "
1145                    "   one line.                                            \n"
1146                    " */",
1147                    getLLVMStyleWithColumns(20)));
1148   EXPECT_EQ("/*\n"
1149             " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n"
1150             " * so_it_should_be_broken\n"
1151             " * wherever_a_space_occurs\n"
1152             " */",
1153             format("/*\n"
1154                    " * This_is_a_comment_with_words_that_dont_fit_on_one_line "
1155                    "   so_it_should_be_broken "
1156                    "   wherever_a_space_occurs                             \n"
1157                    " */",
1158                    getLLVMStyleWithColumns(20)));
1159   EXPECT_EQ("/*\n"
1160             " *    This_comment_can_not_be_broken_into_lines\n"
1161             " */",
1162             format("/*\n"
1163                    " *    This_comment_can_not_be_broken_into_lines\n"
1164                    " */",
1165                    getLLVMStyleWithColumns(20)));
1166   EXPECT_EQ("{\n"
1167             "  /*\n"
1168             "  This is another\n"
1169             "  long comment that\n"
1170             "  doesn't fit on one\n"
1171             "  line    1234567890\n"
1172             "  */\n"
1173             "}",
1174             format("{\n"
1175                    "/*\n"
1176                    "This is another     "
1177                    "  long comment that "
1178                    "  doesn't fit on one"
1179                    "  line    1234567890\n"
1180                    "*/\n"
1181                    "}",
1182                    getLLVMStyleWithColumns(20)));
1183   EXPECT_EQ("{\n"
1184             "  /*\n"
1185             "   * This        i s\n"
1186             "   * another comment\n"
1187             "   * t hat  doesn' t\n"
1188             "   * fit on one l i\n"
1189             "   * n e\n"
1190             "   */\n"
1191             "}",
1192             format("{\n"
1193                    "/*\n"
1194                    " * This        i s"
1195                    "   another comment"
1196                    "   t hat  doesn' t"
1197                    "   fit on one l i"
1198                    "   n e\n"
1199                    " */\n"
1200                    "}",
1201                    getLLVMStyleWithColumns(20)));
1202   EXPECT_EQ("/*\n"
1203             " * This is a long\n"
1204             " * comment that\n"
1205             " * doesn't fit on\n"
1206             " * one line\n"
1207             " */",
1208             format("   /*\n"
1209                    "    * This is a long comment that doesn't fit on one line\n"
1210                    "    */",
1211                    getLLVMStyleWithColumns(20)));
1212   EXPECT_EQ("{\n"
1213             "  if (something) /* This is a\n"
1214             "                    long\n"
1215             "                    comment */\n"
1216             "    ;\n"
1217             "}",
1218             format("{\n"
1219                    "  if (something) /* This is a long comment */\n"
1220                    "    ;\n"
1221                    "}",
1222                    getLLVMStyleWithColumns(30)));
1223 
1224   EXPECT_EQ("/* A comment before\n"
1225             " * a macro\n"
1226             " * definition */\n"
1227             "#define a b",
1228             format("/* A comment before a macro definition */\n"
1229                    "#define a b",
1230                    getLLVMStyleWithColumns(20)));
1231 
1232   EXPECT_EQ("/* some comment\n"
1233             " *   a comment that\n"
1234             " * we break another\n"
1235             " * comment we have\n"
1236             " * to break a left\n"
1237             " * comment\n"
1238             " */",
1239             format("  /* some comment\n"
1240                    "       *   a comment that we break\n"
1241                    "   * another comment we have to break\n"
1242                    "* a left comment\n"
1243                    "   */",
1244                    getLLVMStyleWithColumns(20)));
1245 
1246   EXPECT_EQ("/**\n"
1247             " * multiline block\n"
1248             " * comment\n"
1249             " *\n"
1250             " */",
1251             format("/**\n"
1252                    " * multiline block comment\n"
1253                    " *\n"
1254                    " */",
1255                    getLLVMStyleWithColumns(20)));
1256 
1257   // This reproduces a crashing bug where both adaptStartOfLine and
1258   // getCommentSplit were trying to wrap after the "/**".
1259   EXPECT_EQ("/** multilineblockcommentwithnowrapopportunity */",
1260             format("/** multilineblockcommentwithnowrapopportunity */",
1261                    getLLVMStyleWithColumns(20)));
1262 
1263   EXPECT_EQ("/*\n"
1264             "\n"
1265             "\n"
1266             "    */\n",
1267             format("  /*       \n"
1268                    "      \n"
1269                    "               \n"
1270                    "      */\n"));
1271 
1272   EXPECT_EQ("/* a a */",
1273             format("/* a a            */", getLLVMStyleWithColumns(15)));
1274   EXPECT_EQ("/* a a bc  */",
1275             format("/* a a            bc  */", getLLVMStyleWithColumns(15)));
1276   EXPECT_EQ("/* aaa aaa\n"
1277             " * aaaaa */",
1278             format("/* aaa aaa aaaaa       */", getLLVMStyleWithColumns(15)));
1279   EXPECT_EQ("/* aaa aaa\n"
1280             " * aaaaa     */",
1281             format("/* aaa aaa aaaaa     */", getLLVMStyleWithColumns(15)));
1282 }
1283 
TEST_F(FormatTestComments,SplitsLongLinesInCommentsInPreprocessor)1284 TEST_F(FormatTestComments, SplitsLongLinesInCommentsInPreprocessor) {
1285   EXPECT_EQ("#define X          \\\n"
1286             "  /*               \\\n"
1287             "   Test            \\\n"
1288             "   Macro comment   \\\n"
1289             "   with a long     \\\n"
1290             "   line            \\\n"
1291             "   */              \\\n"
1292             "  A + B",
1293             format("#define X \\\n"
1294                    "  /*\n"
1295                    "   Test\n"
1296                    "   Macro comment with a long  line\n"
1297                    "   */ \\\n"
1298                    "  A + B",
1299                    getLLVMStyleWithColumns(20)));
1300   EXPECT_EQ("#define X          \\\n"
1301             "  /* Macro comment \\\n"
1302             "     with a long   \\\n"
1303             "     line */       \\\n"
1304             "  A + B",
1305             format("#define X \\\n"
1306                    "  /* Macro comment with a long\n"
1307                    "     line */ \\\n"
1308                    "  A + B",
1309                    getLLVMStyleWithColumns(20)));
1310   EXPECT_EQ("#define X          \\\n"
1311             "  /* Macro comment \\\n"
1312             "   * with a long   \\\n"
1313             "   * line */       \\\n"
1314             "  A + B",
1315             format("#define X \\\n"
1316                    "  /* Macro comment with a long  line */ \\\n"
1317                    "  A + B",
1318                    getLLVMStyleWithColumns(20)));
1319 }
1320 
TEST_F(FormatTestComments,KeepsTrailingPPCommentsAndSectionCommentsSeparate)1321 TEST_F(FormatTestComments, KeepsTrailingPPCommentsAndSectionCommentsSeparate) {
1322   verifyFormat("#ifdef A // line about A\n"
1323                "// section comment\n"
1324                "#endif",
1325                getLLVMStyleWithColumns(80));
1326   verifyFormat("#ifdef A // line 1 about A\n"
1327                "         // line 2 about A\n"
1328                "// section comment\n"
1329                "#endif",
1330                getLLVMStyleWithColumns(80));
1331   EXPECT_EQ("#ifdef A // line 1 about A\n"
1332             "         // line 2 about A\n"
1333             "// section comment\n"
1334             "#endif",
1335             format("#ifdef A // line 1 about A\n"
1336                    "          // line 2 about A\n"
1337                    "// section comment\n"
1338                    "#endif",
1339                    getLLVMStyleWithColumns(80)));
1340   verifyFormat("int f() {\n"
1341                "  int i;\n"
1342                "#ifdef A // comment about A\n"
1343                "  // section comment 1\n"
1344                "  // section comment 2\n"
1345                "  i = 2;\n"
1346                "#else // comment about #else\n"
1347                "  // section comment 3\n"
1348                "  i = 4;\n"
1349                "#endif\n"
1350                "}", getLLVMStyleWithColumns(80));
1351 }
1352 
TEST_F(FormatTestComments,AlignsPPElseEndifComments)1353 TEST_F(FormatTestComments, AlignsPPElseEndifComments) {
1354   verifyFormat("#if A\n"
1355                "#else  // A\n"
1356                "int iiii;\n"
1357                "#endif // B",
1358                getLLVMStyleWithColumns(20));
1359   verifyFormat("#if A\n"
1360                "#else  // A\n"
1361                "int iiii; // CC\n"
1362                "#endif // B",
1363                getLLVMStyleWithColumns(20));
1364   EXPECT_EQ("#if A\n"
1365             "#else  // A1\n"
1366             "       // A2\n"
1367             "int ii;\n"
1368             "#endif // B",
1369             format("#if A\n"
1370                    "#else  // A1\n"
1371                    "       // A2\n"
1372                    "int ii;\n"
1373                    "#endif // B",
1374                    getLLVMStyleWithColumns(20)));
1375 }
1376 
TEST_F(FormatTestComments,CommentsInStaticInitializers)1377 TEST_F(FormatTestComments, CommentsInStaticInitializers) {
1378   EXPECT_EQ(
1379       "static SomeType type = {aaaaaaaaaaaaaaaaaaaa, /* comment */\n"
1380       "                        aaaaaaaaaaaaaaaaaaaa /* comment */,\n"
1381       "                        /* comment */ aaaaaaaaaaaaaaaaaaaa,\n"
1382       "                        aaaaaaaaaaaaaaaaaaaa, // comment\n"
1383       "                        aaaaaaaaaaaaaaaaaaaa};",
1384       format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa  ,  /* comment */\n"
1385              "                   aaaaaaaaaaaaaaaaaaaa   /* comment */ ,\n"
1386              "                     /* comment */   aaaaaaaaaaaaaaaaaaaa ,\n"
1387              "              aaaaaaaaaaaaaaaaaaaa ,   // comment\n"
1388              "                  aaaaaaaaaaaaaaaaaaaa };"));
1389   verifyFormat("static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
1390                "                        bbbbbbbbbbb, ccccccccccc};");
1391   verifyFormat("static SomeType type = {aaaaaaaaaaa,\n"
1392                "                        // comment for bb....\n"
1393                "                        bbbbbbbbbbb, ccccccccccc};");
1394   verifyGoogleFormat(
1395       "static SomeType type = {aaaaaaaaaaa,  // comment for aa...\n"
1396       "                        bbbbbbbbbbb, ccccccccccc};");
1397   verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n"
1398                      "                        // comment for bb....\n"
1399                      "                        bbbbbbbbbbb, ccccccccccc};");
1400 
1401   verifyFormat("S s = {{a, b, c},  // Group #1\n"
1402                "       {d, e, f},  // Group #2\n"
1403                "       {g, h, i}}; // Group #3");
1404   verifyFormat("S s = {{// Group #1\n"
1405                "        a, b, c},\n"
1406                "       {// Group #2\n"
1407                "        d, e, f},\n"
1408                "       {// Group #3\n"
1409                "        g, h, i}};");
1410 
1411   EXPECT_EQ("S s = {\n"
1412             "    // Some comment\n"
1413             "    a,\n"
1414             "\n"
1415             "    // Comment after empty line\n"
1416             "    b}",
1417             format("S s =    {\n"
1418                    "      // Some comment\n"
1419                    "  a,\n"
1420                    "  \n"
1421                    "     // Comment after empty line\n"
1422                    "      b\n"
1423                    "}"));
1424   EXPECT_EQ("S s = {\n"
1425             "    /* Some comment */\n"
1426             "    a,\n"
1427             "\n"
1428             "    /* Comment after empty line */\n"
1429             "    b}",
1430             format("S s =    {\n"
1431                    "      /* Some comment */\n"
1432                    "  a,\n"
1433                    "  \n"
1434                    "     /* Comment after empty line */\n"
1435                    "      b\n"
1436                    "}"));
1437   verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n"
1438                "    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
1439                "    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
1440                "    0x00, 0x00, 0x00, 0x00};            // comment\n");
1441 }
1442 
TEST_F(FormatTestComments,LineCommentsAfterRightBrace)1443 TEST_F(FormatTestComments, LineCommentsAfterRightBrace) {
1444   EXPECT_EQ("if (true) { // comment about branch\n"
1445             "  // comment about f\n"
1446             "  f();\n"
1447             "}",
1448             format("if (true) { // comment about branch\n"
1449                    "  // comment about f\n"
1450                    "  f();\n"
1451                    "}",
1452                    getLLVMStyleWithColumns(80)));
1453   EXPECT_EQ("if (1) { // if line 1\n"
1454             "         // if line 2\n"
1455             "         // if line 3\n"
1456             "  // f line 1\n"
1457             "  // f line 2\n"
1458             "  f();\n"
1459             "} else { // else line 1\n"
1460             "         // else line 2\n"
1461             "         // else line 3\n"
1462             "  // g line 1\n"
1463             "  g();\n"
1464             "}",
1465             format("if (1) { // if line 1\n"
1466                    "          // if line 2\n"
1467                    "        // if line 3\n"
1468                    "  // f line 1\n"
1469                    "    // f line 2\n"
1470                    "  f();\n"
1471                    "} else { // else line 1\n"
1472                    "        // else line 2\n"
1473                    "         // else line 3\n"
1474                    "  // g line 1\n"
1475                    "  g();\n"
1476                    "}"));
1477   EXPECT_EQ("do { // line 1\n"
1478             "     // line 2\n"
1479             "     // line 3\n"
1480             "  f();\n"
1481             "} while (true);",
1482             format("do { // line 1\n"
1483                    "     // line 2\n"
1484                    "   // line 3\n"
1485                    "  f();\n"
1486                    "} while (true);",
1487                    getLLVMStyleWithColumns(80)));
1488   EXPECT_EQ("while (a < b) { // line 1\n"
1489             "  // line 2\n"
1490             "  // line 3\n"
1491             "  f();\n"
1492             "}",
1493             format("while (a < b) {// line 1\n"
1494                    "  // line 2\n"
1495                    "  // line 3\n"
1496                    "  f();\n"
1497                    "}",
1498                    getLLVMStyleWithColumns(80)));
1499 }
1500 
TEST_F(FormatTestComments,ReflowsComments)1501 TEST_F(FormatTestComments, ReflowsComments) {
1502   // Break a long line and reflow with the full next line.
1503   EXPECT_EQ("// long long long\n"
1504             "// long long",
1505             format("// long long long long\n"
1506                    "// long",
1507                    getLLVMStyleWithColumns(20)));
1508 
1509   // Keep the trailing newline while reflowing.
1510   EXPECT_EQ("// long long long\n"
1511             "// long long\n",
1512             format("// long long long long\n"
1513                    "// long\n",
1514                    getLLVMStyleWithColumns(20)));
1515 
1516   // Break a long line and reflow with a part of the next line.
1517   EXPECT_EQ("// long long long\n"
1518             "// long long\n"
1519             "// long_long",
1520             format("// long long long long\n"
1521                    "// long long_long",
1522                    getLLVMStyleWithColumns(20)));
1523 
1524   // Break but do not reflow if the first word from the next line is too long.
1525   EXPECT_EQ("// long long long\n"
1526             "// long\n"
1527             "// long_long_long\n",
1528             format("// long long long long\n"
1529                    "// long_long_long\n",
1530                    getLLVMStyleWithColumns(20)));
1531 
1532   // Don't break or reflow short lines.
1533   verifyFormat("// long\n"
1534                "// long long long lo\n"
1535                "// long long long lo\n"
1536                "// long",
1537                getLLVMStyleWithColumns(20));
1538 
1539   // Keep prefixes and decorations while reflowing.
1540   EXPECT_EQ("/// long long long\n"
1541             "/// long long\n",
1542             format("/// long long long long\n"
1543                    "/// long\n",
1544                    getLLVMStyleWithColumns(20)));
1545   EXPECT_EQ("//! long long long\n"
1546             "//! long long\n",
1547             format("//! long long long long\n"
1548                    "//! long\n",
1549                    getLLVMStyleWithColumns(20)));
1550   EXPECT_EQ("/* long long long\n"
1551             " * long long */",
1552             format("/* long long long long\n"
1553                    " * long */",
1554                    getLLVMStyleWithColumns(20)));
1555   EXPECT_EQ("///< long long long\n"
1556             "///< long long\n",
1557             format("///< long long long long\n"
1558                    "///< long\n",
1559                    getLLVMStyleWithColumns(20)));
1560   EXPECT_EQ("//!< long long long\n"
1561             "//!< long long\n",
1562             format("//!< long long long long\n"
1563                    "//!< long\n",
1564                    getLLVMStyleWithColumns(20)));
1565 
1566   // Don't bring leading whitespace up while reflowing.
1567   EXPECT_EQ("/*  long long long\n"
1568             " * long long long\n"
1569             " */",
1570             format("/*  long long long long\n"
1571                    " *  long long\n"
1572                    " */",
1573                    getLLVMStyleWithColumns(20)));
1574 
1575   // Reflow the last line of a block comment with its trailing '*/'.
1576   EXPECT_EQ("/* long long long\n"
1577             "   long long */",
1578             format("/* long long long long\n"
1579                    "   long */",
1580                    getLLVMStyleWithColumns(20)));
1581 
1582   // Reflow two short lines; keep the postfix of the last one.
1583   EXPECT_EQ("/* long long long\n"
1584             " * long long long */",
1585             format("/* long long long long\n"
1586                    " * long\n"
1587                    " * long */",
1588                    getLLVMStyleWithColumns(20)));
1589 
1590   // Put the postfix of the last short reflow line on a newline if it doesn't
1591   // fit.
1592   EXPECT_EQ("/* long long long\n"
1593             " * long long longg\n"
1594             " */",
1595             format("/* long long long long\n"
1596                    " * long\n"
1597                    " * longg */",
1598                    getLLVMStyleWithColumns(20)));
1599 
1600   // Reflow lines with leading whitespace.
1601   EXPECT_EQ("{\n"
1602             "  /*\n"
1603             "   * long long long\n"
1604             "   * long long long\n"
1605             "   * long long long\n"
1606             "   */\n"
1607             "}",
1608             format("{\n"
1609                    "/*\n"
1610                    " * long long long long\n"
1611                    " *   long\n"
1612                    " * long long long long\n"
1613                    " */\n"
1614                    "}",
1615                    getLLVMStyleWithColumns(20)));
1616 
1617   // Break single line block comments that are first in the line with ' *'
1618   // decoration.
1619   EXPECT_EQ("/* long long long\n"
1620             " * long */",
1621             format("/* long long long long */", getLLVMStyleWithColumns(20)));
1622 
1623   // Break single line block comment that are not first in the line with '  '
1624   // decoration.
1625   EXPECT_EQ("int i; /* long long\n"
1626             "          long */",
1627             format("int i; /* long long long */", getLLVMStyleWithColumns(20)));
1628 
1629   // Reflow a line that goes just over the column limit.
1630   EXPECT_EQ("// long long long\n"
1631             "// lon long",
1632             format("// long long long lon\n"
1633                    "// long",
1634                    getLLVMStyleWithColumns(20)));
1635 
1636   // Stop reflowing if the next line has a different indentation than the
1637   // previous line.
1638   EXPECT_EQ("// long long long\n"
1639             "// long\n"
1640             "//  long long\n"
1641             "//  long",
1642             format("// long long long long\n"
1643                    "//  long long\n"
1644                    "//  long",
1645                    getLLVMStyleWithColumns(20)));
1646 
1647   // Reflow into the last part of a really long line that has been broken into
1648   // multiple lines.
1649   EXPECT_EQ("// long long long\n"
1650             "// long long long\n"
1651             "// long long long\n",
1652             format("// long long long long long long long long\n"
1653                    "// long\n",
1654                    getLLVMStyleWithColumns(20)));
1655 
1656   // Break the first line, then reflow the beginning of the second and third
1657   // line up.
1658   EXPECT_EQ("// long long long\n"
1659             "// lon1 lon2 lon2\n"
1660             "// lon2 lon3 lon3",
1661             format("// long long long lon1\n"
1662                    "// lon2 lon2 lon2\n"
1663                    "// lon3 lon3",
1664                    getLLVMStyleWithColumns(20)));
1665 
1666   // Reflow the beginning of the second line, then break the rest.
1667   EXPECT_EQ("// long long long\n"
1668             "// lon1 lon2 lon2\n"
1669             "// lon2 lon2 lon2\n"
1670             "// lon3",
1671             format("// long long long lon1\n"
1672                    "// lon2 lon2 lon2 lon2 lon2 lon3",
1673                    getLLVMStyleWithColumns(20)));
1674 
1675   // Shrink the first line, then reflow the second line up.
1676   EXPECT_EQ("// long long long", format("// long              long\n"
1677                                         "// long",
1678                                         getLLVMStyleWithColumns(20)));
1679 
1680   // Don't shrink leading whitespace.
1681   EXPECT_EQ("int i; ///           a",
1682             format("int i; ///           a", getLLVMStyleWithColumns(20)));
1683 
1684   // Shrink trailing whitespace if there is no postfix and reflow.
1685   EXPECT_EQ("// long long long\n"
1686             "// long long",
1687             format("// long long long long    \n"
1688                    "// long",
1689                    getLLVMStyleWithColumns(20)));
1690 
1691   // Shrink trailing whitespace to a single one if there is postfix.
1692   EXPECT_EQ("/* long long long */",
1693             format("/* long long long     */", getLLVMStyleWithColumns(20)));
1694 
1695   // Break a block comment postfix if exceeding the line limit.
1696   EXPECT_EQ("/*               long\n"
1697             " */",
1698             format("/*               long */", getLLVMStyleWithColumns(20)));
1699 
1700   // Reflow indented comments.
1701   EXPECT_EQ("{\n"
1702             "  // long long long\n"
1703             "  // long long\n"
1704             "  int i; /* long lon\n"
1705             "            g long\n"
1706             "          */\n"
1707             "}",
1708             format("{\n"
1709                    "  // long long long long\n"
1710                    "  // long\n"
1711                    "  int i; /* long lon g\n"
1712                    "            long */\n"
1713                    "}",
1714                    getLLVMStyleWithColumns(20)));
1715 
1716   // Don't realign trailing comments after reflow has happened.
1717   EXPECT_EQ("// long long long\n"
1718             "// long long\n"
1719             "long i; // long",
1720             format("// long long long long\n"
1721                    "// long\n"
1722                    "long i; // long",
1723                    getLLVMStyleWithColumns(20)));
1724   EXPECT_EQ("// long long long\n"
1725             "// longng long long\n"
1726             "// long lo",
1727             format("// long long long longng\n"
1728                    "// long long long\n"
1729                    "// lo",
1730                    getLLVMStyleWithColumns(20)));
1731 
1732   // Reflow lines after a broken line.
1733   EXPECT_EQ("int a; // Trailing\n"
1734             "       // comment on\n"
1735             "       // 2 or 3\n"
1736             "       // lines.\n",
1737             format("int a; // Trailing comment\n"
1738                    "       // on 2\n"
1739                    "       // or 3\n"
1740                    "       // lines.\n",
1741                    getLLVMStyleWithColumns(20)));
1742   EXPECT_EQ("/// This long line\n"
1743             "/// gets reflown.\n",
1744             format("/// This long line gets\n"
1745                    "/// reflown.\n",
1746                    getLLVMStyleWithColumns(20)));
1747   EXPECT_EQ("//! This long line\n"
1748             "//! gets reflown.\n",
1749             format(" //! This long line gets\n"
1750                    " //! reflown.\n",
1751                    getLLVMStyleWithColumns(20)));
1752   EXPECT_EQ("/* This long line\n"
1753             " * gets reflown.\n"
1754             " */\n",
1755             format("/* This long line gets\n"
1756                    " * reflown.\n"
1757                    " */\n",
1758                    getLLVMStyleWithColumns(20)));
1759 
1760   // Reflow after indentation makes a line too long.
1761   EXPECT_EQ("{\n"
1762             "  // long long long\n"
1763             "  // lo long\n"
1764             "}\n",
1765             format("{\n"
1766                    "// long long long lo\n"
1767                    "// long\n"
1768                    "}\n",
1769                    getLLVMStyleWithColumns(20)));
1770 
1771   // Break and reflow multiple lines.
1772   EXPECT_EQ("/*\n"
1773             " * Reflow the end of\n"
1774             " * line by 11 22 33\n"
1775             " * 4.\n"
1776             " */\n",
1777             format("/*\n"
1778                    " * Reflow the end of line\n"
1779                    " * by\n"
1780                    " * 11\n"
1781                    " * 22\n"
1782                    " * 33\n"
1783                    " * 4.\n"
1784                    " */\n",
1785                    getLLVMStyleWithColumns(20)));
1786   EXPECT_EQ("/// First line gets\n"
1787             "/// broken. Second\n"
1788             "/// line gets\n"
1789             "/// reflown and\n"
1790             "/// broken. Third\n"
1791             "/// gets reflown.\n",
1792             format("/// First line gets broken.\n"
1793                    "/// Second line gets reflown and broken.\n"
1794                    "/// Third gets reflown.\n",
1795                    getLLVMStyleWithColumns(20)));
1796   EXPECT_EQ("int i; // first long\n"
1797             "       // long snd\n"
1798             "       // long.\n",
1799             format("int i; // first long long\n"
1800                    "       // snd long.\n",
1801                    getLLVMStyleWithColumns(20)));
1802   EXPECT_EQ("{\n"
1803             "  // first long line\n"
1804             "  // line second\n"
1805             "  // long line line\n"
1806             "  // third long line\n"
1807             "  // line\n"
1808             "}\n",
1809             format("{\n"
1810                    "  // first long line line\n"
1811                    "  // second long line line\n"
1812                    "  // third long line line\n"
1813                    "}\n",
1814                    getLLVMStyleWithColumns(20)));
1815   EXPECT_EQ("int i; /* first line\n"
1816             "        * second\n"
1817             "        * line third\n"
1818             "        * line\n"
1819             "        */",
1820             format("int i; /* first line\n"
1821                    "        * second line\n"
1822                    "        * third line\n"
1823                    "        */",
1824                    getLLVMStyleWithColumns(20)));
1825 
1826   // Reflow the last two lines of a section that starts with a line having
1827   // different indentation.
1828   EXPECT_EQ(
1829       "//     long\n"
1830       "// long long long\n"
1831       "// long long",
1832       format("//     long\n"
1833              "// long long long long\n"
1834              "// long",
1835              getLLVMStyleWithColumns(20)));
1836 
1837   // Keep the block comment endling '*/' while reflowing.
1838   EXPECT_EQ("/* Long long long\n"
1839             " * line short */\n",
1840             format("/* Long long long line\n"
1841                    " * short */\n",
1842                    getLLVMStyleWithColumns(20)));
1843 
1844   // Don't reflow between separate blocks of comments.
1845   EXPECT_EQ("/* First comment\n"
1846             " * block will */\n"
1847             "/* Snd\n"
1848             " */\n",
1849             format("/* First comment block\n"
1850                    " * will */\n"
1851                    "/* Snd\n"
1852                    " */\n",
1853                    getLLVMStyleWithColumns(20)));
1854 
1855   // Don't reflow across blank comment lines.
1856   EXPECT_EQ("int i; // This long\n"
1857             "       // line gets\n"
1858             "       // broken.\n"
1859             "       //\n"
1860             "       // keep.\n",
1861             format("int i; // This long line gets broken.\n"
1862                    "       //  \n"
1863                    "       // keep.\n",
1864                    getLLVMStyleWithColumns(20)));
1865   EXPECT_EQ("{\n"
1866             "  /// long long long\n"
1867             "  /// long long\n"
1868             "  ///\n"
1869             "  /// long\n"
1870             "}",
1871             format("{\n"
1872                    "  /// long long long long\n"
1873                    "  /// long\n"
1874                    "  ///\n"
1875                    "  /// long\n"
1876                    "}",
1877                    getLLVMStyleWithColumns(20)));
1878   EXPECT_EQ("//! long long long\n"
1879             "//! long\n"
1880             "\n"
1881             "//! long",
1882             format("//! long long long long\n"
1883                    "\n"
1884                    "//! long",
1885                    getLLVMStyleWithColumns(20)));
1886   EXPECT_EQ("/* long long long\n"
1887             "   long\n"
1888             "\n"
1889             "   long */",
1890             format("/* long long long long\n"
1891                    "\n"
1892                    "   long */",
1893                    getLLVMStyleWithColumns(20)));
1894   EXPECT_EQ("/* long long long\n"
1895             " * long\n"
1896             " *\n"
1897             " * long */",
1898             format("/* long long long long\n"
1899                    " *\n"
1900                    " * long */",
1901                    getLLVMStyleWithColumns(20)));
1902 
1903   // Don't reflow lines having content that is a single character.
1904   EXPECT_EQ("// long long long\n"
1905             "// long\n"
1906             "// l",
1907             format("// long long long long\n"
1908                    "// l",
1909                    getLLVMStyleWithColumns(20)));
1910 
1911   // Don't reflow lines starting with two punctuation characters.
1912   EXPECT_EQ("// long long long\n"
1913             "// long\n"
1914             "// ... --- ...",
1915             format(
1916                 "// long long long long\n"
1917                 "// ... --- ...",
1918                 getLLVMStyleWithColumns(20)));
1919 
1920   // Don't reflow lines starting with '@'.
1921   EXPECT_EQ("// long long long\n"
1922             "// long\n"
1923             "// @param arg",
1924             format("// long long long long\n"
1925                    "// @param arg",
1926                    getLLVMStyleWithColumns(20)));
1927 
1928   // Don't reflow lines starting with 'TODO'.
1929   EXPECT_EQ("// long long long\n"
1930             "// long\n"
1931             "// TODO: long",
1932             format("// long long long long\n"
1933                    "// TODO: long",
1934                    getLLVMStyleWithColumns(20)));
1935 
1936   // Don't reflow lines starting with 'FIXME'.
1937   EXPECT_EQ("// long long long\n"
1938             "// long\n"
1939             "// FIXME: long",
1940             format("// long long long long\n"
1941                    "// FIXME: long",
1942                    getLLVMStyleWithColumns(20)));
1943 
1944   // Don't reflow lines starting with 'XXX'.
1945   EXPECT_EQ("// long long long\n"
1946             "// long\n"
1947             "// XXX: long",
1948             format("// long long long long\n"
1949                    "// XXX: long",
1950                    getLLVMStyleWithColumns(20)));
1951 
1952   // Don't reflow comment pragmas.
1953   EXPECT_EQ("// long long long\n"
1954             "// long\n"
1955             "// IWYU pragma:",
1956             format("// long long long long\n"
1957                    "// IWYU pragma:",
1958                    getLLVMStyleWithColumns(20)));
1959   EXPECT_EQ("/* long long long\n"
1960             " * long\n"
1961             " * IWYU pragma:\n"
1962             " */",
1963             format("/* long long long long\n"
1964                    " * IWYU pragma:\n"
1965                    " */",
1966                    getLLVMStyleWithColumns(20)));
1967 
1968   // Reflow lines that have a non-punctuation character among their first 2
1969   // characters.
1970   EXPECT_EQ("// long long long\n"
1971             "// long 'long'",
1972             format(
1973                 "// long long long long\n"
1974                 "// 'long'",
1975                 getLLVMStyleWithColumns(20)));
1976 
1977   // Don't reflow between separate blocks of comments.
1978   EXPECT_EQ("/* First comment\n"
1979             " * block will */\n"
1980             "/* Snd\n"
1981             " */\n",
1982             format("/* First comment block\n"
1983                    " * will */\n"
1984                    "/* Snd\n"
1985                    " */\n",
1986                    getLLVMStyleWithColumns(20)));
1987 
1988   // Don't reflow lines having different indentation.
1989   EXPECT_EQ("// long long long\n"
1990             "// long\n"
1991             "//  long",
1992             format("// long long long long\n"
1993                    "//  long",
1994                    getLLVMStyleWithColumns(20)));
1995 
1996   // Don't reflow separate bullets in list
1997   EXPECT_EQ("// - long long long\n"
1998             "// long\n"
1999             "// - long",
2000             format("// - long long long long\n"
2001                    "// - long",
2002                    getLLVMStyleWithColumns(20)));
2003   EXPECT_EQ("// * long long long\n"
2004             "// long\n"
2005             "// * long",
2006             format("// * long long long long\n"
2007                    "// * long",
2008                    getLLVMStyleWithColumns(20)));
2009   EXPECT_EQ("// + long long long\n"
2010             "// long\n"
2011             "// + long",
2012             format("// + long long long long\n"
2013                    "// + long",
2014                    getLLVMStyleWithColumns(20)));
2015   EXPECT_EQ("// 1. long long long\n"
2016             "// long\n"
2017             "// 2. long",
2018             format("// 1. long long long long\n"
2019                    "// 2. long",
2020                    getLLVMStyleWithColumns(20)));
2021   EXPECT_EQ("// -# long long long\n"
2022             "// long\n"
2023             "// -# long",
2024             format("// -# long long long long\n"
2025                    "// -# long",
2026                    getLLVMStyleWithColumns(20)));
2027 
2028   EXPECT_EQ("// - long long long\n"
2029             "// long long long\n"
2030             "// - long",
2031             format("// - long long long long\n"
2032                    "// long long\n"
2033                    "// - long",
2034                    getLLVMStyleWithColumns(20)));
2035   EXPECT_EQ("// - long long long\n"
2036             "// long long long\n"
2037             "// long\n"
2038             "// - long",
2039             format("// - long long long long\n"
2040                    "// long long long\n"
2041                    "// - long",
2042                    getLLVMStyleWithColumns(20)));
2043 
2044   // Large number (>2 digits) are not list items
2045   EXPECT_EQ("// long long long\n"
2046             "// long 1024. long.",
2047             format("// long long long long\n"
2048                    "// 1024. long.",
2049                    getLLVMStyleWithColumns(20)));
2050 
2051   // Do not break before number, to avoid introducing a non-reflowable doxygen
2052   // list item.
2053   EXPECT_EQ("// long long\n"
2054             "// long 10. long.",
2055             format("// long long long 10.\n"
2056                    "// long.",
2057                    getLLVMStyleWithColumns(20)));
2058 
2059   // Don't break or reflow after implicit string literals.
2060   verifyFormat("#include <t> // l l l\n"
2061                "             // l",
2062                getLLVMStyleWithColumns(20));
2063 
2064   // Don't break or reflow comments on import lines.
2065   EXPECT_EQ("#include \"t\" /* l l l\n"
2066             "                * l */",
2067             format("#include \"t\" /* l l l\n"
2068                    "                * l */",
2069                    getLLVMStyleWithColumns(20)));
2070 
2071   // Don't reflow between different trailing comment sections.
2072   EXPECT_EQ("int i; // long long\n"
2073             "       // long\n"
2074             "int j; // long long\n"
2075             "       // long\n",
2076             format("int i; // long long long\n"
2077                    "int j; // long long long\n",
2078                    getLLVMStyleWithColumns(20)));
2079 
2080   // Don't reflow if the first word on the next line is longer than the
2081   // available space at current line.
2082   EXPECT_EQ("int i; // trigger\n"
2083             "       // reflow\n"
2084             "       // longsec\n",
2085             format("int i; // trigger reflow\n"
2086                    "       // longsec\n",
2087                    getLLVMStyleWithColumns(20)));
2088 
2089   // Simple case that correctly handles reflow in parameter lists.
2090   EXPECT_EQ("a = f(/* looooooooong\n"
2091             "       * long long\n"
2092             "       */\n"
2093             "      a);",
2094             format("a = f(/* looooooooong long\n* long\n*/ a);",
2095                    getLLVMStyleWithColumns(22)));
2096   // Tricky case that has fewer lines if we reflow the comment, ending up with
2097   // fewer lines.
2098   EXPECT_EQ("a = f(/* loooooong\n"
2099             "       * long long\n"
2100             "       */\n"
2101             "      a);",
2102             format("a = f(/* loooooong long\n* long\n*/ a);",
2103                    getLLVMStyleWithColumns(22)));
2104 
2105   // Keep empty comment lines.
2106   EXPECT_EQ("/**/", format(" /**/", getLLVMStyleWithColumns(20)));
2107   EXPECT_EQ("/* */", format(" /* */", getLLVMStyleWithColumns(20)));
2108   EXPECT_EQ("/*  */", format(" /*  */", getLLVMStyleWithColumns(20)));
2109   EXPECT_EQ("//", format(" //  ", getLLVMStyleWithColumns(20)));
2110   EXPECT_EQ("///", format(" ///  ", getLLVMStyleWithColumns(20)));
2111 }
2112 
TEST_F(FormatTestComments,ReflowsCommentsPrecise)2113 TEST_F(FormatTestComments, ReflowsCommentsPrecise) {
2114   // FIXME: This assumes we do not continue compressing whitespace once we are
2115   // in reflow mode. Consider compressing whitespace.
2116 
2117   // Test that we stop reflowing precisely at the column limit.
2118   // After reflowing, "// reflows into   foo" does not fit the column limit,
2119   // so we compress the whitespace.
2120   EXPECT_EQ("// some text that\n"
2121             "// reflows into foo\n",
2122             format("// some text that reflows\n"
2123                    "// into   foo\n",
2124                    getLLVMStyleWithColumns(20)));
2125   // Given one more column, "// reflows into   foo" does fit the limit, so we
2126   // do not compress the whitespace.
2127   EXPECT_EQ("// some text that\n"
2128             "// reflows into   foo\n",
2129             format("// some text that reflows\n"
2130                    "// into   foo\n",
2131                    getLLVMStyleWithColumns(21)));
2132 
2133   // Make sure that we correctly account for the space added in the reflow case
2134   // when making the reflowing decision.
2135   // First, when the next line ends precisely one column over the limit, do not
2136   // reflow.
2137   EXPECT_EQ("// some text that\n"
2138             "// reflows\n"
2139             "// into1234567\n",
2140             format("// some text that reflows\n"
2141                    "// into1234567\n",
2142                    getLLVMStyleWithColumns(21)));
2143   // Secondly, when the next line ends later, but the first word in that line
2144   // is precisely one column over the limit, do not reflow.
2145   EXPECT_EQ("// some text that\n"
2146             "// reflows\n"
2147             "// into1234567 f\n",
2148             format("// some text that reflows\n"
2149                    "// into1234567 f\n",
2150                    getLLVMStyleWithColumns(21)));
2151 }
2152 
TEST_F(FormatTestComments,ReflowsCommentsWithExtraWhitespace)2153 TEST_F(FormatTestComments, ReflowsCommentsWithExtraWhitespace) {
2154   // Baseline.
2155   EXPECT_EQ("// some text\n"
2156             "// that re flows\n",
2157             format("// some text that\n"
2158                    "// re flows\n",
2159                    getLLVMStyleWithColumns(16)));
2160   EXPECT_EQ("// some text\n"
2161             "// that re flows\n",
2162             format("// some text that\n"
2163                    "// re    flows\n",
2164                    getLLVMStyleWithColumns(16)));
2165   EXPECT_EQ("/* some text\n"
2166             " * that re flows\n"
2167             " */\n",
2168             format("/* some text that\n"
2169                    "*      re       flows\n"
2170                    "*/\n",
2171                    getLLVMStyleWithColumns(16)));
2172   // FIXME: We do not reflow if the indent of two subsequent lines differs;
2173   // given that this is different behavior from block comments, do we want
2174   // to keep this?
2175   EXPECT_EQ("// some text\n"
2176             "// that\n"
2177             "//     re flows\n",
2178             format("// some text that\n"
2179                    "//     re       flows\n",
2180                    getLLVMStyleWithColumns(16)));
2181   // Space within parts of a line that fit.
2182   // FIXME: Use the earliest possible split while reflowing to compress the
2183   // whitespace within the line.
2184   EXPECT_EQ("// some text that\n"
2185             "// does re   flow\n"
2186             "// more  here\n",
2187             format("// some text that does\n"
2188                    "// re   flow  more  here\n",
2189                    getLLVMStyleWithColumns(21)));
2190 }
2191 
TEST_F(FormatTestComments,IgnoresIf0Contents)2192 TEST_F(FormatTestComments, IgnoresIf0Contents) {
2193   EXPECT_EQ("#if 0\n"
2194             "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
2195             "#endif\n"
2196             "void f() {}",
2197             format("#if 0\n"
2198                    "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
2199                    "#endif\n"
2200                    "void f(  ) {  }"));
2201   EXPECT_EQ("#if false\n"
2202             "void f(  ) {  }\n"
2203             "#endif\n"
2204             "void g() {}\n",
2205             format("#if false\n"
2206                    "void f(  ) {  }\n"
2207                    "#endif\n"
2208                    "void g(  ) {  }\n"));
2209   EXPECT_EQ("enum E {\n"
2210             "  One,\n"
2211             "  Two,\n"
2212             "#if 0\n"
2213             "Three,\n"
2214             "      Four,\n"
2215             "#endif\n"
2216             "  Five\n"
2217             "};",
2218             format("enum E {\n"
2219                    "  One,Two,\n"
2220                    "#if 0\n"
2221                    "Three,\n"
2222                    "      Four,\n"
2223                    "#endif\n"
2224                    "  Five};"));
2225   EXPECT_EQ("enum F {\n"
2226             "  One,\n"
2227             "#if 1\n"
2228             "  Two,\n"
2229             "#if 0\n"
2230             "Three,\n"
2231             "      Four,\n"
2232             "#endif\n"
2233             "  Five\n"
2234             "#endif\n"
2235             "};",
2236             format("enum F {\n"
2237                    "One,\n"
2238                    "#if 1\n"
2239                    "Two,\n"
2240                    "#if 0\n"
2241                    "Three,\n"
2242                    "      Four,\n"
2243                    "#endif\n"
2244                    "Five\n"
2245                    "#endif\n"
2246                    "};"));
2247   EXPECT_EQ("enum G {\n"
2248             "  One,\n"
2249             "#if 0\n"
2250             "Two,\n"
2251             "#else\n"
2252             "  Three,\n"
2253             "#endif\n"
2254             "  Four\n"
2255             "};",
2256             format("enum G {\n"
2257                    "One,\n"
2258                    "#if 0\n"
2259                    "Two,\n"
2260                    "#else\n"
2261                    "Three,\n"
2262                    "#endif\n"
2263                    "Four\n"
2264                    "};"));
2265   EXPECT_EQ("enum H {\n"
2266             "  One,\n"
2267             "#if 0\n"
2268             "#ifdef Q\n"
2269             "Two,\n"
2270             "#else\n"
2271             "Three,\n"
2272             "#endif\n"
2273             "#endif\n"
2274             "  Four\n"
2275             "};",
2276             format("enum H {\n"
2277                    "One,\n"
2278                    "#if 0\n"
2279                    "#ifdef Q\n"
2280                    "Two,\n"
2281                    "#else\n"
2282                    "Three,\n"
2283                    "#endif\n"
2284                    "#endif\n"
2285                    "Four\n"
2286                    "};"));
2287   EXPECT_EQ("enum I {\n"
2288             "  One,\n"
2289             "#if /* test */ 0 || 1\n"
2290             "Two,\n"
2291             "Three,\n"
2292             "#endif\n"
2293             "  Four\n"
2294             "};",
2295             format("enum I {\n"
2296                    "One,\n"
2297                    "#if /* test */ 0 || 1\n"
2298                    "Two,\n"
2299                    "Three,\n"
2300                    "#endif\n"
2301                    "Four\n"
2302                    "};"));
2303   EXPECT_EQ("enum J {\n"
2304             "  One,\n"
2305             "#if 0\n"
2306             "#if 0\n"
2307             "Two,\n"
2308             "#else\n"
2309             "Three,\n"
2310             "#endif\n"
2311             "Four,\n"
2312             "#endif\n"
2313             "  Five\n"
2314             "};",
2315             format("enum J {\n"
2316                    "One,\n"
2317                    "#if 0\n"
2318                    "#if 0\n"
2319                    "Two,\n"
2320                    "#else\n"
2321                    "Three,\n"
2322                    "#endif\n"
2323                    "Four,\n"
2324                    "#endif\n"
2325                    "Five\n"
2326                    "};"));
2327 
2328   // Ignore stuff in SWIG-blocks.
2329   EXPECT_EQ("#ifdef SWIG\n"
2330             "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
2331             "#endif\n"
2332             "void f() {}",
2333             format("#ifdef SWIG\n"
2334                    "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
2335                    "#endif\n"
2336                    "void f(  ) {  }"));
2337   EXPECT_EQ("#ifndef SWIG\n"
2338             "void f() {}\n"
2339             "#endif",
2340             format("#ifndef SWIG\n"
2341                    "void f(      ) {       }\n"
2342                    "#endif"));
2343 }
2344 
TEST_F(FormatTestComments,DontCrashOnBlockComments)2345 TEST_F(FormatTestComments, DontCrashOnBlockComments) {
2346   EXPECT_EQ(
2347       "int xxxxxxxxx; /* "
2348       "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\n"
2349       "zzzzzz\n"
2350       "0*/",
2351       format("int xxxxxxxxx;                          /* "
2352              "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy zzzzzz\n"
2353              "0*/"));
2354 }
2355 
TEST_F(FormatTestComments,BlockCommentsInControlLoops)2356 TEST_F(FormatTestComments, BlockCommentsInControlLoops) {
2357   verifyFormat("if (0) /* a comment in a strange place */ {\n"
2358                "  f();\n"
2359                "}");
2360   verifyFormat("if (0) /* a comment in a strange place */ {\n"
2361                "  f();\n"
2362                "} /* another comment */ else /* comment #3 */ {\n"
2363                "  g();\n"
2364                "}");
2365   verifyFormat("while (0) /* a comment in a strange place */ {\n"
2366                "  f();\n"
2367                "}");
2368   verifyFormat("for (;;) /* a comment in a strange place */ {\n"
2369                "  f();\n"
2370                "}");
2371   verifyFormat("do /* a comment in a strange place */ {\n"
2372                "  f();\n"
2373                "} /* another comment */ while (0);");
2374 }
2375 
TEST_F(FormatTestComments,BlockComments)2376 TEST_F(FormatTestComments, BlockComments) {
2377   EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */",
2378             format("/* *//* */  /* */\n/* *//* */  /* */"));
2379   EXPECT_EQ("/* */ a /* */ b;", format("  /* */  a/* */  b;"));
2380   EXPECT_EQ("#define A /*123*/ \\\n"
2381             "  b\n"
2382             "/* */\n"
2383             "someCall(\n"
2384             "    parameter);",
2385             format("#define A /*123*/ b\n"
2386                    "/* */\n"
2387                    "someCall(parameter);",
2388                    getLLVMStyleWithColumns(15)));
2389 
2390   EXPECT_EQ("#define A\n"
2391             "/* */ someCall(\n"
2392             "    parameter);",
2393             format("#define A\n"
2394                    "/* */someCall(parameter);",
2395                    getLLVMStyleWithColumns(15)));
2396   EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/"));
2397   EXPECT_EQ("/*\n"
2398             " *\n"
2399             " * aaaaaa\n"
2400             " * aaaaaa\n"
2401             " */",
2402             format("/*\n"
2403                    "*\n"
2404                    " * aaaaaa aaaaaa\n"
2405                    "*/",
2406                    getLLVMStyleWithColumns(10)));
2407   EXPECT_EQ("/*\n"
2408             "**\n"
2409             "* aaaaaa\n"
2410             "*aaaaaa\n"
2411             "*/",
2412             format("/*\n"
2413                    "**\n"
2414                    "* aaaaaa aaaaaa\n"
2415                    "*/",
2416                    getLLVMStyleWithColumns(10)));
2417   EXPECT_EQ("int aaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
2418             "    /* line 1\n"
2419             "       bbbbbbbbbbbb */\n"
2420             "    bbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
2421             format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
2422                    "    /* line 1\n"
2423                    "       bbbbbbbbbbbb */ bbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
2424             getLLVMStyleWithColumns(50)));
2425 
2426   FormatStyle NoBinPacking = getLLVMStyle();
2427   NoBinPacking.BinPackParameters = false;
2428   EXPECT_EQ("someFunction(1, /* comment 1 */\n"
2429             "             2, /* comment 2 */\n"
2430             "             3, /* comment 3 */\n"
2431             "             aaaa,\n"
2432             "             bbbb);",
2433             format("someFunction (1,   /* comment 1 */\n"
2434                    "                2, /* comment 2 */  \n"
2435                    "               3,   /* comment 3 */\n"
2436                    "aaaa, bbbb );",
2437                    NoBinPacking));
2438   verifyFormat(
2439       "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2440       "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
2441   EXPECT_EQ(
2442       "bool aaaaaaaaaaaaa = /* trailing comment */\n"
2443       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2444       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;",
2445       format(
2446           "bool       aaaaaaaaaaaaa =       /* trailing comment */\n"
2447           "    aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa    ||\n"
2448           "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa   || aaaaaaaaaaaaaaaaaaaaaaaaaa;"));
2449   EXPECT_EQ(
2450       "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
2451       "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;   /* comment */\n"
2452       "int cccccccccccccccccccccccccccccc;       /* comment */\n",
2453       format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
2454              "int      bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
2455              "int    cccccccccccccccccccccccccccccc;  /* comment */\n"));
2456 
2457   verifyFormat("void f(int * /* unused */) {}");
2458 
2459   EXPECT_EQ("/*\n"
2460             " **\n"
2461             " */",
2462             format("/*\n"
2463                    " **\n"
2464                    " */"));
2465   EXPECT_EQ("/*\n"
2466             " *q\n"
2467             " */",
2468             format("/*\n"
2469                    " *q\n"
2470                    " */"));
2471   EXPECT_EQ("/*\n"
2472             " * q\n"
2473             " */",
2474             format("/*\n"
2475                    " * q\n"
2476                    " */"));
2477   EXPECT_EQ("/*\n"
2478             " **/",
2479             format("/*\n"
2480                    " **/"));
2481   EXPECT_EQ("/*\n"
2482             " ***/",
2483             format("/*\n"
2484                    " ***/"));
2485 }
2486 
TEST_F(FormatTestComments,BlockCommentsInMacros)2487 TEST_F(FormatTestComments, BlockCommentsInMacros) {
2488   EXPECT_EQ("#define A          \\\n"
2489             "  {                \\\n"
2490             "    /* one line */ \\\n"
2491             "    someCall();",
2492             format("#define A {        \\\n"
2493                    "  /* one line */   \\\n"
2494                    "  someCall();",
2495                    getLLVMStyleWithColumns(20)));
2496   EXPECT_EQ("#define A          \\\n"
2497             "  {                \\\n"
2498             "    /* previous */ \\\n"
2499             "    /* one line */ \\\n"
2500             "    someCall();",
2501             format("#define A {        \\\n"
2502                    "  /* previous */   \\\n"
2503                    "  /* one line */   \\\n"
2504                    "  someCall();",
2505                    getLLVMStyleWithColumns(20)));
2506 }
2507 
TEST_F(FormatTestComments,BlockCommentsAtEndOfLine)2508 TEST_F(FormatTestComments, BlockCommentsAtEndOfLine) {
2509   EXPECT_EQ("a = {\n"
2510             "    1111 /*    */\n"
2511             "};",
2512             format("a = {1111 /*    */\n"
2513                    "};",
2514                    getLLVMStyleWithColumns(15)));
2515   EXPECT_EQ("a = {\n"
2516             "    1111 /*      */\n"
2517             "};",
2518             format("a = {1111 /*      */\n"
2519                    "};",
2520                    getLLVMStyleWithColumns(15)));
2521   EXPECT_EQ("a = {\n"
2522             "    1111 /*      a\n"
2523             "          */\n"
2524             "};",
2525             format("a = {1111 /*      a */\n"
2526                    "};",
2527                    getLLVMStyleWithColumns(15)));
2528 }
2529 
TEST_F(FormatTestComments,BreaksAfterMultilineBlockCommentsInParamLists)2530 TEST_F(FormatTestComments, BreaksAfterMultilineBlockCommentsInParamLists) {
2531   EXPECT_EQ("a = f(/* long\n"
2532             "         long */\n"
2533             "      a);",
2534             format("a = f(/* long long */ a);", getLLVMStyleWithColumns(16)));
2535   EXPECT_EQ("a = f(\n"
2536             "    /* long\n"
2537             "       long */\n"
2538             "    a);",
2539             format("a = f(/* long long */ a);", getLLVMStyleWithColumns(15)));
2540 
2541   EXPECT_EQ("a = f(/* long\n"
2542             "         long\n"
2543             "       */\n"
2544             "      a);",
2545             format("a = f(/* long\n"
2546                    "         long\n"
2547                    "       */a);",
2548                    getLLVMStyleWithColumns(16)));
2549 
2550   EXPECT_EQ("a = f(/* long\n"
2551             "         long\n"
2552             "       */\n"
2553             "      a);",
2554             format("a = f(/* long\n"
2555                    "         long\n"
2556                    "       */ a);",
2557                    getLLVMStyleWithColumns(16)));
2558 
2559   EXPECT_EQ("a = f(/* long\n"
2560             "         long\n"
2561             "       */\n"
2562             "      (1 + 1));",
2563             format("a = f(/* long\n"
2564                    "         long\n"
2565                    "       */ (1 + 1));",
2566                    getLLVMStyleWithColumns(16)));
2567 
2568   EXPECT_EQ(
2569       "a = f(a,\n"
2570       "      /* long\n"
2571       "         long */\n"
2572       "      b);",
2573       format("a = f(a, /* long long */ b);", getLLVMStyleWithColumns(16)));
2574 
2575   EXPECT_EQ(
2576       "a = f(\n"
2577       "    a,\n"
2578       "    /* long\n"
2579       "       long */\n"
2580       "    b);",
2581       format("a = f(a, /* long long */ b);", getLLVMStyleWithColumns(15)));
2582 
2583   EXPECT_EQ("a = f(a,\n"
2584             "      /* long\n"
2585             "         long */\n"
2586             "      (1 + 1));",
2587             format("a = f(a, /* long long */ (1 + 1));",
2588                    getLLVMStyleWithColumns(16)));
2589   EXPECT_EQ("a = f(\n"
2590             "    a,\n"
2591             "    /* long\n"
2592             "       long */\n"
2593             "    (1 + 1));",
2594             format("a = f(a, /* long long */ (1 + 1));",
2595                    getLLVMStyleWithColumns(15)));
2596 }
2597 
TEST_F(FormatTestComments,IndentLineCommentsInStartOfBlockAtEndOfFile)2598 TEST_F(FormatTestComments, IndentLineCommentsInStartOfBlockAtEndOfFile) {
2599   verifyFormat("{\n"
2600                "  // a\n"
2601                "  // b");
2602 }
2603 
TEST_F(FormatTestComments,AlignTrailingComments)2604 TEST_F(FormatTestComments, AlignTrailingComments) {
2605   EXPECT_EQ("#define MACRO(V)                       \\\n"
2606             "  V(Rt2) /* one more char */           \\\n"
2607             "  V(Rs)  /* than here  */              \\\n"
2608             "/* comment 3 */\n",
2609             format("#define MACRO(V)\\\n"
2610                    "V(Rt2)  /* one more char */ \\\n"
2611                    "V(Rs) /* than here  */    \\\n"
2612                    "/* comment 3 */\n",
2613                    getLLVMStyleWithColumns(40)));
2614   EXPECT_EQ("int i = f(abc, // line 1\n"
2615             "          d,   // line 2\n"
2616             "               // line 3\n"
2617             "          b);",
2618             format("int i = f(abc, // line 1\n"
2619                    "          d, // line 2\n"
2620                    "             // line 3\n"
2621                    "          b);",
2622                    getLLVMStyleWithColumns(40)));
2623 
2624   // Align newly broken trailing comments.
2625   EXPECT_EQ("int ab; // line\n"
2626             "int a;  // long\n"
2627             "        // long\n",
2628             format("int ab; // line\n"
2629                    "int a; // long long\n",
2630                    getLLVMStyleWithColumns(15)));
2631   EXPECT_EQ("int ab; // line\n"
2632             "int a;  // long\n"
2633             "        // long\n"
2634             "        // long",
2635             format("int ab; // line\n"
2636                    "int a; // long long\n"
2637                    "       // long",
2638                    getLLVMStyleWithColumns(15)));
2639   EXPECT_EQ("int ab; // line\n"
2640             "int a;  // long\n"
2641             "        // long\n"
2642             "pt c;   // long",
2643             format("int ab; // line\n"
2644                    "int a; // long long\n"
2645                    "pt c; // long",
2646                    getLLVMStyleWithColumns(15)));
2647   EXPECT_EQ("int ab; // line\n"
2648             "int a;  // long\n"
2649             "        // long\n"
2650             "\n"
2651             "// long",
2652             format("int ab; // line\n"
2653                    "int a; // long long\n"
2654                    "\n"
2655                    "// long",
2656                    getLLVMStyleWithColumns(15)));
2657 
2658   // Don't align newly broken trailing comments if that would put them over the
2659   // column limit.
2660   EXPECT_EQ("int i, j; // line 1\n"
2661             "int k; // line longg\n"
2662             "       // long",
2663             format("int i, j; // line 1\n"
2664                    "int k; // line longg long",
2665                    getLLVMStyleWithColumns(20)));
2666 
2667   // Always align if ColumnLimit = 0
2668   EXPECT_EQ("int i, j; // line 1\n"
2669             "int k;    // line longg long",
2670             format("int i, j; // line 1\n"
2671                    "int k; // line longg long",
2672                    getLLVMStyleWithColumns(0)));
2673 
2674   // Align comment line sections aligned with the next token with the next
2675   // token.
2676   EXPECT_EQ("class A {\n"
2677             "public: // public comment\n"
2678             "  // comment about a\n"
2679             "  int a;\n"
2680             "};",
2681             format("class A {\n"
2682                    "public: // public comment\n"
2683                    "  // comment about a\n"
2684                    "  int a;\n"
2685                    "};",
2686                    getLLVMStyleWithColumns(40)));
2687   EXPECT_EQ("class A {\n"
2688             "public: // public comment 1\n"
2689             "        // public comment 2\n"
2690             "  // comment 1 about a\n"
2691             "  // comment 2 about a\n"
2692             "  int a;\n"
2693             "};",
2694             format("class A {\n"
2695                    "public: // public comment 1\n"
2696                    "   // public comment 2\n"
2697                    "  // comment 1 about a\n"
2698                    "  // comment 2 about a\n"
2699                    "  int a;\n"
2700                    "};",
2701                    getLLVMStyleWithColumns(40)));
2702   EXPECT_EQ("int f(int n) { // comment line 1 on f\n"
2703             "               // comment line 2 on f\n"
2704             "  // comment line 1 before return\n"
2705             "  // comment line 2 before return\n"
2706             "  return n; // comment line 1 on return\n"
2707             "            // comment line 2 on return\n"
2708             "  // comment line 1 after return\n"
2709             "}",
2710             format("int f(int n) { // comment line 1 on f\n"
2711                    "   // comment line 2 on f\n"
2712                    "  // comment line 1 before return\n"
2713                    "  // comment line 2 before return\n"
2714                    "  return n; // comment line 1 on return\n"
2715                    "   // comment line 2 on return\n"
2716                    "  // comment line 1 after return\n"
2717                    "}",
2718                    getLLVMStyleWithColumns(40)));
2719   EXPECT_EQ("int f(int n) {\n"
2720             "  switch (n) { // comment line 1 on switch\n"
2721             "               // comment line 2 on switch\n"
2722             "  // comment line 1 before case 1\n"
2723             "  // comment line 2 before case 1\n"
2724             "  case 1: // comment line 1 on case 1\n"
2725             "          // comment line 2 on case 1\n"
2726             "    // comment line 1 before return 1\n"
2727             "    // comment line 2 before return 1\n"
2728             "    return 1; // comment line 1 on return 1\n"
2729             "              // comment line 2 on return 1\n"
2730             "  // comment line 1 before default\n"
2731             "  // comment line 2 before default\n"
2732             "  default: // comment line 1 on default\n"
2733             "           // comment line 2 on default\n"
2734             "    // comment line 1 before return 2\n"
2735             "    return 2 * f(n - 1); // comment line 1 on return 2\n"
2736             "                         // comment line 2 on return 2\n"
2737             "    // comment line 1 after return\n"
2738             "    // comment line 2 after return\n"
2739             "  }\n"
2740             "}",
2741             format("int f(int n) {\n"
2742                    "  switch (n) { // comment line 1 on switch\n"
2743                    "              // comment line 2 on switch\n"
2744                    "    // comment line 1 before case 1\n"
2745                    "    // comment line 2 before case 1\n"
2746                    "    case 1: // comment line 1 on case 1\n"
2747                    "              // comment line 2 on case 1\n"
2748                    "    // comment line 1 before return 1\n"
2749                    "    // comment line 2 before return 1\n"
2750                    "    return 1;  // comment line 1 on return 1\n"
2751                    "             // comment line 2 on return 1\n"
2752                    "    // comment line 1 before default\n"
2753                    "    // comment line 2 before default\n"
2754                    "    default:   // comment line 1 on default\n"
2755                    "                // comment line 2 on default\n"
2756                    "    // comment line 1 before return 2\n"
2757                    "    return 2 * f(n - 1); // comment line 1 on return 2\n"
2758                    "                        // comment line 2 on return 2\n"
2759                    "    // comment line 1 after return\n"
2760                    "     // comment line 2 after return\n"
2761                    "  }\n"
2762                    "}",
2763                    getLLVMStyleWithColumns(80)));
2764 
2765   // If all the lines in a sequence of line comments are aligned with the next
2766   // token, the first line belongs to the previous token and the other lines
2767   // belong to the next token.
2768   EXPECT_EQ("int a; // line about a\n"
2769             "long b;",
2770             format("int a; // line about a\n"
2771                    "       long b;",
2772                    getLLVMStyleWithColumns(80)));
2773   EXPECT_EQ("int a; // line about a\n"
2774             "// line about b\n"
2775             "long b;",
2776             format("int a; // line about a\n"
2777                    "       // line about b\n"
2778                    "       long b;",
2779                    getLLVMStyleWithColumns(80)));
2780   EXPECT_EQ("int a; // line about a\n"
2781             "// line 1 about b\n"
2782             "// line 2 about b\n"
2783             "long b;",
2784             format("int a; // line about a\n"
2785                    "       // line 1 about b\n"
2786                    "       // line 2 about b\n"
2787                    "       long b;",
2788                    getLLVMStyleWithColumns(80)));
2789 }
2790 
TEST_F(FormatTestComments,AlignsBlockCommentDecorations)2791 TEST_F(FormatTestComments, AlignsBlockCommentDecorations) {
2792   EXPECT_EQ("/*\n"
2793             " */",
2794             format("/*\n"
2795                    "*/", getLLVMStyle()));
2796   EXPECT_EQ("/*\n"
2797             " */",
2798             format("/*\n"
2799                    " */", getLLVMStyle()));
2800   EXPECT_EQ("/*\n"
2801             " */",
2802             format("/*\n"
2803                    "  */", getLLVMStyle()));
2804 
2805   // Align a single line.
2806   EXPECT_EQ("/*\n"
2807             " * line */",
2808             format("/*\n"
2809                    "* line */",
2810                    getLLVMStyle()));
2811   EXPECT_EQ("/*\n"
2812             " * line */",
2813             format("/*\n"
2814                    " * line */",
2815                    getLLVMStyle()));
2816   EXPECT_EQ("/*\n"
2817             " * line */",
2818             format("/*\n"
2819                    "  * line */",
2820                    getLLVMStyle()));
2821   EXPECT_EQ("/*\n"
2822             " * line */",
2823             format("/*\n"
2824                    "   * line */",
2825                    getLLVMStyle()));
2826   EXPECT_EQ("/**\n"
2827             " * line */",
2828             format("/**\n"
2829                    "* line */",
2830                    getLLVMStyle()));
2831   EXPECT_EQ("/**\n"
2832             " * line */",
2833             format("/**\n"
2834                    " * line */",
2835                    getLLVMStyle()));
2836   EXPECT_EQ("/**\n"
2837             " * line */",
2838             format("/**\n"
2839                    "  * line */",
2840                    getLLVMStyle()));
2841   EXPECT_EQ("/**\n"
2842             " * line */",
2843             format("/**\n"
2844                    "   * line */",
2845                    getLLVMStyle()));
2846   EXPECT_EQ("/**\n"
2847             " * line */",
2848             format("/**\n"
2849                    "    * line */",
2850                    getLLVMStyle()));
2851 
2852   // Align the end '*/' after a line.
2853   EXPECT_EQ("/*\n"
2854             " * line\n"
2855             " */",
2856             format("/*\n"
2857                    "* line\n"
2858                    "*/", getLLVMStyle()));
2859   EXPECT_EQ("/*\n"
2860             " * line\n"
2861             " */",
2862             format("/*\n"
2863                    "   * line\n"
2864                    "  */", getLLVMStyle()));
2865   EXPECT_EQ("/*\n"
2866             " * line\n"
2867             " */",
2868             format("/*\n"
2869                    "  * line\n"
2870                    "  */", getLLVMStyle()));
2871 
2872   // Align two lines.
2873   EXPECT_EQ("/* line 1\n"
2874             " * line 2 */",
2875             format("/* line 1\n"
2876                    " * line 2 */",
2877                    getLLVMStyle()));
2878   EXPECT_EQ("/* line 1\n"
2879             " * line 2 */",
2880             format("/* line 1\n"
2881                    "* line 2 */",
2882                    getLLVMStyle()));
2883   EXPECT_EQ("/* line 1\n"
2884             " * line 2 */",
2885             format("/* line 1\n"
2886                    "  * line 2 */",
2887                    getLLVMStyle()));
2888   EXPECT_EQ("/* line 1\n"
2889             " * line 2 */",
2890             format("/* line 1\n"
2891                    "   * line 2 */",
2892                    getLLVMStyle()));
2893   EXPECT_EQ("/* line 1\n"
2894             " * line 2 */",
2895             format("/* line 1\n"
2896                    "    * line 2 */",
2897                    getLLVMStyle()));
2898   EXPECT_EQ("int i; /* line 1\n"
2899             "        * line 2 */",
2900             format("int i; /* line 1\n"
2901                    "* line 2 */",
2902                    getLLVMStyle()));
2903   EXPECT_EQ("int i; /* line 1\n"
2904             "        * line 2 */",
2905             format("int i; /* line 1\n"
2906                    "        * line 2 */",
2907                    getLLVMStyle()));
2908   EXPECT_EQ("int i; /* line 1\n"
2909             "        * line 2 */",
2910             format("int i; /* line 1\n"
2911                    "             * line 2 */",
2912                    getLLVMStyle()));
2913 
2914   // Align several lines.
2915   EXPECT_EQ("/* line 1\n"
2916             " * line 2\n"
2917             " * line 3 */",
2918             format("/* line 1\n"
2919                    " * line 2\n"
2920                    "* line 3 */",
2921                    getLLVMStyle()));
2922   EXPECT_EQ("/* line 1\n"
2923             " * line 2\n"
2924             " * line 3 */",
2925             format("/* line 1\n"
2926                    "  * line 2\n"
2927                    "* line 3 */",
2928                    getLLVMStyle()));
2929   EXPECT_EQ("/*\n"
2930             "** line 1\n"
2931             "** line 2\n"
2932             "*/",
2933             format("/*\n"
2934                    "** line 1\n"
2935                    " ** line 2\n"
2936                    "*/",
2937                    getLLVMStyle()));
2938 
2939   // Align with different indent after the decorations.
2940   EXPECT_EQ("/*\n"
2941             " * line 1\n"
2942             " *  line 2\n"
2943             " * line 3\n"
2944             " *   line 4\n"
2945             " */",
2946             format("/*\n"
2947                    "* line 1\n"
2948                    "  *  line 2\n"
2949                    "   * line 3\n"
2950                    "*   line 4\n"
2951                    "*/", getLLVMStyle()));
2952 
2953   // Align empty or blank lines.
2954   EXPECT_EQ("/**\n"
2955             " *\n"
2956             " *\n"
2957             " *\n"
2958             " */",
2959             format("/**\n"
2960                    "*  \n"
2961                    " * \n"
2962                    "  *\n"
2963                    "*/", getLLVMStyle()));
2964 
2965   // Align while breaking and reflowing.
2966   EXPECT_EQ("/*\n"
2967             " * long long long\n"
2968             " * long long\n"
2969             " *\n"
2970             " * long */",
2971             format("/*\n"
2972                    " * long long long long\n"
2973                    " * long\n"
2974                    "  *\n"
2975                    "* long */",
2976                    getLLVMStyleWithColumns(20)));
2977 }
2978 
TEST_F(FormatTestComments,NoCrash_Bug34236)2979 TEST_F(FormatTestComments, NoCrash_Bug34236) {
2980   // This is a test case from a crasher reported in:
2981   // https://bugs.llvm.org/show_bug.cgi?id=34236
2982   // Temporarily disable formatting for readability.
2983   // clang-format off
2984   EXPECT_EQ(
2985 "/*                                                                */ /*\n"
2986 "                                                                      *       a\n"
2987 "                                                                      * b c d*/",
2988       format(
2989 "/*                                                                */ /*\n"
2990 " *       a b\n"
2991 " *       c     d*/",
2992           getLLVMStyleWithColumns(80)));
2993   // clang-format on
2994 }
2995 
TEST_F(FormatTestComments,NonTrailingBlockComments)2996 TEST_F(FormatTestComments, NonTrailingBlockComments) {
2997   verifyFormat("const /** comment comment */ A = B;",
2998                getLLVMStyleWithColumns(40));
2999 
3000   verifyFormat("const /** comment comment comment */ A =\n"
3001                "    B;",
3002                getLLVMStyleWithColumns(40));
3003 
3004   EXPECT_EQ("const /** comment comment comment\n"
3005             "         comment */\n"
3006             "    A = B;",
3007             format("const /** comment comment comment comment */\n"
3008                    "    A = B;",
3009                    getLLVMStyleWithColumns(40)));
3010 }
3011 
TEST_F(FormatTestComments,PythonStyleComments)3012 TEST_F(FormatTestComments, PythonStyleComments) {
3013   // Keeps a space after '#'.
3014   EXPECT_EQ("# comment\n"
3015             "key: value",
3016             format("#comment\n"
3017                    "key:value",
3018                    getTextProtoStyleWithColumns(20)));
3019   EXPECT_EQ("# comment\n"
3020             "key: value",
3021             format("# comment\n"
3022                    "key:value",
3023                    getTextProtoStyleWithColumns(20)));
3024   // Breaks long comment.
3025   EXPECT_EQ("# comment comment\n"
3026             "# comment\n"
3027             "key: value",
3028             format("# comment comment comment\n"
3029                    "key:value",
3030                    getTextProtoStyleWithColumns(20)));
3031   // Indents comments.
3032   EXPECT_EQ("data {\n"
3033             "  # comment comment\n"
3034             "  # comment\n"
3035             "  key: value\n"
3036             "}",
3037             format("data {\n"
3038                    "# comment comment comment\n"
3039                    "key: value}",
3040                    getTextProtoStyleWithColumns(20)));
3041   EXPECT_EQ("data {\n"
3042             "  # comment comment\n"
3043             "  # comment\n"
3044             "  key: value\n"
3045             "}",
3046             format("data {# comment comment comment\n"
3047                    "key: value}",
3048                    getTextProtoStyleWithColumns(20)));
3049   // Reflows long comments.
3050   EXPECT_EQ("# comment comment\n"
3051             "# comment comment\n"
3052             "key: value",
3053             format("# comment comment comment\n"
3054                    "# comment\n"
3055                    "key:value",
3056                    getTextProtoStyleWithColumns(20)));
3057   // Breaks trailing comments.
3058   EXPECT_EQ("k: val  # comment\n"
3059             "        # comment\n"
3060             "a: 1",
3061             format("k:val#comment comment\n"
3062                    "a:1",
3063                    getTextProtoStyleWithColumns(20)));
3064   EXPECT_EQ("id {\n"
3065             "  k: val  # comment\n"
3066             "          # comment\n"
3067             "  # line line\n"
3068             "  a: 1\n"
3069             "}",
3070             format("id {k:val#comment comment\n"
3071                    "# line line\n"
3072                    "a:1}",
3073                    getTextProtoStyleWithColumns(20)));
3074   // Aligns trailing comments.
3075   EXPECT_EQ("k: val  # commen1\n"
3076             "        # commen2\n"
3077             "        # commen3\n"
3078             "# commen4\n"
3079             "a: 1  # commen5\n"
3080             "      # commen6\n"
3081             "      # commen7",
3082             format("k:val#commen1 commen2\n"
3083                    " # commen3\n"
3084                    "# commen4\n"
3085                    "a:1#commen5 commen6\n"
3086                    " #commen7",
3087                    getTextProtoStyleWithColumns(20)));
3088 }
3089 
TEST_F(FormatTestComments,BreaksBeforeTrailingUnbreakableSequence)3090 TEST_F(FormatTestComments, BreaksBeforeTrailingUnbreakableSequence) {
3091   // The end of /* trail */ is exactly at 80 columns, but the unbreakable
3092   // trailing sequence ); after it exceeds the column limit. Make sure we
3093   // correctly break the line in that case.
3094   verifyFormat("int a =\n"
3095                "    foo(/* trail */);",
3096                getLLVMStyleWithColumns(23));
3097 }
3098 
TEST_F(FormatTestComments,ReflowBackslashCrash)3099 TEST_F(FormatTestComments, ReflowBackslashCrash) {
3100 // clang-format off
3101   EXPECT_EQ(
3102 "// How to run:\n"
3103 "// bbbbb run \\\n"
3104 "// rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr\n"
3105 "// \\ <log_file> -- --output_directory=\"<output_directory>\"",
3106   format(
3107 "// How to run:\n"
3108 "// bbbbb run \\\n"
3109 "// rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr \\\n"
3110 "// <log_file> -- --output_directory=\"<output_directory>\""));
3111 // clang-format on
3112 }
3113 
TEST_F(FormatTestComments,IndentsLongJavadocAnnotatedLines)3114 TEST_F(FormatTestComments, IndentsLongJavadocAnnotatedLines) {
3115   FormatStyle Style = getGoogleStyle(FormatStyle::LK_Java);
3116   Style.ColumnLimit = 60;
3117   FormatStyle Style20 = getGoogleStyle(FormatStyle::LK_Java);
3118   Style20.ColumnLimit = 20;
3119   EXPECT_EQ(
3120       "/**\n"
3121       " * @param x long long long long long long long long long\n"
3122       " *     long\n"
3123       " */\n",
3124       format("/**\n"
3125              " * @param x long long long long long long long long long long\n"
3126              " */\n",
3127              Style));
3128   EXPECT_EQ("/**\n"
3129             " * @param x long long long long long long long long long\n"
3130             " *     long long long long long long long long long long\n"
3131             " */\n",
3132             format("/**\n"
3133                    " * @param x long long long long long long long long long "
3134                    "long long long long long long long long long long\n"
3135                    " */\n",
3136                    Style));
3137   EXPECT_EQ("/**\n"
3138             " * @param x long long long long long long long long long\n"
3139             " *     long long long long long long long long long long\n"
3140             " *     long\n"
3141             " */\n",
3142             format("/**\n"
3143                    " * @param x long long long long long long long long long "
3144                    "long long long long long long long long long long long\n"
3145                    " */\n",
3146                    Style));
3147   EXPECT_EQ(
3148       "/**\n"
3149       " * Sentence that\n"
3150       " * should be broken.\n"
3151       " * @param short\n"
3152       " * keep indentation\n"
3153       " */\n", format(
3154           "/**\n"
3155           " * Sentence that should be broken.\n"
3156           " * @param short\n"
3157           " * keep indentation\n"
3158           " */\n", Style20));
3159 
3160   EXPECT_EQ("/**\n"
3161             " * @param l1 long1\n"
3162             " *     to break\n"
3163             " * @param l2 long2\n"
3164             " *     to break\n"
3165             " */\n",
3166             format("/**\n"
3167                    " * @param l1 long1 to break\n"
3168                    " * @param l2 long2 to break\n"
3169                    " */\n",
3170                    Style20));
3171 
3172   EXPECT_EQ("/**\n"
3173             " * @param xx to\n"
3174             " *     break\n"
3175             " * no reflow\n"
3176             " */\n",
3177             format("/**\n"
3178                    " * @param xx to break\n"
3179                    " * no reflow\n"
3180                    " */\n",
3181                    Style20));
3182 
3183   EXPECT_EQ("/**\n"
3184             " * @param xx to\n"
3185             " *     break yes\n"
3186             " *     reflow\n"
3187             " */\n",
3188             format("/**\n"
3189                    " * @param xx to break\n"
3190                    " *     yes reflow\n"
3191                    " */\n",
3192                    Style20));
3193 
3194   FormatStyle JSStyle20 = getGoogleStyle(FormatStyle::LK_JavaScript);
3195   JSStyle20.ColumnLimit = 20;
3196   EXPECT_EQ("/**\n"
3197             " * @param l1 long1\n"
3198             " *     to break\n"
3199             " */\n",
3200             format("/**\n"
3201                    " * @param l1 long1 to break\n"
3202                    " */\n",
3203                    JSStyle20));
3204   EXPECT_EQ("/**\n"
3205             " * @param {l1 long1\n"
3206             " *     to break}\n"
3207             " */\n",
3208             format("/**\n"
3209                    " * @param {l1 long1 to break}\n"
3210                    " */\n",
3211                    JSStyle20));
3212 }
3213 
3214 } // end namespace
3215 } // end namespace format
3216 } // end namespace clang
3217