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   EXPECT_EQ("/* This is a long\n"
1100             " * comment that\n"
1101             " * doesn't\n"
1102             " * fit on one line.\n"
1103             " */",
1104             format("/* "
1105                    "This is a long                                         "
1106                    "comment that "
1107                    "doesn't                                    "
1108                    "fit on one line.  */",
1109                    getLLVMStyleWithColumns(20)));
1110   EXPECT_EQ(
1111       "/* a b c d\n"
1112       " * e f  g\n"
1113       " * h i j k\n"
1114       " */",
1115       format("/* a b c d e f  g h i j k */", getLLVMStyleWithColumns(10)));
1116   EXPECT_EQ(
1117       "/* a b c d\n"
1118       " * e f  g\n"
1119       " * h i j k\n"
1120       " */",
1121       format("\\\n/* a b c d e f  g h i j k */", getLLVMStyleWithColumns(10)));
1122   EXPECT_EQ("/*\n"
1123             "This is a long\n"
1124             "comment that doesn't\n"
1125             "fit on one line.\n"
1126             "*/",
1127             format("/*\n"
1128                    "This is a long                                         "
1129                    "comment that doesn't                                    "
1130                    "fit on one line.                                      \n"
1131                    "*/",
1132                    getLLVMStyleWithColumns(20)));
1133   EXPECT_EQ("/*\n"
1134             " * This is a long\n"
1135             " * comment that\n"
1136             " * doesn't fit on\n"
1137             " * one line.\n"
1138             " */",
1139             format("/*      \n"
1140                    " * This is a long "
1141                    "   comment that     "
1142                    "   doesn't fit on   "
1143                    "   one line.                                            \n"
1144                    " */",
1145                    getLLVMStyleWithColumns(20)));
1146   EXPECT_EQ("/*\n"
1147             " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n"
1148             " * so_it_should_be_broken\n"
1149             " * wherever_a_space_occurs\n"
1150             " */",
1151             format("/*\n"
1152                    " * This_is_a_comment_with_words_that_dont_fit_on_one_line "
1153                    "   so_it_should_be_broken "
1154                    "   wherever_a_space_occurs                             \n"
1155                    " */",
1156                    getLLVMStyleWithColumns(20)));
1157   EXPECT_EQ("/*\n"
1158             " *    This_comment_can_not_be_broken_into_lines\n"
1159             " */",
1160             format("/*\n"
1161                    " *    This_comment_can_not_be_broken_into_lines\n"
1162                    " */",
1163                    getLLVMStyleWithColumns(20)));
1164   EXPECT_EQ("{\n"
1165             "  /*\n"
1166             "  This is another\n"
1167             "  long comment that\n"
1168             "  doesn't fit on one\n"
1169             "  line    1234567890\n"
1170             "  */\n"
1171             "}",
1172             format("{\n"
1173                    "/*\n"
1174                    "This is another     "
1175                    "  long comment that "
1176                    "  doesn't fit on one"
1177                    "  line    1234567890\n"
1178                    "*/\n"
1179                    "}",
1180                    getLLVMStyleWithColumns(20)));
1181   EXPECT_EQ("{\n"
1182             "  /*\n"
1183             "   * This        i s\n"
1184             "   * another comment\n"
1185             "   * t hat  doesn' t\n"
1186             "   * fit on one l i\n"
1187             "   * n e\n"
1188             "   */\n"
1189             "}",
1190             format("{\n"
1191                    "/*\n"
1192                    " * This        i s"
1193                    "   another comment"
1194                    "   t hat  doesn' t"
1195                    "   fit on one l i"
1196                    "   n e\n"
1197                    " */\n"
1198                    "}",
1199                    getLLVMStyleWithColumns(20)));
1200   EXPECT_EQ("/*\n"
1201             " * This is a long\n"
1202             " * comment that\n"
1203             " * doesn't fit on\n"
1204             " * one line\n"
1205             " */",
1206             format("   /*\n"
1207                    "    * This is a long comment that doesn't fit on one line\n"
1208                    "    */",
1209                    getLLVMStyleWithColumns(20)));
1210   EXPECT_EQ("{\n"
1211             "  if (something) /* This is a\n"
1212             "                    long\n"
1213             "                    comment */\n"
1214             "    ;\n"
1215             "}",
1216             format("{\n"
1217                    "  if (something) /* This is a long comment */\n"
1218                    "    ;\n"
1219                    "}",
1220                    getLLVMStyleWithColumns(30)));
1221 
1222   EXPECT_EQ("/* A comment before\n"
1223             " * a macro\n"
1224             " * definition */\n"
1225             "#define a b",
1226             format("/* A comment before a macro definition */\n"
1227                    "#define a b",
1228                    getLLVMStyleWithColumns(20)));
1229 
1230   EXPECT_EQ("/* some comment\n"
1231             " *   a comment that\n"
1232             " * we break another\n"
1233             " * comment we have\n"
1234             " * to break a left\n"
1235             " * comment\n"
1236             " */",
1237             format("  /* some comment\n"
1238                    "       *   a comment that we break\n"
1239                    "   * another comment we have to break\n"
1240                    "* a left comment\n"
1241                    "   */",
1242                    getLLVMStyleWithColumns(20)));
1243 
1244   EXPECT_EQ("/**\n"
1245             " * multiline block\n"
1246             " * comment\n"
1247             " *\n"
1248             " */",
1249             format("/**\n"
1250                    " * multiline block comment\n"
1251                    " *\n"
1252                    " */",
1253                    getLLVMStyleWithColumns(20)));
1254 
1255   EXPECT_EQ("/*\n"
1256             "\n"
1257             "\n"
1258             "    */\n",
1259             format("  /*       \n"
1260                    "      \n"
1261                    "               \n"
1262                    "      */\n"));
1263 
1264   EXPECT_EQ("/* a a */",
1265             format("/* a a            */", getLLVMStyleWithColumns(15)));
1266   EXPECT_EQ("/* a a bc  */",
1267             format("/* a a            bc  */", getLLVMStyleWithColumns(15)));
1268   EXPECT_EQ("/* aaa aaa\n"
1269             " * aaaaa */",
1270             format("/* aaa aaa aaaaa       */", getLLVMStyleWithColumns(15)));
1271   EXPECT_EQ("/* aaa aaa\n"
1272             " * aaaaa     */",
1273             format("/* aaa aaa aaaaa     */", getLLVMStyleWithColumns(15)));
1274 }
1275 
1276 TEST_F(FormatTestComments, SplitsLongLinesInCommentsInPreprocessor) {
1277   EXPECT_EQ("#define X          \\\n"
1278             "  /*               \\\n"
1279             "   Test            \\\n"
1280             "   Macro comment   \\\n"
1281             "   with a long     \\\n"
1282             "   line            \\\n"
1283             "   */              \\\n"
1284             "  A + B",
1285             format("#define X \\\n"
1286                    "  /*\n"
1287                    "   Test\n"
1288                    "   Macro comment with a long  line\n"
1289                    "   */ \\\n"
1290                    "  A + B",
1291                    getLLVMStyleWithColumns(20)));
1292   EXPECT_EQ("#define X          \\\n"
1293             "  /* Macro comment \\\n"
1294             "     with a long   \\\n"
1295             "     line */       \\\n"
1296             "  A + B",
1297             format("#define X \\\n"
1298                    "  /* Macro comment with a long\n"
1299                    "     line */ \\\n"
1300                    "  A + B",
1301                    getLLVMStyleWithColumns(20)));
1302   EXPECT_EQ("#define X          \\\n"
1303             "  /* Macro comment \\\n"
1304             "   * with a long   \\\n"
1305             "   * line */       \\\n"
1306             "  A + B",
1307             format("#define X \\\n"
1308                    "  /* Macro comment with a long  line */ \\\n"
1309                    "  A + B",
1310                    getLLVMStyleWithColumns(20)));
1311 }
1312 
1313 TEST_F(FormatTestComments, KeepsTrailingPPCommentsAndSectionCommentsSeparate) {
1314   verifyFormat("#ifdef A // line about A\n"
1315                "// section comment\n"
1316                "#endif",
1317                getLLVMStyleWithColumns(80));
1318   verifyFormat("#ifdef A // line 1 about A\n"
1319                "         // line 2 about A\n"
1320                "// section comment\n"
1321                "#endif",
1322                getLLVMStyleWithColumns(80));
1323   EXPECT_EQ("#ifdef A // line 1 about A\n"
1324             "         // line 2 about A\n"
1325             "// section comment\n"
1326             "#endif",
1327             format("#ifdef A // line 1 about A\n"
1328                    "          // line 2 about A\n"
1329                    "// section comment\n"
1330                    "#endif",
1331                    getLLVMStyleWithColumns(80)));
1332   verifyFormat("int f() {\n"
1333                "  int i;\n"
1334                "#ifdef A // comment about A\n"
1335                "  // section comment 1\n"
1336                "  // section comment 2\n"
1337                "  i = 2;\n"
1338                "#else // comment about #else\n"
1339                "  // section comment 3\n"
1340                "  i = 4;\n"
1341                "#endif\n"
1342                "}", getLLVMStyleWithColumns(80));
1343 }
1344 
1345 TEST_F(FormatTestComments, AlignsPPElseEndifComments) {
1346   verifyFormat("#if A\n"
1347                "#else  // A\n"
1348                "int iiii;\n"
1349                "#endif // B",
1350                getLLVMStyleWithColumns(20));
1351   verifyFormat("#if A\n"
1352                "#else  // A\n"
1353                "int iiii; // CC\n"
1354                "#endif // B",
1355                getLLVMStyleWithColumns(20));
1356   EXPECT_EQ("#if A\n"
1357             "#else  // A1\n"
1358             "       // A2\n"
1359             "int ii;\n"
1360             "#endif // B",
1361             format("#if A\n"
1362                    "#else  // A1\n"
1363                    "       // A2\n"
1364                    "int ii;\n"
1365                    "#endif // B",
1366                    getLLVMStyleWithColumns(20)));
1367 }
1368 
1369 TEST_F(FormatTestComments, CommentsInStaticInitializers) {
1370   EXPECT_EQ(
1371       "static SomeType type = {aaaaaaaaaaaaaaaaaaaa, /* comment */\n"
1372       "                        aaaaaaaaaaaaaaaaaaaa /* comment */,\n"
1373       "                        /* comment */ aaaaaaaaaaaaaaaaaaaa,\n"
1374       "                        aaaaaaaaaaaaaaaaaaaa, // comment\n"
1375       "                        aaaaaaaaaaaaaaaaaaaa};",
1376       format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa  ,  /* comment */\n"
1377              "                   aaaaaaaaaaaaaaaaaaaa   /* comment */ ,\n"
1378              "                     /* comment */   aaaaaaaaaaaaaaaaaaaa ,\n"
1379              "              aaaaaaaaaaaaaaaaaaaa ,   // comment\n"
1380              "                  aaaaaaaaaaaaaaaaaaaa };"));
1381   verifyFormat("static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
1382                "                        bbbbbbbbbbb, ccccccccccc};");
1383   verifyFormat("static SomeType type = {aaaaaaaaaaa,\n"
1384                "                        // comment for bb....\n"
1385                "                        bbbbbbbbbbb, ccccccccccc};");
1386   verifyGoogleFormat(
1387       "static SomeType type = {aaaaaaaaaaa,  // comment for aa...\n"
1388       "                        bbbbbbbbbbb, ccccccccccc};");
1389   verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n"
1390                      "                        // comment for bb....\n"
1391                      "                        bbbbbbbbbbb, ccccccccccc};");
1392 
1393   verifyFormat("S s = {{a, b, c},  // Group #1\n"
1394                "       {d, e, f},  // Group #2\n"
1395                "       {g, h, i}}; // Group #3");
1396   verifyFormat("S s = {{// Group #1\n"
1397                "        a, b, c},\n"
1398                "       {// Group #2\n"
1399                "        d, e, f},\n"
1400                "       {// Group #3\n"
1401                "        g, h, i}};");
1402 
1403   EXPECT_EQ("S s = {\n"
1404             "    // Some comment\n"
1405             "    a,\n"
1406             "\n"
1407             "    // Comment after empty line\n"
1408             "    b}",
1409             format("S s =    {\n"
1410                    "      // Some comment\n"
1411                    "  a,\n"
1412                    "  \n"
1413                    "     // Comment after empty line\n"
1414                    "      b\n"
1415                    "}"));
1416   EXPECT_EQ("S s = {\n"
1417             "    /* Some comment */\n"
1418             "    a,\n"
1419             "\n"
1420             "    /* Comment after empty line */\n"
1421             "    b}",
1422             format("S s =    {\n"
1423                    "      /* Some comment */\n"
1424                    "  a,\n"
1425                    "  \n"
1426                    "     /* Comment after empty line */\n"
1427                    "      b\n"
1428                    "}"));
1429   verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n"
1430                "    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
1431                "    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
1432                "    0x00, 0x00, 0x00, 0x00};            // comment\n");
1433 }
1434 
1435 TEST_F(FormatTestComments, LineCommentsAfterRightBrace) {
1436   EXPECT_EQ("if (true) { // comment about branch\n"
1437             "  // comment about f\n"
1438             "  f();\n"
1439             "}",
1440             format("if (true) { // comment about branch\n"
1441                    "  // comment about f\n"
1442                    "  f();\n"
1443                    "}",
1444                    getLLVMStyleWithColumns(80)));
1445   EXPECT_EQ("if (1) { // if line 1\n"
1446             "         // if line 2\n"
1447             "         // if line 3\n"
1448             "  // f line 1\n"
1449             "  // f line 2\n"
1450             "  f();\n"
1451             "} else { // else line 1\n"
1452             "         // else line 2\n"
1453             "         // else line 3\n"
1454             "  // g line 1\n"
1455             "  g();\n"
1456             "}",
1457             format("if (1) { // if line 1\n"
1458                    "          // if line 2\n"
1459                    "        // if line 3\n"
1460                    "  // f line 1\n"
1461                    "    // f line 2\n"
1462                    "  f();\n"
1463                    "} else { // else line 1\n"
1464                    "        // else line 2\n"
1465                    "         // else line 3\n"
1466                    "  // g line 1\n"
1467                    "  g();\n"
1468                    "}"));
1469   EXPECT_EQ("do { // line 1\n"
1470             "     // line 2\n"
1471             "     // line 3\n"
1472             "  f();\n"
1473             "} while (true);",
1474             format("do { // line 1\n"
1475                    "     // line 2\n"
1476                    "   // line 3\n"
1477                    "  f();\n"
1478                    "} while (true);",
1479                    getLLVMStyleWithColumns(80)));
1480   EXPECT_EQ("while (a < b) { // line 1\n"
1481             "  // line 2\n"
1482             "  // line 3\n"
1483             "  f();\n"
1484             "}",
1485             format("while (a < b) {// line 1\n"
1486                    "  // line 2\n"
1487                    "  // line 3\n"
1488                    "  f();\n"
1489                    "}",
1490                    getLLVMStyleWithColumns(80)));
1491 }
1492 
1493 TEST_F(FormatTestComments, ReflowsComments) {
1494   // Break a long line and reflow with the full next line.
1495   EXPECT_EQ("// long long long\n"
1496             "// long long",
1497             format("// long long long long\n"
1498                    "// long",
1499                    getLLVMStyleWithColumns(20)));
1500 
1501   // Keep the trailing newline while reflowing.
1502   EXPECT_EQ("// long long long\n"
1503             "// long long\n",
1504             format("// long long long long\n"
1505                    "// long\n",
1506                    getLLVMStyleWithColumns(20)));
1507 
1508   // Break a long line and reflow with a part of the next line.
1509   EXPECT_EQ("// long long long\n"
1510             "// long long\n"
1511             "// long_long",
1512             format("// long long long long\n"
1513                    "// long long_long",
1514                    getLLVMStyleWithColumns(20)));
1515 
1516   // Break but do not reflow if the first word from the next line is too long.
1517   EXPECT_EQ("// long long long\n"
1518             "// long\n"
1519             "// long_long_long\n",
1520             format("// long long long long\n"
1521                    "// long_long_long\n",
1522                    getLLVMStyleWithColumns(20)));
1523 
1524   // Don't break or reflow short lines.
1525   verifyFormat("// long\n"
1526                "// long long long lo\n"
1527                "// long long long lo\n"
1528                "// long",
1529                getLLVMStyleWithColumns(20));
1530 
1531   // Keep prefixes and decorations while reflowing.
1532   EXPECT_EQ("/// long long long\n"
1533             "/// long long\n",
1534             format("/// long long long long\n"
1535                    "/// long\n",
1536                    getLLVMStyleWithColumns(20)));
1537   EXPECT_EQ("//! long long long\n"
1538             "//! long long\n",
1539             format("//! long long long long\n"
1540                    "//! long\n",
1541                    getLLVMStyleWithColumns(20)));
1542   EXPECT_EQ("/* long long long\n"
1543             " * long long */",
1544             format("/* long long long long\n"
1545                    " * long */",
1546                    getLLVMStyleWithColumns(20)));
1547   EXPECT_EQ("///< long long long\n"
1548             "///< long long\n",
1549             format("///< long long long long\n"
1550                    "///< long\n",
1551                    getLLVMStyleWithColumns(20)));
1552   EXPECT_EQ("//!< long long long\n"
1553             "//!< long long\n",
1554             format("//!< long long long long\n"
1555                    "//!< long\n",
1556                    getLLVMStyleWithColumns(20)));
1557 
1558   // Don't bring leading whitespace up while reflowing.
1559   EXPECT_EQ("/*  long long long\n"
1560             " * long long long\n"
1561             " */",
1562             format("/*  long long long long\n"
1563                    " *  long long\n"
1564                    " */",
1565                    getLLVMStyleWithColumns(20)));
1566 
1567   // Reflow the last line of a block comment with its trailing '*/'.
1568   EXPECT_EQ("/* long long long\n"
1569             "   long long */",
1570             format("/* long long long long\n"
1571                    "   long */",
1572                    getLLVMStyleWithColumns(20)));
1573 
1574   // Reflow two short lines; keep the postfix of the last one.
1575   EXPECT_EQ("/* long long long\n"
1576             " * long long long */",
1577             format("/* long long long long\n"
1578                    " * long\n"
1579                    " * long */",
1580                    getLLVMStyleWithColumns(20)));
1581 
1582   // Put the postfix of the last short reflow line on a newline if it doesn't
1583   // fit.
1584   EXPECT_EQ("/* long long long\n"
1585             " * long long longg\n"
1586             " */",
1587             format("/* long long long long\n"
1588                    " * long\n"
1589                    " * longg */",
1590                    getLLVMStyleWithColumns(20)));
1591 
1592   // Reflow lines with leading whitespace.
1593   EXPECT_EQ("{\n"
1594             "  /*\n"
1595             "   * long long long\n"
1596             "   * long long long\n"
1597             "   * long long long\n"
1598             "   */\n"
1599             "}",
1600             format("{\n"
1601                    "/*\n"
1602                    " * long long long long\n"
1603                    " *   long\n"
1604                    " * long long long long\n"
1605                    " */\n"
1606                    "}",
1607                    getLLVMStyleWithColumns(20)));
1608 
1609   // Break single line block comments that are first in the line with ' *'
1610   // decoration.
1611   EXPECT_EQ("/* long long long\n"
1612             " * long */",
1613             format("/* long long long long */", getLLVMStyleWithColumns(20)));
1614 
1615   // Break single line block comment that are not first in the line with '  '
1616   // decoration.
1617   EXPECT_EQ("int i; /* long long\n"
1618             "          long */",
1619             format("int i; /* long long long */", getLLVMStyleWithColumns(20)));
1620 
1621   // Reflow a line that goes just over the column limit.
1622   EXPECT_EQ("// long long long\n"
1623             "// lon long",
1624             format("// long long long lon\n"
1625                    "// long",
1626                    getLLVMStyleWithColumns(20)));
1627 
1628   // Stop reflowing if the next line has a different indentation than the
1629   // previous line.
1630   EXPECT_EQ("// long long long\n"
1631             "// long\n"
1632             "//  long long\n"
1633             "//  long",
1634             format("// long long long long\n"
1635                    "//  long long\n"
1636                    "//  long",
1637                    getLLVMStyleWithColumns(20)));
1638 
1639   // Reflow into the last part of a really long line that has been broken into
1640   // multiple lines.
1641   EXPECT_EQ("// long long long\n"
1642             "// long long long\n"
1643             "// long long long\n",
1644             format("// long long long long long long long long\n"
1645                    "// long\n",
1646                    getLLVMStyleWithColumns(20)));
1647 
1648   // Break the first line, then reflow the beginning of the second and third
1649   // line up.
1650   EXPECT_EQ("// long long long\n"
1651             "// lon1 lon2 lon2\n"
1652             "// lon2 lon3 lon3",
1653             format("// long long long lon1\n"
1654                    "// lon2 lon2 lon2\n"
1655                    "// lon3 lon3",
1656                    getLLVMStyleWithColumns(20)));
1657 
1658   // Reflow the beginning of the second line, then break the rest.
1659   EXPECT_EQ("// long long long\n"
1660             "// lon1 lon2 lon2\n"
1661             "// lon2 lon2 lon2\n"
1662             "// lon3",
1663             format("// long long long lon1\n"
1664                    "// lon2 lon2 lon2 lon2 lon2 lon3",
1665                    getLLVMStyleWithColumns(20)));
1666 
1667   // Shrink the first line, then reflow the second line up.
1668   EXPECT_EQ("// long long long", format("// long              long\n"
1669                                         "// long",
1670                                         getLLVMStyleWithColumns(20)));
1671 
1672   // Don't shrink leading whitespace.
1673   EXPECT_EQ("int i; ///           a",
1674             format("int i; ///           a", getLLVMStyleWithColumns(20)));
1675 
1676   // Shrink trailing whitespace if there is no postfix and reflow.
1677   EXPECT_EQ("// long long long\n"
1678             "// long long",
1679             format("// long long long long    \n"
1680                    "// long",
1681                    getLLVMStyleWithColumns(20)));
1682 
1683   // Shrink trailing whitespace to a single one if there is postfix.
1684   EXPECT_EQ("/* long long long */",
1685             format("/* long long long     */", getLLVMStyleWithColumns(20)));
1686 
1687   // Break a block comment postfix if exceeding the line limit.
1688   EXPECT_EQ("/*               long\n"
1689             " */",
1690             format("/*               long */", getLLVMStyleWithColumns(20)));
1691 
1692   // Reflow indented comments.
1693   EXPECT_EQ("{\n"
1694             "  // long long long\n"
1695             "  // long long\n"
1696             "  int i; /* long lon\n"
1697             "            g long\n"
1698             "          */\n"
1699             "}",
1700             format("{\n"
1701                    "  // long long long long\n"
1702                    "  // long\n"
1703                    "  int i; /* long lon g\n"
1704                    "            long */\n"
1705                    "}",
1706                    getLLVMStyleWithColumns(20)));
1707 
1708   // Don't realign trailing comments after reflow has happened.
1709   EXPECT_EQ("// long long long\n"
1710             "// long long\n"
1711             "long i; // long",
1712             format("// long long long long\n"
1713                    "// long\n"
1714                    "long i; // long",
1715                    getLLVMStyleWithColumns(20)));
1716   EXPECT_EQ("// long long long\n"
1717             "// longng long long\n"
1718             "// long lo",
1719             format("// long long long longng\n"
1720                    "// long long long\n"
1721                    "// lo",
1722                    getLLVMStyleWithColumns(20)));
1723 
1724   // Reflow lines after a broken line.
1725   EXPECT_EQ("int a; // Trailing\n"
1726             "       // comment on\n"
1727             "       // 2 or 3\n"
1728             "       // lines.\n",
1729             format("int a; // Trailing comment\n"
1730                    "       // on 2\n"
1731                    "       // or 3\n"
1732                    "       // lines.\n",
1733                    getLLVMStyleWithColumns(20)));
1734   EXPECT_EQ("/// This long line\n"
1735             "/// gets reflown.\n",
1736             format("/// This long line gets\n"
1737                    "/// reflown.\n",
1738                    getLLVMStyleWithColumns(20)));
1739   EXPECT_EQ("//! This long line\n"
1740             "//! gets reflown.\n",
1741             format(" //! This long line gets\n"
1742                    " //! reflown.\n",
1743                    getLLVMStyleWithColumns(20)));
1744   EXPECT_EQ("/* This long line\n"
1745             " * gets reflown.\n"
1746             " */\n",
1747             format("/* This long line gets\n"
1748                    " * reflown.\n"
1749                    " */\n",
1750                    getLLVMStyleWithColumns(20)));
1751 
1752   // Reflow after indentation makes a line too long.
1753   EXPECT_EQ("{\n"
1754             "  // long long long\n"
1755             "  // lo long\n"
1756             "}\n",
1757             format("{\n"
1758                    "// long long long lo\n"
1759                    "// long\n"
1760                    "}\n",
1761                    getLLVMStyleWithColumns(20)));
1762 
1763   // Break and reflow multiple lines.
1764   EXPECT_EQ("/*\n"
1765             " * Reflow the end of\n"
1766             " * line by 11 22 33\n"
1767             " * 4.\n"
1768             " */\n",
1769             format("/*\n"
1770                    " * Reflow the end of line\n"
1771                    " * by\n"
1772                    " * 11\n"
1773                    " * 22\n"
1774                    " * 33\n"
1775                    " * 4.\n"
1776                    " */\n",
1777                    getLLVMStyleWithColumns(20)));
1778   EXPECT_EQ("/// First line gets\n"
1779             "/// broken. Second\n"
1780             "/// line gets\n"
1781             "/// reflown and\n"
1782             "/// broken. Third\n"
1783             "/// gets reflown.\n",
1784             format("/// First line gets broken.\n"
1785                    "/// Second line gets reflown and broken.\n"
1786                    "/// Third gets reflown.\n",
1787                    getLLVMStyleWithColumns(20)));
1788   EXPECT_EQ("int i; // first long\n"
1789             "       // long snd\n"
1790             "       // long.\n",
1791             format("int i; // first long long\n"
1792                    "       // snd long.\n",
1793                    getLLVMStyleWithColumns(20)));
1794   EXPECT_EQ("{\n"
1795             "  // first long line\n"
1796             "  // line second\n"
1797             "  // long line line\n"
1798             "  // third long line\n"
1799             "  // line\n"
1800             "}\n",
1801             format("{\n"
1802                    "  // first long line line\n"
1803                    "  // second long line line\n"
1804                    "  // third long line line\n"
1805                    "}\n",
1806                    getLLVMStyleWithColumns(20)));
1807   EXPECT_EQ("int i; /* first line\n"
1808             "        * second\n"
1809             "        * line third\n"
1810             "        * line\n"
1811             "        */",
1812             format("int i; /* first line\n"
1813                    "        * second line\n"
1814                    "        * third line\n"
1815                    "        */",
1816                    getLLVMStyleWithColumns(20)));
1817 
1818   // Reflow the last two lines of a section that starts with a line having
1819   // different indentation.
1820   EXPECT_EQ(
1821       "//     long\n"
1822       "// long long long\n"
1823       "// long long",
1824       format("//     long\n"
1825              "// long long long long\n"
1826              "// long",
1827              getLLVMStyleWithColumns(20)));
1828 
1829   // Keep the block comment endling '*/' while reflowing.
1830   EXPECT_EQ("/* Long long long\n"
1831             " * line short */\n",
1832             format("/* Long long long line\n"
1833                    " * short */\n",
1834                    getLLVMStyleWithColumns(20)));
1835 
1836   // Don't reflow between separate blocks of comments.
1837   EXPECT_EQ("/* First comment\n"
1838             " * block will */\n"
1839             "/* Snd\n"
1840             " */\n",
1841             format("/* First comment block\n"
1842                    " * will */\n"
1843                    "/* Snd\n"
1844                    " */\n",
1845                    getLLVMStyleWithColumns(20)));
1846 
1847   // Don't reflow across blank comment lines.
1848   EXPECT_EQ("int i; // This long\n"
1849             "       // line gets\n"
1850             "       // broken.\n"
1851             "       //\n"
1852             "       // keep.\n",
1853             format("int i; // This long line gets broken.\n"
1854                    "       //  \n"
1855                    "       // keep.\n",
1856                    getLLVMStyleWithColumns(20)));
1857   EXPECT_EQ("{\n"
1858             "  /// long long long\n"
1859             "  /// long long\n"
1860             "  ///\n"
1861             "  /// long\n"
1862             "}",
1863             format("{\n"
1864                    "  /// long long long long\n"
1865                    "  /// long\n"
1866                    "  ///\n"
1867                    "  /// long\n"
1868                    "}",
1869                    getLLVMStyleWithColumns(20)));
1870   EXPECT_EQ("//! long long long\n"
1871             "//! long\n"
1872             "\n"
1873             "//! long",
1874             format("//! long long long long\n"
1875                    "\n"
1876                    "//! long",
1877                    getLLVMStyleWithColumns(20)));
1878   EXPECT_EQ("/* long long long\n"
1879             "   long\n"
1880             "\n"
1881             "   long */",
1882             format("/* long long long long\n"
1883                    "\n"
1884                    "   long */",
1885                    getLLVMStyleWithColumns(20)));
1886   EXPECT_EQ("/* long long long\n"
1887             " * long\n"
1888             " *\n"
1889             " * long */",
1890             format("/* long long long long\n"
1891                    " *\n"
1892                    " * long */",
1893                    getLLVMStyleWithColumns(20)));
1894 
1895   // Don't reflow lines having content that is a single character.
1896   EXPECT_EQ("// long long long\n"
1897             "// long\n"
1898             "// l",
1899             format("// long long long long\n"
1900                    "// l",
1901                    getLLVMStyleWithColumns(20)));
1902 
1903   // Don't reflow lines starting with two punctuation characters.
1904   EXPECT_EQ("// long long long\n"
1905             "// long\n"
1906             "// ... --- ...",
1907             format(
1908                 "// long long long long\n"
1909                 "// ... --- ...",
1910                 getLLVMStyleWithColumns(20)));
1911 
1912   // Don't reflow lines starting with '@'.
1913   EXPECT_EQ("// long long long\n"
1914             "// long\n"
1915             "// @param arg",
1916             format("// long long long long\n"
1917                    "// @param arg",
1918                    getLLVMStyleWithColumns(20)));
1919 
1920   // Don't reflow lines starting with 'TODO'.
1921   EXPECT_EQ("// long long long\n"
1922             "// long\n"
1923             "// TODO: long",
1924             format("// long long long long\n"
1925                    "// TODO: long",
1926                    getLLVMStyleWithColumns(20)));
1927 
1928   // Don't reflow lines starting with 'FIXME'.
1929   EXPECT_EQ("// long long long\n"
1930             "// long\n"
1931             "// FIXME: long",
1932             format("// long long long long\n"
1933                    "// FIXME: long",
1934                    getLLVMStyleWithColumns(20)));
1935 
1936   // Don't reflow lines starting with 'XXX'.
1937   EXPECT_EQ("// long long long\n"
1938             "// long\n"
1939             "// XXX: long",
1940             format("// long long long long\n"
1941                    "// XXX: long",
1942                    getLLVMStyleWithColumns(20)));
1943 
1944   // Don't reflow comment pragmas.
1945   EXPECT_EQ("// long long long\n"
1946             "// long\n"
1947             "// IWYU pragma:",
1948             format("// long long long long\n"
1949                    "// IWYU pragma:",
1950                    getLLVMStyleWithColumns(20)));
1951   EXPECT_EQ("/* long long long\n"
1952             " * long\n"
1953             " * IWYU pragma:\n"
1954             " */",
1955             format("/* long long long long\n"
1956                    " * IWYU pragma:\n"
1957                    " */",
1958                    getLLVMStyleWithColumns(20)));
1959 
1960   // Reflow lines that have a non-punctuation character among their first 2
1961   // characters.
1962   EXPECT_EQ("// long long long\n"
1963             "// long 'long'",
1964             format(
1965                 "// long long long long\n"
1966                 "// 'long'",
1967                 getLLVMStyleWithColumns(20)));
1968 
1969   // Don't reflow between separate blocks of comments.
1970   EXPECT_EQ("/* First comment\n"
1971             " * block will */\n"
1972             "/* Snd\n"
1973             " */\n",
1974             format("/* First comment block\n"
1975                    " * will */\n"
1976                    "/* Snd\n"
1977                    " */\n",
1978                    getLLVMStyleWithColumns(20)));
1979 
1980   // Don't reflow lines having different indentation.
1981   EXPECT_EQ("// long long long\n"
1982             "// long\n"
1983             "//  long",
1984             format("// long long long long\n"
1985                    "//  long",
1986                    getLLVMStyleWithColumns(20)));
1987 
1988   // Don't reflow separate bullets in list
1989   EXPECT_EQ("// - long long long\n"
1990             "// long\n"
1991             "// - long",
1992             format("// - long long long long\n"
1993                    "// - long",
1994                    getLLVMStyleWithColumns(20)));
1995   EXPECT_EQ("// * long long long\n"
1996             "// long\n"
1997             "// * long",
1998             format("// * long long long long\n"
1999                    "// * long",
2000                    getLLVMStyleWithColumns(20)));
2001   EXPECT_EQ("// + long long long\n"
2002             "// long\n"
2003             "// + long",
2004             format("// + long long long long\n"
2005                    "// + long",
2006                    getLLVMStyleWithColumns(20)));
2007   EXPECT_EQ("// 1. long long long\n"
2008             "// long\n"
2009             "// 2. long",
2010             format("// 1. long long long long\n"
2011                    "// 2. long",
2012                    getLLVMStyleWithColumns(20)));
2013   EXPECT_EQ("// -# long long long\n"
2014             "// long\n"
2015             "// -# long",
2016             format("// -# long long long long\n"
2017                    "// -# long",
2018                    getLLVMStyleWithColumns(20)));
2019 
2020   EXPECT_EQ("// - long long long\n"
2021             "// long long long\n"
2022             "// - long",
2023             format("// - long long long long\n"
2024                    "// long long\n"
2025                    "// - long",
2026                    getLLVMStyleWithColumns(20)));
2027   EXPECT_EQ("// - long long long\n"
2028             "// long long long\n"
2029             "// long\n"
2030             "// - long",
2031             format("// - long long long long\n"
2032                    "// long long long\n"
2033                    "// - long",
2034                    getLLVMStyleWithColumns(20)));
2035 
2036   // Large number (>2 digits) are not list items
2037   EXPECT_EQ("// long long long\n"
2038             "// long 1024. long.",
2039             format("// long long long long\n"
2040                    "// 1024. long.",
2041                    getLLVMStyleWithColumns(20)));
2042 
2043   // Do not break before number, to avoid introducing a non-reflowable doxygen
2044   // list item.
2045   EXPECT_EQ("// long long\n"
2046             "// long 10. long.",
2047             format("// long long long 10.\n"
2048                    "// long.",
2049                    getLLVMStyleWithColumns(20)));
2050 
2051   // Don't break or reflow after implicit string literals.
2052   verifyFormat("#include <t> // l l l\n"
2053                "             // l",
2054                getLLVMStyleWithColumns(20));
2055 
2056   // Don't break or reflow comments on import lines.
2057   EXPECT_EQ("#include \"t\" /* l l l\n"
2058             "                * l */",
2059             format("#include \"t\" /* l l l\n"
2060                    "                * l */",
2061                    getLLVMStyleWithColumns(20)));
2062 
2063   // Don't reflow between different trailing comment sections.
2064   EXPECT_EQ("int i; // long long\n"
2065             "       // long\n"
2066             "int j; // long long\n"
2067             "       // long\n",
2068             format("int i; // long long long\n"
2069                    "int j; // long long long\n",
2070                    getLLVMStyleWithColumns(20)));
2071 
2072   // Don't reflow if the first word on the next line is longer than the
2073   // available space at current line.
2074   EXPECT_EQ("int i; // trigger\n"
2075             "       // reflow\n"
2076             "       // longsec\n",
2077             format("int i; // trigger reflow\n"
2078                    "       // longsec\n",
2079                    getLLVMStyleWithColumns(20)));
2080 
2081   // Keep empty comment lines.
2082   EXPECT_EQ("/**/", format(" /**/", getLLVMStyleWithColumns(20)));
2083   EXPECT_EQ("/* */", format(" /* */", getLLVMStyleWithColumns(20)));
2084   EXPECT_EQ("/*  */", format(" /*  */", getLLVMStyleWithColumns(20)));
2085   EXPECT_EQ("//", format(" //  ", getLLVMStyleWithColumns(20)));
2086   EXPECT_EQ("///", format(" ///  ", getLLVMStyleWithColumns(20)));
2087 }
2088 
2089 TEST_F(FormatTestComments, IgnoresIf0Contents) {
2090   EXPECT_EQ("#if 0\n"
2091             "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
2092             "#endif\n"
2093             "void f() {}",
2094             format("#if 0\n"
2095                    "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
2096                    "#endif\n"
2097                    "void f(  ) {  }"));
2098   EXPECT_EQ("#if false\n"
2099             "void f(  ) {  }\n"
2100             "#endif\n"
2101             "void g() {}\n",
2102             format("#if false\n"
2103                    "void f(  ) {  }\n"
2104                    "#endif\n"
2105                    "void g(  ) {  }\n"));
2106   EXPECT_EQ("enum E {\n"
2107             "  One,\n"
2108             "  Two,\n"
2109             "#if 0\n"
2110             "Three,\n"
2111             "      Four,\n"
2112             "#endif\n"
2113             "  Five\n"
2114             "};",
2115             format("enum E {\n"
2116                    "  One,Two,\n"
2117                    "#if 0\n"
2118                    "Three,\n"
2119                    "      Four,\n"
2120                    "#endif\n"
2121                    "  Five};"));
2122   EXPECT_EQ("enum F {\n"
2123             "  One,\n"
2124             "#if 1\n"
2125             "  Two,\n"
2126             "#if 0\n"
2127             "Three,\n"
2128             "      Four,\n"
2129             "#endif\n"
2130             "  Five\n"
2131             "#endif\n"
2132             "};",
2133             format("enum F {\n"
2134                    "One,\n"
2135                    "#if 1\n"
2136                    "Two,\n"
2137                    "#if 0\n"
2138                    "Three,\n"
2139                    "      Four,\n"
2140                    "#endif\n"
2141                    "Five\n"
2142                    "#endif\n"
2143                    "};"));
2144   EXPECT_EQ("enum G {\n"
2145             "  One,\n"
2146             "#if 0\n"
2147             "Two,\n"
2148             "#else\n"
2149             "  Three,\n"
2150             "#endif\n"
2151             "  Four\n"
2152             "};",
2153             format("enum G {\n"
2154                    "One,\n"
2155                    "#if 0\n"
2156                    "Two,\n"
2157                    "#else\n"
2158                    "Three,\n"
2159                    "#endif\n"
2160                    "Four\n"
2161                    "};"));
2162   EXPECT_EQ("enum H {\n"
2163             "  One,\n"
2164             "#if 0\n"
2165             "#ifdef Q\n"
2166             "Two,\n"
2167             "#else\n"
2168             "Three,\n"
2169             "#endif\n"
2170             "#endif\n"
2171             "  Four\n"
2172             "};",
2173             format("enum H {\n"
2174                    "One,\n"
2175                    "#if 0\n"
2176                    "#ifdef Q\n"
2177                    "Two,\n"
2178                    "#else\n"
2179                    "Three,\n"
2180                    "#endif\n"
2181                    "#endif\n"
2182                    "Four\n"
2183                    "};"));
2184   EXPECT_EQ("enum I {\n"
2185             "  One,\n"
2186             "#if /* test */ 0 || 1\n"
2187             "Two,\n"
2188             "Three,\n"
2189             "#endif\n"
2190             "  Four\n"
2191             "};",
2192             format("enum I {\n"
2193                    "One,\n"
2194                    "#if /* test */ 0 || 1\n"
2195                    "Two,\n"
2196                    "Three,\n"
2197                    "#endif\n"
2198                    "Four\n"
2199                    "};"));
2200   EXPECT_EQ("enum J {\n"
2201             "  One,\n"
2202             "#if 0\n"
2203             "#if 0\n"
2204             "Two,\n"
2205             "#else\n"
2206             "Three,\n"
2207             "#endif\n"
2208             "Four,\n"
2209             "#endif\n"
2210             "  Five\n"
2211             "};",
2212             format("enum J {\n"
2213                    "One,\n"
2214                    "#if 0\n"
2215                    "#if 0\n"
2216                    "Two,\n"
2217                    "#else\n"
2218                    "Three,\n"
2219                    "#endif\n"
2220                    "Four,\n"
2221                    "#endif\n"
2222                    "Five\n"
2223                    "};"));
2224 
2225   // Ignore stuff in SWIG-blocks.
2226   EXPECT_EQ("#ifdef SWIG\n"
2227             "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
2228             "#endif\n"
2229             "void f() {}",
2230             format("#ifdef SWIG\n"
2231                    "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
2232                    "#endif\n"
2233                    "void f(  ) {  }"));
2234   EXPECT_EQ("#ifndef SWIG\n"
2235             "void f() {}\n"
2236             "#endif",
2237             format("#ifndef SWIG\n"
2238                    "void f(      ) {       }\n"
2239                    "#endif"));
2240 }
2241 
2242 TEST_F(FormatTestComments, DontCrashOnBlockComments) {
2243   EXPECT_EQ(
2244       "int xxxxxxxxx; /* "
2245       "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\n"
2246       "zzzzzz\n"
2247       "0*/",
2248       format("int xxxxxxxxx;                          /* "
2249              "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy zzzzzz\n"
2250              "0*/"));
2251 }
2252 
2253 TEST_F(FormatTestComments, BlockCommentsInControlLoops) {
2254   verifyFormat("if (0) /* a comment in a strange place */ {\n"
2255                "  f();\n"
2256                "}");
2257   verifyFormat("if (0) /* a comment in a strange place */ {\n"
2258                "  f();\n"
2259                "} /* another comment */ else /* comment #3 */ {\n"
2260                "  g();\n"
2261                "}");
2262   verifyFormat("while (0) /* a comment in a strange place */ {\n"
2263                "  f();\n"
2264                "}");
2265   verifyFormat("for (;;) /* a comment in a strange place */ {\n"
2266                "  f();\n"
2267                "}");
2268   verifyFormat("do /* a comment in a strange place */ {\n"
2269                "  f();\n"
2270                "} /* another comment */ while (0);");
2271 }
2272 
2273 TEST_F(FormatTestComments, BlockComments) {
2274   EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */",
2275             format("/* *//* */  /* */\n/* *//* */  /* */"));
2276   EXPECT_EQ("/* */ a /* */ b;", format("  /* */  a/* */  b;"));
2277   EXPECT_EQ("#define A /*123*/ \\\n"
2278             "  b\n"
2279             "/* */\n"
2280             "someCall(\n"
2281             "    parameter);",
2282             format("#define A /*123*/ b\n"
2283                    "/* */\n"
2284                    "someCall(parameter);",
2285                    getLLVMStyleWithColumns(15)));
2286 
2287   EXPECT_EQ("#define A\n"
2288             "/* */ someCall(\n"
2289             "    parameter);",
2290             format("#define A\n"
2291                    "/* */someCall(parameter);",
2292                    getLLVMStyleWithColumns(15)));
2293   EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/"));
2294   EXPECT_EQ("/*\n"
2295             " *\n"
2296             " * aaaaaa\n"
2297             " * aaaaaa\n"
2298             " */",
2299             format("/*\n"
2300                    "*\n"
2301                    " * aaaaaa aaaaaa\n"
2302                    "*/",
2303                    getLLVMStyleWithColumns(10)));
2304   EXPECT_EQ("/*\n"
2305             "**\n"
2306             "* aaaaaa\n"
2307             "*aaaaaa\n"
2308             "*/",
2309             format("/*\n"
2310                    "**\n"
2311                    "* aaaaaa aaaaaa\n"
2312                    "*/",
2313                    getLLVMStyleWithColumns(10)));
2314   EXPECT_EQ("int aaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
2315             "    /* line 1\n"
2316             "       bbbbbbbbbbbb */\n"
2317             "    bbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
2318             format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
2319                    "    /* line 1\n"
2320                    "       bbbbbbbbbbbb */ bbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
2321             getLLVMStyleWithColumns(50)));
2322 
2323   FormatStyle NoBinPacking = getLLVMStyle();
2324   NoBinPacking.BinPackParameters = false;
2325   EXPECT_EQ("someFunction(1, /* comment 1 */\n"
2326             "             2, /* comment 2 */\n"
2327             "             3, /* comment 3 */\n"
2328             "             aaaa,\n"
2329             "             bbbb);",
2330             format("someFunction (1,   /* comment 1 */\n"
2331                    "                2, /* comment 2 */  \n"
2332                    "               3,   /* comment 3 */\n"
2333                    "aaaa, bbbb );",
2334                    NoBinPacking));
2335   verifyFormat(
2336       "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2337       "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
2338   EXPECT_EQ(
2339       "bool aaaaaaaaaaaaa = /* trailing comment */\n"
2340       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2341       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;",
2342       format(
2343           "bool       aaaaaaaaaaaaa =       /* trailing comment */\n"
2344           "    aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa    ||\n"
2345           "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa   || aaaaaaaaaaaaaaaaaaaaaaaaaa;"));
2346   EXPECT_EQ(
2347       "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
2348       "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;   /* comment */\n"
2349       "int cccccccccccccccccccccccccccccc;       /* comment */\n",
2350       format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
2351              "int      bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
2352              "int    cccccccccccccccccccccccccccccc;  /* comment */\n"));
2353 
2354   verifyFormat("void f(int * /* unused */) {}");
2355 
2356   EXPECT_EQ("/*\n"
2357             " **\n"
2358             " */",
2359             format("/*\n"
2360                    " **\n"
2361                    " */"));
2362   EXPECT_EQ("/*\n"
2363             " *q\n"
2364             " */",
2365             format("/*\n"
2366                    " *q\n"
2367                    " */"));
2368   EXPECT_EQ("/*\n"
2369             " * q\n"
2370             " */",
2371             format("/*\n"
2372                    " * q\n"
2373                    " */"));
2374   EXPECT_EQ("/*\n"
2375             " **/",
2376             format("/*\n"
2377                    " **/"));
2378   EXPECT_EQ("/*\n"
2379             " ***/",
2380             format("/*\n"
2381                    " ***/"));
2382 }
2383 
2384 TEST_F(FormatTestComments, BlockCommentsInMacros) {
2385   EXPECT_EQ("#define A          \\\n"
2386             "  {                \\\n"
2387             "    /* one line */ \\\n"
2388             "    someCall();",
2389             format("#define A {        \\\n"
2390                    "  /* one line */   \\\n"
2391                    "  someCall();",
2392                    getLLVMStyleWithColumns(20)));
2393   EXPECT_EQ("#define A          \\\n"
2394             "  {                \\\n"
2395             "    /* previous */ \\\n"
2396             "    /* one line */ \\\n"
2397             "    someCall();",
2398             format("#define A {        \\\n"
2399                    "  /* previous */   \\\n"
2400                    "  /* one line */   \\\n"
2401                    "  someCall();",
2402                    getLLVMStyleWithColumns(20)));
2403 }
2404 
2405 TEST_F(FormatTestComments, BlockCommentsAtEndOfLine) {
2406   EXPECT_EQ("a = {\n"
2407             "    1111 /*    */\n"
2408             "};",
2409             format("a = {1111 /*    */\n"
2410                    "};",
2411                    getLLVMStyleWithColumns(15)));
2412   EXPECT_EQ("a = {\n"
2413             "    1111 /*      */\n"
2414             "};",
2415             format("a = {1111 /*      */\n"
2416                    "};",
2417                    getLLVMStyleWithColumns(15)));
2418   EXPECT_EQ("a = {\n"
2419             "    1111 /*      a\n"
2420             "          */\n"
2421             "};",
2422             format("a = {1111 /*      a */\n"
2423                    "};",
2424                    getLLVMStyleWithColumns(15)));
2425 }
2426 
2427 TEST_F(FormatTestComments, BreaksAfterMultilineBlockCommentsInParamLists) {
2428   EXPECT_EQ("a = f(/* long\n"
2429             "         long\n"
2430             "       */\n"
2431             "      a);",
2432             format("a = f(/* long long */ a);", getLLVMStyleWithColumns(15)));
2433 
2434   EXPECT_EQ("a = f(/* long\n"
2435             "         long\n"
2436             "       */\n"
2437             "      a);",
2438             format("a = f(/* long\n"
2439                    "         long\n"
2440                    "       */a);",
2441                    getLLVMStyleWithColumns(15)));
2442 
2443   EXPECT_EQ("a = f(/* long\n"
2444             "         long\n"
2445             "       */\n"
2446             "      a);",
2447             format("a = f(/* long\n"
2448                    "         long\n"
2449                    "       */ a);",
2450                    getLLVMStyleWithColumns(15)));
2451 
2452   EXPECT_EQ("a = f(/* long\n"
2453             "         long\n"
2454             "       */\n"
2455             "      (1 + 1));",
2456             format("a = f(/* long\n"
2457                    "         long\n"
2458                    "       */ (1 + 1));",
2459                    getLLVMStyleWithColumns(15)));
2460 
2461   EXPECT_EQ(
2462       "a = f(a,\n"
2463       "      /* long\n"
2464       "         long\n"
2465       "       */\n"
2466       "      b);",
2467       format("a = f(a, /* long long */ b);", getLLVMStyleWithColumns(15)));
2468 
2469   EXPECT_EQ(
2470       "a = f(a,\n"
2471       "      /* long\n"
2472       "         long\n"
2473       "       */\n"
2474       "      (1 + 1));",
2475       format("a = f(a, /* long long */ (1 + 1));", getLLVMStyleWithColumns(15)));
2476 }
2477 
2478 TEST_F(FormatTestComments, IndentLineCommentsInStartOfBlockAtEndOfFile) {
2479   verifyFormat("{\n"
2480                "  // a\n"
2481                "  // b");
2482 }
2483 
2484 TEST_F(FormatTestComments, AlignTrailingComments) {
2485   EXPECT_EQ("#define MACRO(V)                       \\\n"
2486             "  V(Rt2) /* one more char */           \\\n"
2487             "  V(Rs)  /* than here  */              \\\n"
2488             "/* comment 3 */\n",
2489             format("#define MACRO(V)\\\n"
2490                    "V(Rt2)  /* one more char */ \\\n"
2491                    "V(Rs) /* than here  */    \\\n"
2492                    "/* comment 3 */\n",
2493                    getLLVMStyleWithColumns(40)));
2494   EXPECT_EQ("int i = f(abc, // line 1\n"
2495             "          d,   // line 2\n"
2496             "               // line 3\n"
2497             "          b);",
2498             format("int i = f(abc, // line 1\n"
2499                    "          d, // line 2\n"
2500                    "             // line 3\n"
2501                    "          b);",
2502                    getLLVMStyleWithColumns(40)));
2503 
2504   // Align newly broken trailing comments.
2505   EXPECT_EQ("int ab; // line\n"
2506             "int a;  // long\n"
2507             "        // long\n",
2508             format("int ab; // line\n"
2509                    "int a; // long long\n",
2510                    getLLVMStyleWithColumns(15)));
2511   EXPECT_EQ("int ab; // line\n"
2512             "int a;  // long\n"
2513             "        // long\n"
2514             "        // long",
2515             format("int ab; // line\n"
2516                    "int a; // long long\n"
2517                    "       // long",
2518                    getLLVMStyleWithColumns(15)));
2519   EXPECT_EQ("int ab; // line\n"
2520             "int a;  // long\n"
2521             "        // long\n"
2522             "pt c;   // long",
2523             format("int ab; // line\n"
2524                    "int a; // long long\n"
2525                    "pt c; // long",
2526                    getLLVMStyleWithColumns(15)));
2527   EXPECT_EQ("int ab; // line\n"
2528             "int a;  // long\n"
2529             "        // long\n"
2530             "\n"
2531             "// long",
2532             format("int ab; // line\n"
2533                    "int a; // long long\n"
2534                    "\n"
2535                    "// long",
2536                    getLLVMStyleWithColumns(15)));
2537 
2538   // Don't align newly broken trailing comments if that would put them over the
2539   // column limit.
2540   EXPECT_EQ("int i, j; // line 1\n"
2541             "int k; // line longg\n"
2542             "       // long",
2543             format("int i, j; // line 1\n"
2544                    "int k; // line longg long",
2545                    getLLVMStyleWithColumns(20)));
2546 
2547   // Always align if ColumnLimit = 0
2548   EXPECT_EQ("int i, j; // line 1\n"
2549             "int k;    // line longg long",
2550             format("int i, j; // line 1\n"
2551                    "int k; // line longg long",
2552                    getLLVMStyleWithColumns(0)));
2553 
2554   // Align comment line sections aligned with the next token with the next
2555   // token.
2556   EXPECT_EQ("class A {\n"
2557             "public: // public comment\n"
2558             "  // comment about a\n"
2559             "  int a;\n"
2560             "};",
2561             format("class A {\n"
2562                    "public: // public comment\n"
2563                    "  // comment about a\n"
2564                    "  int a;\n"
2565                    "};",
2566                    getLLVMStyleWithColumns(40)));
2567   EXPECT_EQ("class A {\n"
2568             "public: // public comment 1\n"
2569             "        // public comment 2\n"
2570             "  // comment 1 about a\n"
2571             "  // comment 2 about a\n"
2572             "  int a;\n"
2573             "};",
2574             format("class A {\n"
2575                    "public: // public comment 1\n"
2576                    "   // public comment 2\n"
2577                    "  // comment 1 about a\n"
2578                    "  // comment 2 about a\n"
2579                    "  int a;\n"
2580                    "};",
2581                    getLLVMStyleWithColumns(40)));
2582   EXPECT_EQ("int f(int n) { // comment line 1 on f\n"
2583             "               // comment line 2 on f\n"
2584             "  // comment line 1 before return\n"
2585             "  // comment line 2 before return\n"
2586             "  return n; // comment line 1 on return\n"
2587             "            // comment line 2 on return\n"
2588             "  // comment line 1 after return\n"
2589             "}",
2590             format("int f(int n) { // comment line 1 on f\n"
2591                    "   // comment line 2 on f\n"
2592                    "  // comment line 1 before return\n"
2593                    "  // comment line 2 before return\n"
2594                    "  return n; // comment line 1 on return\n"
2595                    "   // comment line 2 on return\n"
2596                    "  // comment line 1 after return\n"
2597                    "}",
2598                    getLLVMStyleWithColumns(40)));
2599   EXPECT_EQ("int f(int n) {\n"
2600             "  switch (n) { // comment line 1 on switch\n"
2601             "               // comment line 2 on switch\n"
2602             "  // comment line 1 before case 1\n"
2603             "  // comment line 2 before case 1\n"
2604             "  case 1: // comment line 1 on case 1\n"
2605             "          // comment line 2 on case 1\n"
2606             "    // comment line 1 before return 1\n"
2607             "    // comment line 2 before return 1\n"
2608             "    return 1; // comment line 1 on return 1\n"
2609             "              // comment line 2 on return 1\n"
2610             "  // comment line 1 before default\n"
2611             "  // comment line 2 before default\n"
2612             "  default: // comment line 1 on default\n"
2613             "           // comment line 2 on default\n"
2614             "    // comment line 1 before return 2\n"
2615             "    return 2 * f(n - 1); // comment line 1 on return 2\n"
2616             "                         // comment line 2 on return 2\n"
2617             "    // comment line 1 after return\n"
2618             "    // comment line 2 after return\n"
2619             "  }\n"
2620             "}",
2621             format("int f(int n) {\n"
2622                    "  switch (n) { // comment line 1 on switch\n"
2623                    "              // comment line 2 on switch\n"
2624                    "    // comment line 1 before case 1\n"
2625                    "    // comment line 2 before case 1\n"
2626                    "    case 1: // comment line 1 on case 1\n"
2627                    "              // comment line 2 on case 1\n"
2628                    "    // comment line 1 before return 1\n"
2629                    "    // comment line 2 before return 1\n"
2630                    "    return 1;  // comment line 1 on return 1\n"
2631                    "             // comment line 2 on return 1\n"
2632                    "    // comment line 1 before default\n"
2633                    "    // comment line 2 before default\n"
2634                    "    default:   // comment line 1 on default\n"
2635                    "                // comment line 2 on default\n"
2636                    "    // comment line 1 before return 2\n"
2637                    "    return 2 * f(n - 1); // comment line 1 on return 2\n"
2638                    "                        // comment line 2 on return 2\n"
2639                    "    // comment line 1 after return\n"
2640                    "     // comment line 2 after return\n"
2641                    "  }\n"
2642                    "}",
2643                    getLLVMStyleWithColumns(80)));
2644 
2645   // If all the lines in a sequence of line comments are aligned with the next
2646   // token, the first line belongs to the previous token and the other lines
2647   // belong to the next token.
2648   EXPECT_EQ("int a; // line about a\n"
2649             "long b;",
2650             format("int a; // line about a\n"
2651                    "       long b;",
2652                    getLLVMStyleWithColumns(80)));
2653   EXPECT_EQ("int a; // line about a\n"
2654             "// line about b\n"
2655             "long b;",
2656             format("int a; // line about a\n"
2657                    "       // line about b\n"
2658                    "       long b;",
2659                    getLLVMStyleWithColumns(80)));
2660   EXPECT_EQ("int a; // line about a\n"
2661             "// line 1 about b\n"
2662             "// line 2 about b\n"
2663             "long b;",
2664             format("int a; // line about a\n"
2665                    "       // line 1 about b\n"
2666                    "       // line 2 about b\n"
2667                    "       long b;",
2668                    getLLVMStyleWithColumns(80)));
2669 }
2670 
2671 TEST_F(FormatTestComments, AlignsBlockCommentDecorations) {
2672   EXPECT_EQ("/*\n"
2673             " */",
2674             format("/*\n"
2675                    "*/", getLLVMStyle()));
2676   EXPECT_EQ("/*\n"
2677             " */",
2678             format("/*\n"
2679                    " */", getLLVMStyle()));
2680   EXPECT_EQ("/*\n"
2681             " */",
2682             format("/*\n"
2683                    "  */", getLLVMStyle()));
2684 
2685   // Align a single line.
2686   EXPECT_EQ("/*\n"
2687             " * line */",
2688             format("/*\n"
2689                    "* line */",
2690                    getLLVMStyle()));
2691   EXPECT_EQ("/*\n"
2692             " * line */",
2693             format("/*\n"
2694                    " * line */",
2695                    getLLVMStyle()));
2696   EXPECT_EQ("/*\n"
2697             " * line */",
2698             format("/*\n"
2699                    "  * line */",
2700                    getLLVMStyle()));
2701   EXPECT_EQ("/*\n"
2702             " * line */",
2703             format("/*\n"
2704                    "   * line */",
2705                    getLLVMStyle()));
2706   EXPECT_EQ("/**\n"
2707             " * line */",
2708             format("/**\n"
2709                    "* line */",
2710                    getLLVMStyle()));
2711   EXPECT_EQ("/**\n"
2712             " * line */",
2713             format("/**\n"
2714                    " * line */",
2715                    getLLVMStyle()));
2716   EXPECT_EQ("/**\n"
2717             " * line */",
2718             format("/**\n"
2719                    "  * line */",
2720                    getLLVMStyle()));
2721   EXPECT_EQ("/**\n"
2722             " * line */",
2723             format("/**\n"
2724                    "   * line */",
2725                    getLLVMStyle()));
2726   EXPECT_EQ("/**\n"
2727             " * line */",
2728             format("/**\n"
2729                    "    * line */",
2730                    getLLVMStyle()));
2731 
2732   // Align the end '*/' after a line.
2733   EXPECT_EQ("/*\n"
2734             " * line\n"
2735             " */",
2736             format("/*\n"
2737                    "* line\n"
2738                    "*/", getLLVMStyle()));
2739   EXPECT_EQ("/*\n"
2740             " * line\n"
2741             " */",
2742             format("/*\n"
2743                    "   * line\n"
2744                    "  */", getLLVMStyle()));
2745   EXPECT_EQ("/*\n"
2746             " * line\n"
2747             " */",
2748             format("/*\n"
2749                    "  * line\n"
2750                    "  */", getLLVMStyle()));
2751 
2752   // Align two lines.
2753   EXPECT_EQ("/* line 1\n"
2754             " * line 2 */",
2755             format("/* line 1\n"
2756                    " * line 2 */",
2757                    getLLVMStyle()));
2758   EXPECT_EQ("/* line 1\n"
2759             " * line 2 */",
2760             format("/* line 1\n"
2761                    "* line 2 */",
2762                    getLLVMStyle()));
2763   EXPECT_EQ("/* line 1\n"
2764             " * line 2 */",
2765             format("/* line 1\n"
2766                    "  * line 2 */",
2767                    getLLVMStyle()));
2768   EXPECT_EQ("/* line 1\n"
2769             " * line 2 */",
2770             format("/* line 1\n"
2771                    "   * line 2 */",
2772                    getLLVMStyle()));
2773   EXPECT_EQ("/* line 1\n"
2774             " * line 2 */",
2775             format("/* line 1\n"
2776                    "    * line 2 */",
2777                    getLLVMStyle()));
2778   EXPECT_EQ("int i; /* line 1\n"
2779             "        * line 2 */",
2780             format("int i; /* line 1\n"
2781                    "* line 2 */",
2782                    getLLVMStyle()));
2783   EXPECT_EQ("int i; /* line 1\n"
2784             "        * line 2 */",
2785             format("int i; /* line 1\n"
2786                    "        * line 2 */",
2787                    getLLVMStyle()));
2788   EXPECT_EQ("int i; /* line 1\n"
2789             "        * line 2 */",
2790             format("int i; /* line 1\n"
2791                    "             * line 2 */",
2792                    getLLVMStyle()));
2793 
2794   // Align several lines.
2795   EXPECT_EQ("/* line 1\n"
2796             " * line 2\n"
2797             " * line 3 */",
2798             format("/* line 1\n"
2799                    " * line 2\n"
2800                    "* line 3 */",
2801                    getLLVMStyle()));
2802   EXPECT_EQ("/* line 1\n"
2803             " * line 2\n"
2804             " * line 3 */",
2805             format("/* line 1\n"
2806                    "  * line 2\n"
2807                    "* line 3 */",
2808                    getLLVMStyle()));
2809   EXPECT_EQ("/*\n"
2810             "** line 1\n"
2811             "** line 2\n"
2812             "*/",
2813             format("/*\n"
2814                    "** line 1\n"
2815                    " ** line 2\n"
2816                    "*/",
2817                    getLLVMStyle()));
2818 
2819   // Align with different indent after the decorations.
2820   EXPECT_EQ("/*\n"
2821             " * line 1\n"
2822             " *  line 2\n"
2823             " * line 3\n"
2824             " *   line 4\n"
2825             " */",
2826             format("/*\n"
2827                    "* line 1\n"
2828                    "  *  line 2\n"
2829                    "   * line 3\n"
2830                    "*   line 4\n"
2831                    "*/", getLLVMStyle()));
2832 
2833   // Align empty or blank lines.
2834   EXPECT_EQ("/**\n"
2835             " *\n"
2836             " *\n"
2837             " *\n"
2838             " */",
2839             format("/**\n"
2840                    "*  \n"
2841                    " * \n"
2842                    "  *\n"
2843                    "*/", getLLVMStyle()));
2844 
2845   // Align while breaking and reflowing.
2846   EXPECT_EQ("/*\n"
2847             " * long long long\n"
2848             " * long long\n"
2849             " *\n"
2850             " * long */",
2851             format("/*\n"
2852                    " * long long long long\n"
2853                    " * long\n"
2854                    "  *\n"
2855                    "* long */",
2856                    getLLVMStyleWithColumns(20)));
2857 }
2858 
2859 TEST_F(FormatTestComments, NoCrush_Bug34236) {
2860   // This is a test case from a crasher reported in:
2861   // https://bugs.llvm.org/show_bug.cgi?id=34236
2862   // Temporarily disable formatting for readability.
2863   // clang-format off
2864   EXPECT_EQ(
2865 "/*                                                                */ /*\n"
2866 "                                                                      *       a\n"
2867 "                                                                      * b c\n"
2868 "                                                                      * d*/",
2869       format(
2870 "/*                                                                */ /*\n"
2871 " *       a b\n"
2872 " *       c     d*/",
2873           getLLVMStyleWithColumns(80)));
2874   // clang-format on
2875 }
2876 
2877 TEST_F(FormatTestComments, NonTrailingBlockComments) {
2878   verifyFormat("const /** comment comment */ A = B;",
2879                getLLVMStyleWithColumns(40));
2880 
2881   verifyFormat("const /** comment comment comment */ A =\n"
2882                "    B;",
2883                getLLVMStyleWithColumns(40));
2884 
2885   EXPECT_EQ("const /** comment comment comment\n"
2886             "         comment */\n"
2887             "    A = B;",
2888             format("const /** comment comment comment comment */\n"
2889                    "    A = B;",
2890                    getLLVMStyleWithColumns(40)));
2891 }
2892 
2893 TEST_F(FormatTestComments, PythonStyleComments) {
2894   // Keeps a space after '#'.
2895   EXPECT_EQ("# comment\n"
2896             "key: value",
2897             format("#comment\n"
2898                    "key:value",
2899                    getTextProtoStyleWithColumns(20)));
2900   EXPECT_EQ("# comment\n"
2901             "key: value",
2902             format("# comment\n"
2903                    "key:value",
2904                    getTextProtoStyleWithColumns(20)));
2905   // Breaks long comment.
2906   EXPECT_EQ("# comment comment\n"
2907             "# comment\n"
2908             "key: value",
2909             format("# comment comment comment\n"
2910                    "key:value",
2911                    getTextProtoStyleWithColumns(20)));
2912   // Indents comments.
2913   EXPECT_EQ("data {\n"
2914             "  # comment comment\n"
2915             "  # comment\n"
2916             "  key: value\n"
2917             "}",
2918             format("data {\n"
2919                    "# comment comment comment\n"
2920                    "key: value}",
2921                    getTextProtoStyleWithColumns(20)));
2922   EXPECT_EQ("data {\n"
2923             "  # comment comment\n"
2924             "  # comment\n"
2925             "  key: value\n"
2926             "}",
2927             format("data {# comment comment comment\n"
2928                    "key: value}",
2929                    getTextProtoStyleWithColumns(20)));
2930   // Reflows long comments.
2931   EXPECT_EQ("# comment comment\n"
2932             "# comment comment\n"
2933             "key: value",
2934             format("# comment comment comment\n"
2935                    "# comment\n"
2936                    "key:value",
2937                    getTextProtoStyleWithColumns(20)));
2938   // Breaks trailing comments.
2939   EXPECT_EQ("k: val  # comment\n"
2940             "        # comment\n"
2941             "a: 1",
2942             format("k:val#comment comment\n"
2943                    "a:1",
2944                    getTextProtoStyleWithColumns(20)));
2945   EXPECT_EQ("id {\n"
2946             "  k: val  # comment\n"
2947             "          # comment\n"
2948             "  # line line\n"
2949             "  a: 1\n"
2950             "}",
2951             format("id {k:val#comment comment\n"
2952                    "# line line\n"
2953                    "a:1}",
2954                    getTextProtoStyleWithColumns(20)));
2955   // Aligns trailing comments.
2956   EXPECT_EQ("k: val  # commen1\n"
2957             "        # commen2\n"
2958             "        # commen3\n"
2959             "# commen4\n"
2960             "a: 1  # commen5\n"
2961             "      # commen6\n"
2962             "      # commen7",
2963             format("k:val#commen1 commen2\n"
2964                    " # commen3\n"
2965                    "# commen4\n"
2966                    "a:1#commen5 commen6\n"
2967                    " #commen7",
2968                    getTextProtoStyleWithColumns(20)));
2969 }
2970 
2971 } // end namespace
2972 } // end namespace format
2973 } // end namespace clang
2974