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

source code of clang/unittests/Format/FormatTestComments.cpp