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