Debian Codegraph @

clang/unittests/Format/FormatTestComments.cpp

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