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