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 /// \brief Verify that clang-format does not crash on the given input.
verifyNoCrash(llvm::StringRef Code,const FormatStyle & Style=getLLVMStyle ())71 void verifyNoCrash(llvm::StringRef Code,
72 const FormatStyle &Style = getLLVMStyle()) {
73 format(Code, Style);
74 }
75
76 int ReplacementCount;
77 };
78
TEST_F(FormatTest,MessUp)79 TEST_F(FormatTest, MessUp) {
80 EXPECT_EQ("1 2 3", test::messUp("1 2 3"));
81 EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n"));
82 EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc"));
83 EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc"));
84 EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne"));
85 }
86
87 //===----------------------------------------------------------------------===//
88 // Basic function tests.
89 //===----------------------------------------------------------------------===//
90
TEST_F(FormatTest,DoesNotChangeCorrectlyFormattedCode)91 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) {
92 EXPECT_EQ(";", format(";"));
93 }
94
TEST_F(FormatTest,FormatsGlobalStatementsAt0)95 TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
96 EXPECT_EQ("int i;", format(" int i;"));
97 EXPECT_EQ("\nint i;", format(" \n\t \v \f int i;"));
98 EXPECT_EQ("int i;\nint j;", format(" int i; int j;"));
99 EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;"));
100 }
101
TEST_F(FormatTest,FormatsUnwrappedLinesAtFirstFormat)102 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
103 EXPECT_EQ("int i;", format("int\ni;"));
104 }
105
TEST_F(FormatTest,FormatsNestedBlockStatements)106 TEST_F(FormatTest, FormatsNestedBlockStatements) {
107 EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}"));
108 }
109
TEST_F(FormatTest,FormatsNestedCall)110 TEST_F(FormatTest, FormatsNestedCall) {
111 verifyFormat("Method(f1, f2(f3));");
112 verifyFormat("Method(f1(f2, f3()));");
113 verifyFormat("Method(f1(f2, (f3())));");
114 }
115
TEST_F(FormatTest,NestedNameSpecifiers)116 TEST_F(FormatTest, NestedNameSpecifiers) {
117 verifyFormat("vector<::Type> v;");
118 verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())");
119 verifyFormat("static constexpr bool Bar = decltype(bar())::value;");
120 verifyFormat("bool a = 2 < ::SomeFunction();");
121 }
122
TEST_F(FormatTest,OnlyGeneratesNecessaryReplacements)123 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) {
124 EXPECT_EQ("if (a) {\n"
125 " f();\n"
126 "}",
127 format("if(a){f();}"));
128 EXPECT_EQ(4, ReplacementCount);
129 EXPECT_EQ("if (a) {\n"
130 " f();\n"
131 "}",
132 format("if (a) {\n"
133 " f();\n"
134 "}"));
135 EXPECT_EQ(0, ReplacementCount);
136 }
137
TEST_F(FormatTest,RemovesTrailingWhitespaceOfFormattedLine)138 TEST_F(FormatTest, RemovesTrailingWhitespaceOfFormattedLine) {
139 EXPECT_EQ("int a;\nint b;", format("int a; \nint b;", 0, 0, getLLVMStyle()));
140 EXPECT_EQ("int a;", format("int a; "));
141 EXPECT_EQ("int a;\n", format("int a; \n \n \n "));
142 EXPECT_EQ("int a;\nint b; ",
143 format("int a; \nint b; ", 0, 0, getLLVMStyle()));
144 }
145
TEST_F(FormatTest,FormatsCorrectRegionForLeadingWhitespace)146 TEST_F(FormatTest, FormatsCorrectRegionForLeadingWhitespace) {
147 EXPECT_EQ("int b;\nint a;",
148 format("int b;\n int a;", 7, 0, getLLVMStyle()));
149 EXPECT_EQ("int b;\n int a;",
150 format("int b;\n int a;", 6, 0, getLLVMStyle()));
151
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 26, 0, getLLVMStyleWithColumns(12)));
159 EXPECT_EQ("#define A \\\n"
160 " int a; \\\n"
161 " int b;",
162 format("#define A \\\n"
163 " int a; \\\n"
164 " int b;",
165 25, 0, getLLVMStyleWithColumns(12)));
166 }
167
TEST_F(FormatTest,FormatLineWhenInvokedOnTrailingNewline)168 TEST_F(FormatTest, FormatLineWhenInvokedOnTrailingNewline) {
169 EXPECT_EQ("int b;\n\nint a;",
170 format("int b;\n\nint a;", 8, 0, getLLVMStyle()));
171 EXPECT_EQ("int b;\n\nint a;",
172 format("int b;\n\nint a;", 7, 0, getLLVMStyle()));
173
174 // This might not strictly be correct, but is likely good in all practical
175 // cases.
176 EXPECT_EQ("int b;\nint a;",
177 format("int b;int a;", 7, 0, getLLVMStyle()));
178 }
179
TEST_F(FormatTest,RemovesWhitespaceWhenTriggeredOnEmptyLine)180 TEST_F(FormatTest, RemovesWhitespaceWhenTriggeredOnEmptyLine) {
181 EXPECT_EQ("int a;\n\n int b;",
182 format("int a;\n \n\n int b;", 8, 0, getLLVMStyle()));
183 EXPECT_EQ("int a;\n\n int b;",
184 format("int a;\n \n\n int b;", 9, 0, getLLVMStyle()));
185 }
186
TEST_F(FormatTest,RemovesEmptyLines)187 TEST_F(FormatTest, RemovesEmptyLines) {
188 EXPECT_EQ("class C {\n"
189 " int i;\n"
190 "};",
191 format("class C {\n"
192 " int i;\n"
193 "\n"
194 "};"));
195
196 // Don't remove empty lines at the start of namespaces or extern "C" blocks.
197 EXPECT_EQ("namespace N {\n"
198 "\n"
199 "int i;\n"
200 "}",
201 format("namespace N {\n"
202 "\n"
203 "int i;\n"
204 "}",
205 getGoogleStyle()));
206 EXPECT_EQ("extern /**/ \"C\" /**/ {\n"
207 "\n"
208 "int i;\n"
209 "}",
210 format("extern /**/ \"C\" /**/ {\n"
211 "\n"
212 "int i;\n"
213 "}",
214 getGoogleStyle()));
215
216 // ...but do keep inlining and removing empty lines for non-block extern "C"
217 // functions.
218 verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle());
219 EXPECT_EQ("extern \"C\" int f() {\n"
220 " int i = 42;\n"
221 " return i;\n"
222 "}",
223 format("extern \"C\" int f() {\n"
224 "\n"
225 " int i = 42;\n"
226 " return i;\n"
227 "}",
228 getGoogleStyle()));
229
230 // Remove empty lines at the beginning and end of blocks.
231 EXPECT_EQ("void f() {\n"
232 "\n"
233 " if (a) {\n"
234 "\n"
235 " f();\n"
236 " }\n"
237 "}",
238 format("void f() {\n"
239 "\n"
240 " if (a) {\n"
241 "\n"
242 " f();\n"
243 "\n"
244 " }\n"
245 "\n"
246 "}",
247 getLLVMStyle()));
248 EXPECT_EQ("void f() {\n"
249 " if (a) {\n"
250 " f();\n"
251 " }\n"
252 "}",
253 format("void f() {\n"
254 "\n"
255 " if (a) {\n"
256 "\n"
257 " f();\n"
258 "\n"
259 " }\n"
260 "\n"
261 "}",
262 getGoogleStyle()));
263
264 // Don't remove empty lines in more complex control statements.
265 EXPECT_EQ("void f() {\n"
266 " if (a) {\n"
267 " f();\n"
268 "\n"
269 " } else if (b) {\n"
270 " f();\n"
271 " }\n"
272 "}",
273 format("void f() {\n"
274 " if (a) {\n"
275 " f();\n"
276 "\n"
277 " } else if (b) {\n"
278 " f();\n"
279 "\n"
280 " }\n"
281 "\n"
282 "}"));
283
284 // FIXME: This is slightly inconsistent.
285 EXPECT_EQ("namespace {\n"
286 "int i;\n"
287 "}",
288 format("namespace {\n"
289 "int i;\n"
290 "\n"
291 "}"));
292 EXPECT_EQ("namespace {\n"
293 "int i;\n"
294 "\n"
295 "} // namespace",
296 format("namespace {\n"
297 "int i;\n"
298 "\n"
299 "} // namespace"));
300 }
301
TEST_F(FormatTest,ReformatsMovedLines)302 TEST_F(FormatTest, ReformatsMovedLines) {
303 EXPECT_EQ(
304 "template <typename T> T *getFETokenInfo() const {\n"
305 " return static_cast<T *>(FETokenInfo);\n"
306 "}\n"
307 " int a; // <- Should not be formatted",
308 format(
309 "template<typename T>\n"
310 "T *getFETokenInfo() const { return static_cast<T*>(FETokenInfo); }\n"
311 " int a; // <- Should not be formatted",
312 9, 5, getLLVMStyle()));
313 }
314
TEST_F(FormatTest,RecognizesBinaryOperatorKeywords)315 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) {
316 verifyFormat("x = (a) and (b);");
317 verifyFormat("x = (a) or (b);");
318 verifyFormat("x = (a) bitand (b);");
319 verifyFormat("x = (a) bitor (b);");
320 verifyFormat("x = (a) not_eq (b);");
321 verifyFormat("x = (a) and_eq (b);");
322 verifyFormat("x = (a) or_eq (b);");
323 verifyFormat("x = (a) xor (b);");
324 }
325
326 //===----------------------------------------------------------------------===//
327 // Tests for control statements.
328 //===----------------------------------------------------------------------===//
329
TEST_F(FormatTest,FormatIfWithoutCompoundStatement)330 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
331 verifyFormat("if (true)\n f();\ng();");
332 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();");
333 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();");
334
335 FormatStyle AllowsMergedIf = getLLVMStyle();
336 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
337 verifyFormat("if (a)\n"
338 " // comment\n"
339 " f();",
340 AllowsMergedIf);
341 verifyFormat("if (a)\n"
342 " ;",
343 AllowsMergedIf);
344 verifyFormat("if (a)\n"
345 " if (b) return;",
346 AllowsMergedIf);
347
348 verifyFormat("if (a) // Can't merge this\n"
349 " f();\n",
350 AllowsMergedIf);
351 verifyFormat("if (a) /* still don't merge */\n"
352 " f();",
353 AllowsMergedIf);
354 verifyFormat("if (a) { // Never merge this\n"
355 " f();\n"
356 "}",
357 AllowsMergedIf);
358 verifyFormat("if (a) {/* Never merge this */\n"
359 " f();\n"
360 "}",
361 AllowsMergedIf);
362
363 EXPECT_EQ("if (a) return;", format("if(a)\nreturn;", 7, 1, AllowsMergedIf));
364 EXPECT_EQ("if (a) return; // comment",
365 format("if(a)\nreturn; // comment", 20, 1, AllowsMergedIf));
366
367 AllowsMergedIf.ColumnLimit = 14;
368 verifyFormat("if (a) return;", AllowsMergedIf);
369 verifyFormat("if (aaaaaaaaa)\n"
370 " return;",
371 AllowsMergedIf);
372
373 AllowsMergedIf.ColumnLimit = 13;
374 verifyFormat("if (a)\n return;", AllowsMergedIf);
375 }
376
TEST_F(FormatTest,FormatLoopsWithoutCompoundStatement)377 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
378 FormatStyle AllowsMergedLoops = getLLVMStyle();
379 AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
380 verifyFormat("while (true) continue;", AllowsMergedLoops);
381 verifyFormat("for (;;) continue;", AllowsMergedLoops);
382 verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
383 verifyFormat("while (true)\n"
384 " ;",
385 AllowsMergedLoops);
386 verifyFormat("for (;;)\n"
387 " ;",
388 AllowsMergedLoops);
389 verifyFormat("for (;;)\n"
390 " for (;;) continue;",
391 AllowsMergedLoops);
392 verifyFormat("for (;;) // Can't merge this\n"
393 " continue;",
394 AllowsMergedLoops);
395 verifyFormat("for (;;) /* still don't merge */\n"
396 " continue;",
397 AllowsMergedLoops);
398 }
399
TEST_F(FormatTest,FormatShortBracedStatements)400 TEST_F(FormatTest, FormatShortBracedStatements) {
401 FormatStyle AllowSimpleBracedStatements = getLLVMStyle();
402 AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true;
403
404 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true;
405 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
406
407 verifyFormat("if (true) {}", AllowSimpleBracedStatements);
408 verifyFormat("while (true) {}", AllowSimpleBracedStatements);
409 verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
410 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
411 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
412 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
413 verifyFormat("if (true) { //\n"
414 " f();\n"
415 "}",
416 AllowSimpleBracedStatements);
417 verifyFormat("if (true) {\n"
418 " f();\n"
419 " f();\n"
420 "}",
421 AllowSimpleBracedStatements);
422
423 verifyFormat("template <int> struct A2 {\n"
424 " struct B {};\n"
425 "};",
426 AllowSimpleBracedStatements);
427
428 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false;
429 verifyFormat("if (true) {\n"
430 " f();\n"
431 "}",
432 AllowSimpleBracedStatements);
433
434 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
435 verifyFormat("while (true) {\n"
436 " f();\n"
437 "}",
438 AllowSimpleBracedStatements);
439 verifyFormat("for (;;) {\n"
440 " f();\n"
441 "}",
442 AllowSimpleBracedStatements);
443 }
444
TEST_F(FormatTest,ParseIfElse)445 TEST_F(FormatTest, ParseIfElse) {
446 verifyFormat("if (true)\n"
447 " if (true)\n"
448 " if (true)\n"
449 " f();\n"
450 " else\n"
451 " g();\n"
452 " else\n"
453 " h();\n"
454 "else\n"
455 " i();");
456 verifyFormat("if (true)\n"
457 " if (true)\n"
458 " if (true) {\n"
459 " if (true)\n"
460 " f();\n"
461 " } else {\n"
462 " g();\n"
463 " }\n"
464 " else\n"
465 " h();\n"
466 "else {\n"
467 " i();\n"
468 "}");
469 verifyFormat("void f() {\n"
470 " if (a) {\n"
471 " } else {\n"
472 " }\n"
473 "}");
474 }
475
TEST_F(FormatTest,ElseIf)476 TEST_F(FormatTest, ElseIf) {
477 verifyFormat("if (a) {\n} else if (b) {\n}");
478 verifyFormat("if (a)\n"
479 " f();\n"
480 "else if (b)\n"
481 " g();\n"
482 "else\n"
483 " h();");
484 verifyFormat("if (a) {\n"
485 " f();\n"
486 "}\n"
487 "// or else ..\n"
488 "else {\n"
489 " g()\n"
490 "}");
491
492 verifyFormat("if (a) {\n"
493 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
494 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
495 "}");
496 verifyFormat("if (a) {\n"
497 "} else if (\n"
498 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
499 "}",
500 getLLVMStyleWithColumns(62));
501 }
502
TEST_F(FormatTest,FormatsForLoop)503 TEST_F(FormatTest, FormatsForLoop) {
504 verifyFormat(
505 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
506 " ++VeryVeryLongLoopVariable)\n"
507 " ;");
508 verifyFormat("for (;;)\n"
509 " f();");
510 verifyFormat("for (;;) {\n}");
511 verifyFormat("for (;;) {\n"
512 " f();\n"
513 "}");
514 verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
515
516 verifyFormat(
517 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
518 " E = UnwrappedLines.end();\n"
519 " I != E; ++I) {\n}");
520
521 verifyFormat(
522 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
523 " ++IIIII) {\n}");
524 verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
525 " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
526 " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
527 verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
528 " I = FD->getDeclsInPrototypeScope().begin(),\n"
529 " E = FD->getDeclsInPrototypeScope().end();\n"
530 " I != E; ++I) {\n}");
531
532 verifyFormat(
533 "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
534 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
535 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
536 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
537 " ++aaaaaaaaaaa) {\n}");
538 verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
539 " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n"
540 " ++i) {\n}");
541 verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
542 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
543 "}");
544 verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
545 " aaaaaaaaaa);\n"
546 " iter; ++iter) {\n"
547 "}");
548
549 FormatStyle NoBinPacking = getLLVMStyle();
550 NoBinPacking.BinPackParameters = false;
551 verifyFormat("for (int aaaaaaaaaaa = 1;\n"
552 " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
553 " aaaaaaaaaaaaaaaa,\n"
554 " aaaaaaaaaaaaaaaa,\n"
555 " aaaaaaaaaaaaaaaa);\n"
556 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
557 "}",
558 NoBinPacking);
559 verifyFormat(
560 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
561 " E = UnwrappedLines.end();\n"
562 " I != E;\n"
563 " ++I) {\n}",
564 NoBinPacking);
565 }
566
TEST_F(FormatTest,RangeBasedForLoops)567 TEST_F(FormatTest, RangeBasedForLoops) {
568 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
569 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
570 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
571 " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
572 verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
573 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
574 verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
575 " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
576 }
577
TEST_F(FormatTest,ForEachLoops)578 TEST_F(FormatTest, ForEachLoops) {
579 verifyFormat("void f() {\n"
580 " foreach (Item *item, itemlist) {}\n"
581 " Q_FOREACH (Item *item, itemlist) {}\n"
582 " BOOST_FOREACH (Item *item, itemlist) {}\n"
583 " UNKNOWN_FORACH(Item * item, itemlist) {}\n"
584 "}");
585 }
586
TEST_F(FormatTest,FormatsWhileLoop)587 TEST_F(FormatTest, FormatsWhileLoop) {
588 verifyFormat("while (true) {\n}");
589 verifyFormat("while (true)\n"
590 " f();");
591 verifyFormat("while () {\n}");
592 verifyFormat("while () {\n"
593 " f();\n"
594 "}");
595 }
596
TEST_F(FormatTest,FormatsDoWhile)597 TEST_F(FormatTest, FormatsDoWhile) {
598 verifyFormat("do {\n"
599 " do_something();\n"
600 "} while (something());");
601 verifyFormat("do\n"
602 " do_something();\n"
603 "while (something());");
604 }
605
TEST_F(FormatTest,FormatsSwitchStatement)606 TEST_F(FormatTest, FormatsSwitchStatement) {
607 verifyFormat("switch (x) {\n"
608 "case 1:\n"
609 " f();\n"
610 " break;\n"
611 "case kFoo:\n"
612 "case ns::kBar:\n"
613 "case kBaz:\n"
614 " break;\n"
615 "default:\n"
616 " g();\n"
617 " break;\n"
618 "}");
619 verifyFormat("switch (x) {\n"
620 "case 1: {\n"
621 " f();\n"
622 " break;\n"
623 "}\n"
624 "case 2: {\n"
625 " break;\n"
626 "}\n"
627 "}");
628 verifyFormat("switch (x) {\n"
629 "case 1: {\n"
630 " f();\n"
631 " {\n"
632 " g();\n"
633 " h();\n"
634 " }\n"
635 " break;\n"
636 "}\n"
637 "}");
638 verifyFormat("switch (x) {\n"
639 "case 1: {\n"
640 " f();\n"
641 " if (foo) {\n"
642 " g();\n"
643 " h();\n"
644 " }\n"
645 " break;\n"
646 "}\n"
647 "}");
648 verifyFormat("switch (x) {\n"
649 "case 1: {\n"
650 " f();\n"
651 " g();\n"
652 "} break;\n"
653 "}");
654 verifyFormat("switch (test)\n"
655 " ;");
656 verifyFormat("switch (x) {\n"
657 "default: {\n"
658 " // Do nothing.\n"
659 "}\n"
660 "}");
661 verifyFormat("switch (x) {\n"
662 "// comment\n"
663 "// if 1, do f()\n"
664 "case 1:\n"
665 " f();\n"
666 "}");
667 verifyFormat("switch (x) {\n"
668 "case 1:\n"
669 " // Do amazing stuff\n"
670 " {\n"
671 " f();\n"
672 " g();\n"
673 " }\n"
674 " break;\n"
675 "}");
676 verifyFormat("#define A \\\n"
677 " switch (x) { \\\n"
678 " case a: \\\n"
679 " foo = b; \\\n"
680 " }", getLLVMStyleWithColumns(20));
681 verifyFormat("#define OPERATION_CASE(name) \\\n"
682 " case OP_name: \\\n"
683 " return operations::Operation##name\n",
684 getLLVMStyleWithColumns(40));
685
686 verifyGoogleFormat("switch (x) {\n"
687 " case 1:\n"
688 " f();\n"
689 " break;\n"
690 " case kFoo:\n"
691 " case ns::kBar:\n"
692 " case kBaz:\n"
693 " break;\n"
694 " default:\n"
695 " g();\n"
696 " break;\n"
697 "}");
698 verifyGoogleFormat("switch (x) {\n"
699 " case 1: {\n"
700 " f();\n"
701 " break;\n"
702 " }\n"
703 "}");
704 verifyGoogleFormat("switch (test)\n"
705 " ;");
706
707 verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
708 " case OP_name: \\\n"
709 " return operations::Operation##name\n");
710 verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
711 " // Get the correction operation class.\n"
712 " switch (OpCode) {\n"
713 " CASE(Add);\n"
714 " CASE(Subtract);\n"
715 " default:\n"
716 " return operations::Unknown;\n"
717 " }\n"
718 "#undef OPERATION_CASE\n"
719 "}");
720 verifyFormat("DEBUG({\n"
721 " switch (x) {\n"
722 " case A:\n"
723 " f();\n"
724 " break;\n"
725 " // On B:\n"
726 " case B:\n"
727 " g();\n"
728 " break;\n"
729 " }\n"
730 "});");
731 verifyFormat("switch (a) {\n"
732 "case (b):\n"
733 " return;\n"
734 "}");
735
736 verifyFormat("switch (a) {\n"
737 "case some_namespace::\n"
738 " some_constant:\n"
739 " return;\n"
740 "}",
741 getLLVMStyleWithColumns(34));
742 }
743
TEST_F(FormatTest,CaseRanges)744 TEST_F(FormatTest, CaseRanges) {
745 verifyFormat("switch (x) {\n"
746 "case 'A' ... 'Z':\n"
747 "case 1 ... 5:\n"
748 " break;\n"
749 "}");
750 }
751
TEST_F(FormatTest,ShortCaseLabels)752 TEST_F(FormatTest, ShortCaseLabels) {
753 FormatStyle Style = getLLVMStyle();
754 Style.AllowShortCaseLabelsOnASingleLine = true;
755 verifyFormat("switch (a) {\n"
756 "case 1: x = 1; break;\n"
757 "case 2: return;\n"
758 "case 3:\n"
759 "case 4:\n"
760 "case 5: return;\n"
761 "case 6: // comment\n"
762 " return;\n"
763 "case 7:\n"
764 " // comment\n"
765 " return;\n"
766 "default: y = 1; break;\n"
767 "}",
768 Style);
769 verifyFormat("switch (a) {\n"
770 "#if FOO\n"
771 "case 0: return 0;\n"
772 "#endif\n"
773 "}",
774 Style);
775 verifyFormat("switch (a) {\n"
776 "case 1: {\n"
777 "}\n"
778 "case 2: {\n"
779 " return;\n"
780 "}\n"
781 "case 3: {\n"
782 " x = 1;\n"
783 " return;\n"
784 "}\n"
785 "case 4:\n"
786 " if (x)\n"
787 " return;\n"
788 "}",
789 Style);
790 Style.ColumnLimit = 21;
791 verifyFormat("switch (a) {\n"
792 "case 1: x = 1; break;\n"
793 "case 2: return;\n"
794 "case 3:\n"
795 "case 4:\n"
796 "case 5: return;\n"
797 "default:\n"
798 " y = 1;\n"
799 " break;\n"
800 "}",
801 Style);
802 }
803
TEST_F(FormatTest,FormatsLabels)804 TEST_F(FormatTest, FormatsLabels) {
805 verifyFormat("void f() {\n"
806 " some_code();\n"
807 "test_label:\n"
808 " some_other_code();\n"
809 " {\n"
810 " some_more_code();\n"
811 " another_label:\n"
812 " some_more_code();\n"
813 " }\n"
814 "}");
815 verifyFormat("{\n"
816 " some_code();\n"
817 "test_label:\n"
818 " some_other_code();\n"
819 "}");
820 }
821
822 //===----------------------------------------------------------------------===//
823 // Tests for comments.
824 //===----------------------------------------------------------------------===//
825
TEST_F(FormatTest,UnderstandsSingleLineComments)826 TEST_F(FormatTest, UnderstandsSingleLineComments) {
827 verifyFormat("//* */");
828 verifyFormat("// line 1\n"
829 "// line 2\n"
830 "void f() {}\n");
831
832 verifyFormat("void f() {\n"
833 " // Doesn't do anything\n"
834 "}");
835 verifyFormat("SomeObject\n"
836 " // Calling someFunction on SomeObject\n"
837 " .someFunction();");
838 verifyFormat("auto result = SomeObject\n"
839 " // Calling someFunction on SomeObject\n"
840 " .someFunction();");
841 verifyFormat("void f(int i, // some comment (probably for i)\n"
842 " int j, // some comment (probably for j)\n"
843 " int k); // some comment (probably for k)");
844 verifyFormat("void f(int i,\n"
845 " // some comment (probably for j)\n"
846 " int j,\n"
847 " // some comment (probably for k)\n"
848 " int k);");
849
850 verifyFormat("int i // This is a fancy variable\n"
851 " = 5; // with nicely aligned comment.");
852
853 verifyFormat("// Leading comment.\n"
854 "int a; // Trailing comment.");
855 verifyFormat("int a; // Trailing comment\n"
856 " // on 2\n"
857 " // or 3 lines.\n"
858 "int b;");
859 verifyFormat("int a; // Trailing comment\n"
860 "\n"
861 "// Leading comment.\n"
862 "int b;");
863 verifyFormat("int a; // Comment.\n"
864 " // More details.\n"
865 "int bbbb; // Another comment.");
866 verifyFormat(
867 "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
868 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // comment\n"
869 "int cccccccccccccccccccccccccccccc; // comment\n"
870 "int ddd; // looooooooooooooooooooooooong comment\n"
871 "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
872 "int bbbbbbbbbbbbbbbbbbbbb; // comment\n"
873 "int ccccccccccccccccccc; // comment");
874
875 verifyFormat("#include \"a\" // comment\n"
876 "#include \"a/b/c\" // comment");
877 verifyFormat("#include <a> // comment\n"
878 "#include <a/b/c> // comment");
879 EXPECT_EQ("#include \"a\" // comment\n"
880 "#include \"a/b/c\" // comment",
881 format("#include \\\n"
882 " \"a\" // comment\n"
883 "#include \"a/b/c\" // comment"));
884
885 verifyFormat("enum E {\n"
886 " // comment\n"
887 " VAL_A, // comment\n"
888 " VAL_B\n"
889 "};");
890
891 verifyFormat(
892 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
893 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
894 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
895 " // Comment inside a statement.\n"
896 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
897 verifyFormat("SomeFunction(a,\n"
898 " // comment\n"
899 " b + x);");
900 verifyFormat("SomeFunction(a, a,\n"
901 " // comment\n"
902 " b + x);");
903 verifyFormat(
904 "bool aaaaaaaaaaaaa = // comment\n"
905 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
906 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
907
908 verifyFormat("int aaaa; // aaaaa\n"
909 "int aa; // aaaaaaa",
910 getLLVMStyleWithColumns(20));
911
912 EXPECT_EQ("void f() { // This does something ..\n"
913 "}\n"
914 "int a; // This is unrelated",
915 format("void f() { // This does something ..\n"
916 " }\n"
917 "int a; // This is unrelated"));
918 EXPECT_EQ("class C {\n"
919 " void f() { // This does something ..\n"
920 " } // awesome..\n"
921 "\n"
922 " int a; // This is unrelated\n"
923 "};",
924 format("class C{void f() { // This does something ..\n"
925 " } // awesome..\n"
926 " \n"
927 "int a; // This is unrelated\n"
928 "};"));
929
930 EXPECT_EQ("int i; // single line trailing comment",
931 format("int i;\\\n// single line trailing comment"));
932
933 verifyGoogleFormat("int a; // Trailing comment.");
934
935 verifyFormat("someFunction(anotherFunction( // Force break.\n"
936 " parameter));");
937
938 verifyGoogleFormat("#endif // HEADER_GUARD");
939
940 verifyFormat("const char *test[] = {\n"
941 " // A\n"
942 " \"aaaa\",\n"
943 " // B\n"
944 " \"aaaaa\"};");
945 verifyGoogleFormat(
946 "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
947 " aaaaaaaaaaaaaaaaaaaaaa); // 81_cols_with_this_comment");
948 EXPECT_EQ("D(a, {\n"
949 " // test\n"
950 " int a;\n"
951 "});",
952 format("D(a, {\n"
953 "// test\n"
954 "int a;\n"
955 "});"));
956
957 EXPECT_EQ("lineWith(); // comment\n"
958 "// at start\n"
959 "otherLine();",
960 format("lineWith(); // comment\n"
961 "// at start\n"
962 "otherLine();"));
963 EXPECT_EQ("lineWith(); // comment\n"
964 " // at start\n"
965 "otherLine();",
966 format("lineWith(); // comment\n"
967 " // at start\n"
968 "otherLine();"));
969
970 EXPECT_EQ("lineWith(); // comment\n"
971 "// at start\n"
972 "otherLine(); // comment",
973 format("lineWith(); // comment\n"
974 "// at start\n"
975 "otherLine(); // comment"));
976 EXPECT_EQ("lineWith();\n"
977 "// at start\n"
978 "otherLine(); // comment",
979 format("lineWith();\n"
980 " // at start\n"
981 "otherLine(); // comment"));
982 EXPECT_EQ("// first\n"
983 "// at start\n"
984 "otherLine(); // comment",
985 format("// first\n"
986 " // at start\n"
987 "otherLine(); // comment"));
988 EXPECT_EQ("f();\n"
989 "// first\n"
990 "// at start\n"
991 "otherLine(); // comment",
992 format("f();\n"
993 "// first\n"
994 " // at start\n"
995 "otherLine(); // comment"));
996 verifyFormat("f(); // comment\n"
997 "// first\n"
998 "// at start\n"
999 "otherLine();");
1000 EXPECT_EQ("f(); // comment\n"
1001 "// first\n"
1002 "// at start\n"
1003 "otherLine();",
1004 format("f(); // comment\n"
1005 "// first\n"
1006 " // at start\n"
1007 "otherLine();"));
1008 EXPECT_EQ("f(); // comment\n"
1009 " // first\n"
1010 "// at start\n"
1011 "otherLine();",
1012 format("f(); // comment\n"
1013 " // first\n"
1014 "// at start\n"
1015 "otherLine();"));
1016 EXPECT_EQ("void f() {\n"
1017 " lineWith(); // comment\n"
1018 " // at start\n"
1019 "}",
1020 format("void f() {\n"
1021 " lineWith(); // comment\n"
1022 " // at start\n"
1023 "}"));
1024
1025 verifyFormat(
1026 "#define A \\\n"
1027 " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n"
1028 " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
1029 getLLVMStyleWithColumns(60));
1030 verifyFormat(
1031 "#define A \\\n"
1032 " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n"
1033 " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
1034 getLLVMStyleWithColumns(61));
1035
1036 verifyFormat("if ( // This is some comment\n"
1037 " x + 3) {\n"
1038 "}");
1039 EXPECT_EQ("if ( // This is some comment\n"
1040 " // spanning two lines\n"
1041 " x + 3) {\n"
1042 "}",
1043 format("if( // This is some comment\n"
1044 " // spanning two lines\n"
1045 " x + 3) {\n"
1046 "}"));
1047
1048 verifyNoCrash("/\\\n/");
1049 verifyNoCrash("/\\\n* */");
1050 }
1051
TEST_F(FormatTest,KeepsParameterWithTrailingCommentsOnTheirOwnLine)1052 TEST_F(FormatTest, KeepsParameterWithTrailingCommentsOnTheirOwnLine) {
1053 EXPECT_EQ("SomeFunction(a,\n"
1054 " b, // comment\n"
1055 " c);",
1056 format("SomeFunction(a,\n"
1057 " b, // comment\n"
1058 " c);"));
1059 EXPECT_EQ("SomeFunction(a, b,\n"
1060 " // comment\n"
1061 " c);",
1062 format("SomeFunction(a,\n"
1063 " b,\n"
1064 " // comment\n"
1065 " c);"));
1066 EXPECT_EQ("SomeFunction(a, b, // comment (unclear relation)\n"
1067 " c);",
1068 format("SomeFunction(a, b, // comment (unclear relation)\n"
1069 " c);"));
1070 EXPECT_EQ("SomeFunction(a, // comment\n"
1071 " b,\n"
1072 " c); // comment",
1073 format("SomeFunction(a, // comment\n"
1074 " b,\n"
1075 " c); // comment"));
1076 }
1077
TEST_F(FormatTest,CanFormatCommentsLocally)1078 TEST_F(FormatTest, CanFormatCommentsLocally) {
1079 EXPECT_EQ("int a; // comment\n"
1080 "int b; // comment",
1081 format("int a; // comment\n"
1082 "int b; // comment",
1083 0, 0, getLLVMStyle()));
1084 EXPECT_EQ("int a; // comment\n"
1085 " // line 2\n"
1086 "int b;",
1087 format("int a; // comment\n"
1088 " // line 2\n"
1089 "int b;",
1090 28, 0, getLLVMStyle()));
1091 EXPECT_EQ("int aaaaaa; // comment\n"
1092 "int b;\n"
1093 "int c; // unrelated comment",
1094 format("int aaaaaa; // comment\n"
1095 "int b;\n"
1096 "int c; // unrelated comment",
1097 31, 0, getLLVMStyle()));
1098
1099 EXPECT_EQ("int a; // This\n"
1100 " // is\n"
1101 " // a",
1102 format("int a; // This\n"
1103 " // is\n"
1104 " // a",
1105 0, 0, getLLVMStyle()));
1106 EXPECT_EQ("int a; // This\n"
1107 " // is\n"
1108 " // a\n"
1109 "// This is b\n"
1110 "int b;",
1111 format("int a; // This\n"
1112 " // is\n"
1113 " // a\n"
1114 "// This is b\n"
1115 "int b;",
1116 0, 0, getLLVMStyle()));
1117 EXPECT_EQ("int a; // This\n"
1118 " // is\n"
1119 " // a\n"
1120 "\n"
1121 " // This is unrelated",
1122 format("int a; // This\n"
1123 " // is\n"
1124 " // a\n"
1125 "\n"
1126 " // This is unrelated",
1127 0, 0, getLLVMStyle()));
1128 EXPECT_EQ("int a;\n"
1129 "// This is\n"
1130 "// not formatted. ",
1131 format("int a;\n"
1132 "// This is\n"
1133 "// not formatted. ",
1134 0, 0, getLLVMStyle()));
1135 }
1136
TEST_F(FormatTest,RemovesTrailingWhitespaceOfComments)1137 TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) {
1138 EXPECT_EQ("// comment", format("// comment "));
1139 EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment",
1140 format("int aaaaaaa, bbbbbbb; // comment ",
1141 getLLVMStyleWithColumns(33)));
1142 EXPECT_EQ("// comment\\\n", format("// comment\\\n \t \v \f "));
1143 EXPECT_EQ("// comment \\\n", format("// comment \\\n \t \v \f "));
1144 }
1145
TEST_F(FormatTest,UnderstandsBlockComments)1146 TEST_F(FormatTest, UnderstandsBlockComments) {
1147 verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);");
1148 verifyFormat("void f() { g(/*aaa=*/x, /*bbb=*/!y); }");
1149 EXPECT_EQ("f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n"
1150 " bbbbbbbbbbbbbbbbbbbbbbbbb);",
1151 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \\\n"
1152 "/* Trailing comment for aa... */\n"
1153 " bbbbbbbbbbbbbbbbbbbbbbbbb);"));
1154 EXPECT_EQ(
1155 "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1156 " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);",
1157 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n"
1158 "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);"));
1159 EXPECT_EQ(
1160 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1161 " aaaaaaaaaaaaaaaaaa,\n"
1162 " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
1163 "}",
1164 format("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1165 " aaaaaaaaaaaaaaaaaa ,\n"
1166 " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
1167 "}"));
1168
1169 FormatStyle NoBinPacking = getLLVMStyle();
1170 NoBinPacking.BinPackParameters = false;
1171 verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n"
1172 " /* parameter 2 */ aaaaaa,\n"
1173 " /* parameter 3 */ aaaaaa,\n"
1174 " /* parameter 4 */ aaaaaa);",
1175 NoBinPacking);
1176
1177 // Aligning block comments in macros.
1178 verifyGoogleFormat("#define A \\\n"
1179 " int i; /*a*/ \\\n"
1180 " int jjj; /*b*/");
1181 }
1182
TEST_F(FormatTest,AlignsBlockComments)1183 TEST_F(FormatTest, AlignsBlockComments) {
1184 EXPECT_EQ("/*\n"
1185 " * Really multi-line\n"
1186 " * comment.\n"
1187 " */\n"
1188 "void f() {}",
1189 format(" /*\n"
1190 " * Really multi-line\n"
1191 " * comment.\n"
1192 " */\n"
1193 " void f() {}"));
1194 EXPECT_EQ("class C {\n"
1195 " /*\n"
1196 " * Another multi-line\n"
1197 " * comment.\n"
1198 " */\n"
1199 " void f() {}\n"
1200 "};",
1201 format("class C {\n"
1202 "/*\n"
1203 " * Another multi-line\n"
1204 " * comment.\n"
1205 " */\n"
1206 "void f() {}\n"
1207 "};"));
1208 EXPECT_EQ("/*\n"
1209 " 1. This is a comment with non-trivial formatting.\n"
1210 " 1.1. We have to indent/outdent all lines equally\n"
1211 " 1.1.1. to keep the formatting.\n"
1212 " */",
1213 format(" /*\n"
1214 " 1. This is a comment with non-trivial formatting.\n"
1215 " 1.1. We have to indent/outdent all lines equally\n"
1216 " 1.1.1. to keep the formatting.\n"
1217 " */"));
1218 EXPECT_EQ("/*\n"
1219 "Don't try to outdent if there's not enough indentation.\n"
1220 "*/",
1221 format(" /*\n"
1222 " Don't try to outdent if there's not enough indentation.\n"
1223 " */"));
1224
1225 EXPECT_EQ("int i; /* Comment with empty...\n"
1226 " *\n"
1227 " * line. */",
1228 format("int i; /* Comment with empty...\n"
1229 " *\n"
1230 " * line. */"));
1231 EXPECT_EQ("int foobar = 0; /* comment */\n"
1232 "int bar = 0; /* multiline\n"
1233 " comment 1 */\n"
1234 "int baz = 0; /* multiline\n"
1235 " comment 2 */\n"
1236 "int bzz = 0; /* multiline\n"
1237 " comment 3 */",
1238 format("int foobar = 0; /* comment */\n"
1239 "int bar = 0; /* multiline\n"
1240 " comment 1 */\n"
1241 "int baz = 0; /* multiline\n"
1242 " comment 2 */\n"
1243 "int bzz = 0; /* multiline\n"
1244 " comment 3 */"));
1245 EXPECT_EQ("int foobar = 0; /* comment */\n"
1246 "int bar = 0; /* multiline\n"
1247 " comment */\n"
1248 "int baz = 0; /* multiline\n"
1249 "comment */",
1250 format("int foobar = 0; /* comment */\n"
1251 "int bar = 0; /* multiline\n"
1252 "comment */\n"
1253 "int baz = 0; /* multiline\n"
1254 "comment */"));
1255 }
1256
TEST_F(FormatTest,CorrectlyHandlesLengthOfBlockComments)1257 TEST_F(FormatTest, CorrectlyHandlesLengthOfBlockComments) {
1258 EXPECT_EQ("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1259 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */",
1260 format("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1261 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */"));
1262 EXPECT_EQ(
1263 "void ffffffffffff(\n"
1264 " int aaaaaaaa, int bbbbbbbb,\n"
1265 " int cccccccccccc) { /*\n"
1266 " aaaaaaaaaa\n"
1267 " aaaaaaaaaaaaa\n"
1268 " bbbbbbbbbbbbbb\n"
1269 " bbbbbbbbbb\n"
1270 " */\n"
1271 "}",
1272 format("void ffffffffffff(int aaaaaaaa, int bbbbbbbb, int cccccccccccc)\n"
1273 "{ /*\n"
1274 " aaaaaaaaaa aaaaaaaaaaaaa\n"
1275 " bbbbbbbbbbbbbb bbbbbbbbbb\n"
1276 " */\n"
1277 "}",
1278 getLLVMStyleWithColumns(40)));
1279 }
1280
TEST_F(FormatTest,DontBreakNonTrailingBlockComments)1281 TEST_F(FormatTest, DontBreakNonTrailingBlockComments) {
1282 EXPECT_EQ("void ffffffffff(\n"
1283 " int aaaaa /* test */);",
1284 format("void ffffffffff(int aaaaa /* test */);",
1285 getLLVMStyleWithColumns(35)));
1286 }
1287
TEST_F(FormatTest,SplitsLongCxxComments)1288 TEST_F(FormatTest, SplitsLongCxxComments) {
1289 EXPECT_EQ("// A comment that\n"
1290 "// doesn't fit on\n"
1291 "// one line",
1292 format("// A comment that doesn't fit on one line",
1293 getLLVMStyleWithColumns(20)));
1294 EXPECT_EQ("// a b c d\n"
1295 "// e f g\n"
1296 "// h i j k",
1297 format("// a b c d e f g h i j k",
1298 getLLVMStyleWithColumns(10)));
1299 EXPECT_EQ("// a b c d\n"
1300 "// e f g\n"
1301 "// h i j k",
1302 format("\\\n// a b c d e f g h i j k",
1303 getLLVMStyleWithColumns(10)));
1304 EXPECT_EQ("if (true) // A comment that\n"
1305 " // doesn't fit on\n"
1306 " // one line",
1307 format("if (true) // A comment that doesn't fit on one line ",
1308 getLLVMStyleWithColumns(30)));
1309 EXPECT_EQ("// Don't_touch_leading_whitespace",
1310 format("// Don't_touch_leading_whitespace",
1311 getLLVMStyleWithColumns(20)));
1312 EXPECT_EQ("// Add leading\n"
1313 "// whitespace",
1314 format("//Add leading whitespace", getLLVMStyleWithColumns(20)));
1315 EXPECT_EQ("// whitespace", format("//whitespace", getLLVMStyle()));
1316 EXPECT_EQ("// Even if it makes the line exceed the column\n"
1317 "// limit",
1318 format("//Even if it makes the line exceed the column limit",
1319 getLLVMStyleWithColumns(51)));
1320 EXPECT_EQ("//--But not here", format("//--But not here", getLLVMStyle()));
1321
1322 EXPECT_EQ("// aa bb cc dd",
1323 format("// aa bb cc dd ",
1324 getLLVMStyleWithColumns(15)));
1325
1326 EXPECT_EQ("// A comment before\n"
1327 "// a macro\n"
1328 "// definition\n"
1329 "#define a b",
1330 format("// A comment before a macro definition\n"
1331 "#define a b",
1332 getLLVMStyleWithColumns(20)));
1333 EXPECT_EQ("void ffffff(\n"
1334 " int aaaaaaaaa, // wwww\n"
1335 " int bbbbbbbbbb, // xxxxxxx\n"
1336 " // yyyyyyyyyy\n"
1337 " int c, int d, int e) {}",
1338 format("void ffffff(\n"
1339 " int aaaaaaaaa, // wwww\n"
1340 " int bbbbbbbbbb, // xxxxxxx yyyyyyyyyy\n"
1341 " int c, int d, int e) {}",
1342 getLLVMStyleWithColumns(40)));
1343 EXPECT_EQ("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
1344 format("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
1345 getLLVMStyleWithColumns(20)));
1346 EXPECT_EQ(
1347 "#define XXX // a b c d\n"
1348 " // e f g h",
1349 format("#define XXX // a b c d e f g h", getLLVMStyleWithColumns(22)));
1350 EXPECT_EQ(
1351 "#define XXX // q w e r\n"
1352 " // t y u i",
1353 format("#define XXX //q w e r t y u i", getLLVMStyleWithColumns(22)));
1354 }
1355
TEST_F(FormatTest,PreservesHangingIndentInCxxComments)1356 TEST_F(FormatTest, PreservesHangingIndentInCxxComments) {
1357 EXPECT_EQ("// A comment\n"
1358 "// that doesn't\n"
1359 "// fit on one\n"
1360 "// line",
1361 format("// A comment that doesn't fit on one line",
1362 getLLVMStyleWithColumns(20)));
1363 EXPECT_EQ("/// A comment\n"
1364 "/// that doesn't\n"
1365 "/// fit on one\n"
1366 "/// line",
1367 format("/// A comment that doesn't fit on one line",
1368 getLLVMStyleWithColumns(20)));
1369 }
1370
TEST_F(FormatTest,DontSplitLineCommentsWithEscapedNewlines)1371 TEST_F(FormatTest, DontSplitLineCommentsWithEscapedNewlines) {
1372 EXPECT_EQ("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
1373 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
1374 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
1375 format("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
1376 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
1377 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
1378 EXPECT_EQ("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1379 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1380 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
1381 format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1382 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1383 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
1384 getLLVMStyleWithColumns(50)));
1385 // FIXME: One day we might want to implement adjustment of leading whitespace
1386 // of the consecutive lines in this kind of comment:
1387 EXPECT_EQ("double\n"
1388 " a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1389 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1390 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
1391 format("double a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1392 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1393 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
1394 getLLVMStyleWithColumns(49)));
1395 }
1396
TEST_F(FormatTest,DontSplitLineCommentsWithPragmas)1397 TEST_F(FormatTest, DontSplitLineCommentsWithPragmas) {
1398 FormatStyle Pragmas = getLLVMStyleWithColumns(30);
1399 Pragmas.CommentPragmas = "^ IWYU pragma:";
1400 EXPECT_EQ(
1401 "// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb",
1402 format("// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", Pragmas));
1403 EXPECT_EQ(
1404 "/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */",
1405 format("/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", Pragmas));
1406 }
1407
TEST_F(FormatTest,PriorityOfCommentBreaking)1408 TEST_F(FormatTest, PriorityOfCommentBreaking) {
1409 EXPECT_EQ("if (xxx ==\n"
1410 " yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
1411 " zzz)\n"
1412 " q();",
1413 format("if (xxx == yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
1414 " zzz) q();",
1415 getLLVMStyleWithColumns(40)));
1416 EXPECT_EQ("if (xxxxxxxxxx ==\n"
1417 " yyy && // aaaaaa bbbbbbbb cccc\n"
1418 " zzz)\n"
1419 " q();",
1420 format("if (xxxxxxxxxx == yyy && // aaaaaa bbbbbbbb cccc\n"
1421 " zzz) q();",
1422 getLLVMStyleWithColumns(40)));
1423 EXPECT_EQ("if (xxxxxxxxxx &&\n"
1424 " yyy || // aaaaaa bbbbbbbb cccc\n"
1425 " zzz)\n"
1426 " q();",
1427 format("if (xxxxxxxxxx && yyy || // aaaaaa bbbbbbbb cccc\n"
1428 " zzz) q();",
1429 getLLVMStyleWithColumns(40)));
1430 EXPECT_EQ("fffffffff(\n"
1431 " &xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
1432 " zzz);",
1433 format("fffffffff(&xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
1434 " zzz);",
1435 getLLVMStyleWithColumns(40)));
1436 }
1437
TEST_F(FormatTest,MultiLineCommentsInDefines)1438 TEST_F(FormatTest, MultiLineCommentsInDefines) {
1439 EXPECT_EQ("#define A(x) /* \\\n"
1440 " a comment \\\n"
1441 " inside */ \\\n"
1442 " f();",
1443 format("#define A(x) /* \\\n"
1444 " a comment \\\n"
1445 " inside */ \\\n"
1446 " f();",
1447 getLLVMStyleWithColumns(17)));
1448 EXPECT_EQ("#define A( \\\n"
1449 " x) /* \\\n"
1450 " a comment \\\n"
1451 " inside */ \\\n"
1452 " f();",
1453 format("#define A( \\\n"
1454 " x) /* \\\n"
1455 " a comment \\\n"
1456 " inside */ \\\n"
1457 " f();",
1458 getLLVMStyleWithColumns(17)));
1459 }
1460
TEST_F(FormatTest,ParsesCommentsAdjacentToPPDirectives)1461 TEST_F(FormatTest, ParsesCommentsAdjacentToPPDirectives) {
1462 EXPECT_EQ("namespace {}\n// Test\n#define A",
1463 format("namespace {}\n // Test\n#define A"));
1464 EXPECT_EQ("namespace {}\n/* Test */\n#define A",
1465 format("namespace {}\n /* Test */\n#define A"));
1466 EXPECT_EQ("namespace {}\n/* Test */ #define A",
1467 format("namespace {}\n /* Test */ #define A"));
1468 }
1469
TEST_F(FormatTest,SplitsLongLinesInComments)1470 TEST_F(FormatTest, SplitsLongLinesInComments) {
1471 EXPECT_EQ("/* This is a long\n"
1472 " * comment that\n"
1473 " * doesn't\n"
1474 " * fit on one line.\n"
1475 " */",
1476 format("/* "
1477 "This is a long "
1478 "comment that "
1479 "doesn't "
1480 "fit on one line. */",
1481 getLLVMStyleWithColumns(20)));
1482 EXPECT_EQ("/* a b c d\n"
1483 " * e f g\n"
1484 " * h i j k\n"
1485 " */",
1486 format("/* a b c d e f g h i j k */",
1487 getLLVMStyleWithColumns(10)));
1488 EXPECT_EQ("/* a b c d\n"
1489 " * e f g\n"
1490 " * h i j k\n"
1491 " */",
1492 format("\\\n/* a b c d e f g h i j k */",
1493 getLLVMStyleWithColumns(10)));
1494 EXPECT_EQ("/*\n"
1495 "This is a long\n"
1496 "comment that doesn't\n"
1497 "fit on one line.\n"
1498 "*/",
1499 format("/*\n"
1500 "This is a long "
1501 "comment that doesn't "
1502 "fit on one line. \n"
1503 "*/", getLLVMStyleWithColumns(20)));
1504 EXPECT_EQ("/*\n"
1505 " * This is a long\n"
1506 " * comment that\n"
1507 " * doesn't fit on\n"
1508 " * one line.\n"
1509 " */",
1510 format("/* \n"
1511 " * This is a long "
1512 " comment that "
1513 " doesn't fit on "
1514 " one line. \n"
1515 " */", getLLVMStyleWithColumns(20)));
1516 EXPECT_EQ("/*\n"
1517 " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n"
1518 " * so_it_should_be_broken\n"
1519 " * wherever_a_space_occurs\n"
1520 " */",
1521 format("/*\n"
1522 " * This_is_a_comment_with_words_that_dont_fit_on_one_line "
1523 " so_it_should_be_broken "
1524 " wherever_a_space_occurs \n"
1525 " */",
1526 getLLVMStyleWithColumns(20)));
1527 EXPECT_EQ("/*\n"
1528 " * This_comment_can_not_be_broken_into_lines\n"
1529 " */",
1530 format("/*\n"
1531 " * This_comment_can_not_be_broken_into_lines\n"
1532 " */",
1533 getLLVMStyleWithColumns(20)));
1534 EXPECT_EQ("{\n"
1535 " /*\n"
1536 " This is another\n"
1537 " long comment that\n"
1538 " doesn't fit on one\n"
1539 " line 1234567890\n"
1540 " */\n"
1541 "}",
1542 format("{\n"
1543 "/*\n"
1544 "This is another "
1545 " long comment that "
1546 " doesn't fit on one"
1547 " line 1234567890\n"
1548 "*/\n"
1549 "}", getLLVMStyleWithColumns(20)));
1550 EXPECT_EQ("{\n"
1551 " /*\n"
1552 " * This i s\n"
1553 " * another comment\n"
1554 " * t hat doesn' t\n"
1555 " * fit on one l i\n"
1556 " * n e\n"
1557 " */\n"
1558 "}",
1559 format("{\n"
1560 "/*\n"
1561 " * This i s"
1562 " another comment"
1563 " t hat doesn' t"
1564 " fit on one l i"
1565 " n e\n"
1566 " */\n"
1567 "}", getLLVMStyleWithColumns(20)));
1568 EXPECT_EQ("/*\n"
1569 " * This is a long\n"
1570 " * comment that\n"
1571 " * doesn't fit on\n"
1572 " * one line\n"
1573 " */",
1574 format(" /*\n"
1575 " * This is a long comment that doesn't fit on one line\n"
1576 " */", getLLVMStyleWithColumns(20)));
1577 EXPECT_EQ("{\n"
1578 " if (something) /* This is a\n"
1579 " long\n"
1580 " comment */\n"
1581 " ;\n"
1582 "}",
1583 format("{\n"
1584 " if (something) /* This is a long comment */\n"
1585 " ;\n"
1586 "}",
1587 getLLVMStyleWithColumns(30)));
1588
1589 EXPECT_EQ("/* A comment before\n"
1590 " * a macro\n"
1591 " * definition */\n"
1592 "#define a b",
1593 format("/* A comment before a macro definition */\n"
1594 "#define a b",
1595 getLLVMStyleWithColumns(20)));
1596
1597 EXPECT_EQ("/* some comment\n"
1598 " * a comment\n"
1599 "* that we break\n"
1600 " * another comment\n"
1601 "* we have to break\n"
1602 "* a left comment\n"
1603 " */",
1604 format(" /* some comment\n"
1605 " * a comment that we break\n"
1606 " * another comment we have to break\n"
1607 "* a left comment\n"
1608 " */",
1609 getLLVMStyleWithColumns(20)));
1610
1611 EXPECT_EQ("/*\n"
1612 "\n"
1613 "\n"
1614 " */\n",
1615 format(" /* \n"
1616 " \n"
1617 " \n"
1618 " */\n"));
1619
1620 EXPECT_EQ("/* a a */",
1621 format("/* a a */", getLLVMStyleWithColumns(15)));
1622 EXPECT_EQ("/* a a bc */",
1623 format("/* a a bc */", getLLVMStyleWithColumns(15)));
1624 EXPECT_EQ("/* aaa aaa\n"
1625 " * aaaaa */",
1626 format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15)));
1627 EXPECT_EQ("/* aaa aaa\n"
1628 " * aaaaa */",
1629 format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15)));
1630 }
1631
TEST_F(FormatTest,SplitsLongLinesInCommentsInPreprocessor)1632 TEST_F(FormatTest, SplitsLongLinesInCommentsInPreprocessor) {
1633 EXPECT_EQ("#define X \\\n"
1634 " /* \\\n"
1635 " Test \\\n"
1636 " Macro comment \\\n"
1637 " with a long \\\n"
1638 " line \\\n"
1639 " */ \\\n"
1640 " A + B",
1641 format("#define X \\\n"
1642 " /*\n"
1643 " Test\n"
1644 " Macro comment with a long line\n"
1645 " */ \\\n"
1646 " A + B",
1647 getLLVMStyleWithColumns(20)));
1648 EXPECT_EQ("#define X \\\n"
1649 " /* Macro comment \\\n"
1650 " with a long \\\n"
1651 " line */ \\\n"
1652 " A + B",
1653 format("#define X \\\n"
1654 " /* Macro comment with a long\n"
1655 " line */ \\\n"
1656 " A + B",
1657 getLLVMStyleWithColumns(20)));
1658 EXPECT_EQ("#define X \\\n"
1659 " /* Macro comment \\\n"
1660 " * with a long \\\n"
1661 " * line */ \\\n"
1662 " A + B",
1663 format("#define X \\\n"
1664 " /* Macro comment with a long line */ \\\n"
1665 " A + B",
1666 getLLVMStyleWithColumns(20)));
1667 }
1668
TEST_F(FormatTest,CommentsInStaticInitializers)1669 TEST_F(FormatTest, CommentsInStaticInitializers) {
1670 EXPECT_EQ(
1671 "static SomeType type = {aaaaaaaaaaaaaaaaaaaa, /* comment */\n"
1672 " aaaaaaaaaaaaaaaaaaaa /* comment */,\n"
1673 " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n"
1674 " aaaaaaaaaaaaaaaaaaaa, // comment\n"
1675 " aaaaaaaaaaaaaaaaaaaa};",
1676 format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n"
1677 " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n"
1678 " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n"
1679 " aaaaaaaaaaaaaaaaaaaa , // comment\n"
1680 " aaaaaaaaaaaaaaaaaaaa };"));
1681 verifyFormat("static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
1682 " bbbbbbbbbbb, ccccccccccc};");
1683 verifyFormat("static SomeType type = {aaaaaaaaaaa,\n"
1684 " // comment for bb....\n"
1685 " bbbbbbbbbbb, ccccccccccc};");
1686 verifyGoogleFormat(
1687 "static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
1688 " bbbbbbbbbbb, ccccccccccc};");
1689 verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n"
1690 " // comment for bb....\n"
1691 " bbbbbbbbbbb, ccccccccccc};");
1692
1693 verifyFormat("S s = {{a, b, c}, // Group #1\n"
1694 " {d, e, f}, // Group #2\n"
1695 " {g, h, i}}; // Group #3");
1696 verifyFormat("S s = {{// Group #1\n"
1697 " a, b, c},\n"
1698 " {// Group #2\n"
1699 " d, e, f},\n"
1700 " {// Group #3\n"
1701 " g, h, i}};");
1702
1703 EXPECT_EQ("S s = {\n"
1704 " // Some comment\n"
1705 " a,\n"
1706 "\n"
1707 " // Comment after empty line\n"
1708 " b}",
1709 format("S s = {\n"
1710 " // Some comment\n"
1711 " a,\n"
1712 " \n"
1713 " // Comment after empty line\n"
1714 " b\n"
1715 "}"));
1716 EXPECT_EQ("S s = {\n"
1717 " /* Some comment */\n"
1718 " a,\n"
1719 "\n"
1720 " /* Comment after empty line */\n"
1721 " b}",
1722 format("S s = {\n"
1723 " /* Some comment */\n"
1724 " a,\n"
1725 " \n"
1726 " /* Comment after empty line */\n"
1727 " b\n"
1728 "}"));
1729 verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n"
1730 " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
1731 " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
1732 " 0x00, 0x00, 0x00, 0x00}; // comment\n");
1733 }
1734
TEST_F(FormatTest,IgnoresIf0Contents)1735 TEST_F(FormatTest, IgnoresIf0Contents) {
1736 EXPECT_EQ("#if 0\n"
1737 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
1738 "#endif\n"
1739 "void f() {}",
1740 format("#if 0\n"
1741 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
1742 "#endif\n"
1743 "void f( ) { }"));
1744 EXPECT_EQ("#if false\n"
1745 "void f( ) { }\n"
1746 "#endif\n"
1747 "void g() {}\n",
1748 format("#if false\n"
1749 "void f( ) { }\n"
1750 "#endif\n"
1751 "void g( ) { }\n"));
1752 EXPECT_EQ("enum E {\n"
1753 " One,\n"
1754 " Two,\n"
1755 "#if 0\n"
1756 "Three,\n"
1757 " Four,\n"
1758 "#endif\n"
1759 " Five\n"
1760 "};",
1761 format("enum E {\n"
1762 " One,Two,\n"
1763 "#if 0\n"
1764 "Three,\n"
1765 " Four,\n"
1766 "#endif\n"
1767 " Five};"));
1768 EXPECT_EQ("enum F {\n"
1769 " One,\n"
1770 "#if 1\n"
1771 " Two,\n"
1772 "#if 0\n"
1773 "Three,\n"
1774 " Four,\n"
1775 "#endif\n"
1776 " Five\n"
1777 "#endif\n"
1778 "};",
1779 format("enum F {\n"
1780 "One,\n"
1781 "#if 1\n"
1782 "Two,\n"
1783 "#if 0\n"
1784 "Three,\n"
1785 " Four,\n"
1786 "#endif\n"
1787 "Five\n"
1788 "#endif\n"
1789 "};"));
1790 EXPECT_EQ("enum G {\n"
1791 " One,\n"
1792 "#if 0\n"
1793 "Two,\n"
1794 "#else\n"
1795 " Three,\n"
1796 "#endif\n"
1797 " Four\n"
1798 "};",
1799 format("enum G {\n"
1800 "One,\n"
1801 "#if 0\n"
1802 "Two,\n"
1803 "#else\n"
1804 "Three,\n"
1805 "#endif\n"
1806 "Four\n"
1807 "};"));
1808 EXPECT_EQ("enum H {\n"
1809 " One,\n"
1810 "#if 0\n"
1811 "#ifdef Q\n"
1812 "Two,\n"
1813 "#else\n"
1814 "Three,\n"
1815 "#endif\n"
1816 "#endif\n"
1817 " Four\n"
1818 "};",
1819 format("enum H {\n"
1820 "One,\n"
1821 "#if 0\n"
1822 "#ifdef Q\n"
1823 "Two,\n"
1824 "#else\n"
1825 "Three,\n"
1826 "#endif\n"
1827 "#endif\n"
1828 "Four\n"
1829 "};"));
1830 EXPECT_EQ("enum I {\n"
1831 " One,\n"
1832 "#if /* test */ 0 || 1\n"
1833 "Two,\n"
1834 "Three,\n"
1835 "#endif\n"
1836 " Four\n"
1837 "};",
1838 format("enum I {\n"
1839 "One,\n"
1840 "#if /* test */ 0 || 1\n"
1841 "Two,\n"
1842 "Three,\n"
1843 "#endif\n"
1844 "Four\n"
1845 "};"));
1846 EXPECT_EQ("enum J {\n"
1847 " One,\n"
1848 "#if 0\n"
1849 "#if 0\n"
1850 "Two,\n"
1851 "#else\n"
1852 "Three,\n"
1853 "#endif\n"
1854 "Four,\n"
1855 "#endif\n"
1856 " Five\n"
1857 "};",
1858 format("enum J {\n"
1859 "One,\n"
1860 "#if 0\n"
1861 "#if 0\n"
1862 "Two,\n"
1863 "#else\n"
1864 "Three,\n"
1865 "#endif\n"
1866 "Four,\n"
1867 "#endif\n"
1868 "Five\n"
1869 "};"));
1870
1871 }
1872
1873 //===----------------------------------------------------------------------===//
1874 // Tests for classes, namespaces, etc.
1875 //===----------------------------------------------------------------------===//
1876
TEST_F(FormatTest,DoesNotBreakSemiAfterClassDecl)1877 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
1878 verifyFormat("class A {};");
1879 }
1880
TEST_F(FormatTest,UnderstandsAccessSpecifiers)1881 TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
1882 verifyFormat("class A {\n"
1883 "public:\n"
1884 "public: // comment\n"
1885 "protected:\n"
1886 "private:\n"
1887 " void f() {}\n"
1888 "};");
1889 verifyGoogleFormat("class A {\n"
1890 " public:\n"
1891 " protected:\n"
1892 " private:\n"
1893 " void f() {}\n"
1894 "};");
1895 verifyFormat("class A {\n"
1896 "public slots:\n"
1897 " void f() {}\n"
1898 "public Q_SLOTS:\n"
1899 " void f() {}\n"
1900 "signals:\n"
1901 " void g();\n"
1902 "};");
1903 }
1904
TEST_F(FormatTest,SeparatesLogicalBlocks)1905 TEST_F(FormatTest, SeparatesLogicalBlocks) {
1906 EXPECT_EQ("class A {\n"
1907 "public:\n"
1908 " void f();\n"
1909 "\n"
1910 "private:\n"
1911 " void g() {}\n"
1912 " // test\n"
1913 "protected:\n"
1914 " int h;\n"
1915 "};",
1916 format("class A {\n"
1917 "public:\n"
1918 "void f();\n"
1919 "private:\n"
1920 "void g() {}\n"
1921 "// test\n"
1922 "protected:\n"
1923 "int h;\n"
1924 "};"));
1925 EXPECT_EQ("class A {\n"
1926 "protected:\n"
1927 "public:\n"
1928 " void f();\n"
1929 "};",
1930 format("class A {\n"
1931 "protected:\n"
1932 "\n"
1933 "public:\n"
1934 "\n"
1935 " void f();\n"
1936 "};"));
1937
1938 // Even ensure proper spacing inside macros.
1939 EXPECT_EQ("#define B \\\n"
1940 " class A { \\\n"
1941 " protected: \\\n"
1942 " public: \\\n"
1943 " void f(); \\\n"
1944 " };",
1945 format("#define B \\\n"
1946 " class A { \\\n"
1947 " protected: \\\n"
1948 " \\\n"
1949 " public: \\\n"
1950 " \\\n"
1951 " void f(); \\\n"
1952 " };",
1953 getGoogleStyle()));
1954 // But don't remove empty lines after macros ending in access specifiers.
1955 EXPECT_EQ("#define A private:\n"
1956 "\n"
1957 "int i;",
1958 format("#define A private:\n"
1959 "\n"
1960 "int i;"));
1961 }
1962
TEST_F(FormatTest,FormatsClasses)1963 TEST_F(FormatTest, FormatsClasses) {
1964 verifyFormat("class A : public B {};");
1965 verifyFormat("class A : public ::B {};");
1966
1967 verifyFormat(
1968 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1969 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
1970 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
1971 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1972 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
1973 verifyFormat(
1974 "class A : public B, public C, public D, public E, public F {};");
1975 verifyFormat("class AAAAAAAAAAAA : public B,\n"
1976 " public C,\n"
1977 " public D,\n"
1978 " public E,\n"
1979 " public F,\n"
1980 " public G {};");
1981
1982 verifyFormat("class\n"
1983 " ReallyReallyLongClassName {\n"
1984 " int i;\n"
1985 "};",
1986 getLLVMStyleWithColumns(32));
1987 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
1988 " aaaaaaaaaaaaaaaa> {};");
1989 verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
1990 " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
1991 " aaaaaaaaaaaaaaaaaaaaaa> {};");
1992 verifyFormat("template <class R, class C>\n"
1993 "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
1994 " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
1995 verifyFormat("class ::A::B {};");
1996 }
1997
TEST_F(FormatTest,FormatsVariableDeclarationsAfterStructOrClass)1998 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
1999 verifyFormat("class A {\n} a, b;");
2000 verifyFormat("struct A {\n} a, b;");
2001 verifyFormat("union A {\n} a;");
2002 }
2003
TEST_F(FormatTest,FormatsEnum)2004 TEST_F(FormatTest, FormatsEnum) {
2005 verifyFormat("enum {\n"
2006 " Zero,\n"
2007 " One = 1,\n"
2008 " Two = One + 1,\n"
2009 " Three = (One + Two),\n"
2010 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
2011 " Five = (One, Two, Three, Four, 5)\n"
2012 "};");
2013 verifyGoogleFormat("enum {\n"
2014 " Zero,\n"
2015 " One = 1,\n"
2016 " Two = One + 1,\n"
2017 " Three = (One + Two),\n"
2018 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
2019 " Five = (One, Two, Three, Four, 5)\n"
2020 "};");
2021 verifyFormat("enum Enum {};");
2022 verifyFormat("enum {};");
2023 verifyFormat("enum X E {} d;");
2024 verifyFormat("enum __attribute__((...)) E {} d;");
2025 verifyFormat("enum __declspec__((...)) E {} d;");
2026 verifyFormat("enum X f() {\n a();\n return 42;\n}");
2027 verifyFormat("enum {\n"
2028 " Bar = Foo<int, int>::value\n"
2029 "};",
2030 getLLVMStyleWithColumns(30));
2031
2032 verifyFormat("enum ShortEnum { A, B, C };");
2033 verifyGoogleFormat("enum ShortEnum { A, B, C };");
2034
2035 EXPECT_EQ("enum KeepEmptyLines {\n"
2036 " ONE,\n"
2037 "\n"
2038 " TWO,\n"
2039 "\n"
2040 " THREE\n"
2041 "}",
2042 format("enum KeepEmptyLines {\n"
2043 " ONE,\n"
2044 "\n"
2045 " TWO,\n"
2046 "\n"
2047 "\n"
2048 " THREE\n"
2049 "}"));
2050 verifyFormat("enum E { // comment\n"
2051 " ONE,\n"
2052 " TWO\n"
2053 "};\n"
2054 "int i;");
2055 }
2056
TEST_F(FormatTest,FormatsEnumsWithErrors)2057 TEST_F(FormatTest, FormatsEnumsWithErrors) {
2058 verifyFormat("enum Type {\n"
2059 " One = 0; // These semicolons should be commas.\n"
2060 " Two = 1;\n"
2061 "};");
2062 verifyFormat("namespace n {\n"
2063 "enum Type {\n"
2064 " One,\n"
2065 " Two, // missing };\n"
2066 " int i;\n"
2067 "}\n"
2068 "void g() {}");
2069 }
2070
TEST_F(FormatTest,FormatsEnumStruct)2071 TEST_F(FormatTest, FormatsEnumStruct) {
2072 verifyFormat("enum struct {\n"
2073 " Zero,\n"
2074 " One = 1,\n"
2075 " Two = One + 1,\n"
2076 " Three = (One + Two),\n"
2077 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
2078 " Five = (One, Two, Three, Four, 5)\n"
2079 "};");
2080 verifyFormat("enum struct Enum {};");
2081 verifyFormat("enum struct {};");
2082 verifyFormat("enum struct X E {} d;");
2083 verifyFormat("enum struct __attribute__((...)) E {} d;");
2084 verifyFormat("enum struct __declspec__((...)) E {} d;");
2085 verifyFormat("enum struct X f() {\n a();\n return 42;\n}");
2086 }
2087
TEST_F(FormatTest,FormatsEnumClass)2088 TEST_F(FormatTest, FormatsEnumClass) {
2089 verifyFormat("enum class {\n"
2090 " Zero,\n"
2091 " One = 1,\n"
2092 " Two = One + 1,\n"
2093 " Three = (One + Two),\n"
2094 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
2095 " Five = (One, Two, Three, Four, 5)\n"
2096 "};");
2097 verifyFormat("enum class Enum {};");
2098 verifyFormat("enum class {};");
2099 verifyFormat("enum class X E {} d;");
2100 verifyFormat("enum class __attribute__((...)) E {} d;");
2101 verifyFormat("enum class __declspec__((...)) E {} d;");
2102 verifyFormat("enum class X f() {\n a();\n return 42;\n}");
2103 }
2104
TEST_F(FormatTest,FormatsEnumTypes)2105 TEST_F(FormatTest, FormatsEnumTypes) {
2106 verifyFormat("enum X : int {\n"
2107 " A, // Force multiple lines.\n"
2108 " B\n"
2109 "};");
2110 verifyFormat("enum X : int { A, B };");
2111 verifyFormat("enum X : std::uint32_t { A, B };");
2112 }
2113
TEST_F(FormatTest,FormatsNSEnums)2114 TEST_F(FormatTest, FormatsNSEnums) {
2115 verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
2116 verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
2117 " // Information about someDecentlyLongValue.\n"
2118 " someDecentlyLongValue,\n"
2119 " // Information about anotherDecentlyLongValue.\n"
2120 " anotherDecentlyLongValue,\n"
2121 " // Information about aThirdDecentlyLongValue.\n"
2122 " aThirdDecentlyLongValue\n"
2123 "};");
2124 verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
2125 " a = 1,\n"
2126 " b = 2,\n"
2127 " c = 3,\n"
2128 "};");
2129 verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
2130 " a = 1,\n"
2131 " b = 2,\n"
2132 " c = 3,\n"
2133 "};");
2134 verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
2135 " a = 1,\n"
2136 " b = 2,\n"
2137 " c = 3,\n"
2138 "};");
2139 }
2140
TEST_F(FormatTest,FormatsBitfields)2141 TEST_F(FormatTest, FormatsBitfields) {
2142 verifyFormat("struct Bitfields {\n"
2143 " unsigned sClass : 8;\n"
2144 " unsigned ValueKind : 2;\n"
2145 "};");
2146 verifyFormat("struct A {\n"
2147 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
2148 " bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
2149 "};");
2150 verifyFormat("struct MyStruct {\n"
2151 " uchar data;\n"
2152 " uchar : 8;\n"
2153 " uchar : 8;\n"
2154 " uchar other;\n"
2155 "};");
2156 }
2157
TEST_F(FormatTest,FormatsNamespaces)2158 TEST_F(FormatTest, FormatsNamespaces) {
2159 verifyFormat("namespace some_namespace {\n"
2160 "class A {};\n"
2161 "void f() { f(); }\n"
2162 "}");
2163 verifyFormat("namespace {\n"
2164 "class A {};\n"
2165 "void f() { f(); }\n"
2166 "}");
2167 verifyFormat("inline namespace X {\n"
2168 "class A {};\n"
2169 "void f() { f(); }\n"
2170 "}");
2171 verifyFormat("using namespace some_namespace;\n"
2172 "class A {};\n"
2173 "void f() { f(); }");
2174
2175 // This code is more common than we thought; if we
2176 // layout this correctly the semicolon will go into
2177 // its own line, which is undesirable.
2178 verifyFormat("namespace {};");
2179 verifyFormat("namespace {\n"
2180 "class A {};\n"
2181 "};");
2182
2183 verifyFormat("namespace {\n"
2184 "int SomeVariable = 0; // comment\n"
2185 "} // namespace");
2186 EXPECT_EQ("#ifndef HEADER_GUARD\n"
2187 "#define HEADER_GUARD\n"
2188 "namespace my_namespace {\n"
2189 "int i;\n"
2190 "} // my_namespace\n"
2191 "#endif // HEADER_GUARD",
2192 format("#ifndef HEADER_GUARD\n"
2193 " #define HEADER_GUARD\n"
2194 " namespace my_namespace {\n"
2195 "int i;\n"
2196 "} // my_namespace\n"
2197 "#endif // HEADER_GUARD"));
2198
2199 FormatStyle Style = getLLVMStyle();
2200 Style.NamespaceIndentation = FormatStyle::NI_All;
2201 EXPECT_EQ("namespace out {\n"
2202 " int i;\n"
2203 " namespace in {\n"
2204 " int i;\n"
2205 " } // namespace\n"
2206 "} // namespace",
2207 format("namespace out {\n"
2208 "int i;\n"
2209 "namespace in {\n"
2210 "int i;\n"
2211 "} // namespace\n"
2212 "} // namespace",
2213 Style));
2214
2215 Style.NamespaceIndentation = FormatStyle::NI_Inner;
2216 EXPECT_EQ("namespace out {\n"
2217 "int i;\n"
2218 "namespace in {\n"
2219 " int i;\n"
2220 "} // namespace\n"
2221 "} // namespace",
2222 format("namespace out {\n"
2223 "int i;\n"
2224 "namespace in {\n"
2225 "int i;\n"
2226 "} // namespace\n"
2227 "} // namespace",
2228 Style));
2229 }
2230
TEST_F(FormatTest,FormatsExternC)2231 TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); }
2232
TEST_F(FormatTest,FormatsInlineASM)2233 TEST_F(FormatTest, FormatsInlineASM) {
2234 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
2235 verifyFormat("asm(\"nop\" ::: \"memory\");");
2236 verifyFormat(
2237 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
2238 " \"cpuid\\n\\t\"\n"
2239 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
2240 " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
2241 " : \"a\"(value));");
2242 EXPECT_EQ(
2243 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
2244 " __asm {\n"
2245 " mov edx,[that] // vtable in edx\n"
2246 " mov eax,methodIndex\n"
2247 " call [edx][eax*4] // stdcall\n"
2248 " }\n"
2249 "}",
2250 format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
2251 " __asm {\n"
2252 " mov edx,[that] // vtable in edx\n"
2253 " mov eax,methodIndex\n"
2254 " call [edx][eax*4] // stdcall\n"
2255 " }\n"
2256 "}"));
2257 verifyFormat("void function() {\n"
2258 " // comment\n"
2259 " asm(\"\");\n"
2260 "}");
2261 }
2262
TEST_F(FormatTest,FormatTryCatch)2263 TEST_F(FormatTest, FormatTryCatch) {
2264 verifyFormat("try {\n"
2265 " throw a * b;\n"
2266 "} catch (int a) {\n"
2267 " // Do nothing.\n"
2268 "} catch (...) {\n"
2269 " exit(42);\n"
2270 "}");
2271
2272 // Function-level try statements.
2273 verifyFormat("int f() try { return 4; } catch (...) {\n"
2274 " return 5;\n"
2275 "}");
2276 verifyFormat("class A {\n"
2277 " int a;\n"
2278 " A() try : a(0) {\n"
2279 " } catch (...) {\n"
2280 " throw;\n"
2281 " }\n"
2282 "};\n");
2283
2284 // Incomplete try-catch blocks.
2285 verifyFormat("try {} catch (");
2286 }
2287
TEST_F(FormatTest,FormatSEHTryCatch)2288 TEST_F(FormatTest, FormatSEHTryCatch) {
2289 verifyFormat("__try {\n"
2290 " int a = b * c;\n"
2291 "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
2292 " // Do nothing.\n"
2293 "}");
2294
2295 verifyFormat("__try {\n"
2296 " int a = b * c;\n"
2297 "} __finally {\n"
2298 " // Do nothing.\n"
2299 "}");
2300
2301 verifyFormat("DEBUG({\n"
2302 " __try {\n"
2303 " } __finally {\n"
2304 " }\n"
2305 "});\n");
2306 }
2307
TEST_F(FormatTest,IncompleteTryCatchBlocks)2308 TEST_F(FormatTest, IncompleteTryCatchBlocks) {
2309 verifyFormat("try {\n"
2310 " f();\n"
2311 "} catch {\n"
2312 " g();\n"
2313 "}");
2314 verifyFormat("try {\n"
2315 " f();\n"
2316 "} catch (A a) MACRO(x) {\n"
2317 " g();\n"
2318 "} catch (B b) MACRO(x) {\n"
2319 " g();\n"
2320 "}");
2321 }
2322
TEST_F(FormatTest,FormatTryCatchBraceStyles)2323 TEST_F(FormatTest, FormatTryCatchBraceStyles) {
2324 FormatStyle Style = getLLVMStyle();
2325 Style.BreakBeforeBraces = FormatStyle::BS_Attach;
2326 verifyFormat("try {\n"
2327 " // something\n"
2328 "} catch (...) {\n"
2329 " // something\n"
2330 "}",
2331 Style);
2332 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
2333 verifyFormat("try {\n"
2334 " // something\n"
2335 "}\n"
2336 "catch (...) {\n"
2337 " // something\n"
2338 "}",
2339 Style);
2340 verifyFormat("__try {\n"
2341 " // something\n"
2342 "}\n"
2343 "__finally {\n"
2344 " // something\n"
2345 "}",
2346 Style);
2347 verifyFormat("@try {\n"
2348 " // something\n"
2349 "}\n"
2350 "@finally {\n"
2351 " // something\n"
2352 "}",
2353 Style);
2354 Style.BreakBeforeBraces = FormatStyle::BS_Allman;
2355 verifyFormat("try\n"
2356 "{\n"
2357 " // something\n"
2358 "}\n"
2359 "catch (...)\n"
2360 "{\n"
2361 " // something\n"
2362 "}",
2363 Style);
2364 Style.BreakBeforeBraces = FormatStyle::BS_GNU;
2365 verifyFormat("try\n"
2366 " {\n"
2367 " // something\n"
2368 " }\n"
2369 "catch (...)\n"
2370 " {\n"
2371 " // something\n"
2372 " }",
2373 Style);
2374 }
2375
TEST_F(FormatTest,FormatObjCTryCatch)2376 TEST_F(FormatTest, FormatObjCTryCatch) {
2377 verifyFormat("@try {\n"
2378 " f();\n"
2379 "} @catch (NSException e) {\n"
2380 " @throw;\n"
2381 "} @finally {\n"
2382 " exit(42);\n"
2383 "}");
2384 verifyFormat("DEBUG({\n"
2385 " @try {\n"
2386 " } @finally {\n"
2387 " }\n"
2388 "});\n");
2389 }
2390
TEST_F(FormatTest,StaticInitializers)2391 TEST_F(FormatTest, StaticInitializers) {
2392 verifyFormat("static SomeClass SC = {1, 'a'};");
2393
2394 verifyFormat(
2395 "static SomeClass WithALoooooooooooooooooooongName = {\n"
2396 " 100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
2397
2398 // Here, everything other than the "}" would fit on a line.
2399 verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
2400 " 10000000000000000000000000};");
2401 EXPECT_EQ("S s = {a,\n"
2402 "\n"
2403 " b};",
2404 format("S s = {\n"
2405 " a,\n"
2406 "\n"
2407 " b\n"
2408 "};"));
2409
2410 // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
2411 // line. However, the formatting looks a bit off and this probably doesn't
2412 // happen often in practice.
2413 verifyFormat("static int Variable[1] = {\n"
2414 " {1000000000000000000000000000000000000}};",
2415 getLLVMStyleWithColumns(40));
2416 }
2417
TEST_F(FormatTest,DesignatedInitializers)2418 TEST_F(FormatTest, DesignatedInitializers) {
2419 verifyFormat("const struct A a = {.a = 1, .b = 2};");
2420 verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
2421 " .bbbbbbbbbb = 2,\n"
2422 " .cccccccccc = 3,\n"
2423 " .dddddddddd = 4,\n"
2424 " .eeeeeeeeee = 5};");
2425 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
2426 " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
2427 " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
2428 " .ccccccccccccccccccccccccccc = 3,\n"
2429 " .ddddddddddddddddddddddddddd = 4,\n"
2430 " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
2431
2432 verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
2433 }
2434
TEST_F(FormatTest,NestedStaticInitializers)2435 TEST_F(FormatTest, NestedStaticInitializers) {
2436 verifyFormat("static A x = {{{}}};\n");
2437 verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
2438 " {init1, init2, init3, init4}}};",
2439 getLLVMStyleWithColumns(50));
2440
2441 verifyFormat("somes Status::global_reps[3] = {\n"
2442 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
2443 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
2444 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
2445 getLLVMStyleWithColumns(60));
2446 verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
2447 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
2448 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
2449 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
2450 verifyFormat(
2451 "CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
2452 " {rect.fRight - rect.fLeft, rect.fBottom - rect.fTop}};");
2453
2454 verifyFormat(
2455 "SomeArrayOfSomeType a = {\n"
2456 " {{1, 2, 3},\n"
2457 " {1, 2, 3},\n"
2458 " {111111111111111111111111111111, 222222222222222222222222222222,\n"
2459 " 333333333333333333333333333333},\n"
2460 " {1, 2, 3},\n"
2461 " {1, 2, 3}}};");
2462 verifyFormat(
2463 "SomeArrayOfSomeType a = {\n"
2464 " {{1, 2, 3}},\n"
2465 " {{1, 2, 3}},\n"
2466 " {{111111111111111111111111111111, 222222222222222222222222222222,\n"
2467 " 333333333333333333333333333333}},\n"
2468 " {{1, 2, 3}},\n"
2469 " {{1, 2, 3}}};");
2470
2471 verifyFormat(
2472 "struct {\n"
2473 " unsigned bit;\n"
2474 " const char *const name;\n"
2475 "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
2476 " {kOsWin, \"Windows\"},\n"
2477 " {kOsLinux, \"Linux\"},\n"
2478 " {kOsCrOS, \"Chrome OS\"}};");
2479 verifyFormat(
2480 "struct {\n"
2481 " unsigned bit;\n"
2482 " const char *const name;\n"
2483 "} kBitsToOs[] = {\n"
2484 " {kOsMac, \"Mac\"},\n"
2485 " {kOsWin, \"Windows\"},\n"
2486 " {kOsLinux, \"Linux\"},\n"
2487 " {kOsCrOS, \"Chrome OS\"},\n"
2488 "};");
2489 }
2490
TEST_F(FormatTest,FormatsSmallMacroDefinitionsInSingleLine)2491 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
2492 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
2493 " \\\n"
2494 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
2495 }
2496
TEST_F(FormatTest,DoesNotBreakPureVirtualFunctionDefinition)2497 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
2498 verifyFormat("virtual void write(ELFWriter *writerrr,\n"
2499 " OwningPtr<FileOutputBuffer> &buffer) = 0;");
2500 }
2501
TEST_F(FormatTest,BreaksStringLiteralsOnlyInDefine)2502 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
2503 verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
2504 getLLVMStyleWithColumns(40));
2505 verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
2506 getLLVMStyleWithColumns(40));
2507 EXPECT_EQ("#define Q \\\n"
2508 " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n"
2509 " \"aaaaaaaa.cpp\"",
2510 format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
2511 getLLVMStyleWithColumns(40)));
2512 }
2513
TEST_F(FormatTest,UnderstandsLinePPDirective)2514 TEST_F(FormatTest, UnderstandsLinePPDirective) {
2515 EXPECT_EQ("# 123 \"A string literal\"",
2516 format(" # 123 \"A string literal\""));
2517 }
2518
TEST_F(FormatTest,LayoutUnknownPPDirective)2519 TEST_F(FormatTest, LayoutUnknownPPDirective) {
2520 EXPECT_EQ("#;", format("#;"));
2521 verifyFormat("#\n;\n;\n;");
2522 }
2523
TEST_F(FormatTest,UnescapedEndOfLineEndsPPDirective)2524 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
2525 EXPECT_EQ("#line 42 \"test\"\n",
2526 format("# \\\n line \\\n 42 \\\n \"test\"\n"));
2527 EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n",
2528 getLLVMStyleWithColumns(12)));
2529 }
2530
TEST_F(FormatTest,EndOfFileEndsPPDirective)2531 TEST_F(FormatTest, EndOfFileEndsPPDirective) {
2532 EXPECT_EQ("#line 42 \"test\"",
2533 format("# \\\n line \\\n 42 \\\n \"test\""));
2534 EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B"));
2535 }
2536
TEST_F(FormatTest,DoesntRemoveUnknownTokens)2537 TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
2538 verifyFormat("#define A \\x20");
2539 verifyFormat("#define A \\ x20");
2540 EXPECT_EQ("#define A \\ x20", format("#define A \\ x20"));
2541 verifyFormat("#define A ''");
2542 verifyFormat("#define A ''qqq");
2543 verifyFormat("#define A `qqq");
2544 verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
2545 EXPECT_EQ("const char *c = STRINGIFY(\n"
2546 "\\na : b);",
2547 format("const char * c = STRINGIFY(\n"
2548 "\\na : b);"));
2549
2550 verifyFormat("a\r\\");
2551 verifyFormat("a\v\\");
2552 verifyFormat("a\f\\");
2553 }
2554
TEST_F(FormatTest,IndentsPPDirectiveInReducedSpace)2555 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
2556 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
2557 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12));
2558 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12));
2559 // FIXME: We never break before the macro name.
2560 verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12));
2561
2562 verifyFormat("#define A A\n#define A A");
2563 verifyFormat("#define A(X) A\n#define A A");
2564
2565 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
2566 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22));
2567 }
2568
TEST_F(FormatTest,HandlePreprocessorDirectiveContext)2569 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
2570 EXPECT_EQ("// somecomment\n"
2571 "#include \"a.h\"\n"
2572 "#define A( \\\n"
2573 " A, B)\n"
2574 "#include \"b.h\"\n"
2575 "// somecomment\n",
2576 format(" // somecomment\n"
2577 " #include \"a.h\"\n"
2578 "#define A(A,\\\n"
2579 " B)\n"
2580 " #include \"b.h\"\n"
2581 " // somecomment\n",
2582 getLLVMStyleWithColumns(13)));
2583 }
2584
TEST_F(FormatTest,LayoutSingleHash)2585 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
2586
TEST_F(FormatTest,LayoutCodeInMacroDefinitions)2587 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
2588 EXPECT_EQ("#define A \\\n"
2589 " c; \\\n"
2590 " e;\n"
2591 "f;",
2592 format("#define A c; e;\n"
2593 "f;",
2594 getLLVMStyleWithColumns(14)));
2595 }
2596
TEST_F(FormatTest,LayoutRemainingTokens)2597 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
2598
TEST_F(FormatTest,AlwaysFormatsEntireMacroDefinitions)2599 TEST_F(FormatTest, AlwaysFormatsEntireMacroDefinitions) {
2600 EXPECT_EQ("int i;\n"
2601 "#define A \\\n"
2602 " int i; \\\n"
2603 " int j\n"
2604 "int k;",
2605 format("int i;\n"
2606 "#define A \\\n"
2607 " int i ; \\\n"
2608 " int j\n"
2609 "int k;",
2610 8, 0, getGoogleStyle())); // 8: position of "#define".
2611 EXPECT_EQ("int i;\n"
2612 "#define A \\\n"
2613 " int i; \\\n"
2614 " int j\n"
2615 "int k;",
2616 format("int i;\n"
2617 "#define A \\\n"
2618 " int i ; \\\n"
2619 " int j\n"
2620 "int k;",
2621 45, 0, getGoogleStyle())); // 45: position of "j".
2622 }
2623
TEST_F(FormatTest,MacroDefinitionInsideStatement)2624 TEST_F(FormatTest, MacroDefinitionInsideStatement) {
2625 EXPECT_EQ("int x,\n"
2626 "#define A\n"
2627 " y;",
2628 format("int x,\n#define A\ny;"));
2629 }
2630
TEST_F(FormatTest,HashInMacroDefinition)2631 TEST_F(FormatTest, HashInMacroDefinition) {
2632 EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
2633 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11));
2634 verifyFormat("#define A \\\n"
2635 " { \\\n"
2636 " f(#c); \\\n"
2637 " }",
2638 getLLVMStyleWithColumns(11));
2639
2640 verifyFormat("#define A(X) \\\n"
2641 " void function##X()",
2642 getLLVMStyleWithColumns(22));
2643
2644 verifyFormat("#define A(a, b, c) \\\n"
2645 " void a##b##c()",
2646 getLLVMStyleWithColumns(22));
2647
2648 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
2649 }
2650
TEST_F(FormatTest,RespectWhitespaceInMacroDefinitions)2651 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
2652 EXPECT_EQ("#define A (x)", format("#define A (x)"));
2653 EXPECT_EQ("#define A(x)", format("#define A(x)"));
2654 }
2655
TEST_F(FormatTest,EmptyLinesInMacroDefinitions)2656 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
2657 EXPECT_EQ("#define A b;", format("#define A \\\n"
2658 " \\\n"
2659 " b;",
2660 getLLVMStyleWithColumns(25)));
2661 EXPECT_EQ("#define A \\\n"
2662 " \\\n"
2663 " a; \\\n"
2664 " b;",
2665 format("#define A \\\n"
2666 " \\\n"
2667 " a; \\\n"
2668 " b;",
2669 getLLVMStyleWithColumns(11)));
2670 EXPECT_EQ("#define A \\\n"
2671 " a; \\\n"
2672 " \\\n"
2673 " b;",
2674 format("#define A \\\n"
2675 " a; \\\n"
2676 " \\\n"
2677 " b;",
2678 getLLVMStyleWithColumns(11)));
2679 }
2680
TEST_F(FormatTest,MacroDefinitionsWithIncompleteCode)2681 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
2682 verifyFormat("#define A :");
2683 verifyFormat("#define SOMECASES \\\n"
2684 " case 1: \\\n"
2685 " case 2\n",
2686 getLLVMStyleWithColumns(20));
2687 verifyFormat("#define A template <typename T>");
2688 verifyFormat("#define STR(x) #x\n"
2689 "f(STR(this_is_a_string_literal{));");
2690 verifyFormat("#pragma omp threadprivate( \\\n"
2691 " y)), // expected-warning",
2692 getLLVMStyleWithColumns(28));
2693 verifyFormat("#d, = };");
2694 verifyFormat("#if \"a");
2695 verifyFormat("({\n"
2696 "#define b }\\\n"
2697 " a\n"
2698 "a");
2699 verifyFormat("#define A \\\n"
2700 " { \\\n"
2701 " {\n"
2702 "#define B \\\n"
2703 " } \\\n"
2704 " }",
2705 getLLVMStyleWithColumns(15));
2706
2707 verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
2708 verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
2709 verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
2710 verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}");
2711 }
2712
TEST_F(FormatTest,MacrosWithoutTrailingSemicolon)2713 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
2714 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
2715 EXPECT_EQ("class A : public QObject {\n"
2716 " Q_OBJECT\n"
2717 "\n"
2718 " A() {}\n"
2719 "};",
2720 format("class A : public QObject {\n"
2721 " Q_OBJECT\n"
2722 "\n"
2723 " A() {\n}\n"
2724 "} ;"));
2725 EXPECT_EQ("SOME_MACRO\n"
2726 "namespace {\n"
2727 "void f();\n"
2728 "}",
2729 format("SOME_MACRO\n"
2730 " namespace {\n"
2731 "void f( );\n"
2732 "}"));
2733 // Only if the identifier contains at least 5 characters.
2734 EXPECT_EQ("HTTP f();",
2735 format("HTTP\nf();"));
2736 EXPECT_EQ("MACRO\nf();",
2737 format("MACRO\nf();"));
2738 // Only if everything is upper case.
2739 EXPECT_EQ("class A : public QObject {\n"
2740 " Q_Object A() {}\n"
2741 "};",
2742 format("class A : public QObject {\n"
2743 " Q_Object\n"
2744 " A() {\n}\n"
2745 "} ;"));
2746
2747 // Only if the next line can actually start an unwrapped line.
2748 EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
2749 format("SOME_WEIRD_LOG_MACRO\n"
2750 "<< SomeThing;"));
2751
2752 verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
2753 "(n, buffers))\n", getChromiumStyle(FormatStyle::LK_Cpp));
2754 }
2755
TEST_F(FormatTest,MacroCallsWithoutTrailingSemicolon)2756 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
2757 EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
2758 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
2759 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
2760 "class X {};\n"
2761 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
2762 "int *createScopDetectionPass() { return 0; }",
2763 format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
2764 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
2765 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
2766 " class X {};\n"
2767 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
2768 " int *createScopDetectionPass() { return 0; }"));
2769 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
2770 // braces, so that inner block is indented one level more.
2771 EXPECT_EQ("int q() {\n"
2772 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
2773 " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
2774 " IPC_END_MESSAGE_MAP()\n"
2775 "}",
2776 format("int q() {\n"
2777 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
2778 " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
2779 " IPC_END_MESSAGE_MAP()\n"
2780 "}"));
2781
2782 // Same inside macros.
2783 EXPECT_EQ("#define LIST(L) \\\n"
2784 " L(A) \\\n"
2785 " L(B) \\\n"
2786 " L(C)",
2787 format("#define LIST(L) \\\n"
2788 " L(A) \\\n"
2789 " L(B) \\\n"
2790 " L(C)",
2791 getGoogleStyle()));
2792
2793 // These must not be recognized as macros.
2794 EXPECT_EQ("int q() {\n"
2795 " f(x);\n"
2796 " f(x) {}\n"
2797 " f(x)->g();\n"
2798 " f(x)->*g();\n"
2799 " f(x).g();\n"
2800 " f(x) = x;\n"
2801 " f(x) += x;\n"
2802 " f(x) -= x;\n"
2803 " f(x) *= x;\n"
2804 " f(x) /= x;\n"
2805 " f(x) %= x;\n"
2806 " f(x) &= x;\n"
2807 " f(x) |= x;\n"
2808 " f(x) ^= x;\n"
2809 " f(x) >>= x;\n"
2810 " f(x) <<= x;\n"
2811 " f(x)[y].z();\n"
2812 " LOG(INFO) << x;\n"
2813 " ifstream(x) >> x;\n"
2814 "}\n",
2815 format("int q() {\n"
2816 " f(x)\n;\n"
2817 " f(x)\n {}\n"
2818 " f(x)\n->g();\n"
2819 " f(x)\n->*g();\n"
2820 " f(x)\n.g();\n"
2821 " f(x)\n = x;\n"
2822 " f(x)\n += x;\n"
2823 " f(x)\n -= x;\n"
2824 " f(x)\n *= x;\n"
2825 " f(x)\n /= x;\n"
2826 " f(x)\n %= x;\n"
2827 " f(x)\n &= x;\n"
2828 " f(x)\n |= x;\n"
2829 " f(x)\n ^= x;\n"
2830 " f(x)\n >>= x;\n"
2831 " f(x)\n <<= x;\n"
2832 " f(x)\n[y].z();\n"
2833 " LOG(INFO)\n << x;\n"
2834 " ifstream(x)\n >> x;\n"
2835 "}\n"));
2836 EXPECT_EQ("int q() {\n"
2837 " F(x)\n"
2838 " if (1) {\n"
2839 " }\n"
2840 " F(x)\n"
2841 " while (1) {\n"
2842 " }\n"
2843 " F(x)\n"
2844 " G(x);\n"
2845 " F(x)\n"
2846 " try {\n"
2847 " Q();\n"
2848 " } catch (...) {\n"
2849 " }\n"
2850 "}\n",
2851 format("int q() {\n"
2852 "F(x)\n"
2853 "if (1) {}\n"
2854 "F(x)\n"
2855 "while (1) {}\n"
2856 "F(x)\n"
2857 "G(x);\n"
2858 "F(x)\n"
2859 "try { Q(); } catch (...) {}\n"
2860 "}\n"));
2861 EXPECT_EQ("class A {\n"
2862 " A() : t(0) {}\n"
2863 " A(int i) noexcept() : {}\n"
2864 " A(X x)\n" // FIXME: function-level try blocks are broken.
2865 " try : t(0) {\n"
2866 " } catch (...) {\n"
2867 " }\n"
2868 "};",
2869 format("class A {\n"
2870 " A()\n : t(0) {}\n"
2871 " A(int i)\n noexcept() : {}\n"
2872 " A(X x)\n"
2873 " try : t(0) {} catch (...) {}\n"
2874 "};"));
2875 EXPECT_EQ(
2876 "class SomeClass {\n"
2877 "public:\n"
2878 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2879 "};",
2880 format("class SomeClass {\n"
2881 "public:\n"
2882 " SomeClass()\n"
2883 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2884 "};"));
2885 EXPECT_EQ(
2886 "class SomeClass {\n"
2887 "public:\n"
2888 " SomeClass()\n"
2889 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2890 "};",
2891 format("class SomeClass {\n"
2892 "public:\n"
2893 " SomeClass()\n"
2894 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2895 "};", getLLVMStyleWithColumns(40)));
2896 }
2897
TEST_F(FormatTest,LayoutMacroDefinitionsStatementsSpanningBlocks)2898 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
2899 verifyFormat("#define A \\\n"
2900 " f({ \\\n"
2901 " g(); \\\n"
2902 " });", getLLVMStyleWithColumns(11));
2903 }
2904
TEST_F(FormatTest,IndentPreprocessorDirectivesAtZero)2905 TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
2906 EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}"));
2907 }
2908
TEST_F(FormatTest,FormatHashIfNotAtStartOfLine)2909 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
2910 verifyFormat("{\n { a #c; }\n}");
2911 }
2912
TEST_F(FormatTest,FormatUnbalancedStructuralElements)2913 TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
2914 EXPECT_EQ("#define A \\\n { \\\n {\nint i;",
2915 format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
2916 EXPECT_EQ("#define A \\\n } \\\n }\nint i;",
2917 format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
2918 }
2919
TEST_F(FormatTest,EscapedNewlineAtStartOfToken)2920 TEST_F(FormatTest, EscapedNewlineAtStartOfToken) {
2921 EXPECT_EQ(
2922 "#define A \\\n int i; \\\n int j;",
2923 format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11)));
2924 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
2925 }
2926
TEST_F(FormatTest,NoEscapedNewlineHandlingInBlockComments)2927 TEST_F(FormatTest, NoEscapedNewlineHandlingInBlockComments) {
2928 EXPECT_EQ("/* \\ \\ \\\n*/", format("\\\n/* \\ \\ \\\n*/"));
2929 }
2930
TEST_F(FormatTest,DontCrashOnBlockComments)2931 TEST_F(FormatTest, DontCrashOnBlockComments) {
2932 EXPECT_EQ(
2933 "int xxxxxxxxx; /* "
2934 "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\n"
2935 "zzzzzz\n"
2936 "0*/",
2937 format("int xxxxxxxxx; /* "
2938 "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy zzzzzz\n"
2939 "0*/"));
2940 }
2941
TEST_F(FormatTest,CalculateSpaceOnConsecutiveLinesInMacro)2942 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
2943 verifyFormat("#define A \\\n"
2944 " int v( \\\n"
2945 " a); \\\n"
2946 " int i;",
2947 getLLVMStyleWithColumns(11));
2948 }
2949
TEST_F(FormatTest,MixingPreprocessorDirectivesAndNormalCode)2950 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
2951 EXPECT_EQ(
2952 "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
2953 " \\\n"
2954 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
2955 "\n"
2956 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
2957 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
2958 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro("
2959 "\\\n"
2960 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
2961 " \n"
2962 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
2963 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
2964 }
2965
TEST_F(FormatTest,LayoutStatementsAroundPreprocessorDirectives)2966 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
2967 EXPECT_EQ("int\n"
2968 "#define A\n"
2969 " a;",
2970 format("int\n#define A\na;"));
2971 verifyFormat("functionCallTo(\n"
2972 " someOtherFunction(\n"
2973 " withSomeParameters, whichInSequence,\n"
2974 " areLongerThanALine(andAnotherCall,\n"
2975 "#define A B\n"
2976 " withMoreParamters,\n"
2977 " whichStronglyInfluenceTheLayout),\n"
2978 " andMoreParameters),\n"
2979 " trailing);",
2980 getLLVMStyleWithColumns(69));
2981 verifyFormat("Foo::Foo()\n"
2982 "#ifdef BAR\n"
2983 " : baz(0)\n"
2984 "#endif\n"
2985 "{\n"
2986 "}");
2987 verifyFormat("void f() {\n"
2988 " if (true)\n"
2989 "#ifdef A\n"
2990 " f(42);\n"
2991 " x();\n"
2992 "#else\n"
2993 " g();\n"
2994 " x();\n"
2995 "#endif\n"
2996 "}");
2997 verifyFormat("void f(param1, param2,\n"
2998 " param3,\n"
2999 "#ifdef A\n"
3000 " param4(param5,\n"
3001 "#ifdef A1\n"
3002 " param6,\n"
3003 "#ifdef A2\n"
3004 " param7),\n"
3005 "#else\n"
3006 " param8),\n"
3007 " param9,\n"
3008 "#endif\n"
3009 " param10,\n"
3010 "#endif\n"
3011 " param11)\n"
3012 "#else\n"
3013 " param12)\n"
3014 "#endif\n"
3015 "{\n"
3016 " x();\n"
3017 "}",
3018 getLLVMStyleWithColumns(28));
3019 verifyFormat("#if 1\n"
3020 "int i;");
3021 verifyFormat(
3022 "#if 1\n"
3023 "#endif\n"
3024 "#if 1\n"
3025 "#else\n"
3026 "#endif\n");
3027 verifyFormat("DEBUG({\n"
3028 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3029 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
3030 "});\n"
3031 "#if a\n"
3032 "#else\n"
3033 "#endif");
3034
3035 verifyFormat("void f(\n"
3036 "#if A\n"
3037 " );\n"
3038 "#else\n"
3039 "#endif");
3040 }
3041
TEST_F(FormatTest,GraciouslyHandleIncorrectPreprocessorConditions)3042 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
3043 verifyFormat("#endif\n"
3044 "#if B");
3045 }
3046
TEST_F(FormatTest,FormatsJoinedLinesOnSubsequentRuns)3047 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
3048 FormatStyle SingleLine = getLLVMStyle();
3049 SingleLine.AllowShortIfStatementsOnASingleLine = true;
3050 verifyFormat(
3051 "#if 0\n"
3052 "#elif 1\n"
3053 "#endif\n"
3054 "void foo() {\n"
3055 " if (test) foo2();\n"
3056 "}",
3057 SingleLine);
3058 }
3059
TEST_F(FormatTest,LayoutBlockInsideParens)3060 TEST_F(FormatTest, LayoutBlockInsideParens) {
3061 verifyFormat("functionCall({ int i; });");
3062 verifyFormat("functionCall({\n"
3063 " int i;\n"
3064 " int j;\n"
3065 "});");
3066 verifyFormat("functionCall({\n"
3067 " int i;\n"
3068 " int j;\n"
3069 "}, aaaa, bbbb, cccc);");
3070 verifyFormat("functionA(functionB({\n"
3071 " int i;\n"
3072 " int j;\n"
3073 " }),\n"
3074 " aaaa, bbbb, cccc);");
3075 verifyFormat("functionCall(\n"
3076 " {\n"
3077 " int i;\n"
3078 " int j;\n"
3079 " },\n"
3080 " aaaa, bbbb, // comment\n"
3081 " cccc);");
3082 verifyFormat("functionA(functionB({\n"
3083 " int i;\n"
3084 " int j;\n"
3085 " }),\n"
3086 " aaaa, bbbb, // comment\n"
3087 " cccc);");
3088 verifyFormat("functionCall(aaaa, bbbb, { int i; });");
3089 verifyFormat("functionCall(aaaa, bbbb, {\n"
3090 " int i;\n"
3091 " int j;\n"
3092 "});");
3093 verifyFormat(
3094 "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
3095 " {\n"
3096 " int i; // break\n"
3097 " },\n"
3098 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
3099 " ccccccccccccccccc));");
3100 verifyFormat("DEBUG({\n"
3101 " if (a)\n"
3102 " f();\n"
3103 "});");
3104 }
3105
TEST_F(FormatTest,LayoutBlockInsideStatement)3106 TEST_F(FormatTest, LayoutBlockInsideStatement) {
3107 EXPECT_EQ("SOME_MACRO { int i; }\n"
3108 "int i;",
3109 format(" SOME_MACRO {int i;} int i;"));
3110 }
3111
TEST_F(FormatTest,LayoutNestedBlocks)3112 TEST_F(FormatTest, LayoutNestedBlocks) {
3113 verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
3114 " struct s {\n"
3115 " int i;\n"
3116 " };\n"
3117 " s kBitsToOs[] = {{10}};\n"
3118 " for (int i = 0; i < 10; ++i)\n"
3119 " return;\n"
3120 "}");
3121 verifyFormat("call(parameter, {\n"
3122 " something();\n"
3123 " // Comment using all columns.\n"
3124 " somethingelse();\n"
3125 "});",
3126 getLLVMStyleWithColumns(40));
3127 verifyFormat("DEBUG( //\n"
3128 " { f(); }, a);");
3129 verifyFormat("DEBUG( //\n"
3130 " {\n"
3131 " f(); //\n"
3132 " },\n"
3133 " a);");
3134
3135 EXPECT_EQ("call(parameter, {\n"
3136 " something();\n"
3137 " // Comment too\n"
3138 " // looooooooooong.\n"
3139 " somethingElse();\n"
3140 "});",
3141 format("call(parameter, {\n"
3142 " something();\n"
3143 " // Comment too looooooooooong.\n"
3144 " somethingElse();\n"
3145 "});",
3146 getLLVMStyleWithColumns(29)));
3147 EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });"));
3148 EXPECT_EQ("DEBUG({ // comment\n"
3149 " int i;\n"
3150 "});",
3151 format("DEBUG({ // comment\n"
3152 "int i;\n"
3153 "});"));
3154 EXPECT_EQ("DEBUG({\n"
3155 " int i;\n"
3156 "\n"
3157 " // comment\n"
3158 " int j;\n"
3159 "});",
3160 format("DEBUG({\n"
3161 " int i;\n"
3162 "\n"
3163 " // comment\n"
3164 " int j;\n"
3165 "});"));
3166
3167 verifyFormat("DEBUG({\n"
3168 " if (a)\n"
3169 " return;\n"
3170 "});");
3171 verifyGoogleFormat("DEBUG({\n"
3172 " if (a) return;\n"
3173 "});");
3174 FormatStyle Style = getGoogleStyle();
3175 Style.ColumnLimit = 45;
3176 verifyFormat("Debug(aaaaa, {\n"
3177 " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
3178 "}, a);",
3179 Style);
3180
3181 verifyNoCrash("^{v^{a}}");
3182 }
3183
TEST_F(FormatTest,IndividualStatementsOfNestedBlocks)3184 TEST_F(FormatTest, IndividualStatementsOfNestedBlocks) {
3185 EXPECT_EQ("DEBUG({\n"
3186 " int i;\n"
3187 " int j;\n"
3188 "});",
3189 format("DEBUG( {\n"
3190 " int i;\n"
3191 " int j;\n"
3192 "} ) ;",
3193 20, 1, getLLVMStyle()));
3194 EXPECT_EQ("DEBUG( {\n"
3195 " int i;\n"
3196 " int j;\n"
3197 "} ) ;",
3198 format("DEBUG( {\n"
3199 " int i;\n"
3200 " int j;\n"
3201 "} ) ;",
3202 41, 1, getLLVMStyle()));
3203 EXPECT_EQ("DEBUG( {\n"
3204 " int i;\n"
3205 " int j;\n"
3206 "} ) ;",
3207 format("DEBUG( {\n"
3208 " int i;\n"
3209 " int j;\n"
3210 "} ) ;",
3211 41, 1, getLLVMStyle()));
3212 EXPECT_EQ("DEBUG({\n"
3213 " int i;\n"
3214 " int j;\n"
3215 "});",
3216 format("DEBUG( {\n"
3217 " int i;\n"
3218 " int j;\n"
3219 "} ) ;",
3220 20, 1, getLLVMStyle()));
3221
3222 EXPECT_EQ("Debug({\n"
3223 " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n"
3224 " return;\n"
3225 " },\n"
3226 " a);",
3227 format("Debug({\n"
3228 " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n"
3229 " return;\n"
3230 " },\n"
3231 " a);",
3232 50, 1, getLLVMStyle()));
3233 EXPECT_EQ("DEBUG({\n"
3234 " DEBUG({\n"
3235 " int a;\n"
3236 " int b;\n"
3237 " }) ;\n"
3238 "});",
3239 format("DEBUG({\n"
3240 " DEBUG({\n"
3241 " int a;\n"
3242 " int b;\n" // Format this line only.
3243 " }) ;\n" // Don't touch this line.
3244 "});",
3245 35, 0, getLLVMStyle()));
3246 EXPECT_EQ("DEBUG({\n"
3247 " int a; //\n"
3248 "});",
3249 format("DEBUG({\n"
3250 " int a; //\n"
3251 "});",
3252 0, 0, getLLVMStyle()));
3253 }
3254
TEST_F(FormatTest,PutEmptyBlocksIntoOneLine)3255 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
3256 EXPECT_EQ("{}", format("{}"));
3257 verifyFormat("enum E {};");
3258 verifyFormat("enum E {}");
3259 }
3260
3261 //===----------------------------------------------------------------------===//
3262 // Line break tests.
3263 //===----------------------------------------------------------------------===//
3264
TEST_F(FormatTest,PreventConfusingIndents)3265 TEST_F(FormatTest, PreventConfusingIndents) {
3266 verifyFormat(
3267 "void f() {\n"
3268 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
3269 " parameter, parameter, parameter)),\n"
3270 " SecondLongCall(parameter));\n"
3271 "}");
3272 verifyFormat(
3273 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3274 " aaaaaaaaaaaaaaaaaaaaaaaa(\n"
3275 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3276 " aaaaaaaaaaaaaaaaaaaaaaaa);");
3277 verifyFormat(
3278 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3279 " [aaaaaaaaaaaaaaaaaaaaaaaa\n"
3280 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
3281 " [aaaaaaaaaaaaaaaaaaaaaaaa]];");
3282 verifyFormat(
3283 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
3284 " aaaaaaaaaaaaaaaaaaaaaaaa<\n"
3285 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
3286 " aaaaaaaaaaaaaaaaaaaaaaaa>;");
3287 verifyFormat("int a = bbbb && ccc && fffff(\n"
3288 "#define A Just forcing a new line\n"
3289 " ddd);");
3290 }
3291
TEST_F(FormatTest,LineBreakingInBinaryExpressions)3292 TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
3293 verifyFormat(
3294 "bool aaaaaaa =\n"
3295 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
3296 " bbbbbbbb();");
3297 verifyFormat(
3298 "bool aaaaaaa =\n"
3299 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
3300 " bbbbbbbb();");
3301
3302 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
3303 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
3304 " ccccccccc == ddddddddddd;");
3305 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
3306 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
3307 " ccccccccc == ddddddddddd;");
3308 verifyFormat(
3309 "bool aaaaaaaaaaaaaaaaaaaaa =\n"
3310 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
3311 " ccccccccc == ddddddddddd;");
3312
3313 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
3314 " aaaaaa) &&\n"
3315 " bbbbbb && cccccc;");
3316 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
3317 " aaaaaa) >>\n"
3318 " bbbbbb;");
3319 verifyFormat("Whitespaces.addUntouchableComment(\n"
3320 " SourceMgr.getSpellingColumnNumber(\n"
3321 " TheLine.Last->FormatTok.Tok.getLocation()) -\n"
3322 " 1);");
3323
3324 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3325 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
3326 " cccccc) {\n}");
3327 verifyFormat("b = a &&\n"
3328 " // Comment\n"
3329 " b.c && d;");
3330
3331 // If the LHS of a comparison is not a binary expression itself, the
3332 // additional linebreak confuses many people.
3333 verifyFormat(
3334 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3335 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
3336 "}");
3337 verifyFormat(
3338 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3339 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3340 "}");
3341 verifyFormat(
3342 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
3343 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3344 "}");
3345 // Even explicit parentheses stress the precedence enough to make the
3346 // additional break unnecessary.
3347 verifyFormat(
3348 "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3349 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3350 "}");
3351 // This cases is borderline, but with the indentation it is still readable.
3352 verifyFormat(
3353 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3354 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3355 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
3356 "}",
3357 getLLVMStyleWithColumns(75));
3358
3359 // If the LHS is a binary expression, we should still use the additional break
3360 // as otherwise the formatting hides the operator precedence.
3361 verifyFormat(
3362 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3363 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3364 " 5) {\n"
3365 "}");
3366
3367 FormatStyle OnePerLine = getLLVMStyle();
3368 OnePerLine.BinPackParameters = false;
3369 verifyFormat(
3370 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3371 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3372 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
3373 OnePerLine);
3374 }
3375
TEST_F(FormatTest,ExpressionIndentation)3376 TEST_F(FormatTest, ExpressionIndentation) {
3377 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3378 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3379 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3380 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3381 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
3382 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
3383 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3384 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
3385 " ccccccccccccccccccccccccccccccccccccccccc;");
3386 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3387 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3388 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3389 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3390 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3391 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3392 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3393 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3394 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3395 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3396 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3397 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3398 verifyFormat("if () {\n"
3399 "} else if (aaaaa &&\n"
3400 " bbbbb > // break\n"
3401 " ccccc) {\n"
3402 "}");
3403
3404 // Presence of a trailing comment used to change indentation of b.
3405 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
3406 " b;\n"
3407 "return aaaaaaaaaaaaaaaaaaa +\n"
3408 " b; //",
3409 getLLVMStyleWithColumns(30));
3410 }
3411
TEST_F(FormatTest,ExpressionIndentationBreakingBeforeOperators)3412 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
3413 // Not sure what the best system is here. Like this, the LHS can be found
3414 // immediately above an operator (everything with the same or a higher
3415 // indent). The RHS is aligned right of the operator and so compasses
3416 // everything until something with the same indent as the operator is found.
3417 // FIXME: Is this a good system?
3418 FormatStyle Style = getLLVMStyle();
3419 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
3420 verifyFormat(
3421 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3422 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3423 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3424 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3425 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3426 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3427 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3428 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3429 " > ccccccccccccccccccccccccccccccccccccccccc;",
3430 Style);
3431 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3432 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3433 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3434 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3435 Style);
3436 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3437 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3438 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3439 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3440 Style);
3441 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3442 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3443 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3444 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3445 Style);
3446 verifyFormat("if () {\n"
3447 "} else if (aaaaa\n"
3448 " && bbbbb // break\n"
3449 " > ccccc) {\n"
3450 "}",
3451 Style);
3452 verifyFormat("return (a)\n"
3453 " // comment\n"
3454 " + b;",
3455 Style);
3456 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3457 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3458 " + cc;",
3459 Style);
3460
3461 // Forced by comments.
3462 verifyFormat(
3463 "unsigned ContentSize =\n"
3464 " sizeof(int16_t) // DWARF ARange version number\n"
3465 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
3466 " + sizeof(int8_t) // Pointer Size (in bytes)\n"
3467 " + sizeof(int8_t); // Segment Size (in bytes)");
3468
3469 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
3470 " == boost::fusion::at_c<1>(iiii).second;",
3471 Style);
3472
3473 Style.ColumnLimit = 60;
3474 verifyFormat("zzzzzzzzzz\n"
3475 " = bbbbbbbbbbbbbbbbb\n"
3476 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
3477 Style);
3478 }
3479
TEST_F(FormatTest,NoOperandAlignment)3480 TEST_F(FormatTest, NoOperandAlignment) {
3481 FormatStyle Style = getLLVMStyle();
3482 Style.AlignOperands = false;
3483 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
3484 verifyFormat(
3485 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3486 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3487 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3488 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3489 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3490 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3491 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3492 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3493 " > ccccccccccccccccccccccccccccccccccccccccc;",
3494 Style);
3495
3496 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3497 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3498 " + cc;",
3499 Style);
3500 verifyFormat("int a = aa\n"
3501 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3502 " * cccccccccccccccccccccccccccccccccccc;",
3503 Style);
3504
3505 Style.AlignAfterOpenBracket = false;
3506 verifyFormat("return (a > b\n"
3507 " // comment1\n"
3508 " // comment2\n"
3509 " || c);",
3510 Style);
3511 }
3512
TEST_F(FormatTest,BreakingBeforeNonAssigmentOperators)3513 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
3514 FormatStyle Style = getLLVMStyle();
3515 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
3516 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
3517 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3518 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
3519 Style);
3520 }
3521
TEST_F(FormatTest,ConstructorInitializers)3522 TEST_F(FormatTest, ConstructorInitializers) {
3523 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
3524 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
3525 getLLVMStyleWithColumns(45));
3526 verifyFormat("Constructor()\n"
3527 " : Inttializer(FitsOnTheLine) {}",
3528 getLLVMStyleWithColumns(44));
3529 verifyFormat("Constructor()\n"
3530 " : Inttializer(FitsOnTheLine) {}",
3531 getLLVMStyleWithColumns(43));
3532
3533 verifyFormat(
3534 "SomeClass::Constructor()\n"
3535 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
3536
3537 verifyFormat(
3538 "SomeClass::Constructor()\n"
3539 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3540 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
3541 verifyFormat(
3542 "SomeClass::Constructor()\n"
3543 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3544 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
3545
3546 verifyFormat("Constructor()\n"
3547 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3548 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3549 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3550 " aaaaaaaaaaaaaaaaaaaaaaa() {}");
3551
3552 verifyFormat("Constructor()\n"
3553 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3554 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3555
3556 verifyFormat("Constructor(int Parameter = 0)\n"
3557 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
3558 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
3559 verifyFormat("Constructor()\n"
3560 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
3561 "}",
3562 getLLVMStyleWithColumns(60));
3563 verifyFormat("Constructor()\n"
3564 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3565 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
3566
3567 // Here a line could be saved by splitting the second initializer onto two
3568 // lines, but that is not desirable.
3569 verifyFormat("Constructor()\n"
3570 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
3571 " aaaaaaaaaaa(aaaaaaaaaaa),\n"
3572 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3573
3574 FormatStyle OnePerLine = getLLVMStyle();
3575 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
3576 verifyFormat("SomeClass::Constructor()\n"
3577 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3578 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3579 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3580 OnePerLine);
3581 verifyFormat("SomeClass::Constructor()\n"
3582 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
3583 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3584 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3585 OnePerLine);
3586 verifyFormat("MyClass::MyClass(int var)\n"
3587 " : some_var_(var), // 4 space indent\n"
3588 " some_other_var_(var + 1) { // lined up\n"
3589 "}",
3590 OnePerLine);
3591 verifyFormat("Constructor()\n"
3592 " : aaaaa(aaaaaa),\n"
3593 " aaaaa(aaaaaa),\n"
3594 " aaaaa(aaaaaa),\n"
3595 " aaaaa(aaaaaa),\n"
3596 " aaaaa(aaaaaa) {}",
3597 OnePerLine);
3598 verifyFormat("Constructor()\n"
3599 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
3600 " aaaaaaaaaaaaaaaaaaaaaa) {}",
3601 OnePerLine);
3602
3603 EXPECT_EQ("Constructor()\n"
3604 " : // Comment forcing unwanted break.\n"
3605 " aaaa(aaaa) {}",
3606 format("Constructor() :\n"
3607 " // Comment forcing unwanted break.\n"
3608 " aaaa(aaaa) {}"));
3609 }
3610
TEST_F(FormatTest,MemoizationTests)3611 TEST_F(FormatTest, MemoizationTests) {
3612 // This breaks if the memoization lookup does not take \c Indent and
3613 // \c LastSpace into account.
3614 verifyFormat(
3615 "extern CFRunLoopTimerRef\n"
3616 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
3617 " CFTimeInterval interval, CFOptionFlags flags,\n"
3618 " CFIndex order, CFRunLoopTimerCallBack callout,\n"
3619 " CFRunLoopTimerContext *context) {}");
3620
3621 // Deep nesting somewhat works around our memoization.
3622 verifyFormat(
3623 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3624 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3625 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3626 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3627 " aaaaa())))))))))))))))))))))))))))))))))))))));",
3628 getLLVMStyleWithColumns(65));
3629 verifyFormat(
3630 "aaaaa(\n"
3631 " aaaaa,\n"
3632 " aaaaa(\n"
3633 " aaaaa,\n"
3634 " aaaaa(\n"
3635 " aaaaa,\n"
3636 " aaaaa(\n"
3637 " aaaaa,\n"
3638 " aaaaa(\n"
3639 " aaaaa,\n"
3640 " aaaaa(\n"
3641 " aaaaa,\n"
3642 " aaaaa(\n"
3643 " aaaaa,\n"
3644 " aaaaa(\n"
3645 " aaaaa,\n"
3646 " aaaaa(\n"
3647 " aaaaa,\n"
3648 " aaaaa(\n"
3649 " aaaaa,\n"
3650 " aaaaa(\n"
3651 " aaaaa,\n"
3652 " aaaaa(\n"
3653 " aaaaa,\n"
3654 " aaaaa))))))))))));",
3655 getLLVMStyleWithColumns(65));
3656 verifyFormat(
3657 "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"
3658 " a),\n"
3659 " a),\n"
3660 " a),\n"
3661 " a),\n"
3662 " a),\n"
3663 " a),\n"
3664 " a),\n"
3665 " a),\n"
3666 " a),\n"
3667 " a),\n"
3668 " a),\n"
3669 " a),\n"
3670 " a),\n"
3671 " a),\n"
3672 " a),\n"
3673 " a),\n"
3674 " a)",
3675 getLLVMStyleWithColumns(65));
3676
3677 // This test takes VERY long when memoization is broken.
3678 FormatStyle OnePerLine = getLLVMStyle();
3679 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
3680 OnePerLine.BinPackParameters = false;
3681 std::string input = "Constructor()\n"
3682 " : aaaa(a,\n";
3683 for (unsigned i = 0, e = 80; i != e; ++i) {
3684 input += " a,\n";
3685 }
3686 input += " a) {}";
3687 verifyFormat(input, OnePerLine);
3688 }
3689
TEST_F(FormatTest,BreaksAsHighAsPossible)3690 TEST_F(FormatTest, BreaksAsHighAsPossible) {
3691 verifyFormat(
3692 "void f() {\n"
3693 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
3694 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
3695 " f();\n"
3696 "}");
3697 verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
3698 " Intervals[i - 1].getRange().getLast()) {\n}");
3699 }
3700
TEST_F(FormatTest,BreaksFunctionDeclarations)3701 TEST_F(FormatTest, BreaksFunctionDeclarations) {
3702 // Principially, we break function declarations in a certain order:
3703 // 1) break amongst arguments.
3704 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
3705 " Cccccccccccccc cccccccccccccc);");
3706 verifyFormat(
3707 "template <class TemplateIt>\n"
3708 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
3709 " TemplateIt *stop) {}");
3710
3711 // 2) break after return type.
3712 verifyFormat(
3713 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3714 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
3715 getGoogleStyle());
3716
3717 // 3) break after (.
3718 verifyFormat(
3719 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
3720 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
3721 getGoogleStyle());
3722
3723 // 4) break before after nested name specifiers.
3724 verifyFormat(
3725 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3726 "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
3727 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
3728 getGoogleStyle());
3729
3730 // However, there are exceptions, if a sufficient amount of lines can be
3731 // saved.
3732 // FIXME: The precise cut-offs wrt. the number of saved lines might need some
3733 // more adjusting.
3734 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
3735 " Cccccccccccccc cccccccccc,\n"
3736 " Cccccccccccccc cccccccccc,\n"
3737 " Cccccccccccccc cccccccccc,\n"
3738 " Cccccccccccccc cccccccccc);");
3739 verifyFormat(
3740 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3741 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3742 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3743 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
3744 getGoogleStyle());
3745 verifyFormat(
3746 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
3747 " Cccccccccccccc cccccccccc,\n"
3748 " Cccccccccccccc cccccccccc,\n"
3749 " Cccccccccccccc cccccccccc,\n"
3750 " Cccccccccccccc cccccccccc,\n"
3751 " Cccccccccccccc cccccccccc,\n"
3752 " Cccccccccccccc cccccccccc);");
3753 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
3754 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3755 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3756 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3757 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
3758
3759 // Break after multi-line parameters.
3760 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3761 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3762 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3763 " bbbb bbbb);");
3764 verifyFormat("void SomeLoooooooooooongFunction(\n"
3765 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
3766 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3767 " int bbbbbbbbbbbbb);");
3768
3769 // Treat overloaded operators like other functions.
3770 verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3771 "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
3772 verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3773 "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
3774 verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3775 "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
3776 verifyGoogleFormat(
3777 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
3778 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
3779 verifyGoogleFormat(
3780 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
3781 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
3782 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3783 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
3784 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
3785 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
3786 verifyGoogleFormat(
3787 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
3788 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3789 " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
3790
3791 FormatStyle Style = getLLVMStyle();
3792 Style.PointerAlignment = FormatStyle::PAS_Left;
3793 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3794 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
3795 Style);
3796 verifyFormat(
3797 "void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
3798 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3799 Style);
3800 }
3801
TEST_F(FormatTest,TrailingReturnType)3802 TEST_F(FormatTest, TrailingReturnType) {
3803 verifyFormat("auto foo() -> int;\n");
3804 verifyFormat("struct S {\n"
3805 " auto bar() const -> int;\n"
3806 "};");
3807 verifyFormat("template <size_t Order, typename T>\n"
3808 "auto load_img(const std::string &filename)\n"
3809 " -> alias::tensor<Order, T, mem::tag::cpu> {}");
3810 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
3811 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
3812 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
3813
3814 // Not trailing return types.
3815 verifyFormat("void f() { auto a = b->c(); }");
3816 }
3817
TEST_F(FormatTest,BreaksFunctionDeclarationsWithTrailingTokens)3818 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
3819 // Avoid breaking before trailing 'const' or other trailing annotations, if
3820 // they are not function-like.
3821 FormatStyle Style = getGoogleStyle();
3822 Style.ColumnLimit = 47;
3823 verifyFormat("void someLongFunction(\n"
3824 " int someLoooooooooooooongParameter) const {\n}",
3825 getLLVMStyleWithColumns(47));
3826 verifyFormat("LoooooongReturnType\n"
3827 "someLoooooooongFunction() const {}",
3828 getLLVMStyleWithColumns(47));
3829 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
3830 " const {}",
3831 Style);
3832 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3833 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
3834 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3835 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
3836 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3837 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
3838 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
3839 " aaaaaaaaaaa aaaaa) const override;");
3840 verifyGoogleFormat(
3841 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
3842 " const override;");
3843
3844 // Even if the first parameter has to be wrapped.
3845 verifyFormat("void someLongFunction(\n"
3846 " int someLongParameter) const {}",
3847 getLLVMStyleWithColumns(46));
3848 verifyFormat("void someLongFunction(\n"
3849 " int someLongParameter) const {}",
3850 Style);
3851 verifyFormat("void someLongFunction(\n"
3852 " int someLongParameter) override {}",
3853 Style);
3854 verifyFormat("void someLongFunction(\n"
3855 " int someLongParameter) OVERRIDE {}",
3856 Style);
3857 verifyFormat("void someLongFunction(\n"
3858 " int someLongParameter) final {}",
3859 Style);
3860 verifyFormat("void someLongFunction(\n"
3861 " int someLongParameter) FINAL {}",
3862 Style);
3863 verifyFormat("void someLongFunction(\n"
3864 " int parameter) const override {}",
3865 Style);
3866
3867 Style.BreakBeforeBraces = FormatStyle::BS_Allman;
3868 verifyFormat("void someLongFunction(\n"
3869 " int someLongParameter) const\n"
3870 "{\n"
3871 "}",
3872 Style);
3873
3874 // Unless these are unknown annotations.
3875 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
3876 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3877 " LONG_AND_UGLY_ANNOTATION;");
3878
3879 // Breaking before function-like trailing annotations is fine to keep them
3880 // close to their arguments.
3881 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3882 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
3883 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
3884 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
3885 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
3886 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
3887 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
3888 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
3889 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
3890
3891 verifyFormat(
3892 "void aaaaaaaaaaaaaaaaaa()\n"
3893 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
3894 " aaaaaaaaaaaaaaaaaaaaaaaaa));");
3895 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3896 " __attribute__((unused));");
3897 verifyGoogleFormat(
3898 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3899 " GUARDED_BY(aaaaaaaaaaaa);");
3900 verifyGoogleFormat(
3901 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3902 " GUARDED_BY(aaaaaaaaaaaa);");
3903 verifyGoogleFormat(
3904 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
3905 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
3906 }
3907
TEST_F(FormatTest,BreaksDesireably)3908 TEST_F(FormatTest, BreaksDesireably) {
3909 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
3910 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
3911 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
3912 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3913 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
3914 "}");
3915
3916 verifyFormat(
3917 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3918 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3919
3920 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3921 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3922 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
3923
3924 verifyFormat(
3925 "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3926 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
3927 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3928 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
3929
3930 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3931 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3932
3933 verifyFormat(
3934 "void f() {\n"
3935 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
3936 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
3937 "}");
3938 verifyFormat(
3939 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3940 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
3941 verifyFormat(
3942 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3943 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
3944 verifyFormat(
3945 "aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3946 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3947 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3948
3949 // Indent consistently independent of call expression.
3950 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
3951 " dddddddddddddddddddddddddddddd));\n"
3952 "aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
3953 " dddddddddddddddddddddddddddddd));");
3954
3955 // This test case breaks on an incorrect memoization, i.e. an optimization not
3956 // taking into account the StopAt value.
3957 verifyFormat(
3958 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
3959 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
3960 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
3961 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3962
3963 verifyFormat("{\n {\n {\n"
3964 " Annotation.SpaceRequiredBefore =\n"
3965 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
3966 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
3967 " }\n }\n}");
3968
3969 // Break on an outer level if there was a break on an inner level.
3970 EXPECT_EQ("f(g(h(a, // comment\n"
3971 " b, c),\n"
3972 " d, e),\n"
3973 " x, y);",
3974 format("f(g(h(a, // comment\n"
3975 " b, c), d, e), x, y);"));
3976
3977 // Prefer breaking similar line breaks.
3978 verifyFormat(
3979 "const int kTrackingOptions = NSTrackingMouseMoved |\n"
3980 " NSTrackingMouseEnteredAndExited |\n"
3981 " NSTrackingActiveAlways;");
3982 }
3983
TEST_F(FormatTest,FormatsDeclarationsOnePerLine)3984 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
3985 FormatStyle NoBinPacking = getGoogleStyle();
3986 NoBinPacking.BinPackParameters = false;
3987 NoBinPacking.BinPackArguments = true;
3988 verifyFormat("void f() {\n"
3989 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
3990 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
3991 "}",
3992 NoBinPacking);
3993 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
3994 " int aaaaaaaaaaaaaaaaaaaa,\n"
3995 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3996 NoBinPacking);
3997 }
3998
TEST_F(FormatTest,FormatsOneParameterPerLineIfNecessary)3999 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
4000 FormatStyle NoBinPacking = getGoogleStyle();
4001 NoBinPacking.BinPackParameters = false;
4002 NoBinPacking.BinPackArguments = false;
4003 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
4004 " aaaaaaaaaaaaaaaaaaaa,\n"
4005 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
4006 NoBinPacking);
4007 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
4008 " aaaaaaaaaaaaa,\n"
4009 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
4010 NoBinPacking);
4011 verifyFormat(
4012 "aaaaaaaa(aaaaaaaaaaaaa,\n"
4013 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4014 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
4015 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4016 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
4017 NoBinPacking);
4018 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
4019 " .aaaaaaaaaaaaaaaaaa();",
4020 NoBinPacking);
4021 verifyFormat("void f() {\n"
4022 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4023 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
4024 "}",
4025 NoBinPacking);
4026
4027 verifyFormat(
4028 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4029 " aaaaaaaaaaaa,\n"
4030 " aaaaaaaaaaaa);",
4031 NoBinPacking);
4032 verifyFormat(
4033 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
4034 " ddddddddddddddddddddddddddddd),\n"
4035 " test);",
4036 NoBinPacking);
4037
4038 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
4039 " aaaaaaaaaaaaaaaaaaaaaaa,\n"
4040 " aaaaaaaaaaaaaaaaaaaaaaa> aaaaaaaaaaaaaaaaaa;",
4041 NoBinPacking);
4042 verifyFormat("a(\"a\"\n"
4043 " \"a\",\n"
4044 " a);");
4045
4046 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
4047 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
4048 " aaaaaaaaa,\n"
4049 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4050 NoBinPacking);
4051 verifyFormat(
4052 "void f() {\n"
4053 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
4054 " .aaaaaaa();\n"
4055 "}",
4056 NoBinPacking);
4057 verifyFormat(
4058 "template <class SomeType, class SomeOtherType>\n"
4059 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
4060 NoBinPacking);
4061 }
4062
TEST_F(FormatTest,AdaptiveOnePerLineFormatting)4063 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
4064 FormatStyle Style = getLLVMStyleWithColumns(15);
4065 Style.ExperimentalAutoDetectBinPacking = true;
4066 EXPECT_EQ("aaa(aaaa,\n"
4067 " aaaa,\n"
4068 " aaaa);\n"
4069 "aaa(aaaa,\n"
4070 " aaaa,\n"
4071 " aaaa);",
4072 format("aaa(aaaa,\n" // one-per-line
4073 " aaaa,\n"
4074 " aaaa );\n"
4075 "aaa(aaaa, aaaa, aaaa);", // inconclusive
4076 Style));
4077 EXPECT_EQ("aaa(aaaa, aaaa,\n"
4078 " aaaa);\n"
4079 "aaa(aaaa, aaaa,\n"
4080 " aaaa);",
4081 format("aaa(aaaa, aaaa,\n" // bin-packed
4082 " aaaa );\n"
4083 "aaa(aaaa, aaaa, aaaa);", // inconclusive
4084 Style));
4085 }
4086
TEST_F(FormatTest,FormatsBuilderPattern)4087 TEST_F(FormatTest, FormatsBuilderPattern) {
4088 verifyFormat(
4089 "return llvm::StringSwitch<Reference::Kind>(name)\n"
4090 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
4091 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
4092 " .StartsWith(\".init\", ORDER_INIT)\n"
4093 " .StartsWith(\".fini\", ORDER_FINI)\n"
4094 " .StartsWith(\".hash\", ORDER_HASH)\n"
4095 " .Default(ORDER_TEXT);\n");
4096
4097 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
4098 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
4099 verifyFormat(
4100 "aaaaaaa->aaaaaaa->aaaaaaaaaaaaaaaa(\n"
4101 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4102 " ->aaaaaaaa(aaaaaaaaaaaaaaa);");
4103 verifyFormat(
4104 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
4105 " aaaaaaaaaaaaaa);");
4106 verifyFormat(
4107 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
4108 " aaaaaa->aaaaaaaaaaaa()\n"
4109 " ->aaaaaaaaaaaaaaaa(\n"
4110 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4111 " ->aaaaaaaaaaaaaaaaa();");
4112 verifyGoogleFormat(
4113 "void f() {\n"
4114 " someo->Add((new util::filetools::Handler(dir))\n"
4115 " ->OnEvent1(NewPermanentCallback(\n"
4116 " this, &HandlerHolderClass::EventHandlerCBA))\n"
4117 " ->OnEvent2(NewPermanentCallback(\n"
4118 " this, &HandlerHolderClass::EventHandlerCBB))\n"
4119 " ->OnEvent3(NewPermanentCallback(\n"
4120 " this, &HandlerHolderClass::EventHandlerCBC))\n"
4121 " ->OnEvent5(NewPermanentCallback(\n"
4122 " this, &HandlerHolderClass::EventHandlerCBD))\n"
4123 " ->OnEvent6(NewPermanentCallback(\n"
4124 " this, &HandlerHolderClass::EventHandlerCBE)));\n"
4125 "}");
4126
4127 verifyFormat(
4128 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
4129 verifyFormat("aaaaaaaaaaaaaaa()\n"
4130 " .aaaaaaaaaaaaaaa()\n"
4131 " .aaaaaaaaaaaaaaa()\n"
4132 " .aaaaaaaaaaaaaaa()\n"
4133 " .aaaaaaaaaaaaaaa();");
4134 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4135 " .aaaaaaaaaaaaaaa()\n"
4136 " .aaaaaaaaaaaaaaa()\n"
4137 " .aaaaaaaaaaaaaaa();");
4138 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4139 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4140 " .aaaaaaaaaaaaaaa();");
4141 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
4142 " ->aaaaaaaaaaaaaae(0)\n"
4143 " ->aaaaaaaaaaaaaaa();");
4144
4145 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
4146 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4147 " .has<bbbbbbbbbbbbbbbbbbbbb>();");
4148 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
4149 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
4150 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
4151
4152 // Prefer not to break after empty parentheses.
4153 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
4154 " First->LastNewlineOffset);");
4155 }
4156
TEST_F(FormatTest,BreaksAccordingToOperatorPrecedence)4157 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
4158 verifyFormat(
4159 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
4160 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
4161 verifyFormat(
4162 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
4163 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
4164
4165 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
4166 " ccccccccccccccccccccccccc) {\n}");
4167 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
4168 " ccccccccccccccccccccccccc) {\n}");
4169
4170 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
4171 " ccccccccccccccccccccccccc) {\n}");
4172 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
4173 " ccccccccccccccccccccccccc) {\n}");
4174
4175 verifyFormat(
4176 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
4177 " ccccccccccccccccccccccccc) {\n}");
4178 verifyFormat(
4179 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
4180 " ccccccccccccccccccccccccc) {\n}");
4181
4182 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
4183 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
4184 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
4185 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
4186 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
4187 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
4188 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
4189 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
4190
4191 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
4192 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
4193 " aaaaaaaaaaaaaaa != aa) {\n}");
4194 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
4195 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
4196 " aaaaaaaaaaaaaaa != aa) {\n}");
4197 }
4198
TEST_F(FormatTest,BreaksAfterAssignments)4199 TEST_F(FormatTest, BreaksAfterAssignments) {
4200 verifyFormat(
4201 "unsigned Cost =\n"
4202 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
4203 " SI->getPointerAddressSpaceee());\n");
4204 verifyFormat(
4205 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
4206 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
4207
4208 verifyFormat(
4209 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
4210 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
4211 verifyFormat("unsigned OriginalStartColumn =\n"
4212 " SourceMgr.getSpellingColumnNumber(\n"
4213 " Current.FormatTok.getStartOfNonWhitespace()) -\n"
4214 " 1;");
4215 }
4216
TEST_F(FormatTest,AlignsAfterAssignments)4217 TEST_F(FormatTest, AlignsAfterAssignments) {
4218 verifyFormat(
4219 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4220 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
4221 verifyFormat(
4222 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4223 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
4224 verifyFormat(
4225 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4226 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
4227 verifyFormat(
4228 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4229 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
4230 verifyFormat(
4231 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
4232 " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
4233 " aaaaaaaaaaaaaaaaaaaaaaaa;");
4234 }
4235
TEST_F(FormatTest,AlignsAfterReturn)4236 TEST_F(FormatTest, AlignsAfterReturn) {
4237 verifyFormat(
4238 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4239 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
4240 verifyFormat(
4241 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4242 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
4243 verifyFormat(
4244 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
4245 " aaaaaaaaaaaaaaaaaaaaaa();");
4246 verifyFormat(
4247 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
4248 " aaaaaaaaaaaaaaaaaaaaaa());");
4249 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4250 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4251 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4252 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
4253 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4254 verifyFormat("return\n"
4255 " // true if code is one of a or b.\n"
4256 " code == a || code == b;");
4257 }
4258
TEST_F(FormatTest,AlignsAfterOpenBracket)4259 TEST_F(FormatTest, AlignsAfterOpenBracket) {
4260 verifyFormat(
4261 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
4262 " aaaaaaaaa aaaaaaa) {}");
4263 verifyFormat(
4264 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
4265 " aaaaaaaaaaa aaaaaaaaa);");
4266 verifyFormat(
4267 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
4268 " aaaaaaaaaaaaaaaaaaaaa));");
4269 FormatStyle Style = getLLVMStyle();
4270 Style.AlignAfterOpenBracket = false;
4271 verifyFormat(
4272 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4273 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
4274 Style);
4275 verifyFormat(
4276 "SomeLongVariableName->someVeryLongFunctionName(\n"
4277 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
4278 Style);
4279 verifyFormat(
4280 "SomeLongVariableName->someFunction(\n"
4281 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
4282 Style);
4283 verifyFormat(
4284 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
4285 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4286 Style);
4287 verifyFormat(
4288 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
4289 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4290 Style);
4291 verifyFormat(
4292 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
4293 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
4294 Style);
4295 }
4296
TEST_F(FormatTest,ParenthesesAndOperandAlignment)4297 TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
4298 FormatStyle Style = getLLVMStyleWithColumns(40);
4299 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4300 " bbbbbbbbbbbbbbbbbbbbbb);",
4301 Style);
4302 Style.AlignAfterOpenBracket = true;
4303 Style.AlignOperands = false;
4304 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4305 " bbbbbbbbbbbbbbbbbbbbbb);",
4306 Style);
4307 Style.AlignAfterOpenBracket = false;
4308 Style.AlignOperands = true;
4309 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4310 " bbbbbbbbbbbbbbbbbbbbbb);",
4311 Style);
4312 Style.AlignAfterOpenBracket = false;
4313 Style.AlignOperands = false;
4314 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4315 " bbbbbbbbbbbbbbbbbbbbbb);",
4316 Style);
4317 }
4318
TEST_F(FormatTest,BreaksConditionalExpressions)4319 TEST_F(FormatTest, BreaksConditionalExpressions) {
4320 verifyFormat(
4321 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4322 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4323 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4324 verifyFormat(
4325 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4326 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4327 verifyFormat(
4328 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
4329 " : aaaaaaaaaaaaa);");
4330 verifyFormat(
4331 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4332 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4333 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4334 " aaaaaaaaaaaaa);");
4335 verifyFormat(
4336 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4337 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4338 " aaaaaaaaaaaaa);");
4339 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4340 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4341 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4342 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4343 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4344 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4345 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4346 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4347 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4348 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4349 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4350 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4351 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4352 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4353 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4354 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4355 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4356 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4357 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4358 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4359 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
4360 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4361 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4362 " : aaaaaaaaaaaaaaaa;");
4363 verifyFormat(
4364 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4365 " ? aaaaaaaaaaaaaaa\n"
4366 " : aaaaaaaaaaaaaaa;");
4367 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
4368 " aaaaaaaaa\n"
4369 " ? b\n"
4370 " : c);");
4371 verifyFormat("return aaaa == bbbb\n"
4372 " // comment\n"
4373 " ? aaaa\n"
4374 " : bbbb;");
4375 verifyFormat(
4376 "unsigned Indent =\n"
4377 " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n"
4378 " ? IndentForLevel[TheLine.Level]\n"
4379 " : TheLine * 2,\n"
4380 " TheLine.InPPDirective, PreviousEndOfLineColumn);",
4381 getLLVMStyleWithColumns(70));
4382 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
4383 " ? aaaaaaaaaaaaaaa\n"
4384 " : bbbbbbbbbbbbbbb //\n"
4385 " ? ccccccccccccccc\n"
4386 " : ddddddddddddddd;");
4387 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
4388 " ? aaaaaaaaaaaaaaa\n"
4389 " : (bbbbbbbbbbbbbbb //\n"
4390 " ? ccccccccccccccc\n"
4391 " : ddddddddddddddd);");
4392 verifyFormat(
4393 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4394 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4395 " aaaaaaaaaaaaaaaaaaaaa +\n"
4396 " aaaaaaaaaaaaaaaaaaaaa\n"
4397 " : aaaaaaaaaa;");
4398 verifyFormat(
4399 "aaaaaa = aaaaaaaaaaaa\n"
4400 " ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4401 " : aaaaaaaaaaaaaaaaaaaaaa\n"
4402 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4403
4404 FormatStyle NoBinPacking = getLLVMStyle();
4405 NoBinPacking.BinPackArguments = false;
4406 verifyFormat(
4407 "void f() {\n"
4408 " g(aaa,\n"
4409 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
4410 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4411 " ? aaaaaaaaaaaaaaa\n"
4412 " : aaaaaaaaaaaaaaa);\n"
4413 "}",
4414 NoBinPacking);
4415 verifyFormat(
4416 "void f() {\n"
4417 " g(aaa,\n"
4418 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
4419 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4420 " ?: aaaaaaaaaaaaaaa);\n"
4421 "}",
4422 NoBinPacking);
4423
4424 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
4425 " // comment.\n"
4426 " ccccccccccccccccccccccccccccccccccccccc\n"
4427 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4428 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
4429
4430 // Assignments in conditional expressions. Apparently not uncommon :-(.
4431 verifyFormat("return a != b\n"
4432 " // comment\n"
4433 " ? a = b\n"
4434 " : a = b;");
4435 verifyFormat("return a != b\n"
4436 " // comment\n"
4437 " ? a = a != b\n"
4438 " // comment\n"
4439 " ? a = b\n"
4440 " : a\n"
4441 " : a;\n");
4442 verifyFormat("return a != b\n"
4443 " // comment\n"
4444 " ? a\n"
4445 " : a = a != b\n"
4446 " // comment\n"
4447 " ? a = b\n"
4448 " : a;");
4449 }
4450
TEST_F(FormatTest,BreaksConditionalExpressionsAfterOperator)4451 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
4452 FormatStyle Style = getLLVMStyle();
4453 Style.BreakBeforeTernaryOperators = false;
4454 Style.ColumnLimit = 70;
4455 verifyFormat(
4456 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4457 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4458 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4459 Style);
4460 verifyFormat(
4461 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4462 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4463 Style);
4464 verifyFormat(
4465 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
4466 " aaaaaaaaaaaaa);",
4467 Style);
4468 verifyFormat(
4469 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4470 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4471 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4472 " aaaaaaaaaaaaa);",
4473 Style);
4474 verifyFormat(
4475 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4476 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4477 " aaaaaaaaaaaaa);",
4478 Style);
4479 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4480 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4481 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
4482 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4483 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4484 Style);
4485 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4486 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4487 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4488 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
4489 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4490 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4491 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4492 Style);
4493 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4494 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
4495 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4496 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4497 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4498 Style);
4499 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4500 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4501 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4502 Style);
4503 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
4504 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4505 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4506 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4507 Style);
4508 verifyFormat(
4509 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4510 " aaaaaaaaaaaaaaa :\n"
4511 " aaaaaaaaaaaaaaa;",
4512 Style);
4513 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
4514 " aaaaaaaaa ?\n"
4515 " b :\n"
4516 " c);",
4517 Style);
4518 verifyFormat(
4519 "unsigned Indent =\n"
4520 " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0 ?\n"
4521 " IndentForLevel[TheLine.Level] :\n"
4522 " TheLine * 2,\n"
4523 " TheLine.InPPDirective, PreviousEndOfLineColumn);",
4524 Style);
4525 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
4526 " aaaaaaaaaaaaaaa :\n"
4527 " bbbbbbbbbbbbbbb ? //\n"
4528 " ccccccccccccccc :\n"
4529 " ddddddddddddddd;",
4530 Style);
4531 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
4532 " aaaaaaaaaaaaaaa :\n"
4533 " (bbbbbbbbbbbbbbb ? //\n"
4534 " ccccccccccccccc :\n"
4535 " ddddddddddddddd);",
4536 Style);
4537 }
4538
TEST_F(FormatTest,DeclarationsOfMultipleVariables)4539 TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
4540 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
4541 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
4542 verifyFormat("bool a = true, b = false;");
4543
4544 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4545 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
4546 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
4547 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
4548 verifyFormat(
4549 "bool aaaaaaaaaaaaaaaaaaaaa =\n"
4550 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
4551 " d = e && f;");
4552 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
4553 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
4554 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
4555 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
4556 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
4557 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
4558
4559 FormatStyle Style = getGoogleStyle();
4560 Style.PointerAlignment = FormatStyle::PAS_Left;
4561 Style.DerivePointerAlignment = false;
4562 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4563 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
4564 " *b = bbbbbbbbbbbbbbbbbbb;",
4565 Style);
4566 verifyFormat(
4567 "aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
4568 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
4569 Style);
4570 }
4571
TEST_F(FormatTest,ConditionalExpressionsInBrackets)4572 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
4573 verifyFormat("arr[foo ? bar : baz];");
4574 verifyFormat("f()[foo ? bar : baz];");
4575 verifyFormat("(a + b)[foo ? bar : baz];");
4576 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
4577 }
4578
TEST_F(FormatTest,AlignsStringLiterals)4579 TEST_F(FormatTest, AlignsStringLiterals) {
4580 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
4581 " \"short literal\");");
4582 verifyFormat(
4583 "looooooooooooooooooooooooongFunction(\n"
4584 " \"short literal\"\n"
4585 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
4586 verifyFormat("someFunction(\"Always break between multi-line\"\n"
4587 " \" string literals\",\n"
4588 " and, other, parameters);");
4589 EXPECT_EQ("fun + \"1243\" /* comment */\n"
4590 " \"5678\";",
4591 format("fun + \"1243\" /* comment */\n"
4592 " \"5678\";",
4593 getLLVMStyleWithColumns(28)));
4594 EXPECT_EQ(
4595 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
4596 " \"aaaaaaaaaaaaaaaaaaaaa\"\n"
4597 " \"aaaaaaaaaaaaaaaa\";",
4598 format("aaaaaa ="
4599 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
4600 "aaaaaaaaaaaaaaaaaaaaa\" "
4601 "\"aaaaaaaaaaaaaaaa\";"));
4602 verifyFormat("a = a + \"a\"\n"
4603 " \"a\"\n"
4604 " \"a\";");
4605 verifyFormat("f(\"a\", \"b\"\n"
4606 " \"c\");");
4607
4608 verifyFormat(
4609 "#define LL_FORMAT \"ll\"\n"
4610 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
4611 " \"d, ddddddddd: %\" LL_FORMAT \"d\");");
4612
4613 verifyFormat("#define A(X) \\\n"
4614 " \"aaaaa\" #X \"bbbbbb\" \\\n"
4615 " \"ccccc\"",
4616 getLLVMStyleWithColumns(23));
4617 verifyFormat("#define A \"def\"\n"
4618 "f(\"abc\" A \"ghi\"\n"
4619 " \"jkl\");");
4620
4621 verifyFormat("f(L\"a\"\n"
4622 " L\"b\")");
4623 verifyFormat("#define A(X) \\\n"
4624 " L\"aaaaa\" #X L\"bbbbbb\" \\\n"
4625 " L\"ccccc\"",
4626 getLLVMStyleWithColumns(25));
4627 }
4628
TEST_F(FormatTest,AlwaysBreakAfterDefinitionReturnType)4629 TEST_F(FormatTest, AlwaysBreakAfterDefinitionReturnType) {
4630 FormatStyle AfterType = getLLVMStyle();
4631 AfterType.AlwaysBreakAfterDefinitionReturnType = true;
4632 verifyFormat("const char *\n"
4633 "f(void) {\n" // Break here.
4634 " return \"\";\n"
4635 "}\n"
4636 "const char *bar(void);\n", // No break here.
4637 AfterType);
4638 verifyFormat("template <class T>\n"
4639 "T *\n"
4640 "f(T &c) {\n" // Break here.
4641 " return NULL;\n"
4642 "}\n"
4643 "template <class T> T *f(T &c);\n", // No break here.
4644 AfterType);
4645 AfterType.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
4646 verifyFormat("const char *\n"
4647 "f(void)\n" // Break here.
4648 "{\n"
4649 " return \"\";\n"
4650 "}\n"
4651 "const char *bar(void);\n", // No break here.
4652 AfterType);
4653 verifyFormat("template <class T>\n"
4654 "T *\n" // Problem here: no line break
4655 "f(T &c)\n" // Break here.
4656 "{\n"
4657 " return NULL;\n"
4658 "}\n"
4659 "template <class T> T *f(T &c);\n", // No break here.
4660 AfterType);
4661 }
4662
TEST_F(FormatTest,AlwaysBreakBeforeMultilineStrings)4663 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
4664 FormatStyle NoBreak = getLLVMStyle();
4665 NoBreak.AlwaysBreakBeforeMultilineStrings = false;
4666 FormatStyle Break = getLLVMStyle();
4667 Break.AlwaysBreakBeforeMultilineStrings = true;
4668 verifyFormat("aaaa = \"bbbb\"\n"
4669 " \"cccc\";",
4670 NoBreak);
4671 verifyFormat("aaaa =\n"
4672 " \"bbbb\"\n"
4673 " \"cccc\";",
4674 Break);
4675 verifyFormat("aaaa(\"bbbb\"\n"
4676 " \"cccc\");",
4677 NoBreak);
4678 verifyFormat("aaaa(\n"
4679 " \"bbbb\"\n"
4680 " \"cccc\");",
4681 Break);
4682 verifyFormat("aaaa(qqq, \"bbbb\"\n"
4683 " \"cccc\");",
4684 NoBreak);
4685 verifyFormat("aaaa(qqq,\n"
4686 " \"bbbb\"\n"
4687 " \"cccc\");",
4688 Break);
4689 verifyFormat("aaaa(qqq,\n"
4690 " L\"bbbb\"\n"
4691 " L\"cccc\");",
4692 Break);
4693
4694 // As we break before unary operators, breaking right after them is bad.
4695 verifyFormat("string foo = abc ? \"x\"\n"
4696 " \"blah blah blah blah blah blah\"\n"
4697 " : \"y\";",
4698 Break);
4699
4700 // Don't break if there is no column gain.
4701 verifyFormat("f(\"aaaa\"\n"
4702 " \"bbbb\");",
4703 Break);
4704
4705 // Treat literals with escaped newlines like multi-line string literals.
4706 EXPECT_EQ("x = \"a\\\n"
4707 "b\\\n"
4708 "c\";",
4709 format("x = \"a\\\n"
4710 "b\\\n"
4711 "c\";",
4712 NoBreak));
4713 EXPECT_EQ("x =\n"
4714 " \"a\\\n"
4715 "b\\\n"
4716 "c\";",
4717 format("x = \"a\\\n"
4718 "b\\\n"
4719 "c\";",
4720 Break));
4721
4722 // Exempt ObjC strings for now.
4723 EXPECT_EQ("NSString *const kString = @\"aaaa\"\n"
4724 " \"bbbb\";",
4725 format("NSString *const kString = @\"aaaa\"\n"
4726 "\"bbbb\";",
4727 Break));
4728
4729 Break.ColumnLimit = 0;
4730 verifyFormat("const char *hello = \"hello llvm\";", Break);
4731 }
4732
TEST_F(FormatTest,AlignsPipes)4733 TEST_F(FormatTest, AlignsPipes) {
4734 verifyFormat(
4735 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4736 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4737 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4738 verifyFormat(
4739 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
4740 " << aaaaaaaaaaaaaaaaaaaa;");
4741 verifyFormat(
4742 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4743 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4744 verifyFormat(
4745 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
4746 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
4747 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
4748 verifyFormat(
4749 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4750 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4751 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4752 verifyFormat(
4753 "llvm::errs() << \"a: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4754 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4755 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4756 verifyFormat(
4757 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4758 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4759 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4760 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
4761 verifyFormat(
4762 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4763 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4764
4765 verifyFormat("return out << \"somepacket = {\\n\"\n"
4766 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
4767 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
4768 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
4769 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
4770 " << \"}\";");
4771
4772 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
4773 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
4774 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
4775 verifyFormat(
4776 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
4777 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
4778 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
4779 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
4780 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
4781 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
4782 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
4783 verifyFormat(
4784 "void f() {\n"
4785 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
4786 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
4787 "}");
4788 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
4789 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
4790
4791 // Breaking before the first "<<" is generally not desirable.
4792 verifyFormat(
4793 "llvm::errs()\n"
4794 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4795 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4796 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4797 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4798 getLLVMStyleWithColumns(70));
4799 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
4800 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4801 " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
4802 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4803 " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
4804 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4805 getLLVMStyleWithColumns(70));
4806
4807 // But sometimes, breaking before the first "<<" is desirable.
4808 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
4809 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
4810 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
4811 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4812 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4813 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
4814 " << BEF << IsTemplate << Description << E->getType();");
4815
4816 verifyFormat(
4817 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4818 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4819
4820 // Incomplete string literal.
4821 EXPECT_EQ("llvm::errs() << \"\n"
4822 " << a;",
4823 format("llvm::errs() << \"\n<<a;"));
4824
4825 verifyFormat("void f() {\n"
4826 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
4827 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
4828 "}");
4829
4830 // Handle 'endl'.
4831 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
4832 " << bbbbbbbbbbbbbbbbbbbbbb << endl;");
4833 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
4834 }
4835
TEST_F(FormatTest,UnderstandsEquals)4836 TEST_F(FormatTest, UnderstandsEquals) {
4837 verifyFormat(
4838 "aaaaaaaaaaaaaaaaa =\n"
4839 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4840 verifyFormat(
4841 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4842 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
4843 verifyFormat(
4844 "if (a) {\n"
4845 " f();\n"
4846 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4847 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
4848 "}");
4849
4850 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4851 " 100000000 + 10000000) {\n}");
4852 }
4853
TEST_F(FormatTest,WrapsAtFunctionCallsIfNecessary)4854 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
4855 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
4856 " .looooooooooooooooooooooooooooooooooooooongFunction();");
4857
4858 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
4859 " ->looooooooooooooooooooooooooooooooooooooongFunction();");
4860
4861 verifyFormat(
4862 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
4863 " Parameter2);");
4864
4865 verifyFormat(
4866 "ShortObject->shortFunction(\n"
4867 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
4868 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
4869
4870 verifyFormat("loooooooooooooongFunction(\n"
4871 " LoooooooooooooongObject->looooooooooooooooongFunction());");
4872
4873 verifyFormat(
4874 "function(LoooooooooooooooooooooooooooooooooooongObject\n"
4875 " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
4876
4877 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
4878 " .WillRepeatedly(Return(SomeValue));");
4879 verifyFormat("void f() {\n"
4880 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
4881 " .Times(2)\n"
4882 " .WillRepeatedly(Return(SomeValue));\n"
4883 "}");
4884 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
4885 " ccccccccccccccccccccccc);");
4886 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4887 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4888 " .aaaaa(aaaaa),\n"
4889 " aaaaaaaaaaaaaaaaaaaaa);");
4890 verifyFormat("void f() {\n"
4891 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4892 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
4893 "}");
4894 verifyFormat(
4895 "aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4896 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4897 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4898 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4899 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4900 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4901 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4902 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4903 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
4904 "}");
4905
4906 // Here, it is not necessary to wrap at "." or "->".
4907 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
4908 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
4909 verifyFormat(
4910 "aaaaaaaaaaa->aaaaaaaaa(\n"
4911 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4912 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
4913
4914 verifyFormat(
4915 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4916 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
4917 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
4918 " aaaaaaaaa()->aaaaaa()->aaaaa());");
4919 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
4920 " aaaaaaaaa()->aaaaaa()->aaaaa());");
4921
4922 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4923 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4924 " .a();");
4925
4926 FormatStyle NoBinPacking = getLLVMStyle();
4927 NoBinPacking.BinPackParameters = false;
4928 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
4929 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
4930 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
4931 " aaaaaaaaaaaaaaaaaaa,\n"
4932 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4933 NoBinPacking);
4934
4935 // If there is a subsequent call, change to hanging indentation.
4936 verifyFormat(
4937 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4938 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
4939 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4940 verifyFormat(
4941 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4942 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
4943 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4944 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4945 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4946 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4947 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4948 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
4949 }
4950
TEST_F(FormatTest,WrapsTemplateDeclarations)4951 TEST_F(FormatTest, WrapsTemplateDeclarations) {
4952 verifyFormat("template <typename T>\n"
4953 "virtual void loooooooooooongFunction(int Param1, int Param2);");
4954 verifyFormat("template <typename T>\n"
4955 "// T should be one of {A, B}.\n"
4956 "virtual void loooooooooooongFunction(int Param1, int Param2);");
4957 verifyFormat(
4958 "template <typename T>\n"
4959 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
4960 verifyFormat("template <typename T>\n"
4961 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
4962 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
4963 verifyFormat(
4964 "template <typename T>\n"
4965 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
4966 " int Paaaaaaaaaaaaaaaaaaaaram2);");
4967 verifyFormat(
4968 "template <typename T>\n"
4969 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
4970 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
4971 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4972 verifyFormat("template <typename T>\n"
4973 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4974 " int aaaaaaaaaaaaaaaaaaaaaa);");
4975 verifyFormat(
4976 "template <typename T1, typename T2 = char, typename T3 = char,\n"
4977 " typename T4 = char>\n"
4978 "void f();");
4979 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
4980 " template <typename> class cccccccccccccccccccccc,\n"
4981 " typename ddddddddddddd>\n"
4982 "class C {};");
4983 verifyFormat(
4984 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
4985 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4986
4987 verifyFormat("void f() {\n"
4988 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
4989 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
4990 "}");
4991
4992 verifyFormat("template <typename T> class C {};");
4993 verifyFormat("template <typename T> void f();");
4994 verifyFormat("template <typename T> void f() {}");
4995 verifyFormat(
4996 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
4997 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4998 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
4999 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
5000 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5001 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
5002 " bbbbbbbbbbbbbbbbbbbbbbbb);",
5003 getLLVMStyleWithColumns(72));
5004 EXPECT_EQ("static_cast<A< //\n"
5005 " B> *>(\n"
5006 "\n"
5007 " );",
5008 format("static_cast<A<//\n"
5009 " B>*>(\n"
5010 "\n"
5011 " );"));
5012 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5013 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
5014
5015 FormatStyle AlwaysBreak = getLLVMStyle();
5016 AlwaysBreak.AlwaysBreakTemplateDeclarations = true;
5017 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
5018 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
5019 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
5020 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5021 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
5022 " ccccccccccccccccccccccccccccccccccccccccccccccc);");
5023 verifyFormat("template <template <typename> class Fooooooo,\n"
5024 " template <typename> class Baaaaaaar>\n"
5025 "struct C {};",
5026 AlwaysBreak);
5027 verifyFormat("template <typename T> // T can be A, B or C.\n"
5028 "struct C {};",
5029 AlwaysBreak);
5030 }
5031
TEST_F(FormatTest,WrapsAtNestedNameSpecifiers)5032 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
5033 verifyFormat(
5034 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5035 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5036 verifyFormat(
5037 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5038 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5039 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
5040
5041 // FIXME: Should we have the extra indent after the second break?
5042 verifyFormat(
5043 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5044 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5045 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5046
5047 verifyFormat(
5048 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
5049 " cccccccccccccccccccccccccccccccccccccccccccccc());");
5050
5051 // Breaking at nested name specifiers is generally not desirable.
5052 verifyFormat(
5053 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5054 " aaaaaaaaaaaaaaaaaaaaaaa);");
5055
5056 verifyFormat(
5057 "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5058 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5059 " aaaaaaaaaaaaaaaaaaaaa);",
5060 getLLVMStyleWithColumns(74));
5061
5062 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5063 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5064 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5065 }
5066
TEST_F(FormatTest,UnderstandsTemplateParameters)5067 TEST_F(FormatTest, UnderstandsTemplateParameters) {
5068 verifyFormat("A<int> a;");
5069 verifyFormat("A<A<A<int>>> a;");
5070 verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
5071 verifyFormat("bool x = a < 1 || 2 > a;");
5072 verifyFormat("bool x = 5 < f<int>();");
5073 verifyFormat("bool x = f<int>() > 5;");
5074 verifyFormat("bool x = 5 < a<int>::x;");
5075 verifyFormat("bool x = a < 4 ? a > 2 : false;");
5076 verifyFormat("bool x = f() ? a < 2 : a > 2;");
5077
5078 verifyGoogleFormat("A<A<int>> a;");
5079 verifyGoogleFormat("A<A<A<int>>> a;");
5080 verifyGoogleFormat("A<A<A<A<int>>>> a;");
5081 verifyGoogleFormat("A<A<int> > a;");
5082 verifyGoogleFormat("A<A<A<int> > > a;");
5083 verifyGoogleFormat("A<A<A<A<int> > > > a;");
5084 verifyGoogleFormat("A<::A<int>> a;");
5085 verifyGoogleFormat("A<::A> a;");
5086 verifyGoogleFormat("A< ::A> a;");
5087 verifyGoogleFormat("A< ::A<int> > a;");
5088 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
5089 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
5090 EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
5091 EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
5092
5093 verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
5094
5095 verifyFormat("test >> a >> b;");
5096 verifyFormat("test << a >> b;");
5097
5098 verifyFormat("f<int>();");
5099 verifyFormat("template <typename T> void f() {}");
5100 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
5101
5102 // Not template parameters.
5103 verifyFormat("return a < b && c > d;");
5104 verifyFormat("void f() {\n"
5105 " while (a < b && c > d) {\n"
5106 " }\n"
5107 "}");
5108 verifyFormat("template <typename... Types>\n"
5109 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
5110
5111 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5112 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
5113 getLLVMStyleWithColumns(60));
5114 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
5115 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
5116 }
5117
TEST_F(FormatTest,UnderstandsBinaryOperators)5118 TEST_F(FormatTest, UnderstandsBinaryOperators) {
5119 verifyFormat("COMPARE(a, ==, b);");
5120 }
5121
TEST_F(FormatTest,UnderstandsPointersToMembers)5122 TEST_F(FormatTest, UnderstandsPointersToMembers) {
5123 verifyFormat("int A::*x;");
5124 verifyFormat("int (S::*func)(void *);");
5125 verifyFormat("void f() { int (S::*func)(void *); }");
5126 verifyFormat("typedef bool *(Class::*Member)() const;");
5127 verifyFormat("void f() {\n"
5128 " (a->*f)();\n"
5129 " a->*x;\n"
5130 " (a.*f)();\n"
5131 " ((*a).*f)();\n"
5132 " a.*x;\n"
5133 "}");
5134 verifyFormat("void f() {\n"
5135 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
5136 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
5137 "}");
5138 verifyFormat(
5139 "(aaaaaaaaaa->*bbbbbbb)(\n"
5140 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
5141 FormatStyle Style = getLLVMStyle();
5142 Style.PointerAlignment = FormatStyle::PAS_Left;
5143 verifyFormat("typedef bool* (Class::*Member)() const;", Style);
5144 }
5145
TEST_F(FormatTest,UnderstandsUnaryOperators)5146 TEST_F(FormatTest, UnderstandsUnaryOperators) {
5147 verifyFormat("int a = -2;");
5148 verifyFormat("f(-1, -2, -3);");
5149 verifyFormat("a[-1] = 5;");
5150 verifyFormat("int a = 5 + -2;");
5151 verifyFormat("if (i == -1) {\n}");
5152 verifyFormat("if (i != -1) {\n}");
5153 verifyFormat("if (i > -1) {\n}");
5154 verifyFormat("if (i < -1) {\n}");
5155 verifyFormat("++(a->f());");
5156 verifyFormat("--(a->f());");
5157 verifyFormat("(a->f())++;");
5158 verifyFormat("a[42]++;");
5159 verifyFormat("if (!(a->f())) {\n}");
5160
5161 verifyFormat("a-- > b;");
5162 verifyFormat("b ? -a : c;");
5163 verifyFormat("n * sizeof char16;");
5164 verifyFormat("n * alignof char16;", getGoogleStyle());
5165 verifyFormat("sizeof(char);");
5166 verifyFormat("alignof(char);", getGoogleStyle());
5167
5168 verifyFormat("return -1;");
5169 verifyFormat("switch (a) {\n"
5170 "case -1:\n"
5171 " break;\n"
5172 "}");
5173 verifyFormat("#define X -1");
5174 verifyFormat("#define X -kConstant");
5175
5176 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
5177 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
5178
5179 verifyFormat("int a = /* confusing comment */ -1;");
5180 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
5181 verifyFormat("int a = i /* confusing comment */++;");
5182 }
5183
TEST_F(FormatTest,DoesNotIndentRelativeToUnaryOperators)5184 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
5185 verifyFormat("if (!aaaaaaaaaa( // break\n"
5186 " aaaaa)) {\n"
5187 "}");
5188 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
5189 " aaaaa));");
5190 verifyFormat("*aaa = aaaaaaa( // break\n"
5191 " bbbbbb);");
5192 }
5193
TEST_F(FormatTest,UnderstandsOverloadedOperators)5194 TEST_F(FormatTest, UnderstandsOverloadedOperators) {
5195 verifyFormat("bool operator<();");
5196 verifyFormat("bool operator>();");
5197 verifyFormat("bool operator=();");
5198 verifyFormat("bool operator==();");
5199 verifyFormat("bool operator!=();");
5200 verifyFormat("int operator+();");
5201 verifyFormat("int operator++();");
5202 verifyFormat("bool operator();");
5203 verifyFormat("bool operator()();");
5204 verifyFormat("bool operator[]();");
5205 verifyFormat("operator bool();");
5206 verifyFormat("operator int();");
5207 verifyFormat("operator void *();");
5208 verifyFormat("operator SomeType<int>();");
5209 verifyFormat("operator SomeType<int, int>();");
5210 verifyFormat("operator SomeType<SomeType<int>>();");
5211 verifyFormat("void *operator new(std::size_t size);");
5212 verifyFormat("void *operator new[](std::size_t size);");
5213 verifyFormat("void operator delete(void *ptr);");
5214 verifyFormat("void operator delete[](void *ptr);");
5215 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
5216 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
5217
5218 verifyFormat(
5219 "ostream &operator<<(ostream &OutputStream,\n"
5220 " SomeReallyLongType WithSomeReallyLongValue);");
5221 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
5222 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
5223 " return left.group < right.group;\n"
5224 "}");
5225 verifyFormat("SomeType &operator=(const SomeType &S);");
5226 verifyFormat("f.template operator()<int>();");
5227
5228 verifyGoogleFormat("operator void*();");
5229 verifyGoogleFormat("operator SomeType<SomeType<int>>();");
5230 verifyGoogleFormat("operator ::A();");
5231
5232 verifyFormat("using A::operator+;");
5233
5234 verifyFormat("string // break\n"
5235 "operator()() & {}");
5236 verifyFormat("string // break\n"
5237 "operator()() && {}");
5238 }
5239
TEST_F(FormatTest,UnderstandsFunctionRefQualification)5240 TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
5241 verifyFormat("Deleted &operator=(const Deleted &)& = default;");
5242 verifyFormat("Deleted &operator=(const Deleted &)&& = delete;");
5243 verifyFormat("SomeType MemberFunction(const Deleted &)& = delete;");
5244 verifyFormat("SomeType MemberFunction(const Deleted &)&& = delete;");
5245 verifyFormat("Deleted &operator=(const Deleted &)&;");
5246 verifyFormat("Deleted &operator=(const Deleted &)&&;");
5247 verifyFormat("SomeType MemberFunction(const Deleted &)&;");
5248 verifyFormat("SomeType MemberFunction(const Deleted &)&&;");
5249
5250 verifyGoogleFormat("Deleted& operator=(const Deleted&)& = default;");
5251 verifyGoogleFormat("SomeType MemberFunction(const Deleted&)& = delete;");
5252 verifyGoogleFormat("Deleted& operator=(const Deleted&)&;");
5253 verifyGoogleFormat("SomeType MemberFunction(const Deleted&)&;");
5254
5255 FormatStyle Spaces = getLLVMStyle();
5256 Spaces.SpacesInCStyleCastParentheses = true;
5257 verifyFormat("Deleted &operator=(const Deleted &)& = default;", Spaces);
5258 verifyFormat("SomeType MemberFunction(const Deleted &)& = delete;", Spaces);
5259 verifyFormat("Deleted &operator=(const Deleted &)&;", Spaces);
5260 verifyFormat("SomeType MemberFunction(const Deleted &)&;", Spaces);
5261
5262 Spaces.SpacesInCStyleCastParentheses = false;
5263 Spaces.SpacesInParentheses = true;
5264 verifyFormat("Deleted &operator=( const Deleted & )& = default;", Spaces);
5265 verifyFormat("SomeType MemberFunction( const Deleted & )& = delete;", Spaces);
5266 verifyFormat("Deleted &operator=( const Deleted & )&;", Spaces);
5267 verifyFormat("SomeType MemberFunction( const Deleted & )&;", Spaces);
5268 }
5269
TEST_F(FormatTest,UnderstandsNewAndDelete)5270 TEST_F(FormatTest, UnderstandsNewAndDelete) {
5271 verifyFormat("void f() {\n"
5272 " A *a = new A;\n"
5273 " A *a = new (placement) A;\n"
5274 " delete a;\n"
5275 " delete (A *)a;\n"
5276 "}");
5277 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
5278 " typename aaaaaaaaaaaaaaaaaaaaaaaa();");
5279 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5280 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
5281 " typename aaaaaaaaaaaaaaaaaaaaaaaa();");
5282 verifyFormat("delete[] h->p;");
5283 }
5284
TEST_F(FormatTest,UnderstandsUsesOfStarAndAmp)5285 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
5286 verifyFormat("int *f(int *a) {}");
5287 verifyFormat("int main(int argc, char **argv) {}");
5288 verifyFormat("Test::Test(int b) : a(b * b) {}");
5289 verifyIndependentOfContext("f(a, *a);");
5290 verifyFormat("void g() { f(*a); }");
5291 verifyIndependentOfContext("int a = b * 10;");
5292 verifyIndependentOfContext("int a = 10 * b;");
5293 verifyIndependentOfContext("int a = b * c;");
5294 verifyIndependentOfContext("int a += b * c;");
5295 verifyIndependentOfContext("int a -= b * c;");
5296 verifyIndependentOfContext("int a *= b * c;");
5297 verifyIndependentOfContext("int a /= b * c;");
5298 verifyIndependentOfContext("int a = *b;");
5299 verifyIndependentOfContext("int a = *b * c;");
5300 verifyIndependentOfContext("int a = b * *c;");
5301 verifyIndependentOfContext("return 10 * b;");
5302 verifyIndependentOfContext("return *b * *c;");
5303 verifyIndependentOfContext("return a & ~b;");
5304 verifyIndependentOfContext("f(b ? *c : *d);");
5305 verifyIndependentOfContext("int a = b ? *c : *d;");
5306 verifyIndependentOfContext("*b = a;");
5307 verifyIndependentOfContext("a * ~b;");
5308 verifyIndependentOfContext("a * !b;");
5309 verifyIndependentOfContext("a * +b;");
5310 verifyIndependentOfContext("a * -b;");
5311 verifyIndependentOfContext("a * ++b;");
5312 verifyIndependentOfContext("a * --b;");
5313 verifyIndependentOfContext("a[4] * b;");
5314 verifyIndependentOfContext("a[a * a] = 1;");
5315 verifyIndependentOfContext("f() * b;");
5316 verifyIndependentOfContext("a * [self dostuff];");
5317 verifyIndependentOfContext("int x = a * (a + b);");
5318 verifyIndependentOfContext("(a *)(a + b);");
5319 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
5320 verifyIndependentOfContext("int *pa = (int *)&a;");
5321 verifyIndependentOfContext("return sizeof(int **);");
5322 verifyIndependentOfContext("return sizeof(int ******);");
5323 verifyIndependentOfContext("return (int **&)a;");
5324 verifyIndependentOfContext("f((*PointerToArray)[10]);");
5325 verifyFormat("void f(Type (*parameter)[10]) {}");
5326 verifyGoogleFormat("return sizeof(int**);");
5327 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
5328 verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
5329 verifyFormat("auto a = [](int **&, int ***) {};");
5330 verifyFormat("auto PointerBinding = [](const char *S) {};");
5331 verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
5332 verifyFormat("[](const decltype(*a) &value) {}");
5333 verifyIndependentOfContext("typedef void (*f)(int *a);");
5334 verifyIndependentOfContext("int i{a * b};");
5335 verifyIndependentOfContext("aaa && aaa->f();");
5336 verifyIndependentOfContext("int x = ~*p;");
5337 verifyFormat("Constructor() : a(a), area(width * height) {}");
5338 verifyFormat("Constructor() : a(a), area(a, width * height) {}");
5339 verifyFormat("void f() { f(a, c * d); }");
5340
5341 verifyIndependentOfContext("InvalidRegions[*R] = 0;");
5342
5343 verifyIndependentOfContext("A<int *> a;");
5344 verifyIndependentOfContext("A<int **> a;");
5345 verifyIndependentOfContext("A<int *, int *> a;");
5346 verifyIndependentOfContext("A<int *[]> a;");
5347 verifyIndependentOfContext(
5348 "const char *const p = reinterpret_cast<const char *const>(q);");
5349 verifyIndependentOfContext("A<int **, int **> a;");
5350 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
5351 verifyFormat("for (char **a = b; *a; ++a) {\n}");
5352 verifyFormat("for (; a && b;) {\n}");
5353 verifyFormat("bool foo = true && [] { return false; }();");
5354
5355 verifyFormat(
5356 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5357 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5358
5359 verifyGoogleFormat("**outparam = 1;");
5360 verifyGoogleFormat("*outparam = a * b;");
5361 verifyGoogleFormat("int main(int argc, char** argv) {}");
5362 verifyGoogleFormat("A<int*> a;");
5363 verifyGoogleFormat("A<int**> a;");
5364 verifyGoogleFormat("A<int*, int*> a;");
5365 verifyGoogleFormat("A<int**, int**> a;");
5366 verifyGoogleFormat("f(b ? *c : *d);");
5367 verifyGoogleFormat("int a = b ? *c : *d;");
5368 verifyGoogleFormat("Type* t = **x;");
5369 verifyGoogleFormat("Type* t = *++*x;");
5370 verifyGoogleFormat("*++*x;");
5371 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
5372 verifyGoogleFormat("Type* t = x++ * y;");
5373 verifyGoogleFormat(
5374 "const char* const p = reinterpret_cast<const char* const>(q);");
5375 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
5376 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
5377 verifyGoogleFormat("template <typename T>\n"
5378 "void f(int i = 0, SomeType** temps = NULL);");
5379
5380 FormatStyle Left = getLLVMStyle();
5381 Left.PointerAlignment = FormatStyle::PAS_Left;
5382 verifyFormat("x = *a(x) = *a(y);", Left);
5383
5384 verifyIndependentOfContext("a = *(x + y);");
5385 verifyIndependentOfContext("a = &(x + y);");
5386 verifyIndependentOfContext("*(x + y).call();");
5387 verifyIndependentOfContext("&(x + y)->call();");
5388 verifyFormat("void f() { &(*I).first; }");
5389
5390 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
5391 verifyFormat(
5392 "int *MyValues = {\n"
5393 " *A, // Operator detection might be confused by the '{'\n"
5394 " *BB // Operator detection might be confused by previous comment\n"
5395 "};");
5396
5397 verifyIndependentOfContext("if (int *a = &b)");
5398 verifyIndependentOfContext("if (int &a = *b)");
5399 verifyIndependentOfContext("if (a & b[i])");
5400 verifyIndependentOfContext("if (a::b::c::d & b[i])");
5401 verifyIndependentOfContext("if (*b[i])");
5402 verifyIndependentOfContext("if (int *a = (&b))");
5403 verifyIndependentOfContext("while (int *a = &b)");
5404 verifyIndependentOfContext("size = sizeof *a;");
5405 verifyIndependentOfContext("if (a && (b = c))");
5406 verifyFormat("void f() {\n"
5407 " for (const int &v : Values) {\n"
5408 " }\n"
5409 "}");
5410 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
5411 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
5412 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
5413
5414 verifyFormat("#define A (!a * b)");
5415 verifyFormat("#define MACRO \\\n"
5416 " int *i = a * b; \\\n"
5417 " void f(a *b);",
5418 getLLVMStyleWithColumns(19));
5419
5420 verifyIndependentOfContext("A = new SomeType *[Length];");
5421 verifyIndependentOfContext("A = new SomeType *[Length]();");
5422 verifyIndependentOfContext("T **t = new T *;");
5423 verifyIndependentOfContext("T **t = new T *();");
5424 verifyGoogleFormat("A = new SomeType* [Length]();");
5425 verifyGoogleFormat("A = new SomeType* [Length];");
5426 verifyGoogleFormat("T** t = new T*;");
5427 verifyGoogleFormat("T** t = new T*();");
5428
5429 FormatStyle PointerLeft = getLLVMStyle();
5430 PointerLeft.PointerAlignment = FormatStyle::PAS_Left;
5431 verifyFormat("delete *x;", PointerLeft);
5432 verifyFormat("STATIC_ASSERT((a & b) == 0);");
5433 verifyFormat("STATIC_ASSERT(0 == (a & b));");
5434 verifyFormat("template <bool a, bool b> "
5435 "typename t::if<x && y>::type f() {}");
5436 verifyFormat("template <int *y> f() {}");
5437 verifyFormat("vector<int *> v;");
5438 verifyFormat("vector<int *const> v;");
5439 verifyFormat("vector<int *const **const *> v;");
5440 verifyFormat("vector<int *volatile> v;");
5441 verifyFormat("vector<a * b> v;");
5442 verifyFormat("foo<b && false>();");
5443 verifyFormat("foo<b & 1>();");
5444 verifyFormat("decltype(*::std::declval<const T &>()) void F();");
5445 verifyFormat(
5446 "template <class T, class = typename std::enable_if<\n"
5447 " std::is_integral<T>::value &&\n"
5448 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
5449 "void F();",
5450 getLLVMStyleWithColumns(76));
5451 verifyFormat(
5452 "template <class T,\n"
5453 " class = typename ::std::enable_if<\n"
5454 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
5455 "void F();",
5456 getGoogleStyleWithColumns(68));
5457
5458 verifyIndependentOfContext("MACRO(int *i);");
5459 verifyIndependentOfContext("MACRO(auto *a);");
5460 verifyIndependentOfContext("MACRO(const A *a);");
5461 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
5462 // FIXME: Is there a way to make this work?
5463 // verifyIndependentOfContext("MACRO(A *a);");
5464
5465 verifyFormat("DatumHandle const *operator->() const { return input_; }");
5466
5467 EXPECT_EQ("#define OP(x) \\\n"
5468 " ostream &operator<<(ostream &s, const A &a) { \\\n"
5469 " return s << a.DebugString(); \\\n"
5470 " }",
5471 format("#define OP(x) \\\n"
5472 " ostream &operator<<(ostream &s, const A &a) { \\\n"
5473 " return s << a.DebugString(); \\\n"
5474 " }",
5475 getLLVMStyleWithColumns(50)));
5476
5477 // FIXME: We cannot handle this case yet; we might be able to figure out that
5478 // foo<x> d > v; doesn't make sense.
5479 verifyFormat("foo<a<b && c> d> v;");
5480
5481 FormatStyle PointerMiddle = getLLVMStyle();
5482 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
5483 verifyFormat("delete *x;", PointerMiddle);
5484 verifyFormat("int * x;", PointerMiddle);
5485 verifyFormat("template <int * y> f() {}", PointerMiddle);
5486 verifyFormat("int * f(int * a) {}", PointerMiddle);
5487 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
5488 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
5489 verifyFormat("A<int *> a;", PointerMiddle);
5490 verifyFormat("A<int **> a;", PointerMiddle);
5491 verifyFormat("A<int *, int *> a;", PointerMiddle);
5492 verifyFormat("A<int * []> a;", PointerMiddle);
5493 verifyFormat("A = new SomeType * [Length]();", PointerMiddle);
5494 verifyFormat("A = new SomeType * [Length];", PointerMiddle);
5495 verifyFormat("T ** t = new T *;", PointerMiddle);
5496 }
5497
TEST_F(FormatTest,UnderstandsAttributes)5498 TEST_F(FormatTest, UnderstandsAttributes) {
5499 verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
5500 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
5501 "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
5502 }
5503
TEST_F(FormatTest,UnderstandsEllipsis)5504 TEST_F(FormatTest, UnderstandsEllipsis) {
5505 verifyFormat("int printf(const char *fmt, ...);");
5506 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
5507 verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}");
5508
5509 FormatStyle PointersLeft = getLLVMStyle();
5510 PointersLeft.PointerAlignment = FormatStyle::PAS_Left;
5511 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft);
5512 }
5513
TEST_F(FormatTest,AdaptivelyFormatsPointersAndReferences)5514 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
5515 EXPECT_EQ("int *a;\n"
5516 "int *a;\n"
5517 "int *a;",
5518 format("int *a;\n"
5519 "int* a;\n"
5520 "int *a;",
5521 getGoogleStyle()));
5522 EXPECT_EQ("int* a;\n"
5523 "int* a;\n"
5524 "int* a;",
5525 format("int* a;\n"
5526 "int* a;\n"
5527 "int *a;",
5528 getGoogleStyle()));
5529 EXPECT_EQ("int *a;\n"
5530 "int *a;\n"
5531 "int *a;",
5532 format("int *a;\n"
5533 "int * a;\n"
5534 "int * a;",
5535 getGoogleStyle()));
5536 }
5537
TEST_F(FormatTest,UnderstandsRvalueReferences)5538 TEST_F(FormatTest, UnderstandsRvalueReferences) {
5539 verifyFormat("int f(int &&a) {}");
5540 verifyFormat("int f(int a, char &&b) {}");
5541 verifyFormat("void f() { int &&a = b; }");
5542 verifyGoogleFormat("int f(int a, char&& b) {}");
5543 verifyGoogleFormat("void f() { int&& a = b; }");
5544
5545 verifyIndependentOfContext("A<int &&> a;");
5546 verifyIndependentOfContext("A<int &&, int &&> a;");
5547 verifyGoogleFormat("A<int&&> a;");
5548 verifyGoogleFormat("A<int&&, int&&> a;");
5549
5550 // Not rvalue references:
5551 verifyFormat("template <bool B, bool C> class A {\n"
5552 " static_assert(B && C, \"Something is wrong\");\n"
5553 "};");
5554 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
5555 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
5556 verifyFormat("#define A(a, b) (a && b)");
5557 }
5558
TEST_F(FormatTest,FormatsBinaryOperatorsPrecedingEquals)5559 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
5560 verifyFormat("void f() {\n"
5561 " x[aaaaaaaaa -\n"
5562 " b] = 23;\n"
5563 "}",
5564 getLLVMStyleWithColumns(15));
5565 }
5566
TEST_F(FormatTest,FormatsCasts)5567 TEST_F(FormatTest, FormatsCasts) {
5568 verifyFormat("Type *A = static_cast<Type *>(P);");
5569 verifyFormat("Type *A = (Type *)P;");
5570 verifyFormat("Type *A = (vector<Type *, int *>)P;");
5571 verifyFormat("int a = (int)(2.0f);");
5572 verifyFormat("int a = (int)2.0f;");
5573 verifyFormat("x[(int32)y];");
5574 verifyFormat("x = (int32)y;");
5575 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
5576 verifyFormat("int a = (int)*b;");
5577 verifyFormat("int a = (int)2.0f;");
5578 verifyFormat("int a = (int)~0;");
5579 verifyFormat("int a = (int)++a;");
5580 verifyFormat("int a = (int)sizeof(int);");
5581 verifyFormat("int a = (int)+2;");
5582 verifyFormat("my_int a = (my_int)2.0f;");
5583 verifyFormat("my_int a = (my_int)sizeof(int);");
5584 verifyFormat("return (my_int)aaa;");
5585 verifyFormat("#define x ((int)-1)");
5586 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
5587 verifyFormat("#define p(q) ((int *)&q)");
5588 verifyFormat("fn(a)(b) + 1;");
5589
5590 verifyFormat("void f() { my_int a = (my_int)*b; }");
5591 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
5592 verifyFormat("my_int a = (my_int)~0;");
5593 verifyFormat("my_int a = (my_int)++a;");
5594 verifyFormat("my_int a = (my_int)-2;");
5595 verifyFormat("my_int a = (my_int)1;");
5596 verifyFormat("my_int a = (my_int *)1;");
5597 verifyFormat("my_int a = (const my_int)-1;");
5598 verifyFormat("my_int a = (const my_int *)-1;");
5599 verifyFormat("my_int a = (my_int)(my_int)-1;");
5600
5601 // FIXME: single value wrapped with paren will be treated as cast.
5602 verifyFormat("void f(int i = (kValue)*kMask) {}");
5603
5604 verifyFormat("{ (void)F; }");
5605
5606 // Don't break after a cast's
5607 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5608 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
5609 " bbbbbbbbbbbbbbbbbbbbbb);");
5610
5611 // These are not casts.
5612 verifyFormat("void f(int *) {}");
5613 verifyFormat("f(foo)->b;");
5614 verifyFormat("f(foo).b;");
5615 verifyFormat("f(foo)(b);");
5616 verifyFormat("f(foo)[b];");
5617 verifyFormat("[](foo) { return 4; }(bar);");
5618 verifyFormat("(*funptr)(foo)[4];");
5619 verifyFormat("funptrs[4](foo)[4];");
5620 verifyFormat("void f(int *);");
5621 verifyFormat("void f(int *) = 0;");
5622 verifyFormat("void f(SmallVector<int>) {}");
5623 verifyFormat("void f(SmallVector<int>);");
5624 verifyFormat("void f(SmallVector<int>) = 0;");
5625 verifyFormat("void f(int i = (kA * kB) & kMask) {}");
5626 verifyFormat("int a = sizeof(int) * b;");
5627 verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
5628 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
5629 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
5630 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
5631
5632 // These are not casts, but at some point were confused with casts.
5633 verifyFormat("virtual void foo(int *) override;");
5634 verifyFormat("virtual void foo(char &) const;");
5635 verifyFormat("virtual void foo(int *a, char *) const;");
5636 verifyFormat("int a = sizeof(int *) + b;");
5637 verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
5638
5639 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
5640 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
5641 // FIXME: The indentation here is not ideal.
5642 verifyFormat(
5643 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5644 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
5645 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
5646 }
5647
TEST_F(FormatTest,FormatsFunctionTypes)5648 TEST_F(FormatTest, FormatsFunctionTypes) {
5649 verifyFormat("A<bool()> a;");
5650 verifyFormat("A<SomeType()> a;");
5651 verifyFormat("A<void (*)(int, std::string)> a;");
5652 verifyFormat("A<void *(int)>;");
5653 verifyFormat("void *(*a)(int *, SomeType *);");
5654 verifyFormat("int (*func)(void *);");
5655 verifyFormat("void f() { int (*func)(void *); }");
5656 verifyFormat("template <class CallbackClass>\n"
5657 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
5658
5659 verifyGoogleFormat("A<void*(int*, SomeType*)>;");
5660 verifyGoogleFormat("void* (*a)(int);");
5661 verifyGoogleFormat(
5662 "template <class CallbackClass>\n"
5663 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
5664
5665 // Other constructs can look somewhat like function types:
5666 verifyFormat("A<sizeof(*x)> a;");
5667 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
5668 verifyFormat("some_var = function(*some_pointer_var)[0];");
5669 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
5670 }
5671
TEST_F(FormatTest,FormatsPointersToArrayTypes)5672 TEST_F(FormatTest, FormatsPointersToArrayTypes) {
5673 verifyFormat("A (*foo_)[6];");
5674 verifyFormat("vector<int> (*foo_)[6];");
5675 }
5676
TEST_F(FormatTest,BreaksLongVariableDeclarations)5677 TEST_F(FormatTest, BreaksLongVariableDeclarations) {
5678 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5679 " LoooooooooooooooooooooooooooooooooooooooongVariable;");
5680 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
5681 " LoooooooooooooooooooooooooooooooooooooooongVariable;");
5682 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5683 " *LoooooooooooooooooooooooooooooooooooooooongVariable;");
5684
5685 // Different ways of ()-initializiation.
5686 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5687 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
5688 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5689 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
5690 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5691 " LoooooooooooooooooooooooooooooooooooooooongVariable({});");
5692 }
5693
TEST_F(FormatTest,BreaksLongDeclarations)5694 TEST_F(FormatTest, BreaksLongDeclarations) {
5695 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
5696 " AnotherNameForTheLongType;");
5697 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
5698 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5699 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5700 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
5701 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
5702 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
5703 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5704 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5705 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
5706 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5707 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
5708 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5709 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
5710 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5711 FormatStyle Indented = getLLVMStyle();
5712 Indented.IndentWrappedFunctionNames = true;
5713 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5714 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
5715 Indented);
5716 verifyFormat(
5717 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5718 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
5719 Indented);
5720 verifyFormat(
5721 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
5722 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
5723 Indented);
5724 verifyFormat(
5725 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
5726 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
5727 Indented);
5728
5729 // FIXME: Without the comment, this breaks after "(".
5730 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n"
5731 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
5732 getGoogleStyle());
5733
5734 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
5735 " int LoooooooooooooooooooongParam2) {}");
5736 verifyFormat(
5737 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
5738 " SourceLocation L, IdentifierIn *II,\n"
5739 " Type *T) {}");
5740 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
5741 "ReallyReaaallyLongFunctionName(\n"
5742 " const std::string &SomeParameter,\n"
5743 " const SomeType<string, SomeOtherTemplateParameter>\n"
5744 " &ReallyReallyLongParameterName,\n"
5745 " const SomeType<string, SomeOtherTemplateParameter>\n"
5746 " &AnotherLongParameterName) {}");
5747 verifyFormat("template <typename A>\n"
5748 "SomeLoooooooooooooooooooooongType<\n"
5749 " typename some_namespace::SomeOtherType<A>::Type>\n"
5750 "Function() {}");
5751
5752 verifyGoogleFormat(
5753 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
5754 " aaaaaaaaaaaaaaaaaaaaaaa;");
5755 verifyGoogleFormat(
5756 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
5757 " SourceLocation L) {}");
5758 verifyGoogleFormat(
5759 "some_namespace::LongReturnType\n"
5760 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
5761 " int first_long_parameter, int second_parameter) {}");
5762
5763 verifyGoogleFormat("template <typename T>\n"
5764 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
5765 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
5766 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5767 " int aaaaaaaaaaaaaaaaaaaaaaa);");
5768
5769 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
5770 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5771 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5772 }
5773
TEST_F(FormatTest,FormatsArrays)5774 TEST_F(FormatTest, FormatsArrays) {
5775 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
5776 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
5777 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5778 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
5779 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5780 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
5781 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5782 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
5783 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
5784 verifyFormat(
5785 "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
5786 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
5787 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
5788
5789 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
5790 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
5791 verifyFormat(
5792 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
5793 " .aaaaaaa[0]\n"
5794 " .aaaaaaaaaaaaaaaaaaaaaa();");
5795
5796 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
5797 }
5798
TEST_F(FormatTest,LineStartsWithSpecialCharacter)5799 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
5800 verifyFormat("(a)->b();");
5801 verifyFormat("--a;");
5802 }
5803
TEST_F(FormatTest,HandlesIncludeDirectives)5804 TEST_F(FormatTest, HandlesIncludeDirectives) {
5805 verifyFormat("#include <string>\n"
5806 "#include <a/b/c.h>\n"
5807 "#include \"a/b/string\"\n"
5808 "#include \"string.h\"\n"
5809 "#include \"string.h\"\n"
5810 "#include <a-a>\n"
5811 "#include < path with space >\n"
5812 "#include \"abc.h\" // this is included for ABC\n"
5813 "#include \"some long include\" // with a comment\n"
5814 "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"",
5815 getLLVMStyleWithColumns(35));
5816 EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\""));
5817 EXPECT_EQ("#include <a>", format("#include<a>"));
5818
5819 verifyFormat("#import <string>");
5820 verifyFormat("#import <a/b/c.h>");
5821 verifyFormat("#import \"a/b/string\"");
5822 verifyFormat("#import \"string.h\"");
5823 verifyFormat("#import \"string.h\"");
5824 verifyFormat("#if __has_include(<strstream>)\n"
5825 "#include <strstream>\n"
5826 "#endif");
5827
5828 verifyFormat("#define MY_IMPORT <a/b>");
5829
5830 // Protocol buffer definition or missing "#".
5831 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
5832 getLLVMStyleWithColumns(30));
5833
5834 FormatStyle Style = getLLVMStyle();
5835 Style.AlwaysBreakBeforeMultilineStrings = true;
5836 Style.ColumnLimit = 0;
5837 verifyFormat("#import \"abc.h\"", Style);
5838
5839 // But 'import' might also be a regular C++ namespace.
5840 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5841 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5842 }
5843
5844 //===----------------------------------------------------------------------===//
5845 // Error recovery tests.
5846 //===----------------------------------------------------------------------===//
5847
TEST_F(FormatTest,IncompleteParameterLists)5848 TEST_F(FormatTest, IncompleteParameterLists) {
5849 FormatStyle NoBinPacking = getLLVMStyle();
5850 NoBinPacking.BinPackParameters = false;
5851 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
5852 " double *min_x,\n"
5853 " double *max_x,\n"
5854 " double *min_y,\n"
5855 " double *max_y,\n"
5856 " double *min_z,\n"
5857 " double *max_z, ) {}",
5858 NoBinPacking);
5859 }
5860
TEST_F(FormatTest,IncorrectCodeTrailingStuff)5861 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
5862 verifyFormat("void f() { return; }\n42");
5863 verifyFormat("void f() {\n"
5864 " if (0)\n"
5865 " return;\n"
5866 "}\n"
5867 "42");
5868 verifyFormat("void f() { return }\n42");
5869 verifyFormat("void f() {\n"
5870 " if (0)\n"
5871 " return\n"
5872 "}\n"
5873 "42");
5874 }
5875
TEST_F(FormatTest,IncorrectCodeMissingSemicolon)5876 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
5877 EXPECT_EQ("void f() { return }", format("void f ( ) { return }"));
5878 EXPECT_EQ("void f() {\n"
5879 " if (a)\n"
5880 " return\n"
5881 "}",
5882 format("void f ( ) { if ( a ) return }"));
5883 EXPECT_EQ("namespace N {\n"
5884 "void f()\n"
5885 "}",
5886 format("namespace N { void f() }"));
5887 EXPECT_EQ("namespace N {\n"
5888 "void f() {}\n"
5889 "void g()\n"
5890 "}",
5891 format("namespace N { void f( ) { } void g( ) }"));
5892 }
5893
TEST_F(FormatTest,IndentationWithinColumnLimitNotPossible)5894 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
5895 verifyFormat("int aaaaaaaa =\n"
5896 " // Overlylongcomment\n"
5897 " b;",
5898 getLLVMStyleWithColumns(20));
5899 verifyFormat("function(\n"
5900 " ShortArgument,\n"
5901 " LoooooooooooongArgument);\n",
5902 getLLVMStyleWithColumns(20));
5903 }
5904
TEST_F(FormatTest,IncorrectAccessSpecifier)5905 TEST_F(FormatTest, IncorrectAccessSpecifier) {
5906 verifyFormat("public:");
5907 verifyFormat("class A {\n"
5908 "public\n"
5909 " void f() {}\n"
5910 "};");
5911 verifyFormat("public\n"
5912 "int qwerty;");
5913 verifyFormat("public\n"
5914 "B {}");
5915 verifyFormat("public\n"
5916 "{}");
5917 verifyFormat("public\n"
5918 "B { int x; }");
5919 }
5920
TEST_F(FormatTest,IncorrectCodeUnbalancedBraces)5921 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
5922 verifyFormat("{");
5923 verifyFormat("#})");
5924 verifyNoCrash("(/**/[:!] ?[).");
5925 }
5926
TEST_F(FormatTest,IncorrectCodeDoNoWhile)5927 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
5928 verifyFormat("do {\n}");
5929 verifyFormat("do {\n}\n"
5930 "f();");
5931 verifyFormat("do {\n}\n"
5932 "wheeee(fun);");
5933 verifyFormat("do {\n"
5934 " f();\n"
5935 "}");
5936 }
5937
TEST_F(FormatTest,IncorrectCodeMissingParens)5938 TEST_F(FormatTest, IncorrectCodeMissingParens) {
5939 verifyFormat("if {\n foo;\n foo();\n}");
5940 verifyFormat("switch {\n foo;\n foo();\n}");
5941 verifyFormat("for {\n foo;\n foo();\n}");
5942 verifyFormat("while {\n foo;\n foo();\n}");
5943 verifyFormat("do {\n foo;\n foo();\n} while;");
5944 }
5945
TEST_F(FormatTest,DoesNotTouchUnwrappedLinesWithErrors)5946 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
5947 verifyFormat("namespace {\n"
5948 "class Foo { Foo (\n"
5949 "};\n"
5950 "} // comment");
5951 }
5952
TEST_F(FormatTest,IncorrectCodeErrorDetection)5953 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
5954 EXPECT_EQ("{\n {}\n", format("{\n{\n}\n"));
5955 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n"));
5956 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n"));
5957 EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n"));
5958
5959 EXPECT_EQ("{\n"
5960 " {\n"
5961 " breakme(\n"
5962 " qwe);\n"
5963 " }\n",
5964 format("{\n"
5965 " {\n"
5966 " breakme(qwe);\n"
5967 "}\n",
5968 getLLVMStyleWithColumns(10)));
5969 }
5970
TEST_F(FormatTest,LayoutCallsInsideBraceInitializers)5971 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
5972 verifyFormat("int x = {\n"
5973 " avariable,\n"
5974 " b(alongervariable)};",
5975 getLLVMStyleWithColumns(25));
5976 }
5977
TEST_F(FormatTest,LayoutBraceInitializersInReturnStatement)5978 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
5979 verifyFormat("return (a)(b){1, 2, 3};");
5980 }
5981
TEST_F(FormatTest,LayoutCxx11BraceInitializers)5982 TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
5983 verifyFormat("vector<int> x{1, 2, 3, 4};");
5984 verifyFormat("vector<int> x{\n"
5985 " 1, 2, 3, 4,\n"
5986 "};");
5987 verifyFormat("vector<T> x{{}, {}, {}, {}};");
5988 verifyFormat("f({1, 2});");
5989 verifyFormat("auto v = Foo{-1};");
5990 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
5991 verifyFormat("Class::Class : member{1, 2, 3} {}");
5992 verifyFormat("new vector<int>{1, 2, 3};");
5993 verifyFormat("new int[3]{1, 2, 3};");
5994 verifyFormat("new int{1};");
5995 verifyFormat("return {arg1, arg2};");
5996 verifyFormat("return {arg1, SomeType{parameter}};");
5997 verifyFormat("int count = set<int>{f(), g(), h()}.size();");
5998 verifyFormat("new T{arg1, arg2};");
5999 verifyFormat("f(MyMap[{composite, key}]);");
6000 verifyFormat("class Class {\n"
6001 " T member = {arg1, arg2};\n"
6002 "};");
6003 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
6004 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
6005 verifyFormat("int a = std::is_integral<int>{} + 0;");
6006
6007 verifyFormat("int foo(int i) { return fo1{}(i); }");
6008 verifyFormat("int foo(int i) { return fo1{}(i); }");
6009 verifyFormat("auto i = decltype(x){};");
6010 verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
6011 verifyFormat("Node n{1, Node{1000}, //\n"
6012 " 2};");
6013 verifyFormat("Aaaa aaaaaaa{\n"
6014 " {\n"
6015 " aaaa,\n"
6016 " },\n"
6017 "};");
6018
6019 // In combination with BinPackParameters = false.
6020 FormatStyle NoBinPacking = getLLVMStyle();
6021 NoBinPacking.BinPackParameters = false;
6022 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
6023 " bbbbb,\n"
6024 " ccccc,\n"
6025 " ddddd,\n"
6026 " eeeee,\n"
6027 " ffffff,\n"
6028 " ggggg,\n"
6029 " hhhhhh,\n"
6030 " iiiiii,\n"
6031 " jjjjjj,\n"
6032 " kkkkkk};",
6033 NoBinPacking);
6034 verifyFormat("const Aaaaaa aaaaa = {\n"
6035 " aaaaa,\n"
6036 " bbbbb,\n"
6037 " ccccc,\n"
6038 " ddddd,\n"
6039 " eeeee,\n"
6040 " ffffff,\n"
6041 " ggggg,\n"
6042 " hhhhhh,\n"
6043 " iiiiii,\n"
6044 " jjjjjj,\n"
6045 " kkkkkk,\n"
6046 "};",
6047 NoBinPacking);
6048 verifyFormat(
6049 "const Aaaaaa aaaaa = {\n"
6050 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n"
6051 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n"
6052 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
6053 "};",
6054 NoBinPacking);
6055
6056 // FIXME: The alignment of these trailing comments might be bad. Then again,
6057 // this might be utterly useless in real code.
6058 verifyFormat("Constructor::Constructor()\n"
6059 " : some_value{ //\n"
6060 " aaaaaaa, //\n"
6061 " bbbbbbb} {}");
6062
6063 // In braced lists, the first comment is always assumed to belong to the
6064 // first element. Thus, it can be moved to the next or previous line as
6065 // appropriate.
6066 EXPECT_EQ("function({// First element:\n"
6067 " 1,\n"
6068 " // Second element:\n"
6069 " 2});",
6070 format("function({\n"
6071 " // First element:\n"
6072 " 1,\n"
6073 " // Second element:\n"
6074 " 2});"));
6075 EXPECT_EQ("std::vector<int> MyNumbers{\n"
6076 " // First element:\n"
6077 " 1,\n"
6078 " // Second element:\n"
6079 " 2};",
6080 format("std::vector<int> MyNumbers{// First element:\n"
6081 " 1,\n"
6082 " // Second element:\n"
6083 " 2};",
6084 getLLVMStyleWithColumns(30)));
6085 // A trailing comma should still lead to an enforced line break.
6086 EXPECT_EQ("vector<int> SomeVector = {\n"
6087 " // aaa\n"
6088 " 1, 2,\n"
6089 "};",
6090 format("vector<int> SomeVector = { // aaa\n"
6091 " 1, 2, };"));
6092
6093 FormatStyle ExtraSpaces = getLLVMStyle();
6094 ExtraSpaces.Cpp11BracedListStyle = false;
6095 ExtraSpaces.ColumnLimit = 75;
6096 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
6097 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
6098 verifyFormat("f({ 1, 2 });", ExtraSpaces);
6099 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
6100 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
6101 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
6102 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
6103 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
6104 verifyFormat("return { arg1, arg2 };", ExtraSpaces);
6105 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
6106 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
6107 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
6108 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
6109 verifyFormat("class Class {\n"
6110 " T member = { arg1, arg2 };\n"
6111 "};",
6112 ExtraSpaces);
6113 verifyFormat(
6114 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6115 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
6116 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
6117 " bbbbbbbbbbbbbbbbbbbb, bbbbb };",
6118 ExtraSpaces);
6119 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
6120 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
6121 ExtraSpaces);
6122 verifyFormat(
6123 "someFunction(OtherParam,\n"
6124 " BracedList{ // comment 1 (Forcing interesting break)\n"
6125 " param1, param2,\n"
6126 " // comment 2\n"
6127 " param3, param4 });",
6128 ExtraSpaces);
6129 verifyFormat(
6130 "std::this_thread::sleep_for(\n"
6131 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
6132 ExtraSpaces);
6133 verifyFormat(
6134 "std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{\n"
6135 " aaaaaaa, aaaaaaaaaa, aaaaa, aaaaaaaaaaaaaaa, aaa, aaaaaaaaaa, a,\n"
6136 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaa,\n"
6137 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa, aaaaaaa, a};");
6138 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
6139 }
6140
TEST_F(FormatTest,FormatsBracedListsInColumnLayout)6141 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
6142 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6143 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6144 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6145 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6146 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6147 " 1, 22, 333, 4444, 55555, 666666, 7777777};");
6148 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6149 " // line comment\n"
6150 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6151 " 1, 22, 333, 4444, 55555,\n"
6152 " // line comment\n"
6153 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6154 " 1, 22, 333, 4444, 55555, 666666, 7777777};");
6155 verifyFormat(
6156 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6157 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6158 " 1, 22, 333, 4444, 55555, 666666, // comment\n"
6159 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
6160 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
6161 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
6162 " 7777777};");
6163 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6164 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6165 " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
6166 verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6167 " 1, 1, 1, 1};",
6168 getLLVMStyleWithColumns(39));
6169 verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6170 " 1, 1, 1, 1};",
6171 getLLVMStyleWithColumns(38));
6172 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
6173 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
6174 getLLVMStyleWithColumns(43));
6175
6176 // Trailing commas.
6177 verifyFormat("vector<int> x = {\n"
6178 " 1, 1, 1, 1, 1, 1, 1, 1,\n"
6179 "};",
6180 getLLVMStyleWithColumns(39));
6181 verifyFormat("vector<int> x = {\n"
6182 " 1, 1, 1, 1, 1, 1, 1, 1, //\n"
6183 "};",
6184 getLLVMStyleWithColumns(39));
6185 verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6186 " 1, 1, 1, 1,\n"
6187 " /**/ /**/};",
6188 getLLVMStyleWithColumns(39));
6189 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
6190 " {aaaaaaaaaaaaaaaaaaa},\n"
6191 " {aaaaaaaaaaaaaaaaaaaaa},\n"
6192 " {aaaaaaaaaaaaaaaaa}};",
6193 getLLVMStyleWithColumns(60));
6194
6195 // With nested lists, we should either format one item per line or all nested
6196 // lists one one line.
6197 // FIXME: For some nested lists, we can do better.
6198 verifyFormat(
6199 "SomeStruct my_struct_array = {\n"
6200 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
6201 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
6202 " {aaa, aaa},\n"
6203 " {aaa, aaa},\n"
6204 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
6205 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6206 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
6207
6208 // No column layout should be used here.
6209 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
6210 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
6211
6212 verifyNoCrash("a<,");
6213 }
6214
TEST_F(FormatTest,PullTrivialFunctionDefinitionsIntoSingleLine)6215 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
6216 FormatStyle DoNotMerge = getLLVMStyle();
6217 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
6218
6219 verifyFormat("void f() { return 42; }");
6220 verifyFormat("void f() {\n"
6221 " return 42;\n"
6222 "}",
6223 DoNotMerge);
6224 verifyFormat("void f() {\n"
6225 " // Comment\n"
6226 "}");
6227 verifyFormat("{\n"
6228 "#error {\n"
6229 " int a;\n"
6230 "}");
6231 verifyFormat("{\n"
6232 " int a;\n"
6233 "#error {\n"
6234 "}");
6235 verifyFormat("void f() {} // comment");
6236 verifyFormat("void f() { int a; } // comment");
6237 verifyFormat("void f() {\n"
6238 "} // comment",
6239 DoNotMerge);
6240 verifyFormat("void f() {\n"
6241 " int a;\n"
6242 "} // comment",
6243 DoNotMerge);
6244 verifyFormat("void f() {\n"
6245 "} // comment",
6246 getLLVMStyleWithColumns(15));
6247
6248 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
6249 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22));
6250
6251 verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
6252 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
6253 verifyFormat("class C {\n"
6254 " C()\n"
6255 " : iiiiiiii(nullptr),\n"
6256 " kkkkkkk(nullptr),\n"
6257 " mmmmmmm(nullptr),\n"
6258 " nnnnnnn(nullptr) {}\n"
6259 "};",
6260 getGoogleStyle());
6261
6262 FormatStyle NoColumnLimit = getLLVMStyle();
6263 NoColumnLimit.ColumnLimit = 0;
6264 EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
6265 EXPECT_EQ("class C {\n"
6266 " A() : b(0) {}\n"
6267 "};", format("class C{A():b(0){}};", NoColumnLimit));
6268 EXPECT_EQ("A()\n"
6269 " : b(0) {\n"
6270 "}",
6271 format("A()\n:b(0)\n{\n}", NoColumnLimit));
6272
6273 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
6274 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
6275 FormatStyle::SFS_None;
6276 EXPECT_EQ("A()\n"
6277 " : b(0) {\n"
6278 "}",
6279 format("A():b(0){}", DoNotMergeNoColumnLimit));
6280 EXPECT_EQ("A()\n"
6281 " : b(0) {\n"
6282 "}",
6283 format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
6284
6285 verifyFormat("#define A \\\n"
6286 " void f() { \\\n"
6287 " int i; \\\n"
6288 " }",
6289 getLLVMStyleWithColumns(20));
6290 verifyFormat("#define A \\\n"
6291 " void f() { int i; }",
6292 getLLVMStyleWithColumns(21));
6293 verifyFormat("#define A \\\n"
6294 " void f() { \\\n"
6295 " int i; \\\n"
6296 " } \\\n"
6297 " int j;",
6298 getLLVMStyleWithColumns(22));
6299 verifyFormat("#define A \\\n"
6300 " void f() { int i; } \\\n"
6301 " int j;",
6302 getLLVMStyleWithColumns(23));
6303 }
6304
TEST_F(FormatTest,PullInlineFunctionDefinitionsIntoSingleLine)6305 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
6306 FormatStyle MergeInlineOnly = getLLVMStyle();
6307 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
6308 verifyFormat("class C {\n"
6309 " int f() { return 42; }\n"
6310 "};",
6311 MergeInlineOnly);
6312 verifyFormat("int f() {\n"
6313 " return 42;\n"
6314 "}",
6315 MergeInlineOnly);
6316 }
6317
TEST_F(FormatTest,UnderstandContextOfRecordTypeKeywords)6318 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
6319 // Elaborate type variable declarations.
6320 verifyFormat("struct foo a = {bar};\nint n;");
6321 verifyFormat("class foo a = {bar};\nint n;");
6322 verifyFormat("union foo a = {bar};\nint n;");
6323
6324 // Elaborate types inside function definitions.
6325 verifyFormat("struct foo f() {}\nint n;");
6326 verifyFormat("class foo f() {}\nint n;");
6327 verifyFormat("union foo f() {}\nint n;");
6328
6329 // Templates.
6330 verifyFormat("template <class X> void f() {}\nint n;");
6331 verifyFormat("template <struct X> void f() {}\nint n;");
6332 verifyFormat("template <union X> void f() {}\nint n;");
6333
6334 // Actual definitions...
6335 verifyFormat("struct {\n} n;");
6336 verifyFormat(
6337 "template <template <class T, class Y>, class Z> class X {\n} n;");
6338 verifyFormat("union Z {\n int n;\n} x;");
6339 verifyFormat("class MACRO Z {\n} n;");
6340 verifyFormat("class MACRO(X) Z {\n} n;");
6341 verifyFormat("class __attribute__(X) Z {\n} n;");
6342 verifyFormat("class __declspec(X) Z {\n} n;");
6343 verifyFormat("class A##B##C {\n} n;");
6344 verifyFormat("class alignas(16) Z {\n} n;");
6345
6346 // Redefinition from nested context:
6347 verifyFormat("class A::B::C {\n} n;");
6348
6349 // Template definitions.
6350 verifyFormat(
6351 "template <typename F>\n"
6352 "Matcher(const Matcher<F> &Other,\n"
6353 " typename enable_if_c<is_base_of<F, T>::value &&\n"
6354 " !is_same<F, T>::value>::type * = 0)\n"
6355 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
6356
6357 // FIXME: This is still incorrectly handled at the formatter side.
6358 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
6359
6360 // FIXME:
6361 // This now gets parsed incorrectly as class definition.
6362 // verifyFormat("class A<int> f() {\n}\nint n;");
6363
6364 // Elaborate types where incorrectly parsing the structural element would
6365 // break the indent.
6366 verifyFormat("if (true)\n"
6367 " class X x;\n"
6368 "else\n"
6369 " f();\n");
6370
6371 // This is simply incomplete. Formatting is not important, but must not crash.
6372 verifyFormat("class A:");
6373 }
6374
TEST_F(FormatTest,DoNotInterfereWithErrorAndWarning)6375 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
6376 EXPECT_EQ("#error Leave all white!!!!! space* alone!\n",
6377 format("#error Leave all white!!!!! space* alone!\n"));
6378 EXPECT_EQ(
6379 "#warning Leave all white!!!!! space* alone!\n",
6380 format("#warning Leave all white!!!!! space* alone!\n"));
6381 EXPECT_EQ("#error 1", format(" # error 1"));
6382 EXPECT_EQ("#warning 1", format(" # warning 1"));
6383 }
6384
TEST_F(FormatTest,FormatHashIfExpressions)6385 TEST_F(FormatTest, FormatHashIfExpressions) {
6386 verifyFormat("#if AAAA && BBBB");
6387 // FIXME: Come up with a better indentation for #elif.
6388 verifyFormat(
6389 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n"
6390 " defined(BBBBBBBB)\n"
6391 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n"
6392 " defined(BBBBBBBB)\n"
6393 "#endif",
6394 getLLVMStyleWithColumns(65));
6395 }
6396
TEST_F(FormatTest,MergeHandlingInTheFaceOfPreprocessorDirectives)6397 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
6398 FormatStyle AllowsMergedIf = getGoogleStyle();
6399 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
6400 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
6401 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
6402 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf);
6403 EXPECT_EQ("if (true) return 42;",
6404 format("if (true)\nreturn 42;", AllowsMergedIf));
6405 FormatStyle ShortMergedIf = AllowsMergedIf;
6406 ShortMergedIf.ColumnLimit = 25;
6407 verifyFormat("#define A \\\n"
6408 " if (true) return 42;",
6409 ShortMergedIf);
6410 verifyFormat("#define A \\\n"
6411 " f(); \\\n"
6412 " if (true)\n"
6413 "#define B",
6414 ShortMergedIf);
6415 verifyFormat("#define A \\\n"
6416 " f(); \\\n"
6417 " if (true)\n"
6418 "g();",
6419 ShortMergedIf);
6420 verifyFormat("{\n"
6421 "#ifdef A\n"
6422 " // Comment\n"
6423 " if (true) continue;\n"
6424 "#endif\n"
6425 " // Comment\n"
6426 " if (true) continue;\n"
6427 "}",
6428 ShortMergedIf);
6429 ShortMergedIf.ColumnLimit = 29;
6430 verifyFormat("#define A \\\n"
6431 " if (aaaaaaaaaa) return 1; \\\n"
6432 " return 2;",
6433 ShortMergedIf);
6434 ShortMergedIf.ColumnLimit = 28;
6435 verifyFormat("#define A \\\n"
6436 " if (aaaaaaaaaa) \\\n"
6437 " return 1; \\\n"
6438 " return 2;",
6439 ShortMergedIf);
6440 }
6441
TEST_F(FormatTest,BlockCommentsInControlLoops)6442 TEST_F(FormatTest, BlockCommentsInControlLoops) {
6443 verifyFormat("if (0) /* a comment in a strange place */ {\n"
6444 " f();\n"
6445 "}");
6446 verifyFormat("if (0) /* a comment in a strange place */ {\n"
6447 " f();\n"
6448 "} /* another comment */ else /* comment #3 */ {\n"
6449 " g();\n"
6450 "}");
6451 verifyFormat("while (0) /* a comment in a strange place */ {\n"
6452 " f();\n"
6453 "}");
6454 verifyFormat("for (;;) /* a comment in a strange place */ {\n"
6455 " f();\n"
6456 "}");
6457 verifyFormat("do /* a comment in a strange place */ {\n"
6458 " f();\n"
6459 "} /* another comment */ while (0);");
6460 }
6461
TEST_F(FormatTest,BlockComments)6462 TEST_F(FormatTest, BlockComments) {
6463 EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */",
6464 format("/* *//* */ /* */\n/* *//* */ /* */"));
6465 EXPECT_EQ("/* */ a /* */ b;", format(" /* */ a/* */ b;"));
6466 EXPECT_EQ("#define A /*123*/ \\\n"
6467 " b\n"
6468 "/* */\n"
6469 "someCall(\n"
6470 " parameter);",
6471 format("#define A /*123*/ b\n"
6472 "/* */\n"
6473 "someCall(parameter);",
6474 getLLVMStyleWithColumns(15)));
6475
6476 EXPECT_EQ("#define A\n"
6477 "/* */ someCall(\n"
6478 " parameter);",
6479 format("#define A\n"
6480 "/* */someCall(parameter);",
6481 getLLVMStyleWithColumns(15)));
6482 EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/"));
6483 EXPECT_EQ("/*\n"
6484 "*\n"
6485 " * aaaaaa\n"
6486 "*aaaaaa\n"
6487 "*/",
6488 format("/*\n"
6489 "*\n"
6490 " * aaaaaa aaaaaa\n"
6491 "*/",
6492 getLLVMStyleWithColumns(10)));
6493 EXPECT_EQ("/*\n"
6494 "**\n"
6495 "* aaaaaa\n"
6496 "*aaaaaa\n"
6497 "*/",
6498 format("/*\n"
6499 "**\n"
6500 "* aaaaaa aaaaaa\n"
6501 "*/",
6502 getLLVMStyleWithColumns(10)));
6503
6504 FormatStyle NoBinPacking = getLLVMStyle();
6505 NoBinPacking.BinPackParameters = false;
6506 EXPECT_EQ("someFunction(1, /* comment 1 */\n"
6507 " 2, /* comment 2 */\n"
6508 " 3, /* comment 3 */\n"
6509 " aaaa,\n"
6510 " bbbb);",
6511 format("someFunction (1, /* comment 1 */\n"
6512 " 2, /* comment 2 */ \n"
6513 " 3, /* comment 3 */\n"
6514 "aaaa, bbbb );",
6515 NoBinPacking));
6516 verifyFormat(
6517 "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
6518 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
6519 EXPECT_EQ(
6520 "bool aaaaaaaaaaaaa = /* trailing comment */\n"
6521 " aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
6522 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;",
6523 format(
6524 "bool aaaaaaaaaaaaa = /* trailing comment */\n"
6525 " aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
6526 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;"));
6527 EXPECT_EQ(
6528 "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
6529 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
6530 "int cccccccccccccccccccccccccccccc; /* comment */\n",
6531 format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
6532 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
6533 "int cccccccccccccccccccccccccccccc; /* comment */\n"));
6534
6535 verifyFormat("void f(int * /* unused */) {}");
6536
6537 EXPECT_EQ("/*\n"
6538 " **\n"
6539 " */",
6540 format("/*\n"
6541 " **\n"
6542 " */"));
6543 EXPECT_EQ("/*\n"
6544 " *q\n"
6545 " */",
6546 format("/*\n"
6547 " *q\n"
6548 " */"));
6549 EXPECT_EQ("/*\n"
6550 " * q\n"
6551 " */",
6552 format("/*\n"
6553 " * q\n"
6554 " */"));
6555 EXPECT_EQ("/*\n"
6556 " **/",
6557 format("/*\n"
6558 " **/"));
6559 EXPECT_EQ("/*\n"
6560 " ***/",
6561 format("/*\n"
6562 " ***/"));
6563 }
6564
TEST_F(FormatTest,BlockCommentsInMacros)6565 TEST_F(FormatTest, BlockCommentsInMacros) {
6566 EXPECT_EQ("#define A \\\n"
6567 " { \\\n"
6568 " /* one line */ \\\n"
6569 " someCall();",
6570 format("#define A { \\\n"
6571 " /* one line */ \\\n"
6572 " someCall();",
6573 getLLVMStyleWithColumns(20)));
6574 EXPECT_EQ("#define A \\\n"
6575 " { \\\n"
6576 " /* previous */ \\\n"
6577 " /* one line */ \\\n"
6578 " someCall();",
6579 format("#define A { \\\n"
6580 " /* previous */ \\\n"
6581 " /* one line */ \\\n"
6582 " someCall();",
6583 getLLVMStyleWithColumns(20)));
6584 }
6585
TEST_F(FormatTest,BlockCommentsAtEndOfLine)6586 TEST_F(FormatTest, BlockCommentsAtEndOfLine) {
6587 EXPECT_EQ("a = {\n"
6588 " 1111 /* */\n"
6589 "};",
6590 format("a = {1111 /* */\n"
6591 "};",
6592 getLLVMStyleWithColumns(15)));
6593 EXPECT_EQ("a = {\n"
6594 " 1111 /* */\n"
6595 "};",
6596 format("a = {1111 /* */\n"
6597 "};",
6598 getLLVMStyleWithColumns(15)));
6599
6600 // FIXME: The formatting is still wrong here.
6601 EXPECT_EQ("a = {\n"
6602 " 1111 /* a\n"
6603 " */\n"
6604 "};",
6605 format("a = {1111 /* a */\n"
6606 "};",
6607 getLLVMStyleWithColumns(15)));
6608 }
6609
TEST_F(FormatTest,IndentLineCommentsInStartOfBlockAtEndOfFile)6610 TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) {
6611 // FIXME: This is not what we want...
6612 verifyFormat("{\n"
6613 "// a"
6614 "// b");
6615 }
6616
TEST_F(FormatTest,FormatStarDependingOnContext)6617 TEST_F(FormatTest, FormatStarDependingOnContext) {
6618 verifyFormat("void f(int *a);");
6619 verifyFormat("void f() { f(fint * b); }");
6620 verifyFormat("class A {\n void f(int *a);\n};");
6621 verifyFormat("class A {\n int *a;\n};");
6622 verifyFormat("namespace a {\n"
6623 "namespace b {\n"
6624 "class A {\n"
6625 " void f() {}\n"
6626 " int *a;\n"
6627 "};\n"
6628 "}\n"
6629 "}");
6630 }
6631
TEST_F(FormatTest,SpecialTokensAtEndOfLine)6632 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
6633 verifyFormat("while");
6634 verifyFormat("operator");
6635 }
6636
6637 //===----------------------------------------------------------------------===//
6638 // Objective-C tests.
6639 //===----------------------------------------------------------------------===//
6640
TEST_F(FormatTest,FormatForObjectiveCMethodDecls)6641 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
6642 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
6643 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
6644 format("-(NSUInteger)indexOfObject:(id)anObject;"));
6645 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
6646 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
6647 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
6648 format("-(NSInteger)Method3:(id)anObject;"));
6649 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
6650 format("-(NSInteger)Method4:(id)anObject;"));
6651 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
6652 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
6653 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
6654 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
6655 EXPECT_EQ(
6656 "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;",
6657 format(
6658 "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;"));
6659
6660 // Very long objectiveC method declaration.
6661 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
6662 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
6663 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
6664 " inRange:(NSRange)range\n"
6665 " outRange:(NSRange)out_range\n"
6666 " outRange1:(NSRange)out_range1\n"
6667 " outRange2:(NSRange)out_range2\n"
6668 " outRange3:(NSRange)out_range3\n"
6669 " outRange4:(NSRange)out_range4\n"
6670 " outRange5:(NSRange)out_range5\n"
6671 " outRange6:(NSRange)out_range6\n"
6672 " outRange7:(NSRange)out_range7\n"
6673 " outRange8:(NSRange)out_range8\n"
6674 " outRange9:(NSRange)out_range9;");
6675
6676 verifyFormat("- (int)sum:(vector<int>)numbers;");
6677 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
6678 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
6679 // protocol lists (but not for template classes):
6680 //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
6681
6682 verifyFormat("- (int (*)())foo:(int (*)())f;");
6683 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
6684
6685 // If there's no return type (very rare in practice!), LLVM and Google style
6686 // agree.
6687 verifyFormat("- foo;");
6688 verifyFormat("- foo:(int)f;");
6689 verifyGoogleFormat("- foo:(int)foo;");
6690 }
6691
TEST_F(FormatTest,FormatObjCInterface)6692 TEST_F(FormatTest, FormatObjCInterface) {
6693 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
6694 "@public\n"
6695 " int field1;\n"
6696 "@protected\n"
6697 " int field2;\n"
6698 "@private\n"
6699 " int field3;\n"
6700 "@package\n"
6701 " int field4;\n"
6702 "}\n"
6703 "+ (id)init;\n"
6704 "@end");
6705
6706 verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
6707 " @public\n"
6708 " int field1;\n"
6709 " @protected\n"
6710 " int field2;\n"
6711 " @private\n"
6712 " int field3;\n"
6713 " @package\n"
6714 " int field4;\n"
6715 "}\n"
6716 "+ (id)init;\n"
6717 "@end");
6718
6719 verifyFormat("@interface /* wait for it */ Foo\n"
6720 "+ (id)init;\n"
6721 "// Look, a comment!\n"
6722 "- (int)answerWith:(int)i;\n"
6723 "@end");
6724
6725 verifyFormat("@interface Foo\n"
6726 "@end\n"
6727 "@interface Bar\n"
6728 "@end");
6729
6730 verifyFormat("@interface Foo : Bar\n"
6731 "+ (id)init;\n"
6732 "@end");
6733
6734 verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n"
6735 "+ (id)init;\n"
6736 "@end");
6737
6738 verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
6739 "+ (id)init;\n"
6740 "@end");
6741
6742 verifyFormat("@interface Foo (HackStuff)\n"
6743 "+ (id)init;\n"
6744 "@end");
6745
6746 verifyFormat("@interface Foo ()\n"
6747 "+ (id)init;\n"
6748 "@end");
6749
6750 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
6751 "+ (id)init;\n"
6752 "@end");
6753
6754 verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
6755 "+ (id)init;\n"
6756 "@end");
6757
6758 verifyFormat("@interface Foo {\n"
6759 " int _i;\n"
6760 "}\n"
6761 "+ (id)init;\n"
6762 "@end");
6763
6764 verifyFormat("@interface Foo : Bar {\n"
6765 " int _i;\n"
6766 "}\n"
6767 "+ (id)init;\n"
6768 "@end");
6769
6770 verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
6771 " int _i;\n"
6772 "}\n"
6773 "+ (id)init;\n"
6774 "@end");
6775
6776 verifyFormat("@interface Foo (HackStuff) {\n"
6777 " int _i;\n"
6778 "}\n"
6779 "+ (id)init;\n"
6780 "@end");
6781
6782 verifyFormat("@interface Foo () {\n"
6783 " int _i;\n"
6784 "}\n"
6785 "+ (id)init;\n"
6786 "@end");
6787
6788 verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
6789 " int _i;\n"
6790 "}\n"
6791 "+ (id)init;\n"
6792 "@end");
6793
6794 FormatStyle OnePerLine = getGoogleStyle();
6795 OnePerLine.BinPackParameters = false;
6796 verifyFormat("@interface aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ()<\n"
6797 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6798 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6799 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6800 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
6801 "}",
6802 OnePerLine);
6803 }
6804
TEST_F(FormatTest,FormatObjCImplementation)6805 TEST_F(FormatTest, FormatObjCImplementation) {
6806 verifyFormat("@implementation Foo : NSObject {\n"
6807 "@public\n"
6808 " int field1;\n"
6809 "@protected\n"
6810 " int field2;\n"
6811 "@private\n"
6812 " int field3;\n"
6813 "@package\n"
6814 " int field4;\n"
6815 "}\n"
6816 "+ (id)init {\n}\n"
6817 "@end");
6818
6819 verifyGoogleFormat("@implementation Foo : NSObject {\n"
6820 " @public\n"
6821 " int field1;\n"
6822 " @protected\n"
6823 " int field2;\n"
6824 " @private\n"
6825 " int field3;\n"
6826 " @package\n"
6827 " int field4;\n"
6828 "}\n"
6829 "+ (id)init {\n}\n"
6830 "@end");
6831
6832 verifyFormat("@implementation Foo\n"
6833 "+ (id)init {\n"
6834 " if (true)\n"
6835 " return nil;\n"
6836 "}\n"
6837 "// Look, a comment!\n"
6838 "- (int)answerWith:(int)i {\n"
6839 " return i;\n"
6840 "}\n"
6841 "+ (int)answerWith:(int)i {\n"
6842 " return i;\n"
6843 "}\n"
6844 "@end");
6845
6846 verifyFormat("@implementation Foo\n"
6847 "@end\n"
6848 "@implementation Bar\n"
6849 "@end");
6850
6851 EXPECT_EQ("@implementation Foo : Bar\n"
6852 "+ (id)init {\n}\n"
6853 "- (void)foo {\n}\n"
6854 "@end",
6855 format("@implementation Foo : Bar\n"
6856 "+(id)init{}\n"
6857 "-(void)foo{}\n"
6858 "@end"));
6859
6860 verifyFormat("@implementation Foo {\n"
6861 " int _i;\n"
6862 "}\n"
6863 "+ (id)init {\n}\n"
6864 "@end");
6865
6866 verifyFormat("@implementation Foo : Bar {\n"
6867 " int _i;\n"
6868 "}\n"
6869 "+ (id)init {\n}\n"
6870 "@end");
6871
6872 verifyFormat("@implementation Foo (HackStuff)\n"
6873 "+ (id)init {\n}\n"
6874 "@end");
6875 verifyFormat("@implementation ObjcClass\n"
6876 "- (void)method;\n"
6877 "{}\n"
6878 "@end");
6879 }
6880
TEST_F(FormatTest,FormatObjCProtocol)6881 TEST_F(FormatTest, FormatObjCProtocol) {
6882 verifyFormat("@protocol Foo\n"
6883 "@property(weak) id delegate;\n"
6884 "- (NSUInteger)numberOfThings;\n"
6885 "@end");
6886
6887 verifyFormat("@protocol MyProtocol <NSObject>\n"
6888 "- (NSUInteger)numberOfThings;\n"
6889 "@end");
6890
6891 verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
6892 "- (NSUInteger)numberOfThings;\n"
6893 "@end");
6894
6895 verifyFormat("@protocol Foo;\n"
6896 "@protocol Bar;\n");
6897
6898 verifyFormat("@protocol Foo\n"
6899 "@end\n"
6900 "@protocol Bar\n"
6901 "@end");
6902
6903 verifyFormat("@protocol myProtocol\n"
6904 "- (void)mandatoryWithInt:(int)i;\n"
6905 "@optional\n"
6906 "- (void)optional;\n"
6907 "@required\n"
6908 "- (void)required;\n"
6909 "@optional\n"
6910 "@property(assign) int madProp;\n"
6911 "@end\n");
6912
6913 verifyFormat("@property(nonatomic, assign, readonly)\n"
6914 " int *looooooooooooooooooooooooooooongNumber;\n"
6915 "@property(nonatomic, assign, readonly)\n"
6916 " NSString *looooooooooooooooooooooooooooongName;");
6917
6918 verifyFormat("@implementation PR18406\n"
6919 "}\n"
6920 "@end");
6921 }
6922
TEST_F(FormatTest,FormatObjCMethodDeclarations)6923 TEST_F(FormatTest, FormatObjCMethodDeclarations) {
6924 verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n"
6925 " rect:(NSRect)theRect\n"
6926 " interval:(float)theInterval {\n"
6927 "}");
6928 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
6929 " longKeyword:(NSRect)theRect\n"
6930 " evenLongerKeyword:(float)theInterval\n"
6931 " error:(NSError **)theError {\n"
6932 "}");
6933 verifyFormat("- (instancetype)initXxxxxx:(id<x>)x\n"
6934 " y:(id<yyyyyyyyyyyyyyyyyyyy>)y\n"
6935 " NS_DESIGNATED_INITIALIZER;",
6936 getLLVMStyleWithColumns(60));
6937 }
6938
TEST_F(FormatTest,FormatObjCMethodExpr)6939 TEST_F(FormatTest, FormatObjCMethodExpr) {
6940 verifyFormat("[foo bar:baz];");
6941 verifyFormat("return [foo bar:baz];");
6942 verifyFormat("return (a)[foo bar:baz];");
6943 verifyFormat("f([foo bar:baz]);");
6944 verifyFormat("f(2, [foo bar:baz]);");
6945 verifyFormat("f(2, a ? b : c);");
6946 verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
6947
6948 // Unary operators.
6949 verifyFormat("int a = +[foo bar:baz];");
6950 verifyFormat("int a = -[foo bar:baz];");
6951 verifyFormat("int a = ![foo bar:baz];");
6952 verifyFormat("int a = ~[foo bar:baz];");
6953 verifyFormat("int a = ++[foo bar:baz];");
6954 verifyFormat("int a = --[foo bar:baz];");
6955 verifyFormat("int a = sizeof [foo bar:baz];");
6956 verifyFormat("int a = alignof [foo bar:baz];", getGoogleStyle());
6957 verifyFormat("int a = &[foo bar:baz];");
6958 verifyFormat("int a = *[foo bar:baz];");
6959 // FIXME: Make casts work, without breaking f()[4].
6960 //verifyFormat("int a = (int)[foo bar:baz];");
6961 //verifyFormat("return (int)[foo bar:baz];");
6962 //verifyFormat("(void)[foo bar:baz];");
6963 verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];");
6964
6965 // Binary operators.
6966 verifyFormat("[foo bar:baz], [foo bar:baz];");
6967 verifyFormat("[foo bar:baz] = [foo bar:baz];");
6968 verifyFormat("[foo bar:baz] *= [foo bar:baz];");
6969 verifyFormat("[foo bar:baz] /= [foo bar:baz];");
6970 verifyFormat("[foo bar:baz] %= [foo bar:baz];");
6971 verifyFormat("[foo bar:baz] += [foo bar:baz];");
6972 verifyFormat("[foo bar:baz] -= [foo bar:baz];");
6973 verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
6974 verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
6975 verifyFormat("[foo bar:baz] &= [foo bar:baz];");
6976 verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
6977 verifyFormat("[foo bar:baz] |= [foo bar:baz];");
6978 verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
6979 verifyFormat("[foo bar:baz] || [foo bar:baz];");
6980 verifyFormat("[foo bar:baz] && [foo bar:baz];");
6981 verifyFormat("[foo bar:baz] | [foo bar:baz];");
6982 verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
6983 verifyFormat("[foo bar:baz] & [foo bar:baz];");
6984 verifyFormat("[foo bar:baz] == [foo bar:baz];");
6985 verifyFormat("[foo bar:baz] != [foo bar:baz];");
6986 verifyFormat("[foo bar:baz] >= [foo bar:baz];");
6987 verifyFormat("[foo bar:baz] <= [foo bar:baz];");
6988 verifyFormat("[foo bar:baz] > [foo bar:baz];");
6989 verifyFormat("[foo bar:baz] < [foo bar:baz];");
6990 verifyFormat("[foo bar:baz] >> [foo bar:baz];");
6991 verifyFormat("[foo bar:baz] << [foo bar:baz];");
6992 verifyFormat("[foo bar:baz] - [foo bar:baz];");
6993 verifyFormat("[foo bar:baz] + [foo bar:baz];");
6994 verifyFormat("[foo bar:baz] * [foo bar:baz];");
6995 verifyFormat("[foo bar:baz] / [foo bar:baz];");
6996 verifyFormat("[foo bar:baz] % [foo bar:baz];");
6997 // Whew!
6998
6999 verifyFormat("return in[42];");
7000 verifyFormat("for (auto v : in[1]) {\n}");
7001 verifyFormat("for (int i = 0; i < in[a]; ++i) {\n}");
7002 verifyFormat("for (int i = 0; in[a] < i; ++i) {\n}");
7003 verifyFormat("for (int i = 0; i < n; ++i, ++in[a]) {\n}");
7004 verifyFormat("for (int i = 0; i < n; ++i, in[a]++) {\n}");
7005 verifyFormat("for (int i = 0; i < f(in[a]); ++i, in[a]++) {\n}");
7006 verifyFormat("for (id foo in [self getStuffFor:bla]) {\n"
7007 "}");
7008 verifyFormat("[self aaaaa:MACRO(a, b:, c:)];");
7009
7010 verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
7011 verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
7012 verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
7013 verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
7014 verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
7015 verifyFormat("[button setAction:@selector(zoomOut:)];");
7016 verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
7017
7018 verifyFormat("arr[[self indexForFoo:a]];");
7019 verifyFormat("throw [self errorFor:a];");
7020 verifyFormat("@throw [self errorFor:a];");
7021
7022 verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];");
7023 verifyFormat("[(id)foo bar:(id) ? baz : quux];");
7024 verifyFormat("4 > 4 ? (id)a : (id)baz;");
7025
7026 // This tests that the formatter doesn't break after "backing" but before ":",
7027 // which would be at 80 columns.
7028 verifyFormat(
7029 "void f() {\n"
7030 " if ((self = [super initWithContentRect:contentRect\n"
7031 " styleMask:styleMask ?: otherMask\n"
7032 " backing:NSBackingStoreBuffered\n"
7033 " defer:YES]))");
7034
7035 verifyFormat(
7036 "[foo checkThatBreakingAfterColonWorksOk:\n"
7037 " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
7038
7039 verifyFormat("[myObj short:arg1 // Force line break\n"
7040 " longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n"
7041 " evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n"
7042 " error:arg4];");
7043 verifyFormat(
7044 "void f() {\n"
7045 " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
7046 " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
7047 " pos.width(), pos.height())\n"
7048 " styleMask:NSBorderlessWindowMask\n"
7049 " backing:NSBackingStoreBuffered\n"
7050 " defer:NO]);\n"
7051 "}");
7052 verifyFormat(
7053 "void f() {\n"
7054 " popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n"
7055 " iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n"
7056 " pos.width(), pos.height())\n"
7057 " syeMask:NSBorderlessWindowMask\n"
7058 " bking:NSBackingStoreBuffered\n"
7059 " der:NO]);\n"
7060 "}",
7061 getLLVMStyleWithColumns(70));
7062 verifyFormat(
7063 "void f() {\n"
7064 " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
7065 " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
7066 " pos.width(), pos.height())\n"
7067 " styleMask:NSBorderlessWindowMask\n"
7068 " backing:NSBackingStoreBuffered\n"
7069 " defer:NO]);\n"
7070 "}",
7071 getChromiumStyle(FormatStyle::LK_Cpp));
7072 verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n"
7073 " with:contentsNativeView];");
7074
7075 verifyFormat(
7076 "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n"
7077 " owner:nillllll];");
7078
7079 verifyFormat(
7080 "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n"
7081 " forType:kBookmarkButtonDragType];");
7082
7083 verifyFormat("[defaultCenter addObserver:self\n"
7084 " selector:@selector(willEnterFullscreen)\n"
7085 " name:kWillEnterFullscreenNotification\n"
7086 " object:nil];");
7087 verifyFormat("[image_rep drawInRect:drawRect\n"
7088 " fromRect:NSZeroRect\n"
7089 " operation:NSCompositeCopy\n"
7090 " fraction:1.0\n"
7091 " respectFlipped:NO\n"
7092 " hints:nil];");
7093
7094 verifyFormat(
7095 "scoped_nsobject<NSTextField> message(\n"
7096 " // The frame will be fixed up when |-setMessageText:| is called.\n"
7097 " [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);");
7098 verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n"
7099 " aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n"
7100 " aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n"
7101 " aaaa:bbb];");
7102 verifyFormat("[self param:function( //\n"
7103 " parameter)]");
7104 verifyFormat(
7105 "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
7106 " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
7107 " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];");
7108
7109 // Variadic parameters.
7110 verifyFormat(
7111 "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];");
7112 verifyFormat(
7113 "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
7114 " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
7115 " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];");
7116 verifyFormat("[self // break\n"
7117 " a:a\n"
7118 " aaa:aaa];");
7119 verifyFormat("bool a = ([aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaa ||\n"
7120 " [aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaaaaa);");
7121 }
7122
TEST_F(FormatTest,ObjCAt)7123 TEST_F(FormatTest, ObjCAt) {
7124 verifyFormat("@autoreleasepool");
7125 verifyFormat("@catch");
7126 verifyFormat("@class");
7127 verifyFormat("@compatibility_alias");
7128 verifyFormat("@defs");
7129 verifyFormat("@dynamic");
7130 verifyFormat("@encode");
7131 verifyFormat("@end");
7132 verifyFormat("@finally");
7133 verifyFormat("@implementation");
7134 verifyFormat("@import");
7135 verifyFormat("@interface");
7136 verifyFormat("@optional");
7137 verifyFormat("@package");
7138 verifyFormat("@private");
7139 verifyFormat("@property");
7140 verifyFormat("@protected");
7141 verifyFormat("@protocol");
7142 verifyFormat("@public");
7143 verifyFormat("@required");
7144 verifyFormat("@selector");
7145 verifyFormat("@synchronized");
7146 verifyFormat("@synthesize");
7147 verifyFormat("@throw");
7148 verifyFormat("@try");
7149
7150 EXPECT_EQ("@interface", format("@ interface"));
7151
7152 // The precise formatting of this doesn't matter, nobody writes code like
7153 // this.
7154 verifyFormat("@ /*foo*/ interface");
7155 }
7156
TEST_F(FormatTest,ObjCSnippets)7157 TEST_F(FormatTest, ObjCSnippets) {
7158 verifyFormat("@autoreleasepool {\n"
7159 " foo();\n"
7160 "}");
7161 verifyFormat("@class Foo, Bar;");
7162 verifyFormat("@compatibility_alias AliasName ExistingClass;");
7163 verifyFormat("@dynamic textColor;");
7164 verifyFormat("char *buf1 = @encode(int *);");
7165 verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
7166 verifyFormat("char *buf1 = @encode(int **);");
7167 verifyFormat("Protocol *proto = @protocol(p1);");
7168 verifyFormat("SEL s = @selector(foo:);");
7169 verifyFormat("@synchronized(self) {\n"
7170 " f();\n"
7171 "}");
7172
7173 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
7174 verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
7175
7176 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
7177 verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
7178 verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
7179 verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
7180 getMozillaStyle());
7181 verifyFormat("@property BOOL editable;", getMozillaStyle());
7182 verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
7183 getWebKitStyle());
7184 verifyFormat("@property BOOL editable;", getWebKitStyle());
7185
7186 verifyFormat("@import foo.bar;\n"
7187 "@import baz;");
7188 }
7189
TEST_F(FormatTest,ObjCLiterals)7190 TEST_F(FormatTest, ObjCLiterals) {
7191 verifyFormat("@\"String\"");
7192 verifyFormat("@1");
7193 verifyFormat("@+4.8");
7194 verifyFormat("@-4");
7195 verifyFormat("@1LL");
7196 verifyFormat("@.5");
7197 verifyFormat("@'c'");
7198 verifyFormat("@true");
7199
7200 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
7201 verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);");
7202 verifyFormat("NSNumber *favoriteColor = @(Green);");
7203 verifyFormat("NSString *path = @(getenv(\"PATH\"));");
7204
7205 verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];");
7206 }
7207
TEST_F(FormatTest,ObjCDictLiterals)7208 TEST_F(FormatTest, ObjCDictLiterals) {
7209 verifyFormat("@{");
7210 verifyFormat("@{}");
7211 verifyFormat("@{@\"one\" : @1}");
7212 verifyFormat("return @{@\"one\" : @1;");
7213 verifyFormat("@{@\"one\" : @1}");
7214
7215 verifyFormat("@{@\"one\" : @{@2 : @1}}");
7216 verifyFormat("@{\n"
7217 " @\"one\" : @{@2 : @1},\n"
7218 "}");
7219
7220 verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}");
7221 verifyFormat("[self setDict:@{}");
7222 verifyFormat("[self setDict:@{@1 : @2}");
7223 verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);");
7224 verifyFormat(
7225 "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};");
7226 verifyFormat(
7227 "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};");
7228
7229 verifyFormat(
7230 "NSDictionary *d = @{\n"
7231 " @\"nam\" : NSUserNam(),\n"
7232 " @\"dte\" : [NSDate date],\n"
7233 " @\"processInfo\" : [NSProcessInfo processInfo]\n"
7234 "};");
7235 verifyFormat(
7236 "@{\n"
7237 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
7238 "regularFont,\n"
7239 "};");
7240 verifyGoogleFormat(
7241 "@{\n"
7242 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
7243 "regularFont,\n"
7244 "};");
7245 verifyFormat(
7246 "@{\n"
7247 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n"
7248 " reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n"
7249 "};");
7250
7251 // We should try to be robust in case someone forgets the "@".
7252 verifyFormat(
7253 "NSDictionary *d = {\n"
7254 " @\"nam\" : NSUserNam(),\n"
7255 " @\"dte\" : [NSDate date],\n"
7256 " @\"processInfo\" : [NSProcessInfo processInfo]\n"
7257 "};");
7258 verifyFormat("NSMutableDictionary *dictionary =\n"
7259 " [NSMutableDictionary dictionaryWithDictionary:@{\n"
7260 " aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n"
7261 " bbbbbbbbbbbbbbbbbb : bbbbb,\n"
7262 " cccccccccccccccc : ccccccccccccccc\n"
7263 " }];");
7264 }
7265
TEST_F(FormatTest,ObjCArrayLiterals)7266 TEST_F(FormatTest, ObjCArrayLiterals) {
7267 verifyFormat("@[");
7268 verifyFormat("@[]");
7269 verifyFormat(
7270 "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];");
7271 verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];");
7272 verifyFormat("NSArray *array = @[ [foo description] ];");
7273
7274 verifyFormat(
7275 "NSArray *some_variable = @[\n"
7276 " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
7277 " @\"aaaaaaaaaaaaaaaaa\",\n"
7278 " @\"aaaaaaaaaaaaaaaaa\",\n"
7279 " @\"aaaaaaaaaaaaaaaaa\"\n"
7280 "];");
7281 verifyFormat("NSArray *some_variable = @[\n"
7282 " @\"aaaaaaaaaaaaaaaaa\",\n"
7283 " @\"aaaaaaaaaaaaaaaaa\",\n"
7284 " @\"aaaaaaaaaaaaaaaaa\",\n"
7285 " @\"aaaaaaaaaaaaaaaaa\",\n"
7286 "];");
7287 verifyGoogleFormat("NSArray *some_variable = @[\n"
7288 " @\"aaaaaaaaaaaaaaaaa\",\n"
7289 " @\"aaaaaaaaaaaaaaaaa\",\n"
7290 " @\"aaaaaaaaaaaaaaaaa\",\n"
7291 " @\"aaaaaaaaaaaaaaaaa\"\n"
7292 "];");
7293 verifyFormat("NSArray *array = @[\n"
7294 " @\"a\",\n"
7295 " @\"a\",\n" // Trailing comma -> one per line.
7296 "];");
7297
7298 // We should try to be robust in case someone forgets the "@".
7299 verifyFormat("NSArray *some_variable = [\n"
7300 " @\"aaaaaaaaaaaaaaaaa\",\n"
7301 " @\"aaaaaaaaaaaaaaaaa\",\n"
7302 " @\"aaaaaaaaaaaaaaaaa\",\n"
7303 " @\"aaaaaaaaaaaaaaaaa\",\n"
7304 "];");
7305 verifyFormat(
7306 "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n"
7307 " index:(NSUInteger)index\n"
7308 " nonDigitAttributes:\n"
7309 " (NSDictionary *)noDigitAttributes;");
7310 verifyFormat(
7311 "[someFunction someLooooooooooooongParameter:\n"
7312 " @[ NSBundle.mainBundle.infoDictionary[@\"a\"] ]];");
7313 }
7314
TEST_F(FormatTest,ReformatRegionAdjustsIndent)7315 TEST_F(FormatTest, ReformatRegionAdjustsIndent) {
7316 EXPECT_EQ("{\n"
7317 "{\n"
7318 "a;\n"
7319 "b;\n"
7320 "}\n"
7321 "}",
7322 format("{\n"
7323 "{\n"
7324 "a;\n"
7325 " b;\n"
7326 "}\n"
7327 "}",
7328 13, 2, getLLVMStyle()));
7329 EXPECT_EQ("{\n"
7330 "{\n"
7331 " a;\n"
7332 "b;\n"
7333 "}\n"
7334 "}",
7335 format("{\n"
7336 "{\n"
7337 " a;\n"
7338 "b;\n"
7339 "}\n"
7340 "}",
7341 9, 2, getLLVMStyle()));
7342 EXPECT_EQ("{\n"
7343 "{\n"
7344 "public:\n"
7345 " b;\n"
7346 "}\n"
7347 "}",
7348 format("{\n"
7349 "{\n"
7350 "public:\n"
7351 " b;\n"
7352 "}\n"
7353 "}",
7354 17, 2, getLLVMStyle()));
7355 EXPECT_EQ("{\n"
7356 "{\n"
7357 "a;\n"
7358 "}\n"
7359 "{\n"
7360 " b; //\n"
7361 "}\n"
7362 "}",
7363 format("{\n"
7364 "{\n"
7365 "a;\n"
7366 "}\n"
7367 "{\n"
7368 " b; //\n"
7369 "}\n"
7370 "}",
7371 22, 2, getLLVMStyle()));
7372 EXPECT_EQ(" {\n"
7373 " a; //\n"
7374 " }",
7375 format(" {\n"
7376 "a; //\n"
7377 " }",
7378 4, 2, getLLVMStyle()));
7379 EXPECT_EQ("void f() {}\n"
7380 "void g() {}",
7381 format("void f() {}\n"
7382 "void g() {}",
7383 13, 0, getLLVMStyle()));
7384 EXPECT_EQ("int a; // comment\n"
7385 " // line 2\n"
7386 "int b;",
7387 format("int a; // comment\n"
7388 " // line 2\n"
7389 " int b;",
7390 35, 0, getLLVMStyle()));
7391 EXPECT_EQ(" int a;\n"
7392 " void\n"
7393 " ffffff() {\n"
7394 " }",
7395 format(" int a;\n"
7396 "void ffffff() {}",
7397 11, 0, getLLVMStyleWithColumns(11)));
7398
7399 EXPECT_EQ(" void f() {\n"
7400 "#define A 1\n"
7401 " }",
7402 format(" void f() {\n"
7403 " #define A 1\n" // Format this line.
7404 " }",
7405 20, 0, getLLVMStyle()));
7406 EXPECT_EQ(" void f() {\n"
7407 " int i;\n"
7408 "#define A \\\n"
7409 " int i; \\\n"
7410 " int j;\n"
7411 " int k;\n"
7412 " }",
7413 format(" void f() {\n"
7414 " int i;\n"
7415 "#define A \\\n"
7416 " int i; \\\n"
7417 " int j;\n"
7418 " int k;\n" // Format this line.
7419 " }",
7420 67, 0, getLLVMStyle()));
7421 }
7422
TEST_F(FormatTest,BreaksStringLiterals)7423 TEST_F(FormatTest, BreaksStringLiterals) {
7424 EXPECT_EQ("\"some text \"\n"
7425 "\"other\";",
7426 format("\"some text other\";", getLLVMStyleWithColumns(12)));
7427 EXPECT_EQ("\"some text \"\n"
7428 "\"other\";",
7429 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
7430 EXPECT_EQ(
7431 "#define A \\\n"
7432 " \"some \" \\\n"
7433 " \"text \" \\\n"
7434 " \"other\";",
7435 format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
7436 EXPECT_EQ(
7437 "#define A \\\n"
7438 " \"so \" \\\n"
7439 " \"text \" \\\n"
7440 " \"other\";",
7441 format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
7442
7443 EXPECT_EQ("\"some text\"",
7444 format("\"some text\"", getLLVMStyleWithColumns(1)));
7445 EXPECT_EQ("\"some text\"",
7446 format("\"some text\"", getLLVMStyleWithColumns(11)));
7447 EXPECT_EQ("\"some \"\n"
7448 "\"text\"",
7449 format("\"some text\"", getLLVMStyleWithColumns(10)));
7450 EXPECT_EQ("\"some \"\n"
7451 "\"text\"",
7452 format("\"some text\"", getLLVMStyleWithColumns(7)));
7453 EXPECT_EQ("\"some\"\n"
7454 "\" tex\"\n"
7455 "\"t\"",
7456 format("\"some text\"", getLLVMStyleWithColumns(6)));
7457 EXPECT_EQ("\"some\"\n"
7458 "\" tex\"\n"
7459 "\" and\"",
7460 format("\"some tex and\"", getLLVMStyleWithColumns(6)));
7461 EXPECT_EQ("\"some\"\n"
7462 "\"/tex\"\n"
7463 "\"/and\"",
7464 format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
7465
7466 EXPECT_EQ("variable =\n"
7467 " \"long string \"\n"
7468 " \"literal\";",
7469 format("variable = \"long string literal\";",
7470 getLLVMStyleWithColumns(20)));
7471
7472 EXPECT_EQ("variable = f(\n"
7473 " \"long string \"\n"
7474 " \"literal\",\n"
7475 " short,\n"
7476 " loooooooooooooooooooong);",
7477 format("variable = f(\"long string literal\", short, "
7478 "loooooooooooooooooooong);",
7479 getLLVMStyleWithColumns(20)));
7480
7481 EXPECT_EQ("f(g(\"long string \"\n"
7482 " \"literal\"),\n"
7483 " b);",
7484 format("f(g(\"long string literal\"), b);",
7485 getLLVMStyleWithColumns(20)));
7486 EXPECT_EQ("f(g(\"long string \"\n"
7487 " \"literal\",\n"
7488 " a),\n"
7489 " b);",
7490 format("f(g(\"long string literal\", a), b);",
7491 getLLVMStyleWithColumns(20)));
7492 EXPECT_EQ(
7493 "f(\"one two\".split(\n"
7494 " variable));",
7495 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
7496 EXPECT_EQ("f(\"one two three four five six \"\n"
7497 " \"seven\".split(\n"
7498 " really_looooong_variable));",
7499 format("f(\"one two three four five six seven\"."
7500 "split(really_looooong_variable));",
7501 getLLVMStyleWithColumns(33)));
7502
7503 EXPECT_EQ("f(\"some \"\n"
7504 " \"text\",\n"
7505 " other);",
7506 format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
7507
7508 // Only break as a last resort.
7509 verifyFormat(
7510 "aaaaaaaaaaaaaaaaaaaa(\n"
7511 " aaaaaaaaaaaaaaaaaaaa,\n"
7512 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
7513
7514 EXPECT_EQ(
7515 "\"splitmea\"\n"
7516 "\"trandomp\"\n"
7517 "\"oint\"",
7518 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
7519
7520 EXPECT_EQ(
7521 "\"split/\"\n"
7522 "\"pathat/\"\n"
7523 "\"slashes\"",
7524 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
7525
7526 EXPECT_EQ(
7527 "\"split/\"\n"
7528 "\"pathat/\"\n"
7529 "\"slashes\"",
7530 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
7531 EXPECT_EQ("\"split at \"\n"
7532 "\"spaces/at/\"\n"
7533 "\"slashes.at.any$\"\n"
7534 "\"non-alphanumeric%\"\n"
7535 "\"1111111111characte\"\n"
7536 "\"rs\"",
7537 format("\"split at "
7538 "spaces/at/"
7539 "slashes.at."
7540 "any$non-"
7541 "alphanumeric%"
7542 "1111111111characte"
7543 "rs\"",
7544 getLLVMStyleWithColumns(20)));
7545
7546 // Verify that splitting the strings understands
7547 // Style::AlwaysBreakBeforeMultilineStrings.
7548 EXPECT_EQ("aaaaaaaaaaaa(aaaaaaaaaaaaa,\n"
7549 " \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
7550 " \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
7551 format("aaaaaaaaaaaa(aaaaaaaaaaaaa, \"aaaaaaaaaaaaaaaaaaaaaa "
7552 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa "
7553 "aaaaaaaaaaaaaaaaaaaaaa\");",
7554 getGoogleStyle()));
7555 EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7556 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
7557 format("return \"aaaaaaaaaaaaaaaaaaaaaa "
7558 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
7559 "aaaaaaaaaaaaaaaaaaaaaa\";",
7560 getGoogleStyle()));
7561 EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7562 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
7563 format("llvm::outs() << "
7564 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
7565 "aaaaaaaaaaaaaaaaaaa\";"));
7566 EXPECT_EQ("ffff(\n"
7567 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7568 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
7569 format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
7570 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
7571 getGoogleStyle()));
7572
7573 FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
7574 AlignLeft.AlignEscapedNewlinesLeft = true;
7575 EXPECT_EQ(
7576 "#define A \\\n"
7577 " \"some \" \\\n"
7578 " \"text \" \\\n"
7579 " \"other\";",
7580 format("#define A \"some text other\";", AlignLeft));
7581 }
7582
TEST_F(FormatTest,BreaksStringLiteralsWithTabs)7583 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
7584 EXPECT_EQ(
7585 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
7586 "(\n"
7587 " \"x\t\");",
7588 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
7589 "aaaaaaa("
7590 "\"x\t\");"));
7591 }
7592
TEST_F(FormatTest,BreaksWideAndNSStringLiterals)7593 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
7594 EXPECT_EQ(
7595 "u8\"utf8 string \"\n"
7596 "u8\"literal\";",
7597 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
7598 EXPECT_EQ(
7599 "u\"utf16 string \"\n"
7600 "u\"literal\";",
7601 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
7602 EXPECT_EQ(
7603 "U\"utf32 string \"\n"
7604 "U\"literal\";",
7605 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
7606 EXPECT_EQ("L\"wide string \"\n"
7607 "L\"literal\";",
7608 format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
7609 EXPECT_EQ("@\"NSString \"\n"
7610 "@\"literal\";",
7611 format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
7612
7613 // This input makes clang-format try to split the incomplete unicode escape
7614 // sequence, which used to lead to a crasher.
7615 verifyNoCrash(
7616 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
7617 getLLVMStyleWithColumns(60));
7618 }
7619
TEST_F(FormatTest,DoesNotBreakRawStringLiterals)7620 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
7621 FormatStyle Style = getGoogleStyleWithColumns(15);
7622 EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
7623 EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
7624 EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
7625 EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
7626 EXPECT_EQ("u8R\"x(raw literal)x\";",
7627 format("u8R\"x(raw literal)x\";", Style));
7628 }
7629
TEST_F(FormatTest,BreaksStringLiteralsWithin_TMacro)7630 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
7631 FormatStyle Style = getLLVMStyleWithColumns(20);
7632 EXPECT_EQ(
7633 "_T(\"aaaaaaaaaaaaaa\")\n"
7634 "_T(\"aaaaaaaaaaaaaa\")\n"
7635 "_T(\"aaaaaaaaaaaa\")",
7636 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
7637 EXPECT_EQ("f(x, _T(\"aaaaaaaaa\")\n"
7638 " _T(\"aaaaaa\"),\n"
7639 " z);",
7640 format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
7641
7642 // FIXME: Handle embedded spaces in one iteration.
7643 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
7644 // "_T(\"aaaaaaaaaaaaa\")\n"
7645 // "_T(\"aaaaaaaaaaaaa\")\n"
7646 // "_T(\"a\")",
7647 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
7648 // getLLVMStyleWithColumns(20)));
7649 EXPECT_EQ(
7650 "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
7651 format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
7652 EXPECT_EQ("f(\n"
7653 "#if !TEST\n"
7654 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
7655 "#endif\n"
7656 " );",
7657 format("f(\n"
7658 "#if !TEST\n"
7659 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
7660 "#endif\n"
7661 ");"));
7662 EXPECT_EQ("f(\n"
7663 "\n"
7664 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
7665 format("f(\n"
7666 "\n"
7667 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"));
7668 }
7669
TEST_F(FormatTest,DontSplitStringLiteralsWithEscapedNewlines)7670 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
7671 EXPECT_EQ(
7672 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7673 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7674 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
7675 format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7676 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7677 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
7678 }
7679
TEST_F(FormatTest,CountsCharactersInMultilineRawStringLiterals)7680 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
7681 EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
7682 format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle()));
7683 EXPECT_EQ("fffffffffff(g(R\"x(\n"
7684 "multiline raw string literal xxxxxxxxxxxxxx\n"
7685 ")x\",\n"
7686 " a),\n"
7687 " b);",
7688 format("fffffffffff(g(R\"x(\n"
7689 "multiline raw string literal xxxxxxxxxxxxxx\n"
7690 ")x\", a), b);",
7691 getGoogleStyleWithColumns(20)));
7692 EXPECT_EQ("fffffffffff(\n"
7693 " g(R\"x(qqq\n"
7694 "multiline raw string literal xxxxxxxxxxxxxx\n"
7695 ")x\",\n"
7696 " a),\n"
7697 " b);",
7698 format("fffffffffff(g(R\"x(qqq\n"
7699 "multiline raw string literal xxxxxxxxxxxxxx\n"
7700 ")x\", a), b);",
7701 getGoogleStyleWithColumns(20)));
7702
7703 EXPECT_EQ("fffffffffff(R\"x(\n"
7704 "multiline raw string literal xxxxxxxxxxxxxx\n"
7705 ")x\");",
7706 format("fffffffffff(R\"x(\n"
7707 "multiline raw string literal xxxxxxxxxxxxxx\n"
7708 ")x\");",
7709 getGoogleStyleWithColumns(20)));
7710 EXPECT_EQ("fffffffffff(R\"x(\n"
7711 "multiline raw string literal xxxxxxxxxxxxxx\n"
7712 ")x\" + bbbbbb);",
7713 format("fffffffffff(R\"x(\n"
7714 "multiline raw string literal xxxxxxxxxxxxxx\n"
7715 ")x\" + bbbbbb);",
7716 getGoogleStyleWithColumns(20)));
7717 EXPECT_EQ("fffffffffff(\n"
7718 " R\"x(\n"
7719 "multiline raw string literal xxxxxxxxxxxxxx\n"
7720 ")x\" +\n"
7721 " bbbbbb);",
7722 format("fffffffffff(\n"
7723 " R\"x(\n"
7724 "multiline raw string literal xxxxxxxxxxxxxx\n"
7725 ")x\" + bbbbbb);",
7726 getGoogleStyleWithColumns(20)));
7727 }
7728
TEST_F(FormatTest,SkipsUnknownStringLiterals)7729 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
7730 verifyFormat("string a = \"unterminated;");
7731 EXPECT_EQ("function(\"unterminated,\n"
7732 " OtherParameter);",
7733 format("function( \"unterminated,\n"
7734 " OtherParameter);"));
7735 }
7736
TEST_F(FormatTest,DoesNotTryToParseUDLiteralsInPreCpp11Code)7737 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
7738 FormatStyle Style = getLLVMStyle();
7739 Style.Standard = FormatStyle::LS_Cpp03;
7740 EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
7741 format("#define x(_a) printf(\"foo\"_a);", Style));
7742 }
7743
TEST_F(FormatTest,UnderstandsCpp1y)7744 TEST_F(FormatTest, UnderstandsCpp1y) {
7745 verifyFormat("int bi{1'000'000};");
7746 }
7747
TEST_F(FormatTest,BreakStringLiteralsBeforeUnbreakableTokenSequence)7748 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
7749 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
7750 " \"ddeeefff\");",
7751 format("someFunction(\"aaabbbcccdddeeefff\");",
7752 getLLVMStyleWithColumns(25)));
7753 EXPECT_EQ("someFunction1234567890(\n"
7754 " \"aaabbbcccdddeeefff\");",
7755 format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
7756 getLLVMStyleWithColumns(26)));
7757 EXPECT_EQ("someFunction1234567890(\n"
7758 " \"aaabbbcccdddeeeff\"\n"
7759 " \"f\");",
7760 format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
7761 getLLVMStyleWithColumns(25)));
7762 EXPECT_EQ("someFunction1234567890(\n"
7763 " \"aaabbbcccdddeeeff\"\n"
7764 " \"f\");",
7765 format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
7766 getLLVMStyleWithColumns(24)));
7767 EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
7768 " \"ddde \"\n"
7769 " \"efff\");",
7770 format("someFunction(\"aaabbbcc ddde efff\");",
7771 getLLVMStyleWithColumns(25)));
7772 EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
7773 " \"ddeeefff\");",
7774 format("someFunction(\"aaabbbccc ddeeefff\");",
7775 getLLVMStyleWithColumns(25)));
7776 EXPECT_EQ("someFunction1234567890(\n"
7777 " \"aaabb \"\n"
7778 " \"cccdddeeefff\");",
7779 format("someFunction1234567890(\"aaabb cccdddeeefff\");",
7780 getLLVMStyleWithColumns(25)));
7781 EXPECT_EQ("#define A \\\n"
7782 " string s = \\\n"
7783 " \"123456789\" \\\n"
7784 " \"0\"; \\\n"
7785 " int i;",
7786 format("#define A string s = \"1234567890\"; int i;",
7787 getLLVMStyleWithColumns(20)));
7788 // FIXME: Put additional penalties on breaking at non-whitespace locations.
7789 EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
7790 " \"dddeeeff\"\n"
7791 " \"f\");",
7792 format("someFunction(\"aaabbbcc dddeeefff\");",
7793 getLLVMStyleWithColumns(25)));
7794 }
7795
TEST_F(FormatTest,DoNotBreakStringLiteralsInEscapeSequence)7796 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
7797 EXPECT_EQ("\"\\a\"",
7798 format("\"\\a\"", getLLVMStyleWithColumns(3)));
7799 EXPECT_EQ("\"\\\"",
7800 format("\"\\\"", getLLVMStyleWithColumns(2)));
7801 EXPECT_EQ("\"test\"\n"
7802 "\"\\n\"",
7803 format("\"test\\n\"", getLLVMStyleWithColumns(7)));
7804 EXPECT_EQ("\"tes\\\\\"\n"
7805 "\"n\"",
7806 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
7807 EXPECT_EQ("\"\\\\\\\\\"\n"
7808 "\"\\n\"",
7809 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
7810 EXPECT_EQ("\"\\uff01\"",
7811 format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
7812 EXPECT_EQ("\"\\uff01\"\n"
7813 "\"test\"",
7814 format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
7815 EXPECT_EQ("\"\\Uff01ff02\"",
7816 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
7817 EXPECT_EQ("\"\\x000000000001\"\n"
7818 "\"next\"",
7819 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
7820 EXPECT_EQ("\"\\x000000000001next\"",
7821 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
7822 EXPECT_EQ("\"\\x000000000001\"",
7823 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
7824 EXPECT_EQ("\"test\"\n"
7825 "\"\\000000\"\n"
7826 "\"000001\"",
7827 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
7828 EXPECT_EQ("\"test\\000\"\n"
7829 "\"00000000\"\n"
7830 "\"1\"",
7831 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
7832 }
7833
TEST_F(FormatTest,DoNotCreateUnreasonableUnwrappedLines)7834 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
7835 verifyFormat("void f() {\n"
7836 " return g() {}\n"
7837 " void h() {}");
7838 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
7839 "g();\n"
7840 "}");
7841 }
7842
TEST_F(FormatTest,DoNotPrematurelyEndUnwrappedLineForReturnStatements)7843 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
7844 verifyFormat(
7845 "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
7846 }
7847
TEST_F(FormatTest,FormatsClosingBracesInEmptyNestedBlocks)7848 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
7849 verifyFormat("class X {\n"
7850 " void f() {\n"
7851 " }\n"
7852 "};",
7853 getLLVMStyleWithColumns(12));
7854 }
7855
TEST_F(FormatTest,ConfigurableIndentWidth)7856 TEST_F(FormatTest, ConfigurableIndentWidth) {
7857 FormatStyle EightIndent = getLLVMStyleWithColumns(18);
7858 EightIndent.IndentWidth = 8;
7859 EightIndent.ContinuationIndentWidth = 8;
7860 verifyFormat("void f() {\n"
7861 " someFunction();\n"
7862 " if (true) {\n"
7863 " f();\n"
7864 " }\n"
7865 "}",
7866 EightIndent);
7867 verifyFormat("class X {\n"
7868 " void f() {\n"
7869 " }\n"
7870 "};",
7871 EightIndent);
7872 verifyFormat("int x[] = {\n"
7873 " call(),\n"
7874 " call()};",
7875 EightIndent);
7876 }
7877
TEST_F(FormatTest,ConfigurableFunctionDeclarationIndentAfterType)7878 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
7879 verifyFormat("double\n"
7880 "f();",
7881 getLLVMStyleWithColumns(8));
7882 }
7883
TEST_F(FormatTest,ConfigurableUseOfTab)7884 TEST_F(FormatTest, ConfigurableUseOfTab) {
7885 FormatStyle Tab = getLLVMStyleWithColumns(42);
7886 Tab.IndentWidth = 8;
7887 Tab.UseTab = FormatStyle::UT_Always;
7888 Tab.AlignEscapedNewlinesLeft = true;
7889
7890 EXPECT_EQ("if (aaaaaaaa && // q\n"
7891 " bb)\t\t// w\n"
7892 "\t;",
7893 format("if (aaaaaaaa &&// q\n"
7894 "bb)// w\n"
7895 ";",
7896 Tab));
7897 EXPECT_EQ("if (aaa && bbb) // w\n"
7898 "\t;",
7899 format("if(aaa&&bbb)// w\n"
7900 ";",
7901 Tab));
7902
7903 verifyFormat("class X {\n"
7904 "\tvoid f() {\n"
7905 "\t\tsomeFunction(parameter1,\n"
7906 "\t\t\t parameter2);\n"
7907 "\t}\n"
7908 "};",
7909 Tab);
7910 verifyFormat("#define A \\\n"
7911 "\tvoid f() { \\\n"
7912 "\t\tsomeFunction( \\\n"
7913 "\t\t parameter1, \\\n"
7914 "\t\t parameter2); \\\n"
7915 "\t}",
7916 Tab);
7917 EXPECT_EQ("void f() {\n"
7918 "\tf();\n"
7919 "\tg();\n"
7920 "}",
7921 format("void f() {\n"
7922 "\tf();\n"
7923 "\tg();\n"
7924 "}",
7925 0, 0, Tab));
7926 EXPECT_EQ("void f() {\n"
7927 "\tf();\n"
7928 "\tg();\n"
7929 "}",
7930 format("void f() {\n"
7931 "\tf();\n"
7932 "\tg();\n"
7933 "}",
7934 16, 0, Tab));
7935 EXPECT_EQ("void f() {\n"
7936 " \tf();\n"
7937 "\tg();\n"
7938 "}",
7939 format("void f() {\n"
7940 " \tf();\n"
7941 " \tg();\n"
7942 "}",
7943 21, 0, Tab));
7944
7945 Tab.TabWidth = 4;
7946 Tab.IndentWidth = 8;
7947 verifyFormat("class TabWidth4Indent8 {\n"
7948 "\t\tvoid f() {\n"
7949 "\t\t\t\tsomeFunction(parameter1,\n"
7950 "\t\t\t\t\t\t\t parameter2);\n"
7951 "\t\t}\n"
7952 "};",
7953 Tab);
7954
7955 Tab.TabWidth = 4;
7956 Tab.IndentWidth = 4;
7957 verifyFormat("class TabWidth4Indent4 {\n"
7958 "\tvoid f() {\n"
7959 "\t\tsomeFunction(parameter1,\n"
7960 "\t\t\t\t\t parameter2);\n"
7961 "\t}\n"
7962 "};",
7963 Tab);
7964
7965 Tab.TabWidth = 8;
7966 Tab.IndentWidth = 4;
7967 verifyFormat("class TabWidth8Indent4 {\n"
7968 " void f() {\n"
7969 "\tsomeFunction(parameter1,\n"
7970 "\t\t parameter2);\n"
7971 " }\n"
7972 "};",
7973 Tab);
7974
7975 Tab.TabWidth = 8;
7976 Tab.IndentWidth = 8;
7977 EXPECT_EQ("/*\n"
7978 "\t a\t\tcomment\n"
7979 "\t in multiple lines\n"
7980 " */",
7981 format(" /*\t \t \n"
7982 " \t \t a\t\tcomment\t \t\n"
7983 " \t \t in multiple lines\t\n"
7984 " \t */",
7985 Tab));
7986
7987 Tab.UseTab = FormatStyle::UT_ForIndentation;
7988 verifyFormat("{\n"
7989 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7990 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7991 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7992 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7993 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7994 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7995 "};",
7996 Tab);
7997 verifyFormat("enum A {\n"
7998 "\ta1, // Force multiple lines\n"
7999 "\ta2,\n"
8000 "\ta3\n"
8001 "};",
8002 Tab);
8003 EXPECT_EQ("if (aaaaaaaa && // q\n"
8004 " bb) // w\n"
8005 "\t;",
8006 format("if (aaaaaaaa &&// q\n"
8007 "bb)// w\n"
8008 ";",
8009 Tab));
8010 verifyFormat("class X {\n"
8011 "\tvoid f() {\n"
8012 "\t\tsomeFunction(parameter1,\n"
8013 "\t\t parameter2);\n"
8014 "\t}\n"
8015 "};",
8016 Tab);
8017 verifyFormat("{\n"
8018 "\tQ({\n"
8019 "\t\tint a;\n"
8020 "\t\tsomeFunction(aaaaaaaa,\n"
8021 "\t\t bbbbbbb);\n"
8022 "\t}, p);\n"
8023 "}",
8024 Tab);
8025 EXPECT_EQ("{\n"
8026 "\t/* aaaa\n"
8027 "\t bbbb */\n"
8028 "}",
8029 format("{\n"
8030 "/* aaaa\n"
8031 " bbbb */\n"
8032 "}",
8033 Tab));
8034 EXPECT_EQ("{\n"
8035 "\t/*\n"
8036 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8037 "\t bbbbbbbbbbbbb\n"
8038 "\t*/\n"
8039 "}",
8040 format("{\n"
8041 "/*\n"
8042 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8043 "*/\n"
8044 "}",
8045 Tab));
8046 EXPECT_EQ("{\n"
8047 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8048 "\t// bbbbbbbbbbbbb\n"
8049 "}",
8050 format("{\n"
8051 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8052 "}",
8053 Tab));
8054 EXPECT_EQ("{\n"
8055 "\t/*\n"
8056 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8057 "\t bbbbbbbbbbbbb\n"
8058 "\t*/\n"
8059 "}",
8060 format("{\n"
8061 "\t/*\n"
8062 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8063 "\t*/\n"
8064 "}",
8065 Tab));
8066 EXPECT_EQ("{\n"
8067 "\t/*\n"
8068 "\n"
8069 "\t*/\n"
8070 "}",
8071 format("{\n"
8072 "\t/*\n"
8073 "\n"
8074 "\t*/\n"
8075 "}",
8076 Tab));
8077 EXPECT_EQ("{\n"
8078 "\t/*\n"
8079 " asdf\n"
8080 "\t*/\n"
8081 "}",
8082 format("{\n"
8083 "\t/*\n"
8084 " asdf\n"
8085 "\t*/\n"
8086 "}",
8087 Tab));
8088
8089 Tab.UseTab = FormatStyle::UT_Never;
8090 EXPECT_EQ("/*\n"
8091 " a\t\tcomment\n"
8092 " in multiple lines\n"
8093 " */",
8094 format(" /*\t \t \n"
8095 " \t \t a\t\tcomment\t \t\n"
8096 " \t \t in multiple lines\t\n"
8097 " \t */",
8098 Tab));
8099 EXPECT_EQ("/* some\n"
8100 " comment */",
8101 format(" \t \t /* some\n"
8102 " \t \t comment */",
8103 Tab));
8104 EXPECT_EQ("int a; /* some\n"
8105 " comment */",
8106 format(" \t \t int a; /* some\n"
8107 " \t \t comment */",
8108 Tab));
8109
8110 EXPECT_EQ("int a; /* some\n"
8111 "comment */",
8112 format(" \t \t int\ta; /* some\n"
8113 " \t \t comment */",
8114 Tab));
8115 EXPECT_EQ("f(\"\t\t\"); /* some\n"
8116 " comment */",
8117 format(" \t \t f(\"\t\t\"); /* some\n"
8118 " \t \t comment */",
8119 Tab));
8120 EXPECT_EQ("{\n"
8121 " /*\n"
8122 " * Comment\n"
8123 " */\n"
8124 " int i;\n"
8125 "}",
8126 format("{\n"
8127 "\t/*\n"
8128 "\t * Comment\n"
8129 "\t */\n"
8130 "\t int i;\n"
8131 "}"));
8132 }
8133
TEST_F(FormatTest,CalculatesOriginalColumn)8134 TEST_F(FormatTest, CalculatesOriginalColumn) {
8135 EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8136 "q\"; /* some\n"
8137 " comment */",
8138 format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8139 "q\"; /* some\n"
8140 " comment */",
8141 getLLVMStyle()));
8142 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
8143 "/* some\n"
8144 " comment */",
8145 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
8146 " /* some\n"
8147 " comment */",
8148 getLLVMStyle()));
8149 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8150 "qqq\n"
8151 "/* some\n"
8152 " comment */",
8153 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8154 "qqq\n"
8155 " /* some\n"
8156 " comment */",
8157 getLLVMStyle()));
8158 EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8159 "wwww; /* some\n"
8160 " comment */",
8161 format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8162 "wwww; /* some\n"
8163 " comment */",
8164 getLLVMStyle()));
8165 }
8166
TEST_F(FormatTest,ConfigurableSpaceBeforeParens)8167 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
8168 FormatStyle NoSpace = getLLVMStyle();
8169 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
8170
8171 verifyFormat("while(true)\n"
8172 " continue;", NoSpace);
8173 verifyFormat("for(;;)\n"
8174 " continue;", NoSpace);
8175 verifyFormat("if(true)\n"
8176 " f();\n"
8177 "else if(true)\n"
8178 " f();", NoSpace);
8179 verifyFormat("do {\n"
8180 " do_something();\n"
8181 "} while(something());", NoSpace);
8182 verifyFormat("switch(x) {\n"
8183 "default:\n"
8184 " break;\n"
8185 "}", NoSpace);
8186 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
8187 verifyFormat("size_t x = sizeof(x);", NoSpace);
8188 verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
8189 verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
8190 verifyFormat("alignas(128) char a[128];", NoSpace);
8191 verifyFormat("size_t x = alignof(MyType);", NoSpace);
8192 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
8193 verifyFormat("int f() throw(Deprecated);", NoSpace);
8194
8195 FormatStyle Space = getLLVMStyle();
8196 Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
8197
8198 verifyFormat("int f ();", Space);
8199 verifyFormat("void f (int a, T b) {\n"
8200 " while (true)\n"
8201 " continue;\n"
8202 "}",
8203 Space);
8204 verifyFormat("if (true)\n"
8205 " f ();\n"
8206 "else if (true)\n"
8207 " f ();",
8208 Space);
8209 verifyFormat("do {\n"
8210 " do_something ();\n"
8211 "} while (something ());",
8212 Space);
8213 verifyFormat("switch (x) {\n"
8214 "default:\n"
8215 " break;\n"
8216 "}",
8217 Space);
8218 verifyFormat("A::A () : a (1) {}", Space);
8219 verifyFormat("void f () __attribute__ ((asdf));", Space);
8220 verifyFormat("*(&a + 1);\n"
8221 "&((&a)[1]);\n"
8222 "a[(b + c) * d];\n"
8223 "(((a + 1) * 2) + 3) * 4;",
8224 Space);
8225 verifyFormat("#define A(x) x", Space);
8226 verifyFormat("#define A (x) x", Space);
8227 verifyFormat("#if defined(x)\n"
8228 "#endif",
8229 Space);
8230 verifyFormat("auto i = std::make_unique<int> (5);", Space);
8231 verifyFormat("size_t x = sizeof (x);", Space);
8232 verifyFormat("auto f (int x) -> decltype (x);", Space);
8233 verifyFormat("int f (T x) noexcept (x.create ());", Space);
8234 verifyFormat("alignas (128) char a[128];", Space);
8235 verifyFormat("size_t x = alignof (MyType);", Space);
8236 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
8237 verifyFormat("int f () throw (Deprecated);", Space);
8238 }
8239
TEST_F(FormatTest,ConfigurableSpacesInParentheses)8240 TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
8241 FormatStyle Spaces = getLLVMStyle();
8242
8243 Spaces.SpacesInParentheses = true;
8244 verifyFormat("call( x, y, z );", Spaces);
8245 verifyFormat("call();", Spaces);
8246 verifyFormat("std::function<void( int, int )> callback;", Spaces);
8247 verifyFormat("while ( (bool)1 )\n"
8248 " continue;", Spaces);
8249 verifyFormat("for ( ;; )\n"
8250 " continue;", Spaces);
8251 verifyFormat("if ( true )\n"
8252 " f();\n"
8253 "else if ( true )\n"
8254 " f();", Spaces);
8255 verifyFormat("do {\n"
8256 " do_something( (int)i );\n"
8257 "} while ( something() );", Spaces);
8258 verifyFormat("switch ( x ) {\n"
8259 "default:\n"
8260 " break;\n"
8261 "}", Spaces);
8262
8263 Spaces.SpacesInParentheses = false;
8264 Spaces.SpacesInCStyleCastParentheses = true;
8265 verifyFormat("Type *A = ( Type * )P;", Spaces);
8266 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
8267 verifyFormat("x = ( int32 )y;", Spaces);
8268 verifyFormat("int a = ( int )(2.0f);", Spaces);
8269 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
8270 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
8271 verifyFormat("#define x (( int )-1)", Spaces);
8272
8273 // Run the first set of tests again with:
8274 Spaces.SpacesInParentheses = false,
8275 Spaces.SpaceInEmptyParentheses = true;
8276 Spaces.SpacesInCStyleCastParentheses = true;
8277 verifyFormat("call(x, y, z);", Spaces);
8278 verifyFormat("call( );", Spaces);
8279 verifyFormat("std::function<void(int, int)> callback;", Spaces);
8280 verifyFormat("while (( bool )1)\n"
8281 " continue;", Spaces);
8282 verifyFormat("for (;;)\n"
8283 " continue;", Spaces);
8284 verifyFormat("if (true)\n"
8285 " f( );\n"
8286 "else if (true)\n"
8287 " f( );", Spaces);
8288 verifyFormat("do {\n"
8289 " do_something(( int )i);\n"
8290 "} while (something( ));", Spaces);
8291 verifyFormat("switch (x) {\n"
8292 "default:\n"
8293 " break;\n"
8294 "}", Spaces);
8295
8296 // Run the first set of tests again with:
8297 Spaces.SpaceAfterCStyleCast = true;
8298 verifyFormat("call(x, y, z);", Spaces);
8299 verifyFormat("call( );", Spaces);
8300 verifyFormat("std::function<void(int, int)> callback;", Spaces);
8301 verifyFormat("while (( bool ) 1)\n"
8302 " continue;",
8303 Spaces);
8304 verifyFormat("for (;;)\n"
8305 " continue;",
8306 Spaces);
8307 verifyFormat("if (true)\n"
8308 " f( );\n"
8309 "else if (true)\n"
8310 " f( );",
8311 Spaces);
8312 verifyFormat("do {\n"
8313 " do_something(( int ) i);\n"
8314 "} while (something( ));",
8315 Spaces);
8316 verifyFormat("switch (x) {\n"
8317 "default:\n"
8318 " break;\n"
8319 "}",
8320 Spaces);
8321
8322 // Run subset of tests again with:
8323 Spaces.SpacesInCStyleCastParentheses = false;
8324 Spaces.SpaceAfterCStyleCast = true;
8325 verifyFormat("while ((bool) 1)\n"
8326 " continue;",
8327 Spaces);
8328 verifyFormat("do {\n"
8329 " do_something((int) i);\n"
8330 "} while (something( ));",
8331 Spaces);
8332 }
8333
TEST_F(FormatTest,ConfigurableSpacesInSquareBrackets)8334 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
8335 verifyFormat("int a[5];");
8336 verifyFormat("a[3] += 42;");
8337
8338 FormatStyle Spaces = getLLVMStyle();
8339 Spaces.SpacesInSquareBrackets = true;
8340 // Lambdas unchanged.
8341 verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
8342 verifyFormat("return [i, args...] {};", Spaces);
8343
8344 // Not lambdas.
8345 verifyFormat("int a[ 5 ];", Spaces);
8346 verifyFormat("a[ 3 ] += 42;", Spaces);
8347 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
8348 verifyFormat("double &operator[](int i) { return 0; }\n"
8349 "int i;",
8350 Spaces);
8351 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
8352 verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
8353 verifyFormat("int i = (*b)[ a ]->f();", Spaces);
8354 }
8355
TEST_F(FormatTest,ConfigurableSpaceBeforeAssignmentOperators)8356 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
8357 verifyFormat("int a = 5;");
8358 verifyFormat("a += 42;");
8359 verifyFormat("a or_eq 8;");
8360
8361 FormatStyle Spaces = getLLVMStyle();
8362 Spaces.SpaceBeforeAssignmentOperators = false;
8363 verifyFormat("int a= 5;", Spaces);
8364 verifyFormat("a+= 42;", Spaces);
8365 verifyFormat("a or_eq 8;", Spaces);
8366 }
8367
TEST_F(FormatTest,LinuxBraceBreaking)8368 TEST_F(FormatTest, LinuxBraceBreaking) {
8369 FormatStyle LinuxBraceStyle = getLLVMStyle();
8370 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
8371 verifyFormat("namespace a\n"
8372 "{\n"
8373 "class A\n"
8374 "{\n"
8375 " void f()\n"
8376 " {\n"
8377 " if (true) {\n"
8378 " a();\n"
8379 " b();\n"
8380 " }\n"
8381 " }\n"
8382 " void g() { return; }\n"
8383 "};\n"
8384 "struct B {\n"
8385 " int x;\n"
8386 "};\n"
8387 "}\n",
8388 LinuxBraceStyle);
8389 verifyFormat("enum X {\n"
8390 " Y = 0,\n"
8391 "}\n",
8392 LinuxBraceStyle);
8393 verifyFormat("struct S {\n"
8394 " int Type;\n"
8395 " union {\n"
8396 " int x;\n"
8397 " double y;\n"
8398 " } Value;\n"
8399 " class C\n"
8400 " {\n"
8401 " MyFavoriteType Value;\n"
8402 " } Class;\n"
8403 "}\n",
8404 LinuxBraceStyle);
8405 }
8406
TEST_F(FormatTest,StroustrupBraceBreaking)8407 TEST_F(FormatTest, StroustrupBraceBreaking) {
8408 FormatStyle StroustrupBraceStyle = getLLVMStyle();
8409 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
8410 verifyFormat("namespace a {\n"
8411 "class A {\n"
8412 " void f()\n"
8413 " {\n"
8414 " if (true) {\n"
8415 " a();\n"
8416 " b();\n"
8417 " }\n"
8418 " }\n"
8419 " void g() { return; }\n"
8420 "};\n"
8421 "struct B {\n"
8422 " int x;\n"
8423 "};\n"
8424 "}\n",
8425 StroustrupBraceStyle);
8426
8427 verifyFormat("void foo()\n"
8428 "{\n"
8429 " if (a) {\n"
8430 " a();\n"
8431 " }\n"
8432 " else {\n"
8433 " b();\n"
8434 " }\n"
8435 "}\n",
8436 StroustrupBraceStyle);
8437
8438 verifyFormat("#ifdef _DEBUG\n"
8439 "int foo(int i = 0)\n"
8440 "#else\n"
8441 "int foo(int i = 5)\n"
8442 "#endif\n"
8443 "{\n"
8444 " return i;\n"
8445 "}",
8446 StroustrupBraceStyle);
8447
8448 verifyFormat("void foo() {}\n"
8449 "void bar()\n"
8450 "#ifdef _DEBUG\n"
8451 "{\n"
8452 " foo();\n"
8453 "}\n"
8454 "#else\n"
8455 "{\n"
8456 "}\n"
8457 "#endif",
8458 StroustrupBraceStyle);
8459
8460 verifyFormat("void foobar() { int i = 5; }\n"
8461 "#ifdef _DEBUG\n"
8462 "void bar() {}\n"
8463 "#else\n"
8464 "void bar() { foobar(); }\n"
8465 "#endif",
8466 StroustrupBraceStyle);
8467 }
8468
TEST_F(FormatTest,AllmanBraceBreaking)8469 TEST_F(FormatTest, AllmanBraceBreaking) {
8470 FormatStyle AllmanBraceStyle = getLLVMStyle();
8471 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
8472 verifyFormat("namespace a\n"
8473 "{\n"
8474 "class A\n"
8475 "{\n"
8476 " void f()\n"
8477 " {\n"
8478 " if (true)\n"
8479 " {\n"
8480 " a();\n"
8481 " b();\n"
8482 " }\n"
8483 " }\n"
8484 " void g() { return; }\n"
8485 "};\n"
8486 "struct B\n"
8487 "{\n"
8488 " int x;\n"
8489 "};\n"
8490 "}",
8491 AllmanBraceStyle);
8492
8493 verifyFormat("void f()\n"
8494 "{\n"
8495 " if (true)\n"
8496 " {\n"
8497 " a();\n"
8498 " }\n"
8499 " else if (false)\n"
8500 " {\n"
8501 " b();\n"
8502 " }\n"
8503 " else\n"
8504 " {\n"
8505 " c();\n"
8506 " }\n"
8507 "}\n",
8508 AllmanBraceStyle);
8509
8510 verifyFormat("void f()\n"
8511 "{\n"
8512 " for (int i = 0; i < 10; ++i)\n"
8513 " {\n"
8514 " a();\n"
8515 " }\n"
8516 " while (false)\n"
8517 " {\n"
8518 " b();\n"
8519 " }\n"
8520 " do\n"
8521 " {\n"
8522 " c();\n"
8523 " } while (false)\n"
8524 "}\n",
8525 AllmanBraceStyle);
8526
8527 verifyFormat("void f(int a)\n"
8528 "{\n"
8529 " switch (a)\n"
8530 " {\n"
8531 " case 0:\n"
8532 " break;\n"
8533 " case 1:\n"
8534 " {\n"
8535 " break;\n"
8536 " }\n"
8537 " case 2:\n"
8538 " {\n"
8539 " }\n"
8540 " break;\n"
8541 " default:\n"
8542 " break;\n"
8543 " }\n"
8544 "}\n",
8545 AllmanBraceStyle);
8546
8547 verifyFormat("enum X\n"
8548 "{\n"
8549 " Y = 0,\n"
8550 "}\n",
8551 AllmanBraceStyle);
8552 verifyFormat("enum X\n"
8553 "{\n"
8554 " Y = 0\n"
8555 "}\n",
8556 AllmanBraceStyle);
8557
8558 verifyFormat("@interface BSApplicationController ()\n"
8559 "{\n"
8560 "@private\n"
8561 " id _extraIvar;\n"
8562 "}\n"
8563 "@end\n",
8564 AllmanBraceStyle);
8565
8566 verifyFormat("#ifdef _DEBUG\n"
8567 "int foo(int i = 0)\n"
8568 "#else\n"
8569 "int foo(int i = 5)\n"
8570 "#endif\n"
8571 "{\n"
8572 " return i;\n"
8573 "}",
8574 AllmanBraceStyle);
8575
8576 verifyFormat("void foo() {}\n"
8577 "void bar()\n"
8578 "#ifdef _DEBUG\n"
8579 "{\n"
8580 " foo();\n"
8581 "}\n"
8582 "#else\n"
8583 "{\n"
8584 "}\n"
8585 "#endif",
8586 AllmanBraceStyle);
8587
8588 verifyFormat("void foobar() { int i = 5; }\n"
8589 "#ifdef _DEBUG\n"
8590 "void bar() {}\n"
8591 "#else\n"
8592 "void bar() { foobar(); }\n"
8593 "#endif",
8594 AllmanBraceStyle);
8595
8596 // This shouldn't affect ObjC blocks..
8597 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
8598 " // ...\n"
8599 " int i;\n"
8600 "}];",
8601 AllmanBraceStyle);
8602 verifyFormat("void (^block)(void) = ^{\n"
8603 " // ...\n"
8604 " int i;\n"
8605 "};",
8606 AllmanBraceStyle);
8607 // .. or dict literals.
8608 verifyFormat("void f()\n"
8609 "{\n"
8610 " [object someMethod:@{ @\"a\" : @\"b\" }];\n"
8611 "}",
8612 AllmanBraceStyle);
8613 verifyFormat("int f()\n"
8614 "{ // comment\n"
8615 " return 42;\n"
8616 "}",
8617 AllmanBraceStyle);
8618
8619 AllmanBraceStyle.ColumnLimit = 19;
8620 verifyFormat("void f() { int i; }", AllmanBraceStyle);
8621 AllmanBraceStyle.ColumnLimit = 18;
8622 verifyFormat("void f()\n"
8623 "{\n"
8624 " int i;\n"
8625 "}",
8626 AllmanBraceStyle);
8627 AllmanBraceStyle.ColumnLimit = 80;
8628
8629 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
8630 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true;
8631 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
8632 verifyFormat("void f(bool b)\n"
8633 "{\n"
8634 " if (b)\n"
8635 " {\n"
8636 " return;\n"
8637 " }\n"
8638 "}\n",
8639 BreakBeforeBraceShortIfs);
8640 verifyFormat("void f(bool b)\n"
8641 "{\n"
8642 " if (b) return;\n"
8643 "}\n",
8644 BreakBeforeBraceShortIfs);
8645 verifyFormat("void f(bool b)\n"
8646 "{\n"
8647 " while (b)\n"
8648 " {\n"
8649 " return;\n"
8650 " }\n"
8651 "}\n",
8652 BreakBeforeBraceShortIfs);
8653 }
8654
TEST_F(FormatTest,GNUBraceBreaking)8655 TEST_F(FormatTest, GNUBraceBreaking) {
8656 FormatStyle GNUBraceStyle = getLLVMStyle();
8657 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
8658 verifyFormat("namespace a\n"
8659 "{\n"
8660 "class A\n"
8661 "{\n"
8662 " void f()\n"
8663 " {\n"
8664 " int a;\n"
8665 " {\n"
8666 " int b;\n"
8667 " }\n"
8668 " if (true)\n"
8669 " {\n"
8670 " a();\n"
8671 " b();\n"
8672 " }\n"
8673 " }\n"
8674 " void g() { return; }\n"
8675 "}\n"
8676 "}",
8677 GNUBraceStyle);
8678
8679 verifyFormat("void f()\n"
8680 "{\n"
8681 " if (true)\n"
8682 " {\n"
8683 " a();\n"
8684 " }\n"
8685 " else if (false)\n"
8686 " {\n"
8687 " b();\n"
8688 " }\n"
8689 " else\n"
8690 " {\n"
8691 " c();\n"
8692 " }\n"
8693 "}\n",
8694 GNUBraceStyle);
8695
8696 verifyFormat("void f()\n"
8697 "{\n"
8698 " for (int i = 0; i < 10; ++i)\n"
8699 " {\n"
8700 " a();\n"
8701 " }\n"
8702 " while (false)\n"
8703 " {\n"
8704 " b();\n"
8705 " }\n"
8706 " do\n"
8707 " {\n"
8708 " c();\n"
8709 " }\n"
8710 " while (false);\n"
8711 "}\n",
8712 GNUBraceStyle);
8713
8714 verifyFormat("void f(int a)\n"
8715 "{\n"
8716 " switch (a)\n"
8717 " {\n"
8718 " case 0:\n"
8719 " break;\n"
8720 " case 1:\n"
8721 " {\n"
8722 " break;\n"
8723 " }\n"
8724 " case 2:\n"
8725 " {\n"
8726 " }\n"
8727 " break;\n"
8728 " default:\n"
8729 " break;\n"
8730 " }\n"
8731 "}\n",
8732 GNUBraceStyle);
8733
8734 verifyFormat("enum X\n"
8735 "{\n"
8736 " Y = 0,\n"
8737 "}\n",
8738 GNUBraceStyle);
8739
8740 verifyFormat("@interface BSApplicationController ()\n"
8741 "{\n"
8742 "@private\n"
8743 " id _extraIvar;\n"
8744 "}\n"
8745 "@end\n",
8746 GNUBraceStyle);
8747
8748 verifyFormat("#ifdef _DEBUG\n"
8749 "int foo(int i = 0)\n"
8750 "#else\n"
8751 "int foo(int i = 5)\n"
8752 "#endif\n"
8753 "{\n"
8754 " return i;\n"
8755 "}",
8756 GNUBraceStyle);
8757
8758 verifyFormat("void foo() {}\n"
8759 "void bar()\n"
8760 "#ifdef _DEBUG\n"
8761 "{\n"
8762 " foo();\n"
8763 "}\n"
8764 "#else\n"
8765 "{\n"
8766 "}\n"
8767 "#endif",
8768 GNUBraceStyle);
8769
8770 verifyFormat("void foobar() { int i = 5; }\n"
8771 "#ifdef _DEBUG\n"
8772 "void bar() {}\n"
8773 "#else\n"
8774 "void bar() { foobar(); }\n"
8775 "#endif",
8776 GNUBraceStyle);
8777 }
TEST_F(FormatTest,CatchExceptionReferenceBinding)8778 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
8779 verifyFormat("void f() {\n"
8780 " try {\n"
8781 " } catch (const Exception &e) {\n"
8782 " }\n"
8783 "}\n",
8784 getLLVMStyle());
8785 }
8786
TEST_F(FormatTest,UnderstandsPragmas)8787 TEST_F(FormatTest, UnderstandsPragmas) {
8788 verifyFormat("#pragma omp reduction(| : var)");
8789 verifyFormat("#pragma omp reduction(+ : var)");
8790
8791 EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
8792 "(including parentheses).",
8793 format("#pragma mark Any non-hyphenated or hyphenated string "
8794 "(including parentheses)."));
8795 }
8796
8797 #define EXPECT_ALL_STYLES_EQUAL(Styles) \
8798 for (size_t i = 1; i < Styles.size(); ++i) \
8799 EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " \
8800 << Styles.size() \
8801 << " differs from Style #0"
8802
TEST_F(FormatTest,GetsPredefinedStyleByName)8803 TEST_F(FormatTest, GetsPredefinedStyleByName) {
8804 SmallVector<FormatStyle, 3> Styles;
8805 Styles.resize(3);
8806
8807 Styles[0] = getLLVMStyle();
8808 EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
8809 EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
8810 EXPECT_ALL_STYLES_EQUAL(Styles);
8811
8812 Styles[0] = getGoogleStyle();
8813 EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
8814 EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
8815 EXPECT_ALL_STYLES_EQUAL(Styles);
8816
8817 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
8818 EXPECT_TRUE(
8819 getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
8820 EXPECT_TRUE(
8821 getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
8822 EXPECT_ALL_STYLES_EQUAL(Styles);
8823
8824 Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
8825 EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
8826 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
8827 EXPECT_ALL_STYLES_EQUAL(Styles);
8828
8829 Styles[0] = getMozillaStyle();
8830 EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
8831 EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
8832 EXPECT_ALL_STYLES_EQUAL(Styles);
8833
8834 Styles[0] = getWebKitStyle();
8835 EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
8836 EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
8837 EXPECT_ALL_STYLES_EQUAL(Styles);
8838
8839 Styles[0] = getGNUStyle();
8840 EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
8841 EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
8842 EXPECT_ALL_STYLES_EQUAL(Styles);
8843
8844 EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
8845 }
8846
TEST_F(FormatTest,GetsCorrectBasedOnStyle)8847 TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
8848 SmallVector<FormatStyle, 8> Styles;
8849 Styles.resize(2);
8850
8851 Styles[0] = getGoogleStyle();
8852 Styles[1] = getLLVMStyle();
8853 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
8854 EXPECT_ALL_STYLES_EQUAL(Styles);
8855
8856 Styles.resize(5);
8857 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
8858 Styles[1] = getLLVMStyle();
8859 Styles[1].Language = FormatStyle::LK_JavaScript;
8860 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
8861
8862 Styles[2] = getLLVMStyle();
8863 Styles[2].Language = FormatStyle::LK_JavaScript;
8864 EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
8865 "BasedOnStyle: Google",
8866 &Styles[2]).value());
8867
8868 Styles[3] = getLLVMStyle();
8869 Styles[3].Language = FormatStyle::LK_JavaScript;
8870 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
8871 "Language: JavaScript",
8872 &Styles[3]).value());
8873
8874 Styles[4] = getLLVMStyle();
8875 Styles[4].Language = FormatStyle::LK_JavaScript;
8876 EXPECT_EQ(0, parseConfiguration("---\n"
8877 "BasedOnStyle: LLVM\n"
8878 "IndentWidth: 123\n"
8879 "---\n"
8880 "BasedOnStyle: Google\n"
8881 "Language: JavaScript",
8882 &Styles[4]).value());
8883 EXPECT_ALL_STYLES_EQUAL(Styles);
8884 }
8885
8886 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \
8887 Style.FIELD = false; \
8888 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \
8889 EXPECT_TRUE(Style.FIELD); \
8890 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \
8891 EXPECT_FALSE(Style.FIELD);
8892
8893 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
8894
8895 #define CHECK_PARSE(TEXT, FIELD, VALUE) \
8896 EXPECT_NE(VALUE, Style.FIELD); \
8897 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \
8898 EXPECT_EQ(VALUE, Style.FIELD)
8899
TEST_F(FormatTest,ParsesConfigurationBools)8900 TEST_F(FormatTest, ParsesConfigurationBools) {
8901 FormatStyle Style = {};
8902 Style.Language = FormatStyle::LK_Cpp;
8903 CHECK_PARSE_BOOL(AlignAfterOpenBracket);
8904 CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft);
8905 CHECK_PARSE_BOOL(AlignOperands);
8906 CHECK_PARSE_BOOL(AlignTrailingComments);
8907 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
8908 CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine);
8909 CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
8910 CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine);
8911 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
8912 CHECK_PARSE_BOOL(AlwaysBreakAfterDefinitionReturnType);
8913 CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations);
8914 CHECK_PARSE_BOOL(BinPackParameters);
8915 CHECK_PARSE_BOOL(BinPackArguments);
8916 CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
8917 CHECK_PARSE_BOOL(BreakConstructorInitializersBeforeComma);
8918 CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
8919 CHECK_PARSE_BOOL(DerivePointerAlignment);
8920 CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
8921 CHECK_PARSE_BOOL(IndentCaseLabels);
8922 CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
8923 CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
8924 CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
8925 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
8926 CHECK_PARSE_BOOL(Cpp11BracedListStyle);
8927 CHECK_PARSE_BOOL(SpacesInParentheses);
8928 CHECK_PARSE_BOOL(SpacesInSquareBrackets);
8929 CHECK_PARSE_BOOL(SpacesInAngles);
8930 CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
8931 CHECK_PARSE_BOOL(SpacesInContainerLiterals);
8932 CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
8933 CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
8934 CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
8935 }
8936
8937 #undef CHECK_PARSE_BOOL
8938
TEST_F(FormatTest,ParsesConfiguration)8939 TEST_F(FormatTest, ParsesConfiguration) {
8940 FormatStyle Style = {};
8941 Style.Language = FormatStyle::LK_Cpp;
8942 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
8943 CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
8944 ConstructorInitializerIndentWidth, 1234u);
8945 CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
8946 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
8947 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
8948 CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
8949 PenaltyBreakBeforeFirstCallParameter, 1234u);
8950 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
8951 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
8952 PenaltyReturnTypeOnItsOwnLine, 1234u);
8953 CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
8954 SpacesBeforeTrailingComments, 1234u);
8955 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
8956 CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
8957
8958 Style.PointerAlignment = FormatStyle::PAS_Middle;
8959 CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
8960 FormatStyle::PAS_Left);
8961 CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
8962 FormatStyle::PAS_Right);
8963 CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
8964 FormatStyle::PAS_Middle);
8965 // For backward compatibility:
8966 CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
8967 FormatStyle::PAS_Left);
8968 CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
8969 FormatStyle::PAS_Right);
8970 CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
8971 FormatStyle::PAS_Middle);
8972
8973 Style.Standard = FormatStyle::LS_Auto;
8974 CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
8975 CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11);
8976 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
8977 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
8978 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
8979
8980 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
8981 CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
8982 BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
8983 CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
8984 FormatStyle::BOS_None);
8985 CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
8986 FormatStyle::BOS_All);
8987 // For backward compatibility:
8988 CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
8989 FormatStyle::BOS_None);
8990 CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
8991 FormatStyle::BOS_All);
8992
8993 Style.UseTab = FormatStyle::UT_ForIndentation;
8994 CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
8995 CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
8996 CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
8997 // For backward compatibility:
8998 CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
8999 CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
9000
9001 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
9002 CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
9003 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
9004 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
9005 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
9006 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
9007 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
9008 CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
9009 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
9010 // For backward compatibility:
9011 CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
9012 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
9013 CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
9014 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
9015
9016 Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
9017 CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
9018 FormatStyle::SBPO_Never);
9019 CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
9020 FormatStyle::SBPO_Always);
9021 CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
9022 FormatStyle::SBPO_ControlStatements);
9023 // For backward compatibility:
9024 CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
9025 FormatStyle::SBPO_Never);
9026 CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
9027 FormatStyle::SBPO_ControlStatements);
9028
9029 Style.ColumnLimit = 123;
9030 FormatStyle BaseStyle = getLLVMStyle();
9031 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
9032 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
9033
9034 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
9035 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
9036 FormatStyle::BS_Attach);
9037 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
9038 FormatStyle::BS_Linux);
9039 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
9040 FormatStyle::BS_Stroustrup);
9041 CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
9042 FormatStyle::BS_Allman);
9043 CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
9044
9045 Style.NamespaceIndentation = FormatStyle::NI_All;
9046 CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
9047 FormatStyle::NI_None);
9048 CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
9049 FormatStyle::NI_Inner);
9050 CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
9051 FormatStyle::NI_All);
9052
9053 Style.ForEachMacros.clear();
9054 std::vector<std::string> BoostForeach;
9055 BoostForeach.push_back("BOOST_FOREACH");
9056 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
9057 std::vector<std::string> BoostAndQForeach;
9058 BoostAndQForeach.push_back("BOOST_FOREACH");
9059 BoostAndQForeach.push_back("Q_FOREACH");
9060 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
9061 BoostAndQForeach);
9062 }
9063
TEST_F(FormatTest,ParsesConfigurationWithLanguages)9064 TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
9065 FormatStyle Style = {};
9066 Style.Language = FormatStyle::LK_Cpp;
9067 CHECK_PARSE("Language: Cpp\n"
9068 "IndentWidth: 12",
9069 IndentWidth, 12u);
9070 EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
9071 "IndentWidth: 34",
9072 &Style),
9073 ParseError::Unsuitable);
9074 EXPECT_EQ(12u, Style.IndentWidth);
9075 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
9076 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
9077
9078 Style.Language = FormatStyle::LK_JavaScript;
9079 CHECK_PARSE("Language: JavaScript\n"
9080 "IndentWidth: 12",
9081 IndentWidth, 12u);
9082 CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
9083 EXPECT_EQ(parseConfiguration("Language: Cpp\n"
9084 "IndentWidth: 34",
9085 &Style),
9086 ParseError::Unsuitable);
9087 EXPECT_EQ(23u, Style.IndentWidth);
9088 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
9089 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
9090
9091 CHECK_PARSE("BasedOnStyle: LLVM\n"
9092 "IndentWidth: 67",
9093 IndentWidth, 67u);
9094
9095 CHECK_PARSE("---\n"
9096 "Language: JavaScript\n"
9097 "IndentWidth: 12\n"
9098 "---\n"
9099 "Language: Cpp\n"
9100 "IndentWidth: 34\n"
9101 "...\n",
9102 IndentWidth, 12u);
9103
9104 Style.Language = FormatStyle::LK_Cpp;
9105 CHECK_PARSE("---\n"
9106 "Language: JavaScript\n"
9107 "IndentWidth: 12\n"
9108 "---\n"
9109 "Language: Cpp\n"
9110 "IndentWidth: 34\n"
9111 "...\n",
9112 IndentWidth, 34u);
9113 CHECK_PARSE("---\n"
9114 "IndentWidth: 78\n"
9115 "---\n"
9116 "Language: JavaScript\n"
9117 "IndentWidth: 56\n"
9118 "...\n",
9119 IndentWidth, 78u);
9120
9121 Style.ColumnLimit = 123;
9122 Style.IndentWidth = 234;
9123 Style.BreakBeforeBraces = FormatStyle::BS_Linux;
9124 Style.TabWidth = 345;
9125 EXPECT_FALSE(parseConfiguration("---\n"
9126 "IndentWidth: 456\n"
9127 "BreakBeforeBraces: Allman\n"
9128 "---\n"
9129 "Language: JavaScript\n"
9130 "IndentWidth: 111\n"
9131 "TabWidth: 111\n"
9132 "---\n"
9133 "Language: Cpp\n"
9134 "BreakBeforeBraces: Stroustrup\n"
9135 "TabWidth: 789\n"
9136 "...\n",
9137 &Style));
9138 EXPECT_EQ(123u, Style.ColumnLimit);
9139 EXPECT_EQ(456u, Style.IndentWidth);
9140 EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
9141 EXPECT_EQ(789u, Style.TabWidth);
9142
9143 EXPECT_EQ(parseConfiguration("---\n"
9144 "Language: JavaScript\n"
9145 "IndentWidth: 56\n"
9146 "---\n"
9147 "IndentWidth: 78\n"
9148 "...\n",
9149 &Style),
9150 ParseError::Error);
9151 EXPECT_EQ(parseConfiguration("---\n"
9152 "Language: JavaScript\n"
9153 "IndentWidth: 56\n"
9154 "---\n"
9155 "Language: JavaScript\n"
9156 "IndentWidth: 78\n"
9157 "...\n",
9158 &Style),
9159 ParseError::Error);
9160
9161 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
9162 }
9163
9164 #undef CHECK_PARSE
9165
TEST_F(FormatTest,UsesLanguageForBasedOnStyle)9166 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
9167 FormatStyle Style = {};
9168 Style.Language = FormatStyle::LK_JavaScript;
9169 Style.BreakBeforeTernaryOperators = true;
9170 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
9171 EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
9172
9173 Style.BreakBeforeTernaryOperators = true;
9174 EXPECT_EQ(0, parseConfiguration("---\n"
9175 "BasedOnStyle: Google\n"
9176 "---\n"
9177 "Language: JavaScript\n"
9178 "IndentWidth: 76\n"
9179 "...\n", &Style).value());
9180 EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
9181 EXPECT_EQ(76u, Style.IndentWidth);
9182 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
9183 }
9184
TEST_F(FormatTest,ConfigurationRoundTripTest)9185 TEST_F(FormatTest, ConfigurationRoundTripTest) {
9186 FormatStyle Style = getLLVMStyle();
9187 std::string YAML = configurationAsText(Style);
9188 FormatStyle ParsedStyle = {};
9189 ParsedStyle.Language = FormatStyle::LK_Cpp;
9190 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
9191 EXPECT_EQ(Style, ParsedStyle);
9192 }
9193
TEST_F(FormatTest,WorksFor8bitEncodings)9194 TEST_F(FormatTest, WorksFor8bitEncodings) {
9195 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
9196 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
9197 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
9198 "\"\xef\xee\xf0\xf3...\"",
9199 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
9200 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
9201 "\xef\xee\xf0\xf3...\"",
9202 getLLVMStyleWithColumns(12)));
9203 }
9204
TEST_F(FormatTest,HandlesUTF8BOM)9205 TEST_F(FormatTest, HandlesUTF8BOM) {
9206 EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
9207 EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
9208 format("\xef\xbb\xbf#include <iostream>"));
9209 EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
9210 format("\xef\xbb\xbf\n#include <iostream>"));
9211 }
9212
9213 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
9214 #if !defined(_MSC_VER)
9215
TEST_F(FormatTest,CountsUTF8CharactersProperly)9216 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
9217 verifyFormat("\"Однажды в студёную зимнюю пору...\"",
9218 getLLVMStyleWithColumns(35));
9219 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
9220 getLLVMStyleWithColumns(31));
9221 verifyFormat("// Однажды в студёную зимнюю пору...",
9222 getLLVMStyleWithColumns(36));
9223 verifyFormat("// 一 二 三 四 五 六 七 八 九 十",
9224 getLLVMStyleWithColumns(32));
9225 verifyFormat("/* Однажды в студёную зимнюю пору... */",
9226 getLLVMStyleWithColumns(39));
9227 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
9228 getLLVMStyleWithColumns(35));
9229 }
9230
TEST_F(FormatTest,SplitsUTF8Strings)9231 TEST_F(FormatTest, SplitsUTF8Strings) {
9232 // Non-printable characters' width is currently considered to be the length in
9233 // bytes in UTF8. The characters can be displayed in very different manner
9234 // (zero-width, single width with a substitution glyph, expanded to their code
9235 // (e.g. "<8d>"), so there's no single correct way to handle them.
9236 EXPECT_EQ("\"aaaaÄ\"\n"
9237 "\"\xc2\x8d\";",
9238 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
9239 EXPECT_EQ("\"aaaaaaaÄ\"\n"
9240 "\"\xc2\x8d\";",
9241 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
9242 EXPECT_EQ(
9243 "\"Однажды, в \"\n"
9244 "\"студёную \"\n"
9245 "\"зимнюю \"\n"
9246 "\"пору,\"",
9247 format("\"Однажды, в студёную зимнюю пору,\"",
9248 getLLVMStyleWithColumns(13)));
9249 EXPECT_EQ("\"一 二 三 \"\n"
9250 "\"四 五六 \"\n"
9251 "\"七 八 九 \"\n"
9252 "\"十\"",
9253 format("\"一 二 三 四 五六 七 八 九 十\"",
9254 getLLVMStyleWithColumns(11)));
9255 EXPECT_EQ("\"一\t二 \"\n"
9256 "\"\t三 \"\n"
9257 "\"四 五\t六 \"\n"
9258 "\"\t七 \"\n"
9259 "\"八九十\tqq\"",
9260 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
9261 getLLVMStyleWithColumns(11)));
9262 }
9263
9264
TEST_F(FormatTest,HandlesDoubleWidthCharsInMultiLineStrings)9265 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
9266 EXPECT_EQ("const char *sssss =\n"
9267 " \"一二三四五六七八\\\n"
9268 " 九 十\";",
9269 format("const char *sssss = \"一二三四五六七八\\\n"
9270 " 九 十\";",
9271 getLLVMStyleWithColumns(30)));
9272 }
9273
TEST_F(FormatTest,SplitsUTF8LineComments)9274 TEST_F(FormatTest, SplitsUTF8LineComments) {
9275 EXPECT_EQ("// aaaaÄ\xc2\x8d",
9276 format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
9277 EXPECT_EQ("// Я из лесу\n"
9278 "// вышел; был\n"
9279 "// сильный\n"
9280 "// мороз.",
9281 format("// Я из лесу вышел; был сильный мороз.",
9282 getLLVMStyleWithColumns(13)));
9283 EXPECT_EQ("// 一二三\n"
9284 "// 四五六七\n"
9285 "// 八 九\n"
9286 "// 十",
9287 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9)));
9288 }
9289
TEST_F(FormatTest,SplitsUTF8BlockComments)9290 TEST_F(FormatTest, SplitsUTF8BlockComments) {
9291 EXPECT_EQ("/* Гляжу,\n"
9292 " * поднимается\n"
9293 " * медленно в\n"
9294 " * гору\n"
9295 " * Лошадка,\n"
9296 " * везущая\n"
9297 " * хворосту\n"
9298 " * воз. */",
9299 format("/* Гляжу, поднимается медленно в гору\n"
9300 " * Лошадка, везущая хворосту воз. */",
9301 getLLVMStyleWithColumns(13)));
9302 EXPECT_EQ(
9303 "/* 一二三\n"
9304 " * 四五六七\n"
9305 " * 八 九\n"
9306 " * 十 */",
9307 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9)));
9308 EXPECT_EQ("/* �������� ��������\n"
9309 " * ��������\n"
9310 " * ������-�� */",
9311 format("/* �������� �������� �������� ������-�� */", getLLVMStyleWithColumns(12)));
9312 }
9313
9314 #endif // _MSC_VER
9315
TEST_F(FormatTest,ConstructorInitializerIndentWidth)9316 TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
9317 FormatStyle Style = getLLVMStyle();
9318
9319 Style.ConstructorInitializerIndentWidth = 4;
9320 verifyFormat(
9321 "SomeClass::Constructor()\n"
9322 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
9323 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
9324 Style);
9325
9326 Style.ConstructorInitializerIndentWidth = 2;
9327 verifyFormat(
9328 "SomeClass::Constructor()\n"
9329 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
9330 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
9331 Style);
9332
9333 Style.ConstructorInitializerIndentWidth = 0;
9334 verifyFormat(
9335 "SomeClass::Constructor()\n"
9336 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
9337 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
9338 Style);
9339 }
9340
TEST_F(FormatTest,BreakConstructorInitializersBeforeComma)9341 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
9342 FormatStyle Style = getLLVMStyle();
9343 Style.BreakConstructorInitializersBeforeComma = true;
9344 Style.ConstructorInitializerIndentWidth = 4;
9345 verifyFormat("SomeClass::Constructor()\n"
9346 " : a(a)\n"
9347 " , b(b)\n"
9348 " , c(c) {}",
9349 Style);
9350 verifyFormat("SomeClass::Constructor()\n"
9351 " : a(a) {}",
9352 Style);
9353
9354 Style.ColumnLimit = 0;
9355 verifyFormat("SomeClass::Constructor()\n"
9356 " : a(a) {}",
9357 Style);
9358 verifyFormat("SomeClass::Constructor()\n"
9359 " : a(a)\n"
9360 " , b(b)\n"
9361 " , c(c) {}",
9362 Style);
9363 verifyFormat("SomeClass::Constructor()\n"
9364 " : a(a) {\n"
9365 " foo();\n"
9366 " bar();\n"
9367 "}",
9368 Style);
9369
9370 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
9371 verifyFormat("SomeClass::Constructor()\n"
9372 " : a(a)\n"
9373 " , b(b)\n"
9374 " , c(c) {\n}",
9375 Style);
9376 verifyFormat("SomeClass::Constructor()\n"
9377 " : a(a) {\n}",
9378 Style);
9379
9380 Style.ColumnLimit = 80;
9381 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
9382 Style.ConstructorInitializerIndentWidth = 2;
9383 verifyFormat("SomeClass::Constructor()\n"
9384 " : a(a)\n"
9385 " , b(b)\n"
9386 " , c(c) {}",
9387 Style);
9388
9389 Style.ConstructorInitializerIndentWidth = 0;
9390 verifyFormat("SomeClass::Constructor()\n"
9391 ": a(a)\n"
9392 ", b(b)\n"
9393 ", c(c) {}",
9394 Style);
9395
9396 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
9397 Style.ConstructorInitializerIndentWidth = 4;
9398 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
9399 verifyFormat(
9400 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
9401 Style);
9402 verifyFormat(
9403 "SomeClass::Constructor()\n"
9404 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
9405 Style);
9406 Style.ConstructorInitializerIndentWidth = 4;
9407 Style.ColumnLimit = 60;
9408 verifyFormat("SomeClass::Constructor()\n"
9409 " : aaaaaaaa(aaaaaaaa)\n"
9410 " , aaaaaaaa(aaaaaaaa)\n"
9411 " , aaaaaaaa(aaaaaaaa) {}",
9412 Style);
9413 }
9414
TEST_F(FormatTest,Destructors)9415 TEST_F(FormatTest, Destructors) {
9416 verifyFormat("void F(int &i) { i.~int(); }");
9417 verifyFormat("void F(int &i) { i->~int(); }");
9418 }
9419
TEST_F(FormatTest,FormatsWithWebKitStyle)9420 TEST_F(FormatTest, FormatsWithWebKitStyle) {
9421 FormatStyle Style = getWebKitStyle();
9422
9423 // Don't indent in outer namespaces.
9424 verifyFormat("namespace outer {\n"
9425 "int i;\n"
9426 "namespace inner {\n"
9427 " int i;\n"
9428 "} // namespace inner\n"
9429 "} // namespace outer\n"
9430 "namespace other_outer {\n"
9431 "int i;\n"
9432 "}",
9433 Style);
9434
9435 // Don't indent case labels.
9436 verifyFormat("switch (variable) {\n"
9437 "case 1:\n"
9438 "case 2:\n"
9439 " doSomething();\n"
9440 " break;\n"
9441 "default:\n"
9442 " ++variable;\n"
9443 "}",
9444 Style);
9445
9446 // Wrap before binary operators.
9447 EXPECT_EQ(
9448 "void f()\n"
9449 "{\n"
9450 " if (aaaaaaaaaaaaaaaa\n"
9451 " && bbbbbbbbbbbbbbbbbbbbbbbb\n"
9452 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
9453 " return;\n"
9454 "}",
9455 format(
9456 "void f() {\n"
9457 "if (aaaaaaaaaaaaaaaa\n"
9458 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
9459 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
9460 "return;\n"
9461 "}",
9462 Style));
9463
9464 // Allow functions on a single line.
9465 verifyFormat("void f() { return; }", Style);
9466
9467 // Constructor initializers are formatted one per line with the "," on the
9468 // new line.
9469 verifyFormat("Constructor()\n"
9470 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9471 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
9472 " aaaaaaaaaaaaaa)\n"
9473 " , aaaaaaaaaaaaaaaaaaaaaaa()\n"
9474 "{\n"
9475 "}",
9476 Style);
9477 verifyFormat("SomeClass::Constructor()\n"
9478 " : a(a)\n"
9479 "{\n"
9480 "}",
9481 Style);
9482 EXPECT_EQ("SomeClass::Constructor()\n"
9483 " : a(a)\n"
9484 "{\n"
9485 "}",
9486 format("SomeClass::Constructor():a(a){}", Style));
9487 verifyFormat("SomeClass::Constructor()\n"
9488 " : a(a)\n"
9489 " , b(b)\n"
9490 " , c(c)\n"
9491 "{\n"
9492 "}", Style);
9493 verifyFormat("SomeClass::Constructor()\n"
9494 " : a(a)\n"
9495 "{\n"
9496 " foo();\n"
9497 " bar();\n"
9498 "}",
9499 Style);
9500
9501 // Access specifiers should be aligned left.
9502 verifyFormat("class C {\n"
9503 "public:\n"
9504 " int i;\n"
9505 "};",
9506 Style);
9507
9508 // Do not align comments.
9509 verifyFormat("int a; // Do not\n"
9510 "double b; // align comments.",
9511 Style);
9512
9513 // Do not align operands.
9514 EXPECT_EQ("ASSERT(aaaa\n"
9515 " || bbbb);",
9516 format("ASSERT ( aaaa\n||bbbb);", Style));
9517
9518 // Accept input's line breaks.
9519 EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
9520 " || bbbbbbbbbbbbbbb) {\n"
9521 " i++;\n"
9522 "}",
9523 format("if (aaaaaaaaaaaaaaa\n"
9524 "|| bbbbbbbbbbbbbbb) { i++; }",
9525 Style));
9526 EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
9527 " i++;\n"
9528 "}",
9529 format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
9530
9531 // Don't automatically break all macro definitions (llvm.org/PR17842).
9532 verifyFormat("#define aNumber 10", Style);
9533 // However, generally keep the line breaks that the user authored.
9534 EXPECT_EQ("#define aNumber \\\n"
9535 " 10",
9536 format("#define aNumber \\\n"
9537 " 10",
9538 Style));
9539
9540 // Keep empty and one-element array literals on a single line.
9541 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
9542 " copyItems:YES];",
9543 format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
9544 "copyItems:YES];",
9545 Style));
9546 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
9547 " copyItems:YES];",
9548 format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
9549 " copyItems:YES];",
9550 Style));
9551 // FIXME: This does not seem right, there should be more indentation before
9552 // the array literal's entries. Nested blocks have the same problem.
9553 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
9554 " @\"a\",\n"
9555 " @\"a\"\n"
9556 "]\n"
9557 " copyItems:YES];",
9558 format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
9559 " @\"a\",\n"
9560 " @\"a\"\n"
9561 " ]\n"
9562 " copyItems:YES];",
9563 Style));
9564 EXPECT_EQ(
9565 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
9566 " copyItems:YES];",
9567 format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
9568 " copyItems:YES];",
9569 Style));
9570
9571 verifyFormat("[self.a b:c c:d];", Style);
9572 EXPECT_EQ("[self.a b:c\n"
9573 " c:d];",
9574 format("[self.a b:c\n"
9575 "c:d];",
9576 Style));
9577 }
9578
TEST_F(FormatTest,FormatsLambdas)9579 TEST_F(FormatTest, FormatsLambdas) {
9580 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
9581 verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
9582 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
9583 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
9584 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
9585 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
9586 verifyFormat("void f() {\n"
9587 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
9588 "}\n");
9589 verifyFormat("void f() {\n"
9590 " other(x.begin(), //\n"
9591 " x.end(), //\n"
9592 " [&](int, int) { return 1; });\n"
9593 "}\n");
9594 verifyFormat("SomeFunction([]() { // A cool function...\n"
9595 " return 43;\n"
9596 "});");
9597 EXPECT_EQ("SomeFunction([]() {\n"
9598 "#define A a\n"
9599 " return 43;\n"
9600 "});",
9601 format("SomeFunction([](){\n"
9602 "#define A a\n"
9603 "return 43;\n"
9604 "});"));
9605 verifyFormat("void f() {\n"
9606 " SomeFunction([](decltype(x), A *a) {});\n"
9607 "}");
9608 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9609 " [](const aaaaaaaaaa &a) { return a; });");
9610 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
9611 " SomeOtherFunctioooooooooooooooooooooooooon();\n"
9612 "});");
9613 verifyFormat("Constructor()\n"
9614 " : Field([] { // comment\n"
9615 " int i;\n"
9616 " }) {}");
9617
9618 // Lambdas with return types.
9619 verifyFormat("int c = []() -> int { return 2; }();\n");
9620 verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
9621 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
9622 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
9623 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
9624 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
9625 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
9626 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
9627 " int j) -> int {\n"
9628 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
9629 "};");
9630 verifyFormat(
9631 "aaaaaaaaaaaaaaaaaaaaaa(\n"
9632 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
9633 " return aaaaaaaaaaaaaaaaa;\n"
9634 " });",
9635 getLLVMStyleWithColumns(70));
9636
9637 // Multiple lambdas in the same parentheses change indentation rules.
9638 verifyFormat("SomeFunction(\n"
9639 " []() {\n"
9640 " int i = 42;\n"
9641 " return i;\n"
9642 " },\n"
9643 " []() {\n"
9644 " int j = 43;\n"
9645 " return j;\n"
9646 " });");
9647
9648 // More complex introducers.
9649 verifyFormat("return [i, args...] {};");
9650
9651 // Not lambdas.
9652 verifyFormat("constexpr char hello[]{\"hello\"};");
9653 verifyFormat("double &operator[](int i) { return 0; }\n"
9654 "int i;");
9655 verifyFormat("std::unique_ptr<int[]> foo() {}");
9656 verifyFormat("int i = a[a][a]->f();");
9657 verifyFormat("int i = (*b)[a]->f();");
9658
9659 // Other corner cases.
9660 verifyFormat("void f() {\n"
9661 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
9662 " );\n"
9663 "}");
9664
9665 // Lambdas created through weird macros.
9666 verifyFormat("void f() {\n"
9667 " MACRO((const AA &a) { return 1; });\n"
9668 "}");
9669
9670 verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
9671 " doo_dah();\n"
9672 " doo_dah();\n"
9673 " })) {\n"
9674 "}");
9675 verifyFormat("auto lambda = []() {\n"
9676 " int a = 2\n"
9677 "#if A\n"
9678 " + 2\n"
9679 "#endif\n"
9680 " ;\n"
9681 "};");
9682 }
9683
TEST_F(FormatTest,FormatsBlocks)9684 TEST_F(FormatTest, FormatsBlocks) {
9685 FormatStyle ShortBlocks = getLLVMStyle();
9686 ShortBlocks.AllowShortBlocksOnASingleLine = true;
9687 verifyFormat("int (^Block)(int, int);", ShortBlocks);
9688 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
9689 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
9690 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
9691 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
9692 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
9693
9694 verifyFormat("foo(^{ bar(); });", ShortBlocks);
9695 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
9696 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
9697
9698 verifyFormat("[operation setCompletionBlock:^{\n"
9699 " [self onOperationDone];\n"
9700 "}];");
9701 verifyFormat("int i = {[operation setCompletionBlock:^{\n"
9702 " [self onOperationDone];\n"
9703 "}]};");
9704 verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
9705 " f();\n"
9706 "}];");
9707 verifyFormat("int a = [operation block:^int(int *i) {\n"
9708 " return 1;\n"
9709 "}];");
9710 verifyFormat("[myObject doSomethingWith:arg1\n"
9711 " aaa:^int(int *a) {\n"
9712 " return 1;\n"
9713 " }\n"
9714 " bbb:f(a * bbbbbbbb)];");
9715
9716 verifyFormat("[operation setCompletionBlock:^{\n"
9717 " [self.delegate newDataAvailable];\n"
9718 "}];",
9719 getLLVMStyleWithColumns(60));
9720 verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
9721 " NSString *path = [self sessionFilePath];\n"
9722 " if (path) {\n"
9723 " // ...\n"
9724 " }\n"
9725 "});");
9726 verifyFormat("[[SessionService sharedService]\n"
9727 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
9728 " if (window) {\n"
9729 " [self windowDidLoad:window];\n"
9730 " } else {\n"
9731 " [self errorLoadingWindow];\n"
9732 " }\n"
9733 " }];");
9734 verifyFormat("void (^largeBlock)(void) = ^{\n"
9735 " // ...\n"
9736 "};\n",
9737 getLLVMStyleWithColumns(40));
9738 verifyFormat("[[SessionService sharedService]\n"
9739 " loadWindowWithCompletionBlock: //\n"
9740 " ^(SessionWindow *window) {\n"
9741 " if (window) {\n"
9742 " [self windowDidLoad:window];\n"
9743 " } else {\n"
9744 " [self errorLoadingWindow];\n"
9745 " }\n"
9746 " }];",
9747 getLLVMStyleWithColumns(60));
9748 verifyFormat("[myObject doSomethingWith:arg1\n"
9749 " firstBlock:^(Foo *a) {\n"
9750 " // ...\n"
9751 " int i;\n"
9752 " }\n"
9753 " secondBlock:^(Bar *b) {\n"
9754 " // ...\n"
9755 " int i;\n"
9756 " }\n"
9757 " thirdBlock:^Foo(Bar *b) {\n"
9758 " // ...\n"
9759 " int i;\n"
9760 " }];");
9761 verifyFormat("[myObject doSomethingWith:arg1\n"
9762 " firstBlock:-1\n"
9763 " secondBlock:^(Bar *b) {\n"
9764 " // ...\n"
9765 " int i;\n"
9766 " }];");
9767
9768 verifyFormat("f(^{\n"
9769 " @autoreleasepool {\n"
9770 " if (a) {\n"
9771 " g();\n"
9772 " }\n"
9773 " }\n"
9774 "});");
9775 verifyFormat("Block b = ^int *(A *a, B *b) {}");
9776
9777 FormatStyle FourIndent = getLLVMStyle();
9778 FourIndent.ObjCBlockIndentWidth = 4;
9779 verifyFormat("[operation setCompletionBlock:^{\n"
9780 " [self onOperationDone];\n"
9781 "}];",
9782 FourIndent);
9783 }
9784
TEST_F(FormatTest,SupportsCRLF)9785 TEST_F(FormatTest, SupportsCRLF) {
9786 EXPECT_EQ("int a;\r\n"
9787 "int b;\r\n"
9788 "int c;\r\n",
9789 format("int a;\r\n"
9790 " int b;\r\n"
9791 " int c;\r\n",
9792 getLLVMStyle()));
9793 EXPECT_EQ("int a;\r\n"
9794 "int b;\r\n"
9795 "int c;\r\n",
9796 format("int a;\r\n"
9797 " int b;\n"
9798 " int c;\r\n",
9799 getLLVMStyle()));
9800 EXPECT_EQ("int a;\n"
9801 "int b;\n"
9802 "int c;\n",
9803 format("int a;\r\n"
9804 " int b;\n"
9805 " int c;\n",
9806 getLLVMStyle()));
9807 EXPECT_EQ("\"aaaaaaa \"\r\n"
9808 "\"bbbbbbb\";\r\n",
9809 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
9810 EXPECT_EQ("#define A \\\r\n"
9811 " b; \\\r\n"
9812 " c; \\\r\n"
9813 " d;\r\n",
9814 format("#define A \\\r\n"
9815 " b; \\\r\n"
9816 " c; d; \r\n",
9817 getGoogleStyle()));
9818
9819 EXPECT_EQ("/*\r\n"
9820 "multi line block comments\r\n"
9821 "should not introduce\r\n"
9822 "an extra carriage return\r\n"
9823 "*/\r\n",
9824 format("/*\r\n"
9825 "multi line block comments\r\n"
9826 "should not introduce\r\n"
9827 "an extra carriage return\r\n"
9828 "*/\r\n"));
9829 }
9830
TEST_F(FormatTest,MunchSemicolonAfterBlocks)9831 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
9832 verifyFormat("MY_CLASS(C) {\n"
9833 " int i;\n"
9834 " int j;\n"
9835 "};");
9836 }
9837
TEST_F(FormatTest,ConfigurableContinuationIndentWidth)9838 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
9839 FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
9840 TwoIndent.ContinuationIndentWidth = 2;
9841
9842 EXPECT_EQ("int i =\n"
9843 " longFunction(\n"
9844 " arg);",
9845 format("int i = longFunction(arg);", TwoIndent));
9846
9847 FormatStyle SixIndent = getLLVMStyleWithColumns(20);
9848 SixIndent.ContinuationIndentWidth = 6;
9849
9850 EXPECT_EQ("int i =\n"
9851 " longFunction(\n"
9852 " arg);",
9853 format("int i = longFunction(arg);", SixIndent));
9854 }
9855
TEST_F(FormatTest,SpacesInAngles)9856 TEST_F(FormatTest, SpacesInAngles) {
9857 FormatStyle Spaces = getLLVMStyle();
9858 Spaces.SpacesInAngles = true;
9859
9860 verifyFormat("static_cast< int >(arg);", Spaces);
9861 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
9862 verifyFormat("f< int, float >();", Spaces);
9863 verifyFormat("template <> g() {}", Spaces);
9864 verifyFormat("template < std::vector< int > > f() {}", Spaces);
9865
9866 Spaces.Standard = FormatStyle::LS_Cpp03;
9867 Spaces.SpacesInAngles = true;
9868 verifyFormat("A< A< int > >();", Spaces);
9869
9870 Spaces.SpacesInAngles = false;
9871 verifyFormat("A<A<int> >();", Spaces);
9872
9873 Spaces.Standard = FormatStyle::LS_Cpp11;
9874 Spaces.SpacesInAngles = true;
9875 verifyFormat("A< A< int > >();", Spaces);
9876
9877 Spaces.SpacesInAngles = false;
9878 verifyFormat("A<A<int>>();", Spaces);
9879 }
9880
TEST_F(FormatTest,TripleAngleBrackets)9881 TEST_F(FormatTest, TripleAngleBrackets) {
9882 verifyFormat("f<<<1, 1>>>();");
9883 verifyFormat("f<<<1, 1, 1, s>>>();");
9884 verifyFormat("f<<<a, b, c, d>>>();");
9885 EXPECT_EQ("f<<<1, 1>>>();",
9886 format("f <<< 1, 1 >>> ();"));
9887 verifyFormat("f<param><<<1, 1>>>();");
9888 verifyFormat("f<1><<<1, 1>>>();");
9889 EXPECT_EQ("f<param><<<1, 1>>>();",
9890 format("f< param > <<< 1, 1 >>> ();"));
9891 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
9892 "aaaaaaaaaaa<<<\n 1, 1>>>();");
9893 }
9894
TEST_F(FormatTest,MergeLessLessAtEnd)9895 TEST_F(FormatTest, MergeLessLessAtEnd) {
9896 verifyFormat("<<");
9897 EXPECT_EQ("< < <", format("\\\n<<<"));
9898 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
9899 "aaallvm::outs() <<");
9900 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
9901 "aaaallvm::outs()\n <<");
9902 }
9903
TEST_F(FormatTest,HandleUnbalancedImplicitBracesAcrossPPBranches)9904 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
9905 std::string code = "#if A\n"
9906 "#if B\n"
9907 "a.\n"
9908 "#endif\n"
9909 " a = 1;\n"
9910 "#else\n"
9911 "#endif\n"
9912 "#if C\n"
9913 "#else\n"
9914 "#endif\n";
9915 EXPECT_EQ(code, format(code));
9916 }
9917
TEST_F(FormatTest,HandleConflictMarkers)9918 TEST_F(FormatTest, HandleConflictMarkers) {
9919 // Git/SVN conflict markers.
9920 EXPECT_EQ("int a;\n"
9921 "void f() {\n"
9922 " callme(some(parameter1,\n"
9923 "<<<<<<< text by the vcs\n"
9924 " parameter2),\n"
9925 "||||||| text by the vcs\n"
9926 " parameter2),\n"
9927 " parameter3,\n"
9928 "======= text by the vcs\n"
9929 " parameter2, parameter3),\n"
9930 ">>>>>>> text by the vcs\n"
9931 " otherparameter);\n",
9932 format("int a;\n"
9933 "void f() {\n"
9934 " callme(some(parameter1,\n"
9935 "<<<<<<< text by the vcs\n"
9936 " parameter2),\n"
9937 "||||||| text by the vcs\n"
9938 " parameter2),\n"
9939 " parameter3,\n"
9940 "======= text by the vcs\n"
9941 " parameter2,\n"
9942 " parameter3),\n"
9943 ">>>>>>> text by the vcs\n"
9944 " otherparameter);\n"));
9945
9946 // Perforce markers.
9947 EXPECT_EQ("void f() {\n"
9948 " function(\n"
9949 ">>>> text by the vcs\n"
9950 " parameter,\n"
9951 "==== text by the vcs\n"
9952 " parameter,\n"
9953 "==== text by the vcs\n"
9954 " parameter,\n"
9955 "<<<< text by the vcs\n"
9956 " parameter);\n",
9957 format("void f() {\n"
9958 " function(\n"
9959 ">>>> text by the vcs\n"
9960 " parameter,\n"
9961 "==== text by the vcs\n"
9962 " parameter,\n"
9963 "==== text by the vcs\n"
9964 " parameter,\n"
9965 "<<<< text by the vcs\n"
9966 " parameter);\n"));
9967
9968 EXPECT_EQ("<<<<<<<\n"
9969 "|||||||\n"
9970 "=======\n"
9971 ">>>>>>>",
9972 format("<<<<<<<\n"
9973 "|||||||\n"
9974 "=======\n"
9975 ">>>>>>>"));
9976
9977 EXPECT_EQ("<<<<<<<\n"
9978 "|||||||\n"
9979 "int i;\n"
9980 "=======\n"
9981 ">>>>>>>",
9982 format("<<<<<<<\n"
9983 "|||||||\n"
9984 "int i;\n"
9985 "=======\n"
9986 ">>>>>>>"));
9987
9988 // FIXME: Handle parsing of macros around conflict markers correctly:
9989 EXPECT_EQ("#define Macro \\\n"
9990 "<<<<<<<\n"
9991 "Something \\\n"
9992 "|||||||\n"
9993 "Else \\\n"
9994 "=======\n"
9995 "Other \\\n"
9996 ">>>>>>>\n"
9997 " End int i;\n",
9998 format("#define Macro \\\n"
9999 "<<<<<<<\n"
10000 " Something \\\n"
10001 "|||||||\n"
10002 " Else \\\n"
10003 "=======\n"
10004 " Other \\\n"
10005 ">>>>>>>\n"
10006 " End\n"
10007 "int i;\n"));
10008 }
10009
TEST_F(FormatTest,DisableRegions)10010 TEST_F(FormatTest, DisableRegions) {
10011 EXPECT_EQ("int i;\n"
10012 "// clang-format off\n"
10013 " int j;\n"
10014 "// clang-format on\n"
10015 "int k;",
10016 format(" int i;\n"
10017 " // clang-format off\n"
10018 " int j;\n"
10019 " // clang-format on\n"
10020 " int k;"));
10021 EXPECT_EQ("int i;\n"
10022 "/* clang-format off */\n"
10023 " int j;\n"
10024 "/* clang-format on */\n"
10025 "int k;",
10026 format(" int i;\n"
10027 " /* clang-format off */\n"
10028 " int j;\n"
10029 " /* clang-format on */\n"
10030 " int k;"));
10031 }
10032
TEST_F(FormatTest,DoNotCrashOnInvalidInput)10033 TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
10034 format("? ) =");
10035 }
10036
10037 } // end namespace tooling
10038 } // end namespace clang
10039