xref: /llvm-project/clang/unittests/Format/FormatTest.cpp (revision 1c220488346e2f471147b7a42f5c31de49ffdd12)
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("string // break\n"
5168                "operator()() & {}");
5169   verifyFormat("string // break\n"
5170                "operator()() && {}");
5171 }
5172 
5173 TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
5174   verifyFormat("Deleted &operator=(const Deleted &)& = default;");
5175   verifyFormat("Deleted &operator=(const Deleted &)&& = delete;");
5176   verifyFormat("SomeType MemberFunction(const Deleted &)& = delete;");
5177   verifyFormat("SomeType MemberFunction(const Deleted &)&& = delete;");
5178   verifyFormat("Deleted &operator=(const Deleted &)&;");
5179   verifyFormat("Deleted &operator=(const Deleted &)&&;");
5180   verifyFormat("SomeType MemberFunction(const Deleted &)&;");
5181   verifyFormat("SomeType MemberFunction(const Deleted &)&&;");
5182 
5183   verifyGoogleFormat("Deleted& operator=(const Deleted&)& = default;");
5184   verifyGoogleFormat("SomeType MemberFunction(const Deleted&)& = delete;");
5185   verifyGoogleFormat("Deleted& operator=(const Deleted&)&;");
5186   verifyGoogleFormat("SomeType MemberFunction(const Deleted&)&;");
5187 
5188   FormatStyle Spaces = getLLVMStyle();
5189   Spaces.SpacesInCStyleCastParentheses = true;
5190   verifyFormat("Deleted &operator=(const Deleted &)& = default;", Spaces);
5191   verifyFormat("SomeType MemberFunction(const Deleted &)& = delete;", Spaces);
5192   verifyFormat("Deleted &operator=(const Deleted &)&;", Spaces);
5193   verifyFormat("SomeType MemberFunction(const Deleted &)&;", Spaces);
5194 
5195   Spaces.SpacesInCStyleCastParentheses = false;
5196   Spaces.SpacesInParentheses = true;
5197   verifyFormat("Deleted &operator=( const Deleted & )& = default;", Spaces);
5198   verifyFormat("SomeType MemberFunction( const Deleted & )& = delete;", Spaces);
5199   verifyFormat("Deleted &operator=( const Deleted & )&;", Spaces);
5200   verifyFormat("SomeType MemberFunction( const Deleted & )&;", Spaces);
5201 }
5202 
5203 TEST_F(FormatTest, UnderstandsNewAndDelete) {
5204   verifyFormat("void f() {\n"
5205                "  A *a = new A;\n"
5206                "  A *a = new (placement) A;\n"
5207                "  delete a;\n"
5208                "  delete (A *)a;\n"
5209                "}");
5210   verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
5211                "    typename aaaaaaaaaaaaaaaaaaaaaaaa();");
5212   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5213                "    new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
5214                "        typename aaaaaaaaaaaaaaaaaaaaaaaa();");
5215   verifyFormat("delete[] h->p;");
5216 }
5217 
5218 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
5219   verifyFormat("int *f(int *a) {}");
5220   verifyFormat("int main(int argc, char **argv) {}");
5221   verifyFormat("Test::Test(int b) : a(b * b) {}");
5222   verifyIndependentOfContext("f(a, *a);");
5223   verifyFormat("void g() { f(*a); }");
5224   verifyIndependentOfContext("int a = b * 10;");
5225   verifyIndependentOfContext("int a = 10 * b;");
5226   verifyIndependentOfContext("int a = b * c;");
5227   verifyIndependentOfContext("int a += b * c;");
5228   verifyIndependentOfContext("int a -= b * c;");
5229   verifyIndependentOfContext("int a *= b * c;");
5230   verifyIndependentOfContext("int a /= b * c;");
5231   verifyIndependentOfContext("int a = *b;");
5232   verifyIndependentOfContext("int a = *b * c;");
5233   verifyIndependentOfContext("int a = b * *c;");
5234   verifyIndependentOfContext("return 10 * b;");
5235   verifyIndependentOfContext("return *b * *c;");
5236   verifyIndependentOfContext("return a & ~b;");
5237   verifyIndependentOfContext("f(b ? *c : *d);");
5238   verifyIndependentOfContext("int a = b ? *c : *d;");
5239   verifyIndependentOfContext("*b = a;");
5240   verifyIndependentOfContext("a * ~b;");
5241   verifyIndependentOfContext("a * !b;");
5242   verifyIndependentOfContext("a * +b;");
5243   verifyIndependentOfContext("a * -b;");
5244   verifyIndependentOfContext("a * ++b;");
5245   verifyIndependentOfContext("a * --b;");
5246   verifyIndependentOfContext("a[4] * b;");
5247   verifyIndependentOfContext("a[a * a] = 1;");
5248   verifyIndependentOfContext("f() * b;");
5249   verifyIndependentOfContext("a * [self dostuff];");
5250   verifyIndependentOfContext("int x = a * (a + b);");
5251   verifyIndependentOfContext("(a *)(a + b);");
5252   verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
5253   verifyIndependentOfContext("int *pa = (int *)&a;");
5254   verifyIndependentOfContext("return sizeof(int **);");
5255   verifyIndependentOfContext("return sizeof(int ******);");
5256   verifyIndependentOfContext("return (int **&)a;");
5257   verifyIndependentOfContext("f((*PointerToArray)[10]);");
5258   verifyFormat("void f(Type (*parameter)[10]) {}");
5259   verifyGoogleFormat("return sizeof(int**);");
5260   verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
5261   verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
5262   verifyFormat("auto a = [](int **&, int ***) {};");
5263   verifyFormat("auto PointerBinding = [](const char *S) {};");
5264   verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
5265   verifyFormat("[](const decltype(*a) &value) {}");
5266   verifyIndependentOfContext("typedef void (*f)(int *a);");
5267   verifyIndependentOfContext("int i{a * b};");
5268   verifyIndependentOfContext("aaa && aaa->f();");
5269   verifyIndependentOfContext("int x = ~*p;");
5270   verifyFormat("Constructor() : a(a), area(width * height) {}");
5271   verifyFormat("Constructor() : a(a), area(a, width * height) {}");
5272   verifyFormat("void f() { f(a, c * d); }");
5273 
5274   verifyIndependentOfContext("InvalidRegions[*R] = 0;");
5275 
5276   verifyIndependentOfContext("A<int *> a;");
5277   verifyIndependentOfContext("A<int **> a;");
5278   verifyIndependentOfContext("A<int *, int *> a;");
5279   verifyIndependentOfContext("A<int *[]> a;");
5280   verifyIndependentOfContext(
5281       "const char *const p = reinterpret_cast<const char *const>(q);");
5282   verifyIndependentOfContext("A<int **, int **> a;");
5283   verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
5284   verifyFormat("for (char **a = b; *a; ++a) {\n}");
5285   verifyFormat("for (; a && b;) {\n}");
5286   verifyFormat("bool foo = true && [] { return false; }();");
5287 
5288   verifyFormat(
5289       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5290       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5291 
5292   verifyGoogleFormat("**outparam = 1;");
5293   verifyGoogleFormat("*outparam = a * b;");
5294   verifyGoogleFormat("int main(int argc, char** argv) {}");
5295   verifyGoogleFormat("A<int*> a;");
5296   verifyGoogleFormat("A<int**> a;");
5297   verifyGoogleFormat("A<int*, int*> a;");
5298   verifyGoogleFormat("A<int**, int**> a;");
5299   verifyGoogleFormat("f(b ? *c : *d);");
5300   verifyGoogleFormat("int a = b ? *c : *d;");
5301   verifyGoogleFormat("Type* t = **x;");
5302   verifyGoogleFormat("Type* t = *++*x;");
5303   verifyGoogleFormat("*++*x;");
5304   verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
5305   verifyGoogleFormat("Type* t = x++ * y;");
5306   verifyGoogleFormat(
5307       "const char* const p = reinterpret_cast<const char* const>(q);");
5308   verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
5309   verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
5310   verifyGoogleFormat("template <typename T>\n"
5311                      "void f(int i = 0, SomeType** temps = NULL);");
5312 
5313   FormatStyle Left = getLLVMStyle();
5314   Left.PointerAlignment = FormatStyle::PAS_Left;
5315   verifyFormat("x = *a(x) = *a(y);", Left);
5316 
5317   verifyIndependentOfContext("a = *(x + y);");
5318   verifyIndependentOfContext("a = &(x + y);");
5319   verifyIndependentOfContext("*(x + y).call();");
5320   verifyIndependentOfContext("&(x + y)->call();");
5321   verifyFormat("void f() { &(*I).first; }");
5322 
5323   verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
5324   verifyFormat(
5325       "int *MyValues = {\n"
5326       "    *A, // Operator detection might be confused by the '{'\n"
5327       "    *BB // Operator detection might be confused by previous comment\n"
5328       "};");
5329 
5330   verifyIndependentOfContext("if (int *a = &b)");
5331   verifyIndependentOfContext("if (int &a = *b)");
5332   verifyIndependentOfContext("if (a & b[i])");
5333   verifyIndependentOfContext("if (a::b::c::d & b[i])");
5334   verifyIndependentOfContext("if (*b[i])");
5335   verifyIndependentOfContext("if (int *a = (&b))");
5336   verifyIndependentOfContext("while (int *a = &b)");
5337   verifyIndependentOfContext("size = sizeof *a;");
5338   verifyFormat("void f() {\n"
5339                "  for (const int &v : Values) {\n"
5340                "  }\n"
5341                "}");
5342   verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
5343   verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
5344   verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
5345 
5346   verifyFormat("#define A (!a * b)");
5347   verifyFormat("#define MACRO     \\\n"
5348                "  int *i = a * b; \\\n"
5349                "  void f(a *b);",
5350                getLLVMStyleWithColumns(19));
5351 
5352   verifyIndependentOfContext("A = new SomeType *[Length];");
5353   verifyIndependentOfContext("A = new SomeType *[Length]();");
5354   verifyIndependentOfContext("T **t = new T *;");
5355   verifyIndependentOfContext("T **t = new T *();");
5356   verifyGoogleFormat("A = new SomeType* [Length]();");
5357   verifyGoogleFormat("A = new SomeType* [Length];");
5358   verifyGoogleFormat("T** t = new T*;");
5359   verifyGoogleFormat("T** t = new T*();");
5360 
5361   FormatStyle PointerLeft = getLLVMStyle();
5362   PointerLeft.PointerAlignment = FormatStyle::PAS_Left;
5363   verifyFormat("delete *x;", PointerLeft);
5364   verifyFormat("STATIC_ASSERT((a & b) == 0);");
5365   verifyFormat("STATIC_ASSERT(0 == (a & b));");
5366   verifyFormat("template <bool a, bool b> "
5367                "typename t::if<x && y>::type f() {}");
5368   verifyFormat("template <int *y> f() {}");
5369   verifyFormat("vector<int *> v;");
5370   verifyFormat("vector<int *const> v;");
5371   verifyFormat("vector<int *const **const *> v;");
5372   verifyFormat("vector<int *volatile> v;");
5373   verifyFormat("vector<a * b> v;");
5374   verifyFormat("foo<b && false>();");
5375   verifyFormat("foo<b & 1>();");
5376   verifyFormat("decltype(*::std::declval<const T &>()) void F();");
5377   verifyFormat(
5378       "template <class T, class = typename std::enable_if<\n"
5379       "                       std::is_integral<T>::value &&\n"
5380       "                       (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
5381       "void F();",
5382       getLLVMStyleWithColumns(76));
5383   verifyFormat(
5384       "template <class T,\n"
5385       "          class = typename ::std::enable_if<\n"
5386       "              ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
5387       "void F();",
5388       getGoogleStyleWithColumns(68));
5389 
5390   verifyIndependentOfContext("MACRO(int *i);");
5391   verifyIndependentOfContext("MACRO(auto *a);");
5392   verifyIndependentOfContext("MACRO(const A *a);");
5393   verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
5394   // FIXME: Is there a way to make this work?
5395   // verifyIndependentOfContext("MACRO(A *a);");
5396 
5397   verifyFormat("DatumHandle const *operator->() const { return input_; }");
5398 
5399   EXPECT_EQ("#define OP(x)                                    \\\n"
5400             "  ostream &operator<<(ostream &s, const A &a) {  \\\n"
5401             "    return s << a.DebugString();                 \\\n"
5402             "  }",
5403             format("#define OP(x) \\\n"
5404                    "  ostream &operator<<(ostream &s, const A &a) { \\\n"
5405                    "    return s << a.DebugString(); \\\n"
5406                    "  }",
5407                    getLLVMStyleWithColumns(50)));
5408 
5409   // FIXME: We cannot handle this case yet; we might be able to figure out that
5410   // foo<x> d > v; doesn't make sense.
5411   verifyFormat("foo<a<b && c> d> v;");
5412 
5413   FormatStyle PointerMiddle = getLLVMStyle();
5414   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
5415   verifyFormat("delete *x;", PointerMiddle);
5416   verifyFormat("int * x;", PointerMiddle);
5417   verifyFormat("template <int * y> f() {}", PointerMiddle);
5418   verifyFormat("int * f(int * a) {}", PointerMiddle);
5419   verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
5420   verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
5421   verifyFormat("A<int *> a;", PointerMiddle);
5422   verifyFormat("A<int **> a;", PointerMiddle);
5423   verifyFormat("A<int *, int *> a;", PointerMiddle);
5424   verifyFormat("A<int * []> a;", PointerMiddle);
5425   verifyFormat("A = new SomeType * [Length]();", PointerMiddle);
5426   verifyFormat("A = new SomeType * [Length];", PointerMiddle);
5427   verifyFormat("T ** t = new T *;", PointerMiddle);
5428 }
5429 
5430 TEST_F(FormatTest, UnderstandsAttributes) {
5431   verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
5432   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
5433                "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
5434 }
5435 
5436 TEST_F(FormatTest, UnderstandsEllipsis) {
5437   verifyFormat("int printf(const char *fmt, ...);");
5438   verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
5439   verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}");
5440 
5441   FormatStyle PointersLeft = getLLVMStyle();
5442   PointersLeft.PointerAlignment = FormatStyle::PAS_Left;
5443   verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft);
5444 }
5445 
5446 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
5447   EXPECT_EQ("int *a;\n"
5448             "int *a;\n"
5449             "int *a;",
5450             format("int *a;\n"
5451                    "int* a;\n"
5452                    "int *a;",
5453                    getGoogleStyle()));
5454   EXPECT_EQ("int* a;\n"
5455             "int* a;\n"
5456             "int* a;",
5457             format("int* a;\n"
5458                    "int* a;\n"
5459                    "int *a;",
5460                    getGoogleStyle()));
5461   EXPECT_EQ("int *a;\n"
5462             "int *a;\n"
5463             "int *a;",
5464             format("int *a;\n"
5465                    "int * a;\n"
5466                    "int *  a;",
5467                    getGoogleStyle()));
5468 }
5469 
5470 TEST_F(FormatTest, UnderstandsRvalueReferences) {
5471   verifyFormat("int f(int &&a) {}");
5472   verifyFormat("int f(int a, char &&b) {}");
5473   verifyFormat("void f() { int &&a = b; }");
5474   verifyGoogleFormat("int f(int a, char&& b) {}");
5475   verifyGoogleFormat("void f() { int&& a = b; }");
5476 
5477   verifyIndependentOfContext("A<int &&> a;");
5478   verifyIndependentOfContext("A<int &&, int &&> a;");
5479   verifyGoogleFormat("A<int&&> a;");
5480   verifyGoogleFormat("A<int&&, int&&> a;");
5481 
5482   // Not rvalue references:
5483   verifyFormat("template <bool B, bool C> class A {\n"
5484                "  static_assert(B && C, \"Something is wrong\");\n"
5485                "};");
5486   verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
5487   verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
5488   verifyFormat("#define A(a, b) (a && b)");
5489 }
5490 
5491 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
5492   verifyFormat("void f() {\n"
5493                "  x[aaaaaaaaa -\n"
5494                "    b] = 23;\n"
5495                "}",
5496                getLLVMStyleWithColumns(15));
5497 }
5498 
5499 TEST_F(FormatTest, FormatsCasts) {
5500   verifyFormat("Type *A = static_cast<Type *>(P);");
5501   verifyFormat("Type *A = (Type *)P;");
5502   verifyFormat("Type *A = (vector<Type *, int *>)P;");
5503   verifyFormat("int a = (int)(2.0f);");
5504   verifyFormat("int a = (int)2.0f;");
5505   verifyFormat("x[(int32)y];");
5506   verifyFormat("x = (int32)y;");
5507   verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
5508   verifyFormat("int a = (int)*b;");
5509   verifyFormat("int a = (int)2.0f;");
5510   verifyFormat("int a = (int)~0;");
5511   verifyFormat("int a = (int)++a;");
5512   verifyFormat("int a = (int)sizeof(int);");
5513   verifyFormat("int a = (int)+2;");
5514   verifyFormat("my_int a = (my_int)2.0f;");
5515   verifyFormat("my_int a = (my_int)sizeof(int);");
5516   verifyFormat("return (my_int)aaa;");
5517   verifyFormat("#define x ((int)-1)");
5518   verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
5519   verifyFormat("#define p(q) ((int *)&q)");
5520   verifyFormat("fn(a)(b) + 1;");
5521 
5522   verifyFormat("void f() { my_int a = (my_int)*b; }");
5523   verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
5524   verifyFormat("my_int a = (my_int)~0;");
5525   verifyFormat("my_int a = (my_int)++a;");
5526   verifyFormat("my_int a = (my_int)-2;");
5527   verifyFormat("my_int a = (my_int)1;");
5528   verifyFormat("my_int a = (my_int *)1;");
5529   verifyFormat("my_int a = (const my_int)-1;");
5530   verifyFormat("my_int a = (const my_int *)-1;");
5531   verifyFormat("my_int a = (my_int)(my_int)-1;");
5532 
5533   // FIXME: single value wrapped with paren will be treated as cast.
5534   verifyFormat("void f(int i = (kValue)*kMask) {}");
5535 
5536   verifyFormat("{ (void)F; }");
5537 
5538   // Don't break after a cast's
5539   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5540                "    (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
5541                "                                   bbbbbbbbbbbbbbbbbbbbbb);");
5542 
5543   // These are not casts.
5544   verifyFormat("void f(int *) {}");
5545   verifyFormat("f(foo)->b;");
5546   verifyFormat("f(foo).b;");
5547   verifyFormat("f(foo)(b);");
5548   verifyFormat("f(foo)[b];");
5549   verifyFormat("[](foo) { return 4; }(bar);");
5550   verifyFormat("(*funptr)(foo)[4];");
5551   verifyFormat("funptrs[4](foo)[4];");
5552   verifyFormat("void f(int *);");
5553   verifyFormat("void f(int *) = 0;");
5554   verifyFormat("void f(SmallVector<int>) {}");
5555   verifyFormat("void f(SmallVector<int>);");
5556   verifyFormat("void f(SmallVector<int>) = 0;");
5557   verifyFormat("void f(int i = (kA * kB) & kMask) {}");
5558   verifyFormat("int a = sizeof(int) * b;");
5559   verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
5560   verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
5561   verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
5562   verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
5563 
5564   // These are not casts, but at some point were confused with casts.
5565   verifyFormat("virtual void foo(int *) override;");
5566   verifyFormat("virtual void foo(char &) const;");
5567   verifyFormat("virtual void foo(int *a, char *) const;");
5568   verifyFormat("int a = sizeof(int *) + b;");
5569   verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
5570 
5571   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
5572                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
5573   // FIXME: The indentation here is not ideal.
5574   verifyFormat(
5575       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5576       "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
5577       "        [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
5578 }
5579 
5580 TEST_F(FormatTest, FormatsFunctionTypes) {
5581   verifyFormat("A<bool()> a;");
5582   verifyFormat("A<SomeType()> a;");
5583   verifyFormat("A<void (*)(int, std::string)> a;");
5584   verifyFormat("A<void *(int)>;");
5585   verifyFormat("void *(*a)(int *, SomeType *);");
5586   verifyFormat("int (*func)(void *);");
5587   verifyFormat("void f() { int (*func)(void *); }");
5588   verifyFormat("template <class CallbackClass>\n"
5589                "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
5590 
5591   verifyGoogleFormat("A<void*(int*, SomeType*)>;");
5592   verifyGoogleFormat("void* (*a)(int);");
5593   verifyGoogleFormat(
5594       "template <class CallbackClass>\n"
5595       "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
5596 
5597   // Other constructs can look somewhat like function types:
5598   verifyFormat("A<sizeof(*x)> a;");
5599   verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
5600   verifyFormat("some_var = function(*some_pointer_var)[0];");
5601   verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
5602 }
5603 
5604 TEST_F(FormatTest, BreaksLongVariableDeclarations) {
5605   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5606                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
5607   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
5608                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
5609 
5610   // Different ways of ()-initializiation.
5611   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5612                "    LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
5613   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5614                "    LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
5615   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5616                "    LoooooooooooooooooooooooooooooooooooooooongVariable({});");
5617 }
5618 
5619 TEST_F(FormatTest, BreaksLongDeclarations) {
5620   verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
5621                "    AnotherNameForTheLongType;");
5622   verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
5623                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5624   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5625                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
5626   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5627                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5628   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
5629                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5630   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
5631                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5632   verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
5633                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5634   FormatStyle Indented = getLLVMStyle();
5635   Indented.IndentWrappedFunctionNames = true;
5636   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5637                "    LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
5638                Indented);
5639   verifyFormat(
5640       "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5641       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
5642       Indented);
5643   verifyFormat(
5644       "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
5645       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
5646       Indented);
5647   verifyFormat(
5648       "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
5649       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
5650       Indented);
5651 
5652   // FIXME: Without the comment, this breaks after "(".
5653   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType  // break\n"
5654                "    (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
5655                getGoogleStyle());
5656 
5657   verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
5658                "                  int LoooooooooooooooooooongParam2) {}");
5659   verifyFormat(
5660       "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
5661       "                                   SourceLocation L, IdentifierIn *II,\n"
5662       "                                   Type *T) {}");
5663   verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
5664                "ReallyReallyLongFunctionName(\n"
5665                "    const std::string &SomeParameter,\n"
5666                "    const SomeType<string, SomeOtherTemplateParameter> &\n"
5667                "        ReallyReallyLongParameterName,\n"
5668                "    const SomeType<string, SomeOtherTemplateParameter> &\n"
5669                "        AnotherLongParameterName) {}");
5670   verifyFormat("template <typename A>\n"
5671                "SomeLoooooooooooooooooooooongType<\n"
5672                "    typename some_namespace::SomeOtherType<A>::Type>\n"
5673                "Function() {}");
5674 
5675   verifyGoogleFormat(
5676       "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
5677       "    aaaaaaaaaaaaaaaaaaaaaaa;");
5678   verifyGoogleFormat(
5679       "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
5680       "                                   SourceLocation L) {}");
5681   verifyGoogleFormat(
5682       "some_namespace::LongReturnType\n"
5683       "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
5684       "    int first_long_parameter, int second_parameter) {}");
5685 
5686   verifyGoogleFormat("template <typename T>\n"
5687                      "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
5688                      "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
5689   verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5690                      "                   int aaaaaaaaaaaaaaaaaaaaaaa);");
5691 
5692   verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
5693                "    const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
5694                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5695 }
5696 
5697 TEST_F(FormatTest, FormatsArrays) {
5698   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
5699                "                         [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
5700   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5701                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
5702   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5703                "    [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
5704   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5705                "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
5706                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
5707   verifyFormat(
5708       "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
5709       "             << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
5710       "                                  [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
5711 
5712   verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
5713                      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
5714   verifyFormat(
5715       "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
5716       "                                  .aaaaaaa[0]\n"
5717       "                                  .aaaaaaaaaaaaaaaaaaaaaa();");
5718 
5719   verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
5720 }
5721 
5722 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
5723   verifyFormat("(a)->b();");
5724   verifyFormat("--a;");
5725 }
5726 
5727 TEST_F(FormatTest, HandlesIncludeDirectives) {
5728   verifyFormat("#include <string>\n"
5729                "#include <a/b/c.h>\n"
5730                "#include \"a/b/string\"\n"
5731                "#include \"string.h\"\n"
5732                "#include \"string.h\"\n"
5733                "#include <a-a>\n"
5734                "#include < path with space >\n"
5735                "#include \"abc.h\" // this is included for ABC\n"
5736                "#include \"some long include\" // with a comment\n"
5737                "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"",
5738                getLLVMStyleWithColumns(35));
5739   EXPECT_EQ("#include \"a.h\"", format("#include  \"a.h\""));
5740   EXPECT_EQ("#include <a>", format("#include<a>"));
5741 
5742   verifyFormat("#import <string>");
5743   verifyFormat("#import <a/b/c.h>");
5744   verifyFormat("#import \"a/b/string\"");
5745   verifyFormat("#import \"string.h\"");
5746   verifyFormat("#import \"string.h\"");
5747   verifyFormat("#if __has_include(<strstream>)\n"
5748                "#include <strstream>\n"
5749                "#endif");
5750 
5751   verifyFormat("#define MY_IMPORT <a/b>");
5752 
5753   // Protocol buffer definition or missing "#".
5754   verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
5755                getLLVMStyleWithColumns(30));
5756 
5757   FormatStyle Style = getLLVMStyle();
5758   Style.AlwaysBreakBeforeMultilineStrings = true;
5759   Style.ColumnLimit = 0;
5760   verifyFormat("#import \"abc.h\"", Style);
5761 
5762   // But 'import' might also be a regular C++ namespace.
5763   verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5764                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5765 }
5766 
5767 //===----------------------------------------------------------------------===//
5768 // Error recovery tests.
5769 //===----------------------------------------------------------------------===//
5770 
5771 TEST_F(FormatTest, IncompleteParameterLists) {
5772   FormatStyle NoBinPacking = getLLVMStyle();
5773   NoBinPacking.BinPackParameters = false;
5774   verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
5775                "                        double *min_x,\n"
5776                "                        double *max_x,\n"
5777                "                        double *min_y,\n"
5778                "                        double *max_y,\n"
5779                "                        double *min_z,\n"
5780                "                        double *max_z, ) {}",
5781                NoBinPacking);
5782 }
5783 
5784 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
5785   verifyFormat("void f() { return; }\n42");
5786   verifyFormat("void f() {\n"
5787                "  if (0)\n"
5788                "    return;\n"
5789                "}\n"
5790                "42");
5791   verifyFormat("void f() { return }\n42");
5792   verifyFormat("void f() {\n"
5793                "  if (0)\n"
5794                "    return\n"
5795                "}\n"
5796                "42");
5797 }
5798 
5799 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
5800   EXPECT_EQ("void f() { return }", format("void  f ( )  {  return  }"));
5801   EXPECT_EQ("void f() {\n"
5802             "  if (a)\n"
5803             "    return\n"
5804             "}",
5805             format("void  f  (  )  {  if  ( a )  return  }"));
5806   EXPECT_EQ("namespace N {\n"
5807             "void f()\n"
5808             "}",
5809             format("namespace  N  {  void f()  }"));
5810   EXPECT_EQ("namespace N {\n"
5811             "void f() {}\n"
5812             "void g()\n"
5813             "}",
5814             format("namespace N  { void f( ) { } void g( ) }"));
5815 }
5816 
5817 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
5818   verifyFormat("int aaaaaaaa =\n"
5819                "    // Overlylongcomment\n"
5820                "    b;",
5821                getLLVMStyleWithColumns(20));
5822   verifyFormat("function(\n"
5823                "    ShortArgument,\n"
5824                "    LoooooooooooongArgument);\n",
5825                getLLVMStyleWithColumns(20));
5826 }
5827 
5828 TEST_F(FormatTest, IncorrectAccessSpecifier) {
5829   verifyFormat("public:");
5830   verifyFormat("class A {\n"
5831                "public\n"
5832                "  void f() {}\n"
5833                "};");
5834   verifyFormat("public\n"
5835                "int qwerty;");
5836   verifyFormat("public\n"
5837                "B {}");
5838   verifyFormat("public\n"
5839                "{}");
5840   verifyFormat("public\n"
5841                "B { int x; }");
5842 }
5843 
5844 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
5845   verifyFormat("{");
5846   verifyFormat("#})");
5847   verifyNoCrash("(/**/[:!] ?[).");
5848 }
5849 
5850 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
5851   verifyFormat("do {\n}");
5852   verifyFormat("do {\n}\n"
5853                "f();");
5854   verifyFormat("do {\n}\n"
5855                "wheeee(fun);");
5856   verifyFormat("do {\n"
5857                "  f();\n"
5858                "}");
5859 }
5860 
5861 TEST_F(FormatTest, IncorrectCodeMissingParens) {
5862   verifyFormat("if {\n  foo;\n  foo();\n}");
5863   verifyFormat("switch {\n  foo;\n  foo();\n}");
5864   verifyFormat("for {\n  foo;\n  foo();\n}");
5865   verifyFormat("while {\n  foo;\n  foo();\n}");
5866   verifyFormat("do {\n  foo;\n  foo();\n} while;");
5867 }
5868 
5869 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
5870   verifyFormat("namespace {\n"
5871                "class Foo { Foo (\n"
5872                "};\n"
5873                "} // comment");
5874 }
5875 
5876 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
5877   EXPECT_EQ("{\n  {}\n", format("{\n{\n}\n"));
5878   EXPECT_EQ("{\n  {}\n", format("{\n  {\n}\n"));
5879   EXPECT_EQ("{\n  {}\n", format("{\n  {\n  }\n"));
5880   EXPECT_EQ("{\n  {}\n}\n}\n", format("{\n  {\n    }\n  }\n}\n"));
5881 
5882   EXPECT_EQ("{\n"
5883             "  {\n"
5884             "    breakme(\n"
5885             "        qwe);\n"
5886             "  }\n",
5887             format("{\n"
5888                    "    {\n"
5889                    " breakme(qwe);\n"
5890                    "}\n",
5891                    getLLVMStyleWithColumns(10)));
5892 }
5893 
5894 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
5895   verifyFormat("int x = {\n"
5896                "    avariable,\n"
5897                "    b(alongervariable)};",
5898                getLLVMStyleWithColumns(25));
5899 }
5900 
5901 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
5902   verifyFormat("return (a)(b){1, 2, 3};");
5903 }
5904 
5905 TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
5906   verifyFormat("vector<int> x{1, 2, 3, 4};");
5907   verifyFormat("vector<int> x{\n"
5908                "    1, 2, 3, 4,\n"
5909                "};");
5910   verifyFormat("vector<T> x{{}, {}, {}, {}};");
5911   verifyFormat("f({1, 2});");
5912   verifyFormat("auto v = Foo{-1};");
5913   verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
5914   verifyFormat("Class::Class : member{1, 2, 3} {}");
5915   verifyFormat("new vector<int>{1, 2, 3};");
5916   verifyFormat("new int[3]{1, 2, 3};");
5917   verifyFormat("new int{1};");
5918   verifyFormat("return {arg1, arg2};");
5919   verifyFormat("return {arg1, SomeType{parameter}};");
5920   verifyFormat("int count = set<int>{f(), g(), h()}.size();");
5921   verifyFormat("new T{arg1, arg2};");
5922   verifyFormat("f(MyMap[{composite, key}]);");
5923   verifyFormat("class Class {\n"
5924                "  T member = {arg1, arg2};\n"
5925                "};");
5926   verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
5927   verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
5928   verifyFormat("int a = std::is_integral<int>{} + 0;");
5929 
5930   verifyFormat("int foo(int i) { return fo1{}(i); }");
5931   verifyFormat("int foo(int i) { return fo1{}(i); }");
5932   verifyFormat("auto i = decltype(x){};");
5933   verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
5934   verifyFormat("Node n{1, Node{1000}, //\n"
5935                "       2};");
5936 
5937   // In combination with BinPackParameters = false.
5938   FormatStyle NoBinPacking = getLLVMStyle();
5939   NoBinPacking.BinPackParameters = false;
5940   verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
5941                "                      bbbbb,\n"
5942                "                      ccccc,\n"
5943                "                      ddddd,\n"
5944                "                      eeeee,\n"
5945                "                      ffffff,\n"
5946                "                      ggggg,\n"
5947                "                      hhhhhh,\n"
5948                "                      iiiiii,\n"
5949                "                      jjjjjj,\n"
5950                "                      kkkkkk};",
5951                NoBinPacking);
5952   verifyFormat("const Aaaaaa aaaaa = {\n"
5953                "    aaaaa,\n"
5954                "    bbbbb,\n"
5955                "    ccccc,\n"
5956                "    ddddd,\n"
5957                "    eeeee,\n"
5958                "    ffffff,\n"
5959                "    ggggg,\n"
5960                "    hhhhhh,\n"
5961                "    iiiiii,\n"
5962                "    jjjjjj,\n"
5963                "    kkkkkk,\n"
5964                "};",
5965                NoBinPacking);
5966   verifyFormat(
5967       "const Aaaaaa aaaaa = {\n"
5968       "    aaaaa,  bbbbb,  ccccc,  ddddd,  eeeee,  ffffff, ggggg, hhhhhh,\n"
5969       "    iiiiii, jjjjjj, kkkkkk, aaaaa,  bbbbb,  ccccc,  ddddd, eeeee,\n"
5970       "    ffffff, ggggg,  hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
5971       "};",
5972       NoBinPacking);
5973 
5974   // FIXME: The alignment of these trailing comments might be bad. Then again,
5975   // this might be utterly useless in real code.
5976   verifyFormat("Constructor::Constructor()\n"
5977                "    : some_value{         //\n"
5978                "                 aaaaaaa, //\n"
5979                "                 bbbbbbb} {}");
5980 
5981   // In braced lists, the first comment is always assumed to belong to the
5982   // first element. Thus, it can be moved to the next or previous line as
5983   // appropriate.
5984   EXPECT_EQ("function({// First element:\n"
5985             "          1,\n"
5986             "          // Second element:\n"
5987             "          2});",
5988             format("function({\n"
5989                    "    // First element:\n"
5990                    "    1,\n"
5991                    "    // Second element:\n"
5992                    "    2});"));
5993   EXPECT_EQ("std::vector<int> MyNumbers{\n"
5994             "    // First element:\n"
5995             "    1,\n"
5996             "    // Second element:\n"
5997             "    2};",
5998             format("std::vector<int> MyNumbers{// First element:\n"
5999                    "                           1,\n"
6000                    "                           // Second element:\n"
6001                    "                           2};",
6002                    getLLVMStyleWithColumns(30)));
6003   // A trailing comma should still lead to an enforced line break.
6004   EXPECT_EQ("vector<int> SomeVector = {\n"
6005             "    // aaa\n"
6006             "    1, 2,\n"
6007             "};",
6008             format("vector<int> SomeVector = { // aaa\n"
6009                    "    1, 2, };"));
6010 
6011   FormatStyle ExtraSpaces = getLLVMStyle();
6012   ExtraSpaces.Cpp11BracedListStyle = false;
6013   ExtraSpaces.ColumnLimit = 75;
6014   verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
6015   verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
6016   verifyFormat("f({ 1, 2 });", ExtraSpaces);
6017   verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
6018   verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
6019   verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
6020   verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
6021   verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
6022   verifyFormat("return { arg1, arg2 };", ExtraSpaces);
6023   verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
6024   verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
6025   verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
6026   verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
6027   verifyFormat("class Class {\n"
6028                "  T member = { arg1, arg2 };\n"
6029                "};",
6030                ExtraSpaces);
6031   verifyFormat(
6032       "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6033       "                                 aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
6034       "                  : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
6035       "                                 bbbbbbbbbbbbbbbbbbbb, bbbbb };",
6036       ExtraSpaces);
6037   verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
6038   verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
6039                ExtraSpaces);
6040   verifyFormat(
6041       "someFunction(OtherParam,\n"
6042       "             BracedList{ // comment 1 (Forcing interesting break)\n"
6043       "                         param1, param2,\n"
6044       "                         // comment 2\n"
6045       "                         param3, param4 });",
6046       ExtraSpaces);
6047   verifyFormat(
6048       "std::this_thread::sleep_for(\n"
6049       "    std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
6050       ExtraSpaces);
6051   verifyFormat(
6052       "std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{\n"
6053       "    aaaaaaa, aaaaaaaaaa, aaaaa, aaaaaaaaaaaaaaa, aaa, aaaaaaaaaa, a,\n"
6054       "    aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaa,\n"
6055       "    aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa, aaaaaaa, a};");
6056   verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
6057 }
6058 
6059 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
6060   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6061                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6062                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6063                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6064                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6065                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
6066   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6067                "                 // line comment\n"
6068                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6069                "                 1, 22, 333, 4444, 55555,\n"
6070                "                 // line comment\n"
6071                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6072                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
6073   verifyFormat(
6074       "vector<int> x = {1,       22, 333, 4444, 55555, 666666, 7777777,\n"
6075       "                 1,       22, 333, 4444, 55555, 666666, 7777777,\n"
6076       "                 1,       22, 333, 4444, 55555, 666666, // comment\n"
6077       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6078       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6079       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6080       "                 7777777};");
6081   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6082                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6083                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
6084   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6085                "                 1, 1, 1, 1};",
6086                getLLVMStyleWithColumns(39));
6087   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6088                "                 1, 1, 1, 1};",
6089                getLLVMStyleWithColumns(38));
6090   verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
6091                "    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
6092                getLLVMStyleWithColumns(43));
6093 
6094   // Trailing commas.
6095   verifyFormat("vector<int> x = {\n"
6096                "    1, 1, 1, 1, 1, 1, 1, 1,\n"
6097                "};",
6098                getLLVMStyleWithColumns(39));
6099   verifyFormat("vector<int> x = {\n"
6100                "    1, 1, 1, 1, 1, 1, 1, 1, //\n"
6101                "};",
6102                getLLVMStyleWithColumns(39));
6103   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6104                "                 1, 1, 1, 1,\n"
6105                "                 /**/ /**/};",
6106                getLLVMStyleWithColumns(39));
6107   verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
6108                "        {aaaaaaaaaaaaaaaaaaa},\n"
6109                "        {aaaaaaaaaaaaaaaaaaaaa},\n"
6110                "        {aaaaaaaaaaaaaaaaa}};",
6111                getLLVMStyleWithColumns(60));
6112 
6113   // With nested lists, we should either format one item per line or all nested
6114   // lists one one line.
6115   // FIXME: For some nested lists, we can do better.
6116   verifyFormat(
6117       "SomeStruct my_struct_array = {\n"
6118       "    {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
6119       "     aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
6120       "    {aaa, aaa},\n"
6121       "    {aaa, aaa},\n"
6122       "    {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
6123       "    {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6124       "     aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
6125 
6126   // No column layout should be used here.
6127   verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
6128                "                   bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
6129 
6130   verifyNoCrash("a<,");
6131 }
6132 
6133 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
6134   FormatStyle DoNotMerge = getLLVMStyle();
6135   DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
6136 
6137   verifyFormat("void f() { return 42; }");
6138   verifyFormat("void f() {\n"
6139                "  return 42;\n"
6140                "}",
6141                DoNotMerge);
6142   verifyFormat("void f() {\n"
6143                "  // Comment\n"
6144                "}");
6145   verifyFormat("{\n"
6146                "#error {\n"
6147                "  int a;\n"
6148                "}");
6149   verifyFormat("{\n"
6150                "  int a;\n"
6151                "#error {\n"
6152                "}");
6153   verifyFormat("void f() {} // comment");
6154   verifyFormat("void f() { int a; } // comment");
6155   verifyFormat("void f() {\n"
6156                "} // comment",
6157                DoNotMerge);
6158   verifyFormat("void f() {\n"
6159                "  int a;\n"
6160                "} // comment",
6161                DoNotMerge);
6162   verifyFormat("void f() {\n"
6163                "} // comment",
6164                getLLVMStyleWithColumns(15));
6165 
6166   verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
6167   verifyFormat("void f() {\n  return 42;\n}", getLLVMStyleWithColumns(22));
6168 
6169   verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
6170   verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
6171   verifyFormat("class C {\n"
6172                "  C()\n"
6173                "      : iiiiiiii(nullptr),\n"
6174                "        kkkkkkk(nullptr),\n"
6175                "        mmmmmmm(nullptr),\n"
6176                "        nnnnnnn(nullptr) {}\n"
6177                "};",
6178                getGoogleStyle());
6179 
6180   FormatStyle NoColumnLimit = getLLVMStyle();
6181   NoColumnLimit.ColumnLimit = 0;
6182   EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
6183   EXPECT_EQ("class C {\n"
6184             "  A() : b(0) {}\n"
6185             "};", format("class C{A():b(0){}};", NoColumnLimit));
6186   EXPECT_EQ("A()\n"
6187             "    : b(0) {\n"
6188             "}",
6189             format("A()\n:b(0)\n{\n}", NoColumnLimit));
6190 
6191   FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
6192   DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
6193       FormatStyle::SFS_None;
6194   EXPECT_EQ("A()\n"
6195             "    : b(0) {\n"
6196             "}",
6197             format("A():b(0){}", DoNotMergeNoColumnLimit));
6198   EXPECT_EQ("A()\n"
6199             "    : b(0) {\n"
6200             "}",
6201             format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
6202 
6203   verifyFormat("#define A          \\\n"
6204                "  void f() {       \\\n"
6205                "    int i;         \\\n"
6206                "  }",
6207                getLLVMStyleWithColumns(20));
6208   verifyFormat("#define A           \\\n"
6209                "  void f() { int i; }",
6210                getLLVMStyleWithColumns(21));
6211   verifyFormat("#define A            \\\n"
6212                "  void f() {         \\\n"
6213                "    int i;           \\\n"
6214                "  }                  \\\n"
6215                "  int j;",
6216                getLLVMStyleWithColumns(22));
6217   verifyFormat("#define A             \\\n"
6218                "  void f() { int i; } \\\n"
6219                "  int j;",
6220                getLLVMStyleWithColumns(23));
6221 }
6222 
6223 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
6224   FormatStyle MergeInlineOnly = getLLVMStyle();
6225   MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
6226   verifyFormat("class C {\n"
6227                "  int f() { return 42; }\n"
6228                "};",
6229                MergeInlineOnly);
6230   verifyFormat("int f() {\n"
6231                "  return 42;\n"
6232                "}",
6233                MergeInlineOnly);
6234 }
6235 
6236 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
6237   // Elaborate type variable declarations.
6238   verifyFormat("struct foo a = {bar};\nint n;");
6239   verifyFormat("class foo a = {bar};\nint n;");
6240   verifyFormat("union foo a = {bar};\nint n;");
6241 
6242   // Elaborate types inside function definitions.
6243   verifyFormat("struct foo f() {}\nint n;");
6244   verifyFormat("class foo f() {}\nint n;");
6245   verifyFormat("union foo f() {}\nint n;");
6246 
6247   // Templates.
6248   verifyFormat("template <class X> void f() {}\nint n;");
6249   verifyFormat("template <struct X> void f() {}\nint n;");
6250   verifyFormat("template <union X> void f() {}\nint n;");
6251 
6252   // Actual definitions...
6253   verifyFormat("struct {\n} n;");
6254   verifyFormat(
6255       "template <template <class T, class Y>, class Z> class X {\n} n;");
6256   verifyFormat("union Z {\n  int n;\n} x;");
6257   verifyFormat("class MACRO Z {\n} n;");
6258   verifyFormat("class MACRO(X) Z {\n} n;");
6259   verifyFormat("class __attribute__(X) Z {\n} n;");
6260   verifyFormat("class __declspec(X) Z {\n} n;");
6261   verifyFormat("class A##B##C {\n} n;");
6262   verifyFormat("class alignas(16) Z {\n} n;");
6263 
6264   // Redefinition from nested context:
6265   verifyFormat("class A::B::C {\n} n;");
6266 
6267   // Template definitions.
6268   verifyFormat(
6269       "template <typename F>\n"
6270       "Matcher(const Matcher<F> &Other,\n"
6271       "        typename enable_if_c<is_base_of<F, T>::value &&\n"
6272       "                             !is_same<F, T>::value>::type * = 0)\n"
6273       "    : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
6274 
6275   // FIXME: This is still incorrectly handled at the formatter side.
6276   verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
6277 
6278   // FIXME:
6279   // This now gets parsed incorrectly as class definition.
6280   // verifyFormat("class A<int> f() {\n}\nint n;");
6281 
6282   // Elaborate types where incorrectly parsing the structural element would
6283   // break the indent.
6284   verifyFormat("if (true)\n"
6285                "  class X x;\n"
6286                "else\n"
6287                "  f();\n");
6288 
6289   // This is simply incomplete. Formatting is not important, but must not crash.
6290   verifyFormat("class A:");
6291 }
6292 
6293 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
6294   EXPECT_EQ("#error Leave     all         white!!!!! space* alone!\n",
6295             format("#error Leave     all         white!!!!! space* alone!\n"));
6296   EXPECT_EQ(
6297       "#warning Leave     all         white!!!!! space* alone!\n",
6298       format("#warning Leave     all         white!!!!! space* alone!\n"));
6299   EXPECT_EQ("#error 1", format("  #  error   1"));
6300   EXPECT_EQ("#warning 1", format("  #  warning 1"));
6301 }
6302 
6303 TEST_F(FormatTest, FormatHashIfExpressions) {
6304   verifyFormat("#if AAAA && BBBB");
6305   // FIXME: Come up with a better indentation for #elif.
6306   verifyFormat(
6307       "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) &&  \\\n"
6308       "    defined(BBBBBBBB)\n"
6309       "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) &&  \\\n"
6310       "    defined(BBBBBBBB)\n"
6311       "#endif",
6312       getLLVMStyleWithColumns(65));
6313 }
6314 
6315 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
6316   FormatStyle AllowsMergedIf = getGoogleStyle();
6317   AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
6318   verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
6319   verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
6320   verifyFormat("if (true)\n#error E\n  return 42;", AllowsMergedIf);
6321   EXPECT_EQ("if (true) return 42;",
6322             format("if (true)\nreturn 42;", AllowsMergedIf));
6323   FormatStyle ShortMergedIf = AllowsMergedIf;
6324   ShortMergedIf.ColumnLimit = 25;
6325   verifyFormat("#define A \\\n"
6326                "  if (true) return 42;",
6327                ShortMergedIf);
6328   verifyFormat("#define A \\\n"
6329                "  f();    \\\n"
6330                "  if (true)\n"
6331                "#define B",
6332                ShortMergedIf);
6333   verifyFormat("#define A \\\n"
6334                "  f();    \\\n"
6335                "  if (true)\n"
6336                "g();",
6337                ShortMergedIf);
6338   verifyFormat("{\n"
6339                "#ifdef A\n"
6340                "  // Comment\n"
6341                "  if (true) continue;\n"
6342                "#endif\n"
6343                "  // Comment\n"
6344                "  if (true) continue;\n"
6345                "}",
6346                ShortMergedIf);
6347   ShortMergedIf.ColumnLimit = 29;
6348   verifyFormat("#define A                   \\\n"
6349                "  if (aaaaaaaaaa) return 1; \\\n"
6350                "  return 2;",
6351                ShortMergedIf);
6352   ShortMergedIf.ColumnLimit = 28;
6353   verifyFormat("#define A         \\\n"
6354                "  if (aaaaaaaaaa) \\\n"
6355                "    return 1;     \\\n"
6356                "  return 2;",
6357                ShortMergedIf);
6358 }
6359 
6360 TEST_F(FormatTest, BlockCommentsInControlLoops) {
6361   verifyFormat("if (0) /* a comment in a strange place */ {\n"
6362                "  f();\n"
6363                "}");
6364   verifyFormat("if (0) /* a comment in a strange place */ {\n"
6365                "  f();\n"
6366                "} /* another comment */ else /* comment #3 */ {\n"
6367                "  g();\n"
6368                "}");
6369   verifyFormat("while (0) /* a comment in a strange place */ {\n"
6370                "  f();\n"
6371                "}");
6372   verifyFormat("for (;;) /* a comment in a strange place */ {\n"
6373                "  f();\n"
6374                "}");
6375   verifyFormat("do /* a comment in a strange place */ {\n"
6376                "  f();\n"
6377                "} /* another comment */ while (0);");
6378 }
6379 
6380 TEST_F(FormatTest, BlockComments) {
6381   EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */",
6382             format("/* *//* */  /* */\n/* *//* */  /* */"));
6383   EXPECT_EQ("/* */ a /* */ b;", format("  /* */  a/* */  b;"));
6384   EXPECT_EQ("#define A /*123*/ \\\n"
6385             "  b\n"
6386             "/* */\n"
6387             "someCall(\n"
6388             "    parameter);",
6389             format("#define A /*123*/ b\n"
6390                    "/* */\n"
6391                    "someCall(parameter);",
6392                    getLLVMStyleWithColumns(15)));
6393 
6394   EXPECT_EQ("#define A\n"
6395             "/* */ someCall(\n"
6396             "    parameter);",
6397             format("#define A\n"
6398                    "/* */someCall(parameter);",
6399                    getLLVMStyleWithColumns(15)));
6400   EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/"));
6401   EXPECT_EQ("/*\n"
6402             "*\n"
6403             " * aaaaaa\n"
6404             "*aaaaaa\n"
6405             "*/",
6406             format("/*\n"
6407                    "*\n"
6408                    " * aaaaaa aaaaaa\n"
6409                    "*/",
6410                    getLLVMStyleWithColumns(10)));
6411   EXPECT_EQ("/*\n"
6412             "**\n"
6413             "* aaaaaa\n"
6414             "*aaaaaa\n"
6415             "*/",
6416             format("/*\n"
6417                    "**\n"
6418                    "* aaaaaa aaaaaa\n"
6419                    "*/",
6420                    getLLVMStyleWithColumns(10)));
6421 
6422   FormatStyle NoBinPacking = getLLVMStyle();
6423   NoBinPacking.BinPackParameters = false;
6424   EXPECT_EQ("someFunction(1, /* comment 1 */\n"
6425             "             2, /* comment 2 */\n"
6426             "             3, /* comment 3 */\n"
6427             "             aaaa,\n"
6428             "             bbbb);",
6429             format("someFunction (1,   /* comment 1 */\n"
6430                    "                2, /* comment 2 */  \n"
6431                    "               3,   /* comment 3 */\n"
6432                    "aaaa, bbbb );",
6433                    NoBinPacking));
6434   verifyFormat(
6435       "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
6436       "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
6437   EXPECT_EQ(
6438       "bool aaaaaaaaaaaaa = /* trailing comment */\n"
6439       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
6440       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;",
6441       format(
6442           "bool       aaaaaaaaaaaaa =       /* trailing comment */\n"
6443           "    aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa    ||\n"
6444           "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa   || aaaaaaaaaaaaaaaaaaaaaaaaaa;"));
6445   EXPECT_EQ(
6446       "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
6447       "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;   /* comment */\n"
6448       "int cccccccccccccccccccccccccccccc;       /* comment */\n",
6449       format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
6450              "int      bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
6451              "int    cccccccccccccccccccccccccccccc;  /* comment */\n"));
6452 
6453   verifyFormat("void f(int * /* unused */) {}");
6454 
6455   EXPECT_EQ("/*\n"
6456             " **\n"
6457             " */",
6458             format("/*\n"
6459                    " **\n"
6460                    " */"));
6461   EXPECT_EQ("/*\n"
6462             " *q\n"
6463             " */",
6464             format("/*\n"
6465                    " *q\n"
6466                    " */"));
6467   EXPECT_EQ("/*\n"
6468             " * q\n"
6469             " */",
6470             format("/*\n"
6471                    " * q\n"
6472                    " */"));
6473   EXPECT_EQ("/*\n"
6474             " **/",
6475             format("/*\n"
6476                    " **/"));
6477   EXPECT_EQ("/*\n"
6478             " ***/",
6479             format("/*\n"
6480                    " ***/"));
6481 }
6482 
6483 TEST_F(FormatTest, BlockCommentsInMacros) {
6484   EXPECT_EQ("#define A          \\\n"
6485             "  {                \\\n"
6486             "    /* one line */ \\\n"
6487             "    someCall();",
6488             format("#define A {        \\\n"
6489                    "  /* one line */   \\\n"
6490                    "  someCall();",
6491                    getLLVMStyleWithColumns(20)));
6492   EXPECT_EQ("#define A          \\\n"
6493             "  {                \\\n"
6494             "    /* previous */ \\\n"
6495             "    /* one line */ \\\n"
6496             "    someCall();",
6497             format("#define A {        \\\n"
6498                    "  /* previous */   \\\n"
6499                    "  /* one line */   \\\n"
6500                    "  someCall();",
6501                    getLLVMStyleWithColumns(20)));
6502 }
6503 
6504 TEST_F(FormatTest, BlockCommentsAtEndOfLine) {
6505   EXPECT_EQ("a = {\n"
6506             "    1111 /*    */\n"
6507             "};",
6508             format("a = {1111 /*    */\n"
6509                    "};",
6510                    getLLVMStyleWithColumns(15)));
6511   EXPECT_EQ("a = {\n"
6512             "    1111 /*      */\n"
6513             "};",
6514             format("a = {1111 /*      */\n"
6515                    "};",
6516                    getLLVMStyleWithColumns(15)));
6517 
6518   // FIXME: The formatting is still wrong here.
6519   EXPECT_EQ("a = {\n"
6520             "    1111 /*      a\n"
6521             "            */\n"
6522             "};",
6523             format("a = {1111 /*      a */\n"
6524                    "};",
6525                    getLLVMStyleWithColumns(15)));
6526 }
6527 
6528 TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) {
6529   // FIXME: This is not what we want...
6530   verifyFormat("{\n"
6531                "// a"
6532                "// b");
6533 }
6534 
6535 TEST_F(FormatTest, FormatStarDependingOnContext) {
6536   verifyFormat("void f(int *a);");
6537   verifyFormat("void f() { f(fint * b); }");
6538   verifyFormat("class A {\n  void f(int *a);\n};");
6539   verifyFormat("class A {\n  int *a;\n};");
6540   verifyFormat("namespace a {\n"
6541                "namespace b {\n"
6542                "class A {\n"
6543                "  void f() {}\n"
6544                "  int *a;\n"
6545                "};\n"
6546                "}\n"
6547                "}");
6548 }
6549 
6550 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
6551   verifyFormat("while");
6552   verifyFormat("operator");
6553 }
6554 
6555 //===----------------------------------------------------------------------===//
6556 // Objective-C tests.
6557 //===----------------------------------------------------------------------===//
6558 
6559 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
6560   verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
6561   EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
6562             format("-(NSUInteger)indexOfObject:(id)anObject;"));
6563   EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
6564   EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
6565   EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
6566             format("-(NSInteger)Method3:(id)anObject;"));
6567   EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
6568             format("-(NSInteger)Method4:(id)anObject;"));
6569   EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
6570             format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
6571   EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
6572             format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
6573   EXPECT_EQ(
6574       "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;",
6575       format(
6576           "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;"));
6577 
6578   // Very long objectiveC method declaration.
6579   verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
6580                "                    inRange:(NSRange)range\n"
6581                "                   outRange:(NSRange)out_range\n"
6582                "                  outRange1:(NSRange)out_range1\n"
6583                "                  outRange2:(NSRange)out_range2\n"
6584                "                  outRange3:(NSRange)out_range3\n"
6585                "                  outRange4:(NSRange)out_range4\n"
6586                "                  outRange5:(NSRange)out_range5\n"
6587                "                  outRange6:(NSRange)out_range6\n"
6588                "                  outRange7:(NSRange)out_range7\n"
6589                "                  outRange8:(NSRange)out_range8\n"
6590                "                  outRange9:(NSRange)out_range9;");
6591 
6592   verifyFormat("- (int)sum:(vector<int>)numbers;");
6593   verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
6594   // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
6595   // protocol lists (but not for template classes):
6596   //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
6597 
6598   verifyFormat("- (int (*)())foo:(int (*)())f;");
6599   verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
6600 
6601   // If there's no return type (very rare in practice!), LLVM and Google style
6602   // agree.
6603   verifyFormat("- foo;");
6604   verifyFormat("- foo:(int)f;");
6605   verifyGoogleFormat("- foo:(int)foo;");
6606 }
6607 
6608 TEST_F(FormatTest, FormatObjCInterface) {
6609   verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
6610                "@public\n"
6611                "  int field1;\n"
6612                "@protected\n"
6613                "  int field2;\n"
6614                "@private\n"
6615                "  int field3;\n"
6616                "@package\n"
6617                "  int field4;\n"
6618                "}\n"
6619                "+ (id)init;\n"
6620                "@end");
6621 
6622   verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
6623                      " @public\n"
6624                      "  int field1;\n"
6625                      " @protected\n"
6626                      "  int field2;\n"
6627                      " @private\n"
6628                      "  int field3;\n"
6629                      " @package\n"
6630                      "  int field4;\n"
6631                      "}\n"
6632                      "+ (id)init;\n"
6633                      "@end");
6634 
6635   verifyFormat("@interface /* wait for it */ Foo\n"
6636                "+ (id)init;\n"
6637                "// Look, a comment!\n"
6638                "- (int)answerWith:(int)i;\n"
6639                "@end");
6640 
6641   verifyFormat("@interface Foo\n"
6642                "@end\n"
6643                "@interface Bar\n"
6644                "@end");
6645 
6646   verifyFormat("@interface Foo : Bar\n"
6647                "+ (id)init;\n"
6648                "@end");
6649 
6650   verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n"
6651                "+ (id)init;\n"
6652                "@end");
6653 
6654   verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
6655                      "+ (id)init;\n"
6656                      "@end");
6657 
6658   verifyFormat("@interface Foo (HackStuff)\n"
6659                "+ (id)init;\n"
6660                "@end");
6661 
6662   verifyFormat("@interface Foo ()\n"
6663                "+ (id)init;\n"
6664                "@end");
6665 
6666   verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
6667                "+ (id)init;\n"
6668                "@end");
6669 
6670   verifyGoogleFormat("@interface Foo (HackStuff) <MyProtocol>\n"
6671                      "+ (id)init;\n"
6672                      "@end");
6673 
6674   verifyFormat("@interface Foo {\n"
6675                "  int _i;\n"
6676                "}\n"
6677                "+ (id)init;\n"
6678                "@end");
6679 
6680   verifyFormat("@interface Foo : Bar {\n"
6681                "  int _i;\n"
6682                "}\n"
6683                "+ (id)init;\n"
6684                "@end");
6685 
6686   verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
6687                "  int _i;\n"
6688                "}\n"
6689                "+ (id)init;\n"
6690                "@end");
6691 
6692   verifyFormat("@interface Foo (HackStuff) {\n"
6693                "  int _i;\n"
6694                "}\n"
6695                "+ (id)init;\n"
6696                "@end");
6697 
6698   verifyFormat("@interface Foo () {\n"
6699                "  int _i;\n"
6700                "}\n"
6701                "+ (id)init;\n"
6702                "@end");
6703 
6704   verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
6705                "  int _i;\n"
6706                "}\n"
6707                "+ (id)init;\n"
6708                "@end");
6709 
6710   FormatStyle OnePerLine = getGoogleStyle();
6711   OnePerLine.BinPackParameters = false;
6712   verifyFormat("@interface aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa () <\n"
6713                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6714                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6715                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6716                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
6717                "}",
6718                OnePerLine);
6719 }
6720 
6721 TEST_F(FormatTest, FormatObjCImplementation) {
6722   verifyFormat("@implementation Foo : NSObject {\n"
6723                "@public\n"
6724                "  int field1;\n"
6725                "@protected\n"
6726                "  int field2;\n"
6727                "@private\n"
6728                "  int field3;\n"
6729                "@package\n"
6730                "  int field4;\n"
6731                "}\n"
6732                "+ (id)init {\n}\n"
6733                "@end");
6734 
6735   verifyGoogleFormat("@implementation Foo : NSObject {\n"
6736                      " @public\n"
6737                      "  int field1;\n"
6738                      " @protected\n"
6739                      "  int field2;\n"
6740                      " @private\n"
6741                      "  int field3;\n"
6742                      " @package\n"
6743                      "  int field4;\n"
6744                      "}\n"
6745                      "+ (id)init {\n}\n"
6746                      "@end");
6747 
6748   verifyFormat("@implementation Foo\n"
6749                "+ (id)init {\n"
6750                "  if (true)\n"
6751                "    return nil;\n"
6752                "}\n"
6753                "// Look, a comment!\n"
6754                "- (int)answerWith:(int)i {\n"
6755                "  return i;\n"
6756                "}\n"
6757                "+ (int)answerWith:(int)i {\n"
6758                "  return i;\n"
6759                "}\n"
6760                "@end");
6761 
6762   verifyFormat("@implementation Foo\n"
6763                "@end\n"
6764                "@implementation Bar\n"
6765                "@end");
6766 
6767   EXPECT_EQ("@implementation Foo : Bar\n"
6768             "+ (id)init {\n}\n"
6769             "- (void)foo {\n}\n"
6770             "@end",
6771             format("@implementation Foo : Bar\n"
6772                    "+(id)init{}\n"
6773                    "-(void)foo{}\n"
6774                    "@end"));
6775 
6776   verifyFormat("@implementation Foo {\n"
6777                "  int _i;\n"
6778                "}\n"
6779                "+ (id)init {\n}\n"
6780                "@end");
6781 
6782   verifyFormat("@implementation Foo : Bar {\n"
6783                "  int _i;\n"
6784                "}\n"
6785                "+ (id)init {\n}\n"
6786                "@end");
6787 
6788   verifyFormat("@implementation Foo (HackStuff)\n"
6789                "+ (id)init {\n}\n"
6790                "@end");
6791   verifyFormat("@implementation ObjcClass\n"
6792                "- (void)method;\n"
6793                "{}\n"
6794                "@end");
6795 }
6796 
6797 TEST_F(FormatTest, FormatObjCProtocol) {
6798   verifyFormat("@protocol Foo\n"
6799                "@property(weak) id delegate;\n"
6800                "- (NSUInteger)numberOfThings;\n"
6801                "@end");
6802 
6803   verifyFormat("@protocol MyProtocol <NSObject>\n"
6804                "- (NSUInteger)numberOfThings;\n"
6805                "@end");
6806 
6807   verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
6808                      "- (NSUInteger)numberOfThings;\n"
6809                      "@end");
6810 
6811   verifyFormat("@protocol Foo;\n"
6812                "@protocol Bar;\n");
6813 
6814   verifyFormat("@protocol Foo\n"
6815                "@end\n"
6816                "@protocol Bar\n"
6817                "@end");
6818 
6819   verifyFormat("@protocol myProtocol\n"
6820                "- (void)mandatoryWithInt:(int)i;\n"
6821                "@optional\n"
6822                "- (void)optional;\n"
6823                "@required\n"
6824                "- (void)required;\n"
6825                "@optional\n"
6826                "@property(assign) int madProp;\n"
6827                "@end\n");
6828 
6829   verifyFormat("@property(nonatomic, assign, readonly)\n"
6830                "    int *looooooooooooooooooooooooooooongNumber;\n"
6831                "@property(nonatomic, assign, readonly)\n"
6832                "    NSString *looooooooooooooooooooooooooooongName;");
6833 
6834   verifyFormat("@implementation PR18406\n"
6835                "}\n"
6836                "@end");
6837 }
6838 
6839 TEST_F(FormatTest, FormatObjCMethodDeclarations) {
6840   verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n"
6841                "                   rect:(NSRect)theRect\n"
6842                "               interval:(float)theInterval {\n"
6843                "}");
6844   verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
6845                "          longKeyword:(NSRect)theRect\n"
6846                "    evenLongerKeyword:(float)theInterval\n"
6847                "                error:(NSError **)theError {\n"
6848                "}");
6849   verifyFormat("- (instancetype)initXxxxxx:(id<x>)x\n"
6850                "                         y:(id<yyyyyyyyyyyyyyyyyyyy>)y\n"
6851                "    NS_DESIGNATED_INITIALIZER;",
6852                getLLVMStyleWithColumns(60));
6853 }
6854 
6855 TEST_F(FormatTest, FormatObjCMethodExpr) {
6856   verifyFormat("[foo bar:baz];");
6857   verifyFormat("return [foo bar:baz];");
6858   verifyFormat("return (a)[foo bar:baz];");
6859   verifyFormat("f([foo bar:baz]);");
6860   verifyFormat("f(2, [foo bar:baz]);");
6861   verifyFormat("f(2, a ? b : c);");
6862   verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
6863 
6864   // Unary operators.
6865   verifyFormat("int a = +[foo bar:baz];");
6866   verifyFormat("int a = -[foo bar:baz];");
6867   verifyFormat("int a = ![foo bar:baz];");
6868   verifyFormat("int a = ~[foo bar:baz];");
6869   verifyFormat("int a = ++[foo bar:baz];");
6870   verifyFormat("int a = --[foo bar:baz];");
6871   verifyFormat("int a = sizeof [foo bar:baz];");
6872   verifyFormat("int a = alignof [foo bar:baz];", getGoogleStyle());
6873   verifyFormat("int a = &[foo bar:baz];");
6874   verifyFormat("int a = *[foo bar:baz];");
6875   // FIXME: Make casts work, without breaking f()[4].
6876   //verifyFormat("int a = (int)[foo bar:baz];");
6877   //verifyFormat("return (int)[foo bar:baz];");
6878   //verifyFormat("(void)[foo bar:baz];");
6879   verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];");
6880 
6881   // Binary operators.
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   verifyFormat("[foo bar:baz] -= [foo bar:baz];");
6889   verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
6890   verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
6891   verifyFormat("[foo bar:baz] &= [foo bar:baz];");
6892   verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
6893   verifyFormat("[foo bar:baz] |= [foo bar:baz];");
6894   verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
6895   verifyFormat("[foo bar:baz] || [foo bar:baz];");
6896   verifyFormat("[foo bar:baz] && [foo bar:baz];");
6897   verifyFormat("[foo bar:baz] | [foo bar:baz];");
6898   verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
6899   verifyFormat("[foo bar:baz] & [foo bar:baz];");
6900   verifyFormat("[foo bar:baz] == [foo bar:baz];");
6901   verifyFormat("[foo bar:baz] != [foo bar:baz];");
6902   verifyFormat("[foo bar:baz] >= [foo bar:baz];");
6903   verifyFormat("[foo bar:baz] <= [foo bar:baz];");
6904   verifyFormat("[foo bar:baz] > [foo bar:baz];");
6905   verifyFormat("[foo bar:baz] < [foo bar:baz];");
6906   verifyFormat("[foo bar:baz] >> [foo bar:baz];");
6907   verifyFormat("[foo bar:baz] << [foo bar:baz];");
6908   verifyFormat("[foo bar:baz] - [foo bar:baz];");
6909   verifyFormat("[foo bar:baz] + [foo bar:baz];");
6910   verifyFormat("[foo bar:baz] * [foo bar:baz];");
6911   verifyFormat("[foo bar:baz] / [foo bar:baz];");
6912   verifyFormat("[foo bar:baz] % [foo bar:baz];");
6913   // Whew!
6914 
6915   verifyFormat("return in[42];");
6916   verifyFormat("for (auto v : in[1]) {\n}");
6917   verifyFormat("for (id foo in [self getStuffFor:bla]) {\n"
6918                "}");
6919   verifyFormat("[self aaaaa:MACRO(a, b:, c:)];");
6920 
6921   verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
6922   verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
6923   verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
6924   verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
6925   verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
6926   verifyFormat("[button setAction:@selector(zoomOut:)];");
6927   verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
6928 
6929   verifyFormat("arr[[self indexForFoo:a]];");
6930   verifyFormat("throw [self errorFor:a];");
6931   verifyFormat("@throw [self errorFor:a];");
6932 
6933   verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];");
6934   verifyFormat("[(id)foo bar:(id) ? baz : quux];");
6935   verifyFormat("4 > 4 ? (id)a : (id)baz;");
6936 
6937   // This tests that the formatter doesn't break after "backing" but before ":",
6938   // which would be at 80 columns.
6939   verifyFormat(
6940       "void f() {\n"
6941       "  if ((self = [super initWithContentRect:contentRect\n"
6942       "                               styleMask:styleMask ?: otherMask\n"
6943       "                                 backing:NSBackingStoreBuffered\n"
6944       "                                   defer:YES]))");
6945 
6946   verifyFormat(
6947       "[foo checkThatBreakingAfterColonWorksOk:\n"
6948       "         [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
6949 
6950   verifyFormat("[myObj short:arg1 // Force line break\n"
6951                "          longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n"
6952                "    evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n"
6953                "                error:arg4];");
6954   verifyFormat(
6955       "void f() {\n"
6956       "  popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
6957       "      initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
6958       "                                     pos.width(), pos.height())\n"
6959       "                styleMask:NSBorderlessWindowMask\n"
6960       "                  backing:NSBackingStoreBuffered\n"
6961       "                    defer:NO]);\n"
6962       "}");
6963   verifyFormat(
6964       "void f() {\n"
6965       "  popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n"
6966       "      iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n"
6967       "                                 pos.width(), pos.height())\n"
6968       "                syeMask:NSBorderlessWindowMask\n"
6969       "                  bking:NSBackingStoreBuffered\n"
6970       "                    der:NO]);\n"
6971       "}",
6972       getLLVMStyleWithColumns(70));
6973   verifyFormat(
6974       "void f() {\n"
6975       "  popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
6976       "      initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
6977       "                                     pos.width(), pos.height())\n"
6978       "                styleMask:NSBorderlessWindowMask\n"
6979       "                  backing:NSBackingStoreBuffered\n"
6980       "                    defer:NO]);\n"
6981       "}",
6982       getChromiumStyle(FormatStyle::LK_Cpp));
6983   verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n"
6984                "                             with:contentsNativeView];");
6985 
6986   verifyFormat(
6987       "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n"
6988       "           owner:nillllll];");
6989 
6990   verifyFormat(
6991       "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n"
6992       "        forType:kBookmarkButtonDragType];");
6993 
6994   verifyFormat("[defaultCenter addObserver:self\n"
6995                "                  selector:@selector(willEnterFullscreen)\n"
6996                "                      name:kWillEnterFullscreenNotification\n"
6997                "                    object:nil];");
6998   verifyFormat("[image_rep drawInRect:drawRect\n"
6999                "             fromRect:NSZeroRect\n"
7000                "            operation:NSCompositeCopy\n"
7001                "             fraction:1.0\n"
7002                "       respectFlipped:NO\n"
7003                "                hints:nil];");
7004 
7005   verifyFormat(
7006       "scoped_nsobject<NSTextField> message(\n"
7007       "    // The frame will be fixed up when |-setMessageText:| is called.\n"
7008       "    [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);");
7009   verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n"
7010                "    aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n"
7011                "         aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n"
7012                "          aaaa:bbb];");
7013   verifyFormat("[self param:function( //\n"
7014                "                parameter)]");
7015   verifyFormat(
7016       "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
7017       "                 aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
7018       "                 aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];");
7019 
7020   // Variadic parameters.
7021   verifyFormat(
7022       "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];");
7023   verifyFormat(
7024       "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
7025       "                    aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
7026       "                    aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];");
7027   verifyFormat("[self // break\n"
7028                "      a:a\n"
7029                "    aaa:aaa];");
7030   verifyFormat("bool a = ([aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaa ||\n"
7031                "          [aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaaaaa);");
7032 }
7033 
7034 TEST_F(FormatTest, ObjCAt) {
7035   verifyFormat("@autoreleasepool");
7036   verifyFormat("@catch");
7037   verifyFormat("@class");
7038   verifyFormat("@compatibility_alias");
7039   verifyFormat("@defs");
7040   verifyFormat("@dynamic");
7041   verifyFormat("@encode");
7042   verifyFormat("@end");
7043   verifyFormat("@finally");
7044   verifyFormat("@implementation");
7045   verifyFormat("@import");
7046   verifyFormat("@interface");
7047   verifyFormat("@optional");
7048   verifyFormat("@package");
7049   verifyFormat("@private");
7050   verifyFormat("@property");
7051   verifyFormat("@protected");
7052   verifyFormat("@protocol");
7053   verifyFormat("@public");
7054   verifyFormat("@required");
7055   verifyFormat("@selector");
7056   verifyFormat("@synchronized");
7057   verifyFormat("@synthesize");
7058   verifyFormat("@throw");
7059   verifyFormat("@try");
7060 
7061   EXPECT_EQ("@interface", format("@ interface"));
7062 
7063   // The precise formatting of this doesn't matter, nobody writes code like
7064   // this.
7065   verifyFormat("@ /*foo*/ interface");
7066 }
7067 
7068 TEST_F(FormatTest, ObjCSnippets) {
7069   verifyFormat("@autoreleasepool {\n"
7070                "  foo();\n"
7071                "}");
7072   verifyFormat("@class Foo, Bar;");
7073   verifyFormat("@compatibility_alias AliasName ExistingClass;");
7074   verifyFormat("@dynamic textColor;");
7075   verifyFormat("char *buf1 = @encode(int *);");
7076   verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
7077   verifyFormat("char *buf1 = @encode(int **);");
7078   verifyFormat("Protocol *proto = @protocol(p1);");
7079   verifyFormat("SEL s = @selector(foo:);");
7080   verifyFormat("@synchronized(self) {\n"
7081                "  f();\n"
7082                "}");
7083 
7084   verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
7085   verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
7086 
7087   verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
7088   verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
7089   verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
7090   verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
7091                getMozillaStyle());
7092   verifyFormat("@property BOOL editable;", getMozillaStyle());
7093   verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
7094                getWebKitStyle());
7095   verifyFormat("@property BOOL editable;", getWebKitStyle());
7096 
7097   verifyFormat("@import foo.bar;\n"
7098                "@import baz;");
7099 }
7100 
7101 TEST_F(FormatTest, ObjCLiterals) {
7102   verifyFormat("@\"String\"");
7103   verifyFormat("@1");
7104   verifyFormat("@+4.8");
7105   verifyFormat("@-4");
7106   verifyFormat("@1LL");
7107   verifyFormat("@.5");
7108   verifyFormat("@'c'");
7109   verifyFormat("@true");
7110 
7111   verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
7112   verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);");
7113   verifyFormat("NSNumber *favoriteColor = @(Green);");
7114   verifyFormat("NSString *path = @(getenv(\"PATH\"));");
7115 
7116   verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];");
7117 }
7118 
7119 TEST_F(FormatTest, ObjCDictLiterals) {
7120   verifyFormat("@{");
7121   verifyFormat("@{}");
7122   verifyFormat("@{@\"one\" : @1}");
7123   verifyFormat("return @{@\"one\" : @1;");
7124   verifyFormat("@{@\"one\" : @1}");
7125 
7126   verifyFormat("@{@\"one\" : @{@2 : @1}}");
7127   verifyFormat("@{\n"
7128                "  @\"one\" : @{@2 : @1},\n"
7129                "}");
7130 
7131   verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}");
7132   verifyFormat("[self setDict:@{}");
7133   verifyFormat("[self setDict:@{@1 : @2}");
7134   verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);");
7135   verifyFormat(
7136       "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};");
7137   verifyFormat(
7138       "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};");
7139 
7140   verifyFormat(
7141       "NSDictionary *d = @{\n"
7142       "  @\"nam\" : NSUserNam(),\n"
7143       "  @\"dte\" : [NSDate date],\n"
7144       "  @\"processInfo\" : [NSProcessInfo processInfo]\n"
7145       "};");
7146   verifyFormat(
7147       "@{\n"
7148       "  NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
7149       "regularFont,\n"
7150       "};");
7151   verifyGoogleFormat(
7152       "@{\n"
7153       "  NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
7154       "regularFont,\n"
7155       "};");
7156   verifyFormat(
7157       "@{\n"
7158       "  NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n"
7159       "      reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n"
7160       "};");
7161 
7162   // We should try to be robust in case someone forgets the "@".
7163   verifyFormat(
7164       "NSDictionary *d = {\n"
7165       "  @\"nam\" : NSUserNam(),\n"
7166       "  @\"dte\" : [NSDate date],\n"
7167       "  @\"processInfo\" : [NSProcessInfo processInfo]\n"
7168       "};");
7169   verifyFormat("NSMutableDictionary *dictionary =\n"
7170                "    [NSMutableDictionary dictionaryWithDictionary:@{\n"
7171                "      aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n"
7172                "      bbbbbbbbbbbbbbbbbb : bbbbb,\n"
7173                "      cccccccccccccccc : ccccccccccccccc\n"
7174                "    }];");
7175 }
7176 
7177 TEST_F(FormatTest, ObjCArrayLiterals) {
7178   verifyFormat("@[");
7179   verifyFormat("@[]");
7180   verifyFormat(
7181       "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];");
7182   verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];");
7183   verifyFormat("NSArray *array = @[ [foo description] ];");
7184 
7185   verifyFormat(
7186       "NSArray *some_variable = @[\n"
7187       "  aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
7188       "  @\"aaaaaaaaaaaaaaaaa\",\n"
7189       "  @\"aaaaaaaaaaaaaaaaa\",\n"
7190       "  @\"aaaaaaaaaaaaaaaaa\"\n"
7191       "];");
7192   verifyFormat("NSArray *some_variable = @[\n"
7193                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7194                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7195                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7196                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7197                "];");
7198   verifyGoogleFormat("NSArray *some_variable = @[\n"
7199                      "  @\"aaaaaaaaaaaaaaaaa\",\n"
7200                      "  @\"aaaaaaaaaaaaaaaaa\",\n"
7201                      "  @\"aaaaaaaaaaaaaaaaa\",\n"
7202                      "  @\"aaaaaaaaaaaaaaaaa\"\n"
7203                      "];");
7204 
7205   // We should try to be robust in case someone forgets the "@".
7206   verifyFormat("NSArray *some_variable = [\n"
7207                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7208                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7209                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7210                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7211                "];");
7212   verifyFormat(
7213       "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n"
7214       "                                             index:(NSUInteger)index\n"
7215       "                                nonDigitAttributes:\n"
7216       "                                    (NSDictionary *)noDigitAttributes;");
7217   verifyFormat(
7218       "[someFunction someLooooooooooooongParameter:\n"
7219       "                  @[ NSBundle.mainBundle.infoDictionary[@\"a\"] ]];");
7220 }
7221 
7222 TEST_F(FormatTest, ReformatRegionAdjustsIndent) {
7223   EXPECT_EQ("{\n"
7224             "{\n"
7225             "a;\n"
7226             "b;\n"
7227             "}\n"
7228             "}",
7229             format("{\n"
7230                    "{\n"
7231                    "a;\n"
7232                    "     b;\n"
7233                    "}\n"
7234                    "}",
7235                    13, 2, getLLVMStyle()));
7236   EXPECT_EQ("{\n"
7237             "{\n"
7238             "  a;\n"
7239             "b;\n"
7240             "}\n"
7241             "}",
7242             format("{\n"
7243                    "{\n"
7244                    "     a;\n"
7245                    "b;\n"
7246                    "}\n"
7247                    "}",
7248                    9, 2, getLLVMStyle()));
7249   EXPECT_EQ("{\n"
7250             "{\n"
7251             "public:\n"
7252             "  b;\n"
7253             "}\n"
7254             "}",
7255             format("{\n"
7256                    "{\n"
7257                    "public:\n"
7258                    "     b;\n"
7259                    "}\n"
7260                    "}",
7261                    17, 2, getLLVMStyle()));
7262   EXPECT_EQ("{\n"
7263             "{\n"
7264             "a;\n"
7265             "}\n"
7266             "{\n"
7267             "  b; //\n"
7268             "}\n"
7269             "}",
7270             format("{\n"
7271                    "{\n"
7272                    "a;\n"
7273                    "}\n"
7274                    "{\n"
7275                    "           b; //\n"
7276                    "}\n"
7277                    "}",
7278                    22, 2, getLLVMStyle()));
7279   EXPECT_EQ("  {\n"
7280             "    a; //\n"
7281             "  }",
7282             format("  {\n"
7283                    "a; //\n"
7284                    "  }",
7285                    4, 2, getLLVMStyle()));
7286   EXPECT_EQ("void f() {}\n"
7287             "void g() {}",
7288             format("void f() {}\n"
7289                    "void g() {}",
7290                    13, 0, getLLVMStyle()));
7291   EXPECT_EQ("int a; // comment\n"
7292             "       // line 2\n"
7293             "int b;",
7294             format("int a; // comment\n"
7295                    "       // line 2\n"
7296                    "  int b;",
7297                    35, 0, getLLVMStyle()));
7298   EXPECT_EQ("  int a;\n"
7299             "  void\n"
7300             "  ffffff() {\n"
7301             "  }",
7302             format("  int a;\n"
7303                    "void ffffff() {}",
7304                    11, 0, getLLVMStyleWithColumns(11)));
7305 
7306   EXPECT_EQ(" void f() {\n"
7307             "#define A 1\n"
7308             " }",
7309             format(" void f() {\n"
7310                    "     #define A 1\n" // Format this line.
7311                    " }",
7312                    20, 0, getLLVMStyle()));
7313   EXPECT_EQ(" void f() {\n"
7314             "    int i;\n"
7315             "#define A \\\n"
7316             "    int i;  \\\n"
7317             "   int j;\n"
7318             "    int k;\n"
7319             " }",
7320             format(" void f() {\n"
7321                    "    int i;\n"
7322                    "#define A \\\n"
7323                    "    int i;  \\\n"
7324                    "   int j;\n"
7325                    "      int k;\n" // Format this line.
7326                    " }",
7327                    67, 0, getLLVMStyle()));
7328 }
7329 
7330 TEST_F(FormatTest, BreaksStringLiterals) {
7331   EXPECT_EQ("\"some text \"\n"
7332             "\"other\";",
7333             format("\"some text other\";", getLLVMStyleWithColumns(12)));
7334   EXPECT_EQ("\"some text \"\n"
7335             "\"other\";",
7336             format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
7337   EXPECT_EQ(
7338       "#define A  \\\n"
7339       "  \"some \"  \\\n"
7340       "  \"text \"  \\\n"
7341       "  \"other\";",
7342       format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
7343   EXPECT_EQ(
7344       "#define A  \\\n"
7345       "  \"so \"    \\\n"
7346       "  \"text \"  \\\n"
7347       "  \"other\";",
7348       format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
7349 
7350   EXPECT_EQ("\"some text\"",
7351             format("\"some text\"", getLLVMStyleWithColumns(1)));
7352   EXPECT_EQ("\"some text\"",
7353             format("\"some text\"", getLLVMStyleWithColumns(11)));
7354   EXPECT_EQ("\"some \"\n"
7355             "\"text\"",
7356             format("\"some text\"", getLLVMStyleWithColumns(10)));
7357   EXPECT_EQ("\"some \"\n"
7358             "\"text\"",
7359             format("\"some text\"", getLLVMStyleWithColumns(7)));
7360   EXPECT_EQ("\"some\"\n"
7361             "\" tex\"\n"
7362             "\"t\"",
7363             format("\"some text\"", getLLVMStyleWithColumns(6)));
7364   EXPECT_EQ("\"some\"\n"
7365             "\" tex\"\n"
7366             "\" and\"",
7367             format("\"some tex and\"", getLLVMStyleWithColumns(6)));
7368   EXPECT_EQ("\"some\"\n"
7369             "\"/tex\"\n"
7370             "\"/and\"",
7371             format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
7372 
7373   EXPECT_EQ("variable =\n"
7374             "    \"long string \"\n"
7375             "    \"literal\";",
7376             format("variable = \"long string literal\";",
7377                    getLLVMStyleWithColumns(20)));
7378 
7379   EXPECT_EQ("variable = f(\n"
7380             "    \"long string \"\n"
7381             "    \"literal\",\n"
7382             "    short,\n"
7383             "    loooooooooooooooooooong);",
7384             format("variable = f(\"long string literal\", short, "
7385                    "loooooooooooooooooooong);",
7386                    getLLVMStyleWithColumns(20)));
7387 
7388   EXPECT_EQ("f(g(\"long string \"\n"
7389             "    \"literal\"),\n"
7390             "  b);",
7391             format("f(g(\"long string literal\"), b);",
7392                    getLLVMStyleWithColumns(20)));
7393   EXPECT_EQ("f(g(\"long string \"\n"
7394             "    \"literal\",\n"
7395             "    a),\n"
7396             "  b);",
7397             format("f(g(\"long string literal\", a), b);",
7398                    getLLVMStyleWithColumns(20)));
7399   EXPECT_EQ(
7400       "f(\"one two\".split(\n"
7401       "    variable));",
7402       format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
7403   EXPECT_EQ("f(\"one two three four five six \"\n"
7404             "  \"seven\".split(\n"
7405             "      really_looooong_variable));",
7406             format("f(\"one two three four five six seven\"."
7407                    "split(really_looooong_variable));",
7408                    getLLVMStyleWithColumns(33)));
7409 
7410   EXPECT_EQ("f(\"some \"\n"
7411             "  \"text\",\n"
7412             "  other);",
7413             format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
7414 
7415   // Only break as a last resort.
7416   verifyFormat(
7417       "aaaaaaaaaaaaaaaaaaaa(\n"
7418       "    aaaaaaaaaaaaaaaaaaaa,\n"
7419       "    aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
7420 
7421   EXPECT_EQ(
7422       "\"splitmea\"\n"
7423       "\"trandomp\"\n"
7424       "\"oint\"",
7425       format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
7426 
7427   EXPECT_EQ(
7428       "\"split/\"\n"
7429       "\"pathat/\"\n"
7430       "\"slashes\"",
7431       format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
7432 
7433   EXPECT_EQ(
7434       "\"split/\"\n"
7435       "\"pathat/\"\n"
7436       "\"slashes\"",
7437       format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
7438   EXPECT_EQ("\"split at \"\n"
7439             "\"spaces/at/\"\n"
7440             "\"slashes.at.any$\"\n"
7441             "\"non-alphanumeric%\"\n"
7442             "\"1111111111characte\"\n"
7443             "\"rs\"",
7444             format("\"split at "
7445                    "spaces/at/"
7446                    "slashes.at."
7447                    "any$non-"
7448                    "alphanumeric%"
7449                    "1111111111characte"
7450                    "rs\"",
7451                    getLLVMStyleWithColumns(20)));
7452 
7453   // Verify that splitting the strings understands
7454   // Style::AlwaysBreakBeforeMultilineStrings.
7455   EXPECT_EQ("aaaaaaaaaaaa(aaaaaaaaaaaaa,\n"
7456             "             \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
7457             "             \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
7458             format("aaaaaaaaaaaa(aaaaaaaaaaaaa, \"aaaaaaaaaaaaaaaaaaaaaa "
7459                    "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa "
7460                    "aaaaaaaaaaaaaaaaaaaaaa\");",
7461                    getGoogleStyle()));
7462   EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7463             "       \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
7464             format("return \"aaaaaaaaaaaaaaaaaaaaaa "
7465                    "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
7466                    "aaaaaaaaaaaaaaaaaaaaaa\";",
7467                    getGoogleStyle()));
7468   EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7469             "                \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
7470             format("llvm::outs() << "
7471                    "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
7472                    "aaaaaaaaaaaaaaaaaaa\";"));
7473   EXPECT_EQ("ffff(\n"
7474             "    {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7475             "     \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
7476             format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
7477                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
7478                    getGoogleStyle()));
7479 
7480   FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
7481   AlignLeft.AlignEscapedNewlinesLeft = true;
7482   EXPECT_EQ(
7483       "#define A \\\n"
7484       "  \"some \" \\\n"
7485       "  \"text \" \\\n"
7486       "  \"other\";",
7487       format("#define A \"some text other\";", AlignLeft));
7488 }
7489 
7490 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
7491   EXPECT_EQ(
7492       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
7493       "(\n"
7494       "    \"x\t\");",
7495       format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
7496              "aaaaaaa("
7497              "\"x\t\");"));
7498 }
7499 
7500 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
7501   EXPECT_EQ(
7502       "u8\"utf8 string \"\n"
7503       "u8\"literal\";",
7504       format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
7505   EXPECT_EQ(
7506       "u\"utf16 string \"\n"
7507       "u\"literal\";",
7508       format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
7509   EXPECT_EQ(
7510       "U\"utf32 string \"\n"
7511       "U\"literal\";",
7512       format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
7513   EXPECT_EQ("L\"wide string \"\n"
7514             "L\"literal\";",
7515             format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
7516   EXPECT_EQ("@\"NSString \"\n"
7517             "@\"literal\";",
7518             format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
7519 
7520   // This input makes clang-format try to split the incomplete unicode escape
7521   // sequence, which used to lead to a crasher.
7522   verifyNoCrash(
7523       "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
7524       getLLVMStyleWithColumns(60));
7525 }
7526 
7527 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
7528   FormatStyle Style = getGoogleStyleWithColumns(15);
7529   EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
7530   EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
7531   EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
7532   EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
7533   EXPECT_EQ("u8R\"x(raw literal)x\";",
7534             format("u8R\"x(raw literal)x\";", Style));
7535 }
7536 
7537 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
7538   FormatStyle Style = getLLVMStyleWithColumns(20);
7539   EXPECT_EQ(
7540       "_T(\"aaaaaaaaaaaaaa\")\n"
7541       "_T(\"aaaaaaaaaaaaaa\")\n"
7542       "_T(\"aaaaaaaaaaaa\")",
7543       format("  _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
7544   EXPECT_EQ("f(x, _T(\"aaaaaaaaa\")\n"
7545             "     _T(\"aaaaaa\"),\n"
7546             "  z);",
7547             format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
7548 
7549   // FIXME: Handle embedded spaces in one iteration.
7550   //  EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
7551   //            "_T(\"aaaaaaaaaaaaa\")\n"
7552   //            "_T(\"aaaaaaaaaaaaa\")\n"
7553   //            "_T(\"a\")",
7554   //            format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
7555   //                   getLLVMStyleWithColumns(20)));
7556   EXPECT_EQ(
7557       "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
7558       format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
7559 }
7560 
7561 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
7562   EXPECT_EQ(
7563       "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7564       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7565       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
7566       format("aaaaaaaaaaa  =  \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7567              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7568              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
7569 }
7570 
7571 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
7572   EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
7573             format("f(g(R\"x(raw literal)x\",   a), b);", getGoogleStyle()));
7574   EXPECT_EQ("fffffffffff(g(R\"x(\n"
7575             "multiline raw string literal xxxxxxxxxxxxxx\n"
7576             ")x\",\n"
7577             "              a),\n"
7578             "            b);",
7579             format("fffffffffff(g(R\"x(\n"
7580                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7581                    ")x\", a), b);",
7582                    getGoogleStyleWithColumns(20)));
7583   EXPECT_EQ("fffffffffff(\n"
7584             "    g(R\"x(qqq\n"
7585             "multiline raw string literal xxxxxxxxxxxxxx\n"
7586             ")x\",\n"
7587             "      a),\n"
7588             "    b);",
7589             format("fffffffffff(g(R\"x(qqq\n"
7590                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7591                    ")x\", a), b);",
7592                    getGoogleStyleWithColumns(20)));
7593 
7594   EXPECT_EQ("fffffffffff(R\"x(\n"
7595             "multiline raw string literal xxxxxxxxxxxxxx\n"
7596             ")x\");",
7597             format("fffffffffff(R\"x(\n"
7598                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7599                    ")x\");",
7600                    getGoogleStyleWithColumns(20)));
7601   EXPECT_EQ("fffffffffff(R\"x(\n"
7602             "multiline raw string literal xxxxxxxxxxxxxx\n"
7603             ")x\" + bbbbbb);",
7604             format("fffffffffff(R\"x(\n"
7605                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7606                    ")x\" +   bbbbbb);",
7607                    getGoogleStyleWithColumns(20)));
7608   EXPECT_EQ("fffffffffff(\n"
7609             "    R\"x(\n"
7610             "multiline raw string literal xxxxxxxxxxxxxx\n"
7611             ")x\" +\n"
7612             "    bbbbbb);",
7613             format("fffffffffff(\n"
7614                    " R\"x(\n"
7615                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7616                    ")x\" + bbbbbb);",
7617                    getGoogleStyleWithColumns(20)));
7618 }
7619 
7620 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
7621   verifyFormat("string a = \"unterminated;");
7622   EXPECT_EQ("function(\"unterminated,\n"
7623             "         OtherParameter);",
7624             format("function(  \"unterminated,\n"
7625                    "    OtherParameter);"));
7626 }
7627 
7628 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
7629   FormatStyle Style = getLLVMStyle();
7630   Style.Standard = FormatStyle::LS_Cpp03;
7631   EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
7632             format("#define x(_a) printf(\"foo\"_a);", Style));
7633 }
7634 
7635 TEST_F(FormatTest, UnderstandsCpp1y) {
7636   verifyFormat("int bi{1'000'000};");
7637 }
7638 
7639 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
7640   EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
7641             "             \"ddeeefff\");",
7642             format("someFunction(\"aaabbbcccdddeeefff\");",
7643                    getLLVMStyleWithColumns(25)));
7644   EXPECT_EQ("someFunction1234567890(\n"
7645             "    \"aaabbbcccdddeeefff\");",
7646             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
7647                    getLLVMStyleWithColumns(26)));
7648   EXPECT_EQ("someFunction1234567890(\n"
7649             "    \"aaabbbcccdddeeeff\"\n"
7650             "    \"f\");",
7651             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
7652                    getLLVMStyleWithColumns(25)));
7653   EXPECT_EQ("someFunction1234567890(\n"
7654             "    \"aaabbbcccdddeeeff\"\n"
7655             "    \"f\");",
7656             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
7657                    getLLVMStyleWithColumns(24)));
7658   EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
7659             "             \"ddde \"\n"
7660             "             \"efff\");",
7661             format("someFunction(\"aaabbbcc ddde efff\");",
7662                    getLLVMStyleWithColumns(25)));
7663   EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
7664             "             \"ddeeefff\");",
7665             format("someFunction(\"aaabbbccc ddeeefff\");",
7666                    getLLVMStyleWithColumns(25)));
7667   EXPECT_EQ("someFunction1234567890(\n"
7668             "    \"aaabb \"\n"
7669             "    \"cccdddeeefff\");",
7670             format("someFunction1234567890(\"aaabb cccdddeeefff\");",
7671                    getLLVMStyleWithColumns(25)));
7672   EXPECT_EQ("#define A          \\\n"
7673             "  string s =       \\\n"
7674             "      \"123456789\"  \\\n"
7675             "      \"0\";         \\\n"
7676             "  int i;",
7677             format("#define A string s = \"1234567890\"; int i;",
7678                    getLLVMStyleWithColumns(20)));
7679   // FIXME: Put additional penalties on breaking at non-whitespace locations.
7680   EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
7681             "             \"dddeeeff\"\n"
7682             "             \"f\");",
7683             format("someFunction(\"aaabbbcc dddeeefff\");",
7684                    getLLVMStyleWithColumns(25)));
7685 }
7686 
7687 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
7688   EXPECT_EQ("\"\\a\"",
7689             format("\"\\a\"", getLLVMStyleWithColumns(3)));
7690   EXPECT_EQ("\"\\\"",
7691             format("\"\\\"", getLLVMStyleWithColumns(2)));
7692   EXPECT_EQ("\"test\"\n"
7693             "\"\\n\"",
7694             format("\"test\\n\"", getLLVMStyleWithColumns(7)));
7695   EXPECT_EQ("\"tes\\\\\"\n"
7696             "\"n\"",
7697             format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
7698   EXPECT_EQ("\"\\\\\\\\\"\n"
7699             "\"\\n\"",
7700             format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
7701   EXPECT_EQ("\"\\uff01\"",
7702             format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
7703   EXPECT_EQ("\"\\uff01\"\n"
7704             "\"test\"",
7705             format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
7706   EXPECT_EQ("\"\\Uff01ff02\"",
7707             format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
7708   EXPECT_EQ("\"\\x000000000001\"\n"
7709             "\"next\"",
7710             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
7711   EXPECT_EQ("\"\\x000000000001next\"",
7712             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
7713   EXPECT_EQ("\"\\x000000000001\"",
7714             format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
7715   EXPECT_EQ("\"test\"\n"
7716             "\"\\000000\"\n"
7717             "\"000001\"",
7718             format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
7719   EXPECT_EQ("\"test\\000\"\n"
7720             "\"00000000\"\n"
7721             "\"1\"",
7722             format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
7723 }
7724 
7725 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
7726   verifyFormat("void f() {\n"
7727                "  return g() {}\n"
7728                "  void h() {}");
7729   verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
7730                "g();\n"
7731                "}");
7732 }
7733 
7734 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
7735   verifyFormat(
7736       "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
7737 }
7738 
7739 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
7740   verifyFormat("class X {\n"
7741                "  void f() {\n"
7742                "  }\n"
7743                "};",
7744                getLLVMStyleWithColumns(12));
7745 }
7746 
7747 TEST_F(FormatTest, ConfigurableIndentWidth) {
7748   FormatStyle EightIndent = getLLVMStyleWithColumns(18);
7749   EightIndent.IndentWidth = 8;
7750   EightIndent.ContinuationIndentWidth = 8;
7751   verifyFormat("void f() {\n"
7752                "        someFunction();\n"
7753                "        if (true) {\n"
7754                "                f();\n"
7755                "        }\n"
7756                "}",
7757                EightIndent);
7758   verifyFormat("class X {\n"
7759                "        void f() {\n"
7760                "        }\n"
7761                "};",
7762                EightIndent);
7763   verifyFormat("int x[] = {\n"
7764                "        call(),\n"
7765                "        call()};",
7766                EightIndent);
7767 }
7768 
7769 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
7770   verifyFormat("double\n"
7771                "f();",
7772                getLLVMStyleWithColumns(8));
7773 }
7774 
7775 TEST_F(FormatTest, ConfigurableUseOfTab) {
7776   FormatStyle Tab = getLLVMStyleWithColumns(42);
7777   Tab.IndentWidth = 8;
7778   Tab.UseTab = FormatStyle::UT_Always;
7779   Tab.AlignEscapedNewlinesLeft = true;
7780 
7781   EXPECT_EQ("if (aaaaaaaa && // q\n"
7782             "    bb)\t\t// w\n"
7783             "\t;",
7784             format("if (aaaaaaaa &&// q\n"
7785                    "bb)// w\n"
7786                    ";",
7787                    Tab));
7788   EXPECT_EQ("if (aaa && bbb) // w\n"
7789             "\t;",
7790             format("if(aaa&&bbb)// w\n"
7791                    ";",
7792                    Tab));
7793 
7794   verifyFormat("class X {\n"
7795                "\tvoid f() {\n"
7796                "\t\tsomeFunction(parameter1,\n"
7797                "\t\t\t     parameter2);\n"
7798                "\t}\n"
7799                "};",
7800                Tab);
7801   verifyFormat("#define A                        \\\n"
7802                "\tvoid f() {               \\\n"
7803                "\t\tsomeFunction(    \\\n"
7804                "\t\t    parameter1,  \\\n"
7805                "\t\t    parameter2); \\\n"
7806                "\t}",
7807                Tab);
7808   EXPECT_EQ("void f() {\n"
7809             "\tf();\n"
7810             "\tg();\n"
7811             "}",
7812             format("void f() {\n"
7813                    "\tf();\n"
7814                    "\tg();\n"
7815                    "}",
7816                    0, 0, Tab));
7817   EXPECT_EQ("void f() {\n"
7818             "\tf();\n"
7819             "\tg();\n"
7820             "}",
7821             format("void f() {\n"
7822                    "\tf();\n"
7823                    "\tg();\n"
7824                    "}",
7825                    16, 0, Tab));
7826   EXPECT_EQ("void f() {\n"
7827             "  \tf();\n"
7828             "\tg();\n"
7829             "}",
7830             format("void f() {\n"
7831                    "  \tf();\n"
7832                    "  \tg();\n"
7833                    "}",
7834                    21, 0, Tab));
7835 
7836   Tab.TabWidth = 4;
7837   Tab.IndentWidth = 8;
7838   verifyFormat("class TabWidth4Indent8 {\n"
7839                "\t\tvoid f() {\n"
7840                "\t\t\t\tsomeFunction(parameter1,\n"
7841                "\t\t\t\t\t\t\t parameter2);\n"
7842                "\t\t}\n"
7843                "};",
7844                Tab);
7845 
7846   Tab.TabWidth = 4;
7847   Tab.IndentWidth = 4;
7848   verifyFormat("class TabWidth4Indent4 {\n"
7849                "\tvoid f() {\n"
7850                "\t\tsomeFunction(parameter1,\n"
7851                "\t\t\t\t\t parameter2);\n"
7852                "\t}\n"
7853                "};",
7854                Tab);
7855 
7856   Tab.TabWidth = 8;
7857   Tab.IndentWidth = 4;
7858   verifyFormat("class TabWidth8Indent4 {\n"
7859                "    void f() {\n"
7860                "\tsomeFunction(parameter1,\n"
7861                "\t\t     parameter2);\n"
7862                "    }\n"
7863                "};",
7864                Tab);
7865 
7866   Tab.TabWidth = 8;
7867   Tab.IndentWidth = 8;
7868   EXPECT_EQ("/*\n"
7869             "\t      a\t\tcomment\n"
7870             "\t      in multiple lines\n"
7871             "       */",
7872             format("   /*\t \t \n"
7873                    " \t \t a\t\tcomment\t \t\n"
7874                    " \t \t in multiple lines\t\n"
7875                    " \t  */",
7876                    Tab));
7877 
7878   Tab.UseTab = FormatStyle::UT_ForIndentation;
7879   verifyFormat("{\n"
7880                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7881                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7882                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7883                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7884                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7885                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7886                "};",
7887                Tab);
7888   verifyFormat("enum A {\n"
7889                "\ta1, // Force multiple lines\n"
7890                "\ta2,\n"
7891                "\ta3\n"
7892                "};",
7893                Tab);
7894   EXPECT_EQ("if (aaaaaaaa && // q\n"
7895             "    bb)         // w\n"
7896             "\t;",
7897             format("if (aaaaaaaa &&// q\n"
7898                    "bb)// w\n"
7899                    ";",
7900                    Tab));
7901   verifyFormat("class X {\n"
7902                "\tvoid f() {\n"
7903                "\t\tsomeFunction(parameter1,\n"
7904                "\t\t             parameter2);\n"
7905                "\t}\n"
7906                "};",
7907                Tab);
7908   verifyFormat("{\n"
7909                "\tQ({\n"
7910                "\t\tint a;\n"
7911                "\t\tsomeFunction(aaaaaaaa,\n"
7912                "\t\t             bbbbbbb);\n"
7913                "\t}, p);\n"
7914                "}",
7915                Tab);
7916   EXPECT_EQ("{\n"
7917             "\t/* aaaa\n"
7918             "\t   bbbb */\n"
7919             "}",
7920             format("{\n"
7921                    "/* aaaa\n"
7922                    "   bbbb */\n"
7923                    "}",
7924                    Tab));
7925   EXPECT_EQ("{\n"
7926             "\t/*\n"
7927             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7928             "\t  bbbbbbbbbbbbb\n"
7929             "\t*/\n"
7930             "}",
7931             format("{\n"
7932                    "/*\n"
7933                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
7934                    "*/\n"
7935                    "}",
7936                    Tab));
7937   EXPECT_EQ("{\n"
7938             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7939             "\t// bbbbbbbbbbbbb\n"
7940             "}",
7941             format("{\n"
7942                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
7943                    "}",
7944                    Tab));
7945   EXPECT_EQ("{\n"
7946             "\t/*\n"
7947             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7948             "\t  bbbbbbbbbbbbb\n"
7949             "\t*/\n"
7950             "}",
7951             format("{\n"
7952                    "\t/*\n"
7953                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
7954                    "\t*/\n"
7955                    "}",
7956                    Tab));
7957   EXPECT_EQ("{\n"
7958             "\t/*\n"
7959             "\n"
7960             "\t*/\n"
7961             "}",
7962             format("{\n"
7963                    "\t/*\n"
7964                    "\n"
7965                    "\t*/\n"
7966                    "}",
7967                    Tab));
7968   EXPECT_EQ("{\n"
7969             "\t/*\n"
7970             " asdf\n"
7971             "\t*/\n"
7972             "}",
7973             format("{\n"
7974                    "\t/*\n"
7975                    " asdf\n"
7976                    "\t*/\n"
7977                    "}",
7978                    Tab));
7979 
7980   Tab.UseTab = FormatStyle::UT_Never;
7981   EXPECT_EQ("/*\n"
7982             "              a\t\tcomment\n"
7983             "              in multiple lines\n"
7984             "       */",
7985             format("   /*\t \t \n"
7986                    " \t \t a\t\tcomment\t \t\n"
7987                    " \t \t in multiple lines\t\n"
7988                    " \t  */",
7989                    Tab));
7990   EXPECT_EQ("/* some\n"
7991             "   comment */",
7992            format(" \t \t /* some\n"
7993                   " \t \t    comment */",
7994                   Tab));
7995   EXPECT_EQ("int a; /* some\n"
7996             "   comment */",
7997            format(" \t \t int a; /* some\n"
7998                   " \t \t    comment */",
7999                   Tab));
8000 
8001   EXPECT_EQ("int a; /* some\n"
8002             "comment */",
8003            format(" \t \t int\ta; /* some\n"
8004                   " \t \t    comment */",
8005                   Tab));
8006   EXPECT_EQ("f(\"\t\t\"); /* some\n"
8007             "    comment */",
8008            format(" \t \t f(\"\t\t\"); /* some\n"
8009                   " \t \t    comment */",
8010                   Tab));
8011   EXPECT_EQ("{\n"
8012             "  /*\n"
8013             "   * Comment\n"
8014             "   */\n"
8015             "  int i;\n"
8016             "}",
8017             format("{\n"
8018                    "\t/*\n"
8019                    "\t * Comment\n"
8020                    "\t */\n"
8021                    "\t int i;\n"
8022                    "}"));
8023 }
8024 
8025 TEST_F(FormatTest, CalculatesOriginalColumn) {
8026   EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8027             "q\"; /* some\n"
8028             "       comment */",
8029             format("  \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8030                    "q\"; /* some\n"
8031                    "       comment */",
8032                    getLLVMStyle()));
8033   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
8034             "/* some\n"
8035             "   comment */",
8036             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
8037                    " /* some\n"
8038                    "    comment */",
8039                    getLLVMStyle()));
8040   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8041             "qqq\n"
8042             "/* some\n"
8043             "   comment */",
8044             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8045                    "qqq\n"
8046                    " /* some\n"
8047                    "    comment */",
8048                    getLLVMStyle()));
8049   EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8050             "wwww; /* some\n"
8051             "         comment */",
8052             format("  inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8053                    "wwww; /* some\n"
8054                    "         comment */",
8055                    getLLVMStyle()));
8056 }
8057 
8058 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
8059   FormatStyle NoSpace = getLLVMStyle();
8060   NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
8061 
8062   verifyFormat("while(true)\n"
8063                "  continue;", NoSpace);
8064   verifyFormat("for(;;)\n"
8065                "  continue;", NoSpace);
8066   verifyFormat("if(true)\n"
8067                "  f();\n"
8068                "else if(true)\n"
8069                "  f();", NoSpace);
8070   verifyFormat("do {\n"
8071                "  do_something();\n"
8072                "} while(something());", NoSpace);
8073   verifyFormat("switch(x) {\n"
8074                "default:\n"
8075                "  break;\n"
8076                "}", NoSpace);
8077   verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
8078   verifyFormat("size_t x = sizeof(x);", NoSpace);
8079   verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
8080   verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
8081   verifyFormat("alignas(128) char a[128];", NoSpace);
8082   verifyFormat("size_t x = alignof(MyType);", NoSpace);
8083   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
8084   verifyFormat("int f() throw(Deprecated);", NoSpace);
8085 
8086   FormatStyle Space = getLLVMStyle();
8087   Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
8088 
8089   verifyFormat("int f ();", Space);
8090   verifyFormat("void f (int a, T b) {\n"
8091                "  while (true)\n"
8092                "    continue;\n"
8093                "}",
8094                Space);
8095   verifyFormat("if (true)\n"
8096                "  f ();\n"
8097                "else if (true)\n"
8098                "  f ();",
8099                Space);
8100   verifyFormat("do {\n"
8101                "  do_something ();\n"
8102                "} while (something ());",
8103                Space);
8104   verifyFormat("switch (x) {\n"
8105                "default:\n"
8106                "  break;\n"
8107                "}",
8108                Space);
8109   verifyFormat("A::A () : a (1) {}", Space);
8110   verifyFormat("void f () __attribute__ ((asdf));", Space);
8111   verifyFormat("*(&a + 1);\n"
8112                "&((&a)[1]);\n"
8113                "a[(b + c) * d];\n"
8114                "(((a + 1) * 2) + 3) * 4;",
8115                Space);
8116   verifyFormat("#define A(x) x", Space);
8117   verifyFormat("#define A (x) x", Space);
8118   verifyFormat("#if defined(x)\n"
8119                "#endif",
8120                Space);
8121   verifyFormat("auto i = std::make_unique<int> (5);", Space);
8122   verifyFormat("size_t x = sizeof (x);", Space);
8123   verifyFormat("auto f (int x) -> decltype (x);", Space);
8124   verifyFormat("int f (T x) noexcept (x.create ());", Space);
8125   verifyFormat("alignas (128) char a[128];", Space);
8126   verifyFormat("size_t x = alignof (MyType);", Space);
8127   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
8128   verifyFormat("int f () throw (Deprecated);", Space);
8129 }
8130 
8131 TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
8132   FormatStyle Spaces = getLLVMStyle();
8133 
8134   Spaces.SpacesInParentheses = true;
8135   verifyFormat("call( x, y, z );", Spaces);
8136   verifyFormat("while ( (bool)1 )\n"
8137                "  continue;", Spaces);
8138   verifyFormat("for ( ;; )\n"
8139                "  continue;", Spaces);
8140   verifyFormat("if ( true )\n"
8141                "  f();\n"
8142                "else if ( true )\n"
8143                "  f();", Spaces);
8144   verifyFormat("do {\n"
8145                "  do_something( (int)i );\n"
8146                "} while ( something() );", Spaces);
8147   verifyFormat("switch ( x ) {\n"
8148                "default:\n"
8149                "  break;\n"
8150                "}", Spaces);
8151 
8152   Spaces.SpacesInParentheses = false;
8153   Spaces.SpacesInCStyleCastParentheses = true;
8154   verifyFormat("Type *A = ( Type * )P;", Spaces);
8155   verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
8156   verifyFormat("x = ( int32 )y;", Spaces);
8157   verifyFormat("int a = ( int )(2.0f);", Spaces);
8158   verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
8159   verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
8160   verifyFormat("#define x (( int )-1)", Spaces);
8161 
8162   Spaces.SpacesInParentheses = false;
8163   Spaces.SpaceInEmptyParentheses = true;
8164   verifyFormat("call(x, y, z);", Spaces);
8165   verifyFormat("call( )", Spaces);
8166 
8167   // Run the first set of tests again with
8168   // Spaces.SpacesInParentheses = false,
8169   // Spaces.SpaceInEmptyParentheses = true and
8170   // Spaces.SpacesInCStyleCastParentheses = true
8171   Spaces.SpacesInParentheses = false,
8172   Spaces.SpaceInEmptyParentheses = true;
8173   Spaces.SpacesInCStyleCastParentheses = true;
8174   verifyFormat("call(x, y, z);", Spaces);
8175   verifyFormat("while (( bool )1)\n"
8176                "  continue;", Spaces);
8177   verifyFormat("for (;;)\n"
8178                "  continue;", Spaces);
8179   verifyFormat("if (true)\n"
8180                "  f( );\n"
8181                "else if (true)\n"
8182                "  f( );", Spaces);
8183   verifyFormat("do {\n"
8184                "  do_something(( int )i);\n"
8185                "} while (something( ));", Spaces);
8186   verifyFormat("switch (x) {\n"
8187                "default:\n"
8188                "  break;\n"
8189                "}", Spaces);
8190 
8191   Spaces.SpaceAfterCStyleCast = true;
8192   verifyFormat("call(x, y, z);", Spaces);
8193   verifyFormat("while (( bool ) 1)\n"
8194                "  continue;",
8195                Spaces);
8196   verifyFormat("for (;;)\n"
8197                "  continue;",
8198                Spaces);
8199   verifyFormat("if (true)\n"
8200                "  f( );\n"
8201                "else if (true)\n"
8202                "  f( );",
8203                Spaces);
8204   verifyFormat("do {\n"
8205                "  do_something(( int ) i);\n"
8206                "} while (something( ));",
8207                Spaces);
8208   verifyFormat("switch (x) {\n"
8209                "default:\n"
8210                "  break;\n"
8211                "}",
8212                Spaces);
8213   Spaces.SpacesInCStyleCastParentheses = false;
8214   Spaces.SpaceAfterCStyleCast = true;
8215   verifyFormat("while ((bool) 1)\n"
8216                "  continue;",
8217                Spaces);
8218   verifyFormat("do {\n"
8219                "  do_something((int) i);\n"
8220                "} while (something( ));",
8221                Spaces);
8222 }
8223 
8224 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
8225   verifyFormat("int a[5];");
8226   verifyFormat("a[3] += 42;");
8227 
8228   FormatStyle Spaces = getLLVMStyle();
8229   Spaces.SpacesInSquareBrackets = true;
8230   // Lambdas unchanged.
8231   verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
8232   verifyFormat("return [i, args...] {};", Spaces);
8233 
8234   // Not lambdas.
8235   verifyFormat("int a[ 5 ];", Spaces);
8236   verifyFormat("a[ 3 ] += 42;", Spaces);
8237   verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
8238   verifyFormat("double &operator[](int i) { return 0; }\n"
8239                "int i;",
8240                Spaces);
8241   verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
8242   verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
8243   verifyFormat("int i = (*b)[ a ]->f();", Spaces);
8244 }
8245 
8246 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
8247   verifyFormat("int a = 5;");
8248   verifyFormat("a += 42;");
8249   verifyFormat("a or_eq 8;");
8250 
8251   FormatStyle Spaces = getLLVMStyle();
8252   Spaces.SpaceBeforeAssignmentOperators = false;
8253   verifyFormat("int a= 5;", Spaces);
8254   verifyFormat("a+= 42;", Spaces);
8255   verifyFormat("a or_eq 8;", Spaces);
8256 }
8257 
8258 TEST_F(FormatTest, LinuxBraceBreaking) {
8259   FormatStyle LinuxBraceStyle = getLLVMStyle();
8260   LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
8261   verifyFormat("namespace a\n"
8262                "{\n"
8263                "class A\n"
8264                "{\n"
8265                "  void f()\n"
8266                "  {\n"
8267                "    if (true) {\n"
8268                "      a();\n"
8269                "      b();\n"
8270                "    }\n"
8271                "  }\n"
8272                "  void g() { return; }\n"
8273                "};\n"
8274                "struct B {\n"
8275                "  int x;\n"
8276                "};\n"
8277                "}\n",
8278                LinuxBraceStyle);
8279   verifyFormat("enum X {\n"
8280                "  Y = 0,\n"
8281                "}\n",
8282                LinuxBraceStyle);
8283   verifyFormat("struct S {\n"
8284                "  int Type;\n"
8285                "  union {\n"
8286                "    int x;\n"
8287                "    double y;\n"
8288                "  } Value;\n"
8289                "  class C\n"
8290                "  {\n"
8291                "    MyFavoriteType Value;\n"
8292                "  } Class;\n"
8293                "}\n",
8294                LinuxBraceStyle);
8295 }
8296 
8297 TEST_F(FormatTest, StroustrupBraceBreaking) {
8298   FormatStyle StroustrupBraceStyle = getLLVMStyle();
8299   StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
8300   verifyFormat("namespace a {\n"
8301                "class A {\n"
8302                "  void f()\n"
8303                "  {\n"
8304                "    if (true) {\n"
8305                "      a();\n"
8306                "      b();\n"
8307                "    }\n"
8308                "  }\n"
8309                "  void g() { return; }\n"
8310                "};\n"
8311                "struct B {\n"
8312                "  int x;\n"
8313                "};\n"
8314                "}\n",
8315                StroustrupBraceStyle);
8316 
8317   verifyFormat("void foo()\n"
8318                "{\n"
8319                "  if (a) {\n"
8320                "    a();\n"
8321                "  }\n"
8322                "  else {\n"
8323                "    b();\n"
8324                "  }\n"
8325                "}\n",
8326                StroustrupBraceStyle);
8327 
8328   verifyFormat("#ifdef _DEBUG\n"
8329                "int foo(int i = 0)\n"
8330                "#else\n"
8331                "int foo(int i = 5)\n"
8332                "#endif\n"
8333                "{\n"
8334                "  return i;\n"
8335                "}",
8336                StroustrupBraceStyle);
8337 
8338   verifyFormat("void foo() {}\n"
8339                "void bar()\n"
8340                "#ifdef _DEBUG\n"
8341                "{\n"
8342                "  foo();\n"
8343                "}\n"
8344                "#else\n"
8345                "{\n"
8346                "}\n"
8347                "#endif",
8348                StroustrupBraceStyle);
8349 
8350   verifyFormat("void foobar() { int i = 5; }\n"
8351                "#ifdef _DEBUG\n"
8352                "void bar() {}\n"
8353                "#else\n"
8354                "void bar() { foobar(); }\n"
8355                "#endif",
8356                StroustrupBraceStyle);
8357 }
8358 
8359 TEST_F(FormatTest, AllmanBraceBreaking) {
8360   FormatStyle AllmanBraceStyle = getLLVMStyle();
8361   AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
8362   verifyFormat("namespace a\n"
8363                "{\n"
8364                "class A\n"
8365                "{\n"
8366                "  void f()\n"
8367                "  {\n"
8368                "    if (true)\n"
8369                "    {\n"
8370                "      a();\n"
8371                "      b();\n"
8372                "    }\n"
8373                "  }\n"
8374                "  void g() { return; }\n"
8375                "};\n"
8376                "struct B\n"
8377                "{\n"
8378                "  int x;\n"
8379                "};\n"
8380                "}",
8381                AllmanBraceStyle);
8382 
8383   verifyFormat("void f()\n"
8384                "{\n"
8385                "  if (true)\n"
8386                "  {\n"
8387                "    a();\n"
8388                "  }\n"
8389                "  else if (false)\n"
8390                "  {\n"
8391                "    b();\n"
8392                "  }\n"
8393                "  else\n"
8394                "  {\n"
8395                "    c();\n"
8396                "  }\n"
8397                "}\n",
8398                AllmanBraceStyle);
8399 
8400   verifyFormat("void f()\n"
8401                "{\n"
8402                "  for (int i = 0; i < 10; ++i)\n"
8403                "  {\n"
8404                "    a();\n"
8405                "  }\n"
8406                "  while (false)\n"
8407                "  {\n"
8408                "    b();\n"
8409                "  }\n"
8410                "  do\n"
8411                "  {\n"
8412                "    c();\n"
8413                "  } while (false)\n"
8414                "}\n",
8415                AllmanBraceStyle);
8416 
8417   verifyFormat("void f(int a)\n"
8418                "{\n"
8419                "  switch (a)\n"
8420                "  {\n"
8421                "  case 0:\n"
8422                "    break;\n"
8423                "  case 1:\n"
8424                "  {\n"
8425                "    break;\n"
8426                "  }\n"
8427                "  case 2:\n"
8428                "  {\n"
8429                "  }\n"
8430                "  break;\n"
8431                "  default:\n"
8432                "    break;\n"
8433                "  }\n"
8434                "}\n",
8435                AllmanBraceStyle);
8436 
8437   verifyFormat("enum X\n"
8438                "{\n"
8439                "  Y = 0,\n"
8440                "}\n",
8441                AllmanBraceStyle);
8442   verifyFormat("enum X\n"
8443                "{\n"
8444                "  Y = 0\n"
8445                "}\n",
8446                AllmanBraceStyle);
8447 
8448   verifyFormat("@interface BSApplicationController ()\n"
8449                "{\n"
8450                "@private\n"
8451                "  id _extraIvar;\n"
8452                "}\n"
8453                "@end\n",
8454                AllmanBraceStyle);
8455 
8456   verifyFormat("#ifdef _DEBUG\n"
8457                "int foo(int i = 0)\n"
8458                "#else\n"
8459                "int foo(int i = 5)\n"
8460                "#endif\n"
8461                "{\n"
8462                "  return i;\n"
8463                "}",
8464                AllmanBraceStyle);
8465 
8466   verifyFormat("void foo() {}\n"
8467                "void bar()\n"
8468                "#ifdef _DEBUG\n"
8469                "{\n"
8470                "  foo();\n"
8471                "}\n"
8472                "#else\n"
8473                "{\n"
8474                "}\n"
8475                "#endif",
8476                AllmanBraceStyle);
8477 
8478   verifyFormat("void foobar() { int i = 5; }\n"
8479                "#ifdef _DEBUG\n"
8480                "void bar() {}\n"
8481                "#else\n"
8482                "void bar() { foobar(); }\n"
8483                "#endif",
8484                AllmanBraceStyle);
8485 
8486   // This shouldn't affect ObjC blocks..
8487   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
8488                "  // ...\n"
8489                "  int i;\n"
8490                "}];",
8491                AllmanBraceStyle);
8492   verifyFormat("void (^block)(void) = ^{\n"
8493                "  // ...\n"
8494                "  int i;\n"
8495                "};",
8496                AllmanBraceStyle);
8497   // .. or dict literals.
8498   verifyFormat("void f()\n"
8499                "{\n"
8500                "  [object someMethod:@{ @\"a\" : @\"b\" }];\n"
8501                "}",
8502                AllmanBraceStyle);
8503   verifyFormat("int f()\n"
8504                "{ // comment\n"
8505                "  return 42;\n"
8506                "}",
8507                AllmanBraceStyle);
8508 
8509   AllmanBraceStyle.ColumnLimit = 19;
8510   verifyFormat("void f() { int i; }", AllmanBraceStyle);
8511   AllmanBraceStyle.ColumnLimit = 18;
8512   verifyFormat("void f()\n"
8513                "{\n"
8514                "  int i;\n"
8515                "}",
8516                AllmanBraceStyle);
8517   AllmanBraceStyle.ColumnLimit = 80;
8518 
8519   FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
8520   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true;
8521   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
8522   verifyFormat("void f(bool b)\n"
8523                "{\n"
8524                "  if (b)\n"
8525                "  {\n"
8526                "    return;\n"
8527                "  }\n"
8528                "}\n",
8529                BreakBeforeBraceShortIfs);
8530   verifyFormat("void f(bool b)\n"
8531                "{\n"
8532                "  if (b) return;\n"
8533                "}\n",
8534                BreakBeforeBraceShortIfs);
8535   verifyFormat("void f(bool b)\n"
8536                "{\n"
8537                "  while (b)\n"
8538                "  {\n"
8539                "    return;\n"
8540                "  }\n"
8541                "}\n",
8542                BreakBeforeBraceShortIfs);
8543 }
8544 
8545 TEST_F(FormatTest, GNUBraceBreaking) {
8546   FormatStyle GNUBraceStyle = getLLVMStyle();
8547   GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
8548   verifyFormat("namespace a\n"
8549                "{\n"
8550                "class A\n"
8551                "{\n"
8552                "  void f()\n"
8553                "  {\n"
8554                "    int a;\n"
8555                "    {\n"
8556                "      int b;\n"
8557                "    }\n"
8558                "    if (true)\n"
8559                "      {\n"
8560                "        a();\n"
8561                "        b();\n"
8562                "      }\n"
8563                "  }\n"
8564                "  void g() { return; }\n"
8565                "}\n"
8566                "}",
8567                GNUBraceStyle);
8568 
8569   verifyFormat("void f()\n"
8570                "{\n"
8571                "  if (true)\n"
8572                "    {\n"
8573                "      a();\n"
8574                "    }\n"
8575                "  else if (false)\n"
8576                "    {\n"
8577                "      b();\n"
8578                "    }\n"
8579                "  else\n"
8580                "    {\n"
8581                "      c();\n"
8582                "    }\n"
8583                "}\n",
8584                GNUBraceStyle);
8585 
8586   verifyFormat("void f()\n"
8587                "{\n"
8588                "  for (int i = 0; i < 10; ++i)\n"
8589                "    {\n"
8590                "      a();\n"
8591                "    }\n"
8592                "  while (false)\n"
8593                "    {\n"
8594                "      b();\n"
8595                "    }\n"
8596                "  do\n"
8597                "    {\n"
8598                "      c();\n"
8599                "    }\n"
8600                "  while (false);\n"
8601                "}\n",
8602                GNUBraceStyle);
8603 
8604   verifyFormat("void f(int a)\n"
8605                "{\n"
8606                "  switch (a)\n"
8607                "    {\n"
8608                "    case 0:\n"
8609                "      break;\n"
8610                "    case 1:\n"
8611                "      {\n"
8612                "        break;\n"
8613                "      }\n"
8614                "    case 2:\n"
8615                "      {\n"
8616                "      }\n"
8617                "      break;\n"
8618                "    default:\n"
8619                "      break;\n"
8620                "    }\n"
8621                "}\n",
8622                GNUBraceStyle);
8623 
8624   verifyFormat("enum X\n"
8625                "{\n"
8626                "  Y = 0,\n"
8627                "}\n",
8628                GNUBraceStyle);
8629 
8630   verifyFormat("@interface BSApplicationController ()\n"
8631                "{\n"
8632                "@private\n"
8633                "  id _extraIvar;\n"
8634                "}\n"
8635                "@end\n",
8636                GNUBraceStyle);
8637 
8638   verifyFormat("#ifdef _DEBUG\n"
8639                "int foo(int i = 0)\n"
8640                "#else\n"
8641                "int foo(int i = 5)\n"
8642                "#endif\n"
8643                "{\n"
8644                "  return i;\n"
8645                "}",
8646                GNUBraceStyle);
8647 
8648   verifyFormat("void foo() {}\n"
8649                "void bar()\n"
8650                "#ifdef _DEBUG\n"
8651                "{\n"
8652                "  foo();\n"
8653                "}\n"
8654                "#else\n"
8655                "{\n"
8656                "}\n"
8657                "#endif",
8658                GNUBraceStyle);
8659 
8660   verifyFormat("void foobar() { int i = 5; }\n"
8661                "#ifdef _DEBUG\n"
8662                "void bar() {}\n"
8663                "#else\n"
8664                "void bar() { foobar(); }\n"
8665                "#endif",
8666                GNUBraceStyle);
8667 }
8668 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
8669   verifyFormat("void f() {\n"
8670                "  try {\n"
8671                "  } catch (const Exception &e) {\n"
8672                "  }\n"
8673                "}\n",
8674                getLLVMStyle());
8675 }
8676 
8677 TEST_F(FormatTest, UnderstandsPragmas) {
8678   verifyFormat("#pragma omp reduction(| : var)");
8679   verifyFormat("#pragma omp reduction(+ : var)");
8680 
8681   EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
8682             "(including parentheses).",
8683             format("#pragma    mark   Any non-hyphenated or hyphenated string "
8684                    "(including parentheses)."));
8685 }
8686 
8687 #define EXPECT_ALL_STYLES_EQUAL(Styles)                                        \
8688   for (size_t i = 1; i < Styles.size(); ++i)                                   \
8689     EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of "                \
8690                                     << Styles.size()                           \
8691                                     << " differs from Style #0"
8692 
8693 TEST_F(FormatTest, GetsPredefinedStyleByName) {
8694   SmallVector<FormatStyle, 3> Styles;
8695   Styles.resize(3);
8696 
8697   Styles[0] = getLLVMStyle();
8698   EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
8699   EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
8700   EXPECT_ALL_STYLES_EQUAL(Styles);
8701 
8702   Styles[0] = getGoogleStyle();
8703   EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
8704   EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
8705   EXPECT_ALL_STYLES_EQUAL(Styles);
8706 
8707   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
8708   EXPECT_TRUE(
8709       getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
8710   EXPECT_TRUE(
8711       getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
8712   EXPECT_ALL_STYLES_EQUAL(Styles);
8713 
8714   Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
8715   EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
8716   EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
8717   EXPECT_ALL_STYLES_EQUAL(Styles);
8718 
8719   Styles[0] = getMozillaStyle();
8720   EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
8721   EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
8722   EXPECT_ALL_STYLES_EQUAL(Styles);
8723 
8724   Styles[0] = getWebKitStyle();
8725   EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
8726   EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
8727   EXPECT_ALL_STYLES_EQUAL(Styles);
8728 
8729   Styles[0] = getGNUStyle();
8730   EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
8731   EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
8732   EXPECT_ALL_STYLES_EQUAL(Styles);
8733 
8734   EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
8735 }
8736 
8737 TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
8738   SmallVector<FormatStyle, 8> Styles;
8739   Styles.resize(2);
8740 
8741   Styles[0] = getGoogleStyle();
8742   Styles[1] = getLLVMStyle();
8743   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
8744   EXPECT_ALL_STYLES_EQUAL(Styles);
8745 
8746   Styles.resize(5);
8747   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
8748   Styles[1] = getLLVMStyle();
8749   Styles[1].Language = FormatStyle::LK_JavaScript;
8750   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
8751 
8752   Styles[2] = getLLVMStyle();
8753   Styles[2].Language = FormatStyle::LK_JavaScript;
8754   EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
8755                                   "BasedOnStyle: Google",
8756                                   &Styles[2]).value());
8757 
8758   Styles[3] = getLLVMStyle();
8759   Styles[3].Language = FormatStyle::LK_JavaScript;
8760   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
8761                                   "Language: JavaScript",
8762                                   &Styles[3]).value());
8763 
8764   Styles[4] = getLLVMStyle();
8765   Styles[4].Language = FormatStyle::LK_JavaScript;
8766   EXPECT_EQ(0, parseConfiguration("---\n"
8767                                   "BasedOnStyle: LLVM\n"
8768                                   "IndentWidth: 123\n"
8769                                   "---\n"
8770                                   "BasedOnStyle: Google\n"
8771                                   "Language: JavaScript",
8772                                   &Styles[4]).value());
8773   EXPECT_ALL_STYLES_EQUAL(Styles);
8774 }
8775 
8776 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME)                             \
8777   Style.FIELD = false;                                                         \
8778   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value());      \
8779   EXPECT_TRUE(Style.FIELD);                                                    \
8780   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value());     \
8781   EXPECT_FALSE(Style.FIELD);
8782 
8783 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
8784 
8785 #define CHECK_PARSE(TEXT, FIELD, VALUE)                                        \
8786   EXPECT_NE(VALUE, Style.FIELD);                                               \
8787   EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value());                      \
8788   EXPECT_EQ(VALUE, Style.FIELD)
8789 
8790 TEST_F(FormatTest, ParsesConfigurationBools) {
8791   FormatStyle Style = {};
8792   Style.Language = FormatStyle::LK_Cpp;
8793   CHECK_PARSE_BOOL(AlignAfterOpenBracket);
8794   CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft);
8795   CHECK_PARSE_BOOL(AlignOperands);
8796   CHECK_PARSE_BOOL(AlignTrailingComments);
8797   CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
8798   CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine);
8799   CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
8800   CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine);
8801   CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
8802   CHECK_PARSE_BOOL(AlwaysBreakAfterDefinitionReturnType);
8803   CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations);
8804   CHECK_PARSE_BOOL(BinPackParameters);
8805   CHECK_PARSE_BOOL(BinPackArguments);
8806   CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
8807   CHECK_PARSE_BOOL(BreakConstructorInitializersBeforeComma);
8808   CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
8809   CHECK_PARSE_BOOL(DerivePointerAlignment);
8810   CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
8811   CHECK_PARSE_BOOL(IndentCaseLabels);
8812   CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
8813   CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
8814   CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
8815   CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
8816   CHECK_PARSE_BOOL(Cpp11BracedListStyle);
8817   CHECK_PARSE_BOOL(SpacesInParentheses);
8818   CHECK_PARSE_BOOL(SpacesInSquareBrackets);
8819   CHECK_PARSE_BOOL(SpacesInAngles);
8820   CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
8821   CHECK_PARSE_BOOL(SpacesInContainerLiterals);
8822   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
8823   CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
8824   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
8825 }
8826 
8827 #undef CHECK_PARSE_BOOL
8828 
8829 TEST_F(FormatTest, ParsesConfiguration) {
8830   FormatStyle Style = {};
8831   Style.Language = FormatStyle::LK_Cpp;
8832   CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
8833   CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
8834               ConstructorInitializerIndentWidth, 1234u);
8835   CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
8836   CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
8837   CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
8838   CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
8839               PenaltyBreakBeforeFirstCallParameter, 1234u);
8840   CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
8841   CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
8842               PenaltyReturnTypeOnItsOwnLine, 1234u);
8843   CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
8844               SpacesBeforeTrailingComments, 1234u);
8845   CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
8846   CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
8847 
8848   Style.PointerAlignment = FormatStyle::PAS_Middle;
8849   CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
8850               FormatStyle::PAS_Left);
8851   CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
8852               FormatStyle::PAS_Right);
8853   CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
8854               FormatStyle::PAS_Middle);
8855   // For backward compatibility:
8856   CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
8857               FormatStyle::PAS_Left);
8858   CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
8859               FormatStyle::PAS_Right);
8860   CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
8861               FormatStyle::PAS_Middle);
8862 
8863   Style.Standard = FormatStyle::LS_Auto;
8864   CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
8865   CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11);
8866   CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
8867   CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
8868   CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
8869 
8870   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
8871   CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
8872               BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
8873   CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
8874               FormatStyle::BOS_None);
8875   CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
8876               FormatStyle::BOS_All);
8877   // For backward compatibility:
8878   CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
8879               FormatStyle::BOS_None);
8880   CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
8881               FormatStyle::BOS_All);
8882 
8883   Style.UseTab = FormatStyle::UT_ForIndentation;
8884   CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
8885   CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
8886   CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
8887   // For backward compatibility:
8888   CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
8889   CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
8890 
8891   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
8892   CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
8893               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
8894   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
8895               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
8896   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
8897               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
8898   CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
8899               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
8900   // For backward compatibility:
8901   CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
8902               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
8903   CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
8904               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
8905 
8906   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
8907   CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
8908               FormatStyle::SBPO_Never);
8909   CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
8910               FormatStyle::SBPO_Always);
8911   CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
8912               FormatStyle::SBPO_ControlStatements);
8913   // For backward compatibility:
8914   CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
8915               FormatStyle::SBPO_Never);
8916   CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
8917               FormatStyle::SBPO_ControlStatements);
8918 
8919   Style.ColumnLimit = 123;
8920   FormatStyle BaseStyle = getLLVMStyle();
8921   CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
8922   CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
8923 
8924   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
8925   CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
8926               FormatStyle::BS_Attach);
8927   CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
8928               FormatStyle::BS_Linux);
8929   CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
8930               FormatStyle::BS_Stroustrup);
8931   CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
8932               FormatStyle::BS_Allman);
8933   CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
8934 
8935   Style.NamespaceIndentation = FormatStyle::NI_All;
8936   CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
8937               FormatStyle::NI_None);
8938   CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
8939               FormatStyle::NI_Inner);
8940   CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
8941               FormatStyle::NI_All);
8942 
8943   Style.ForEachMacros.clear();
8944   std::vector<std::string> BoostForeach;
8945   BoostForeach.push_back("BOOST_FOREACH");
8946   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
8947   std::vector<std::string> BoostAndQForeach;
8948   BoostAndQForeach.push_back("BOOST_FOREACH");
8949   BoostAndQForeach.push_back("Q_FOREACH");
8950   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
8951               BoostAndQForeach);
8952 }
8953 
8954 TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
8955   FormatStyle Style = {};
8956   Style.Language = FormatStyle::LK_Cpp;
8957   CHECK_PARSE("Language: Cpp\n"
8958               "IndentWidth: 12",
8959               IndentWidth, 12u);
8960   EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
8961                                "IndentWidth: 34",
8962                                &Style),
8963             ParseError::Unsuitable);
8964   EXPECT_EQ(12u, Style.IndentWidth);
8965   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
8966   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
8967 
8968   Style.Language = FormatStyle::LK_JavaScript;
8969   CHECK_PARSE("Language: JavaScript\n"
8970               "IndentWidth: 12",
8971               IndentWidth, 12u);
8972   CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
8973   EXPECT_EQ(parseConfiguration("Language: Cpp\n"
8974                                "IndentWidth: 34",
8975                                &Style),
8976             ParseError::Unsuitable);
8977   EXPECT_EQ(23u, Style.IndentWidth);
8978   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
8979   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
8980 
8981   CHECK_PARSE("BasedOnStyle: LLVM\n"
8982               "IndentWidth: 67",
8983               IndentWidth, 67u);
8984 
8985   CHECK_PARSE("---\n"
8986               "Language: JavaScript\n"
8987               "IndentWidth: 12\n"
8988               "---\n"
8989               "Language: Cpp\n"
8990               "IndentWidth: 34\n"
8991               "...\n",
8992               IndentWidth, 12u);
8993 
8994   Style.Language = FormatStyle::LK_Cpp;
8995   CHECK_PARSE("---\n"
8996               "Language: JavaScript\n"
8997               "IndentWidth: 12\n"
8998               "---\n"
8999               "Language: Cpp\n"
9000               "IndentWidth: 34\n"
9001               "...\n",
9002               IndentWidth, 34u);
9003   CHECK_PARSE("---\n"
9004               "IndentWidth: 78\n"
9005               "---\n"
9006               "Language: JavaScript\n"
9007               "IndentWidth: 56\n"
9008               "...\n",
9009               IndentWidth, 78u);
9010 
9011   Style.ColumnLimit = 123;
9012   Style.IndentWidth = 234;
9013   Style.BreakBeforeBraces = FormatStyle::BS_Linux;
9014   Style.TabWidth = 345;
9015   EXPECT_FALSE(parseConfiguration("---\n"
9016                                   "IndentWidth: 456\n"
9017                                   "BreakBeforeBraces: Allman\n"
9018                                   "---\n"
9019                                   "Language: JavaScript\n"
9020                                   "IndentWidth: 111\n"
9021                                   "TabWidth: 111\n"
9022                                   "---\n"
9023                                   "Language: Cpp\n"
9024                                   "BreakBeforeBraces: Stroustrup\n"
9025                                   "TabWidth: 789\n"
9026                                   "...\n",
9027                                   &Style));
9028   EXPECT_EQ(123u, Style.ColumnLimit);
9029   EXPECT_EQ(456u, Style.IndentWidth);
9030   EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
9031   EXPECT_EQ(789u, Style.TabWidth);
9032 
9033   EXPECT_EQ(parseConfiguration("---\n"
9034                                "Language: JavaScript\n"
9035                                "IndentWidth: 56\n"
9036                                "---\n"
9037                                "IndentWidth: 78\n"
9038                                "...\n",
9039                                &Style),
9040             ParseError::Error);
9041   EXPECT_EQ(parseConfiguration("---\n"
9042                                "Language: JavaScript\n"
9043                                "IndentWidth: 56\n"
9044                                "---\n"
9045                                "Language: JavaScript\n"
9046                                "IndentWidth: 78\n"
9047                                "...\n",
9048                                &Style),
9049             ParseError::Error);
9050 
9051   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
9052 }
9053 
9054 #undef CHECK_PARSE
9055 
9056 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
9057   FormatStyle Style = {};
9058   Style.Language = FormatStyle::LK_JavaScript;
9059   Style.BreakBeforeTernaryOperators = true;
9060   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
9061   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
9062 
9063   Style.BreakBeforeTernaryOperators = true;
9064   EXPECT_EQ(0, parseConfiguration("---\n"
9065               "BasedOnStyle: Google\n"
9066               "---\n"
9067               "Language: JavaScript\n"
9068               "IndentWidth: 76\n"
9069               "...\n", &Style).value());
9070   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
9071   EXPECT_EQ(76u, Style.IndentWidth);
9072   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
9073 }
9074 
9075 TEST_F(FormatTest, ConfigurationRoundTripTest) {
9076   FormatStyle Style = getLLVMStyle();
9077   std::string YAML = configurationAsText(Style);
9078   FormatStyle ParsedStyle = {};
9079   ParsedStyle.Language = FormatStyle::LK_Cpp;
9080   EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
9081   EXPECT_EQ(Style, ParsedStyle);
9082 }
9083 
9084 TEST_F(FormatTest, WorksFor8bitEncodings) {
9085   EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
9086             "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
9087             "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
9088             "\"\xef\xee\xf0\xf3...\"",
9089             format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
9090                    "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
9091                    "\xef\xee\xf0\xf3...\"",
9092                    getLLVMStyleWithColumns(12)));
9093 }
9094 
9095 TEST_F(FormatTest, HandlesUTF8BOM) {
9096   EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
9097   EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
9098             format("\xef\xbb\xbf#include <iostream>"));
9099   EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
9100             format("\xef\xbb\xbf\n#include <iostream>"));
9101 }
9102 
9103 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
9104 #if !defined(_MSC_VER)
9105 
9106 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
9107   verifyFormat("\"Однажды в студёную зимнюю пору...\"",
9108                getLLVMStyleWithColumns(35));
9109   verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
9110                getLLVMStyleWithColumns(31));
9111   verifyFormat("// Однажды в студёную зимнюю пору...",
9112                getLLVMStyleWithColumns(36));
9113   verifyFormat("// 一 二 三 四 五 六 七 八 九 十",
9114                getLLVMStyleWithColumns(32));
9115   verifyFormat("/* Однажды в студёную зимнюю пору... */",
9116                getLLVMStyleWithColumns(39));
9117   verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
9118                getLLVMStyleWithColumns(35));
9119 }
9120 
9121 TEST_F(FormatTest, SplitsUTF8Strings) {
9122   // Non-printable characters' width is currently considered to be the length in
9123   // bytes in UTF8. The characters can be displayed in very different manner
9124   // (zero-width, single width with a substitution glyph, expanded to their code
9125   // (e.g. "<8d>"), so there's no single correct way to handle them.
9126   EXPECT_EQ("\"aaaaÄ\"\n"
9127             "\"\xc2\x8d\";",
9128             format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
9129   EXPECT_EQ("\"aaaaaaaÄ\"\n"
9130             "\"\xc2\x8d\";",
9131             format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
9132   EXPECT_EQ(
9133       "\"Однажды, в \"\n"
9134       "\"студёную \"\n"
9135       "\"зимнюю \"\n"
9136       "\"пору,\"",
9137       format("\"Однажды, в студёную зимнюю пору,\"",
9138              getLLVMStyleWithColumns(13)));
9139   EXPECT_EQ("\"一 二 三 \"\n"
9140             "\"四 五六 \"\n"
9141             "\"七 八 九 \"\n"
9142             "\"十\"",
9143             format("\"一 二 三 四 五六 七 八 九 十\"",
9144                    getLLVMStyleWithColumns(11)));
9145   EXPECT_EQ("\"一\t二 \"\n"
9146             "\"\t三 \"\n"
9147             "\"四 五\t六 \"\n"
9148             "\"\t七 \"\n"
9149             "\"八九十\tqq\"",
9150             format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
9151                    getLLVMStyleWithColumns(11)));
9152 }
9153 
9154 
9155 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
9156   EXPECT_EQ("const char *sssss =\n"
9157             "    \"一二三四五六七八\\\n"
9158             " 九 十\";",
9159             format("const char *sssss = \"一二三四五六七八\\\n"
9160                    " 九 十\";",
9161                    getLLVMStyleWithColumns(30)));
9162 }
9163 
9164 TEST_F(FormatTest, SplitsUTF8LineComments) {
9165   EXPECT_EQ("// aaaaÄ\xc2\x8d",
9166             format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
9167   EXPECT_EQ("// Я из лесу\n"
9168             "// вышел; был\n"
9169             "// сильный\n"
9170             "// мороз.",
9171             format("// Я из лесу вышел; был сильный мороз.",
9172                    getLLVMStyleWithColumns(13)));
9173   EXPECT_EQ("// 一二三\n"
9174             "// 四五六七\n"
9175             "// 八  九\n"
9176             "// 十",
9177             format("// 一二三 四五六七 八  九 十", getLLVMStyleWithColumns(9)));
9178 }
9179 
9180 TEST_F(FormatTest, SplitsUTF8BlockComments) {
9181   EXPECT_EQ("/* Гляжу,\n"
9182             " * поднимается\n"
9183             " * медленно в\n"
9184             " * гору\n"
9185             " * Лошадка,\n"
9186             " * везущая\n"
9187             " * хворосту\n"
9188             " * воз. */",
9189             format("/* Гляжу, поднимается медленно в гору\n"
9190                    " * Лошадка, везущая хворосту воз. */",
9191                    getLLVMStyleWithColumns(13)));
9192   EXPECT_EQ(
9193       "/* 一二三\n"
9194       " * 四五六七\n"
9195       " * 八  九\n"
9196       " * 十  */",
9197       format("/* 一二三 四五六七 八  九 十  */", getLLVMStyleWithColumns(9)));
9198   EXPECT_EQ("/* �������� ��������\n"
9199             " * ��������\n"
9200             " * ������-�� */",
9201             format("/* �������� �������� �������� ������-�� */", getLLVMStyleWithColumns(12)));
9202 }
9203 
9204 #endif // _MSC_VER
9205 
9206 TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
9207   FormatStyle Style = getLLVMStyle();
9208 
9209   Style.ConstructorInitializerIndentWidth = 4;
9210   verifyFormat(
9211       "SomeClass::Constructor()\n"
9212       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
9213       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
9214       Style);
9215 
9216   Style.ConstructorInitializerIndentWidth = 2;
9217   verifyFormat(
9218       "SomeClass::Constructor()\n"
9219       "  : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
9220       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
9221       Style);
9222 
9223   Style.ConstructorInitializerIndentWidth = 0;
9224   verifyFormat(
9225       "SomeClass::Constructor()\n"
9226       ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
9227       "  aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
9228       Style);
9229 }
9230 
9231 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
9232   FormatStyle Style = getLLVMStyle();
9233   Style.BreakConstructorInitializersBeforeComma = true;
9234   Style.ConstructorInitializerIndentWidth = 4;
9235   verifyFormat("SomeClass::Constructor()\n"
9236                "    : a(a)\n"
9237                "    , b(b)\n"
9238                "    , c(c) {}",
9239                Style);
9240   verifyFormat("SomeClass::Constructor()\n"
9241                "    : a(a) {}",
9242                Style);
9243 
9244   Style.ColumnLimit = 0;
9245   verifyFormat("SomeClass::Constructor()\n"
9246                "    : a(a) {}",
9247                Style);
9248   verifyFormat("SomeClass::Constructor()\n"
9249                "    : a(a)\n"
9250                "    , b(b)\n"
9251                "    , c(c) {}",
9252                Style);
9253   verifyFormat("SomeClass::Constructor()\n"
9254                "    : a(a) {\n"
9255                "  foo();\n"
9256                "  bar();\n"
9257                "}",
9258                Style);
9259 
9260   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
9261   verifyFormat("SomeClass::Constructor()\n"
9262                "    : a(a)\n"
9263                "    , b(b)\n"
9264                "    , c(c) {\n}",
9265                Style);
9266   verifyFormat("SomeClass::Constructor()\n"
9267                "    : a(a) {\n}",
9268                Style);
9269 
9270   Style.ColumnLimit = 80;
9271   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
9272   Style.ConstructorInitializerIndentWidth = 2;
9273   verifyFormat("SomeClass::Constructor()\n"
9274                "  : a(a)\n"
9275                "  , b(b)\n"
9276                "  , c(c) {}",
9277                Style);
9278 
9279   Style.ConstructorInitializerIndentWidth = 0;
9280   verifyFormat("SomeClass::Constructor()\n"
9281                ": a(a)\n"
9282                ", b(b)\n"
9283                ", c(c) {}",
9284                Style);
9285 
9286   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
9287   Style.ConstructorInitializerIndentWidth = 4;
9288   verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
9289   verifyFormat(
9290       "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
9291       Style);
9292   verifyFormat(
9293       "SomeClass::Constructor()\n"
9294       "    : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
9295       Style);
9296   Style.ConstructorInitializerIndentWidth = 4;
9297   Style.ColumnLimit = 60;
9298   verifyFormat("SomeClass::Constructor()\n"
9299                "    : aaaaaaaa(aaaaaaaa)\n"
9300                "    , aaaaaaaa(aaaaaaaa)\n"
9301                "    , aaaaaaaa(aaaaaaaa) {}",
9302                Style);
9303 }
9304 
9305 TEST_F(FormatTest, Destructors) {
9306   verifyFormat("void F(int &i) { i.~int(); }");
9307   verifyFormat("void F(int &i) { i->~int(); }");
9308 }
9309 
9310 TEST_F(FormatTest, FormatsWithWebKitStyle) {
9311   FormatStyle Style = getWebKitStyle();
9312 
9313   // Don't indent in outer namespaces.
9314   verifyFormat("namespace outer {\n"
9315                "int i;\n"
9316                "namespace inner {\n"
9317                "    int i;\n"
9318                "} // namespace inner\n"
9319                "} // namespace outer\n"
9320                "namespace other_outer {\n"
9321                "int i;\n"
9322                "}",
9323                Style);
9324 
9325   // Don't indent case labels.
9326   verifyFormat("switch (variable) {\n"
9327                "case 1:\n"
9328                "case 2:\n"
9329                "    doSomething();\n"
9330                "    break;\n"
9331                "default:\n"
9332                "    ++variable;\n"
9333                "}",
9334                Style);
9335 
9336   // Wrap before binary operators.
9337   EXPECT_EQ(
9338       "void f()\n"
9339       "{\n"
9340       "    if (aaaaaaaaaaaaaaaa\n"
9341       "        && bbbbbbbbbbbbbbbbbbbbbbbb\n"
9342       "        && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
9343       "        return;\n"
9344       "}",
9345       format(
9346           "void f() {\n"
9347           "if (aaaaaaaaaaaaaaaa\n"
9348           "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
9349           "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
9350           "return;\n"
9351           "}",
9352           Style));
9353 
9354   // Allow functions on a single line.
9355   verifyFormat("void f() { return; }", Style);
9356 
9357   // Constructor initializers are formatted one per line with the "," on the
9358   // new line.
9359   verifyFormat("Constructor()\n"
9360                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9361                "    , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
9362                "          aaaaaaaaaaaaaa)\n"
9363                "    , aaaaaaaaaaaaaaaaaaaaaaa()\n"
9364                "{\n"
9365                "}",
9366                Style);
9367   verifyFormat("SomeClass::Constructor()\n"
9368                "    : a(a)\n"
9369                "{\n"
9370                "}",
9371                Style);
9372   EXPECT_EQ("SomeClass::Constructor()\n"
9373             "    : a(a)\n"
9374             "{\n"
9375             "}",
9376             format("SomeClass::Constructor():a(a){}", Style));
9377   verifyFormat("SomeClass::Constructor()\n"
9378                "    : a(a)\n"
9379                "    , b(b)\n"
9380                "    , c(c)\n"
9381                "{\n"
9382                "}", Style);
9383   verifyFormat("SomeClass::Constructor()\n"
9384                "    : a(a)\n"
9385                "{\n"
9386                "    foo();\n"
9387                "    bar();\n"
9388                "}",
9389                Style);
9390 
9391   // Access specifiers should be aligned left.
9392   verifyFormat("class C {\n"
9393                "public:\n"
9394                "    int i;\n"
9395                "};",
9396                Style);
9397 
9398   // Do not align comments.
9399   verifyFormat("int a; // Do not\n"
9400                "double b; // align comments.",
9401                Style);
9402 
9403   // Do not align operands.
9404   EXPECT_EQ("ASSERT(aaaa\n"
9405             "    || bbbb);",
9406             format("ASSERT ( aaaa\n||bbbb);", Style));
9407 
9408   // Accept input's line breaks.
9409   EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
9410             "    || bbbbbbbbbbbbbbb) {\n"
9411             "    i++;\n"
9412             "}",
9413             format("if (aaaaaaaaaaaaaaa\n"
9414                    "|| bbbbbbbbbbbbbbb) { i++; }",
9415                    Style));
9416   EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
9417             "    i++;\n"
9418             "}",
9419             format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
9420 
9421   // Don't automatically break all macro definitions (llvm.org/PR17842).
9422   verifyFormat("#define aNumber 10", Style);
9423   // However, generally keep the line breaks that the user authored.
9424   EXPECT_EQ("#define aNumber \\\n"
9425             "    10",
9426             format("#define aNumber \\\n"
9427                    " 10",
9428                    Style));
9429 
9430   // Keep empty and one-element array literals on a single line.
9431   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
9432             "                                  copyItems:YES];",
9433             format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
9434                    "copyItems:YES];",
9435                    Style));
9436   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
9437             "                                  copyItems:YES];",
9438             format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
9439                    "             copyItems:YES];",
9440                    Style));
9441   // FIXME: This does not seem right, there should be more indentation before
9442   // the array literal's entries. Nested blocks have the same problem.
9443   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
9444             "    @\"a\",\n"
9445             "    @\"a\"\n"
9446             "]\n"
9447             "                                  copyItems:YES];",
9448             format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
9449                    "     @\"a\",\n"
9450                    "     @\"a\"\n"
9451                    "     ]\n"
9452                    "       copyItems:YES];",
9453                    Style));
9454   EXPECT_EQ(
9455       "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
9456       "                                  copyItems:YES];",
9457       format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
9458              "   copyItems:YES];",
9459              Style));
9460 
9461   verifyFormat("[self.a b:c c:d];", Style);
9462   EXPECT_EQ("[self.a b:c\n"
9463             "        c:d];",
9464             format("[self.a b:c\n"
9465                    "c:d];",
9466                    Style));
9467 }
9468 
9469 TEST_F(FormatTest, FormatsLambdas) {
9470   verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
9471   verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
9472   verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
9473   verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
9474   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
9475   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
9476   verifyFormat("void f() {\n"
9477                "  other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
9478                "}\n");
9479   verifyFormat("void f() {\n"
9480                "  other(x.begin(), //\n"
9481                "        x.end(),   //\n"
9482                "        [&](int, int) { return 1; });\n"
9483                "}\n");
9484   verifyFormat("SomeFunction([]() { // A cool function...\n"
9485                "  return 43;\n"
9486                "});");
9487   EXPECT_EQ("SomeFunction([]() {\n"
9488             "#define A a\n"
9489             "  return 43;\n"
9490             "});",
9491             format("SomeFunction([](){\n"
9492                    "#define A a\n"
9493                    "return 43;\n"
9494                    "});"));
9495   verifyFormat("void f() {\n"
9496                "  SomeFunction([](decltype(x), A *a) {});\n"
9497                "}");
9498   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9499                "    [](const aaaaaaaaaa &a) { return a; });");
9500   verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
9501                "  SomeOtherFunctioooooooooooooooooooooooooon();\n"
9502                "});");
9503   verifyFormat("Constructor()\n"
9504                "    : Field([] { // comment\n"
9505                "        int i;\n"
9506                "      }) {}");
9507 
9508   // Lambdas with return types.
9509   verifyFormat("int c = []() -> int { return 2; }();\n");
9510   verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
9511   verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
9512   verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
9513   verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
9514   verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
9515   verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
9516   verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
9517                "                   int j) -> int {\n"
9518                "  return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
9519                "};");
9520 
9521   // Multiple lambdas in the same parentheses change indentation rules.
9522   verifyFormat("SomeFunction(\n"
9523                "    []() {\n"
9524                "      int i = 42;\n"
9525                "      return i;\n"
9526                "    },\n"
9527                "    []() {\n"
9528                "      int j = 43;\n"
9529                "      return j;\n"
9530                "    });");
9531 
9532   // More complex introducers.
9533   verifyFormat("return [i, args...] {};");
9534 
9535   // Not lambdas.
9536   verifyFormat("constexpr char hello[]{\"hello\"};");
9537   verifyFormat("double &operator[](int i) { return 0; }\n"
9538                "int i;");
9539   verifyFormat("std::unique_ptr<int[]> foo() {}");
9540   verifyFormat("int i = a[a][a]->f();");
9541   verifyFormat("int i = (*b)[a]->f();");
9542 
9543   // Other corner cases.
9544   verifyFormat("void f() {\n"
9545                "  bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
9546                "      );\n"
9547                "}");
9548 
9549   // Lambdas created through weird macros.
9550   verifyFormat("void f() {\n"
9551                "  MACRO((const AA &a) { return 1; });\n"
9552                "}");
9553 
9554   verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
9555                "      doo_dah();\n"
9556                "      doo_dah();\n"
9557                "    })) {\n"
9558                "}");
9559   verifyFormat("auto lambda = []() {\n"
9560                "  int a = 2\n"
9561                "#if A\n"
9562                "          + 2\n"
9563                "#endif\n"
9564                "      ;\n"
9565                "};");
9566 }
9567 
9568 TEST_F(FormatTest, FormatsBlocks) {
9569   FormatStyle ShortBlocks = getLLVMStyle();
9570   ShortBlocks.AllowShortBlocksOnASingleLine = true;
9571   verifyFormat("int (^Block)(int, int);", ShortBlocks);
9572   verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
9573   verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
9574   verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
9575   verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
9576   verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
9577 
9578   verifyFormat("foo(^{ bar(); });", ShortBlocks);
9579   verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
9580   verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
9581 
9582   verifyFormat("[operation setCompletionBlock:^{\n"
9583                "  [self onOperationDone];\n"
9584                "}];");
9585   verifyFormat("int i = {[operation setCompletionBlock:^{\n"
9586                "  [self onOperationDone];\n"
9587                "}]};");
9588   verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
9589                "  f();\n"
9590                "}];");
9591   verifyFormat("int a = [operation block:^int(int *i) {\n"
9592                "  return 1;\n"
9593                "}];");
9594   verifyFormat("[myObject doSomethingWith:arg1\n"
9595                "                      aaa:^int(int *a) {\n"
9596                "                        return 1;\n"
9597                "                      }\n"
9598                "                      bbb:f(a * bbbbbbbb)];");
9599 
9600   verifyFormat("[operation setCompletionBlock:^{\n"
9601                "  [self.delegate newDataAvailable];\n"
9602                "}];",
9603                getLLVMStyleWithColumns(60));
9604   verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
9605                "  NSString *path = [self sessionFilePath];\n"
9606                "  if (path) {\n"
9607                "    // ...\n"
9608                "  }\n"
9609                "});");
9610   verifyFormat("[[SessionService sharedService]\n"
9611                "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
9612                "      if (window) {\n"
9613                "        [self windowDidLoad:window];\n"
9614                "      } else {\n"
9615                "        [self errorLoadingWindow];\n"
9616                "      }\n"
9617                "    }];");
9618   verifyFormat("void (^largeBlock)(void) = ^{\n"
9619                "  // ...\n"
9620                "};\n",
9621                getLLVMStyleWithColumns(40));
9622   verifyFormat("[[SessionService sharedService]\n"
9623                "    loadWindowWithCompletionBlock: //\n"
9624                "        ^(SessionWindow *window) {\n"
9625                "          if (window) {\n"
9626                "            [self windowDidLoad:window];\n"
9627                "          } else {\n"
9628                "            [self errorLoadingWindow];\n"
9629                "          }\n"
9630                "        }];",
9631                getLLVMStyleWithColumns(60));
9632   verifyFormat("[myObject doSomethingWith:arg1\n"
9633                "    firstBlock:^(Foo *a) {\n"
9634                "      // ...\n"
9635                "      int i;\n"
9636                "    }\n"
9637                "    secondBlock:^(Bar *b) {\n"
9638                "      // ...\n"
9639                "      int i;\n"
9640                "    }\n"
9641                "    thirdBlock:^Foo(Bar *b) {\n"
9642                "      // ...\n"
9643                "      int i;\n"
9644                "    }];");
9645   verifyFormat("[myObject doSomethingWith:arg1\n"
9646                "               firstBlock:-1\n"
9647                "              secondBlock:^(Bar *b) {\n"
9648                "                // ...\n"
9649                "                int i;\n"
9650                "              }];");
9651 
9652   verifyFormat("f(^{\n"
9653                "  @autoreleasepool {\n"
9654                "    if (a) {\n"
9655                "      g();\n"
9656                "    }\n"
9657                "  }\n"
9658                "});");
9659   verifyFormat("Block b = ^int *(A *a, B *b) {}");
9660 
9661   FormatStyle FourIndent = getLLVMStyle();
9662   FourIndent.ObjCBlockIndentWidth = 4;
9663   verifyFormat("[operation setCompletionBlock:^{\n"
9664                "    [self onOperationDone];\n"
9665                "}];",
9666                FourIndent);
9667 }
9668 
9669 TEST_F(FormatTest, SupportsCRLF) {
9670   EXPECT_EQ("int a;\r\n"
9671             "int b;\r\n"
9672             "int c;\r\n",
9673             format("int a;\r\n"
9674                    "  int b;\r\n"
9675                    "    int c;\r\n",
9676                    getLLVMStyle()));
9677   EXPECT_EQ("int a;\r\n"
9678             "int b;\r\n"
9679             "int c;\r\n",
9680             format("int a;\r\n"
9681                    "  int b;\n"
9682                    "    int c;\r\n",
9683                    getLLVMStyle()));
9684   EXPECT_EQ("int a;\n"
9685             "int b;\n"
9686             "int c;\n",
9687             format("int a;\r\n"
9688                    "  int b;\n"
9689                    "    int c;\n",
9690                    getLLVMStyle()));
9691   EXPECT_EQ("\"aaaaaaa \"\r\n"
9692             "\"bbbbbbb\";\r\n",
9693             format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
9694   EXPECT_EQ("#define A \\\r\n"
9695             "  b;      \\\r\n"
9696             "  c;      \\\r\n"
9697             "  d;\r\n",
9698             format("#define A \\\r\n"
9699                    "  b; \\\r\n"
9700                    "  c; d; \r\n",
9701                    getGoogleStyle()));
9702 
9703   EXPECT_EQ("/*\r\n"
9704             "multi line block comments\r\n"
9705             "should not introduce\r\n"
9706             "an extra carriage return\r\n"
9707             "*/\r\n",
9708             format("/*\r\n"
9709                    "multi line block comments\r\n"
9710                    "should not introduce\r\n"
9711                    "an extra carriage return\r\n"
9712                    "*/\r\n"));
9713 }
9714 
9715 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
9716   verifyFormat("MY_CLASS(C) {\n"
9717                "  int i;\n"
9718                "  int j;\n"
9719                "};");
9720 }
9721 
9722 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
9723   FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
9724   TwoIndent.ContinuationIndentWidth = 2;
9725 
9726   EXPECT_EQ("int i =\n"
9727             "  longFunction(\n"
9728             "    arg);",
9729             format("int i = longFunction(arg);", TwoIndent));
9730 
9731   FormatStyle SixIndent = getLLVMStyleWithColumns(20);
9732   SixIndent.ContinuationIndentWidth = 6;
9733 
9734   EXPECT_EQ("int i =\n"
9735             "      longFunction(\n"
9736             "            arg);",
9737             format("int i = longFunction(arg);", SixIndent));
9738 }
9739 
9740 TEST_F(FormatTest, SpacesInAngles) {
9741   FormatStyle Spaces = getLLVMStyle();
9742   Spaces.SpacesInAngles = true;
9743 
9744   verifyFormat("static_cast< int >(arg);", Spaces);
9745   verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
9746   verifyFormat("f< int, float >();", Spaces);
9747   verifyFormat("template <> g() {}", Spaces);
9748   verifyFormat("template < std::vector< int > > f() {}", Spaces);
9749 
9750   Spaces.Standard = FormatStyle::LS_Cpp03;
9751   Spaces.SpacesInAngles = true;
9752   verifyFormat("A< A< int > >();", Spaces);
9753 
9754   Spaces.SpacesInAngles = false;
9755   verifyFormat("A<A<int> >();", Spaces);
9756 
9757   Spaces.Standard = FormatStyle::LS_Cpp11;
9758   Spaces.SpacesInAngles = true;
9759   verifyFormat("A< A< int > >();", Spaces);
9760 
9761   Spaces.SpacesInAngles = false;
9762   verifyFormat("A<A<int>>();", Spaces);
9763 }
9764 
9765 TEST_F(FormatTest, TripleAngleBrackets) {
9766   verifyFormat("f<<<1, 1>>>();");
9767   verifyFormat("f<<<1, 1, 1, s>>>();");
9768   verifyFormat("f<<<a, b, c, d>>>();");
9769   EXPECT_EQ("f<<<1, 1>>>();",
9770             format("f <<< 1, 1 >>> ();"));
9771   verifyFormat("f<param><<<1, 1>>>();");
9772   verifyFormat("f<1><<<1, 1>>>();");
9773   EXPECT_EQ("f<param><<<1, 1>>>();",
9774             format("f< param > <<< 1, 1 >>> ();"));
9775   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
9776                "aaaaaaaaaaa<<<\n    1, 1>>>();");
9777 }
9778 
9779 TEST_F(FormatTest, MergeLessLessAtEnd) {
9780   verifyFormat("<<");
9781   EXPECT_EQ("< < <", format("\\\n<<<"));
9782   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
9783                "aaallvm::outs() <<");
9784   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
9785                "aaaallvm::outs()\n    <<");
9786 }
9787 
9788 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
9789   std::string code = "#if A\n"
9790                      "#if B\n"
9791                      "a.\n"
9792                      "#endif\n"
9793                      "    a = 1;\n"
9794                      "#else\n"
9795                      "#endif\n"
9796                      "#if C\n"
9797                      "#else\n"
9798                      "#endif\n";
9799   EXPECT_EQ(code, format(code));
9800 }
9801 
9802 TEST_F(FormatTest, HandleConflictMarkers) {
9803   // Git/SVN conflict markers.
9804   EXPECT_EQ("int a;\n"
9805             "void f() {\n"
9806             "  callme(some(parameter1,\n"
9807             "<<<<<<< text by the vcs\n"
9808             "              parameter2),\n"
9809             "||||||| text by the vcs\n"
9810             "              parameter2),\n"
9811             "         parameter3,\n"
9812             "======= text by the vcs\n"
9813             "              parameter2, parameter3),\n"
9814             ">>>>>>> text by the vcs\n"
9815             "         otherparameter);\n",
9816             format("int a;\n"
9817                    "void f() {\n"
9818                    "  callme(some(parameter1,\n"
9819                    "<<<<<<< text by the vcs\n"
9820                    "  parameter2),\n"
9821                    "||||||| text by the vcs\n"
9822                    "  parameter2),\n"
9823                    "  parameter3,\n"
9824                    "======= text by the vcs\n"
9825                    "  parameter2,\n"
9826                    "  parameter3),\n"
9827                    ">>>>>>> text by the vcs\n"
9828                    "  otherparameter);\n"));
9829 
9830   // Perforce markers.
9831   EXPECT_EQ("void f() {\n"
9832             "  function(\n"
9833             ">>>> text by the vcs\n"
9834             "      parameter,\n"
9835             "==== text by the vcs\n"
9836             "      parameter,\n"
9837             "==== text by the vcs\n"
9838             "      parameter,\n"
9839             "<<<< text by the vcs\n"
9840             "      parameter);\n",
9841             format("void f() {\n"
9842                    "  function(\n"
9843                    ">>>> text by the vcs\n"
9844                    "  parameter,\n"
9845                    "==== text by the vcs\n"
9846                    "  parameter,\n"
9847                    "==== text by the vcs\n"
9848                    "  parameter,\n"
9849                    "<<<< text by the vcs\n"
9850                    "  parameter);\n"));
9851 
9852   EXPECT_EQ("<<<<<<<\n"
9853             "|||||||\n"
9854             "=======\n"
9855             ">>>>>>>",
9856             format("<<<<<<<\n"
9857                    "|||||||\n"
9858                    "=======\n"
9859                    ">>>>>>>"));
9860 
9861   EXPECT_EQ("<<<<<<<\n"
9862             "|||||||\n"
9863             "int i;\n"
9864             "=======\n"
9865             ">>>>>>>",
9866             format("<<<<<<<\n"
9867                    "|||||||\n"
9868                    "int i;\n"
9869                    "=======\n"
9870                    ">>>>>>>"));
9871 
9872   // FIXME: Handle parsing of macros around conflict markers correctly:
9873   EXPECT_EQ("#define Macro \\\n"
9874             "<<<<<<<\n"
9875             "Something \\\n"
9876             "|||||||\n"
9877             "Else \\\n"
9878             "=======\n"
9879             "Other \\\n"
9880             ">>>>>>>\n"
9881             "    End int i;\n",
9882             format("#define Macro \\\n"
9883                    "<<<<<<<\n"
9884                    "  Something \\\n"
9885                    "|||||||\n"
9886                    "  Else \\\n"
9887                    "=======\n"
9888                    "  Other \\\n"
9889                    ">>>>>>>\n"
9890                    "  End\n"
9891                    "int i;\n"));
9892 }
9893 
9894 TEST_F(FormatTest, DisableRegions) {
9895   EXPECT_EQ("int i;\n"
9896             "// clang-format off\n"
9897             "  int j;\n"
9898             "// clang-format on\n"
9899             "int k;",
9900             format(" int  i;\n"
9901                    "   // clang-format off\n"
9902                    "  int j;\n"
9903                    " // clang-format on\n"
9904                    "   int   k;"));
9905   EXPECT_EQ("int i;\n"
9906             "/* clang-format off */\n"
9907             "  int j;\n"
9908             "/* clang-format on */\n"
9909             "int k;",
9910             format(" int  i;\n"
9911                    "   /* clang-format off */\n"
9912                    "  int j;\n"
9913                    " /* clang-format on */\n"
9914                    "   int   k;"));
9915 }
9916 
9917 TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
9918   format("? ) =");
9919 }
9920 
9921 } // end namespace tooling
9922 } // end namespace clang
9923