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