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 "clang/Format/Format.h"
11 
12 #include "../Tooling/ReplacementTest.h"
13 #include "FormatTestUtils.h"
14 
15 #include "clang/Frontend/TextDiagnosticPrinter.h"
16 #include "llvm/Support/Debug.h"
17 #include "llvm/Support/MemoryBuffer.h"
18 #include "gtest/gtest.h"
19 
20 #define DEBUG_TYPE "format-test"
21 
22 using clang::tooling::ReplacementTest;
23 using clang::tooling::toReplacements;
24 
25 namespace clang {
26 namespace format {
27 namespace {
28 
29 FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); }
30 
31 class FormatTest : public ::testing::Test {
32 protected:
33   enum StatusCheck {
34     SC_ExpectComplete,
35     SC_ExpectIncomplete,
36     SC_DoNotCheck
37   };
38 
39   std::string format(llvm::StringRef Code,
40                      const FormatStyle &Style = getLLVMStyle(),
41                      StatusCheck CheckComplete = SC_ExpectComplete) {
42     DEBUG(llvm::errs() << "---\n");
43     DEBUG(llvm::errs() << Code << "\n\n");
44     std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
45     FormattingAttemptStatus Status;
46     tooling::Replacements Replaces =
47         reformat(Style, Code, Ranges, "<stdin>", &Status);
48     if (CheckComplete != SC_DoNotCheck) {
49       bool ExpectedCompleteFormat = CheckComplete == SC_ExpectComplete;
50       EXPECT_EQ(ExpectedCompleteFormat, Status.FormatComplete)
51           << Code << "\n\n";
52     }
53     ReplacementCount = Replaces.size();
54     auto Result = applyAllReplacements(Code, Replaces);
55     EXPECT_TRUE(static_cast<bool>(Result));
56     DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
57     return *Result;
58   }
59 
60   FormatStyle getStyleWithColumns(FormatStyle Style, unsigned ColumnLimit) {
61     Style.ColumnLimit = ColumnLimit;
62     return Style;
63   }
64 
65   FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
66     return getStyleWithColumns(getLLVMStyle(), ColumnLimit);
67   }
68 
69   FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) {
70     return getStyleWithColumns(getGoogleStyle(), ColumnLimit);
71   }
72 
73   void verifyFormat(llvm::StringRef Expected, llvm::StringRef Code,
74                     const FormatStyle &Style = getLLVMStyle()) {
75     EXPECT_EQ(Expected.str(), format(Expected, Style))
76         << "Expected code is not stable";
77     EXPECT_EQ(Expected.str(), format(Code, Style));
78     if (Style.Language == FormatStyle::LK_Cpp) {
79       // Objective-C++ is a superset of C++, so everything checked for C++
80       // needs to be checked for Objective-C++ as well.
81       FormatStyle ObjCStyle = Style;
82       ObjCStyle.Language = FormatStyle::LK_ObjC;
83       EXPECT_EQ(Expected.str(), format(test::messUp(Code), ObjCStyle));
84     }
85   }
86 
87   void verifyFormat(llvm::StringRef Code,
88                     const FormatStyle &Style = getLLVMStyle()) {
89     verifyFormat(Code, test::messUp(Code), Style);
90   }
91 
92   void verifyIncompleteFormat(llvm::StringRef Code,
93                               const FormatStyle &Style = getLLVMStyle()) {
94     EXPECT_EQ(Code.str(),
95               format(test::messUp(Code), Style, SC_ExpectIncomplete));
96   }
97 
98   void verifyGoogleFormat(llvm::StringRef Code) {
99     verifyFormat(Code, getGoogleStyle());
100   }
101 
102   void verifyIndependentOfContext(llvm::StringRef text) {
103     verifyFormat(text);
104     verifyFormat(llvm::Twine("void f() { " + text + " }").str());
105   }
106 
107   /// \brief Verify that clang-format does not crash on the given input.
108   void verifyNoCrash(llvm::StringRef Code,
109                      const FormatStyle &Style = getLLVMStyle()) {
110     format(Code, Style, SC_DoNotCheck);
111   }
112 
113   int ReplacementCount;
114 };
115 
116 TEST_F(FormatTest, MessUp) {
117   EXPECT_EQ("1 2 3", test::messUp("1 2 3"));
118   EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n"));
119   EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc"));
120   EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc"));
121   EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne"));
122 }
123 
124 //===----------------------------------------------------------------------===//
125 // Basic function tests.
126 //===----------------------------------------------------------------------===//
127 
128 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) {
129   EXPECT_EQ(";", format(";"));
130 }
131 
132 TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
133   EXPECT_EQ("int i;", format("  int i;"));
134   EXPECT_EQ("\nint i;", format(" \n\t \v \f  int i;"));
135   EXPECT_EQ("int i;\nint j;", format("    int i; int j;"));
136   EXPECT_EQ("int i;\nint j;", format("    int i;\n  int j;"));
137 }
138 
139 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
140   EXPECT_EQ("int i;", format("int\ni;"));
141 }
142 
143 TEST_F(FormatTest, FormatsNestedBlockStatements) {
144   EXPECT_EQ("{\n  {\n    {}\n  }\n}", format("{{{}}}"));
145 }
146 
147 TEST_F(FormatTest, FormatsNestedCall) {
148   verifyFormat("Method(f1, f2(f3));");
149   verifyFormat("Method(f1(f2, f3()));");
150   verifyFormat("Method(f1(f2, (f3())));");
151 }
152 
153 TEST_F(FormatTest, NestedNameSpecifiers) {
154   verifyFormat("vector<::Type> v;");
155   verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())");
156   verifyFormat("static constexpr bool Bar = decltype(bar())::value;");
157   verifyFormat("bool a = 2 < ::SomeFunction();");
158   verifyFormat("ALWAYS_INLINE ::std::string getName();");
159   verifyFormat("some::string getName();");
160 }
161 
162 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) {
163   EXPECT_EQ("if (a) {\n"
164             "  f();\n"
165             "}",
166             format("if(a){f();}"));
167   EXPECT_EQ(4, ReplacementCount);
168   EXPECT_EQ("if (a) {\n"
169             "  f();\n"
170             "}",
171             format("if (a) {\n"
172                    "  f();\n"
173                    "}"));
174   EXPECT_EQ(0, ReplacementCount);
175   EXPECT_EQ("/*\r\n"
176             "\r\n"
177             "*/\r\n",
178             format("/*\r\n"
179                    "\r\n"
180                    "*/\r\n"));
181   EXPECT_EQ(0, ReplacementCount);
182 }
183 
184 TEST_F(FormatTest, RemovesEmptyLines) {
185   EXPECT_EQ("class C {\n"
186             "  int i;\n"
187             "};",
188             format("class C {\n"
189                    " int i;\n"
190                    "\n"
191                    "};"));
192 
193   // Don't remove empty lines at the start of namespaces or extern "C" blocks.
194   EXPECT_EQ("namespace N {\n"
195             "\n"
196             "int i;\n"
197             "}",
198             format("namespace N {\n"
199                    "\n"
200                    "int    i;\n"
201                    "}",
202                    getGoogleStyle()));
203   EXPECT_EQ("extern /**/ \"C\" /**/ {\n"
204             "\n"
205             "int i;\n"
206             "}",
207             format("extern /**/ \"C\" /**/ {\n"
208                    "\n"
209                    "int    i;\n"
210                    "}",
211                    getGoogleStyle()));
212 
213   // ...but do keep inlining and removing empty lines for non-block extern "C"
214   // functions.
215   verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle());
216   EXPECT_EQ("extern \"C\" int f() {\n"
217             "  int i = 42;\n"
218             "  return i;\n"
219             "}",
220             format("extern \"C\" int f() {\n"
221                    "\n"
222                    "  int i = 42;\n"
223                    "  return i;\n"
224                    "}",
225                    getGoogleStyle()));
226 
227   // Remove empty lines at the beginning and end of blocks.
228   EXPECT_EQ("void f() {\n"
229             "\n"
230             "  if (a) {\n"
231             "\n"
232             "    f();\n"
233             "  }\n"
234             "}",
235             format("void f() {\n"
236                    "\n"
237                    "  if (a) {\n"
238                    "\n"
239                    "    f();\n"
240                    "\n"
241                    "  }\n"
242                    "\n"
243                    "}",
244                    getLLVMStyle()));
245   EXPECT_EQ("void f() {\n"
246             "  if (a) {\n"
247             "    f();\n"
248             "  }\n"
249             "}",
250             format("void f() {\n"
251                    "\n"
252                    "  if (a) {\n"
253                    "\n"
254                    "    f();\n"
255                    "\n"
256                    "  }\n"
257                    "\n"
258                    "}",
259                    getGoogleStyle()));
260 
261   // Don't remove empty lines in more complex control statements.
262   EXPECT_EQ("void f() {\n"
263             "  if (a) {\n"
264             "    f();\n"
265             "\n"
266             "  } else if (b) {\n"
267             "    f();\n"
268             "  }\n"
269             "}",
270             format("void f() {\n"
271                    "  if (a) {\n"
272                    "    f();\n"
273                    "\n"
274                    "  } else if (b) {\n"
275                    "    f();\n"
276                    "\n"
277                    "  }\n"
278                    "\n"
279                    "}"));
280 
281   // FIXME: This is slightly inconsistent.
282   FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
283   LLVMWithNoNamespaceFix.FixNamespaceComments = false;
284   EXPECT_EQ("namespace {\n"
285             "int i;\n"
286             "}",
287             format("namespace {\n"
288                    "int i;\n"
289                    "\n"
290                    "}", LLVMWithNoNamespaceFix));
291   EXPECT_EQ("namespace {\n"
292             "int i;\n"
293             "}",
294             format("namespace {\n"
295                    "int i;\n"
296                    "\n"
297                    "}"));
298   EXPECT_EQ("namespace {\n"
299             "int i;\n"
300             "\n"
301             "} // namespace",
302             format("namespace {\n"
303                    "int i;\n"
304                    "\n"
305                    "}  // namespace"));
306 
307   FormatStyle Style = getLLVMStyle();
308   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
309   Style.MaxEmptyLinesToKeep = 2;
310   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
311   Style.BraceWrapping.AfterClass = true;
312   Style.BraceWrapping.AfterFunction = true;
313   Style.KeepEmptyLinesAtTheStartOfBlocks = false;
314 
315   EXPECT_EQ("class Foo\n"
316             "{\n"
317             "  Foo() {}\n"
318             "\n"
319             "  void funk() {}\n"
320             "};",
321             format("class Foo\n"
322                    "{\n"
323                    "  Foo()\n"
324                    "  {\n"
325                    "  }\n"
326                    "\n"
327                    "  void funk() {}\n"
328                    "};",
329                    Style));
330 }
331 
332 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) {
333   verifyFormat("x = (a) and (b);");
334   verifyFormat("x = (a) or (b);");
335   verifyFormat("x = (a) bitand (b);");
336   verifyFormat("x = (a) bitor (b);");
337   verifyFormat("x = (a) not_eq (b);");
338   verifyFormat("x = (a) and_eq (b);");
339   verifyFormat("x = (a) or_eq (b);");
340   verifyFormat("x = (a) xor (b);");
341 }
342 
343 TEST_F(FormatTest, RecognizesUnaryOperatorKeywords) {
344   verifyFormat("x = compl(a);");
345   verifyFormat("x = not(a);");
346   verifyFormat("x = bitand(a);");
347   // Unary operator must not be merged with the next identifier
348   verifyFormat("x = compl a;");
349   verifyFormat("x = not a;");
350   verifyFormat("x = bitand a;");
351 }
352 
353 //===----------------------------------------------------------------------===//
354 // Tests for control statements.
355 //===----------------------------------------------------------------------===//
356 
357 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
358   verifyFormat("if (true)\n  f();\ng();");
359   verifyFormat("if (a)\n  if (b)\n    if (c)\n      g();\nh();");
360   verifyFormat("if (a)\n  if (b) {\n    f();\n  }\ng();");
361   verifyFormat("if constexpr (true)\n"
362                "  f();\ng();");
363   verifyFormat("if constexpr (a)\n"
364                "  if constexpr (b)\n"
365                "    if constexpr (c)\n"
366                "      g();\n"
367                "h();");
368   verifyFormat("if constexpr (a)\n"
369                "  if constexpr (b) {\n"
370                "    f();\n"
371                "  }\n"
372                "g();");
373 
374   FormatStyle AllowsMergedIf = getLLVMStyle();
375   AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left;
376   AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
377   verifyFormat("if (a)\n"
378                "  // comment\n"
379                "  f();",
380                AllowsMergedIf);
381   verifyFormat("{\n"
382                "  if (a)\n"
383                "  label:\n"
384                "    f();\n"
385                "}",
386                AllowsMergedIf);
387   verifyFormat("#define A \\\n"
388                "  if (a)  \\\n"
389                "  label:  \\\n"
390                "    f()",
391                AllowsMergedIf);
392   verifyFormat("if (a)\n"
393                "  ;",
394                AllowsMergedIf);
395   verifyFormat("if (a)\n"
396                "  if (b) return;",
397                AllowsMergedIf);
398 
399   verifyFormat("if (a) // Can't merge this\n"
400                "  f();\n",
401                AllowsMergedIf);
402   verifyFormat("if (a) /* still don't merge */\n"
403                "  f();",
404                AllowsMergedIf);
405   verifyFormat("if (a) { // Never merge this\n"
406                "  f();\n"
407                "}",
408                AllowsMergedIf);
409   verifyFormat("if (a) { /* Never merge this */\n"
410                "  f();\n"
411                "}",
412                AllowsMergedIf);
413 
414   AllowsMergedIf.ColumnLimit = 14;
415   verifyFormat("if (a) return;", AllowsMergedIf);
416   verifyFormat("if (aaaaaaaaa)\n"
417                "  return;",
418                AllowsMergedIf);
419 
420   AllowsMergedIf.ColumnLimit = 13;
421   verifyFormat("if (a)\n  return;", AllowsMergedIf);
422 }
423 
424 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
425   FormatStyle AllowsMergedLoops = getLLVMStyle();
426   AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
427   verifyFormat("while (true) continue;", AllowsMergedLoops);
428   verifyFormat("for (;;) continue;", AllowsMergedLoops);
429   verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
430   verifyFormat("while (true)\n"
431                "  ;",
432                AllowsMergedLoops);
433   verifyFormat("for (;;)\n"
434                "  ;",
435                AllowsMergedLoops);
436   verifyFormat("for (;;)\n"
437                "  for (;;) continue;",
438                AllowsMergedLoops);
439   verifyFormat("for (;;) // Can't merge this\n"
440                "  continue;",
441                AllowsMergedLoops);
442   verifyFormat("for (;;) /* still don't merge */\n"
443                "  continue;",
444                AllowsMergedLoops);
445 }
446 
447 TEST_F(FormatTest, FormatShortBracedStatements) {
448   FormatStyle AllowSimpleBracedStatements = getLLVMStyle();
449   AllowSimpleBracedStatements.ColumnLimit = 40;
450   AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true;
451 
452   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true;
453   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
454 
455   AllowSimpleBracedStatements.BreakBeforeBraces = FormatStyle::BS_Custom;
456   AllowSimpleBracedStatements.BraceWrapping.AfterFunction = true;
457   AllowSimpleBracedStatements.BraceWrapping.SplitEmptyRecord = false;
458 
459   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
460   verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
461   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
462   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
463   verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
464   verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements);
465   verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
466   verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
467   verifyFormat("if (true) {\n"
468                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
469                "}",
470                AllowSimpleBracedStatements);
471   verifyFormat("if (true) { //\n"
472                "  f();\n"
473                "}",
474                AllowSimpleBracedStatements);
475   verifyFormat("if (true) {\n"
476                "  f();\n"
477                "  f();\n"
478                "}",
479                AllowSimpleBracedStatements);
480   verifyFormat("if (true) {\n"
481                "  f();\n"
482                "} else {\n"
483                "  f();\n"
484                "}",
485                AllowSimpleBracedStatements);
486 
487   verifyFormat("struct A2 {\n"
488                "  int X;\n"
489                "};",
490                AllowSimpleBracedStatements);
491   verifyFormat("typedef struct A2 {\n"
492                "  int X;\n"
493                "} A2_t;",
494                AllowSimpleBracedStatements);
495   verifyFormat("template <int> struct A2 {\n"
496                "  struct B {};\n"
497                "};",
498                AllowSimpleBracedStatements);
499 
500   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false;
501   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
502   verifyFormat("if (true) {\n"
503                "  f();\n"
504                "}",
505                AllowSimpleBracedStatements);
506   verifyFormat("if (true) {\n"
507                "  f();\n"
508                "} else {\n"
509                "  f();\n"
510                "}",
511                AllowSimpleBracedStatements);
512 
513   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
514   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
515   verifyFormat("while (true) {\n"
516                "  f();\n"
517                "}",
518                AllowSimpleBracedStatements);
519   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
520   verifyFormat("for (;;) {\n"
521                "  f();\n"
522                "}",
523                AllowSimpleBracedStatements);
524 
525   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true;
526   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
527   AllowSimpleBracedStatements.BraceWrapping.AfterControlStatement = true;
528 
529   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
530   verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
531   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
532   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
533   verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
534   verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements);
535   verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
536   verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
537   verifyFormat("if (true)\n"
538                "{\n"
539                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
540                "}",
541                AllowSimpleBracedStatements);
542   verifyFormat("if (true)\n"
543                "{ //\n"
544                "  f();\n"
545                "}",
546                AllowSimpleBracedStatements);
547   verifyFormat("if (true)\n"
548                "{\n"
549                "  f();\n"
550                "  f();\n"
551                "}",
552                AllowSimpleBracedStatements);
553   verifyFormat("if (true)\n"
554                "{\n"
555                "  f();\n"
556                "} else\n"
557                "{\n"
558                "  f();\n"
559                "}",
560                AllowSimpleBracedStatements);
561 
562   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false;
563   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
564   verifyFormat("if (true)\n"
565                "{\n"
566                "  f();\n"
567                "}",
568                AllowSimpleBracedStatements);
569   verifyFormat("if (true)\n"
570                "{\n"
571                "  f();\n"
572                "} else\n"
573                "{\n"
574                "  f();\n"
575                "}",
576                AllowSimpleBracedStatements);
577 
578   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
579   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
580   verifyFormat("while (true)\n"
581                "{\n"
582                "  f();\n"
583                "}",
584                AllowSimpleBracedStatements);
585   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
586   verifyFormat("for (;;)\n"
587                "{\n"
588                "  f();\n"
589                "}",
590                AllowSimpleBracedStatements);
591 }
592 
593 TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) {
594   FormatStyle Style = getLLVMStyleWithColumns(60);
595   Style.AllowShortBlocksOnASingleLine = true;
596   Style.AllowShortIfStatementsOnASingleLine = true;
597   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
598   EXPECT_EQ("#define A                                                  \\\n"
599             "  if (HANDLEwernufrnuLwrmviferuvnierv)                     \\\n"
600             "  { RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; }\n"
601             "X;",
602             format("#define A \\\n"
603                    "   if (HANDLEwernufrnuLwrmviferuvnierv) { \\\n"
604                    "      RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n"
605                    "   }\n"
606                    "X;",
607                    Style));
608 }
609 
610 TEST_F(FormatTest, ParseIfElse) {
611   verifyFormat("if (true)\n"
612                "  if (true)\n"
613                "    if (true)\n"
614                "      f();\n"
615                "    else\n"
616                "      g();\n"
617                "  else\n"
618                "    h();\n"
619                "else\n"
620                "  i();");
621   verifyFormat("if (true)\n"
622                "  if (true)\n"
623                "    if (true) {\n"
624                "      if (true)\n"
625                "        f();\n"
626                "    } else {\n"
627                "      g();\n"
628                "    }\n"
629                "  else\n"
630                "    h();\n"
631                "else {\n"
632                "  i();\n"
633                "}");
634   verifyFormat("if (true)\n"
635                "  if constexpr (true)\n"
636                "    if (true) {\n"
637                "      if constexpr (true)\n"
638                "        f();\n"
639                "    } else {\n"
640                "      g();\n"
641                "    }\n"
642                "  else\n"
643                "    h();\n"
644                "else {\n"
645                "  i();\n"
646                "}");
647   verifyFormat("void f() {\n"
648                "  if (a) {\n"
649                "  } else {\n"
650                "  }\n"
651                "}");
652 }
653 
654 TEST_F(FormatTest, ElseIf) {
655   verifyFormat("if (a) {\n} else if (b) {\n}");
656   verifyFormat("if (a)\n"
657                "  f();\n"
658                "else if (b)\n"
659                "  g();\n"
660                "else\n"
661                "  h();");
662   verifyFormat("if constexpr (a)\n"
663                "  f();\n"
664                "else if constexpr (b)\n"
665                "  g();\n"
666                "else\n"
667                "  h();");
668   verifyFormat("if (a) {\n"
669                "  f();\n"
670                "}\n"
671                "// or else ..\n"
672                "else {\n"
673                "  g()\n"
674                "}");
675 
676   verifyFormat("if (a) {\n"
677                "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
678                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
679                "}");
680   verifyFormat("if (a) {\n"
681                "} else if (\n"
682                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
683                "}",
684                getLLVMStyleWithColumns(62));
685   verifyFormat("if (a) {\n"
686                "} else if constexpr (\n"
687                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
688                "}",
689                getLLVMStyleWithColumns(62));
690 }
691 
692 TEST_F(FormatTest, FormatsForLoop) {
693   verifyFormat(
694       "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
695       "     ++VeryVeryLongLoopVariable)\n"
696       "  ;");
697   verifyFormat("for (;;)\n"
698                "  f();");
699   verifyFormat("for (;;) {\n}");
700   verifyFormat("for (;;) {\n"
701                "  f();\n"
702                "}");
703   verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
704 
705   verifyFormat(
706       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
707       "                                          E = UnwrappedLines.end();\n"
708       "     I != E; ++I) {\n}");
709 
710   verifyFormat(
711       "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
712       "     ++IIIII) {\n}");
713   verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
714                "         aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
715                "     aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
716   verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
717                "         I = FD->getDeclsInPrototypeScope().begin(),\n"
718                "         E = FD->getDeclsInPrototypeScope().end();\n"
719                "     I != E; ++I) {\n}");
720   verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n"
721                "         I = Container.begin(),\n"
722                "         E = Container.end();\n"
723                "     I != E; ++I) {\n}",
724                getLLVMStyleWithColumns(76));
725 
726   verifyFormat(
727       "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
728       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
729       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
730       "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
731       "     ++aaaaaaaaaaa) {\n}");
732   verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
733                "                bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n"
734                "     ++i) {\n}");
735   verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
736                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
737                "}");
738   verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
739                "         aaaaaaaaaa);\n"
740                "     iter; ++iter) {\n"
741                "}");
742   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
743                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
744                "     aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n"
745                "     ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {");
746 
747   // These should not be formatted as Objective-C for-in loops.
748   verifyFormat("for (Foo *x = 0; x != in; x++) {\n}");
749   verifyFormat("Foo *x;\nfor (x = 0; x != in; x++) {\n}");
750   verifyFormat("Foo *x;\nfor (x in y) {\n}");
751   verifyFormat("for (const Foo<Bar> &baz = in.value(); !baz.at_end(); ++baz) {\n}");
752 
753   FormatStyle NoBinPacking = getLLVMStyle();
754   NoBinPacking.BinPackParameters = false;
755   verifyFormat("for (int aaaaaaaaaaa = 1;\n"
756                "     aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
757                "                                           aaaaaaaaaaaaaaaa,\n"
758                "                                           aaaaaaaaaaaaaaaa,\n"
759                "                                           aaaaaaaaaaaaaaaa);\n"
760                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
761                "}",
762                NoBinPacking);
763   verifyFormat(
764       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
765       "                                          E = UnwrappedLines.end();\n"
766       "     I != E;\n"
767       "     ++I) {\n}",
768       NoBinPacking);
769 
770   FormatStyle AlignLeft = getLLVMStyle();
771   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
772   verifyFormat("for (A* a = start; a < end; ++a, ++value) {\n}", AlignLeft);
773 }
774 
775 TEST_F(FormatTest, RangeBasedForLoops) {
776   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
777                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
778   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
779                "     aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
780   verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
781                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
782   verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
783                "     aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
784 }
785 
786 TEST_F(FormatTest, ForEachLoops) {
787   verifyFormat("void f() {\n"
788                "  foreach (Item *item, itemlist) {}\n"
789                "  Q_FOREACH (Item *item, itemlist) {}\n"
790                "  BOOST_FOREACH (Item *item, itemlist) {}\n"
791                "  UNKNOWN_FORACH(Item * item, itemlist) {}\n"
792                "}");
793 
794   // As function-like macros.
795   verifyFormat("#define foreach(x, y)\n"
796                "#define Q_FOREACH(x, y)\n"
797                "#define BOOST_FOREACH(x, y)\n"
798                "#define UNKNOWN_FOREACH(x, y)\n");
799 
800   // Not as function-like macros.
801   verifyFormat("#define foreach (x, y)\n"
802                "#define Q_FOREACH (x, y)\n"
803                "#define BOOST_FOREACH (x, y)\n"
804                "#define UNKNOWN_FOREACH (x, y)\n");
805 }
806 
807 TEST_F(FormatTest, FormatsWhileLoop) {
808   verifyFormat("while (true) {\n}");
809   verifyFormat("while (true)\n"
810                "  f();");
811   verifyFormat("while () {\n}");
812   verifyFormat("while () {\n"
813                "  f();\n"
814                "}");
815 }
816 
817 TEST_F(FormatTest, FormatsDoWhile) {
818   verifyFormat("do {\n"
819                "  do_something();\n"
820                "} while (something());");
821   verifyFormat("do\n"
822                "  do_something();\n"
823                "while (something());");
824 }
825 
826 TEST_F(FormatTest, FormatsSwitchStatement) {
827   verifyFormat("switch (x) {\n"
828                "case 1:\n"
829                "  f();\n"
830                "  break;\n"
831                "case kFoo:\n"
832                "case ns::kBar:\n"
833                "case kBaz:\n"
834                "  break;\n"
835                "default:\n"
836                "  g();\n"
837                "  break;\n"
838                "}");
839   verifyFormat("switch (x) {\n"
840                "case 1: {\n"
841                "  f();\n"
842                "  break;\n"
843                "}\n"
844                "case 2: {\n"
845                "  break;\n"
846                "}\n"
847                "}");
848   verifyFormat("switch (x) {\n"
849                "case 1: {\n"
850                "  f();\n"
851                "  {\n"
852                "    g();\n"
853                "    h();\n"
854                "  }\n"
855                "  break;\n"
856                "}\n"
857                "}");
858   verifyFormat("switch (x) {\n"
859                "case 1: {\n"
860                "  f();\n"
861                "  if (foo) {\n"
862                "    g();\n"
863                "    h();\n"
864                "  }\n"
865                "  break;\n"
866                "}\n"
867                "}");
868   verifyFormat("switch (x) {\n"
869                "case 1: {\n"
870                "  f();\n"
871                "  g();\n"
872                "} break;\n"
873                "}");
874   verifyFormat("switch (test)\n"
875                "  ;");
876   verifyFormat("switch (x) {\n"
877                "default: {\n"
878                "  // Do nothing.\n"
879                "}\n"
880                "}");
881   verifyFormat("switch (x) {\n"
882                "// comment\n"
883                "// if 1, do f()\n"
884                "case 1:\n"
885                "  f();\n"
886                "}");
887   verifyFormat("switch (x) {\n"
888                "case 1:\n"
889                "  // Do amazing stuff\n"
890                "  {\n"
891                "    f();\n"
892                "    g();\n"
893                "  }\n"
894                "  break;\n"
895                "}");
896   verifyFormat("#define A          \\\n"
897                "  switch (x) {     \\\n"
898                "  case a:          \\\n"
899                "    foo = b;       \\\n"
900                "  }",
901                getLLVMStyleWithColumns(20));
902   verifyFormat("#define OPERATION_CASE(name)           \\\n"
903                "  case OP_name:                        \\\n"
904                "    return operations::Operation##name\n",
905                getLLVMStyleWithColumns(40));
906   verifyFormat("switch (x) {\n"
907                "case 1:;\n"
908                "default:;\n"
909                "  int i;\n"
910                "}");
911 
912   verifyGoogleFormat("switch (x) {\n"
913                      "  case 1:\n"
914                      "    f();\n"
915                      "    break;\n"
916                      "  case kFoo:\n"
917                      "  case ns::kBar:\n"
918                      "  case kBaz:\n"
919                      "    break;\n"
920                      "  default:\n"
921                      "    g();\n"
922                      "    break;\n"
923                      "}");
924   verifyGoogleFormat("switch (x) {\n"
925                      "  case 1: {\n"
926                      "    f();\n"
927                      "    break;\n"
928                      "  }\n"
929                      "}");
930   verifyGoogleFormat("switch (test)\n"
931                      "  ;");
932 
933   verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
934                      "  case OP_name:              \\\n"
935                      "    return operations::Operation##name\n");
936   verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
937                      "  // Get the correction operation class.\n"
938                      "  switch (OpCode) {\n"
939                      "    CASE(Add);\n"
940                      "    CASE(Subtract);\n"
941                      "    default:\n"
942                      "      return operations::Unknown;\n"
943                      "  }\n"
944                      "#undef OPERATION_CASE\n"
945                      "}");
946   verifyFormat("DEBUG({\n"
947                "  switch (x) {\n"
948                "  case A:\n"
949                "    f();\n"
950                "    break;\n"
951                "    // fallthrough\n"
952                "  case B:\n"
953                "    g();\n"
954                "    break;\n"
955                "  }\n"
956                "});");
957   EXPECT_EQ("DEBUG({\n"
958             "  switch (x) {\n"
959             "  case A:\n"
960             "    f();\n"
961             "    break;\n"
962             "  // On B:\n"
963             "  case B:\n"
964             "    g();\n"
965             "    break;\n"
966             "  }\n"
967             "});",
968             format("DEBUG({\n"
969                    "  switch (x) {\n"
970                    "  case A:\n"
971                    "    f();\n"
972                    "    break;\n"
973                    "  // On B:\n"
974                    "  case B:\n"
975                    "    g();\n"
976                    "    break;\n"
977                    "  }\n"
978                    "});",
979                    getLLVMStyle()));
980   verifyFormat("switch (a) {\n"
981                "case (b):\n"
982                "  return;\n"
983                "}");
984 
985   verifyFormat("switch (a) {\n"
986                "case some_namespace::\n"
987                "    some_constant:\n"
988                "  return;\n"
989                "}",
990                getLLVMStyleWithColumns(34));
991 }
992 
993 TEST_F(FormatTest, CaseRanges) {
994   verifyFormat("switch (x) {\n"
995                "case 'A' ... 'Z':\n"
996                "case 1 ... 5:\n"
997                "case a ... b:\n"
998                "  break;\n"
999                "}");
1000 }
1001 
1002 TEST_F(FormatTest, ShortCaseLabels) {
1003   FormatStyle Style = getLLVMStyle();
1004   Style.AllowShortCaseLabelsOnASingleLine = true;
1005   verifyFormat("switch (a) {\n"
1006                "case 1: x = 1; break;\n"
1007                "case 2: return;\n"
1008                "case 3:\n"
1009                "case 4:\n"
1010                "case 5: return;\n"
1011                "case 6: // comment\n"
1012                "  return;\n"
1013                "case 7:\n"
1014                "  // comment\n"
1015                "  return;\n"
1016                "case 8:\n"
1017                "  x = 8; // comment\n"
1018                "  break;\n"
1019                "default: y = 1; break;\n"
1020                "}",
1021                Style);
1022   verifyFormat("switch (a) {\n"
1023                "case 0: return; // comment\n"
1024                "case 1: break;  // comment\n"
1025                "case 2: return;\n"
1026                "// comment\n"
1027                "case 3: return;\n"
1028                "// comment 1\n"
1029                "// comment 2\n"
1030                "// comment 3\n"
1031                "case 4: break; /* comment */\n"
1032                "case 5:\n"
1033                "  // comment\n"
1034                "  break;\n"
1035                "case 6: /* comment */ x = 1; break;\n"
1036                "case 7: x = /* comment */ 1; break;\n"
1037                "case 8:\n"
1038                "  x = 1; /* comment */\n"
1039                "  break;\n"
1040                "case 9:\n"
1041                "  break; // comment line 1\n"
1042                "         // comment line 2\n"
1043                "}",
1044                Style);
1045   EXPECT_EQ("switch (a) {\n"
1046             "case 1:\n"
1047             "  x = 8;\n"
1048             "  // fall through\n"
1049             "case 2: x = 8;\n"
1050             "// comment\n"
1051             "case 3:\n"
1052             "  return; /* comment line 1\n"
1053             "           * comment line 2 */\n"
1054             "case 4: i = 8;\n"
1055             "// something else\n"
1056             "#if FOO\n"
1057             "case 5: break;\n"
1058             "#endif\n"
1059             "}",
1060             format("switch (a) {\n"
1061                    "case 1: x = 8;\n"
1062                    "  // fall through\n"
1063                    "case 2:\n"
1064                    "  x = 8;\n"
1065                    "// comment\n"
1066                    "case 3:\n"
1067                    "  return; /* comment line 1\n"
1068                    "           * comment line 2 */\n"
1069                    "case 4:\n"
1070                    "  i = 8;\n"
1071                    "// something else\n"
1072                    "#if FOO\n"
1073                    "case 5: break;\n"
1074                    "#endif\n"
1075                    "}",
1076                    Style));
1077   EXPECT_EQ("switch (a) {\n" "case 0:\n"
1078             "  return; // long long long long long long long long long long long long comment\n"
1079             "          // line\n" "}",
1080             format("switch (a) {\n"
1081                    "case 0: return; // long long long long long long long long long long long long comment line\n"
1082                    "}",
1083                    Style));
1084   EXPECT_EQ("switch (a) {\n"
1085             "case 0:\n"
1086             "  return; /* long long long long long long long long long long long long comment\n"
1087             "             line */\n"
1088             "}",
1089             format("switch (a) {\n"
1090                    "case 0: return; /* long long long long long long long long long long long long comment line */\n"
1091                    "}",
1092                    Style));
1093   verifyFormat("switch (a) {\n"
1094                "#if FOO\n"
1095                "case 0: return 0;\n"
1096                "#endif\n"
1097                "}",
1098                Style);
1099   verifyFormat("switch (a) {\n"
1100                "case 1: {\n"
1101                "}\n"
1102                "case 2: {\n"
1103                "  return;\n"
1104                "}\n"
1105                "case 3: {\n"
1106                "  x = 1;\n"
1107                "  return;\n"
1108                "}\n"
1109                "case 4:\n"
1110                "  if (x)\n"
1111                "    return;\n"
1112                "}",
1113                Style);
1114   Style.ColumnLimit = 21;
1115   verifyFormat("switch (a) {\n"
1116                "case 1: x = 1; break;\n"
1117                "case 2: return;\n"
1118                "case 3:\n"
1119                "case 4:\n"
1120                "case 5: return;\n"
1121                "default:\n"
1122                "  y = 1;\n"
1123                "  break;\n"
1124                "}",
1125                Style);
1126 }
1127 
1128 TEST_F(FormatTest, FormatsLabels) {
1129   verifyFormat("void f() {\n"
1130                "  some_code();\n"
1131                "test_label:\n"
1132                "  some_other_code();\n"
1133                "  {\n"
1134                "    some_more_code();\n"
1135                "  another_label:\n"
1136                "    some_more_code();\n"
1137                "  }\n"
1138                "}");
1139   verifyFormat("{\n"
1140                "  some_code();\n"
1141                "test_label:\n"
1142                "  some_other_code();\n"
1143                "}");
1144   verifyFormat("{\n"
1145                "  some_code();\n"
1146                "test_label:;\n"
1147                "  int i = 0;\n"
1148                "}");
1149 }
1150 
1151 //===----------------------------------------------------------------------===//
1152 // Tests for classes, namespaces, etc.
1153 //===----------------------------------------------------------------------===//
1154 
1155 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
1156   verifyFormat("class A {};");
1157 }
1158 
1159 TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
1160   verifyFormat("class A {\n"
1161                "public:\n"
1162                "public: // comment\n"
1163                "protected:\n"
1164                "private:\n"
1165                "  void f() {}\n"
1166                "};");
1167   verifyGoogleFormat("class A {\n"
1168                      " public:\n"
1169                      " protected:\n"
1170                      " private:\n"
1171                      "  void f() {}\n"
1172                      "};");
1173   verifyFormat("class A {\n"
1174                "public slots:\n"
1175                "  void f1() {}\n"
1176                "public Q_SLOTS:\n"
1177                "  void f2() {}\n"
1178                "protected slots:\n"
1179                "  void f3() {}\n"
1180                "protected Q_SLOTS:\n"
1181                "  void f4() {}\n"
1182                "private slots:\n"
1183                "  void f5() {}\n"
1184                "private Q_SLOTS:\n"
1185                "  void f6() {}\n"
1186                "signals:\n"
1187                "  void g1();\n"
1188                "Q_SIGNALS:\n"
1189                "  void g2();\n"
1190                "};");
1191 
1192   // Don't interpret 'signals' the wrong way.
1193   verifyFormat("signals.set();");
1194   verifyFormat("for (Signals signals : f()) {\n}");
1195   verifyFormat("{\n"
1196                "  signals.set(); // This needs indentation.\n"
1197                "}");
1198   verifyFormat("void f() {\n"
1199                "label:\n"
1200                "  signals.baz();\n"
1201                "}");
1202 }
1203 
1204 TEST_F(FormatTest, SeparatesLogicalBlocks) {
1205   EXPECT_EQ("class A {\n"
1206             "public:\n"
1207             "  void f();\n"
1208             "\n"
1209             "private:\n"
1210             "  void g() {}\n"
1211             "  // test\n"
1212             "protected:\n"
1213             "  int h;\n"
1214             "};",
1215             format("class A {\n"
1216                    "public:\n"
1217                    "void f();\n"
1218                    "private:\n"
1219                    "void g() {}\n"
1220                    "// test\n"
1221                    "protected:\n"
1222                    "int h;\n"
1223                    "};"));
1224   EXPECT_EQ("class A {\n"
1225             "protected:\n"
1226             "public:\n"
1227             "  void f();\n"
1228             "};",
1229             format("class A {\n"
1230                    "protected:\n"
1231                    "\n"
1232                    "public:\n"
1233                    "\n"
1234                    "  void f();\n"
1235                    "};"));
1236 
1237   // Even ensure proper spacing inside macros.
1238   EXPECT_EQ("#define B     \\\n"
1239             "  class A {   \\\n"
1240             "   protected: \\\n"
1241             "   public:    \\\n"
1242             "    void f(); \\\n"
1243             "  };",
1244             format("#define B     \\\n"
1245                    "  class A {   \\\n"
1246                    "   protected: \\\n"
1247                    "              \\\n"
1248                    "   public:    \\\n"
1249                    "              \\\n"
1250                    "    void f(); \\\n"
1251                    "  };",
1252                    getGoogleStyle()));
1253   // But don't remove empty lines after macros ending in access specifiers.
1254   EXPECT_EQ("#define A private:\n"
1255             "\n"
1256             "int i;",
1257             format("#define A         private:\n"
1258                    "\n"
1259                    "int              i;"));
1260 }
1261 
1262 TEST_F(FormatTest, FormatsClasses) {
1263   verifyFormat("class A : public B {};");
1264   verifyFormat("class A : public ::B {};");
1265 
1266   verifyFormat(
1267       "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1268       "                             public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
1269   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
1270                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1271                "      public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
1272   verifyFormat(
1273       "class A : public B, public C, public D, public E, public F {};");
1274   verifyFormat("class AAAAAAAAAAAA : public B,\n"
1275                "                     public C,\n"
1276                "                     public D,\n"
1277                "                     public E,\n"
1278                "                     public F,\n"
1279                "                     public G {};");
1280 
1281   verifyFormat("class\n"
1282                "    ReallyReallyLongClassName {\n"
1283                "  int i;\n"
1284                "};",
1285                getLLVMStyleWithColumns(32));
1286   verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
1287                "                           aaaaaaaaaaaaaaaa> {};");
1288   verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
1289                "    : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
1290                "                                 aaaaaaaaaaaaaaaaaaaaaa> {};");
1291   verifyFormat("template <class R, class C>\n"
1292                "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
1293                "    : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
1294   verifyFormat("class ::A::B {};");
1295 }
1296 
1297 TEST_F(FormatTest, BreakBeforeInheritanceComma) {
1298   FormatStyle StyleWithInheritanceBreak = getLLVMStyle();
1299   StyleWithInheritanceBreak.BreakBeforeInheritanceComma = true;
1300 
1301   verifyFormat("class MyClass : public X {};", StyleWithInheritanceBreak);
1302   verifyFormat("class MyClass\n"
1303                "    : public X\n"
1304                "    , public Y {};",
1305                StyleWithInheritanceBreak);
1306 }
1307 
1308 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
1309   verifyFormat("class A {\n} a, b;");
1310   verifyFormat("struct A {\n} a, b;");
1311   verifyFormat("union A {\n} a;");
1312 }
1313 
1314 TEST_F(FormatTest, FormatsEnum) {
1315   verifyFormat("enum {\n"
1316                "  Zero,\n"
1317                "  One = 1,\n"
1318                "  Two = One + 1,\n"
1319                "  Three = (One + Two),\n"
1320                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1321                "  Five = (One, Two, Three, Four, 5)\n"
1322                "};");
1323   verifyGoogleFormat("enum {\n"
1324                      "  Zero,\n"
1325                      "  One = 1,\n"
1326                      "  Two = One + 1,\n"
1327                      "  Three = (One + Two),\n"
1328                      "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1329                      "  Five = (One, Two, Three, Four, 5)\n"
1330                      "};");
1331   verifyFormat("enum Enum {};");
1332   verifyFormat("enum {};");
1333   verifyFormat("enum X E {} d;");
1334   verifyFormat("enum __attribute__((...)) E {} d;");
1335   verifyFormat("enum __declspec__((...)) E {} d;");
1336   verifyFormat("enum {\n"
1337                "  Bar = Foo<int, int>::value\n"
1338                "};",
1339                getLLVMStyleWithColumns(30));
1340 
1341   verifyFormat("enum ShortEnum { A, B, C };");
1342   verifyGoogleFormat("enum ShortEnum { A, B, C };");
1343 
1344   EXPECT_EQ("enum KeepEmptyLines {\n"
1345             "  ONE,\n"
1346             "\n"
1347             "  TWO,\n"
1348             "\n"
1349             "  THREE\n"
1350             "}",
1351             format("enum KeepEmptyLines {\n"
1352                    "  ONE,\n"
1353                    "\n"
1354                    "  TWO,\n"
1355                    "\n"
1356                    "\n"
1357                    "  THREE\n"
1358                    "}"));
1359   verifyFormat("enum E { // comment\n"
1360                "  ONE,\n"
1361                "  TWO\n"
1362                "};\n"
1363                "int i;");
1364   // Not enums.
1365   verifyFormat("enum X f() {\n"
1366                "  a();\n"
1367                "  return 42;\n"
1368                "}");
1369   verifyFormat("enum X Type::f() {\n"
1370                "  a();\n"
1371                "  return 42;\n"
1372                "}");
1373   verifyFormat("enum ::X f() {\n"
1374                "  a();\n"
1375                "  return 42;\n"
1376                "}");
1377   verifyFormat("enum ns::X f() {\n"
1378                "  a();\n"
1379                "  return 42;\n"
1380                "}");
1381 }
1382 
1383 TEST_F(FormatTest, FormatsEnumsWithErrors) {
1384   verifyFormat("enum Type {\n"
1385                "  One = 0; // These semicolons should be commas.\n"
1386                "  Two = 1;\n"
1387                "};");
1388   verifyFormat("namespace n {\n"
1389                "enum Type {\n"
1390                "  One,\n"
1391                "  Two, // missing };\n"
1392                "  int i;\n"
1393                "}\n"
1394                "void g() {}");
1395 }
1396 
1397 TEST_F(FormatTest, FormatsEnumStruct) {
1398   verifyFormat("enum struct {\n"
1399                "  Zero,\n"
1400                "  One = 1,\n"
1401                "  Two = One + 1,\n"
1402                "  Three = (One + Two),\n"
1403                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1404                "  Five = (One, Two, Three, Four, 5)\n"
1405                "};");
1406   verifyFormat("enum struct Enum {};");
1407   verifyFormat("enum struct {};");
1408   verifyFormat("enum struct X E {} d;");
1409   verifyFormat("enum struct __attribute__((...)) E {} d;");
1410   verifyFormat("enum struct __declspec__((...)) E {} d;");
1411   verifyFormat("enum struct X f() {\n  a();\n  return 42;\n}");
1412 }
1413 
1414 TEST_F(FormatTest, FormatsEnumClass) {
1415   verifyFormat("enum class {\n"
1416                "  Zero,\n"
1417                "  One = 1,\n"
1418                "  Two = One + 1,\n"
1419                "  Three = (One + Two),\n"
1420                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1421                "  Five = (One, Two, Three, Four, 5)\n"
1422                "};");
1423   verifyFormat("enum class Enum {};");
1424   verifyFormat("enum class {};");
1425   verifyFormat("enum class X E {} d;");
1426   verifyFormat("enum class __attribute__((...)) E {} d;");
1427   verifyFormat("enum class __declspec__((...)) E {} d;");
1428   verifyFormat("enum class X f() {\n  a();\n  return 42;\n}");
1429 }
1430 
1431 TEST_F(FormatTest, FormatsEnumTypes) {
1432   verifyFormat("enum X : int {\n"
1433                "  A, // Force multiple lines.\n"
1434                "  B\n"
1435                "};");
1436   verifyFormat("enum X : int { A, B };");
1437   verifyFormat("enum X : std::uint32_t { A, B };");
1438 }
1439 
1440 TEST_F(FormatTest, FormatsTypedefEnum) {
1441   FormatStyle Style = getLLVMStyle();
1442   Style.ColumnLimit = 40;
1443   verifyFormat("typedef enum {} EmptyEnum;");
1444   verifyFormat("typedef enum { A, B, C } ShortEnum;");
1445   verifyFormat("typedef enum {\n"
1446                "  ZERO = 0,\n"
1447                "  ONE = 1,\n"
1448                "  TWO = 2,\n"
1449                "  THREE = 3\n"
1450                "} LongEnum;",
1451                Style);
1452   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1453   Style.BraceWrapping.AfterEnum = true;
1454   verifyFormat("typedef enum {} EmptyEnum;");
1455   verifyFormat("typedef enum { A, B, C } ShortEnum;");
1456   verifyFormat("typedef enum\n"
1457                "{\n"
1458                "  ZERO = 0,\n"
1459                "  ONE = 1,\n"
1460                "  TWO = 2,\n"
1461                "  THREE = 3\n"
1462                "} LongEnum;",
1463                Style);
1464 }
1465 
1466 TEST_F(FormatTest, FormatsNSEnums) {
1467   verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
1468   verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
1469                      "  // Information about someDecentlyLongValue.\n"
1470                      "  someDecentlyLongValue,\n"
1471                      "  // Information about anotherDecentlyLongValue.\n"
1472                      "  anotherDecentlyLongValue,\n"
1473                      "  // Information about aThirdDecentlyLongValue.\n"
1474                      "  aThirdDecentlyLongValue\n"
1475                      "};");
1476   verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
1477                      "  a = 1,\n"
1478                      "  b = 2,\n"
1479                      "  c = 3,\n"
1480                      "};");
1481   verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
1482                      "  a = 1,\n"
1483                      "  b = 2,\n"
1484                      "  c = 3,\n"
1485                      "};");
1486   verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
1487                      "  a = 1,\n"
1488                      "  b = 2,\n"
1489                      "  c = 3,\n"
1490                      "};");
1491 }
1492 
1493 TEST_F(FormatTest, FormatsBitfields) {
1494   verifyFormat("struct Bitfields {\n"
1495                "  unsigned sClass : 8;\n"
1496                "  unsigned ValueKind : 2;\n"
1497                "};");
1498   verifyFormat("struct A {\n"
1499                "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
1500                "      bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
1501                "};");
1502   verifyFormat("struct MyStruct {\n"
1503                "  uchar data;\n"
1504                "  uchar : 8;\n"
1505                "  uchar : 8;\n"
1506                "  uchar other;\n"
1507                "};");
1508 }
1509 
1510 TEST_F(FormatTest, FormatsNamespaces) {
1511   FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
1512   LLVMWithNoNamespaceFix.FixNamespaceComments = false;
1513 
1514   verifyFormat("namespace some_namespace {\n"
1515                "class A {};\n"
1516                "void f() { f(); }\n"
1517                "}",
1518                LLVMWithNoNamespaceFix);
1519   verifyFormat("namespace {\n"
1520                "class A {};\n"
1521                "void f() { f(); }\n"
1522                "}",
1523                LLVMWithNoNamespaceFix);
1524   verifyFormat("inline namespace X {\n"
1525                "class A {};\n"
1526                "void f() { f(); }\n"
1527                "}",
1528                LLVMWithNoNamespaceFix);
1529   verifyFormat("using namespace some_namespace;\n"
1530                "class A {};\n"
1531                "void f() { f(); }",
1532                LLVMWithNoNamespaceFix);
1533 
1534   // This code is more common than we thought; if we
1535   // layout this correctly the semicolon will go into
1536   // its own line, which is undesirable.
1537   verifyFormat("namespace {};",
1538                LLVMWithNoNamespaceFix);
1539   verifyFormat("namespace {\n"
1540                "class A {};\n"
1541                "};",
1542                LLVMWithNoNamespaceFix);
1543 
1544   verifyFormat("namespace {\n"
1545                "int SomeVariable = 0; // comment\n"
1546                "} // namespace",
1547                LLVMWithNoNamespaceFix);
1548   EXPECT_EQ("#ifndef HEADER_GUARD\n"
1549             "#define HEADER_GUARD\n"
1550             "namespace my_namespace {\n"
1551             "int i;\n"
1552             "} // my_namespace\n"
1553             "#endif // HEADER_GUARD",
1554             format("#ifndef HEADER_GUARD\n"
1555                    " #define HEADER_GUARD\n"
1556                    "   namespace my_namespace {\n"
1557                    "int i;\n"
1558                    "}    // my_namespace\n"
1559                    "#endif    // HEADER_GUARD",
1560                    LLVMWithNoNamespaceFix));
1561 
1562   EXPECT_EQ("namespace A::B {\n"
1563             "class C {};\n"
1564             "}",
1565             format("namespace A::B {\n"
1566                    "class C {};\n"
1567                    "}",
1568                    LLVMWithNoNamespaceFix));
1569 
1570   FormatStyle Style = getLLVMStyle();
1571   Style.NamespaceIndentation = FormatStyle::NI_All;
1572   EXPECT_EQ("namespace out {\n"
1573             "  int i;\n"
1574             "  namespace in {\n"
1575             "    int i;\n"
1576             "  } // namespace in\n"
1577             "} // namespace out",
1578             format("namespace out {\n"
1579                    "int i;\n"
1580                    "namespace in {\n"
1581                    "int i;\n"
1582                    "} // namespace in\n"
1583                    "} // namespace out",
1584                    Style));
1585 
1586   Style.NamespaceIndentation = FormatStyle::NI_Inner;
1587   EXPECT_EQ("namespace out {\n"
1588             "int i;\n"
1589             "namespace in {\n"
1590             "  int i;\n"
1591             "} // namespace in\n"
1592             "} // namespace out",
1593             format("namespace out {\n"
1594                    "int i;\n"
1595                    "namespace in {\n"
1596                    "int i;\n"
1597                    "} // namespace in\n"
1598                    "} // namespace out",
1599                    Style));
1600 }
1601 
1602 TEST_F(FormatTest, FormatsCompactNamespaces) {
1603   FormatStyle Style = getLLVMStyle();
1604   Style.CompactNamespaces = true;
1605 
1606   verifyFormat("namespace A { namespace B {\n"
1607 			   "}} // namespace A::B",
1608 			   Style);
1609 
1610   EXPECT_EQ("namespace out { namespace in {\n"
1611             "}} // namespace out::in",
1612             format("namespace out {\n"
1613                    "namespace in {\n"
1614                    "} // namespace in\n"
1615                    "} // namespace out",
1616                    Style));
1617 
1618   // Only namespaces which have both consecutive opening and end get compacted
1619   EXPECT_EQ("namespace out {\n"
1620             "namespace in1 {\n"
1621             "} // namespace in1\n"
1622             "namespace in2 {\n"
1623             "} // namespace in2\n"
1624             "} // namespace out",
1625             format("namespace out {\n"
1626                    "namespace in1 {\n"
1627                    "} // namespace in1\n"
1628                    "namespace in2 {\n"
1629                    "} // namespace in2\n"
1630                    "} // namespace out",
1631                    Style));
1632 
1633   EXPECT_EQ("namespace out {\n"
1634             "int i;\n"
1635             "namespace in {\n"
1636             "int j;\n"
1637             "} // namespace in\n"
1638             "int k;\n"
1639             "} // namespace out",
1640             format("namespace out { int i;\n"
1641                    "namespace in { int j; } // namespace in\n"
1642                    "int k; } // namespace out",
1643                    Style));
1644 
1645   EXPECT_EQ("namespace A { namespace B { namespace C {\n"
1646             "}}} // namespace A::B::C\n",
1647             format("namespace A { namespace B {\n"
1648                    "namespace C {\n"
1649                    "}} // namespace B::C\n"
1650                    "} // namespace A\n",
1651                    Style));
1652 
1653   Style.ColumnLimit = 40;
1654   EXPECT_EQ("namespace aaaaaaaaaa {\n"
1655             "namespace bbbbbbbbbb {\n"
1656             "}} // namespace aaaaaaaaaa::bbbbbbbbbb",
1657             format("namespace aaaaaaaaaa {\n"
1658                    "namespace bbbbbbbbbb {\n"
1659                    "} // namespace bbbbbbbbbb\n"
1660                    "} // namespace aaaaaaaaaa",
1661                    Style));
1662 
1663   EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n"
1664             "namespace cccccc {\n"
1665             "}}} // namespace aaaaaa::bbbbbb::cccccc",
1666             format("namespace aaaaaa {\n"
1667                    "namespace bbbbbb {\n"
1668                    "namespace cccccc {\n"
1669                    "} // namespace cccccc\n"
1670                    "} // namespace bbbbbb\n"
1671                    "} // namespace aaaaaa",
1672                    Style));
1673   Style.ColumnLimit = 80;
1674 
1675   // Extra semicolon after 'inner' closing brace prevents merging
1676   EXPECT_EQ("namespace out { namespace in {\n"
1677             "}; } // namespace out::in",
1678             format("namespace out {\n"
1679                    "namespace in {\n"
1680                    "}; // namespace in\n"
1681                    "} // namespace out",
1682                    Style));
1683 
1684   // Extra semicolon after 'outer' closing brace is conserved
1685   EXPECT_EQ("namespace out { namespace in {\n"
1686             "}}; // namespace out::in",
1687             format("namespace out {\n"
1688                    "namespace in {\n"
1689                    "} // namespace in\n"
1690                    "}; // namespace out",
1691                    Style));
1692 
1693   Style.NamespaceIndentation = FormatStyle::NI_All;
1694   EXPECT_EQ("namespace out { namespace in {\n"
1695             "  int i;\n"
1696             "}} // namespace out::in",
1697             format("namespace out {\n"
1698                    "namespace in {\n"
1699                    "int i;\n"
1700                    "} // namespace in\n"
1701                    "} // namespace out",
1702                    Style));
1703   EXPECT_EQ("namespace out { namespace mid {\n"
1704             "  namespace in {\n"
1705             "    int j;\n"
1706             "  } // namespace in\n"
1707             "  int k;\n"
1708             "}} // namespace out::mid",
1709             format("namespace out { namespace mid {\n"
1710                    "namespace in { int j; } // namespace in\n"
1711                    "int k; }} // namespace out::mid",
1712                    Style));
1713 
1714   Style.NamespaceIndentation = FormatStyle::NI_Inner;
1715   EXPECT_EQ("namespace out { namespace in {\n"
1716             "  int i;\n"
1717             "}} // namespace out::in",
1718             format("namespace out {\n"
1719                    "namespace in {\n"
1720                    "int i;\n"
1721                    "} // namespace in\n"
1722                    "} // namespace out",
1723                    Style));
1724   EXPECT_EQ("namespace out { namespace mid { namespace in {\n"
1725             "  int i;\n"
1726             "}}} // namespace out::mid::in",
1727             format("namespace out {\n"
1728                    "namespace mid {\n"
1729                    "namespace in {\n"
1730                    "int i;\n"
1731                    "} // namespace in\n"
1732                    "} // namespace mid\n"
1733                    "} // namespace out",
1734                    Style));
1735 }
1736 
1737 TEST_F(FormatTest, FormatsExternC) {
1738   verifyFormat("extern \"C\" {\nint a;");
1739   verifyFormat("extern \"C\" {}");
1740   verifyFormat("extern \"C\" {\n"
1741                "int foo();\n"
1742                "}");
1743   verifyFormat("extern \"C\" int foo() {}");
1744   verifyFormat("extern \"C\" int foo();");
1745   verifyFormat("extern \"C\" int foo() {\n"
1746                "  int i = 42;\n"
1747                "  return i;\n"
1748                "}");
1749 
1750   FormatStyle Style = getLLVMStyle();
1751   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1752   Style.BraceWrapping.AfterFunction = true;
1753   verifyFormat("extern \"C\" int foo() {}", Style);
1754   verifyFormat("extern \"C\" int foo();", Style);
1755   verifyFormat("extern \"C\" int foo()\n"
1756                "{\n"
1757                "  int i = 42;\n"
1758                "  return i;\n"
1759                "}",
1760                Style);
1761 
1762   Style.BraceWrapping.AfterExternBlock = true;
1763   Style.BraceWrapping.SplitEmptyRecord = false;
1764   verifyFormat("extern \"C\"\n"
1765                "{}",
1766                Style);
1767   verifyFormat("extern \"C\"\n"
1768                "{\n"
1769                "  int foo();\n"
1770                "}",
1771                Style);
1772 }
1773 
1774 TEST_F(FormatTest, FormatsInlineASM) {
1775   verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
1776   verifyFormat("asm(\"nop\" ::: \"memory\");");
1777   verifyFormat(
1778       "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
1779       "    \"cpuid\\n\\t\"\n"
1780       "    \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
1781       "    : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
1782       "    : \"a\"(value));");
1783   EXPECT_EQ(
1784       "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
1785       "  __asm {\n"
1786       "        mov     edx,[that] // vtable in edx\n"
1787       "        mov     eax,methodIndex\n"
1788       "        call    [edx][eax*4] // stdcall\n"
1789       "  }\n"
1790       "}",
1791       format("void NS_InvokeByIndex(void *that,   unsigned int methodIndex) {\n"
1792              "    __asm {\n"
1793              "        mov     edx,[that] // vtable in edx\n"
1794              "        mov     eax,methodIndex\n"
1795              "        call    [edx][eax*4] // stdcall\n"
1796              "    }\n"
1797              "}"));
1798   EXPECT_EQ("_asm {\n"
1799             "  xor eax, eax;\n"
1800             "  cpuid;\n"
1801             "}",
1802             format("_asm {\n"
1803                    "  xor eax, eax;\n"
1804                    "  cpuid;\n"
1805                    "}"));
1806   verifyFormat("void function() {\n"
1807                "  // comment\n"
1808                "  asm(\"\");\n"
1809                "}");
1810   EXPECT_EQ("__asm {\n"
1811             "}\n"
1812             "int i;",
1813             format("__asm   {\n"
1814                    "}\n"
1815                    "int   i;"));
1816 }
1817 
1818 TEST_F(FormatTest, FormatTryCatch) {
1819   verifyFormat("try {\n"
1820                "  throw a * b;\n"
1821                "} catch (int a) {\n"
1822                "  // Do nothing.\n"
1823                "} catch (...) {\n"
1824                "  exit(42);\n"
1825                "}");
1826 
1827   // Function-level try statements.
1828   verifyFormat("int f() try { return 4; } catch (...) {\n"
1829                "  return 5;\n"
1830                "}");
1831   verifyFormat("class A {\n"
1832                "  int a;\n"
1833                "  A() try : a(0) {\n"
1834                "  } catch (...) {\n"
1835                "    throw;\n"
1836                "  }\n"
1837                "};\n");
1838 
1839   // Incomplete try-catch blocks.
1840   verifyIncompleteFormat("try {} catch (");
1841 }
1842 
1843 TEST_F(FormatTest, FormatSEHTryCatch) {
1844   verifyFormat("__try {\n"
1845                "  int a = b * c;\n"
1846                "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
1847                "  // Do nothing.\n"
1848                "}");
1849 
1850   verifyFormat("__try {\n"
1851                "  int a = b * c;\n"
1852                "} __finally {\n"
1853                "  // Do nothing.\n"
1854                "}");
1855 
1856   verifyFormat("DEBUG({\n"
1857                "  __try {\n"
1858                "  } __finally {\n"
1859                "  }\n"
1860                "});\n");
1861 }
1862 
1863 TEST_F(FormatTest, IncompleteTryCatchBlocks) {
1864   verifyFormat("try {\n"
1865                "  f();\n"
1866                "} catch {\n"
1867                "  g();\n"
1868                "}");
1869   verifyFormat("try {\n"
1870                "  f();\n"
1871                "} catch (A a) MACRO(x) {\n"
1872                "  g();\n"
1873                "} catch (B b) MACRO(x) {\n"
1874                "  g();\n"
1875                "}");
1876 }
1877 
1878 TEST_F(FormatTest, FormatTryCatchBraceStyles) {
1879   FormatStyle Style = getLLVMStyle();
1880   for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla,
1881                           FormatStyle::BS_WebKit}) {
1882     Style.BreakBeforeBraces = BraceStyle;
1883     verifyFormat("try {\n"
1884                  "  // something\n"
1885                  "} catch (...) {\n"
1886                  "  // something\n"
1887                  "}",
1888                  Style);
1889   }
1890   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
1891   verifyFormat("try {\n"
1892                "  // something\n"
1893                "}\n"
1894                "catch (...) {\n"
1895                "  // something\n"
1896                "}",
1897                Style);
1898   verifyFormat("__try {\n"
1899                "  // something\n"
1900                "}\n"
1901                "__finally {\n"
1902                "  // something\n"
1903                "}",
1904                Style);
1905   verifyFormat("@try {\n"
1906                "  // something\n"
1907                "}\n"
1908                "@finally {\n"
1909                "  // something\n"
1910                "}",
1911                Style);
1912   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
1913   verifyFormat("try\n"
1914                "{\n"
1915                "  // something\n"
1916                "}\n"
1917                "catch (...)\n"
1918                "{\n"
1919                "  // something\n"
1920                "}",
1921                Style);
1922   Style.BreakBeforeBraces = FormatStyle::BS_GNU;
1923   verifyFormat("try\n"
1924                "  {\n"
1925                "    // something\n"
1926                "  }\n"
1927                "catch (...)\n"
1928                "  {\n"
1929                "    // something\n"
1930                "  }",
1931                Style);
1932   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1933   Style.BraceWrapping.BeforeCatch = true;
1934   verifyFormat("try {\n"
1935                "  // something\n"
1936                "}\n"
1937                "catch (...) {\n"
1938                "  // something\n"
1939                "}",
1940                Style);
1941 }
1942 
1943 TEST_F(FormatTest, StaticInitializers) {
1944   verifyFormat("static SomeClass SC = {1, 'a'};");
1945 
1946   verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
1947                "    100000000, "
1948                "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
1949 
1950   // Here, everything other than the "}" would fit on a line.
1951   verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
1952                "    10000000000000000000000000};");
1953   EXPECT_EQ("S s = {a,\n"
1954             "\n"
1955             "       b};",
1956             format("S s = {\n"
1957                    "  a,\n"
1958                    "\n"
1959                    "  b\n"
1960                    "};"));
1961 
1962   // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
1963   // line. However, the formatting looks a bit off and this probably doesn't
1964   // happen often in practice.
1965   verifyFormat("static int Variable[1] = {\n"
1966                "    {1000000000000000000000000000000000000}};",
1967                getLLVMStyleWithColumns(40));
1968 }
1969 
1970 TEST_F(FormatTest, DesignatedInitializers) {
1971   verifyFormat("const struct A a = {.a = 1, .b = 2};");
1972   verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
1973                "                    .bbbbbbbbbb = 2,\n"
1974                "                    .cccccccccc = 3,\n"
1975                "                    .dddddddddd = 4,\n"
1976                "                    .eeeeeeeeee = 5};");
1977   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
1978                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
1979                "    .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
1980                "    .ccccccccccccccccccccccccccc = 3,\n"
1981                "    .ddddddddddddddddddddddddddd = 4,\n"
1982                "    .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
1983 
1984   verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
1985 
1986   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
1987   verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n"
1988                "                    [2] = bbbbbbbbbb,\n"
1989                "                    [3] = cccccccccc,\n"
1990                "                    [4] = dddddddddd,\n"
1991                "                    [5] = eeeeeeeeee};");
1992   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
1993                "    [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1994                "    [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
1995                "    [3] = cccccccccccccccccccccccccccccccccccccc,\n"
1996                "    [4] = dddddddddddddddddddddddddddddddddddddd,\n"
1997                "    [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};");
1998 }
1999 
2000 TEST_F(FormatTest, NestedStaticInitializers) {
2001   verifyFormat("static A x = {{{}}};\n");
2002   verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
2003                "               {init1, init2, init3, init4}}};",
2004                getLLVMStyleWithColumns(50));
2005 
2006   verifyFormat("somes Status::global_reps[3] = {\n"
2007                "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
2008                "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
2009                "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
2010                getLLVMStyleWithColumns(60));
2011   verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
2012                      "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
2013                      "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
2014                      "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
2015   verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
2016                "                  {rect.fRight - rect.fLeft, rect.fBottom - "
2017                "rect.fTop}};");
2018 
2019   verifyFormat(
2020       "SomeArrayOfSomeType a = {\n"
2021       "    {{1, 2, 3},\n"
2022       "     {1, 2, 3},\n"
2023       "     {111111111111111111111111111111, 222222222222222222222222222222,\n"
2024       "      333333333333333333333333333333},\n"
2025       "     {1, 2, 3},\n"
2026       "     {1, 2, 3}}};");
2027   verifyFormat(
2028       "SomeArrayOfSomeType a = {\n"
2029       "    {{1, 2, 3}},\n"
2030       "    {{1, 2, 3}},\n"
2031       "    {{111111111111111111111111111111, 222222222222222222222222222222,\n"
2032       "      333333333333333333333333333333}},\n"
2033       "    {{1, 2, 3}},\n"
2034       "    {{1, 2, 3}}};");
2035 
2036   verifyFormat("struct {\n"
2037                "  unsigned bit;\n"
2038                "  const char *const name;\n"
2039                "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
2040                "                 {kOsWin, \"Windows\"},\n"
2041                "                 {kOsLinux, \"Linux\"},\n"
2042                "                 {kOsCrOS, \"Chrome OS\"}};");
2043   verifyFormat("struct {\n"
2044                "  unsigned bit;\n"
2045                "  const char *const name;\n"
2046                "} kBitsToOs[] = {\n"
2047                "    {kOsMac, \"Mac\"},\n"
2048                "    {kOsWin, \"Windows\"},\n"
2049                "    {kOsLinux, \"Linux\"},\n"
2050                "    {kOsCrOS, \"Chrome OS\"},\n"
2051                "};");
2052 }
2053 
2054 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
2055   verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
2056                "                      \\\n"
2057                "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
2058 }
2059 
2060 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
2061   verifyFormat("virtual void write(ELFWriter *writerrr,\n"
2062                "                   OwningPtr<FileOutputBuffer> &buffer) = 0;");
2063 
2064   // Do break defaulted and deleted functions.
2065   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
2066                "    default;",
2067                getLLVMStyleWithColumns(40));
2068   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
2069                "    delete;",
2070                getLLVMStyleWithColumns(40));
2071 }
2072 
2073 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
2074   verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
2075                getLLVMStyleWithColumns(40));
2076   verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
2077                getLLVMStyleWithColumns(40));
2078   EXPECT_EQ("#define Q                              \\\n"
2079             "  \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\"    \\\n"
2080             "  \"aaaaaaaa.cpp\"",
2081             format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
2082                    getLLVMStyleWithColumns(40)));
2083 }
2084 
2085 TEST_F(FormatTest, UnderstandsLinePPDirective) {
2086   EXPECT_EQ("# 123 \"A string literal\"",
2087             format("   #     123    \"A string literal\""));
2088 }
2089 
2090 TEST_F(FormatTest, LayoutUnknownPPDirective) {
2091   EXPECT_EQ("#;", format("#;"));
2092   verifyFormat("#\n;\n;\n;");
2093 }
2094 
2095 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
2096   EXPECT_EQ("#line 42 \"test\"\n",
2097             format("#  \\\n  line  \\\n  42  \\\n  \"test\"\n"));
2098   EXPECT_EQ("#define A B\n", format("#  \\\n define  \\\n    A  \\\n       B\n",
2099                                     getLLVMStyleWithColumns(12)));
2100 }
2101 
2102 TEST_F(FormatTest, EndOfFileEndsPPDirective) {
2103   EXPECT_EQ("#line 42 \"test\"",
2104             format("#  \\\n  line  \\\n  42  \\\n  \"test\""));
2105   EXPECT_EQ("#define A B", format("#  \\\n define  \\\n    A  \\\n       B"));
2106 }
2107 
2108 TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
2109   verifyFormat("#define A \\x20");
2110   verifyFormat("#define A \\ x20");
2111   EXPECT_EQ("#define A \\ x20", format("#define A \\   x20"));
2112   verifyFormat("#define A ''");
2113   verifyFormat("#define A ''qqq");
2114   verifyFormat("#define A `qqq");
2115   verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
2116   EXPECT_EQ("const char *c = STRINGIFY(\n"
2117             "\\na : b);",
2118             format("const char * c = STRINGIFY(\n"
2119                    "\\na : b);"));
2120 
2121   verifyFormat("a\r\\");
2122   verifyFormat("a\v\\");
2123   verifyFormat("a\f\\");
2124 }
2125 
2126 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
2127   verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
2128   verifyFormat("#define A( \\\n    BB)", getLLVMStyleWithColumns(12));
2129   verifyFormat("#define A( \\\n    A, B)", getLLVMStyleWithColumns(12));
2130   // FIXME: We never break before the macro name.
2131   verifyFormat("#define AA( \\\n    B)", getLLVMStyleWithColumns(12));
2132 
2133   verifyFormat("#define A A\n#define A A");
2134   verifyFormat("#define A(X) A\n#define A A");
2135 
2136   verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
2137   verifyFormat("#define Something    \\\n  Other", getLLVMStyleWithColumns(22));
2138 }
2139 
2140 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
2141   EXPECT_EQ("// somecomment\n"
2142             "#include \"a.h\"\n"
2143             "#define A(  \\\n"
2144             "    A, B)\n"
2145             "#include \"b.h\"\n"
2146             "// somecomment\n",
2147             format("  // somecomment\n"
2148                    "  #include \"a.h\"\n"
2149                    "#define A(A,\\\n"
2150                    "    B)\n"
2151                    "    #include \"b.h\"\n"
2152                    " // somecomment\n",
2153                    getLLVMStyleWithColumns(13)));
2154 }
2155 
2156 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
2157 
2158 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
2159   EXPECT_EQ("#define A    \\\n"
2160             "  c;         \\\n"
2161             "  e;\n"
2162             "f;",
2163             format("#define A c; e;\n"
2164                    "f;",
2165                    getLLVMStyleWithColumns(14)));
2166 }
2167 
2168 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
2169 
2170 TEST_F(FormatTest, MacroDefinitionInsideStatement) {
2171   EXPECT_EQ("int x,\n"
2172             "#define A\n"
2173             "    y;",
2174             format("int x,\n#define A\ny;"));
2175 }
2176 
2177 TEST_F(FormatTest, HashInMacroDefinition) {
2178   EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
2179   verifyFormat("#define A \\\n  b #c;", getLLVMStyleWithColumns(11));
2180   verifyFormat("#define A  \\\n"
2181                "  {        \\\n"
2182                "    f(#c); \\\n"
2183                "  }",
2184                getLLVMStyleWithColumns(11));
2185 
2186   verifyFormat("#define A(X)         \\\n"
2187                "  void function##X()",
2188                getLLVMStyleWithColumns(22));
2189 
2190   verifyFormat("#define A(a, b, c)   \\\n"
2191                "  void a##b##c()",
2192                getLLVMStyleWithColumns(22));
2193 
2194   verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
2195 }
2196 
2197 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
2198   EXPECT_EQ("#define A (x)", format("#define A (x)"));
2199   EXPECT_EQ("#define A(x)", format("#define A(x)"));
2200 }
2201 
2202 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
2203   EXPECT_EQ("#define A b;", format("#define A \\\n"
2204                                    "          \\\n"
2205                                    "  b;",
2206                                    getLLVMStyleWithColumns(25)));
2207   EXPECT_EQ("#define A \\\n"
2208             "          \\\n"
2209             "  a;      \\\n"
2210             "  b;",
2211             format("#define A \\\n"
2212                    "          \\\n"
2213                    "  a;      \\\n"
2214                    "  b;",
2215                    getLLVMStyleWithColumns(11)));
2216   EXPECT_EQ("#define A \\\n"
2217             "  a;      \\\n"
2218             "          \\\n"
2219             "  b;",
2220             format("#define A \\\n"
2221                    "  a;      \\\n"
2222                    "          \\\n"
2223                    "  b;",
2224                    getLLVMStyleWithColumns(11)));
2225 }
2226 
2227 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
2228   verifyIncompleteFormat("#define A :");
2229   verifyFormat("#define SOMECASES  \\\n"
2230                "  case 1:          \\\n"
2231                "  case 2\n",
2232                getLLVMStyleWithColumns(20));
2233   verifyFormat("#define MACRO(a) \\\n"
2234                "  if (a)         \\\n"
2235                "    f();         \\\n"
2236                "  else           \\\n"
2237                "    g()",
2238                getLLVMStyleWithColumns(18));
2239   verifyFormat("#define A template <typename T>");
2240   verifyIncompleteFormat("#define STR(x) #x\n"
2241                          "f(STR(this_is_a_string_literal{));");
2242   verifyFormat("#pragma omp threadprivate( \\\n"
2243                "    y)), // expected-warning",
2244                getLLVMStyleWithColumns(28));
2245   verifyFormat("#d, = };");
2246   verifyFormat("#if \"a");
2247   verifyIncompleteFormat("({\n"
2248                          "#define b     \\\n"
2249                          "  }           \\\n"
2250                          "  a\n"
2251                          "a",
2252                          getLLVMStyleWithColumns(15));
2253   verifyFormat("#define A     \\\n"
2254                "  {           \\\n"
2255                "    {\n"
2256                "#define B     \\\n"
2257                "  }           \\\n"
2258                "  }",
2259                getLLVMStyleWithColumns(15));
2260   verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
2261   verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
2262   verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
2263   verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() {      \n)}");
2264 }
2265 
2266 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
2267   verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
2268   EXPECT_EQ("class A : public QObject {\n"
2269             "  Q_OBJECT\n"
2270             "\n"
2271             "  A() {}\n"
2272             "};",
2273             format("class A  :  public QObject {\n"
2274                    "     Q_OBJECT\n"
2275                    "\n"
2276                    "  A() {\n}\n"
2277                    "}  ;"));
2278   EXPECT_EQ("MACRO\n"
2279             "/*static*/ int i;",
2280             format("MACRO\n"
2281                    " /*static*/ int   i;"));
2282   EXPECT_EQ("SOME_MACRO\n"
2283             "namespace {\n"
2284             "void f();\n"
2285             "} // namespace",
2286             format("SOME_MACRO\n"
2287                    "  namespace    {\n"
2288                    "void   f(  );\n"
2289                    "} // namespace"));
2290   // Only if the identifier contains at least 5 characters.
2291   EXPECT_EQ("HTTP f();", format("HTTP\nf();"));
2292   EXPECT_EQ("MACRO\nf();", format("MACRO\nf();"));
2293   // Only if everything is upper case.
2294   EXPECT_EQ("class A : public QObject {\n"
2295             "  Q_Object A() {}\n"
2296             "};",
2297             format("class A  :  public QObject {\n"
2298                    "     Q_Object\n"
2299                    "  A() {\n}\n"
2300                    "}  ;"));
2301 
2302   // Only if the next line can actually start an unwrapped line.
2303   EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
2304             format("SOME_WEIRD_LOG_MACRO\n"
2305                    "<< SomeThing;"));
2306 
2307   verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
2308                "(n, buffers))\n",
2309                getChromiumStyle(FormatStyle::LK_Cpp));
2310 }
2311 
2312 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
2313   EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
2314             "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
2315             "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
2316             "class X {};\n"
2317             "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
2318             "int *createScopDetectionPass() { return 0; }",
2319             format("  INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
2320                    "  INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
2321                    "  INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
2322                    "  class X {};\n"
2323                    "  INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
2324                    "  int *createScopDetectionPass() { return 0; }"));
2325   // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
2326   // braces, so that inner block is indented one level more.
2327   EXPECT_EQ("int q() {\n"
2328             "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
2329             "  IPC_MESSAGE_HANDLER(xxx, qqq)\n"
2330             "  IPC_END_MESSAGE_MAP()\n"
2331             "}",
2332             format("int q() {\n"
2333                    "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
2334                    "    IPC_MESSAGE_HANDLER(xxx, qqq)\n"
2335                    "  IPC_END_MESSAGE_MAP()\n"
2336                    "}"));
2337 
2338   // Same inside macros.
2339   EXPECT_EQ("#define LIST(L) \\\n"
2340             "  L(A)          \\\n"
2341             "  L(B)          \\\n"
2342             "  L(C)",
2343             format("#define LIST(L) \\\n"
2344                    "  L(A) \\\n"
2345                    "  L(B) \\\n"
2346                    "  L(C)",
2347                    getGoogleStyle()));
2348 
2349   // These must not be recognized as macros.
2350   EXPECT_EQ("int q() {\n"
2351             "  f(x);\n"
2352             "  f(x) {}\n"
2353             "  f(x)->g();\n"
2354             "  f(x)->*g();\n"
2355             "  f(x).g();\n"
2356             "  f(x) = x;\n"
2357             "  f(x) += x;\n"
2358             "  f(x) -= x;\n"
2359             "  f(x) *= x;\n"
2360             "  f(x) /= x;\n"
2361             "  f(x) %= x;\n"
2362             "  f(x) &= x;\n"
2363             "  f(x) |= x;\n"
2364             "  f(x) ^= x;\n"
2365             "  f(x) >>= x;\n"
2366             "  f(x) <<= x;\n"
2367             "  f(x)[y].z();\n"
2368             "  LOG(INFO) << x;\n"
2369             "  ifstream(x) >> x;\n"
2370             "}\n",
2371             format("int q() {\n"
2372                    "  f(x)\n;\n"
2373                    "  f(x)\n {}\n"
2374                    "  f(x)\n->g();\n"
2375                    "  f(x)\n->*g();\n"
2376                    "  f(x)\n.g();\n"
2377                    "  f(x)\n = x;\n"
2378                    "  f(x)\n += x;\n"
2379                    "  f(x)\n -= x;\n"
2380                    "  f(x)\n *= x;\n"
2381                    "  f(x)\n /= x;\n"
2382                    "  f(x)\n %= x;\n"
2383                    "  f(x)\n &= x;\n"
2384                    "  f(x)\n |= x;\n"
2385                    "  f(x)\n ^= x;\n"
2386                    "  f(x)\n >>= x;\n"
2387                    "  f(x)\n <<= x;\n"
2388                    "  f(x)\n[y].z();\n"
2389                    "  LOG(INFO)\n << x;\n"
2390                    "  ifstream(x)\n >> x;\n"
2391                    "}\n"));
2392   EXPECT_EQ("int q() {\n"
2393             "  F(x)\n"
2394             "  if (1) {\n"
2395             "  }\n"
2396             "  F(x)\n"
2397             "  while (1) {\n"
2398             "  }\n"
2399             "  F(x)\n"
2400             "  G(x);\n"
2401             "  F(x)\n"
2402             "  try {\n"
2403             "    Q();\n"
2404             "  } catch (...) {\n"
2405             "  }\n"
2406             "}\n",
2407             format("int q() {\n"
2408                    "F(x)\n"
2409                    "if (1) {}\n"
2410                    "F(x)\n"
2411                    "while (1) {}\n"
2412                    "F(x)\n"
2413                    "G(x);\n"
2414                    "F(x)\n"
2415                    "try { Q(); } catch (...) {}\n"
2416                    "}\n"));
2417   EXPECT_EQ("class A {\n"
2418             "  A() : t(0) {}\n"
2419             "  A(int i) noexcept() : {}\n"
2420             "  A(X x)\n" // FIXME: function-level try blocks are broken.
2421             "  try : t(0) {\n"
2422             "  } catch (...) {\n"
2423             "  }\n"
2424             "};",
2425             format("class A {\n"
2426                    "  A()\n : t(0) {}\n"
2427                    "  A(int i)\n noexcept() : {}\n"
2428                    "  A(X x)\n"
2429                    "  try : t(0) {} catch (...) {}\n"
2430                    "};"));
2431   EXPECT_EQ("class SomeClass {\n"
2432             "public:\n"
2433             "  SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2434             "};",
2435             format("class SomeClass {\n"
2436                    "public:\n"
2437                    "  SomeClass()\n"
2438                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2439                    "};"));
2440   EXPECT_EQ("class SomeClass {\n"
2441             "public:\n"
2442             "  SomeClass()\n"
2443             "      EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2444             "};",
2445             format("class SomeClass {\n"
2446                    "public:\n"
2447                    "  SomeClass()\n"
2448                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2449                    "};",
2450                    getLLVMStyleWithColumns(40)));
2451 
2452   verifyFormat("MACRO(>)");
2453 }
2454 
2455 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
2456   verifyFormat("#define A \\\n"
2457                "  f({     \\\n"
2458                "    g();  \\\n"
2459                "  });",
2460                getLLVMStyleWithColumns(11));
2461 }
2462 
2463 TEST_F(FormatTest, IndentPreprocessorDirectives) {
2464   FormatStyle Style = getLLVMStyle();
2465   Style.IndentPPDirectives = FormatStyle::PPDIS_None;
2466   Style.ColumnLimit = 40;
2467   verifyFormat("#ifdef _WIN32\n"
2468                "#define A 0\n"
2469                "#ifdef VAR2\n"
2470                "#define B 1\n"
2471                "#include <someheader.h>\n"
2472                "#define MACRO                          \\\n"
2473                "  some_very_long_func_aaaaaaaaaa();\n"
2474                "#endif\n"
2475                "#else\n"
2476                "#define A 1\n"
2477                "#endif",
2478                Style);
2479   Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
2480   verifyFormat("#ifdef _WIN32\n"
2481                "#  define A 0\n"
2482                "#  ifdef VAR2\n"
2483                "#    define B 1\n"
2484                "#    include <someheader.h>\n"
2485                "#    define MACRO                      \\\n"
2486                "      some_very_long_func_aaaaaaaaaa();\n"
2487                "#  endif\n"
2488                "#else\n"
2489                "#  define A 1\n"
2490                "#endif",
2491                Style);
2492   verifyFormat("#if A\n"
2493                "#  define MACRO                        \\\n"
2494                "    void a(int x) {                    \\\n"
2495                "      b();                             \\\n"
2496                "      c();                             \\\n"
2497                "      d();                             \\\n"
2498                "      e();                             \\\n"
2499                "      f();                             \\\n"
2500                "    }\n"
2501                "#endif",
2502                Style);
2503   // Comments before include guard.
2504   verifyFormat("// file comment\n"
2505                "// file comment\n"
2506                "#ifndef HEADER_H\n"
2507                "#define HEADER_H\n"
2508                "code();\n"
2509                "#endif",
2510                Style);
2511   // Test with include guards.
2512   verifyFormat("#ifndef HEADER_H\n"
2513                "#define HEADER_H\n"
2514                "code();\n"
2515                "#endif",
2516                Style);
2517   // Include guards must have a #define with the same variable immediately
2518   // after #ifndef.
2519   verifyFormat("#ifndef NOT_GUARD\n"
2520                "#  define FOO\n"
2521                "code();\n"
2522                "#endif",
2523                Style);
2524 
2525   // Include guards must cover the entire file.
2526   verifyFormat("code();\n"
2527                "code();\n"
2528                "#ifndef NOT_GUARD\n"
2529                "#  define NOT_GUARD\n"
2530                "code();\n"
2531                "#endif",
2532                Style);
2533   verifyFormat("#ifndef NOT_GUARD\n"
2534                "#  define NOT_GUARD\n"
2535                "code();\n"
2536                "#endif\n"
2537                "code();",
2538                Style);
2539   // Test with trailing blank lines.
2540   verifyFormat("#ifndef HEADER_H\n"
2541                "#define HEADER_H\n"
2542                "code();\n"
2543                "#endif\n",
2544                Style);
2545   // Include guards don't have #else.
2546   verifyFormat("#ifndef NOT_GUARD\n"
2547                "#  define NOT_GUARD\n"
2548                "code();\n"
2549                "#else\n"
2550                "#endif",
2551                Style);
2552   verifyFormat("#ifndef NOT_GUARD\n"
2553                "#  define NOT_GUARD\n"
2554                "code();\n"
2555                "#elif FOO\n"
2556                "#endif",
2557                Style);
2558   // Non-identifier #define after potential include guard.
2559   verifyFormat("#ifndef FOO\n"
2560                "#  define 1\n"
2561                "#endif\n",
2562                Style);
2563   // #if closes past last non-preprocessor line.
2564   verifyFormat("#ifndef FOO\n"
2565                "#define FOO\n"
2566                "#if 1\n"
2567                "int i;\n"
2568                "#  define A 0\n"
2569                "#endif\n"
2570                "#endif\n",
2571                Style);
2572   // FIXME: This doesn't handle the case where there's code between the
2573   // #ifndef and #define but all other conditions hold. This is because when
2574   // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the
2575   // previous code line yet, so we can't detect it.
2576   EXPECT_EQ("#ifndef NOT_GUARD\n"
2577             "code();\n"
2578             "#define NOT_GUARD\n"
2579             "code();\n"
2580             "#endif",
2581             format("#ifndef NOT_GUARD\n"
2582                    "code();\n"
2583                    "#  define NOT_GUARD\n"
2584                    "code();\n"
2585                    "#endif",
2586                    Style));
2587   // FIXME: This doesn't handle cases where legitimate preprocessor lines may
2588   // be outside an include guard. Examples are #pragma once and
2589   // #pragma GCC diagnostic, or anything else that does not change the meaning
2590   // of the file if it's included multiple times.
2591   EXPECT_EQ("#ifdef WIN32\n"
2592             "#  pragma once\n"
2593             "#endif\n"
2594             "#ifndef HEADER_H\n"
2595             "#  define HEADER_H\n"
2596             "code();\n"
2597             "#endif",
2598             format("#ifdef WIN32\n"
2599                    "#  pragma once\n"
2600                    "#endif\n"
2601                    "#ifndef HEADER_H\n"
2602                    "#define HEADER_H\n"
2603                    "code();\n"
2604                    "#endif",
2605                    Style));
2606   // FIXME: This does not detect when there is a single non-preprocessor line
2607   // in front of an include-guard-like structure where other conditions hold
2608   // because ScopedLineState hides the line.
2609   EXPECT_EQ("code();\n"
2610             "#ifndef HEADER_H\n"
2611             "#define HEADER_H\n"
2612             "code();\n"
2613             "#endif",
2614             format("code();\n"
2615                    "#ifndef HEADER_H\n"
2616                    "#  define HEADER_H\n"
2617                    "code();\n"
2618                    "#endif",
2619                    Style));
2620   // Keep comments aligned with #, otherwise indent comments normally. These
2621   // tests cannot use verifyFormat because messUp manipulates leading
2622   // whitespace.
2623   {
2624     const char *Expected = ""
2625                            "void f() {\n"
2626                            "#if 1\n"
2627                            "// Preprocessor aligned.\n"
2628                            "#  define A 0\n"
2629                            "  // Code. Separated by blank line.\n"
2630                            "\n"
2631                            "#  define B 0\n"
2632                            "  // Code. Not aligned with #\n"
2633                            "#  define C 0\n"
2634                            "#endif";
2635     const char *ToFormat = ""
2636                            "void f() {\n"
2637                            "#if 1\n"
2638                            "// Preprocessor aligned.\n"
2639                            "#  define A 0\n"
2640                            "// Code. Separated by blank line.\n"
2641                            "\n"
2642                            "#  define B 0\n"
2643                            "   // Code. Not aligned with #\n"
2644                            "#  define C 0\n"
2645                            "#endif";
2646     EXPECT_EQ(Expected, format(ToFormat, Style));
2647     EXPECT_EQ(Expected, format(Expected, Style));
2648   }
2649   // Keep block quotes aligned.
2650   {
2651     const char *Expected = ""
2652                            "void f() {\n"
2653                            "#if 1\n"
2654                            "/* Preprocessor aligned. */\n"
2655                            "#  define A 0\n"
2656                            "  /* Code. Separated by blank line. */\n"
2657                            "\n"
2658                            "#  define B 0\n"
2659                            "  /* Code. Not aligned with # */\n"
2660                            "#  define C 0\n"
2661                            "#endif";
2662     const char *ToFormat = ""
2663                            "void f() {\n"
2664                            "#if 1\n"
2665                            "/* Preprocessor aligned. */\n"
2666                            "#  define A 0\n"
2667                            "/* Code. Separated by blank line. */\n"
2668                            "\n"
2669                            "#  define B 0\n"
2670                            "   /* Code. Not aligned with # */\n"
2671                            "#  define C 0\n"
2672                            "#endif";
2673     EXPECT_EQ(Expected, format(ToFormat, Style));
2674     EXPECT_EQ(Expected, format(Expected, Style));
2675   }
2676   // Keep comments aligned with un-indented directives.
2677   {
2678     const char *Expected = ""
2679                            "void f() {\n"
2680                            "// Preprocessor aligned.\n"
2681                            "#define A 0\n"
2682                            "  // Code. Separated by blank line.\n"
2683                            "\n"
2684                            "#define B 0\n"
2685                            "  // Code. Not aligned with #\n"
2686                            "#define C 0\n";
2687     const char *ToFormat = ""
2688                            "void f() {\n"
2689                            "// Preprocessor aligned.\n"
2690                            "#define A 0\n"
2691                            "// Code. Separated by blank line.\n"
2692                            "\n"
2693                            "#define B 0\n"
2694                            "   // Code. Not aligned with #\n"
2695                            "#define C 0\n";
2696     EXPECT_EQ(Expected, format(ToFormat, Style));
2697     EXPECT_EQ(Expected, format(Expected, Style));
2698   }
2699   // Test with tabs.
2700   Style.UseTab = FormatStyle::UT_Always;
2701   Style.IndentWidth = 8;
2702   Style.TabWidth = 8;
2703   verifyFormat("#ifdef _WIN32\n"
2704                "#\tdefine A 0\n"
2705                "#\tifdef VAR2\n"
2706                "#\t\tdefine B 1\n"
2707                "#\t\tinclude <someheader.h>\n"
2708                "#\t\tdefine MACRO          \\\n"
2709                "\t\t\tsome_very_long_func_aaaaaaaaaa();\n"
2710                "#\tendif\n"
2711                "#else\n"
2712                "#\tdefine A 1\n"
2713                "#endif",
2714                Style);
2715 
2716   // Regression test: Multiline-macro inside include guards.
2717   verifyFormat("#ifndef HEADER_H\n"
2718                "#define HEADER_H\n"
2719                "#define A()        \\\n"
2720                "  int i;           \\\n"
2721                "  int j;\n"
2722                "#endif // HEADER_H",
2723                getLLVMStyleWithColumns(20));
2724 }
2725 
2726 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
2727   verifyFormat("{\n  { a #c; }\n}");
2728 }
2729 
2730 TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
2731   EXPECT_EQ("#define A \\\n  {       \\\n    {\nint i;",
2732             format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
2733   EXPECT_EQ("#define A \\\n  }       \\\n  }\nint i;",
2734             format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
2735 }
2736 
2737 TEST_F(FormatTest, EscapedNewlines) {
2738   FormatStyle Narrow = getLLVMStyleWithColumns(11);
2739   EXPECT_EQ("#define A \\\n  int i;  \\\n  int j;",
2740             format("#define A \\\nint i;\\\n  int j;", Narrow));
2741   EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;"));
2742   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
2743   EXPECT_EQ("/* \\  \\  \\\n */", format("\\\n/* \\  \\  \\\n */"));
2744   EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>"));
2745 
2746   FormatStyle AlignLeft = getLLVMStyle();
2747   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
2748   EXPECT_EQ("#define MACRO(x) \\\n"
2749             "private:         \\\n"
2750             "  int x(int a);\n",
2751             format("#define MACRO(x) \\\n"
2752                    "private:         \\\n"
2753                    "  int x(int a);\n",
2754                    AlignLeft));
2755 
2756   // CRLF line endings
2757   EXPECT_EQ("#define A \\\r\n  int i;  \\\r\n  int j;",
2758             format("#define A \\\r\nint i;\\\r\n  int j;", Narrow));
2759   EXPECT_EQ("#define A\r\n\r\nint i;", format("#define A \\\r\n\r\n int i;"));
2760   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
2761   EXPECT_EQ("/* \\  \\  \\\r\n */", format("\\\r\n/* \\  \\  \\\r\n */"));
2762   EXPECT_EQ("<a\r\n\\\\\r\n>", format("<a\r\n\\\\\r\n>"));
2763   EXPECT_EQ("#define MACRO(x) \\\r\n"
2764             "private:         \\\r\n"
2765             "  int x(int a);\r\n",
2766             format("#define MACRO(x) \\\r\n"
2767                    "private:         \\\r\n"
2768                    "  int x(int a);\r\n",
2769                    AlignLeft));
2770 
2771   FormatStyle DontAlign = getLLVMStyle();
2772   DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
2773   DontAlign.MaxEmptyLinesToKeep = 3;
2774   // FIXME: can't use verifyFormat here because the newline before
2775   // "public:" is not inserted the first time it's reformatted
2776   EXPECT_EQ("#define A \\\n"
2777             "  class Foo { \\\n"
2778             "    void bar(); \\\n"
2779             "\\\n"
2780             "\\\n"
2781             "\\\n"
2782             "  public: \\\n"
2783             "    void baz(); \\\n"
2784             "  };",
2785             format("#define A \\\n"
2786                    "  class Foo { \\\n"
2787                    "    void bar(); \\\n"
2788                    "\\\n"
2789                    "\\\n"
2790                    "\\\n"
2791                    "  public: \\\n"
2792                    "    void baz(); \\\n"
2793                    "  };",
2794                    DontAlign));
2795 }
2796 
2797 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
2798   verifyFormat("#define A \\\n"
2799                "  int v(  \\\n"
2800                "      a); \\\n"
2801                "  int i;",
2802                getLLVMStyleWithColumns(11));
2803 }
2804 
2805 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
2806   EXPECT_EQ(
2807       "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
2808       "                      \\\n"
2809       "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
2810       "\n"
2811       "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
2812       "    aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
2813       format("  #define   ALooooooooooooooooooooooooooooooooooooooongMacro("
2814              "\\\n"
2815              "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
2816              "  \n"
2817              "   AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
2818              "  aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
2819 }
2820 
2821 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
2822   EXPECT_EQ("int\n"
2823             "#define A\n"
2824             "    a;",
2825             format("int\n#define A\na;"));
2826   verifyFormat("functionCallTo(\n"
2827                "    someOtherFunction(\n"
2828                "        withSomeParameters, whichInSequence,\n"
2829                "        areLongerThanALine(andAnotherCall,\n"
2830                "#define A B\n"
2831                "                           withMoreParamters,\n"
2832                "                           whichStronglyInfluenceTheLayout),\n"
2833                "        andMoreParameters),\n"
2834                "    trailing);",
2835                getLLVMStyleWithColumns(69));
2836   verifyFormat("Foo::Foo()\n"
2837                "#ifdef BAR\n"
2838                "    : baz(0)\n"
2839                "#endif\n"
2840                "{\n"
2841                "}");
2842   verifyFormat("void f() {\n"
2843                "  if (true)\n"
2844                "#ifdef A\n"
2845                "    f(42);\n"
2846                "  x();\n"
2847                "#else\n"
2848                "    g();\n"
2849                "  x();\n"
2850                "#endif\n"
2851                "}");
2852   verifyFormat("void f(param1, param2,\n"
2853                "       param3,\n"
2854                "#ifdef A\n"
2855                "       param4(param5,\n"
2856                "#ifdef A1\n"
2857                "              param6,\n"
2858                "#ifdef A2\n"
2859                "              param7),\n"
2860                "#else\n"
2861                "              param8),\n"
2862                "       param9,\n"
2863                "#endif\n"
2864                "       param10,\n"
2865                "#endif\n"
2866                "       param11)\n"
2867                "#else\n"
2868                "       param12)\n"
2869                "#endif\n"
2870                "{\n"
2871                "  x();\n"
2872                "}",
2873                getLLVMStyleWithColumns(28));
2874   verifyFormat("#if 1\n"
2875                "int i;");
2876   verifyFormat("#if 1\n"
2877                "#endif\n"
2878                "#if 1\n"
2879                "#else\n"
2880                "#endif\n");
2881   verifyFormat("DEBUG({\n"
2882                "  return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2883                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
2884                "});\n"
2885                "#if a\n"
2886                "#else\n"
2887                "#endif");
2888 
2889   verifyIncompleteFormat("void f(\n"
2890                          "#if A\n"
2891                          ");\n"
2892                          "#else\n"
2893                          "#endif");
2894 }
2895 
2896 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
2897   verifyFormat("#endif\n"
2898                "#if B");
2899 }
2900 
2901 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
2902   FormatStyle SingleLine = getLLVMStyle();
2903   SingleLine.AllowShortIfStatementsOnASingleLine = true;
2904   verifyFormat("#if 0\n"
2905                "#elif 1\n"
2906                "#endif\n"
2907                "void foo() {\n"
2908                "  if (test) foo2();\n"
2909                "}",
2910                SingleLine);
2911 }
2912 
2913 TEST_F(FormatTest, LayoutBlockInsideParens) {
2914   verifyFormat("functionCall({ int i; });");
2915   verifyFormat("functionCall({\n"
2916                "  int i;\n"
2917                "  int j;\n"
2918                "});");
2919   verifyFormat("functionCall(\n"
2920                "    {\n"
2921                "      int i;\n"
2922                "      int j;\n"
2923                "    },\n"
2924                "    aaaa, bbbb, cccc);");
2925   verifyFormat("functionA(functionB({\n"
2926                "            int i;\n"
2927                "            int j;\n"
2928                "          }),\n"
2929                "          aaaa, bbbb, cccc);");
2930   verifyFormat("functionCall(\n"
2931                "    {\n"
2932                "      int i;\n"
2933                "      int j;\n"
2934                "    },\n"
2935                "    aaaa, bbbb, // comment\n"
2936                "    cccc);");
2937   verifyFormat("functionA(functionB({\n"
2938                "            int i;\n"
2939                "            int j;\n"
2940                "          }),\n"
2941                "          aaaa, bbbb, // comment\n"
2942                "          cccc);");
2943   verifyFormat("functionCall(aaaa, bbbb, { int i; });");
2944   verifyFormat("functionCall(aaaa, bbbb, {\n"
2945                "  int i;\n"
2946                "  int j;\n"
2947                "});");
2948   verifyFormat(
2949       "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
2950       "    {\n"
2951       "      int i; // break\n"
2952       "    },\n"
2953       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
2954       "                                     ccccccccccccccccc));");
2955   verifyFormat("DEBUG({\n"
2956                "  if (a)\n"
2957                "    f();\n"
2958                "});");
2959 }
2960 
2961 TEST_F(FormatTest, LayoutBlockInsideStatement) {
2962   EXPECT_EQ("SOME_MACRO { int i; }\n"
2963             "int i;",
2964             format("  SOME_MACRO  {int i;}  int i;"));
2965 }
2966 
2967 TEST_F(FormatTest, LayoutNestedBlocks) {
2968   verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
2969                "  struct s {\n"
2970                "    int i;\n"
2971                "  };\n"
2972                "  s kBitsToOs[] = {{10}};\n"
2973                "  for (int i = 0; i < 10; ++i)\n"
2974                "    return;\n"
2975                "}");
2976   verifyFormat("call(parameter, {\n"
2977                "  something();\n"
2978                "  // Comment using all columns.\n"
2979                "  somethingelse();\n"
2980                "});",
2981                getLLVMStyleWithColumns(40));
2982   verifyFormat("DEBUG( //\n"
2983                "    { f(); }, a);");
2984   verifyFormat("DEBUG( //\n"
2985                "    {\n"
2986                "      f(); //\n"
2987                "    },\n"
2988                "    a);");
2989 
2990   EXPECT_EQ("call(parameter, {\n"
2991             "  something();\n"
2992             "  // Comment too\n"
2993             "  // looooooooooong.\n"
2994             "  somethingElse();\n"
2995             "});",
2996             format("call(parameter, {\n"
2997                    "  something();\n"
2998                    "  // Comment too looooooooooong.\n"
2999                    "  somethingElse();\n"
3000                    "});",
3001                    getLLVMStyleWithColumns(29)));
3002   EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int   i; });"));
3003   EXPECT_EQ("DEBUG({ // comment\n"
3004             "  int i;\n"
3005             "});",
3006             format("DEBUG({ // comment\n"
3007                    "int  i;\n"
3008                    "});"));
3009   EXPECT_EQ("DEBUG({\n"
3010             "  int i;\n"
3011             "\n"
3012             "  // comment\n"
3013             "  int j;\n"
3014             "});",
3015             format("DEBUG({\n"
3016                    "  int  i;\n"
3017                    "\n"
3018                    "  // comment\n"
3019                    "  int  j;\n"
3020                    "});"));
3021 
3022   verifyFormat("DEBUG({\n"
3023                "  if (a)\n"
3024                "    return;\n"
3025                "});");
3026   verifyGoogleFormat("DEBUG({\n"
3027                      "  if (a) return;\n"
3028                      "});");
3029   FormatStyle Style = getGoogleStyle();
3030   Style.ColumnLimit = 45;
3031   verifyFormat("Debug(aaaaa,\n"
3032                "      {\n"
3033                "        if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
3034                "      },\n"
3035                "      a);",
3036                Style);
3037 
3038   verifyFormat("SomeFunction({MACRO({ return output; }), b});");
3039 
3040   verifyNoCrash("^{v^{a}}");
3041 }
3042 
3043 TEST_F(FormatTest, FormatNestedBlocksInMacros) {
3044   EXPECT_EQ("#define MACRO()                     \\\n"
3045             "  Debug(aaa, /* force line break */ \\\n"
3046             "        {                           \\\n"
3047             "          int i;                    \\\n"
3048             "          int j;                    \\\n"
3049             "        })",
3050             format("#define   MACRO()   Debug(aaa,  /* force line break */ \\\n"
3051                    "          {  int   i;  int  j;   })",
3052                    getGoogleStyle()));
3053 
3054   EXPECT_EQ("#define A                                       \\\n"
3055             "  [] {                                          \\\n"
3056             "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \\\n"
3057             "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
3058             "  }",
3059             format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
3060                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
3061                    getGoogleStyle()));
3062 }
3063 
3064 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
3065   EXPECT_EQ("{}", format("{}"));
3066   verifyFormat("enum E {};");
3067   verifyFormat("enum E {}");
3068 }
3069 
3070 TEST_F(FormatTest, FormatBeginBlockEndMacros) {
3071   FormatStyle Style = getLLVMStyle();
3072   Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
3073   Style.MacroBlockEnd = "^[A-Z_]+_END$";
3074   verifyFormat("FOO_BEGIN\n"
3075                "  FOO_ENTRY\n"
3076                "FOO_END", Style);
3077   verifyFormat("FOO_BEGIN\n"
3078                "  NESTED_FOO_BEGIN\n"
3079                "    NESTED_FOO_ENTRY\n"
3080                "  NESTED_FOO_END\n"
3081                "FOO_END", Style);
3082   verifyFormat("FOO_BEGIN(Foo, Bar)\n"
3083                "  int x;\n"
3084                "  x = 1;\n"
3085                "FOO_END(Baz)", Style);
3086 }
3087 
3088 //===----------------------------------------------------------------------===//
3089 // Line break tests.
3090 //===----------------------------------------------------------------------===//
3091 
3092 TEST_F(FormatTest, PreventConfusingIndents) {
3093   verifyFormat(
3094       "void f() {\n"
3095       "  SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
3096       "                         parameter, parameter, parameter)),\n"
3097       "                     SecondLongCall(parameter));\n"
3098       "}");
3099   verifyFormat(
3100       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3101       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
3102       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3103       "    aaaaaaaaaaaaaaaaaaaaaaaa);");
3104   verifyFormat(
3105       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3106       "    [aaaaaaaaaaaaaaaaaaaaaaaa\n"
3107       "         [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
3108       "         [aaaaaaaaaaaaaaaaaaaaaaaa]];");
3109   verifyFormat(
3110       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
3111       "    aaaaaaaaaaaaaaaaaaaaaaaa<\n"
3112       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
3113       "    aaaaaaaaaaaaaaaaaaaaaaaa>;");
3114   verifyFormat("int a = bbbb && ccc &&\n"
3115                "        fffff(\n"
3116                "#define A Just forcing a new line\n"
3117                "            ddd);");
3118 }
3119 
3120 TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
3121   verifyFormat(
3122       "bool aaaaaaa =\n"
3123       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
3124       "    bbbbbbbb();");
3125   verifyFormat(
3126       "bool aaaaaaa =\n"
3127       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
3128       "    bbbbbbbb();");
3129 
3130   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
3131                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
3132                "    ccccccccc == ddddddddddd;");
3133   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
3134                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
3135                "    ccccccccc == ddddddddddd;");
3136   verifyFormat(
3137       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
3138       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
3139       "    ccccccccc == ddddddddddd;");
3140 
3141   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
3142                "                 aaaaaa) &&\n"
3143                "         bbbbbb && cccccc;");
3144   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
3145                "                 aaaaaa) >>\n"
3146                "         bbbbbb;");
3147   verifyFormat("aa = Whitespaces.addUntouchableComment(\n"
3148                "    SourceMgr.getSpellingColumnNumber(\n"
3149                "        TheLine.Last->FormatTok.Tok.getLocation()) -\n"
3150                "    1);");
3151 
3152   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3153                "     bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
3154                "    cccccc) {\n}");
3155   verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3156                "               bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
3157                "              cccccc) {\n}");
3158   verifyFormat("b = a &&\n"
3159                "    // Comment\n"
3160                "    b.c && d;");
3161 
3162   // If the LHS of a comparison is not a binary expression itself, the
3163   // additional linebreak confuses many people.
3164   verifyFormat(
3165       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3166       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
3167       "}");
3168   verifyFormat(
3169       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3170       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3171       "}");
3172   verifyFormat(
3173       "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
3174       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3175       "}");
3176   verifyFormat(
3177       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3178       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n"
3179       "}");
3180   // Even explicit parentheses stress the precedence enough to make the
3181   // additional break unnecessary.
3182   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3183                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3184                "}");
3185   // This cases is borderline, but with the indentation it is still readable.
3186   verifyFormat(
3187       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3188       "        aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3189       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
3190       "}",
3191       getLLVMStyleWithColumns(75));
3192 
3193   // If the LHS is a binary expression, we should still use the additional break
3194   // as otherwise the formatting hides the operator precedence.
3195   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3196                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3197                "    5) {\n"
3198                "}");
3199   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3200                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n"
3201                "    5) {\n"
3202                "}");
3203 
3204   FormatStyle OnePerLine = getLLVMStyle();
3205   OnePerLine.BinPackParameters = false;
3206   verifyFormat(
3207       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3208       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3209       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
3210       OnePerLine);
3211 
3212   verifyFormat("int i = someFunction(aaaaaaa, 0)\n"
3213                "                .aaa(aaaaaaaaaaaaa) *\n"
3214                "            aaaaaaa +\n"
3215                "        aaaaaaa;",
3216                getLLVMStyleWithColumns(40));
3217 }
3218 
3219 TEST_F(FormatTest, ExpressionIndentation) {
3220   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3221                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3222                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3223                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3224                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
3225                "                     bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
3226                "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3227                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
3228                "                 ccccccccccccccccccccccccccccccccccccccccc;");
3229   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3230                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3231                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3232                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3233   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3234                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3235                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3236                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3237   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3238                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3239                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3240                "        bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3241   verifyFormat("if () {\n"
3242                "} else if (aaaaa && bbbbb > // break\n"
3243                "                        ccccc) {\n"
3244                "}");
3245   verifyFormat("if () {\n"
3246                "} else if (aaaaa &&\n"
3247                "           bbbbb > // break\n"
3248                "               ccccc &&\n"
3249                "           ddddd) {\n"
3250                "}");
3251 
3252   // Presence of a trailing comment used to change indentation of b.
3253   verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
3254                "       b;\n"
3255                "return aaaaaaaaaaaaaaaaaaa +\n"
3256                "       b; //",
3257                getLLVMStyleWithColumns(30));
3258 }
3259 
3260 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
3261   // Not sure what the best system is here. Like this, the LHS can be found
3262   // immediately above an operator (everything with the same or a higher
3263   // indent). The RHS is aligned right of the operator and so compasses
3264   // everything until something with the same indent as the operator is found.
3265   // FIXME: Is this a good system?
3266   FormatStyle Style = getLLVMStyle();
3267   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
3268   verifyFormat(
3269       "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3270       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3271       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3272       "                 == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3273       "                            * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3274       "                        + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3275       "             && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3276       "                        * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3277       "                    > ccccccccccccccccccccccccccccccccccccccccc;",
3278       Style);
3279   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3280                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3281                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3282                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3283                Style);
3284   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3285                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3286                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3287                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3288                Style);
3289   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3290                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3291                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3292                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3293                Style);
3294   verifyFormat("if () {\n"
3295                "} else if (aaaaa\n"
3296                "           && bbbbb // break\n"
3297                "                  > ccccc) {\n"
3298                "}",
3299                Style);
3300   verifyFormat("return (a)\n"
3301                "       // comment\n"
3302                "       + b;",
3303                Style);
3304   verifyFormat(
3305       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3306       "                 * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3307       "             + cc;",
3308       Style);
3309 
3310   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3311                "    = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
3312                Style);
3313 
3314   // Forced by comments.
3315   verifyFormat(
3316       "unsigned ContentSize =\n"
3317       "    sizeof(int16_t)   // DWARF ARange version number\n"
3318       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
3319       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
3320       "    + sizeof(int8_t); // Segment Size (in bytes)");
3321 
3322   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
3323                "       == boost::fusion::at_c<1>(iiii).second;",
3324                Style);
3325 
3326   Style.ColumnLimit = 60;
3327   verifyFormat("zzzzzzzzzz\n"
3328                "    = bbbbbbbbbbbbbbbbb\n"
3329                "      >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
3330                Style);
3331 }
3332 
3333 TEST_F(FormatTest, EnforcedOperatorWraps) {
3334   // Here we'd like to wrap after the || operators, but a comment is forcing an
3335   // earlier wrap.
3336   verifyFormat("bool x = aaaaa //\n"
3337                "         || bbbbb\n"
3338                "         //\n"
3339                "         || cccc;");
3340 }
3341 
3342 TEST_F(FormatTest, NoOperandAlignment) {
3343   FormatStyle Style = getLLVMStyle();
3344   Style.AlignOperands = false;
3345   verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n"
3346                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3347                "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3348                Style);
3349   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
3350   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3351                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3352                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3353                "        == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3354                "                * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3355                "            + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3356                "    && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3357                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3358                "        > ccccccccccccccccccccccccccccccccccccccccc;",
3359                Style);
3360 
3361   verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3362                "        * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3363                "    + cc;",
3364                Style);
3365   verifyFormat("int a = aa\n"
3366                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3367                "        * cccccccccccccccccccccccccccccccccccc;\n",
3368                Style);
3369 
3370   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
3371   verifyFormat("return (a > b\n"
3372                "    // comment1\n"
3373                "    // comment2\n"
3374                "    || c);",
3375                Style);
3376 }
3377 
3378 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
3379   FormatStyle Style = getLLVMStyle();
3380   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
3381   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
3382                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3383                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
3384                Style);
3385 }
3386 
3387 TEST_F(FormatTest, AllowBinPackingInsideArguments) {
3388   FormatStyle Style = getLLVMStyle();
3389   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
3390   Style.BinPackArguments = false;
3391   Style.ColumnLimit = 40;
3392   verifyFormat("void test() {\n"
3393                "  someFunction(\n"
3394                "      this + argument + is + quite\n"
3395                "      + long + so + it + gets + wrapped\n"
3396                "      + but + remains + bin - packed);\n"
3397                "}",
3398                Style);
3399   verifyFormat("void test() {\n"
3400                "  someFunction(arg1,\n"
3401                "               this + argument + is\n"
3402                "                   + quite + long + so\n"
3403                "                   + it + gets + wrapped\n"
3404                "                   + but + remains + bin\n"
3405                "                   - packed,\n"
3406                "               arg3);\n"
3407                "}",
3408                Style);
3409   verifyFormat("void test() {\n"
3410                "  someFunction(\n"
3411                "      arg1,\n"
3412                "      this + argument + has\n"
3413                "          + anotherFunc(nested,\n"
3414                "                        calls + whose\n"
3415                "                            + arguments\n"
3416                "                            + are + also\n"
3417                "                            + wrapped,\n"
3418                "                        in + addition)\n"
3419                "          + to + being + bin - packed,\n"
3420                "      arg3);\n"
3421                "}",
3422                Style);
3423 
3424   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
3425   verifyFormat("void test() {\n"
3426                "  someFunction(\n"
3427                "      arg1,\n"
3428                "      this + argument + has +\n"
3429                "          anotherFunc(nested,\n"
3430                "                      calls + whose +\n"
3431                "                          arguments +\n"
3432                "                          are + also +\n"
3433                "                          wrapped,\n"
3434                "                      in + addition) +\n"
3435                "          to + being + bin - packed,\n"
3436                "      arg3);\n"
3437                "}",
3438                Style);
3439 }
3440 
3441 TEST_F(FormatTest, ConstructorInitializers) {
3442   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
3443   verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
3444                getLLVMStyleWithColumns(45));
3445   verifyFormat("Constructor()\n"
3446                "    : Inttializer(FitsOnTheLine) {}",
3447                getLLVMStyleWithColumns(44));
3448   verifyFormat("Constructor()\n"
3449                "    : Inttializer(FitsOnTheLine) {}",
3450                getLLVMStyleWithColumns(43));
3451 
3452   verifyFormat("template <typename T>\n"
3453                "Constructor() : Initializer(FitsOnTheLine) {}",
3454                getLLVMStyleWithColumns(45));
3455 
3456   verifyFormat(
3457       "SomeClass::Constructor()\n"
3458       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
3459 
3460   verifyFormat(
3461       "SomeClass::Constructor()\n"
3462       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3463       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
3464   verifyFormat(
3465       "SomeClass::Constructor()\n"
3466       "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3467       "      aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
3468   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3469                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3470                "    : aaaaaaaaaa(aaaaaa) {}");
3471 
3472   verifyFormat("Constructor()\n"
3473                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3474                "      aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3475                "                               aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3476                "      aaaaaaaaaaaaaaaaaaaaaaa() {}");
3477 
3478   verifyFormat("Constructor()\n"
3479                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3480                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3481 
3482   verifyFormat("Constructor(int Parameter = 0)\n"
3483                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
3484                "      aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
3485   verifyFormat("Constructor()\n"
3486                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
3487                "}",
3488                getLLVMStyleWithColumns(60));
3489   verifyFormat("Constructor()\n"
3490                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3491                "          aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
3492 
3493   // Here a line could be saved by splitting the second initializer onto two
3494   // lines, but that is not desirable.
3495   verifyFormat("Constructor()\n"
3496                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
3497                "      aaaaaaaaaaa(aaaaaaaaaaa),\n"
3498                "      aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3499 
3500   FormatStyle OnePerLine = getLLVMStyle();
3501   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
3502   OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
3503   verifyFormat("SomeClass::Constructor()\n"
3504                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3505                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3506                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3507                OnePerLine);
3508   verifyFormat("SomeClass::Constructor()\n"
3509                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
3510                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3511                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3512                OnePerLine);
3513   verifyFormat("MyClass::MyClass(int var)\n"
3514                "    : some_var_(var),            // 4 space indent\n"
3515                "      some_other_var_(var + 1) { // lined up\n"
3516                "}",
3517                OnePerLine);
3518   verifyFormat("Constructor()\n"
3519                "    : aaaaa(aaaaaa),\n"
3520                "      aaaaa(aaaaaa),\n"
3521                "      aaaaa(aaaaaa),\n"
3522                "      aaaaa(aaaaaa),\n"
3523                "      aaaaa(aaaaaa) {}",
3524                OnePerLine);
3525   verifyFormat("Constructor()\n"
3526                "    : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
3527                "            aaaaaaaaaaaaaaaaaaaaaa) {}",
3528                OnePerLine);
3529   OnePerLine.BinPackParameters = false;
3530   verifyFormat(
3531       "Constructor()\n"
3532       "    : aaaaaaaaaaaaaaaaaaaaaaaa(\n"
3533       "          aaaaaaaaaaa().aaa(),\n"
3534       "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3535       OnePerLine);
3536   OnePerLine.ColumnLimit = 60;
3537   verifyFormat("Constructor()\n"
3538                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
3539                "      bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
3540                OnePerLine);
3541 
3542   EXPECT_EQ("Constructor()\n"
3543             "    : // Comment forcing unwanted break.\n"
3544             "      aaaa(aaaa) {}",
3545             format("Constructor() :\n"
3546                    "    // Comment forcing unwanted break.\n"
3547                    "    aaaa(aaaa) {}"));
3548 }
3549 
3550 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) {
3551   FormatStyle Style = getLLVMStyle();
3552   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
3553 
3554   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
3555   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}",
3556                getStyleWithColumns(Style, 45));
3557   verifyFormat("Constructor() :\n"
3558                "    Initializer(FitsOnTheLine) {}",
3559                getStyleWithColumns(Style, 44));
3560   verifyFormat("Constructor() :\n"
3561                "    Initializer(FitsOnTheLine) {}",
3562                getStyleWithColumns(Style, 43));
3563 
3564   verifyFormat("template <typename T>\n"
3565                "Constructor() : Initializer(FitsOnTheLine) {}",
3566                getStyleWithColumns(Style, 50));
3567 
3568   verifyFormat(
3569       "SomeClass::Constructor() :\n"
3570       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
3571 	  Style);
3572 
3573   verifyFormat(
3574       "SomeClass::Constructor() :\n"
3575       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3576       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3577 	  Style);
3578   verifyFormat(
3579       "SomeClass::Constructor() :\n"
3580       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3581       "    aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
3582 	  Style);
3583   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3584                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
3585                "    aaaaaaaaaa(aaaaaa) {}",
3586 			   Style);
3587 
3588   verifyFormat("Constructor() :\n"
3589                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3590                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3591                "                             aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3592                "    aaaaaaaaaaaaaaaaaaaaaaa() {}",
3593 			   Style);
3594 
3595   verifyFormat("Constructor() :\n"
3596                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3597                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3598 			   Style);
3599 
3600   verifyFormat("Constructor(int Parameter = 0) :\n"
3601                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
3602                "    aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}",
3603 			   Style);
3604   verifyFormat("Constructor() :\n"
3605                "    aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
3606                "}",
3607                getStyleWithColumns(Style, 60));
3608   verifyFormat("Constructor() :\n"
3609                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3610                "        aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}",
3611 			   Style);
3612 
3613   // Here a line could be saved by splitting the second initializer onto two
3614   // lines, but that is not desirable.
3615   verifyFormat("Constructor() :\n"
3616                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
3617                "    aaaaaaaaaaa(aaaaaaaaaaa),\n"
3618                "    aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3619 			   Style);
3620 
3621   FormatStyle OnePerLine = Style;
3622   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
3623   OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
3624   verifyFormat("SomeClass::Constructor() :\n"
3625                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3626                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3627                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3628                OnePerLine);
3629   verifyFormat("SomeClass::Constructor() :\n"
3630                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
3631                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3632                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3633                OnePerLine);
3634   verifyFormat("MyClass::MyClass(int var) :\n"
3635                "    some_var_(var),            // 4 space indent\n"
3636                "    some_other_var_(var + 1) { // lined up\n"
3637                "}",
3638                OnePerLine);
3639   verifyFormat("Constructor() :\n"
3640                "    aaaaa(aaaaaa),\n"
3641                "    aaaaa(aaaaaa),\n"
3642                "    aaaaa(aaaaaa),\n"
3643                "    aaaaa(aaaaaa),\n"
3644                "    aaaaa(aaaaaa) {}",
3645                OnePerLine);
3646   verifyFormat("Constructor() :\n"
3647                "    aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
3648                "          aaaaaaaaaaaaaaaaaaaaaa) {}",
3649                OnePerLine);
3650   OnePerLine.BinPackParameters = false;
3651   verifyFormat(
3652       "Constructor() :\n"
3653       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
3654       "        aaaaaaaaaaa().aaa(),\n"
3655       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3656       OnePerLine);
3657   OnePerLine.ColumnLimit = 60;
3658   verifyFormat("Constructor() :\n"
3659                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
3660                "    bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
3661                OnePerLine);
3662 
3663   EXPECT_EQ("Constructor() :\n"
3664             "    // Comment forcing unwanted break.\n"
3665             "    aaaa(aaaa) {}",
3666             format("Constructor() :\n"
3667                    "    // Comment forcing unwanted break.\n"
3668                    "    aaaa(aaaa) {}",
3669 				   Style));
3670 
3671   Style.ColumnLimit = 0;
3672   verifyFormat("SomeClass::Constructor() :\n"
3673                "    a(a) {}",
3674                Style);
3675   verifyFormat("SomeClass::Constructor() noexcept :\n"
3676                "    a(a) {}",
3677                Style);
3678   verifyFormat("SomeClass::Constructor() :\n"
3679 			   "    a(a), b(b), c(c) {}",
3680                Style);
3681   verifyFormat("SomeClass::Constructor() :\n"
3682                "    a(a) {\n"
3683                "  foo();\n"
3684                "  bar();\n"
3685                "}",
3686                Style);
3687 
3688   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
3689   verifyFormat("SomeClass::Constructor() :\n"
3690 			   "    a(a), b(b), c(c) {\n"
3691 			   "}",
3692                Style);
3693   verifyFormat("SomeClass::Constructor() :\n"
3694                "    a(a) {\n"
3695 			   "}",
3696                Style);
3697 
3698   Style.ColumnLimit = 80;
3699   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
3700   Style.ConstructorInitializerIndentWidth = 2;
3701   verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}",
3702                Style);
3703   verifyFormat("SomeClass::Constructor() :\n"
3704                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3705                "  bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}",
3706                Style);
3707 }
3708 
3709 #ifndef EXPENSIVE_CHECKS
3710 // Expensive checks enables libstdc++ checking which includes validating the
3711 // state of ranges used in std::priority_queue - this blows out the
3712 // runtime/scalability of the function and makes this test unacceptably slow.
3713 TEST_F(FormatTest, MemoizationTests) {
3714   // This breaks if the memoization lookup does not take \c Indent and
3715   // \c LastSpace into account.
3716   verifyFormat(
3717       "extern CFRunLoopTimerRef\n"
3718       "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
3719       "                     CFTimeInterval interval, CFOptionFlags flags,\n"
3720       "                     CFIndex order, CFRunLoopTimerCallBack callout,\n"
3721       "                     CFRunLoopTimerContext *context) {}");
3722 
3723   // Deep nesting somewhat works around our memoization.
3724   verifyFormat(
3725       "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3726       "    aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3727       "        aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3728       "            aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3729       "                aaaaa())))))))))))))))))))))))))))))))))))))));",
3730       getLLVMStyleWithColumns(65));
3731   verifyFormat(
3732       "aaaaa(\n"
3733       "    aaaaa,\n"
3734       "    aaaaa(\n"
3735       "        aaaaa,\n"
3736       "        aaaaa(\n"
3737       "            aaaaa,\n"
3738       "            aaaaa(\n"
3739       "                aaaaa,\n"
3740       "                aaaaa(\n"
3741       "                    aaaaa,\n"
3742       "                    aaaaa(\n"
3743       "                        aaaaa,\n"
3744       "                        aaaaa(\n"
3745       "                            aaaaa,\n"
3746       "                            aaaaa(\n"
3747       "                                aaaaa,\n"
3748       "                                aaaaa(\n"
3749       "                                    aaaaa,\n"
3750       "                                    aaaaa(\n"
3751       "                                        aaaaa,\n"
3752       "                                        aaaaa(\n"
3753       "                                            aaaaa,\n"
3754       "                                            aaaaa(\n"
3755       "                                                aaaaa,\n"
3756       "                                                aaaaa))))))))))));",
3757       getLLVMStyleWithColumns(65));
3758   verifyFormat(
3759       "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"
3760       "                                  a),\n"
3761       "                                a),\n"
3762       "                              a),\n"
3763       "                            a),\n"
3764       "                          a),\n"
3765       "                        a),\n"
3766       "                      a),\n"
3767       "                    a),\n"
3768       "                  a),\n"
3769       "                a),\n"
3770       "              a),\n"
3771       "            a),\n"
3772       "          a),\n"
3773       "        a),\n"
3774       "      a),\n"
3775       "    a),\n"
3776       "  a)",
3777       getLLVMStyleWithColumns(65));
3778 
3779   // This test takes VERY long when memoization is broken.
3780   FormatStyle OnePerLine = getLLVMStyle();
3781   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
3782   OnePerLine.BinPackParameters = false;
3783   std::string input = "Constructor()\n"
3784                       "    : aaaa(a,\n";
3785   for (unsigned i = 0, e = 80; i != e; ++i) {
3786     input += "           a,\n";
3787   }
3788   input += "           a) {}";
3789   verifyFormat(input, OnePerLine);
3790 }
3791 #endif
3792 
3793 TEST_F(FormatTest, BreaksAsHighAsPossible) {
3794   verifyFormat(
3795       "void f() {\n"
3796       "  if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
3797       "      (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
3798       "    f();\n"
3799       "}");
3800   verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
3801                "    Intervals[i - 1].getRange().getLast()) {\n}");
3802 }
3803 
3804 TEST_F(FormatTest, BreaksFunctionDeclarations) {
3805   // Principially, we break function declarations in a certain order:
3806   // 1) break amongst arguments.
3807   verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
3808                "                              Cccccccccccccc cccccccccccccc);");
3809   verifyFormat("template <class TemplateIt>\n"
3810                "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
3811                "                            TemplateIt *stop) {}");
3812 
3813   // 2) break after return type.
3814   verifyFormat(
3815       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3816       "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
3817       getGoogleStyle());
3818 
3819   // 3) break after (.
3820   verifyFormat(
3821       "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
3822       "    Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
3823       getGoogleStyle());
3824 
3825   // 4) break before after nested name specifiers.
3826   verifyFormat(
3827       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3828       "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
3829       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
3830       getGoogleStyle());
3831 
3832   // However, there are exceptions, if a sufficient amount of lines can be
3833   // saved.
3834   // FIXME: The precise cut-offs wrt. the number of saved lines might need some
3835   // more adjusting.
3836   verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
3837                "                                  Cccccccccccccc cccccccccc,\n"
3838                "                                  Cccccccccccccc cccccccccc,\n"
3839                "                                  Cccccccccccccc cccccccccc,\n"
3840                "                                  Cccccccccccccc cccccccccc);");
3841   verifyFormat(
3842       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3843       "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3844       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3845       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
3846       getGoogleStyle());
3847   verifyFormat(
3848       "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
3849       "                                          Cccccccccccccc cccccccccc,\n"
3850       "                                          Cccccccccccccc cccccccccc,\n"
3851       "                                          Cccccccccccccc cccccccccc,\n"
3852       "                                          Cccccccccccccc cccccccccc,\n"
3853       "                                          Cccccccccccccc cccccccccc,\n"
3854       "                                          Cccccccccccccc cccccccccc);");
3855   verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
3856                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3857                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3858                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3859                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
3860 
3861   // Break after multi-line parameters.
3862   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3863                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3864                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3865                "    bbbb bbbb);");
3866   verifyFormat("void SomeLoooooooooooongFunction(\n"
3867                "    std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
3868                "        aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3869                "    int bbbbbbbbbbbbb);");
3870 
3871   // Treat overloaded operators like other functions.
3872   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3873                "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
3874   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3875                "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
3876   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3877                "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
3878   verifyGoogleFormat(
3879       "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
3880       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
3881   verifyGoogleFormat(
3882       "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
3883       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
3884   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3885                "    int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
3886   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
3887                "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
3888   verifyGoogleFormat(
3889       "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
3890       "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3891       "    bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
3892   verifyGoogleFormat(
3893       "template <typename T>\n"
3894       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3895       "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n"
3896       "    aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);");
3897 
3898   FormatStyle Style = getLLVMStyle();
3899   Style.PointerAlignment = FormatStyle::PAS_Left;
3900   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3901                "    aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
3902                Style);
3903   verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
3904                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3905                Style);
3906 }
3907 
3908 TEST_F(FormatTest, TrailingReturnType) {
3909   verifyFormat("auto foo() -> int;\n");
3910   verifyFormat("struct S {\n"
3911                "  auto bar() const -> int;\n"
3912                "};");
3913   verifyFormat("template <size_t Order, typename T>\n"
3914                "auto load_img(const std::string &filename)\n"
3915                "    -> alias::tensor<Order, T, mem::tag::cpu> {}");
3916   verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
3917                "    -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
3918   verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
3919   verifyFormat("template <typename T>\n"
3920                "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
3921                "    -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
3922 
3923   // Not trailing return types.
3924   verifyFormat("void f() { auto a = b->c(); }");
3925 }
3926 
3927 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
3928   // Avoid breaking before trailing 'const' or other trailing annotations, if
3929   // they are not function-like.
3930   FormatStyle Style = getGoogleStyle();
3931   Style.ColumnLimit = 47;
3932   verifyFormat("void someLongFunction(\n"
3933                "    int someLoooooooooooooongParameter) const {\n}",
3934                getLLVMStyleWithColumns(47));
3935   verifyFormat("LoooooongReturnType\n"
3936                "someLoooooooongFunction() const {}",
3937                getLLVMStyleWithColumns(47));
3938   verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
3939                "    const {}",
3940                Style);
3941   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3942                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
3943   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3944                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
3945   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3946                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
3947   verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
3948                "                   aaaaaaaaaaa aaaaa) const override;");
3949   verifyGoogleFormat(
3950       "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
3951       "    const override;");
3952 
3953   // Even if the first parameter has to be wrapped.
3954   verifyFormat("void someLongFunction(\n"
3955                "    int someLongParameter) const {}",
3956                getLLVMStyleWithColumns(46));
3957   verifyFormat("void someLongFunction(\n"
3958                "    int someLongParameter) const {}",
3959                Style);
3960   verifyFormat("void someLongFunction(\n"
3961                "    int someLongParameter) override {}",
3962                Style);
3963   verifyFormat("void someLongFunction(\n"
3964                "    int someLongParameter) OVERRIDE {}",
3965                Style);
3966   verifyFormat("void someLongFunction(\n"
3967                "    int someLongParameter) final {}",
3968                Style);
3969   verifyFormat("void someLongFunction(\n"
3970                "    int someLongParameter) FINAL {}",
3971                Style);
3972   verifyFormat("void someLongFunction(\n"
3973                "    int parameter) const override {}",
3974                Style);
3975 
3976   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
3977   verifyFormat("void someLongFunction(\n"
3978                "    int someLongParameter) const\n"
3979                "{\n"
3980                "}",
3981                Style);
3982 
3983   // Unless these are unknown annotations.
3984   verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
3985                "                  aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3986                "    LONG_AND_UGLY_ANNOTATION;");
3987 
3988   // Breaking before function-like trailing annotations is fine to keep them
3989   // close to their arguments.
3990   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3991                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
3992   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
3993                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
3994   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
3995                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
3996   verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
3997                      "    AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
3998   verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
3999 
4000   verifyFormat(
4001       "void aaaaaaaaaaaaaaaaaa()\n"
4002       "    __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
4003       "                   aaaaaaaaaaaaaaaaaaaaaaaaa));");
4004   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4005                "    __attribute__((unused));");
4006   verifyGoogleFormat(
4007       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4008       "    GUARDED_BY(aaaaaaaaaaaa);");
4009   verifyGoogleFormat(
4010       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4011       "    GUARDED_BY(aaaaaaaaaaaa);");
4012   verifyGoogleFormat(
4013       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
4014       "    aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4015   verifyGoogleFormat(
4016       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
4017       "    aaaaaaaaaaaaaaaaaaaaaaaaa;");
4018 }
4019 
4020 TEST_F(FormatTest, FunctionAnnotations) {
4021   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
4022                "int OldFunction(const string &parameter) {}");
4023   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
4024                "string OldFunction(const string &parameter) {}");
4025   verifyFormat("template <typename T>\n"
4026                "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
4027                "string OldFunction(const string &parameter) {}");
4028 
4029   // Not function annotations.
4030   verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4031                "                << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
4032   verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
4033                "       ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
4034   verifyFormat("MACRO(abc).function() // wrap\n"
4035                "    << abc;");
4036   verifyFormat("MACRO(abc)->function() // wrap\n"
4037                "    << abc;");
4038   verifyFormat("MACRO(abc)::function() // wrap\n"
4039                "    << abc;");
4040 }
4041 
4042 TEST_F(FormatTest, BreaksDesireably) {
4043   verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
4044                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
4045                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
4046   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4047                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
4048                "}");
4049 
4050   verifyFormat(
4051       "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4052       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
4053 
4054   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4055                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4056                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4057 
4058   verifyFormat(
4059       "aaaaaaaa(aaaaaaaaaaaaa,\n"
4060       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4061       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
4062       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4063       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
4064 
4065   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
4066                "    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4067 
4068   verifyFormat(
4069       "void f() {\n"
4070       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
4071       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
4072       "}");
4073   verifyFormat(
4074       "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4075       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4076   verifyFormat(
4077       "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4078       "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4079   verifyFormat(
4080       "aaaaaa(aaa,\n"
4081       "       new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4082       "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4083       "       aaaa);");
4084   verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4085                "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4086                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4087 
4088   // Indent consistently independent of call expression and unary operator.
4089   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
4090                "    dddddddddddddddddddddddddddddd));");
4091   verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
4092                "    dddddddddddddddddddddddddddddd));");
4093   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
4094                "    dddddddddddddddddddddddddddddd));");
4095 
4096   // This test case breaks on an incorrect memoization, i.e. an optimization not
4097   // taking into account the StopAt value.
4098   verifyFormat(
4099       "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
4100       "       aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
4101       "       aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
4102       "       (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4103 
4104   verifyFormat("{\n  {\n    {\n"
4105                "      Annotation.SpaceRequiredBefore =\n"
4106                "          Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
4107                "          Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
4108                "    }\n  }\n}");
4109 
4110   // Break on an outer level if there was a break on an inner level.
4111   EXPECT_EQ("f(g(h(a, // comment\n"
4112             "      b, c),\n"
4113             "    d, e),\n"
4114             "  x, y);",
4115             format("f(g(h(a, // comment\n"
4116                    "    b, c), d, e), x, y);"));
4117 
4118   // Prefer breaking similar line breaks.
4119   verifyFormat(
4120       "const int kTrackingOptions = NSTrackingMouseMoved |\n"
4121       "                             NSTrackingMouseEnteredAndExited |\n"
4122       "                             NSTrackingActiveAlways;");
4123 }
4124 
4125 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
4126   FormatStyle NoBinPacking = getGoogleStyle();
4127   NoBinPacking.BinPackParameters = false;
4128   NoBinPacking.BinPackArguments = true;
4129   verifyFormat("void f() {\n"
4130                "  f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
4131                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
4132                "}",
4133                NoBinPacking);
4134   verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
4135                "       int aaaaaaaaaaaaaaaaaaaa,\n"
4136                "       int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4137                NoBinPacking);
4138 
4139   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
4140   verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4141                "                        vector<int> bbbbbbbbbbbbbbb);",
4142                NoBinPacking);
4143   // FIXME: This behavior difference is probably not wanted. However, currently
4144   // we cannot distinguish BreakBeforeParameter being set because of the wrapped
4145   // template arguments from BreakBeforeParameter being set because of the
4146   // one-per-line formatting.
4147   verifyFormat(
4148       "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n"
4149       "                                             aaaaaaaaaa> aaaaaaaaaa);",
4150       NoBinPacking);
4151   verifyFormat(
4152       "void fffffffffff(\n"
4153       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n"
4154       "        aaaaaaaaaa);");
4155 }
4156 
4157 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
4158   FormatStyle NoBinPacking = getGoogleStyle();
4159   NoBinPacking.BinPackParameters = false;
4160   NoBinPacking.BinPackArguments = false;
4161   verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
4162                "  aaaaaaaaaaaaaaaaaaaa,\n"
4163                "  aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
4164                NoBinPacking);
4165   verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
4166                "        aaaaaaaaaaaaa,\n"
4167                "        aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
4168                NoBinPacking);
4169   verifyFormat(
4170       "aaaaaaaa(aaaaaaaaaaaaa,\n"
4171       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4172       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
4173       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4174       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
4175       NoBinPacking);
4176   verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
4177                "    .aaaaaaaaaaaaaaaaaa();",
4178                NoBinPacking);
4179   verifyFormat("void f() {\n"
4180                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4181                "      aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
4182                "}",
4183                NoBinPacking);
4184 
4185   verifyFormat(
4186       "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4187       "             aaaaaaaaaaaa,\n"
4188       "             aaaaaaaaaaaa);",
4189       NoBinPacking);
4190   verifyFormat(
4191       "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
4192       "                               ddddddddddddddddddddddddddddd),\n"
4193       "             test);",
4194       NoBinPacking);
4195 
4196   verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
4197                "            aaaaaaaaaaaaaaaaaaaaaaa,\n"
4198                "            aaaaaaaaaaaaaaaaaaaaaaa>\n"
4199                "    aaaaaaaaaaaaaaaaaa;",
4200                NoBinPacking);
4201   verifyFormat("a(\"a\"\n"
4202                "  \"a\",\n"
4203                "  a);");
4204 
4205   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
4206   verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
4207                "                aaaaaaaaa,\n"
4208                "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4209                NoBinPacking);
4210   verifyFormat(
4211       "void f() {\n"
4212       "  aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
4213       "      .aaaaaaa();\n"
4214       "}",
4215       NoBinPacking);
4216   verifyFormat(
4217       "template <class SomeType, class SomeOtherType>\n"
4218       "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
4219       NoBinPacking);
4220 }
4221 
4222 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
4223   FormatStyle Style = getLLVMStyleWithColumns(15);
4224   Style.ExperimentalAutoDetectBinPacking = true;
4225   EXPECT_EQ("aaa(aaaa,\n"
4226             "    aaaa,\n"
4227             "    aaaa);\n"
4228             "aaa(aaaa,\n"
4229             "    aaaa,\n"
4230             "    aaaa);",
4231             format("aaa(aaaa,\n" // one-per-line
4232                    "  aaaa,\n"
4233                    "    aaaa  );\n"
4234                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
4235                    Style));
4236   EXPECT_EQ("aaa(aaaa, aaaa,\n"
4237             "    aaaa);\n"
4238             "aaa(aaaa, aaaa,\n"
4239             "    aaaa);",
4240             format("aaa(aaaa,  aaaa,\n" // bin-packed
4241                    "    aaaa  );\n"
4242                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
4243                    Style));
4244 }
4245 
4246 TEST_F(FormatTest, FormatsBuilderPattern) {
4247   verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
4248                "    .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
4249                "    .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
4250                "    .StartsWith(\".init\", ORDER_INIT)\n"
4251                "    .StartsWith(\".fini\", ORDER_FINI)\n"
4252                "    .StartsWith(\".hash\", ORDER_HASH)\n"
4253                "    .Default(ORDER_TEXT);\n");
4254 
4255   verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
4256                "       aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
4257   verifyFormat(
4258       "aaaaaaa->aaaaaaa\n"
4259       "    ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4260       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4261       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
4262   verifyFormat(
4263       "aaaaaaa->aaaaaaa\n"
4264       "    ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4265       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
4266   verifyFormat(
4267       "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
4268       "    aaaaaaaaaaaaaa);");
4269   verifyFormat(
4270       "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
4271       "    aaaaaa->aaaaaaaaaaaa()\n"
4272       "        ->aaaaaaaaaaaaaaaa(\n"
4273       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4274       "        ->aaaaaaaaaaaaaaaaa();");
4275   verifyGoogleFormat(
4276       "void f() {\n"
4277       "  someo->Add((new util::filetools::Handler(dir))\n"
4278       "                 ->OnEvent1(NewPermanentCallback(\n"
4279       "                     this, &HandlerHolderClass::EventHandlerCBA))\n"
4280       "                 ->OnEvent2(NewPermanentCallback(\n"
4281       "                     this, &HandlerHolderClass::EventHandlerCBB))\n"
4282       "                 ->OnEvent3(NewPermanentCallback(\n"
4283       "                     this, &HandlerHolderClass::EventHandlerCBC))\n"
4284       "                 ->OnEvent5(NewPermanentCallback(\n"
4285       "                     this, &HandlerHolderClass::EventHandlerCBD))\n"
4286       "                 ->OnEvent6(NewPermanentCallback(\n"
4287       "                     this, &HandlerHolderClass::EventHandlerCBE)));\n"
4288       "}");
4289 
4290   verifyFormat(
4291       "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
4292   verifyFormat("aaaaaaaaaaaaaaa()\n"
4293                "    .aaaaaaaaaaaaaaa()\n"
4294                "    .aaaaaaaaaaaaaaa()\n"
4295                "    .aaaaaaaaaaaaaaa()\n"
4296                "    .aaaaaaaaaaaaaaa();");
4297   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4298                "    .aaaaaaaaaaaaaaa()\n"
4299                "    .aaaaaaaaaaaaaaa()\n"
4300                "    .aaaaaaaaaaaaaaa();");
4301   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4302                "    .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4303                "    .aaaaaaaaaaaaaaa();");
4304   verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
4305                "    ->aaaaaaaaaaaaaae(0)\n"
4306                "    ->aaaaaaaaaaaaaaa();");
4307 
4308   // Don't linewrap after very short segments.
4309   verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4310                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4311                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4312   verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4313                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4314                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4315   verifyFormat("aaa()\n"
4316                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4317                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4318                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4319 
4320   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
4321                "    .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4322                "    .has<bbbbbbbbbbbbbbbbbbbbb>();");
4323   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
4324                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
4325                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
4326 
4327   // Prefer not to break after empty parentheses.
4328   verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
4329                "    First->LastNewlineOffset);");
4330 
4331   // Prefer not to create "hanging" indents.
4332   verifyFormat(
4333       "return !soooooooooooooome_map\n"
4334       "            .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4335       "            .second;");
4336   verifyFormat(
4337       "return aaaaaaaaaaaaaaaa\n"
4338       "    .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n"
4339       "    .aaaa(aaaaaaaaaaaaaa);");
4340   // No hanging indent here.
4341   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n"
4342                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4343   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n"
4344                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4345   verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
4346                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4347                getLLVMStyleWithColumns(60));
4348   verifyFormat("aaaaaaaaaaaaaaaaaa\n"
4349                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
4350                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4351                getLLVMStyleWithColumns(59));
4352   verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4353                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4354                "    .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4355 }
4356 
4357 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
4358   verifyFormat(
4359       "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
4360       "    bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
4361   verifyFormat(
4362       "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
4363       "    bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
4364 
4365   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
4366                "    ccccccccccccccccccccccccc) {\n}");
4367   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
4368                "    ccccccccccccccccccccccccc) {\n}");
4369 
4370   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
4371                "    ccccccccccccccccccccccccc) {\n}");
4372   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
4373                "    ccccccccccccccccccccccccc) {\n}");
4374 
4375   verifyFormat(
4376       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
4377       "    ccccccccccccccccccccccccc) {\n}");
4378   verifyFormat(
4379       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
4380       "    ccccccccccccccccccccccccc) {\n}");
4381 
4382   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
4383                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
4384                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
4385                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
4386   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
4387                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
4388                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
4389                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
4390 
4391   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
4392                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
4393                "    aaaaaaaaaaaaaaa != aa) {\n}");
4394   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
4395                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
4396                "    aaaaaaaaaaaaaaa != aa) {\n}");
4397 }
4398 
4399 TEST_F(FormatTest, BreaksAfterAssignments) {
4400   verifyFormat(
4401       "unsigned Cost =\n"
4402       "    TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
4403       "                        SI->getPointerAddressSpaceee());\n");
4404   verifyFormat(
4405       "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
4406       "    Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
4407 
4408   verifyFormat(
4409       "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
4410       "    aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
4411   verifyFormat("unsigned OriginalStartColumn =\n"
4412                "    SourceMgr.getSpellingColumnNumber(\n"
4413                "        Current.FormatTok.getStartOfNonWhitespace()) -\n"
4414                "    1;");
4415 }
4416 
4417 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) {
4418   FormatStyle Style = getLLVMStyle();
4419   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4420                "    bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;",
4421                Style);
4422 
4423   Style.PenaltyBreakAssignment = 20;
4424   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
4425                "                                 cccccccccccccccccccccccccc;",
4426                Style);
4427 }
4428 
4429 TEST_F(FormatTest, AlignsAfterAssignments) {
4430   verifyFormat(
4431       "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4432       "             aaaaaaaaaaaaaaaaaaaaaaaaa;");
4433   verifyFormat(
4434       "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4435       "          aaaaaaaaaaaaaaaaaaaaaaaaa;");
4436   verifyFormat(
4437       "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4438       "           aaaaaaaaaaaaaaaaaaaaaaaaa;");
4439   verifyFormat(
4440       "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4441       "              aaaaaaaaaaaaaaaaaaaaaaaaa);");
4442   verifyFormat(
4443       "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
4444       "                                            aaaaaaaaaaaaaaaaaaaaaaaa +\n"
4445       "                                            aaaaaaaaaaaaaaaaaaaaaaaa;");
4446 }
4447 
4448 TEST_F(FormatTest, AlignsAfterReturn) {
4449   verifyFormat(
4450       "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4451       "       aaaaaaaaaaaaaaaaaaaaaaaaa;");
4452   verifyFormat(
4453       "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4454       "        aaaaaaaaaaaaaaaaaaaaaaaaa);");
4455   verifyFormat(
4456       "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
4457       "       aaaaaaaaaaaaaaaaaaaaaa();");
4458   verifyFormat(
4459       "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
4460       "        aaaaaaaaaaaaaaaaaaaaaa());");
4461   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4462                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4463   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4464                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
4465                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4466   verifyFormat("return\n"
4467                "    // true if code is one of a or b.\n"
4468                "    code == a || code == b;");
4469 }
4470 
4471 TEST_F(FormatTest, AlignsAfterOpenBracket) {
4472   verifyFormat(
4473       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
4474       "                                                aaaaaaaaa aaaaaaa) {}");
4475   verifyFormat(
4476       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
4477       "                                               aaaaaaaaaaa aaaaaaaaa);");
4478   verifyFormat(
4479       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
4480       "                                             aaaaaaaaaaaaaaaaaaaaa));");
4481   FormatStyle Style = getLLVMStyle();
4482   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4483   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4484                "    aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
4485                Style);
4486   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
4487                "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
4488                Style);
4489   verifyFormat("SomeLongVariableName->someFunction(\n"
4490                "    foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
4491                Style);
4492   verifyFormat(
4493       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
4494       "    aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4495       Style);
4496   verifyFormat(
4497       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
4498       "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4499       Style);
4500   verifyFormat(
4501       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
4502       "    aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
4503       Style);
4504 
4505   verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n"
4506                "    ccccccc(aaaaaaaaaaaaaaaaa,         //\n"
4507                "        b));",
4508                Style);
4509 
4510   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
4511   Style.BinPackArguments = false;
4512   Style.BinPackParameters = false;
4513   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4514                "    aaaaaaaaaaa aaaaaaaa,\n"
4515                "    aaaaaaaaa aaaaaaa,\n"
4516                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4517                Style);
4518   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
4519                "    aaaaaaaaaaa aaaaaaaaa,\n"
4520                "    aaaaaaaaaaa aaaaaaaaa,\n"
4521                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4522                Style);
4523   verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
4524                "    aaaaaaaaaaaaaaa,\n"
4525                "    aaaaaaaaaaaaaaaaaaaaa,\n"
4526                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
4527                Style);
4528   verifyFormat(
4529       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
4530       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
4531       Style);
4532   verifyFormat(
4533       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
4534       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
4535       Style);
4536   verifyFormat(
4537       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
4538       "    aaaaaaaaaaaaaaaaaaaaa(\n"
4539       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n"
4540       "    aaaaaaaaaaaaaaaa);",
4541       Style);
4542   verifyFormat(
4543       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
4544       "    aaaaaaaaaaaaaaaaaaaaa(\n"
4545       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n"
4546       "    aaaaaaaaaaaaaaaa);",
4547       Style);
4548 }
4549 
4550 TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
4551   FormatStyle Style = getLLVMStyleWithColumns(40);
4552   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4553                "          bbbbbbbbbbbbbbbbbbbbbb);",
4554                Style);
4555   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
4556   Style.AlignOperands = false;
4557   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4558                "          bbbbbbbbbbbbbbbbbbbbbb);",
4559                Style);
4560   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4561   Style.AlignOperands = true;
4562   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4563                "          bbbbbbbbbbbbbbbbbbbbbb);",
4564                Style);
4565   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4566   Style.AlignOperands = false;
4567   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4568                "    bbbbbbbbbbbbbbbbbbbbbb);",
4569                Style);
4570 }
4571 
4572 TEST_F(FormatTest, BreaksConditionalExpressions) {
4573   verifyFormat(
4574       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4575       "                               ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4576       "                               : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4577   verifyFormat(
4578       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
4579       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4580       "                                : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4581   verifyFormat(
4582       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4583       "                                   : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4584   verifyFormat(
4585       "aaaa(aaaaaaaaa, aaaaaaaaa,\n"
4586       "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4587       "             : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4588   verifyFormat(
4589       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
4590       "                                                    : aaaaaaaaaaaaa);");
4591   verifyFormat(
4592       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4593       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4594       "                                    : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4595       "                   aaaaaaaaaaaaa);");
4596   verifyFormat(
4597       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4598       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4599       "                   aaaaaaaaaaaaa);");
4600   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4601                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4602                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4603                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4604                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4605   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4606                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4607                "           ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4608                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4609                "           : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4610                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4611                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4612   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4613                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4614                "           ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4615                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4616                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4617   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4618                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4619                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4620   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
4621                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4622                "        ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4623                "        : aaaaaaaaaaaaaaaa;");
4624   verifyFormat(
4625       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4626       "    ? aaaaaaaaaaaaaaa\n"
4627       "    : aaaaaaaaaaaaaaa;");
4628   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
4629                "          aaaaaaaaa\n"
4630                "      ? b\n"
4631                "      : c);");
4632   verifyFormat("return aaaa == bbbb\n"
4633                "           // comment\n"
4634                "           ? aaaa\n"
4635                "           : bbbb;");
4636   verifyFormat("unsigned Indent =\n"
4637                "    format(TheLine.First,\n"
4638                "           IndentForLevel[TheLine.Level] >= 0\n"
4639                "               ? IndentForLevel[TheLine.Level]\n"
4640                "               : TheLine * 2,\n"
4641                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
4642                getLLVMStyleWithColumns(60));
4643   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
4644                "                  ? aaaaaaaaaaaaaaa\n"
4645                "                  : bbbbbbbbbbbbbbb //\n"
4646                "                        ? ccccccccccccccc\n"
4647                "                        : ddddddddddddddd;");
4648   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
4649                "                  ? aaaaaaaaaaaaaaa\n"
4650                "                  : (bbbbbbbbbbbbbbb //\n"
4651                "                         ? ccccccccccccccc\n"
4652                "                         : ddddddddddddddd);");
4653   verifyFormat(
4654       "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4655       "                                      ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4656       "                                            aaaaaaaaaaaaaaaaaaaaa +\n"
4657       "                                            aaaaaaaaaaaaaaaaaaaaa\n"
4658       "                                      : aaaaaaaaaa;");
4659   verifyFormat(
4660       "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4661       "                                   : aaaaaaaaaaaaaaaaaaaaaa\n"
4662       "                      : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4663 
4664   FormatStyle NoBinPacking = getLLVMStyle();
4665   NoBinPacking.BinPackArguments = false;
4666   verifyFormat(
4667       "void f() {\n"
4668       "  g(aaa,\n"
4669       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
4670       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4671       "        ? aaaaaaaaaaaaaaa\n"
4672       "        : aaaaaaaaaaaaaaa);\n"
4673       "}",
4674       NoBinPacking);
4675   verifyFormat(
4676       "void f() {\n"
4677       "  g(aaa,\n"
4678       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
4679       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4680       "        ?: aaaaaaaaaaaaaaa);\n"
4681       "}",
4682       NoBinPacking);
4683 
4684   verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
4685                "             // comment.\n"
4686                "             ccccccccccccccccccccccccccccccccccccccc\n"
4687                "                 ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4688                "                 : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
4689 
4690   // Assignments in conditional expressions. Apparently not uncommon :-(.
4691   verifyFormat("return a != b\n"
4692                "           // comment\n"
4693                "           ? a = b\n"
4694                "           : a = b;");
4695   verifyFormat("return a != b\n"
4696                "           // comment\n"
4697                "           ? a = a != b\n"
4698                "                     // comment\n"
4699                "                     ? a = b\n"
4700                "                     : a\n"
4701                "           : a;\n");
4702   verifyFormat("return a != b\n"
4703                "           // comment\n"
4704                "           ? a\n"
4705                "           : a = a != b\n"
4706                "                     // comment\n"
4707                "                     ? a = b\n"
4708                "                     : a;");
4709 }
4710 
4711 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
4712   FormatStyle Style = getLLVMStyle();
4713   Style.BreakBeforeTernaryOperators = false;
4714   Style.ColumnLimit = 70;
4715   verifyFormat(
4716       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4717       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4718       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4719       Style);
4720   verifyFormat(
4721       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
4722       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4723       "                                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4724       Style);
4725   verifyFormat(
4726       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4727       "                                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4728       Style);
4729   verifyFormat(
4730       "aaaa(aaaaaaaa, aaaaaaaaaa,\n"
4731       "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4732       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4733       Style);
4734   verifyFormat(
4735       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
4736       "                                                      aaaaaaaaaaaaa);",
4737       Style);
4738   verifyFormat(
4739       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4740       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4741       "                                      aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4742       "                   aaaaaaaaaaaaa);",
4743       Style);
4744   verifyFormat(
4745       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4746       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4747       "                   aaaaaaaaaaaaa);",
4748       Style);
4749   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4750                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4751                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
4752                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4753                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4754                Style);
4755   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4756                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4757                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4758                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
4759                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4760                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4761                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4762                Style);
4763   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4764                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
4765                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4766                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4767                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4768                Style);
4769   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4770                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4771                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4772                Style);
4773   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
4774                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4775                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4776                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4777                Style);
4778   verifyFormat(
4779       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4780       "    aaaaaaaaaaaaaaa :\n"
4781       "    aaaaaaaaaaaaaaa;",
4782       Style);
4783   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
4784                "          aaaaaaaaa ?\n"
4785                "      b :\n"
4786                "      c);",
4787                Style);
4788   verifyFormat("unsigned Indent =\n"
4789                "    format(TheLine.First,\n"
4790                "           IndentForLevel[TheLine.Level] >= 0 ?\n"
4791                "               IndentForLevel[TheLine.Level] :\n"
4792                "               TheLine * 2,\n"
4793                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
4794                Style);
4795   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
4796                "                  aaaaaaaaaaaaaaa :\n"
4797                "                  bbbbbbbbbbbbbbb ? //\n"
4798                "                      ccccccccccccccc :\n"
4799                "                      ddddddddddddddd;",
4800                Style);
4801   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
4802                "                  aaaaaaaaaaaaaaa :\n"
4803                "                  (bbbbbbbbbbbbbbb ? //\n"
4804                "                       ccccccccccccccc :\n"
4805                "                       ddddddddddddddd);",
4806                Style);
4807   verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4808                "            /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n"
4809                "            ccccccccccccccccccccccccccc;",
4810                Style);
4811   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4812                "           aaaaa :\n"
4813                "           bbbbbbbbbbbbbbb + cccccccccccccccc;",
4814                Style);
4815 }
4816 
4817 TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
4818   verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
4819                "     aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
4820   verifyFormat("bool a = true, b = false;");
4821 
4822   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4823                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
4824                "     bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
4825                "         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
4826   verifyFormat(
4827       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
4828       "         bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
4829       "     d = e && f;");
4830   verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
4831                "          c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
4832   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
4833                "          *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
4834   verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
4835                "          ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
4836 
4837   FormatStyle Style = getGoogleStyle();
4838   Style.PointerAlignment = FormatStyle::PAS_Left;
4839   Style.DerivePointerAlignment = false;
4840   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4841                "    *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
4842                "    *b = bbbbbbbbbbbbbbbbbbb;",
4843                Style);
4844   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
4845                "          *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
4846                Style);
4847   verifyFormat("vector<int*> a, b;", Style);
4848   verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style);
4849 }
4850 
4851 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
4852   verifyFormat("arr[foo ? bar : baz];");
4853   verifyFormat("f()[foo ? bar : baz];");
4854   verifyFormat("(a + b)[foo ? bar : baz];");
4855   verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
4856 }
4857 
4858 TEST_F(FormatTest, AlignsStringLiterals) {
4859   verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
4860                "                                      \"short literal\");");
4861   verifyFormat(
4862       "looooooooooooooooooooooooongFunction(\n"
4863       "    \"short literal\"\n"
4864       "    \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
4865   verifyFormat("someFunction(\"Always break between multi-line\"\n"
4866                "             \" string literals\",\n"
4867                "             and, other, parameters);");
4868   EXPECT_EQ("fun + \"1243\" /* comment */\n"
4869             "      \"5678\";",
4870             format("fun + \"1243\" /* comment */\n"
4871                    "    \"5678\";",
4872                    getLLVMStyleWithColumns(28)));
4873   EXPECT_EQ(
4874       "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
4875       "         \"aaaaaaaaaaaaaaaaaaaaa\"\n"
4876       "         \"aaaaaaaaaaaaaaaa\";",
4877       format("aaaaaa ="
4878              "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
4879              "aaaaaaaaaaaaaaaaaaaaa\" "
4880              "\"aaaaaaaaaaaaaaaa\";"));
4881   verifyFormat("a = a + \"a\"\n"
4882                "        \"a\"\n"
4883                "        \"a\";");
4884   verifyFormat("f(\"a\", \"b\"\n"
4885                "       \"c\");");
4886 
4887   verifyFormat(
4888       "#define LL_FORMAT \"ll\"\n"
4889       "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
4890       "       \"d, ddddddddd: %\" LL_FORMAT \"d\");");
4891 
4892   verifyFormat("#define A(X)          \\\n"
4893                "  \"aaaaa\" #X \"bbbbbb\" \\\n"
4894                "  \"ccccc\"",
4895                getLLVMStyleWithColumns(23));
4896   verifyFormat("#define A \"def\"\n"
4897                "f(\"abc\" A \"ghi\"\n"
4898                "  \"jkl\");");
4899 
4900   verifyFormat("f(L\"a\"\n"
4901                "  L\"b\");");
4902   verifyFormat("#define A(X)            \\\n"
4903                "  L\"aaaaa\" #X L\"bbbbbb\" \\\n"
4904                "  L\"ccccc\"",
4905                getLLVMStyleWithColumns(25));
4906 
4907   verifyFormat("f(@\"a\"\n"
4908                "  @\"b\");");
4909   verifyFormat("NSString s = @\"a\"\n"
4910                "             @\"b\"\n"
4911                "             @\"c\";");
4912   verifyFormat("NSString s = @\"a\"\n"
4913                "              \"b\"\n"
4914                "              \"c\";");
4915 }
4916 
4917 TEST_F(FormatTest, ReturnTypeBreakingStyle) {
4918   FormatStyle Style = getLLVMStyle();
4919   // No declarations or definitions should be moved to own line.
4920   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None;
4921   verifyFormat("class A {\n"
4922                "  int f() { return 1; }\n"
4923                "  int g();\n"
4924                "};\n"
4925                "int f() { return 1; }\n"
4926                "int g();\n",
4927                Style);
4928 
4929   // All declarations and definitions should have the return type moved to its
4930   // own
4931   // line.
4932   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
4933   verifyFormat("class E {\n"
4934                "  int\n"
4935                "  f() {\n"
4936                "    return 1;\n"
4937                "  }\n"
4938                "  int\n"
4939                "  g();\n"
4940                "};\n"
4941                "int\n"
4942                "f() {\n"
4943                "  return 1;\n"
4944                "}\n"
4945                "int\n"
4946                "g();\n",
4947                Style);
4948 
4949   // Top-level definitions, and no kinds of declarations should have the
4950   // return type moved to its own line.
4951   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions;
4952   verifyFormat("class B {\n"
4953                "  int f() { return 1; }\n"
4954                "  int g();\n"
4955                "};\n"
4956                "int\n"
4957                "f() {\n"
4958                "  return 1;\n"
4959                "}\n"
4960                "int g();\n",
4961                Style);
4962 
4963   // Top-level definitions and declarations should have the return type moved
4964   // to its own line.
4965   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel;
4966   verifyFormat("class C {\n"
4967                "  int f() { return 1; }\n"
4968                "  int g();\n"
4969                "};\n"
4970                "int\n"
4971                "f() {\n"
4972                "  return 1;\n"
4973                "}\n"
4974                "int\n"
4975                "g();\n",
4976                Style);
4977 
4978   // All definitions should have the return type moved to its own line, but no
4979   // kinds of declarations.
4980   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
4981   verifyFormat("class D {\n"
4982                "  int\n"
4983                "  f() {\n"
4984                "    return 1;\n"
4985                "  }\n"
4986                "  int g();\n"
4987                "};\n"
4988                "int\n"
4989                "f() {\n"
4990                "  return 1;\n"
4991                "}\n"
4992                "int g();\n",
4993                Style);
4994   verifyFormat("const char *\n"
4995                "f(void) {\n" // Break here.
4996                "  return \"\";\n"
4997                "}\n"
4998                "const char *bar(void);\n", // No break here.
4999                Style);
5000   verifyFormat("template <class T>\n"
5001                "T *\n"
5002                "f(T &c) {\n" // Break here.
5003                "  return NULL;\n"
5004                "}\n"
5005                "template <class T> T *f(T &c);\n", // No break here.
5006                Style);
5007   verifyFormat("class C {\n"
5008                "  int\n"
5009                "  operator+() {\n"
5010                "    return 1;\n"
5011                "  }\n"
5012                "  int\n"
5013                "  operator()() {\n"
5014                "    return 1;\n"
5015                "  }\n"
5016                "};\n",
5017                Style);
5018   verifyFormat("void\n"
5019                "A::operator()() {}\n"
5020                "void\n"
5021                "A::operator>>() {}\n"
5022                "void\n"
5023                "A::operator+() {}\n",
5024                Style);
5025   verifyFormat("void *operator new(std::size_t s);", // No break here.
5026                Style);
5027   verifyFormat("void *\n"
5028                "operator new(std::size_t s) {}",
5029                Style);
5030   verifyFormat("void *\n"
5031                "operator delete[](void *ptr) {}",
5032                Style);
5033   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
5034   verifyFormat("const char *\n"
5035                "f(void)\n" // Break here.
5036                "{\n"
5037                "  return \"\";\n"
5038                "}\n"
5039                "const char *bar(void);\n", // No break here.
5040                Style);
5041   verifyFormat("template <class T>\n"
5042                "T *\n"     // Problem here: no line break
5043                "f(T &c)\n" // Break here.
5044                "{\n"
5045                "  return NULL;\n"
5046                "}\n"
5047                "template <class T> T *f(T &c);\n", // No break here.
5048                Style);
5049 }
5050 
5051 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
5052   FormatStyle NoBreak = getLLVMStyle();
5053   NoBreak.AlwaysBreakBeforeMultilineStrings = false;
5054   FormatStyle Break = getLLVMStyle();
5055   Break.AlwaysBreakBeforeMultilineStrings = true;
5056   verifyFormat("aaaa = \"bbbb\"\n"
5057                "       \"cccc\";",
5058                NoBreak);
5059   verifyFormat("aaaa =\n"
5060                "    \"bbbb\"\n"
5061                "    \"cccc\";",
5062                Break);
5063   verifyFormat("aaaa(\"bbbb\"\n"
5064                "     \"cccc\");",
5065                NoBreak);
5066   verifyFormat("aaaa(\n"
5067                "    \"bbbb\"\n"
5068                "    \"cccc\");",
5069                Break);
5070   verifyFormat("aaaa(qqq, \"bbbb\"\n"
5071                "          \"cccc\");",
5072                NoBreak);
5073   verifyFormat("aaaa(qqq,\n"
5074                "     \"bbbb\"\n"
5075                "     \"cccc\");",
5076                Break);
5077   verifyFormat("aaaa(qqq,\n"
5078                "     L\"bbbb\"\n"
5079                "     L\"cccc\");",
5080                Break);
5081   verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
5082                "                      \"bbbb\"));",
5083                Break);
5084   verifyFormat("string s = someFunction(\n"
5085                "    \"abc\"\n"
5086                "    \"abc\");",
5087                Break);
5088 
5089   // As we break before unary operators, breaking right after them is bad.
5090   verifyFormat("string foo = abc ? \"x\"\n"
5091                "                   \"blah blah blah blah blah blah\"\n"
5092                "                 : \"y\";",
5093                Break);
5094 
5095   // Don't break if there is no column gain.
5096   verifyFormat("f(\"aaaa\"\n"
5097                "  \"bbbb\");",
5098                Break);
5099 
5100   // Treat literals with escaped newlines like multi-line string literals.
5101   EXPECT_EQ("x = \"a\\\n"
5102             "b\\\n"
5103             "c\";",
5104             format("x = \"a\\\n"
5105                    "b\\\n"
5106                    "c\";",
5107                    NoBreak));
5108   EXPECT_EQ("xxxx =\n"
5109             "    \"a\\\n"
5110             "b\\\n"
5111             "c\";",
5112             format("xxxx = \"a\\\n"
5113                    "b\\\n"
5114                    "c\";",
5115                    Break));
5116 
5117   EXPECT_EQ("NSString *const kString =\n"
5118             "    @\"aaaa\"\n"
5119             "    @\"bbbb\";",
5120             format("NSString *const kString = @\"aaaa\"\n"
5121                    "@\"bbbb\";",
5122                    Break));
5123 
5124   Break.ColumnLimit = 0;
5125   verifyFormat("const char *hello = \"hello llvm\";", Break);
5126 }
5127 
5128 TEST_F(FormatTest, AlignsPipes) {
5129   verifyFormat(
5130       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5131       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5132       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5133   verifyFormat(
5134       "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
5135       "                     << aaaaaaaaaaaaaaaaaaaa;");
5136   verifyFormat(
5137       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5138       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5139   verifyFormat(
5140       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
5141       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5142   verifyFormat(
5143       "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
5144       "                \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
5145       "             << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
5146   verifyFormat(
5147       "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5148       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5149       "         << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5150   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5151                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5152                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5153                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
5154   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
5155                "             << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
5156   verifyFormat(
5157       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5158       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5159   verifyFormat(
5160       "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n"
5161       "                                       aaaaaaaaaaaaaaaaaaaaaaaaaa);");
5162 
5163   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
5164                "             << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
5165   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5166                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5167                "                    aaaaaaaaaaaaaaaaaaaaa)\n"
5168                "             << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
5169   verifyFormat("LOG_IF(aaa == //\n"
5170                "       bbb)\n"
5171                "    << a << b;");
5172 
5173   // But sometimes, breaking before the first "<<" is desirable.
5174   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
5175                "    << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
5176   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
5177                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5178                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5179   verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
5180                "    << BEF << IsTemplate << Description << E->getType();");
5181   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
5182                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5183                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5184   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
5185                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5186                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5187                "    << aaa;");
5188 
5189   verifyFormat(
5190       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5191       "                    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5192 
5193   // Incomplete string literal.
5194   EXPECT_EQ("llvm::errs() << \"\n"
5195             "             << a;",
5196             format("llvm::errs() << \"\n<<a;"));
5197 
5198   verifyFormat("void f() {\n"
5199                "  CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
5200                "      << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
5201                "}");
5202 
5203   // Handle 'endl'.
5204   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
5205                "             << bbbbbbbbbbbbbbbbbbbbbb << endl;");
5206   verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
5207 
5208   // Handle '\n'.
5209   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n"
5210                "             << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
5211   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n"
5212                "             << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';");
5213   verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n"
5214                "             << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";");
5215   verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
5216 }
5217 
5218 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) {
5219   verifyFormat("return out << \"somepacket = {\\n\"\n"
5220                "           << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
5221                "           << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
5222                "           << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
5223                "           << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
5224                "           << \"}\";");
5225 
5226   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
5227                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
5228                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
5229   verifyFormat(
5230       "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
5231       "             << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
5232       "             << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
5233       "             << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
5234       "             << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
5235   verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
5236                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
5237   verifyFormat(
5238       "void f() {\n"
5239       "  llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
5240       "               << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
5241       "}");
5242 
5243   // Breaking before the first "<<" is generally not desirable.
5244   verifyFormat(
5245       "llvm::errs()\n"
5246       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5247       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5248       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5249       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
5250       getLLVMStyleWithColumns(70));
5251   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
5252                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5253                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
5254                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5255                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
5256                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
5257                getLLVMStyleWithColumns(70));
5258 
5259   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
5260                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
5261                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;");
5262   verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
5263                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
5264                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);");
5265   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n"
5266                "           (aaaa + aaaa);",
5267                getLLVMStyleWithColumns(40));
5268   verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n"
5269                "                  (aaaaaaa + aaaaa));",
5270                getLLVMStyleWithColumns(40));
5271   verifyFormat(
5272       "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n"
5273       "                  SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n"
5274       "                  bbbbbbbbbbbbbbbbbbbbbbb);");
5275 }
5276 
5277 TEST_F(FormatTest, UnderstandsEquals) {
5278   verifyFormat(
5279       "aaaaaaaaaaaaaaaaa =\n"
5280       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5281   verifyFormat(
5282       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5283       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
5284   verifyFormat(
5285       "if (a) {\n"
5286       "  f();\n"
5287       "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5288       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
5289       "}");
5290 
5291   verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5292                "        100000000 + 10000000) {\n}");
5293 }
5294 
5295 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
5296   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
5297                "    .looooooooooooooooooooooooooooooooooooooongFunction();");
5298 
5299   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
5300                "    ->looooooooooooooooooooooooooooooooooooooongFunction();");
5301 
5302   verifyFormat(
5303       "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
5304       "                                                          Parameter2);");
5305 
5306   verifyFormat(
5307       "ShortObject->shortFunction(\n"
5308       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
5309       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
5310 
5311   verifyFormat("loooooooooooooongFunction(\n"
5312                "    LoooooooooooooongObject->looooooooooooooooongFunction());");
5313 
5314   verifyFormat(
5315       "function(LoooooooooooooooooooooooooooooooooooongObject\n"
5316       "             ->loooooooooooooooooooooooooooooooooooooooongFunction());");
5317 
5318   verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
5319                "    .WillRepeatedly(Return(SomeValue));");
5320   verifyFormat("void f() {\n"
5321                "  EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
5322                "      .Times(2)\n"
5323                "      .WillRepeatedly(Return(SomeValue));\n"
5324                "}");
5325   verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
5326                "    ccccccccccccccccccccccc);");
5327   verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5328                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5329                "          .aaaaa(aaaaa),\n"
5330                "      aaaaaaaaaaaaaaaaaaaaa);");
5331   verifyFormat("void f() {\n"
5332                "  aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5333                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
5334                "}");
5335   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5336                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5337                "    .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5338                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5339                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
5340   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5341                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5342                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5343                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
5344                "}");
5345 
5346   // Here, it is not necessary to wrap at "." or "->".
5347   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
5348                "    aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
5349   verifyFormat(
5350       "aaaaaaaaaaa->aaaaaaaaa(\n"
5351       "    aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5352       "    aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
5353 
5354   verifyFormat(
5355       "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5356       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
5357   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
5358                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
5359   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
5360                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
5361 
5362   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5363                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5364                "    .a();");
5365 
5366   FormatStyle NoBinPacking = getLLVMStyle();
5367   NoBinPacking.BinPackParameters = false;
5368   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
5369                "    .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
5370                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
5371                "                         aaaaaaaaaaaaaaaaaaa,\n"
5372                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5373                NoBinPacking);
5374 
5375   // If there is a subsequent call, change to hanging indentation.
5376   verifyFormat(
5377       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5378       "                         aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
5379       "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5380   verifyFormat(
5381       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5382       "    aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
5383   verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5384                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5385                "                 .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5386   verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5387                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5388                "               .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
5389 }
5390 
5391 TEST_F(FormatTest, WrapsTemplateDeclarations) {
5392   verifyFormat("template <typename T>\n"
5393                "virtual void loooooooooooongFunction(int Param1, int Param2);");
5394   verifyFormat("template <typename T>\n"
5395                "// T should be one of {A, B}.\n"
5396                "virtual void loooooooooooongFunction(int Param1, int Param2);");
5397   verifyFormat(
5398       "template <typename T>\n"
5399       "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
5400   verifyFormat("template <typename T>\n"
5401                "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
5402                "       int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
5403   verifyFormat(
5404       "template <typename T>\n"
5405       "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
5406       "                                      int Paaaaaaaaaaaaaaaaaaaaram2);");
5407   verifyFormat(
5408       "template <typename T>\n"
5409       "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
5410       "                    aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
5411       "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5412   verifyFormat("template <typename T>\n"
5413                "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5414                "    int aaaaaaaaaaaaaaaaaaaaaa);");
5415   verifyFormat(
5416       "template <typename T1, typename T2 = char, typename T3 = char,\n"
5417       "          typename T4 = char>\n"
5418       "void f();");
5419   verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
5420                "          template <typename> class cccccccccccccccccccccc,\n"
5421                "          typename ddddddddddddd>\n"
5422                "class C {};");
5423   verifyFormat(
5424       "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
5425       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5426 
5427   verifyFormat("void f() {\n"
5428                "  a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
5429                "      a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
5430                "}");
5431 
5432   verifyFormat("template <typename T> class C {};");
5433   verifyFormat("template <typename T> void f();");
5434   verifyFormat("template <typename T> void f() {}");
5435   verifyFormat(
5436       "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
5437       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5438       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
5439       "    new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
5440       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5441       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
5442       "        bbbbbbbbbbbbbbbbbbbbbbbb);",
5443       getLLVMStyleWithColumns(72));
5444   EXPECT_EQ("static_cast<A< //\n"
5445             "    B> *>(\n"
5446             "\n"
5447             ");",
5448             format("static_cast<A<//\n"
5449                    "    B>*>(\n"
5450                    "\n"
5451                    "    );"));
5452   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5453                "    const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
5454 
5455   FormatStyle AlwaysBreak = getLLVMStyle();
5456   AlwaysBreak.AlwaysBreakTemplateDeclarations = true;
5457   verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
5458   verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
5459   verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
5460   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5461                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
5462                "    ccccccccccccccccccccccccccccccccccccccccccccccc);");
5463   verifyFormat("template <template <typename> class Fooooooo,\n"
5464                "          template <typename> class Baaaaaaar>\n"
5465                "struct C {};",
5466                AlwaysBreak);
5467   verifyFormat("template <typename T> // T can be A, B or C.\n"
5468                "struct C {};",
5469                AlwaysBreak);
5470   verifyFormat("template <enum E> class A {\n"
5471                "public:\n"
5472                "  E *f();\n"
5473                "};");
5474 }
5475 
5476 TEST_F(FormatTest, WrapsTemplateParameters) {
5477   FormatStyle Style = getLLVMStyle();
5478   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
5479   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
5480   verifyFormat(
5481       "template <typename... a> struct q {};\n"
5482       "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
5483       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
5484       "    y;",
5485       Style);
5486   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
5487   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
5488   verifyFormat(
5489       "template <typename... a> struct r {};\n"
5490       "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
5491       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
5492       "    y;",
5493       Style);
5494   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
5495   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
5496   verifyFormat(
5497       "template <typename... a> struct s {};\n"
5498       "extern s<\n"
5499       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
5500       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n"
5501       "    y;",
5502       Style);
5503   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
5504   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
5505   verifyFormat(
5506       "template <typename... a> struct t {};\n"
5507       "extern t<\n"
5508       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
5509       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n"
5510       "    y;",
5511       Style);
5512 }
5513 
5514 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
5515   verifyFormat(
5516       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5517       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5518   verifyFormat(
5519       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5520       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5521       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
5522 
5523   // FIXME: Should we have the extra indent after the second break?
5524   verifyFormat(
5525       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5526       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5527       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5528 
5529   verifyFormat(
5530       "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
5531       "                    cccccccccccccccccccccccccccccccccccccccccccccc());");
5532 
5533   // Breaking at nested name specifiers is generally not desirable.
5534   verifyFormat(
5535       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5536       "    aaaaaaaaaaaaaaaaaaaaaaa);");
5537 
5538   verifyFormat(
5539       "aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n"
5540       "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5541       "                       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5542       "                   aaaaaaaaaaaaaaaaaaaaa);",
5543       getLLVMStyleWithColumns(74));
5544 
5545   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5546                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5547                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5548 }
5549 
5550 TEST_F(FormatTest, UnderstandsTemplateParameters) {
5551   verifyFormat("A<int> a;");
5552   verifyFormat("A<A<A<int>>> a;");
5553   verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
5554   verifyFormat("bool x = a < 1 || 2 > a;");
5555   verifyFormat("bool x = 5 < f<int>();");
5556   verifyFormat("bool x = f<int>() > 5;");
5557   verifyFormat("bool x = 5 < a<int>::x;");
5558   verifyFormat("bool x = a < 4 ? a > 2 : false;");
5559   verifyFormat("bool x = f() ? a < 2 : a > 2;");
5560 
5561   verifyGoogleFormat("A<A<int>> a;");
5562   verifyGoogleFormat("A<A<A<int>>> a;");
5563   verifyGoogleFormat("A<A<A<A<int>>>> a;");
5564   verifyGoogleFormat("A<A<int> > a;");
5565   verifyGoogleFormat("A<A<A<int> > > a;");
5566   verifyGoogleFormat("A<A<A<A<int> > > > a;");
5567   verifyGoogleFormat("A<::A<int>> a;");
5568   verifyGoogleFormat("A<::A> a;");
5569   verifyGoogleFormat("A< ::A> a;");
5570   verifyGoogleFormat("A< ::A<int> > a;");
5571   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
5572   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
5573   EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
5574   EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
5575   EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };",
5576             format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle()));
5577 
5578   verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
5579 
5580   verifyFormat("test >> a >> b;");
5581   verifyFormat("test << a >> b;");
5582 
5583   verifyFormat("f<int>();");
5584   verifyFormat("template <typename T> void f() {}");
5585   verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
5586   verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
5587                "sizeof(char)>::type>;");
5588   verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
5589   verifyFormat("f(a.operator()<A>());");
5590   verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5591                "      .template operator()<A>());",
5592                getLLVMStyleWithColumns(35));
5593 
5594   // Not template parameters.
5595   verifyFormat("return a < b && c > d;");
5596   verifyFormat("void f() {\n"
5597                "  while (a < b && c > d) {\n"
5598                "  }\n"
5599                "}");
5600   verifyFormat("template <typename... Types>\n"
5601                "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
5602 
5603   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5604                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
5605                getLLVMStyleWithColumns(60));
5606   verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
5607   verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
5608   verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
5609 }
5610 
5611 TEST_F(FormatTest, BitshiftOperatorWidth) {
5612   EXPECT_EQ("int a = 1 << 2; /* foo\n"
5613             "                   bar */",
5614             format("int    a=1<<2;  /* foo\n"
5615                    "                   bar */"));
5616 
5617   EXPECT_EQ("int b = 256 >> 1; /* foo\n"
5618             "                     bar */",
5619             format("int  b  =256>>1 ;  /* foo\n"
5620                    "                      bar */"));
5621 }
5622 
5623 TEST_F(FormatTest, UnderstandsBinaryOperators) {
5624   verifyFormat("COMPARE(a, ==, b);");
5625   verifyFormat("auto s = sizeof...(Ts) - 1;");
5626 }
5627 
5628 TEST_F(FormatTest, UnderstandsPointersToMembers) {
5629   verifyFormat("int A::*x;");
5630   verifyFormat("int (S::*func)(void *);");
5631   verifyFormat("void f() { int (S::*func)(void *); }");
5632   verifyFormat("typedef bool *(Class::*Member)() const;");
5633   verifyFormat("void f() {\n"
5634                "  (a->*f)();\n"
5635                "  a->*x;\n"
5636                "  (a.*f)();\n"
5637                "  ((*a).*f)();\n"
5638                "  a.*x;\n"
5639                "}");
5640   verifyFormat("void f() {\n"
5641                "  (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
5642                "      aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
5643                "}");
5644   verifyFormat(
5645       "(aaaaaaaaaa->*bbbbbbb)(\n"
5646       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
5647   FormatStyle Style = getLLVMStyle();
5648   Style.PointerAlignment = FormatStyle::PAS_Left;
5649   verifyFormat("typedef bool* (Class::*Member)() const;", Style);
5650 }
5651 
5652 TEST_F(FormatTest, UnderstandsUnaryOperators) {
5653   verifyFormat("int a = -2;");
5654   verifyFormat("f(-1, -2, -3);");
5655   verifyFormat("a[-1] = 5;");
5656   verifyFormat("int a = 5 + -2;");
5657   verifyFormat("if (i == -1) {\n}");
5658   verifyFormat("if (i != -1) {\n}");
5659   verifyFormat("if (i > -1) {\n}");
5660   verifyFormat("if (i < -1) {\n}");
5661   verifyFormat("++(a->f());");
5662   verifyFormat("--(a->f());");
5663   verifyFormat("(a->f())++;");
5664   verifyFormat("a[42]++;");
5665   verifyFormat("if (!(a->f())) {\n}");
5666   verifyFormat("if (!+i) {\n}");
5667   verifyFormat("~&a;");
5668 
5669   verifyFormat("a-- > b;");
5670   verifyFormat("b ? -a : c;");
5671   verifyFormat("n * sizeof char16;");
5672   verifyFormat("n * alignof char16;", getGoogleStyle());
5673   verifyFormat("sizeof(char);");
5674   verifyFormat("alignof(char);", getGoogleStyle());
5675 
5676   verifyFormat("return -1;");
5677   verifyFormat("switch (a) {\n"
5678                "case -1:\n"
5679                "  break;\n"
5680                "}");
5681   verifyFormat("#define X -1");
5682   verifyFormat("#define X -kConstant");
5683 
5684   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
5685   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
5686 
5687   verifyFormat("int a = /* confusing comment */ -1;");
5688   // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
5689   verifyFormat("int a = i /* confusing comment */++;");
5690 }
5691 
5692 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
5693   verifyFormat("if (!aaaaaaaaaa( // break\n"
5694                "        aaaaa)) {\n"
5695                "}");
5696   verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
5697                "    aaaaa));");
5698   verifyFormat("*aaa = aaaaaaa( // break\n"
5699                "    bbbbbb);");
5700 }
5701 
5702 TEST_F(FormatTest, UnderstandsOverloadedOperators) {
5703   verifyFormat("bool operator<();");
5704   verifyFormat("bool operator>();");
5705   verifyFormat("bool operator=();");
5706   verifyFormat("bool operator==();");
5707   verifyFormat("bool operator!=();");
5708   verifyFormat("int operator+();");
5709   verifyFormat("int operator++();");
5710   verifyFormat("int operator++(int) volatile noexcept;");
5711   verifyFormat("bool operator,();");
5712   verifyFormat("bool operator();");
5713   verifyFormat("bool operator()();");
5714   verifyFormat("bool operator[]();");
5715   verifyFormat("operator bool();");
5716   verifyFormat("operator int();");
5717   verifyFormat("operator void *();");
5718   verifyFormat("operator SomeType<int>();");
5719   verifyFormat("operator SomeType<int, int>();");
5720   verifyFormat("operator SomeType<SomeType<int>>();");
5721   verifyFormat("void *operator new(std::size_t size);");
5722   verifyFormat("void *operator new[](std::size_t size);");
5723   verifyFormat("void operator delete(void *ptr);");
5724   verifyFormat("void operator delete[](void *ptr);");
5725   verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
5726                "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
5727   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n"
5728                "    aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;");
5729 
5730   verifyFormat(
5731       "ostream &operator<<(ostream &OutputStream,\n"
5732       "                    SomeReallyLongType WithSomeReallyLongValue);");
5733   verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
5734                "               const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
5735                "  return left.group < right.group;\n"
5736                "}");
5737   verifyFormat("SomeType &operator=(const SomeType &S);");
5738   verifyFormat("f.template operator()<int>();");
5739 
5740   verifyGoogleFormat("operator void*();");
5741   verifyGoogleFormat("operator SomeType<SomeType<int>>();");
5742   verifyGoogleFormat("operator ::A();");
5743 
5744   verifyFormat("using A::operator+;");
5745   verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
5746                "int i;");
5747 }
5748 
5749 TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
5750   verifyFormat("Deleted &operator=(const Deleted &) & = default;");
5751   verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
5752   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
5753   verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
5754   verifyFormat("Deleted &operator=(const Deleted &) &;");
5755   verifyFormat("Deleted &operator=(const Deleted &) &&;");
5756   verifyFormat("SomeType MemberFunction(const Deleted &) &;");
5757   verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
5758   verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
5759   verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
5760   verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
5761   verifyFormat("void Fn(T const &) const &;");
5762   verifyFormat("void Fn(T const volatile &&) const volatile &&;");
5763   verifyFormat("template <typename T>\n"
5764                "void F(T) && = delete;",
5765                getGoogleStyle());
5766 
5767   FormatStyle AlignLeft = getLLVMStyle();
5768   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
5769   verifyFormat("void A::b() && {}", AlignLeft);
5770   verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft);
5771   verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
5772                AlignLeft);
5773   verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft);
5774   verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft);
5775   verifyFormat("auto Function(T t) & -> void {}", AlignLeft);
5776   verifyFormat("auto Function(T... t) & -> void {}", AlignLeft);
5777   verifyFormat("auto Function(T) & -> void {}", AlignLeft);
5778   verifyFormat("auto Function(T) & -> void;", AlignLeft);
5779   verifyFormat("void Fn(T const&) const&;", AlignLeft);
5780   verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft);
5781 
5782   FormatStyle Spaces = getLLVMStyle();
5783   Spaces.SpacesInCStyleCastParentheses = true;
5784   verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
5785   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
5786   verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
5787   verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
5788 
5789   Spaces.SpacesInCStyleCastParentheses = false;
5790   Spaces.SpacesInParentheses = true;
5791   verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
5792   verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces);
5793   verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
5794   verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
5795 }
5796 
5797 TEST_F(FormatTest, UnderstandsNewAndDelete) {
5798   verifyFormat("void f() {\n"
5799                "  A *a = new A;\n"
5800                "  A *a = new (placement) A;\n"
5801                "  delete a;\n"
5802                "  delete (A *)a;\n"
5803                "}");
5804   verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
5805                "    typename aaaaaaaaaaaaaaaaaaaaaaaa();");
5806   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5807                "    new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
5808                "        typename aaaaaaaaaaaaaaaaaaaaaaaa();");
5809   verifyFormat("delete[] h->p;");
5810 }
5811 
5812 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
5813   verifyFormat("int *f(int *a) {}");
5814   verifyFormat("int main(int argc, char **argv) {}");
5815   verifyFormat("Test::Test(int b) : a(b * b) {}");
5816   verifyIndependentOfContext("f(a, *a);");
5817   verifyFormat("void g() { f(*a); }");
5818   verifyIndependentOfContext("int a = b * 10;");
5819   verifyIndependentOfContext("int a = 10 * b;");
5820   verifyIndependentOfContext("int a = b * c;");
5821   verifyIndependentOfContext("int a += b * c;");
5822   verifyIndependentOfContext("int a -= b * c;");
5823   verifyIndependentOfContext("int a *= b * c;");
5824   verifyIndependentOfContext("int a /= b * c;");
5825   verifyIndependentOfContext("int a = *b;");
5826   verifyIndependentOfContext("int a = *b * c;");
5827   verifyIndependentOfContext("int a = b * *c;");
5828   verifyIndependentOfContext("int a = b * (10);");
5829   verifyIndependentOfContext("S << b * (10);");
5830   verifyIndependentOfContext("return 10 * b;");
5831   verifyIndependentOfContext("return *b * *c;");
5832   verifyIndependentOfContext("return a & ~b;");
5833   verifyIndependentOfContext("f(b ? *c : *d);");
5834   verifyIndependentOfContext("int a = b ? *c : *d;");
5835   verifyIndependentOfContext("*b = a;");
5836   verifyIndependentOfContext("a * ~b;");
5837   verifyIndependentOfContext("a * !b;");
5838   verifyIndependentOfContext("a * +b;");
5839   verifyIndependentOfContext("a * -b;");
5840   verifyIndependentOfContext("a * ++b;");
5841   verifyIndependentOfContext("a * --b;");
5842   verifyIndependentOfContext("a[4] * b;");
5843   verifyIndependentOfContext("a[a * a] = 1;");
5844   verifyIndependentOfContext("f() * b;");
5845   verifyIndependentOfContext("a * [self dostuff];");
5846   verifyIndependentOfContext("int x = a * (a + b);");
5847   verifyIndependentOfContext("(a *)(a + b);");
5848   verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
5849   verifyIndependentOfContext("int *pa = (int *)&a;");
5850   verifyIndependentOfContext("return sizeof(int **);");
5851   verifyIndependentOfContext("return sizeof(int ******);");
5852   verifyIndependentOfContext("return (int **&)a;");
5853   verifyIndependentOfContext("f((*PointerToArray)[10]);");
5854   verifyFormat("void f(Type (*parameter)[10]) {}");
5855   verifyFormat("void f(Type (&parameter)[10]) {}");
5856   verifyGoogleFormat("return sizeof(int**);");
5857   verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
5858   verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
5859   verifyFormat("auto a = [](int **&, int ***) {};");
5860   verifyFormat("auto PointerBinding = [](const char *S) {};");
5861   verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
5862   verifyFormat("[](const decltype(*a) &value) {}");
5863   verifyFormat("decltype(a * b) F();");
5864   verifyFormat("#define MACRO() [](A *a) { return 1; }");
5865   verifyFormat("Constructor() : member([](A *a, B *b) {}) {}");
5866   verifyIndependentOfContext("typedef void (*f)(int *a);");
5867   verifyIndependentOfContext("int i{a * b};");
5868   verifyIndependentOfContext("aaa && aaa->f();");
5869   verifyIndependentOfContext("int x = ~*p;");
5870   verifyFormat("Constructor() : a(a), area(width * height) {}");
5871   verifyFormat("Constructor() : a(a), area(a, width * height) {}");
5872   verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
5873   verifyFormat("void f() { f(a, c * d); }");
5874   verifyFormat("void f() { f(new a(), c * d); }");
5875   verifyFormat("void f(const MyOverride &override);");
5876   verifyFormat("void f(const MyFinal &final);");
5877   verifyIndependentOfContext("bool a = f() && override.f();");
5878   verifyIndependentOfContext("bool a = f() && final.f();");
5879 
5880   verifyIndependentOfContext("InvalidRegions[*R] = 0;");
5881 
5882   verifyIndependentOfContext("A<int *> a;");
5883   verifyIndependentOfContext("A<int **> a;");
5884   verifyIndependentOfContext("A<int *, int *> a;");
5885   verifyIndependentOfContext("A<int *[]> a;");
5886   verifyIndependentOfContext(
5887       "const char *const p = reinterpret_cast<const char *const>(q);");
5888   verifyIndependentOfContext("A<int **, int **> a;");
5889   verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
5890   verifyFormat("for (char **a = b; *a; ++a) {\n}");
5891   verifyFormat("for (; a && b;) {\n}");
5892   verifyFormat("bool foo = true && [] { return false; }();");
5893 
5894   verifyFormat(
5895       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5896       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5897 
5898   verifyGoogleFormat("int const* a = &b;");
5899   verifyGoogleFormat("**outparam = 1;");
5900   verifyGoogleFormat("*outparam = a * b;");
5901   verifyGoogleFormat("int main(int argc, char** argv) {}");
5902   verifyGoogleFormat("A<int*> a;");
5903   verifyGoogleFormat("A<int**> a;");
5904   verifyGoogleFormat("A<int*, int*> a;");
5905   verifyGoogleFormat("A<int**, int**> a;");
5906   verifyGoogleFormat("f(b ? *c : *d);");
5907   verifyGoogleFormat("int a = b ? *c : *d;");
5908   verifyGoogleFormat("Type* t = **x;");
5909   verifyGoogleFormat("Type* t = *++*x;");
5910   verifyGoogleFormat("*++*x;");
5911   verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
5912   verifyGoogleFormat("Type* t = x++ * y;");
5913   verifyGoogleFormat(
5914       "const char* const p = reinterpret_cast<const char* const>(q);");
5915   verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
5916   verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
5917   verifyGoogleFormat("template <typename T>\n"
5918                      "void f(int i = 0, SomeType** temps = NULL);");
5919 
5920   FormatStyle Left = getLLVMStyle();
5921   Left.PointerAlignment = FormatStyle::PAS_Left;
5922   verifyFormat("x = *a(x) = *a(y);", Left);
5923   verifyFormat("for (;; *a = b) {\n}", Left);
5924   verifyFormat("return *this += 1;", Left);
5925   verifyFormat("throw *x;", Left);
5926   verifyFormat("delete *x;", Left);
5927   verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left);
5928   verifyFormat("[](const decltype(*a)* ptr) {}", Left);
5929   verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left);
5930 
5931   verifyIndependentOfContext("a = *(x + y);");
5932   verifyIndependentOfContext("a = &(x + y);");
5933   verifyIndependentOfContext("*(x + y).call();");
5934   verifyIndependentOfContext("&(x + y)->call();");
5935   verifyFormat("void f() { &(*I).first; }");
5936 
5937   verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
5938   verifyFormat(
5939       "int *MyValues = {\n"
5940       "    *A, // Operator detection might be confused by the '{'\n"
5941       "    *BB // Operator detection might be confused by previous comment\n"
5942       "};");
5943 
5944   verifyIndependentOfContext("if (int *a = &b)");
5945   verifyIndependentOfContext("if (int &a = *b)");
5946   verifyIndependentOfContext("if (a & b[i])");
5947   verifyIndependentOfContext("if (a::b::c::d & b[i])");
5948   verifyIndependentOfContext("if (*b[i])");
5949   verifyIndependentOfContext("if (int *a = (&b))");
5950   verifyIndependentOfContext("while (int *a = &b)");
5951   verifyIndependentOfContext("size = sizeof *a;");
5952   verifyIndependentOfContext("if (a && (b = c))");
5953   verifyFormat("void f() {\n"
5954                "  for (const int &v : Values) {\n"
5955                "  }\n"
5956                "}");
5957   verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
5958   verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
5959   verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
5960 
5961   verifyFormat("#define A (!a * b)");
5962   verifyFormat("#define MACRO     \\\n"
5963                "  int *i = a * b; \\\n"
5964                "  void f(a *b);",
5965                getLLVMStyleWithColumns(19));
5966 
5967   verifyIndependentOfContext("A = new SomeType *[Length];");
5968   verifyIndependentOfContext("A = new SomeType *[Length]();");
5969   verifyIndependentOfContext("T **t = new T *;");
5970   verifyIndependentOfContext("T **t = new T *();");
5971   verifyGoogleFormat("A = new SomeType*[Length]();");
5972   verifyGoogleFormat("A = new SomeType*[Length];");
5973   verifyGoogleFormat("T** t = new T*;");
5974   verifyGoogleFormat("T** t = new T*();");
5975 
5976   verifyFormat("STATIC_ASSERT((a & b) == 0);");
5977   verifyFormat("STATIC_ASSERT(0 == (a & b));");
5978   verifyFormat("template <bool a, bool b> "
5979                "typename t::if<x && y>::type f() {}");
5980   verifyFormat("template <int *y> f() {}");
5981   verifyFormat("vector<int *> v;");
5982   verifyFormat("vector<int *const> v;");
5983   verifyFormat("vector<int *const **const *> v;");
5984   verifyFormat("vector<int *volatile> v;");
5985   verifyFormat("vector<a * b> v;");
5986   verifyFormat("foo<b && false>();");
5987   verifyFormat("foo<b & 1>();");
5988   verifyFormat("decltype(*::std::declval<const T &>()) void F();");
5989   verifyFormat(
5990       "template <class T, class = typename std::enable_if<\n"
5991       "                       std::is_integral<T>::value &&\n"
5992       "                       (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
5993       "void F();",
5994       getLLVMStyleWithColumns(70));
5995   verifyFormat(
5996       "template <class T,\n"
5997       "          class = typename std::enable_if<\n"
5998       "              std::is_integral<T>::value &&\n"
5999       "              (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n"
6000       "          class U>\n"
6001       "void F();",
6002       getLLVMStyleWithColumns(70));
6003   verifyFormat(
6004       "template <class T,\n"
6005       "          class = typename ::std::enable_if<\n"
6006       "              ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
6007       "void F();",
6008       getGoogleStyleWithColumns(68));
6009 
6010   verifyIndependentOfContext("MACRO(int *i);");
6011   verifyIndependentOfContext("MACRO(auto *a);");
6012   verifyIndependentOfContext("MACRO(const A *a);");
6013   verifyIndependentOfContext("MACRO(A *const a);");
6014   verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
6015   verifyFormat("void f() { f(float{1}, a * a); }");
6016   // FIXME: Is there a way to make this work?
6017   // verifyIndependentOfContext("MACRO(A *a);");
6018 
6019   verifyFormat("DatumHandle const *operator->() const { return input_; }");
6020   verifyFormat("return options != nullptr && operator==(*options);");
6021 
6022   EXPECT_EQ("#define OP(x)                                    \\\n"
6023             "  ostream &operator<<(ostream &s, const A &a) {  \\\n"
6024             "    return s << a.DebugString();                 \\\n"
6025             "  }",
6026             format("#define OP(x) \\\n"
6027                    "  ostream &operator<<(ostream &s, const A &a) { \\\n"
6028                    "    return s << a.DebugString(); \\\n"
6029                    "  }",
6030                    getLLVMStyleWithColumns(50)));
6031 
6032   // FIXME: We cannot handle this case yet; we might be able to figure out that
6033   // foo<x> d > v; doesn't make sense.
6034   verifyFormat("foo<a<b && c> d> v;");
6035 
6036   FormatStyle PointerMiddle = getLLVMStyle();
6037   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
6038   verifyFormat("delete *x;", PointerMiddle);
6039   verifyFormat("int * x;", PointerMiddle);
6040   verifyFormat("int *[] x;", PointerMiddle);
6041   verifyFormat("template <int * y> f() {}", PointerMiddle);
6042   verifyFormat("int * f(int * a) {}", PointerMiddle);
6043   verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
6044   verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
6045   verifyFormat("A<int *> a;", PointerMiddle);
6046   verifyFormat("A<int **> a;", PointerMiddle);
6047   verifyFormat("A<int *, int *> a;", PointerMiddle);
6048   verifyFormat("A<int *[]> a;", PointerMiddle);
6049   verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
6050   verifyFormat("A = new SomeType *[Length];", PointerMiddle);
6051   verifyFormat("T ** t = new T *;", PointerMiddle);
6052 
6053   // Member function reference qualifiers aren't binary operators.
6054   verifyFormat("string // break\n"
6055                "operator()() & {}");
6056   verifyFormat("string // break\n"
6057                "operator()() && {}");
6058   verifyGoogleFormat("template <typename T>\n"
6059                      "auto x() & -> int {}");
6060 }
6061 
6062 TEST_F(FormatTest, UnderstandsAttributes) {
6063   verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
6064   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
6065                "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
6066   FormatStyle AfterType = getLLVMStyle();
6067   AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
6068   verifyFormat("__attribute__((nodebug)) void\n"
6069                "foo() {}\n",
6070                AfterType);
6071 }
6072 
6073 TEST_F(FormatTest, UnderstandsSquareAttributes) {
6074   verifyFormat("SomeType s [[unused]] (InitValue);");
6075   verifyFormat("SomeType s [[gnu::unused]] (InitValue);");
6076   verifyFormat("SomeType s [[using gnu: unused]] (InitValue);");
6077   verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}");
6078   verifyFormat("void f() [[deprecated(\"so sorry\")]];");
6079   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6080                "    [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);");
6081 }
6082 
6083 TEST_F(FormatTest, UnderstandsEllipsis) {
6084   verifyFormat("int printf(const char *fmt, ...);");
6085   verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
6086   verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}");
6087 
6088   FormatStyle PointersLeft = getLLVMStyle();
6089   PointersLeft.PointerAlignment = FormatStyle::PAS_Left;
6090   verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft);
6091 }
6092 
6093 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
6094   EXPECT_EQ("int *a;\n"
6095             "int *a;\n"
6096             "int *a;",
6097             format("int *a;\n"
6098                    "int* a;\n"
6099                    "int *a;",
6100                    getGoogleStyle()));
6101   EXPECT_EQ("int* a;\n"
6102             "int* a;\n"
6103             "int* a;",
6104             format("int* a;\n"
6105                    "int* a;\n"
6106                    "int *a;",
6107                    getGoogleStyle()));
6108   EXPECT_EQ("int *a;\n"
6109             "int *a;\n"
6110             "int *a;",
6111             format("int *a;\n"
6112                    "int * a;\n"
6113                    "int *  a;",
6114                    getGoogleStyle()));
6115   EXPECT_EQ("auto x = [] {\n"
6116             "  int *a;\n"
6117             "  int *a;\n"
6118             "  int *a;\n"
6119             "};",
6120             format("auto x=[]{int *a;\n"
6121                    "int * a;\n"
6122                    "int *  a;};",
6123                    getGoogleStyle()));
6124 }
6125 
6126 TEST_F(FormatTest, UnderstandsRvalueReferences) {
6127   verifyFormat("int f(int &&a) {}");
6128   verifyFormat("int f(int a, char &&b) {}");
6129   verifyFormat("void f() { int &&a = b; }");
6130   verifyGoogleFormat("int f(int a, char&& b) {}");
6131   verifyGoogleFormat("void f() { int&& a = b; }");
6132 
6133   verifyIndependentOfContext("A<int &&> a;");
6134   verifyIndependentOfContext("A<int &&, int &&> a;");
6135   verifyGoogleFormat("A<int&&> a;");
6136   verifyGoogleFormat("A<int&&, int&&> a;");
6137 
6138   // Not rvalue references:
6139   verifyFormat("template <bool B, bool C> class A {\n"
6140                "  static_assert(B && C, \"Something is wrong\");\n"
6141                "};");
6142   verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
6143   verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
6144   verifyFormat("#define A(a, b) (a && b)");
6145 }
6146 
6147 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
6148   verifyFormat("void f() {\n"
6149                "  x[aaaaaaaaa -\n"
6150                "    b] = 23;\n"
6151                "}",
6152                getLLVMStyleWithColumns(15));
6153 }
6154 
6155 TEST_F(FormatTest, FormatsCasts) {
6156   verifyFormat("Type *A = static_cast<Type *>(P);");
6157   verifyFormat("Type *A = (Type *)P;");
6158   verifyFormat("Type *A = (vector<Type *, int *>)P;");
6159   verifyFormat("int a = (int)(2.0f);");
6160   verifyFormat("int a = (int)2.0f;");
6161   verifyFormat("x[(int32)y];");
6162   verifyFormat("x = (int32)y;");
6163   verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
6164   verifyFormat("int a = (int)*b;");
6165   verifyFormat("int a = (int)2.0f;");
6166   verifyFormat("int a = (int)~0;");
6167   verifyFormat("int a = (int)++a;");
6168   verifyFormat("int a = (int)sizeof(int);");
6169   verifyFormat("int a = (int)+2;");
6170   verifyFormat("my_int a = (my_int)2.0f;");
6171   verifyFormat("my_int a = (my_int)sizeof(int);");
6172   verifyFormat("return (my_int)aaa;");
6173   verifyFormat("#define x ((int)-1)");
6174   verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
6175   verifyFormat("#define p(q) ((int *)&q)");
6176   verifyFormat("fn(a)(b) + 1;");
6177 
6178   verifyFormat("void f() { my_int a = (my_int)*b; }");
6179   verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
6180   verifyFormat("my_int a = (my_int)~0;");
6181   verifyFormat("my_int a = (my_int)++a;");
6182   verifyFormat("my_int a = (my_int)-2;");
6183   verifyFormat("my_int a = (my_int)1;");
6184   verifyFormat("my_int a = (my_int *)1;");
6185   verifyFormat("my_int a = (const my_int)-1;");
6186   verifyFormat("my_int a = (const my_int *)-1;");
6187   verifyFormat("my_int a = (my_int)(my_int)-1;");
6188   verifyFormat("my_int a = (ns::my_int)-2;");
6189   verifyFormat("case (my_int)ONE:");
6190   verifyFormat("auto x = (X)this;");
6191 
6192   // FIXME: single value wrapped with paren will be treated as cast.
6193   verifyFormat("void f(int i = (kValue)*kMask) {}");
6194 
6195   verifyFormat("{ (void)F; }");
6196 
6197   // Don't break after a cast's
6198   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
6199                "    (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
6200                "                                   bbbbbbbbbbbbbbbbbbbbbb);");
6201 
6202   // These are not casts.
6203   verifyFormat("void f(int *) {}");
6204   verifyFormat("f(foo)->b;");
6205   verifyFormat("f(foo).b;");
6206   verifyFormat("f(foo)(b);");
6207   verifyFormat("f(foo)[b];");
6208   verifyFormat("[](foo) { return 4; }(bar);");
6209   verifyFormat("(*funptr)(foo)[4];");
6210   verifyFormat("funptrs[4](foo)[4];");
6211   verifyFormat("void f(int *);");
6212   verifyFormat("void f(int *) = 0;");
6213   verifyFormat("void f(SmallVector<int>) {}");
6214   verifyFormat("void f(SmallVector<int>);");
6215   verifyFormat("void f(SmallVector<int>) = 0;");
6216   verifyFormat("void f(int i = (kA * kB) & kMask) {}");
6217   verifyFormat("int a = sizeof(int) * b;");
6218   verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
6219   verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
6220   verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
6221   verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
6222 
6223   // These are not casts, but at some point were confused with casts.
6224   verifyFormat("virtual void foo(int *) override;");
6225   verifyFormat("virtual void foo(char &) const;");
6226   verifyFormat("virtual void foo(int *a, char *) const;");
6227   verifyFormat("int a = sizeof(int *) + b;");
6228   verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
6229   verifyFormat("bool b = f(g<int>) && c;");
6230   verifyFormat("typedef void (*f)(int i) func;");
6231 
6232   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
6233                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
6234   // FIXME: The indentation here is not ideal.
6235   verifyFormat(
6236       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6237       "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
6238       "        [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
6239 }
6240 
6241 TEST_F(FormatTest, FormatsFunctionTypes) {
6242   verifyFormat("A<bool()> a;");
6243   verifyFormat("A<SomeType()> a;");
6244   verifyFormat("A<void (*)(int, std::string)> a;");
6245   verifyFormat("A<void *(int)>;");
6246   verifyFormat("void *(*a)(int *, SomeType *);");
6247   verifyFormat("int (*func)(void *);");
6248   verifyFormat("void f() { int (*func)(void *); }");
6249   verifyFormat("template <class CallbackClass>\n"
6250                "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
6251 
6252   verifyGoogleFormat("A<void*(int*, SomeType*)>;");
6253   verifyGoogleFormat("void* (*a)(int);");
6254   verifyGoogleFormat(
6255       "template <class CallbackClass>\n"
6256       "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
6257 
6258   // Other constructs can look somewhat like function types:
6259   verifyFormat("A<sizeof(*x)> a;");
6260   verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
6261   verifyFormat("some_var = function(*some_pointer_var)[0];");
6262   verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
6263   verifyFormat("int x = f(&h)();");
6264   verifyFormat("returnsFunction(&param1, &param2)(param);");
6265   verifyFormat("std::function<\n"
6266                "    LooooooooooongTemplatedType<\n"
6267                "        SomeType>*(\n"
6268                "        LooooooooooooooooongType type)>\n"
6269                "    function;",
6270                getGoogleStyleWithColumns(40));
6271 }
6272 
6273 TEST_F(FormatTest, FormatsPointersToArrayTypes) {
6274   verifyFormat("A (*foo_)[6];");
6275   verifyFormat("vector<int> (*foo_)[6];");
6276 }
6277 
6278 TEST_F(FormatTest, BreaksLongVariableDeclarations) {
6279   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6280                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
6281   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
6282                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
6283   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6284                "    *LoooooooooooooooooooooooooooooooooooooooongVariable;");
6285 
6286   // Different ways of ()-initializiation.
6287   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6288                "    LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
6289   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6290                "    LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
6291   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6292                "    LoooooooooooooooooooooooooooooooooooooooongVariable({});");
6293   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6294                "    LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
6295 
6296   // Lambdas should not confuse the variable declaration heuristic.
6297   verifyFormat("LooooooooooooooooongType\n"
6298                "    variable(nullptr, [](A *a) {});",
6299                getLLVMStyleWithColumns(40));
6300 }
6301 
6302 TEST_F(FormatTest, BreaksLongDeclarations) {
6303   verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
6304                "    AnotherNameForTheLongType;");
6305   verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
6306                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
6307   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6308                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
6309   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
6310                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
6311   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6312                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6313   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
6314                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6315   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
6316                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6317   verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
6318                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6319   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6320                "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);");
6321   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6322                "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}");
6323   FormatStyle Indented = getLLVMStyle();
6324   Indented.IndentWrappedFunctionNames = true;
6325   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6326                "    LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
6327                Indented);
6328   verifyFormat(
6329       "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6330       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
6331       Indented);
6332   verifyFormat(
6333       "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
6334       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
6335       Indented);
6336   verifyFormat(
6337       "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
6338       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
6339       Indented);
6340 
6341   // FIXME: Without the comment, this breaks after "(".
6342   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType  // break\n"
6343                "    (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
6344                getGoogleStyle());
6345 
6346   verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
6347                "                  int LoooooooooooooooooooongParam2) {}");
6348   verifyFormat(
6349       "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
6350       "                                   SourceLocation L, IdentifierIn *II,\n"
6351       "                                   Type *T) {}");
6352   verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
6353                "ReallyReaaallyLongFunctionName(\n"
6354                "    const std::string &SomeParameter,\n"
6355                "    const SomeType<string, SomeOtherTemplateParameter>\n"
6356                "        &ReallyReallyLongParameterName,\n"
6357                "    const SomeType<string, SomeOtherTemplateParameter>\n"
6358                "        &AnotherLongParameterName) {}");
6359   verifyFormat("template <typename A>\n"
6360                "SomeLoooooooooooooooooooooongType<\n"
6361                "    typename some_namespace::SomeOtherType<A>::Type>\n"
6362                "Function() {}");
6363 
6364   verifyGoogleFormat(
6365       "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
6366       "    aaaaaaaaaaaaaaaaaaaaaaa;");
6367   verifyGoogleFormat(
6368       "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
6369       "                                   SourceLocation L) {}");
6370   verifyGoogleFormat(
6371       "some_namespace::LongReturnType\n"
6372       "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
6373       "    int first_long_parameter, int second_parameter) {}");
6374 
6375   verifyGoogleFormat("template <typename T>\n"
6376                      "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
6377                      "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
6378   verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6379                      "                   int aaaaaaaaaaaaaaaaaaaaaaa);");
6380 
6381   verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
6382                "    const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6383                "        *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6384   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6385                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
6386                "        aaaaaaaaaaaaaaaaaaaaaaaa);");
6387   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6388                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
6389                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
6390                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6391 
6392   verifyFormat("template <typename T> // Templates on own line.\n"
6393                "static int            // Some comment.\n"
6394                "MyFunction(int a);",
6395                getLLVMStyle());
6396 }
6397 
6398 TEST_F(FormatTest, FormatsArrays) {
6399   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
6400                "                         [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
6401   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n"
6402                "                         [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;");
6403   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n"
6404                "    aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}");
6405   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6406                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
6407   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6408                "    [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
6409   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6410                "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
6411                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
6412   verifyFormat(
6413       "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
6414       "             << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
6415       "                                  [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
6416   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n"
6417                "    .aaaaaaaaaaaaaaaaaaaaaa();");
6418 
6419   verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
6420                      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
6421   verifyFormat(
6422       "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
6423       "                                  .aaaaaaa[0]\n"
6424       "                                  .aaaaaaaaaaaaaaaaaaaaaa();");
6425   verifyFormat("a[::b::c];");
6426 
6427   verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
6428 
6429   FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
6430   verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit);
6431 }
6432 
6433 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
6434   verifyFormat("(a)->b();");
6435   verifyFormat("--a;");
6436 }
6437 
6438 TEST_F(FormatTest, HandlesIncludeDirectives) {
6439   verifyFormat("#include <string>\n"
6440                "#include <a/b/c.h>\n"
6441                "#include \"a/b/string\"\n"
6442                "#include \"string.h\"\n"
6443                "#include \"string.h\"\n"
6444                "#include <a-a>\n"
6445                "#include < path with space >\n"
6446                "#include_next <test.h>"
6447                "#include \"abc.h\" // this is included for ABC\n"
6448                "#include \"some long include\" // with a comment\n"
6449                "#include \"some very long include path\"\n"
6450                "#include <some/very/long/include/path>\n",
6451                getLLVMStyleWithColumns(35));
6452   EXPECT_EQ("#include \"a.h\"", format("#include  \"a.h\""));
6453   EXPECT_EQ("#include <a>", format("#include<a>"));
6454 
6455   verifyFormat("#import <string>");
6456   verifyFormat("#import <a/b/c.h>");
6457   verifyFormat("#import \"a/b/string\"");
6458   verifyFormat("#import \"string.h\"");
6459   verifyFormat("#import \"string.h\"");
6460   verifyFormat("#if __has_include(<strstream>)\n"
6461                "#include <strstream>\n"
6462                "#endif");
6463 
6464   verifyFormat("#define MY_IMPORT <a/b>");
6465 
6466   verifyFormat("#if __has_include(<a/b>)");
6467   verifyFormat("#if __has_include_next(<a/b>)");
6468   verifyFormat("#define F __has_include(<a/b>)");
6469   verifyFormat("#define F __has_include_next(<a/b>)");
6470 
6471   // Protocol buffer definition or missing "#".
6472   verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
6473                getLLVMStyleWithColumns(30));
6474 
6475   FormatStyle Style = getLLVMStyle();
6476   Style.AlwaysBreakBeforeMultilineStrings = true;
6477   Style.ColumnLimit = 0;
6478   verifyFormat("#import \"abc.h\"", Style);
6479 
6480   // But 'import' might also be a regular C++ namespace.
6481   verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6482                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6483 }
6484 
6485 //===----------------------------------------------------------------------===//
6486 // Error recovery tests.
6487 //===----------------------------------------------------------------------===//
6488 
6489 TEST_F(FormatTest, IncompleteParameterLists) {
6490   FormatStyle NoBinPacking = getLLVMStyle();
6491   NoBinPacking.BinPackParameters = false;
6492   verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
6493                "                        double *min_x,\n"
6494                "                        double *max_x,\n"
6495                "                        double *min_y,\n"
6496                "                        double *max_y,\n"
6497                "                        double *min_z,\n"
6498                "                        double *max_z, ) {}",
6499                NoBinPacking);
6500 }
6501 
6502 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
6503   verifyFormat("void f() { return; }\n42");
6504   verifyFormat("void f() {\n"
6505                "  if (0)\n"
6506                "    return;\n"
6507                "}\n"
6508                "42");
6509   verifyFormat("void f() { return }\n42");
6510   verifyFormat("void f() {\n"
6511                "  if (0)\n"
6512                "    return\n"
6513                "}\n"
6514                "42");
6515 }
6516 
6517 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
6518   EXPECT_EQ("void f() { return }", format("void  f ( )  {  return  }"));
6519   EXPECT_EQ("void f() {\n"
6520             "  if (a)\n"
6521             "    return\n"
6522             "}",
6523             format("void  f  (  )  {  if  ( a )  return  }"));
6524   EXPECT_EQ("namespace N {\n"
6525             "void f()\n"
6526             "}",
6527             format("namespace  N  {  void f()  }"));
6528   EXPECT_EQ("namespace N {\n"
6529             "void f() {}\n"
6530             "void g()\n"
6531             "} // namespace N",
6532             format("namespace N  { void f( ) { } void g( ) }"));
6533 }
6534 
6535 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
6536   verifyFormat("int aaaaaaaa =\n"
6537                "    // Overlylongcomment\n"
6538                "    b;",
6539                getLLVMStyleWithColumns(20));
6540   verifyFormat("function(\n"
6541                "    ShortArgument,\n"
6542                "    LoooooooooooongArgument);\n",
6543                getLLVMStyleWithColumns(20));
6544 }
6545 
6546 TEST_F(FormatTest, IncorrectAccessSpecifier) {
6547   verifyFormat("public:");
6548   verifyFormat("class A {\n"
6549                "public\n"
6550                "  void f() {}\n"
6551                "};");
6552   verifyFormat("public\n"
6553                "int qwerty;");
6554   verifyFormat("public\n"
6555                "B {}");
6556   verifyFormat("public\n"
6557                "{}");
6558   verifyFormat("public\n"
6559                "B { int x; }");
6560 }
6561 
6562 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
6563   verifyFormat("{");
6564   verifyFormat("#})");
6565   verifyNoCrash("(/**/[:!] ?[).");
6566 }
6567 
6568 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
6569   verifyFormat("do {\n}");
6570   verifyFormat("do {\n}\n"
6571                "f();");
6572   verifyFormat("do {\n}\n"
6573                "wheeee(fun);");
6574   verifyFormat("do {\n"
6575                "  f();\n"
6576                "}");
6577 }
6578 
6579 TEST_F(FormatTest, IncorrectCodeMissingParens) {
6580   verifyFormat("if {\n  foo;\n  foo();\n}");
6581   verifyFormat("switch {\n  foo;\n  foo();\n}");
6582   verifyIncompleteFormat("for {\n  foo;\n  foo();\n}");
6583   verifyFormat("while {\n  foo;\n  foo();\n}");
6584   verifyFormat("do {\n  foo;\n  foo();\n} while;");
6585 }
6586 
6587 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
6588   verifyIncompleteFormat("namespace {\n"
6589                          "class Foo { Foo (\n"
6590                          "};\n"
6591                          "} // namespace");
6592 }
6593 
6594 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
6595   EXPECT_EQ("{\n  {}\n", format("{\n{\n}\n"));
6596   EXPECT_EQ("{\n  {}\n", format("{\n  {\n}\n"));
6597   EXPECT_EQ("{\n  {}\n", format("{\n  {\n  }\n"));
6598   EXPECT_EQ("{\n  {}\n}\n}\n", format("{\n  {\n    }\n  }\n}\n"));
6599 
6600   EXPECT_EQ("{\n"
6601             "  {\n"
6602             "    breakme(\n"
6603             "        qwe);\n"
6604             "  }\n",
6605             format("{\n"
6606                    "    {\n"
6607                    " breakme(qwe);\n"
6608                    "}\n",
6609                    getLLVMStyleWithColumns(10)));
6610 }
6611 
6612 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
6613   verifyFormat("int x = {\n"
6614                "    avariable,\n"
6615                "    b(alongervariable)};",
6616                getLLVMStyleWithColumns(25));
6617 }
6618 
6619 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
6620   verifyFormat("return (a)(b){1, 2, 3};");
6621 }
6622 
6623 TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
6624   verifyFormat("vector<int> x{1, 2, 3, 4};");
6625   verifyFormat("vector<int> x{\n"
6626                "    1,\n"
6627                "    2,\n"
6628                "    3,\n"
6629                "    4,\n"
6630                "};");
6631   verifyFormat("vector<T> x{{}, {}, {}, {}};");
6632   verifyFormat("f({1, 2});");
6633   verifyFormat("auto v = Foo{-1};");
6634   verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
6635   verifyFormat("Class::Class : member{1, 2, 3} {}");
6636   verifyFormat("new vector<int>{1, 2, 3};");
6637   verifyFormat("new int[3]{1, 2, 3};");
6638   verifyFormat("new int{1};");
6639   verifyFormat("return {arg1, arg2};");
6640   verifyFormat("return {arg1, SomeType{parameter}};");
6641   verifyFormat("int count = set<int>{f(), g(), h()}.size();");
6642   verifyFormat("new T{arg1, arg2};");
6643   verifyFormat("f(MyMap[{composite, key}]);");
6644   verifyFormat("class Class {\n"
6645                "  T member = {arg1, arg2};\n"
6646                "};");
6647   verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
6648   verifyFormat("const struct A a = {.a = 1, .b = 2};");
6649   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
6650   verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
6651   verifyFormat("int a = std::is_integral<int>{} + 0;");
6652 
6653   verifyFormat("int foo(int i) { return fo1{}(i); }");
6654   verifyFormat("int foo(int i) { return fo1{}(i); }");
6655   verifyFormat("auto i = decltype(x){};");
6656   verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
6657   verifyFormat("Node n{1, Node{1000}, //\n"
6658                "       2};");
6659   verifyFormat("Aaaa aaaaaaa{\n"
6660                "    {\n"
6661                "        aaaa,\n"
6662                "    },\n"
6663                "};");
6664   verifyFormat("class C : public D {\n"
6665                "  SomeClass SC{2};\n"
6666                "};");
6667   verifyFormat("class C : public A {\n"
6668                "  class D : public B {\n"
6669                "    void f() { int i{2}; }\n"
6670                "  };\n"
6671                "};");
6672   verifyFormat("#define A {a, a},");
6673 
6674   // Binpacking only if there is no trailing comma
6675   verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n"
6676                "                      cccccccccc, dddddddddd};",
6677 			   getLLVMStyleWithColumns(50));
6678   verifyFormat("const Aaaaaa aaaaa = {\n"
6679                "    aaaaaaaaaaa,\n"
6680                "    bbbbbbbbbbb,\n"
6681                "    ccccccccccc,\n"
6682                "    ddddddddddd,\n"
6683                "};", getLLVMStyleWithColumns(50));
6684 
6685   // Cases where distinguising braced lists and blocks is hard.
6686   verifyFormat("vector<int> v{12} GUARDED_BY(mutex);");
6687   verifyFormat("void f() {\n"
6688                "  return; // comment\n"
6689                "}\n"
6690                "SomeType t;");
6691   verifyFormat("void f() {\n"
6692                "  if (a) {\n"
6693                "    f();\n"
6694                "  }\n"
6695                "}\n"
6696                "SomeType t;");
6697 
6698   // In combination with BinPackArguments = false.
6699   FormatStyle NoBinPacking = getLLVMStyle();
6700   NoBinPacking.BinPackArguments = false;
6701   verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
6702                "                      bbbbb,\n"
6703                "                      ccccc,\n"
6704                "                      ddddd,\n"
6705                "                      eeeee,\n"
6706                "                      ffffff,\n"
6707                "                      ggggg,\n"
6708                "                      hhhhhh,\n"
6709                "                      iiiiii,\n"
6710                "                      jjjjjj,\n"
6711                "                      kkkkkk};",
6712                NoBinPacking);
6713   verifyFormat("const Aaaaaa aaaaa = {\n"
6714                "    aaaaa,\n"
6715                "    bbbbb,\n"
6716                "    ccccc,\n"
6717                "    ddddd,\n"
6718                "    eeeee,\n"
6719                "    ffffff,\n"
6720                "    ggggg,\n"
6721                "    hhhhhh,\n"
6722                "    iiiiii,\n"
6723                "    jjjjjj,\n"
6724                "    kkkkkk,\n"
6725                "};",
6726                NoBinPacking);
6727   verifyFormat(
6728       "const Aaaaaa aaaaa = {\n"
6729       "    aaaaa,  bbbbb,  ccccc,  ddddd,  eeeee,  ffffff, ggggg, hhhhhh,\n"
6730       "    iiiiii, jjjjjj, kkkkkk, aaaaa,  bbbbb,  ccccc,  ddddd, eeeee,\n"
6731       "    ffffff, ggggg,  hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
6732       "};",
6733       NoBinPacking);
6734 
6735   // FIXME: The alignment of these trailing comments might be bad. Then again,
6736   // this might be utterly useless in real code.
6737   verifyFormat("Constructor::Constructor()\n"
6738                "    : some_value{         //\n"
6739                "                 aaaaaaa, //\n"
6740                "                 bbbbbbb} {}");
6741 
6742   // In braced lists, the first comment is always assumed to belong to the
6743   // first element. Thus, it can be moved to the next or previous line as
6744   // appropriate.
6745   EXPECT_EQ("function({// First element:\n"
6746             "          1,\n"
6747             "          // Second element:\n"
6748             "          2});",
6749             format("function({\n"
6750                    "    // First element:\n"
6751                    "    1,\n"
6752                    "    // Second element:\n"
6753                    "    2});"));
6754   EXPECT_EQ("std::vector<int> MyNumbers{\n"
6755             "    // First element:\n"
6756             "    1,\n"
6757             "    // Second element:\n"
6758             "    2};",
6759             format("std::vector<int> MyNumbers{// First element:\n"
6760                    "                           1,\n"
6761                    "                           // Second element:\n"
6762                    "                           2};",
6763                    getLLVMStyleWithColumns(30)));
6764   // A trailing comma should still lead to an enforced line break and no
6765   // binpacking.
6766   EXPECT_EQ("vector<int> SomeVector = {\n"
6767             "    // aaa\n"
6768             "    1,\n"
6769             "    2,\n"
6770             "};",
6771             format("vector<int> SomeVector = { // aaa\n"
6772                    "    1, 2, };"));
6773 
6774   FormatStyle ExtraSpaces = getLLVMStyle();
6775   ExtraSpaces.Cpp11BracedListStyle = false;
6776   ExtraSpaces.ColumnLimit = 75;
6777   verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
6778   verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
6779   verifyFormat("f({ 1, 2 });", ExtraSpaces);
6780   verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
6781   verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
6782   verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
6783   verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
6784   verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
6785   verifyFormat("return { arg1, arg2 };", ExtraSpaces);
6786   verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
6787   verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
6788   verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
6789   verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
6790   verifyFormat("class Class {\n"
6791                "  T member = { arg1, arg2 };\n"
6792                "};",
6793                ExtraSpaces);
6794   verifyFormat(
6795       "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6796       "                                 aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
6797       "                  : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
6798       "                                 bbbbbbbbbbbbbbbbbbbb, bbbbb };",
6799       ExtraSpaces);
6800   verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
6801   verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
6802                ExtraSpaces);
6803   verifyFormat(
6804       "someFunction(OtherParam,\n"
6805       "             BracedList{ // comment 1 (Forcing interesting break)\n"
6806       "                         param1, param2,\n"
6807       "                         // comment 2\n"
6808       "                         param3, param4 });",
6809       ExtraSpaces);
6810   verifyFormat(
6811       "std::this_thread::sleep_for(\n"
6812       "    std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
6813       ExtraSpaces);
6814   verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n"
6815                "    aaaaaaa,\n"
6816                "    aaaaaaaaaa,\n"
6817                "    aaaaa,\n"
6818                "    aaaaaaaaaaaaaaa,\n"
6819                "    aaa,\n"
6820                "    aaaaaaaaaa,\n"
6821                "    a,\n"
6822                "    aaaaaaaaaaaaaaaaaaaaa,\n"
6823                "    aaaaaaaaaaaa,\n"
6824                "    aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
6825                "    aaaaaaa,\n"
6826                "    a};");
6827   verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
6828   verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces);
6829   verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces);
6830 }
6831 
6832 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
6833   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6834                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6835                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6836                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6837                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6838                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
6839   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
6840                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6841                "                 1, 22, 333, 4444, 55555, //\n"
6842                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6843                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
6844   verifyFormat(
6845       "vector<int> x = {1,       22, 333, 4444, 55555, 666666, 7777777,\n"
6846       "                 1,       22, 333, 4444, 55555, 666666, 7777777,\n"
6847       "                 1,       22, 333, 4444, 55555, 666666, // comment\n"
6848       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6849       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6850       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6851       "                 7777777};");
6852   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6853                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6854                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
6855   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6856                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6857                "    // Separating comment.\n"
6858                "    X86::R8, X86::R9, X86::R10, X86::R11, 0};");
6859   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6860                "    // Leading comment\n"
6861                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6862                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
6863   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6864                "                 1, 1, 1, 1};",
6865                getLLVMStyleWithColumns(39));
6866   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6867                "                 1, 1, 1, 1};",
6868                getLLVMStyleWithColumns(38));
6869   verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
6870                "    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
6871                getLLVMStyleWithColumns(43));
6872   verifyFormat(
6873       "static unsigned SomeValues[10][3] = {\n"
6874       "    {1, 4, 0},  {4, 9, 0},  {4, 5, 9},  {8, 5, 4}, {1, 8, 4},\n"
6875       "    {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
6876   verifyFormat("static auto fields = new vector<string>{\n"
6877                "    \"aaaaaaaaaaaaa\",\n"
6878                "    \"aaaaaaaaaaaaa\",\n"
6879                "    \"aaaaaaaaaaaa\",\n"
6880                "    \"aaaaaaaaaaaaaa\",\n"
6881                "    \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
6882                "    \"aaaaaaaaaaaa\",\n"
6883                "    \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
6884                "};");
6885   verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
6886   verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
6887                "                 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
6888                "                 3, cccccccccccccccccccccc};",
6889                getLLVMStyleWithColumns(60));
6890 
6891   // Trailing commas.
6892   verifyFormat("vector<int> x = {\n"
6893                "    1, 1, 1, 1, 1, 1, 1, 1,\n"
6894                "};",
6895                getLLVMStyleWithColumns(39));
6896   verifyFormat("vector<int> x = {\n"
6897                "    1, 1, 1, 1, 1, 1, 1, 1, //\n"
6898                "};",
6899                getLLVMStyleWithColumns(39));
6900   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6901                "                 1, 1, 1, 1,\n"
6902                "                 /**/ /**/};",
6903                getLLVMStyleWithColumns(39));
6904 
6905   // Trailing comment in the first line.
6906   verifyFormat("vector<int> iiiiiiiiiiiiiii = {                      //\n"
6907                "    1111111111, 2222222222, 33333333333, 4444444444, //\n"
6908                "    111111111,  222222222,  3333333333,  444444444,  //\n"
6909                "    11111111,   22222222,   333333333,   44444444};");
6910   // Trailing comment in the last line.
6911   verifyFormat("int aaaaa[] = {\n"
6912                "    1, 2, 3, // comment\n"
6913                "    4, 5, 6  // comment\n"
6914                "};");
6915 
6916   // With nested lists, we should either format one item per line or all nested
6917   // lists one on line.
6918   // FIXME: For some nested lists, we can do better.
6919   verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
6920                "        {aaaaaaaaaaaaaaaaaaa},\n"
6921                "        {aaaaaaaaaaaaaaaaaaaaa},\n"
6922                "        {aaaaaaaaaaaaaaaaa}};",
6923                getLLVMStyleWithColumns(60));
6924   verifyFormat(
6925       "SomeStruct my_struct_array = {\n"
6926       "    {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
6927       "     aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
6928       "    {aaa, aaa},\n"
6929       "    {aaa, aaa},\n"
6930       "    {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
6931       "    {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6932       "     aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
6933 
6934   // No column layout should be used here.
6935   verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
6936                "                   bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
6937 
6938   verifyNoCrash("a<,");
6939 
6940   // No braced initializer here.
6941   verifyFormat("void f() {\n"
6942                "  struct Dummy {};\n"
6943                "  f(v);\n"
6944                "}");
6945 
6946   // Long lists should be formatted in columns even if they are nested.
6947   verifyFormat(
6948       "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6949       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6950       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6951       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6952       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6953       "                          1, 22, 333, 4444, 55555, 666666, 7777777});");
6954 
6955   // Allow "single-column" layout even if that violates the column limit. There
6956   // isn't going to be a better way.
6957   verifyFormat("std::vector<int> a = {\n"
6958                "    aaaaaaaa,\n"
6959                "    aaaaaaaa,\n"
6960                "    aaaaaaaa,\n"
6961                "    aaaaaaaa,\n"
6962                "    aaaaaaaaaa,\n"
6963                "    aaaaaaaa,\n"
6964                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa};",
6965                getLLVMStyleWithColumns(30));
6966   verifyFormat("vector<int> aaaa = {\n"
6967                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6968                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6969                "    aaaaaa.aaaaaaa,\n"
6970                "    aaaaaa.aaaaaaa,\n"
6971                "    aaaaaa.aaaaaaa,\n"
6972                "    aaaaaa.aaaaaaa,\n"
6973                "};");
6974 
6975   // Don't create hanging lists.
6976   verifyFormat("someFunction(Param, {List1, List2,\n"
6977                "                     List3});",
6978                getLLVMStyleWithColumns(35));
6979   verifyFormat("someFunction(Param, Param,\n"
6980                "             {List1, List2,\n"
6981                "              List3});",
6982                getLLVMStyleWithColumns(35));
6983   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n"
6984                "                               aaaaaaaaaaaaaaaaaaaaaaa);");
6985 }
6986 
6987 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
6988   FormatStyle DoNotMerge = getLLVMStyle();
6989   DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
6990 
6991   verifyFormat("void f() { return 42; }");
6992   verifyFormat("void f() {\n"
6993                "  return 42;\n"
6994                "}",
6995                DoNotMerge);
6996   verifyFormat("void f() {\n"
6997                "  // Comment\n"
6998                "}");
6999   verifyFormat("{\n"
7000                "#error {\n"
7001                "  int a;\n"
7002                "}");
7003   verifyFormat("{\n"
7004                "  int a;\n"
7005                "#error {\n"
7006                "}");
7007   verifyFormat("void f() {} // comment");
7008   verifyFormat("void f() { int a; } // comment");
7009   verifyFormat("void f() {\n"
7010                "} // comment",
7011                DoNotMerge);
7012   verifyFormat("void f() {\n"
7013                "  int a;\n"
7014                "} // comment",
7015                DoNotMerge);
7016   verifyFormat("void f() {\n"
7017                "} // comment",
7018                getLLVMStyleWithColumns(15));
7019 
7020   verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
7021   verifyFormat("void f() {\n  return 42;\n}", getLLVMStyleWithColumns(22));
7022 
7023   verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
7024   verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
7025   verifyFormat("class C {\n"
7026                "  C()\n"
7027                "      : iiiiiiii(nullptr),\n"
7028                "        kkkkkkk(nullptr),\n"
7029                "        mmmmmmm(nullptr),\n"
7030                "        nnnnnnn(nullptr) {}\n"
7031                "};",
7032                getGoogleStyle());
7033 
7034   FormatStyle NoColumnLimit = getLLVMStyle();
7035   NoColumnLimit.ColumnLimit = 0;
7036   EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
7037   EXPECT_EQ("class C {\n"
7038             "  A() : b(0) {}\n"
7039             "};",
7040             format("class C{A():b(0){}};", NoColumnLimit));
7041   EXPECT_EQ("A()\n"
7042             "    : b(0) {\n"
7043             "}",
7044             format("A()\n:b(0)\n{\n}", NoColumnLimit));
7045 
7046   FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
7047   DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
7048       FormatStyle::SFS_None;
7049   EXPECT_EQ("A()\n"
7050             "    : b(0) {\n"
7051             "}",
7052             format("A():b(0){}", DoNotMergeNoColumnLimit));
7053   EXPECT_EQ("A()\n"
7054             "    : b(0) {\n"
7055             "}",
7056             format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
7057 
7058   verifyFormat("#define A          \\\n"
7059                "  void f() {       \\\n"
7060                "    int i;         \\\n"
7061                "  }",
7062                getLLVMStyleWithColumns(20));
7063   verifyFormat("#define A           \\\n"
7064                "  void f() { int i; }",
7065                getLLVMStyleWithColumns(21));
7066   verifyFormat("#define A            \\\n"
7067                "  void f() {         \\\n"
7068                "    int i;           \\\n"
7069                "  }                  \\\n"
7070                "  int j;",
7071                getLLVMStyleWithColumns(22));
7072   verifyFormat("#define A             \\\n"
7073                "  void f() { int i; } \\\n"
7074                "  int j;",
7075                getLLVMStyleWithColumns(23));
7076 }
7077 
7078 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) {
7079   FormatStyle MergeEmptyOnly = getLLVMStyle();
7080   MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
7081   verifyFormat("class C {\n"
7082                "  int f() {}\n"
7083                "};",
7084                MergeEmptyOnly);
7085   verifyFormat("class C {\n"
7086                "  int f() {\n"
7087                "    return 42;\n"
7088                "  }\n"
7089                "};",
7090                MergeEmptyOnly);
7091   verifyFormat("int f() {}", MergeEmptyOnly);
7092   verifyFormat("int f() {\n"
7093                "  return 42;\n"
7094                "}",
7095                MergeEmptyOnly);
7096 
7097   // Also verify behavior when BraceWrapping.AfterFunction = true
7098   MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
7099   MergeEmptyOnly.BraceWrapping.AfterFunction = true;
7100   verifyFormat("int f() {}", MergeEmptyOnly);
7101   verifyFormat("class C {\n"
7102                "  int f() {}\n"
7103                "};",
7104                MergeEmptyOnly);
7105 }
7106 
7107 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
7108   FormatStyle MergeInlineOnly = getLLVMStyle();
7109   MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
7110   verifyFormat("class C {\n"
7111                "  int f() { return 42; }\n"
7112                "};",
7113                MergeInlineOnly);
7114   verifyFormat("int f() {\n"
7115                "  return 42;\n"
7116                "}",
7117                MergeInlineOnly);
7118 
7119   // SFS_Inline implies SFS_Empty
7120   verifyFormat("class C {\n"
7121                "  int f() {}\n"
7122                "};",
7123                MergeInlineOnly);
7124   verifyFormat("int f() {}", MergeInlineOnly);
7125 
7126   // Also verify behavior when BraceWrapping.AfterFunction = true
7127   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
7128   MergeInlineOnly.BraceWrapping.AfterFunction = true;
7129   verifyFormat("class C {\n"
7130                "  int f() { return 42; }\n"
7131                "};",
7132                MergeInlineOnly);
7133   verifyFormat("int f()\n"
7134                "{\n"
7135                "  return 42;\n"
7136                "}",
7137                MergeInlineOnly);
7138 
7139   // SFS_Inline implies SFS_Empty
7140   verifyFormat("int f() {}", MergeInlineOnly);
7141   verifyFormat("class C {\n"
7142                "  int f() {}\n"
7143                "};",
7144                MergeInlineOnly);
7145 }
7146 
7147 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) {
7148   FormatStyle MergeInlineOnly = getLLVMStyle();
7149   MergeInlineOnly.AllowShortFunctionsOnASingleLine =
7150       FormatStyle::SFS_InlineOnly;
7151   verifyFormat("class C {\n"
7152                "  int f() { return 42; }\n"
7153                "};",
7154                MergeInlineOnly);
7155   verifyFormat("int f() {\n"
7156                "  return 42;\n"
7157                "}",
7158                MergeInlineOnly);
7159 
7160   // SFS_InlineOnly does not imply SFS_Empty
7161   verifyFormat("class C {\n"
7162                "  int f() {}\n"
7163                "};",
7164                MergeInlineOnly);
7165   verifyFormat("int f() {\n"
7166                "}",
7167                MergeInlineOnly);
7168 
7169   // Also verify behavior when BraceWrapping.AfterFunction = true
7170   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
7171   MergeInlineOnly.BraceWrapping.AfterFunction = true;
7172   verifyFormat("class C {\n"
7173                "  int f() { return 42; }\n"
7174                "};",
7175                MergeInlineOnly);
7176   verifyFormat("int f()\n"
7177                "{\n"
7178                "  return 42;\n"
7179                "}",
7180                MergeInlineOnly);
7181 
7182   // SFS_InlineOnly does not imply SFS_Empty
7183   verifyFormat("int f()\n"
7184                "{\n"
7185                "}",
7186                MergeInlineOnly);
7187   verifyFormat("class C {\n"
7188                "  int f() {}\n"
7189                "};",
7190                MergeInlineOnly);
7191 }
7192 
7193 TEST_F(FormatTest, SplitEmptyFunction) {
7194   FormatStyle Style = getLLVMStyle();
7195   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
7196   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7197   Style.BraceWrapping.AfterFunction = true;
7198   Style.BraceWrapping.SplitEmptyFunction = false;
7199   Style.ColumnLimit = 40;
7200 
7201   verifyFormat("int f()\n"
7202                "{}",
7203                Style);
7204   verifyFormat("int f()\n"
7205                "{\n"
7206                "  return 42;\n"
7207                "}",
7208                Style);
7209   verifyFormat("int f()\n"
7210                "{\n"
7211                "  // some comment\n"
7212                "}",
7213                Style);
7214 
7215   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
7216   verifyFormat("int f() {}", Style);
7217   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
7218                "{}",
7219                Style);
7220   verifyFormat("int f()\n"
7221                "{\n"
7222                "  return 0;\n"
7223                "}",
7224                Style);
7225 
7226   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
7227   verifyFormat("class Foo {\n"
7228                "  int f() {}\n"
7229                "};\n",
7230                Style);
7231   verifyFormat("class Foo {\n"
7232                "  int f() { return 0; }\n"
7233                "};\n",
7234                Style);
7235   verifyFormat("class Foo {\n"
7236                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
7237                "  {}\n"
7238                "};\n",
7239                Style);
7240   verifyFormat("class Foo {\n"
7241                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
7242                "  {\n"
7243                "    return 0;\n"
7244                "  }\n"
7245                "};\n",
7246                Style);
7247 
7248   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
7249   verifyFormat("int f() {}", Style);
7250   verifyFormat("int f() { return 0; }", Style);
7251   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
7252                "{}",
7253                Style);
7254   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
7255                "{\n"
7256                "  return 0;\n"
7257                "}",
7258                Style);
7259 }
7260 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) {
7261   FormatStyle Style = getLLVMStyle();
7262   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
7263   verifyFormat("#ifdef A\n"
7264                "int f() {}\n"
7265                "#else\n"
7266                "int g() {}\n"
7267                "#endif",
7268                Style);
7269 }
7270 
7271 TEST_F(FormatTest, SplitEmptyClass) {
7272   FormatStyle Style = getLLVMStyle();
7273   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7274   Style.BraceWrapping.AfterClass = true;
7275   Style.BraceWrapping.SplitEmptyRecord = false;
7276 
7277   verifyFormat("class Foo\n"
7278                "{};",
7279                Style);
7280   verifyFormat("/* something */ class Foo\n"
7281                "{};",
7282                Style);
7283   verifyFormat("template <typename X> class Foo\n"
7284                "{};",
7285                Style);
7286   verifyFormat("class Foo\n"
7287                "{\n"
7288                "  Foo();\n"
7289                "};",
7290                Style);
7291   verifyFormat("typedef class Foo\n"
7292                "{\n"
7293                "} Foo_t;",
7294                Style);
7295 }
7296 
7297 TEST_F(FormatTest, SplitEmptyStruct) {
7298   FormatStyle Style = getLLVMStyle();
7299   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7300   Style.BraceWrapping.AfterStruct = true;
7301   Style.BraceWrapping.SplitEmptyRecord = false;
7302 
7303   verifyFormat("struct Foo\n"
7304                "{};",
7305                Style);
7306   verifyFormat("/* something */ struct Foo\n"
7307                "{};",
7308                Style);
7309   verifyFormat("template <typename X> struct Foo\n"
7310                "{};",
7311                Style);
7312   verifyFormat("struct Foo\n"
7313                "{\n"
7314                "  Foo();\n"
7315                "};",
7316                Style);
7317   verifyFormat("typedef struct Foo\n"
7318                "{\n"
7319                "} Foo_t;",
7320                Style);
7321   //typedef struct Bar {} Bar_t;
7322 }
7323 
7324 TEST_F(FormatTest, SplitEmptyUnion) {
7325   FormatStyle Style = getLLVMStyle();
7326   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7327   Style.BraceWrapping.AfterUnion = true;
7328   Style.BraceWrapping.SplitEmptyRecord = false;
7329 
7330   verifyFormat("union Foo\n"
7331                "{};",
7332                Style);
7333   verifyFormat("/* something */ union Foo\n"
7334                "{};",
7335                Style);
7336   verifyFormat("union Foo\n"
7337                "{\n"
7338                "  A,\n"
7339                "};",
7340                Style);
7341   verifyFormat("typedef union Foo\n"
7342                "{\n"
7343                "} Foo_t;",
7344                Style);
7345 }
7346 
7347 TEST_F(FormatTest, SplitEmptyNamespace) {
7348   FormatStyle Style = getLLVMStyle();
7349   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7350   Style.BraceWrapping.AfterNamespace = true;
7351   Style.BraceWrapping.SplitEmptyNamespace = false;
7352 
7353   verifyFormat("namespace Foo\n"
7354                "{};",
7355                Style);
7356   verifyFormat("/* something */ namespace Foo\n"
7357                "{};",
7358                Style);
7359   verifyFormat("inline namespace Foo\n"
7360                "{};",
7361                Style);
7362   verifyFormat("namespace Foo\n"
7363                "{\n"
7364                "void Bar();\n"
7365                "};",
7366                Style);
7367 }
7368 
7369 TEST_F(FormatTest, NeverMergeShortRecords) {
7370   FormatStyle Style = getLLVMStyle();
7371 
7372   verifyFormat("class Foo {\n"
7373                "  Foo();\n"
7374                "};",
7375                Style);
7376   verifyFormat("typedef class Foo {\n"
7377                "  Foo();\n"
7378                "} Foo_t;",
7379                Style);
7380   verifyFormat("struct Foo {\n"
7381                "  Foo();\n"
7382                "};",
7383                Style);
7384   verifyFormat("typedef struct Foo {\n"
7385                "  Foo();\n"
7386                "} Foo_t;",
7387                Style);
7388   verifyFormat("union Foo {\n"
7389                "  A,\n"
7390                "};",
7391                Style);
7392   verifyFormat("typedef union Foo {\n"
7393                "  A,\n"
7394                "} Foo_t;",
7395                Style);
7396   verifyFormat("namespace Foo {\n"
7397                "void Bar();\n"
7398                "};",
7399                Style);
7400 
7401   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7402   Style.BraceWrapping.AfterClass = true;
7403   Style.BraceWrapping.AfterStruct = true;
7404   Style.BraceWrapping.AfterUnion = true;
7405   Style.BraceWrapping.AfterNamespace = true;
7406   verifyFormat("class Foo\n"
7407                "{\n"
7408                "  Foo();\n"
7409                "};",
7410                Style);
7411   verifyFormat("typedef class Foo\n"
7412                "{\n"
7413                "  Foo();\n"
7414                "} Foo_t;",
7415                Style);
7416   verifyFormat("struct Foo\n"
7417                "{\n"
7418                "  Foo();\n"
7419                "};",
7420                Style);
7421   verifyFormat("typedef struct Foo\n"
7422                "{\n"
7423                "  Foo();\n"
7424                "} Foo_t;",
7425                Style);
7426   verifyFormat("union Foo\n"
7427                "{\n"
7428                "  A,\n"
7429                "};",
7430                Style);
7431   verifyFormat("typedef union Foo\n"
7432                "{\n"
7433                "  A,\n"
7434                "} Foo_t;",
7435                Style);
7436   verifyFormat("namespace Foo\n"
7437                "{\n"
7438                "void Bar();\n"
7439                "};",
7440                Style);
7441 }
7442 
7443 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
7444   // Elaborate type variable declarations.
7445   verifyFormat("struct foo a = {bar};\nint n;");
7446   verifyFormat("class foo a = {bar};\nint n;");
7447   verifyFormat("union foo a = {bar};\nint n;");
7448 
7449   // Elaborate types inside function definitions.
7450   verifyFormat("struct foo f() {}\nint n;");
7451   verifyFormat("class foo f() {}\nint n;");
7452   verifyFormat("union foo f() {}\nint n;");
7453 
7454   // Templates.
7455   verifyFormat("template <class X> void f() {}\nint n;");
7456   verifyFormat("template <struct X> void f() {}\nint n;");
7457   verifyFormat("template <union X> void f() {}\nint n;");
7458 
7459   // Actual definitions...
7460   verifyFormat("struct {\n} n;");
7461   verifyFormat(
7462       "template <template <class T, class Y>, class Z> class X {\n} n;");
7463   verifyFormat("union Z {\n  int n;\n} x;");
7464   verifyFormat("class MACRO Z {\n} n;");
7465   verifyFormat("class MACRO(X) Z {\n} n;");
7466   verifyFormat("class __attribute__(X) Z {\n} n;");
7467   verifyFormat("class __declspec(X) Z {\n} n;");
7468   verifyFormat("class A##B##C {\n} n;");
7469   verifyFormat("class alignas(16) Z {\n} n;");
7470   verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
7471   verifyFormat("class MACROA MACRO(X) Z {\n} n;");
7472 
7473   // Redefinition from nested context:
7474   verifyFormat("class A::B::C {\n} n;");
7475 
7476   // Template definitions.
7477   verifyFormat(
7478       "template <typename F>\n"
7479       "Matcher(const Matcher<F> &Other,\n"
7480       "        typename enable_if_c<is_base_of<F, T>::value &&\n"
7481       "                             !is_same<F, T>::value>::type * = 0)\n"
7482       "    : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
7483 
7484   // FIXME: This is still incorrectly handled at the formatter side.
7485   verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
7486   verifyFormat("int i = SomeFunction(a<b, a> b);");
7487 
7488   // FIXME:
7489   // This now gets parsed incorrectly as class definition.
7490   // verifyFormat("class A<int> f() {\n}\nint n;");
7491 
7492   // Elaborate types where incorrectly parsing the structural element would
7493   // break the indent.
7494   verifyFormat("if (true)\n"
7495                "  class X x;\n"
7496                "else\n"
7497                "  f();\n");
7498 
7499   // This is simply incomplete. Formatting is not important, but must not crash.
7500   verifyFormat("class A:");
7501 }
7502 
7503 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
7504   EXPECT_EQ("#error Leave     all         white!!!!! space* alone!\n",
7505             format("#error Leave     all         white!!!!! space* alone!\n"));
7506   EXPECT_EQ(
7507       "#warning Leave     all         white!!!!! space* alone!\n",
7508       format("#warning Leave     all         white!!!!! space* alone!\n"));
7509   EXPECT_EQ("#error 1", format("  #  error   1"));
7510   EXPECT_EQ("#warning 1", format("  #  warning 1"));
7511 }
7512 
7513 TEST_F(FormatTest, FormatHashIfExpressions) {
7514   verifyFormat("#if AAAA && BBBB");
7515   verifyFormat("#if (AAAA && BBBB)");
7516   verifyFormat("#elif (AAAA && BBBB)");
7517   // FIXME: Come up with a better indentation for #elif.
7518   verifyFormat(
7519       "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) &&  \\\n"
7520       "    defined(BBBBBBBB)\n"
7521       "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) &&  \\\n"
7522       "    defined(BBBBBBBB)\n"
7523       "#endif",
7524       getLLVMStyleWithColumns(65));
7525 }
7526 
7527 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
7528   FormatStyle AllowsMergedIf = getGoogleStyle();
7529   AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
7530   verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
7531   verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
7532   verifyFormat("if (true)\n#error E\n  return 42;", AllowsMergedIf);
7533   EXPECT_EQ("if (true) return 42;",
7534             format("if (true)\nreturn 42;", AllowsMergedIf));
7535   FormatStyle ShortMergedIf = AllowsMergedIf;
7536   ShortMergedIf.ColumnLimit = 25;
7537   verifyFormat("#define A \\\n"
7538                "  if (true) return 42;",
7539                ShortMergedIf);
7540   verifyFormat("#define A \\\n"
7541                "  f();    \\\n"
7542                "  if (true)\n"
7543                "#define B",
7544                ShortMergedIf);
7545   verifyFormat("#define A \\\n"
7546                "  f();    \\\n"
7547                "  if (true)\n"
7548                "g();",
7549                ShortMergedIf);
7550   verifyFormat("{\n"
7551                "#ifdef A\n"
7552                "  // Comment\n"
7553                "  if (true) continue;\n"
7554                "#endif\n"
7555                "  // Comment\n"
7556                "  if (true) continue;\n"
7557                "}",
7558                ShortMergedIf);
7559   ShortMergedIf.ColumnLimit = 33;
7560   verifyFormat("#define A \\\n"
7561                "  if constexpr (true) return 42;",
7562                ShortMergedIf);
7563   ShortMergedIf.ColumnLimit = 29;
7564   verifyFormat("#define A                   \\\n"
7565                "  if (aaaaaaaaaa) return 1; \\\n"
7566                "  return 2;",
7567                ShortMergedIf);
7568   ShortMergedIf.ColumnLimit = 28;
7569   verifyFormat("#define A         \\\n"
7570                "  if (aaaaaaaaaa) \\\n"
7571                "    return 1;     \\\n"
7572                "  return 2;",
7573                ShortMergedIf);
7574   verifyFormat("#define A                \\\n"
7575                "  if constexpr (aaaaaaa) \\\n"
7576                "    return 1;            \\\n"
7577                "  return 2;",
7578                ShortMergedIf);
7579 }
7580 
7581 TEST_F(FormatTest, FormatStarDependingOnContext) {
7582   verifyFormat("void f(int *a);");
7583   verifyFormat("void f() { f(fint * b); }");
7584   verifyFormat("class A {\n  void f(int *a);\n};");
7585   verifyFormat("class A {\n  int *a;\n};");
7586   verifyFormat("namespace a {\n"
7587                "namespace b {\n"
7588                "class A {\n"
7589                "  void f() {}\n"
7590                "  int *a;\n"
7591                "};\n"
7592                "} // namespace b\n"
7593                "} // namespace a");
7594 }
7595 
7596 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
7597   verifyFormat("while");
7598   verifyFormat("operator");
7599 }
7600 
7601 TEST_F(FormatTest, SkipsDeeplyNestedLines) {
7602   // This code would be painfully slow to format if we didn't skip it.
7603   std::string Code("A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" // 20x
7604                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
7605                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
7606                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
7607                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
7608                    "A(1, 1)\n"
7609                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x
7610                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7611                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7612                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7613                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7614                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7615                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7616                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7617                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7618                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n");
7619   // Deeply nested part is untouched, rest is formatted.
7620   EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n",
7621             format(std::string("int    i;\n") + Code + "int    j;\n",
7622                    getLLVMStyle(), SC_ExpectIncomplete));
7623 }
7624 
7625 //===----------------------------------------------------------------------===//
7626 // Objective-C tests.
7627 //===----------------------------------------------------------------------===//
7628 
7629 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
7630   verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
7631   EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
7632             format("-(NSUInteger)indexOfObject:(id)anObject;"));
7633   EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
7634   EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
7635   EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
7636             format("-(NSInteger)Method3:(id)anObject;"));
7637   EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
7638             format("-(NSInteger)Method4:(id)anObject;"));
7639   EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
7640             format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
7641   EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
7642             format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
7643   EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject "
7644             "forAllCells:(BOOL)flag;",
7645             format("- (void)sendAction:(SEL)aSelector to:(id)anObject "
7646                    "forAllCells:(BOOL)flag;"));
7647 
7648   // Very long objectiveC method declaration.
7649   verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
7650                "    (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
7651   verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
7652                "                    inRange:(NSRange)range\n"
7653                "                   outRange:(NSRange)out_range\n"
7654                "                  outRange1:(NSRange)out_range1\n"
7655                "                  outRange2:(NSRange)out_range2\n"
7656                "                  outRange3:(NSRange)out_range3\n"
7657                "                  outRange4:(NSRange)out_range4\n"
7658                "                  outRange5:(NSRange)out_range5\n"
7659                "                  outRange6:(NSRange)out_range6\n"
7660                "                  outRange7:(NSRange)out_range7\n"
7661                "                  outRange8:(NSRange)out_range8\n"
7662                "                  outRange9:(NSRange)out_range9;");
7663 
7664   // When the function name has to be wrapped.
7665   FormatStyle Style = getLLVMStyle();
7666   Style.IndentWrappedFunctionNames = false;
7667   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
7668                "veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
7669                "           anotherName:(NSString)bbbbbbbbbbbbbb {\n"
7670                "}",
7671                Style);
7672   Style.IndentWrappedFunctionNames = true;
7673   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
7674                "    veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
7675                "               anotherName:(NSString)bbbbbbbbbbbbbb {\n"
7676                "}",
7677                Style);
7678 
7679   verifyFormat("- (int)sum:(vector<int>)numbers;");
7680   verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
7681   // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
7682   // protocol lists (but not for template classes):
7683   // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
7684 
7685   verifyFormat("- (int (*)())foo:(int (*)())f;");
7686   verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
7687 
7688   // If there's no return type (very rare in practice!), LLVM and Google style
7689   // agree.
7690   verifyFormat("- foo;");
7691   verifyFormat("- foo:(int)f;");
7692   verifyGoogleFormat("- foo:(int)foo;");
7693 }
7694 
7695 
7696 TEST_F(FormatTest, BreaksStringLiterals) {
7697   EXPECT_EQ("\"some text \"\n"
7698             "\"other\";",
7699             format("\"some text other\";", getLLVMStyleWithColumns(12)));
7700   EXPECT_EQ("\"some text \"\n"
7701             "\"other\";",
7702             format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
7703   EXPECT_EQ(
7704       "#define A  \\\n"
7705       "  \"some \"  \\\n"
7706       "  \"text \"  \\\n"
7707       "  \"other\";",
7708       format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
7709   EXPECT_EQ(
7710       "#define A  \\\n"
7711       "  \"so \"    \\\n"
7712       "  \"text \"  \\\n"
7713       "  \"other\";",
7714       format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
7715 
7716   EXPECT_EQ("\"some text\"",
7717             format("\"some text\"", getLLVMStyleWithColumns(1)));
7718   EXPECT_EQ("\"some text\"",
7719             format("\"some text\"", getLLVMStyleWithColumns(11)));
7720   EXPECT_EQ("\"some \"\n"
7721             "\"text\"",
7722             format("\"some text\"", getLLVMStyleWithColumns(10)));
7723   EXPECT_EQ("\"some \"\n"
7724             "\"text\"",
7725             format("\"some text\"", getLLVMStyleWithColumns(7)));
7726   EXPECT_EQ("\"some\"\n"
7727             "\" tex\"\n"
7728             "\"t\"",
7729             format("\"some text\"", getLLVMStyleWithColumns(6)));
7730   EXPECT_EQ("\"some\"\n"
7731             "\" tex\"\n"
7732             "\" and\"",
7733             format("\"some tex and\"", getLLVMStyleWithColumns(6)));
7734   EXPECT_EQ("\"some\"\n"
7735             "\"/tex\"\n"
7736             "\"/and\"",
7737             format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
7738 
7739   EXPECT_EQ("variable =\n"
7740             "    \"long string \"\n"
7741             "    \"literal\";",
7742             format("variable = \"long string literal\";",
7743                    getLLVMStyleWithColumns(20)));
7744 
7745   EXPECT_EQ("variable = f(\n"
7746             "    \"long string \"\n"
7747             "    \"literal\",\n"
7748             "    short,\n"
7749             "    loooooooooooooooooooong);",
7750             format("variable = f(\"long string literal\", short, "
7751                    "loooooooooooooooooooong);",
7752                    getLLVMStyleWithColumns(20)));
7753 
7754   EXPECT_EQ(
7755       "f(g(\"long string \"\n"
7756       "    \"literal\"),\n"
7757       "  b);",
7758       format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20)));
7759   EXPECT_EQ("f(g(\"long string \"\n"
7760             "    \"literal\",\n"
7761             "    a),\n"
7762             "  b);",
7763             format("f(g(\"long string literal\", a), b);",
7764                    getLLVMStyleWithColumns(20)));
7765   EXPECT_EQ(
7766       "f(\"one two\".split(\n"
7767       "    variable));",
7768       format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
7769   EXPECT_EQ("f(\"one two three four five six \"\n"
7770             "  \"seven\".split(\n"
7771             "      really_looooong_variable));",
7772             format("f(\"one two three four five six seven\"."
7773                    "split(really_looooong_variable));",
7774                    getLLVMStyleWithColumns(33)));
7775 
7776   EXPECT_EQ("f(\"some \"\n"
7777             "  \"text\",\n"
7778             "  other);",
7779             format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
7780 
7781   // Only break as a last resort.
7782   verifyFormat(
7783       "aaaaaaaaaaaaaaaaaaaa(\n"
7784       "    aaaaaaaaaaaaaaaaaaaa,\n"
7785       "    aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
7786 
7787   EXPECT_EQ("\"splitmea\"\n"
7788             "\"trandomp\"\n"
7789             "\"oint\"",
7790             format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
7791 
7792   EXPECT_EQ("\"split/\"\n"
7793             "\"pathat/\"\n"
7794             "\"slashes\"",
7795             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
7796 
7797   EXPECT_EQ("\"split/\"\n"
7798             "\"pathat/\"\n"
7799             "\"slashes\"",
7800             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
7801   EXPECT_EQ("\"split at \"\n"
7802             "\"spaces/at/\"\n"
7803             "\"slashes.at.any$\"\n"
7804             "\"non-alphanumeric%\"\n"
7805             "\"1111111111characte\"\n"
7806             "\"rs\"",
7807             format("\"split at "
7808                    "spaces/at/"
7809                    "slashes.at."
7810                    "any$non-"
7811                    "alphanumeric%"
7812                    "1111111111characte"
7813                    "rs\"",
7814                    getLLVMStyleWithColumns(20)));
7815 
7816   // Verify that splitting the strings understands
7817   // Style::AlwaysBreakBeforeMultilineStrings.
7818   EXPECT_EQ(
7819       "aaaaaaaaaaaa(\n"
7820       "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
7821       "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
7822       format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
7823              "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
7824              "aaaaaaaaaaaaaaaaaaaaaa\");",
7825              getGoogleStyle()));
7826   EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7827             "       \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
7828             format("return \"aaaaaaaaaaaaaaaaaaaaaa "
7829                    "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
7830                    "aaaaaaaaaaaaaaaaaaaaaa\";",
7831                    getGoogleStyle()));
7832   EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7833             "                \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
7834             format("llvm::outs() << "
7835                    "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
7836                    "aaaaaaaaaaaaaaaaaaa\";"));
7837   EXPECT_EQ("ffff(\n"
7838             "    {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7839             "     \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
7840             format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
7841                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
7842                    getGoogleStyle()));
7843 
7844   FormatStyle Style = getLLVMStyleWithColumns(12);
7845   Style.BreakStringLiterals = false;
7846   EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style));
7847 
7848   FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
7849   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
7850   EXPECT_EQ("#define A \\\n"
7851             "  \"some \" \\\n"
7852             "  \"text \" \\\n"
7853             "  \"other\";",
7854             format("#define A \"some text other\";", AlignLeft));
7855 }
7856 
7857 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) {
7858   EXPECT_EQ("C a = \"some more \"\n"
7859             "      \"text\";",
7860             format("C a = \"some more text\";", getLLVMStyleWithColumns(18)));
7861 }
7862 
7863 TEST_F(FormatTest, FullyRemoveEmptyLines) {
7864   FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80);
7865   NoEmptyLines.MaxEmptyLinesToKeep = 0;
7866   EXPECT_EQ("int i = a(b());",
7867             format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines));
7868 }
7869 
7870 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
7871   EXPECT_EQ(
7872       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
7873       "(\n"
7874       "    \"x\t\");",
7875       format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
7876              "aaaaaaa("
7877              "\"x\t\");"));
7878 }
7879 
7880 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
7881   EXPECT_EQ(
7882       "u8\"utf8 string \"\n"
7883       "u8\"literal\";",
7884       format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
7885   EXPECT_EQ(
7886       "u\"utf16 string \"\n"
7887       "u\"literal\";",
7888       format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
7889   EXPECT_EQ(
7890       "U\"utf32 string \"\n"
7891       "U\"literal\";",
7892       format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
7893   EXPECT_EQ("L\"wide string \"\n"
7894             "L\"literal\";",
7895             format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
7896   EXPECT_EQ("@\"NSString \"\n"
7897             "@\"literal\";",
7898             format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
7899   verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26));
7900 
7901   // This input makes clang-format try to split the incomplete unicode escape
7902   // sequence, which used to lead to a crasher.
7903   verifyNoCrash(
7904       "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
7905       getLLVMStyleWithColumns(60));
7906 }
7907 
7908 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
7909   FormatStyle Style = getGoogleStyleWithColumns(15);
7910   EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
7911   EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
7912   EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
7913   EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
7914   EXPECT_EQ("u8R\"x(raw literal)x\";",
7915             format("u8R\"x(raw literal)x\";", Style));
7916 }
7917 
7918 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
7919   FormatStyle Style = getLLVMStyleWithColumns(20);
7920   EXPECT_EQ(
7921       "_T(\"aaaaaaaaaaaaaa\")\n"
7922       "_T(\"aaaaaaaaaaaaaa\")\n"
7923       "_T(\"aaaaaaaaaaaa\")",
7924       format("  _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
7925   EXPECT_EQ("f(x,\n"
7926             "  _T(\"aaaaaaaaaaaa\")\n"
7927             "  _T(\"aaa\"),\n"
7928             "  z);",
7929             format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
7930 
7931   // FIXME: Handle embedded spaces in one iteration.
7932   //  EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
7933   //            "_T(\"aaaaaaaaaaaaa\")\n"
7934   //            "_T(\"aaaaaaaaaaaaa\")\n"
7935   //            "_T(\"a\")",
7936   //            format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
7937   //                   getLLVMStyleWithColumns(20)));
7938   EXPECT_EQ(
7939       "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
7940       format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
7941   EXPECT_EQ("f(\n"
7942             "#if !TEST\n"
7943             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
7944             "#endif\n"
7945             ");",
7946             format("f(\n"
7947                    "#if !TEST\n"
7948                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
7949                    "#endif\n"
7950                    ");"));
7951   EXPECT_EQ("f(\n"
7952             "\n"
7953             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
7954             format("f(\n"
7955                    "\n"
7956                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"));
7957 }
7958 
7959 TEST_F(FormatTest, BreaksStringLiteralOperands) {
7960   // In a function call with two operands, the second can be broken with no line
7961   // break before it.
7962   EXPECT_EQ("func(a, \"long long \"\n"
7963             "        \"long long\");",
7964             format("func(a, \"long long long long\");",
7965                    getLLVMStyleWithColumns(24)));
7966   // In a function call with three operands, the second must be broken with a
7967   // line break before it.
7968   EXPECT_EQ("func(a,\n"
7969             "     \"long long long \"\n"
7970             "     \"long\",\n"
7971             "     c);",
7972             format("func(a, \"long long long long\", c);",
7973                    getLLVMStyleWithColumns(24)));
7974   // In a function call with three operands, the third must be broken with a
7975   // line break before it.
7976   EXPECT_EQ("func(a, b,\n"
7977             "     \"long long long \"\n"
7978             "     \"long\");",
7979             format("func(a, b, \"long long long long\");",
7980                    getLLVMStyleWithColumns(24)));
7981   // In a function call with three operands, both the second and the third must
7982   // be broken with a line break before them.
7983   EXPECT_EQ("func(a,\n"
7984             "     \"long long long \"\n"
7985             "     \"long\",\n"
7986             "     \"long long long \"\n"
7987             "     \"long\");",
7988             format("func(a, \"long long long long\", \"long long long long\");",
7989                    getLLVMStyleWithColumns(24)));
7990   // In a chain of << with two operands, the second can be broken with no line
7991   // break before it.
7992   EXPECT_EQ("a << \"line line \"\n"
7993             "     \"line\";",
7994             format("a << \"line line line\";",
7995                    getLLVMStyleWithColumns(20)));
7996   // In a chain of << with three operands, the second can be broken with no line
7997   // break before it.
7998   EXPECT_EQ("abcde << \"line \"\n"
7999             "         \"line line\"\n"
8000             "      << c;",
8001             format("abcde << \"line line line\" << c;",
8002                    getLLVMStyleWithColumns(20)));
8003   // In a chain of << with three operands, the third must be broken with a line
8004   // break before it.
8005   EXPECT_EQ("a << b\n"
8006             "  << \"line line \"\n"
8007             "     \"line\";",
8008             format("a << b << \"line line line\";",
8009                    getLLVMStyleWithColumns(20)));
8010   // In a chain of << with three operands, the second can be broken with no line
8011   // break before it and the third must be broken with a line break before it.
8012   EXPECT_EQ("abcd << \"line line \"\n"
8013             "        \"line\"\n"
8014             "     << \"line line \"\n"
8015             "        \"line\";",
8016             format("abcd << \"line line line\" << \"line line line\";",
8017                    getLLVMStyleWithColumns(20)));
8018   // In a chain of binary operators with two operands, the second can be broken
8019   // with no line break before it.
8020   EXPECT_EQ("abcd + \"line line \"\n"
8021             "       \"line line\";",
8022             format("abcd + \"line line line line\";",
8023                    getLLVMStyleWithColumns(20)));
8024   // In a chain of binary operators with three operands, the second must be
8025   // broken with a line break before it.
8026   EXPECT_EQ("abcd +\n"
8027             "    \"line line \"\n"
8028             "    \"line line\" +\n"
8029             "    e;",
8030             format("abcd + \"line line line line\" + e;",
8031                    getLLVMStyleWithColumns(20)));
8032   // In a function call with two operands, with AlignAfterOpenBracket enabled,
8033   // the first must be broken with a line break before it.
8034   FormatStyle Style = getLLVMStyleWithColumns(25);
8035   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
8036   EXPECT_EQ("someFunction(\n"
8037             "    \"long long long \"\n"
8038             "    \"long\",\n"
8039             "    a);",
8040             format("someFunction(\"long long long long\", a);", Style));
8041 }
8042 
8043 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
8044   EXPECT_EQ(
8045       "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
8046       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
8047       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
8048       format("aaaaaaaaaaa  =  \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
8049              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
8050              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
8051 }
8052 
8053 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
8054   EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
8055             format("f(g(R\"x(raw literal)x\",   a), b);", getGoogleStyle()));
8056   EXPECT_EQ("fffffffffff(g(R\"x(\n"
8057             "multiline raw string literal xxxxxxxxxxxxxx\n"
8058             ")x\",\n"
8059             "              a),\n"
8060             "            b);",
8061             format("fffffffffff(g(R\"x(\n"
8062                    "multiline raw string literal xxxxxxxxxxxxxx\n"
8063                    ")x\", a), b);",
8064                    getGoogleStyleWithColumns(20)));
8065   EXPECT_EQ("fffffffffff(\n"
8066             "    g(R\"x(qqq\n"
8067             "multiline raw string literal xxxxxxxxxxxxxx\n"
8068             ")x\",\n"
8069             "      a),\n"
8070             "    b);",
8071             format("fffffffffff(g(R\"x(qqq\n"
8072                    "multiline raw string literal xxxxxxxxxxxxxx\n"
8073                    ")x\", a), b);",
8074                    getGoogleStyleWithColumns(20)));
8075 
8076   EXPECT_EQ("fffffffffff(R\"x(\n"
8077             "multiline raw string literal xxxxxxxxxxxxxx\n"
8078             ")x\");",
8079             format("fffffffffff(R\"x(\n"
8080                    "multiline raw string literal xxxxxxxxxxxxxx\n"
8081                    ")x\");",
8082                    getGoogleStyleWithColumns(20)));
8083   EXPECT_EQ("fffffffffff(R\"x(\n"
8084             "multiline raw string literal xxxxxxxxxxxxxx\n"
8085             ")x\" + bbbbbb);",
8086             format("fffffffffff(R\"x(\n"
8087                    "multiline raw string literal xxxxxxxxxxxxxx\n"
8088                    ")x\" +   bbbbbb);",
8089                    getGoogleStyleWithColumns(20)));
8090   EXPECT_EQ("fffffffffff(\n"
8091             "    R\"x(\n"
8092             "multiline raw string literal xxxxxxxxxxxxxx\n"
8093             ")x\" +\n"
8094             "    bbbbbb);",
8095             format("fffffffffff(\n"
8096                    " R\"x(\n"
8097                    "multiline raw string literal xxxxxxxxxxxxxx\n"
8098                    ")x\" + bbbbbb);",
8099                    getGoogleStyleWithColumns(20)));
8100   EXPECT_EQ("fffffffffff(R\"(single line raw string)\" + bbbbbb);",
8101             format("fffffffffff(\n"
8102                    " R\"(single line raw string)\" + bbbbbb);"));
8103 }
8104 
8105 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
8106   verifyFormat("string a = \"unterminated;");
8107   EXPECT_EQ("function(\"unterminated,\n"
8108             "         OtherParameter);",
8109             format("function(  \"unterminated,\n"
8110                    "    OtherParameter);"));
8111 }
8112 
8113 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
8114   FormatStyle Style = getLLVMStyle();
8115   Style.Standard = FormatStyle::LS_Cpp03;
8116   EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
8117             format("#define x(_a) printf(\"foo\"_a);", Style));
8118 }
8119 
8120 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
8121 
8122 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
8123   EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
8124             "             \"ddeeefff\");",
8125             format("someFunction(\"aaabbbcccdddeeefff\");",
8126                    getLLVMStyleWithColumns(25)));
8127   EXPECT_EQ("someFunction1234567890(\n"
8128             "    \"aaabbbcccdddeeefff\");",
8129             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
8130                    getLLVMStyleWithColumns(26)));
8131   EXPECT_EQ("someFunction1234567890(\n"
8132             "    \"aaabbbcccdddeeeff\"\n"
8133             "    \"f\");",
8134             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
8135                    getLLVMStyleWithColumns(25)));
8136   EXPECT_EQ("someFunction1234567890(\n"
8137             "    \"aaabbbcccdddeeeff\"\n"
8138             "    \"f\");",
8139             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
8140                    getLLVMStyleWithColumns(24)));
8141   EXPECT_EQ("someFunction(\n"
8142             "    \"aaabbbcc ddde \"\n"
8143             "    \"efff\");",
8144             format("someFunction(\"aaabbbcc ddde efff\");",
8145                    getLLVMStyleWithColumns(25)));
8146   EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
8147             "             \"ddeeefff\");",
8148             format("someFunction(\"aaabbbccc ddeeefff\");",
8149                    getLLVMStyleWithColumns(25)));
8150   EXPECT_EQ("someFunction1234567890(\n"
8151             "    \"aaabb \"\n"
8152             "    \"cccdddeeefff\");",
8153             format("someFunction1234567890(\"aaabb cccdddeeefff\");",
8154                    getLLVMStyleWithColumns(25)));
8155   EXPECT_EQ("#define A          \\\n"
8156             "  string s =       \\\n"
8157             "      \"123456789\"  \\\n"
8158             "      \"0\";         \\\n"
8159             "  int i;",
8160             format("#define A string s = \"1234567890\"; int i;",
8161                    getLLVMStyleWithColumns(20)));
8162   EXPECT_EQ("someFunction(\n"
8163             "    \"aaabbbcc \"\n"
8164             "    \"dddeeefff\");",
8165             format("someFunction(\"aaabbbcc dddeeefff\");",
8166                    getLLVMStyleWithColumns(25)));
8167 }
8168 
8169 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
8170   EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3)));
8171   EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2)));
8172   EXPECT_EQ("\"test\"\n"
8173             "\"\\n\"",
8174             format("\"test\\n\"", getLLVMStyleWithColumns(7)));
8175   EXPECT_EQ("\"tes\\\\\"\n"
8176             "\"n\"",
8177             format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
8178   EXPECT_EQ("\"\\\\\\\\\"\n"
8179             "\"\\n\"",
8180             format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
8181   EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
8182   EXPECT_EQ("\"\\uff01\"\n"
8183             "\"test\"",
8184             format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
8185   EXPECT_EQ("\"\\Uff01ff02\"",
8186             format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
8187   EXPECT_EQ("\"\\x000000000001\"\n"
8188             "\"next\"",
8189             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
8190   EXPECT_EQ("\"\\x000000000001next\"",
8191             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
8192   EXPECT_EQ("\"\\x000000000001\"",
8193             format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
8194   EXPECT_EQ("\"test\"\n"
8195             "\"\\000000\"\n"
8196             "\"000001\"",
8197             format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
8198   EXPECT_EQ("\"test\\000\"\n"
8199             "\"00000000\"\n"
8200             "\"1\"",
8201             format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
8202 }
8203 
8204 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
8205   verifyFormat("void f() {\n"
8206                "  return g() {}\n"
8207                "  void h() {}");
8208   verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
8209                "g();\n"
8210                "}");
8211 }
8212 
8213 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
8214   verifyFormat(
8215       "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
8216 }
8217 
8218 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
8219   verifyFormat("class X {\n"
8220                "  void f() {\n"
8221                "  }\n"
8222                "};",
8223                getLLVMStyleWithColumns(12));
8224 }
8225 
8226 TEST_F(FormatTest, ConfigurableIndentWidth) {
8227   FormatStyle EightIndent = getLLVMStyleWithColumns(18);
8228   EightIndent.IndentWidth = 8;
8229   EightIndent.ContinuationIndentWidth = 8;
8230   verifyFormat("void f() {\n"
8231                "        someFunction();\n"
8232                "        if (true) {\n"
8233                "                f();\n"
8234                "        }\n"
8235                "}",
8236                EightIndent);
8237   verifyFormat("class X {\n"
8238                "        void f() {\n"
8239                "        }\n"
8240                "};",
8241                EightIndent);
8242   verifyFormat("int x[] = {\n"
8243                "        call(),\n"
8244                "        call()};",
8245                EightIndent);
8246 }
8247 
8248 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
8249   verifyFormat("double\n"
8250                "f();",
8251                getLLVMStyleWithColumns(8));
8252 }
8253 
8254 TEST_F(FormatTest, ConfigurableUseOfTab) {
8255   FormatStyle Tab = getLLVMStyleWithColumns(42);
8256   Tab.IndentWidth = 8;
8257   Tab.UseTab = FormatStyle::UT_Always;
8258   Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left;
8259 
8260   EXPECT_EQ("if (aaaaaaaa && // q\n"
8261             "    bb)\t\t// w\n"
8262             "\t;",
8263             format("if (aaaaaaaa &&// q\n"
8264                    "bb)// w\n"
8265                    ";",
8266                    Tab));
8267   EXPECT_EQ("if (aaa && bbb) // w\n"
8268             "\t;",
8269             format("if(aaa&&bbb)// w\n"
8270                    ";",
8271                    Tab));
8272 
8273   verifyFormat("class X {\n"
8274                "\tvoid f() {\n"
8275                "\t\tsomeFunction(parameter1,\n"
8276                "\t\t\t     parameter2);\n"
8277                "\t}\n"
8278                "};",
8279                Tab);
8280   verifyFormat("#define A                        \\\n"
8281                "\tvoid f() {               \\\n"
8282                "\t\tsomeFunction(    \\\n"
8283                "\t\t    parameter1,  \\\n"
8284                "\t\t    parameter2); \\\n"
8285                "\t}",
8286                Tab);
8287 
8288   Tab.TabWidth = 4;
8289   Tab.IndentWidth = 8;
8290   verifyFormat("class TabWidth4Indent8 {\n"
8291                "\t\tvoid f() {\n"
8292                "\t\t\t\tsomeFunction(parameter1,\n"
8293                "\t\t\t\t\t\t\t parameter2);\n"
8294                "\t\t}\n"
8295                "};",
8296                Tab);
8297 
8298   Tab.TabWidth = 4;
8299   Tab.IndentWidth = 4;
8300   verifyFormat("class TabWidth4Indent4 {\n"
8301                "\tvoid f() {\n"
8302                "\t\tsomeFunction(parameter1,\n"
8303                "\t\t\t\t\t parameter2);\n"
8304                "\t}\n"
8305                "};",
8306                Tab);
8307 
8308   Tab.TabWidth = 8;
8309   Tab.IndentWidth = 4;
8310   verifyFormat("class TabWidth8Indent4 {\n"
8311                "    void f() {\n"
8312                "\tsomeFunction(parameter1,\n"
8313                "\t\t     parameter2);\n"
8314                "    }\n"
8315                "};",
8316                Tab);
8317 
8318   Tab.TabWidth = 8;
8319   Tab.IndentWidth = 8;
8320   EXPECT_EQ("/*\n"
8321             "\t      a\t\tcomment\n"
8322             "\t      in multiple lines\n"
8323             "       */",
8324             format("   /*\t \t \n"
8325                    " \t \t a\t\tcomment\t \t\n"
8326                    " \t \t in multiple lines\t\n"
8327                    " \t  */",
8328                    Tab));
8329 
8330   Tab.UseTab = FormatStyle::UT_ForIndentation;
8331   verifyFormat("{\n"
8332                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8333                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8334                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8335                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8336                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8337                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8338                "};",
8339                Tab);
8340   verifyFormat("enum AA {\n"
8341                "\ta1, // Force multiple lines\n"
8342                "\ta2,\n"
8343                "\ta3\n"
8344                "};",
8345                Tab);
8346   EXPECT_EQ("if (aaaaaaaa && // q\n"
8347             "    bb)         // w\n"
8348             "\t;",
8349             format("if (aaaaaaaa &&// q\n"
8350                    "bb)// w\n"
8351                    ";",
8352                    Tab));
8353   verifyFormat("class X {\n"
8354                "\tvoid f() {\n"
8355                "\t\tsomeFunction(parameter1,\n"
8356                "\t\t             parameter2);\n"
8357                "\t}\n"
8358                "};",
8359                Tab);
8360   verifyFormat("{\n"
8361                "\tQ(\n"
8362                "\t    {\n"
8363                "\t\t    int a;\n"
8364                "\t\t    someFunction(aaaaaaaa,\n"
8365                "\t\t                 bbbbbbb);\n"
8366                "\t    },\n"
8367                "\t    p);\n"
8368                "}",
8369                Tab);
8370   EXPECT_EQ("{\n"
8371             "\t/* aaaa\n"
8372             "\t   bbbb */\n"
8373             "}",
8374             format("{\n"
8375                    "/* aaaa\n"
8376                    "   bbbb */\n"
8377                    "}",
8378                    Tab));
8379   EXPECT_EQ("{\n"
8380             "\t/*\n"
8381             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8382             "\t  bbbbbbbbbbbbb\n"
8383             "\t*/\n"
8384             "}",
8385             format("{\n"
8386                    "/*\n"
8387                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8388                    "*/\n"
8389                    "}",
8390                    Tab));
8391   EXPECT_EQ("{\n"
8392             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8393             "\t// bbbbbbbbbbbbb\n"
8394             "}",
8395             format("{\n"
8396                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8397                    "}",
8398                    Tab));
8399   EXPECT_EQ("{\n"
8400             "\t/*\n"
8401             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8402             "\t  bbbbbbbbbbbbb\n"
8403             "\t*/\n"
8404             "}",
8405             format("{\n"
8406                    "\t/*\n"
8407                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8408                    "\t*/\n"
8409                    "}",
8410                    Tab));
8411   EXPECT_EQ("{\n"
8412             "\t/*\n"
8413             "\n"
8414             "\t*/\n"
8415             "}",
8416             format("{\n"
8417                    "\t/*\n"
8418                    "\n"
8419                    "\t*/\n"
8420                    "}",
8421                    Tab));
8422   EXPECT_EQ("{\n"
8423             "\t/*\n"
8424             " asdf\n"
8425             "\t*/\n"
8426             "}",
8427             format("{\n"
8428                    "\t/*\n"
8429                    " asdf\n"
8430                    "\t*/\n"
8431                    "}",
8432                    Tab));
8433 
8434   Tab.UseTab = FormatStyle::UT_Never;
8435   EXPECT_EQ("/*\n"
8436             "              a\t\tcomment\n"
8437             "              in multiple lines\n"
8438             "       */",
8439             format("   /*\t \t \n"
8440                    " \t \t a\t\tcomment\t \t\n"
8441                    " \t \t in multiple lines\t\n"
8442                    " \t  */",
8443                    Tab));
8444   EXPECT_EQ("/* some\n"
8445             "   comment */",
8446             format(" \t \t /* some\n"
8447                    " \t \t    comment */",
8448                    Tab));
8449   EXPECT_EQ("int a; /* some\n"
8450             "   comment */",
8451             format(" \t \t int a; /* some\n"
8452                    " \t \t    comment */",
8453                    Tab));
8454 
8455   EXPECT_EQ("int a; /* some\n"
8456             "comment */",
8457             format(" \t \t int\ta; /* some\n"
8458                    " \t \t    comment */",
8459                    Tab));
8460   EXPECT_EQ("f(\"\t\t\"); /* some\n"
8461             "    comment */",
8462             format(" \t \t f(\"\t\t\"); /* some\n"
8463                    " \t \t    comment */",
8464                    Tab));
8465   EXPECT_EQ("{\n"
8466             "  /*\n"
8467             "   * Comment\n"
8468             "   */\n"
8469             "  int i;\n"
8470             "}",
8471             format("{\n"
8472                    "\t/*\n"
8473                    "\t * Comment\n"
8474                    "\t */\n"
8475                    "\t int i;\n"
8476                    "}"));
8477 
8478   Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
8479   Tab.TabWidth = 8;
8480   Tab.IndentWidth = 8;
8481   EXPECT_EQ("if (aaaaaaaa && // q\n"
8482             "    bb)         // w\n"
8483             "\t;",
8484             format("if (aaaaaaaa &&// q\n"
8485                    "bb)// w\n"
8486                    ";",
8487                    Tab));
8488   EXPECT_EQ("if (aaa && bbb) // w\n"
8489             "\t;",
8490             format("if(aaa&&bbb)// w\n"
8491                    ";",
8492                    Tab));
8493   verifyFormat("class X {\n"
8494                "\tvoid f() {\n"
8495                "\t\tsomeFunction(parameter1,\n"
8496                "\t\t\t     parameter2);\n"
8497                "\t}\n"
8498                "};",
8499                Tab);
8500   verifyFormat("#define A                        \\\n"
8501                "\tvoid f() {               \\\n"
8502                "\t\tsomeFunction(    \\\n"
8503                "\t\t    parameter1,  \\\n"
8504                "\t\t    parameter2); \\\n"
8505                "\t}",
8506                Tab);
8507   Tab.TabWidth = 4;
8508   Tab.IndentWidth = 8;
8509   verifyFormat("class TabWidth4Indent8 {\n"
8510                "\t\tvoid f() {\n"
8511                "\t\t\t\tsomeFunction(parameter1,\n"
8512                "\t\t\t\t\t\t\t parameter2);\n"
8513                "\t\t}\n"
8514                "};",
8515                Tab);
8516   Tab.TabWidth = 4;
8517   Tab.IndentWidth = 4;
8518   verifyFormat("class TabWidth4Indent4 {\n"
8519                "\tvoid f() {\n"
8520                "\t\tsomeFunction(parameter1,\n"
8521                "\t\t\t\t\t parameter2);\n"
8522                "\t}\n"
8523                "};",
8524                Tab);
8525   Tab.TabWidth = 8;
8526   Tab.IndentWidth = 4;
8527   verifyFormat("class TabWidth8Indent4 {\n"
8528                "    void f() {\n"
8529                "\tsomeFunction(parameter1,\n"
8530                "\t\t     parameter2);\n"
8531                "    }\n"
8532                "};",
8533                Tab);
8534   Tab.TabWidth = 8;
8535   Tab.IndentWidth = 8;
8536   EXPECT_EQ("/*\n"
8537             "\t      a\t\tcomment\n"
8538             "\t      in multiple lines\n"
8539             "       */",
8540             format("   /*\t \t \n"
8541                    " \t \t a\t\tcomment\t \t\n"
8542                    " \t \t in multiple lines\t\n"
8543                    " \t  */",
8544                    Tab));
8545   verifyFormat("{\n"
8546                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8547                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8548                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8549                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8550                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8551                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8552                "};",
8553                Tab);
8554   verifyFormat("enum AA {\n"
8555                "\ta1, // Force multiple lines\n"
8556                "\ta2,\n"
8557                "\ta3\n"
8558                "};",
8559                Tab);
8560   EXPECT_EQ("if (aaaaaaaa && // q\n"
8561             "    bb)         // w\n"
8562             "\t;",
8563             format("if (aaaaaaaa &&// q\n"
8564                    "bb)// w\n"
8565                    ";",
8566                    Tab));
8567   verifyFormat("class X {\n"
8568                "\tvoid f() {\n"
8569                "\t\tsomeFunction(parameter1,\n"
8570                "\t\t\t     parameter2);\n"
8571                "\t}\n"
8572                "};",
8573                Tab);
8574   verifyFormat("{\n"
8575                "\tQ(\n"
8576                "\t    {\n"
8577                "\t\t    int a;\n"
8578                "\t\t    someFunction(aaaaaaaa,\n"
8579                "\t\t\t\t bbbbbbb);\n"
8580                "\t    },\n"
8581                "\t    p);\n"
8582                "}",
8583                Tab);
8584   EXPECT_EQ("{\n"
8585             "\t/* aaaa\n"
8586             "\t   bbbb */\n"
8587             "}",
8588             format("{\n"
8589                    "/* aaaa\n"
8590                    "   bbbb */\n"
8591                    "}",
8592                    Tab));
8593   EXPECT_EQ("{\n"
8594             "\t/*\n"
8595             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8596             "\t  bbbbbbbbbbbbb\n"
8597             "\t*/\n"
8598             "}",
8599             format("{\n"
8600                    "/*\n"
8601                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8602                    "*/\n"
8603                    "}",
8604                    Tab));
8605   EXPECT_EQ("{\n"
8606             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8607             "\t// bbbbbbbbbbbbb\n"
8608             "}",
8609             format("{\n"
8610                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8611                    "}",
8612                    Tab));
8613   EXPECT_EQ("{\n"
8614             "\t/*\n"
8615             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8616             "\t  bbbbbbbbbbbbb\n"
8617             "\t*/\n"
8618             "}",
8619             format("{\n"
8620                    "\t/*\n"
8621                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8622                    "\t*/\n"
8623                    "}",
8624                    Tab));
8625   EXPECT_EQ("{\n"
8626             "\t/*\n"
8627             "\n"
8628             "\t*/\n"
8629             "}",
8630             format("{\n"
8631                    "\t/*\n"
8632                    "\n"
8633                    "\t*/\n"
8634                    "}",
8635                    Tab));
8636   EXPECT_EQ("{\n"
8637             "\t/*\n"
8638             " asdf\n"
8639             "\t*/\n"
8640             "}",
8641             format("{\n"
8642                    "\t/*\n"
8643                    " asdf\n"
8644                    "\t*/\n"
8645                    "}",
8646                    Tab));
8647   EXPECT_EQ("/*\n"
8648             "\t      a\t\tcomment\n"
8649             "\t      in multiple lines\n"
8650             "       */",
8651             format("   /*\t \t \n"
8652                    " \t \t a\t\tcomment\t \t\n"
8653                    " \t \t in multiple lines\t\n"
8654                    " \t  */",
8655                    Tab));
8656   EXPECT_EQ("/* some\n"
8657             "   comment */",
8658             format(" \t \t /* some\n"
8659                    " \t \t    comment */",
8660                    Tab));
8661   EXPECT_EQ("int a; /* some\n"
8662             "   comment */",
8663             format(" \t \t int a; /* some\n"
8664                    " \t \t    comment */",
8665                    Tab));
8666   EXPECT_EQ("int a; /* some\n"
8667             "comment */",
8668             format(" \t \t int\ta; /* some\n"
8669                    " \t \t    comment */",
8670                    Tab));
8671   EXPECT_EQ("f(\"\t\t\"); /* some\n"
8672             "    comment */",
8673             format(" \t \t f(\"\t\t\"); /* some\n"
8674                    " \t \t    comment */",
8675                    Tab));
8676   EXPECT_EQ("{\n"
8677             "  /*\n"
8678             "   * Comment\n"
8679             "   */\n"
8680             "  int i;\n"
8681             "}",
8682             format("{\n"
8683                    "\t/*\n"
8684                    "\t * Comment\n"
8685                    "\t */\n"
8686                    "\t int i;\n"
8687                    "}"));
8688   Tab.AlignConsecutiveAssignments = true;
8689   Tab.AlignConsecutiveDeclarations = true;
8690   Tab.TabWidth = 4;
8691   Tab.IndentWidth = 4;
8692   verifyFormat("class Assign {\n"
8693                "\tvoid f() {\n"
8694                "\t\tint         x      = 123;\n"
8695                "\t\tint         random = 4;\n"
8696                "\t\tstd::string alphabet =\n"
8697                "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
8698                "\t}\n"
8699                "};",
8700                Tab);
8701 }
8702 
8703 TEST_F(FormatTest, CalculatesOriginalColumn) {
8704   EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8705             "q\"; /* some\n"
8706             "       comment */",
8707             format("  \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8708                    "q\"; /* some\n"
8709                    "       comment */",
8710                    getLLVMStyle()));
8711   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
8712             "/* some\n"
8713             "   comment */",
8714             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
8715                    " /* some\n"
8716                    "    comment */",
8717                    getLLVMStyle()));
8718   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8719             "qqq\n"
8720             "/* some\n"
8721             "   comment */",
8722             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8723                    "qqq\n"
8724                    " /* some\n"
8725                    "    comment */",
8726                    getLLVMStyle()));
8727   EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8728             "wwww; /* some\n"
8729             "         comment */",
8730             format("  inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8731                    "wwww; /* some\n"
8732                    "         comment */",
8733                    getLLVMStyle()));
8734 }
8735 
8736 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
8737   FormatStyle NoSpace = getLLVMStyle();
8738   NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
8739 
8740   verifyFormat("while(true)\n"
8741                "  continue;",
8742                NoSpace);
8743   verifyFormat("for(;;)\n"
8744                "  continue;",
8745                NoSpace);
8746   verifyFormat("if(true)\n"
8747                "  f();\n"
8748                "else if(true)\n"
8749                "  f();",
8750                NoSpace);
8751   verifyFormat("do {\n"
8752                "  do_something();\n"
8753                "} while(something());",
8754                NoSpace);
8755   verifyFormat("switch(x) {\n"
8756                "default:\n"
8757                "  break;\n"
8758                "}",
8759                NoSpace);
8760   verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
8761   verifyFormat("size_t x = sizeof(x);", NoSpace);
8762   verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
8763   verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
8764   verifyFormat("alignas(128) char a[128];", NoSpace);
8765   verifyFormat("size_t x = alignof(MyType);", NoSpace);
8766   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
8767   verifyFormat("int f() throw(Deprecated);", NoSpace);
8768   verifyFormat("typedef void (*cb)(int);", NoSpace);
8769   verifyFormat("T A::operator()();", NoSpace);
8770   verifyFormat("X A::operator++(T);", NoSpace);
8771 
8772   FormatStyle Space = getLLVMStyle();
8773   Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
8774 
8775   verifyFormat("int f ();", Space);
8776   verifyFormat("void f (int a, T b) {\n"
8777                "  while (true)\n"
8778                "    continue;\n"
8779                "}",
8780                Space);
8781   verifyFormat("if (true)\n"
8782                "  f ();\n"
8783                "else if (true)\n"
8784                "  f ();",
8785                Space);
8786   verifyFormat("do {\n"
8787                "  do_something ();\n"
8788                "} while (something ());",
8789                Space);
8790   verifyFormat("switch (x) {\n"
8791                "default:\n"
8792                "  break;\n"
8793                "}",
8794                Space);
8795   verifyFormat("A::A () : a (1) {}", Space);
8796   verifyFormat("void f () __attribute__ ((asdf));", Space);
8797   verifyFormat("*(&a + 1);\n"
8798                "&((&a)[1]);\n"
8799                "a[(b + c) * d];\n"
8800                "(((a + 1) * 2) + 3) * 4;",
8801                Space);
8802   verifyFormat("#define A(x) x", Space);
8803   verifyFormat("#define A (x) x", Space);
8804   verifyFormat("#if defined(x)\n"
8805                "#endif",
8806                Space);
8807   verifyFormat("auto i = std::make_unique<int> (5);", Space);
8808   verifyFormat("size_t x = sizeof (x);", Space);
8809   verifyFormat("auto f (int x) -> decltype (x);", Space);
8810   verifyFormat("int f (T x) noexcept (x.create ());", Space);
8811   verifyFormat("alignas (128) char a[128];", Space);
8812   verifyFormat("size_t x = alignof (MyType);", Space);
8813   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
8814   verifyFormat("int f () throw (Deprecated);", Space);
8815   verifyFormat("typedef void (*cb) (int);", Space);
8816   verifyFormat("T A::operator() ();", Space);
8817   verifyFormat("X A::operator++ (T);", Space);
8818 }
8819 
8820 TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
8821   FormatStyle Spaces = getLLVMStyle();
8822 
8823   Spaces.SpacesInParentheses = true;
8824   verifyFormat("do_something( ::globalVar );", Spaces);
8825   verifyFormat("call( x, y, z );", Spaces);
8826   verifyFormat("call();", Spaces);
8827   verifyFormat("std::function<void( int, int )> callback;", Spaces);
8828   verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
8829                Spaces);
8830   verifyFormat("while ( (bool)1 )\n"
8831                "  continue;",
8832                Spaces);
8833   verifyFormat("for ( ;; )\n"
8834                "  continue;",
8835                Spaces);
8836   verifyFormat("if ( true )\n"
8837                "  f();\n"
8838                "else if ( true )\n"
8839                "  f();",
8840                Spaces);
8841   verifyFormat("do {\n"
8842                "  do_something( (int)i );\n"
8843                "} while ( something() );",
8844                Spaces);
8845   verifyFormat("switch ( x ) {\n"
8846                "default:\n"
8847                "  break;\n"
8848                "}",
8849                Spaces);
8850 
8851   Spaces.SpacesInParentheses = false;
8852   Spaces.SpacesInCStyleCastParentheses = true;
8853   verifyFormat("Type *A = ( Type * )P;", Spaces);
8854   verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
8855   verifyFormat("x = ( int32 )y;", Spaces);
8856   verifyFormat("int a = ( int )(2.0f);", Spaces);
8857   verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
8858   verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
8859   verifyFormat("#define x (( int )-1)", Spaces);
8860 
8861   // Run the first set of tests again with:
8862   Spaces.SpacesInParentheses = false;
8863   Spaces.SpaceInEmptyParentheses = true;
8864   Spaces.SpacesInCStyleCastParentheses = true;
8865   verifyFormat("call(x, y, z);", Spaces);
8866   verifyFormat("call( );", Spaces);
8867   verifyFormat("std::function<void(int, int)> callback;", Spaces);
8868   verifyFormat("while (( bool )1)\n"
8869                "  continue;",
8870                Spaces);
8871   verifyFormat("for (;;)\n"
8872                "  continue;",
8873                Spaces);
8874   verifyFormat("if (true)\n"
8875                "  f( );\n"
8876                "else if (true)\n"
8877                "  f( );",
8878                Spaces);
8879   verifyFormat("do {\n"
8880                "  do_something(( int )i);\n"
8881                "} while (something( ));",
8882                Spaces);
8883   verifyFormat("switch (x) {\n"
8884                "default:\n"
8885                "  break;\n"
8886                "}",
8887                Spaces);
8888 
8889   // Run the first set of tests again with:
8890   Spaces.SpaceAfterCStyleCast = true;
8891   verifyFormat("call(x, y, z);", Spaces);
8892   verifyFormat("call( );", Spaces);
8893   verifyFormat("std::function<void(int, int)> callback;", Spaces);
8894   verifyFormat("while (( bool ) 1)\n"
8895                "  continue;",
8896                Spaces);
8897   verifyFormat("for (;;)\n"
8898                "  continue;",
8899                Spaces);
8900   verifyFormat("if (true)\n"
8901                "  f( );\n"
8902                "else if (true)\n"
8903                "  f( );",
8904                Spaces);
8905   verifyFormat("do {\n"
8906                "  do_something(( int ) i);\n"
8907                "} while (something( ));",
8908                Spaces);
8909   verifyFormat("switch (x) {\n"
8910                "default:\n"
8911                "  break;\n"
8912                "}",
8913                Spaces);
8914 
8915   // Run subset of tests again with:
8916   Spaces.SpacesInCStyleCastParentheses = false;
8917   Spaces.SpaceAfterCStyleCast = true;
8918   verifyFormat("while ((bool) 1)\n"
8919                "  continue;",
8920                Spaces);
8921   verifyFormat("do {\n"
8922                "  do_something((int) i);\n"
8923                "} while (something( ));",
8924                Spaces);
8925 }
8926 
8927 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
8928   verifyFormat("int a[5];");
8929   verifyFormat("a[3] += 42;");
8930 
8931   FormatStyle Spaces = getLLVMStyle();
8932   Spaces.SpacesInSquareBrackets = true;
8933   // Lambdas unchanged.
8934   verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
8935   verifyFormat("return [i, args...] {};", Spaces);
8936 
8937   // Not lambdas.
8938   verifyFormat("int a[ 5 ];", Spaces);
8939   verifyFormat("a[ 3 ] += 42;", Spaces);
8940   verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
8941   verifyFormat("double &operator[](int i) { return 0; }\n"
8942                "int i;",
8943                Spaces);
8944   verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
8945   verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
8946   verifyFormat("int i = (*b)[ a ]->f();", Spaces);
8947 }
8948 
8949 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
8950   verifyFormat("int a = 5;");
8951   verifyFormat("a += 42;");
8952   verifyFormat("a or_eq 8;");
8953 
8954   FormatStyle Spaces = getLLVMStyle();
8955   Spaces.SpaceBeforeAssignmentOperators = false;
8956   verifyFormat("int a= 5;", Spaces);
8957   verifyFormat("a+= 42;", Spaces);
8958   verifyFormat("a or_eq 8;", Spaces);
8959 }
8960 
8961 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) {
8962   verifyFormat("class Foo : public Bar {};");
8963   verifyFormat("Foo::Foo() : foo(1) {}");
8964   verifyFormat("for (auto a : b) {\n}");
8965   verifyFormat("int x = a ? b : c;");
8966   verifyFormat("{\n"
8967                "label0:\n"
8968                "  int x = 0;\n"
8969                "}");
8970   verifyFormat("switch (x) {\n"
8971                "case 1:\n"
8972                "default:\n"
8973                "}");
8974 
8975   FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30);
8976   CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false;
8977   verifyFormat("class Foo : public Bar {};", CtorInitializerStyle);
8978   verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle);
8979   verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle);
8980   verifyFormat("int x = a ? b : c;", CtorInitializerStyle);
8981   verifyFormat("{\n"
8982                "label1:\n"
8983                "  int x = 0;\n"
8984                "}",
8985                CtorInitializerStyle);
8986   verifyFormat("switch (x) {\n"
8987                "case 1:\n"
8988                "default:\n"
8989                "}",
8990                CtorInitializerStyle);
8991   CtorInitializerStyle.BreakConstructorInitializers =
8992       FormatStyle::BCIS_AfterColon;
8993   verifyFormat("Fooooooooooo::Fooooooooooo():\n"
8994                "    aaaaaaaaaaaaaaaa(1),\n"
8995                "    bbbbbbbbbbbbbbbb(2) {}",
8996                CtorInitializerStyle);
8997   CtorInitializerStyle.BreakConstructorInitializers =
8998       FormatStyle::BCIS_BeforeComma;
8999   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
9000                "    : aaaaaaaaaaaaaaaa(1)\n"
9001                "    , bbbbbbbbbbbbbbbb(2) {}",
9002                CtorInitializerStyle);
9003   CtorInitializerStyle.BreakConstructorInitializers =
9004       FormatStyle::BCIS_BeforeColon;
9005   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
9006                "    : aaaaaaaaaaaaaaaa(1),\n"
9007                "      bbbbbbbbbbbbbbbb(2) {}",
9008                CtorInitializerStyle);
9009   CtorInitializerStyle.ConstructorInitializerIndentWidth = 0;
9010   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
9011                ": aaaaaaaaaaaaaaaa(1),\n"
9012                "  bbbbbbbbbbbbbbbb(2) {}",
9013                CtorInitializerStyle);
9014 
9015   FormatStyle InheritanceStyle = getLLVMStyle();
9016   InheritanceStyle.SpaceBeforeInheritanceColon = false;
9017   verifyFormat("class Foo: public Bar {};", InheritanceStyle);
9018   verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle);
9019   verifyFormat("for (auto a : b) {\n}", InheritanceStyle);
9020   verifyFormat("int x = a ? b : c;", InheritanceStyle);
9021   verifyFormat("{\n"
9022                "label2:\n"
9023                "  int x = 0;\n"
9024                "}",
9025                InheritanceStyle);
9026   verifyFormat("switch (x) {\n"
9027                "case 1:\n"
9028                "default:\n"
9029                "}",
9030                InheritanceStyle);
9031 
9032   FormatStyle ForLoopStyle = getLLVMStyle();
9033   ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false;
9034   verifyFormat("class Foo : public Bar {};", ForLoopStyle);
9035   verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle);
9036   verifyFormat("for (auto a: b) {\n}", ForLoopStyle);
9037   verifyFormat("int x = a ? b : c;", ForLoopStyle);
9038   verifyFormat("{\n"
9039                "label2:\n"
9040                "  int x = 0;\n"
9041                "}",
9042                ForLoopStyle);
9043   verifyFormat("switch (x) {\n"
9044                "case 1:\n"
9045                "default:\n"
9046                "}",
9047                ForLoopStyle);
9048 
9049   FormatStyle NoSpaceStyle = getLLVMStyle();
9050   NoSpaceStyle.SpaceBeforeCtorInitializerColon = false;
9051   NoSpaceStyle.SpaceBeforeInheritanceColon = false;
9052   NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
9053   verifyFormat("class Foo: public Bar {};", NoSpaceStyle);
9054   verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle);
9055   verifyFormat("for (auto a: b) {\n}", NoSpaceStyle);
9056   verifyFormat("int x = a ? b : c;", NoSpaceStyle);
9057   verifyFormat("{\n"
9058                "label3:\n"
9059                "  int x = 0;\n"
9060                "}",
9061                NoSpaceStyle);
9062   verifyFormat("switch (x) {\n"
9063                "case 1:\n"
9064                "default:\n"
9065                "}",
9066                NoSpaceStyle);
9067 }
9068 
9069 TEST_F(FormatTest, AlignConsecutiveAssignments) {
9070   FormatStyle Alignment = getLLVMStyle();
9071   Alignment.AlignConsecutiveAssignments = false;
9072   verifyFormat("int a = 5;\n"
9073                "int oneTwoThree = 123;",
9074                Alignment);
9075   verifyFormat("int a = 5;\n"
9076                "int oneTwoThree = 123;",
9077                Alignment);
9078 
9079   Alignment.AlignConsecutiveAssignments = true;
9080   verifyFormat("int a           = 5;\n"
9081                "int oneTwoThree = 123;",
9082                Alignment);
9083   verifyFormat("int a           = method();\n"
9084                "int oneTwoThree = 133;",
9085                Alignment);
9086   verifyFormat("a &= 5;\n"
9087                "bcd *= 5;\n"
9088                "ghtyf += 5;\n"
9089                "dvfvdb -= 5;\n"
9090                "a /= 5;\n"
9091                "vdsvsv %= 5;\n"
9092                "sfdbddfbdfbb ^= 5;\n"
9093                "dvsdsv |= 5;\n"
9094                "int dsvvdvsdvvv = 123;",
9095                Alignment);
9096   verifyFormat("int i = 1, j = 10;\n"
9097                "something = 2000;",
9098                Alignment);
9099   verifyFormat("something = 2000;\n"
9100                "int i = 1, j = 10;\n",
9101                Alignment);
9102   verifyFormat("something = 2000;\n"
9103                "another   = 911;\n"
9104                "int i = 1, j = 10;\n"
9105                "oneMore = 1;\n"
9106                "i       = 2;",
9107                Alignment);
9108   verifyFormat("int a   = 5;\n"
9109                "int one = 1;\n"
9110                "method();\n"
9111                "int oneTwoThree = 123;\n"
9112                "int oneTwo      = 12;",
9113                Alignment);
9114   verifyFormat("int oneTwoThree = 123;\n"
9115                "int oneTwo      = 12;\n"
9116                "method();\n",
9117                Alignment);
9118   verifyFormat("int oneTwoThree = 123; // comment\n"
9119                "int oneTwo      = 12;  // comment",
9120                Alignment);
9121   EXPECT_EQ("int a = 5;\n"
9122             "\n"
9123             "int oneTwoThree = 123;",
9124             format("int a       = 5;\n"
9125                    "\n"
9126                    "int oneTwoThree= 123;",
9127                    Alignment));
9128   EXPECT_EQ("int a   = 5;\n"
9129             "int one = 1;\n"
9130             "\n"
9131             "int oneTwoThree = 123;",
9132             format("int a = 5;\n"
9133                    "int one = 1;\n"
9134                    "\n"
9135                    "int oneTwoThree = 123;",
9136                    Alignment));
9137   EXPECT_EQ("int a   = 5;\n"
9138             "int one = 1;\n"
9139             "\n"
9140             "int oneTwoThree = 123;\n"
9141             "int oneTwo      = 12;",
9142             format("int a = 5;\n"
9143                    "int one = 1;\n"
9144                    "\n"
9145                    "int oneTwoThree = 123;\n"
9146                    "int oneTwo = 12;",
9147                    Alignment));
9148   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
9149   verifyFormat("#define A \\\n"
9150                "  int aaaa       = 12; \\\n"
9151                "  int b          = 23; \\\n"
9152                "  int ccc        = 234; \\\n"
9153                "  int dddddddddd = 2345;",
9154                Alignment);
9155   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
9156   verifyFormat("#define A               \\\n"
9157                "  int aaaa       = 12;  \\\n"
9158                "  int b          = 23;  \\\n"
9159                "  int ccc        = 234; \\\n"
9160                "  int dddddddddd = 2345;",
9161                Alignment);
9162   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
9163   verifyFormat("#define A                                                      "
9164                "                \\\n"
9165                "  int aaaa       = 12;                                         "
9166                "                \\\n"
9167                "  int b          = 23;                                         "
9168                "                \\\n"
9169                "  int ccc        = 234;                                        "
9170                "                \\\n"
9171                "  int dddddddddd = 2345;",
9172                Alignment);
9173   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
9174                "k = 4, int l = 5,\n"
9175                "                  int m = 6) {\n"
9176                "  int j      = 10;\n"
9177                "  otherThing = 1;\n"
9178                "}",
9179                Alignment);
9180   verifyFormat("void SomeFunction(int parameter = 0) {\n"
9181                "  int i   = 1;\n"
9182                "  int j   = 2;\n"
9183                "  int big = 10000;\n"
9184                "}",
9185                Alignment);
9186   verifyFormat("class C {\n"
9187                "public:\n"
9188                "  int i            = 1;\n"
9189                "  virtual void f() = 0;\n"
9190                "};",
9191                Alignment);
9192   verifyFormat("int i = 1;\n"
9193                "if (SomeType t = getSomething()) {\n"
9194                "}\n"
9195                "int j   = 2;\n"
9196                "int big = 10000;",
9197                Alignment);
9198   verifyFormat("int j = 7;\n"
9199                "for (int k = 0; k < N; ++k) {\n"
9200                "}\n"
9201                "int j   = 2;\n"
9202                "int big = 10000;\n"
9203                "}",
9204                Alignment);
9205   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
9206   verifyFormat("int i = 1;\n"
9207                "LooooooooooongType loooooooooooooooooooooongVariable\n"
9208                "    = someLooooooooooooooooongFunction();\n"
9209                "int j = 2;",
9210                Alignment);
9211   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
9212   verifyFormat("int i = 1;\n"
9213                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
9214                "    someLooooooooooooooooongFunction();\n"
9215                "int j = 2;",
9216                Alignment);
9217 
9218   verifyFormat("auto lambda = []() {\n"
9219                "  auto i = 0;\n"
9220                "  return 0;\n"
9221                "};\n"
9222                "int i  = 0;\n"
9223                "auto v = type{\n"
9224                "    i = 1,   //\n"
9225                "    (i = 2), //\n"
9226                "    i = 3    //\n"
9227                "};",
9228                Alignment);
9229 
9230   verifyFormat(
9231       "int i      = 1;\n"
9232       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
9233       "                          loooooooooooooooooooooongParameterB);\n"
9234       "int j      = 2;",
9235       Alignment);
9236 
9237   verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
9238                "          typename B   = very_long_type_name_1,\n"
9239                "          typename T_2 = very_long_type_name_2>\n"
9240                "auto foo() {}\n",
9241                Alignment);
9242   verifyFormat("int a, b = 1;\n"
9243                "int c  = 2;\n"
9244                "int dd = 3;\n",
9245                Alignment);
9246   verifyFormat("int aa       = ((1 > 2) ? 3 : 4);\n"
9247                "float b[1][] = {{3.f}};\n",
9248                Alignment);
9249   verifyFormat("for (int i = 0; i < 1; i++)\n"
9250                "  int x = 1;\n",
9251                Alignment);
9252   verifyFormat("for (i = 0; i < 1; i++)\n"
9253                "  x = 1;\n"
9254                "y = 1;\n",
9255                Alignment);
9256 }
9257 
9258 TEST_F(FormatTest, AlignConsecutiveDeclarations) {
9259   FormatStyle Alignment = getLLVMStyle();
9260   Alignment.AlignConsecutiveDeclarations = false;
9261   verifyFormat("float const a = 5;\n"
9262                "int oneTwoThree = 123;",
9263                Alignment);
9264   verifyFormat("int a = 5;\n"
9265                "float const oneTwoThree = 123;",
9266                Alignment);
9267 
9268   Alignment.AlignConsecutiveDeclarations = true;
9269   verifyFormat("float const a = 5;\n"
9270                "int         oneTwoThree = 123;",
9271                Alignment);
9272   verifyFormat("int         a = method();\n"
9273                "float const oneTwoThree = 133;",
9274                Alignment);
9275   verifyFormat("int i = 1, j = 10;\n"
9276                "something = 2000;",
9277                Alignment);
9278   verifyFormat("something = 2000;\n"
9279                "int i = 1, j = 10;\n",
9280                Alignment);
9281   verifyFormat("float      something = 2000;\n"
9282                "double     another = 911;\n"
9283                "int        i = 1, j = 10;\n"
9284                "const int *oneMore = 1;\n"
9285                "unsigned   i = 2;",
9286                Alignment);
9287   verifyFormat("float a = 5;\n"
9288                "int   one = 1;\n"
9289                "method();\n"
9290                "const double       oneTwoThree = 123;\n"
9291                "const unsigned int oneTwo = 12;",
9292                Alignment);
9293   verifyFormat("int      oneTwoThree{0}; // comment\n"
9294                "unsigned oneTwo;         // comment",
9295                Alignment);
9296   EXPECT_EQ("float const a = 5;\n"
9297             "\n"
9298             "int oneTwoThree = 123;",
9299             format("float const   a = 5;\n"
9300                    "\n"
9301                    "int           oneTwoThree= 123;",
9302                    Alignment));
9303   EXPECT_EQ("float a = 5;\n"
9304             "int   one = 1;\n"
9305             "\n"
9306             "unsigned oneTwoThree = 123;",
9307             format("float    a = 5;\n"
9308                    "int      one = 1;\n"
9309                    "\n"
9310                    "unsigned oneTwoThree = 123;",
9311                    Alignment));
9312   EXPECT_EQ("float a = 5;\n"
9313             "int   one = 1;\n"
9314             "\n"
9315             "unsigned oneTwoThree = 123;\n"
9316             "int      oneTwo = 12;",
9317             format("float    a = 5;\n"
9318                    "int one = 1;\n"
9319                    "\n"
9320                    "unsigned oneTwoThree = 123;\n"
9321                    "int oneTwo = 12;",
9322                    Alignment));
9323   // Function prototype alignment
9324   verifyFormat("int    a();\n"
9325                "double b();",
9326                Alignment);
9327   verifyFormat("int    a(int x);\n"
9328                "double b();",
9329                Alignment);
9330   unsigned OldColumnLimit = Alignment.ColumnLimit;
9331   // We need to set ColumnLimit to zero, in order to stress nested alignments,
9332   // otherwise the function parameters will be re-flowed onto a single line.
9333   Alignment.ColumnLimit = 0;
9334   EXPECT_EQ("int    a(int   x,\n"
9335             "         float y);\n"
9336             "double b(int    x,\n"
9337             "         double y);",
9338             format("int a(int x,\n"
9339                    " float y);\n"
9340                    "double b(int x,\n"
9341                    " double y);",
9342                    Alignment));
9343   // This ensures that function parameters of function declarations are
9344   // correctly indented when their owning functions are indented.
9345   // The failure case here is for 'double y' to not be indented enough.
9346   EXPECT_EQ("double a(int x);\n"
9347             "int    b(int    y,\n"
9348             "         double z);",
9349             format("double a(int x);\n"
9350                    "int b(int y,\n"
9351                    " double z);",
9352                    Alignment));
9353   // Set ColumnLimit low so that we induce wrapping immediately after
9354   // the function name and opening paren.
9355   Alignment.ColumnLimit = 13;
9356   verifyFormat("int function(\n"
9357                "    int  x,\n"
9358                "    bool y);",
9359                Alignment);
9360   Alignment.ColumnLimit = OldColumnLimit;
9361   // Ensure function pointers don't screw up recursive alignment
9362   verifyFormat("int    a(int x, void (*fp)(int y));\n"
9363                "double b();",
9364                Alignment);
9365   Alignment.AlignConsecutiveAssignments = true;
9366   // Ensure recursive alignment is broken by function braces, so that the
9367   // "a = 1" does not align with subsequent assignments inside the function
9368   // body.
9369   verifyFormat("int func(int a = 1) {\n"
9370                "  int b  = 2;\n"
9371                "  int cc = 3;\n"
9372                "}",
9373                Alignment);
9374   verifyFormat("float      something = 2000;\n"
9375                "double     another   = 911;\n"
9376                "int        i = 1, j = 10;\n"
9377                "const int *oneMore = 1;\n"
9378                "unsigned   i       = 2;",
9379                Alignment);
9380   verifyFormat("int      oneTwoThree = {0}; // comment\n"
9381                "unsigned oneTwo      = 0;   // comment",
9382                Alignment);
9383   // Make sure that scope is correctly tracked, in the absence of braces
9384   verifyFormat("for (int i = 0; i < n; i++)\n"
9385                "  j = i;\n"
9386                "double x = 1;\n",
9387                Alignment);
9388   verifyFormat("if (int i = 0)\n"
9389                "  j = i;\n"
9390                "double x = 1;\n",
9391                Alignment);
9392   // Ensure operator[] and operator() are comprehended
9393   verifyFormat("struct test {\n"
9394                "  long long int foo();\n"
9395                "  int           operator[](int a);\n"
9396                "  double        bar();\n"
9397                "};\n",
9398                Alignment);
9399   verifyFormat("struct test {\n"
9400                "  long long int foo();\n"
9401                "  int           operator()(int a);\n"
9402                "  double        bar();\n"
9403                "};\n",
9404                Alignment);
9405   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
9406             "  int const i   = 1;\n"
9407             "  int *     j   = 2;\n"
9408             "  int       big = 10000;\n"
9409             "\n"
9410             "  unsigned oneTwoThree = 123;\n"
9411             "  int      oneTwo      = 12;\n"
9412             "  method();\n"
9413             "  float k  = 2;\n"
9414             "  int   ll = 10000;\n"
9415             "}",
9416             format("void SomeFunction(int parameter= 0) {\n"
9417                    " int const  i= 1;\n"
9418                    "  int *j=2;\n"
9419                    " int big  =  10000;\n"
9420                    "\n"
9421                    "unsigned oneTwoThree  =123;\n"
9422                    "int oneTwo = 12;\n"
9423                    "  method();\n"
9424                    "float k= 2;\n"
9425                    "int ll=10000;\n"
9426                    "}",
9427                    Alignment));
9428   Alignment.AlignConsecutiveAssignments = false;
9429   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
9430   verifyFormat("#define A \\\n"
9431                "  int       aaaa = 12; \\\n"
9432                "  float     b = 23; \\\n"
9433                "  const int ccc = 234; \\\n"
9434                "  unsigned  dddddddddd = 2345;",
9435                Alignment);
9436   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
9437   verifyFormat("#define A              \\\n"
9438                "  int       aaaa = 12; \\\n"
9439                "  float     b = 23;    \\\n"
9440                "  const int ccc = 234; \\\n"
9441                "  unsigned  dddddddddd = 2345;",
9442                Alignment);
9443   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
9444   Alignment.ColumnLimit = 30;
9445   verifyFormat("#define A                    \\\n"
9446                "  int       aaaa = 12;       \\\n"
9447                "  float     b = 23;          \\\n"
9448                "  const int ccc = 234;       \\\n"
9449                "  int       dddddddddd = 2345;",
9450                Alignment);
9451   Alignment.ColumnLimit = 80;
9452   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
9453                "k = 4, int l = 5,\n"
9454                "                  int m = 6) {\n"
9455                "  const int j = 10;\n"
9456                "  otherThing = 1;\n"
9457                "}",
9458                Alignment);
9459   verifyFormat("void SomeFunction(int parameter = 0) {\n"
9460                "  int const i = 1;\n"
9461                "  int *     j = 2;\n"
9462                "  int       big = 10000;\n"
9463                "}",
9464                Alignment);
9465   verifyFormat("class C {\n"
9466                "public:\n"
9467                "  int          i = 1;\n"
9468                "  virtual void f() = 0;\n"
9469                "};",
9470                Alignment);
9471   verifyFormat("float i = 1;\n"
9472                "if (SomeType t = getSomething()) {\n"
9473                "}\n"
9474                "const unsigned j = 2;\n"
9475                "int            big = 10000;",
9476                Alignment);
9477   verifyFormat("float j = 7;\n"
9478                "for (int k = 0; k < N; ++k) {\n"
9479                "}\n"
9480                "unsigned j = 2;\n"
9481                "int      big = 10000;\n"
9482                "}",
9483                Alignment);
9484   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
9485   verifyFormat("float              i = 1;\n"
9486                "LooooooooooongType loooooooooooooooooooooongVariable\n"
9487                "    = someLooooooooooooooooongFunction();\n"
9488                "int j = 2;",
9489                Alignment);
9490   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
9491   verifyFormat("int                i = 1;\n"
9492                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
9493                "    someLooooooooooooooooongFunction();\n"
9494                "int j = 2;",
9495                Alignment);
9496 
9497   Alignment.AlignConsecutiveAssignments = true;
9498   verifyFormat("auto lambda = []() {\n"
9499                "  auto  ii = 0;\n"
9500                "  float j  = 0;\n"
9501                "  return 0;\n"
9502                "};\n"
9503                "int   i  = 0;\n"
9504                "float i2 = 0;\n"
9505                "auto  v  = type{\n"
9506                "    i = 1,   //\n"
9507                "    (i = 2), //\n"
9508                "    i = 3    //\n"
9509                "};",
9510                Alignment);
9511   Alignment.AlignConsecutiveAssignments = false;
9512 
9513   verifyFormat(
9514       "int      i = 1;\n"
9515       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
9516       "                          loooooooooooooooooooooongParameterB);\n"
9517       "int      j = 2;",
9518       Alignment);
9519 
9520   // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
9521   // We expect declarations and assignments to align, as long as it doesn't
9522   // exceed the column limit, starting a new alignment sequence whenever it
9523   // happens.
9524   Alignment.AlignConsecutiveAssignments = true;
9525   Alignment.ColumnLimit = 30;
9526   verifyFormat("float    ii              = 1;\n"
9527                "unsigned j               = 2;\n"
9528                "int someVerylongVariable = 1;\n"
9529                "AnotherLongType  ll = 123456;\n"
9530                "VeryVeryLongType k  = 2;\n"
9531                "int              myvar = 1;",
9532                Alignment);
9533   Alignment.ColumnLimit = 80;
9534   Alignment.AlignConsecutiveAssignments = false;
9535 
9536   verifyFormat(
9537       "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
9538       "          typename LongType, typename B>\n"
9539       "auto foo() {}\n",
9540       Alignment);
9541   verifyFormat("float a, b = 1;\n"
9542                "int   c = 2;\n"
9543                "int   dd = 3;\n",
9544                Alignment);
9545   verifyFormat("int   aa = ((1 > 2) ? 3 : 4);\n"
9546                "float b[1][] = {{3.f}};\n",
9547                Alignment);
9548   Alignment.AlignConsecutiveAssignments = true;
9549   verifyFormat("float a, b = 1;\n"
9550                "int   c  = 2;\n"
9551                "int   dd = 3;\n",
9552                Alignment);
9553   verifyFormat("int   aa     = ((1 > 2) ? 3 : 4);\n"
9554                "float b[1][] = {{3.f}};\n",
9555                Alignment);
9556   Alignment.AlignConsecutiveAssignments = false;
9557 
9558   Alignment.ColumnLimit = 30;
9559   Alignment.BinPackParameters = false;
9560   verifyFormat("void foo(float     a,\n"
9561                "         float     b,\n"
9562                "         int       c,\n"
9563                "         uint32_t *d) {\n"
9564                "  int *  e = 0;\n"
9565                "  float  f = 0;\n"
9566                "  double g = 0;\n"
9567                "}\n"
9568                "void bar(ino_t     a,\n"
9569                "         int       b,\n"
9570                "         uint32_t *c,\n"
9571                "         bool      d) {}\n",
9572                Alignment);
9573   Alignment.BinPackParameters = true;
9574   Alignment.ColumnLimit = 80;
9575 
9576   // Bug 33507
9577   Alignment.PointerAlignment = FormatStyle::PAS_Middle;
9578   verifyFormat(
9579       "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n"
9580       "  static const Version verVs2017;\n"
9581       "  return true;\n"
9582       "});\n",
9583       Alignment);
9584   Alignment.PointerAlignment = FormatStyle::PAS_Right;
9585 }
9586 
9587 TEST_F(FormatTest, LinuxBraceBreaking) {
9588   FormatStyle LinuxBraceStyle = getLLVMStyle();
9589   LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
9590   verifyFormat("namespace a\n"
9591                "{\n"
9592                "class A\n"
9593                "{\n"
9594                "  void f()\n"
9595                "  {\n"
9596                "    if (true) {\n"
9597                "      a();\n"
9598                "      b();\n"
9599                "    } else {\n"
9600                "      a();\n"
9601                "    }\n"
9602                "  }\n"
9603                "  void g() { return; }\n"
9604                "};\n"
9605                "struct B {\n"
9606                "  int x;\n"
9607                "};\n"
9608                "} // namespace a\n",
9609                LinuxBraceStyle);
9610   verifyFormat("enum X {\n"
9611                "  Y = 0,\n"
9612                "}\n",
9613                LinuxBraceStyle);
9614   verifyFormat("struct S {\n"
9615                "  int Type;\n"
9616                "  union {\n"
9617                "    int x;\n"
9618                "    double y;\n"
9619                "  } Value;\n"
9620                "  class C\n"
9621                "  {\n"
9622                "    MyFavoriteType Value;\n"
9623                "  } Class;\n"
9624                "}\n",
9625                LinuxBraceStyle);
9626 }
9627 
9628 TEST_F(FormatTest, MozillaBraceBreaking) {
9629   FormatStyle MozillaBraceStyle = getLLVMStyle();
9630   MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
9631   MozillaBraceStyle.FixNamespaceComments = false;
9632   verifyFormat("namespace a {\n"
9633                "class A\n"
9634                "{\n"
9635                "  void f()\n"
9636                "  {\n"
9637                "    if (true) {\n"
9638                "      a();\n"
9639                "      b();\n"
9640                "    }\n"
9641                "  }\n"
9642                "  void g() { return; }\n"
9643                "};\n"
9644                "enum E\n"
9645                "{\n"
9646                "  A,\n"
9647                "  // foo\n"
9648                "  B,\n"
9649                "  C\n"
9650                "};\n"
9651                "struct B\n"
9652                "{\n"
9653                "  int x;\n"
9654                "};\n"
9655                "}\n",
9656                MozillaBraceStyle);
9657   verifyFormat("struct S\n"
9658                "{\n"
9659                "  int Type;\n"
9660                "  union\n"
9661                "  {\n"
9662                "    int x;\n"
9663                "    double y;\n"
9664                "  } Value;\n"
9665                "  class C\n"
9666                "  {\n"
9667                "    MyFavoriteType Value;\n"
9668                "  } Class;\n"
9669                "}\n",
9670                MozillaBraceStyle);
9671 }
9672 
9673 TEST_F(FormatTest, StroustrupBraceBreaking) {
9674   FormatStyle StroustrupBraceStyle = getLLVMStyle();
9675   StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
9676   verifyFormat("namespace a {\n"
9677                "class A {\n"
9678                "  void f()\n"
9679                "  {\n"
9680                "    if (true) {\n"
9681                "      a();\n"
9682                "      b();\n"
9683                "    }\n"
9684                "  }\n"
9685                "  void g() { return; }\n"
9686                "};\n"
9687                "struct B {\n"
9688                "  int x;\n"
9689                "};\n"
9690                "} // namespace a\n",
9691                StroustrupBraceStyle);
9692 
9693   verifyFormat("void foo()\n"
9694                "{\n"
9695                "  if (a) {\n"
9696                "    a();\n"
9697                "  }\n"
9698                "  else {\n"
9699                "    b();\n"
9700                "  }\n"
9701                "}\n",
9702                StroustrupBraceStyle);
9703 
9704   verifyFormat("#ifdef _DEBUG\n"
9705                "int foo(int i = 0)\n"
9706                "#else\n"
9707                "int foo(int i = 5)\n"
9708                "#endif\n"
9709                "{\n"
9710                "  return i;\n"
9711                "}",
9712                StroustrupBraceStyle);
9713 
9714   verifyFormat("void foo() {}\n"
9715                "void bar()\n"
9716                "#ifdef _DEBUG\n"
9717                "{\n"
9718                "  foo();\n"
9719                "}\n"
9720                "#else\n"
9721                "{\n"
9722                "}\n"
9723                "#endif",
9724                StroustrupBraceStyle);
9725 
9726   verifyFormat("void foobar() { int i = 5; }\n"
9727                "#ifdef _DEBUG\n"
9728                "void bar() {}\n"
9729                "#else\n"
9730                "void bar() { foobar(); }\n"
9731                "#endif",
9732                StroustrupBraceStyle);
9733 }
9734 
9735 TEST_F(FormatTest, AllmanBraceBreaking) {
9736   FormatStyle AllmanBraceStyle = getLLVMStyle();
9737   AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
9738 
9739   EXPECT_EQ("namespace a\n"
9740             "{\n"
9741             "void f();\n"
9742             "void g();\n"
9743             "} // namespace a\n",
9744             format("namespace a\n"
9745                    "{\n"
9746                    "void f();\n"
9747                    "void g();\n"
9748                    "}\n",
9749                    AllmanBraceStyle));
9750 
9751   verifyFormat("namespace a\n"
9752                "{\n"
9753                "class A\n"
9754                "{\n"
9755                "  void f()\n"
9756                "  {\n"
9757                "    if (true)\n"
9758                "    {\n"
9759                "      a();\n"
9760                "      b();\n"
9761                "    }\n"
9762                "  }\n"
9763                "  void g() { return; }\n"
9764                "};\n"
9765                "struct B\n"
9766                "{\n"
9767                "  int x;\n"
9768                "};\n"
9769                "} // namespace a",
9770                AllmanBraceStyle);
9771 
9772   verifyFormat("void f()\n"
9773                "{\n"
9774                "  if (true)\n"
9775                "  {\n"
9776                "    a();\n"
9777                "  }\n"
9778                "  else if (false)\n"
9779                "  {\n"
9780                "    b();\n"
9781                "  }\n"
9782                "  else\n"
9783                "  {\n"
9784                "    c();\n"
9785                "  }\n"
9786                "}\n",
9787                AllmanBraceStyle);
9788 
9789   verifyFormat("void f()\n"
9790                "{\n"
9791                "  for (int i = 0; i < 10; ++i)\n"
9792                "  {\n"
9793                "    a();\n"
9794                "  }\n"
9795                "  while (false)\n"
9796                "  {\n"
9797                "    b();\n"
9798                "  }\n"
9799                "  do\n"
9800                "  {\n"
9801                "    c();\n"
9802                "  } while (false)\n"
9803                "}\n",
9804                AllmanBraceStyle);
9805 
9806   verifyFormat("void f(int a)\n"
9807                "{\n"
9808                "  switch (a)\n"
9809                "  {\n"
9810                "  case 0:\n"
9811                "    break;\n"
9812                "  case 1:\n"
9813                "  {\n"
9814                "    break;\n"
9815                "  }\n"
9816                "  case 2:\n"
9817                "  {\n"
9818                "  }\n"
9819                "  break;\n"
9820                "  default:\n"
9821                "    break;\n"
9822                "  }\n"
9823                "}\n",
9824                AllmanBraceStyle);
9825 
9826   verifyFormat("enum X\n"
9827                "{\n"
9828                "  Y = 0,\n"
9829                "}\n",
9830                AllmanBraceStyle);
9831   verifyFormat("enum X\n"
9832                "{\n"
9833                "  Y = 0\n"
9834                "}\n",
9835                AllmanBraceStyle);
9836 
9837   verifyFormat("@interface BSApplicationController ()\n"
9838                "{\n"
9839                "@private\n"
9840                "  id _extraIvar;\n"
9841                "}\n"
9842                "@end\n",
9843                AllmanBraceStyle);
9844 
9845   verifyFormat("#ifdef _DEBUG\n"
9846                "int foo(int i = 0)\n"
9847                "#else\n"
9848                "int foo(int i = 5)\n"
9849                "#endif\n"
9850                "{\n"
9851                "  return i;\n"
9852                "}",
9853                AllmanBraceStyle);
9854 
9855   verifyFormat("void foo() {}\n"
9856                "void bar()\n"
9857                "#ifdef _DEBUG\n"
9858                "{\n"
9859                "  foo();\n"
9860                "}\n"
9861                "#else\n"
9862                "{\n"
9863                "}\n"
9864                "#endif",
9865                AllmanBraceStyle);
9866 
9867   verifyFormat("void foobar() { int i = 5; }\n"
9868                "#ifdef _DEBUG\n"
9869                "void bar() {}\n"
9870                "#else\n"
9871                "void bar() { foobar(); }\n"
9872                "#endif",
9873                AllmanBraceStyle);
9874 
9875   // This shouldn't affect ObjC blocks..
9876   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
9877                "  // ...\n"
9878                "  int i;\n"
9879                "}];",
9880                AllmanBraceStyle);
9881   verifyFormat("void (^block)(void) = ^{\n"
9882                "  // ...\n"
9883                "  int i;\n"
9884                "};",
9885                AllmanBraceStyle);
9886   // .. or dict literals.
9887   verifyFormat("void f()\n"
9888                "{\n"
9889                "  // ...\n"
9890                "  [object someMethod:@{@\"a\" : @\"b\"}];\n"
9891                "}",
9892                AllmanBraceStyle);
9893   verifyFormat("void f()\n"
9894                "{\n"
9895                "  // ...\n"
9896                "  [object someMethod:@{a : @\"b\"}];\n"
9897                "}",
9898                AllmanBraceStyle);
9899   verifyFormat("int f()\n"
9900                "{ // comment\n"
9901                "  return 42;\n"
9902                "}",
9903                AllmanBraceStyle);
9904 
9905   AllmanBraceStyle.ColumnLimit = 19;
9906   verifyFormat("void f() { int i; }", AllmanBraceStyle);
9907   AllmanBraceStyle.ColumnLimit = 18;
9908   verifyFormat("void f()\n"
9909                "{\n"
9910                "  int i;\n"
9911                "}",
9912                AllmanBraceStyle);
9913   AllmanBraceStyle.ColumnLimit = 80;
9914 
9915   FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
9916   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true;
9917   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
9918   verifyFormat("void f(bool b)\n"
9919                "{\n"
9920                "  if (b)\n"
9921                "  {\n"
9922                "    return;\n"
9923                "  }\n"
9924                "}\n",
9925                BreakBeforeBraceShortIfs);
9926   verifyFormat("void f(bool b)\n"
9927                "{\n"
9928                "  if constexpr (b)\n"
9929                "  {\n"
9930                "    return;\n"
9931                "  }\n"
9932                "}\n",
9933                BreakBeforeBraceShortIfs);
9934   verifyFormat("void f(bool b)\n"
9935                "{\n"
9936                "  if (b) return;\n"
9937                "}\n",
9938                BreakBeforeBraceShortIfs);
9939   verifyFormat("void f(bool b)\n"
9940                "{\n"
9941                "  if constexpr (b) return;\n"
9942                "}\n",
9943                BreakBeforeBraceShortIfs);
9944   verifyFormat("void f(bool b)\n"
9945                "{\n"
9946                "  while (b)\n"
9947                "  {\n"
9948                "    return;\n"
9949                "  }\n"
9950                "}\n",
9951                BreakBeforeBraceShortIfs);
9952 }
9953 
9954 TEST_F(FormatTest, GNUBraceBreaking) {
9955   FormatStyle GNUBraceStyle = getLLVMStyle();
9956   GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
9957   verifyFormat("namespace a\n"
9958                "{\n"
9959                "class A\n"
9960                "{\n"
9961                "  void f()\n"
9962                "  {\n"
9963                "    int a;\n"
9964                "    {\n"
9965                "      int b;\n"
9966                "    }\n"
9967                "    if (true)\n"
9968                "      {\n"
9969                "        a();\n"
9970                "        b();\n"
9971                "      }\n"
9972                "  }\n"
9973                "  void g() { return; }\n"
9974                "}\n"
9975                "} // namespace a",
9976                GNUBraceStyle);
9977 
9978   verifyFormat("void f()\n"
9979                "{\n"
9980                "  if (true)\n"
9981                "    {\n"
9982                "      a();\n"
9983                "    }\n"
9984                "  else if (false)\n"
9985                "    {\n"
9986                "      b();\n"
9987                "    }\n"
9988                "  else\n"
9989                "    {\n"
9990                "      c();\n"
9991                "    }\n"
9992                "}\n",
9993                GNUBraceStyle);
9994 
9995   verifyFormat("void f()\n"
9996                "{\n"
9997                "  for (int i = 0; i < 10; ++i)\n"
9998                "    {\n"
9999                "      a();\n"
10000                "    }\n"
10001                "  while (false)\n"
10002                "    {\n"
10003                "      b();\n"
10004                "    }\n"
10005                "  do\n"
10006                "    {\n"
10007                "      c();\n"
10008                "    }\n"
10009                "  while (false);\n"
10010                "}\n",
10011                GNUBraceStyle);
10012 
10013   verifyFormat("void f(int a)\n"
10014                "{\n"
10015                "  switch (a)\n"
10016                "    {\n"
10017                "    case 0:\n"
10018                "      break;\n"
10019                "    case 1:\n"
10020                "      {\n"
10021                "        break;\n"
10022                "      }\n"
10023                "    case 2:\n"
10024                "      {\n"
10025                "      }\n"
10026                "      break;\n"
10027                "    default:\n"
10028                "      break;\n"
10029                "    }\n"
10030                "}\n",
10031                GNUBraceStyle);
10032 
10033   verifyFormat("enum X\n"
10034                "{\n"
10035                "  Y = 0,\n"
10036                "}\n",
10037                GNUBraceStyle);
10038 
10039   verifyFormat("@interface BSApplicationController ()\n"
10040                "{\n"
10041                "@private\n"
10042                "  id _extraIvar;\n"
10043                "}\n"
10044                "@end\n",
10045                GNUBraceStyle);
10046 
10047   verifyFormat("#ifdef _DEBUG\n"
10048                "int foo(int i = 0)\n"
10049                "#else\n"
10050                "int foo(int i = 5)\n"
10051                "#endif\n"
10052                "{\n"
10053                "  return i;\n"
10054                "}",
10055                GNUBraceStyle);
10056 
10057   verifyFormat("void foo() {}\n"
10058                "void bar()\n"
10059                "#ifdef _DEBUG\n"
10060                "{\n"
10061                "  foo();\n"
10062                "}\n"
10063                "#else\n"
10064                "{\n"
10065                "}\n"
10066                "#endif",
10067                GNUBraceStyle);
10068 
10069   verifyFormat("void foobar() { int i = 5; }\n"
10070                "#ifdef _DEBUG\n"
10071                "void bar() {}\n"
10072                "#else\n"
10073                "void bar() { foobar(); }\n"
10074                "#endif",
10075                GNUBraceStyle);
10076 }
10077 
10078 TEST_F(FormatTest, WebKitBraceBreaking) {
10079   FormatStyle WebKitBraceStyle = getLLVMStyle();
10080   WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
10081   WebKitBraceStyle.FixNamespaceComments = false;
10082   verifyFormat("namespace a {\n"
10083                "class A {\n"
10084                "  void f()\n"
10085                "  {\n"
10086                "    if (true) {\n"
10087                "      a();\n"
10088                "      b();\n"
10089                "    }\n"
10090                "  }\n"
10091                "  void g() { return; }\n"
10092                "};\n"
10093                "enum E {\n"
10094                "  A,\n"
10095                "  // foo\n"
10096                "  B,\n"
10097                "  C\n"
10098                "};\n"
10099                "struct B {\n"
10100                "  int x;\n"
10101                "};\n"
10102                "}\n",
10103                WebKitBraceStyle);
10104   verifyFormat("struct S {\n"
10105                "  int Type;\n"
10106                "  union {\n"
10107                "    int x;\n"
10108                "    double y;\n"
10109                "  } Value;\n"
10110                "  class C {\n"
10111                "    MyFavoriteType Value;\n"
10112                "  } Class;\n"
10113                "};\n",
10114                WebKitBraceStyle);
10115 }
10116 
10117 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
10118   verifyFormat("void f() {\n"
10119                "  try {\n"
10120                "  } catch (const Exception &e) {\n"
10121                "  }\n"
10122                "}\n",
10123                getLLVMStyle());
10124 }
10125 
10126 TEST_F(FormatTest, UnderstandsPragmas) {
10127   verifyFormat("#pragma omp reduction(| : var)");
10128   verifyFormat("#pragma omp reduction(+ : var)");
10129 
10130   EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
10131             "(including parentheses).",
10132             format("#pragma    mark   Any non-hyphenated or hyphenated string "
10133                    "(including parentheses)."));
10134 }
10135 
10136 TEST_F(FormatTest, UnderstandPragmaOption) {
10137   verifyFormat("#pragma option -C -A");
10138 
10139   EXPECT_EQ("#pragma option -C -A", format("#pragma    option   -C   -A"));
10140 }
10141 
10142 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) {
10143   FormatStyle Style = getLLVMStyle();
10144   Style.ColumnLimit = 20;
10145 
10146   verifyFormat("int a; // the\n"
10147                "       // comment", Style);
10148   EXPECT_EQ("int a; /* first line\n"
10149             "        * second\n"
10150             "        * line third\n"
10151             "        * line\n"
10152             "        */",
10153             format("int a; /* first line\n"
10154                    "        * second\n"
10155                    "        * line third\n"
10156                    "        * line\n"
10157                    "        */",
10158                    Style));
10159   EXPECT_EQ("int a; // first line\n"
10160             "       // second\n"
10161             "       // line third\n"
10162             "       // line",
10163             format("int a; // first line\n"
10164                    "       // second line\n"
10165                    "       // third line",
10166                    Style));
10167 
10168   Style.PenaltyExcessCharacter = 90;
10169   verifyFormat("int a; // the comment", Style);
10170   EXPECT_EQ("int a; // the comment\n"
10171             "       // aaa",
10172             format("int a; // the comment aaa", Style));
10173   EXPECT_EQ("int a; /* first line\n"
10174             "        * second line\n"
10175             "        * third line\n"
10176             "        */",
10177             format("int a; /* first line\n"
10178                    "        * second line\n"
10179                    "        * third line\n"
10180                    "        */",
10181                    Style));
10182   EXPECT_EQ("int a; // first line\n"
10183             "       // second line\n"
10184             "       // third line",
10185             format("int a; // first line\n"
10186                    "       // second line\n"
10187                    "       // third line",
10188                    Style));
10189   // FIXME: Investigate why this is not getting the same layout as the test
10190   // above.
10191   EXPECT_EQ("int a; /* first line\n"
10192             "        * second line\n"
10193             "        * third line\n"
10194             "        */",
10195             format("int a; /* first line second line third line"
10196                    "\n*/",
10197                    Style));
10198 
10199   EXPECT_EQ("// foo bar baz bazfoo\n"
10200             "// foo bar foo bar\n",
10201             format("// foo bar baz bazfoo\n"
10202                    "// foo bar foo           bar\n",
10203                    Style));
10204   EXPECT_EQ("// foo bar baz bazfoo\n"
10205             "// foo bar foo bar\n",
10206             format("// foo bar baz      bazfoo\n"
10207                    "// foo            bar foo bar\n",
10208                    Style));
10209 
10210   // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the
10211   // next one.
10212   EXPECT_EQ("// foo bar baz bazfoo\n"
10213             "// bar foo bar\n",
10214             format("// foo bar baz      bazfoo bar\n"
10215                    "// foo            bar\n",
10216                    Style));
10217 
10218   EXPECT_EQ("// foo bar baz bazfoo\n"
10219             "// foo bar baz bazfoo\n"
10220             "// bar foo bar\n",
10221             format("// foo bar baz      bazfoo\n"
10222                    "// foo bar baz      bazfoo bar\n"
10223                    "// foo bar\n",
10224                    Style));
10225 
10226   EXPECT_EQ("// foo bar baz bazfoo\n"
10227             "// foo bar baz bazfoo\n"
10228             "// bar foo bar\n",
10229             format("// foo bar baz      bazfoo\n"
10230                    "// foo bar baz      bazfoo bar\n"
10231                    "// foo           bar\n",
10232                    Style));
10233 
10234   // Make sure we do not keep protruding characters if strict mode reflow is
10235   // cheaper than keeping protruding characters.
10236   Style.ColumnLimit = 21;
10237   EXPECT_EQ("// foo foo foo foo\n"
10238             "// foo foo foo foo\n"
10239             "// foo foo foo foo\n",
10240             format("// foo foo foo foo foo foo foo foo foo foo foo foo\n",
10241                            Style));
10242 
10243   EXPECT_EQ("int a = /* long block\n"
10244             "           comment */\n"
10245             "    42;",
10246             format("int a = /* long block comment */ 42;", Style));
10247 }
10248 
10249 #define EXPECT_ALL_STYLES_EQUAL(Styles)                                        \
10250   for (size_t i = 1; i < Styles.size(); ++i)                                   \
10251   EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \
10252                                   << " differs from Style #0"
10253 
10254 TEST_F(FormatTest, GetsPredefinedStyleByName) {
10255   SmallVector<FormatStyle, 3> Styles;
10256   Styles.resize(3);
10257 
10258   Styles[0] = getLLVMStyle();
10259   EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
10260   EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
10261   EXPECT_ALL_STYLES_EQUAL(Styles);
10262 
10263   Styles[0] = getGoogleStyle();
10264   EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
10265   EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
10266   EXPECT_ALL_STYLES_EQUAL(Styles);
10267 
10268   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
10269   EXPECT_TRUE(
10270       getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
10271   EXPECT_TRUE(
10272       getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
10273   EXPECT_ALL_STYLES_EQUAL(Styles);
10274 
10275   Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
10276   EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
10277   EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
10278   EXPECT_ALL_STYLES_EQUAL(Styles);
10279 
10280   Styles[0] = getMozillaStyle();
10281   EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
10282   EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
10283   EXPECT_ALL_STYLES_EQUAL(Styles);
10284 
10285   Styles[0] = getWebKitStyle();
10286   EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
10287   EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
10288   EXPECT_ALL_STYLES_EQUAL(Styles);
10289 
10290   Styles[0] = getGNUStyle();
10291   EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
10292   EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
10293   EXPECT_ALL_STYLES_EQUAL(Styles);
10294 
10295   EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
10296 }
10297 
10298 TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
10299   SmallVector<FormatStyle, 8> Styles;
10300   Styles.resize(2);
10301 
10302   Styles[0] = getGoogleStyle();
10303   Styles[1] = getLLVMStyle();
10304   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
10305   EXPECT_ALL_STYLES_EQUAL(Styles);
10306 
10307   Styles.resize(5);
10308   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
10309   Styles[1] = getLLVMStyle();
10310   Styles[1].Language = FormatStyle::LK_JavaScript;
10311   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
10312 
10313   Styles[2] = getLLVMStyle();
10314   Styles[2].Language = FormatStyle::LK_JavaScript;
10315   EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
10316                                   "BasedOnStyle: Google",
10317                                   &Styles[2])
10318                    .value());
10319 
10320   Styles[3] = getLLVMStyle();
10321   Styles[3].Language = FormatStyle::LK_JavaScript;
10322   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
10323                                   "Language: JavaScript",
10324                                   &Styles[3])
10325                    .value());
10326 
10327   Styles[4] = getLLVMStyle();
10328   Styles[4].Language = FormatStyle::LK_JavaScript;
10329   EXPECT_EQ(0, parseConfiguration("---\n"
10330                                   "BasedOnStyle: LLVM\n"
10331                                   "IndentWidth: 123\n"
10332                                   "---\n"
10333                                   "BasedOnStyle: Google\n"
10334                                   "Language: JavaScript",
10335                                   &Styles[4])
10336                    .value());
10337   EXPECT_ALL_STYLES_EQUAL(Styles);
10338 }
10339 
10340 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME)                             \
10341   Style.FIELD = false;                                                         \
10342   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value());      \
10343   EXPECT_TRUE(Style.FIELD);                                                    \
10344   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value());     \
10345   EXPECT_FALSE(Style.FIELD);
10346 
10347 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
10348 
10349 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME)              \
10350   Style.STRUCT.FIELD = false;                                                  \
10351   EXPECT_EQ(0,                                                                 \
10352             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": true", &Style)   \
10353                 .value());                                                     \
10354   EXPECT_TRUE(Style.STRUCT.FIELD);                                             \
10355   EXPECT_EQ(0,                                                                 \
10356             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": false", &Style)  \
10357                 .value());                                                     \
10358   EXPECT_FALSE(Style.STRUCT.FIELD);
10359 
10360 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD)                                 \
10361   CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD)
10362 
10363 #define CHECK_PARSE(TEXT, FIELD, VALUE)                                        \
10364   EXPECT_NE(VALUE, Style.FIELD);                                               \
10365   EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value());                      \
10366   EXPECT_EQ(VALUE, Style.FIELD)
10367 
10368 TEST_F(FormatTest, ParsesConfigurationBools) {
10369   FormatStyle Style = {};
10370   Style.Language = FormatStyle::LK_Cpp;
10371   CHECK_PARSE_BOOL(AlignOperands);
10372   CHECK_PARSE_BOOL(AlignTrailingComments);
10373   CHECK_PARSE_BOOL(AlignConsecutiveAssignments);
10374   CHECK_PARSE_BOOL(AlignConsecutiveDeclarations);
10375   CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
10376   CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine);
10377   CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
10378   CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine);
10379   CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
10380   CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations);
10381   CHECK_PARSE_BOOL(BinPackArguments);
10382   CHECK_PARSE_BOOL(BinPackParameters);
10383   CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations);
10384   CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
10385   CHECK_PARSE_BOOL(BreakStringLiterals);
10386   CHECK_PARSE_BOOL(BreakBeforeInheritanceComma)
10387   CHECK_PARSE_BOOL(CompactNamespaces);
10388   CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
10389   CHECK_PARSE_BOOL(DerivePointerAlignment);
10390   CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
10391   CHECK_PARSE_BOOL(DisableFormat);
10392   CHECK_PARSE_BOOL(IndentCaseLabels);
10393   CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
10394   CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
10395   CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
10396   CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
10397   CHECK_PARSE_BOOL(Cpp11BracedListStyle);
10398   CHECK_PARSE_BOOL(ReflowComments);
10399   CHECK_PARSE_BOOL(SortIncludes);
10400   CHECK_PARSE_BOOL(SortUsingDeclarations);
10401   CHECK_PARSE_BOOL(SpacesInParentheses);
10402   CHECK_PARSE_BOOL(SpacesInSquareBrackets);
10403   CHECK_PARSE_BOOL(SpacesInAngles);
10404   CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
10405   CHECK_PARSE_BOOL(SpacesInContainerLiterals);
10406   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
10407   CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
10408   CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword);
10409   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
10410   CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon);
10411   CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon);
10412   CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon);
10413 
10414   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass);
10415   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement);
10416   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum);
10417   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction);
10418   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace);
10419   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration);
10420   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct);
10421   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion);
10422   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock);
10423   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch);
10424   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse);
10425   CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces);
10426   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction);
10427   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord);
10428   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace);
10429 }
10430 
10431 #undef CHECK_PARSE_BOOL
10432 
10433 TEST_F(FormatTest, ParsesConfiguration) {
10434   FormatStyle Style = {};
10435   Style.Language = FormatStyle::LK_Cpp;
10436   CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
10437   CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
10438               ConstructorInitializerIndentWidth, 1234u);
10439   CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
10440   CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
10441   CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
10442   CHECK_PARSE("PenaltyBreakAssignment: 1234",
10443               PenaltyBreakAssignment, 1234u);
10444   CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
10445               PenaltyBreakBeforeFirstCallParameter, 1234u);
10446   CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
10447   CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
10448               PenaltyReturnTypeOnItsOwnLine, 1234u);
10449   CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
10450               SpacesBeforeTrailingComments, 1234u);
10451   CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
10452   CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
10453   CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$");
10454 
10455   Style.PointerAlignment = FormatStyle::PAS_Middle;
10456   CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
10457               FormatStyle::PAS_Left);
10458   CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
10459               FormatStyle::PAS_Right);
10460   CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
10461               FormatStyle::PAS_Middle);
10462   // For backward compatibility:
10463   CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
10464               FormatStyle::PAS_Left);
10465   CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
10466               FormatStyle::PAS_Right);
10467   CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
10468               FormatStyle::PAS_Middle);
10469 
10470   Style.Standard = FormatStyle::LS_Auto;
10471   CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
10472   CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11);
10473   CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
10474   CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
10475   CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
10476 
10477   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
10478   CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
10479               BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
10480   CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
10481               FormatStyle::BOS_None);
10482   CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
10483               FormatStyle::BOS_All);
10484   // For backward compatibility:
10485   CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
10486               FormatStyle::BOS_None);
10487   CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
10488               FormatStyle::BOS_All);
10489 
10490   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
10491   CHECK_PARSE("BreakConstructorInitializers: BeforeComma",
10492               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
10493   CHECK_PARSE("BreakConstructorInitializers: AfterColon",
10494               BreakConstructorInitializers, FormatStyle::BCIS_AfterColon);
10495   CHECK_PARSE("BreakConstructorInitializers: BeforeColon",
10496               BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon);
10497   // For backward compatibility:
10498   CHECK_PARSE("BreakConstructorInitializersBeforeComma: true",
10499               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
10500 
10501   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
10502   CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket,
10503               FormatStyle::BAS_Align);
10504   CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket,
10505               FormatStyle::BAS_DontAlign);
10506   CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket,
10507               FormatStyle::BAS_AlwaysBreak);
10508   // For backward compatibility:
10509   CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket,
10510               FormatStyle::BAS_DontAlign);
10511   CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket,
10512               FormatStyle::BAS_Align);
10513 
10514   Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
10515   CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines,
10516               FormatStyle::ENAS_DontAlign);
10517   CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines,
10518               FormatStyle::ENAS_Left);
10519   CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines,
10520               FormatStyle::ENAS_Right);
10521   // For backward compatibility:
10522   CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines,
10523               FormatStyle::ENAS_Left);
10524   CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines,
10525               FormatStyle::ENAS_Right);
10526 
10527   Style.UseTab = FormatStyle::UT_ForIndentation;
10528   CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
10529   CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
10530   CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
10531   CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab,
10532               FormatStyle::UT_ForContinuationAndIndentation);
10533   // For backward compatibility:
10534   CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
10535   CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
10536 
10537   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
10538   CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
10539               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
10540   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
10541               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
10542   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
10543               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
10544   CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
10545               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
10546   // For backward compatibility:
10547   CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
10548               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
10549   CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
10550               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
10551 
10552   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
10553   CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
10554               FormatStyle::SBPO_Never);
10555   CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
10556               FormatStyle::SBPO_Always);
10557   CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
10558               FormatStyle::SBPO_ControlStatements);
10559   // For backward compatibility:
10560   CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
10561               FormatStyle::SBPO_Never);
10562   CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
10563               FormatStyle::SBPO_ControlStatements);
10564 
10565   Style.ColumnLimit = 123;
10566   FormatStyle BaseStyle = getLLVMStyle();
10567   CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
10568   CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
10569 
10570   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
10571   CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
10572               FormatStyle::BS_Attach);
10573   CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
10574               FormatStyle::BS_Linux);
10575   CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces,
10576               FormatStyle::BS_Mozilla);
10577   CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
10578               FormatStyle::BS_Stroustrup);
10579   CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
10580               FormatStyle::BS_Allman);
10581   CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
10582   CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces,
10583               FormatStyle::BS_WebKit);
10584   CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces,
10585               FormatStyle::BS_Custom);
10586 
10587   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
10588   CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType,
10589               FormatStyle::RTBS_None);
10590   CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType,
10591               FormatStyle::RTBS_All);
10592   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel",
10593               AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel);
10594   CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions",
10595               AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions);
10596   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions",
10597               AlwaysBreakAfterReturnType,
10598               FormatStyle::RTBS_TopLevelDefinitions);
10599 
10600   Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
10601   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None",
10602               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None);
10603   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All",
10604               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All);
10605   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel",
10606               AlwaysBreakAfterDefinitionReturnType,
10607               FormatStyle::DRTBS_TopLevel);
10608 
10609   Style.NamespaceIndentation = FormatStyle::NI_All;
10610   CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
10611               FormatStyle::NI_None);
10612   CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
10613               FormatStyle::NI_Inner);
10614   CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
10615               FormatStyle::NI_All);
10616 
10617   // FIXME: This is required because parsing a configuration simply overwrites
10618   // the first N elements of the list instead of resetting it.
10619   Style.ForEachMacros.clear();
10620   std::vector<std::string> BoostForeach;
10621   BoostForeach.push_back("BOOST_FOREACH");
10622   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
10623   std::vector<std::string> BoostAndQForeach;
10624   BoostAndQForeach.push_back("BOOST_FOREACH");
10625   BoostAndQForeach.push_back("Q_FOREACH");
10626   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
10627               BoostAndQForeach);
10628 
10629   Style.IncludeCategories.clear();
10630   std::vector<FormatStyle::IncludeCategory> ExpectedCategories = {{"abc/.*", 2},
10631                                                                   {".*", 1}};
10632   CHECK_PARSE("IncludeCategories:\n"
10633               "  - Regex: abc/.*\n"
10634               "    Priority: 2\n"
10635               "  - Regex: .*\n"
10636               "    Priority: 1",
10637               IncludeCategories, ExpectedCategories);
10638   CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeIsMainRegex, "abc$");
10639 
10640   Style.RawStringFormats.clear();
10641   std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = {
10642       {
10643           FormatStyle::LK_TextProto,
10644           {"pb", "proto"},
10645           {"PARSE_TEXT_PROTO"},
10646           /*CanonicalDelimiter=*/"",
10647           "llvm",
10648       },
10649       {
10650           FormatStyle::LK_Cpp,
10651           {"cc", "cpp"},
10652           {"C_CODEBLOCK", "CPPEVAL"},
10653           /*CanonicalDelimiter=*/"cc",
10654           /*BasedOnStyle=*/"",
10655       },
10656   };
10657 
10658   CHECK_PARSE("RawStringFormats:\n"
10659               "  - Language: TextProto\n"
10660               "    Delimiters:\n"
10661               "      - 'pb'\n"
10662               "      - 'proto'\n"
10663               "    EnclosingFunctions:\n"
10664               "      - 'PARSE_TEXT_PROTO'\n"
10665               "    BasedOnStyle: llvm\n"
10666               "  - Language: Cpp\n"
10667               "    Delimiters:\n"
10668               "      - 'cc'\n"
10669               "      - 'cpp'\n"
10670               "    EnclosingFunctions:\n"
10671               "      - 'C_CODEBLOCK'\n"
10672               "      - 'CPPEVAL'\n"
10673               "    CanonicalDelimiter: 'cc'",
10674               RawStringFormats, ExpectedRawStringFormats);
10675 }
10676 
10677 TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
10678   FormatStyle Style = {};
10679   Style.Language = FormatStyle::LK_Cpp;
10680   CHECK_PARSE("Language: Cpp\n"
10681               "IndentWidth: 12",
10682               IndentWidth, 12u);
10683   EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
10684                                "IndentWidth: 34",
10685                                &Style),
10686             ParseError::Unsuitable);
10687   EXPECT_EQ(12u, Style.IndentWidth);
10688   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
10689   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
10690 
10691   Style.Language = FormatStyle::LK_JavaScript;
10692   CHECK_PARSE("Language: JavaScript\n"
10693               "IndentWidth: 12",
10694               IndentWidth, 12u);
10695   CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
10696   EXPECT_EQ(parseConfiguration("Language: Cpp\n"
10697                                "IndentWidth: 34",
10698                                &Style),
10699             ParseError::Unsuitable);
10700   EXPECT_EQ(23u, Style.IndentWidth);
10701   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
10702   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
10703 
10704   CHECK_PARSE("BasedOnStyle: LLVM\n"
10705               "IndentWidth: 67",
10706               IndentWidth, 67u);
10707 
10708   CHECK_PARSE("---\n"
10709               "Language: JavaScript\n"
10710               "IndentWidth: 12\n"
10711               "---\n"
10712               "Language: Cpp\n"
10713               "IndentWidth: 34\n"
10714               "...\n",
10715               IndentWidth, 12u);
10716 
10717   Style.Language = FormatStyle::LK_Cpp;
10718   CHECK_PARSE("---\n"
10719               "Language: JavaScript\n"
10720               "IndentWidth: 12\n"
10721               "---\n"
10722               "Language: Cpp\n"
10723               "IndentWidth: 34\n"
10724               "...\n",
10725               IndentWidth, 34u);
10726   CHECK_PARSE("---\n"
10727               "IndentWidth: 78\n"
10728               "---\n"
10729               "Language: JavaScript\n"
10730               "IndentWidth: 56\n"
10731               "...\n",
10732               IndentWidth, 78u);
10733 
10734   Style.ColumnLimit = 123;
10735   Style.IndentWidth = 234;
10736   Style.BreakBeforeBraces = FormatStyle::BS_Linux;
10737   Style.TabWidth = 345;
10738   EXPECT_FALSE(parseConfiguration("---\n"
10739                                   "IndentWidth: 456\n"
10740                                   "BreakBeforeBraces: Allman\n"
10741                                   "---\n"
10742                                   "Language: JavaScript\n"
10743                                   "IndentWidth: 111\n"
10744                                   "TabWidth: 111\n"
10745                                   "---\n"
10746                                   "Language: Cpp\n"
10747                                   "BreakBeforeBraces: Stroustrup\n"
10748                                   "TabWidth: 789\n"
10749                                   "...\n",
10750                                   &Style));
10751   EXPECT_EQ(123u, Style.ColumnLimit);
10752   EXPECT_EQ(456u, Style.IndentWidth);
10753   EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
10754   EXPECT_EQ(789u, Style.TabWidth);
10755 
10756   EXPECT_EQ(parseConfiguration("---\n"
10757                                "Language: JavaScript\n"
10758                                "IndentWidth: 56\n"
10759                                "---\n"
10760                                "IndentWidth: 78\n"
10761                                "...\n",
10762                                &Style),
10763             ParseError::Error);
10764   EXPECT_EQ(parseConfiguration("---\n"
10765                                "Language: JavaScript\n"
10766                                "IndentWidth: 56\n"
10767                                "---\n"
10768                                "Language: JavaScript\n"
10769                                "IndentWidth: 78\n"
10770                                "...\n",
10771                                &Style),
10772             ParseError::Error);
10773 
10774   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
10775 }
10776 
10777 #undef CHECK_PARSE
10778 
10779 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
10780   FormatStyle Style = {};
10781   Style.Language = FormatStyle::LK_JavaScript;
10782   Style.BreakBeforeTernaryOperators = true;
10783   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
10784   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
10785 
10786   Style.BreakBeforeTernaryOperators = true;
10787   EXPECT_EQ(0, parseConfiguration("---\n"
10788                                   "BasedOnStyle: Google\n"
10789                                   "---\n"
10790                                   "Language: JavaScript\n"
10791                                   "IndentWidth: 76\n"
10792                                   "...\n",
10793                                   &Style)
10794                    .value());
10795   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
10796   EXPECT_EQ(76u, Style.IndentWidth);
10797   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
10798 }
10799 
10800 TEST_F(FormatTest, ConfigurationRoundTripTest) {
10801   FormatStyle Style = getLLVMStyle();
10802   std::string YAML = configurationAsText(Style);
10803   FormatStyle ParsedStyle = {};
10804   ParsedStyle.Language = FormatStyle::LK_Cpp;
10805   EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
10806   EXPECT_EQ(Style, ParsedStyle);
10807 }
10808 
10809 TEST_F(FormatTest, WorksFor8bitEncodings) {
10810   EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
10811             "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
10812             "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
10813             "\"\xef\xee\xf0\xf3...\"",
10814             format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
10815                    "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
10816                    "\xef\xee\xf0\xf3...\"",
10817                    getLLVMStyleWithColumns(12)));
10818 }
10819 
10820 TEST_F(FormatTest, HandlesUTF8BOM) {
10821   EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
10822   EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
10823             format("\xef\xbb\xbf#include <iostream>"));
10824   EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
10825             format("\xef\xbb\xbf\n#include <iostream>"));
10826 }
10827 
10828 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
10829 #if !defined(_MSC_VER)
10830 
10831 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
10832   verifyFormat("\"Однажды в студёную зимнюю пору...\"",
10833                getLLVMStyleWithColumns(35));
10834   verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
10835                getLLVMStyleWithColumns(31));
10836   verifyFormat("// Однажды в студёную зимнюю пору...",
10837                getLLVMStyleWithColumns(36));
10838   verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
10839   verifyFormat("/* Однажды в студёную зимнюю пору... */",
10840                getLLVMStyleWithColumns(39));
10841   verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
10842                getLLVMStyleWithColumns(35));
10843 }
10844 
10845 TEST_F(FormatTest, SplitsUTF8Strings) {
10846   // Non-printable characters' width is currently considered to be the length in
10847   // bytes in UTF8. The characters can be displayed in very different manner
10848   // (zero-width, single width with a substitution glyph, expanded to their code
10849   // (e.g. "<8d>"), so there's no single correct way to handle them.
10850   EXPECT_EQ("\"aaaaÄ\"\n"
10851             "\"\xc2\x8d\";",
10852             format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
10853   EXPECT_EQ("\"aaaaaaaÄ\"\n"
10854             "\"\xc2\x8d\";",
10855             format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
10856   EXPECT_EQ("\"Однажды, в \"\n"
10857             "\"студёную \"\n"
10858             "\"зимнюю \"\n"
10859             "\"пору,\"",
10860             format("\"Однажды, в студёную зимнюю пору,\"",
10861                    getLLVMStyleWithColumns(13)));
10862   EXPECT_EQ(
10863       "\"一 二 三 \"\n"
10864       "\"四 五六 \"\n"
10865       "\"七 八 九 \"\n"
10866       "\"十\"",
10867       format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
10868   EXPECT_EQ("\"一\t\"\n"
10869             "\"二 \t\"\n"
10870             "\"三 四 \"\n"
10871             "\"五\t\"\n"
10872             "\"六 \t\"\n"
10873             "\"七 \"\n"
10874             "\"八九十\tqq\"",
10875             format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
10876                    getLLVMStyleWithColumns(11)));
10877 
10878   // UTF8 character in an escape sequence.
10879   EXPECT_EQ("\"aaaaaa\"\n"
10880             "\"\\\xC2\x8D\"",
10881             format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
10882 }
10883 
10884 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
10885   EXPECT_EQ("const char *sssss =\n"
10886             "    \"一二三四五六七八\\\n"
10887             " 九 十\";",
10888             format("const char *sssss = \"一二三四五六七八\\\n"
10889                    " 九 十\";",
10890                    getLLVMStyleWithColumns(30)));
10891 }
10892 
10893 TEST_F(FormatTest, SplitsUTF8LineComments) {
10894   EXPECT_EQ("// aaaaÄ\xc2\x8d",
10895             format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
10896   EXPECT_EQ("// Я из лесу\n"
10897             "// вышел; был\n"
10898             "// сильный\n"
10899             "// мороз.",
10900             format("// Я из лесу вышел; был сильный мороз.",
10901                    getLLVMStyleWithColumns(13)));
10902   EXPECT_EQ("// 一二三\n"
10903             "// 四五六七\n"
10904             "// 八  九\n"
10905             "// 十",
10906             format("// 一二三 四五六七 八  九 十", getLLVMStyleWithColumns(9)));
10907 }
10908 
10909 TEST_F(FormatTest, SplitsUTF8BlockComments) {
10910   EXPECT_EQ("/* Гляжу,\n"
10911             " * поднимается\n"
10912             " * медленно в\n"
10913             " * гору\n"
10914             " * Лошадка,\n"
10915             " * везущая\n"
10916             " * хворосту\n"
10917             " * воз. */",
10918             format("/* Гляжу, поднимается медленно в гору\n"
10919                    " * Лошадка, везущая хворосту воз. */",
10920                    getLLVMStyleWithColumns(13)));
10921   EXPECT_EQ(
10922       "/* 一二三\n"
10923       " * 四五六七\n"
10924       " * 八  九\n"
10925       " * 十  */",
10926       format("/* 一二三 四五六七 八  九 十  */", getLLVMStyleWithColumns(9)));
10927   EXPECT_EQ("/* �������� ��������\n"
10928             " * ��������\n"
10929             " * ������-�� */",
10930             format("/* �������� �������� �������� ������-�� */", getLLVMStyleWithColumns(12)));
10931 }
10932 
10933 #endif // _MSC_VER
10934 
10935 TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
10936   FormatStyle Style = getLLVMStyle();
10937 
10938   Style.ConstructorInitializerIndentWidth = 4;
10939   verifyFormat(
10940       "SomeClass::Constructor()\n"
10941       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
10942       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
10943       Style);
10944 
10945   Style.ConstructorInitializerIndentWidth = 2;
10946   verifyFormat(
10947       "SomeClass::Constructor()\n"
10948       "  : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
10949       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
10950       Style);
10951 
10952   Style.ConstructorInitializerIndentWidth = 0;
10953   verifyFormat(
10954       "SomeClass::Constructor()\n"
10955       ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
10956       "  aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
10957       Style);
10958   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
10959   verifyFormat(
10960       "SomeLongTemplateVariableName<\n"
10961       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>",
10962       Style);
10963   verifyFormat(
10964       "bool smaller = 1 < bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
10965       "                       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
10966       Style);
10967 }
10968 
10969 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
10970   FormatStyle Style = getLLVMStyle();
10971   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
10972   Style.ConstructorInitializerIndentWidth = 4;
10973   verifyFormat("SomeClass::Constructor()\n"
10974                "    : a(a)\n"
10975                "    , b(b)\n"
10976                "    , c(c) {}",
10977                Style);
10978   verifyFormat("SomeClass::Constructor()\n"
10979                "    : a(a) {}",
10980                Style);
10981 
10982   Style.ColumnLimit = 0;
10983   verifyFormat("SomeClass::Constructor()\n"
10984                "    : a(a) {}",
10985                Style);
10986   verifyFormat("SomeClass::Constructor() noexcept\n"
10987                "    : a(a) {}",
10988                Style);
10989   verifyFormat("SomeClass::Constructor()\n"
10990                "    : a(a)\n"
10991                "    , b(b)\n"
10992                "    , c(c) {}",
10993                Style);
10994   verifyFormat("SomeClass::Constructor()\n"
10995                "    : a(a) {\n"
10996                "  foo();\n"
10997                "  bar();\n"
10998                "}",
10999                Style);
11000 
11001   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
11002   verifyFormat("SomeClass::Constructor()\n"
11003                "    : a(a)\n"
11004                "    , b(b)\n"
11005                "    , c(c) {\n}",
11006                Style);
11007   verifyFormat("SomeClass::Constructor()\n"
11008                "    : a(a) {\n}",
11009                Style);
11010 
11011   Style.ColumnLimit = 80;
11012   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
11013   Style.ConstructorInitializerIndentWidth = 2;
11014   verifyFormat("SomeClass::Constructor()\n"
11015                "  : a(a)\n"
11016                "  , b(b)\n"
11017                "  , c(c) {}",
11018                Style);
11019 
11020   Style.ConstructorInitializerIndentWidth = 0;
11021   verifyFormat("SomeClass::Constructor()\n"
11022                ": a(a)\n"
11023                ", b(b)\n"
11024                ", c(c) {}",
11025                Style);
11026 
11027   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
11028   Style.ConstructorInitializerIndentWidth = 4;
11029   verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
11030   verifyFormat(
11031       "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
11032       Style);
11033   verifyFormat(
11034       "SomeClass::Constructor()\n"
11035       "    : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
11036       Style);
11037   Style.ConstructorInitializerIndentWidth = 4;
11038   Style.ColumnLimit = 60;
11039   verifyFormat("SomeClass::Constructor()\n"
11040                "    : aaaaaaaa(aaaaaaaa)\n"
11041                "    , aaaaaaaa(aaaaaaaa)\n"
11042                "    , aaaaaaaa(aaaaaaaa) {}",
11043                Style);
11044 }
11045 
11046 TEST_F(FormatTest, Destructors) {
11047   verifyFormat("void F(int &i) { i.~int(); }");
11048   verifyFormat("void F(int &i) { i->~int(); }");
11049 }
11050 
11051 TEST_F(FormatTest, FormatsWithWebKitStyle) {
11052   FormatStyle Style = getWebKitStyle();
11053 
11054   // Don't indent in outer namespaces.
11055   verifyFormat("namespace outer {\n"
11056                "int i;\n"
11057                "namespace inner {\n"
11058                "    int i;\n"
11059                "} // namespace inner\n"
11060                "} // namespace outer\n"
11061                "namespace other_outer {\n"
11062                "int i;\n"
11063                "}",
11064                Style);
11065 
11066   // Don't indent case labels.
11067   verifyFormat("switch (variable) {\n"
11068                "case 1:\n"
11069                "case 2:\n"
11070                "    doSomething();\n"
11071                "    break;\n"
11072                "default:\n"
11073                "    ++variable;\n"
11074                "}",
11075                Style);
11076 
11077   // Wrap before binary operators.
11078   EXPECT_EQ("void f()\n"
11079             "{\n"
11080             "    if (aaaaaaaaaaaaaaaa\n"
11081             "        && bbbbbbbbbbbbbbbbbbbbbbbb\n"
11082             "        && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
11083             "        return;\n"
11084             "}",
11085             format("void f() {\n"
11086                    "if (aaaaaaaaaaaaaaaa\n"
11087                    "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
11088                    "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
11089                    "return;\n"
11090                    "}",
11091                    Style));
11092 
11093   // Allow functions on a single line.
11094   verifyFormat("void f() { return; }", Style);
11095 
11096   // Constructor initializers are formatted one per line with the "," on the
11097   // new line.
11098   verifyFormat("Constructor()\n"
11099                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
11100                "    , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
11101                "          aaaaaaaaaaaaaa)\n"
11102                "    , aaaaaaaaaaaaaaaaaaaaaaa()\n"
11103                "{\n"
11104                "}",
11105                Style);
11106   verifyFormat("SomeClass::Constructor()\n"
11107                "    : a(a)\n"
11108                "{\n"
11109                "}",
11110                Style);
11111   EXPECT_EQ("SomeClass::Constructor()\n"
11112             "    : a(a)\n"
11113             "{\n"
11114             "}",
11115             format("SomeClass::Constructor():a(a){}", Style));
11116   verifyFormat("SomeClass::Constructor()\n"
11117                "    : a(a)\n"
11118                "    , b(b)\n"
11119                "    , c(c)\n"
11120                "{\n"
11121                "}",
11122                Style);
11123   verifyFormat("SomeClass::Constructor()\n"
11124                "    : a(a)\n"
11125                "{\n"
11126                "    foo();\n"
11127                "    bar();\n"
11128                "}",
11129                Style);
11130 
11131   // Access specifiers should be aligned left.
11132   verifyFormat("class C {\n"
11133                "public:\n"
11134                "    int i;\n"
11135                "};",
11136                Style);
11137 
11138   // Do not align comments.
11139   verifyFormat("int a; // Do not\n"
11140                "double b; // align comments.",
11141                Style);
11142 
11143   // Do not align operands.
11144   EXPECT_EQ("ASSERT(aaaa\n"
11145             "    || bbbb);",
11146             format("ASSERT ( aaaa\n||bbbb);", Style));
11147 
11148   // Accept input's line breaks.
11149   EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
11150             "    || bbbbbbbbbbbbbbb) {\n"
11151             "    i++;\n"
11152             "}",
11153             format("if (aaaaaaaaaaaaaaa\n"
11154                    "|| bbbbbbbbbbbbbbb) { i++; }",
11155                    Style));
11156   EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
11157             "    i++;\n"
11158             "}",
11159             format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
11160 
11161   // Don't automatically break all macro definitions (llvm.org/PR17842).
11162   verifyFormat("#define aNumber 10", Style);
11163   // However, generally keep the line breaks that the user authored.
11164   EXPECT_EQ("#define aNumber \\\n"
11165             "    10",
11166             format("#define aNumber \\\n"
11167                    " 10",
11168                    Style));
11169 
11170   // Keep empty and one-element array literals on a single line.
11171   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
11172             "                                  copyItems:YES];",
11173             format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
11174                    "copyItems:YES];",
11175                    Style));
11176   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
11177             "                                  copyItems:YES];",
11178             format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
11179                    "             copyItems:YES];",
11180                    Style));
11181   // FIXME: This does not seem right, there should be more indentation before
11182   // the array literal's entries. Nested blocks have the same problem.
11183   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
11184             "    @\"a\",\n"
11185             "    @\"a\"\n"
11186             "]\n"
11187             "                                  copyItems:YES];",
11188             format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
11189                    "     @\"a\",\n"
11190                    "     @\"a\"\n"
11191                    "     ]\n"
11192                    "       copyItems:YES];",
11193                    Style));
11194   EXPECT_EQ(
11195       "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
11196       "                                  copyItems:YES];",
11197       format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
11198              "   copyItems:YES];",
11199              Style));
11200 
11201   verifyFormat("[self.a b:c c:d];", Style);
11202   EXPECT_EQ("[self.a b:c\n"
11203             "        c:d];",
11204             format("[self.a b:c\n"
11205                    "c:d];",
11206                    Style));
11207 }
11208 
11209 TEST_F(FormatTest, FormatsLambdas) {
11210   verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
11211   verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
11212   verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
11213   verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
11214   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
11215   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
11216   verifyFormat("auto c = [a = [b = 42] {}] {};\n");
11217   verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n");
11218   verifyFormat("int x = f(*+[] {});");
11219   verifyFormat("void f() {\n"
11220                "  other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
11221                "}\n");
11222   verifyFormat("void f() {\n"
11223                "  other(x.begin(), //\n"
11224                "        x.end(),   //\n"
11225                "        [&](int, int) { return 1; });\n"
11226                "}\n");
11227   verifyFormat("SomeFunction([]() { // A cool function...\n"
11228                "  return 43;\n"
11229                "});");
11230   EXPECT_EQ("SomeFunction([]() {\n"
11231             "#define A a\n"
11232             "  return 43;\n"
11233             "});",
11234             format("SomeFunction([](){\n"
11235                    "#define A a\n"
11236                    "return 43;\n"
11237                    "});"));
11238   verifyFormat("void f() {\n"
11239                "  SomeFunction([](decltype(x), A *a) {});\n"
11240                "}");
11241   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11242                "    [](const aaaaaaaaaa &a) { return a; });");
11243   verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
11244                "  SomeOtherFunctioooooooooooooooooooooooooon();\n"
11245                "});");
11246   verifyFormat("Constructor()\n"
11247                "    : Field([] { // comment\n"
11248                "        int i;\n"
11249                "      }) {}");
11250   verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
11251                "  return some_parameter.size();\n"
11252                "};");
11253   verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n"
11254                "    [](const string &s) { return s; };");
11255   verifyFormat("int i = aaaaaa ? 1 //\n"
11256                "               : [] {\n"
11257                "                   return 2; //\n"
11258                "                 }();");
11259   verifyFormat("llvm::errs() << \"number of twos is \"\n"
11260                "             << std::count_if(v.begin(), v.end(), [](int x) {\n"
11261                "                  return x == 2; // force break\n"
11262                "                });");
11263   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11264                "    [=](int iiiiiiiiiiii) {\n"
11265                "      return aaaaaaaaaaaaaaaaaaaaaaa !=\n"
11266                "             aaaaaaaaaaaaaaaaaaaaaaa;\n"
11267                "    });",
11268                getLLVMStyleWithColumns(60));
11269   verifyFormat("SomeFunction({[&] {\n"
11270                "                // comment\n"
11271                "              },\n"
11272                "              [&] {\n"
11273                "                // comment\n"
11274                "              }});");
11275   verifyFormat("SomeFunction({[&] {\n"
11276                "  // comment\n"
11277                "}});");
11278   verifyFormat("virtual aaaaaaaaaaaaaaaa(std::function<bool()> bbbbbbbbbbbb =\n"
11279                "                             [&]() { return true; },\n"
11280                "                         aaaaa aaaaaaaaa);");
11281 
11282   // Lambdas with return types.
11283   verifyFormat("int c = []() -> int { return 2; }();\n");
11284   verifyFormat("int c = []() -> int * { return 2; }();\n");
11285   verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
11286   verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
11287   verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
11288   verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
11289   verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
11290   verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
11291   verifyFormat("[a, a]() -> a<1> {};");
11292   verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
11293                "                   int j) -> int {\n"
11294                "  return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
11295                "};");
11296   verifyFormat(
11297       "aaaaaaaaaaaaaaaaaaaaaa(\n"
11298       "    [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
11299       "      return aaaaaaaaaaaaaaaaa;\n"
11300       "    });",
11301       getLLVMStyleWithColumns(70));
11302   verifyFormat("[]() //\n"
11303                "    -> int {\n"
11304                "  return 1; //\n"
11305                "};");
11306 
11307   // Multiple lambdas in the same parentheses change indentation rules.
11308   verifyFormat("SomeFunction(\n"
11309                "    []() {\n"
11310                "      int i = 42;\n"
11311                "      return i;\n"
11312                "    },\n"
11313                "    []() {\n"
11314                "      int j = 43;\n"
11315                "      return j;\n"
11316                "    });");
11317 
11318   // More complex introducers.
11319   verifyFormat("return [i, args...] {};");
11320 
11321   // Not lambdas.
11322   verifyFormat("constexpr char hello[]{\"hello\"};");
11323   verifyFormat("double &operator[](int i) { return 0; }\n"
11324                "int i;");
11325   verifyFormat("std::unique_ptr<int[]> foo() {}");
11326   verifyFormat("int i = a[a][a]->f();");
11327   verifyFormat("int i = (*b)[a]->f();");
11328 
11329   // Other corner cases.
11330   verifyFormat("void f() {\n"
11331                "  bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
11332                "  );\n"
11333                "}");
11334 
11335   // Lambdas created through weird macros.
11336   verifyFormat("void f() {\n"
11337                "  MACRO((const AA &a) { return 1; });\n"
11338                "  MACRO((AA &a) { return 1; });\n"
11339                "}");
11340 
11341   verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
11342                "      doo_dah();\n"
11343                "      doo_dah();\n"
11344                "    })) {\n"
11345                "}");
11346   verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n"
11347                "                doo_dah();\n"
11348                "                doo_dah();\n"
11349                "              })) {\n"
11350                "}");
11351   verifyFormat("auto lambda = []() {\n"
11352                "  int a = 2\n"
11353                "#if A\n"
11354                "          + 2\n"
11355                "#endif\n"
11356                "      ;\n"
11357                "};");
11358 
11359   // Lambdas with complex multiline introducers.
11360   verifyFormat(
11361       "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11362       "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n"
11363       "        -> ::std::unordered_set<\n"
11364       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
11365       "      //\n"
11366       "    });");
11367 }
11368 
11369 TEST_F(FormatTest, EmptyLinesInLambdas) {
11370   verifyFormat("auto lambda = []() {\n"
11371                "  x(); //\n"
11372                "};",
11373                "auto lambda = []() {\n"
11374                "\n"
11375                "  x(); //\n"
11376                "\n"
11377                "};");
11378 }
11379 
11380 TEST_F(FormatTest, FormatsBlocks) {
11381   FormatStyle ShortBlocks = getLLVMStyle();
11382   ShortBlocks.AllowShortBlocksOnASingleLine = true;
11383   verifyFormat("int (^Block)(int, int);", ShortBlocks);
11384   verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
11385   verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
11386   verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
11387   verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
11388   verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
11389 
11390   verifyFormat("foo(^{ bar(); });", ShortBlocks);
11391   verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
11392   verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
11393 
11394   verifyFormat("[operation setCompletionBlock:^{\n"
11395                "  [self onOperationDone];\n"
11396                "}];");
11397   verifyFormat("int i = {[operation setCompletionBlock:^{\n"
11398                "  [self onOperationDone];\n"
11399                "}]};");
11400   verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
11401                "  f();\n"
11402                "}];");
11403   verifyFormat("int a = [operation block:^int(int *i) {\n"
11404                "  return 1;\n"
11405                "}];");
11406   verifyFormat("[myObject doSomethingWith:arg1\n"
11407                "                      aaa:^int(int *a) {\n"
11408                "                        return 1;\n"
11409                "                      }\n"
11410                "                      bbb:f(a * bbbbbbbb)];");
11411 
11412   verifyFormat("[operation setCompletionBlock:^{\n"
11413                "  [self.delegate newDataAvailable];\n"
11414                "}];",
11415                getLLVMStyleWithColumns(60));
11416   verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
11417                "  NSString *path = [self sessionFilePath];\n"
11418                "  if (path) {\n"
11419                "    // ...\n"
11420                "  }\n"
11421                "});");
11422   verifyFormat("[[SessionService sharedService]\n"
11423                "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
11424                "      if (window) {\n"
11425                "        [self windowDidLoad:window];\n"
11426                "      } else {\n"
11427                "        [self errorLoadingWindow];\n"
11428                "      }\n"
11429                "    }];");
11430   verifyFormat("void (^largeBlock)(void) = ^{\n"
11431                "  // ...\n"
11432                "};\n",
11433                getLLVMStyleWithColumns(40));
11434   verifyFormat("[[SessionService sharedService]\n"
11435                "    loadWindowWithCompletionBlock: //\n"
11436                "        ^(SessionWindow *window) {\n"
11437                "          if (window) {\n"
11438                "            [self windowDidLoad:window];\n"
11439                "          } else {\n"
11440                "            [self errorLoadingWindow];\n"
11441                "          }\n"
11442                "        }];",
11443                getLLVMStyleWithColumns(60));
11444   verifyFormat("[myObject doSomethingWith:arg1\n"
11445                "    firstBlock:^(Foo *a) {\n"
11446                "      // ...\n"
11447                "      int i;\n"
11448                "    }\n"
11449                "    secondBlock:^(Bar *b) {\n"
11450                "      // ...\n"
11451                "      int i;\n"
11452                "    }\n"
11453                "    thirdBlock:^Foo(Bar *b) {\n"
11454                "      // ...\n"
11455                "      int i;\n"
11456                "    }];");
11457   verifyFormat("[myObject doSomethingWith:arg1\n"
11458                "               firstBlock:-1\n"
11459                "              secondBlock:^(Bar *b) {\n"
11460                "                // ...\n"
11461                "                int i;\n"
11462                "              }];");
11463 
11464   verifyFormat("f(^{\n"
11465                "  @autoreleasepool {\n"
11466                "    if (a) {\n"
11467                "      g();\n"
11468                "    }\n"
11469                "  }\n"
11470                "});");
11471   verifyFormat("Block b = ^int *(A *a, B *b) {}");
11472   verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n"
11473                "};");
11474 
11475   FormatStyle FourIndent = getLLVMStyle();
11476   FourIndent.ObjCBlockIndentWidth = 4;
11477   verifyFormat("[operation setCompletionBlock:^{\n"
11478                "    [self onOperationDone];\n"
11479                "}];",
11480                FourIndent);
11481 }
11482 
11483 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
11484   FormatStyle ZeroColumn = getLLVMStyle();
11485   ZeroColumn.ColumnLimit = 0;
11486 
11487   verifyFormat("[[SessionService sharedService] "
11488                "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
11489                "  if (window) {\n"
11490                "    [self windowDidLoad:window];\n"
11491                "  } else {\n"
11492                "    [self errorLoadingWindow];\n"
11493                "  }\n"
11494                "}];",
11495                ZeroColumn);
11496   EXPECT_EQ("[[SessionService sharedService]\n"
11497             "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
11498             "      if (window) {\n"
11499             "        [self windowDidLoad:window];\n"
11500             "      } else {\n"
11501             "        [self errorLoadingWindow];\n"
11502             "      }\n"
11503             "    }];",
11504             format("[[SessionService sharedService]\n"
11505                    "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
11506                    "                if (window) {\n"
11507                    "    [self windowDidLoad:window];\n"
11508                    "  } else {\n"
11509                    "    [self errorLoadingWindow];\n"
11510                    "  }\n"
11511                    "}];",
11512                    ZeroColumn));
11513   verifyFormat("[myObject doSomethingWith:arg1\n"
11514                "    firstBlock:^(Foo *a) {\n"
11515                "      // ...\n"
11516                "      int i;\n"
11517                "    }\n"
11518                "    secondBlock:^(Bar *b) {\n"
11519                "      // ...\n"
11520                "      int i;\n"
11521                "    }\n"
11522                "    thirdBlock:^Foo(Bar *b) {\n"
11523                "      // ...\n"
11524                "      int i;\n"
11525                "    }];",
11526                ZeroColumn);
11527   verifyFormat("f(^{\n"
11528                "  @autoreleasepool {\n"
11529                "    if (a) {\n"
11530                "      g();\n"
11531                "    }\n"
11532                "  }\n"
11533                "});",
11534                ZeroColumn);
11535   verifyFormat("void (^largeBlock)(void) = ^{\n"
11536                "  // ...\n"
11537                "};",
11538                ZeroColumn);
11539 
11540   ZeroColumn.AllowShortBlocksOnASingleLine = true;
11541   EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
11542             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
11543   ZeroColumn.AllowShortBlocksOnASingleLine = false;
11544   EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
11545             "  int i;\n"
11546             "};",
11547             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
11548 }
11549 
11550 TEST_F(FormatTest, SupportsCRLF) {
11551   EXPECT_EQ("int a;\r\n"
11552             "int b;\r\n"
11553             "int c;\r\n",
11554             format("int a;\r\n"
11555                    "  int b;\r\n"
11556                    "    int c;\r\n",
11557                    getLLVMStyle()));
11558   EXPECT_EQ("int a;\r\n"
11559             "int b;\r\n"
11560             "int c;\r\n",
11561             format("int a;\r\n"
11562                    "  int b;\n"
11563                    "    int c;\r\n",
11564                    getLLVMStyle()));
11565   EXPECT_EQ("int a;\n"
11566             "int b;\n"
11567             "int c;\n",
11568             format("int a;\r\n"
11569                    "  int b;\n"
11570                    "    int c;\n",
11571                    getLLVMStyle()));
11572   EXPECT_EQ("\"aaaaaaa \"\r\n"
11573             "\"bbbbbbb\";\r\n",
11574             format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
11575   EXPECT_EQ("#define A \\\r\n"
11576             "  b;      \\\r\n"
11577             "  c;      \\\r\n"
11578             "  d;\r\n",
11579             format("#define A \\\r\n"
11580                    "  b; \\\r\n"
11581                    "  c; d; \r\n",
11582                    getGoogleStyle()));
11583 
11584   EXPECT_EQ("/*\r\n"
11585             "multi line block comments\r\n"
11586             "should not introduce\r\n"
11587             "an extra carriage return\r\n"
11588             "*/\r\n",
11589             format("/*\r\n"
11590                    "multi line block comments\r\n"
11591                    "should not introduce\r\n"
11592                    "an extra carriage return\r\n"
11593                    "*/\r\n"));
11594 }
11595 
11596 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
11597   verifyFormat("MY_CLASS(C) {\n"
11598                "  int i;\n"
11599                "  int j;\n"
11600                "};");
11601 }
11602 
11603 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
11604   FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
11605   TwoIndent.ContinuationIndentWidth = 2;
11606 
11607   EXPECT_EQ("int i =\n"
11608             "  longFunction(\n"
11609             "    arg);",
11610             format("int i = longFunction(arg);", TwoIndent));
11611 
11612   FormatStyle SixIndent = getLLVMStyleWithColumns(20);
11613   SixIndent.ContinuationIndentWidth = 6;
11614 
11615   EXPECT_EQ("int i =\n"
11616             "      longFunction(\n"
11617             "            arg);",
11618             format("int i = longFunction(arg);", SixIndent));
11619 }
11620 
11621 TEST_F(FormatTest, SpacesInAngles) {
11622   FormatStyle Spaces = getLLVMStyle();
11623   Spaces.SpacesInAngles = true;
11624 
11625   verifyFormat("static_cast< int >(arg);", Spaces);
11626   verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
11627   verifyFormat("f< int, float >();", Spaces);
11628   verifyFormat("template <> g() {}", Spaces);
11629   verifyFormat("template < std::vector< int > > f() {}", Spaces);
11630   verifyFormat("std::function< void(int, int) > fct;", Spaces);
11631   verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
11632                Spaces);
11633 
11634   Spaces.Standard = FormatStyle::LS_Cpp03;
11635   Spaces.SpacesInAngles = true;
11636   verifyFormat("A< A< int > >();", Spaces);
11637 
11638   Spaces.SpacesInAngles = false;
11639   verifyFormat("A<A<int> >();", Spaces);
11640 
11641   Spaces.Standard = FormatStyle::LS_Cpp11;
11642   Spaces.SpacesInAngles = true;
11643   verifyFormat("A< A< int > >();", Spaces);
11644 
11645   Spaces.SpacesInAngles = false;
11646   verifyFormat("A<A<int>>();", Spaces);
11647 }
11648 
11649 TEST_F(FormatTest, SpaceAfterTemplateKeyword) {
11650   FormatStyle Style = getLLVMStyle();
11651   Style.SpaceAfterTemplateKeyword = false;
11652   verifyFormat("template<int> void foo();", Style);
11653 }
11654 
11655 TEST_F(FormatTest, TripleAngleBrackets) {
11656   verifyFormat("f<<<1, 1>>>();");
11657   verifyFormat("f<<<1, 1, 1, s>>>();");
11658   verifyFormat("f<<<a, b, c, d>>>();");
11659   EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();"));
11660   verifyFormat("f<param><<<1, 1>>>();");
11661   verifyFormat("f<1><<<1, 1>>>();");
11662   EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();"));
11663   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
11664                "aaaaaaaaaaa<<<\n    1, 1>>>();");
11665   verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n"
11666                "    <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();");
11667 }
11668 
11669 TEST_F(FormatTest, MergeLessLessAtEnd) {
11670   verifyFormat("<<");
11671   EXPECT_EQ("< < <", format("\\\n<<<"));
11672   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
11673                "aaallvm::outs() <<");
11674   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
11675                "aaaallvm::outs()\n    <<");
11676 }
11677 
11678 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
11679   std::string code = "#if A\n"
11680                      "#if B\n"
11681                      "a.\n"
11682                      "#endif\n"
11683                      "    a = 1;\n"
11684                      "#else\n"
11685                      "#endif\n"
11686                      "#if C\n"
11687                      "#else\n"
11688                      "#endif\n";
11689   EXPECT_EQ(code, format(code));
11690 }
11691 
11692 TEST_F(FormatTest, HandleConflictMarkers) {
11693   // Git/SVN conflict markers.
11694   EXPECT_EQ("int a;\n"
11695             "void f() {\n"
11696             "  callme(some(parameter1,\n"
11697             "<<<<<<< text by the vcs\n"
11698             "              parameter2),\n"
11699             "||||||| text by the vcs\n"
11700             "              parameter2),\n"
11701             "         parameter3,\n"
11702             "======= text by the vcs\n"
11703             "              parameter2, parameter3),\n"
11704             ">>>>>>> text by the vcs\n"
11705             "         otherparameter);\n",
11706             format("int a;\n"
11707                    "void f() {\n"
11708                    "  callme(some(parameter1,\n"
11709                    "<<<<<<< text by the vcs\n"
11710                    "  parameter2),\n"
11711                    "||||||| text by the vcs\n"
11712                    "  parameter2),\n"
11713                    "  parameter3,\n"
11714                    "======= text by the vcs\n"
11715                    "  parameter2,\n"
11716                    "  parameter3),\n"
11717                    ">>>>>>> text by the vcs\n"
11718                    "  otherparameter);\n"));
11719 
11720   // Perforce markers.
11721   EXPECT_EQ("void f() {\n"
11722             "  function(\n"
11723             ">>>> text by the vcs\n"
11724             "      parameter,\n"
11725             "==== text by the vcs\n"
11726             "      parameter,\n"
11727             "==== text by the vcs\n"
11728             "      parameter,\n"
11729             "<<<< text by the vcs\n"
11730             "      parameter);\n",
11731             format("void f() {\n"
11732                    "  function(\n"
11733                    ">>>> text by the vcs\n"
11734                    "  parameter,\n"
11735                    "==== text by the vcs\n"
11736                    "  parameter,\n"
11737                    "==== text by the vcs\n"
11738                    "  parameter,\n"
11739                    "<<<< text by the vcs\n"
11740                    "  parameter);\n"));
11741 
11742   EXPECT_EQ("<<<<<<<\n"
11743             "|||||||\n"
11744             "=======\n"
11745             ">>>>>>>",
11746             format("<<<<<<<\n"
11747                    "|||||||\n"
11748                    "=======\n"
11749                    ">>>>>>>"));
11750 
11751   EXPECT_EQ("<<<<<<<\n"
11752             "|||||||\n"
11753             "int i;\n"
11754             "=======\n"
11755             ">>>>>>>",
11756             format("<<<<<<<\n"
11757                    "|||||||\n"
11758                    "int i;\n"
11759                    "=======\n"
11760                    ">>>>>>>"));
11761 
11762   // FIXME: Handle parsing of macros around conflict markers correctly:
11763   EXPECT_EQ("#define Macro \\\n"
11764             "<<<<<<<\n"
11765             "Something \\\n"
11766             "|||||||\n"
11767             "Else \\\n"
11768             "=======\n"
11769             "Other \\\n"
11770             ">>>>>>>\n"
11771             "    End int i;\n",
11772             format("#define Macro \\\n"
11773                    "<<<<<<<\n"
11774                    "  Something \\\n"
11775                    "|||||||\n"
11776                    "  Else \\\n"
11777                    "=======\n"
11778                    "  Other \\\n"
11779                    ">>>>>>>\n"
11780                    "  End\n"
11781                    "int i;\n"));
11782 }
11783 
11784 TEST_F(FormatTest, DisableRegions) {
11785   EXPECT_EQ("int i;\n"
11786             "// clang-format off\n"
11787             "  int j;\n"
11788             "// clang-format on\n"
11789             "int k;",
11790             format(" int  i;\n"
11791                    "   // clang-format off\n"
11792                    "  int j;\n"
11793                    " // clang-format on\n"
11794                    "   int   k;"));
11795   EXPECT_EQ("int i;\n"
11796             "/* clang-format off */\n"
11797             "  int j;\n"
11798             "/* clang-format on */\n"
11799             "int k;",
11800             format(" int  i;\n"
11801                    "   /* clang-format off */\n"
11802                    "  int j;\n"
11803                    " /* clang-format on */\n"
11804                    "   int   k;"));
11805 
11806   // Don't reflow comments within disabled regions.
11807   EXPECT_EQ(
11808       "// clang-format off\n"
11809       "// long long long long long long line\n"
11810       "/* clang-format on */\n"
11811       "/* long long long\n"
11812       " * long long long\n"
11813       " * line */\n"
11814       "int i;\n"
11815       "/* clang-format off */\n"
11816       "/* long long long long long long line */\n",
11817       format("// clang-format off\n"
11818              "// long long long long long long line\n"
11819              "/* clang-format on */\n"
11820              "/* long long long long long long line */\n"
11821              "int i;\n"
11822              "/* clang-format off */\n"
11823              "/* long long long long long long line */\n",
11824              getLLVMStyleWithColumns(20)));
11825 }
11826 
11827 TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
11828   format("? ) =");
11829   verifyNoCrash("#define a\\\n /**/}");
11830 }
11831 
11832 TEST_F(FormatTest, FormatsTableGenCode) {
11833   FormatStyle Style = getLLVMStyle();
11834   Style.Language = FormatStyle::LK_TableGen;
11835   verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style);
11836 }
11837 
11838 TEST_F(FormatTest, ArrayOfTemplates) {
11839   EXPECT_EQ("auto a = new unique_ptr<int>[10];",
11840             format("auto a = new unique_ptr<int > [ 10];"));
11841 
11842   FormatStyle Spaces = getLLVMStyle();
11843   Spaces.SpacesInSquareBrackets = true;
11844   EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];",
11845             format("auto a = new unique_ptr<int > [10];", Spaces));
11846 }
11847 
11848 TEST_F(FormatTest, ArrayAsTemplateType) {
11849   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;",
11850             format("auto a = unique_ptr < Foo < Bar>[ 10]> ;"));
11851 
11852   FormatStyle Spaces = getLLVMStyle();
11853   Spaces.SpacesInSquareBrackets = true;
11854   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;",
11855             format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces));
11856 }
11857 
11858 TEST_F(FormatTest, NoSpaceAfterSuper) {
11859     verifyFormat("__super::FooBar();");
11860 }
11861 
11862 TEST(FormatStyle, GetStyleWithEmptyFileName) {
11863   vfs::InMemoryFileSystem FS;
11864   auto Style1 = getStyle("file", "", "Google", "", &FS);
11865   ASSERT_TRUE((bool)Style1);
11866   ASSERT_EQ(*Style1, getGoogleStyle());
11867 }
11868 
11869 TEST(FormatStyle, GetStyleOfFile) {
11870   vfs::InMemoryFileSystem FS;
11871   // Test 1: format file in the same directory.
11872   ASSERT_TRUE(
11873       FS.addFile("/a/.clang-format", 0,
11874                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM")));
11875   ASSERT_TRUE(
11876       FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
11877   auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS);
11878   ASSERT_TRUE((bool)Style1);
11879   ASSERT_EQ(*Style1, getLLVMStyle());
11880 
11881   // Test 2.1: fallback to default.
11882   ASSERT_TRUE(
11883       FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
11884   auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS);
11885   ASSERT_TRUE((bool)Style2);
11886   ASSERT_EQ(*Style2, getMozillaStyle());
11887 
11888   // Test 2.2: no format on 'none' fallback style.
11889   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
11890   ASSERT_TRUE((bool)Style2);
11891   ASSERT_EQ(*Style2, getNoStyle());
11892 
11893   // Test 2.3: format if config is found with no based style while fallback is
11894   // 'none'.
11895   ASSERT_TRUE(FS.addFile("/b/.clang-format", 0,
11896                          llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2")));
11897   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
11898   ASSERT_TRUE((bool)Style2);
11899   ASSERT_EQ(*Style2, getLLVMStyle());
11900 
11901   // Test 2.4: format if yaml with no based style, while fallback is 'none'.
11902   Style2 = getStyle("{}", "a.h", "none", "", &FS);
11903   ASSERT_TRUE((bool)Style2);
11904   ASSERT_EQ(*Style2, getLLVMStyle());
11905 
11906   // Test 3: format file in parent directory.
11907   ASSERT_TRUE(
11908       FS.addFile("/c/.clang-format", 0,
11909                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
11910   ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0,
11911                          llvm::MemoryBuffer::getMemBuffer("int i;")));
11912   auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS);
11913   ASSERT_TRUE((bool)Style3);
11914   ASSERT_EQ(*Style3, getGoogleStyle());
11915 
11916   // Test 4: error on invalid fallback style
11917   auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS);
11918   ASSERT_FALSE((bool)Style4);
11919   llvm::consumeError(Style4.takeError());
11920 
11921   // Test 5: error on invalid yaml on command line
11922   auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS);
11923   ASSERT_FALSE((bool)Style5);
11924   llvm::consumeError(Style5.takeError());
11925 
11926   // Test 6: error on invalid style
11927   auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS);
11928   ASSERT_FALSE((bool)Style6);
11929   llvm::consumeError(Style6.takeError());
11930 
11931   // Test 7: found config file, error on parsing it
11932   ASSERT_TRUE(
11933       FS.addFile("/d/.clang-format", 0,
11934                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n"
11935                                                   "InvalidKey: InvalidValue")));
11936   ASSERT_TRUE(
11937       FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
11938   auto Style7 = getStyle("file", "/d/.clang-format", "LLVM", "", &FS);
11939   ASSERT_FALSE((bool)Style7);
11940   llvm::consumeError(Style7.takeError());
11941 }
11942 
11943 TEST_F(ReplacementTest, FormatCodeAfterReplacements) {
11944   // Column limit is 20.
11945   std::string Code = "Type *a =\n"
11946                      "    new Type();\n"
11947                      "g(iiiii, 0, jjjjj,\n"
11948                      "  0, kkkkk, 0, mm);\n"
11949                      "int  bad     = format   ;";
11950   std::string Expected = "auto a = new Type();\n"
11951                          "g(iiiii, nullptr,\n"
11952                          "  jjjjj, nullptr,\n"
11953                          "  kkkkk, nullptr,\n"
11954                          "  mm);\n"
11955                          "int  bad     = format   ;";
11956   FileID ID = Context.createInMemoryFile("format.cpp", Code);
11957   tooling::Replacements Replaces = toReplacements(
11958       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6,
11959                             "auto "),
11960        tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1,
11961                             "nullptr"),
11962        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1,
11963                             "nullptr"),
11964        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1,
11965                             "nullptr")});
11966 
11967   format::FormatStyle Style = format::getLLVMStyle();
11968   Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility.
11969   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
11970   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
11971       << llvm::toString(FormattedReplaces.takeError()) << "\n";
11972   auto Result = applyAllReplacements(Code, *FormattedReplaces);
11973   EXPECT_TRUE(static_cast<bool>(Result));
11974   EXPECT_EQ(Expected, *Result);
11975 }
11976 
11977 TEST_F(ReplacementTest, SortIncludesAfterReplacement) {
11978   std::string Code = "#include \"a.h\"\n"
11979                      "#include \"c.h\"\n"
11980                      "\n"
11981                      "int main() {\n"
11982                      "  return 0;\n"
11983                      "}";
11984   std::string Expected = "#include \"a.h\"\n"
11985                          "#include \"b.h\"\n"
11986                          "#include \"c.h\"\n"
11987                          "\n"
11988                          "int main() {\n"
11989                          "  return 0;\n"
11990                          "}";
11991   FileID ID = Context.createInMemoryFile("fix.cpp", Code);
11992   tooling::Replacements Replaces = toReplacements(
11993       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0,
11994                             "#include \"b.h\"\n")});
11995 
11996   format::FormatStyle Style = format::getLLVMStyle();
11997   Style.SortIncludes = true;
11998   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
11999   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
12000       << llvm::toString(FormattedReplaces.takeError()) << "\n";
12001   auto Result = applyAllReplacements(Code, *FormattedReplaces);
12002   EXPECT_TRUE(static_cast<bool>(Result));
12003   EXPECT_EQ(Expected, *Result);
12004 }
12005 
12006 TEST_F(FormatTest, FormatSortsUsingDeclarations) {
12007   EXPECT_EQ("using std::cin;\n"
12008             "using std::cout;",
12009             format("using std::cout;\n"
12010                    "using std::cin;", getGoogleStyle()));
12011 }
12012 
12013 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) {
12014   format::FormatStyle Style = format::getLLVMStyle();
12015   Style.Standard = FormatStyle::LS_Cpp03;
12016   // cpp03 recognize this string as identifier u8 and literal character 'a'
12017   EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style));
12018 }
12019 
12020 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) {
12021   // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers
12022   // all modes, including C++11, C++14 and C++17
12023   EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';"));
12024 }
12025 
12026 TEST_F(FormatTest, DoNotFormatLikelyXml) {
12027   EXPECT_EQ("<!-- ;> -->",
12028             format("<!-- ;> -->", getGoogleStyle()));
12029   EXPECT_EQ(" <!-- >; -->",
12030             format(" <!-- >; -->", getGoogleStyle()));
12031 }
12032 
12033 TEST_F(FormatTest, StructuredBindings) {
12034   // Structured bindings is a C++17 feature.
12035   // all modes, including C++11, C++14 and C++17
12036   verifyFormat("auto [a, b] = f();");
12037   EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();"));
12038   EXPECT_EQ("const auto [a, b] = f();", format("const   auto[a, b] = f();"));
12039   EXPECT_EQ("auto const [a, b] = f();", format("auto  const[a, b] = f();"));
12040   EXPECT_EQ("auto const volatile [a, b] = f();",
12041             format("auto  const   volatile[a, b] = f();"));
12042   EXPECT_EQ("auto [a, b, c] = f();", format("auto   [  a  ,  b,c   ] = f();"));
12043   EXPECT_EQ("auto &[a, b, c] = f();",
12044             format("auto   &[  a  ,  b,c   ] = f();"));
12045   EXPECT_EQ("auto &&[a, b, c] = f();",
12046             format("auto   &&[  a  ,  b,c   ] = f();"));
12047   EXPECT_EQ("auto const &[a, b] = f();", format("auto  const&[a, b] = f();"));
12048   EXPECT_EQ("auto const volatile &&[a, b] = f();",
12049             format("auto  const  volatile  &&[a, b] = f();"));
12050   EXPECT_EQ("auto const &&[a, b] = f();", format("auto  const   &&  [a, b] = f();"));
12051   EXPECT_EQ("const auto &[a, b] = f();", format("const  auto  &  [a, b] = f();"));
12052   EXPECT_EQ("const auto volatile &&[a, b] = f();",
12053             format("const  auto   volatile  &&[a, b] = f();"));
12054   EXPECT_EQ("volatile const auto &&[a, b] = f();",
12055             format("volatile  const  auto   &&[a, b] = f();"));
12056   EXPECT_EQ("const auto &&[a, b] = f();", format("const  auto  &&  [a, b] = f();"));
12057 
12058   // Make sure we don't mistake structured bindings for lambdas.
12059   FormatStyle PointerMiddle = getLLVMStyle();
12060   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
12061   verifyFormat("auto [a1, b]{A * i};", getGoogleStyle());
12062   verifyFormat("auto [a2, b]{A * i};", getLLVMStyle());
12063   verifyFormat("auto [a3, b]{A * i};", PointerMiddle);
12064   verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle());
12065   verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle());
12066   verifyFormat("auto const [a3, b]{A * i};", PointerMiddle);
12067   verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle());
12068   verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle());
12069   verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle);
12070   verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle());
12071   verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle());
12072   verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle);
12073 
12074   EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}",
12075             format("for (const auto   &&   [a, b] : some_range) {\n}"));
12076   EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}",
12077             format("for (const auto   &   [a, b] : some_range) {\n}"));
12078   EXPECT_EQ("for (const auto [a, b] : some_range) {\n}",
12079             format("for (const auto[a, b] : some_range) {\n}"));
12080   EXPECT_EQ("auto [x, y](expr);", format("auto[x,y]  (expr);"));
12081   EXPECT_EQ("auto &[x, y](expr);", format("auto  &  [x,y]  (expr);"));
12082   EXPECT_EQ("auto &&[x, y](expr);", format("auto  &&  [x,y]  (expr);"));
12083   EXPECT_EQ("auto const &[x, y](expr);", format("auto  const  &  [x,y]  (expr);"));
12084   EXPECT_EQ("auto const &&[x, y](expr);", format("auto  const  &&  [x,y]  (expr);"));
12085   EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y]     {expr};"));
12086   EXPECT_EQ("auto const &[x, y]{expr};", format("auto  const  &  [x,y]  {expr};"));
12087   EXPECT_EQ("auto const &&[x, y]{expr};", format("auto  const  &&  [x,y]  {expr};"));
12088 
12089   format::FormatStyle Spaces = format::getLLVMStyle();
12090   Spaces.SpacesInSquareBrackets = true;
12091   verifyFormat("auto [ a, b ] = f();", Spaces);
12092   verifyFormat("auto &&[ a, b ] = f();", Spaces);
12093   verifyFormat("auto &[ a, b ] = f();", Spaces);
12094   verifyFormat("auto const &&[ a, b ] = f();", Spaces);
12095   verifyFormat("auto const &[ a, b ] = f();", Spaces);
12096 }
12097 
12098 TEST_F(FormatTest, FileAndCode) {
12099   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", ""));
12100   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", ""));
12101   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", ""));
12102   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", ""));
12103   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@interface Foo\n@end\n"));
12104   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", ""));
12105   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo", "@interface Foo\n@end\n"));
12106   EXPECT_EQ(FormatStyle::LK_ObjC,
12107             guessLanguage("foo.h", "int DoStuff(CGRect rect);\n"));
12108   EXPECT_EQ(
12109       FormatStyle::LK_ObjC,
12110       guessLanguage("foo.h",
12111                     "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));\n"));
12112   EXPECT_EQ(
12113       FormatStyle::LK_Cpp,
12114       guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;"));
12115 }
12116 
12117 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) {
12118   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];"));
12119   EXPECT_EQ(FormatStyle::LK_ObjC,
12120             guessLanguage("foo.h", "array[[calculator getIndex]];"));
12121   EXPECT_EQ(FormatStyle::LK_Cpp,
12122             guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];"));
12123   EXPECT_EQ(
12124       FormatStyle::LK_Cpp,
12125       guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];"));
12126   EXPECT_EQ(FormatStyle::LK_ObjC,
12127             guessLanguage("foo.h", "[[noreturn foo] bar];"));
12128   EXPECT_EQ(FormatStyle::LK_Cpp,
12129             guessLanguage("foo.h", "[[clang::fallthrough]];"));
12130   EXPECT_EQ(FormatStyle::LK_ObjC,
12131             guessLanguage("foo.h", "[[clang:fallthrough] foo];"));
12132   EXPECT_EQ(FormatStyle::LK_Cpp,
12133             guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];"));
12134   EXPECT_EQ(FormatStyle::LK_Cpp,
12135             guessLanguage("foo.h", "[[using clang: fallthrough]];"));
12136   EXPECT_EQ(FormatStyle::LK_ObjC,
12137             guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];"));
12138   EXPECT_EQ(FormatStyle::LK_Cpp,
12139             guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];"));
12140   EXPECT_EQ(
12141       FormatStyle::LK_Cpp,
12142       guessLanguage("foo.h",
12143                     "[[clang::callable_when(\"unconsumed\", \"unknown\")]]"));
12144   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]"));
12145 }
12146 
12147 TEST_F(FormatTest, GuessLanguageWithCaret) {
12148   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);"));
12149   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);"));
12150   EXPECT_EQ(FormatStyle::LK_ObjC,
12151             guessLanguage("foo.h", "int(^)(char, float);"));
12152   EXPECT_EQ(FormatStyle::LK_ObjC,
12153             guessLanguage("foo.h", "int(^foo)(char, float);"));
12154   EXPECT_EQ(FormatStyle::LK_ObjC,
12155             guessLanguage("foo.h", "int(^foo[10])(char, float);"));
12156   EXPECT_EQ(FormatStyle::LK_ObjC,
12157             guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);"));
12158   EXPECT_EQ(
12159       FormatStyle::LK_ObjC,
12160       guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);"));
12161 }
12162 
12163 TEST_F(FormatTest, GuessLanguageWithChildLines) {
12164   EXPECT_EQ(FormatStyle::LK_Cpp,
12165             guessLanguage("foo.h", "#define FOO ({ std::string s; })"));
12166   EXPECT_EQ(FormatStyle::LK_ObjC,
12167             guessLanguage("foo.h", "#define FOO ({ NSString *s; })"));
12168   EXPECT_EQ(
12169       FormatStyle::LK_Cpp,
12170       guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })"));
12171   EXPECT_EQ(
12172       FormatStyle::LK_ObjC,
12173       guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })"));
12174 }
12175 
12176 } // end namespace
12177 } // end namespace format
12178 } // end namespace clang
12179