1 //===- unittest/Format/FormatTest.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
getGoogleStyle()20 FormatStyle getGoogleStyle() {
21 return getGoogleStyle(FormatStyle::LK_Cpp);
22 }
23
24 class FormatTest : public ::testing::Test {
25 protected:
format(llvm::StringRef Code,unsigned Offset,unsigned Length,const FormatStyle & Style)26 std::string format(llvm::StringRef Code, unsigned Offset, unsigned Length,
27 const FormatStyle &Style) {
28 DEBUG(llvm::errs() << "---\n");
29 DEBUG(llvm::errs() << Code << "\n\n");
30 std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length));
31 tooling::Replacements Replaces = reformat(Style, Code, Ranges);
32 ReplacementCount = Replaces.size();
33 std::string Result = applyAllReplacements(Code, Replaces);
34 EXPECT_NE("", Result);
35 DEBUG(llvm::errs() << "\n" << Result << "\n\n");
36 return Result;
37 }
38
39 std::string
format(llvm::StringRef Code,const FormatStyle & Style=getLLVMStyle ())40 format(llvm::StringRef Code, const FormatStyle &Style = getLLVMStyle()) {
41 return format(Code, 0, Code.size(), Style);
42 }
43
getLLVMStyleWithColumns(unsigned ColumnLimit)44 FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
45 FormatStyle Style = getLLVMStyle();
46 Style.ColumnLimit = ColumnLimit;
47 return Style;
48 }
49
getGoogleStyleWithColumns(unsigned ColumnLimit)50 FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) {
51 FormatStyle Style = getGoogleStyle();
52 Style.ColumnLimit = ColumnLimit;
53 return Style;
54 }
55
verifyFormat(llvm::StringRef Code,const FormatStyle & Style=getLLVMStyle ())56 void verifyFormat(llvm::StringRef Code,
57 const FormatStyle &Style = getLLVMStyle()) {
58 EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
59 }
60
verifyGoogleFormat(llvm::StringRef Code)61 void verifyGoogleFormat(llvm::StringRef Code) {
62 verifyFormat(Code, getGoogleStyle());
63 }
64
verifyIndependentOfContext(llvm::StringRef text)65 void verifyIndependentOfContext(llvm::StringRef text) {
66 verifyFormat(text);
67 verifyFormat(llvm::Twine("void f() { " + text + " }").str());
68 }
69
70 int ReplacementCount;
71 };
72
TEST_F(FormatTest,MessUp)73 TEST_F(FormatTest, MessUp) {
74 EXPECT_EQ("1 2 3", test::messUp("1 2 3"));
75 EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n"));
76 EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc"));
77 EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc"));
78 EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne"));
79 }
80
81 //===----------------------------------------------------------------------===//
82 // Basic function tests.
83 //===----------------------------------------------------------------------===//
84
TEST_F(FormatTest,DoesNotChangeCorrectlyFormattedCode)85 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) {
86 EXPECT_EQ(";", format(";"));
87 }
88
TEST_F(FormatTest,FormatsGlobalStatementsAt0)89 TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
90 EXPECT_EQ("int i;", format(" int i;"));
91 EXPECT_EQ("\nint i;", format(" \n\t \v \f int i;"));
92 EXPECT_EQ("int i;\nint j;", format(" int i; int j;"));
93 EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;"));
94 }
95
TEST_F(FormatTest,FormatsUnwrappedLinesAtFirstFormat)96 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
97 EXPECT_EQ("int i;", format("int\ni;"));
98 }
99
TEST_F(FormatTest,FormatsNestedBlockStatements)100 TEST_F(FormatTest, FormatsNestedBlockStatements) {
101 EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}"));
102 }
103
TEST_F(FormatTest,FormatsNestedCall)104 TEST_F(FormatTest, FormatsNestedCall) {
105 verifyFormat("Method(f1, f2(f3));");
106 verifyFormat("Method(f1(f2, f3()));");
107 verifyFormat("Method(f1(f2, (f3())));");
108 }
109
TEST_F(FormatTest,NestedNameSpecifiers)110 TEST_F(FormatTest, NestedNameSpecifiers) {
111 verifyFormat("vector<::Type> v;");
112 verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())");
113 verifyFormat("static constexpr bool Bar = decltype(bar())::value;");
114 }
115
TEST_F(FormatTest,OnlyGeneratesNecessaryReplacements)116 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) {
117 EXPECT_EQ("if (a) {\n"
118 " f();\n"
119 "}",
120 format("if(a){f();}"));
121 EXPECT_EQ(4, ReplacementCount);
122 EXPECT_EQ("if (a) {\n"
123 " f();\n"
124 "}",
125 format("if (a) {\n"
126 " f();\n"
127 "}"));
128 EXPECT_EQ(0, ReplacementCount);
129 }
130
TEST_F(FormatTest,RemovesTrailingWhitespaceOfFormattedLine)131 TEST_F(FormatTest, RemovesTrailingWhitespaceOfFormattedLine) {
132 EXPECT_EQ("int a;\nint b;", format("int a; \nint b;", 0, 0, getLLVMStyle()));
133 EXPECT_EQ("int a;", format("int a; "));
134 EXPECT_EQ("int a;\n", format("int a; \n \n \n "));
135 EXPECT_EQ("int a;\nint b; ",
136 format("int a; \nint b; ", 0, 0, getLLVMStyle()));
137 }
138
TEST_F(FormatTest,FormatsCorrectRegionForLeadingWhitespace)139 TEST_F(FormatTest, FormatsCorrectRegionForLeadingWhitespace) {
140 EXPECT_EQ("int b;\nint a;",
141 format("int b;\n int a;", 7, 0, getLLVMStyle()));
142 EXPECT_EQ("int b;\n int a;",
143 format("int b;\n int a;", 6, 0, getLLVMStyle()));
144
145 EXPECT_EQ("#define A \\\n"
146 " int a; \\\n"
147 " int b;",
148 format("#define A \\\n"
149 " int a; \\\n"
150 " int b;",
151 26, 0, getLLVMStyleWithColumns(12)));
152 EXPECT_EQ("#define A \\\n"
153 " int a; \\\n"
154 " int b;",
155 format("#define A \\\n"
156 " int a; \\\n"
157 " int b;",
158 25, 0, getLLVMStyleWithColumns(12)));
159 }
160
TEST_F(FormatTest,RemovesWhitespaceWhenTriggeredOnEmptyLine)161 TEST_F(FormatTest, RemovesWhitespaceWhenTriggeredOnEmptyLine) {
162 EXPECT_EQ("int a;\n\n int b;",
163 format("int a;\n \n\n int b;", 7, 0, getLLVMStyle()));
164 EXPECT_EQ("int a;\n\n int b;",
165 format("int a;\n \n\n int b;", 9, 0, getLLVMStyle()));
166 }
167
TEST_F(FormatTest,RemovesEmptyLines)168 TEST_F(FormatTest, RemovesEmptyLines) {
169 EXPECT_EQ("class C {\n"
170 " int i;\n"
171 "};",
172 format("class C {\n"
173 " int i;\n"
174 "\n"
175 "};"));
176
177 // Don't remove empty lines at the start of namespaces.
178 EXPECT_EQ("namespace N {\n"
179 "\n"
180 "int i;\n"
181 "}",
182 format("namespace N {\n"
183 "\n"
184 "int i;\n"
185 "}",
186 getGoogleStyle()));
187
188 // Remove empty lines at the beginning and end of blocks.
189 EXPECT_EQ("void f() {\n"
190 "\n"
191 " if (a) {\n"
192 "\n"
193 " f();\n"
194 " }\n"
195 "}",
196 format("void f() {\n"
197 "\n"
198 " if (a) {\n"
199 "\n"
200 " f();\n"
201 "\n"
202 " }\n"
203 "\n"
204 "}",
205 getLLVMStyle()));
206 EXPECT_EQ("void f() {\n"
207 " if (a) {\n"
208 " f();\n"
209 " }\n"
210 "}",
211 format("void f() {\n"
212 "\n"
213 " if (a) {\n"
214 "\n"
215 " f();\n"
216 "\n"
217 " }\n"
218 "\n"
219 "}",
220 getGoogleStyle()));
221
222 // Don't remove empty lines in more complex control statements.
223 EXPECT_EQ("void f() {\n"
224 " if (a) {\n"
225 " f();\n"
226 "\n"
227 " } else if (b) {\n"
228 " f();\n"
229 " }\n"
230 "}",
231 format("void f() {\n"
232 " if (a) {\n"
233 " f();\n"
234 "\n"
235 " } else if (b) {\n"
236 " f();\n"
237 "\n"
238 " }\n"
239 "\n"
240 "}"));
241
242 // FIXME: This is slightly inconsistent.
243 EXPECT_EQ("namespace {\n"
244 "int i;\n"
245 "}",
246 format("namespace {\n"
247 "int i;\n"
248 "\n"
249 "}"));
250 EXPECT_EQ("namespace {\n"
251 "int i;\n"
252 "\n"
253 "} // namespace",
254 format("namespace {\n"
255 "int i;\n"
256 "\n"
257 "} // namespace"));
258 }
259
TEST_F(FormatTest,ReformatsMovedLines)260 TEST_F(FormatTest, ReformatsMovedLines) {
261 EXPECT_EQ(
262 "template <typename T> T *getFETokenInfo() const {\n"
263 " return static_cast<T *>(FETokenInfo);\n"
264 "}\n"
265 " int a; // <- Should not be formatted",
266 format(
267 "template<typename T>\n"
268 "T *getFETokenInfo() const { return static_cast<T*>(FETokenInfo); }\n"
269 " int a; // <- Should not be formatted",
270 9, 5, getLLVMStyle()));
271 }
272
TEST_F(FormatTest,RecognizesBinaryOperatorKeywords)273 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) {
274 verifyFormat("x = (a) and (b);");
275 verifyFormat("x = (a) or (b);");
276 verifyFormat("x = (a) bitand (b);");
277 verifyFormat("x = (a) bitor (b);");
278 verifyFormat("x = (a) not_eq (b);");
279 verifyFormat("x = (a) and_eq (b);");
280 verifyFormat("x = (a) or_eq (b);");
281 verifyFormat("x = (a) xor (b);");
282 }
283
284 //===----------------------------------------------------------------------===//
285 // Tests for control statements.
286 //===----------------------------------------------------------------------===//
287
TEST_F(FormatTest,FormatIfWithoutCompoundStatement)288 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
289 verifyFormat("if (true)\n f();\ng();");
290 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();");
291 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();");
292
293 FormatStyle AllowsMergedIf = getLLVMStyle();
294 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
295 verifyFormat("if (a)\n"
296 " // comment\n"
297 " f();",
298 AllowsMergedIf);
299 verifyFormat("if (a)\n"
300 " ;",
301 AllowsMergedIf);
302 verifyFormat("if (a)\n"
303 " if (b) return;",
304 AllowsMergedIf);
305
306 verifyFormat("if (a) // Can't merge this\n"
307 " f();\n",
308 AllowsMergedIf);
309 verifyFormat("if (a) /* still don't merge */\n"
310 " f();",
311 AllowsMergedIf);
312 verifyFormat("if (a) { // Never merge this\n"
313 " f();\n"
314 "}",
315 AllowsMergedIf);
316 verifyFormat("if (a) {/* Never merge this */\n"
317 " f();\n"
318 "}",
319 AllowsMergedIf);
320
321 EXPECT_EQ("if (a) return;", format("if(a)\nreturn;", 7, 1, AllowsMergedIf));
322 EXPECT_EQ("if (a) return; // comment",
323 format("if(a)\nreturn; // comment", 20, 1, AllowsMergedIf));
324
325 AllowsMergedIf.ColumnLimit = 14;
326 verifyFormat("if (a) return;", AllowsMergedIf);
327 verifyFormat("if (aaaaaaaaa)\n"
328 " return;",
329 AllowsMergedIf);
330
331 AllowsMergedIf.ColumnLimit = 13;
332 verifyFormat("if (a)\n return;", AllowsMergedIf);
333 }
334
TEST_F(FormatTest,FormatLoopsWithoutCompoundStatement)335 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
336 FormatStyle AllowsMergedLoops = getLLVMStyle();
337 AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
338 verifyFormat("while (true) continue;", AllowsMergedLoops);
339 verifyFormat("for (;;) continue;", AllowsMergedLoops);
340 verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
341 verifyFormat("while (true)\n"
342 " ;",
343 AllowsMergedLoops);
344 verifyFormat("for (;;)\n"
345 " ;",
346 AllowsMergedLoops);
347 verifyFormat("for (;;)\n"
348 " for (;;) continue;",
349 AllowsMergedLoops);
350 verifyFormat("for (;;) // Can't merge this\n"
351 " continue;",
352 AllowsMergedLoops);
353 verifyFormat("for (;;) /* still don't merge */\n"
354 " continue;",
355 AllowsMergedLoops);
356 }
357
TEST_F(FormatTest,FormatShortBracedStatements)358 TEST_F(FormatTest, FormatShortBracedStatements) {
359 FormatStyle AllowSimpleBracedStatements = getLLVMStyle();
360 AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true;
361
362 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true;
363 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
364
365 verifyFormat("if (true) {}", AllowSimpleBracedStatements);
366 verifyFormat("while (true) {}", AllowSimpleBracedStatements);
367 verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
368 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
369 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
370 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
371 verifyFormat("if (true) { //\n"
372 " f();\n"
373 "}",
374 AllowSimpleBracedStatements);
375 verifyFormat("if (true) {\n"
376 " f();\n"
377 " f();\n"
378 "}",
379 AllowSimpleBracedStatements);
380
381 verifyFormat("template <int> struct A2 {\n"
382 " struct B {};\n"
383 "};",
384 AllowSimpleBracedStatements);
385
386 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false;
387 verifyFormat("if (true) {\n"
388 " f();\n"
389 "}",
390 AllowSimpleBracedStatements);
391
392 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
393 verifyFormat("while (true) {\n"
394 " f();\n"
395 "}",
396 AllowSimpleBracedStatements);
397 verifyFormat("for (;;) {\n"
398 " f();\n"
399 "}",
400 AllowSimpleBracedStatements);
401 }
402
TEST_F(FormatTest,ParseIfElse)403 TEST_F(FormatTest, ParseIfElse) {
404 verifyFormat("if (true)\n"
405 " if (true)\n"
406 " if (true)\n"
407 " f();\n"
408 " else\n"
409 " g();\n"
410 " else\n"
411 " h();\n"
412 "else\n"
413 " i();");
414 verifyFormat("if (true)\n"
415 " if (true)\n"
416 " if (true) {\n"
417 " if (true)\n"
418 " f();\n"
419 " } else {\n"
420 " g();\n"
421 " }\n"
422 " else\n"
423 " h();\n"
424 "else {\n"
425 " i();\n"
426 "}");
427 verifyFormat("void f() {\n"
428 " if (a) {\n"
429 " } else {\n"
430 " }\n"
431 "}");
432 }
433
TEST_F(FormatTest,ElseIf)434 TEST_F(FormatTest, ElseIf) {
435 verifyFormat("if (a) {\n} else if (b) {\n}");
436 verifyFormat("if (a)\n"
437 " f();\n"
438 "else if (b)\n"
439 " g();\n"
440 "else\n"
441 " h();");
442 verifyFormat("if (a) {\n"
443 " f();\n"
444 "}\n"
445 "// or else ..\n"
446 "else {\n"
447 " g()\n"
448 "}");
449
450 verifyFormat("if (a) {\n"
451 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
452 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
453 "}");
454 }
455
TEST_F(FormatTest,FormatsForLoop)456 TEST_F(FormatTest, FormatsForLoop) {
457 verifyFormat(
458 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
459 " ++VeryVeryLongLoopVariable)\n"
460 " ;");
461 verifyFormat("for (;;)\n"
462 " f();");
463 verifyFormat("for (;;) {\n}");
464 verifyFormat("for (;;) {\n"
465 " f();\n"
466 "}");
467 verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
468
469 verifyFormat(
470 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
471 " E = UnwrappedLines.end();\n"
472 " I != E; ++I) {\n}");
473
474 verifyFormat(
475 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
476 " ++IIIII) {\n}");
477 verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
478 " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
479 " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
480 verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
481 " I = FD->getDeclsInPrototypeScope().begin(),\n"
482 " E = FD->getDeclsInPrototypeScope().end();\n"
483 " I != E; ++I) {\n}");
484
485 // FIXME: Not sure whether we want extra identation in line 3 here:
486 verifyFormat(
487 "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
488 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
489 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
490 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
491 " ++aaaaaaaaaaa) {\n}");
492 verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
493 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
494 "}");
495 verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
496 " aaaaaaaaaa);\n"
497 " iter; ++iter) {\n"
498 "}");
499
500 FormatStyle NoBinPacking = getLLVMStyle();
501 NoBinPacking.BinPackParameters = false;
502 verifyFormat("for (int aaaaaaaaaaa = 1;\n"
503 " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
504 " aaaaaaaaaaaaaaaa,\n"
505 " aaaaaaaaaaaaaaaa,\n"
506 " aaaaaaaaaaaaaaaa);\n"
507 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
508 "}",
509 NoBinPacking);
510 verifyFormat(
511 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
512 " E = UnwrappedLines.end();\n"
513 " I != E;\n"
514 " ++I) {\n}",
515 NoBinPacking);
516 }
517
TEST_F(FormatTest,RangeBasedForLoops)518 TEST_F(FormatTest, RangeBasedForLoops) {
519 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
520 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
521 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
522 " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
523 verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
524 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
525 verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
526 " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
527 }
528
TEST_F(FormatTest,ForEachLoops)529 TEST_F(FormatTest, ForEachLoops) {
530 verifyFormat("void f() {\n"
531 " foreach (Item *item, itemlist) {}\n"
532 " Q_FOREACH (Item *item, itemlist) {}\n"
533 " BOOST_FOREACH (Item *item, itemlist) {}\n"
534 " UNKNOWN_FORACH(Item * item, itemlist) {}\n"
535 "}");
536 }
537
TEST_F(FormatTest,FormatsWhileLoop)538 TEST_F(FormatTest, FormatsWhileLoop) {
539 verifyFormat("while (true) {\n}");
540 verifyFormat("while (true)\n"
541 " f();");
542 verifyFormat("while () {\n}");
543 verifyFormat("while () {\n"
544 " f();\n"
545 "}");
546 }
547
TEST_F(FormatTest,FormatsDoWhile)548 TEST_F(FormatTest, FormatsDoWhile) {
549 verifyFormat("do {\n"
550 " do_something();\n"
551 "} while (something());");
552 verifyFormat("do\n"
553 " do_something();\n"
554 "while (something());");
555 }
556
TEST_F(FormatTest,FormatsSwitchStatement)557 TEST_F(FormatTest, FormatsSwitchStatement) {
558 verifyFormat("switch (x) {\n"
559 "case 1:\n"
560 " f();\n"
561 " break;\n"
562 "case kFoo:\n"
563 "case ns::kBar:\n"
564 "case kBaz:\n"
565 " break;\n"
566 "default:\n"
567 " g();\n"
568 " break;\n"
569 "}");
570 verifyFormat("switch (x) {\n"
571 "case 1: {\n"
572 " f();\n"
573 " break;\n"
574 "}\n"
575 "case 2: {\n"
576 " break;\n"
577 "}\n"
578 "}");
579 verifyFormat("switch (x) {\n"
580 "case 1: {\n"
581 " f();\n"
582 " {\n"
583 " g();\n"
584 " h();\n"
585 " }\n"
586 " break;\n"
587 "}\n"
588 "}");
589 verifyFormat("switch (x) {\n"
590 "case 1: {\n"
591 " f();\n"
592 " if (foo) {\n"
593 " g();\n"
594 " h();\n"
595 " }\n"
596 " break;\n"
597 "}\n"
598 "}");
599 verifyFormat("switch (x) {\n"
600 "case 1: {\n"
601 " f();\n"
602 " g();\n"
603 "} break;\n"
604 "}");
605 verifyFormat("switch (test)\n"
606 " ;");
607 verifyFormat("switch (x) {\n"
608 "default: {\n"
609 " // Do nothing.\n"
610 "}\n"
611 "}");
612 verifyFormat("switch (x) {\n"
613 "// comment\n"
614 "// if 1, do f()\n"
615 "case 1:\n"
616 " f();\n"
617 "}");
618 verifyFormat("switch (x) {\n"
619 "case 1:\n"
620 " // Do amazing stuff\n"
621 " {\n"
622 " f();\n"
623 " g();\n"
624 " }\n"
625 " break;\n"
626 "}");
627 verifyFormat("#define A \\\n"
628 " switch (x) { \\\n"
629 " case a: \\\n"
630 " foo = b; \\\n"
631 " }", getLLVMStyleWithColumns(20));
632 verifyFormat("#define OPERATION_CASE(name) \\\n"
633 " case OP_name: \\\n"
634 " return operations::Operation##name\n",
635 getLLVMStyleWithColumns(40));
636
637 verifyGoogleFormat("switch (x) {\n"
638 " case 1:\n"
639 " f();\n"
640 " break;\n"
641 " case kFoo:\n"
642 " case ns::kBar:\n"
643 " case kBaz:\n"
644 " break;\n"
645 " default:\n"
646 " g();\n"
647 " break;\n"
648 "}");
649 verifyGoogleFormat("switch (x) {\n"
650 " case 1: {\n"
651 " f();\n"
652 " break;\n"
653 " }\n"
654 "}");
655 verifyGoogleFormat("switch (test)\n"
656 " ;");
657
658 verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
659 " case OP_name: \\\n"
660 " return operations::Operation##name\n");
661 verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
662 " // Get the correction operation class.\n"
663 " switch (OpCode) {\n"
664 " CASE(Add);\n"
665 " CASE(Subtract);\n"
666 " default:\n"
667 " return operations::Unknown;\n"
668 " }\n"
669 "#undef OPERATION_CASE\n"
670 "}");
671 verifyFormat("DEBUG({\n"
672 " switch (x) {\n"
673 " case A:\n"
674 " f();\n"
675 " break;\n"
676 " // On B:\n"
677 " case B:\n"
678 " g();\n"
679 " break;\n"
680 " }\n"
681 "});");
682 verifyFormat("switch (a) {\n"
683 "case (b):\n"
684 " return;\n"
685 "}");
686
687 verifyFormat("switch (a) {\n"
688 "case some_namespace::\n"
689 " some_constant:\n"
690 " return;\n"
691 "}",
692 getLLVMStyleWithColumns(34));
693 }
694
TEST_F(FormatTest,CaseRanges)695 TEST_F(FormatTest, CaseRanges) {
696 verifyFormat("switch (x) {\n"
697 "case 'A' ... 'Z':\n"
698 "case 1 ... 5:\n"
699 " break;\n"
700 "}");
701 }
702
TEST_F(FormatTest,FormatsLabels)703 TEST_F(FormatTest, FormatsLabels) {
704 verifyFormat("void f() {\n"
705 " some_code();\n"
706 "test_label:\n"
707 " some_other_code();\n"
708 " {\n"
709 " some_more_code();\n"
710 " another_label:\n"
711 " some_more_code();\n"
712 " }\n"
713 "}");
714 verifyFormat("some_code();\n"
715 "test_label:\n"
716 "some_other_code();");
717 }
718
719 //===----------------------------------------------------------------------===//
720 // Tests for comments.
721 //===----------------------------------------------------------------------===//
722
TEST_F(FormatTest,UnderstandsSingleLineComments)723 TEST_F(FormatTest, UnderstandsSingleLineComments) {
724 verifyFormat("//* */");
725 verifyFormat("// line 1\n"
726 "// line 2\n"
727 "void f() {}\n");
728
729 verifyFormat("void f() {\n"
730 " // Doesn't do anything\n"
731 "}");
732 verifyFormat("SomeObject\n"
733 " // Calling someFunction on SomeObject\n"
734 " .someFunction();");
735 verifyFormat("auto result = SomeObject\n"
736 " // Calling someFunction on SomeObject\n"
737 " .someFunction();");
738 verifyFormat("void f(int i, // some comment (probably for i)\n"
739 " int j, // some comment (probably for j)\n"
740 " int k); // some comment (probably for k)");
741 verifyFormat("void f(int i,\n"
742 " // some comment (probably for j)\n"
743 " int j,\n"
744 " // some comment (probably for k)\n"
745 " int k);");
746
747 verifyFormat("int i // This is a fancy variable\n"
748 " = 5; // with nicely aligned comment.");
749
750 verifyFormat("// Leading comment.\n"
751 "int a; // Trailing comment.");
752 verifyFormat("int a; // Trailing comment\n"
753 " // on 2\n"
754 " // or 3 lines.\n"
755 "int b;");
756 verifyFormat("int a; // Trailing comment\n"
757 "\n"
758 "// Leading comment.\n"
759 "int b;");
760 verifyFormat("int a; // Comment.\n"
761 " // More details.\n"
762 "int bbbb; // Another comment.");
763 verifyFormat(
764 "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
765 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // comment\n"
766 "int cccccccccccccccccccccccccccccc; // comment\n"
767 "int ddd; // looooooooooooooooooooooooong comment\n"
768 "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
769 "int bbbbbbbbbbbbbbbbbbbbb; // comment\n"
770 "int ccccccccccccccccccc; // comment");
771
772 verifyFormat("#include \"a\" // comment\n"
773 "#include \"a/b/c\" // comment");
774 verifyFormat("#include <a> // comment\n"
775 "#include <a/b/c> // comment");
776 EXPECT_EQ("#include \"a\" // comment\n"
777 "#include \"a/b/c\" // comment",
778 format("#include \\\n"
779 " \"a\" // comment\n"
780 "#include \"a/b/c\" // comment"));
781
782 verifyFormat("enum E {\n"
783 " // comment\n"
784 " VAL_A, // comment\n"
785 " VAL_B\n"
786 "};");
787
788 verifyFormat(
789 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
790 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
791 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
792 " // Comment inside a statement.\n"
793 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
794 verifyFormat(
795 "bool aaaaaaaaaaaaa = // comment\n"
796 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
797 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
798
799 verifyFormat("int aaaa; // aaaaa\n"
800 "int aa; // aaaaaaa",
801 getLLVMStyleWithColumns(20));
802
803 EXPECT_EQ("void f() { // This does something ..\n"
804 "}\n"
805 "int a; // This is unrelated",
806 format("void f() { // This does something ..\n"
807 " }\n"
808 "int a; // This is unrelated"));
809 EXPECT_EQ("class C {\n"
810 " void f() { // This does something ..\n"
811 " } // awesome..\n"
812 "\n"
813 " int a; // This is unrelated\n"
814 "};",
815 format("class C{void f() { // This does something ..\n"
816 " } // awesome..\n"
817 " \n"
818 "int a; // This is unrelated\n"
819 "};"));
820
821 EXPECT_EQ("int i; // single line trailing comment",
822 format("int i;\\\n// single line trailing comment"));
823
824 verifyGoogleFormat("int a; // Trailing comment.");
825
826 verifyFormat("someFunction(anotherFunction( // Force break.\n"
827 " parameter));");
828
829 verifyGoogleFormat("#endif // HEADER_GUARD");
830
831 verifyFormat("const char *test[] = {\n"
832 " // A\n"
833 " \"aaaa\",\n"
834 " // B\n"
835 " \"aaaaa\"};");
836 verifyGoogleFormat(
837 "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
838 " aaaaaaaaaaaaaaaaaaaaaa); // 81_cols_with_this_comment");
839 EXPECT_EQ("D(a, {\n"
840 " // test\n"
841 " int a;\n"
842 "});",
843 format("D(a, {\n"
844 "// test\n"
845 "int a;\n"
846 "});"));
847
848 EXPECT_EQ("lineWith(); // comment\n"
849 "// at start\n"
850 "otherLine();",
851 format("lineWith(); // comment\n"
852 "// at start\n"
853 "otherLine();"));
854 EXPECT_EQ("lineWith(); // comment\n"
855 " // at start\n"
856 "otherLine();",
857 format("lineWith(); // comment\n"
858 " // at start\n"
859 "otherLine();"));
860
861 EXPECT_EQ("lineWith(); // comment\n"
862 "// at start\n"
863 "otherLine(); // comment",
864 format("lineWith(); // comment\n"
865 "// at start\n"
866 "otherLine(); // comment"));
867 EXPECT_EQ("lineWith();\n"
868 "// at start\n"
869 "otherLine(); // comment",
870 format("lineWith();\n"
871 " // at start\n"
872 "otherLine(); // comment"));
873 EXPECT_EQ("// first\n"
874 "// at start\n"
875 "otherLine(); // comment",
876 format("// first\n"
877 " // at start\n"
878 "otherLine(); // comment"));
879 EXPECT_EQ("f();\n"
880 "// first\n"
881 "// at start\n"
882 "otherLine(); // comment",
883 format("f();\n"
884 "// first\n"
885 " // at start\n"
886 "otherLine(); // comment"));
887 verifyFormat("f(); // comment\n"
888 "// first\n"
889 "// at start\n"
890 "otherLine();");
891 EXPECT_EQ("f(); // comment\n"
892 "// first\n"
893 "// at start\n"
894 "otherLine();",
895 format("f(); // comment\n"
896 "// first\n"
897 " // at start\n"
898 "otherLine();"));
899 EXPECT_EQ("f(); // comment\n"
900 " // first\n"
901 "// at start\n"
902 "otherLine();",
903 format("f(); // comment\n"
904 " // first\n"
905 "// at start\n"
906 "otherLine();"));
907
908 verifyFormat(
909 "#define A \\\n"
910 " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n"
911 " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
912 getLLVMStyleWithColumns(60));
913 verifyFormat(
914 "#define A \\\n"
915 " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n"
916 " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
917 getLLVMStyleWithColumns(61));
918
919 verifyFormat("if ( // This is some comment\n"
920 " x + 3) {\n"
921 "}");
922 EXPECT_EQ("if ( // This is some comment\n"
923 " // spanning two lines\n"
924 " x + 3) {\n"
925 "}",
926 format("if( // This is some comment\n"
927 " // spanning two lines\n"
928 " x + 3) {\n"
929 "}"));
930 }
931
TEST_F(FormatTest,KeepsParameterWithTrailingCommentsOnTheirOwnLine)932 TEST_F(FormatTest, KeepsParameterWithTrailingCommentsOnTheirOwnLine) {
933 EXPECT_EQ("SomeFunction(a,\n"
934 " b, // comment\n"
935 " c);",
936 format("SomeFunction(a,\n"
937 " b, // comment\n"
938 " c);"));
939 EXPECT_EQ("SomeFunction(a, b,\n"
940 " // comment\n"
941 " c);",
942 format("SomeFunction(a,\n"
943 " b,\n"
944 " // comment\n"
945 " c);"));
946 EXPECT_EQ("SomeFunction(a, b, // comment (unclear relation)\n"
947 " c);",
948 format("SomeFunction(a, b, // comment (unclear relation)\n"
949 " c);"));
950 EXPECT_EQ("SomeFunction(a, // comment\n"
951 " b,\n"
952 " c); // comment",
953 format("SomeFunction(a, // comment\n"
954 " b,\n"
955 " c); // comment"));
956 }
957
TEST_F(FormatTest,CanFormatCommentsLocally)958 TEST_F(FormatTest, CanFormatCommentsLocally) {
959 EXPECT_EQ("int a; // comment\n"
960 "int b; // comment",
961 format("int a; // comment\n"
962 "int b; // comment",
963 0, 0, getLLVMStyle()));
964 EXPECT_EQ("int a; // comment\n"
965 " // line 2\n"
966 "int b;",
967 format("int a; // comment\n"
968 " // line 2\n"
969 "int b;",
970 28, 0, getLLVMStyle()));
971 EXPECT_EQ("int aaaaaa; // comment\n"
972 "int b;\n"
973 "int c; // unrelated comment",
974 format("int aaaaaa; // comment\n"
975 "int b;\n"
976 "int c; // unrelated comment",
977 31, 0, getLLVMStyle()));
978
979 EXPECT_EQ("int a; // This\n"
980 " // is\n"
981 " // a",
982 format("int a; // This\n"
983 " // is\n"
984 " // a",
985 0, 0, getLLVMStyle()));
986 EXPECT_EQ("int a; // This\n"
987 " // is\n"
988 " // a\n"
989 "// This is b\n"
990 "int b;",
991 format("int a; // This\n"
992 " // is\n"
993 " // a\n"
994 "// This is b\n"
995 "int b;",
996 0, 0, getLLVMStyle()));
997 EXPECT_EQ("int a; // This\n"
998 " // is\n"
999 " // a\n"
1000 "\n"
1001 " // This is unrelated",
1002 format("int a; // This\n"
1003 " // is\n"
1004 " // a\n"
1005 "\n"
1006 " // This is unrelated",
1007 0, 0, getLLVMStyle()));
1008 EXPECT_EQ("int a;\n"
1009 "// This is\n"
1010 "// not formatted. ",
1011 format("int a;\n"
1012 "// This is\n"
1013 "// not formatted. ",
1014 0, 0, getLLVMStyle()));
1015 }
1016
TEST_F(FormatTest,RemovesTrailingWhitespaceOfComments)1017 TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) {
1018 EXPECT_EQ("// comment", format("// comment "));
1019 EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment",
1020 format("int aaaaaaa, bbbbbbb; // comment ",
1021 getLLVMStyleWithColumns(33)));
1022 EXPECT_EQ("// comment\\\n", format("// comment\\\n \t \v \f "));
1023 EXPECT_EQ("// comment \\\n", format("// comment \\\n \t \v \f "));
1024 }
1025
TEST_F(FormatTest,UnderstandsBlockComments)1026 TEST_F(FormatTest, UnderstandsBlockComments) {
1027 verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);");
1028 verifyFormat("void f() { g(/*aaa=*/x, /*bbb=*/!y); }");
1029 EXPECT_EQ("f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n"
1030 " bbbbbbbbbbbbbbbbbbbbbbbbb);",
1031 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \\\n"
1032 "/* Trailing comment for aa... */\n"
1033 " bbbbbbbbbbbbbbbbbbbbbbbbb);"));
1034 EXPECT_EQ(
1035 "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1036 " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);",
1037 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n"
1038 "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);"));
1039 EXPECT_EQ(
1040 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1041 " aaaaaaaaaaaaaaaaaa,\n"
1042 " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
1043 "}",
1044 format("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1045 " aaaaaaaaaaaaaaaaaa ,\n"
1046 " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
1047 "}"));
1048
1049 FormatStyle NoBinPacking = getLLVMStyle();
1050 NoBinPacking.BinPackParameters = false;
1051 verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n"
1052 " /* parameter 2 */ aaaaaa,\n"
1053 " /* parameter 3 */ aaaaaa,\n"
1054 " /* parameter 4 */ aaaaaa);",
1055 NoBinPacking);
1056
1057 // Aligning block comments in macros.
1058 verifyGoogleFormat("#define A \\\n"
1059 " int i; /*a*/ \\\n"
1060 " int jjj; /*b*/");
1061 }
1062
TEST_F(FormatTest,AlignsBlockComments)1063 TEST_F(FormatTest, AlignsBlockComments) {
1064 EXPECT_EQ("/*\n"
1065 " * Really multi-line\n"
1066 " * comment.\n"
1067 " */\n"
1068 "void f() {}",
1069 format(" /*\n"
1070 " * Really multi-line\n"
1071 " * comment.\n"
1072 " */\n"
1073 " void f() {}"));
1074 EXPECT_EQ("class C {\n"
1075 " /*\n"
1076 " * Another multi-line\n"
1077 " * comment.\n"
1078 " */\n"
1079 " void f() {}\n"
1080 "};",
1081 format("class C {\n"
1082 "/*\n"
1083 " * Another multi-line\n"
1084 " * comment.\n"
1085 " */\n"
1086 "void f() {}\n"
1087 "};"));
1088 EXPECT_EQ("/*\n"
1089 " 1. This is a comment with non-trivial formatting.\n"
1090 " 1.1. We have to indent/outdent all lines equally\n"
1091 " 1.1.1. to keep the formatting.\n"
1092 " */",
1093 format(" /*\n"
1094 " 1. This is a comment with non-trivial formatting.\n"
1095 " 1.1. We have to indent/outdent all lines equally\n"
1096 " 1.1.1. to keep the formatting.\n"
1097 " */"));
1098 EXPECT_EQ("/*\n"
1099 "Don't try to outdent if there's not enough indentation.\n"
1100 "*/",
1101 format(" /*\n"
1102 " Don't try to outdent if there's not enough indentation.\n"
1103 " */"));
1104
1105 EXPECT_EQ("int i; /* Comment with empty...\n"
1106 " *\n"
1107 " * line. */",
1108 format("int i; /* Comment with empty...\n"
1109 " *\n"
1110 " * line. */"));
1111 EXPECT_EQ("int foobar = 0; /* comment */\n"
1112 "int bar = 0; /* multiline\n"
1113 " comment 1 */\n"
1114 "int baz = 0; /* multiline\n"
1115 " comment 2 */\n"
1116 "int bzz = 0; /* multiline\n"
1117 " comment 3 */",
1118 format("int foobar = 0; /* comment */\n"
1119 "int bar = 0; /* multiline\n"
1120 " comment 1 */\n"
1121 "int baz = 0; /* multiline\n"
1122 " comment 2 */\n"
1123 "int bzz = 0; /* multiline\n"
1124 " comment 3 */"));
1125 EXPECT_EQ("int foobar = 0; /* comment */\n"
1126 "int bar = 0; /* multiline\n"
1127 " comment */\n"
1128 "int baz = 0; /* multiline\n"
1129 "comment */",
1130 format("int foobar = 0; /* comment */\n"
1131 "int bar = 0; /* multiline\n"
1132 "comment */\n"
1133 "int baz = 0; /* multiline\n"
1134 "comment */"));
1135 }
1136
TEST_F(FormatTest,CorrectlyHandlesLengthOfBlockComments)1137 TEST_F(FormatTest, CorrectlyHandlesLengthOfBlockComments) {
1138 EXPECT_EQ("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1139 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */",
1140 format("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1141 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */"));
1142 EXPECT_EQ(
1143 "void ffffffffffff(\n"
1144 " int aaaaaaaa, int bbbbbbbb,\n"
1145 " int cccccccccccc) { /*\n"
1146 " aaaaaaaaaa\n"
1147 " aaaaaaaaaaaaa\n"
1148 " bbbbbbbbbbbbbb\n"
1149 " bbbbbbbbbb\n"
1150 " */\n"
1151 "}",
1152 format("void ffffffffffff(int aaaaaaaa, int bbbbbbbb, int cccccccccccc)\n"
1153 "{ /*\n"
1154 " aaaaaaaaaa aaaaaaaaaaaaa\n"
1155 " bbbbbbbbbbbbbb bbbbbbbbbb\n"
1156 " */\n"
1157 "}",
1158 getLLVMStyleWithColumns(40)));
1159 }
1160
TEST_F(FormatTest,DontBreakNonTrailingBlockComments)1161 TEST_F(FormatTest, DontBreakNonTrailingBlockComments) {
1162 EXPECT_EQ("void\n"
1163 "ffffffffff(int aaaaa /* test */);",
1164 format("void ffffffffff(int aaaaa /* test */);",
1165 getLLVMStyleWithColumns(35)));
1166 }
1167
TEST_F(FormatTest,SplitsLongCxxComments)1168 TEST_F(FormatTest, SplitsLongCxxComments) {
1169 EXPECT_EQ("// A comment that\n"
1170 "// doesn't fit on\n"
1171 "// one line",
1172 format("// A comment that doesn't fit on one line",
1173 getLLVMStyleWithColumns(20)));
1174 EXPECT_EQ("// a b c d\n"
1175 "// e f g\n"
1176 "// h i j k",
1177 format("// a b c d e f g h i j k",
1178 getLLVMStyleWithColumns(10)));
1179 EXPECT_EQ("// a b c d\n"
1180 "// e f g\n"
1181 "// h i j k",
1182 format("\\\n// a b c d e f g h i j k",
1183 getLLVMStyleWithColumns(10)));
1184 EXPECT_EQ("if (true) // A comment that\n"
1185 " // doesn't fit on\n"
1186 " // one line",
1187 format("if (true) // A comment that doesn't fit on one line ",
1188 getLLVMStyleWithColumns(30)));
1189 EXPECT_EQ("// Don't_touch_leading_whitespace",
1190 format("// Don't_touch_leading_whitespace",
1191 getLLVMStyleWithColumns(20)));
1192 EXPECT_EQ("// Add leading\n"
1193 "// whitespace",
1194 format("//Add leading whitespace", getLLVMStyleWithColumns(20)));
1195 EXPECT_EQ("// whitespace", format("//whitespace", getLLVMStyle()));
1196 EXPECT_EQ("// Even if it makes the line exceed the column\n"
1197 "// limit",
1198 format("//Even if it makes the line exceed the column limit",
1199 getLLVMStyleWithColumns(51)));
1200 EXPECT_EQ("//--But not here", format("//--But not here", getLLVMStyle()));
1201
1202 EXPECT_EQ("// aa bb cc dd",
1203 format("// aa bb cc dd ",
1204 getLLVMStyleWithColumns(15)));
1205
1206 EXPECT_EQ("// A comment before\n"
1207 "// a macro\n"
1208 "// definition\n"
1209 "#define a b",
1210 format("// A comment before a macro definition\n"
1211 "#define a b",
1212 getLLVMStyleWithColumns(20)));
1213 EXPECT_EQ("void\n"
1214 "ffffff(int aaaaaaaaa, // wwww\n"
1215 " int bbbbbbbbbb, // xxxxxxx\n"
1216 " // yyyyyyyyyy\n"
1217 " int c, int d, int e) {}",
1218 format("void ffffff(\n"
1219 " int aaaaaaaaa, // wwww\n"
1220 " int bbbbbbbbbb, // xxxxxxx yyyyyyyyyy\n"
1221 " int c, int d, int e) {}",
1222 getLLVMStyleWithColumns(40)));
1223 EXPECT_EQ("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
1224 format("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
1225 getLLVMStyleWithColumns(20)));
1226 EXPECT_EQ(
1227 "#define XXX // a b c d\n"
1228 " // e f g h",
1229 format("#define XXX // a b c d e f g h", getLLVMStyleWithColumns(22)));
1230 EXPECT_EQ(
1231 "#define XXX // q w e r\n"
1232 " // t y u i",
1233 format("#define XXX //q w e r t y u i", getLLVMStyleWithColumns(22)));
1234 }
1235
TEST_F(FormatTest,PreservesHangingIndentInCxxComments)1236 TEST_F(FormatTest, PreservesHangingIndentInCxxComments) {
1237 EXPECT_EQ("// A comment\n"
1238 "// that doesn't\n"
1239 "// fit on one\n"
1240 "// line",
1241 format("// A comment that doesn't fit on one line",
1242 getLLVMStyleWithColumns(20)));
1243 EXPECT_EQ("/// A comment\n"
1244 "/// that doesn't\n"
1245 "/// fit on one\n"
1246 "/// line",
1247 format("/// A comment that doesn't fit on one line",
1248 getLLVMStyleWithColumns(20)));
1249 }
1250
TEST_F(FormatTest,DontSplitLineCommentsWithEscapedNewlines)1251 TEST_F(FormatTest, DontSplitLineCommentsWithEscapedNewlines) {
1252 EXPECT_EQ("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
1253 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
1254 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
1255 format("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
1256 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
1257 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
1258 EXPECT_EQ("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1259 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1260 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
1261 format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1262 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1263 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
1264 getLLVMStyleWithColumns(50)));
1265 // FIXME: One day we might want to implement adjustment of leading whitespace
1266 // of the consecutive lines in this kind of comment:
1267 EXPECT_EQ("double\n"
1268 " a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1269 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1270 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
1271 format("double a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1272 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1273 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
1274 getLLVMStyleWithColumns(49)));
1275 }
1276
TEST_F(FormatTest,DontSplitLineCommentsWithPragmas)1277 TEST_F(FormatTest, DontSplitLineCommentsWithPragmas) {
1278 FormatStyle Pragmas = getLLVMStyleWithColumns(30);
1279 Pragmas.CommentPragmas = "^ IWYU pragma:";
1280 EXPECT_EQ(
1281 "// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb",
1282 format("// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", Pragmas));
1283 EXPECT_EQ(
1284 "/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */",
1285 format("/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", Pragmas));
1286 }
1287
TEST_F(FormatTest,PriorityOfCommentBreaking)1288 TEST_F(FormatTest, PriorityOfCommentBreaking) {
1289 EXPECT_EQ("if (xxx ==\n"
1290 " yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
1291 " zzz)\n"
1292 " q();",
1293 format("if (xxx == yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
1294 " zzz) q();",
1295 getLLVMStyleWithColumns(40)));
1296 EXPECT_EQ("if (xxxxxxxxxx ==\n"
1297 " yyy && // aaaaaa bbbbbbbb cccc\n"
1298 " zzz)\n"
1299 " q();",
1300 format("if (xxxxxxxxxx == yyy && // aaaaaa bbbbbbbb cccc\n"
1301 " zzz) q();",
1302 getLLVMStyleWithColumns(40)));
1303 EXPECT_EQ("if (xxxxxxxxxx &&\n"
1304 " yyy || // aaaaaa bbbbbbbb cccc\n"
1305 " zzz)\n"
1306 " q();",
1307 format("if (xxxxxxxxxx && yyy || // aaaaaa bbbbbbbb cccc\n"
1308 " zzz) q();",
1309 getLLVMStyleWithColumns(40)));
1310 EXPECT_EQ("fffffffff(\n"
1311 " &xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
1312 " zzz);",
1313 format("fffffffff(&xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
1314 " zzz);",
1315 getLLVMStyleWithColumns(40)));
1316 }
1317
TEST_F(FormatTest,MultiLineCommentsInDefines)1318 TEST_F(FormatTest, MultiLineCommentsInDefines) {
1319 EXPECT_EQ("#define A(x) /* \\\n"
1320 " a comment \\\n"
1321 " inside */ \\\n"
1322 " f();",
1323 format("#define A(x) /* \\\n"
1324 " a comment \\\n"
1325 " inside */ \\\n"
1326 " f();",
1327 getLLVMStyleWithColumns(17)));
1328 EXPECT_EQ("#define A( \\\n"
1329 " x) /* \\\n"
1330 " a comment \\\n"
1331 " inside */ \\\n"
1332 " f();",
1333 format("#define A( \\\n"
1334 " x) /* \\\n"
1335 " a comment \\\n"
1336 " inside */ \\\n"
1337 " f();",
1338 getLLVMStyleWithColumns(17)));
1339 }
1340
TEST_F(FormatTest,ParsesCommentsAdjacentToPPDirectives)1341 TEST_F(FormatTest, ParsesCommentsAdjacentToPPDirectives) {
1342 EXPECT_EQ("namespace {}\n// Test\n#define A",
1343 format("namespace {}\n // Test\n#define A"));
1344 EXPECT_EQ("namespace {}\n/* Test */\n#define A",
1345 format("namespace {}\n /* Test */\n#define A"));
1346 EXPECT_EQ("namespace {}\n/* Test */ #define A",
1347 format("namespace {}\n /* Test */ #define A"));
1348 }
1349
TEST_F(FormatTest,SplitsLongLinesInComments)1350 TEST_F(FormatTest, SplitsLongLinesInComments) {
1351 EXPECT_EQ("/* This is a long\n"
1352 " * comment that\n"
1353 " * doesn't\n"
1354 " * fit on one line.\n"
1355 " */",
1356 format("/* "
1357 "This is a long "
1358 "comment that "
1359 "doesn't "
1360 "fit on one line. */",
1361 getLLVMStyleWithColumns(20)));
1362 EXPECT_EQ("/* a b c d\n"
1363 " * e f g\n"
1364 " * h i j k\n"
1365 " */",
1366 format("/* a b c d e f g h i j k */",
1367 getLLVMStyleWithColumns(10)));
1368 EXPECT_EQ("/* a b c d\n"
1369 " * e f g\n"
1370 " * h i j k\n"
1371 " */",
1372 format("\\\n/* a b c d e f g h i j k */",
1373 getLLVMStyleWithColumns(10)));
1374 EXPECT_EQ("/*\n"
1375 "This is a long\n"
1376 "comment that doesn't\n"
1377 "fit on one line.\n"
1378 "*/",
1379 format("/*\n"
1380 "This is a long "
1381 "comment that doesn't "
1382 "fit on one line. \n"
1383 "*/", getLLVMStyleWithColumns(20)));
1384 EXPECT_EQ("/*\n"
1385 " * This is a long\n"
1386 " * comment that\n"
1387 " * doesn't fit on\n"
1388 " * one line.\n"
1389 " */",
1390 format("/* \n"
1391 " * This is a long "
1392 " comment that "
1393 " doesn't fit on "
1394 " one line. \n"
1395 " */", getLLVMStyleWithColumns(20)));
1396 EXPECT_EQ("/*\n"
1397 " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n"
1398 " * so_it_should_be_broken\n"
1399 " * wherever_a_space_occurs\n"
1400 " */",
1401 format("/*\n"
1402 " * This_is_a_comment_with_words_that_dont_fit_on_one_line "
1403 " so_it_should_be_broken "
1404 " wherever_a_space_occurs \n"
1405 " */",
1406 getLLVMStyleWithColumns(20)));
1407 EXPECT_EQ("/*\n"
1408 " * This_comment_can_not_be_broken_into_lines\n"
1409 " */",
1410 format("/*\n"
1411 " * This_comment_can_not_be_broken_into_lines\n"
1412 " */",
1413 getLLVMStyleWithColumns(20)));
1414 EXPECT_EQ("{\n"
1415 " /*\n"
1416 " This is another\n"
1417 " long comment that\n"
1418 " doesn't fit on one\n"
1419 " line 1234567890\n"
1420 " */\n"
1421 "}",
1422 format("{\n"
1423 "/*\n"
1424 "This is another "
1425 " long comment that "
1426 " doesn't fit on one"
1427 " line 1234567890\n"
1428 "*/\n"
1429 "}", getLLVMStyleWithColumns(20)));
1430 EXPECT_EQ("{\n"
1431 " /*\n"
1432 " * This i s\n"
1433 " * another comment\n"
1434 " * t hat doesn' t\n"
1435 " * fit on one l i\n"
1436 " * n e\n"
1437 " */\n"
1438 "}",
1439 format("{\n"
1440 "/*\n"
1441 " * This i s"
1442 " another comment"
1443 " t hat doesn' t"
1444 " fit on one l i"
1445 " n e\n"
1446 " */\n"
1447 "}", getLLVMStyleWithColumns(20)));
1448 EXPECT_EQ("/*\n"
1449 " * This is a long\n"
1450 " * comment that\n"
1451 " * doesn't fit on\n"
1452 " * one line\n"
1453 " */",
1454 format(" /*\n"
1455 " * This is a long comment that doesn't fit on one line\n"
1456 " */", getLLVMStyleWithColumns(20)));
1457 EXPECT_EQ("{\n"
1458 " if (something) /* This is a\n"
1459 " long\n"
1460 " comment */\n"
1461 " ;\n"
1462 "}",
1463 format("{\n"
1464 " if (something) /* This is a long comment */\n"
1465 " ;\n"
1466 "}",
1467 getLLVMStyleWithColumns(30)));
1468
1469 EXPECT_EQ("/* A comment before\n"
1470 " * a macro\n"
1471 " * definition */\n"
1472 "#define a b",
1473 format("/* A comment before a macro definition */\n"
1474 "#define a b",
1475 getLLVMStyleWithColumns(20)));
1476
1477 EXPECT_EQ("/* some comment\n"
1478 " * a comment\n"
1479 "* that we break\n"
1480 " * another comment\n"
1481 "* we have to break\n"
1482 "* a left comment\n"
1483 " */",
1484 format(" /* some comment\n"
1485 " * a comment that we break\n"
1486 " * another comment we have to break\n"
1487 "* a left comment\n"
1488 " */",
1489 getLLVMStyleWithColumns(20)));
1490
1491 EXPECT_EQ("/*\n"
1492 "\n"
1493 "\n"
1494 " */\n",
1495 format(" /* \n"
1496 " \n"
1497 " \n"
1498 " */\n"));
1499
1500 EXPECT_EQ("/* a a */",
1501 format("/* a a */", getLLVMStyleWithColumns(15)));
1502 EXPECT_EQ("/* a a bc */",
1503 format("/* a a bc */", getLLVMStyleWithColumns(15)));
1504 EXPECT_EQ("/* aaa aaa\n"
1505 " * aaaaa */",
1506 format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15)));
1507 EXPECT_EQ("/* aaa aaa\n"
1508 " * aaaaa */",
1509 format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15)));
1510 }
1511
TEST_F(FormatTest,SplitsLongLinesInCommentsInPreprocessor)1512 TEST_F(FormatTest, SplitsLongLinesInCommentsInPreprocessor) {
1513 EXPECT_EQ("#define X \\\n"
1514 " /* \\\n"
1515 " Test \\\n"
1516 " Macro comment \\\n"
1517 " with a long \\\n"
1518 " line \\\n"
1519 " */ \\\n"
1520 " A + B",
1521 format("#define X \\\n"
1522 " /*\n"
1523 " Test\n"
1524 " Macro comment with a long line\n"
1525 " */ \\\n"
1526 " A + B",
1527 getLLVMStyleWithColumns(20)));
1528 EXPECT_EQ("#define X \\\n"
1529 " /* Macro comment \\\n"
1530 " with a long \\\n"
1531 " line */ \\\n"
1532 " A + B",
1533 format("#define X \\\n"
1534 " /* Macro comment with a long\n"
1535 " line */ \\\n"
1536 " A + B",
1537 getLLVMStyleWithColumns(20)));
1538 EXPECT_EQ("#define X \\\n"
1539 " /* Macro comment \\\n"
1540 " * with a long \\\n"
1541 " * line */ \\\n"
1542 " A + B",
1543 format("#define X \\\n"
1544 " /* Macro comment with a long line */ \\\n"
1545 " A + B",
1546 getLLVMStyleWithColumns(20)));
1547 }
1548
TEST_F(FormatTest,CommentsInStaticInitializers)1549 TEST_F(FormatTest, CommentsInStaticInitializers) {
1550 EXPECT_EQ(
1551 "static SomeType type = {aaaaaaaaaaaaaaaaaaaa, /* comment */\n"
1552 " aaaaaaaaaaaaaaaaaaaa /* comment */,\n"
1553 " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n"
1554 " aaaaaaaaaaaaaaaaaaaa, // comment\n"
1555 " aaaaaaaaaaaaaaaaaaaa};",
1556 format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n"
1557 " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n"
1558 " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n"
1559 " aaaaaaaaaaaaaaaaaaaa , // comment\n"
1560 " aaaaaaaaaaaaaaaaaaaa };"));
1561 verifyFormat("static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
1562 " bbbbbbbbbbb, ccccccccccc};");
1563 verifyFormat("static SomeType type = {aaaaaaaaaaa,\n"
1564 " // comment for bb....\n"
1565 " bbbbbbbbbbb, ccccccccccc};");
1566 verifyGoogleFormat(
1567 "static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
1568 " bbbbbbbbbbb, ccccccccccc};");
1569 verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n"
1570 " // comment for bb....\n"
1571 " bbbbbbbbbbb, ccccccccccc};");
1572
1573 verifyFormat("S s = {{a, b, c}, // Group #1\n"
1574 " {d, e, f}, // Group #2\n"
1575 " {g, h, i}}; // Group #3");
1576 verifyFormat("S s = {{// Group #1\n"
1577 " a, b, c},\n"
1578 " {// Group #2\n"
1579 " d, e, f},\n"
1580 " {// Group #3\n"
1581 " g, h, i}};");
1582
1583 EXPECT_EQ("S s = {\n"
1584 " // Some comment\n"
1585 " a,\n"
1586 "\n"
1587 " // Comment after empty line\n"
1588 " b}",
1589 format("S s = {\n"
1590 " // Some comment\n"
1591 " a,\n"
1592 " \n"
1593 " // Comment after empty line\n"
1594 " b\n"
1595 "}"));
1596 EXPECT_EQ("S s = {\n"
1597 " /* Some comment */\n"
1598 " a,\n"
1599 "\n"
1600 " /* Comment after empty line */\n"
1601 " b}",
1602 format("S s = {\n"
1603 " /* Some comment */\n"
1604 " a,\n"
1605 " \n"
1606 " /* Comment after empty line */\n"
1607 " b\n"
1608 "}"));
1609 verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n"
1610 " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
1611 " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
1612 " 0x00, 0x00, 0x00, 0x00}; // comment\n");
1613 }
1614
TEST_F(FormatTest,IgnoresIf0Contents)1615 TEST_F(FormatTest, IgnoresIf0Contents) {
1616 EXPECT_EQ("#if 0\n"
1617 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
1618 "#endif\n"
1619 "void f() {}",
1620 format("#if 0\n"
1621 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
1622 "#endif\n"
1623 "void f( ) { }"));
1624 EXPECT_EQ("#if false\n"
1625 "void f( ) { }\n"
1626 "#endif\n"
1627 "void g() {}\n",
1628 format("#if false\n"
1629 "void f( ) { }\n"
1630 "#endif\n"
1631 "void g( ) { }\n"));
1632 EXPECT_EQ("enum E {\n"
1633 " One,\n"
1634 " Two,\n"
1635 "#if 0\n"
1636 "Three,\n"
1637 " Four,\n"
1638 "#endif\n"
1639 " Five\n"
1640 "};",
1641 format("enum E {\n"
1642 " One,Two,\n"
1643 "#if 0\n"
1644 "Three,\n"
1645 " Four,\n"
1646 "#endif\n"
1647 " Five};"));
1648 EXPECT_EQ("enum F {\n"
1649 " One,\n"
1650 "#if 1\n"
1651 " Two,\n"
1652 "#if 0\n"
1653 "Three,\n"
1654 " Four,\n"
1655 "#endif\n"
1656 " Five\n"
1657 "#endif\n"
1658 "};",
1659 format("enum F {\n"
1660 "One,\n"
1661 "#if 1\n"
1662 "Two,\n"
1663 "#if 0\n"
1664 "Three,\n"
1665 " Four,\n"
1666 "#endif\n"
1667 "Five\n"
1668 "#endif\n"
1669 "};"));
1670 EXPECT_EQ("enum G {\n"
1671 " One,\n"
1672 "#if 0\n"
1673 "Two,\n"
1674 "#else\n"
1675 " Three,\n"
1676 "#endif\n"
1677 " Four\n"
1678 "};",
1679 format("enum G {\n"
1680 "One,\n"
1681 "#if 0\n"
1682 "Two,\n"
1683 "#else\n"
1684 "Three,\n"
1685 "#endif\n"
1686 "Four\n"
1687 "};"));
1688 EXPECT_EQ("enum H {\n"
1689 " One,\n"
1690 "#if 0\n"
1691 "#ifdef Q\n"
1692 "Two,\n"
1693 "#else\n"
1694 "Three,\n"
1695 "#endif\n"
1696 "#endif\n"
1697 " Four\n"
1698 "};",
1699 format("enum H {\n"
1700 "One,\n"
1701 "#if 0\n"
1702 "#ifdef Q\n"
1703 "Two,\n"
1704 "#else\n"
1705 "Three,\n"
1706 "#endif\n"
1707 "#endif\n"
1708 "Four\n"
1709 "};"));
1710 EXPECT_EQ("enum I {\n"
1711 " One,\n"
1712 "#if /* test */ 0 || 1\n"
1713 "Two,\n"
1714 "Three,\n"
1715 "#endif\n"
1716 " Four\n"
1717 "};",
1718 format("enum I {\n"
1719 "One,\n"
1720 "#if /* test */ 0 || 1\n"
1721 "Two,\n"
1722 "Three,\n"
1723 "#endif\n"
1724 "Four\n"
1725 "};"));
1726 EXPECT_EQ("enum J {\n"
1727 " One,\n"
1728 "#if 0\n"
1729 "#if 0\n"
1730 "Two,\n"
1731 "#else\n"
1732 "Three,\n"
1733 "#endif\n"
1734 "Four,\n"
1735 "#endif\n"
1736 " Five\n"
1737 "};",
1738 format("enum J {\n"
1739 "One,\n"
1740 "#if 0\n"
1741 "#if 0\n"
1742 "Two,\n"
1743 "#else\n"
1744 "Three,\n"
1745 "#endif\n"
1746 "Four,\n"
1747 "#endif\n"
1748 "Five\n"
1749 "};"));
1750
1751 }
1752
1753 //===----------------------------------------------------------------------===//
1754 // Tests for classes, namespaces, etc.
1755 //===----------------------------------------------------------------------===//
1756
TEST_F(FormatTest,DoesNotBreakSemiAfterClassDecl)1757 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
1758 verifyFormat("class A {};");
1759 }
1760
TEST_F(FormatTest,UnderstandsAccessSpecifiers)1761 TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
1762 verifyFormat("class A {\n"
1763 "public:\n"
1764 "public: // comment\n"
1765 "protected:\n"
1766 "private:\n"
1767 " void f() {}\n"
1768 "};");
1769 verifyGoogleFormat("class A {\n"
1770 " public:\n"
1771 " protected:\n"
1772 " private:\n"
1773 " void f() {}\n"
1774 "};");
1775 verifyFormat("class A {\n"
1776 "public slots:\n"
1777 " void f() {}\n"
1778 "public Q_SLOTS:\n"
1779 " void f() {}\n"
1780 "};");
1781 }
1782
TEST_F(FormatTest,SeparatesLogicalBlocks)1783 TEST_F(FormatTest, SeparatesLogicalBlocks) {
1784 EXPECT_EQ("class A {\n"
1785 "public:\n"
1786 " void f();\n"
1787 "\n"
1788 "private:\n"
1789 " void g() {}\n"
1790 " // test\n"
1791 "protected:\n"
1792 " int h;\n"
1793 "};",
1794 format("class A {\n"
1795 "public:\n"
1796 "void f();\n"
1797 "private:\n"
1798 "void g() {}\n"
1799 "// test\n"
1800 "protected:\n"
1801 "int h;\n"
1802 "};"));
1803 EXPECT_EQ("class A {\n"
1804 "protected:\n"
1805 "public:\n"
1806 " void f();\n"
1807 "};",
1808 format("class A {\n"
1809 "protected:\n"
1810 "\n"
1811 "public:\n"
1812 "\n"
1813 " void f();\n"
1814 "};"));
1815 }
1816
TEST_F(FormatTest,FormatsClasses)1817 TEST_F(FormatTest, FormatsClasses) {
1818 verifyFormat("class A : public B {};");
1819 verifyFormat("class A : public ::B {};");
1820
1821 verifyFormat(
1822 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1823 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
1824 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
1825 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1826 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
1827 verifyFormat(
1828 "class A : public B, public C, public D, public E, public F {};");
1829 verifyFormat("class AAAAAAAAAAAA : public B,\n"
1830 " public C,\n"
1831 " public D,\n"
1832 " public E,\n"
1833 " public F,\n"
1834 " public G {};");
1835
1836 verifyFormat("class\n"
1837 " ReallyReallyLongClassName {\n"
1838 " int i;\n"
1839 "};",
1840 getLLVMStyleWithColumns(32));
1841 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
1842 " aaaaaaaaaaaaaaaa> {};");
1843 verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
1844 " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
1845 " aaaaaaaaaaaaaaaaaaaaaa> {};");
1846 verifyFormat("template <class R, class C>\n"
1847 "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
1848 " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
1849 }
1850
TEST_F(FormatTest,FormatsVariableDeclarationsAfterStructOrClass)1851 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
1852 verifyFormat("class A {\n} a, b;");
1853 verifyFormat("struct A {\n} a, b;");
1854 verifyFormat("union A {\n} a;");
1855 }
1856
TEST_F(FormatTest,FormatsEnum)1857 TEST_F(FormatTest, FormatsEnum) {
1858 verifyFormat("enum {\n"
1859 " Zero,\n"
1860 " One = 1,\n"
1861 " Two = One + 1,\n"
1862 " Three = (One + Two),\n"
1863 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
1864 " Five = (One, Two, Three, Four, 5)\n"
1865 "};");
1866 verifyGoogleFormat("enum {\n"
1867 " Zero,\n"
1868 " One = 1,\n"
1869 " Two = One + 1,\n"
1870 " Three = (One + Two),\n"
1871 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
1872 " Five = (One, Two, Three, Four, 5)\n"
1873 "};");
1874 verifyFormat("enum Enum {};");
1875 verifyFormat("enum {};");
1876 verifyFormat("enum X E {} d;");
1877 verifyFormat("enum __attribute__((...)) E {} d;");
1878 verifyFormat("enum __declspec__((...)) E {} d;");
1879 verifyFormat("enum X f() {\n a();\n return 42;\n}");
1880 verifyFormat("enum {\n"
1881 " Bar = Foo<int, int>::value\n"
1882 "};",
1883 getLLVMStyleWithColumns(30));
1884
1885 verifyFormat("enum ShortEnum { A, B, C };");
1886 verifyGoogleFormat("enum ShortEnum { A, B, C };");
1887
1888 EXPECT_EQ("enum KeepEmptyLines {\n"
1889 " ONE,\n"
1890 "\n"
1891 " TWO,\n"
1892 "\n"
1893 " THREE\n"
1894 "}",
1895 format("enum KeepEmptyLines {\n"
1896 " ONE,\n"
1897 "\n"
1898 " TWO,\n"
1899 "\n"
1900 "\n"
1901 " THREE\n"
1902 "}"));
1903 verifyFormat("enum E { // comment\n"
1904 " ONE,\n"
1905 " TWO\n"
1906 "};");
1907 }
1908
TEST_F(FormatTest,FormatsEnumsWithErrors)1909 TEST_F(FormatTest, FormatsEnumsWithErrors) {
1910 verifyFormat("enum Type {\n"
1911 " One = 0; // These semicolons should be commas.\n"
1912 " Two = 1;\n"
1913 "};");
1914 verifyFormat("namespace n {\n"
1915 "enum Type {\n"
1916 " One,\n"
1917 " Two, // missing };\n"
1918 " int i;\n"
1919 "}\n"
1920 "void g() {}");
1921 }
1922
TEST_F(FormatTest,FormatsEnumStruct)1923 TEST_F(FormatTest, FormatsEnumStruct) {
1924 verifyFormat("enum struct {\n"
1925 " Zero,\n"
1926 " One = 1,\n"
1927 " Two = One + 1,\n"
1928 " Three = (One + Two),\n"
1929 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
1930 " Five = (One, Two, Three, Four, 5)\n"
1931 "};");
1932 verifyFormat("enum struct Enum {};");
1933 verifyFormat("enum struct {};");
1934 verifyFormat("enum struct X E {} d;");
1935 verifyFormat("enum struct __attribute__((...)) E {} d;");
1936 verifyFormat("enum struct __declspec__((...)) E {} d;");
1937 verifyFormat("enum struct X f() {\n a();\n return 42;\n}");
1938 }
1939
TEST_F(FormatTest,FormatsEnumClass)1940 TEST_F(FormatTest, FormatsEnumClass) {
1941 verifyFormat("enum class {\n"
1942 " Zero,\n"
1943 " One = 1,\n"
1944 " Two = One + 1,\n"
1945 " Three = (One + Two),\n"
1946 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
1947 " Five = (One, Two, Three, Four, 5)\n"
1948 "};");
1949 verifyFormat("enum class Enum {};");
1950 verifyFormat("enum class {};");
1951 verifyFormat("enum class X E {} d;");
1952 verifyFormat("enum class __attribute__((...)) E {} d;");
1953 verifyFormat("enum class __declspec__((...)) E {} d;");
1954 verifyFormat("enum class X f() {\n a();\n return 42;\n}");
1955 }
1956
TEST_F(FormatTest,FormatsEnumTypes)1957 TEST_F(FormatTest, FormatsEnumTypes) {
1958 verifyFormat("enum X : int {\n"
1959 " A, // Force multiple lines.\n"
1960 " B\n"
1961 "};");
1962 verifyFormat("enum X : int { A, B };");
1963 verifyFormat("enum X : std::uint32_t { A, B };");
1964 }
1965
TEST_F(FormatTest,FormatsNSEnums)1966 TEST_F(FormatTest, FormatsNSEnums) {
1967 verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
1968 verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
1969 " // Information about someDecentlyLongValue.\n"
1970 " someDecentlyLongValue,\n"
1971 " // Information about anotherDecentlyLongValue.\n"
1972 " anotherDecentlyLongValue,\n"
1973 " // Information about aThirdDecentlyLongValue.\n"
1974 " aThirdDecentlyLongValue\n"
1975 "};");
1976 }
1977
TEST_F(FormatTest,FormatsBitfields)1978 TEST_F(FormatTest, FormatsBitfields) {
1979 verifyFormat("struct Bitfields {\n"
1980 " unsigned sClass : 8;\n"
1981 " unsigned ValueKind : 2;\n"
1982 "};");
1983 verifyFormat("struct A {\n"
1984 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
1985 " bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
1986 "};");
1987 }
1988
TEST_F(FormatTest,FormatsNamespaces)1989 TEST_F(FormatTest, FormatsNamespaces) {
1990 verifyFormat("namespace some_namespace {\n"
1991 "class A {};\n"
1992 "void f() { f(); }\n"
1993 "}");
1994 verifyFormat("namespace {\n"
1995 "class A {};\n"
1996 "void f() { f(); }\n"
1997 "}");
1998 verifyFormat("inline namespace X {\n"
1999 "class A {};\n"
2000 "void f() { f(); }\n"
2001 "}");
2002 verifyFormat("using namespace some_namespace;\n"
2003 "class A {};\n"
2004 "void f() { f(); }");
2005
2006 // This code is more common than we thought; if we
2007 // layout this correctly the semicolon will go into
2008 // its own line, which is undesirable.
2009 verifyFormat("namespace {};");
2010 verifyFormat("namespace {\n"
2011 "class A {};\n"
2012 "};");
2013
2014 verifyFormat("namespace {\n"
2015 "int SomeVariable = 0; // comment\n"
2016 "} // namespace");
2017 EXPECT_EQ("#ifndef HEADER_GUARD\n"
2018 "#define HEADER_GUARD\n"
2019 "namespace my_namespace {\n"
2020 "int i;\n"
2021 "} // my_namespace\n"
2022 "#endif // HEADER_GUARD",
2023 format("#ifndef HEADER_GUARD\n"
2024 " #define HEADER_GUARD\n"
2025 " namespace my_namespace {\n"
2026 "int i;\n"
2027 "} // my_namespace\n"
2028 "#endif // HEADER_GUARD"));
2029
2030 FormatStyle Style = getLLVMStyle();
2031 Style.NamespaceIndentation = FormatStyle::NI_All;
2032 EXPECT_EQ("namespace out {\n"
2033 " int i;\n"
2034 " namespace in {\n"
2035 " int i;\n"
2036 " } // namespace\n"
2037 "} // namespace",
2038 format("namespace out {\n"
2039 "int i;\n"
2040 "namespace in {\n"
2041 "int i;\n"
2042 "} // namespace\n"
2043 "} // namespace",
2044 Style));
2045
2046 Style.NamespaceIndentation = FormatStyle::NI_Inner;
2047 EXPECT_EQ("namespace out {\n"
2048 "int i;\n"
2049 "namespace in {\n"
2050 " int i;\n"
2051 "} // namespace\n"
2052 "} // namespace",
2053 format("namespace out {\n"
2054 "int i;\n"
2055 "namespace in {\n"
2056 "int i;\n"
2057 "} // namespace\n"
2058 "} // namespace",
2059 Style));
2060 }
2061
TEST_F(FormatTest,FormatsExternC)2062 TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); }
2063
TEST_F(FormatTest,FormatsInlineASM)2064 TEST_F(FormatTest, FormatsInlineASM) {
2065 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
2066 verifyFormat(
2067 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
2068 " \"cpuid\\n\\t\"\n"
2069 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
2070 " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
2071 " : \"a\"(value));");
2072 }
2073
TEST_F(FormatTest,FormatTryCatch)2074 TEST_F(FormatTest, FormatTryCatch) {
2075 verifyFormat("try {\n"
2076 " throw a * b;\n"
2077 "} catch (int a) {\n"
2078 " // Do nothing.\n"
2079 "} catch (...) {\n"
2080 " exit(42);\n"
2081 "}");
2082
2083 // Function-level try statements.
2084 verifyFormat("int f() try { return 4; } catch (...) {\n"
2085 " return 5;\n"
2086 "}");
2087 verifyFormat("class A {\n"
2088 " int a;\n"
2089 " A() try : a(0) {\n"
2090 " } catch (...) {\n"
2091 " throw;\n"
2092 " }\n"
2093 "};\n");
2094 }
2095
TEST_F(FormatTest,IncompleteTryCatchBlocks)2096 TEST_F(FormatTest, IncompleteTryCatchBlocks) {
2097 verifyFormat("try {\n"
2098 " f();\n"
2099 "} catch {\n"
2100 " g();\n"
2101 "}");
2102 verifyFormat("try {\n"
2103 " f();\n"
2104 "} catch (A a) MACRO(x) {\n"
2105 " g();\n"
2106 "} catch (B b) MACRO(x) {\n"
2107 " g();\n"
2108 "}");
2109 }
2110
TEST_F(FormatTest,FormatTryCatchBraceStyles)2111 TEST_F(FormatTest, FormatTryCatchBraceStyles) {
2112 FormatStyle Style = getLLVMStyle();
2113 Style.BreakBeforeBraces = FormatStyle::BS_Attach;
2114 verifyFormat("try {\n"
2115 " // something\n"
2116 "} catch (...) {\n"
2117 " // something\n"
2118 "}",
2119 Style);
2120 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
2121 verifyFormat("try {\n"
2122 " // something\n"
2123 "}\n"
2124 "catch (...) {\n"
2125 " // something\n"
2126 "}",
2127 Style);
2128 Style.BreakBeforeBraces = FormatStyle::BS_Allman;
2129 verifyFormat("try\n"
2130 "{\n"
2131 " // something\n"
2132 "}\n"
2133 "catch (...)\n"
2134 "{\n"
2135 " // something\n"
2136 "}",
2137 Style);
2138 Style.BreakBeforeBraces = FormatStyle::BS_GNU;
2139 verifyFormat("try\n"
2140 " {\n"
2141 " // something\n"
2142 " }\n"
2143 "catch (...)\n"
2144 " {\n"
2145 " // something\n"
2146 " }",
2147 Style);
2148 }
2149
TEST_F(FormatTest,FormatObjCTryCatch)2150 TEST_F(FormatTest, FormatObjCTryCatch) {
2151 verifyFormat("@try {\n"
2152 " f();\n"
2153 "}\n"
2154 "@catch (NSException e) {\n"
2155 " @throw;\n"
2156 "}\n"
2157 "@finally {\n"
2158 " exit(42);\n"
2159 "}");
2160 }
2161
TEST_F(FormatTest,StaticInitializers)2162 TEST_F(FormatTest, StaticInitializers) {
2163 verifyFormat("static SomeClass SC = {1, 'a'};");
2164
2165 verifyFormat(
2166 "static SomeClass WithALoooooooooooooooooooongName = {\n"
2167 " 100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
2168
2169 // Here, everything other than the "}" would fit on a line.
2170 verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
2171 " 10000000000000000000000000};");
2172 EXPECT_EQ("S s = {a,\n"
2173 "\n"
2174 " b};",
2175 format("S s = {\n"
2176 " a,\n"
2177 "\n"
2178 " b\n"
2179 "};"));
2180
2181 // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
2182 // line. However, the formatting looks a bit off and this probably doesn't
2183 // happen often in practice.
2184 verifyFormat("static int Variable[1] = {\n"
2185 " {1000000000000000000000000000000000000}};",
2186 getLLVMStyleWithColumns(40));
2187 }
2188
TEST_F(FormatTest,DesignatedInitializers)2189 TEST_F(FormatTest, DesignatedInitializers) {
2190 verifyFormat("const struct A a = {.a = 1, .b = 2};");
2191 verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
2192 " .bbbbbbbbbb = 2,\n"
2193 " .cccccccccc = 3,\n"
2194 " .dddddddddd = 4,\n"
2195 " .eeeeeeeeee = 5};");
2196 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
2197 " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
2198 " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
2199 " .ccccccccccccccccccccccccccc = 3,\n"
2200 " .ddddddddddddddddddddddddddd = 4,\n"
2201 " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
2202
2203 verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
2204 }
2205
TEST_F(FormatTest,NestedStaticInitializers)2206 TEST_F(FormatTest, NestedStaticInitializers) {
2207 verifyFormat("static A x = {{{}}};\n");
2208 verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
2209 " {init1, init2, init3, init4}}};",
2210 getLLVMStyleWithColumns(50));
2211
2212 verifyFormat("somes Status::global_reps[3] = {\n"
2213 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
2214 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
2215 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
2216 getLLVMStyleWithColumns(60));
2217 verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
2218 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
2219 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
2220 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
2221 verifyFormat(
2222 "CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
2223 " {rect.fRight - rect.fLeft, rect.fBottom - rect.fTop}};");
2224
2225 verifyFormat(
2226 "SomeArrayOfSomeType a = {\n"
2227 " {{1, 2, 3},\n"
2228 " {1, 2, 3},\n"
2229 " {111111111111111111111111111111, 222222222222222222222222222222,\n"
2230 " 333333333333333333333333333333},\n"
2231 " {1, 2, 3},\n"
2232 " {1, 2, 3}}};");
2233 verifyFormat(
2234 "SomeArrayOfSomeType a = {\n"
2235 " {{1, 2, 3}},\n"
2236 " {{1, 2, 3}},\n"
2237 " {{111111111111111111111111111111, 222222222222222222222222222222,\n"
2238 " 333333333333333333333333333333}},\n"
2239 " {{1, 2, 3}},\n"
2240 " {{1, 2, 3}}};");
2241
2242 verifyFormat(
2243 "struct {\n"
2244 " unsigned bit;\n"
2245 " const char *const name;\n"
2246 "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
2247 " {kOsWin, \"Windows\"},\n"
2248 " {kOsLinux, \"Linux\"},\n"
2249 " {kOsCrOS, \"Chrome OS\"}};");
2250 }
2251
TEST_F(FormatTest,FormatsSmallMacroDefinitionsInSingleLine)2252 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
2253 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
2254 " \\\n"
2255 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
2256 }
2257
TEST_F(FormatTest,DoesNotBreakPureVirtualFunctionDefinition)2258 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
2259 verifyFormat("virtual void write(ELFWriter *writerrr,\n"
2260 " OwningPtr<FileOutputBuffer> &buffer) = 0;");
2261 }
2262
TEST_F(FormatTest,BreaksStringLiteralsOnlyInDefine)2263 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
2264 verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
2265 getLLVMStyleWithColumns(40));
2266 verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
2267 getLLVMStyleWithColumns(40));
2268 EXPECT_EQ("#define Q \\\n"
2269 " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n"
2270 " \"aaaaaaaa.cpp\"",
2271 format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
2272 getLLVMStyleWithColumns(40)));
2273 }
2274
TEST_F(FormatTest,UnderstandsLinePPDirective)2275 TEST_F(FormatTest, UnderstandsLinePPDirective) {
2276 EXPECT_EQ("# 123 \"A string literal\"",
2277 format(" # 123 \"A string literal\""));
2278 }
2279
TEST_F(FormatTest,LayoutUnknownPPDirective)2280 TEST_F(FormatTest, LayoutUnknownPPDirective) {
2281 EXPECT_EQ("#;", format("#;"));
2282 verifyFormat("#\n;\n;\n;");
2283 }
2284
TEST_F(FormatTest,UnescapedEndOfLineEndsPPDirective)2285 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
2286 EXPECT_EQ("#line 42 \"test\"\n",
2287 format("# \\\n line \\\n 42 \\\n \"test\"\n"));
2288 EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n",
2289 getLLVMStyleWithColumns(12)));
2290 }
2291
TEST_F(FormatTest,EndOfFileEndsPPDirective)2292 TEST_F(FormatTest, EndOfFileEndsPPDirective) {
2293 EXPECT_EQ("#line 42 \"test\"",
2294 format("# \\\n line \\\n 42 \\\n \"test\""));
2295 EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B"));
2296 }
2297
TEST_F(FormatTest,DoesntRemoveUnknownTokens)2298 TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
2299 verifyFormat("#define A \\x20");
2300 verifyFormat("#define A \\ x20");
2301 EXPECT_EQ("#define A \\ x20", format("#define A \\ x20"));
2302 verifyFormat("#define A ''");
2303 verifyFormat("#define A ''qqq");
2304 verifyFormat("#define A `qqq");
2305 verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
2306 EXPECT_EQ("const char *c = STRINGIFY(\n"
2307 "\\na : b);",
2308 format("const char * c = STRINGIFY(\n"
2309 "\\na : b);"));
2310 }
2311
TEST_F(FormatTest,IndentsPPDirectiveInReducedSpace)2312 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
2313 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
2314 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12));
2315 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12));
2316 // FIXME: We never break before the macro name.
2317 verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12));
2318
2319 verifyFormat("#define A A\n#define A A");
2320 verifyFormat("#define A(X) A\n#define A A");
2321
2322 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
2323 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22));
2324 }
2325
TEST_F(FormatTest,HandlePreprocessorDirectiveContext)2326 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
2327 EXPECT_EQ("// somecomment\n"
2328 "#include \"a.h\"\n"
2329 "#define A( \\\n"
2330 " A, B)\n"
2331 "#include \"b.h\"\n"
2332 "// somecomment\n",
2333 format(" // somecomment\n"
2334 " #include \"a.h\"\n"
2335 "#define A(A,\\\n"
2336 " B)\n"
2337 " #include \"b.h\"\n"
2338 " // somecomment\n",
2339 getLLVMStyleWithColumns(13)));
2340 }
2341
TEST_F(FormatTest,LayoutSingleHash)2342 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
2343
TEST_F(FormatTest,LayoutCodeInMacroDefinitions)2344 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
2345 EXPECT_EQ("#define A \\\n"
2346 " c; \\\n"
2347 " e;\n"
2348 "f;",
2349 format("#define A c; e;\n"
2350 "f;",
2351 getLLVMStyleWithColumns(14)));
2352 }
2353
TEST_F(FormatTest,LayoutRemainingTokens)2354 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
2355
TEST_F(FormatTest,AlwaysFormatsEntireMacroDefinitions)2356 TEST_F(FormatTest, AlwaysFormatsEntireMacroDefinitions) {
2357 EXPECT_EQ("int i;\n"
2358 "#define A \\\n"
2359 " int i; \\\n"
2360 " int j\n"
2361 "int k;",
2362 format("int i;\n"
2363 "#define A \\\n"
2364 " int i ; \\\n"
2365 " int j\n"
2366 "int k;",
2367 8, 0, getGoogleStyle())); // 8: position of "#define".
2368 EXPECT_EQ("int i;\n"
2369 "#define A \\\n"
2370 " int i; \\\n"
2371 " int j\n"
2372 "int k;",
2373 format("int i;\n"
2374 "#define A \\\n"
2375 " int i ; \\\n"
2376 " int j\n"
2377 "int k;",
2378 45, 0, getGoogleStyle())); // 45: position of "j".
2379 }
2380
TEST_F(FormatTest,MacroDefinitionInsideStatement)2381 TEST_F(FormatTest, MacroDefinitionInsideStatement) {
2382 EXPECT_EQ("int x,\n"
2383 "#define A\n"
2384 " y;",
2385 format("int x,\n#define A\ny;"));
2386 }
2387
TEST_F(FormatTest,HashInMacroDefinition)2388 TEST_F(FormatTest, HashInMacroDefinition) {
2389 EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
2390 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11));
2391 verifyFormat("#define A \\\n"
2392 " { \\\n"
2393 " f(#c); \\\n"
2394 " }",
2395 getLLVMStyleWithColumns(11));
2396
2397 verifyFormat("#define A(X) \\\n"
2398 " void function##X()",
2399 getLLVMStyleWithColumns(22));
2400
2401 verifyFormat("#define A(a, b, c) \\\n"
2402 " void a##b##c()",
2403 getLLVMStyleWithColumns(22));
2404
2405 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
2406 }
2407
TEST_F(FormatTest,RespectWhitespaceInMacroDefinitions)2408 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
2409 EXPECT_EQ("#define A (x)", format("#define A (x)"));
2410 EXPECT_EQ("#define A(x)", format("#define A(x)"));
2411 }
2412
TEST_F(FormatTest,EmptyLinesInMacroDefinitions)2413 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
2414 EXPECT_EQ("#define A b;", format("#define A \\\n"
2415 " \\\n"
2416 " b;",
2417 getLLVMStyleWithColumns(25)));
2418 EXPECT_EQ("#define A \\\n"
2419 " \\\n"
2420 " a; \\\n"
2421 " b;",
2422 format("#define A \\\n"
2423 " \\\n"
2424 " a; \\\n"
2425 " b;",
2426 getLLVMStyleWithColumns(11)));
2427 EXPECT_EQ("#define A \\\n"
2428 " a; \\\n"
2429 " \\\n"
2430 " b;",
2431 format("#define A \\\n"
2432 " a; \\\n"
2433 " \\\n"
2434 " b;",
2435 getLLVMStyleWithColumns(11)));
2436 }
2437
TEST_F(FormatTest,MacroDefinitionsWithIncompleteCode)2438 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
2439 verifyFormat("#define A :");
2440 verifyFormat("#define SOMECASES \\\n"
2441 " case 1: \\\n"
2442 " case 2\n",
2443 getLLVMStyleWithColumns(20));
2444 verifyFormat("#define A template <typename T>");
2445 verifyFormat("#define STR(x) #x\n"
2446 "f(STR(this_is_a_string_literal{));");
2447 verifyFormat("#pragma omp threadprivate( \\\n"
2448 " y)), // expected-warning",
2449 getLLVMStyleWithColumns(28));
2450 }
2451
TEST_F(FormatTest,MacrosWithoutTrailingSemicolon)2452 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
2453 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
2454 EXPECT_EQ("class A : public QObject {\n"
2455 " Q_OBJECT\n"
2456 "\n"
2457 " A() {}\n"
2458 "};",
2459 format("class A : public QObject {\n"
2460 " Q_OBJECT\n"
2461 "\n"
2462 " A() {\n}\n"
2463 "} ;"));
2464 EXPECT_EQ("SOME_MACRO\n"
2465 "namespace {\n"
2466 "void f();\n"
2467 "}",
2468 format("SOME_MACRO\n"
2469 " namespace {\n"
2470 "void f( );\n"
2471 "}"));
2472 // Only if the identifier contains at least 5 characters.
2473 EXPECT_EQ("HTTP f();",
2474 format("HTTP\nf();"));
2475 EXPECT_EQ("MACRO\nf();",
2476 format("MACRO\nf();"));
2477 // Only if everything is upper case.
2478 EXPECT_EQ("class A : public QObject {\n"
2479 " Q_Object A() {}\n"
2480 "};",
2481 format("class A : public QObject {\n"
2482 " Q_Object\n"
2483 " A() {\n}\n"
2484 "} ;"));
2485 }
2486
TEST_F(FormatTest,MacroCallsWithoutTrailingSemicolon)2487 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
2488 EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
2489 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
2490 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
2491 "class X {};\n"
2492 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
2493 "int *createScopDetectionPass() { return 0; }",
2494 format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
2495 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
2496 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
2497 " class X {};\n"
2498 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
2499 " int *createScopDetectionPass() { return 0; }"));
2500 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
2501 // braces, so that inner block is indented one level more.
2502 EXPECT_EQ("int q() {\n"
2503 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
2504 " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
2505 " IPC_END_MESSAGE_MAP()\n"
2506 "}",
2507 format("int q() {\n"
2508 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
2509 " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
2510 " IPC_END_MESSAGE_MAP()\n"
2511 "}"));
2512
2513 // Same inside macros.
2514 EXPECT_EQ("#define LIST(L) \\\n"
2515 " L(A) \\\n"
2516 " L(B) \\\n"
2517 " L(C)",
2518 format("#define LIST(L) \\\n"
2519 " L(A) \\\n"
2520 " L(B) \\\n"
2521 " L(C)",
2522 getGoogleStyle()));
2523
2524 // These must not be recognized as macros.
2525 EXPECT_EQ("int q() {\n"
2526 " f(x);\n"
2527 " f(x) {}\n"
2528 " f(x)->g();\n"
2529 " f(x)->*g();\n"
2530 " f(x).g();\n"
2531 " f(x) = x;\n"
2532 " f(x) += x;\n"
2533 " f(x) -= x;\n"
2534 " f(x) *= x;\n"
2535 " f(x) /= x;\n"
2536 " f(x) %= x;\n"
2537 " f(x) &= x;\n"
2538 " f(x) |= x;\n"
2539 " f(x) ^= x;\n"
2540 " f(x) >>= x;\n"
2541 " f(x) <<= x;\n"
2542 " f(x)[y].z();\n"
2543 " LOG(INFO) << x;\n"
2544 " ifstream(x) >> x;\n"
2545 "}\n",
2546 format("int q() {\n"
2547 " f(x)\n;\n"
2548 " f(x)\n {}\n"
2549 " f(x)\n->g();\n"
2550 " f(x)\n->*g();\n"
2551 " f(x)\n.g();\n"
2552 " f(x)\n = x;\n"
2553 " f(x)\n += x;\n"
2554 " f(x)\n -= x;\n"
2555 " f(x)\n *= x;\n"
2556 " f(x)\n /= x;\n"
2557 " f(x)\n %= x;\n"
2558 " f(x)\n &= x;\n"
2559 " f(x)\n |= x;\n"
2560 " f(x)\n ^= x;\n"
2561 " f(x)\n >>= x;\n"
2562 " f(x)\n <<= x;\n"
2563 " f(x)\n[y].z();\n"
2564 " LOG(INFO)\n << x;\n"
2565 " ifstream(x)\n >> x;\n"
2566 "}\n"));
2567 EXPECT_EQ("int q() {\n"
2568 " F(x)\n"
2569 " if (1) {\n"
2570 " }\n"
2571 " F(x)\n"
2572 " while (1) {\n"
2573 " }\n"
2574 " F(x)\n"
2575 " G(x);\n"
2576 " F(x)\n"
2577 " try {\n"
2578 " Q();\n"
2579 " } catch (...) {\n"
2580 " }\n"
2581 "}\n",
2582 format("int q() {\n"
2583 "F(x)\n"
2584 "if (1) {}\n"
2585 "F(x)\n"
2586 "while (1) {}\n"
2587 "F(x)\n"
2588 "G(x);\n"
2589 "F(x)\n"
2590 "try { Q(); } catch (...) {}\n"
2591 "}\n"));
2592 EXPECT_EQ("class A {\n"
2593 " A() : t(0) {}\n"
2594 " A(int i) noexcept() : {}\n"
2595 " A(X x)\n" // FIXME: function-level try blocks are broken.
2596 " try : t(0) {\n"
2597 " } catch (...) {\n"
2598 " }\n"
2599 "};",
2600 format("class A {\n"
2601 " A()\n : t(0) {}\n"
2602 " A(int i)\n noexcept() : {}\n"
2603 " A(X x)\n"
2604 " try : t(0) {} catch (...) {}\n"
2605 "};"));
2606 EXPECT_EQ(
2607 "class SomeClass {\n"
2608 "public:\n"
2609 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2610 "};",
2611 format("class SomeClass {\n"
2612 "public:\n"
2613 " SomeClass()\n"
2614 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2615 "};"));
2616 EXPECT_EQ(
2617 "class SomeClass {\n"
2618 "public:\n"
2619 " SomeClass()\n"
2620 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2621 "};",
2622 format("class SomeClass {\n"
2623 "public:\n"
2624 " SomeClass()\n"
2625 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2626 "};", getLLVMStyleWithColumns(40)));
2627 }
2628
TEST_F(FormatTest,LayoutMacroDefinitionsStatementsSpanningBlocks)2629 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
2630 verifyFormat("#define A \\\n"
2631 " f({ \\\n"
2632 " g(); \\\n"
2633 " });", getLLVMStyleWithColumns(11));
2634 }
2635
TEST_F(FormatTest,IndentPreprocessorDirectivesAtZero)2636 TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
2637 EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}"));
2638 }
2639
TEST_F(FormatTest,FormatHashIfNotAtStartOfLine)2640 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
2641 verifyFormat("{\n { a #c; }\n}");
2642 }
2643
TEST_F(FormatTest,FormatUnbalancedStructuralElements)2644 TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
2645 EXPECT_EQ("#define A \\\n { \\\n {\nint i;",
2646 format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
2647 EXPECT_EQ("#define A \\\n } \\\n }\nint i;",
2648 format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
2649 }
2650
TEST_F(FormatTest,EscapedNewlineAtStartOfToken)2651 TEST_F(FormatTest, EscapedNewlineAtStartOfToken) {
2652 EXPECT_EQ(
2653 "#define A \\\n int i; \\\n int j;",
2654 format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11)));
2655 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
2656 }
2657
TEST_F(FormatTest,NoEscapedNewlineHandlingInBlockComments)2658 TEST_F(FormatTest, NoEscapedNewlineHandlingInBlockComments) {
2659 EXPECT_EQ("/* \\ \\ \\\n*/", format("\\\n/* \\ \\ \\\n*/"));
2660 }
2661
TEST_F(FormatTest,CalculateSpaceOnConsecutiveLinesInMacro)2662 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
2663 verifyFormat("#define A \\\n"
2664 " int v( \\\n"
2665 " a); \\\n"
2666 " int i;",
2667 getLLVMStyleWithColumns(11));
2668 }
2669
TEST_F(FormatTest,MixingPreprocessorDirectivesAndNormalCode)2670 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
2671 EXPECT_EQ(
2672 "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
2673 " \\\n"
2674 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
2675 "\n"
2676 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
2677 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
2678 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro("
2679 "\\\n"
2680 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
2681 " \n"
2682 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
2683 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
2684 }
2685
TEST_F(FormatTest,LayoutStatementsAroundPreprocessorDirectives)2686 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
2687 EXPECT_EQ("int\n"
2688 "#define A\n"
2689 " a;",
2690 format("int\n#define A\na;"));
2691 verifyFormat("functionCallTo(\n"
2692 " someOtherFunction(\n"
2693 " withSomeParameters, whichInSequence,\n"
2694 " areLongerThanALine(andAnotherCall,\n"
2695 "#define A B\n"
2696 " withMoreParamters,\n"
2697 " whichStronglyInfluenceTheLayout),\n"
2698 " andMoreParameters),\n"
2699 " trailing);",
2700 getLLVMStyleWithColumns(69));
2701 verifyFormat("Foo::Foo()\n"
2702 "#ifdef BAR\n"
2703 " : baz(0)\n"
2704 "#endif\n"
2705 "{\n"
2706 "}");
2707 verifyFormat("void f() {\n"
2708 " if (true)\n"
2709 "#ifdef A\n"
2710 " f(42);\n"
2711 " x();\n"
2712 "#else\n"
2713 " g();\n"
2714 " x();\n"
2715 "#endif\n"
2716 "}");
2717 verifyFormat("void f(param1, param2,\n"
2718 " param3,\n"
2719 "#ifdef A\n"
2720 " param4(param5,\n"
2721 "#ifdef A1\n"
2722 " param6,\n"
2723 "#ifdef A2\n"
2724 " param7),\n"
2725 "#else\n"
2726 " param8),\n"
2727 " param9,\n"
2728 "#endif\n"
2729 " param10,\n"
2730 "#endif\n"
2731 " param11)\n"
2732 "#else\n"
2733 " param12)\n"
2734 "#endif\n"
2735 "{\n"
2736 " x();\n"
2737 "}",
2738 getLLVMStyleWithColumns(28));
2739 verifyFormat("#if 1\n"
2740 "int i;");
2741 verifyFormat(
2742 "#if 1\n"
2743 "#endif\n"
2744 "#if 1\n"
2745 "#else\n"
2746 "#endif\n");
2747 verifyFormat("DEBUG({\n"
2748 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2749 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
2750 "});\n"
2751 "#if a\n"
2752 "#else\n"
2753 "#endif");
2754 }
2755
TEST_F(FormatTest,GraciouslyHandleIncorrectPreprocessorConditions)2756 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
2757 verifyFormat("#endif\n"
2758 "#if B");
2759 }
2760
TEST_F(FormatTest,FormatsJoinedLinesOnSubsequentRuns)2761 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
2762 FormatStyle SingleLine = getLLVMStyle();
2763 SingleLine.AllowShortIfStatementsOnASingleLine = true;
2764 verifyFormat(
2765 "#if 0\n"
2766 "#elif 1\n"
2767 "#endif\n"
2768 "void foo() {\n"
2769 " if (test) foo2();\n"
2770 "}",
2771 SingleLine);
2772 }
2773
TEST_F(FormatTest,LayoutBlockInsideParens)2774 TEST_F(FormatTest, LayoutBlockInsideParens) {
2775 EXPECT_EQ("functionCall({ int i; });", format(" functionCall ( {int i;} );"));
2776 EXPECT_EQ("functionCall({\n"
2777 " int i;\n"
2778 " int j;\n"
2779 "});",
2780 format(" functionCall ( {int i;int j;} );"));
2781 EXPECT_EQ("functionCall({\n"
2782 " int i;\n"
2783 " int j;\n"
2784 " },\n"
2785 " aaaa, bbbb, cccc);",
2786 format(" functionCall ( {int i;int j;}, aaaa, bbbb, cccc);"));
2787 EXPECT_EQ("functionCall(aaaa, bbbb, { int i; });",
2788 format(" functionCall (aaaa, bbbb, {int i;});"));
2789 EXPECT_EQ("functionCall(aaaa, bbbb, {\n"
2790 " int i;\n"
2791 " int j;\n"
2792 "});",
2793 format(" functionCall (aaaa, bbbb, {int i;int j;});"));
2794 EXPECT_EQ("functionCall(aaaa, bbbb, { int i; });",
2795 format(" functionCall (aaaa, bbbb, {int i;});"));
2796 verifyFormat(
2797 "Aaa({\n"
2798 " int i; // break\n"
2799 " },\n"
2800 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
2801 " ccccccccccccccccc));");
2802 verifyFormat("DEBUG({\n"
2803 " if (a)\n"
2804 " f();\n"
2805 "});");
2806 }
2807
TEST_F(FormatTest,LayoutBlockInsideStatement)2808 TEST_F(FormatTest, LayoutBlockInsideStatement) {
2809 EXPECT_EQ("SOME_MACRO { int i; }\n"
2810 "int i;",
2811 format(" SOME_MACRO {int i;} int i;"));
2812 }
2813
TEST_F(FormatTest,LayoutNestedBlocks)2814 TEST_F(FormatTest, LayoutNestedBlocks) {
2815 verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
2816 " struct s {\n"
2817 " int i;\n"
2818 " };\n"
2819 " s kBitsToOs[] = {{10}};\n"
2820 " for (int i = 0; i < 10; ++i)\n"
2821 " return;\n"
2822 "}");
2823 verifyFormat("call(parameter, {\n"
2824 " something();\n"
2825 " // Comment using all columns.\n"
2826 " somethingelse();\n"
2827 "});",
2828 getLLVMStyleWithColumns(40));
2829 verifyFormat("DEBUG( //\n"
2830 " { f(); }, a);");
2831 verifyFormat("DEBUG( //\n"
2832 " {\n"
2833 " f(); //\n"
2834 " },\n"
2835 " a);");
2836
2837 EXPECT_EQ("call(parameter, {\n"
2838 " something();\n"
2839 " // Comment too\n"
2840 " // looooooooooong.\n"
2841 " somethingElse();\n"
2842 "});",
2843 format("call(parameter, {\n"
2844 " something();\n"
2845 " // Comment too looooooooooong.\n"
2846 " somethingElse();\n"
2847 "});",
2848 getLLVMStyleWithColumns(29)));
2849 EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });"));
2850 EXPECT_EQ("DEBUG({ // comment\n"
2851 " int i;\n"
2852 "});",
2853 format("DEBUG({ // comment\n"
2854 "int i;\n"
2855 "});"));
2856 EXPECT_EQ("DEBUG({\n"
2857 " int i;\n"
2858 "\n"
2859 " // comment\n"
2860 " int j;\n"
2861 "});",
2862 format("DEBUG({\n"
2863 " int i;\n"
2864 "\n"
2865 " // comment\n"
2866 " int j;\n"
2867 "});"));
2868
2869 verifyFormat("DEBUG({\n"
2870 " if (a)\n"
2871 " return;\n"
2872 "});");
2873 verifyGoogleFormat("DEBUG({\n"
2874 " if (a) return;\n"
2875 "});");
2876 FormatStyle Style = getGoogleStyle();
2877 Style.ColumnLimit = 45;
2878 verifyFormat("Debug(aaaaa, {\n"
2879 " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n"
2880 " return;\n"
2881 " },\n"
2882 " a);", Style);
2883 }
2884
TEST_F(FormatTest,IndividualStatementsOfNestedBlocks)2885 TEST_F(FormatTest, IndividualStatementsOfNestedBlocks) {
2886 EXPECT_EQ("DEBUG({\n"
2887 " int i;\n"
2888 " int j;\n"
2889 "});",
2890 format("DEBUG( {\n"
2891 " int i;\n"
2892 " int j;\n"
2893 "} ) ;",
2894 20, 1, getLLVMStyle()));
2895 EXPECT_EQ("DEBUG( {\n"
2896 " int i;\n"
2897 " int j;\n"
2898 "} ) ;",
2899 format("DEBUG( {\n"
2900 " int i;\n"
2901 " int j;\n"
2902 "} ) ;",
2903 41, 1, getLLVMStyle()));
2904 EXPECT_EQ("DEBUG( {\n"
2905 " int i;\n"
2906 " int j;\n"
2907 "} ) ;",
2908 format("DEBUG( {\n"
2909 " int i;\n"
2910 " int j;\n"
2911 "} ) ;",
2912 41, 1, getLLVMStyle()));
2913 EXPECT_EQ("DEBUG({\n"
2914 " int i;\n"
2915 " int j;\n"
2916 "});",
2917 format("DEBUG( {\n"
2918 " int i;\n"
2919 " int j;\n"
2920 "} ) ;",
2921 20, 1, getLLVMStyle()));
2922
2923 EXPECT_EQ("Debug({\n"
2924 " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n"
2925 " return;\n"
2926 " },\n"
2927 " a);",
2928 format("Debug({\n"
2929 " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n"
2930 " return;\n"
2931 " },\n"
2932 " a);",
2933 50, 1, getLLVMStyle()));
2934 EXPECT_EQ("DEBUG({\n"
2935 " DEBUG({\n"
2936 " int a;\n"
2937 " int b;\n"
2938 " }) ;\n"
2939 "});",
2940 format("DEBUG({\n"
2941 " DEBUG({\n"
2942 " int a;\n"
2943 " int b;\n" // Format this line only.
2944 " }) ;\n" // Don't touch this line.
2945 "});",
2946 35, 0, getLLVMStyle()));
2947 EXPECT_EQ("DEBUG({\n"
2948 " int a; //\n"
2949 "});",
2950 format("DEBUG({\n"
2951 " int a; //\n"
2952 "});",
2953 0, 0, getLLVMStyle()));
2954 }
2955
TEST_F(FormatTest,PutEmptyBlocksIntoOneLine)2956 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
2957 EXPECT_EQ("{}", format("{}"));
2958 verifyFormat("enum E {};");
2959 verifyFormat("enum E {}");
2960 }
2961
2962 //===----------------------------------------------------------------------===//
2963 // Line break tests.
2964 //===----------------------------------------------------------------------===//
2965
TEST_F(FormatTest,PreventConfusingIndents)2966 TEST_F(FormatTest, PreventConfusingIndents) {
2967 verifyFormat(
2968 "void f() {\n"
2969 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
2970 " parameter, parameter, parameter)),\n"
2971 " SecondLongCall(parameter));\n"
2972 "}");
2973 verifyFormat(
2974 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2975 " aaaaaaaaaaaaaaaaaaaaaaaa(\n"
2976 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
2977 " aaaaaaaaaaaaaaaaaaaaaaaa);");
2978 verifyFormat(
2979 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2980 " [aaaaaaaaaaaaaaaaaaaaaaaa\n"
2981 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
2982 " [aaaaaaaaaaaaaaaaaaaaaaaa]];");
2983 verifyFormat(
2984 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
2985 " aaaaaaaaaaaaaaaaaaaaaaaa<\n"
2986 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
2987 " aaaaaaaaaaaaaaaaaaaaaaaa>;");
2988 verifyFormat("int a = bbbb && ccc && fffff(\n"
2989 "#define A Just forcing a new line\n"
2990 " ddd);");
2991 }
2992
TEST_F(FormatTest,LineBreakingInBinaryExpressions)2993 TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
2994 verifyFormat(
2995 "bool aaaaaaa =\n"
2996 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
2997 " bbbbbbbb();");
2998 verifyFormat(
2999 "bool aaaaaaa =\n"
3000 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
3001 " bbbbbbbb();");
3002
3003 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
3004 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
3005 " ccccccccc == ddddddddddd;");
3006 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
3007 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
3008 " ccccccccc == ddddddddddd;");
3009 verifyFormat(
3010 "bool aaaaaaaaaaaaaaaaaaaaa =\n"
3011 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
3012 " ccccccccc == ddddddddddd;");
3013
3014 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
3015 " aaaaaa) &&\n"
3016 " bbbbbb && cccccc;");
3017 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
3018 " aaaaaa) >>\n"
3019 " bbbbbb;");
3020 verifyFormat("Whitespaces.addUntouchableComment(\n"
3021 " SourceMgr.getSpellingColumnNumber(\n"
3022 " TheLine.Last->FormatTok.Tok.getLocation()) -\n"
3023 " 1);");
3024
3025 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3026 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
3027 " cccccc) {\n}");
3028
3029 // If the LHS of a comparison is not a binary expression itself, the
3030 // additional linebreak confuses many people.
3031 verifyFormat(
3032 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3033 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
3034 "}");
3035 verifyFormat(
3036 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3037 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3038 "}");
3039 verifyFormat(
3040 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
3041 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3042 "}");
3043 // Even explicit parentheses stress the precedence enough to make the
3044 // additional break unnecessary.
3045 verifyFormat(
3046 "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3047 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3048 "}");
3049 // This cases is borderline, but with the indentation it is still readable.
3050 verifyFormat(
3051 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3052 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3053 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
3054 "}",
3055 getLLVMStyleWithColumns(75));
3056
3057 // If the LHS is a binary expression, we should still use the additional break
3058 // as otherwise the formatting hides the operator precedence.
3059 verifyFormat(
3060 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3061 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3062 " 5) {\n"
3063 "}");
3064
3065 FormatStyle OnePerLine = getLLVMStyle();
3066 OnePerLine.BinPackParameters = false;
3067 verifyFormat(
3068 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3069 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3070 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
3071 OnePerLine);
3072 }
3073
TEST_F(FormatTest,ExpressionIndentation)3074 TEST_F(FormatTest, ExpressionIndentation) {
3075 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3076 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3077 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3078 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3079 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
3080 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
3081 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3082 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
3083 " ccccccccccccccccccccccccccccccccccccccccc;");
3084 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3085 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3086 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3087 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3088 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3089 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3090 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3091 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3092 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3093 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3094 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3095 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3096 verifyFormat("if () {\n"
3097 "} else if (aaaaa &&\n"
3098 " bbbbb > // break\n"
3099 " ccccc) {\n"
3100 "}");
3101
3102 // Presence of a trailing comment used to change indentation of b.
3103 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
3104 " b;\n"
3105 "return aaaaaaaaaaaaaaaaaaa +\n"
3106 " b; //",
3107 getLLVMStyleWithColumns(30));
3108 }
3109
TEST_F(FormatTest,ExpressionIndentationBreakingBeforeOperators)3110 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
3111 // Not sure what the best system is here. Like this, the LHS can be found
3112 // immediately above an operator (everything with the same or a higher
3113 // indent). The RHS is aligned right of the operator and so compasses
3114 // everything until something with the same indent as the operator is found.
3115 // FIXME: Is this a good system?
3116 FormatStyle Style = getLLVMStyle();
3117 Style.BreakBeforeBinaryOperators = true;
3118 verifyFormat(
3119 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3120 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3121 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3122 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3123 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3124 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3125 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3126 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3127 " > ccccccccccccccccccccccccccccccccccccccccc;",
3128 Style);
3129 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3130 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3131 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3132 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3133 Style);
3134 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3135 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3136 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3137 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3138 Style);
3139 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3140 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3141 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3142 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3143 Style);
3144 verifyFormat("if () {\n"
3145 "} else if (aaaaa\n"
3146 " && bbbbb // break\n"
3147 " > ccccc) {\n"
3148 "}",
3149 Style);
3150
3151 // Forced by comments.
3152 verifyFormat(
3153 "unsigned ContentSize =\n"
3154 " sizeof(int16_t) // DWARF ARange version number\n"
3155 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
3156 " + sizeof(int8_t) // Pointer Size (in bytes)\n"
3157 " + sizeof(int8_t); // Segment Size (in bytes)");
3158
3159 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
3160 " == boost::fusion::at_c<1>(iiii).second;",
3161 Style);
3162
3163 Style.ColumnLimit = 60;
3164 verifyFormat("zzzzzzzzzz\n"
3165 " = bbbbbbbbbbbbbbbbb\n"
3166 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
3167 Style);
3168 }
3169
TEST_F(FormatTest,ConstructorInitializers)3170 TEST_F(FormatTest, ConstructorInitializers) {
3171 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
3172 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
3173 getLLVMStyleWithColumns(45));
3174 verifyFormat("Constructor()\n"
3175 " : Inttializer(FitsOnTheLine) {}",
3176 getLLVMStyleWithColumns(44));
3177 verifyFormat("Constructor()\n"
3178 " : Inttializer(FitsOnTheLine) {}",
3179 getLLVMStyleWithColumns(43));
3180
3181 verifyFormat(
3182 "SomeClass::Constructor()\n"
3183 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
3184
3185 verifyFormat(
3186 "SomeClass::Constructor()\n"
3187 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3188 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
3189 verifyFormat(
3190 "SomeClass::Constructor()\n"
3191 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3192 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
3193
3194 verifyFormat("Constructor()\n"
3195 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3196 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3197 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3198 " aaaaaaaaaaaaaaaaaaaaaaa() {}");
3199
3200 verifyFormat("Constructor()\n"
3201 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3202 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3203
3204 verifyFormat("Constructor(int Parameter = 0)\n"
3205 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
3206 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
3207 verifyFormat("Constructor()\n"
3208 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
3209 "}",
3210 getLLVMStyleWithColumns(60));
3211 verifyFormat("Constructor()\n"
3212 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3213 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
3214
3215 // Here a line could be saved by splitting the second initializer onto two
3216 // lines, but that is not desirable.
3217 verifyFormat("Constructor()\n"
3218 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
3219 " aaaaaaaaaaa(aaaaaaaaaaa),\n"
3220 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3221
3222 FormatStyle OnePerLine = getLLVMStyle();
3223 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
3224 verifyFormat("SomeClass::Constructor()\n"
3225 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3226 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3227 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3228 OnePerLine);
3229 verifyFormat("SomeClass::Constructor()\n"
3230 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
3231 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3232 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3233 OnePerLine);
3234 verifyFormat("MyClass::MyClass(int var)\n"
3235 " : some_var_(var), // 4 space indent\n"
3236 " some_other_var_(var + 1) { // lined up\n"
3237 "}",
3238 OnePerLine);
3239 verifyFormat("Constructor()\n"
3240 " : aaaaa(aaaaaa),\n"
3241 " aaaaa(aaaaaa),\n"
3242 " aaaaa(aaaaaa),\n"
3243 " aaaaa(aaaaaa),\n"
3244 " aaaaa(aaaaaa) {}",
3245 OnePerLine);
3246 verifyFormat("Constructor()\n"
3247 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
3248 " aaaaaaaaaaaaaaaaaaaaaa) {}",
3249 OnePerLine);
3250
3251 EXPECT_EQ("Constructor()\n"
3252 " : // Comment forcing unwanted break.\n"
3253 " aaaa(aaaa) {}",
3254 format("Constructor() :\n"
3255 " // Comment forcing unwanted break.\n"
3256 " aaaa(aaaa) {}"));
3257 }
3258
TEST_F(FormatTest,MemoizationTests)3259 TEST_F(FormatTest, MemoizationTests) {
3260 // This breaks if the memoization lookup does not take \c Indent and
3261 // \c LastSpace into account.
3262 verifyFormat(
3263 "extern CFRunLoopTimerRef\n"
3264 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
3265 " CFTimeInterval interval, CFOptionFlags flags,\n"
3266 " CFIndex order, CFRunLoopTimerCallBack callout,\n"
3267 " CFRunLoopTimerContext *context) {}");
3268
3269 // Deep nesting somewhat works around our memoization.
3270 verifyFormat(
3271 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3272 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3273 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3274 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3275 " aaaaa())))))))))))))))))))))))))))))))))))))));",
3276 getLLVMStyleWithColumns(65));
3277 verifyFormat(
3278 "aaaaa(\n"
3279 " aaaaa,\n"
3280 " aaaaa(\n"
3281 " aaaaa,\n"
3282 " aaaaa(\n"
3283 " aaaaa,\n"
3284 " aaaaa(\n"
3285 " aaaaa,\n"
3286 " aaaaa(\n"
3287 " aaaaa,\n"
3288 " aaaaa(\n"
3289 " aaaaa,\n"
3290 " aaaaa(\n"
3291 " aaaaa,\n"
3292 " aaaaa(\n"
3293 " aaaaa,\n"
3294 " aaaaa(\n"
3295 " aaaaa,\n"
3296 " aaaaa(\n"
3297 " aaaaa,\n"
3298 " aaaaa(\n"
3299 " aaaaa,\n"
3300 " aaaaa(\n"
3301 " aaaaa,\n"
3302 " aaaaa))))))))))));",
3303 getLLVMStyleWithColumns(65));
3304 verifyFormat(
3305 "a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(), a), a), a), a),\n"
3306 " a),\n"
3307 " a),\n"
3308 " a),\n"
3309 " a),\n"
3310 " a),\n"
3311 " a),\n"
3312 " a),\n"
3313 " a),\n"
3314 " a),\n"
3315 " a),\n"
3316 " a),\n"
3317 " a),\n"
3318 " a),\n"
3319 " a),\n"
3320 " a),\n"
3321 " a),\n"
3322 " a)",
3323 getLLVMStyleWithColumns(65));
3324
3325 // This test takes VERY long when memoization is broken.
3326 FormatStyle OnePerLine = getLLVMStyle();
3327 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
3328 OnePerLine.BinPackParameters = false;
3329 std::string input = "Constructor()\n"
3330 " : aaaa(a,\n";
3331 for (unsigned i = 0, e = 80; i != e; ++i) {
3332 input += " a,\n";
3333 }
3334 input += " a) {}";
3335 verifyFormat(input, OnePerLine);
3336 }
3337
TEST_F(FormatTest,BreaksAsHighAsPossible)3338 TEST_F(FormatTest, BreaksAsHighAsPossible) {
3339 verifyFormat(
3340 "void f() {\n"
3341 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
3342 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
3343 " f();\n"
3344 "}");
3345 verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
3346 " Intervals[i - 1].getRange().getLast()) {\n}");
3347 }
3348
TEST_F(FormatTest,BreaksFunctionDeclarations)3349 TEST_F(FormatTest, BreaksFunctionDeclarations) {
3350 // Principially, we break function declarations in a certain order:
3351 // 1) break amongst arguments.
3352 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
3353 " Cccccccccccccc cccccccccccccc);");
3354 verifyFormat(
3355 "template <class TemplateIt>\n"
3356 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
3357 " TemplateIt *stop) {}");
3358
3359 // 2) break after return type.
3360 verifyFormat(
3361 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3362 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
3363 getGoogleStyle());
3364
3365 // 3) break after (.
3366 verifyFormat(
3367 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
3368 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
3369 getGoogleStyle());
3370
3371 // 4) break before after nested name specifiers.
3372 verifyFormat(
3373 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3374 "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
3375 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
3376 getGoogleStyle());
3377
3378 // However, there are exceptions, if a sufficient amount of lines can be
3379 // saved.
3380 // FIXME: The precise cut-offs wrt. the number of saved lines might need some
3381 // more adjusting.
3382 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
3383 " Cccccccccccccc cccccccccc,\n"
3384 " Cccccccccccccc cccccccccc,\n"
3385 " Cccccccccccccc cccccccccc,\n"
3386 " Cccccccccccccc cccccccccc);");
3387 verifyFormat(
3388 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3389 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3390 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3391 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
3392 getGoogleStyle());
3393 verifyFormat(
3394 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
3395 " Cccccccccccccc cccccccccc,\n"
3396 " Cccccccccccccc cccccccccc,\n"
3397 " Cccccccccccccc cccccccccc,\n"
3398 " Cccccccccccccc cccccccccc,\n"
3399 " Cccccccccccccc cccccccccc,\n"
3400 " Cccccccccccccc cccccccccc);");
3401 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
3402 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3403 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3404 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3405 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
3406
3407 // Break after multi-line parameters.
3408 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3409 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3410 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3411 " bbbb bbbb);");
3412
3413 // Treat overloaded operators like other functions.
3414 verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3415 "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
3416 verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3417 "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
3418 verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3419 "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
3420 verifyGoogleFormat(
3421 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
3422 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
3423 verifyGoogleFormat(
3424 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
3425 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
3426 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3427 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
3428 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
3429 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
3430 }
3431
TEST_F(FormatTest,TrailingReturnType)3432 TEST_F(FormatTest, TrailingReturnType) {
3433 verifyFormat("auto foo() -> int;\n");
3434 verifyFormat("struct S {\n"
3435 " auto bar() const -> int;\n"
3436 "};");
3437 verifyFormat("template <size_t Order, typename T>\n"
3438 "auto load_img(const std::string &filename)\n"
3439 " -> alias::tensor<Order, T, mem::tag::cpu> {}");
3440
3441 // Not trailing return types.
3442 verifyFormat("void f() { auto a = b->c(); }");
3443 }
3444
TEST_F(FormatTest,BreaksFunctionDeclarationsWithTrailingTokens)3445 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
3446 // Avoid breaking before trailing 'const' or other trailing annotations, if
3447 // they are not function-like.
3448 FormatStyle Style = getGoogleStyle();
3449 Style.ColumnLimit = 47;
3450 verifyFormat("void\n"
3451 "someLongFunction(int someLongParameter) const {\n}",
3452 getLLVMStyleWithColumns(47));
3453 verifyFormat("LoooooongReturnType\n"
3454 "someLoooooooongFunction() const {}",
3455 getLLVMStyleWithColumns(47));
3456 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
3457 " const {}",
3458 Style);
3459 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3460 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
3461 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3462 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
3463 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3464 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
3465 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
3466 " aaaaaaaaaaa aaaaa) const override;");
3467 verifyGoogleFormat(
3468 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
3469 " const override;");
3470
3471 // Even if the first parameter has to be wrapped.
3472 verifyFormat("void someLongFunction(\n"
3473 " int someLongParameter) const {}",
3474 getLLVMStyleWithColumns(46));
3475 verifyFormat("void someLongFunction(\n"
3476 " int someLongParameter) const {}",
3477 Style);
3478 verifyFormat("void someLongFunction(\n"
3479 " int someLongParameter) override {}",
3480 Style);
3481 verifyFormat("void someLongFunction(\n"
3482 " int someLongParameter) OVERRIDE {}",
3483 Style);
3484 verifyFormat("void someLongFunction(\n"
3485 " int someLongParameter) final {}",
3486 Style);
3487 verifyFormat("void someLongFunction(\n"
3488 " int someLongParameter) FINAL {}",
3489 Style);
3490 verifyFormat("void someLongFunction(\n"
3491 " int parameter) const override {}",
3492 Style);
3493
3494 Style.BreakBeforeBraces = FormatStyle::BS_Allman;
3495 verifyFormat("void someLongFunction(\n"
3496 " int someLongParameter) const\n"
3497 "{\n"
3498 "}",
3499 Style);
3500
3501 // Unless these are unknown annotations.
3502 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
3503 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3504 " LONG_AND_UGLY_ANNOTATION;");
3505
3506 // Breaking before function-like trailing annotations is fine to keep them
3507 // close to their arguments.
3508 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3509 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
3510 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
3511 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
3512 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
3513 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
3514 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
3515 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
3516
3517 verifyFormat(
3518 "void aaaaaaaaaaaaaaaaaa()\n"
3519 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
3520 " aaaaaaaaaaaaaaaaaaaaaaaaa));");
3521 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3522 " __attribute__((unused));");
3523 verifyGoogleFormat(
3524 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3525 " GUARDED_BY(aaaaaaaaaaaa);");
3526 verifyGoogleFormat(
3527 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3528 " GUARDED_BY(aaaaaaaaaaaa);");
3529 verifyGoogleFormat(
3530 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
3531 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
3532 }
3533
TEST_F(FormatTest,BreaksDesireably)3534 TEST_F(FormatTest, BreaksDesireably) {
3535 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
3536 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
3537 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
3538 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3539 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
3540 "}");
3541
3542 verifyFormat(
3543 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3544 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3545
3546 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3547 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3548 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
3549
3550 verifyFormat(
3551 "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3552 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
3553 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3554 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
3555
3556 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3557 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3558
3559 verifyFormat(
3560 "void f() {\n"
3561 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
3562 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
3563 "}");
3564 verifyFormat(
3565 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3566 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
3567 verifyFormat(
3568 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3569 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
3570 verifyFormat(
3571 "aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3572 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3573 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3574
3575 // Indent consistently independent of call expression.
3576 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
3577 " dddddddddddddddddddddddddddddd));\n"
3578 "aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
3579 " dddddddddddddddddddddddddddddd));");
3580
3581 // This test case breaks on an incorrect memoization, i.e. an optimization not
3582 // taking into account the StopAt value.
3583 verifyFormat(
3584 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
3585 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
3586 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
3587 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3588
3589 verifyFormat("{\n {\n {\n"
3590 " Annotation.SpaceRequiredBefore =\n"
3591 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
3592 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
3593 " }\n }\n}");
3594
3595 // Break on an outer level if there was a break on an inner level.
3596 EXPECT_EQ("f(g(h(a, // comment\n"
3597 " b, c),\n"
3598 " d, e),\n"
3599 " x, y);",
3600 format("f(g(h(a, // comment\n"
3601 " b, c), d, e), x, y);"));
3602
3603 // Prefer breaking similar line breaks.
3604 verifyFormat(
3605 "const int kTrackingOptions = NSTrackingMouseMoved |\n"
3606 " NSTrackingMouseEnteredAndExited |\n"
3607 " NSTrackingActiveAlways;");
3608 }
3609
TEST_F(FormatTest,FormatsOneParameterPerLineIfNecessary)3610 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
3611 FormatStyle NoBinPacking = getGoogleStyle();
3612 NoBinPacking.BinPackParameters = false;
3613 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
3614 " aaaaaaaaaaaaaaaaaaaa,\n"
3615 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
3616 NoBinPacking);
3617 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
3618 " aaaaaaaaaaaaa,\n"
3619 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
3620 NoBinPacking);
3621 verifyFormat(
3622 "aaaaaaaa(aaaaaaaaaaaaa,\n"
3623 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3624 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
3625 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3626 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
3627 NoBinPacking);
3628 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
3629 " .aaaaaaaaaaaaaaaaaa();",
3630 NoBinPacking);
3631 verifyFormat("void f() {\n"
3632 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3633 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
3634 "}",
3635 NoBinPacking);
3636
3637 verifyFormat(
3638 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3639 " aaaaaaaaaaaa,\n"
3640 " aaaaaaaaaaaa);",
3641 NoBinPacking);
3642 verifyFormat(
3643 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
3644 " ddddddddddddddddddddddddddddd),\n"
3645 " test);",
3646 NoBinPacking);
3647
3648 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
3649 " aaaaaaaaaaaaaaaaaaaaaaa,\n"
3650 " aaaaaaaaaaaaaaaaaaaaaaa> aaaaaaaaaaaaaaaaaa;",
3651 NoBinPacking);
3652 verifyFormat("a(\"a\"\n"
3653 " \"a\",\n"
3654 " a);");
3655
3656 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
3657 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
3658 " aaaaaaaaa,\n"
3659 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3660 NoBinPacking);
3661 verifyFormat(
3662 "void f() {\n"
3663 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
3664 " .aaaaaaa();\n"
3665 "}",
3666 NoBinPacking);
3667 verifyFormat(
3668 "template <class SomeType, class SomeOtherType>\n"
3669 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
3670 NoBinPacking);
3671 }
3672
TEST_F(FormatTest,AdaptiveOnePerLineFormatting)3673 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
3674 FormatStyle Style = getLLVMStyleWithColumns(15);
3675 Style.ExperimentalAutoDetectBinPacking = true;
3676 EXPECT_EQ("aaa(aaaa,\n"
3677 " aaaa,\n"
3678 " aaaa);\n"
3679 "aaa(aaaa,\n"
3680 " aaaa,\n"
3681 " aaaa);",
3682 format("aaa(aaaa,\n" // one-per-line
3683 " aaaa,\n"
3684 " aaaa );\n"
3685 "aaa(aaaa, aaaa, aaaa);", // inconclusive
3686 Style));
3687 EXPECT_EQ("aaa(aaaa, aaaa,\n"
3688 " aaaa);\n"
3689 "aaa(aaaa, aaaa,\n"
3690 " aaaa);",
3691 format("aaa(aaaa, aaaa,\n" // bin-packed
3692 " aaaa );\n"
3693 "aaa(aaaa, aaaa, aaaa);", // inconclusive
3694 Style));
3695 }
3696
TEST_F(FormatTest,FormatsBuilderPattern)3697 TEST_F(FormatTest, FormatsBuilderPattern) {
3698 verifyFormat(
3699 "return llvm::StringSwitch<Reference::Kind>(name)\n"
3700 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
3701 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
3702 " .StartsWith(\".init\", ORDER_INIT)\n"
3703 " .StartsWith(\".fini\", ORDER_FINI)\n"
3704 " .StartsWith(\".hash\", ORDER_HASH)\n"
3705 " .Default(ORDER_TEXT);\n");
3706
3707 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
3708 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
3709 verifyFormat(
3710 "aaaaaaa->aaaaaaa->aaaaaaaaaaaaaaaa(\n"
3711 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3712 " ->aaaaaaaa(aaaaaaaaaaaaaaa);");
3713 verifyFormat(
3714 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
3715 " aaaaaaaaaaaaaa);");
3716 verifyFormat(
3717 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
3718 " aaaaaa->aaaaaaaaaaaa()\n"
3719 " ->aaaaaaaaaaaaaaaa(\n"
3720 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3721 " ->aaaaaaaaaaaaaaaaa();");
3722 verifyGoogleFormat(
3723 "void f() {\n"
3724 " someo->Add((new util::filetools::Handler(dir))\n"
3725 " ->OnEvent1(NewPermanentCallback(\n"
3726 " this, &HandlerHolderClass::EventHandlerCBA))\n"
3727 " ->OnEvent2(NewPermanentCallback(\n"
3728 " this, &HandlerHolderClass::EventHandlerCBB))\n"
3729 " ->OnEvent3(NewPermanentCallback(\n"
3730 " this, &HandlerHolderClass::EventHandlerCBC))\n"
3731 " ->OnEvent5(NewPermanentCallback(\n"
3732 " this, &HandlerHolderClass::EventHandlerCBD))\n"
3733 " ->OnEvent6(NewPermanentCallback(\n"
3734 " this, &HandlerHolderClass::EventHandlerCBE)));\n"
3735 "}");
3736
3737 verifyFormat(
3738 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
3739 verifyFormat("aaaaaaaaaaaaaaa()\n"
3740 " .aaaaaaaaaaaaaaa()\n"
3741 " .aaaaaaaaaaaaaaa()\n"
3742 " .aaaaaaaaaaaaaaa()\n"
3743 " .aaaaaaaaaaaaaaa();");
3744 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
3745 " .aaaaaaaaaaaaaaa()\n"
3746 " .aaaaaaaaaaaaaaa()\n"
3747 " .aaaaaaaaaaaaaaa();");
3748 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
3749 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
3750 " .aaaaaaaaaaaaaaa();");
3751 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
3752 " ->aaaaaaaaaaaaaae(0)\n"
3753 " ->aaaaaaaaaaaaaaa();");
3754
3755 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
3756 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
3757 " .has<bbbbbbbbbbbbbbbbbbbbb>();");
3758 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
3759 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
3760 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
3761
3762 // Prefer not to break after empty parentheses.
3763 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
3764 " First->LastNewlineOffset);");
3765 }
3766
TEST_F(FormatTest,BreaksAccordingToOperatorPrecedence)3767 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
3768 verifyFormat(
3769 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3770 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
3771 verifyFormat(
3772 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
3773 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
3774
3775 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
3776 " ccccccccccccccccccccccccc) {\n}");
3777 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
3778 " ccccccccccccccccccccccccc) {\n}");
3779
3780 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
3781 " ccccccccccccccccccccccccc) {\n}");
3782 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
3783 " ccccccccccccccccccccccccc) {\n}");
3784
3785 verifyFormat(
3786 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
3787 " ccccccccccccccccccccccccc) {\n}");
3788 verifyFormat(
3789 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
3790 " ccccccccccccccccccccccccc) {\n}");
3791
3792 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
3793 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
3794 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
3795 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
3796 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
3797 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
3798 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
3799 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
3800
3801 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
3802 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
3803 " aaaaaaaaaaaaaaa != aa) {\n}");
3804 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
3805 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
3806 " aaaaaaaaaaaaaaa != aa) {\n}");
3807 }
3808
TEST_F(FormatTest,BreaksAfterAssignments)3809 TEST_F(FormatTest, BreaksAfterAssignments) {
3810 verifyFormat(
3811 "unsigned Cost =\n"
3812 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
3813 " SI->getPointerAddressSpaceee());\n");
3814 verifyFormat(
3815 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
3816 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
3817
3818 verifyFormat(
3819 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
3820 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
3821 verifyFormat("unsigned OriginalStartColumn =\n"
3822 " SourceMgr.getSpellingColumnNumber(\n"
3823 " Current.FormatTok.getStartOfNonWhitespace()) -\n"
3824 " 1;");
3825 }
3826
TEST_F(FormatTest,AlignsAfterAssignments)3827 TEST_F(FormatTest, AlignsAfterAssignments) {
3828 verifyFormat(
3829 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3830 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
3831 verifyFormat(
3832 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3833 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
3834 verifyFormat(
3835 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3836 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
3837 verifyFormat(
3838 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3839 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
3840 verifyFormat(
3841 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
3842 " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
3843 " aaaaaaaaaaaaaaaaaaaaaaaa;");
3844 }
3845
TEST_F(FormatTest,AlignsAfterReturn)3846 TEST_F(FormatTest, AlignsAfterReturn) {
3847 verifyFormat(
3848 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3849 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
3850 verifyFormat(
3851 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3852 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
3853 verifyFormat(
3854 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
3855 " aaaaaaaaaaaaaaaaaaaaaa();");
3856 verifyFormat(
3857 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
3858 " aaaaaaaaaaaaaaaaaaaaaa());");
3859 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3860 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3861 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3862 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
3863 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
3864 verifyFormat("return\n"
3865 " // true if code is one of a or b.\n"
3866 " code == a || code == b;");
3867 }
3868
TEST_F(FormatTest,BreaksConditionalExpressions)3869 TEST_F(FormatTest, BreaksConditionalExpressions) {
3870 verifyFormat(
3871 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3872 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3873 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3874 verifyFormat(
3875 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3876 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3877 verifyFormat(
3878 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
3879 " : aaaaaaaaaaaaa);");
3880 verifyFormat(
3881 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3882 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3883 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3884 " aaaaaaaaaaaaa);");
3885 verifyFormat(
3886 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3887 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3888 " aaaaaaaaaaaaa);");
3889 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3890 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3891 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3892 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3893 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3894 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3895 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3896 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3897 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3898 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3899 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3900 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3901 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3902 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3903 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3904 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3905 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3906 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3907 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3908 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
3909 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
3910 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3911 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3912 " : aaaaaaaaaaaaaaaa;");
3913 verifyFormat(
3914 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3915 " ? aaaaaaaaaaaaaaa\n"
3916 " : aaaaaaaaaaaaaaa;");
3917 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
3918 " aaaaaaaaa\n"
3919 " ? b\n"
3920 " : c);");
3921 verifyFormat(
3922 "unsigned Indent =\n"
3923 " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n"
3924 " ? IndentForLevel[TheLine.Level]\n"
3925 " : TheLine * 2,\n"
3926 " TheLine.InPPDirective, PreviousEndOfLineColumn);",
3927 getLLVMStyleWithColumns(70));
3928 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
3929 " ? aaaaaaaaaaaaaaa\n"
3930 " : bbbbbbbbbbbbbbb //\n"
3931 " ? ccccccccccccccc\n"
3932 " : ddddddddddddddd;");
3933 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
3934 " ? aaaaaaaaaaaaaaa\n"
3935 " : (bbbbbbbbbbbbbbb //\n"
3936 " ? ccccccccccccccc\n"
3937 " : ddddddddddddddd);");
3938 verifyFormat(
3939 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3940 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3941 " aaaaaaaaaaaaaaaaaaaaa +\n"
3942 " aaaaaaaaaaaaaaaaaaaaa\n"
3943 " : aaaaaaaaaa;");
3944
3945 FormatStyle NoBinPacking = getLLVMStyle();
3946 NoBinPacking.BinPackParameters = false;
3947 verifyFormat(
3948 "void f() {\n"
3949 " g(aaa,\n"
3950 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
3951 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3952 " ? aaaaaaaaaaaaaaa\n"
3953 " : aaaaaaaaaaaaaaa);\n"
3954 "}",
3955 NoBinPacking);
3956 verifyFormat(
3957 "void f() {\n"
3958 " g(aaa,\n"
3959 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
3960 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3961 " ?: aaaaaaaaaaaaaaa);\n"
3962 "}",
3963 NoBinPacking);
3964 }
3965
TEST_F(FormatTest,BreaksConditionalExpressionsAfterOperator)3966 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
3967 FormatStyle Style = getLLVMStyle();
3968 Style.BreakBeforeTernaryOperators = false;
3969 Style.ColumnLimit = 70;
3970 verifyFormat(
3971 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
3972 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
3973 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3974 Style);
3975 verifyFormat(
3976 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
3977 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3978 Style);
3979 verifyFormat(
3980 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
3981 " aaaaaaaaaaaaa);",
3982 Style);
3983 verifyFormat(
3984 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3985 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
3986 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3987 " aaaaaaaaaaaaa);",
3988 Style);
3989 verifyFormat(
3990 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3991 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3992 " aaaaaaaaaaaaa);",
3993 Style);
3994 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
3995 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3996 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
3997 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3998 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3999 Style);
4000 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4001 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4002 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4003 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
4004 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4005 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4006 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4007 Style);
4008 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4009 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
4010 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4011 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4012 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4013 Style);
4014 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4015 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4016 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4017 Style);
4018 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
4019 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4020 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4021 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4022 Style);
4023 verifyFormat(
4024 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4025 " aaaaaaaaaaaaaaa :\n"
4026 " aaaaaaaaaaaaaaa;",
4027 Style);
4028 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
4029 " aaaaaaaaa ?\n"
4030 " b :\n"
4031 " c);",
4032 Style);
4033 verifyFormat(
4034 "unsigned Indent =\n"
4035 " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0 ?\n"
4036 " IndentForLevel[TheLine.Level] :\n"
4037 " TheLine * 2,\n"
4038 " TheLine.InPPDirective, PreviousEndOfLineColumn);",
4039 Style);
4040 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
4041 " aaaaaaaaaaaaaaa :\n"
4042 " bbbbbbbbbbbbbbb ? //\n"
4043 " ccccccccccccccc :\n"
4044 " ddddddddddddddd;",
4045 Style);
4046 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
4047 " aaaaaaaaaaaaaaa :\n"
4048 " (bbbbbbbbbbbbbbb ? //\n"
4049 " ccccccccccccccc :\n"
4050 " ddddddddddddddd);",
4051 Style);
4052 }
4053
TEST_F(FormatTest,DeclarationsOfMultipleVariables)4054 TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
4055 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
4056 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
4057 verifyFormat("bool a = true, b = false;");
4058
4059 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4060 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
4061 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
4062 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
4063 verifyFormat(
4064 "bool aaaaaaaaaaaaaaaaaaaaa =\n"
4065 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
4066 " d = e && f;");
4067 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
4068 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
4069 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
4070 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
4071 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
4072 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
4073 // FIXME: If multiple variables are defined, the "*" needs to move to the new
4074 // line. Also fix indent for breaking after the type, this looks bad.
4075 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
4076 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
4077 " *b = bbbbbbbbbbbbbbbbbbb;",
4078 getGoogleStyle());
4079
4080 // Not ideal, but pointer-with-type does not allow much here.
4081 verifyGoogleFormat(
4082 "aaaaaaaaa* a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
4083 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;");
4084 }
4085
TEST_F(FormatTest,ConditionalExpressionsInBrackets)4086 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
4087 verifyFormat("arr[foo ? bar : baz];");
4088 verifyFormat("f()[foo ? bar : baz];");
4089 verifyFormat("(a + b)[foo ? bar : baz];");
4090 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
4091 }
4092
TEST_F(FormatTest,AlignsStringLiterals)4093 TEST_F(FormatTest, AlignsStringLiterals) {
4094 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
4095 " \"short literal\");");
4096 verifyFormat(
4097 "looooooooooooooooooooooooongFunction(\n"
4098 " \"short literal\"\n"
4099 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
4100 verifyFormat("someFunction(\"Always break between multi-line\"\n"
4101 " \" string literals\",\n"
4102 " and, other, parameters);");
4103 EXPECT_EQ("fun + \"1243\" /* comment */\n"
4104 " \"5678\";",
4105 format("fun + \"1243\" /* comment */\n"
4106 " \"5678\";",
4107 getLLVMStyleWithColumns(28)));
4108 EXPECT_EQ(
4109 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
4110 " \"aaaaaaaaaaaaaaaaaaaaa\"\n"
4111 " \"aaaaaaaaaaaaaaaa\";",
4112 format("aaaaaa ="
4113 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
4114 "aaaaaaaaaaaaaaaaaaaaa\" "
4115 "\"aaaaaaaaaaaaaaaa\";"));
4116 verifyFormat("a = a + \"a\"\n"
4117 " \"a\"\n"
4118 " \"a\";");
4119 verifyFormat("f(\"a\", \"b\"\n"
4120 " \"c\");");
4121
4122 verifyFormat(
4123 "#define LL_FORMAT \"ll\"\n"
4124 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
4125 " \"d, ddddddddd: %\" LL_FORMAT \"d\");");
4126
4127 verifyFormat("#define A(X) \\\n"
4128 " \"aaaaa\" #X \"bbbbbb\" \\\n"
4129 " \"ccccc\"",
4130 getLLVMStyleWithColumns(23));
4131 verifyFormat("#define A \"def\"\n"
4132 "f(\"abc\" A \"ghi\"\n"
4133 " \"jkl\");");
4134
4135 verifyFormat("f(L\"a\"\n"
4136 " L\"b\")");
4137 verifyFormat("#define A(X) \\\n"
4138 " L\"aaaaa\" #X L\"bbbbbb\" \\\n"
4139 " L\"ccccc\"",
4140 getLLVMStyleWithColumns(25));
4141 }
4142
TEST_F(FormatTest,AlwaysBreakBeforeMultilineStrings)4143 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
4144 FormatStyle NoBreak = getLLVMStyle();
4145 NoBreak.AlwaysBreakBeforeMultilineStrings = false;
4146 FormatStyle Break = getLLVMStyle();
4147 Break.AlwaysBreakBeforeMultilineStrings = true;
4148 verifyFormat("aaaa = \"bbbb\"\n"
4149 " \"cccc\";",
4150 NoBreak);
4151 verifyFormat("aaaa =\n"
4152 " \"bbbb\"\n"
4153 " \"cccc\";",
4154 Break);
4155 verifyFormat("aaaa(\"bbbb\"\n"
4156 " \"cccc\");",
4157 NoBreak);
4158 verifyFormat("aaaa(\n"
4159 " \"bbbb\"\n"
4160 " \"cccc\");",
4161 Break);
4162 verifyFormat("aaaa(qqq, \"bbbb\"\n"
4163 " \"cccc\");",
4164 NoBreak);
4165 verifyFormat("aaaa(qqq,\n"
4166 " \"bbbb\"\n"
4167 " \"cccc\");",
4168 Break);
4169 verifyFormat("aaaa(qqq,\n"
4170 " L\"bbbb\"\n"
4171 " L\"cccc\");",
4172 Break);
4173
4174 // As we break before unary operators, breaking right after them is bad.
4175 verifyFormat("string foo = abc ? \"x\"\n"
4176 " \"blah blah blah blah blah blah\"\n"
4177 " : \"y\";",
4178 Break);
4179
4180 // Don't break if there is no column gain.
4181 verifyFormat("f(\"aaaa\"\n"
4182 " \"bbbb\");",
4183 Break);
4184
4185 // Treat literals with escaped newlines like multi-line string literals.
4186 EXPECT_EQ("x = \"a\\\n"
4187 "b\\\n"
4188 "c\";",
4189 format("x = \"a\\\n"
4190 "b\\\n"
4191 "c\";",
4192 NoBreak));
4193 EXPECT_EQ("x =\n"
4194 " \"a\\\n"
4195 "b\\\n"
4196 "c\";",
4197 format("x = \"a\\\n"
4198 "b\\\n"
4199 "c\";",
4200 Break));
4201
4202 // Exempt ObjC strings for now.
4203 EXPECT_EQ("NSString *const kString = @\"aaaa\"\n"
4204 " \"bbbb\";",
4205 format("NSString *const kString = @\"aaaa\"\n"
4206 "\"bbbb\";",
4207 Break));
4208 }
4209
TEST_F(FormatTest,AlignsPipes)4210 TEST_F(FormatTest, AlignsPipes) {
4211 verifyFormat(
4212 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4213 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4214 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4215 verifyFormat(
4216 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
4217 " << aaaaaaaaaaaaaaaaaaaa;");
4218 verifyFormat(
4219 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4220 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4221 verifyFormat(
4222 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
4223 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
4224 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
4225 verifyFormat(
4226 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4227 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4228 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4229 verifyFormat(
4230 "llvm::errs() << \"a: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4231 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4232 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4233 verifyFormat(
4234 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4235 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4236 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4237 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
4238 verifyFormat(
4239 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4240 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4241
4242 verifyFormat("return out << \"somepacket = {\\n\"\n"
4243 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
4244 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
4245 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
4246 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
4247 " << \"}\";");
4248
4249 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
4250 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
4251 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
4252 verifyFormat(
4253 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
4254 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
4255 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
4256 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
4257 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
4258 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
4259 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
4260 verifyFormat(
4261 "void f() {\n"
4262 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
4263 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
4264 "}");
4265 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
4266 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
4267
4268 // Breaking before the first "<<" is generally not desirable.
4269 verifyFormat(
4270 "llvm::errs()\n"
4271 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4272 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4273 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4274 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4275 getLLVMStyleWithColumns(70));
4276 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
4277 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4278 " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
4279 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4280 " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
4281 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4282 getLLVMStyleWithColumns(70));
4283
4284 // But sometimes, breaking before the first "<<" is desirable.
4285 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
4286 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
4287 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
4288 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4289 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4290 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
4291 " << BEF << IsTemplate << Description << E->getType();");
4292
4293 verifyFormat(
4294 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4295 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4296
4297 // Incomplete string literal.
4298 EXPECT_EQ("llvm::errs() << \"\n"
4299 " << a;",
4300 format("llvm::errs() << \"\n<<a;"));
4301
4302 verifyFormat("void f() {\n"
4303 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
4304 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
4305 "}");
4306 }
4307
TEST_F(FormatTest,UnderstandsEquals)4308 TEST_F(FormatTest, UnderstandsEquals) {
4309 verifyFormat(
4310 "aaaaaaaaaaaaaaaaa =\n"
4311 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4312 verifyFormat(
4313 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4314 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
4315 verifyFormat(
4316 "if (a) {\n"
4317 " f();\n"
4318 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4319 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
4320 "}");
4321
4322 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4323 " 100000000 + 10000000) {\n}");
4324 }
4325
TEST_F(FormatTest,WrapsAtFunctionCallsIfNecessary)4326 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
4327 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
4328 " .looooooooooooooooooooooooooooooooooooooongFunction();");
4329
4330 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
4331 " ->looooooooooooooooooooooooooooooooooooooongFunction();");
4332
4333 verifyFormat(
4334 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
4335 " Parameter2);");
4336
4337 verifyFormat(
4338 "ShortObject->shortFunction(\n"
4339 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
4340 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
4341
4342 verifyFormat("loooooooooooooongFunction(\n"
4343 " LoooooooooooooongObject->looooooooooooooooongFunction());");
4344
4345 verifyFormat(
4346 "function(LoooooooooooooooooooooooooooooooooooongObject\n"
4347 " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
4348
4349 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
4350 " .WillRepeatedly(Return(SomeValue));");
4351 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
4352 " ccccccccccccccccccccccc);");
4353 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4354 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa).aaaaa(aaaaa),\n"
4355 " aaaaaaaaaaaaaaaaaaaaa);");
4356 verifyFormat("void f() {\n"
4357 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4358 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
4359 "}");
4360 verifyFormat(
4361 "aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4362 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4363 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4364 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4365 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4366 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4367 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4368 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4369 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
4370 "}");
4371
4372 // Here, it is not necessary to wrap at "." or "->".
4373 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
4374 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
4375 verifyFormat(
4376 "aaaaaaaaaaa->aaaaaaaaa(\n"
4377 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4378 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
4379
4380 verifyFormat(
4381 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4382 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
4383 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
4384 " aaaaaaaaa()->aaaaaa()->aaaaa());");
4385 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
4386 " aaaaaaaaa()->aaaaaa()->aaaaa());");
4387
4388 // FIXME: Should we break before .a()?
4389 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4390 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa).a();");
4391
4392 FormatStyle NoBinPacking = getLLVMStyle();
4393 NoBinPacking.BinPackParameters = false;
4394 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
4395 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
4396 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
4397 " aaaaaaaaaaaaaaaaaaa,\n"
4398 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4399 NoBinPacking);
4400
4401 // If there is a subsequent call, change to hanging indentation.
4402 verifyFormat(
4403 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4404 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
4405 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4406 verifyFormat(
4407 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4408 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
4409 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4410 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4411 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4412 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4413 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4414 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
4415 }
4416
TEST_F(FormatTest,WrapsTemplateDeclarations)4417 TEST_F(FormatTest, WrapsTemplateDeclarations) {
4418 verifyFormat("template <typename T>\n"
4419 "virtual void loooooooooooongFunction(int Param1, int Param2);");
4420 verifyFormat("template <typename T>\n"
4421 "// T should be one of {A, B}.\n"
4422 "virtual void loooooooooooongFunction(int Param1, int Param2);");
4423 verifyFormat(
4424 "template <typename T>\n"
4425 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
4426 verifyFormat("template <typename T>\n"
4427 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
4428 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
4429 verifyFormat(
4430 "template <typename T>\n"
4431 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
4432 " int Paaaaaaaaaaaaaaaaaaaaram2);");
4433 verifyFormat(
4434 "template <typename T>\n"
4435 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
4436 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
4437 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4438 verifyFormat("template <typename T>\n"
4439 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4440 " int aaaaaaaaaaaaaaaaaaaaaa);");
4441 verifyFormat(
4442 "template <typename T1, typename T2 = char, typename T3 = char,\n"
4443 " typename T4 = char>\n"
4444 "void f();");
4445 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
4446 " template <typename> class cccccccccccccccccccccc,\n"
4447 " typename ddddddddddddd>\n"
4448 "class C {};");
4449 verifyFormat(
4450 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
4451 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4452
4453 verifyFormat("void f() {\n"
4454 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
4455 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
4456 "}");
4457
4458 verifyFormat("template <typename T> class C {};");
4459 verifyFormat("template <typename T> void f();");
4460 verifyFormat("template <typename T> void f() {}");
4461 verifyFormat(
4462 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
4463 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4464 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
4465 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
4466 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4467 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
4468 " bbbbbbbbbbbbbbbbbbbbbbbb);",
4469 getLLVMStyleWithColumns(72));
4470
4471 FormatStyle AlwaysBreak = getLLVMStyle();
4472 AlwaysBreak.AlwaysBreakTemplateDeclarations = true;
4473 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
4474 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
4475 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
4476 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4477 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
4478 " ccccccccccccccccccccccccccccccccccccccccccccccc);");
4479 verifyFormat("template <template <typename> class Fooooooo,\n"
4480 " template <typename> class Baaaaaaar>\n"
4481 "struct C {};",
4482 AlwaysBreak);
4483 verifyFormat("template <typename T> // T can be A, B or C.\n"
4484 "struct C {};",
4485 AlwaysBreak);
4486 }
4487
TEST_F(FormatTest,WrapsAtNestedNameSpecifiers)4488 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
4489 verifyFormat(
4490 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
4491 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4492 verifyFormat(
4493 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
4494 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4495 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
4496
4497 // FIXME: Should we have the extra indent after the second break?
4498 verifyFormat(
4499 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
4500 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
4501 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4502
4503 verifyFormat(
4504 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
4505 " cccccccccccccccccccccccccccccccccccccccccccccc());");
4506
4507 // Breaking at nested name specifiers is generally not desirable.
4508 verifyFormat(
4509 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4510 " aaaaaaaaaaaaaaaaaaaaaaa);");
4511
4512 verifyFormat(
4513 "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
4514 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4515 " aaaaaaaaaaaaaaaaaaaaa);",
4516 getLLVMStyleWithColumns(74));
4517
4518 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
4519 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4520 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4521 }
4522
TEST_F(FormatTest,UnderstandsTemplateParameters)4523 TEST_F(FormatTest, UnderstandsTemplateParameters) {
4524 verifyFormat("A<int> a;");
4525 verifyFormat("A<A<A<int>>> a;");
4526 verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
4527 verifyFormat("bool x = a < 1 || 2 > a;");
4528 verifyFormat("bool x = 5 < f<int>();");
4529 verifyFormat("bool x = f<int>() > 5;");
4530 verifyFormat("bool x = 5 < a<int>::x;");
4531 verifyFormat("bool x = a < 4 ? a > 2 : false;");
4532 verifyFormat("bool x = f() ? a < 2 : a > 2;");
4533
4534 verifyGoogleFormat("A<A<int>> a;");
4535 verifyGoogleFormat("A<A<A<int>>> a;");
4536 verifyGoogleFormat("A<A<A<A<int>>>> a;");
4537 verifyGoogleFormat("A<A<int> > a;");
4538 verifyGoogleFormat("A<A<A<int> > > a;");
4539 verifyGoogleFormat("A<A<A<A<int> > > > a;");
4540 verifyGoogleFormat("A<::A<int>> a;");
4541 verifyGoogleFormat("A<::A> a;");
4542 verifyGoogleFormat("A< ::A> a;");
4543 verifyGoogleFormat("A< ::A<int> > a;");
4544 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
4545 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
4546 EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
4547 EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
4548
4549 verifyFormat("test >> a >> b;");
4550 verifyFormat("test << a >> b;");
4551
4552 verifyFormat("f<int>();");
4553 verifyFormat("template <typename T> void f() {}");
4554
4555 // Not template parameters.
4556 verifyFormat("return a < b && c > d;");
4557 verifyFormat("void f() {\n"
4558 " while (a < b && c > d) {\n"
4559 " }\n"
4560 "}");
4561 verifyFormat("template <typename... Types>\n"
4562 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
4563
4564 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4565 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
4566 getLLVMStyleWithColumns(60));
4567 }
4568
TEST_F(FormatTest,UnderstandsBinaryOperators)4569 TEST_F(FormatTest, UnderstandsBinaryOperators) {
4570 verifyFormat("COMPARE(a, ==, b);");
4571 }
4572
TEST_F(FormatTest,UnderstandsPointersToMembers)4573 TEST_F(FormatTest, UnderstandsPointersToMembers) {
4574 verifyFormat("int A::*x;");
4575 verifyFormat("int (S::*func)(void *);");
4576 verifyFormat("void f() { int (S::*func)(void *); }");
4577 verifyFormat("typedef bool *(Class::*Member)() const;");
4578 verifyFormat("void f() {\n"
4579 " (a->*f)();\n"
4580 " a->*x;\n"
4581 " (a.*f)();\n"
4582 " ((*a).*f)();\n"
4583 " a.*x;\n"
4584 "}");
4585 verifyFormat("void f() {\n"
4586 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
4587 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
4588 "}");
4589 verifyFormat(
4590 "(aaaaaaaaaa->*bbbbbbb)(\n"
4591 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4592 FormatStyle Style = getLLVMStyle();
4593 Style.PointerAlignment = FormatStyle::PAS_Left;
4594 verifyFormat("typedef bool* (Class::*Member)() const;", Style);
4595 }
4596
TEST_F(FormatTest,UnderstandsUnaryOperators)4597 TEST_F(FormatTest, UnderstandsUnaryOperators) {
4598 verifyFormat("int a = -2;");
4599 verifyFormat("f(-1, -2, -3);");
4600 verifyFormat("a[-1] = 5;");
4601 verifyFormat("int a = 5 + -2;");
4602 verifyFormat("if (i == -1) {\n}");
4603 verifyFormat("if (i != -1) {\n}");
4604 verifyFormat("if (i > -1) {\n}");
4605 verifyFormat("if (i < -1) {\n}");
4606 verifyFormat("++(a->f());");
4607 verifyFormat("--(a->f());");
4608 verifyFormat("(a->f())++;");
4609 verifyFormat("a[42]++;");
4610 verifyFormat("if (!(a->f())) {\n}");
4611
4612 verifyFormat("a-- > b;");
4613 verifyFormat("b ? -a : c;");
4614 verifyFormat("n * sizeof char16;");
4615 verifyFormat("n * alignof char16;", getGoogleStyle());
4616 verifyFormat("sizeof(char);");
4617 verifyFormat("alignof(char);", getGoogleStyle());
4618
4619 verifyFormat("return -1;");
4620 verifyFormat("switch (a) {\n"
4621 "case -1:\n"
4622 " break;\n"
4623 "}");
4624 verifyFormat("#define X -1");
4625 verifyFormat("#define X -kConstant");
4626
4627 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
4628 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
4629
4630 verifyFormat("int a = /* confusing comment */ -1;");
4631 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
4632 verifyFormat("int a = i /* confusing comment */++;");
4633 }
4634
TEST_F(FormatTest,DoesNotIndentRelativeToUnaryOperators)4635 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
4636 verifyFormat("if (!aaaaaaaaaa( // break\n"
4637 " aaaaa)) {\n"
4638 "}");
4639 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
4640 " aaaaa));");
4641 verifyFormat("*aaa = aaaaaaa( // break\n"
4642 " bbbbbb);");
4643 }
4644
TEST_F(FormatTest,UnderstandsOverloadedOperators)4645 TEST_F(FormatTest, UnderstandsOverloadedOperators) {
4646 verifyFormat("bool operator<();");
4647 verifyFormat("bool operator>();");
4648 verifyFormat("bool operator=();");
4649 verifyFormat("bool operator==();");
4650 verifyFormat("bool operator!=();");
4651 verifyFormat("int operator+();");
4652 verifyFormat("int operator++();");
4653 verifyFormat("bool operator();");
4654 verifyFormat("bool operator()();");
4655 verifyFormat("bool operator[]();");
4656 verifyFormat("operator bool();");
4657 verifyFormat("operator int();");
4658 verifyFormat("operator void *();");
4659 verifyFormat("operator SomeType<int>();");
4660 verifyFormat("operator SomeType<int, int>();");
4661 verifyFormat("operator SomeType<SomeType<int>>();");
4662 verifyFormat("void *operator new(std::size_t size);");
4663 verifyFormat("void *operator new[](std::size_t size);");
4664 verifyFormat("void operator delete(void *ptr);");
4665 verifyFormat("void operator delete[](void *ptr);");
4666 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
4667 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
4668
4669 verifyFormat(
4670 "ostream &operator<<(ostream &OutputStream,\n"
4671 " SomeReallyLongType WithSomeReallyLongValue);");
4672 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
4673 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
4674 " return left.group < right.group;\n"
4675 "}");
4676 verifyFormat("SomeType &operator=(const SomeType &S);");
4677
4678 verifyGoogleFormat("operator void*();");
4679 verifyGoogleFormat("operator SomeType<SomeType<int>>();");
4680 verifyGoogleFormat("operator ::A();");
4681
4682 verifyFormat("using A::operator+;");
4683
4684 verifyFormat("Deleted &operator=(const Deleted &)& = default;");
4685 verifyFormat("Deleted &operator=(const Deleted &)&& = delete;");
4686 verifyGoogleFormat("Deleted& operator=(const Deleted&)& = default;");
4687 verifyGoogleFormat("Deleted& operator=(const Deleted&)&& = delete;");
4688
4689 verifyFormat("string // break\n"
4690 "operator()() & {}");
4691 verifyFormat("string // break\n"
4692 "operator()() && {}");
4693 }
4694
TEST_F(FormatTest,UnderstandsNewAndDelete)4695 TEST_F(FormatTest, UnderstandsNewAndDelete) {
4696 verifyFormat("void f() {\n"
4697 " A *a = new A;\n"
4698 " A *a = new (placement) A;\n"
4699 " delete a;\n"
4700 " delete (A *)a;\n"
4701 "}");
4702 }
4703
TEST_F(FormatTest,UnderstandsUsesOfStarAndAmp)4704 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
4705 verifyFormat("int *f(int *a) {}");
4706 verifyFormat("int main(int argc, char **argv) {}");
4707 verifyFormat("Test::Test(int b) : a(b * b) {}");
4708 verifyIndependentOfContext("f(a, *a);");
4709 verifyFormat("void g() { f(*a); }");
4710 verifyIndependentOfContext("int a = b * 10;");
4711 verifyIndependentOfContext("int a = 10 * b;");
4712 verifyIndependentOfContext("int a = b * c;");
4713 verifyIndependentOfContext("int a += b * c;");
4714 verifyIndependentOfContext("int a -= b * c;");
4715 verifyIndependentOfContext("int a *= b * c;");
4716 verifyIndependentOfContext("int a /= b * c;");
4717 verifyIndependentOfContext("int a = *b;");
4718 verifyIndependentOfContext("int a = *b * c;");
4719 verifyIndependentOfContext("int a = b * *c;");
4720 verifyIndependentOfContext("return 10 * b;");
4721 verifyIndependentOfContext("return *b * *c;");
4722 verifyIndependentOfContext("return a & ~b;");
4723 verifyIndependentOfContext("f(b ? *c : *d);");
4724 verifyIndependentOfContext("int a = b ? *c : *d;");
4725 verifyIndependentOfContext("*b = a;");
4726 verifyIndependentOfContext("a * ~b;");
4727 verifyIndependentOfContext("a * !b;");
4728 verifyIndependentOfContext("a * +b;");
4729 verifyIndependentOfContext("a * -b;");
4730 verifyIndependentOfContext("a * ++b;");
4731 verifyIndependentOfContext("a * --b;");
4732 verifyIndependentOfContext("a[4] * b;");
4733 verifyIndependentOfContext("a[a * a] = 1;");
4734 verifyIndependentOfContext("f() * b;");
4735 verifyIndependentOfContext("a * [self dostuff];");
4736 verifyIndependentOfContext("int x = a * (a + b);");
4737 verifyIndependentOfContext("(a *)(a + b);");
4738 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
4739 verifyIndependentOfContext("int *pa = (int *)&a;");
4740 verifyIndependentOfContext("return sizeof(int **);");
4741 verifyIndependentOfContext("return sizeof(int ******);");
4742 verifyIndependentOfContext("return (int **&)a;");
4743 verifyIndependentOfContext("f((*PointerToArray)[10]);");
4744 verifyFormat("void f(Type (*parameter)[10]) {}");
4745 verifyGoogleFormat("return sizeof(int**);");
4746 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
4747 verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
4748 verifyFormat("auto a = [](int **&, int ***) {};");
4749 verifyFormat("auto PointerBinding = [](const char *S) {};");
4750 verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
4751 verifyFormat("[](const decltype(*a) &value) {}");
4752 verifyIndependentOfContext("typedef void (*f)(int *a);");
4753 verifyIndependentOfContext("int i{a * b};");
4754 verifyIndependentOfContext("aaa && aaa->f();");
4755
4756 verifyIndependentOfContext("InvalidRegions[*R] = 0;");
4757
4758 verifyIndependentOfContext("A<int *> a;");
4759 verifyIndependentOfContext("A<int **> a;");
4760 verifyIndependentOfContext("A<int *, int *> a;");
4761 verifyIndependentOfContext("A<int *[]> a;");
4762 verifyIndependentOfContext(
4763 "const char *const p = reinterpret_cast<const char *const>(q);");
4764 verifyIndependentOfContext("A<int **, int **> a;");
4765 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
4766 verifyFormat("for (char **a = b; *a; ++a) {\n}");
4767 verifyFormat("for (; a && b;) {\n}");
4768 verifyFormat("bool foo = true && [] { return false; }();");
4769
4770 verifyFormat(
4771 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4772 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4773
4774 verifyGoogleFormat("int main(int argc, char** argv) {}");
4775 verifyGoogleFormat("A<int*> a;");
4776 verifyGoogleFormat("A<int**> a;");
4777 verifyGoogleFormat("A<int*, int*> a;");
4778 verifyGoogleFormat("A<int**, int**> a;");
4779 verifyGoogleFormat("f(b ? *c : *d);");
4780 verifyGoogleFormat("int a = b ? *c : *d;");
4781 verifyGoogleFormat("Type* t = **x;");
4782 verifyGoogleFormat("Type* t = *++*x;");
4783 verifyGoogleFormat("*++*x;");
4784 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
4785 verifyGoogleFormat("Type* t = x++ * y;");
4786 verifyGoogleFormat(
4787 "const char* const p = reinterpret_cast<const char* const>(q);");
4788
4789 verifyIndependentOfContext("a = *(x + y);");
4790 verifyIndependentOfContext("a = &(x + y);");
4791 verifyIndependentOfContext("*(x + y).call();");
4792 verifyIndependentOfContext("&(x + y)->call();");
4793 verifyFormat("void f() { &(*I).first; }");
4794
4795 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
4796 verifyFormat(
4797 "int *MyValues = {\n"
4798 " *A, // Operator detection might be confused by the '{'\n"
4799 " *BB // Operator detection might be confused by previous comment\n"
4800 "};");
4801
4802 verifyIndependentOfContext("if (int *a = &b)");
4803 verifyIndependentOfContext("if (int &a = *b)");
4804 verifyIndependentOfContext("if (a & b[i])");
4805 verifyIndependentOfContext("if (a::b::c::d & b[i])");
4806 verifyIndependentOfContext("if (*b[i])");
4807 verifyIndependentOfContext("if (int *a = (&b))");
4808 verifyIndependentOfContext("while (int *a = &b)");
4809 verifyIndependentOfContext("size = sizeof *a;");
4810 verifyFormat("void f() {\n"
4811 " for (const int &v : Values) {\n"
4812 " }\n"
4813 "}");
4814 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
4815 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
4816 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
4817
4818 verifyFormat("#define A (!a * b)");
4819 verifyFormat("#define MACRO \\\n"
4820 " int *i = a * b; \\\n"
4821 " void f(a *b);",
4822 getLLVMStyleWithColumns(19));
4823
4824 verifyIndependentOfContext("A = new SomeType *[Length];");
4825 verifyIndependentOfContext("A = new SomeType *[Length]();");
4826 verifyIndependentOfContext("T **t = new T *;");
4827 verifyIndependentOfContext("T **t = new T *();");
4828 verifyGoogleFormat("A = new SomeType* [Length]();");
4829 verifyGoogleFormat("A = new SomeType* [Length];");
4830 verifyGoogleFormat("T** t = new T*;");
4831 verifyGoogleFormat("T** t = new T*();");
4832
4833 FormatStyle PointerLeft = getLLVMStyle();
4834 PointerLeft.PointerAlignment = FormatStyle::PAS_Left;
4835 verifyFormat("delete *x;", PointerLeft);
4836 verifyFormat("STATIC_ASSERT((a & b) == 0);");
4837 verifyFormat("STATIC_ASSERT(0 == (a & b));");
4838 verifyFormat("template <bool a, bool b> "
4839 "typename t::if<x && y>::type f() {}");
4840 verifyFormat("template <int *y> f() {}");
4841 verifyFormat("vector<int *> v;");
4842 verifyFormat("vector<int *const> v;");
4843 verifyFormat("vector<int *const **const *> v;");
4844 verifyFormat("vector<int *volatile> v;");
4845 verifyFormat("vector<a * b> v;");
4846 verifyFormat("foo<b && false>();");
4847 verifyFormat("foo<b & 1>();");
4848
4849 verifyIndependentOfContext("MACRO(int *i);");
4850 verifyIndependentOfContext("MACRO(auto *a);");
4851 verifyIndependentOfContext("MACRO(const A *a);");
4852 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
4853 // FIXME: Is there a way to make this work?
4854 // verifyIndependentOfContext("MACRO(A *a);");
4855
4856 verifyFormat("DatumHandle const *operator->() const { return input_; }");
4857
4858 EXPECT_EQ("#define OP(x) \\\n"
4859 " ostream &operator<<(ostream &s, const A &a) { \\\n"
4860 " return s << a.DebugString(); \\\n"
4861 " }",
4862 format("#define OP(x) \\\n"
4863 " ostream &operator<<(ostream &s, const A &a) { \\\n"
4864 " return s << a.DebugString(); \\\n"
4865 " }",
4866 getLLVMStyleWithColumns(50)));
4867
4868 // FIXME: We cannot handle this case yet; we might be able to figure out that
4869 // foo<x> d > v; doesn't make sense.
4870 verifyFormat("foo<a < b && c> d > v;");
4871
4872 FormatStyle PointerMiddle = getLLVMStyle();
4873 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
4874 verifyFormat("delete *x;", PointerMiddle);
4875 verifyFormat("int * x;", PointerMiddle);
4876 verifyFormat("template <int * y> f() {}", PointerMiddle);
4877 verifyFormat("int * f(int * a) {}", PointerMiddle);
4878 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
4879 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
4880 verifyFormat("A<int *> a;", PointerMiddle);
4881 verifyFormat("A<int **> a;", PointerMiddle);
4882 verifyFormat("A<int *, int *> a;", PointerMiddle);
4883 verifyFormat("A<int * []> a;", PointerMiddle);
4884 verifyFormat("A = new SomeType * [Length]();", PointerMiddle);
4885 verifyFormat("A = new SomeType * [Length];", PointerMiddle);
4886 verifyFormat("T ** t = new T *;", PointerMiddle);
4887 }
4888
TEST_F(FormatTest,UnderstandsAttributes)4889 TEST_F(FormatTest, UnderstandsAttributes) {
4890 verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
4891 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
4892 "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
4893 }
4894
TEST_F(FormatTest,UnderstandsEllipsis)4895 TEST_F(FormatTest, UnderstandsEllipsis) {
4896 verifyFormat("int printf(const char *fmt, ...);");
4897 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
4898 verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}");
4899
4900 FormatStyle PointersLeft = getLLVMStyle();
4901 PointersLeft.PointerAlignment = FormatStyle::PAS_Left;
4902 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft);
4903 }
4904
TEST_F(FormatTest,AdaptivelyFormatsPointersAndReferences)4905 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
4906 EXPECT_EQ("int *a;\n"
4907 "int *a;\n"
4908 "int *a;",
4909 format("int *a;\n"
4910 "int* a;\n"
4911 "int *a;",
4912 getGoogleStyle()));
4913 EXPECT_EQ("int* a;\n"
4914 "int* a;\n"
4915 "int* a;",
4916 format("int* a;\n"
4917 "int* a;\n"
4918 "int *a;",
4919 getGoogleStyle()));
4920 EXPECT_EQ("int *a;\n"
4921 "int *a;\n"
4922 "int *a;",
4923 format("int *a;\n"
4924 "int * a;\n"
4925 "int * a;",
4926 getGoogleStyle()));
4927 }
4928
TEST_F(FormatTest,UnderstandsRvalueReferences)4929 TEST_F(FormatTest, UnderstandsRvalueReferences) {
4930 verifyFormat("int f(int &&a) {}");
4931 verifyFormat("int f(int a, char &&b) {}");
4932 verifyFormat("void f() { int &&a = b; }");
4933 verifyGoogleFormat("int f(int a, char&& b) {}");
4934 verifyGoogleFormat("void f() { int&& a = b; }");
4935
4936 verifyIndependentOfContext("A<int &&> a;");
4937 verifyIndependentOfContext("A<int &&, int &&> a;");
4938 verifyGoogleFormat("A<int&&> a;");
4939 verifyGoogleFormat("A<int&&, int&&> a;");
4940
4941 // Not rvalue references:
4942 verifyFormat("template <bool B, bool C> class A {\n"
4943 " static_assert(B && C, \"Something is wrong\");\n"
4944 "};");
4945 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
4946 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
4947 verifyFormat("#define A(a, b) (a && b)");
4948 }
4949
TEST_F(FormatTest,FormatsBinaryOperatorsPrecedingEquals)4950 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
4951 verifyFormat("void f() {\n"
4952 " x[aaaaaaaaa -\n"
4953 " b] = 23;\n"
4954 "}",
4955 getLLVMStyleWithColumns(15));
4956 }
4957
TEST_F(FormatTest,FormatsCasts)4958 TEST_F(FormatTest, FormatsCasts) {
4959 verifyFormat("Type *A = static_cast<Type *>(P);");
4960 verifyFormat("Type *A = (Type *)P;");
4961 verifyFormat("Type *A = (vector<Type *, int *>)P;");
4962 verifyFormat("int a = (int)(2.0f);");
4963 verifyFormat("int a = (int)2.0f;");
4964 verifyFormat("x[(int32)y];");
4965 verifyFormat("x = (int32)y;");
4966 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
4967 verifyFormat("int a = (int)*b;");
4968 verifyFormat("int a = (int)2.0f;");
4969 verifyFormat("int a = (int)~0;");
4970 verifyFormat("int a = (int)++a;");
4971 verifyFormat("int a = (int)sizeof(int);");
4972 verifyFormat("int a = (int)+2;");
4973 verifyFormat("my_int a = (my_int)2.0f;");
4974 verifyFormat("my_int a = (my_int)sizeof(int);");
4975 verifyFormat("return (my_int)aaa;");
4976 verifyFormat("#define x ((int)-1)");
4977 verifyFormat("#define p(q) ((int *)&q)");
4978
4979 verifyFormat("void f() { my_int a = (my_int)*b; }");
4980 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
4981 verifyFormat("my_int a = (my_int)~0;");
4982 verifyFormat("my_int a = (my_int)++a;");
4983 verifyFormat("my_int a = (my_int)+2;");
4984 verifyFormat("my_int a = (my_int)1;");
4985 verifyFormat("my_int a = (my_int *)1;");
4986 verifyFormat("my_int a = (const my_int)-1;");
4987 verifyFormat("my_int a = (const my_int *)-1;");
4988
4989 // FIXME: single value wrapped with paren will be treated as cast.
4990 verifyFormat("void f(int i = (kValue)*kMask) {}");
4991
4992 verifyFormat("{ (void)F; }");
4993
4994 // Don't break after a cast's
4995 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4996 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
4997 " bbbbbbbbbbbbbbbbbbbbbb);");
4998
4999 // These are not casts.
5000 verifyFormat("void f(int *) {}");
5001 verifyFormat("f(foo)->b;");
5002 verifyFormat("f(foo).b;");
5003 verifyFormat("f(foo)(b);");
5004 verifyFormat("f(foo)[b];");
5005 verifyFormat("[](foo) { return 4; }(bar);");
5006 verifyFormat("(*funptr)(foo)[4];");
5007 verifyFormat("funptrs[4](foo)[4];");
5008 verifyFormat("void f(int *);");
5009 verifyFormat("void f(int *) = 0;");
5010 verifyFormat("void f(SmallVector<int>) {}");
5011 verifyFormat("void f(SmallVector<int>);");
5012 verifyFormat("void f(SmallVector<int>) = 0;");
5013 verifyFormat("void f(int i = (kA * kB) & kMask) {}");
5014 verifyFormat("int a = sizeof(int) * b;");
5015 verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
5016 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
5017 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
5018 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
5019
5020 // These are not casts, but at some point were confused with casts.
5021 verifyFormat("virtual void foo(int *) override;");
5022 verifyFormat("virtual void foo(char &) const;");
5023 verifyFormat("virtual void foo(int *a, char *) const;");
5024 verifyFormat("int a = sizeof(int *) + b;");
5025 verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
5026
5027 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
5028 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
5029 // FIXME: The indentation here is not ideal.
5030 verifyFormat(
5031 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5032 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
5033 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
5034 }
5035
TEST_F(FormatTest,FormatsFunctionTypes)5036 TEST_F(FormatTest, FormatsFunctionTypes) {
5037 verifyFormat("A<bool()> a;");
5038 verifyFormat("A<SomeType()> a;");
5039 verifyFormat("A<void (*)(int, std::string)> a;");
5040 verifyFormat("A<void *(int)>;");
5041 verifyFormat("void *(*a)(int *, SomeType *);");
5042 verifyFormat("int (*func)(void *);");
5043 verifyFormat("void f() { int (*func)(void *); }");
5044 verifyFormat("template <class CallbackClass>\n"
5045 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
5046
5047 verifyGoogleFormat("A<void*(int*, SomeType*)>;");
5048 verifyGoogleFormat("void* (*a)(int);");
5049 verifyGoogleFormat(
5050 "template <class CallbackClass>\n"
5051 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
5052
5053 // Other constructs can look somewhat like function types:
5054 verifyFormat("A<sizeof(*x)> a;");
5055 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
5056 verifyFormat("some_var = function(*some_pointer_var)[0];");
5057 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
5058 }
5059
TEST_F(FormatTest,BreaksLongVariableDeclarations)5060 TEST_F(FormatTest, BreaksLongVariableDeclarations) {
5061 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5062 " LoooooooooooooooooooooooooooooooooooooooongVariable;");
5063 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
5064 " LoooooooooooooooooooooooooooooooooooooooongVariable;");
5065
5066 // Different ways of ()-initializiation.
5067 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5068 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
5069 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5070 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
5071 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5072 " LoooooooooooooooooooooooooooooooooooooooongVariable({});");
5073 }
5074
TEST_F(FormatTest,BreaksLongDeclarations)5075 TEST_F(FormatTest, BreaksLongDeclarations) {
5076 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
5077 " AnotherNameForTheLongType;");
5078 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
5079 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5080 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5081 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
5082 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5083 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5084 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
5085 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5086 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
5087 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5088 FormatStyle Indented = getLLVMStyle();
5089 Indented.IndentWrappedFunctionNames = true;
5090 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5091 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
5092 Indented);
5093 verifyFormat(
5094 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5095 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
5096 Indented);
5097 verifyFormat(
5098 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
5099 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
5100 Indented);
5101 verifyFormat(
5102 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
5103 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
5104 Indented);
5105
5106 // FIXME: Without the comment, this breaks after "(".
5107 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n"
5108 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
5109 getGoogleStyle());
5110
5111 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
5112 " int LoooooooooooooooooooongParam2) {}");
5113 verifyFormat(
5114 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
5115 " SourceLocation L, IdentifierIn *II,\n"
5116 " Type *T) {}");
5117 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
5118 "ReallyReallyLongFunctionName(\n"
5119 " const std::string &SomeParameter,\n"
5120 " const SomeType<string, SomeOtherTemplateParameter> &\n"
5121 " ReallyReallyLongParameterName,\n"
5122 " const SomeType<string, SomeOtherTemplateParameter> &\n"
5123 " AnotherLongParameterName) {}");
5124 verifyFormat("template <typename A>\n"
5125 "SomeLoooooooooooooooooooooongType<\n"
5126 " typename some_namespace::SomeOtherType<A>::Type>\n"
5127 "Function() {}");
5128
5129 verifyGoogleFormat(
5130 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
5131 " aaaaaaaaaaaaaaaaaaaaaaa;");
5132 verifyGoogleFormat(
5133 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
5134 " SourceLocation L) {}");
5135 verifyGoogleFormat(
5136 "some_namespace::LongReturnType\n"
5137 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
5138 " int first_long_parameter, int second_parameter) {}");
5139
5140 verifyGoogleFormat("template <typename T>\n"
5141 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
5142 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
5143 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5144 " int aaaaaaaaaaaaaaaaaaaaaaa);");
5145 }
5146
TEST_F(FormatTest,FormatsArrays)5147 TEST_F(FormatTest, FormatsArrays) {
5148 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
5149 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
5150 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5151 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
5152 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5153 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
5154 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5155 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
5156 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
5157 verifyFormat(
5158 "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
5159 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
5160 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
5161
5162 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
5163 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
5164 verifyFormat(
5165 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
5166 " .aaaaaaa[0]\n"
5167 " .aaaaaaaaaaaaaaaaaaaaaa();");
5168 }
5169
TEST_F(FormatTest,LineStartsWithSpecialCharacter)5170 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
5171 verifyFormat("(a)->b();");
5172 verifyFormat("--a;");
5173 }
5174
TEST_F(FormatTest,HandlesIncludeDirectives)5175 TEST_F(FormatTest, HandlesIncludeDirectives) {
5176 verifyFormat("#include <string>\n"
5177 "#include <a/b/c.h>\n"
5178 "#include \"a/b/string\"\n"
5179 "#include \"string.h\"\n"
5180 "#include \"string.h\"\n"
5181 "#include <a-a>\n"
5182 "#include < path with space >\n"
5183 "#include \"abc.h\" // this is included for ABC\n"
5184 "#include \"some long include\" // with a comment\n"
5185 "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"",
5186 getLLVMStyleWithColumns(35));
5187 EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\""));
5188 EXPECT_EQ("#include <a>", format("#include<a>"));
5189
5190 verifyFormat("#import <string>");
5191 verifyFormat("#import <a/b/c.h>");
5192 verifyFormat("#import \"a/b/string\"");
5193 verifyFormat("#import \"string.h\"");
5194 verifyFormat("#import \"string.h\"");
5195 verifyFormat("#if __has_include(<strstream>)\n"
5196 "#include <strstream>\n"
5197 "#endif");
5198
5199 // Protocol buffer definition or missing "#".
5200 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
5201 getLLVMStyleWithColumns(30));
5202
5203 FormatStyle Style = getLLVMStyle();
5204 Style.AlwaysBreakBeforeMultilineStrings = true;
5205 Style.ColumnLimit = 0;
5206 verifyFormat("#import \"abc.h\"", Style);
5207 }
5208
5209 //===----------------------------------------------------------------------===//
5210 // Error recovery tests.
5211 //===----------------------------------------------------------------------===//
5212
TEST_F(FormatTest,IncompleteParameterLists)5213 TEST_F(FormatTest, IncompleteParameterLists) {
5214 FormatStyle NoBinPacking = getLLVMStyle();
5215 NoBinPacking.BinPackParameters = false;
5216 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
5217 " double *min_x,\n"
5218 " double *max_x,\n"
5219 " double *min_y,\n"
5220 " double *max_y,\n"
5221 " double *min_z,\n"
5222 " double *max_z, ) {}",
5223 NoBinPacking);
5224 }
5225
TEST_F(FormatTest,IncorrectCodeTrailingStuff)5226 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
5227 verifyFormat("void f() { return; }\n42");
5228 verifyFormat("void f() {\n"
5229 " if (0)\n"
5230 " return;\n"
5231 "}\n"
5232 "42");
5233 verifyFormat("void f() { return }\n42");
5234 verifyFormat("void f() {\n"
5235 " if (0)\n"
5236 " return\n"
5237 "}\n"
5238 "42");
5239 }
5240
TEST_F(FormatTest,IncorrectCodeMissingSemicolon)5241 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
5242 EXPECT_EQ("void f() { return }", format("void f ( ) { return }"));
5243 EXPECT_EQ("void f() {\n"
5244 " if (a)\n"
5245 " return\n"
5246 "}",
5247 format("void f ( ) { if ( a ) return }"));
5248 EXPECT_EQ("namespace N {\n"
5249 "void f()\n"
5250 "}",
5251 format("namespace N { void f() }"));
5252 EXPECT_EQ("namespace N {\n"
5253 "void f() {}\n"
5254 "void g()\n"
5255 "}",
5256 format("namespace N { void f( ) { } void g( ) }"));
5257 }
5258
TEST_F(FormatTest,IndentationWithinColumnLimitNotPossible)5259 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
5260 verifyFormat("int aaaaaaaa =\n"
5261 " // Overlylongcomment\n"
5262 " b;",
5263 getLLVMStyleWithColumns(20));
5264 verifyFormat("function(\n"
5265 " ShortArgument,\n"
5266 " LoooooooooooongArgument);\n",
5267 getLLVMStyleWithColumns(20));
5268 }
5269
TEST_F(FormatTest,IncorrectAccessSpecifier)5270 TEST_F(FormatTest, IncorrectAccessSpecifier) {
5271 verifyFormat("public:");
5272 verifyFormat("class A {\n"
5273 "public\n"
5274 " void f() {}\n"
5275 "};");
5276 verifyFormat("public\n"
5277 "int qwerty;");
5278 verifyFormat("public\n"
5279 "B {}");
5280 verifyFormat("public\n"
5281 "{}");
5282 verifyFormat("public\n"
5283 "B { int x; }");
5284 }
5285
TEST_F(FormatTest,IncorrectCodeUnbalancedBraces)5286 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
5287 verifyFormat("{");
5288 verifyFormat("#})");
5289 }
5290
TEST_F(FormatTest,IncorrectCodeDoNoWhile)5291 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
5292 verifyFormat("do {\n}");
5293 verifyFormat("do {\n}\n"
5294 "f();");
5295 verifyFormat("do {\n}\n"
5296 "wheeee(fun);");
5297 verifyFormat("do {\n"
5298 " f();\n"
5299 "}");
5300 }
5301
TEST_F(FormatTest,IncorrectCodeMissingParens)5302 TEST_F(FormatTest, IncorrectCodeMissingParens) {
5303 verifyFormat("if {\n foo;\n foo();\n}");
5304 verifyFormat("switch {\n foo;\n foo();\n}");
5305 verifyFormat("for {\n foo;\n foo();\n}");
5306 verifyFormat("while {\n foo;\n foo();\n}");
5307 verifyFormat("do {\n foo;\n foo();\n} while;");
5308 }
5309
TEST_F(FormatTest,DoesNotTouchUnwrappedLinesWithErrors)5310 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
5311 verifyFormat("namespace {\n"
5312 "class Foo { Foo (\n"
5313 "};\n"
5314 "} // comment");
5315 }
5316
TEST_F(FormatTest,IncorrectCodeErrorDetection)5317 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
5318 EXPECT_EQ("{\n {}\n", format("{\n{\n}\n"));
5319 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n"));
5320 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n"));
5321 EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n"));
5322
5323 EXPECT_EQ("{\n"
5324 " {\n"
5325 " breakme(\n"
5326 " qwe);\n"
5327 " }\n",
5328 format("{\n"
5329 " {\n"
5330 " breakme(qwe);\n"
5331 "}\n",
5332 getLLVMStyleWithColumns(10)));
5333 }
5334
TEST_F(FormatTest,LayoutCallsInsideBraceInitializers)5335 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
5336 verifyFormat("int x = {\n"
5337 " avariable,\n"
5338 " b(alongervariable)};",
5339 getLLVMStyleWithColumns(25));
5340 }
5341
TEST_F(FormatTest,LayoutBraceInitializersInReturnStatement)5342 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
5343 verifyFormat("return (a)(b){1, 2, 3};");
5344 }
5345
TEST_F(FormatTest,LayoutCxx11BraceInitializers)5346 TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
5347 verifyFormat("vector<int> x{1, 2, 3, 4};");
5348 verifyFormat("vector<int> x{\n"
5349 " 1, 2, 3, 4,\n"
5350 "};");
5351 verifyFormat("vector<T> x{{}, {}, {}, {}};");
5352 verifyFormat("f({1, 2});");
5353 verifyFormat("auto v = Foo{-1};");
5354 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
5355 verifyFormat("Class::Class : member{1, 2, 3} {}");
5356 verifyFormat("new vector<int>{1, 2, 3};");
5357 verifyFormat("new int[3]{1, 2, 3};");
5358 verifyFormat("new int{1};");
5359 verifyFormat("return {arg1, arg2};");
5360 verifyFormat("return {arg1, SomeType{parameter}};");
5361 verifyFormat("int count = set<int>{f(), g(), h()}.size();");
5362 verifyFormat("new T{arg1, arg2};");
5363 verifyFormat("f(MyMap[{composite, key}]);");
5364 verifyFormat("class Class {\n"
5365 " T member = {arg1, arg2};\n"
5366 "};");
5367 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
5368 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
5369 verifyFormat("int a = std::is_integral<int>{} + 0;");
5370
5371 verifyFormat("int foo(int i) { return fo1{}(i); }");
5372 verifyFormat("int foo(int i) { return fo1{}(i); }");
5373 verifyFormat("auto i = decltype(x){};");
5374
5375 // In combination with BinPackParameters = false.
5376 FormatStyle NoBinPacking = getLLVMStyle();
5377 NoBinPacking.BinPackParameters = false;
5378 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
5379 " bbbbb,\n"
5380 " ccccc,\n"
5381 " ddddd,\n"
5382 " eeeee,\n"
5383 " ffffff,\n"
5384 " ggggg,\n"
5385 " hhhhhh,\n"
5386 " iiiiii,\n"
5387 " jjjjjj,\n"
5388 " kkkkkk};",
5389 NoBinPacking);
5390 verifyFormat("const Aaaaaa aaaaa = {\n"
5391 " aaaaa,\n"
5392 " bbbbb,\n"
5393 " ccccc,\n"
5394 " ddddd,\n"
5395 " eeeee,\n"
5396 " ffffff,\n"
5397 " ggggg,\n"
5398 " hhhhhh,\n"
5399 " iiiiii,\n"
5400 " jjjjjj,\n"
5401 " kkkkkk,\n"
5402 "};",
5403 NoBinPacking);
5404
5405 // FIXME: The alignment of these trailing comments might be bad. Then again,
5406 // this might be utterly useless in real code.
5407 verifyFormat("Constructor::Constructor()\n"
5408 " : some_value{ //\n"
5409 " aaaaaaa //\n"
5410 " } {}");
5411
5412 // In braced lists, the first comment is always assumed to belong to the
5413 // first element. Thus, it can be moved to the next or previous line as
5414 // appropriate.
5415 EXPECT_EQ("function({// First element:\n"
5416 " 1,\n"
5417 " // Second element:\n"
5418 " 2});",
5419 format("function({\n"
5420 " // First element:\n"
5421 " 1,\n"
5422 " // Second element:\n"
5423 " 2});"));
5424 EXPECT_EQ("std::vector<int> MyNumbers{\n"
5425 " // First element:\n"
5426 " 1,\n"
5427 " // Second element:\n"
5428 " 2};",
5429 format("std::vector<int> MyNumbers{// First element:\n"
5430 " 1,\n"
5431 " // Second element:\n"
5432 " 2};",
5433 getLLVMStyleWithColumns(30)));
5434
5435 FormatStyle ExtraSpaces = getLLVMStyle();
5436 ExtraSpaces.Cpp11BracedListStyle = false;
5437 ExtraSpaces.ColumnLimit = 75;
5438 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
5439 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
5440 verifyFormat("f({ 1, 2 });", ExtraSpaces);
5441 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
5442 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
5443 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
5444 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
5445 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
5446 verifyFormat("return { arg1, arg2 };", ExtraSpaces);
5447 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
5448 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
5449 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
5450 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
5451 verifyFormat("class Class {\n"
5452 " T member = { arg1, arg2 };\n"
5453 "};",
5454 ExtraSpaces);
5455 verifyFormat(
5456 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5457 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
5458 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
5459 " bbbbbbbbbbbbbbbbbbbb, bbbbb };",
5460 ExtraSpaces);
5461 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
5462 verifyFormat("DoSomethingWithVector({\n"
5463 " {} /* No data */\n"
5464 " },\n"
5465 " { { 1, 2 } });",
5466 ExtraSpaces);
5467 verifyFormat(
5468 "someFunction(OtherParam,\n"
5469 " BracedList{ // comment 1 (Forcing interesting break)\n"
5470 " param1, param2,\n"
5471 " // comment 2\n"
5472 " param3, param4\n"
5473 " });",
5474 ExtraSpaces);
5475 verifyFormat(
5476 "std::this_thread::sleep_for(\n"
5477 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
5478 ExtraSpaces);
5479 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{\n"
5480 " aaaaaaa, aaaaaaaaaa,\n"
5481 " aaaaa, aaaaaaaaaaaaaaa,\n"
5482 " aaa, aaaaaaaaaa,\n"
5483 " a, aaaaaaaaaaaaaaaaaaaaa,\n"
5484 " aaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
5485 " aaaaaaa, a\n"
5486 "};",
5487 ExtraSpaces);
5488 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
5489 }
5490
TEST_F(FormatTest,FormatsBracedListsInColumnLayout)5491 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
5492 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
5493 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
5494 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
5495 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
5496 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
5497 " 1, 22, 333, 4444, 55555, 666666, 7777777};");
5498 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
5499 " // line comment\n"
5500 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
5501 " 1, 22, 333, 4444, 55555,\n"
5502 " // line comment\n"
5503 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
5504 " 1, 22, 333, 4444, 55555, 666666, 7777777};");
5505 verifyFormat(
5506 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
5507 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
5508 " 1, 22, 333, 4444, 55555, 666666, // comment\n"
5509 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
5510 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
5511 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
5512 " 7777777};");
5513 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
5514 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
5515 " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
5516 verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
5517 " 1, 1, 1, 1};",
5518 getLLVMStyleWithColumns(39));
5519 verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
5520 " 1, 1, 1, 1};",
5521 getLLVMStyleWithColumns(38));
5522 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
5523 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
5524 getLLVMStyleWithColumns(43));
5525
5526 // Trailing commas.
5527 verifyFormat("vector<int> x = {\n"
5528 " 1, 1, 1, 1, 1, 1, 1, 1,\n"
5529 "};",
5530 getLLVMStyleWithColumns(39));
5531 verifyFormat("vector<int> x = {\n"
5532 " 1, 1, 1, 1, 1, 1, 1, 1, //\n"
5533 "};",
5534 getLLVMStyleWithColumns(39));
5535 verifyFormat("vector<int> x = {\n"
5536 " 1, 1, 1, 1, 1, 1, 1, 1,\n"
5537 " /**/ /**/\n"
5538 "};",
5539 getLLVMStyleWithColumns(39));
5540 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
5541 " {aaaaaaaaaaaaaaaaaaa},\n"
5542 " {aaaaaaaaaaaaaaaaaaaaa},\n"
5543 " {aaaaaaaaaaaaaaaaa}};",
5544 getLLVMStyleWithColumns(60));
5545
5546 // With nested lists, we should either format one item per line or all nested
5547 // lists one one line.
5548 // FIXME: For some nested lists, we can do better.
5549 verifyFormat(
5550 "SomeStruct my_struct_array = {\n"
5551 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
5552 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
5553 " {aaa, aaa},\n"
5554 " {aaa, aaa},\n"
5555 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
5556 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
5557 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
5558
5559 // No column layout should be used here.
5560 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
5561 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
5562 }
5563
TEST_F(FormatTest,PullTrivialFunctionDefinitionsIntoSingleLine)5564 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
5565 FormatStyle DoNotMerge = getLLVMStyle();
5566 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
5567
5568 verifyFormat("void f() { return 42; }");
5569 verifyFormat("void f() {\n"
5570 " return 42;\n"
5571 "}",
5572 DoNotMerge);
5573 verifyFormat("void f() {\n"
5574 " // Comment\n"
5575 "}");
5576 verifyFormat("{\n"
5577 "#error {\n"
5578 " int a;\n"
5579 "}");
5580 verifyFormat("{\n"
5581 " int a;\n"
5582 "#error {\n"
5583 "}");
5584 verifyFormat("void f() {} // comment");
5585 verifyFormat("void f() { int a; } // comment");
5586 verifyFormat("void f() {\n"
5587 "} // comment",
5588 DoNotMerge);
5589 verifyFormat("void f() {\n"
5590 " int a;\n"
5591 "} // comment",
5592 DoNotMerge);
5593 verifyFormat("void f() {\n"
5594 "} // comment",
5595 getLLVMStyleWithColumns(15));
5596
5597 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
5598 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22));
5599
5600 verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
5601 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
5602 verifyFormat("class C {\n"
5603 " C()\n"
5604 " : iiiiiiii(nullptr),\n"
5605 " kkkkkkk(nullptr),\n"
5606 " mmmmmmm(nullptr),\n"
5607 " nnnnnnn(nullptr) {}\n"
5608 "};",
5609 getGoogleStyle());
5610
5611 FormatStyle NoColumnLimit = getLLVMStyle();
5612 NoColumnLimit.ColumnLimit = 0;
5613 EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
5614 EXPECT_EQ("class C {\n"
5615 " A() : b(0) {}\n"
5616 "};", format("class C{A():b(0){}};", NoColumnLimit));
5617 EXPECT_EQ("A()\n"
5618 " : b(0) {\n"
5619 "}",
5620 format("A()\n:b(0)\n{\n}", NoColumnLimit));
5621
5622 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
5623 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
5624 FormatStyle::SFS_None;
5625 EXPECT_EQ("A()\n"
5626 " : b(0) {\n"
5627 "}",
5628 format("A():b(0){}", DoNotMergeNoColumnLimit));
5629 EXPECT_EQ("A()\n"
5630 " : b(0) {\n"
5631 "}",
5632 format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
5633
5634 verifyFormat("#define A \\\n"
5635 " void f() { \\\n"
5636 " int i; \\\n"
5637 " }",
5638 getLLVMStyleWithColumns(20));
5639 verifyFormat("#define A \\\n"
5640 " void f() { int i; }",
5641 getLLVMStyleWithColumns(21));
5642 verifyFormat("#define A \\\n"
5643 " void f() { \\\n"
5644 " int i; \\\n"
5645 " } \\\n"
5646 " int j;",
5647 getLLVMStyleWithColumns(22));
5648 verifyFormat("#define A \\\n"
5649 " void f() { int i; } \\\n"
5650 " int j;",
5651 getLLVMStyleWithColumns(23));
5652 }
5653
TEST_F(FormatTest,PullInlineFunctionDefinitionsIntoSingleLine)5654 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
5655 FormatStyle MergeInlineOnly = getLLVMStyle();
5656 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
5657 verifyFormat("class C {\n"
5658 " int f() { return 42; }\n"
5659 "};",
5660 MergeInlineOnly);
5661 verifyFormat("int f() {\n"
5662 " return 42;\n"
5663 "}",
5664 MergeInlineOnly);
5665 }
5666
TEST_F(FormatTest,UnderstandContextOfRecordTypeKeywords)5667 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
5668 // Elaborate type variable declarations.
5669 verifyFormat("struct foo a = {bar};\nint n;");
5670 verifyFormat("class foo a = {bar};\nint n;");
5671 verifyFormat("union foo a = {bar};\nint n;");
5672
5673 // Elaborate types inside function definitions.
5674 verifyFormat("struct foo f() {}\nint n;");
5675 verifyFormat("class foo f() {}\nint n;");
5676 verifyFormat("union foo f() {}\nint n;");
5677
5678 // Templates.
5679 verifyFormat("template <class X> void f() {}\nint n;");
5680 verifyFormat("template <struct X> void f() {}\nint n;");
5681 verifyFormat("template <union X> void f() {}\nint n;");
5682
5683 // Actual definitions...
5684 verifyFormat("struct {\n} n;");
5685 verifyFormat(
5686 "template <template <class T, class Y>, class Z> class X {\n} n;");
5687 verifyFormat("union Z {\n int n;\n} x;");
5688 verifyFormat("class MACRO Z {\n} n;");
5689 verifyFormat("class MACRO(X) Z {\n} n;");
5690 verifyFormat("class __attribute__(X) Z {\n} n;");
5691 verifyFormat("class __declspec(X) Z {\n} n;");
5692 verifyFormat("class A##B##C {\n} n;");
5693 verifyFormat("class alignas(16) Z {\n} n;");
5694
5695 // Redefinition from nested context:
5696 verifyFormat("class A::B::C {\n} n;");
5697
5698 // Template definitions.
5699 verifyFormat(
5700 "template <typename F>\n"
5701 "Matcher(const Matcher<F> &Other,\n"
5702 " typename enable_if_c<is_base_of<F, T>::value &&\n"
5703 " !is_same<F, T>::value>::type * = 0)\n"
5704 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
5705
5706 // FIXME: This is still incorrectly handled at the formatter side.
5707 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
5708
5709 // FIXME:
5710 // This now gets parsed incorrectly as class definition.
5711 // verifyFormat("class A<int> f() {\n}\nint n;");
5712
5713 // Elaborate types where incorrectly parsing the structural element would
5714 // break the indent.
5715 verifyFormat("if (true)\n"
5716 " class X x;\n"
5717 "else\n"
5718 " f();\n");
5719
5720 // This is simply incomplete. Formatting is not important, but must not crash.
5721 verifyFormat("class A:");
5722 }
5723
TEST_F(FormatTest,DoNotInterfereWithErrorAndWarning)5724 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
5725 EXPECT_EQ("#error Leave all white!!!!! space* alone!\n",
5726 format("#error Leave all white!!!!! space* alone!\n"));
5727 EXPECT_EQ(
5728 "#warning Leave all white!!!!! space* alone!\n",
5729 format("#warning Leave all white!!!!! space* alone!\n"));
5730 EXPECT_EQ("#error 1", format(" # error 1"));
5731 EXPECT_EQ("#warning 1", format(" # warning 1"));
5732 }
5733
TEST_F(FormatTest,FormatHashIfExpressions)5734 TEST_F(FormatTest, FormatHashIfExpressions) {
5735 verifyFormat("#if AAAA && BBBB");
5736 // FIXME: Come up with a better indentation for #elif.
5737 verifyFormat(
5738 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n"
5739 " defined(BBBBBBBB)\n"
5740 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n"
5741 " defined(BBBBBBBB)\n"
5742 "#endif",
5743 getLLVMStyleWithColumns(65));
5744 }
5745
TEST_F(FormatTest,MergeHandlingInTheFaceOfPreprocessorDirectives)5746 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
5747 FormatStyle AllowsMergedIf = getGoogleStyle();
5748 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
5749 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
5750 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
5751 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf);
5752 EXPECT_EQ("if (true) return 42;",
5753 format("if (true)\nreturn 42;", AllowsMergedIf));
5754 FormatStyle ShortMergedIf = AllowsMergedIf;
5755 ShortMergedIf.ColumnLimit = 25;
5756 verifyFormat("#define A \\\n"
5757 " if (true) return 42;",
5758 ShortMergedIf);
5759 verifyFormat("#define A \\\n"
5760 " f(); \\\n"
5761 " if (true)\n"
5762 "#define B",
5763 ShortMergedIf);
5764 verifyFormat("#define A \\\n"
5765 " f(); \\\n"
5766 " if (true)\n"
5767 "g();",
5768 ShortMergedIf);
5769 verifyFormat("{\n"
5770 "#ifdef A\n"
5771 " // Comment\n"
5772 " if (true) continue;\n"
5773 "#endif\n"
5774 " // Comment\n"
5775 " if (true) continue;\n"
5776 "}",
5777 ShortMergedIf);
5778 ShortMergedIf.ColumnLimit = 29;
5779 verifyFormat("#define A \\\n"
5780 " if (aaaaaaaaaa) return 1; \\\n"
5781 " return 2;",
5782 ShortMergedIf);
5783 ShortMergedIf.ColumnLimit = 28;
5784 verifyFormat("#define A \\\n"
5785 " if (aaaaaaaaaa) \\\n"
5786 " return 1; \\\n"
5787 " return 2;",
5788 ShortMergedIf);
5789 }
5790
TEST_F(FormatTest,BlockCommentsInControlLoops)5791 TEST_F(FormatTest, BlockCommentsInControlLoops) {
5792 verifyFormat("if (0) /* a comment in a strange place */ {\n"
5793 " f();\n"
5794 "}");
5795 verifyFormat("if (0) /* a comment in a strange place */ {\n"
5796 " f();\n"
5797 "} /* another comment */ else /* comment #3 */ {\n"
5798 " g();\n"
5799 "}");
5800 verifyFormat("while (0) /* a comment in a strange place */ {\n"
5801 " f();\n"
5802 "}");
5803 verifyFormat("for (;;) /* a comment in a strange place */ {\n"
5804 " f();\n"
5805 "}");
5806 verifyFormat("do /* a comment in a strange place */ {\n"
5807 " f();\n"
5808 "} /* another comment */ while (0);");
5809 }
5810
TEST_F(FormatTest,BlockComments)5811 TEST_F(FormatTest, BlockComments) {
5812 EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */",
5813 format("/* *//* */ /* */\n/* *//* */ /* */"));
5814 EXPECT_EQ("/* */ a /* */ b;", format(" /* */ a/* */ b;"));
5815 EXPECT_EQ("#define A /*123*/ \\\n"
5816 " b\n"
5817 "/* */\n"
5818 "someCall(\n"
5819 " parameter);",
5820 format("#define A /*123*/ b\n"
5821 "/* */\n"
5822 "someCall(parameter);",
5823 getLLVMStyleWithColumns(15)));
5824
5825 EXPECT_EQ("#define A\n"
5826 "/* */ someCall(\n"
5827 " parameter);",
5828 format("#define A\n"
5829 "/* */someCall(parameter);",
5830 getLLVMStyleWithColumns(15)));
5831 EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/"));
5832 EXPECT_EQ("/*\n"
5833 "*\n"
5834 " * aaaaaa\n"
5835 "*aaaaaa\n"
5836 "*/",
5837 format("/*\n"
5838 "*\n"
5839 " * aaaaaa aaaaaa\n"
5840 "*/",
5841 getLLVMStyleWithColumns(10)));
5842 EXPECT_EQ("/*\n"
5843 "**\n"
5844 "* aaaaaa\n"
5845 "*aaaaaa\n"
5846 "*/",
5847 format("/*\n"
5848 "**\n"
5849 "* aaaaaa aaaaaa\n"
5850 "*/",
5851 getLLVMStyleWithColumns(10)));
5852
5853 FormatStyle NoBinPacking = getLLVMStyle();
5854 NoBinPacking.BinPackParameters = false;
5855 EXPECT_EQ("someFunction(1, /* comment 1 */\n"
5856 " 2, /* comment 2 */\n"
5857 " 3, /* comment 3 */\n"
5858 " aaaa,\n"
5859 " bbbb);",
5860 format("someFunction (1, /* comment 1 */\n"
5861 " 2, /* comment 2 */ \n"
5862 " 3, /* comment 3 */\n"
5863 "aaaa, bbbb );",
5864 NoBinPacking));
5865 verifyFormat(
5866 "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5867 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5868 EXPECT_EQ(
5869 "bool aaaaaaaaaaaaa = /* trailing comment */\n"
5870 " aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5871 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;",
5872 format(
5873 "bool aaaaaaaaaaaaa = /* trailing comment */\n"
5874 " aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5875 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;"));
5876 EXPECT_EQ(
5877 "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
5878 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
5879 "int cccccccccccccccccccccccccccccc; /* comment */\n",
5880 format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
5881 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
5882 "int cccccccccccccccccccccccccccccc; /* comment */\n"));
5883
5884 verifyFormat("void f(int * /* unused */) {}");
5885
5886 EXPECT_EQ("/*\n"
5887 " **\n"
5888 " */",
5889 format("/*\n"
5890 " **\n"
5891 " */"));
5892 EXPECT_EQ("/*\n"
5893 " *q\n"
5894 " */",
5895 format("/*\n"
5896 " *q\n"
5897 " */"));
5898 EXPECT_EQ("/*\n"
5899 " * q\n"
5900 " */",
5901 format("/*\n"
5902 " * q\n"
5903 " */"));
5904 EXPECT_EQ("/*\n"
5905 " **/",
5906 format("/*\n"
5907 " **/"));
5908 EXPECT_EQ("/*\n"
5909 " ***/",
5910 format("/*\n"
5911 " ***/"));
5912 }
5913
TEST_F(FormatTest,BlockCommentsInMacros)5914 TEST_F(FormatTest, BlockCommentsInMacros) {
5915 EXPECT_EQ("#define A \\\n"
5916 " { \\\n"
5917 " /* one line */ \\\n"
5918 " someCall();",
5919 format("#define A { \\\n"
5920 " /* one line */ \\\n"
5921 " someCall();",
5922 getLLVMStyleWithColumns(20)));
5923 EXPECT_EQ("#define A \\\n"
5924 " { \\\n"
5925 " /* previous */ \\\n"
5926 " /* one line */ \\\n"
5927 " someCall();",
5928 format("#define A { \\\n"
5929 " /* previous */ \\\n"
5930 " /* one line */ \\\n"
5931 " someCall();",
5932 getLLVMStyleWithColumns(20)));
5933 }
5934
TEST_F(FormatTest,BlockCommentsAtEndOfLine)5935 TEST_F(FormatTest, BlockCommentsAtEndOfLine) {
5936 EXPECT_EQ("a = {\n"
5937 " 1111 /* */\n"
5938 "};",
5939 format("a = {1111 /* */\n"
5940 "};",
5941 getLLVMStyleWithColumns(15)));
5942 EXPECT_EQ("a = {\n"
5943 " 1111 /* */\n"
5944 "};",
5945 format("a = {1111 /* */\n"
5946 "};",
5947 getLLVMStyleWithColumns(15)));
5948
5949 // FIXME: The formatting is still wrong here.
5950 EXPECT_EQ("a = {\n"
5951 " 1111 /* a\n"
5952 " */\n"
5953 "};",
5954 format("a = {1111 /* a */\n"
5955 "};",
5956 getLLVMStyleWithColumns(15)));
5957 }
5958
TEST_F(FormatTest,IndentLineCommentsInStartOfBlockAtEndOfFile)5959 TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) {
5960 // FIXME: This is not what we want...
5961 verifyFormat("{\n"
5962 "// a"
5963 "// b");
5964 }
5965
TEST_F(FormatTest,FormatStarDependingOnContext)5966 TEST_F(FormatTest, FormatStarDependingOnContext) {
5967 verifyFormat("void f(int *a);");
5968 verifyFormat("void f() { f(fint * b); }");
5969 verifyFormat("class A {\n void f(int *a);\n};");
5970 verifyFormat("class A {\n int *a;\n};");
5971 verifyFormat("namespace a {\n"
5972 "namespace b {\n"
5973 "class A {\n"
5974 " void f() {}\n"
5975 " int *a;\n"
5976 "};\n"
5977 "}\n"
5978 "}");
5979 }
5980
TEST_F(FormatTest,SpecialTokensAtEndOfLine)5981 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
5982 verifyFormat("while");
5983 verifyFormat("operator");
5984 }
5985
5986 //===----------------------------------------------------------------------===//
5987 // Objective-C tests.
5988 //===----------------------------------------------------------------------===//
5989
TEST_F(FormatTest,FormatForObjectiveCMethodDecls)5990 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
5991 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
5992 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
5993 format("-(NSUInteger)indexOfObject:(id)anObject;"));
5994 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
5995 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
5996 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
5997 format("-(NSInteger)Method3:(id)anObject;"));
5998 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
5999 format("-(NSInteger)Method4:(id)anObject;"));
6000 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
6001 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
6002 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
6003 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
6004 EXPECT_EQ(
6005 "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;",
6006 format(
6007 "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;"));
6008
6009 // Very long objectiveC method declaration.
6010 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
6011 " inRange:(NSRange)range\n"
6012 " outRange:(NSRange)out_range\n"
6013 " outRange1:(NSRange)out_range1\n"
6014 " outRange2:(NSRange)out_range2\n"
6015 " outRange3:(NSRange)out_range3\n"
6016 " outRange4:(NSRange)out_range4\n"
6017 " outRange5:(NSRange)out_range5\n"
6018 " outRange6:(NSRange)out_range6\n"
6019 " outRange7:(NSRange)out_range7\n"
6020 " outRange8:(NSRange)out_range8\n"
6021 " outRange9:(NSRange)out_range9;");
6022
6023 verifyFormat("- (int)sum:(vector<int>)numbers;");
6024 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
6025 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
6026 // protocol lists (but not for template classes):
6027 //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
6028
6029 verifyFormat("- (int (*)())foo:(int (*)())f;");
6030 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
6031
6032 // If there's no return type (very rare in practice!), LLVM and Google style
6033 // agree.
6034 verifyFormat("- foo;");
6035 verifyFormat("- foo:(int)f;");
6036 verifyGoogleFormat("- foo:(int)foo;");
6037 }
6038
TEST_F(FormatTest,FormatObjCInterface)6039 TEST_F(FormatTest, FormatObjCInterface) {
6040 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
6041 "@public\n"
6042 " int field1;\n"
6043 "@protected\n"
6044 " int field2;\n"
6045 "@private\n"
6046 " int field3;\n"
6047 "@package\n"
6048 " int field4;\n"
6049 "}\n"
6050 "+ (id)init;\n"
6051 "@end");
6052
6053 verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
6054 " @public\n"
6055 " int field1;\n"
6056 " @protected\n"
6057 " int field2;\n"
6058 " @private\n"
6059 " int field3;\n"
6060 " @package\n"
6061 " int field4;\n"
6062 "}\n"
6063 "+ (id)init;\n"
6064 "@end");
6065
6066 verifyFormat("@interface /* wait for it */ Foo\n"
6067 "+ (id)init;\n"
6068 "// Look, a comment!\n"
6069 "- (int)answerWith:(int)i;\n"
6070 "@end");
6071
6072 verifyFormat("@interface Foo\n"
6073 "@end\n"
6074 "@interface Bar\n"
6075 "@end");
6076
6077 verifyFormat("@interface Foo : Bar\n"
6078 "+ (id)init;\n"
6079 "@end");
6080
6081 verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n"
6082 "+ (id)init;\n"
6083 "@end");
6084
6085 verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
6086 "+ (id)init;\n"
6087 "@end");
6088
6089 verifyFormat("@interface Foo (HackStuff)\n"
6090 "+ (id)init;\n"
6091 "@end");
6092
6093 verifyFormat("@interface Foo ()\n"
6094 "+ (id)init;\n"
6095 "@end");
6096
6097 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
6098 "+ (id)init;\n"
6099 "@end");
6100
6101 verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
6102 "+ (id)init;\n"
6103 "@end");
6104
6105 verifyFormat("@interface Foo {\n"
6106 " int _i;\n"
6107 "}\n"
6108 "+ (id)init;\n"
6109 "@end");
6110
6111 verifyFormat("@interface Foo : Bar {\n"
6112 " int _i;\n"
6113 "}\n"
6114 "+ (id)init;\n"
6115 "@end");
6116
6117 verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
6118 " int _i;\n"
6119 "}\n"
6120 "+ (id)init;\n"
6121 "@end");
6122
6123 verifyFormat("@interface Foo (HackStuff) {\n"
6124 " int _i;\n"
6125 "}\n"
6126 "+ (id)init;\n"
6127 "@end");
6128
6129 verifyFormat("@interface Foo () {\n"
6130 " int _i;\n"
6131 "}\n"
6132 "+ (id)init;\n"
6133 "@end");
6134
6135 verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
6136 " int _i;\n"
6137 "}\n"
6138 "+ (id)init;\n"
6139 "@end");
6140
6141 FormatStyle OnePerLine = getGoogleStyle();
6142 OnePerLine.BinPackParameters = false;
6143 verifyFormat("@interface aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ()<\n"
6144 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6145 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6146 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6147 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
6148 "}",
6149 OnePerLine);
6150 }
6151
TEST_F(FormatTest,FormatObjCImplementation)6152 TEST_F(FormatTest, FormatObjCImplementation) {
6153 verifyFormat("@implementation Foo : NSObject {\n"
6154 "@public\n"
6155 " int field1;\n"
6156 "@protected\n"
6157 " int field2;\n"
6158 "@private\n"
6159 " int field3;\n"
6160 "@package\n"
6161 " int field4;\n"
6162 "}\n"
6163 "+ (id)init {\n}\n"
6164 "@end");
6165
6166 verifyGoogleFormat("@implementation Foo : NSObject {\n"
6167 " @public\n"
6168 " int field1;\n"
6169 " @protected\n"
6170 " int field2;\n"
6171 " @private\n"
6172 " int field3;\n"
6173 " @package\n"
6174 " int field4;\n"
6175 "}\n"
6176 "+ (id)init {\n}\n"
6177 "@end");
6178
6179 verifyFormat("@implementation Foo\n"
6180 "+ (id)init {\n"
6181 " if (true)\n"
6182 " return nil;\n"
6183 "}\n"
6184 "// Look, a comment!\n"
6185 "- (int)answerWith:(int)i {\n"
6186 " return i;\n"
6187 "}\n"
6188 "+ (int)answerWith:(int)i {\n"
6189 " return i;\n"
6190 "}\n"
6191 "@end");
6192
6193 verifyFormat("@implementation Foo\n"
6194 "@end\n"
6195 "@implementation Bar\n"
6196 "@end");
6197
6198 EXPECT_EQ("@implementation Foo : Bar\n"
6199 "+ (id)init {\n}\n"
6200 "- (void)foo {\n}\n"
6201 "@end",
6202 format("@implementation Foo : Bar\n"
6203 "+(id)init{}\n"
6204 "-(void)foo{}\n"
6205 "@end"));
6206
6207 verifyFormat("@implementation Foo {\n"
6208 " int _i;\n"
6209 "}\n"
6210 "+ (id)init {\n}\n"
6211 "@end");
6212
6213 verifyFormat("@implementation Foo : Bar {\n"
6214 " int _i;\n"
6215 "}\n"
6216 "+ (id)init {\n}\n"
6217 "@end");
6218
6219 verifyFormat("@implementation Foo (HackStuff)\n"
6220 "+ (id)init {\n}\n"
6221 "@end");
6222 verifyFormat("@implementation ObjcClass\n"
6223 "- (void)method;\n"
6224 "{}\n"
6225 "@end");
6226 }
6227
TEST_F(FormatTest,FormatObjCProtocol)6228 TEST_F(FormatTest, FormatObjCProtocol) {
6229 verifyFormat("@protocol Foo\n"
6230 "@property(weak) id delegate;\n"
6231 "- (NSUInteger)numberOfThings;\n"
6232 "@end");
6233
6234 verifyFormat("@protocol MyProtocol <NSObject>\n"
6235 "- (NSUInteger)numberOfThings;\n"
6236 "@end");
6237
6238 verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
6239 "- (NSUInteger)numberOfThings;\n"
6240 "@end");
6241
6242 verifyFormat("@protocol Foo;\n"
6243 "@protocol Bar;\n");
6244
6245 verifyFormat("@protocol Foo\n"
6246 "@end\n"
6247 "@protocol Bar\n"
6248 "@end");
6249
6250 verifyFormat("@protocol myProtocol\n"
6251 "- (void)mandatoryWithInt:(int)i;\n"
6252 "@optional\n"
6253 "- (void)optional;\n"
6254 "@required\n"
6255 "- (void)required;\n"
6256 "@optional\n"
6257 "@property(assign) int madProp;\n"
6258 "@end\n");
6259
6260 verifyFormat("@property(nonatomic, assign, readonly)\n"
6261 " int *looooooooooooooooooooooooooooongNumber;\n"
6262 "@property(nonatomic, assign, readonly)\n"
6263 " NSString *looooooooooooooooooooooooooooongName;");
6264
6265 verifyFormat("@implementation PR18406\n"
6266 "}\n"
6267 "@end");
6268 }
6269
TEST_F(FormatTest,FormatObjCMethodDeclarations)6270 TEST_F(FormatTest, FormatObjCMethodDeclarations) {
6271 verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n"
6272 " rect:(NSRect)theRect\n"
6273 " interval:(float)theInterval {\n"
6274 "}");
6275 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
6276 " longKeyword:(NSRect)theRect\n"
6277 " evenLongerKeyword:(float)theInterval\n"
6278 " error:(NSError **)theError {\n"
6279 "}");
6280 }
6281
TEST_F(FormatTest,FormatObjCMethodExpr)6282 TEST_F(FormatTest, FormatObjCMethodExpr) {
6283 verifyFormat("[foo bar:baz];");
6284 verifyFormat("return [foo bar:baz];");
6285 verifyFormat("f([foo bar:baz]);");
6286 verifyFormat("f(2, [foo bar:baz]);");
6287 verifyFormat("f(2, a ? b : c);");
6288 verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
6289
6290 // Unary operators.
6291 verifyFormat("int a = +[foo bar:baz];");
6292 verifyFormat("int a = -[foo bar:baz];");
6293 verifyFormat("int a = ![foo bar:baz];");
6294 verifyFormat("int a = ~[foo bar:baz];");
6295 verifyFormat("int a = ++[foo bar:baz];");
6296 verifyFormat("int a = --[foo bar:baz];");
6297 verifyFormat("int a = sizeof [foo bar:baz];");
6298 verifyFormat("int a = alignof [foo bar:baz];", getGoogleStyle());
6299 verifyFormat("int a = &[foo bar:baz];");
6300 verifyFormat("int a = *[foo bar:baz];");
6301 // FIXME: Make casts work, without breaking f()[4].
6302 //verifyFormat("int a = (int)[foo bar:baz];");
6303 //verifyFormat("return (int)[foo bar:baz];");
6304 //verifyFormat("(void)[foo bar:baz];");
6305 verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];");
6306
6307 // Binary operators.
6308 verifyFormat("[foo bar:baz], [foo bar:baz];");
6309 verifyFormat("[foo bar:baz] = [foo bar:baz];");
6310 verifyFormat("[foo bar:baz] *= [foo bar:baz];");
6311 verifyFormat("[foo bar:baz] /= [foo bar:baz];");
6312 verifyFormat("[foo bar:baz] %= [foo bar:baz];");
6313 verifyFormat("[foo bar:baz] += [foo bar:baz];");
6314 verifyFormat("[foo bar:baz] -= [foo bar:baz];");
6315 verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
6316 verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
6317 verifyFormat("[foo bar:baz] &= [foo bar:baz];");
6318 verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
6319 verifyFormat("[foo bar:baz] |= [foo bar:baz];");
6320 verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
6321 verifyFormat("[foo bar:baz] || [foo bar:baz];");
6322 verifyFormat("[foo bar:baz] && [foo bar:baz];");
6323 verifyFormat("[foo bar:baz] | [foo bar:baz];");
6324 verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
6325 verifyFormat("[foo bar:baz] & [foo bar:baz];");
6326 verifyFormat("[foo bar:baz] == [foo bar:baz];");
6327 verifyFormat("[foo bar:baz] != [foo bar:baz];");
6328 verifyFormat("[foo bar:baz] >= [foo bar:baz];");
6329 verifyFormat("[foo bar:baz] <= [foo bar:baz];");
6330 verifyFormat("[foo bar:baz] > [foo bar:baz];");
6331 verifyFormat("[foo bar:baz] < [foo bar:baz];");
6332 verifyFormat("[foo bar:baz] >> [foo bar:baz];");
6333 verifyFormat("[foo bar:baz] << [foo bar:baz];");
6334 verifyFormat("[foo bar:baz] - [foo bar:baz];");
6335 verifyFormat("[foo bar:baz] + [foo bar:baz];");
6336 verifyFormat("[foo bar:baz] * [foo bar:baz];");
6337 verifyFormat("[foo bar:baz] / [foo bar:baz];");
6338 verifyFormat("[foo bar:baz] % [foo bar:baz];");
6339 // Whew!
6340
6341 verifyFormat("return in[42];");
6342 verifyFormat("for (id foo in [self getStuffFor:bla]) {\n"
6343 "}");
6344
6345 verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
6346 verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
6347 verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
6348 verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
6349 verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
6350 verifyFormat("[button setAction:@selector(zoomOut:)];");
6351 verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
6352
6353 verifyFormat("arr[[self indexForFoo:a]];");
6354 verifyFormat("throw [self errorFor:a];");
6355 verifyFormat("@throw [self errorFor:a];");
6356
6357 verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];");
6358 verifyFormat("[(id)foo bar:(id) ? baz : quux];");
6359 verifyFormat("4 > 4 ? (id)a : (id)baz;");
6360
6361 // This tests that the formatter doesn't break after "backing" but before ":",
6362 // which would be at 80 columns.
6363 verifyFormat(
6364 "void f() {\n"
6365 " if ((self = [super initWithContentRect:contentRect\n"
6366 " styleMask:styleMask ?: otherMask\n"
6367 " backing:NSBackingStoreBuffered\n"
6368 " defer:YES]))");
6369
6370 verifyFormat(
6371 "[foo checkThatBreakingAfterColonWorksOk:\n"
6372 " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
6373
6374 verifyFormat("[myObj short:arg1 // Force line break\n"
6375 " longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n"
6376 " evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n"
6377 " error:arg4];");
6378 verifyFormat(
6379 "void f() {\n"
6380 " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
6381 " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
6382 " pos.width(), pos.height())\n"
6383 " styleMask:NSBorderlessWindowMask\n"
6384 " backing:NSBackingStoreBuffered\n"
6385 " defer:NO]);\n"
6386 "}");
6387 verifyFormat(
6388 "void f() {\n"
6389 " popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n"
6390 " iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n"
6391 " pos.width(), pos.height())\n"
6392 " syeMask:NSBorderlessWindowMask\n"
6393 " bking:NSBackingStoreBuffered\n"
6394 " der:NO]);\n"
6395 "}",
6396 getLLVMStyleWithColumns(70));
6397 verifyFormat("{\n"
6398 " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
6399 " initWithContentRect:NSMakeRect(origin_global.x,\n"
6400 " origin_global.y,\n"
6401 " pos.width(),\n"
6402 " pos.height())\n"
6403 " styleMask:NSBorderlessWindowMask\n"
6404 " backing:NSBackingStoreBuffered\n"
6405 " defer:NO]);\n"
6406 "}",
6407 getChromiumStyle(FormatStyle::LK_Cpp));
6408 verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n"
6409 " with:contentsNativeView];");
6410
6411 verifyFormat(
6412 "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n"
6413 " owner:nillllll];");
6414
6415 verifyFormat(
6416 "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n"
6417 " forType:kBookmarkButtonDragType];");
6418
6419 verifyFormat("[defaultCenter addObserver:self\n"
6420 " selector:@selector(willEnterFullscreen)\n"
6421 " name:kWillEnterFullscreenNotification\n"
6422 " object:nil];");
6423 verifyFormat("[image_rep drawInRect:drawRect\n"
6424 " fromRect:NSZeroRect\n"
6425 " operation:NSCompositeCopy\n"
6426 " fraction:1.0\n"
6427 " respectFlipped:NO\n"
6428 " hints:nil];");
6429
6430 verifyFormat(
6431 "scoped_nsobject<NSTextField> message(\n"
6432 " // The frame will be fixed up when |-setMessageText:| is called.\n"
6433 " [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);");
6434 verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n"
6435 " aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n"
6436 " aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n"
6437 " aaaa:bbb];");
6438 verifyFormat("[self param:function( //\n"
6439 " parameter)]");
6440 verifyFormat(
6441 "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
6442 " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
6443 " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];");
6444
6445 // Variadic parameters.
6446 verifyFormat(
6447 "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];");
6448 verifyFormat(
6449 "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
6450 " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
6451 " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];");
6452 verifyFormat("[self // break\n"
6453 " a:a\n"
6454 " aaa:aaa];");
6455 verifyFormat("bool a = ([aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaa ||\n"
6456 " [aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaaaaa);");
6457 }
6458
TEST_F(FormatTest,ObjCAt)6459 TEST_F(FormatTest, ObjCAt) {
6460 verifyFormat("@autoreleasepool");
6461 verifyFormat("@catch");
6462 verifyFormat("@class");
6463 verifyFormat("@compatibility_alias");
6464 verifyFormat("@defs");
6465 verifyFormat("@dynamic");
6466 verifyFormat("@encode");
6467 verifyFormat("@end");
6468 verifyFormat("@finally");
6469 verifyFormat("@implementation");
6470 verifyFormat("@import");
6471 verifyFormat("@interface");
6472 verifyFormat("@optional");
6473 verifyFormat("@package");
6474 verifyFormat("@private");
6475 verifyFormat("@property");
6476 verifyFormat("@protected");
6477 verifyFormat("@protocol");
6478 verifyFormat("@public");
6479 verifyFormat("@required");
6480 verifyFormat("@selector");
6481 verifyFormat("@synchronized");
6482 verifyFormat("@synthesize");
6483 verifyFormat("@throw");
6484 verifyFormat("@try");
6485
6486 EXPECT_EQ("@interface", format("@ interface"));
6487
6488 // The precise formatting of this doesn't matter, nobody writes code like
6489 // this.
6490 verifyFormat("@ /*foo*/ interface");
6491 }
6492
TEST_F(FormatTest,ObjCSnippets)6493 TEST_F(FormatTest, ObjCSnippets) {
6494 verifyFormat("@autoreleasepool {\n"
6495 " foo();\n"
6496 "}");
6497 verifyFormat("@class Foo, Bar;");
6498 verifyFormat("@compatibility_alias AliasName ExistingClass;");
6499 verifyFormat("@dynamic textColor;");
6500 verifyFormat("char *buf1 = @encode(int *);");
6501 verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
6502 verifyFormat("char *buf1 = @encode(int **);");
6503 verifyFormat("Protocol *proto = @protocol(p1);");
6504 verifyFormat("SEL s = @selector(foo:);");
6505 verifyFormat("@synchronized(self) {\n"
6506 " f();\n"
6507 "}");
6508
6509 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
6510 verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
6511
6512 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
6513 verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
6514 verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
6515 verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
6516 getMozillaStyle());
6517 verifyFormat("@property BOOL editable;", getMozillaStyle());
6518 verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
6519 getWebKitStyle());
6520 verifyFormat("@property BOOL editable;", getWebKitStyle());
6521
6522 verifyFormat("@import foo.bar;\n"
6523 "@import baz;");
6524 }
6525
TEST_F(FormatTest,ObjCLiterals)6526 TEST_F(FormatTest, ObjCLiterals) {
6527 verifyFormat("@\"String\"");
6528 verifyFormat("@1");
6529 verifyFormat("@+4.8");
6530 verifyFormat("@-4");
6531 verifyFormat("@1LL");
6532 verifyFormat("@.5");
6533 verifyFormat("@'c'");
6534 verifyFormat("@true");
6535
6536 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
6537 verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);");
6538 verifyFormat("NSNumber *favoriteColor = @(Green);");
6539 verifyFormat("NSString *path = @(getenv(\"PATH\"));");
6540
6541 verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];");
6542 }
6543
TEST_F(FormatTest,ObjCDictLiterals)6544 TEST_F(FormatTest, ObjCDictLiterals) {
6545 verifyFormat("@{");
6546 verifyFormat("@{}");
6547 verifyFormat("@{@\"one\" : @1}");
6548 verifyFormat("return @{@\"one\" : @1;");
6549 verifyFormat("@{@\"one\" : @1}");
6550
6551 verifyFormat("@{@\"one\" : @{@2 : @1}}");
6552 verifyFormat("@{\n"
6553 " @\"one\" : @{@2 : @1},\n"
6554 "}");
6555
6556 verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}");
6557 verifyFormat("[self setDict:@{}");
6558 verifyFormat("[self setDict:@{@1 : @2}");
6559 verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);");
6560 verifyFormat(
6561 "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};");
6562 verifyFormat(
6563 "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};");
6564
6565 verifyFormat(
6566 "NSDictionary *d = @{\n"
6567 " @\"nam\" : NSUserNam(),\n"
6568 " @\"dte\" : [NSDate date],\n"
6569 " @\"processInfo\" : [NSProcessInfo processInfo]\n"
6570 "};");
6571 verifyFormat(
6572 "@{\n"
6573 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
6574 "regularFont,\n"
6575 "};");
6576 verifyGoogleFormat(
6577 "@{\n"
6578 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
6579 "regularFont,\n"
6580 "};");
6581 verifyFormat(
6582 "@{\n"
6583 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n"
6584 " reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n"
6585 "};");
6586
6587 // We should try to be robust in case someone forgets the "@".
6588 verifyFormat(
6589 "NSDictionary *d = {\n"
6590 " @\"nam\" : NSUserNam(),\n"
6591 " @\"dte\" : [NSDate date],\n"
6592 " @\"processInfo\" : [NSProcessInfo processInfo]\n"
6593 "};");
6594 verifyFormat("NSMutableDictionary *dictionary =\n"
6595 " [NSMutableDictionary dictionaryWithDictionary:@{\n"
6596 " aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n"
6597 " bbbbbbbbbbbbbbbbbb : bbbbb,\n"
6598 " cccccccccccccccc : ccccccccccccccc\n"
6599 " }];");
6600 }
6601
TEST_F(FormatTest,ObjCArrayLiterals)6602 TEST_F(FormatTest, ObjCArrayLiterals) {
6603 verifyFormat("@[");
6604 verifyFormat("@[]");
6605 verifyFormat(
6606 "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];");
6607 verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];");
6608 verifyFormat("NSArray *array = @[ [foo description] ];");
6609
6610 verifyFormat(
6611 "NSArray *some_variable = @[\n"
6612 " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
6613 " @\"aaaaaaaaaaaaaaaaa\",\n"
6614 " @\"aaaaaaaaaaaaaaaaa\",\n"
6615 " @\"aaaaaaaaaaaaaaaaa\"\n"
6616 "];");
6617 verifyFormat("NSArray *some_variable = @[\n"
6618 " @\"aaaaaaaaaaaaaaaaa\",\n"
6619 " @\"aaaaaaaaaaaaaaaaa\",\n"
6620 " @\"aaaaaaaaaaaaaaaaa\",\n"
6621 " @\"aaaaaaaaaaaaaaaaa\",\n"
6622 "];");
6623 verifyGoogleFormat("NSArray *some_variable = @[\n"
6624 " @\"aaaaaaaaaaaaaaaaa\",\n"
6625 " @\"aaaaaaaaaaaaaaaaa\",\n"
6626 " @\"aaaaaaaaaaaaaaaaa\",\n"
6627 " @\"aaaaaaaaaaaaaaaaa\"\n"
6628 "];");
6629
6630 // We should try to be robust in case someone forgets the "@".
6631 verifyFormat("NSArray *some_variable = [\n"
6632 " @\"aaaaaaaaaaaaaaaaa\",\n"
6633 " @\"aaaaaaaaaaaaaaaaa\",\n"
6634 " @\"aaaaaaaaaaaaaaaaa\",\n"
6635 " @\"aaaaaaaaaaaaaaaaa\",\n"
6636 "];");
6637 verifyFormat(
6638 "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n"
6639 " index:(NSUInteger)index\n"
6640 " nonDigitAttributes:\n"
6641 " (NSDictionary *)noDigitAttributes;");
6642 verifyFormat(
6643 "[someFunction someLooooooooooooongParameter:\n"
6644 " @[ NSBundle.mainBundle.infoDictionary[@\"a\"] ]];");
6645 }
6646
TEST_F(FormatTest,ReformatRegionAdjustsIndent)6647 TEST_F(FormatTest, ReformatRegionAdjustsIndent) {
6648 EXPECT_EQ("{\n"
6649 "{\n"
6650 "a;\n"
6651 "b;\n"
6652 "}\n"
6653 "}",
6654 format("{\n"
6655 "{\n"
6656 "a;\n"
6657 " b;\n"
6658 "}\n"
6659 "}",
6660 13, 2, getLLVMStyle()));
6661 EXPECT_EQ("{\n"
6662 "{\n"
6663 " a;\n"
6664 "b;\n"
6665 "}\n"
6666 "}",
6667 format("{\n"
6668 "{\n"
6669 " a;\n"
6670 "b;\n"
6671 "}\n"
6672 "}",
6673 9, 2, getLLVMStyle()));
6674 EXPECT_EQ("{\n"
6675 "{\n"
6676 "public:\n"
6677 " b;\n"
6678 "}\n"
6679 "}",
6680 format("{\n"
6681 "{\n"
6682 "public:\n"
6683 " b;\n"
6684 "}\n"
6685 "}",
6686 17, 2, getLLVMStyle()));
6687 EXPECT_EQ("{\n"
6688 "{\n"
6689 "a;\n"
6690 "}\n"
6691 "{\n"
6692 " b; //\n"
6693 "}\n"
6694 "}",
6695 format("{\n"
6696 "{\n"
6697 "a;\n"
6698 "}\n"
6699 "{\n"
6700 " b; //\n"
6701 "}\n"
6702 "}",
6703 22, 2, getLLVMStyle()));
6704 EXPECT_EQ(" {\n"
6705 " a; //\n"
6706 " }",
6707 format(" {\n"
6708 "a; //\n"
6709 " }",
6710 4, 2, getLLVMStyle()));
6711 EXPECT_EQ("void f() {}\n"
6712 "void g() {}",
6713 format("void f() {}\n"
6714 "void g() {}",
6715 13, 0, getLLVMStyle()));
6716 EXPECT_EQ("int a; // comment\n"
6717 " // line 2\n"
6718 "int b;",
6719 format("int a; // comment\n"
6720 " // line 2\n"
6721 " int b;",
6722 35, 0, getLLVMStyle()));
6723 EXPECT_EQ(" int a;\n"
6724 " void\n"
6725 " ffffff() {\n"
6726 " }",
6727 format(" int a;\n"
6728 "void ffffff() {}",
6729 11, 0, getLLVMStyleWithColumns(11)));
6730
6731 EXPECT_EQ(" void f() {\n"
6732 "#define A 1\n"
6733 " }",
6734 format(" void f() {\n"
6735 " #define A 1\n" // Format this line.
6736 " }",
6737 20, 0, getLLVMStyle()));
6738 EXPECT_EQ(" void f() {\n"
6739 " int i;\n"
6740 "#define A \\\n"
6741 " int i; \\\n"
6742 " int j;\n"
6743 " int k;\n"
6744 " }",
6745 format(" void f() {\n"
6746 " int i;\n"
6747 "#define A \\\n"
6748 " int i; \\\n"
6749 " int j;\n"
6750 " int k;\n" // Format this line.
6751 " }",
6752 67, 0, getLLVMStyle()));
6753 }
6754
TEST_F(FormatTest,BreaksStringLiterals)6755 TEST_F(FormatTest, BreaksStringLiterals) {
6756 EXPECT_EQ("\"some text \"\n"
6757 "\"other\";",
6758 format("\"some text other\";", getLLVMStyleWithColumns(12)));
6759 EXPECT_EQ("\"some text \"\n"
6760 "\"other\";",
6761 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
6762 EXPECT_EQ(
6763 "#define A \\\n"
6764 " \"some \" \\\n"
6765 " \"text \" \\\n"
6766 " \"other\";",
6767 format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
6768 EXPECT_EQ(
6769 "#define A \\\n"
6770 " \"so \" \\\n"
6771 " \"text \" \\\n"
6772 " \"other\";",
6773 format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
6774
6775 EXPECT_EQ("\"some text\"",
6776 format("\"some text\"", getLLVMStyleWithColumns(1)));
6777 EXPECT_EQ("\"some text\"",
6778 format("\"some text\"", getLLVMStyleWithColumns(11)));
6779 EXPECT_EQ("\"some \"\n"
6780 "\"text\"",
6781 format("\"some text\"", getLLVMStyleWithColumns(10)));
6782 EXPECT_EQ("\"some \"\n"
6783 "\"text\"",
6784 format("\"some text\"", getLLVMStyleWithColumns(7)));
6785 EXPECT_EQ("\"some\"\n"
6786 "\" tex\"\n"
6787 "\"t\"",
6788 format("\"some text\"", getLLVMStyleWithColumns(6)));
6789 EXPECT_EQ("\"some\"\n"
6790 "\" tex\"\n"
6791 "\" and\"",
6792 format("\"some tex and\"", getLLVMStyleWithColumns(6)));
6793 EXPECT_EQ("\"some\"\n"
6794 "\"/tex\"\n"
6795 "\"/and\"",
6796 format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
6797
6798 EXPECT_EQ("variable =\n"
6799 " \"long string \"\n"
6800 " \"literal\";",
6801 format("variable = \"long string literal\";",
6802 getLLVMStyleWithColumns(20)));
6803
6804 EXPECT_EQ("variable = f(\n"
6805 " \"long string \"\n"
6806 " \"literal\",\n"
6807 " short,\n"
6808 " loooooooooooooooooooong);",
6809 format("variable = f(\"long string literal\", short, "
6810 "loooooooooooooooooooong);",
6811 getLLVMStyleWithColumns(20)));
6812
6813 EXPECT_EQ("f(g(\"long string \"\n"
6814 " \"literal\"),\n"
6815 " b);",
6816 format("f(g(\"long string literal\"), b);",
6817 getLLVMStyleWithColumns(20)));
6818 EXPECT_EQ("f(g(\"long string \"\n"
6819 " \"literal\",\n"
6820 " a),\n"
6821 " b);",
6822 format("f(g(\"long string literal\", a), b);",
6823 getLLVMStyleWithColumns(20)));
6824 EXPECT_EQ(
6825 "f(\"one two\".split(\n"
6826 " variable));",
6827 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
6828 EXPECT_EQ("f(\"one two three four five six \"\n"
6829 " \"seven\".split(\n"
6830 " really_looooong_variable));",
6831 format("f(\"one two three four five six seven\"."
6832 "split(really_looooong_variable));",
6833 getLLVMStyleWithColumns(33)));
6834
6835 EXPECT_EQ("f(\"some \"\n"
6836 " \"text\",\n"
6837 " other);",
6838 format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
6839
6840 // Only break as a last resort.
6841 verifyFormat(
6842 "aaaaaaaaaaaaaaaaaaaa(\n"
6843 " aaaaaaaaaaaaaaaaaaaa,\n"
6844 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
6845
6846 EXPECT_EQ(
6847 "\"splitmea\"\n"
6848 "\"trandomp\"\n"
6849 "\"oint\"",
6850 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
6851
6852 EXPECT_EQ(
6853 "\"split/\"\n"
6854 "\"pathat/\"\n"
6855 "\"slashes\"",
6856 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
6857
6858 EXPECT_EQ(
6859 "\"split/\"\n"
6860 "\"pathat/\"\n"
6861 "\"slashes\"",
6862 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
6863 EXPECT_EQ("\"split at \"\n"
6864 "\"spaces/at/\"\n"
6865 "\"slashes.at.any$\"\n"
6866 "\"non-alphanumeric%\"\n"
6867 "\"1111111111characte\"\n"
6868 "\"rs\"",
6869 format("\"split at "
6870 "spaces/at/"
6871 "slashes.at."
6872 "any$non-"
6873 "alphanumeric%"
6874 "1111111111characte"
6875 "rs\"",
6876 getLLVMStyleWithColumns(20)));
6877
6878 // Verify that splitting the strings understands
6879 // Style::AlwaysBreakBeforeMultilineStrings.
6880 EXPECT_EQ("aaaaaaaaaaaa(aaaaaaaaaaaaa,\n"
6881 " \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
6882 " \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
6883 format("aaaaaaaaaaaa(aaaaaaaaaaaaa, \"aaaaaaaaaaaaaaaaaaaaaa "
6884 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa "
6885 "aaaaaaaaaaaaaaaaaaaaaa\");",
6886 getGoogleStyle()));
6887 EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
6888 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
6889 format("return \"aaaaaaaaaaaaaaaaaaaaaa "
6890 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
6891 "aaaaaaaaaaaaaaaaaaaaaa\";",
6892 getGoogleStyle()));
6893 EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
6894 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
6895 format("llvm::outs() << "
6896 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
6897 "aaaaaaaaaaaaaaaaaaa\";"));
6898 EXPECT_EQ("ffff(\n"
6899 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
6900 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
6901 format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
6902 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
6903 getGoogleStyle()));
6904
6905 FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
6906 AlignLeft.AlignEscapedNewlinesLeft = true;
6907 EXPECT_EQ(
6908 "#define A \\\n"
6909 " \"some \" \\\n"
6910 " \"text \" \\\n"
6911 " \"other\";",
6912 format("#define A \"some text other\";", AlignLeft));
6913 }
6914
TEST_F(FormatTest,BreaksStringLiteralsWithTabs)6915 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
6916 EXPECT_EQ(
6917 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
6918 "(\n"
6919 " \"x\t\");",
6920 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
6921 "aaaaaaa("
6922 "\"x\t\");"));
6923 }
6924
TEST_F(FormatTest,BreaksWideAndNSStringLiterals)6925 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
6926 EXPECT_EQ(
6927 "u8\"utf8 string \"\n"
6928 "u8\"literal\";",
6929 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
6930 EXPECT_EQ(
6931 "u\"utf16 string \"\n"
6932 "u\"literal\";",
6933 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
6934 EXPECT_EQ(
6935 "U\"utf32 string \"\n"
6936 "U\"literal\";",
6937 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
6938 EXPECT_EQ("L\"wide string \"\n"
6939 "L\"literal\";",
6940 format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
6941 EXPECT_EQ("@\"NSString \"\n"
6942 "@\"literal\";",
6943 format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
6944 }
6945
TEST_F(FormatTest,BreaksRawStringLiterals)6946 TEST_F(FormatTest, BreaksRawStringLiterals) {
6947 EXPECT_EQ("R\"x(raw )x\"\n"
6948 "R\"x(literal)x\";",
6949 format("R\"x(raw literal)x\";", getGoogleStyleWithColumns(15)));
6950 EXPECT_EQ("uR\"x(raw )x\"\n"
6951 "uR\"x(literal)x\";",
6952 format("uR\"x(raw literal)x\";", getGoogleStyleWithColumns(16)));
6953 EXPECT_EQ("u8R\"x(raw )x\"\n"
6954 "u8R\"x(literal)x\";",
6955 format("u8R\"x(raw literal)x\";", getGoogleStyleWithColumns(17)));
6956 EXPECT_EQ("LR\"x(raw )x\"\n"
6957 "LR\"x(literal)x\";",
6958 format("LR\"x(raw literal)x\";", getGoogleStyleWithColumns(16)));
6959 EXPECT_EQ("UR\"x(raw )x\"\n"
6960 "UR\"x(literal)x\";",
6961 format("UR\"x(raw literal)x\";", getGoogleStyleWithColumns(16)));
6962 }
6963
TEST_F(FormatTest,BreaksStringLiteralsWithin_TMacro)6964 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
6965 FormatStyle Style = getLLVMStyleWithColumns(20);
6966 EXPECT_EQ(
6967 "_T(\"aaaaaaaaaaaaaa\")\n"
6968 "_T(\"aaaaaaaaaaaaaa\")\n"
6969 "_T(\"aaaaaaaaaaaa\")",
6970 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
6971 EXPECT_EQ("f(x, _T(\"aaaaaaaaa\")\n"
6972 " _T(\"aaaaaa\"),\n"
6973 " z);",
6974 format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
6975
6976 // FIXME: Handle embedded spaces in one iteration.
6977 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
6978 // "_T(\"aaaaaaaaaaaaa\")\n"
6979 // "_T(\"aaaaaaaaaaaaa\")\n"
6980 // "_T(\"a\")",
6981 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
6982 // getLLVMStyleWithColumns(20)));
6983 EXPECT_EQ(
6984 "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
6985 format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
6986 }
6987
TEST_F(FormatTest,DontSplitStringLiteralsWithEscapedNewlines)6988 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
6989 EXPECT_EQ(
6990 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
6991 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
6992 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
6993 format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
6994 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
6995 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
6996 }
6997
TEST_F(FormatTest,CountsCharactersInMultilineRawStringLiterals)6998 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
6999 EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
7000 format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle()));
7001 EXPECT_EQ("fffffffffff(g(R\"x(\n"
7002 "multiline raw string literal xxxxxxxxxxxxxx\n"
7003 ")x\",\n"
7004 " a),\n"
7005 " b);",
7006 format("fffffffffff(g(R\"x(\n"
7007 "multiline raw string literal xxxxxxxxxxxxxx\n"
7008 ")x\", a), b);",
7009 getGoogleStyleWithColumns(20)));
7010 EXPECT_EQ("fffffffffff(\n"
7011 " g(R\"x(qqq\n"
7012 "multiline raw string literal xxxxxxxxxxxxxx\n"
7013 ")x\",\n"
7014 " a),\n"
7015 " b);",
7016 format("fffffffffff(g(R\"x(qqq\n"
7017 "multiline raw string literal xxxxxxxxxxxxxx\n"
7018 ")x\", a), b);",
7019 getGoogleStyleWithColumns(20)));
7020
7021 EXPECT_EQ("fffffffffff(R\"x(\n"
7022 "multiline raw string literal xxxxxxxxxxxxxx\n"
7023 ")x\");",
7024 format("fffffffffff(R\"x(\n"
7025 "multiline raw string literal xxxxxxxxxxxxxx\n"
7026 ")x\");",
7027 getGoogleStyleWithColumns(20)));
7028 EXPECT_EQ("fffffffffff(R\"x(\n"
7029 "multiline raw string literal xxxxxxxxxxxxxx\n"
7030 ")x\" + bbbbbb);",
7031 format("fffffffffff(R\"x(\n"
7032 "multiline raw string literal xxxxxxxxxxxxxx\n"
7033 ")x\" + bbbbbb);",
7034 getGoogleStyleWithColumns(20)));
7035 EXPECT_EQ("fffffffffff(\n"
7036 " R\"x(\n"
7037 "multiline raw string literal xxxxxxxxxxxxxx\n"
7038 ")x\" +\n"
7039 " bbbbbb);",
7040 format("fffffffffff(\n"
7041 " R\"x(\n"
7042 "multiline raw string literal xxxxxxxxxxxxxx\n"
7043 ")x\" + bbbbbb);",
7044 getGoogleStyleWithColumns(20)));
7045 }
7046
TEST_F(FormatTest,SkipsUnknownStringLiterals)7047 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
7048 verifyFormat("string a = \"unterminated;");
7049 EXPECT_EQ("function(\"unterminated,\n"
7050 " OtherParameter);",
7051 format("function( \"unterminated,\n"
7052 " OtherParameter);"));
7053 }
7054
TEST_F(FormatTest,DoesNotTryToParseUDLiteralsInPreCpp11Code)7055 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
7056 FormatStyle Style = getLLVMStyle();
7057 Style.Standard = FormatStyle::LS_Cpp03;
7058 EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
7059 format("#define x(_a) printf(\"foo\"_a);", Style));
7060 }
7061
TEST_F(FormatTest,UnderstandsCpp1y)7062 TEST_F(FormatTest, UnderstandsCpp1y) {
7063 verifyFormat("int bi{1'000'000};");
7064 }
7065
TEST_F(FormatTest,BreakStringLiteralsBeforeUnbreakableTokenSequence)7066 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
7067 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
7068 " \"ddeeefff\");",
7069 format("someFunction(\"aaabbbcccdddeeefff\");",
7070 getLLVMStyleWithColumns(25)));
7071 EXPECT_EQ("someFunction1234567890(\n"
7072 " \"aaabbbcccdddeeefff\");",
7073 format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
7074 getLLVMStyleWithColumns(26)));
7075 EXPECT_EQ("someFunction1234567890(\n"
7076 " \"aaabbbcccdddeeeff\"\n"
7077 " \"f\");",
7078 format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
7079 getLLVMStyleWithColumns(25)));
7080 EXPECT_EQ("someFunction1234567890(\n"
7081 " \"aaabbbcccdddeeeff\"\n"
7082 " \"f\");",
7083 format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
7084 getLLVMStyleWithColumns(24)));
7085 EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
7086 " \"ddde \"\n"
7087 " \"efff\");",
7088 format("someFunction(\"aaabbbcc ddde efff\");",
7089 getLLVMStyleWithColumns(25)));
7090 EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
7091 " \"ddeeefff\");",
7092 format("someFunction(\"aaabbbccc ddeeefff\");",
7093 getLLVMStyleWithColumns(25)));
7094 EXPECT_EQ("someFunction1234567890(\n"
7095 " \"aaabb \"\n"
7096 " \"cccdddeeefff\");",
7097 format("someFunction1234567890(\"aaabb cccdddeeefff\");",
7098 getLLVMStyleWithColumns(25)));
7099 EXPECT_EQ("#define A \\\n"
7100 " string s = \\\n"
7101 " \"123456789\" \\\n"
7102 " \"0\"; \\\n"
7103 " int i;",
7104 format("#define A string s = \"1234567890\"; int i;",
7105 getLLVMStyleWithColumns(20)));
7106 // FIXME: Put additional penalties on breaking at non-whitespace locations.
7107 EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
7108 " \"dddeeeff\"\n"
7109 " \"f\");",
7110 format("someFunction(\"aaabbbcc dddeeefff\");",
7111 getLLVMStyleWithColumns(25)));
7112 }
7113
TEST_F(FormatTest,DoNotBreakStringLiteralsInEscapeSequence)7114 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
7115 EXPECT_EQ("\"\\a\"",
7116 format("\"\\a\"", getLLVMStyleWithColumns(3)));
7117 EXPECT_EQ("\"\\\"",
7118 format("\"\\\"", getLLVMStyleWithColumns(2)));
7119 EXPECT_EQ("\"test\"\n"
7120 "\"\\n\"",
7121 format("\"test\\n\"", getLLVMStyleWithColumns(7)));
7122 EXPECT_EQ("\"tes\\\\\"\n"
7123 "\"n\"",
7124 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
7125 EXPECT_EQ("\"\\\\\\\\\"\n"
7126 "\"\\n\"",
7127 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
7128 EXPECT_EQ("\"\\uff01\"",
7129 format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
7130 EXPECT_EQ("\"\\uff01\"\n"
7131 "\"test\"",
7132 format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
7133 EXPECT_EQ("\"\\Uff01ff02\"",
7134 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
7135 EXPECT_EQ("\"\\x000000000001\"\n"
7136 "\"next\"",
7137 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
7138 EXPECT_EQ("\"\\x000000000001next\"",
7139 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
7140 EXPECT_EQ("\"\\x000000000001\"",
7141 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
7142 EXPECT_EQ("\"test\"\n"
7143 "\"\\000000\"\n"
7144 "\"000001\"",
7145 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
7146 EXPECT_EQ("\"test\\000\"\n"
7147 "\"00000000\"\n"
7148 "\"1\"",
7149 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
7150 // FIXME: We probably don't need to care about escape sequences in raw
7151 // literals.
7152 EXPECT_EQ("R\"(\\x)\"\n"
7153 "R\"(\\x00)\"\n",
7154 format("R\"(\\x\\x00)\"\n", getGoogleStyleWithColumns(7)));
7155 }
7156
TEST_F(FormatTest,DoNotCreateUnreasonableUnwrappedLines)7157 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
7158 verifyFormat("void f() {\n"
7159 " return g() {}\n"
7160 " void h() {}");
7161 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
7162 "g();\n"
7163 "}");
7164 }
7165
TEST_F(FormatTest,DoNotPrematurelyEndUnwrappedLineForReturnStatements)7166 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
7167 verifyFormat(
7168 "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
7169 }
7170
TEST_F(FormatTest,FormatsClosingBracesInEmptyNestedBlocks)7171 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
7172 verifyFormat("class X {\n"
7173 " void f() {\n"
7174 " }\n"
7175 "};",
7176 getLLVMStyleWithColumns(12));
7177 }
7178
TEST_F(FormatTest,ConfigurableIndentWidth)7179 TEST_F(FormatTest, ConfigurableIndentWidth) {
7180 FormatStyle EightIndent = getLLVMStyleWithColumns(18);
7181 EightIndent.IndentWidth = 8;
7182 EightIndent.ContinuationIndentWidth = 8;
7183 verifyFormat("void f() {\n"
7184 " someFunction();\n"
7185 " if (true) {\n"
7186 " f();\n"
7187 " }\n"
7188 "}",
7189 EightIndent);
7190 verifyFormat("class X {\n"
7191 " void f() {\n"
7192 " }\n"
7193 "};",
7194 EightIndent);
7195 verifyFormat("int x[] = {\n"
7196 " call(),\n"
7197 " call()};",
7198 EightIndent);
7199 }
7200
TEST_F(FormatTest,ConfigurableFunctionDeclarationIndentAfterType)7201 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
7202 verifyFormat("void\n"
7203 "f();",
7204 getLLVMStyleWithColumns(8));
7205 }
7206
TEST_F(FormatTest,ConfigurableUseOfTab)7207 TEST_F(FormatTest, ConfigurableUseOfTab) {
7208 FormatStyle Tab = getLLVMStyleWithColumns(42);
7209 Tab.IndentWidth = 8;
7210 Tab.UseTab = FormatStyle::UT_Always;
7211 Tab.AlignEscapedNewlinesLeft = true;
7212
7213 EXPECT_EQ("if (aaaaaaaa && // q\n"
7214 " bb)\t\t// w\n"
7215 "\t;",
7216 format("if (aaaaaaaa &&// q\n"
7217 "bb)// w\n"
7218 ";",
7219 Tab));
7220 EXPECT_EQ("if (aaa && bbb) // w\n"
7221 "\t;",
7222 format("if(aaa&&bbb)// w\n"
7223 ";",
7224 Tab));
7225
7226 verifyFormat("class X {\n"
7227 "\tvoid f() {\n"
7228 "\t\tsomeFunction(parameter1,\n"
7229 "\t\t\t parameter2);\n"
7230 "\t}\n"
7231 "};",
7232 Tab);
7233 verifyFormat("#define A \\\n"
7234 "\tvoid f() { \\\n"
7235 "\t\tsomeFunction( \\\n"
7236 "\t\t parameter1, \\\n"
7237 "\t\t parameter2); \\\n"
7238 "\t}",
7239 Tab);
7240 EXPECT_EQ("void f() {\n"
7241 "\tf();\n"
7242 "\tg();\n"
7243 "}",
7244 format("void f() {\n"
7245 "\tf();\n"
7246 "\tg();\n"
7247 "}",
7248 0, 0, Tab));
7249 EXPECT_EQ("void f() {\n"
7250 "\tf();\n"
7251 "\tg();\n"
7252 "}",
7253 format("void f() {\n"
7254 "\tf();\n"
7255 "\tg();\n"
7256 "}",
7257 16, 0, Tab));
7258 EXPECT_EQ("void f() {\n"
7259 " \tf();\n"
7260 "\tg();\n"
7261 "}",
7262 format("void f() {\n"
7263 " \tf();\n"
7264 " \tg();\n"
7265 "}",
7266 21, 0, Tab));
7267
7268 Tab.TabWidth = 4;
7269 Tab.IndentWidth = 8;
7270 verifyFormat("class TabWidth4Indent8 {\n"
7271 "\t\tvoid f() {\n"
7272 "\t\t\t\tsomeFunction(parameter1,\n"
7273 "\t\t\t\t\t\t\t parameter2);\n"
7274 "\t\t}\n"
7275 "};",
7276 Tab);
7277
7278 Tab.TabWidth = 4;
7279 Tab.IndentWidth = 4;
7280 verifyFormat("class TabWidth4Indent4 {\n"
7281 "\tvoid f() {\n"
7282 "\t\tsomeFunction(parameter1,\n"
7283 "\t\t\t\t\t parameter2);\n"
7284 "\t}\n"
7285 "};",
7286 Tab);
7287
7288 Tab.TabWidth = 8;
7289 Tab.IndentWidth = 4;
7290 verifyFormat("class TabWidth8Indent4 {\n"
7291 " void f() {\n"
7292 "\tsomeFunction(parameter1,\n"
7293 "\t\t parameter2);\n"
7294 " }\n"
7295 "};",
7296 Tab);
7297
7298 Tab.TabWidth = 8;
7299 Tab.IndentWidth = 8;
7300 EXPECT_EQ("/*\n"
7301 "\t a\t\tcomment\n"
7302 "\t in multiple lines\n"
7303 " */",
7304 format(" /*\t \t \n"
7305 " \t \t a\t\tcomment\t \t\n"
7306 " \t \t in multiple lines\t\n"
7307 " \t */",
7308 Tab));
7309
7310 Tab.UseTab = FormatStyle::UT_ForIndentation;
7311 verifyFormat("{\n"
7312 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7313 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7314 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7315 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7316 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7317 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7318 "};",
7319 Tab);
7320 verifyFormat("enum A {\n"
7321 "\ta1, // Force multiple lines\n"
7322 "\ta2,\n"
7323 "\ta3\n"
7324 "};",
7325 Tab);
7326 EXPECT_EQ("if (aaaaaaaa && // q\n"
7327 " bb) // w\n"
7328 "\t;",
7329 format("if (aaaaaaaa &&// q\n"
7330 "bb)// w\n"
7331 ";",
7332 Tab));
7333 verifyFormat("class X {\n"
7334 "\tvoid f() {\n"
7335 "\t\tsomeFunction(parameter1,\n"
7336 "\t\t parameter2);\n"
7337 "\t}\n"
7338 "};",
7339 Tab);
7340 verifyFormat("{\n"
7341 "\tQ({\n"
7342 "\t\t int a;\n"
7343 "\t\t someFunction(aaaaaaaaaa,\n"
7344 "\t\t bbbbbbbbb);\n"
7345 "\t },\n"
7346 "\t p);\n"
7347 "}",
7348 Tab);
7349 EXPECT_EQ("{\n"
7350 "\t/* aaaa\n"
7351 "\t bbbb */\n"
7352 "}",
7353 format("{\n"
7354 "/* aaaa\n"
7355 " bbbb */\n"
7356 "}",
7357 Tab));
7358 EXPECT_EQ("{\n"
7359 "\t/*\n"
7360 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7361 "\t bbbbbbbbbbbbb\n"
7362 "\t*/\n"
7363 "}",
7364 format("{\n"
7365 "/*\n"
7366 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
7367 "*/\n"
7368 "}",
7369 Tab));
7370 EXPECT_EQ("{\n"
7371 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7372 "\t// bbbbbbbbbbbbb\n"
7373 "}",
7374 format("{\n"
7375 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
7376 "}",
7377 Tab));
7378 EXPECT_EQ("{\n"
7379 "\t/*\n"
7380 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7381 "\t bbbbbbbbbbbbb\n"
7382 "\t*/\n"
7383 "}",
7384 format("{\n"
7385 "\t/*\n"
7386 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
7387 "\t*/\n"
7388 "}",
7389 Tab));
7390 EXPECT_EQ("{\n"
7391 "\t/*\n"
7392 "\n"
7393 "\t*/\n"
7394 "}",
7395 format("{\n"
7396 "\t/*\n"
7397 "\n"
7398 "\t*/\n"
7399 "}",
7400 Tab));
7401 EXPECT_EQ("{\n"
7402 "\t/*\n"
7403 " asdf\n"
7404 "\t*/\n"
7405 "}",
7406 format("{\n"
7407 "\t/*\n"
7408 " asdf\n"
7409 "\t*/\n"
7410 "}",
7411 Tab));
7412
7413 Tab.UseTab = FormatStyle::UT_Never;
7414 EXPECT_EQ("/*\n"
7415 " a\t\tcomment\n"
7416 " in multiple lines\n"
7417 " */",
7418 format(" /*\t \t \n"
7419 " \t \t a\t\tcomment\t \t\n"
7420 " \t \t in multiple lines\t\n"
7421 " \t */",
7422 Tab));
7423 EXPECT_EQ("/* some\n"
7424 " comment */",
7425 format(" \t \t /* some\n"
7426 " \t \t comment */",
7427 Tab));
7428 EXPECT_EQ("int a; /* some\n"
7429 " comment */",
7430 format(" \t \t int a; /* some\n"
7431 " \t \t comment */",
7432 Tab));
7433
7434 EXPECT_EQ("int a; /* some\n"
7435 "comment */",
7436 format(" \t \t int\ta; /* some\n"
7437 " \t \t comment */",
7438 Tab));
7439 EXPECT_EQ("f(\"\t\t\"); /* some\n"
7440 " comment */",
7441 format(" \t \t f(\"\t\t\"); /* some\n"
7442 " \t \t comment */",
7443 Tab));
7444 EXPECT_EQ("{\n"
7445 " /*\n"
7446 " * Comment\n"
7447 " */\n"
7448 " int i;\n"
7449 "}",
7450 format("{\n"
7451 "\t/*\n"
7452 "\t * Comment\n"
7453 "\t */\n"
7454 "\t int i;\n"
7455 "}"));
7456 }
7457
TEST_F(FormatTest,CalculatesOriginalColumn)7458 TEST_F(FormatTest, CalculatesOriginalColumn) {
7459 EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
7460 "q\"; /* some\n"
7461 " comment */",
7462 format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
7463 "q\"; /* some\n"
7464 " comment */",
7465 getLLVMStyle()));
7466 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
7467 "/* some\n"
7468 " comment */",
7469 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
7470 " /* some\n"
7471 " comment */",
7472 getLLVMStyle()));
7473 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
7474 "qqq\n"
7475 "/* some\n"
7476 " comment */",
7477 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
7478 "qqq\n"
7479 " /* some\n"
7480 " comment */",
7481 getLLVMStyle()));
7482 EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
7483 "wwww; /* some\n"
7484 " comment */",
7485 format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
7486 "wwww; /* some\n"
7487 " comment */",
7488 getLLVMStyle()));
7489 }
7490
TEST_F(FormatTest,ConfigurableSpaceBeforeParens)7491 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
7492 FormatStyle NoSpace = getLLVMStyle();
7493 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
7494
7495 verifyFormat("while(true)\n"
7496 " continue;", NoSpace);
7497 verifyFormat("for(;;)\n"
7498 " continue;", NoSpace);
7499 verifyFormat("if(true)\n"
7500 " f();\n"
7501 "else if(true)\n"
7502 " f();", NoSpace);
7503 verifyFormat("do {\n"
7504 " do_something();\n"
7505 "} while(something());", NoSpace);
7506 verifyFormat("switch(x) {\n"
7507 "default:\n"
7508 " break;\n"
7509 "}", NoSpace);
7510
7511 FormatStyle Space = getLLVMStyle();
7512 Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
7513
7514 verifyFormat("int f ();", Space);
7515 verifyFormat("void f (int a, T b) {\n"
7516 " while (true)\n"
7517 " continue;\n"
7518 "}",
7519 Space);
7520 verifyFormat("if (true)\n"
7521 " f ();\n"
7522 "else if (true)\n"
7523 " f ();",
7524 Space);
7525 verifyFormat("do {\n"
7526 " do_something ();\n"
7527 "} while (something ());",
7528 Space);
7529 verifyFormat("switch (x) {\n"
7530 "default:\n"
7531 " break;\n"
7532 "}",
7533 Space);
7534 verifyFormat("A::A () : a (1) {}", Space);
7535 verifyFormat("void f () __attribute__ ((asdf));", Space);
7536 verifyFormat("*(&a + 1);\n"
7537 "&((&a)[1]);\n"
7538 "a[(b + c) * d];\n"
7539 "(((a + 1) * 2) + 3) * 4;",
7540 Space);
7541 verifyFormat("#define A(x) x", Space);
7542 verifyFormat("#define A (x) x", Space);
7543 verifyFormat("#if defined(x)\n"
7544 "#endif",
7545 Space);
7546 }
7547
TEST_F(FormatTest,ConfigurableSpacesInParentheses)7548 TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
7549 FormatStyle Spaces = getLLVMStyle();
7550
7551 Spaces.SpacesInParentheses = true;
7552 verifyFormat("call( x, y, z );", Spaces);
7553 verifyFormat("while ( (bool)1 )\n"
7554 " continue;", Spaces);
7555 verifyFormat("for ( ;; )\n"
7556 " continue;", Spaces);
7557 verifyFormat("if ( true )\n"
7558 " f();\n"
7559 "else if ( true )\n"
7560 " f();", Spaces);
7561 verifyFormat("do {\n"
7562 " do_something( (int)i );\n"
7563 "} while ( something() );", Spaces);
7564 verifyFormat("switch ( x ) {\n"
7565 "default:\n"
7566 " break;\n"
7567 "}", Spaces);
7568
7569 Spaces.SpacesInParentheses = false;
7570 Spaces.SpacesInCStyleCastParentheses = true;
7571 verifyFormat("Type *A = ( Type * )P;", Spaces);
7572 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
7573 verifyFormat("x = ( int32 )y;", Spaces);
7574 verifyFormat("int a = ( int )(2.0f);", Spaces);
7575 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
7576 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
7577 verifyFormat("#define x (( int )-1)", Spaces);
7578
7579 Spaces.SpacesInParentheses = false;
7580 Spaces.SpaceInEmptyParentheses = true;
7581 verifyFormat("call(x, y, z);", Spaces);
7582 verifyFormat("call( )", Spaces);
7583
7584 // Run the first set of tests again with
7585 // Spaces.SpacesInParentheses = false,
7586 // Spaces.SpaceInEmptyParentheses = true and
7587 // Spaces.SpacesInCStyleCastParentheses = true
7588 Spaces.SpacesInParentheses = false,
7589 Spaces.SpaceInEmptyParentheses = true;
7590 Spaces.SpacesInCStyleCastParentheses = true;
7591 verifyFormat("call(x, y, z);", Spaces);
7592 verifyFormat("while (( bool )1)\n"
7593 " continue;", Spaces);
7594 verifyFormat("for (;;)\n"
7595 " continue;", Spaces);
7596 verifyFormat("if (true)\n"
7597 " f( );\n"
7598 "else if (true)\n"
7599 " f( );", Spaces);
7600 verifyFormat("do {\n"
7601 " do_something(( int )i);\n"
7602 "} while (something( ));", Spaces);
7603 verifyFormat("switch (x) {\n"
7604 "default:\n"
7605 " break;\n"
7606 "}", Spaces);
7607 }
7608
TEST_F(FormatTest,ConfigurableSpaceBeforeAssignmentOperators)7609 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
7610 verifyFormat("int a = 5;");
7611 verifyFormat("a += 42;");
7612 verifyFormat("a or_eq 8;");
7613
7614 FormatStyle Spaces = getLLVMStyle();
7615 Spaces.SpaceBeforeAssignmentOperators = false;
7616 verifyFormat("int a= 5;", Spaces);
7617 verifyFormat("a+= 42;", Spaces);
7618 verifyFormat("a or_eq 8;", Spaces);
7619 }
7620
TEST_F(FormatTest,LinuxBraceBreaking)7621 TEST_F(FormatTest, LinuxBraceBreaking) {
7622 FormatStyle BreakBeforeBrace = getLLVMStyle();
7623 BreakBeforeBrace.BreakBeforeBraces = FormatStyle::BS_Linux;
7624 verifyFormat("namespace a\n"
7625 "{\n"
7626 "class A\n"
7627 "{\n"
7628 " void f()\n"
7629 " {\n"
7630 " if (true) {\n"
7631 " a();\n"
7632 " b();\n"
7633 " }\n"
7634 " }\n"
7635 " void g() { return; }\n"
7636 "}\n"
7637 "}",
7638 BreakBeforeBrace);
7639 }
7640
TEST_F(FormatTest,StroustrupBraceBreaking)7641 TEST_F(FormatTest, StroustrupBraceBreaking) {
7642 FormatStyle BreakBeforeBrace = getLLVMStyle();
7643 BreakBeforeBrace.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
7644 verifyFormat("namespace a {\n"
7645 "class A {\n"
7646 " void f()\n"
7647 " {\n"
7648 " if (true) {\n"
7649 " a();\n"
7650 " b();\n"
7651 " }\n"
7652 " }\n"
7653 " void g() { return; }\n"
7654 "}\n"
7655 "}",
7656 BreakBeforeBrace);
7657
7658 verifyFormat("#ifdef _DEBUG\n"
7659 "int foo(int i = 0)\n"
7660 "#else\n"
7661 "int foo(int i = 5)\n"
7662 "#endif\n"
7663 "{\n"
7664 " return i;\n"
7665 "}",
7666 BreakBeforeBrace);
7667
7668 verifyFormat("void foo() {}\n"
7669 "void bar()\n"
7670 "#ifdef _DEBUG\n"
7671 "{\n"
7672 " foo();\n"
7673 "}\n"
7674 "#else\n"
7675 "{\n"
7676 "}\n"
7677 "#endif",
7678 BreakBeforeBrace);
7679
7680 verifyFormat("void foobar() { int i = 5; }\n"
7681 "#ifdef _DEBUG\n"
7682 "void bar() {}\n"
7683 "#else\n"
7684 "void bar() { foobar(); }\n"
7685 "#endif",
7686 BreakBeforeBrace);
7687 }
7688
TEST_F(FormatTest,AllmanBraceBreaking)7689 TEST_F(FormatTest, AllmanBraceBreaking) {
7690 FormatStyle BreakBeforeBrace = getLLVMStyle();
7691 BreakBeforeBrace.BreakBeforeBraces = FormatStyle::BS_Allman;
7692 verifyFormat("namespace a\n"
7693 "{\n"
7694 "class A\n"
7695 "{\n"
7696 " void f()\n"
7697 " {\n"
7698 " if (true)\n"
7699 " {\n"
7700 " a();\n"
7701 " b();\n"
7702 " }\n"
7703 " }\n"
7704 " void g() { return; }\n"
7705 "}\n"
7706 "}",
7707 BreakBeforeBrace);
7708
7709 verifyFormat("void f()\n"
7710 "{\n"
7711 " if (true)\n"
7712 " {\n"
7713 " a();\n"
7714 " }\n"
7715 " else if (false)\n"
7716 " {\n"
7717 " b();\n"
7718 " }\n"
7719 " else\n"
7720 " {\n"
7721 " c();\n"
7722 " }\n"
7723 "}\n",
7724 BreakBeforeBrace);
7725
7726 verifyFormat("void f()\n"
7727 "{\n"
7728 " for (int i = 0; i < 10; ++i)\n"
7729 " {\n"
7730 " a();\n"
7731 " }\n"
7732 " while (false)\n"
7733 " {\n"
7734 " b();\n"
7735 " }\n"
7736 " do\n"
7737 " {\n"
7738 " c();\n"
7739 " } while (false)\n"
7740 "}\n",
7741 BreakBeforeBrace);
7742
7743 verifyFormat("void f(int a)\n"
7744 "{\n"
7745 " switch (a)\n"
7746 " {\n"
7747 " case 0:\n"
7748 " break;\n"
7749 " case 1:\n"
7750 " {\n"
7751 " break;\n"
7752 " }\n"
7753 " case 2:\n"
7754 " {\n"
7755 " }\n"
7756 " break;\n"
7757 " default:\n"
7758 " break;\n"
7759 " }\n"
7760 "}\n",
7761 BreakBeforeBrace);
7762
7763 verifyFormat("enum X\n"
7764 "{\n"
7765 " Y = 0,\n"
7766 "}\n",
7767 BreakBeforeBrace);
7768 verifyFormat("enum X\n"
7769 "{\n"
7770 " Y = 0\n"
7771 "}\n",
7772 BreakBeforeBrace);
7773
7774 verifyFormat("@interface BSApplicationController ()\n"
7775 "{\n"
7776 "@private\n"
7777 " id _extraIvar;\n"
7778 "}\n"
7779 "@end\n",
7780 BreakBeforeBrace);
7781
7782 verifyFormat("#ifdef _DEBUG\n"
7783 "int foo(int i = 0)\n"
7784 "#else\n"
7785 "int foo(int i = 5)\n"
7786 "#endif\n"
7787 "{\n"
7788 " return i;\n"
7789 "}",
7790 BreakBeforeBrace);
7791
7792 verifyFormat("void foo() {}\n"
7793 "void bar()\n"
7794 "#ifdef _DEBUG\n"
7795 "{\n"
7796 " foo();\n"
7797 "}\n"
7798 "#else\n"
7799 "{\n"
7800 "}\n"
7801 "#endif",
7802 BreakBeforeBrace);
7803
7804 verifyFormat("void foobar() { int i = 5; }\n"
7805 "#ifdef _DEBUG\n"
7806 "void bar() {}\n"
7807 "#else\n"
7808 "void bar() { foobar(); }\n"
7809 "#endif",
7810 BreakBeforeBrace);
7811
7812 // This shouldn't affect ObjC blocks..
7813 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
7814 " // ...\n"
7815 " int i;\n"
7816 "}];",
7817 BreakBeforeBrace);
7818 verifyFormat("void (^block)(void) = ^{\n"
7819 " // ...\n"
7820 " int i;\n"
7821 "};",
7822 BreakBeforeBrace);
7823 // .. or dict literals.
7824 verifyFormat("void f()\n"
7825 "{\n"
7826 " [object someMethod:@{ @\"a\" : @\"b\" }];\n"
7827 "}",
7828 BreakBeforeBrace);
7829
7830 BreakBeforeBrace.ColumnLimit = 19;
7831 verifyFormat("void f() { int i; }", BreakBeforeBrace);
7832 BreakBeforeBrace.ColumnLimit = 18;
7833 verifyFormat("void f()\n"
7834 "{\n"
7835 " int i;\n"
7836 "}",
7837 BreakBeforeBrace);
7838 BreakBeforeBrace.ColumnLimit = 80;
7839
7840 FormatStyle BreakBeforeBraceShortIfs = BreakBeforeBrace;
7841 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true;
7842 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
7843 verifyFormat("void f(bool b)\n"
7844 "{\n"
7845 " if (b)\n"
7846 " {\n"
7847 " return;\n"
7848 " }\n"
7849 "}\n",
7850 BreakBeforeBraceShortIfs);
7851 verifyFormat("void f(bool b)\n"
7852 "{\n"
7853 " if (b) return;\n"
7854 "}\n",
7855 BreakBeforeBraceShortIfs);
7856 verifyFormat("void f(bool b)\n"
7857 "{\n"
7858 " while (b)\n"
7859 " {\n"
7860 " return;\n"
7861 " }\n"
7862 "}\n",
7863 BreakBeforeBraceShortIfs);
7864 }
7865
TEST_F(FormatTest,GNUBraceBreaking)7866 TEST_F(FormatTest, GNUBraceBreaking) {
7867 FormatStyle GNUBraceStyle = getLLVMStyle();
7868 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
7869 verifyFormat("namespace a\n"
7870 "{\n"
7871 "class A\n"
7872 "{\n"
7873 " void f()\n"
7874 " {\n"
7875 " int a;\n"
7876 " {\n"
7877 " int b;\n"
7878 " }\n"
7879 " if (true)\n"
7880 " {\n"
7881 " a();\n"
7882 " b();\n"
7883 " }\n"
7884 " }\n"
7885 " void g() { return; }\n"
7886 "}\n"
7887 "}",
7888 GNUBraceStyle);
7889
7890 verifyFormat("void f()\n"
7891 "{\n"
7892 " if (true)\n"
7893 " {\n"
7894 " a();\n"
7895 " }\n"
7896 " else if (false)\n"
7897 " {\n"
7898 " b();\n"
7899 " }\n"
7900 " else\n"
7901 " {\n"
7902 " c();\n"
7903 " }\n"
7904 "}\n",
7905 GNUBraceStyle);
7906
7907 verifyFormat("void f()\n"
7908 "{\n"
7909 " for (int i = 0; i < 10; ++i)\n"
7910 " {\n"
7911 " a();\n"
7912 " }\n"
7913 " while (false)\n"
7914 " {\n"
7915 " b();\n"
7916 " }\n"
7917 " do\n"
7918 " {\n"
7919 " c();\n"
7920 " }\n"
7921 " while (false);\n"
7922 "}\n",
7923 GNUBraceStyle);
7924
7925 verifyFormat("void f(int a)\n"
7926 "{\n"
7927 " switch (a)\n"
7928 " {\n"
7929 " case 0:\n"
7930 " break;\n"
7931 " case 1:\n"
7932 " {\n"
7933 " break;\n"
7934 " }\n"
7935 " case 2:\n"
7936 " {\n"
7937 " }\n"
7938 " break;\n"
7939 " default:\n"
7940 " break;\n"
7941 " }\n"
7942 "}\n",
7943 GNUBraceStyle);
7944
7945 verifyFormat("enum X\n"
7946 "{\n"
7947 " Y = 0,\n"
7948 "}\n",
7949 GNUBraceStyle);
7950
7951 verifyFormat("@interface BSApplicationController ()\n"
7952 "{\n"
7953 "@private\n"
7954 " id _extraIvar;\n"
7955 "}\n"
7956 "@end\n",
7957 GNUBraceStyle);
7958
7959 verifyFormat("#ifdef _DEBUG\n"
7960 "int foo(int i = 0)\n"
7961 "#else\n"
7962 "int foo(int i = 5)\n"
7963 "#endif\n"
7964 "{\n"
7965 " return i;\n"
7966 "}",
7967 GNUBraceStyle);
7968
7969 verifyFormat("void foo() {}\n"
7970 "void bar()\n"
7971 "#ifdef _DEBUG\n"
7972 "{\n"
7973 " foo();\n"
7974 "}\n"
7975 "#else\n"
7976 "{\n"
7977 "}\n"
7978 "#endif",
7979 GNUBraceStyle);
7980
7981 verifyFormat("void foobar() { int i = 5; }\n"
7982 "#ifdef _DEBUG\n"
7983 "void bar() {}\n"
7984 "#else\n"
7985 "void bar() { foobar(); }\n"
7986 "#endif",
7987 GNUBraceStyle);
7988 }
TEST_F(FormatTest,CatchExceptionReferenceBinding)7989 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
7990 verifyFormat("void f() {\n"
7991 " try {\n"
7992 " } catch (const Exception &e) {\n"
7993 " }\n"
7994 "}\n",
7995 getLLVMStyle());
7996 }
7997
TEST_F(FormatTest,UnderstandsPragmas)7998 TEST_F(FormatTest, UnderstandsPragmas) {
7999 verifyFormat("#pragma omp reduction(| : var)");
8000 verifyFormat("#pragma omp reduction(+ : var)");
8001
8002 EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
8003 "(including parentheses).",
8004 format("#pragma mark Any non-hyphenated or hyphenated string "
8005 "(including parentheses)."));
8006 }
8007
8008 #define EXPECT_ALL_STYLES_EQUAL(Styles) \
8009 for (size_t i = 1; i < Styles.size(); ++i) \
8010 EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " \
8011 << Styles.size() \
8012 << " differs from Style #0"
8013
TEST_F(FormatTest,GetsPredefinedStyleByName)8014 TEST_F(FormatTest, GetsPredefinedStyleByName) {
8015 SmallVector<FormatStyle, 3> Styles;
8016 Styles.resize(3);
8017
8018 Styles[0] = getLLVMStyle();
8019 EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
8020 EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
8021 EXPECT_ALL_STYLES_EQUAL(Styles);
8022
8023 Styles[0] = getGoogleStyle();
8024 EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
8025 EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
8026 EXPECT_ALL_STYLES_EQUAL(Styles);
8027
8028 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
8029 EXPECT_TRUE(
8030 getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
8031 EXPECT_TRUE(
8032 getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
8033 EXPECT_ALL_STYLES_EQUAL(Styles);
8034
8035 Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
8036 EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
8037 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
8038 EXPECT_ALL_STYLES_EQUAL(Styles);
8039
8040 Styles[0] = getMozillaStyle();
8041 EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
8042 EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
8043 EXPECT_ALL_STYLES_EQUAL(Styles);
8044
8045 Styles[0] = getWebKitStyle();
8046 EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
8047 EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
8048 EXPECT_ALL_STYLES_EQUAL(Styles);
8049
8050 Styles[0] = getGNUStyle();
8051 EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
8052 EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
8053 EXPECT_ALL_STYLES_EQUAL(Styles);
8054
8055 EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
8056 }
8057
TEST_F(FormatTest,GetsCorrectBasedOnStyle)8058 TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
8059 SmallVector<FormatStyle, 8> Styles;
8060 Styles.resize(2);
8061
8062 Styles[0] = getGoogleStyle();
8063 Styles[1] = getLLVMStyle();
8064 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
8065 EXPECT_ALL_STYLES_EQUAL(Styles);
8066
8067 Styles.resize(5);
8068 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
8069 Styles[1] = getLLVMStyle();
8070 Styles[1].Language = FormatStyle::LK_JavaScript;
8071 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
8072
8073 Styles[2] = getLLVMStyle();
8074 Styles[2].Language = FormatStyle::LK_JavaScript;
8075 EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
8076 "BasedOnStyle: Google",
8077 &Styles[2]).value());
8078
8079 Styles[3] = getLLVMStyle();
8080 Styles[3].Language = FormatStyle::LK_JavaScript;
8081 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
8082 "Language: JavaScript",
8083 &Styles[3]).value());
8084
8085 Styles[4] = getLLVMStyle();
8086 Styles[4].Language = FormatStyle::LK_JavaScript;
8087 EXPECT_EQ(0, parseConfiguration("---\n"
8088 "BasedOnStyle: LLVM\n"
8089 "IndentWidth: 123\n"
8090 "---\n"
8091 "BasedOnStyle: Google\n"
8092 "Language: JavaScript",
8093 &Styles[4]).value());
8094 EXPECT_ALL_STYLES_EQUAL(Styles);
8095 }
8096
8097 #define CHECK_PARSE(TEXT, FIELD, VALUE) \
8098 EXPECT_NE(VALUE, Style.FIELD); \
8099 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \
8100 EXPECT_EQ(VALUE, Style.FIELD)
8101
8102 #define CHECK_PARSE_BOOL(FIELD) \
8103 Style.FIELD = false; \
8104 EXPECT_EQ(0, parseConfiguration(#FIELD ": true", &Style).value()); \
8105 EXPECT_TRUE(Style.FIELD); \
8106 EXPECT_EQ(0, parseConfiguration(#FIELD ": false", &Style).value()); \
8107 EXPECT_FALSE(Style.FIELD);
8108
TEST_F(FormatTest,ParsesConfiguration)8109 TEST_F(FormatTest, ParsesConfiguration) {
8110 FormatStyle Style = {};
8111 Style.Language = FormatStyle::LK_Cpp;
8112 CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft);
8113 CHECK_PARSE_BOOL(AlignTrailingComments);
8114 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
8115 CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine);
8116 CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine);
8117 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
8118 CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations);
8119 CHECK_PARSE_BOOL(BinPackParameters);
8120 CHECK_PARSE_BOOL(BreakBeforeBinaryOperators);
8121 CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
8122 CHECK_PARSE_BOOL(BreakConstructorInitializersBeforeComma);
8123 CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
8124 CHECK_PARSE_BOOL(DerivePointerAlignment);
8125 CHECK_PARSE_BOOL(IndentCaseLabels);
8126 CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
8127 CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
8128 CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
8129 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
8130 CHECK_PARSE_BOOL(Cpp11BracedListStyle);
8131 CHECK_PARSE_BOOL(SpacesInParentheses);
8132 CHECK_PARSE_BOOL(SpacesInAngles);
8133 CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
8134 CHECK_PARSE_BOOL(SpacesInContainerLiterals);
8135 CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
8136 CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
8137
8138 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
8139 CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
8140 ConstructorInitializerIndentWidth, 1234u);
8141 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
8142 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
8143 CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
8144 PenaltyBreakBeforeFirstCallParameter, 1234u);
8145 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
8146 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
8147 PenaltyReturnTypeOnItsOwnLine, 1234u);
8148 CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
8149 SpacesBeforeTrailingComments, 1234u);
8150 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
8151 CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
8152
8153 Style.PointerAlignment = FormatStyle::PAS_Middle;
8154 CHECK_PARSE("PointerAlignment: Left", PointerAlignment, FormatStyle::PAS_Left);
8155 CHECK_PARSE("PointerAlignment: Right", PointerAlignment, FormatStyle::PAS_Right);
8156 CHECK_PARSE("PointerAlignment: Middle", PointerAlignment, FormatStyle::PAS_Middle);
8157
8158 Style.Standard = FormatStyle::LS_Auto;
8159 CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
8160 CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11);
8161 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
8162 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
8163 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
8164
8165 Style.UseTab = FormatStyle::UT_ForIndentation;
8166 CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
8167 CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
8168 CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
8169 CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
8170 CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
8171
8172 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
8173 CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
8174 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
8175 CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
8176 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
8177 CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
8178 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
8179 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
8180 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
8181 CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
8182 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
8183
8184 Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
8185 CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
8186 FormatStyle::SBPO_Never);
8187 CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
8188 FormatStyle::SBPO_Always);
8189 CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
8190 FormatStyle::SBPO_ControlStatements);
8191 // For backward compatibility:
8192 CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
8193 FormatStyle::SBPO_Never);
8194 CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
8195 FormatStyle::SBPO_ControlStatements);
8196
8197 Style.ColumnLimit = 123;
8198 FormatStyle BaseStyle = getLLVMStyle();
8199 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
8200 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
8201
8202 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
8203 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
8204 FormatStyle::BS_Attach);
8205 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
8206 FormatStyle::BS_Linux);
8207 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
8208 FormatStyle::BS_Stroustrup);
8209 CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
8210 FormatStyle::BS_Allman);
8211 CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
8212
8213 Style.NamespaceIndentation = FormatStyle::NI_All;
8214 CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
8215 FormatStyle::NI_None);
8216 CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
8217 FormatStyle::NI_Inner);
8218 CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
8219 FormatStyle::NI_All);
8220
8221 Style.ForEachMacros.clear();
8222 std::vector<std::string> BoostForeach;
8223 BoostForeach.push_back("BOOST_FOREACH");
8224 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
8225 std::vector<std::string> BoostAndQForeach;
8226 BoostAndQForeach.push_back("BOOST_FOREACH");
8227 BoostAndQForeach.push_back("Q_FOREACH");
8228 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
8229 BoostAndQForeach);
8230 }
8231
TEST_F(FormatTest,ParsesConfigurationWithLanguages)8232 TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
8233 FormatStyle Style = {};
8234 Style.Language = FormatStyle::LK_Cpp;
8235 CHECK_PARSE("Language: Cpp\n"
8236 "IndentWidth: 12",
8237 IndentWidth, 12u);
8238 EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
8239 "IndentWidth: 34",
8240 &Style),
8241 ParseError::Unsuitable);
8242 EXPECT_EQ(12u, Style.IndentWidth);
8243 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
8244 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
8245
8246 Style.Language = FormatStyle::LK_JavaScript;
8247 CHECK_PARSE("Language: JavaScript\n"
8248 "IndentWidth: 12",
8249 IndentWidth, 12u);
8250 CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
8251 EXPECT_EQ(parseConfiguration("Language: Cpp\n"
8252 "IndentWidth: 34",
8253 &Style),
8254 ParseError::Unsuitable);
8255 EXPECT_EQ(23u, Style.IndentWidth);
8256 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
8257 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
8258
8259 CHECK_PARSE("BasedOnStyle: LLVM\n"
8260 "IndentWidth: 67",
8261 IndentWidth, 67u);
8262
8263 CHECK_PARSE("---\n"
8264 "Language: JavaScript\n"
8265 "IndentWidth: 12\n"
8266 "---\n"
8267 "Language: Cpp\n"
8268 "IndentWidth: 34\n"
8269 "...\n",
8270 IndentWidth, 12u);
8271
8272 Style.Language = FormatStyle::LK_Cpp;
8273 CHECK_PARSE("---\n"
8274 "Language: JavaScript\n"
8275 "IndentWidth: 12\n"
8276 "---\n"
8277 "Language: Cpp\n"
8278 "IndentWidth: 34\n"
8279 "...\n",
8280 IndentWidth, 34u);
8281 CHECK_PARSE("---\n"
8282 "IndentWidth: 78\n"
8283 "---\n"
8284 "Language: JavaScript\n"
8285 "IndentWidth: 56\n"
8286 "...\n",
8287 IndentWidth, 78u);
8288
8289 Style.ColumnLimit = 123;
8290 Style.IndentWidth = 234;
8291 Style.BreakBeforeBraces = FormatStyle::BS_Linux;
8292 Style.TabWidth = 345;
8293 EXPECT_FALSE(parseConfiguration("---\n"
8294 "IndentWidth: 456\n"
8295 "BreakBeforeBraces: Allman\n"
8296 "---\n"
8297 "Language: JavaScript\n"
8298 "IndentWidth: 111\n"
8299 "TabWidth: 111\n"
8300 "---\n"
8301 "Language: Cpp\n"
8302 "BreakBeforeBraces: Stroustrup\n"
8303 "TabWidth: 789\n"
8304 "...\n",
8305 &Style));
8306 EXPECT_EQ(123u, Style.ColumnLimit);
8307 EXPECT_EQ(456u, Style.IndentWidth);
8308 EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
8309 EXPECT_EQ(789u, Style.TabWidth);
8310
8311 EXPECT_EQ(parseConfiguration("---\n"
8312 "Language: JavaScript\n"
8313 "IndentWidth: 56\n"
8314 "---\n"
8315 "IndentWidth: 78\n"
8316 "...\n",
8317 &Style),
8318 ParseError::Error);
8319 EXPECT_EQ(parseConfiguration("---\n"
8320 "Language: JavaScript\n"
8321 "IndentWidth: 56\n"
8322 "---\n"
8323 "Language: JavaScript\n"
8324 "IndentWidth: 78\n"
8325 "...\n",
8326 &Style),
8327 ParseError::Error);
8328
8329 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
8330 }
8331
8332 #undef CHECK_PARSE
8333 #undef CHECK_PARSE_BOOL
8334
TEST_F(FormatTest,UsesLanguageForBasedOnStyle)8335 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
8336 FormatStyle Style = {};
8337 Style.Language = FormatStyle::LK_JavaScript;
8338 Style.BreakBeforeTernaryOperators = true;
8339 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
8340 EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
8341
8342 Style.BreakBeforeTernaryOperators = true;
8343 EXPECT_EQ(0, parseConfiguration("---\n"
8344 "BasedOnStyle: Google\n"
8345 "---\n"
8346 "Language: JavaScript\n"
8347 "IndentWidth: 76\n"
8348 "...\n", &Style).value());
8349 EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
8350 EXPECT_EQ(76u, Style.IndentWidth);
8351 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
8352 }
8353
TEST_F(FormatTest,ConfigurationRoundTripTest)8354 TEST_F(FormatTest, ConfigurationRoundTripTest) {
8355 FormatStyle Style = getLLVMStyle();
8356 std::string YAML = configurationAsText(Style);
8357 FormatStyle ParsedStyle = {};
8358 ParsedStyle.Language = FormatStyle::LK_Cpp;
8359 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
8360 EXPECT_EQ(Style, ParsedStyle);
8361 }
8362
TEST_F(FormatTest,WorksFor8bitEncodings)8363 TEST_F(FormatTest, WorksFor8bitEncodings) {
8364 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
8365 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
8366 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
8367 "\"\xef\xee\xf0\xf3...\"",
8368 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
8369 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
8370 "\xef\xee\xf0\xf3...\"",
8371 getLLVMStyleWithColumns(12)));
8372 }
8373
TEST_F(FormatTest,HandlesUTF8BOM)8374 TEST_F(FormatTest, HandlesUTF8BOM) {
8375 EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
8376 EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
8377 format("\xef\xbb\xbf#include <iostream>"));
8378 EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
8379 format("\xef\xbb\xbf\n#include <iostream>"));
8380 }
8381
8382 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
8383 #if !defined(_MSC_VER)
8384
TEST_F(FormatTest,CountsUTF8CharactersProperly)8385 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
8386 verifyFormat("\"Однажды в студёную зимнюю пору...\"",
8387 getLLVMStyleWithColumns(35));
8388 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
8389 getLLVMStyleWithColumns(31));
8390 verifyFormat("// Однажды в студёную зимнюю пору...",
8391 getLLVMStyleWithColumns(36));
8392 verifyFormat("// 一 二 三 四 五 六 七 八 九 十",
8393 getLLVMStyleWithColumns(32));
8394 verifyFormat("/* Однажды в студёную зимнюю пору... */",
8395 getLLVMStyleWithColumns(39));
8396 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
8397 getLLVMStyleWithColumns(35));
8398 }
8399
TEST_F(FormatTest,SplitsUTF8Strings)8400 TEST_F(FormatTest, SplitsUTF8Strings) {
8401 // Non-printable characters' width is currently considered to be the length in
8402 // bytes in UTF8. The characters can be displayed in very different manner
8403 // (zero-width, single width with a substitution glyph, expanded to their code
8404 // (e.g. "<8d>"), so there's no single correct way to handle them.
8405 EXPECT_EQ("\"aaaaÄ\"\n"
8406 "\"\xc2\x8d\";",
8407 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
8408 EXPECT_EQ("\"aaaaaaaÄ\"\n"
8409 "\"\xc2\x8d\";",
8410 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
8411 EXPECT_EQ(
8412 "\"Однажды, в \"\n"
8413 "\"студёную \"\n"
8414 "\"зимнюю \"\n"
8415 "\"пору,\"",
8416 format("\"Однажды, в студёную зимнюю пору,\"",
8417 getLLVMStyleWithColumns(13)));
8418 EXPECT_EQ("\"一 二 三 \"\n"
8419 "\"四 五六 \"\n"
8420 "\"七 八 九 \"\n"
8421 "\"十\"",
8422 format("\"一 二 三 四 五六 七 八 九 十\"",
8423 getLLVMStyleWithColumns(11)));
8424 EXPECT_EQ("\"一\t二 \"\n"
8425 "\"\t三 \"\n"
8426 "\"四 五\t六 \"\n"
8427 "\"\t七 \"\n"
8428 "\"八九十\tqq\"",
8429 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
8430 getLLVMStyleWithColumns(11)));
8431 }
8432
8433
TEST_F(FormatTest,HandlesDoubleWidthCharsInMultiLineStrings)8434 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
8435 EXPECT_EQ("const char *sssss =\n"
8436 " \"一二三四五六七八\\\n"
8437 " 九 十\";",
8438 format("const char *sssss = \"一二三四五六七八\\\n"
8439 " 九 十\";",
8440 getLLVMStyleWithColumns(30)));
8441 }
8442
TEST_F(FormatTest,SplitsUTF8LineComments)8443 TEST_F(FormatTest, SplitsUTF8LineComments) {
8444 EXPECT_EQ("// aaaaÄ\xc2\x8d",
8445 format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
8446 EXPECT_EQ("// Я из лесу\n"
8447 "// вышел; был\n"
8448 "// сильный\n"
8449 "// мороз.",
8450 format("// Я из лесу вышел; был сильный мороз.",
8451 getLLVMStyleWithColumns(13)));
8452 EXPECT_EQ("// 一二三\n"
8453 "// 四五六七\n"
8454 "// 八 九\n"
8455 "// 十",
8456 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9)));
8457 }
8458
TEST_F(FormatTest,SplitsUTF8BlockComments)8459 TEST_F(FormatTest, SplitsUTF8BlockComments) {
8460 EXPECT_EQ("/* Гляжу,\n"
8461 " * поднимается\n"
8462 " * медленно в\n"
8463 " * гору\n"
8464 " * Лошадка,\n"
8465 " * везущая\n"
8466 " * хворосту\n"
8467 " * воз. */",
8468 format("/* Гляжу, поднимается медленно в гору\n"
8469 " * Лошадка, везущая хворосту воз. */",
8470 getLLVMStyleWithColumns(13)));
8471 EXPECT_EQ(
8472 "/* 一二三\n"
8473 " * 四五六七\n"
8474 " * 八 九\n"
8475 " * 十 */",
8476 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9)));
8477 EXPECT_EQ("/* \n"
8478 " * \n"
8479 " * - */",
8480 format("/* - */", getLLVMStyleWithColumns(12)));
8481 }
8482
8483 #endif // _MSC_VER
8484
TEST_F(FormatTest,ConstructorInitializerIndentWidth)8485 TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
8486 FormatStyle Style = getLLVMStyle();
8487
8488 Style.ConstructorInitializerIndentWidth = 4;
8489 verifyFormat(
8490 "SomeClass::Constructor()\n"
8491 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
8492 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
8493 Style);
8494
8495 Style.ConstructorInitializerIndentWidth = 2;
8496 verifyFormat(
8497 "SomeClass::Constructor()\n"
8498 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
8499 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
8500 Style);
8501
8502 Style.ConstructorInitializerIndentWidth = 0;
8503 verifyFormat(
8504 "SomeClass::Constructor()\n"
8505 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
8506 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
8507 Style);
8508
8509 Style.BreakConstructorInitializersBeforeComma = true;
8510 Style.ConstructorInitializerIndentWidth = 4;
8511 verifyFormat("SomeClass::Constructor()\n"
8512 " : a(a)\n"
8513 " , b(b)\n"
8514 " , c(c) {}",
8515 Style);
8516 verifyFormat("SomeClass::Constructor()\n"
8517 " : a(a) {}",
8518 Style);
8519
8520 Style.ColumnLimit = 0;
8521 verifyFormat("SomeClass::Constructor()\n"
8522 " : a(a) {}",
8523 Style);
8524 verifyFormat("SomeClass::Constructor()\n"
8525 " : a(a)\n"
8526 " , b(b)\n"
8527 " , c(c) {}",
8528 Style);
8529 verifyFormat("SomeClass::Constructor()\n"
8530 " : a(a) {\n"
8531 " foo();\n"
8532 " bar();\n"
8533 "}",
8534 Style);
8535
8536 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
8537 verifyFormat("SomeClass::Constructor()\n"
8538 " : a(a)\n"
8539 " , b(b)\n"
8540 " , c(c) {\n}",
8541 Style);
8542 verifyFormat("SomeClass::Constructor()\n"
8543 " : a(a) {\n}",
8544 Style);
8545
8546 Style.ColumnLimit = 80;
8547 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
8548 Style.ConstructorInitializerIndentWidth = 2;
8549 verifyFormat("SomeClass::Constructor()\n"
8550 " : a(a)\n"
8551 " , b(b)\n"
8552 " , c(c) {}",
8553 Style);
8554
8555 Style.ConstructorInitializerIndentWidth = 0;
8556 verifyFormat("SomeClass::Constructor()\n"
8557 ": a(a)\n"
8558 ", b(b)\n"
8559 ", c(c) {}",
8560 Style);
8561
8562 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
8563 Style.ConstructorInitializerIndentWidth = 4;
8564 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
8565 verifyFormat(
8566 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
8567 Style);
8568 verifyFormat(
8569 "SomeClass::Constructor()\n"
8570 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
8571 Style);
8572 Style.ConstructorInitializerIndentWidth = 4;
8573 Style.ColumnLimit = 60;
8574 verifyFormat("SomeClass::Constructor()\n"
8575 " : aaaaaaaa(aaaaaaaa)\n"
8576 " , aaaaaaaa(aaaaaaaa)\n"
8577 " , aaaaaaaa(aaaaaaaa) {}",
8578 Style);
8579 }
8580
TEST_F(FormatTest,FormatsWithWebKitStyle)8581 TEST_F(FormatTest, FormatsWithWebKitStyle) {
8582 FormatStyle Style = getWebKitStyle();
8583
8584 // Don't indent in outer namespaces.
8585 verifyFormat("namespace outer {\n"
8586 "int i;\n"
8587 "namespace inner {\n"
8588 " int i;\n"
8589 "} // namespace inner\n"
8590 "} // namespace outer\n"
8591 "namespace other_outer {\n"
8592 "int i;\n"
8593 "}",
8594 Style);
8595
8596 // Don't indent case labels.
8597 verifyFormat("switch (variable) {\n"
8598 "case 1:\n"
8599 "case 2:\n"
8600 " doSomething();\n"
8601 " break;\n"
8602 "default:\n"
8603 " ++variable;\n"
8604 "}",
8605 Style);
8606
8607 // Wrap before binary operators.
8608 EXPECT_EQ(
8609 "void f()\n"
8610 "{\n"
8611 " if (aaaaaaaaaaaaaaaa\n"
8612 " && bbbbbbbbbbbbbbbbbbbbbbbb\n"
8613 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
8614 " return;\n"
8615 "}",
8616 format(
8617 "void f() {\n"
8618 "if (aaaaaaaaaaaaaaaa\n"
8619 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
8620 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
8621 "return;\n"
8622 "}",
8623 Style));
8624
8625 // Allow functions on a single line.
8626 verifyFormat("void f() { return; }", Style);
8627
8628 // Constructor initializers are formatted one per line with the "," on the
8629 // new line.
8630 verifyFormat("Constructor()\n"
8631 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8632 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
8633 " aaaaaaaaaaaaaa)\n"
8634 " , aaaaaaaaaaaaaaaaaaaaaaa()\n"
8635 "{\n"
8636 "}",
8637 Style);
8638 verifyFormat("SomeClass::Constructor()\n"
8639 " : a(a)\n"
8640 "{\n"
8641 "}",
8642 Style);
8643 EXPECT_EQ("SomeClass::Constructor()\n"
8644 " : a(a)\n"
8645 "{\n"
8646 "}",
8647 format("SomeClass::Constructor():a(a){}", Style));
8648 verifyFormat("SomeClass::Constructor()\n"
8649 " : a(a)\n"
8650 " , b(b)\n"
8651 " , c(c)\n"
8652 "{\n"
8653 "}", Style);
8654 verifyFormat("SomeClass::Constructor()\n"
8655 " : a(a)\n"
8656 "{\n"
8657 " foo();\n"
8658 " bar();\n"
8659 "}",
8660 Style);
8661
8662 // Access specifiers should be aligned left.
8663 verifyFormat("class C {\n"
8664 "public:\n"
8665 " int i;\n"
8666 "};",
8667 Style);
8668
8669 // Do not align comments.
8670 verifyFormat("int a; // Do not\n"
8671 "double b; // align comments.",
8672 Style);
8673
8674 // Accept input's line breaks.
8675 EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
8676 " || bbbbbbbbbbbbbbb) {\n"
8677 " i++;\n"
8678 "}",
8679 format("if (aaaaaaaaaaaaaaa\n"
8680 "|| bbbbbbbbbbbbbbb) { i++; }",
8681 Style));
8682 EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
8683 " i++;\n"
8684 "}",
8685 format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
8686
8687 // Don't automatically break all macro definitions (llvm.org/PR17842).
8688 verifyFormat("#define aNumber 10", Style);
8689 // However, generally keep the line breaks that the user authored.
8690 EXPECT_EQ("#define aNumber \\\n"
8691 " 10",
8692 format("#define aNumber \\\n"
8693 " 10",
8694 Style));
8695
8696 // Keep empty and one-element array literals on a single line.
8697 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
8698 " copyItems:YES];",
8699 format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
8700 "copyItems:YES];",
8701 Style));
8702 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
8703 " copyItems:YES];",
8704 format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
8705 " copyItems:YES];",
8706 Style));
8707 // FIXME: This does not seem right, there should be more indentation before
8708 // the array literal's entries. Nested blocks have the same problem.
8709 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
8710 " @\"a\",\n"
8711 " @\"a\"\n"
8712 "]\n"
8713 " copyItems:YES];",
8714 format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
8715 " @\"a\",\n"
8716 " @\"a\"\n"
8717 " ]\n"
8718 " copyItems:YES];",
8719 Style));
8720 EXPECT_EQ(
8721 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
8722 " copyItems:YES];",
8723 format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
8724 " copyItems:YES];",
8725 Style));
8726
8727 verifyFormat("[self.a b:c c:d];", Style);
8728 EXPECT_EQ("[self.a b:c\n"
8729 " c:d];",
8730 format("[self.a b:c\n"
8731 "c:d];",
8732 Style));
8733 }
8734
TEST_F(FormatTest,FormatsLambdas)8735 TEST_F(FormatTest, FormatsLambdas) {
8736 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
8737 verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
8738 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
8739 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
8740 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
8741 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
8742 verifyFormat("void f() {\n"
8743 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
8744 "}\n");
8745 verifyFormat("void f() {\n"
8746 " other(x.begin(), //\n"
8747 " x.end(), //\n"
8748 " [&](int, int) { return 1; });\n"
8749 "}\n");
8750 verifyFormat("SomeFunction([]() { // A cool function...\n"
8751 " return 43;\n"
8752 "});");
8753 verifyFormat("void f() {\n"
8754 " SomeFunction([](decltype(x), A *a) {});\n"
8755 "}");
8756 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8757 " [](const aaaaaaaaaa &a) { return a; });");
8758
8759 // Lambdas with return types.
8760 verifyFormat("int c = []() -> int { return 2; }();\n");
8761 verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
8762 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
8763 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
8764 " int j) -> int {\n"
8765 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
8766 "};");
8767
8768 // Multiple lambdas in the same parentheses change indentation rules.
8769 verifyFormat("SomeFunction([]() {\n"
8770 " int i = 42;\n"
8771 " return i;\n"
8772 " },\n"
8773 " []() {\n"
8774 " int j = 43;\n"
8775 " return j;\n"
8776 " });");
8777
8778 // More complex introducers.
8779 verifyFormat("return [i, args...] {};");
8780
8781 // Not lambdas.
8782 verifyFormat("constexpr char hello[]{\"hello\"};");
8783 verifyFormat("double &operator[](int i) { return 0; }\n"
8784 "int i;");
8785 verifyFormat("std::unique_ptr<int[]> foo() {}");
8786 verifyFormat("int i = a[a][a]->f();");
8787 verifyFormat("int i = (*b)[a]->f();");
8788
8789 // Other corner cases.
8790 verifyFormat("void f() {\n"
8791 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
8792 " );\n"
8793 "}");
8794
8795 // Lambdas created through weird macros.
8796 verifyFormat("void f() {\n"
8797 " MACRO((const AA &a) { return 1; });\n"
8798 "}");
8799 }
8800
TEST_F(FormatTest,FormatsBlocks)8801 TEST_F(FormatTest, FormatsBlocks) {
8802 verifyFormat("int (^Block)(int, int);");
8803 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)");
8804 verifyFormat("void (^block)(int) = ^(id test) { int i; };");
8805 verifyFormat("void (^block)(int) = ^(int test) { int i; };");
8806 verifyFormat("void (^block)(int) = ^id(int test) { int i; };");
8807 verifyFormat("void (^block)(int) = ^int(int test) { int i; };");
8808
8809 verifyFormat("foo(^{ bar(); });");
8810 verifyFormat("foo(a, ^{ bar(); });");
8811
8812 // FIXME: Make whitespace formatting consistent. Ask a ObjC dev how
8813 // it would ideally look.
8814 verifyFormat("[operation setCompletionBlock:^{ [self onOperationDone]; }];");
8815 verifyFormat("int i = {[operation setCompletionBlock : ^{ [self "
8816 "onOperationDone]; }]};");
8817 verifyFormat("[operation setCompletionBlock:^(int *i) { f(); }];");
8818 verifyFormat("int a = [operation block:^int(int *i) { return 1; }];");
8819 verifyFormat("[myObject doSomethingWith:arg1\n"
8820 " aaa:^int(int *a) { return 1; }\n"
8821 " bbb:f(a * bbbbbbbb)];");
8822
8823 verifyFormat("[operation setCompletionBlock:^{\n"
8824 " [self.delegate newDataAvailable];\n"
8825 "}];",
8826 getLLVMStyleWithColumns(60));
8827 verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
8828 " NSString *path = [self sessionFilePath];\n"
8829 " if (path) {\n"
8830 " // ...\n"
8831 " }\n"
8832 "});");
8833 verifyFormat("[[SessionService sharedService]\n"
8834 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
8835 " if (window) {\n"
8836 " [self windowDidLoad:window];\n"
8837 " } else {\n"
8838 " [self errorLoadingWindow];\n"
8839 " }\n"
8840 " }];");
8841 verifyFormat("void (^largeBlock)(void) = ^{\n"
8842 " // ...\n"
8843 "};\n",
8844 getLLVMStyleWithColumns(40));
8845 verifyFormat("[[SessionService sharedService]\n"
8846 " loadWindowWithCompletionBlock: //\n"
8847 " ^(SessionWindow *window) {\n"
8848 " if (window) {\n"
8849 " [self windowDidLoad:window];\n"
8850 " } else {\n"
8851 " [self errorLoadingWindow];\n"
8852 " }\n"
8853 " }];",
8854 getLLVMStyleWithColumns(60));
8855 verifyFormat("[myObject doSomethingWith:arg1\n"
8856 " firstBlock:^(Foo *a) {\n"
8857 " // ...\n"
8858 " int i;\n"
8859 " }\n"
8860 " secondBlock:^(Bar *b) {\n"
8861 " // ...\n"
8862 " int i;\n"
8863 " }\n"
8864 " thirdBlock:^Foo(Bar *b) {\n"
8865 " // ...\n"
8866 " int i;\n"
8867 " }];");
8868 verifyFormat("[myObject doSomethingWith:arg1\n"
8869 " firstBlock:-1\n"
8870 " secondBlock:^(Bar *b) {\n"
8871 " // ...\n"
8872 " int i;\n"
8873 " }];");
8874
8875 verifyFormat("f(^{\n"
8876 " @autoreleasepool {\n"
8877 " if (a) {\n"
8878 " g();\n"
8879 " }\n"
8880 " }\n"
8881 "});");
8882 }
8883
TEST_F(FormatTest,SupportsCRLF)8884 TEST_F(FormatTest, SupportsCRLF) {
8885 EXPECT_EQ("int a;\r\n"
8886 "int b;\r\n"
8887 "int c;\r\n",
8888 format("int a;\r\n"
8889 " int b;\r\n"
8890 " int c;\r\n",
8891 getLLVMStyle()));
8892 EXPECT_EQ("int a;\r\n"
8893 "int b;\r\n"
8894 "int c;\r\n",
8895 format("int a;\r\n"
8896 " int b;\n"
8897 " int c;\r\n",
8898 getLLVMStyle()));
8899 EXPECT_EQ("int a;\n"
8900 "int b;\n"
8901 "int c;\n",
8902 format("int a;\r\n"
8903 " int b;\n"
8904 " int c;\n",
8905 getLLVMStyle()));
8906 EXPECT_EQ("\"aaaaaaa \"\r\n"
8907 "\"bbbbbbb\";\r\n",
8908 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
8909 EXPECT_EQ("#define A \\\r\n"
8910 " b; \\\r\n"
8911 " c; \\\r\n"
8912 " d;\r\n",
8913 format("#define A \\\r\n"
8914 " b; \\\r\n"
8915 " c; d; \r\n",
8916 getGoogleStyle()));
8917
8918 EXPECT_EQ("/*\r\n"
8919 "multi line block comments\r\n"
8920 "should not introduce\r\n"
8921 "an extra carriage return\r\n"
8922 "*/\r\n",
8923 format("/*\r\n"
8924 "multi line block comments\r\n"
8925 "should not introduce\r\n"
8926 "an extra carriage return\r\n"
8927 "*/\r\n"));
8928 }
8929
TEST_F(FormatTest,MunchSemicolonAfterBlocks)8930 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
8931 verifyFormat("MY_CLASS(C) {\n"
8932 " int i;\n"
8933 " int j;\n"
8934 "};");
8935 }
8936
TEST_F(FormatTest,ConfigurableContinuationIndentWidth)8937 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
8938 FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
8939 TwoIndent.ContinuationIndentWidth = 2;
8940
8941 EXPECT_EQ("int i =\n"
8942 " longFunction(\n"
8943 " arg);",
8944 format("int i = longFunction(arg);", TwoIndent));
8945
8946 FormatStyle SixIndent = getLLVMStyleWithColumns(20);
8947 SixIndent.ContinuationIndentWidth = 6;
8948
8949 EXPECT_EQ("int i =\n"
8950 " longFunction(\n"
8951 " arg);",
8952 format("int i = longFunction(arg);", SixIndent));
8953 }
8954
TEST_F(FormatTest,SpacesInAngles)8955 TEST_F(FormatTest, SpacesInAngles) {
8956 FormatStyle Spaces = getLLVMStyle();
8957 Spaces.SpacesInAngles = true;
8958
8959 verifyFormat("static_cast< int >(arg);", Spaces);
8960 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
8961 verifyFormat("f< int, float >();", Spaces);
8962 verifyFormat("template <> g() {}", Spaces);
8963 verifyFormat("template < std::vector< int > > f() {}", Spaces);
8964
8965 Spaces.Standard = FormatStyle::LS_Cpp03;
8966 Spaces.SpacesInAngles = true;
8967 verifyFormat("A< A< int > >();", Spaces);
8968
8969 Spaces.SpacesInAngles = false;
8970 verifyFormat("A<A<int> >();", Spaces);
8971
8972 Spaces.Standard = FormatStyle::LS_Cpp11;
8973 Spaces.SpacesInAngles = true;
8974 verifyFormat("A< A< int > >();", Spaces);
8975
8976 Spaces.SpacesInAngles = false;
8977 verifyFormat("A<A<int>>();", Spaces);
8978 }
8979
TEST_F(FormatTest,HandleUnbalancedImplicitBracesAcrossPPBranches)8980 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
8981 std::string code = "#if A\n"
8982 "#if B\n"
8983 "a.\n"
8984 "#endif\n"
8985 " a = 1;\n"
8986 "#else\n"
8987 "#endif\n"
8988 "#if C\n"
8989 "#else\n"
8990 "#endif\n";
8991 EXPECT_EQ(code, format(code));
8992 }
8993
TEST_F(FormatTest,HandleConflictMarkers)8994 TEST_F(FormatTest, HandleConflictMarkers) {
8995 // Git/SVN conflict markers.
8996 EXPECT_EQ("int a;\n"
8997 "void f() {\n"
8998 " callme(some(parameter1,\n"
8999 "<<<<<<< text by the vcs\n"
9000 " parameter2),\n"
9001 "||||||| text by the vcs\n"
9002 " parameter2),\n"
9003 " parameter3,\n"
9004 "======= text by the vcs\n"
9005 " parameter2, parameter3),\n"
9006 ">>>>>>> text by the vcs\n"
9007 " otherparameter);\n",
9008 format("int a;\n"
9009 "void f() {\n"
9010 " callme(some(parameter1,\n"
9011 "<<<<<<< text by the vcs\n"
9012 " parameter2),\n"
9013 "||||||| text by the vcs\n"
9014 " parameter2),\n"
9015 " parameter3,\n"
9016 "======= text by the vcs\n"
9017 " parameter2,\n"
9018 " parameter3),\n"
9019 ">>>>>>> text by the vcs\n"
9020 " otherparameter);\n"));
9021
9022 // Perforce markers.
9023 EXPECT_EQ("void f() {\n"
9024 " function(\n"
9025 ">>>> text by the vcs\n"
9026 " parameter,\n"
9027 "==== text by the vcs\n"
9028 " parameter,\n"
9029 "==== text by the vcs\n"
9030 " parameter,\n"
9031 "<<<< text by the vcs\n"
9032 " parameter);\n",
9033 format("void f() {\n"
9034 " function(\n"
9035 ">>>> text by the vcs\n"
9036 " parameter,\n"
9037 "==== text by the vcs\n"
9038 " parameter,\n"
9039 "==== text by the vcs\n"
9040 " parameter,\n"
9041 "<<<< text by the vcs\n"
9042 " parameter);\n"));
9043
9044 EXPECT_EQ("<<<<<<<\n"
9045 "|||||||\n"
9046 "=======\n"
9047 ">>>>>>>",
9048 format("<<<<<<<\n"
9049 "|||||||\n"
9050 "=======\n"
9051 ">>>>>>>"));
9052
9053 EXPECT_EQ("<<<<<<<\n"
9054 "|||||||\n"
9055 "int i;\n"
9056 "=======\n"
9057 ">>>>>>>",
9058 format("<<<<<<<\n"
9059 "|||||||\n"
9060 "int i;\n"
9061 "=======\n"
9062 ">>>>>>>"));
9063
9064 // FIXME: Handle parsing of macros around conflict markers correctly:
9065 EXPECT_EQ("#define Macro \\\n"
9066 "<<<<<<<\n"
9067 "Something \\\n"
9068 "|||||||\n"
9069 "Else \\\n"
9070 "=======\n"
9071 "Other \\\n"
9072 ">>>>>>>\n"
9073 " End int i;\n",
9074 format("#define Macro \\\n"
9075 "<<<<<<<\n"
9076 " Something \\\n"
9077 "|||||||\n"
9078 " Else \\\n"
9079 "=======\n"
9080 " Other \\\n"
9081 ">>>>>>>\n"
9082 " End\n"
9083 "int i;\n"));
9084 }
9085
9086 } // end namespace tooling
9087 } // end namespace clang
9088