Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / unittests / Format / FormatTestComments.cpp
blob967ffa32db79c75f8088a1732b44971b9c59fbdb
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);
1074 TEST_F(FormatTestComments, SplitsLongLinesInComments) {
1075 // FIXME: Do we need to fix up the " */" at the end?
1076 // It doesn't look like any of our current logic triggers this.
1077 EXPECT_EQ("/* This is a long\n"
1078 " * comment that\n"
1079 " * doesn't fit on\n"
1080 " * one line. */",
1081 format("/* "
1082 "This is a long "
1083 "comment that "
1084 "doesn't "
1085 "fit on one line. */",
1086 getLLVMStyleWithColumns(20)));
1087 EXPECT_EQ(
1088 "/* a b c d\n"
1089 " * e f g\n"
1090 " * h i j k\n"
1091 " */",
1092 format("/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10)));
1093 EXPECT_EQ(
1094 "/* a b c d\n"
1095 " * e f g\n"
1096 " * h i j k\n"
1097 " */",
1098 format("\\\n/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10)));
1099 EXPECT_EQ("/*\n"
1100 "This is a long\n"
1101 "comment that doesn't\n"
1102 "fit on one line.\n"
1103 "*/",
1104 format("/*\n"
1105 "This is a long "
1106 "comment that doesn't "
1107 "fit on one line. \n"
1108 "*/",
1109 getLLVMStyleWithColumns(20)));
1110 EXPECT_EQ("/*\n"
1111 " * This is a long\n"
1112 " * comment that\n"
1113 " * doesn't fit on\n"
1114 " * one line.\n"
1115 " */",
1116 format("/* \n"
1117 " * This is a long "
1118 " comment that "
1119 " doesn't fit on "
1120 " one line. \n"
1121 " */",
1122 getLLVMStyleWithColumns(20)));
1123 EXPECT_EQ("/*\n"
1124 " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n"
1125 " * so_it_should_be_broken\n"
1126 " * wherever_a_space_occurs\n"
1127 " */",
1128 format("/*\n"
1129 " * This_is_a_comment_with_words_that_dont_fit_on_one_line "
1130 " so_it_should_be_broken "
1131 " wherever_a_space_occurs \n"
1132 " */",
1133 getLLVMStyleWithColumns(20)));
1134 EXPECT_EQ("/*\n"
1135 " * This_comment_can_not_be_broken_into_lines\n"
1136 " */",
1137 format("/*\n"
1138 " * This_comment_can_not_be_broken_into_lines\n"
1139 " */",
1140 getLLVMStyleWithColumns(20)));
1141 EXPECT_EQ("{\n"
1142 " /*\n"
1143 " This is another\n"
1144 " long comment that\n"
1145 " doesn't fit on one\n"
1146 " line 1234567890\n"
1147 " */\n"
1148 "}",
1149 format("{\n"
1150 "/*\n"
1151 "This is another "
1152 " long comment that "
1153 " doesn't fit on one"
1154 " line 1234567890\n"
1155 "*/\n"
1156 "}",
1157 getLLVMStyleWithColumns(20)));
1158 EXPECT_EQ("{\n"
1159 " /*\n"
1160 " * This i s\n"
1161 " * another comment\n"
1162 " * t hat doesn' t\n"
1163 " * fit on one l i\n"
1164 " * n e\n"
1165 " */\n"
1166 "}",
1167 format("{\n"
1168 "/*\n"
1169 " * This i s"
1170 " another comment"
1171 " t hat doesn' t"
1172 " fit on one l i"
1173 " n e\n"
1174 " */\n"
1175 "}",
1176 getLLVMStyleWithColumns(20)));
1177 EXPECT_EQ("/*\n"
1178 " * This is a long\n"
1179 " * comment that\n"
1180 " * doesn't fit on\n"
1181 " * one line\n"
1182 " */",
1183 format(" /*\n"
1184 " * This is a long comment that doesn't fit on one line\n"
1185 " */",
1186 getLLVMStyleWithColumns(20)));
1187 EXPECT_EQ("{\n"
1188 " if (something) /* This is a\n"
1189 " long\n"
1190 " comment */\n"
1191 " ;\n"
1192 "}",
1193 format("{\n"
1194 " if (something) /* This is a long comment */\n"
1195 " ;\n"
1196 "}",
1197 getLLVMStyleWithColumns(30)));
1199 EXPECT_EQ("/* A comment before\n"
1200 " * a macro\n"
1201 " * definition */\n"
1202 "#define a b",
1203 format("/* A comment before a macro definition */\n"
1204 "#define a b",
1205 getLLVMStyleWithColumns(20)));
1207 EXPECT_EQ("/* some comment\n"
1208 " * a comment that\n"
1209 " * we break another\n"
1210 " * comment we have\n"
1211 " * to break a left\n"
1212 " * comment\n"
1213 " */",
1214 format(" /* some comment\n"
1215 " * a comment that we break\n"
1216 " * another comment we have to break\n"
1217 "* a left comment\n"
1218 " */",
1219 getLLVMStyleWithColumns(20)));
1221 EXPECT_EQ("/**\n"
1222 " * multiline block\n"
1223 " * comment\n"
1224 " *\n"
1225 " */",
1226 format("/**\n"
1227 " * multiline block comment\n"
1228 " *\n"
1229 " */",
1230 getLLVMStyleWithColumns(20)));
1232 // This reproduces a crashing bug where both adaptStartOfLine and
1233 // getCommentSplit were trying to wrap after the "/**".
1234 verifyFormat("/** multilineblockcommentwithnowrapopportunity */",
1235 getLLVMStyleWithColumns(20));
1237 EXPECT_EQ("/*\n"
1238 "\n"
1239 "\n"
1240 " */",
1241 format(" /* \n"
1242 " \n"
1243 " \n"
1244 " */"));
1246 EXPECT_EQ("/* a a */",
1247 format("/* a a */", getLLVMStyleWithColumns(15)));
1248 EXPECT_EQ("/* a a bc */",
1249 format("/* a a bc */", getLLVMStyleWithColumns(15)));
1250 EXPECT_EQ("/* aaa aaa\n"
1251 " * aaaaa */",
1252 format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15)));
1253 EXPECT_EQ("/* aaa aaa\n"
1254 " * aaaaa */",
1255 format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15)));
1258 TEST_F(FormatTestComments, SplitsLongLinesInCommentsInPreprocessor) {
1259 EXPECT_EQ("#define X \\\n"
1260 " /* \\\n"
1261 " Test \\\n"
1262 " Macro comment \\\n"
1263 " with a long \\\n"
1264 " line \\\n"
1265 " */ \\\n"
1266 " A + B",
1267 format("#define X \\\n"
1268 " /*\n"
1269 " Test\n"
1270 " Macro comment with a long line\n"
1271 " */ \\\n"
1272 " A + B",
1273 getLLVMStyleWithColumns(20)));
1274 EXPECT_EQ("#define X \\\n"
1275 " /* Macro comment \\\n"
1276 " with a long \\\n"
1277 " line */ \\\n"
1278 " A + B",
1279 format("#define X \\\n"
1280 " /* Macro comment with a long\n"
1281 " line */ \\\n"
1282 " A + B",
1283 getLLVMStyleWithColumns(20)));
1284 EXPECT_EQ("#define X \\\n"
1285 " /* Macro comment \\\n"
1286 " * with a long \\\n"
1287 " * line */ \\\n"
1288 " A + B",
1289 format("#define X \\\n"
1290 " /* Macro comment with a long line */ \\\n"
1291 " A + B",
1292 getLLVMStyleWithColumns(20)));
1295 TEST_F(FormatTestComments, KeepsTrailingPPCommentsAndSectionCommentsSeparate) {
1296 verifyFormat("#ifdef A // line about A\n"
1297 "// section comment\n"
1298 "#endif",
1299 getLLVMStyleWithColumns(80));
1300 verifyFormat("#ifdef A // line 1 about A\n"
1301 " // line 2 about A\n"
1302 "// section comment\n"
1303 "#endif",
1304 getLLVMStyleWithColumns(80));
1305 EXPECT_EQ("#ifdef A // line 1 about A\n"
1306 " // line 2 about A\n"
1307 "// section comment\n"
1308 "#endif",
1309 format("#ifdef A // line 1 about A\n"
1310 " // line 2 about A\n"
1311 "// section comment\n"
1312 "#endif",
1313 getLLVMStyleWithColumns(80)));
1314 verifyFormat("int f() {\n"
1315 " int i;\n"
1316 "#ifdef A // comment about A\n"
1317 " // section comment 1\n"
1318 " // section comment 2\n"
1319 " i = 2;\n"
1320 "#else // comment about #else\n"
1321 " // section comment 3\n"
1322 " i = 4;\n"
1323 "#endif\n"
1324 "}",
1325 getLLVMStyleWithColumns(80));
1328 TEST_F(FormatTestComments, AlignsPPElseEndifComments) {
1329 verifyFormat("#if A\n"
1330 "#else // A\n"
1331 "int iiii;\n"
1332 "#endif // B",
1333 getLLVMStyleWithColumns(20));
1334 verifyFormat("#if A\n"
1335 "#else // A\n"
1336 "int iiii; // CC\n"
1337 "#endif // B",
1338 getLLVMStyleWithColumns(20));
1339 EXPECT_EQ("#if A\n"
1340 "#else // A1\n"
1341 " // A2\n"
1342 "int ii;\n"
1343 "#endif // B",
1344 format("#if A\n"
1345 "#else // A1\n"
1346 " // A2\n"
1347 "int ii;\n"
1348 "#endif // B",
1349 getLLVMStyleWithColumns(20)));
1352 TEST_F(FormatTestComments, CommentsInStaticInitializers) {
1353 EXPECT_EQ(
1354 "static SomeType type = {aaaaaaaaaaaaaaaaaaaa, /* comment */\n"
1355 " aaaaaaaaaaaaaaaaaaaa /* comment */,\n"
1356 " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n"
1357 " aaaaaaaaaaaaaaaaaaaa, // comment\n"
1358 " aaaaaaaaaaaaaaaaaaaa};",
1359 format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n"
1360 " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n"
1361 " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n"
1362 " aaaaaaaaaaaaaaaaaaaa , // comment\n"
1363 " aaaaaaaaaaaaaaaaaaaa };"));
1364 verifyFormat("static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
1365 " bbbbbbbbbbb, ccccccccccc};");
1366 verifyFormat("static SomeType type = {aaaaaaaaaaa,\n"
1367 " // comment for bb....\n"
1368 " bbbbbbbbbbb, ccccccccccc};");
1369 verifyGoogleFormat(
1370 "static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
1371 " bbbbbbbbbbb, ccccccccccc};");
1372 verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n"
1373 " // comment for bb....\n"
1374 " bbbbbbbbbbb, ccccccccccc};");
1376 verifyFormat("S s = {{a, b, c}, // Group #1\n"
1377 " {d, e, f}, // Group #2\n"
1378 " {g, h, i}}; // Group #3");
1379 verifyFormat("S s = {{// Group #1\n"
1380 " a, b, c},\n"
1381 " {// Group #2\n"
1382 " d, e, f},\n"
1383 " {// Group #3\n"
1384 " g, h, i}};");
1386 EXPECT_EQ("S s = {\n"
1387 " // Some comment\n"
1388 " a,\n"
1389 "\n"
1390 " // Comment after empty line\n"
1391 " b}",
1392 format("S s = {\n"
1393 " // Some comment\n"
1394 " a,\n"
1395 " \n"
1396 " // Comment after empty line\n"
1397 " b\n"
1398 "}"));
1399 EXPECT_EQ("S s = {\n"
1400 " /* Some comment */\n"
1401 " a,\n"
1402 "\n"
1403 " /* Comment after empty line */\n"
1404 " b}",
1405 format("S s = {\n"
1406 " /* Some comment */\n"
1407 " a,\n"
1408 " \n"
1409 " /* Comment after empty line */\n"
1410 " b\n"
1411 "}"));
1412 verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n"
1413 " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
1414 " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
1415 " 0x00, 0x00, 0x00, 0x00}; // comment");
1418 TEST_F(FormatTestComments, LineCommentsAfterRightBrace) {
1419 EXPECT_EQ("if (true) { // comment about branch\n"
1420 " // comment about f\n"
1421 " f();\n"
1422 "}",
1423 format("if (true) { // comment about branch\n"
1424 " // comment about f\n"
1425 " f();\n"
1426 "}",
1427 getLLVMStyleWithColumns(80)));
1428 EXPECT_EQ("if (1) { // if line 1\n"
1429 " // if line 2\n"
1430 " // if line 3\n"
1431 " // f line 1\n"
1432 " // f line 2\n"
1433 " f();\n"
1434 "} else { // else line 1\n"
1435 " // else line 2\n"
1436 " // else line 3\n"
1437 " // g line 1\n"
1438 " g();\n"
1439 "}",
1440 format("if (1) { // if line 1\n"
1441 " // if line 2\n"
1442 " // if line 3\n"
1443 " // f line 1\n"
1444 " // f line 2\n"
1445 " f();\n"
1446 "} else { // else line 1\n"
1447 " // else line 2\n"
1448 " // else line 3\n"
1449 " // g line 1\n"
1450 " g();\n"
1451 "}"));
1452 EXPECT_EQ("do { // line 1\n"
1453 " // line 2\n"
1454 " // line 3\n"
1455 " f();\n"
1456 "} while (true);",
1457 format("do { // line 1\n"
1458 " // line 2\n"
1459 " // line 3\n"
1460 " f();\n"
1461 "} while (true);",
1462 getLLVMStyleWithColumns(80)));
1463 EXPECT_EQ("while (a < b) { // line 1\n"
1464 " // line 2\n"
1465 " // line 3\n"
1466 " f();\n"
1467 "}",
1468 format("while (a < b) {// line 1\n"
1469 " // line 2\n"
1470 " // line 3\n"
1471 " f();\n"
1472 "}",
1473 getLLVMStyleWithColumns(80)));
1476 TEST_F(FormatTestComments, ReflowsComments) {
1477 // Break a long line and reflow with the full next line.
1478 EXPECT_EQ("// long long long\n"
1479 "// long long",
1480 format("// long long long long\n"
1481 "// long",
1482 getLLVMStyleWithColumns(20)));
1484 // Keep the trailing newline while reflowing.
1485 EXPECT_EQ("// long long long\n"
1486 "// long long",
1487 format("// long long long long\n"
1488 "// long",
1489 getLLVMStyleWithColumns(20)));
1491 // Break a long line and reflow with a part of the next line.
1492 EXPECT_EQ("// long long long\n"
1493 "// long long\n"
1494 "// long_long",
1495 format("// long long long long\n"
1496 "// long long_long",
1497 getLLVMStyleWithColumns(20)));
1499 // Break but do not reflow if the first word from the next line is too long.
1500 EXPECT_EQ("// long long long\n"
1501 "// long\n"
1502 "// long_long_long",
1503 format("// long long long long\n"
1504 "// long_long_long",
1505 getLLVMStyleWithColumns(20)));
1507 // Don't break or reflow short lines.
1508 verifyFormat("// long\n"
1509 "// long long long lo\n"
1510 "// long long long lo\n"
1511 "// long",
1512 getLLVMStyleWithColumns(20));
1514 // Keep prefixes and decorations while reflowing.
1515 EXPECT_EQ("/// long long long\n"
1516 "/// long long",
1517 format("/// long long long long\n"
1518 "/// long",
1519 getLLVMStyleWithColumns(20)));
1520 EXPECT_EQ("//! long long long\n"
1521 "//! long long",
1522 format("//! long long long long\n"
1523 "//! long",
1524 getLLVMStyleWithColumns(20)));
1525 EXPECT_EQ("/* long long long\n"
1526 " * long long */",
1527 format("/* long long long long\n"
1528 " * long */",
1529 getLLVMStyleWithColumns(20)));
1530 EXPECT_EQ("///< long long long\n"
1531 "///< long long",
1532 format("///< long long long long\n"
1533 "///< long",
1534 getLLVMStyleWithColumns(20)));
1535 EXPECT_EQ("//!< long long long\n"
1536 "//!< long long",
1537 format("//!< long long long long\n"
1538 "//!< long",
1539 getLLVMStyleWithColumns(20)));
1541 // Don't bring leading whitespace up while reflowing.
1542 EXPECT_EQ("/* long long long\n"
1543 " * long long long\n"
1544 " */",
1545 format("/* long long long long\n"
1546 " * long long\n"
1547 " */",
1548 getLLVMStyleWithColumns(20)));
1550 // Reflow the last line of a block comment with its trailing '*/'.
1551 EXPECT_EQ("/* long long long\n"
1552 " long long */",
1553 format("/* long long long long\n"
1554 " long */",
1555 getLLVMStyleWithColumns(20)));
1557 // Reflow two short lines; keep the postfix of the last one.
1558 EXPECT_EQ("/* long long long\n"
1559 " * long long long */",
1560 format("/* long long long long\n"
1561 " * long\n"
1562 " * long */",
1563 getLLVMStyleWithColumns(20)));
1565 // Put the postfix of the last short reflow line on a newline if it doesn't
1566 // fit.
1567 EXPECT_EQ("/* long long long\n"
1568 " * long long longg\n"
1569 " */",
1570 format("/* long long long long\n"
1571 " * long\n"
1572 " * longg */",
1573 getLLVMStyleWithColumns(20)));
1575 // Reflow lines with leading whitespace.
1576 EXPECT_EQ("{\n"
1577 " /*\n"
1578 " * long long long\n"
1579 " * long long long\n"
1580 " * long long long\n"
1581 " */\n"
1582 "}",
1583 format("{\n"
1584 "/*\n"
1585 " * long long long long\n"
1586 " * long\n"
1587 " * long long long long\n"
1588 " */\n"
1589 "}",
1590 getLLVMStyleWithColumns(20)));
1592 // Break single line block comments that are first in the line with ' *'
1593 // decoration.
1594 EXPECT_EQ("/* long long long\n"
1595 " * long */",
1596 format("/* long long long long */", getLLVMStyleWithColumns(20)));
1598 // Break single line block comment that are not first in the line with ' '
1599 // decoration.
1600 EXPECT_EQ("int i; /* long long\n"
1601 " long */",
1602 format("int i; /* long long long */", getLLVMStyleWithColumns(20)));
1604 // Reflow a line that goes just over the column limit.
1605 EXPECT_EQ("// long long long\n"
1606 "// lon long",
1607 format("// long long long lon\n"
1608 "// long",
1609 getLLVMStyleWithColumns(20)));
1611 // Stop reflowing if the next line has a different indentation than the
1612 // previous line.
1613 EXPECT_EQ("// long long long\n"
1614 "// long\n"
1615 "// long long\n"
1616 "// long",
1617 format("// long long long long\n"
1618 "// long long\n"
1619 "// long",
1620 getLLVMStyleWithColumns(20)));
1622 // Reflow into the last part of a really long line that has been broken into
1623 // multiple lines.
1624 EXPECT_EQ("// long long long\n"
1625 "// long long long\n"
1626 "// long long long",
1627 format("// long long long long long long long long\n"
1628 "// long",
1629 getLLVMStyleWithColumns(20)));
1631 // Break the first line, then reflow the beginning of the second and third
1632 // line up.
1633 EXPECT_EQ("// long long long\n"
1634 "// lon1 lon2 lon2\n"
1635 "// lon2 lon3 lon3",
1636 format("// long long long lon1\n"
1637 "// lon2 lon2 lon2\n"
1638 "// lon3 lon3",
1639 getLLVMStyleWithColumns(20)));
1641 // Reflow the beginning of the second line, then break the rest.
1642 EXPECT_EQ("// long long long\n"
1643 "// lon1 lon2 lon2\n"
1644 "// lon2 lon2 lon2\n"
1645 "// lon3",
1646 format("// long long long lon1\n"
1647 "// lon2 lon2 lon2 lon2 lon2 lon3",
1648 getLLVMStyleWithColumns(20)));
1650 // Shrink the first line, then reflow the second line up.
1651 EXPECT_EQ("// long long long", format("// long long\n"
1652 "// long",
1653 getLLVMStyleWithColumns(20)));
1655 // Don't shrink leading whitespace.
1656 verifyNoChange("int i; /// a", getLLVMStyleWithColumns(20));
1658 // Shrink trailing whitespace if there is no postfix and reflow.
1659 EXPECT_EQ("// long long long\n"
1660 "// long long",
1661 format("// long long long long \n"
1662 "// long",
1663 getLLVMStyleWithColumns(20)));
1665 // Shrink trailing whitespace to a single one if there is postfix.
1666 EXPECT_EQ("/* long long long */",
1667 format("/* long long long */", getLLVMStyleWithColumns(20)));
1669 // Break a block comment postfix if exceeding the line limit.
1670 EXPECT_EQ("/* long\n"
1671 " */",
1672 format("/* long */", getLLVMStyleWithColumns(20)));
1674 // Reflow indented comments.
1675 EXPECT_EQ("{\n"
1676 " // long long long\n"
1677 " // long long\n"
1678 " int i; /* long lon\n"
1679 " g long\n"
1680 " */\n"
1681 "}",
1682 format("{\n"
1683 " // long long long long\n"
1684 " // long\n"
1685 " int i; /* long lon g\n"
1686 " long */\n"
1687 "}",
1688 getLLVMStyleWithColumns(20)));
1690 // Don't realign trailing comments after reflow has happened.
1691 EXPECT_EQ("// long long long\n"
1692 "// long long\n"
1693 "long i; // long",
1694 format("// long long long long\n"
1695 "// long\n"
1696 "long i; // long",
1697 getLLVMStyleWithColumns(20)));
1698 EXPECT_EQ("// long long long\n"
1699 "// longng long long\n"
1700 "// long lo",
1701 format("// long long long longng\n"
1702 "// long long long\n"
1703 "// lo",
1704 getLLVMStyleWithColumns(20)));
1706 // Reflow lines after a broken line.
1707 EXPECT_EQ("int a; // Trailing\n"
1708 " // comment on\n"
1709 " // 2 or 3\n"
1710 " // lines.",
1711 format("int a; // Trailing comment\n"
1712 " // on 2\n"
1713 " // or 3\n"
1714 " // lines.",
1715 getLLVMStyleWithColumns(20)));
1716 EXPECT_EQ("/// This long line\n"
1717 "/// gets reflown.",
1718 format("/// This long line gets\n"
1719 "/// reflown.",
1720 getLLVMStyleWithColumns(20)));
1721 EXPECT_EQ("//! This long line\n"
1722 "//! gets reflown.",
1723 format(" //! This long line gets\n"
1724 " //! reflown.",
1725 getLLVMStyleWithColumns(20)));
1726 EXPECT_EQ("/* This long line\n"
1727 " * gets reflown.\n"
1728 " */",
1729 format("/* This long line gets\n"
1730 " * reflown.\n"
1731 " */",
1732 getLLVMStyleWithColumns(20)));
1734 // Reflow after indentation makes a line too long.
1735 EXPECT_EQ("{\n"
1736 " // long long long\n"
1737 " // lo long\n"
1738 "}",
1739 format("{\n"
1740 "// long long long lo\n"
1741 "// long\n"
1742 "}",
1743 getLLVMStyleWithColumns(20)));
1745 // Break and reflow multiple lines.
1746 EXPECT_EQ("/*\n"
1747 " * Reflow the end of\n"
1748 " * line by 11 22 33\n"
1749 " * 4.\n"
1750 " */",
1751 format("/*\n"
1752 " * Reflow the end of line\n"
1753 " * by\n"
1754 " * 11\n"
1755 " * 22\n"
1756 " * 33\n"
1757 " * 4.\n"
1758 " */",
1759 getLLVMStyleWithColumns(20)));
1760 EXPECT_EQ("/// First line gets\n"
1761 "/// broken. Second\n"
1762 "/// line gets\n"
1763 "/// reflown and\n"
1764 "/// broken. Third\n"
1765 "/// gets reflown.",
1766 format("/// First line gets broken.\n"
1767 "/// Second line gets reflown and broken.\n"
1768 "/// Third gets reflown.",
1769 getLLVMStyleWithColumns(20)));
1770 EXPECT_EQ("int i; // first long\n"
1771 " // long snd\n"
1772 " // long.",
1773 format("int i; // first long long\n"
1774 " // snd long.",
1775 getLLVMStyleWithColumns(20)));
1776 EXPECT_EQ("{\n"
1777 " // first long line\n"
1778 " // line second\n"
1779 " // long line line\n"
1780 " // third long line\n"
1781 " // line\n"
1782 "}",
1783 format("{\n"
1784 " // first long line line\n"
1785 " // second long line line\n"
1786 " // third long line line\n"
1787 "}",
1788 getLLVMStyleWithColumns(20)));
1789 EXPECT_EQ("int i; /* first line\n"
1790 " * second\n"
1791 " * line third\n"
1792 " * line\n"
1793 " */",
1794 format("int i; /* first line\n"
1795 " * second line\n"
1796 " * third line\n"
1797 " */",
1798 getLLVMStyleWithColumns(20)));
1800 // Reflow the last two lines of a section that starts with a line having
1801 // different indentation.
1802 EXPECT_EQ("// long\n"
1803 "// long long long\n"
1804 "// long long",
1805 format("// long\n"
1806 "// long long long long\n"
1807 "// long",
1808 getLLVMStyleWithColumns(20)));
1810 // Keep the block comment endling '*/' while reflowing.
1811 EXPECT_EQ("/* Long long long\n"
1812 " * line short */",
1813 format("/* Long long long line\n"
1814 " * short */",
1815 getLLVMStyleWithColumns(20)));
1817 // Don't reflow between separate blocks of comments.
1818 EXPECT_EQ("/* First comment\n"
1819 " * block will */\n"
1820 "/* Snd\n"
1821 " */",
1822 format("/* First comment block\n"
1823 " * will */\n"
1824 "/* Snd\n"
1825 " */",
1826 getLLVMStyleWithColumns(20)));
1828 // Don't reflow across blank comment lines.
1829 EXPECT_EQ("int i; // This long\n"
1830 " // line gets\n"
1831 " // broken.\n"
1832 " //\n"
1833 " // keep.",
1834 format("int i; // This long line gets broken.\n"
1835 " // \n"
1836 " // keep.",
1837 getLLVMStyleWithColumns(20)));
1838 EXPECT_EQ("{\n"
1839 " /// long long long\n"
1840 " /// long long\n"
1841 " ///\n"
1842 " /// long\n"
1843 "}",
1844 format("{\n"
1845 " /// long long long long\n"
1846 " /// long\n"
1847 " ///\n"
1848 " /// long\n"
1849 "}",
1850 getLLVMStyleWithColumns(20)));
1851 EXPECT_EQ("//! long long long\n"
1852 "//! long\n"
1853 "\n"
1854 "//! long",
1855 format("//! long long long long\n"
1856 "\n"
1857 "//! long",
1858 getLLVMStyleWithColumns(20)));
1859 EXPECT_EQ("/* long long long\n"
1860 " long\n"
1861 "\n"
1862 " long */",
1863 format("/* long long long long\n"
1864 "\n"
1865 " long */",
1866 getLLVMStyleWithColumns(20)));
1867 EXPECT_EQ("/* long long long\n"
1868 " * long\n"
1869 " *\n"
1870 " * long */",
1871 format("/* long long long long\n"
1872 " *\n"
1873 " * long */",
1874 getLLVMStyleWithColumns(20)));
1876 // Don't reflow lines having content that is a single character.
1877 EXPECT_EQ("// long long long\n"
1878 "// long\n"
1879 "// l",
1880 format("// long long long long\n"
1881 "// l",
1882 getLLVMStyleWithColumns(20)));
1884 // Don't reflow lines starting with two punctuation characters.
1885 EXPECT_EQ("// long long long\n"
1886 "// long\n"
1887 "// ... --- ...",
1888 format("// long long long long\n"
1889 "// ... --- ...",
1890 getLLVMStyleWithColumns(20)));
1892 // Don't reflow lines starting with '@'.
1893 EXPECT_EQ("// long long long\n"
1894 "// long\n"
1895 "// @param arg",
1896 format("// long long long long\n"
1897 "// @param arg",
1898 getLLVMStyleWithColumns(20)));
1900 // Don't reflow lines starting with 'TODO'.
1901 EXPECT_EQ("// long long long\n"
1902 "// long\n"
1903 "// TODO: long",
1904 format("// long long long long\n"
1905 "// TODO: long",
1906 getLLVMStyleWithColumns(20)));
1908 // Don't reflow lines starting with 'FIXME'.
1909 EXPECT_EQ("// long long long\n"
1910 "// long\n"
1911 "// FIXME: long",
1912 format("// long long long long\n"
1913 "// FIXME: long",
1914 getLLVMStyleWithColumns(20)));
1916 // Don't reflow lines starting with 'XXX'.
1917 EXPECT_EQ("// long long long\n"
1918 "// long\n"
1919 "// XXX: long",
1920 format("// long long long long\n"
1921 "// XXX: long",
1922 getLLVMStyleWithColumns(20)));
1924 // Don't reflow comment pragmas.
1925 EXPECT_EQ("// long long long\n"
1926 "// long\n"
1927 "// IWYU pragma:",
1928 format("// long long long long\n"
1929 "// IWYU pragma:",
1930 getLLVMStyleWithColumns(20)));
1931 EXPECT_EQ("/* long long long\n"
1932 " * long\n"
1933 " * IWYU pragma:\n"
1934 " */",
1935 format("/* long long long long\n"
1936 " * IWYU pragma:\n"
1937 " */",
1938 getLLVMStyleWithColumns(20)));
1940 // Reflow lines that have a non-punctuation character among their first 2
1941 // characters.
1942 EXPECT_EQ("// long long long\n"
1943 "// long 'long'",
1944 format("// long long long long\n"
1945 "// 'long'",
1946 getLLVMStyleWithColumns(20)));
1948 // Don't reflow between separate blocks of comments.
1949 EXPECT_EQ("/* First comment\n"
1950 " * block will */\n"
1951 "/* Snd\n"
1952 " */",
1953 format("/* First comment block\n"
1954 " * will */\n"
1955 "/* Snd\n"
1956 " */",
1957 getLLVMStyleWithColumns(20)));
1959 // Don't reflow lines having different indentation.
1960 EXPECT_EQ("// long long long\n"
1961 "// long\n"
1962 "// long",
1963 format("// long long long long\n"
1964 "// long",
1965 getLLVMStyleWithColumns(20)));
1967 // Don't reflow separate bullets in list
1968 EXPECT_EQ("// - long long long\n"
1969 "// long\n"
1970 "// - long",
1971 format("// - long long long long\n"
1972 "// - long",
1973 getLLVMStyleWithColumns(20)));
1974 EXPECT_EQ("// * long long long\n"
1975 "// long\n"
1976 "// * long",
1977 format("// * long long long long\n"
1978 "// * long",
1979 getLLVMStyleWithColumns(20)));
1980 EXPECT_EQ("// + long long long\n"
1981 "// long\n"
1982 "// + long",
1983 format("// + long long long long\n"
1984 "// + long",
1985 getLLVMStyleWithColumns(20)));
1986 EXPECT_EQ("// 1. long long long\n"
1987 "// long\n"
1988 "// 2. long",
1989 format("// 1. long long long long\n"
1990 "// 2. long",
1991 getLLVMStyleWithColumns(20)));
1992 EXPECT_EQ("// -# long long long\n"
1993 "// long\n"
1994 "// -# long",
1995 format("// -# long long long long\n"
1996 "// -# long",
1997 getLLVMStyleWithColumns(20)));
1999 EXPECT_EQ("// - long long long\n"
2000 "// long long long\n"
2001 "// - long",
2002 format("// - long long long long\n"
2003 "// long long\n"
2004 "// - long",
2005 getLLVMStyleWithColumns(20)));
2006 EXPECT_EQ("// - long long long\n"
2007 "// long long long\n"
2008 "// long\n"
2009 "// - long",
2010 format("// - long long long long\n"
2011 "// long long long\n"
2012 "// - long",
2013 getLLVMStyleWithColumns(20)));
2015 // Large number (>2 digits) are not list items
2016 EXPECT_EQ("// long long long\n"
2017 "// long 1024. long.",
2018 format("// long long long long\n"
2019 "// 1024. long.",
2020 getLLVMStyleWithColumns(20)));
2022 // Do not break before number, to avoid introducing a non-reflowable doxygen
2023 // list item.
2024 EXPECT_EQ("// long long\n"
2025 "// long 10. long.",
2026 format("// long long long 10.\n"
2027 "// long.",
2028 getLLVMStyleWithColumns(20)));
2030 // Don't break or reflow after implicit string literals.
2031 verifyFormat("#include <t> // l l l\n"
2032 " // l",
2033 getLLVMStyleWithColumns(20));
2035 // Don't break or reflow comments on import lines.
2036 EXPECT_EQ("#include \"t\" /* l l l\n"
2037 " * l */",
2038 format("#include \"t\" /* l l l\n"
2039 " * l */",
2040 getLLVMStyleWithColumns(20)));
2042 // Don't reflow between different trailing comment sections.
2043 EXPECT_EQ("int i; // long long\n"
2044 " // long\n"
2045 "int j; // long long\n"
2046 " // long",
2047 format("int i; // long long long\n"
2048 "int j; // long long long",
2049 getLLVMStyleWithColumns(20)));
2051 // Don't reflow if the first word on the next line is longer than the
2052 // available space at current line.
2053 EXPECT_EQ("int i; // trigger\n"
2054 " // reflow\n"
2055 " // longsec",
2056 format("int i; // trigger reflow\n"
2057 " // longsec",
2058 getLLVMStyleWithColumns(20)));
2060 // Simple case that correctly handles reflow in parameter lists.
2061 EXPECT_EQ("a = f(/* looooooooong\n"
2062 " * long long\n"
2063 " */\n"
2064 " a);",
2065 format("a = f(/* looooooooong long\n* long\n*/ a);",
2066 getLLVMStyleWithColumns(22)));
2067 // Tricky case that has fewer lines if we reflow the comment, ending up with
2068 // fewer lines.
2069 EXPECT_EQ("a = f(/* loooooong\n"
2070 " * long long\n"
2071 " */\n"
2072 " a);",
2073 format("a = f(/* loooooong long\n* long\n*/ a);",
2074 getLLVMStyleWithColumns(22)));
2076 // Keep empty comment lines.
2077 EXPECT_EQ("/**/", format(" /**/", getLLVMStyleWithColumns(20)));
2078 EXPECT_EQ("/* */", format(" /* */", getLLVMStyleWithColumns(20)));
2079 EXPECT_EQ("/* */", format(" /* */", getLLVMStyleWithColumns(20)));
2080 EXPECT_EQ("//", format(" // ", getLLVMStyleWithColumns(20)));
2081 EXPECT_EQ("///", format(" /// ", getLLVMStyleWithColumns(20)));
2084 TEST_F(FormatTestComments, ReflowsCommentsPrecise) {
2085 // FIXME: This assumes we do not continue compressing whitespace once we are
2086 // in reflow mode. Consider compressing whitespace.
2088 // Test that we stop reflowing precisely at the column limit.
2089 // After reflowing, "// reflows into foo" does not fit the column limit,
2090 // so we compress the whitespace.
2091 EXPECT_EQ("// some text that\n"
2092 "// reflows into foo",
2093 format("// some text that reflows\n"
2094 "// into foo",
2095 getLLVMStyleWithColumns(20)));
2096 // Given one more column, "// reflows into foo" does fit the limit, so we
2097 // do not compress the whitespace.
2098 EXPECT_EQ("// some text that\n"
2099 "// reflows into foo",
2100 format("// some text that reflows\n"
2101 "// into foo",
2102 getLLVMStyleWithColumns(21)));
2104 // Make sure that we correctly account for the space added in the reflow case
2105 // when making the reflowing decision.
2106 // First, when the next line ends precisely one column over the limit, do not
2107 // reflow.
2108 EXPECT_EQ("// some text that\n"
2109 "// reflows\n"
2110 "// into1234567",
2111 format("// some text that reflows\n"
2112 "// into1234567",
2113 getLLVMStyleWithColumns(21)));
2114 // Secondly, when the next line ends later, but the first word in that line
2115 // is precisely one column over the limit, do not reflow.
2116 EXPECT_EQ("// some text that\n"
2117 "// reflows\n"
2118 "// into1234567 f",
2119 format("// some text that reflows\n"
2120 "// into1234567 f",
2121 getLLVMStyleWithColumns(21)));
2124 TEST_F(FormatTestComments, ReflowsCommentsWithExtraWhitespace) {
2125 // Baseline.
2126 EXPECT_EQ("// some text\n"
2127 "// that re flows",
2128 format("// some text that\n"
2129 "// re flows",
2130 getLLVMStyleWithColumns(16)));
2131 EXPECT_EQ("// some text\n"
2132 "// that re flows",
2133 format("// some text that\n"
2134 "// re flows",
2135 getLLVMStyleWithColumns(16)));
2136 EXPECT_EQ("/* some text\n"
2137 " * that re flows\n"
2138 " */",
2139 format("/* some text that\n"
2140 "* re flows\n"
2141 "*/",
2142 getLLVMStyleWithColumns(16)));
2143 // FIXME: We do not reflow if the indent of two subsequent lines differs;
2144 // given that this is different behavior from block comments, do we want
2145 // to keep this?
2146 EXPECT_EQ("// some text\n"
2147 "// that\n"
2148 "// re flows",
2149 format("// some text that\n"
2150 "// re flows",
2151 getLLVMStyleWithColumns(16)));
2152 // Space within parts of a line that fit.
2153 // FIXME: Use the earliest possible split while reflowing to compress the
2154 // whitespace within the line.
2155 EXPECT_EQ("// some text that\n"
2156 "// does re flow\n"
2157 "// more here",
2158 format("// some text that does\n"
2159 "// re flow more here",
2160 getLLVMStyleWithColumns(21)));
2163 TEST_F(FormatTestComments, IgnoresIf0Contents) {
2164 EXPECT_EQ("#if 0\n"
2165 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
2166 "#endif\n"
2167 "void f() {}",
2168 format("#if 0\n"
2169 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
2170 "#endif\n"
2171 "void f( ) { }"));
2172 EXPECT_EQ("#if false\n"
2173 "void f( ) { }\n"
2174 "#endif\n"
2175 "void g() {}",
2176 format("#if false\n"
2177 "void f( ) { }\n"
2178 "#endif\n"
2179 "void g( ) { }"));
2180 EXPECT_EQ("enum E {\n"
2181 " One,\n"
2182 " Two,\n"
2183 "#if 0\n"
2184 "Three,\n"
2185 " Four,\n"
2186 "#endif\n"
2187 " Five\n"
2188 "};",
2189 format("enum E {\n"
2190 " One,Two,\n"
2191 "#if 0\n"
2192 "Three,\n"
2193 " Four,\n"
2194 "#endif\n"
2195 " Five};"));
2196 EXPECT_EQ("enum F {\n"
2197 " One,\n"
2198 "#if 1\n"
2199 " Two,\n"
2200 "#if 0\n"
2201 "Three,\n"
2202 " Four,\n"
2203 "#endif\n"
2204 " Five\n"
2205 "#endif\n"
2206 "};",
2207 format("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 EXPECT_EQ("enum G {\n"
2219 " One,\n"
2220 "#if 0\n"
2221 "Two,\n"
2222 "#else\n"
2223 " Three,\n"
2224 "#endif\n"
2225 " Four\n"
2226 "};",
2227 format("enum G {\n"
2228 "One,\n"
2229 "#if 0\n"
2230 "Two,\n"
2231 "#else\n"
2232 "Three,\n"
2233 "#endif\n"
2234 "Four\n"
2235 "};"));
2236 EXPECT_EQ("enum H {\n"
2237 " One,\n"
2238 "#if 0\n"
2239 "#ifdef Q\n"
2240 "Two,\n"
2241 "#else\n"
2242 "Three,\n"
2243 "#endif\n"
2244 "#endif\n"
2245 " Four\n"
2246 "};",
2247 format("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 EXPECT_EQ("enum I {\n"
2259 " One,\n"
2260 "#if /* test */ 0 || 1\n"
2261 "Two,\n"
2262 "Three,\n"
2263 "#endif\n"
2264 " Four\n"
2265 "};",
2266 format("enum I {\n"
2267 "One,\n"
2268 "#if /* test */ 0 || 1\n"
2269 "Two,\n"
2270 "Three,\n"
2271 "#endif\n"
2272 "Four\n"
2273 "};"));
2274 EXPECT_EQ("enum J {\n"
2275 " One,\n"
2276 "#if 0\n"
2277 "#if 0\n"
2278 "Two,\n"
2279 "#else\n"
2280 "Three,\n"
2281 "#endif\n"
2282 "Four,\n"
2283 "#endif\n"
2284 " Five\n"
2285 "};",
2286 format("enum J {\n"
2287 "One,\n"
2288 "#if 0\n"
2289 "#if 0\n"
2290 "Two,\n"
2291 "#else\n"
2292 "Three,\n"
2293 "#endif\n"
2294 "Four,\n"
2295 "#endif\n"
2296 "Five\n"
2297 "};"));
2299 // Ignore stuff in SWIG-blocks.
2300 EXPECT_EQ("#ifdef SWIG\n"
2301 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
2302 "#endif\n"
2303 "void f() {}",
2304 format("#ifdef SWIG\n"
2305 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
2306 "#endif\n"
2307 "void f( ) { }"));
2308 EXPECT_EQ("#ifndef SWIG\n"
2309 "void f() {}\n"
2310 "#endif",
2311 format("#ifndef SWIG\n"
2312 "void f( ) { }\n"
2313 "#endif"));
2316 TEST_F(FormatTestComments, DontCrashOnBlockComments) {
2317 EXPECT_EQ(
2318 "int xxxxxxxxx; /* "
2319 "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\n"
2320 "zzzzzz\n"
2321 "0*/",
2322 format("int xxxxxxxxx; /* "
2323 "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy zzzzzz\n"
2324 "0*/"));
2327 TEST_F(FormatTestComments, BlockCommentsInControlLoops) {
2328 verifyFormat("if (0) /* a comment in a strange place */ {\n"
2329 " f();\n"
2330 "}");
2331 verifyFormat("if (0) /* a comment in a strange place */ {\n"
2332 " f();\n"
2333 "} /* another comment */ else /* comment #3 */ {\n"
2334 " g();\n"
2335 "}");
2336 verifyFormat("while (0) /* a comment in a strange place */ {\n"
2337 " f();\n"
2338 "}");
2339 verifyFormat("for (;;) /* a comment in a strange place */ {\n"
2340 " f();\n"
2341 "}");
2342 verifyFormat("do /* a comment in a strange place */ {\n"
2343 " f();\n"
2344 "} /* another comment */ while (0);");
2347 TEST_F(FormatTestComments, BlockComments) {
2348 EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */",
2349 format("/* *//* */ /* */\n/* *//* */ /* */"));
2350 EXPECT_EQ("/* */ a /* */ b;", format(" /* */ a/* */ b;"));
2351 EXPECT_EQ("#define A /*123*/ \\\n"
2352 " b\n"
2353 "/* */\n"
2354 "someCall(\n"
2355 " parameter);",
2356 format("#define A /*123*/ b\n"
2357 "/* */\n"
2358 "someCall(parameter);",
2359 getLLVMStyleWithColumns(15)));
2361 EXPECT_EQ("#define A\n"
2362 "/* */ someCall(\n"
2363 " parameter);",
2364 format("#define A\n"
2365 "/* */someCall(parameter);",
2366 getLLVMStyleWithColumns(15)));
2367 verifyNoChange("/*\n**\n*/");
2368 EXPECT_EQ("/*\n"
2369 " *\n"
2370 " * aaaaaa\n"
2371 " * aaaaaa\n"
2372 " */",
2373 format("/*\n"
2374 "*\n"
2375 " * aaaaaa aaaaaa\n"
2376 "*/",
2377 getLLVMStyleWithColumns(10)));
2378 EXPECT_EQ("/*\n"
2379 "**\n"
2380 "* aaaaaa\n"
2381 "*aaaaaa\n"
2382 "*/",
2383 format("/*\n"
2384 "**\n"
2385 "* aaaaaa aaaaaa\n"
2386 "*/",
2387 getLLVMStyleWithColumns(10)));
2388 EXPECT_EQ("int aaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
2389 " /* line 1\n"
2390 " bbbbbbbbbbbb */\n"
2391 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
2392 format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
2393 " /* line 1\n"
2394 " bbbbbbbbbbbb */ bbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
2395 getLLVMStyleWithColumns(50)));
2397 FormatStyle NoBinPacking = getLLVMStyle();
2398 NoBinPacking.BinPackParameters = false;
2399 EXPECT_EQ("someFunction(1, /* comment 1 */\n"
2400 " 2, /* comment 2 */\n"
2401 " 3, /* comment 3 */\n"
2402 " aaaa,\n"
2403 " bbbb);",
2404 format("someFunction (1, /* comment 1 */\n"
2405 " 2, /* comment 2 */ \n"
2406 " 3, /* comment 3 */\n"
2407 "aaaa, bbbb );",
2408 NoBinPacking));
2409 verifyFormat(
2410 "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2411 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
2412 EXPECT_EQ(
2413 "bool aaaaaaaaaaaaa = /* trailing comment */\n"
2414 " aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2415 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;",
2416 format(
2417 "bool aaaaaaaaaaaaa = /* trailing comment */\n"
2418 " aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2419 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;"));
2420 EXPECT_EQ(
2421 "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
2422 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
2423 "int cccccccccccccccccccccccccccccc; /* comment */",
2424 format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
2425 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
2426 "int cccccccccccccccccccccccccccccc; /* comment */"));
2428 verifyFormat("void f(int * /* unused */) {}");
2430 EXPECT_EQ("/*\n"
2431 " **\n"
2432 " */",
2433 format("/*\n"
2434 " **\n"
2435 " */"));
2436 EXPECT_EQ("/*\n"
2437 " *q\n"
2438 " */",
2439 format("/*\n"
2440 " *q\n"
2441 " */"));
2442 EXPECT_EQ("/*\n"
2443 " * q\n"
2444 " */",
2445 format("/*\n"
2446 " * q\n"
2447 " */"));
2448 EXPECT_EQ("/*\n"
2449 " **/",
2450 format("/*\n"
2451 " **/"));
2452 EXPECT_EQ("/*\n"
2453 " ***/",
2454 format("/*\n"
2455 " ***/"));
2458 TEST_F(FormatTestComments, BlockCommentsInMacros) {
2459 EXPECT_EQ("#define A \\\n"
2460 " { \\\n"
2461 " /* one line */ \\\n"
2462 " someCall();",
2463 format("#define A { \\\n"
2464 " /* one line */ \\\n"
2465 " someCall();",
2466 getLLVMStyleWithColumns(20)));
2467 EXPECT_EQ("#define A \\\n"
2468 " { \\\n"
2469 " /* previous */ \\\n"
2470 " /* one line */ \\\n"
2471 " someCall();",
2472 format("#define A { \\\n"
2473 " /* previous */ \\\n"
2474 " /* one line */ \\\n"
2475 " someCall();",
2476 getLLVMStyleWithColumns(20)));
2479 TEST_F(FormatTestComments, BlockCommentsAtEndOfLine) {
2480 EXPECT_EQ("a = {\n"
2481 " 1111 /* */\n"
2482 "};",
2483 format("a = {1111 /* */\n"
2484 "};",
2485 getLLVMStyleWithColumns(15)));
2486 EXPECT_EQ("a = {\n"
2487 " 1111 /* */\n"
2488 "};",
2489 format("a = {1111 /* */\n"
2490 "};",
2491 getLLVMStyleWithColumns(15)));
2492 EXPECT_EQ("a = {\n"
2493 " 1111 /* a\n"
2494 " */\n"
2495 "};",
2496 format("a = {1111 /* a */\n"
2497 "};",
2498 getLLVMStyleWithColumns(15)));
2501 TEST_F(FormatTestComments, BreaksAfterMultilineBlockCommentsInParamLists) {
2502 EXPECT_EQ("a = f(/* long\n"
2503 " long */\n"
2504 " a);",
2505 format("a = f(/* long long */ a);", getLLVMStyleWithColumns(16)));
2506 EXPECT_EQ("a = f(\n"
2507 " /* long\n"
2508 " long */\n"
2509 " a);",
2510 format("a = f(/* long long */ a);", getLLVMStyleWithColumns(15)));
2512 EXPECT_EQ("a = f(/* long\n"
2513 " long\n"
2514 " */\n"
2515 " a);",
2516 format("a = f(/* long\n"
2517 " long\n"
2518 " */a);",
2519 getLLVMStyleWithColumns(16)));
2521 EXPECT_EQ("a = f(/* long\n"
2522 " long\n"
2523 " */\n"
2524 " a);",
2525 format("a = f(/* long\n"
2526 " long\n"
2527 " */ a);",
2528 getLLVMStyleWithColumns(16)));
2530 EXPECT_EQ("a = f(/* long\n"
2531 " long\n"
2532 " */\n"
2533 " (1 + 1));",
2534 format("a = f(/* long\n"
2535 " long\n"
2536 " */ (1 + 1));",
2537 getLLVMStyleWithColumns(16)));
2539 EXPECT_EQ(
2540 "a = f(a,\n"
2541 " /* long\n"
2542 " long */\n"
2543 " b);",
2544 format("a = f(a, /* long long */ b);", getLLVMStyleWithColumns(16)));
2546 EXPECT_EQ(
2547 "a = f(\n"
2548 " a,\n"
2549 " /* long\n"
2550 " long */\n"
2551 " b);",
2552 format("a = f(a, /* long long */ b);", getLLVMStyleWithColumns(15)));
2554 EXPECT_EQ("a = f(a,\n"
2555 " /* long\n"
2556 " long */\n"
2557 " (1 + 1));",
2558 format("a = f(a, /* long long */ (1 + 1));",
2559 getLLVMStyleWithColumns(16)));
2560 EXPECT_EQ("a = f(\n"
2561 " a,\n"
2562 " /* long\n"
2563 " long */\n"
2564 " (1 + 1));",
2565 format("a = f(a, /* long long */ (1 + 1));",
2566 getLLVMStyleWithColumns(15)));
2569 TEST_F(FormatTestComments, IndentLineCommentsInStartOfBlockAtEndOfFile) {
2570 verifyFormat("{\n"
2571 " // a\n"
2572 " // b");
2575 TEST_F(FormatTestComments, AlignTrailingComments) {
2576 EXPECT_EQ("#define MACRO(V) \\\n"
2577 " V(Rt2) /* one more char */ \\\n"
2578 " V(Rs) /* than here */ \\\n"
2579 "/* comment 3 */\n",
2580 format("#define MACRO(V)\\\n"
2581 "V(Rt2) /* one more char */ \\\n"
2582 "V(Rs) /* than here */ \\\n"
2583 "/* comment 3 */\n",
2584 getLLVMStyleWithColumns(40)));
2585 EXPECT_EQ("int i = f(abc, // line 1\n"
2586 " d, // line 2\n"
2587 " // line 3\n"
2588 " b);",
2589 format("int i = f(abc, // line 1\n"
2590 " d, // line 2\n"
2591 " // line 3\n"
2592 " b);",
2593 getLLVMStyleWithColumns(40)));
2595 // Align newly broken trailing comments.
2596 EXPECT_EQ("int ab; // line\n"
2597 "int a; // long\n"
2598 " // long",
2599 format("int ab; // line\n"
2600 "int a; // long long",
2601 getLLVMStyleWithColumns(15)));
2602 EXPECT_EQ("int ab; // line\n"
2603 "int a; // long\n"
2604 " // long\n"
2605 " // long",
2606 format("int ab; // line\n"
2607 "int a; // long long\n"
2608 " // long",
2609 getLLVMStyleWithColumns(15)));
2610 EXPECT_EQ("int ab; // line\n"
2611 "int a; // long\n"
2612 " // long\n"
2613 "pt c; // long",
2614 format("int ab; // line\n"
2615 "int a; // long long\n"
2616 "pt c; // long",
2617 getLLVMStyleWithColumns(15)));
2618 EXPECT_EQ("int ab; // line\n"
2619 "int a; // long\n"
2620 " // long\n"
2621 "\n"
2622 "// long",
2623 format("int ab; // line\n"
2624 "int a; // long long\n"
2625 "\n"
2626 "// long",
2627 getLLVMStyleWithColumns(15)));
2629 // Don't align newly broken trailing comments if that would put them over the
2630 // column limit.
2631 EXPECT_EQ("int i, j; // line 1\n"
2632 "int k; // line longg\n"
2633 " // long",
2634 format("int i, j; // line 1\n"
2635 "int k; // line longg long",
2636 getLLVMStyleWithColumns(20)));
2638 // Always align if ColumnLimit = 0
2639 EXPECT_EQ("int i, j; // line 1\n"
2640 "int k; // line longg long",
2641 format("int i, j; // line 1\n"
2642 "int k; // line longg long",
2643 getLLVMStyleWithColumns(0)));
2645 // Align comment line sections aligned with the next token with the next
2646 // token.
2647 EXPECT_EQ("class A {\n"
2648 "public: // public comment\n"
2649 " // comment about a\n"
2650 " int a;\n"
2651 "};",
2652 format("class A {\n"
2653 "public: // public comment\n"
2654 " // comment about a\n"
2655 " int a;\n"
2656 "};",
2657 getLLVMStyleWithColumns(40)));
2658 EXPECT_EQ("class A {\n"
2659 "public: // public comment 1\n"
2660 " // public comment 2\n"
2661 " // comment 1 about a\n"
2662 " // comment 2 about a\n"
2663 " int a;\n"
2664 "};",
2665 format("class A {\n"
2666 "public: // public comment 1\n"
2667 " // public comment 2\n"
2668 " // comment 1 about a\n"
2669 " // comment 2 about a\n"
2670 " int a;\n"
2671 "};",
2672 getLLVMStyleWithColumns(40)));
2673 EXPECT_EQ("int f(int n) { // comment line 1 on f\n"
2674 " // comment line 2 on f\n"
2675 " // comment line 1 before return\n"
2676 " // comment line 2 before return\n"
2677 " return n; // comment line 1 on return\n"
2678 " // comment line 2 on return\n"
2679 " // comment line 1 after return\n"
2680 "}",
2681 format("int f(int n) { // comment line 1 on f\n"
2682 " // comment line 2 on f\n"
2683 " // comment line 1 before return\n"
2684 " // comment line 2 before return\n"
2685 " return n; // comment line 1 on return\n"
2686 " // comment line 2 on return\n"
2687 " // comment line 1 after return\n"
2688 "}",
2689 getLLVMStyleWithColumns(40)));
2690 EXPECT_EQ("int f(int n) {\n"
2691 " switch (n) { // comment line 1 on switch\n"
2692 " // comment line 2 on switch\n"
2693 " // comment line 1 before case 1\n"
2694 " // comment line 2 before case 1\n"
2695 " case 1: // comment line 1 on case 1\n"
2696 " // comment line 2 on case 1\n"
2697 " // comment line 1 before return 1\n"
2698 " // comment line 2 before return 1\n"
2699 " return 1; // comment line 1 on return 1\n"
2700 " // comment line 2 on return 1\n"
2701 " // comment line 1 before default\n"
2702 " // comment line 2 before default\n"
2703 " default: // comment line 1 on default\n"
2704 " // comment line 2 on default\n"
2705 " // comment line 1 before return 2\n"
2706 " return 2 * f(n - 1); // comment line 1 on return 2\n"
2707 " // comment line 2 on return 2\n"
2708 " // comment line 1 after return\n"
2709 " // comment line 2 after return\n"
2710 " }\n"
2711 "}",
2712 format("int f(int n) {\n"
2713 " switch (n) { // comment line 1 on switch\n"
2714 " // comment line 2 on switch\n"
2715 " // comment line 1 before case 1\n"
2716 " // comment line 2 before case 1\n"
2717 " case 1: // comment line 1 on case 1\n"
2718 " // comment line 2 on case 1\n"
2719 " // comment line 1 before return 1\n"
2720 " // comment line 2 before return 1\n"
2721 " return 1; // comment line 1 on return 1\n"
2722 " // comment line 2 on return 1\n"
2723 " // comment line 1 before default\n"
2724 " // comment line 2 before default\n"
2725 " default: // comment line 1 on default\n"
2726 " // comment line 2 on default\n"
2727 " // comment line 1 before return 2\n"
2728 " return 2 * f(n - 1); // comment line 1 on return 2\n"
2729 " // comment line 2 on return 2\n"
2730 " // comment line 1 after return\n"
2731 " // comment line 2 after return\n"
2732 " }\n"
2733 "}",
2734 getLLVMStyleWithColumns(80)));
2736 // If all the lines in a sequence of line comments are aligned with the next
2737 // token, the first line belongs to the previous token and the other lines
2738 // belong to the next token.
2739 EXPECT_EQ("int a; // line about a\n"
2740 "long b;",
2741 format("int a; // line about a\n"
2742 " long b;",
2743 getLLVMStyleWithColumns(80)));
2744 EXPECT_EQ("int a; // line about a\n"
2745 "// line about b\n"
2746 "long b;",
2747 format("int a; // line about a\n"
2748 " // line about b\n"
2749 " long b;",
2750 getLLVMStyleWithColumns(80)));
2751 EXPECT_EQ("int a; // line about a\n"
2752 "// line 1 about b\n"
2753 "// line 2 about b\n"
2754 "long b;",
2755 format("int a; // line about a\n"
2756 " // line 1 about b\n"
2757 " // line 2 about b\n"
2758 " long b;",
2759 getLLVMStyleWithColumns(80)));
2761 // Checks an edge case in preprocessor handling.
2762 // These comments should *not* be aligned
2763 EXPECT_EQ(
2764 "#if FOO\n"
2765 "#else\n"
2766 "long a; // Line about a\n"
2767 "#endif\n"
2768 "#if BAR\n"
2769 "#else\n"
2770 "long b_long_name; // Line about b\n"
2771 "#endif",
2772 format("#if FOO\n"
2773 "#else\n"
2774 "long a; // Line about a\n" // Previous (bad) behavior
2775 "#endif\n"
2776 "#if BAR\n"
2777 "#else\n"
2778 "long b_long_name; // Line about b\n"
2779 "#endif",
2780 getLLVMStyleWithColumns(80)));
2782 // bug 47589
2783 EXPECT_EQ(
2784 "namespace m {\n\n"
2785 "#define FOO_GLOBAL 0 // Global scope.\n"
2786 "#define FOO_LINKLOCAL 1 // Link-local scope.\n"
2787 "#define FOO_SITELOCAL 2 // Site-local scope (deprecated).\n"
2788 "#define FOO_UNIQUELOCAL 3 // Unique local\n"
2789 "#define FOO_NODELOCAL 4 // Loopback\n\n"
2790 "} // namespace m",
2791 format("namespace m {\n\n"
2792 "#define FOO_GLOBAL 0 // Global scope.\n"
2793 "#define FOO_LINKLOCAL 1 // Link-local scope.\n"
2794 "#define FOO_SITELOCAL 2 // Site-local scope (deprecated).\n"
2795 "#define FOO_UNIQUELOCAL 3 // Unique local\n"
2796 "#define FOO_NODELOCAL 4 // Loopback\n\n"
2797 "} // namespace m",
2798 getLLVMStyleWithColumns(80)));
2800 // https://llvm.org/PR53441
2801 verifyFormat("/* */ //\n"
2802 "int a; //");
2803 verifyFormat("/**/ //\n"
2804 "int a; //");
2807 TEST_F(FormatTestComments, AlignTrailingCommentsAcrossEmptyLines) {
2808 FormatStyle Style = getLLVMStyle();
2809 Style.AlignTrailingComments.Kind = FormatStyle::TCAS_Always;
2810 Style.AlignTrailingComments.OverEmptyLines = 1;
2811 verifyFormat("#include \"a.h\" // simple\n"
2812 "\n"
2813 "#include \"aa.h\" // example case",
2814 Style);
2816 verifyFormat("#include \"a.h\" // align across\n"
2817 "\n"
2818 "#include \"aa.h\" // two empty lines\n"
2819 "\n"
2820 "#include \"aaa.h\" // in a row",
2821 Style);
2823 verifyFormat("#include \"a.h\" // align\n"
2824 "#include \"aa.h\" // comment\n"
2825 "#include \"aaa.h\" // blocks\n"
2826 "\n"
2827 "#include \"aaaa.h\" // across\n"
2828 "#include \"aaaaa.h\" // one\n"
2829 "#include \"aaaaaa.h\" // empty line",
2830 Style);
2832 verifyFormat("#include \"a.h\" // align trailing comments\n"
2833 "#include \"a.h\"\n"
2834 "#include \"aa.h\" // across a line without comment",
2835 Style);
2837 verifyFormat("#include \"a.h\" // align across\n"
2838 "#include \"a.h\"\n"
2839 "#include \"aa.h\" // two lines without comment\n"
2840 "#include \"a.h\"\n"
2841 "#include \"aaa.h\" // in a row",
2842 Style);
2844 verifyFormat("#include \"a.h\" // align\n"
2845 "#include \"aa.h\" // comment\n"
2846 "#include \"aaa.h\" // blocks\n"
2847 "#include \"a.h\"\n"
2848 "#include \"aaaa.h\" // across\n"
2849 "#include \"aaaaa.h\" // a line without\n"
2850 "#include \"aaaaaa.h\" // comment",
2851 Style);
2853 // Start of testing OverEmptyLines
2854 Style.MaxEmptyLinesToKeep = 3;
2855 Style.AlignTrailingComments.OverEmptyLines = 2;
2856 // Cannot use verifyFormat here
2857 // test::messUp removes all new lines which changes the logic
2858 EXPECT_EQ("#include \"a.h\" // comment\n"
2859 "\n"
2860 "\n"
2861 "\n"
2862 "#include \"ab.h\" // comment\n"
2863 "\n"
2864 "\n"
2865 "#include \"abcdefg.h\" // comment",
2866 format("#include \"a.h\" // comment\n"
2867 "\n"
2868 "\n"
2869 "\n"
2870 "#include \"ab.h\" // comment\n"
2871 "\n"
2872 "\n"
2873 "#include \"abcdefg.h\" // comment",
2874 Style));
2876 Style.MaxEmptyLinesToKeep = 1;
2877 Style.AlignTrailingComments.OverEmptyLines = 1;
2878 // End of testing OverEmptyLines
2880 Style.ColumnLimit = 15;
2881 EXPECT_EQ("int ab; // line\n"
2882 "int a; // long\n"
2883 " // long\n"
2884 "\n"
2885 " // long",
2886 format("int ab; // line\n"
2887 "int a; // long long\n"
2888 "\n"
2889 "// long",
2890 Style));
2892 Style.ColumnLimit = 15;
2893 EXPECT_EQ("int ab; // line\n"
2894 "\n"
2895 "int a; // long\n"
2896 " // long",
2897 format("int ab; // line\n"
2898 "\n"
2899 "int a; // long long",
2900 Style));
2902 Style.ColumnLimit = 30;
2903 EXPECT_EQ("int foo = 12345; // comment\n"
2904 "int bar =\n"
2905 " 1234; // This is a very\n"
2906 " // long comment\n"
2907 " // which is wrapped\n"
2908 " // arround.\n"
2909 "\n"
2910 "int x = 2; // Is this still\n"
2911 " // aligned?",
2912 format("int foo = 12345; // comment\n"
2913 "int bar = 1234; // This is a very long comment\n"
2914 " // which is wrapped arround.\n"
2915 "\n"
2916 "int x = 2; // Is this still aligned?",
2917 Style));
2919 Style.ColumnLimit = 35;
2920 EXPECT_EQ("int foo = 12345; // comment\n"
2921 "int bar =\n"
2922 " 1234; // This is a very long\n"
2923 " // comment which is\n"
2924 " // wrapped arround.\n"
2925 "\n"
2926 "int x =\n"
2927 " 2; // Is this still aligned?",
2928 format("int foo = 12345; // comment\n"
2929 "int bar = 1234; // This is a very long comment\n"
2930 " // which is wrapped arround.\n"
2931 "\n"
2932 "int x = 2; // Is this still aligned?",
2933 Style));
2935 Style.ColumnLimit = 40;
2936 EXPECT_EQ("int foo = 12345; // comment\n"
2937 "int bar =\n"
2938 " 1234; // This is a very long comment\n"
2939 " // which is wrapped arround.\n"
2940 "\n"
2941 "int x = 2; // Is this still aligned?",
2942 format("int foo = 12345; // comment\n"
2943 "int bar = 1234; // This is a very long comment\n"
2944 " // which is wrapped arround.\n"
2945 "\n"
2946 "int x = 2; // Is this still aligned?",
2947 Style));
2949 Style.ColumnLimit = 45;
2950 EXPECT_EQ("int foo = 12345; // comment\n"
2951 "int bar =\n"
2952 " 1234; // This is a very long comment\n"
2953 " // which is wrapped arround.\n"
2954 "\n"
2955 "int x = 2; // Is this still aligned?",
2956 format("int foo = 12345; // comment\n"
2957 "int bar = 1234; // This is a very long comment\n"
2958 " // which is wrapped arround.\n"
2959 "\n"
2960 "int x = 2; // Is this still aligned?",
2961 Style));
2963 Style.ColumnLimit = 80;
2964 EXPECT_EQ("int a; // line about a\n"
2965 "\n"
2966 "// line about b\n"
2967 "long b;",
2968 format("int a; // line about a\n"
2969 "\n"
2970 " // line about b\n"
2971 " long b;",
2972 Style));
2974 Style.ColumnLimit = 80;
2975 EXPECT_EQ("int a; // line about a\n"
2976 "\n"
2977 "// line 1 about b\n"
2978 "// line 2 about b\n"
2979 "long b;",
2980 format("int a; // line about a\n"
2981 "\n"
2982 " // line 1 about b\n"
2983 " // line 2 about b\n"
2984 " long b;",
2985 Style));
2988 TEST_F(FormatTestComments, AlignTrailingCommentsLeave) {
2989 FormatStyle Style = getLLVMStyle();
2990 Style.AlignTrailingComments.Kind = FormatStyle::TCAS_Leave;
2992 EXPECT_EQ("int a;// do not touch\n"
2993 "int b; // any comments\n"
2994 "int c; // comment\n"
2995 "int d; // comment",
2996 format("int a;// do not touch\n"
2997 "int b; // any comments\n"
2998 "int c; // comment\n"
2999 "int d; // comment",
3000 Style));
3002 EXPECT_EQ("int a; // do not touch\n"
3003 "int b; // any comments\n"
3004 "int c; // comment\n"
3005 "int d;// comment",
3006 format("int a; // do not touch\n"
3007 "int b; // any comments\n"
3008 "int c; // comment\n"
3009 "int d;// comment",
3010 Style));
3012 EXPECT_EQ("// do not touch\n"
3013 "int a; // any comments\n"
3014 "\n"
3015 " // comment\n"
3016 "// comment\n"
3017 "\n"
3018 "// comment",
3019 format("// do not touch\n"
3020 "int a; // any comments\n"
3021 "\n"
3022 " // comment\n"
3023 "// comment\n"
3024 "\n"
3025 "// comment",
3026 Style));
3028 EXPECT_EQ("// do not touch\n"
3029 "int a; // any comments\n"
3030 "\n"
3031 " // comment\n"
3032 "// comment\n"
3033 "\n"
3034 "// comment",
3035 format("// do not touch\n"
3036 "int a; // any comments\n"
3037 "\n"
3038 "\n"
3039 " // comment\n"
3040 "// comment\n"
3041 "\n"
3042 "\n"
3043 "// comment",
3044 Style));
3046 verifyFormat("namespace ns {\n"
3047 "int i;\n"
3048 "int j;\n"
3049 "} // namespace ns",
3050 "namespace ns {\n"
3051 "int i;\n"
3052 "int j;\n"
3053 "}",
3054 Style);
3056 Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
3057 verifyNoChange("#define FOO \\\n"
3058 " /* foo(); */ \\\n"
3059 " bar();",
3060 Style);
3062 // Allow to keep 2 empty lines
3063 Style.MaxEmptyLinesToKeep = 2;
3064 EXPECT_EQ("// do not touch\n"
3065 "int a; // any comments\n"
3066 "\n"
3067 "\n"
3068 " // comment\n"
3069 "// comment\n"
3070 "\n"
3071 "// comment",
3072 format("// do not touch\n"
3073 "int a; // any comments\n"
3074 "\n"
3075 "\n"
3076 " // comment\n"
3077 "// comment\n"
3078 "\n"
3079 "// comment",
3080 Style));
3081 Style.MaxEmptyLinesToKeep = 1;
3083 // Just format comments normally when leaving exceeds the column limit
3084 Style.ColumnLimit = 35;
3085 EXPECT_EQ("int foo = 12345; // comment\n"
3086 "int bar =\n"
3087 " 1234; // This is a very long\n"
3088 " // comment which is\n"
3089 " // wrapped arround.",
3090 format("int foo = 12345; // comment\n"
3091 "int bar = 1234; // This is a very long comment\n"
3092 " // which is wrapped arround.",
3093 Style));
3096 TEST_F(FormatTestComments, DontAlignNamespaceComments) {
3097 FormatStyle Style = getLLVMStyle();
3098 Style.NamespaceIndentation = FormatStyle::NI_All;
3099 Style.NamespaceMacros.push_back("TESTSUITE");
3100 Style.ShortNamespaceLines = 0;
3102 StringRef Input = "namespace A {\n"
3103 " TESTSUITE(B) {\n"
3104 " namespace C {\n"
3105 " namespace D { //\n"
3106 " } // namespace D\n"
3107 " std::string Foo = Bar; // Comment\n"
3108 " std::string BazString = Baz; // C2\n"
3109 " } // namespace C\n"
3110 " }\n"
3111 "} // NaMeSpAcE A";
3113 EXPECT_TRUE(Style.FixNamespaceComments);
3114 EXPECT_EQ(Style.AlignTrailingComments.Kind, FormatStyle::TCAS_Always);
3115 verifyFormat("namespace A {\n"
3116 " TESTSUITE(B) {\n"
3117 " namespace C {\n"
3118 " namespace D { //\n"
3119 " } // namespace D\n"
3120 " std::string Foo = Bar; // Comment\n"
3121 " std::string BazString = Baz; // C2\n"
3122 " } // namespace C\n"
3123 " } // TESTSUITE(B)\n"
3124 "} // NaMeSpAcE A",
3125 Input, Style);
3127 Style.AlignTrailingComments.Kind = FormatStyle::TCAS_Never;
3128 verifyFormat("namespace A {\n"
3129 " TESTSUITE(B) {\n"
3130 " namespace C {\n"
3131 " namespace D { //\n"
3132 " } // namespace D\n"
3133 " std::string Foo = Bar; // Comment\n"
3134 " std::string BazString = Baz; // C2\n"
3135 " } // namespace C\n"
3136 " } // TESTSUITE(B)\n"
3137 "} // NaMeSpAcE A",
3138 Input, Style);
3140 Style.AlignTrailingComments.Kind = FormatStyle::TCAS_Leave;
3141 verifyFormat("namespace A {\n"
3142 " TESTSUITE(B) {\n"
3143 " namespace C {\n"
3144 " namespace D { //\n"
3145 " } // namespace D\n"
3146 " std::string Foo = Bar; // Comment\n"
3147 " std::string BazString = Baz; // C2\n"
3148 " } // namespace C\n"
3149 " } // TESTSUITE(B)\n"
3150 "} // NaMeSpAcE A",
3151 Input, Style);
3153 Style.FixNamespaceComments = false;
3154 Style.AlignTrailingComments.Kind = FormatStyle::TCAS_Always;
3155 verifyFormat("namespace A {\n"
3156 " TESTSUITE(B) {\n"
3157 " namespace C {\n"
3158 " namespace D { //\n"
3159 " } // namespace D\n"
3160 " std::string Foo = Bar; // Comment\n"
3161 " std::string BazString = Baz; // C2\n"
3162 " } // namespace C\n"
3163 " }\n"
3164 "} // NaMeSpAcE A",
3165 Input, Style);
3167 Style.AlignTrailingComments.Kind = FormatStyle::TCAS_Never;
3168 verifyFormat("namespace A {\n"
3169 " TESTSUITE(B) {\n"
3170 " namespace C {\n"
3171 " namespace D { //\n"
3172 " } // namespace D\n"
3173 " std::string Foo = Bar; // Comment\n"
3174 " std::string BazString = Baz; // C2\n"
3175 " } // namespace C\n"
3176 " }\n"
3177 "} // NaMeSpAcE A",
3178 Input, Style);
3180 Style.AlignTrailingComments.Kind = FormatStyle::TCAS_Leave;
3181 verifyFormat("namespace A {\n"
3182 " TESTSUITE(B) {\n"
3183 " namespace C {\n"
3184 " namespace D { //\n"
3185 " } // namespace D\n"
3186 " std::string Foo = Bar; // Comment\n"
3187 " std::string BazString = Baz; // C2\n"
3188 " } // namespace C\n"
3189 " }\n"
3190 "} // NaMeSpAcE A",
3191 Input, Style);
3193 Style.AlignTrailingComments.Kind = FormatStyle::TCAS_Always;
3194 Style.FixNamespaceComments = true;
3195 Input = "namespace A {\n"
3196 " int Foo;\n"
3197 " int Bar;\n"
3198 "}\n"
3199 "// Comment";
3201 verifyFormat("namespace A {\n"
3202 " int Foo;\n"
3203 " int Bar;\n"
3204 "} // namespace A\n"
3205 "// Comment",
3206 Input, Style);
3208 Style.FixNamespaceComments = false;
3209 verifyFormat(Input, Style);
3212 TEST_F(FormatTestComments, DontAlignOverScope) {
3213 verifyFormat("if (foo) {\n"
3214 " int aLongVariable; // with comment\n"
3215 " int f; // aligned\n"
3216 "} // not aligned\n"
3217 "int bar; // new align\n"
3218 "int foobar; // group");
3220 verifyFormat("if (foo) {\n"
3221 " // something\n"
3222 "} else {\n"
3223 " int aLongVariable; // with comment\n"
3224 " int f; // aligned\n"
3225 "} // not aligned\n"
3226 "int bar; // new align\n"
3227 "int foobar; // group");
3229 verifyFormat("if (foo) {\n"
3230 " // something\n"
3231 "} else if (foo) {\n"
3232 " int aLongVariable; // with comment\n"
3233 " int f; // aligned\n"
3234 "} // not aligned\n"
3235 "int bar; // new align\n"
3236 "int foobar; // group");
3238 verifyFormat("while (foo) {\n"
3239 " int aLongVariable; // with comment\n"
3240 " int f; // aligned\n"
3241 "} // not aligned\n"
3242 "int bar; // new align\n"
3243 "int foobar; // group");
3245 verifyFormat("for (;;) {\n"
3246 " int aLongVariable; // with comment\n"
3247 " int f; // aligned\n"
3248 "} // not aligned\n"
3249 "int bar; // new align\n"
3250 "int foobar; // group");
3252 verifyFormat("do {\n"
3253 " int aLongVariable; // with comment\n"
3254 " int f; // aligned\n"
3255 "} while (foo); // not aligned\n"
3256 "int bar; // new align\n"
3257 "int foobar; // group");
3259 verifyFormat("do\n"
3260 " int aLongVariable; // with comment\n"
3261 "while (foo); // not aigned\n"
3262 "int bar; // new align\n"
3263 "int foobar; // group");
3265 verifyFormat("do\n"
3266 " int aLongVariable; // with comment\n"
3267 "/**/ while (foo); // not aigned\n"
3268 "int bar; // new align\n"
3269 "int foobar; // group");
3271 verifyFormat("switch (foo) {\n"
3272 "case 7: {\n"
3273 " int aLongVariable; // with comment\n"
3274 " int f; // aligned\n"
3275 "} // case not aligned\n"
3276 "} // switch also not aligned\n"
3277 "int bar; // new align\n"
3278 "int foobar; // group");
3280 verifyFormat("switch (foo) {\n"
3281 "default: {\n"
3282 " int aLongVariable; // with comment\n"
3283 " int f; // aligned\n"
3284 "} // case not aligned\n"
3285 "} // switch also not aligned\n"
3286 "int bar; // new align\n"
3287 "int foobar; // group");
3289 verifyFormat("class C {\n"
3290 " int aLongVariable; // with comment\n"
3291 " int f; // aligned\n"
3292 "}; // not aligned\n"
3293 "int bar; // new align\n"
3294 "int foobar; // group");
3296 verifyFormat("struct S {\n"
3297 " int aLongVariable; // with comment\n"
3298 " int f; // aligned\n"
3299 "}; // not aligned\n"
3300 "int bar; // new align\n"
3301 "int foobar; // group");
3303 verifyFormat("union U {\n"
3304 " int aLongVariable; // with comment\n"
3305 " int f; // aligned\n"
3306 "}; // not aligned\n"
3307 "int bar; // new align\n"
3308 "int foobar; // group");
3310 verifyFormat("enum E {\n"
3311 " aLongVariable, // with comment\n"
3312 " f // aligned\n"
3313 "}; // not aligned\n"
3314 "int bar; // new align\n"
3315 "int foobar; // group");
3317 verifyFormat("void foo() {\n"
3318 " {\n"
3319 " int aLongVariable; // with comment\n"
3320 " int f; // aligned\n"
3321 " } // not aligned\n"
3322 " int bar; // new align\n"
3323 " int foobar; // group\n"
3324 "}");
3326 verifyFormat("auto longLambda = [] { // comment\n"
3327 " int aLongVariable; // with comment\n"
3328 " int f; // aligned\n"
3329 "}; // not aligned\n"
3330 "int bar; // new align\n"
3331 "int foobar; // group\n"
3332 "auto shortLambda = [] { return 5; }; // aligned");
3334 verifyFormat("auto longLambdaResult = [] { // comment\n"
3335 " int aLongVariable; // with comment\n"
3336 " int f; // aligned\n"
3337 "}(); // not aligned\n"
3338 "int bar; // new align\n"
3339 "int foobar; // group\n"
3340 "auto shortLambda = [] { return 5; }(); // aligned");
3342 verifyFormat(
3343 "auto longLambdaResult = [](auto I, auto J) { // comment\n"
3344 " int aLongVariable; // with comment\n"
3345 " int f; // aligned\n"
3346 "}(\"Input\", 5); // not aligned\n"
3347 "int bar; // new align\n"
3348 "int foobar; // group\n"
3349 "auto shortL = [](auto I, auto J) { return 5; }(\"In\", 5); // aligned");
3351 verifyFormat("enum E1 { V1, V2 }; // Aligned\n"
3352 "enum E2 { LongerNames, InThis, Enum }; // Comments");
3354 verifyFormat("class C {\n"
3355 " int aLongVariable; // with comment\n"
3356 " int f; // aligned\n"
3357 "} /* middle comment */; // not aligned\n"
3358 "int bar; // new align\n"
3359 "int foobar; // group");
3362 TEST_F(FormatTestComments, AlignsBlockCommentDecorations) {
3363 EXPECT_EQ("/*\n"
3364 " */",
3365 format("/*\n"
3366 "*/"));
3367 EXPECT_EQ("/*\n"
3368 " */",
3369 format("/*\n"
3370 " */"));
3371 EXPECT_EQ("/*\n"
3372 " */",
3373 format("/*\n"
3374 " */"));
3376 // Align a single line.
3377 EXPECT_EQ("/*\n"
3378 " * line */",
3379 format("/*\n"
3380 "* line */"));
3381 EXPECT_EQ("/*\n"
3382 " * line */",
3383 format("/*\n"
3384 " * line */"));
3385 EXPECT_EQ("/*\n"
3386 " * line */",
3387 format("/*\n"
3388 " * line */"));
3389 EXPECT_EQ("/*\n"
3390 " * line */",
3391 format("/*\n"
3392 " * line */"));
3393 EXPECT_EQ("/**\n"
3394 " * line */",
3395 format("/**\n"
3396 "* line */"));
3397 EXPECT_EQ("/**\n"
3398 " * line */",
3399 format("/**\n"
3400 " * line */"));
3401 EXPECT_EQ("/**\n"
3402 " * line */",
3403 format("/**\n"
3404 " * line */"));
3405 EXPECT_EQ("/**\n"
3406 " * line */",
3407 format("/**\n"
3408 " * line */"));
3409 EXPECT_EQ("/**\n"
3410 " * line */",
3411 format("/**\n"
3412 " * line */"));
3414 // Align the end '*/' after a line.
3415 EXPECT_EQ("/*\n"
3416 " * line\n"
3417 " */",
3418 format("/*\n"
3419 "* line\n"
3420 "*/"));
3421 EXPECT_EQ("/*\n"
3422 " * line\n"
3423 " */",
3424 format("/*\n"
3425 " * line\n"
3426 " */"));
3427 EXPECT_EQ("/*\n"
3428 " * line\n"
3429 " */",
3430 format("/*\n"
3431 " * line\n"
3432 " */"));
3434 // Align two lines.
3435 EXPECT_EQ("/* line 1\n"
3436 " * line 2 */",
3437 format("/* line 1\n"
3438 " * line 2 */"));
3439 EXPECT_EQ("/* line 1\n"
3440 " * line 2 */",
3441 format("/* line 1\n"
3442 "* line 2 */"));
3443 EXPECT_EQ("/* line 1\n"
3444 " * line 2 */",
3445 format("/* line 1\n"
3446 " * line 2 */"));
3447 EXPECT_EQ("/* line 1\n"
3448 " * line 2 */",
3449 format("/* line 1\n"
3450 " * line 2 */"));
3451 EXPECT_EQ("/* line 1\n"
3452 " * line 2 */",
3453 format("/* line 1\n"
3454 " * line 2 */"));
3455 EXPECT_EQ("int i; /* line 1\n"
3456 " * line 2 */",
3457 format("int i; /* line 1\n"
3458 "* line 2 */"));
3459 EXPECT_EQ("int i; /* line 1\n"
3460 " * line 2 */",
3461 format("int i; /* line 1\n"
3462 " * line 2 */"));
3463 EXPECT_EQ("int i; /* line 1\n"
3464 " * line 2 */",
3465 format("int i; /* line 1\n"
3466 " * line 2 */"));
3468 // Align several lines.
3469 EXPECT_EQ("/* line 1\n"
3470 " * line 2\n"
3471 " * line 3 */",
3472 format("/* line 1\n"
3473 " * line 2\n"
3474 "* line 3 */"));
3475 EXPECT_EQ("/* line 1\n"
3476 " * line 2\n"
3477 " * line 3 */",
3478 format("/* line 1\n"
3479 " * line 2\n"
3480 "* line 3 */"));
3481 EXPECT_EQ("/*\n"
3482 "** line 1\n"
3483 "** line 2\n"
3484 "*/",
3485 format("/*\n"
3486 "** line 1\n"
3487 " ** line 2\n"
3488 "*/"));
3490 // Align with different indent after the decorations.
3491 EXPECT_EQ("/*\n"
3492 " * line 1\n"
3493 " * line 2\n"
3494 " * line 3\n"
3495 " * line 4\n"
3496 " */",
3497 format("/*\n"
3498 "* line 1\n"
3499 " * line 2\n"
3500 " * line 3\n"
3501 "* line 4\n"
3502 "*/"));
3504 // Align empty or blank lines.
3505 EXPECT_EQ("/**\n"
3506 " *\n"
3507 " *\n"
3508 " *\n"
3509 " */",
3510 format("/**\n"
3511 "* \n"
3512 " * \n"
3513 " *\n"
3514 "*/"));
3516 // Align while breaking and reflowing.
3517 EXPECT_EQ("/*\n"
3518 " * long long long\n"
3519 " * long long\n"
3520 " *\n"
3521 " * long */",
3522 format("/*\n"
3523 " * long long long long\n"
3524 " * long\n"
3525 " *\n"
3526 "* long */",
3527 getLLVMStyleWithColumns(20)));
3530 TEST_F(FormatTestComments, NoCrash_Bug34236) {
3531 // This is a test case from a crasher reported in:
3532 // https://bugs.llvm.org/show_bug.cgi?id=34236
3533 // Temporarily disable formatting for readability.
3534 // clang-format off
3535 EXPECT_EQ(
3536 "/* */ /*\n"
3537 " * a\n"
3538 " * b c d*/",
3539 format(
3540 "/* */ /*\n"
3541 " * a b\n"
3542 " * c d*/",
3543 getLLVMStyleWithColumns(80)));
3544 // clang-format on
3547 TEST_F(FormatTestComments, NonTrailingBlockComments) {
3548 verifyFormat("const /** comment comment */ A = B;",
3549 getLLVMStyleWithColumns(40));
3551 verifyFormat("const /** comment comment comment */ A =\n"
3552 " B;",
3553 getLLVMStyleWithColumns(40));
3555 EXPECT_EQ("const /** comment comment comment\n"
3556 " comment */\n"
3557 " A = B;",
3558 format("const /** comment comment comment comment */\n"
3559 " A = B;",
3560 getLLVMStyleWithColumns(40)));
3563 TEST_F(FormatTestComments, PythonStyleComments) {
3564 // Keeps a space after '#'.
3565 EXPECT_EQ("# comment\n"
3566 "key: value",
3567 format("#comment\n"
3568 "key:value",
3569 getTextProtoStyleWithColumns(20)));
3570 EXPECT_EQ("# comment\n"
3571 "key: value",
3572 format("# comment\n"
3573 "key:value",
3574 getTextProtoStyleWithColumns(20)));
3575 // Breaks long comment.
3576 EXPECT_EQ("# comment comment\n"
3577 "# comment\n"
3578 "key: value",
3579 format("# comment comment comment\n"
3580 "key:value",
3581 getTextProtoStyleWithColumns(20)));
3582 // Indents comments.
3583 EXPECT_EQ("data {\n"
3584 " # comment comment\n"
3585 " # comment\n"
3586 " key: value\n"
3587 "}",
3588 format("data {\n"
3589 "# comment comment comment\n"
3590 "key: value}",
3591 getTextProtoStyleWithColumns(20)));
3592 EXPECT_EQ("data {\n"
3593 " # comment comment\n"
3594 " # comment\n"
3595 " key: value\n"
3596 "}",
3597 format("data {# comment comment comment\n"
3598 "key: value}",
3599 getTextProtoStyleWithColumns(20)));
3600 // Reflows long comments.
3601 EXPECT_EQ("# comment comment\n"
3602 "# comment comment\n"
3603 "key: value",
3604 format("# comment comment comment\n"
3605 "# comment\n"
3606 "key:value",
3607 getTextProtoStyleWithColumns(20)));
3608 // Breaks trailing comments.
3609 EXPECT_EQ("k: val # comment\n"
3610 " # comment\n"
3611 "a: 1",
3612 format("k:val#comment comment\n"
3613 "a:1",
3614 getTextProtoStyleWithColumns(20)));
3615 EXPECT_EQ("id {\n"
3616 " k: val # comment\n"
3617 " # comment\n"
3618 " # line line\n"
3619 " a: 1\n"
3620 "}",
3621 format("id {k:val#comment comment\n"
3622 "# line line\n"
3623 "a:1}",
3624 getTextProtoStyleWithColumns(20)));
3625 // Aligns trailing comments.
3626 EXPECT_EQ("k: val # commen1\n"
3627 " # commen2\n"
3628 " # commen3\n"
3629 "# commen4\n"
3630 "a: 1 # commen5\n"
3631 " # commen6\n"
3632 " # commen7",
3633 format("k:val#commen1 commen2\n"
3634 " #commen3\n"
3635 "# commen4\n"
3636 "a:1#commen5 commen6\n"
3637 " #commen7",
3638 getTextProtoStyleWithColumns(20)));
3641 TEST_F(FormatTestComments, BreaksBeforeTrailingUnbreakableSequence) {
3642 // The end of /* trail */ is exactly at 80 columns, but the unbreakable
3643 // trailing sequence ); after it exceeds the column limit. Make sure we
3644 // correctly break the line in that case.
3645 verifyFormat("int a =\n"
3646 " foo(/* trail */);",
3647 getLLVMStyleWithColumns(23));
3650 TEST_F(FormatTestComments, ReflowBackslashCrash) {
3651 // clang-format off
3652 EXPECT_EQ(
3653 "// How to run:\n"
3654 "// bbbbb run \\\n"
3655 "// rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr\n"
3656 "// \\ <log_file> -- --output_directory=\"<output_directory>\"",
3657 format(
3658 "// How to run:\n"
3659 "// bbbbb run \\\n"
3660 "// rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr \\\n"
3661 "// <log_file> -- --output_directory=\"<output_directory>\""));
3662 // clang-format on
3665 TEST_F(FormatTestComments, IndentsLongJavadocAnnotatedLines) {
3666 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Java);
3667 Style.ColumnLimit = 60;
3668 FormatStyle Style20 = getGoogleStyle(FormatStyle::LK_Java);
3669 Style20.ColumnLimit = 20;
3670 EXPECT_EQ(
3671 "/**\n"
3672 " * @param x long long long long long long long long long\n"
3673 " * long\n"
3674 " */",
3675 format("/**\n"
3676 " * @param x long long long long long long long long long long\n"
3677 " */",
3678 Style));
3679 EXPECT_EQ("/**\n"
3680 " * @param x long long long long long long long long long\n"
3681 " * long long long long long long long long long long\n"
3682 " */",
3683 format("/**\n"
3684 " * @param x long long long long long long long long long "
3685 "long long long long long long long long long long\n"
3686 " */",
3687 Style));
3688 EXPECT_EQ("/**\n"
3689 " * @param x long long long long long long long long long\n"
3690 " * long long long long long long long long long long\n"
3691 " * long\n"
3692 " */",
3693 format("/**\n"
3694 " * @param x long long long long long long long long long "
3695 "long long long long long long long long long long long\n"
3696 " */",
3697 Style));
3698 EXPECT_EQ("/**\n"
3699 " * Sentence that\n"
3700 " * should be broken.\n"
3701 " * @param short\n"
3702 " * keep indentation\n"
3703 " */",
3704 format("/**\n"
3705 " * Sentence that should be broken.\n"
3706 " * @param short\n"
3707 " * keep indentation\n"
3708 " */",
3709 Style20));
3711 EXPECT_EQ("/**\n"
3712 " * @param l1 long1\n"
3713 " * to break\n"
3714 " * @param l2 long2\n"
3715 " * to break\n"
3716 " */",
3717 format("/**\n"
3718 " * @param l1 long1 to break\n"
3719 " * @param l2 long2 to break\n"
3720 " */",
3721 Style20));
3723 EXPECT_EQ("/**\n"
3724 " * @param xx to\n"
3725 " * break\n"
3726 " * no reflow\n"
3727 " */",
3728 format("/**\n"
3729 " * @param xx to break\n"
3730 " * no reflow\n"
3731 " */",
3732 Style20));
3734 EXPECT_EQ("/**\n"
3735 " * @param xx to\n"
3736 " * break yes\n"
3737 " * reflow\n"
3738 " */",
3739 format("/**\n"
3740 " * @param xx to break\n"
3741 " * yes reflow\n"
3742 " */",
3743 Style20));
3745 FormatStyle JSStyle20 = getGoogleStyle(FormatStyle::LK_JavaScript);
3746 JSStyle20.ColumnLimit = 20;
3747 EXPECT_EQ("/**\n"
3748 " * @param l1 long1\n"
3749 " * to break\n"
3750 " */",
3751 format("/**\n"
3752 " * @param l1 long1 to break\n"
3753 " */",
3754 JSStyle20));
3755 EXPECT_EQ("/**\n"
3756 " * @param {l1 long1\n"
3757 " * to break}\n"
3758 " */",
3759 format("/**\n"
3760 " * @param {l1 long1 to break}\n"
3761 " */",
3762 JSStyle20));
3765 TEST_F(FormatTestComments, SpaceAtLineCommentBegin) {
3766 FormatStyle Style = getLLVMStyle();
3767 StringRef NoTextInComment = " // \n"
3768 "\n"
3769 "void foo() {// \n"
3770 "// \n"
3771 "}";
3773 EXPECT_EQ("//\n"
3774 "\n"
3775 "void foo() { //\n"
3776 " //\n"
3777 "}",
3778 format(NoTextInComment, Style));
3780 Style.SpacesInLineCommentPrefix.Minimum = 0;
3781 verifyFormat("//#comment", Style);
3782 EXPECT_EQ("//\n"
3783 "\n"
3784 "void foo() { //\n"
3785 " //\n"
3786 "}",
3787 format(NoTextInComment, Style));
3789 Style.SpacesInLineCommentPrefix.Minimum = 5;
3790 EXPECT_EQ("// #comment", format("//#comment", Style));
3791 EXPECT_EQ("//\n"
3792 "\n"
3793 "void foo() { //\n"
3794 " //\n"
3795 "}",
3796 format(NoTextInComment, Style));
3798 Style = getLLVMStyle();
3799 StringRef Code =
3800 "//Free comment without space\n"
3801 "\n"
3802 "// Free comment with 3 spaces\n"
3803 "\n"
3804 "///Free Doxygen without space\n"
3805 "\n"
3806 "/// Free Doxygen with 3 spaces\n"
3807 "\n"
3808 "//🐉 A nice dragon\n"
3809 "\n"
3810 "//\t abccba\n"
3811 "\n"
3812 "//\\t deffed\n"
3813 "\n"
3814 "// 🐉 Another nice dragon\n"
3815 "\n"
3816 "// \t Three leading spaces following tab\n"
3817 "\n"
3818 "// \\t Three leading spaces following backslash\n"
3819 "\n"
3820 "/// A Doxygen Comment with a nested list:\n"
3821 "/// - Foo\n"
3822 "/// - Bar\n"
3823 "/// - Baz\n"
3824 "/// - End\n"
3825 "/// of the inner list\n"
3826 "/// .\n"
3827 "/// .\n"
3828 "\n"
3829 "namespace Foo {\n"
3830 "bool bar(bool b) {\n"
3831 " bool ret1 = true; ///<Doxygenstyle without space\n"
3832 " bool ret2 = true; ///< Doxygenstyle with 3 spaces\n"
3833 " if (b) {\n"
3834 " //Foo\n"
3835 "\n"
3836 " // In function comment\n"
3837 " ret2 = false;\n"
3838 " } // End of if\n"
3839 "\n"
3840 "// if (ret1) {\n" // Commented out at the beginning of the line
3841 "// return ret2;\n"
3842 "// }\n"
3843 "\n"
3844 " //if (ret1) {\n" // Commtented out at the beginning of the content
3845 " // return ret2;\n"
3846 " //}\n"
3847 "\n"
3848 " return ret1 && ret2;\n"
3849 "}\n"
3850 "}\n"
3851 "\n"
3852 "namespace Bar {\n"
3853 "int foo();\n"
3854 "} // namespace Bar\n"
3855 "//@Nothing added because of the non ascii char\n"
3856 "\n"
3857 "//@ Nothing removed because of the non ascii char\n"
3858 "\n"
3859 "// Comment to move to the left\n"
3860 "//But not this?\n"
3861 "// @but this\n"
3862 "\n"
3863 "//Comment to move to the right\n"
3864 "//@ this stays\n"
3865 "\n"
3866 "//} will not move\n"
3867 "\n"
3868 "//vv will only move\n"
3869 "//} if the line above does";
3871 EXPECT_EQ("// Free comment without space\n"
3872 "\n"
3873 "// Free comment with 3 spaces\n"
3874 "\n"
3875 "/// Free Doxygen without space\n"
3876 "\n"
3877 "/// Free Doxygen with 3 spaces\n"
3878 "\n"
3879 "// 🐉 A nice dragon\n"
3880 "\n"
3881 "//\t abccba\n"
3882 "\n"
3883 "//\\t deffed\n"
3884 "\n"
3885 "// 🐉 Another nice dragon\n"
3886 "\n"
3887 "// \t Three leading spaces following tab\n"
3888 "\n"
3889 "// \\t Three leading spaces following backslash\n"
3890 "\n"
3891 "/// A Doxygen Comment with a nested list:\n"
3892 "/// - Foo\n"
3893 "/// - Bar\n"
3894 "/// - Baz\n"
3895 "/// - End\n"
3896 "/// of the inner list\n"
3897 "/// .\n"
3898 "/// .\n"
3899 "\n"
3900 "namespace Foo {\n"
3901 "bool bar(bool b) {\n"
3902 " bool ret1 = true; ///< Doxygenstyle without space\n"
3903 " bool ret2 = true; ///< Doxygenstyle with 3 spaces\n"
3904 " if (b) {\n"
3905 " // Foo\n"
3906 "\n"
3907 " // In function comment\n"
3908 " ret2 = false;\n"
3909 " } // End of if\n"
3910 "\n"
3911 " // if (ret1) {\n"
3912 " // return ret2;\n"
3913 " // }\n"
3914 "\n"
3915 " // if (ret1) {\n"
3916 " // return ret2;\n"
3917 " // }\n"
3918 "\n"
3919 " return ret1 && ret2;\n"
3920 "}\n"
3921 "} // namespace Foo\n"
3922 "\n"
3923 "namespace Bar {\n"
3924 "int foo();\n"
3925 "} // namespace Bar\n"
3926 "//@Nothing added because of the non ascii char\n"
3927 "\n"
3928 "//@ Nothing removed because of the non ascii char\n"
3929 "\n"
3930 "// Comment to move to the left\n"
3931 "// But not this?\n"
3932 "// @but this\n"
3933 "\n"
3934 "// Comment to move to the right\n"
3935 "//@ this stays\n"
3936 "\n"
3937 "//} will not move\n"
3938 "\n"
3939 "// vv will only move\n"
3940 "// } if the line above does",
3941 format(Code, Style));
3943 Style.SpacesInLineCommentPrefix = {0, 0};
3944 EXPECT_EQ("//#comment", format("// #comment", Style));
3945 EXPECT_EQ("//Free comment without space\n"
3946 "\n"
3947 "//Free comment with 3 spaces\n"
3948 "\n"
3949 "///Free Doxygen without space\n"
3950 "\n"
3951 "///Free Doxygen with 3 spaces\n"
3952 "\n"
3953 "//🐉 A nice dragon\n"
3954 "\n"
3955 "//\t abccba\n"
3956 "\n"
3957 "//\\t deffed\n"
3958 "\n"
3959 "//🐉 Another nice dragon\n"
3960 "\n"
3961 "//\t Three leading spaces following tab\n"
3962 "\n"
3963 "//\\t Three leading spaces following backslash\n"
3964 "\n"
3965 "///A Doxygen Comment with a nested list:\n"
3966 "///- Foo\n"
3967 "///- Bar\n"
3968 "/// - Baz\n" // Here we keep the relative indentation
3969 "/// - End\n"
3970 "/// of the inner list\n"
3971 "/// .\n"
3972 "///.\n"
3973 "\n"
3974 "namespace Foo {\n"
3975 "bool bar(bool b) {\n"
3976 " bool ret1 = true; ///<Doxygenstyle without space\n"
3977 " bool ret2 = true; ///<Doxygenstyle with 3 spaces\n"
3978 " if (b) {\n"
3979 " //Foo\n"
3980 "\n"
3981 " //In function comment\n"
3982 " ret2 = false;\n"
3983 " } //End of if\n"
3984 "\n"
3985 " //if (ret1) {\n"
3986 " // return ret2;\n"
3987 " //}\n"
3988 "\n"
3989 " //if (ret1) {\n"
3990 " // return ret2;\n"
3991 " //}\n"
3992 "\n"
3993 " return ret1 && ret2;\n"
3994 "}\n"
3995 "} //namespace Foo\n"
3996 "\n"
3997 "namespace Bar {\n"
3998 "int foo();\n"
3999 "} //namespace Bar\n"
4000 "//@Nothing added because of the non ascii char\n"
4001 "\n"
4002 "//@ Nothing removed because of the non ascii char\n"
4003 "\n"
4004 "//Comment to move to the left\n"
4005 "//But not this?\n"
4006 "//@but this\n"
4007 "\n"
4008 "//Comment to move to the right\n"
4009 "//@ this stays\n"
4010 "\n"
4011 "//} will not move\n"
4012 "\n"
4013 "//vv will only move\n"
4014 "//} if the line above does",
4015 format(Code, Style));
4017 Style.SpacesInLineCommentPrefix = {2, -1u};
4018 EXPECT_EQ("// Free comment without space\n"
4019 "\n"
4020 "// Free comment with 3 spaces\n"
4021 "\n"
4022 "/// Free Doxygen without space\n"
4023 "\n"
4024 "/// Free Doxygen with 3 spaces\n"
4025 "\n"
4026 "// 🐉 A nice dragon\n"
4027 "\n"
4028 "//\t abccba\n"
4029 "\n"
4030 "//\\t deffed\n"
4031 "\n"
4032 "// 🐉 Another nice dragon\n"
4033 "\n"
4034 "// \t Three leading spaces following tab\n"
4035 "\n"
4036 "// \\t Three leading spaces following backslash\n"
4037 "\n"
4038 "/// A Doxygen Comment with a nested list:\n"
4039 "/// - Foo\n"
4040 "/// - Bar\n"
4041 "/// - Baz\n"
4042 "/// - End\n"
4043 "/// of the inner list\n"
4044 "/// .\n"
4045 "/// .\n"
4046 "\n"
4047 "namespace Foo {\n"
4048 "bool bar(bool b) {\n"
4049 " bool ret1 = true; ///< Doxygenstyle without space\n"
4050 " bool ret2 = true; ///< Doxygenstyle with 3 spaces\n"
4051 " if (b) {\n"
4052 " // Foo\n"
4053 "\n"
4054 " // In function comment\n"
4055 " ret2 = false;\n"
4056 " } // End of if\n"
4057 "\n"
4058 " // if (ret1) {\n"
4059 " // return ret2;\n"
4060 " // }\n"
4061 "\n"
4062 " // if (ret1) {\n"
4063 " // return ret2;\n"
4064 " // }\n"
4065 "\n"
4066 " return ret1 && ret2;\n"
4067 "}\n"
4068 "} // namespace Foo\n"
4069 "\n"
4070 "namespace Bar {\n"
4071 "int foo();\n"
4072 "} // namespace Bar\n"
4073 "//@Nothing added because of the non ascii char\n"
4074 "\n"
4075 "//@ Nothing removed because of the non ascii char\n"
4076 "\n"
4077 "// Comment to move to the left\n"
4078 "// But not this?\n"
4079 "// @but this\n"
4080 "\n"
4081 "// Comment to move to the right\n"
4082 "//@ this stays\n"
4083 "\n"
4084 "//} will not move\n"
4085 "\n"
4086 "// vv will only move\n"
4087 "// } if the line above does",
4088 format(Code, Style));
4090 Style = getLLVMStyleWithColumns(20);
4091 StringRef WrapCode = "//Lorem ipsum dolor sit amet\n"
4092 "\n"
4093 "// Lorem ipsum dolor sit amet\n"
4094 "\n"
4095 "void f() {//Hello World\n"
4096 "}";
4098 EXPECT_EQ("// Lorem ipsum dolor\n"
4099 "// sit amet\n"
4100 "\n"
4101 "// Lorem ipsum\n"
4102 "// dolor sit amet\n"
4103 "\n"
4104 "void f() { // Hello\n"
4105 " // World\n"
4106 "}",
4107 format(WrapCode, Style));
4109 Style.SpacesInLineCommentPrefix = {0, 0};
4110 EXPECT_EQ("//Lorem ipsum dolor\n"
4111 "//sit amet\n"
4112 "\n"
4113 "//Lorem ipsum\n"
4114 "//dolor sit amet\n"
4115 "\n"
4116 "void f() { //Hello\n"
4117 " //World\n"
4118 "}",
4119 format(WrapCode, Style));
4121 Style.SpacesInLineCommentPrefix = {1, 1};
4122 EXPECT_EQ("// Lorem ipsum dolor\n"
4123 "// sit amet\n"
4124 "\n"
4125 "// Lorem ipsum\n"
4126 "// dolor sit amet\n"
4127 "\n"
4128 "void f() { // Hello\n"
4129 " // World\n"
4130 "}",
4131 format(WrapCode, Style));
4132 EXPECT_EQ("// x\n"
4133 "// y",
4134 format("// x\n"
4135 "// y",
4136 Style));
4137 EXPECT_EQ(
4138 "// loooooooooooooooooooooooooooooong\n"
4139 "// commentcomments\n"
4140 "// normal comments",
4141 format("// loooooooooooooooooooooooooooooong commentcomments\n"
4142 "// normal comments",
4143 Style));
4145 Style.SpacesInLineCommentPrefix = {3, 3};
4146 EXPECT_EQ("// Lorem ipsum\n"
4147 "// dolor sit amet\n"
4148 "\n"
4149 "// Lorem ipsum\n"
4150 "// dolor sit\n"
4151 "// amet\n"
4152 "\n"
4153 "void f() { // Hello\n"
4154 " // World\n"
4155 "}",
4156 format(WrapCode, Style));
4158 Style = getLLVMStyleWithColumns(20);
4159 StringRef LotsOfSpaces = "// This are more spaces "
4160 "than the ColumnLimit, what now?\n"
4161 "\n"
4162 "// Comment\n"
4163 "\n"
4164 "// This is a text to split in multiple "
4165 "lines, please. Thank you very much!\n"
4166 "\n"
4167 "// A comment with\n"
4168 "// some indentation that has to be split.\n"
4169 "// And now without";
4170 EXPECT_EQ("// This are more spaces "
4171 "than the ColumnLimit, what now?\n"
4172 "\n"
4173 "// Comment\n"
4174 "\n"
4175 "// This is a text to\n"
4176 "// split in multiple\n"
4177 "// lines, please.\n"
4178 "// Thank you very\n"
4179 "// much!\n"
4180 "\n"
4181 "// A comment with\n"
4182 "// some\n"
4183 "// indentation\n"
4184 "// that has to be\n"
4185 "// split.\n"
4186 "// And now without",
4187 format(LotsOfSpaces, Style));
4189 Style.SpacesInLineCommentPrefix = {0, 0};
4190 EXPECT_EQ("//This are more\n"
4191 "//spaces than the\n"
4192 "//ColumnLimit, what\n"
4193 "//now?\n"
4194 "\n"
4195 "//Comment\n"
4196 "\n"
4197 "//This is a text to\n"
4198 "//split in multiple\n"
4199 "//lines, please.\n"
4200 "//Thank you very\n"
4201 "//much!\n"
4202 "\n"
4203 "//A comment with\n"
4204 "// some indentation\n"
4205 "// that has to be\n"
4206 "// split.\n"
4207 "//And now without",
4208 format(LotsOfSpaces, Style));
4210 Style.SpacesInLineCommentPrefix = {3, 3};
4211 EXPECT_EQ("// This are more\n"
4212 "// spaces than the\n"
4213 "// ColumnLimit,\n"
4214 "// what now?\n"
4215 "\n"
4216 "// Comment\n"
4217 "\n"
4218 "// This is a text\n"
4219 "// to split in\n"
4220 "// multiple lines,\n"
4221 "// please. Thank\n"
4222 "// you very much!\n"
4223 "\n"
4224 "// A comment with\n"
4225 "// some\n"
4226 "// indentation\n"
4227 "// that has to\n"
4228 "// be split.\n"
4229 "// And now without",
4230 format(LotsOfSpaces, Style));
4232 Style.SpacesInLineCommentPrefix = {30, -1u};
4233 EXPECT_EQ("// This are more spaces than the "
4234 "ColumnLimit, what now?\n"
4235 "\n"
4236 "// Comment\n"
4237 "\n"
4238 "// This is a text to split in "
4239 "multiple lines, please. Thank you very much!\n"
4240 "\n"
4241 "// A comment with\n"
4242 "// some indentation that has to be "
4243 "split.\n"
4244 "// And now without",
4245 format(LotsOfSpaces, Style));
4247 Style.SpacesInLineCommentPrefix = {2, 4};
4248 EXPECT_EQ("// A Comment to be\n"
4249 "// moved\n"
4250 "// with indent\n"
4251 "\n"
4252 "// A Comment to be\n"
4253 "// moved\n"
4254 "// with indent\n"
4255 "\n"
4256 "// A Comment to be\n"
4257 "// moved\n"
4258 "// with indent\n"
4259 "\n"
4260 "// A Comment to be\n"
4261 "// moved\n"
4262 "// with indent\n"
4263 "\n"
4264 "// A Comment to\n"
4265 "// be moved\n"
4266 "// with indent\n"
4267 "\n"
4268 "// A Comment to\n"
4269 "// be moved\n"
4270 "// with indent\n"
4271 "\n"
4272 "// A Comment to\n"
4273 "// be moved\n"
4274 "// with indent",
4275 format("//A Comment to be moved\n"
4276 "// with indent\n"
4277 "\n"
4278 "// A Comment to be moved\n"
4279 "// with indent\n"
4280 "\n"
4281 "// A Comment to be moved\n"
4282 "// with indent\n"
4283 "\n"
4284 "// A Comment to be moved\n"
4285 "// with indent\n"
4286 "\n"
4287 "// A Comment to be moved\n"
4288 "// with indent\n"
4289 "\n"
4290 "// A Comment to be moved\n"
4291 "// with indent\n"
4292 "\n"
4293 "// A Comment to be moved\n"
4294 "// with indent",
4295 Style));
4297 Style.ColumnLimit = 30;
4298 EXPECT_EQ("int i; // A Comment to be\n"
4299 " // moved\n"
4300 " // with indent\n"
4301 "\n"
4302 "int i; // A Comment to be\n"
4303 " // moved\n"
4304 " // with indent\n"
4305 "\n"
4306 "int i; // A Comment to be\n"
4307 " // moved\n"
4308 " // with indent\n"
4309 "\n"
4310 "int i; // A Comment to be\n"
4311 " // moved\n"
4312 " // with indent\n"
4313 "\n"
4314 "int i; // A Comment to be\n"
4315 " // moved\n"
4316 " // with indent\n"
4317 "\n"
4318 "int i; // A Comment to be\n"
4319 " // moved\n"
4320 " // with indent\n"
4321 "\n"
4322 "int i; // A Comment to be\n"
4323 " // moved\n"
4324 " // with indent",
4325 format("int i;//A Comment to be moved\n"
4326 " // with indent\n"
4327 "\n"
4328 "int i;// A Comment to be moved\n"
4329 " // with indent\n"
4330 "\n"
4331 "int i;// A Comment to be moved\n"
4332 " // with indent\n"
4333 "\n"
4334 "int i;// A Comment to be moved\n"
4335 " // with indent\n"
4336 "\n"
4337 "int i;// A Comment to be moved\n"
4338 " // with indent\n"
4339 "\n"
4340 "int i;// A Comment to be moved\n"
4341 " // with indent\n"
4342 "\n"
4343 "int i;// A Comment to be moved\n"
4344 " // with indent",
4345 Style));
4347 Style = getLLVMStyleWithColumns(0);
4348 EXPECT_EQ("// Free comment without space\n"
4349 "\n"
4350 "// Free comment with 3 spaces\n"
4351 "\n"
4352 "/// Free Doxygen without space\n"
4353 "\n"
4354 "/// Free Doxygen with 3 spaces\n"
4355 "\n"
4356 "// 🐉 A nice dragon\n"
4357 "\n"
4358 "//\t abccba\n"
4359 "\n"
4360 "//\\t deffed\n"
4361 "\n"
4362 "// 🐉 Another nice dragon\n"
4363 "\n"
4364 "// \t Three leading spaces following tab\n"
4365 "\n"
4366 "// \\t Three leading spaces following backslash\n"
4367 "\n"
4368 "/// A Doxygen Comment with a nested list:\n"
4369 "/// - Foo\n"
4370 "/// - Bar\n"
4371 "/// - Baz\n"
4372 "/// - End\n"
4373 "/// of the inner list\n"
4374 "/// .\n"
4375 "/// .\n"
4376 "\n"
4377 "namespace Foo {\n"
4378 "bool bar(bool b) {\n"
4379 " bool ret1 = true; ///< Doxygenstyle without space\n"
4380 " bool ret2 = true; ///< Doxygenstyle with 3 spaces\n"
4381 " if (b) {\n"
4382 " // Foo\n"
4383 "\n"
4384 " // In function comment\n"
4385 " ret2 = false;\n"
4386 " } // End of if\n"
4387 "\n"
4388 " // if (ret1) {\n"
4389 " // return ret2;\n"
4390 " // }\n"
4391 "\n"
4392 " // if (ret1) {\n"
4393 " // return ret2;\n"
4394 " // }\n"
4395 "\n"
4396 " return ret1 && ret2;\n"
4397 "}\n"
4398 "} // namespace Foo\n"
4399 "\n"
4400 "namespace Bar {\n"
4401 "int foo();\n"
4402 "} // namespace Bar\n"
4403 "//@Nothing added because of the non ascii char\n"
4404 "\n"
4405 "//@ Nothing removed because of the non ascii char\n"
4406 "\n"
4407 "// Comment to move to the left\n"
4408 "// But not this?\n"
4409 "// @but this\n"
4410 "\n"
4411 "// Comment to move to the right\n"
4412 "//@ this stays\n"
4413 "\n"
4414 "//} will not move\n"
4415 "\n"
4416 "// vv will only move\n"
4417 "// } if the line above does",
4418 format(Code, Style));
4420 Style.SpacesInLineCommentPrefix = {0, 0};
4421 EXPECT_EQ("//Free comment without space\n"
4422 "\n"
4423 "//Free comment with 3 spaces\n"
4424 "\n"
4425 "///Free Doxygen without space\n"
4426 "\n"
4427 "///Free Doxygen with 3 spaces\n"
4428 "\n"
4429 "//🐉 A nice dragon\n"
4430 "\n"
4431 "//\t abccba\n"
4432 "\n"
4433 "//\\t deffed\n"
4434 "\n"
4435 "//🐉 Another nice dragon\n"
4436 "\n"
4437 "//\t Three leading spaces following tab\n"
4438 "\n"
4439 "//\\t Three leading spaces following backslash\n"
4440 "\n"
4441 "///A Doxygen Comment with a nested list:\n"
4442 "///- Foo\n"
4443 "///- Bar\n"
4444 "/// - Baz\n" // Here we keep the relative indentation
4445 "/// - End\n"
4446 "/// of the inner list\n"
4447 "/// .\n"
4448 "///.\n"
4449 "\n"
4450 "namespace Foo {\n"
4451 "bool bar(bool b) {\n"
4452 " bool ret1 = true; ///<Doxygenstyle without space\n"
4453 " bool ret2 = true; ///<Doxygenstyle with 3 spaces\n"
4454 " if (b) {\n"
4455 " //Foo\n"
4456 "\n"
4457 " //In function comment\n"
4458 " ret2 = false;\n"
4459 " } //End of if\n"
4460 "\n"
4461 " //if (ret1) {\n"
4462 " // return ret2;\n"
4463 " //}\n"
4464 "\n"
4465 " //if (ret1) {\n"
4466 " // return ret2;\n"
4467 " //}\n"
4468 "\n"
4469 " return ret1 && ret2;\n"
4470 "}\n"
4471 "} //namespace Foo\n"
4472 "\n"
4473 "namespace Bar {\n"
4474 "int foo();\n"
4475 "} //namespace Bar\n"
4476 "//@Nothing added because of the non ascii char\n"
4477 "\n"
4478 "//@ Nothing removed because of the non ascii char\n"
4479 "\n"
4480 "//Comment to move to the left\n"
4481 "//But not this?\n"
4482 "//@but this\n"
4483 "\n"
4484 "//Comment to move to the right\n"
4485 "//@ this stays\n"
4486 "\n"
4487 "//} will not move\n"
4488 "\n"
4489 "//vv will only move\n"
4490 "//} if the line above does",
4491 format(Code, Style));
4493 Style.SpacesInLineCommentPrefix = {2, -1u};
4494 EXPECT_EQ("// Free comment without space\n"
4495 "\n"
4496 "// Free comment with 3 spaces\n"
4497 "\n"
4498 "/// Free Doxygen without space\n"
4499 "\n"
4500 "/// Free Doxygen with 3 spaces\n"
4501 "\n"
4502 "// 🐉 A nice dragon\n"
4503 "\n"
4504 "//\t abccba\n"
4505 "\n"
4506 "//\\t deffed\n"
4507 "\n"
4508 "// 🐉 Another nice dragon\n"
4509 "\n"
4510 "// \t Three leading spaces following tab\n"
4511 "\n"
4512 "// \\t Three leading spaces following backslash\n"
4513 "\n"
4514 "/// A Doxygen Comment with a nested list:\n"
4515 "/// - Foo\n"
4516 "/// - Bar\n"
4517 "/// - Baz\n"
4518 "/// - End\n"
4519 "/// of the inner list\n"
4520 "/// .\n"
4521 "/// .\n"
4522 "\n"
4523 "namespace Foo {\n"
4524 "bool bar(bool b) {\n"
4525 " bool ret1 = true; ///< Doxygenstyle without space\n"
4526 " bool ret2 = true; ///< Doxygenstyle with 3 spaces\n"
4527 " if (b) {\n"
4528 " // Foo\n"
4529 "\n"
4530 " // In function comment\n"
4531 " ret2 = false;\n"
4532 " } // End of if\n"
4533 "\n"
4534 " // if (ret1) {\n"
4535 " // return ret2;\n"
4536 " // }\n"
4537 "\n"
4538 " // if (ret1) {\n"
4539 " // return ret2;\n"
4540 " // }\n"
4541 "\n"
4542 " return ret1 && ret2;\n"
4543 "}\n"
4544 "} // namespace Foo\n"
4545 "\n"
4546 "namespace Bar {\n"
4547 "int foo();\n"
4548 "} // namespace Bar\n"
4549 "//@Nothing added because of the non ascii char\n"
4550 "\n"
4551 "//@ Nothing removed because of the non ascii char\n"
4552 "\n"
4553 "// Comment to move to the left\n"
4554 "// But not this?\n"
4555 "// @but this\n"
4556 "\n"
4557 "// Comment to move to the right\n"
4558 "//@ this stays\n"
4559 "\n"
4560 "//} will not move\n"
4561 "\n"
4562 "// vv will only move\n"
4563 "// } if the line above does",
4564 format(Code, Style));
4567 TEST_F(FormatTestComments, SplitCommentIntroducers) {
4568 EXPECT_EQ(R"(//
4572 format(R"(//
4576 getLLVMStyleWithColumns(10)));
4579 } // end namespace
4580 } // namespace test
4581 } // end namespace format
4582 } // end namespace clang