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