Debian Codegraph @

llvm-project/clang/unittests/Format/FormatTestComments.cpp

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