1 //===- unittest/Format/FormatTestSelective.cpp - Formatting unit tests ----===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "FormatTestUtils.h"
11 #include "clang/Format/Format.h"
12 #include "llvm/Support/Debug.h"
13 #include "gtest/gtest.h"
14
15 #define DEBUG_TYPE "format-test"
16
17 namespace clang {
18 namespace format {
19 namespace {
20
21 class FormatTestSelective : public ::testing::Test {
22 protected:
format(llvm::StringRef Code,unsigned Offset,unsigned Length)23 std::string format(llvm::StringRef Code, unsigned Offset, unsigned Length) {
24 LLVM_DEBUG(llvm::errs() << "---\n");
25 LLVM_DEBUG(llvm::errs() << Code << "\n\n");
26 std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length));
27 FormattingAttemptStatus Status;
28 tooling::Replacements Replaces =
29 reformat(Style, Code, Ranges, "<stdin>", &Status);
30 EXPECT_TRUE(Status.FormatComplete) << Code << "\n\n";
31 auto Result = applyAllReplacements(Code, Replaces);
32 EXPECT_TRUE(static_cast<bool>(Result));
33 LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
34 return *Result;
35 }
36
37 FormatStyle Style = getLLVMStyle();
38 };
39
TEST_F(FormatTestSelective,RemovesTrailingWhitespaceOfFormattedLine)40 TEST_F(FormatTestSelective, RemovesTrailingWhitespaceOfFormattedLine) {
41 EXPECT_EQ("int a;\nint b;", format("int a; \nint b;", 0, 0));
42 EXPECT_EQ("int a;", format("int a; ", 0, 0));
43 EXPECT_EQ("int a;\n", format("int a; \n \n \n ", 0, 0));
44 EXPECT_EQ("int a;\nint b; ", format("int a; \nint b; ", 0, 0));
45 }
46
TEST_F(FormatTestSelective,FormatsCorrectRegionForLeadingWhitespace)47 TEST_F(FormatTestSelective, FormatsCorrectRegionForLeadingWhitespace) {
48 EXPECT_EQ("{int b;\n"
49 " int a;\n"
50 "}",
51 format("{int b;\n int a;}", 8, 0));
52 EXPECT_EQ("{\n"
53 " int b;\n"
54 " int a;}",
55 format("{int b;\n int a;}", 7, 0));
56
57 Style.ColumnLimit = 12;
58 EXPECT_EQ("#define A \\\n"
59 " int a; \\\n"
60 " int b;",
61 format("#define A \\\n"
62 " int a; \\\n"
63 " int b;",
64 26, 0));
65 EXPECT_EQ("#define A \\\n"
66 " int a; \\\n"
67 " int b;",
68 format("#define A \\\n"
69 " int a; \\\n"
70 " int b;",
71 25, 0));
72 }
73
TEST_F(FormatTestSelective,FormatLineWhenInvokedOnTrailingNewline)74 TEST_F(FormatTestSelective, FormatLineWhenInvokedOnTrailingNewline) {
75 EXPECT_EQ("int b;\n\nint a;", format("int b;\n\nint a;", 8, 0));
76 EXPECT_EQ("int b;\n\nint a;", format("int b;\n\nint a;", 7, 0));
77
78 // This might not strictly be correct, but is likely good in all practical
79 // cases.
80 EXPECT_EQ("int b;\nint a;", format("int b;int a;", 7, 0));
81 }
82
TEST_F(FormatTestSelective,RemovesWhitespaceWhenTriggeredOnEmptyLine)83 TEST_F(FormatTestSelective, RemovesWhitespaceWhenTriggeredOnEmptyLine) {
84 EXPECT_EQ("int a;\n\n int b;", format("int a;\n \n\n int b;", 8, 0));
85 EXPECT_EQ("int a;\n\n int b;", format("int a;\n \n\n int b;", 9, 0));
86 }
87
TEST_F(FormatTestSelective,ReformatsMovedLines)88 TEST_F(FormatTestSelective, ReformatsMovedLines) {
89 EXPECT_EQ(
90 "template <typename T> T *getFETokenInfo() const {\n"
91 " return static_cast<T *>(FETokenInfo);\n"
92 "}\n"
93 "int a; // <- Should not be formatted",
94 format(
95 "template<typename T>\n"
96 "T *getFETokenInfo() const { return static_cast<T*>(FETokenInfo); }\n"
97 "int a; // <- Should not be formatted",
98 9, 5));
99 }
100
TEST_F(FormatTestSelective,FormatsIfWithoutCompoundStatement)101 TEST_F(FormatTestSelective, FormatsIfWithoutCompoundStatement) {
102 Style.AllowShortIfStatementsOnASingleLine = true;
103 EXPECT_EQ("if (a) return;", format("if(a)\nreturn;", 7, 1));
104 EXPECT_EQ("if (a) return; // comment",
105 format("if(a)\nreturn; // comment", 20, 1));
106 }
107
TEST_F(FormatTestSelective,FormatsCommentsLocally)108 TEST_F(FormatTestSelective, FormatsCommentsLocally) {
109 EXPECT_EQ("int a; // comment\n"
110 "int b; // comment",
111 format("int a; // comment\n"
112 "int b; // comment",
113 0, 0));
114 EXPECT_EQ("int a; // comment\n"
115 " // line 2\n"
116 "int b;",
117 format("int a; // comment\n"
118 " // line 2\n"
119 "int b;",
120 28, 0));
121 EXPECT_EQ("int a; // comment\n"
122 "// comment 2\n"
123 "int b;",
124 format("int a; // comment\n"
125 "// comment 2\n"
126 "int b;", 28, 0));
127 EXPECT_EQ("int aaaaaa; // comment\n"
128 "int b;\n"
129 "int c; // unrelated comment",
130 format("int aaaaaa; // comment\n"
131 "int b;\n"
132 "int c; // unrelated comment",
133 31, 0));
134
135 EXPECT_EQ("int a; // This\n"
136 " // is\n"
137 " // a",
138 format("int a; // This\n"
139 " // is\n"
140 " // a",
141 0, 0));
142 EXPECT_EQ("int a; // This\n"
143 " // is\n"
144 " // a\n"
145 "// This is b\n"
146 "int b;",
147 format("int a; // This\n"
148 " // is\n"
149 " // a\n"
150 "// This is b\n"
151 "int b;",
152 0, 0));
153 EXPECT_EQ("int a; // This\n"
154 " // is\n"
155 " // a\n"
156 "\n"
157 "//This is unrelated",
158 format("int a; // This\n"
159 " // is\n"
160 " // a\n"
161 "\n"
162 "//This is unrelated",
163 0, 0));
164 EXPECT_EQ("int a;\n"
165 "// This is\n"
166 "// not formatted. ",
167 format("int a;\n"
168 "// This is\n"
169 "// not formatted. ",
170 0, 0));
171 EXPECT_EQ("int x; // Format this line.\n"
172 "int xx; //\n"
173 "int xxxxx; //",
174 format("int x; // Format this line.\n"
175 "int xx; //\n"
176 "int xxxxx; //",
177 0, 0));
178 }
179
TEST_F(FormatTestSelective,ContinueReindenting)180 TEST_F(FormatTestSelective, ContinueReindenting) {
181 // When we change an indent, we continue formatting as long as following
182 // lines are not indented correctly.
183 EXPECT_EQ("int i;\n"
184 "int b;\n"
185 "int c;\n"
186 "int d;\n"
187 "int e;\n"
188 " int f;\n",
189 format("int i;\n"
190 " int b;\n"
191 " int c;\n"
192 " int d;\n"
193 "int e;\n"
194 " int f;\n",
195 11, 0));
196 }
197
TEST_F(FormatTestSelective,ReindentClosingBrace)198 TEST_F(FormatTestSelective, ReindentClosingBrace) {
199 EXPECT_EQ("int i;\n"
200 "int f() {\n"
201 " int a;\n"
202 " int b;\n"
203 "}\n"
204 " int c;\n",
205 format("int i;\n"
206 " int f(){\n"
207 "int a;\n"
208 "int b;\n"
209 " }\n"
210 " int c;\n",
211 11, 0));
212 EXPECT_EQ("void f() {\n"
213 " if (foo) {\n"
214 " b();\n"
215 " } else {\n"
216 " c();\n"
217 " }\n"
218 "int d;\n"
219 "}\n",
220 format("void f() {\n"
221 " if (foo) {\n"
222 "b();\n"
223 "}else{\n"
224 "c();\n"
225 "}\n"
226 "int d;\n"
227 "}\n",
228 13, 0));
229 EXPECT_EQ("int i = []() {\n"
230 " class C {\n"
231 " int a;\n"
232 " int b;\n"
233 " };\n"
234 " int c;\n"
235 "};\n",
236 format("int i = []() {\n"
237 " class C{\n"
238 "int a;\n"
239 "int b;\n"
240 "};\n"
241 "int c;\n"
242 " };\n",
243 17, 0));
244 }
245
TEST_F(FormatTestSelective,IndividualStatementsOfNestedBlocks)246 TEST_F(FormatTestSelective, IndividualStatementsOfNestedBlocks) {
247 EXPECT_EQ("DEBUG({\n"
248 " int i;\n"
249 " int j;\n"
250 "});",
251 format("DEBUG( {\n"
252 " int i;\n"
253 " int j;\n"
254 "} ) ;",
255 20, 1));
256 EXPECT_EQ("DEBUG( {\n"
257 " int i;\n"
258 " int j;\n"
259 "} ) ;",
260 format("DEBUG( {\n"
261 " int i;\n"
262 " int j;\n"
263 "} ) ;",
264 41, 1));
265 EXPECT_EQ("DEBUG( {\n"
266 " int i;\n"
267 " int j;\n"
268 "} ) ;",
269 format("DEBUG( {\n"
270 " int i;\n"
271 " int j;\n"
272 "} ) ;",
273 41, 1));
274 EXPECT_EQ("DEBUG({\n"
275 " int i;\n"
276 " int j;\n"
277 "});",
278 format("DEBUG( {\n"
279 " int i;\n"
280 " int j;\n"
281 "} ) ;",
282 20, 1));
283
284 EXPECT_EQ("Debug({\n"
285 " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n"
286 " return;\n"
287 " },\n"
288 " a);",
289 format("Debug({\n"
290 " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n"
291 " return;\n"
292 " },\n"
293 " a);",
294 50, 1));
295 EXPECT_EQ("DEBUG({\n"
296 " DEBUG({\n"
297 " int a;\n"
298 " int b;\n"
299 " }) ;\n"
300 "});",
301 format("DEBUG({\n"
302 " DEBUG({\n"
303 " int a;\n"
304 " int b;\n" // Format this line only.
305 " }) ;\n" // Don't touch this line.
306 "});",
307 35, 0));
308 EXPECT_EQ("DEBUG({\n"
309 " int a; //\n"
310 "});",
311 format("DEBUG({\n"
312 " int a; //\n"
313 "});",
314 0, 0));
315 EXPECT_EQ("someFunction(\n"
316 " [] {\n"
317 " // Only with this comment.\n"
318 " int i; // invoke formatting here.\n"
319 " }, // force line break\n"
320 " aaa);",
321 format("someFunction(\n"
322 " [] {\n"
323 " // Only with this comment.\n"
324 " int i; // invoke formatting here.\n"
325 " }, // force line break\n"
326 " aaa);",
327 63, 1));
328
329 EXPECT_EQ("int longlongname; // comment\n"
330 "int x = f({\n"
331 " int x; // comment\n"
332 " int y; // comment\n"
333 "});",
334 format("int longlongname; // comment\n"
335 "int x = f({\n"
336 " int x; // comment\n"
337 " int y; // comment\n"
338 "});",
339 65, 0));
340 EXPECT_EQ("int s = f({\n"
341 " class X {\n"
342 " public:\n"
343 " void f();\n"
344 " };\n"
345 "});",
346 format("int s = f({\n"
347 " class X {\n"
348 " public:\n"
349 " void f();\n"
350 " };\n"
351 "});",
352 0, 0));
353 EXPECT_EQ("SomeFunction(\n"
354 " [] {\n"
355 " int i;\n"
356 " return i;\n" // Format this line.
357 " },\n"
358 " [] {\n"
359 " return 2;\n" // Don't fix this.
360 " });",
361 format("SomeFunction(\n"
362 " [] {\n"
363 " int i;\n"
364 " return i;\n" // Format this line.
365 " },\n"
366 " [] {\n"
367 " return 2;\n" // Don't fix this.
368 " });",
369 40, 0));
370 }
371
TEST_F(FormatTestSelective,WrongIndent)372 TEST_F(FormatTestSelective, WrongIndent) {
373 EXPECT_EQ("namespace {\n"
374 "int i;\n"
375 "int j;\n"
376 "}",
377 format("namespace {\n"
378 " int i;\n" // Format here.
379 " int j;\n"
380 "}",
381 15, 0));
382 EXPECT_EQ("namespace {\n"
383 " int i;\n"
384 " int j;\n"
385 "}",
386 format("namespace {\n"
387 " int i;\n"
388 " int j;\n" // Format here.
389 "}",
390 24, 0));
391 }
392
TEST_F(FormatTestSelective,AlwaysFormatsEntireMacroDefinitions)393 TEST_F(FormatTestSelective, AlwaysFormatsEntireMacroDefinitions) {
394 Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
395 EXPECT_EQ("int i;\n"
396 "#define A \\\n"
397 " int i; \\\n"
398 " int j\n"
399 "int k;",
400 format("int i;\n"
401 "#define A \\\n"
402 " int i ; \\\n"
403 " int j\n"
404 "int k;",
405 8, 0)); // 8: position of "#define".
406 EXPECT_EQ("int i;\n"
407 "#define A \\\n"
408 " int i; \\\n"
409 " int j\n"
410 "int k;",
411 format("int i;\n"
412 "#define A \\\n"
413 " int i ; \\\n"
414 " int j\n"
415 "int k;",
416 45, 0)); // 45: position of "j".
417 }
418
TEST_F(FormatTestSelective,ReformatRegionAdjustsIndent)419 TEST_F(FormatTestSelective, ReformatRegionAdjustsIndent) {
420 EXPECT_EQ("{\n"
421 "{\n"
422 "a;\n"
423 "b;\n"
424 "}\n"
425 "}",
426 format("{\n"
427 "{\n"
428 "a;\n"
429 " b;\n"
430 "}\n"
431 "}",
432 13, 2));
433 EXPECT_EQ("{\n"
434 "{\n"
435 " a;\n"
436 " b;\n"
437 " c;\n"
438 " d;\n"
439 "}\n"
440 "}",
441 format("{\n"
442 "{\n"
443 " a;\n"
444 " b;\n"
445 " c;\n"
446 " d;\n"
447 "}\n"
448 "}",
449 9, 2));
450 EXPECT_EQ("{\n"
451 "{\n"
452 "public:\n"
453 " b;\n"
454 "}\n"
455 "}",
456 format("{\n"
457 "{\n"
458 "public:\n"
459 " b;\n"
460 "}\n"
461 "}",
462 17, 2));
463 EXPECT_EQ("{\n"
464 "{\n"
465 "a;\n"
466 "}\n"
467 "{\n"
468 " b; //\n"
469 "}\n"
470 "}",
471 format("{\n"
472 "{\n"
473 "a;\n"
474 "}\n"
475 "{\n"
476 " b; //\n"
477 "}\n"
478 "}",
479 22, 2));
480 EXPECT_EQ(" {\n"
481 " a; //\n"
482 " }",
483 format(" {\n"
484 "a; //\n"
485 " }",
486 4, 2));
487 EXPECT_EQ("void f() {}\n"
488 "void g() {}",
489 format("void f() {}\n"
490 "void g() {}",
491 13, 0));
492 EXPECT_EQ("int a; // comment\n"
493 " // line 2\n"
494 "int b;",
495 format("int a; // comment\n"
496 " // line 2\n"
497 " int b;",
498 35, 0));
499
500 EXPECT_EQ(" void f() {\n"
501 "#define A 1\n"
502 " }",
503 format(" void f() {\n"
504 " #define A 1\n" // Format this line.
505 " }",
506 20, 0));
507 EXPECT_EQ(" void f() {\n"
508 " int i;\n"
509 "#define A \\\n"
510 " int i; \\\n"
511 " int j;\n"
512 " int k;\n"
513 " }",
514 format(" void f() {\n"
515 " int i;\n"
516 "#define A \\\n"
517 " int i; \\\n"
518 " int j;\n"
519 " int k;\n" // Format this line.
520 " }",
521 67, 0));
522
523 Style.ColumnLimit = 11;
524 EXPECT_EQ(" int a;\n"
525 " void\n"
526 " ffffff() {\n"
527 " }",
528 format(" int a;\n"
529 "void ffffff() {}",
530 11, 0));
531 }
532
TEST_F(FormatTestSelective,UnderstandsTabs)533 TEST_F(FormatTestSelective, UnderstandsTabs) {
534 Style.IndentWidth = 8;
535 Style.UseTab = FormatStyle::UT_Always;
536 Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
537 EXPECT_EQ("void f() {\n"
538 "\tf();\n"
539 "\tg();\n"
540 "}",
541 format("void f() {\n"
542 "\tf();\n"
543 "\tg();\n"
544 "}",
545 0, 0));
546 EXPECT_EQ("void f() {\n"
547 "\tf();\n"
548 "\tg();\n"
549 "}",
550 format("void f() {\n"
551 "\tf();\n"
552 "\tg();\n"
553 "}",
554 16, 0));
555 EXPECT_EQ("void f() {\n"
556 " \tf();\n"
557 "\tg();\n"
558 "}",
559 format("void f() {\n"
560 " \tf();\n"
561 " \tg();\n"
562 "}",
563 21, 0));
564 }
565
TEST_F(FormatTestSelective,StopFormattingWhenLeavingScope)566 TEST_F(FormatTestSelective, StopFormattingWhenLeavingScope) {
567 EXPECT_EQ(
568 "void f() {\n"
569 " if (a) {\n"
570 " g();\n"
571 " h();\n"
572 " }\n"
573 "\n"
574 "void g() {\n"
575 "}",
576 format("void f() {\n"
577 " if (a) {\n" // Assume this was added without the closing brace.
578 " g();\n"
579 " h();\n"
580 "}\n"
581 "\n"
582 "void g() {\n" // Make sure not to format this.
583 "}",
584 15, 0));
585 }
586
TEST_F(FormatTestSelective,SelectivelyRequoteJavaScript)587 TEST_F(FormatTestSelective, SelectivelyRequoteJavaScript) {
588 Style = getGoogleStyle(FormatStyle::LK_JavaScript);
589 EXPECT_EQ(
590 "var x = \"a\";\n"
591 "var x = 'a';\n"
592 "var x = \"a\";",
593 format("var x = \"a\";\n"
594 "var x = \"a\";\n"
595 "var x = \"a\";",
596 20, 0));
597 }
598
TEST_F(FormatTestSelective,KeepsIndentAfterCommentSectionImport)599 TEST_F(FormatTestSelective, KeepsIndentAfterCommentSectionImport) {
600 std::string Code = "#include <a> // line 1\n" // 23 chars long
601 " // line 2\n" // 23 chars long
602 "\n" // this newline is char 47
603 "int i;"; // this line is not indented
604 EXPECT_EQ(Code, format(Code, 47, 1));
605 }
606
607 } // end namespace
608 } // end namespace format
609 } // end namespace clang
610