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