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