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