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     LLVM_DEBUG(llvm::errs() << "---\n");
43     LLVM_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     LLVM_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   // Don't remove empty lines before namespace endings.
282   FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
283   LLVMWithNoNamespaceFix.FixNamespaceComments = false;
284   EXPECT_EQ("namespace {\n"
285             "int i;\n"
286             "\n"
287             "}",
288             format("namespace {\n"
289                    "int i;\n"
290                    "\n"
291                    "}", LLVMWithNoNamespaceFix));
292   EXPECT_EQ("namespace {\n"
293             "int i;\n"
294             "}",
295             format("namespace {\n"
296                    "int i;\n"
297                    "}", LLVMWithNoNamespaceFix));
298   EXPECT_EQ("namespace {\n"
299             "int i;\n"
300             "\n"
301             "};",
302             format("namespace {\n"
303                    "int i;\n"
304                    "\n"
305                    "};", LLVMWithNoNamespaceFix));
306   EXPECT_EQ("namespace {\n"
307             "int i;\n"
308             "};",
309             format("namespace {\n"
310                    "int i;\n"
311                    "};", LLVMWithNoNamespaceFix));
312   EXPECT_EQ("namespace {\n"
313             "int i;\n"
314             "\n"
315             "}",
316             format("namespace {\n"
317                    "int i;\n"
318                    "\n"
319                    "}"));
320   EXPECT_EQ("namespace {\n"
321             "int i;\n"
322             "\n"
323             "} // namespace",
324             format("namespace {\n"
325                    "int i;\n"
326                    "\n"
327                    "}  // namespace"));
328 
329   FormatStyle Style = getLLVMStyle();
330   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
331   Style.MaxEmptyLinesToKeep = 2;
332   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
333   Style.BraceWrapping.AfterClass = true;
334   Style.BraceWrapping.AfterFunction = true;
335   Style.KeepEmptyLinesAtTheStartOfBlocks = false;
336 
337   EXPECT_EQ("class Foo\n"
338             "{\n"
339             "  Foo() {}\n"
340             "\n"
341             "  void funk() {}\n"
342             "};",
343             format("class Foo\n"
344                    "{\n"
345                    "  Foo()\n"
346                    "  {\n"
347                    "  }\n"
348                    "\n"
349                    "  void funk() {}\n"
350                    "};",
351                    Style));
352 }
353 
354 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) {
355   verifyFormat("x = (a) and (b);");
356   verifyFormat("x = (a) or (b);");
357   verifyFormat("x = (a) bitand (b);");
358   verifyFormat("x = (a) bitor (b);");
359   verifyFormat("x = (a) not_eq (b);");
360   verifyFormat("x = (a) and_eq (b);");
361   verifyFormat("x = (a) or_eq (b);");
362   verifyFormat("x = (a) xor (b);");
363 }
364 
365 TEST_F(FormatTest, RecognizesUnaryOperatorKeywords) {
366   verifyFormat("x = compl(a);");
367   verifyFormat("x = not(a);");
368   verifyFormat("x = bitand(a);");
369   // Unary operator must not be merged with the next identifier
370   verifyFormat("x = compl a;");
371   verifyFormat("x = not a;");
372   verifyFormat("x = bitand a;");
373 }
374 
375 //===----------------------------------------------------------------------===//
376 // Tests for control statements.
377 //===----------------------------------------------------------------------===//
378 
379 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
380   verifyFormat("if (true)\n  f();\ng();");
381   verifyFormat("if (a)\n  if (b)\n    if (c)\n      g();\nh();");
382   verifyFormat("if (a)\n  if (b) {\n    f();\n  }\ng();");
383   verifyFormat("if constexpr (true)\n"
384                "  f();\ng();");
385   verifyFormat("if constexpr (a)\n"
386                "  if constexpr (b)\n"
387                "    if constexpr (c)\n"
388                "      g();\n"
389                "h();");
390   verifyFormat("if constexpr (a)\n"
391                "  if constexpr (b) {\n"
392                "    f();\n"
393                "  }\n"
394                "g();");
395 
396   FormatStyle AllowsMergedIf = getLLVMStyle();
397   AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left;
398   AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
399   verifyFormat("if (a)\n"
400                "  // comment\n"
401                "  f();",
402                AllowsMergedIf);
403   verifyFormat("{\n"
404                "  if (a)\n"
405                "  label:\n"
406                "    f();\n"
407                "}",
408                AllowsMergedIf);
409   verifyFormat("#define A \\\n"
410                "  if (a)  \\\n"
411                "  label:  \\\n"
412                "    f()",
413                AllowsMergedIf);
414   verifyFormat("if (a)\n"
415                "  ;",
416                AllowsMergedIf);
417   verifyFormat("if (a)\n"
418                "  if (b) return;",
419                AllowsMergedIf);
420 
421   verifyFormat("if (a) // Can't merge this\n"
422                "  f();\n",
423                AllowsMergedIf);
424   verifyFormat("if (a) /* still don't merge */\n"
425                "  f();",
426                AllowsMergedIf);
427   verifyFormat("if (a) { // Never merge this\n"
428                "  f();\n"
429                "}",
430                AllowsMergedIf);
431   verifyFormat("if (a) { /* Never merge this */\n"
432                "  f();\n"
433                "}",
434                AllowsMergedIf);
435 
436   AllowsMergedIf.ColumnLimit = 14;
437   verifyFormat("if (a) return;", AllowsMergedIf);
438   verifyFormat("if (aaaaaaaaa)\n"
439                "  return;",
440                AllowsMergedIf);
441 
442   AllowsMergedIf.ColumnLimit = 13;
443   verifyFormat("if (a)\n  return;", AllowsMergedIf);
444 }
445 
446 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
447   FormatStyle AllowsMergedLoops = getLLVMStyle();
448   AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
449   verifyFormat("while (true) continue;", AllowsMergedLoops);
450   verifyFormat("for (;;) continue;", AllowsMergedLoops);
451   verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
452   verifyFormat("while (true)\n"
453                "  ;",
454                AllowsMergedLoops);
455   verifyFormat("for (;;)\n"
456                "  ;",
457                AllowsMergedLoops);
458   verifyFormat("for (;;)\n"
459                "  for (;;) continue;",
460                AllowsMergedLoops);
461   verifyFormat("for (;;) // Can't merge this\n"
462                "  continue;",
463                AllowsMergedLoops);
464   verifyFormat("for (;;) /* still don't merge */\n"
465                "  continue;",
466                AllowsMergedLoops);
467 }
468 
469 TEST_F(FormatTest, FormatShortBracedStatements) {
470   FormatStyle AllowSimpleBracedStatements = getLLVMStyle();
471   AllowSimpleBracedStatements.ColumnLimit = 40;
472   AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true;
473 
474   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true;
475   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
476 
477   AllowSimpleBracedStatements.BreakBeforeBraces = FormatStyle::BS_Custom;
478   AllowSimpleBracedStatements.BraceWrapping.AfterFunction = true;
479   AllowSimpleBracedStatements.BraceWrapping.SplitEmptyRecord = false;
480 
481   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
482   verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
483   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
484   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
485   verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
486   verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements);
487   verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
488   verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
489   verifyFormat("if (true) {\n"
490                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
491                "}",
492                AllowSimpleBracedStatements);
493   verifyFormat("if (true) { //\n"
494                "  f();\n"
495                "}",
496                AllowSimpleBracedStatements);
497   verifyFormat("if (true) {\n"
498                "  f();\n"
499                "  f();\n"
500                "}",
501                AllowSimpleBracedStatements);
502   verifyFormat("if (true) {\n"
503                "  f();\n"
504                "} else {\n"
505                "  f();\n"
506                "}",
507                AllowSimpleBracedStatements);
508 
509   verifyFormat("struct A2 {\n"
510                "  int X;\n"
511                "};",
512                AllowSimpleBracedStatements);
513   verifyFormat("typedef struct A2 {\n"
514                "  int X;\n"
515                "} A2_t;",
516                AllowSimpleBracedStatements);
517   verifyFormat("template <int> struct A2 {\n"
518                "  struct B {};\n"
519                "};",
520                AllowSimpleBracedStatements);
521 
522   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false;
523   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
524   verifyFormat("if (true) {\n"
525                "  f();\n"
526                "}",
527                AllowSimpleBracedStatements);
528   verifyFormat("if (true) {\n"
529                "  f();\n"
530                "} else {\n"
531                "  f();\n"
532                "}",
533                AllowSimpleBracedStatements);
534 
535   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
536   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
537   verifyFormat("while (true) {\n"
538                "  f();\n"
539                "}",
540                AllowSimpleBracedStatements);
541   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
542   verifyFormat("for (;;) {\n"
543                "  f();\n"
544                "}",
545                AllowSimpleBracedStatements);
546 
547   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true;
548   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
549   AllowSimpleBracedStatements.BraceWrapping.AfterControlStatement = true;
550 
551   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
552   verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
553   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
554   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
555   verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
556   verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements);
557   verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
558   verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
559   verifyFormat("if (true)\n"
560                "{\n"
561                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
562                "}",
563                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                "  f();\n"
573                "}",
574                AllowSimpleBracedStatements);
575   verifyFormat("if (true)\n"
576                "{\n"
577                "  f();\n"
578                "} else\n"
579                "{\n"
580                "  f();\n"
581                "}",
582                AllowSimpleBracedStatements);
583 
584   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false;
585   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
586   verifyFormat("if (true)\n"
587                "{\n"
588                "  f();\n"
589                "}",
590                AllowSimpleBracedStatements);
591   verifyFormat("if (true)\n"
592                "{\n"
593                "  f();\n"
594                "} else\n"
595                "{\n"
596                "  f();\n"
597                "}",
598                AllowSimpleBracedStatements);
599 
600   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
601   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
602   verifyFormat("while (true)\n"
603                "{\n"
604                "  f();\n"
605                "}",
606                AllowSimpleBracedStatements);
607   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
608   verifyFormat("for (;;)\n"
609                "{\n"
610                "  f();\n"
611                "}",
612                AllowSimpleBracedStatements);
613 }
614 
615 TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) {
616   FormatStyle Style = getLLVMStyleWithColumns(60);
617   Style.AllowShortBlocksOnASingleLine = true;
618   Style.AllowShortIfStatementsOnASingleLine = true;
619   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
620   EXPECT_EQ("#define A                                                  \\\n"
621             "  if (HANDLEwernufrnuLwrmviferuvnierv)                     \\\n"
622             "  { RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; }\n"
623             "X;",
624             format("#define A \\\n"
625                    "   if (HANDLEwernufrnuLwrmviferuvnierv) { \\\n"
626                    "      RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n"
627                    "   }\n"
628                    "X;",
629                    Style));
630 }
631 
632 TEST_F(FormatTest, ParseIfElse) {
633   verifyFormat("if (true)\n"
634                "  if (true)\n"
635                "    if (true)\n"
636                "      f();\n"
637                "    else\n"
638                "      g();\n"
639                "  else\n"
640                "    h();\n"
641                "else\n"
642                "  i();");
643   verifyFormat("if (true)\n"
644                "  if (true)\n"
645                "    if (true) {\n"
646                "      if (true)\n"
647                "        f();\n"
648                "    } else {\n"
649                "      g();\n"
650                "    }\n"
651                "  else\n"
652                "    h();\n"
653                "else {\n"
654                "  i();\n"
655                "}");
656   verifyFormat("if (true)\n"
657                "  if constexpr (true)\n"
658                "    if (true) {\n"
659                "      if constexpr (true)\n"
660                "        f();\n"
661                "    } else {\n"
662                "      g();\n"
663                "    }\n"
664                "  else\n"
665                "    h();\n"
666                "else {\n"
667                "  i();\n"
668                "}");
669   verifyFormat("void f() {\n"
670                "  if (a) {\n"
671                "  } else {\n"
672                "  }\n"
673                "}");
674 }
675 
676 TEST_F(FormatTest, ElseIf) {
677   verifyFormat("if (a) {\n} else if (b) {\n}");
678   verifyFormat("if (a)\n"
679                "  f();\n"
680                "else if (b)\n"
681                "  g();\n"
682                "else\n"
683                "  h();");
684   verifyFormat("if constexpr (a)\n"
685                "  f();\n"
686                "else if constexpr (b)\n"
687                "  g();\n"
688                "else\n"
689                "  h();");
690   verifyFormat("if (a) {\n"
691                "  f();\n"
692                "}\n"
693                "// or else ..\n"
694                "else {\n"
695                "  g()\n"
696                "}");
697 
698   verifyFormat("if (a) {\n"
699                "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
700                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
701                "}");
702   verifyFormat("if (a) {\n"
703                "} else if (\n"
704                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
705                "}",
706                getLLVMStyleWithColumns(62));
707   verifyFormat("if (a) {\n"
708                "} else if constexpr (\n"
709                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
710                "}",
711                getLLVMStyleWithColumns(62));
712 }
713 
714 TEST_F(FormatTest, FormatsForLoop) {
715   verifyFormat(
716       "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
717       "     ++VeryVeryLongLoopVariable)\n"
718       "  ;");
719   verifyFormat("for (;;)\n"
720                "  f();");
721   verifyFormat("for (;;) {\n}");
722   verifyFormat("for (;;) {\n"
723                "  f();\n"
724                "}");
725   verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
726 
727   verifyFormat(
728       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
729       "                                          E = UnwrappedLines.end();\n"
730       "     I != E; ++I) {\n}");
731 
732   verifyFormat(
733       "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
734       "     ++IIIII) {\n}");
735   verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
736                "         aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
737                "     aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
738   verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
739                "         I = FD->getDeclsInPrototypeScope().begin(),\n"
740                "         E = FD->getDeclsInPrototypeScope().end();\n"
741                "     I != E; ++I) {\n}");
742   verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n"
743                "         I = Container.begin(),\n"
744                "         E = Container.end();\n"
745                "     I != E; ++I) {\n}",
746                getLLVMStyleWithColumns(76));
747 
748   verifyFormat(
749       "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
750       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
751       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
752       "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
753       "     ++aaaaaaaaaaa) {\n}");
754   verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
755                "                bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n"
756                "     ++i) {\n}");
757   verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
758                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
759                "}");
760   verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
761                "         aaaaaaaaaa);\n"
762                "     iter; ++iter) {\n"
763                "}");
764   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
765                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
766                "     aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n"
767                "     ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {");
768 
769   // These should not be formatted as Objective-C for-in loops.
770   verifyFormat("for (Foo *x = 0; x != in; x++) {\n}");
771   verifyFormat("Foo *x;\nfor (x = 0; x != in; x++) {\n}");
772   verifyFormat("Foo *x;\nfor (x in y) {\n}");
773   verifyFormat("for (const Foo<Bar> &baz = in.value(); !baz.at_end(); ++baz) {\n}");
774 
775   FormatStyle NoBinPacking = getLLVMStyle();
776   NoBinPacking.BinPackParameters = false;
777   verifyFormat("for (int aaaaaaaaaaa = 1;\n"
778                "     aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
779                "                                           aaaaaaaaaaaaaaaa,\n"
780                "                                           aaaaaaaaaaaaaaaa,\n"
781                "                                           aaaaaaaaaaaaaaaa);\n"
782                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
783                "}",
784                NoBinPacking);
785   verifyFormat(
786       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
787       "                                          E = UnwrappedLines.end();\n"
788       "     I != E;\n"
789       "     ++I) {\n}",
790       NoBinPacking);
791 
792   FormatStyle AlignLeft = getLLVMStyle();
793   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
794   verifyFormat("for (A* a = start; a < end; ++a, ++value) {\n}", AlignLeft);
795 }
796 
797 TEST_F(FormatTest, RangeBasedForLoops) {
798   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
799                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
800   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
801                "     aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
802   verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
803                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
804   verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
805                "     aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
806 }
807 
808 TEST_F(FormatTest, ForEachLoops) {
809   verifyFormat("void f() {\n"
810                "  foreach (Item *item, itemlist) {}\n"
811                "  Q_FOREACH (Item *item, itemlist) {}\n"
812                "  BOOST_FOREACH (Item *item, itemlist) {}\n"
813                "  UNKNOWN_FORACH(Item * item, itemlist) {}\n"
814                "}");
815 
816   // As function-like macros.
817   verifyFormat("#define foreach(x, y)\n"
818                "#define Q_FOREACH(x, y)\n"
819                "#define BOOST_FOREACH(x, y)\n"
820                "#define UNKNOWN_FOREACH(x, y)\n");
821 
822   // Not as function-like macros.
823   verifyFormat("#define foreach (x, y)\n"
824                "#define Q_FOREACH (x, y)\n"
825                "#define BOOST_FOREACH (x, y)\n"
826                "#define UNKNOWN_FOREACH (x, y)\n");
827 }
828 
829 TEST_F(FormatTest, FormatsWhileLoop) {
830   verifyFormat("while (true) {\n}");
831   verifyFormat("while (true)\n"
832                "  f();");
833   verifyFormat("while () {\n}");
834   verifyFormat("while () {\n"
835                "  f();\n"
836                "}");
837 }
838 
839 TEST_F(FormatTest, FormatsDoWhile) {
840   verifyFormat("do {\n"
841                "  do_something();\n"
842                "} while (something());");
843   verifyFormat("do\n"
844                "  do_something();\n"
845                "while (something());");
846 }
847 
848 TEST_F(FormatTest, FormatsSwitchStatement) {
849   verifyFormat("switch (x) {\n"
850                "case 1:\n"
851                "  f();\n"
852                "  break;\n"
853                "case kFoo:\n"
854                "case ns::kBar:\n"
855                "case kBaz:\n"
856                "  break;\n"
857                "default:\n"
858                "  g();\n"
859                "  break;\n"
860                "}");
861   verifyFormat("switch (x) {\n"
862                "case 1: {\n"
863                "  f();\n"
864                "  break;\n"
865                "}\n"
866                "case 2: {\n"
867                "  break;\n"
868                "}\n"
869                "}");
870   verifyFormat("switch (x) {\n"
871                "case 1: {\n"
872                "  f();\n"
873                "  {\n"
874                "    g();\n"
875                "    h();\n"
876                "  }\n"
877                "  break;\n"
878                "}\n"
879                "}");
880   verifyFormat("switch (x) {\n"
881                "case 1: {\n"
882                "  f();\n"
883                "  if (foo) {\n"
884                "    g();\n"
885                "    h();\n"
886                "  }\n"
887                "  break;\n"
888                "}\n"
889                "}");
890   verifyFormat("switch (x) {\n"
891                "case 1: {\n"
892                "  f();\n"
893                "  g();\n"
894                "} break;\n"
895                "}");
896   verifyFormat("switch (test)\n"
897                "  ;");
898   verifyFormat("switch (x) {\n"
899                "default: {\n"
900                "  // Do nothing.\n"
901                "}\n"
902                "}");
903   verifyFormat("switch (x) {\n"
904                "// comment\n"
905                "// if 1, do f()\n"
906                "case 1:\n"
907                "  f();\n"
908                "}");
909   verifyFormat("switch (x) {\n"
910                "case 1:\n"
911                "  // Do amazing stuff\n"
912                "  {\n"
913                "    f();\n"
914                "    g();\n"
915                "  }\n"
916                "  break;\n"
917                "}");
918   verifyFormat("#define A          \\\n"
919                "  switch (x) {     \\\n"
920                "  case a:          \\\n"
921                "    foo = b;       \\\n"
922                "  }",
923                getLLVMStyleWithColumns(20));
924   verifyFormat("#define OPERATION_CASE(name)           \\\n"
925                "  case OP_name:                        \\\n"
926                "    return operations::Operation##name\n",
927                getLLVMStyleWithColumns(40));
928   verifyFormat("switch (x) {\n"
929                "case 1:;\n"
930                "default:;\n"
931                "  int i;\n"
932                "}");
933 
934   verifyGoogleFormat("switch (x) {\n"
935                      "  case 1:\n"
936                      "    f();\n"
937                      "    break;\n"
938                      "  case kFoo:\n"
939                      "  case ns::kBar:\n"
940                      "  case kBaz:\n"
941                      "    break;\n"
942                      "  default:\n"
943                      "    g();\n"
944                      "    break;\n"
945                      "}");
946   verifyGoogleFormat("switch (x) {\n"
947                      "  case 1: {\n"
948                      "    f();\n"
949                      "    break;\n"
950                      "  }\n"
951                      "}");
952   verifyGoogleFormat("switch (test)\n"
953                      "  ;");
954 
955   verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
956                      "  case OP_name:              \\\n"
957                      "    return operations::Operation##name\n");
958   verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
959                      "  // Get the correction operation class.\n"
960                      "  switch (OpCode) {\n"
961                      "    CASE(Add);\n"
962                      "    CASE(Subtract);\n"
963                      "    default:\n"
964                      "      return operations::Unknown;\n"
965                      "  }\n"
966                      "#undef OPERATION_CASE\n"
967                      "}");
968   verifyFormat("DEBUG({\n"
969                "  switch (x) {\n"
970                "  case A:\n"
971                "    f();\n"
972                "    break;\n"
973                "    // fallthrough\n"
974                "  case B:\n"
975                "    g();\n"
976                "    break;\n"
977                "  }\n"
978                "});");
979   EXPECT_EQ("DEBUG({\n"
980             "  switch (x) {\n"
981             "  case A:\n"
982             "    f();\n"
983             "    break;\n"
984             "  // On B:\n"
985             "  case B:\n"
986             "    g();\n"
987             "    break;\n"
988             "  }\n"
989             "});",
990             format("DEBUG({\n"
991                    "  switch (x) {\n"
992                    "  case A:\n"
993                    "    f();\n"
994                    "    break;\n"
995                    "  // On B:\n"
996                    "  case B:\n"
997                    "    g();\n"
998                    "    break;\n"
999                    "  }\n"
1000                    "});",
1001                    getLLVMStyle()));
1002   verifyFormat("switch (a) {\n"
1003                "case (b):\n"
1004                "  return;\n"
1005                "}");
1006 
1007   verifyFormat("switch (a) {\n"
1008                "case some_namespace::\n"
1009                "    some_constant:\n"
1010                "  return;\n"
1011                "}",
1012                getLLVMStyleWithColumns(34));
1013 }
1014 
1015 TEST_F(FormatTest, CaseRanges) {
1016   verifyFormat("switch (x) {\n"
1017                "case 'A' ... 'Z':\n"
1018                "case 1 ... 5:\n"
1019                "case a ... b:\n"
1020                "  break;\n"
1021                "}");
1022 }
1023 
1024 TEST_F(FormatTest, ShortCaseLabels) {
1025   FormatStyle Style = getLLVMStyle();
1026   Style.AllowShortCaseLabelsOnASingleLine = true;
1027   verifyFormat("switch (a) {\n"
1028                "case 1: x = 1; break;\n"
1029                "case 2: return;\n"
1030                "case 3:\n"
1031                "case 4:\n"
1032                "case 5: return;\n"
1033                "case 6: // comment\n"
1034                "  return;\n"
1035                "case 7:\n"
1036                "  // comment\n"
1037                "  return;\n"
1038                "case 8:\n"
1039                "  x = 8; // comment\n"
1040                "  break;\n"
1041                "default: y = 1; break;\n"
1042                "}",
1043                Style);
1044   verifyFormat("switch (a) {\n"
1045                "case 0: return; // comment\n"
1046                "case 1: break;  // comment\n"
1047                "case 2: return;\n"
1048                "// comment\n"
1049                "case 3: return;\n"
1050                "// comment 1\n"
1051                "// comment 2\n"
1052                "// comment 3\n"
1053                "case 4: break; /* comment */\n"
1054                "case 5:\n"
1055                "  // comment\n"
1056                "  break;\n"
1057                "case 6: /* comment */ x = 1; break;\n"
1058                "case 7: x = /* comment */ 1; break;\n"
1059                "case 8:\n"
1060                "  x = 1; /* comment */\n"
1061                "  break;\n"
1062                "case 9:\n"
1063                "  break; // comment line 1\n"
1064                "         // comment line 2\n"
1065                "}",
1066                Style);
1067   EXPECT_EQ("switch (a) {\n"
1068             "case 1:\n"
1069             "  x = 8;\n"
1070             "  // fall through\n"
1071             "case 2: x = 8;\n"
1072             "// comment\n"
1073             "case 3:\n"
1074             "  return; /* comment line 1\n"
1075             "           * comment line 2 */\n"
1076             "case 4: i = 8;\n"
1077             "// something else\n"
1078             "#if FOO\n"
1079             "case 5: break;\n"
1080             "#endif\n"
1081             "}",
1082             format("switch (a) {\n"
1083                    "case 1: x = 8;\n"
1084                    "  // fall through\n"
1085                    "case 2:\n"
1086                    "  x = 8;\n"
1087                    "// comment\n"
1088                    "case 3:\n"
1089                    "  return; /* comment line 1\n"
1090                    "           * comment line 2 */\n"
1091                    "case 4:\n"
1092                    "  i = 8;\n"
1093                    "// something else\n"
1094                    "#if FOO\n"
1095                    "case 5: break;\n"
1096                    "#endif\n"
1097                    "}",
1098                    Style));
1099   EXPECT_EQ("switch (a) {\n" "case 0:\n"
1100             "  return; // long long long long long long long long long long long long comment\n"
1101             "          // line\n" "}",
1102             format("switch (a) {\n"
1103                    "case 0: return; // long long long long long long long long long long long long comment line\n"
1104                    "}",
1105                    Style));
1106   EXPECT_EQ("switch (a) {\n"
1107             "case 0:\n"
1108             "  return; /* long long long long long long long long long long long long comment\n"
1109             "             line */\n"
1110             "}",
1111             format("switch (a) {\n"
1112                    "case 0: return; /* long long long long long long long long long long long long comment line */\n"
1113                    "}",
1114                    Style));
1115   verifyFormat("switch (a) {\n"
1116                "#if FOO\n"
1117                "case 0: return 0;\n"
1118                "#endif\n"
1119                "}",
1120                Style);
1121   verifyFormat("switch (a) {\n"
1122                "case 1: {\n"
1123                "}\n"
1124                "case 2: {\n"
1125                "  return;\n"
1126                "}\n"
1127                "case 3: {\n"
1128                "  x = 1;\n"
1129                "  return;\n"
1130                "}\n"
1131                "case 4:\n"
1132                "  if (x)\n"
1133                "    return;\n"
1134                "}",
1135                Style);
1136   Style.ColumnLimit = 21;
1137   verifyFormat("switch (a) {\n"
1138                "case 1: x = 1; break;\n"
1139                "case 2: return;\n"
1140                "case 3:\n"
1141                "case 4:\n"
1142                "case 5: return;\n"
1143                "default:\n"
1144                "  y = 1;\n"
1145                "  break;\n"
1146                "}",
1147                Style);
1148   Style.ColumnLimit = 80;
1149   Style.AllowShortCaseLabelsOnASingleLine = false;
1150   Style.IndentCaseLabels = true;
1151   EXPECT_EQ("switch (n) {\n"
1152             "  default /*comments*/:\n"
1153             "    return true;\n"
1154             "  case 0:\n"
1155             "    return false;\n"
1156             "}",
1157             format("switch (n) {\n"
1158                    "default/*comments*/:\n"
1159                    "  return true;\n"
1160                    "case 0:\n"
1161                    "  return false;\n"
1162                    "}",
1163                    Style));
1164 }
1165 
1166 TEST_F(FormatTest, FormatsLabels) {
1167   verifyFormat("void f() {\n"
1168                "  some_code();\n"
1169                "test_label:\n"
1170                "  some_other_code();\n"
1171                "  {\n"
1172                "    some_more_code();\n"
1173                "  another_label:\n"
1174                "    some_more_code();\n"
1175                "  }\n"
1176                "}");
1177   verifyFormat("{\n"
1178                "  some_code();\n"
1179                "test_label:\n"
1180                "  some_other_code();\n"
1181                "}");
1182   verifyFormat("{\n"
1183                "  some_code();\n"
1184                "test_label:;\n"
1185                "  int i = 0;\n"
1186                "}");
1187 }
1188 
1189 //===----------------------------------------------------------------------===//
1190 // Tests for classes, namespaces, etc.
1191 //===----------------------------------------------------------------------===//
1192 
1193 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
1194   verifyFormat("class A {};");
1195 }
1196 
1197 TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
1198   verifyFormat("class A {\n"
1199                "public:\n"
1200                "public: // comment\n"
1201                "protected:\n"
1202                "private:\n"
1203                "  void f() {}\n"
1204                "};");
1205   verifyGoogleFormat("class A {\n"
1206                      " public:\n"
1207                      " protected:\n"
1208                      " private:\n"
1209                      "  void f() {}\n"
1210                      "};");
1211   verifyFormat("class A {\n"
1212                "public slots:\n"
1213                "  void f1() {}\n"
1214                "public Q_SLOTS:\n"
1215                "  void f2() {}\n"
1216                "protected slots:\n"
1217                "  void f3() {}\n"
1218                "protected Q_SLOTS:\n"
1219                "  void f4() {}\n"
1220                "private slots:\n"
1221                "  void f5() {}\n"
1222                "private Q_SLOTS:\n"
1223                "  void f6() {}\n"
1224                "signals:\n"
1225                "  void g1();\n"
1226                "Q_SIGNALS:\n"
1227                "  void g2();\n"
1228                "};");
1229 
1230   // Don't interpret 'signals' the wrong way.
1231   verifyFormat("signals.set();");
1232   verifyFormat("for (Signals signals : f()) {\n}");
1233   verifyFormat("{\n"
1234                "  signals.set(); // This needs indentation.\n"
1235                "}");
1236   verifyFormat("void f() {\n"
1237                "label:\n"
1238                "  signals.baz();\n"
1239                "}");
1240 }
1241 
1242 TEST_F(FormatTest, SeparatesLogicalBlocks) {
1243   EXPECT_EQ("class A {\n"
1244             "public:\n"
1245             "  void f();\n"
1246             "\n"
1247             "private:\n"
1248             "  void g() {}\n"
1249             "  // test\n"
1250             "protected:\n"
1251             "  int h;\n"
1252             "};",
1253             format("class A {\n"
1254                    "public:\n"
1255                    "void f();\n"
1256                    "private:\n"
1257                    "void g() {}\n"
1258                    "// test\n"
1259                    "protected:\n"
1260                    "int h;\n"
1261                    "};"));
1262   EXPECT_EQ("class A {\n"
1263             "protected:\n"
1264             "public:\n"
1265             "  void f();\n"
1266             "};",
1267             format("class A {\n"
1268                    "protected:\n"
1269                    "\n"
1270                    "public:\n"
1271                    "\n"
1272                    "  void f();\n"
1273                    "};"));
1274 
1275   // Even ensure proper spacing inside macros.
1276   EXPECT_EQ("#define B     \\\n"
1277             "  class A {   \\\n"
1278             "   protected: \\\n"
1279             "   public:    \\\n"
1280             "    void f(); \\\n"
1281             "  };",
1282             format("#define B     \\\n"
1283                    "  class A {   \\\n"
1284                    "   protected: \\\n"
1285                    "              \\\n"
1286                    "   public:    \\\n"
1287                    "              \\\n"
1288                    "    void f(); \\\n"
1289                    "  };",
1290                    getGoogleStyle()));
1291   // But don't remove empty lines after macros ending in access specifiers.
1292   EXPECT_EQ("#define A private:\n"
1293             "\n"
1294             "int i;",
1295             format("#define A         private:\n"
1296                    "\n"
1297                    "int              i;"));
1298 }
1299 
1300 TEST_F(FormatTest, FormatsClasses) {
1301   verifyFormat("class A : public B {};");
1302   verifyFormat("class A : public ::B {};");
1303 
1304   verifyFormat(
1305       "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1306       "                             public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
1307   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
1308                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1309                "      public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
1310   verifyFormat(
1311       "class A : public B, public C, public D, public E, public F {};");
1312   verifyFormat("class AAAAAAAAAAAA : public B,\n"
1313                "                     public C,\n"
1314                "                     public D,\n"
1315                "                     public E,\n"
1316                "                     public F,\n"
1317                "                     public G {};");
1318 
1319   verifyFormat("class\n"
1320                "    ReallyReallyLongClassName {\n"
1321                "  int i;\n"
1322                "};",
1323                getLLVMStyleWithColumns(32));
1324   verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
1325                "                           aaaaaaaaaaaaaaaa> {};");
1326   verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
1327                "    : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
1328                "                                 aaaaaaaaaaaaaaaaaaaaaa> {};");
1329   verifyFormat("template <class R, class C>\n"
1330                "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
1331                "    : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
1332   verifyFormat("class ::A::B {};");
1333 }
1334 
1335 TEST_F(FormatTest, BreakInheritanceStyle) {
1336   FormatStyle StyleWithInheritanceBreakBeforeComma = getLLVMStyle();
1337   StyleWithInheritanceBreakBeforeComma.BreakInheritanceList =
1338           FormatStyle::BILS_BeforeComma;
1339   verifyFormat("class MyClass : public X {};",
1340                StyleWithInheritanceBreakBeforeComma);
1341   verifyFormat("class MyClass\n"
1342                "    : public X\n"
1343                "    , public Y {};",
1344                StyleWithInheritanceBreakBeforeComma);
1345   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA\n"
1346                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n"
1347                "    , public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
1348                StyleWithInheritanceBreakBeforeComma);
1349   verifyFormat("struct aaaaaaaaaaaaa\n"
1350                "    : public aaaaaaaaaaaaaaaaaaa< // break\n"
1351                "          aaaaaaaaaaaaaaaa> {};",
1352                StyleWithInheritanceBreakBeforeComma);
1353 
1354   FormatStyle StyleWithInheritanceBreakAfterColon = getLLVMStyle();
1355   StyleWithInheritanceBreakAfterColon.BreakInheritanceList =
1356           FormatStyle::BILS_AfterColon;
1357   verifyFormat("class MyClass : public X {};",
1358                StyleWithInheritanceBreakAfterColon);
1359   verifyFormat("class MyClass : public X, public Y {};",
1360                StyleWithInheritanceBreakAfterColon);
1361   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA :\n"
1362                "    public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1363                "    public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
1364                StyleWithInheritanceBreakAfterColon);
1365   verifyFormat("struct aaaaaaaaaaaaa :\n"
1366                "    public aaaaaaaaaaaaaaaaaaa< // break\n"
1367                "        aaaaaaaaaaaaaaaa> {};",
1368                StyleWithInheritanceBreakAfterColon);
1369 }
1370 
1371 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
1372   verifyFormat("class A {\n} a, b;");
1373   verifyFormat("struct A {\n} a, b;");
1374   verifyFormat("union A {\n} a;");
1375 }
1376 
1377 TEST_F(FormatTest, FormatsEnum) {
1378   verifyFormat("enum {\n"
1379                "  Zero,\n"
1380                "  One = 1,\n"
1381                "  Two = One + 1,\n"
1382                "  Three = (One + Two),\n"
1383                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1384                "  Five = (One, Two, Three, Four, 5)\n"
1385                "};");
1386   verifyGoogleFormat("enum {\n"
1387                      "  Zero,\n"
1388                      "  One = 1,\n"
1389                      "  Two = One + 1,\n"
1390                      "  Three = (One + Two),\n"
1391                      "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1392                      "  Five = (One, Two, Three, Four, 5)\n"
1393                      "};");
1394   verifyFormat("enum Enum {};");
1395   verifyFormat("enum {};");
1396   verifyFormat("enum X E {} d;");
1397   verifyFormat("enum __attribute__((...)) E {} d;");
1398   verifyFormat("enum __declspec__((...)) E {} d;");
1399   verifyFormat("enum {\n"
1400                "  Bar = Foo<int, int>::value\n"
1401                "};",
1402                getLLVMStyleWithColumns(30));
1403 
1404   verifyFormat("enum ShortEnum { A, B, C };");
1405   verifyGoogleFormat("enum ShortEnum { A, B, C };");
1406 
1407   EXPECT_EQ("enum KeepEmptyLines {\n"
1408             "  ONE,\n"
1409             "\n"
1410             "  TWO,\n"
1411             "\n"
1412             "  THREE\n"
1413             "}",
1414             format("enum KeepEmptyLines {\n"
1415                    "  ONE,\n"
1416                    "\n"
1417                    "  TWO,\n"
1418                    "\n"
1419                    "\n"
1420                    "  THREE\n"
1421                    "}"));
1422   verifyFormat("enum E { // comment\n"
1423                "  ONE,\n"
1424                "  TWO\n"
1425                "};\n"
1426                "int i;");
1427   // Not enums.
1428   verifyFormat("enum X f() {\n"
1429                "  a();\n"
1430                "  return 42;\n"
1431                "}");
1432   verifyFormat("enum X Type::f() {\n"
1433                "  a();\n"
1434                "  return 42;\n"
1435                "}");
1436   verifyFormat("enum ::X f() {\n"
1437                "  a();\n"
1438                "  return 42;\n"
1439                "}");
1440   verifyFormat("enum ns::X f() {\n"
1441                "  a();\n"
1442                "  return 42;\n"
1443                "}");
1444 }
1445 
1446 TEST_F(FormatTest, FormatsEnumsWithErrors) {
1447   verifyFormat("enum Type {\n"
1448                "  One = 0; // These semicolons should be commas.\n"
1449                "  Two = 1;\n"
1450                "};");
1451   verifyFormat("namespace n {\n"
1452                "enum Type {\n"
1453                "  One,\n"
1454                "  Two, // missing };\n"
1455                "  int i;\n"
1456                "}\n"
1457                "void g() {}");
1458 }
1459 
1460 TEST_F(FormatTest, FormatsEnumStruct) {
1461   verifyFormat("enum struct {\n"
1462                "  Zero,\n"
1463                "  One = 1,\n"
1464                "  Two = One + 1,\n"
1465                "  Three = (One + Two),\n"
1466                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1467                "  Five = (One, Two, Three, Four, 5)\n"
1468                "};");
1469   verifyFormat("enum struct Enum {};");
1470   verifyFormat("enum struct {};");
1471   verifyFormat("enum struct X E {} d;");
1472   verifyFormat("enum struct __attribute__((...)) E {} d;");
1473   verifyFormat("enum struct __declspec__((...)) E {} d;");
1474   verifyFormat("enum struct X f() {\n  a();\n  return 42;\n}");
1475 }
1476 
1477 TEST_F(FormatTest, FormatsEnumClass) {
1478   verifyFormat("enum class {\n"
1479                "  Zero,\n"
1480                "  One = 1,\n"
1481                "  Two = One + 1,\n"
1482                "  Three = (One + Two),\n"
1483                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1484                "  Five = (One, Two, Three, Four, 5)\n"
1485                "};");
1486   verifyFormat("enum class Enum {};");
1487   verifyFormat("enum class {};");
1488   verifyFormat("enum class X E {} d;");
1489   verifyFormat("enum class __attribute__((...)) E {} d;");
1490   verifyFormat("enum class __declspec__((...)) E {} d;");
1491   verifyFormat("enum class X f() {\n  a();\n  return 42;\n}");
1492 }
1493 
1494 TEST_F(FormatTest, FormatsEnumTypes) {
1495   verifyFormat("enum X : int {\n"
1496                "  A, // Force multiple lines.\n"
1497                "  B\n"
1498                "};");
1499   verifyFormat("enum X : int { A, B };");
1500   verifyFormat("enum X : std::uint32_t { A, B };");
1501 }
1502 
1503 TEST_F(FormatTest, FormatsTypedefEnum) {
1504   FormatStyle Style = getLLVMStyle();
1505   Style.ColumnLimit = 40;
1506   verifyFormat("typedef enum {} EmptyEnum;");
1507   verifyFormat("typedef enum { A, B, C } ShortEnum;");
1508   verifyFormat("typedef enum {\n"
1509                "  ZERO = 0,\n"
1510                "  ONE = 1,\n"
1511                "  TWO = 2,\n"
1512                "  THREE = 3\n"
1513                "} LongEnum;",
1514                Style);
1515   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1516   Style.BraceWrapping.AfterEnum = true;
1517   verifyFormat("typedef enum {} EmptyEnum;");
1518   verifyFormat("typedef enum { A, B, C } ShortEnum;");
1519   verifyFormat("typedef enum\n"
1520                "{\n"
1521                "  ZERO = 0,\n"
1522                "  ONE = 1,\n"
1523                "  TWO = 2,\n"
1524                "  THREE = 3\n"
1525                "} LongEnum;",
1526                Style);
1527 }
1528 
1529 TEST_F(FormatTest, FormatsNSEnums) {
1530   verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
1531   verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
1532                      "  // Information about someDecentlyLongValue.\n"
1533                      "  someDecentlyLongValue,\n"
1534                      "  // Information about anotherDecentlyLongValue.\n"
1535                      "  anotherDecentlyLongValue,\n"
1536                      "  // Information about aThirdDecentlyLongValue.\n"
1537                      "  aThirdDecentlyLongValue\n"
1538                      "};");
1539   verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
1540                      "  a = 1,\n"
1541                      "  b = 2,\n"
1542                      "  c = 3,\n"
1543                      "};");
1544   verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
1545                      "  a = 1,\n"
1546                      "  b = 2,\n"
1547                      "  c = 3,\n"
1548                      "};");
1549   verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
1550                      "  a = 1,\n"
1551                      "  b = 2,\n"
1552                      "  c = 3,\n"
1553                      "};");
1554 }
1555 
1556 TEST_F(FormatTest, FormatsBitfields) {
1557   verifyFormat("struct Bitfields {\n"
1558                "  unsigned sClass : 8;\n"
1559                "  unsigned ValueKind : 2;\n"
1560                "};");
1561   verifyFormat("struct A {\n"
1562                "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
1563                "      bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
1564                "};");
1565   verifyFormat("struct MyStruct {\n"
1566                "  uchar data;\n"
1567                "  uchar : 8;\n"
1568                "  uchar : 8;\n"
1569                "  uchar other;\n"
1570                "};");
1571 }
1572 
1573 TEST_F(FormatTest, FormatsNamespaces) {
1574   FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
1575   LLVMWithNoNamespaceFix.FixNamespaceComments = false;
1576 
1577   verifyFormat("namespace some_namespace {\n"
1578                "class A {};\n"
1579                "void f() { f(); }\n"
1580                "}",
1581                LLVMWithNoNamespaceFix);
1582   verifyFormat("namespace {\n"
1583                "class A {};\n"
1584                "void f() { f(); }\n"
1585                "}",
1586                LLVMWithNoNamespaceFix);
1587   verifyFormat("inline namespace X {\n"
1588                "class A {};\n"
1589                "void f() { f(); }\n"
1590                "}",
1591                LLVMWithNoNamespaceFix);
1592   verifyFormat("using namespace some_namespace;\n"
1593                "class A {};\n"
1594                "void f() { f(); }",
1595                LLVMWithNoNamespaceFix);
1596 
1597   // This code is more common than we thought; if we
1598   // layout this correctly the semicolon will go into
1599   // its own line, which is undesirable.
1600   verifyFormat("namespace {};",
1601                LLVMWithNoNamespaceFix);
1602   verifyFormat("namespace {\n"
1603                "class A {};\n"
1604                "};",
1605                LLVMWithNoNamespaceFix);
1606 
1607   verifyFormat("namespace {\n"
1608                "int SomeVariable = 0; // comment\n"
1609                "} // namespace",
1610                LLVMWithNoNamespaceFix);
1611   EXPECT_EQ("#ifndef HEADER_GUARD\n"
1612             "#define HEADER_GUARD\n"
1613             "namespace my_namespace {\n"
1614             "int i;\n"
1615             "} // my_namespace\n"
1616             "#endif // HEADER_GUARD",
1617             format("#ifndef HEADER_GUARD\n"
1618                    " #define HEADER_GUARD\n"
1619                    "   namespace my_namespace {\n"
1620                    "int i;\n"
1621                    "}    // my_namespace\n"
1622                    "#endif    // HEADER_GUARD",
1623                    LLVMWithNoNamespaceFix));
1624 
1625   EXPECT_EQ("namespace A::B {\n"
1626             "class C {};\n"
1627             "}",
1628             format("namespace A::B {\n"
1629                    "class C {};\n"
1630                    "}",
1631                    LLVMWithNoNamespaceFix));
1632 
1633   FormatStyle Style = getLLVMStyle();
1634   Style.NamespaceIndentation = FormatStyle::NI_All;
1635   EXPECT_EQ("namespace out {\n"
1636             "  int i;\n"
1637             "  namespace in {\n"
1638             "    int i;\n"
1639             "  } // namespace in\n"
1640             "} // namespace out",
1641             format("namespace out {\n"
1642                    "int i;\n"
1643                    "namespace in {\n"
1644                    "int i;\n"
1645                    "} // namespace in\n"
1646                    "} // namespace out",
1647                    Style));
1648 
1649   Style.NamespaceIndentation = FormatStyle::NI_Inner;
1650   EXPECT_EQ("namespace out {\n"
1651             "int i;\n"
1652             "namespace in {\n"
1653             "  int i;\n"
1654             "} // namespace in\n"
1655             "} // namespace out",
1656             format("namespace out {\n"
1657                    "int i;\n"
1658                    "namespace in {\n"
1659                    "int i;\n"
1660                    "} // namespace in\n"
1661                    "} // namespace out",
1662                    Style));
1663 }
1664 
1665 TEST_F(FormatTest, FormatsCompactNamespaces) {
1666   FormatStyle Style = getLLVMStyle();
1667   Style.CompactNamespaces = true;
1668 
1669   verifyFormat("namespace A { namespace B {\n"
1670 			   "}} // namespace A::B",
1671 			   Style);
1672 
1673   EXPECT_EQ("namespace out { namespace in {\n"
1674             "}} // namespace out::in",
1675             format("namespace out {\n"
1676                    "namespace in {\n"
1677                    "} // namespace in\n"
1678                    "} // namespace out",
1679                    Style));
1680 
1681   // Only namespaces which have both consecutive opening and end get compacted
1682   EXPECT_EQ("namespace out {\n"
1683             "namespace in1 {\n"
1684             "} // namespace in1\n"
1685             "namespace in2 {\n"
1686             "} // namespace in2\n"
1687             "} // namespace out",
1688             format("namespace out {\n"
1689                    "namespace in1 {\n"
1690                    "} // namespace in1\n"
1691                    "namespace in2 {\n"
1692                    "} // namespace in2\n"
1693                    "} // namespace out",
1694                    Style));
1695 
1696   EXPECT_EQ("namespace out {\n"
1697             "int i;\n"
1698             "namespace in {\n"
1699             "int j;\n"
1700             "} // namespace in\n"
1701             "int k;\n"
1702             "} // namespace out",
1703             format("namespace out { int i;\n"
1704                    "namespace in { int j; } // namespace in\n"
1705                    "int k; } // namespace out",
1706                    Style));
1707 
1708   EXPECT_EQ("namespace A { namespace B { namespace C {\n"
1709             "}}} // namespace A::B::C\n",
1710             format("namespace A { namespace B {\n"
1711                    "namespace C {\n"
1712                    "}} // namespace B::C\n"
1713                    "} // namespace A\n",
1714                    Style));
1715 
1716   Style.ColumnLimit = 40;
1717   EXPECT_EQ("namespace aaaaaaaaaa {\n"
1718             "namespace bbbbbbbbbb {\n"
1719             "}} // namespace aaaaaaaaaa::bbbbbbbbbb",
1720             format("namespace aaaaaaaaaa {\n"
1721                    "namespace bbbbbbbbbb {\n"
1722                    "} // namespace bbbbbbbbbb\n"
1723                    "} // namespace aaaaaaaaaa",
1724                    Style));
1725 
1726   EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n"
1727             "namespace cccccc {\n"
1728             "}}} // namespace aaaaaa::bbbbbb::cccccc",
1729             format("namespace aaaaaa {\n"
1730                    "namespace bbbbbb {\n"
1731                    "namespace cccccc {\n"
1732                    "} // namespace cccccc\n"
1733                    "} // namespace bbbbbb\n"
1734                    "} // namespace aaaaaa",
1735                    Style));
1736   Style.ColumnLimit = 80;
1737 
1738   // Extra semicolon after 'inner' closing brace prevents merging
1739   EXPECT_EQ("namespace out { namespace in {\n"
1740             "}; } // namespace out::in",
1741             format("namespace out {\n"
1742                    "namespace in {\n"
1743                    "}; // namespace in\n"
1744                    "} // namespace out",
1745                    Style));
1746 
1747   // Extra semicolon after 'outer' closing brace is conserved
1748   EXPECT_EQ("namespace out { namespace in {\n"
1749             "}}; // namespace out::in",
1750             format("namespace out {\n"
1751                    "namespace in {\n"
1752                    "} // namespace in\n"
1753                    "}; // namespace out",
1754                    Style));
1755 
1756   Style.NamespaceIndentation = FormatStyle::NI_All;
1757   EXPECT_EQ("namespace out { namespace in {\n"
1758             "  int i;\n"
1759             "}} // namespace out::in",
1760             format("namespace out {\n"
1761                    "namespace in {\n"
1762                    "int i;\n"
1763                    "} // namespace in\n"
1764                    "} // namespace out",
1765                    Style));
1766   EXPECT_EQ("namespace out { namespace mid {\n"
1767             "  namespace in {\n"
1768             "    int j;\n"
1769             "  } // namespace in\n"
1770             "  int k;\n"
1771             "}} // namespace out::mid",
1772             format("namespace out { namespace mid {\n"
1773                    "namespace in { int j; } // namespace in\n"
1774                    "int k; }} // namespace out::mid",
1775                    Style));
1776 
1777   Style.NamespaceIndentation = FormatStyle::NI_Inner;
1778   EXPECT_EQ("namespace out { namespace in {\n"
1779             "  int i;\n"
1780             "}} // namespace out::in",
1781             format("namespace out {\n"
1782                    "namespace in {\n"
1783                    "int i;\n"
1784                    "} // namespace in\n"
1785                    "} // namespace out",
1786                    Style));
1787   EXPECT_EQ("namespace out { namespace mid { namespace in {\n"
1788             "  int i;\n"
1789             "}}} // namespace out::mid::in",
1790             format("namespace out {\n"
1791                    "namespace mid {\n"
1792                    "namespace in {\n"
1793                    "int i;\n"
1794                    "} // namespace in\n"
1795                    "} // namespace mid\n"
1796                    "} // namespace out",
1797                    Style));
1798 }
1799 
1800 TEST_F(FormatTest, FormatsExternC) {
1801   verifyFormat("extern \"C\" {\nint a;");
1802   verifyFormat("extern \"C\" {}");
1803   verifyFormat("extern \"C\" {\n"
1804                "int foo();\n"
1805                "}");
1806   verifyFormat("extern \"C\" int foo() {}");
1807   verifyFormat("extern \"C\" int foo();");
1808   verifyFormat("extern \"C\" int foo() {\n"
1809                "  int i = 42;\n"
1810                "  return i;\n"
1811                "}");
1812 
1813   FormatStyle Style = getLLVMStyle();
1814   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1815   Style.BraceWrapping.AfterFunction = true;
1816   verifyFormat("extern \"C\" int foo() {}", Style);
1817   verifyFormat("extern \"C\" int foo();", Style);
1818   verifyFormat("extern \"C\" int foo()\n"
1819                "{\n"
1820                "  int i = 42;\n"
1821                "  return i;\n"
1822                "}",
1823                Style);
1824 
1825   Style.BraceWrapping.AfterExternBlock = true;
1826   Style.BraceWrapping.SplitEmptyRecord = false;
1827   verifyFormat("extern \"C\"\n"
1828                "{}",
1829                Style);
1830   verifyFormat("extern \"C\"\n"
1831                "{\n"
1832                "  int foo();\n"
1833                "}",
1834                Style);
1835 }
1836 
1837 TEST_F(FormatTest, FormatsInlineASM) {
1838   verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
1839   verifyFormat("asm(\"nop\" ::: \"memory\");");
1840   verifyFormat(
1841       "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
1842       "    \"cpuid\\n\\t\"\n"
1843       "    \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
1844       "    : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
1845       "    : \"a\"(value));");
1846   EXPECT_EQ(
1847       "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
1848       "  __asm {\n"
1849       "        mov     edx,[that] // vtable in edx\n"
1850       "        mov     eax,methodIndex\n"
1851       "        call    [edx][eax*4] // stdcall\n"
1852       "  }\n"
1853       "}",
1854       format("void NS_InvokeByIndex(void *that,   unsigned int methodIndex) {\n"
1855              "    __asm {\n"
1856              "        mov     edx,[that] // vtable in edx\n"
1857              "        mov     eax,methodIndex\n"
1858              "        call    [edx][eax*4] // stdcall\n"
1859              "    }\n"
1860              "}"));
1861   EXPECT_EQ("_asm {\n"
1862             "  xor eax, eax;\n"
1863             "  cpuid;\n"
1864             "}",
1865             format("_asm {\n"
1866                    "  xor eax, eax;\n"
1867                    "  cpuid;\n"
1868                    "}"));
1869   verifyFormat("void function() {\n"
1870                "  // comment\n"
1871                "  asm(\"\");\n"
1872                "}");
1873   EXPECT_EQ("__asm {\n"
1874             "}\n"
1875             "int i;",
1876             format("__asm   {\n"
1877                    "}\n"
1878                    "int   i;"));
1879 }
1880 
1881 TEST_F(FormatTest, FormatTryCatch) {
1882   verifyFormat("try {\n"
1883                "  throw a * b;\n"
1884                "} catch (int a) {\n"
1885                "  // Do nothing.\n"
1886                "} catch (...) {\n"
1887                "  exit(42);\n"
1888                "}");
1889 
1890   // Function-level try statements.
1891   verifyFormat("int f() try { return 4; } catch (...) {\n"
1892                "  return 5;\n"
1893                "}");
1894   verifyFormat("class A {\n"
1895                "  int a;\n"
1896                "  A() try : a(0) {\n"
1897                "  } catch (...) {\n"
1898                "    throw;\n"
1899                "  }\n"
1900                "};\n");
1901 
1902   // Incomplete try-catch blocks.
1903   verifyIncompleteFormat("try {} catch (");
1904 }
1905 
1906 TEST_F(FormatTest, FormatSEHTryCatch) {
1907   verifyFormat("__try {\n"
1908                "  int a = b * c;\n"
1909                "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
1910                "  // Do nothing.\n"
1911                "}");
1912 
1913   verifyFormat("__try {\n"
1914                "  int a = b * c;\n"
1915                "} __finally {\n"
1916                "  // Do nothing.\n"
1917                "}");
1918 
1919   verifyFormat("DEBUG({\n"
1920                "  __try {\n"
1921                "  } __finally {\n"
1922                "  }\n"
1923                "});\n");
1924 }
1925 
1926 TEST_F(FormatTest, IncompleteTryCatchBlocks) {
1927   verifyFormat("try {\n"
1928                "  f();\n"
1929                "} catch {\n"
1930                "  g();\n"
1931                "}");
1932   verifyFormat("try {\n"
1933                "  f();\n"
1934                "} catch (A a) MACRO(x) {\n"
1935                "  g();\n"
1936                "} catch (B b) MACRO(x) {\n"
1937                "  g();\n"
1938                "}");
1939 }
1940 
1941 TEST_F(FormatTest, FormatTryCatchBraceStyles) {
1942   FormatStyle Style = getLLVMStyle();
1943   for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla,
1944                           FormatStyle::BS_WebKit}) {
1945     Style.BreakBeforeBraces = BraceStyle;
1946     verifyFormat("try {\n"
1947                  "  // something\n"
1948                  "} catch (...) {\n"
1949                  "  // something\n"
1950                  "}",
1951                  Style);
1952   }
1953   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
1954   verifyFormat("try {\n"
1955                "  // something\n"
1956                "}\n"
1957                "catch (...) {\n"
1958                "  // something\n"
1959                "}",
1960                Style);
1961   verifyFormat("__try {\n"
1962                "  // something\n"
1963                "}\n"
1964                "__finally {\n"
1965                "  // something\n"
1966                "}",
1967                Style);
1968   verifyFormat("@try {\n"
1969                "  // something\n"
1970                "}\n"
1971                "@finally {\n"
1972                "  // something\n"
1973                "}",
1974                Style);
1975   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
1976   verifyFormat("try\n"
1977                "{\n"
1978                "  // something\n"
1979                "}\n"
1980                "catch (...)\n"
1981                "{\n"
1982                "  // something\n"
1983                "}",
1984                Style);
1985   Style.BreakBeforeBraces = FormatStyle::BS_GNU;
1986   verifyFormat("try\n"
1987                "  {\n"
1988                "    // something\n"
1989                "  }\n"
1990                "catch (...)\n"
1991                "  {\n"
1992                "    // something\n"
1993                "  }",
1994                Style);
1995   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1996   Style.BraceWrapping.BeforeCatch = true;
1997   verifyFormat("try {\n"
1998                "  // something\n"
1999                "}\n"
2000                "catch (...) {\n"
2001                "  // something\n"
2002                "}",
2003                Style);
2004 }
2005 
2006 TEST_F(FormatTest, StaticInitializers) {
2007   verifyFormat("static SomeClass SC = {1, 'a'};");
2008 
2009   verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
2010                "    100000000, "
2011                "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
2012 
2013   // Here, everything other than the "}" would fit on a line.
2014   verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
2015                "    10000000000000000000000000};");
2016   EXPECT_EQ("S s = {a,\n"
2017             "\n"
2018             "       b};",
2019             format("S s = {\n"
2020                    "  a,\n"
2021                    "\n"
2022                    "  b\n"
2023                    "};"));
2024 
2025   // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
2026   // line. However, the formatting looks a bit off and this probably doesn't
2027   // happen often in practice.
2028   verifyFormat("static int Variable[1] = {\n"
2029                "    {1000000000000000000000000000000000000}};",
2030                getLLVMStyleWithColumns(40));
2031 }
2032 
2033 TEST_F(FormatTest, DesignatedInitializers) {
2034   verifyFormat("const struct A a = {.a = 1, .b = 2};");
2035   verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
2036                "                    .bbbbbbbbbb = 2,\n"
2037                "                    .cccccccccc = 3,\n"
2038                "                    .dddddddddd = 4,\n"
2039                "                    .eeeeeeeeee = 5};");
2040   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
2041                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
2042                "    .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
2043                "    .ccccccccccccccccccccccccccc = 3,\n"
2044                "    .ddddddddddddddddddddddddddd = 4,\n"
2045                "    .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
2046 
2047   verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
2048 
2049   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
2050   verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n"
2051                "                    [2] = bbbbbbbbbb,\n"
2052                "                    [3] = cccccccccc,\n"
2053                "                    [4] = dddddddddd,\n"
2054                "                    [5] = eeeeeeeeee};");
2055   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
2056                "    [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
2057                "    [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
2058                "    [3] = cccccccccccccccccccccccccccccccccccccc,\n"
2059                "    [4] = dddddddddddddddddddddddddddddddddddddd,\n"
2060                "    [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};");
2061 }
2062 
2063 TEST_F(FormatTest, NestedStaticInitializers) {
2064   verifyFormat("static A x = {{{}}};\n");
2065   verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
2066                "               {init1, init2, init3, init4}}};",
2067                getLLVMStyleWithColumns(50));
2068 
2069   verifyFormat("somes Status::global_reps[3] = {\n"
2070                "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
2071                "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
2072                "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
2073                getLLVMStyleWithColumns(60));
2074   verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
2075                      "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
2076                      "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
2077                      "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
2078   verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
2079                "                  {rect.fRight - rect.fLeft, rect.fBottom - "
2080                "rect.fTop}};");
2081 
2082   verifyFormat(
2083       "SomeArrayOfSomeType a = {\n"
2084       "    {{1, 2, 3},\n"
2085       "     {1, 2, 3},\n"
2086       "     {111111111111111111111111111111, 222222222222222222222222222222,\n"
2087       "      333333333333333333333333333333},\n"
2088       "     {1, 2, 3},\n"
2089       "     {1, 2, 3}}};");
2090   verifyFormat(
2091       "SomeArrayOfSomeType a = {\n"
2092       "    {{1, 2, 3}},\n"
2093       "    {{1, 2, 3}},\n"
2094       "    {{111111111111111111111111111111, 222222222222222222222222222222,\n"
2095       "      333333333333333333333333333333}},\n"
2096       "    {{1, 2, 3}},\n"
2097       "    {{1, 2, 3}}};");
2098 
2099   verifyFormat("struct {\n"
2100                "  unsigned bit;\n"
2101                "  const char *const name;\n"
2102                "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
2103                "                 {kOsWin, \"Windows\"},\n"
2104                "                 {kOsLinux, \"Linux\"},\n"
2105                "                 {kOsCrOS, \"Chrome OS\"}};");
2106   verifyFormat("struct {\n"
2107                "  unsigned bit;\n"
2108                "  const char *const name;\n"
2109                "} kBitsToOs[] = {\n"
2110                "    {kOsMac, \"Mac\"},\n"
2111                "    {kOsWin, \"Windows\"},\n"
2112                "    {kOsLinux, \"Linux\"},\n"
2113                "    {kOsCrOS, \"Chrome OS\"},\n"
2114                "};");
2115 }
2116 
2117 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
2118   verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
2119                "                      \\\n"
2120                "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
2121 }
2122 
2123 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
2124   verifyFormat("virtual void write(ELFWriter *writerrr,\n"
2125                "                   OwningPtr<FileOutputBuffer> &buffer) = 0;");
2126 
2127   // Do break defaulted and deleted functions.
2128   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
2129                "    default;",
2130                getLLVMStyleWithColumns(40));
2131   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
2132                "    delete;",
2133                getLLVMStyleWithColumns(40));
2134 }
2135 
2136 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
2137   verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
2138                getLLVMStyleWithColumns(40));
2139   verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
2140                getLLVMStyleWithColumns(40));
2141   EXPECT_EQ("#define Q                              \\\n"
2142             "  \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\"    \\\n"
2143             "  \"aaaaaaaa.cpp\"",
2144             format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
2145                    getLLVMStyleWithColumns(40)));
2146 }
2147 
2148 TEST_F(FormatTest, UnderstandsLinePPDirective) {
2149   EXPECT_EQ("# 123 \"A string literal\"",
2150             format("   #     123    \"A string literal\""));
2151 }
2152 
2153 TEST_F(FormatTest, LayoutUnknownPPDirective) {
2154   EXPECT_EQ("#;", format("#;"));
2155   verifyFormat("#\n;\n;\n;");
2156 }
2157 
2158 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
2159   EXPECT_EQ("#line 42 \"test\"\n",
2160             format("#  \\\n  line  \\\n  42  \\\n  \"test\"\n"));
2161   EXPECT_EQ("#define A B\n", format("#  \\\n define  \\\n    A  \\\n       B\n",
2162                                     getLLVMStyleWithColumns(12)));
2163 }
2164 
2165 TEST_F(FormatTest, EndOfFileEndsPPDirective) {
2166   EXPECT_EQ("#line 42 \"test\"",
2167             format("#  \\\n  line  \\\n  42  \\\n  \"test\""));
2168   EXPECT_EQ("#define A B", format("#  \\\n define  \\\n    A  \\\n       B"));
2169 }
2170 
2171 TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
2172   verifyFormat("#define A \\x20");
2173   verifyFormat("#define A \\ x20");
2174   EXPECT_EQ("#define A \\ x20", format("#define A \\   x20"));
2175   verifyFormat("#define A ''");
2176   verifyFormat("#define A ''qqq");
2177   verifyFormat("#define A `qqq");
2178   verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
2179   EXPECT_EQ("const char *c = STRINGIFY(\n"
2180             "\\na : b);",
2181             format("const char * c = STRINGIFY(\n"
2182                    "\\na : b);"));
2183 
2184   verifyFormat("a\r\\");
2185   verifyFormat("a\v\\");
2186   verifyFormat("a\f\\");
2187 }
2188 
2189 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
2190   verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
2191   verifyFormat("#define A( \\\n    BB)", getLLVMStyleWithColumns(12));
2192   verifyFormat("#define A( \\\n    A, B)", getLLVMStyleWithColumns(12));
2193   // FIXME: We never break before the macro name.
2194   verifyFormat("#define AA( \\\n    B)", getLLVMStyleWithColumns(12));
2195 
2196   verifyFormat("#define A A\n#define A A");
2197   verifyFormat("#define A(X) A\n#define A A");
2198 
2199   verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
2200   verifyFormat("#define Something    \\\n  Other", getLLVMStyleWithColumns(22));
2201 }
2202 
2203 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
2204   EXPECT_EQ("// somecomment\n"
2205             "#include \"a.h\"\n"
2206             "#define A(  \\\n"
2207             "    A, B)\n"
2208             "#include \"b.h\"\n"
2209             "// somecomment\n",
2210             format("  // somecomment\n"
2211                    "  #include \"a.h\"\n"
2212                    "#define A(A,\\\n"
2213                    "    B)\n"
2214                    "    #include \"b.h\"\n"
2215                    " // somecomment\n",
2216                    getLLVMStyleWithColumns(13)));
2217 }
2218 
2219 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
2220 
2221 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
2222   EXPECT_EQ("#define A    \\\n"
2223             "  c;         \\\n"
2224             "  e;\n"
2225             "f;",
2226             format("#define A c; e;\n"
2227                    "f;",
2228                    getLLVMStyleWithColumns(14)));
2229 }
2230 
2231 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
2232 
2233 TEST_F(FormatTest, MacroDefinitionInsideStatement) {
2234   EXPECT_EQ("int x,\n"
2235             "#define A\n"
2236             "    y;",
2237             format("int x,\n#define A\ny;"));
2238 }
2239 
2240 TEST_F(FormatTest, HashInMacroDefinition) {
2241   EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
2242   verifyFormat("#define A \\\n  b #c;", getLLVMStyleWithColumns(11));
2243   verifyFormat("#define A  \\\n"
2244                "  {        \\\n"
2245                "    f(#c); \\\n"
2246                "  }",
2247                getLLVMStyleWithColumns(11));
2248 
2249   verifyFormat("#define A(X)         \\\n"
2250                "  void function##X()",
2251                getLLVMStyleWithColumns(22));
2252 
2253   verifyFormat("#define A(a, b, c)   \\\n"
2254                "  void a##b##c()",
2255                getLLVMStyleWithColumns(22));
2256 
2257   verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
2258 }
2259 
2260 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
2261   EXPECT_EQ("#define A (x)", format("#define A (x)"));
2262   EXPECT_EQ("#define A(x)", format("#define A(x)"));
2263 }
2264 
2265 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
2266   EXPECT_EQ("#define A b;", format("#define A \\\n"
2267                                    "          \\\n"
2268                                    "  b;",
2269                                    getLLVMStyleWithColumns(25)));
2270   EXPECT_EQ("#define A \\\n"
2271             "          \\\n"
2272             "  a;      \\\n"
2273             "  b;",
2274             format("#define A \\\n"
2275                    "          \\\n"
2276                    "  a;      \\\n"
2277                    "  b;",
2278                    getLLVMStyleWithColumns(11)));
2279   EXPECT_EQ("#define A \\\n"
2280             "  a;      \\\n"
2281             "          \\\n"
2282             "  b;",
2283             format("#define A \\\n"
2284                    "  a;      \\\n"
2285                    "          \\\n"
2286                    "  b;",
2287                    getLLVMStyleWithColumns(11)));
2288 }
2289 
2290 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
2291   verifyIncompleteFormat("#define A :");
2292   verifyFormat("#define SOMECASES  \\\n"
2293                "  case 1:          \\\n"
2294                "  case 2\n",
2295                getLLVMStyleWithColumns(20));
2296   verifyFormat("#define MACRO(a) \\\n"
2297                "  if (a)         \\\n"
2298                "    f();         \\\n"
2299                "  else           \\\n"
2300                "    g()",
2301                getLLVMStyleWithColumns(18));
2302   verifyFormat("#define A template <typename T>");
2303   verifyIncompleteFormat("#define STR(x) #x\n"
2304                          "f(STR(this_is_a_string_literal{));");
2305   verifyFormat("#pragma omp threadprivate( \\\n"
2306                "    y)), // expected-warning",
2307                getLLVMStyleWithColumns(28));
2308   verifyFormat("#d, = };");
2309   verifyFormat("#if \"a");
2310   verifyIncompleteFormat("({\n"
2311                          "#define b     \\\n"
2312                          "  }           \\\n"
2313                          "  a\n"
2314                          "a",
2315                          getLLVMStyleWithColumns(15));
2316   verifyFormat("#define A     \\\n"
2317                "  {           \\\n"
2318                "    {\n"
2319                "#define B     \\\n"
2320                "  }           \\\n"
2321                "  }",
2322                getLLVMStyleWithColumns(15));
2323   verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
2324   verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
2325   verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
2326   verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() {      \n)}");
2327 }
2328 
2329 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
2330   verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
2331   EXPECT_EQ("class A : public QObject {\n"
2332             "  Q_OBJECT\n"
2333             "\n"
2334             "  A() {}\n"
2335             "};",
2336             format("class A  :  public QObject {\n"
2337                    "     Q_OBJECT\n"
2338                    "\n"
2339                    "  A() {\n}\n"
2340                    "}  ;"));
2341   EXPECT_EQ("MACRO\n"
2342             "/*static*/ int i;",
2343             format("MACRO\n"
2344                    " /*static*/ int   i;"));
2345   EXPECT_EQ("SOME_MACRO\n"
2346             "namespace {\n"
2347             "void f();\n"
2348             "} // namespace",
2349             format("SOME_MACRO\n"
2350                    "  namespace    {\n"
2351                    "void   f(  );\n"
2352                    "} // namespace"));
2353   // Only if the identifier contains at least 5 characters.
2354   EXPECT_EQ("HTTP f();", format("HTTP\nf();"));
2355   EXPECT_EQ("MACRO\nf();", format("MACRO\nf();"));
2356   // Only if everything is upper case.
2357   EXPECT_EQ("class A : public QObject {\n"
2358             "  Q_Object A() {}\n"
2359             "};",
2360             format("class A  :  public QObject {\n"
2361                    "     Q_Object\n"
2362                    "  A() {\n}\n"
2363                    "}  ;"));
2364 
2365   // Only if the next line can actually start an unwrapped line.
2366   EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
2367             format("SOME_WEIRD_LOG_MACRO\n"
2368                    "<< SomeThing;"));
2369 
2370   verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
2371                "(n, buffers))\n",
2372                getChromiumStyle(FormatStyle::LK_Cpp));
2373 }
2374 
2375 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
2376   EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
2377             "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
2378             "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
2379             "class X {};\n"
2380             "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
2381             "int *createScopDetectionPass() { return 0; }",
2382             format("  INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
2383                    "  INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
2384                    "  INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
2385                    "  class X {};\n"
2386                    "  INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
2387                    "  int *createScopDetectionPass() { return 0; }"));
2388   // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
2389   // braces, so that inner block is indented one level more.
2390   EXPECT_EQ("int q() {\n"
2391             "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
2392             "  IPC_MESSAGE_HANDLER(xxx, qqq)\n"
2393             "  IPC_END_MESSAGE_MAP()\n"
2394             "}",
2395             format("int q() {\n"
2396                    "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
2397                    "    IPC_MESSAGE_HANDLER(xxx, qqq)\n"
2398                    "  IPC_END_MESSAGE_MAP()\n"
2399                    "}"));
2400 
2401   // Same inside macros.
2402   EXPECT_EQ("#define LIST(L) \\\n"
2403             "  L(A)          \\\n"
2404             "  L(B)          \\\n"
2405             "  L(C)",
2406             format("#define LIST(L) \\\n"
2407                    "  L(A) \\\n"
2408                    "  L(B) \\\n"
2409                    "  L(C)",
2410                    getGoogleStyle()));
2411 
2412   // These must not be recognized as macros.
2413   EXPECT_EQ("int q() {\n"
2414             "  f(x);\n"
2415             "  f(x) {}\n"
2416             "  f(x)->g();\n"
2417             "  f(x)->*g();\n"
2418             "  f(x).g();\n"
2419             "  f(x) = x;\n"
2420             "  f(x) += x;\n"
2421             "  f(x) -= x;\n"
2422             "  f(x) *= x;\n"
2423             "  f(x) /= x;\n"
2424             "  f(x) %= x;\n"
2425             "  f(x) &= x;\n"
2426             "  f(x) |= x;\n"
2427             "  f(x) ^= x;\n"
2428             "  f(x) >>= x;\n"
2429             "  f(x) <<= x;\n"
2430             "  f(x)[y].z();\n"
2431             "  LOG(INFO) << x;\n"
2432             "  ifstream(x) >> x;\n"
2433             "}\n",
2434             format("int q() {\n"
2435                    "  f(x)\n;\n"
2436                    "  f(x)\n {}\n"
2437                    "  f(x)\n->g();\n"
2438                    "  f(x)\n->*g();\n"
2439                    "  f(x)\n.g();\n"
2440                    "  f(x)\n = x;\n"
2441                    "  f(x)\n += x;\n"
2442                    "  f(x)\n -= x;\n"
2443                    "  f(x)\n *= x;\n"
2444                    "  f(x)\n /= x;\n"
2445                    "  f(x)\n %= x;\n"
2446                    "  f(x)\n &= x;\n"
2447                    "  f(x)\n |= x;\n"
2448                    "  f(x)\n ^= x;\n"
2449                    "  f(x)\n >>= x;\n"
2450                    "  f(x)\n <<= x;\n"
2451                    "  f(x)\n[y].z();\n"
2452                    "  LOG(INFO)\n << x;\n"
2453                    "  ifstream(x)\n >> x;\n"
2454                    "}\n"));
2455   EXPECT_EQ("int q() {\n"
2456             "  F(x)\n"
2457             "  if (1) {\n"
2458             "  }\n"
2459             "  F(x)\n"
2460             "  while (1) {\n"
2461             "  }\n"
2462             "  F(x)\n"
2463             "  G(x);\n"
2464             "  F(x)\n"
2465             "  try {\n"
2466             "    Q();\n"
2467             "  } catch (...) {\n"
2468             "  }\n"
2469             "}\n",
2470             format("int q() {\n"
2471                    "F(x)\n"
2472                    "if (1) {}\n"
2473                    "F(x)\n"
2474                    "while (1) {}\n"
2475                    "F(x)\n"
2476                    "G(x);\n"
2477                    "F(x)\n"
2478                    "try { Q(); } catch (...) {}\n"
2479                    "}\n"));
2480   EXPECT_EQ("class A {\n"
2481             "  A() : t(0) {}\n"
2482             "  A(int i) noexcept() : {}\n"
2483             "  A(X x)\n" // FIXME: function-level try blocks are broken.
2484             "  try : t(0) {\n"
2485             "  } catch (...) {\n"
2486             "  }\n"
2487             "};",
2488             format("class A {\n"
2489                    "  A()\n : t(0) {}\n"
2490                    "  A(int i)\n noexcept() : {}\n"
2491                    "  A(X x)\n"
2492                    "  try : t(0) {} catch (...) {}\n"
2493                    "};"));
2494   EXPECT_EQ("class SomeClass {\n"
2495             "public:\n"
2496             "  SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2497             "};",
2498             format("class SomeClass {\n"
2499                    "public:\n"
2500                    "  SomeClass()\n"
2501                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2502                    "};"));
2503   EXPECT_EQ("class SomeClass {\n"
2504             "public:\n"
2505             "  SomeClass()\n"
2506             "      EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2507             "};",
2508             format("class SomeClass {\n"
2509                    "public:\n"
2510                    "  SomeClass()\n"
2511                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2512                    "};",
2513                    getLLVMStyleWithColumns(40)));
2514 
2515   verifyFormat("MACRO(>)");
2516 }
2517 
2518 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
2519   verifyFormat("#define A \\\n"
2520                "  f({     \\\n"
2521                "    g();  \\\n"
2522                "  });",
2523                getLLVMStyleWithColumns(11));
2524 }
2525 
2526 TEST_F(FormatTest, IndentPreprocessorDirectives) {
2527   FormatStyle Style = getLLVMStyle();
2528   Style.IndentPPDirectives = FormatStyle::PPDIS_None;
2529   Style.ColumnLimit = 40;
2530   verifyFormat("#ifdef _WIN32\n"
2531                "#define A 0\n"
2532                "#ifdef VAR2\n"
2533                "#define B 1\n"
2534                "#include <someheader.h>\n"
2535                "#define MACRO                          \\\n"
2536                "  some_very_long_func_aaaaaaaaaa();\n"
2537                "#endif\n"
2538                "#else\n"
2539                "#define A 1\n"
2540                "#endif",
2541                Style);
2542   Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
2543   verifyFormat("#ifdef _WIN32\n"
2544                "#  define A 0\n"
2545                "#  ifdef VAR2\n"
2546                "#    define B 1\n"
2547                "#    include <someheader.h>\n"
2548                "#    define MACRO                      \\\n"
2549                "      some_very_long_func_aaaaaaaaaa();\n"
2550                "#  endif\n"
2551                "#else\n"
2552                "#  define A 1\n"
2553                "#endif",
2554                Style);
2555   verifyFormat("#if A\n"
2556                "#  define MACRO                        \\\n"
2557                "    void a(int x) {                    \\\n"
2558                "      b();                             \\\n"
2559                "      c();                             \\\n"
2560                "      d();                             \\\n"
2561                "      e();                             \\\n"
2562                "      f();                             \\\n"
2563                "    }\n"
2564                "#endif",
2565                Style);
2566   // Comments before include guard.
2567   verifyFormat("// file comment\n"
2568                "// file comment\n"
2569                "#ifndef HEADER_H\n"
2570                "#define HEADER_H\n"
2571                "code();\n"
2572                "#endif",
2573                Style);
2574   // Test with include guards.
2575   verifyFormat("#ifndef HEADER_H\n"
2576                "#define HEADER_H\n"
2577                "code();\n"
2578                "#endif",
2579                Style);
2580   // Include guards must have a #define with the same variable immediately
2581   // after #ifndef.
2582   verifyFormat("#ifndef NOT_GUARD\n"
2583                "#  define FOO\n"
2584                "code();\n"
2585                "#endif",
2586                Style);
2587 
2588   // Include guards must cover the entire file.
2589   verifyFormat("code();\n"
2590                "code();\n"
2591                "#ifndef NOT_GUARD\n"
2592                "#  define NOT_GUARD\n"
2593                "code();\n"
2594                "#endif",
2595                Style);
2596   verifyFormat("#ifndef NOT_GUARD\n"
2597                "#  define NOT_GUARD\n"
2598                "code();\n"
2599                "#endif\n"
2600                "code();",
2601                Style);
2602   // Test with trailing blank lines.
2603   verifyFormat("#ifndef HEADER_H\n"
2604                "#define HEADER_H\n"
2605                "code();\n"
2606                "#endif\n",
2607                Style);
2608   // Include guards don't have #else.
2609   verifyFormat("#ifndef NOT_GUARD\n"
2610                "#  define NOT_GUARD\n"
2611                "code();\n"
2612                "#else\n"
2613                "#endif",
2614                Style);
2615   verifyFormat("#ifndef NOT_GUARD\n"
2616                "#  define NOT_GUARD\n"
2617                "code();\n"
2618                "#elif FOO\n"
2619                "#endif",
2620                Style);
2621   // Non-identifier #define after potential include guard.
2622   verifyFormat("#ifndef FOO\n"
2623                "#  define 1\n"
2624                "#endif\n",
2625                Style);
2626   // #if closes past last non-preprocessor line.
2627   verifyFormat("#ifndef FOO\n"
2628                "#define FOO\n"
2629                "#if 1\n"
2630                "int i;\n"
2631                "#  define A 0\n"
2632                "#endif\n"
2633                "#endif\n",
2634                Style);
2635   // FIXME: This doesn't handle the case where there's code between the
2636   // #ifndef and #define but all other conditions hold. This is because when
2637   // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the
2638   // previous code line yet, so we can't detect it.
2639   EXPECT_EQ("#ifndef NOT_GUARD\n"
2640             "code();\n"
2641             "#define NOT_GUARD\n"
2642             "code();\n"
2643             "#endif",
2644             format("#ifndef NOT_GUARD\n"
2645                    "code();\n"
2646                    "#  define NOT_GUARD\n"
2647                    "code();\n"
2648                    "#endif",
2649                    Style));
2650   // FIXME: This doesn't handle cases where legitimate preprocessor lines may
2651   // be outside an include guard. Examples are #pragma once and
2652   // #pragma GCC diagnostic, or anything else that does not change the meaning
2653   // of the file if it's included multiple times.
2654   EXPECT_EQ("#ifdef WIN32\n"
2655             "#  pragma once\n"
2656             "#endif\n"
2657             "#ifndef HEADER_H\n"
2658             "#  define HEADER_H\n"
2659             "code();\n"
2660             "#endif",
2661             format("#ifdef WIN32\n"
2662                    "#  pragma once\n"
2663                    "#endif\n"
2664                    "#ifndef HEADER_H\n"
2665                    "#define HEADER_H\n"
2666                    "code();\n"
2667                    "#endif",
2668                    Style));
2669   // FIXME: This does not detect when there is a single non-preprocessor line
2670   // in front of an include-guard-like structure where other conditions hold
2671   // because ScopedLineState hides the line.
2672   EXPECT_EQ("code();\n"
2673             "#ifndef HEADER_H\n"
2674             "#define HEADER_H\n"
2675             "code();\n"
2676             "#endif",
2677             format("code();\n"
2678                    "#ifndef HEADER_H\n"
2679                    "#  define HEADER_H\n"
2680                    "code();\n"
2681                    "#endif",
2682                    Style));
2683   // Keep comments aligned with #, otherwise indent comments normally. These
2684   // tests cannot use verifyFormat because messUp manipulates leading
2685   // whitespace.
2686   {
2687     const char *Expected = ""
2688                            "void f() {\n"
2689                            "#if 1\n"
2690                            "// Preprocessor aligned.\n"
2691                            "#  define A 0\n"
2692                            "  // Code. Separated by blank line.\n"
2693                            "\n"
2694                            "#  define B 0\n"
2695                            "  // Code. Not aligned with #\n"
2696                            "#  define C 0\n"
2697                            "#endif";
2698     const char *ToFormat = ""
2699                            "void f() {\n"
2700                            "#if 1\n"
2701                            "// Preprocessor aligned.\n"
2702                            "#  define A 0\n"
2703                            "// Code. Separated by blank line.\n"
2704                            "\n"
2705                            "#  define B 0\n"
2706                            "   // Code. Not aligned with #\n"
2707                            "#  define C 0\n"
2708                            "#endif";
2709     EXPECT_EQ(Expected, format(ToFormat, Style));
2710     EXPECT_EQ(Expected, format(Expected, Style));
2711   }
2712   // Keep block quotes aligned.
2713   {
2714     const char *Expected = ""
2715                            "void f() {\n"
2716                            "#if 1\n"
2717                            "/* Preprocessor aligned. */\n"
2718                            "#  define A 0\n"
2719                            "  /* Code. Separated by blank line. */\n"
2720                            "\n"
2721                            "#  define B 0\n"
2722                            "  /* Code. Not aligned with # */\n"
2723                            "#  define C 0\n"
2724                            "#endif";
2725     const char *ToFormat = ""
2726                            "void f() {\n"
2727                            "#if 1\n"
2728                            "/* Preprocessor aligned. */\n"
2729                            "#  define A 0\n"
2730                            "/* Code. Separated by blank line. */\n"
2731                            "\n"
2732                            "#  define B 0\n"
2733                            "   /* Code. Not aligned with # */\n"
2734                            "#  define C 0\n"
2735                            "#endif";
2736     EXPECT_EQ(Expected, format(ToFormat, Style));
2737     EXPECT_EQ(Expected, format(Expected, Style));
2738   }
2739   // Keep comments aligned with un-indented directives.
2740   {
2741     const char *Expected = ""
2742                            "void f() {\n"
2743                            "// Preprocessor aligned.\n"
2744                            "#define A 0\n"
2745                            "  // Code. Separated by blank line.\n"
2746                            "\n"
2747                            "#define B 0\n"
2748                            "  // Code. Not aligned with #\n"
2749                            "#define C 0\n";
2750     const char *ToFormat = ""
2751                            "void f() {\n"
2752                            "// Preprocessor aligned.\n"
2753                            "#define A 0\n"
2754                            "// Code. Separated by blank line.\n"
2755                            "\n"
2756                            "#define B 0\n"
2757                            "   // Code. Not aligned with #\n"
2758                            "#define C 0\n";
2759     EXPECT_EQ(Expected, format(ToFormat, Style));
2760     EXPECT_EQ(Expected, format(Expected, Style));
2761   }
2762   // Test with tabs.
2763   Style.UseTab = FormatStyle::UT_Always;
2764   Style.IndentWidth = 8;
2765   Style.TabWidth = 8;
2766   verifyFormat("#ifdef _WIN32\n"
2767                "#\tdefine A 0\n"
2768                "#\tifdef VAR2\n"
2769                "#\t\tdefine B 1\n"
2770                "#\t\tinclude <someheader.h>\n"
2771                "#\t\tdefine MACRO          \\\n"
2772                "\t\t\tsome_very_long_func_aaaaaaaaaa();\n"
2773                "#\tendif\n"
2774                "#else\n"
2775                "#\tdefine A 1\n"
2776                "#endif",
2777                Style);
2778 
2779   // Regression test: Multiline-macro inside include guards.
2780   verifyFormat("#ifndef HEADER_H\n"
2781                "#define HEADER_H\n"
2782                "#define A()        \\\n"
2783                "  int i;           \\\n"
2784                "  int j;\n"
2785                "#endif // HEADER_H",
2786                getLLVMStyleWithColumns(20));
2787 }
2788 
2789 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
2790   verifyFormat("{\n  { a #c; }\n}");
2791 }
2792 
2793 TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
2794   EXPECT_EQ("#define A \\\n  {       \\\n    {\nint i;",
2795             format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
2796   EXPECT_EQ("#define A \\\n  }       \\\n  }\nint i;",
2797             format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
2798 }
2799 
2800 TEST_F(FormatTest, EscapedNewlines) {
2801   FormatStyle Narrow = getLLVMStyleWithColumns(11);
2802   EXPECT_EQ("#define A \\\n  int i;  \\\n  int j;",
2803             format("#define A \\\nint i;\\\n  int j;", Narrow));
2804   EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;"));
2805   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
2806   EXPECT_EQ("/* \\  \\  \\\n */", format("\\\n/* \\  \\  \\\n */"));
2807   EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>"));
2808 
2809   FormatStyle AlignLeft = getLLVMStyle();
2810   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
2811   EXPECT_EQ("#define MACRO(x) \\\n"
2812             "private:         \\\n"
2813             "  int x(int a);\n",
2814             format("#define MACRO(x) \\\n"
2815                    "private:         \\\n"
2816                    "  int x(int a);\n",
2817                    AlignLeft));
2818 
2819   // CRLF line endings
2820   EXPECT_EQ("#define A \\\r\n  int i;  \\\r\n  int j;",
2821             format("#define A \\\r\nint i;\\\r\n  int j;", Narrow));
2822   EXPECT_EQ("#define A\r\n\r\nint i;", format("#define A \\\r\n\r\n int i;"));
2823   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
2824   EXPECT_EQ("/* \\  \\  \\\r\n */", format("\\\r\n/* \\  \\  \\\r\n */"));
2825   EXPECT_EQ("<a\r\n\\\\\r\n>", format("<a\r\n\\\\\r\n>"));
2826   EXPECT_EQ("#define MACRO(x) \\\r\n"
2827             "private:         \\\r\n"
2828             "  int x(int a);\r\n",
2829             format("#define MACRO(x) \\\r\n"
2830                    "private:         \\\r\n"
2831                    "  int x(int a);\r\n",
2832                    AlignLeft));
2833 
2834   FormatStyle DontAlign = getLLVMStyle();
2835   DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
2836   DontAlign.MaxEmptyLinesToKeep = 3;
2837   // FIXME: can't use verifyFormat here because the newline before
2838   // "public:" is not inserted the first time it's reformatted
2839   EXPECT_EQ("#define A \\\n"
2840             "  class Foo { \\\n"
2841             "    void bar(); \\\n"
2842             "\\\n"
2843             "\\\n"
2844             "\\\n"
2845             "  public: \\\n"
2846             "    void baz(); \\\n"
2847             "  };",
2848             format("#define A \\\n"
2849                    "  class Foo { \\\n"
2850                    "    void bar(); \\\n"
2851                    "\\\n"
2852                    "\\\n"
2853                    "\\\n"
2854                    "  public: \\\n"
2855                    "    void baz(); \\\n"
2856                    "  };",
2857                    DontAlign));
2858 }
2859 
2860 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
2861   verifyFormat("#define A \\\n"
2862                "  int v(  \\\n"
2863                "      a); \\\n"
2864                "  int i;",
2865                getLLVMStyleWithColumns(11));
2866 }
2867 
2868 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
2869   EXPECT_EQ(
2870       "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
2871       "                      \\\n"
2872       "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
2873       "\n"
2874       "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
2875       "    aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
2876       format("  #define   ALooooooooooooooooooooooooooooooooooooooongMacro("
2877              "\\\n"
2878              "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
2879              "  \n"
2880              "   AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
2881              "  aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
2882 }
2883 
2884 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
2885   EXPECT_EQ("int\n"
2886             "#define A\n"
2887             "    a;",
2888             format("int\n#define A\na;"));
2889   verifyFormat("functionCallTo(\n"
2890                "    someOtherFunction(\n"
2891                "        withSomeParameters, whichInSequence,\n"
2892                "        areLongerThanALine(andAnotherCall,\n"
2893                "#define A B\n"
2894                "                           withMoreParamters,\n"
2895                "                           whichStronglyInfluenceTheLayout),\n"
2896                "        andMoreParameters),\n"
2897                "    trailing);",
2898                getLLVMStyleWithColumns(69));
2899   verifyFormat("Foo::Foo()\n"
2900                "#ifdef BAR\n"
2901                "    : baz(0)\n"
2902                "#endif\n"
2903                "{\n"
2904                "}");
2905   verifyFormat("void f() {\n"
2906                "  if (true)\n"
2907                "#ifdef A\n"
2908                "    f(42);\n"
2909                "  x();\n"
2910                "#else\n"
2911                "    g();\n"
2912                "  x();\n"
2913                "#endif\n"
2914                "}");
2915   verifyFormat("void f(param1, param2,\n"
2916                "       param3,\n"
2917                "#ifdef A\n"
2918                "       param4(param5,\n"
2919                "#ifdef A1\n"
2920                "              param6,\n"
2921                "#ifdef A2\n"
2922                "              param7),\n"
2923                "#else\n"
2924                "              param8),\n"
2925                "       param9,\n"
2926                "#endif\n"
2927                "       param10,\n"
2928                "#endif\n"
2929                "       param11)\n"
2930                "#else\n"
2931                "       param12)\n"
2932                "#endif\n"
2933                "{\n"
2934                "  x();\n"
2935                "}",
2936                getLLVMStyleWithColumns(28));
2937   verifyFormat("#if 1\n"
2938                "int i;");
2939   verifyFormat("#if 1\n"
2940                "#endif\n"
2941                "#if 1\n"
2942                "#else\n"
2943                "#endif\n");
2944   verifyFormat("DEBUG({\n"
2945                "  return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2946                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
2947                "});\n"
2948                "#if a\n"
2949                "#else\n"
2950                "#endif");
2951 
2952   verifyIncompleteFormat("void f(\n"
2953                          "#if A\n"
2954                          ");\n"
2955                          "#else\n"
2956                          "#endif");
2957 }
2958 
2959 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
2960   verifyFormat("#endif\n"
2961                "#if B");
2962 }
2963 
2964 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
2965   FormatStyle SingleLine = getLLVMStyle();
2966   SingleLine.AllowShortIfStatementsOnASingleLine = true;
2967   verifyFormat("#if 0\n"
2968                "#elif 1\n"
2969                "#endif\n"
2970                "void foo() {\n"
2971                "  if (test) foo2();\n"
2972                "}",
2973                SingleLine);
2974 }
2975 
2976 TEST_F(FormatTest, LayoutBlockInsideParens) {
2977   verifyFormat("functionCall({ int i; });");
2978   verifyFormat("functionCall({\n"
2979                "  int i;\n"
2980                "  int j;\n"
2981                "});");
2982   verifyFormat("functionCall(\n"
2983                "    {\n"
2984                "      int i;\n"
2985                "      int j;\n"
2986                "    },\n"
2987                "    aaaa, bbbb, cccc);");
2988   verifyFormat("functionA(functionB({\n"
2989                "            int i;\n"
2990                "            int j;\n"
2991                "          }),\n"
2992                "          aaaa, bbbb, cccc);");
2993   verifyFormat("functionCall(\n"
2994                "    {\n"
2995                "      int i;\n"
2996                "      int j;\n"
2997                "    },\n"
2998                "    aaaa, bbbb, // comment\n"
2999                "    cccc);");
3000   verifyFormat("functionA(functionB({\n"
3001                "            int i;\n"
3002                "            int j;\n"
3003                "          }),\n"
3004                "          aaaa, bbbb, // comment\n"
3005                "          cccc);");
3006   verifyFormat("functionCall(aaaa, bbbb, { int i; });");
3007   verifyFormat("functionCall(aaaa, bbbb, {\n"
3008                "  int i;\n"
3009                "  int j;\n"
3010                "});");
3011   verifyFormat(
3012       "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
3013       "    {\n"
3014       "      int i; // break\n"
3015       "    },\n"
3016       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
3017       "                                     ccccccccccccccccc));");
3018   verifyFormat("DEBUG({\n"
3019                "  if (a)\n"
3020                "    f();\n"
3021                "});");
3022 }
3023 
3024 TEST_F(FormatTest, LayoutBlockInsideStatement) {
3025   EXPECT_EQ("SOME_MACRO { int i; }\n"
3026             "int i;",
3027             format("  SOME_MACRO  {int i;}  int i;"));
3028 }
3029 
3030 TEST_F(FormatTest, LayoutNestedBlocks) {
3031   verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
3032                "  struct s {\n"
3033                "    int i;\n"
3034                "  };\n"
3035                "  s kBitsToOs[] = {{10}};\n"
3036                "  for (int i = 0; i < 10; ++i)\n"
3037                "    return;\n"
3038                "}");
3039   verifyFormat("call(parameter, {\n"
3040                "  something();\n"
3041                "  // Comment using all columns.\n"
3042                "  somethingelse();\n"
3043                "});",
3044                getLLVMStyleWithColumns(40));
3045   verifyFormat("DEBUG( //\n"
3046                "    { f(); }, a);");
3047   verifyFormat("DEBUG( //\n"
3048                "    {\n"
3049                "      f(); //\n"
3050                "    },\n"
3051                "    a);");
3052 
3053   EXPECT_EQ("call(parameter, {\n"
3054             "  something();\n"
3055             "  // Comment too\n"
3056             "  // looooooooooong.\n"
3057             "  somethingElse();\n"
3058             "});",
3059             format("call(parameter, {\n"
3060                    "  something();\n"
3061                    "  // Comment too looooooooooong.\n"
3062                    "  somethingElse();\n"
3063                    "});",
3064                    getLLVMStyleWithColumns(29)));
3065   EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int   i; });"));
3066   EXPECT_EQ("DEBUG({ // comment\n"
3067             "  int i;\n"
3068             "});",
3069             format("DEBUG({ // comment\n"
3070                    "int  i;\n"
3071                    "});"));
3072   EXPECT_EQ("DEBUG({\n"
3073             "  int i;\n"
3074             "\n"
3075             "  // comment\n"
3076             "  int j;\n"
3077             "});",
3078             format("DEBUG({\n"
3079                    "  int  i;\n"
3080                    "\n"
3081                    "  // comment\n"
3082                    "  int  j;\n"
3083                    "});"));
3084 
3085   verifyFormat("DEBUG({\n"
3086                "  if (a)\n"
3087                "    return;\n"
3088                "});");
3089   verifyGoogleFormat("DEBUG({\n"
3090                      "  if (a) return;\n"
3091                      "});");
3092   FormatStyle Style = getGoogleStyle();
3093   Style.ColumnLimit = 45;
3094   verifyFormat("Debug(aaaaa,\n"
3095                "      {\n"
3096                "        if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
3097                "      },\n"
3098                "      a);",
3099                Style);
3100 
3101   verifyFormat("SomeFunction({MACRO({ return output; }), b});");
3102 
3103   verifyNoCrash("^{v^{a}}");
3104 }
3105 
3106 TEST_F(FormatTest, FormatNestedBlocksInMacros) {
3107   EXPECT_EQ("#define MACRO()                     \\\n"
3108             "  Debug(aaa, /* force line break */ \\\n"
3109             "        {                           \\\n"
3110             "          int i;                    \\\n"
3111             "          int j;                    \\\n"
3112             "        })",
3113             format("#define   MACRO()   Debug(aaa,  /* force line break */ \\\n"
3114                    "          {  int   i;  int  j;   })",
3115                    getGoogleStyle()));
3116 
3117   EXPECT_EQ("#define A                                       \\\n"
3118             "  [] {                                          \\\n"
3119             "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \\\n"
3120             "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
3121             "  }",
3122             format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
3123                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
3124                    getGoogleStyle()));
3125 }
3126 
3127 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
3128   EXPECT_EQ("{}", format("{}"));
3129   verifyFormat("enum E {};");
3130   verifyFormat("enum E {}");
3131 }
3132 
3133 TEST_F(FormatTest, FormatBeginBlockEndMacros) {
3134   FormatStyle Style = getLLVMStyle();
3135   Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
3136   Style.MacroBlockEnd = "^[A-Z_]+_END$";
3137   verifyFormat("FOO_BEGIN\n"
3138                "  FOO_ENTRY\n"
3139                "FOO_END", Style);
3140   verifyFormat("FOO_BEGIN\n"
3141                "  NESTED_FOO_BEGIN\n"
3142                "    NESTED_FOO_ENTRY\n"
3143                "  NESTED_FOO_END\n"
3144                "FOO_END", Style);
3145   verifyFormat("FOO_BEGIN(Foo, Bar)\n"
3146                "  int x;\n"
3147                "  x = 1;\n"
3148                "FOO_END(Baz)", Style);
3149 }
3150 
3151 //===----------------------------------------------------------------------===//
3152 // Line break tests.
3153 //===----------------------------------------------------------------------===//
3154 
3155 TEST_F(FormatTest, PreventConfusingIndents) {
3156   verifyFormat(
3157       "void f() {\n"
3158       "  SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
3159       "                         parameter, parameter, parameter)),\n"
3160       "                     SecondLongCall(parameter));\n"
3161       "}");
3162   verifyFormat(
3163       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3164       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
3165       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3166       "    aaaaaaaaaaaaaaaaaaaaaaaa);");
3167   verifyFormat(
3168       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3169       "    [aaaaaaaaaaaaaaaaaaaaaaaa\n"
3170       "         [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
3171       "         [aaaaaaaaaaaaaaaaaaaaaaaa]];");
3172   verifyFormat(
3173       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
3174       "    aaaaaaaaaaaaaaaaaaaaaaaa<\n"
3175       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
3176       "    aaaaaaaaaaaaaaaaaaaaaaaa>;");
3177   verifyFormat("int a = bbbb && ccc &&\n"
3178                "        fffff(\n"
3179                "#define A Just forcing a new line\n"
3180                "            ddd);");
3181 }
3182 
3183 TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
3184   verifyFormat(
3185       "bool aaaaaaa =\n"
3186       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
3187       "    bbbbbbbb();");
3188   verifyFormat(
3189       "bool aaaaaaa =\n"
3190       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
3191       "    bbbbbbbb();");
3192 
3193   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
3194                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
3195                "    ccccccccc == ddddddddddd;");
3196   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
3197                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
3198                "    ccccccccc == ddddddddddd;");
3199   verifyFormat(
3200       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
3201       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
3202       "    ccccccccc == ddddddddddd;");
3203 
3204   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
3205                "                 aaaaaa) &&\n"
3206                "         bbbbbb && cccccc;");
3207   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
3208                "                 aaaaaa) >>\n"
3209                "         bbbbbb;");
3210   verifyFormat("aa = Whitespaces.addUntouchableComment(\n"
3211                "    SourceMgr.getSpellingColumnNumber(\n"
3212                "        TheLine.Last->FormatTok.Tok.getLocation()) -\n"
3213                "    1);");
3214 
3215   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3216                "     bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
3217                "    cccccc) {\n}");
3218   verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3219                "               bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
3220                "              cccccc) {\n}");
3221   verifyFormat("b = a &&\n"
3222                "    // Comment\n"
3223                "    b.c && d;");
3224 
3225   // If the LHS of a comparison is not a binary expression itself, the
3226   // additional linebreak confuses many people.
3227   verifyFormat(
3228       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3229       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
3230       "}");
3231   verifyFormat(
3232       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3233       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3234       "}");
3235   verifyFormat(
3236       "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
3237       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3238       "}");
3239   verifyFormat(
3240       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3241       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n"
3242       "}");
3243   // Even explicit parentheses stress the precedence enough to make the
3244   // additional break unnecessary.
3245   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3246                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3247                "}");
3248   // This cases is borderline, but with the indentation it is still readable.
3249   verifyFormat(
3250       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3251       "        aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3252       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
3253       "}",
3254       getLLVMStyleWithColumns(75));
3255 
3256   // If the LHS is a binary expression, we should still use the additional break
3257   // as otherwise the formatting hides the operator precedence.
3258   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3259                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3260                "    5) {\n"
3261                "}");
3262   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3263                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n"
3264                "    5) {\n"
3265                "}");
3266 
3267   FormatStyle OnePerLine = getLLVMStyle();
3268   OnePerLine.BinPackParameters = false;
3269   verifyFormat(
3270       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3271       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3272       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
3273       OnePerLine);
3274 
3275   verifyFormat("int i = someFunction(aaaaaaa, 0)\n"
3276                "                .aaa(aaaaaaaaaaaaa) *\n"
3277                "            aaaaaaa +\n"
3278                "        aaaaaaa;",
3279                getLLVMStyleWithColumns(40));
3280 }
3281 
3282 TEST_F(FormatTest, ExpressionIndentation) {
3283   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3284                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3285                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3286                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3287                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
3288                "                     bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
3289                "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3290                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
3291                "                 ccccccccccccccccccccccccccccccccccccccccc;");
3292   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3293                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3294                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3295                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3296   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3297                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3298                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3299                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3300   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3301                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3302                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3303                "        bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3304   verifyFormat("if () {\n"
3305                "} else if (aaaaa && bbbbb > // break\n"
3306                "                        ccccc) {\n"
3307                "}");
3308   verifyFormat("if () {\n"
3309                "} else if (aaaaa &&\n"
3310                "           bbbbb > // break\n"
3311                "               ccccc &&\n"
3312                "           ddddd) {\n"
3313                "}");
3314 
3315   // Presence of a trailing comment used to change indentation of b.
3316   verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
3317                "       b;\n"
3318                "return aaaaaaaaaaaaaaaaaaa +\n"
3319                "       b; //",
3320                getLLVMStyleWithColumns(30));
3321 }
3322 
3323 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
3324   // Not sure what the best system is here. Like this, the LHS can be found
3325   // immediately above an operator (everything with the same or a higher
3326   // indent). The RHS is aligned right of the operator and so compasses
3327   // everything until something with the same indent as the operator is found.
3328   // FIXME: Is this a good system?
3329   FormatStyle Style = getLLVMStyle();
3330   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
3331   verifyFormat(
3332       "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3333       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3334       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3335       "                 == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3336       "                            * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3337       "                        + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3338       "             && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3339       "                        * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3340       "                    > ccccccccccccccccccccccccccccccccccccccccc;",
3341       Style);
3342   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3343                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3344                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3345                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3346                Style);
3347   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3348                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3349                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3350                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3351                Style);
3352   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3353                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3354                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3355                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3356                Style);
3357   verifyFormat("if () {\n"
3358                "} else if (aaaaa\n"
3359                "           && bbbbb // break\n"
3360                "                  > ccccc) {\n"
3361                "}",
3362                Style);
3363   verifyFormat("return (a)\n"
3364                "       // comment\n"
3365                "       + b;",
3366                Style);
3367   verifyFormat(
3368       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3369       "                 * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3370       "             + cc;",
3371       Style);
3372 
3373   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3374                "    = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
3375                Style);
3376 
3377   // Forced by comments.
3378   verifyFormat(
3379       "unsigned ContentSize =\n"
3380       "    sizeof(int16_t)   // DWARF ARange version number\n"
3381       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
3382       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
3383       "    + sizeof(int8_t); // Segment Size (in bytes)");
3384 
3385   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
3386                "       == boost::fusion::at_c<1>(iiii).second;",
3387                Style);
3388 
3389   Style.ColumnLimit = 60;
3390   verifyFormat("zzzzzzzzzz\n"
3391                "    = bbbbbbbbbbbbbbbbb\n"
3392                "      >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
3393                Style);
3394 
3395   Style.ColumnLimit = 80;
3396   Style.IndentWidth = 4;
3397   Style.TabWidth = 4;
3398   Style.UseTab = FormatStyle::UT_Always;
3399   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
3400   Style.AlignOperands = false;
3401   EXPECT_EQ("return someVeryVeryLongConditionThatBarelyFitsOnALine\n"
3402             "\t&& (someOtherLongishConditionPart1\n"
3403             "\t\t|| someOtherEvenLongerNestedConditionPart2);",
3404             format("return someVeryVeryLongConditionThatBarelyFitsOnALine && (someOtherLongishConditionPart1 || someOtherEvenLongerNestedConditionPart2);",
3405                    Style));
3406 }
3407 
3408 TEST_F(FormatTest, EnforcedOperatorWraps) {
3409   // Here we'd like to wrap after the || operators, but a comment is forcing an
3410   // earlier wrap.
3411   verifyFormat("bool x = aaaaa //\n"
3412                "         || bbbbb\n"
3413                "         //\n"
3414                "         || cccc;");
3415 }
3416 
3417 TEST_F(FormatTest, NoOperandAlignment) {
3418   FormatStyle Style = getLLVMStyle();
3419   Style.AlignOperands = false;
3420   verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n"
3421                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3422                "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3423                Style);
3424   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
3425   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3426                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3427                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3428                "        == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3429                "                * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3430                "            + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3431                "    && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3432                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3433                "        > ccccccccccccccccccccccccccccccccccccccccc;",
3434                Style);
3435 
3436   verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3437                "        * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3438                "    + cc;",
3439                Style);
3440   verifyFormat("int a = aa\n"
3441                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3442                "        * cccccccccccccccccccccccccccccccccccc;\n",
3443                Style);
3444 
3445   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
3446   verifyFormat("return (a > b\n"
3447                "    // comment1\n"
3448                "    // comment2\n"
3449                "    || c);",
3450                Style);
3451 }
3452 
3453 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
3454   FormatStyle Style = getLLVMStyle();
3455   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
3456   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
3457                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3458                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
3459                Style);
3460 }
3461 
3462 TEST_F(FormatTest, AllowBinPackingInsideArguments) {
3463   FormatStyle Style = getLLVMStyle();
3464   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
3465   Style.BinPackArguments = false;
3466   Style.ColumnLimit = 40;
3467   verifyFormat("void test() {\n"
3468                "  someFunction(\n"
3469                "      this + argument + is + quite\n"
3470                "      + long + so + it + gets + wrapped\n"
3471                "      + but + remains + bin - packed);\n"
3472                "}",
3473                Style);
3474   verifyFormat("void test() {\n"
3475                "  someFunction(arg1,\n"
3476                "               this + argument + is\n"
3477                "                   + quite + long + so\n"
3478                "                   + it + gets + wrapped\n"
3479                "                   + but + remains + bin\n"
3480                "                   - packed,\n"
3481                "               arg3);\n"
3482                "}",
3483                Style);
3484   verifyFormat("void test() {\n"
3485                "  someFunction(\n"
3486                "      arg1,\n"
3487                "      this + argument + has\n"
3488                "          + anotherFunc(nested,\n"
3489                "                        calls + whose\n"
3490                "                            + arguments\n"
3491                "                            + are + also\n"
3492                "                            + wrapped,\n"
3493                "                        in + addition)\n"
3494                "          + to + being + bin - packed,\n"
3495                "      arg3);\n"
3496                "}",
3497                Style);
3498 
3499   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
3500   verifyFormat("void test() {\n"
3501                "  someFunction(\n"
3502                "      arg1,\n"
3503                "      this + argument + has +\n"
3504                "          anotherFunc(nested,\n"
3505                "                      calls + whose +\n"
3506                "                          arguments +\n"
3507                "                          are + also +\n"
3508                "                          wrapped,\n"
3509                "                      in + addition) +\n"
3510                "          to + being + bin - packed,\n"
3511                "      arg3);\n"
3512                "}",
3513                Style);
3514 }
3515 
3516 TEST_F(FormatTest, ConstructorInitializers) {
3517   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
3518   verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
3519                getLLVMStyleWithColumns(45));
3520   verifyFormat("Constructor()\n"
3521                "    : Inttializer(FitsOnTheLine) {}",
3522                getLLVMStyleWithColumns(44));
3523   verifyFormat("Constructor()\n"
3524                "    : Inttializer(FitsOnTheLine) {}",
3525                getLLVMStyleWithColumns(43));
3526 
3527   verifyFormat("template <typename T>\n"
3528                "Constructor() : Initializer(FitsOnTheLine) {}",
3529                getLLVMStyleWithColumns(45));
3530 
3531   verifyFormat(
3532       "SomeClass::Constructor()\n"
3533       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
3534 
3535   verifyFormat(
3536       "SomeClass::Constructor()\n"
3537       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3538       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
3539   verifyFormat(
3540       "SomeClass::Constructor()\n"
3541       "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3542       "      aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
3543   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3544                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3545                "    : aaaaaaaaaa(aaaaaa) {}");
3546 
3547   verifyFormat("Constructor()\n"
3548                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3549                "      aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3550                "                               aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3551                "      aaaaaaaaaaaaaaaaaaaaaaa() {}");
3552 
3553   verifyFormat("Constructor()\n"
3554                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3555                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3556 
3557   verifyFormat("Constructor(int Parameter = 0)\n"
3558                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
3559                "      aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
3560   verifyFormat("Constructor()\n"
3561                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
3562                "}",
3563                getLLVMStyleWithColumns(60));
3564   verifyFormat("Constructor()\n"
3565                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3566                "          aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
3567 
3568   // Here a line could be saved by splitting the second initializer onto two
3569   // lines, but that is not desirable.
3570   verifyFormat("Constructor()\n"
3571                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
3572                "      aaaaaaaaaaa(aaaaaaaaaaa),\n"
3573                "      aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3574 
3575   FormatStyle OnePerLine = getLLVMStyle();
3576   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
3577   OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
3578   verifyFormat("SomeClass::Constructor()\n"
3579                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3580                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3581                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3582                OnePerLine);
3583   verifyFormat("SomeClass::Constructor()\n"
3584                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
3585                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3586                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3587                OnePerLine);
3588   verifyFormat("MyClass::MyClass(int var)\n"
3589                "    : some_var_(var),            // 4 space indent\n"
3590                "      some_other_var_(var + 1) { // lined up\n"
3591                "}",
3592                OnePerLine);
3593   verifyFormat("Constructor()\n"
3594                "    : aaaaa(aaaaaa),\n"
3595                "      aaaaa(aaaaaa),\n"
3596                "      aaaaa(aaaaaa),\n"
3597                "      aaaaa(aaaaaa),\n"
3598                "      aaaaa(aaaaaa) {}",
3599                OnePerLine);
3600   verifyFormat("Constructor()\n"
3601                "    : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
3602                "            aaaaaaaaaaaaaaaaaaaaaa) {}",
3603                OnePerLine);
3604   OnePerLine.BinPackParameters = false;
3605   verifyFormat(
3606       "Constructor()\n"
3607       "    : aaaaaaaaaaaaaaaaaaaaaaaa(\n"
3608       "          aaaaaaaaaaa().aaa(),\n"
3609       "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3610       OnePerLine);
3611   OnePerLine.ColumnLimit = 60;
3612   verifyFormat("Constructor()\n"
3613                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
3614                "      bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
3615                OnePerLine);
3616 
3617   EXPECT_EQ("Constructor()\n"
3618             "    : // Comment forcing unwanted break.\n"
3619             "      aaaa(aaaa) {}",
3620             format("Constructor() :\n"
3621                    "    // Comment forcing unwanted break.\n"
3622                    "    aaaa(aaaa) {}"));
3623 }
3624 
3625 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) {
3626   FormatStyle Style = getLLVMStyle();
3627   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
3628 
3629   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
3630   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}",
3631                getStyleWithColumns(Style, 45));
3632   verifyFormat("Constructor() :\n"
3633                "    Initializer(FitsOnTheLine) {}",
3634                getStyleWithColumns(Style, 44));
3635   verifyFormat("Constructor() :\n"
3636                "    Initializer(FitsOnTheLine) {}",
3637                getStyleWithColumns(Style, 43));
3638 
3639   verifyFormat("template <typename T>\n"
3640                "Constructor() : Initializer(FitsOnTheLine) {}",
3641                getStyleWithColumns(Style, 50));
3642 
3643   verifyFormat(
3644       "SomeClass::Constructor() :\n"
3645       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
3646 	  Style);
3647 
3648   verifyFormat(
3649       "SomeClass::Constructor() :\n"
3650       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3651       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3652 	  Style);
3653   verifyFormat(
3654       "SomeClass::Constructor() :\n"
3655       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3656       "    aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
3657 	  Style);
3658   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3659                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
3660                "    aaaaaaaaaa(aaaaaa) {}",
3661 			   Style);
3662 
3663   verifyFormat("Constructor() :\n"
3664                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3665                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3666                "                             aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3667                "    aaaaaaaaaaaaaaaaaaaaaaa() {}",
3668 			   Style);
3669 
3670   verifyFormat("Constructor() :\n"
3671                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3672                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3673 			   Style);
3674 
3675   verifyFormat("Constructor(int Parameter = 0) :\n"
3676                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
3677                "    aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}",
3678 			   Style);
3679   verifyFormat("Constructor() :\n"
3680                "    aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
3681                "}",
3682                getStyleWithColumns(Style, 60));
3683   verifyFormat("Constructor() :\n"
3684                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3685                "        aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}",
3686 			   Style);
3687 
3688   // Here a line could be saved by splitting the second initializer onto two
3689   // lines, but that is not desirable.
3690   verifyFormat("Constructor() :\n"
3691                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
3692                "    aaaaaaaaaaa(aaaaaaaaaaa),\n"
3693                "    aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3694 			   Style);
3695 
3696   FormatStyle OnePerLine = Style;
3697   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
3698   OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
3699   verifyFormat("SomeClass::Constructor() :\n"
3700                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3701                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3702                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3703                OnePerLine);
3704   verifyFormat("SomeClass::Constructor() :\n"
3705                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
3706                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3707                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3708                OnePerLine);
3709   verifyFormat("MyClass::MyClass(int var) :\n"
3710                "    some_var_(var),            // 4 space indent\n"
3711                "    some_other_var_(var + 1) { // lined up\n"
3712                "}",
3713                OnePerLine);
3714   verifyFormat("Constructor() :\n"
3715                "    aaaaa(aaaaaa),\n"
3716                "    aaaaa(aaaaaa),\n"
3717                "    aaaaa(aaaaaa),\n"
3718                "    aaaaa(aaaaaa),\n"
3719                "    aaaaa(aaaaaa) {}",
3720                OnePerLine);
3721   verifyFormat("Constructor() :\n"
3722                "    aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
3723                "          aaaaaaaaaaaaaaaaaaaaaa) {}",
3724                OnePerLine);
3725   OnePerLine.BinPackParameters = false;
3726   verifyFormat(
3727       "Constructor() :\n"
3728       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
3729       "        aaaaaaaaaaa().aaa(),\n"
3730       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3731       OnePerLine);
3732   OnePerLine.ColumnLimit = 60;
3733   verifyFormat("Constructor() :\n"
3734                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
3735                "    bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
3736                OnePerLine);
3737 
3738   EXPECT_EQ("Constructor() :\n"
3739             "    // Comment forcing unwanted break.\n"
3740             "    aaaa(aaaa) {}",
3741             format("Constructor() :\n"
3742                    "    // Comment forcing unwanted break.\n"
3743                    "    aaaa(aaaa) {}",
3744 				   Style));
3745 
3746   Style.ColumnLimit = 0;
3747   verifyFormat("SomeClass::Constructor() :\n"
3748                "    a(a) {}",
3749                Style);
3750   verifyFormat("SomeClass::Constructor() noexcept :\n"
3751                "    a(a) {}",
3752                Style);
3753   verifyFormat("SomeClass::Constructor() :\n"
3754 			   "    a(a), b(b), c(c) {}",
3755                Style);
3756   verifyFormat("SomeClass::Constructor() :\n"
3757                "    a(a) {\n"
3758                "  foo();\n"
3759                "  bar();\n"
3760                "}",
3761                Style);
3762 
3763   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
3764   verifyFormat("SomeClass::Constructor() :\n"
3765 			   "    a(a), b(b), c(c) {\n"
3766 			   "}",
3767                Style);
3768   verifyFormat("SomeClass::Constructor() :\n"
3769                "    a(a) {\n"
3770 			   "}",
3771                Style);
3772 
3773   Style.ColumnLimit = 80;
3774   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
3775   Style.ConstructorInitializerIndentWidth = 2;
3776   verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}",
3777                Style);
3778   verifyFormat("SomeClass::Constructor() :\n"
3779                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3780                "  bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}",
3781                Style);
3782 
3783   // `ConstructorInitializerIndentWidth` actually applies to InheritanceList as well
3784   Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
3785   verifyFormat("class SomeClass\n"
3786                "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3787                "    public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
3788                Style);
3789   Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
3790   verifyFormat("class SomeClass\n"
3791                "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3792                "  , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
3793                Style);
3794   Style.BreakInheritanceList = FormatStyle::BILS_AfterColon;
3795   verifyFormat("class SomeClass :\n"
3796                "  public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3797                "  public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
3798                Style);
3799 }
3800 
3801 #ifndef EXPENSIVE_CHECKS
3802 // Expensive checks enables libstdc++ checking which includes validating the
3803 // state of ranges used in std::priority_queue - this blows out the
3804 // runtime/scalability of the function and makes this test unacceptably slow.
3805 TEST_F(FormatTest, MemoizationTests) {
3806   // This breaks if the memoization lookup does not take \c Indent and
3807   // \c LastSpace into account.
3808   verifyFormat(
3809       "extern CFRunLoopTimerRef\n"
3810       "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
3811       "                     CFTimeInterval interval, CFOptionFlags flags,\n"
3812       "                     CFIndex order, CFRunLoopTimerCallBack callout,\n"
3813       "                     CFRunLoopTimerContext *context) {}");
3814 
3815   // Deep nesting somewhat works around our memoization.
3816   verifyFormat(
3817       "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3818       "    aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3819       "        aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3820       "            aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3821       "                aaaaa())))))))))))))))))))))))))))))))))))))));",
3822       getLLVMStyleWithColumns(65));
3823   verifyFormat(
3824       "aaaaa(\n"
3825       "    aaaaa,\n"
3826       "    aaaaa(\n"
3827       "        aaaaa,\n"
3828       "        aaaaa(\n"
3829       "            aaaaa,\n"
3830       "            aaaaa(\n"
3831       "                aaaaa,\n"
3832       "                aaaaa(\n"
3833       "                    aaaaa,\n"
3834       "                    aaaaa(\n"
3835       "                        aaaaa,\n"
3836       "                        aaaaa(\n"
3837       "                            aaaaa,\n"
3838       "                            aaaaa(\n"
3839       "                                aaaaa,\n"
3840       "                                aaaaa(\n"
3841       "                                    aaaaa,\n"
3842       "                                    aaaaa(\n"
3843       "                                        aaaaa,\n"
3844       "                                        aaaaa(\n"
3845       "                                            aaaaa,\n"
3846       "                                            aaaaa(\n"
3847       "                                                aaaaa,\n"
3848       "                                                aaaaa))))))))))));",
3849       getLLVMStyleWithColumns(65));
3850   verifyFormat(
3851       "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"
3852       "                                  a),\n"
3853       "                                a),\n"
3854       "                              a),\n"
3855       "                            a),\n"
3856       "                          a),\n"
3857       "                        a),\n"
3858       "                      a),\n"
3859       "                    a),\n"
3860       "                  a),\n"
3861       "                a),\n"
3862       "              a),\n"
3863       "            a),\n"
3864       "          a),\n"
3865       "        a),\n"
3866       "      a),\n"
3867       "    a),\n"
3868       "  a)",
3869       getLLVMStyleWithColumns(65));
3870 
3871   // This test takes VERY long when memoization is broken.
3872   FormatStyle OnePerLine = getLLVMStyle();
3873   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
3874   OnePerLine.BinPackParameters = false;
3875   std::string input = "Constructor()\n"
3876                       "    : aaaa(a,\n";
3877   for (unsigned i = 0, e = 80; i != e; ++i) {
3878     input += "           a,\n";
3879   }
3880   input += "           a) {}";
3881   verifyFormat(input, OnePerLine);
3882 }
3883 #endif
3884 
3885 TEST_F(FormatTest, BreaksAsHighAsPossible) {
3886   verifyFormat(
3887       "void f() {\n"
3888       "  if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
3889       "      (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
3890       "    f();\n"
3891       "}");
3892   verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
3893                "    Intervals[i - 1].getRange().getLast()) {\n}");
3894 }
3895 
3896 TEST_F(FormatTest, BreaksFunctionDeclarations) {
3897   // Principially, we break function declarations in a certain order:
3898   // 1) break amongst arguments.
3899   verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
3900                "                              Cccccccccccccc cccccccccccccc);");
3901   verifyFormat("template <class TemplateIt>\n"
3902                "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
3903                "                            TemplateIt *stop) {}");
3904 
3905   // 2) break after return type.
3906   verifyFormat(
3907       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3908       "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
3909       getGoogleStyle());
3910 
3911   // 3) break after (.
3912   verifyFormat(
3913       "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
3914       "    Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
3915       getGoogleStyle());
3916 
3917   // 4) break before after nested name specifiers.
3918   verifyFormat(
3919       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3920       "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
3921       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
3922       getGoogleStyle());
3923 
3924   // However, there are exceptions, if a sufficient amount of lines can be
3925   // saved.
3926   // FIXME: The precise cut-offs wrt. the number of saved lines might need some
3927   // more adjusting.
3928   verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
3929                "                                  Cccccccccccccc cccccccccc,\n"
3930                "                                  Cccccccccccccc cccccccccc,\n"
3931                "                                  Cccccccccccccc cccccccccc,\n"
3932                "                                  Cccccccccccccc cccccccccc);");
3933   verifyFormat(
3934       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3935       "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3936       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3937       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
3938       getGoogleStyle());
3939   verifyFormat(
3940       "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
3941       "                                          Cccccccccccccc cccccccccc,\n"
3942       "                                          Cccccccccccccc cccccccccc,\n"
3943       "                                          Cccccccccccccc cccccccccc,\n"
3944       "                                          Cccccccccccccc cccccccccc,\n"
3945       "                                          Cccccccccccccc cccccccccc,\n"
3946       "                                          Cccccccccccccc cccccccccc);");
3947   verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
3948                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3949                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3950                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3951                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
3952 
3953   // Break after multi-line parameters.
3954   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3955                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3956                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3957                "    bbbb bbbb);");
3958   verifyFormat("void SomeLoooooooooooongFunction(\n"
3959                "    std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
3960                "        aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3961                "    int bbbbbbbbbbbbb);");
3962 
3963   // Treat overloaded operators like other functions.
3964   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3965                "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
3966   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3967                "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
3968   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3969                "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
3970   verifyGoogleFormat(
3971       "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
3972       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
3973   verifyGoogleFormat(
3974       "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
3975       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
3976   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3977                "    int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
3978   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
3979                "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
3980   verifyGoogleFormat(
3981       "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
3982       "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3983       "    bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
3984   verifyGoogleFormat(
3985       "template <typename T>\n"
3986       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3987       "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n"
3988       "    aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);");
3989 
3990   FormatStyle Style = getLLVMStyle();
3991   Style.PointerAlignment = FormatStyle::PAS_Left;
3992   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3993                "    aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
3994                Style);
3995   verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
3996                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3997                Style);
3998 }
3999 
4000 TEST_F(FormatTest, TrailingReturnType) {
4001   verifyFormat("auto foo() -> int;\n");
4002   verifyFormat("struct S {\n"
4003                "  auto bar() const -> int;\n"
4004                "};");
4005   verifyFormat("template <size_t Order, typename T>\n"
4006                "auto load_img(const std::string &filename)\n"
4007                "    -> alias::tensor<Order, T, mem::tag::cpu> {}");
4008   verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
4009                "    -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
4010   verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
4011   verifyFormat("template <typename T>\n"
4012                "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
4013                "    -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
4014 
4015   // Not trailing return types.
4016   verifyFormat("void f() { auto a = b->c(); }");
4017 }
4018 
4019 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
4020   // Avoid breaking before trailing 'const' or other trailing annotations, if
4021   // they are not function-like.
4022   FormatStyle Style = getGoogleStyle();
4023   Style.ColumnLimit = 47;
4024   verifyFormat("void someLongFunction(\n"
4025                "    int someLoooooooooooooongParameter) const {\n}",
4026                getLLVMStyleWithColumns(47));
4027   verifyFormat("LoooooongReturnType\n"
4028                "someLoooooooongFunction() const {}",
4029                getLLVMStyleWithColumns(47));
4030   verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
4031                "    const {}",
4032                Style);
4033   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
4034                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
4035   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
4036                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
4037   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
4038                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
4039   verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
4040                "                   aaaaaaaaaaa aaaaa) const override;");
4041   verifyGoogleFormat(
4042       "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4043       "    const override;");
4044 
4045   // Even if the first parameter has to be wrapped.
4046   verifyFormat("void someLongFunction(\n"
4047                "    int someLongParameter) const {}",
4048                getLLVMStyleWithColumns(46));
4049   verifyFormat("void someLongFunction(\n"
4050                "    int someLongParameter) const {}",
4051                Style);
4052   verifyFormat("void someLongFunction(\n"
4053                "    int someLongParameter) override {}",
4054                Style);
4055   verifyFormat("void someLongFunction(\n"
4056                "    int someLongParameter) OVERRIDE {}",
4057                Style);
4058   verifyFormat("void someLongFunction(\n"
4059                "    int someLongParameter) final {}",
4060                Style);
4061   verifyFormat("void someLongFunction(\n"
4062                "    int someLongParameter) FINAL {}",
4063                Style);
4064   verifyFormat("void someLongFunction(\n"
4065                "    int parameter) const override {}",
4066                Style);
4067 
4068   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
4069   verifyFormat("void someLongFunction(\n"
4070                "    int someLongParameter) const\n"
4071                "{\n"
4072                "}",
4073                Style);
4074 
4075   // Unless these are unknown annotations.
4076   verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
4077                "                  aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4078                "    LONG_AND_UGLY_ANNOTATION;");
4079 
4080   // Breaking before function-like trailing annotations is fine to keep them
4081   // close to their arguments.
4082   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4083                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
4084   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
4085                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
4086   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
4087                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
4088   verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
4089                      "    AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
4090   verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
4091 
4092   verifyFormat(
4093       "void aaaaaaaaaaaaaaaaaa()\n"
4094       "    __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
4095       "                   aaaaaaaaaaaaaaaaaaaaaaaaa));");
4096   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4097                "    __attribute__((unused));");
4098   verifyGoogleFormat(
4099       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4100       "    GUARDED_BY(aaaaaaaaaaaa);");
4101   verifyGoogleFormat(
4102       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4103       "    GUARDED_BY(aaaaaaaaaaaa);");
4104   verifyGoogleFormat(
4105       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
4106       "    aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4107   verifyGoogleFormat(
4108       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
4109       "    aaaaaaaaaaaaaaaaaaaaaaaaa;");
4110 }
4111 
4112 TEST_F(FormatTest, FunctionAnnotations) {
4113   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
4114                "int OldFunction(const string &parameter) {}");
4115   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
4116                "string OldFunction(const string &parameter) {}");
4117   verifyFormat("template <typename T>\n"
4118                "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
4119                "string OldFunction(const string &parameter) {}");
4120 
4121   // Not function annotations.
4122   verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4123                "                << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
4124   verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
4125                "       ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
4126   verifyFormat("MACRO(abc).function() // wrap\n"
4127                "    << abc;");
4128   verifyFormat("MACRO(abc)->function() // wrap\n"
4129                "    << abc;");
4130   verifyFormat("MACRO(abc)::function() // wrap\n"
4131                "    << abc;");
4132 }
4133 
4134 TEST_F(FormatTest, BreaksDesireably) {
4135   verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
4136                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
4137                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
4138   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4139                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
4140                "}");
4141 
4142   verifyFormat(
4143       "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4144       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
4145 
4146   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4147                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4148                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4149 
4150   verifyFormat(
4151       "aaaaaaaa(aaaaaaaaaaaaa,\n"
4152       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4153       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
4154       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4155       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
4156 
4157   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
4158                "    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4159 
4160   verifyFormat(
4161       "void f() {\n"
4162       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
4163       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
4164       "}");
4165   verifyFormat(
4166       "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4167       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4168   verifyFormat(
4169       "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4170       "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4171   verifyFormat(
4172       "aaaaaa(aaa,\n"
4173       "       new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4174       "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4175       "       aaaa);");
4176   verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4177                "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4178                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4179 
4180   // Indent consistently independent of call expression and unary operator.
4181   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
4182                "    dddddddddddddddddddddddddddddd));");
4183   verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
4184                "    dddddddddddddddddddddddddddddd));");
4185   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
4186                "    dddddddddddddddddddddddddddddd));");
4187 
4188   // This test case breaks on an incorrect memoization, i.e. an optimization not
4189   // taking into account the StopAt value.
4190   verifyFormat(
4191       "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
4192       "       aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
4193       "       aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
4194       "       (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4195 
4196   verifyFormat("{\n  {\n    {\n"
4197                "      Annotation.SpaceRequiredBefore =\n"
4198                "          Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
4199                "          Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
4200                "    }\n  }\n}");
4201 
4202   // Break on an outer level if there was a break on an inner level.
4203   EXPECT_EQ("f(g(h(a, // comment\n"
4204             "      b, c),\n"
4205             "    d, e),\n"
4206             "  x, y);",
4207             format("f(g(h(a, // comment\n"
4208                    "    b, c), d, e), x, y);"));
4209 
4210   // Prefer breaking similar line breaks.
4211   verifyFormat(
4212       "const int kTrackingOptions = NSTrackingMouseMoved |\n"
4213       "                             NSTrackingMouseEnteredAndExited |\n"
4214       "                             NSTrackingActiveAlways;");
4215 }
4216 
4217 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
4218   FormatStyle NoBinPacking = getGoogleStyle();
4219   NoBinPacking.BinPackParameters = false;
4220   NoBinPacking.BinPackArguments = true;
4221   verifyFormat("void f() {\n"
4222                "  f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
4223                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
4224                "}",
4225                NoBinPacking);
4226   verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
4227                "       int aaaaaaaaaaaaaaaaaaaa,\n"
4228                "       int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4229                NoBinPacking);
4230 
4231   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
4232   verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4233                "                        vector<int> bbbbbbbbbbbbbbb);",
4234                NoBinPacking);
4235   // FIXME: This behavior difference is probably not wanted. However, currently
4236   // we cannot distinguish BreakBeforeParameter being set because of the wrapped
4237   // template arguments from BreakBeforeParameter being set because of the
4238   // one-per-line formatting.
4239   verifyFormat(
4240       "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n"
4241       "                                             aaaaaaaaaa> aaaaaaaaaa);",
4242       NoBinPacking);
4243   verifyFormat(
4244       "void fffffffffff(\n"
4245       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n"
4246       "        aaaaaaaaaa);");
4247 }
4248 
4249 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
4250   FormatStyle NoBinPacking = getGoogleStyle();
4251   NoBinPacking.BinPackParameters = false;
4252   NoBinPacking.BinPackArguments = false;
4253   verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
4254                "  aaaaaaaaaaaaaaaaaaaa,\n"
4255                "  aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
4256                NoBinPacking);
4257   verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
4258                "        aaaaaaaaaaaaa,\n"
4259                "        aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
4260                NoBinPacking);
4261   verifyFormat(
4262       "aaaaaaaa(aaaaaaaaaaaaa,\n"
4263       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4264       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
4265       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4266       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
4267       NoBinPacking);
4268   verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
4269                "    .aaaaaaaaaaaaaaaaaa();",
4270                NoBinPacking);
4271   verifyFormat("void f() {\n"
4272                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4273                "      aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
4274                "}",
4275                NoBinPacking);
4276 
4277   verifyFormat(
4278       "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4279       "             aaaaaaaaaaaa,\n"
4280       "             aaaaaaaaaaaa);",
4281       NoBinPacking);
4282   verifyFormat(
4283       "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
4284       "                               ddddddddddddddddddddddddddddd),\n"
4285       "             test);",
4286       NoBinPacking);
4287 
4288   verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
4289                "            aaaaaaaaaaaaaaaaaaaaaaa,\n"
4290                "            aaaaaaaaaaaaaaaaaaaaaaa>\n"
4291                "    aaaaaaaaaaaaaaaaaa;",
4292                NoBinPacking);
4293   verifyFormat("a(\"a\"\n"
4294                "  \"a\",\n"
4295                "  a);");
4296 
4297   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
4298   verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
4299                "                aaaaaaaaa,\n"
4300                "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4301                NoBinPacking);
4302   verifyFormat(
4303       "void f() {\n"
4304       "  aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
4305       "      .aaaaaaa();\n"
4306       "}",
4307       NoBinPacking);
4308   verifyFormat(
4309       "template <class SomeType, class SomeOtherType>\n"
4310       "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
4311       NoBinPacking);
4312 }
4313 
4314 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
4315   FormatStyle Style = getLLVMStyleWithColumns(15);
4316   Style.ExperimentalAutoDetectBinPacking = true;
4317   EXPECT_EQ("aaa(aaaa,\n"
4318             "    aaaa,\n"
4319             "    aaaa);\n"
4320             "aaa(aaaa,\n"
4321             "    aaaa,\n"
4322             "    aaaa);",
4323             format("aaa(aaaa,\n" // one-per-line
4324                    "  aaaa,\n"
4325                    "    aaaa  );\n"
4326                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
4327                    Style));
4328   EXPECT_EQ("aaa(aaaa, aaaa,\n"
4329             "    aaaa);\n"
4330             "aaa(aaaa, aaaa,\n"
4331             "    aaaa);",
4332             format("aaa(aaaa,  aaaa,\n" // bin-packed
4333                    "    aaaa  );\n"
4334                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
4335                    Style));
4336 }
4337 
4338 TEST_F(FormatTest, FormatsBuilderPattern) {
4339   verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
4340                "    .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
4341                "    .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
4342                "    .StartsWith(\".init\", ORDER_INIT)\n"
4343                "    .StartsWith(\".fini\", ORDER_FINI)\n"
4344                "    .StartsWith(\".hash\", ORDER_HASH)\n"
4345                "    .Default(ORDER_TEXT);\n");
4346 
4347   verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
4348                "       aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
4349   verifyFormat(
4350       "aaaaaaa->aaaaaaa\n"
4351       "    ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4352       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4353       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
4354   verifyFormat(
4355       "aaaaaaa->aaaaaaa\n"
4356       "    ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4357       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
4358   verifyFormat(
4359       "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
4360       "    aaaaaaaaaaaaaa);");
4361   verifyFormat(
4362       "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
4363       "    aaaaaa->aaaaaaaaaaaa()\n"
4364       "        ->aaaaaaaaaaaaaaaa(\n"
4365       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4366       "        ->aaaaaaaaaaaaaaaaa();");
4367   verifyGoogleFormat(
4368       "void f() {\n"
4369       "  someo->Add((new util::filetools::Handler(dir))\n"
4370       "                 ->OnEvent1(NewPermanentCallback(\n"
4371       "                     this, &HandlerHolderClass::EventHandlerCBA))\n"
4372       "                 ->OnEvent2(NewPermanentCallback(\n"
4373       "                     this, &HandlerHolderClass::EventHandlerCBB))\n"
4374       "                 ->OnEvent3(NewPermanentCallback(\n"
4375       "                     this, &HandlerHolderClass::EventHandlerCBC))\n"
4376       "                 ->OnEvent5(NewPermanentCallback(\n"
4377       "                     this, &HandlerHolderClass::EventHandlerCBD))\n"
4378       "                 ->OnEvent6(NewPermanentCallback(\n"
4379       "                     this, &HandlerHolderClass::EventHandlerCBE)));\n"
4380       "}");
4381 
4382   verifyFormat(
4383       "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
4384   verifyFormat("aaaaaaaaaaaaaaa()\n"
4385                "    .aaaaaaaaaaaaaaa()\n"
4386                "    .aaaaaaaaaaaaaaa()\n"
4387                "    .aaaaaaaaaaaaaaa()\n"
4388                "    .aaaaaaaaaaaaaaa();");
4389   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4390                "    .aaaaaaaaaaaaaaa()\n"
4391                "    .aaaaaaaaaaaaaaa()\n"
4392                "    .aaaaaaaaaaaaaaa();");
4393   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4394                "    .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4395                "    .aaaaaaaaaaaaaaa();");
4396   verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
4397                "    ->aaaaaaaaaaaaaae(0)\n"
4398                "    ->aaaaaaaaaaaaaaa();");
4399 
4400   // Don't linewrap after very short segments.
4401   verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4402                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4403                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4404   verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4405                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4406                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4407   verifyFormat("aaa()\n"
4408                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4409                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4410                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4411 
4412   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
4413                "    .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4414                "    .has<bbbbbbbbbbbbbbbbbbbbb>();");
4415   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
4416                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
4417                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
4418 
4419   // Prefer not to break after empty parentheses.
4420   verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
4421                "    First->LastNewlineOffset);");
4422 
4423   // Prefer not to create "hanging" indents.
4424   verifyFormat(
4425       "return !soooooooooooooome_map\n"
4426       "            .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4427       "            .second;");
4428   verifyFormat(
4429       "return aaaaaaaaaaaaaaaa\n"
4430       "    .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n"
4431       "    .aaaa(aaaaaaaaaaaaaa);");
4432   // No hanging indent here.
4433   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n"
4434                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4435   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n"
4436                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4437   verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
4438                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4439                getLLVMStyleWithColumns(60));
4440   verifyFormat("aaaaaaaaaaaaaaaaaa\n"
4441                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
4442                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4443                getLLVMStyleWithColumns(59));
4444   verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4445                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4446                "    .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4447 }
4448 
4449 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
4450   verifyFormat(
4451       "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
4452       "    bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
4453   verifyFormat(
4454       "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
4455       "    bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
4456 
4457   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
4458                "    ccccccccccccccccccccccccc) {\n}");
4459   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
4460                "    ccccccccccccccccccccccccc) {\n}");
4461 
4462   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
4463                "    ccccccccccccccccccccccccc) {\n}");
4464   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
4465                "    ccccccccccccccccccccccccc) {\n}");
4466 
4467   verifyFormat(
4468       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
4469       "    ccccccccccccccccccccccccc) {\n}");
4470   verifyFormat(
4471       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
4472       "    ccccccccccccccccccccccccc) {\n}");
4473 
4474   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
4475                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
4476                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
4477                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
4478   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
4479                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
4480                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
4481                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
4482 
4483   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
4484                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
4485                "    aaaaaaaaaaaaaaa != aa) {\n}");
4486   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
4487                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
4488                "    aaaaaaaaaaaaaaa != aa) {\n}");
4489 }
4490 
4491 TEST_F(FormatTest, BreaksAfterAssignments) {
4492   verifyFormat(
4493       "unsigned Cost =\n"
4494       "    TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
4495       "                        SI->getPointerAddressSpaceee());\n");
4496   verifyFormat(
4497       "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
4498       "    Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
4499 
4500   verifyFormat(
4501       "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
4502       "    aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
4503   verifyFormat("unsigned OriginalStartColumn =\n"
4504                "    SourceMgr.getSpellingColumnNumber(\n"
4505                "        Current.FormatTok.getStartOfNonWhitespace()) -\n"
4506                "    1;");
4507 }
4508 
4509 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) {
4510   FormatStyle Style = getLLVMStyle();
4511   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4512                "    bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;",
4513                Style);
4514 
4515   Style.PenaltyBreakAssignment = 20;
4516   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
4517                "                                 cccccccccccccccccccccccccc;",
4518                Style);
4519 }
4520 
4521 TEST_F(FormatTest, AlignsAfterAssignments) {
4522   verifyFormat(
4523       "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4524       "             aaaaaaaaaaaaaaaaaaaaaaaaa;");
4525   verifyFormat(
4526       "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4527       "          aaaaaaaaaaaaaaaaaaaaaaaaa;");
4528   verifyFormat(
4529       "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4530       "           aaaaaaaaaaaaaaaaaaaaaaaaa;");
4531   verifyFormat(
4532       "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4533       "              aaaaaaaaaaaaaaaaaaaaaaaaa);");
4534   verifyFormat(
4535       "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
4536       "                                            aaaaaaaaaaaaaaaaaaaaaaaa +\n"
4537       "                                            aaaaaaaaaaaaaaaaaaaaaaaa;");
4538 }
4539 
4540 TEST_F(FormatTest, AlignsAfterReturn) {
4541   verifyFormat(
4542       "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4543       "       aaaaaaaaaaaaaaaaaaaaaaaaa;");
4544   verifyFormat(
4545       "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4546       "        aaaaaaaaaaaaaaaaaaaaaaaaa);");
4547   verifyFormat(
4548       "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
4549       "       aaaaaaaaaaaaaaaaaaaaaa();");
4550   verifyFormat(
4551       "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
4552       "        aaaaaaaaaaaaaaaaaaaaaa());");
4553   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4554                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4555   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4556                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
4557                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4558   verifyFormat("return\n"
4559                "    // true if code is one of a or b.\n"
4560                "    code == a || code == b;");
4561 }
4562 
4563 TEST_F(FormatTest, AlignsAfterOpenBracket) {
4564   verifyFormat(
4565       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
4566       "                                                aaaaaaaaa aaaaaaa) {}");
4567   verifyFormat(
4568       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
4569       "                                               aaaaaaaaaaa aaaaaaaaa);");
4570   verifyFormat(
4571       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
4572       "                                             aaaaaaaaaaaaaaaaaaaaa));");
4573   FormatStyle Style = getLLVMStyle();
4574   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4575   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4576                "    aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
4577                Style);
4578   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
4579                "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
4580                Style);
4581   verifyFormat("SomeLongVariableName->someFunction(\n"
4582                "    foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
4583                Style);
4584   verifyFormat(
4585       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
4586       "    aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4587       Style);
4588   verifyFormat(
4589       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
4590       "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4591       Style);
4592   verifyFormat(
4593       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
4594       "    aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
4595       Style);
4596 
4597   verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n"
4598                "    ccccccc(aaaaaaaaaaaaaaaaa,         //\n"
4599                "        b));",
4600                Style);
4601 
4602   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
4603   Style.BinPackArguments = false;
4604   Style.BinPackParameters = false;
4605   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4606                "    aaaaaaaaaaa aaaaaaaa,\n"
4607                "    aaaaaaaaa aaaaaaa,\n"
4608                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4609                Style);
4610   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
4611                "    aaaaaaaaaaa aaaaaaaaa,\n"
4612                "    aaaaaaaaaaa aaaaaaaaa,\n"
4613                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4614                Style);
4615   verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
4616                "    aaaaaaaaaaaaaaa,\n"
4617                "    aaaaaaaaaaaaaaaaaaaaa,\n"
4618                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
4619                Style);
4620   verifyFormat(
4621       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
4622       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
4623       Style);
4624   verifyFormat(
4625       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
4626       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
4627       Style);
4628   verifyFormat(
4629       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
4630       "    aaaaaaaaaaaaaaaaaaaaa(\n"
4631       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n"
4632       "    aaaaaaaaaaaaaaaa);",
4633       Style);
4634   verifyFormat(
4635       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
4636       "    aaaaaaaaaaaaaaaaaaaaa(\n"
4637       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n"
4638       "    aaaaaaaaaaaaaaaa);",
4639       Style);
4640 }
4641 
4642 TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
4643   FormatStyle Style = getLLVMStyleWithColumns(40);
4644   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4645                "          bbbbbbbbbbbbbbbbbbbbbb);",
4646                Style);
4647   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
4648   Style.AlignOperands = false;
4649   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4650                "          bbbbbbbbbbbbbbbbbbbbbb);",
4651                Style);
4652   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4653   Style.AlignOperands = true;
4654   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4655                "          bbbbbbbbbbbbbbbbbbbbbb);",
4656                Style);
4657   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4658   Style.AlignOperands = false;
4659   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4660                "    bbbbbbbbbbbbbbbbbbbbbb);",
4661                Style);
4662 }
4663 
4664 TEST_F(FormatTest, BreaksConditionalExpressions) {
4665   verifyFormat(
4666       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4667       "                               ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4668       "                               : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4669   verifyFormat(
4670       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
4671       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4672       "                                : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4673   verifyFormat(
4674       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4675       "                                   : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4676   verifyFormat(
4677       "aaaa(aaaaaaaaa, aaaaaaaaa,\n"
4678       "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4679       "             : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4680   verifyFormat(
4681       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
4682       "                                                    : aaaaaaaaaaaaa);");
4683   verifyFormat(
4684       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4685       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4686       "                                    : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4687       "                   aaaaaaaaaaaaa);");
4688   verifyFormat(
4689       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4690       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4691       "                   aaaaaaaaaaaaa);");
4692   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4693                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4694                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4695                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4696                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4697   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4698                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4699                "           ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4700                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4701                "           : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4702                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4703                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4704   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4705                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4706                "           ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4707                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4708                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4709   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4710                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4711                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4712   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
4713                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4714                "        ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4715                "        : aaaaaaaaaaaaaaaa;");
4716   verifyFormat(
4717       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4718       "    ? aaaaaaaaaaaaaaa\n"
4719       "    : aaaaaaaaaaaaaaa;");
4720   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
4721                "          aaaaaaaaa\n"
4722                "      ? b\n"
4723                "      : c);");
4724   verifyFormat("return aaaa == bbbb\n"
4725                "           // comment\n"
4726                "           ? aaaa\n"
4727                "           : bbbb;");
4728   verifyFormat("unsigned Indent =\n"
4729                "    format(TheLine.First,\n"
4730                "           IndentForLevel[TheLine.Level] >= 0\n"
4731                "               ? IndentForLevel[TheLine.Level]\n"
4732                "               : TheLine * 2,\n"
4733                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
4734                getLLVMStyleWithColumns(60));
4735   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
4736                "                  ? aaaaaaaaaaaaaaa\n"
4737                "                  : bbbbbbbbbbbbbbb //\n"
4738                "                        ? ccccccccccccccc\n"
4739                "                        : ddddddddddddddd;");
4740   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
4741                "                  ? aaaaaaaaaaaaaaa\n"
4742                "                  : (bbbbbbbbbbbbbbb //\n"
4743                "                         ? ccccccccccccccc\n"
4744                "                         : ddddddddddddddd);");
4745   verifyFormat(
4746       "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4747       "                                      ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4748       "                                            aaaaaaaaaaaaaaaaaaaaa +\n"
4749       "                                            aaaaaaaaaaaaaaaaaaaaa\n"
4750       "                                      : aaaaaaaaaa;");
4751   verifyFormat(
4752       "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4753       "                                   : aaaaaaaaaaaaaaaaaaaaaa\n"
4754       "                      : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4755 
4756   FormatStyle NoBinPacking = getLLVMStyle();
4757   NoBinPacking.BinPackArguments = false;
4758   verifyFormat(
4759       "void f() {\n"
4760       "  g(aaa,\n"
4761       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
4762       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4763       "        ? aaaaaaaaaaaaaaa\n"
4764       "        : aaaaaaaaaaaaaaa);\n"
4765       "}",
4766       NoBinPacking);
4767   verifyFormat(
4768       "void f() {\n"
4769       "  g(aaa,\n"
4770       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
4771       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4772       "        ?: aaaaaaaaaaaaaaa);\n"
4773       "}",
4774       NoBinPacking);
4775 
4776   verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
4777                "             // comment.\n"
4778                "             ccccccccccccccccccccccccccccccccccccccc\n"
4779                "                 ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4780                "                 : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
4781 
4782   // Assignments in conditional expressions. Apparently not uncommon :-(.
4783   verifyFormat("return a != b\n"
4784                "           // comment\n"
4785                "           ? a = b\n"
4786                "           : a = b;");
4787   verifyFormat("return a != b\n"
4788                "           // comment\n"
4789                "           ? a = a != b\n"
4790                "                     // comment\n"
4791                "                     ? a = b\n"
4792                "                     : a\n"
4793                "           : a;\n");
4794   verifyFormat("return a != b\n"
4795                "           // comment\n"
4796                "           ? a\n"
4797                "           : a = a != b\n"
4798                "                     // comment\n"
4799                "                     ? a = b\n"
4800                "                     : a;");
4801 }
4802 
4803 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
4804   FormatStyle Style = getLLVMStyle();
4805   Style.BreakBeforeTernaryOperators = false;
4806   Style.ColumnLimit = 70;
4807   verifyFormat(
4808       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4809       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4810       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4811       Style);
4812   verifyFormat(
4813       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
4814       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4815       "                                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4816       Style);
4817   verifyFormat(
4818       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4819       "                                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4820       Style);
4821   verifyFormat(
4822       "aaaa(aaaaaaaa, aaaaaaaaaa,\n"
4823       "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4824       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4825       Style);
4826   verifyFormat(
4827       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
4828       "                                                      aaaaaaaaaaaaa);",
4829       Style);
4830   verifyFormat(
4831       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4832       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4833       "                                      aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4834       "                   aaaaaaaaaaaaa);",
4835       Style);
4836   verifyFormat(
4837       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4838       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4839       "                   aaaaaaaaaaaaa);",
4840       Style);
4841   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4842                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4843                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
4844                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4845                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4846                Style);
4847   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4848                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4849                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4850                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
4851                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4852                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4853                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4854                Style);
4855   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4856                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
4857                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4858                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4859                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4860                Style);
4861   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4862                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4863                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4864                Style);
4865   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
4866                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4867                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4868                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4869                Style);
4870   verifyFormat(
4871       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4872       "    aaaaaaaaaaaaaaa :\n"
4873       "    aaaaaaaaaaaaaaa;",
4874       Style);
4875   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
4876                "          aaaaaaaaa ?\n"
4877                "      b :\n"
4878                "      c);",
4879                Style);
4880   verifyFormat("unsigned Indent =\n"
4881                "    format(TheLine.First,\n"
4882                "           IndentForLevel[TheLine.Level] >= 0 ?\n"
4883                "               IndentForLevel[TheLine.Level] :\n"
4884                "               TheLine * 2,\n"
4885                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
4886                Style);
4887   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
4888                "                  aaaaaaaaaaaaaaa :\n"
4889                "                  bbbbbbbbbbbbbbb ? //\n"
4890                "                      ccccccccccccccc :\n"
4891                "                      ddddddddddddddd;",
4892                Style);
4893   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
4894                "                  aaaaaaaaaaaaaaa :\n"
4895                "                  (bbbbbbbbbbbbbbb ? //\n"
4896                "                       ccccccccccccccc :\n"
4897                "                       ddddddddddddddd);",
4898                Style);
4899   verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4900                "            /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n"
4901                "            ccccccccccccccccccccccccccc;",
4902                Style);
4903   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4904                "           aaaaa :\n"
4905                "           bbbbbbbbbbbbbbb + cccccccccccccccc;",
4906                Style);
4907 }
4908 
4909 TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
4910   verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
4911                "     aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
4912   verifyFormat("bool a = true, b = false;");
4913 
4914   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4915                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
4916                "     bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
4917                "         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
4918   verifyFormat(
4919       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
4920       "         bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
4921       "     d = e && f;");
4922   verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
4923                "          c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
4924   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
4925                "          *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
4926   verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
4927                "          ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
4928 
4929   FormatStyle Style = getGoogleStyle();
4930   Style.PointerAlignment = FormatStyle::PAS_Left;
4931   Style.DerivePointerAlignment = false;
4932   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4933                "    *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
4934                "    *b = bbbbbbbbbbbbbbbbbbb;",
4935                Style);
4936   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
4937                "          *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
4938                Style);
4939   verifyFormat("vector<int*> a, b;", Style);
4940   verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style);
4941 }
4942 
4943 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
4944   verifyFormat("arr[foo ? bar : baz];");
4945   verifyFormat("f()[foo ? bar : baz];");
4946   verifyFormat("(a + b)[foo ? bar : baz];");
4947   verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
4948 }
4949 
4950 TEST_F(FormatTest, AlignsStringLiterals) {
4951   verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
4952                "                                      \"short literal\");");
4953   verifyFormat(
4954       "looooooooooooooooooooooooongFunction(\n"
4955       "    \"short literal\"\n"
4956       "    \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
4957   verifyFormat("someFunction(\"Always break between multi-line\"\n"
4958                "             \" string literals\",\n"
4959                "             and, other, parameters);");
4960   EXPECT_EQ("fun + \"1243\" /* comment */\n"
4961             "      \"5678\";",
4962             format("fun + \"1243\" /* comment */\n"
4963                    "    \"5678\";",
4964                    getLLVMStyleWithColumns(28)));
4965   EXPECT_EQ(
4966       "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
4967       "         \"aaaaaaaaaaaaaaaaaaaaa\"\n"
4968       "         \"aaaaaaaaaaaaaaaa\";",
4969       format("aaaaaa ="
4970              "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
4971              "aaaaaaaaaaaaaaaaaaaaa\" "
4972              "\"aaaaaaaaaaaaaaaa\";"));
4973   verifyFormat("a = a + \"a\"\n"
4974                "        \"a\"\n"
4975                "        \"a\";");
4976   verifyFormat("f(\"a\", \"b\"\n"
4977                "       \"c\");");
4978 
4979   verifyFormat(
4980       "#define LL_FORMAT \"ll\"\n"
4981       "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
4982       "       \"d, ddddddddd: %\" LL_FORMAT \"d\");");
4983 
4984   verifyFormat("#define A(X)          \\\n"
4985                "  \"aaaaa\" #X \"bbbbbb\" \\\n"
4986                "  \"ccccc\"",
4987                getLLVMStyleWithColumns(23));
4988   verifyFormat("#define A \"def\"\n"
4989                "f(\"abc\" A \"ghi\"\n"
4990                "  \"jkl\");");
4991 
4992   verifyFormat("f(L\"a\"\n"
4993                "  L\"b\");");
4994   verifyFormat("#define A(X)            \\\n"
4995                "  L\"aaaaa\" #X L\"bbbbbb\" \\\n"
4996                "  L\"ccccc\"",
4997                getLLVMStyleWithColumns(25));
4998 
4999   verifyFormat("f(@\"a\"\n"
5000                "  @\"b\");");
5001   verifyFormat("NSString s = @\"a\"\n"
5002                "             @\"b\"\n"
5003                "             @\"c\";");
5004   verifyFormat("NSString s = @\"a\"\n"
5005                "              \"b\"\n"
5006                "              \"c\";");
5007 }
5008 
5009 TEST_F(FormatTest, ReturnTypeBreakingStyle) {
5010   FormatStyle Style = getLLVMStyle();
5011   // No declarations or definitions should be moved to own line.
5012   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None;
5013   verifyFormat("class A {\n"
5014                "  int f() { return 1; }\n"
5015                "  int g();\n"
5016                "};\n"
5017                "int f() { return 1; }\n"
5018                "int g();\n",
5019                Style);
5020 
5021   // All declarations and definitions should have the return type moved to its
5022   // own
5023   // line.
5024   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
5025   verifyFormat("class E {\n"
5026                "  int\n"
5027                "  f() {\n"
5028                "    return 1;\n"
5029                "  }\n"
5030                "  int\n"
5031                "  g();\n"
5032                "};\n"
5033                "int\n"
5034                "f() {\n"
5035                "  return 1;\n"
5036                "}\n"
5037                "int\n"
5038                "g();\n",
5039                Style);
5040 
5041   // Top-level definitions, and no kinds of declarations should have the
5042   // return type moved to its own line.
5043   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions;
5044   verifyFormat("class B {\n"
5045                "  int f() { return 1; }\n"
5046                "  int g();\n"
5047                "};\n"
5048                "int\n"
5049                "f() {\n"
5050                "  return 1;\n"
5051                "}\n"
5052                "int g();\n",
5053                Style);
5054 
5055   // Top-level definitions and declarations should have the return type moved
5056   // to its own line.
5057   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel;
5058   verifyFormat("class C {\n"
5059                "  int f() { return 1; }\n"
5060                "  int g();\n"
5061                "};\n"
5062                "int\n"
5063                "f() {\n"
5064                "  return 1;\n"
5065                "}\n"
5066                "int\n"
5067                "g();\n",
5068                Style);
5069 
5070   // All definitions should have the return type moved to its own line, but no
5071   // kinds of declarations.
5072   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
5073   verifyFormat("class D {\n"
5074                "  int\n"
5075                "  f() {\n"
5076                "    return 1;\n"
5077                "  }\n"
5078                "  int g();\n"
5079                "};\n"
5080                "int\n"
5081                "f() {\n"
5082                "  return 1;\n"
5083                "}\n"
5084                "int g();\n",
5085                Style);
5086   verifyFormat("const char *\n"
5087                "f(void) {\n" // Break here.
5088                "  return \"\";\n"
5089                "}\n"
5090                "const char *bar(void);\n", // No break here.
5091                Style);
5092   verifyFormat("template <class T>\n"
5093                "T *\n"
5094                "f(T &c) {\n" // Break here.
5095                "  return NULL;\n"
5096                "}\n"
5097                "template <class T> T *f(T &c);\n", // No break here.
5098                Style);
5099   verifyFormat("class C {\n"
5100                "  int\n"
5101                "  operator+() {\n"
5102                "    return 1;\n"
5103                "  }\n"
5104                "  int\n"
5105                "  operator()() {\n"
5106                "    return 1;\n"
5107                "  }\n"
5108                "};\n",
5109                Style);
5110   verifyFormat("void\n"
5111                "A::operator()() {}\n"
5112                "void\n"
5113                "A::operator>>() {}\n"
5114                "void\n"
5115                "A::operator+() {}\n",
5116                Style);
5117   verifyFormat("void *operator new(std::size_t s);", // No break here.
5118                Style);
5119   verifyFormat("void *\n"
5120                "operator new(std::size_t s) {}",
5121                Style);
5122   verifyFormat("void *\n"
5123                "operator delete[](void *ptr) {}",
5124                Style);
5125   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
5126   verifyFormat("const char *\n"
5127                "f(void)\n" // Break here.
5128                "{\n"
5129                "  return \"\";\n"
5130                "}\n"
5131                "const char *bar(void);\n", // No break here.
5132                Style);
5133   verifyFormat("template <class T>\n"
5134                "T *\n"     // Problem here: no line break
5135                "f(T &c)\n" // Break here.
5136                "{\n"
5137                "  return NULL;\n"
5138                "}\n"
5139                "template <class T> T *f(T &c);\n", // No break here.
5140                Style);
5141 }
5142 
5143 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
5144   FormatStyle NoBreak = getLLVMStyle();
5145   NoBreak.AlwaysBreakBeforeMultilineStrings = false;
5146   FormatStyle Break = getLLVMStyle();
5147   Break.AlwaysBreakBeforeMultilineStrings = true;
5148   verifyFormat("aaaa = \"bbbb\"\n"
5149                "       \"cccc\";",
5150                NoBreak);
5151   verifyFormat("aaaa =\n"
5152                "    \"bbbb\"\n"
5153                "    \"cccc\";",
5154                Break);
5155   verifyFormat("aaaa(\"bbbb\"\n"
5156                "     \"cccc\");",
5157                NoBreak);
5158   verifyFormat("aaaa(\n"
5159                "    \"bbbb\"\n"
5160                "    \"cccc\");",
5161                Break);
5162   verifyFormat("aaaa(qqq, \"bbbb\"\n"
5163                "          \"cccc\");",
5164                NoBreak);
5165   verifyFormat("aaaa(qqq,\n"
5166                "     \"bbbb\"\n"
5167                "     \"cccc\");",
5168                Break);
5169   verifyFormat("aaaa(qqq,\n"
5170                "     L\"bbbb\"\n"
5171                "     L\"cccc\");",
5172                Break);
5173   verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
5174                "                      \"bbbb\"));",
5175                Break);
5176   verifyFormat("string s = someFunction(\n"
5177                "    \"abc\"\n"
5178                "    \"abc\");",
5179                Break);
5180 
5181   // As we break before unary operators, breaking right after them is bad.
5182   verifyFormat("string foo = abc ? \"x\"\n"
5183                "                   \"blah blah blah blah blah blah\"\n"
5184                "                 : \"y\";",
5185                Break);
5186 
5187   // Don't break if there is no column gain.
5188   verifyFormat("f(\"aaaa\"\n"
5189                "  \"bbbb\");",
5190                Break);
5191 
5192   // Treat literals with escaped newlines like multi-line string literals.
5193   EXPECT_EQ("x = \"a\\\n"
5194             "b\\\n"
5195             "c\";",
5196             format("x = \"a\\\n"
5197                    "b\\\n"
5198                    "c\";",
5199                    NoBreak));
5200   EXPECT_EQ("xxxx =\n"
5201             "    \"a\\\n"
5202             "b\\\n"
5203             "c\";",
5204             format("xxxx = \"a\\\n"
5205                    "b\\\n"
5206                    "c\";",
5207                    Break));
5208 
5209   EXPECT_EQ("NSString *const kString =\n"
5210             "    @\"aaaa\"\n"
5211             "    @\"bbbb\";",
5212             format("NSString *const kString = @\"aaaa\"\n"
5213                    "@\"bbbb\";",
5214                    Break));
5215 
5216   Break.ColumnLimit = 0;
5217   verifyFormat("const char *hello = \"hello llvm\";", Break);
5218 }
5219 
5220 TEST_F(FormatTest, AlignsPipes) {
5221   verifyFormat(
5222       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5223       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5224       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5225   verifyFormat(
5226       "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
5227       "                     << aaaaaaaaaaaaaaaaaaaa;");
5228   verifyFormat(
5229       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5230       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5231   verifyFormat(
5232       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
5233       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5234   verifyFormat(
5235       "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
5236       "                \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
5237       "             << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
5238   verifyFormat(
5239       "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5240       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5241       "         << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5242   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5243                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5244                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5245                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
5246   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
5247                "             << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
5248   verifyFormat(
5249       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5250       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5251   verifyFormat(
5252       "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n"
5253       "                                       aaaaaaaaaaaaaaaaaaaaaaaaaa);");
5254 
5255   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
5256                "             << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
5257   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5258                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5259                "                    aaaaaaaaaaaaaaaaaaaaa)\n"
5260                "             << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
5261   verifyFormat("LOG_IF(aaa == //\n"
5262                "       bbb)\n"
5263                "    << a << b;");
5264 
5265   // But sometimes, breaking before the first "<<" is desirable.
5266   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
5267                "    << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
5268   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
5269                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5270                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5271   verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
5272                "    << BEF << IsTemplate << Description << E->getType();");
5273   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
5274                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5275                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5276   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
5277                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5278                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5279                "    << aaa;");
5280 
5281   verifyFormat(
5282       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5283       "                    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5284 
5285   // Incomplete string literal.
5286   EXPECT_EQ("llvm::errs() << \"\n"
5287             "             << a;",
5288             format("llvm::errs() << \"\n<<a;"));
5289 
5290   verifyFormat("void f() {\n"
5291                "  CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
5292                "      << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
5293                "}");
5294 
5295   // Handle 'endl'.
5296   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
5297                "             << bbbbbbbbbbbbbbbbbbbbbb << endl;");
5298   verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
5299 
5300   // Handle '\n'.
5301   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n"
5302                "             << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
5303   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n"
5304                "             << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';");
5305   verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n"
5306                "             << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";");
5307   verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
5308 }
5309 
5310 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) {
5311   verifyFormat("return out << \"somepacket = {\\n\"\n"
5312                "           << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
5313                "           << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
5314                "           << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
5315                "           << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
5316                "           << \"}\";");
5317 
5318   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
5319                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
5320                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
5321   verifyFormat(
5322       "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
5323       "             << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
5324       "             << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
5325       "             << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
5326       "             << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
5327   verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
5328                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
5329   verifyFormat(
5330       "void f() {\n"
5331       "  llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
5332       "               << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
5333       "}");
5334 
5335   // Breaking before the first "<<" is generally not desirable.
5336   verifyFormat(
5337       "llvm::errs()\n"
5338       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5339       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5340       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5341       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
5342       getLLVMStyleWithColumns(70));
5343   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
5344                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5345                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
5346                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5347                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
5348                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
5349                getLLVMStyleWithColumns(70));
5350 
5351   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
5352                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
5353                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;");
5354   verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
5355                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
5356                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);");
5357   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n"
5358                "           (aaaa + aaaa);",
5359                getLLVMStyleWithColumns(40));
5360   verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n"
5361                "                  (aaaaaaa + aaaaa));",
5362                getLLVMStyleWithColumns(40));
5363   verifyFormat(
5364       "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n"
5365       "                  SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n"
5366       "                  bbbbbbbbbbbbbbbbbbbbbbb);");
5367 }
5368 
5369 TEST_F(FormatTest, UnderstandsEquals) {
5370   verifyFormat(
5371       "aaaaaaaaaaaaaaaaa =\n"
5372       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5373   verifyFormat(
5374       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5375       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
5376   verifyFormat(
5377       "if (a) {\n"
5378       "  f();\n"
5379       "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5380       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
5381       "}");
5382 
5383   verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5384                "        100000000 + 10000000) {\n}");
5385 }
5386 
5387 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
5388   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
5389                "    .looooooooooooooooooooooooooooooooooooooongFunction();");
5390 
5391   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
5392                "    ->looooooooooooooooooooooooooooooooooooooongFunction();");
5393 
5394   verifyFormat(
5395       "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
5396       "                                                          Parameter2);");
5397 
5398   verifyFormat(
5399       "ShortObject->shortFunction(\n"
5400       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
5401       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
5402 
5403   verifyFormat("loooooooooooooongFunction(\n"
5404                "    LoooooooooooooongObject->looooooooooooooooongFunction());");
5405 
5406   verifyFormat(
5407       "function(LoooooooooooooooooooooooooooooooooooongObject\n"
5408       "             ->loooooooooooooooooooooooooooooooooooooooongFunction());");
5409 
5410   verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
5411                "    .WillRepeatedly(Return(SomeValue));");
5412   verifyFormat("void f() {\n"
5413                "  EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
5414                "      .Times(2)\n"
5415                "      .WillRepeatedly(Return(SomeValue));\n"
5416                "}");
5417   verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
5418                "    ccccccccccccccccccccccc);");
5419   verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5420                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5421                "          .aaaaa(aaaaa),\n"
5422                "      aaaaaaaaaaaaaaaaaaaaa);");
5423   verifyFormat("void f() {\n"
5424                "  aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5425                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
5426                "}");
5427   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5428                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5429                "    .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5430                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5431                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
5432   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5433                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5434                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5435                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
5436                "}");
5437 
5438   // Here, it is not necessary to wrap at "." or "->".
5439   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
5440                "    aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
5441   verifyFormat(
5442       "aaaaaaaaaaa->aaaaaaaaa(\n"
5443       "    aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5444       "    aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
5445 
5446   verifyFormat(
5447       "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5448       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
5449   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
5450                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
5451   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
5452                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
5453 
5454   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5455                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5456                "    .a();");
5457 
5458   FormatStyle NoBinPacking = getLLVMStyle();
5459   NoBinPacking.BinPackParameters = false;
5460   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
5461                "    .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
5462                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
5463                "                         aaaaaaaaaaaaaaaaaaa,\n"
5464                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5465                NoBinPacking);
5466 
5467   // If there is a subsequent call, change to hanging indentation.
5468   verifyFormat(
5469       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5470       "                         aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
5471       "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5472   verifyFormat(
5473       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5474       "    aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
5475   verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5476                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5477                "                 .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5478   verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5479                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5480                "               .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
5481 }
5482 
5483 TEST_F(FormatTest, WrapsTemplateDeclarations) {
5484   verifyFormat("template <typename T>\n"
5485                "virtual void loooooooooooongFunction(int Param1, int Param2);");
5486   verifyFormat("template <typename T>\n"
5487                "// T should be one of {A, B}.\n"
5488                "virtual void loooooooooooongFunction(int Param1, int Param2);");
5489   verifyFormat(
5490       "template <typename T>\n"
5491       "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
5492   verifyFormat("template <typename T>\n"
5493                "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
5494                "       int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
5495   verifyFormat(
5496       "template <typename T>\n"
5497       "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
5498       "                                      int Paaaaaaaaaaaaaaaaaaaaram2);");
5499   verifyFormat(
5500       "template <typename T>\n"
5501       "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
5502       "                    aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
5503       "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5504   verifyFormat("template <typename T>\n"
5505                "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5506                "    int aaaaaaaaaaaaaaaaaaaaaa);");
5507   verifyFormat(
5508       "template <typename T1, typename T2 = char, typename T3 = char,\n"
5509       "          typename T4 = char>\n"
5510       "void f();");
5511   verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
5512                "          template <typename> class cccccccccccccccccccccc,\n"
5513                "          typename ddddddddddddd>\n"
5514                "class C {};");
5515   verifyFormat(
5516       "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
5517       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5518 
5519   verifyFormat("void f() {\n"
5520                "  a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
5521                "      a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
5522                "}");
5523 
5524   verifyFormat("template <typename T> class C {};");
5525   verifyFormat("template <typename T> void f();");
5526   verifyFormat("template <typename T> void f() {}");
5527   verifyFormat(
5528       "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
5529       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5530       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
5531       "    new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
5532       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5533       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
5534       "        bbbbbbbbbbbbbbbbbbbbbbbb);",
5535       getLLVMStyleWithColumns(72));
5536   EXPECT_EQ("static_cast<A< //\n"
5537             "    B> *>(\n"
5538             "\n"
5539             ");",
5540             format("static_cast<A<//\n"
5541                    "    B>*>(\n"
5542                    "\n"
5543                    "    );"));
5544   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5545                "    const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
5546 
5547   FormatStyle AlwaysBreak = getLLVMStyle();
5548   AlwaysBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
5549   verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
5550   verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
5551   verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
5552   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5553                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
5554                "    ccccccccccccccccccccccccccccccccccccccccccccccc);");
5555   verifyFormat("template <template <typename> class Fooooooo,\n"
5556                "          template <typename> class Baaaaaaar>\n"
5557                "struct C {};",
5558                AlwaysBreak);
5559   verifyFormat("template <typename T> // T can be A, B or C.\n"
5560                "struct C {};",
5561                AlwaysBreak);
5562   verifyFormat("template <enum E> class A {\n"
5563                "public:\n"
5564                "  E *f();\n"
5565                "};");
5566 
5567   FormatStyle NeverBreak = getLLVMStyle();
5568   NeverBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_No;
5569   verifyFormat("template <typename T> class C {};", NeverBreak);
5570   verifyFormat("template <typename T> void f();", NeverBreak);
5571   verifyFormat("template <typename T> void f() {}", NeverBreak);
5572   verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbb) {}",
5573                NeverBreak);
5574   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5575                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
5576                "    ccccccccccccccccccccccccccccccccccccccccccccccc);",
5577                NeverBreak);
5578   verifyFormat("template <template <typename> class Fooooooo,\n"
5579                "          template <typename> class Baaaaaaar>\n"
5580                "struct C {};",
5581                NeverBreak);
5582   verifyFormat("template <typename T> // T can be A, B or C.\n"
5583                "struct C {};",
5584                NeverBreak);
5585   verifyFormat("template <enum E> class A {\n"
5586                "public:\n"
5587                "  E *f();\n"
5588                "};", NeverBreak);
5589   NeverBreak.PenaltyBreakTemplateDeclaration = 100;
5590   verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbb) {}",
5591                NeverBreak);
5592 }
5593 
5594 TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) {
5595   FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
5596   Style.ColumnLimit = 60;
5597   EXPECT_EQ("// Baseline - no comments.\n"
5598             "template <\n"
5599             "    typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n"
5600             "void f() {}",
5601             format("// Baseline - no comments.\n"
5602                    "template <\n"
5603                    "    typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n"
5604                    "void f() {}",
5605                    Style));
5606 
5607   EXPECT_EQ("template <\n"
5608             "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  // trailing\n"
5609             "void f() {}",
5610             format("template <\n"
5611                    "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
5612                    "void f() {}",
5613                    Style));
5614 
5615   EXPECT_EQ(
5616       "template <\n"
5617       "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n"
5618       "void f() {}",
5619       format("template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  /* line */\n"
5620              "void f() {}",
5621              Style));
5622 
5623   EXPECT_EQ(
5624       "template <\n"
5625       "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  // trailing\n"
5626       "                                               // multiline\n"
5627       "void f() {}",
5628       format("template <\n"
5629              "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
5630              "                                              // multiline\n"
5631              "void f() {}",
5632              Style));
5633 
5634   EXPECT_EQ(
5635       "template <typename aaaaaaaaaa<\n"
5636       "    bbbbbbbbbbbb>::value>  // trailing loooong\n"
5637       "void f() {}",
5638       format(
5639           "template <\n"
5640           "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n"
5641           "void f() {}",
5642           Style));
5643 }
5644 
5645 TEST_F(FormatTest, WrapsTemplateParameters) {
5646   FormatStyle Style = getLLVMStyle();
5647   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
5648   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
5649   verifyFormat(
5650       "template <typename... a> struct q {};\n"
5651       "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
5652       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
5653       "    y;",
5654       Style);
5655   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
5656   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
5657   verifyFormat(
5658       "template <typename... a> struct r {};\n"
5659       "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
5660       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
5661       "    y;",
5662       Style);
5663   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
5664   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
5665   verifyFormat(
5666       "template <typename... a> struct s {};\n"
5667       "extern s<\n"
5668       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
5669       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n"
5670       "    y;",
5671       Style);
5672   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
5673   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
5674   verifyFormat(
5675       "template <typename... a> struct t {};\n"
5676       "extern t<\n"
5677       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
5678       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n"
5679       "    y;",
5680       Style);
5681 }
5682 
5683 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
5684   verifyFormat(
5685       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5686       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5687   verifyFormat(
5688       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5689       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5690       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
5691 
5692   // FIXME: Should we have the extra indent after the second break?
5693   verifyFormat(
5694       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5695       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5696       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5697 
5698   verifyFormat(
5699       "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
5700       "                    cccccccccccccccccccccccccccccccccccccccccccccc());");
5701 
5702   // Breaking at nested name specifiers is generally not desirable.
5703   verifyFormat(
5704       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5705       "    aaaaaaaaaaaaaaaaaaaaaaa);");
5706 
5707   verifyFormat(
5708       "aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n"
5709       "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5710       "                       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5711       "                   aaaaaaaaaaaaaaaaaaaaa);",
5712       getLLVMStyleWithColumns(74));
5713 
5714   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5715                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5716                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5717 }
5718 
5719 TEST_F(FormatTest, UnderstandsTemplateParameters) {
5720   verifyFormat("A<int> a;");
5721   verifyFormat("A<A<A<int>>> a;");
5722   verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
5723   verifyFormat("bool x = a < 1 || 2 > a;");
5724   verifyFormat("bool x = 5 < f<int>();");
5725   verifyFormat("bool x = f<int>() > 5;");
5726   verifyFormat("bool x = 5 < a<int>::x;");
5727   verifyFormat("bool x = a < 4 ? a > 2 : false;");
5728   verifyFormat("bool x = f() ? a < 2 : a > 2;");
5729 
5730   verifyGoogleFormat("A<A<int>> a;");
5731   verifyGoogleFormat("A<A<A<int>>> a;");
5732   verifyGoogleFormat("A<A<A<A<int>>>> a;");
5733   verifyGoogleFormat("A<A<int> > a;");
5734   verifyGoogleFormat("A<A<A<int> > > a;");
5735   verifyGoogleFormat("A<A<A<A<int> > > > a;");
5736   verifyGoogleFormat("A<::A<int>> a;");
5737   verifyGoogleFormat("A<::A> a;");
5738   verifyGoogleFormat("A< ::A> a;");
5739   verifyGoogleFormat("A< ::A<int> > a;");
5740   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
5741   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
5742   EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
5743   EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
5744   EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };",
5745             format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle()));
5746 
5747   verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
5748 
5749   verifyFormat("test >> a >> b;");
5750   verifyFormat("test << a >> b;");
5751 
5752   verifyFormat("f<int>();");
5753   verifyFormat("template <typename T> void f() {}");
5754   verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
5755   verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
5756                "sizeof(char)>::type>;");
5757   verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
5758   verifyFormat("f(a.operator()<A>());");
5759   verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5760                "      .template operator()<A>());",
5761                getLLVMStyleWithColumns(35));
5762 
5763   // Not template parameters.
5764   verifyFormat("return a < b && c > d;");
5765   verifyFormat("void f() {\n"
5766                "  while (a < b && c > d) {\n"
5767                "  }\n"
5768                "}");
5769   verifyFormat("template <typename... Types>\n"
5770                "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
5771 
5772   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5773                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
5774                getLLVMStyleWithColumns(60));
5775   verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
5776   verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
5777   verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
5778 }
5779 
5780 TEST_F(FormatTest, BitshiftOperatorWidth) {
5781   EXPECT_EQ("int a = 1 << 2; /* foo\n"
5782             "                   bar */",
5783             format("int    a=1<<2;  /* foo\n"
5784                    "                   bar */"));
5785 
5786   EXPECT_EQ("int b = 256 >> 1; /* foo\n"
5787             "                     bar */",
5788             format("int  b  =256>>1 ;  /* foo\n"
5789                    "                      bar */"));
5790 }
5791 
5792 TEST_F(FormatTest, UnderstandsBinaryOperators) {
5793   verifyFormat("COMPARE(a, ==, b);");
5794   verifyFormat("auto s = sizeof...(Ts) - 1;");
5795 }
5796 
5797 TEST_F(FormatTest, UnderstandsPointersToMembers) {
5798   verifyFormat("int A::*x;");
5799   verifyFormat("int (S::*func)(void *);");
5800   verifyFormat("void f() { int (S::*func)(void *); }");
5801   verifyFormat("typedef bool *(Class::*Member)() const;");
5802   verifyFormat("void f() {\n"
5803                "  (a->*f)();\n"
5804                "  a->*x;\n"
5805                "  (a.*f)();\n"
5806                "  ((*a).*f)();\n"
5807                "  a.*x;\n"
5808                "}");
5809   verifyFormat("void f() {\n"
5810                "  (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
5811                "      aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
5812                "}");
5813   verifyFormat(
5814       "(aaaaaaaaaa->*bbbbbbb)(\n"
5815       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
5816   FormatStyle Style = getLLVMStyle();
5817   Style.PointerAlignment = FormatStyle::PAS_Left;
5818   verifyFormat("typedef bool* (Class::*Member)() const;", Style);
5819 }
5820 
5821 TEST_F(FormatTest, UnderstandsUnaryOperators) {
5822   verifyFormat("int a = -2;");
5823   verifyFormat("f(-1, -2, -3);");
5824   verifyFormat("a[-1] = 5;");
5825   verifyFormat("int a = 5 + -2;");
5826   verifyFormat("if (i == -1) {\n}");
5827   verifyFormat("if (i != -1) {\n}");
5828   verifyFormat("if (i > -1) {\n}");
5829   verifyFormat("if (i < -1) {\n}");
5830   verifyFormat("++(a->f());");
5831   verifyFormat("--(a->f());");
5832   verifyFormat("(a->f())++;");
5833   verifyFormat("a[42]++;");
5834   verifyFormat("if (!(a->f())) {\n}");
5835   verifyFormat("if (!+i) {\n}");
5836   verifyFormat("~&a;");
5837 
5838   verifyFormat("a-- > b;");
5839   verifyFormat("b ? -a : c;");
5840   verifyFormat("n * sizeof char16;");
5841   verifyFormat("n * alignof char16;", getGoogleStyle());
5842   verifyFormat("sizeof(char);");
5843   verifyFormat("alignof(char);", getGoogleStyle());
5844 
5845   verifyFormat("return -1;");
5846   verifyFormat("switch (a) {\n"
5847                "case -1:\n"
5848                "  break;\n"
5849                "}");
5850   verifyFormat("#define X -1");
5851   verifyFormat("#define X -kConstant");
5852 
5853   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
5854   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
5855 
5856   verifyFormat("int a = /* confusing comment */ -1;");
5857   // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
5858   verifyFormat("int a = i /* confusing comment */++;");
5859 }
5860 
5861 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
5862   verifyFormat("if (!aaaaaaaaaa( // break\n"
5863                "        aaaaa)) {\n"
5864                "}");
5865   verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
5866                "    aaaaa));");
5867   verifyFormat("*aaa = aaaaaaa( // break\n"
5868                "    bbbbbb);");
5869 }
5870 
5871 TEST_F(FormatTest, UnderstandsOverloadedOperators) {
5872   verifyFormat("bool operator<();");
5873   verifyFormat("bool operator>();");
5874   verifyFormat("bool operator=();");
5875   verifyFormat("bool operator==();");
5876   verifyFormat("bool operator!=();");
5877   verifyFormat("int operator+();");
5878   verifyFormat("int operator++();");
5879   verifyFormat("int operator++(int) volatile noexcept;");
5880   verifyFormat("bool operator,();");
5881   verifyFormat("bool operator();");
5882   verifyFormat("bool operator()();");
5883   verifyFormat("bool operator[]();");
5884   verifyFormat("operator bool();");
5885   verifyFormat("operator int();");
5886   verifyFormat("operator void *();");
5887   verifyFormat("operator SomeType<int>();");
5888   verifyFormat("operator SomeType<int, int>();");
5889   verifyFormat("operator SomeType<SomeType<int>>();");
5890   verifyFormat("void *operator new(std::size_t size);");
5891   verifyFormat("void *operator new[](std::size_t size);");
5892   verifyFormat("void operator delete(void *ptr);");
5893   verifyFormat("void operator delete[](void *ptr);");
5894   verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
5895                "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
5896   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n"
5897                "    aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;");
5898 
5899   verifyFormat(
5900       "ostream &operator<<(ostream &OutputStream,\n"
5901       "                    SomeReallyLongType WithSomeReallyLongValue);");
5902   verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
5903                "               const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
5904                "  return left.group < right.group;\n"
5905                "}");
5906   verifyFormat("SomeType &operator=(const SomeType &S);");
5907   verifyFormat("f.template operator()<int>();");
5908 
5909   verifyGoogleFormat("operator void*();");
5910   verifyGoogleFormat("operator SomeType<SomeType<int>>();");
5911   verifyGoogleFormat("operator ::A();");
5912 
5913   verifyFormat("using A::operator+;");
5914   verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
5915                "int i;");
5916 }
5917 
5918 TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
5919   verifyFormat("Deleted &operator=(const Deleted &) & = default;");
5920   verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
5921   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
5922   verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
5923   verifyFormat("Deleted &operator=(const Deleted &) &;");
5924   verifyFormat("Deleted &operator=(const Deleted &) &&;");
5925   verifyFormat("SomeType MemberFunction(const Deleted &) &;");
5926   verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
5927   verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
5928   verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
5929   verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
5930   verifyFormat("void Fn(T const &) const &;");
5931   verifyFormat("void Fn(T const volatile &&) const volatile &&;");
5932   verifyFormat("template <typename T>\n"
5933                "void F(T) && = delete;",
5934                getGoogleStyle());
5935 
5936   FormatStyle AlignLeft = getLLVMStyle();
5937   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
5938   verifyFormat("void A::b() && {}", AlignLeft);
5939   verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft);
5940   verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
5941                AlignLeft);
5942   verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft);
5943   verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft);
5944   verifyFormat("auto Function(T t) & -> void {}", AlignLeft);
5945   verifyFormat("auto Function(T... t) & -> void {}", AlignLeft);
5946   verifyFormat("auto Function(T) & -> void {}", AlignLeft);
5947   verifyFormat("auto Function(T) & -> void;", AlignLeft);
5948   verifyFormat("void Fn(T const&) const&;", AlignLeft);
5949   verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft);
5950 
5951   FormatStyle Spaces = getLLVMStyle();
5952   Spaces.SpacesInCStyleCastParentheses = true;
5953   verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
5954   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
5955   verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
5956   verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
5957 
5958   Spaces.SpacesInCStyleCastParentheses = false;
5959   Spaces.SpacesInParentheses = true;
5960   verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
5961   verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces);
5962   verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
5963   verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
5964 }
5965 
5966 TEST_F(FormatTest, UnderstandsNewAndDelete) {
5967   verifyFormat("void f() {\n"
5968                "  A *a = new A;\n"
5969                "  A *a = new (placement) A;\n"
5970                "  delete a;\n"
5971                "  delete (A *)a;\n"
5972                "}");
5973   verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
5974                "    typename aaaaaaaaaaaaaaaaaaaaaaaa();");
5975   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5976                "    new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
5977                "        typename aaaaaaaaaaaaaaaaaaaaaaaa();");
5978   verifyFormat("delete[] h->p;");
5979 }
5980 
5981 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
5982   verifyFormat("int *f(int *a) {}");
5983   verifyFormat("int main(int argc, char **argv) {}");
5984   verifyFormat("Test::Test(int b) : a(b * b) {}");
5985   verifyIndependentOfContext("f(a, *a);");
5986   verifyFormat("void g() { f(*a); }");
5987   verifyIndependentOfContext("int a = b * 10;");
5988   verifyIndependentOfContext("int a = 10 * b;");
5989   verifyIndependentOfContext("int a = b * c;");
5990   verifyIndependentOfContext("int a += b * c;");
5991   verifyIndependentOfContext("int a -= b * c;");
5992   verifyIndependentOfContext("int a *= b * c;");
5993   verifyIndependentOfContext("int a /= b * c;");
5994   verifyIndependentOfContext("int a = *b;");
5995   verifyIndependentOfContext("int a = *b * c;");
5996   verifyIndependentOfContext("int a = b * *c;");
5997   verifyIndependentOfContext("int a = b * (10);");
5998   verifyIndependentOfContext("S << b * (10);");
5999   verifyIndependentOfContext("return 10 * b;");
6000   verifyIndependentOfContext("return *b * *c;");
6001   verifyIndependentOfContext("return a & ~b;");
6002   verifyIndependentOfContext("f(b ? *c : *d);");
6003   verifyIndependentOfContext("int a = b ? *c : *d;");
6004   verifyIndependentOfContext("*b = a;");
6005   verifyIndependentOfContext("a * ~b;");
6006   verifyIndependentOfContext("a * !b;");
6007   verifyIndependentOfContext("a * +b;");
6008   verifyIndependentOfContext("a * -b;");
6009   verifyIndependentOfContext("a * ++b;");
6010   verifyIndependentOfContext("a * --b;");
6011   verifyIndependentOfContext("a[4] * b;");
6012   verifyIndependentOfContext("a[a * a] = 1;");
6013   verifyIndependentOfContext("f() * b;");
6014   verifyIndependentOfContext("a * [self dostuff];");
6015   verifyIndependentOfContext("int x = a * (a + b);");
6016   verifyIndependentOfContext("(a *)(a + b);");
6017   verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
6018   verifyIndependentOfContext("int *pa = (int *)&a;");
6019   verifyIndependentOfContext("return sizeof(int **);");
6020   verifyIndependentOfContext("return sizeof(int ******);");
6021   verifyIndependentOfContext("return (int **&)a;");
6022   verifyIndependentOfContext("f((*PointerToArray)[10]);");
6023   verifyFormat("void f(Type (*parameter)[10]) {}");
6024   verifyFormat("void f(Type (&parameter)[10]) {}");
6025   verifyGoogleFormat("return sizeof(int**);");
6026   verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
6027   verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
6028   verifyFormat("auto a = [](int **&, int ***) {};");
6029   verifyFormat("auto PointerBinding = [](const char *S) {};");
6030   verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
6031   verifyFormat("[](const decltype(*a) &value) {}");
6032   verifyFormat("decltype(a * b) F();");
6033   verifyFormat("#define MACRO() [](A *a) { return 1; }");
6034   verifyFormat("Constructor() : member([](A *a, B *b) {}) {}");
6035   verifyIndependentOfContext("typedef void (*f)(int *a);");
6036   verifyIndependentOfContext("int i{a * b};");
6037   verifyIndependentOfContext("aaa && aaa->f();");
6038   verifyIndependentOfContext("int x = ~*p;");
6039   verifyFormat("Constructor() : a(a), area(width * height) {}");
6040   verifyFormat("Constructor() : a(a), area(a, width * height) {}");
6041   verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
6042   verifyFormat("void f() { f(a, c * d); }");
6043   verifyFormat("void f() { f(new a(), c * d); }");
6044   verifyFormat("void f(const MyOverride &override);");
6045   verifyFormat("void f(const MyFinal &final);");
6046   verifyIndependentOfContext("bool a = f() && override.f();");
6047   verifyIndependentOfContext("bool a = f() && final.f();");
6048 
6049   verifyIndependentOfContext("InvalidRegions[*R] = 0;");
6050 
6051   verifyIndependentOfContext("A<int *> a;");
6052   verifyIndependentOfContext("A<int **> a;");
6053   verifyIndependentOfContext("A<int *, int *> a;");
6054   verifyIndependentOfContext("A<int *[]> a;");
6055   verifyIndependentOfContext(
6056       "const char *const p = reinterpret_cast<const char *const>(q);");
6057   verifyIndependentOfContext("A<int **, int **> a;");
6058   verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
6059   verifyFormat("for (char **a = b; *a; ++a) {\n}");
6060   verifyFormat("for (; a && b;) {\n}");
6061   verifyFormat("bool foo = true && [] { return false; }();");
6062 
6063   verifyFormat(
6064       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6065       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6066 
6067   verifyGoogleFormat("int const* a = &b;");
6068   verifyGoogleFormat("**outparam = 1;");
6069   verifyGoogleFormat("*outparam = a * b;");
6070   verifyGoogleFormat("int main(int argc, char** argv) {}");
6071   verifyGoogleFormat("A<int*> a;");
6072   verifyGoogleFormat("A<int**> a;");
6073   verifyGoogleFormat("A<int*, int*> a;");
6074   verifyGoogleFormat("A<int**, int**> a;");
6075   verifyGoogleFormat("f(b ? *c : *d);");
6076   verifyGoogleFormat("int a = b ? *c : *d;");
6077   verifyGoogleFormat("Type* t = **x;");
6078   verifyGoogleFormat("Type* t = *++*x;");
6079   verifyGoogleFormat("*++*x;");
6080   verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
6081   verifyGoogleFormat("Type* t = x++ * y;");
6082   verifyGoogleFormat(
6083       "const char* const p = reinterpret_cast<const char* const>(q);");
6084   verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
6085   verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
6086   verifyGoogleFormat("template <typename T>\n"
6087                      "void f(int i = 0, SomeType** temps = NULL);");
6088 
6089   FormatStyle Left = getLLVMStyle();
6090   Left.PointerAlignment = FormatStyle::PAS_Left;
6091   verifyFormat("x = *a(x) = *a(y);", Left);
6092   verifyFormat("for (;; *a = b) {\n}", Left);
6093   verifyFormat("return *this += 1;", Left);
6094   verifyFormat("throw *x;", Left);
6095   verifyFormat("delete *x;", Left);
6096   verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left);
6097   verifyFormat("[](const decltype(*a)* ptr) {}", Left);
6098   verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left);
6099 
6100   verifyIndependentOfContext("a = *(x + y);");
6101   verifyIndependentOfContext("a = &(x + y);");
6102   verifyIndependentOfContext("*(x + y).call();");
6103   verifyIndependentOfContext("&(x + y)->call();");
6104   verifyFormat("void f() { &(*I).first; }");
6105 
6106   verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
6107   verifyFormat(
6108       "int *MyValues = {\n"
6109       "    *A, // Operator detection might be confused by the '{'\n"
6110       "    *BB // Operator detection might be confused by previous comment\n"
6111       "};");
6112 
6113   verifyIndependentOfContext("if (int *a = &b)");
6114   verifyIndependentOfContext("if (int &a = *b)");
6115   verifyIndependentOfContext("if (a & b[i])");
6116   verifyIndependentOfContext("if (a::b::c::d & b[i])");
6117   verifyIndependentOfContext("if (*b[i])");
6118   verifyIndependentOfContext("if (int *a = (&b))");
6119   verifyIndependentOfContext("while (int *a = &b)");
6120   verifyIndependentOfContext("size = sizeof *a;");
6121   verifyIndependentOfContext("if (a && (b = c))");
6122   verifyFormat("void f() {\n"
6123                "  for (const int &v : Values) {\n"
6124                "  }\n"
6125                "}");
6126   verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
6127   verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
6128   verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
6129 
6130   verifyFormat("#define A (!a * b)");
6131   verifyFormat("#define MACRO     \\\n"
6132                "  int *i = a * b; \\\n"
6133                "  void f(a *b);",
6134                getLLVMStyleWithColumns(19));
6135 
6136   verifyIndependentOfContext("A = new SomeType *[Length];");
6137   verifyIndependentOfContext("A = new SomeType *[Length]();");
6138   verifyIndependentOfContext("T **t = new T *;");
6139   verifyIndependentOfContext("T **t = new T *();");
6140   verifyGoogleFormat("A = new SomeType*[Length]();");
6141   verifyGoogleFormat("A = new SomeType*[Length];");
6142   verifyGoogleFormat("T** t = new T*;");
6143   verifyGoogleFormat("T** t = new T*();");
6144 
6145   verifyFormat("STATIC_ASSERT((a & b) == 0);");
6146   verifyFormat("STATIC_ASSERT(0 == (a & b));");
6147   verifyFormat("template <bool a, bool b> "
6148                "typename t::if<x && y>::type f() {}");
6149   verifyFormat("template <int *y> f() {}");
6150   verifyFormat("vector<int *> v;");
6151   verifyFormat("vector<int *const> v;");
6152   verifyFormat("vector<int *const **const *> v;");
6153   verifyFormat("vector<int *volatile> v;");
6154   verifyFormat("vector<a * b> v;");
6155   verifyFormat("foo<b && false>();");
6156   verifyFormat("foo<b & 1>();");
6157   verifyFormat("decltype(*::std::declval<const T &>()) void F();");
6158   verifyFormat(
6159       "template <class T, class = typename std::enable_if<\n"
6160       "                       std::is_integral<T>::value &&\n"
6161       "                       (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
6162       "void F();",
6163       getLLVMStyleWithColumns(70));
6164   verifyFormat(
6165       "template <class T,\n"
6166       "          class = typename std::enable_if<\n"
6167       "              std::is_integral<T>::value &&\n"
6168       "              (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n"
6169       "          class U>\n"
6170       "void F();",
6171       getLLVMStyleWithColumns(70));
6172   verifyFormat(
6173       "template <class T,\n"
6174       "          class = typename ::std::enable_if<\n"
6175       "              ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
6176       "void F();",
6177       getGoogleStyleWithColumns(68));
6178 
6179   verifyIndependentOfContext("MACRO(int *i);");
6180   verifyIndependentOfContext("MACRO(auto *a);");
6181   verifyIndependentOfContext("MACRO(const A *a);");
6182   verifyIndependentOfContext("MACRO(A *const a);");
6183   verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
6184   verifyFormat("void f() { f(float{1}, a * a); }");
6185   // FIXME: Is there a way to make this work?
6186   // verifyIndependentOfContext("MACRO(A *a);");
6187 
6188   verifyFormat("DatumHandle const *operator->() const { return input_; }");
6189   verifyFormat("return options != nullptr && operator==(*options);");
6190 
6191   EXPECT_EQ("#define OP(x)                                    \\\n"
6192             "  ostream &operator<<(ostream &s, const A &a) {  \\\n"
6193             "    return s << a.DebugString();                 \\\n"
6194             "  }",
6195             format("#define OP(x) \\\n"
6196                    "  ostream &operator<<(ostream &s, const A &a) { \\\n"
6197                    "    return s << a.DebugString(); \\\n"
6198                    "  }",
6199                    getLLVMStyleWithColumns(50)));
6200 
6201   // FIXME: We cannot handle this case yet; we might be able to figure out that
6202   // foo<x> d > v; doesn't make sense.
6203   verifyFormat("foo<a<b && c> d> v;");
6204 
6205   FormatStyle PointerMiddle = getLLVMStyle();
6206   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
6207   verifyFormat("delete *x;", PointerMiddle);
6208   verifyFormat("int * x;", PointerMiddle);
6209   verifyFormat("int *[] x;", PointerMiddle);
6210   verifyFormat("template <int * y> f() {}", PointerMiddle);
6211   verifyFormat("int * f(int * a) {}", PointerMiddle);
6212   verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
6213   verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
6214   verifyFormat("A<int *> a;", PointerMiddle);
6215   verifyFormat("A<int **> a;", PointerMiddle);
6216   verifyFormat("A<int *, int *> a;", PointerMiddle);
6217   verifyFormat("A<int *[]> a;", PointerMiddle);
6218   verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
6219   verifyFormat("A = new SomeType *[Length];", PointerMiddle);
6220   verifyFormat("T ** t = new T *;", PointerMiddle);
6221 
6222   // Member function reference qualifiers aren't binary operators.
6223   verifyFormat("string // break\n"
6224                "operator()() & {}");
6225   verifyFormat("string // break\n"
6226                "operator()() && {}");
6227   verifyGoogleFormat("template <typename T>\n"
6228                      "auto x() & -> int {}");
6229 }
6230 
6231 TEST_F(FormatTest, UnderstandsAttributes) {
6232   verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
6233   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
6234                "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
6235   FormatStyle AfterType = getLLVMStyle();
6236   AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
6237   verifyFormat("__attribute__((nodebug)) void\n"
6238                "foo() {}\n",
6239                AfterType);
6240 }
6241 
6242 TEST_F(FormatTest, UnderstandsSquareAttributes) {
6243   verifyFormat("SomeType s [[unused]] (InitValue);");
6244   verifyFormat("SomeType s [[gnu::unused]] (InitValue);");
6245   verifyFormat("SomeType s [[using gnu: unused]] (InitValue);");
6246   verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}");
6247   verifyFormat("void f() [[deprecated(\"so sorry\")]];");
6248   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6249                "    [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);");
6250 
6251   // Make sure we do not mistake attributes for array subscripts.
6252   verifyFormat("int a() {}\n"
6253                "[[unused]] int b() {}\n");
6254 
6255   // On the other hand, we still need to correctly find array subscripts.
6256   verifyFormat("int a = std::vector<int>{1, 2, 3}[0];");
6257 
6258   // Make sure we do not parse attributes as lambda introducers.
6259   FormatStyle MultiLineFunctions = getLLVMStyle();
6260   MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
6261   verifyFormat("[[unused]] int b() {\n"
6262                "  return 42;\n"
6263                "}\n",
6264                MultiLineFunctions);
6265 }
6266 
6267 TEST_F(FormatTest, UnderstandsEllipsis) {
6268   verifyFormat("int printf(const char *fmt, ...);");
6269   verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
6270   verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}");
6271 
6272   FormatStyle PointersLeft = getLLVMStyle();
6273   PointersLeft.PointerAlignment = FormatStyle::PAS_Left;
6274   verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft);
6275 }
6276 
6277 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
6278   EXPECT_EQ("int *a;\n"
6279             "int *a;\n"
6280             "int *a;",
6281             format("int *a;\n"
6282                    "int* a;\n"
6283                    "int *a;",
6284                    getGoogleStyle()));
6285   EXPECT_EQ("int* a;\n"
6286             "int* a;\n"
6287             "int* a;",
6288             format("int* a;\n"
6289                    "int* a;\n"
6290                    "int *a;",
6291                    getGoogleStyle()));
6292   EXPECT_EQ("int *a;\n"
6293             "int *a;\n"
6294             "int *a;",
6295             format("int *a;\n"
6296                    "int * a;\n"
6297                    "int *  a;",
6298                    getGoogleStyle()));
6299   EXPECT_EQ("auto x = [] {\n"
6300             "  int *a;\n"
6301             "  int *a;\n"
6302             "  int *a;\n"
6303             "};",
6304             format("auto x=[]{int *a;\n"
6305                    "int * a;\n"
6306                    "int *  a;};",
6307                    getGoogleStyle()));
6308 }
6309 
6310 TEST_F(FormatTest, UnderstandsRvalueReferences) {
6311   verifyFormat("int f(int &&a) {}");
6312   verifyFormat("int f(int a, char &&b) {}");
6313   verifyFormat("void f() { int &&a = b; }");
6314   verifyGoogleFormat("int f(int a, char&& b) {}");
6315   verifyGoogleFormat("void f() { int&& a = b; }");
6316 
6317   verifyIndependentOfContext("A<int &&> a;");
6318   verifyIndependentOfContext("A<int &&, int &&> a;");
6319   verifyGoogleFormat("A<int&&> a;");
6320   verifyGoogleFormat("A<int&&, int&&> a;");
6321 
6322   // Not rvalue references:
6323   verifyFormat("template <bool B, bool C> class A {\n"
6324                "  static_assert(B && C, \"Something is wrong\");\n"
6325                "};");
6326   verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
6327   verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
6328   verifyFormat("#define A(a, b) (a && b)");
6329 }
6330 
6331 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
6332   verifyFormat("void f() {\n"
6333                "  x[aaaaaaaaa -\n"
6334                "    b] = 23;\n"
6335                "}",
6336                getLLVMStyleWithColumns(15));
6337 }
6338 
6339 TEST_F(FormatTest, FormatsCasts) {
6340   verifyFormat("Type *A = static_cast<Type *>(P);");
6341   verifyFormat("Type *A = (Type *)P;");
6342   verifyFormat("Type *A = (vector<Type *, int *>)P;");
6343   verifyFormat("int a = (int)(2.0f);");
6344   verifyFormat("int a = (int)2.0f;");
6345   verifyFormat("x[(int32)y];");
6346   verifyFormat("x = (int32)y;");
6347   verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
6348   verifyFormat("int a = (int)*b;");
6349   verifyFormat("int a = (int)2.0f;");
6350   verifyFormat("int a = (int)~0;");
6351   verifyFormat("int a = (int)++a;");
6352   verifyFormat("int a = (int)sizeof(int);");
6353   verifyFormat("int a = (int)+2;");
6354   verifyFormat("my_int a = (my_int)2.0f;");
6355   verifyFormat("my_int a = (my_int)sizeof(int);");
6356   verifyFormat("return (my_int)aaa;");
6357   verifyFormat("#define x ((int)-1)");
6358   verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
6359   verifyFormat("#define p(q) ((int *)&q)");
6360   verifyFormat("fn(a)(b) + 1;");
6361 
6362   verifyFormat("void f() { my_int a = (my_int)*b; }");
6363   verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
6364   verifyFormat("my_int a = (my_int)~0;");
6365   verifyFormat("my_int a = (my_int)++a;");
6366   verifyFormat("my_int a = (my_int)-2;");
6367   verifyFormat("my_int a = (my_int)1;");
6368   verifyFormat("my_int a = (my_int *)1;");
6369   verifyFormat("my_int a = (const my_int)-1;");
6370   verifyFormat("my_int a = (const my_int *)-1;");
6371   verifyFormat("my_int a = (my_int)(my_int)-1;");
6372   verifyFormat("my_int a = (ns::my_int)-2;");
6373   verifyFormat("case (my_int)ONE:");
6374   verifyFormat("auto x = (X)this;");
6375 
6376   // FIXME: single value wrapped with paren will be treated as cast.
6377   verifyFormat("void f(int i = (kValue)*kMask) {}");
6378 
6379   verifyFormat("{ (void)F; }");
6380 
6381   // Don't break after a cast's
6382   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
6383                "    (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
6384                "                                   bbbbbbbbbbbbbbbbbbbbbb);");
6385 
6386   // These are not casts.
6387   verifyFormat("void f(int *) {}");
6388   verifyFormat("f(foo)->b;");
6389   verifyFormat("f(foo).b;");
6390   verifyFormat("f(foo)(b);");
6391   verifyFormat("f(foo)[b];");
6392   verifyFormat("[](foo) { return 4; }(bar);");
6393   verifyFormat("(*funptr)(foo)[4];");
6394   verifyFormat("funptrs[4](foo)[4];");
6395   verifyFormat("void f(int *);");
6396   verifyFormat("void f(int *) = 0;");
6397   verifyFormat("void f(SmallVector<int>) {}");
6398   verifyFormat("void f(SmallVector<int>);");
6399   verifyFormat("void f(SmallVector<int>) = 0;");
6400   verifyFormat("void f(int i = (kA * kB) & kMask) {}");
6401   verifyFormat("int a = sizeof(int) * b;");
6402   verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
6403   verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
6404   verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
6405   verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
6406 
6407   // These are not casts, but at some point were confused with casts.
6408   verifyFormat("virtual void foo(int *) override;");
6409   verifyFormat("virtual void foo(char &) const;");
6410   verifyFormat("virtual void foo(int *a, char *) const;");
6411   verifyFormat("int a = sizeof(int *) + b;");
6412   verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
6413   verifyFormat("bool b = f(g<int>) && c;");
6414   verifyFormat("typedef void (*f)(int i) func;");
6415 
6416   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
6417                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
6418   // FIXME: The indentation here is not ideal.
6419   verifyFormat(
6420       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6421       "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
6422       "        [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
6423 }
6424 
6425 TEST_F(FormatTest, FormatsFunctionTypes) {
6426   verifyFormat("A<bool()> a;");
6427   verifyFormat("A<SomeType()> a;");
6428   verifyFormat("A<void (*)(int, std::string)> a;");
6429   verifyFormat("A<void *(int)>;");
6430   verifyFormat("void *(*a)(int *, SomeType *);");
6431   verifyFormat("int (*func)(void *);");
6432   verifyFormat("void f() { int (*func)(void *); }");
6433   verifyFormat("template <class CallbackClass>\n"
6434                "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
6435 
6436   verifyGoogleFormat("A<void*(int*, SomeType*)>;");
6437   verifyGoogleFormat("void* (*a)(int);");
6438   verifyGoogleFormat(
6439       "template <class CallbackClass>\n"
6440       "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
6441 
6442   // Other constructs can look somewhat like function types:
6443   verifyFormat("A<sizeof(*x)> a;");
6444   verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
6445   verifyFormat("some_var = function(*some_pointer_var)[0];");
6446   verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
6447   verifyFormat("int x = f(&h)();");
6448   verifyFormat("returnsFunction(&param1, &param2)(param);");
6449   verifyFormat("std::function<\n"
6450                "    LooooooooooongTemplatedType<\n"
6451                "        SomeType>*(\n"
6452                "        LooooooooooooooooongType type)>\n"
6453                "    function;",
6454                getGoogleStyleWithColumns(40));
6455 }
6456 
6457 TEST_F(FormatTest, FormatsPointersToArrayTypes) {
6458   verifyFormat("A (*foo_)[6];");
6459   verifyFormat("vector<int> (*foo_)[6];");
6460 }
6461 
6462 TEST_F(FormatTest, BreaksLongVariableDeclarations) {
6463   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6464                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
6465   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
6466                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
6467   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6468                "    *LoooooooooooooooooooooooooooooooooooooooongVariable;");
6469 
6470   // Different ways of ()-initializiation.
6471   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6472                "    LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
6473   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6474                "    LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
6475   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6476                "    LoooooooooooooooooooooooooooooooooooooooongVariable({});");
6477   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6478                "    LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
6479 
6480   // Lambdas should not confuse the variable declaration heuristic.
6481   verifyFormat("LooooooooooooooooongType\n"
6482                "    variable(nullptr, [](A *a) {});",
6483                getLLVMStyleWithColumns(40));
6484 }
6485 
6486 TEST_F(FormatTest, BreaksLongDeclarations) {
6487   verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
6488                "    AnotherNameForTheLongType;");
6489   verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
6490                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
6491   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6492                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
6493   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
6494                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
6495   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6496                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6497   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
6498                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6499   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
6500                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6501   verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
6502                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6503   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6504                "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);");
6505   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6506                "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}");
6507   FormatStyle Indented = getLLVMStyle();
6508   Indented.IndentWrappedFunctionNames = true;
6509   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6510                "    LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
6511                Indented);
6512   verifyFormat(
6513       "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6514       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
6515       Indented);
6516   verifyFormat(
6517       "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
6518       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
6519       Indented);
6520   verifyFormat(
6521       "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
6522       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
6523       Indented);
6524 
6525   // FIXME: Without the comment, this breaks after "(".
6526   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType  // break\n"
6527                "    (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
6528                getGoogleStyle());
6529 
6530   verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
6531                "                  int LoooooooooooooooooooongParam2) {}");
6532   verifyFormat(
6533       "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
6534       "                                   SourceLocation L, IdentifierIn *II,\n"
6535       "                                   Type *T) {}");
6536   verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
6537                "ReallyReaaallyLongFunctionName(\n"
6538                "    const std::string &SomeParameter,\n"
6539                "    const SomeType<string, SomeOtherTemplateParameter>\n"
6540                "        &ReallyReallyLongParameterName,\n"
6541                "    const SomeType<string, SomeOtherTemplateParameter>\n"
6542                "        &AnotherLongParameterName) {}");
6543   verifyFormat("template <typename A>\n"
6544                "SomeLoooooooooooooooooooooongType<\n"
6545                "    typename some_namespace::SomeOtherType<A>::Type>\n"
6546                "Function() {}");
6547 
6548   verifyGoogleFormat(
6549       "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
6550       "    aaaaaaaaaaaaaaaaaaaaaaa;");
6551   verifyGoogleFormat(
6552       "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
6553       "                                   SourceLocation L) {}");
6554   verifyGoogleFormat(
6555       "some_namespace::LongReturnType\n"
6556       "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
6557       "    int first_long_parameter, int second_parameter) {}");
6558 
6559   verifyGoogleFormat("template <typename T>\n"
6560                      "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
6561                      "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
6562   verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6563                      "                   int aaaaaaaaaaaaaaaaaaaaaaa);");
6564 
6565   verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
6566                "    const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6567                "        *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6568   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6569                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
6570                "        aaaaaaaaaaaaaaaaaaaaaaaa);");
6571   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6572                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
6573                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
6574                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6575 
6576   verifyFormat("template <typename T> // Templates on own line.\n"
6577                "static int            // Some comment.\n"
6578                "MyFunction(int a);",
6579                getLLVMStyle());
6580 }
6581 
6582 TEST_F(FormatTest, FormatsArrays) {
6583   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
6584                "                         [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
6585   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n"
6586                "                         [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;");
6587   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n"
6588                "    aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}");
6589   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6590                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
6591   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6592                "    [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
6593   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6594                "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
6595                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
6596   verifyFormat(
6597       "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
6598       "             << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
6599       "                                  [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
6600   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n"
6601                "    .aaaaaaaaaaaaaaaaaaaaaa();");
6602 
6603   verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
6604                      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
6605   verifyFormat(
6606       "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
6607       "                                  .aaaaaaa[0]\n"
6608       "                                  .aaaaaaaaaaaaaaaaaaaaaa();");
6609   verifyFormat("a[::b::c];");
6610 
6611   verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
6612 
6613   FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
6614   verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit);
6615 }
6616 
6617 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
6618   verifyFormat("(a)->b();");
6619   verifyFormat("--a;");
6620 }
6621 
6622 TEST_F(FormatTest, HandlesIncludeDirectives) {
6623   verifyFormat("#include <string>\n"
6624                "#include <a/b/c.h>\n"
6625                "#include \"a/b/string\"\n"
6626                "#include \"string.h\"\n"
6627                "#include \"string.h\"\n"
6628                "#include <a-a>\n"
6629                "#include < path with space >\n"
6630                "#include_next <test.h>"
6631                "#include \"abc.h\" // this is included for ABC\n"
6632                "#include \"some long include\" // with a comment\n"
6633                "#include \"some very long include path\"\n"
6634                "#include <some/very/long/include/path>\n",
6635                getLLVMStyleWithColumns(35));
6636   EXPECT_EQ("#include \"a.h\"", format("#include  \"a.h\""));
6637   EXPECT_EQ("#include <a>", format("#include<a>"));
6638 
6639   verifyFormat("#import <string>");
6640   verifyFormat("#import <a/b/c.h>");
6641   verifyFormat("#import \"a/b/string\"");
6642   verifyFormat("#import \"string.h\"");
6643   verifyFormat("#import \"string.h\"");
6644   verifyFormat("#if __has_include(<strstream>)\n"
6645                "#include <strstream>\n"
6646                "#endif");
6647 
6648   verifyFormat("#define MY_IMPORT <a/b>");
6649 
6650   verifyFormat("#if __has_include(<a/b>)");
6651   verifyFormat("#if __has_include_next(<a/b>)");
6652   verifyFormat("#define F __has_include(<a/b>)");
6653   verifyFormat("#define F __has_include_next(<a/b>)");
6654 
6655   // Protocol buffer definition or missing "#".
6656   verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
6657                getLLVMStyleWithColumns(30));
6658 
6659   FormatStyle Style = getLLVMStyle();
6660   Style.AlwaysBreakBeforeMultilineStrings = true;
6661   Style.ColumnLimit = 0;
6662   verifyFormat("#import \"abc.h\"", Style);
6663 
6664   // But 'import' might also be a regular C++ namespace.
6665   verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6666                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6667 }
6668 
6669 //===----------------------------------------------------------------------===//
6670 // Error recovery tests.
6671 //===----------------------------------------------------------------------===//
6672 
6673 TEST_F(FormatTest, IncompleteParameterLists) {
6674   FormatStyle NoBinPacking = getLLVMStyle();
6675   NoBinPacking.BinPackParameters = false;
6676   verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
6677                "                        double *min_x,\n"
6678                "                        double *max_x,\n"
6679                "                        double *min_y,\n"
6680                "                        double *max_y,\n"
6681                "                        double *min_z,\n"
6682                "                        double *max_z, ) {}",
6683                NoBinPacking);
6684 }
6685 
6686 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
6687   verifyFormat("void f() { return; }\n42");
6688   verifyFormat("void f() {\n"
6689                "  if (0)\n"
6690                "    return;\n"
6691                "}\n"
6692                "42");
6693   verifyFormat("void f() { return }\n42");
6694   verifyFormat("void f() {\n"
6695                "  if (0)\n"
6696                "    return\n"
6697                "}\n"
6698                "42");
6699 }
6700 
6701 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
6702   EXPECT_EQ("void f() { return }", format("void  f ( )  {  return  }"));
6703   EXPECT_EQ("void f() {\n"
6704             "  if (a)\n"
6705             "    return\n"
6706             "}",
6707             format("void  f  (  )  {  if  ( a )  return  }"));
6708   EXPECT_EQ("namespace N {\n"
6709             "void f()\n"
6710             "}",
6711             format("namespace  N  {  void f()  }"));
6712   EXPECT_EQ("namespace N {\n"
6713             "void f() {}\n"
6714             "void g()\n"
6715             "} // namespace N",
6716             format("namespace N  { void f( ) { } void g( ) }"));
6717 }
6718 
6719 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
6720   verifyFormat("int aaaaaaaa =\n"
6721                "    // Overlylongcomment\n"
6722                "    b;",
6723                getLLVMStyleWithColumns(20));
6724   verifyFormat("function(\n"
6725                "    ShortArgument,\n"
6726                "    LoooooooooooongArgument);\n",
6727                getLLVMStyleWithColumns(20));
6728 }
6729 
6730 TEST_F(FormatTest, IncorrectAccessSpecifier) {
6731   verifyFormat("public:");
6732   verifyFormat("class A {\n"
6733                "public\n"
6734                "  void f() {}\n"
6735                "};");
6736   verifyFormat("public\n"
6737                "int qwerty;");
6738   verifyFormat("public\n"
6739                "B {}");
6740   verifyFormat("public\n"
6741                "{}");
6742   verifyFormat("public\n"
6743                "B { int x; }");
6744 }
6745 
6746 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
6747   verifyFormat("{");
6748   verifyFormat("#})");
6749   verifyNoCrash("(/**/[:!] ?[).");
6750 }
6751 
6752 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) {
6753   // Found by oss-fuzz:
6754   // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212
6755   FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
6756   Style.ColumnLimit = 60;
6757   verifyNoCrash(
6758       "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20"
6759       "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20"
6760       "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a",
6761       Style);
6762 }
6763 
6764 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
6765   verifyFormat("do {\n}");
6766   verifyFormat("do {\n}\n"
6767                "f();");
6768   verifyFormat("do {\n}\n"
6769                "wheeee(fun);");
6770   verifyFormat("do {\n"
6771                "  f();\n"
6772                "}");
6773 }
6774 
6775 TEST_F(FormatTest, IncorrectCodeMissingParens) {
6776   verifyFormat("if {\n  foo;\n  foo();\n}");
6777   verifyFormat("switch {\n  foo;\n  foo();\n}");
6778   verifyIncompleteFormat("for {\n  foo;\n  foo();\n}");
6779   verifyFormat("while {\n  foo;\n  foo();\n}");
6780   verifyFormat("do {\n  foo;\n  foo();\n} while;");
6781 }
6782 
6783 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
6784   verifyIncompleteFormat("namespace {\n"
6785                          "class Foo { Foo (\n"
6786                          "};\n"
6787                          "} // namespace");
6788 }
6789 
6790 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
6791   EXPECT_EQ("{\n  {}\n", format("{\n{\n}\n"));
6792   EXPECT_EQ("{\n  {}\n", format("{\n  {\n}\n"));
6793   EXPECT_EQ("{\n  {}\n", format("{\n  {\n  }\n"));
6794   EXPECT_EQ("{\n  {}\n}\n}\n", format("{\n  {\n    }\n  }\n}\n"));
6795 
6796   EXPECT_EQ("{\n"
6797             "  {\n"
6798             "    breakme(\n"
6799             "        qwe);\n"
6800             "  }\n",
6801             format("{\n"
6802                    "    {\n"
6803                    " breakme(qwe);\n"
6804                    "}\n",
6805                    getLLVMStyleWithColumns(10)));
6806 }
6807 
6808 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
6809   verifyFormat("int x = {\n"
6810                "    avariable,\n"
6811                "    b(alongervariable)};",
6812                getLLVMStyleWithColumns(25));
6813 }
6814 
6815 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
6816   verifyFormat("return (a)(b){1, 2, 3};");
6817 }
6818 
6819 TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
6820   verifyFormat("vector<int> x{1, 2, 3, 4};");
6821   verifyFormat("vector<int> x{\n"
6822                "    1,\n"
6823                "    2,\n"
6824                "    3,\n"
6825                "    4,\n"
6826                "};");
6827   verifyFormat("vector<T> x{{}, {}, {}, {}};");
6828   verifyFormat("f({1, 2});");
6829   verifyFormat("auto v = Foo{-1};");
6830   verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
6831   verifyFormat("Class::Class : member{1, 2, 3} {}");
6832   verifyFormat("new vector<int>{1, 2, 3};");
6833   verifyFormat("new int[3]{1, 2, 3};");
6834   verifyFormat("new int{1};");
6835   verifyFormat("return {arg1, arg2};");
6836   verifyFormat("return {arg1, SomeType{parameter}};");
6837   verifyFormat("int count = set<int>{f(), g(), h()}.size();");
6838   verifyFormat("new T{arg1, arg2};");
6839   verifyFormat("f(MyMap[{composite, key}]);");
6840   verifyFormat("class Class {\n"
6841                "  T member = {arg1, arg2};\n"
6842                "};");
6843   verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
6844   verifyFormat("const struct A a = {.a = 1, .b = 2};");
6845   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
6846   verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
6847   verifyFormat("int a = std::is_integral<int>{} + 0;");
6848 
6849   verifyFormat("int foo(int i) { return fo1{}(i); }");
6850   verifyFormat("int foo(int i) { return fo1{}(i); }");
6851   verifyFormat("auto i = decltype(x){};");
6852   verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
6853   verifyFormat("Node n{1, Node{1000}, //\n"
6854                "       2};");
6855   verifyFormat("Aaaa aaaaaaa{\n"
6856                "    {\n"
6857                "        aaaa,\n"
6858                "    },\n"
6859                "};");
6860   verifyFormat("class C : public D {\n"
6861                "  SomeClass SC{2};\n"
6862                "};");
6863   verifyFormat("class C : public A {\n"
6864                "  class D : public B {\n"
6865                "    void f() { int i{2}; }\n"
6866                "  };\n"
6867                "};");
6868   verifyFormat("#define A {a, a},");
6869 
6870   // Avoid breaking between equal sign and opening brace
6871   FormatStyle AvoidBreakingFirstArgument = getLLVMStyle();
6872   AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200;
6873   verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n"
6874                "    {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n"
6875                "     {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n"
6876                "     {\"ccccccccccccccccccccc\", 2}};",
6877                AvoidBreakingFirstArgument);
6878 
6879   // Binpacking only if there is no trailing comma
6880   verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n"
6881                "                      cccccccccc, dddddddddd};",
6882 			   getLLVMStyleWithColumns(50));
6883   verifyFormat("const Aaaaaa aaaaa = {\n"
6884                "    aaaaaaaaaaa,\n"
6885                "    bbbbbbbbbbb,\n"
6886                "    ccccccccccc,\n"
6887                "    ddddddddddd,\n"
6888                "};", getLLVMStyleWithColumns(50));
6889 
6890   // Cases where distinguising braced lists and blocks is hard.
6891   verifyFormat("vector<int> v{12} GUARDED_BY(mutex);");
6892   verifyFormat("void f() {\n"
6893                "  return; // comment\n"
6894                "}\n"
6895                "SomeType t;");
6896   verifyFormat("void f() {\n"
6897                "  if (a) {\n"
6898                "    f();\n"
6899                "  }\n"
6900                "}\n"
6901                "SomeType t;");
6902 
6903   // In combination with BinPackArguments = false.
6904   FormatStyle NoBinPacking = getLLVMStyle();
6905   NoBinPacking.BinPackArguments = false;
6906   verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
6907                "                      bbbbb,\n"
6908                "                      ccccc,\n"
6909                "                      ddddd,\n"
6910                "                      eeeee,\n"
6911                "                      ffffff,\n"
6912                "                      ggggg,\n"
6913                "                      hhhhhh,\n"
6914                "                      iiiiii,\n"
6915                "                      jjjjjj,\n"
6916                "                      kkkkkk};",
6917                NoBinPacking);
6918   verifyFormat("const Aaaaaa aaaaa = {\n"
6919                "    aaaaa,\n"
6920                "    bbbbb,\n"
6921                "    ccccc,\n"
6922                "    ddddd,\n"
6923                "    eeeee,\n"
6924                "    ffffff,\n"
6925                "    ggggg,\n"
6926                "    hhhhhh,\n"
6927                "    iiiiii,\n"
6928                "    jjjjjj,\n"
6929                "    kkkkkk,\n"
6930                "};",
6931                NoBinPacking);
6932   verifyFormat(
6933       "const Aaaaaa aaaaa = {\n"
6934       "    aaaaa,  bbbbb,  ccccc,  ddddd,  eeeee,  ffffff, ggggg, hhhhhh,\n"
6935       "    iiiiii, jjjjjj, kkkkkk, aaaaa,  bbbbb,  ccccc,  ddddd, eeeee,\n"
6936       "    ffffff, ggggg,  hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
6937       "};",
6938       NoBinPacking);
6939 
6940   // FIXME: The alignment of these trailing comments might be bad. Then again,
6941   // this might be utterly useless in real code.
6942   verifyFormat("Constructor::Constructor()\n"
6943                "    : some_value{         //\n"
6944                "                 aaaaaaa, //\n"
6945                "                 bbbbbbb} {}");
6946 
6947   // In braced lists, the first comment is always assumed to belong to the
6948   // first element. Thus, it can be moved to the next or previous line as
6949   // appropriate.
6950   EXPECT_EQ("function({// First element:\n"
6951             "          1,\n"
6952             "          // Second element:\n"
6953             "          2});",
6954             format("function({\n"
6955                    "    // First element:\n"
6956                    "    1,\n"
6957                    "    // Second element:\n"
6958                    "    2});"));
6959   EXPECT_EQ("std::vector<int> MyNumbers{\n"
6960             "    // First element:\n"
6961             "    1,\n"
6962             "    // Second element:\n"
6963             "    2};",
6964             format("std::vector<int> MyNumbers{// First element:\n"
6965                    "                           1,\n"
6966                    "                           // Second element:\n"
6967                    "                           2};",
6968                    getLLVMStyleWithColumns(30)));
6969   // A trailing comma should still lead to an enforced line break and no
6970   // binpacking.
6971   EXPECT_EQ("vector<int> SomeVector = {\n"
6972             "    // aaa\n"
6973             "    1,\n"
6974             "    2,\n"
6975             "};",
6976             format("vector<int> SomeVector = { // aaa\n"
6977                    "    1, 2, };"));
6978 
6979   FormatStyle ExtraSpaces = getLLVMStyle();
6980   ExtraSpaces.Cpp11BracedListStyle = false;
6981   ExtraSpaces.ColumnLimit = 75;
6982   verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
6983   verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
6984   verifyFormat("f({ 1, 2 });", ExtraSpaces);
6985   verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
6986   verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
6987   verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
6988   verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
6989   verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
6990   verifyFormat("return { arg1, arg2 };", ExtraSpaces);
6991   verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
6992   verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
6993   verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
6994   verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
6995   verifyFormat("class Class {\n"
6996                "  T member = { arg1, arg2 };\n"
6997                "};",
6998                ExtraSpaces);
6999   verifyFormat(
7000       "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7001       "                                 aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
7002       "                  : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
7003       "                                 bbbbbbbbbbbbbbbbbbbb, bbbbb };",
7004       ExtraSpaces);
7005   verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
7006   verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
7007                ExtraSpaces);
7008   verifyFormat(
7009       "someFunction(OtherParam,\n"
7010       "             BracedList{ // comment 1 (Forcing interesting break)\n"
7011       "                         param1, param2,\n"
7012       "                         // comment 2\n"
7013       "                         param3, param4 });",
7014       ExtraSpaces);
7015   verifyFormat(
7016       "std::this_thread::sleep_for(\n"
7017       "    std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
7018       ExtraSpaces);
7019   verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n"
7020                "    aaaaaaa,\n"
7021                "    aaaaaaaaaa,\n"
7022                "    aaaaa,\n"
7023                "    aaaaaaaaaaaaaaa,\n"
7024                "    aaa,\n"
7025                "    aaaaaaaaaa,\n"
7026                "    a,\n"
7027                "    aaaaaaaaaaaaaaaaaaaaa,\n"
7028                "    aaaaaaaaaaaa,\n"
7029                "    aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
7030                "    aaaaaaa,\n"
7031                "    a};");
7032   verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
7033   verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces);
7034   verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces);
7035 
7036   // Avoid breaking between initializer/equal sign and opening brace
7037   ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200;
7038   verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n"
7039                "  { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
7040                "  { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
7041                "  { \"ccccccccccccccccccccc\", 2 }\n"
7042                "};",
7043                ExtraSpaces);
7044   verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n"
7045                "  { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
7046                "  { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
7047                "  { \"ccccccccccccccccccccc\", 2 }\n"
7048                "};",
7049                ExtraSpaces);
7050 
7051   FormatStyle SpaceBeforeBrace = getLLVMStyle();
7052   SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true;
7053   verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace);
7054   verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace);
7055 }
7056 
7057 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
7058   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7059                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7060                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7061                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7062                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7063                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
7064   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
7065                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7066                "                 1, 22, 333, 4444, 55555, //\n"
7067                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7068                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
7069   verifyFormat(
7070       "vector<int> x = {1,       22, 333, 4444, 55555, 666666, 7777777,\n"
7071       "                 1,       22, 333, 4444, 55555, 666666, 7777777,\n"
7072       "                 1,       22, 333, 4444, 55555, 666666, // comment\n"
7073       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
7074       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
7075       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
7076       "                 7777777};");
7077   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
7078                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
7079                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
7080   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
7081                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
7082                "    // Separating comment.\n"
7083                "    X86::R8, X86::R9, X86::R10, X86::R11, 0};");
7084   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
7085                "    // Leading comment\n"
7086                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
7087                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
7088   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
7089                "                 1, 1, 1, 1};",
7090                getLLVMStyleWithColumns(39));
7091   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
7092                "                 1, 1, 1, 1};",
7093                getLLVMStyleWithColumns(38));
7094   verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
7095                "    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
7096                getLLVMStyleWithColumns(43));
7097   verifyFormat(
7098       "static unsigned SomeValues[10][3] = {\n"
7099       "    {1, 4, 0},  {4, 9, 0},  {4, 5, 9},  {8, 5, 4}, {1, 8, 4},\n"
7100       "    {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
7101   verifyFormat("static auto fields = new vector<string>{\n"
7102                "    \"aaaaaaaaaaaaa\",\n"
7103                "    \"aaaaaaaaaaaaa\",\n"
7104                "    \"aaaaaaaaaaaa\",\n"
7105                "    \"aaaaaaaaaaaaaa\",\n"
7106                "    \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
7107                "    \"aaaaaaaaaaaa\",\n"
7108                "    \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
7109                "};");
7110   verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
7111   verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
7112                "                 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
7113                "                 3, cccccccccccccccccccccc};",
7114                getLLVMStyleWithColumns(60));
7115 
7116   // Trailing commas.
7117   verifyFormat("vector<int> x = {\n"
7118                "    1, 1, 1, 1, 1, 1, 1, 1,\n"
7119                "};",
7120                getLLVMStyleWithColumns(39));
7121   verifyFormat("vector<int> x = {\n"
7122                "    1, 1, 1, 1, 1, 1, 1, 1, //\n"
7123                "};",
7124                getLLVMStyleWithColumns(39));
7125   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
7126                "                 1, 1, 1, 1,\n"
7127                "                 /**/ /**/};",
7128                getLLVMStyleWithColumns(39));
7129 
7130   // Trailing comment in the first line.
7131   verifyFormat("vector<int> iiiiiiiiiiiiiii = {                      //\n"
7132                "    1111111111, 2222222222, 33333333333, 4444444444, //\n"
7133                "    111111111,  222222222,  3333333333,  444444444,  //\n"
7134                "    11111111,   22222222,   333333333,   44444444};");
7135   // Trailing comment in the last line.
7136   verifyFormat("int aaaaa[] = {\n"
7137                "    1, 2, 3, // comment\n"
7138                "    4, 5, 6  // comment\n"
7139                "};");
7140 
7141   // With nested lists, we should either format one item per line or all nested
7142   // lists one on line.
7143   // FIXME: For some nested lists, we can do better.
7144   verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
7145                "        {aaaaaaaaaaaaaaaaaaa},\n"
7146                "        {aaaaaaaaaaaaaaaaaaaaa},\n"
7147                "        {aaaaaaaaaaaaaaaaa}};",
7148                getLLVMStyleWithColumns(60));
7149   verifyFormat(
7150       "SomeStruct my_struct_array = {\n"
7151       "    {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
7152       "     aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
7153       "    {aaa, aaa},\n"
7154       "    {aaa, aaa},\n"
7155       "    {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
7156       "    {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7157       "     aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
7158 
7159   // No column layout should be used here.
7160   verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
7161                "                   bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
7162 
7163   verifyNoCrash("a<,");
7164 
7165   // No braced initializer here.
7166   verifyFormat("void f() {\n"
7167                "  struct Dummy {};\n"
7168                "  f(v);\n"
7169                "}");
7170 
7171   // Long lists should be formatted in columns even if they are nested.
7172   verifyFormat(
7173       "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7174       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7175       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7176       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7177       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7178       "                          1, 22, 333, 4444, 55555, 666666, 7777777});");
7179 
7180   // Allow "single-column" layout even if that violates the column limit. There
7181   // isn't going to be a better way.
7182   verifyFormat("std::vector<int> a = {\n"
7183                "    aaaaaaaa,\n"
7184                "    aaaaaaaa,\n"
7185                "    aaaaaaaa,\n"
7186                "    aaaaaaaa,\n"
7187                "    aaaaaaaaaa,\n"
7188                "    aaaaaaaa,\n"
7189                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa};",
7190                getLLVMStyleWithColumns(30));
7191   verifyFormat("vector<int> aaaa = {\n"
7192                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7193                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7194                "    aaaaaa.aaaaaaa,\n"
7195                "    aaaaaa.aaaaaaa,\n"
7196                "    aaaaaa.aaaaaaa,\n"
7197                "    aaaaaa.aaaaaaa,\n"
7198                "};");
7199 
7200   // Don't create hanging lists.
7201   verifyFormat("someFunction(Param, {List1, List2,\n"
7202                "                     List3});",
7203                getLLVMStyleWithColumns(35));
7204   verifyFormat("someFunction(Param, Param,\n"
7205                "             {List1, List2,\n"
7206                "              List3});",
7207                getLLVMStyleWithColumns(35));
7208   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n"
7209                "                               aaaaaaaaaaaaaaaaaaaaaaa);");
7210 }
7211 
7212 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
7213   FormatStyle DoNotMerge = getLLVMStyle();
7214   DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
7215 
7216   verifyFormat("void f() { return 42; }");
7217   verifyFormat("void f() {\n"
7218                "  return 42;\n"
7219                "}",
7220                DoNotMerge);
7221   verifyFormat("void f() {\n"
7222                "  // Comment\n"
7223                "}");
7224   verifyFormat("{\n"
7225                "#error {\n"
7226                "  int a;\n"
7227                "}");
7228   verifyFormat("{\n"
7229                "  int a;\n"
7230                "#error {\n"
7231                "}");
7232   verifyFormat("void f() {} // comment");
7233   verifyFormat("void f() { int a; } // comment");
7234   verifyFormat("void f() {\n"
7235                "} // comment",
7236                DoNotMerge);
7237   verifyFormat("void f() {\n"
7238                "  int a;\n"
7239                "} // comment",
7240                DoNotMerge);
7241   verifyFormat("void f() {\n"
7242                "} // comment",
7243                getLLVMStyleWithColumns(15));
7244 
7245   verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
7246   verifyFormat("void f() {\n  return 42;\n}", getLLVMStyleWithColumns(22));
7247 
7248   verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
7249   verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
7250   verifyFormat("class C {\n"
7251                "  C()\n"
7252                "      : iiiiiiii(nullptr),\n"
7253                "        kkkkkkk(nullptr),\n"
7254                "        mmmmmmm(nullptr),\n"
7255                "        nnnnnnn(nullptr) {}\n"
7256                "};",
7257                getGoogleStyle());
7258 
7259   FormatStyle NoColumnLimit = getLLVMStyle();
7260   NoColumnLimit.ColumnLimit = 0;
7261   EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
7262   EXPECT_EQ("class C {\n"
7263             "  A() : b(0) {}\n"
7264             "};",
7265             format("class C{A():b(0){}};", NoColumnLimit));
7266   EXPECT_EQ("A()\n"
7267             "    : b(0) {\n"
7268             "}",
7269             format("A()\n:b(0)\n{\n}", NoColumnLimit));
7270 
7271   FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
7272   DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
7273       FormatStyle::SFS_None;
7274   EXPECT_EQ("A()\n"
7275             "    : b(0) {\n"
7276             "}",
7277             format("A():b(0){}", DoNotMergeNoColumnLimit));
7278   EXPECT_EQ("A()\n"
7279             "    : b(0) {\n"
7280             "}",
7281             format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
7282 
7283   verifyFormat("#define A          \\\n"
7284                "  void f() {       \\\n"
7285                "    int i;         \\\n"
7286                "  }",
7287                getLLVMStyleWithColumns(20));
7288   verifyFormat("#define A           \\\n"
7289                "  void f() { int i; }",
7290                getLLVMStyleWithColumns(21));
7291   verifyFormat("#define A            \\\n"
7292                "  void f() {         \\\n"
7293                "    int i;           \\\n"
7294                "  }                  \\\n"
7295                "  int j;",
7296                getLLVMStyleWithColumns(22));
7297   verifyFormat("#define A             \\\n"
7298                "  void f() { int i; } \\\n"
7299                "  int j;",
7300                getLLVMStyleWithColumns(23));
7301 }
7302 
7303 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) {
7304   FormatStyle MergeEmptyOnly = getLLVMStyle();
7305   MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
7306   verifyFormat("class C {\n"
7307                "  int f() {}\n"
7308                "};",
7309                MergeEmptyOnly);
7310   verifyFormat("class C {\n"
7311                "  int f() {\n"
7312                "    return 42;\n"
7313                "  }\n"
7314                "};",
7315                MergeEmptyOnly);
7316   verifyFormat("int f() {}", MergeEmptyOnly);
7317   verifyFormat("int f() {\n"
7318                "  return 42;\n"
7319                "}",
7320                MergeEmptyOnly);
7321 
7322   // Also verify behavior when BraceWrapping.AfterFunction = true
7323   MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
7324   MergeEmptyOnly.BraceWrapping.AfterFunction = true;
7325   verifyFormat("int f() {}", MergeEmptyOnly);
7326   verifyFormat("class C {\n"
7327                "  int f() {}\n"
7328                "};",
7329                MergeEmptyOnly);
7330 }
7331 
7332 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
7333   FormatStyle MergeInlineOnly = getLLVMStyle();
7334   MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
7335   verifyFormat("class C {\n"
7336                "  int f() { return 42; }\n"
7337                "};",
7338                MergeInlineOnly);
7339   verifyFormat("int f() {\n"
7340                "  return 42;\n"
7341                "}",
7342                MergeInlineOnly);
7343 
7344   // SFS_Inline implies SFS_Empty
7345   verifyFormat("class C {\n"
7346                "  int f() {}\n"
7347                "};",
7348                MergeInlineOnly);
7349   verifyFormat("int f() {}", MergeInlineOnly);
7350 
7351   // Also verify behavior when BraceWrapping.AfterFunction = true
7352   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
7353   MergeInlineOnly.BraceWrapping.AfterFunction = true;
7354   verifyFormat("class C {\n"
7355                "  int f() { return 42; }\n"
7356                "};",
7357                MergeInlineOnly);
7358   verifyFormat("int f()\n"
7359                "{\n"
7360                "  return 42;\n"
7361                "}",
7362                MergeInlineOnly);
7363 
7364   // SFS_Inline implies SFS_Empty
7365   verifyFormat("int f() {}", MergeInlineOnly);
7366   verifyFormat("class C {\n"
7367                "  int f() {}\n"
7368                "};",
7369                MergeInlineOnly);
7370 }
7371 
7372 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) {
7373   FormatStyle MergeInlineOnly = getLLVMStyle();
7374   MergeInlineOnly.AllowShortFunctionsOnASingleLine =
7375       FormatStyle::SFS_InlineOnly;
7376   verifyFormat("class C {\n"
7377                "  int f() { return 42; }\n"
7378                "};",
7379                MergeInlineOnly);
7380   verifyFormat("int f() {\n"
7381                "  return 42;\n"
7382                "}",
7383                MergeInlineOnly);
7384 
7385   // SFS_InlineOnly does not imply SFS_Empty
7386   verifyFormat("class C {\n"
7387                "  int f() {}\n"
7388                "};",
7389                MergeInlineOnly);
7390   verifyFormat("int f() {\n"
7391                "}",
7392                MergeInlineOnly);
7393 
7394   // Also verify behavior when BraceWrapping.AfterFunction = true
7395   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
7396   MergeInlineOnly.BraceWrapping.AfterFunction = true;
7397   verifyFormat("class C {\n"
7398                "  int f() { return 42; }\n"
7399                "};",
7400                MergeInlineOnly);
7401   verifyFormat("int f()\n"
7402                "{\n"
7403                "  return 42;\n"
7404                "}",
7405                MergeInlineOnly);
7406 
7407   // SFS_InlineOnly does not imply SFS_Empty
7408   verifyFormat("int f()\n"
7409                "{\n"
7410                "}",
7411                MergeInlineOnly);
7412   verifyFormat("class C {\n"
7413                "  int f() {}\n"
7414                "};",
7415                MergeInlineOnly);
7416 }
7417 
7418 TEST_F(FormatTest, SplitEmptyFunction) {
7419   FormatStyle Style = getLLVMStyle();
7420   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
7421   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7422   Style.BraceWrapping.AfterFunction = true;
7423   Style.BraceWrapping.SplitEmptyFunction = false;
7424   Style.ColumnLimit = 40;
7425 
7426   verifyFormat("int f()\n"
7427                "{}",
7428                Style);
7429   verifyFormat("int f()\n"
7430                "{\n"
7431                "  return 42;\n"
7432                "}",
7433                Style);
7434   verifyFormat("int f()\n"
7435                "{\n"
7436                "  // some comment\n"
7437                "}",
7438                Style);
7439 
7440   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
7441   verifyFormat("int f() {}", Style);
7442   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
7443                "{}",
7444                Style);
7445   verifyFormat("int f()\n"
7446                "{\n"
7447                "  return 0;\n"
7448                "}",
7449                Style);
7450 
7451   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
7452   verifyFormat("class Foo {\n"
7453                "  int f() {}\n"
7454                "};\n",
7455                Style);
7456   verifyFormat("class Foo {\n"
7457                "  int f() { return 0; }\n"
7458                "};\n",
7459                Style);
7460   verifyFormat("class Foo {\n"
7461                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
7462                "  {}\n"
7463                "};\n",
7464                Style);
7465   verifyFormat("class Foo {\n"
7466                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
7467                "  {\n"
7468                "    return 0;\n"
7469                "  }\n"
7470                "};\n",
7471                Style);
7472 
7473   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
7474   verifyFormat("int f() {}", Style);
7475   verifyFormat("int f() { return 0; }", Style);
7476   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
7477                "{}",
7478                Style);
7479   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
7480                "{\n"
7481                "  return 0;\n"
7482                "}",
7483                Style);
7484 }
7485 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) {
7486   FormatStyle Style = getLLVMStyle();
7487   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
7488   verifyFormat("#ifdef A\n"
7489                "int f() {}\n"
7490                "#else\n"
7491                "int g() {}\n"
7492                "#endif",
7493                Style);
7494 }
7495 
7496 TEST_F(FormatTest, SplitEmptyClass) {
7497   FormatStyle Style = getLLVMStyle();
7498   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7499   Style.BraceWrapping.AfterClass = true;
7500   Style.BraceWrapping.SplitEmptyRecord = false;
7501 
7502   verifyFormat("class Foo\n"
7503                "{};",
7504                Style);
7505   verifyFormat("/* something */ class Foo\n"
7506                "{};",
7507                Style);
7508   verifyFormat("template <typename X> class Foo\n"
7509                "{};",
7510                Style);
7511   verifyFormat("class Foo\n"
7512                "{\n"
7513                "  Foo();\n"
7514                "};",
7515                Style);
7516   verifyFormat("typedef class Foo\n"
7517                "{\n"
7518                "} Foo_t;",
7519                Style);
7520 }
7521 
7522 TEST_F(FormatTest, SplitEmptyStruct) {
7523   FormatStyle Style = getLLVMStyle();
7524   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7525   Style.BraceWrapping.AfterStruct = true;
7526   Style.BraceWrapping.SplitEmptyRecord = false;
7527 
7528   verifyFormat("struct Foo\n"
7529                "{};",
7530                Style);
7531   verifyFormat("/* something */ struct Foo\n"
7532                "{};",
7533                Style);
7534   verifyFormat("template <typename X> struct Foo\n"
7535                "{};",
7536                Style);
7537   verifyFormat("struct Foo\n"
7538                "{\n"
7539                "  Foo();\n"
7540                "};",
7541                Style);
7542   verifyFormat("typedef struct Foo\n"
7543                "{\n"
7544                "} Foo_t;",
7545                Style);
7546   //typedef struct Bar {} Bar_t;
7547 }
7548 
7549 TEST_F(FormatTest, SplitEmptyUnion) {
7550   FormatStyle Style = getLLVMStyle();
7551   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7552   Style.BraceWrapping.AfterUnion = true;
7553   Style.BraceWrapping.SplitEmptyRecord = false;
7554 
7555   verifyFormat("union Foo\n"
7556                "{};",
7557                Style);
7558   verifyFormat("/* something */ union Foo\n"
7559                "{};",
7560                Style);
7561   verifyFormat("union Foo\n"
7562                "{\n"
7563                "  A,\n"
7564                "};",
7565                Style);
7566   verifyFormat("typedef union Foo\n"
7567                "{\n"
7568                "} Foo_t;",
7569                Style);
7570 }
7571 
7572 TEST_F(FormatTest, SplitEmptyNamespace) {
7573   FormatStyle Style = getLLVMStyle();
7574   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7575   Style.BraceWrapping.AfterNamespace = true;
7576   Style.BraceWrapping.SplitEmptyNamespace = false;
7577 
7578   verifyFormat("namespace Foo\n"
7579                "{};",
7580                Style);
7581   verifyFormat("/* something */ namespace Foo\n"
7582                "{};",
7583                Style);
7584   verifyFormat("inline namespace Foo\n"
7585                "{};",
7586                Style);
7587   verifyFormat("namespace Foo\n"
7588                "{\n"
7589                "void Bar();\n"
7590                "};",
7591                Style);
7592 }
7593 
7594 TEST_F(FormatTest, NeverMergeShortRecords) {
7595   FormatStyle Style = getLLVMStyle();
7596 
7597   verifyFormat("class Foo {\n"
7598                "  Foo();\n"
7599                "};",
7600                Style);
7601   verifyFormat("typedef class Foo {\n"
7602                "  Foo();\n"
7603                "} Foo_t;",
7604                Style);
7605   verifyFormat("struct Foo {\n"
7606                "  Foo();\n"
7607                "};",
7608                Style);
7609   verifyFormat("typedef struct Foo {\n"
7610                "  Foo();\n"
7611                "} Foo_t;",
7612                Style);
7613   verifyFormat("union Foo {\n"
7614                "  A,\n"
7615                "};",
7616                Style);
7617   verifyFormat("typedef union Foo {\n"
7618                "  A,\n"
7619                "} Foo_t;",
7620                Style);
7621   verifyFormat("namespace Foo {\n"
7622                "void Bar();\n"
7623                "};",
7624                Style);
7625 
7626   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7627   Style.BraceWrapping.AfterClass = true;
7628   Style.BraceWrapping.AfterStruct = true;
7629   Style.BraceWrapping.AfterUnion = true;
7630   Style.BraceWrapping.AfterNamespace = true;
7631   verifyFormat("class Foo\n"
7632                "{\n"
7633                "  Foo();\n"
7634                "};",
7635                Style);
7636   verifyFormat("typedef class Foo\n"
7637                "{\n"
7638                "  Foo();\n"
7639                "} Foo_t;",
7640                Style);
7641   verifyFormat("struct Foo\n"
7642                "{\n"
7643                "  Foo();\n"
7644                "};",
7645                Style);
7646   verifyFormat("typedef struct Foo\n"
7647                "{\n"
7648                "  Foo();\n"
7649                "} Foo_t;",
7650                Style);
7651   verifyFormat("union Foo\n"
7652                "{\n"
7653                "  A,\n"
7654                "};",
7655                Style);
7656   verifyFormat("typedef union Foo\n"
7657                "{\n"
7658                "  A,\n"
7659                "} Foo_t;",
7660                Style);
7661   verifyFormat("namespace Foo\n"
7662                "{\n"
7663                "void Bar();\n"
7664                "};",
7665                Style);
7666 }
7667 
7668 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
7669   // Elaborate type variable declarations.
7670   verifyFormat("struct foo a = {bar};\nint n;");
7671   verifyFormat("class foo a = {bar};\nint n;");
7672   verifyFormat("union foo a = {bar};\nint n;");
7673 
7674   // Elaborate types inside function definitions.
7675   verifyFormat("struct foo f() {}\nint n;");
7676   verifyFormat("class foo f() {}\nint n;");
7677   verifyFormat("union foo f() {}\nint n;");
7678 
7679   // Templates.
7680   verifyFormat("template <class X> void f() {}\nint n;");
7681   verifyFormat("template <struct X> void f() {}\nint n;");
7682   verifyFormat("template <union X> void f() {}\nint n;");
7683 
7684   // Actual definitions...
7685   verifyFormat("struct {\n} n;");
7686   verifyFormat(
7687       "template <template <class T, class Y>, class Z> class X {\n} n;");
7688   verifyFormat("union Z {\n  int n;\n} x;");
7689   verifyFormat("class MACRO Z {\n} n;");
7690   verifyFormat("class MACRO(X) Z {\n} n;");
7691   verifyFormat("class __attribute__(X) Z {\n} n;");
7692   verifyFormat("class __declspec(X) Z {\n} n;");
7693   verifyFormat("class A##B##C {\n} n;");
7694   verifyFormat("class alignas(16) Z {\n} n;");
7695   verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
7696   verifyFormat("class MACROA MACRO(X) Z {\n} n;");
7697 
7698   // Redefinition from nested context:
7699   verifyFormat("class A::B::C {\n} n;");
7700 
7701   // Template definitions.
7702   verifyFormat(
7703       "template <typename F>\n"
7704       "Matcher(const Matcher<F> &Other,\n"
7705       "        typename enable_if_c<is_base_of<F, T>::value &&\n"
7706       "                             !is_same<F, T>::value>::type * = 0)\n"
7707       "    : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
7708 
7709   // FIXME: This is still incorrectly handled at the formatter side.
7710   verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
7711   verifyFormat("int i = SomeFunction(a<b, a> b);");
7712 
7713   // FIXME:
7714   // This now gets parsed incorrectly as class definition.
7715   // verifyFormat("class A<int> f() {\n}\nint n;");
7716 
7717   // Elaborate types where incorrectly parsing the structural element would
7718   // break the indent.
7719   verifyFormat("if (true)\n"
7720                "  class X x;\n"
7721                "else\n"
7722                "  f();\n");
7723 
7724   // This is simply incomplete. Formatting is not important, but must not crash.
7725   verifyFormat("class A:");
7726 }
7727 
7728 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
7729   EXPECT_EQ("#error Leave     all         white!!!!! space* alone!\n",
7730             format("#error Leave     all         white!!!!! space* alone!\n"));
7731   EXPECT_EQ(
7732       "#warning Leave     all         white!!!!! space* alone!\n",
7733       format("#warning Leave     all         white!!!!! space* alone!\n"));
7734   EXPECT_EQ("#error 1", format("  #  error   1"));
7735   EXPECT_EQ("#warning 1", format("  #  warning 1"));
7736 }
7737 
7738 TEST_F(FormatTest, FormatHashIfExpressions) {
7739   verifyFormat("#if AAAA && BBBB");
7740   verifyFormat("#if (AAAA && BBBB)");
7741   verifyFormat("#elif (AAAA && BBBB)");
7742   // FIXME: Come up with a better indentation for #elif.
7743   verifyFormat(
7744       "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) &&  \\\n"
7745       "    defined(BBBBBBBB)\n"
7746       "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) &&  \\\n"
7747       "    defined(BBBBBBBB)\n"
7748       "#endif",
7749       getLLVMStyleWithColumns(65));
7750 }
7751 
7752 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
7753   FormatStyle AllowsMergedIf = getGoogleStyle();
7754   AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
7755   verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
7756   verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
7757   verifyFormat("if (true)\n#error E\n  return 42;", AllowsMergedIf);
7758   EXPECT_EQ("if (true) return 42;",
7759             format("if (true)\nreturn 42;", AllowsMergedIf));
7760   FormatStyle ShortMergedIf = AllowsMergedIf;
7761   ShortMergedIf.ColumnLimit = 25;
7762   verifyFormat("#define A \\\n"
7763                "  if (true) return 42;",
7764                ShortMergedIf);
7765   verifyFormat("#define A \\\n"
7766                "  f();    \\\n"
7767                "  if (true)\n"
7768                "#define B",
7769                ShortMergedIf);
7770   verifyFormat("#define A \\\n"
7771                "  f();    \\\n"
7772                "  if (true)\n"
7773                "g();",
7774                ShortMergedIf);
7775   verifyFormat("{\n"
7776                "#ifdef A\n"
7777                "  // Comment\n"
7778                "  if (true) continue;\n"
7779                "#endif\n"
7780                "  // Comment\n"
7781                "  if (true) continue;\n"
7782                "}",
7783                ShortMergedIf);
7784   ShortMergedIf.ColumnLimit = 33;
7785   verifyFormat("#define A \\\n"
7786                "  if constexpr (true) return 42;",
7787                ShortMergedIf);
7788   ShortMergedIf.ColumnLimit = 29;
7789   verifyFormat("#define A                   \\\n"
7790                "  if (aaaaaaaaaa) return 1; \\\n"
7791                "  return 2;",
7792                ShortMergedIf);
7793   ShortMergedIf.ColumnLimit = 28;
7794   verifyFormat("#define A         \\\n"
7795                "  if (aaaaaaaaaa) \\\n"
7796                "    return 1;     \\\n"
7797                "  return 2;",
7798                ShortMergedIf);
7799   verifyFormat("#define A                \\\n"
7800                "  if constexpr (aaaaaaa) \\\n"
7801                "    return 1;            \\\n"
7802                "  return 2;",
7803                ShortMergedIf);
7804 }
7805 
7806 TEST_F(FormatTest, FormatStarDependingOnContext) {
7807   verifyFormat("void f(int *a);");
7808   verifyFormat("void f() { f(fint * b); }");
7809   verifyFormat("class A {\n  void f(int *a);\n};");
7810   verifyFormat("class A {\n  int *a;\n};");
7811   verifyFormat("namespace a {\n"
7812                "namespace b {\n"
7813                "class A {\n"
7814                "  void f() {}\n"
7815                "  int *a;\n"
7816                "};\n"
7817                "} // namespace b\n"
7818                "} // namespace a");
7819 }
7820 
7821 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
7822   verifyFormat("while");
7823   verifyFormat("operator");
7824 }
7825 
7826 TEST_F(FormatTest, SkipsDeeplyNestedLines) {
7827   // This code would be painfully slow to format if we didn't skip it.
7828   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
7829                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
7830                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
7831                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
7832                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
7833                    "A(1, 1)\n"
7834                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x
7835                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7836                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7837                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7838                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7839                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7840                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7841                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7842                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7843                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n");
7844   // Deeply nested part is untouched, rest is formatted.
7845   EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n",
7846             format(std::string("int    i;\n") + Code + "int    j;\n",
7847                    getLLVMStyle(), SC_ExpectIncomplete));
7848 }
7849 
7850 //===----------------------------------------------------------------------===//
7851 // Objective-C tests.
7852 //===----------------------------------------------------------------------===//
7853 
7854 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
7855   verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
7856   EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
7857             format("-(NSUInteger)indexOfObject:(id)anObject;"));
7858   EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
7859   EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
7860   EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
7861             format("-(NSInteger)Method3:(id)anObject;"));
7862   EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
7863             format("-(NSInteger)Method4:(id)anObject;"));
7864   EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
7865             format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
7866   EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
7867             format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
7868   EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject "
7869             "forAllCells:(BOOL)flag;",
7870             format("- (void)sendAction:(SEL)aSelector to:(id)anObject "
7871                    "forAllCells:(BOOL)flag;"));
7872 
7873   // Very long objectiveC method declaration.
7874   verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
7875                "    (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
7876   verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
7877                "                    inRange:(NSRange)range\n"
7878                "                   outRange:(NSRange)out_range\n"
7879                "                  outRange1:(NSRange)out_range1\n"
7880                "                  outRange2:(NSRange)out_range2\n"
7881                "                  outRange3:(NSRange)out_range3\n"
7882                "                  outRange4:(NSRange)out_range4\n"
7883                "                  outRange5:(NSRange)out_range5\n"
7884                "                  outRange6:(NSRange)out_range6\n"
7885                "                  outRange7:(NSRange)out_range7\n"
7886                "                  outRange8:(NSRange)out_range8\n"
7887                "                  outRange9:(NSRange)out_range9;");
7888 
7889   // When the function name has to be wrapped.
7890   FormatStyle Style = getLLVMStyle();
7891   // ObjC ignores IndentWrappedFunctionNames when wrapping methods
7892   // and always indents instead.
7893   Style.IndentWrappedFunctionNames = false;
7894   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
7895                "    veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
7896                "               anotherName:(NSString)bbbbbbbbbbbbbb {\n"
7897                "}",
7898                Style);
7899   Style.IndentWrappedFunctionNames = true;
7900   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
7901                "    veryLooooooooooongName:(NSString)cccccccccccccc\n"
7902                "               anotherName:(NSString)dddddddddddddd {\n"
7903                "}",
7904                Style);
7905 
7906   verifyFormat("- (int)sum:(vector<int>)numbers;");
7907   verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
7908   // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
7909   // protocol lists (but not for template classes):
7910   // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
7911 
7912   verifyFormat("- (int (*)())foo:(int (*)())f;");
7913   verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
7914 
7915   // If there's no return type (very rare in practice!), LLVM and Google style
7916   // agree.
7917   verifyFormat("- foo;");
7918   verifyFormat("- foo:(int)f;");
7919   verifyGoogleFormat("- foo:(int)foo;");
7920 }
7921 
7922 
7923 TEST_F(FormatTest, BreaksStringLiterals) {
7924   EXPECT_EQ("\"some text \"\n"
7925             "\"other\";",
7926             format("\"some text other\";", getLLVMStyleWithColumns(12)));
7927   EXPECT_EQ("\"some text \"\n"
7928             "\"other\";",
7929             format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
7930   EXPECT_EQ(
7931       "#define A  \\\n"
7932       "  \"some \"  \\\n"
7933       "  \"text \"  \\\n"
7934       "  \"other\";",
7935       format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
7936   EXPECT_EQ(
7937       "#define A  \\\n"
7938       "  \"so \"    \\\n"
7939       "  \"text \"  \\\n"
7940       "  \"other\";",
7941       format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
7942 
7943   EXPECT_EQ("\"some text\"",
7944             format("\"some text\"", getLLVMStyleWithColumns(1)));
7945   EXPECT_EQ("\"some text\"",
7946             format("\"some text\"", getLLVMStyleWithColumns(11)));
7947   EXPECT_EQ("\"some \"\n"
7948             "\"text\"",
7949             format("\"some text\"", getLLVMStyleWithColumns(10)));
7950   EXPECT_EQ("\"some \"\n"
7951             "\"text\"",
7952             format("\"some text\"", getLLVMStyleWithColumns(7)));
7953   EXPECT_EQ("\"some\"\n"
7954             "\" tex\"\n"
7955             "\"t\"",
7956             format("\"some text\"", getLLVMStyleWithColumns(6)));
7957   EXPECT_EQ("\"some\"\n"
7958             "\" tex\"\n"
7959             "\" and\"",
7960             format("\"some tex and\"", getLLVMStyleWithColumns(6)));
7961   EXPECT_EQ("\"some\"\n"
7962             "\"/tex\"\n"
7963             "\"/and\"",
7964             format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
7965 
7966   EXPECT_EQ("variable =\n"
7967             "    \"long string \"\n"
7968             "    \"literal\";",
7969             format("variable = \"long string literal\";",
7970                    getLLVMStyleWithColumns(20)));
7971 
7972   EXPECT_EQ("variable = f(\n"
7973             "    \"long string \"\n"
7974             "    \"literal\",\n"
7975             "    short,\n"
7976             "    loooooooooooooooooooong);",
7977             format("variable = f(\"long string literal\", short, "
7978                    "loooooooooooooooooooong);",
7979                    getLLVMStyleWithColumns(20)));
7980 
7981   EXPECT_EQ(
7982       "f(g(\"long string \"\n"
7983       "    \"literal\"),\n"
7984       "  b);",
7985       format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20)));
7986   EXPECT_EQ("f(g(\"long string \"\n"
7987             "    \"literal\",\n"
7988             "    a),\n"
7989             "  b);",
7990             format("f(g(\"long string literal\", a), b);",
7991                    getLLVMStyleWithColumns(20)));
7992   EXPECT_EQ(
7993       "f(\"one two\".split(\n"
7994       "    variable));",
7995       format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
7996   EXPECT_EQ("f(\"one two three four five six \"\n"
7997             "  \"seven\".split(\n"
7998             "      really_looooong_variable));",
7999             format("f(\"one two three four five six seven\"."
8000                    "split(really_looooong_variable));",
8001                    getLLVMStyleWithColumns(33)));
8002 
8003   EXPECT_EQ("f(\"some \"\n"
8004             "  \"text\",\n"
8005             "  other);",
8006             format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
8007 
8008   // Only break as a last resort.
8009   verifyFormat(
8010       "aaaaaaaaaaaaaaaaaaaa(\n"
8011       "    aaaaaaaaaaaaaaaaaaaa,\n"
8012       "    aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
8013 
8014   EXPECT_EQ("\"splitmea\"\n"
8015             "\"trandomp\"\n"
8016             "\"oint\"",
8017             format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
8018 
8019   EXPECT_EQ("\"split/\"\n"
8020             "\"pathat/\"\n"
8021             "\"slashes\"",
8022             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
8023 
8024   EXPECT_EQ("\"split/\"\n"
8025             "\"pathat/\"\n"
8026             "\"slashes\"",
8027             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
8028   EXPECT_EQ("\"split at \"\n"
8029             "\"spaces/at/\"\n"
8030             "\"slashes.at.any$\"\n"
8031             "\"non-alphanumeric%\"\n"
8032             "\"1111111111characte\"\n"
8033             "\"rs\"",
8034             format("\"split at "
8035                    "spaces/at/"
8036                    "slashes.at."
8037                    "any$non-"
8038                    "alphanumeric%"
8039                    "1111111111characte"
8040                    "rs\"",
8041                    getLLVMStyleWithColumns(20)));
8042 
8043   // Verify that splitting the strings understands
8044   // Style::AlwaysBreakBeforeMultilineStrings.
8045   EXPECT_EQ(
8046       "aaaaaaaaaaaa(\n"
8047       "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
8048       "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
8049       format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
8050              "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
8051              "aaaaaaaaaaaaaaaaaaaaaa\");",
8052              getGoogleStyle()));
8053   EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
8054             "       \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
8055             format("return \"aaaaaaaaaaaaaaaaaaaaaa "
8056                    "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
8057                    "aaaaaaaaaaaaaaaaaaaaaa\";",
8058                    getGoogleStyle()));
8059   EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
8060             "                \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
8061             format("llvm::outs() << "
8062                    "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
8063                    "aaaaaaaaaaaaaaaaaaa\";"));
8064   EXPECT_EQ("ffff(\n"
8065             "    {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
8066             "     \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
8067             format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
8068                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
8069                    getGoogleStyle()));
8070 
8071   FormatStyle Style = getLLVMStyleWithColumns(12);
8072   Style.BreakStringLiterals = false;
8073   EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style));
8074 
8075   FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
8076   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
8077   EXPECT_EQ("#define A \\\n"
8078             "  \"some \" \\\n"
8079             "  \"text \" \\\n"
8080             "  \"other\";",
8081             format("#define A \"some text other\";", AlignLeft));
8082 }
8083 
8084 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) {
8085   EXPECT_EQ("C a = \"some more \"\n"
8086             "      \"text\";",
8087             format("C a = \"some more text\";", getLLVMStyleWithColumns(18)));
8088 }
8089 
8090 TEST_F(FormatTest, FullyRemoveEmptyLines) {
8091   FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80);
8092   NoEmptyLines.MaxEmptyLinesToKeep = 0;
8093   EXPECT_EQ("int i = a(b());",
8094             format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines));
8095 }
8096 
8097 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
8098   EXPECT_EQ(
8099       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
8100       "(\n"
8101       "    \"x\t\");",
8102       format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
8103              "aaaaaaa("
8104              "\"x\t\");"));
8105 }
8106 
8107 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
8108   EXPECT_EQ(
8109       "u8\"utf8 string \"\n"
8110       "u8\"literal\";",
8111       format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
8112   EXPECT_EQ(
8113       "u\"utf16 string \"\n"
8114       "u\"literal\";",
8115       format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
8116   EXPECT_EQ(
8117       "U\"utf32 string \"\n"
8118       "U\"literal\";",
8119       format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
8120   EXPECT_EQ("L\"wide string \"\n"
8121             "L\"literal\";",
8122             format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
8123   EXPECT_EQ("@\"NSString \"\n"
8124             "@\"literal\";",
8125             format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
8126   verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26));
8127 
8128   // This input makes clang-format try to split the incomplete unicode escape
8129   // sequence, which used to lead to a crasher.
8130   verifyNoCrash(
8131       "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
8132       getLLVMStyleWithColumns(60));
8133 }
8134 
8135 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
8136   FormatStyle Style = getGoogleStyleWithColumns(15);
8137   EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
8138   EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
8139   EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
8140   EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
8141   EXPECT_EQ("u8R\"x(raw literal)x\";",
8142             format("u8R\"x(raw literal)x\";", Style));
8143 }
8144 
8145 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
8146   FormatStyle Style = getLLVMStyleWithColumns(20);
8147   EXPECT_EQ(
8148       "_T(\"aaaaaaaaaaaaaa\")\n"
8149       "_T(\"aaaaaaaaaaaaaa\")\n"
8150       "_T(\"aaaaaaaaaaaa\")",
8151       format("  _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
8152   EXPECT_EQ("f(x,\n"
8153             "  _T(\"aaaaaaaaaaaa\")\n"
8154             "  _T(\"aaa\"),\n"
8155             "  z);",
8156             format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
8157 
8158   // FIXME: Handle embedded spaces in one iteration.
8159   //  EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
8160   //            "_T(\"aaaaaaaaaaaaa\")\n"
8161   //            "_T(\"aaaaaaaaaaaaa\")\n"
8162   //            "_T(\"a\")",
8163   //            format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
8164   //                   getLLVMStyleWithColumns(20)));
8165   EXPECT_EQ(
8166       "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
8167       format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
8168   EXPECT_EQ("f(\n"
8169             "#if !TEST\n"
8170             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
8171             "#endif\n"
8172             ");",
8173             format("f(\n"
8174                    "#if !TEST\n"
8175                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
8176                    "#endif\n"
8177                    ");"));
8178   EXPECT_EQ("f(\n"
8179             "\n"
8180             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
8181             format("f(\n"
8182                    "\n"
8183                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"));
8184 }
8185 
8186 TEST_F(FormatTest, BreaksStringLiteralOperands) {
8187   // In a function call with two operands, the second can be broken with no line
8188   // break before it.
8189   EXPECT_EQ("func(a, \"long long \"\n"
8190             "        \"long long\");",
8191             format("func(a, \"long long long long\");",
8192                    getLLVMStyleWithColumns(24)));
8193   // In a function call with three operands, the second must be broken with a
8194   // line break before it.
8195   EXPECT_EQ("func(a,\n"
8196             "     \"long long long \"\n"
8197             "     \"long\",\n"
8198             "     c);",
8199             format("func(a, \"long long long long\", c);",
8200                    getLLVMStyleWithColumns(24)));
8201   // In a function call with three operands, the third must be broken with a
8202   // line break before it.
8203   EXPECT_EQ("func(a, b,\n"
8204             "     \"long long long \"\n"
8205             "     \"long\");",
8206             format("func(a, b, \"long long long long\");",
8207                    getLLVMStyleWithColumns(24)));
8208   // In a function call with three operands, both the second and the third must
8209   // be broken with a line break before them.
8210   EXPECT_EQ("func(a,\n"
8211             "     \"long long long \"\n"
8212             "     \"long\",\n"
8213             "     \"long long long \"\n"
8214             "     \"long\");",
8215             format("func(a, \"long long long long\", \"long long long long\");",
8216                    getLLVMStyleWithColumns(24)));
8217   // In a chain of << with two operands, the second can be broken with no line
8218   // break before it.
8219   EXPECT_EQ("a << \"line line \"\n"
8220             "     \"line\";",
8221             format("a << \"line line line\";",
8222                    getLLVMStyleWithColumns(20)));
8223   // In a chain of << with three operands, the second can be broken with no line
8224   // break before it.
8225   EXPECT_EQ("abcde << \"line \"\n"
8226             "         \"line line\"\n"
8227             "      << c;",
8228             format("abcde << \"line line line\" << c;",
8229                    getLLVMStyleWithColumns(20)));
8230   // In a chain of << with three operands, the third must be broken with a line
8231   // break before it.
8232   EXPECT_EQ("a << b\n"
8233             "  << \"line line \"\n"
8234             "     \"line\";",
8235             format("a << b << \"line line line\";",
8236                    getLLVMStyleWithColumns(20)));
8237   // In a chain of << with three operands, the second can be broken with no line
8238   // break before it and the third must be broken with a line break before it.
8239   EXPECT_EQ("abcd << \"line line \"\n"
8240             "        \"line\"\n"
8241             "     << \"line line \"\n"
8242             "        \"line\";",
8243             format("abcd << \"line line line\" << \"line line line\";",
8244                    getLLVMStyleWithColumns(20)));
8245   // In a chain of binary operators with two operands, the second can be broken
8246   // with no line break before it.
8247   EXPECT_EQ("abcd + \"line line \"\n"
8248             "       \"line line\";",
8249             format("abcd + \"line line line line\";",
8250                    getLLVMStyleWithColumns(20)));
8251   // In a chain of binary operators with three operands, the second must be
8252   // broken with a line break before it.
8253   EXPECT_EQ("abcd +\n"
8254             "    \"line line \"\n"
8255             "    \"line line\" +\n"
8256             "    e;",
8257             format("abcd + \"line line line line\" + e;",
8258                    getLLVMStyleWithColumns(20)));
8259   // In a function call with two operands, with AlignAfterOpenBracket enabled,
8260   // the first must be broken with a line break before it.
8261   FormatStyle Style = getLLVMStyleWithColumns(25);
8262   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
8263   EXPECT_EQ("someFunction(\n"
8264             "    \"long long long \"\n"
8265             "    \"long\",\n"
8266             "    a);",
8267             format("someFunction(\"long long long long\", a);", Style));
8268 }
8269 
8270 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
8271   EXPECT_EQ(
8272       "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
8273       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
8274       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
8275       format("aaaaaaaaaaa  =  \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
8276              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
8277              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
8278 }
8279 
8280 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
8281   EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
8282             format("f(g(R\"x(raw literal)x\",   a), b);", getGoogleStyle()));
8283   EXPECT_EQ("fffffffffff(g(R\"x(\n"
8284             "multiline raw string literal xxxxxxxxxxxxxx\n"
8285             ")x\",\n"
8286             "              a),\n"
8287             "            b);",
8288             format("fffffffffff(g(R\"x(\n"
8289                    "multiline raw string literal xxxxxxxxxxxxxx\n"
8290                    ")x\", a), b);",
8291                    getGoogleStyleWithColumns(20)));
8292   EXPECT_EQ("fffffffffff(\n"
8293             "    g(R\"x(qqq\n"
8294             "multiline raw string literal xxxxxxxxxxxxxx\n"
8295             ")x\",\n"
8296             "      a),\n"
8297             "    b);",
8298             format("fffffffffff(g(R\"x(qqq\n"
8299                    "multiline raw string literal xxxxxxxxxxxxxx\n"
8300                    ")x\", a), b);",
8301                    getGoogleStyleWithColumns(20)));
8302 
8303   EXPECT_EQ("fffffffffff(R\"x(\n"
8304             "multiline raw string literal xxxxxxxxxxxxxx\n"
8305             ")x\");",
8306             format("fffffffffff(R\"x(\n"
8307                    "multiline raw string literal xxxxxxxxxxxxxx\n"
8308                    ")x\");",
8309                    getGoogleStyleWithColumns(20)));
8310   EXPECT_EQ("fffffffffff(R\"x(\n"
8311             "multiline raw string literal xxxxxxxxxxxxxx\n"
8312             ")x\" + bbbbbb);",
8313             format("fffffffffff(R\"x(\n"
8314                    "multiline raw string literal xxxxxxxxxxxxxx\n"
8315                    ")x\" +   bbbbbb);",
8316                    getGoogleStyleWithColumns(20)));
8317   EXPECT_EQ("fffffffffff(\n"
8318             "    R\"x(\n"
8319             "multiline raw string literal xxxxxxxxxxxxxx\n"
8320             ")x\" +\n"
8321             "    bbbbbb);",
8322             format("fffffffffff(\n"
8323                    " R\"x(\n"
8324                    "multiline raw string literal xxxxxxxxxxxxxx\n"
8325                    ")x\" + bbbbbb);",
8326                    getGoogleStyleWithColumns(20)));
8327   EXPECT_EQ("fffffffffff(R\"(single line raw string)\" + bbbbbb);",
8328             format("fffffffffff(\n"
8329                    " R\"(single line raw string)\" + bbbbbb);"));
8330 }
8331 
8332 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
8333   verifyFormat("string a = \"unterminated;");
8334   EXPECT_EQ("function(\"unterminated,\n"
8335             "         OtherParameter);",
8336             format("function(  \"unterminated,\n"
8337                    "    OtherParameter);"));
8338 }
8339 
8340 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
8341   FormatStyle Style = getLLVMStyle();
8342   Style.Standard = FormatStyle::LS_Cpp03;
8343   EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
8344             format("#define x(_a) printf(\"foo\"_a);", Style));
8345 }
8346 
8347 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
8348 
8349 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
8350   EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
8351             "             \"ddeeefff\");",
8352             format("someFunction(\"aaabbbcccdddeeefff\");",
8353                    getLLVMStyleWithColumns(25)));
8354   EXPECT_EQ("someFunction1234567890(\n"
8355             "    \"aaabbbcccdddeeefff\");",
8356             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
8357                    getLLVMStyleWithColumns(26)));
8358   EXPECT_EQ("someFunction1234567890(\n"
8359             "    \"aaabbbcccdddeeeff\"\n"
8360             "    \"f\");",
8361             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
8362                    getLLVMStyleWithColumns(25)));
8363   EXPECT_EQ("someFunction1234567890(\n"
8364             "    \"aaabbbcccdddeeeff\"\n"
8365             "    \"f\");",
8366             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
8367                    getLLVMStyleWithColumns(24)));
8368   EXPECT_EQ("someFunction(\n"
8369             "    \"aaabbbcc ddde \"\n"
8370             "    \"efff\");",
8371             format("someFunction(\"aaabbbcc ddde efff\");",
8372                    getLLVMStyleWithColumns(25)));
8373   EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
8374             "             \"ddeeefff\");",
8375             format("someFunction(\"aaabbbccc ddeeefff\");",
8376                    getLLVMStyleWithColumns(25)));
8377   EXPECT_EQ("someFunction1234567890(\n"
8378             "    \"aaabb \"\n"
8379             "    \"cccdddeeefff\");",
8380             format("someFunction1234567890(\"aaabb cccdddeeefff\");",
8381                    getLLVMStyleWithColumns(25)));
8382   EXPECT_EQ("#define A          \\\n"
8383             "  string s =       \\\n"
8384             "      \"123456789\"  \\\n"
8385             "      \"0\";         \\\n"
8386             "  int i;",
8387             format("#define A string s = \"1234567890\"; int i;",
8388                    getLLVMStyleWithColumns(20)));
8389   EXPECT_EQ("someFunction(\n"
8390             "    \"aaabbbcc \"\n"
8391             "    \"dddeeefff\");",
8392             format("someFunction(\"aaabbbcc dddeeefff\");",
8393                    getLLVMStyleWithColumns(25)));
8394 }
8395 
8396 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
8397   EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3)));
8398   EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2)));
8399   EXPECT_EQ("\"test\"\n"
8400             "\"\\n\"",
8401             format("\"test\\n\"", getLLVMStyleWithColumns(7)));
8402   EXPECT_EQ("\"tes\\\\\"\n"
8403             "\"n\"",
8404             format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
8405   EXPECT_EQ("\"\\\\\\\\\"\n"
8406             "\"\\n\"",
8407             format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
8408   EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
8409   EXPECT_EQ("\"\\uff01\"\n"
8410             "\"test\"",
8411             format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
8412   EXPECT_EQ("\"\\Uff01ff02\"",
8413             format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
8414   EXPECT_EQ("\"\\x000000000001\"\n"
8415             "\"next\"",
8416             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
8417   EXPECT_EQ("\"\\x000000000001next\"",
8418             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
8419   EXPECT_EQ("\"\\x000000000001\"",
8420             format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
8421   EXPECT_EQ("\"test\"\n"
8422             "\"\\000000\"\n"
8423             "\"000001\"",
8424             format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
8425   EXPECT_EQ("\"test\\000\"\n"
8426             "\"00000000\"\n"
8427             "\"1\"",
8428             format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
8429 }
8430 
8431 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
8432   verifyFormat("void f() {\n"
8433                "  return g() {}\n"
8434                "  void h() {}");
8435   verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
8436                "g();\n"
8437                "}");
8438 }
8439 
8440 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
8441   verifyFormat(
8442       "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
8443 }
8444 
8445 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
8446   verifyFormat("class X {\n"
8447                "  void f() {\n"
8448                "  }\n"
8449                "};",
8450                getLLVMStyleWithColumns(12));
8451 }
8452 
8453 TEST_F(FormatTest, ConfigurableIndentWidth) {
8454   FormatStyle EightIndent = getLLVMStyleWithColumns(18);
8455   EightIndent.IndentWidth = 8;
8456   EightIndent.ContinuationIndentWidth = 8;
8457   verifyFormat("void f() {\n"
8458                "        someFunction();\n"
8459                "        if (true) {\n"
8460                "                f();\n"
8461                "        }\n"
8462                "}",
8463                EightIndent);
8464   verifyFormat("class X {\n"
8465                "        void f() {\n"
8466                "        }\n"
8467                "};",
8468                EightIndent);
8469   verifyFormat("int x[] = {\n"
8470                "        call(),\n"
8471                "        call()};",
8472                EightIndent);
8473 }
8474 
8475 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
8476   verifyFormat("double\n"
8477                "f();",
8478                getLLVMStyleWithColumns(8));
8479 }
8480 
8481 TEST_F(FormatTest, ConfigurableUseOfTab) {
8482   FormatStyle Tab = getLLVMStyleWithColumns(42);
8483   Tab.IndentWidth = 8;
8484   Tab.UseTab = FormatStyle::UT_Always;
8485   Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left;
8486 
8487   EXPECT_EQ("if (aaaaaaaa && // q\n"
8488             "    bb)\t\t// w\n"
8489             "\t;",
8490             format("if (aaaaaaaa &&// q\n"
8491                    "bb)// w\n"
8492                    ";",
8493                    Tab));
8494   EXPECT_EQ("if (aaa && bbb) // w\n"
8495             "\t;",
8496             format("if(aaa&&bbb)// w\n"
8497                    ";",
8498                    Tab));
8499 
8500   verifyFormat("class X {\n"
8501                "\tvoid f() {\n"
8502                "\t\tsomeFunction(parameter1,\n"
8503                "\t\t\t     parameter2);\n"
8504                "\t}\n"
8505                "};",
8506                Tab);
8507   verifyFormat("#define A                        \\\n"
8508                "\tvoid f() {               \\\n"
8509                "\t\tsomeFunction(    \\\n"
8510                "\t\t    parameter1,  \\\n"
8511                "\t\t    parameter2); \\\n"
8512                "\t}",
8513                Tab);
8514 
8515   Tab.TabWidth = 4;
8516   Tab.IndentWidth = 8;
8517   verifyFormat("class TabWidth4Indent8 {\n"
8518                "\t\tvoid f() {\n"
8519                "\t\t\t\tsomeFunction(parameter1,\n"
8520                "\t\t\t\t\t\t\t parameter2);\n"
8521                "\t\t}\n"
8522                "};",
8523                Tab);
8524 
8525   Tab.TabWidth = 4;
8526   Tab.IndentWidth = 4;
8527   verifyFormat("class TabWidth4Indent4 {\n"
8528                "\tvoid f() {\n"
8529                "\t\tsomeFunction(parameter1,\n"
8530                "\t\t\t\t\t parameter2);\n"
8531                "\t}\n"
8532                "};",
8533                Tab);
8534 
8535   Tab.TabWidth = 8;
8536   Tab.IndentWidth = 4;
8537   verifyFormat("class TabWidth8Indent4 {\n"
8538                "    void f() {\n"
8539                "\tsomeFunction(parameter1,\n"
8540                "\t\t     parameter2);\n"
8541                "    }\n"
8542                "};",
8543                Tab);
8544 
8545   Tab.TabWidth = 8;
8546   Tab.IndentWidth = 8;
8547   EXPECT_EQ("/*\n"
8548             "\t      a\t\tcomment\n"
8549             "\t      in multiple lines\n"
8550             "       */",
8551             format("   /*\t \t \n"
8552                    " \t \t a\t\tcomment\t \t\n"
8553                    " \t \t in multiple lines\t\n"
8554                    " \t  */",
8555                    Tab));
8556 
8557   Tab.UseTab = FormatStyle::UT_ForIndentation;
8558   verifyFormat("{\n"
8559                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8560                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8561                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8562                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8563                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8564                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8565                "};",
8566                Tab);
8567   verifyFormat("enum AA {\n"
8568                "\ta1, // Force multiple lines\n"
8569                "\ta2,\n"
8570                "\ta3\n"
8571                "};",
8572                Tab);
8573   EXPECT_EQ("if (aaaaaaaa && // q\n"
8574             "    bb)         // w\n"
8575             "\t;",
8576             format("if (aaaaaaaa &&// q\n"
8577                    "bb)// w\n"
8578                    ";",
8579                    Tab));
8580   verifyFormat("class X {\n"
8581                "\tvoid f() {\n"
8582                "\t\tsomeFunction(parameter1,\n"
8583                "\t\t             parameter2);\n"
8584                "\t}\n"
8585                "};",
8586                Tab);
8587   verifyFormat("{\n"
8588                "\tQ(\n"
8589                "\t    {\n"
8590                "\t\t    int a;\n"
8591                "\t\t    someFunction(aaaaaaaa,\n"
8592                "\t\t                 bbbbbbb);\n"
8593                "\t    },\n"
8594                "\t    p);\n"
8595                "}",
8596                Tab);
8597   EXPECT_EQ("{\n"
8598             "\t/* aaaa\n"
8599             "\t   bbbb */\n"
8600             "}",
8601             format("{\n"
8602                    "/* aaaa\n"
8603                    "   bbbb */\n"
8604                    "}",
8605                    Tab));
8606   EXPECT_EQ("{\n"
8607             "\t/*\n"
8608             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8609             "\t  bbbbbbbbbbbbb\n"
8610             "\t*/\n"
8611             "}",
8612             format("{\n"
8613                    "/*\n"
8614                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8615                    "*/\n"
8616                    "}",
8617                    Tab));
8618   EXPECT_EQ("{\n"
8619             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8620             "\t// bbbbbbbbbbbbb\n"
8621             "}",
8622             format("{\n"
8623                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8624                    "}",
8625                    Tab));
8626   EXPECT_EQ("{\n"
8627             "\t/*\n"
8628             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8629             "\t  bbbbbbbbbbbbb\n"
8630             "\t*/\n"
8631             "}",
8632             format("{\n"
8633                    "\t/*\n"
8634                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8635                    "\t*/\n"
8636                    "}",
8637                    Tab));
8638   EXPECT_EQ("{\n"
8639             "\t/*\n"
8640             "\n"
8641             "\t*/\n"
8642             "}",
8643             format("{\n"
8644                    "\t/*\n"
8645                    "\n"
8646                    "\t*/\n"
8647                    "}",
8648                    Tab));
8649   EXPECT_EQ("{\n"
8650             "\t/*\n"
8651             " asdf\n"
8652             "\t*/\n"
8653             "}",
8654             format("{\n"
8655                    "\t/*\n"
8656                    " asdf\n"
8657                    "\t*/\n"
8658                    "}",
8659                    Tab));
8660 
8661   Tab.UseTab = FormatStyle::UT_Never;
8662   EXPECT_EQ("/*\n"
8663             "              a\t\tcomment\n"
8664             "              in multiple lines\n"
8665             "       */",
8666             format("   /*\t \t \n"
8667                    " \t \t a\t\tcomment\t \t\n"
8668                    " \t \t in multiple lines\t\n"
8669                    " \t  */",
8670                    Tab));
8671   EXPECT_EQ("/* some\n"
8672             "   comment */",
8673             format(" \t \t /* some\n"
8674                    " \t \t    comment */",
8675                    Tab));
8676   EXPECT_EQ("int a; /* some\n"
8677             "   comment */",
8678             format(" \t \t int a; /* some\n"
8679                    " \t \t    comment */",
8680                    Tab));
8681 
8682   EXPECT_EQ("int a; /* some\n"
8683             "comment */",
8684             format(" \t \t int\ta; /* some\n"
8685                    " \t \t    comment */",
8686                    Tab));
8687   EXPECT_EQ("f(\"\t\t\"); /* some\n"
8688             "    comment */",
8689             format(" \t \t f(\"\t\t\"); /* some\n"
8690                    " \t \t    comment */",
8691                    Tab));
8692   EXPECT_EQ("{\n"
8693             "  /*\n"
8694             "   * Comment\n"
8695             "   */\n"
8696             "  int i;\n"
8697             "}",
8698             format("{\n"
8699                    "\t/*\n"
8700                    "\t * Comment\n"
8701                    "\t */\n"
8702                    "\t int i;\n"
8703                    "}"));
8704 
8705   Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
8706   Tab.TabWidth = 8;
8707   Tab.IndentWidth = 8;
8708   EXPECT_EQ("if (aaaaaaaa && // q\n"
8709             "    bb)         // w\n"
8710             "\t;",
8711             format("if (aaaaaaaa &&// q\n"
8712                    "bb)// w\n"
8713                    ";",
8714                    Tab));
8715   EXPECT_EQ("if (aaa && bbb) // w\n"
8716             "\t;",
8717             format("if(aaa&&bbb)// w\n"
8718                    ";",
8719                    Tab));
8720   verifyFormat("class X {\n"
8721                "\tvoid f() {\n"
8722                "\t\tsomeFunction(parameter1,\n"
8723                "\t\t\t     parameter2);\n"
8724                "\t}\n"
8725                "};",
8726                Tab);
8727   verifyFormat("#define A                        \\\n"
8728                "\tvoid f() {               \\\n"
8729                "\t\tsomeFunction(    \\\n"
8730                "\t\t    parameter1,  \\\n"
8731                "\t\t    parameter2); \\\n"
8732                "\t}",
8733                Tab);
8734   Tab.TabWidth = 4;
8735   Tab.IndentWidth = 8;
8736   verifyFormat("class TabWidth4Indent8 {\n"
8737                "\t\tvoid f() {\n"
8738                "\t\t\t\tsomeFunction(parameter1,\n"
8739                "\t\t\t\t\t\t\t parameter2);\n"
8740                "\t\t}\n"
8741                "};",
8742                Tab);
8743   Tab.TabWidth = 4;
8744   Tab.IndentWidth = 4;
8745   verifyFormat("class TabWidth4Indent4 {\n"
8746                "\tvoid f() {\n"
8747                "\t\tsomeFunction(parameter1,\n"
8748                "\t\t\t\t\t parameter2);\n"
8749                "\t}\n"
8750                "};",
8751                Tab);
8752   Tab.TabWidth = 8;
8753   Tab.IndentWidth = 4;
8754   verifyFormat("class TabWidth8Indent4 {\n"
8755                "    void f() {\n"
8756                "\tsomeFunction(parameter1,\n"
8757                "\t\t     parameter2);\n"
8758                "    }\n"
8759                "};",
8760                Tab);
8761   Tab.TabWidth = 8;
8762   Tab.IndentWidth = 8;
8763   EXPECT_EQ("/*\n"
8764             "\t      a\t\tcomment\n"
8765             "\t      in multiple lines\n"
8766             "       */",
8767             format("   /*\t \t \n"
8768                    " \t \t a\t\tcomment\t \t\n"
8769                    " \t \t in multiple lines\t\n"
8770                    " \t  */",
8771                    Tab));
8772   verifyFormat("{\n"
8773                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8774                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8775                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8776                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8777                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8778                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8779                "};",
8780                Tab);
8781   verifyFormat("enum AA {\n"
8782                "\ta1, // Force multiple lines\n"
8783                "\ta2,\n"
8784                "\ta3\n"
8785                "};",
8786                Tab);
8787   EXPECT_EQ("if (aaaaaaaa && // q\n"
8788             "    bb)         // w\n"
8789             "\t;",
8790             format("if (aaaaaaaa &&// q\n"
8791                    "bb)// w\n"
8792                    ";",
8793                    Tab));
8794   verifyFormat("class X {\n"
8795                "\tvoid f() {\n"
8796                "\t\tsomeFunction(parameter1,\n"
8797                "\t\t\t     parameter2);\n"
8798                "\t}\n"
8799                "};",
8800                Tab);
8801   verifyFormat("{\n"
8802                "\tQ(\n"
8803                "\t    {\n"
8804                "\t\t    int a;\n"
8805                "\t\t    someFunction(aaaaaaaa,\n"
8806                "\t\t\t\t bbbbbbb);\n"
8807                "\t    },\n"
8808                "\t    p);\n"
8809                "}",
8810                Tab);
8811   EXPECT_EQ("{\n"
8812             "\t/* aaaa\n"
8813             "\t   bbbb */\n"
8814             "}",
8815             format("{\n"
8816                    "/* aaaa\n"
8817                    "   bbbb */\n"
8818                    "}",
8819                    Tab));
8820   EXPECT_EQ("{\n"
8821             "\t/*\n"
8822             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8823             "\t  bbbbbbbbbbbbb\n"
8824             "\t*/\n"
8825             "}",
8826             format("{\n"
8827                    "/*\n"
8828                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8829                    "*/\n"
8830                    "}",
8831                    Tab));
8832   EXPECT_EQ("{\n"
8833             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8834             "\t// bbbbbbbbbbbbb\n"
8835             "}",
8836             format("{\n"
8837                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8838                    "}",
8839                    Tab));
8840   EXPECT_EQ("{\n"
8841             "\t/*\n"
8842             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8843             "\t  bbbbbbbbbbbbb\n"
8844             "\t*/\n"
8845             "}",
8846             format("{\n"
8847                    "\t/*\n"
8848                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8849                    "\t*/\n"
8850                    "}",
8851                    Tab));
8852   EXPECT_EQ("{\n"
8853             "\t/*\n"
8854             "\n"
8855             "\t*/\n"
8856             "}",
8857             format("{\n"
8858                    "\t/*\n"
8859                    "\n"
8860                    "\t*/\n"
8861                    "}",
8862                    Tab));
8863   EXPECT_EQ("{\n"
8864             "\t/*\n"
8865             " asdf\n"
8866             "\t*/\n"
8867             "}",
8868             format("{\n"
8869                    "\t/*\n"
8870                    " asdf\n"
8871                    "\t*/\n"
8872                    "}",
8873                    Tab));
8874   EXPECT_EQ("/*\n"
8875             "\t      a\t\tcomment\n"
8876             "\t      in multiple lines\n"
8877             "       */",
8878             format("   /*\t \t \n"
8879                    " \t \t a\t\tcomment\t \t\n"
8880                    " \t \t in multiple lines\t\n"
8881                    " \t  */",
8882                    Tab));
8883   EXPECT_EQ("/* some\n"
8884             "   comment */",
8885             format(" \t \t /* some\n"
8886                    " \t \t    comment */",
8887                    Tab));
8888   EXPECT_EQ("int a; /* some\n"
8889             "   comment */",
8890             format(" \t \t int a; /* some\n"
8891                    " \t \t    comment */",
8892                    Tab));
8893   EXPECT_EQ("int a; /* some\n"
8894             "comment */",
8895             format(" \t \t int\ta; /* some\n"
8896                    " \t \t    comment */",
8897                    Tab));
8898   EXPECT_EQ("f(\"\t\t\"); /* some\n"
8899             "    comment */",
8900             format(" \t \t f(\"\t\t\"); /* some\n"
8901                    " \t \t    comment */",
8902                    Tab));
8903   EXPECT_EQ("{\n"
8904             "  /*\n"
8905             "   * Comment\n"
8906             "   */\n"
8907             "  int i;\n"
8908             "}",
8909             format("{\n"
8910                    "\t/*\n"
8911                    "\t * Comment\n"
8912                    "\t */\n"
8913                    "\t int i;\n"
8914                    "}"));
8915   Tab.AlignConsecutiveAssignments = true;
8916   Tab.AlignConsecutiveDeclarations = true;
8917   Tab.TabWidth = 4;
8918   Tab.IndentWidth = 4;
8919   verifyFormat("class Assign {\n"
8920                "\tvoid f() {\n"
8921                "\t\tint         x      = 123;\n"
8922                "\t\tint         random = 4;\n"
8923                "\t\tstd::string alphabet =\n"
8924                "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
8925                "\t}\n"
8926                "};",
8927                Tab);
8928 }
8929 
8930 TEST_F(FormatTest, CalculatesOriginalColumn) {
8931   EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8932             "q\"; /* some\n"
8933             "       comment */",
8934             format("  \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8935                    "q\"; /* some\n"
8936                    "       comment */",
8937                    getLLVMStyle()));
8938   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
8939             "/* some\n"
8940             "   comment */",
8941             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
8942                    " /* some\n"
8943                    "    comment */",
8944                    getLLVMStyle()));
8945   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8946             "qqq\n"
8947             "/* some\n"
8948             "   comment */",
8949             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8950                    "qqq\n"
8951                    " /* some\n"
8952                    "    comment */",
8953                    getLLVMStyle()));
8954   EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8955             "wwww; /* some\n"
8956             "         comment */",
8957             format("  inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8958                    "wwww; /* some\n"
8959                    "         comment */",
8960                    getLLVMStyle()));
8961 }
8962 
8963 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
8964   FormatStyle NoSpace = getLLVMStyle();
8965   NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
8966 
8967   verifyFormat("while(true)\n"
8968                "  continue;",
8969                NoSpace);
8970   verifyFormat("for(;;)\n"
8971                "  continue;",
8972                NoSpace);
8973   verifyFormat("if(true)\n"
8974                "  f();\n"
8975                "else if(true)\n"
8976                "  f();",
8977                NoSpace);
8978   verifyFormat("do {\n"
8979                "  do_something();\n"
8980                "} while(something());",
8981                NoSpace);
8982   verifyFormat("switch(x) {\n"
8983                "default:\n"
8984                "  break;\n"
8985                "}",
8986                NoSpace);
8987   verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
8988   verifyFormat("size_t x = sizeof(x);", NoSpace);
8989   verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
8990   verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
8991   verifyFormat("alignas(128) char a[128];", NoSpace);
8992   verifyFormat("size_t x = alignof(MyType);", NoSpace);
8993   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
8994   verifyFormat("int f() throw(Deprecated);", NoSpace);
8995   verifyFormat("typedef void (*cb)(int);", NoSpace);
8996   verifyFormat("T A::operator()();", NoSpace);
8997   verifyFormat("X A::operator++(T);", NoSpace);
8998 
8999   FormatStyle Space = getLLVMStyle();
9000   Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
9001 
9002   verifyFormat("int f ();", Space);
9003   verifyFormat("void f (int a, T b) {\n"
9004                "  while (true)\n"
9005                "    continue;\n"
9006                "}",
9007                Space);
9008   verifyFormat("if (true)\n"
9009                "  f ();\n"
9010                "else if (true)\n"
9011                "  f ();",
9012                Space);
9013   verifyFormat("do {\n"
9014                "  do_something ();\n"
9015                "} while (something ());",
9016                Space);
9017   verifyFormat("switch (x) {\n"
9018                "default:\n"
9019                "  break;\n"
9020                "}",
9021                Space);
9022   verifyFormat("A::A () : a (1) {}", Space);
9023   verifyFormat("void f () __attribute__ ((asdf));", Space);
9024   verifyFormat("*(&a + 1);\n"
9025                "&((&a)[1]);\n"
9026                "a[(b + c) * d];\n"
9027                "(((a + 1) * 2) + 3) * 4;",
9028                Space);
9029   verifyFormat("#define A(x) x", Space);
9030   verifyFormat("#define A (x) x", Space);
9031   verifyFormat("#if defined(x)\n"
9032                "#endif",
9033                Space);
9034   verifyFormat("auto i = std::make_unique<int> (5);", Space);
9035   verifyFormat("size_t x = sizeof (x);", Space);
9036   verifyFormat("auto f (int x) -> decltype (x);", Space);
9037   verifyFormat("int f (T x) noexcept (x.create ());", Space);
9038   verifyFormat("alignas (128) char a[128];", Space);
9039   verifyFormat("size_t x = alignof (MyType);", Space);
9040   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
9041   verifyFormat("int f () throw (Deprecated);", Space);
9042   verifyFormat("typedef void (*cb) (int);", Space);
9043   verifyFormat("T A::operator() ();", Space);
9044   verifyFormat("X A::operator++ (T);", Space);
9045 }
9046 
9047 TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
9048   FormatStyle Spaces = getLLVMStyle();
9049 
9050   Spaces.SpacesInParentheses = true;
9051   verifyFormat("do_something( ::globalVar );", Spaces);
9052   verifyFormat("call( x, y, z );", Spaces);
9053   verifyFormat("call();", Spaces);
9054   verifyFormat("std::function<void( int, int )> callback;", Spaces);
9055   verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
9056                Spaces);
9057   verifyFormat("while ( (bool)1 )\n"
9058                "  continue;",
9059                Spaces);
9060   verifyFormat("for ( ;; )\n"
9061                "  continue;",
9062                Spaces);
9063   verifyFormat("if ( true )\n"
9064                "  f();\n"
9065                "else if ( true )\n"
9066                "  f();",
9067                Spaces);
9068   verifyFormat("do {\n"
9069                "  do_something( (int)i );\n"
9070                "} while ( something() );",
9071                Spaces);
9072   verifyFormat("switch ( x ) {\n"
9073                "default:\n"
9074                "  break;\n"
9075                "}",
9076                Spaces);
9077 
9078   Spaces.SpacesInParentheses = false;
9079   Spaces.SpacesInCStyleCastParentheses = true;
9080   verifyFormat("Type *A = ( Type * )P;", Spaces);
9081   verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
9082   verifyFormat("x = ( int32 )y;", Spaces);
9083   verifyFormat("int a = ( int )(2.0f);", Spaces);
9084   verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
9085   verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
9086   verifyFormat("#define x (( int )-1)", Spaces);
9087 
9088   // Run the first set of tests again with:
9089   Spaces.SpacesInParentheses = false;
9090   Spaces.SpaceInEmptyParentheses = true;
9091   Spaces.SpacesInCStyleCastParentheses = true;
9092   verifyFormat("call(x, y, z);", Spaces);
9093   verifyFormat("call( );", Spaces);
9094   verifyFormat("std::function<void(int, int)> callback;", Spaces);
9095   verifyFormat("while (( bool )1)\n"
9096                "  continue;",
9097                Spaces);
9098   verifyFormat("for (;;)\n"
9099                "  continue;",
9100                Spaces);
9101   verifyFormat("if (true)\n"
9102                "  f( );\n"
9103                "else if (true)\n"
9104                "  f( );",
9105                Spaces);
9106   verifyFormat("do {\n"
9107                "  do_something(( int )i);\n"
9108                "} while (something( ));",
9109                Spaces);
9110   verifyFormat("switch (x) {\n"
9111                "default:\n"
9112                "  break;\n"
9113                "}",
9114                Spaces);
9115 
9116   // Run the first set of tests again with:
9117   Spaces.SpaceAfterCStyleCast = true;
9118   verifyFormat("call(x, y, z);", Spaces);
9119   verifyFormat("call( );", Spaces);
9120   verifyFormat("std::function<void(int, int)> callback;", Spaces);
9121   verifyFormat("while (( bool ) 1)\n"
9122                "  continue;",
9123                Spaces);
9124   verifyFormat("for (;;)\n"
9125                "  continue;",
9126                Spaces);
9127   verifyFormat("if (true)\n"
9128                "  f( );\n"
9129                "else if (true)\n"
9130                "  f( );",
9131                Spaces);
9132   verifyFormat("do {\n"
9133                "  do_something(( int ) i);\n"
9134                "} while (something( ));",
9135                Spaces);
9136   verifyFormat("switch (x) {\n"
9137                "default:\n"
9138                "  break;\n"
9139                "}",
9140                Spaces);
9141 
9142   // Run subset of tests again with:
9143   Spaces.SpacesInCStyleCastParentheses = false;
9144   Spaces.SpaceAfterCStyleCast = true;
9145   verifyFormat("while ((bool) 1)\n"
9146                "  continue;",
9147                Spaces);
9148   verifyFormat("do {\n"
9149                "  do_something((int) i);\n"
9150                "} while (something( ));",
9151                Spaces);
9152 }
9153 
9154 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
9155   verifyFormat("int a[5];");
9156   verifyFormat("a[3] += 42;");
9157 
9158   FormatStyle Spaces = getLLVMStyle();
9159   Spaces.SpacesInSquareBrackets = true;
9160   // Lambdas unchanged.
9161   verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
9162   verifyFormat("return [i, args...] {};", Spaces);
9163 
9164   // Not lambdas.
9165   verifyFormat("int a[ 5 ];", Spaces);
9166   verifyFormat("a[ 3 ] += 42;", Spaces);
9167   verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
9168   verifyFormat("double &operator[](int i) { return 0; }\n"
9169                "int i;",
9170                Spaces);
9171   verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
9172   verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
9173   verifyFormat("int i = (*b)[ a ]->f();", Spaces);
9174 }
9175 
9176 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
9177   verifyFormat("int a = 5;");
9178   verifyFormat("a += 42;");
9179   verifyFormat("a or_eq 8;");
9180 
9181   FormatStyle Spaces = getLLVMStyle();
9182   Spaces.SpaceBeforeAssignmentOperators = false;
9183   verifyFormat("int a= 5;", Spaces);
9184   verifyFormat("a+= 42;", Spaces);
9185   verifyFormat("a or_eq 8;", Spaces);
9186 }
9187 
9188 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) {
9189   verifyFormat("class Foo : public Bar {};");
9190   verifyFormat("Foo::Foo() : foo(1) {}");
9191   verifyFormat("for (auto a : b) {\n}");
9192   verifyFormat("int x = a ? b : c;");
9193   verifyFormat("{\n"
9194                "label0:\n"
9195                "  int x = 0;\n"
9196                "}");
9197   verifyFormat("switch (x) {\n"
9198                "case 1:\n"
9199                "default:\n"
9200                "}");
9201 
9202   FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30);
9203   CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false;
9204   verifyFormat("class Foo : public Bar {};", CtorInitializerStyle);
9205   verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle);
9206   verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle);
9207   verifyFormat("int x = a ? b : c;", CtorInitializerStyle);
9208   verifyFormat("{\n"
9209                "label1:\n"
9210                "  int x = 0;\n"
9211                "}",
9212                CtorInitializerStyle);
9213   verifyFormat("switch (x) {\n"
9214                "case 1:\n"
9215                "default:\n"
9216                "}",
9217                CtorInitializerStyle);
9218   CtorInitializerStyle.BreakConstructorInitializers =
9219       FormatStyle::BCIS_AfterColon;
9220   verifyFormat("Fooooooooooo::Fooooooooooo():\n"
9221                "    aaaaaaaaaaaaaaaa(1),\n"
9222                "    bbbbbbbbbbbbbbbb(2) {}",
9223                CtorInitializerStyle);
9224   CtorInitializerStyle.BreakConstructorInitializers =
9225       FormatStyle::BCIS_BeforeComma;
9226   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
9227                "    : aaaaaaaaaaaaaaaa(1)\n"
9228                "    , bbbbbbbbbbbbbbbb(2) {}",
9229                CtorInitializerStyle);
9230   CtorInitializerStyle.BreakConstructorInitializers =
9231       FormatStyle::BCIS_BeforeColon;
9232   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
9233                "    : aaaaaaaaaaaaaaaa(1),\n"
9234                "      bbbbbbbbbbbbbbbb(2) {}",
9235                CtorInitializerStyle);
9236   CtorInitializerStyle.ConstructorInitializerIndentWidth = 0;
9237   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
9238                ": aaaaaaaaaaaaaaaa(1),\n"
9239                "  bbbbbbbbbbbbbbbb(2) {}",
9240                CtorInitializerStyle);
9241 
9242   FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30);
9243   InheritanceStyle.SpaceBeforeInheritanceColon = false;
9244   verifyFormat("class Foo: public Bar {};", InheritanceStyle);
9245   verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle);
9246   verifyFormat("for (auto a : b) {\n}", InheritanceStyle);
9247   verifyFormat("int x = a ? b : c;", InheritanceStyle);
9248   verifyFormat("{\n"
9249                "label2:\n"
9250                "  int x = 0;\n"
9251                "}",
9252                InheritanceStyle);
9253   verifyFormat("switch (x) {\n"
9254                "case 1:\n"
9255                "default:\n"
9256                "}",
9257                InheritanceStyle);
9258   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon;
9259   verifyFormat("class Foooooooooooooooooooooo:\n"
9260                "    public aaaaaaaaaaaaaaaaaa,\n"
9261                "    public bbbbbbbbbbbbbbbbbb {\n"
9262                "}",
9263                InheritanceStyle);
9264   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
9265   verifyFormat("class Foooooooooooooooooooooo\n"
9266                "    : public aaaaaaaaaaaaaaaaaa\n"
9267                "    , public bbbbbbbbbbbbbbbbbb {\n"
9268                "}",
9269                InheritanceStyle);
9270   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
9271   verifyFormat("class Foooooooooooooooooooooo\n"
9272                "    : public aaaaaaaaaaaaaaaaaa,\n"
9273                "      public bbbbbbbbbbbbbbbbbb {\n"
9274                "}",
9275                InheritanceStyle);
9276   InheritanceStyle.ConstructorInitializerIndentWidth = 0;
9277   verifyFormat("class Foooooooooooooooooooooo\n"
9278                ": public aaaaaaaaaaaaaaaaaa,\n"
9279                "  public bbbbbbbbbbbbbbbbbb {}",
9280                InheritanceStyle);
9281 
9282   FormatStyle ForLoopStyle = getLLVMStyle();
9283   ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false;
9284   verifyFormat("class Foo : public Bar {};", ForLoopStyle);
9285   verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle);
9286   verifyFormat("for (auto a: b) {\n}", ForLoopStyle);
9287   verifyFormat("int x = a ? b : c;", ForLoopStyle);
9288   verifyFormat("{\n"
9289                "label2:\n"
9290                "  int x = 0;\n"
9291                "}",
9292                ForLoopStyle);
9293   verifyFormat("switch (x) {\n"
9294                "case 1:\n"
9295                "default:\n"
9296                "}",
9297                ForLoopStyle);
9298 
9299   FormatStyle NoSpaceStyle = getLLVMStyle();
9300   NoSpaceStyle.SpaceBeforeCtorInitializerColon = false;
9301   NoSpaceStyle.SpaceBeforeInheritanceColon = false;
9302   NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
9303   verifyFormat("class Foo: public Bar {};", NoSpaceStyle);
9304   verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle);
9305   verifyFormat("for (auto a: b) {\n}", NoSpaceStyle);
9306   verifyFormat("int x = a ? b : c;", NoSpaceStyle);
9307   verifyFormat("{\n"
9308                "label3:\n"
9309                "  int x = 0;\n"
9310                "}",
9311                NoSpaceStyle);
9312   verifyFormat("switch (x) {\n"
9313                "case 1:\n"
9314                "default:\n"
9315                "}",
9316                NoSpaceStyle);
9317 }
9318 
9319 TEST_F(FormatTest, AlignConsecutiveAssignments) {
9320   FormatStyle Alignment = getLLVMStyle();
9321   Alignment.AlignConsecutiveAssignments = false;
9322   verifyFormat("int a = 5;\n"
9323                "int oneTwoThree = 123;",
9324                Alignment);
9325   verifyFormat("int a = 5;\n"
9326                "int oneTwoThree = 123;",
9327                Alignment);
9328 
9329   Alignment.AlignConsecutiveAssignments = true;
9330   verifyFormat("int a           = 5;\n"
9331                "int oneTwoThree = 123;",
9332                Alignment);
9333   verifyFormat("int a           = method();\n"
9334                "int oneTwoThree = 133;",
9335                Alignment);
9336   verifyFormat("a &= 5;\n"
9337                "bcd *= 5;\n"
9338                "ghtyf += 5;\n"
9339                "dvfvdb -= 5;\n"
9340                "a /= 5;\n"
9341                "vdsvsv %= 5;\n"
9342                "sfdbddfbdfbb ^= 5;\n"
9343                "dvsdsv |= 5;\n"
9344                "int dsvvdvsdvvv = 123;",
9345                Alignment);
9346   verifyFormat("int i = 1, j = 10;\n"
9347                "something = 2000;",
9348                Alignment);
9349   verifyFormat("something = 2000;\n"
9350                "int i = 1, j = 10;\n",
9351                Alignment);
9352   verifyFormat("something = 2000;\n"
9353                "another   = 911;\n"
9354                "int i = 1, j = 10;\n"
9355                "oneMore = 1;\n"
9356                "i       = 2;",
9357                Alignment);
9358   verifyFormat("int a   = 5;\n"
9359                "int one = 1;\n"
9360                "method();\n"
9361                "int oneTwoThree = 123;\n"
9362                "int oneTwo      = 12;",
9363                Alignment);
9364   verifyFormat("int oneTwoThree = 123;\n"
9365                "int oneTwo      = 12;\n"
9366                "method();\n",
9367                Alignment);
9368   verifyFormat("int oneTwoThree = 123; // comment\n"
9369                "int oneTwo      = 12;  // comment",
9370                Alignment);
9371   EXPECT_EQ("int a = 5;\n"
9372             "\n"
9373             "int oneTwoThree = 123;",
9374             format("int a       = 5;\n"
9375                    "\n"
9376                    "int oneTwoThree= 123;",
9377                    Alignment));
9378   EXPECT_EQ("int a   = 5;\n"
9379             "int one = 1;\n"
9380             "\n"
9381             "int oneTwoThree = 123;",
9382             format("int a = 5;\n"
9383                    "int one = 1;\n"
9384                    "\n"
9385                    "int oneTwoThree = 123;",
9386                    Alignment));
9387   EXPECT_EQ("int a   = 5;\n"
9388             "int one = 1;\n"
9389             "\n"
9390             "int oneTwoThree = 123;\n"
9391             "int oneTwo      = 12;",
9392             format("int a = 5;\n"
9393                    "int one = 1;\n"
9394                    "\n"
9395                    "int oneTwoThree = 123;\n"
9396                    "int oneTwo = 12;",
9397                    Alignment));
9398   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
9399   verifyFormat("#define A \\\n"
9400                "  int aaaa       = 12; \\\n"
9401                "  int b          = 23; \\\n"
9402                "  int ccc        = 234; \\\n"
9403                "  int dddddddddd = 2345;",
9404                Alignment);
9405   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
9406   verifyFormat("#define A               \\\n"
9407                "  int aaaa       = 12;  \\\n"
9408                "  int b          = 23;  \\\n"
9409                "  int ccc        = 234; \\\n"
9410                "  int dddddddddd = 2345;",
9411                Alignment);
9412   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
9413   verifyFormat("#define A                                                      "
9414                "                \\\n"
9415                "  int aaaa       = 12;                                         "
9416                "                \\\n"
9417                "  int b          = 23;                                         "
9418                "                \\\n"
9419                "  int ccc        = 234;                                        "
9420                "                \\\n"
9421                "  int dddddddddd = 2345;",
9422                Alignment);
9423   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
9424                "k = 4, int l = 5,\n"
9425                "                  int m = 6) {\n"
9426                "  int j      = 10;\n"
9427                "  otherThing = 1;\n"
9428                "}",
9429                Alignment);
9430   verifyFormat("void SomeFunction(int parameter = 0) {\n"
9431                "  int i   = 1;\n"
9432                "  int j   = 2;\n"
9433                "  int big = 10000;\n"
9434                "}",
9435                Alignment);
9436   verifyFormat("class C {\n"
9437                "public:\n"
9438                "  int i            = 1;\n"
9439                "  virtual void f() = 0;\n"
9440                "};",
9441                Alignment);
9442   verifyFormat("int i = 1;\n"
9443                "if (SomeType t = getSomething()) {\n"
9444                "}\n"
9445                "int j   = 2;\n"
9446                "int big = 10000;",
9447                Alignment);
9448   verifyFormat("int j = 7;\n"
9449                "for (int k = 0; k < N; ++k) {\n"
9450                "}\n"
9451                "int j   = 2;\n"
9452                "int big = 10000;\n"
9453                "}",
9454                Alignment);
9455   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
9456   verifyFormat("int i = 1;\n"
9457                "LooooooooooongType loooooooooooooooooooooongVariable\n"
9458                "    = someLooooooooooooooooongFunction();\n"
9459                "int j = 2;",
9460                Alignment);
9461   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
9462   verifyFormat("int i = 1;\n"
9463                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
9464                "    someLooooooooooooooooongFunction();\n"
9465                "int j = 2;",
9466                Alignment);
9467 
9468   verifyFormat("auto lambda = []() {\n"
9469                "  auto i = 0;\n"
9470                "  return 0;\n"
9471                "};\n"
9472                "int i  = 0;\n"
9473                "auto v = type{\n"
9474                "    i = 1,   //\n"
9475                "    (i = 2), //\n"
9476                "    i = 3    //\n"
9477                "};",
9478                Alignment);
9479 
9480   verifyFormat(
9481       "int i      = 1;\n"
9482       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
9483       "                          loooooooooooooooooooooongParameterB);\n"
9484       "int j      = 2;",
9485       Alignment);
9486 
9487   verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
9488                "          typename B   = very_long_type_name_1,\n"
9489                "          typename T_2 = very_long_type_name_2>\n"
9490                "auto foo() {}\n",
9491                Alignment);
9492   verifyFormat("int a, b = 1;\n"
9493                "int c  = 2;\n"
9494                "int dd = 3;\n",
9495                Alignment);
9496   verifyFormat("int aa       = ((1 > 2) ? 3 : 4);\n"
9497                "float b[1][] = {{3.f}};\n",
9498                Alignment);
9499   verifyFormat("for (int i = 0; i < 1; i++)\n"
9500                "  int x = 1;\n",
9501                Alignment);
9502   verifyFormat("for (i = 0; i < 1; i++)\n"
9503                "  x = 1;\n"
9504                "y = 1;\n",
9505                Alignment);
9506 }
9507 
9508 TEST_F(FormatTest, AlignConsecutiveDeclarations) {
9509   FormatStyle Alignment = getLLVMStyle();
9510   Alignment.AlignConsecutiveDeclarations = false;
9511   verifyFormat("float const a = 5;\n"
9512                "int oneTwoThree = 123;",
9513                Alignment);
9514   verifyFormat("int a = 5;\n"
9515                "float const oneTwoThree = 123;",
9516                Alignment);
9517 
9518   Alignment.AlignConsecutiveDeclarations = true;
9519   verifyFormat("float const a = 5;\n"
9520                "int         oneTwoThree = 123;",
9521                Alignment);
9522   verifyFormat("int         a = method();\n"
9523                "float const oneTwoThree = 133;",
9524                Alignment);
9525   verifyFormat("int i = 1, j = 10;\n"
9526                "something = 2000;",
9527                Alignment);
9528   verifyFormat("something = 2000;\n"
9529                "int i = 1, j = 10;\n",
9530                Alignment);
9531   verifyFormat("float      something = 2000;\n"
9532                "double     another = 911;\n"
9533                "int        i = 1, j = 10;\n"
9534                "const int *oneMore = 1;\n"
9535                "unsigned   i = 2;",
9536                Alignment);
9537   verifyFormat("float a = 5;\n"
9538                "int   one = 1;\n"
9539                "method();\n"
9540                "const double       oneTwoThree = 123;\n"
9541                "const unsigned int oneTwo = 12;",
9542                Alignment);
9543   verifyFormat("int      oneTwoThree{0}; // comment\n"
9544                "unsigned oneTwo;         // comment",
9545                Alignment);
9546   EXPECT_EQ("float const a = 5;\n"
9547             "\n"
9548             "int oneTwoThree = 123;",
9549             format("float const   a = 5;\n"
9550                    "\n"
9551                    "int           oneTwoThree= 123;",
9552                    Alignment));
9553   EXPECT_EQ("float a = 5;\n"
9554             "int   one = 1;\n"
9555             "\n"
9556             "unsigned oneTwoThree = 123;",
9557             format("float    a = 5;\n"
9558                    "int      one = 1;\n"
9559                    "\n"
9560                    "unsigned oneTwoThree = 123;",
9561                    Alignment));
9562   EXPECT_EQ("float a = 5;\n"
9563             "int   one = 1;\n"
9564             "\n"
9565             "unsigned oneTwoThree = 123;\n"
9566             "int      oneTwo = 12;",
9567             format("float    a = 5;\n"
9568                    "int one = 1;\n"
9569                    "\n"
9570                    "unsigned oneTwoThree = 123;\n"
9571                    "int oneTwo = 12;",
9572                    Alignment));
9573   // Function prototype alignment
9574   verifyFormat("int    a();\n"
9575                "double b();",
9576                Alignment);
9577   verifyFormat("int    a(int x);\n"
9578                "double b();",
9579                Alignment);
9580   unsigned OldColumnLimit = Alignment.ColumnLimit;
9581   // We need to set ColumnLimit to zero, in order to stress nested alignments,
9582   // otherwise the function parameters will be re-flowed onto a single line.
9583   Alignment.ColumnLimit = 0;
9584   EXPECT_EQ("int    a(int   x,\n"
9585             "         float y);\n"
9586             "double b(int    x,\n"
9587             "         double y);",
9588             format("int a(int x,\n"
9589                    " float y);\n"
9590                    "double b(int x,\n"
9591                    " double y);",
9592                    Alignment));
9593   // This ensures that function parameters of function declarations are
9594   // correctly indented when their owning functions are indented.
9595   // The failure case here is for 'double y' to not be indented enough.
9596   EXPECT_EQ("double a(int x);\n"
9597             "int    b(int    y,\n"
9598             "         double z);",
9599             format("double a(int x);\n"
9600                    "int b(int y,\n"
9601                    " double z);",
9602                    Alignment));
9603   // Set ColumnLimit low so that we induce wrapping immediately after
9604   // the function name and opening paren.
9605   Alignment.ColumnLimit = 13;
9606   verifyFormat("int function(\n"
9607                "    int  x,\n"
9608                "    bool y);",
9609                Alignment);
9610   Alignment.ColumnLimit = OldColumnLimit;
9611   // Ensure function pointers don't screw up recursive alignment
9612   verifyFormat("int    a(int x, void (*fp)(int y));\n"
9613                "double b();",
9614                Alignment);
9615   Alignment.AlignConsecutiveAssignments = true;
9616   // Ensure recursive alignment is broken by function braces, so that the
9617   // "a = 1" does not align with subsequent assignments inside the function
9618   // body.
9619   verifyFormat("int func(int a = 1) {\n"
9620                "  int b  = 2;\n"
9621                "  int cc = 3;\n"
9622                "}",
9623                Alignment);
9624   verifyFormat("float      something = 2000;\n"
9625                "double     another   = 911;\n"
9626                "int        i = 1, j = 10;\n"
9627                "const int *oneMore = 1;\n"
9628                "unsigned   i       = 2;",
9629                Alignment);
9630   verifyFormat("int      oneTwoThree = {0}; // comment\n"
9631                "unsigned oneTwo      = 0;   // comment",
9632                Alignment);
9633   // Make sure that scope is correctly tracked, in the absence of braces
9634   verifyFormat("for (int i = 0; i < n; i++)\n"
9635                "  j = i;\n"
9636                "double x = 1;\n",
9637                Alignment);
9638   verifyFormat("if (int i = 0)\n"
9639                "  j = i;\n"
9640                "double x = 1;\n",
9641                Alignment);
9642   // Ensure operator[] and operator() are comprehended
9643   verifyFormat("struct test {\n"
9644                "  long long int foo();\n"
9645                "  int           operator[](int a);\n"
9646                "  double        bar();\n"
9647                "};\n",
9648                Alignment);
9649   verifyFormat("struct test {\n"
9650                "  long long int foo();\n"
9651                "  int           operator()(int a);\n"
9652                "  double        bar();\n"
9653                "};\n",
9654                Alignment);
9655   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
9656             "  int const i   = 1;\n"
9657             "  int *     j   = 2;\n"
9658             "  int       big = 10000;\n"
9659             "\n"
9660             "  unsigned oneTwoThree = 123;\n"
9661             "  int      oneTwo      = 12;\n"
9662             "  method();\n"
9663             "  float k  = 2;\n"
9664             "  int   ll = 10000;\n"
9665             "}",
9666             format("void SomeFunction(int parameter= 0) {\n"
9667                    " int const  i= 1;\n"
9668                    "  int *j=2;\n"
9669                    " int big  =  10000;\n"
9670                    "\n"
9671                    "unsigned oneTwoThree  =123;\n"
9672                    "int oneTwo = 12;\n"
9673                    "  method();\n"
9674                    "float k= 2;\n"
9675                    "int ll=10000;\n"
9676                    "}",
9677                    Alignment));
9678   Alignment.AlignConsecutiveAssignments = false;
9679   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
9680   verifyFormat("#define A \\\n"
9681                "  int       aaaa = 12; \\\n"
9682                "  float     b = 23; \\\n"
9683                "  const int ccc = 234; \\\n"
9684                "  unsigned  dddddddddd = 2345;",
9685                Alignment);
9686   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
9687   verifyFormat("#define A              \\\n"
9688                "  int       aaaa = 12; \\\n"
9689                "  float     b = 23;    \\\n"
9690                "  const int ccc = 234; \\\n"
9691                "  unsigned  dddddddddd = 2345;",
9692                Alignment);
9693   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
9694   Alignment.ColumnLimit = 30;
9695   verifyFormat("#define A                    \\\n"
9696                "  int       aaaa = 12;       \\\n"
9697                "  float     b = 23;          \\\n"
9698                "  const int ccc = 234;       \\\n"
9699                "  int       dddddddddd = 2345;",
9700                Alignment);
9701   Alignment.ColumnLimit = 80;
9702   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
9703                "k = 4, int l = 5,\n"
9704                "                  int m = 6) {\n"
9705                "  const int j = 10;\n"
9706                "  otherThing = 1;\n"
9707                "}",
9708                Alignment);
9709   verifyFormat("void SomeFunction(int parameter = 0) {\n"
9710                "  int const i = 1;\n"
9711                "  int *     j = 2;\n"
9712                "  int       big = 10000;\n"
9713                "}",
9714                Alignment);
9715   verifyFormat("class C {\n"
9716                "public:\n"
9717                "  int          i = 1;\n"
9718                "  virtual void f() = 0;\n"
9719                "};",
9720                Alignment);
9721   verifyFormat("float i = 1;\n"
9722                "if (SomeType t = getSomething()) {\n"
9723                "}\n"
9724                "const unsigned j = 2;\n"
9725                "int            big = 10000;",
9726                Alignment);
9727   verifyFormat("float j = 7;\n"
9728                "for (int k = 0; k < N; ++k) {\n"
9729                "}\n"
9730                "unsigned j = 2;\n"
9731                "int      big = 10000;\n"
9732                "}",
9733                Alignment);
9734   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
9735   verifyFormat("float              i = 1;\n"
9736                "LooooooooooongType loooooooooooooooooooooongVariable\n"
9737                "    = someLooooooooooooooooongFunction();\n"
9738                "int j = 2;",
9739                Alignment);
9740   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
9741   verifyFormat("int                i = 1;\n"
9742                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
9743                "    someLooooooooooooooooongFunction();\n"
9744                "int j = 2;",
9745                Alignment);
9746 
9747   Alignment.AlignConsecutiveAssignments = true;
9748   verifyFormat("auto lambda = []() {\n"
9749                "  auto  ii = 0;\n"
9750                "  float j  = 0;\n"
9751                "  return 0;\n"
9752                "};\n"
9753                "int   i  = 0;\n"
9754                "float i2 = 0;\n"
9755                "auto  v  = type{\n"
9756                "    i = 1,   //\n"
9757                "    (i = 2), //\n"
9758                "    i = 3    //\n"
9759                "};",
9760                Alignment);
9761   Alignment.AlignConsecutiveAssignments = false;
9762 
9763   verifyFormat(
9764       "int      i = 1;\n"
9765       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
9766       "                          loooooooooooooooooooooongParameterB);\n"
9767       "int      j = 2;",
9768       Alignment);
9769 
9770   // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
9771   // We expect declarations and assignments to align, as long as it doesn't
9772   // exceed the column limit, starting a new alignment sequence whenever it
9773   // happens.
9774   Alignment.AlignConsecutiveAssignments = true;
9775   Alignment.ColumnLimit = 30;
9776   verifyFormat("float    ii              = 1;\n"
9777                "unsigned j               = 2;\n"
9778                "int someVerylongVariable = 1;\n"
9779                "AnotherLongType  ll = 123456;\n"
9780                "VeryVeryLongType k  = 2;\n"
9781                "int              myvar = 1;",
9782                Alignment);
9783   Alignment.ColumnLimit = 80;
9784   Alignment.AlignConsecutiveAssignments = false;
9785 
9786   verifyFormat(
9787       "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
9788       "          typename LongType, typename B>\n"
9789       "auto foo() {}\n",
9790       Alignment);
9791   verifyFormat("float a, b = 1;\n"
9792                "int   c = 2;\n"
9793                "int   dd = 3;\n",
9794                Alignment);
9795   verifyFormat("int   aa = ((1 > 2) ? 3 : 4);\n"
9796                "float b[1][] = {{3.f}};\n",
9797                Alignment);
9798   Alignment.AlignConsecutiveAssignments = true;
9799   verifyFormat("float a, b = 1;\n"
9800                "int   c  = 2;\n"
9801                "int   dd = 3;\n",
9802                Alignment);
9803   verifyFormat("int   aa     = ((1 > 2) ? 3 : 4);\n"
9804                "float b[1][] = {{3.f}};\n",
9805                Alignment);
9806   Alignment.AlignConsecutiveAssignments = false;
9807 
9808   Alignment.ColumnLimit = 30;
9809   Alignment.BinPackParameters = false;
9810   verifyFormat("void foo(float     a,\n"
9811                "         float     b,\n"
9812                "         int       c,\n"
9813                "         uint32_t *d) {\n"
9814                "  int *  e = 0;\n"
9815                "  float  f = 0;\n"
9816                "  double g = 0;\n"
9817                "}\n"
9818                "void bar(ino_t     a,\n"
9819                "         int       b,\n"
9820                "         uint32_t *c,\n"
9821                "         bool      d) {}\n",
9822                Alignment);
9823   Alignment.BinPackParameters = true;
9824   Alignment.ColumnLimit = 80;
9825 
9826   // Bug 33507
9827   Alignment.PointerAlignment = FormatStyle::PAS_Middle;
9828   verifyFormat(
9829       "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n"
9830       "  static const Version verVs2017;\n"
9831       "  return true;\n"
9832       "});\n",
9833       Alignment);
9834   Alignment.PointerAlignment = FormatStyle::PAS_Right;
9835 
9836   // See llvm.org/PR35641
9837   Alignment.AlignConsecutiveDeclarations = true;
9838   verifyFormat("int func() { //\n"
9839                "  int      b;\n"
9840                "  unsigned c;\n"
9841                "}",
9842                Alignment);
9843 }
9844 
9845 TEST_F(FormatTest, LinuxBraceBreaking) {
9846   FormatStyle LinuxBraceStyle = getLLVMStyle();
9847   LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
9848   verifyFormat("namespace a\n"
9849                "{\n"
9850                "class A\n"
9851                "{\n"
9852                "  void f()\n"
9853                "  {\n"
9854                "    if (true) {\n"
9855                "      a();\n"
9856                "      b();\n"
9857                "    } else {\n"
9858                "      a();\n"
9859                "    }\n"
9860                "  }\n"
9861                "  void g() { return; }\n"
9862                "};\n"
9863                "struct B {\n"
9864                "  int x;\n"
9865                "};\n"
9866                "} // namespace a\n",
9867                LinuxBraceStyle);
9868   verifyFormat("enum X {\n"
9869                "  Y = 0,\n"
9870                "}\n",
9871                LinuxBraceStyle);
9872   verifyFormat("struct S {\n"
9873                "  int Type;\n"
9874                "  union {\n"
9875                "    int x;\n"
9876                "    double y;\n"
9877                "  } Value;\n"
9878                "  class C\n"
9879                "  {\n"
9880                "    MyFavoriteType Value;\n"
9881                "  } Class;\n"
9882                "}\n",
9883                LinuxBraceStyle);
9884 }
9885 
9886 TEST_F(FormatTest, MozillaBraceBreaking) {
9887   FormatStyle MozillaBraceStyle = getLLVMStyle();
9888   MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
9889   MozillaBraceStyle.FixNamespaceComments = false;
9890   verifyFormat("namespace a {\n"
9891                "class A\n"
9892                "{\n"
9893                "  void f()\n"
9894                "  {\n"
9895                "    if (true) {\n"
9896                "      a();\n"
9897                "      b();\n"
9898                "    }\n"
9899                "  }\n"
9900                "  void g() { return; }\n"
9901                "};\n"
9902                "enum E\n"
9903                "{\n"
9904                "  A,\n"
9905                "  // foo\n"
9906                "  B,\n"
9907                "  C\n"
9908                "};\n"
9909                "struct B\n"
9910                "{\n"
9911                "  int x;\n"
9912                "};\n"
9913                "}\n",
9914                MozillaBraceStyle);
9915   verifyFormat("struct S\n"
9916                "{\n"
9917                "  int Type;\n"
9918                "  union\n"
9919                "  {\n"
9920                "    int x;\n"
9921                "    double y;\n"
9922                "  } Value;\n"
9923                "  class C\n"
9924                "  {\n"
9925                "    MyFavoriteType Value;\n"
9926                "  } Class;\n"
9927                "}\n",
9928                MozillaBraceStyle);
9929 }
9930 
9931 TEST_F(FormatTest, StroustrupBraceBreaking) {
9932   FormatStyle StroustrupBraceStyle = getLLVMStyle();
9933   StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
9934   verifyFormat("namespace a {\n"
9935                "class A {\n"
9936                "  void f()\n"
9937                "  {\n"
9938                "    if (true) {\n"
9939                "      a();\n"
9940                "      b();\n"
9941                "    }\n"
9942                "  }\n"
9943                "  void g() { return; }\n"
9944                "};\n"
9945                "struct B {\n"
9946                "  int x;\n"
9947                "};\n"
9948                "} // namespace a\n",
9949                StroustrupBraceStyle);
9950 
9951   verifyFormat("void foo()\n"
9952                "{\n"
9953                "  if (a) {\n"
9954                "    a();\n"
9955                "  }\n"
9956                "  else {\n"
9957                "    b();\n"
9958                "  }\n"
9959                "}\n",
9960                StroustrupBraceStyle);
9961 
9962   verifyFormat("#ifdef _DEBUG\n"
9963                "int foo(int i = 0)\n"
9964                "#else\n"
9965                "int foo(int i = 5)\n"
9966                "#endif\n"
9967                "{\n"
9968                "  return i;\n"
9969                "}",
9970                StroustrupBraceStyle);
9971 
9972   verifyFormat("void foo() {}\n"
9973                "void bar()\n"
9974                "#ifdef _DEBUG\n"
9975                "{\n"
9976                "  foo();\n"
9977                "}\n"
9978                "#else\n"
9979                "{\n"
9980                "}\n"
9981                "#endif",
9982                StroustrupBraceStyle);
9983 
9984   verifyFormat("void foobar() { int i = 5; }\n"
9985                "#ifdef _DEBUG\n"
9986                "void bar() {}\n"
9987                "#else\n"
9988                "void bar() { foobar(); }\n"
9989                "#endif",
9990                StroustrupBraceStyle);
9991 }
9992 
9993 TEST_F(FormatTest, AllmanBraceBreaking) {
9994   FormatStyle AllmanBraceStyle = getLLVMStyle();
9995   AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
9996 
9997   EXPECT_EQ("namespace a\n"
9998             "{\n"
9999             "void f();\n"
10000             "void g();\n"
10001             "} // namespace a\n",
10002             format("namespace a\n"
10003                    "{\n"
10004                    "void f();\n"
10005                    "void g();\n"
10006                    "}\n",
10007                    AllmanBraceStyle));
10008 
10009   verifyFormat("namespace a\n"
10010                "{\n"
10011                "class A\n"
10012                "{\n"
10013                "  void f()\n"
10014                "  {\n"
10015                "    if (true)\n"
10016                "    {\n"
10017                "      a();\n"
10018                "      b();\n"
10019                "    }\n"
10020                "  }\n"
10021                "  void g() { return; }\n"
10022                "};\n"
10023                "struct B\n"
10024                "{\n"
10025                "  int x;\n"
10026                "};\n"
10027                "} // namespace a",
10028                AllmanBraceStyle);
10029 
10030   verifyFormat("void f()\n"
10031                "{\n"
10032                "  if (true)\n"
10033                "  {\n"
10034                "    a();\n"
10035                "  }\n"
10036                "  else if (false)\n"
10037                "  {\n"
10038                "    b();\n"
10039                "  }\n"
10040                "  else\n"
10041                "  {\n"
10042                "    c();\n"
10043                "  }\n"
10044                "}\n",
10045                AllmanBraceStyle);
10046 
10047   verifyFormat("void f()\n"
10048                "{\n"
10049                "  for (int i = 0; i < 10; ++i)\n"
10050                "  {\n"
10051                "    a();\n"
10052                "  }\n"
10053                "  while (false)\n"
10054                "  {\n"
10055                "    b();\n"
10056                "  }\n"
10057                "  do\n"
10058                "  {\n"
10059                "    c();\n"
10060                "  } while (false)\n"
10061                "}\n",
10062                AllmanBraceStyle);
10063 
10064   verifyFormat("void f(int a)\n"
10065                "{\n"
10066                "  switch (a)\n"
10067                "  {\n"
10068                "  case 0:\n"
10069                "    break;\n"
10070                "  case 1:\n"
10071                "  {\n"
10072                "    break;\n"
10073                "  }\n"
10074                "  case 2:\n"
10075                "  {\n"
10076                "  }\n"
10077                "  break;\n"
10078                "  default:\n"
10079                "    break;\n"
10080                "  }\n"
10081                "}\n",
10082                AllmanBraceStyle);
10083 
10084   verifyFormat("enum X\n"
10085                "{\n"
10086                "  Y = 0,\n"
10087                "}\n",
10088                AllmanBraceStyle);
10089   verifyFormat("enum X\n"
10090                "{\n"
10091                "  Y = 0\n"
10092                "}\n",
10093                AllmanBraceStyle);
10094 
10095   verifyFormat("@interface BSApplicationController ()\n"
10096                "{\n"
10097                "@private\n"
10098                "  id _extraIvar;\n"
10099                "}\n"
10100                "@end\n",
10101                AllmanBraceStyle);
10102 
10103   verifyFormat("#ifdef _DEBUG\n"
10104                "int foo(int i = 0)\n"
10105                "#else\n"
10106                "int foo(int i = 5)\n"
10107                "#endif\n"
10108                "{\n"
10109                "  return i;\n"
10110                "}",
10111                AllmanBraceStyle);
10112 
10113   verifyFormat("void foo() {}\n"
10114                "void bar()\n"
10115                "#ifdef _DEBUG\n"
10116                "{\n"
10117                "  foo();\n"
10118                "}\n"
10119                "#else\n"
10120                "{\n"
10121                "}\n"
10122                "#endif",
10123                AllmanBraceStyle);
10124 
10125   verifyFormat("void foobar() { int i = 5; }\n"
10126                "#ifdef _DEBUG\n"
10127                "void bar() {}\n"
10128                "#else\n"
10129                "void bar() { foobar(); }\n"
10130                "#endif",
10131                AllmanBraceStyle);
10132 
10133   // This shouldn't affect ObjC blocks..
10134   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
10135                "  // ...\n"
10136                "  int i;\n"
10137                "}];",
10138                AllmanBraceStyle);
10139   verifyFormat("void (^block)(void) = ^{\n"
10140                "  // ...\n"
10141                "  int i;\n"
10142                "};",
10143                AllmanBraceStyle);
10144   // .. or dict literals.
10145   verifyFormat("void f()\n"
10146                "{\n"
10147                "  // ...\n"
10148                "  [object someMethod:@{@\"a\" : @\"b\"}];\n"
10149                "}",
10150                AllmanBraceStyle);
10151   verifyFormat("void f()\n"
10152                "{\n"
10153                "  // ...\n"
10154                "  [object someMethod:@{a : @\"b\"}];\n"
10155                "}",
10156                AllmanBraceStyle);
10157   verifyFormat("int f()\n"
10158                "{ // comment\n"
10159                "  return 42;\n"
10160                "}",
10161                AllmanBraceStyle);
10162 
10163   AllmanBraceStyle.ColumnLimit = 19;
10164   verifyFormat("void f() { int i; }", AllmanBraceStyle);
10165   AllmanBraceStyle.ColumnLimit = 18;
10166   verifyFormat("void f()\n"
10167                "{\n"
10168                "  int i;\n"
10169                "}",
10170                AllmanBraceStyle);
10171   AllmanBraceStyle.ColumnLimit = 80;
10172 
10173   FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
10174   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true;
10175   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
10176   verifyFormat("void f(bool b)\n"
10177                "{\n"
10178                "  if (b)\n"
10179                "  {\n"
10180                "    return;\n"
10181                "  }\n"
10182                "}\n",
10183                BreakBeforeBraceShortIfs);
10184   verifyFormat("void f(bool b)\n"
10185                "{\n"
10186                "  if constexpr (b)\n"
10187                "  {\n"
10188                "    return;\n"
10189                "  }\n"
10190                "}\n",
10191                BreakBeforeBraceShortIfs);
10192   verifyFormat("void f(bool b)\n"
10193                "{\n"
10194                "  if (b) return;\n"
10195                "}\n",
10196                BreakBeforeBraceShortIfs);
10197   verifyFormat("void f(bool b)\n"
10198                "{\n"
10199                "  if constexpr (b) return;\n"
10200                "}\n",
10201                BreakBeforeBraceShortIfs);
10202   verifyFormat("void f(bool b)\n"
10203                "{\n"
10204                "  while (b)\n"
10205                "  {\n"
10206                "    return;\n"
10207                "  }\n"
10208                "}\n",
10209                BreakBeforeBraceShortIfs);
10210 }
10211 
10212 TEST_F(FormatTest, GNUBraceBreaking) {
10213   FormatStyle GNUBraceStyle = getLLVMStyle();
10214   GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
10215   verifyFormat("namespace a\n"
10216                "{\n"
10217                "class A\n"
10218                "{\n"
10219                "  void f()\n"
10220                "  {\n"
10221                "    int a;\n"
10222                "    {\n"
10223                "      int b;\n"
10224                "    }\n"
10225                "    if (true)\n"
10226                "      {\n"
10227                "        a();\n"
10228                "        b();\n"
10229                "      }\n"
10230                "  }\n"
10231                "  void g() { return; }\n"
10232                "}\n"
10233                "} // namespace a",
10234                GNUBraceStyle);
10235 
10236   verifyFormat("void f()\n"
10237                "{\n"
10238                "  if (true)\n"
10239                "    {\n"
10240                "      a();\n"
10241                "    }\n"
10242                "  else if (false)\n"
10243                "    {\n"
10244                "      b();\n"
10245                "    }\n"
10246                "  else\n"
10247                "    {\n"
10248                "      c();\n"
10249                "    }\n"
10250                "}\n",
10251                GNUBraceStyle);
10252 
10253   verifyFormat("void f()\n"
10254                "{\n"
10255                "  for (int i = 0; i < 10; ++i)\n"
10256                "    {\n"
10257                "      a();\n"
10258                "    }\n"
10259                "  while (false)\n"
10260                "    {\n"
10261                "      b();\n"
10262                "    }\n"
10263                "  do\n"
10264                "    {\n"
10265                "      c();\n"
10266                "    }\n"
10267                "  while (false);\n"
10268                "}\n",
10269                GNUBraceStyle);
10270 
10271   verifyFormat("void f(int a)\n"
10272                "{\n"
10273                "  switch (a)\n"
10274                "    {\n"
10275                "    case 0:\n"
10276                "      break;\n"
10277                "    case 1:\n"
10278                "      {\n"
10279                "        break;\n"
10280                "      }\n"
10281                "    case 2:\n"
10282                "      {\n"
10283                "      }\n"
10284                "      break;\n"
10285                "    default:\n"
10286                "      break;\n"
10287                "    }\n"
10288                "}\n",
10289                GNUBraceStyle);
10290 
10291   verifyFormat("enum X\n"
10292                "{\n"
10293                "  Y = 0,\n"
10294                "}\n",
10295                GNUBraceStyle);
10296 
10297   verifyFormat("@interface BSApplicationController ()\n"
10298                "{\n"
10299                "@private\n"
10300                "  id _extraIvar;\n"
10301                "}\n"
10302                "@end\n",
10303                GNUBraceStyle);
10304 
10305   verifyFormat("#ifdef _DEBUG\n"
10306                "int foo(int i = 0)\n"
10307                "#else\n"
10308                "int foo(int i = 5)\n"
10309                "#endif\n"
10310                "{\n"
10311                "  return i;\n"
10312                "}",
10313                GNUBraceStyle);
10314 
10315   verifyFormat("void foo() {}\n"
10316                "void bar()\n"
10317                "#ifdef _DEBUG\n"
10318                "{\n"
10319                "  foo();\n"
10320                "}\n"
10321                "#else\n"
10322                "{\n"
10323                "}\n"
10324                "#endif",
10325                GNUBraceStyle);
10326 
10327   verifyFormat("void foobar() { int i = 5; }\n"
10328                "#ifdef _DEBUG\n"
10329                "void bar() {}\n"
10330                "#else\n"
10331                "void bar() { foobar(); }\n"
10332                "#endif",
10333                GNUBraceStyle);
10334 }
10335 
10336 TEST_F(FormatTest, WebKitBraceBreaking) {
10337   FormatStyle WebKitBraceStyle = getLLVMStyle();
10338   WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
10339   WebKitBraceStyle.FixNamespaceComments = false;
10340   verifyFormat("namespace a {\n"
10341                "class A {\n"
10342                "  void f()\n"
10343                "  {\n"
10344                "    if (true) {\n"
10345                "      a();\n"
10346                "      b();\n"
10347                "    }\n"
10348                "  }\n"
10349                "  void g() { return; }\n"
10350                "};\n"
10351                "enum E {\n"
10352                "  A,\n"
10353                "  // foo\n"
10354                "  B,\n"
10355                "  C\n"
10356                "};\n"
10357                "struct B {\n"
10358                "  int x;\n"
10359                "};\n"
10360                "}\n",
10361                WebKitBraceStyle);
10362   verifyFormat("struct S {\n"
10363                "  int Type;\n"
10364                "  union {\n"
10365                "    int x;\n"
10366                "    double y;\n"
10367                "  } Value;\n"
10368                "  class C {\n"
10369                "    MyFavoriteType Value;\n"
10370                "  } Class;\n"
10371                "};\n",
10372                WebKitBraceStyle);
10373 }
10374 
10375 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
10376   verifyFormat("void f() {\n"
10377                "  try {\n"
10378                "  } catch (const Exception &e) {\n"
10379                "  }\n"
10380                "}\n",
10381                getLLVMStyle());
10382 }
10383 
10384 TEST_F(FormatTest, UnderstandsPragmas) {
10385   verifyFormat("#pragma omp reduction(| : var)");
10386   verifyFormat("#pragma omp reduction(+ : var)");
10387 
10388   EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
10389             "(including parentheses).",
10390             format("#pragma    mark   Any non-hyphenated or hyphenated string "
10391                    "(including parentheses)."));
10392 }
10393 
10394 TEST_F(FormatTest, UnderstandPragmaOption) {
10395   verifyFormat("#pragma option -C -A");
10396 
10397   EXPECT_EQ("#pragma option -C -A", format("#pragma    option   -C   -A"));
10398 }
10399 
10400 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) {
10401   FormatStyle Style = getLLVMStyle();
10402   Style.ColumnLimit = 20;
10403 
10404   verifyFormat("int a; // the\n"
10405                "       // comment", Style);
10406   EXPECT_EQ("int a; /* first line\n"
10407             "        * second\n"
10408             "        * line third\n"
10409             "        * line\n"
10410             "        */",
10411             format("int a; /* first line\n"
10412                    "        * second\n"
10413                    "        * line third\n"
10414                    "        * line\n"
10415                    "        */",
10416                    Style));
10417   EXPECT_EQ("int a; // first line\n"
10418             "       // second\n"
10419             "       // line third\n"
10420             "       // line",
10421             format("int a; // first line\n"
10422                    "       // second line\n"
10423                    "       // third line",
10424                    Style));
10425 
10426   Style.PenaltyExcessCharacter = 90;
10427   verifyFormat("int a; // the comment", Style);
10428   EXPECT_EQ("int a; // the comment\n"
10429             "       // aaa",
10430             format("int a; // the comment aaa", Style));
10431   EXPECT_EQ("int a; /* first line\n"
10432             "        * second line\n"
10433             "        * third line\n"
10434             "        */",
10435             format("int a; /* first line\n"
10436                    "        * second line\n"
10437                    "        * third line\n"
10438                    "        */",
10439                    Style));
10440   EXPECT_EQ("int a; // first line\n"
10441             "       // second line\n"
10442             "       // third line",
10443             format("int a; // first line\n"
10444                    "       // second line\n"
10445                    "       // third line",
10446                    Style));
10447   // FIXME: Investigate why this is not getting the same layout as the test
10448   // above.
10449   EXPECT_EQ("int a; /* first line\n"
10450             "        * second line\n"
10451             "        * third line\n"
10452             "        */",
10453             format("int a; /* first line second line third line"
10454                    "\n*/",
10455                    Style));
10456 
10457   EXPECT_EQ("// foo bar baz bazfoo\n"
10458             "// foo bar foo bar\n",
10459             format("// foo bar baz bazfoo\n"
10460                    "// foo bar foo           bar\n",
10461                    Style));
10462   EXPECT_EQ("// foo bar baz bazfoo\n"
10463             "// foo bar foo bar\n",
10464             format("// foo bar baz      bazfoo\n"
10465                    "// foo            bar foo bar\n",
10466                    Style));
10467 
10468   // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the
10469   // next one.
10470   EXPECT_EQ("// foo bar baz bazfoo\n"
10471             "// bar foo bar\n",
10472             format("// foo bar baz      bazfoo bar\n"
10473                    "// foo            bar\n",
10474                    Style));
10475 
10476   EXPECT_EQ("// foo bar baz bazfoo\n"
10477             "// foo bar baz bazfoo\n"
10478             "// bar foo bar\n",
10479             format("// foo bar baz      bazfoo\n"
10480                    "// foo bar baz      bazfoo bar\n"
10481                    "// foo bar\n",
10482                    Style));
10483 
10484   EXPECT_EQ("// foo bar baz bazfoo\n"
10485             "// foo bar baz bazfoo\n"
10486             "// bar foo bar\n",
10487             format("// foo bar baz      bazfoo\n"
10488                    "// foo bar baz      bazfoo bar\n"
10489                    "// foo           bar\n",
10490                    Style));
10491 
10492   // Make sure we do not keep protruding characters if strict mode reflow is
10493   // cheaper than keeping protruding characters.
10494   Style.ColumnLimit = 21;
10495   EXPECT_EQ("// foo foo foo foo\n"
10496             "// foo foo foo foo\n"
10497             "// foo foo foo foo\n",
10498             format("// foo foo foo foo foo foo foo foo foo foo foo foo\n",
10499                            Style));
10500 
10501   EXPECT_EQ("int a = /* long block\n"
10502             "           comment */\n"
10503             "    42;",
10504             format("int a = /* long block comment */ 42;", Style));
10505 }
10506 
10507 #define EXPECT_ALL_STYLES_EQUAL(Styles)                                        \
10508   for (size_t i = 1; i < Styles.size(); ++i)                                   \
10509   EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \
10510                                   << " differs from Style #0"
10511 
10512 TEST_F(FormatTest, GetsPredefinedStyleByName) {
10513   SmallVector<FormatStyle, 3> Styles;
10514   Styles.resize(3);
10515 
10516   Styles[0] = getLLVMStyle();
10517   EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
10518   EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
10519   EXPECT_ALL_STYLES_EQUAL(Styles);
10520 
10521   Styles[0] = getGoogleStyle();
10522   EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
10523   EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
10524   EXPECT_ALL_STYLES_EQUAL(Styles);
10525 
10526   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
10527   EXPECT_TRUE(
10528       getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
10529   EXPECT_TRUE(
10530       getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
10531   EXPECT_ALL_STYLES_EQUAL(Styles);
10532 
10533   Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
10534   EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
10535   EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
10536   EXPECT_ALL_STYLES_EQUAL(Styles);
10537 
10538   Styles[0] = getMozillaStyle();
10539   EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
10540   EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
10541   EXPECT_ALL_STYLES_EQUAL(Styles);
10542 
10543   Styles[0] = getWebKitStyle();
10544   EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
10545   EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
10546   EXPECT_ALL_STYLES_EQUAL(Styles);
10547 
10548   Styles[0] = getGNUStyle();
10549   EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
10550   EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
10551   EXPECT_ALL_STYLES_EQUAL(Styles);
10552 
10553   EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
10554 }
10555 
10556 TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
10557   SmallVector<FormatStyle, 8> Styles;
10558   Styles.resize(2);
10559 
10560   Styles[0] = getGoogleStyle();
10561   Styles[1] = getLLVMStyle();
10562   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
10563   EXPECT_ALL_STYLES_EQUAL(Styles);
10564 
10565   Styles.resize(5);
10566   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
10567   Styles[1] = getLLVMStyle();
10568   Styles[1].Language = FormatStyle::LK_JavaScript;
10569   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
10570 
10571   Styles[2] = getLLVMStyle();
10572   Styles[2].Language = FormatStyle::LK_JavaScript;
10573   EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
10574                                   "BasedOnStyle: Google",
10575                                   &Styles[2])
10576                    .value());
10577 
10578   Styles[3] = getLLVMStyle();
10579   Styles[3].Language = FormatStyle::LK_JavaScript;
10580   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
10581                                   "Language: JavaScript",
10582                                   &Styles[3])
10583                    .value());
10584 
10585   Styles[4] = getLLVMStyle();
10586   Styles[4].Language = FormatStyle::LK_JavaScript;
10587   EXPECT_EQ(0, parseConfiguration("---\n"
10588                                   "BasedOnStyle: LLVM\n"
10589                                   "IndentWidth: 123\n"
10590                                   "---\n"
10591                                   "BasedOnStyle: Google\n"
10592                                   "Language: JavaScript",
10593                                   &Styles[4])
10594                    .value());
10595   EXPECT_ALL_STYLES_EQUAL(Styles);
10596 }
10597 
10598 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME)                             \
10599   Style.FIELD = false;                                                         \
10600   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value());      \
10601   EXPECT_TRUE(Style.FIELD);                                                    \
10602   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value());     \
10603   EXPECT_FALSE(Style.FIELD);
10604 
10605 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
10606 
10607 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME)              \
10608   Style.STRUCT.FIELD = false;                                                  \
10609   EXPECT_EQ(0,                                                                 \
10610             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": true", &Style)   \
10611                 .value());                                                     \
10612   EXPECT_TRUE(Style.STRUCT.FIELD);                                             \
10613   EXPECT_EQ(0,                                                                 \
10614             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": false", &Style)  \
10615                 .value());                                                     \
10616   EXPECT_FALSE(Style.STRUCT.FIELD);
10617 
10618 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD)                                 \
10619   CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD)
10620 
10621 #define CHECK_PARSE(TEXT, FIELD, VALUE)                                        \
10622   EXPECT_NE(VALUE, Style.FIELD);                                               \
10623   EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value());                      \
10624   EXPECT_EQ(VALUE, Style.FIELD)
10625 
10626 TEST_F(FormatTest, ParsesConfigurationBools) {
10627   FormatStyle Style = {};
10628   Style.Language = FormatStyle::LK_Cpp;
10629   CHECK_PARSE_BOOL(AlignOperands);
10630   CHECK_PARSE_BOOL(AlignTrailingComments);
10631   CHECK_PARSE_BOOL(AlignConsecutiveAssignments);
10632   CHECK_PARSE_BOOL(AlignConsecutiveDeclarations);
10633   CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
10634   CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine);
10635   CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
10636   CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine);
10637   CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
10638   CHECK_PARSE_BOOL(BinPackArguments);
10639   CHECK_PARSE_BOOL(BinPackParameters);
10640   CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations);
10641   CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
10642   CHECK_PARSE_BOOL(BreakStringLiterals);
10643   CHECK_PARSE_BOOL(CompactNamespaces);
10644   CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
10645   CHECK_PARSE_BOOL(DerivePointerAlignment);
10646   CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
10647   CHECK_PARSE_BOOL(DisableFormat);
10648   CHECK_PARSE_BOOL(IndentCaseLabels);
10649   CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
10650   CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
10651   CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
10652   CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
10653   CHECK_PARSE_BOOL(Cpp11BracedListStyle);
10654   CHECK_PARSE_BOOL(ReflowComments);
10655   CHECK_PARSE_BOOL(SortIncludes);
10656   CHECK_PARSE_BOOL(SortUsingDeclarations);
10657   CHECK_PARSE_BOOL(SpacesInParentheses);
10658   CHECK_PARSE_BOOL(SpacesInSquareBrackets);
10659   CHECK_PARSE_BOOL(SpacesInAngles);
10660   CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
10661   CHECK_PARSE_BOOL(SpacesInContainerLiterals);
10662   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
10663   CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
10664   CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword);
10665   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
10666   CHECK_PARSE_BOOL(SpaceBeforeCpp11BracedList);
10667   CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon);
10668   CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon);
10669   CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon);
10670 
10671   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass);
10672   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement);
10673   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum);
10674   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction);
10675   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace);
10676   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration);
10677   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct);
10678   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion);
10679   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock);
10680   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch);
10681   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse);
10682   CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces);
10683   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction);
10684   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord);
10685   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace);
10686 }
10687 
10688 #undef CHECK_PARSE_BOOL
10689 
10690 TEST_F(FormatTest, ParsesConfiguration) {
10691   FormatStyle Style = {};
10692   Style.Language = FormatStyle::LK_Cpp;
10693   CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
10694   CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
10695               ConstructorInitializerIndentWidth, 1234u);
10696   CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
10697   CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
10698   CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
10699   CHECK_PARSE("PenaltyBreakAssignment: 1234",
10700               PenaltyBreakAssignment, 1234u);
10701   CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
10702               PenaltyBreakBeforeFirstCallParameter, 1234u);
10703   CHECK_PARSE("PenaltyBreakTemplateDeclaration: 1234",
10704               PenaltyBreakTemplateDeclaration, 1234u);
10705   CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
10706   CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
10707               PenaltyReturnTypeOnItsOwnLine, 1234u);
10708   CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
10709               SpacesBeforeTrailingComments, 1234u);
10710   CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
10711   CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
10712   CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$");
10713 
10714   Style.PointerAlignment = FormatStyle::PAS_Middle;
10715   CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
10716               FormatStyle::PAS_Left);
10717   CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
10718               FormatStyle::PAS_Right);
10719   CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
10720               FormatStyle::PAS_Middle);
10721   // For backward compatibility:
10722   CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
10723               FormatStyle::PAS_Left);
10724   CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
10725               FormatStyle::PAS_Right);
10726   CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
10727               FormatStyle::PAS_Middle);
10728 
10729   Style.Standard = FormatStyle::LS_Auto;
10730   CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
10731   CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11);
10732   CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
10733   CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
10734   CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
10735 
10736   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
10737   CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
10738               BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
10739   CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
10740               FormatStyle::BOS_None);
10741   CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
10742               FormatStyle::BOS_All);
10743   // For backward compatibility:
10744   CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
10745               FormatStyle::BOS_None);
10746   CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
10747               FormatStyle::BOS_All);
10748 
10749   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
10750   CHECK_PARSE("BreakConstructorInitializers: BeforeComma",
10751               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
10752   CHECK_PARSE("BreakConstructorInitializers: AfterColon",
10753               BreakConstructorInitializers, FormatStyle::BCIS_AfterColon);
10754   CHECK_PARSE("BreakConstructorInitializers: BeforeColon",
10755               BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon);
10756   // For backward compatibility:
10757   CHECK_PARSE("BreakConstructorInitializersBeforeComma: true",
10758               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
10759 
10760   Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
10761   CHECK_PARSE("BreakInheritanceList: BeforeComma",
10762               BreakInheritanceList, FormatStyle::BILS_BeforeComma);
10763   CHECK_PARSE("BreakInheritanceList: AfterColon",
10764               BreakInheritanceList, FormatStyle::BILS_AfterColon);
10765   CHECK_PARSE("BreakInheritanceList: BeforeColon",
10766               BreakInheritanceList, FormatStyle::BILS_BeforeColon);
10767   // For backward compatibility:
10768   CHECK_PARSE("BreakBeforeInheritanceComma: true",
10769               BreakInheritanceList, FormatStyle::BILS_BeforeComma);
10770 
10771   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
10772   CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket,
10773               FormatStyle::BAS_Align);
10774   CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket,
10775               FormatStyle::BAS_DontAlign);
10776   CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket,
10777               FormatStyle::BAS_AlwaysBreak);
10778   // For backward compatibility:
10779   CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket,
10780               FormatStyle::BAS_DontAlign);
10781   CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket,
10782               FormatStyle::BAS_Align);
10783 
10784   Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
10785   CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines,
10786               FormatStyle::ENAS_DontAlign);
10787   CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines,
10788               FormatStyle::ENAS_Left);
10789   CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines,
10790               FormatStyle::ENAS_Right);
10791   // For backward compatibility:
10792   CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines,
10793               FormatStyle::ENAS_Left);
10794   CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines,
10795               FormatStyle::ENAS_Right);
10796 
10797   Style.UseTab = FormatStyle::UT_ForIndentation;
10798   CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
10799   CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
10800   CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
10801   CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab,
10802               FormatStyle::UT_ForContinuationAndIndentation);
10803   // For backward compatibility:
10804   CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
10805   CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
10806 
10807   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
10808   CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
10809               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
10810   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
10811               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
10812   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
10813               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
10814   CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
10815               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
10816   // For backward compatibility:
10817   CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
10818               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
10819   CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
10820               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
10821 
10822   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
10823   CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
10824               FormatStyle::SBPO_Never);
10825   CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
10826               FormatStyle::SBPO_Always);
10827   CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
10828               FormatStyle::SBPO_ControlStatements);
10829   // For backward compatibility:
10830   CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
10831               FormatStyle::SBPO_Never);
10832   CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
10833               FormatStyle::SBPO_ControlStatements);
10834 
10835   Style.ColumnLimit = 123;
10836   FormatStyle BaseStyle = getLLVMStyle();
10837   CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
10838   CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
10839 
10840   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
10841   CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
10842               FormatStyle::BS_Attach);
10843   CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
10844               FormatStyle::BS_Linux);
10845   CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces,
10846               FormatStyle::BS_Mozilla);
10847   CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
10848               FormatStyle::BS_Stroustrup);
10849   CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
10850               FormatStyle::BS_Allman);
10851   CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
10852   CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces,
10853               FormatStyle::BS_WebKit);
10854   CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces,
10855               FormatStyle::BS_Custom);
10856 
10857   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
10858   CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType,
10859               FormatStyle::RTBS_None);
10860   CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType,
10861               FormatStyle::RTBS_All);
10862   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel",
10863               AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel);
10864   CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions",
10865               AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions);
10866   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions",
10867               AlwaysBreakAfterReturnType,
10868               FormatStyle::RTBS_TopLevelDefinitions);
10869 
10870   Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
10871   CHECK_PARSE("AlwaysBreakTemplateDeclarations: No", AlwaysBreakTemplateDeclarations,
10872               FormatStyle::BTDS_No);
10873   CHECK_PARSE("AlwaysBreakTemplateDeclarations: MultiLine", AlwaysBreakTemplateDeclarations,
10874               FormatStyle::BTDS_MultiLine);
10875   CHECK_PARSE("AlwaysBreakTemplateDeclarations: Yes", AlwaysBreakTemplateDeclarations,
10876               FormatStyle::BTDS_Yes);
10877   CHECK_PARSE("AlwaysBreakTemplateDeclarations: false", AlwaysBreakTemplateDeclarations,
10878               FormatStyle::BTDS_MultiLine);
10879   CHECK_PARSE("AlwaysBreakTemplateDeclarations: true", AlwaysBreakTemplateDeclarations,
10880               FormatStyle::BTDS_Yes);
10881 
10882   Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
10883   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None",
10884               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None);
10885   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All",
10886               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All);
10887   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel",
10888               AlwaysBreakAfterDefinitionReturnType,
10889               FormatStyle::DRTBS_TopLevel);
10890 
10891   Style.NamespaceIndentation = FormatStyle::NI_All;
10892   CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
10893               FormatStyle::NI_None);
10894   CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
10895               FormatStyle::NI_Inner);
10896   CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
10897               FormatStyle::NI_All);
10898 
10899   // FIXME: This is required because parsing a configuration simply overwrites
10900   // the first N elements of the list instead of resetting it.
10901   Style.ForEachMacros.clear();
10902   std::vector<std::string> BoostForeach;
10903   BoostForeach.push_back("BOOST_FOREACH");
10904   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
10905   std::vector<std::string> BoostAndQForeach;
10906   BoostAndQForeach.push_back("BOOST_FOREACH");
10907   BoostAndQForeach.push_back("Q_FOREACH");
10908   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
10909               BoostAndQForeach);
10910 
10911   Style.IncludeStyle.IncludeCategories.clear();
10912   std::vector<tooling::IncludeStyle::IncludeCategory> ExpectedCategories = {
10913       {"abc/.*", 2}, {".*", 1}};
10914   CHECK_PARSE("IncludeCategories:\n"
10915               "  - Regex: abc/.*\n"
10916               "    Priority: 2\n"
10917               "  - Regex: .*\n"
10918               "    Priority: 1",
10919               IncludeStyle.IncludeCategories, ExpectedCategories);
10920   CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeStyle.IncludeIsMainRegex,
10921               "abc$");
10922 
10923   Style.RawStringFormats.clear();
10924   std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = {
10925       {
10926           FormatStyle::LK_TextProto,
10927           {"pb", "proto"},
10928           {"PARSE_TEXT_PROTO"},
10929           /*CanonicalDelimiter=*/"",
10930           "llvm",
10931       },
10932       {
10933           FormatStyle::LK_Cpp,
10934           {"cc", "cpp"},
10935           {"C_CODEBLOCK", "CPPEVAL"},
10936           /*CanonicalDelimiter=*/"cc",
10937           /*BasedOnStyle=*/"",
10938       },
10939   };
10940 
10941   CHECK_PARSE("RawStringFormats:\n"
10942               "  - Language: TextProto\n"
10943               "    Delimiters:\n"
10944               "      - 'pb'\n"
10945               "      - 'proto'\n"
10946               "    EnclosingFunctions:\n"
10947               "      - 'PARSE_TEXT_PROTO'\n"
10948               "    BasedOnStyle: llvm\n"
10949               "  - Language: Cpp\n"
10950               "    Delimiters:\n"
10951               "      - 'cc'\n"
10952               "      - 'cpp'\n"
10953               "    EnclosingFunctions:\n"
10954               "      - 'C_CODEBLOCK'\n"
10955               "      - 'CPPEVAL'\n"
10956               "    CanonicalDelimiter: 'cc'",
10957               RawStringFormats, ExpectedRawStringFormats);
10958 }
10959 
10960 TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
10961   FormatStyle Style = {};
10962   Style.Language = FormatStyle::LK_Cpp;
10963   CHECK_PARSE("Language: Cpp\n"
10964               "IndentWidth: 12",
10965               IndentWidth, 12u);
10966   EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
10967                                "IndentWidth: 34",
10968                                &Style),
10969             ParseError::Unsuitable);
10970   EXPECT_EQ(12u, Style.IndentWidth);
10971   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
10972   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
10973 
10974   Style.Language = FormatStyle::LK_JavaScript;
10975   CHECK_PARSE("Language: JavaScript\n"
10976               "IndentWidth: 12",
10977               IndentWidth, 12u);
10978   CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
10979   EXPECT_EQ(parseConfiguration("Language: Cpp\n"
10980                                "IndentWidth: 34",
10981                                &Style),
10982             ParseError::Unsuitable);
10983   EXPECT_EQ(23u, Style.IndentWidth);
10984   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
10985   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
10986 
10987   CHECK_PARSE("BasedOnStyle: LLVM\n"
10988               "IndentWidth: 67",
10989               IndentWidth, 67u);
10990 
10991   CHECK_PARSE("---\n"
10992               "Language: JavaScript\n"
10993               "IndentWidth: 12\n"
10994               "---\n"
10995               "Language: Cpp\n"
10996               "IndentWidth: 34\n"
10997               "...\n",
10998               IndentWidth, 12u);
10999 
11000   Style.Language = FormatStyle::LK_Cpp;
11001   CHECK_PARSE("---\n"
11002               "Language: JavaScript\n"
11003               "IndentWidth: 12\n"
11004               "---\n"
11005               "Language: Cpp\n"
11006               "IndentWidth: 34\n"
11007               "...\n",
11008               IndentWidth, 34u);
11009   CHECK_PARSE("---\n"
11010               "IndentWidth: 78\n"
11011               "---\n"
11012               "Language: JavaScript\n"
11013               "IndentWidth: 56\n"
11014               "...\n",
11015               IndentWidth, 78u);
11016 
11017   Style.ColumnLimit = 123;
11018   Style.IndentWidth = 234;
11019   Style.BreakBeforeBraces = FormatStyle::BS_Linux;
11020   Style.TabWidth = 345;
11021   EXPECT_FALSE(parseConfiguration("---\n"
11022                                   "IndentWidth: 456\n"
11023                                   "BreakBeforeBraces: Allman\n"
11024                                   "---\n"
11025                                   "Language: JavaScript\n"
11026                                   "IndentWidth: 111\n"
11027                                   "TabWidth: 111\n"
11028                                   "---\n"
11029                                   "Language: Cpp\n"
11030                                   "BreakBeforeBraces: Stroustrup\n"
11031                                   "TabWidth: 789\n"
11032                                   "...\n",
11033                                   &Style));
11034   EXPECT_EQ(123u, Style.ColumnLimit);
11035   EXPECT_EQ(456u, Style.IndentWidth);
11036   EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
11037   EXPECT_EQ(789u, Style.TabWidth);
11038 
11039   EXPECT_EQ(parseConfiguration("---\n"
11040                                "Language: JavaScript\n"
11041                                "IndentWidth: 56\n"
11042                                "---\n"
11043                                "IndentWidth: 78\n"
11044                                "...\n",
11045                                &Style),
11046             ParseError::Error);
11047   EXPECT_EQ(parseConfiguration("---\n"
11048                                "Language: JavaScript\n"
11049                                "IndentWidth: 56\n"
11050                                "---\n"
11051                                "Language: JavaScript\n"
11052                                "IndentWidth: 78\n"
11053                                "...\n",
11054                                &Style),
11055             ParseError::Error);
11056 
11057   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
11058 }
11059 
11060 #undef CHECK_PARSE
11061 
11062 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
11063   FormatStyle Style = {};
11064   Style.Language = FormatStyle::LK_JavaScript;
11065   Style.BreakBeforeTernaryOperators = true;
11066   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
11067   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
11068 
11069   Style.BreakBeforeTernaryOperators = true;
11070   EXPECT_EQ(0, parseConfiguration("---\n"
11071                                   "BasedOnStyle: Google\n"
11072                                   "---\n"
11073                                   "Language: JavaScript\n"
11074                                   "IndentWidth: 76\n"
11075                                   "...\n",
11076                                   &Style)
11077                    .value());
11078   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
11079   EXPECT_EQ(76u, Style.IndentWidth);
11080   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
11081 }
11082 
11083 TEST_F(FormatTest, ConfigurationRoundTripTest) {
11084   FormatStyle Style = getLLVMStyle();
11085   std::string YAML = configurationAsText(Style);
11086   FormatStyle ParsedStyle = {};
11087   ParsedStyle.Language = FormatStyle::LK_Cpp;
11088   EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
11089   EXPECT_EQ(Style, ParsedStyle);
11090 }
11091 
11092 TEST_F(FormatTest, WorksFor8bitEncodings) {
11093   EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
11094             "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
11095             "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
11096             "\"\xef\xee\xf0\xf3...\"",
11097             format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
11098                    "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
11099                    "\xef\xee\xf0\xf3...\"",
11100                    getLLVMStyleWithColumns(12)));
11101 }
11102 
11103 TEST_F(FormatTest, HandlesUTF8BOM) {
11104   EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
11105   EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
11106             format("\xef\xbb\xbf#include <iostream>"));
11107   EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
11108             format("\xef\xbb\xbf\n#include <iostream>"));
11109 }
11110 
11111 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
11112 #if !defined(_MSC_VER)
11113 
11114 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
11115   verifyFormat("\"Однажды в студёную зимнюю пору...\"",
11116                getLLVMStyleWithColumns(35));
11117   verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
11118                getLLVMStyleWithColumns(31));
11119   verifyFormat("// Однажды в студёную зимнюю пору...",
11120                getLLVMStyleWithColumns(36));
11121   verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
11122   verifyFormat("/* Однажды в студёную зимнюю пору... */",
11123                getLLVMStyleWithColumns(39));
11124   verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
11125                getLLVMStyleWithColumns(35));
11126 }
11127 
11128 TEST_F(FormatTest, SplitsUTF8Strings) {
11129   // Non-printable characters' width is currently considered to be the length in
11130   // bytes in UTF8. The characters can be displayed in very different manner
11131   // (zero-width, single width with a substitution glyph, expanded to their code
11132   // (e.g. "<8d>"), so there's no single correct way to handle them.
11133   EXPECT_EQ("\"aaaaÄ\"\n"
11134             "\"\xc2\x8d\";",
11135             format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
11136   EXPECT_EQ("\"aaaaaaaÄ\"\n"
11137             "\"\xc2\x8d\";",
11138             format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
11139   EXPECT_EQ("\"Однажды, в \"\n"
11140             "\"студёную \"\n"
11141             "\"зимнюю \"\n"
11142             "\"пору,\"",
11143             format("\"Однажды, в студёную зимнюю пору,\"",
11144                    getLLVMStyleWithColumns(13)));
11145   EXPECT_EQ(
11146       "\"一 二 三 \"\n"
11147       "\"四 五六 \"\n"
11148       "\"七 八 九 \"\n"
11149       "\"十\"",
11150       format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
11151   EXPECT_EQ("\"一\t\"\n"
11152             "\"二 \t\"\n"
11153             "\"三 四 \"\n"
11154             "\"五\t\"\n"
11155             "\"六 \t\"\n"
11156             "\"七 \"\n"
11157             "\"八九十\tqq\"",
11158             format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
11159                    getLLVMStyleWithColumns(11)));
11160 
11161   // UTF8 character in an escape sequence.
11162   EXPECT_EQ("\"aaaaaa\"\n"
11163             "\"\\\xC2\x8D\"",
11164             format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
11165 }
11166 
11167 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
11168   EXPECT_EQ("const char *sssss =\n"
11169             "    \"一二三四五六七八\\\n"
11170             " 九 十\";",
11171             format("const char *sssss = \"一二三四五六七八\\\n"
11172                    " 九 十\";",
11173                    getLLVMStyleWithColumns(30)));
11174 }
11175 
11176 TEST_F(FormatTest, SplitsUTF8LineComments) {
11177   EXPECT_EQ("// aaaaÄ\xc2\x8d",
11178             format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
11179   EXPECT_EQ("// Я из лесу\n"
11180             "// вышел; был\n"
11181             "// сильный\n"
11182             "// мороз.",
11183             format("// Я из лесу вышел; был сильный мороз.",
11184                    getLLVMStyleWithColumns(13)));
11185   EXPECT_EQ("// 一二三\n"
11186             "// 四五六七\n"
11187             "// 八  九\n"
11188             "// 十",
11189             format("// 一二三 四五六七 八  九 十", getLLVMStyleWithColumns(9)));
11190 }
11191 
11192 TEST_F(FormatTest, SplitsUTF8BlockComments) {
11193   EXPECT_EQ("/* Гляжу,\n"
11194             " * поднимается\n"
11195             " * медленно в\n"
11196             " * гору\n"
11197             " * Лошадка,\n"
11198             " * везущая\n"
11199             " * хворосту\n"
11200             " * воз. */",
11201             format("/* Гляжу, поднимается медленно в гору\n"
11202                    " * Лошадка, везущая хворосту воз. */",
11203                    getLLVMStyleWithColumns(13)));
11204   EXPECT_EQ(
11205       "/* 一二三\n"
11206       " * 四五六七\n"
11207       " * 八  九\n"
11208       " * 十  */",
11209       format("/* 一二三 四五六七 八  九 十  */", getLLVMStyleWithColumns(9)));
11210   EXPECT_EQ("/* �������� ��������\n"
11211             " * ��������\n"
11212             " * ������-�� */",
11213             format("/* �������� �������� �������� ������-�� */", getLLVMStyleWithColumns(12)));
11214 }
11215 
11216 #endif // _MSC_VER
11217 
11218 TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
11219   FormatStyle Style = getLLVMStyle();
11220 
11221   Style.ConstructorInitializerIndentWidth = 4;
11222   verifyFormat(
11223       "SomeClass::Constructor()\n"
11224       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
11225       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
11226       Style);
11227 
11228   Style.ConstructorInitializerIndentWidth = 2;
11229   verifyFormat(
11230       "SomeClass::Constructor()\n"
11231       "  : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
11232       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
11233       Style);
11234 
11235   Style.ConstructorInitializerIndentWidth = 0;
11236   verifyFormat(
11237       "SomeClass::Constructor()\n"
11238       ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
11239       "  aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
11240       Style);
11241   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
11242   verifyFormat(
11243       "SomeLongTemplateVariableName<\n"
11244       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>",
11245       Style);
11246   verifyFormat(
11247       "bool smaller = 1 < bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
11248       "                       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
11249       Style);
11250 }
11251 
11252 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
11253   FormatStyle Style = getLLVMStyle();
11254   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
11255   Style.ConstructorInitializerIndentWidth = 4;
11256   verifyFormat("SomeClass::Constructor()\n"
11257                "    : a(a)\n"
11258                "    , b(b)\n"
11259                "    , c(c) {}",
11260                Style);
11261   verifyFormat("SomeClass::Constructor()\n"
11262                "    : a(a) {}",
11263                Style);
11264 
11265   Style.ColumnLimit = 0;
11266   verifyFormat("SomeClass::Constructor()\n"
11267                "    : a(a) {}",
11268                Style);
11269   verifyFormat("SomeClass::Constructor() noexcept\n"
11270                "    : a(a) {}",
11271                Style);
11272   verifyFormat("SomeClass::Constructor()\n"
11273                "    : a(a)\n"
11274                "    , b(b)\n"
11275                "    , c(c) {}",
11276                Style);
11277   verifyFormat("SomeClass::Constructor()\n"
11278                "    : a(a) {\n"
11279                "  foo();\n"
11280                "  bar();\n"
11281                "}",
11282                Style);
11283 
11284   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
11285   verifyFormat("SomeClass::Constructor()\n"
11286                "    : a(a)\n"
11287                "    , b(b)\n"
11288                "    , c(c) {\n}",
11289                Style);
11290   verifyFormat("SomeClass::Constructor()\n"
11291                "    : a(a) {\n}",
11292                Style);
11293 
11294   Style.ColumnLimit = 80;
11295   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
11296   Style.ConstructorInitializerIndentWidth = 2;
11297   verifyFormat("SomeClass::Constructor()\n"
11298                "  : a(a)\n"
11299                "  , b(b)\n"
11300                "  , c(c) {}",
11301                Style);
11302 
11303   Style.ConstructorInitializerIndentWidth = 0;
11304   verifyFormat("SomeClass::Constructor()\n"
11305                ": a(a)\n"
11306                ", b(b)\n"
11307                ", c(c) {}",
11308                Style);
11309 
11310   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
11311   Style.ConstructorInitializerIndentWidth = 4;
11312   verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
11313   verifyFormat(
11314       "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
11315       Style);
11316   verifyFormat(
11317       "SomeClass::Constructor()\n"
11318       "    : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
11319       Style);
11320   Style.ConstructorInitializerIndentWidth = 4;
11321   Style.ColumnLimit = 60;
11322   verifyFormat("SomeClass::Constructor()\n"
11323                "    : aaaaaaaa(aaaaaaaa)\n"
11324                "    , aaaaaaaa(aaaaaaaa)\n"
11325                "    , aaaaaaaa(aaaaaaaa) {}",
11326                Style);
11327 }
11328 
11329 TEST_F(FormatTest, Destructors) {
11330   verifyFormat("void F(int &i) { i.~int(); }");
11331   verifyFormat("void F(int &i) { i->~int(); }");
11332 }
11333 
11334 TEST_F(FormatTest, FormatsWithWebKitStyle) {
11335   FormatStyle Style = getWebKitStyle();
11336 
11337   // Don't indent in outer namespaces.
11338   verifyFormat("namespace outer {\n"
11339                "int i;\n"
11340                "namespace inner {\n"
11341                "    int i;\n"
11342                "} // namespace inner\n"
11343                "} // namespace outer\n"
11344                "namespace other_outer {\n"
11345                "int i;\n"
11346                "}",
11347                Style);
11348 
11349   // Don't indent case labels.
11350   verifyFormat("switch (variable) {\n"
11351                "case 1:\n"
11352                "case 2:\n"
11353                "    doSomething();\n"
11354                "    break;\n"
11355                "default:\n"
11356                "    ++variable;\n"
11357                "}",
11358                Style);
11359 
11360   // Wrap before binary operators.
11361   EXPECT_EQ("void f()\n"
11362             "{\n"
11363             "    if (aaaaaaaaaaaaaaaa\n"
11364             "        && bbbbbbbbbbbbbbbbbbbbbbbb\n"
11365             "        && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
11366             "        return;\n"
11367             "}",
11368             format("void f() {\n"
11369                    "if (aaaaaaaaaaaaaaaa\n"
11370                    "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
11371                    "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
11372                    "return;\n"
11373                    "}",
11374                    Style));
11375 
11376   // Allow functions on a single line.
11377   verifyFormat("void f() { return; }", Style);
11378 
11379   // Constructor initializers are formatted one per line with the "," on the
11380   // new line.
11381   verifyFormat("Constructor()\n"
11382                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
11383                "    , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
11384                "          aaaaaaaaaaaaaa)\n"
11385                "    , aaaaaaaaaaaaaaaaaaaaaaa()\n"
11386                "{\n"
11387                "}",
11388                Style);
11389   verifyFormat("SomeClass::Constructor()\n"
11390                "    : a(a)\n"
11391                "{\n"
11392                "}",
11393                Style);
11394   EXPECT_EQ("SomeClass::Constructor()\n"
11395             "    : a(a)\n"
11396             "{\n"
11397             "}",
11398             format("SomeClass::Constructor():a(a){}", Style));
11399   verifyFormat("SomeClass::Constructor()\n"
11400                "    : a(a)\n"
11401                "    , b(b)\n"
11402                "    , c(c)\n"
11403                "{\n"
11404                "}",
11405                Style);
11406   verifyFormat("SomeClass::Constructor()\n"
11407                "    : a(a)\n"
11408                "{\n"
11409                "    foo();\n"
11410                "    bar();\n"
11411                "}",
11412                Style);
11413 
11414   // Access specifiers should be aligned left.
11415   verifyFormat("class C {\n"
11416                "public:\n"
11417                "    int i;\n"
11418                "};",
11419                Style);
11420 
11421   // Do not align comments.
11422   verifyFormat("int a; // Do not\n"
11423                "double b; // align comments.",
11424                Style);
11425 
11426   // Do not align operands.
11427   EXPECT_EQ("ASSERT(aaaa\n"
11428             "    || bbbb);",
11429             format("ASSERT ( aaaa\n||bbbb);", Style));
11430 
11431   // Accept input's line breaks.
11432   EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
11433             "    || bbbbbbbbbbbbbbb) {\n"
11434             "    i++;\n"
11435             "}",
11436             format("if (aaaaaaaaaaaaaaa\n"
11437                    "|| bbbbbbbbbbbbbbb) { i++; }",
11438                    Style));
11439   EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
11440             "    i++;\n"
11441             "}",
11442             format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
11443 
11444   // Don't automatically break all macro definitions (llvm.org/PR17842).
11445   verifyFormat("#define aNumber 10", Style);
11446   // However, generally keep the line breaks that the user authored.
11447   EXPECT_EQ("#define aNumber \\\n"
11448             "    10",
11449             format("#define aNumber \\\n"
11450                    " 10",
11451                    Style));
11452 
11453   // Keep empty and one-element array literals on a single line.
11454   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
11455             "                                  copyItems:YES];",
11456             format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
11457                    "copyItems:YES];",
11458                    Style));
11459   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
11460             "                                  copyItems:YES];",
11461             format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
11462                    "             copyItems:YES];",
11463                    Style));
11464   // FIXME: This does not seem right, there should be more indentation before
11465   // the array literal's entries. Nested blocks have the same problem.
11466   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
11467             "    @\"a\",\n"
11468             "    @\"a\"\n"
11469             "]\n"
11470             "                                  copyItems:YES];",
11471             format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
11472                    "     @\"a\",\n"
11473                    "     @\"a\"\n"
11474                    "     ]\n"
11475                    "       copyItems:YES];",
11476                    Style));
11477   EXPECT_EQ(
11478       "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
11479       "                                  copyItems:YES];",
11480       format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
11481              "   copyItems:YES];",
11482              Style));
11483 
11484   verifyFormat("[self.a b:c c:d];", Style);
11485   EXPECT_EQ("[self.a b:c\n"
11486             "        c:d];",
11487             format("[self.a b:c\n"
11488                    "c:d];",
11489                    Style));
11490 }
11491 
11492 TEST_F(FormatTest, FormatsLambdas) {
11493   verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
11494   verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
11495   verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
11496   verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
11497   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
11498   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
11499   verifyFormat("auto c = [a = [b = 42] {}] {};\n");
11500   verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n");
11501   verifyFormat("int x = f(*+[] {});");
11502   verifyFormat("void f() {\n"
11503                "  other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
11504                "}\n");
11505   verifyFormat("void f() {\n"
11506                "  other(x.begin(), //\n"
11507                "        x.end(),   //\n"
11508                "        [&](int, int) { return 1; });\n"
11509                "}\n");
11510   verifyFormat("SomeFunction([]() { // A cool function...\n"
11511                "  return 43;\n"
11512                "});");
11513   EXPECT_EQ("SomeFunction([]() {\n"
11514             "#define A a\n"
11515             "  return 43;\n"
11516             "});",
11517             format("SomeFunction([](){\n"
11518                    "#define A a\n"
11519                    "return 43;\n"
11520                    "});"));
11521   verifyFormat("void f() {\n"
11522                "  SomeFunction([](decltype(x), A *a) {});\n"
11523                "}");
11524   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11525                "    [](const aaaaaaaaaa &a) { return a; });");
11526   verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
11527                "  SomeOtherFunctioooooooooooooooooooooooooon();\n"
11528                "});");
11529   verifyFormat("Constructor()\n"
11530                "    : Field([] { // comment\n"
11531                "        int i;\n"
11532                "      }) {}");
11533   verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
11534                "  return some_parameter.size();\n"
11535                "};");
11536   verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n"
11537                "    [](const string &s) { return s; };");
11538   verifyFormat("int i = aaaaaa ? 1 //\n"
11539                "               : [] {\n"
11540                "                   return 2; //\n"
11541                "                 }();");
11542   verifyFormat("llvm::errs() << \"number of twos is \"\n"
11543                "             << std::count_if(v.begin(), v.end(), [](int x) {\n"
11544                "                  return x == 2; // force break\n"
11545                "                });");
11546   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11547                "    [=](int iiiiiiiiiiii) {\n"
11548                "      return aaaaaaaaaaaaaaaaaaaaaaa !=\n"
11549                "             aaaaaaaaaaaaaaaaaaaaaaa;\n"
11550                "    });",
11551                getLLVMStyleWithColumns(60));
11552   verifyFormat("SomeFunction({[&] {\n"
11553                "                // comment\n"
11554                "              },\n"
11555                "              [&] {\n"
11556                "                // comment\n"
11557                "              }});");
11558   verifyFormat("SomeFunction({[&] {\n"
11559                "  // comment\n"
11560                "}});");
11561   verifyFormat("virtual aaaaaaaaaaaaaaaa(std::function<bool()> bbbbbbbbbbbb =\n"
11562                "                             [&]() { return true; },\n"
11563                "                         aaaaa aaaaaaaaa);");
11564 
11565   // Lambdas with return types.
11566   verifyFormat("int c = []() -> int { return 2; }();\n");
11567   verifyFormat("int c = []() -> int * { return 2; }();\n");
11568   verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
11569   verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
11570   verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
11571   verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
11572   verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
11573   verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
11574   verifyFormat("[a, a]() -> a<1> {};");
11575   verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
11576                "                   int j) -> int {\n"
11577                "  return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
11578                "};");
11579   verifyFormat(
11580       "aaaaaaaaaaaaaaaaaaaaaa(\n"
11581       "    [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
11582       "      return aaaaaaaaaaaaaaaaa;\n"
11583       "    });",
11584       getLLVMStyleWithColumns(70));
11585   verifyFormat("[]() //\n"
11586                "    -> int {\n"
11587                "  return 1; //\n"
11588                "};");
11589 
11590   // Multiple lambdas in the same parentheses change indentation rules.
11591   verifyFormat("SomeFunction(\n"
11592                "    []() {\n"
11593                "      int i = 42;\n"
11594                "      return i;\n"
11595                "    },\n"
11596                "    []() {\n"
11597                "      int j = 43;\n"
11598                "      return j;\n"
11599                "    });");
11600 
11601   // More complex introducers.
11602   verifyFormat("return [i, args...] {};");
11603 
11604   // Not lambdas.
11605   verifyFormat("constexpr char hello[]{\"hello\"};");
11606   verifyFormat("double &operator[](int i) { return 0; }\n"
11607                "int i;");
11608   verifyFormat("std::unique_ptr<int[]> foo() {}");
11609   verifyFormat("int i = a[a][a]->f();");
11610   verifyFormat("int i = (*b)[a]->f();");
11611 
11612   // Other corner cases.
11613   verifyFormat("void f() {\n"
11614                "  bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
11615                "  );\n"
11616                "}");
11617 
11618   // Lambdas created through weird macros.
11619   verifyFormat("void f() {\n"
11620                "  MACRO((const AA &a) { return 1; });\n"
11621                "  MACRO((AA &a) { return 1; });\n"
11622                "}");
11623 
11624   verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
11625                "      doo_dah();\n"
11626                "      doo_dah();\n"
11627                "    })) {\n"
11628                "}");
11629   verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n"
11630                "                doo_dah();\n"
11631                "                doo_dah();\n"
11632                "              })) {\n"
11633                "}");
11634   verifyFormat("auto lambda = []() {\n"
11635                "  int a = 2\n"
11636                "#if A\n"
11637                "          + 2\n"
11638                "#endif\n"
11639                "      ;\n"
11640                "};");
11641 
11642   // Lambdas with complex multiline introducers.
11643   verifyFormat(
11644       "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11645       "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n"
11646       "        -> ::std::unordered_set<\n"
11647       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
11648       "      //\n"
11649       "    });");
11650 }
11651 
11652 TEST_F(FormatTest, EmptyLinesInLambdas) {
11653   verifyFormat("auto lambda = []() {\n"
11654                "  x(); //\n"
11655                "};",
11656                "auto lambda = []() {\n"
11657                "\n"
11658                "  x(); //\n"
11659                "\n"
11660                "};");
11661 }
11662 
11663 TEST_F(FormatTest, FormatsBlocks) {
11664   FormatStyle ShortBlocks = getLLVMStyle();
11665   ShortBlocks.AllowShortBlocksOnASingleLine = true;
11666   verifyFormat("int (^Block)(int, int);", ShortBlocks);
11667   verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
11668   verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
11669   verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
11670   verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
11671   verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
11672 
11673   verifyFormat("foo(^{ bar(); });", ShortBlocks);
11674   verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
11675   verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
11676 
11677   verifyFormat("[operation setCompletionBlock:^{\n"
11678                "  [self onOperationDone];\n"
11679                "}];");
11680   verifyFormat("int i = {[operation setCompletionBlock:^{\n"
11681                "  [self onOperationDone];\n"
11682                "}]};");
11683   verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
11684                "  f();\n"
11685                "}];");
11686   verifyFormat("int a = [operation block:^int(int *i) {\n"
11687                "  return 1;\n"
11688                "}];");
11689   verifyFormat("[myObject doSomethingWith:arg1\n"
11690                "                      aaa:^int(int *a) {\n"
11691                "                        return 1;\n"
11692                "                      }\n"
11693                "                      bbb:f(a * bbbbbbbb)];");
11694 
11695   verifyFormat("[operation setCompletionBlock:^{\n"
11696                "  [self.delegate newDataAvailable];\n"
11697                "}];",
11698                getLLVMStyleWithColumns(60));
11699   verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
11700                "  NSString *path = [self sessionFilePath];\n"
11701                "  if (path) {\n"
11702                "    // ...\n"
11703                "  }\n"
11704                "});");
11705   verifyFormat("[[SessionService sharedService]\n"
11706                "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
11707                "      if (window) {\n"
11708                "        [self windowDidLoad:window];\n"
11709                "      } else {\n"
11710                "        [self errorLoadingWindow];\n"
11711                "      }\n"
11712                "    }];");
11713   verifyFormat("void (^largeBlock)(void) = ^{\n"
11714                "  // ...\n"
11715                "};\n",
11716                getLLVMStyleWithColumns(40));
11717   verifyFormat("[[SessionService sharedService]\n"
11718                "    loadWindowWithCompletionBlock: //\n"
11719                "        ^(SessionWindow *window) {\n"
11720                "          if (window) {\n"
11721                "            [self windowDidLoad:window];\n"
11722                "          } else {\n"
11723                "            [self errorLoadingWindow];\n"
11724                "          }\n"
11725                "        }];",
11726                getLLVMStyleWithColumns(60));
11727   verifyFormat("[myObject doSomethingWith:arg1\n"
11728                "    firstBlock:^(Foo *a) {\n"
11729                "      // ...\n"
11730                "      int i;\n"
11731                "    }\n"
11732                "    secondBlock:^(Bar *b) {\n"
11733                "      // ...\n"
11734                "      int i;\n"
11735                "    }\n"
11736                "    thirdBlock:^Foo(Bar *b) {\n"
11737                "      // ...\n"
11738                "      int i;\n"
11739                "    }];");
11740   verifyFormat("[myObject doSomethingWith:arg1\n"
11741                "               firstBlock:-1\n"
11742                "              secondBlock:^(Bar *b) {\n"
11743                "                // ...\n"
11744                "                int i;\n"
11745                "              }];");
11746 
11747   verifyFormat("f(^{\n"
11748                "  @autoreleasepool {\n"
11749                "    if (a) {\n"
11750                "      g();\n"
11751                "    }\n"
11752                "  }\n"
11753                "});");
11754   verifyFormat("Block b = ^int *(A *a, B *b) {}");
11755   verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n"
11756                "};");
11757 
11758   FormatStyle FourIndent = getLLVMStyle();
11759   FourIndent.ObjCBlockIndentWidth = 4;
11760   verifyFormat("[operation setCompletionBlock:^{\n"
11761                "    [self onOperationDone];\n"
11762                "}];",
11763                FourIndent);
11764 }
11765 
11766 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
11767   FormatStyle ZeroColumn = getLLVMStyle();
11768   ZeroColumn.ColumnLimit = 0;
11769 
11770   verifyFormat("[[SessionService sharedService] "
11771                "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
11772                "  if (window) {\n"
11773                "    [self windowDidLoad:window];\n"
11774                "  } else {\n"
11775                "    [self errorLoadingWindow];\n"
11776                "  }\n"
11777                "}];",
11778                ZeroColumn);
11779   EXPECT_EQ("[[SessionService sharedService]\n"
11780             "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
11781             "      if (window) {\n"
11782             "        [self windowDidLoad:window];\n"
11783             "      } else {\n"
11784             "        [self errorLoadingWindow];\n"
11785             "      }\n"
11786             "    }];",
11787             format("[[SessionService sharedService]\n"
11788                    "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
11789                    "                if (window) {\n"
11790                    "    [self windowDidLoad:window];\n"
11791                    "  } else {\n"
11792                    "    [self errorLoadingWindow];\n"
11793                    "  }\n"
11794                    "}];",
11795                    ZeroColumn));
11796   verifyFormat("[myObject doSomethingWith:arg1\n"
11797                "    firstBlock:^(Foo *a) {\n"
11798                "      // ...\n"
11799                "      int i;\n"
11800                "    }\n"
11801                "    secondBlock:^(Bar *b) {\n"
11802                "      // ...\n"
11803                "      int i;\n"
11804                "    }\n"
11805                "    thirdBlock:^Foo(Bar *b) {\n"
11806                "      // ...\n"
11807                "      int i;\n"
11808                "    }];",
11809                ZeroColumn);
11810   verifyFormat("f(^{\n"
11811                "  @autoreleasepool {\n"
11812                "    if (a) {\n"
11813                "      g();\n"
11814                "    }\n"
11815                "  }\n"
11816                "});",
11817                ZeroColumn);
11818   verifyFormat("void (^largeBlock)(void) = ^{\n"
11819                "  // ...\n"
11820                "};",
11821                ZeroColumn);
11822 
11823   ZeroColumn.AllowShortBlocksOnASingleLine = true;
11824   EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
11825             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
11826   ZeroColumn.AllowShortBlocksOnASingleLine = false;
11827   EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
11828             "  int i;\n"
11829             "};",
11830             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
11831 }
11832 
11833 TEST_F(FormatTest, SupportsCRLF) {
11834   EXPECT_EQ("int a;\r\n"
11835             "int b;\r\n"
11836             "int c;\r\n",
11837             format("int a;\r\n"
11838                    "  int b;\r\n"
11839                    "    int c;\r\n",
11840                    getLLVMStyle()));
11841   EXPECT_EQ("int a;\r\n"
11842             "int b;\r\n"
11843             "int c;\r\n",
11844             format("int a;\r\n"
11845                    "  int b;\n"
11846                    "    int c;\r\n",
11847                    getLLVMStyle()));
11848   EXPECT_EQ("int a;\n"
11849             "int b;\n"
11850             "int c;\n",
11851             format("int a;\r\n"
11852                    "  int b;\n"
11853                    "    int c;\n",
11854                    getLLVMStyle()));
11855   EXPECT_EQ("\"aaaaaaa \"\r\n"
11856             "\"bbbbbbb\";\r\n",
11857             format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
11858   EXPECT_EQ("#define A \\\r\n"
11859             "  b;      \\\r\n"
11860             "  c;      \\\r\n"
11861             "  d;\r\n",
11862             format("#define A \\\r\n"
11863                    "  b; \\\r\n"
11864                    "  c; d; \r\n",
11865                    getGoogleStyle()));
11866 
11867   EXPECT_EQ("/*\r\n"
11868             "multi line block comments\r\n"
11869             "should not introduce\r\n"
11870             "an extra carriage return\r\n"
11871             "*/\r\n",
11872             format("/*\r\n"
11873                    "multi line block comments\r\n"
11874                    "should not introduce\r\n"
11875                    "an extra carriage return\r\n"
11876                    "*/\r\n"));
11877 }
11878 
11879 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
11880   verifyFormat("MY_CLASS(C) {\n"
11881                "  int i;\n"
11882                "  int j;\n"
11883                "};");
11884 }
11885 
11886 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
11887   FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
11888   TwoIndent.ContinuationIndentWidth = 2;
11889 
11890   EXPECT_EQ("int i =\n"
11891             "  longFunction(\n"
11892             "    arg);",
11893             format("int i = longFunction(arg);", TwoIndent));
11894 
11895   FormatStyle SixIndent = getLLVMStyleWithColumns(20);
11896   SixIndent.ContinuationIndentWidth = 6;
11897 
11898   EXPECT_EQ("int i =\n"
11899             "      longFunction(\n"
11900             "            arg);",
11901             format("int i = longFunction(arg);", SixIndent));
11902 }
11903 
11904 TEST_F(FormatTest, SpacesInAngles) {
11905   FormatStyle Spaces = getLLVMStyle();
11906   Spaces.SpacesInAngles = true;
11907 
11908   verifyFormat("static_cast< int >(arg);", Spaces);
11909   verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
11910   verifyFormat("f< int, float >();", Spaces);
11911   verifyFormat("template <> g() {}", Spaces);
11912   verifyFormat("template < std::vector< int > > f() {}", Spaces);
11913   verifyFormat("std::function< void(int, int) > fct;", Spaces);
11914   verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
11915                Spaces);
11916 
11917   Spaces.Standard = FormatStyle::LS_Cpp03;
11918   Spaces.SpacesInAngles = true;
11919   verifyFormat("A< A< int > >();", Spaces);
11920 
11921   Spaces.SpacesInAngles = false;
11922   verifyFormat("A<A<int> >();", Spaces);
11923 
11924   Spaces.Standard = FormatStyle::LS_Cpp11;
11925   Spaces.SpacesInAngles = true;
11926   verifyFormat("A< A< int > >();", Spaces);
11927 
11928   Spaces.SpacesInAngles = false;
11929   verifyFormat("A<A<int>>();", Spaces);
11930 }
11931 
11932 TEST_F(FormatTest, SpaceAfterTemplateKeyword) {
11933   FormatStyle Style = getLLVMStyle();
11934   Style.SpaceAfterTemplateKeyword = false;
11935   verifyFormat("template<int> void foo();", Style);
11936 }
11937 
11938 TEST_F(FormatTest, TripleAngleBrackets) {
11939   verifyFormat("f<<<1, 1>>>();");
11940   verifyFormat("f<<<1, 1, 1, s>>>();");
11941   verifyFormat("f<<<a, b, c, d>>>();");
11942   EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();"));
11943   verifyFormat("f<param><<<1, 1>>>();");
11944   verifyFormat("f<1><<<1, 1>>>();");
11945   EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();"));
11946   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
11947                "aaaaaaaaaaa<<<\n    1, 1>>>();");
11948   verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n"
11949                "    <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();");
11950 }
11951 
11952 TEST_F(FormatTest, MergeLessLessAtEnd) {
11953   verifyFormat("<<");
11954   EXPECT_EQ("< < <", format("\\\n<<<"));
11955   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
11956                "aaallvm::outs() <<");
11957   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
11958                "aaaallvm::outs()\n    <<");
11959 }
11960 
11961 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
11962   std::string code = "#if A\n"
11963                      "#if B\n"
11964                      "a.\n"
11965                      "#endif\n"
11966                      "    a = 1;\n"
11967                      "#else\n"
11968                      "#endif\n"
11969                      "#if C\n"
11970                      "#else\n"
11971                      "#endif\n";
11972   EXPECT_EQ(code, format(code));
11973 }
11974 
11975 TEST_F(FormatTest, HandleConflictMarkers) {
11976   // Git/SVN conflict markers.
11977   EXPECT_EQ("int a;\n"
11978             "void f() {\n"
11979             "  callme(some(parameter1,\n"
11980             "<<<<<<< text by the vcs\n"
11981             "              parameter2),\n"
11982             "||||||| text by the vcs\n"
11983             "              parameter2),\n"
11984             "         parameter3,\n"
11985             "======= text by the vcs\n"
11986             "              parameter2, parameter3),\n"
11987             ">>>>>>> text by the vcs\n"
11988             "         otherparameter);\n",
11989             format("int a;\n"
11990                    "void f() {\n"
11991                    "  callme(some(parameter1,\n"
11992                    "<<<<<<< text by the vcs\n"
11993                    "  parameter2),\n"
11994                    "||||||| text by the vcs\n"
11995                    "  parameter2),\n"
11996                    "  parameter3,\n"
11997                    "======= text by the vcs\n"
11998                    "  parameter2,\n"
11999                    "  parameter3),\n"
12000                    ">>>>>>> text by the vcs\n"
12001                    "  otherparameter);\n"));
12002 
12003   // Perforce markers.
12004   EXPECT_EQ("void f() {\n"
12005             "  function(\n"
12006             ">>>> text by the vcs\n"
12007             "      parameter,\n"
12008             "==== text by the vcs\n"
12009             "      parameter,\n"
12010             "==== text by the vcs\n"
12011             "      parameter,\n"
12012             "<<<< text by the vcs\n"
12013             "      parameter);\n",
12014             format("void f() {\n"
12015                    "  function(\n"
12016                    ">>>> text by the vcs\n"
12017                    "  parameter,\n"
12018                    "==== text by the vcs\n"
12019                    "  parameter,\n"
12020                    "==== text by the vcs\n"
12021                    "  parameter,\n"
12022                    "<<<< text by the vcs\n"
12023                    "  parameter);\n"));
12024 
12025   EXPECT_EQ("<<<<<<<\n"
12026             "|||||||\n"
12027             "=======\n"
12028             ">>>>>>>",
12029             format("<<<<<<<\n"
12030                    "|||||||\n"
12031                    "=======\n"
12032                    ">>>>>>>"));
12033 
12034   EXPECT_EQ("<<<<<<<\n"
12035             "|||||||\n"
12036             "int i;\n"
12037             "=======\n"
12038             ">>>>>>>",
12039             format("<<<<<<<\n"
12040                    "|||||||\n"
12041                    "int i;\n"
12042                    "=======\n"
12043                    ">>>>>>>"));
12044 
12045   // FIXME: Handle parsing of macros around conflict markers correctly:
12046   EXPECT_EQ("#define Macro \\\n"
12047             "<<<<<<<\n"
12048             "Something \\\n"
12049             "|||||||\n"
12050             "Else \\\n"
12051             "=======\n"
12052             "Other \\\n"
12053             ">>>>>>>\n"
12054             "    End int i;\n",
12055             format("#define Macro \\\n"
12056                    "<<<<<<<\n"
12057                    "  Something \\\n"
12058                    "|||||||\n"
12059                    "  Else \\\n"
12060                    "=======\n"
12061                    "  Other \\\n"
12062                    ">>>>>>>\n"
12063                    "  End\n"
12064                    "int i;\n"));
12065 }
12066 
12067 TEST_F(FormatTest, DisableRegions) {
12068   EXPECT_EQ("int i;\n"
12069             "// clang-format off\n"
12070             "  int j;\n"
12071             "// clang-format on\n"
12072             "int k;",
12073             format(" int  i;\n"
12074                    "   // clang-format off\n"
12075                    "  int j;\n"
12076                    " // clang-format on\n"
12077                    "   int   k;"));
12078   EXPECT_EQ("int i;\n"
12079             "/* clang-format off */\n"
12080             "  int j;\n"
12081             "/* clang-format on */\n"
12082             "int k;",
12083             format(" int  i;\n"
12084                    "   /* clang-format off */\n"
12085                    "  int j;\n"
12086                    " /* clang-format on */\n"
12087                    "   int   k;"));
12088 
12089   // Don't reflow comments within disabled regions.
12090   EXPECT_EQ(
12091       "// clang-format off\n"
12092       "// long long long long long long line\n"
12093       "/* clang-format on */\n"
12094       "/* long long long\n"
12095       " * long long long\n"
12096       " * line */\n"
12097       "int i;\n"
12098       "/* clang-format off */\n"
12099       "/* long long long long long long line */\n",
12100       format("// clang-format off\n"
12101              "// long long long long long long line\n"
12102              "/* clang-format on */\n"
12103              "/* long long long long long long line */\n"
12104              "int i;\n"
12105              "/* clang-format off */\n"
12106              "/* long long long long long long line */\n",
12107              getLLVMStyleWithColumns(20)));
12108 }
12109 
12110 TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
12111   format("? ) =");
12112   verifyNoCrash("#define a\\\n /**/}");
12113 }
12114 
12115 TEST_F(FormatTest, FormatsTableGenCode) {
12116   FormatStyle Style = getLLVMStyle();
12117   Style.Language = FormatStyle::LK_TableGen;
12118   verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style);
12119 }
12120 
12121 TEST_F(FormatTest, ArrayOfTemplates) {
12122   EXPECT_EQ("auto a = new unique_ptr<int>[10];",
12123             format("auto a = new unique_ptr<int > [ 10];"));
12124 
12125   FormatStyle Spaces = getLLVMStyle();
12126   Spaces.SpacesInSquareBrackets = true;
12127   EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];",
12128             format("auto a = new unique_ptr<int > [10];", Spaces));
12129 }
12130 
12131 TEST_F(FormatTest, ArrayAsTemplateType) {
12132   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;",
12133             format("auto a = unique_ptr < Foo < Bar>[ 10]> ;"));
12134 
12135   FormatStyle Spaces = getLLVMStyle();
12136   Spaces.SpacesInSquareBrackets = true;
12137   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;",
12138             format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces));
12139 }
12140 
12141 TEST_F(FormatTest, NoSpaceAfterSuper) {
12142     verifyFormat("__super::FooBar();");
12143 }
12144 
12145 TEST(FormatStyle, GetStyleWithEmptyFileName) {
12146   vfs::InMemoryFileSystem FS;
12147   auto Style1 = getStyle("file", "", "Google", "", &FS);
12148   ASSERT_TRUE((bool)Style1);
12149   ASSERT_EQ(*Style1, getGoogleStyle());
12150 }
12151 
12152 TEST(FormatStyle, GetStyleOfFile) {
12153   vfs::InMemoryFileSystem FS;
12154   // Test 1: format file in the same directory.
12155   ASSERT_TRUE(
12156       FS.addFile("/a/.clang-format", 0,
12157                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM")));
12158   ASSERT_TRUE(
12159       FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
12160   auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS);
12161   ASSERT_TRUE((bool)Style1);
12162   ASSERT_EQ(*Style1, getLLVMStyle());
12163 
12164   // Test 2.1: fallback to default.
12165   ASSERT_TRUE(
12166       FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
12167   auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS);
12168   ASSERT_TRUE((bool)Style2);
12169   ASSERT_EQ(*Style2, getMozillaStyle());
12170 
12171   // Test 2.2: no format on 'none' fallback style.
12172   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
12173   ASSERT_TRUE((bool)Style2);
12174   ASSERT_EQ(*Style2, getNoStyle());
12175 
12176   // Test 2.3: format if config is found with no based style while fallback is
12177   // 'none'.
12178   ASSERT_TRUE(FS.addFile("/b/.clang-format", 0,
12179                          llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2")));
12180   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
12181   ASSERT_TRUE((bool)Style2);
12182   ASSERT_EQ(*Style2, getLLVMStyle());
12183 
12184   // Test 2.4: format if yaml with no based style, while fallback is 'none'.
12185   Style2 = getStyle("{}", "a.h", "none", "", &FS);
12186   ASSERT_TRUE((bool)Style2);
12187   ASSERT_EQ(*Style2, getLLVMStyle());
12188 
12189   // Test 3: format file in parent directory.
12190   ASSERT_TRUE(
12191       FS.addFile("/c/.clang-format", 0,
12192                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
12193   ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0,
12194                          llvm::MemoryBuffer::getMemBuffer("int i;")));
12195   auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS);
12196   ASSERT_TRUE((bool)Style3);
12197   ASSERT_EQ(*Style3, getGoogleStyle());
12198 
12199   // Test 4: error on invalid fallback style
12200   auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS);
12201   ASSERT_FALSE((bool)Style4);
12202   llvm::consumeError(Style4.takeError());
12203 
12204   // Test 5: error on invalid yaml on command line
12205   auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS);
12206   ASSERT_FALSE((bool)Style5);
12207   llvm::consumeError(Style5.takeError());
12208 
12209   // Test 6: error on invalid style
12210   auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS);
12211   ASSERT_FALSE((bool)Style6);
12212   llvm::consumeError(Style6.takeError());
12213 
12214   // Test 7: found config file, error on parsing it
12215   ASSERT_TRUE(
12216       FS.addFile("/d/.clang-format", 0,
12217                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n"
12218                                                   "InvalidKey: InvalidValue")));
12219   ASSERT_TRUE(
12220       FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
12221   auto Style7 = getStyle("file", "/d/.clang-format", "LLVM", "", &FS);
12222   ASSERT_FALSE((bool)Style7);
12223   llvm::consumeError(Style7.takeError());
12224 }
12225 
12226 TEST_F(ReplacementTest, FormatCodeAfterReplacements) {
12227   // Column limit is 20.
12228   std::string Code = "Type *a =\n"
12229                      "    new Type();\n"
12230                      "g(iiiii, 0, jjjjj,\n"
12231                      "  0, kkkkk, 0, mm);\n"
12232                      "int  bad     = format   ;";
12233   std::string Expected = "auto a = new Type();\n"
12234                          "g(iiiii, nullptr,\n"
12235                          "  jjjjj, nullptr,\n"
12236                          "  kkkkk, nullptr,\n"
12237                          "  mm);\n"
12238                          "int  bad     = format   ;";
12239   FileID ID = Context.createInMemoryFile("format.cpp", Code);
12240   tooling::Replacements Replaces = toReplacements(
12241       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6,
12242                             "auto "),
12243        tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1,
12244                             "nullptr"),
12245        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1,
12246                             "nullptr"),
12247        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1,
12248                             "nullptr")});
12249 
12250   format::FormatStyle Style = format::getLLVMStyle();
12251   Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility.
12252   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
12253   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
12254       << llvm::toString(FormattedReplaces.takeError()) << "\n";
12255   auto Result = applyAllReplacements(Code, *FormattedReplaces);
12256   EXPECT_TRUE(static_cast<bool>(Result));
12257   EXPECT_EQ(Expected, *Result);
12258 }
12259 
12260 TEST_F(ReplacementTest, SortIncludesAfterReplacement) {
12261   std::string Code = "#include \"a.h\"\n"
12262                      "#include \"c.h\"\n"
12263                      "\n"
12264                      "int main() {\n"
12265                      "  return 0;\n"
12266                      "}";
12267   std::string Expected = "#include \"a.h\"\n"
12268                          "#include \"b.h\"\n"
12269                          "#include \"c.h\"\n"
12270                          "\n"
12271                          "int main() {\n"
12272                          "  return 0;\n"
12273                          "}";
12274   FileID ID = Context.createInMemoryFile("fix.cpp", Code);
12275   tooling::Replacements Replaces = toReplacements(
12276       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0,
12277                             "#include \"b.h\"\n")});
12278 
12279   format::FormatStyle Style = format::getLLVMStyle();
12280   Style.SortIncludes = true;
12281   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
12282   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
12283       << llvm::toString(FormattedReplaces.takeError()) << "\n";
12284   auto Result = applyAllReplacements(Code, *FormattedReplaces);
12285   EXPECT_TRUE(static_cast<bool>(Result));
12286   EXPECT_EQ(Expected, *Result);
12287 }
12288 
12289 TEST_F(FormatTest, FormatSortsUsingDeclarations) {
12290   EXPECT_EQ("using std::cin;\n"
12291             "using std::cout;",
12292             format("using std::cout;\n"
12293                    "using std::cin;", getGoogleStyle()));
12294 }
12295 
12296 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) {
12297   format::FormatStyle Style = format::getLLVMStyle();
12298   Style.Standard = FormatStyle::LS_Cpp03;
12299   // cpp03 recognize this string as identifier u8 and literal character 'a'
12300   EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style));
12301 }
12302 
12303 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) {
12304   // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers
12305   // all modes, including C++11, C++14 and C++17
12306   EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';"));
12307 }
12308 
12309 TEST_F(FormatTest, DoNotFormatLikelyXml) {
12310   EXPECT_EQ("<!-- ;> -->",
12311             format("<!-- ;> -->", getGoogleStyle()));
12312   EXPECT_EQ(" <!-- >; -->",
12313             format(" <!-- >; -->", getGoogleStyle()));
12314 }
12315 
12316 TEST_F(FormatTest, StructuredBindings) {
12317   // Structured bindings is a C++17 feature.
12318   // all modes, including C++11, C++14 and C++17
12319   verifyFormat("auto [a, b] = f();");
12320   EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();"));
12321   EXPECT_EQ("const auto [a, b] = f();", format("const   auto[a, b] = f();"));
12322   EXPECT_EQ("auto const [a, b] = f();", format("auto  const[a, b] = f();"));
12323   EXPECT_EQ("auto const volatile [a, b] = f();",
12324             format("auto  const   volatile[a, b] = f();"));
12325   EXPECT_EQ("auto [a, b, c] = f();", format("auto   [  a  ,  b,c   ] = f();"));
12326   EXPECT_EQ("auto &[a, b, c] = f();",
12327             format("auto   &[  a  ,  b,c   ] = f();"));
12328   EXPECT_EQ("auto &&[a, b, c] = f();",
12329             format("auto   &&[  a  ,  b,c   ] = f();"));
12330   EXPECT_EQ("auto const &[a, b] = f();", format("auto  const&[a, b] = f();"));
12331   EXPECT_EQ("auto const volatile &&[a, b] = f();",
12332             format("auto  const  volatile  &&[a, b] = f();"));
12333   EXPECT_EQ("auto const &&[a, b] = f();", format("auto  const   &&  [a, b] = f();"));
12334   EXPECT_EQ("const auto &[a, b] = f();", format("const  auto  &  [a, b] = f();"));
12335   EXPECT_EQ("const auto volatile &&[a, b] = f();",
12336             format("const  auto   volatile  &&[a, b] = f();"));
12337   EXPECT_EQ("volatile const auto &&[a, b] = f();",
12338             format("volatile  const  auto   &&[a, b] = f();"));
12339   EXPECT_EQ("const auto &&[a, b] = f();", format("const  auto  &&  [a, b] = f();"));
12340 
12341   // Make sure we don't mistake structured bindings for lambdas.
12342   FormatStyle PointerMiddle = getLLVMStyle();
12343   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
12344   verifyFormat("auto [a1, b]{A * i};", getGoogleStyle());
12345   verifyFormat("auto [a2, b]{A * i};", getLLVMStyle());
12346   verifyFormat("auto [a3, b]{A * i};", PointerMiddle);
12347   verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle());
12348   verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle());
12349   verifyFormat("auto const [a3, b]{A * i};", PointerMiddle);
12350   verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle());
12351   verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle());
12352   verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle);
12353   verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle());
12354   verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle());
12355   verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle);
12356 
12357   EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}",
12358             format("for (const auto   &&   [a, b] : some_range) {\n}"));
12359   EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}",
12360             format("for (const auto   &   [a, b] : some_range) {\n}"));
12361   EXPECT_EQ("for (const auto [a, b] : some_range) {\n}",
12362             format("for (const auto[a, b] : some_range) {\n}"));
12363   EXPECT_EQ("auto [x, y](expr);", format("auto[x,y]  (expr);"));
12364   EXPECT_EQ("auto &[x, y](expr);", format("auto  &  [x,y]  (expr);"));
12365   EXPECT_EQ("auto &&[x, y](expr);", format("auto  &&  [x,y]  (expr);"));
12366   EXPECT_EQ("auto const &[x, y](expr);", format("auto  const  &  [x,y]  (expr);"));
12367   EXPECT_EQ("auto const &&[x, y](expr);", format("auto  const  &&  [x,y]  (expr);"));
12368   EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y]     {expr};"));
12369   EXPECT_EQ("auto const &[x, y]{expr};", format("auto  const  &  [x,y]  {expr};"));
12370   EXPECT_EQ("auto const &&[x, y]{expr};", format("auto  const  &&  [x,y]  {expr};"));
12371 
12372   format::FormatStyle Spaces = format::getLLVMStyle();
12373   Spaces.SpacesInSquareBrackets = true;
12374   verifyFormat("auto [ a, b ] = f();", Spaces);
12375   verifyFormat("auto &&[ a, b ] = f();", Spaces);
12376   verifyFormat("auto &[ a, b ] = f();", Spaces);
12377   verifyFormat("auto const &&[ a, b ] = f();", Spaces);
12378   verifyFormat("auto const &[ a, b ] = f();", Spaces);
12379 }
12380 
12381 TEST_F(FormatTest, FileAndCode) {
12382   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", ""));
12383   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", ""));
12384   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", ""));
12385   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", ""));
12386   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@interface Foo\n@end\n"));
12387   EXPECT_EQ(
12388       FormatStyle::LK_ObjC,
12389       guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }"));
12390   EXPECT_EQ(FormatStyle::LK_ObjC,
12391             guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))"));
12392   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;"));
12393   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", ""));
12394   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo", "@interface Foo\n@end\n"));
12395   EXPECT_EQ(FormatStyle::LK_ObjC,
12396             guessLanguage("foo.h", "int DoStuff(CGRect rect);\n"));
12397   EXPECT_EQ(
12398       FormatStyle::LK_ObjC,
12399       guessLanguage("foo.h",
12400                     "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));\n"));
12401   EXPECT_EQ(
12402       FormatStyle::LK_Cpp,
12403       guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;"));
12404 }
12405 
12406 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) {
12407   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];"));
12408   EXPECT_EQ(FormatStyle::LK_ObjC,
12409             guessLanguage("foo.h", "array[[calculator getIndex]];"));
12410   EXPECT_EQ(FormatStyle::LK_Cpp,
12411             guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];"));
12412   EXPECT_EQ(
12413       FormatStyle::LK_Cpp,
12414       guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];"));
12415   EXPECT_EQ(FormatStyle::LK_ObjC,
12416             guessLanguage("foo.h", "[[noreturn foo] bar];"));
12417   EXPECT_EQ(FormatStyle::LK_Cpp,
12418             guessLanguage("foo.h", "[[clang::fallthrough]];"));
12419   EXPECT_EQ(FormatStyle::LK_ObjC,
12420             guessLanguage("foo.h", "[[clang:fallthrough] foo];"));
12421   EXPECT_EQ(FormatStyle::LK_Cpp,
12422             guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];"));
12423   EXPECT_EQ(FormatStyle::LK_Cpp,
12424             guessLanguage("foo.h", "[[using clang: fallthrough]];"));
12425   EXPECT_EQ(FormatStyle::LK_ObjC,
12426             guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];"));
12427   EXPECT_EQ(FormatStyle::LK_Cpp,
12428             guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];"));
12429   EXPECT_EQ(
12430       FormatStyle::LK_Cpp,
12431       guessLanguage("foo.h",
12432                     "[[clang::callable_when(\"unconsumed\", \"unknown\")]]"));
12433   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]"));
12434 }
12435 
12436 TEST_F(FormatTest, GuessLanguageWithCaret) {
12437   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);"));
12438   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);"));
12439   EXPECT_EQ(FormatStyle::LK_ObjC,
12440             guessLanguage("foo.h", "int(^)(char, float);"));
12441   EXPECT_EQ(FormatStyle::LK_ObjC,
12442             guessLanguage("foo.h", "int(^foo)(char, float);"));
12443   EXPECT_EQ(FormatStyle::LK_ObjC,
12444             guessLanguage("foo.h", "int(^foo[10])(char, float);"));
12445   EXPECT_EQ(FormatStyle::LK_ObjC,
12446             guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);"));
12447   EXPECT_EQ(
12448       FormatStyle::LK_ObjC,
12449       guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);"));
12450 }
12451 
12452 TEST_F(FormatTest, GuessLanguageWithChildLines) {
12453   EXPECT_EQ(FormatStyle::LK_Cpp,
12454             guessLanguage("foo.h", "#define FOO ({ std::string s; })"));
12455   EXPECT_EQ(FormatStyle::LK_ObjC,
12456             guessLanguage("foo.h", "#define FOO ({ NSString *s; })"));
12457   EXPECT_EQ(
12458       FormatStyle::LK_Cpp,
12459       guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })"));
12460   EXPECT_EQ(
12461       FormatStyle::LK_ObjC,
12462       guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })"));
12463 }
12464 
12465 } // end namespace
12466 } // end namespace format
12467 } // end namespace clang
12468