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