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